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