FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/options.c
Date: 2023-09-22 12:20:07
Exec Total Coverage
Lines: 107 145 73.8%
Functions: 9 15 60.0%
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 2445 static const char* format_to_name(void* ptr)
43 {
44 2445 AVFormatContext* fc = (AVFormatContext*) ptr;
45
2/2
✓ Branch 0 taken 1610 times.
✓ Branch 1 taken 835 times.
2445 if(fc->iformat) return fc->iformat->name;
46
1/2
✓ Branch 0 taken 835 times.
✗ Branch 1 not taken.
835 else if(fc->oformat) return fc->oformat->name;
47 else return fc->av_class->class_name;
48 }
49
50 52285 static void *format_child_next(void *obj, void *prev)
51 {
52 52285 AVFormatContext *s = obj;
53
4/4
✓ Branch 0 taken 21217 times.
✓ Branch 1 taken 31068 times.
✓ Branch 2 taken 20767 times.
✓ Branch 3 taken 450 times.
52285 if (!prev && s->priv_data &&
54
4/4
✓ Branch 0 taken 20765 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2438 times.
✓ Branch 3 taken 18327 times.
20767 ((s->iformat && s->iformat->priv_class) ||
55
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2438 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2440 s->oformat && s->oformat->priv_class))
56 18329 return s->priv_data;
57
6/6
✓ Branch 0 taken 25838 times.
✓ Branch 1 taken 8118 times.
✓ Branch 2 taken 25826 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 12913 times.
✓ Branch 5 taken 12913 times.
33956 if (s->pb && s->pb->av_class && prev != s->pb)
58 12913 return s->pb;
59 21043 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 21333329 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 21333329 void *val = (void*)(((uintptr_t)*iter) & ((1 << ITER_STATE_SHIFT) - 1));
77 21333329 unsigned int state = ((uintptr_t)*iter) >> ITER_STATE_SHIFT;
78 21333329 const AVClass *ret = NULL;
79
80
2/2
✓ Branch 0 taken 90130 times.
✓ Branch 1 taken 21243199 times.
21333329 if (state == CHILD_CLASS_ITER_AVIO) {
81 90130 ret = &ff_avio_class;
82 90130 state++;
83 90130 goto finish;
84 }
85
86
2/2
✓ Branch 0 taken 6754150 times.
✓ Branch 1 taken 14489049 times.
21243199 if (state == CHILD_CLASS_ITER_MUX) {
87 const AVOutputFormat *ofmt;
88
89
2/2
✓ Branch 1 taken 16480639 times.
✓ Branch 2 taken 90006 times.
16570645 while ((ofmt = av_muxer_iterate(&val))) {
90 16480639 ret = ofmt->priv_class;
91
2/2
✓ Branch 0 taken 6664144 times.
✓ Branch 1 taken 9816495 times.
16480639 if (ret)
92 6664144 goto finish;
93 }
94
95 90006 val = NULL;
96 90006 state++;
97 }
98
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14579055 times.
14579055 if (state == CHILD_CLASS_ITER_DEMUX) {
100 const AVInputFormat *ifmt;
101
102
2/2
✓ Branch 1 taken 32038180 times.
✓ Branch 2 taken 89988 times.
32128168 while ((ifmt = av_demuxer_iterate(&val))) {
103 32038180 ret = ifmt->priv_class;
104
2/2
✓ Branch 0 taken 14489067 times.
✓ Branch 1 taken 17549113 times.
32038180 if (ret)
105 14489067 goto finish;
106 }
107 89988 val = NULL;
108 89988 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 21333329 times.
21333329 av_assert0(!((uintptr_t)val >> ITER_STATE_SHIFT));
114 21333329 *iter = (void*)((uintptr_t)val | (state << ITER_STATE_SHIFT));
115 21333329 return ret;
116 }
117
118 2445 static AVClassCategory get_category(void *ptr)
119 {
120 2445 AVFormatContext* s = ptr;
121
2/2
✓ Branch 0 taken 1610 times.
✓ Branch 1 taken 835 times.
2445 if(s->iformat) return AV_CLASS_CATEGORY_DEMUXER;
122 835 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 92206 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 87858 times.
✓ Branch 1 taken 4348 times.
92206 if (!strcmp(url, s->url) ||
142
4/4
✓ Branch 0 taken 87049 times.
✓ Branch 1 taken 809 times.
✓ Branch 2 taken 62 times.
✓ Branch 3 taken 86987 times.
87858 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 91998 loglevel = AV_LOG_DEBUG;
146 } else
147 208 loglevel = AV_LOG_INFO;
148
149
2/2
✓ Branch 0 taken 819 times.
✓ Branch 1 taken 91387 times.
92206 av_log(s, loglevel, "Opening \'%s\' for %s\n", url, flags & AVIO_FLAG_WRITE ? "writing" : "reading");
150
151 92206 return ffio_open_whitelist(pb, url, flags, &s->interrupt_callback, options, s->protocol_whitelist, s->protocol_blacklist);
152 }
153
154 #if FF_API_AVFORMAT_IO_CLOSE
155 void ff_format_io_close_default(AVFormatContext *s, AVIOContext *pb)
156 {
157 avio_close(pb);
158 }
159 #endif
160
161 87881 static int io_close2_default(AVFormatContext *s, AVIOContext *pb)
162 {
163 87881 return avio_close(pb);
164 }
165
166 13756 AVFormatContext *avformat_alloc_context(void)
167 {
168 13756 FFFormatContext *const si = av_mallocz(sizeof(*si));
169 AVFormatContext *s;
170
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13756 times.
13756 if (!si)
172 return NULL;
173
174 13756 s = &si->pub;
175 13756 s->av_class = &av_format_context_class;
176 13756 s->io_open = io_open_default;
177 #if FF_API_AVFORMAT_IO_CLOSE
178 FF_DISABLE_DEPRECATION_WARNINGS
179 13756 s->io_close = ff_format_io_close_default;
180 FF_ENABLE_DEPRECATION_WARNINGS
181 #endif
182 13756 s->io_close2= io_close2_default;
183
184 13756 av_opt_set_defaults(s);
185
186 13756 si->pkt = av_packet_alloc();
187 13756 si->parse_pkt = av_packet_alloc();
188
2/4
✓ Branch 0 taken 13756 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13756 times.
13756 if (!si->pkt || !si->parse_pkt) {
189 avformat_free_context(s);
190 return NULL;
191 }
192
193 #if FF_API_LAVF_SHORTEST
194 13756 si->shortest_end = AV_NOPTS_VALUE;
195 #endif
196
197 13756 return s;
198 }
199
200 enum AVDurationEstimationMethod av_fmt_ctx_get_duration_estimation_method(const AVFormatContext* ctx)
201 {
202 return ctx->duration_estimation_method;
203 }
204
205 95775 const AVClass *avformat_get_class(void)
206 {
207 95775 return &av_format_context_class;
208 }
209
210 static const AVOption stream_options[] = {
211 { "disposition", NULL, offsetof(AVStream, disposition), AV_OPT_TYPE_FLAGS, { .i64 = 0 },
212 .flags = AV_OPT_FLAG_ENCODING_PARAM, .unit = "disposition" },
213 { "default", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DEFAULT }, .unit = "disposition" },
214 { "dub", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DUB }, .unit = "disposition" },
215 { "original", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_ORIGINAL }, .unit = "disposition" },
216 { "comment", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_COMMENT }, .unit = "disposition" },
217 { "lyrics", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_LYRICS }, .unit = "disposition" },
218 { "karaoke", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_KARAOKE }, .unit = "disposition" },
219 { "forced", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_FORCED }, .unit = "disposition" },
220 { "hearing_impaired", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_HEARING_IMPAIRED }, .unit = "disposition" },
221 { "visual_impaired", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_VISUAL_IMPAIRED }, .unit = "disposition" },
222 { "clean_effects", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CLEAN_EFFECTS }, .unit = "disposition" },
223 { "attached_pic", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_ATTACHED_PIC }, .unit = "disposition" },
224 { "timed_thumbnails", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_TIMED_THUMBNAILS }, .unit = "disposition" },
225 { "captions", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CAPTIONS }, .unit = "disposition" },
226 { "descriptions", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DESCRIPTIONS }, .unit = "disposition" },
227 { "metadata", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_METADATA }, .unit = "disposition" },
228 { "dependent", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DEPENDENT }, .unit = "disposition" },
229 { "still_image", .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_STILL_IMAGE }, .unit = "disposition" },
230 { NULL }
231 };
232
233 static const AVClass stream_class = {
234 .class_name = "AVStream",
235 .item_name = av_default_item_name,
236 .version = LIBAVUTIL_VERSION_INT,
237 .option = stream_options,
238 };
239
240 const AVClass *av_stream_get_class(void)
241 {
242 return &stream_class;
243 }
244
245 14715 AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c)
246 {
247 14715 FFFormatContext *const si = ffformatcontext(s);
248 FFStream *sti;
249 AVStream *st;
250 AVStream **streams;
251
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14715 times.
14715 if (s->nb_streams >= s->max_streams) {
253 av_log(s, AV_LOG_ERROR, "Number of streams exceeds max_streams parameter"
254 " (%d), see the documentation if you wish to increase it\n",
255 s->max_streams);
256 return NULL;
257 }
258 14715 streams = av_realloc_array(s->streams, s->nb_streams + 1, sizeof(*streams));
259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14715 times.
14715 if (!streams)
260 return NULL;
261 14715 s->streams = streams;
262
263 14715 sti = av_mallocz(sizeof(*sti));
264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14715 times.
14715 if (!sti)
265 return NULL;
266 14715 st = &sti->pub;
267
268 14715 st->av_class = &stream_class;
269 14715 st->codecpar = avcodec_parameters_alloc();
270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14715 times.
14715 if (!st->codecpar)
271 goto fail;
272
273 14715 sti->avctx = avcodec_alloc_context3(NULL);
274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14715 times.
14715 if (!sti->avctx)
275 goto fail;
276
277
2/2
✓ Branch 0 taken 7700 times.
✓ Branch 1 taken 7015 times.
14715 if (s->iformat) {
278 7700 sti->info = av_mallocz(sizeof(*sti->info));
279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7700 times.
7700 if (!sti->info)
280 goto fail;
281
282 #if FF_API_R_FRAME_RATE
283 7700 sti->info->last_dts = AV_NOPTS_VALUE;
284 #endif
285 7700 sti->info->fps_first_dts = AV_NOPTS_VALUE;
286 7700 sti->info->fps_last_dts = AV_NOPTS_VALUE;
287
288 /* default pts setting is MPEG-like */
289 7700 avpriv_set_pts_info(st, 33, 1, 90000);
290 /* we set the current DTS to 0 so that formats without any timestamps
291 * but durations get some timestamps, formats with some unknown
292 * timestamps have their first few packets buffered and the
293 * timestamps corrected before they are returned to the user */
294 7700 sti->cur_dts = RELATIVE_TS_BASE;
295 } else {
296 7015 sti->cur_dts = AV_NOPTS_VALUE;
297 }
298
299 14715 st->index = s->nb_streams;
300 14715 st->start_time = AV_NOPTS_VALUE;
301 14715 st->duration = AV_NOPTS_VALUE;
302 14715 sti->first_dts = AV_NOPTS_VALUE;
303 14715 sti->probe_packets = s->max_probe_packets;
304 14715 sti->pts_wrap_reference = AV_NOPTS_VALUE;
305 14715 sti->pts_wrap_behavior = AV_PTS_WRAP_IGNORE;
306
307 14715 sti->last_IP_pts = AV_NOPTS_VALUE;
308 14715 sti->last_dts_for_order_check = AV_NOPTS_VALUE;
309
2/2
✓ Branch 0 taken 250155 times.
✓ Branch 1 taken 14715 times.
264870 for (int i = 0; i < MAX_REORDER_DELAY + 1; i++)
310 250155 sti->pts_buffer[i] = AV_NOPTS_VALUE;
311
312 14715 st->sample_aspect_ratio = (AVRational) { 0, 1 };
313
314 14715 sti->inject_global_side_data = si->inject_global_side_data;
315
316 14715 sti->need_context_update = 1;
317
318 14715 s->streams[s->nb_streams++] = st;
319 14715 return st;
320 fail:
321 ff_free_stream(&st);
322 return NULL;
323 }
324
325 static int option_is_disposition(const AVOption *opt)
326 {
327 return opt->type == AV_OPT_TYPE_CONST &&
328 opt->unit && !strcmp(opt->unit, "disposition");
329 }
330
331 int av_disposition_from_string(const char *disp)
332 {
333 for (const AVOption *opt = stream_options; opt->name; opt++)
334 if (option_is_disposition(opt) && !strcmp(disp, opt->name))
335 return opt->default_val.i64;
336 return AVERROR(EINVAL);
337 }
338
339 const char *av_disposition_to_string(int disposition)
340 {
341 int val;
342
343 if (disposition <= 0)
344 return NULL;
345
346 val = 1 << ff_ctz(disposition);
347 for (const AVOption *opt = stream_options; opt->name; opt++)
348 if (option_is_disposition(opt) && opt->default_val.i64 == val)
349 return opt->name;
350
351 return NULL;
352 }
353