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