]> git.sesse.net Git - ffmpeg/blob - doc/examples/decode_video.c
Merge commit '4537647c0429fe7c8ee655ac3fda856ba67f58a0'
[ffmpeg] / doc / examples / decode_video.c
1 /*
2  * Copyright (c) 2001 Fabrice Bellard
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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
20  * THE SOFTWARE.
21  */
22
23 /**
24  * @file
25  * video decoding with libavcodec API example
26  *
27  * @example decode_video.c
28  */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include <libavcodec/avcodec.h>
35
36 #define INBUF_SIZE 4096
37
38 static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
39                      char *filename)
40 {
41     FILE *f;
42     int i;
43
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);
48     fclose(f);
49 }
50
51 static int decode_write_frame(const char *outfilename, AVCodecContext *avctx,
52                               AVFrame *frame, int *frame_count, AVPacket *pkt, int last)
53 {
54     int len, got_frame;
55     char buf[1024];
56
57     len = avcodec_decode_video2(avctx, frame, &got_frame, pkt);
58     if (len < 0) {
59         fprintf(stderr, "Error while decoding frame %d\n", *frame_count);
60         return len;
61     }
62     if (got_frame) {
63         printf("Saving %sframe %3d\n", last ? "last " : "", *frame_count);
64         fflush(stdout);
65
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);
70         (*frame_count)++;
71     }
72     if (pkt->data) {
73         pkt->size -= len;
74         pkt->data += len;
75     }
76     return 0;
77 }
78
79 int main(int argc, char **argv)
80 {
81     const char *filename, *outfilename;
82     const AVCodec *codec;
83     AVCodecContext *c= NULL;
84     int frame_count;
85     FILE *f;
86     AVFrame *frame;
87     uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
88     AVPacket avpkt;
89
90     if (argc <= 2) {
91         fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
92         exit(0);
93     }
94     filename    = argv[1];
95     outfilename = argv[2];
96
97     avcodec_register_all();
98
99     av_init_packet(&avpkt);
100
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);
103
104     /* find the MPEG-1 video decoder */
105     codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO);
106     if (!codec) {
107         fprintf(stderr, "Codec not found\n");
108         exit(1);
109     }
110
111     c = avcodec_alloc_context3(codec);
112     if (!c) {
113         fprintf(stderr, "Could not allocate video codec context\n");
114         exit(1);
115     }
116
117     if (codec->capabilities & AV_CODEC_CAP_TRUNCATED)
118         c->flags |= AV_CODEC_FLAG_TRUNCATED; // we do not send complete frames
119
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. */
123
124     /* open it */
125     if (avcodec_open2(c, codec, NULL) < 0) {
126         fprintf(stderr, "Could not open codec\n");
127         exit(1);
128     }
129
130     f = fopen(filename, "rb");
131     if (!f) {
132         fprintf(stderr, "Could not open %s\n", filename);
133         exit(1);
134     }
135
136     frame = av_frame_alloc();
137     if (!frame) {
138         fprintf(stderr, "Could not allocate video frame\n");
139         exit(1);
140     }
141
142     frame_count = 0;
143     for (;;) {
144         avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
145         if (avpkt.size == 0)
146             break;
147
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.
151
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. */
156
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 */
160
161         /* here, we use a stream based decoder (mpeg1video), so we
162            feed decoder and see if it could decode a frame */
163         avpkt.data = inbuf;
164         while (avpkt.size > 0)
165             if (decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 0) < 0)
166                 exit(1);
167     }
168
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. */
172     avpkt.data = NULL;
173     avpkt.size = 0;
174     decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 1);
175
176     fclose(f);
177
178     avcodec_free_context(&c);
179     av_frame_free(&frame);
180
181     return 0;
182 }