]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/mlpdec.c
Merge remote branch 'qatar/master'
[ffmpeg] / libavcodec / mlpdec.c
index d211ebd3ac522d790bc1f99fe111b17814fd3963..7b3bd710bbdc5455e323afe85a5995fb464f8837 100644 (file)
@@ -20,7 +20,7 @@
  */
 
 /**
- * @file libavcodec/mlpdec.c
+ * @file
  * MLP decoder
  */
 
@@ -62,6 +62,9 @@ typedef struct SubStream {
     //! For each channel output by the matrix, the output channel to map it to
     uint8_t     ch_assign[MAX_CHANNELS];
 
+    //! Channel coding parameters for channels in the substream
+    ChannelParams channel_params[MAX_CHANNELS];
+
     //! The left shift applied to random noise in 0x31ea substreams.
     uint8_t     noise_shift;
     //! The current seed value for the pseudorandom noise generator(s).
@@ -130,6 +133,9 @@ typedef struct MLPDecodeContext {
     //! Index of the last substream to decode - further substreams are skipped.
     uint8_t     max_decoded_substream;
 
+    //! Stream needs channel reordering to comply with FFmpeg's channel order
+    uint8_t     needs_reordering;
+
     //! number of PCM samples contained in each frame
     int         access_unit_size;
     //! next power of two above the number of samples in each frame
@@ -137,8 +143,6 @@ typedef struct MLPDecodeContext {
 
     SubStream   substream[MAX_SUBSTREAMS];
 
-    ChannelParams channel_params[MAX_CHANNELS];
-
     int         matrix_changed;
     int         filter_changed[MAX_CHANNELS][NUM_FILTERS];
 
@@ -173,8 +177,8 @@ static av_cold void init_static(void)
 static inline int32_t calculate_sign_huff(MLPDecodeContext *m,
                                           unsigned int substr, unsigned int ch)
 {
-    ChannelParams *cp = &m->channel_params[ch];
     SubStream *s = &m->substream[substr];
+    ChannelParams *cp = &s->channel_params[ch];
     int lsb_bits = cp->huff_lsbs - s->quant_step_size[ch];
     int sign_shift = lsb_bits + (cp->codebook ? 2 - cp->codebook : -1);
     int32_t sign_huff_offset = cp->huff_offset;
@@ -202,7 +206,7 @@ static inline int read_huff_channels(MLPDecodeContext *m, GetBitContext *gbp,
             m->bypassed_lsbs[pos + s->blockpos][mat] = get_bits1(gbp);
 
     for (channel = s->min_channel; channel <= s->max_channel; channel++) {
-        ChannelParams *cp = &m->channel_params[channel];
+        ChannelParams *cp = &s->channel_params[channel];
         int codebook = cp->codebook;
         int quant_step_size = s->quant_step_size[channel];
         int lsb_bits = cp->huff_lsbs - quant_step_size;
@@ -317,14 +321,33 @@ static int read_major_sync(MLPDecodeContext *m, GetBitContext *gb)
 
     m->avctx->bits_per_raw_sample = mh.group1_bits;
     if (mh.group1_bits > 16)
-        m->avctx->sample_fmt = SAMPLE_FMT_S32;
+        m->avctx->sample_fmt = AV_SAMPLE_FMT_S32;
     else
-        m->avctx->sample_fmt = SAMPLE_FMT_S16;
+        m->avctx->sample_fmt = AV_SAMPLE_FMT_S16;
 
     m->params_valid = 1;
     for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
         m->substream[substr].restart_seen = 0;
 
+    if (mh.stream_type == 0xbb) {
+        /* MLP stream */
+        m->avctx->channel_layout = ff_mlp_layout[mh.channels_mlp];
+    } else { /* mh.stream_type == 0xba */
+        /* TrueHD stream */
+        if (mh.channels_thd_stream2) {
+            m->avctx->channel_layout = ff_truehd_layout(mh.channels_thd_stream2);
+        } else {
+            m->avctx->channel_layout = ff_truehd_layout(mh.channels_thd_stream1);
+        }
+        if (m->avctx->channels &&
+            av_get_channel_layout_nb_channels(m->avctx->channel_layout) != m->avctx->channels) {
+            m->avctx->channel_layout = 0;
+            av_log_ask_for_sample(m->avctx, "Unknown channel layout.");
+        }
+    }
+
+    m->needs_reordering = mh.channels_mlp >= 18 && mh.channels_mlp <= 20;
+
     return 0;
 }
 
@@ -397,7 +420,7 @@ static int read_restart_header(MLPDecodeContext *m, GetBitContext *gbp,
     if (m->avctx->request_channels > 0
         && s->max_channel + 1 >= m->avctx->request_channels
         && substr < m->max_decoded_substream) {
-        av_log(m->avctx, AV_LOG_INFO,
+        av_log(m->avctx, AV_LOG_DEBUG,
                "Extracting %d channel downmix from substream %d. "
                "Further substreams will be skipped.\n",
                s->max_channel + 1, substr);
@@ -435,6 +458,24 @@ static int read_restart_header(MLPDecodeContext *m, GetBitContext *gbp,
         s->ch_assign[ch_assign] = ch;
     }
 
+    if (m->avctx->codec_id == CODEC_ID_MLP && m->needs_reordering) {
+        if (m->avctx->channel_layout == (AV_CH_LAYOUT_2_2|AV_CH_LOW_FREQUENCY) ||
+            m->avctx->channel_layout == AV_CH_LAYOUT_5POINT0) {
+            int i = s->ch_assign[4];
+            s->ch_assign[4] = s->ch_assign[3];
+            s->ch_assign[3] = s->ch_assign[2];
+            s->ch_assign[2] = i;
+        } else if (m->avctx->channel_layout == AV_CH_LAYOUT_5POINT1) {
+            FFSWAP(int, s->ch_assign[2], s->ch_assign[4]);
+            FFSWAP(int, s->ch_assign[3], s->ch_assign[5]);
+        }
+    }
+    if (m->avctx->codec_id == CODEC_ID_TRUEHD &&
+        m->avctx->channel_layout == AV_CH_LAYOUT_7POINT1) {
+        FFSWAP(int, s->ch_assign[4], s->ch_assign[6]);
+        FFSWAP(int, s->ch_assign[5], s->ch_assign[7]);
+    }
+
     checksum = ff_mlp_restart_checksum(buf, get_bits_count(gbp) - start_count);
 
     if (checksum != get_bits(gbp, 8))
@@ -450,7 +491,7 @@ static int read_restart_header(MLPDecodeContext *m, GetBitContext *gbp,
     memset(s->quant_step_size, 0, sizeof(s->quant_step_size));
 
     for (ch = s->min_channel; ch <= s->max_channel; ch++) {
-        ChannelParams *cp = &m->channel_params[ch];
+        ChannelParams *cp = &s->channel_params[ch];
         cp->filter_params[FIR].order = 0;
         cp->filter_params[IIR].order = 0;
         cp->filter_params[FIR].shift = 0;
@@ -472,9 +513,11 @@ static int read_restart_header(MLPDecodeContext *m, GetBitContext *gbp,
 /** Read parameters for one of the prediction filters. */
 
 static int read_filter_params(MLPDecodeContext *m, GetBitContext *gbp,
-                              unsigned int channel, unsigned int filter)
+                              unsigned int substr, unsigned int channel,
+                              unsigned int filter)
 {
-    FilterParams *fp = &m->channel_params[channel].filter_params[filter];
+    SubStream *s = &m->substream[substr];
+    FilterParams *fp = &s->channel_params[channel].filter_params[filter];
     const int max_order = filter ? MAX_IIR_ORDER : MAX_FIR_ORDER;
     const char fchar = filter ? 'I' : 'F';
     int i, order;
@@ -497,7 +540,7 @@ static int read_filter_params(MLPDecodeContext *m, GetBitContext *gbp,
     fp->order = order;
 
     if (order > 0) {
-        int32_t *fcoeff = m->channel_params[channel].coeff[filter];
+        int32_t *fcoeff = s->channel_params[channel].coeff[filter];
         int coeff_bits, coeff_shift;
 
         fp->shift = get_bits(gbp, 4);
@@ -610,19 +653,19 @@ static int read_matrix_params(MLPDecodeContext *m, unsigned int substr, GetBitCo
 static int read_channel_params(MLPDecodeContext *m, unsigned int substr,
                                GetBitContext *gbp, unsigned int ch)
 {
-    ChannelParams *cp = &m->channel_params[ch];
+    SubStream *s = &m->substream[substr];
+    ChannelParams *cp = &s->channel_params[ch];
     FilterParams *fir = &cp->filter_params[FIR];
     FilterParams *iir = &cp->filter_params[IIR];
-    SubStream *s = &m->substream[substr];
 
     if (s->param_presence_flags & PARAM_FIR)
         if (get_bits1(gbp))
-            if (read_filter_params(m, gbp, ch, FIR) < 0)
+            if (read_filter_params(m, gbp, substr, ch, FIR) < 0)
                 return -1;
 
     if (s->param_presence_flags & PARAM_IIR)
         if (get_bits1(gbp))
-            if (read_filter_params(m, gbp, ch, IIR) < 0)
+            if (read_filter_params(m, gbp, substr, ch, IIR) < 0)
                 return -1;
 
     if (fir->order + iir->order > 8) {
@@ -697,7 +740,7 @@ static int read_decoding_params(MLPDecodeContext *m, GetBitContext *gbp,
     if (s->param_presence_flags & PARAM_QUANTSTEP)
         if (get_bits1(gbp))
             for (ch = 0; ch <= s->max_channel; ch++) {
-                ChannelParams *cp = &m->channel_params[ch];
+                ChannelParams *cp = &s->channel_params[ch];
 
                 s->quant_step_size[ch] = get_bits(gbp, 4);
 
@@ -721,12 +764,12 @@ static void filter_channel(MLPDecodeContext *m, unsigned int substr,
                            unsigned int channel)
 {
     SubStream *s = &m->substream[substr];
-    const int32_t *fircoeff = m->channel_params[channel].coeff[FIR];
+    const int32_t *fircoeff = s->channel_params[channel].coeff[FIR];
     int32_t state_buffer[NUM_FILTERS][MAX_BLOCKSIZE + MAX_FIR_ORDER];
     int32_t *firbuf = state_buffer[FIR] + MAX_BLOCKSIZE;
     int32_t *iirbuf = state_buffer[IIR] + MAX_BLOCKSIZE;
-    FilterParams *fir = &m->channel_params[channel].filter_params[FIR];
-    FilterParams *iir = &m->channel_params[channel].filter_params[IIR];
+    FilterParams *fir = &s->channel_params[channel].filter_params[FIR];
+    FilterParams *iir = &s->channel_params[channel].filter_params[IIR];
     unsigned int filter_shift = fir->shift;
     int32_t mask = MSB_MASK(s->quant_step_size[channel]);
 
@@ -928,7 +971,7 @@ static int output_data_internal(MLPDecodeContext *m, unsigned int substr,
 static int output_data(MLPDecodeContext *m, unsigned int substr,
                        uint8_t *data, unsigned int *data_size)
 {
-    if (m->avctx->sample_fmt == SAMPLE_FMT_S32)
+    if (m->avctx->sample_fmt == AV_SAMPLE_FMT_S32)
         return output_data_internal(m, substr, data, data_size, 1);
     else
         return output_data_internal(m, substr, data, data_size, 0);
@@ -936,8 +979,8 @@ static int output_data(MLPDecodeContext *m, unsigned int substr,
 
 
 /** Read an access unit from the stream.
- *  Returns < 0 on error, 0 if not enough data is present in the input stream
- *  otherwise returns the number of bytes consumed. */
+ *  @return negative on error, 0 if not enough data is present in the input stream,
+ *  otherwise the number of bytes consumed. */
 
 static int read_access_unit(AVCodecContext *avctx, void* data, int *data_size,
                             AVPacket *avpkt)
@@ -959,7 +1002,7 @@ static int read_access_unit(AVCodecContext *avctx, void* data, int *data_size,
 
     length = (AV_RB16(buf) & 0xfff) * 2;
 
-    if (length > buf_size)
+    if (length < 4 || length > buf_size)
         return -1;
 
     init_get_bits(&gb, (buf + 4), (length - 4) * 8);
@@ -1134,10 +1177,9 @@ error:
     return -1;
 }
 
-#if CONFIG_MLP_DECODER
-AVCodec mlp_decoder = {
+AVCodec ff_mlp_decoder = {
     "mlp",
-    CODEC_TYPE_AUDIO,
+    AVMEDIA_TYPE_AUDIO,
     CODEC_ID_MLP,
     sizeof(MLPDecodeContext),
     mlp_decode_init,
@@ -1146,12 +1188,11 @@ AVCodec mlp_decoder = {
     read_access_unit,
     .long_name = NULL_IF_CONFIG_SMALL("MLP (Meridian Lossless Packing)"),
 };
-#endif /* CONFIG_MLP_DECODER */
 
 #if CONFIG_TRUEHD_DECODER
-AVCodec truehd_decoder = {
+AVCodec ff_truehd_decoder = {
     "truehd",
-    CODEC_TYPE_AUDIO,
+    AVMEDIA_TYPE_AUDIO,
     CODEC_ID_TRUEHD,
     sizeof(MLPDecodeContext),
     mlp_decode_init,