• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • 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 "libavutil/intreadwrite.h"
00031 #include "bitstream.h"
00032 #include "libavutil/crc.h"
00033 #include "parser.h"
00034 #include "mlp_parser.h"
00035 #include "mlp.h"
00036 
00038 #define VLC_BITS            9
00039 
00040 
00041 static const char* sample_message =
00042     "Please file a bug report following the instructions at "
00043     "http://ffmpeg.org/bugreports.html and include "
00044     "a sample of this file.";
00045 
00046 typedef struct SubStream {
00048     uint8_t     restart_seen;
00049 
00051 
00052 
00053     uint16_t    noise_type;
00054 
00056     uint8_t     min_channel;
00058     uint8_t     max_channel;
00060     uint8_t     max_matrix_channel;
00061 
00063     uint8_t     noise_shift;
00065     uint32_t    noisegen_seed;
00066 
00068     uint8_t     data_check_present;
00069 
00071     uint8_t     param_presence_flags;
00072 #define PARAM_BLOCKSIZE     (1 << 7)
00073 #define PARAM_MATRIX        (1 << 6)
00074 #define PARAM_OUTSHIFT      (1 << 5)
00075 #define PARAM_QUANTSTEP     (1 << 4)
00076 #define PARAM_FIR           (1 << 3)
00077 #define PARAM_IIR           (1 << 2)
00078 #define PARAM_HUFFOFFSET    (1 << 1)
00079 
00080 
00082 
00084 
00085     uint8_t     num_primitive_matrices;
00086 
00088     uint8_t     matrix_out_ch[MAX_MATRICES];
00089 
00091     uint8_t     lsb_bypass[MAX_MATRICES];
00093     int32_t     matrix_coeff[MAX_MATRICES][MAX_CHANNELS+2];
00095     uint8_t     matrix_noise_shift[MAX_MATRICES];
00097 
00099     uint8_t     quant_step_size[MAX_CHANNELS];
00100 
00102     uint16_t    blocksize;
00104     uint16_t    blockpos;
00105 
00107     int8_t      output_shift[MAX_CHANNELS];
00108 
00110     int32_t     lossless_check_data;
00111 
00112 } SubStream;
00113 
00114 typedef struct MLPDecodeContext {
00115     AVCodecContext *avctx;
00116 
00118     uint8_t     params_valid;
00119 
00121     uint8_t     num_substreams;
00122 
00124     uint8_t     max_decoded_substream;
00125 
00127     int         access_unit_size;
00129     int         access_unit_size_pow2;
00130 
00131     SubStream   substream[MAX_SUBSTREAMS];
00132 
00133     ChannelParams channel_params[MAX_CHANNELS];
00134 
00135     int8_t      noise_buffer[MAX_BLOCKSIZE_POW2];
00136     int8_t      bypassed_lsbs[MAX_BLOCKSIZE][MAX_CHANNELS];
00137     int32_t     sample_buffer[MAX_BLOCKSIZE][MAX_CHANNELS+2];
00138 } MLPDecodeContext;
00139 
00140 static VLC huff_vlc[3];
00141 
00144 static av_cold void init_static(void)
00145 {
00146     INIT_VLC_STATIC(&huff_vlc[0], VLC_BITS, 18,
00147                 &ff_mlp_huffman_tables[0][0][1], 2, 1,
00148                 &ff_mlp_huffman_tables[0][0][0], 2, 1, 512);
00149     INIT_VLC_STATIC(&huff_vlc[1], VLC_BITS, 16,
00150                 &ff_mlp_huffman_tables[1][0][1], 2, 1,
00151                 &ff_mlp_huffman_tables[1][0][0], 2, 1, 512);
00152     INIT_VLC_STATIC(&huff_vlc[2], VLC_BITS, 15,
00153                 &ff_mlp_huffman_tables[2][0][1], 2, 1,
00154                 &ff_mlp_huffman_tables[2][0][0], 2, 1, 512);
00155 
00156     ff_mlp_init_crc();
00157 }
00158 
00159 static inline int32_t calculate_sign_huff(MLPDecodeContext *m,
00160                                           unsigned int substr, unsigned int ch)
00161 {
00162     ChannelParams *cp = &m->channel_params[ch];
00163     SubStream *s = &m->substream[substr];
00164     int lsb_bits = cp->huff_lsbs - s->quant_step_size[ch];
00165     int sign_shift = lsb_bits + (cp->codebook ? 2 - cp->codebook : -1);
00166     int32_t sign_huff_offset = cp->huff_offset;
00167 
00168     if (cp->codebook > 0)
00169         sign_huff_offset -= 7 << lsb_bits;
00170 
00171     if (sign_shift >= 0)
00172         sign_huff_offset -= 1 << sign_shift;
00173 
00174     return sign_huff_offset;
00175 }
00176 
00180 static inline int read_huff_channels(MLPDecodeContext *m, GetBitContext *gbp,
00181                                      unsigned int substr, unsigned int pos)
00182 {
00183     SubStream *s = &m->substream[substr];
00184     unsigned int mat, channel;
00185 
00186     for (mat = 0; mat < s->num_primitive_matrices; mat++)
00187         if (s->lsb_bypass[mat])
00188             m->bypassed_lsbs[pos + s->blockpos][mat] = get_bits1(gbp);
00189 
00190     for (channel = s->min_channel; channel <= s->max_channel; channel++) {
00191         ChannelParams *cp = &m->channel_params[channel];
00192         int codebook = cp->codebook;
00193         int quant_step_size = s->quant_step_size[channel];
00194         int lsb_bits = cp->huff_lsbs - quant_step_size;
00195         int result = 0;
00196 
00197         if (codebook > 0)
00198             result = get_vlc2(gbp, huff_vlc[codebook-1].table,
00199                             VLC_BITS, (9 + VLC_BITS - 1) / VLC_BITS);
00200 
00201         if (result < 0)
00202             return -1;
00203 
00204         if (lsb_bits > 0)
00205             result = (result << lsb_bits) + get_bits(gbp, lsb_bits);
00206 
00207         result  += cp->sign_huff_offset;
00208         result <<= quant_step_size;
00209 
00210         m->sample_buffer[pos + s->blockpos][channel] = result;
00211     }
00212 
00213     return 0;
00214 }
00215 
00216 static av_cold int mlp_decode_init(AVCodecContext *avctx)
00217 {
00218     MLPDecodeContext *m = avctx->priv_data;
00219     int substr;
00220 
00221     init_static();
00222     m->avctx = avctx;
00223     for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
00224         m->substream[substr].lossless_check_data = 0xffffffff;
00225 
00226     return 0;
00227 }
00228 
00234 static int read_major_sync(MLPDecodeContext *m, GetBitContext *gb)
00235 {
00236     MLPHeaderInfo mh;
00237     int substr;
00238 
00239     if (ff_mlp_read_major_sync(m->avctx, &mh, gb) != 0)
00240         return -1;
00241 
00242     if (mh.group1_bits == 0) {
00243         av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown bits per sample\n");
00244         return -1;
00245     }
00246     if (mh.group2_bits > mh.group1_bits) {
00247         av_log(m->avctx, AV_LOG_ERROR,
00248                "Channel group 2 cannot have more bits per sample than group 1.\n");
00249         return -1;
00250     }
00251 
00252     if (mh.group2_samplerate && mh.group2_samplerate != mh.group1_samplerate) {
00253         av_log(m->avctx, AV_LOG_ERROR,
00254                "Channel groups with differing sample rates are not currently supported.\n");
00255         return -1;
00256     }
00257 
00258     if (mh.group1_samplerate == 0) {
00259         av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown sampling rate\n");
00260         return -1;
00261     }
00262     if (mh.group1_samplerate > MAX_SAMPLERATE) {
00263         av_log(m->avctx, AV_LOG_ERROR,
00264                "Sampling rate %d is greater than the supported maximum (%d).\n",
00265                mh.group1_samplerate, MAX_SAMPLERATE);
00266         return -1;
00267     }
00268     if (mh.access_unit_size > MAX_BLOCKSIZE) {
00269         av_log(m->avctx, AV_LOG_ERROR,
00270                "Block size %d is greater than the supported maximum (%d).\n",
00271                mh.access_unit_size, MAX_BLOCKSIZE);
00272         return -1;
00273     }
00274     if (mh.access_unit_size_pow2 > MAX_BLOCKSIZE_POW2) {
00275         av_log(m->avctx, AV_LOG_ERROR,
00276                "Block size pow2 %d is greater than the supported maximum (%d).\n",
00277                mh.access_unit_size_pow2, MAX_BLOCKSIZE_POW2);
00278         return -1;
00279     }
00280 
00281     if (mh.num_substreams == 0)
00282         return -1;
00283     if (mh.num_substreams > MAX_SUBSTREAMS) {
00284         av_log(m->avctx, AV_LOG_ERROR,
00285                "Number of substreams %d is larger than the maximum supported "
00286                "by the decoder. %s\n", mh.num_substreams, sample_message);
00287         return -1;
00288     }
00289 
00290     m->access_unit_size      = mh.access_unit_size;
00291     m->access_unit_size_pow2 = mh.access_unit_size_pow2;
00292 
00293     m->num_substreams        = mh.num_substreams;
00294     m->max_decoded_substream = m->num_substreams - 1;
00295 
00296     m->avctx->sample_rate    = mh.group1_samplerate;
00297     m->avctx->frame_size     = mh.access_unit_size;
00298 
00299     m->avctx->bits_per_raw_sample = mh.group1_bits;
00300     if (mh.group1_bits > 16)
00301         m->avctx->sample_fmt = SAMPLE_FMT_S32;
00302     else
00303         m->avctx->sample_fmt = SAMPLE_FMT_S16;
00304 
00305     m->params_valid = 1;
00306     for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
00307         m->substream[substr].restart_seen = 0;
00308 
00309     return 0;
00310 }
00311 
00316 static int read_restart_header(MLPDecodeContext *m, GetBitContext *gbp,
00317                                const uint8_t *buf, unsigned int substr)
00318 {
00319     SubStream *s = &m->substream[substr];
00320     unsigned int ch;
00321     int sync_word, tmp;
00322     uint8_t checksum;
00323     uint8_t lossless_check;
00324     int start_count = get_bits_count(gbp);
00325 
00326     sync_word = get_bits(gbp, 13);
00327 
00328     if (sync_word != 0x31ea >> 1) {
00329         av_log(m->avctx, AV_LOG_ERROR,
00330                "restart header sync incorrect (got 0x%04x)\n", sync_word);
00331         return -1;
00332     }
00333     s->noise_type = get_bits1(gbp);
00334 
00335     skip_bits(gbp, 16); /* Output timestamp */
00336 
00337     s->min_channel        = get_bits(gbp, 4);
00338     s->max_channel        = get_bits(gbp, 4);
00339     s->max_matrix_channel = get_bits(gbp, 4);
00340 
00341     if (s->min_channel > s->max_channel) {
00342         av_log(m->avctx, AV_LOG_ERROR,
00343                "Substream min channel cannot be greater than max channel.\n");
00344         return -1;
00345     }
00346 
00347     if (m->avctx->request_channels > 0
00348         && s->max_channel + 1 >= m->avctx->request_channels
00349         && substr < m->max_decoded_substream) {
00350         av_log(m->avctx, AV_LOG_INFO,
00351                "Extracting %d channel downmix from substream %d. "
00352                "Further substreams will be skipped.\n",
00353                s->max_channel + 1, substr);
00354         m->max_decoded_substream = substr;
00355     }
00356 
00357     s->noise_shift   = get_bits(gbp,  4);
00358     s->noisegen_seed = get_bits(gbp, 23);
00359 
00360     skip_bits(gbp, 19);
00361 
00362     s->data_check_present = get_bits1(gbp);
00363     lossless_check = get_bits(gbp, 8);
00364     if (substr == m->max_decoded_substream
00365         && s->lossless_check_data != 0xffffffff) {
00366         tmp = xor_32_to_8(s->lossless_check_data);
00367         if (tmp != lossless_check)
00368             av_log(m->avctx, AV_LOG_WARNING,
00369                    "Lossless check failed - expected %02x, calculated %02x.\n",
00370                    lossless_check, tmp);
00371         else
00372             dprintf(m->avctx, "Lossless check passed for substream %d (%x).\n",
00373                     substr, tmp);
00374     }
00375 
00376     skip_bits(gbp, 16);
00377 
00378     for (ch = 0; ch <= s->max_matrix_channel; ch++) {
00379         int ch_assign = get_bits(gbp, 6);
00380         dprintf(m->avctx, "ch_assign[%d][%d] = %d\n", substr, ch,
00381                 ch_assign);
00382         if (ch_assign != ch) {
00383             av_log(m->avctx, AV_LOG_ERROR,
00384                    "Non-1:1 channel assignments are used in this stream. %s\n",
00385                    sample_message);
00386             return -1;
00387         }
00388     }
00389 
00390     checksum = ff_mlp_restart_checksum(buf, get_bits_count(gbp) - start_count);
00391 
00392     if (checksum != get_bits(gbp, 8))
00393         av_log(m->avctx, AV_LOG_ERROR, "restart header checksum error\n");
00394 
00395     /* Set default decoding parameters. */
00396     s->param_presence_flags   = 0xff;
00397     s->num_primitive_matrices = 0;
00398     s->blocksize              = 8;
00399     s->lossless_check_data    = 0;
00400 
00401     memset(s->output_shift   , 0, sizeof(s->output_shift   ));
00402     memset(s->quant_step_size, 0, sizeof(s->quant_step_size));
00403 
00404     for (ch = s->min_channel; ch <= s->max_channel; ch++) {
00405         ChannelParams *cp = &m->channel_params[ch];
00406         cp->filter_params[FIR].order = 0;
00407         cp->filter_params[IIR].order = 0;
00408         cp->filter_params[FIR].shift = 0;
00409         cp->filter_params[IIR].shift = 0;
00410 
00411         /* Default audio coding is 24-bit raw PCM. */
00412         cp->huff_offset      = 0;
00413         cp->sign_huff_offset = (-1) << 23;
00414         cp->codebook         = 0;
00415         cp->huff_lsbs        = 24;
00416     }
00417 
00418     if (substr == m->max_decoded_substream) {
00419         m->avctx->channels = s->max_channel + 1;
00420     }
00421 
00422     return 0;
00423 }
00424 
00427 static int read_filter_params(MLPDecodeContext *m, GetBitContext *gbp,
00428                               unsigned int channel, unsigned int filter)
00429 {
00430     FilterParams *fp = &m->channel_params[channel].filter_params[filter];
00431     const char fchar = filter ? 'I' : 'F';
00432     int i, order;
00433 
00434     // Filter is 0 for FIR, 1 for IIR.
00435     assert(filter < 2);
00436 
00437     order = get_bits(gbp, 4);
00438     if (order > MAX_FILTER_ORDER) {
00439         av_log(m->avctx, AV_LOG_ERROR,
00440                "%cIR filter order %d is greater than maximum %d.\n",
00441                fchar, order, MAX_FILTER_ORDER);
00442         return -1;
00443     }
00444     fp->order = order;
00445 
00446     if (order > 0) {
00447         int coeff_bits, coeff_shift;
00448 
00449         fp->shift = get_bits(gbp, 4);
00450 
00451         coeff_bits  = get_bits(gbp, 5);
00452         coeff_shift = get_bits(gbp, 3);
00453         if (coeff_bits < 1 || coeff_bits > 16) {
00454             av_log(m->avctx, AV_LOG_ERROR,
00455                    "%cIR filter coeff_bits must be between 1 and 16.\n",
00456                    fchar);
00457             return -1;
00458         }
00459         if (coeff_bits + coeff_shift > 16) {
00460             av_log(m->avctx, AV_LOG_ERROR,
00461                    "Sum of coeff_bits and coeff_shift for %cIR filter must be 16 or less.\n",
00462                    fchar);
00463             return -1;
00464         }
00465 
00466         for (i = 0; i < order; i++)
00467             fp->coeff[i] = get_sbits(gbp, coeff_bits) << coeff_shift;
00468 
00469         if (get_bits1(gbp)) {
00470             int state_bits, state_shift;
00471 
00472             if (filter == FIR) {
00473                 av_log(m->avctx, AV_LOG_ERROR,
00474                        "FIR filter has state data specified.\n");
00475                 return -1;
00476             }
00477 
00478             state_bits  = get_bits(gbp, 4);
00479             state_shift = get_bits(gbp, 4);
00480 
00481             /* TODO: Check validity of state data. */
00482 
00483             for (i = 0; i < order; i++)
00484                 fp->state[i] = get_sbits(gbp, state_bits) << state_shift;
00485         }
00486     }
00487 
00488     return 0;
00489 }
00490 
00494 static int read_decoding_params(MLPDecodeContext *m, GetBitContext *gbp,
00495                                 unsigned int substr)
00496 {
00497     SubStream *s = &m->substream[substr];
00498     unsigned int mat, ch;
00499 
00500     if (get_bits1(gbp))
00501         s->param_presence_flags = get_bits(gbp, 8);
00502 
00503     if (s->param_presence_flags & PARAM_BLOCKSIZE)
00504         if (get_bits1(gbp)) {
00505             s->blocksize = get_bits(gbp, 9);
00506             if (s->blocksize > MAX_BLOCKSIZE) {
00507                 av_log(m->avctx, AV_LOG_ERROR, "block size too large\n");
00508                 s->blocksize = 0;
00509                 return -1;
00510             }
00511         }
00512 
00513     if (s->param_presence_flags & PARAM_MATRIX)
00514         if (get_bits1(gbp)) {
00515             s->num_primitive_matrices = get_bits(gbp, 4);
00516 
00517             for (mat = 0; mat < s->num_primitive_matrices; mat++) {
00518                 int frac_bits, max_chan;
00519                 s->matrix_out_ch[mat] = get_bits(gbp, 4);
00520                 frac_bits             = get_bits(gbp, 4);
00521                 s->lsb_bypass   [mat] = get_bits1(gbp);
00522 
00523                 if (s->matrix_out_ch[mat] > s->max_channel) {
00524                     av_log(m->avctx, AV_LOG_ERROR,
00525                            "Invalid channel %d specified as output from matrix.\n",
00526                            s->matrix_out_ch[mat]);
00527                     return -1;
00528                 }
00529                 if (frac_bits > 14) {
00530                     av_log(m->avctx, AV_LOG_ERROR,
00531                            "Too many fractional bits specified.\n");
00532                     return -1;
00533                 }
00534 
00535                 max_chan = s->max_matrix_channel;
00536                 if (!s->noise_type)
00537                     max_chan+=2;
00538 
00539                 for (ch = 0; ch <= max_chan; ch++) {
00540                     int coeff_val = 0;
00541                     if (get_bits1(gbp))
00542                         coeff_val = get_sbits(gbp, frac_bits + 2);
00543 
00544                     s->matrix_coeff[mat][ch] = coeff_val << (14 - frac_bits);
00545                 }
00546 
00547                 if (s->noise_type)
00548                     s->matrix_noise_shift[mat] = get_bits(gbp, 4);
00549                 else
00550                     s->matrix_noise_shift[mat] = 0;
00551             }
00552         }
00553 
00554     if (s->param_presence_flags & PARAM_OUTSHIFT)
00555         if (get_bits1(gbp))
00556             for (ch = 0; ch <= s->max_matrix_channel; ch++) {
00557                 s->output_shift[ch] = get_bits(gbp, 4);
00558                 dprintf(m->avctx, "output shift[%d] = %d\n",
00559                         ch, s->output_shift[ch]);
00560                 /* TODO: validate */
00561             }
00562 
00563     if (s->param_presence_flags & PARAM_QUANTSTEP)
00564         if (get_bits1(gbp))
00565             for (ch = 0; ch <= s->max_channel; ch++) {
00566                 ChannelParams *cp = &m->channel_params[ch];
00567 
00568                 s->quant_step_size[ch] = get_bits(gbp, 4);
00569                 /* TODO: validate */
00570 
00571                 cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);
00572             }
00573 
00574     for (ch = s->min_channel; ch <= s->max_channel; ch++)
00575         if (get_bits1(gbp)) {
00576             ChannelParams *cp = &m->channel_params[ch];
00577             FilterParams *fir = &cp->filter_params[FIR];
00578             FilterParams *iir = &cp->filter_params[IIR];
00579 
00580             if (s->param_presence_flags & PARAM_FIR)
00581                 if (get_bits1(gbp))
00582                     if (read_filter_params(m, gbp, ch, FIR) < 0)
00583                         return -1;
00584 
00585             if (s->param_presence_flags & PARAM_IIR)
00586                 if (get_bits1(gbp))
00587                     if (read_filter_params(m, gbp, ch, IIR) < 0)
00588                         return -1;
00589 
00590             if (fir->order && iir->order &&
00591                 fir->shift != iir->shift) {
00592                 av_log(m->avctx, AV_LOG_ERROR,
00593                        "FIR and IIR filters must use the same precision.\n");
00594                 return -1;
00595             }
00596             /* The FIR and IIR filters must have the same precision.
00597              * To simplify the filtering code, only the precision of the
00598              * FIR filter is considered. If only the IIR filter is employed,
00599              * the FIR filter precision is set to that of the IIR filter, so
00600              * that the filtering code can use it. */
00601             if (!fir->order && iir->order)
00602                 fir->shift = iir->shift;
00603 
00604             if (s->param_presence_flags & PARAM_HUFFOFFSET)
00605                 if (get_bits1(gbp))
00606                     cp->huff_offset = get_sbits(gbp, 15);
00607 
00608             cp->codebook  = get_bits(gbp, 2);
00609             cp->huff_lsbs = get_bits(gbp, 5);
00610 
00611             cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);
00612 
00613             /* TODO: validate */
00614         }
00615 
00616     return 0;
00617 }
00618 
00619 #define MSB_MASK(bits)  (-1u << bits)
00620 
00624 static void filter_channel(MLPDecodeContext *m, unsigned int substr,
00625                            unsigned int channel)
00626 {
00627     SubStream *s = &m->substream[substr];
00628     int32_t filter_state_buffer[NUM_FILTERS][MAX_BLOCKSIZE + MAX_FILTER_ORDER];
00629     FilterParams *fp[NUM_FILTERS] = { &m->channel_params[channel].filter_params[FIR],
00630                                       &m->channel_params[channel].filter_params[IIR], };
00631     unsigned int filter_shift = fp[FIR]->shift;
00632     int32_t mask = MSB_MASK(s->quant_step_size[channel]);
00633     int index = MAX_BLOCKSIZE;
00634     int j, i;
00635 
00636     for (j = 0; j < NUM_FILTERS; j++) {
00637         memcpy(&filter_state_buffer[j][MAX_BLOCKSIZE], &fp[j]->state[0],
00638                MAX_FILTER_ORDER * sizeof(int32_t));
00639     }
00640 
00641     for (i = 0; i < s->blocksize; i++) {
00642         int32_t residual = m->sample_buffer[i + s->blockpos][channel];
00643         unsigned int order;
00644         int64_t accum = 0;
00645         int32_t result;
00646 
00647         /* TODO: Move this code to DSPContext? */
00648 
00649         for (j = 0; j < NUM_FILTERS; j++)
00650             for (order = 0; order < fp[j]->order; order++)
00651                 accum += (int64_t)filter_state_buffer[j][index + order] *
00652                                   fp[j]->coeff[order];
00653 
00654         accum  = accum >> filter_shift;
00655         result = (accum + residual) & mask;
00656 
00657         --index;
00658 
00659         filter_state_buffer[FIR][index] = result;
00660         filter_state_buffer[IIR][index] = result - accum;
00661 
00662         m->sample_buffer[i + s->blockpos][channel] = result;
00663     }
00664 
00665     for (j = 0; j < NUM_FILTERS; j++) {
00666         memcpy(&fp[j]->state[0], &filter_state_buffer[j][index],
00667                MAX_FILTER_ORDER * sizeof(int32_t));
00668     }
00669 }
00670 
00673 static int read_block_data(MLPDecodeContext *m, GetBitContext *gbp,
00674                            unsigned int substr)
00675 {
00676     SubStream *s = &m->substream[substr];
00677     unsigned int i, ch, expected_stream_pos = 0;
00678 
00679     if (s->data_check_present) {
00680         expected_stream_pos  = get_bits_count(gbp);
00681         expected_stream_pos += get_bits(gbp, 16);
00682         av_log(m->avctx, AV_LOG_WARNING, "This file contains some features "
00683                "we have not tested yet. %s\n", sample_message);
00684     }
00685 
00686     if (s->blockpos + s->blocksize > m->access_unit_size) {
00687         av_log(m->avctx, AV_LOG_ERROR, "too many audio samples in frame\n");
00688         return -1;
00689     }
00690 
00691     memset(&m->bypassed_lsbs[s->blockpos][0], 0,
00692            s->blocksize * sizeof(m->bypassed_lsbs[0]));
00693 
00694     for (i = 0; i < s->blocksize; i++) {
00695         if (read_huff_channels(m, gbp, substr, i) < 0)
00696             return -1;
00697     }
00698 
00699     for (ch = s->min_channel; ch <= s->max_channel; ch++) {
00700         filter_channel(m, substr, ch);
00701     }
00702 
00703     s->blockpos += s->blocksize;
00704 
00705     if (s->data_check_present) {
00706         if (get_bits_count(gbp) != expected_stream_pos)
00707             av_log(m->avctx, AV_LOG_ERROR, "block data length mismatch\n");
00708         skip_bits(gbp, 8);
00709     }
00710 
00711     return 0;
00712 }
00713 
00716 static const int8_t noise_table[256] = {
00717      30,  51,  22,  54,   3,   7,  -4,  38,  14,  55,  46,  81,  22,  58,  -3,   2,
00718      52,  31,  -7,  51,  15,  44,  74,  30,  85, -17,  10,  33,  18,  80,  28,  62,
00719      10,  32,  23,  69,  72,  26,  35,  17,  73,  60,   8,  56,   2,   6,  -2,  -5,
00720      51,   4,  11,  50,  66,  76,  21,  44,  33,  47,   1,  26,  64,  48,  57,  40,
00721      38,  16, -10, -28,  92,  22, -18,  29, -10,   5, -13,  49,  19,  24,  70,  34,
00722      61,  48,  30,  14,  -6,  25,  58,  33,  42,  60,  67,  17,  54,  17,  22,  30,
00723      67,  44,  -9,  50, -11,  43,  40,  32,  59,  82,  13,  49, -14,  55,  60,  36,
00724      48,  49,  31,  47,  15,  12,   4,  65,   1,  23,  29,  39,  45,  -2,  84,  69,
00725       0,  72,  37,  57,  27,  41, -15, -16,  35,  31,  14,  61,  24,   0,  27,  24,
00726      16,  41,  55,  34,  53,   9,  56,  12,  25,  29,  53,   5,  20, -20,  -8,  20,
00727      13,  28,  -3,  78,  38,  16,  11,  62,  46,  29,  21,  24,  46,  65,  43, -23,
00728      89,  18,  74,  21,  38, -12,  19,  12, -19,   8,  15,  33,   4,  57,   9,  -8,
00729      36,  35,  26,  28,   7,  83,  63,  79,  75,  11,   3,  87,  37,  47,  34,  40,
00730      39,  19,  20,  42,  27,  34,  39,  77,  13,  42,  59,  64,  45,  -1,  32,  37,
00731      45,  -5,  53,  -6,   7,  36,  50,  23,   6,  32,   9, -21,  18,  71,  27,  52,
00732     -25,  31,  35,  42,  -1,  68,  63,  52,  26,  43,  66,  37,  41,  25,  40,  70,
00733 };
00734 
00745 static void generate_2_noise_channels(MLPDecodeContext *m, unsigned int substr)
00746 {
00747     SubStream *s = &m->substream[substr];
00748     unsigned int i;
00749     uint32_t seed = s->noisegen_seed;
00750     unsigned int maxchan = s->max_matrix_channel;
00751 
00752     for (i = 0; i < s->blockpos; i++) {
00753         uint16_t seed_shr7 = seed >> 7;
00754         m->sample_buffer[i][maxchan+1] = ((int8_t)(seed >> 15)) << s->noise_shift;
00755         m->sample_buffer[i][maxchan+2] = ((int8_t) seed_shr7)   << s->noise_shift;
00756 
00757         seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
00758     }
00759 
00760     s->noisegen_seed = seed;
00761 }
00762 
00765 static void fill_noise_buffer(MLPDecodeContext *m, unsigned int substr)
00766 {
00767     SubStream *s = &m->substream[substr];
00768     unsigned int i;
00769     uint32_t seed = s->noisegen_seed;
00770 
00771     for (i = 0; i < m->access_unit_size_pow2; i++) {
00772         uint8_t seed_shr15 = seed >> 15;
00773         m->noise_buffer[i] = noise_table[seed_shr15];
00774         seed = (seed << 8) ^ seed_shr15 ^ (seed_shr15 << 5);
00775     }
00776 
00777     s->noisegen_seed = seed;
00778 }
00779 
00780 
00784 static void rematrix_channels(MLPDecodeContext *m, unsigned int substr)
00785 {
00786     SubStream *s = &m->substream[substr];
00787     unsigned int mat, src_ch, i;
00788     unsigned int maxchan;
00789 
00790     maxchan = s->max_matrix_channel;
00791     if (!s->noise_type) {
00792         generate_2_noise_channels(m, substr);
00793         maxchan += 2;
00794     } else {
00795         fill_noise_buffer(m, substr);
00796     }
00797 
00798     for (mat = 0; mat < s->num_primitive_matrices; mat++) {
00799         int matrix_noise_shift = s->matrix_noise_shift[mat];
00800         unsigned int dest_ch = s->matrix_out_ch[mat];
00801         int32_t mask = MSB_MASK(s->quant_step_size[dest_ch]);
00802 
00803         /* TODO: DSPContext? */
00804 
00805         for (i = 0; i < s->blockpos; i++) {
00806             int64_t accum = 0;
00807             for (src_ch = 0; src_ch <= maxchan; src_ch++) {
00808                 accum += (int64_t)m->sample_buffer[i][src_ch]
00809                                   * s->matrix_coeff[mat][src_ch];
00810             }
00811             if (matrix_noise_shift) {
00812                 uint32_t index = s->num_primitive_matrices - mat;
00813                 index = (i * (index * 2 + 1) + index) & (m->access_unit_size_pow2 - 1);
00814                 accum += m->noise_buffer[index] << (matrix_noise_shift + 7);
00815             }
00816             m->sample_buffer[i][dest_ch] = ((accum >> 14) & mask)
00817                                              + m->bypassed_lsbs[i][mat];
00818         }
00819     }
00820 }
00821 
00824 static int output_data_internal(MLPDecodeContext *m, unsigned int substr,
00825                                 uint8_t *data, unsigned int *data_size, int is32)
00826 {
00827     SubStream *s = &m->substream[substr];
00828     unsigned int i, ch = 0;
00829     int32_t *data_32 = (int32_t*) data;
00830     int16_t *data_16 = (int16_t*) data;
00831 
00832     if (*data_size < (s->max_channel + 1) * s->blockpos * (is32 ? 4 : 2))
00833         return -1;
00834 
00835     for (i = 0; i < s->blockpos; i++) {
00836         for (ch = 0; ch <= s->max_channel; ch++) {
00837             int32_t sample = m->sample_buffer[i][ch] << s->output_shift[ch];
00838             s->lossless_check_data ^= (sample & 0xffffff) << ch;
00839             if (is32) *data_32++ = sample << 8;
00840             else      *data_16++ = sample >> 8;
00841         }
00842     }
00843 
00844     *data_size = i * ch * (is32 ? 4 : 2);
00845 
00846     return 0;
00847 }
00848 
00849 static int output_data(MLPDecodeContext *m, unsigned int substr,
00850                        uint8_t *data, unsigned int *data_size)
00851 {
00852     if (m->avctx->sample_fmt == SAMPLE_FMT_S32)
00853         return output_data_internal(m, substr, data, data_size, 1);
00854     else
00855         return output_data_internal(m, substr, data, data_size, 0);
00856 }
00857 
00858 
00863 static int read_access_unit(AVCodecContext *avctx, void* data, int *data_size,
00864                             const uint8_t *buf, int buf_size)
00865 {
00866     MLPDecodeContext *m = avctx->priv_data;
00867     GetBitContext gb;
00868     unsigned int length, substr;
00869     unsigned int substream_start;
00870     unsigned int header_size = 4;
00871     unsigned int substr_header_size = 0;
00872     uint8_t substream_parity_present[MAX_SUBSTREAMS];
00873     uint16_t substream_data_len[MAX_SUBSTREAMS];
00874     uint8_t parity_bits;
00875 
00876     if (buf_size < 4)
00877         return 0;
00878 
00879     length = (AV_RB16(buf) & 0xfff) * 2;
00880 
00881     if (length < 4 || length > buf_size)
00882         return -1;
00883 
00884     init_get_bits(&gb, (buf + 4), (length - 4) * 8);
00885 
00886     if (show_bits_long(&gb, 31) == (0xf8726fba >> 1)) {
00887         dprintf(m->avctx, "Found major sync.\n");
00888         if (read_major_sync(m, &gb) < 0)
00889             goto error;
00890         header_size += 28;
00891     }
00892 
00893     if (!m->params_valid) {
00894         av_log(m->avctx, AV_LOG_WARNING,
00895                "Stream parameters not seen; skipping frame.\n");
00896         *data_size = 0;
00897         return length;
00898     }
00899 
00900     substream_start = 0;
00901 
00902     for (substr = 0; substr < m->num_substreams; substr++) {
00903         int extraword_present, checkdata_present, end;
00904 
00905         extraword_present = get_bits1(&gb);
00906         skip_bits1(&gb);
00907         checkdata_present = get_bits1(&gb);
00908         skip_bits1(&gb);
00909 
00910         end = get_bits(&gb, 12) * 2;
00911 
00912         substr_header_size += 2;
00913 
00914         if (extraword_present) {
00915             skip_bits(&gb, 16);
00916             substr_header_size += 2;
00917         }
00918 
00919         if (end + header_size + substr_header_size > length) {
00920             av_log(m->avctx, AV_LOG_ERROR,
00921                    "Indicated length of substream %d data goes off end of "
00922                    "packet.\n", substr);
00923             end = length - header_size - substr_header_size;
00924         }
00925 
00926         if (end < substream_start) {
00927             av_log(avctx, AV_LOG_ERROR,
00928                    "Indicated end offset of substream %d data "
00929                    "is smaller than calculated start offset.\n",
00930                    substr);
00931             goto error;
00932         }
00933 
00934         if (substr > m->max_decoded_substream)
00935             continue;
00936 
00937         substream_parity_present[substr] = checkdata_present;
00938         substream_data_len[substr] = end - substream_start;
00939         substream_start = end;
00940     }
00941 
00942     parity_bits  = ff_mlp_calculate_parity(buf, 4);
00943     parity_bits ^= ff_mlp_calculate_parity(buf + header_size, substr_header_size);
00944 
00945     if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
00946         av_log(avctx, AV_LOG_ERROR, "Parity check failed.\n");
00947         goto error;
00948     }
00949 
00950     buf += header_size + substr_header_size;
00951 
00952     for (substr = 0; substr <= m->max_decoded_substream; substr++) {
00953         SubStream *s = &m->substream[substr];
00954         init_get_bits(&gb, buf, substream_data_len[substr] * 8);
00955 
00956         s->blockpos = 0;
00957         do {
00958             if (get_bits1(&gb)) {
00959                 if (get_bits1(&gb)) {
00960                     /* A restart header should be present. */
00961                     if (read_restart_header(m, &gb, buf, substr) < 0)
00962                         goto next_substr;
00963                     s->restart_seen = 1;
00964                 }
00965 
00966                 if (!s->restart_seen) {
00967                     av_log(m->avctx, AV_LOG_ERROR,
00968                            "No restart header present in substream %d.\n",
00969                            substr);
00970                     goto next_substr;
00971                 }
00972 
00973                 if (read_decoding_params(m, &gb, substr) < 0)
00974                     goto next_substr;
00975             }
00976 
00977             if (!s->restart_seen) {
00978                 av_log(m->avctx, AV_LOG_ERROR,
00979                        "No restart header present in substream %d.\n",
00980                        substr);
00981                 goto next_substr;
00982             }
00983 
00984             if (read_block_data(m, &gb, substr) < 0)
00985                 return -1;
00986 
00987         } while ((get_bits_count(&gb) < substream_data_len[substr] * 8)
00988                  && get_bits1(&gb) == 0);
00989 
00990         skip_bits(&gb, (-get_bits_count(&gb)) & 15);
00991         if (substream_data_len[substr] * 8 - get_bits_count(&gb) >= 32 &&
00992             (show_bits_long(&gb, 32) == END_OF_STREAM ||
00993              show_bits_long(&gb, 20) == 0xd234e)) {
00994             skip_bits(&gb, 18);
00995             if (substr == m->max_decoded_substream)
00996                 av_log(m->avctx, AV_LOG_INFO, "End of stream indicated.\n");
00997 
00998             if (get_bits1(&gb)) {
00999                 int shorten_by = get_bits(&gb, 13);
01000                 shorten_by = FFMIN(shorten_by, s->blockpos);
01001                 s->blockpos -= shorten_by;
01002             } else
01003                 skip_bits(&gb, 13);
01004         }
01005         if (substream_data_len[substr] * 8 - get_bits_count(&gb) >= 16 &&
01006             substream_parity_present[substr]) {
01007             uint8_t parity, checksum;
01008 
01009             parity = ff_mlp_calculate_parity(buf, substream_data_len[substr] - 2);
01010             if ((parity ^ get_bits(&gb, 8)) != 0xa9)
01011                 av_log(m->avctx, AV_LOG_ERROR,
01012                        "Substream %d parity check failed.\n", substr);
01013 
01014             checksum = ff_mlp_checksum8(buf, substream_data_len[substr] - 2);
01015             if (checksum != get_bits(&gb, 8))
01016                 av_log(m->avctx, AV_LOG_ERROR, "Substream %d checksum failed.\n",
01017                        substr);
01018         }
01019         if (substream_data_len[substr] * 8 != get_bits_count(&gb)) {
01020             av_log(m->avctx, AV_LOG_ERROR, "substream %d length mismatch\n",
01021                    substr);
01022             return -1;
01023         }
01024 
01025 next_substr:
01026         buf += substream_data_len[substr];
01027     }
01028 
01029     rematrix_channels(m, m->max_decoded_substream);
01030 
01031     if (output_data(m, m->max_decoded_substream, data, data_size) < 0)
01032         return -1;
01033 
01034     return length;
01035 
01036 error:
01037     m->params_valid = 0;
01038     return -1;
01039 }
01040 
01041 AVCodec mlp_decoder = {
01042     "mlp",
01043     CODEC_TYPE_AUDIO,
01044     CODEC_ID_MLP,
01045     sizeof(MLPDecodeContext),
01046     mlp_decode_init,
01047     NULL,
01048     NULL,
01049     read_access_unit,
01050     .long_name = NULL_IF_CONFIG_SMALL("MLP (Meridian Lossless Packing)/TrueHD"),
01051 };
01052 

Generated on Sat Feb 16 2013 09:23:12 for ffmpeg by  doxygen 1.7.1