Coverage Report

Created: 2026-05-31 06:50

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