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