Browse code

rtpenc: MP4A-LATM payload support

This is enabled with an AVOption on the RTP muxer. The SDP
generator looks for a latm flag in the rtpflags field.

Signed-off-by: Martin Storsjö <martin@martin.st>

Juan Carlos Rodriguez authored on 2011/05/18 21:00:03
Showing 7 changed files
... ...
@@ -545,7 +545,7 @@ OBJS-$(CONFIG_NUT_MUXER)               += mpegaudiodata.o
545 545
 OBJS-$(CONFIG_OGG_DEMUXER)             += flacdec.o flacdata.o flac.o \
546 546
                                           dirac.o mpeg12data.o
547 547
 OBJS-$(CONFIG_OGG_MUXER)               += xiph.o flacdec.o flacdata.o flac.o
548
-OBJS-$(CONFIG_RTP_MUXER)               += mpegvideo.o xiph.o
548
+OBJS-$(CONFIG_RTP_MUXER)               += mpeg4audio.o mpegvideo.o xiph.o
549 549
 OBJS-$(CONFIG_SPDIF_DEMUXER)           += aacadtsdec.o mpeg4audio.o
550 550
 OBJS-$(CONFIG_WEBM_MUXER)              += xiph.o mpeg4audio.o \
551 551
                                           flacdec.o flacdata.o flac.o \
... ...
@@ -228,6 +228,7 @@ OBJS-$(CONFIG_RSO_MUXER)                 += rsoenc.o rso.o
228 228
 OBJS-$(CONFIG_RPL_DEMUXER)               += rpl.o
229 229
 OBJS-$(CONFIG_RTP_MUXER)                 += rtp.o         \
230 230
                                             rtpenc_aac.o     \
231
+                                            rtpenc_latm.o    \
231 232
                                             rtpenc_amr.o     \
232 233
                                             rtpenc_h263.o    \
233 234
                                             rtpenc_mpv.o     \
... ...
@@ -23,11 +23,25 @@
23 23
 #include "mpegts.h"
24 24
 #include "internal.h"
25 25
 #include "libavutil/random_seed.h"
26
+#include "libavutil/opt.h"
26 27
 
27 28
 #include "rtpenc.h"
28 29
 
29 30
 //#define DEBUG
30 31
 
