[libav-devel] [PATCH 07/57] ac3/eac3: implement new audio decoding API

Justin Ruggles justin.ruggles at gmail.com
Sat Nov 26 23:06:18 CET 2011


---
 libavcodec/ac3dec.c |   32 ++++++++++++++++++++++++--------
 libavcodec/ac3dec.h |    1 +
 2 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c
index 8e216c0..7e11cf4 100644
--- a/libavcodec/ac3dec.c
+++ b/libavcodec/ac3dec.c
@@ -208,6 +208,9 @@ static av_cold int ac3_decode_init(AVCodecContext *avctx)
     }
     s->downmixed = 1;
 
+    avcodec_get_frame_defaults(&s->frame);
+    avctx->coded_frame = &s->frame;
+
     return 0;
 }
 
@@ -1296,15 +1299,15 @@ static int decode_audio_block(AC3DecodeContext *s, int blk)
 /**
  * Decode a single AC-3 frame.
  */
-static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size,
-                            AVPacket *avpkt)
+static int ac3_decode_frame(AVCodecContext * avctx, void *data,
+                            int *got_frame_ptr, AVPacket *avpkt)
 {
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
     AC3DecodeContext *s = avctx->priv_data;
-    float   *out_samples_flt = data;
-    int16_t *out_samples_s16 = data;
-    int blk, ch, err;
+    float   *out_samples_flt;
+    int16_t *out_samples_s16;
+    int blk, ch, err, ret;
     const uint8_t *channel_map;
     const float *output[AC3_MAX_CHANNELS];
 
@@ -1321,7 +1324,6 @@ static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size,
     init_get_bits(&s->gbc, buf, buf_size * 8);
 
     /* parse the syncinfo */
-    *data_size = 0;
     err = parse_frame_header(s);
 
     if (err) {
@@ -1343,6 +1345,7 @@ static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size,
                 /* TODO: add support for substreams and dependent frames */
                 if(s->frame_type == EAC3_FRAME_TYPE_DEPENDENT || s->substreamid) {
                     av_log(avctx, AV_LOG_ERROR, "unsupported frame type : skipping frame\n");
+                    *got_frame_ptr = 0;
                     return s->frame_size;
                 } else {
                     av_log(avctx, AV_LOG_ERROR, "invalid frame type\n");
@@ -1400,6 +1403,15 @@ static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size,
     if (s->bitstream_mode == 0x7 && s->channels > 1)
         avctx->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
 
+    /* get output buffer */
+    s->frame.nb_samples = s->num_blocks * 256;
+    if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        return ret;
+    }
+    out_samples_flt = (float   *)s->frame.data[0];
+    out_samples_s16 = (int16_t *)s->frame.data[0];
+
     /* decode the audio blocks */
     channel_map = ff_ac3_dec_channel_map[s->output_mode & ~AC3_OUTPUT_LFEON][s->lfe_on];
     for (ch = 0; ch < s->out_channels; ch++)
@@ -1419,8 +1431,10 @@ static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size,
             out_samples_s16 += 256 * s->out_channels;
         }
     }
-    *data_size = s->num_blocks * 256 * avctx->channels *
-                 av_get_bytes_per_sample(avctx->sample_fmt);
+
+    *got_frame_ptr   = 1;
+    *(AVFrame *)data = s->frame;
+
     return FFMIN(buf_size, s->frame_size);
 }
 
@@ -1458,6 +1472,7 @@ AVCodec ff_ac3_decoder = {
     .init = ac3_decode_init,
     .close = ac3_decode_end,
     .decode = ac3_decode_frame,
+    .capabilities = CODEC_CAP_DR1,
     .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52A (AC-3)"),
     .sample_fmts = (const enum AVSampleFormat[]) {
         AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
@@ -1480,6 +1495,7 @@ AVCodec ff_eac3_decoder = {
     .init = ac3_decode_init,
     .close = ac3_decode_end,
     .decode = ac3_decode_frame,
+    .capabilities = CODEC_CAP_DR1,
     .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52B (AC-3, E-AC-3)"),
     .sample_fmts = (const enum AVSampleFormat[]) {
         AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
diff --git a/libavcodec/ac3dec.h b/libavcodec/ac3dec.h
index 3826251..56c6553 100644
--- a/libavcodec/ac3dec.h
+++ b/libavcodec/ac3dec.h
@@ -68,6 +68,7 @@
 typedef struct {
     AVClass        *class;                  ///< class for AVOptions
     AVCodecContext *avctx;                  ///< parent context
+    AVFrame frame;                          ///< AVFrame for decoded output
     GetBitContext gbc;                      ///< bitstream reader
 
 ///@name Bit stream information
-- 
1.7.1



More information about the libav-devel mailing list