FFmpeg coverage


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