00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00033 #include <math.h>
00034 #include <stddef.h>
00035 #include <stdio.h>
00036
00037 #define ALT_BITSTREAM_READER_LE
00038 #include "avcodec.h"
00039 #include "bitstream.h"
00040 #include "dsputil.h"
00041 #include "mpegaudio.h"
00042
00043 #include "qdm2data.h"
00044
00045 #undef NDEBUG
00046 #include <assert.h>
00047
00048
00049 #define SOFTCLIP_THRESHOLD 27600
00050 #define HARDCLIP_THRESHOLD 35716
00051
00052
00053 #define QDM2_LIST_ADD(list, size, packet) \
00054 do { \
00055 if (size > 0) { \
00056 list[size - 1].next = &list[size]; \
00057 } \
00058 list[size].packet = packet; \
00059 list[size].next = NULL; \
00060 size++; \
00061 } while(0)
00062
00063
00064 #define QDM2_SB_USED(sub_sampling) (((sub_sampling) >= 2) ? 30 : 8 << (sub_sampling))
00065
00066 #define FIX_NOISE_IDX(noise_idx) \
00067 if ((noise_idx) >= 3840) \
00068 (noise_idx) -= 3840; \
00069
00070 #define SB_DITHERING_NOISE(sb,noise_idx) (noise_table[(noise_idx)++] * sb_noise_attenuation[(sb)])
00071
00072 #define BITS_LEFT(length,gb) ((length) - get_bits_count ((gb)))
00073
00074 #define SAMPLES_NEEDED \
00075 av_log (NULL,AV_LOG_INFO,"This file triggers some untested code. Please contact the developers.\n");
00076
00077 #define SAMPLES_NEEDED_2(why) \
00078 av_log (NULL,AV_LOG_INFO,"This file triggers some missing code. Please contact the developers.\nPosition: %s\n",why);
00079
00080 #define QDM2_MAX_FRAME_SIZE 512
00081
00082 typedef int8_t sb_int8_array[2][30][64];
00083
00087 typedef struct {
00088 int type;
00089 unsigned int size;
00090 const uint8_t *data;
00091 } QDM2SubPacket;
00092
00096 typedef struct QDM2SubPNode {
00097 QDM2SubPacket *packet;
00098 struct QDM2SubPNode *next;
00099 } QDM2SubPNode;
00100
00101 typedef struct {
00102 float re;
00103 float im;
00104 } QDM2Complex;
00105
00106 typedef struct {
00107 float level;
00108 QDM2Complex *complex;
00109 const float *table;
00110 int phase;
00111 int phase_shift;
00112 int duration;
00113 short time_index;
00114 short cutoff;
00115 } FFTTone;
00116
00117 typedef struct {
00118 int16_t sub_packet;
00119 uint8_t channel;
00120 int16_t offset;
00121 int16_t exp;
00122 uint8_t phase;
00123 } FFTCoefficient;
00124
00125 typedef struct {
00126 DECLARE_ALIGNED_16(QDM2Complex, complex[MPA_MAX_CHANNELS][256]);
00127 } QDM2FFT;
00128
00132 typedef struct {
00134 int nb_channels;
00135 int channels;
00136 int group_size;
00137 int fft_size;
00138 int checksum_size;
00139
00141 int group_order;
00142 int fft_order;
00143 int fft_frame_size;
00144 int frame_size;
00145 int frequency_range;
00146 int sub_sampling;
00147 int coeff_per_sb_select;
00148 int cm_table_select;
00149
00151 QDM2SubPacket sub_packets[16];
00152 QDM2SubPNode sub_packet_list_A[16];
00153 QDM2SubPNode sub_packet_list_B[16];
00154 int sub_packets_B;
00155 QDM2SubPNode sub_packet_list_C[16];
00156 QDM2SubPNode sub_packet_list_D[16];
00157
00159 FFTTone fft_tones[1000];
00160 int fft_tone_start;
00161 int fft_tone_end;
00162 FFTCoefficient fft_coefs[1000];
00163 int fft_coefs_index;
00164 int fft_coefs_min_index[5];
00165 int fft_coefs_max_index[5];
00166 int fft_level_exp[6];
00167 RDFTContext rdft_ctx;
00168 QDM2FFT fft;
00169
00171 const uint8_t *compressed_data;
00172 int compressed_size;
00173 float output_buffer[QDM2_MAX_FRAME_SIZE * MPA_MAX_CHANNELS * 2];
00174
00176 DECLARE_ALIGNED_16(MPA_INT, synth_buf[MPA_MAX_CHANNELS][512*2]);
00177 int synth_buf_offset[MPA_MAX_CHANNELS];
00178 DECLARE_ALIGNED_16(int32_t, sb_samples[MPA_MAX_CHANNELS][128][SBLIMIT]);
00179
00181 float tone_level[MPA_MAX_CHANNELS][30][64];
00182 int8_t coding_method[MPA_MAX_CHANNELS][30][64];
00183 int8_t quantized_coeffs[MPA_MAX_CHANNELS][10][8];
00184 int8_t tone_level_idx_base[MPA_MAX_CHANNELS][30][8];
00185 int8_t tone_level_idx_hi1[MPA_MAX_CHANNELS][3][8][8];
00186 int8_t tone_level_idx_mid[MPA_MAX_CHANNELS][26][8];
00187 int8_t tone_level_idx_hi2[MPA_MAX_CHANNELS][26];
00188 int8_t tone_level_idx[MPA_MAX_CHANNELS][30][64];
00189 int8_t tone_level_idx_temp[MPA_MAX_CHANNELS][30][64];
00190
00191
00192 int has_errors;
00193 int superblocktype_2_3;
00194 int do_synth_filter;
00195
00196 int sub_packet;
00197 int noise_idx;
00198 } QDM2Context;
00199
00200
00201 static uint8_t empty_buffer[FF_INPUT_BUFFER_PADDING_SIZE];
00202
00203 static VLC vlc_tab_level;
00204 static VLC vlc_tab_diff;
00205 static VLC vlc_tab_run;
00206 static VLC fft_level_exp_alt_vlc;
00207 static VLC fft_level_exp_vlc;
00208 static VLC fft_stereo_exp_vlc;
00209 static VLC fft_stereo_phase_vlc;
00210 static VLC vlc_tab_tone_level_idx_hi1;
00211 static VLC vlc_tab_tone_level_idx_mid;
00212 static VLC vlc_tab_tone_level_idx_hi2;
00213 static VLC vlc_tab_type30;
00214 static VLC vlc_tab_type34;
00215 static VLC vlc_tab_fft_tone_offset[5];
00216
00217 static uint16_t softclip_table[HARDCLIP_THRESHOLD - SOFTCLIP_THRESHOLD + 1];
00218 static float noise_table[4096];
00219 static uint8_t random_dequant_index[256][5];
00220 static uint8_t random_dequant_type24[128][3];
00221 static float noise_samples[128];
00222
00223 static DECLARE_ALIGNED_16(MPA_INT, mpa_window[512]);
00224
00225
00226 static av_cold void softclip_table_init(void) {
00227 int i;
00228 double dfl = SOFTCLIP_THRESHOLD - 32767;
00229 float delta = 1.0 / -dfl;
00230 for (i = 0; i < HARDCLIP_THRESHOLD - SOFTCLIP_THRESHOLD + 1; i++)
00231 softclip_table[i] = SOFTCLIP_THRESHOLD - ((int)(sin((float)i * delta) * dfl) & 0x0000FFFF);
00232 }
00233
00234
00235
00236 static av_cold void rnd_table_init(void) {
00237 int i,j;
00238 uint32_t ldw,hdw;
00239 uint64_t tmp64_1;
00240 uint64_t random_seed = 0;
00241 float delta = 1.0 / 16384.0;
00242 for(i = 0; i < 4096 ;i++) {
00243 random_seed = random_seed * 214013 + 2531011;
00244 noise_table[i] = (delta * (float)(((int32_t)random_seed >> 16) & 0x00007FFF)- 1.0) * 1.3;
00245 }
00246
00247 for (i = 0; i < 256 ;i++) {
00248 random_seed = 81;
00249 ldw = i;
00250 for (j = 0; j < 5 ;j++) {
00251 random_dequant_index[i][j] = (uint8_t)((ldw / random_seed) & 0xFF);
00252 ldw = (uint32_t)ldw % (uint32_t)random_seed;
00253 tmp64_1 = (random_seed * 0x55555556);
00254 hdw = (uint32_t)(tmp64_1 >> 32);
00255 random_seed = (uint64_t)(hdw + (ldw >> 31));
00256 }
00257 }
00258 for (i = 0; i < 128 ;i++) {
00259 random_seed = 25;
00260 ldw = i;
00261 for (j = 0; j < 3 ;j++) {
00262 random_dequant_type24[i][j] = (uint8_t)((ldw / random_seed) & 0xFF);
00263 ldw = (uint32_t)ldw % (uint32_t)random_seed;
00264 tmp64_1 = (random_seed * 0x66666667);
00265 hdw = (uint32_t)(tmp64_1 >> 33);
00266 random_seed = hdw + (ldw >> 31);
00267 }
00268 }
00269 }
00270
00271
00272 static av_cold void init_noise_samples(void) {
00273 int i;
00274 int random_seed = 0;
00275 float delta = 1.0 / 16384.0;
00276 for (i = 0; i < 128;i++) {
00277 random_seed = random_seed * 214013 + 2531011;
00278 noise_samples[i] = (delta * (float)((random_seed >> 16) & 0x00007fff) - 1.0);
00279 }
00280 }
00281
00282
00283 static av_cold void qdm2_init_vlc(void)
00284 {
00285 init_vlc (&vlc_tab_level, 8, 24,
00286 vlc_tab_level_huffbits, 1, 1,
00287 vlc_tab_level_huffcodes, 2, 2, INIT_VLC_USE_STATIC | INIT_VLC_LE);
00288
00289 init_vlc (&vlc_tab_diff, 8, 37,
00290 vlc_tab_diff_huffbits, 1, 1,
00291 vlc_tab_diff_huffcodes, 2, 2, INIT_VLC_USE_STATIC | INIT_VLC_LE);
00292
00293 init_vlc (&vlc_tab_run, 5, 6,
00294 vlc_tab_run_huffbits, 1, 1,
00295 vlc_tab_run_huffcodes, 1, 1, INIT_VLC_USE_STATIC | INIT_VLC_LE);
00296
00297 init_vlc (&fft_level_exp_alt_vlc, 8, 28,
00298 fft_level_exp_alt_huffbits, 1, 1,
00299 fft_level_exp_alt_huffcodes, 2, 2, INIT_VLC_USE_STATIC | INIT_VLC_LE);
00300
00301 init_vlc (&fft_level_exp_vlc, 8, 20,
00302 fft_level_exp_huffbits, 1, 1,
00303 fft_level_exp_huffcodes, 2, 2, INIT_VLC_USE_STATIC | INIT_VLC_LE);
00304
00305 init_vlc (&fft_stereo_exp_vlc, 6, 7,
00306 fft_stereo_exp_huffbits, 1, 1,
00307 fft_stereo_exp_huffcodes, 1, 1, INIT_VLC_USE_STATIC | INIT_VLC_LE);
00308
00309 init_vlc (&fft_stereo_phase_vlc, 6, 9,
00310 fft_stereo_phase_huffbits, 1, 1,
00311 fft_stereo_phase_huffcodes, 1, 1, INIT_VLC_USE_STATIC | INIT_VLC_LE);
00312
00313 init_vlc (&vlc_tab_tone_level_idx_hi1, 8, 20,
00314 vlc_tab_tone_level_idx_hi1_huffbits, 1, 1,
00315 vlc_tab_tone_level_idx_hi1_huffcodes, 2, 2, INIT_VLC_USE_STATIC | INIT_VLC_LE);
00316
00317 init_vlc (&vlc_tab_tone_level_idx_mid, 8, 24,
00318 vlc_tab_tone_level_idx_mid_huffbits, 1, 1,
00319 vlc_tab_tone_level_idx_mid_huffcodes, 2, 2, INIT_VLC_USE_STATIC | INIT_VLC_LE);
00320
00321 init_vlc (&vlc_tab_tone_level_idx_hi2, 8, 24,
00322 vlc_tab_tone_level_idx_hi2_huffbits, 1, 1,
00323 vlc_tab_tone_level_idx_hi2_huffcodes, 2, 2, INIT_VLC_USE_STATIC | INIT_VLC_LE);
00324
00325 init_vlc (&vlc_tab_type30, 6, 9,
00326 vlc_tab_type30_huffbits, 1, 1,
00327 vlc_tab_type30_huffcodes, 1, 1, INIT_VLC_USE_STATIC | INIT_VLC_LE);
00328
00329 init_vlc (&vlc_tab_type34, 5, 10,
00330 vlc_tab_type34_huffbits, 1, 1,
00331 vlc_tab_type34_huffcodes, 1, 1, INIT_VLC_USE_STATIC | INIT_VLC_LE);
00332
00333 init_vlc (&vlc_tab_fft_tone_offset[0], 8, 23,
00334 vlc_tab_fft_tone_offset_0_huffbits, 1, 1,
00335 vlc_tab_fft_tone_offset_0_huffcodes, 2, 2, INIT_VLC_USE_STATIC | INIT_VLC_LE);
00336
00337 init_vlc (&vlc_tab_fft_tone_offset[1], 8, 28,
00338 vlc_tab_fft_tone_offset_1_huffbits, 1, 1,
00339 vlc_tab_fft_tone_offset_1_huffcodes, 2, 2, INIT_VLC_USE_STATIC | INIT_VLC_LE);
00340
00341 init_vlc (&vlc_tab_fft_tone_offset[2], 8, 32,
00342 vlc_tab_fft_tone_offset_2_huffbits, 1, 1,
00343 vlc_tab_fft_tone_offset_2_huffcodes, 2, 2, INIT_VLC_USE_STATIC | INIT_VLC_LE);
00344
00345 init_vlc (&vlc_tab_fft_tone_offset[3], 8, 35,
00346 vlc_tab_fft_tone_offset_3_huffbits, 1, 1,
00347 vlc_tab_fft_tone_offset_3_huffcodes, 2, 2, INIT_VLC_USE_STATIC | INIT_VLC_LE);
00348
00349 init_vlc (&vlc_tab_fft_tone_offset[4], 8, 38,
00350 vlc_tab_fft_tone_offset_4_huffbits, 1, 1,
00351 vlc_tab_fft_tone_offset_4_huffcodes, 2, 2, INIT_VLC_USE_STATIC | INIT_VLC_LE);
00352 }
00353
00354
00355
00356 static const float f2i_scale = (float) (1 << (FRAC_BITS - 15));
00357
00358
00359 static int qdm2_get_vlc (GetBitContext *gb, VLC *vlc, int flag, int depth)
00360 {
00361 int value;
00362
00363 value = get_vlc2(gb, vlc->table, vlc->bits, depth);
00364
00365
00366 if (value-- == 0)
00367 value = get_bits (gb, get_bits (gb, 3) + 1);
00368
00369
00370 if (flag) {
00371 int tmp = vlc_stage3_values[value];
00372
00373 if ((value & ~3) > 0)
00374 tmp += get_bits (gb, (value >> 2));
00375 value = tmp;
00376 }
00377
00378 return value;
00379 }
00380
00381
00382 static int qdm2_get_se_vlc (VLC *vlc, GetBitContext *gb, int depth)
00383 {
00384 int value = qdm2_get_vlc (gb, vlc, 0, depth);
00385
00386 return (value & 1) ? ((value + 1) >> 1) : -(value >> 1);
00387 }
00388
00389
00399 static uint16_t qdm2_packet_checksum (const uint8_t *data, int length, int value) {
00400 int i;
00401
00402 for (i=0; i < length; i++)
00403 value -= data[i];
00404
00405 return (uint16_t)(value & 0xffff);
00406 }
00407
00408
00415 static void qdm2_decode_sub_packet_header (GetBitContext *gb, QDM2SubPacket *sub_packet)
00416 {
00417 sub_packet->type = get_bits (gb, 8);
00418
00419 if (sub_packet->type == 0) {
00420 sub_packet->size = 0;
00421 sub_packet->data = NULL;
00422 } else {
00423 sub_packet->size = get_bits (gb, 8);
00424
00425 if (sub_packet->type & 0x80) {
00426 sub_packet->size <<= 8;
00427 sub_packet->size |= get_bits (gb, 8);
00428 sub_packet->type &= 0x7f;
00429 }
00430
00431 if (sub_packet->type == 0x7f)
00432 sub_packet->type |= (get_bits (gb, 8) << 8);
00433
00434 sub_packet->data = &gb->buffer[get_bits_count(gb) / 8];
00435 }
00436
00437 av_log(NULL,AV_LOG_DEBUG,"Subpacket: type=%d size=%d start_offs=%x\n",
00438 sub_packet->type, sub_packet->size, get_bits_count(gb) / 8);
00439 }
00440
00441
00449 static QDM2SubPNode* qdm2_search_subpacket_type_in_list (QDM2SubPNode *list, int type)
00450 {
00451 while (list != NULL && list->packet != NULL) {
00452 if (list->packet->type == type)
00453 return list;
00454 list = list->next;
00455 }
00456 return NULL;
00457 }
00458
00459
00466 static void average_quantized_coeffs (QDM2Context *q)
00467 {
00468 int i, j, n, ch, sum;
00469
00470 n = coeff_per_sb_for_avg[q->coeff_per_sb_select][QDM2_SB_USED(q->sub_sampling) - 1] + 1;
00471
00472 for (ch = 0; ch < q->nb_channels; ch++)
00473 for (i = 0; i < n; i++) {
00474 sum = 0;
00475
00476 for (j = 0; j < 8; j++)
00477 sum += q->quantized_coeffs[ch][i][j];
00478
00479 sum /= 8;
00480 if (sum > 0)
00481 sum--;
00482
00483 for (j=0; j < 8; j++)
00484 q->quantized_coeffs[ch][i][j] = sum;
00485 }
00486 }
00487
00488
00496 static void build_sb_samples_from_noise (QDM2Context *q, int sb)
00497 {
00498 int ch, j;
00499
00500 FIX_NOISE_IDX(q->noise_idx);
00501
00502 if (!q->nb_channels)
00503 return;
00504
00505 for (ch = 0; ch < q->nb_channels; ch++)
00506 for (j = 0; j < 64; j++) {
00507 q->sb_samples[ch][j * 2][sb] = (int32_t)(f2i_scale * SB_DITHERING_NOISE(sb,q->noise_idx) * q->tone_level[ch][sb][j] + .5);
00508 q->sb_samples[ch][j * 2 + 1][sb] = (int32_t)(f2i_scale * SB_DITHERING_NOISE(sb,q->noise_idx) * q->tone_level[ch][sb][j] + .5);
00509 }
00510 }
00511
00512
00521 static void fix_coding_method_array (int sb, int channels, sb_int8_array coding_method)
00522 {
00523 int j,k;
00524 int ch;
00525 int run, case_val;
00526 int switchtable[23] = {0,5,1,5,5,5,5,5,2,5,5,5,5,5,5,5,3,5,5,5,5,5,4};
00527
00528 for (ch = 0; ch < channels; ch++) {
00529 for (j = 0; j < 64; ) {
00530 if((coding_method[ch][sb][j] - 8) > 22) {
00531 run = 1;
00532 case_val = 8;
00533 } else {
00534 switch (switchtable[coding_method[ch][sb][j]-8]) {
00535 case 0: run = 10; case_val = 10; break;
00536 case 1: run = 1; case_val = 16; break;
00537 case 2: run = 5; case_val = 24; break;
00538 case 3: run = 3; case_val = 30; break;
00539 case 4: run = 1; case_val = 30; break;
00540 case 5: run = 1; case_val = 8; break;
00541 default: run = 1; case_val = 8; break;
00542 }
00543 }
00544 for (k = 0; k < run; k++)
00545 if (j + k < 128)
00546 if (coding_method[ch][sb + (j + k) / 64][(j + k) % 64] > coding_method[ch][sb][j])
00547 if (k > 0) {
00548 SAMPLES_NEEDED
00549
00550 memset(&coding_method[ch][sb][j + k], case_val, k * sizeof(int8_t));
00551 memset(&coding_method[ch][sb][j + k], case_val, 3 * sizeof(int8_t));
00552 }
00553 j += run;
00554 }
00555 }
00556 }
00557
00558
00566 static void fill_tone_level_array (QDM2Context *q, int flag)
00567 {
00568 int i, sb, ch, sb_used;
00569 int tmp, tab;
00570
00571
00572 if (q->nb_channels <= 0)
00573 return;
00574
00575 for (ch = 0; ch < q->nb_channels; ch++)
00576 for (sb = 0; sb < 30; sb++)
00577 for (i = 0; i < 8; i++) {
00578 if ((tab=coeff_per_sb_for_dequant[q->coeff_per_sb_select][sb]) < (last_coeff[q->coeff_per_sb_select] - 1))
00579 tmp = q->quantized_coeffs[ch][tab + 1][i] * dequant_table[q->coeff_per_sb_select][tab + 1][sb]+
00580 q->quantized_coeffs[ch][tab][i] * dequant_table[q->coeff_per_sb_select][tab][sb];
00581 else
00582 tmp = q->quantized_coeffs[ch][tab][i] * dequant_table[q->coeff_per_sb_select][tab][sb];
00583 if(tmp < 0)
00584 tmp += 0xff;
00585 q->tone_level_idx_base[ch][sb][i] = (tmp / 256) & 0xff;
00586 }
00587
00588 sb_used = QDM2_SB_USED(q->sub_sampling);
00589
00590 if ((q->superblocktype_2_3 != 0) && !flag) {
00591 for (sb = 0; sb < sb_used; sb++)
00592 for (ch = 0; ch < q->nb_channels; ch++)
00593 for (i = 0; i < 64; i++) {
00594 q->tone_level_idx[ch][sb][i] = q->tone_level_idx_base[ch][sb][i / 8];
00595 if (q->tone_level_idx[ch][sb][i] < 0)
00596 q->tone_level[ch][sb][i] = 0;
00597 else
00598 q->tone_level[ch][sb][i] = fft_tone_level_table[0][q->tone_level_idx[ch][sb][i] & 0x3f];
00599 }
00600 } else {
00601 tab = q->superblocktype_2_3 ? 0 : 1;
00602 for (sb = 0; sb < sb_used; sb++) {
00603 if ((sb >= 4) && (sb <= 23)) {
00604 for (ch = 0; ch < q->nb_channels; ch++)
00605 for (i = 0; i < 64; i++) {
00606 tmp = q->tone_level_idx_base[ch][sb][i / 8] -
00607 q->tone_level_idx_hi1[ch][sb / 8][i / 8][i % 8] -
00608 q->tone_level_idx_mid[ch][sb - 4][i / 8] -
00609 q->tone_level_idx_hi2[ch][sb - 4];
00610 q->tone_level_idx[ch][sb][i] = tmp & 0xff;
00611 if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
00612 q->tone_level[ch][sb][i] = 0;
00613 else
00614 q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
00615 }
00616 } else {
00617 if (sb > 4) {
00618 for (ch = 0; ch < q->nb_channels; ch++)
00619 for (i = 0; i < 64; i++) {
00620 tmp = q->tone_level_idx_base[ch][sb][i / 8] -
00621 q->tone_level_idx_hi1[ch][2][i / 8][i % 8] -
00622 q->tone_level_idx_hi2[ch][sb - 4];
00623 q->tone_level_idx[ch][sb][i] = tmp & 0xff;
00624 if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
00625 q->tone_level[ch][sb][i] = 0;
00626 else
00627 q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
00628 }
00629 } else {
00630 for (ch = 0; ch < q->nb_channels; ch++)
00631 for (i = 0; i < 64; i++) {
00632 tmp = q->tone_level_idx[ch][sb][i] = q->tone_level_idx_base[ch][sb][i / 8];
00633 if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
00634 q->tone_level[ch][sb][i] = 0;
00635 else
00636 q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
00637 }
00638 }
00639 }
00640 }
00641 }
00642
00643 return;
00644 }
00645
00646
00661 static void fill_coding_method_array (sb_int8_array tone_level_idx, sb_int8_array tone_level_idx_temp,
00662 sb_int8_array coding_method, int nb_channels,
00663 int c, int superblocktype_2_3, int cm_table_select)
00664 {
00665 int ch, sb, j;
00666 int tmp, acc, esp_40, comp;
00667 int add1, add2, add3, add4;
00668 int64_t multres;
00669
00670
00671 if (nb_channels <= 0)
00672 return;
00673
00674 if (!superblocktype_2_3) {
00675
00676 SAMPLES_NEEDED
00677 for (ch = 0; ch < nb_channels; ch++)
00678 for (sb = 0; sb < 30; sb++) {
00679 for (j = 1; j < 63; j++) {
00680 add1 = tone_level_idx[ch][sb][j] - 10;
00681 if (add1 < 0)
00682 add1 = 0;
00683 add2 = add3 = add4 = 0;
00684 if (sb > 1) {
00685 add2 = tone_level_idx[ch][sb - 2][j] + tone_level_idx_offset_table[sb][0] - 6;
00686 if (add2 < 0)
00687 add2 = 0;
00688 }
00689 if (sb > 0) {
00690 add3 = tone_level_idx[ch][sb - 1][j] + tone_level_idx_offset_table[sb][1] - 6;
00691 if (add3 < 0)
00692 add3 = 0;
00693 }
00694 if (sb < 29) {
00695 add4 = tone_level_idx[ch][sb + 1][j] + tone_level_idx_offset_table[sb][3] - 6;
00696 if (add4 < 0)
00697 add4 = 0;
00698 }
00699 tmp = tone_level_idx[ch][sb][j + 1] * 2 - add4 - add3 - add2 - add1;
00700 if (tmp < 0)
00701 tmp = 0;
00702 tone_level_idx_temp[ch][sb][j + 1] = tmp & 0xff;
00703 }
00704 tone_level_idx_temp[ch][sb][0] = tone_level_idx_temp[ch][sb][1];
00705 }
00706 acc = 0;
00707 for (ch = 0; ch < nb_channels; ch++)
00708 for (sb = 0; sb < 30; sb++)
00709 for (j = 0; j < 64; j++)
00710 acc += tone_level_idx_temp[ch][sb][j];
00711 if (acc)
00712 tmp = c * 256 / (acc & 0xffff);
00713 multres = 0x66666667 * (acc * 10);
00714 esp_40 = (multres >> 32) / 8 + ((multres & 0xffffffff) >> 31);
00715 for (ch = 0; ch < nb_channels; ch++)
00716 for (sb = 0; sb < 30; sb++)
00717 for (j = 0; j < 64; j++) {
00718 comp = tone_level_idx_temp[ch][sb][j]* esp_40 * 10;
00719 if (comp < 0)
00720 comp += 0xff;
00721 comp /= 256;
00722 switch(sb) {
00723 case 0:
00724 if (comp < 30)
00725 comp = 30;
00726 comp += 15;
00727 break;
00728 case 1:
00729 if (comp < 24)
00730 comp = 24;
00731 comp += 10;
00732 break;
00733 case 2:
00734 case 3:
00735 case 4:
00736 if (comp < 16)
00737 comp = 16;
00738 }
00739 if (comp <= 5)
00740 tmp = 0;
00741 else if (comp <= 10)
00742 tmp = 10;
00743 else if (comp <= 16)
00744 tmp = 16;
00745 else if (comp <= 24)
00746 tmp = -1;
00747 else
00748 tmp = 0;
00749 coding_method[ch][sb][j] = ((tmp & 0xfffa) + 30 )& 0xff;
00750 }
00751 for (sb = 0; sb < 30; sb++)
00752 fix_coding_method_array(sb, nb_channels, coding_method);
00753 for (ch = 0; ch < nb_channels; ch++)
00754 for (sb = 0; sb < 30; sb++)
00755 for (j = 0; j < 64; j++)
00756 if (sb >= 10) {
00757 if (coding_method[ch][sb][j] < 10)
00758 coding_method[ch][sb][j] = 10;
00759 } else {
00760 if (sb >= 2) {
00761 if (coding_method[ch][sb][j] < 16)
00762 coding_method[ch][sb][j] = 16;
00763 } else {
00764 if (coding_method[ch][sb][j] < 30)
00765 coding_method[ch][sb][j] = 30;
00766 }
00767 }
00768 } else {
00769 for (ch = 0; ch < nb_channels; ch++)
00770 for (sb = 0; sb < 30; sb++)
00771 for (j = 0; j < 64; j++)
00772 coding_method[ch][sb][j] = coding_method_table[cm_table_select][sb];
00773 }
00774
00775 return;
00776 }
00777
00778
00790 static void synthfilt_build_sb_samples (QDM2Context *q, GetBitContext *gb, int length, int sb_min, int sb_max)
00791 {
00792 int sb, j, k, n, ch, run, channels;
00793 int joined_stereo, zero_encoding, chs;
00794 int type34_first;
00795 float type34_div = 0;
00796 float type34_predictor;
00797 float samples[10], sign_bits[16];
00798
00799 if (length == 0) {
00800
00801 for (sb=sb_min; sb < sb_max; sb++)
00802 build_sb_samples_from_noise (q, sb);
00803
00804 return;
00805 }
00806
00807 for (sb = sb_min; sb < sb_max; sb++) {
00808 FIX_NOISE_IDX(q->noise_idx);
00809
00810 channels = q->nb_channels;
00811
00812 if (q->nb_channels <= 1 || sb < 12)
00813 joined_stereo = 0;
00814 else if (sb >= 24)
00815 joined_stereo = 1;
00816 else
00817 joined_stereo = (BITS_LEFT(length,gb) >= 1) ? get_bits1 (gb) : 0;
00818
00819 if (joined_stereo) {
00820 if (BITS_LEFT(length,gb) >= 16)
00821 for (j = 0; j < 16; j++)
00822 sign_bits[j] = get_bits1 (gb);
00823
00824 for (j = 0; j < 64; j++)
00825 if (q->coding_method[1][sb][j] > q->coding_method[0][sb][j])
00826 q->coding_method[0][sb][j] = q->coding_method[1][sb][j];
00827
00828 fix_coding_method_array(sb, q->nb_channels, q->coding_method);
00829 channels = 1;
00830 }
00831
00832 for (ch = 0; ch < channels; ch++) {
00833 zero_encoding = (BITS_LEFT(length,gb) >= 1) ? get_bits1(gb) : 0;
00834 type34_predictor = 0.0;
00835 type34_first = 1;
00836
00837 for (j = 0; j < 128; ) {
00838 switch (q->coding_method[ch][sb][j / 2]) {
00839 case 8:
00840 if (BITS_LEFT(length,gb) >= 10) {
00841 if (zero_encoding) {
00842 for (k = 0; k < 5; k++) {
00843 if ((j + 2 * k) >= 128)
00844 break;
00845 samples[2 * k] = get_bits1(gb) ? dequant_1bit[joined_stereo][2 * get_bits1(gb)] : 0;
00846 }
00847 } else {
00848 n = get_bits(gb, 8);
00849 for (k = 0; k < 5; k++)
00850 samples[2 * k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
00851 }
00852 for (k = 0; k < 5; k++)
00853 samples[2 * k + 1] = SB_DITHERING_NOISE(sb,q->noise_idx);
00854 } else {
00855 for (k = 0; k < 10; k++)
00856 samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
00857 }
00858 run = 10;
00859 break;
00860
00861 case 10:
00862 if (BITS_LEFT(length,gb) >= 1) {
00863 float f = 0.81;
00864
00865 if (get_bits1(gb))
00866 f = -f;
00867 f -= noise_samples[((sb + 1) * (j +5 * ch + 1)) & 127] * 9.0 / 40.0;
00868 samples[0] = f;
00869 } else {
00870 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
00871 }
00872 run = 1;
00873 break;
00874
00875 case 16:
00876 if (BITS_LEFT(length,gb) >= 10) {
00877 if (zero_encoding) {
00878 for (k = 0; k < 5; k++) {
00879 if ((j + k) >= 128)
00880 break;
00881 samples[k] = (get_bits1(gb) == 0) ? 0 : dequant_1bit[joined_stereo][2 * get_bits1(gb)];
00882 }
00883 } else {
00884 n = get_bits (gb, 8);
00885 for (k = 0; k < 5; k++)
00886 samples[k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
00887 }
00888 } else {
00889 for (k = 0; k < 5; k++)
00890 samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
00891 }
00892 run = 5;
00893 break;
00894
00895 case 24:
00896 if (BITS_LEFT(length,gb) >= 7) {
00897 n = get_bits(gb, 7);
00898 for (k = 0; k < 3; k++)
00899 samples[k] = (random_dequant_type24[n][k] - 2.0) * 0.5;
00900 } else {
00901 for (k = 0; k < 3; k++)
00902 samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
00903 }
00904 run = 3;
00905 break;
00906
00907 case 30:
00908 if (BITS_LEFT(length,gb) >= 4) {
00909 unsigned index = qdm2_get_vlc(gb, &vlc_tab_type30, 0, 1);
00910 if (index < FF_ARRAY_ELEMS(type30_dequant)) {
00911 samples[0] = type30_dequant[index];
00912 } else
00913 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
00914 } else
00915 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
00916
00917 run = 1;
00918 break;
00919
00920 case 34:
00921 if (BITS_LEFT(length,gb) >= 7) {
00922 if (type34_first) {
00923 type34_div = (float)(1 << get_bits(gb, 2));
00924 samples[0] = ((float)get_bits(gb, 5) - 16.0) / 15.0;
00925 type34_predictor = samples[0];
00926 type34_first = 0;
00927 } else {
00928 unsigned index = qdm2_get_vlc(gb, &vlc_tab_type34, 0, 1);
00929 if (index < FF_ARRAY_ELEMS(type34_delta)) {
00930 samples[0] = type34_delta[index] / type34_div + type34_predictor;
00931 type34_predictor = samples[0];
00932 } else
00933 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
00934 }
00935 } else {
00936 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
00937 }
00938 run = 1;
00939 break;
00940
00941 default:
00942 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
00943 run = 1;
00944 break;
00945 }
00946
00947 if (joined_stereo) {
00948 float tmp[10][MPA_MAX_CHANNELS];
00949
00950 for (k = 0; k < run; k++) {
00951 tmp[k][0] = samples[k];
00952 tmp[k][1] = (sign_bits[(j + k) / 8]) ? -samples[k] : samples[k];
00953 }
00954 for (chs = 0; chs < q->nb_channels; chs++)
00955 for (k = 0; k < run; k++)
00956 if ((j + k) < 128)
00957 q->sb_samples[chs][j + k][sb] = (int32_t)(f2i_scale * q->tone_level[chs][sb][((j + k)/2)] * tmp[k][chs] + .5);
00958 } else {
00959 for (k = 0; k < run; k++)
00960 if ((j + k) < 128)
00961 q->sb_samples[ch][j + k][sb] = (int32_t)(f2i_scale * q->tone_level[ch][sb][(j + k)/2] * samples[k] + .5);
00962 }
00963
00964 j += run;
00965 }
00966 }
00967 }
00968 }
00969
00970
00981 static void init_quantized_coeffs_elem0 (int8_t *quantized_coeffs, GetBitContext *gb, int length)
00982 {
00983 int i, k, run, level, diff;
00984
00985 if (BITS_LEFT(length,gb) < 16)
00986 return;
00987 level = qdm2_get_vlc(gb, &vlc_tab_level, 0, 2);
00988
00989 quantized_coeffs[0] = level;
00990
00991 for (i = 0; i < 7; ) {
00992 if (BITS_LEFT(length,gb) < 16)
00993 break;
00994 run = qdm2_get_vlc(gb, &vlc_tab_run, 0, 1) + 1;
00995
00996 if (BITS_LEFT(length,gb) < 16)
00997 break;
00998 diff = qdm2_get_se_vlc(&vlc_tab_diff, gb, 2);
00999
01000 for (k = 1; k <= run; k++)
01001 quantized_coeffs[i + k] = (level + ((k * diff) / run));
01002
01003 level += diff;
01004 i += run;
01005 }
01006 }
01007
01008
01018 static void init_tone_level_dequantization (QDM2Context *q, GetBitContext *gb, int length)
01019 {
01020 int sb, j, k, n, ch;
01021
01022 for (ch = 0; ch < q->nb_channels; ch++) {
01023 init_quantized_coeffs_elem0(q->quantized_coeffs[ch][0], gb, length);
01024
01025 if (BITS_LEFT(length,gb) < 16) {
01026 memset(q->quantized_coeffs[ch][0], 0, 8);
01027 break;
01028 }
01029 }
01030
01031 n = q->sub_sampling + 1;
01032
01033 for (sb = 0; sb < n; sb++)
01034 for (ch = 0; ch < q->nb_channels; ch++)
01035 for (j = 0; j < 8; j++) {
01036 if (BITS_LEFT(length,gb) < 1)
01037 break;
01038 if (get_bits1(gb)) {
01039 for (k=0; k < 8; k++) {
01040 if (BITS_LEFT(length,gb) < 16)
01041 break;
01042 q->tone_level_idx_hi1[ch][sb][j][k] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_hi1, 0, 2);
01043 }
01044 } else {
01045 for (k=0; k < 8; k++)
01046 q->tone_level_idx_hi1[ch][sb][j][k] = 0;
01047 }
01048 }
01049
01050 n = QDM2_SB_USED(q->sub_sampling) - 4;
01051
01052 for (sb = 0; sb < n; sb++)
01053 for (ch = 0; ch < q->nb_channels; ch++) {
01054 if (BITS_LEFT(length,gb) < 16)
01055 break;
01056 q->tone_level_idx_hi2[ch][sb] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_hi2, 0, 2);
01057 if (sb > 19)
01058 q->tone_level_idx_hi2[ch][sb] -= 16;
01059 else
01060 for (j = 0; j < 8; j++)
01061 q->tone_level_idx_mid[ch][sb][j] = -16;
01062 }
01063
01064 n = QDM2_SB_USED(q->sub_sampling) - 5;
01065
01066 for (sb = 0; sb < n; sb++)
01067 for (ch = 0; ch < q->nb_channels; ch++)
01068 for (j = 0; j < 8; j++) {
01069 if (BITS_LEFT(length,gb) < 16)
01070 break;
01071 q->tone_level_idx_mid[ch][sb][j] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_mid, 0, 2) - 32;
01072 }
01073 }
01074
01081 static void process_subpacket_9 (QDM2Context *q, QDM2SubPNode *node)
01082 {
01083 GetBitContext gb;
01084 int i, j, k, n, ch, run, level, diff;
01085
01086 init_get_bits(&gb, node->packet->data, node->packet->size*8);
01087
01088 n = coeff_per_sb_for_avg[q->coeff_per_sb_select][QDM2_SB_USED(q->sub_sampling) - 1] + 1;
01089
01090 for (i = 1; i < n; i++)
01091 for (ch=0; ch < q->nb_channels; ch++) {
01092 level = qdm2_get_vlc(&gb, &vlc_tab_level, 0, 2);
01093 q->quantized_coeffs[ch][i][0] = level;
01094
01095 for (j = 0; j < (8 - 1); ) {
01096 run = qdm2_get_vlc(&gb, &vlc_tab_run, 0, 1) + 1;
01097 diff = qdm2_get_se_vlc(&vlc_tab_diff, &gb, 2);
01098
01099 for (k = 1; k <= run; k++)
01100 q->quantized_coeffs[ch][i][j + k] = (level + ((k*diff) / run));
01101
01102 level += diff;
01103 j += run;
01104 }
01105 }
01106
01107 for (ch = 0; ch < q->nb_channels; ch++)
01108 for (i = 0; i < 8; i++)
01109 q->quantized_coeffs[ch][0][i] = 0;
01110 }
01111
01112
01120 static void process_subpacket_10 (QDM2Context *q, QDM2SubPNode *node, int length)
01121 {
01122 GetBitContext gb;
01123
01124 init_get_bits(&gb, ((node == NULL) ? empty_buffer : node->packet->data), ((node == NULL) ? 0 : node->packet->size*8));
01125
01126 if (length != 0) {
01127 init_tone_level_dequantization(q, &gb, length);
01128 fill_tone_level_array(q, 1);
01129 } else {
01130 fill_tone_level_array(q, 0);
01131 }
01132 }
01133
01134
01142 static void process_subpacket_11 (QDM2Context *q, QDM2SubPNode *node, int length)
01143 {
01144 GetBitContext gb;
01145
01146 init_get_bits(&gb, ((node == NULL) ? empty_buffer : node->packet->data), ((node == NULL) ? 0 : node->packet->size*8));
01147 if (length >= 32) {
01148 int c = get_bits (&gb, 13);
01149
01150 if (c > 3)
01151 fill_coding_method_array (q->tone_level_idx, q->tone_level_idx_temp, q->coding_method,
01152 q->nb_channels, 8*c, q->superblocktype_2_3, q->cm_table_select);
01153 }
01154
01155 synthfilt_build_sb_samples(q, &gb, length, 0, 8);
01156 }
01157
01158
01166 static void process_subpacket_12 (QDM2Context *q, QDM2SubPNode *node, int length)
01167 {
01168 GetBitContext gb;
01169
01170 init_get_bits(&gb, ((node == NULL) ? empty_buffer : node->packet->data), ((node == NULL) ? 0 : node->packet->size*8));
01171 synthfilt_build_sb_samples(q, &gb, length, 8, QDM2_SB_USED(q->sub_sampling));
01172 }
01173
01174
01175
01176
01177
01178
01179
01180 static void process_synthesis_subpackets (QDM2Context *q, QDM2SubPNode *list)
01181 {
01182 QDM2SubPNode *nodes[4];
01183
01184 nodes[0] = qdm2_search_subpacket_type_in_list(list, 9);
01185 if (nodes[0] != NULL)
01186 process_subpacket_9(q, nodes[0]);
01187
01188 nodes[1] = qdm2_search_subpacket_type_in_list(list, 10);
01189 if (nodes[1] != NULL)
01190 process_subpacket_10(q, nodes[1], nodes[1]->packet->size << 3);
01191 else
01192 process_subpacket_10(q, NULL, 0);
01193
01194 nodes[2] = qdm2_search_subpacket_type_in_list(list, 11);
01195 if (nodes[0] != NULL && nodes[1] != NULL && nodes[2] != NULL)
01196 process_subpacket_11(q, nodes[2], (nodes[2]->packet->size << 3));
01197 else
01198 process_subpacket_11(q, NULL, 0);
01199
01200 nodes[3] = qdm2_search_subpacket_type_in_list(list, 12);
01201 if (nodes[0] != NULL && nodes[1] != NULL && nodes[3] != NULL)
01202 process_subpacket_12(q, nodes[3], (nodes[3]->packet->size << 3));
01203 else
01204 process_subpacket_12(q, NULL, 0);
01205 }
01206
01207
01208
01209
01210
01211
01212
01213 static void qdm2_decode_super_block (QDM2Context *q)
01214 {
01215 GetBitContext gb;
01216 QDM2SubPacket header, *packet;
01217 int i, packet_bytes, sub_packet_size, sub_packets_D;
01218 unsigned int next_index = 0;
01219
01220 memset(q->tone_level_idx_hi1, 0, sizeof(q->tone_level_idx_hi1));
01221 memset(q->tone_level_idx_mid, 0, sizeof(q->tone_level_idx_mid));
01222 memset(q->tone_level_idx_hi2, 0, sizeof(q->tone_level_idx_hi2));
01223
01224 q->sub_packets_B = 0;
01225 sub_packets_D = 0;
01226
01227 average_quantized_coeffs(q);
01228
01229 init_get_bits(&gb, q->compressed_data, q->compressed_size*8);
01230 qdm2_decode_sub_packet_header(&gb, &header);
01231
01232 if (header.type < 2 || header.type >= 8) {
01233 q->has_errors = 1;
01234 av_log(NULL,AV_LOG_ERROR,"bad superblock type\n");
01235 return;
01236 }
01237
01238 q->superblocktype_2_3 = (header.type == 2 || header.type == 3);
01239 packet_bytes = (q->compressed_size - get_bits_count(&gb) / 8);
01240
01241 init_get_bits(&gb, header.data, header.size*8);
01242
01243 if (header.type == 2 || header.type == 4 || header.type == 5) {
01244 int csum = 257 * get_bits(&gb, 8) + 2 * get_bits(&gb, 8);
01245
01246 csum = qdm2_packet_checksum(q->compressed_data, q->checksum_size, csum);
01247
01248 if (csum != 0) {
01249 q->has_errors = 1;
01250 av_log(NULL,AV_LOG_ERROR,"bad packet checksum\n");
01251 return;
01252 }
01253 }
01254
01255 q->sub_packet_list_B[0].packet = NULL;
01256 q->sub_packet_list_D[0].packet = NULL;
01257
01258 for (i = 0; i < 6; i++)
01259 if (--q->fft_level_exp[i] < 0)
01260 q->fft_level_exp[i] = 0;
01261
01262 for (i = 0; packet_bytes > 0; i++) {
01263 int j;
01264
01265 q->sub_packet_list_A[i].next = NULL;
01266
01267 if (i > 0) {
01268 q->sub_packet_list_A[i - 1].next = &q->sub_packet_list_A[i];
01269
01270
01271 init_get_bits(&gb, header.data, header.size*8);
01272 skip_bits(&gb, next_index*8);
01273
01274 if (next_index >= header.size)
01275 break;
01276 }
01277
01278
01279 packet = &q->sub_packets[i];
01280 qdm2_decode_sub_packet_header(&gb, packet);
01281 next_index = packet->size + get_bits_count(&gb) / 8;
01282 sub_packet_size = ((packet->size > 0xff) ? 1 : 0) + packet->size + 2;
01283
01284 if (packet->type == 0)
01285 break;
01286
01287 if (sub_packet_size > packet_bytes) {
01288 if (packet->type != 10 && packet->type != 11 && packet->type != 12)
01289 break;
01290 packet->size += packet_bytes - sub_packet_size;
01291 }
01292
01293 packet_bytes -= sub_packet_size;
01294
01295
01296 q->sub_packet_list_A[i].packet = packet;
01297
01298
01299 if (packet->type == 8) {
01300 SAMPLES_NEEDED_2("packet type 8");
01301 return;
01302 } else if (packet->type >= 9 && packet->type <= 12) {
01303
01304 QDM2_LIST_ADD(q->sub_packet_list_D, sub_packets_D, packet);
01305 } else if (packet->type == 13) {
01306 for (j = 0; j < 6; j++)
01307 q->fft_level_exp[j] = get_bits(&gb, 6);
01308 } else if (packet->type == 14) {
01309 for (j = 0; j < 6; j++)
01310 q->fft_level_exp[j] = qdm2_get_vlc(&gb, &fft_level_exp_vlc, 0, 2);
01311 } else if (packet->type == 15) {
01312 SAMPLES_NEEDED_2("packet type 15")
01313 return;
01314 } else if (packet->type >= 16 && packet->type < 48 && !fft_subpackets[packet->type - 16]) {
01315
01316 QDM2_LIST_ADD(q->sub_packet_list_B, q->sub_packets_B, packet);
01317 }
01318 }
01319
01320
01321 if (q->sub_packet_list_D[0].packet != NULL) {
01322 process_synthesis_subpackets(q, q->sub_packet_list_D);
01323 q->do_synth_filter = 1;
01324 } else if (q->do_synth_filter) {
01325 process_subpacket_10(q, NULL, 0);
01326 process_subpacket_11(q, NULL, 0);
01327 process_subpacket_12(q, NULL, 0);
01328 }
01329
01330 }
01331
01332
01333 static void qdm2_fft_init_coefficient (QDM2Context *q, int sub_packet,
01334 int offset, int duration, int channel,
01335 int exp, int phase)
01336 {
01337 if (q->fft_coefs_min_index[duration] < 0)
01338 q->fft_coefs_min_index[duration] = q->fft_coefs_index;
01339
01340 q->fft_coefs[q->fft_coefs_index].sub_packet = ((sub_packet >= 16) ? (sub_packet - 16) : sub_packet);
01341 q->fft_coefs[q->fft_coefs_index].channel = channel;
01342 q->fft_coefs[q->fft_coefs_index].offset = offset;
01343 q->fft_coefs[q->fft_coefs_index].exp = exp;
01344 q->fft_coefs[q->fft_coefs_index].phase = phase;
01345 q->fft_coefs_index++;
01346 }
01347
01348
01349 static void qdm2_fft_decode_tones (QDM2Context *q, int duration, GetBitContext *gb, int b)
01350 {
01351 int channel, stereo, phase, exp;
01352 int local_int_4, local_int_8, stereo_phase, local_int_10;
01353 int local_int_14, stereo_exp, local_int_20, local_int_28;
01354 int n, offset;
01355
01356 local_int_4 = 0;
01357 local_int_28 = 0;
01358 local_int_20 = 2;
01359 local_int_8 = (4 - duration);
01360 local_int_10 = 1 << (q->group_order - duration - 1);
01361 offset = 1;
01362
01363 while (get_bits_left(gb)>0) {
01364 if (q->superblocktype_2_3) {
01365 while ((n = qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2)) < 2) {
01366 offset = 1;
01367 if (n == 0) {
01368 local_int_4 += local_int_10;
01369 local_int_28 += (1 << local_int_8);
01370 } else {
01371 local_int_4 += 8*local_int_10;
01372 local_int_28 += (8 << local_int_8);
01373 }
01374 }
01375 offset += (n - 2);
01376 } else {
01377 offset += qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2);
01378 while (offset >= (local_int_10 - 1)) {
01379 offset += (1 - (local_int_10 - 1));
01380 local_int_4 += local_int_10;
01381 local_int_28 += (1 << local_int_8);
01382 }
01383 }
01384
01385 if (local_int_4 >= q->group_size)
01386 return;
01387
01388 local_int_14 = (offset >> local_int_8);
01389 if (local_int_14 >= FF_ARRAY_ELEMS(fft_level_index_table))
01390 return;
01391
01392 if (q->nb_channels > 1) {
01393 channel = get_bits1(gb);
01394 stereo = get_bits1(gb);
01395 } else {
01396 channel = 0;
01397 stereo = 0;
01398 }
01399
01400 exp = qdm2_get_vlc(gb, (b ? &fft_level_exp_vlc : &fft_level_exp_alt_vlc), 0, 2);
01401 exp += q->fft_level_exp[fft_level_index_table[local_int_14]];
01402 exp = (exp < 0) ? 0 : exp;
01403
01404 phase = get_bits(gb, 3);
01405 stereo_exp = 0;
01406 stereo_phase = 0;
01407
01408 if (stereo) {
01409 stereo_exp = (exp - qdm2_get_vlc(gb, &fft_stereo_exp_vlc, 0, 1));
01410 stereo_phase = (phase - qdm2_get_vlc(gb, &fft_stereo_phase_vlc, 0, 1));
01411 if (stereo_phase < 0)
01412 stereo_phase += 8;
01413 }
01414
01415 if (q->frequency_range > (local_int_14 + 1)) {
01416 int sub_packet = (local_int_20 + local_int_28);
01417
01418 qdm2_fft_init_coefficient(q, sub_packet, offset, duration, channel, exp, phase);
01419 if (stereo)
01420 qdm2_fft_init_coefficient(q, sub_packet, offset, duration, (1 - channel), stereo_exp, stereo_phase);
01421 }
01422
01423 offset++;
01424 }
01425 }
01426
01427
01428 static void qdm2_decode_fft_packets (QDM2Context *q)
01429 {
01430 int i, j, min, max, value, type, unknown_flag;
01431 GetBitContext gb;
01432
01433 if (q->sub_packet_list_B[0].packet == NULL)
01434 return;
01435
01436
01437 q->fft_coefs_index = 0;
01438 for (i=0; i < 5; i++)
01439 q->fft_coefs_min_index[i] = -1;
01440
01441
01442 for (i = 0, max = 256; i < q->sub_packets_B; i++) {
01443 QDM2SubPacket *packet= NULL;
01444
01445
01446 for (j = 0, min = 0; j < q->sub_packets_B; j++) {
01447 value = q->sub_packet_list_B[j].packet->type;
01448 if (value > min && value < max) {
01449 min = value;
01450 packet = q->sub_packet_list_B[j].packet;
01451 }
01452 }
01453
01454 max = min;
01455
01456
01457 if (!packet)
01458 return;
01459
01460 if (i == 0 && (packet->type < 16 || packet->type >= 48 || fft_subpackets[packet->type - 16]))
01461 return;
01462
01463
01464 init_get_bits (&gb, packet->data, packet->size*8);
01465
01466 if (packet->type >= 32 && packet->type < 48 && !fft_subpackets[packet->type - 16])
01467 unknown_flag = 1;
01468 else
01469 unknown_flag = 0;
01470
01471 type = packet->type;
01472
01473 if ((type >= 17 && type < 24) || (type >= 33 && type < 40)) {
01474 int duration = q->sub_sampling + 5 - (type & 15);
01475
01476 if (duration >= 0 && duration < 4)
01477 qdm2_fft_decode_tones(q, duration, &gb, unknown_flag);
01478 } else if (type == 31) {
01479 for (j=0; j < 4; j++)
01480 qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
01481 } else if (type == 46) {
01482 for (j=0; j < 6; j++)
01483 q->fft_level_exp[j] = get_bits(&gb, 6);
01484 for (j=0; j < 4; j++)
01485 qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
01486 }
01487 }
01488
01489
01490 for (i = 0, j = -1; i < 5; i++)
01491 if (q->fft_coefs_min_index[i] >= 0) {
01492 if (j >= 0)
01493 q->fft_coefs_max_index[j] = q->fft_coefs_min_index[i];
01494 j = i;
01495 }
01496 if (j >= 0)
01497 q->fft_coefs_max_index[j] = q->fft_coefs_index;
01498 }
01499
01500
01501 static void qdm2_fft_generate_tone (QDM2Context *q, FFTTone *tone)
01502 {
01503 float level, f[6];
01504 int i;
01505 QDM2Complex c;
01506 const double iscale = 2.0*M_PI / 512.0;
01507
01508 tone->phase += tone->phase_shift;
01509
01510
01511 level = fft_tone_envelope_table[tone->duration][tone->time_index] * tone->level;
01512 c.im = level * sin(tone->phase*iscale);
01513 c.re = level * cos(tone->phase*iscale);
01514
01515
01516 if (tone->duration >= 3 || tone->cutoff >= 3) {
01517 tone->complex[0].im += c.im;
01518 tone->complex[0].re += c.re;
01519 tone->complex[1].im -= c.im;
01520 tone->complex[1].re -= c.re;
01521 } else {
01522 f[1] = -tone->table[4];
01523 f[0] = tone->table[3] - tone->table[0];
01524 f[2] = 1.0 - tone->table[2] - tone->table[3];
01525 f[3] = tone->table[1] + tone->table[4] - 1.0;
01526 f[4] = tone->table[0] - tone->table[1];
01527 f[5] = tone->table[2];
01528 for (i = 0; i < 2; i++) {
01529 tone->complex[fft_cutoff_index_table[tone->cutoff][i]].re += c.re * f[i];
01530 tone->complex[fft_cutoff_index_table[tone->cutoff][i]].im += c.im *((tone->cutoff <= i) ? -f[i] : f[i]);
01531 }
01532 for (i = 0; i < 4; i++) {
01533 tone->complex[i].re += c.re * f[i+2];
01534 tone->complex[i].im += c.im * f[i+2];
01535 }
01536 }
01537
01538
01539 if (++tone->time_index < ((1 << (5 - tone->duration)) - 1)) {
01540 memcpy(&q->fft_tones[q->fft_tone_end], tone, sizeof(FFTTone));
01541 q->fft_tone_end = (q->fft_tone_end + 1) % 1000;
01542 }
01543 }
01544
01545
01546 static void qdm2_fft_tone_synthesizer (QDM2Context *q, int sub_packet)
01547 {
01548 int i, j, ch;
01549 const double iscale = 0.25 * M_PI;
01550
01551 for (ch = 0; ch < q->channels; ch++) {
01552 memset(q->fft.complex[ch], 0, q->fft_size * sizeof(QDM2Complex));
01553 }
01554
01555
01556
01557 if (q->fft_coefs_min_index[4] >= 0)
01558 for (i = q->fft_coefs_min_index[4]; i < q->fft_coefs_max_index[4]; i++) {
01559 float level;
01560 QDM2Complex c;
01561
01562 if (q->fft_coefs[i].sub_packet != sub_packet)
01563 break;
01564
01565 ch = (q->channels == 1) ? 0 : q->fft_coefs[i].channel;
01566 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];
01567
01568 c.re = level * cos(q->fft_coefs[i].phase * iscale);
01569 c.im = level * sin(q->fft_coefs[i].phase * iscale);
01570 q->fft.complex[ch][q->fft_coefs[i].offset + 0].re += c.re;
01571 q->fft.complex[ch][q->fft_coefs[i].offset + 0].im += c.im;
01572 q->fft.complex[ch][q->fft_coefs[i].offset + 1].re -= c.re;
01573 q->fft.complex[ch][q->fft_coefs[i].offset + 1].im -= c.im;
01574 }
01575
01576
01577 for (i = q->fft_tone_end; i != q->fft_tone_start; ) {
01578 qdm2_fft_generate_tone(q, &q->fft_tones[q->fft_tone_start]);
01579 q->fft_tone_start = (q->fft_tone_start + 1) % 1000;
01580 }
01581
01582
01583 for (i = 0; i < 4; i++)
01584 if (q->fft_coefs_min_index[i] >= 0) {
01585 for (j = q->fft_coefs_min_index[i]; j < q->fft_coefs_max_index[i]; j++) {
01586 int offset, four_i;
01587 FFTTone tone;
01588
01589 if (q->fft_coefs[j].sub_packet != sub_packet)
01590 break;
01591
01592 four_i = (4 - i);
01593 offset = q->fft_coefs[j].offset >> four_i;
01594 ch = (q->channels == 1) ? 0 : q->fft_coefs[j].channel;
01595
01596 if (offset < q->frequency_range) {
01597 if (offset < 2)
01598 tone.cutoff = offset;
01599 else
01600 tone.cutoff = (offset >= 60) ? 3 : 2;
01601
01602 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];
01603 tone.complex = &q->fft.complex[ch][offset];
01604 tone.table = fft_tone_sample_table[i][q->fft_coefs[j].offset - (offset << four_i)];
01605 tone.phase = 64 * q->fft_coefs[j].phase - (offset << 8) - 128;
01606 tone.phase_shift = (2 * q->fft_coefs[j].offset + 1) << (7 - four_i);
01607 tone.duration = i;
01608 tone.time_index = 0;
01609
01610 qdm2_fft_generate_tone(q, &tone);
01611 }
01612 }
01613 q->fft_coefs_min_index[i] = j;
01614 }
01615 }
01616
01617
01618 static void qdm2_calculate_fft (QDM2Context *q, int channel, int sub_packet)
01619 {
01620 const float gain = (q->channels == 1 && q->nb_channels == 2) ? 0.5f : 1.0f;
01621 int i;
01622 q->fft.complex[channel][0].re *= 2.0f;
01623 q->fft.complex[channel][0].im = 0.0f;
01624 ff_rdft_calc(&q->rdft_ctx, (FFTSample *)q->fft.complex[channel]);
01625
01626 for (i = 0; i < ((q->fft_frame_size + 15) & ~15); i++)
01627 q->output_buffer[q->channels * i + channel] += ((float *) q->fft.complex[channel])[i] * gain;
01628 }
01629
01630
01635 static void qdm2_synthesis_filter (QDM2Context *q, int index)
01636 {
01637 OUT_INT samples[MPA_MAX_CHANNELS * MPA_FRAME_SIZE];
01638 int i, k, ch, sb_used, sub_sampling, dither_state = 0;
01639
01640
01641 sb_used = QDM2_SB_USED(q->sub_sampling);
01642
01643 for (ch = 0; ch < q->channels; ch++)
01644 for (i = 0; i < 8; i++)
01645 for (k=sb_used; k < SBLIMIT; k++)
01646 q->sb_samples[ch][(8 * index) + i][k] = 0;
01647
01648 for (ch = 0; ch < q->nb_channels; ch++) {
01649 OUT_INT *samples_ptr = samples + ch;
01650
01651 for (i = 0; i < 8; i++) {
01652 ff_mpa_synth_filter(q->synth_buf[ch], &(q->synth_buf_offset[ch]),
01653 mpa_window, &dither_state,
01654 samples_ptr, q->nb_channels,
01655 q->sb_samples[ch][(8 * index) + i]);
01656 samples_ptr += 32 * q->nb_channels;
01657 }
01658 }
01659
01660
01661 sub_sampling = (4 >> q->sub_sampling);
01662
01663 for (ch = 0; ch < q->channels; ch++)
01664 for (i = 0; i < q->frame_size; i++)
01665 q->output_buffer[q->channels * i + ch] += (float)(samples[q->nb_channels * sub_sampling * i + ch] >> (sizeof(OUT_INT)*8-16));
01666 }
01667
01668
01674 static av_cold void qdm2_init(QDM2Context *q) {
01675 static int initialized = 0;
01676
01677 if (initialized != 0)
01678 return;
01679 initialized = 1;
01680
01681 qdm2_init_vlc();
01682 ff_mpa_synth_init(mpa_window);
01683 softclip_table_init();
01684 rnd_table_init();
01685 init_noise_samples();
01686
01687 av_log(NULL, AV_LOG_DEBUG, "init done\n");
01688 }
01689
01690
01691 #if 0
01692 static void dump_context(QDM2Context *q)
01693 {
01694 int i;
01695 #define PRINT(a,b) av_log(NULL,AV_LOG_DEBUG," %s = %d\n", a, b);
01696 PRINT("compressed_data",q->compressed_data);
01697 PRINT("compressed_size",q->compressed_size);
01698 PRINT("frame_size",q->frame_size);
01699 PRINT("checksum_size",q->checksum_size);
01700 PRINT("channels",q->channels);
01701 PRINT("nb_channels",q->nb_channels);
01702 PRINT("fft_frame_size",q->fft_frame_size);
01703 PRINT("fft_size",q->fft_size);
01704 PRINT("sub_sampling",q->sub_sampling);
01705 PRINT("fft_order",q->fft_order);
01706 PRINT("group_order",q->group_order);
01707 PRINT("group_size",q->group_size);
01708 PRINT("sub_packet",q->sub_packet);
01709 PRINT("frequency_range",q->frequency_range);
01710 PRINT("has_errors",q->has_errors);
01711 PRINT("fft_tone_end",q->fft_tone_end);
01712 PRINT("fft_tone_start",q->fft_tone_start);
01713 PRINT("fft_coefs_index",q->fft_coefs_index);
01714 PRINT("coeff_per_sb_select",q->coeff_per_sb_select);
01715 PRINT("cm_table_select",q->cm_table_select);
01716 PRINT("noise_idx",q->noise_idx);
01717
01718 for (i = q->fft_tone_start; i < q->fft_tone_end; i++)
01719 {
01720 FFTTone *t = &q->fft_tones[i];
01721
01722 av_log(NULL,AV_LOG_DEBUG,"Tone (%d) dump:\n", i);
01723 av_log(NULL,AV_LOG_DEBUG," level = %f\n", t->level);
01724
01725 PRINT(" phase", t->phase);
01726 PRINT(" phase_shift", t->phase_shift);
01727 PRINT(" duration", t->duration);
01728 PRINT(" samples_im", t->samples_im);
01729 PRINT(" samples_re", t->samples_re);
01730 PRINT(" table", t->table);
01731 }
01732
01733 }
01734 #endif
01735
01736
01740 static av_cold int qdm2_decode_init(AVCodecContext *avctx)
01741 {
01742 QDM2Context *s = avctx->priv_data;
01743 uint8_t *extradata;
01744 int extradata_size;
01745 int tmp_val, tmp, size;
01746
01747
01748
01749
01750
01751
01752
01753
01754
01755
01756
01757
01758
01759
01760
01761
01762
01763
01764
01765
01766
01767
01768
01769
01770
01771
01772
01773
01774
01775
01776
01777
01778
01779
01780
01781 if (!avctx->extradata || (avctx->extradata_size < 48)) {
01782 av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n");
01783 return -1;
01784 }
01785
01786 extradata = avctx->extradata;
01787 extradata_size = avctx->extradata_size;
01788
01789 while (extradata_size > 7) {
01790 if (!memcmp(extradata, "frmaQDM", 7))
01791 break;
01792 extradata++;
01793 extradata_size--;
01794 }
01795
01796 if (extradata_size < 12) {
01797 av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n",
01798 extradata_size);
01799 return -1;
01800 }
01801
01802 if (memcmp(extradata, "frmaQDM", 7)) {
01803 av_log(avctx, AV_LOG_ERROR, "invalid headers, QDM? not found\n");
01804 return -1;
01805 }
01806
01807 if (extradata[7] == 'C') {
01808
01809 av_log(avctx, AV_LOG_ERROR, "stream is QDMC version 1, which is not supported\n");
01810 return -1;
01811 }
01812
01813 extradata += 8;
01814 extradata_size -= 8;
01815
01816 size = AV_RB32(extradata);
01817
01818 if(size > extradata_size){
01819 av_log(avctx, AV_LOG_ERROR, "extradata size too small, %i < %i\n",
01820 extradata_size, size);
01821 return -1;
01822 }
01823
01824 extradata += 4;
01825 av_log(avctx, AV_LOG_DEBUG, "size: %d\n", size);
01826 if (AV_RB32(extradata) != MKBETAG('Q','D','C','A')) {
01827 av_log(avctx, AV_LOG_ERROR, "invalid extradata, expecting QDCA\n");
01828 return -1;
01829 }
01830
01831 extradata += 8;
01832
01833 avctx->channels = s->nb_channels = s->channels = AV_RB32(extradata);
01834 extradata += 4;
01835 if (s->channels > MPA_MAX_CHANNELS)
01836 return AVERROR_INVALIDDATA;
01837
01838 avctx->sample_rate = AV_RB32(extradata);
01839 extradata += 4;
01840
01841 avctx->bit_rate = AV_RB32(extradata);
01842 extradata += 4;
01843
01844 s->group_size = AV_RB32(extradata);
01845 extradata += 4;
01846
01847 s->fft_size = AV_RB32(extradata);
01848 extradata += 4;
01849
01850 s->checksum_size = AV_RB32(extradata);
01851 extradata += 4;
01852
01853 s->fft_order = av_log2(s->fft_size) + 1;
01854 s->fft_frame_size = 2 * s->fft_size;
01855
01856
01857 s->group_order = av_log2(s->group_size) + 1;
01858 s->frame_size = s->group_size / 16;
01859
01860 if (s->frame_size > QDM2_MAX_FRAME_SIZE)
01861 return AVERROR_INVALIDDATA;
01862
01863 s->sub_sampling = s->fft_order - 7;
01864 s->frequency_range = 255 / (1 << (2 - s->sub_sampling));
01865
01866 switch ((s->sub_sampling * 2 + s->channels - 1)) {
01867 case 0: tmp = 40; break;
01868 case 1: tmp = 48; break;
01869 case 2: tmp = 56; break;
01870 case 3: tmp = 72; break;
01871 case 4: tmp = 80; break;
01872 case 5: tmp = 100;break;
01873 default: tmp=s->sub_sampling; break;
01874 }
01875 tmp_val = 0;
01876 if ((tmp * 1000) < avctx->bit_rate) tmp_val = 1;
01877 if ((tmp * 1440) < avctx->bit_rate) tmp_val = 2;
01878 if ((tmp * 1760) < avctx->bit_rate) tmp_val = 3;
01879 if ((tmp * 2240) < avctx->bit_rate) tmp_val = 4;
01880 s->cm_table_select = tmp_val;
01881
01882 if (s->sub_sampling == 0)
01883 tmp = 7999;
01884 else
01885 tmp = ((-(s->sub_sampling -1)) & 8000) + 20000;
01886
01887
01888
01889
01890
01891 if (tmp < 8000)
01892 s->coeff_per_sb_select = 0;
01893 else if (tmp <= 16000)
01894 s->coeff_per_sb_select = 1;
01895 else
01896 s->coeff_per_sb_select = 2;
01897
01898
01899 if ((s->fft_order < 7) || (s->fft_order > 9)) {
01900 av_log(avctx, AV_LOG_ERROR, "Unknown FFT order (%d), contact the developers!\n", s->fft_order);
01901 return -1;
01902 }
01903
01904 ff_rdft_init(&s->rdft_ctx, s->fft_order, IRDFT);
01905
01906 qdm2_init(s);
01907
01908 avctx->sample_fmt = SAMPLE_FMT_S16;
01909
01910
01911 return 0;
01912 }
01913
01914
01915 static av_cold int qdm2_decode_close(AVCodecContext *avctx)
01916 {
01917 QDM2Context *s = avctx->priv_data;
01918
01919 ff_rdft_end(&s->rdft_ctx);
01920
01921 return 0;
01922 }
01923
01924
01925 static int qdm2_decode (QDM2Context *q, const uint8_t *in, int16_t *out)
01926 {
01927 int ch, i;
01928 const int frame_size = (q->frame_size * q->channels);
01929
01930 if((unsigned)frame_size > FF_ARRAY_ELEMS(q->output_buffer)/2)
01931 return -1;
01932
01933
01934 q->compressed_data = in;
01935 q->compressed_size = q->checksum_size;
01936
01937
01938
01939
01940 memmove(q->output_buffer, &q->output_buffer[frame_size], frame_size * sizeof(float));
01941 memset(&q->output_buffer[frame_size], 0, frame_size * sizeof(float));
01942
01943
01944 if (q->sub_packet == 0) {
01945 q->has_errors = 0;
01946 av_log(NULL,AV_LOG_DEBUG,"Superblock follows\n");
01947 qdm2_decode_super_block(q);
01948 }
01949
01950
01951 if (!q->has_errors) {
01952 if (q->sub_packet == 2)
01953 qdm2_decode_fft_packets(q);
01954
01955 qdm2_fft_tone_synthesizer(q, q->sub_packet);
01956 }
01957
01958
01959 for (ch = 0; ch < q->channels; ch++) {
01960 qdm2_calculate_fft(q, ch, q->sub_packet);
01961
01962 if (!q->has_errors && q->sub_packet_list_C[0].packet != NULL) {
01963 SAMPLES_NEEDED_2("has errors, and C list is not empty")
01964 return -1;
01965 }
01966 }
01967
01968
01969 if (!q->has_errors && q->do_synth_filter)
01970 qdm2_synthesis_filter(q, q->sub_packet);
01971
01972 q->sub_packet = (q->sub_packet + 1) % 16;
01973
01974
01975 for (i = 0; i < frame_size; i++) {
01976 int value = (int)q->output_buffer[i];
01977
01978 if (value > SOFTCLIP_THRESHOLD)
01979 value = (value > HARDCLIP_THRESHOLD) ? 32767 : softclip_table[ value - SOFTCLIP_THRESHOLD];
01980 else if (value < -SOFTCLIP_THRESHOLD)
01981 value = (value < -HARDCLIP_THRESHOLD) ? -32767 : -softclip_table[-value - SOFTCLIP_THRESHOLD];
01982
01983 out[i] = value;
01984 }
01985
01986 return 0;
01987 }
01988
01989
01990 static int qdm2_decode_frame(AVCodecContext *avctx,
01991 void *data, int *data_size,
01992 const uint8_t *buf, int buf_size)
01993 {
01994 QDM2Context *s = avctx->priv_data;
01995 int16_t *out = data;
01996 int i, out_size;
01997
01998 if(!buf)
01999 return 0;
02000 if(buf_size < s->checksum_size)
02001 return -1;
02002
02003 out_size = 16 * s->channels * s->frame_size *
02004 av_get_bits_per_sample_format(avctx->sample_fmt)/8;
02005 if (*data_size < out_size) {
02006 av_log(avctx, AV_LOG_ERROR, "Output buffer is too small\n");
02007 return AVERROR(EINVAL);
02008 }
02009
02010 av_log(avctx, AV_LOG_DEBUG, "decode(%d): %p[%d] -> %p[%d]\n",
02011 buf_size, buf, s->checksum_size, data, *data_size);
02012
02013 for (i = 0; i < 16; i++) {
02014 if (qdm2_decode(s, buf, out) < 0)
02015 return -1;
02016 out += s->channels * s->frame_size;
02017 }
02018
02019 *data_size = out_size;
02020
02021 return buf_size;
02022 }
02023
02024 AVCodec qdm2_decoder =
02025 {
02026 .name = "qdm2",
02027 .type = CODEC_TYPE_AUDIO,
02028 .id = CODEC_ID_QDM2,
02029 .priv_data_size = sizeof(QDM2Context),
02030 .init = qdm2_decode_init,
02031 .close = qdm2_decode_close,
02032 .decode = qdm2_decode_frame,
02033 .long_name = NULL_IF_CONFIG_SMALL("QDesign Music Codec 2"),
02034 };