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