00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00034 #include <math.h>
00035 #include <stddef.h>
00036 #include <stdio.h>
00037
00038 #define BITSTREAM_READER_LE
00039 #include "avcodec.h"
00040 #include "get_bits.h"
00041 #include "dsputil.h"
00042 #include "rdft.h"
00043 #include "mpegaudiodsp.h"
00044 #include "mpegaudio.h"
00045
00046 #include "qdm2data.h"
00047 #include "qdm2_tablegen.h"
00048
00049 #undef NDEBUG
00050 #include <assert.h>
00051
00052
00053 #define QDM2_LIST_ADD(list, size, packet) \
00054 do { \
00055 if (size > 0) { \
00056 list[size - 1].next = &list[size]; \
00057 } \
00058 list[size].packet = packet; \
00059 list[size].next = NULL; \
00060 size++; \
00061 } while(0)
00062
00063
00064 #define QDM2_SB_USED(sub_sampling) (((sub_sampling) >= 2) ? 30 : 8 << (sub_sampling))
00065
00066 #define FIX_NOISE_IDX(noise_idx) \
00067 if ((noise_idx) >= 3840) \
00068 (noise_idx) -= 3840; \
00069
00070 #define SB_DITHERING_NOISE(sb,noise_idx) (noise_table[(noise_idx)++] * sb_noise_attenuation[(sb)])
00071
00072 #define 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 unsigned index = qdm2_get_vlc(gb, &vlc_tab_type30, 0, 1);
00889 if (index < FF_ARRAY_ELEMS(type30_dequant)) {
00890 samples[0] = type30_dequant[index];
00891 } else
00892 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
00893 } else
00894 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
00895
00896 run = 1;
00897 break;
00898
00899 case 34:
00900 if (BITS_LEFT(length,gb) >= 7) {
00901 if (type34_first) {
00902 type34_div = (float)(1 << get_bits(gb, 2));
00903 samples[0] = ((float)get_bits(gb, 5) - 16.0) / 15.0;
00904 type34_predictor = samples[0];
00905 type34_first = 0;
00906 } else {
00907 unsigned index = qdm2_get_vlc(gb, &vlc_tab_type34, 0, 1);
00908 if (index < FF_ARRAY_ELEMS(type34_delta)) {
00909 samples[0] = type34_delta[index] / type34_div + type34_predictor;
00910 type34_predictor = samples[0];
00911 } else
00912 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
00913 }
00914 } else {
00915 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
00916 }
00917 run = 1;
00918 break;
00919
00920 default:
00921 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
00922 run = 1;
00923 break;
00924 }
00925
00926 if (joined_stereo) {
00927 float tmp[10][MPA_MAX_CHANNELS];
00928
00929 for (k = 0; k < run; k++) {
00930 tmp[k][0] = samples[k];
00931 tmp[k][1] = (sign_bits[(j + k) / 8]) ? -samples[k] : samples[k];
00932 }
00933 for (chs = 0; chs < q->nb_channels; chs++)
00934 for (k = 0; k < run; k++)
00935 if ((j + k) < 128)
00936 q->sb_samples[chs][j + k][sb] = q->tone_level[chs][sb][((j + k)/2)] * tmp[k][chs];
00937 } else {
00938 for (k = 0; k < run; k++)
00939 if ((j + k) < 128)
00940 q->sb_samples[ch][j + k][sb] = q->tone_level[ch][sb][(j + k)/2] * samples[k];
00941 }
00942
00943 j += run;
00944 }
00945 }
00946 }
00947 }
00948
00949
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);
01223 csum += 2 * get_bits(&gb, 8);
01224
01225 csum = qdm2_packet_checksum(q->compressed_data, q->checksum_size, csum);
01226
01227 if (csum != 0) {
01228 q->has_errors = 1;
01229 av_log(NULL,AV_LOG_ERROR,"bad packet checksum\n");
01230 return;
01231 }
01232 }
01233
01234 q->sub_packet_list_B[0].packet = NULL;
01235 q->sub_packet_list_D[0].packet = NULL;
01236
01237 for (i = 0; i < 6; i++)
01238 if (--q->fft_level_exp[i] < 0)
01239 q->fft_level_exp[i] = 0;
01240
01241 for (i = 0; packet_bytes > 0; i++) {
01242 int j;
01243
01244 q->sub_packet_list_A[i].next = NULL;
01245
01246 if (i > 0) {
01247 q->sub_packet_list_A[i - 1].next = &q->sub_packet_list_A[i];
01248
01249
01250 init_get_bits(&gb, header.data, header.size*8);
01251 skip_bits(&gb, next_index*8);
01252
01253 if (next_index >= header.size)
01254 break;
01255 }
01256
01257
01258 packet = &q->sub_packets[i];
01259 qdm2_decode_sub_packet_header(&gb, packet);
01260 next_index = packet->size + get_bits_count(&gb) / 8;
01261 sub_packet_size = ((packet->size > 0xff) ? 1 : 0) + packet->size + 2;
01262
01263 if (packet->type == 0)
01264 break;
01265
01266 if (sub_packet_size > packet_bytes) {
01267 if (packet->type != 10 && packet->type != 11 && packet->type != 12)
01268 break;
01269 packet->size += packet_bytes - sub_packet_size;
01270 }
01271
01272 packet_bytes -= sub_packet_size;
01273
01274
01275 q->sub_packet_list_A[i].packet = packet;
01276
01277
01278 if (packet->type == 8) {
01279 SAMPLES_NEEDED_2("packet type 8");
01280 return;
01281 } else if (packet->type >= 9 && packet->type <= 12) {
01282
01283 QDM2_LIST_ADD(q->sub_packet_list_D, sub_packets_D, packet);
01284 } else if (packet->type == 13) {
01285 for (j = 0; j < 6; j++)
01286 q->fft_level_exp[j] = get_bits(&gb, 6);
01287 } else if (packet->type == 14) {
01288 for (j = 0; j < 6; j++)
01289 q->fft_level_exp[j] = qdm2_get_vlc(&gb, &fft_level_exp_vlc, 0, 2);
01290 } else if (packet->type == 15) {
01291 SAMPLES_NEEDED_2("packet type 15")
01292 return;
01293 } else if (packet->type >= 16 && packet->type < 48 && !fft_subpackets[packet->type - 16]) {
01294
01295 QDM2_LIST_ADD(q->sub_packet_list_B, q->sub_packets_B, packet);
01296 }
01297 }
01298
01299
01300 if (q->sub_packet_list_D[0].packet != NULL) {
01301 process_synthesis_subpackets(q, q->sub_packet_list_D);
01302 q->do_synth_filter = 1;
01303 } else if (q->do_synth_filter) {
01304 process_subpacket_10(q, NULL, 0);
01305 process_subpacket_11(q, NULL, 0);
01306 process_subpacket_12(q, NULL, 0);
01307 }
01308
01309 }
01310
01311
01312 static void qdm2_fft_init_coefficient (QDM2Context *q, int sub_packet,
01313 int offset, int duration, int channel,
01314 int exp, int phase)
01315 {
01316 if (q->fft_coefs_min_index[duration] < 0)
01317 q->fft_coefs_min_index[duration] = q->fft_coefs_index;
01318
01319 q->fft_coefs[q->fft_coefs_index].sub_packet = ((sub_packet >= 16) ? (sub_packet - 16) : sub_packet);
01320 q->fft_coefs[q->fft_coefs_index].channel = channel;
01321 q->fft_coefs[q->fft_coefs_index].offset = offset;
01322 q->fft_coefs[q->fft_coefs_index].exp = exp;
01323 q->fft_coefs[q->fft_coefs_index].phase = phase;
01324 q->fft_coefs_index++;
01325 }
01326
01327
01328 static void qdm2_fft_decode_tones (QDM2Context *q, int duration, GetBitContext *gb, int b)
01329 {
01330 int channel, stereo, phase, exp;
01331 int local_int_4, local_int_8, stereo_phase, local_int_10;
01332 int local_int_14, stereo_exp, local_int_20, local_int_28;
01333 int n, offset;
01334
01335 local_int_4 = 0;
01336 local_int_28 = 0;
01337 local_int_20 = 2;
01338 local_int_8 = (4 - duration);
01339 local_int_10 = 1 << (q->group_order - duration - 1);
01340 offset = 1;
01341
01342 while (get_bits_left(gb)>0) {
01343 if (q->superblocktype_2_3) {
01344 while ((n = qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2)) < 2) {
01345 offset = 1;
01346 if (n == 0) {
01347 local_int_4 += local_int_10;
01348 local_int_28 += (1 << local_int_8);
01349 } else {
01350 local_int_4 += 8*local_int_10;
01351 local_int_28 += (8 << local_int_8);
01352 }
01353 }
01354 offset += (n - 2);
01355 } else {
01356 offset += qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2);
01357 while (offset >= (local_int_10 - 1)) {
01358 offset += (1 - (local_int_10 - 1));
01359 local_int_4 += local_int_10;
01360 local_int_28 += (1 << local_int_8);
01361 }
01362 }
01363
01364 if (local_int_4 >= q->group_size)
01365 return;
01366
01367 local_int_14 = (offset >> local_int_8);
01368 if (local_int_14 >= FF_ARRAY_ELEMS(fft_level_index_table))
01369 return;
01370
01371 if (q->nb_channels > 1) {
01372 channel = get_bits1(gb);
01373 stereo = get_bits1(gb);
01374 } else {
01375 channel = 0;
01376 stereo = 0;
01377 }
01378
01379 exp = qdm2_get_vlc(gb, (b ? &fft_level_exp_vlc : &fft_level_exp_alt_vlc), 0, 2);
01380 exp += q->fft_level_exp[fft_level_index_table[local_int_14]];
01381 exp = (exp < 0) ? 0 : exp;
01382
01383 phase = get_bits(gb, 3);
01384 stereo_exp = 0;
01385 stereo_phase = 0;
01386
01387 if (stereo) {
01388 stereo_exp = (exp - qdm2_get_vlc(gb, &fft_stereo_exp_vlc, 0, 1));
01389 stereo_phase = (phase - qdm2_get_vlc(gb, &fft_stereo_phase_vlc, 0, 1));
01390 if (stereo_phase < 0)
01391 stereo_phase += 8;
01392 }
01393
01394 if (q->frequency_range > (local_int_14 + 1)) {
01395 int sub_packet = (local_int_20 + local_int_28);
01396
01397 qdm2_fft_init_coefficient(q, sub_packet, offset, duration, channel, exp, phase);
01398 if (stereo)
01399 qdm2_fft_init_coefficient(q, sub_packet, offset, duration, (1 - channel), stereo_exp, stereo_phase);
01400 }
01401
01402 offset++;
01403 }
01404 }
01405
01406
01407 static void qdm2_decode_fft_packets (QDM2Context *q)
01408 {
01409 int i, j, min, max, value, type, unknown_flag;
01410 GetBitContext gb;
01411
01412 if (q->sub_packet_list_B[0].packet == NULL)
01413 return;
01414
01415
01416 q->fft_coefs_index = 0;
01417 for (i=0; i < 5; i++)
01418 q->fft_coefs_min_index[i] = -1;
01419
01420
01421 for (i = 0, max = 256; i < q->sub_packets_B; i++) {
01422 QDM2SubPacket *packet= NULL;
01423
01424
01425 for (j = 0, min = 0; j < q->sub_packets_B; j++) {
01426 value = q->sub_packet_list_B[j].packet->type;
01427 if (value > min && value < max) {
01428 min = value;
01429 packet = q->sub_packet_list_B[j].packet;
01430 }
01431 }
01432
01433 max = min;
01434
01435
01436 if (!packet)
01437 return;
01438
01439 if (i == 0 && (packet->type < 16 || packet->type >= 48 || fft_subpackets[packet->type - 16]))
01440 return;
01441
01442
01443 init_get_bits (&gb, packet->data, packet->size*8);
01444
01445 if (packet->type >= 32 && packet->type < 48 && !fft_subpackets[packet->type - 16])
01446 unknown_flag = 1;
01447 else
01448 unknown_flag = 0;
01449
01450 type = packet->type;
01451
01452 if ((type >= 17 && type < 24) || (type >= 33 && type < 40)) {
01453 int duration = q->sub_sampling + 5 - (type & 15);
01454
01455 if (duration >= 0 && duration < 4)
01456 qdm2_fft_decode_tones(q, duration, &gb, unknown_flag);
01457 } else if (type == 31) {
01458 for (j=0; j < 4; j++)
01459 qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
01460 } else if (type == 46) {
01461 for (j=0; j < 6; j++)
01462 q->fft_level_exp[j] = get_bits(&gb, 6);
01463 for (j=0; j < 4; j++)
01464 qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
01465 }
01466 }
01467
01468
01469 for (i = 0, j = -1; i < 5; i++)
01470 if (q->fft_coefs_min_index[i] >= 0) {
01471 if (j >= 0)
01472 q->fft_coefs_max_index[j] = q->fft_coefs_min_index[i];
01473 j = i;
01474 }
01475 if (j >= 0)
01476 q->fft_coefs_max_index[j] = q->fft_coefs_index;
01477 }
01478
01479
01480 static void qdm2_fft_generate_tone (QDM2Context *q, FFTTone *tone)
01481 {
01482 float level, f[6];
01483 int i;
01484 QDM2Complex c;
01485 const double iscale = 2.0*M_PI / 512.0;
01486
01487 tone->phase += tone->phase_shift;
01488
01489
01490 level = fft_tone_envelope_table[tone->duration][tone->time_index] * tone->level;
01491 c.im = level * sin(tone->phase*iscale);
01492 c.re = level * cos(tone->phase*iscale);
01493
01494
01495 if (tone->duration >= 3 || tone->cutoff >= 3) {
01496 tone->complex[0].im += c.im;
01497 tone->complex[0].re += c.re;
01498 tone->complex[1].im -= c.im;
01499 tone->complex[1].re -= c.re;
01500 } else {
01501 f[1] = -tone->table[4];
01502 f[0] = tone->table[3] - tone->table[0];
01503 f[2] = 1.0 - tone->table[2] - tone->table[3];
01504 f[3] = tone->table[1] + tone->table[4] - 1.0;
01505 f[4] = tone->table[0] - tone->table[1];
01506 f[5] = tone->table[2];
01507 for (i = 0; i < 2; i++) {
01508 tone->complex[fft_cutoff_index_table[tone->cutoff][i]].re += c.re * f[i];
01509 tone->complex[fft_cutoff_index_table[tone->cutoff][i]].im += c.im *((tone->cutoff <= i) ? -f[i] : f[i]);
01510 }
01511 for (i = 0; i < 4; i++) {
01512 tone->complex[i].re += c.re * f[i+2];
01513 tone->complex[i].im += c.im * f[i+2];
01514 }
01515 }
01516
01517
01518 if (++tone->time_index < ((1 << (5 - tone->duration)) - 1)) {
01519 memcpy(&q->fft_tones[q->fft_tone_end], tone, sizeof(FFTTone));
01520 q->fft_tone_end = (q->fft_tone_end + 1) % 1000;
01521 }
01522 }
01523
01524
01525 static void qdm2_fft_tone_synthesizer (QDM2Context *q, int sub_packet)
01526 {
01527 int i, j, ch;
01528 const double iscale = 0.25 * M_PI;
01529
01530 for (ch = 0; ch < q->channels; ch++) {
01531 memset(q->fft.complex[ch], 0, q->fft_size * sizeof(QDM2Complex));
01532 }
01533
01534
01535
01536 if (q->fft_coefs_min_index[4] >= 0)
01537 for (i = q->fft_coefs_min_index[4]; i < q->fft_coefs_max_index[4]; i++) {
01538 float level;
01539 QDM2Complex c;
01540
01541 if (q->fft_coefs[i].sub_packet != sub_packet)
01542 break;
01543
01544 ch = (q->channels == 1) ? 0 : q->fft_coefs[i].channel;
01545 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];
01546
01547 c.re = level * cos(q->fft_coefs[i].phase * iscale);
01548 c.im = level * sin(q->fft_coefs[i].phase * iscale);
01549 q->fft.complex[ch][q->fft_coefs[i].offset + 0].re += c.re;
01550 q->fft.complex[ch][q->fft_coefs[i].offset + 0].im += c.im;
01551 q->fft.complex[ch][q->fft_coefs[i].offset + 1].re -= c.re;
01552 q->fft.complex[ch][q->fft_coefs[i].offset + 1].im -= c.im;
01553 }
01554
01555
01556 for (i = q->fft_tone_end; i != q->fft_tone_start; ) {
01557 qdm2_fft_generate_tone(q, &q->fft_tones[q->fft_tone_start]);
01558 q->fft_tone_start = (q->fft_tone_start + 1) % 1000;
01559 }
01560
01561
01562 for (i = 0; i < 4; i++)
01563 if (q->fft_coefs_min_index[i] >= 0) {
01564 for (j = q->fft_coefs_min_index[i]; j < q->fft_coefs_max_index[i]; j++) {
01565 int offset, four_i;
01566 FFTTone tone;
01567
01568 if (q->fft_coefs[j].sub_packet != sub_packet)
01569 break;
01570
01571 four_i = (4 - i);
01572 offset = q->fft_coefs[j].offset >> four_i;
01573 ch = (q->channels == 1) ? 0 : q->fft_coefs[j].channel;
01574
01575 if (offset < q->frequency_range) {
01576 if (offset < 2)
01577 tone.cutoff = offset;
01578 else
01579 tone.cutoff = (offset >= 60) ? 3 : 2;
01580
01581 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];
01582 tone.complex = &q->fft.complex[ch][offset];
01583 tone.table = fft_tone_sample_table[i][q->fft_coefs[j].offset - (offset << four_i)];
01584 tone.phase = 64 * q->fft_coefs[j].phase - (offset << 8) - 128;
01585 tone.phase_shift = (2 * q->fft_coefs[j].offset + 1) << (7 - four_i);
01586 tone.duration = i;
01587 tone.time_index = 0;
01588
01589 qdm2_fft_generate_tone(q, &tone);
01590 }
01591 }
01592 q->fft_coefs_min_index[i] = j;
01593 }
01594 }
01595
01596
01597 static void qdm2_calculate_fft (QDM2Context *q, int channel, int sub_packet)
01598 {
01599 const float gain = (q->channels == 1 && q->nb_channels == 2) ? 0.5f : 1.0f;
01600 int i;
01601 q->fft.complex[channel][0].re *= 2.0f;
01602 q->fft.complex[channel][0].im = 0.0f;
01603 q->rdft_ctx.rdft_calc(&q->rdft_ctx, (FFTSample *)q->fft.complex[channel]);
01604
01605 for (i = 0; i < ((q->fft_frame_size + 15) & ~15); i++)
01606 q->output_buffer[q->channels * i + channel] += ((float *) q->fft.complex[channel])[i] * gain;
01607 }
01608
01609
01614 static void qdm2_synthesis_filter (QDM2Context *q, int index)
01615 {
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 float *samples_ptr = q->samples + ch;
01628
01629 for (i = 0; i < 8; i++) {
01630 ff_mpa_synth_filter_float(&q->mpadsp,
01631 q->synth_buf[ch], &(q->synth_buf_offset[ch]),
01632 ff_mpa_synth_window_float, &dither_state,
01633 samples_ptr, q->nb_channels,
01634 q->sb_samples[ch][(8 * index) + i]);
01635 samples_ptr += 32 * q->nb_channels;
01636 }
01637 }
01638
01639
01640 sub_sampling = (4 >> q->sub_sampling);
01641
01642 for (ch = 0; ch < q->channels; ch++)
01643 for (i = 0; i < q->frame_size; i++)
01644 q->output_buffer[q->channels * i + ch] += (1 << 23) * q->samples[q->nb_channels * sub_sampling * i + ch];
01645 }
01646
01647
01653 static av_cold void qdm2_init(QDM2Context *q) {
01654 static int initialized = 0;
01655
01656 if (initialized != 0)
01657 return;
01658 initialized = 1;
01659
01660 qdm2_init_vlc();
01661 ff_mpa_synth_init_float(ff_mpa_synth_window_float);
01662 softclip_table_init();
01663 rnd_table_init();
01664 init_noise_samples();
01665
01666 av_log(NULL, AV_LOG_DEBUG, "init done\n");
01667 }
01668
01669
01670 #if 0
01671 static void dump_context(QDM2Context *q)
01672 {
01673 int i;
01674 #define PRINT(a,b) av_log(NULL,AV_LOG_DEBUG," %s = %d\n", a, b);
01675 PRINT("compressed_data",q->compressed_data);
01676 PRINT("compressed_size",q->compressed_size);
01677 PRINT("frame_size",q->frame_size);
01678 PRINT("checksum_size",q->checksum_size);
01679 PRINT("channels",q->channels);
01680 PRINT("nb_channels",q->nb_channels);
01681 PRINT("fft_frame_size",q->fft_frame_size);
01682 PRINT("fft_size",q->fft_size);
01683 PRINT("sub_sampling",q->sub_sampling);
01684 PRINT("fft_order",q->fft_order);
01685 PRINT("group_order",q->group_order);
01686 PRINT("group_size",q->group_size);
01687 PRINT("sub_packet",q->sub_packet);
01688 PRINT("frequency_range",q->frequency_range);
01689 PRINT("has_errors",q->has_errors);
01690 PRINT("fft_tone_end",q->fft_tone_end);
01691 PRINT("fft_tone_start",q->fft_tone_start);
01692 PRINT("fft_coefs_index",q->fft_coefs_index);
01693 PRINT("coeff_per_sb_select",q->coeff_per_sb_select);
01694 PRINT("cm_table_select",q->cm_table_select);
01695 PRINT("noise_idx",q->noise_idx);
01696
01697 for (i = q->fft_tone_start; i < q->fft_tone_end; i++)
01698 {
01699 FFTTone *t = &q->fft_tones[i];
01700
01701 av_log(NULL,AV_LOG_DEBUG,"Tone (%d) dump:\n", i);
01702 av_log(NULL,AV_LOG_DEBUG," level = %f\n", t->level);
01703
01704 PRINT(" phase", t->phase);
01705 PRINT(" phase_shift", t->phase_shift);
01706 PRINT(" duration", t->duration);
01707 PRINT(" samples_im", t->samples_im);
01708 PRINT(" samples_re", t->samples_re);
01709 PRINT(" table", t->table);
01710 }
01711
01712 }
01713 #endif
01714
01715
01719 static av_cold int qdm2_decode_init(AVCodecContext *avctx)
01720 {
01721 QDM2Context *s = avctx->priv_data;
01722 uint8_t *extradata;
01723 int extradata_size;
01724 int tmp_val, tmp, size;
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
01760 if (!avctx->extradata || (avctx->extradata_size < 48)) {
01761 av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n");
01762 return -1;
01763 }
01764
01765 extradata = avctx->extradata;
01766 extradata_size = avctx->extradata_size;
01767
01768 while (extradata_size > 7) {
01769 if (!memcmp(extradata, "frmaQDM", 7))
01770 break;
01771 extradata++;
01772 extradata_size--;
01773 }
01774
01775 if (extradata_size < 12) {
01776 av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n",
01777 extradata_size);
01778 return -1;
01779 }
01780
01781 if (memcmp(extradata, "frmaQDM", 7)) {
01782 av_log(avctx, AV_LOG_ERROR, "invalid headers, QDM? not found\n");
01783 return -1;
01784 }
01785
01786 if (extradata[7] == 'C') {
01787
01788 av_log(avctx, AV_LOG_ERROR, "stream is QDMC version 1, which is not supported\n");
01789 return -1;
01790 }
01791
01792 extradata += 8;
01793 extradata_size -= 8;
01794
01795 size = AV_RB32(extradata);
01796
01797 if(size > extradata_size){
01798 av_log(avctx, AV_LOG_ERROR, "extradata size too small, %i < %i\n",
01799 extradata_size, size);
01800 return -1;
01801 }
01802
01803 extradata += 4;
01804 av_log(avctx, AV_LOG_DEBUG, "size: %d\n", size);
01805 if (AV_RB32(extradata) != MKBETAG('Q','D','C','A')) {
01806 av_log(avctx, AV_LOG_ERROR, "invalid extradata, expecting QDCA\n");
01807 return -1;
01808 }
01809
01810 extradata += 8;
01811
01812 avctx->channels = s->nb_channels = s->channels = AV_RB32(extradata);
01813 extradata += 4;
01814 if (s->channels > MPA_MAX_CHANNELS)
01815 return AVERROR_INVALIDDATA;
01816
01817 avctx->sample_rate = AV_RB32(extradata);
01818 extradata += 4;
01819
01820 avctx->bit_rate = AV_RB32(extradata);
01821 extradata += 4;
01822
01823 s->group_size = AV_RB32(extradata);
01824 extradata += 4;
01825
01826 s->fft_size = AV_RB32(extradata);
01827 extradata += 4;
01828
01829 s->checksum_size = AV_RB32(extradata);
01830 if (s->checksum_size >= 1U << 28) {
01831 av_log(avctx, AV_LOG_ERROR, "data block size too large (%u)\n", s->checksum_size);
01832 return AVERROR_INVALIDDATA;
01833 }
01834
01835 s->fft_order = av_log2(s->fft_size) + 1;
01836 s->fft_frame_size = 2 * s->fft_size;
01837
01838
01839 s->group_order = av_log2(s->group_size) + 1;
01840 s->frame_size = s->group_size / 16;
01841
01842 if (s->frame_size > QDM2_MAX_FRAME_SIZE)
01843 return AVERROR_INVALIDDATA;
01844
01845 s->sub_sampling = s->fft_order - 7;
01846 s->frequency_range = 255 / (1 << (2 - s->sub_sampling));
01847
01848 switch ((s->sub_sampling * 2 + s->channels - 1)) {
01849 case 0: tmp = 40; break;
01850 case 1: tmp = 48; break;
01851 case 2: tmp = 56; break;
01852 case 3: tmp = 72; break;
01853 case 4: tmp = 80; break;
01854 case 5: tmp = 100;break;
01855 default: tmp=s->sub_sampling; break;
01856 }
01857 tmp_val = 0;
01858 if ((tmp * 1000) < avctx->bit_rate) tmp_val = 1;
01859 if ((tmp * 1440) < avctx->bit_rate) tmp_val = 2;
01860 if ((tmp * 1760) < avctx->bit_rate) tmp_val = 3;
01861 if ((tmp * 2240) < avctx->bit_rate) tmp_val = 4;
01862 s->cm_table_select = tmp_val;
01863
01864 if (s->sub_sampling == 0)
01865 tmp = 7999;
01866 else
01867 tmp = ((-(s->sub_sampling -1)) & 8000) + 20000;
01868
01869
01870
01871
01872
01873 if (tmp < 8000)
01874 s->coeff_per_sb_select = 0;
01875 else if (tmp <= 16000)
01876 s->coeff_per_sb_select = 1;
01877 else
01878 s->coeff_per_sb_select = 2;
01879
01880
01881 if ((s->fft_order < 7) || (s->fft_order > 9)) {
01882 av_log(avctx, AV_LOG_ERROR, "Unknown FFT order (%d), contact the developers!\n", s->fft_order);
01883 return -1;
01884 }
01885
01886 ff_rdft_init(&s->rdft_ctx, s->fft_order, IDFT_C2R);
01887 ff_mpadsp_init(&s->mpadsp);
01888
01889 qdm2_init(s);
01890
01891 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
01892
01893 avcodec_get_frame_defaults(&s->frame);
01894 avctx->coded_frame = &s->frame;
01895
01896
01897 return 0;
01898 }
01899
01900
01901 static av_cold int qdm2_decode_close(AVCodecContext *avctx)
01902 {
01903 QDM2Context *s = avctx->priv_data;
01904
01905 ff_rdft_end(&s->rdft_ctx);
01906
01907 return 0;
01908 }
01909
01910
01911 static int qdm2_decode (QDM2Context *q, const uint8_t *in, int16_t *out)
01912 {
01913 int ch, i;
01914 const int frame_size = (q->frame_size * q->channels);
01915
01916 if((unsigned)frame_size > FF_ARRAY_ELEMS(q->output_buffer)/2)
01917 return -1;
01918
01919
01920 q->compressed_data = in;
01921 q->compressed_size = q->checksum_size;
01922
01923
01924
01925
01926 memmove(q->output_buffer, &q->output_buffer[frame_size], frame_size * sizeof(float));
01927 memset(&q->output_buffer[frame_size], 0, frame_size * sizeof(float));
01928
01929
01930 if (q->sub_packet == 0) {
01931 q->has_errors = 0;
01932 av_log(NULL,AV_LOG_DEBUG,"Superblock follows\n");
01933 qdm2_decode_super_block(q);
01934 }
01935
01936
01937 if (!q->has_errors) {
01938 if (q->sub_packet == 2)
01939 qdm2_decode_fft_packets(q);
01940
01941 qdm2_fft_tone_synthesizer(q, q->sub_packet);
01942 }
01943
01944
01945 for (ch = 0; ch < q->channels; ch++) {
01946 qdm2_calculate_fft(q, ch, q->sub_packet);
01947
01948 if (!q->has_errors && q->sub_packet_list_C[0].packet != NULL) {
01949 SAMPLES_NEEDED_2("has errors, and C list is not empty")
01950 return -1;
01951 }
01952 }
01953
01954
01955 if (!q->has_errors && q->do_synth_filter)
01956 qdm2_synthesis_filter(q, q->sub_packet);
01957
01958 q->sub_packet = (q->sub_packet + 1) % 16;
01959
01960
01961 for (i = 0; i < frame_size; i++) {
01962 int value = (int)q->output_buffer[i];
01963
01964 if (value > SOFTCLIP_THRESHOLD)
01965 value = (value > HARDCLIP_THRESHOLD) ? 32767 : softclip_table[ value - SOFTCLIP_THRESHOLD];
01966 else if (value < -SOFTCLIP_THRESHOLD)
01967 value = (value < -HARDCLIP_THRESHOLD) ? -32767 : -softclip_table[-value - SOFTCLIP_THRESHOLD];
01968
01969 out[i] = value;
01970 }
01971
01972 return 0;
01973 }
01974
01975
01976 static int qdm2_decode_frame(AVCodecContext *avctx, void *data,
01977 int *got_frame_ptr, AVPacket *avpkt)
01978 {
01979 const uint8_t *buf = avpkt->data;
01980 int buf_size = avpkt->size;
01981 QDM2Context *s = avctx->priv_data;
01982 int16_t *out;
01983 int i, ret;
01984
01985 if(!buf)
01986 return 0;
01987 if(buf_size < s->checksum_size)
01988 return -1;
01989
01990
01991 s->frame.nb_samples = 16 * s->frame_size;
01992 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
01993 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01994 return ret;
01995 }
01996 out = (int16_t *)s->frame.data[0];
01997
01998 for (i = 0; i < 16; i++) {
01999 if (qdm2_decode(s, buf, out) < 0)
02000 return -1;
02001 out += s->channels * s->frame_size;
02002 }
02003
02004 *got_frame_ptr = 1;
02005 *(AVFrame *)data = s->frame;
02006
02007 return s->checksum_size;
02008 }
02009
02010 AVCodec ff_qdm2_decoder =
02011 {
02012 .name = "qdm2",
02013 .type = AVMEDIA_TYPE_AUDIO,
02014 .id = CODEC_ID_QDM2,
02015 .priv_data_size = sizeof(QDM2Context),
02016 .init = qdm2_decode_init,
02017 .close = qdm2_decode_close,
02018 .decode = qdm2_decode_frame,
02019 .capabilities = CODEC_CAP_DR1,
02020 .long_name = NULL_IF_CONFIG_SMALL("QDesign Music Codec 2"),
02021 };