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