2 * Copyright (c) 2001 Fabrice Bellard
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * video decoding with libavcodec API example
27 * @example decode_video.c
34 #include <libavcodec/avcodec.h>
36 #define INBUF_SIZE 4096
38 static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
44 f = fopen(filename,"w");
45 fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);
46 for (i = 0; i < ysize; i++)
47 fwrite(buf + i * wrap, 1, xsize, f);
51 static void decode(AVCodecContext *dec_ctx, AVFrame *frame, AVPacket *pkt,
57 ret = avcodec_send_packet(dec_ctx, pkt);
59 fprintf(stderr, "Error sending a packet for decoding\n");
64 ret = avcodec_receive_frame(dec_ctx, frame);
65 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
68 fprintf(stderr, "Error during decoding\n");
72 printf("saving frame %3d\n", dec_ctx->frame_number);
75 /* the picture is allocated by the decoder. no need to
77 snprintf(buf, sizeof(buf), "%s-%d", filename, dec_ctx->frame_number);
78 pgm_save(frame->data[0], frame->linesize[0],
79 frame->width, frame->height, buf);
83 int main(int argc, char **argv)
85 const char *filename, *outfilename;
87 AVCodecParserContext *parser;
88 AVCodecContext *c= NULL;
91 uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
98 fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
102 outfilename = argv[2];
104 pkt = av_packet_alloc();
108 /* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
109 memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
111 /* find the MPEG-1 video decoder */
112 codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO);
114 fprintf(stderr, "Codec not found\n");
118 parser = av_parser_init(codec->id);
120 fprintf(stderr, "parser not found\n");
124 c = avcodec_alloc_context3(codec);
126 fprintf(stderr, "Could not allocate video codec context\n");
130 /* For some codecs, such as msmpeg4 and mpeg4, width and height
131 MUST be initialized there because this information is not
132 available in the bitstream. */
135 if (avcodec_open2(c, codec, NULL) < 0) {
136 fprintf(stderr, "Could not open codec\n");
140 f = fopen(filename, "rb");
142 fprintf(stderr, "Could not open %s\n", filename);
146 frame = av_frame_alloc();
148 fprintf(stderr, "Could not allocate video frame\n");
153 /* read raw data from the input file */
154 data_size = fread(inbuf, 1, INBUF_SIZE, f);
158 /* use the parser to split the data into frames */
160 while (data_size > 0) {
161 ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
162 data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
164 fprintf(stderr, "Error while parsing\n");
171 decode(c, frame, pkt, outfilename);
175 /* flush the decoder */
176 decode(c, frame, NULL, outfilename);
180 av_parser_close(parser);
181 avcodec_free_context(&c);
182 av_frame_free(&frame);
183 av_packet_free(&pkt);