[FFmpeg-devel] [PATCH 181/279] aac: convert to new channel layout API

Lynne dev at lynne.ee
Wed Dec 8 10:54:21 EET 2021


8 Dec 2021, 02:06 by jamrial at gmail.com:

> From: Anton Khirnov <anton at khirnov.net>
>
> Signed-off-by: Vittorio Giovara <vittorio.giovara at gmail.com>
> Signed-off-by: Anton Khirnov <anton at khirnov.net>
> Signed-off-by: James Almer <jamrial at gmail.com>
> ---
>  libavcodec/aac.h              | 11 ++++--
>  libavcodec/aac_ac3_parser.c   |  9 +++--
>  libavcodec/aaccoder.c         | 10 +++---
>  libavcodec/aaccoder_twoloop.h |  4 +--
>  libavcodec/aacdec_template.c  | 59 ++++++++++++++++++++++++--------
>  libavcodec/aacenc.c           | 13 ++++---
>  libavcodec/aacenc.h           | 64 ++++++++++++++++++-----------------
>  libavcodec/aacenctab.h        | 16 ++++-----
>  libavcodec/aacpsy.c           |  8 ++---
>  libavcodec/psymodel.c         |  8 ++---
>  libavcodec/psymodel.h         |  2 +-
>  11 files changed, 125 insertions(+), 79 deletions(-)
>
> diff --git a/libavcodec/aac.h b/libavcodec/aac.h
> index 1e82f56ca9..53be546857 100644
> --- a/libavcodec/aac.h
> +++ b/libavcodec/aac.h
> @@ -32,6 +32,7 @@
>  
>  
>  #include "aac_defines.h"
> +#include "libavutil/channel_layout.h"
>  #include "libavutil/float_dsp.h"
>  #include "libavutil/fixed_dsp.h"
>  #include "libavutil/mem_internal.h"
> @@ -125,8 +126,7 @@ typedef struct OutputConfiguration {
>  MPEG4AudioConfig m4ac;
>  uint8_t layout_map[MAX_ELEM_ID*4][3];
>  int layout_map_tags;
> -    int channels;
> -    uint64_t channel_layout;
> +    AVChannelLayout ch_layout;
>  enum OCStatus status;
>  } OutputConfiguration;
>  
> @@ -288,6 +288,11 @@ typedef struct ChannelElement {
>  SpectralBandReplication sbr;
>  } ChannelElement;
>  
> +enum AACOutputChannelOrder {
> +    CHANNEL_ORDER_DEFAULT,
> +    CHANNEL_ORDER_CODED,
> +};
> +
>  /**
>  * main AAC context
>  */
> @@ -352,6 +357,8 @@ struct AACContext {
>  int dmono_mode;      ///< 0->not dmono, 1->use first channel, 2->use second channel
>  /** @} */
>  
> +    enum AACOutputChannelOrder output_channel_order;
> +
>  DECLARE_ALIGNED(32, INTFLOAT, temp)[128];
>  
>  OutputConfiguration oc[2];
> diff --git a/libavcodec/aac_ac3_parser.c b/libavcodec/aac_ac3_parser.c
> index e84d30aea2..cd54a0a3e2 100644
> --- a/libavcodec/aac_ac3_parser.c
> +++ b/libavcodec/aac_ac3_parser.c
> @@ -90,8 +90,13 @@ get_next:
>  if (avctx->codec_id != AV_CODEC_ID_AAC) {
>  avctx->sample_rate = s->sample_rate;
>  if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) {
> -                avctx->channels = s->channels;
> -                avctx->channel_layout = s->channel_layout;
> +                av_channel_layout_uninit(&avctx->ch_layout);
> +                if (s->channel_layout) {
> +                    av_channel_layout_from_mask(&avctx->ch_layout, s->channel_layout);
> +                } else {
> +                    avctx->ch_layout.order       = AV_CHANNEL_ORDER_UNSPEC;
> +                    avctx->ch_layout.nb_channels = s->channels;
> +                }
>  }
>  s1->duration = s->samples;
>  avctx->audio_service_type = s->service_type;
> diff --git a/libavcodec/aaccoder.c b/libavcodec/aaccoder.c
> index 7bbd4d5b2e..f460479498 100644
> --- a/libavcodec/aaccoder.c
> +++ b/libavcodec/aaccoder.c
> @@ -397,7 +397,7 @@ static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
>  const float lambda)
>  {
>  int start = 0, i, w, w2, g;
> -    int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels * (lambda / 120.f);
> +    int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->ch_layout.nb_channels * (lambda / 120.f);
>  float dists[128] = { 0 }, uplims[128] = { 0 };
>  float maxvals[128];
>  int fflag, minscaler;
> @@ -556,7 +556,7 @@ static void search_for_pns(AACEncContext *s, AVCodecContext *avctx, SingleChanne
>  const float pns_transient_energy_r = FFMIN(0.7f, lambda / 140.f);
>  
>  int refbits = avctx->bit_rate * 1024.0 / avctx->sample_rate
> -        / ((avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : avctx->channels)
> +        / ((avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : avctx->ch_layout.nb_channels)
>  * (lambda / 120.f);
>  
>  /** Keep this in sync with twoloop's cutoff selection */
> @@ -564,7 +564,7 @@ static void search_for_pns(AACEncContext *s, AVCodecContext *avctx, SingleChanne
>  int prev = -1000, prev_sf = -1;
>  int frame_bit_rate = (avctx->flags & AV_CODEC_FLAG_QSCALE)
>  ? (refbits * rate_bandwidth_multiplier * avctx->sample_rate / 1024)
> -        : (avctx->bit_rate / avctx->channels);
> +        : (avctx->bit_rate / avctx->ch_layout.nb_channels);
>  
>  frame_bit_rate *= 1.15f;
>  
> @@ -693,14 +693,14 @@ static void mark_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelEleme
>  const float pns_transient_energy_r = FFMIN(0.7f, lambda / 140.f);
>  
>  int refbits = avctx->bit_rate * 1024.0 / avctx->sample_rate
> -        / ((avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : avctx->channels)
> +        / ((avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : avctx->ch_layout.nb_channels)
>  * (lambda / 120.f);
>  
>  /** Keep this in sync with twoloop's cutoff selection */
>  float rate_bandwidth_multiplier = 1.5f;
>  int frame_bit_rate = (avctx->flags & AV_CODEC_FLAG_QSCALE)
>  ? (refbits * rate_bandwidth_multiplier * avctx->sample_rate / 1024)
> -        : (avctx->bit_rate / avctx->channels);
> +        : (avctx->bit_rate / avctx->ch_layout.nb_channels);
>  
>  frame_bit_rate *= 1.15f;
>  
> diff --git a/libavcodec/aaccoder_twoloop.h b/libavcodec/aaccoder_twoloop.h
> index 8e1bc88a85..0504a916ad 100644
> --- a/libavcodec/aaccoder_twoloop.h
> +++ b/libavcodec/aaccoder_twoloop.h
> @@ -71,7 +71,7 @@ static void search_for_quantizers_twoloop(AVCodecContext *avctx,
>  {
>  int start = 0, i, w, w2, g, recomprd;
>  int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate
> -        / ((avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : avctx->channels)
> +        / ((avctx->flags & AV_CODEC_FLAG_QSCALE) ? 2.0f : avctx->ch_layout.nb_channels)
>  * (lambda / 120.f);
>  int refbits = destbits;
>  int toomanybits, toofewbits;
> @@ -186,7 +186,7 @@ static void search_for_quantizers_twoloop(AVCodecContext *avctx,
>  float rate_bandwidth_multiplier = 1.5f;
>  int frame_bit_rate = (avctx->flags & AV_CODEC_FLAG_QSCALE)
>  ? (refbits * rate_bandwidth_multiplier * avctx->sample_rate / 1024)
> -            : (avctx->bit_rate / avctx->channels);
> +            : (avctx->bit_rate / avctx->ch_layout.nb_channels);
>  
>  /** Compensate for extensions that increase efficiency */
>  if (s->options.pns || s->options.intensity_stereo)
> diff --git a/libavcodec/aacdec_template.c b/libavcodec/aacdec_template.c
> index 7149b331ae..387a4acfce 100644
> --- a/libavcodec/aacdec_template.c
> +++ b/libavcodec/aacdec_template.c
> @@ -174,7 +174,7 @@ static int frame_configure_elements(AVCodecContext *avctx)
>  
>  /* get output buffer */
>  av_frame_unref(ac->frame);
> -    if (!avctx->channels)
> +    if (!avctx->ch_layout.nb_channels)
>  return 1;
>  
>  ac->frame->nb_samples = 2048;
> @@ -182,7 +182,7 @@ static int frame_configure_elements(AVCodecContext *avctx)
>  return ret;
>  
>  /* map output channel pointers to AVFrame data */
> -    for (ch = 0; ch < avctx->channels; ch++) {
> +    for (ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
>  if (ac->output_element[ch])
>  ac->output_element[ch]->ret = (INTFLOAT *)ac->frame->extended_data[ch];
>  }
> @@ -517,8 +517,7 @@ static int push_output_configuration(AACContext *ac) {
>  static void pop_output_configuration(AACContext *ac) {
>  if (ac->oc[1].status != OC_LOCKED && ac->oc[0].status != OC_NONE) {
>  ac->oc[1] = ac->oc[0];
> -        ac->avctx->channels = ac->oc[1].channels;
> -        ac->avctx->channel_layout = ac->oc[1].channel_layout;
> +        ac->avctx->ch_layout = ac->oc[1].ch_layout;
>  output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
>  ac->oc[1].status, 0);
>  }
> @@ -555,7 +554,14 @@ static int output_configure(AACContext *ac,
>  }
>  // Try to sniff a reasonable channel order, otherwise output the
>  // channels in the order the PCE declared them.
> -    if (avctx->request_channel_layout != AV_CH_LAYOUT_NATIVE)
> +#if FF_API_OLD_CHANNEL_LAYOUT
> +FF_DISABLE_DEPRECATION_WARNINGS
> +    if (avctx->request_channel_layout == AV_CH_LAYOUT_NATIVE)
> +        ac->output_channel_order = CHANNEL_ORDER_CODED;
> +FF_ENABLE_DEPRECATION_WARNINGS
> +#endif
> +
> +    if (ac->output_channel_order == CHANNEL_ORDER_DEFAULT)
>  layout = sniff_channel_order(layout_map, tags);
>  for (i = 0; i < tags; i++) {
>  int type =     layout_map[i][0];
> @@ -577,9 +583,22 @@ static int output_configure(AACContext *ac,
>  }
>  }
>  
> -    if (layout) avctx->channel_layout = layout;
> -                            ac->oc[1].channel_layout = layout;
> -    avctx->channels       = ac->oc[1].channels       = channels;
> +    av_channel_layout_uninit(&ac->oc[1].ch_layout);
> +    if (layout)
> +        av_channel_layout_from_mask(&ac->oc[1].ch_layout, layout);
> +    else {
> +        ac->oc[1].ch_layout.order       = AV_CHANNEL_ORDER_UNSPEC;
> +        ac->oc[1].ch_layout.nb_channels = channels;
> +    }
> +
> +    av_channel_layout_copy(&avctx->ch_layout, &ac->oc[1].ch_layout);
> +#if FF_API_OLD_CHANNEL_LAYOUT
> +FF_DISABLE_DEPRECATION_WARNINGS
> +    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 
>

Shouldn't the layout be set to AV_CHANNEL_ORDER_CUSTOM if
the user has asked for a custom layout?


More information about the ffmpeg-devel mailing list