diff mbox series

[FFmpeg-devel,231/281] libspeexdec: convert to new channel layout API

Message ID 20220113020518.730-22-jamrial@gmail.com
State New
Headers show
Series New channel layout API | expand

Commit Message

James Almer Jan. 13, 2022, 2:05 a.m. UTC
From: Anton Khirnov <anton@khirnov.net>

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/libspeexdec.c | 19 ++++++++++---------
 libavcodec/libspeexenc.c | 17 +++++++++--------
 2 files changed, 19 insertions(+), 17 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/libspeexdec.c b/libavcodec/libspeexdec.c
index 6f032907fc..8029786b1f 100644
--- a/libavcodec/libspeexdec.c
+++ b/libavcodec/libspeexdec.c
@@ -42,7 +42,7 @@  static av_cold int libspeex_decode_init(AVCodecContext *avctx)
     LibSpeexContext *s = avctx->priv_data;
     const SpeexMode *mode;
     SpeexHeader *header = NULL;
-    int spx_mode;
+    int spx_mode, channels;
 
     if (avctx->extradata && avctx->extradata_size >= 80) {
         header = speex_packet_to_header(avctx->extradata,
@@ -68,7 +68,7 @@  static av_cold int libspeex_decode_init(AVCodecContext *avctx)
         spx_mode           = 0;
     } else if (header) {
         avctx->sample_rate = header->rate;
-        avctx->channels    = header->nb_channels;
+        channels           = header->nb_channels;
         spx_mode           = header->mode;
         speex_header_free(header);
     } else {
@@ -94,14 +94,15 @@  static av_cold int libspeex_decode_init(AVCodecContext *avctx)
     if (!avctx->sample_rate)
         avctx->sample_rate = 8000 << spx_mode;
 
-    if (avctx->channels < 1 || avctx->channels > 2) {
+    if (channels < 1 || channels > 2) {
         /* libspeex can handle mono or stereo if initialized as stereo */
         av_log(avctx, AV_LOG_ERROR, "Invalid channel count: %d.\n"
-                                    "Decoding as stereo.\n", avctx->channels);
-        avctx->channels = 2;
+                                    "Decoding as stereo.\n", channels);
+        channels = 2;
     }
-    avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
-                                                   AV_CH_LAYOUT_MONO;
+    av_channel_layout_uninit(&avctx->ch_layout);
+    avctx->ch_layout = channels == 2 ? (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO :
+                                       (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
 
     speex_bits_init(&s->bits);
     s->dec_state = speex_decoder_init(mode);
@@ -110,7 +111,7 @@  static av_cold int libspeex_decode_init(AVCodecContext *avctx)
         return -1;
     }
 
-    if (avctx->channels == 2) {
+    if (channels == 2) {
         SpeexCallback callback;
         callback.callback_id = SPEEX_INBAND_STEREO;
         callback.func = speex_std_stereo_request_handler;
@@ -163,7 +164,7 @@  static int libspeex_decode_frame(AVCodecContext *avctx, void *data,
         av_log(avctx, AV_LOG_ERROR, "Error decoding Speex frame.\n");
         return AVERROR_INVALIDDATA;
     }
-    if (avctx->channels == 2)
+    if (avctx->ch_layout.nb_channels == 2)
         speex_decode_stereo_int(output, s->frame_size, &s->stereo);
 
     *got_frame_ptr = 1;
diff --git a/libavcodec/libspeexenc.c b/libavcodec/libspeexenc.c
index d095b41bee..058dafad39 100644
--- a/libavcodec/libspeexenc.c
+++ b/libavcodec/libspeexenc.c
@@ -28,8 +28,8 @@ 
  * order to control various encoding parameters.
  *
  * Channels
- *     Speex only supports mono or stereo, so avctx->channels must be set to
- *     1 or 2.
+ *     Speex only supports mono or stereo, so avctx->ch_layout.nb_channels must
+ *     be set to 1 or 2.
  *
  * Sample Rate / Encoding Mode
  *     Speex has 3 modes, each of which uses a specific sample rate.
@@ -114,7 +114,7 @@  static av_cold void print_enc_params(AVCodecContext *avctx,
 {
     const char *mode_str = "unknown";
 
-    av_log(avctx, AV_LOG_DEBUG, "channels: %d\n", avctx->channels);
+    av_log(avctx, AV_LOG_DEBUG, "channels: %d\n", avctx->ch_layout.nb_channels);
     switch (s->header.mode) {
     case SPEEX_MODEID_NB:  mode_str = "narrowband";     break;
     case SPEEX_MODEID_WB:  mode_str = "wideband";       break;
@@ -146,15 +146,16 @@  static av_cold void print_enc_params(AVCodecContext *avctx,
 static av_cold int encode_init(AVCodecContext *avctx)
 {
     LibSpeexEncContext *s = avctx->priv_data;
+    int channels = avctx->ch_layout.nb_channels;
     const SpeexMode *mode;
     uint8_t *header_data;
     int header_size;
     int32_t complexity;
 
     /* channels */
-    if (avctx->channels < 1 || avctx->channels > 2) {
+    if (channels < 1 || channels > 2) {
         av_log(avctx, AV_LOG_ERROR, "Invalid channels (%d). Only stereo and "
-               "mono are supported\n", avctx->channels);
+               "mono are supported\n", channels);
         return AVERROR(EINVAL);
     }
 
@@ -175,7 +176,7 @@  static av_cold int encode_init(AVCodecContext *avctx)
         av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex\n");
         return -1;
     }
-    speex_init_header(&s->header, avctx->sample_rate, avctx->channels, mode);
+    speex_init_header(&s->header, avctx->sample_rate, channels, mode);
 
     /* rate control method and parameters */
     if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
@@ -210,7 +211,7 @@  static av_cold int encode_init(AVCodecContext *avctx)
         }
         /* stereo side information adds about 800 bps to the base bitrate */
         /* TODO: this should be calculated exactly */
-        avctx->bit_rate = s->header.bitrate + (avctx->channels == 2 ? 800 : 0);
+        avctx->bit_rate = s->header.bitrate + (channels == 2 ? 800 : 0);
     }
 
     /* VAD is activated with VBR or can be turned on by itself */
@@ -275,7 +276,7 @@  static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
 
     if (samples) {
         /* encode Speex frame */
-        if (avctx->channels == 2)
+        if (avctx->ch_layout.nb_channels == 2)
             speex_encode_stereo_int(samples, s->header.frame_size, &s->bits);
         speex_encode_int(s->enc_state, samples, &s->bits);
         s->pkt_frame_count++;