Mercurial > libavcodec.hg
comparison vp3.c @ 3489:9aacd9410e57 libavcodec
attempt to implement xiphs useless and stupid quantization matrix mess
author | michael |
---|---|
date | Mon, 17 Jul 2006 09:51:59 +0000 |
parents | a647bf3a7fa8 |
children | b43d45362038 |
comparison
equal
deleted
inserted
replaced
3488:a647bf3a7fa8 | 3489:9aacd9410e57 |
---|---|
260 ScanTable scantable; | 260 ScanTable scantable; |
261 | 261 |
262 /* tables */ | 262 /* tables */ |
263 uint16_t coded_dc_scale_factor[64]; | 263 uint16_t coded_dc_scale_factor[64]; |
264 uint32_t coded_ac_scale_factor[64]; | 264 uint32_t coded_ac_scale_factor[64]; |
265 uint16_t coded_intra_y_dequant[64]; | 265 uint8_t base_matrix[384][64]; |
266 uint16_t coded_intra_c_dequant[64]; | 266 uint8_t qr_count[2][3]; |
267 uint16_t coded_inter_dequant[64]; | 267 uint8_t qr_size [2][3][64]; |
268 uint16_t qr_base[2][3][64]; | |
268 | 269 |
269 /* this is a list of indices into the all_fragments array indicating | 270 /* this is a list of indices into the all_fragments array indicating |
270 * which of the fragments are coded */ | 271 * which of the fragments are coded */ |
271 int *coded_fragment_list; | 272 int *coded_fragment_list; |
272 int coded_fragment_list_index; | 273 int coded_fragment_list_index; |
641 * This function sets up the dequantization tables used for a particular | 642 * This function sets up the dequantization tables used for a particular |
642 * frame. | 643 * frame. |
643 */ | 644 */ |
644 static void init_dequantizer(Vp3DecodeContext *s) | 645 static void init_dequantizer(Vp3DecodeContext *s) |
645 { | 646 { |
646 | |
647 int ac_scale_factor = s->coded_ac_scale_factor[s->quality_index]; | 647 int ac_scale_factor = s->coded_ac_scale_factor[s->quality_index]; |
648 int dc_scale_factor = s->coded_dc_scale_factor[s->quality_index]; | 648 int dc_scale_factor = s->coded_dc_scale_factor[s->quality_index]; |
649 int i, j; | 649 int i, j, plane, inter, qri, bmi, bmj, qistart; |
650 | 650 |
651 debug_vp3(" vp3: initializing dequantization tables\n"); | 651 debug_vp3(" vp3: initializing dequantization tables\n"); |
652 | 652 |
653 /* | 653 for(inter=0; inter<2; inter++){ |
654 * Scale dequantizers: | 654 for(plane=0; plane<3; plane++){ |
655 * | 655 int sum=0; |
656 * quantizer * sf | 656 for(qri=0; qri<s->qr_count[inter][plane]; qri++){ |
657 * -------------- | 657 sum+= s->qr_size[inter][plane][qri]; |
658 * 100 | 658 if(s->quality_index <= sum) |
659 * | 659 break; |
660 * where sf = dc_scale_factor for DC quantizer | 660 } |
661 * or ac_scale_factor for AC quantizer | 661 qistart= sum - s->qr_size[inter][plane][qri]; |
662 * | 662 bmi= s->qr_base[inter][plane][qri ]; |
663 * Then, saturate the result to a lower limit of MIN_DEQUANT_VAL. | 663 bmj= s->qr_base[inter][plane][qri+1]; |
664 */ | 664 for(i=0; i<64; i++){ |
665 #define SCALER 4 | 665 int coeff= ( 2*(sum -s->quality_index)*s->base_matrix[bmi][i] |
666 | 666 - 2*(qistart-s->quality_index)*s->base_matrix[bmj][i] |
667 /* scale DC quantizers */ | 667 + s->qr_size[inter][plane][qri]) |
668 s->qmat[0][0][0] = s->coded_intra_y_dequant[0] * dc_scale_factor / 100; | 668 / (2*s->qr_size[inter][plane][qri]); |
669 if (s->qmat[0][0][0] < MIN_DEQUANT_VAL * 2) | 669 |
670 s->qmat[0][0][0] = MIN_DEQUANT_VAL * 2; | 670 int qmin= 8<<(inter + !plane); |
671 s->qmat[0][0][0] *= SCALER; | 671 int qscale= i ? ac_scale_factor : dc_scale_factor; |
672 | 672 |
673 s->qmat[0][1][0] = s->coded_intra_c_dequant[0] * dc_scale_factor / 100; | 673 s->qmat[inter][plane][i]= clip((qscale * coeff)/100 * 4, qmin, 4096); |
674 if (s->qmat[0][1][0] < MIN_DEQUANT_VAL * 2) | 674 } |
675 s->qmat[0][1][0] = MIN_DEQUANT_VAL * 2; | 675 } |
676 s->qmat[0][1][0] *= SCALER; | 676 } |
677 | |
678 s->qmat[1][0][0] = s->coded_inter_dequant[0] * dc_scale_factor / 100; | |
679 if (s->qmat[1][0][0] < MIN_DEQUANT_VAL * 4) | |
680 s->qmat[1][0][0] = MIN_DEQUANT_VAL * 4; | |
681 s->qmat[1][0][0] *= SCALER; | |
682 | |
683 /* scale AC quantizers, zigzag at the same time in preparation for | |
684 * the dequantization phase */ | |
685 for (i = 1; i < 64; i++) { | |
686 int k= s->scantable.scantable[i]; | |
687 j = s->scantable.permutated[i]; | |
688 | |
689 s->qmat[0][0][j] = s->coded_intra_y_dequant[k] * ac_scale_factor / 100; | |
690 if (s->qmat[0][0][j] < MIN_DEQUANT_VAL) | |
691 s->qmat[0][0][j] = MIN_DEQUANT_VAL; | |
692 s->qmat[0][0][j] *= SCALER; | |
693 | |
694 s->qmat[0][1][j] = s->coded_intra_c_dequant[k] * ac_scale_factor / 100; | |
695 if (s->qmat[0][1][j] < MIN_DEQUANT_VAL) | |
696 s->qmat[0][1][j] = MIN_DEQUANT_VAL; | |
697 s->qmat[0][1][j] *= SCALER; | |
698 | |
699 s->qmat[1][0][j] = s->coded_inter_dequant[k] * ac_scale_factor / 100; | |
700 if (s->qmat[1][0][j] < MIN_DEQUANT_VAL * 2) | |
701 s->qmat[1][0][j] = MIN_DEQUANT_VAL * 2; | |
702 s->qmat[1][0][j] *= SCALER; | |
703 } | |
704 | |
705 memcpy(s->qmat[0][2], s->qmat[0][1], sizeof(s->qmat[0][0])); | |
706 memcpy(s->qmat[1][1], s->qmat[1][0], sizeof(s->qmat[0][0])); | |
707 memcpy(s->qmat[1][2], s->qmat[1][0], sizeof(s->qmat[0][0])); | |
708 | 677 |
709 memset(s->qscale_table, (FFMAX(s->qmat[0][0][1], s->qmat[0][1][1])+8)/16, 512); //FIXME finetune | 678 memset(s->qscale_table, (FFMAX(s->qmat[0][0][1], s->qmat[0][1][1])+8)/16, 512); //FIXME finetune |
710 | |
711 /* print debug information as requested */ | |
712 debug_dequantizers("intra Y dequantizers:\n"); | |
713 for (i = 0; i < 8; i++) { | |
714 for (j = i * 8; j < i * 8 + 8; j++) { | |
715 debug_dequantizers(" %4d,", s->qmat[0][0][j]); | |
716 } | |
717 debug_dequantizers("\n"); | |
718 } | |
719 debug_dequantizers("\n"); | |
720 | |
721 debug_dequantizers("intra C dequantizers:\n"); | |
722 for (i = 0; i < 8; i++) { | |
723 for (j = i * 8; j < i * 8 + 8; j++) { | |
724 debug_dequantizers(" %4d,", s->qmat[0][1][j]); | |
725 } | |
726 debug_dequantizers("\n"); | |
727 } | |
728 debug_dequantizers("\n"); | |
729 | |
730 debug_dequantizers("interframe dequantizers:\n"); | |
731 for (i = 0; i < 8; i++) { | |
732 for (j = i * 8; j < i * 8 + 8; j++) { | |
733 debug_dequantizers(" %4d,", s->qmat[1][0][j]); | |
734 } | |
735 debug_dequantizers("\n"); | |
736 } | |
737 debug_dequantizers("\n"); | |
738 } | 679 } |
739 | 680 |
740 /* | 681 /* |
741 * This function initializes the loop filter boundary limits if the frame's | 682 * This function initializes the loop filter boundary limits if the frame's |
742 * quality index is different from the previous frame's. | 683 * quality index is different from the previous frame's. |
2199 * This is the ffmpeg/libavcodec API init function. | 2140 * This is the ffmpeg/libavcodec API init function. |
2200 */ | 2141 */ |
2201 static int vp3_decode_init(AVCodecContext *avctx) | 2142 static int vp3_decode_init(AVCodecContext *avctx) |
2202 { | 2143 { |
2203 Vp3DecodeContext *s = avctx->priv_data; | 2144 Vp3DecodeContext *s = avctx->priv_data; |
2204 int i; | 2145 int i, inter, plane; |
2205 int c_width; | 2146 int c_width; |
2206 int c_height; | 2147 int c_height; |
2207 int y_superblock_count; | 2148 int y_superblock_count; |
2208 int c_superblock_count; | 2149 int c_superblock_count; |
2209 | 2150 |
2282 for (i = 0; i < 64; i++) | 2223 for (i = 0; i < 64; i++) |
2283 s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i]; | 2224 s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i]; |
2284 for (i = 0; i < 64; i++) | 2225 for (i = 0; i < 64; i++) |
2285 s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i]; | 2226 s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i]; |
2286 for (i = 0; i < 64; i++) | 2227 for (i = 0; i < 64; i++) |
2287 s->coded_intra_y_dequant[i] = vp31_intra_y_dequant[i]; | 2228 s->base_matrix[0][i] = vp31_intra_y_dequant[i]; |
2288 for (i = 0; i < 64; i++) | 2229 for (i = 0; i < 64; i++) |
2289 s->coded_intra_c_dequant[i] = vp31_intra_c_dequant[i]; | 2230 s->base_matrix[1][i] = vp31_intra_c_dequant[i]; |
2290 for (i = 0; i < 64; i++) | 2231 for (i = 0; i < 64; i++) |
2291 s->coded_inter_dequant[i] = vp31_inter_dequant[i]; | 2232 s->base_matrix[2][i] = vp31_inter_dequant[i]; |
2292 for (i = 0; i < 64; i++) | 2233 for (i = 0; i < 64; i++) |
2293 s->filter_limit_values[i] = vp31_filter_limit_values[i]; | 2234 s->filter_limit_values[i] = vp31_filter_limit_values[i]; |
2235 | |
2236 for(inter=0; inter<2; inter++){ | |
2237 for(plane=0; plane<3; plane++){ | |
2238 s->qr_count[inter][plane]= 1; | |
2239 s->qr_size [inter][plane][0]= 63; | |
2240 s->qr_base [inter][plane][0]= | |
2241 s->qr_base [inter][plane][1]= 2*inter + (!!plane)*!inter; | |
2242 } | |
2243 } | |
2294 | 2244 |
2295 /* init VLC tables */ | 2245 /* init VLC tables */ |
2296 for (i = 0; i < 16; i++) { | 2246 for (i = 0; i < 16; i++) { |
2297 | 2247 |
2298 /* DC histograms */ | 2248 /* DC histograms */ |
2712 } | 2662 } |
2713 | 2663 |
2714 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb) | 2664 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb) |
2715 { | 2665 { |
2716 Vp3DecodeContext *s = avctx->priv_data; | 2666 Vp3DecodeContext *s = avctx->priv_data; |
2717 int i, n, matrices; | 2667 int i, n, matrices, inter, plane; |
2718 | 2668 |
2719 if (s->theora >= 0x030200) { | 2669 if (s->theora >= 0x030200) { |
2720 n = get_bits(gb, 3); | 2670 n = get_bits(gb, 3); |
2721 /* loop filter limit values table */ | 2671 /* loop filter limit values table */ |
2722 for (i = 0; i < 64; i++) | 2672 for (i = 0; i < 64; i++) |
2741 | 2691 |
2742 if (s->theora >= 0x030200) | 2692 if (s->theora >= 0x030200) |
2743 matrices = get_bits(gb, 9) + 1; | 2693 matrices = get_bits(gb, 9) + 1; |
2744 else | 2694 else |
2745 matrices = 3; | 2695 matrices = 3; |
2746 if (matrices != 3) { | 2696 |
2747 av_log(avctx,AV_LOG_ERROR, "unsupported matrices: %d\n", matrices); | 2697 if(matrices > 384){ |
2748 // return -1; | 2698 av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n"); |
2749 } | 2699 return -1; |
2750 /* y coeffs */ | 2700 } |
2751 for (i = 0; i < 64; i++) | 2701 |
2752 s->coded_intra_y_dequant[i] = get_bits(gb, 8); | 2702 for(n=0; n<matrices; n++){ |
2753 | |
2754 /* uv coeffs */ | |
2755 for (i = 0; i < 64; i++) | |
2756 s->coded_intra_c_dequant[i] = get_bits(gb, 8); | |
2757 | |
2758 /* inter coeffs */ | |
2759 for (i = 0; i < 64; i++) | |
2760 s->coded_inter_dequant[i] = get_bits(gb, 8); | |
2761 | |
2762 /* skip unknown matrices */ | |
2763 n = matrices - 3; | |
2764 while(n--) | |
2765 for (i = 0; i < 64; i++) | 2703 for (i = 0; i < 64; i++) |
2766 skip_bits(gb, 8); | 2704 s->base_matrix[n][i]= get_bits(gb, 8); |
2767 | 2705 } |
2768 for (i = 0; i <= 1; i++) { | 2706 |
2769 for (n = 0; n <= 2; n++) { | 2707 for (inter = 0; inter <= 1; inter++) { |
2770 int newqr; | 2708 for (plane = 0; plane <= 2; plane++) { |
2771 if (i > 0 || n > 0) | 2709 int newqr= 1; |
2710 if (inter || plane > 0) | |
2772 newqr = get_bits(gb, 1); | 2711 newqr = get_bits(gb, 1); |
2773 else | |
2774 newqr = 1; | |
2775 if (!newqr) { | 2712 if (!newqr) { |
2776 if (i > 0) | 2713 int qtj, plj; |
2777 get_bits(gb, 1); | 2714 if(inter && get_bits(gb, 1)){ |
2778 //FIXME this is simply incomplete | 2715 qtj = 0; |
2779 } | 2716 plj = plane; |
2780 else { | 2717 }else{ |
2718 qtj= (3*inter + plane - 1) / 3; | |
2719 plj= (plane + 2) % 3; | |
2720 } | |
2721 s->qr_count[inter][plane]= s->qr_count[qtj][plj]; | |
2722 memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], sizeof(s->qr_size[0][0])); | |
2723 memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], sizeof(s->qr_base[0][0])); | |
2724 } else { | |
2725 int qri= 0; | |
2781 int qi = 0; | 2726 int qi = 0; |
2782 //FIXME this is simply incomplete | 2727 |
2783 skip_bits(gb, av_log2(matrices-1)+1); | 2728 for(;;){ |
2784 while (qi < 63) { | 2729 i= get_bits(gb, av_log2(matrices-1)+1); |
2785 qi += get_bits(gb, av_log2(63-qi)+1) + 1; | 2730 if(i>= matrices){ |
2786 skip_bits(gb, av_log2(matrices-1)+1); | 2731 av_log(avctx, AV_LOG_ERROR, "invalid base matrix index\n"); |
2732 return -1; | |
2733 } | |
2734 s->qr_base[inter][plane][qri]= i; | |
2735 if(qi >= 63) | |
2736 break; | |
2737 i = get_bits(gb, av_log2(63-qi)+1) + 1; | |
2738 s->qr_size[inter][plane][qri++]= i; | |
2739 qi += i; | |
2787 } | 2740 } |
2741 | |
2788 if (qi > 63) { | 2742 if (qi > 63) { |
2789 av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi); | 2743 av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi); |
2790 return -1; | 2744 return -1; |
2791 } | 2745 } |
2746 s->qr_count[inter][plane]= qri; | |
2792 } | 2747 } |
2793 } | 2748 } |
2794 } | 2749 } |
2795 | 2750 |
2796 /* Huffman tables */ | 2751 /* Huffman tables */ |