[FFmpeg-devel] [PATCH 213/281] g723_1: convert to new channel layout API

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


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/g723_1_parser.c |  2 +-
 libavcodec/g723_1dec.c     | 19 ++++++++++---------
 libavcodec/g723_1enc.c     |  8 +++-----
 3 files changed, 14 insertions(+), 15 deletions(-)

diff --git a/libavcodec/g723_1_parser.c b/libavcodec/g723_1_parser.c
index b6b3fcb84b..2ed1a8ab19 100644
--- a/libavcodec/g723_1_parser.c
+++ b/libavcodec/g723_1_parser.c
@@ -37,7 +37,7 @@ static int g723_1_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
     int next = END_NOT_FOUND;
 
     if (buf_size > 0)
-        next = frame_size[buf[0] & 3] * FFMAX(1, avctx->channels);
+        next = frame_size[buf[0] & 3] * FFMAX(1, avctx->ch_layout.nb_channels);
 
     if (ff_combine_frame(pc, next, &buf, &buf_size) < 0 || !buf_size) {
         *poutbuf      = NULL;
diff --git a/libavcodec/g723_1dec.c b/libavcodec/g723_1dec.c
index 7d75adbd7a..8f381b1331 100644
--- a/libavcodec/g723_1dec.c
+++ b/libavcodec/g723_1dec.c
@@ -117,12 +117,12 @@ static av_cold int g723_1_decode_init(AVCodecContext *avctx)
     G723_1_Context *s = avctx->priv_data;
 
     avctx->sample_fmt     = AV_SAMPLE_FMT_S16P;
-    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 (avctx->ch_layout.nb_channels < 1 || avctx->ch_layout.nb_channels > 2) {
+        av_log(avctx, AV_LOG_ERROR, "Only mono and stereo are supported (requested channels: %d).\n",
+               avctx->ch_layout.nb_channels);
         return AVERROR(EINVAL);
     }
-    avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
-    for (int ch = 0; ch < avctx->channels; ch++) {
+    for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
         G723_1_ChannelContext *p = &s->ch[ch];
 
         p->pf_gain = 1 << 12;
@@ -932,6 +932,7 @@ static int g723_1_decode_frame(AVCodecContext *avctx, void *data,
     const uint8_t *buf = avpkt->data;
     int buf_size       = avpkt->size;
     int dec_mode       = buf[0] & 3;
+    int channels       = avctx->ch_layout.nb_channels;
 
     PPFParam ppf[SUBFRAMES];
     int16_t cur_lsp[LPC_ORDER];
@@ -940,7 +941,7 @@ static int g723_1_decode_frame(AVCodecContext *avctx, void *data,
     int16_t *out;
     int bad_frame = 0, i, j, ret;
 
-    if (buf_size < frame_size[dec_mode] * avctx->channels) {
+    if (buf_size < frame_size[dec_mode] * channels) {
         if (buf_size)
             av_log(avctx, AV_LOG_WARNING,
                    "Expected %d bytes, got %d - skipping packet\n",
@@ -953,12 +954,12 @@ static int g723_1_decode_frame(AVCodecContext *avctx, void *data,
     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
 
-    for (int ch = 0; ch < avctx->channels; ch++) {
+    for (int ch = 0; ch < channels; ch++) {
         G723_1_ChannelContext *p = &s->ch[ch];
         int16_t *audio = p->audio;
 
-        if (unpack_bitstream(p, buf + ch * (buf_size / avctx->channels),
-                             buf_size / avctx->channels) < 0) {
+        if (unpack_bitstream(p, buf + ch * (buf_size / channels),
+                             buf_size / channels) < 0) {
             bad_frame = 1;
             if (p->past_frame_type == ACTIVE_FRAME)
                 p->cur_frame_type = ACTIVE_FRAME;
@@ -1090,7 +1091,7 @@ static int g723_1_decode_frame(AVCodecContext *avctx, void *data,
 
     *got_frame_ptr = 1;
 
-    return frame_size[dec_mode] * avctx->channels;
+    return frame_size[dec_mode] * channels;
 }
 
 #define OFFSET(x) offsetof(G723_1_Context, x)
diff --git a/libavcodec/g723_1enc.c b/libavcodec/g723_1enc.c
index 2a8149b4cd..7e893fafc7 100644
--- a/libavcodec/g723_1enc.c
+++ b/libavcodec/g723_1enc.c
@@ -99,11 +99,6 @@ static av_cold int g723_1_encode_init(AVCodecContext *avctx)
         return AVERROR(EINVAL);
     }
 
-    if (avctx->channels != 1) {
-        av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
-        return AVERROR(EINVAL);
-    }
-
     if (avctx->bit_rate == 6300) {
         p->cur_rate = RATE_6300;
     } else if (avctx->bit_rate == 5300) {
@@ -1256,5 +1251,8 @@ const AVCodec ff_g723_1_encoder = {
     .sample_fmts    = (const enum AVSampleFormat[]) {
         AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
     },
+    .ch_layouts     = (const AVChannelLayout[]){
+        AV_CHANNEL_LAYOUT_MONO, { 0 }
+    },
     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
 };
-- 
2.34.1



More information about the ffmpeg-devel mailing list