2006-09-10 17:02:42 +03:00
|
|
|
/*
|
2017-03-29 14:07:39 +02:00
|
|
|
* Copyright (c) 2001 Fabrice Bellard
|
2006-09-10 17:02:42 +03:00
|
|
|
*
|
2017-03-29 14:07:39 +02:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2006-10-07 18:30:46 +03:00
|
|
|
*
|
2017-03-29 14:07:39 +02:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2006-09-10 17:02:42 +03:00
|
|
|
*
|
2017-03-29 14:07:39 +02:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2006-09-10 17:02:42 +03:00
|
|
|
*/
|
|
|
|
|
2003-03-06 13:32:04 +02:00
|
|
|
/**
|
2010-04-20 17:45:34 +03:00
|
|
|
* @file
|
2016-10-19 21:05:22 +02:00
|
|
|
* video decoding with libavcodec API example
|
2001-07-22 17:18:56 +03:00
|
|
|
*
|
2016-10-19 21:05:22 +02:00
|
|
|
* @example decode_video.c
|
2001-07-22 17:18:56 +03:00
|
|
|
*/
|
2003-03-06 13:32:04 +02:00
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
#include <stdio.h>
|
2016-10-19 21:05:22 +02:00
|
|
|
#include <stdlib.h>
|
2001-07-22 17:18:56 +03:00
|
|
|
#include <string.h>
|
2007-02-18 23:23:50 +02:00
|
|
|
|
2017-03-29 14:07:39 +02:00
|
|
|
#include <libavcodec/avcodec.h>
|
2001-07-22 17:18:56 +03:00
|
|
|
|
|
|
|
#define INBUF_SIZE 4096
|
|
|
|
|
2009-03-31 12:32:59 +03:00
|
|
|
static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
|
|
|
|
char *filename)
|
2001-07-22 17:18:56 +03:00
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
int i;
|
|
|
|
|
2020-06-25 00:01:36 +02:00
|
|
|
f = fopen(filename,"wb");
|
2017-03-29 14:07:39 +02:00
|
|
|
fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);
|
|
|
|
for (i = 0; i < ysize; i++)
|
|
|
|
fwrite(buf + i * wrap, 1, xsize, f);
|
2001-07-22 17:18:56 +03:00
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2016-10-19 21:56:22 +02:00
|
|
|
static void decode(AVCodecContext *dec_ctx, AVFrame *frame, AVPacket *pkt,
|
|
|
|
const char *filename)
|
2017-03-29 14:07:39 +02:00
|
|
|
{
|
|
|
|
char buf[1024];
|
2016-10-19 21:56:22 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = avcodec_send_packet(dec_ctx, pkt);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Error sending a packet for decoding\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-10-19 21:56:22 +02:00
|
|
|
|
2016-10-19 21:56:22 +02:00
|
|
|
while (ret >= 0) {
|
|
|
|
ret = avcodec_receive_frame(dec_ctx, frame);
|
|
|
|
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
|
|
|
|
return;
|
|
|
|
else if (ret < 0) {
|
|
|
|
fprintf(stderr, "Error during decoding\n");
|
2016-10-19 21:56:22 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
2016-10-19 21:56:22 +02:00
|
|
|
|
|
|
|
printf("saving frame %3d\n", dec_ctx->frame_number);
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
/* the picture is allocated by the decoder. no need to
|
|
|
|
free it */
|
2017-04-07 23:05:57 +02:00
|
|
|
snprintf(buf, sizeof(buf), "%s-%d", filename, dec_ctx->frame_number);
|
2016-10-19 21:56:22 +02:00
|
|
|
pgm_save(frame->data[0], frame->linesize[0],
|
|
|
|
frame->width, frame->height, buf);
|
2017-03-29 14:07:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-19 21:05:22 +02:00
|
|
|
int main(int argc, char **argv)
|
2001-07-22 17:18:56 +03:00
|
|
|
{
|
2016-10-19 21:05:22 +02:00
|
|
|
const char *filename, *outfilename;
|
2016-10-20 11:03:20 +02:00
|
|
|
const AVCodec *codec;
|
2016-10-19 21:56:22 +02:00
|
|
|
AVCodecParserContext *parser;
|
2002-09-19 21:24:52 +03:00
|
|
|
AVCodecContext *c= NULL;
|
2001-07-22 17:18:56 +03:00
|
|
|
FILE *f;
|
2017-03-29 14:07:39 +02:00
|
|
|
AVFrame *frame;
|
2015-06-29 23:48:34 +02:00
|
|
|
uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
|
2016-10-19 21:56:22 +02:00
|
|
|
uint8_t *data;
|
|
|
|
size_t data_size;
|
|
|
|
int ret;
|
2021-07-14 04:58:42 +02:00
|
|
|
int eof;
|
2016-10-20 11:03:20 +02:00
|
|
|
AVPacket *pkt;
|
2009-04-10 14:07:52 +03:00
|
|
|
|
2016-10-19 21:05:22 +02:00
|
|
|
if (argc <= 2) {
|
2019-08-21 13:34:49 +02:00
|
|
|
fprintf(stderr, "Usage: %s <input file> <output file>\n"
|
|
|
|
"And check your input file is encoded by mpeg1video please.\n", argv[0]);
|
2016-10-19 21:05:22 +02:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
filename = argv[1];
|
|
|
|
outfilename = argv[2];
|
|
|
|
|
2016-10-20 11:03:20 +02:00
|
|
|
pkt = av_packet_alloc();
|
|
|
|
if (!pkt)
|
|
|
|
exit(1);
|
2001-07-22 17:18:56 +03:00
|
|
|
|
2016-04-27 19:45:23 +02:00
|
|
|
/* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
|
2015-06-29 23:48:34 +02:00
|
|
|
memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
|
2003-08-01 14:01:05 +03:00
|
|
|
|
2016-04-27 19:45:23 +02:00
|
|
|
/* find the MPEG-1 video decoder */
|
2012-08-05 12:11:04 +03:00
|
|
|
codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO);
|
2001-07-22 17:18:56 +03:00
|
|
|
if (!codec) {
|
2017-03-29 14:07:39 +02:00
|
|
|
fprintf(stderr, "Codec not found\n");
|
2001-07-22 17:18:56 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-10-19 21:56:22 +02:00
|
|
|
parser = av_parser_init(codec->id);
|
|
|
|
if (!parser) {
|
|
|
|
fprintf(stderr, "parser not found\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2011-06-18 14:40:48 +03:00
|
|
|
c = avcodec_alloc_context3(codec);
|
2017-03-29 14:07:39 +02:00
|
|
|
if (!c) {
|
|
|
|
fprintf(stderr, "Could not allocate video codec context\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
|
2007-06-12 12:29:25 +03:00
|
|
|
/* For some codecs, such as msmpeg4 and mpeg4, width and height
|
|
|
|
MUST be initialized there because this information is not
|
|
|
|
available in the bitstream. */
|
2001-07-22 17:18:56 +03:00
|
|
|
|
|
|
|
/* open it */
|
2012-02-19 12:34:40 +03:00
|
|
|
if (avcodec_open2(c, codec, NULL) < 0) {
|
2017-03-29 14:07:39 +02:00
|
|
|
fprintf(stderr, "Could not open codec\n");
|
2001-07-22 17:18:56 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2004-04-09 18:04:51 +03:00
|
|
|
f = fopen(filename, "rb");
|
2001-07-22 17:18:56 +03:00
|
|
|
if (!f) {
|
2017-03-29 14:07:39 +02:00
|
|
|
fprintf(stderr, "Could not open %s\n", filename);
|
2001-07-22 17:18:56 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2017-03-29 14:07:39 +02:00
|
|
|
frame = av_frame_alloc();
|
|
|
|
if (!frame) {
|
|
|
|
fprintf(stderr, "Could not allocate video frame\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2021-07-14 04:58:42 +02:00
|
|
|
do {
|
2016-10-19 21:56:22 +02:00
|
|
|
/* read raw data from the input file */
|
|
|
|
data_size = fread(inbuf, 1, INBUF_SIZE, f);
|
2021-07-14 04:58:42 +02:00
|
|
|
if (ferror(f))
|
2001-07-22 17:18:56 +03:00
|
|
|
break;
|
2021-07-14 04:58:42 +02:00
|
|
|
eof = !data_size;
|
2001-07-22 17:18:56 +03:00
|
|
|
|
2016-10-19 21:56:22 +02:00
|
|
|
/* use the parser to split the data into frames */
|
|
|
|
data = inbuf;
|
2021-07-14 04:58:42 +02:00
|
|
|
while (data_size > 0 || eof) {
|
2016-10-20 11:03:20 +02:00
|
|
|
ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
|
2016-10-19 21:56:22 +02:00
|
|
|
data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Error while parsing\n");
|
2001-07-22 17:18:56 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
2016-10-19 21:56:22 +02:00
|
|
|
data += ret;
|
|
|
|
data_size -= ret;
|
|
|
|
|
2016-10-20 11:03:20 +02:00
|
|
|
if (pkt->size)
|
2017-04-04 19:47:09 +02:00
|
|
|
decode(c, frame, pkt, outfilename);
|
2021-07-14 04:58:42 +02:00
|
|
|
else if (eof)
|
|
|
|
break;
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
2021-07-14 04:58:42 +02:00
|
|
|
} while (!eof);
|
2001-07-22 17:18:56 +03:00
|
|
|
|
2016-10-19 21:56:22 +02:00
|
|
|
/* flush the decoder */
|
2017-04-04 19:41:23 +02:00
|
|
|
decode(c, frame, NULL, outfilename);
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
fclose(f);
|
|
|
|
|
2016-10-19 21:56:22 +02:00
|
|
|
av_parser_close(parser);
|
2016-07-20 16:10:17 +02:00
|
|
|
avcodec_free_context(&c);
|
2017-03-29 14:07:39 +02:00
|
|
|
av_frame_free(&frame);
|
2016-10-20 11:03:20 +02:00
|
|
|
av_packet_free(&pkt);
|
2001-07-22 17:18:56 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|