Browse code

rtpenc: MP4A-LATM payload support

Juan Carlos Rodriguez authored on 2011/05/18 22:21:48
Showing 7 changed files
... ...
@@ -233,6 +233,7 @@ OBJS-$(CONFIG_RSO_MUXER)                 += rsoenc.o rso.o
233 233
 OBJS-$(CONFIG_RPL_DEMUXER)               += rpl.o
234 234
 OBJS-$(CONFIG_RTP_MUXER)                 += rtp.o         \
235 235
                                             rtpenc_aac.o     \
236
+                                            rtpenc_latm.o    \
236 237
                                             rtpenc_amr.o     \
237 238
                                             rtpenc_h263.o    \
238 239
                                             rtpenc_mpv.o     \
... ...
@@ -729,6 +729,7 @@ typedef struct AVFormatContext {
729 729
 #define AVFMT_FLAG_NOFILLIN     0x0010 ///< Do not infer any values from other values, just return what is stored in the container
730 730
 #define AVFMT_FLAG_NOPARSE      0x0020 ///< Do not use AVParsers, you also must set AVFMT_FLAG_NOFILLIN as the fillin code works on frames and no parsing -> no frames. Also seeking to frames can not work if parsing to find frame boundaries has been disabled
731 731
 #define AVFMT_FLAG_RTP_HINT     0x0040 ///< Add RTP hinting to the output file
732
+#define AVFMT_FLAG_MP4A_LATM    0x0080 ///< Enable RTP MP4A-LATM payload
732 733
 #define AVFMT_FLAG_SORT_DTS    0x10000 ///< try to interleave outputted packets by dts (using this flag can slow demuxing down)
733 734
 #define AVFMT_FLAG_PRIV_OPT    0x20000 ///< Enable use of private options by delaying codec open (this could be made default once all code is converted)
734 735
 
... ...
@@ -51,6 +51,7 @@ static const AVOption options[]={
51 51
 {"igndts", "ignore dts", 0, FF_OPT_TYPE_CONST, {.dbl = AVFMT_FLAG_IGNDTS }, INT_MIN, INT_MAX, D, "fflags"},
52 52
 {"rtphint", "add rtp hinting", 0, FF_OPT_TYPE_CONST, {.dbl = AVFMT_FLAG_RTP_HINT }, INT_MIN, INT_MAX, E, "fflags"},
53 53
 {"sortdts", "try to interleave outputted packets by dts", 0, FF_OPT_TYPE_CONST, {.dbl = AVFMT_FLAG_SORT_DTS }, INT_MIN, INT_MAX, D, "fflags"},
54
+{"latm", "enable RTP MP4A-LATM payload", 0, FF_OPT_TYPE_CONST, {.dbl = AVFMT_FLAG_MP4A_LATM }, INT_MIN, INT_MAX, E, "fflags"},
54 55
 {"analyzeduration", "how many microseconds are analyzed to estimate duration", OFFSET(max_analyze_duration), FF_OPT_TYPE_INT, {.dbl = 5*AV_TIME_BASE }, 0, INT_MAX, D},
55 56
 {"cryptokey", "decryption key", OFFSET(key), FF_OPT_TYPE_BINARY, {.dbl = 0}, 0, 0, D},
56 57
 {"indexmem", "max memory used for timestamp index (per stream)", OFFSET(max_index_size), FF_OPT_TYPE_INT, {.dbl = 1<<20 }, 0, INT_MAX, D},
... ...
@@ -404,7 +404,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 (s1->flags & AVFMT_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:
... ...
@@ -65,6 +65,7 @@ void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m);
65 65
 void ff_rtp_send_h264(AVFormatContext *s1, const uint8_t *buf1, int size);
66 66
 void ff_rtp_send_h263(AVFormatContext *s1, const uint8_t *buf1, int size);
67 67
 void ff_rtp_send_aac(AVFormatContext *s1, const uint8_t *buff, int size);
68
+void ff_rtp_send_latm(AVFormatContext *s1, const uint8_t *buff, int size);
68 69
 void ff_rtp_send_amr(AVFormatContext *s1, const uint8_t *buff, int size);
69 70
 void ff_rtp_send_mpegvideo(AVFormatContext *s1, const uint8_t *buf1, int size);
70 71
 void ff_rtp_send_xiph(AVFormatContext *s1, const uint8_t *buff, int size);
71 72
new file mode 100644
... ...
@@ -0,0 +1,60 @@
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 FFmpeg.
5
+ *
6
+ * FFmpeg 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
+    /* MP4A-LATM
26
+     * The RTP payload format specification is described in RFC 3016
27
+     * The encoding specifications are provided in ISO/IEC 14496-3 */
28
+
29
+    RTPMuxContext *s = s1->priv_data;
30
+    int header_size;
31
+    int offset = 0;
32
+    int len    = 0;
33
+
34
+    /* skip ADTS header, if present */
35
+    if ((s1->streams[0]->codec->extradata_size) == 0) {
36
+        size -= 7;
37
+        buff += 7;
38
+    }
39
+
40
+    /* PayloadLengthInfo() */
41
+    header_size = size/0xFF + 1;
42
+    memset(s->buf, 0xFF, header_size - 1);
43
+    s->buf[header_size - 1] = size % 0xFF;
44
+
45
+    s->timestamp = s->cur_timestamp;
46
+
47
+    /* PayloadMux() */
48
+    while (size > 0) {
49
+        len   = FFMIN(size, s->max_payload_size - (!offset ? header_size : 0));
50
+        size -= len;
51
+        if (!offset) {
52
+            memcpy(s->buf + header_size, buff, len);
53
+            ff_rtp_send_data(s1, s->buf, header_size + len, !size);
54
+        } else {
55
+            ff_rtp_send_data(s1, buff + offset, len, !size);
56
+        }
57
+        offset += len;
58
+    }
59
+}
... ...
@@ -23,6 +23,7 @@
23 23
 #include "libavutil/base64.h"
24 24
 #include "libavutil/parseutils.h"
25 25
 #include "libavcodec/xiph.h"
26
+#include "libavcodec/mpeg4audio.h"
26 27
 #include "avformat.h"
27 28
 #include "internal.h"
28 29
 #include "avc.h"
... ...
@@ -299,6 +300,69 @@ xiph_fail:
299 299
     return NULL;
300 300
 }
