FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
decode_video.c
Go to the documentation of this file.
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), "%s-%d", 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;
84  int frame_count;
85  FILE *f;
86  AVFrame *frame;
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 
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 */
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 
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 
179  av_frame_free(&frame);
180 
181  return 0;
182 }
#define NULL
Definition: coverity.c:32
#define INBUF_SIZE
Definition: decode_video.c:36
This structure describes decoded (raw) audio or video data.
Definition: frame.h:187
static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize, char *filename)
Definition: decode_video.c:38
int size
Definition: avcodec.h:1658
int main(int argc, char **argv)
Definition: decode_video.c:79
static AVPacket pkt
void avcodec_register_all(void)
Register all the codecs, parsers and bitstream filters which were enabled at configuration time...
Definition: allcodecs.c:720
AVCodec.
Definition: avcodec.h:3681
uint8_t
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1657
int width
width and height of the video frame
Definition: frame.h:239
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
int capabilities
Codec capabilities.
Definition: avcodec.h:3700
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1827
#define wrap(func)
Definition: neontest.h:65
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:157
Libavcodec external API header.
attribute_deprecated int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, const AVPacket *avpkt)
Decode the video frame of size avpkt->size from avpkt->data into picture.
Definition: utils.c:2227
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer...
Definition: options.c:172
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:218
main external API structure.
Definition: avcodec.h:1732
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: utils.c:3168
void * buf
Definition: avisynth_c.h:690
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: utils.c:1242
#define snprintf
Definition: snprintf.h:34
static int decode_write_frame(const char *outfilename, AVCodecContext *avctx, AVFrame *frame, int *frame_count, AVPacket *pkt, int last)
Definition: decode_video.c:51
#define AV_CODEC_CAP_TRUNCATED
Definition: avcodec.h:995
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:201
static double c[64]
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:769
int len
#define AV_CODEC_FLAG_TRUNCATED
Input bitstream might be truncated at a random location instead of only at frame boundaries.
Definition: avcodec.h:905
int height
Definition: frame.h:239
This structure stores compressed data.
Definition: avcodec.h:1634