Mercurial > libavcodec.hg
comparison vp3.c @ 3484:9a70c6d3c653 libavcodec
check how many bits are left after decoding extradata
this change is messy as whoever wrote the original code copied the GetBitContext instead of passing a pointer to it which has to be fixed for the above thing
author | michael |
---|---|
date | Sun, 16 Jul 2006 20:30:39 +0000 |
parents | 26b0ce29f5f5 |
children | f574901e49d2 |
comparison
equal
deleted
inserted
replaced
3483:26b0ce29f5f5 | 3484:9a70c6d3c653 |
---|---|
326 | 326 |
327 uint32_t filter_limit_values[64]; | 327 uint32_t filter_limit_values[64]; |
328 int bounding_values_array[256]; | 328 int bounding_values_array[256]; |
329 } Vp3DecodeContext; | 329 } Vp3DecodeContext; |
330 | 330 |
331 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext gb); | 331 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb); |
332 | 332 |
333 /************************************************************************ | 333 /************************************************************************ |
334 * VP3 specific functions | 334 * VP3 specific functions |
335 ************************************************************************/ | 335 ************************************************************************/ |
336 | 336 |
2415 skip_bits(&gb, 6*8); /* "theora" */ | 2415 skip_bits(&gb, 6*8); /* "theora" */ |
2416 | 2416 |
2417 switch(ptype) | 2417 switch(ptype) |
2418 { | 2418 { |
2419 case 1: | 2419 case 1: |
2420 theora_decode_comments(avctx, gb); | 2420 theora_decode_comments(avctx, &gb); |
2421 break; | 2421 break; |
2422 case 2: | 2422 case 2: |
2423 theora_decode_tables(avctx, gb); | 2423 theora_decode_tables(avctx, &gb); |
2424 init_dequantizer(s); | 2424 init_dequantizer(s); |
2425 break; | 2425 break; |
2426 default: | 2426 default: |
2427 av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype); | 2427 av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype); |
2428 } | 2428 } |
2643 s->huff_code_size--; | 2643 s->huff_code_size--; |
2644 } | 2644 } |
2645 return 0; | 2645 return 0; |
2646 } | 2646 } |
2647 | 2647 |
2648 static int theora_decode_header(AVCodecContext *avctx, GetBitContext gb) | 2648 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb) |
2649 { | 2649 { |
2650 Vp3DecodeContext *s = avctx->priv_data; | 2650 Vp3DecodeContext *s = avctx->priv_data; |
2651 | 2651 |
2652 s->theora = get_bits_long(&gb, 24); | 2652 s->theora = get_bits_long(gb, 24); |
2653 av_log(avctx, AV_LOG_INFO, "Theora bitstream version %X\n", s->theora); | 2653 av_log(avctx, AV_LOG_INFO, "Theora bitstream version %X\n", s->theora); |
2654 | 2654 |
2655 /* 3.2.0 aka alpha3 has the same frame orientation as original vp3 */ | 2655 /* 3.2.0 aka alpha3 has the same frame orientation as original vp3 */ |
2656 /* but previous versions have the image flipped relative to vp3 */ | 2656 /* but previous versions have the image flipped relative to vp3 */ |
2657 if (s->theora < 0x030200) | 2657 if (s->theora < 0x030200) |
2658 { | 2658 { |
2659 s->flipped_image = 1; | 2659 s->flipped_image = 1; |
2660 av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n"); | 2660 av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n"); |
2661 } | 2661 } |
2662 | 2662 |
2663 s->width = get_bits(&gb, 16) << 4; | 2663 s->width = get_bits(gb, 16) << 4; |
2664 s->height = get_bits(&gb, 16) << 4; | 2664 s->height = get_bits(gb, 16) << 4; |
2665 | 2665 |
2666 if(avcodec_check_dimensions(avctx, s->width, s->height)){ | 2666 if(avcodec_check_dimensions(avctx, s->width, s->height)){ |
2667 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height); | 2667 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height); |
2668 s->width= s->height= 0; | 2668 s->width= s->height= 0; |
2669 return -1; | 2669 return -1; |
2670 } | 2670 } |
2671 | 2671 |
2672 if (s->theora >= 0x030400) | 2672 if (s->theora >= 0x030400) |
2673 { | 2673 { |
2674 skip_bits(&gb, 32); /* total number of superblocks in a frame */ | 2674 skip_bits(gb, 32); /* total number of superblocks in a frame */ |
2675 // fixme, the next field is 36bits long | 2675 // fixme, the next field is 36bits long |
2676 skip_bits(&gb, 32); /* total number of blocks in a frame */ | 2676 skip_bits(gb, 32); /* total number of blocks in a frame */ |
2677 skip_bits(&gb, 4); /* total number of blocks in a frame */ | 2677 skip_bits(gb, 4); /* total number of blocks in a frame */ |
2678 skip_bits(&gb, 32); /* total number of macroblocks in a frame */ | 2678 skip_bits(gb, 32); /* total number of macroblocks in a frame */ |
2679 | 2679 |
2680 skip_bits(&gb, 24); /* frame width */ | 2680 skip_bits(gb, 24); /* frame width */ |
2681 skip_bits(&gb, 24); /* frame height */ | 2681 skip_bits(gb, 24); /* frame height */ |
2682 } | 2682 } |
2683 else | 2683 else |
2684 { | 2684 { |
2685 skip_bits(&gb, 24); /* frame width */ | 2685 skip_bits(gb, 24); /* frame width */ |
2686 skip_bits(&gb, 24); /* frame height */ | 2686 skip_bits(gb, 24); /* frame height */ |
2687 } | 2687 } |
2688 | 2688 |
2689 skip_bits(&gb, 8); /* offset x */ | 2689 skip_bits(gb, 8); /* offset x */ |
2690 skip_bits(&gb, 8); /* offset y */ | 2690 skip_bits(gb, 8); /* offset y */ |
2691 | 2691 |
2692 skip_bits(&gb, 32); /* fps numerator */ | 2692 skip_bits(gb, 32); /* fps numerator */ |
2693 skip_bits(&gb, 32); /* fps denumerator */ | 2693 skip_bits(gb, 32); /* fps denumerator */ |
2694 skip_bits(&gb, 24); /* aspect numerator */ | 2694 skip_bits(gb, 24); /* aspect numerator */ |
2695 skip_bits(&gb, 24); /* aspect denumerator */ | 2695 skip_bits(gb, 24); /* aspect denumerator */ |
2696 | 2696 |
2697 if (s->theora < 0x030200) | 2697 if (s->theora < 0x030200) |
2698 skip_bits(&gb, 5); /* keyframe frequency force */ | 2698 skip_bits(gb, 5); /* keyframe frequency force */ |
2699 skip_bits(&gb, 8); /* colorspace */ | 2699 skip_bits(gb, 8); /* colorspace */ |
2700 if (s->theora >= 0x030400) | 2700 if (s->theora >= 0x030400) |
2701 skip_bits(&gb, 2); /* pixel format: 420,res,422,444 */ | 2701 skip_bits(gb, 2); /* pixel format: 420,res,422,444 */ |
2702 skip_bits(&gb, 24); /* bitrate */ | 2702 skip_bits(gb, 24); /* bitrate */ |
2703 | 2703 |
2704 skip_bits(&gb, 6); /* quality hint */ | 2704 skip_bits(gb, 6); /* quality hint */ |
2705 | 2705 |
2706 if (s->theora >= 0x030200) | 2706 if (s->theora >= 0x030200) |
2707 { | 2707 { |
2708 skip_bits(&gb, 5); /* keyframe frequency force */ | 2708 skip_bits(gb, 5); /* keyframe frequency force */ |
2709 | 2709 |
2710 if (s->theora < 0x030400) | 2710 if (s->theora < 0x030400) |
2711 skip_bits(&gb, 5); /* spare bits */ | 2711 skip_bits(gb, 5); /* spare bits */ |
2712 } | 2712 } |
2713 | 2713 |
2714 // align_get_bits(&gb); | 2714 // align_get_bits(gb); |
2715 | 2715 |
2716 avctx->width = s->width; | 2716 avctx->width = s->width; |
2717 avctx->height = s->height; | 2717 avctx->height = s->height; |
2718 | 2718 |
2719 return 0; | 2719 return 0; |
2720 } | 2720 } |
2721 | 2721 |
2722 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext gb) | 2722 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb) |
2723 { | 2723 { |
2724 Vp3DecodeContext *s = avctx->priv_data; | 2724 Vp3DecodeContext *s = avctx->priv_data; |
2725 int i, n, matrices; | 2725 int i, n, matrices; |
2726 | 2726 |
2727 if (s->theora >= 0x030200) { | 2727 if (s->theora >= 0x030200) { |
2728 n = get_bits(&gb, 3); | 2728 n = get_bits(gb, 3); |
2729 /* loop filter limit values table */ | 2729 /* loop filter limit values table */ |
2730 for (i = 0; i < 64; i++) | 2730 for (i = 0; i < 64; i++) |
2731 s->filter_limit_values[i] = get_bits(&gb, n); | 2731 s->filter_limit_values[i] = get_bits(gb, n); |
2732 } | 2732 } |
2733 | 2733 |
2734 if (s->theora >= 0x030200) | 2734 if (s->theora >= 0x030200) |
2735 n = get_bits(&gb, 4) + 1; | 2735 n = get_bits(gb, 4) + 1; |
2736 else | 2736 else |
2737 n = 16; | 2737 n = 16; |
2738 /* quality threshold table */ | 2738 /* quality threshold table */ |
2739 for (i = 0; i < 64; i++) | 2739 for (i = 0; i < 64; i++) |
2740 s->coded_ac_scale_factor[i] = get_bits(&gb, n); | 2740 s->coded_ac_scale_factor[i] = get_bits(gb, n); |
2741 | 2741 |
2742 if (s->theora >= 0x030200) | 2742 if (s->theora >= 0x030200) |
2743 n = get_bits(&gb, 4) + 1; | 2743 n = get_bits(gb, 4) + 1; |
2744 else | 2744 else |
2745 n = 16; | 2745 n = 16; |
2746 /* dc scale factor table */ | 2746 /* dc scale factor table */ |
2747 for (i = 0; i < 64; i++) | 2747 for (i = 0; i < 64; i++) |
2748 s->coded_dc_scale_factor[i] = get_bits(&gb, n); | 2748 s->coded_dc_scale_factor[i] = get_bits(gb, n); |
2749 | 2749 |
2750 if (s->theora >= 0x030200) | 2750 if (s->theora >= 0x030200) |
2751 matrices = get_bits(&gb, 9) + 1; | 2751 matrices = get_bits(gb, 9) + 1; |
2752 else | 2752 else |
2753 matrices = 3; | 2753 matrices = 3; |
2754 if (matrices != 3) { | 2754 if (matrices != 3) { |
2755 av_log(avctx,AV_LOG_ERROR, "unsupported matrices: %d\n", matrices); | 2755 av_log(avctx,AV_LOG_ERROR, "unsupported matrices: %d\n", matrices); |
2756 // return -1; | 2756 // return -1; |
2757 } | 2757 } |
2758 /* y coeffs */ | 2758 /* y coeffs */ |
2759 for (i = 0; i < 64; i++) | 2759 for (i = 0; i < 64; i++) |
2760 s->coded_intra_y_dequant[i] = get_bits(&gb, 8); | 2760 s->coded_intra_y_dequant[i] = get_bits(gb, 8); |
2761 | 2761 |
2762 /* uv coeffs */ | 2762 /* uv coeffs */ |
2763 for (i = 0; i < 64; i++) | 2763 for (i = 0; i < 64; i++) |
2764 s->coded_intra_c_dequant[i] = get_bits(&gb, 8); | 2764 s->coded_intra_c_dequant[i] = get_bits(gb, 8); |
2765 | 2765 |
2766 /* inter coeffs */ | 2766 /* inter coeffs */ |
2767 for (i = 0; i < 64; i++) | 2767 for (i = 0; i < 64; i++) |
2768 s->coded_inter_dequant[i] = get_bits(&gb, 8); | 2768 s->coded_inter_dequant[i] = get_bits(gb, 8); |
2769 | 2769 |
2770 /* skip unknown matrices */ | 2770 /* skip unknown matrices */ |
2771 n = matrices - 3; | 2771 n = matrices - 3; |
2772 while(n--) | 2772 while(n--) |
2773 for (i = 0; i < 64; i++) | 2773 for (i = 0; i < 64; i++) |
2774 skip_bits(&gb, 8); | 2774 skip_bits(gb, 8); |
2775 | 2775 |
2776 for (i = 0; i <= 1; i++) { | 2776 for (i = 0; i <= 1; i++) { |
2777 for (n = 0; n <= 2; n++) { | 2777 for (n = 0; n <= 2; n++) { |
2778 int newqr; | 2778 int newqr; |
2779 if (i > 0 || n > 0) | 2779 if (i > 0 || n > 0) |
2780 newqr = get_bits(&gb, 1); | 2780 newqr = get_bits(gb, 1); |
2781 else | 2781 else |
2782 newqr = 1; | 2782 newqr = 1; |
2783 if (!newqr) { | 2783 if (!newqr) { |
2784 if (i > 0) | 2784 if (i > 0) |
2785 get_bits(&gb, 1); | 2785 get_bits(gb, 1); |
2786 } | 2786 } |
2787 else { | 2787 else { |
2788 int qi = 0; | 2788 int qi = 0; |
2789 skip_bits(&gb, av_log2(matrices-1)+1); | 2789 skip_bits(gb, av_log2(matrices-1)+1); |
2790 while (qi < 63) { | 2790 while (qi < 63) { |
2791 qi += get_bits(&gb, av_log2(63-qi)+1) + 1; | 2791 qi += get_bits(gb, av_log2(63-qi)+1) + 1; |
2792 skip_bits(&gb, av_log2(matrices-1)+1); | 2792 skip_bits(gb, av_log2(matrices-1)+1); |
2793 } | 2793 } |
2794 if (qi > 63) { | 2794 if (qi > 63) { |
2795 av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi); | 2795 av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi); |
2796 return -1; | 2796 return -1; |
2797 } | 2797 } |
2801 | 2801 |
2802 /* Huffman tables */ | 2802 /* Huffman tables */ |
2803 for (s->hti = 0; s->hti < 80; s->hti++) { | 2803 for (s->hti = 0; s->hti < 80; s->hti++) { |
2804 s->entries = 0; | 2804 s->entries = 0; |
2805 s->huff_code_size = 1; | 2805 s->huff_code_size = 1; |
2806 if (!get_bits(&gb, 1)) { | 2806 if (!get_bits(gb, 1)) { |
2807 s->hbits = 0; | 2807 s->hbits = 0; |
2808 read_huffman_tree(avctx, &gb); | 2808 read_huffman_tree(avctx, gb); |
2809 s->hbits = 1; | 2809 s->hbits = 1; |
2810 read_huffman_tree(avctx, &gb); | 2810 read_huffman_tree(avctx, gb); |
2811 } | 2811 } |
2812 } | 2812 } |
2813 | 2813 |
2814 s->theora_tables = 1; | 2814 s->theora_tables = 1; |
2815 | 2815 |
2843 debug_vp3("Theora headerpacket type: %x\n", ptype); | 2843 debug_vp3("Theora headerpacket type: %x\n", ptype); |
2844 | 2844 |
2845 if (!(ptype & 0x80)) | 2845 if (!(ptype & 0x80)) |
2846 { | 2846 { |
2847 av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n"); | 2847 av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n"); |
2848 return -1; | 2848 // return -1; |
2849 } | 2849 } |
2850 | 2850 |
2851 // FIXME: check for this aswell | 2851 // FIXME: check for this aswell |
2852 skip_bits(&gb, 6*8); /* "theora" */ | 2852 skip_bits(&gb, 6*8); /* "theora" */ |
2853 | 2853 |
2854 switch(ptype) | 2854 switch(ptype) |
2855 { | 2855 { |
2856 case 0x80: | 2856 case 0x80: |
2857 theora_decode_header(avctx, gb); | 2857 theora_decode_header(avctx, &gb); |
2858 break; | 2858 break; |
2859 case 0x81: | 2859 case 0x81: |
2860 // FIXME: is this needed? it breaks sometimes | 2860 // FIXME: is this needed? it breaks sometimes |
2861 // theora_decode_comments(avctx, gb); | 2861 // theora_decode_comments(avctx, gb); |
2862 break; | 2862 break; |
2863 case 0x82: | 2863 case 0x82: |
2864 theora_decode_tables(avctx, gb); | 2864 theora_decode_tables(avctx, &gb); |
2865 break; | 2865 break; |
2866 default: | 2866 default: |
2867 av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80); | 2867 av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80); |
2868 break; | 2868 break; |
2869 } | 2869 } |
2870 if(8*op_bytes != get_bits_count(&gb)) | |
2871 av_log(avctx, AV_LOG_ERROR, "%d bits left in packet %X\n", 8*op_bytes - get_bits_count(&gb), ptype); | |
2870 } | 2872 } |
2871 | 2873 |
2872 vp3_decode_init(avctx); | 2874 vp3_decode_init(avctx); |
2873 return 0; | 2875 return 0; |
2874 } | 2876 } |