[2/5] mpeg4audio: Add helpers for outputting LATM AAC

Message ID 1305893663-56629-3-git-send-email-martin@martin.st
State Superseded
Headers show

Commit Message

Martin Storsjö May 20, 2011, 12:14 p.m.
From: Juan Carlos Rodriguez <ing.juancarlosrodriguez@hotmail.com>

---
 libavcodec/mpeg4audio.c |   44 ++++++++++++++++++++++++++++++++++++++++++++
 libavcodec/mpeg4audio.h |    5 +++++
 2 files changed, 49 insertions(+), 0 deletions(-)

Patch

diff --git a/libavcodec/mpeg4audio.c b/libavcodec/mpeg4audio.c
index f0399af..48033aa 100644
--- a/libavcodec/mpeg4audio.c
+++ b/libavcodec/mpeg4audio.c
@@ -181,3 +181,47 @@  int ff_copy_pce_data(PutBitContext *pb, GetBitContext *gb)
 
     return put_bits_count(pb) - offset;
 }
+
+int ff_aac_make_latm_header(AVCodecContext *ctx)
+{
+    int rate_index;
+
+    for (rate_index = 0; rate_index < 16; rate_index++)
+        if (ff_mpeg4audio_sample_rates[rate_index] == ctx->sample_rate)
+            break;
+    if (rate_index == 16) {
+        av_log(ctx, AV_LOG_ERROR, "Unsupported sample rate\n");
+        return AVERROR(ENOSYS);
+    }
+
+    av_freep(&ctx->extradata);
+    ctx->extradata_size = 6;
+    ctx->extradata = av_mallocz(ctx->extradata_size +
+                                FF_INPUT_BUFFER_PADDING_SIZE);
+    if (!ctx->extradata)
+        return AVERROR(ENOMEM);
+
+    ctx->extradata[0] = 0x40;
+    ctx->extradata[1] = 0;
+    ctx->extradata[2] = 0x20 | rate_index;
+    ctx->extradata[3] = ctx->channels << 4;
+    ctx->extradata[4] = 0x3f;
+    ctx->extradata[5] = 0xc0;
+    return 0;
+}
+
+int ff_aac_latm_wrap(AVCodecContext *avctx, uint8_t *buffer, int length,
+                     int buf_size)
+{
+    int header_size = length/0xff + 1;
+    if (length <= 0)
+        return length;
+    if (header_size + length > buf_size) {
+        av_log(avctx, AV_LOG_ERROR, "Output buffer not large enough for LATM header\n");
+        return AVERROR(EINVAL);
+    }
+    memmove(buffer + header_size, buffer, length);
+    memset(buffer, 0xff, header_size - 1);
+    buffer[header_size - 1] = length % 0xff;
+    return length + header_size;
+}
diff --git a/libavcodec/mpeg4audio.h b/libavcodec/mpeg4audio.h
index 2d2b4a2..21341ac 100644
--- a/libavcodec/mpeg4audio.h
+++ b/libavcodec/mpeg4audio.h
@@ -23,6 +23,7 @@ 
 #define AVCODEC_MPEG4AUDIO_H
 
 #include <stdint.h>
+#include "avcodec.h"
 #include "get_bits.h"
 #include "put_bits.h"
 
@@ -103,4 +104,8 @@  enum AudioObjectType {
 
 int ff_copy_pce_data(PutBitContext *pb, GetBitContext *gb);
 
+int ff_aac_make_latm_header(AVCodecContext *ctx);
+int ff_aac_latm_wrap(AVCodecContext *ctx, uint8_t *buffer, int length,
+                     int buf_size);
+
 #endif /* AVCODEC_MPEG4AUDIO_H */