FFmpeg
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 libavcodec video decoding API usage example
25  * @example decode_video.c *
26  *
27  * Read from an MPEG1 video file, decode frames, and generate PGM images as
28  * output.
29  */
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 
35 #include <libavcodec/avcodec.h>
36 
37 #define INBUF_SIZE 4096
38 
39 static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
40  char *filename)
41 {
42  FILE *f;
43  int i;
44 
45  f = fopen(filename,"wb");
46  fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);
47  for (i = 0; i < ysize; i++)
48  fwrite(buf + i * wrap, 1, xsize, f);
49  fclose(f);
50 }
51 
53  const char *filename)
54 {
55  char buf[1024];
56  int ret;
57 
59  if (ret < 0) {
60  fprintf(stderr, "Error sending a packet for decoding\n");
61  exit(1);
62  }
63 
64  while (ret >= 0) {
66  if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
67  return;
68  else if (ret < 0) {
69  fprintf(stderr, "Error during decoding\n");
70  exit(1);
71  }
72 
73  printf("saving frame %3"PRId64"\n", dec_ctx->frame_num);
74  fflush(stdout);
75 
76  /* the picture is allocated by the decoder. no need to
77  free it */
78  snprintf(buf, sizeof(buf), "%s-%"PRId64, filename, dec_ctx->frame_num);
79  pgm_save(frame->data[0], frame->linesize[0],
80  frame->width, frame->height, buf);
81  }
82 }
83 
84 int main(int argc, char **argv)
85 {
86  const char *filename, *outfilename;
87  const AVCodec *codec;
88  AVCodecParserContext *parser;
90  FILE *f;
91  AVFrame *frame;
93  uint8_t *data;
94  size_t data_size;
95  int ret;
96  int eof;
97  AVPacket *pkt;
98 
99  if (argc <= 2) {
100  fprintf(stderr, "Usage: %s <input file> <output file>\n"
101  "And check your input file is encoded by mpeg1video please.\n", argv[0]);
102  exit(0);
103  }
104  filename = argv[1];
105  outfilename = argv[2];
106 
107  pkt = av_packet_alloc();
108  if (!pkt)
109  exit(1);
110 
111  /* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
112  memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
113 
114  /* find the MPEG-1 video decoder */
116  if (!codec) {
117  fprintf(stderr, "Codec not found\n");
118  exit(1);
119  }
120 
121  parser = av_parser_init(codec->id);
122  if (!parser) {
123  fprintf(stderr, "parser not found\n");
124  exit(1);
125  }
126 
127  c = avcodec_alloc_context3(codec);
128  if (!c) {
129  fprintf(stderr, "Could not allocate video codec context\n");
130  exit(1);
131  }
132 
133  /* For some codecs, such as msmpeg4 and mpeg4, width and height
134  MUST be initialized there because this information is not
135  available in the bitstream. */
136 
137  /* open it */
138  if (avcodec_open2(c, codec, NULL) < 0) {
139  fprintf(stderr, "Could not open codec\n");
140  exit(1);
141  }
142 
143  f = fopen(filename, "rb");
144  if (!f) {
145  fprintf(stderr, "Could not open %s\n", filename);
146  exit(1);
147  }
148 
149  frame = av_frame_alloc();
150  if (!frame) {
151  fprintf(stderr, "Could not allocate video frame\n");
152  exit(1);
153  }
154 
155  do {
156  /* read raw data from the input file */
157  data_size = fread(inbuf, 1, INBUF_SIZE, f);
158  if (ferror(f))
159  break;
160  eof = !data_size;
161 
162  /* use the parser to split the data into frames */
163  data = inbuf;
164  while (data_size > 0 || eof) {
165  ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
166  data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
167  if (ret < 0) {
168  fprintf(stderr, "Error while parsing\n");
169  exit(1);
170  }
171  data += ret;
172  data_size -= ret;
173 
174  if (pkt->size)
175  decode(c, frame, pkt, outfilename);
176  else if (eof)
177  break;
178  }
179  } while (!eof);
180 
181  /* flush the decoder */
182  decode(c, frame, NULL, outfilename);
183 
184  fclose(f);
185 
186  av_parser_close(parser);
190 
191  return 0;
192 }
AVCodec
AVCodec.
Definition: codec.h:187
dec_ctx
static AVCodecContext * dec_ctx
Definition: decode_filter_audio.c:46
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:100
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
AVFrame::width
int width
Definition: frame.h:412
AVPacket::data
uint8_t * data
Definition: packet.h:522
pgm_save
static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize, char *filename)
Definition: decode_video.c:39
data
const char data[16]
Definition: mxf.c:148
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:74
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
wrap
#define wrap(func)
Definition: neontest.h:65
av_parser_init
AVCodecParserContext * av_parser_init(int codec_id)
Definition: parser.c:32
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:88
pkt
AVPacket * pkt
Definition: movenc.c:59
avcodec_alloc_context3
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:154
avcodec_receive_frame
int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder or encoder (when the AV_CODEC_FLAG_RECON_FRAME flag is used...
Definition: avcodec.c:715
frame
static AVFrame * frame
Definition: demux_decode.c:54
decode
static void decode(AVCodecContext *dec_ctx, AVFrame *frame, AVPacket *pkt, const char *filename)
Definition: decode_video.c:52
NULL
#define NULL
Definition: coverity.c:32
avcodec_free_context
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:169
avcodec_open2
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: avcodec.c:128
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AV_CODEC_ID_MPEG1VIDEO
@ AV_CODEC_ID_MPEG1VIDEO
Definition: codec_id.h:53
avcodec_find_decoder
const AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: allcodecs.c:981
f
f
Definition: af_crystalizer.c:121
AVPacket::size
int size
Definition: packet.h:523
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
printf
printf("static const uint8_t my_array[100] = {\n")
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:63
AVCodec::id
enum AVCodecID id
Definition: codec.h:201
avcodec_send_packet
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition: decode.c:709
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
avcodec.h
AVCodecParserContext
Definition: avcodec.h:2781
AVCodecContext::frame_num
int64_t frame_num
Frame counter, set by libavcodec.
Definition: avcodec.h:2122
ret
ret
Definition: filter_design.txt:187
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
AVCodecContext
main external API structure.
Definition: avcodec.h:445
AVFrame::height
int height
Definition: frame.h:412
av_parser_parse2
int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int64_t pts, int64_t dts, int64_t pos)
Parse a packet.
Definition: parser.c:115
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:385
main
int main(int argc, char **argv)
Definition: decode_video.c:84
snprintf
#define snprintf
Definition: snprintf.h:34
INBUF_SIZE
#define INBUF_SIZE
Definition: decode_video.c:37
av_parser_close
void av_parser_close(AVCodecParserContext *s)
Definition: parser.c:193