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