[FFmpeg-devel] [PATCH 135/281] rtp: convert to new channel layout API

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


From: Vittorio Giovara <vittorio.giovara at gmail.com>

Signed-off-by: Vittorio Giovara <vittorio.giovara at gmail.com>
Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavformat/rtp.c        | 10 ++++++----
 libavformat/rtpdec.c     |  4 ++--
 libavformat/rtpdec_amr.c |  3 +--
 libavformat/rtpenc.c     | 14 +++++++-------
 libavformat/rtsp.c       |  6 +++---
 libavformat/rtsp.h       |  1 -
 6 files changed, 19 insertions(+), 19 deletions(-)

diff --git a/libavformat/rtp.c b/libavformat/rtp.c
index c536a6f082..004191a550 100644
--- a/libavformat/rtp.c
+++ b/libavformat/rtp.c
@@ -77,8 +77,10 @@ int ff_rtp_get_codec_info(AVCodecParameters *par, int payload_type)
             if (rtp_payload_types[i].codec_id != AV_CODEC_ID_NONE) {
                 par->codec_type = rtp_payload_types[i].codec_type;
                 par->codec_id = rtp_payload_types[i].codec_id;
-                if (rtp_payload_types[i].audio_channels > 0)
-                    par->channels = rtp_payload_types[i].audio_channels;
+                if (rtp_payload_types[i].audio_channels > 0) {
+                    par->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
+                    par->ch_layout.nb_channels = rtp_payload_types[i].audio_channels;
+                }
                 if (rtp_payload_types[i].clock_rate > 0)
                     par->sample_rate = rtp_payload_types[i].clock_rate;
                 return 0;
@@ -111,13 +113,13 @@ int ff_rtp_get_payload_type(const AVFormatContext *fmt,
             /* G722 has 8000 as nominal rate even if the sample rate is 16000,
              * see section 4.5.2 in RFC 3551. */
             if (par->codec_id == AV_CODEC_ID_ADPCM_G722 &&
-                par->sample_rate == 16000 && par->channels == 1)
+                par->sample_rate == 16000 && par->ch_layout.nb_channels == 1)
                 return rtp_payload_types[i].pt;
             if (par->codec_type == AVMEDIA_TYPE_AUDIO &&
                 ((rtp_payload_types[i].clock_rate > 0 &&
                   par->sample_rate != rtp_payload_types[i].clock_rate) ||
                  (rtp_payload_types[i].audio_channels > 0 &&
-                  par->channels != rtp_payload_types[i].audio_channels)))
+                  par->ch_layout.nb_channels != rtp_payload_types[i].audio_channels)))
                 continue;
             return rtp_payload_types[i].pt;
         }
diff --git a/libavformat/rtpdec.c b/libavformat/rtpdec.c
index 20fe2b82d7..e56fde9d48 100644
--- a/libavformat/rtpdec.c
+++ b/libavformat/rtpdec.c
@@ -538,7 +538,7 @@ static int opus_write_extradata(AVCodecParameters *codecpar)
      * This mapping family only supports mono and stereo layouts. And RFC7587
      * specifies that the number of channels in the SDP must be 2.
      */
