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