Mercurial > libavcodec.hg
annotate huffyuv.c @ 2373:f977a8871c2c libavcodec
split ffhuffyuv into 2 codecs:
"huffyuv" is compatible with the official version
"ffvhuff" contains our improvements
author | lorenm |
---|---|
date | Sun, 28 Nov 2004 18:29:38 +0000 |
parents | 26560d4fdb1f |
children | b36811e1386a |
rev | line source |
---|---|
866 | 1 /* |
2 * huffyuv codec for libavcodec | |
3 * | |
1006 | 4 * Copyright (c) 2002-2003 Michael Niedermayer <michaelni@gmx.at> |
866 | 5 * |
6 * This library is free software; you can redistribute it and/or | |
7 * modify it under the terms of the GNU Lesser General Public | |
8 * License as published by the Free Software Foundation; either | |
9 * version 2 of the License, or (at your option) any later version. | |
10 * | |
11 * This library is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * Lesser General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU Lesser General Public | |
17 * License along with this library; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 * | |
20 * see http://www.pcisys.net/~melanson/codecs/huffyuv.txt for a description of | |
21 * the algorithm used | |
22 */ | |
1106 | 23 |
24 /** | |
25 * @file huffyuv.c | |
26 * huffyuv codec for libavcodec. | |
27 */ | |
866 | 28 |
29 #include "common.h" | |
30 #include "avcodec.h" | |
31 #include "dsputil.h" | |
32 | |
33 #define VLC_BITS 11 | |
903 | 34 |
2176 | 35 #ifdef WORDS_BIGENDIAN |
36 #define B 3 | |
37 #define G 2 | |
38 #define R 1 | |
39 #else | |
40 #define B 0 | |
41 #define G 1 | |
42 #define R 2 | |
43 #endif | |
44 | |
866 | 45 typedef enum Predictor{ |
46 LEFT= 0, | |
47 PLANE, | |
48 MEDIAN, | |
49 } Predictor; | |
50 | |
51 typedef struct HYuvContext{ | |
52 AVCodecContext *avctx; | |
53 Predictor predictor; | |
54 GetBitContext gb; | |
55 PutBitContext pb; | |
56 int interlaced; | |
57 int decorrelate; | |
58 int bitstream_bpp; | |
59 int version; | |
60 int yuy2; //use yuy2 instead of 422P | |
61 int bgr32; //use bgr32 instead of bgr24 | |
62 int width, height; | |
63 int flags; | |
2369 | 64 int context; |
866 | 65 int picture_number; |
871 | 66 int last_slice_end; |
1528 | 67 uint8_t __align8 temp[3][2560]; |
866 | 68 uint64_t stats[3][256]; |
69 uint8_t len[3][256]; | |
70 uint32_t bits[3][256]; | |
71 VLC vlc[3]; | |
925 | 72 AVFrame picture; |
866 | 73 uint8_t __align8 bitstream_buffer[1024*1024*3]; //FIXME dynamic alloc or some other solution |
74 DSPContext dsp; | |
75 }HYuvContext; | |
76 | |
1082 | 77 static const unsigned char classic_shift_luma[] = { |
1080
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
78 34,36,35,69,135,232,9,16,10,24,11,23,12,16,13,10,14,8,15,8, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
79 16,8,17,20,16,10,207,206,205,236,11,8,10,21,9,23,8,8,199,70, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
80 69,68, 0 |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
81 }; |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
82 |
1082 | 83 static const unsigned char classic_shift_chroma[] = { |
1080
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
84 66,36,37,38,39,40,41,75,76,77,110,239,144,81,82,83,84,85,118,183, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
85 56,57,88,89,56,89,154,57,58,57,26,141,57,56,58,57,58,57,184,119, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
86 214,245,116,83,82,49,80,79,78,77,44,75,41,40,39,38,37,36,34, 0 |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
87 }; |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
88 |
1082 | 89 static const unsigned char classic_add_luma[256] = { |
1080
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
90 3, 9, 5, 12, 10, 35, 32, 29, 27, 50, 48, 45, 44, 41, 39, 37, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
91 73, 70, 68, 65, 64, 61, 58, 56, 53, 50, 49, 46, 44, 41, 38, 36, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
92 68, 65, 63, 61, 58, 55, 53, 51, 48, 46, 45, 43, 41, 39, 38, 36, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
93 35, 33, 32, 30, 29, 27, 26, 25, 48, 47, 46, 44, 43, 41, 40, 39, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
94 37, 36, 35, 34, 32, 31, 30, 28, 27, 26, 24, 23, 22, 20, 19, 37, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
95 35, 34, 33, 31, 30, 29, 27, 26, 24, 23, 21, 20, 18, 17, 15, 29, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
96 27, 26, 24, 22, 21, 19, 17, 16, 14, 26, 25, 23, 21, 19, 18, 16, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
97 15, 27, 25, 23, 21, 19, 17, 16, 14, 26, 25, 23, 21, 18, 17, 14, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
98 12, 17, 19, 13, 4, 9, 2, 11, 1, 7, 8, 0, 16, 3, 14, 6, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
99 12, 10, 5, 15, 18, 11, 10, 13, 15, 16, 19, 20, 22, 24, 27, 15, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
100 18, 20, 22, 24, 26, 14, 17, 20, 22, 24, 27, 15, 18, 20, 23, 25, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
101 28, 16, 19, 22, 25, 28, 32, 36, 21, 25, 29, 33, 38, 42, 45, 49, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
102 28, 31, 34, 37, 40, 42, 44, 47, 49, 50, 52, 54, 56, 57, 59, 60, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
103 62, 64, 66, 67, 69, 35, 37, 39, 40, 42, 43, 45, 47, 48, 51, 52, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
104 54, 55, 57, 59, 60, 62, 63, 66, 67, 69, 71, 72, 38, 40, 42, 43, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
105 46, 47, 49, 51, 26, 28, 30, 31, 33, 34, 18, 19, 11, 13, 7, 8, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
106 }; |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
107 |
1082 | 108 static const unsigned char classic_add_chroma[256] = { |
1080
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
109 3, 1, 2, 2, 2, 2, 3, 3, 7, 5, 7, 5, 8, 6, 11, 9, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
110 7, 13, 11, 10, 9, 8, 7, 5, 9, 7, 6, 4, 7, 5, 8, 7, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
111 11, 8, 13, 11, 19, 15, 22, 23, 20, 33, 32, 28, 27, 29, 51, 77, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
112 43, 45, 76, 81, 46, 82, 75, 55, 56,144, 58, 80, 60, 74,147, 63, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
113 143, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
114 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 27, 30, 21, 22, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
115 17, 14, 5, 6,100, 54, 47, 50, 51, 53,106,107,108,109,110,111, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
116 112,113,114,115, 4,117,118, 92, 94,121,122, 3,124,103, 2, 1, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
117 0,129,130,131,120,119,126,125,136,137,138,139,140,141,142,134, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
118 135,132,133,104, 64,101, 62, 57,102, 95, 93, 59, 61, 28, 97, 96, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
119 52, 49, 48, 29, 32, 25, 24, 46, 23, 98, 45, 44, 43, 20, 42, 41, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
120 19, 18, 99, 40, 15, 39, 38, 16, 13, 12, 11, 37, 10, 9, 8, 36, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
121 7,128,127,105,123,116, 35, 34, 33,145, 31, 79, 42,146, 78, 26, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
122 83, 48, 49, 50, 44, 47, 26, 31, 30, 18, 17, 19, 21, 24, 25, 13, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
123 14, 16, 17, 18, 20, 21, 12, 14, 15, 9, 10, 6, 9, 6, 5, 8, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
124 6, 12, 8, 10, 7, 9, 6, 4, 6, 2, 2, 3, 3, 3, 3, 2, |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
125 }; |
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
126 |
866 | 127 static inline int add_left_prediction(uint8_t *dst, uint8_t *src, int w, int acc){ |
128 int i; | |
129 | |
130 for(i=0; i<w-1; i++){ | |
131 acc+= src[i]; | |
132 dst[i]= acc; | |
133 i++; | |
134 acc+= src[i]; | |
135 dst[i]= acc; | |
136 } | |
137 | |
138 for(; i<w; i++){ | |
139 acc+= src[i]; | |
140 dst[i]= acc; | |
141 } | |
142 | |
143 return acc; | |
144 } | |
145 | |
146 static inline void add_median_prediction(uint8_t *dst, uint8_t *src1, uint8_t *diff, int w, int *left, int *left_top){ | |
147 int i; | |
148 uint8_t l, lt; | |
149 | |
150 l= *left; | |
151 lt= *left_top; | |
152 | |
153 for(i=0; i<w; i++){ | |
154 l= mid_pred(l, src1[i], (l + src1[i] - lt)&0xFF) + diff[i]; | |
155 lt= src1[i]; | |
156 dst[i]= l; | |
157 } | |
158 | |
159 *left= l; | |
160 *left_top= lt; | |
161 } | |
1232
e88d3b1fb2a1
more #ifdef CONFIG_ENCODERS by (Wolfgang Hesseler <qv at multimediaware dot com>)
michaelni
parents:
1228
diff
changeset
|
162 |
866 | 163 static inline void add_left_prediction_bgr32(uint8_t *dst, uint8_t *src, int w, int *red, int *green, int *blue){ |
164 int i; | |
165 int r,g,b; | |
166 r= *red; | |
167 g= *green; | |
168 b= *blue; | |
169 | |
170 for(i=0; i<w; i++){ | |
2176 | 171 b+= src[4*i+B]; |
172 g+= src[4*i+G]; | |
173 r+= src[4*i+R]; | |
866 | 174 |
2176 | 175 dst[4*i+B]= b; |
176 dst[4*i+G]= g; | |
177 dst[4*i+R]= r; | |
866 | 178 } |
179 | |
180 *red= r; | |
181 *green= g; | |
182 *blue= b; | |
183 } | |
184 | |
871 | 185 static inline int sub_left_prediction(HYuvContext *s, uint8_t *dst, uint8_t *src, int w, int left){ |
866 | 186 int i; |
871 | 187 if(w<32){ |
188 for(i=0; i<w; i++){ | |
189 const int temp= src[i]; | |
190 dst[i]= temp - left; | |
191 left= temp; | |
192 } | |
193 return left; | |
194 }else{ | |
195 for(i=0; i<16; i++){ | |
196 const int temp= src[i]; | |
197 dst[i]= temp - left; | |
198 left= temp; | |
199 } | |
200 s->dsp.diff_bytes(dst+16, src+16, src+15, w-16); | |
201 return src[w-1]; | |
866 | 202 } |
203 } | |
1325 | 204 |
866 | 205 static void read_len_table(uint8_t *dst, GetBitContext *gb){ |
206 int i, val, repeat; | |
207 | |
208 for(i=0; i<256;){ | |
209 repeat= get_bits(gb, 3); | |
210 val = get_bits(gb, 5); | |
211 if(repeat==0) | |
212 repeat= get_bits(gb, 8); | |
213 //printf("%d %d\n", val, repeat); | |
214 while (repeat--) | |
215 dst[i++] = val; | |
216 } | |
217 } | |
218 | |
219 static int generate_bits_table(uint32_t *dst, uint8_t *len_table){ | |
220 int len, index; | |
221 uint32_t bits=0; | |
222 | |
223 for(len=32; len>0; len--){ | |
224 for(index=0; index<256; index++){ | |
1279 | 225 if(len_table[index]==len) |
226 dst[index]= bits++; | |
866 | 227 } |
1279 | 228 if(bits & 1){ |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1529
diff
changeset
|
229 av_log(NULL, AV_LOG_ERROR, "Error generating huffman table\n"); |
1279 | 230 return -1; |
231 } | |
232 bits >>= 1; | |
866 | 233 } |
234 return 0; | |
235 } | |
236 | |
237 static void generate_len_table(uint8_t *dst, uint64_t *stats, int size){ | |
238 uint64_t counts[2*size]; | |
239 int up[2*size]; | |
240 int offset, i, next; | |
241 | |
242 for(offset=1; ; offset<<=1){ | |
243 for(i=0; i<size; i++){ | |
244 counts[i]= stats[i] + offset - 1; | |
245 } | |
246 | |
247 for(next=size; next<size*2; next++){ | |
248 uint64_t min1, min2; | |
249 int min1_i, min2_i; | |
250 | |
251 min1=min2= INT64_MAX; | |
252 min1_i= min2_i=-1; | |
253 | |
254 for(i=0; i<next; i++){ | |
255 if(min2 > counts[i]){ | |
256 if(min1 > counts[i]){ | |
257 min2= min1; | |
258 min2_i= min1_i; | |
259 min1= counts[i]; | |
260 min1_i= i; | |
261 }else{ | |
262 min2= counts[i]; | |
263 min2_i= i; | |
264 } | |
265 } | |
266 } | |
267 | |
268 if(min2==INT64_MAX) break; | |
269 | |
270 counts[next]= min1 + min2; | |
271 counts[min1_i]= | |
869 | 272 counts[min2_i]= INT64_MAX; |
866 | 273 up[min1_i]= |
274 up[min2_i]= next; | |
275 up[next]= -1; | |
276 } | |
277 | |
278 for(i=0; i<size; i++){ | |
279 int len; | |
280 int index=i; | |
281 | |
282 for(len=0; up[index] != -1; len++) | |
283 index= up[index]; | |
284 | |
2233 | 285 if(len >= 32) break; |
866 | 286 |
287 dst[i]= len; | |
288 } | |
289 if(i==size) break; | |
290 } | |
291 } | |
292 | |
293 static int read_huffman_tables(HYuvContext *s, uint8_t *src, int length){ | |
294 GetBitContext gb; | |
295 int i; | |
296 | |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
1006
diff
changeset
|
297 init_get_bits(&gb, src, length*8); |
866 | 298 |
299 for(i=0; i<3; i++){ | |
300 read_len_table(s->len[i], &gb); | |
301 | |
302 if(generate_bits_table(s->bits[i], s->len[i])<0){ | |
303 return -1; | |
304 } | |
305 #if 0 | |
306 for(j=0; j<256; j++){ | |
307 printf("%6X, %2d, %3d\n", s->bits[i][j], s->len[i][j], j); | |
308 } | |
309 #endif | |
2369 | 310 free_vlc(&s->vlc[i]); |
2370
26560d4fdb1f
Memory leak fix patch by (Burkhard Plaum <plaum >at< ipf.uni-stuttgart )dot( de>)
michael
parents:
2369
diff
changeset
|
311 init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4, 0); |
866 | 312 } |
313 | |
2369 | 314 return (get_bits_count(&gb)+7)/8; |
866 | 315 } |
316 | |
317 static int read_old_huffman_tables(HYuvContext *s){ | |
1080
a150aba978de
huffyuv v1 tables, as they are essential and the only possible way for decding of v1 files they very likely cant be copyrighted ...
michaelni
parents:
1078
diff
changeset
|
318 #if 1 |
866 | 319 GetBitContext gb; |
320 int i; | |
321 | |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
1006
diff
changeset
|
322 init_get_bits(&gb, classic_shift_luma, sizeof(classic_shift_luma)*8); |
866 | 323 read_len_table(s->len[0], &gb); |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
1006
diff
changeset
|
324 init_get_bits(&gb, classic_shift_chroma, sizeof(classic_shift_chroma)*8); |
866 | 325 read_len_table(s->len[1], &gb); |
326 | |
327 for(i=0; i<256; i++) s->bits[0][i] = classic_add_luma [i]; | |
328 for(i=0; i<256; i++) s->bits[1][i] = classic_add_chroma[i]; | |
329 | |
330 if(s->bitstream_bpp >= 24){ | |
331 memcpy(s->bits[1], s->bits[0], 256*sizeof(uint32_t)); | |
332 memcpy(s->len[1] , s->len [0], 256*sizeof(uint8_t)); | |
333 } | |
334 memcpy(s->bits[2], s->bits[1], 256*sizeof(uint32_t)); | |
335 memcpy(s->len[2] , s->len [1], 256*sizeof(uint8_t)); | |
336 | |
2369 | 337 for(i=0; i<3; i++){ |
338 free_vlc(&s->vlc[i]); | |
2370
26560d4fdb1f
Memory leak fix patch by (Burkhard Plaum <plaum >at< ipf.uni-stuttgart )dot( de>)
michael
parents:
2369
diff
changeset
|
339 init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4, 0); |
2369 | 340 } |
866 | 341 |
342 return 0; | |
343 #else | |
344 fprintf(stderr, "v1 huffyuv is not supported \n"); | |
345 return -1; | |
346 #endif | |
347 } | |
348 | |
349 static int decode_init(AVCodecContext *avctx) | |
350 { | |
351 HYuvContext *s = avctx->priv_data; | |
903 | 352 int width, height; |
866 | 353 |
354 s->avctx= avctx; | |
355 s->flags= avctx->flags; | |
356 | |
1097 | 357 dsputil_init(&s->dsp, avctx); |
2369 | 358 memset(s->vlc, 0, 3*sizeof(VLC)); |
866 | 359 |
360 width= s->width= avctx->width; | |
361 height= s->height= avctx->height; | |
925 | 362 avctx->coded_frame= &s->picture; |
903 | 363 |
866 | 364 s->bgr32=1; |
365 assert(width && height); | |
366 //if(avctx->extradata) | |
367 // printf("extradata:%X, extradata_size:%d\n", *(uint32_t*)avctx->extradata, avctx->extradata_size); | |
368 if(avctx->extradata_size){ | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
369 if((avctx->bits_per_sample&7) && avctx->bits_per_sample != 12) |
866 | 370 s->version=1; // do such files exist at all? |
371 else | |
372 s->version=2; | |
373 }else | |
374 s->version=0; | |
375 | |
376 if(s->version==2){ | |
377 int method; | |
378 | |
379 method= ((uint8_t*)avctx->extradata)[0]; | |
380 s->decorrelate= method&64 ? 1 : 0; | |
381 s->predictor= method&63; | |
382 s->bitstream_bpp= ((uint8_t*)avctx->extradata)[1]; | |
383 if(s->bitstream_bpp==0) | |
384 s->bitstream_bpp= avctx->bits_per_sample&~7; | |
2369 | 385 s->context= ((uint8_t*)avctx->extradata)[2] & 0x40 ? 1 : 0; |
866 | 386 |
387 if(read_huffman_tables(s, ((uint8_t*)avctx->extradata)+4, avctx->extradata_size) < 0) | |
388 return -1; | |
389 }else{ | |
390 switch(avctx->bits_per_sample&7){ | |
391 case 1: | |
392 s->predictor= LEFT; | |
393 s->decorrelate= 0; | |
394 break; | |
395 case 2: | |
396 s->predictor= LEFT; | |
397 s->decorrelate= 1; | |
398 break; | |
399 case 3: | |
400 s->predictor= PLANE; | |
401 s->decorrelate= avctx->bits_per_sample >= 24; | |
402 break; | |
403 case 4: | |
404 s->predictor= MEDIAN; | |
405 s->decorrelate= 0; | |
406 break; | |
407 default: | |
408 s->predictor= LEFT; //OLD | |
409 s->decorrelate= 0; | |
410 break; | |
411 } | |
412 s->bitstream_bpp= avctx->bits_per_sample & ~7; | |
2369 | 413 s->context= 0; |
866 | 414 |
415 if(read_old_huffman_tables(s) < 0) | |
416 return -1; | |
417 } | |
418 | |
2237
d43321e67acd
(non)interlaced huffyuv patch by (Loren Merritt <lorenm at u dot washington dot edu>)
michael
parents:
2233
diff
changeset
|
419 if(((uint8_t*)avctx->extradata)[2] & 0x20) |
d43321e67acd
(non)interlaced huffyuv patch by (Loren Merritt <lorenm at u dot washington dot edu>)
michael
parents:
2233
diff
changeset
|
420 s->interlaced= ((uint8_t*)avctx->extradata)[2] & 0x10 ? 1 : 0; |
d43321e67acd
(non)interlaced huffyuv patch by (Loren Merritt <lorenm at u dot washington dot edu>)
michael
parents:
2233
diff
changeset
|
421 else |
d43321e67acd
(non)interlaced huffyuv patch by (Loren Merritt <lorenm at u dot washington dot edu>)
michael
parents:
2233
diff
changeset
|
422 s->interlaced= height > 288; |
866 | 423 |
424 switch(s->bitstream_bpp){ | |
425 case 12: | |
426 avctx->pix_fmt = PIX_FMT_YUV420P; | |
427 break; | |
428 case 16: | |
429 if(s->yuy2){ | |
430 avctx->pix_fmt = PIX_FMT_YUV422; | |
431 }else{ | |
432 avctx->pix_fmt = PIX_FMT_YUV422P; | |
433 } | |
434 break; | |
435 case 24: | |
436 case 32: | |
437 if(s->bgr32){ | |
990
165064f921ee
changed BGRA32 to RGBA32. XXX: clarify expected behaviour on big endian cpu
bellard
parents:
925
diff
changeset
|
438 avctx->pix_fmt = PIX_FMT_RGBA32; |
866 | 439 }else{ |
440 avctx->pix_fmt = PIX_FMT_BGR24; | |
441 } | |
442 break; | |
443 default: | |
444 assert(0); | |
445 } | |
446 | |
2190 | 447 // av_log(NULL, AV_LOG_DEBUG, "pred:%d bpp:%d hbpp:%d il:%d\n", s->predictor, s->bitstream_bpp, avctx->bits_per_sample, s->interlaced); |
448 | |
866 | 449 return 0; |
450 } | |
451 | |
2369 | 452 static int store_table(HYuvContext *s, uint8_t *len, uint8_t *buf){ |
866 | 453 int i; |
2369 | 454 int index= 0; |
866 | 455 |
456 for(i=0; i<256;){ | |
457 int val= len[i]; | |
1529
cb523a2ca00f
fix the case where all vlc codes are 8 bits long (repeat=256)
michael
parents:
1528
diff
changeset
|
458 int repeat=0; |
866 | 459 |
1529
cb523a2ca00f
fix the case where all vlc codes are 8 bits long (repeat=256)
michael
parents:
1528
diff
changeset
|
460 for(; i<256 && len[i]==val && repeat<255; i++) |
cb523a2ca00f
fix the case where all vlc codes are 8 bits long (repeat=256)
michael
parents:
1528
diff
changeset
|
461 repeat++; |
866 | 462 |
1529
cb523a2ca00f
fix the case where all vlc codes are 8 bits long (repeat=256)
michael
parents:
1528
diff
changeset
|
463 assert(val < 32 && val >0 && repeat<256 && repeat>0); |
866 | 464 if(repeat>7){ |
2369 | 465 buf[index++]= val; |
466 buf[index++]= repeat; | |
866 | 467 }else{ |
2369 | 468 buf[index++]= val | (repeat<<5); |
866 | 469 } |
470 } | |
471 | |
2369 | 472 return index; |
866 | 473 } |
474 | |
475 static int encode_init(AVCodecContext *avctx) | |
476 { | |
477 HYuvContext *s = avctx->priv_data; | |
478 int i, j, width, height; | |
479 | |
480 s->avctx= avctx; | |
481 s->flags= avctx->flags; | |
482 | |
1097 | 483 dsputil_init(&s->dsp, avctx); |
866 | 484 |
485 width= s->width= avctx->width; | |
486 height= s->height= avctx->height; | |
487 | |
488 assert(width && height); | |
489 | |
1526 | 490 avctx->extradata= av_mallocz(1024*30); |
491 avctx->stats_out= av_mallocz(1024*30); | |
866 | 492 s->version=2; |
493 | |
925 | 494 avctx->coded_frame= &s->picture; |
903 | 495 |
866 | 496 switch(avctx->pix_fmt){ |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
497 case PIX_FMT_YUV420P: |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
498 s->bitstream_bpp= 12; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
499 break; |
866 | 500 case PIX_FMT_YUV422P: |
501 s->bitstream_bpp= 16; | |
502 break; | |
503 default: | |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1529
diff
changeset
|
504 av_log(avctx, AV_LOG_ERROR, "format not supported\n"); |
866 | 505 return -1; |
506 } | |
507 avctx->bits_per_sample= s->bitstream_bpp; | |
508 s->decorrelate= s->bitstream_bpp >= 24; | |
509 s->predictor= avctx->prediction_method; | |
2237
d43321e67acd
(non)interlaced huffyuv patch by (Loren Merritt <lorenm at u dot washington dot edu>)
michael
parents:
2233
diff
changeset
|
510 s->interlaced= avctx->flags&CODEC_FLAG_INTERLACED_ME ? 1 : 0; |
2369 | 511 if(avctx->context_model==1){ |
512 s->context= avctx->context_model; | |
513 if(s->flags & (CODEC_FLAG_PASS1|CODEC_FLAG_PASS2)){ | |
514 av_log(avctx, AV_LOG_ERROR, "context=1 is not compatible with 2 pass huffyuv encoding\n"); | |
515 return -1; | |
516 } | |
517 }else s->context= 0; | |
866 | 518 |
2373 | 519 if(avctx->codec->id==CODEC_ID_HUFFYUV){ |
520 if(avctx->pix_fmt==PIX_FMT_YUV420P){ | |
521 av_log(avctx, AV_LOG_ERROR, "Error: YV12 is not supported by huffyuv; use vcodec=ffvhuff or format=422p\n"); | |
522 return -1; | |
523 } | |
524 if(avctx->context_model){ | |
525 av_log(avctx, AV_LOG_ERROR, "Error: per-frame huffman tables are not supported by huffyuv; use vcodec=ffvhuff\n"); | |
526 return -1; | |
527 } | |
528 if(s->interlaced != ( height > 288 )) | |
529 av_log(avctx, AV_LOG_INFO, "using huffyuv 2.2.0 or newer interlacing flag\n"); | |
530 }else if(avctx->strict_std_compliance>=0){ | |
531 av_log(avctx, AV_LOG_ERROR, "This codec is under development; files encoded with it may not be decodeable with future versions!!! Set vstrict=-1 to use it anyway.\n"); | |
532 return -1; | |
533 } | |
534 | |
866 | 535 ((uint8_t*)avctx->extradata)[0]= s->predictor; |
536 ((uint8_t*)avctx->extradata)[1]= s->bitstream_bpp; | |
2237
d43321e67acd
(non)interlaced huffyuv patch by (Loren Merritt <lorenm at u dot washington dot edu>)
michael
parents:
2233
diff
changeset
|
537 ((uint8_t*)avctx->extradata)[2]= 0x20 | (s->interlaced ? 0x10 : 0); |
2369 | 538 if(s->context) |
539 ((uint8_t*)avctx->extradata)[2]|= 0x40; | |
866 | 540 ((uint8_t*)avctx->extradata)[3]= 0; |
541 s->avctx->extradata_size= 4; | |
542 | |
543 if(avctx->stats_in){ | |
544 char *p= avctx->stats_in; | |
545 | |
546 for(i=0; i<3; i++) | |
547 for(j=0; j<256; j++) | |
548 s->stats[i][j]= 1; | |
549 | |
550 for(;;){ | |
551 for(i=0; i<3; i++){ | |
552 char *next; | |
553 | |
554 for(j=0; j<256; j++){ | |
555 s->stats[i][j]+= strtol(p, &next, 0); | |
556 if(next==p) return -1; | |
557 p=next; | |
558 } | |
559 } | |
560 if(p[0]==0 || p[1]==0 || p[2]==0) break; | |
561 } | |
562 }else{ | |
563 for(i=0; i<3; i++) | |
564 for(j=0; j<256; j++){ | |
565 int d= FFMIN(j, 256-j); | |
566 | |
567 s->stats[i][j]= 100000000/(d+1); | |
568 } | |
569 } | |
570 | |
571 for(i=0; i<3; i++){ | |
572 generate_len_table(s->len[i], s->stats[i], 256); | |
573 | |
574 if(generate_bits_table(s->bits[i], s->len[i])<0){ | |
575 return -1; | |
576 } | |
577 | |
2369 | 578 s->avctx->extradata_size+= |
579 store_table(s, s->len[i], &((uint8_t*)s->avctx->extradata)[s->avctx->extradata_size]); | |
866 | 580 } |
581 | |
2369 | 582 if(s->context){ |
583 for(i=0; i<3; i++){ | |
584 int pels = width*height / (i?40:10); | |
585 for(j=0; j<256; j++){ | |
586 int d= FFMIN(j, 256-j); | |
587 s->stats[i][j]= pels/(d+1); | |
588 } | |
589 } | |
590 }else{ | |
591 for(i=0; i<3; i++) | |
592 for(j=0; j<256; j++) | |
593 s->stats[i][j]= 0; | |
594 } | |
866 | 595 |
596 // printf("pred:%d bpp:%d hbpp:%d il:%d\n", s->predictor, s->bitstream_bpp, avctx->bits_per_sample, s->interlaced); | |
1232
e88d3b1fb2a1
more #ifdef CONFIG_ENCODERS by (Wolfgang Hesseler <qv at multimediaware dot com>)
michaelni
parents:
1228
diff
changeset
|
597 |
866 | 598 s->picture_number=0; |
1232
e88d3b1fb2a1
more #ifdef CONFIG_ENCODERS by (Wolfgang Hesseler <qv at multimediaware dot com>)
michaelni
parents:
1228
diff
changeset
|
599 |
866 | 600 return 0; |
601 } | |
602 | |
603 static void decode_422_bitstream(HYuvContext *s, int count){ | |
604 int i; | |
1232
e88d3b1fb2a1
more #ifdef CONFIG_ENCODERS by (Wolfgang Hesseler <qv at multimediaware dot com>)
michaelni
parents:
1228
diff
changeset
|
605 |
866 | 606 count/=2; |
607 | |
608 for(i=0; i<count; i++){ | |
609 s->temp[0][2*i ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); | |
610 s->temp[1][ i ]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); | |
611 s->temp[0][2*i+1]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); | |
612 s->temp[2][ i ]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); | |
613 } | |
614 } | |
615 | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
616 static void decode_gray_bitstream(HYuvContext *s, int count){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
617 int i; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
618 |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
619 count/=2; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
620 |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
621 for(i=0; i<count; i++){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
622 s->temp[0][2*i ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
623 s->temp[0][2*i+1]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
624 } |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
625 } |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
626 |
866 | 627 static void encode_422_bitstream(HYuvContext *s, int count){ |
628 int i; | |
629 | |
630 count/=2; | |
631 if(s->flags&CODEC_FLAG_PASS1){ | |
632 for(i=0; i<count; i++){ | |
633 s->stats[0][ s->temp[0][2*i ] ]++; | |
634 s->stats[1][ s->temp[1][ i ] ]++; | |
635 s->stats[0][ s->temp[0][2*i+1] ]++; | |
636 s->stats[2][ s->temp[2][ i ] ]++; | |
637 } | |
2369 | 638 }else if(s->context){ |
639 for(i=0; i<count; i++){ | |
640 s->stats[0][ s->temp[0][2*i ] ]++; | |
641 put_bits(&s->pb, s->len[0][ s->temp[0][2*i ] ], s->bits[0][ s->temp[0][2*i ] ]); | |
642 s->stats[1][ s->temp[1][ i ] ]++; | |
643 put_bits(&s->pb, s->len[1][ s->temp[1][ i ] ], s->bits[1][ s->temp[1][ i ] ]); | |
644 s->stats[0][ s->temp[0][2*i+1] ]++; | |
645 put_bits(&s->pb, s->len[0][ s->temp[0][2*i+1] ], s->bits[0][ s->temp[0][2*i+1] ]); | |
646 s->stats[2][ s->temp[2][ i ] ]++; | |
647 put_bits(&s->pb, s->len[2][ s->temp[2][ i ] ], s->bits[2][ s->temp[2][ i ] ]); | |
648 } | |
866 | 649 }else{ |
650 for(i=0; i<count; i++){ | |
651 put_bits(&s->pb, s->len[0][ s->temp[0][2*i ] ], s->bits[0][ s->temp[0][2*i ] ]); | |
652 put_bits(&s->pb, s->len[1][ s->temp[1][ i ] ], s->bits[1][ s->temp[1][ i ] ]); | |
653 put_bits(&s->pb, s->len[0][ s->temp[0][2*i+1] ], s->bits[0][ s->temp[0][2*i+1] ]); | |
654 put_bits(&s->pb, s->len[2][ s->temp[2][ i ] ], s->bits[2][ s->temp[2][ i ] ]); | |
655 } | |
656 } | |
657 } | |
658 | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
659 static void encode_gray_bitstream(HYuvContext *s, int count){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
660 int i; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
661 |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
662 count/=2; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
663 if(s->flags&CODEC_FLAG_PASS1){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
664 for(i=0; i<count; i++){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
665 s->stats[0][ s->temp[0][2*i ] ]++; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
666 s->stats[0][ s->temp[0][2*i+1] ]++; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
667 } |
2369 | 668 }else if(s->context){ |
669 for(i=0; i<count; i++){ | |
670 s->stats[0][ s->temp[0][2*i ] ]++; | |
671 put_bits(&s->pb, s->len[0][ s->temp[0][2*i ] ], s->bits[0][ s->temp[0][2*i ] ]); | |
672 s->stats[0][ s->temp[0][2*i+1] ]++; | |
673 put_bits(&s->pb, s->len[0][ s->temp[0][2*i+1] ], s->bits[0][ s->temp[0][2*i+1] ]); | |
674 } | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
675 }else{ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
676 for(i=0; i<count; i++){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
677 put_bits(&s->pb, s->len[0][ s->temp[0][2*i ] ], s->bits[0][ s->temp[0][2*i ] ]); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
678 put_bits(&s->pb, s->len[0][ s->temp[0][2*i+1] ], s->bits[0][ s->temp[0][2*i+1] ]); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
679 } |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
680 } |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
681 } |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
682 |
866 | 683 static void decode_bgr_bitstream(HYuvContext *s, int count){ |
684 int i; | |
1232
e88d3b1fb2a1
more #ifdef CONFIG_ENCODERS by (Wolfgang Hesseler <qv at multimediaware dot com>)
michaelni
parents:
1228
diff
changeset
|
685 |
866 | 686 if(s->decorrelate){ |
687 if(s->bitstream_bpp==24){ | |
688 for(i=0; i<count; i++){ | |
2177 | 689 s->temp[0][4*i+G]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); |
690 s->temp[0][4*i+B]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3) + s->temp[0][4*i+G]; | |
691 s->temp[0][4*i+R]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3) + s->temp[0][4*i+G]; | |
866 | 692 } |
693 }else{ | |
694 for(i=0; i<count; i++){ | |
2176 | 695 s->temp[0][4*i+G]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); |
696 s->temp[0][4*i+B]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3) + s->temp[0][4*i+G]; | |
697 s->temp[0][4*i+R]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3) + s->temp[0][4*i+G]; | |
866 | 698 get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); //?! |
699 } | |
700 } | |
701 }else{ | |
702 if(s->bitstream_bpp==24){ | |
703 for(i=0; i<count; i++){ | |
2177 | 704 s->temp[0][4*i+B]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); |
705 s->temp[0][4*i+G]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); | |
706 s->temp[0][4*i+R]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); | |
866 | 707 } |
708 }else{ | |
709 for(i=0; i<count; i++){ | |
2176 | 710 s->temp[0][4*i+B]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); |
711 s->temp[0][4*i+G]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); | |
712 s->temp[0][4*i+R]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); | |
866 | 713 get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); //?! |
714 } | |
715 } | |
716 } | |
717 } | |
718 | |
871 | 719 static void draw_slice(HYuvContext *s, int y){ |
720 int h, cy; | |
1368 | 721 int offset[4]; |
871 | 722 |
723 if(s->avctx->draw_horiz_band==NULL) | |
724 return; | |
725 | |
726 h= y - s->last_slice_end; | |
727 y -= h; | |
728 | |
729 if(s->bitstream_bpp==12){ | |
730 cy= y>>1; | |
731 }else{ | |
732 cy= y; | |
733 } | |
1368 | 734 |
735 offset[0] = s->picture.linesize[0]*y; | |
736 offset[1] = s->picture.linesize[1]*cy; | |
737 offset[2] = s->picture.linesize[2]*cy; | |
738 offset[3] = 0; | |
871 | 739 emms_c(); |
740 | |
1370 | 741 s->avctx->draw_horiz_band(s->avctx, &s->picture, offset, y, 3, h); |
871 | 742 |
743 s->last_slice_end= y + h; | |
744 } | |
745 | |
866 | 746 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){ |
747 HYuvContext *s = avctx->priv_data; | |
748 const int width= s->width; | |
749 const int width2= s->width>>1; | |
750 const int height= s->height; | |
870 | 751 int fake_ystride, fake_ustride, fake_vstride; |
925 | 752 AVFrame * const p= &s->picture; |
2369 | 753 int table_size= 0; |
866 | 754 |
925 | 755 AVFrame *picture = data; |
866 | 756 |
757 /* no supplementary picture */ | |
758 if (buf_size == 0) | |
759 return 0; | |
760 | |
1273 | 761 s->dsp.bswap_buf((uint32_t*)s->bitstream_buffer, (uint32_t*)buf, buf_size/4); |
866 | 762 |
1228 | 763 if(p->data[0]) |
764 avctx->release_buffer(avctx, p); | |
765 | |
903 | 766 p->reference= 0; |
767 if(avctx->get_buffer(avctx, p) < 0){ | |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1529
diff
changeset
|
768 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); |
903 | 769 return -1; |
770 } | |
2369 | 771 |
772 if(s->context){ | |
773 table_size = read_huffman_tables(s, s->bitstream_buffer, buf_size); | |
774 if(table_size < 0) | |
775 return -1; | |
776 } | |
777 | |
778 init_get_bits(&s->gb, s->bitstream_buffer+table_size, (buf_size-table_size)*8); | |
870 | 779 |
903 | 780 fake_ystride= s->interlaced ? p->linesize[0]*2 : p->linesize[0]; |
781 fake_ustride= s->interlaced ? p->linesize[1]*2 : p->linesize[1]; | |
782 fake_vstride= s->interlaced ? p->linesize[2]*2 : p->linesize[2]; | |
871 | 783 |
784 s->last_slice_end= 0; | |
870 | 785 |
866 | 786 if(s->bitstream_bpp<24){ |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
787 int y, cy; |
866 | 788 int lefty, leftu, leftv; |
789 int lefttopy, lefttopu, lefttopv; | |
790 | |
791 if(s->yuy2){ | |
903 | 792 p->data[0][3]= get_bits(&s->gb, 8); |
793 p->data[0][2]= get_bits(&s->gb, 8); | |
794 p->data[0][1]= get_bits(&s->gb, 8); | |
795 p->data[0][0]= get_bits(&s->gb, 8); | |
866 | 796 |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1529
diff
changeset
|
797 av_log(avctx, AV_LOG_ERROR, "YUY2 output isnt implemenetd yet\n"); |
866 | 798 return -1; |
799 }else{ | |
800 | |
903 | 801 leftv= p->data[2][0]= get_bits(&s->gb, 8); |
802 lefty= p->data[0][1]= get_bits(&s->gb, 8); | |
803 leftu= p->data[1][0]= get_bits(&s->gb, 8); | |
804 p->data[0][0]= get_bits(&s->gb, 8); | |
866 | 805 |
806 switch(s->predictor){ | |
807 case LEFT: | |
808 case PLANE: | |
809 decode_422_bitstream(s, width-2); | |
903 | 810 lefty= add_left_prediction(p->data[0] + 2, s->temp[0], width-2, lefty); |
866 | 811 if(!(s->flags&CODEC_FLAG_GRAY)){ |
903 | 812 leftu= add_left_prediction(p->data[1] + 1, s->temp[1], width2-1, leftu); |
813 leftv= add_left_prediction(p->data[2] + 1, s->temp[2], width2-1, leftv); | |
866 | 814 } |
815 | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
816 for(cy=y=1; y<s->height; y++,cy++){ |
866 | 817 uint8_t *ydst, *udst, *vdst; |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
818 |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
819 if(s->bitstream_bpp==12){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
820 decode_gray_bitstream(s, width); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
821 |
903 | 822 ydst= p->data[0] + p->linesize[0]*y; |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
823 |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
824 lefty= add_left_prediction(ydst, s->temp[0], width, lefty); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
825 if(s->predictor == PLANE){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
826 if(y>s->interlaced) |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
827 s->dsp.add_bytes(ydst, ydst - fake_ystride, width); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
828 } |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
829 y++; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
830 if(y>=s->height) break; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
831 } |
866 | 832 |
871 | 833 draw_slice(s, y); |
834 | |
903 | 835 ydst= p->data[0] + p->linesize[0]*y; |
836 udst= p->data[1] + p->linesize[1]*cy; | |
837 vdst= p->data[2] + p->linesize[2]*cy; | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
838 |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
839 decode_422_bitstream(s, width); |
866 | 840 lefty= add_left_prediction(ydst, s->temp[0], width, lefty); |
841 if(!(s->flags&CODEC_FLAG_GRAY)){ | |
842 leftu= add_left_prediction(udst, s->temp[1], width2, leftu); | |
843 leftv= add_left_prediction(vdst, s->temp[2], width2, leftv); | |
844 } | |
845 if(s->predictor == PLANE){ | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
846 if(cy>s->interlaced){ |
866 | 847 s->dsp.add_bytes(ydst, ydst - fake_ystride, width); |
848 if(!(s->flags&CODEC_FLAG_GRAY)){ | |
849 s->dsp.add_bytes(udst, udst - fake_ustride, width2); | |
850 s->dsp.add_bytes(vdst, vdst - fake_vstride, width2); | |
851 } | |
852 } | |
853 } | |
854 } | |
871 | 855 draw_slice(s, height); |
856 | |
866 | 857 break; |
858 case MEDIAN: | |
859 /* first line except first 2 pixels is left predicted */ | |
860 decode_422_bitstream(s, width-2); | |
903 | 861 lefty= add_left_prediction(p->data[0] + 2, s->temp[0], width-2, lefty); |
866 | 862 if(!(s->flags&CODEC_FLAG_GRAY)){ |
903 | 863 leftu= add_left_prediction(p->data[1] + 1, s->temp[1], width2-1, leftu); |
864 leftv= add_left_prediction(p->data[2] + 1, s->temp[2], width2-1, leftv); | |
866 | 865 } |
866 | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
867 cy=y=1; |
866 | 868 |
869 /* second line is left predicted for interlaced case */ | |
870 if(s->interlaced){ | |
871 decode_422_bitstream(s, width); | |
903 | 872 lefty= add_left_prediction(p->data[0] + p->linesize[0], s->temp[0], width, lefty); |
866 | 873 if(!(s->flags&CODEC_FLAG_GRAY)){ |
903 | 874 leftu= add_left_prediction(p->data[1] + p->linesize[2], s->temp[1], width2, leftu); |
875 leftv= add_left_prediction(p->data[2] + p->linesize[1], s->temp[2], width2, leftv); | |
866 | 876 } |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
877 y++; cy++; |
866 | 878 } |
879 | |
880 /* next 4 pixels are left predicted too */ | |
881 decode_422_bitstream(s, 4); | |
903 | 882 lefty= add_left_prediction(p->data[0] + fake_ystride, s->temp[0], 4, lefty); |
866 | 883 if(!(s->flags&CODEC_FLAG_GRAY)){ |
903 | 884 leftu= add_left_prediction(p->data[1] + fake_ustride, s->temp[1], 2, leftu); |
885 leftv= add_left_prediction(p->data[2] + fake_vstride, s->temp[2], 2, leftv); | |
866 | 886 } |
887 | |
888 /* next line except the first 4 pixels is median predicted */ | |
903 | 889 lefttopy= p->data[0][3]; |
866 | 890 decode_422_bitstream(s, width-4); |
903 | 891 add_median_prediction(p->data[0] + fake_ystride+4, p->data[0]+4, s->temp[0], width-4, &lefty, &lefttopy); |
866 | 892 if(!(s->flags&CODEC_FLAG_GRAY)){ |
903 | 893 lefttopu= p->data[1][1]; |
894 lefttopv= p->data[2][1]; | |
895 add_median_prediction(p->data[1] + fake_ustride+2, p->data[1]+2, s->temp[1], width2-2, &leftu, &lefttopu); | |
896 add_median_prediction(p->data[2] + fake_vstride+2, p->data[2]+2, s->temp[2], width2-2, &leftv, &lefttopv); | |
866 | 897 } |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
898 y++; cy++; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
899 |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
900 for(; y<height; y++,cy++){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
901 uint8_t *ydst, *udst, *vdst; |
866 | 902 |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
903 if(s->bitstream_bpp==12){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
904 while(2*cy > y){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
905 decode_gray_bitstream(s, width); |
903 | 906 ydst= p->data[0] + p->linesize[0]*y; |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
907 add_median_prediction(ydst, ydst - fake_ystride, s->temp[0], width, &lefty, &lefttopy); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
908 y++; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
909 } |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
910 if(y>=height) break; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
911 } |
871 | 912 draw_slice(s, y); |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
913 |
866 | 914 decode_422_bitstream(s, width); |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
915 |
903 | 916 ydst= p->data[0] + p->linesize[0]*y; |
917 udst= p->data[1] + p->linesize[1]*cy; | |
918 vdst= p->data[2] + p->linesize[2]*cy; | |
866 | 919 |
920 add_median_prediction(ydst, ydst - fake_ystride, s->temp[0], width, &lefty, &lefttopy); | |
921 if(!(s->flags&CODEC_FLAG_GRAY)){ | |
922 add_median_prediction(udst, udst - fake_ustride, s->temp[1], width2, &leftu, &lefttopu); | |
923 add_median_prediction(vdst, vdst - fake_vstride, s->temp[2], width2, &leftv, &lefttopv); | |
924 } | |
925 } | |
871 | 926 |
927 draw_slice(s, height); | |
866 | 928 break; |
929 } | |
930 } | |
931 }else{ | |
932 int y; | |
933 int leftr, leftg, leftb; | |
903 | 934 const int last_line= (height-1)*p->linesize[0]; |
866 | 935 |
936 if(s->bitstream_bpp==32){ | |
2177 | 937 skip_bits(&s->gb, 8); |
938 leftr= p->data[0][last_line+R]= get_bits(&s->gb, 8); | |
939 leftg= p->data[0][last_line+G]= get_bits(&s->gb, 8); | |
940 leftb= p->data[0][last_line+B]= get_bits(&s->gb, 8); | |
866 | 941 }else{ |
2177 | 942 leftr= p->data[0][last_line+R]= get_bits(&s->gb, 8); |
943 leftg= p->data[0][last_line+G]= get_bits(&s->gb, 8); | |
944 leftb= p->data[0][last_line+B]= get_bits(&s->gb, 8); | |
866 | 945 skip_bits(&s->gb, 8); |
946 } | |
947 | |
948 if(s->bgr32){ | |
949 switch(s->predictor){ | |
950 case LEFT: | |
951 case PLANE: | |
952 decode_bgr_bitstream(s, width-1); | |
903 | 953 add_left_prediction_bgr32(p->data[0] + last_line+4, s->temp[0], width-1, &leftr, &leftg, &leftb); |
866 | 954 |
955 for(y=s->height-2; y>=0; y--){ //yes its stored upside down | |
956 decode_bgr_bitstream(s, width); | |
957 | |
903 | 958 add_left_prediction_bgr32(p->data[0] + p->linesize[0]*y, s->temp[0], width, &leftr, &leftg, &leftb); |
866 | 959 if(s->predictor == PLANE){ |
2351 | 960 if((y&s->interlaced)==0 && y<s->height-1-s->interlaced){ |
903 | 961 s->dsp.add_bytes(p->data[0] + p->linesize[0]*y, |
962 p->data[0] + p->linesize[0]*y + fake_ystride, fake_ystride); | |
866 | 963 } |
964 } | |
965 } | |
871 | 966 draw_slice(s, height); // just 1 large slice as this isnt possible in reverse order |
866 | 967 break; |
968 default: | |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1529
diff
changeset
|
969 av_log(avctx, AV_LOG_ERROR, "prediction type not supported!\n"); |
866 | 970 } |
971 }else{ | |
972 | |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1529
diff
changeset
|
973 av_log(avctx, AV_LOG_ERROR, "BGR24 output isnt implemenetd yet\n"); |
866 | 974 return -1; |
975 } | |
976 } | |
977 emms_c(); | |
978 | |
903 | 979 *picture= *p; |
925 | 980 *data_size = sizeof(AVFrame); |
866 | 981 |
1078
c55bffc3b84e
round readed bits up to next 32bits, as orginal huffyuv cant handle less then 32bit blocks
michaelni
parents:
1064
diff
changeset
|
982 return (get_bits_count(&s->gb)+31)/32*4; |
866 | 983 } |
984 | |
985 static int decode_end(AVCodecContext *avctx) | |
986 { | |
987 HYuvContext *s = avctx->priv_data; | |
988 int i; | |
989 | |
990 for(i=0; i<3; i++){ | |
903 | 991 free_vlc(&s->vlc[i]); |
992 } | |
866 | 993 |
994 return 0; | |
995 } | |
996 | |
997 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){ | |
998 HYuvContext *s = avctx->priv_data; | |
925 | 999 AVFrame *pict = data; |
866 | 1000 const int width= s->width; |
1001 const int width2= s->width>>1; | |
1002 const int height= s->height; | |
1003 const int fake_ystride= s->interlaced ? pict->linesize[0]*2 : pict->linesize[0]; | |
1004 const int fake_ustride= s->interlaced ? pict->linesize[1]*2 : pict->linesize[1]; | |
1005 const int fake_vstride= s->interlaced ? pict->linesize[2]*2 : pict->linesize[2]; | |
925 | 1006 AVFrame * const p= &s->picture; |
2369 | 1007 int i, j, size=0; |
866 | 1008 |
903 | 1009 *p = *pict; |
1006 | 1010 p->pict_type= FF_I_TYPE; |
1011 p->key_frame= 1; | |
866 | 1012 |
2369 | 1013 if(s->context){ |
1014 for(i=0; i<3; i++){ | |
1015 generate_len_table(s->len[i], s->stats[i], 256); | |
1016 if(generate_bits_table(s->bits[i], s->len[i])<0) | |
1017 return -1; | |
1018 size+= store_table(s, s->len[i], &buf[size]); | |
1019 } | |
1020 | |
1021 for(i=0; i<3; i++) | |
1022 for(j=0; j<256; j++) | |
1023 s->stats[i][j] >>= 1; | |
1024 } | |
1025 | |
1026 init_put_bits(&s->pb, buf+size, buf_size-size); | |
1027 | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1028 if(avctx->pix_fmt == PIX_FMT_YUV422P || avctx->pix_fmt == PIX_FMT_YUV420P){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1029 int lefty, leftu, leftv, y, cy; |
866 | 1030 |
903 | 1031 put_bits(&s->pb, 8, leftv= p->data[2][0]); |
1032 put_bits(&s->pb, 8, lefty= p->data[0][1]); | |
1033 put_bits(&s->pb, 8, leftu= p->data[1][0]); | |
1034 put_bits(&s->pb, 8, p->data[0][0]); | |
866 | 1035 |
903 | 1036 lefty= sub_left_prediction(s, s->temp[0], p->data[0]+2, width-2 , lefty); |
1037 leftu= sub_left_prediction(s, s->temp[1], p->data[1]+1, width2-1, leftu); | |
1038 leftv= sub_left_prediction(s, s->temp[2], p->data[2]+1, width2-1, leftv); | |
866 | 1039 |
1040 encode_422_bitstream(s, width-2); | |
1041 | |
1042 if(s->predictor==MEDIAN){ | |
1043 int lefttopy, lefttopu, lefttopv; | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1044 cy=y=1; |
866 | 1045 if(s->interlaced){ |
903 | 1046 lefty= sub_left_prediction(s, s->temp[0], p->data[0]+p->linesize[0], width , lefty); |
1047 leftu= sub_left_prediction(s, s->temp[1], p->data[1]+p->linesize[1], width2, leftu); | |
1048 leftv= sub_left_prediction(s, s->temp[2], p->data[2]+p->linesize[2], width2, leftv); | |
866 | 1049 |
1050 encode_422_bitstream(s, width); | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1051 y++; cy++; |
866 | 1052 } |
1053 | |
903 | 1054 lefty= sub_left_prediction(s, s->temp[0], p->data[0]+fake_ystride, 4, lefty); |
2190 | 1055 leftu= sub_left_prediction(s, s->temp[1], p->data[1]+fake_ustride, 2, leftu); |
1056 leftv= sub_left_prediction(s, s->temp[2], p->data[2]+fake_vstride, 2, leftv); | |
866 | 1057 |
1058 encode_422_bitstream(s, 4); | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1059 |
903 | 1060 lefttopy= p->data[0][3]; |
1061 lefttopu= p->data[1][1]; | |
1062 lefttopv= p->data[2][1]; | |
1527 | 1063 s->dsp.sub_hfyu_median_prediction(s->temp[0], p->data[0]+4, p->data[0] + fake_ystride+4, width-4 , &lefty, &lefttopy); |
1064 s->dsp.sub_hfyu_median_prediction(s->temp[1], p->data[1]+2, p->data[1] + fake_ustride+2, width2-2, &leftu, &lefttopu); | |
1065 s->dsp.sub_hfyu_median_prediction(s->temp[2], p->data[2]+2, p->data[2] + fake_vstride+2, width2-2, &leftv, &lefttopv); | |
866 | 1066 encode_422_bitstream(s, width-4); |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1067 y++; cy++; |
866 | 1068 |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1069 for(; y<height; y++,cy++){ |
866 | 1070 uint8_t *ydst, *udst, *vdst; |
1071 | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1072 if(s->bitstream_bpp==12){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1073 while(2*cy > y){ |
903 | 1074 ydst= p->data[0] + p->linesize[0]*y; |
1527 | 1075 s->dsp.sub_hfyu_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy); |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1076 encode_gray_bitstream(s, width); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1077 y++; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1078 } |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1079 if(y>=height) break; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1080 } |
903 | 1081 ydst= p->data[0] + p->linesize[0]*y; |
1082 udst= p->data[1] + p->linesize[1]*cy; | |
1083 vdst= p->data[2] + p->linesize[2]*cy; | |
866 | 1084 |
1527 | 1085 s->dsp.sub_hfyu_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy); |
1086 s->dsp.sub_hfyu_median_prediction(s->temp[1], udst - fake_ustride, udst, width2, &leftu, &lefttopu); | |
1087 s->dsp.sub_hfyu_median_prediction(s->temp[2], vdst - fake_vstride, vdst, width2, &leftv, &lefttopv); | |
866 | 1088 |
1089 encode_422_bitstream(s, width); | |
1090 } | |
1091 }else{ | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1092 for(cy=y=1; y<height; y++,cy++){ |
866 | 1093 uint8_t *ydst, *udst, *vdst; |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1094 |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1095 /* encode a luma only line & y++ */ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1096 if(s->bitstream_bpp==12){ |
903 | 1097 ydst= p->data[0] + p->linesize[0]*y; |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1098 |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1099 if(s->predictor == PLANE && s->interlaced < y){ |
871 | 1100 s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width); |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1101 |
871 | 1102 lefty= sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty); |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1103 }else{ |
871 | 1104 lefty= sub_left_prediction(s, s->temp[0], ydst, width , lefty); |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1105 } |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1106 encode_gray_bitstream(s, width); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1107 y++; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1108 if(y>=height) break; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1109 } |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1110 |
903 | 1111 ydst= p->data[0] + p->linesize[0]*y; |
1112 udst= p->data[1] + p->linesize[1]*cy; | |
1113 vdst= p->data[2] + p->linesize[2]*cy; | |
866 | 1114 |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1115 if(s->predictor == PLANE && s->interlaced < cy){ |
871 | 1116 s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width); |
1117 s->dsp.diff_bytes(s->temp[2], udst, udst - fake_ustride, width2); | |
1526 | 1118 s->dsp.diff_bytes(s->temp[2] + 1250, vdst, vdst - fake_vstride, width2); |
866 | 1119 |
871 | 1120 lefty= sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty); |
1121 leftu= sub_left_prediction(s, s->temp[1], s->temp[2], width2, leftu); | |
1526 | 1122 leftv= sub_left_prediction(s, s->temp[2], s->temp[2] + 1250, width2, leftv); |
866 | 1123 }else{ |
871 | 1124 lefty= sub_left_prediction(s, s->temp[0], ydst, width , lefty); |
1125 leftu= sub_left_prediction(s, s->temp[1], udst, width2, leftu); | |
1126 leftv= sub_left_prediction(s, s->temp[2], vdst, width2, leftv); | |
866 | 1127 } |
1128 | |
1129 encode_422_bitstream(s, width); | |
1130 } | |
1131 } | |
1132 }else{ | |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1529
diff
changeset
|
1133 av_log(avctx, AV_LOG_ERROR, "Format not supported!\n"); |
866 | 1134 } |
1135 emms_c(); | |
1136 | |
2369 | 1137 size+= (put_bits_count(&s->pb)+31)/8; |
1138 size/= 4; | |
866 | 1139 |
1140 if((s->flags&CODEC_FLAG_PASS1) && (s->picture_number&31)==0){ | |
1141 int j; | |
1142 char *p= avctx->stats_out; | |
1143 for(i=0; i<3; i++){ | |
1144 for(j=0; j<256; j++){ | |
1282 | 1145 sprintf(p, "%llu ", s->stats[i][j]); |
866 | 1146 p+= strlen(p); |
1147 s->stats[i][j]= 0; | |
1148 } | |
1149 sprintf(p, "\n"); | |
1150 p++; | |
1151 } | |
1152 }else{ | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1153 flush_put_bits(&s->pb); |
1273 | 1154 s->dsp.bswap_buf((uint32_t*)buf, (uint32_t*)buf, size); |
2239
506fdbb9d19c
huffyuv writes to AVCodecContext.stats_out only once every 32 frames,
michael
parents:
2238
diff
changeset
|
1155 avctx->stats_out[0] = '\0'; |
866 | 1156 } |
1157 | |
1158 s->picture_number++; | |
903 | 1159 |
866 | 1160 return size*4; |
1161 } | |
1162 | |
1163 static int encode_end(AVCodecContext *avctx) | |
1164 { | |
1165 // HYuvContext *s = avctx->priv_data; | |
1166 | |
1167 av_freep(&avctx->extradata); | |
1168 av_freep(&avctx->stats_out); | |
1169 | |
1170 return 0; | |
1171 } | |
1172 | |
1130 | 1173 static const AVOption huffyuv_options[] = |
1174 { | |
1175 AVOPTION_CODEC_INT("prediction_method", "prediction_method", prediction_method, 0, 2, 0), | |
1176 AVOPTION_END() | |
1177 }; | |
1178 | |
2373 | 1179 static const AVOption ffvhuff_options[] = |
1180 { | |
1181 AVOPTION_CODEC_INT("prediction_method", "prediction_method", prediction_method, 0, 2, 0), | |
1182 AVOPTION_CODEC_INT("context_model", "context_model", context_model, 0, 2, 0), | |
1183 AVOPTION_END() | |
1184 }; | |
1185 | |
1186 | |
866 | 1187 AVCodec huffyuv_decoder = { |
1188 "huffyuv", | |
1189 CODEC_TYPE_VIDEO, | |
1190 CODEC_ID_HUFFYUV, | |
1191 sizeof(HYuvContext), | |
1192 decode_init, | |
1193 NULL, | |
1194 decode_end, | |
1195 decode_frame, | |
871 | 1196 CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND, |
866 | 1197 NULL |
1198 }; | |
1199 | |
2373 | 1200 AVCodec ffvhuff_decoder = { |
1201 "ffvhuff", | |
1202 CODEC_TYPE_VIDEO, | |
1203 CODEC_ID_FFVHUFF, | |
1204 sizeof(HYuvContext), | |
1205 decode_init, | |
1206 NULL, | |
1207 decode_end, | |
1208 decode_frame, | |
1209 CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND, | |
1210 NULL | |
1211 }; | |
1212 | |
1232
e88d3b1fb2a1
more #ifdef CONFIG_ENCODERS by (Wolfgang Hesseler <qv at multimediaware dot com>)
michaelni
parents:
1228
diff
changeset
|
1213 #ifdef CONFIG_ENCODERS |
e88d3b1fb2a1
more #ifdef CONFIG_ENCODERS by (Wolfgang Hesseler <qv at multimediaware dot com>)
michaelni
parents:
1228
diff
changeset
|
1214 |
866 | 1215 AVCodec huffyuv_encoder = { |
1216 "huffyuv", | |
1217 CODEC_TYPE_VIDEO, | |
1218 CODEC_ID_HUFFYUV, | |
1219 sizeof(HYuvContext), | |
1220 encode_init, | |
1221 encode_frame, | |
1222 encode_end, | |
1130 | 1223 .options = huffyuv_options, |
866 | 1224 }; |
1232
e88d3b1fb2a1
more #ifdef CONFIG_ENCODERS by (Wolfgang Hesseler <qv at multimediaware dot com>)
michaelni
parents:
1228
diff
changeset
|
1225 |
2373 | 1226 AVCodec ffvhuff_encoder = { |
1227 "ffvhuff", | |
1228 CODEC_TYPE_VIDEO, | |
1229 CODEC_ID_FFVHUFF, | |
1230 sizeof(HYuvContext), | |
1231 encode_init, | |
1232 encode_frame, | |
1233 encode_end, | |
1234 .options = ffvhuff_options, | |
1235 }; | |
1236 | |
1246 | 1237 #endif //CONFIG_ENCODERS |