diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c index 92639dda6b..f3bbbbd722 100644 --- a/libavcodec/avcodec.c +++ b/libavcodec/avcodec.c @@ -119,7 +119,7 @@ static int64_t get_bit_rate(AVCodecContext *ctx) case AVMEDIA_TYPE_AUDIO: bits_per_sample = av_get_bits_per_sample(ctx->codec_id); if (bits_per_sample) { - bit_rate = ctx->sample_rate * (int64_t)ctx->channels; + bit_rate = ctx->sample_rate * (int64_t)ctx->ch_layout.nb_channels; if (bit_rate > INT64_MAX / bits_per_sample) { bit_rate = 0; } else @@ -137,6 +137,8 @@ static int64_t get_bit_rate(AVCodecContext *ctx) int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options) { int ret = 0; + int orig_channels; + uint64_t orig_channel_layout; AVCodecInternal *avci; if (avcodec_is_open(avctx)) @@ -246,12 +248,6 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code } } - if (avctx->channels > FF_SANE_NB_CHANNELS || avctx->channels < 0) { - av_log(avctx, AV_LOG_ERROR, "Too many or invalid channels: %d\n", avctx->channels); - ret = AVERROR(EINVAL); - goto free_and_end; - } - if (avctx->sample_rate < 0) { av_log(avctx, AV_LOG_ERROR, "Invalid sample rate: %d\n", avctx->sample_rate); ret = AVERROR(EINVAL); @@ -263,6 +259,39 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code goto free_and_end; } +#if FF_API_OLD_CHANNEL_LAYOUT +FF_DISABLE_DEPRECATION_WARNINGS + /* compat wrapper for old-style callers */ + if (avctx->channel_layout && !avctx->channels) + avctx->channels = av_popcount64(avctx->channel_layout); + + if ((avctx->channels > 0 && avctx->ch_layout.nb_channels != avctx->channels) || + (avctx->channel_layout && (avctx->ch_layout.order != AV_CHANNEL_ORDER_NATIVE || + avctx->ch_layout.u.mask != avctx->channel_layout))) { + if (avctx->channel_layout) { + av_channel_layout_from_mask(&avctx->ch_layout, avctx->channel_layout); + } else { + avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; + avctx->ch_layout.nb_channels = avctx->channels; + } + } + + /* temporary compat wrapper for new-style callers and old-style codecs; + * to be removed once all the codecs have been converted */ + if (avctx->ch_layout.nb_channels) { + avctx->channels = avctx->ch_layout.nb_channels; + avctx->channel_layout = avctx->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ? + avctx->ch_layout.u.mask : 0; + } +FF_ENABLE_DEPRECATION_WARNINGS +#endif + + if (avctx->ch_layout.nb_channels > FF_SANE_NB_CHANNELS) { + av_log(avctx, AV_LOG_ERROR, "Too many channels: %d\n", avctx->ch_layout.nb_channels); + ret = AVERROR(EINVAL); + goto free_and_end; + } + avctx->frame_number = 0; avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id); @@ -317,6 +346,13 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code if (!HAVE_THREADS && !(codec->caps_internal & FF_CODEC_CAP_AUTO_THREADS)) avctx->thread_count = 1; +#if FF_API_OLD_CHANNEL_LAYOUT +FF_DISABLE_DEPRECATION_WARNINGS + orig_channels = avctx->channels; + orig_channel_layout = avctx->channel_layout; +FF_ENABLE_DEPRECATION_WARNINGS +#endif + if (!(avctx->active_thread_type & FF_THREAD_FRAME) || avci->frame_thread_encoder) { if (avctx->codec->init) { @@ -336,6 +372,26 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code if (av_codec_is_decoder(avctx->codec)) { if (!avctx->bit_rate) avctx->bit_rate = get_bit_rate(avctx); + +#if FF_API_OLD_CHANNEL_LAYOUT +FF_DISABLE_DEPRECATION_WARNINGS + /* decoder setting the old-style fields */ + if (avctx->channels != orig_channels || + avctx->channel_layout != orig_channel_layout) { + av_channel_layout_uninit(&avctx->ch_layout); + if (avctx->channel_layout) { + av_channel_layout_from_mask(&avctx->ch_layout, avctx->channel_layout); + } else { + avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; + avctx->ch_layout.nb_channels = avctx->channels; + } + } + + /* update the deprecated fields for old-style callers */ + avctx->channels = avctx->ch_layout.nb_channels; + avctx->channel_layout = avctx->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ? + avctx->ch_layout.u.mask : 0; + /* validate channel layout from the decoder */ if (avctx->channel_layout) { int channels = av_get_channel_layout_nb_channels(avctx->channel_layout); @@ -360,6 +416,8 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code ret = AVERROR(EINVAL); goto free_and_end; } +FF_ENABLE_DEPRECATION_WARNINGS +#endif #if FF_API_AVCTX_TIMEBASE if (avctx->framerate.num > 0 && avctx->framerate.den > 0) @@ -479,6 +537,8 @@ av_cold int avcodec_close(AVCodecContext *avctx) av_bsf_free(&avci->bsf); + av_channel_layout_uninit(&avci->initial_ch_layout); + av_freep(&avctx->internal); } @@ -661,7 +721,12 @@ void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode) if (enc->sample_rate) { av_bprintf(&bprint, "%d Hz, ", enc->sample_rate); } - av_bprint_channel_layout(&bprint, enc->channels, enc->channel_layout); + { + char buf[512]; + int ret = av_channel_layout_describe(&enc->ch_layout, buf, sizeof(buf)); + if (ret >= 0) + av_bprintf(&bprint, "%s", buf); + } if (enc->sample_fmt != AV_SAMPLE_FMT_NONE && (str = av_get_sample_fmt_name(enc->sample_fmt))) { av_bprintf(&bprint, ", %s", str); diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 79af8dcc05..187fdf03d5 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -990,7 +990,15 @@ typedef struct AVCodecContext { /* audio only */ int sample_rate; ///< samples per second - int channels; ///< number of audio channels + +#if FF_API_OLD_CHANNEL_LAYOUT + /** + * number of audio channels + * @deprecated use ch_layout.nb_channels + */ + attribute_deprecated + int channels; +#endif /** * audio sample format @@ -1035,19 +1043,25 @@ typedef struct AVCodecContext { */ int cutoff; +#if FF_API_OLD_CHANNEL_LAYOUT /** * Audio channel layout. * - encoding: set by user. * - decoding: set by user, may be overwritten by libavcodec. + * @deprecated use ch_layout */ + attribute_deprecated uint64_t channel_layout; /** * Request decoder to use this channel layout if it can (0 for default) * - encoding: unused * - decoding: Set by user. + * @deprecated use "downmix" codec private option */ + attribute_deprecated uint64_t request_channel_layout; +#endif /** * Type of service that the audio stream conveys. @@ -2027,6 +2041,14 @@ typedef struct AVCodecContext { * - decoding: unused */ int (*get_encode_buffer)(struct AVCodecContext *s, AVPacket *pkt, int flags); + + /** + * Audio channel layout. + * - encoding: must be set by the caller, to one of AVCodec.ch_layouts. + * - decoding: may be set by the caller if known e.g. from the container. + * The decoder can then override during decoding as needed. + */ + AVChannelLayout ch_layout; } AVCodecContext; /** diff --git a/libavcodec/codec.h b/libavcodec/codec.h index a8147ec21f..204a558798 100644 --- a/libavcodec/codec.h +++ b/libavcodec/codec.h @@ -224,7 +224,13 @@ typedef struct AVCodec { const enum AVPixelFormat *pix_fmts; ///< array of supported pixel formats, or NULL if unknown, array is terminated by -1 const int *supported_samplerates; ///< array of supported audio samplerates, or NULL if unknown, array is terminated by 0 const enum AVSampleFormat *sample_fmts; ///< array of supported sample formats, or NULL if unknown, array is terminated by -1 +#if FF_API_OLD_CHANNEL_LAYOUT + /** + * @deprecated use ch_layouts instead + */ + attribute_deprecated const uint64_t *channel_layouts; ///< array of support channel layouts, or NULL if unknown. array is terminated by 0 +#endif const AVClass *priv_class; ///< AVClass for the private context const AVProfile *profiles; ///< array of recognized profiles, or NULL if unknown, array is terminated by {FF_PROFILE_UNKNOWN} @@ -240,6 +246,11 @@ typedef struct AVCodec { */ const char *wrapper_name; + /** + * Array of supported channel layouts, terminated with a zeroed layout. + */ + const AVChannelLayout *ch_layouts; + /***************************************************************** * No fields below this line are part of the public API. They * may not be used outside of libavcodec and can be changed and diff --git a/libavcodec/codec_par.c b/libavcodec/codec_par.c index 5d36f6db45..e4b329c838 100644 --- a/libavcodec/codec_par.c +++ b/libavcodec/codec_par.c @@ -98,6 +98,8 @@ int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec) { + int ret; + codec_parameters_reset(par); par->codec_type = codec->codec_type; @@ -126,14 +128,27 @@ int avcodec_parameters_from_context(AVCodecParameters *par, break; case AVMEDIA_TYPE_AUDIO: par->format = codec->sample_fmt; - if (codec->channel_layout) - av_channel_layout_from_mask(&par->ch_layout, codec->channel_layout); - else { - par->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; - par->ch_layout.nb_channels = codec->channels; - } #if FF_API_OLD_CHANNEL_LAYOUT FF_DISABLE_DEPRECATION_WARNINGS + // if the old/new fields are set inconsistently, prefer the old ones + if ((codec->channels && codec->channels != codec->ch_layout.nb_channels) || + (codec->channel_layout && (codec->ch_layout.order != AV_CHANNEL_ORDER_NATIVE || + codec->ch_layout.u.mask != codec->channel_layout))) { + if (codec->channel_layout) + av_channel_layout_from_mask(&par->ch_layout, codec->channel_layout); + else { + par->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; + par->ch_layout.nb_channels = codec->channels; + } +FF_ENABLE_DEPRECATION_WARNINGS + } else { +#endif + ret = av_channel_layout_copy(&par->ch_layout, &codec->ch_layout); + if (ret < 0) + return ret; +#if FF_API_OLD_CHANNEL_LAYOUT +FF_DISABLE_DEPRECATION_WARNINGS + } par->channel_layout = par->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ? par->ch_layout.u.mask : 0; par->channels = par->ch_layout.nb_channels; @@ -166,6 +181,8 @@ FF_ENABLE_DEPRECATION_WARNINGS int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par) { + int ret; + codec->codec_type = par->codec_type; codec->codec_id = par->codec_id; codec->codec_tag = par->codec_tag; @@ -192,18 +209,31 @@ int avcodec_parameters_to_context(AVCodecContext *codec, break; case AVMEDIA_TYPE_AUDIO: codec->sample_fmt = par->format; - if (par->ch_layout.nb_channels) { - codec->channel_layout = par->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ? - par->ch_layout.u.mask : 0; - codec->channels = par->ch_layout.nb_channels; - } #if FF_API_OLD_CHANNEL_LAYOUT - else { FF_DISABLE_DEPRECATION_WARNINGS - codec->channel_layout = par->channel_layout; - codec->channels = par->channels; + // if the old/new fields are set inconsistently, prefer the old ones + if ((par->channels && par->channels != par->ch_layout.nb_channels) || + (par->channel_layout && (par->ch_layout.order != AV_CHANNEL_ORDER_NATIVE || + par->ch_layout.u.mask != par->channel_layout))) { + if (par->channel_layout) + av_channel_layout_from_mask(&codec->ch_layout, par->channel_layout); + else { + codec->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; + codec->ch_layout.nb_channels = par->channels; + } FF_ENABLE_DEPRECATION_WARNINGS + } else { +#endif + ret = av_channel_layout_copy(&codec->ch_layout, &par->ch_layout); + if (ret < 0) + return ret; +#if FF_API_OLD_CHANNEL_LAYOUT +FF_DISABLE_DEPRECATION_WARNINGS } + codec->channel_layout = codec->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ? + codec->ch_layout.u.mask : 0; + codec->channels = codec->ch_layout.nb_channels; +FF_ENABLE_DEPRECATION_WARNINGS #endif codec->sample_rate = par->sample_rate; codec->block_align = par->block_align; diff --git a/libavcodec/decode.c b/libavcodec/decode.c index 48a953da5c..a22fc2c574 100644 --- a/libavcodec/decode.c +++ b/libavcodec/decode.c @@ -352,10 +352,22 @@ static inline int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame, if (ret >= 0 && got_frame) { if (frame->format == AV_SAMPLE_FMT_NONE) frame->format = avctx->sample_fmt; + if (!frame->ch_layout.nb_channels) { + int ret2 = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout); + if (ret2 < 0) { + ret = ret2; + got_frame = 0; + } + } +#if FF_API_OLD_CHANNEL_LAYOUT +FF_DISABLE_DEPRECATION_WARNINGS if (!frame->channel_layout) - frame->channel_layout = avctx->channel_layout; + frame->channel_layout = avctx->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ? + avctx->ch_layout.u.mask : 0; if (!frame->channels) - frame->channels = avctx->channels; + frame->channels = avctx->ch_layout.nb_channels; +FF_ENABLE_DEPRECATION_WARNINGS +#endif if (!frame->sample_rate) frame->sample_rate = avctx->sample_rate; } @@ -388,7 +400,7 @@ static inline int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame, avci->skip_samples); } else { av_samples_copy(frame->extended_data, frame->extended_data, 0, avci->skip_samples, - frame->nb_samples - avci->skip_samples, avctx->channels, frame->format); + frame->nb_samples - avci->skip_samples, avctx->ch_layout.nb_channels, frame->format); if(avctx->pkt_timebase.num && avctx->sample_rate) { int64_t diff_ts = av_rescale_q(avci->skip_samples, (AVRational){1, avctx->sample_rate}, @@ -678,8 +690,17 @@ int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *fr case AVMEDIA_TYPE_AUDIO: avci->initial_sample_rate = frame->sample_rate ? frame->sample_rate : avctx->sample_rate; - avci->initial_channels = frame->channels; +#if FF_API_OLD_CHANNEL_LAYOUT +FF_DISABLE_DEPRECATION_WARNINGS + avci->initial_channels = frame->ch_layout.nb_channels; avci->initial_channel_layout = frame->channel_layout; +FF_ENABLE_DEPRECATION_WARNINGS +#endif + ret = av_channel_layout_copy(&avci->initial_ch_layout, &frame->ch_layout); + if (ret < 0) { + av_frame_unref(frame); + return ret; + } break; } } @@ -693,10 +714,15 @@ int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *fr avci->initial_height != frame->height; break; case AVMEDIA_TYPE_AUDIO: +FF_DISABLE_DEPRECATION_WARNINGS changed |= avci->initial_sample_rate != frame->sample_rate || avci->initial_sample_rate != avctx->sample_rate || +#if FF_API_OLD_CHANNEL_LAYOUT avci->initial_channels != frame->channels || - avci->initial_channel_layout != frame->channel_layout; + avci->initial_channel_layout != frame->channel_layout || +#endif + av_channel_layout_compare(&avci->initial_ch_layout, &frame->ch_layout); +FF_ENABLE_DEPRECATION_WARNINGS break; } @@ -1258,7 +1284,13 @@ static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame) if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) { int planar = av_sample_fmt_is_planar(frame->format); - ch = frame->channels; + ch = frame->ch_layout.nb_channels; +#if FF_API_OLD_CHANNEL_LAYOUT +FF_DISABLE_DEPRECATION_WARNINGS + if (!ch) + ch = frame->channels; +FF_ENABLE_DEPRECATION_WARNINGS +#endif planes = planar ? ch : 1; } @@ -1566,25 +1598,18 @@ int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame) frame->sample_rate = avctx->sample_rate; if (frame->format < 0) frame->format = avctx->sample_fmt; - if (!frame->channel_layout) { - if (avctx->channel_layout) { - if (av_get_channel_layout_nb_channels(avctx->channel_layout) != - avctx->channels) { - av_log(avctx, AV_LOG_ERROR, "Inconsistent channel " - "configuration.\n"); - return AVERROR(EINVAL); - } - - frame->channel_layout = avctx->channel_layout; - } else { - if (avctx->channels > FF_SANE_NB_CHANNELS) { - av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n", - avctx->channels); - return AVERROR(ENOSYS); - } - } + if (!frame->ch_layout.nb_channels) { + int ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout); + if (ret < 0) + return ret; } - frame->channels = avctx->channels; +#if FF_API_OLD_CHANNEL_LAYOUT +FF_DISABLE_DEPRECATION_WARNINGS + frame->channels = frame->ch_layout.nb_channels; + frame->channel_layout = frame->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ? + frame->ch_layout.u.mask : 0; +FF_ENABLE_DEPRECATION_WARNINGS +#endif break; } return 0; @@ -1674,7 +1699,36 @@ int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags) goto fail; } } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) { - if (frame->nb_samples * (int64_t)avctx->channels > avctx->max_samples) { +#if FF_API_OLD_CHANNEL_LAYOUT +FF_DISABLE_DEPRECATION_WARNINGS + /* temporary compat layer for decoders setting the old-style channel + * layout fields; shall be removed after all the decoders are converted + * to the new API */ + if ((avctx->channels > 0 && avctx->ch_layout.nb_channels != avctx->channels) || + (avctx->channel_layout && (avctx->ch_layout.order != AV_CHANNEL_ORDER_NATIVE || + avctx->ch_layout.u.mask != avctx->channel_layout))) { + av_channel_layout_uninit(&avctx->ch_layout); + if (avctx->channel_layout) { + if (av_popcount64(avctx->channel_layout) != avctx->channels) { + av_log(avctx, AV_LOG_ERROR, "Inconsistent channel layout/channels\n"); + ret = AVERROR(EINVAL); + goto fail; + } + av_channel_layout_from_mask(&avctx->ch_layout, avctx->channel_layout); + } else { + avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; + avctx->ch_layout.nb_channels = avctx->channels; + } + } + + /* compat layer for old-style get_buffer() implementations */ + avctx->channels = avctx->ch_layout.nb_channels; + avctx->channel_layout = (avctx->ch_layout.order == AV_CHANNEL_ORDER_NATIVE) ? + avctx->ch_layout.u.mask : 0; +FF_ENABLE_DEPRECATION_WARNINGS +#endif + + if (frame->nb_samples * (int64_t)avctx->ch_layout.nb_channels > avctx->max_samples) { av_log(avctx, AV_LOG_ERROR, "samples per frame %d, exceeds max_samples %"PRId64"\n", frame->nb_samples, avctx->max_samples); ret = AVERROR(EINVAL); goto fail; @@ -1784,7 +1838,7 @@ FF_DISABLE_DEPRECATION_WARNINGS FF_ENABLE_DEPRECATION_WARNINGS #endif - if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && avctx->channels == 0 && + if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && avctx->ch_layout.nb_channels == 0 && !(avctx->codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)) { av_log(avctx, AV_LOG_ERROR, "Decoder requires channel count but channels not set\n"); return AVERROR(EINVAL); diff --git a/libavcodec/encode.c b/libavcodec/encode.c index 618be0573d..f9f8879b8e 100644 --- a/libavcodec/encode.c +++ b/libavcodec/encode.c @@ -114,9 +114,10 @@ static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src) int ret; frame->format = src->format; - frame->channel_layout = src->channel_layout; - frame->channels = src->channels; frame->nb_samples = s->frame_size; + ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout); + if (ret < 0) + goto fail; ret = av_frame_get_buffer(frame, 0); if (ret < 0) goto fail; @@ -126,11 +127,12 @@ static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src) goto fail; if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0, - src->nb_samples, s->channels, s->sample_fmt)) < 0) + src->nb_samples, s->ch_layout.nb_channels, + s->sample_fmt)) < 0) goto fail; if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples, frame->nb_samples - src->nb_samples, - s->channels, s->sample_fmt)) < 0) + s->ch_layout.nb_channels, s->sample_fmt)) < 0) goto fail; return 0; @@ -419,7 +421,7 @@ int ff_encode_preinit(AVCodecContext *avctx) for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) { if (avctx->sample_fmt == avctx->codec->sample_fmts[i]) break; - if (avctx->channels == 1 && + if (avctx->ch_layout.nb_channels == 1 && av_get_planar_sample_fmt(avctx->sample_fmt) == av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) { avctx->sample_fmt = avctx->codec->sample_fmts[i]; @@ -467,7 +469,27 @@ int ff_encode_preinit(AVCodecContext *avctx) avctx->sample_rate); return AVERROR(EINVAL); } - if (avctx->codec->channel_layouts) { + if (avctx->codec->ch_layouts) { + if (!av_channel_layout_check(&avctx->ch_layout)) { + av_log(avctx, AV_LOG_WARNING, "Channel layout not specified correctly\n"); + return AVERROR(EINVAL); + } + + for (i = 0; avctx->codec->ch_layouts[i].nb_channels; i++) { + if (!av_channel_layout_compare(&avctx->ch_layout, &avctx->codec->ch_layouts[i])) + break; + } + if (!avctx->codec->ch_layouts[i].nb_channels) { + char buf[512]; + int ret = av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf)); + if (ret > 0) + av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf); + return AVERROR(EINVAL); + } +FF_DISABLE_DEPRECATION_WARNINGS + } +#if FF_API_OLD_CHANNEL_LAYOUT + else if (avctx->codec->channel_layouts) { if (!avctx->channel_layout) { av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n"); } else { @@ -500,6 +522,8 @@ int ff_encode_preinit(AVCodecContext *avctx) avctx->channels); return AVERROR(EINVAL); } +FF_ENABLE_DEPRECATION_WARNINGS +#endif if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) { const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt); if ( avctx->bits_per_raw_sample < 0 diff --git a/libavcodec/internal.h b/libavcodec/internal.h index cf82f15c19..3766fe05bc 100644 --- a/libavcodec/internal.h +++ b/libavcodec/internal.h @@ -208,8 +208,11 @@ typedef struct AVCodecInternal { int initial_format; int initial_width, initial_height; int initial_sample_rate; +#if FF_API_OLD_CHANNEL_LAYOUT int initial_channels; uint64_t initial_channel_layout; +#endif + AVChannelLayout initial_ch_layout; } AVCodecInternal; struct AVCodecDefault { diff --git a/libavcodec/options.c b/libavcodec/options.c index bba6078b62..a01214a511 100644 --- a/libavcodec/options.c +++ b/libavcodec/options.c @@ -103,6 +103,8 @@ static int init_context_defaults(AVCodecContext *s, const AVCodec *codec) flags= AV_OPT_FLAG_SUBTITLE_PARAM; av_opt_set_defaults2(s, flags, flags); + av_channel_layout_uninit(&s->ch_layout); + s->time_base = (AVRational){0,1}; s->framerate = (AVRational){ 0, 1 }; s->pkt_timebase = (AVRational){ 0, 1 }; @@ -112,6 +114,7 @@ static int init_context_defaults(AVCodecContext *s, const AVCodec *codec) s->execute = avcodec_default_execute; s->execute2 = avcodec_default_execute2; s->sample_aspect_ratio = (AVRational){0,1}; + s->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; s->pix_fmt = AV_PIX_FMT_NONE; s->sw_pix_fmt = AV_PIX_FMT_NONE; s->sample_fmt = AV_SAMPLE_FMT_NONE; @@ -167,6 +170,7 @@ void avcodec_free_context(AVCodecContext **pavctx) av_freep(&avctx->intra_matrix); av_freep(&avctx->inter_matrix); av_freep(&avctx->rc_override); + av_channel_layout_uninit(&avctx->ch_layout); av_freep(pavctx); } @@ -177,6 +181,7 @@ const AVClass *avcodec_get_class(void) } #if FF_API_GET_FRAME_CLASS +FF_DISABLE_DEPRECATION_WARNINGS #define FOFFSET(x) offsetof(AVFrame,x) static const AVOption frame_options[]={ @@ -187,7 +192,9 @@ static const AVOption frame_options[]={ {"width", "", FOFFSET(width), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0}, {"height", "", FOFFSET(height), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0}, {"format", "", FOFFSET(format), AV_OPT_TYPE_INT, {.i64 = -1 }, 0, INT_MAX, 0}, +#if FF_API_OLD_CHANNEL_LAYOUT {"channel_layout", "", FOFFSET(channel_layout), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, 0}, +#endif {"sample_rate", "", FOFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, 0}, {NULL}, }; @@ -203,6 +210,7 @@ const AVClass *avcodec_get_frame_class(void) { return &av_frame_class; } +FF_ENABLE_DEPRECATION_WARNINGS #endif #define SROFFSET(x) offsetof(AVSubtitleRect,x) diff --git a/libavcodec/options_table.h b/libavcodec/options_table.h index 130341a2ec..4dca85747e 100644 --- a/libavcodec/options_table.h +++ b/libavcodec/options_table.h @@ -89,7 +89,9 @@ static const AVOption avcodec_options[] = { {"time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, INT_MAX}, {"g", "set the group of picture (GOP) size", OFFSET(gop_size), AV_OPT_TYPE_INT, {.i64 = 12 }, INT_MIN, INT_MAX, V|E}, {"ar", "set audio sampling rate (in Hz)", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, 0, INT_MAX, A|D|E}, +#if FF_API_OLD_CHANNEL_LAYOUT {"ac", "set number of audio channels", OFFSET(channels), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, 0, INT_MAX, A|D|E}, +#endif {"cutoff", "set cutoff bandwidth", OFFSET(cutoff), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX, A|E}, {"frame_size", NULL, OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, 0, INT_MAX, A|E}, {"frame_number", NULL, OFFSET(frame_number), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX}, @@ -262,8 +264,11 @@ static const AVOption avcodec_options[] = { {"mv0_threshold", NULL, OFFSET(mv0_threshold), AV_OPT_TYPE_INT, {.i64 = 256 }, 0, INT_MAX, V|E}, {"compression_level", NULL, OFFSET(compression_level), AV_OPT_TYPE_INT, {.i64 = FF_COMPRESSION_DEFAULT }, INT_MIN, INT_MAX, V|A|E}, {"bits_per_raw_sample", NULL, OFFSET(bits_per_raw_sample), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, 0, INT_MAX}, +{"ch_layout", NULL, OFFSET(ch_layout), AV_OPT_TYPE_CHLAYOUT, {.str = NULL }, 0, 0, A|E|D, "ch_layout"}, +#if FF_API_OLD_CHANNEL_LAYOUT {"channel_layout", NULL, OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64 = DEFAULT }, 0, UINT64_MAX, A|E|D, "channel_layout"}, {"request_channel_layout", NULL, OFFSET(request_channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, {.i64 = DEFAULT }, 0, UINT64_MAX, A|D, "request_channel_layout"}, +#endif {"rc_max_vbv_use", NULL, OFFSET(rc_max_available_vbv_use), AV_OPT_TYPE_FLOAT, {.dbl = 0 }, 0.0, FLT_MAX, V|E}, {"rc_min_vbv_use", NULL, OFFSET(rc_min_vbv_overflow_use), AV_OPT_TYPE_FLOAT, {.dbl = 3 }, 0.0, FLT_MAX, V|E}, {"ticks_per_frame", NULL, OFFSET(ticks_per_frame), AV_OPT_TYPE_INT, {.i64 = 1 }, 1, INT_MAX, A|V|E|D}, diff --git a/libavcodec/pthread_frame.c b/libavcodec/pthread_frame.c index 33b5a2e628..ddf2460534 100644 --- a/libavcodec/pthread_frame.c +++ b/libavcodec/pthread_frame.c @@ -294,10 +294,18 @@ static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, dst->hwaccel = src->hwaccel; dst->hwaccel_context = src->hwaccel_context; - dst->channels = src->channels; dst->sample_rate = src->sample_rate; dst->sample_fmt = src->sample_fmt; +#if FF_API_OLD_CHANNEL_LAYOUT +FF_DISABLE_DEPRECATION_WARNINGS + dst->channels = src->channels; dst->channel_layout = src->channel_layout; +FF_ENABLE_DEPRECATION_WARNINGS +#endif + err = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout); + if (err < 0) + return err; + dst->internal->hwaccel_priv_data = src->internal->hwaccel_priv_data; if (!!dst->hw_frames_ctx != !!src->hw_frames_ctx || diff --git a/libavcodec/utils.c b/libavcodec/utils.c index 643c90f1f1..fe2db93782 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -804,8 +804,16 @@ static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba, int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes) { - int duration = get_audio_frame_duration(avctx->codec_id, avctx->sample_rate, - avctx->channels, avctx->block_align, + int channels = avctx->ch_layout.nb_channels; + int duration; +#if FF_API_OLD_CHANNEL_LAYOUT +FF_DISABLE_DEPRECATION_WARNINGS + if (!channels) + channels = avctx->channels; +FF_ENABLE_DEPRECATION_WARNINGS +#endif + duration = get_audio_frame_duration(avctx->codec_id, avctx->sample_rate, + channels, avctx->block_align, avctx->codec_tag, avctx->bits_per_coded_sample, avctx->bit_rate, avctx->extradata, avctx->frame_size, frame_bytes); diff --git a/libavformat/demux.c b/libavformat/demux.c index f7ebba2474..938af13831 100644 --- a/libavformat/demux.c +++ b/libavformat/demux.c @@ -1329,16 +1329,38 @@ static int read_frame_internal(AVFormatContext *s, AVPacket *pkt) } got_packet = 1; } else if (st->discard < AVDISCARD_ALL) { +#if FF_API_OLD_CHANNEL_LAYOUT +FF_DISABLE_DEPRECATION_WARNINGS + int orig_channels = sti->avctx->channels; + uint64_t orig_channel_layout = sti->avctx->channel_layout; +FF_ENABLE_DEPRECATION_WARNINGS +#endif if ((ret = parse_packet(s, pkt, pkt->stream_index, 0)) < 0) return ret; st->codecpar->sample_rate = sti->avctx->sample_rate; st->codecpar->bit_rate = sti->avctx->bit_rate; #if FF_API_OLD_CHANNEL_LAYOUT FF_DISABLE_DEPRECATION_WARNINGS - st->codecpar->channels = sti->avctx->channels; - st->codecpar->channel_layout = sti->avctx->channel_layout; + /* parser setting the old-style fields */ + if (sti->avctx->channels != orig_channels || + sti->avctx->channel_layout != orig_channel_layout) { + av_channel_layout_uninit(&sti->avctx->ch_layout); + if (sti->avctx->channel_layout) { + av_channel_layout_from_mask(&sti->avctx->ch_layout, sti->avctx->channel_layout); + } else { + sti->avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; + sti->avctx->ch_layout.nb_channels = sti->avctx->channels; + } + } + + st->codecpar->channels = sti->avctx->ch_layout.nb_channels; + st->codecpar->channel_layout = sti->avctx->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ? + sti->avctx->ch_layout.u.mask : 0; FF_ENABLE_DEPRECATION_WARNINGS #endif + ret = av_channel_layout_copy(&st->codecpar->ch_layout, &sti->avctx->ch_layout); + if (ret < 0) + return ret; st->codecpar->codec_id = sti->avctx->codec_id; } else { /* free packet */ @@ -1948,7 +1970,7 @@ static int has_codec_parameters(const AVStream *st, const char **errmsg_ptr) FAIL("unspecified sample format"); if (!avctx->sample_rate) FAIL("unspecified sample rate"); - if (!avctx->channels) + if (!avctx->ch_layout.nb_channels) FAIL("unspecified number of channels"); if (sti->info->found_decoder >= 0 && !sti->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS) FAIL("no decodable DTS frames");