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