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