../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
406 / 630
64.4%
Branches:
267 / 566
47.2%
Functions:
25 / 27
92.6%

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