Mercurial > libavcodec.hg
comparison cook.c @ 9556:e8c7a09475d5 libavcodec
Factor out COOKSubpacket, in preparation for multichannel support.
author | banan |
---|---|
date | Sat, 25 Apr 2009 10:20:48 +0000 |
parents | 0dce4fe6e6f3 |
children | c740baa765a4 |
comparison
equal
deleted
inserted
replaced
9555:8fdc1a9dc411 | 9556:e8c7a09475d5 |
---|---|
68 typedef struct { | 68 typedef struct { |
69 int *now; | 69 int *now; |
70 int *previous; | 70 int *previous; |
71 } cook_gains; | 71 } cook_gains; |
72 | 72 |
73 typedef struct { | |
74 int ch_idx; | |
75 int size; | |
76 int num_channels; | |
77 int cookversion; | |
78 int samples_per_frame; | |
79 int subbands; | |
80 int js_subband_start; | |
81 int js_vlc_bits; | |
82 int samples_per_channel; | |
83 int log2_numvector_size; | |
84 unsigned int channel_mask; | |
85 VLC ccpl; ///< channel coupling | |
86 int joint_stereo; | |
87 int bits_per_subpacket; | |
88 int bits_per_subpdiv; | |
89 int total_subbands; | |
90 int numvector_size; ///< 1 << log2_numvector_size; | |
91 | |
92 float mono_previous_buffer1[1024]; | |
93 float mono_previous_buffer2[1024]; | |
94 /** gain buffers */ | |
95 cook_gains gains1; | |
96 cook_gains gains2; | |
97 int gain_1[9]; | |
98 int gain_2[9]; | |
99 int gain_3[9]; | |
100 int gain_4[9]; | |
101 } COOKSubpacket; | |
102 | |
73 typedef struct cook { | 103 typedef struct cook { |
74 /* | 104 /* |
75 * The following 5 functions provide the lowlevel arithmetic on | 105 * The following 5 functions provide the lowlevel arithmetic on |
76 * the internal audio buffers. | 106 * the internal audio buffers. |
77 */ | 107 */ |
78 void (* scalar_dequant)(struct cook *q, int index, int quant_index, | 108 void (* scalar_dequant)(struct cook *q, int index, int quant_index, |
79 int* subband_coef_index, int* subband_coef_sign, | 109 int* subband_coef_index, int* subband_coef_sign, |
80 float* mlt_p); | 110 float* mlt_p); |
81 | 111 |
82 void (* decouple) (struct cook *q, | 112 void (* decouple) (struct cook *q, |
113 COOKSubpacket *p, | |
83 int subband, | 114 int subband, |
84 float f1, float f2, | 115 float f1, float f2, |
85 float *decode_buffer, | 116 float *decode_buffer, |
86 float *mlt_buffer1, float *mlt_buffer2); | 117 float *mlt_buffer1, float *mlt_buffer2); |
87 | 118 |
95 | 126 |
96 AVCodecContext* avctx; | 127 AVCodecContext* avctx; |
97 GetBitContext gb; | 128 GetBitContext gb; |
98 /* stream data */ | 129 /* stream data */ |
99 int nb_channels; | 130 int nb_channels; |
100 int joint_stereo; | |
101 int bit_rate; | 131 int bit_rate; |
102 int sample_rate; | 132 int sample_rate; |
133 int num_vectors; | |
103 int samples_per_channel; | 134 int samples_per_channel; |
104 int samples_per_frame; | |
105 int subbands; | |
106 int log2_numvector_size; | |
107 int numvector_size; //1 << log2_numvector_size; | |
108 int js_subband_start; | |
109 int total_subbands; | |
110 int num_vectors; | |
111 int bits_per_subpacket; | |
112 int cookversion; | |
113 /* states */ | 135 /* states */ |
114 AVLFG random_state; | 136 AVLFG random_state; |
115 | 137 |
116 /* transform data */ | 138 /* transform data */ |
117 MDCTContext mdct_ctx; | 139 MDCTContext mdct_ctx; |
118 float* mlt_window; | 140 float* mlt_window; |
119 | 141 |
120 /* gain buffers */ | |
121 cook_gains gains1; | |
122 cook_gains gains2; | |
123 int gain_1[9]; | |
124 int gain_2[9]; | |
125 int gain_3[9]; | |
126 int gain_4[9]; | |
127 | |
128 /* VLC data */ | 142 /* VLC data */ |
129 int js_vlc_bits; | |
130 VLC envelope_quant_index[13]; | 143 VLC envelope_quant_index[13]; |
131 VLC sqvh[7]; //scalar quantization | 144 VLC sqvh[7]; //scalar quantization |
132 VLC ccpl; //channel coupling | |
133 | 145 |
134 /* generatable tables and related variables */ | 146 /* generatable tables and related variables */ |
135 int gain_size_factor; | 147 int gain_size_factor; |
136 float gain_table[23]; | 148 float gain_table[23]; |
137 | 149 |
138 /* data buffers */ | 150 /* data buffers */ |
139 | 151 |
140 uint8_t* decoded_bytes_buffer; | 152 uint8_t* decoded_bytes_buffer; |
141 DECLARE_ALIGNED_16(float,mono_mdct_output[2048]); | 153 DECLARE_ALIGNED_16(float,mono_mdct_output[2048]); |
142 float mono_previous_buffer1[1024]; | |
143 float mono_previous_buffer2[1024]; | |
144 float decode_buffer_1[1024]; | 154 float decode_buffer_1[1024]; |
145 float decode_buffer_2[1024]; | 155 float decode_buffer_2[1024]; |
146 float decode_buffer_0[1060]; /* static allocation for joint decode */ | 156 float decode_buffer_0[1060]; /* static allocation for joint decode */ |
147 | 157 |
148 const float *cplscales[5]; | 158 const float *cplscales[5]; |
159 int num_subpackets; | |
160 COOKSubpacket subpacket[MAX_SUBPACKETS]; | |
149 } COOKContext; | 161 } COOKContext; |
150 | 162 |
151 static float pow2tab[127]; | 163 static float pow2tab[127]; |
152 static float rootpow2tab[127]; | 164 static float rootpow2tab[127]; |
153 | 165 |
219 result |= init_vlc (&q->sqvh[i], vhvlcsize_tab[i], vhsize_tab[i], | 231 result |= init_vlc (&q->sqvh[i], vhvlcsize_tab[i], vhsize_tab[i], |
220 cvh_huffbits[i], 1, 1, | 232 cvh_huffbits[i], 1, 1, |
221 cvh_huffcodes[i], 2, 2, 0); | 233 cvh_huffcodes[i], 2, 2, 0); |
222 } | 234 } |
223 | 235 |
224 if (q->nb_channels==2 && q->joint_stereo==1){ | 236 for(i=0;i<q->num_subpackets;i++){ |
225 result |= init_vlc (&q->ccpl, 6, (1<<q->js_vlc_bits)-1, | 237 if (q->subpacket[i].joint_stereo==1){ |
226 ccpl_huffbits[q->js_vlc_bits-2], 1, 1, | 238 result |= init_vlc (&q->subpacket[i].ccpl, 6, (1<<q->subpacket[i].js_vlc_bits)-1, |
227 ccpl_huffcodes[q->js_vlc_bits-2], 2, 2, 0); | 239 ccpl_huffbits[q->subpacket[i].js_vlc_bits-2], 1, 1, |
228 av_log(q->avctx,AV_LOG_DEBUG,"Joint-stereo VLC used.\n"); | 240 ccpl_huffcodes[q->subpacket[i].js_vlc_bits-2], 2, 2, 0); |
241 av_log(q->avctx,AV_LOG_DEBUG,"subpacket %i Joint-stereo VLC used.\n",i); | |
242 } | |
229 } | 243 } |
230 | 244 |
231 av_log(q->avctx,AV_LOG_DEBUG,"VLC tables initialized.\n"); | 245 av_log(q->avctx,AV_LOG_DEBUG,"VLC tables initialized.\n"); |
232 return result; | 246 return result; |
233 } | 247 } |
335 free_vlc(&q->envelope_quant_index[i]); | 349 free_vlc(&q->envelope_quant_index[i]); |
336 } | 350 } |
337 for (i=0 ; i<7 ; i++) { | 351 for (i=0 ; i<7 ; i++) { |
338 free_vlc(&q->sqvh[i]); | 352 free_vlc(&q->sqvh[i]); |
339 } | 353 } |
340 if(q->nb_channels==2 && q->joint_stereo==1 ){ | 354 for (i=0 ; i<q->num_subpackets ; i++) { |
341 free_vlc(&q->ccpl); | 355 free_vlc(&q->subpacket[i].ccpl); |
342 } | 356 } |
343 | 357 |
344 av_log(avctx,AV_LOG_DEBUG,"Memory deallocated.\n"); | 358 av_log(avctx,AV_LOG_DEBUG,"Memory deallocated.\n"); |
345 | 359 |
346 return 0; | 360 return 0; |
375 * | 389 * |
376 * @param q pointer to the COOKContext | 390 * @param q pointer to the COOKContext |
377 * @param quant_index_table pointer to the array | 391 * @param quant_index_table pointer to the array |
378 */ | 392 */ |
379 | 393 |
380 static void decode_envelope(COOKContext *q, int* quant_index_table) { | 394 static void decode_envelope(COOKContext *q, COOKSubpacket *p, int* quant_index_table) { |
381 int i,j, vlc_index; | 395 int i,j, vlc_index; |
382 | 396 |
383 quant_index_table[0]= get_bits(&q->gb,6) - 6; //This is used later in categorize | 397 quant_index_table[0]= get_bits(&q->gb,6) - 6; //This is used later in categorize |
384 | 398 |
385 for (i=1 ; i < q->total_subbands ; i++){ | 399 for (i=1 ; i < p->total_subbands ; i++){ |
386 vlc_index=i; | 400 vlc_index=i; |
387 if (i >= q->js_subband_start * 2) { | 401 if (i >= p->js_subband_start * 2) { |
388 vlc_index-=q->js_subband_start; | 402 vlc_index-=p->js_subband_start; |
389 } else { | 403 } else { |
390 vlc_index/=2; | 404 vlc_index/=2; |
391 if(vlc_index < 1) vlc_index = 1; | 405 if(vlc_index < 1) vlc_index = 1; |
392 } | 406 } |
393 if (vlc_index>13) vlc_index = 13; //the VLC tables >13 are identical to No. 13 | 407 if (vlc_index>13) vlc_index = 13; //the VLC tables >13 are identical to No. 13 |
405 * @param quant_index_table pointer to the array | 419 * @param quant_index_table pointer to the array |
406 * @param category pointer to the category array | 420 * @param category pointer to the category array |
407 * @param category_index pointer to the category_index array | 421 * @param category_index pointer to the category_index array |
408 */ | 422 */ |
409 | 423 |
410 static void categorize(COOKContext *q, int* quant_index_table, | 424 static void categorize(COOKContext *q, COOKSubpacket *p, int* quant_index_table, |
411 int* category, int* category_index){ | 425 int* category, int* category_index){ |
412 int exp_idx, bias, tmpbias1, tmpbias2, bits_left, num_bits, index, v, i, j; | 426 int exp_idx, bias, tmpbias1, tmpbias2, bits_left, num_bits, index, v, i, j; |
413 int exp_index2[102]; | 427 int exp_index2[102]; |
414 int exp_index1[102]; | 428 int exp_index1[102]; |
415 | 429 |
416 int tmp_categorize_array[128*2]; | 430 int tmp_categorize_array[128*2]; |
417 int tmp_categorize_array1_idx=q->numvector_size; | 431 int tmp_categorize_array1_idx=p->numvector_size; |
418 int tmp_categorize_array2_idx=q->numvector_size; | 432 int tmp_categorize_array2_idx=p->numvector_size; |
419 | 433 |
420 bits_left = q->bits_per_subpacket - get_bits_count(&q->gb); | 434 bits_left = p->bits_per_subpacket - get_bits_count(&q->gb); |
421 | 435 |
422 if(bits_left > q->samples_per_channel) { | 436 if(bits_left > q->samples_per_channel) { |
423 bits_left = q->samples_per_channel + | 437 bits_left = q->samples_per_channel + |
424 ((bits_left - q->samples_per_channel)*5)/8; | 438 ((bits_left - q->samples_per_channel)*5)/8; |
425 //av_log(q->avctx, AV_LOG_ERROR, "bits_left = %d\n",bits_left); | 439 //av_log(q->avctx, AV_LOG_ERROR, "bits_left = %d\n",bits_left); |
433 | 447 |
434 /* Estimate bias. */ | 448 /* Estimate bias. */ |
435 for (i=32 ; i>0 ; i=i/2){ | 449 for (i=32 ; i>0 ; i=i/2){ |
436 num_bits = 0; | 450 num_bits = 0; |
437 index = 0; | 451 index = 0; |
438 for (j=q->total_subbands ; j>0 ; j--){ | 452 for (j=p->total_subbands ; j>0 ; j--){ |
439 exp_idx = av_clip((i - quant_index_table[index] + bias) / 2, 0, 7); | 453 exp_idx = av_clip((i - quant_index_table[index] + bias) / 2, 0, 7); |
440 index++; | 454 index++; |
441 num_bits+=expbits_tab[exp_idx]; | 455 num_bits+=expbits_tab[exp_idx]; |
442 } | 456 } |
443 if(num_bits >= bits_left - 32){ | 457 if(num_bits >= bits_left - 32){ |
445 } | 459 } |
446 } | 460 } |
447 | 461 |
448 /* Calculate total number of bits. */ | 462 /* Calculate total number of bits. */ |
449 num_bits=0; | 463 num_bits=0; |
450 for (i=0 ; i<q->total_subbands ; i++) { | 464 for (i=0 ; i<p->total_subbands ; i++) { |
451 exp_idx = av_clip((bias - quant_index_table[i]) / 2, 0, 7); | 465 exp_idx = av_clip((bias - quant_index_table[i]) / 2, 0, 7); |
452 num_bits += expbits_tab[exp_idx]; | 466 num_bits += expbits_tab[exp_idx]; |
453 exp_index1[i] = exp_idx; | 467 exp_index1[i] = exp_idx; |
454 exp_index2[i] = exp_idx; | 468 exp_index2[i] = exp_idx; |
455 } | 469 } |
456 tmpbias1 = tmpbias2 = num_bits; | 470 tmpbias1 = tmpbias2 = num_bits; |
457 | 471 |
458 for (j = 1 ; j < q->numvector_size ; j++) { | 472 for (j = 1 ; j < p->numvector_size ; j++) { |
459 if (tmpbias1 + tmpbias2 > 2*bits_left) { /* ---> */ | 473 if (tmpbias1 + tmpbias2 > 2*bits_left) { /* ---> */ |
460 int max = -999999; | 474 int max = -999999; |
461 index=-1; | 475 index=-1; |
462 for (i=0 ; i<q->total_subbands ; i++){ | 476 for (i=0 ; i<p->total_subbands ; i++){ |
463 if (exp_index1[i] < 7) { | 477 if (exp_index1[i] < 7) { |
464 v = (-2*exp_index1[i]) - quant_index_table[i] + bias; | 478 v = (-2*exp_index1[i]) - quant_index_table[i] + bias; |
465 if ( v >= max) { | 479 if ( v >= max) { |
466 max = v; | 480 max = v; |
467 index = i; | 481 index = i; |
474 expbits_tab[exp_index1[index]+1]; | 488 expbits_tab[exp_index1[index]+1]; |
475 ++exp_index1[index]; | 489 ++exp_index1[index]; |
476 } else { /* <--- */ | 490 } else { /* <--- */ |
477 int min = 999999; | 491 int min = 999999; |
478 index=-1; | 492 index=-1; |
479 for (i=0 ; i<q->total_subbands ; i++){ | 493 for (i=0 ; i<p->total_subbands ; i++){ |
480 if(exp_index2[i] > 0){ | 494 if(exp_index2[i] > 0){ |
481 v = (-2*exp_index2[i])-quant_index_table[i]+bias; | 495 v = (-2*exp_index2[i])-quant_index_table[i]+bias; |
482 if ( v < min) { | 496 if ( v < min) { |
483 min = v; | 497 min = v; |
484 index = i; | 498 index = i; |
491 expbits_tab[exp_index2[index]-1]; | 505 expbits_tab[exp_index2[index]-1]; |
492 --exp_index2[index]; | 506 --exp_index2[index]; |
493 } | 507 } |
494 } | 508 } |
495 | 509 |
496 for(i=0 ; i<q->total_subbands ; i++) | 510 for(i=0 ; i<p->total_subbands ; i++) |
497 category[i] = exp_index2[i]; | 511 category[i] = exp_index2[i]; |
498 | 512 |
499 for(i=0 ; i<q->numvector_size-1 ; i++) | 513 for(i=0 ; i<p->numvector_size-1 ; i++) |
500 category_index[i] = tmp_categorize_array[tmp_categorize_array2_idx++]; | 514 category_index[i] = tmp_categorize_array[tmp_categorize_array2_idx++]; |
501 | 515 |
502 } | 516 } |
503 | 517 |
504 | 518 |
554 * @param category pointer to the category array | 568 * @param category pointer to the category array |
555 * @param subband_coef_index array of indexes to quant_centroid_tab | 569 * @param subband_coef_index array of indexes to quant_centroid_tab |
556 * @param subband_coef_sign signs of coefficients | 570 * @param subband_coef_sign signs of coefficients |
557 */ | 571 */ |
558 | 572 |
559 static int unpack_SQVH(COOKContext *q, int category, int* subband_coef_index, | 573 static int unpack_SQVH(COOKContext *q, COOKSubpacket *p, int category, int* subband_coef_index, |
560 int* subband_coef_sign) { | 574 int* subband_coef_sign) { |
561 int i,j; | 575 int i,j; |
562 int vlc, vd ,tmp, result; | 576 int vlc, vd ,tmp, result; |
563 | 577 |
564 vd = vd_tab[category]; | 578 vd = vd_tab[category]; |
565 result = 0; | 579 result = 0; |
566 for(i=0 ; i<vpr_tab[category] ; i++){ | 580 for(i=0 ; i<vpr_tab[category] ; i++){ |
567 vlc = get_vlc2(&q->gb, q->sqvh[category].table, q->sqvh[category].bits, 3); | 581 vlc = get_vlc2(&q->gb, q->sqvh[category].table, q->sqvh[category].bits, 3); |
568 if (q->bits_per_subpacket < get_bits_count(&q->gb)){ | 582 if (p->bits_per_subpacket < get_bits_count(&q->gb)){ |
569 vlc = 0; | 583 vlc = 0; |
570 result = 1; | 584 result = 1; |
571 } | 585 } |
572 for(j=vd-1 ; j>=0 ; j--){ | 586 for(j=vd-1 ; j>=0 ; j--){ |
573 tmp = (vlc * invradix_tab[category])/0x100000; | 587 tmp = (vlc * invradix_tab[category])/0x100000; |
574 subband_coef_index[vd*i+j] = vlc - tmp * (kmax_tab[category]+1); | 588 subband_coef_index[vd*i+j] = vlc - tmp * (kmax_tab[category]+1); |
575 vlc = tmp; | 589 vlc = tmp; |
576 } | 590 } |
577 for(j=0 ; j<vd ; j++){ | 591 for(j=0 ; j<vd ; j++){ |
578 if (subband_coef_index[i*vd + j]) { | 592 if (subband_coef_index[i*vd + j]) { |
579 if(get_bits_count(&q->gb) < q->bits_per_subpacket){ | 593 if(get_bits_count(&q->gb) < p->bits_per_subpacket){ |
580 subband_coef_sign[i*vd+j] = get_bits1(&q->gb); | 594 subband_coef_sign[i*vd+j] = get_bits1(&q->gb); |
581 } else { | 595 } else { |
582 result=1; | 596 result=1; |
583 subband_coef_sign[i*vd+j]=0; | 597 subband_coef_sign[i*vd+j]=0; |
584 } | 598 } |
599 * @param quant_index_table pointer to the array | 613 * @param quant_index_table pointer to the array |
600 * @param mlt_buffer pointer to mlt coefficients | 614 * @param mlt_buffer pointer to mlt coefficients |
601 */ | 615 */ |
602 | 616 |
603 | 617 |
604 static void decode_vectors(COOKContext* q, int* category, | 618 static void decode_vectors(COOKContext* q, COOKSubpacket* p, int* category, |
605 int *quant_index_table, float* mlt_buffer){ | 619 int *quant_index_table, float* mlt_buffer){ |
606 /* A zero in this table means that the subband coefficient is | 620 /* A zero in this table means that the subband coefficient is |
607 random noise coded. */ | 621 random noise coded. */ |
608 int subband_coef_index[SUBBAND_SIZE]; | 622 int subband_coef_index[SUBBAND_SIZE]; |
609 /* A zero in this table means that the subband coefficient is a | 623 /* A zero in this table means that the subband coefficient is a |
610 positive multiplicator. */ | 624 positive multiplicator. */ |
611 int subband_coef_sign[SUBBAND_SIZE]; | 625 int subband_coef_sign[SUBBAND_SIZE]; |
612 int band, j; | 626 int band, j; |
613 int index=0; | 627 int index=0; |
614 | 628 |
615 for(band=0 ; band<q->total_subbands ; band++){ | 629 for(band=0 ; band<p->total_subbands ; band++){ |
616 index = category[band]; | 630 index = category[band]; |
617 if(category[band] < 7){ | 631 if(category[band] < 7){ |
618 if(unpack_SQVH(q, category[band], subband_coef_index, subband_coef_sign)){ | 632 if(unpack_SQVH(q, p, category[band], subband_coef_index, subband_coef_sign)){ |
619 index=7; | 633 index=7; |
620 for(j=0 ; j<q->total_subbands ; j++) category[band+j]=7; | 634 for(j=0 ; j<p->total_subbands ; j++) category[band+j]=7; |
621 } | 635 } |
622 } | 636 } |
623 if(index>=7) { | 637 if(index>=7) { |
624 memset(subband_coef_index, 0, sizeof(subband_coef_index)); | 638 memset(subband_coef_index, 0, sizeof(subband_coef_index)); |
625 memset(subband_coef_sign, 0, sizeof(subband_coef_sign)); | 639 memset(subband_coef_sign, 0, sizeof(subband_coef_sign)); |
627 q->scalar_dequant(q, index, quant_index_table[band], | 641 q->scalar_dequant(q, index, quant_index_table[band], |
628 subband_coef_index, subband_coef_sign, | 642 subband_coef_index, subband_coef_sign, |
629 &mlt_buffer[band * SUBBAND_SIZE]); | 643 &mlt_buffer[band * SUBBAND_SIZE]); |
630 } | 644 } |
631 | 645 |
632 if(q->total_subbands*SUBBAND_SIZE >= q->samples_per_channel){ | 646 if(p->total_subbands*SUBBAND_SIZE >= q->samples_per_channel){ |
633 return; | 647 return; |
634 } /* FIXME: should this be removed, or moved into loop above? */ | 648 } /* FIXME: should this be removed, or moved into loop above? */ |
635 } | 649 } |
636 | 650 |
637 | 651 |
640 * | 654 * |
641 * @param q pointer to the COOKContext | 655 * @param q pointer to the COOKContext |
642 * @param mlt_buffer pointer to mlt coefficients | 656 * @param mlt_buffer pointer to mlt coefficients |
643 */ | 657 */ |
644 | 658 |
645 static void mono_decode(COOKContext *q, float* mlt_buffer) { | 659 static void mono_decode(COOKContext *q, COOKSubpacket *p, float* mlt_buffer) { |
646 | 660 |
647 int category_index[128]; | 661 int category_index[128]; |
648 int quant_index_table[102]; | 662 int quant_index_table[102]; |
649 int category[128]; | 663 int category[128]; |
650 | 664 |
651 memset(&category, 0, 128*sizeof(int)); | 665 memset(&category, 0, 128*sizeof(int)); |
652 memset(&category_index, 0, 128*sizeof(int)); | 666 memset(&category_index, 0, 128*sizeof(int)); |
653 | 667 |
654 decode_envelope(q, quant_index_table); | 668 decode_envelope(q, p, quant_index_table); |
655 q->num_vectors = get_bits(&q->gb,q->log2_numvector_size); | 669 q->num_vectors = get_bits(&q->gb,p->log2_numvector_size); |
656 categorize(q, quant_index_table, category, category_index); | 670 categorize(q, p, quant_index_table, category, category_index); |
657 expand_category(q, category, category_index); | 671 expand_category(q, category, category_index); |
658 decode_vectors(q, category, quant_index_table, mlt_buffer); | 672 decode_vectors(q, p, category, quant_index_table, mlt_buffer); |
659 } | 673 } |
660 | 674 |
661 | 675 |
662 /** | 676 /** |
663 * the actual requantization of the timedomain samples | 677 * the actual requantization of the timedomain samples |
758 * @param q pointer to the COOKContext | 772 * @param q pointer to the COOKContext |
759 * @param decouple_tab decoupling array | 773 * @param decouple_tab decoupling array |
760 * | 774 * |
761 */ | 775 */ |
762 | 776 |
763 static void decouple_info(COOKContext *q, int* decouple_tab){ | 777 static void decouple_info(COOKContext *q, COOKSubpacket *p, int* decouple_tab){ |
764 int length, i; | 778 int length, i; |
765 | 779 |
766 if(get_bits1(&q->gb)) { | 780 if(get_bits1(&q->gb)) { |
767 if(cplband[q->js_subband_start] > cplband[q->subbands-1]) return; | 781 if(cplband[p->js_subband_start] > cplband[p->subbands-1]) return; |
768 | 782 |
769 length = cplband[q->subbands-1] - cplband[q->js_subband_start] + 1; | 783 length = cplband[p->subbands-1] - cplband[p->js_subband_start] + 1; |
770 for (i=0 ; i<length ; i++) { | 784 for (i=0 ; i<length ; i++) { |
771 decouple_tab[cplband[q->js_subband_start] + i] = get_vlc2(&q->gb, q->ccpl.table, q->ccpl.bits, 2); | 785 decouple_tab[cplband[p->js_subband_start] + i] = get_vlc2(&q->gb, p->ccpl.table, p->ccpl.bits, 2); |
772 } | 786 } |
773 return; | 787 return; |
774 } | 788 } |
775 | 789 |
776 if(cplband[q->js_subband_start] > cplband[q->subbands-1]) return; | 790 if(cplband[p->js_subband_start] > cplband[p->subbands-1]) return; |
777 | 791 |
778 length = cplband[q->subbands-1] - cplband[q->js_subband_start] + 1; | 792 length = cplband[p->subbands-1] - cplband[p->js_subband_start] + 1; |
779 for (i=0 ; i<length ; i++) { | 793 for (i=0 ; i<length ; i++) { |
780 decouple_tab[cplband[q->js_subband_start] + i] = get_bits(&q->gb, q->js_vlc_bits); | 794 decouple_tab[cplband[p->js_subband_start] + i] = get_bits(&q->gb, p->js_vlc_bits); |
781 } | 795 } |
782 return; | 796 return; |
783 } | 797 } |
784 | 798 |
785 /* | 799 /* |
792 * @param decode_buffer input buffer | 806 * @param decode_buffer input buffer |
793 * @param mlt_buffer1 pointer to left channel mlt coefficients | 807 * @param mlt_buffer1 pointer to left channel mlt coefficients |
794 * @param mlt_buffer2 pointer to right channel mlt coefficients | 808 * @param mlt_buffer2 pointer to right channel mlt coefficients |
795 */ | 809 */ |
796 static void decouple_float (COOKContext *q, | 810 static void decouple_float (COOKContext *q, |
811 COOKSubpacket *p, | |
797 int subband, | 812 int subband, |
798 float f1, float f2, | 813 float f1, float f2, |
799 float *decode_buffer, | 814 float *decode_buffer, |
800 float *mlt_buffer1, float *mlt_buffer2) | 815 float *mlt_buffer1, float *mlt_buffer2) |
801 { | 816 { |
802 int j, tmp_idx; | 817 int j, tmp_idx; |
803 for (j=0 ; j<SUBBAND_SIZE ; j++) { | 818 for (j=0 ; j<SUBBAND_SIZE ; j++) { |
804 tmp_idx = ((q->js_subband_start + subband)*SUBBAND_SIZE)+j; | 819 tmp_idx = ((p->js_subband_start + subband)*SUBBAND_SIZE)+j; |
805 mlt_buffer1[SUBBAND_SIZE*subband + j] = f1 * decode_buffer[tmp_idx]; | 820 mlt_buffer1[SUBBAND_SIZE*subband + j] = f1 * decode_buffer[tmp_idx]; |
806 mlt_buffer2[SUBBAND_SIZE*subband + j] = f2 * decode_buffer[tmp_idx]; | 821 mlt_buffer2[SUBBAND_SIZE*subband + j] = f2 * decode_buffer[tmp_idx]; |
807 } | 822 } |
808 } | 823 } |
809 | 824 |
813 * @param q pointer to the COOKContext | 828 * @param q pointer to the COOKContext |
814 * @param mlt_buffer1 pointer to left channel mlt coefficients | 829 * @param mlt_buffer1 pointer to left channel mlt coefficients |
815 * @param mlt_buffer2 pointer to right channel mlt coefficients | 830 * @param mlt_buffer2 pointer to right channel mlt coefficients |
816 */ | 831 */ |
817 | 832 |
818 static void joint_decode(COOKContext *q, float* mlt_buffer1, | 833 static void joint_decode(COOKContext *q, COOKSubpacket *p, float* mlt_buffer1, |
819 float* mlt_buffer2) { | 834 float* mlt_buffer2) { |
820 int i,j; | 835 int i,j; |
821 int decouple_tab[SUBBAND_SIZE]; | 836 int decouple_tab[SUBBAND_SIZE]; |
822 float *decode_buffer = q->decode_buffer_0; | 837 float *decode_buffer = q->decode_buffer_0; |
823 int idx, cpl_tmp; | 838 int idx, cpl_tmp; |
828 memset(decode_buffer, 0, sizeof(decode_buffer)); | 843 memset(decode_buffer, 0, sizeof(decode_buffer)); |
829 | 844 |
830 /* Make sure the buffers are zeroed out. */ | 845 /* Make sure the buffers are zeroed out. */ |
831 memset(mlt_buffer1,0, 1024*sizeof(float)); | 846 memset(mlt_buffer1,0, 1024*sizeof(float)); |
832 memset(mlt_buffer2,0, 1024*sizeof(float)); | 847 memset(mlt_buffer2,0, 1024*sizeof(float)); |
833 decouple_info(q, decouple_tab); | 848 decouple_info(q, p, decouple_tab); |
834 mono_decode(q, decode_buffer); | 849 mono_decode(q, p, decode_buffer); |
835 | 850 |
836 /* The two channels are stored interleaved in decode_buffer. */ | 851 /* The two channels are stored interleaved in decode_buffer. */ |
837 for (i=0 ; i<q->js_subband_start ; i++) { | 852 for (i=0 ; i<p->js_subband_start ; i++) { |
838 for (j=0 ; j<SUBBAND_SIZE ; j++) { | 853 for (j=0 ; j<SUBBAND_SIZE ; j++) { |
839 mlt_buffer1[i*20+j] = decode_buffer[i*40+j]; | 854 mlt_buffer1[i*20+j] = decode_buffer[i*40+j]; |
840 mlt_buffer2[i*20+j] = decode_buffer[i*40+20+j]; | 855 mlt_buffer2[i*20+j] = decode_buffer[i*40+20+j]; |
841 } | 856 } |
842 } | 857 } |
843 | 858 |
844 /* When we reach js_subband_start (the higher frequencies) | 859 /* When we reach js_subband_start (the higher frequencies) |
845 the coefficients are stored in a coupling scheme. */ | 860 the coefficients are stored in a coupling scheme. */ |
846 idx = (1 << q->js_vlc_bits) - 1; | 861 idx = (1 << p->js_vlc_bits) - 1; |
847 for (i=q->js_subband_start ; i<q->subbands ; i++) { | 862 for (i=p->js_subband_start ; i<p->subbands ; i++) { |
848 cpl_tmp = cplband[i]; | 863 cpl_tmp = cplband[i]; |
849 idx -=decouple_tab[cpl_tmp]; | 864 idx -=decouple_tab[cpl_tmp]; |
850 cplscale = q->cplscales[q->js_vlc_bits-2]; //choose decoupler table | 865 cplscale = q->cplscales[p->js_vlc_bits-2]; //choose decoupler table |
851 f1 = cplscale[decouple_tab[cpl_tmp]]; | 866 f1 = cplscale[decouple_tab[cpl_tmp]]; |
852 f2 = cplscale[idx-1]; | 867 f2 = cplscale[idx-1]; |
853 q->decouple (q, i, f1, f2, decode_buffer, mlt_buffer1, mlt_buffer2); | 868 q->decouple (q, p, i, f1, f2, decode_buffer, mlt_buffer1, mlt_buffer2); |
854 idx = (1 << q->js_vlc_bits) - 1; | 869 idx = (1 << p->js_vlc_bits) - 1; |
855 } | 870 } |
856 } | 871 } |
857 | 872 |
858 /** | 873 /** |
859 * First part of subpacket decoding: | 874 * First part of subpacket decoding: |
863 * @param inbuffer pointer to raw stream data | 878 * @param inbuffer pointer to raw stream data |
864 * @param gain_ptr array of current/prev gain pointers | 879 * @param gain_ptr array of current/prev gain pointers |
865 */ | 880 */ |
866 | 881 |
867 static inline void | 882 static inline void |
868 decode_bytes_and_gain(COOKContext *q, const uint8_t *inbuffer, | 883 decode_bytes_and_gain(COOKContext *q, COOKSubpacket *p, const uint8_t *inbuffer, |
869 cook_gains *gains_ptr) | 884 cook_gains *gains_ptr) |
870 { | 885 { |
871 int offset; | 886 int offset; |
872 | 887 |
873 offset = decode_bytes(inbuffer, q->decoded_bytes_buffer, | 888 offset = decode_bytes(inbuffer, q->decoded_bytes_buffer, |
874 q->bits_per_subpacket/8); | 889 p->bits_per_subpacket/8); |
875 init_get_bits(&q->gb, q->decoded_bytes_buffer + offset, | 890 init_get_bits(&q->gb, q->decoded_bytes_buffer + offset, |
876 q->bits_per_subpacket); | 891 p->bits_per_subpacket); |
877 decode_gain_info(&q->gb, gains_ptr->now); | 892 decode_gain_info(&q->gb, gains_ptr->now); |
878 | 893 |
879 /* Swap current and previous gains */ | 894 /* Swap current and previous gains */ |
880 FFSWAP(int *, gains_ptr->now, gains_ptr->previous); | 895 FFSWAP(int *, gains_ptr->now, gains_ptr->previous); |
881 } | 896 } |
932 * @param sub_packet_size subpacket size | 947 * @param sub_packet_size subpacket size |
933 * @param outbuffer pointer to the outbuffer | 948 * @param outbuffer pointer to the outbuffer |
934 */ | 949 */ |
935 | 950 |
936 | 951 |
937 static int decode_subpacket(COOKContext *q, const uint8_t *inbuffer, | 952 static void decode_subpacket(COOKContext *q, COOKSubpacket* p, const uint8_t *inbuffer, int16_t *outbuffer) { |
938 int sub_packet_size, int16_t *outbuffer) { | 953 int sub_packet_size = p->size; |
939 /* packet dump */ | 954 /* packet dump */ |
940 // for (i=0 ; i<sub_packet_size ; i++) { | 955 // for (i=0 ; i<sub_packet_size ; i++) { |
941 // av_log(q->avctx, AV_LOG_ERROR, "%02x", inbuffer[i]); | 956 // av_log(q->avctx, AV_LOG_ERROR, "%02x", inbuffer[i]); |
942 // } | 957 // } |
943 // av_log(q->avctx, AV_LOG_ERROR, "\n"); | 958 // av_log(q->avctx, AV_LOG_ERROR, "\n"); |
944 | 959 memset(q->decode_buffer_1,0,sizeof(q->decode_buffer_1)); |
945 decode_bytes_and_gain(q, inbuffer, &q->gains1); | 960 decode_bytes_and_gain(q, p, inbuffer, &p->gains1); |
946 | 961 |
947 if (q->joint_stereo) { | 962 if (p->joint_stereo) { |
948 joint_decode(q, q->decode_buffer_1, q->decode_buffer_2); | 963 joint_decode(q, p, q->decode_buffer_1, q->decode_buffer_2); |
949 } else { | 964 } else { |
950 mono_decode(q, q->decode_buffer_1); | 965 mono_decode(q, p, q->decode_buffer_1); |
951 | 966 |
952 if (q->nb_channels == 2) { | 967 if (p->num_channels == 2) { |
953 decode_bytes_and_gain(q, inbuffer + sub_packet_size/2, &q->gains2); | 968 decode_bytes_and_gain(q, p, inbuffer + sub_packet_size/2, &p->gains2); |
954 mono_decode(q, q->decode_buffer_2); | 969 mono_decode(q, p, q->decode_buffer_2); |
955 } | 970 } |
956 } | 971 } |
957 | 972 |
958 mlt_compensate_output(q, q->decode_buffer_1, &q->gains1, | 973 mlt_compensate_output(q, q->decode_buffer_1, &p->gains1, |
959 q->mono_previous_buffer1, outbuffer, 0); | 974 p->mono_previous_buffer1, outbuffer, p->ch_idx); |
960 | 975 |
961 if (q->nb_channels == 2) { | 976 if (p->num_channels == 2) { |
962 if (q->joint_stereo) { | 977 if (p->joint_stereo) { |
963 mlt_compensate_output(q, q->decode_buffer_2, &q->gains1, | 978 mlt_compensate_output(q, q->decode_buffer_2, &p->gains1, |
964 q->mono_previous_buffer2, outbuffer, 1); | 979 p->mono_previous_buffer2, outbuffer, p->ch_idx + 1); |
965 } else { | 980 } else { |
966 mlt_compensate_output(q, q->decode_buffer_2, &q->gains2, | 981 mlt_compensate_output(q, q->decode_buffer_2, &p->gains2, |
967 q->mono_previous_buffer2, outbuffer, 1); | 982 p->mono_previous_buffer2, outbuffer, p->ch_idx + 1); |
968 } | 983 } |
969 } | 984 } |
970 return q->samples_per_frame * sizeof(int16_t); | 985 |
971 } | 986 } |
972 | 987 |
973 | 988 |
974 /** | 989 /** |
975 * Cook frame decoding | 990 * Cook frame decoding |
981 void *data, int *data_size, | 996 void *data, int *data_size, |
982 AVPacket *avpkt) { | 997 AVPacket *avpkt) { |
983 const uint8_t *buf = avpkt->data; | 998 const uint8_t *buf = avpkt->data; |
984 int buf_size = avpkt->size; | 999 int buf_size = avpkt->size; |
985 COOKContext *q = avctx->priv_data; | 1000 COOKContext *q = avctx->priv_data; |
1001 int i; | |
1002 int offset = 0; | |
1003 int chidx = 0; | |
986 | 1004 |
987 if (buf_size < avctx->block_align) | 1005 if (buf_size < avctx->block_align) |
988 return buf_size; | 1006 return buf_size; |
989 | 1007 |
990 *data_size = decode_subpacket(q, buf, avctx->block_align, data); | 1008 /* estimate subpacket sizes */ |
1009 q->subpacket[0].size = avctx->block_align; | |
1010 | |
1011 /* decode supbackets */ | |
1012 *data_size = 0; | |
1013 for(i=0;i<q->num_subpackets;i++){ | |
1014 q->subpacket[i].bits_per_subpacket = (q->subpacket[i].size*8)>>q->subpacket[i].bits_per_subpdiv; | |
1015 q->subpacket[i].ch_idx = chidx; | |
1016 av_log(avctx,AV_LOG_DEBUG,"subpacket[%i] size %i js %i %i block_align %i\n",i,q->subpacket[i].size,q->subpacket[i].joint_stereo,offset,avctx->block_align); | |
1017 decode_subpacket(q, &q->subpacket[i], buf + offset, (int16_t*)data); | |
1018 offset += q->subpacket[i].size; | |
1019 chidx += q->subpacket[i].num_channels; | |
1020 av_log(avctx,AV_LOG_DEBUG,"subpacket[%i] %i %i\n",i,q->subpacket[i].size * 8,get_bits_count(&q->gb)); | |
1021 } | |
1022 *data_size = sizeof(int16_t) * q->nb_channels * q->samples_per_channel; | |
991 | 1023 |
992 /* Discard the first two frames: no valid audio. */ | 1024 /* Discard the first two frames: no valid audio. */ |
993 if (avctx->frame_number < 2) *data_size = 0; | 1025 if (avctx->frame_number < 2) *data_size = 0; |
994 | 1026 |
995 return avctx->block_align; | 1027 return avctx->block_align; |
1039 | 1071 |
1040 static av_cold int cook_decode_init(AVCodecContext *avctx) | 1072 static av_cold int cook_decode_init(AVCodecContext *avctx) |
1041 { | 1073 { |
1042 COOKContext *q = avctx->priv_data; | 1074 COOKContext *q = avctx->priv_data; |
1043 const uint8_t *edata_ptr = avctx->extradata; | 1075 const uint8_t *edata_ptr = avctx->extradata; |
1076 const uint8_t *edata_ptr_end = edata_ptr + avctx->extradata_size; | |
1077 int extradata_size = avctx->extradata_size; | |
1078 int s = 0; | |
1044 q->avctx = avctx; | 1079 q->avctx = avctx; |
1045 | 1080 |
1046 /* Take care of the codec specific extradata. */ | 1081 /* Take care of the codec specific extradata. */ |
1047 if (avctx->extradata_size <= 0) { | 1082 if (extradata_size <= 0) { |
1048 av_log(avctx,AV_LOG_ERROR,"Necessary extradata missing!\n"); | 1083 av_log(avctx,AV_LOG_ERROR,"Necessary extradata missing!\n"); |
1049 return -1; | 1084 return -1; |
1050 } else { | 1085 } |
1051 /* 8 for mono, 16 for stereo, ? for multichannel | 1086 av_log(avctx,AV_LOG_DEBUG,"codecdata_length=%d\n",avctx->extradata_size); |
1052 Swap to right endianness so we don't need to care later on. */ | |
1053 av_log(avctx,AV_LOG_DEBUG,"codecdata_length=%d\n",avctx->extradata_size); | |
1054 if (avctx->extradata_size >= 8){ | |
1055 q->cookversion = bytestream_get_be32(&edata_ptr); | |
1056 q->samples_per_frame = bytestream_get_be16(&edata_ptr); | |
1057 q->subbands = bytestream_get_be16(&edata_ptr); | |
1058 } | |
1059 if (avctx->extradata_size >= 16){ | |
1060 bytestream_get_be32(&edata_ptr); //Unknown unused | |
1061 q->js_subband_start = bytestream_get_be16(&edata_ptr); | |
1062 q->js_vlc_bits = bytestream_get_be16(&edata_ptr); | |
1063 } | |
1064 } | |
1065 | 1087 |
1066 /* Take data from the AVCodecContext (RM container). */ | 1088 /* Take data from the AVCodecContext (RM container). */ |
1067 q->sample_rate = avctx->sample_rate; | 1089 q->sample_rate = avctx->sample_rate; |
1068 q->nb_channels = avctx->channels; | 1090 q->nb_channels = avctx->channels; |
1069 q->bit_rate = avctx->bit_rate; | 1091 q->bit_rate = avctx->bit_rate; |
1070 | 1092 |
1071 /* Initialize RNG. */ | 1093 /* Initialize RNG. */ |
1072 av_lfg_init(&q->random_state, ff_random_get_seed()); | 1094 av_lfg_init(&q->random_state, ff_random_get_seed()); |
1073 | 1095 |
1074 /* Initialize extradata related variables. */ | 1096 while(edata_ptr < edata_ptr_end){ |
1075 q->samples_per_channel = q->samples_per_frame / q->nb_channels; | 1097 /* 8 for mono, 16 for stereo, ? for multichannel |
1076 q->bits_per_subpacket = avctx->block_align * 8; | 1098 Swap to right endianness so we don't need to care later on. */ |
1077 | 1099 if (extradata_size >= 8){ |
1078 /* Initialize default data states. */ | 1100 q->subpacket[s].cookversion = bytestream_get_be32(&edata_ptr); |
1079 q->log2_numvector_size = 5; | 1101 q->subpacket[s].samples_per_frame = bytestream_get_be16(&edata_ptr); |
1080 q->total_subbands = q->subbands; | 1102 q->subpacket[s].subbands = bytestream_get_be16(&edata_ptr); |
1081 | 1103 extradata_size -= 8; |
1082 /* Initialize version-dependent variables */ | 1104 } |
1083 av_log(avctx,AV_LOG_DEBUG,"q->cookversion=%x\n",q->cookversion); | 1105 if (avctx->extradata_size >= 8){ |
1084 q->joint_stereo = 0; | 1106 bytestream_get_be32(&edata_ptr); //Unknown unused |
1085 switch (q->cookversion) { | 1107 q->subpacket[s].js_subband_start = bytestream_get_be16(&edata_ptr); |
1086 case MONO: | 1108 q->subpacket[s].js_vlc_bits = bytestream_get_be16(&edata_ptr); |
1087 if (q->nb_channels != 1) { | 1109 extradata_size -= 8; |
1088 av_log(avctx,AV_LOG_ERROR,"Container channels != 1, report sample!\n"); | 1110 } |
1111 | |
1112 /* Initialize extradata related variables. */ | |
1113 q->subpacket[s].samples_per_channel = q->subpacket[s].samples_per_frame / q->nb_channels; | |
1114 q->subpacket[s].bits_per_subpacket = avctx->block_align * 8; | |
1115 | |
1116 /* Initialize default data states. */ | |
1117 q->subpacket[s].log2_numvector_size = 5; | |
1118 q->subpacket[s].total_subbands = q->subpacket[s].subbands; | |
1119 q->subpacket[s].num_channels = 1; | |
1120 | |
1121 /* Initialize version-dependent variables */ | |
1122 | |
1123 av_log(avctx,AV_LOG_DEBUG,"subpacket[%i].cookversion=%x\n",s,q->subpacket[s].cookversion); | |
1124 q->subpacket[s].joint_stereo = 0; | |
1125 switch (q->subpacket[s].cookversion) { | |
1126 case MONO: | |
1127 if (q->nb_channels != 1) { | |
1128 av_log(avctx,AV_LOG_ERROR,"Container channels != 1, report sample!\n"); | |
1129 return -1; | |
1130 } | |
1131 av_log(avctx,AV_LOG_DEBUG,"MONO\n"); | |
1132 break; | |
1133 case STEREO: | |
1134 if (q->nb_channels != 1) | |
1135 q->subpacket[s].bits_per_subpdiv = 1; | |
1136 q->subpacket[s].num_channels = 2; | |
1137 av_log(avctx,AV_LOG_DEBUG,"STEREO\n"); | |
1138 break; | |
1139 case JOINT_STEREO: | |
1140 if (q->nb_channels != 2) { | |
1141 av_log(avctx,AV_LOG_ERROR,"Container channels != 2, report sample!\n"); | |
1142 return -1; | |
1143 } | |
1144 av_log(avctx,AV_LOG_DEBUG,"JOINT_STEREO\n"); | |
1145 if (avctx->extradata_size >= 16){ | |
1146 q->subpacket[s].total_subbands = q->subpacket[s].subbands + q->subpacket[s].js_subband_start; | |
1147 q->subpacket[s].joint_stereo = 1; | |
1148 q->subpacket[s].num_channels = 2; | |
1149 } | |
1150 if (q->subpacket[s].samples_per_channel > 256) { | |
1151 q->subpacket[s].log2_numvector_size = 6; | |
1152 } | |
1153 if (q->subpacket[s].samples_per_channel > 512) { | |
1154 q->subpacket[s].log2_numvector_size = 7; | |
1155 } | |
1156 break; | |
1157 case MC_COOK: | |
1158 av_log(avctx,AV_LOG_ERROR,"MC_COOK not supported!\n"); | |
1089 return -1; | 1159 return -1; |
1090 } | 1160 break; |
1091 av_log(avctx,AV_LOG_DEBUG,"MONO\n"); | 1161 default: |
1092 break; | 1162 av_log(avctx,AV_LOG_ERROR,"Unknown Cook version, report sample!\n"); |
1093 case STEREO: | |
1094 if (q->nb_channels != 1) { | |
1095 q->bits_per_subpacket = q->bits_per_subpacket/2; | |
1096 } | |
1097 av_log(avctx,AV_LOG_DEBUG,"STEREO\n"); | |
1098 break; | |
1099 case JOINT_STEREO: | |
1100 if (q->nb_channels != 2) { | |
1101 av_log(avctx,AV_LOG_ERROR,"Container channels != 2, report sample!\n"); | |
1102 return -1; | 1163 return -1; |
1103 } | 1164 break; |
1104 av_log(avctx,AV_LOG_DEBUG,"JOINT_STEREO\n"); | 1165 } |
1105 if (avctx->extradata_size >= 16){ | 1166 |
1106 q->total_subbands = q->subbands + q->js_subband_start; | 1167 if(s > 1 && q->subpacket[s].samples_per_channel != q->samples_per_channel) { |
1107 q->joint_stereo = 1; | 1168 av_log(avctx,AV_LOG_ERROR,"different number of samples per channel!\n"); |
1108 } | |
1109 if (q->samples_per_channel > 256) { | |
1110 q->log2_numvector_size = 6; | |
1111 } | |
1112 if (q->samples_per_channel > 512) { | |
1113 q->log2_numvector_size = 7; | |
1114 } | |
1115 break; | |
1116 case MC_COOK: | |
1117 av_log(avctx,AV_LOG_ERROR,"MC_COOK not supported!\n"); | |
1118 return -1; | 1169 return -1; |
1119 break; | 1170 } else |
1120 default: | 1171 q->samples_per_channel = q->subpacket[0].samples_per_channel; |
1121 av_log(avctx,AV_LOG_ERROR,"Unknown Cook version, report sample!\n"); | 1172 |
1173 | |
1174 /* Initialize variable relations */ | |
1175 q->subpacket[s].numvector_size = (1 << q->subpacket[s].log2_numvector_size); | |
1176 | |
1177 /* Try to catch some obviously faulty streams, othervise it might be exploitable */ | |
1178 if (q->subpacket[s].total_subbands > 53) { | |
1179 av_log(avctx,AV_LOG_ERROR,"total_subbands > 53, report sample!\n"); | |
1122 return -1; | 1180 return -1; |
1123 break; | 1181 } |
1124 } | 1182 |
1125 | 1183 if ((q->subpacket[s].js_vlc_bits > 6) || (q->subpacket[s].js_vlc_bits < 0)) { |
1126 /* Initialize variable relations */ | 1184 av_log(avctx,AV_LOG_ERROR,"js_vlc_bits = %d, only >= 0 and <= 6 allowed!\n",q->subpacket[s].js_vlc_bits); |
1127 q->numvector_size = (1 << q->log2_numvector_size); | 1185 return -1; |
1128 | 1186 } |
1187 | |
1188 if (q->subpacket[s].subbands > 50) { | |
1189 av_log(avctx,AV_LOG_ERROR,"subbands > 50, report sample!\n"); | |
1190 return -1; | |
1191 } | |
1192 q->subpacket[s].gains1.now = q->subpacket[s].gain_1; | |
1193 q->subpacket[s].gains1.previous = q->subpacket[s].gain_2; | |
1194 q->subpacket[s].gains2.now = q->subpacket[s].gain_3; | |
1195 q->subpacket[s].gains2.previous = q->subpacket[s].gain_4; | |
1196 | |
1197 q->num_subpackets++; | |
1198 s++; | |
1199 } | |
1129 /* Generate tables */ | 1200 /* Generate tables */ |
1130 init_pow2table(); | 1201 init_pow2table(); |
1131 init_gain_table(q); | 1202 init_gain_table(q); |
1132 init_cplscales_table(q); | 1203 init_cplscales_table(q); |
1133 | 1204 |
1139 return -1; | 1210 return -1; |
1140 | 1211 |
1141 /* Pad the databuffer with: | 1212 /* Pad the databuffer with: |
1142 DECODE_BYTES_PAD1 or DECODE_BYTES_PAD2 for decode_bytes(), | 1213 DECODE_BYTES_PAD1 or DECODE_BYTES_PAD2 for decode_bytes(), |
1143 FF_INPUT_BUFFER_PADDING_SIZE, for the bitstreamreader. */ | 1214 FF_INPUT_BUFFER_PADDING_SIZE, for the bitstreamreader. */ |
1144 if (q->nb_channels==2 && q->joint_stereo==0) { | |
1145 q->decoded_bytes_buffer = | |
1146 av_mallocz(avctx->block_align/2 | |
1147 + DECODE_BYTES_PAD2(avctx->block_align/2) | |
1148 + FF_INPUT_BUFFER_PADDING_SIZE); | |
1149 } else { | |
1150 q->decoded_bytes_buffer = | 1215 q->decoded_bytes_buffer = |
1151 av_mallocz(avctx->block_align | 1216 av_mallocz(avctx->block_align |
1152 + DECODE_BYTES_PAD1(avctx->block_align) | 1217 + DECODE_BYTES_PAD1(avctx->block_align) |
1153 + FF_INPUT_BUFFER_PADDING_SIZE); | 1218 + FF_INPUT_BUFFER_PADDING_SIZE); |
1154 } | |
1155 if (q->decoded_bytes_buffer == NULL) | 1219 if (q->decoded_bytes_buffer == NULL) |
1156 return -1; | 1220 return -1; |
1157 | |
1158 q->gains1.now = q->gain_1; | |
1159 q->gains1.previous = q->gain_2; | |
1160 q->gains2.now = q->gain_3; | |
1161 q->gains2.previous = q->gain_4; | |
1162 | 1221 |
1163 /* Initialize transform. */ | 1222 /* Initialize transform. */ |
1164 if ( init_cook_mlt(q) != 0 ) | 1223 if ( init_cook_mlt(q) != 0 ) |
1165 return -1; | 1224 return -1; |
1166 | 1225 |
1172 q->interpolate = interpolate_float; | 1231 q->interpolate = interpolate_float; |
1173 q->saturate_output = saturate_output_float; | 1232 q->saturate_output = saturate_output_float; |
1174 } | 1233 } |
1175 | 1234 |
1176 /* Try to catch some obviously faulty streams, othervise it might be exploitable */ | 1235 /* Try to catch some obviously faulty streams, othervise it might be exploitable */ |
1177 if (q->total_subbands > 53) { | |
1178 av_log(avctx,AV_LOG_ERROR,"total_subbands > 53, report sample!\n"); | |
1179 return -1; | |
1180 } | |
1181 if (q->subbands > 50) { | |
1182 av_log(avctx,AV_LOG_ERROR,"subbands > 50, report sample!\n"); | |
1183 return -1; | |
1184 } | |
1185 if ((q->samples_per_channel == 256) || (q->samples_per_channel == 512) || (q->samples_per_channel == 1024)) { | 1236 if ((q->samples_per_channel == 256) || (q->samples_per_channel == 512) || (q->samples_per_channel == 1024)) { |
1186 } else { | 1237 } else { |
1187 av_log(avctx,AV_LOG_ERROR,"unknown amount of samples_per_channel = %d, report sample!\n",q->samples_per_channel); | 1238 av_log(avctx,AV_LOG_ERROR,"unknown amount of samples_per_channel = %d, report sample!\n",q->samples_per_channel); |
1188 return -1; | |
1189 } | |
1190 if ((q->js_vlc_bits > 6) || (q->js_vlc_bits < 0)) { | |
1191 av_log(avctx,AV_LOG_ERROR,"q->js_vlc_bits = %d, only >= 0 and <= 6 allowed!\n",q->js_vlc_bits); | |
1192 return -1; | 1239 return -1; |
1193 } | 1240 } |
1194 | 1241 |
1195 avctx->sample_fmt = SAMPLE_FMT_S16; | 1242 avctx->sample_fmt = SAMPLE_FMT_S16; |
1196 avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO; | 1243 avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO; |