FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/avformat.c
Date: 2024-04-25 05:10:44
Exec Total Coverage
Lines: 419 584 71.7%
Functions: 25 29 86.2%
Branches: 301 555 54.2%

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 <math.h>
23 #include "libavutil/avassert.h"
24 #include "libavutil/avstring.h"
25 #include "libavutil/channel_layout.h"
26 #include "libavutil/frame.h"
27 #include "libavutil/iamf.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/pixfmt.h"
32 #include "libavutil/samplefmt.h"
33 #include "libavcodec/avcodec.h"
34 #include "libavcodec/codec.h"
35 #include "libavcodec/bsf.h"
36 #include "libavcodec/codec_desc.h"
37 #include "libavcodec/packet_internal.h"
38 #include "avformat.h"
39 #include "avio.h"
40 #include "demux.h"
41 #include "mux.h"
42 #include "internal.h"
43
44 15115 void ff_free_stream(AVStream **pst)
45 {
46 15115 AVStream *st = *pst;
47 15115 FFStream *const sti = ffstream(st);
48
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15115 times.
15115 if (!st)
50 return;
51
52 #if FF_API_AVSTREAM_SIDE_DATA
53 FF_DISABLE_DEPRECATION_WARNINGS
54
2/2
✓ Branch 0 taken 491 times.
✓ Branch 1 taken 15115 times.
15606 for (int i = 0; i < st->nb_side_data; i++)
55 491 av_freep(&st->side_data[i].data);
56 15115 av_freep(&st->side_data);
57 FF_ENABLE_DEPRECATION_WARNINGS
58 #endif
59
60
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 15059 times.
15115 if (st->attached_pic.data)
61 56 av_packet_unref(&st->attached_pic);
62
63 15115 av_parser_close(sti->parser);
64 15115 avcodec_free_context(&sti->avctx);
65 15115 av_bsf_free(&sti->bsfc);
66 15115 av_freep(&sti->index_entries);
67 15115 av_freep(&sti->probe_data.buf);
68
69 15115 av_bsf_free(&sti->extract_extradata.bsf);
70
71
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 15087 times.
15115 if (sti->info) {
72 28 av_freep(&sti->info->duration_error);
73 28 av_freep(&sti->info);
74 }
75
76 15115 av_dict_free(&st->metadata);
77 15115 avcodec_parameters_free(&st->codecpar);
78 15115 av_freep(&st->priv_data);
79
80 15115 av_freep(pst);
81 }
82
83 66 void ff_free_stream_group(AVStreamGroup **pstg)
84 {
85 66 AVStreamGroup *stg = *pstg;
86
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (!stg)
88 return;
89
90 66 av_freep(&stg->streams);
91 66 av_dict_free(&stg->metadata);
92 66 av_freep(&stg->priv_data);
93
3/4
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
66 switch (stg->type) {
94 30 case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT: {
95 30 av_iamf_audio_element_free(&stg->params.iamf_audio_element);
96 30 break;
97 }
98 30 case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION: {
99 30 av_iamf_mix_presentation_free(&stg->params.iamf_mix_presentation);
100 30 break;
101 }
102 6 case AV_STREAM_GROUP_PARAMS_TILE_GRID:
103 6 av_opt_free(stg->params.tile_grid);
104 6 av_freep(&stg->params.tile_grid->offsets);
105 6 av_freep(&stg->params.tile_grid);
106 6 break;
107 default:
108 break;
109 }
110
111 66 av_freep(pstg);
112 }
113
114 10 void ff_remove_stream(AVFormatContext *s, AVStream *st)
115 {
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 av_assert0(s->nb_streams>0);
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 av_assert0(s->streams[ s->nb_streams - 1 ] == st);
118
119 10 ff_free_stream(&s->streams[ --s->nb_streams ]);
120 10 }
121
122 void ff_remove_stream_group(AVFormatContext *s, AVStreamGroup *stg)
123 {
124 av_assert0(s->nb_stream_groups > 0);
125 av_assert0(s->stream_groups[ s->nb_stream_groups - 1 ] == stg);
126
127 ff_free_stream_group(&s->stream_groups[ --s->nb_stream_groups ]);
128 }
129
130 /* XXX: suppress the packet queue */
131 18850 void ff_flush_packet_queue(AVFormatContext *s)
132 {
133 18850 FFFormatContext *const si = ffformatcontext(s);
134 18850 avpriv_packet_list_free(&si->parse_queue);
135 18850 avpriv_packet_list_free(&si->packet_buffer);
136 18850 avpriv_packet_list_free(&si->raw_packet_buffer);
137
138 18850 si->raw_packet_buffer_size = 0;
139 18850 }
140
141 14478 void avformat_free_context(AVFormatContext *s)
142 {
143 FFFormatContext *si;
144
145
2/2
✓ Branch 0 taken 485 times.
✓ Branch 1 taken 13993 times.
14478 if (!s)
146 485 return;
147 13993 si = ffformatcontext(s);
148
149
5/6
✓ Branch 0 taken 6826 times.
✓ Branch 1 taken 7167 times.
✓ Branch 3 taken 3887 times.
✓ Branch 4 taken 2939 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3887 times.
13993 if (s->oformat && ffofmt(s->oformat)->deinit && si->initialized)
150 ffofmt(s->oformat)->deinit(s);
151
152 13993 av_opt_free(s);
153
5/6
✓ Branch 0 taken 7167 times.
✓ Branch 1 taken 6826 times.
✓ Branch 2 taken 6188 times.
✓ Branch 3 taken 979 times.
✓ Branch 4 taken 6188 times.
✗ Branch 5 not taken.
13993 if (s->iformat && s->iformat->priv_class && s->priv_data)
154 6188 av_opt_free(s->priv_data);
155
5/6
✓ Branch 0 taken 6826 times.
✓ Branch 1 taken 7167 times.
✓ Branch 2 taken 3958 times.
✓ Branch 3 taken 2868 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3958 times.
13993 if (s->oformat && s->oformat->priv_class && s->priv_data)
156 av_opt_free(s->priv_data);
157
158
2/2
✓ Branch 0 taken 15105 times.
✓ Branch 1 taken 13993 times.
29098 for (unsigned i = 0; i < s->nb_streams; i++)
159 15105 ff_free_stream(&s->streams[i]);
160
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 13993 times.
14059 for (unsigned i = 0; i < s->nb_stream_groups; i++)
161 66 ff_free_stream_group(&s->stream_groups[i]);
162 13993 s->nb_stream_groups = 0;
163 13993 s->nb_streams = 0;
164
165
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 13993 times.
14059 for (unsigned i = 0; i < s->nb_programs; i++) {
166 66 av_dict_free(&s->programs[i]->metadata);
167 66 av_freep(&s->programs[i]->stream_index);
168 66 av_freep(&s->programs[i]);
169 }
170 13993 s->nb_programs = 0;
171
172 13993 av_freep(&s->programs);
173 13993 av_freep(&s->priv_data);
174
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 13993 times.
14098 while (s->nb_chapters--) {
175 105 av_dict_free(&s->chapters[s->nb_chapters]->metadata);
176 105 av_freep(&s->chapters[s->nb_chapters]);
177 }
178 13993 av_freep(&s->chapters);
179 13993 av_dict_free(&s->metadata);
180 13993 av_dict_free(&si->id3v2_meta);
181 13993 av_packet_free(&si->pkt);
182 13993 av_packet_free(&si->parse_pkt);
183 13993 av_freep(&s->streams);
184 13993 av_freep(&s->stream_groups);
185 13993 ff_flush_packet_queue(s);
186 13993 av_freep(&s->url);
187 13993 av_free(s);
188 }
189
190 #if FF_API_AVSTREAM_SIDE_DATA
191 FF_DISABLE_DEPRECATION_WARNINGS
192 uint8_t *av_stream_get_side_data(const AVStream *st,
193 enum AVPacketSideDataType type, size_t *size)
194 {
195 for (int i = 0; i < st->nb_side_data; i++) {
196 if (st->side_data[i].type == type) {
197 if (size)
198 *size = st->side_data[i].size;
199 return st->side_data[i].data;
200 }
201 }
202 if (size)
203 *size = 0;
204 return NULL;
205 }
206
207 int av_stream_add_side_data(AVStream *st, enum AVPacketSideDataType type,
208 uint8_t *data, size_t size)
209 {
210 AVPacketSideData *sd, *tmp;
211
212 for (int i = 0; i < st->nb_side_data; i++) {
213 sd = &st->side_data[i];
214
215 if (sd->type == type) {
216 av_freep(&sd->data);
217 sd->data = data;
218 sd->size = size;
219 return 0;
220 }
221 }
222
223 if (st->nb_side_data + 1U > FFMIN(INT_MAX, SIZE_MAX / sizeof(*tmp)))
224 return AVERROR(ERANGE);
225
226 tmp = av_realloc_array(st->side_data, st->nb_side_data + 1, sizeof(*tmp));
227 if (!tmp) {
228 return AVERROR(ENOMEM);
229 }
230
231 st->side_data = tmp;
232 st->nb_side_data++;
233
234 sd = &st->side_data[st->nb_side_data - 1];
235 sd->type = type;
236 sd->data = data;
237 sd->size = size;
238
239 return 0;
240 }
241
242 uint8_t *av_stream_new_side_data(AVStream *st, enum AVPacketSideDataType type,
243 size_t size)
244 {
245 int ret;
246 uint8_t *data = av_malloc(size);
247
248 if (!data)
249 return NULL;
250
251 ret = av_stream_add_side_data(st, type, data, size);
252 if (ret < 0) {
253 av_freep(&data);
254 return NULL;
255 }
256
257 return data;
258 }
259 FF_ENABLE_DEPRECATION_WARNINGS
260 #endif
261
262 /**
263 * Copy all stream parameters from source to destination stream, with the
264 * exception of the index field, which is usually set by avformat_new_stream().
265 *
266 * @param dst pointer to destination AVStream
267 * @param src pointer to source AVStream
268 * @return >=0 on success, AVERROR code on error
269 */
270 20 static int stream_params_copy(AVStream *dst, const AVStream *src)
271 {
272 int ret;
273
274 20 dst->id = src->id;
275 20 dst->time_base = src->time_base;
276 20 dst->start_time = src->start_time;
277 20 dst->duration = src->duration;
278 20 dst->nb_frames = src->nb_frames;
279 20 dst->disposition = src->disposition;
280 20 dst->discard = src->discard;
281 20 dst->sample_aspect_ratio = src->sample_aspect_ratio;
282 20 dst->avg_frame_rate = src->avg_frame_rate;
283 20 dst->event_flags = src->event_flags;
284 20 dst->r_frame_rate = src->r_frame_rate;
285 20 dst->pts_wrap_bits = src->pts_wrap_bits;
286
287 20 av_dict_free(&dst->metadata);
288 20 ret = av_dict_copy(&dst->metadata, src->metadata, 0);
289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (ret < 0)
290 return ret;
291
292 20 ret = avcodec_parameters_copy(dst->codecpar, src->codecpar);
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (ret < 0)
294 return ret;
295
296 20 av_packet_unref(&dst->attached_pic);
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (src->attached_pic.data) {
298 ret = av_packet_ref(&dst->attached_pic, &src->attached_pic);
299 if (ret < 0)
300 return ret;
301 }
302
303 20 return 0;
304 }
305
306 20 AVStream *ff_stream_clone(AVFormatContext *dst_ctx, const AVStream *src)
307 {
308 AVStream *st;
309 int ret;
310
311 20 st = avformat_new_stream(dst_ctx, NULL);
312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!st)
313 return NULL;
314
315 20 ret = stream_params_copy(st, src);
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (ret < 0) {
317 ff_remove_stream(dst_ctx, st);
318 return NULL;
319 }
320
321 20 return st;
322 }
323
324 23 const char *avformat_stream_group_name(enum AVStreamGroupParamsType type)
325 {
326
3/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
23 switch(type) {
327 10 case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT: return "IAMF Audio Element";
328 10 case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION: return "IAMF Mix Presentation";
329 3 case AV_STREAM_GROUP_PARAMS_TILE_GRID: return "Tile Grid";
330 }
331 return NULL;
332 }
333
334 322 AVProgram *av_new_program(AVFormatContext *ac, int id)
335 {
336 322 AVProgram *program = NULL;
337 int ret;
338
339 322 av_log(ac, AV_LOG_TRACE, "new_program: id=0x%04x\n", id);
340
341
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 322 times.
578 for (unsigned i = 0; i < ac->nb_programs; i++)
342
1/2
✓ Branch 0 taken 256 times.
✗ Branch 1 not taken.
256 if (ac->programs[i]->id == id)
343 256 program = ac->programs[i];
344
345
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 256 times.
322 if (!program) {
346 66 program = av_mallocz(sizeof(*program));
347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (!program)
348 return NULL;
349 66 ret = av_dynarray_add_nofree(&ac->programs, &ac->nb_programs, program);
350
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (ret < 0) {
351 av_free(program);
352 return NULL;
353 }
354 66 program->discard = AVDISCARD_NONE;
355 66 program->pmt_version = -1;
356 66 program->id = id;
357 66 program->pts_wrap_reference = AV_NOPTS_VALUE;
358 66 program->pts_wrap_behavior = AV_PTS_WRAP_IGNORE;
359 66 program->start_time =
360 66 program->end_time = AV_NOPTS_VALUE;
361 }
362 322 return program;
363 }
364
365 531 void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx)
366 {
367 531 AVProgram *program = NULL;
368 void *tmp;
369
370
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 531 times.
531 if (idx >= ac->nb_streams) {
371 av_log(ac, AV_LOG_ERROR, "stream index %d is not valid\n", idx);
372 return;
373 }
374
375
1/2
✓ Branch 0 taken 531 times.
✗ Branch 1 not taken.
531 for (unsigned i = 0; i < ac->nb_programs; i++) {
376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 531 times.
531 if (ac->programs[i]->id != progid)
377 continue;
378 531 program = ac->programs[i];
379
2/2
✓ Branch 0 taken 764 times.
✓ Branch 1 taken 331 times.
1095 for (unsigned j = 0; j < program->nb_stream_indexes; j++)
380
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 564 times.
764 if (program->stream_index[j] == idx)
381 200 return;
382
383 331 tmp = av_realloc_array(program->stream_index, program->nb_stream_indexes+1, sizeof(unsigned int));
384
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 331 times.
331 if (!tmp)
385 return;
386 331 program->stream_index = tmp;
387 331 program->stream_index[program->nb_stream_indexes++] = idx;
388 331 return;
389 }
390 }
391
392 12536 AVProgram *av_find_program_from_stream(AVFormatContext *ic, AVProgram *last, int s)
393 {
394
2/2
✓ Branch 0 taken 1144 times.
✓ Branch 1 taken 11998 times.
13142 for (unsigned i = 0; i < ic->nb_programs; i++) {
395
2/2
✓ Branch 0 taken 603 times.
✓ Branch 1 taken 541 times.
1144 if (ic->programs[i] == last) {
396 603 last = NULL;
397 } else {
398
1/2
✓ Branch 0 taken 541 times.
✗ Branch 1 not taken.
541 if (!last)
399
2/2
✓ Branch 0 taken 1484 times.
✓ Branch 1 taken 3 times.
1487 for (unsigned j = 0; j < ic->programs[i]->nb_stream_indexes; j++)
400
2/2
✓ Branch 0 taken 538 times.
✓ Branch 1 taken 946 times.
1484 if (ic->programs[i]->stream_index[j] == s)
401 538 return ic->programs[i];
402 }
403 }
404 11998 return NULL;
405 }
406
407 1935 int av_find_default_stream_index(AVFormatContext *s)
408 {
409 1935 int best_stream = 0;
410 1935 int best_score = INT_MIN;
411
412
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1935 times.
1935 if (s->nb_streams <= 0)
413 return -1;
414
2/2
✓ Branch 0 taken 2281 times.
✓ Branch 1 taken 1935 times.
4216 for (unsigned i = 0; i < s->nb_streams; i++) {
415 2281 const AVStream *const st = s->streams[i];
416 2281 const FFStream *const sti = cffstream(st);
417 2281 int score = 0;
418
2/2
✓ Branch 0 taken 1329 times.
✓ Branch 1 taken 952 times.
2281 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
419
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1325 times.
1329 if (st->disposition & AV_DISPOSITION_ATTACHED_PIC)
420 4 score -= 400;
421
3/4
✓ Branch 0 taken 1263 times.
✓ Branch 1 taken 66 times.
✓ Branch 2 taken 1263 times.
✗ Branch 3 not taken.
1329 if (st->codecpar->width && st->codecpar->height)
422 1263 score += 50;
423 1329 score+= 25;
424 }
425
2/2
✓ Branch 0 taken 922 times.
✓ Branch 1 taken 1359 times.
2281 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
426
2/2
✓ Branch 0 taken 899 times.
✓ Branch 1 taken 23 times.
922 if (st->codecpar->sample_rate)
427 899 score += 50;
428 }
429
2/2
✓ Branch 0 taken 1980 times.
✓ Branch 1 taken 301 times.
2281 if (sti->codec_info_nb_frames)
430 1980 score += 12;
431
432
2/2
✓ Branch 0 taken 2277 times.
✓ Branch 1 taken 4 times.
2281 if (st->discard != AVDISCARD_ALL)
433 2277 score += 200;
434
435
2/2
✓ Branch 0 taken 1969 times.
✓ Branch 1 taken 312 times.
2281 if (score > best_score) {
436 1969 best_score = score;
437 1969 best_stream = i;
438 }
439 }
440 1935 return best_stream;
441 }
442
443 18 int av_find_best_stream(AVFormatContext *ic, enum AVMediaType type,
444 int wanted_stream_nb, int related_stream,
445 const AVCodec **decoder_ret, int flags)
446 {
447 18 int nb_streams = ic->nb_streams;
448 18 int ret = AVERROR_STREAM_NOT_FOUND;
449 18 int best_count = -1, best_multiframe = -1, best_disposition = -1;
450 int count, multiframe, disposition;
451 18 int64_t best_bitrate = -1;
452 int64_t bitrate;
453 18 unsigned *program = NULL;
454 18 const AVCodec *decoder = NULL, *best_decoder = NULL;
455
456
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
18 if (related_stream >= 0 && wanted_stream_nb < 0) {
457 AVProgram *p = av_find_program_from_stream(ic, NULL, related_stream);
458 if (p) {
459 program = p->stream_index;
460 nb_streams = p->nb_stream_indexes;
461 }
462 }
463
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 18 times.
39 for (unsigned i = 0; i < nb_streams; i++) {
464
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 int real_stream_index = program ? program[i] : i;
465 21 AVStream *st = ic->streams[real_stream_index];
466 21 AVCodecParameters *par = st->codecpar;
467
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 18 times.
21 if (par->codec_type != type)
468 3 continue;
469
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
18 if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb)
470 continue;
471
4/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
18 if (type == AVMEDIA_TYPE_AUDIO && !(par->ch_layout.nb_channels && par->sample_rate))
472 continue;
473
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (decoder_ret) {
474 decoder = ff_find_decoder(ic, st, par->codec_id);
475 if (!decoder) {
476 if (ret < 0)
477 ret = AVERROR_DECODER_NOT_FOUND;
478 continue;
479 }
480 }
481 18 disposition = !(st->disposition & (AV_DISPOSITION_HEARING_IMPAIRED | AV_DISPOSITION_VISUAL_IMPAIRED))
482 18 + !! (st->disposition & AV_DISPOSITION_DEFAULT);
483 18 count = ffstream(st)->codec_info_nb_frames;
484 18 bitrate = par->bit_rate;
485 18 multiframe = FFMIN(5, count);
486
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
18 if ((best_disposition > disposition) ||
487
1/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
18 (best_disposition == disposition && best_multiframe > multiframe) ||
488
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 18 times.
18 (best_disposition == disposition && best_multiframe == multiframe && best_bitrate > bitrate) ||
489 (best_disposition == disposition && best_multiframe == multiframe && best_bitrate == bitrate && best_count >= count))
490 continue;
491 18 best_disposition = disposition;
492 18 best_count = count;
493 18 best_bitrate = bitrate;
494 18 best_multiframe = multiframe;
495 18 ret = real_stream_index;
496 18 best_decoder = decoder;
497
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
18 if (program && i == nb_streams - 1 && ret < 0) {
498 program = NULL;
499 nb_streams = ic->nb_streams;
500 /* no related stream found, try again with everything */
501 i = 0;
502 }
503 }
504
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (decoder_ret)
505 *decoder_ret = best_decoder;
506 18 return ret;
507 }
508
509 /**
510 * Matches a stream specifier (but ignores requested index).
511 *
512 * @param indexptr set to point to the requested stream index if there is one
513 *
514 * @return <0 on error
515 * 0 if st is NOT a matching stream
516 * >0 if st is a matching stream
517 */
518 30651 static int match_stream_specifier(const AVFormatContext *s, const AVStream *st,
519 const char *spec, const char **indexptr,
520 const AVStreamGroup **g, const AVProgram **p)
521 {
522 30651 int match = 1; /* Stores if the specifier matches so far. */
523
2/2
✓ Branch 0 taken 20620 times.
✓ Branch 1 taken 29168 times.
49788 while (*spec) {
524
3/4
✓ Branch 0 taken 1483 times.
✓ Branch 1 taken 19137 times.
✓ Branch 2 taken 1483 times.
✗ Branch 3 not taken.
20620 if (*spec <= '9' && *spec >= '0') { /* opt:index */
525
2/2
✓ Branch 0 taken 661 times.
✓ Branch 1 taken 822 times.
1483 if (indexptr)
526 661 *indexptr = spec;
527 1483 return match;
528
7/8
✓ Branch 0 taken 2059 times.
✓ Branch 1 taken 17078 times.
✓ Branch 2 taken 145 times.
✓ Branch 3 taken 1914 times.
✓ Branch 4 taken 16 times.
✓ Branch 5 taken 129 times.
✓ Branch 6 taken 16 times.
✗ Branch 7 not taken.
19137 } else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
529
3/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
19140 *spec == 't' || *spec == 'V') { /* opt:[vasdtV] */
530 enum AVMediaType type;
531 19124 int nopic = 0;
532
533
4/7
✓ Branch 0 taken 17078 times.
✓ Branch 1 taken 1914 times.
✓ Branch 2 taken 129 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
19124 switch (*spec++) {
534 17078 case 'v': type = AVMEDIA_TYPE_VIDEO; break;
535 1914 case 'a': type = AVMEDIA_TYPE_AUDIO; break;
536 129 case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
537 case 'd': type = AVMEDIA_TYPE_DATA; break;
538 3 case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
539 case 'V': type = AVMEDIA_TYPE_VIDEO; nopic = 1; break;
540 default: av_assert0(0);
541 }
542
3/4
✓ Branch 0 taken 1157 times.
✓ Branch 1 taken 17967 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1157 times.
19124 if (*spec && *spec++ != ':') /* If we are not at the end, then another specifier must follow. */
543 return AVERROR(EINVAL);
544
545
2/2
✓ Branch 0 taken 732 times.
✓ Branch 1 taken 18392 times.
19124 if (type != st->codecpar->codec_type)
546 732 match = 0;
547
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 19124 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
19124 if (nopic && (st->disposition & AV_DISPOSITION_ATTACHED_PIC))
548 match = 0;
549
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
20 } else if (*spec == 'g' && *(spec + 1) == ':') {
550 7 int64_t group_idx = -1, group_id = -1;
551 7 int found = 0;
552 char *endptr;
553 7 spec += 2;
554
2/6
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
7 if (*spec == '#' || (*spec == 'i' && *(spec + 1) == ':')) {
555 spec += 1 + (*spec == 'i');
556 group_id = strtol(spec, &endptr, 0);
557 if (spec == endptr || (*endptr && *endptr++ != ':'))
558 return AVERROR(EINVAL);
559 spec = endptr;
560 } else {
561 7 group_idx = strtol(spec, &endptr, 0);
562 /* Disallow empty id and make sure that if we are not at the end, then another specifier must follow. */
563
2/6
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
7 if (spec == endptr || (*endptr && *endptr++ != ':'))
564 return AVERROR(EINVAL);
565 7 spec = endptr;
566 }
567
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (match) {
568
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (group_id > 0) {
569 for (unsigned i = 0; i < s->nb_stream_groups; i++) {
570 if (group_id == s->stream_groups[i]->id) {
571 group_idx = i;
572 break;
573 }
574 }
575 }
576
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
7 if (group_idx < 0 || group_idx >= s->nb_stream_groups)
577 return AVERROR(EINVAL);
578
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 for (unsigned j = 0; j < s->stream_groups[group_idx]->nb_streams; j++) {
579
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
14 if (st->index == s->stream_groups[group_idx]->streams[j]->index) {
580 7 found = 1;
581
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (g)
582 7 *g = s->stream_groups[group_idx];
583 7 break;
584 }
585 }
586 }
587
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!found)
588 match = 0;
589
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
12 } else if (*spec == 'p' && *(spec + 1) == ':') {
590 int prog_id;
591 6 int found = 0;
592 char *endptr;
593 6 spec += 2;
594 6 prog_id = strtol(spec, &endptr, 0);
595 /* Disallow empty id and make sure that if we are not at the end, then another specifier must follow. */
596
3/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
6 if (spec == endptr || (*endptr && *endptr++ != ':'))
597 return AVERROR(EINVAL);
598 6 spec = endptr;
599
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (match) {
600
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 for (unsigned i = 0; i < s->nb_programs; i++) {
601
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (s->programs[i]->id != prog_id)
602 continue;
603
604
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 for (unsigned j = 0; j < s->programs[i]->nb_stream_indexes; j++) {
605
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
8 if (st->index == s->programs[i]->stream_index[j]) {
606 5 found = 1;
607
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
5 if (p)
608 2 *p = s->programs[i];
609 5 i = s->nb_programs;
610 5 break;
611 }
612 }
613 }
614 }
615
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
6 if (!found)
616 1 match = 0;
617 } else if (*spec == '#' ||
618 (*spec == 'i' && *(spec + 1) == ':')) {
619 int stream_id;
620 char *endptr;
621 spec += 1 + (*spec == 'i');
622 stream_id = strtol(spec, &endptr, 0);
623 if (spec == endptr || *endptr) /* Disallow empty id and make sure we are at the end. */
624 return AVERROR(EINVAL);
625 return match && (stream_id == st->id);
626 } else if (*spec == 'm' && *(spec + 1) == ':') {
627 const AVDictionaryEntry *tag;
628 char *key, *val;
629 int ret;
630
631 if (match) {
632 spec += 2;
633 val = strchr(spec, ':');
634
635 key = val ? av_strndup(spec, val - spec) : av_strdup(spec);
636 if (!key)
637 return AVERROR(ENOMEM);
638
639 tag = av_dict_get(st->metadata, key, NULL, 0);
640 if (tag) {
641 if (!val || !strcmp(tag->value, val + 1))
642 ret = 1;
643 else
644 ret = 0;
645 } else
646 ret = 0;
647
648 av_freep(&key);
649 }
650 return match && ret;
651 } else if (*spec == 'u' && *(spec + 1) == '\0') {
652 const AVCodecParameters *par = st->codecpar;
653 int val;
654 switch (par->codec_type) {
655 case AVMEDIA_TYPE_AUDIO:
656 val = par->sample_rate && par->ch_layout.nb_channels;
657 if (par->format == AV_SAMPLE_FMT_NONE)
658 return 0;
659 break;
660 case AVMEDIA_TYPE_VIDEO:
661 val = par->width && par->height;
662 if (par->format == AV_PIX_FMT_NONE)
663 return 0;
664 break;
665 case AVMEDIA_TYPE_UNKNOWN:
666 val = 0;
667 break;
668 default:
669 val = 1;
670 break;
671 }
672 return match && (par->codec_id != AV_CODEC_ID_NONE && val != 0);
673 } else {
674 return AVERROR(EINVAL);
675 }
676 }
677
678 29168 return match;
679 }
680
681 29829 int avformat_match_stream_specifier(AVFormatContext *s, AVStream *st,
682 const char *spec)
683 {
684 int ret, index;
685 char *endptr;
686 29829 const char *indexptr = NULL;
687 29829 const AVStreamGroup *g = NULL;
688 29829 const AVProgram *p = NULL;
689 int nb_streams;
690
691 29829 ret = match_stream_specifier(s, st, spec, &indexptr, &g, &p);
692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29829 times.
29829 if (ret < 0)
693 goto error;
694
695
2/2
✓ Branch 0 taken 29168 times.
✓ Branch 1 taken 661 times.
29829 if (!indexptr)
696 29168 return ret;
697
698 661 index = strtol(indexptr, &endptr, 0);
699
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 661 times.
661 if (*endptr) { /* We can't have anything after the requested index. */
700 ret = AVERROR(EINVAL);
701 goto error;
702 }
703
704 /* This is not really needed but saves us a loop for simple stream index specifiers. */
705
2/2
✓ Branch 0 taken 326 times.
✓ Branch 1 taken 335 times.
661 if (spec == indexptr)
706 326 return (index == st->index);
707
708 /* If we requested a matching stream index, we have to ensure st is that. */
709
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 335 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 333 times.
335 nb_streams = g ? g->nb_streams : (p ? p->nb_stream_indexes : s->nb_streams);
710
4/4
✓ Branch 0 taken 947 times.
✓ Branch 1 taken 102 times.
✓ Branch 2 taken 822 times.
✓ Branch 3 taken 125 times.
1049 for (int i = 0; i < nb_streams && index >= 0; i++) {
711
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 822 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 820 times.
822 unsigned idx = g ? g->streams[i]->index : (p ? p->stream_index[i] : i);
712 822 const AVStream *candidate = s->streams[idx];
713 822 ret = match_stream_specifier(s, candidate, spec, NULL, NULL, NULL);
714
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 822 times.
822 if (ret < 0)
715 goto error;
716
6/6
✓ Branch 0 taken 507 times.
✓ Branch 1 taken 315 times.
✓ Branch 2 taken 255 times.
✓ Branch 3 taken 252 times.
✓ Branch 4 taken 108 times.
✓ Branch 5 taken 147 times.
822 if (ret > 0 && index-- == 0 && st == candidate)
717 108 return 1;
718 }
719 227 return 0;
720
721 error:
722 if (ret == AVERROR(EINVAL))
723 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
724 return ret;
725 }
726
727 666 AVRational av_guess_sample_aspect_ratio(AVFormatContext *format, AVStream *stream, AVFrame *frame)
728 {
729 666 AVRational undef = {0, 1};
730
1/2
✓ Branch 0 taken 666 times.
✗ Branch 1 not taken.
666 AVRational stream_sample_aspect_ratio = stream ? stream->sample_aspect_ratio : undef;
731
2/4
✓ Branch 0 taken 666 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 666 times.
✗ Branch 3 not taken.
666 AVRational codec_sample_aspect_ratio = stream && stream->codecpar ? stream->codecpar->sample_aspect_ratio : undef;
732
2/2
✓ Branch 0 taken 571 times.
✓ Branch 1 taken 95 times.
666 AVRational frame_sample_aspect_ratio = frame ? frame->sample_aspect_ratio : codec_sample_aspect_ratio;
733
734 666 av_reduce(&stream_sample_aspect_ratio.num, &stream_sample_aspect_ratio.den,
735 666 stream_sample_aspect_ratio.num, stream_sample_aspect_ratio.den, INT_MAX);
736
3/4
✓ Branch 0 taken 538 times.
✓ Branch 1 taken 128 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 538 times.
666 if (stream_sample_aspect_ratio.num <= 0 || stream_sample_aspect_ratio.den <= 0)
737 128 stream_sample_aspect_ratio = undef;
738
739 666 av_reduce(&frame_sample_aspect_ratio.num, &frame_sample_aspect_ratio.den,
740 666 frame_sample_aspect_ratio.num, frame_sample_aspect_ratio.den, INT_MAX);
741
3/4
✓ Branch 0 taken 451 times.
✓ Branch 1 taken 215 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 451 times.
666 if (frame_sample_aspect_ratio.num <= 0 || frame_sample_aspect_ratio.den <= 0)
742 215 frame_sample_aspect_ratio = undef;
743
744
2/2
✓ Branch 0 taken 538 times.
✓ Branch 1 taken 128 times.
666 if (stream_sample_aspect_ratio.num)
745 538 return stream_sample_aspect_ratio;
746 else
747 128 return frame_sample_aspect_ratio;
748 }
749
750 5129 AVRational av_guess_frame_rate(AVFormatContext *format, AVStream *st, AVFrame *frame)
751 {
752 5129 AVRational fr = st->r_frame_rate;
753 5129 const AVCodecDescriptor *desc = cffstream(st)->codec_desc;
754 5129 AVRational avg_fr = st->avg_frame_rate;
755
756
7/10
✓ Branch 0 taken 4515 times.
✓ Branch 1 taken 614 times.
✓ Branch 2 taken 4515 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4515 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4515 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4491 times.
✓ Branch 9 taken 24 times.
9644 if (avg_fr.num > 0 && avg_fr.den > 0 && fr.num > 0 && fr.den > 0 &&
757
2/2
✓ Branch 2 taken 369 times.
✓ Branch 3 taken 4122 times.
9006 av_q2d(avg_fr) < 70 && av_q2d(fr) > 210) {
758 369 fr = avg_fr;
759 }
760
761
3/4
✓ Branch 0 taken 5129 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 318 times.
✓ Branch 3 taken 4811 times.
5129 if (desc && (desc->props & AV_CODEC_PROP_FIELDS)) {
762 318 const AVCodecContext *const avctx = ffstream(st)->avctx;
763 318 AVRational codec_fr = avctx->framerate;
764
765
3/4
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 196 times.
✓ Branch 2 taken 122 times.
✗ Branch 3 not taken.
318 if ( codec_fr.num > 0 && codec_fr.den > 0 &&
766
5/6
✓ Branch 0 taken 122 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 34 times.
✓ Branch 5 taken 88 times.
✓ Branch 7 taken 32 times.
✓ Branch 8 taken 2 times.
122 (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))
767 32 fr = codec_fr;
768 }
769
770 5129 return fr;
771 }
772
773 639 int avformat_transfer_internal_stream_timing_info(const AVOutputFormat *ofmt,
774 AVStream *ost, const AVStream *ist,
775 enum AVTimebaseSource copy_tb)
776 {
777 639 const AVCodecDescriptor *desc = cffstream(ist)->codec_desc;
778 639 const AVCodecContext *const dec_ctx = cffstream(ist)->avctx;
779
780
4/4
✓ Branch 0 taken 630 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 72 times.
✓ Branch 3 taken 558 times.
639 AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 };
781
1/2
✓ Branch 0 taken 639 times.
✗ Branch 1 not taken.
639 AVRational dec_ctx_framerate = dec_ctx ? dec_ctx->framerate : (AVRational){ 0, 0 };
782 639 AVRational dec_ctx_tb = dec_ctx_framerate.num ? av_inv_q(av_mul_q(dec_ctx_framerate, mul))
783
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 554 times.
639 : (ist->codecpar->codec_type == AVMEDIA_TYPE_AUDIO ? (AVRational){0, 1}
784
2/2
✓ Branch 0 taken 265 times.
✓ Branch 1 taken 289 times.
554 : ist->time_base);
785 639 AVRational enc_tb = ist->time_base;
786 #if FF_API_TICKS_PER_FRAME
787 FF_DISABLE_DEPRECATION_WARNINGS
788
1/2
✓ Branch 0 taken 639 times.
✗ Branch 1 not taken.
639 int ticks_per_frame = dec_ctx ? dec_ctx->ticks_per_frame : 1;
789 FF_ENABLE_DEPRECATION_WARNINGS
790 #endif
791
792 /*
793 * Avi is a special case here because it supports variable fps but
794 * having the fps and timebase differe significantly adds quite some
795 * overhead
796 */
797
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 632 times.
639 if (!strcmp(ofmt->name, "avi")) {
798 #if FF_API_R_FRAME_RATE
799
3/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 2 times.
7 if (copy_tb == AVFMT_TBCF_AUTO && ist->r_frame_rate.num
800
1/2
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 && av_q2d(ist->r_frame_rate) >= av_q2d(ist->avg_frame_rate)
801
2/2
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 3 times.
5 && 0.5/av_q2d(ist->r_frame_rate) > av_q2d(ist->time_base)
802
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 && 0.5/av_q2d(ist->r_frame_rate) > av_q2d(dec_ctx_tb)
803 && av_q2d(ist->time_base) < 1.0/500 && av_q2d(dec_ctx_tb) < 1.0/500
804
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 || copy_tb == AVFMT_TBCF_R_FRAMERATE) {
805 enc_tb.num = ist->r_frame_rate.den;
806 enc_tb.den = 2*ist->r_frame_rate.num;
807 } else
808 #endif
809
3/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 3 times.
7 if (copy_tb == AVFMT_TBCF_AUTO && dec_ctx_framerate.num &&
810
2/2
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
4 av_q2d(av_inv_q(dec_ctx_framerate)) > 2*av_q2d(ist->time_base)
811
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 && av_q2d(ist->time_base) < 1.0/500
812
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 || (copy_tb == AVFMT_TBCF_DECODER &&
813 (dec_ctx_framerate.num || ist->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))) {
814 2 enc_tb = dec_ctx_tb;
815 2 enc_tb.den *= 2;
816 #if FF_API_TICKS_PER_FRAME
817 2 enc_tb.num *= ticks_per_frame;
818 #endif
819 }
820
2/2
✓ Branch 0 taken 270 times.
✓ Branch 1 taken 362 times.
632 } else if (!(ofmt->flags & AVFMT_VARIABLE_FPS)
821
2/2
✓ Branch 1 taken 241 times.
✓ Branch 2 taken 29 times.
270 && !av_match_name(ofmt->name, "mov,mp4,3gp,3g2,psp,ipod,ismv,f4v")) {
822
3/4
✓ Branch 0 taken 241 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 213 times.
241 if (copy_tb == AVFMT_TBCF_AUTO && dec_ctx_framerate.num
823
2/2
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 12 times.
28 && av_q2d(av_inv_q(dec_ctx_framerate)) > av_q2d(ist->time_base)
824
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 && av_q2d(ist->time_base) < 1.0/500
825
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 225 times.
225 || (copy_tb == AVFMT_TBCF_DECODER &&
826 (dec_ctx_framerate.num || ist->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))) {
827 16 enc_tb = dec_ctx_tb;
828 #if FF_API_TICKS_PER_FRAME
829 16 enc_tb.num *= ticks_per_frame;
830 #endif
831 }
832 }
833
834
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 639 times.
639 if (ost->codecpar->codec_tag == AV_RL32("tmcd")
835 && dec_ctx_tb.num < dec_ctx_tb.den
836 && dec_ctx_tb.num > 0
837 && 121LL*dec_ctx_tb.num > dec_ctx_tb.den) {
838 enc_tb = dec_ctx_tb;
839 }
840
841 639 av_reduce(&ffstream(ost)->transferred_mux_tb.num,
842 639 &ffstream(ost)->transferred_mux_tb.den,
843 639 enc_tb.num, enc_tb.den, INT_MAX);
844
845 639 return 0;
846 }
847
848 637 AVRational av_stream_get_codec_timebase(const AVStream *st)
849 {
850
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 637 times.
637 return cffstream(st)->avctx ? cffstream(st)->avctx->time_base : cffstream(st)->transferred_mux_tb;
851 }
852
853 19466 void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits,
854 unsigned int pts_num, unsigned int pts_den)
855 {
856 19466 FFStream *const sti = ffstream(st);
857 AVRational new_tb;
858
1/2
✓ Branch 1 taken 19466 times.
✗ Branch 2 not taken.
19466 if (av_reduce(&new_tb.num, &new_tb.den, pts_num, pts_den, INT_MAX)) {
859
2/2
✓ Branch 0 taken 537 times.
✓ Branch 1 taken 18929 times.
19466 if (new_tb.num != pts_num)
860 537 av_log(NULL, AV_LOG_DEBUG,
861 "st:%d removing common factor %d from timebase\n",
862 537 st->index, pts_num / new_tb.num);
863 } else
864 av_log(NULL, AV_LOG_WARNING,
865 "st:%d has too large timebase, reducing\n", st->index);
866
867
3/4
✓ Branch 0 taken 19465 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 19465 times.
19466 if (new_tb.num <= 0 || new_tb.den <= 0) {
868 1 av_log(NULL, AV_LOG_ERROR,
869 "Ignoring attempt to set invalid timebase %d/%d for st:%d\n",
870 new_tb.num, new_tb.den,
871 st->index);
872 1 return;
873 }
874 19465 st->time_base = new_tb;
875
2/2
✓ Branch 0 taken 15790 times.
✓ Branch 1 taken 3675 times.
19465 if (sti->avctx)
876 15790 sti->avctx->pkt_timebase = new_tb;
877 19465 st->pts_wrap_bits = pts_wrap_bits;
878 }
879
880 8827 const AVCodec *ff_find_decoder(AVFormatContext *s, const AVStream *st,
881 enum AVCodecID codec_id)
882 {
883
4/4
✓ Branch 0 taken 6126 times.
✓ Branch 1 taken 2465 times.
✓ Branch 2 taken 92 times.
✓ Branch 3 taken 144 times.
8827 switch (st->codecpar->codec_type) {
884 6126 case AVMEDIA_TYPE_VIDEO:
885
2/2
✓ Branch 0 taken 2709 times.
✓ Branch 1 taken 3417 times.
6126 if (s->video_codec) return s->video_codec;
886 3417 break;
887 2465 case AVMEDIA_TYPE_AUDIO:
888
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2447 times.
2465 if (s->audio_codec) return s->audio_codec;
889 2447 break;
890 92 case AVMEDIA_TYPE_SUBTITLE:
891
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92 times.
92 if (s->subtitle_codec) return s->subtitle_codec;
892 92 break;
893 }
894
895 6100 return avcodec_find_decoder(codec_id);
896 }
897
898 83 int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src)
899 {
900 #define OFF(field) offsetof(AVFormatContext, field)
901 static const unsigned offsets[] = {
902 OFF(codec_whitelist), OFF(format_whitelist),
903 OFF(protocol_whitelist), OFF(protocol_blacklist),
904 };
905 #undef OFF
906
4/8
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 83 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 83 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 83 times.
83 av_assert0(!dst->codec_whitelist &&
907 !dst->format_whitelist &&
908 !dst->protocol_whitelist &&
909 !dst->protocol_blacklist);
910
2/2
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 83 times.
415 for (unsigned i = 0; i < FF_ARRAY_ELEMS(offsets); i++) {
911 332 const char *src_str = *(char *const*)((const char*)src + offsets[i]);
912
913
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 249 times.
332 if (src_str) {
914 83 char *dst_str = av_strdup(src_str);
915
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 83 times.
83 if (!dst_str) {
916 av_log(dst, AV_LOG_ERROR, "Failed to duplicate black/whitelist\n");
917 return AVERROR(ENOMEM);
918 }
919
920 83 *(char **)((char*)dst + offsets[i]) = dst_str;
921 }
922 }
923 83 return 0;
924 }
925
926 557978 int ff_is_intra_only(enum AVCodecID id)
927 {
928 557978 const AVCodecDescriptor *d = avcodec_descriptor_get(id);
929
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 557913 times.
557978 if (!d)
930 65 return 0;
931
4/4
✓ Branch 0 taken 322868 times.
✓ Branch 1 taken 235045 times.
✓ Branch 2 taken 319936 times.
✓ Branch 3 taken 2932 times.
557913 if ((d->type == AVMEDIA_TYPE_VIDEO || d->type == AVMEDIA_TYPE_AUDIO) &&
932
2/2
✓ Branch 0 taken 130761 times.
✓ Branch 1 taken 424220 times.
554981 !(d->props & AV_CODEC_PROP_INTRA_ONLY))
933 130761 return 0;
934 427152 return 1;
935 }
936
937 68 void ff_format_set_url(AVFormatContext *s, char *url)
938 {
939
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 av_assert0(url);
940 68 av_freep(&s->url);
941 68 s->url = url;
942 68 }
943
944 90014 int ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
945 {
946 90014 int ret = 0;
947
2/2
✓ Branch 0 taken 89777 times.
✓ Branch 1 taken 237 times.
90014 if (*pb)
948 89777 ret = s->io_close2(s, *pb);
949 90014 *pb = NULL;
950 90014 return ret;
951 }
952