Mercurial > libavcodec.hg
comparison ffv1.c @ 1306:799839d1e2e1 libavcodec
golomb rice codes
use gradients instead of prediction errors as context model
store independant quantization tables for each point
merge contexts with opposit sign
author | michaelni |
---|---|
date | Fri, 13 Jun 2003 21:31:28 +0000 |
parents | c41f51b5d5d6 |
children | 1cbc2380d172 |
comparison
equal
deleted
inserted
replaced
1305:abca3b39231d | 1306:799839d1e2e1 |
---|---|
26 | 26 |
27 #include "common.h" | 27 #include "common.h" |
28 #include "avcodec.h" | 28 #include "avcodec.h" |
29 #include "dsputil.h" | 29 #include "dsputil.h" |
30 #include "cabac.h" | 30 #include "cabac.h" |
31 #include "golomb.h" | |
31 | 32 |
32 #define MAX_PLANES 4 | 33 #define MAX_PLANES 4 |
33 #define CONTEXT_SIZE 32 | 34 #define CONTEXT_SIZE 32 |
34 | 35 |
35 #if 0 | 36 static const int8_t quant3[256]={ |
36 #define DEFAULT_QDIFF_COUNT (9) | 37 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
37 | |
38 static const uint8_t default_quant_table[512]={ | |
39 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
40 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
41 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
42 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
43 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
44 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
46 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, | |
47 4, | |
48 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, | |
49 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | |
50 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | |
51 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | |
52 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | |
53 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | |
54 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | |
55 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | |
56 }; | |
57 #else | |
58 #define DEFAULT_QDIFF_COUNT (16) | |
59 | |
60 static const uint8_t default_quant_table[256]={ | |
61 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
62 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
63 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
64 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
65 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 38 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
66 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 39 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
40 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
41 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
42 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
43 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
44 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
45 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | |
46 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | |
47 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | |
48 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | |
49 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | |
50 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | |
51 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | |
52 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0, | |
53 }; | |
54 static const int8_t quant5[256]={ | |
55 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
67 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | 56 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
68 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 7, | 57 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
69 8, | 58 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
70 9,10,11,11,12,12,12,12,13,13,13,13,13,13,13,13, | 59 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
71 14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14, | 60 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
72 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, | 61 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
73 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, | 62 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
74 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, | 63 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2, |
75 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, | 64 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2, |
76 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, | 65 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2, |
77 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, | 66 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2, |
78 }; | 67 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2, |
79 #endif | 68 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2, |
80 | 69 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2, |
81 static const int to5[16]={ | 70 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,-1, |
82 0,0,0,0, | |
83 0,0,0,1, | |
84 2,3,4,4, | |
85 4,4,4,4, | |
86 }; | 71 }; |
72 static const int8_t quant7[256]={ | |
73 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
74 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
75 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, | |
76 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, | |
77 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, | |
78 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, | |
79 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, | |
80 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, | |
81 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3, | |
82 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3, | |
83 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3, | |
84 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3, | |
85 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3, | |
86 -3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-2,-2,-2, | |
87 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2, | |
88 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1, | |
89 }; | |
90 static const int8_t quant9[256]={ | |
91 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, | |
92 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
93 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
94 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
95 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
96 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
97 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
98 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
99 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4, | |
100 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4, | |
101 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4, | |
102 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4, | |
103 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4, | |
104 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4, | |
105 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3, | |
106 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-1,-1, | |
107 }; | |
108 static const int8_t quant11[256]={ | |
109 0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, | |
110 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
111 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, | |
112 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, | |
113 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, | |
114 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, | |
115 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, | |
116 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, | |
117 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5, | |
118 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5, | |
119 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5, | |
120 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5, | |
121 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5, | |
122 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-4,-4, | |
123 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4, | |
124 -4,-4,-4,-4,-4,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-1, | |
125 }; | |
126 static const int8_t quant13[256]={ | |
127 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, | |
128 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, | |
129 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, | |
130 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, | |
131 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, | |
132 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, | |
133 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, | |
134 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, | |
135 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6, | |
136 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6, | |
137 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6, | |
138 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6, | |
139 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-5, | |
140 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5, | |
141 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5, | |
142 -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1, | |
143 }; | |
144 | |
145 static const uint8_t log2_run[32]={ | |
146 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, | |
147 4, 4, 5, 5, 6, 6, 7, 7, | |
148 8, 9,10,11,12,13,14,15, | |
149 }; | |
150 | |
151 typedef struct VlcState{ | |
152 int16_t drift; | |
153 uint16_t error_sum; | |
154 int8_t bias; | |
155 uint8_t count; | |
156 } VlcState; | |
87 | 157 |
88 typedef struct PlaneContext{ | 158 typedef struct PlaneContext{ |
89 uint8_t quant_table[256]; | |
90 int qdiff_count; | |
91 int context_count; | 159 int context_count; |
92 uint8_t (*state)[CONTEXT_SIZE]; | 160 uint8_t (*state)[CONTEXT_SIZE]; |
161 VlcState *vlc_state; | |
93 uint8_t interlace_bit_state[2]; | 162 uint8_t interlace_bit_state[2]; |
94 } PlaneContext; | 163 } PlaneContext; |
95 | 164 |
96 typedef struct FFV1Context{ | 165 typedef struct FFV1Context{ |
97 AVCodecContext *avctx; | 166 AVCodecContext *avctx; |
98 CABACContext c; | 167 CABACContext c; |
168 GetBitContext gb; | |
169 PutBitContext pb; | |
99 int version; | 170 int version; |
100 int width, height; | 171 int width, height; |
101 int chroma_h_shift, chroma_v_shift; | 172 int chroma_h_shift, chroma_v_shift; |
102 int flags; | 173 int flags; |
103 int picture_number; | 174 int picture_number; |
104 AVFrame picture; | 175 AVFrame picture; |
105 int plane_count; | 176 int plane_count; |
177 int ac; ///< 1-> CABAC 0-> golomb rice | |
106 PlaneContext plane[MAX_PLANES]; | 178 PlaneContext plane[MAX_PLANES]; |
179 int16_t quant_table[5][256]; | |
107 | 180 |
108 DSPContext dsp; | 181 DSPContext dsp; |
109 }FFV1Context; | 182 }FFV1Context; |
110 | 183 |
111 //1.774215 | 184 static inline int predict(uint8_t *src, uint8_t *last){ |
112 static inline int predict(FFV1Context *s, uint8_t *src, int stride, int x, int y){ | 185 const int LT= last[-1]; |
113 if(x && y){ | 186 const int T= last[ 0]; |
114 // const int RT= src[+1-stride]; | 187 const int L = src[-1]; |
115 const int LT= src[-1-stride]; | 188 uint8_t *cm = cropTbl + MAX_NEG_CROP; |
116 const int T= src[ -stride]; | 189 const int gradient= cm[L + T - LT]; |
117 const int L = src[-1 ]; | 190 |
118 uint8_t *cm = cropTbl + MAX_NEG_CROP; | 191 return mid_pred(L, gradient, T); |
119 const int gradient= cm[L + T - LT]; | 192 } |
120 | 193 |
121 // return gradient; | 194 static inline int get_context(FFV1Context *f, uint8_t *src, uint8_t *last, uint8_t *last2){ |
122 return mid_pred(L, gradient, T); | 195 const int LT= last[-1]; |
123 }else{ | 196 const int T= last[ 0]; |
124 if(y){ | 197 const int RT= last[ 1]; |
125 return src[ -stride]; | 198 const int L = src[-1]; |
126 }else if(x){ | 199 |
127 return src[-1 ]; | 200 if(f->quant_table[3][127]){ |
128 }else{ | 201 const int TT= last2[0]; |
129 return 128; | 202 const int LL= src[-2]; |
130 } | 203 return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF] |
131 } | 204 +f->quant_table[3][(LL-L) & 0xFF] + f->quant_table[4][(TT-T) & 0xFF]; |
132 } | 205 }else |
133 | 206 return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF]; |
134 | 207 } |
135 #if 0 | |
136 static inline void put_symbol(CABACContext, uint8_t *state, int v){ | |
137 put_cabac_ueg(c, state, v, 32, 1, 4 , 32); | |
138 } | |
139 | |
140 static inline int get_symbol(CABACContext, uint8_t *state){ | |
141 return get_cabac_ueg(c, state, 32, 1, 4 , 32); | |
142 } | |
143 #elif 0 | |
144 static inline void put_symbol(CABACContext *c, uint8_t *state, int v){ | |
145 if(v==0) | |
146 put_cabac(c, state+0, 1); | |
147 else{ | |
148 put_cabac(c, state+0, 0); | |
149 put_cabac(c, state+1, v<0); | |
150 if(v<0) state += 64; | |
151 put_cabac_ueg(c, state+2, ABS(v)-1, 32, 0, 4 , 32); | |
152 } | |
153 } | |
154 | |
155 static inline int get_symbol(CABACContext *c, uint8_t *state){ | |
156 if(get_cabac(c, state+0)) | |
157 return 0; | |
158 else{ | |
159 int sign= get_cabac(c, state+1); | |
160 if(sign) | |
161 return -1-get_cabac_ueg(c, state+66, 32, 0, 4 , 32); | |
162 else | |
163 return 1+get_cabac_ueg(c, state+2 , 32, 0, 4 , 32); | |
164 } | |
165 } | |
166 #else | |
167 | 208 |
168 /** | 209 /** |
169 * put | 210 * put |
170 */ | 211 */ |
171 static inline void put_symbol(CABACContext *c, uint8_t *state, int v, int is_signed){ | 212 static inline void put_symbol(CABACContext *c, uint8_t *state, int v, int is_signed){ |
212 return a; | 253 return a; |
213 }else | 254 }else |
214 return -128; | 255 return -128; |
215 } | 256 } |
216 } | 257 } |
258 | |
259 static inline void update_vlc_state(VlcState * const state, const int v){ | |
260 int drift= state->drift; | |
261 int count= state->count; | |
262 state->error_sum += ABS(v); | |
263 drift += v; | |
264 | |
265 if(count == 128){ //FIXME variable | |
266 count >>= 1; | |
267 drift >>= 1; | |
268 state->error_sum >>= 1; | |
269 } | |
270 count++; | |
271 | |
272 if(drift <= -count){ | |
273 if(state->bias > -128) state->bias--; | |
274 | |
275 drift += count; | |
276 if(drift <= -count) | |
277 drift= -count + 1; | |
278 }else if(drift > 0){ | |
279 if(state->bias < 127) state->bias++; | |
280 | |
281 drift -= count; | |
282 if(drift > 0) | |
283 drift= 0; | |
284 } | |
285 | |
286 state->drift= drift; | |
287 state->count= count; | |
288 } | |
289 | |
290 static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v){ | |
291 int i, k, code; | |
292 //printf("final: %d ", v); | |
293 v = (int8_t)(v - state->bias); | |
294 | |
295 i= state->count; | |
296 k=0; | |
297 while(i < state->error_sum){ //FIXME optimize | |
298 k++; | |
299 i += i; | |
300 } | |
301 #if 0 // JPEG LS | |
302 if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1); | |
303 else code= v; | |
304 #else | |
305 code= v ^ ((2*state->drift + state->count)>>31); | |
217 #endif | 306 #endif |
307 | |
308 code = -2*code-1; | |
309 code^= (code>>31); | |
310 //printf("v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code, state->bias, state->error_sum, state->drift, state->count, k); | |
311 set_ur_golomb(pb, code, k, 8, 8); | |
312 | |
313 update_vlc_state(state, v); | |
314 } | |
315 | |
316 static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state){ | |
317 int k, i, v, ret; | |
318 | |
319 i= state->count; | |
320 k=0; | |
321 while(i < state->error_sum){ //FIXME optimize | |
322 k++; | |
323 i += i; | |
324 } | |
325 | |
326 v= get_ur_golomb(gb, k, 8, 8); | |
327 //printf("v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k); | |
328 | |
329 v++; | |
330 if(v&1) v= (v>>1); | |
331 else v= -(v>>1); | |
332 | |
333 #if 0 // JPEG LS | |
334 if(k==0 && 2*state->drift <= - state->count) v ^= (-1); | |
335 #else | |
336 v ^= ((2*state->drift + state->count)>>31); | |
337 #endif | |
338 | |
339 ret= (int8_t)(v + state->bias); | |
340 | |
341 update_vlc_state(state, v); | |
342 //printf("final: %d\n", ret); | |
343 return ret; | |
344 } | |
345 | |
346 | |
218 | 347 |
219 static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){ | 348 static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){ |
220 PlaneContext * const p= &s->plane[plane_index]; | 349 PlaneContext * const p= &s->plane[plane_index]; |
221 CABACContext * const c= &s->c; | 350 CABACContext * const c= &s->c; |
222 int x,y; | 351 int x,y; |
223 uint8_t pred_diff_buffer[4][w+6]; | 352 uint8_t pred_diff_buffer[4][w+6]; //FIXME rema,e |
224 uint8_t *pred_diff[4]= {pred_diff_buffer[0]+3, pred_diff_buffer[1]+3, pred_diff_buffer[2]+3, pred_diff_buffer[3]+3}; | 353 uint8_t *pred_diff[4]= {pred_diff_buffer[0]+3, pred_diff_buffer[1]+3, pred_diff_buffer[2]+3, pred_diff_buffer[3]+3}; |
225 // uint8_t temp_buf[3*w], *temp= temp_buf + 3*w; | 354 int run_index=0; |
226 | 355 |
227 memset(pred_diff_buffer, 0, sizeof(pred_diff_buffer)); | 356 memset(pred_diff_buffer, 0, sizeof(pred_diff_buffer)); |
228 | 357 |
229 for(y=0; y<h; y++){ | 358 for(y=0; y<h; y++){ |
230 uint8_t *temp= pred_diff[0]; //FIXME try a normal buffer | 359 uint8_t *temp= pred_diff[0]; //FIXME try a normal buffer |
360 int run_count=0; | |
361 int run_mode=0; | |
231 | 362 |
232 pred_diff[0]= pred_diff[1]; | 363 pred_diff[0]= pred_diff[1]; |
233 pred_diff[1]= pred_diff[2]; | 364 pred_diff[1]= pred_diff[2]; |
234 pred_diff[2]= pred_diff[3]; | 365 pred_diff[2]= pred_diff[3]; |
235 pred_diff[3]= temp; | 366 pred_diff[3]= temp; |
367 | |
368 pred_diff[3][-1]= pred_diff[2][0 ]; | |
369 pred_diff[2][ w]= pred_diff[2][w-1]; | |
236 | 370 |
237 for(x=0; x<w; x++){ | 371 for(x=0; x<w; x++){ |
238 uint8_t *temp_src= src + x + stride*y; | 372 uint8_t *temp_src= src + x + stride*y; |
239 int diff, context, qdiff; | 373 int diff, context; |
240 | |
241 if(p->context_count == 256) | |
242 context= pred_diff[3+0][x-1] + 16*pred_diff[3-1][x+0]; | |
243 else | |
244 context= pred_diff[3+0][x-1] + 16*pred_diff[3-1][x+0] | |
245 + 16*16*to5[pred_diff[3-1][x+1]] + 16*16*5*to5[pred_diff[3-0][x-2]] + 16*16*5*5*to5[pred_diff[3-2][x+0]]; | |
246 | |
247 diff = (int8_t)(temp_src[0] - predict(s, temp_src, stride, x, y)); | |
248 | 374 |
249 qdiff= p->quant_table[128+diff]; | 375 context= get_context(s, pred_diff[3]+x, pred_diff[2]+x, pred_diff[1]+x); |
250 | 376 diff= temp_src[0] - predict(pred_diff[3]+x, pred_diff[2]+x); |
251 put_symbol(c, p->state[context], diff, 1); | 377 |
252 | 378 if(context < 0){ |
253 pred_diff[3][x]= qdiff; | 379 context = -context; |
254 } | 380 diff= -diff; |
255 } | 381 } |
256 } | 382 |
257 | 383 diff= (int8_t)diff; |
258 static void write_quant_table(CABACContext *c, uint8_t *quant_table){ | 384 |
385 if(s->ac) | |
386 put_symbol(c, p->state[context], diff, 1); | |
387 else{ | |
388 if(context == 0) run_mode=1; | |
389 | |
390 if(run_mode){ | |
391 | |
392 if(diff){ | |
393 while(run_count >= 1<<log2_run[run_index]){ | |
394 run_count -= 1<<log2_run[run_index]; | |
395 run_index++; | |
396 put_bits(&s->pb, 1, 1); | |
397 } | |
398 | |
399 put_bits(&s->pb, 1 + log2_run[run_index], run_count); | |
400 if(run_index) run_index--; | |
401 run_count=0; | |
402 run_mode=0; | |
403 if(diff>0) diff--; | |
404 }else{ | |
405 run_count++; | |
406 } | |
407 } | |
408 | |
409 // printf("count:%d index:%d, mode:%d, x:%d y:%d pos:%d\n", run_count, run_index, run_mode, x, y, (int)get_bit_count(&s->pb)); | |
410 | |
411 if(run_mode == 0) | |
412 put_vlc_symbol(&s->pb, &p->vlc_state[context], diff); | |
413 } | |
414 | |
415 pred_diff[3][x]= temp_src[0]; | |
416 } | |
417 if(run_mode){ | |
418 while(run_count >= 1<<log2_run[run_index]){ | |
419 run_count -= 1<<log2_run[run_index]; | |
420 run_index++; | |
421 put_bits(&s->pb, 1, 1); | |
422 } | |
423 | |
424 if(run_count) | |
425 put_bits(&s->pb, 1, 1); | |
426 } | |
427 } | |
428 } | |
429 | |
430 static void write_quant_table(CABACContext *c, int16_t *quant_table){ | |
259 int last=0; | 431 int last=0; |
260 int i; | 432 int i; |
261 uint8_t state[CONTEXT_SIZE]={0}; | 433 uint8_t state[CONTEXT_SIZE]={0}; |
262 | 434 |
263 for(i=1; i<256 ; i++){ | 435 for(i=1; i<128 ; i++){ |
264 if(quant_table[i] != quant_table[i-1]){ | 436 if(quant_table[i] != quant_table[i-1]){ |
265 put_symbol(c, state, i-last-1, 0); | 437 put_symbol(c, state, i-last-1, 0); |
266 last= i; | 438 last= i; |
267 } | 439 } |
268 } | 440 } |
273 uint8_t state[CONTEXT_SIZE]={0}; | 445 uint8_t state[CONTEXT_SIZE]={0}; |
274 int i; | 446 int i; |
275 CABACContext * const c= &f->c; | 447 CABACContext * const c= &f->c; |
276 | 448 |
277 put_symbol(c, state, f->version, 0); | 449 put_symbol(c, state, f->version, 0); |
450 put_symbol(c, state, f->avctx->coder_type, 0); | |
278 put_symbol(c, state, 0, 0); //YUV cs type | 451 put_symbol(c, state, 0, 0); //YUV cs type |
279 put_cabac(c, state, 1); //chroma planes | 452 put_cabac(c, state, 1); //chroma planes |
280 put_symbol(c, state, f->chroma_h_shift, 0); | 453 put_symbol(c, state, f->chroma_h_shift, 0); |
281 put_symbol(c, state, f->chroma_v_shift, 0); | 454 put_symbol(c, state, f->chroma_v_shift, 0); |
282 put_cabac(c, state, 0); //no transparency plane | 455 put_cabac(c, state, 0); //no transparency plane |
283 | 456 |
284 for(i=0; i<3; i++){ //FIXME chroma & trasparency decission | 457 for(i=0; i<5; i++) |
285 PlaneContext * const p= &f->plane[i]; | 458 write_quant_table(c, f->quant_table[i]); |
286 | |
287 put_symbol(c, state, av_log2(p->context_count), 0); | |
288 write_quant_table(c, p->quant_table); | |
289 } | |
290 } | 459 } |
291 | 460 |
292 static int common_init(AVCodecContext *avctx){ | 461 static int common_init(AVCodecContext *avctx){ |
293 FFV1Context *s = avctx->priv_data; | 462 FFV1Context *s = avctx->priv_data; |
294 int width, height; | 463 int width, height; |
312 int i; | 481 int i; |
313 | 482 |
314 common_init(avctx); | 483 common_init(avctx); |
315 | 484 |
316 s->version=0; | 485 s->version=0; |
486 s->ac= avctx->coder_type; | |
317 | 487 |
318 s->plane_count=3; | 488 s->plane_count=3; |
489 for(i=0; i<256; i++){ | |
490 s->quant_table[0][i]= quant11[i]; | |
491 s->quant_table[1][i]= 11*quant11[i]; | |
492 if(avctx->context_model==0){ | |
493 s->quant_table[2][i]= 11*11*quant11[i]; | |
494 s->quant_table[3][i]= | |
495 s->quant_table[4][i]=0; | |
496 }else{ | |
497 s->quant_table[2][i]= 11*11*quant5 [i]; | |
498 s->quant_table[3][i]= 5*11*11*quant5 [i]; | |
499 s->quant_table[4][i]= 5*5*11*11*quant5 [i]; | |
500 } | |
501 } | |
319 | 502 |
320 for(i=0; i<s->plane_count; i++){ | 503 for(i=0; i<s->plane_count; i++){ |
321 PlaneContext * const p= &s->plane[i]; | 504 PlaneContext * const p= &s->plane[i]; |
322 memcpy(p->quant_table, default_quant_table, sizeof(uint8_t)*256); | 505 |
323 p->qdiff_count= DEFAULT_QDIFF_COUNT; | 506 if(avctx->context_model==0){ |
324 | 507 p->context_count= (11*11*11+1)/2; |
325 #if 1 | 508 }else{ |
326 p->context_count= 256; | 509 p->context_count= (11*11*5*5*5+1)/2; |
327 p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t)); | 510 } |
328 #else | 511 |
329 p->context_count= 16*16*128 /*5*5*5*/; | 512 if(s->ac){ |
330 p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t)); | 513 if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t)); |
331 #endif | 514 }else{ |
515 if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState)); | |
516 } | |
332 } | 517 } |
333 | 518 |
334 avctx->coded_frame= &s->picture; | 519 avctx->coded_frame= &s->picture; |
335 switch(avctx->pix_fmt){ | 520 switch(avctx->pix_fmt){ |
336 case PIX_FMT_YUV444P: | 521 case PIX_FMT_YUV444P: |
360 | 545 |
361 p->interlace_bit_state[0]= 0; | 546 p->interlace_bit_state[0]= 0; |
362 p->interlace_bit_state[1]= 0; | 547 p->interlace_bit_state[1]= 0; |
363 | 548 |
364 for(j=0; j<p->context_count; j++){ | 549 for(j=0; j<p->context_count; j++){ |
365 memset(p->state[j], 0, sizeof(uint8_t)*CONTEXT_SIZE); | 550 if(f->ac){ |
366 p->state[j][7] = 2*62; | 551 memset(p->state[j], 0, sizeof(uint8_t)*CONTEXT_SIZE); |
552 p->state[j][7] = 2*62; | |
553 }else{ | |
554 p->vlc_state[j].drift= 0; | |
555 p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2); | |
556 p->vlc_state[j].bias= 0; | |
557 p->vlc_state[j].count= 1; | |
558 } | |
367 } | 559 } |
368 } | 560 } |
369 } | 561 } |
370 | 562 |
371 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){ | 563 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){ |
373 CABACContext * const c= &f->c; | 565 CABACContext * const c= &f->c; |
374 AVFrame *pict = data; | 566 AVFrame *pict = data; |
375 const int width= f->width; | 567 const int width= f->width; |
376 const int height= f->height; | 568 const int height= f->height; |
377 AVFrame * const p= &f->picture; | 569 AVFrame * const p= &f->picture; |
570 int used_count= 0; | |
378 | 571 |
379 if(avctx->strict_std_compliance >= 0){ | 572 if(avctx->strict_std_compliance >= 0){ |
380 printf("this codec is under development, files encoded with it wont be decodeable with future versions!!!\n" | 573 printf("this codec is under development, files encoded with it wont be decodeable with future versions!!!\n" |
381 "use vstrict=-1 to use it anyway\n"); | 574 "use vstrict=-1 to use it anyway\n"); |
382 return -1; | 575 return -1; |
396 }else{ | 589 }else{ |
397 put_cabac_bypass(c, 0); | 590 put_cabac_bypass(c, 0); |
398 p->key_frame= 0; | 591 p->key_frame= 0; |
399 } | 592 } |
400 | 593 |
594 if(!f->ac){ | |
595 used_count += put_cabac_terminate(c, 1); | |
596 //printf("pos=%d\n", used_count); | |
597 init_put_bits(&f->pb, buf + used_count, buf_size - used_count, NULL, NULL); | |
598 } | |
599 | |
401 if(1){ | 600 if(1){ |
402 const int chroma_width = -((-width )>>f->chroma_h_shift); | 601 const int chroma_width = -((-width )>>f->chroma_h_shift); |
403 const int chroma_height= -((-height)>>f->chroma_v_shift); | 602 const int chroma_height= -((-height)>>f->chroma_v_shift); |
404 | 603 |
405 encode_plane(f, p->data[0], width, height, p->linesize[0], 0); | 604 encode_plane(f, p->data[0], width, height, p->linesize[0], 0); |
409 } | 608 } |
410 emms_c(); | 609 emms_c(); |
411 | 610 |
412 f->picture_number++; | 611 f->picture_number++; |
413 | 612 |
414 return put_cabac_terminate(c, 1); | 613 if(f->ac){ |
614 return put_cabac_terminate(c, 1); | |
615 }else{ | |
616 flush_put_bits(&f->pb); //nicer padding FIXME | |
617 return used_count + (get_bit_count(&f->pb)+7)/8; | |
618 } | |
415 } | 619 } |
416 | 620 |
417 static void common_end(FFV1Context *s){ | 621 static void common_end(FFV1Context *s){ |
418 int i; | 622 int i; |
419 | 623 |
437 PlaneContext * const p= &s->plane[plane_index]; | 641 PlaneContext * const p= &s->plane[plane_index]; |
438 CABACContext * const c= &s->c; | 642 CABACContext * const c= &s->c; |
439 int x,y; | 643 int x,y; |
440 uint8_t pred_diff_buffer[4][w+6]; | 644 uint8_t pred_diff_buffer[4][w+6]; |
441 uint8_t *pred_diff[4]= {pred_diff_buffer[0]+3, pred_diff_buffer[1]+3, pred_diff_buffer[2]+3, pred_diff_buffer[3]+3}; | 645 uint8_t *pred_diff[4]= {pred_diff_buffer[0]+3, pred_diff_buffer[1]+3, pred_diff_buffer[2]+3, pred_diff_buffer[3]+3}; |
442 // uint8_t temp_buf[3*w], *temp= temp_buf + 3*w; | 646 int run_index=0; |
443 | 647 |
444 memset(pred_diff_buffer, 0, sizeof(pred_diff_buffer)); | 648 memset(pred_diff_buffer, 0, sizeof(pred_diff_buffer)); |
445 | 649 |
446 for(y=0; y<h; y++){ | 650 for(y=0; y<h; y++){ |
447 uint8_t *temp= pred_diff[0]; //FIXME try a normal buffer | 651 uint8_t *temp= pred_diff[0]; //FIXME try a normal buffer |
652 int run_count=0; | |
653 int run_mode=0; | |
448 | 654 |
449 pred_diff[0]= pred_diff[1]; | 655 pred_diff[0]= pred_diff[1]; |
450 pred_diff[1]= pred_diff[2]; | 656 pred_diff[1]= pred_diff[2]; |
451 pred_diff[2]= pred_diff[3]; | 657 pred_diff[2]= pred_diff[3]; |
452 pred_diff[3]= temp; | 658 pred_diff[3]= temp; |
453 | 659 |
660 pred_diff[3][-1]= pred_diff[2][0 ]; | |
661 pred_diff[2][ w]= pred_diff[2][w-1]; | |
662 | |
454 for(x=0; x<w; x++){ | 663 for(x=0; x<w; x++){ |
455 uint8_t *temp_src= src + x + stride*y; | 664 uint8_t *temp_src= src + x + stride*y; |
456 int diff, context, qdiff; | 665 int diff, context, sign; |
457 | 666 |
458 if(p->context_count == 256) | 667 context= get_context(s, pred_diff[3] + x, pred_diff[2] + x, pred_diff[1] + x); |
459 context= pred_diff[3+0][x-1] + 16*pred_diff[3-1][x+0]; | 668 if(context < 0){ |
460 else | 669 context= -context; |
461 context= pred_diff[3+0][x-1] + 16*pred_diff[3-1][x+0] | 670 sign=1; |
462 + 16*16*to5[pred_diff[3-1][x+1]] + 16*16*5*to5[pred_diff[3-0][x-2]] + 16*16*5*5*to5[pred_diff[3-2][x+0]]; | 671 }else |
463 | 672 sign=0; |
464 diff= get_symbol(c, p->state[context], 1); | 673 |
465 | 674 |
466 temp_src[0] = predict(s, temp_src, stride, x, y) + diff; | 675 if(s->ac) |
676 diff= get_symbol(c, p->state[context], 1); | |
677 else{ | |
678 if(context == 0 && run_mode==0) run_mode=1; | |
679 | |
680 if(run_mode){ | |
681 if(run_count==0 && run_mode==1){ | |
682 if(get_bits1(&s->gb)){ | |
683 run_count = 1<<log2_run[run_index]; | |
684 if(x + run_count <= w) run_index++; | |
685 }else{ | |
686 if(log2_run[run_index]) run_count = get_bits(&s->gb, log2_run[run_index]); | |
687 else run_count=0; | |
688 if(run_index) run_index--; | |
689 run_mode=2; | |
690 } | |
691 } | |
692 run_count--; | |
693 if(run_count < 0){ | |
694 run_mode=0; | |
695 run_count=0; | |
696 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context]); | |
697 if(diff>=0) diff++; | |
698 }else | |
699 diff=0; | |
700 }else | |
701 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context]); | |
702 | |
703 // printf("count:%d index:%d, mode:%d, x:%d y:%d pos:%d\n", run_count, run_index, run_mode, x, y, get_bits_count(&s->gb)); | |
704 } | |
705 | |
706 if(sign) diff= (int8_t)(-diff); //FIXME remove cast | |
707 | |
708 pred_diff[3][x]= | |
709 temp_src[0] = predict(pred_diff[3] + x, pred_diff[2] + x) + diff; | |
467 | 710 |
468 assert(diff>= -128 && diff <= 127); | 711 assert(diff>= -128 && diff <= 127); |
469 | 712 } |
470 qdiff= p->quant_table[128+diff]; | 713 } |
471 | 714 } |
472 pred_diff[3][x]= qdiff; | 715 |
473 } | 716 static int read_quant_table(CABACContext *c, int16_t *quant_table, int scale){ |
474 } | |
475 } | |
476 | |
477 static int read_quant_table(CABACContext *c, uint8_t *quant_table){ | |
478 int v; | 717 int v; |
479 int i=0; | 718 int i=0; |
480 uint8_t state[CONTEXT_SIZE]={0}; | 719 uint8_t state[CONTEXT_SIZE]={0}; |
481 | 720 |
482 for(v=0; i<256 ; v++){ | 721 for(v=0; i<128 ; v++){ |
483 int len= get_symbol(c, state, 0) + 1; | 722 int len= get_symbol(c, state, 0) + 1; |
484 | 723 |
485 if(len + i > 256) return -1; | 724 if(len + i > 128) return -1; |
486 | 725 |
487 while(len--){ | 726 while(len--){ |
488 quant_table[i++] = v; | 727 quant_table[i] = scale*v; |
728 i++; | |
489 //printf("%2d ",v); | 729 //printf("%2d ",v); |
490 //if(i%16==0) printf("\n"); | 730 //if(i%16==0) printf("\n"); |
491 } | 731 } |
492 } | 732 } |
493 | 733 |
494 return v; | 734 for(i=1; i<128; i++){ |
735 quant_table[256-i]= -quant_table[i]; | |
736 } | |
737 quant_table[128]= -quant_table[127]; | |
738 | |
739 return 2*v - 1; | |
495 } | 740 } |
496 | 741 |
497 static int read_header(FFV1Context *f){ | 742 static int read_header(FFV1Context *f){ |
498 uint8_t state[CONTEXT_SIZE]={0}; | 743 uint8_t state[CONTEXT_SIZE]={0}; |
499 int i; | 744 int i, context_count; |
500 CABACContext * const c= &f->c; | 745 CABACContext * const c= &f->c; |
501 | 746 |
502 f->version= get_symbol(c, state, 0); | 747 f->version= get_symbol(c, state, 0); |
748 f->ac= f->avctx->coder_type= get_symbol(c, state, 0); | |
503 get_symbol(c, state, 0); //YUV cs type | 749 get_symbol(c, state, 0); //YUV cs type |
504 get_cabac(c, state); //no chroma = false | 750 get_cabac(c, state); //no chroma = false |
505 f->chroma_h_shift= get_symbol(c, state, 0); | 751 f->chroma_h_shift= get_symbol(c, state, 0); |
506 f->chroma_v_shift= get_symbol(c, state, 0); | 752 f->chroma_v_shift= get_symbol(c, state, 0); |
507 get_cabac(c, state); //transparency plane | 753 get_cabac(c, state); //transparency plane |
508 f->plane_count= 3; | 754 f->plane_count= 3; |
509 | 755 |
756 context_count=1; | |
757 for(i=0; i<5; i++){ | |
758 context_count*= read_quant_table(c, f->quant_table[i], context_count); | |
759 } | |
760 context_count= (context_count+1)/2; | |
761 | |
510 for(i=0; i<f->plane_count; i++){ | 762 for(i=0; i<f->plane_count; i++){ |
511 PlaneContext * const p= &f->plane[i]; | 763 PlaneContext * const p= &f->plane[i]; |
512 | 764 |
513 p->context_count= 1<<get_symbol(c, state, 0); | 765 p->context_count= context_count; |
514 p->qdiff_count= read_quant_table(c, p->quant_table); | 766 |
515 if(p->qdiff_count < 0) return -1; | 767 if(f->ac){ |
516 | 768 if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t)); |
517 if(!p->state) | 769 }else{ |
518 p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t)); | 770 if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState)); |
771 } | |
519 } | 772 } |
520 | 773 |
521 return 0; | 774 return 0; |
522 } | 775 } |
523 | 776 |
524 static int decode_init(AVCodecContext *avctx) | 777 static int decode_init(AVCodecContext *avctx) |
525 { | 778 { |
526 FFV1Context *s = avctx->priv_data; | 779 // FFV1Context *s = avctx->priv_data; |
527 | 780 |
528 common_init(avctx); | 781 common_init(avctx); |
529 | 782 |
530 #if 0 | 783 #if 0 |
531 switch(s->bitstream_bpp){ | 784 switch(s->bitstream_bpp){ |
585 p->key_frame= 0; | 838 p->key_frame= 0; |
586 } | 839 } |
587 if(avctx->debug&FF_DEBUG_PICT_INFO) | 840 if(avctx->debug&FF_DEBUG_PICT_INFO) |
588 printf("keyframe:%d\n", p->key_frame); | 841 printf("keyframe:%d\n", p->key_frame); |
589 | 842 |
843 if(!f->ac){ | |
844 bytes_read = get_cabac_terminate(c); | |
845 if(bytes_read ==0) printf("error at end of AC stream\n"); | |
846 //printf("pos=%d\n", bytes_read); | |
847 init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read); | |
848 } | |
590 | 849 |
591 if(1){ | 850 if(1){ |
592 const int chroma_width = -((-width )>>f->chroma_h_shift); | 851 const int chroma_width = -((-width )>>f->chroma_h_shift); |
593 const int chroma_height= -((-height)>>f->chroma_v_shift); | 852 const int chroma_height= -((-height)>>f->chroma_v_shift); |
594 decode_plane(f, p->data[0], width, height, p->linesize[0], 0); | 853 decode_plane(f, p->data[0], width, height, p->linesize[0], 0); |
605 | 864 |
606 avctx->release_buffer(avctx, p); //FIXME | 865 avctx->release_buffer(avctx, p); //FIXME |
607 | 866 |
608 *data_size = sizeof(AVFrame); | 867 *data_size = sizeof(AVFrame); |
609 | 868 |
610 bytes_read= get_cabac_terminate(c); | 869 if(f->ac){ |
611 if(bytes_read ==0) printf("error at end of frame\n"); | 870 bytes_read= get_cabac_terminate(c); |
612 | 871 if(bytes_read ==0) printf("error at end of frame\n"); |
872 }else{ | |
873 bytes_read+= (get_bits_count(&f->gb)+7)/8; | |
874 } | |
875 | |
613 return bytes_read; | 876 return bytes_read; |
614 } | 877 } |
615 | 878 |
616 static int decode_end(AVCodecContext *avctx) | 879 static int decode_end(AVCodecContext *avctx) |
617 { | 880 { |