FFmpeg
Loading...
Searching...
No Matches
decode_simple.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19/* shared code for simple demux/decode tools */
20
21#include <stdlib.h>
22#include <string.h>
23
24#include "decode_simple.h"
25
27
28#include "libavcodec/avcodec.h"
29#include "libavcodec/packet.h"
30
31#include "libavutil/dict.h"
32#include "libavutil/error.h"
33#include "libavutil/frame.h"
34
35static int decode_read(DecodeContext *dc, int flush)
36{
37 const int ret_done = flush ? AVERROR_EOF : AVERROR(EAGAIN);
38 int ret = 0;
39
40 while (ret >= 0 &&
41 (dc->max_frames == 0 || dc->decoder->frame_num < dc->max_frames)) {
42 ret = avcodec_receive_frame(dc->decoder, dc->frame);
43 if (ret < 0) {
44 if (ret == AVERROR_EOF) {
45 int err = dc->process_frame(dc, NULL);
46 if (err < 0)
47 return err;
48 }
49
50 return (ret == ret_done) ? 0 : ret;
51 }
52
53 ret = dc->process_frame(dc, dc->frame);
55 if (ret < 0)
56 return ret;
57
58 if (dc->max_frames && dc->decoder->frame_num == dc->max_frames)
59 return 1;
60 }
61
62 return (dc->max_frames == 0 || dc->decoder->frame_num < dc->max_frames) ? 0 : 1;
63}
64
66{
67 int ret;
68
69 ret = avcodec_open2(dc->decoder, NULL, &dc->decoder_opts);
70 if (ret < 0)
71 return ret;
72
73 while (ret >= 0) {
74 ret = av_read_frame(dc->demuxer, dc->pkt);
75 if (ret < 0)
76 break;
77 if (dc->pkt->stream_index != dc->stream->index) {
79 continue;
80 }
81
82 ret = avcodec_send_packet(dc->decoder, dc->pkt);
83 if (ret < 0) {
84 fprintf(stderr, "Error decoding: %d\n", ret);
85 return ret;
86 }
88
89 ret = decode_read(dc, 0);
90 if (ret < 0) {
91 fprintf(stderr, "Error decoding: %d\n", ret);
92 return ret;
93 } else if (ret > 0)
94 goto finish;
95 }
96
98 if (ret >= 0)
99 ret = decode_read(dc, 1);
100 if (ret < 0) {
101 fprintf(stderr, "Error flushing: %d\n", ret);
102 return ret;
103 }
104
105finish:
106 return dc->process_frame(dc, NULL);
107}
108
110{
112
113 av_frame_free(&dc->frame);
114 av_packet_free(&dc->pkt);
115
118}
119
120int ds_open(DecodeContext *dc, const char *url, int stream_idx)
121{
122 const AVCodec *codec;
123 int ret;
124
125 memset(dc, 0, sizeof(*dc));
126
127 dc->pkt = av_packet_alloc();
128 dc->frame = av_frame_alloc();
129 if (!dc->pkt || !dc->frame) {
130 ret = AVERROR(ENOMEM);
131 goto fail;
132 }
133
134 ret = avformat_open_input(&dc->demuxer, url, NULL, NULL);
135 if (ret < 0) {
136 fprintf(stderr, "Error opening input file: %d\n", ret);
137 return ret;
138 }
139
140 if (stream_idx < 0 || stream_idx >= dc->demuxer->nb_streams)
141 return AVERROR(EINVAL);
142
143 dc->stream = dc->demuxer->streams[stream_idx];
144
146 if (!codec)
148
149 dc->decoder = avcodec_alloc_context3(codec);
150 if (!dc->decoder)
151 return AVERROR(ENOMEM);
152
154 if (ret < 0)
155 goto fail;
156
157 return 0;
158
159fail:
160 ds_free(dc);
161 return ret;
162}
static void finish(void)
Libavcodec external API header.
Main libavformat public API header.
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
Definition codec_par.c:206
#define NULL
Definition coverity.c:32
int ds_open(DecodeContext *dc, const char *url, int stream_idx)
void ds_free(DecodeContext *dc)
int ds_run(DecodeContext *dc)
static int decode_read(DecodeContext *dc, int flush)
Public dictionary API.
void(* flush)(AVBSFContext *ctx)
Definition dts2pts.c:610
error code definitions
reference-counted frame API
#define fail
Definition test.h:479
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition avcodec.c:144
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition options.c:149
const AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition allcodecs.c:990
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:164
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Alias for avcodec_receive_frame_flags(avctx, frame, 0).
Definition avcodec.c:720
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition decode.c:730
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition packet.c:74
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition demux.c:1588
int avformat_open_input(AVFormatContext **ps, const char *url, const AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition demux.c:231
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition demux.c:377
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:233
#define AVERROR_DECODER_NOT_FOUND
Decoder not found.
Definition error.h:54
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:496
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
int64_t frame_num
Frame counter, set by libavcodec.
Definition avcodec.h:1883
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
AVCodec.
Definition codec.h:175
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition avformat.h:1389
AVStream ** streams
A list of all streams in the file.
Definition avformat.h:1401
int stream_index
Definition packet.h:605
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int index
stream index in AVFormatContext
Definition avformat.h:772
AVDictionary * decoder_opts
AVPacket * pkt
int(* process_frame)(struct DecodeContext *dc, AVFrame *frame)
AVCodecContext * decoder
AVFormatContext * demuxer
AVStream * stream
AVFrame * frame