FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/avformat.c
Date: 2026-10-03 16:45:23
Exec Total Coverage
Lines: 388 615 63.1%
Functions: 24 27 88.9%
Branches: 251 549 45.7%

Line Branch Exec Source
1 /*
2 * Various functions used by both muxers and demuxers
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "config_components.h"
23
24 #include <math.h>
25 #include "libavutil/avassert.h"
26 #include "libavutil/avstring.h"
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/frame.h"
29 #include "libavutil/iamf.h"
30 #include "libavutil/mem.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/pixfmt.h"
33 #include "libavutil/samplefmt.h"
34 #include "libavcodec/avcodec.h"
35 #include "libavcodec/codec.h"
36 #include "libavcodec/bsf.h"
37 #include "libavcodec/codec_desc.h"
38 #include "packet_internal.h"
39 #include "avformat.h"
40 #include "avformat_internal.h"
41 #include "avio.h"
42 #include "demux.h"
43 #include "mux.h"
44 #include "internal.h"
45 #include "url.h"
46
47 19288 void ff_free_stream(AVStream **pst)
48 {
49 19288 AVStream *st = *pst;
50 19288 FFStream *const sti = ffstream(st);
51
52
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19288 times.
19288 if (!st)
53 ✗ return;
54
55
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 19222 times.
19288 if (st->attached_pic.data)
56 66 av_packet_unref(&st->attached_pic);
57
58 19288 av_parser_close(sti->parser);
59 19288 avcodec_free_context(&sti->avctx);
60 19288 av_bsf_free(&sti->bsfc);
61 19288 av_freep(&sti->index_entries);
62 19288 av_freep(&sti->probe_data.buf);
63
64 19288 av_packet_free(&sti->parse_pkt);
65
66 19288 av_bsf_free(&sti->extract_extradata.bsf);
67
68
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 19148 times.
19288 if (sti->info) {
69 140 av_freep(&sti->info->duration_error);
70 140 av_freep(&sti->info);
71 }
72
73 19288 av_dict_free(&st->metadata);
74 19288 avcodec_parameters_free(&st->codecpar);
75 19288 av_freep(&st->priv_data);
76
77 19288 av_freep(pst);
78 }
79
80 170 void ff_free_stream_group(AVStreamGroup **pstg)
81 {
82 170 AVStreamGroup *stg = *pstg;
83
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 170 times.
170 if (!stg)
85 ✗ return;
86
87 170 av_freep(&stg->streams);
88 170 av_dict_free(&stg->metadata);
89 170 av_freep(&stg->priv_data);
90
5/6
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 55 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 39 times.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
170 switch (stg->type) {
91 58 case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT: {
92 58 av_iamf_audio_element_free(&stg->params.iamf_audio_element);
93 58 break;
94 }
95 55 case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION: {
96 55 av_iamf_mix_presentation_free(&stg->params.iamf_mix_presentation);
97 55 break;
98 }
99 9 case AV_STREAM_GROUP_PARAMS_TILE_GRID:
100 9 av_opt_free(stg->params.tile_grid);
101 9 av_freep(&stg->params.tile_grid->offsets);
102 9 av_packet_side_data_free(&stg->params.tile_grid->coded_side_data,
103 9 &stg->params.tile_grid->nb_coded_side_data);
104 9 av_freep(&stg->params.tile_grid);
105 9 break;
106 39 case AV_STREAM_GROUP_PARAMS_TREF:
107 39 av_opt_free(stg->params.tref);
108 39 av_freep(&stg->params.tref);
109 39 break;
110 9 case AV_STREAM_GROUP_PARAMS_LCEVC:
111 case AV_STREAM_GROUP_PARAMS_DOLBY_VISION:
112 case AV_STREAM_GROUP_PARAMS_GAIN_MAP:
113 9 av_opt_free(stg->params.layered_video);
114 9 av_freep(&stg->params.layered_video);
115 9 break;
116 ✗ default:
117 ✗ break;
118 }
119
120 170 av_freep(pstg);
121 }
122
123 10 void ff_remove_stream(AVFormatContext *s, AVStream *st)
124 {
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 av_assert0(s->nb_streams>0);
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 av_assert0(s->streams[ s->nb_streams - 1 ] == st);
127
128 10 ff_free_stream(&s->streams[ --s->nb_streams ]);
129 10 }
130
131 ✗ void ff_remove_stream_group(AVFormatContext *s, AVStreamGroup *stg)
132 {
133 ✗ av_assert0(s->nb_stream_groups > 0);
134 ✗ av_assert0(s->stream_groups[ s->nb_stream_groups - 1 ] == stg);
135
136 ✗ ff_free_stream_group(&s->stream_groups[ --s->nb_stream_groups ]);
137 ✗ }
138
139 /* XXX: suppress the packet queue */
140 13367 void ff_flush_packet_queue(AVFormatContext *s)
141 {
142 13367 FormatContextInternal *const fci = ff_fc_internal(s);
143 13367 FFFormatContext *const si = &fci->fc;
144 13367 ff_packet_list_free(&fci->parse_queue);
145 13367 ff_packet_list_free(&si->packet_buffer);
146 13367 ff_packet_list_free(&fci->raw_packet_buffer);
147
148 13367 fci->raw_packet_buffer_size = 0;
149 13367 }
150
151 17961 void avformat_free_context(AVFormatContext *s)
152 {
153 FormatContextInternal *fci;
154 FFFormatContext *si;
155
156
2/2
✓ Branch 0 taken 644 times.
✓ Branch 1 taken 17317 times.
17961 if (!s)
157 644 return;
158 17317 fci = ff_fc_internal(s);
159 17317 si = &fci->fc;
160
161
5/6
✓ Branch 0 taken 8985 times.
✓ Branch 1 taken 8332 times.
✓ Branch 3 taken 4497 times.
✓ Branch 4 taken 4488 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4497 times.
17317 if (s->oformat && ffofmt(s->oformat)->deinit && fci->initialized)
162 ✗ ffofmt(s->oformat)->deinit(s);
163
164 17317 av_opt_free(s);
165
5/6
✓ Branch 0 taken 8310 times.
✓ Branch 1 taken 9007 times.
✓ Branch 2 taken 7138 times.
✓ Branch 3 taken 1172 times.
✓ Branch 4 taken 7138 times.
✗ Branch 5 not taken.
17317 if (s->iformat && s->iformat->priv_class && s->priv_data)
166 7138 av_opt_free(s->priv_data);
167
6/6
✓ Branch 0 taken 8985 times.
✓ Branch 1 taken 8332 times.
✓ Branch 2 taken 4567 times.
✓ Branch 3 taken 4418 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 4566 times.
17317 if (s->oformat && s->oformat->priv_class && s->priv_data)
168 1 av_opt_free(s->priv_data);
169
170
2/2
✓ Branch 0 taken 19278 times.
✓ Branch 1 taken 17317 times.
36595 for (unsigned i = 0; i < s->nb_streams; i++)
171 19278 ff_free_stream(&s->streams[i]);
172
2/2
✓ Branch 0 taken 170 times.
✓ Branch 1 taken 17317 times.
17487 for (unsigned i = 0; i < s->nb_stream_groups; i++)
173 170 ff_free_stream_group(&s->stream_groups[i]);
174 17317 s->nb_stream_groups = 0;
175 17317 s->nb_streams = 0;
176
177
2/2
✓ Branch 0 taken 103 times.
✓ Branch 1 taken 17317 times.
17420 for (unsigned i = 0; i < s->nb_programs; i++) {
178 103 av_dict_free(&s->programs[i]->metadata);
179 103 av_freep(&s->programs[i]->stream_index);
180 103 av_freep(&s->programs[i]);
181 }
182 17317 s->nb_programs = 0;
183
184 17317 av_freep(&s->programs);
185 17317 av_freep(&s->priv_data);
186
2/2
✓ Branch 0 taken 208 times.
✓ Branch 1 taken 17317 times.
17525 while (s->nb_chapters--) {
187 208 av_dict_free(&s->chapters[s->nb_chapters]->metadata);
188 208 av_freep(&s->chapters[s->nb_chapters]);
189 }
190 17317 av_freep(&s->chapters);
191 17317 av_dict_free(&s->metadata);
192 17317 av_dict_free(&si->id3v2_meta);
193 #if CONFIG_LIBCURL_PROTOCOL
194 17317 ff_curl_loop_free(&si->curl_loop);
195 #endif
196 17317 av_packet_free(&si->pkt);
197 17317 av_packet_free(&si->parse_pkt);
198 17317 ff_packet_list_free(&si->packet_buffer);
199 17317 av_freep(&s->streams);
200 17317 av_freep(&s->stream_groups);
201
2/2
✓ Branch 0 taken 8310 times.
✓ Branch 1 taken 9007 times.
17317 if (s->iformat)
202 8310 ff_flush_packet_queue(s);
203 17317 av_freep(&s->url);
204 17317 av_freep(&s->name);
205 17317 av_free(s);
206 }
207
208 /**
209 * Copy all stream parameters from source to destination stream, with the
210 * exception of the index field, which is usually set by avformat_new_stream().
211 *
212 * @param dst pointer to destination AVStream
213 * @param src pointer to source AVStream
214 * @return >=0 on success, AVERROR code on error
215 */
216 20 static int stream_params_copy(AVStream *dst, const AVStream *src)
217 {
218 int ret;
219
220 20 dst->id = src->id;
221 20 dst->time_base = src->time_base;
222 20 dst->start_time = src->start_time;
223 20 dst->duration = src->duration;
224 20 dst->nb_frames = src->nb_frames;
225 20 dst->disposition = src->disposition;
226 20 dst->discard = src->discard;
227 20 dst->sample_aspect_ratio = src->sample_aspect_ratio;
228 20 dst->avg_frame_rate = src->avg_frame_rate;
229 20 dst->event_flags = src->event_flags;
230 20 dst->r_frame_rate = src->r_frame_rate;
231 20 dst->pts_wrap_bits = src->pts_wrap_bits;
232
233 20 av_dict_free(&dst->metadata);
234 20 ret = av_dict_copy(&dst->metadata, src->metadata, 0);
235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (ret < 0)
236 ✗ return ret;
237
238 20 ret = avcodec_parameters_copy(dst->codecpar, src->codecpar);
239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (ret < 0)
240 ✗ return ret;
241
242 20 av_packet_unref(&dst->attached_pic);
243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (src->attached_pic.data) {
244 ✗ ret = av_packet_ref(&dst->attached_pic, &src->attached_pic);
245 ✗ if (ret < 0)
246 ✗ return ret;
247 }
248
249 20 return 0;
250 }
251
252 20 AVStream *ff_stream_clone(AVFormatContext *dst_ctx, const AVStream *src)
253 {
254 AVStream *st;
255 int ret;
256
257 20 st = avformat_new_stream(dst_ctx, NULL);
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!st)
259 ✗ return NULL;
260
261 20 ret = stream_params_copy(st, src);
262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (ret < 0) {
263 ✗ ff_remove_stream(dst_ctx, st);
264 ✗ return NULL;
265 }
266
267 20 return st;
268 }
269
270 132 const char *avformat_stream_group_name(enum AVStreamGroupParamsType type)
271 {
272
6/8
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 41 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 32 times.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
132 switch(type) {
273 43 case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT: return "IAMF Audio Element";
274 41 case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION: return "IAMF Mix Presentation";
275 9 case AV_STREAM_GROUP_PARAMS_TILE_GRID: return "Tile Grid";
276 6 case AV_STREAM_GROUP_PARAMS_LCEVC: return "LCEVC (Split video and enhancement)";
277 32 case AV_STREAM_GROUP_PARAMS_TREF: return "Track Reference";
278 1 case AV_STREAM_GROUP_PARAMS_DOLBY_VISION: return "Dolby Vision (Split base and enhancement layer)";
279 ✗ case AV_STREAM_GROUP_PARAMS_GAIN_MAP: return "Gain Map (Split base rendition and gain map)";
280 }
281 ✗ return NULL;
282 }
283
284 579 AVProgram *av_new_program(AVFormatContext *ac, int id)
285 {
286 579 AVProgram *program = NULL;
287 int ret;
288
289 579 av_log(ac, AV_LOG_TRACE, "new_program: id=0x%04x\n", id);
290
291
2/2
✓ Branch 0 taken 477 times.
✓ Branch 1 taken 579 times.
1056 for (unsigned i = 0; i < ac->nb_programs; i++)
292
2/2
✓ Branch 0 taken 476 times.
✓ Branch 1 taken 1 times.
477 if (ac->programs[i]->id == id)
293 476 program = ac->programs[i];
294
295
2/2
✓ Branch 0 taken 103 times.
✓ Branch 1 taken 476 times.
579 if (!program) {
296 103 program = av_mallocz(sizeof(*program));
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 103 times.
103 if (!program)
298 ✗ return NULL;
299 103 ret = av_dynarray_add_nofree(&ac->programs, &ac->nb_programs, program);
300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 103 times.
103 if (ret < 0) {
301 ✗ av_free(program);
302 ✗ return NULL;
303 }
304 103 program->discard = AVDISCARD_NONE;
305 103 program->pmt_version = -1;
306 103 program->id = id;
307 103 program->pts_wrap_reference = AV_NOPTS_VALUE;
308 103 program->pts_wrap_behavior = AV_PTS_WRAP_IGNORE;
309 103 program->start_time =
310 103 program->end_time = AV_NOPTS_VALUE;
311 }
312 579 return program;
313 }
314
315 854 int av_program_add_stream_index2(AVFormatContext *ac, int progid, unsigned idx)
316 {
317 854 AVProgram *program = NULL;
318 void *tmp;
319
320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 854 times.
854 if (idx >= ac->nb_streams) {
321 ✗ av_log(ac, AV_LOG_ERROR, "stream index %d is greater than stream count %d\n", idx, ac->nb_streams);
322 ✗ return AVERROR(EINVAL);
323 }
324
325
1/2
✓ Branch 0 taken 854 times.
✗ Branch 1 not taken.
854 for (unsigned i = 0; i < ac->nb_programs; i++) {
326
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 854 times.
854 if (ac->programs[i]->id != progid)
327 ✗ continue;
328 854 program = ac->programs[i];
329
2/2
✓ Branch 0 taken 5981 times.
✓ Branch 1 taken 571 times.
6552 for (unsigned j = 0; j < program->nb_stream_indexes; j++)
330
2/2
✓ Branch 0 taken 283 times.
✓ Branch 1 taken 5698 times.
5981 if (program->stream_index[j] == idx)
331 283 return 0;
332
333 571 tmp = av_realloc_array(program->stream_index, program->nb_stream_indexes+1, sizeof(unsigned int));
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 571 times.
571 if (!tmp)
335 ✗ return AVERROR(ENOMEM);
336 571 program->stream_index = tmp;
337 571 program->stream_index[program->nb_stream_indexes++] = idx;
338 571 return 0;
339 }
340
341 ✗ av_log(ac, AV_LOG_ERROR, "no program with id %d found\n", progid);
342 ✗ return AVERROR(EINVAL);
343 }
344
345 854 void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx)
346 {
347 854 av_program_add_stream_index2(ac, progid, idx);
348 854 return;
349 }
350
351 ✗ int av_program_copy(AVFormatContext *dst, const AVFormatContext *src, int progid, int flags)
352 {
353 ✗ const AVProgram *src_prog = NULL;
354 ✗ AVProgram *dst_prog = NULL;
355 ✗ int ret, idx = -1, match = -1;
356 ✗ int overwrite = flags & AVFMT_PROGCOPY_OVERWRITE;
357
358 ✗ if ((flags & AVFMT_PROGCOPY_MATCH_BY_ID) && (flags & AVFMT_PROGCOPY_MATCH_BY_INDEX))
359 ✗ return AVERROR(EINVAL);
360 ✗ else if (flags & AVFMT_PROGCOPY_MATCH_BY_ID)
361 ✗ match = 0;
362 ✗ else if (flags & AVFMT_PROGCOPY_MATCH_BY_INDEX)
363 ✗ match = 1;
364
365 ✗ for (unsigned i = 0; i < src->nb_programs; i++) {
366 ✗ if (src->programs[i]->id == progid) {
367 ✗ if (src_prog) {
368 ✗ av_log(dst, AV_LOG_ERROR, "multiple programs found in source with same id 0x%04x. Not copying.\n", progid);
369 ✗ return AVERROR(EINVAL);
370 } else {
371 ✗ src_prog = src->programs[i];
372 }
373 }
374 }
375
376 ✗ if (!src_prog) {
377 ✗ av_log(dst, AV_LOG_ERROR, "source program not found: id=0x%04x\n", progid);
378 ✗ return AVERROR(EINVAL);
379 }
380
381 ✗ for (unsigned i = 0; i < dst->nb_programs; i++) {
382 ✗ if (dst->programs[i]->id == progid) {
383 ✗ if (idx > -1) {
384 ✗ av_log(dst, AV_LOG_ERROR, "multiple programs found in target with same id 0x%04x. Not copying.\n", progid);
385 ✗ return AVERROR(EINVAL);
386 } else {
387 ✗ idx = i;
388 }
389 }
390 }
391
392 ✗ if (idx >= 0 && !overwrite)
393 ✗ return AVERROR(EEXIST);
394
395 ✗ av_log(dst, AV_LOG_TRACE, "%s program: id=0x%04x\n", idx >= 0 ? "overwriting" : "copying", progid);
396
397 ✗ if (idx >= 0) {
398 ✗ dst_prog = dst->programs[idx];
399 ✗ av_dict_free(&dst_prog->metadata);
400 ✗ av_freep(&dst_prog->stream_index);
401 ✗ dst_prog->nb_stream_indexes = 0;
402 } else {
403 ✗ dst_prog = av_new_program(dst, progid);
404 ✗ if (!dst_prog)
405 ✗ return AVERROR(ENOMEM);
406 }
407
408 /* public fields */
409 ✗ dst_prog->id = src_prog->id;
410 ✗ dst_prog->flags = src_prog->flags;
411 ✗ dst_prog->discard = src_prog->discard;
412 ✗ dst_prog->program_num = src_prog->program_num;
413 ✗ dst_prog->pmt_pid = src_prog->pmt_pid;
414 ✗ dst_prog->pcr_pid = src_prog->pcr_pid;
415 ✗ dst_prog->pmt_version = src_prog->pmt_version;
416
417 ✗ if (match == -1 && src->nb_streams) {
418 ✗ match = 0;
419 ✗ for (unsigned i = 0; i < src->nb_streams && !match; i++) {
420 ✗ int src_id = src->streams[i]->id;
421 ✗ if (!src_id) {
422 ✗ match = 1;
423 ✗ break;
424 }
425 ✗ for (unsigned j=i+1; j < src->nb_streams; j++) {
426 ✗ int sib_id = src->streams[j]->id;
427 ✗ if (src_id == sib_id) {
428 ✗ match = 1;
429 ✗ break;
430 }
431 }
432 }
433 }
434
435 ✗ for (unsigned i = 0; i < dst->nb_streams; i++) {
436 ✗ int dst_val = match ? i : dst->streams[i]->id;
437
438 ✗ for (unsigned j = 0; j < src_prog->nb_stream_indexes; j++) {
439 ✗ int src_val = match ? src_prog->stream_index[j] : src->streams[src_prog->stream_index[j]]->id;
440
441 ✗ if (dst_val == src_val) {
442 ✗ ret = av_program_add_stream_index2(dst, dst_prog->id, i);
443 ✗ if (ret < 0)
444 ✗ return ret;
445 }
446 }
447 }
448
449 ✗ ret = av_dict_copy(&dst_prog->metadata, src_prog->metadata, 0);
450 ✗ if (ret < 0)
451 ✗ return ret;
452
453 ✗ return 0;
454 }
455
456 16583 AVProgram *av_find_program_from_stream(AVFormatContext *ic, AVProgram *last, int s)
457 {
458
2/2
✓ Branch 0 taken 3143 times.
✓ Branch 1 taken 15063 times.
18206 for (unsigned i = 0; i < ic->nb_programs; i++) {
459
2/2
✓ Branch 0 taken 1618 times.
✓ Branch 1 taken 1525 times.
3143 if (ic->programs[i] == last) {
460 1618 last = NULL;
461 } else {
462
1/2
✓ Branch 0 taken 1525 times.
✗ Branch 1 not taken.
1525 if (!last)
463
2/2
✓ Branch 0 taken 12579 times.
✓ Branch 1 taken 5 times.
12584 for (unsigned j = 0; j < ic->programs[i]->nb_stream_indexes; j++)
464
2/2
✓ Branch 0 taken 1520 times.
✓ Branch 1 taken 11059 times.
12579 if (ic->programs[i]->stream_index[j] == s)
465 1520 return ic->programs[i];
466 }
467 }
468 15063 return NULL;
469 }
470
471 2015 int av_find_default_stream_index(AVFormatContext *s)
472 {
473 2015 int best_stream = 0;
474 2015 int best_score = INT_MIN;
475
476
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2015 times.
2015 if (s->nb_streams <= 0)
477 ✗ return -1;
478
2/2
✓ Branch 0 taken 2451 times.
✓ Branch 1 taken 2015 times.
4466 for (unsigned i = 0; i < s->nb_streams; i++) {
479 2451 const AVStream *const st = s->streams[i];
480 2451 const FFStream *const sti = cffstream(st);
481 2451 int score = 0;
482
2/2
✓ Branch 0 taken 1412 times.
✓ Branch 1 taken 1039 times.
2451 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
483
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1405 times.
1412 if (st->disposition & AV_DISPOSITION_ATTACHED_PIC)
484 7 score -= 400;
485
3/4
✓ Branch 0 taken 1317 times.
✓ Branch 1 taken 95 times.
✓ Branch 2 taken 1317 times.
✗ Branch 3 not taken.
1412 if (st->codecpar->width && st->codecpar->height)
486 1317 score += 50;
487 1412 score+= 25;
488 }
489
2/2
✓ Branch 0 taken 1008 times.
✓ Branch 1 taken 1443 times.
2451 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
490
2/2
✓ Branch 0 taken 975 times.
✓ Branch 1 taken 33 times.
1008 if (st->codecpar->sample_rate)
491 975 score += 50;
492 }
493
2/2
✓ Branch 0 taken 2024 times.
✓ Branch 1 taken 427 times.
2451 if (sti->codec_info_nb_frames)
494 2024 score += 12;
495
496
2/2
✓ Branch 0 taken 2446 times.
✓ Branch 1 taken 5 times.
2451 if (st->discard != AVDISCARD_ALL)
497 2446 score += 200;
498
499
2/2
✓ Branch 0 taken 2070 times.
✓ Branch 1 taken 381 times.
2451 if (score > best_score) {
500 2070 best_score = score;
501 2070 best_stream = i;
502 }
503 }
504 2015 return best_stream;
505 }
506
507 19 int av_find_best_stream(AVFormatContext *ic, enum AVMediaType type,
508 int wanted_stream_nb, int related_stream,
509 const AVCodec **decoder_ret, int flags)
510 {
511 19 int nb_streams = ic->nb_streams;
512 19 int ret = AVERROR_STREAM_NOT_FOUND;
513 19 int best_count = -1, best_multiframe = -1, best_disposition = -1;
514 int count, multiframe, disposition;
515 19 int64_t best_bitrate = -1;
516 int64_t bitrate;
517 19 unsigned *program = NULL;
518 19 const AVCodec *decoder = NULL, *best_decoder = NULL;
519
520
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
19 if (related_stream >= 0 && wanted_stream_nb < 0) {
521 ✗ AVProgram *p = av_find_program_from_stream(ic, NULL, related_stream);
522 ✗ if (p) {
523 ✗ program = p->stream_index;
524 ✗ nb_streams = p->nb_stream_indexes;
525 }
526 }
527
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 19 times.
41 for (unsigned i = 0; i < nb_streams; i++) {
528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 int real_stream_index = program ? program[i] : i;
529 22 AVStream *st = ic->streams[real_stream_index];
530 22 AVCodecParameters *par = st->codecpar;
531 22 const AVCodecDescriptor *desc = avcodec_descriptor_get(par->codec_id);
532
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 19 times.
22 if (par->codec_type != type)
533 3 continue;
534
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
19 if (desc && (desc->props & AV_CODEC_PROP_ENHANCEMENT))
535 ✗ continue;
536
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
19 if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb)
537 ✗ continue;
538
4/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
19 if (type == AVMEDIA_TYPE_AUDIO && !(par->ch_layout.nb_channels && par->sample_rate))
539 ✗ continue;
540
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (decoder_ret) {
541 ✗ decoder = ff_find_decoder(ic, st, par->codec_id);
542 ✗ if (!decoder) {
543 ✗ if (ret < 0)
544 ✗ ret = AVERROR_DECODER_NOT_FOUND;
545 ✗ continue;
546 }
547 }
548 19 disposition = !(st->disposition & (AV_DISPOSITION_HEARING_IMPAIRED | AV_DISPOSITION_VISUAL_IMPAIRED))
549 19 + !! (st->disposition & AV_DISPOSITION_DEFAULT);
550 19 count = ffstream(st)->codec_info_nb_frames;
551 19 bitrate = par->bit_rate;
552 19 multiframe = FFMIN(5, count);
553
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
19 if ((best_disposition > disposition) ||
554
1/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
19 (best_disposition == disposition && best_multiframe > multiframe) ||
555
1/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 19 times.
19 (best_disposition == disposition && best_multiframe == multiframe && best_bitrate > bitrate) ||
556 ✗ (best_disposition == disposition && best_multiframe == multiframe && best_bitrate == bitrate && best_count >= count))
557 ✗ continue;
558 19 best_disposition = disposition;
559 19 best_count = count;
560 19 best_bitrate = bitrate;
561 19 best_multiframe = multiframe;
562 19 ret = real_stream_index;
563 19 best_decoder = decoder;
564
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
19 if (program && i == nb_streams - 1 && ret < 0) {
565 ✗ program = NULL;
566 ✗ nb_streams = ic->nb_streams;
567 /* no related stream found, try again with everything */
568 ✗ i = 0;
569 }
570 }
571
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (decoder_ret)
572 ✗ *decoder_ret = best_decoder;
573 19 return ret;
574 }
575
576 /**
577 * Matches a stream specifier (but ignores requested index).
578 *
579 * @param indexptr set to point to the requested stream index if there is one
580 *
581 * @return <0 on error
582 * 0 if st is NOT a matching stream
583 * >0 if st is a matching stream
584 */
585 113 static int match_stream_specifier(const AVFormatContext *s, const AVStream *st,
586 const char *spec, const char **indexptr,
587 const AVStreamGroup **g, const AVProgram **p)
588 {
589 113 int match = 1; /* Stores if the specifier matches so far. */
590
2/2
✓ Branch 0 taken 175 times.
✓ Branch 1 taken 55 times.
230 while (*spec) {
591
3/4
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 117 times.
✓ Branch 2 taken 58 times.
✗ Branch 3 not taken.
175 if (*spec <= '9' && *spec >= '0') { /* opt:index */
592
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 25 times.
58 if (indexptr)
593 33 *indexptr = spec;
594 58 return match;
595
7/8
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 83 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 12 times.
✗ Branch 7 not taken.
117 } else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
596
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
117 *spec == 't' || *spec == 'V') { /* opt:[vasdtV] */
597 enum AVMediaType type;
598 105 int nopic = 0;
599
600
3/7
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
105 switch (*spec++) {
601 83 case 'v': type = AVMEDIA_TYPE_VIDEO; break;
602 18 case 'a': type = AVMEDIA_TYPE_AUDIO; break;
603 4 case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
604 ✗ case 'd': type = AVMEDIA_TYPE_DATA; break;
605 ✗ case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
606 ✗ case 'V': type = AVMEDIA_TYPE_VIDEO; nopic = 1; break;
607 ✗ default: av_assert0(0);
608 }
609
3/4
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 50 times.
105 if (*spec && *spec++ != ':') /* If we are not at the end, then another specifier must follow. */
610 ✗ return AVERROR(EINVAL);
611
612
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 72 times.
105 if (type != st->codecpar->codec_type)
613 33 match = 0;
614
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 105 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
105 if (nopic && (st->disposition & AV_DISPOSITION_ATTACHED_PIC))
615 ✗ match = 0;
616
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
12 } else if (*spec == 'g' && *(spec + 1) == ':') {
617 ✗ int64_t group_idx = -1, group_id = -1;
618 ✗ int found = 0;
619 char *endptr;
620 ✗ spec += 2;
621 ✗ if (*spec == '#' || (*spec == 'i' && *(spec + 1) == ':')) {
622 ✗ spec += 1 + (*spec == 'i');
623 ✗ group_id = strtol(spec, &endptr, 0);
624 ✗ if (spec == endptr || (*endptr && *endptr++ != ':'))
625 ✗ return AVERROR(EINVAL);
626 ✗ spec = endptr;
627 } else {
628 ✗ group_idx = strtol(spec, &endptr, 0);
629 /* Disallow empty id and make sure that if we are not at the end, then another specifier must follow. */
630 ✗ if (spec == endptr || (*endptr && *endptr++ != ':'))
631 ✗ return AVERROR(EINVAL);
632 ✗ spec = endptr;
633 }
634 ✗ if (match) {
635 ✗ if (group_id > 0) {
636 ✗ for (unsigned i = 0; i < s->nb_stream_groups; i++) {
637 ✗ if (group_id == s->stream_groups[i]->id) {
638 ✗ group_idx = i;
639 ✗ break;
640 }
641 }
642 }
643 ✗ if (group_idx < 0 || group_idx >= s->nb_stream_groups)
644 ✗ return AVERROR(EINVAL);
645 ✗ for (unsigned j = 0; j < s->stream_groups[group_idx]->nb_streams; j++) {
646 ✗ if (st->index == s->stream_groups[group_idx]->streams[j]->index) {
647 ✗ found = 1;
648 ✗ if (g)
649 ✗ *g = s->stream_groups[group_idx];
650 ✗ break;
651 }
652 }
653 }
654 ✗ if (!found)
655 ✗ match = 0;
656
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
24 } else if (*spec == 'p' && *(spec + 1) == ':') {
657 int prog_id;
658 12 int found = 0;
659 char *endptr;
660 12 spec += 2;
661 12 prog_id = strtol(spec, &endptr, 0);
662 /* Disallow empty id and make sure that if we are not at the end, then another specifier must follow. */
663
3/6
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 12 times.
12 if (spec == endptr || (*endptr && *endptr++ != ':'))
664 ✗ return AVERROR(EINVAL);
665 12 spec = endptr;
666
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (match) {
667
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
24 for (unsigned i = 0; i < s->nb_programs; i++) {
668
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (s->programs[i]->id != prog_id)
669 ✗ continue;
670
671
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
18 for (unsigned j = 0; j < s->programs[i]->nb_stream_indexes; j++) {
672
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 6 times.
16 if (st->index == s->programs[i]->stream_index[j]) {
673 10 found = 1;
674
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
10 if (p)
675 4 *p = s->programs[i];
676 10 i = s->nb_programs;
677 10 break;
678 }
679 }
680 }
681 }
682
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
12 if (!found)
683 2 match = 0;
684 ✗ } else if (*spec == '#' ||
685 ✗ (*spec == 'i' && *(spec + 1) == ':')) {
686 int stream_id;
687 char *endptr;
688 ✗ spec += 1 + (*spec == 'i');
689 ✗ stream_id = strtol(spec, &endptr, 0);
690 ✗ if (spec == endptr || *endptr) /* Disallow empty id and make sure we are at the end. */
691 ✗ return AVERROR(EINVAL);
692 ✗ return match && (stream_id == st->id);
693 ✗ } else if (*spec == 'm' && *(spec + 1) == ':') {
694 const AVDictionaryEntry *tag;
695 int ret;
696
697 ✗ if (match) {
698 ✗ spec += 2;
699 ✗ const char *val = strchr(spec, ':');
700
701 ✗ char *key = val ? av_strndup(spec, val - spec) : av_strdup(spec);
702 ✗ if (!key)
703 ✗ return AVERROR(ENOMEM);
704
705 ✗ tag = av_dict_get(st->metadata, key, NULL, 0);
706 ✗ if (tag) {
707 ✗ if (!val || !strcmp(tag->value, val + 1))
708 ✗ ret = 1;
709 else
710 ✗ ret = 0;
711 } else
712 ✗ ret = 0;
713
714 ✗ av_freep(&key);
715 }
716 ✗ return match && ret;
717 ✗ } else if (*spec == 'u' && *(spec + 1) == '\0') {
718 ✗ const AVCodecParameters *par = st->codecpar;
719 int val;
720 ✗ switch (par->codec_type) {
721 ✗ case AVMEDIA_TYPE_AUDIO:
722 ✗ val = par->sample_rate && par->ch_layout.nb_channels;
723 ✗ if (par->format == AV_SAMPLE_FMT_NONE)
724 ✗ return 0;
725 ✗ break;
726 ✗ case AVMEDIA_TYPE_VIDEO:
727 ✗ val = par->width && par->height;
728 ✗ if (par->format == AV_PIX_FMT_NONE)
729 ✗ return 0;
730 ✗ break;
731 ✗ case AVMEDIA_TYPE_UNKNOWN:
732 ✗ val = 0;
733 ✗ break;
734 ✗ default:
735 ✗ val = 1;
736 ✗ break;
737 }
738 ✗ return match && (par->codec_id != AV_CODEC_ID_NONE && val != 0);
739 } else {
740 ✗ return AVERROR(EINVAL);
741 }
742 }
743
744 55 return match;
745 }
746
747 88 int avformat_match_stream_specifier(AVFormatContext *s, AVStream *st,
748 const char *spec)
749 {
750 int ret, index;
751 char *endptr;
752 88 const char *indexptr = NULL;
753 88 const AVStreamGroup *g = NULL;
754 88 const AVProgram *p = NULL;
755 int nb_streams;
756
757 88 ret = match_stream_specifier(s, st, spec, &indexptr, &g, &p);
758
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
88 if (ret < 0)
759 ✗ goto error;
760
761
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 33 times.
88 if (!indexptr)
762 55 return ret;
763
764 33 index = strtol(indexptr, &endptr, 0);
765
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (*endptr) { /* We can't have anything after the requested index. */
766 ✗ ret = AVERROR(EINVAL);
767 ✗ goto error;
768 }
769
770 /* This is not really needed but saves us a loop for simple stream index specifiers. */
771
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 25 times.
33 if (spec == indexptr)
772 8 return (index == st->index);
773
774 /* If we requested a matching stream index, we have to ensure st is that. */
775
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 21 times.
25 nb_streams = g ? g->nb_streams : (p ? p->nb_stream_indexes : s->nb_streams);
776
3/4
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 18 times.
43 for (int i = 0; i < nb_streams && index >= 0; i++) {
777
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 21 times.
25 unsigned idx = g ? g->streams[i]->index : (p ? p->stream_index[i] : i);
778 25 const AVStream *candidate = s->streams[idx];
779 25 ret = match_stream_specifier(s, candidate, spec, NULL, NULL, NULL);
780
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (ret < 0)
781 ✗ goto error;
782
4/6
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 18 times.
25 if (ret > 0 && index-- == 0 && st == candidate)
783 7 return 1;
784 }
785 18 return 0;
786
787 ✗ error:
788 ✗ if (ret == AVERROR(EINVAL))
789 ✗ av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
790 ✗ return ret;
791 }
792
793 996 AVRational av_guess_sample_aspect_ratio(AVFormatContext *format, AVStream *stream, AVFrame *frame)
794 {
795 996 AVRational undef = {0, 1};
796
1/2
✓ Branch 0 taken 996 times.
✗ Branch 1 not taken.
996 AVRational stream_sample_aspect_ratio = stream ? stream->sample_aspect_ratio : undef;
797
2/4
✓ Branch 0 taken 996 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 996 times.
✗ Branch 3 not taken.
996 AVRational codec_sample_aspect_ratio = stream && stream->codecpar ? stream->codecpar->sample_aspect_ratio : undef;
798
2/2
✓ Branch 0 taken 843 times.
✓ Branch 1 taken 153 times.
996 AVRational frame_sample_aspect_ratio = frame ? frame->sample_aspect_ratio : codec_sample_aspect_ratio;
799
800 996 av_reduce(&stream_sample_aspect_ratio.num, &stream_sample_aspect_ratio.den,
801 996 stream_sample_aspect_ratio.num, stream_sample_aspect_ratio.den, INT_MAX);
802
3/4
✓ Branch 0 taken 603 times.
✓ Branch 1 taken 393 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 603 times.
996 if (stream_sample_aspect_ratio.num <= 0 || stream_sample_aspect_ratio.den <= 0)
803 393 stream_sample_aspect_ratio = undef;
804
805 996 av_reduce(&frame_sample_aspect_ratio.num, &frame_sample_aspect_ratio.den,
806 996 frame_sample_aspect_ratio.num, frame_sample_aspect_ratio.den, INT_MAX);
807
3/4
✓ Branch 0 taken 565 times.
✓ Branch 1 taken 431 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 565 times.
996 if (frame_sample_aspect_ratio.num <= 0 || frame_sample_aspect_ratio.den <= 0)
808 431 frame_sample_aspect_ratio = undef;
809
810
2/2
✓ Branch 0 taken 603 times.
✓ Branch 1 taken 393 times.
996 if (stream_sample_aspect_ratio.num)
811 603 return stream_sample_aspect_ratio;
812 else
813 393 return frame_sample_aspect_ratio;
814 }
815
816 5730 AVRational av_guess_frame_rate(AVFormatContext *format, AVStream *st, AVFrame *frame)
817 {
818 5730 AVRational fr = st->r_frame_rate;
819 5730 const AVCodecDescriptor *desc = cffstream(st)->codec_desc;
820 5730 AVRational avg_fr = st->avg_frame_rate;
821
822
7/10
✓ Branch 0 taken 5078 times.
✓ Branch 1 taken 652 times.
✓ Branch 2 taken 5078 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5078 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5078 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5053 times.
✓ Branch 9 taken 25 times.
10808 if (avg_fr.num > 0 && avg_fr.den > 0 && fr.num > 0 && fr.den > 0 &&
823
2/2
✓ Branch 2 taken 399 times.
✓ Branch 3 taken 4654 times.
10131 av_q2d(avg_fr) < 70 && av_q2d(fr) > 210) {
824 399 fr = avg_fr;
825 }
826
827
3/4
✓ Branch 0 taken 5730 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 330 times.
✓ Branch 3 taken 5400 times.
5730 if (desc && (desc->props & AV_CODEC_PROP_FIELDS)) {
828 330 const AVCodecContext *const avctx = ffstream(st)->avctx;
829 330 AVRational codec_fr = avctx->framerate;
830
831
3/4
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 200 times.
✓ Branch 2 taken 130 times.
✗ Branch 3 not taken.
330 if ( codec_fr.num > 0 && codec_fr.den > 0 &&
832
5/6
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 37 times.
✓ Branch 5 taken 93 times.
✓ Branch 7 taken 35 times.
✓ Branch 8 taken 2 times.
130 (fr.num == 0 || av_q2d(codec_fr) < av_q2d(fr)*0.7 && fabs(1.0 - av_q2d(av_div_q(avg_fr, fr))) > 0.1))
833 35 fr = codec_fr;
834 }
835
836 5730 return fr;
837 }
838
839 23523 void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits,
840 unsigned int pts_num, unsigned int pts_den)
841 {
842 23523 FFStream *const sti = ffstream(st);
843 AVRational new_tb;
844
1/2
✓ Branch 1 taken 23523 times.
✗ Branch 2 not taken.
23523 if (av_reduce(&new_tb.num, &new_tb.den, pts_num, pts_den, INT_MAX)) {
845
2/2
✓ Branch 0 taken 583 times.
✓ Branch 1 taken 22940 times.
23523 if (new_tb.num != pts_num)
846 583 av_log(NULL, AV_LOG_DEBUG,
847 "st:%d removing common factor %d from timebase\n",
848 583 st->index, pts_num / new_tb.num);
849 } else
850 ✗ av_log(NULL, AV_LOG_WARNING,
851 "st:%d has too large timebase, reducing\n", st->index);
852
853
3/4
✓ Branch 0 taken 23522 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 23522 times.
23523 if (new_tb.num <= 0 || new_tb.den <= 0) {
854 1 av_log(NULL, AV_LOG_ERROR,
855 "Ignoring attempt to set invalid timebase %d/%d for st:%d\n",
856 new_tb.num, new_tb.den,
857 st->index);
858 1 return;
859 }
860 23522 st->time_base = new_tb;
861
2/2
✓ Branch 0 taken 19001 times.
✓ Branch 1 taken 4521 times.
23522 if (sti->avctx)
862 19001 sti->avctx->pkt_timebase = new_tb;
863 23522 st->pts_wrap_bits = pts_wrap_bits;
864 }
865
866 10437 const AVCodec *ff_find_decoder(AVFormatContext *s, const AVStream *st,
867 enum AVCodecID codec_id)
868 {
869
4/4
✓ Branch 0 taken 6978 times.
✓ Branch 1 taken 3173 times.
✓ Branch 2 taken 107 times.
✓ Branch 3 taken 179 times.
10437 switch (st->codecpar->codec_type) {
870 6978 case AVMEDIA_TYPE_VIDEO:
871
2/2
✓ Branch 0 taken 3151 times.
✓ Branch 1 taken 3827 times.
6978 if (s->video_codec) return s->video_codec;
872 3827 break;
873 3173 case AVMEDIA_TYPE_AUDIO:
874
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 3145 times.
3173 if (s->audio_codec) return s->audio_codec;
875 3145 break;
876 107 case AVMEDIA_TYPE_SUBTITLE:
877
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 107 times.
107 if (s->subtitle_codec) return s->subtitle_codec;
878 107 break;
879 }
880
881 7258 return avcodec_find_decoder(codec_id);
882 }
883
884 98 int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src)
885 {
886 #define OFF(field) offsetof(AVFormatContext, field)
887 static const unsigned offsets[] = {
888 OFF(codec_whitelist), OFF(format_whitelist),
889 OFF(protocol_whitelist), OFF(protocol_blacklist),
890 };
891 #undef OFF
892
4/8
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 98 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 98 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 98 times.
98 av_assert0(!dst->codec_whitelist &&
893 !dst->format_whitelist &&
894 !dst->protocol_whitelist &&
895 !dst->protocol_blacklist);
896
2/2
✓ Branch 0 taken 392 times.
✓ Branch 1 taken 98 times.
490 for (unsigned i = 0; i < FF_ARRAY_ELEMS(offsets); i++) {
897 392 const char *src_str = *(char *const*)((const char*)src + offsets[i]);
898
899
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 294 times.
392 if (src_str) {
900 98 char *dst_str = av_strdup(src_str);
901
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 98 times.
98 if (!dst_str) {
902 ✗ av_log(dst, AV_LOG_ERROR, "Failed to duplicate black/whitelist\n");
903 ✗ return AVERROR(ENOMEM);
904 }
905
906 98 *(char **)((char*)dst + offsets[i]) = dst_str;
907 }
908 }
909
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 98 times.
98 if (src->recursion_limit <= 0) {
910 ✗ av_log(dst, AV_LOG_ERROR, "Too deep recursion\n");
911 ✗ return AVERROR_INVALIDDATA;
912 }
913 98 dst->recursion_limit = src->recursion_limit - 1;
914 98 return 0;
915 }
916
917 663285 int ff_is_intra_only(enum AVCodecID id)
918 {
919 663285 const AVCodecDescriptor *d = avcodec_descriptor_get(id);
920
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 663220 times.
663285 if (!d)
921 65 return 0;
922
4/4
✓ Branch 0 taken 403502 times.
✓ Branch 1 taken 259718 times.
✓ Branch 2 taken 400034 times.
✓ Branch 3 taken 3468 times.
663220 if ((d->type == AVMEDIA_TYPE_VIDEO || d->type == AVMEDIA_TYPE_AUDIO) &&
923
2/2
✓ Branch 0 taken 211424 times.
✓ Branch 1 taken 448328 times.
659752 !(d->props & AV_CODEC_PROP_INTRA_ONLY))
924 211424 return 0;
925 451796 return 1;
926 }
927
928 107 void ff_format_set_url(AVFormatContext *s, char *url)
929 {
930
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 107 times.
107 av_assert0(url);
931 107 av_freep(&s->url);
932 107 s->url = url;
933 107 }
934
935 ✗ int ff_format_check_set_url(AVFormatContext *s, const char *url)
936 {
937 URLComponents uc;
938 ✗ av_assert0(url);
939 char proto[64];
940
941 ✗ int ret = ff_url_decompose(&uc, url, NULL);
942 ✗ if (ret < 0)
943 ✗ return ret;
944 ✗ av_strlcpy(proto, uc.scheme, FFMIN(sizeof(proto), uc.url_component_end_scheme - uc.scheme));
945
946 ✗ if (s->protocol_whitelist && av_match_list(proto, s->protocol_whitelist, ',') <= 0) {
947 ✗ av_log(s, AV_LOG_ERROR, "Protocol '%s' not on whitelist '%s'!\n", proto, s->protocol_whitelist);
948 ✗ return AVERROR(EINVAL);
949 }
950
951 ✗ if (s->protocol_blacklist && av_match_list(proto, s->protocol_blacklist, ',') > 0) {
952 ✗ av_log(s, AV_LOG_ERROR, "Protocol '%s' on blacklist '%s'!\n", proto, s->protocol_blacklist);
953 ✗ return AVERROR(EINVAL);
954 }
955
956 ✗ char *urldup = av_strdup(url);
957 ✗ if (!urldup)
958 ✗ return AVERROR(ENOMEM);
959
960 ✗ av_freep(&s->url);
961 ✗ s->url = urldup;
962 ✗ return 0;
963 }
964
965
966 111658 int ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
967 {
968 111658 int ret = 0;
969
2/2
✓ Branch 0 taken 108074 times.
✓ Branch 1 taken 3584 times.
111658 if (*pb)
970 108074 ret = s->io_close2(s, *pb);
971 111658 *pb = NULL;
972 111658 return ret;
973 }
974