FFmpeg
api-codec-param-test.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Matthieu Bouron <matthieu.bouron stupeflix.com>
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 #include <stdio.h>
24 #include "libavformat/avformat.h"
25 #include "libavutil/pixdesc.h"
26 #include "libavcodec/internal.h"
27 #include "libavutil/avassert.h"
28 #include "libavutil/opt.h"
29 
31 {
32  int ret = 0;
33  int got_frame = 0;
34  AVFrame *frame = NULL;
35  int skip_frame = codec_ctx->skip_frame;
36 
37  if (!avcodec_is_open(codec_ctx)) {
38  const AVCodec *codec = avcodec_find_decoder(codec_ctx->codec_id);
39 
40  ret = avcodec_open2(codec_ctx, codec, NULL);
41  if (ret < 0) {
42  av_log(codec_ctx, AV_LOG_ERROR, "Failed to open codec\n");
43  goto end;
44  }
45  }
46 
48  if (!frame) {
49  av_log(NULL, AV_LOG_ERROR, "Failed to allocate frame\n");
50  goto end;
51  }
52 
54  codec_ctx->skip_frame = AVDISCARD_ALL;
55  }
56 
57  do {
58  ret = avcodec_decode_video2(codec_ctx, frame, &got_frame, pkt);
59  av_assert0(decode || (!decode && !got_frame));
60  if (ret < 0)
61  break;
62  pkt->data += ret;
63  pkt->size -= ret;
64 
65  if (got_frame) {
66  break;
67  }
68  } while (pkt->size > 0);
69 
70 end:
71  codec_ctx->skip_frame = skip_frame;
72 
74  return ret;
75 }
76 
78 {
79  int ret = 0;
80  int i, done = 0;
81  AVPacket pkt;
82 
84 
85  while (!done) {
86  AVCodecContext *codec_ctx = NULL;
87  AVStream *st;
88 
89  if ((ret = av_read_frame(fmt_ctx, &pkt)) < 0) {
90  av_log(fmt_ctx, AV_LOG_ERROR, "Failed to read frame\n");
91  goto end;
92  }
93 
95  codec_ctx = st->codec;
96 
97  /* Writing to AVStream.codec_info_nb_frames must not be done by
98  * user applications. It is done here for testing purposing as
99  * find_video_stream_info tries to mimic avformat_find_stream_info
100  * which writes to this field.
101  * */
102  if (codec_ctx->codec_type != AVMEDIA_TYPE_VIDEO ||
103  st->codec_info_nb_frames++ > 0) {
105  continue;
106  }
107 
108  ret = try_decode_video_frame(codec_ctx, &pkt, decode);
109  if (ret < 0) {
110  av_log(fmt_ctx, AV_LOG_ERROR, "Failed to decode video frame\n");
111  goto end;
112  }
113 
115 
116  /* check if all video streams have demuxed a packet */
117  done = 1;
118  for (i = 0; i < fmt_ctx->nb_streams; i++) {
119  st = fmt_ctx->streams[i];
120  codec_ctx = st->codec;
121 
122  if (codec_ctx->codec_type != AVMEDIA_TYPE_VIDEO)
123  continue;
124 
125  done &= st->codec_info_nb_frames > 0;
126  }
127  }
128 
129 end:
131 
132  /* close all codecs opened in try_decode_video_frame */
133  for (i = 0; i < fmt_ctx->nb_streams; i++) {
134  AVStream *st = fmt_ctx->streams[i];
135  avcodec_close(st->codec);
136  }
137 
138  return ret < 0;
139 }
140 
142 {
143  int i;
144 
145  for (i = 0; i < fmt_ctx->nb_streams; i++) {
146  const AVOption *opt = NULL;
147  const AVStream *st = fmt_ctx->streams[i];
148  AVCodecContext *codec_ctx = st->codec;
149 
150  printf("stream=%d, decode=%d\n", i, decode);
151  while (opt = av_opt_next(codec_ctx, opt)) {
152  uint8_t *str;
153 
154  if (opt->type == AV_OPT_TYPE_CONST)
155  continue;
156 
157  if (!strcmp(opt->name, "frame_number"))
158  continue;
159 
160  if (av_opt_get(codec_ctx, opt->name, 0, &str) >= 0) {
161  printf(" %s=%s\n", opt->name, str);
162  av_free(str);
163  }
164  }
165  }
166 }
167 
168 static int open_and_probe_video_streams(AVFormatContext **fmt_ctx, const char *filename, int decode)
169 {
170  int ret = 0;
171 
172  ret = avformat_open_input(fmt_ctx, filename, NULL, NULL);
173  if (ret < 0) {
174  av_log(NULL, AV_LOG_ERROR, "Failed to open input '%s'", filename);
175  goto end;
176  }
177 
179  if (ret < 0) {
180  goto end;
181  }
182 
184 
185 end:
186  return ret;
187 }
188 
189 static int check_video_streams(const AVFormatContext *fmt_ctx1, const AVFormatContext *fmt_ctx2)
190 {
191  int i;
192  int ret = 0;
193 
194  av_assert0(fmt_ctx1->nb_streams == fmt_ctx2->nb_streams);
195  for (i = 0; i < fmt_ctx1->nb_streams; i++) {
196  const AVOption *opt = NULL;
197  const AVStream *st1 = fmt_ctx1->streams[i];
198  const AVStream *st2 = fmt_ctx2->streams[i];
199  AVCodecContext *codec_ctx1 = st1->codec;
200  AVCodecContext *codec_ctx2 = st2->codec;
201 
202  if (codec_ctx1->codec_type != AVMEDIA_TYPE_VIDEO)
203  continue;
204 
205  while (opt = av_opt_next(codec_ctx1, opt)) {
206  uint8_t *str1 = NULL, *str2 = NULL;
207 
208  if (opt->type == AV_OPT_TYPE_CONST)
209  continue;
210 
211  if (!strcmp(opt->name, "frame_number"))
212  continue;
213 
214  av_assert0(av_opt_get(codec_ctx1, opt->name, 0, &str1) >= 0);
215  av_assert0(av_opt_get(codec_ctx2, opt->name, 0, &str2) >= 0);
216  if (strcmp(str1, str2)) {
217  av_log(NULL, AV_LOG_ERROR, "Field %s differs: %s %s", opt->name, str1, str2);
218  ret = AVERROR(EINVAL);
219  }
220  av_free(str1);
221  av_free(str2);
222  }
223  }
224 
225  return ret;
226 }
227 
228 int main(int argc, char* argv[])
229 {
230  int ret = 0;
232  AVFormatContext *fmt_ctx_no_decode = NULL;
233 
234  if (argc < 2) {
235  av_log(NULL, AV_LOG_ERROR, "Usage: %s <input>\n", argv[0]);
236  return -1;
237  }
238 
239  if ((ret = open_and_probe_video_streams(&fmt_ctx_no_decode, argv[1], 0)) < 0) {
240  av_log(NULL, AV_LOG_ERROR, "Failed to probe '%s' without frame decoding\n", argv[1]);
241  goto end;
242  }
243 
244  if ((ret = open_and_probe_video_streams(&fmt_ctx, argv[1], 1)) < 0) {
245  av_log(NULL, AV_LOG_ERROR, "Failed to probe '%s' with frame decoding\n", argv[1]);
246  goto end;
247  }
248 
249  ret = check_video_streams(fmt_ctx, fmt_ctx_no_decode);
250 
251 end:
253  avformat_close_input(&fmt_ctx_no_decode);
254 
255  return ret;
256 }
avcodec_close
int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
Definition: utils.c:1117
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:599
AVCodec
AVCodec.
Definition: avcodec.h:3481
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
opt.h
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
end
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
pixdesc.h
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1410
internal.h
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
AVOption
AVOption.
Definition: opt.h:246
av_read_frame
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1785
avcodec_is_open
int avcodec_is_open(AVCodecContext *s)
Definition: utils.c:1938
dump_video_streams
static void dump_video_streams(const AVFormatContext *fmt_ctx, int decode)
Definition: api-codec-param-test.c:141
avformat_close_input
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:4452
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:1574
AVCodecContext::skip_frame
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:3040
find_video_stream_info
static int find_video_stream_info(AVFormatContext *fmt_ctx, int decode)
Definition: api-codec-param-test.c:77
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:1575
AVDISCARD_ALL
@ AVDISCARD_ALL
discard all
Definition: avcodec.h:811
AVFormatContext
Format I/O context.
Definition: avformat.h:1342
NULL
#define NULL
Definition: coverity.c:32
fmt_ctx
static AVFormatContext * fmt_ctx
Definition: demuxing_decoding.c:37
avcodec_open2
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: utils.c:565
AVFormatContext::nb_streams
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1398
AVPacket::size
int size
Definition: avcodec.h:1478
printf
printf("static const uint8_t my_array[100] = {\n")
AVOption::name
const char * name
Definition: opt.h:247
avcodec_find_decoder
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: allcodecs.c:890
open_and_probe_video_streams
static int open_and_probe_video_streams(AVFormatContext **fmt_ctx, const char *filename, int decode)
Definition: api-codec-param-test.c:168
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
avformat_open_input
int avformat_open_input(AVFormatContext **ps, const char *url, ff_const59 AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:540
check_video_streams
static int check_video_streams(const AVFormatContext *fmt_ctx1, const AVFormatContext *fmt_ctx2)
Definition: api-codec-param-test.c:189
avcodec_decode_video2
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: decode.c:898
uint8_t
uint8_t
Definition: audio_convert.c:194
av_opt_next
const AVOption * av_opt_next(const void *obj, const AVOption *last)
Iterate over all AVOptions belonging to obj.
Definition: opt.c:45
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:870
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
avformat.h
AVOption::type
enum AVOptionType type
Definition: opt.h:260
AVCodecContext
main external API structure.
Definition: avcodec.h:1565
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
AVPacket::stream_index
int stream_index
Definition: avcodec.h:1479
AVCodecContext::codec_type
enum AVMediaType codec_type
Definition: avcodec.h:1573
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
av_opt_get
int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
Definition: opt.c:761
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:232
AVStream::codec_info_nb_frames
int codec_info_nb_frames
Number of frames that have been demuxed during avformat_find_stream_info()
Definition: avformat.h:1085
avpriv_codec_get_cap_skip_frame_fill_param
int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec *codec)
Definition: utils.c:506
main
int main(int argc, char *argv[])
Definition: api-codec-param-test.c:228
av_init_packet
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
try_decode_video_frame
static int try_decode_video_frame(AVCodecContext *codec_ctx, AVPacket *pkt, int decode)
Definition: api-codec-param-test.c:30