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 == 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 &&
00341 !m->avctx->request_channels && !m->avctx->request_channel_layout &&
00342 av_get_channel_layout_nb_channels(m->avctx->channel_layout) != m->avctx->channels) {
00343 m->avctx->channel_layout = 0;
00344 av_log_ask_for_sample(m->avctx, "Unknown channel layout.");
00345 }
00346 }
00347
00348 m->needs_reordering = mh.channels_mlp >= 18 && mh.channels_mlp <= 20;
00349
00350 return 0;
00351 }
00352
00357 static int read_restart_header(MLPDecodeContext *m, GetBitContext *gbp,
00358 const uint8_t *buf, unsigned int substr)
00359 {
00360 SubStream *s = &m->substream[substr];
00361 unsigned int ch;
00362 int sync_word, tmp;
00363 uint8_t checksum;
00364 uint8_t lossless_check;
00365 int start_count = get_bits_count(gbp);
00366 const int max_matrix_channel = m->avctx->codec_id == CODEC_ID_MLP
00367 ? MAX_MATRIX_CHANNEL_MLP
00368 : MAX_MATRIX_CHANNEL_TRUEHD;
00369 int max_channel, min_channel, matrix_channel;
00370
00371 sync_word = get_bits(gbp, 13);
00372
00373 if (sync_word != 0x31ea >> 1) {
00374 av_log(m->avctx, AV_LOG_ERROR,
00375 "restart header sync incorrect (got 0x%04x)\n", sync_word);
00376 return AVERROR_INVALIDDATA;
00377 }
00378
00379 s->noise_type = get_bits1(gbp);
00380
00381 if (m->avctx->codec_id == CODEC_ID_MLP && s->noise_type) {
00382 av_log(m->avctx, AV_LOG_ERROR, "MLP must have 0x31ea sync word.\n");
00383 return AVERROR_INVALIDDATA;
00384 }
00385
00386 skip_bits(gbp, 16);
00387
00388 min_channel = get_bits(gbp, 4);
00389 max_channel = get_bits(gbp, 4);
00390 matrix_channel = get_bits(gbp, 4);
00391
00392 if (matrix_channel > max_matrix_channel) {
00393 av_log(m->avctx, AV_LOG_ERROR,
00394 "Max matrix channel cannot be greater than %d.\n",
00395 max_matrix_channel);
00396 return AVERROR_INVALIDDATA;
00397 }
00398
00399 if (max_channel != matrix_channel) {
00400 av_log(m->avctx, AV_LOG_ERROR,
00401 "Max channel must be equal max matrix channel.\n");
00402 return AVERROR_INVALIDDATA;
00403 }
00404
00405
00406
00407 if (max_channel > MAX_MATRIX_CHANNEL_MLP && !s->noise_type) {
00408 av_log_ask_for_sample(m->avctx,
00409 "Number of channels %d is larger than the maximum supported "
00410 "by the decoder.\n", max_channel + 2);
00411 return AVERROR_PATCHWELCOME;
00412 }
00413
00414 if (min_channel > max_channel) {
00415 av_log(m->avctx, AV_LOG_ERROR,
00416 "Substream min channel cannot be greater than max channel.\n");
00417 return AVERROR_INVALIDDATA;
00418 }
00419
00420 s->min_channel = min_channel;
00421 s->max_channel = max_channel;
00422 s->max_matrix_channel = matrix_channel;
00423
00424 if (m->avctx->request_channels > 0
00425 && s->max_channel + 1 >= m->avctx->request_channels
00426 && substr < m->max_decoded_substream) {
00427 av_log(m->avctx, AV_LOG_DEBUG,
00428 "Extracting %d channel downmix from substream %d. "
00429 "Further substreams will be skipped.\n",
00430 s->max_channel + 1, substr);
00431 m->max_decoded_substream = substr;
00432 }
00433
00434 s->noise_shift = get_bits(gbp, 4);
00435 s->noisegen_seed = get_bits(gbp, 23);
00436
00437 skip_bits(gbp, 19);
00438
00439 s->data_check_present = get_bits1(gbp);
00440 lossless_check = get_bits(gbp, 8);
00441 if (substr == m->max_decoded_substream
00442 && s->lossless_check_data != 0xffffffff) {
00443 tmp = xor_32_to_8(s->lossless_check_data);
00444 if (tmp != lossless_check)
00445 av_log(m->avctx, AV_LOG_WARNING,
00446 "Lossless check failed - expected %02x, calculated %02x.\n",
00447 lossless_check, tmp);
00448 }
00449
00450 skip_bits(gbp, 16);
00451
00452 memset(s->ch_assign, 0, sizeof(s->ch_assign));
00453
00454 for (ch = 0; ch <= s->max_matrix_channel; ch++) {
00455 int ch_assign = get_bits(gbp, 6);
00456 if (ch_assign > s->max_matrix_channel) {
00457 av_log_ask_for_sample(m->avctx,
00458 "Assignment of matrix channel %d to invalid output channel %d.\n",
00459 ch, ch_assign);
00460 return AVERROR_PATCHWELCOME;
00461 }
00462 s->ch_assign[ch_assign] = ch;
00463 }
00464
00465 if (m->avctx->codec_id == CODEC_ID_MLP && m->needs_reordering) {
00466 if (m->avctx->channel_layout == (AV_CH_LAYOUT_QUAD|AV_CH_LOW_FREQUENCY) ||
00467 m->avctx->channel_layout == AV_CH_LAYOUT_5POINT0_BACK) {
00468 int i = s->ch_assign[4];
00469 s->ch_assign[4] = s->ch_assign[3];
00470 s->ch_assign[3] = s->ch_assign[2];
00471 s->ch_assign[2] = i;
00472 } else if (m->avctx->channel_layout == AV_CH_LAYOUT_5POINT1_BACK) {
00473 FFSWAP(int, s->ch_assign[2], s->ch_assign[4]);
00474 FFSWAP(int, s->ch_assign[3], s->ch_assign[5]);
00475 }
00476 }
00477 if (m->avctx->codec_id == CODEC_ID_TRUEHD &&
00478 (m->avctx->channel_layout == AV_CH_LAYOUT_7POINT1 ||
00479 m->avctx->channel_layout == AV_CH_LAYOUT_7POINT1_WIDE)) {
00480 FFSWAP(int, s->ch_assign[4], s->ch_assign[6]);
00481 FFSWAP(int, s->ch_assign[5], s->ch_assign[7]);
00482 } else if (m->avctx->codec_id == CODEC_ID_TRUEHD &&
00483 (m->avctx->channel_layout == AV_CH_LAYOUT_6POINT1 ||
00484 m->avctx->channel_layout == (AV_CH_LAYOUT_6POINT1 | AV_CH_TOP_CENTER) ||
00485 m->avctx->channel_layout == (AV_CH_LAYOUT_6POINT1 | AV_CH_TOP_FRONT_CENTER))) {
00486 int i = s->ch_assign[6];
00487 s->ch_assign[6] = s->ch_assign[5];
00488 s->ch_assign[5] = s->ch_assign[4];
00489 s->ch_assign[4] = i;
00490 }
00491
00492 checksum = ff_mlp_restart_checksum(buf, get_bits_count(gbp) - start_count);
00493
00494 if (checksum != get_bits(gbp, 8))
00495 av_log(m->avctx, AV_LOG_ERROR, "restart header checksum error\n");
00496
00497
00498 s->param_presence_flags = 0xff;
00499 s->num_primitive_matrices = 0;
00500 s->blocksize = 8;
00501 s->lossless_check_data = 0;
00502
00503 memset(s->output_shift , 0, sizeof(s->output_shift ));
00504 memset(s->quant_step_size, 0, sizeof(s->quant_step_size));
00505
00506 for (ch = s->min_channel; ch <= s->max_channel; ch++) {
00507 ChannelParams *cp = &s->channel_params[ch];
00508 cp->filter_params[FIR].order = 0;
00509 cp->filter_params[IIR].order = 0;
00510 cp->filter_params[FIR].shift = 0;
00511 cp->filter_params[IIR].shift = 0;
00512
00513
00514 cp->huff_offset = 0;
00515 cp->sign_huff_offset = (-1) << 23;
00516 cp->codebook = 0;
00517 cp->huff_lsbs = 24;
00518 }
00519
00520 if (substr == m->max_decoded_substream)
00521 m->avctx->channels = s->max_matrix_channel + 1;
00522
00523 return 0;
00524 }
00525
00528 static int read_filter_params(MLPDecodeContext *m, GetBitContext *gbp,
00529 unsigned int substr, unsigned int channel,
00530 unsigned int filter)
00531 {
00532 SubStream *s = &m->substream[substr];
00533 FilterParams *fp = &s->channel_params[channel].filter_params[filter];
00534 const int max_order = filter ? MAX_IIR_ORDER : MAX_FIR_ORDER;
00535 const char fchar = filter ? 'I' : 'F';
00536 int i, order;
00537
00538
00539 assert(filter < 2);
00540
00541 if (m->filter_changed[channel][filter]++ > 1) {
00542 av_log(m->avctx, AV_LOG_ERROR, "Filters may change only once per access unit.\n");
00543 return AVERROR_INVALIDDATA;
00544 }
00545
00546 order = get_bits(gbp, 4);
00547 if (order > max_order) {
00548 av_log(m->avctx, AV_LOG_ERROR,
00549 "%cIR filter order %d is greater than maximum %d.\n",
00550 fchar, order, max_order);
00551 return AVERROR_INVALIDDATA;
00552 }
00553 fp->order = order;
00554
00555 if (order > 0) {
00556 int32_t *fcoeff = s->channel_params[channel].coeff[filter];
00557 int coeff_bits, coeff_shift;
00558
00559 fp->shift = get_bits(gbp, 4);
00560
00561 coeff_bits = get_bits(gbp, 5);
00562 coeff_shift = get_bits(gbp, 3);
00563 if (coeff_bits < 1 || coeff_bits > 16) {
00564 av_log(m->avctx, AV_LOG_ERROR,
00565 "%cIR filter coeff_bits must be between 1 and 16.\n",
00566 fchar);
00567 return AVERROR_INVALIDDATA;
00568 }
00569 if (coeff_bits + coeff_shift > 16) {
00570 av_log(m->avctx, AV_LOG_ERROR,
00571 "Sum of coeff_bits and coeff_shift for %cIR filter must be 16 or less.\n",
00572 fchar);
00573 return AVERROR_INVALIDDATA;
00574 }
00575
00576 for (i = 0; i < order; i++)
00577 fcoeff[i] = get_sbits(gbp, coeff_bits) << coeff_shift;
00578
00579 if (get_bits1(gbp)) {
00580 int state_bits, state_shift;
00581
00582 if (filter == FIR) {
00583 av_log(m->avctx, AV_LOG_ERROR,
00584 "FIR filter has state data specified.\n");
00585 return AVERROR_INVALIDDATA;
00586 }
00587
00588 state_bits = get_bits(gbp, 4);
00589 state_shift = get_bits(gbp, 4);
00590
00591
00592
00593 for (i = 0; i < order; i++)
00594 fp->state[i] = get_sbits(gbp, state_bits) << state_shift;
00595 }
00596 }
00597
00598 return 0;
00599 }
00600
00603 static int read_matrix_params(MLPDecodeContext *m, unsigned int substr, GetBitContext *gbp)
00604 {
00605 SubStream *s = &m->substream[substr];
00606 unsigned int mat, ch;
00607 const int max_primitive_matrices = m->avctx->codec_id == CODEC_ID_MLP
00608 ? MAX_MATRICES_MLP
00609 : MAX_MATRICES_TRUEHD;
00610
00611 if (m->matrix_changed++ > 1) {
00612 av_log(m->avctx, AV_LOG_ERROR, "Matrices may change only once per access unit.\n");
00613 return AVERROR_INVALIDDATA;
00614 }
00615
00616 s->num_primitive_matrices = get_bits(gbp, 4);
00617
00618 if (s->num_primitive_matrices > max_primitive_matrices) {
00619 av_log(m->avctx, AV_LOG_ERROR,
00620 "Number of primitive matrices cannot be greater than %d.\n",
00621 max_primitive_matrices);
00622 return AVERROR_INVALIDDATA;
00623 }
00624
00625 for (mat = 0; mat < s->num_primitive_matrices; mat++) {
00626 int frac_bits, max_chan;
00627 s->matrix_out_ch[mat] = get_bits(gbp, 4);
00628 frac_bits = get_bits(gbp, 4);
00629 s->lsb_bypass [mat] = get_bits1(gbp);
00630
00631 if (s->matrix_out_ch[mat] > s->max_matrix_channel) {
00632 av_log(m->avctx, AV_LOG_ERROR,
00633 "Invalid channel %d specified as output from matrix.\n",
00634 s->matrix_out_ch[mat]);
00635 return AVERROR_INVALIDDATA;
00636 }
00637 if (frac_bits > 14) {
00638 av_log(m->avctx, AV_LOG_ERROR,
00639 "Too many fractional bits specified.\n");
00640 return AVERROR_INVALIDDATA;
00641 }
00642
00643 max_chan = s->max_matrix_channel;
00644 if (!s->noise_type)
00645 max_chan+=2;
00646
00647 for (ch = 0; ch <= max_chan; ch++) {
00648 int coeff_val = 0;
00649 if (get_bits1(gbp))
00650 coeff_val = get_sbits(gbp, frac_bits + 2);
00651
00652 s->matrix_coeff[mat][ch] = coeff_val << (14 - frac_bits);
00653 }
00654
00655 if (s->noise_type)
00656 s->matrix_noise_shift[mat] = get_bits(gbp, 4);
00657 else
00658 s->matrix_noise_shift[mat] = 0;
00659 }
00660
00661 return 0;
00662 }
00663
00666 static int read_channel_params(MLPDecodeContext *m, unsigned int substr,
00667 GetBitContext *gbp, unsigned int ch)
00668 {
00669 SubStream *s = &m->substream[substr];
00670 ChannelParams *cp = &s->channel_params[ch];
00671 FilterParams *fir = &cp->filter_params[FIR];
00672 FilterParams *iir = &cp->filter_params[IIR];
00673 int ret;
00674
00675 if (s->param_presence_flags & PARAM_FIR)
00676 if (get_bits1(gbp))
00677 if ((ret = read_filter_params(m, gbp, substr, ch, FIR)) < 0)
00678 return ret;
00679
00680 if (s->param_presence_flags & PARAM_IIR)
00681 if (get_bits1(gbp))
00682 if ((ret = read_filter_params(m, gbp, substr, ch, IIR)) < 0)
00683 return ret;
00684
00685 if (fir->order + iir->order > 8) {
00686 av_log(m->avctx, AV_LOG_ERROR, "Total filter orders too high.\n");
00687 return AVERROR_INVALIDDATA;
00688 }
00689
00690 if (fir->order && iir->order &&
00691 fir->shift != iir->shift) {
00692 av_log(m->avctx, AV_LOG_ERROR,
00693 "FIR and IIR filters must use the same precision.\n");
00694 return AVERROR_INVALIDDATA;
00695 }
00696
00697
00698
00699
00700
00701 if (!fir->order && iir->order)
00702 fir->shift = iir->shift;
00703
00704 if (s->param_presence_flags & PARAM_HUFFOFFSET)
00705 if (get_bits1(gbp))
00706 cp->huff_offset = get_sbits(gbp, 15);
00707
00708 cp->codebook = get_bits(gbp, 2);
00709 cp->huff_lsbs = get_bits(gbp, 5);
00710
00711 if (cp->huff_lsbs > 24) {
00712 av_log(m->avctx, AV_LOG_ERROR, "Invalid huff_lsbs.\n");
00713 return AVERROR_INVALIDDATA;
00714 }
00715
00716 cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);
00717
00718 return 0;
00719 }
00720
00724 static int read_decoding_params(MLPDecodeContext *m, GetBitContext *gbp,
00725 unsigned int substr)
00726 {
00727 SubStream *s = &m->substream[substr];
00728 unsigned int ch;
00729 int ret;
00730
00731 if (s->param_presence_flags & PARAM_PRESENCE)
00732 if (get_bits1(gbp))
00733 s->param_presence_flags = get_bits(gbp, 8);
00734
00735 if (s->param_presence_flags & PARAM_BLOCKSIZE)
00736 if (get_bits1(gbp)) {
00737 s->blocksize = get_bits(gbp, 9);
00738 if (s->blocksize < 8 || s->blocksize > m->access_unit_size) {
00739 av_log(m->avctx, AV_LOG_ERROR, "Invalid blocksize.");
00740 s->blocksize = 0;
00741 return AVERROR_INVALIDDATA;
00742 }
00743 }
00744
00745 if (s->param_presence_flags & PARAM_MATRIX)
00746 if (get_bits1(gbp))
00747 if ((ret = read_matrix_params(m, substr, gbp)) < 0)
00748 return ret;
00749
00750 if (s->param_presence_flags & PARAM_OUTSHIFT)
00751 if (get_bits1(gbp))
00752 for (ch = 0; ch <= s->max_matrix_channel; ch++)
00753 s->output_shift[ch] = get_sbits(gbp, 4);
00754
00755 if (s->param_presence_flags & PARAM_QUANTSTEP)
00756 if (get_bits1(gbp))
00757 for (ch = 0; ch <= s->max_channel; ch++) {
00758 ChannelParams *cp = &s->channel_params[ch];
00759
00760 s->quant_step_size[ch] = get_bits(gbp, 4);
00761
00762 cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);
00763 }
00764
00765 for (ch = s->min_channel; ch <= s->max_channel; ch++)
00766 if (get_bits1(gbp))
00767 if ((ret = read_channel_params(m, substr, gbp, ch)) < 0)
00768 return ret;
00769
00770 return 0;
00771 }
00772
00773 #define MSB_MASK(bits) (-1u << bits)
00774
00778 static void filter_channel(MLPDecodeContext *m, unsigned int substr,
00779 unsigned int channel)
00780 {
00781 SubStream *s = &m->substream[substr];
00782 const int32_t *fircoeff = s->channel_params[channel].coeff[FIR];
00783 int32_t state_buffer[NUM_FILTERS][MAX_BLOCKSIZE + MAX_FIR_ORDER];
00784 int32_t *firbuf = state_buffer[FIR] + MAX_BLOCKSIZE;
00785 int32_t *iirbuf = state_buffer[IIR] + MAX_BLOCKSIZE;
00786 FilterParams *fir = &s->channel_params[channel].filter_params[FIR];
00787 FilterParams *iir = &s->channel_params[channel].filter_params[IIR];
00788 unsigned int filter_shift = fir->shift;
00789 int32_t mask = MSB_MASK(s->quant_step_size[channel]);
00790
00791 memcpy(firbuf, fir->state, MAX_FIR_ORDER * sizeof(int32_t));
00792 memcpy(iirbuf, iir->state, MAX_IIR_ORDER * sizeof(int32_t));
00793
00794 m->dsp.mlp_filter_channel(firbuf, fircoeff,
00795 fir->order, iir->order,
00796 filter_shift, mask, s->blocksize,
00797 &m->sample_buffer[s->blockpos][channel]);
00798
00799 memcpy(fir->state, firbuf - s->blocksize, MAX_FIR_ORDER * sizeof(int32_t));
00800 memcpy(iir->state, iirbuf - s->blocksize, MAX_IIR_ORDER * sizeof(int32_t));
00801 }
00802
00805 static int read_block_data(MLPDecodeContext *m, GetBitContext *gbp,
00806 unsigned int substr)
00807 {
00808 SubStream *s = &m->substream[substr];
00809 unsigned int i, ch, expected_stream_pos = 0;
00810 int ret;
00811
00812 if (s->data_check_present) {
00813 expected_stream_pos = get_bits_count(gbp);
00814 expected_stream_pos += get_bits(gbp, 16);
00815 av_log_ask_for_sample(m->avctx, "This file contains some features "
00816 "we have not tested yet.\n");
00817 }
00818
00819 if (s->blockpos + s->blocksize > m->access_unit_size) {
00820 av_log(m->avctx, AV_LOG_ERROR, "too many audio samples in frame\n");
00821 return AVERROR_INVALIDDATA;
00822 }
00823
00824 memset(&m->bypassed_lsbs[s->blockpos][0], 0,
00825 s->blocksize * sizeof(m->bypassed_lsbs[0]));
00826
00827 for (i = 0; i < s->blocksize; i++)
00828 if ((ret = read_huff_channels(m, gbp, substr, i)) < 0)
00829 return ret;
00830
00831 for (ch = s->min_channel; ch <= s->max_channel; ch++)
00832 filter_channel(m, substr, ch);
00833
00834 s->blockpos += s->blocksize;
00835
00836 if (s->data_check_present) {
00837 if (get_bits_count(gbp) != expected_stream_pos)
00838 av_log(m->avctx, AV_LOG_ERROR, "block data length mismatch\n");
00839 skip_bits(gbp, 8);
00840 }
00841
00842 return 0;
00843 }
00844
00847 static const int8_t noise_table[256] = {
00848 30, 51, 22, 54, 3, 7, -4, 38, 14, 55, 46, 81, 22, 58, -3, 2,
00849 52, 31, -7, 51, 15, 44, 74, 30, 85, -17, 10, 33, 18, 80, 28, 62,
00850 10, 32, 23, 69, 72, 26, 35, 17, 73, 60, 8, 56, 2, 6, -2, -5,
00851 51, 4, 11, 50, 66, 76, 21, 44, 33, 47, 1, 26, 64, 48, 57, 40,
00852 38, 16, -10, -28, 92, 22, -18, 29, -10, 5, -13, 49, 19, 24, 70, 34,
00853 61, 48, 30, 14, -6, 25, 58, 33, 42, 60, 67, 17, 54, 17, 22, 30,
00854 67, 44, -9, 50, -11, 43, 40, 32, 59, 82, 13, 49, -14, 55, 60, 36,
00855 48, 49, 31, 47, 15, 12, 4, 65, 1, 23, 29, 39, 45, -2, 84, 69,
00856 0, 72, 37, 57, 27, 41, -15, -16, 35, 31, 14, 61, 24, 0, 27, 24,
00857 16, 41, 55, 34, 53, 9, 56, 12, 25, 29, 53, 5, 20, -20, -8, 20,
00858 13, 28, -3, 78, 38, 16, 11, 62, 46, 29, 21, 24, 46, 65, 43, -23,
00859 89, 18, 74, 21, 38, -12, 19, 12, -19, 8, 15, 33, 4, 57, 9, -8,
00860 36, 35, 26, 28, 7, 83, 63, 79, 75, 11, 3, 87, 37, 47, 34, 40,
00861 39, 19, 20, 42, 27, 34, 39, 77, 13, 42, 59, 64, 45, -1, 32, 37,
00862 45, -5, 53, -6, 7, 36, 50, 23, 6, 32, 9, -21, 18, 71, 27, 52,
00863 -25, 31, 35, 42, -1, 68, 63, 52, 26, 43, 66, 37, 41, 25, 40, 70,
00864 };
00865
00876 static void generate_2_noise_channels(MLPDecodeContext *m, unsigned int substr)
00877 {
00878 SubStream *s = &m->substream[substr];
00879 unsigned int i;
00880 uint32_t seed = s->noisegen_seed;
00881 unsigned int maxchan = s->max_matrix_channel;
00882
00883 for (i = 0; i < s->blockpos; i++) {
00884 uint16_t seed_shr7 = seed >> 7;
00885 m->sample_buffer[i][maxchan+1] = ((int8_t)(seed >> 15)) << s->noise_shift;
00886 m->sample_buffer[i][maxchan+2] = ((int8_t) seed_shr7) << s->noise_shift;
00887
00888 seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
00889 }
00890
00891 s->noisegen_seed = seed;
00892 }
00893
00896 static void fill_noise_buffer(MLPDecodeContext *m, unsigned int substr)
00897 {
00898 SubStream *s = &m->substream[substr];
00899 unsigned int i;
00900 uint32_t seed = s->noisegen_seed;
00901
00902 for (i = 0; i < m->access_unit_size_pow2; i++) {
00903 uint8_t seed_shr15 = seed >> 15;
00904 m->noise_buffer[i] = noise_table[seed_shr15];
00905 seed = (seed << 8) ^ seed_shr15 ^ (seed_shr15 << 5);
00906 }
00907
00908 s->noisegen_seed = seed;
00909 }
00910
00911
00915 static void rematrix_channels(MLPDecodeContext *m, unsigned int substr)
00916 {
00917 SubStream *s = &m->substream[substr];
00918 unsigned int mat, src_ch, i;
00919 unsigned int maxchan;
00920
00921 maxchan = s->max_matrix_channel;
00922 if (!s->noise_type) {
00923 generate_2_noise_channels(m, substr);
00924 maxchan += 2;
00925 } else {
00926 fill_noise_buffer(m, substr);
00927 }
00928
00929 for (mat = 0; mat < s->num_primitive_matrices; mat++) {
00930 int matrix_noise_shift = s->matrix_noise_shift[mat];
00931 unsigned int dest_ch = s->matrix_out_ch[mat];
00932 int32_t mask = MSB_MASK(s->quant_step_size[dest_ch]);
00933 int32_t *coeffs = s->matrix_coeff[mat];
00934 int index = s->num_primitive_matrices - mat;
00935 int index2 = 2 * index + 1;
00936
00937
00938
00939 for (i = 0; i < s->blockpos; i++) {
00940 int32_t bypassed_lsb = m->bypassed_lsbs[i][mat];
00941 int32_t *samples = m->sample_buffer[i];
00942 int64_t accum = 0;
00943
00944 for (src_ch = 0; src_ch <= maxchan; src_ch++)
00945 accum += (int64_t) samples[src_ch] * coeffs[src_ch];
00946
00947 if (matrix_noise_shift) {
00948 index &= m->access_unit_size_pow2 - 1;
00949 accum += m->noise_buffer[index] << (matrix_noise_shift + 7);
00950 index += index2;
00951 }
00952
00953 samples[dest_ch] = ((accum >> 14) & mask) + bypassed_lsb;
00954 }
00955 }
00956 }
00957
00960 static int output_data(MLPDecodeContext *m, unsigned int substr,
00961 void *data, int *got_frame_ptr)
00962 {
00963 AVCodecContext *avctx = m->avctx;
00964 SubStream *s = &m->substream[substr];
00965 unsigned int i, out_ch = 0;
00966 int32_t *data_32;
00967 int16_t *data_16;
00968 int ret;
00969 int is32 = (m->avctx->sample_fmt == AV_SAMPLE_FMT_S32);
00970
00971 if (m->avctx->channels != s->max_matrix_channel + 1) {
00972 av_log(m->avctx, AV_LOG_ERROR, "channel count mismatch\n");
00973 return AVERROR_INVALIDDATA;
00974 }
00975
00976
00977 m->frame.nb_samples = s->blockpos;
00978 if ((ret = avctx->get_buffer(avctx, &m->frame)) < 0) {
00979 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00980 return ret;
00981 }
00982 data_32 = (int32_t *)m->frame.data[0];
00983 data_16 = (int16_t *)m->frame.data[0];
00984
00985 for (i = 0; i < s->blockpos; i++) {
00986 for (out_ch = 0; out_ch <= s->max_matrix_channel; out_ch++) {
00987 int mat_ch = s->ch_assign[out_ch];
00988 int32_t sample = m->sample_buffer[i][mat_ch]
00989 << s->output_shift[mat_ch];
00990 s->lossless_check_data ^= (sample & 0xffffff) << mat_ch;
00991 if (is32) *data_32++ = sample << 8;
00992 else *data_16++ = sample >> 8;
00993 }
00994 }
00995
00996 *got_frame_ptr = 1;
00997 *(AVFrame *)data = m->frame;
00998
00999 return 0;
01000 }
01001
01006 static int read_access_unit(AVCodecContext *avctx, void* data,
01007 int *got_frame_ptr, AVPacket *avpkt)
01008 {
01009 const uint8_t *buf = avpkt->data;
01010 int buf_size = avpkt->size;
01011 MLPDecodeContext *m = avctx->priv_data;
01012 GetBitContext gb;
01013 unsigned int length, substr;
01014 unsigned int substream_start;
01015 unsigned int header_size = 4;
01016 unsigned int substr_header_size = 0;
01017 uint8_t substream_parity_present[MAX_SUBSTREAMS];
01018 uint16_t substream_data_len[MAX_SUBSTREAMS];
01019 uint8_t parity_bits;
01020 int ret;
01021
01022 if (buf_size < 4)
01023 return 0;
01024
01025 length = (AV_RB16(buf) & 0xfff) * 2;
01026
01027 if (length < 4 || length > buf_size)
01028 return AVERROR_INVALIDDATA;
01029
01030 init_get_bits(&gb, (buf + 4), (length - 4) * 8);
01031
01032 m->is_major_sync_unit = 0;
01033 if (show_bits_long(&gb, 31) == (0xf8726fba >> 1)) {
01034 if (read_major_sync(m, &gb) < 0)
01035 goto error;
01036 m->is_major_sync_unit = 1;
01037 header_size += 28;
01038 }
01039
01040 if (!m->params_valid) {
01041 av_log(m->avctx, AV_LOG_WARNING,
01042 "Stream parameters not seen; skipping frame.\n");
01043 *got_frame_ptr = 0;
01044 return length;
01045 }
01046
01047 substream_start = 0;
01048
01049 for (substr = 0; substr < m->num_substreams; substr++) {
01050 int extraword_present, checkdata_present, end, nonrestart_substr;
01051
01052 extraword_present = get_bits1(&gb);
01053 nonrestart_substr = get_bits1(&gb);
01054 checkdata_present = get_bits1(&gb);
01055 skip_bits1(&gb);
01056
01057 end = get_bits(&gb, 12) * 2;
01058
01059 substr_header_size += 2;
01060
01061 if (extraword_present) {
01062 if (m->avctx->codec_id == CODEC_ID_MLP) {
01063 av_log(m->avctx, AV_LOG_ERROR, "There must be no extraword for MLP.\n");
01064 goto error;
01065 }
01066 skip_bits(&gb, 16);
01067 substr_header_size += 2;
01068 }
01069
01070 if (!(nonrestart_substr ^ m->is_major_sync_unit)) {
01071 av_log(m->avctx, AV_LOG_ERROR, "Invalid nonrestart_substr.\n");
01072 goto error;
01073 }
01074
01075 if (end + header_size + substr_header_size > length) {
01076 av_log(m->avctx, AV_LOG_ERROR,
01077 "Indicated length of substream %d data goes off end of "
01078 "packet.\n", substr);
01079 end = length - header_size - substr_header_size;
01080 }
01081
01082 if (end < substream_start) {
01083 av_log(avctx, AV_LOG_ERROR,
01084 "Indicated end offset of substream %d data "
01085 "is smaller than calculated start offset.\n",
01086 substr);
01087 goto error;
01088 }
01089
01090 if (substr > m->max_decoded_substream)
01091 continue;
01092
01093 substream_parity_present[substr] = checkdata_present;
01094 substream_data_len[substr] = end - substream_start;
01095 substream_start = end;
01096 }
01097
01098 parity_bits = ff_mlp_calculate_parity(buf, 4);
01099 parity_bits ^= ff_mlp_calculate_parity(buf + header_size, substr_header_size);
01100
01101 if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
01102 av_log(avctx, AV_LOG_ERROR, "Parity check failed.\n");
01103 goto error;
01104 }
01105
01106 buf += header_size + substr_header_size;
01107
01108 for (substr = 0; substr <= m->max_decoded_substream; substr++) {
01109 SubStream *s = &m->substream[substr];
01110 init_get_bits(&gb, buf, substream_data_len[substr] * 8);
01111
01112 m->matrix_changed = 0;
01113 memset(m->filter_changed, 0, sizeof(m->filter_changed));
01114
01115 s->blockpos = 0;
01116 do {
01117 if (get_bits1(&gb)) {
01118 if (get_bits1(&gb)) {
01119
01120 if (read_restart_header(m, &gb, buf, substr) < 0)
01121 goto next_substr;
01122 s->restart_seen = 1;
01123 }
01124
01125 if (!s->restart_seen)
01126 goto next_substr;
01127 if (read_decoding_params(m, &gb, substr) < 0)
01128 goto next_substr;
01129 }
01130
01131 if (!s->restart_seen)
01132 goto next_substr;
01133
01134 if ((ret = read_block_data(m, &gb, substr)) < 0)
01135 return ret;
01136
01137 if (get_bits_count(&gb) >= substream_data_len[substr] * 8)
01138 goto substream_length_mismatch;
01139
01140 } while (!get_bits1(&gb));
01141
01142 skip_bits(&gb, (-get_bits_count(&gb)) & 15);
01143
01144 if (substream_data_len[substr] * 8 - get_bits_count(&gb) >= 32) {
01145 int shorten_by;
01146
01147 if (get_bits(&gb, 16) != 0xD234)
01148 return AVERROR_INVALIDDATA;
01149
01150 shorten_by = get_bits(&gb, 16);
01151 if (m->avctx->codec_id == CODEC_ID_TRUEHD && shorten_by & 0x2000)
01152 s->blockpos -= FFMIN(shorten_by & 0x1FFF, s->blockpos);
01153 else if (m->avctx->codec_id == CODEC_ID_MLP && shorten_by != 0xD234)
01154 return AVERROR_INVALIDDATA;
01155
01156 if (substr == m->max_decoded_substream)
01157 av_log(m->avctx, AV_LOG_INFO, "End of stream indicated.\n");
01158 }
01159
01160 if (substream_parity_present[substr]) {
01161 uint8_t parity, checksum;
01162
01163 if (substream_data_len[substr] * 8 - get_bits_count(&gb) != 16)
01164 goto substream_length_mismatch;
01165
01166 parity = ff_mlp_calculate_parity(buf, substream_data_len[substr] - 2);
01167 checksum = ff_mlp_checksum8 (buf, substream_data_len[substr] - 2);
01168
01169 if ((get_bits(&gb, 8) ^ parity) != 0xa9 )
01170 av_log(m->avctx, AV_LOG_ERROR, "Substream %d parity check failed.\n", substr);
01171 if ( get_bits(&gb, 8) != checksum)
01172 av_log(m->avctx, AV_LOG_ERROR, "Substream %d checksum failed.\n" , substr);
01173 }
01174
01175 if (substream_data_len[substr] * 8 != get_bits_count(&gb))
01176 goto substream_length_mismatch;
01177
01178 next_substr:
01179 if (!s->restart_seen)
01180 av_log(m->avctx, AV_LOG_ERROR,
01181 "No restart header present in substream %d.\n", substr);
01182
01183 buf += substream_data_len[substr];
01184 }
01185
01186 rematrix_channels(m, m->max_decoded_substream);
01187
01188 if ((ret = output_data(m, m->max_decoded_substream, data, got_frame_ptr)) < 0)
01189 return ret;
01190
01191 return length;
01192
01193 substream_length_mismatch:
01194 av_log(m->avctx, AV_LOG_ERROR, "substream %d length mismatch\n", substr);
01195 return AVERROR_INVALIDDATA;
01196
01197 error:
01198 m->params_valid = 0;
01199 return AVERROR_INVALIDDATA;
01200 }
01201
01202 AVCodec ff_mlp_decoder = {
01203 .name = "mlp",
01204 .type = AVMEDIA_TYPE_AUDIO,
01205 .id = CODEC_ID_MLP,
01206 .priv_data_size = sizeof(MLPDecodeContext),
01207 .init = mlp_decode_init,
01208 .decode = read_access_unit,
01209 .capabilities = CODEC_CAP_DR1,
01210 .long_name = NULL_IF_CONFIG_SMALL("MLP (Meridian Lossless Packing)"),
01211 };
01212
01213 #if CONFIG_TRUEHD_DECODER
01214 AVCodec ff_truehd_decoder = {
01215 .name = "truehd",
01216 .type = AVMEDIA_TYPE_AUDIO,
01217 .id = CODEC_ID_TRUEHD,
01218 .priv_data_size = sizeof(MLPDecodeContext),
01219 .init = mlp_decode_init,
01220 .decode = read_access_unit,
01221 .capabilities = CODEC_CAP_DR1,
01222 .long_name = NULL_IF_CONFIG_SMALL("TrueHD"),
01223 };
01224 #endif