[FFmpeg-devel] [PATCH 215/281] g729: convert to new channel layout API

James Almer jamrial at gmail.com
Thu Jan 13 04:04:54 EET 2022


From: Anton Khirnov <anton at khirnov.net>

Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavcodec/g729_parser.c |  2 +-
 libavcodec/g729dec.c     | 20 +++++++++++---------
 2 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/libavcodec/g729_parser.c b/libavcodec/g729_parser.c
index 8c06ce4ee6..d66df141f9 100644
--- a/libavcodec/g729_parser.c
+++ b/libavcodec/g729_parser.c
@@ -48,7 +48,7 @@ static int g729_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
         s->block_size = (avctx->bit_rate < 8000) ? G729D_6K4_BLOCK_SIZE : G729_8K_BLOCK_SIZE;
         if (avctx->codec_id == AV_CODEC_ID_ACELP_KELVIN)
             s->block_size++;
-        s->block_size *= avctx->channels;
+        s->block_size *= avctx->ch_layout.nb_channels;
         s->duration   = avctx->frame_size;
     }
 
diff --git a/libavcodec/g729dec.c b/libavcodec/g729dec.c
index 7525ab7491..5a90f9fc1c 100644
--- a/libavcodec/g729dec.c
+++ b/libavcodec/g729dec.c
@@ -350,10 +350,11 @@ static av_cold int decoder_init(AVCodecContext * avctx)
 {
     G729Context *s = avctx->priv_data;
     G729ChannelContext *ctx;
+    int channels = avctx->ch_layout.nb_channels;
     int c,i,k;
 
-    if (avctx->channels < 1 || avctx->channels > 2) {
-        av_log(avctx, AV_LOG_ERROR, "Only mono and stereo are supported (requested channels: %d).\n", avctx->channels);
+    if (channels < 1 || channels > 2) {
+        av_log(avctx, AV_LOG_ERROR, "Only mono and stereo are supported (requested channels: %d).\n", channels);
         return AVERROR(EINVAL);
     }
     avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
@@ -362,11 +363,11 @@ static av_cold int decoder_init(AVCodecContext * avctx)
     avctx->frame_size = SUBFRAME_SIZE << 1;
 
     ctx =
-    s->channel_context = av_mallocz(sizeof(G729ChannelContext) * avctx->channels);
+    s->channel_context = av_mallocz(sizeof(G729ChannelContext) * channels);
     if (!ctx)
         return AVERROR(ENOMEM);
 
-    for (c = 0; c < avctx->channels; c++) {
+    for (c = 0; c < channels; c++) {
         ctx->gain_coeff = 16384; // 1.0 in (1.14)
 
         for (k = 0; k < MA_NP + 1; k++) {
@@ -412,6 +413,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr,
     G729Formats packet_type;
     G729Context *s = avctx->priv_data;
     G729ChannelContext *ctx = s->channel_context;
+    int channels = avctx->ch_layout.nb_channels;
     int16_t lp[2][11];           // (3.12)
     uint8_t ma_predictor;     ///< switched MA predictor of LSP quantizer
     uint8_t quantizer_1st;    ///< first stage vector of quantizer
@@ -430,14 +432,14 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr,
     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
 
-    if (buf_size && buf_size % ((G729_8K_BLOCK_SIZE + (avctx->codec_id == AV_CODEC_ID_ACELP_KELVIN)) * avctx->channels) == 0) {
+    if (buf_size && buf_size % ((G729_8K_BLOCK_SIZE + (avctx->codec_id == AV_CODEC_ID_ACELP_KELVIN)) * channels) == 0) {
         packet_type = FORMAT_G729_8K;
         format = &format_g729_8k;
         //Reset voice decision
         ctx->onset = 0;
         ctx->voice_decision = DECISION_VOICE;
         av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729 @ 8kbit/s");
-    } else if (buf_size == G729D_6K4_BLOCK_SIZE * avctx->channels && avctx->codec_id != AV_CODEC_ID_ACELP_KELVIN) {
+    } else if (buf_size == G729D_6K4_BLOCK_SIZE * channels && avctx->codec_id != AV_CODEC_ID_ACELP_KELVIN) {
         packet_type = FORMAT_G729D_6K4;
         format = &format_g729d_6k4;
         av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729D @ 6.4kbit/s");
@@ -446,13 +448,13 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr,
         return AVERROR_INVALIDDATA;
     }
 
-    for (c = 0; c < avctx->channels; c++) {
+    for (c = 0; c < channels; c++) {
         int frame_erasure = 0; ///< frame erasure detected during decoding
         int bad_pitch = 0;     ///< parity check failed
         int is_periodic = 0;   ///< whether one of the subframes is declared as periodic or not
         out_frame = (int16_t*)frame->data[c];
         if (avctx->codec_id == AV_CODEC_ID_ACELP_KELVIN) {
-            if (*buf != ((avctx->channels - 1 - c) * 0x80 | 2))
+            if (*buf != ((avctx->ch_layout.nb_channels - 1 - c) * 0x80 | 2))
                 avpriv_request_sample(avctx, "First byte value %x for channel %d", *buf, c);
             buf++;
         }
@@ -739,7 +741,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr,
     }
 
     *got_frame_ptr = 1;
-    return (format->block_size + (avctx->codec_id == AV_CODEC_ID_ACELP_KELVIN)) * avctx->channels;
+    return (format->block_size + (avctx->codec_id == AV_CODEC_ID_ACELP_KELVIN)) * channels;
 }
 
 static av_cold int decode_close(AVCodecContext *avctx)
-- 
2.34.1



More information about the ffmpeg-devel mailing list