FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/options.c
Date: 2022-12-05 20:26:17
Exec Total Coverage
Lines: 108 143 75.5%
Functions: 10 15 66.7%
Branches: 61 94 64.9%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #include "avformat.h"
21 #include "avio_internal.h"
22 #include "demux.h"
23 #include "internal.h"
24
25 #include "libavcodec/avcodec.h"
26 #include "libavcodec/codec_par.h"
27
28 #include "libavutil/avassert.h"
29 #include "libavutil/internal.h"
30 #include "libavutil/intmath.h"
31 #include "libavutil/opt.h"
32
33 /**
34 * @file
35 * Options definition for AVFormatContext.
36 */
37
38 FF_DISABLE_DEPRECATION_WARNINGS
39 #include "options_table.h"
40 FF_ENABLE_DEPRECATION_WARNINGS
41
42 2415 static const char* format_to_name(void* ptr)
43 {
44 2415 AVFormatContext* fc = (AVFormatContext*) ptr;
45
2/2
✓ Branch 0 taken 1559 times.
✓ Branch 1 taken 856 times.
2415 if(fc->iformat) return fc->iformat->name;
46
1/2
✓ Branch 0 taken 856 times.
✗ Branch 1 not taken.
856 else if(fc->oformat) return fc->oformat->name;
47 else return "NULL";
48 }
49
50 50710 static void *format_child_next(void *obj, void *prev)
51 {
52 50710 AVFormatContext *s = obj;
53
4/4
✓ Branch 0 taken 20602 times.
✓ Branch 1 taken 30108 times.
✓ Branch 2 taken 20161 times.
✓ Branch 3 taken 441 times.
50710 if (!prev && s->priv_data &&
54
4/4
✓ Branch 0 taken 20159 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2375 times.
✓ Branch 3 taken 17784 times.
20161 ((s->iformat && s->iformat->priv_class) ||
55
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2375 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2377 s->oformat && s->oformat->priv_class))
56 17786 return s->priv_data;
57
6/6
✓ Branch 0 taken 24956 times.
✓ Branch 1 taken 7968 times.
✓ Branch 2 taken 24944 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 12472 times.
✓ Branch 5 taken 12472 times.
32924 if (s->pb && s->pb->av_class && prev != s->pb)
58 12472 return s->pb;
59 20452 return NULL;
60 }
61
62 enum {
63 CHILD_CLASS_ITER_AVIO = 0,
64 CHILD_CLASS_ITER_MUX,
65 CHILD_CLASS_ITER_DEMUX,
66 CHILD_CLASS_ITER_DONE,
67
68 };
69
70 #define ITER_STATE_SHIFT 16
71
72 20130790 static const AVClass *format_child_class_iterate(void **iter)
73 {
74 // we use the low 16 bits of iter as the value to be passed to
75 // av_(de)muxer_iterate()
76 20130790 void *val = (void*)(((uintptr_t)*iter) & ((1 << ITER_STATE_SHIFT) - 1));
77 20130790 unsigned int state = ((uintptr_t)*iter) >> ITER_STATE_SHIFT;
78 20130790 const AVClass *ret = NULL;
79
80
2/2
✓ Branch 0 taken 86885 times.
✓ Branch 1 taken 20043905 times.
20130790 if (state == CHILD_CLASS_ITER_AVIO) {
81 86885 ret = &ff_avio_class;
82 86885 state++;
83 86885 goto finish;
84 }
85
86
2/2
✓ Branch 0 taken 6423983 times.
✓ Branch 1 taken 13619922 times.
20043905 if (state == CHILD_CLASS_ITER_MUX) {
87 const AVOutputFormat *ofmt;
88
89
2/2
✓ Branch 1 taken 15626491 times.
✓ Branch 2 taken 86763 times.
15713254 while ((ofmt = av_muxer_iterate(&val))) {
90 15626491 ret = ofmt->priv_class;
91
2/2
✓ Branch 0 taken 6337220 times.
✓ Branch 1 taken 9289271 times.
15626491 if (ret)
92 6337220 goto finish;
93 }
94
95 86763 val = NULL;
96 86763 state++;
97 }
98
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13706685 times.
13706685 if (state == CHILD_CLASS_ITER_DEMUX) {
100 const AVInputFormat *ifmt;
101
102
2/2
✓ Branch 1 taken 29842658 times.
✓ Branch 2 taken 86745 times.
29929403 while ((ifmt = av_demuxer_iterate(&val))) {
103 29842658 ret = ifmt->priv_class;
104
2/2
✓ Branch 0 taken 13619940 times.
✓ Branch 1 taken 16222718 times.
29842658 if (ret)
105 13619940 goto finish;
106 }
107 86745 val = NULL;
108 86745 state++;
109 }
110
111 finish:
112 // make sure none av_(de)muxer_iterate does not set the high bits of val
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20130790 times.
20130790 av_assert0(!((uintptr_t)val >> ITER_STATE_SHIFT));
114 20130790 *iter = (void*)((uintptr_t)val | (state << ITER_STATE_SHIFT));
115 20130790 return ret;
116 }
117
118 2415 static AVClassCategory get_category(void *ptr)
119 {
120 2415 AVFormatContext* s = ptr;
121
2/2
✓ Branch 0 taken 1559 times.
✓ Branch 1 taken 856 times.
2415 if(s->iformat) return AV_CLASS_CATEGORY_DEMUXER;
122 856 else return AV_CLASS_CATEGORY_MUXER;
123 }
124
125 static const AVClass av_format_context_class = {
126 .class_name = "AVFormatContext",
127 .item_name = format_to_name,
128 .option = avformat_options,
129 .version = LIBAVUTIL_VERSION_INT,
130 .child_next = format_child_next,
131 .child_class_iterate = format_child_class_iterate,
132 .category = AV_CLASS_CATEGORY_MUXER,
133 .get_category = get_category,
134 };
135
136 90434 static int io_open_default(AVFormatContext *s, AVIOContext **pb,
137 const char *url, int flags, AVDictionary **options)
138 {
139 int loglevel;
140
141
2/2
✓ Branch 0 taken 86237 times.
✓ Branch 1 taken 4197 times.
90434 if (!strcmp(url, s->url) ||
142
4/4
✓ Branch 0 taken 85428 times.
✓ Branch 1 taken 809 times.
✓ Branch 2 taken 62 times.
✓ Branch 3 taken 85366 times.
86237 s->iformat && !strcmp(s->iformat->name, "image2") ||
143
4/4
✓ Branch 0 taken 809 times.
✓ Branch 1 taken 62 times.
✓ Branch 2 taken 663 times.
✓ Branch 3 taken 146 times.
871 s->oformat && !strcmp(s->oformat->name, "image2")
144 ) {
145 90226 loglevel = AV_LOG_DEBUG;
146 } else
147 208 loglevel = AV_LOG_INFO;
148
149
2/2
✓ Branch 0 taken 819 times.
✓ Branch 1 taken 89615 times.
90434 av_log(s, loglevel, "Opening \'%s\' for %s\n", url, flags & AVIO_FLAG_WRITE ? "writing" : "reading");
150
151 90434 return ffio_open_whitelist(pb, url, flags, &s->interrupt_callback, options, s->protocol_whitelist, s->protocol_blacklist);
152 }
153
154 void ff_format_io_close_default(AVFormatContext *s, AVIOContext *pb)
155 {
156 avio_close(pb);
157 }
158
159 86260 static int io_close2_default(AVFormatContext *s, AVIOContext *pb)
160 {
161 86260 return avio_close(pb);
162 }
163
164 13362 AVFormatContext *avformat_alloc_context(void)
165 {
166 13362 FFFormatContext *const si = av_mallocz(sizeof(*si));
167 AVFormatContext *s;
168
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13362 times.
13362 if (!si)
170 return NULL;
171
172 13362 s = &si->pub;
173 13362 s->av_class = &av_format_context_class;
174 13362 s->io_open = io_open_default;
175 13362 s->io_close = ff_format_io_close_default;
176 13362 s->io_close2= io_close2_default;
177
178 13362 av_opt_set_defaults(s);
179
180 13362 si->pkt = av_packet_alloc();
181 13362 si->parse_pkt = av_packet_alloc();
182
2/4
✓ Branch 0 taken 13362 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13362 times.
13362 if (!si->pkt || !si->parse_pkt) {
183 avformat_free_context(s);
184 return NULL;
185 }
186
187 13362 si->shortest_end = AV_NOPTS_VALUE;
188
189 13362 return s;
190 }
191
192 enum AVDurationEstimationMethod av_fmt_ctx_get_duration_estimation_method(const AVFormatContext* ctx)
193 {
194 return ctx->duration_estimation_method;
195 }
196
197 93011 const AVClass *avformat_get_class(void)
198 {
199 93011 return &av_format_context_class;
200 }
201
202 static const AVOption stream_options[] = {
203 { "disposition", NULL, offsetof(AVStream, disposition), AV_OPT_TYPE_FLAGS, { .i64 = 0 },
204 .flags = AV_OPT_FLAG_ENCODING_PARAM, .unit = "disposition" },
205 { "default", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DEFAULT }, .unit = "disposition" },
206 { "dub", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DUB }, .unit = "disposition" },
207 { "original", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_ORIGINAL }, .unit = "disposition" },
208 { "comment", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_COMMENT }, .unit = "disposition" },
209 { "lyrics", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_LYRICS }, .unit = "disposition" },
210 { "karaoke", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_KARAOKE }, .unit = "disposition" },
211 { "forced", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_FORCED }, .unit = "disposition" },
212 { "hearing_impaired", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_HEARING_IMPAIRED }, .unit = "disposition" },
213 { "visual_impaired", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_VISUAL_IMPAIRED }, .unit = "disposition" },
214 { "clean_effects", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CLEAN_EFFECTS }, .unit = "disposition" },
215 { "attached_pic", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_ATTACHED_PIC }, .unit = "disposition" },
216 { "timed_thumbnails", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_TIMED_THUMBNAILS }, .unit = "disposition" },
217 { "captions", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CAPTIONS }, .unit = "disposition" },
218 { "descriptions", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DESCRIPTIONS }, .unit = "disposition" },
219 { "metadata", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_METADATA }, .unit = "disposition" },
220 { "dependent", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DEPENDENT }, .unit = "disposition" },
221 { "still_image", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_STILL_IMAGE }, .unit = "disposition" },
222 { NULL }
223 };
224
225 static const AVClass stream_class = {
226 .class_name = "AVStream",
227 .item_name = av_default_item_name,
228 .version = LIBAVUTIL_VERSION_INT,
229 .option = stream_options,
230 };
231
232 13 const AVClass *av_stream_get_class(void)
233 {
234 13 return &stream_class;
235 }
236
237 14262 AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c)
238 {
239 14262 FFFormatContext *const si = ffformatcontext(s);
240 FFStream *sti;
241 AVStream *st;
242 AVStream **streams;
243
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14262 times.
14262 if (s->nb_streams >= s->max_streams) {
245 av_log(s, AV_LOG_ERROR, "Number of streams exceeds max_streams parameter"
246 " (%d), see the documentation if you wish to increase it\n",
247 s->max_streams);
248 return NULL;
249 }
250 14262 streams = av_realloc_array(s->streams, s->nb_streams + 1, sizeof(*streams));
251
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14262 times.
14262 if (!streams)
252 return NULL;
253 14262 s->streams = streams;
254
255 14262 sti = av_mallocz(sizeof(*sti));
256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14262 times.
14262 if (!sti)
257 return NULL;
258 14262 st = &sti->pub;
259
260 #if FF_API_AVSTREAM_CLASS
261 st->av_class = &stream_class;
262 #endif
263
264 14262 st->codecpar = avcodec_parameters_alloc();
265
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14262 times.
14262 if (!st->codecpar)
266 goto fail;
267
268 14262 sti->avctx = avcodec_alloc_context3(NULL);
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14262 times.
14262 if (!sti->avctx)
270 goto fail;
271
272
2/2
✓ Branch 0 taken 7468 times.
✓ Branch 1 taken 6794 times.
14262 if (s->iformat) {
273 7468 sti->info = av_mallocz(sizeof(*sti->info));
274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7468 times.
7468 if (!sti->info)
275 goto fail;
276
277 #if FF_API_R_FRAME_RATE
278 7468 sti->info->last_dts = AV_NOPTS_VALUE;
279 #endif
280 7468 sti->info->fps_first_dts = AV_NOPTS_VALUE;
281 7468 sti->info->fps_last_dts = AV_NOPTS_VALUE;
282
283 /* default pts setting is MPEG-like */
284 7468 avpriv_set_pts_info(st, 33, 1, 90000);
285 /* we set the current DTS to 0 so that formats without any timestamps
286 * but durations get some timestamps, formats with some unknown
287 * timestamps have their first few packets buffered and the
288 * timestamps corrected before they are returned to the user */
289 7468 sti->cur_dts = RELATIVE_TS_BASE;
290 } else {
291 6794 sti->cur_dts = AV_NOPTS_VALUE;
292 }
293
294 14262 st->index = s->nb_streams;
295 14262 st->start_time = AV_NOPTS_VALUE;
296 14262 st->duration = AV_NOPTS_VALUE;
297 14262 sti->first_dts = AV_NOPTS_VALUE;
298 14262 sti->probe_packets = s->max_probe_packets;
299 14262 sti->pts_wrap_reference = AV_NOPTS_VALUE;
300 14262 sti->pts_wrap_behavior = AV_PTS_WRAP_IGNORE;
301
302 14262 sti->last_IP_pts = AV_NOPTS_VALUE;
303 14262 sti->last_dts_for_order_check = AV_NOPTS_VALUE;
304
2/2
✓ Branch 0 taken 242454 times.
✓ Branch 1 taken 14262 times.
256716 for (int i = 0; i < MAX_REORDER_DELAY + 1; i++)
305 242454 sti->pts_buffer[i] = AV_NOPTS_VALUE;
306
307 14262 st->sample_aspect_ratio = (AVRational) { 0, 1 };
308
309 14262 sti->inject_global_side_data = si->inject_global_side_data;
310
311 14262 sti->need_context_update = 1;
312
313 14262 s->streams[s->nb_streams++] = st;
314 14262 return st;
315 fail:
316 ff_free_stream(&st);
317 return NULL;
318 }
319
320 static int option_is_disposition(const AVOption *opt)
321 {
322 return opt->type == AV_OPT_TYPE_CONST &&
323 opt->unit && !strcmp(opt->unit, "disposition");
324 }
325
326 int av_disposition_from_string(const char *disp)
327 {
328 for (const AVOption *opt = stream_options; opt->name; opt++)
329 if (option_is_disposition(opt) && !strcmp(disp, opt->name))
330 return opt->default_val.i64;
331 return AVERROR(EINVAL);
332 }
333
334 const char *av_disposition_to_string(int disposition)
335 {
336 int val;
337
338 if (disposition <= 0)
339 return NULL;
340
341 val = 1 << ff_ctz(disposition);
342 for (const AVOption *opt = stream_options; opt->name; opt++)
343 if (option_is_disposition(opt) && opt->default_val.i64 == val)
344 return opt->name;
345
346 return NULL;
347 }
348