2 * Various functions used by both muxers and demuxers
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
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"
39 #include "avformat_internal.h"
45 void ff_free_stream(AVStream
**pst
)
48 FFStream
*const sti
= ffstream(st
);
53 if (st
->attached_pic
.data
)
54 av_packet_unref(&st
->attached_pic
);
56 av_parser_close(sti
->parser
);
57 avcodec_free_context(&sti
->avctx
);
58 av_bsf_free(&sti
->bsfc
);
59 av_freep(&sti
->index_entries
);
60 av_freep(&sti
->probe_data
.buf
);
62 av_bsf_free(&sti
->extract_extradata
.bsf
);
65 av_freep(&sti
->info
->duration_error
);
69 av_dict_free(&st
->metadata
);
70 avcodec_parameters_free(&st
->codecpar
);
71 av_freep(&st
->priv_data
);
76 void ff_free_stream_group(AVStreamGroup
**pstg
)
78 AVStreamGroup
*stg
= *pstg
;
83 av_freep(&stg
->streams
);
84 av_dict_free(&stg
->metadata
);
85 av_freep(&stg
->priv_data
);
87 case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT
: {
88 av_iamf_audio_element_free(&stg
->params
.iamf_audio_element
);
91 case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION
: {
92 av_iamf_mix_presentation_free(&stg
->params
.iamf_mix_presentation
);
95 case AV_STREAM_GROUP_PARAMS_TILE_GRID
:
96 av_opt_free(stg
->params
.tile_grid
);
97 av_freep(&stg
->params
.tile_grid
->offsets
);
98 av_packet_side_data_free(&stg
->params
.tile_grid
->coded_side_data
,
99 &stg
->params
.tile_grid
->nb_coded_side_data
);
100 av_freep(&stg
->params
.tile_grid
);
102 case AV_STREAM_GROUP_PARAMS_LCEVC
:
103 av_opt_free(stg
->params
.lcevc
);
104 av_freep(&stg
->params
.lcevc
);
113 void ff_remove_stream(AVFormatContext
*s
, AVStream
*st
)
115 av_assert0(s
->nb_streams
>0);
116 av_assert0(s
->streams
[ s
->nb_streams
- 1 ] == st
);
118 ff_free_stream(&s
->streams
[ --s
->nb_streams
]);
121 void ff_remove_stream_group(AVFormatContext
*s
, AVStreamGroup
*stg
)
123 av_assert0(s
->nb_stream_groups
> 0);
124 av_assert0(s
->stream_groups
[ s
->nb_stream_groups
- 1 ] == stg
);
126 ff_free_stream_group(&s
->stream_groups
[ --s
->nb_stream_groups
]);
129 /* XXX: suppress the packet queue */
130 void ff_flush_packet_queue(AVFormatContext
*s
)
132 FormatContextInternal
*const fci
= ff_fc_internal(s
);
133 FFFormatContext
*const si
= &fci
->fc
;
134 avpriv_packet_list_free(&fci
->parse_queue
);
135 avpriv_packet_list_free(&si
->packet_buffer
);
136 avpriv_packet_list_free(&fci
->raw_packet_buffer
);
138 fci
->raw_packet_buffer_size
= 0;
141 void avformat_free_context(AVFormatContext
*s
)
143 FormatContextInternal
*fci
;
148 fci
= ff_fc_internal(s
);
151 if (s
->oformat
&& ffofmt(s
->oformat
)->deinit
&& fci
->initialized
)
152 ffofmt(s
->oformat
)->deinit(s
);
155 if (s
->iformat
&& s
->iformat
->priv_class
&& s
->priv_data
)
156 av_opt_free(s
->priv_data
);
157 if (s
->oformat
&& s
->oformat
->priv_class
&& s
->priv_data
)
158 av_opt_free(s
->priv_data
);
160 for (unsigned i
= 0; i
< s
->nb_streams
; i
++)
161 ff_free_stream(&s
->streams
[i
]);
162 for (unsigned i
= 0; i
< s
->nb_stream_groups
; i
++)
163 ff_free_stream_group(&s
->stream_groups
[i
]);
164 s
->nb_stream_groups
= 0;
167 for (unsigned i
= 0; i
< s
->nb_programs
; i
++) {
168 av_dict_free(&s
->programs
[i
]->metadata
);
169 av_freep(&s
->programs
[i
]->stream_index
);
170 av_freep(&s
->programs
[i
]);
174 av_freep(&s
->programs
);
175 av_freep(&s
->priv_data
);
176 while (s
->nb_chapters
--) {
177 av_dict_free(&s
->chapters
[s
->nb_chapters
]->metadata
);
178 av_freep(&s
->chapters
[s
->nb_chapters
]);
180 av_freep(&s
->chapters
);
181 av_dict_free(&s
->metadata
);
182 av_dict_free(&si
->id3v2_meta
);
183 av_packet_free(&si
->pkt
);
184 av_packet_free(&si
->parse_pkt
);
185 avpriv_packet_list_free(&si
->packet_buffer
);
186 av_freep(&s
->streams
);
187 av_freep(&s
->stream_groups
);
189 ff_flush_packet_queue(s
);
195 * Copy all stream parameters from source to destination stream, with the
196 * exception of the index field, which is usually set by avformat_new_stream().
198 * @param dst pointer to destination AVStream
199 * @param src pointer to source AVStream
200 * @return >=0 on success, AVERROR code on error
202 static int stream_params_copy(AVStream
*dst
, const AVStream
*src
)
207 dst
->time_base
= src
->time_base
;
208 dst
->start_time
= src
->start_time
;
209 dst
->duration
= src
->duration
;
210 dst
->nb_frames
= src
->nb_frames
;
211 dst
->disposition
= src
->disposition
;
212 dst
->discard
= src
->discard
;
213 dst
->sample_aspect_ratio
= src
->sample_aspect_ratio
;
214 dst
->avg_frame_rate
= src
->avg_frame_rate
;
215 dst
->event_flags
= src
->event_flags
;
216 dst
->r_frame_rate
= src
->r_frame_rate
;
217 dst
->pts_wrap_bits
= src
->pts_wrap_bits
;
219 av_dict_free(&dst
->metadata
);
220 ret
= av_dict_copy(&dst
->metadata
, src
->metadata
, 0);
224 ret
= avcodec_parameters_copy(dst
->codecpar
, src
->codecpar
);
228 av_packet_unref(&dst
->attached_pic
);
229 if (src
->attached_pic
.data
) {
230 ret
= av_packet_ref(&dst
->attached_pic
, &src
->attached_pic
);
238 AVStream
*ff_stream_clone(AVFormatContext
*dst_ctx
, const AVStream
*src
)
243 st
= avformat_new_stream(dst_ctx
, NULL
);
247 ret
= stream_params_copy(st
, src
);
249 ff_remove_stream(dst_ctx
, st
);
256 const char *avformat_stream_group_name(enum AVStreamGroupParamsType type
)
259 case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT
: return "IAMF Audio Element";
260 case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION
: return "IAMF Mix Presentation";
261 case AV_STREAM_GROUP_PARAMS_TILE_GRID
: return "Tile Grid";
262 case AV_STREAM_GROUP_PARAMS_LCEVC
: return "LCEVC (Split video and enhancement)";
267 AVProgram
*av_new_program(AVFormatContext
*ac
, int id
)
269 AVProgram
*program
= NULL
;
272 av_log(ac
, AV_LOG_TRACE
, "new_program: id=0x%04x\n", id
);
274 for (unsigned i
= 0; i
< ac
->nb_programs
; i
++)
275 if (ac
->programs
[i
]->id
== id
)
276 program
= ac
->programs
[i
];
279 program
= av_mallocz(sizeof(*program
));
282 ret
= av_dynarray_add_nofree(&ac
->programs
, &ac
->nb_programs
, program
);
287 program
->discard
= AVDISCARD_NONE
;
288 program
->pmt_version
= -1;
290 program
->pts_wrap_reference
= AV_NOPTS_VALUE
;
291 program
->pts_wrap_behavior
= AV_PTS_WRAP_IGNORE
;
292 program
->start_time
=
293 program
->end_time
= AV_NOPTS_VALUE
;
298 void av_program_add_stream_index(AVFormatContext
*ac
, int progid
, unsigned idx
)
300 AVProgram
*program
= NULL
;
303 if (idx
>= ac
->nb_streams
) {
304 av_log(ac
, AV_LOG_ERROR
, "stream index %d is not valid\n", idx
);
308 for (unsigned i
= 0; i
< ac
->nb_programs
; i
++) {
309 if (ac
->programs
[i
]->id
!= progid
)
311 program
= ac
->programs
[i
];
312 for (unsigned j
= 0; j
< program
->nb_stream_indexes
; j
++)
313 if (program
->stream_index
[j
] == idx
)
316 tmp
= av_realloc_array(program
->stream_index
, program
->nb_stream_indexes
+1, sizeof(unsigned int));
319 program
->stream_index
= tmp
;
320 program
->stream_index
[program
->nb_stream_indexes
++] = idx
;
325 AVProgram
*av_find_program_from_stream(AVFormatContext
*ic
, AVProgram
*last
, int s
)
327 for (unsigned i
= 0; i
< ic
->nb_programs
; i
++) {
328 if (ic
->programs
[i
] == last
) {
332 for (unsigned j
= 0; j
< ic
->programs
[i
]->nb_stream_indexes
; j
++)
333 if (ic
->programs
[i
]->stream_index
[j
] == s
)
334 return ic
->programs
[i
];
340 int av_find_default_stream_index(AVFormatContext
*s
)
343 int best_score
= INT_MIN
;
345 if (s
->nb_streams
<= 0)
347 for (unsigned i
= 0; i
< s
->nb_streams
; i
++) {
348 const AVStream
*const st
= s
->streams
[i
];
349 const FFStream
*const sti
= cffstream(st
);
351 if (st
->codecpar
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
352 if (st
->disposition
& AV_DISPOSITION_ATTACHED_PIC
)
354 if (st
->codecpar
->width
&& st
->codecpar
->height
)
358 if (st
->codecpar
->codec_type
== AVMEDIA_TYPE_AUDIO
) {
359 if (st
->codecpar
->sample_rate
)
362 if (sti
->codec_info_nb_frames
)
365 if (st
->discard
!= AVDISCARD_ALL
)
368 if (score
> best_score
) {
376 int av_find_best_stream(AVFormatContext
*ic
, enum AVMediaType type
,
377 int wanted_stream_nb
, int related_stream
,
378 const AVCodec
**decoder_ret
, int flags
)
380 int nb_streams
= ic
->nb_streams
;
381 int ret
= AVERROR_STREAM_NOT_FOUND
;
382 int best_count
= -1, best_multiframe
= -1, best_disposition
= -1;
383 int count
, multiframe
, disposition
;
384 int64_t best_bitrate
= -1;
386 unsigned *program
= NULL
;
387 const AVCodec
*decoder
= NULL
, *best_decoder
= NULL
;
389 if (related_stream
>= 0 && wanted_stream_nb
< 0) {
390 AVProgram
*p
= av_find_program_from_stream(ic
, NULL
, related_stream
);
392 program
= p
->stream_index
;
393 nb_streams
= p
->nb_stream_indexes
;
396 for (unsigned i
= 0; i
< nb_streams
; i
++) {
397 int real_stream_index
= program
? program
[i
] : i
;
398 AVStream
*st
= ic
->streams
[real_stream_index
];
399 AVCodecParameters
*par
= st
->codecpar
;
400 if (par
->codec_type
!= type
)
402 if (wanted_stream_nb
>= 0 && real_stream_index
!= wanted_stream_nb
)
404 if (type
== AVMEDIA_TYPE_AUDIO
&& !(par
->ch_layout
.nb_channels
&& par
->sample_rate
))
407 decoder
= ff_find_decoder(ic
, st
, par
->codec_id
);
410 ret
= AVERROR_DECODER_NOT_FOUND
;
414 disposition
= !(st
->disposition
& (AV_DISPOSITION_HEARING_IMPAIRED
| AV_DISPOSITION_VISUAL_IMPAIRED
))
415 + !! (st
->disposition
& AV_DISPOSITION_DEFAULT
);
416 count
= ffstream(st
)->codec_info_nb_frames
;
417 bitrate
= par
->bit_rate
;
418 multiframe
= FFMIN(5, count
);
419 if ((best_disposition
> disposition
) ||
420 (best_disposition
== disposition
&& best_multiframe
> multiframe
) ||
421 (best_disposition
== disposition
&& best_multiframe
== multiframe
&& best_bitrate
> bitrate
) ||
422 (best_disposition
== disposition
&& best_multiframe
== multiframe
&& best_bitrate
== bitrate
&& best_count
>= count
))
424 best_disposition
= disposition
;
426 best_bitrate
= bitrate
;
427 best_multiframe
= multiframe
;
428 ret
= real_stream_index
;
429 best_decoder
= decoder
;
430 if (program
&& i
== nb_streams
- 1 && ret
< 0) {
432 nb_streams
= ic
->nb_streams
;
433 /* no related stream found, try again with everything */
438 *decoder_ret
= best_decoder
;
443 * Matches a stream specifier (but ignores requested index).
445 * @param indexptr set to point to the requested stream index if there is one
447 * @return <0 on error
448 * 0 if st is NOT a matching stream
449 * >0 if st is a matching stream
451 static int match_stream_specifier(const AVFormatContext
*s
, const AVStream
*st
,
452 const char *spec
, const char **indexptr
,
453 const AVStreamGroup
**g
, const AVProgram
**p
)
455 int match
= 1; /* Stores if the specifier matches so far. */
457 if (*spec
<= '9' && *spec
>= '0') { /* opt:index */
461 } else if (*spec
== 'v' || *spec
== 'a' || *spec
== 's' || *spec
== 'd' ||
462 *spec
== 't' || *spec
== 'V') { /* opt:[vasdtV] */
463 enum AVMediaType type
;
467 case 'v': type
= AVMEDIA_TYPE_VIDEO
; break;
468 case 'a': type
= AVMEDIA_TYPE_AUDIO
; break;
469 case 's': type
= AVMEDIA_TYPE_SUBTITLE
; break;
470 case 'd': type
= AVMEDIA_TYPE_DATA
; break;
471 case 't': type
= AVMEDIA_TYPE_ATTACHMENT
; break;
472 case 'V': type
= AVMEDIA_TYPE_VIDEO
; nopic
= 1; break;
473 default: av_assert0(0);
475 if (*spec
&& *spec
++ != ':') /* If we are not at the end, then another specifier must follow. */
476 return AVERROR(EINVAL
);
478 if (type
!= st
->codecpar
->codec_type
)
480 if (nopic
&& (st
->disposition
& AV_DISPOSITION_ATTACHED_PIC
))
482 } else if (*spec
== 'g' && *(spec
+ 1) == ':') {
483 int64_t group_idx
= -1, group_id
= -1;
487 if (*spec
== '#' || (*spec
== 'i' && *(spec
+ 1) == ':')) {
488 spec
+= 1 + (*spec
== 'i');
489 group_id
= strtol(spec
, &endptr
, 0);
490 if (spec
== endptr
|| (*endptr
&& *endptr
++ != ':'))
491 return AVERROR(EINVAL
);
494 group_idx
= strtol(spec
, &endptr
, 0);
495 /* Disallow empty id and make sure that if we are not at the end, then another specifier must follow. */
496 if (spec
== endptr
|| (*endptr
&& *endptr
++ != ':'))
497 return AVERROR(EINVAL
);
502 for (unsigned i
= 0; i
< s
->nb_stream_groups
; i
++) {
503 if (group_id
== s
->stream_groups
[i
]->id
) {
509 if (group_idx
< 0 || group_idx
>= s
->nb_stream_groups
)
510 return AVERROR(EINVAL
);
511 for (unsigned j
= 0; j
< s
->stream_groups
[group_idx
]->nb_streams
; j
++) {
512 if (st
->index
== s
->stream_groups
[group_idx
]->streams
[j
]->index
) {
515 *g
= s
->stream_groups
[group_idx
];
522 } else if (*spec
== 'p' && *(spec
+ 1) == ':') {
527 prog_id
= strtol(spec
, &endptr
, 0);
528 /* Disallow empty id and make sure that if we are not at the end, then another specifier must follow. */
529 if (spec
== endptr
|| (*endptr
&& *endptr
++ != ':'))
530 return AVERROR(EINVAL
);
533 for (unsigned i
= 0; i
< s
->nb_programs
; i
++) {
534 if (s
->programs
[i
]->id
!= prog_id
)
537 for (unsigned j
= 0; j
< s
->programs
[i
]->nb_stream_indexes
; j
++) {
538 if (st
->index
== s
->programs
[i
]->stream_index
[j
]) {
550 } else if (*spec
== '#' ||
551 (*spec
== 'i' && *(spec
+ 1) == ':')) {
554 spec
+= 1 + (*spec
== 'i');
555 stream_id
= strtol(spec
, &endptr
, 0);
556 if (spec
== endptr
|| *endptr
) /* Disallow empty id and make sure we are at the end. */
557 return AVERROR(EINVAL
);
558 return match
&& (stream_id
== st
->id
);
559 } else if (*spec
== 'm' && *(spec
+ 1) == ':') {
560 const AVDictionaryEntry
*tag
;
566 val
= strchr(spec
, ':');
568 key
= val
? av_strndup(spec
, val
- spec
) : av_strdup(spec
);
570 return AVERROR(ENOMEM
);
572 tag
= av_dict_get(st
->metadata
, key
, NULL
, 0);
574 if (!val
|| !strcmp(tag
->value
, val
+ 1))
584 } else if (*spec
== 'u' && *(spec
+ 1) == '\0') {
585 const AVCodecParameters
*par
= st
->codecpar
;
587 switch (par
->codec_type
) {
588 case AVMEDIA_TYPE_AUDIO
:
589 val
= par
->sample_rate
&& par
->ch_layout
.nb_channels
;
590 if (par
->format
== AV_SAMPLE_FMT_NONE
)
593 case AVMEDIA_TYPE_VIDEO
:
594 val
= par
->width
&& par
->height
;
595 if (par
->format
== AV_PIX_FMT_NONE
)
598 case AVMEDIA_TYPE_UNKNOWN
:
605 return match
&& (par
->codec_id
!= AV_CODEC_ID_NONE
&& val
!= 0);
607 return AVERROR(EINVAL
);
614 int avformat_match_stream_specifier(AVFormatContext
*s
, AVStream
*st
,
619 const char *indexptr
= NULL
;
620 const AVStreamGroup
*g
= NULL
;
621 const AVProgram
*p
= NULL
;
624 ret
= match_stream_specifier(s
, st
, spec
, &indexptr
, &g
, &p
);
631 index
= strtol(indexptr
, &endptr
, 0);
632 if (*endptr
) { /* We can't have anything after the requested index. */
633 ret
= AVERROR(EINVAL
);
637 /* This is not really needed but saves us a loop for simple stream index specifiers. */
638 if (spec
== indexptr
)
639 return (index
== st
->index
);
641 /* If we requested a matching stream index, we have to ensure st is that. */
642 nb_streams
= g
? g
->nb_streams
: (p
? p
->nb_stream_indexes
: s
->nb_streams
);
643 for (int i
= 0; i
< nb_streams
&& index
>= 0; i
++) {
644 unsigned idx
= g
? g
->streams
[i
]->index
: (p
? p
->stream_index
[i
] : i
);
645 const AVStream
*candidate
= s
->streams
[idx
];
646 ret
= match_stream_specifier(s
, candidate
, spec
, NULL
, NULL
, NULL
);
649 if (ret
> 0 && index
-- == 0 && st
== candidate
)
655 if (ret
== AVERROR(EINVAL
))
656 av_log(s
, AV_LOG_ERROR
, "Invalid stream specifier: %s.\n", spec
);
660 AVRational
av_guess_sample_aspect_ratio(AVFormatContext
*format
, AVStream
*stream
, AVFrame
*frame
)
662 AVRational undef
= {0, 1};
663 AVRational stream_sample_aspect_ratio
= stream
? stream
->sample_aspect_ratio
: undef
;
664 AVRational codec_sample_aspect_ratio
= stream
&& stream
->codecpar
? stream
->codecpar
->sample_aspect_ratio
: undef
;
665 AVRational frame_sample_aspect_ratio
= frame
? frame
->sample_aspect_ratio
: codec_sample_aspect_ratio
;
667 av_reduce(&stream_sample_aspect_ratio
.num
, &stream_sample_aspect_ratio
.den
,
668 stream_sample_aspect_ratio
.num
, stream_sample_aspect_ratio
.den
, INT_MAX
);
669 if (stream_sample_aspect_ratio
.num
<= 0 || stream_sample_aspect_ratio
.den
<= 0)
670 stream_sample_aspect_ratio
= undef
;
672 av_reduce(&frame_sample_aspect_ratio
.num
, &frame_sample_aspect_ratio
.den
,
673 frame_sample_aspect_ratio
.num
, frame_sample_aspect_ratio
.den
, INT_MAX
);
674 if (frame_sample_aspect_ratio
.num
<= 0 || frame_sample_aspect_ratio
.den
<= 0)
675 frame_sample_aspect_ratio
= undef
;
677 if (stream_sample_aspect_ratio
.num
)
678 return stream_sample_aspect_ratio
;
680 return frame_sample_aspect_ratio
;
683 AVRational
av_guess_frame_rate(AVFormatContext
*format
, AVStream
*st
, AVFrame
*frame
)
685 AVRational fr
= st
->r_frame_rate
;
686 const AVCodecDescriptor
*desc
= cffstream(st
)->codec_desc
;
687 AVRational avg_fr
= st
->avg_frame_rate
;
689 if (avg_fr
.num
> 0 && avg_fr
.den
> 0 && fr
.num
> 0 && fr
.den
> 0 &&
690 av_q2d(avg_fr
) < 70 && av_q2d(fr
) > 210) {
694 if (desc
&& (desc
->props
& AV_CODEC_PROP_FIELDS
)) {
695 const AVCodecContext
*const avctx
= ffstream(st
)->avctx
;
696 AVRational codec_fr
= avctx
->framerate
;
698 if ( codec_fr
.num
> 0 && codec_fr
.den
> 0 &&
699 (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))
706 #if FF_API_INTERNAL_TIMING
707 int avformat_transfer_internal_stream_timing_info(const AVOutputFormat
*ofmt
,
708 AVStream
*ost
, const AVStream
*ist
,
709 enum AVTimebaseSource copy_tb
)
711 const AVCodecDescriptor
*desc
= cffstream(ist
)->codec_desc
;
712 const AVCodecContext
*const dec_ctx
= cffstream(ist
)->avctx
;
714 AVRational mul
= (AVRational
){ desc
&& (desc
->props
& AV_CODEC_PROP_FIELDS
) ? 2 : 1, 1 };
715 AVRational dec_ctx_framerate
= dec_ctx
? dec_ctx
->framerate
: (AVRational
){ 0, 0 };
716 AVRational dec_ctx_tb
= dec_ctx_framerate
.num
? av_inv_q(av_mul_q(dec_ctx_framerate
, mul
))
717 : (ist
->codecpar
->codec_type
== AVMEDIA_TYPE_AUDIO
? (AVRational
){0, 1}
719 AVRational enc_tb
= ist
->time_base
;
722 * Avi is a special case here because it supports variable fps but
723 * having the fps and timebase differe significantly adds quite some
726 if (!strcmp(ofmt
->name
, "avi")) {
727 #if FF_API_R_FRAME_RATE
728 if (copy_tb
== AVFMT_TBCF_AUTO
&& ist
->r_frame_rate
.num
729 && av_q2d(ist
->r_frame_rate
) >= av_q2d(ist
->avg_frame_rate
)
730 && 0.5/av_q2d(ist
->r_frame_rate
) > av_q2d(ist
->time_base
)
731 && 0.5/av_q2d(ist
->r_frame_rate
) > av_q2d(dec_ctx_tb
)
732 && av_q2d(ist
->time_base
) < 1.0/500 && av_q2d(dec_ctx_tb
) < 1.0/500
733 || copy_tb
== AVFMT_TBCF_R_FRAMERATE
) {
734 enc_tb
.num
= ist
->r_frame_rate
.den
;
735 enc_tb
.den
= 2*ist
->r_frame_rate
.num
;
738 if (copy_tb
== AVFMT_TBCF_AUTO
&& dec_ctx_framerate
.num
&&
739 av_q2d(av_inv_q(dec_ctx_framerate
)) > 2*av_q2d(ist
->time_base
)
740 && av_q2d(ist
->time_base
) < 1.0/500
741 || (copy_tb
== AVFMT_TBCF_DECODER
&&
742 (dec_ctx_framerate
.num
|| ist
->codecpar
->codec_type
== AVMEDIA_TYPE_AUDIO
))) {
746 } else if (!(ofmt
->flags
& AVFMT_VARIABLE_FPS
)
747 && !av_match_name(ofmt
->name
, "mov,mp4,3gp,3g2,psp,ipod,ismv,f4v")) {
748 if (copy_tb
== AVFMT_TBCF_AUTO
&& dec_ctx_framerate
.num
749 && av_q2d(av_inv_q(dec_ctx_framerate
)) > av_q2d(ist
->time_base
)
750 && av_q2d(ist
->time_base
) < 1.0/500
751 || (copy_tb
== AVFMT_TBCF_DECODER
&&
752 (dec_ctx_framerate
.num
|| ist
->codecpar
->codec_type
== AVMEDIA_TYPE_AUDIO
))) {
757 if (ost
->codecpar
->codec_tag
== AV_RL32("tmcd")
758 && dec_ctx_tb
.num
< dec_ctx_tb
.den
759 && dec_ctx_tb
.num
> 0
760 && 121LL*dec_ctx_tb
.num
> dec_ctx_tb
.den
) {
764 av_reduce(&ffstream(ost
)->transferred_mux_tb
.num
,
765 &ffstream(ost
)->transferred_mux_tb
.den
,
766 enc_tb
.num
, enc_tb
.den
, INT_MAX
);
771 AVRational
av_stream_get_codec_timebase(const AVStream
*st
)
773 return cffstream(st
)->avctx
? cffstream(st
)->avctx
->time_base
: cffstream(st
)->transferred_mux_tb
;
777 void avpriv_set_pts_info(AVStream
*st
, int pts_wrap_bits
,
778 unsigned int pts_num
, unsigned int pts_den
)
780 FFStream
*const sti
= ffstream(st
);
782 if (av_reduce(&new_tb
.num
, &new_tb
.den
, pts_num
, pts_den
, INT_MAX
)) {
783 if (new_tb
.num
!= pts_num
)
784 av_log(NULL
, AV_LOG_DEBUG
,
785 "st:%d removing common factor %d from timebase\n",
786 st
->index
, pts_num
/ new_tb
.num
);
788 av_log(NULL
, AV_LOG_WARNING
,
789 "st:%d has too large timebase, reducing\n", st
->index
);
791 if (new_tb
.num
<= 0 || new_tb
.den
<= 0) {
792 av_log(NULL
, AV_LOG_ERROR
,
793 "Ignoring attempt to set invalid timebase %d/%d for st:%d\n",
794 new_tb
.num
, new_tb
.den
,
798 st
->time_base
= new_tb
;
800 sti
->avctx
->pkt_timebase
= new_tb
;
801 st
->pts_wrap_bits
= pts_wrap_bits
;
804 const AVCodec
*ff_find_decoder(AVFormatContext
*s
, const AVStream
*st
,
805 enum AVCodecID codec_id
)
807 switch (st
->codecpar
->codec_type
) {
808 case AVMEDIA_TYPE_VIDEO
:
809 if (s
->video_codec
) return s
->video_codec
;
811 case AVMEDIA_TYPE_AUDIO
:
812 if (s
->audio_codec
) return s
->audio_codec
;
814 case AVMEDIA_TYPE_SUBTITLE
:
815 if (s
->subtitle_codec
) return s
->subtitle_codec
;
819 return avcodec_find_decoder(codec_id
);
822 int ff_copy_whiteblacklists(AVFormatContext
*dst
, const AVFormatContext
*src
)
824 #define OFF(field) offsetof(AVFormatContext, field)
825 static const unsigned offsets
[] = {
826 OFF(codec_whitelist
), OFF(format_whitelist
),
827 OFF(protocol_whitelist
), OFF(protocol_blacklist
),
830 av_assert0(!dst
->codec_whitelist
&&
831 !dst
->format_whitelist
&&
832 !dst
->protocol_whitelist
&&
833 !dst
->protocol_blacklist
);
834 for (unsigned i
= 0; i
< FF_ARRAY_ELEMS(offsets
); i
++) {
835 const char *src_str
= *(char *const*)((const char*)src
+ offsets
[i
]);
838 char *dst_str
= av_strdup(src_str
);
840 av_log(dst
, AV_LOG_ERROR
, "Failed to duplicate black/whitelist\n");
841 return AVERROR(ENOMEM
);
844 *(char **)((char*)dst
+ offsets
[i
]) = dst_str
;
850 int ff_is_intra_only(enum AVCodecID id
)
852 const AVCodecDescriptor
*d
= avcodec_descriptor_get(id
);
855 if ((d
->type
== AVMEDIA_TYPE_VIDEO
|| d
->type
== AVMEDIA_TYPE_AUDIO
) &&
856 !(d
->props
& AV_CODEC_PROP_INTRA_ONLY
))
861 void ff_format_set_url(AVFormatContext
*s
, char *url
)
868 int ff_format_io_close(AVFormatContext
*s
, AVIOContext
**pb
)
872 ret
= s
->io_close2(s
, *pb
);