[FFmpeg-devel] [PATCH 2/3] Add single stream LATM/LOAS decoder

Janne Grunau janne-ffmpeg
Mon Oct 18 19:37:14 CEST 2010


The decoder is basicly just a wrapper around the AAC decoder.
based on patch by Paul Kendall { paul <?t> kcbbs gen nz }
---
 Changelog                |    1 +
 configure                |    1 +
 libavcodec/Makefile      |    2 +
 libavcodec/aacdec.c      |    9 +-
 libavcodec/aacdec.h      |    3 +
 libavcodec/aaclatmdec.c  |  419 ++++++++++++++++++++++++++++++++++++++++++++++
 libavcodec/allcodecs.c   |    2 +
 libavcodec/avcodec.h     |    3 +-
 libavcodec/latm_parser.c |  119 +++++++++++++
 9 files changed, 553 insertions(+), 6 deletions(-)
 create mode 100644 libavcodec/aaclatmdec.c
 create mode 100644 libavcodec/latm_parser.c

diff --git a/Changelog b/Changelog
index 426a92d..b6ada6b 100644
--- a/Changelog
+++ b/Changelog
@@ -47,6 +47,7 @@ version <next>:
 - SAP (Session Announcement Protocol, RFC 2974) muxer and demuxer
 - cropdetect filter
 - ffmpeg -crop* options removed
+- single stream LATM/LOAS decoder
 
 
 version 0.6:
diff --git a/configure b/configure
index 0e6e439..f3e65d4 100755
--- a/configure
+++ b/configure
@@ -1187,6 +1187,7 @@ rdft_select="fft"
 # decoders / encoders / hardware accelerators
 aac_decoder_select="mdct rdft"
 aac_encoder_select="mdct"
+aac_latm_decoder_select="aac_decoder"
 ac3_decoder_select="mdct ac3_parser"
 alac_encoder_select="lpc"
 amrnb_decoder_select="lsp"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 385ae02..bd5f041 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -52,6 +52,7 @@ OBJS-$(CONFIG_AAC_ENCODER)             += aacenc.o aaccoder.o    \
                                           aacpsy.o aactab.o      \
                                           psymodel.o iirfilter.o \
                                           mpeg4audio.o
+OBJS-$(CONFIG_AAC_LATM_DECODER)        += aaclatmdec.o
 OBJS-$(CONFIG_AASC_DECODER)            += aasc.o msrledec.o
 OBJS-$(CONFIG_AC3_DECODER)             += ac3dec.o ac3dec_data.o ac3.o
 OBJS-$(CONFIG_AC3_ENCODER)             += ac3enc.o ac3tab.o ac3.o
@@ -576,6 +577,7 @@ OBJS-$(CONFIG_H264_PARSER)             += h264_parser.o h264.o            \
                                           h264_loopfilter.o h264_cabac.o \
                                           h264_cavlc.o h264_ps.o \
                                           mpegvideo.o error_resilience.o
+OBJS-$(CONFIG_AAC_LATM_PARSER)         += latm_parser.o
 OBJS-$(CONFIG_MJPEG_PARSER)            += mjpeg_parser.o
 OBJS-$(CONFIG_MLP_PARSER)              += mlp_parser.o mlp.o
 OBJS-$(CONFIG_MPEG4VIDEO_PARSER)       += mpeg4video_parser.o h263.o \
diff --git a/libavcodec/aacdec.c b/libavcodec/aacdec.c
index 4306483..b2cf7b2 100644
--- a/libavcodec/aacdec.c
+++ b/libavcodec/aacdec.c
@@ -516,7 +516,7 @@ static void reset_predictor_group(PredictorState *ps, int group_num)
         ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), sizeof(ff_aac_spectral_codes[num][0]), \
         size);
 
