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

Generated on Wed Apr 11 2012 07:31:34 for FFmpeg by  doxygen 1.7.1