]> git.sesse.net Git - ffmpeg/blob - tests/api/api-codec-param-test.c
Merge commit '4f979418c723652ad4e43115118c57a44bd46b52'
[ffmpeg] / tests / api / api-codec-param-test.c
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
30 static int try_decode_video_frame(AVCodecContext *codec_ctx, AVPacket *pkt, int decode)
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
47     frame = av_frame_alloc();
48     if (!frame) {
49         av_log(NULL, AV_LOG_ERROR, "Failed to allocate frame\n");
50         goto end;
51     }
52
53     if (!decode && codec_ctx->codec->caps_internal & FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM) {
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
73     av_frame_free(&frame);
74     return ret;
75 }
76
77 static int find_video_stream_info(AVFormatContext *fmt_ctx, int decode)
78 {
79     int ret = 0;
80     int i, done = 0;
81     AVPacket pkt;
82
83     av_init_packet(&pkt);
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
94         st = fmt_ctx->streams[pkt.stream_index];
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) {
104             av_packet_unref(&pkt);
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
114         av_packet_unref(&pkt);
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:
130     av_packet_unref(&pkt);
131
132     return ret < 0;
133 }
134
135 static void dump_video_streams(const AVFormatContext *fmt_ctx, int decode)
136 {
137     int i;
138
139     for (i = 0; i < fmt_ctx->nb_streams; i++) {
140         const AVOption *opt = NULL;
141         const AVStream *st = fmt_ctx->streams[i];
142         AVCodecContext *codec_ctx = st->codec;
143
144         printf("stream=%d, decode=%d\n", i, decode);
145         while (opt = av_opt_next(codec_ctx, opt)) {
146             uint8_t *str;
147
148             if (opt->type == AV_OPT_TYPE_CONST)
149                 continue;
150
151             if (!strcmp(opt->name, "frame_number"))
152                 continue;
153
154             if (av_opt_get(codec_ctx, opt->name, 0, &str) >= 0) {
155                 printf("    %s=%s\n", opt->name, str);
156                 av_free(str);
157             }
158         }
159     }
160 }
161
162 static int open_and_probe_video_streams(AVFormatContext **fmt_ctx, const char *filename, int decode)
163 {
164     int ret = 0;
165
166     ret = avformat_open_input(fmt_ctx, filename, NULL, NULL);
167     if (ret < 0) {
168         av_log(NULL, AV_LOG_ERROR, "Failed to open input '%s'", filename);
169         goto end;
170     }
171
172     ret = find_video_stream_info(*fmt_ctx, decode);
173     if (ret < 0) {
174         goto end;
175     }
176
177     dump_video_streams(*fmt_ctx, decode);
178
179 end:
180     return ret;
181 }
182
183 static int check_video_streams(const AVFormatContext *fmt_ctx1, const AVFormatContext *fmt_ctx2)
184 {
185     int i;
186     int ret = 0;
187
188     av_assert0(fmt_ctx1->nb_streams == fmt_ctx2->nb_streams);
189     for (i = 0; i < fmt_ctx1->nb_streams; i++) {
190         const AVOption *opt = NULL;
191         const AVStream *st1 = fmt_ctx1->streams[i];
192         const AVStream *st2 = fmt_ctx2->streams[i];
193         AVCodecContext *codec_ctx1 = st1->codec;
194         AVCodecContext *codec_ctx2 = st2->codec;
195
196         if (codec_ctx1->codec_type != AVMEDIA_TYPE_VIDEO)
197             continue;
198
199         while (opt = av_opt_next(codec_ctx1, opt)) {
200             uint8_t *str1 = NULL, *str2 = NULL;
201
202             if (opt->type == AV_OPT_TYPE_CONST)
203                 continue;
204
205             if (!strcmp(opt->name, "frame_number"))
206                 continue;
207
208             av_assert0(av_opt_get(codec_ctx1, opt->name, 0, &str1) >= 0);
209             av_assert0(av_opt_get(codec_ctx2, opt->name, 0, &str2) >= 0);
210             if (strcmp(str1, str2)) {
211                 av_log(NULL, AV_LOG_ERROR, "Field %s differs: %s %s", opt->name, str1, str2);
212                 ret = AVERROR(EINVAL);
213             }
214             av_free(str1);
215             av_free(str2);
216         }
217     }
218
219     return ret;
220 }
221
222 int main(int argc, char* argv[])
223 {
224     int ret = 0;
225     AVFormatContext *fmt_ctx = NULL;
226     AVFormatContext *fmt_ctx_no_decode = NULL;
227
228     av_register_all();
229
230     if (argc < 2) {
231         av_log(NULL, AV_LOG_ERROR, "Usage: %s <input>\n", argv[0]);
232         return -1;
233     }
234
235     if ((ret = open_and_probe_video_streams(&fmt_ctx_no_decode, argv[1], 0)) < 0) {
236         av_log(NULL, AV_LOG_ERROR, "Failed to probe '%s' without frame decoding\n", argv[1]);
237         goto end;
238     }
239
240     if ((ret = open_and_probe_video_streams(&fmt_ctx, argv[1], 1)) < 0) {
241         av_log(NULL, AV_LOG_ERROR, "Failed to probe '%s' with frame decoding\n", argv[1]);
242         goto end;
243     }
244
245     ret = check_video_streams(fmt_ctx, fmt_ctx_no_decode);
246
247 end:
248     avformat_close_input(&fmt_ctx);
249     avformat_close_input(&fmt_ctx_no_decode);
250
251     return ret;
252 }