• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/mlpdec.c

Go to the documentation of this file.
00001 /*
00002  * MLP decoder
00003  * Copyright (c) 2007-2008 Ian Caulfield
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 #include <stdint.h>
00028 
00029 #include "avcodec.h"
00030 #include "dsputil.h"
00031 #include "libavutil/intreadwrite.h"
00032 #include "get_bits.h"
00033 #include "libavutil/crc.h"
00034 #include "parser.h"
00035 #include "mlp_parser.h"
00036 #include "mlp.h"
00037 
00039 #define VLC_BITS            9
00040 
00041 
00042 static const char* sample_message =
00043     "Please file a bug report following the instructions at "
00044     "http://libav.org/bugreports.html and include "
00045     "a sample of this file.";
00046 
00047 typedef struct SubStream {
00049     uint8_t     restart_seen;
00050 
00052 
00053 
00054     uint16_t    noise_type;
00055 
00057     uint8_t     min_channel;
00059     uint8_t     max_channel;
00061     uint8_t     max_matrix_channel;
00063     uint8_t     ch_assign[MAX_CHANNELS];
00064 
00066     ChannelParams channel_params[MAX_CHANNELS];
00067 
00069     uint8_t     noise_shift;
00071     uint32_t    noisegen_seed;
00072 
00074     uint8_t     data_check_present;
00075 
00077     uint8_t     param_presence_flags;
00078 #define PARAM_BLOCKSIZE     (1 << 7)
00079 #define PARAM_MATRIX        (1 << 6)
00080 #define PARAM_OUTSHIFT      (1 << 5)
00081 #define PARAM_QUANTSTEP     (1 << 4)
00082 #define PARAM_FIR           (1 << 3)
00083 #define PARAM_IIR           (1 << 2)
00084 #define PARAM_HUFFOFFSET    (1 << 1)
00085 #define PARAM_PRESENCE      (1 << 0)
00086 
00087 
00089 
00091 
00092     uint8_t     num_primitive_matrices;
00093 
00095     uint8_t     matrix_out_ch[MAX_MATRICES];
00096 
00098     uint8_t     lsb_bypass[MAX_MATRICES];
00100     int32_t     matrix_coeff[MAX_MATRICES][MAX_CHANNELS];
00102     uint8_t     matrix_noise_shift[MAX_MATRICES];
00104 
00106     uint8_t     quant_step_size[MAX_CHANNELS];
00107 
00109     uint16_t    blocksize;
00111     uint16_t    blockpos;
00112 
00114     int8_t      output_shift[MAX_CHANNELS];
00115 
00117     int32_t     lossless_check_data;
00118 
00119 } SubStream;
00120 
00121 typedef struct MLPDecodeContext {
00122     AVCodecContext *avctx;
00123     AVFrame     frame;
00124 
00126     int         is_major_sync_unit;
00127 
00129     uint8_t     params_valid;
00130 
00132     uint8_t     num_substreams;
00133 
00135     uint8_t     max_decoded_substream;
00136 
00138     int         access_unit_size;
00140     int         access_unit_size_pow2;
00141 
00142     SubStream   substream[MAX_SUBSTREAMS];
00143 
00144     int         matrix_changed;
00145     int         filter_changed[MAX_CHANNELS][NUM_FILTERS];
00146 
00147     int8_t      noise_buffer[MAX_BLOCKSIZE_POW2];
00148     int8_t      bypassed_lsbs[MAX_BLOCKSIZE][MAX_CHANNELS];
00149     int32_t     sample_buffer[MAX_BLOCKSIZE][MAX_CHANNELS];
00150 
00151     DSPContext  dsp;
00152 } MLPDecodeContext;
00153 
00154 static VLC huff_vlc[3];
00155 
00158 static av_cold void init_static(void)
00159 {
00160     if (!huff_vlc[0].bits) {
00161         INIT_VLC_STATIC(&huff_vlc[0], VLC_BITS, 18,
00162                     &ff_mlp_huffman_tables[0][0][1], 2, 1,
00163                     &ff_mlp_huffman_tables[0][0][0], 2, 1, 512);
00164         INIT_VLC_STATIC(&huff_vlc[1], VLC_BITS, 16,
00165                     &ff_mlp_huffman_tables[1][0][1], 2, 1,
00166                     &ff_mlp_huffman_tables[1][0][0], 2, 1, 512);
00167         INIT_VLC_STATIC(&huff_vlc[2], VLC_BITS, 15,
00168                     &ff_mlp_huffman_tables[2][0][1], 2, 1,
00169                     &ff_mlp_huffman_tables[2][0][0], 2, 1, 512);
00170     }
00171 
00172     ff_mlp_init_crc();
00173 }
00174 
00175 static inline int32_t calculate_sign_huff(MLPDecodeContext *m,
00176                                           unsigned int substr, unsigned int ch)
00177 {
00178     SubStream *s = &m->substream[substr];
00179     ChannelParams *cp = &s->channel_params[ch];
00180     int lsb_bits = cp->huff_lsbs - s->quant_step_size[ch];
00181     int sign_shift = lsb_bits + (cp->codebook ? 2 - cp->codebook : -1);
00182     int32_t sign_huff_offset = cp->huff_offset;
00183 
00184     if (cp->codebook > 0)
00185         sign_huff_offset -= 7 << lsb_bits;
00186 
00187     if (sign_shift >= 0)
00188         sign_huff_offset -= 1 << sign_shift;
00189 
00190     return sign_huff_offset;
00191 }
00192 
00196 static inline int read_huff_channels(MLPDecodeContext *m, GetBitContext *gbp,
00197                                      unsigned int substr, unsigned int pos)
00198 {
00199     SubStream *s = &m->substream[substr];
00200     unsigned int mat, channel;
00201 
00202     for (mat = 0; mat < s->num_primitive_matrices; mat++)
00203         if (s->lsb_bypass[mat])
00204             m->bypassed_lsbs[pos + s->blockpos][mat] = get_bits1(gbp);
00205 
00206     for (channel = s->min_channel; channel <= s->max_channel; channel++) {
00207         ChannelParams *cp = &s->channel_params[channel];
00208         int codebook = cp->codebook;
00209         int quant_step_size = s->quant_step_size[channel];
00210         int lsb_bits = cp->huff_lsbs - quant_step_size;
00211         int result = 0;
00212 
00213         if (codebook > 0)
00214             result = get_vlc2(gbp, huff_vlc[codebook-1].table,
00215                             VLC_BITS, (9 + VLC_BITS - 1) / VLC_BITS);
00216 
00217         if (result < 0)
00218             return AVERROR_INVALIDDATA;
00219 
00220         if (lsb_bits > 0)
00221             result = (result << lsb_bits) + get_bits(gbp, lsb_bits);
00222 
00223         result  += cp->sign_huff_offset;
00224         result <<= quant_step_size;
00225 
00226         m->sample_buffer[pos + s->blockpos][channel] = result;
00227     }
00228 
00229     return 0;
00230 }
00231 
00232 static av_cold int mlp_decode_init(AVCodecContext *avctx)
00233 {
00234     MLPDecodeContext *m = avctx->priv_data;
00235     int substr;
00236 
00237     init_static();
00238     m->avctx = avctx;
00239     for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
00240         m->substream[substr].lossless_check_data = 0xffffffff;
00241     dsputil_init(&m->dsp, avctx);
00242 
00243     avcodec_get_frame_defaults(&m->frame);
00244     avctx->coded_frame = &m->frame;
00245 
00246     return 0;
00247 }
00248 
00254 static int read_major_sync(MLPDecodeContext *m, GetBitContext *gb)
00255 {
00256     MLPHeaderInfo mh;
00257     int substr, ret;
00258 
00259     if ((ret = ff_mlp_read_major_sync(m->avctx, &mh, gb)) != 0)
00260         return ret;
00261 
00262     if (mh.group1_bits == 0) {
00263         av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown bits per sample\n");
00264         return AVERROR_INVALIDDATA;
00265     }
00266     if (mh.group2_bits > mh.group1_bits) {
00267         av_log(m->avctx, AV_LOG_ERROR,
00268                "Channel group 2 cannot have more bits per sample than group 1.\n");
00269         return AVERROR_INVALIDDATA;
00270     }
00271 
00272     if (mh.group2_samplerate && mh.group2_samplerate != mh.group1_samplerate) {
00273         av_log(m->avctx, AV_LOG_ERROR,
00274                "Channel groups with differing sample rates are not currently supported.\n");
00275         return AVERROR_INVALIDDATA;
00276     }
00277 
00278     if (mh.group1_samplerate == 0) {
00279         av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown sampling rate\n");
00280         return AVERROR_INVALIDDATA;
00281     }
00282     if (mh.group1_samplerate > MAX_SAMPLERATE) {
00283         av_log(m->avctx, AV_LOG_ERROR,
00284                "Sampling rate %d is greater than the supported maximum (%d).\n",
00285                mh.group1_samplerate, MAX_SAMPLERATE);
00286         return AVERROR_INVALIDDATA;
00287     }
00288     if (mh.access_unit_size > MAX_BLOCKSIZE) {
00289         av_log(m->avctx, AV_LOG_ERROR,
00290                "Block size %d is greater than the supported maximum (%d).\n",
00291                mh.access_unit_size, MAX_BLOCKSIZE);
00292         return AVERROR_INVALIDDATA;
00293     }
00294     if (mh.access_unit_size_pow2 > MAX_BLOCKSIZE_POW2) {
00295         av_log(m->avctx, AV_LOG_ERROR,
00296                "Block size pow2 %d is greater than the supported maximum (%d).\n",
00297                mh.access_unit_size_pow2, MAX_BLOCKSIZE_POW2);
00298         return AVERROR_INVALIDDATA;
00299     }
00300 
00301     if (mh.num_substreams == 0)
00302         return AVERROR_INVALIDDATA;
00303     if (m->avctx->codec_id == CODEC_ID_MLP && mh.num_substreams > 2) {
00304         av_log(m->avctx, AV_LOG_ERROR, "MLP only supports up to 2 substreams.\n");
00305         return AVERROR_INVALIDDATA;
00306     }
00307     if (mh.num_substreams > MAX_SUBSTREAMS) {
00308         av_log(m->avctx, AV_LOG_ERROR,
00309                "Number of substreams %d is larger than the maximum supported "
00310                "by the decoder. %s\n", mh.num_substreams, sample_message);
00311         return AVERROR_INVALIDDATA;
00312     }
00313 
00314     m->access_unit_size      = mh.access_unit_size;
00315     m->access_unit_size_pow2 = mh.access_unit_size_pow2;
00316 
00317     m->num_substreams        = mh.num_substreams;
00318     m->max_decoded_substream = m->num_substreams - 1;
00319 
00320     m->avctx->sample_rate    = mh.group1_samplerate;
00321     m->avctx->frame_size     = mh.access_unit_size;
00322 
00323     m->avctx->bits_per_raw_sample = mh.group1_bits;
00324     if (mh.group1_bits > 16)
00325         m->avctx->sample_fmt = AV_SAMPLE_FMT_S32;
00326     else
00327         m->avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00328 
00329     m->params_valid = 1;
00330     for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
00331         m->substream[substr].restart_seen = 0;
00332 
00333     return 0;
00334 }
00335 
00340 static int read_restart_header(MLPDecodeContext *m, GetBitContext *gbp,
00341                                const uint8_t *buf, unsigned int substr)
00342 {
00343     SubStream *s = &m->substream[substr];
00344     unsigned int ch;
00345     int sync_word, tmp;
00346     uint8_t checksum;
00347     uint8_t lossless_check;
00348     int start_count = get_bits_count(gbp);
00349     const int max_matrix_channel = m->avctx->codec_id == CODEC_ID_MLP
00350                                  ? MAX_MATRIX_CHANNEL_MLP
00351                                  : MAX_MATRIX_CHANNEL_TRUEHD;
00352 
00353     sync_word = get_bits(gbp, 13);
00354 
00355     if (sync_word != 0x31ea >> 1) {
00356         av_log(m->avctx, AV_LOG_ERROR,
00357                "restart header sync incorrect (got 0x%04x)\n", sync_word);
00358         return AVERROR_INVALIDDATA;
00359     }
00360 
00361     s->noise_type = get_bits1(gbp);
00362 
00363     if (m->avctx->codec_id == CODEC_ID_MLP && s->noise_type) {
00364         av_log(m->avctx, AV_LOG_ERROR, "MLP must have 0x31ea sync word.\n");
00365         return AVERROR_INVALIDDATA;
00366     }
00367 
00368     skip_bits(gbp, 16); /* Output timestamp */
00369 
00370     s->min_channel        = get_bits(gbp, 4);
00371     s->max_channel        = get_bits(gbp, 4);
00372     s->max_matrix_channel = get_bits(gbp, 4);
00373 
00374     if (s->max_matrix_channel > max_matrix_channel) {
00375         av_log(m->avctx, AV_LOG_ERROR,
00376                "Max matrix channel cannot be greater than %d.\n",
00377                max_matrix_channel);
00378         return AVERROR_INVALIDDATA;
00379     }
00380 
00381     if (s->max_channel != s->max_matrix_channel) {
00382         av_log(m->avctx, AV_LOG_ERROR,
00383                "Max channel must be equal max matrix channel.\n");
00384         return AVERROR_INVALIDDATA;
00385     }
00386 
00387     /* This should happen for TrueHD streams with >6 channels and MLP's noise
00388      * type. It is not yet known if this is allowed. */
00389     if (s->max_channel > MAX_MATRIX_CHANNEL_MLP && !s->noise_type) {
00390         av_log(m->avctx, AV_LOG_ERROR,
00391                "Number of channels %d is larger than the maximum supported "
00392                "by the decoder. %s\n", s->max_channel+2, sample_message);
00393         return AVERROR_INVALIDDATA;
00394     }
00395 
00396     if (s->min_channel > s->max_channel) {
00397         av_log(m->avctx, AV_LOG_ERROR,
00398                "Substream min channel cannot be greater than max channel.\n");
00399         return AVERROR_INVALIDDATA;
00400     }
00401 
00402     if (m->avctx->request_channels > 0
00403         && s->max_channel + 1 >= m->avctx->request_channels
00404         && substr < m->max_decoded_substream) {
00405         av_log(m->avctx, AV_LOG_DEBUG,
00406                "Extracting %d channel downmix from substream %d. "
00407                "Further substreams will be skipped.\n",
00408                s->max_channel + 1, substr);
00409         m->max_decoded_substream = substr;
00410     }
00411 
00412     s->noise_shift   = get_bits(gbp,  4);
00413     s->noisegen_seed = get_bits(gbp, 23);
00414 
00415     skip_bits(gbp, 19);
00416 
00417     s->data_check_present = get_bits1(gbp);
00418     lossless_check = get_bits(gbp, 8);
00419     if (substr == m->max_decoded_substream
00420         && s->lossless_check_data != 0xffffffff) {
00421         tmp = xor_32_to_8(s->lossless_check_data);
00422         if (tmp != lossless_check)
00423             av_log(m->avctx, AV_LOG_WARNING,
00424                    "Lossless check failed - expected %02x, calculated %02x.\n",
00425                    lossless_check, tmp);
00426     }
00427 
00428     skip_bits(gbp, 16);
00429 
00430     memset(s->ch_assign, 0, sizeof(s->ch_assign));
00431 
00432     for (ch = 0; ch <= s->max_matrix_channel; ch++) {
00433         int ch_assign = get_bits(gbp, 6);
00434         if (ch_assign > s->max_matrix_channel) {
00435             av_log(m->avctx, AV_LOG_ERROR,
00436                    "Assignment of matrix channel %d to invalid output channel %d. %s\n",
00437                    ch, ch_assign, sample_message);
00438             return AVERROR_INVALIDDATA;
00439         }
00440         s->ch_assign[ch_assign] = ch;
00441     }
00442 
00443     checksum = ff_mlp_restart_checksum(buf, get_bits_count(gbp) - start_count);
00444 
00445     if (checksum != get_bits(gbp, 8))
00446         av_log(m->avctx, AV_LOG_ERROR, "restart header checksum error\n");
00447 
00448     /* Set default decoding parameters. */
00449     s->param_presence_flags   = 0xff;
00450     s->num_primitive_matrices = 0;
00451     s->blocksize              = 8;
00452     s->lossless_check_data    = 0;
00453 
00454     memset(s->output_shift   , 0, sizeof(s->output_shift   ));
00455     memset(s->quant_step_size, 0, sizeof(s->quant_step_size));
00456 
00457     for (ch = s->min_channel; ch <= s->max_channel; ch++) {
00458         ChannelParams *cp = &s->channel_params[ch];
00459         cp->filter_params[FIR].order = 0;
00460         cp->filter_params[IIR].order = 0;
00461         cp->filter_params[FIR].shift = 0;
00462         cp->filter_params[IIR].shift = 0;
00463 
00464         /* Default audio coding is 24-bit raw PCM. */
00465         cp->huff_offset      = 0;
00466         cp->sign_huff_offset = (-1) << 23;
00467         cp->codebook         = 0;
00468         cp->huff_lsbs        = 24;
00469     }
00470 
00471     if (substr == m->max_decoded_substream)
00472         m->avctx->channels = s->max_matrix_channel + 1;
00473 
00474     return 0;
00475 }
00476 
00479 static int read_filter_params(MLPDecodeContext *m, GetBitContext *gbp,
00480                               unsigned int substr, unsigned int channel,
00481                               unsigned int filter)
00482 {
00483     SubStream *s = &m->substream[substr];
00484     FilterParams *fp = &s->channel_params[channel].filter_params[filter];
00485     const int max_order = filter ? MAX_IIR_ORDER : MAX_FIR_ORDER;
00486     const char fchar = filter ? 'I' : 'F';
00487     int i, order;
00488 
00489     // Filter is 0 for FIR, 1 for IIR.
00490     assert(filter < 2);
00491 
00492     if (m->filter_changed[channel][filter]++ > 1) {
00493         av_log(m->avctx, AV_LOG_ERROR, "Filters may change only once per access unit.\n");
00494         return AVERROR_INVALIDDATA;
00495     }
00496 
00497     order = get_bits(gbp, 4);
00498     if (order > max_order) {
00499         av_log(m->avctx, AV_LOG_ERROR,
00500                "%cIR filter order %d is greater than maximum %d.\n",
00501                fchar, order, max_order);
00502         return AVERROR_INVALIDDATA;
00503     }
00504     fp->order = order;
00505 
00506     if (order > 0) {
00507         int32_t *fcoeff = s->channel_params[channel].coeff[filter];
00508         int coeff_bits, coeff_shift;
00509 
00510         fp->shift = get_bits(gbp, 4);
00511 
00512         coeff_bits  = get_bits(gbp, 5);
00513         coeff_shift = get_bits(gbp, 3);
00514         if (coeff_bits < 1 || coeff_bits > 16) {
00515             av_log(m->avctx, AV_LOG_ERROR,
00516                    "%cIR filter coeff_bits must be between 1 and 16.\n",
00517                    fchar);
00518             return AVERROR_INVALIDDATA;
00519         }
00520         if (coeff_bits + coeff_shift > 16) {
00521             av_log(m->avctx, AV_LOG_ERROR,
00522                    "Sum of coeff_bits and coeff_shift for %cIR filter must be 16 or less.\n",
00523                    fchar);
00524             return AVERROR_INVALIDDATA;
00525         }
00526 
00527         for (i = 0; i < order; i++)
00528             fcoeff[i] = get_sbits(gbp, coeff_bits) << coeff_shift;
00529 
00530         if (get_bits1(gbp)) {
00531             int state_bits, state_shift;
00532 
00533             if (filter == FIR) {
00534                 av_log(m->avctx, AV_LOG_ERROR,
00535                        "FIR filter has state data specified.\n");
00536                 return AVERROR_INVALIDDATA;
00537             }
00538 
00539             state_bits  = get_bits(gbp, 4);
00540             state_shift = get_bits(gbp, 4);
00541 
00542             /* TODO: Check validity of state data. */
00543 
00544             for (i = 0; i < order; i++)
00545                 fp->state[i] = get_sbits(gbp, state_bits) << state_shift;
00546         }
00547     }
00548 
00549     return 0;
00550 }
00551 
00554 static int read_matrix_params(MLPDecodeContext *m, unsigned int substr, GetBitContext *gbp)
00555 {
00556     SubStream *s = &m->substream[substr];
00557     unsigned int mat, ch;
00558     const int max_primitive_matrices = m->avctx->codec_id == CODEC_ID_MLP
00559                                      ? MAX_MATRICES_MLP
00560                                      : MAX_MATRICES_TRUEHD;
00561 
00562     if (m->matrix_changed++ > 1) {
00563         av_log(m->avctx, AV_LOG_ERROR, "Matrices may change only once per access unit.\n");
00564         return AVERROR_INVALIDDATA;
00565     }
00566 
00567     s->num_primitive_matrices = get_bits(gbp, 4);
00568 
00569     if (s->num_primitive_matrices > max_primitive_matrices) {
00570         av_log(m->avctx, AV_LOG_ERROR,
00571                "Number of primitive matrices cannot be greater than %d.\n",
00572                max_primitive_matrices);
00573         return AVERROR_INVALIDDATA;
00574     }
00575 
00576     for (mat = 0; mat < s->num_primitive_matrices; mat++) {
00577         int frac_bits, max_chan;
00578         s->matrix_out_ch[mat] = get_bits(gbp, 4);
00579         frac_bits             = get_bits(gbp, 4);
00580         s->lsb_bypass   [mat] = get_bits1(gbp);
00581 
00582         if (s->matrix_out_ch[mat] > s->max_matrix_channel) {
00583             av_log(m->avctx, AV_LOG_ERROR,
00584                     "Invalid channel %d specified as output from matrix.\n",
00585                     s->matrix_out_ch[mat]);
00586             return AVERROR_INVALIDDATA;
00587         }
00588         if (frac_bits > 14) {
00589             av_log(m->avctx, AV_LOG_ERROR,
00590                     "Too many fractional bits specified.\n");
00591             return AVERROR_INVALIDDATA;
00592         }
00593 
00594         max_chan = s->max_matrix_channel;
00595         if (!s->noise_type)
00596             max_chan+=2;
00597 
00598         for (ch = 0; ch <= max_chan; ch++) {
00599             int coeff_val = 0;
00600             if (get_bits1(gbp))
00601                 coeff_val = get_sbits(gbp, frac_bits + 2);
00602 
00603             s->matrix_coeff[mat][ch] = coeff_val << (14 - frac_bits);
00604         }
00605 
00606         if (s->noise_type)
00607             s->matrix_noise_shift[mat] = get_bits(gbp, 4);
00608         else
00609             s->matrix_noise_shift[mat] = 0;
00610     }
00611 
00612     return 0;
00613 }
00614 
00617 static int read_channel_params(MLPDecodeContext *m, unsigned int substr,
00618                                GetBitContext *gbp, unsigned int ch)
00619 {
00620     SubStream *s = &m->substream[substr];
00621     ChannelParams *cp = &s->channel_params[ch];
00622     FilterParams *fir = &cp->filter_params[FIR];
00623     FilterParams *iir = &cp->filter_params[IIR];
00624     int ret;
00625 
00626     if (s->param_presence_flags & PARAM_FIR)
00627         if (get_bits1(gbp))
00628             if ((ret = read_filter_params(m, gbp, substr, ch, FIR)) < 0)
00629                 return ret;
00630 
00631     if (s->param_presence_flags & PARAM_IIR)
00632         if (get_bits1(gbp))
00633             if ((ret = read_filter_params(m, gbp, substr, ch, IIR)) < 0)
00634                 return ret;
00635 
00636     if (fir->order + iir->order > 8) {
00637         av_log(m->avctx, AV_LOG_ERROR, "Total filter orders too high.\n");
00638         return AVERROR_INVALIDDATA;
00639     }
00640 
00641     if (fir->order && iir->order &&
00642         fir->shift != iir->shift) {
00643         av_log(m->avctx, AV_LOG_ERROR,
00644                 "FIR and IIR filters must use the same precision.\n");
00645         return AVERROR_INVALIDDATA;
00646     }
00647     /* The FIR and IIR filters must have the same precision.
00648      * To simplify the filtering code, only the precision of the
00649      * FIR filter is considered. If only the IIR filter is employed,
00650      * the FIR filter precision is set to that of the IIR filter, so
00651      * that the filtering code can use it. */
00652     if (!fir->order && iir->order)
00653         fir->shift = iir->shift;
00654 
00655     if (s->param_presence_flags & PARAM_HUFFOFFSET)
00656         if (get_bits1(gbp))
00657             cp->huff_offset = get_sbits(gbp, 15);
00658 
00659     cp->codebook  = get_bits(gbp, 2);
00660     cp->huff_lsbs = get_bits(gbp, 5);
00661 
00662     if (cp->huff_lsbs > 24) {
00663         av_log(m->avctx, AV_LOG_ERROR, "Invalid huff_lsbs.\n");
00664         return AVERROR_INVALIDDATA;
00665     }
00666 
00667     cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);
00668 
00669     return 0;
00670 }
00671 
00675 static int read_decoding_params(MLPDecodeContext *m, GetBitContext *gbp,
00676                                 unsigned int substr)
00677 {
00678     SubStream *s = &m->substream[substr];
00679     unsigned int ch;
00680     int ret;
00681 
00682     if (s->param_presence_flags & PARAM_PRESENCE)
00683         if (get_bits1(gbp))
00684             s->param_presence_flags = get_bits(gbp, 8);
00685 
00686     if (s->param_presence_flags & PARAM_BLOCKSIZE)
00687         if (get_bits1(gbp)) {
00688             s->blocksize = get_bits(gbp, 9);
00689             if (s->blocksize < 8 || s->blocksize > m->access_unit_size) {
00690                 av_log(m->avctx, AV_LOG_ERROR, "Invalid blocksize.");
00691                 s->blocksize = 0;
00692                 return AVERROR_INVALIDDATA;
00693             }
00694         }
00695 
00696     if (s->param_presence_flags & PARAM_MATRIX)
00697         if (get_bits1(gbp))
00698             if ((ret = read_matrix_params(m, substr, gbp)) < 0)
00699                 return ret;
00700 
00701     if (s->param_presence_flags & PARAM_OUTSHIFT)
00702         if (get_bits1(gbp))
00703             for (ch = 0; ch <= s->max_matrix_channel; ch++)
00704                 s->output_shift[ch] = get_sbits(gbp, 4);
00705 
00706     if (s->param_presence_flags & PARAM_QUANTSTEP)
00707         if (get_bits1(gbp))
00708             for (ch = 0; ch <= s->max_channel; ch++) {
00709                 ChannelParams *cp = &s->channel_params[ch];
00710 
00711                 s->quant_step_size[ch] = get_bits(gbp, 4);
00712 
00713                 cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);
00714             }
00715 
00716     for (ch = s->min_channel; ch <= s->max_channel; ch++)
00717         if (get_bits1(gbp))
00718             if ((ret = read_channel_params(m, substr, gbp, ch)) < 0)
00719                 return ret;
00720 
00721     return 0;
00722 }
00723 
00724 #define MSB_MASK(bits)  (-1u << bits)
00725 
00729 static void filter_channel(MLPDecodeContext *m, unsigned int substr,
00730                            unsigned int channel)
00731 {
00732     SubStream *s = &m->substream[substr];
00733     const int32_t *fircoeff = s->channel_params[channel].coeff[FIR];
00734     int32_t state_buffer[NUM_FILTERS][MAX_BLOCKSIZE + MAX_FIR_ORDER];
00735     int32_t *firbuf = state_buffer[FIR] + MAX_BLOCKSIZE;
00736     int32_t *iirbuf = state_buffer[IIR] + MAX_BLOCKSIZE;
00737     FilterParams *fir = &s->channel_params[channel].filter_params[FIR];
00738     FilterParams *iir = &s->channel_params[channel].filter_params[IIR];
00739     unsigned int filter_shift = fir->shift;
00740     int32_t mask = MSB_MASK(s->quant_step_size[channel]);
00741 
00742     memcpy(firbuf, fir->state, MAX_FIR_ORDER * sizeof(int32_t));
00743     memcpy(iirbuf, iir->state, MAX_IIR_ORDER * sizeof(int32_t));
00744 
00745     m->dsp.mlp_filter_channel(firbuf, fircoeff,
00746                               fir->order, iir->order,
00747                               filter_shift, mask, s->blocksize,
00748                               &m->sample_buffer[s->blockpos][channel]);
00749 
00750     memcpy(fir->state, firbuf - s->blocksize, MAX_FIR_ORDER * sizeof(int32_t));
00751     memcpy(iir->state, iirbuf - s->blocksize, MAX_IIR_ORDER * sizeof(int32_t));
00752 }
00753 
00756 static int read_block_data(MLPDecodeContext *m, GetBitContext *gbp,
00757                            unsigned int substr)
00758 {
00759     SubStream *s = &m->substream[substr];
00760     unsigned int i, ch, expected_stream_pos = 0;
00761     int ret;
00762 
00763     if (s->data_check_present) {
00764         expected_stream_pos  = get_bits_count(gbp);
00765         expected_stream_pos += get_bits(gbp, 16);
00766         av_log(m->avctx, AV_LOG_WARNING, "This file contains some features "
00767                "we have not tested yet. %s\n", sample_message);
00768     }
00769 
00770     if (s->blockpos + s->blocksize > m->access_unit_size) {
00771         av_log(m->avctx, AV_LOG_ERROR, "too many audio samples in frame\n");
00772         return AVERROR_INVALIDDATA;
00773     }
00774 
00775     memset(&m->bypassed_lsbs[s->blockpos][0], 0,
00776            s->blocksize * sizeof(m->bypassed_lsbs[0]));
00777 
00778     for (i = 0; i < s->blocksize; i++)
00779         if ((ret = read_huff_channels(m, gbp, substr, i)) < 0)
00780             return ret;
00781 
00782     for (ch = s->min_channel; ch <= s->max_channel; ch++)
00783         filter_channel(m, substr, ch);
00784 
00785     s->blockpos += s->blocksize;
00786 
00787     if (s->data_check_present) {
00788         if (get_bits_count(gbp) != expected_stream_pos)
00789             av_log(m->avctx, AV_LOG_ERROR, "block data length mismatch\n");
00790         skip_bits(gbp, 8);
00791     }
00792 
00793     return 0;
00794 }
00795 
00798 static const int8_t noise_table[256] = {
00799      30,  51,  22,  54,   3,   7,  -4,  38,  14,  55,  46,  81,  22,  58,  -3,   2,
00800      52,  31,  -7,  51,  15,  44,  74,  30,  85, -17,  10,  33,  18,  80,  28,  62,
00801      10,  32,  23,  69,  72,  26,  35,  17,  73,  60,   8,  56,   2,   6,  -2,  -5,
00802      51,   4,  11,  50,  66,  76,  21,  44,  33,  47,   1,  26,  64,  48,  57,  40,
00803      38,  16, -10, -28,  92,  22, -18,  29, -10,   5, -13,  49,  19,  24,  70,  34,
00804      61,  48,  30,  14,  -6,  25,  58,  33,  42,  60,  67,  17,  54,  17,  22,  30,
00805      67,  44,  -9,  50, -11,  43,  40,  32,  59,  82,  13,  49, -14,  55,  60,  36,
00806      48,  49,  31,  47,  15,  12,   4,  65,   1,  23,  29,  39,  45,  -2,  84,  69,
00807       0,  72,  37,  57,  27,  41, -15, -16,  35,  31,  14,  61,  24,   0,  27,  24,
00808      16,  41,  55,  34,  53,   9,  56,  12,  25,  29,  53,   5,  20, -20,  -8,  20,
00809      13,  28,  -3,  78,  38,  16,  11,  62,  46,  29,  21,  24,  46,  65,  43, -23,
00810      89,  18,  74,  21,  38, -12,  19,  12, -19,   8,  15,  33,   4,  57,   9,  -8,
00811      36,  35,  26,  28,   7,  83,  63,  79,  75,  11,   3,  87,  37,  47,  34,  40,
00812      39,  19,  20,  42,  27,  34,  39,  77,  13,  42,  59,  64,  45,  -1,  32,  37,
00813      45,  -5,  53,  -6,   7,  36,  50,  23,   6,  32,   9, -21,  18,  71,  27,  52,
00814     -25,  31,  35,  42,  -1,  68,  63,  52,  26,  43,  66,  37,  41,  25,  40,  70,
00815 };
00816 
00827 static void generate_2_noise_channels(MLPDecodeContext *m, unsigned int substr)
00828 {
00829     SubStream *s = &m->substream[substr];
00830     unsigned int i;
00831     uint32_t seed = s->noisegen_seed;
00832     unsigned int maxchan = s->max_matrix_channel;
00833 
00834     for (i = 0; i < s->blockpos; i++) {
00835         uint16_t seed_shr7 = seed >> 7;
00836         m->sample_buffer[i][maxchan+1] = ((int8_t)(seed >> 15)) << s->noise_shift;
00837         m->sample_buffer[i][maxchan+2] = ((int8_t) seed_shr7)   << s->noise_shift;
00838 
00839         seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
00840     }
00841 
00842     s->noisegen_seed = seed;
00843 }
00844 
00847 static void fill_noise_buffer(MLPDecodeContext *m, unsigned int substr)
00848 {
00849     SubStream *s = &m->substream[substr];
00850     unsigned int i;
00851     uint32_t seed = s->noisegen_seed;
00852 
00853     for (i = 0; i < m->access_unit_size_pow2; i++) {
00854         uint8_t seed_shr15 = seed >> 15;
00855         m->noise_buffer[i] = noise_table[seed_shr15];
00856         seed = (seed << 8) ^ seed_shr15 ^ (seed_shr15 << 5);
00857     }
00858 
00859     s->noisegen_seed = seed;
00860 }
00861 
00862 
00866 static void rematrix_channels(MLPDecodeContext *m, unsigned int substr)
00867 {
00868     SubStream *s = &m->substream[substr];
00869     unsigned int mat, src_ch, i;
00870     unsigned int maxchan;
00871 
00872     maxchan = s->max_matrix_channel;
00873     if (!s->noise_type) {
00874         generate_2_noise_channels(m, substr);
00875         maxchan += 2;
00876     } else {
00877         fill_noise_buffer(m, substr);
00878     }
00879 
00880     for (mat = 0; mat < s->num_primitive_matrices; mat++) {
00881         int matrix_noise_shift = s->matrix_noise_shift[mat];
00882         unsigned int dest_ch = s->matrix_out_ch[mat];
00883         int32_t mask = MSB_MASK(s->quant_step_size[dest_ch]);
00884         int32_t *coeffs = s->matrix_coeff[mat];
00885         int index  = s->num_primitive_matrices - mat;
00886         int index2 = 2 * index + 1;
00887 
00888         /* TODO: DSPContext? */
00889 
00890         for (i = 0; i < s->blockpos; i++) {
00891             int32_t bypassed_lsb = m->bypassed_lsbs[i][mat];
00892             int32_t *samples = m->sample_buffer[i];
00893             int64_t accum = 0;
00894 
00895             for (src_ch = 0; src_ch <= maxchan; src_ch++)
00896                 accum += (int64_t) samples[src_ch] * coeffs[src_ch];
00897 
00898             if (matrix_noise_shift) {
00899                 index &= m->access_unit_size_pow2 - 1;
00900                 accum += m->noise_buffer[index] << (matrix_noise_shift + 7);
00901                 index += index2;
00902             }
00903 
00904             samples[dest_ch] = ((accum >> 14) & mask) + bypassed_lsb;
00905         }
00906     }
00907 }
00908 
00911 static int output_data(MLPDecodeContext *m, unsigned int substr,
00912                        void *data, int *got_frame_ptr)
00913 {
00914     AVCodecContext *avctx = m->avctx;
00915     SubStream *s = &m->substream[substr];
00916     unsigned int i, out_ch = 0;
00917     int32_t *data_32;
00918     int16_t *data_16;
00919     int ret;
00920     int is32 = (m->avctx->sample_fmt == AV_SAMPLE_FMT_S32);
00921 
00922     if (m->avctx->channels != s->max_matrix_channel + 1) {
00923         av_log(m->avctx, AV_LOG_ERROR, "channel count mismatch\n");
00924         return AVERROR_INVALIDDATA;
00925     }
00926 
00927     /* get output buffer */
00928     m->frame.nb_samples = s->blockpos;
00929     if ((ret = avctx->get_buffer(avctx, &m->frame)) < 0) {
00930         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00931         return ret;
00932     }
00933     data_32 = (int32_t *)m->frame.data[0];
00934     data_16 = (int16_t *)m->frame.data[0];
00935 
00936     for (i = 0; i < s->blockpos; i++) {
00937         for (out_ch = 0; out_ch <= s->max_matrix_channel; out_ch++) {
00938             int mat_ch = s->ch_assign[out_ch];
00939             int32_t sample = m->sample_buffer[i][mat_ch]
00940                           << s->output_shift[mat_ch];
00941             s->lossless_check_data ^= (sample & 0xffffff) << mat_ch;
00942             if (is32) *data_32++ = sample << 8;
00943             else      *data_16++ = sample >> 8;
00944         }
00945     }
00946 
00947     *got_frame_ptr   = 1;
00948     *(AVFrame *)data = m->frame;
00949 
00950     return 0;
00951 }
00952 
00957 static int read_access_unit(AVCodecContext *avctx, void* data,
00958                             int *got_frame_ptr, AVPacket *avpkt)
00959 {
00960     const uint8_t *buf = avpkt->data;
00961     int buf_size = avpkt->size;
00962     MLPDecodeContext *m = avctx->priv_data;
00963     GetBitContext gb;
00964     unsigned int length, substr;
00965     unsigned int substream_start;
00966     unsigned int header_size = 4;
00967     unsigned int substr_header_size = 0;
00968     uint8_t substream_parity_present[MAX_SUBSTREAMS];
00969     uint16_t substream_data_len[MAX_SUBSTREAMS];
00970     uint8_t parity_bits;
00971     int ret;
00972 
00973     if (buf_size < 4)
00974         return 0;
00975 
00976     length = (AV_RB16(buf) & 0xfff) * 2;
00977 
00978     if (length < 4 || length > buf_size)
00979         return AVERROR_INVALIDDATA;
00980 
00981     init_get_bits(&gb, (buf + 4), (length - 4) * 8);
00982 
00983     m->is_major_sync_unit = 0;
00984     if (show_bits_long(&gb, 31) == (0xf8726fba >> 1)) {
00985         if (read_major_sync(m, &gb) < 0)
00986             goto error;
00987         m->is_major_sync_unit = 1;
00988         header_size += 28;
00989     }
00990 
00991     if (!m->params_valid) {
00992         av_log(m->avctx, AV_LOG_WARNING,
00993                "Stream parameters not seen; skipping frame.\n");
00994         *got_frame_ptr = 0;
00995         return length;
00996     }
00997 
00998     substream_start = 0;
00999 
01000     for (substr = 0; substr < m->num_substreams; substr++) {
01001         int extraword_present, checkdata_present, end, nonrestart_substr;
01002 
01003         extraword_present = get_bits1(&gb);
01004         nonrestart_substr = get_bits1(&gb);
01005         checkdata_present = get_bits1(&gb);
01006         skip_bits1(&gb);
01007 
01008         end = get_bits(&gb, 12) * 2;
01009 
01010         substr_header_size += 2;
01011 
01012         if (extraword_present) {
01013             if (m->avctx->codec_id == CODEC_ID_MLP) {
01014                 av_log(m->avctx, AV_LOG_ERROR, "There must be no extraword for MLP.\n");
01015                 goto error;
01016             }
01017             skip_bits(&gb, 16);
01018             substr_header_size += 2;
01019         }
01020 
01021         if (!(nonrestart_substr ^ m->is_major_sync_unit)) {
01022             av_log(m->avctx, AV_LOG_ERROR, "Invalid nonrestart_substr.\n");
01023             goto error;
01024         }
01025 
01026         if (end + header_size + substr_header_size > length) {
01027             av_log(m->avctx, AV_LOG_ERROR,
01028                    "Indicated length of substream %d data goes off end of "
01029                    "packet.\n", substr);
01030             end = length - header_size - substr_header_size;
01031         }
01032 
01033         if (end < substream_start) {
01034             av_log(avctx, AV_LOG_ERROR,
01035                    "Indicated end offset of substream %d data "
01036                    "is smaller than calculated start offset.\n",
01037                    substr);
01038             goto error;
01039         }
01040 
01041         if (substr > m->max_decoded_substream)
01042             continue;
01043 
01044         substream_parity_present[substr] = checkdata_present;
01045         substream_data_len[substr] = end - substream_start;
01046         substream_start = end;
01047     }
01048 
01049     parity_bits  = ff_mlp_calculate_parity(buf, 4);
01050     parity_bits ^= ff_mlp_calculate_parity(buf + header_size, substr_header_size);
01051 
01052     if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
01053         av_log(avctx, AV_LOG_ERROR, "Parity check failed.\n");
01054         goto error;
01055     }
01056 
01057     buf += header_size + substr_header_size;
01058 
01059     for (substr = 0; substr <= m->max_decoded_substream; substr++) {
01060         SubStream *s = &m->substream[substr];
01061         init_get_bits(&gb, buf, substream_data_len[substr] * 8);
01062 
01063         m->matrix_changed = 0;
01064         memset(m->filter_changed, 0, sizeof(m->filter_changed));
01065 
01066         s->blockpos = 0;
01067         do {
01068             if (get_bits1(&gb)) {
01069                 if (get_bits1(&gb)) {
01070                     /* A restart header should be present. */
01071                     if (read_restart_header(m, &gb, buf, substr) < 0)
01072                         goto next_substr;
01073                     s->restart_seen = 1;
01074                 }
01075 
01076                 if (!s->restart_seen)
01077                     goto next_substr;
01078                 if (read_decoding_params(m, &gb, substr) < 0)
01079                     goto next_substr;
01080             }
01081 
01082             if (!s->restart_seen)
01083                 goto next_substr;
01084 
01085             if ((ret = read_block_data(m, &gb, substr)) < 0)
01086                 return ret;
01087 
01088             if (get_bits_count(&gb) >= substream_data_len[substr] * 8)
01089                 goto substream_length_mismatch;
01090 
01091         } while (!get_bits1(&gb));
01092 
01093         skip_bits(&gb, (-get_bits_count(&gb)) & 15);
01094 
01095         if (substream_data_len[substr] * 8 - get_bits_count(&gb) >= 32) {
01096             int shorten_by;
01097 
01098             if (get_bits(&gb, 16) != 0xD234)
01099                 return AVERROR_INVALIDDATA;
01100 
01101             shorten_by = get_bits(&gb, 16);
01102             if      (m->avctx->codec_id == CODEC_ID_TRUEHD && shorten_by  & 0x2000)
01103                 s->blockpos -= FFMIN(shorten_by & 0x1FFF, s->blockpos);
01104             else if (m->avctx->codec_id == CODEC_ID_MLP    && shorten_by != 0xD234)
01105                 return AVERROR_INVALIDDATA;
01106 
01107             if (substr == m->max_decoded_substream)
01108                 av_log(m->avctx, AV_LOG_INFO, "End of stream indicated.\n");
01109         }
01110 
01111         if (substream_parity_present[substr]) {
01112             uint8_t parity, checksum;
01113 
01114             if (substream_data_len[substr] * 8 - get_bits_count(&gb) != 16)
01115                 goto substream_length_mismatch;
01116 
01117             parity   = ff_mlp_calculate_parity(buf, substream_data_len[substr] - 2);
01118             checksum = ff_mlp_checksum8       (buf, substream_data_len[substr] - 2);
01119 
01120             if ((get_bits(&gb, 8) ^ parity) != 0xa9    )
01121                 av_log(m->avctx, AV_LOG_ERROR, "Substream %d parity check failed.\n", substr);
01122             if ( get_bits(&gb, 8)           != checksum)
01123                 av_log(m->avctx, AV_LOG_ERROR, "Substream %d checksum failed.\n"    , substr);
01124         }
01125 
01126         if (substream_data_len[substr] * 8 != get_bits_count(&gb))
01127             goto substream_length_mismatch;
01128 
01129 next_substr:
01130         if (!s->restart_seen)
01131             av_log(m->avctx, AV_LOG_ERROR,
01132                    "No restart header present in substream %d.\n", substr);
01133 
01134         buf += substream_data_len[substr];
01135     }
01136 
01137     rematrix_channels(m, m->max_decoded_substream);
01138 
01139     if ((ret = output_data(m, m->max_decoded_substream, data, got_frame_ptr)) < 0)
01140         return ret;
01141 
01142     return length;
01143 
01144 substream_length_mismatch:
01145     av_log(m->avctx, AV_LOG_ERROR, "substream %d length mismatch\n", substr);
01146     return AVERROR_INVALIDDATA;
01147 
01148 error:
01149     m->params_valid = 0;
01150     return AVERROR_INVALIDDATA;
01151 }
01152 
01153 AVCodec ff_mlp_decoder = {
01154     .name           = "mlp",
01155     .type           = AVMEDIA_TYPE_AUDIO,
01156     .id             = CODEC_ID_MLP,
01157     .priv_data_size = sizeof(MLPDecodeContext),
01158     .init           = mlp_decode_init,
01159     .decode         = read_access_unit,
01160     .capabilities   = CODEC_CAP_DR1,
01161     .long_name = NULL_IF_CONFIG_SMALL("MLP (Meridian Lossless Packing)"),
01162 };
01163 
01164 #if CONFIG_TRUEHD_DECODER
01165 AVCodec ff_truehd_decoder = {
01166     .name           = "truehd",
01167     .type           = AVMEDIA_TYPE_AUDIO,
01168     .id             = CODEC_ID_TRUEHD,
01169     .priv_data_size = sizeof(MLPDecodeContext),
01170     .init           = mlp_decode_init,
01171     .decode         = read_access_unit,
01172     .capabilities   = CODEC_CAP_DR1,
01173     .long_name = NULL_IF_CONFIG_SMALL("TrueHD"),
01174 };
01175 #endif /* CONFIG_TRUEHD_DECODER */
Generated on Thu Jan 24 2013 17:08:52 for Libav by doxygen 1.7.1