1/* 2 * QDM2 compatible decoder 3 * Copyright (c) 2003 Ewald Snel 4 * Copyright (c) 2005 Benjamin Larsson 5 * Copyright (c) 2005 Alex Beregszaszi 6 * Copyright (c) 2005 Roberto Togni 7 * 8 * This file is part of FFmpeg. 9 * 10 * FFmpeg is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU Lesser General Public 12 * License as published by the Free Software Foundation; either 13 * version 2.1 of the License, or (at your option) any later version. 14 * 15 * FFmpeg is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 * Lesser General Public License for more details. 19 * 20 * You should have received a copy of the GNU Lesser General Public 21 * License along with FFmpeg; if not, write to the Free Software 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 23 */ 24 25/** 26 * @file 27 * QDM2 decoder 28 * @author Ewald Snel, Benjamin Larsson, Alex Beregszaszi, Roberto Togni 29 * 30 * The decoder is not perfect yet, there are still some distortions 31 * especially on files encoded with 16 or 8 subbands. 32 */ 33 34#include <math.h> 35#include <stddef.h> 36#include <stdio.h> 37 38#include "libavutil/channel_layout.h" 39#include "libavutil/mem_internal.h" 40#include "libavutil/thread.h" 41 42#define BITSTREAM_READER_LE 43#include "avcodec.h" 44#include "get_bits.h" 45#include "bytestream.h" 46#include "codec_internal.h" 47#include "internal.h" 48#include "mpegaudio.h" 49#include "mpegaudiodsp.h" 50#include "rdft.h" 51 52#include "qdm2_tablegen.h" 53 54#define QDM2_LIST_ADD(list, size, packet) \ 55do { \ 56 if (size > 0) { \ 57 list[size - 1].next = &list[size]; \ 58 } \ 59 list[size].packet = packet; \ 60 list[size].next = NULL; \ 61 size++; \ 62} while(0) 63 64// Result is 8, 16 or 30 65#define QDM2_SB_USED(sub_sampling) (((sub_sampling) >= 2) ? 30 : 8 << (sub_sampling)) 66 67#define FIX_NOISE_IDX(noise_idx) \ 68 if ((noise_idx) >= 3840) \ 69 (noise_idx) -= 3840; \ 70 71#define SB_DITHERING_NOISE(sb,noise_idx) (noise_table[(noise_idx)++] * sb_noise_attenuation[(sb)]) 72 73#define SAMPLES_NEEDED \ 74 av_log (NULL,AV_LOG_INFO,"This file triggers some untested code. Please contact the developers.\n"); 75 76#define SAMPLES_NEEDED_2(why) \ 77 av_log (NULL,AV_LOG_INFO,"This file triggers some missing code. Please contact the developers.\nPosition: %s\n",why); 78 79#define QDM2_MAX_FRAME_SIZE 512 80 81typedef int8_t sb_int8_array[2][30][64]; 82 83/** 84 * Subpacket 85 */ 86typedef struct QDM2SubPacket { 87 int type; ///< subpacket type 88 unsigned int size; ///< subpacket size 89 const uint8_t *data; ///< pointer to subpacket data (points to input data buffer, it's not a private copy) 90} QDM2SubPacket; 91 92/** 93 * A node in the subpacket list 94 */ 95typedef struct QDM2SubPNode { 96 QDM2SubPacket *packet; ///< packet 97 struct QDM2SubPNode *next; ///< pointer to next packet in the list, NULL if leaf node 98} QDM2SubPNode; 99 100typedef struct QDM2Complex { 101 float re; 102 float im; 103} QDM2Complex; 104 105typedef struct FFTTone { 106 float level; 107 QDM2Complex *complex; 108 const float *table; 109 int phase; 110 int phase_shift; 111 int duration; 112 short time_index; 113 short cutoff; 114} FFTTone; 115 116typedef struct FFTCoefficient { 117 int16_t sub_packet; 118 uint8_t channel; 119 int16_t offset; 120 int16_t exp; 121 uint8_t phase; 122} FFTCoefficient; 123 124typedef struct QDM2FFT { 125 DECLARE_ALIGNED(32, QDM2Complex, complex)[MPA_MAX_CHANNELS][256]; 126} QDM2FFT; 127 128/** 129 * QDM2 decoder context 130 */ 131typedef struct QDM2Context { 132 /// Parameters from codec header, do not change during playback 133 int nb_channels; ///< number of channels 134 int channels; ///< number of channels 135 int group_size; ///< size of frame group (16 frames per group) 136 int fft_size; ///< size of FFT, in complex numbers 137 int checksum_size; ///< size of data block, used also for checksum 138 139 /// Parameters built from header parameters, do not change during playback 140 int group_order; ///< order of frame group 141 int fft_order; ///< order of FFT (actually fftorder+1) 142 int frame_size; ///< size of data frame 143 int frequency_range; 144 int sub_sampling; ///< subsampling: 0=25%, 1=50%, 2=100% */ 145 int coeff_per_sb_select; ///< selector for "num. of coeffs. per subband" tables. Can be 0, 1, 2 146 int cm_table_select; ///< selector for "coding method" tables. Can be 0, 1 (from init: 0-4) 147 148 /// Packets and packet lists 149 QDM2SubPacket sub_packets[16]; ///< the packets themselves 150 QDM2SubPNode sub_packet_list_A[16]; ///< list of all packets 151 QDM2SubPNode sub_packet_list_B[16]; ///< FFT packets B are on list 152 int sub_packets_B; ///< number of packets on 'B' list 153 QDM2SubPNode sub_packet_list_C[16]; ///< packets with errors? 154 QDM2SubPNode sub_packet_list_D[16]; ///< DCT packets 155 156 /// FFT and tones 157 FFTTone fft_tones[1000]; 158 int fft_tone_start; 159 int fft_tone_end; 160 FFTCoefficient fft_coefs[1000]; 161 int fft_coefs_index; 162 int fft_coefs_min_index[5]; 163 int fft_coefs_max_index[5]; 164 int fft_level_exp[6]; 165 RDFTContext rdft_ctx; 166 QDM2FFT fft; 167 168 /// I/O data 169 const uint8_t *compressed_data; 170 int compressed_size; 171 float output_buffer[QDM2_MAX_FRAME_SIZE * MPA_MAX_CHANNELS * 2]; 172 173 /// Synthesis filter 174 MPADSPContext mpadsp; 175 DECLARE_ALIGNED(32, float, synth_buf)[MPA_MAX_CHANNELS][512*2]; 176 int synth_buf_offset[MPA_MAX_CHANNELS]; 177 DECLARE_ALIGNED(32, float, sb_samples)[MPA_MAX_CHANNELS][128][SBLIMIT]; 178 DECLARE_ALIGNED(32, float, samples)[MPA_MAX_CHANNELS * MPA_FRAME_SIZE]; 179 180 /// Mixed temporary data used in decoding 181 float tone_level[MPA_MAX_CHANNELS][30][64]; 182 int8_t coding_method[MPA_MAX_CHANNELS][30][64]; 183 int8_t quantized_coeffs[MPA_MAX_CHANNELS][10][8]; 184 int8_t tone_level_idx_base[MPA_MAX_CHANNELS][30][8]; 185 int8_t tone_level_idx_hi1[MPA_MAX_CHANNELS][3][8][8]; 186 int8_t tone_level_idx_mid[MPA_MAX_CHANNELS][26][8]; 187 int8_t tone_level_idx_hi2[MPA_MAX_CHANNELS][26]; 188 int8_t tone_level_idx[MPA_MAX_CHANNELS][30][64]; 189 int8_t tone_level_idx_temp[MPA_MAX_CHANNELS][30][64]; 190 191 // Flags 192 int has_errors; ///< packet has errors 193 int superblocktype_2_3; ///< select fft tables and some algorithm based on superblock type 194 int do_synth_filter; ///< used to perform or skip synthesis filter 195 196 int sub_packet; 197 int noise_idx; ///< index for dithering noise table 198} QDM2Context; 199 200static const int switchtable[23] = { 201 0, 5, 1, 5, 5, 5, 5, 5, 2, 5, 5, 5, 5, 5, 5, 5, 3, 5, 5, 5, 5, 5, 4 202}; 203 204static int qdm2_get_vlc(GetBitContext *gb, const VLC *vlc, int flag, int depth) 205{ 206 int value; 207 208 value = get_vlc2(gb, vlc->table, vlc->bits, depth); 209 210 /* stage-2, 3 bits exponent escape sequence */ 211 if (value < 0) 212 value = get_bits(gb, get_bits(gb, 3) + 1); 213 214 /* stage-3, optional */ 215 if (flag) { 216 int tmp; 217 218 if (value >= 60) { 219 av_log(NULL, AV_LOG_ERROR, "value %d in qdm2_get_vlc too large\n", value); 220 return 0; 221 } 222 223 tmp= vlc_stage3_values[value]; 224 225 if ((value & ~3) > 0) 226 tmp += get_bits(gb, (value >> 2)); 227 value = tmp; 228 } 229 230 return value; 231} 232 233static int qdm2_get_se_vlc(const VLC *vlc, GetBitContext *gb, int depth) 234{ 235 int value = qdm2_get_vlc(gb, vlc, 0, depth); 236 237 return (value & 1) ? ((value + 1) >> 1) : -(value >> 1); 238} 239 240/** 241 * QDM2 checksum 242 * 243 * @param data pointer to data to be checksummed 244 * @param length data length 245 * @param value checksum value 246 * 247 * @return 0 if checksum is OK 248 */ 249static uint16_t qdm2_packet_checksum(const uint8_t *data, int length, int value) 250{ 251 int i; 252 253 for (i = 0; i < length; i++) 254 value -= data[i]; 255 256 return (uint16_t)(value & 0xffff); 257} 258 259/** 260 * Fill a QDM2SubPacket structure with packet type, size, and data pointer. 261 * 262 * @param gb bitreader context 263 * @param sub_packet packet under analysis 264 */ 265static void qdm2_decode_sub_packet_header(GetBitContext *gb, 266 QDM2SubPacket *sub_packet) 267{ 268 sub_packet->type = get_bits(gb, 8); 269 270 if (sub_packet->type == 0) { 271 sub_packet->size = 0; 272 sub_packet->data = NULL; 273 } else { 274 sub_packet->size = get_bits(gb, 8); 275 276 if (sub_packet->type & 0x80) { 277 sub_packet->size <<= 8; 278 sub_packet->size |= get_bits(gb, 8); 279 sub_packet->type &= 0x7f; 280 } 281 282 if (sub_packet->type == 0x7f) 283 sub_packet->type |= (get_bits(gb, 8) << 8); 284 285 // FIXME: this depends on bitreader-internal data 286 sub_packet->data = &gb->buffer[get_bits_count(gb) / 8]; 287 } 288 289 av_log(NULL, AV_LOG_DEBUG, "Subpacket: type=%d size=%d start_offs=%x\n", 290 sub_packet->type, sub_packet->size, get_bits_count(gb) / 8); 291} 292 293/** 294 * Return node pointer to first packet of requested type in list. 295 * 296 * @param list list of subpackets to be scanned 297 * @param type type of searched subpacket 298 * @return node pointer for subpacket if found, else NULL 299 */ 300static QDM2SubPNode *qdm2_search_subpacket_type_in_list(QDM2SubPNode *list, 301 int type) 302{ 303 while (list && list->packet) { 304 if (list->packet->type == type) 305 return list; 306 list = list->next; 307 } 308 return NULL; 309} 310 311/** 312 * Replace 8 elements with their average value. 313 * Called by qdm2_decode_superblock before starting subblock decoding. 314 * 315 * @param q context 316 */ 317static void average_quantized_coeffs(QDM2Context *q) 318{ 319 int i, j, n, ch, sum; 320 321 n = coeff_per_sb_for_avg[q->coeff_per_sb_select][QDM2_SB_USED(q->sub_sampling) - 1] + 1; 322 323 for (ch = 0; ch < q->nb_channels; ch++) 324 for (i = 0; i < n; i++) { 325 sum = 0; 326 327 for (j = 0; j < 8; j++) 328 sum += q->quantized_coeffs[ch][i][j]; 329 330 sum /= 8; 331 if (sum > 0) 332 sum--; 333 334 for (j = 0; j < 8; j++) 335 q->quantized_coeffs[ch][i][j] = sum; 336 } 337} 338 339/** 340 * Build subband samples with noise weighted by q->tone_level. 341 * Called by synthfilt_build_sb_samples. 342 * 343 * @param q context 344 * @param sb subband index 345 */ 346static void build_sb_samples_from_noise(QDM2Context *q, int sb) 347{ 348 int ch, j; 349 350 FIX_NOISE_IDX(q->noise_idx); 351 352 if (!q->nb_channels) 353 return; 354 355 for (ch = 0; ch < q->nb_channels; ch++) { 356 for (j = 0; j < 64; j++) { 357 q->sb_samples[ch][j * 2][sb] = 358 SB_DITHERING_NOISE(sb, q->noise_idx) * q->tone_level[ch][sb][j]; 359 q->sb_samples[ch][j * 2 + 1][sb] = 360 SB_DITHERING_NOISE(sb, q->noise_idx) * q->tone_level[ch][sb][j]; 361 } 362 } 363} 364 365/** 366 * Called while processing data from subpackets 11 and 12. 367 * Used after making changes to coding_method array. 368 * 369 * @param sb subband index 370 * @param channels number of channels 371 * @param coding_method q->coding_method[0][0][0] 372 */ 373static int fix_coding_method_array(int sb, int channels, 374 sb_int8_array coding_method) 375{ 376 int j, k; 377 int ch; 378 int run, case_val; 379 380 for (ch = 0; ch < channels; ch++) { 381 for (j = 0; j < 64; ) { 382 if (coding_method[ch][sb][j] < 8) 383 return -1; 384 if ((coding_method[ch][sb][j] - 8) > 22) { 385 run = 1; 386 case_val = 8; 387 } else { 388 switch (switchtable[coding_method[ch][sb][j] - 8]) { 389 case 0: run = 10; 390 case_val = 10; 391 break; 392 case 1: run = 1; 393 case_val = 16; 394 break; 395 case 2: run = 5; 396 case_val = 24; 397 break; 398 case 3: run = 3; 399 case_val = 30; 400 break; 401 case 4: run = 1; 402 case_val = 30; 403 break; 404 case 5: run = 1; 405 case_val = 8; 406 break; 407 default: run = 1; 408 case_val = 8; 409 break; 410 } 411 } 412 for (k = 0; k < run; k++) { 413 if (j + k < 128) { 414 int sbjk = sb + (j + k) / 64; 415 if (sbjk > 29) { 416 SAMPLES_NEEDED 417 continue; 418 } 419 if (coding_method[ch][sbjk][(j + k) % 64] > coding_method[ch][sb][j]) { 420 if (k > 0) { 421 SAMPLES_NEEDED 422 //not debugged, almost never used 423 memset(&coding_method[ch][sb][j + k], case_val, 424 k *sizeof(int8_t)); 425 memset(&coding_method[ch][sb][j + k], case_val, 426 3 * sizeof(int8_t)); 427 } 428 } 429 } 430 } 431 j += run; 432 } 433 } 434 return 0; 435} 436 437/** 438 * Related to synthesis filter 439 * Called by process_subpacket_10 440 * 441 * @param q context 442 * @param flag 1 if called after getting data from subpacket 10, 0 if no subpacket 10 443 */ 444static void fill_tone_level_array(QDM2Context *q, int flag) 445{ 446 int i, sb, ch, sb_used; 447 int tmp, tab; 448 449 for (ch = 0; ch < q->nb_channels; ch++) 450 for (sb = 0; sb < 30; sb++) 451 for (i = 0; i < 8; i++) { 452 if ((tab=coeff_per_sb_for_dequant[q->coeff_per_sb_select][sb]) < (last_coeff[q->coeff_per_sb_select] - 1)) 453 tmp = q->quantized_coeffs[ch][tab + 1][i] * dequant_table[q->coeff_per_sb_select][tab + 1][sb]+ 454 q->quantized_coeffs[ch][tab][i] * dequant_table[q->coeff_per_sb_select][tab][sb]; 455 else 456 tmp = q->quantized_coeffs[ch][tab][i] * dequant_table[q->coeff_per_sb_select][tab][sb]; 457 if(tmp < 0) 458 tmp += 0xff; 459 q->tone_level_idx_base[ch][sb][i] = (tmp / 256) & 0xff; 460 } 461 462 sb_used = QDM2_SB_USED(q->sub_sampling); 463 464 if ((q->superblocktype_2_3 != 0) && !flag) { 465 for (sb = 0; sb < sb_used; sb++) 466 for (ch = 0; ch < q->nb_channels; ch++) 467 for (i = 0; i < 64; i++) { 468 q->tone_level_idx[ch][sb][i] = q->tone_level_idx_base[ch][sb][i / 8]; 469 if (q->tone_level_idx[ch][sb][i] < 0) 470 q->tone_level[ch][sb][i] = 0; 471 else 472 q->tone_level[ch][sb][i] = fft_tone_level_table[0][q->tone_level_idx[ch][sb][i] & 0x3f]; 473 } 474 } else { 475 tab = q->superblocktype_2_3 ? 0 : 1; 476 for (sb = 0; sb < sb_used; sb++) { 477 if ((sb >= 4) && (sb <= 23)) { 478 for (ch = 0; ch < q->nb_channels; ch++) 479 for (i = 0; i < 64; i++) { 480 tmp = q->tone_level_idx_base[ch][sb][i / 8] - 481 q->tone_level_idx_hi1[ch][sb / 8][i / 8][i % 8] - 482 q->tone_level_idx_mid[ch][sb - 4][i / 8] - 483 q->tone_level_idx_hi2[ch][sb - 4]; 484 q->tone_level_idx[ch][sb][i] = tmp & 0xff; 485 if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp)) 486 q->tone_level[ch][sb][i] = 0; 487 else 488 q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f]; 489 } 490 } else { 491 if (sb > 4) { 492 for (ch = 0; ch < q->nb_channels; ch++) 493 for (i = 0; i < 64; i++) { 494 tmp = q->tone_level_idx_base[ch][sb][i / 8] - 495 q->tone_level_idx_hi1[ch][2][i / 8][i % 8] - 496 q->tone_level_idx_hi2[ch][sb - 4]; 497 q->tone_level_idx[ch][sb][i] = tmp & 0xff; 498 if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp)) 499 q->tone_level[ch][sb][i] = 0; 500 else 501 q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f]; 502 } 503 } else { 504 for (ch = 0; ch < q->nb_channels; ch++) 505 for (i = 0; i < 64; i++) { 506 tmp = q->tone_level_idx[ch][sb][i] = q->tone_level_idx_base[ch][sb][i / 8]; 507 if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp)) 508 q->tone_level[ch][sb][i] = 0; 509 else 510 q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f]; 511 } 512 } 513 } 514 } 515 } 516} 517 518/** 519 * Related to synthesis filter 520 * Called by process_subpacket_11 521 * c is built with data from subpacket 11 522 * Most of this function is used only if superblock_type_2_3 == 0, 523 * never seen it in samples. 524 * 525 * @param tone_level_idx 526 * @param tone_level_idx_temp 527 * @param coding_method q->coding_method[0][0][0] 528 * @param nb_channels number of channels 529 * @param c coming from subpacket 11, passed as 8*c 530 * @param superblocktype_2_3 flag based on superblock packet type 531 * @param cm_table_select q->cm_table_select 532 */ 533static void fill_coding_method_array(sb_int8_array tone_level_idx, 534 sb_int8_array tone_level_idx_temp, 535 sb_int8_array coding_method, 536 int nb_channels, 537 int c, int superblocktype_2_3, 538 int cm_table_select) 539{ 540 int ch, sb, j; 541 int tmp, acc, esp_40, comp; 542 int add1, add2, add3, add4; 543 int64_t multres; 544 545 if (!superblocktype_2_3) { 546 /* This case is untested, no samples available */ 547 avpriv_request_sample(NULL, "!superblocktype_2_3"); 548 return; 549 for (ch = 0; ch < nb_channels; ch++) { 550 for (sb = 0; sb < 30; sb++) { 551 for (j = 1; j < 63; j++) { // The loop only iterates to 63 so the code doesn't overflow the buffer 552 add1 = tone_level_idx[ch][sb][j] - 10; 553 if (add1 < 0) 554 add1 = 0; 555 add2 = add3 = add4 = 0; 556 if (sb > 1) { 557 add2 = tone_level_idx[ch][sb - 2][j] + tone_level_idx_offset_table[sb][0] - 6; 558 if (add2 < 0) 559 add2 = 0; 560 } 561 if (sb > 0) { 562 add3 = tone_level_idx[ch][sb - 1][j] + tone_level_idx_offset_table[sb][1] - 6; 563 if (add3 < 0) 564 add3 = 0; 565 } 566 if (sb < 29) { 567 add4 = tone_level_idx[ch][sb + 1][j] + tone_level_idx_offset_table[sb][3] - 6; 568 if (add4 < 0) 569 add4 = 0; 570 } 571 tmp = tone_level_idx[ch][sb][j + 1] * 2 - add4 - add3 - add2 - add1; 572 if (tmp < 0) 573 tmp = 0; 574 tone_level_idx_temp[ch][sb][j + 1] = tmp & 0xff; 575 } 576 tone_level_idx_temp[ch][sb][0] = tone_level_idx_temp[ch][sb][1]; 577 } 578 } 579 acc = 0; 580 for (ch = 0; ch < nb_channels; ch++) 581 for (sb = 0; sb < 30; sb++) 582 for (j = 0; j < 64; j++) 583 acc += tone_level_idx_temp[ch][sb][j]; 584 585 multres = 0x66666667LL * (acc * 10); 586 esp_40 = (multres >> 32) / 8 + ((multres & 0xffffffff) >> 31); 587 for (ch = 0; ch < nb_channels; ch++) 588 for (sb = 0; sb < 30; sb++) 589 for (j = 0; j < 64; j++) { 590 comp = tone_level_idx_temp[ch][sb][j]* esp_40 * 10; 591 if (comp < 0) 592 comp += 0xff; 593 comp /= 256; // signed shift 594 switch(sb) { 595 case 0: 596 if (comp < 30) 597 comp = 30; 598 comp += 15; 599 break; 600 case 1: 601 if (comp < 24) 602 comp = 24; 603 comp += 10; 604 break; 605 case 2: 606 case 3: 607 case 4: 608 if (comp < 16) 609 comp = 16; 610 } 611 if (comp <= 5) 612 tmp = 0; 613 else if (comp <= 10) 614 tmp = 10; 615 else if (comp <= 16) 616 tmp = 16; 617 else if (comp <= 24) 618 tmp = -1; 619 else 620 tmp = 0; 621 coding_method[ch][sb][j] = ((tmp & 0xfffa) + 30 )& 0xff; 622 } 623 for (sb = 0; sb < 30; sb++) 624 fix_coding_method_array(sb, nb_channels, coding_method); 625 for (ch = 0; ch < nb_channels; ch++) 626 for (sb = 0; sb < 30; sb++) 627 for (j = 0; j < 64; j++) 628 if (sb >= 10) { 629 if (coding_method[ch][sb][j] < 10) 630 coding_method[ch][sb][j] = 10; 631 } else { 632 if (sb >= 2) { 633 if (coding_method[ch][sb][j] < 16) 634 coding_method[ch][sb][j] = 16; 635 } else { 636 if (coding_method[ch][sb][j] < 30) 637 coding_method[ch][sb][j] = 30; 638 } 639 } 640 } else { // superblocktype_2_3 != 0 641 for (ch = 0; ch < nb_channels; ch++) 642 for (sb = 0; sb < 30; sb++) 643 for (j = 0; j < 64; j++) 644 coding_method[ch][sb][j] = coding_method_table[cm_table_select][sb]; 645 } 646} 647 648/** 649 * Called by process_subpacket_11 to process more data from subpacket 11 650 * with sb 0-8. 651 * Called by process_subpacket_12 to process data from subpacket 12 with 652 * sb 8-sb_used. 653 * 654 * @param q context 655 * @param gb bitreader context 656 * @param length packet length in bits 657 * @param sb_min lower subband processed (sb_min included) 658 * @param sb_max higher subband processed (sb_max excluded) 659 */ 660static int synthfilt_build_sb_samples(QDM2Context *q, GetBitContext *gb, 661 int length, int sb_min, int sb_max) 662{ 663 int sb, j, k, n, ch, run, channels; 664 int joined_stereo, zero_encoding; 665 int type34_first; 666 float type34_div = 0; 667 float type34_predictor; 668 float samples[10]; 669 int sign_bits[16] = {0}; 670 671 if (length == 0) { 672 // If no data use noise 673 for (sb=sb_min; sb < sb_max; sb++) 674 build_sb_samples_from_noise(q, sb); 675 676 return 0; 677 } 678 679 for (sb = sb_min; sb < sb_max; sb++) { 680 channels = q->nb_channels; 681 682 if (q->nb_channels <= 1 || sb < 12) 683 joined_stereo = 0; 684 else if (sb >= 24) 685 joined_stereo = 1; 686 else 687 joined_stereo = (get_bits_left(gb) >= 1) ? get_bits1(gb) : 0; 688 689 if (joined_stereo) { 690 if (get_bits_left(gb) >= 16) 691 for (j = 0; j < 16; j++) 692 sign_bits[j] = get_bits1(gb); 693 694 for (j = 0; j < 64; j++) 695 if (q->coding_method[1][sb][j] > q->coding_method[0][sb][j]) 696 q->coding_method[0][sb][j] = q->coding_method[1][sb][j]; 697 698 if (fix_coding_method_array(sb, q->nb_channels, 699 q->coding_method)) { 700 av_log(NULL, AV_LOG_ERROR, "coding method invalid\n"); 701 build_sb_samples_from_noise(q, sb); 702 continue; 703 } 704 channels = 1; 705 } 706 707 for (ch = 0; ch < channels; ch++) { 708 FIX_NOISE_IDX(q->noise_idx); 709 zero_encoding = (get_bits_left(gb) >= 1) ? get_bits1(gb) : 0; 710 type34_predictor = 0.0; 711 type34_first = 1; 712 713 for (j = 0; j < 128; ) { 714 switch (q->coding_method[ch][sb][j / 2]) { 715 case 8: 716 if (get_bits_left(gb) >= 10) { 717 if (zero_encoding) { 718 for (k = 0; k < 5; k++) { 719 if ((j + 2 * k) >= 128) 720 break; 721 samples[2 * k] = get_bits1(gb) ? dequant_1bit[joined_stereo][2 * get_bits1(gb)] : 0; 722 } 723 } else { 724 n = get_bits(gb, 8); 725 if (n >= 243) { 726 av_log(NULL, AV_LOG_ERROR, "Invalid 8bit codeword\n"); 727 return AVERROR_INVALIDDATA; 728 } 729 730 for (k = 0; k < 5; k++) 731 samples[2 * k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]]; 732 } 733 for (k = 0; k < 5; k++) 734 samples[2 * k + 1] = SB_DITHERING_NOISE(sb,q->noise_idx); 735 } else { 736 for (k = 0; k < 10; k++) 737 samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx); 738 } 739 run = 10; 740 break; 741 742 case 10: 743 if (get_bits_left(gb) >= 1) { 744 float f = 0.81; 745 746 if (get_bits1(gb)) 747 f = -f; 748 f -= noise_samples[((sb + 1) * (j +5 * ch + 1)) & 127] * 9.0 / 40.0; 749 samples[0] = f; 750 } else { 751 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx); 752 } 753 run = 1; 754 break; 755 756 case 16: 757 if (get_bits_left(gb) >= 10) { 758 if (zero_encoding) { 759 for (k = 0; k < 5; k++) { 760 if ((j + k) >= 128) 761 break; 762 samples[k] = (get_bits1(gb) == 0) ? 0 : dequant_1bit[joined_stereo][2 * get_bits1(gb)]; 763 } 764 } else { 765 n = get_bits (gb, 8); 766 if (n >= 243) { 767 av_log(NULL, AV_LOG_ERROR, "Invalid 8bit codeword\n"); 768 return AVERROR_INVALIDDATA; 769 } 770 771 for (k = 0; k < 5; k++) 772 samples[k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]]; 773 } 774 } else { 775 for (k = 0; k < 5; k++) 776 samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx); 777 } 778 run = 5; 779 break; 780 781 case 24: 782 if (get_bits_left(gb) >= 7) { 783 n = get_bits(gb, 7); 784 if (n >= 125) { 785 av_log(NULL, AV_LOG_ERROR, "Invalid 7bit codeword\n"); 786 return AVERROR_INVALIDDATA; 787 } 788 789 for (k = 0; k < 3; k++) 790 samples[k] = (random_dequant_type24[n][k] - 2.0) * 0.5; 791 } else { 792 for (k = 0; k < 3; k++) 793 samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx); 794 } 795 run = 3; 796 break; 797 798 case 30: 799 if (get_bits_left(gb) >= 4) { 800 unsigned index = qdm2_get_vlc(gb, &vlc_tab_type30, 0, 1); 801 if (index >= FF_ARRAY_ELEMS(type30_dequant)) { 802 av_log(NULL, AV_LOG_ERROR, "index %d out of type30_dequant array\n", index); 803 return AVERROR_INVALIDDATA; 804 } 805 samples[0] = type30_dequant[index]; 806 } else 807 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx); 808 809 run = 1; 810 break; 811 812 case 34: 813 if (get_bits_left(gb) >= 7) { 814 if (type34_first) { 815 type34_div = (float)(1 << get_bits(gb, 2)); 816 samples[0] = ((float)get_bits(gb, 5) - 16.0) / 15.0; 817 type34_predictor = samples[0]; 818 type34_first = 0; 819 } else { 820 unsigned index = qdm2_get_vlc(gb, &vlc_tab_type34, 0, 1); 821 if (index >= FF_ARRAY_ELEMS(type34_delta)) { 822 av_log(NULL, AV_LOG_ERROR, "index %d out of type34_delta array\n", index); 823 return AVERROR_INVALIDDATA; 824 } 825 samples[0] = type34_delta[index] / type34_div + type34_predictor; 826 type34_predictor = samples[0]; 827 } 828 } else { 829 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx); 830 } 831 run = 1; 832 break; 833 834 default: 835 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx); 836 run = 1; 837 break; 838 } 839 840 if (joined_stereo) { 841 for (k = 0; k < run && j + k < 128; k++) { 842 q->sb_samples[0][j + k][sb] = 843 q->tone_level[0][sb][(j + k) / 2] * samples[k]; 844 if (q->nb_channels == 2) { 845 if (sign_bits[(j + k) / 8]) 846 q->sb_samples[1][j + k][sb] = 847 q->tone_level[1][sb][(j + k) / 2] * -samples[k]; 848 else 849 q->sb_samples[1][j + k][sb] = 850 q->tone_level[1][sb][(j + k) / 2] * samples[k]; 851 } 852 } 853 } else { 854 for (k = 0; k < run; k++) 855 if ((j + k) < 128) 856 q->sb_samples[ch][j + k][sb] = q->tone_level[ch][sb][(j + k)/2] * samples[k]; 857 } 858 859 j += run; 860 } // j loop 861 } // channel loop 862 } // subband loop 863 return 0; 864} 865 866/** 867 * Init the first element of a channel in quantized_coeffs with data 868 * from packet 10 (quantized_coeffs[ch][0]). 869 * This is similar to process_subpacket_9, but for a single channel 870 * and for element [0] 871 * same VLC tables as process_subpacket_9 are used. 872 * 873 * @param quantized_coeffs pointer to quantized_coeffs[ch][0] 874 * @param gb bitreader context 875 */ 876static int init_quantized_coeffs_elem0(int8_t *quantized_coeffs, 877 GetBitContext *gb) 878{ 879 int i, k, run, level, diff; 880 881 if (get_bits_left(gb) < 16) 882 return -1; 883 level = qdm2_get_vlc(gb, &vlc_tab_level, 0, 2); 884 885 quantized_coeffs[0] = level; 886 887 for (i = 0; i < 7; ) { 888 if (get_bits_left(gb) < 16) 889 return -1; 890 run = qdm2_get_vlc(gb, &vlc_tab_run, 0, 1) + 1; 891 892 if (i + run >= 8) 893 return -1; 894 895 if (get_bits_left(gb) < 16) 896 return -1; 897 diff = qdm2_get_se_vlc(&vlc_tab_diff, gb, 2); 898 899 for (k = 1; k <= run; k++) 900 quantized_coeffs[i + k] = (level + ((k * diff) / run)); 901 902 level += diff; 903 i += run; 904 } 905 return 0; 906} 907 908/** 909 * Related to synthesis filter, process data from packet 10 910 * Init part of quantized_coeffs via function init_quantized_coeffs_elem0 911 * Init tone_level_idx_hi1, tone_level_idx_hi2, tone_level_idx_mid with 912 * data from packet 10 913 * 914 * @param q context 915 * @param gb bitreader context 916 */ 917static void init_tone_level_dequantization(QDM2Context *q, GetBitContext *gb) 918{ 919 int sb, j, k, n, ch; 920 921 for (ch = 0; ch < q->nb_channels; ch++) { 922 init_quantized_coeffs_elem0(q->quantized_coeffs[ch][0], gb); 923 924 if (get_bits_left(gb) < 16) { 925 memset(q->quantized_coeffs[ch][0], 0, 8); 926 break; 927 } 928 } 929 930 n = q->sub_sampling + 1; 931 932 for (sb = 0; sb < n; sb++) 933 for (ch = 0; ch < q->nb_channels; ch++) 934 for (j = 0; j < 8; j++) { 935 if (get_bits_left(gb) < 1) 936 break; 937 if (get_bits1(gb)) { 938 for (k=0; k < 8; k++) { 939 if (get_bits_left(gb) < 16) 940 break; 941 q->tone_level_idx_hi1[ch][sb][j][k] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_hi1, 0, 2); 942 } 943 } else { 944 for (k=0; k < 8; k++) 945 q->tone_level_idx_hi1[ch][sb][j][k] = 0; 946 } 947 } 948 949 n = QDM2_SB_USED(q->sub_sampling) - 4; 950 951 for (sb = 0; sb < n; sb++) 952 for (ch = 0; ch < q->nb_channels; ch++) { 953 if (get_bits_left(gb) < 16) 954 break; 955 q->tone_level_idx_hi2[ch][sb] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_hi2, 0, 2); 956 if (sb > 19) 957 q->tone_level_idx_hi2[ch][sb] -= 16; 958 else 959 for (j = 0; j < 8; j++) 960 q->tone_level_idx_mid[ch][sb][j] = -16; 961 } 962 963 n = QDM2_SB_USED(q->sub_sampling) - 5; 964 965 for (sb = 0; sb < n; sb++) 966 for (ch = 0; ch < q->nb_channels; ch++) 967 for (j = 0; j < 8; j++) { 968 if (get_bits_left(gb) < 16) 969 break; 970 q->tone_level_idx_mid[ch][sb][j] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_mid, 0, 2) - 32; 971 } 972} 973 974/** 975 * Process subpacket 9, init quantized_coeffs with data from it 976 * 977 * @param q context 978 * @param node pointer to node with packet 979 */ 980static int process_subpacket_9(QDM2Context *q, QDM2SubPNode *node) 981{ 982 GetBitContext gb; 983 int i, j, k, n, ch, run, level, diff; 984 985 init_get_bits(&gb, node->packet->data, node->packet->size * 8); 986 987 n = coeff_per_sb_for_avg[q->coeff_per_sb_select][QDM2_SB_USED(q->sub_sampling) - 1] + 1; 988 989 for (i = 1; i < n; i++) 990 for (ch = 0; ch < q->nb_channels; ch++) { 991 level = qdm2_get_vlc(&gb, &vlc_tab_level, 0, 2); 992 q->quantized_coeffs[ch][i][0] = level; 993 994 for (j = 0; j < (8 - 1); ) { 995 run = qdm2_get_vlc(&gb, &vlc_tab_run, 0, 1) + 1; 996 diff = qdm2_get_se_vlc(&vlc_tab_diff, &gb, 2); 997 998 if (j + run >= 8) 999 return -1; 1000 1001 for (k = 1; k <= run; k++) 1002 q->quantized_coeffs[ch][i][j + k] = (level + ((k * diff) / run)); 1003 1004 level += diff; 1005 j += run; 1006 } 1007 } 1008 1009 for (ch = 0; ch < q->nb_channels; ch++) 1010 for (i = 0; i < 8; i++) 1011 q->quantized_coeffs[ch][0][i] = 0; 1012 1013 return 0; 1014} 1015 1016/** 1017 * Process subpacket 10 if not null, else 1018 * 1019 * @param q context 1020 * @param node pointer to node with packet 1021 */ 1022static void process_subpacket_10(QDM2Context *q, QDM2SubPNode *node) 1023{ 1024 GetBitContext gb; 1025 1026 if (node) { 1027 init_get_bits(&gb, node->packet->data, node->packet->size * 8); 1028 init_tone_level_dequantization(q, &gb); 1029 fill_tone_level_array(q, 1); 1030 } else { 1031 fill_tone_level_array(q, 0); 1032 } 1033} 1034 1035/** 1036 * Process subpacket 11 1037 * 1038 * @param q context 1039 * @param node pointer to node with packet 1040 */ 1041static void process_subpacket_11(QDM2Context *q, QDM2SubPNode *node) 1042{ 1043 GetBitContext gb; 1044 int length = 0; 1045 1046 if (node) { 1047 length = node->packet->size * 8; 1048 init_get_bits(&gb, node->packet->data, length); 1049 } 1050 1051 if (length >= 32) { 1052 int c = get_bits(&gb, 13); 1053 1054 if (c > 3) 1055 fill_coding_method_array(q->tone_level_idx, 1056 q->tone_level_idx_temp, q->coding_method, 1057 q->nb_channels, 8 * c, 1058 q->superblocktype_2_3, q->cm_table_select); 1059 } 1060 1061 synthfilt_build_sb_samples(q, &gb, length, 0, 8); 1062} 1063 1064/** 1065 * Process subpacket 12 1066 * 1067 * @param q context 1068 * @param node pointer to node with packet 1069 */ 1070static void process_subpacket_12(QDM2Context *q, QDM2SubPNode *node) 1071{ 1072 GetBitContext gb; 1073 int length = 0; 1074 1075 if (node) { 1076 length = node->packet->size * 8; 1077 init_get_bits(&gb, node->packet->data, length); 1078 } 1079 1080 synthfilt_build_sb_samples(q, &gb, length, 8, QDM2_SB_USED(q->sub_sampling)); 1081} 1082 1083/** 1084 * Process new subpackets for synthesis filter 1085 * 1086 * @param q context 1087 * @param list list with synthesis filter packets (list D) 1088 */ 1089static void process_synthesis_subpackets(QDM2Context *q, QDM2SubPNode *list) 1090{ 1091 QDM2SubPNode *nodes[4]; 1092 1093 nodes[0] = qdm2_search_subpacket_type_in_list(list, 9); 1094 if (nodes[0]) 1095 process_subpacket_9(q, nodes[0]); 1096 1097 nodes[1] = qdm2_search_subpacket_type_in_list(list, 10); 1098 if (nodes[1]) 1099 process_subpacket_10(q, nodes[1]); 1100 else 1101 process_subpacket_10(q, NULL); 1102 1103 nodes[2] = qdm2_search_subpacket_type_in_list(list, 11); 1104 if (nodes[0] && nodes[1] && nodes[2]) 1105 process_subpacket_11(q, nodes[2]); 1106 else 1107 process_subpacket_11(q, NULL); 1108 1109 nodes[3] = qdm2_search_subpacket_type_in_list(list, 12); 1110 if (nodes[0] && nodes[1] && nodes[3]) 1111 process_subpacket_12(q, nodes[3]); 1112 else 1113 process_subpacket_12(q, NULL); 1114} 1115 1116/** 1117 * Decode superblock, fill packet lists. 1118 * 1119 * @param q context 1120 */ 1121static void qdm2_decode_super_block(QDM2Context *q) 1122{ 1123 GetBitContext gb; 1124 QDM2SubPacket header, *packet; 1125 int i, packet_bytes, sub_packet_size, sub_packets_D; 1126 unsigned int next_index = 0; 1127 1128 memset(q->tone_level_idx_hi1, 0, sizeof(q->tone_level_idx_hi1)); 1129 memset(q->tone_level_idx_mid, 0, sizeof(q->tone_level_idx_mid)); 1130 memset(q->tone_level_idx_hi2, 0, sizeof(q->tone_level_idx_hi2)); 1131 1132 q->sub_packets_B = 0; 1133 sub_packets_D = 0; 1134 1135 average_quantized_coeffs(q); // average elements in quantized_coeffs[max_ch][10][8] 1136 1137 init_get_bits(&gb, q->compressed_data, q->compressed_size * 8); 1138 qdm2_decode_sub_packet_header(&gb, &header); 1139 1140 if (header.type < 2 || header.type >= 8) { 1141 q->has_errors = 1; 1142 av_log(NULL, AV_LOG_ERROR, "bad superblock type\n"); 1143 return; 1144 } 1145 1146 q->superblocktype_2_3 = (header.type == 2 || header.type == 3); 1147 packet_bytes = (q->compressed_size - get_bits_count(&gb) / 8); 1148 1149 init_get_bits(&gb, header.data, header.size * 8); 1150 1151 if (header.type == 2 || header.type == 4 || header.type == 5) { 1152 int csum = 257 * get_bits(&gb, 8); 1153 csum += 2 * get_bits(&gb, 8); 1154 1155 csum = qdm2_packet_checksum(q->compressed_data, q->checksum_size, csum); 1156 1157 if (csum != 0) { 1158 q->has_errors = 1; 1159 av_log(NULL, AV_LOG_ERROR, "bad packet checksum\n"); 1160 return; 1161 } 1162 } 1163 1164 q->sub_packet_list_B[0].packet = NULL; 1165 q->sub_packet_list_D[0].packet = NULL; 1166 1167 for (i = 0; i < 6; i++) 1168 if (--q->fft_level_exp[i] < 0) 1169 q->fft_level_exp[i] = 0; 1170 1171 for (i = 0; packet_bytes > 0; i++) { 1172 int j; 1173 1174 if (i >= FF_ARRAY_ELEMS(q->sub_packet_list_A)) { 1175 SAMPLES_NEEDED_2("too many packet bytes"); 1176 return; 1177 } 1178 1179 q->sub_packet_list_A[i].next = NULL; 1180 1181 if (i > 0) { 1182 q->sub_packet_list_A[i - 1].next = &q->sub_packet_list_A[i]; 1183 1184 /* seek to next block */ 1185 init_get_bits(&gb, header.data, header.size * 8); 1186 skip_bits(&gb, next_index * 8); 1187 1188 if (next_index >= header.size) 1189 break; 1190 } 1191 1192 /* decode subpacket */ 1193 packet = &q->sub_packets[i]; 1194 qdm2_decode_sub_packet_header(&gb, packet); 1195 next_index = packet->size + get_bits_count(&gb) / 8; 1196 sub_packet_size = ((packet->size > 0xff) ? 1 : 0) + packet->size + 2; 1197 1198 if (packet->type == 0) 1199 break; 1200 1201 if (sub_packet_size > packet_bytes) { 1202 if (packet->type != 10 && packet->type != 11 && packet->type != 12) 1203 break; 1204 packet->size += packet_bytes - sub_packet_size; 1205 } 1206 1207 packet_bytes -= sub_packet_size; 1208 1209 /* add subpacket to 'all subpackets' list */ 1210 q->sub_packet_list_A[i].packet = packet; 1211 1212 /* add subpacket to related list */ 1213 if (packet->type == 8) { 1214 SAMPLES_NEEDED_2("packet type 8"); 1215 return; 1216 } else if (packet->type >= 9 && packet->type <= 12) { 1217 /* packets for MPEG Audio like Synthesis Filter */ 1218 QDM2_LIST_ADD(q->sub_packet_list_D, sub_packets_D, packet); 1219 } else if (packet->type == 13) { 1220 for (j = 0; j < 6; j++) 1221 q->fft_level_exp[j] = get_bits(&gb, 6); 1222 } else if (packet->type == 14) { 1223 for (j = 0; j < 6; j++) 1224 q->fft_level_exp[j] = qdm2_get_vlc(&gb, &fft_level_exp_vlc, 0, 2); 1225 } else if (packet->type == 15) { 1226 SAMPLES_NEEDED_2("packet type 15") 1227 return; 1228 } else if (packet->type >= 16 && packet->type < 48 && 1229 !fft_subpackets[packet->type - 16]) { 1230 /* packets for FFT */ 1231 QDM2_LIST_ADD(q->sub_packet_list_B, q->sub_packets_B, packet); 1232 } 1233 } // Packet bytes loop 1234 1235 if (q->sub_packet_list_D[0].packet) { 1236 process_synthesis_subpackets(q, q->sub_packet_list_D); 1237 q->do_synth_filter = 1; 1238 } else if (q->do_synth_filter) { 1239 process_subpacket_10(q, NULL); 1240 process_subpacket_11(q, NULL); 1241 process_subpacket_12(q, NULL); 1242 } 1243} 1244 1245static void qdm2_fft_init_coefficient(QDM2Context *q, int sub_packet, 1246 int offset, int duration, int channel, 1247 int exp, int phase) 1248{ 1249 if (q->fft_coefs_min_index[duration] < 0) 1250 q->fft_coefs_min_index[duration] = q->fft_coefs_index; 1251 1252 q->fft_coefs[q->fft_coefs_index].sub_packet = 1253 ((sub_packet >= 16) ? (sub_packet - 16) : sub_packet); 1254 q->fft_coefs[q->fft_coefs_index].channel = channel; 1255 q->fft_coefs[q->fft_coefs_index].offset = offset; 1256 q->fft_coefs[q->fft_coefs_index].exp = exp; 1257 q->fft_coefs[q->fft_coefs_index].phase = phase; 1258 q->fft_coefs_index++; 1259} 1260 1261static void qdm2_fft_decode_tones(QDM2Context *q, int duration, 1262 GetBitContext *gb, int b) 1263{ 1264 int channel, stereo, phase, exp; 1265 int local_int_4, local_int_8, stereo_phase, local_int_10; 1266 int local_int_14, stereo_exp, local_int_20, local_int_28; 1267 int n, offset; 1268 1269 local_int_4 = 0; 1270 local_int_28 = 0; 1271 local_int_20 = 2; 1272 local_int_8 = (4 - duration); 1273 local_int_10 = 1 << (q->group_order - duration - 1); 1274 offset = 1; 1275 1276 while (get_bits_left(gb)>0) { 1277 if (q->superblocktype_2_3) { 1278 while ((n = qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2)) < 2) { 1279 if (get_bits_left(gb)<0) { 1280 if(local_int_4 < q->group_size) 1281 av_log(NULL, AV_LOG_ERROR, "overread in qdm2_fft_decode_tones()\n"); 1282 return; 1283 } 1284 offset = 1; 1285 if (n == 0) { 1286 local_int_4 += local_int_10; 1287 local_int_28 += (1 << local_int_8); 1288 } else { 1289 local_int_4 += 8 * local_int_10; 1290 local_int_28 += (8 << local_int_8); 1291 } 1292 } 1293 offset += (n - 2); 1294 } else { 1295 if (local_int_10 <= 2) { 1296 av_log(NULL, AV_LOG_ERROR, "qdm2_fft_decode_tones() stuck\n"); 1297 return; 1298 } 1299 offset += qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2); 1300 while (offset >= (local_int_10 - 1)) { 1301 offset += (1 - (local_int_10 - 1)); 1302 local_int_4 += local_int_10; 1303 local_int_28 += (1 << local_int_8); 1304 } 1305 } 1306 1307 if (local_int_4 >= q->group_size) 1308 return; 1309 1310 local_int_14 = (offset >> local_int_8); 1311 if (local_int_14 >= FF_ARRAY_ELEMS(fft_level_index_table)) 1312 return; 1313 1314 if (q->nb_channels > 1) { 1315 channel = get_bits1(gb); 1316 stereo = get_bits1(gb); 1317 } else { 1318 channel = 0; 1319 stereo = 0; 1320 } 1321 1322 exp = qdm2_get_vlc(gb, (b ? &fft_level_exp_vlc : &fft_level_exp_alt_vlc), 0, 2); 1323 exp += q->fft_level_exp[fft_level_index_table[local_int_14]]; 1324 exp = (exp < 0) ? 0 : exp; 1325 1326 phase = get_bits(gb, 3); 1327 stereo_exp = 0; 1328 stereo_phase = 0; 1329 1330 if (stereo) { 1331 stereo_exp = (exp - qdm2_get_vlc(gb, &fft_stereo_exp_vlc, 0, 1)); 1332 stereo_phase = (phase - qdm2_get_vlc(gb, &fft_stereo_phase_vlc, 0, 1)); 1333 if (stereo_phase < 0) 1334 stereo_phase += 8; 1335 } 1336 1337 if (q->frequency_range > (local_int_14 + 1)) { 1338 int sub_packet = (local_int_20 + local_int_28); 1339 1340 if (q->fft_coefs_index + stereo >= FF_ARRAY_ELEMS(q->fft_coefs)) 1341 return; 1342 1343 qdm2_fft_init_coefficient(q, sub_packet, offset, duration, 1344 channel, exp, phase); 1345 if (stereo) 1346 qdm2_fft_init_coefficient(q, sub_packet, offset, duration, 1347 1 - channel, 1348 stereo_exp, stereo_phase); 1349 } 1350 offset++; 1351 } 1352} 1353 1354static void qdm2_decode_fft_packets(QDM2Context *q) 1355{ 1356 int i, j, min, max, value, type, unknown_flag; 1357 GetBitContext gb; 1358 1359 if (!q->sub_packet_list_B[0].packet) 1360 return; 1361 1362 /* reset minimum indexes for FFT coefficients */ 1363 q->fft_coefs_index = 0; 1364 for (i = 0; i < 5; i++) 1365 q->fft_coefs_min_index[i] = -1; 1366 1367 /* process subpackets ordered by type, largest type first */ 1368 for (i = 0, max = 256; i < q->sub_packets_B; i++) { 1369 QDM2SubPacket *packet = NULL; 1370 1371 /* find subpacket with largest type less than max */ 1372 for (j = 0, min = 0; j < q->sub_packets_B; j++) { 1373 value = q->sub_packet_list_B[j].packet->type; 1374 if (value > min && value < max) { 1375 min = value; 1376 packet = q->sub_packet_list_B[j].packet; 1377 } 1378 } 1379 1380 max = min; 1381 1382 /* check for errors (?) */ 1383 if (!packet) 1384 return; 1385 1386 if (i == 0 && 1387 (packet->type < 16 || packet->type >= 48 || 1388 fft_subpackets[packet->type - 16])) 1389 return; 1390 1391 /* decode FFT tones */ 1392 init_get_bits(&gb, packet->data, packet->size * 8); 1393 1394 if (packet->type >= 32 && packet->type < 48 && !fft_subpackets[packet->type - 16]) 1395 unknown_flag = 1; 1396 else 1397 unknown_flag = 0; 1398 1399 type = packet->type; 1400 1401 if ((type >= 17 && type < 24) || (type >= 33 && type < 40)) { 1402 int duration = q->sub_sampling + 5 - (type & 15); 1403 1404 if (duration >= 0 && duration < 4) 1405 qdm2_fft_decode_tones(q, duration, &gb, unknown_flag); 1406 } else if (type == 31) { 1407 for (j = 0; j < 4; j++) 1408 qdm2_fft_decode_tones(q, j, &gb, unknown_flag); 1409 } else if (type == 46) { 1410 for (j = 0; j < 6; j++) 1411 q->fft_level_exp[j] = get_bits(&gb, 6); 1412 for (j = 0; j < 4; j++) 1413 qdm2_fft_decode_tones(q, j, &gb, unknown_flag); 1414 } 1415 } // Loop on B packets 1416 1417 /* calculate maximum indexes for FFT coefficients */ 1418 for (i = 0, j = -1; i < 5; i++) 1419 if (q->fft_coefs_min_index[i] >= 0) { 1420 if (j >= 0) 1421 q->fft_coefs_max_index[j] = q->fft_coefs_min_index[i]; 1422 j = i; 1423 } 1424 if (j >= 0) 1425 q->fft_coefs_max_index[j] = q->fft_coefs_index; 1426} 1427 1428static void qdm2_fft_generate_tone(QDM2Context *q, FFTTone *tone) 1429{ 1430 float level, f[6]; 1431 int i; 1432 QDM2Complex c; 1433 const double iscale = 2.0 * M_PI / 512.0; 1434 1435 tone->phase += tone->phase_shift; 1436 1437 /* calculate current level (maximum amplitude) of tone */ 1438 level = fft_tone_envelope_table[tone->duration][tone->time_index] * tone->level; 1439 c.im = level * sin(tone->phase * iscale); 1440 c.re = level * cos(tone->phase * iscale); 1441 1442 /* generate FFT coefficients for tone */ 1443 if (tone->duration >= 3 || tone->cutoff >= 3) { 1444 tone->complex[0].im += c.im; 1445 tone->complex[0].re += c.re; 1446 tone->complex[1].im -= c.im; 1447 tone->complex[1].re -= c.re; 1448 } else { 1449 f[1] = -tone->table[4]; 1450 f[0] = tone->table[3] - tone->table[0]; 1451 f[2] = 1.0 - tone->table[2] - tone->table[3]; 1452 f[3] = tone->table[1] + tone->table[4] - 1.0; 1453 f[4] = tone->table[0] - tone->table[1]; 1454 f[5] = tone->table[2]; 1455 for (i = 0; i < 2; i++) { 1456 tone->complex[fft_cutoff_index_table[tone->cutoff][i]].re += 1457 c.re * f[i]; 1458 tone->complex[fft_cutoff_index_table[tone->cutoff][i]].im += 1459 c.im * ((tone->cutoff <= i) ? -f[i] : f[i]); 1460 } 1461 for (i = 0; i < 4; i++) { 1462 tone->complex[i].re += c.re * f[i + 2]; 1463 tone->complex[i].im += c.im * f[i + 2]; 1464 } 1465 } 1466 1467 /* copy the tone if it has not yet died out */ 1468 if (++tone->time_index < ((1 << (5 - tone->duration)) - 1)) { 1469 memcpy(&q->fft_tones[q->fft_tone_end], tone, sizeof(FFTTone)); 1470 q->fft_tone_end = (q->fft_tone_end + 1) % 1000; 1471 } 1472} 1473 1474static void qdm2_fft_tone_synthesizer(QDM2Context *q, int sub_packet) 1475{ 1476 int i, j, ch; 1477 const double iscale = 0.25 * M_PI; 1478 1479 for (ch = 0; ch < q->channels; ch++) { 1480 memset(q->fft.complex[ch], 0, q->fft_size * sizeof(QDM2Complex)); 1481 } 1482 1483 1484 /* apply FFT tones with duration 4 (1 FFT period) */ 1485 if (q->fft_coefs_min_index[4] >= 0) 1486 for (i = q->fft_coefs_min_index[4]; i < q->fft_coefs_max_index[4]; i++) { 1487 float level; 1488 QDM2Complex c; 1489 1490 if (q->fft_coefs[i].sub_packet != sub_packet) 1491 break; 1492 1493 ch = (q->channels == 1) ? 0 : q->fft_coefs[i].channel; 1494 level = (q->fft_coefs[i].exp < 0) ? 0.0 : fft_tone_level_table[q->superblocktype_2_3 ? 0 : 1][q->fft_coefs[i].exp & 63]; 1495 1496 c.re = level * cos(q->fft_coefs[i].phase * iscale); 1497 c.im = level * sin(q->fft_coefs[i].phase * iscale); 1498 q->fft.complex[ch][q->fft_coefs[i].offset + 0].re += c.re; 1499 q->fft.complex[ch][q->fft_coefs[i].offset + 0].im += c.im; 1500 q->fft.complex[ch][q->fft_coefs[i].offset + 1].re -= c.re; 1501 q->fft.complex[ch][q->fft_coefs[i].offset + 1].im -= c.im; 1502 } 1503 1504 /* generate existing FFT tones */ 1505 for (i = q->fft_tone_end; i != q->fft_tone_start; ) { 1506 qdm2_fft_generate_tone(q, &q->fft_tones[q->fft_tone_start]); 1507 q->fft_tone_start = (q->fft_tone_start + 1) % 1000; 1508 } 1509 1510 /* create and generate new FFT tones with duration 0 (long) to 3 (short) */ 1511 for (i = 0; i < 4; i++) 1512 if (q->fft_coefs_min_index[i] >= 0) { 1513 for (j = q->fft_coefs_min_index[i]; j < q->fft_coefs_max_index[i]; j++) { 1514 int offset, four_i; 1515 FFTTone tone; 1516 1517 if (q->fft_coefs[j].sub_packet != sub_packet) 1518 break; 1519 1520 four_i = (4 - i); 1521 offset = q->fft_coefs[j].offset >> four_i; 1522 ch = (q->channels == 1) ? 0 : q->fft_coefs[j].channel; 1523 1524 if (offset < q->frequency_range) { 1525 if (offset < 2) 1526 tone.cutoff = offset; 1527 else 1528 tone.cutoff = (offset >= 60) ? 3 : 2; 1529 1530 tone.level = (q->fft_coefs[j].exp < 0) ? 0.0 : fft_tone_level_table[q->superblocktype_2_3 ? 0 : 1][q->fft_coefs[j].exp & 63]; 1531 tone.complex = &q->fft.complex[ch][offset]; 1532 tone.table = fft_tone_sample_table[i][q->fft_coefs[j].offset - (offset << four_i)]; 1533 tone.phase = 64 * q->fft_coefs[j].phase - (offset << 8) - 128; 1534 tone.phase_shift = (2 * q->fft_coefs[j].offset + 1) << (7 - four_i); 1535 tone.duration = i; 1536 tone.time_index = 0; 1537 1538 qdm2_fft_generate_tone(q, &tone); 1539 } 1540 } 1541 q->fft_coefs_min_index[i] = j; 1542 } 1543} 1544 1545static void qdm2_calculate_fft(QDM2Context *q, int channel, int sub_packet) 1546{ 1547 const float gain = (q->channels == 1 && q->nb_channels == 2) ? 0.5f : 1.0f; 1548 float *out = q->output_buffer + channel; 1549 int i; 1550 q->fft.complex[channel][0].re *= 2.0f; 1551 q->fft.complex[channel][0].im = 0.0f; 1552 q->rdft_ctx.rdft_calc(&q->rdft_ctx, (FFTSample *)q->fft.complex[channel]); 1553 /* add samples to output buffer */ 1554 for (i = 0; i < FFALIGN(q->fft_size, 8); i++) { 1555 out[0] += q->fft.complex[channel][i].re * gain; 1556 out[q->channels] += q->fft.complex[channel][i].im * gain; 1557 out += 2 * q->channels; 1558 } 1559} 1560 1561/** 1562 * @param q context 1563 * @param index subpacket number 1564 */ 1565static void qdm2_synthesis_filter(QDM2Context *q, int index) 1566{ 1567 int i, k, ch, sb_used, sub_sampling, dither_state = 0; 1568 1569 /* copy sb_samples */ 1570 sb_used = QDM2_SB_USED(q->sub_sampling); 1571 1572 for (ch = 0; ch < q->channels; ch++) 1573 for (i = 0; i < 8; i++) 1574 for (k = sb_used; k < SBLIMIT; k++) 1575 q->sb_samples[ch][(8 * index) + i][k] = 0; 1576 1577 for (ch = 0; ch < q->nb_channels; ch++) { 1578 float *samples_ptr = q->samples + ch; 1579 1580 for (i = 0; i < 8; i++) { 1581 ff_mpa_synth_filter_float(&q->mpadsp, 1582 q->synth_buf[ch], &(q->synth_buf_offset[ch]), 1583 ff_mpa_synth_window_float, &dither_state, 1584 samples_ptr, q->nb_channels, 1585 q->sb_samples[ch][(8 * index) + i]); 1586 samples_ptr += 32 * q->nb_channels; 1587 } 1588 } 1589 1590 /* add samples to output buffer */ 1591 sub_sampling = (4 >> q->sub_sampling); 1592 1593 for (ch = 0; ch < q->channels; ch++) 1594 for (i = 0; i < q->frame_size; i++) 1595 q->output_buffer[q->channels * i + ch] += (1 << 23) * q->samples[q->nb_channels * sub_sampling * i + ch]; 1596} 1597 1598/** 1599 * Init static data (does not depend on specific file) 1600 */ 1601static av_cold void qdm2_init_static_data(void) { 1602 qdm2_init_vlc(); 1603 softclip_table_init(); 1604 rnd_table_init(); 1605 init_noise_samples(); 1606 1607 ff_mpa_synth_init_float(); 1608} 1609 1610/** 1611 * Init parameters from codec extradata 1612 */ 1613static av_cold int qdm2_decode_init(AVCodecContext *avctx) 1614{ 1615 static AVOnce init_static_once = AV_ONCE_INIT; 1616 QDM2Context *s = avctx->priv_data; 1617 int tmp_val, tmp, size; 1618 GetByteContext gb; 1619 1620 /* extradata parsing 1621 1622 Structure: 1623 wave { 1624 frma (QDM2) 1625 QDCA 1626 QDCP 1627 } 1628 1629 32 size (including this field) 1630 32 tag (=frma) 1631 32 type (=QDM2 or QDMC) 1632 1633 32 size (including this field, in bytes) 1634 32 tag (=QDCA) // maybe mandatory parameters 1635 32 unknown (=1) 1636 32 channels (=2) 1637 32 samplerate (=44100) 1638 32 bitrate (=96000) 1639 32 block size (=4096) 1640 32 frame size (=256) (for one channel) 1641 32 packet size (=1300) 1642 1643 32 size (including this field, in bytes) 1644 32 tag (=QDCP) // maybe some tuneable parameters 1645 32 float1 (=1.0) 1646 32 zero ? 1647 32 float2 (=1.0) 1648 32 float3 (=1.0) 1649 32 unknown (27) 1650 32 unknown (8) 1651 32 zero ? 1652 */ 1653 1654 if (!avctx->extradata || (avctx->extradata_size < 48)) { 1655 av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n"); 1656 return AVERROR_INVALIDDATA; 1657 } 1658 1659 bytestream2_init(&gb, avctx->extradata, avctx->extradata_size); 1660 1661 while (bytestream2_get_bytes_left(&gb) > 8) { 1662 if (bytestream2_peek_be64(&gb) == (((uint64_t)MKBETAG('f','r','m','a') << 32) | 1663 (uint64_t)MKBETAG('Q','D','M','2'))) 1664 break; 1665 bytestream2_skip(&gb, 1); 1666 } 1667 1668 if (bytestream2_get_bytes_left(&gb) < 12) { 1669 av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n", 1670 bytestream2_get_bytes_left(&gb)); 1671 return AVERROR_INVALIDDATA; 1672 } 1673 1674 bytestream2_skip(&gb, 8); 1675 size = bytestream2_get_be32(&gb); 1676 1677 if (size > bytestream2_get_bytes_left(&gb)) { 1678 av_log(avctx, AV_LOG_ERROR, "extradata size too small, %i < %i\n", 1679 bytestream2_get_bytes_left(&gb), size); 1680 return AVERROR_INVALIDDATA; 1681 } 1682 1683 av_log(avctx, AV_LOG_DEBUG, "size: %d\n", size); 1684 if (bytestream2_get_be32(&gb) != MKBETAG('Q','D','C','A')) { 1685 av_log(avctx, AV_LOG_ERROR, "invalid extradata, expecting QDCA\n"); 1686 return AVERROR_INVALIDDATA; 1687 } 1688 1689 bytestream2_skip(&gb, 4); 1690 1691 s->nb_channels = s->channels = bytestream2_get_be32(&gb); 1692 if (s->channels <= 0 || s->channels > MPA_MAX_CHANNELS) { 1693 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n"); 1694 return AVERROR_INVALIDDATA; 1695 } 1696 av_channel_layout_uninit(&avctx->ch_layout); 1697 av_channel_layout_default(&avctx->ch_layout, s->channels); 1698 1699 avctx->sample_rate = bytestream2_get_be32(&gb); 1700 avctx->bit_rate = bytestream2_get_be32(&gb); 1701 s->group_size = bytestream2_get_be32(&gb); 1702 s->fft_size = bytestream2_get_be32(&gb); 1703 s->checksum_size = bytestream2_get_be32(&gb); 1704 if (s->checksum_size >= 1U << 28 || s->checksum_size <= 1) { 1705 av_log(avctx, AV_LOG_ERROR, "data block size invalid (%u)\n", s->checksum_size); 1706 return AVERROR_INVALIDDATA; 1707 } 1708 1709 s->fft_order = av_log2(s->fft_size) + 1; 1710 1711 // Fail on unknown fft order 1712 if ((s->fft_order < 7) || (s->fft_order > 9)) { 1713 avpriv_request_sample(avctx, "Unknown FFT order %d", s->fft_order); 1714 return AVERROR_PATCHWELCOME; 1715 } 1716 1717 // something like max decodable tones 1718 s->group_order = av_log2(s->group_size) + 1; 1719 s->frame_size = s->group_size / 16; // 16 iterations per super block 1720 1721 if (s->frame_size > QDM2_MAX_FRAME_SIZE) 1722 return AVERROR_INVALIDDATA; 1723 1724 s->sub_sampling = s->fft_order - 7; 1725 s->frequency_range = 255 / (1 << (2 - s->sub_sampling)); 1726 1727 if (s->frame_size * 4 >> s->sub_sampling > MPA_FRAME_SIZE) { 1728 avpriv_request_sample(avctx, "large frames"); 1729 return AVERROR_PATCHWELCOME; 1730 } 1731 1732 switch ((s->sub_sampling * 2 + s->channels - 1)) { 1733 case 0: tmp = 40; break; 1734 case 1: tmp = 48; break; 1735 case 2: tmp = 56; break; 1736 case 3: tmp = 72; break; 1737 case 4: tmp = 80; break; 1738 case 5: tmp = 100;break; 1739 default: tmp=s->sub_sampling; break; 1740 } 1741 tmp_val = 0; 1742 if ((tmp * 1000) < avctx->bit_rate) tmp_val = 1; 1743 if ((tmp * 1440) < avctx->bit_rate) tmp_val = 2; 1744 if ((tmp * 1760) < avctx->bit_rate) tmp_val = 3; 1745 if ((tmp * 2240) < avctx->bit_rate) tmp_val = 4; 1746 s->cm_table_select = tmp_val; 1747 1748 if (avctx->bit_rate <= 8000) 1749 s->coeff_per_sb_select = 0; 1750 else if (avctx->bit_rate < 16000) 1751 s->coeff_per_sb_select = 1; 1752 else 1753 s->coeff_per_sb_select = 2; 1754 1755 if (s->fft_size != (1 << (s->fft_order - 1))) { 1756 av_log(avctx, AV_LOG_ERROR, "FFT size %d not power of 2.\n", s->fft_size); 1757 return AVERROR_INVALIDDATA; 1758 } 1759 1760 ff_rdft_init(&s->rdft_ctx, s->fft_order, IDFT_C2R); 1761 ff_mpadsp_init(&s->mpadsp); 1762 1763 avctx->sample_fmt = AV_SAMPLE_FMT_S16; 1764 1765 ff_thread_once(&init_static_once, qdm2_init_static_data); 1766 1767 return 0; 1768} 1769 1770static av_cold int qdm2_decode_close(AVCodecContext *avctx) 1771{ 1772 QDM2Context *s = avctx->priv_data; 1773 1774 ff_rdft_end(&s->rdft_ctx); 1775 1776 return 0; 1777} 1778 1779static int qdm2_decode(QDM2Context *q, const uint8_t *in, int16_t *out) 1780{ 1781 int ch, i; 1782 const int frame_size = (q->frame_size * q->channels); 1783 1784 if((unsigned)frame_size > FF_ARRAY_ELEMS(q->output_buffer)/2) 1785 return -1; 1786 1787 /* select input buffer */ 1788 q->compressed_data = in; 1789 q->compressed_size = q->checksum_size; 1790 1791 /* copy old block, clear new block of output samples */ 1792 memmove(q->output_buffer, &q->output_buffer[frame_size], frame_size * sizeof(float)); 1793 memset(&q->output_buffer[frame_size], 0, frame_size * sizeof(float)); 1794 1795 /* decode block of QDM2 compressed data */ 1796 if (q->sub_packet == 0) { 1797 q->has_errors = 0; // zero it for a new super block 1798 av_log(NULL,AV_LOG_DEBUG,"Superblock follows\n"); 1799 qdm2_decode_super_block(q); 1800 } 1801 1802 /* parse subpackets */ 1803 if (!q->has_errors) { 1804 if (q->sub_packet == 2) 1805 qdm2_decode_fft_packets(q); 1806 1807 qdm2_fft_tone_synthesizer(q, q->sub_packet); 1808 } 1809 1810 /* sound synthesis stage 1 (FFT) */ 1811 for (ch = 0; ch < q->channels; ch++) { 1812 qdm2_calculate_fft(q, ch, q->sub_packet); 1813 1814 if (!q->has_errors && q->sub_packet_list_C[0].packet) { 1815 SAMPLES_NEEDED_2("has errors, and C list is not empty") 1816 return -1; 1817 } 1818 } 1819 1820 /* sound synthesis stage 2 (MPEG audio like synthesis filter) */ 1821 if (!q->has_errors && q->do_synth_filter) 1822 qdm2_synthesis_filter(q, q->sub_packet); 1823 1824 q->sub_packet = (q->sub_packet + 1) % 16; 1825 1826 /* clip and convert output float[] to 16-bit signed samples */ 1827 for (i = 0; i < frame_size; i++) { 1828 int value = (int)q->output_buffer[i]; 1829 1830 if (value > SOFTCLIP_THRESHOLD) 1831 value = (value > HARDCLIP_THRESHOLD) ? 32767 : softclip_table[ value - SOFTCLIP_THRESHOLD]; 1832 else if (value < -SOFTCLIP_THRESHOLD) 1833 value = (value < -HARDCLIP_THRESHOLD) ? -32767 : -softclip_table[-value - SOFTCLIP_THRESHOLD]; 1834 1835 out[i] = value; 1836 } 1837 1838 return 0; 1839} 1840 1841static int qdm2_decode_frame(AVCodecContext *avctx, AVFrame *frame, 1842 int *got_frame_ptr, AVPacket *avpkt) 1843{ 1844 const uint8_t *buf = avpkt->data; 1845 int buf_size = avpkt->size; 1846 QDM2Context *s = avctx->priv_data; 1847 int16_t *out; 1848 int i, ret; 1849 1850 if(!buf) 1851 return 0; 1852 if(buf_size < s->checksum_size) 1853 return -1; 1854 1855 /* get output buffer */ 1856 frame->nb_samples = 16 * s->frame_size; 1857 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) 1858 return ret; 1859 out = (int16_t *)frame->data[0]; 1860 1861 for (i = 0; i < 16; i++) { 1862 if ((ret = qdm2_decode(s, buf, out)) < 0) 1863 return ret; 1864 out += s->channels * s->frame_size; 1865 } 1866 1867 *got_frame_ptr = 1; 1868 1869 return s->checksum_size; 1870} 1871 1872const FFCodec ff_qdm2_decoder = { 1873 .p.name = "qdm2", 1874 .p.long_name = NULL_IF_CONFIG_SMALL("QDesign Music Codec 2"), 1875 .p.type = AVMEDIA_TYPE_AUDIO, 1876 .p.id = AV_CODEC_ID_QDM2, 1877 .priv_data_size = sizeof(QDM2Context), 1878 .init = qdm2_decode_init, 1879 .close = qdm2_decode_close, 1880 FF_CODEC_DECODE_CB(qdm2_decode_frame), 1881 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF, 1882 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, 1883}; 1884