301 301
 
302
+static int latm_context2profilelevel(AVCodecContext *c) {
303
+    /* MP4A-LATM
304
+     * The RTP payload format specification is described in RFC 3016
305
+     * The encoding specifications are provided in ISO/IEC 14496-3 */
306
+
307
+    int profile_level = 0x2B;
308
+
309
+    /* TODO: AAC Profile only supports AAC LC Object Type.
310
+     * Different Object Types should implement different Profile Levels */
311
+
312
+    if (c->sample_rate <= 24000) {
313
+        if (c->channels <= 2)
314
+            profile_level = 0x28; // AAC Profile, Level 1
315
+    } else if (c->sample_rate <= 48000) {
316
+        if (c->channels <= 2) {
317
+            profile_level = 0x29; // AAC Profile, Level 2
318
+        } else if (c->channels <= 5) {
319
+            profile_level = 0x2A; // AAC Profile, Level 4
320
+        }
321
+    } else if (c->sample_rate <= 96000) {
322
+        if (c->channels <= 5) {
323
+            profile_level = 0x2B; // AAC Profile, Level 5
324
+        }
325
+    }
326
+
327
+    return profile_level;
328
+}
329
+
330
+static char *latm_context2config(AVCodecContext *c) {
331
+    /* MP4A-LATM
332
+     * The RTP payload format specification is described in RFC 3016
333
+     * The encoding specifications are provided in ISO/IEC 14496-3 */
334
+
335
+    uint8_t config_byte[6];
336
+    int rate_index;
337
+    char *config;
338
+
339
+    for (rate_index = 0; rate_index < 16; rate_index++)
340
+        if (ff_mpeg4audio_sample_rates[rate_index] == c->sample_rate)
341
+            break;
342
+    if (rate_index == 16) {
343
+        av_log(c, AV_LOG_ERROR, "Unsupported sample rate\n");
344
+        return NULL;
345
+    }
346
+
347
+    config_byte[0] = 0x40;
348
+    config_byte[1] = 0;
349
+    config_byte[2] = 0x20 | rate_index;
350
+    config_byte[3] = c->channels << 4;
351
+    config_byte[4] = 0x3f;
352
+    config_byte[5] = 0xc0;
353
+
354
+    config = av_malloc(6*2+1);
355
+    if (!config) {
356
+        av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the config info.\n");
357
+        return NULL;
358
+    }
359
+    ff_data_to_hex(config, config_byte, 6, 1);
360
+    config[12] = 0;
361
+
362
+    return config;
363
+}
364
+
302 365
 static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c, int payload_type, int flags)
303 366
 {
304 367
     char *config = NULL;
... ...
@@ -334,6 +398,15 @@ static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c,
334 334
                                      payload_type, config ? config : "");
335 335
             break;
336 336
         case CODEC_ID_AAC:
337
+            if (flags & AVFMT_FLAG_MP4A_LATM) {
338
+                config = latm_context2config(c);
339
+                if (!config)
340
+                    return NULL;
341
+                av_strlcatf(buff, size, "a=rtpmap:%d MP4A-LATM/%d/%d\r\n"
342
+                                        "a=fmtp:%d profile-level-id=%d;cpresent=0;config=%s\r\n",
343
+                                         payload_type, c->sample_rate, c->channels,
344
+                                         payload_type, latm_context2profilelevel(c), config);
345
+            } else {
337 346
             if (c->extradata_size) {
338 347
                 config = extradata2config(c);
339 348
             } else {
... ...
@@ -352,6 +425,7 @@ static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c,
352 352
                                     "indexdeltalength=3%s\r\n",
353 353
                                      payload_type, c->sample_rate, c->channels,
354 354
                                      payload_type, config);
355
+            }
355 356
             break;
356 357
         case CODEC_ID_PCM_S16BE:
357 358
             if (payload_type >= RTP_PT_PRIVATE)