FFmpeg 9.0
Loading...
Searching...
No Matches
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
39static 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 if (!f) {
47 fprintf(stderr, "Could not open %s\n", filename);
48 return;
49 }
50 fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);
51 for (i = 0; i < ysize; i++)
52 fwrite(buf + i * wrap, 1, xsize, f);
53 fclose(f);
54}
55
57 const char *filename)
58{
59 char buf[1024];
60 int ret;
61
63 if (ret < 0) {
64 fprintf(stderr, "Error sending a packet for decoding\n");
65 exit(1);
66 }
67
68 while (ret >= 0) {
70 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
71 return;
72 else if (ret < 0) {
73 fprintf(stderr, "Error during decoding\n");
74 exit(1);
75 }
76
77 printf("saving frame %3"PRId64"\n", dec_ctx->frame_num);
78 fflush(stdout);
79
80 /* the picture is allocated by the decoder. no need to
81 free it */
82 snprintf(buf, sizeof(buf), "%s-%"PRId64, filename, dec_ctx->frame_num);
83 pgm_save(frame->data[0], frame->linesize[0],
84 frame->width, frame->height, buf);
85 }
86}
87
88int main(int argc, char **argv)
89{
90 const char *filename, *outfilename;
91 const AVCodec *codec;
93 AVCodecContext *c= NULL;
94 FILE *f;
97 uint8_t *data;
98 size_t data_size;
99 int ret;
100 int eof;
101 AVPacket *pkt;
102
103 if (argc <= 2) {
104 fprintf(stderr, "Usage: %s <input file> <output file>\n"
105 "And check your input file is encoded by mpeg1video please.\n", argv[0]);
106 exit(0);
107 }
108 filename = argv[1];
109 outfilename = argv[2];
110
112 if (!pkt)
113 exit(1);
114
115 /* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
116 memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
117
118 /* find the MPEG-1 video decoder */
120 if (!codec) {
121 fprintf(stderr, "Codec not found\n");
122 exit(1);
123 }
124
125 parser = av_parser_init(codec->id);
126 if (!parser) {
127 fprintf(stderr, "parser not found\n");
128 exit(1);
129 }
130
131 c = avcodec_alloc_context3(codec);
132 if (!c) {
133 fprintf(stderr, "Could not allocate video codec context\n");
134 exit(1);
135 }
136
137 /* For some codecs, such as msmpeg4 and mpeg4, width and height
138 MUST be initialized there because this information is not
139 available in the bitstream. */
140
141 /* open it */
142 if (avcodec_open2(c, codec, NULL) < 0) {
143 fprintf(stderr, "Could not open codec\n");
144 exit(1);
145 }
146
147 f = fopen(filename, "rb");
148 if (!f) {
149 fprintf(stderr, "Could not open %s\n", filename);
150 exit(1);
151 }
152
154 if (!frame) {
155 fprintf(stderr, "Could not allocate video frame\n");
156 exit(1);
157 }
158
159 do {
160 /* read raw data from the input file */
161 data_size = fread(inbuf, 1, INBUF_SIZE, f);
162 if (ferror(f))
163 break;
164 eof = !data_size;
165
166 /* use the parser to split the data into frames */
167 data = inbuf;
168 while (data_size > 0 || eof) {
169 ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
170 data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
171 if (ret < 0) {
172 fprintf(stderr, "Error while parsing\n");
173 exit(1);
174 }
175 data += ret;
176 data_size -= ret;
177
178 if (pkt->size)
179 decode(c, frame, pkt, outfilename);
180 else if (eof)
181 break;
182 }
183 } while (!eof);
184
185 /* flush the decoder */
186 decode(c, frame, NULL, outfilename);
187
188 fclose(f);
189
190 av_parser_close(parser);
194
195 return 0;
196}
Libavcodec external API header.
int main(int argc, char *argv[])
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
static AVCodecContext * dec_ctx
#define INBUF_SIZE
static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize, char *filename)
static AVPacket * pkt
static AVFrame * frame
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
const AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
@ AV_CODEC_ID_MPEG1VIDEO
Definition codec_id.h:51
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Alias for avcodec_receive_frame_flags(avctx, frame, 0).
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
AVCodecParserContext * av_parser_init(enum AVCodecID codec_id)
void av_parser_close(AVCodecParserContext *s)
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.
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
main external API structure.
Definition avcodec.h:443
AVCodec.
Definition codec.h:169
enum AVCodecID id
Definition codec.h:183
This structure describes decoded (raw) audio or video data.
Definition frame.h:466
This structure stores compressed data.
Definition packet.h:580