FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/avformat.c
Date: 2025-01-20 09:27:23
Exec Total Coverage
Lines: 368 595 61.8%
Functions: 23 29 79.3%
Branches: 241 559 43.1%

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