32
+static const AVOption options[] = {
33
+    { "rtpflags", "RTP muxer flags", offsetof(RTPMuxContext, flags), FF_OPT_TYPE_FLAGS, {.dbl = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "rtpflags" },
34
+    { "latm", "Use MP4A-LATM packetization instead of MPEG4-GENERIC for AAC", 0, FF_OPT_TYPE_CONST, {.dbl = FF_RTP_FLAG_MP4A_LATM}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "rtpflags" },
35
+    { NULL },
36
+};
37
+
38
+static const AVClass rtp_muxer_class = {
39
+    .class_name = "RTP muxer",
40
+    .item_name  = av_default_item_name,
41
+    .option     = options,
42
+    .version    = LIBAVUTIL_VERSION_INT,
43
+};
44
+
31 45
 #define RTCP_SR_SIZE 28
32 46
 
33 47
 static int is_supported(enum CodecID id)
... ...
@@ -404,7 +418,10 @@ static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
404 404
         ff_rtp_send_mpegvideo(s1, pkt->data, size);
405 405
         break;
406 406
     case CODEC_ID_AAC:
407
-        ff_rtp_send_aac(s1, pkt->data, size);
407
+        if (s->flags & FF_RTP_FLAG_MP4A_LATM)
408
+            ff_rtp_send_latm(s1, pkt->data, size);
409
+        else
410
+            ff_rtp_send_aac(s1, pkt->data, size);
408 411
         break;
409 412
     case CODEC_ID_AMR_NB:
410 413
     case CODEC_ID_AMR_WB:
... ...
@@ -455,4 +472,5 @@ AVOutputFormat ff_rtp_muxer = {
455 455
     rtp_write_header,
456 456
     rtp_write_packet,
457 457
     rtp_write_trailer,
458
+    .priv_class = &rtp_muxer_class,
458 459
 };
... ...
@@ -25,6 +25,7 @@
25 25
 #include "rtp.h"
26 26
 
27 27
 struct RTPMuxContext {
28
+    const AVClass *av_class;
28 29
     AVFormatContext *ic;
29 30
     AVStream *st;
30 31
     int payload_type;
... ...
@@ -56,15 +57,20 @@ struct RTPMuxContext {
56 56
      * (1, 2 or 4)
57 57
      */
58 58
     int nal_length_size;
59
+
60
+    int flags;
59 61
 };
60 62
 
61 63
 typedef struct RTPMuxContext RTPMuxContext;
62 64
 
65
+#define FF_RTP_FLAG_MP4A_LATM 1
66
+
63 67
 void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m);
64 68
 
65 69
 void ff_rtp_send_h264(AVFormatContext *s1, const uint8_t *buf1, int size);
66 70
 void ff_rtp_send_h263(AVFormatContext *s1, const uint8_t *buf1, int size);
67 71
 void ff_rtp_send_aac(AVFormatContext *s1, const uint8_t *buff, int size);
72
+void ff_rtp_send_latm(AVFormatContext *s1, const uint8_t *buff, int size);
68 73
 void ff_rtp_send_amr(AVFormatContext *s1, const uint8_t *buff, int size);
69 74
 void ff_rtp_send_mpegvideo(AVFormatContext *s1, const uint8_t *buf1, int size);
70 75
 void ff_rtp_send_xiph(AVFormatContext *s1, const uint8_t *buff, int size);
71 76
new file mode 100644
... ...
@@ -0,0 +1,61 @@
0
+/*
1
+ * RTP Packetization of MPEG-4 Audio (RFC 3016)
2
+ * Copyright (c) 2011 Juan Carlos Rodriguez <ing.juancarlosrodriguez@hotmail.com>
3
+ *
4
+ * This file is part of Libav.
5
+ *
6
+ * Libav is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "avformat.h"
22
+#include "rtpenc.h"
23
+
24
+void ff_rtp_send_latm(AVFormatContext *s1, const uint8_t *buff, int size)
25
+{
26
+    /* MP4A-LATM
27
+     * The RTP payload format specification is described in RFC 3016
28
+     * The encoding specifications are provided in ISO/IEC 14496-3 */
29
+
30
+    RTPMuxContext *s = s1->priv_data;
31
+    int header_size;
32
+    int offset = 0;
33
+    int len    = 0;
34
+
35
+    /* skip ADTS header, if present */
36
+    if ((s1->streams[0]->codec->extradata_size) == 0) {
37
+        size -= 7;
38
+        buff += 7;
39
+    }
40
+
41
+    /* PayloadLengthInfo() */
42
+    header_size = size/0xFF + 1;
43
+    memset(s->buf, 0xFF, header_size - 1);
44
+    s->buf[header_size - 1] = size % 0xFF;
45
+
46
+    s->timestamp = s->cur_timestamp;
47
+
48
+    /* PayloadMux() */
49
+    while (size > 0) {
50
+        len   = FFMIN(size, s->max_payload_size - (!offset ? header_size : 0));
51
+        size -= len;
52
+        if (!offset) {
53
+            memcpy(s->buf + header_size, buff, len);
54
+            ff_rtp_send_data(s1, s->buf, header_size + len, !size);
55
+        } else {
56
+            ff_rtp_send_data(s1, buff + offset, len, !size);
57
+        }
58
+        offset += len;
59
+    }
60
+}
... ...
@@ -23,7 +23,9 @@
23 23
 #include "libavutil/base64.h"
24 24
 #include "libavutil/dict.h"
25 25
 #include "libavutil/parseutils.h"
26
+#include "libavutil/opt.h"
26 27
 #include "libavcodec/xiph.h"
28
+#include "libavcodec/mpeg4audio.h"
27 29
 #include "avformat.h"
28 30
 #include "internal.h"
29 31
 #include "avc.h"
... ...
@@ -300,6 +302,71 @@ xiph_fail:
300 300
     return NULL;
301 301
 }
302 302
 
