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 int decode_write_frame(const char *outfilename, AVCodecContext *avctx,
52 AVFrame *frame, int *frame_count, AVPacket *pkt, int last)
57 len = avcodec_decode_video2(avctx, frame, &got_frame, pkt);
59 fprintf(stderr, "Error while decoding frame %d\n", *frame_count);
63 printf("Saving %sframe %3d\n", last ? "last " : "", *frame_count);
66 /* the picture is allocated by the decoder, no need to free it */
67 snprintf(buf, sizeof(buf), outfilename, *frame_count);
68 pgm_save(frame->data[0], frame->linesize[0],
69 frame->width, frame->height, buf);
79 int main(int argc, char **argv)
81 const char *filename, *outfilename;
83 AVCodecContext *c= NULL;
87 uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
91 fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
95 outfilename = argv[2];
97 avcodec_register_all();
99 av_init_packet(&avpkt);
101 /* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
102 memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
104 /* find the MPEG-1 video decoder */
105 codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO);
107 fprintf(stderr, "Codec not found\n");
111 c = avcodec_alloc_context3(codec);
113 fprintf(stderr, "Could not allocate video codec context\n");
117 if (codec->capabilities & AV_CODEC_CAP_TRUNCATED)
118 c->flags |= AV_CODEC_FLAG_TRUNCATED; // we do not send complete frames
120 /* For some codecs, such as msmpeg4 and mpeg4, width and height
121 MUST be initialized there because this information is not
122 available in the bitstream. */
125 if (avcodec_open2(c, codec, NULL) < 0) {
126 fprintf(stderr, "Could not open codec\n");
130 f = fopen(filename, "rb");
132 fprintf(stderr, "Could not open %s\n", filename);
136 frame = av_frame_alloc();
138 fprintf(stderr, "Could not allocate video frame\n");
144 avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
148 /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
149 and this is the only method to use them because you cannot
150 know the compressed data size before analysing it.
152 BUT some other codecs (msmpeg4, mpeg4) are inherently frame
153 based, so you must call them with all the data for one
154 frame exactly. You must also initialize 'width' and
155 'height' before initializing them. */
157 /* NOTE2: some codecs allow the raw parameters (frame size,
158 sample rate) to be changed at any frame. We handle this, so
159 you should also take care of it */
161 /* here, we use a stream based decoder (mpeg1video), so we
162 feed decoder and see if it could decode a frame */
164 while (avpkt.size > 0)
165 if (decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 0) < 0)
169 /* Some codecs, such as MPEG, transmit the I- and P-frame with a
170 latency of one frame. You must do the following to have a
171 chance to get the last frame of the video. */
174 decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 1);
178 avcodec_free_context(&c);
179 av_frame_free(&frame);