-static av_cold int aac_decode_init(AVCodecContext *avctx)
+av_cold int ff_aac_decode_init(AVCodecContext *avctx)
 {
     AACContext *ac = avctx->priv_data;
 
@@ -2068,8 +2068,7 @@ static int aac_decode_frame(AVCodecContext *avctx, void *data,
 
     return buf_size > buf_offset ? buf_consumed : buf_size;
 }
-
-static av_cold int aac_decode_close(AVCodecContext *avctx)
+av_cold int ff_aac_decode_close(AVCodecContext *avctx)
 {
     AACContext *ac = avctx->priv_data;
     int i, type;
@@ -2092,9 +2091,9 @@ AVCodec aac_decoder = {
     AVMEDIA_TYPE_AUDIO,
     CODEC_ID_AAC,
     sizeof(AACContext),
-    aac_decode_init,
+    ff_aac_decode_init,
     NULL,
-    aac_decode_close,
+    ff_aac_decode_close,
     aac_decode_frame,
     .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
     .sample_fmts = (const enum SampleFormat[]) {
diff --git a/libavcodec/aacdec.h b/libavcodec/aacdec.h
index a16bfa0..fa1f1d0 100644
--- a/libavcodec/aacdec.h
+++ b/libavcodec/aacdec.h
@@ -28,6 +28,9 @@
 #include "avcodec.h"
 #include "get_bits.h"
 
+av_cold int ff_aac_decode_init(AVCodecContext *avctx);
+
+av_cold int ff_aac_decode_close(AVCodecContext *avctx);
 
 int ff_aac_decode_frame_int(AVCodecContext *avctx, void *data,
                             int *data_size, GetBitContext *gb);
diff --git a/libavcodec/aaclatmdec.c b/libavcodec/aaclatmdec.c
new file mode 100644
index 0000000..229e486
--- /dev/null
+++ b/libavcodec/aaclatmdec.c
@@ -0,0 +1,419 @@
+/*
+ * AAC LATM decoder
+ * Copyright (c) 2008-2010 Paul Kendall <paul at kcbbs.gen.nz>
+ * Copyright (c) 2010      Janne Grunau <janne-ffmpeg at jannau.net>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * AAC LATM decoder
+ * @author Paul Kendall <paul at kcbbs.gen.nz>
+ * @author Janne Grunau <janne-ffmpeg at jannau.net>
+ */
+
+/*
+    Note: This decoder filter is intended to decode LATM streams transferred
+    in MPEG transport streams which only contain one program.
+    To do a more complex LATM demuxing a separate LATM demuxer should be used.
+*/
+
+#include "get_bits.h"
+#include "dsputil.h"
+
+#include "aac.h"
+#include "aacdec.h"
+#include "aacdectab.h"
+#include "mpeg4audio.h"
+
+#include "libavutil/avassert.h"
+
+#define LOAS_SYNC_WORD   0x2b7       ///< 11 bits LOAS sync word
+
+struct LATMContext {
+    AACContext      aac_ctx;
+    int             initialized;         // initilized after a valid extradata is set
+
+    // parser data
+    int             audio_mux_version_A; // LATM syntax version
+    int             frame_length_type;   // 0/1 variable/fixed frame length
+    int             frame_length;        // frame length for fixed frame length
+};
+
+static inline uint32_t latm_get_value(GetBitContext *b)
+{
+    int length = get_bits(b, 2);
+    uint32_t value = 0;
+    int i;
+    for (i = 0; i <= length; i++) {
+        value <<= 8;
+        value |= get_bits(b, 8);
+    }
+    return value;
+}
+
+//copied from ff_copy_pce_data() in mpeg4audio.c
+static int parse_pce_data(GetBitContext *gb)
+{
+    int five_bit_ch, four_bit_ch, comment_size, bits;
+    int offset = get_bits_count(gb);
+
+    skip_bits(gb, 10);                  //Tag, Object Type, Frequency
+    five_bit_ch  = get_bits(gb, 4);     //Front
+    five_bit_ch += get_bits(gb, 4);     //Side
+    five_bit_ch += get_bits(gb, 4);     //Back
+    four_bit_ch  = get_bits(gb, 2);     //LFE
+    four_bit_ch += get_bits(gb, 3);     //Data
+    five_bit_ch += get_bits(gb, 4);     //Coupling
+    if (get_bits(gb, 1))                //Mono Mixdown
+        skip_bits(gb, 4);
+    if (get_bits(gb, 1))                //Stereo Mixdown
+        skip_bits(gb, 4);
+    if (get_bits(gb, 1))                //Matrix Mixdown
+        skip_bits(gb, 3);
+    for (bits = five_bit_ch*5+four_bit_ch*4; bits > 16; bits -= 16)
+        skip_bits(gb, 16);
+    if (bits)
+        skip_bits(gb, bits);
+    align_get_bits(gb);
+    comment_size = get_bits(gb, 8);
+    for (; comment_size > 0; comment_size--)
+        get_bits(gb, 8);
+
+    return get_bits_count(gb) - offset;
+}
+
+static void latm_read_ga_specific_config(int audio_object_type,
+                                         MPEG4AudioConfig *c,
+                                         GetBitContext *gb)
+{
+    int ext_flag;
+
+    skip_bits(gb, 1);                             // framelen_flag
+    if (get_bits(gb, 1))                          // depends_on_coder
+        skip_bits(gb, 14);                        // delay
+    ext_flag = get_bits(gb, 1);
+
+    if (!c->chan_config)
+        parse_pce_data(gb);                       // program_config_element
+
+    if (audio_object_type == AOT_AAC_SCALABLE ||
+        audio_object_type == AOT_ER_AAC_SCALABLE)
+        skip_bits(gb, 3);                         // layer number
+
+    if (!ext_flag)
+        return;
+
+    if (audio_object_type == AOT_ER_BSAC) {
+        skip_bits(gb, 5);                         // numOfSubFrame
+        skip_bits(gb, 11);                        // layer_length
+    } else if (audio_object_type == AOT_ER_AAC_LC       ||
+               audio_object_type == AOT_ER_AAC_LTP      ||
+               audio_object_type == AOT_ER_AAC_SCALABLE ||
+               audio_object_type == AOT_ER_AAC_LD)
+        skip_bits(gb, 3);                         // stuff
+    skip_bits(gb, 1);                             // extflag3
+}
+
+static int latm_read_audio_specific_config(GetBitContext *gb)
+{
+    int num_bits = get_bits_count(gb);
+    int audio_object_type;
+
+    MPEG4AudioConfig b, *c;
+    c = &b;
+
+    c->sbr = -1;
+
+    audio_object_type = get_bits(gb, 5);
+    if (audio_object_type == AOT_ESCAPE) {
+        audio_object_type = AOT_ESCAPE + get_bits(gb, 6) + 1;
+    }
+    c->object_type = audio_object_type;
+
+    c->sampling_index = get_bits(gb, 4);
+    c->sample_rate    = ff_mpeg4audio_sample_rates[c->sampling_index];
+    if (c->sampling_index == 0x0f) {
+        c->sample_rate = get_bits(gb, 24);
+    }
+    c->chan_config = get_bits(gb, 4);
+
+    if (c->chan_config < FF_ARRAY_ELEMS(ff_mpeg4audio_channels))
+        c->channels = ff_mpeg4audio_channels[c->chan_config];
+
+    if (audio_object_type == AOT_AAC_MAIN     ||
+        audio_object_type == AOT_AAC_LC       ||
+        audio_object_type == AOT_AAC_SSR      ||
+        audio_object_type == AOT_AAC_LTP      ||
+        audio_object_type == AOT_AAC_SCALABLE ||
+        audio_object_type == AOT_TWINVQ) {
+        latm_read_ga_specific_config(audio_object_type, c, gb);
+    } else if (audio_object_type == AOT_SBR) {
+        c->sbr                = 1;
+        c->ext_sampling_index = get_bits(gb, 4);
+        c->ext_sample_rate    = ff_mpeg4audio_sample_rates[c->ext_sampling_index];
+        if (c->ext_sampling_index == 0x0f) {
+            c->ext_sample_rate = get_bits(gb, 24);
+        }
+        c->object_type = get_bits(gb, 5);
+    } else if (audio_object_type >= AOT_ER_AAC_LC) {
+        latm_read_ga_specific_config(audio_object_type, c, gb);
+        skip_bits(gb, 2);                         // epConfig
+    }
+
+    if (c->sbr == -1 && c->sample_rate <= 24000)
+        c->sample_rate *= 2;
+
+    // count the extradata
+    return get_bits_count(gb) - num_bits;
+}
+
+static int latm_decode_audio_specific_config(struct LATMContext *latmctx,
+                                             GetBitContext *gb)
+{
+    AVCodecContext *avctx = latmctx->aac_ctx.avctx;
+    int  config_start_bit = get_bits_count(gb);
+    int     bits_consumed = latm_read_audio_specific_config(gb);
+    int     esize;
+
+    if (bits_consumed < 0)
+        return AVERROR_INVALIDDATA;
+
+    esize = (bits_consumed+7) / 8;
+
+    if (avctx->extradata_size != esize) {
+        av_free(avctx->extradata);
+        avctx->extradata = av_malloc(esize + FF_INPUT_BUFFER_PADDING_SIZE);
+        if (!avctx->extradata)
+            return AVERROR(ENOMEM);
+
+        avctx->extradata_size = esize;
+        if (config_start_bit % 8) {
+            av_log_missing_feature(latmctx->aac_ctx.avctx, "audio specific "
+                                   "config not byte aligned.\n", 1);
+            return AVERROR_INVALIDDATA;
+        } else {
+            memcpy(avctx->extradata, gb->buffer + (config_start_bit/8), esize);
+            memset(avctx->extradata+esize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
+        }
+    }
+
+    return bits_consumed;
+}
+
+static int read_stream_mux_config(struct LATMContext *latmctx,
+                                  GetBitContext *gb)
+{
+    int ret, audio_mux_version = get_bits(gb, 1);
+
+    latmctx->audio_mux_version_A = 0;
+    if (audio_mux_version)
+        latmctx->audio_mux_version_A = get_bits(gb, 1);
+
+    if (!latmctx->audio_mux_version_A) {
+
+        if (audio_mux_version)
+            latm_get_value(gb);                 // taraFullness
+
+        skip_bits(gb, 1);                       // allStreamSameTimeFraming
+        skip_bits(gb, 6);                       // numSubFrames
+        // numPrograms
+        if (get_bits(gb, 4)) {                  // numPrograms
+            av_log_missing_feature(latmctx->aac_ctx.avctx,
+                                   "multiple programs are not supported\n", 1);
+            return AVERROR_PATCHWELCOME;
+        }
+
+        // for each program (which there is only on in DVB)
+
+        // for each layer (which there is only on in DVB)
+        if (get_bits(gb, 3)) {                   // numLayer
+            av_log_missing_feature(latmctx->aac_ctx.avctx,
+                                   "multiple layers are not supported\n", 1);
+            return AVERROR_PATCHWELCOME;
+        }
+
+        // for all but first stream: use_same_config = get_bits(gb, 1);
+        if (!audio_mux_version) {
+            ret = latm_decode_audio_specific_config(latmctx, gb);
+            if (ret < 0)
+                return ret;
+        } else {
+            int ascLen = latm_get_value(gb);
+            ret = latm_decode_audio_specific_config(latmctx, gb);
+            if (ret < 0)
+                return ret;
+            ascLen -= ret;
+            skip_bits_long(gb, ascLen);
+        }
+
+        latmctx->frame_length_type = get_bits(gb, 3);
+        switch (latmctx->frame_length_type) {
+        case 0:
+            skip_bits(gb, 8);       // latmBufferFullness
+            break;
+        case 1:
+            latmctx->frame_length = get_bits(gb, 9);
+            break;
+        case 3:
+        case 4:
+        case 5:
+            skip_bits(gb, 6);       // CELP frame length table index
+            break;
+        case 6:
+        case 7:
+            skip_bits(gb, 1);       // HVXC frame length table index
+            break;
+        }
+
+        if (get_bits(gb, 1)) {                  // other data
+            if (audio_mux_version) {
+                latm_get_value(gb);             // other_data_bits
+            } else {
+                int esc;
+                do {
+                    esc = get_bits(gb, 1);
+                    skip_bits(gb, 8);
+                } while (esc);
+            }
+        }
+
+        if (get_bits(gb, 1))                     // crc present
+            skip_bits(gb, 8);                    // config_crc
+    }
+
+    return 0;
+}
+
+static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb)
+{
+    uint8_t tmp;
+
+    if (ctx->frame_length_type == 0) {
+        int mux_slot_length = 0;
+        do {
+            tmp = get_bits(gb, 8);
+            mux_slot_length += tmp;
+        } while (tmp == 255);
+        return mux_slot_length;
+    } else if (ctx->frame_length_type == 1) {
+        return ctx->frame_length;
+    } else if (ctx->frame_length_type == 3 ||
+               ctx->frame_length_type == 5 ||
+               ctx->frame_length_type == 7) {
+        skip_bits(gb, 2);          // mux_slot_length_coded
+    }
+    return 0;
+}
+
+static int read_audio_mux_element(struct LATMContext *latmctx,
+                                  GetBitContext *gb)
+{
+    uint8_t use_same_mux = get_bits(gb, 1);
+    if (!use_same_mux) {
+        read_stream_mux_config(latmctx, gb);
+    } else if (!latmctx->aac_ctx.avctx->extradata) {
+        av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG,
+               "no decoder config found\n");
+        return AVERROR(EAGAIN);
+    }
+    if (latmctx->audio_mux_version_A == 0) {
+        int mux_slot_length_bytes = read_payload_length_info(latmctx, gb);
+        if (mux_slot_length_bytes * 8 > get_bits_left(gb)) {
+            av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, "incomplete frame\n");
+            return AVERROR_INVALIDDATA;
+        }
+    }
+    return 0;
+}
+
+
+static int latm_decode_frame(AVCodecContext *avctx, void *out, int *out_size,
+                             AVPacket *avpkt)
+{
+    struct LATMContext *latmctx = avctx->priv_data;
+    int                 muxlength, err;
+    GetBitContext       gb;
+
+    if (avpkt->size == 0)
+        return 0;
+
+    init_get_bits(&gb, avpkt->data, avpkt->size * 8);
+
+    // check for LOAS sync word
+    if (get_bits(&gb, 11) != LOAS_SYNC_WORD)
+        return AVERROR_INVALIDDATA;
+
+    muxlength = get_bits(&gb, 13);
+    // not enough data, the parser should have sorted this
+    if (muxlength+3 > avpkt->size)
+        return AVERROR_INVALIDDATA;
+
+    err = read_audio_mux_element(latmctx, &gb);
+    if (err < 0)
+        return err;
+
+    if (!latmctx->initialized) {
+        if (!avctx->extradata) {
+            *out_size = 0;
+            return avpkt->size;
+        } else {
+            err = ff_aac_decode_init(avctx);
+            if (err < 0)
+                return err;
+            latmctx->initialized = 1;
+        }
+    }
+
+    err = ff_aac_decode_frame_int(avctx, out, out_size, &gb);
+    if (err < 0)
+        return err;
+
+    return muxlength;
+}
+
+av_cold static int latm_decode_init(AVCodecContext *avctx)
+{
+    struct LATMContext *latmctx = avctx->priv_data;
+    int ret;
+
+    ret = ff_aac_decode_init(avctx);
+
+    if (avctx->extradata_size > 0) {
+        latmctx->initialized = !ret;
+    } else {
+        latmctx->initialized = 0;
+    }
+
+    return ret;
+}
+
+AVCodec aac_latm_decoder = {
+    .name = "aac_latm",
+    .type = CODEC_TYPE_AUDIO,
+    .id   = CODEC_ID_AAC_LATM,
+    .priv_data_size = sizeof(struct LATMContext),
+    .init   = latm_decode_init,
+    .close  = ff_aac_decode_close,
+    .decode = latm_decode_frame,
+    .long_name = NULL_IF_CONFIG_SMALL("AAC LATM (Advanced Audio Codec LATM syntax)"),
+    .sample_fmts = (const enum SampleFormat[]) {
+        SAMPLE_FMT_S16,SAMPLE_FMT_NONE
+    },
+    .channel_layouts = aac_channel_layout,
+};
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 89614ab..c3e4647 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -220,6 +220,7 @@ void avcodec_register_all(void)
 
     /* audio codecs */
     REGISTER_ENCDEC  (AAC, aac);
+    REGISTER_DECODER (AAC_LATM, aac_latm);
     REGISTER_ENCDEC  (AC3, ac3);
     REGISTER_ENCDEC  (ALAC, alac);
     REGISTER_DECODER (ALS, als);
@@ -366,6 +367,7 @@ void avcodec_register_all(void)
 
     /* parsers */
     REGISTER_PARSER  (AAC, aac);
+    REGISTER_PARSER  (AAC_LATM, aac_latm);
     REGISTER_PARSER  (AC3, ac3);
     REGISTER_PARSER  (CAVSVIDEO, cavsvideo);
     REGISTER_PARSER  (DCA, dca);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 4bddbaa..5bbebd2 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -31,7 +31,7 @@
 #include "libavutil/cpu.h"
 
 #define LIBAVCODEC_VERSION_MAJOR 52
-#define LIBAVCODEC_VERSION_MINOR 92
+#define LIBAVCODEC_VERSION_MINOR 93
 #define LIBAVCODEC_VERSION_MICRO  0
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
@@ -376,6 +376,7 @@ enum CodecID {
     CODEC_ID_ATRAC1,
     CODEC_ID_BINKAUDIO_RDFT,
     CODEC_ID_BINKAUDIO_DCT,
+    CODEC_ID_AAC_LATM,
 
     /* subtitle codecs */
     CODEC_ID_DVD_SUBTITLE= 0x17000,
diff --git a/libavcodec/latm_parser.c b/libavcodec/latm_parser.c
new file mode 100644
index 0000000..b8b67ef
--- /dev/null
+++ b/libavcodec/latm_parser.c
@@ -0,0 +1,119 @@
+/*
+ * copyright (c) 2008 Paul Kendall <paul at kcbbs.gen.nz>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * AAC LATM parser
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <sys/types.h>
+
+#include "parser.h"
+
+#define LATM_HEADER     0x56e000        // 0x2b7 (11 bits)
+#define LATM_MASK       0xFFE000        // top 11 bits
+#define LATM_SIZE_MASK  0x001FFF        // bottom 13 bits
+
+typedef struct LATMParseContext{
+    ParseContext pc;
+    int count;
+} LATMParseContext;
+
+/**
+ * finds the end of the current frame in the bitstream.
+ * @return the position of the first byte of the next frame, or -1
+ */
+static int latm_find_frame_end(AVCodecParserContext *s1, const uint8_t *buf,
+                               int buf_size)
+{
+    LATMParseContext *s = s1->priv_data;
+    ParseContext *pc    = &s->pc;
+    int pic_found, i;
+    uint32_t state;
+
+    pic_found = pc->frame_start_found;
+    state     = pc->state;
+
+    i = 0;
+    if (!pic_found) {
+        for (i = 0; i < buf_size; i++) {
+            state = (state<<8) | buf[i];
+            if ((state & LATM_MASK) == LATM_HEADER) {
+                i++;
+                s->count  = -i;
+                pic_found = 1;
+                break;
+            }
+        }
+    }
+
+    if (pic_found) {
+        /* EOF considered as end of frame */
+        if (buf_size == 0)
+            return 0;
+        if ((state & LATM_SIZE_MASK) - s->count <= buf_size) {
+            pc->frame_start_found = 0;
+            pc->state             = -1;
+            return (state & LATM_SIZE_MASK) - s->count;
+        }
+    }
+
+    s->count             += buf_size;
+    pc->frame_start_found = pic_found;
+    pc->state             = state;
+
+    return END_NOT_FOUND;
+}
+
+static int latm_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
+                      const uint8_t **poutbuf, int *poutbuf_size,
+                      const uint8_t *buf, int buf_size)
+{
+    LATMParseContext *s = s1->priv_data;
+    ParseContext *pc    = &s->pc;
+    int next;
+
+    if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) {
+        next = buf_size;
+    } else {
+        next = latm_find_frame_end(s1, buf, buf_size);
+
+        if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
+            *poutbuf      = NULL;
+            *poutbuf_size = 0;
+            return buf_size;
+        }
+    }
+    *poutbuf      = buf;
+    *poutbuf_size = buf_size;
+    return next;
+}
+
+AVCodecParser aac_latm_parser = {
+    { CODEC_ID_AAC_LATM },
+    sizeof(LATMParseContext),
+    NULL,
+    latm_parse,
+    ff_parse_close
+};
-- 
1.7.3.1




More information about the ffmpeg-devel mailing list