2 * Copyright (c) 2015 Matthieu Bouron <matthieu.bouron stupeflix.com>
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
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"
30 static int try_decode_video_frame(AVCodecContext *codec_ctx, AVPacket *pkt, int decode)
34 AVFrame *frame = NULL;
35 int skip_frame = codec_ctx->skip_frame;
37 if (!avcodec_is_open(codec_ctx)) {
38 const AVCodec *codec = avcodec_find_decoder(codec_ctx->codec_id);
40 ret = avcodec_open2(codec_ctx, codec, NULL);
42 av_log(codec_ctx, AV_LOG_ERROR, "Failed to open codec\n");
47 frame = av_frame_alloc();
49 av_log(NULL, AV_LOG_ERROR, "Failed to allocate frame\n");
53 if (!decode && avpriv_codec_get_cap_skip_frame_fill_param(codec_ctx->codec)) {
54 codec_ctx->skip_frame = AVDISCARD_ALL;
58 ret = avcodec_decode_video2(codec_ctx, frame, &got_frame, pkt);
59 av_assert0(decode || (!decode && !got_frame));
68 } while (pkt->size > 0);
71 codec_ctx->skip_frame = skip_frame;
73 av_frame_free(&frame);
77 static int find_video_stream_info(AVFormatContext *fmt_ctx, int decode)
86 AVCodecContext *codec_ctx = NULL;
89 if ((ret = av_read_frame(fmt_ctx, &pkt)) < 0) {
90 av_log(fmt_ctx, AV_LOG_ERROR, "Failed to read frame\n");
94 st = fmt_ctx->streams[pkt.stream_index];
95 codec_ctx = st->codec;
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.
102 if (codec_ctx->codec_type != AVMEDIA_TYPE_VIDEO ||
103 st->codec_info_nb_frames++ > 0) {
104 av_packet_unref(&pkt);
108 ret = try_decode_video_frame(codec_ctx, &pkt, decode);
110 av_log(fmt_ctx, AV_LOG_ERROR, "Failed to decode video frame\n");
114 av_packet_unref(&pkt);
116 /* check if all video streams have demuxed a packet */
118 for (i = 0; i < fmt_ctx->nb_streams; i++) {
119 st = fmt_ctx->streams[i];
120 codec_ctx = st->codec;
122 if (codec_ctx->codec_type != AVMEDIA_TYPE_VIDEO)
125 done &= st->codec_info_nb_frames > 0;
130 av_packet_unref(&pkt);
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);
141 static void dump_video_streams(const AVFormatContext *fmt_ctx, int decode)
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;
150 printf("stream=%d, decode=%d\n", i, decode);
151 while (opt = av_opt_next(codec_ctx, opt)) {
154 if (opt->type == AV_OPT_TYPE_CONST)
157 if (!strcmp(opt->name, "frame_number"))
160 if (av_opt_get(codec_ctx, opt->name, 0, &str) >= 0) {
161 printf(" %s=%s\n", opt->name, str);
168 static int open_and_probe_video_streams(AVFormatContext **fmt_ctx, const char *filename, int decode)
172 ret = avformat_open_input(fmt_ctx, filename, NULL, NULL);
174 av_log(NULL, AV_LOG_ERROR, "Failed to open input '%s'", filename);
178 ret = find_video_stream_info(*fmt_ctx, decode);
183 dump_video_streams(*fmt_ctx, decode);
189 static int check_video_streams(const AVFormatContext *fmt_ctx1, const AVFormatContext *fmt_ctx2)
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;
202 if (codec_ctx1->codec_type != AVMEDIA_TYPE_VIDEO)
205 while (opt = av_opt_next(codec_ctx1, opt)) {
206 uint8_t *str1 = NULL, *str2 = NULL;
208 if (opt->type == AV_OPT_TYPE_CONST)
211 if (!strcmp(opt->name, "frame_number"))
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);
228 int main(int argc, char* argv[])
231 AVFormatContext *fmt_ctx = NULL;
232 AVFormatContext *fmt_ctx_no_decode = NULL;
237 av_log(NULL, AV_LOG_ERROR, "Usage: %s <input>\n", argv[0]);
241 if ((ret = open_and_probe_video_streams(&fmt_ctx_no_decode, argv[1], 0)) < 0) {
242 av_log(NULL, AV_LOG_ERROR, "Failed to probe '%s' without frame decoding\n", argv[1]);
246 if ((ret = open_and_probe_video_streams(&fmt_ctx, argv[1], 1)) < 0) {
247 av_log(NULL, AV_LOG_ERROR, "Failed to probe '%s' with frame decoding\n", argv[1]);
251 ret = check_video_streams(fmt_ctx, fmt_ctx_no_decode);
254 avformat_close_input(&fmt_ctx);
255 avformat_close_input(&fmt_ctx_no_decode);