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/intreadwrite.h"
28 #include "libavutil/mem.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixfmt.h"
31 #include "libavutil/samplefmt.h"
32 #include "libavcodec/avcodec.h"
33 #include "libavcodec/codec.h"
34 #include "libavcodec/bsf.h"
35 #include "libavcodec/codec_desc.h"
36 #include "libavcodec/packet_internal.h"
43 void ff_free_stream(AVStream **pst)
46 FFStream *const sti = ffstream(st);
51 #if FF_API_AVSTREAM_SIDE_DATA
52 FF_DISABLE_DEPRECATION_WARNINGS
53 for (int i = 0; i < st->nb_side_data; i++)
54 av_freep(&st->side_data[i].data);
55 av_freep(&st->side_data);
56 FF_ENABLE_DEPRECATION_WARNINGS
59 if (st->attached_pic.data)
60 av_packet_unref(&st->attached_pic);
62 av_parser_close(sti->parser);
63 avcodec_free_context(&sti->avctx);
64 av_bsf_free(&sti->bsfc);
65 av_freep(&sti->priv_pts);
66 av_freep(&sti->index_entries);
67 av_freep(&sti->probe_data.buf);
69 av_bsf_free(&sti->extract_extradata.bsf);
72 av_freep(&sti->info->duration_error);
76 av_dict_free(&st->metadata);
77 avcodec_parameters_free(&st->codecpar);
78 av_freep(&st->priv_data);
83 void ff_remove_stream(AVFormatContext *s, AVStream *st)
85 av_assert0(s->nb_streams>0);
86 av_assert0(s->streams[ s->nb_streams - 1 ] == st);
88 ff_free_stream(&s->streams[ --s->nb_streams ]);
91 /* XXX: suppress the packet queue */
92 void ff_flush_packet_queue(AVFormatContext *s)
94 FFFormatContext *const si = ffformatcontext(s);
95 avpriv_packet_list_free(&si->parse_queue);
96 avpriv_packet_list_free(&si->packet_buffer);
97 avpriv_packet_list_free(&si->raw_packet_buffer);
99 si->raw_packet_buffer_size = 0;
102 void avformat_free_context(AVFormatContext *s)
108 si = ffformatcontext(s);
110 if (s->oformat && ffofmt(s->oformat)->deinit && si->initialized)
111 ffofmt(s->oformat)->deinit(s);
114 if (s->iformat && s->iformat->priv_class && s->priv_data)
115 av_opt_free(s->priv_data);
116 if (s->oformat && s->oformat->priv_class && s->priv_data)
117 av_opt_free(s->priv_data);
119 for (unsigned i = 0; i < s->nb_streams; i++)
120 ff_free_stream(&s->streams[i]);
123 for (unsigned i = 0; i < s->nb_programs; i++) {
124 av_dict_free(&s->programs[i]->metadata);
125 av_freep(&s->programs[i]->stream_index);
126 av_freep(&s->programs[i]);
130 av_freep(&s->programs);
131 av_freep(&s->priv_data);
132 while (s->nb_chapters--) {
133 av_dict_free(&s->chapters[s->nb_chapters]->metadata);
134 av_freep(&s->chapters[s->nb_chapters]);
136 av_freep(&s->chapters);
137 av_dict_free(&s->metadata);
138 av_dict_free(&si->id3v2_meta);
139 av_packet_free(&si->pkt);
140 av_packet_free(&si->parse_pkt);
141 av_freep(&s->streams);
142 ff_flush_packet_queue(s);
147 #if FF_API_AVSTREAM_SIDE_DATA
148 FF_DISABLE_DEPRECATION_WARNINGS
149 uint8_t *av_stream_get_side_data(const AVStream *st,
150 enum AVPacketSideDataType type, size_t *size)
152 for (int i = 0; i < st->nb_side_data; i++) {
153 if (st->side_data[i].type == type) {
155 *size = st->side_data[i].size;
156 return st->side_data[i].data;
164 int av_stream_add_side_data(AVStream *st, enum AVPacketSideDataType type,
165 uint8_t *data, size_t size)
167 AVPacketSideData *sd, *tmp;
169 for (int i = 0; i < st->nb_side_data; i++) {
170 sd = &st->side_data[i];
172 if (sd->type == type) {
180 if (st->nb_side_data + 1U > FFMIN(INT_MAX, SIZE_MAX / sizeof(*tmp)))
181 return AVERROR(ERANGE);
183 tmp = av_realloc_array(st->side_data, st->nb_side_data + 1, sizeof(*tmp));
185 return AVERROR(ENOMEM);
191 sd = &st->side_data[st->nb_side_data - 1];
199 uint8_t *av_stream_new_side_data(AVStream *st, enum AVPacketSideDataType type,
203 uint8_t *data = av_malloc(size);
208 ret = av_stream_add_side_data(st, type, data, size);
216 FF_ENABLE_DEPRECATION_WARNINGS
220 * Copy all stream parameters from source to destination stream, with the
221 * exception of the index field, which is usually set by avformat_new_stream().
223 * @param dst pointer to destination AVStream
224 * @param src pointer to source AVStream
225 * @return >=0 on success, AVERROR code on error
227 static int stream_params_copy(AVStream *dst, const AVStream *src)
232 dst->time_base = src->time_base;
233 dst->start_time = src->start_time;
234 dst->duration = src->duration;
235 dst->nb_frames = src->nb_frames;
236 dst->disposition = src->disposition;
237 dst->discard = src->discard;
238 dst->sample_aspect_ratio = src->sample_aspect_ratio;
239 dst->avg_frame_rate = src->avg_frame_rate;
240 dst->event_flags = src->event_flags;
241 dst->r_frame_rate = src->r_frame_rate;
242 dst->pts_wrap_bits = src->pts_wrap_bits;
244 av_dict_free(&dst->metadata);
245 ret = av_dict_copy(&dst->metadata, src->metadata, 0);
249 ret = avcodec_parameters_copy(dst->codecpar, src->codecpar);
253 av_packet_unref(&dst->attached_pic);
254 if (src->attached_pic.data) {
255 ret = av_packet_ref(&dst->attached_pic, &src->attached_pic);
263 AVStream *ff_stream_clone(AVFormatContext *dst_ctx, const AVStream *src)
268 st = avformat_new_stream(dst_ctx, NULL);
272 ret = stream_params_copy(st, src);
274 ff_remove_stream(dst_ctx, st);
281 AVProgram *av_new_program(AVFormatContext *ac, int id)
283 AVProgram *program = NULL;
286 av_log(ac, AV_LOG_TRACE, "new_program: id=0x%04x\n", id);
288 for (unsigned i = 0; i < ac->nb_programs; i++)
289 if (ac->programs[i]->id == id)
290 program = ac->programs[i];
293 program = av_mallocz(sizeof(*program));
296 ret = av_dynarray_add_nofree(&ac->programs, &ac->nb_programs, program);
301 program->discard = AVDISCARD_NONE;
302 program->pmt_version = -1;
304 program->pts_wrap_reference = AV_NOPTS_VALUE;
305 program->pts_wrap_behavior = AV_PTS_WRAP_IGNORE;
306 program->start_time =
307 program->end_time = AV_NOPTS_VALUE;
312 void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx)
314 AVProgram *program = NULL;
317 if (idx >= ac->nb_streams) {
318 av_log(ac, AV_LOG_ERROR, "stream index %d is not valid\n", idx);
322 for (unsigned i = 0; i < ac->nb_programs; i++) {
323 if (ac->programs[i]->id != progid)
325 program = ac->programs[i];
326 for (unsigned j = 0; j < program->nb_stream_indexes; j++)
327 if (program->stream_index[j] == idx)
330 tmp = av_realloc_array(program->stream_index, program->nb_stream_indexes+1, sizeof(unsigned int));
333 program->stream_index = tmp;
334 program->stream_index[program->nb_stream_indexes++] = idx;
339 AVProgram *av_find_program_from_stream(AVFormatContext *ic, AVProgram *last, int s)
341 for (unsigned i = 0; i < ic->nb_programs; i++) {
342 if (ic->programs[i] == last) {
346 for (unsigned j = 0; j < ic->programs[i]->nb_stream_indexes; j++)
347 if (ic->programs[i]->stream_index[j] == s)
348 return ic->programs[i];
354 int av_find_default_stream_index(AVFormatContext *s)
357 int best_score = INT_MIN;
359 if (s->nb_streams <= 0)
361 for (unsigned i = 0; i < s->nb_streams; i++) {
362 const AVStream *const st = s->streams[i];
363 const FFStream *const sti = cffstream(st);
365 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
366 if (st->disposition & AV_DISPOSITION_ATTACHED_PIC)
368 if (st->codecpar->width && st->codecpar->height)
372 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
373 if (st->codecpar->sample_rate)
376 if (sti->codec_info_nb_frames)
379 if (st->discard != AVDISCARD_ALL)
382 if (score > best_score) {
390 int av_find_best_stream(AVFormatContext *ic, enum AVMediaType type,
391 int wanted_stream_nb, int related_stream,
392 const AVCodec **decoder_ret, int flags)
394 int nb_streams = ic->nb_streams;
395 int ret = AVERROR_STREAM_NOT_FOUND;
396 int best_count = -1, best_multiframe = -1, best_disposition = -1;
397 int count, multiframe, disposition;
398 int64_t best_bitrate = -1;
400 unsigned *program = NULL;
401 const AVCodec *decoder = NULL, *best_decoder = NULL;
403 if (related_stream >= 0 && wanted_stream_nb < 0) {
404 AVProgram *p = av_find_program_from_stream(ic, NULL, related_stream);
406 program = p->stream_index;
407 nb_streams = p->nb_stream_indexes;
410 for (unsigned i = 0; i < nb_streams; i++) {
411 int real_stream_index = program ? program[i] : i;
412 AVStream *st = ic->streams[real_stream_index];
413 AVCodecParameters *par = st->codecpar;
414 if (par->codec_type != type)
416 if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb)
418 if (type == AVMEDIA_TYPE_AUDIO && !(par->ch_layout.nb_channels && par->sample_rate))
421 decoder = ff_find_decoder(ic, st, par->codec_id);
424 ret = AVERROR_DECODER_NOT_FOUND;
428 disposition = !(st->disposition & (AV_DISPOSITION_HEARING_IMPAIRED | AV_DISPOSITION_VISUAL_IMPAIRED))
429 + !! (st->disposition & AV_DISPOSITION_DEFAULT);
430 count = ffstream(st)->codec_info_nb_frames;
431 bitrate = par->bit_rate;
432 multiframe = FFMIN(5, count);
433 if ((best_disposition > disposition) ||
434 (best_disposition == disposition && best_multiframe > multiframe) ||
435 (best_disposition == disposition && best_multiframe == multiframe && best_bitrate > bitrate) ||
436 (best_disposition == disposition && best_multiframe == multiframe && best_bitrate == bitrate && best_count >= count))
438 best_disposition = disposition;
440 best_bitrate = bitrate;
441 best_multiframe = multiframe;
442 ret = real_stream_index;
443 best_decoder = decoder;
444 if (program && i == nb_streams - 1 && ret < 0) {
446 nb_streams = ic->nb_streams;
447 /* no related stream found, try again with everything */
452 *decoder_ret = best_decoder;
457 * Matches a stream specifier (but ignores requested index).
459 * @param indexptr set to point to the requested stream index if there is one
461 * @return <0 on error
462 * 0 if st is NOT a matching stream
463 * >0 if st is a matching stream
465 static int match_stream_specifier(const AVFormatContext *s, const AVStream *st,
466 const char *spec, const char **indexptr,
469 int match = 1; /* Stores if the specifier matches so far. */
471 if (*spec <= '9' && *spec >= '0') { /* opt:index */
475 } else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
476 *spec == 't' || *spec == 'V') { /* opt:[vasdtV] */
477 enum AVMediaType type;
481 case 'v': type = AVMEDIA_TYPE_VIDEO; break;
482 case 'a': type = AVMEDIA_TYPE_AUDIO; break;
483 case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
484 case 'd': type = AVMEDIA_TYPE_DATA; break;
485 case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
486 case 'V': type = AVMEDIA_TYPE_VIDEO; nopic = 1; break;
487 default: av_assert0(0);
489 if (*spec && *spec++ != ':') /* If we are not at the end, then another specifier must follow. */
490 return AVERROR(EINVAL);
492 if (type != st->codecpar->codec_type)
494 if (nopic && (st->disposition & AV_DISPOSITION_ATTACHED_PIC))
496 } else if (*spec == 'p' && *(spec + 1) == ':') {
501 prog_id = strtol(spec, &endptr, 0);
502 /* Disallow empty id and make sure that if we are not at the end, then another specifier must follow. */
503 if (spec == endptr || (*endptr && *endptr++ != ':'))
504 return AVERROR(EINVAL);
507 for (unsigned i = 0; i < s->nb_programs; i++) {
508 if (s->programs[i]->id != prog_id)
511 for (unsigned j = 0; j < s->programs[i]->nb_stream_indexes; j++) {
512 if (st->index == s->programs[i]->stream_index[j]) {
524 } else if (*spec == '#' ||
525 (*spec == 'i' && *(spec + 1) == ':')) {
528 spec += 1 + (*spec == 'i');
529 stream_id = strtol(spec, &endptr, 0);
530 if (spec == endptr || *endptr) /* Disallow empty id and make sure we are at the end. */
531 return AVERROR(EINVAL);
532 return match && (stream_id == st->id);
533 } else if (*spec == 'm' && *(spec + 1) == ':') {
534 const AVDictionaryEntry *tag;
540 val = strchr(spec, ':');
542 key = val ? av_strndup(spec, val - spec) : av_strdup(spec);
544 return AVERROR(ENOMEM);
546 tag = av_dict_get(st->metadata, key, NULL, 0);
548 if (!val || !strcmp(tag->value, val + 1))
558 } else if (*spec == 'u' && *(spec + 1) == '\0') {
559 const AVCodecParameters *par = st->codecpar;
561 switch (par->codec_type) {
562 case AVMEDIA_TYPE_AUDIO:
563 val = par->sample_rate && par->ch_layout.nb_channels;
564 if (par->format == AV_SAMPLE_FMT_NONE)
567 case AVMEDIA_TYPE_VIDEO:
568 val = par->width && par->height;
569 if (par->format == AV_PIX_FMT_NONE)
572 case AVMEDIA_TYPE_UNKNOWN:
579 return match && (par->codec_id != AV_CODEC_ID_NONE && val != 0);
581 return AVERROR(EINVAL);
588 int avformat_match_stream_specifier(AVFormatContext *s, AVStream *st,
593 const char *indexptr = NULL;
594 const AVProgram *p = NULL;
597 ret = match_stream_specifier(s, st, spec, &indexptr, &p);
604 index = strtol(indexptr, &endptr, 0);
605 if (*endptr) { /* We can't have anything after the requested index. */
606 ret = AVERROR(EINVAL);
610 /* This is not really needed but saves us a loop for simple stream index specifiers. */
611 if (spec == indexptr)
612 return (index == st->index);
614 /* If we requested a matching stream index, we have to ensure st is that. */
615 nb_streams = p ? p->nb_stream_indexes : s->nb_streams;
616 for (int i = 0; i < nb_streams && index >= 0; i++) {
617 const AVStream *candidate = s->streams[p ? p->stream_index[i] : i];
618 ret = match_stream_specifier(s, candidate, spec, NULL, NULL);
621 if (ret > 0 && index-- == 0 && st == candidate)
627 if (ret == AVERROR(EINVAL))
628 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
632 AVRational av_guess_sample_aspect_ratio(AVFormatContext *format, AVStream *stream, AVFrame *frame)
634 AVRational undef = {0, 1};
635 AVRational stream_sample_aspect_ratio = stream ? stream->sample_aspect_ratio : undef;
636 AVRational codec_sample_aspect_ratio = stream && stream->codecpar ? stream->codecpar->sample_aspect_ratio : undef;
637 AVRational frame_sample_aspect_ratio = frame ? frame->sample_aspect_ratio : codec_sample_aspect_ratio;
639 av_reduce(&stream_sample_aspect_ratio.num, &stream_sample_aspect_ratio.den,
640 stream_sample_aspect_ratio.num, stream_sample_aspect_ratio.den, INT_MAX);
641 if (stream_sample_aspect_ratio.num <= 0 || stream_sample_aspect_ratio.den <= 0)
642 stream_sample_aspect_ratio = undef;
644 av_reduce(&frame_sample_aspect_ratio.num, &frame_sample_aspect_ratio.den,
645 frame_sample_aspect_ratio.num, frame_sample_aspect_ratio.den, INT_MAX);
646 if (frame_sample_aspect_ratio.num <= 0 || frame_sample_aspect_ratio.den <= 0)
647 frame_sample_aspect_ratio = undef;
649 if (stream_sample_aspect_ratio.num)
650 return stream_sample_aspect_ratio;
652 return frame_sample_aspect_ratio;
655 AVRational av_guess_frame_rate(AVFormatContext *format, AVStream *st, AVFrame *frame)
657 AVRational fr = st->r_frame_rate;
658 const AVCodecDescriptor *desc = cffstream(st)->codec_desc;
659 AVCodecContext *const avctx = ffstream(st)->avctx;
660 AVRational codec_fr = avctx->framerate;
661 AVRational avg_fr = st->avg_frame_rate;
663 if (avg_fr.num > 0 && avg_fr.den > 0 && fr.num > 0 && fr.den > 0 &&
664 av_q2d(avg_fr) < 70 && av_q2d(fr) > 210) {
668 if (desc && (desc->props & AV_CODEC_PROP_FIELDS)) {
669 if ( codec_fr.num > 0 && codec_fr.den > 0 &&
670 (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))
677 int avformat_transfer_internal_stream_timing_info(const AVOutputFormat *ofmt,
678 AVStream *ost, const AVStream *ist,
679 enum AVTimebaseSource copy_tb)
681 const AVCodecDescriptor *desc = cffstream(ist)->codec_desc;
682 const AVCodecContext *const dec_ctx = cffstream(ist)->avctx;
683 AVCodecContext *const enc_ctx = ffstream(ost)->avctx;
685 AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 };
686 AVRational dec_ctx_tb = dec_ctx->framerate.num ? av_inv_q(av_mul_q(dec_ctx->framerate, mul))
687 : (ist->codecpar->codec_type == AVMEDIA_TYPE_AUDIO ? (AVRational){0, 1}
689 enc_ctx->time_base = ist->time_base;
691 * Avi is a special case here because it supports variable fps but
692 * having the fps and timebase differe significantly adds quite some
695 if (!strcmp(ofmt->name, "avi")) {
696 #if FF_API_R_FRAME_RATE
697 if (copy_tb == AVFMT_TBCF_AUTO && ist->r_frame_rate.num
698 && av_q2d(ist->r_frame_rate) >= av_q2d(ist->avg_frame_rate)
699 && 0.5/av_q2d(ist->r_frame_rate) > av_q2d(ist->time_base)
700 && 0.5/av_q2d(ist->r_frame_rate) > av_q2d(dec_ctx_tb)
701 && av_q2d(ist->time_base) < 1.0/500 && av_q2d(dec_ctx_tb) < 1.0/500
702 || copy_tb == AVFMT_TBCF_R_FRAMERATE) {
703 enc_ctx->time_base.num = ist->r_frame_rate.den;
704 enc_ctx->time_base.den = 2*ist->r_frame_rate.num;
705 #if FF_API_TICKS_PER_FRAME
706 FF_DISABLE_DEPRECATION_WARNINGS
707 enc_ctx->ticks_per_frame = 2;
708 FF_ENABLE_DEPRECATION_WARNINGS
712 if (copy_tb == AVFMT_TBCF_AUTO && dec_ctx->framerate.num &&
713 av_q2d(av_inv_q(dec_ctx->framerate)) > 2*av_q2d(ist->time_base)
714 && av_q2d(ist->time_base) < 1.0/500
715 || (copy_tb == AVFMT_TBCF_DECODER &&
716 (dec_ctx->framerate.num || ist->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))) {
717 enc_ctx->time_base = dec_ctx_tb;
718 enc_ctx->time_base.den *= 2;
719 #if FF_API_TICKS_PER_FRAME
720 FF_DISABLE_DEPRECATION_WARNINGS
721 enc_ctx->time_base.num *= dec_ctx->ticks_per_frame;
722 enc_ctx->ticks_per_frame = 2;
723 FF_ENABLE_DEPRECATION_WARNINGS
726 } else if (!(ofmt->flags & AVFMT_VARIABLE_FPS)
727 && !av_match_name(ofmt->name, "mov,mp4,3gp,3g2,psp,ipod,ismv,f4v")) {
728 if (copy_tb == AVFMT_TBCF_AUTO && dec_ctx->framerate.num
729 && av_q2d(av_inv_q(dec_ctx->framerate)) > av_q2d(ist->time_base)
730 && av_q2d(ist->time_base) < 1.0/500
731 || (copy_tb == AVFMT_TBCF_DECODER &&
732 (dec_ctx->framerate.num || ist->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))) {
733 enc_ctx->time_base = dec_ctx_tb;
734 #if FF_API_TICKS_PER_FRAME
735 FF_DISABLE_DEPRECATION_WARNINGS
736 enc_ctx->time_base.num *= dec_ctx->ticks_per_frame;
737 FF_ENABLE_DEPRECATION_WARNINGS
742 if ((enc_ctx->codec_tag == AV_RL32("tmcd") || ost->codecpar->codec_tag == AV_RL32("tmcd"))
743 && dec_ctx_tb.num < dec_ctx_tb.den
744 && dec_ctx_tb.num > 0
745 && 121LL*dec_ctx_tb.num > dec_ctx_tb.den) {
746 enc_ctx->time_base = dec_ctx_tb;
749 av_reduce(&enc_ctx->time_base.num, &enc_ctx->time_base.den,
750 enc_ctx->time_base.num, enc_ctx->time_base.den, INT_MAX);
755 AVRational av_stream_get_codec_timebase(const AVStream *st)
757 // See avformat_transfer_internal_stream_timing_info() TODO.
758 return cffstream(st)->avctx->time_base;
761 void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits,
762 unsigned int pts_num, unsigned int pts_den)
764 FFStream *const sti = ffstream(st);
766 if (av_reduce(&new_tb.num, &new_tb.den, pts_num, pts_den, INT_MAX)) {
767 if (new_tb.num != pts_num)
768 av_log(NULL, AV_LOG_DEBUG,
769 "st:%d removing common factor %d from timebase\n",
770 st->index, pts_num / new_tb.num);
772 av_log(NULL, AV_LOG_WARNING,
773 "st:%d has too large timebase, reducing\n", st->index);
775 if (new_tb.num <= 0 || new_tb.den <= 0) {
776 av_log(NULL, AV_LOG_ERROR,
777 "Ignoring attempt to set invalid timebase %d/%d for st:%d\n",
778 new_tb.num, new_tb.den,
782 st->time_base = new_tb;
783 sti->avctx->pkt_timebase = new_tb;
784 st->pts_wrap_bits = pts_wrap_bits;
787 const AVCodec *ff_find_decoder(AVFormatContext *s, const AVStream *st,
788 enum AVCodecID codec_id)
790 switch (st->codecpar->codec_type) {
791 case AVMEDIA_TYPE_VIDEO:
792 if (s->video_codec) return s->video_codec;
794 case AVMEDIA_TYPE_AUDIO:
795 if (s->audio_codec) return s->audio_codec;
797 case AVMEDIA_TYPE_SUBTITLE:
798 if (s->subtitle_codec) return s->subtitle_codec;
802 return avcodec_find_decoder(codec_id);
805 int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src)
807 av_assert0(!dst->codec_whitelist &&
808 !dst->format_whitelist &&
809 !dst->protocol_whitelist &&
810 !dst->protocol_blacklist);
811 dst-> codec_whitelist = av_strdup(src->codec_whitelist);
812 dst->format_whitelist = av_strdup(src->format_whitelist);
813 dst->protocol_whitelist = av_strdup(src->protocol_whitelist);
814 dst->protocol_blacklist = av_strdup(src->protocol_blacklist);
815 if ( (src-> codec_whitelist && !dst-> codec_whitelist)
816 || (src-> format_whitelist && !dst-> format_whitelist)
817 || (src->protocol_whitelist && !dst->protocol_whitelist)
818 || (src->protocol_blacklist && !dst->protocol_blacklist)) {
819 av_log(dst, AV_LOG_ERROR, "Failed to duplicate black/whitelist\n");
820 return AVERROR(ENOMEM);
825 int ff_is_intra_only(enum AVCodecID id)
827 const AVCodecDescriptor *d = avcodec_descriptor_get(id);
830 if ((d->type == AVMEDIA_TYPE_VIDEO || d->type == AVMEDIA_TYPE_AUDIO) &&
831 !(d->props & AV_CODEC_PROP_INTRA_ONLY))
836 void ff_format_set_url(AVFormatContext *s, char *url)
843 int ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
847 #if FF_API_AVFORMAT_IO_CLOSE
848 FF_DISABLE_DEPRECATION_WARNINGS
849 if (s->io_close == ff_format_io_close_default || s->io_close == NULL)
851 ret = s->io_close2(s, *pb);
852 #if FF_API_AVFORMAT_IO_CLOSE
855 FF_ENABLE_DEPRECATION_WARNINGS