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