-    if (codecpar->channels > 2) {
+    if (codecpar->ch_layout.nb_channels > 2) {
         return AVERROR_INVALIDDATA;
     }
 
@@ -553,7 +553,7 @@ static int opus_write_extradata(AVCodecParameters *codecpar)
     /* Version */
     bytestream_put_byte  (&bs, 0x1);
     /* Channel count */
-    bytestream_put_byte  (&bs, codecpar->channels);
+    bytestream_put_byte  (&bs, codecpar->ch_layout.nb_channels);
     /* Pre skip */
     bytestream_put_le16  (&bs, 0);
     /* Input sample rate */
diff --git a/libavformat/rtpdec_amr.c b/libavformat/rtpdec_amr.c
index 988b7bddfd..20b435039c 100644
--- a/libavformat/rtpdec_amr.c
+++ b/libavformat/rtpdec_amr.c
@@ -64,11 +64,10 @@ static int amr_handle_packet(AVFormatContext *ctx, PayloadContext *data,
         return AVERROR_INVALIDDATA;
     }
 
-    if (st->codecpar->channels != 1) {
+    if (st->codecpar->ch_layout.nb_channels != 1) {
         av_log(ctx, AV_LOG_ERROR, "Only mono AMR is supported\n");
         return AVERROR_INVALIDDATA;
     }
-    st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
 
     /* The AMR RTP packet consists of one header byte, followed
      * by one TOC byte for each AMR frame in the packet, followed
diff --git a/libavformat/rtpenc.c b/libavformat/rtpenc.c
index 6be67b5885..ce629a8095 100644
--- a/libavformat/rtpenc.c
+++ b/libavformat/rtpenc.c
@@ -236,7 +236,7 @@ static int rtp_write_header(AVFormatContext *s1)
         avpriv_set_pts_info(st, 32, 1, 8000);
         break;
     case AV_CODEC_ID_OPUS:
-        if (st->codecpar->channels > 2) {
+        if (st->codecpar->ch_layout.nb_channels > 2) {
             av_log(s1, AV_LOG_ERROR, "Multistream opus not supported in RTP\n");
             goto fail;
         }
@@ -264,7 +264,7 @@ static int rtp_write_header(AVFormatContext *s1)
             av_log(s1, AV_LOG_ERROR, "RTP max payload size too small for AMR\n");
             goto fail;
         }
-        if (st->codecpar->channels != 1) {
+        if (st->codecpar->ch_layout.nb_channels != 1) {
             av_log(s1, AV_LOG_ERROR, "Only mono is supported\n");
             goto fail;
         }
@@ -541,24 +541,24 @@ static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
     case AV_CODEC_ID_PCM_ALAW:
     case AV_CODEC_ID_PCM_U8:
     case AV_CODEC_ID_PCM_S8:
-        return rtp_send_samples(s1, pkt->data, size, 8 * st->codecpar->channels);
+        return rtp_send_samples(s1, pkt->data, size, 8 * st->codecpar->ch_layout.nb_channels);
     case AV_CODEC_ID_PCM_U16BE:
     case AV_CODEC_ID_PCM_U16LE:
     case AV_CODEC_ID_PCM_S16BE:
     case AV_CODEC_ID_PCM_S16LE:
-        return rtp_send_samples(s1, pkt->data, size, 16 * st->codecpar->channels);
+        return rtp_send_samples(s1, pkt->data, size, 16 * st->codecpar->ch_layout.nb_channels);
     case AV_CODEC_ID_PCM_S24BE:
-        return rtp_send_samples(s1, pkt->data, size, 24 * st->codecpar->channels);
+        return rtp_send_samples(s1, pkt->data, size, 24 * st->codecpar->ch_layout.nb_channels);
     case AV_CODEC_ID_ADPCM_G722:
         /* The actual sample size is half a byte per sample, but since the
          * stream clock rate is 8000 Hz while the sample rate is 16000 Hz,
          * the correct parameter for send_samples_bits is 8 bits per stream
          * clock. */
-        return rtp_send_samples(s1, pkt->data, size, 8 * st->codecpar->channels);
+        return rtp_send_samples(s1, pkt->data, size, 8 * st->codecpar->ch_layout.nb_channels);
     case AV_CODEC_ID_ADPCM_G726:
     case AV_CODEC_ID_ADPCM_G726LE:
         return rtp_send_samples(s1, pkt->data, size,
-                                st->codecpar->bits_per_coded_sample * st->codecpar->channels);
+                                st->codecpar->bits_per_coded_sample * st->codecpar->ch_layout.nb_channels);
     case AV_CODEC_ID_MP2:
     case AV_CODEC_ID_MP3:
         rtp_send_mpegaudio(s1, pkt->data, size);
diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c
index 70c18941ca..5fa756bf5c 100644
--- a/libavformat/rtsp.c
+++ b/libavformat/rtsp.c
@@ -323,19 +323,19 @@ static int sdp_parse_rtpmap(AVFormatContext *s,
     case AVMEDIA_TYPE_AUDIO:
         av_log(s, AV_LOG_DEBUG, "audio codec set to: %s\n", c_name);
         par->sample_rate = RTSP_DEFAULT_AUDIO_SAMPLERATE;
-        par->channels = RTSP_DEFAULT_NB_AUDIO_CHANNELS;
+        par->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
         if (i > 0) {
             par->sample_rate = i;
             avpriv_set_pts_info(st, 32, 1, par->sample_rate);
             get_word_sep(buf, sizeof(buf), "/", &p);
             i = atoi(buf);
             if (i > 0)
-                par->channels = i;
+                av_channel_layout_default(&par->ch_layout, i);
         }
         av_log(s, AV_LOG_DEBUG, "audio samplerate set to: %i\n",
                par->sample_rate);
         av_log(s, AV_LOG_DEBUG, "audio channels set to: %i\n",
-               par->channels);
+               par->ch_layout.nb_channels);
         break;
     case AVMEDIA_TYPE_VIDEO:
         av_log(s, AV_LOG_DEBUG, "video codec set to: %s\n", c_name);
diff --git a/libavformat/rtsp.h b/libavformat/rtsp.h
index 8b64e29d70..3133bf61c1 100644
--- a/libavformat/rtsp.h
+++ b/libavformat/rtsp.h
@@ -75,7 +75,6 @@ enum RTSPControlTransport {
 #define RTSPS_DEFAULT_PORT  322
 #define RTSP_MAX_TRANSPORTS 8
 #define RTSP_TCP_MAX_PACKET_SIZE 1472
-#define RTSP_DEFAULT_NB_AUDIO_CHANNELS 1
 #define RTSP_DEFAULT_AUDIO_SAMPLERATE 44100
 #define RTSP_RTP_PORT_MIN 5000
 #define RTSP_RTP_PORT_MAX 65000
-- 
2.34.1



More information about the ffmpeg-devel mailing list