Mercurial > libavcodec.hg
comparison vp3.c @ 1516:0f0e9dfa6723 libavcodec
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
author | alex |
---|---|
date | Sat, 11 Oct 2003 17:44:21 +0000 |
parents | ca24abf4af22 |
children | c66cdace3bf6 |
comparison
equal
deleted
inserted
replaced
1515:e94e299aee40 | 1516:0f0e9dfa6723 |
---|---|
18 * | 18 * |
19 * VP3 Video Decoder by Mike Melanson (melanson@pcisys.net) | 19 * VP3 Video Decoder by Mike Melanson (melanson@pcisys.net) |
20 * For more information about the VP3 coding process, visit: | 20 * For more information about the VP3 coding process, visit: |
21 * http://www.pcisys.net/~melanson/codecs/ | 21 * http://www.pcisys.net/~melanson/codecs/ |
22 * | 22 * |
23 * Theora decoder by Alex Beregszaszi | |
24 * | |
23 */ | 25 */ |
24 | 26 |
25 /** | 27 /** |
26 * @file vp3.c | 28 * @file vp3.c |
27 * On2 VP3 Video Decoder | 29 * On2 VP3 Video Decoder |
211 | 213 |
212 #define MIN_DEQUANT_VAL 2 | 214 #define MIN_DEQUANT_VAL 2 |
213 | 215 |
214 typedef struct Vp3DecodeContext { | 216 typedef struct Vp3DecodeContext { |
215 AVCodecContext *avctx; | 217 AVCodecContext *avctx; |
218 int theora, theora_tables; | |
216 int width, height; | 219 int width, height; |
217 AVFrame golden_frame; | 220 AVFrame golden_frame; |
218 AVFrame last_frame; | 221 AVFrame last_frame; |
219 AVFrame current_frame; | 222 AVFrame current_frame; |
220 int keyframe; | 223 int keyframe; |
243 int fragment_height; | 246 int fragment_height; |
244 | 247 |
245 Vp3Fragment *all_fragments; | 248 Vp3Fragment *all_fragments; |
246 int u_fragment_start; | 249 int u_fragment_start; |
247 int v_fragment_start; | 250 int v_fragment_start; |
251 | |
252 /* tables */ | |
253 uint16_t coded_dc_scale_factor[64]; | |
254 uint32_t coded_quality_threshold[64]; | |
255 uint16_t coded_intra_y_dequant[64]; | |
256 uint16_t coded_intra_c_dequant[64]; | |
257 uint16_t coded_inter_dequant[64]; | |
248 | 258 |
249 /* this is a list of indices into the all_fragments array indicating | 259 /* this is a list of indices into the all_fragments array indicating |
250 * which of the fragments are coded */ | 260 * which of the fragments are coded */ |
251 int *coded_fragment_list; | 261 int *coded_fragment_list; |
252 int coded_fragment_list_index; | 262 int coded_fragment_list_index; |
1128 * frame. | 1138 * frame. |
1129 */ | 1139 */ |
1130 static void init_dequantizer(Vp3DecodeContext *s) | 1140 static void init_dequantizer(Vp3DecodeContext *s) |
1131 { | 1141 { |
1132 | 1142 |
1133 int quality_scale = vp31_quality_threshold[s->quality_index]; | 1143 int quality_scale = s->coded_quality_threshold[s->quality_index]; |
1134 int dc_scale_factor = vp31_dc_scale_factor[s->quality_index]; | 1144 int dc_scale_factor = s->coded_dc_scale_factor[s->quality_index]; |
1135 int i, j; | 1145 int i, j; |
1136 | 1146 |
1137 debug_vp3(" vp3: initializing dequantization tables\n"); | 1147 debug_vp3(" vp3: initializing dequantization tables\n"); |
1138 | 1148 |
1139 /* | 1149 /* |
1149 * Then, saturate the result to a lower limit of MIN_DEQUANT_VAL. | 1159 * Then, saturate the result to a lower limit of MIN_DEQUANT_VAL. |
1150 */ | 1160 */ |
1151 #define SCALER 4 | 1161 #define SCALER 4 |
1152 | 1162 |
1153 /* scale DC quantizers */ | 1163 /* scale DC quantizers */ |
1154 s->intra_y_dequant[0] = vp31_intra_y_dequant[0] * dc_scale_factor / 100; | 1164 s->intra_y_dequant[0] = s->coded_intra_y_dequant[0] * dc_scale_factor / 100; |
1155 if (s->intra_y_dequant[0] < MIN_DEQUANT_VAL * 2) | 1165 if (s->intra_y_dequant[0] < MIN_DEQUANT_VAL * 2) |
1156 s->intra_y_dequant[0] = MIN_DEQUANT_VAL * 2; | 1166 s->intra_y_dequant[0] = MIN_DEQUANT_VAL * 2; |
1157 s->intra_y_dequant[0] *= SCALER; | 1167 s->intra_y_dequant[0] *= SCALER; |
1158 | 1168 |
1159 s->intra_c_dequant[0] = vp31_intra_c_dequant[0] * dc_scale_factor / 100; | 1169 s->intra_c_dequant[0] = s->coded_intra_c_dequant[0] * dc_scale_factor / 100; |
1160 if (s->intra_c_dequant[0] < MIN_DEQUANT_VAL * 2) | 1170 if (s->intra_c_dequant[0] < MIN_DEQUANT_VAL * 2) |
1161 s->intra_c_dequant[0] = MIN_DEQUANT_VAL * 2; | 1171 s->intra_c_dequant[0] = MIN_DEQUANT_VAL * 2; |
1162 s->intra_c_dequant[0] *= SCALER; | 1172 s->intra_c_dequant[0] *= SCALER; |
1163 | 1173 |
1164 s->inter_dequant[0] = vp31_inter_dequant[0] * dc_scale_factor / 100; | 1174 s->inter_dequant[0] = s->coded_inter_dequant[0] * dc_scale_factor / 100; |
1165 if (s->inter_dequant[0] < MIN_DEQUANT_VAL * 4) | 1175 if (s->inter_dequant[0] < MIN_DEQUANT_VAL * 4) |
1166 s->inter_dequant[0] = MIN_DEQUANT_VAL * 4; | 1176 s->inter_dequant[0] = MIN_DEQUANT_VAL * 4; |
1167 s->inter_dequant[0] *= SCALER; | 1177 s->inter_dequant[0] *= SCALER; |
1168 | 1178 |
1169 /* scale AC quantizers, zigzag at the same time in preparation for | 1179 /* scale AC quantizers, zigzag at the same time in preparation for |
1170 * the dequantization phase */ | 1180 * the dequantization phase */ |
1171 for (i = 1; i < 64; i++) { | 1181 for (i = 1; i < 64; i++) { |
1172 | 1182 |
1173 j = zigzag_index[i]; | 1183 j = zigzag_index[i]; |
1174 | 1184 |
1175 s->intra_y_dequant[j] = vp31_intra_y_dequant[i] * quality_scale / 100; | 1185 s->intra_y_dequant[j] = s->coded_intra_y_dequant[i] * quality_scale / 100; |
1176 if (s->intra_y_dequant[j] < MIN_DEQUANT_VAL) | 1186 if (s->intra_y_dequant[j] < MIN_DEQUANT_VAL) |
1177 s->intra_y_dequant[j] = MIN_DEQUANT_VAL; | 1187 s->intra_y_dequant[j] = MIN_DEQUANT_VAL; |
1178 s->intra_y_dequant[j] *= SCALER; | 1188 s->intra_y_dequant[j] *= SCALER; |
1179 | 1189 |
1180 s->intra_c_dequant[j] = vp31_intra_c_dequant[i] * quality_scale / 100; | 1190 s->intra_c_dequant[j] = s->coded_intra_c_dequant[i] * quality_scale / 100; |
1181 if (s->intra_c_dequant[j] < MIN_DEQUANT_VAL) | 1191 if (s->intra_c_dequant[j] < MIN_DEQUANT_VAL) |
1182 s->intra_c_dequant[j] = MIN_DEQUANT_VAL; | 1192 s->intra_c_dequant[j] = MIN_DEQUANT_VAL; |
1183 s->intra_c_dequant[j] *= SCALER; | 1193 s->intra_c_dequant[j] *= SCALER; |
1184 | 1194 |
1185 s->inter_dequant[j] = vp31_inter_dequant[i] * quality_scale / 100; | 1195 s->inter_dequant[j] = s->coded_inter_dequant[i] * quality_scale / 100; |
1186 if (s->inter_dequant[j] < MIN_DEQUANT_VAL * 2) | 1196 if (s->inter_dequant[j] < MIN_DEQUANT_VAL * 2) |
1187 s->inter_dequant[j] = MIN_DEQUANT_VAL * 2; | 1197 s->inter_dequant[j] = MIN_DEQUANT_VAL * 2; |
1188 s->inter_dequant[j] *= SCALER; | 1198 s->inter_dequant[j] *= SCALER; |
1189 } | 1199 } |
1190 | 1200 |
2612 | 2622 |
2613 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment)); | 2623 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment)); |
2614 s->coded_fragment_list = av_malloc(s->fragment_count * sizeof(int)); | 2624 s->coded_fragment_list = av_malloc(s->fragment_count * sizeof(int)); |
2615 s->pixel_addresses_inited = 0; | 2625 s->pixel_addresses_inited = 0; |
2616 | 2626 |
2627 if (!s->theora_tables) | |
2628 { | |
2629 for (i = 0; i < 64; i++) | |
2630 s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i]; | |
2631 for (i = 0; i < 64; i++) | |
2632 s->coded_quality_threshold[i] = vp31_quality_threshold[i]; | |
2633 for (i = 0; i < 64; i++) | |
2634 s->coded_intra_y_dequant[i] = vp31_intra_y_dequant[i]; | |
2635 for (i = 0; i < 64; i++) | |
2636 s->coded_intra_c_dequant[i] = vp31_intra_c_dequant[i]; | |
2637 for (i = 0; i < 64; i++) | |
2638 s->coded_inter_dequant[i] = vp31_inter_dequant[i]; | |
2639 } | |
2640 | |
2617 /* init VLC tables */ | 2641 /* init VLC tables */ |
2618 for (i = 0; i < 16; i++) { | 2642 for (i = 0; i < 16; i++) { |
2619 | 2643 |
2620 /* DC histograms */ | 2644 /* DC histograms */ |
2621 init_vlc(&s->dc_vlc[i], 5, 32, | 2645 init_vlc(&s->dc_vlc[i], 5, 32, |
2675 static int counter = 0; | 2699 static int counter = 0; |
2676 | 2700 |
2677 *data_size = 0; | 2701 *data_size = 0; |
2678 | 2702 |
2679 init_get_bits(&gb, buf, buf_size * 8); | 2703 init_get_bits(&gb, buf, buf_size * 8); |
2680 | 2704 |
2681 s->keyframe = get_bits(&gb, 1); | 2705 if (s->theora && get_bits1(&gb)) |
2682 s->keyframe ^= 1; | 2706 { |
2683 skip_bits(&gb, 1); | 2707 printf("Theora: bad frame indicator\n"); |
2708 return -1; | |
2709 } | |
2710 | |
2711 s->keyframe = !get_bits1(&gb); | |
2712 if (s->theora && s->keyframe) | |
2713 { | |
2714 if (get_bits1(&gb)) | |
2715 printf("Theora: warning, unsupported keyframe coding type?!\n"); | |
2716 skip_bits(&gb, 2); /* reserved? */ | |
2717 } | |
2718 else | |
2719 skip_bits(&gb, 1); | |
2684 s->last_quality_index = s->quality_index; | 2720 s->last_quality_index = s->quality_index; |
2685 s->quality_index = get_bits(&gb, 6); | 2721 s->quality_index = get_bits(&gb, 6); |
2686 | 2722 |
2687 debug_vp3(" VP3 frame #%d: Q index = %d", counter, s->quality_index); | 2723 debug_vp3(" VP3 %sframe #%d: Q index = %d\n", |
2724 s->keyframe?"key":"", counter, s->quality_index); | |
2688 counter++; | 2725 counter++; |
2689 | 2726 |
2690 if (s->quality_index != s->last_quality_index) | 2727 if (s->quality_index != s->last_quality_index) |
2691 init_dequantizer(s); | 2728 init_dequantizer(s); |
2692 | 2729 |
2693 if (s->keyframe) { | 2730 if (s->keyframe) { |
2694 | |
2695 debug_vp3(", keyframe\n"); | |
2696 /* skip the other 2 header bytes for now */ | 2731 /* skip the other 2 header bytes for now */ |
2697 skip_bits(&gb, 16); | 2732 if (!s->theora) skip_bits(&gb, 16); |
2698 if (s->last_frame.data[0] == s->golden_frame.data[0]) { | 2733 if (s->last_frame.data[0] == s->golden_frame.data[0]) { |
2699 if (s->golden_frame.data[0]) | 2734 if (s->golden_frame.data[0]) |
2700 avctx->release_buffer(avctx, &s->golden_frame); | 2735 avctx->release_buffer(avctx, &s->golden_frame); |
2701 s->last_frame= s->golden_frame; /* ensure that we catch any access to this released frame */ | 2736 s->last_frame= s->golden_frame; /* ensure that we catch any access to this released frame */ |
2702 } else { | 2737 } else { |
2718 /* time to figure out pixel addresses? */ | 2753 /* time to figure out pixel addresses? */ |
2719 if (!s->pixel_addresses_inited) | 2754 if (!s->pixel_addresses_inited) |
2720 vp3_calculate_pixel_addresses(s); | 2755 vp3_calculate_pixel_addresses(s); |
2721 | 2756 |
2722 } else { | 2757 } else { |
2723 | |
2724 debug_vp3("\n"); | |
2725 | |
2726 /* allocate a new current frame */ | 2758 /* allocate a new current frame */ |
2727 s->current_frame.reference = 3; | 2759 s->current_frame.reference = 3; |
2728 if(avctx->get_buffer(avctx, &s->current_frame) < 0) { | 2760 if(avctx->get_buffer(avctx, &s->current_frame) < 0) { |
2729 printf("vp3: get_buffer() failed\n"); | 2761 printf("vp3: get_buffer() failed\n"); |
2730 return -1; | 2762 return -1; |
2816 * to the same frame as either the golden or last frame */ | 2848 * to the same frame as either the golden or last frame */ |
2817 | 2849 |
2818 return 0; | 2850 return 0; |
2819 } | 2851 } |
2820 | 2852 |
2853 /* current version is 3.2.0 */ | |
2854 | |
2855 static int theora_decode_header(AVCodecContext *avctx, GetBitContext gb) | |
2856 { | |
2857 Vp3DecodeContext *s = avctx->priv_data; | |
2858 | |
2859 skip_bits(&gb, 8); /* version major */ | |
2860 skip_bits(&gb, 8); /* version minor */ | |
2861 skip_bits(&gb, 8); /* version micro */ | |
2862 | |
2863 s->width = get_bits(&gb, 16) << 4; | |
2864 s->height = get_bits(&gb, 16) << 4; | |
2865 | |
2866 skip_bits(&gb, 24); /* frame width */ | |
2867 skip_bits(&gb, 24); /* frame height */ | |
2868 | |
2869 skip_bits(&gb, 8); /* offset x */ | |
2870 skip_bits(&gb, 8); /* offset y */ | |
2871 | |
2872 skip_bits(&gb, 32); /* fps numerator */ | |
2873 skip_bits(&gb, 32); /* fps denumerator */ | |
2874 skip_bits(&gb, 24); /* aspect numerator */ | |
2875 skip_bits(&gb, 24); /* aspect denumerator */ | |
2876 | |
2877 skip_bits(&gb, 5); /* keyframe frequency force */ | |
2878 skip_bits(&gb, 8); /* colorspace */ | |
2879 skip_bits(&gb, 24); /* bitrate */ | |
2880 | |
2881 skip_bits(&gb, 6); /* last(?) quality index */ | |
2882 | |
2883 // align_get_bits(&gb); | |
2884 | |
2885 avctx->width = s->width; | |
2886 avctx->height = s->height; | |
2887 | |
2888 vp3_decode_init(avctx); | |
2889 | |
2890 return 0; | |
2891 } | |
2892 | |
2893 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext gb) | |
2894 { | |
2895 Vp3DecodeContext *s = avctx->priv_data; | |
2896 int i; | |
2897 | |
2898 /* quality threshold table */ | |
2899 for (i = 0; i < 64; i++) | |
2900 s->coded_quality_threshold[i] = get_bits(&gb, 16); | |
2901 | |
2902 /* dc scale factor table */ | |
2903 for (i = 0; i < 64; i++) | |
2904 s->coded_dc_scale_factor[i] = get_bits(&gb, 16); | |
2905 | |
2906 /* y coeffs */ | |
2907 for (i = 0; i < 64; i++) | |
2908 s->coded_intra_y_dequant[i] = get_bits(&gb, 8); | |
2909 | |
2910 /* uv coeffs */ | |
2911 for (i = 0; i < 64; i++) | |
2912 s->coded_intra_c_dequant[i] = get_bits(&gb, 8); | |
2913 | |
2914 /* inter coeffs */ | |
2915 for (i = 0; i < 64; i++) | |
2916 s->coded_inter_dequant[i] = get_bits(&gb, 8); | |
2917 | |
2918 s->theora_tables = 1; | |
2919 | |
2920 return 0; | |
2921 } | |
2922 | |
2923 static int theora_decode_init(AVCodecContext *avctx) | |
2924 { | |
2925 Vp3DecodeContext *s = avctx->priv_data; | |
2926 GetBitContext gb; | |
2927 int ptype; | |
2928 | |
2929 s->theora = 1; | |
2930 | |
2931 if (!avctx->extradata_size) | |
2932 return -1; | |
2933 | |
2934 init_get_bits(&gb, avctx->extradata, avctx->extradata_size); | |
2935 | |
2936 ptype = get_bits(&gb, 8); | |
2937 debug_vp3("Theora headerpacket type: %x\n", ptype); | |
2938 | |
2939 if (!(ptype & 0x80)) | |
2940 return -1; | |
2941 | |
2942 skip_bits(&gb, 6*8); /* "theora" */ | |
2943 | |
2944 switch(ptype) | |
2945 { | |
2946 case 0x80: | |
2947 theora_decode_header(avctx, gb); | |
2948 vp3_decode_init(avctx); | |
2949 break; | |
2950 case 0x81: | |
2951 /* comment */ | |
2952 break; | |
2953 case 0x82: | |
2954 theora_decode_tables(avctx, gb); | |
2955 break; | |
2956 } | |
2957 | |
2958 return 0; | |
2959 } | |
2960 | |
2821 AVCodec vp3_decoder = { | 2961 AVCodec vp3_decoder = { |
2822 "vp3", | 2962 "vp3", |
2823 CODEC_TYPE_VIDEO, | 2963 CODEC_TYPE_VIDEO, |
2824 CODEC_ID_VP3, | 2964 CODEC_ID_VP3, |
2825 sizeof(Vp3DecodeContext), | 2965 sizeof(Vp3DecodeContext), |
2828 vp3_decode_end, | 2968 vp3_decode_end, |
2829 vp3_decode_frame, | 2969 vp3_decode_frame, |
2830 0, | 2970 0, |
2831 NULL | 2971 NULL |
2832 }; | 2972 }; |
2973 | |
2974 AVCodec theora_decoder = { | |
2975 "theora", | |
2976 CODEC_TYPE_VIDEO, | |
2977 CODEC_ID_THEORA, | |
2978 sizeof(Vp3DecodeContext), | |
2979 theora_decode_init, | |
2980 NULL, | |
2981 vp3_decode_end, | |
2982 vp3_decode_frame, | |
2983 0, | |
2984 NULL | |
2985 }; |