303
+static int latm_context2profilelevel(AVCodecContext *c)
304
+{
305
+    /* MP4A-LATM
306
+     * The RTP payload format specification is described in RFC 3016
307
+     * The encoding specifications are provided in ISO/IEC 14496-3 */
308
+
309
+    int profile_level = 0x2B;
310
+
311
+    /* TODO: AAC Profile only supports AAC LC Object Type.
312
+     * Different Object Types should implement different Profile Levels */
313
+
314
+    if (c->sample_rate <= 24000) {
315
+        if (c->channels <= 2)
316
+            profile_level = 0x28; // AAC Profile, Level 1
317
+    } else if (c->sample_rate <= 48000) {
318
+        if (c->channels <= 2) {
319
+            profile_level = 0x29; // AAC Profile, Level 2
320
+        } else if (c->channels <= 5) {
321
+            profile_level = 0x2A; // AAC Profile, Level 4
322
+        }
323
+    } else if (c->sample_rate <= 96000) {
324
+        if (c->channels <= 5) {
325
+            profile_level = 0x2B; // AAC Profile, Level 5
326
+        }
327
+    }
328
+
329
+    return profile_level;
330
+}
331
+
332
+static char *latm_context2config(AVCodecContext *c)
333
+{
334
+    /* MP4A-LATM
335
+     * The RTP payload format specification is described in RFC 3016
336
+     * The encoding specifications are provided in ISO/IEC 14496-3 */
337
+
338
+    uint8_t config_byte[6];
339
+    int rate_index;
340
+    char *config;
341
+
342
+    for (rate_index = 0; rate_index < 16; rate_index++)
343
+        if (ff_mpeg4audio_sample_rates[rate_index] == c->sample_rate)
344
+            break;
345
+    if (rate_index == 16) {
346
+        av_log(c, AV_LOG_ERROR, "Unsupported sample rate\n");
347
+        return NULL;
348
+    }
349
+
350
+    config_byte[0] = 0x40;
351
+    config_byte[1] = 0;
352
+    config_byte[2] = 0x20 | rate_index;
353
+    config_byte[3] = c->channels << 4;
354
+    config_byte[4] = 0x3f;
355
+    config_byte[5] = 0xc0;
356
+
357
+    config = av_malloc(6*2+1);
358
+    if (!config) {
359
+        av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the config info.\n");
360
+        return NULL;
361
+    }
362
+    ff_data_to_hex(config, config_byte, 6, 1);
363
+    config[12] = 0;
364
+
365
+    return config;
366
+}
367
+
303 368
 static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c, int payload_type, AVFormatContext *fmt)
304 369
 {
305 370
     char *config = NULL;
... ...
@@ -335,6 +402,16 @@ static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c,
335 335
                                      payload_type, config ? config : "");
336 336
             break;
337 337
         case CODEC_ID_AAC:
338
+            if (fmt && fmt->oformat->priv_class &&
339
+                av_opt_flag_is_set(fmt->priv_data, "rtpflags", "latm")) {
340
+                config = latm_context2config(c);
341
+                if (!config)
342
+                    return NULL;
343
+                av_strlcatf(buff, size, "a=rtpmap:%d MP4A-LATM/%d/%d\r\n"
344
+                                        "a=fmtp:%d profile-level-id=%d;cpresent=0;config=%s\r\n",
345
+                                         payload_type, c->sample_rate, c->channels,
346
+                                         payload_type, latm_context2profilelevel(c), config);
347
+            } else {
338 348
             if (c->extradata_size) {
339 349
                 config = extradata2config(c);
340 350
             } else {
... ...
@@ -353,6 +430,7 @@ static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c,
353 353
                                     "indexdeltalength=3%s\r\n",
354 354
                                      payload_type, c->sample_rate, c->channels,
355 355
                                      payload_type, config);
356
+            }
356 357
             break;
357 358
         case CODEC_ID_PCM_S16BE:
358 359
             if (payload_type >= RTP_PT_PRIVATE)
... ...
@@ -25,7 +25,7 @@
25 25
 
26 26
 #define LIBAVFORMAT_VERSION_MAJOR 53
27 27
 #define LIBAVFORMAT_VERSION_MINOR  1
28
-#define LIBAVFORMAT_VERSION_MICRO  0
28
+#define LIBAVFORMAT_VERSION_MICRO  1
29 29
 
30 30
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
31 31
                                                LIBAVFORMAT_VERSION_MINOR, \