comparison cook.c @ 4692:3318e3f6470f libavcodec

Small simplifications to subband coefficient handling and use av_random(). Patch by Ian Braithwaite ian .. braithwaite ... dk
author banan
date Tue, 20 Mar 2007 18:08:51 +0000
parents d4de8d9a2788
children 78bb9129231b
comparison
equal deleted inserted replaced
4691:0f596a49ba3d 4692:3318e3f6470f
50 #include "avcodec.h" 50 #include "avcodec.h"
51 #include "bitstream.h" 51 #include "bitstream.h"
52 #include "dsputil.h" 52 #include "dsputil.h"
53 #include "common.h" 53 #include "common.h"
54 #include "bytestream.h" 54 #include "bytestream.h"
55 #include "random.h"
55 56
56 #include "cookdata.h" 57 #include "cookdata.h"
57 58
58 /* the different Cook versions */ 59 /* the different Cook versions */
59 #define MONO 0x1000001 60 #define MONO 0x1000001
85 int total_subbands; 86 int total_subbands;
86 int num_vectors; 87 int num_vectors;
87 int bits_per_subpacket; 88 int bits_per_subpacket;
88 int cookversion; 89 int cookversion;
89 /* states */ 90 /* states */
90 int random_state; 91 AVRandomState random_state;
91 92
92 /* transform data */ 93 /* transform data */
93 MDCTContext mdct_ctx; 94 MDCTContext mdct_ctx;
94 DECLARE_ALIGNED_16(FFTSample, mdct_tmp[1024]); /* temporary storage for imlt */ 95 DECLARE_ALIGNED_16(FFTSample, mdct_tmp[1024]); /* temporary storage for imlt */
95 float* mlt_window; 96 float* mlt_window;
368 if (vlc_index>13) vlc_index = 13; //the VLC tables >13 are identical to No. 13 369 if (vlc_index>13) vlc_index = 13; //the VLC tables >13 are identical to No. 13
369 370
370 j = get_vlc2(&q->gb, q->envelope_quant_index[vlc_index-1].table, 371 j = get_vlc2(&q->gb, q->envelope_quant_index[vlc_index-1].table,
371 q->envelope_quant_index[vlc_index-1].bits,2); 372 q->envelope_quant_index[vlc_index-1].bits,2);
372 quant_index_table[i] = quant_index_table[i-1] + j - 12; //differential encoding 373 quant_index_table[i] = quant_index_table[i-1] + j - 12; //differential encoding
373 }
374 }
375
376 /**
377 * Create the quant value table.
378 *
379 * @param q pointer to the COOKContext
380 * @param quant_value_table pointer to the array
381 */
382
383 static void inline dequant_envelope(COOKContext *q, int* quant_index_table,
384 float* quant_value_table){
385
386 int i;
387 for(i=0 ; i < q->total_subbands ; i++){
388 quant_value_table[i] = q->rootpow2tab[quant_index_table[i]+63];
389 } 374 }
390 } 375 }
391 376
392 /** 377 /**
393 * Calculate the category and category_index vector. 378 * Calculate the category and category_index vector.
534 /** 519 /**
535 * The real requantization of the mltcoefs 520 * The real requantization of the mltcoefs
536 * 521 *
537 * @param q pointer to the COOKContext 522 * @param q pointer to the COOKContext
538 * @param index index 523 * @param index index
539 * @param band current subband 524 * @param quant_index quantisation index
540 * @param quant_value_table pointer to the array
541 * @param subband_coef_index array of indexes to quant_centroid_tab 525 * @param subband_coef_index array of indexes to quant_centroid_tab
542 * @param subband_coef_sign signs of coefficients 526 * @param subband_coef_sign signs of coefficients
543 * @param mlt_buffer pointer to the mlt buffer 527 * @param mlt_p pointer into the mlt buffer
544 */ 528 */
545 529
546 530 static void scalar_dequant(COOKContext *q, int index, int quant_index,
547 static void scalar_dequant(COOKContext *q, int index, int band, 531 int* subband_coef_index, int* subband_coef_sign,
548 float* quant_value_table, int* subband_coef_index, 532 float* mlt_p){
549 int* subband_coef_sign, float* mlt_buffer){
550 int i; 533 int i;
551 float f1; 534 float f1;
552 535
553 for(i=0 ; i<SUBBAND_SIZE ; i++) { 536 for(i=0 ; i<SUBBAND_SIZE ; i++) {
554 if (subband_coef_index[i]) { 537 if (subband_coef_index[i]) {
555 if (subband_coef_sign[i]) { 538 f1 = quant_centroid_tab[index][subband_coef_index[i]];
556 f1 = -quant_centroid_tab[index][subband_coef_index[i]]; 539 if (subband_coef_sign[i]) f1 = -f1;
557 } else {
558 f1 = quant_centroid_tab[index][subband_coef_index[i]];
559 }
560 } else { 540 } else {
561 /* noise coding if subband_coef_index[i] == 0 */ 541 /* noise coding if subband_coef_index[i] == 0 */
562 q->random_state = q->random_state * 214013 + 2531011; //typical RNG numbers 542 f1 = dither_tab[index];
563 f1 = randsign[(q->random_state/0x1000000)&1] * dither_tab[index]; //>>31 543 if (av_random(&q->random_state) < 0x80000000) f1 = -f1;
564 } 544 }
565 mlt_buffer[band*20+ i] = f1 * quant_value_table[band]; 545 mlt_p[i] = f1 * q->rootpow2tab[quant_index+63];
566 } 546 }
567 } 547 }
568 /** 548 /**
569 * Unpack the subband_coef_index and subband_coef_sign vectors. 549 * Unpack the subband_coef_index and subband_coef_sign vectors.
570 * 550 *
616 /** 596 /**
617 * Fill the mlt_buffer with mlt coefficients. 597 * Fill the mlt_buffer with mlt coefficients.
618 * 598 *
619 * @param q pointer to the COOKContext 599 * @param q pointer to the COOKContext
620 * @param category pointer to the category array 600 * @param category pointer to the category array
621 * @param quant_value_table pointer to the array 601 * @param quant_index_table pointer to the array
622 * @param mlt_buffer pointer to mlt coefficients 602 * @param mlt_buffer pointer to mlt coefficients
623 */ 603 */
624 604
625 605
626 static void decode_vectors(COOKContext* q, int* category, 606 static void decode_vectors(COOKContext* q, int* category,
627 float* quant_value_table, float* mlt_buffer){ 607 int *quant_index_table, float* mlt_buffer){
628 /* A zero in this table means that the subband coefficient is 608 /* A zero in this table means that the subband coefficient is
629 random noise coded. */ 609 random noise coded. */
630 int subband_coef_index[SUBBAND_SIZE]; 610 int subband_coef_index[SUBBAND_SIZE];
631 /* A zero in this table means that the subband coefficient is a 611 /* A zero in this table means that the subband coefficient is a
632 positive multiplicator. */ 612 positive multiplicator. */
644 } 624 }
645 if(index==7) { 625 if(index==7) {
646 memset(subband_coef_index, 0, sizeof(subband_coef_index)); 626 memset(subband_coef_index, 0, sizeof(subband_coef_index));
647 memset(subband_coef_sign, 0, sizeof(subband_coef_sign)); 627 memset(subband_coef_sign, 0, sizeof(subband_coef_sign));
648 } 628 }
649 scalar_dequant(q, index, band, quant_value_table, subband_coef_index, 629 scalar_dequant(q, index, quant_index_table[band],
650 subband_coef_sign, mlt_buffer); 630 subband_coef_index, subband_coef_sign,
631 &mlt_buffer[band * 20]);
651 } 632 }
652 633
653 if(q->total_subbands*SUBBAND_SIZE >= q->samples_per_channel){ 634 if(q->total_subbands*SUBBAND_SIZE >= q->samples_per_channel){
654 return; 635 return;
655 } /* FIXME: should this be removed, or moved into loop above? */ 636 } /* FIXME: should this be removed, or moved into loop above? */
665 */ 646 */
666 647
667 static void mono_decode(COOKContext *q, float* mlt_buffer) { 648 static void mono_decode(COOKContext *q, float* mlt_buffer) {
668 649
669 int category_index[128]; 650 int category_index[128];
670 float quant_value_table[102];
671 int quant_index_table[102]; 651 int quant_index_table[102];
672 int category[128]; 652 int category[128];
673 653
674 memset(&category, 0, 128*sizeof(int)); 654 memset(&category, 0, 128*sizeof(int));
675 memset(&quant_value_table, 0, 102*sizeof(int));
676 memset(&category_index, 0, 128*sizeof(int)); 655 memset(&category_index, 0, 128*sizeof(int));
677 656
678 decode_envelope(q, quant_index_table); 657 decode_envelope(q, quant_index_table);
679 q->num_vectors = get_bits(&q->gb,q->log2_numvector_size); 658 q->num_vectors = get_bits(&q->gb,q->log2_numvector_size);
680 dequant_envelope(q, quant_index_table, quant_value_table);
681 categorize(q, quant_index_table, category, category_index); 659 categorize(q, quant_index_table, category, category_index);
682 expand_category(q, category, category_index); 660 expand_category(q, category, category_index);
683 decode_vectors(q, category, quant_value_table, mlt_buffer); 661 decode_vectors(q, category, quant_index_table, mlt_buffer);
684 } 662 }
685 663
686 664
687 /** 665 /**
688 * the actual requantization of the timedomain samples 666 * the actual requantization of the timedomain samples
1033 /* Take data from the AVCodecContext (RM container). */ 1011 /* Take data from the AVCodecContext (RM container). */
1034 q->sample_rate = avctx->sample_rate; 1012 q->sample_rate = avctx->sample_rate;
1035 q->nb_channels = avctx->channels; 1013 q->nb_channels = avctx->channels;
1036 q->bit_rate = avctx->bit_rate; 1014 q->bit_rate = avctx->bit_rate;
1037 1015
1038 /* Initialize state. */ 1016 /* Initialize RNG. */
1039 q->random_state = 1; 1017 av_init_random(1, &q->random_state);
1040 1018
1041 /* Initialize extradata related variables. */ 1019 /* Initialize extradata related variables. */
1042 q->samples_per_channel = q->samples_per_frame / q->nb_channels; 1020 q->samples_per_channel = q->samples_per_frame / q->nb_channels;
1043 q->bits_per_subpacket = avctx->block_align * 8; 1021 q->bits_per_subpacket = avctx->block_align * 8;
1044 1022