FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/avformat.c
Date: 2026-10-13 05:06:32
Exec Total Coverage
Lines: 394 623 63.2%
Functions: 24 27 88.9%
Branches: 255 557 45.8%

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