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