lavc: use designated initialisers for all codecs.
[libav.git] / libavcodec / huffyuv.c
CommitLineData
11f18faf
MN
1/*
2 * huffyuv codec for libavcodec
3 *
aaa1e4cd 4 * Copyright (c) 2002-2003 Michael Niedermayer <michaelni@gmx.at>
11f18faf 5 *
7b94177e
DB
6 * see http://www.pcisys.net/~melanson/codecs/huffyuv.txt for a description of
7 * the algorithm used
8 *
2912e87a 9 * This file is part of Libav.
b78e7197 10 *
2912e87a 11 * Libav is free software; you can redistribute it and/or
11f18faf
MN
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
b78e7197 14 * version 2.1 of the License, or (at your option) any later version.
11f18faf 15 *
2912e87a 16 * Libav is distributed in the hope that it will be useful,
11f18faf
MN
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
2912e87a 22 * License along with Libav; if not, write to the Free Software
5509bffa 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
11f18faf 24 */
115329f1 25
983e3246 26/**
ba87f080 27 * @file
983e3246
MN
28 * huffyuv codec for libavcodec.
29 */
11f18faf 30
11f18faf 31#include "avcodec.h"
9106a698 32#include "get_bits.h"
b2755007 33#include "put_bits.h"
11f18faf 34#include "dsputil.h"
76d8846c 35#include "thread.h"
11f18faf 36
11f18faf 37#define VLC_BITS 11
1e491e29 38
63613fe6 39#if HAVE_BIGENDIAN
3ad7dd8c
MN
40#define B 3
41#define G 2
42#define R 1
f267d3ac 43#define A 0
3ad7dd8c
MN
44#else
45#define B 0
46#define G 1
47#define R 2
f267d3ac 48#define A 3
3ad7dd8c
MN
49#endif
50
11f18faf
MN
51typedef enum Predictor{
52 LEFT= 0,
53 PLANE,
54 MEDIAN,
55} Predictor;
115329f1 56
11f18faf
MN
57typedef struct HYuvContext{
58 AVCodecContext *avctx;
59 Predictor predictor;
60 GetBitContext gb;
61 PutBitContext pb;
62 int interlaced;
63 int decorrelate;
64 int bitstream_bpp;
65 int version;
66 int yuy2; //use yuy2 instead of 422P
67 int bgr32; //use bgr32 instead of bgr24
68 int width, height;
69 int flags;
11087086 70 int context;
11f18faf 71 int picture_number;
7c5ab7b8 72 int last_slice_end;
0ecca7a4 73 uint8_t *temp[3];
11f18faf
MN
74 uint64_t stats[3][256];
75 uint8_t len[3][256];
76 uint32_t bits[3][256];
8be6dac8 77 uint32_t pix_bgr_map[1<<VLC_BITS];
19716d64 78 VLC vlc[6]; //Y,U,V,YY,YU,YV
492cd3a9 79 AVFrame picture;
0ecca7a4 80 uint8_t *bitstream_buffer;
f038fe8b 81 unsigned int bitstream_buffer_size;
115329f1 82 DSPContext dsp;
11f18faf
MN
83}HYuvContext;
84
24def10e 85static const unsigned char classic_shift_luma[] = {
676b385c
MN
86 34,36,35,69,135,232,9,16,10,24,11,23,12,16,13,10,14,8,15,8,
87 16,8,17,20,16,10,207,206,205,236,11,8,10,21,9,23,8,8,199,70,
88 69,68, 0
89};
90
24def10e 91static const unsigned char classic_shift_chroma[] = {
676b385c
MN
92 66,36,37,38,39,40,41,75,76,77,110,239,144,81,82,83,84,85,118,183,
93 56,57,88,89,56,89,154,57,58,57,26,141,57,56,58,57,58,57,184,119,
94 214,245,116,83,82,49,80,79,78,77,44,75,41,40,39,38,37,36,34, 0
95};
96
24def10e 97static const unsigned char classic_add_luma[256] = {
676b385c
MN
98 3, 9, 5, 12, 10, 35, 32, 29, 27, 50, 48, 45, 44, 41, 39, 37,
99 73, 70, 68, 65, 64, 61, 58, 56, 53, 50, 49, 46, 44, 41, 38, 36,
100 68, 65, 63, 61, 58, 55, 53, 51, 48, 46, 45, 43, 41, 39, 38, 36,
101 35, 33, 32, 30, 29, 27, 26, 25, 48, 47, 46, 44, 43, 41, 40, 39,
102 37, 36, 35, 34, 32, 31, 30, 28, 27, 26, 24, 23, 22, 20, 19, 37,
103 35, 34, 33, 31, 30, 29, 27, 26, 24, 23, 21, 20, 18, 17, 15, 29,
104 27, 26, 24, 22, 21, 19, 17, 16, 14, 26, 25, 23, 21, 19, 18, 16,
105 15, 27, 25, 23, 21, 19, 17, 16, 14, 26, 25, 23, 21, 18, 17, 14,
106 12, 17, 19, 13, 4, 9, 2, 11, 1, 7, 8, 0, 16, 3, 14, 6,
107 12, 10, 5, 15, 18, 11, 10, 13, 15, 16, 19, 20, 22, 24, 27, 15,
108 18, 20, 22, 24, 26, 14, 17, 20, 22, 24, 27, 15, 18, 20, 23, 25,
109 28, 16, 19, 22, 25, 28, 32, 36, 21, 25, 29, 33, 38, 42, 45, 49,
110 28, 31, 34, 37, 40, 42, 44, 47, 49, 50, 52, 54, 56, 57, 59, 60,
111 62, 64, 66, 67, 69, 35, 37, 39, 40, 42, 43, 45, 47, 48, 51, 52,
112 54, 55, 57, 59, 60, 62, 63, 66, 67, 69, 71, 72, 38, 40, 42, 43,
113 46, 47, 49, 51, 26, 28, 30, 31, 33, 34, 18, 19, 11, 13, 7, 8,
114};
115
24def10e 116static const unsigned char classic_add_chroma[256] = {
676b385c
MN
117 3, 1, 2, 2, 2, 2, 3, 3, 7, 5, 7, 5, 8, 6, 11, 9,
118 7, 13, 11, 10, 9, 8, 7, 5, 9, 7, 6, 4, 7, 5, 8, 7,
119 11, 8, 13, 11, 19, 15, 22, 23, 20, 33, 32, 28, 27, 29, 51, 77,
120 43, 45, 76, 81, 46, 82, 75, 55, 56,144, 58, 80, 60, 74,147, 63,
121 143, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
122 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 27, 30, 21, 22,
123 17, 14, 5, 6,100, 54, 47, 50, 51, 53,106,107,108,109,110,111,
124 112,113,114,115, 4,117,118, 92, 94,121,122, 3,124,103, 2, 1,
125 0,129,130,131,120,119,126,125,136,137,138,139,140,141,142,134,
126 135,132,133,104, 64,101, 62, 57,102, 95, 93, 59, 61, 28, 97, 96,
127 52, 49, 48, 29, 32, 25, 24, 46, 23, 98, 45, 44, 43, 20, 42, 41,
128 19, 18, 99, 40, 15, 39, 38, 16, 13, 12, 11, 37, 10, 9, 8, 36,
129 7,128,127,105,123,116, 35, 34, 33,145, 31, 79, 42,146, 78, 26,
130 83, 48, 49, 50, 44, 47, 26, 31, 30, 18, 17, 19, 21, 24, 25, 13,
131 14, 16, 17, 18, 20, 21, 12, 14, 15, 9, 10, 6, 9, 6, 5, 8,
132 6, 12, 8, 10, 7, 9, 6, 4, 6, 2, 2, 3, 3, 3, 3, 2,
133};
134
7c5ab7b8 135static inline int sub_left_prediction(HYuvContext *s, uint8_t *dst, uint8_t *src, int w, int left){
11f18faf 136 int i;
7c5ab7b8
MN
137 if(w<32){
138 for(i=0; i<w; i++){
139 const int temp= src[i];
140 dst[i]= temp - left;
141 left= temp;
142 }
143 return left;
144 }else{
145 for(i=0; i<16; i++){
146 const int temp= src[i];
147 dst[i]= temp - left;
148 left= temp;
149 }
150 s->dsp.diff_bytes(dst+16, src+16, src+15, w-16);
151 return src[w-1];
11f18faf 152 }
11f18faf 153}
2a250222 154
d2bfaf8a
LM
155static inline void sub_left_prediction_bgr32(HYuvContext *s, uint8_t *dst, uint8_t *src, int w, int *red, int *green, int *blue){
156 int i;
157 int r,g,b;
158 r= *red;
159 g= *green;
160 b= *blue;
161 for(i=0; i<FFMIN(w,4); i++){
162 const int rt= src[i*4+R];
163 const int gt= src[i*4+G];
164 const int bt= src[i*4+B];
165 dst[i*4+R]= rt - r;
166 dst[i*4+G]= gt - g;
167 dst[i*4+B]= bt - b;
168 r = rt;
169 g = gt;
170 b = bt;
171 }
172 s->dsp.diff_bytes(dst+16, src+16, src+12, w*4-16);
173 *red= src[(w-1)*4+R];
174 *green= src[(w-1)*4+G];
175 *blue= src[(w-1)*4+B];
176}
177
e30004fa 178static int read_len_table(uint8_t *dst, GetBitContext *gb){
11f18faf 179 int i, val, repeat;
115329f1 180
11f18faf
MN
181 for(i=0; i<256;){
182 repeat= get_bits(gb, 3);
183 val = get_bits(gb, 5);
184 if(repeat==0)
185 repeat= get_bits(gb, 8);
186//printf("%d %d\n", val, repeat);
e30004fa
LM
187 if(i+repeat > 256) {
188 av_log(NULL, AV_LOG_ERROR, "Error reading huffman table\n");
189 return -1;
190 }
11f18faf
MN
191 while (repeat--)
192 dst[i++] = val;
193 }
e30004fa 194 return 0;
11f18faf
MN
195}
196
22c29c36 197static int generate_bits_table(uint32_t *dst, const uint8_t *len_table){
11f18faf
MN
198 int len, index;
199 uint32_t bits=0;
200
201 for(len=32; len>0; len--){
11f18faf 202 for(index=0; index<256; index++){
14b74d38
MN
203 if(len_table[index]==len)
204 dst[index]= bits++;
11f18faf 205 }
14b74d38 206 if(bits & 1){
9b879566 207 av_log(NULL, AV_LOG_ERROR, "Error generating huffman table\n");
14b74d38
MN
208 return -1;
209 }
210 bits >>= 1;
11f18faf
MN
211 }
212 return 0;
213}
214
b250f9c6 215#if CONFIG_HUFFYUV_ENCODER || CONFIG_FFVHUFF_ENCODER
98ef8c32
LM
216typedef struct {
217 uint64_t val;
218 int name;
fe5c7e58 219} HeapElem;
98ef8c32 220
fe5c7e58 221static void heap_sift(HeapElem *h, int root, int size)
98ef8c32
LM
222{
223 while(root*2+1 < size) {
224 int child = root*2+1;
225 if(child < size-1 && h[child].val > h[child+1].val)
226 child++;
227 if(h[root].val > h[child].val) {
fe5c7e58 228 FFSWAP(HeapElem, h[root], h[child]);
98ef8c32
LM
229 root = child;
230 } else
231 break;
232 }
233}
234
95024414 235static void generate_len_table(uint8_t *dst, const uint64_t *stats){
d45b771c
MR
236 HeapElem h[256];
237 int up[2*256];
238 int len[2*256];
11f18faf 239 int offset, i, next;
95024414 240 int size = 256;
115329f1 241
11f18faf
MN
242 for(offset=1; ; offset<<=1){
243 for(i=0; i<size; i++){
98ef8c32
LM
244 h[i].name = i;
245 h[i].val = (stats[i] << 8) + offset;
11f18faf 246 }
98ef8c32
LM
247 for(i=size/2-1; i>=0; i--)
248 heap_sift(h, i, size);
249
250 for(next=size; next<size*2-1; next++){
251 // merge the two smallest entries, and put it back in the heap
252 uint64_t min1v = h[0].val;
253 up[h[0].name] = next;
254 h[0].val = INT64_MAX;
255 heap_sift(h, 0, size);
256 up[h[0].name] = next;
257 h[0].name = next;
258 h[0].val += min1v;
259 heap_sift(h, 0, size);
11f18faf 260 }
115329f1 261
98ef8c32
LM
262 len[2*size-2] = 0;
263 for(i=2*size-3; i>=size; i--)
264 len[i] = len[up[i]] + 1;
265 for(i=0; i<size; i++) {
266 dst[i] = len[up[i]] + 1;
2bf4aa2e 267 if(dst[i] >= 32) break;
11f18faf
MN
268 }
269 if(i==size) break;
270 }
271}
b250f9c6 272#endif /* CONFIG_HUFFYUV_ENCODER || CONFIG_FFVHUFF_ENCODER */
11f18faf 273
19716d64 274static void generate_joint_tables(HYuvContext *s){
8be6dac8
LM
275 uint16_t symbols[1<<VLC_BITS];
276 uint16_t bits[1<<VLC_BITS];
277 uint8_t len[1<<VLC_BITS];
19716d64 278 if(s->bitstream_bpp < 24){
19716d64
LM
279 int p, i, y, u;
280 for(p=0; p<3; p++){
281 for(i=y=0; y<256; y++){
282 int len0 = s->len[0][y];
283 int limit = VLC_BITS - len0;
f45fcba3
LM
284 if(limit <= 0)
285 continue;
286 for(u=0; u<256; u++){
287 int len1 = s->len[p][u];
288 if(len1 > limit)
289 continue;
290 len[i] = len0 + len1;
291 bits[i] = (s->bits[0][y] << len1) + s->bits[p][u];
292 symbols[i] = (y<<8) + u;
293 if(symbols[i] != 0xffff) // reserved to mean "invalid"
294 i++;
19716d64
LM
295 }
296 }
297 free_vlc(&s->vlc[3+p]);
acc0e0c8 298 init_vlc_sparse(&s->vlc[3+p], VLC_BITS, i, len, 1, 1, bits, 2, 2, symbols, 2, 2, 0);
19716d64 299 }
8be6dac8
LM
300 }else{
301 uint8_t (*map)[4] = (uint8_t(*)[4])s->pix_bgr_map;
302 int i, b, g, r, code;
303 int p0 = s->decorrelate;
304 int p1 = !s->decorrelate;
305 // restrict the range to +/-16 becaues that's pretty much guaranteed to
306 // cover all the combinations that fit in 11 bits total, and it doesn't
307 // matter if we miss a few rare codes.
308 for(i=0, g=-16; g<16; g++){
309 int len0 = s->len[p0][g&255];
310 int limit0 = VLC_BITS - len0;
311 if(limit0 < 2)
312 continue;
313 for(b=-16; b<16; b++){
314 int len1 = s->len[p1][b&255];
315 int limit1 = limit0 - len1;
316 if(limit1 < 1)
317 continue;
318 code = (s->bits[p0][g&255] << len1) + s->bits[p1][b&255];
319 for(r=-16; r<16; r++){
320 int len2 = s->len[2][r&255];
321 if(len2 > limit1)
322 continue;
323 len[i] = len0 + len1 + len2;
324 bits[i] = (code << len2) + s->bits[2][r&255];
325 if(s->decorrelate){
326 map[i][G] = g;
327 map[i][B] = g+b;
328 map[i][R] = g+r;
329 }else{
330 map[i][B] = g;
331 map[i][G] = b;
332 map[i][R] = r;
333 }
334 i++;
335 }
336 }
337 }
338 free_vlc(&s->vlc[3]);
339 init_vlc(&s->vlc[3], VLC_BITS, i, len, 1, 1, bits, 2, 2, 0);
19716d64
LM
340 }
341}
342
22c29c36 343static int read_huffman_tables(HYuvContext *s, const uint8_t *src, int length){
11f18faf
MN
344 GetBitContext gb;
345 int i;
115329f1 346
68f593b4 347 init_get_bits(&gb, src, length*8);
115329f1 348
11f18faf 349 for(i=0; i<3; i++){
e30004fa
LM
350 if(read_len_table(s->len[i], &gb)<0)
351 return -1;
11f18faf
MN
352 if(generate_bits_table(s->bits[i], s->len[i])<0){
353 return -1;
354 }
11087086 355 free_vlc(&s->vlc[i]);
073c2593 356 init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4, 0);
11f18faf 357 }
115329f1 358
19716d64
LM
359 generate_joint_tables(s);
360
11087086 361 return (get_bits_count(&gb)+7)/8;
11f18faf
MN
362}
363
364static int read_old_huffman_tables(HYuvContext *s){
676b385c 365#if 1
11f18faf
MN
366 GetBitContext gb;
367 int i;
368
68f593b4 369 init_get_bits(&gb, classic_shift_luma, sizeof(classic_shift_luma)*8);
e30004fa
LM
370 if(read_len_table(s->len[0], &gb)<0)
371 return -1;
68f593b4 372 init_get_bits(&gb, classic_shift_chroma, sizeof(classic_shift_chroma)*8);
e30004fa
LM
373 if(read_len_table(s->len[1], &gb)<0)
374 return -1;
115329f1 375
11f18faf
MN
376 for(i=0; i<256; i++) s->bits[0][i] = classic_add_luma [i];
377 for(i=0; i<256; i++) s->bits[1][i] = classic_add_chroma[i];
378
379 if(s->bitstream_bpp >= 24){
380 memcpy(s->bits[1], s->bits[0], 256*sizeof(uint32_t));
381 memcpy(s->len[1] , s->len [0], 256*sizeof(uint8_t));
382 }
383 memcpy(s->bits[2], s->bits[1], 256*sizeof(uint32_t));
384 memcpy(s->len[2] , s->len [1], 256*sizeof(uint8_t));
115329f1 385
11087086
LM
386 for(i=0; i<3; i++){
387 free_vlc(&s->vlc[i]);
073c2593 388 init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4, 0);
11087086 389 }
115329f1 390
19716d64
LM
391 generate_joint_tables(s);
392
11f18faf
MN
393 return 0;
394#else
267f7edc 395 av_log(s->avctx, AV_LOG_DEBUG, "v1 huffyuv is not supported \n");
11f18faf
MN
396 return -1;
397#endif
398}
399
5ef251e5 400static av_cold void alloc_temp(HYuvContext *s){
ae2f1d46 401 int i;
115329f1 402
ae2f1d46
MN
403 if(s->bitstream_bpp<24){
404 for(i=0; i<3; i++){
405 s->temp[i]= av_malloc(s->width + 16);
406 }
407 }else{
f267d3ac 408 s->temp[0]= av_mallocz(4*s->width + 16);
ae2f1d46
MN
409 }
410}
411
5ef251e5 412static av_cold int common_init(AVCodecContext *avctx){
11f18faf 413 HYuvContext *s = avctx->priv_data;
11f18faf
MN
414
415 s->avctx= avctx;
416 s->flags= avctx->flags;
115329f1 417
1d98dca3 418 dsputil_init(&s->dsp, avctx);
115329f1 419
0ecca7a4
MN
420 s->width= avctx->width;
421 s->height= avctx->height;
422 assert(s->width>0 && s->height>0);
115329f1 423
0ecca7a4
MN
424 return 0;
425}
426
b250f9c6 427#if CONFIG_HUFFYUV_DECODER || CONFIG_FFVHUFF_DECODER
98a6fff9 428static av_cold int decode_init(AVCodecContext *avctx)
0ecca7a4
MN
429{
430 HYuvContext *s = avctx->priv_data;
431
432 common_init(avctx);
11087086 433 memset(s->vlc, 0, 3*sizeof(VLC));
115329f1 434
492cd3a9 435 avctx->coded_frame= &s->picture;
0ecca7a4 436 s->interlaced= s->height > 288;
1e491e29 437
11f18faf 438s->bgr32=1;
11f18faf
MN
439//if(avctx->extradata)
440// printf("extradata:%X, extradata_size:%d\n", *(uint32_t*)avctx->extradata, avctx->extradata_size);
441 if(avctx->extradata_size){
dd1c8f3e 442 if((avctx->bits_per_coded_sample&7) && avctx->bits_per_coded_sample != 12)
11f18faf
MN
443 s->version=1; // do such files exist at all?
444 else
445 s->version=2;
446 }else
447 s->version=0;
115329f1 448
11f18faf 449 if(s->version==2){
42dd22c4 450 int method, interlace;
11f18faf 451
2818bdf2
MK
452 if (avctx->extradata_size < 4)
453 return -1;
454
11f18faf
MN
455 method= ((uint8_t*)avctx->extradata)[0];
456 s->decorrelate= method&64 ? 1 : 0;
457 s->predictor= method&63;
458 s->bitstream_bpp= ((uint8_t*)avctx->extradata)[1];
115329f1 459 if(s->bitstream_bpp==0)
dd1c8f3e 460 s->bitstream_bpp= avctx->bits_per_coded_sample&~7;
42dd22c4
LM
461 interlace= (((uint8_t*)avctx->extradata)[2] & 0x30) >> 4;
462 s->interlaced= (interlace==1) ? 1 : (interlace==2) ? 0 : s->interlaced;
11087086 463 s->context= ((uint8_t*)avctx->extradata)[2] & 0x40 ? 1 : 0;
115329f1 464
2818bdf2 465 if(read_huffman_tables(s, ((uint8_t*)avctx->extradata)+4, avctx->extradata_size-4) < 0)
11f18faf
MN
466 return -1;
467 }else{
dd1c8f3e 468 switch(avctx->bits_per_coded_sample&7){
11f18faf
MN
469 case 1:
470 s->predictor= LEFT;
471 s->decorrelate= 0;
472 break;
473 case 2:
474 s->predictor= LEFT;
475 s->decorrelate= 1;
476 break;
477 case 3:
478 s->predictor= PLANE;
dd1c8f3e 479 s->decorrelate= avctx->bits_per_coded_sample >= 24;
11f18faf
MN
480 break;
481 case 4:
482 s->predictor= MEDIAN;
483 s->decorrelate= 0;
484 break;
485 default:
486 s->predictor= LEFT; //OLD
487 s->decorrelate= 0;
488 break;
489 }
dd1c8f3e 490 s->bitstream_bpp= avctx->bits_per_coded_sample & ~7;
11087086 491 s->context= 0;
115329f1 492
11f18faf
MN
493 if(read_old_huffman_tables(s) < 0)
494 return -1;
495 }
115329f1 496
11f18faf
MN
497 switch(s->bitstream_bpp){
498 case 12:
499 avctx->pix_fmt = PIX_FMT_YUV420P;
11f18faf
MN
500 break;
501 case 16:
502 if(s->yuy2){
71e445fc 503 avctx->pix_fmt = PIX_FMT_YUYV422;
11f18faf
MN
504 }else{
505 avctx->pix_fmt = PIX_FMT_YUV422P;
11f18faf
MN
506 }
507 break;
508 case 24:
509 case 32:
510 if(s->bgr32){
71e445fc 511 avctx->pix_fmt = PIX_FMT_RGB32;
11f18faf
MN
512 }else{
513 avctx->pix_fmt = PIX_FMT_BGR24;
11f18faf
MN
514 }
515 break;
516 default:
517 assert(0);
11f18faf 518 }
115329f1 519
ae2f1d46 520 alloc_temp(s);
115329f1 521
dd1c8f3e 522// av_log(NULL, AV_LOG_DEBUG, "pred:%d bpp:%d hbpp:%d il:%d\n", s->predictor, s->bitstream_bpp, avctx->bits_per_coded_sample, s->interlaced);
07140e39 523
11f18faf
MN
524 return 0;
525}
76d8846c
AS
526
527static av_cold int decode_init_thread_copy(AVCodecContext *avctx)
528{
529 HYuvContext *s = avctx->priv_data;
530 int i;
531
532 avctx->coded_frame= &s->picture;
533 alloc_temp(s);
534
535 for (i = 0; i < 6; i++)
536 s->vlc[i].table = NULL;
537
538 if(s->version==2){
539 if(read_huffman_tables(s, ((uint8_t*)avctx->extradata)+4, avctx->extradata_size) < 0)
540 return -1;
541 }else{
542 if(read_old_huffman_tables(s) < 0)
543 return -1;
544 }
545
546 return 0;
547}
b250f9c6 548#endif /* CONFIG_HUFFYUV_DECODER || CONFIG_FFVHUFF_DECODER */
11f18faf 549
b250f9c6 550#if CONFIG_HUFFYUV_ENCODER || CONFIG_FFVHUFF_ENCODER
22c29c36 551static int store_table(HYuvContext *s, const uint8_t *len, uint8_t *buf){
11f18faf 552 int i;
11087086 553 int index= 0;
11f18faf
MN
554
555 for(i=0; i<256;){
11f18faf 556 int val= len[i];
a003ee9a 557 int repeat=0;
115329f1 558
a003ee9a
MN
559 for(; i<256 && len[i]==val && repeat<255; i++)
560 repeat++;
115329f1 561
a003ee9a 562 assert(val < 32 && val >0 && repeat<256 && repeat>0);
11f18faf 563 if(repeat>7){
11087086
LM
564 buf[index++]= val;
565 buf[index++]= repeat;
11f18faf 566 }else{
11087086 567 buf[index++]= val | (repeat<<5);
11f18faf
MN
568 }
569 }
115329f1 570
11087086 571 return index;
11f18faf
MN
572}
573
98a6fff9 574static av_cold int encode_init(AVCodecContext *avctx)
11f18faf
MN
575{
576 HYuvContext *s = avctx->priv_data;
0ecca7a4 577 int i, j;
11f18faf 578
0ecca7a4 579 common_init(avctx);
115329f1 580
0ecca7a4
MN
581 avctx->extradata= av_mallocz(1024*30); // 256*3+4 == 772
582 avctx->stats_out= av_mallocz(1024*30); // 21*256*3(%llu ) + 3(\n) + 1(0) = 16132
11f18faf 583 s->version=2;
115329f1 584
492cd3a9 585 avctx->coded_frame= &s->picture;
115329f1 586
11f18faf 587 switch(avctx->pix_fmt){
152ba68c 588 case PIX_FMT_YUV420P:
152ba68c
MN
589 s->bitstream_bpp= 12;
590 break;
11f18faf
MN
591 case PIX_FMT_YUV422P:
592 s->bitstream_bpp= 16;
593 break;
d2bfaf8a
LM
594 case PIX_FMT_RGB32:
595 s->bitstream_bpp= 24;
596 break;
11f18faf 597 default:
9b879566 598 av_log(avctx, AV_LOG_ERROR, "format not supported\n");
11f18faf
MN
599 return -1;
600 }
dd1c8f3e 601 avctx->bits_per_coded_sample= s->bitstream_bpp;
11f18faf
MN
602 s->decorrelate= s->bitstream_bpp >= 24;
603 s->predictor= avctx->prediction_method;
ee598f7b 604 s->interlaced= avctx->flags&CODEC_FLAG_INTERLACED_ME ? 1 : 0;
11087086
LM
605 if(avctx->context_model==1){
606 s->context= avctx->context_model;
11087086
LM
607 if(s->flags & (CODEC_FLAG_PASS1|CODEC_FLAG_PASS2)){
608 av_log(avctx, AV_LOG_ERROR, "context=1 is not compatible with 2 pass huffyuv encoding\n");
609 return -1;
610 }
11087086 611 }else s->context= 0;
115329f1 612
f37b9768
LM
613 if(avctx->codec->id==CODEC_ID_HUFFYUV){
614 if(avctx->pix_fmt==PIX_FMT_YUV420P){
615 av_log(avctx, AV_LOG_ERROR, "Error: YV12 is not supported by huffyuv; use vcodec=ffvhuff or format=422p\n");
616 return -1;
617 }
618 if(avctx->context_model){
619 av_log(avctx, AV_LOG_ERROR, "Error: per-frame huffman tables are not supported by huffyuv; use vcodec=ffvhuff\n");
620 return -1;
621 }
0ecca7a4 622 if(s->interlaced != ( s->height > 288 ))
f37b9768 623 av_log(avctx, AV_LOG_INFO, "using huffyuv 2.2.0 or newer interlacing flag\n");
f37b9768 624 }
115329f1 625
d2bfaf8a
LM
626 if(s->bitstream_bpp>=24 && s->predictor==MEDIAN){
627 av_log(avctx, AV_LOG_ERROR, "Error: RGB is incompatible with median predictor\n");
628 return -1;
629 }
630
631 ((uint8_t*)avctx->extradata)[0]= s->predictor | (s->decorrelate << 6);
11f18faf 632 ((uint8_t*)avctx->extradata)[1]= s->bitstream_bpp;
42dd22c4 633 ((uint8_t*)avctx->extradata)[2]= s->interlaced ? 0x10 : 0x20;
11087086
LM
634 if(s->context)
635 ((uint8_t*)avctx->extradata)[2]|= 0x40;
11f18faf
MN
636 ((uint8_t*)avctx->extradata)[3]= 0;
637 s->avctx->extradata_size= 4;
115329f1 638
11f18faf
MN
639 if(avctx->stats_in){
640 char *p= avctx->stats_in;
115329f1 641
11f18faf
MN
642 for(i=0; i<3; i++)
643 for(j=0; j<256; j++)
644 s->stats[i][j]= 1;
645
646 for(;;){
647 for(i=0; i<3; i++){
648 char *next;
649
650 for(j=0; j<256; j++){
651 s->stats[i][j]+= strtol(p, &next, 0);
652 if(next==p) return -1;
653 p=next;
115329f1 654 }
11f18faf
MN
655 }
656 if(p[0]==0 || p[1]==0 || p[2]==0) break;
657 }
658 }else{
659 for(i=0; i<3; i++)
660 for(j=0; j<256; j++){
661 int d= FFMIN(j, 256-j);
115329f1 662
11f18faf
MN
663 s->stats[i][j]= 100000000/(d+1);
664 }
665 }
115329f1 666
11f18faf 667 for(i=0; i<3; i++){
95024414 668 generate_len_table(s->len[i], s->stats[i]);
11f18faf
MN
669
670 if(generate_bits_table(s->bits[i], s->len[i])<0){
671 return -1;
672 }
115329f1 673
11087086
LM
674 s->avctx->extradata_size+=
675 store_table(s, s->len[i], &((uint8_t*)s->avctx->extradata)[s->avctx->extradata_size]);
11f18faf
MN
676 }
677
11087086
LM
678 if(s->context){
679 for(i=0; i<3; i++){
0ecca7a4 680 int pels = s->width*s->height / (i?40:10);
11087086
LM
681 for(j=0; j<256; j++){
682 int d= FFMIN(j, 256-j);
683 s->stats[i][j]= pels/(d+1);
684 }
685 }
686 }else{
687 for(i=0; i<3; i++)
688 for(j=0; j<256; j++)
689 s->stats[i][j]= 0;
690 }
115329f1 691
dd1c8f3e 692// printf("pred:%d bpp:%d hbpp:%d il:%d\n", s->predictor, s->bitstream_bpp, avctx->bits_per_coded_sample, s->interlaced);
5fd74135 693
ae2f1d46
MN
694 alloc_temp(s);
695
11f18faf 696 s->picture_number=0;
5fd74135 697
11f18faf
MN
698 return 0;
699}
b250f9c6 700#endif /* CONFIG_HUFFYUV_ENCODER || CONFIG_FFVHUFF_ENCODER */
11f18faf 701
19716d64
LM
702/* TODO instead of restarting the read when the code isn't in the first level
703 * of the joint table, jump into the 2nd level of the individual table. */
704#define READ_2PIX(dst0, dst1, plane1){\
acc0e0c8
LM
705 uint16_t code = get_vlc2(&s->gb, s->vlc[3+plane1].table, VLC_BITS, 1);\
706 if(code != 0xffff){\
707 dst0 = code>>8;\
708 dst1 = code;\
19716d64
LM
709 }else{\
710 dst0 = get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3);\
711 dst1 = get_vlc2(&s->gb, s->vlc[plane1].table, VLC_BITS, 3);\
712 }\
713}
714
11f18faf
MN
715static void decode_422_bitstream(HYuvContext *s, int count){
716 int i;
5fd74135 717
11f18faf 718 count/=2;
115329f1 719
6e44ba15 720 if(count >= (get_bits_left(&s->gb))/(31*4)){
37458d7e
MN
721 for(i=0; i<count && get_bits_count(&s->gb) < s->gb.size_in_bits; i++){
722 READ_2PIX(s->temp[0][2*i ], s->temp[1][i], 1);
723 READ_2PIX(s->temp[0][2*i+1], s->temp[2][i], 2);
724 }
725 }else{
fb6ecd72
MN
726 for(i=0; i<count; i++){
727 READ_2PIX(s->temp[0][2*i ], s->temp[1][i], 1);
728 READ_2PIX(s->temp[0][2*i+1], s->temp[2][i], 2);
729 }
37458d7e 730 }
11f18faf
MN
731}
732
152ba68c
MN
733static void decode_gray_bitstream(HYuvContext *s, int count){
734 int i;
115329f1 735
152ba68c 736 count/=2;
115329f1 737
6e44ba15 738 if(count >= (get_bits_left(&s->gb))/(31*2)){
37458d7e
MN
739 for(i=0; i<count && get_bits_count(&s->gb) < s->gb.size_in_bits; i++){
740 READ_2PIX(s->temp[0][2*i ], s->temp[0][2*i+1], 0);
741 }
742 }else{
fb6ecd72
MN
743 for(i=0; i<count; i++){
744 READ_2PIX(s->temp[0][2*i ], s->temp[0][2*i+1], 0);
745 }
37458d7e 746 }
152ba68c
MN
747}
748
b250f9c6 749#if CONFIG_HUFFYUV_ENCODER || CONFIG_FFVHUFF_ENCODER
aa5cfdfb 750static int encode_422_bitstream(HYuvContext *s, int offset, int count){
11f18faf 751 int i;
aa5cfdfb
JD
752 const uint8_t *y = s->temp[0] + offset;
753 const uint8_t *u = s->temp[1] + offset/2;
754 const uint8_t *v = s->temp[2] + offset/2;
115329f1 755
0ecca7a4
MN
756 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < 2*4*count){
757 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
758 return -1;
759 }
115329f1 760
bf1a5a46 761#define LOAD4\
aa5cfdfb
JD
762 int y0 = y[2*i];\
763 int y1 = y[2*i+1];\
764 int u0 = u[i];\
765 int v0 = v[i];
bf1a5a46 766
11f18faf
MN
767 count/=2;
768 if(s->flags&CODEC_FLAG_PASS1){
769 for(i=0; i<count; i++){
bf1a5a46
LM
770 LOAD4;
771 s->stats[0][y0]++;
772 s->stats[1][u0]++;
773 s->stats[0][y1]++;
774 s->stats[2][v0]++;
11f18faf 775 }
09b0499f
MN
776 }
777 if(s->avctx->flags2&CODEC_FLAG2_NO_OUTPUT)
778 return 0;
779 if(s->context){
11087086 780 for(i=0; i<count; i++){
bf1a5a46
LM
781 LOAD4;
782 s->stats[0][y0]++;
783 put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
784 s->stats[1][u0]++;
785 put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
786 s->stats[0][y1]++;
787 put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
788 s->stats[2][v0]++;
789 put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
11087086 790 }
11f18faf
MN
791 }else{
792 for(i=0; i<count; i++){
bf1a5a46
LM
793 LOAD4;
794 put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
795 put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
796 put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
797 put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
11f18faf
MN
798 }
799 }
0ecca7a4 800 return 0;
11f18faf
MN
801}
802
0ecca7a4 803static int encode_gray_bitstream(HYuvContext *s, int count){
152ba68c 804 int i;
115329f1 805
0ecca7a4
MN
806 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < 4*count){
807 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
808 return -1;
809 }
810
bf1a5a46
LM
811#define LOAD2\
812 int y0 = s->temp[0][2*i];\
813 int y1 = s->temp[0][2*i+1];
814#define STAT2\
815 s->stats[0][y0]++;\
816 s->stats[0][y1]++;
817#define WRITE2\
818 put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);\
819 put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
820
152ba68c
MN
821 count/=2;
822 if(s->flags&CODEC_FLAG_PASS1){
823 for(i=0; i<count; i++){
bf1a5a46
LM
824 LOAD2;
825 STAT2;
152ba68c 826 }
09b0499f
MN
827 }
828 if(s->avctx->flags2&CODEC_FLAG2_NO_OUTPUT)
829 return 0;
115329f1 830
09b0499f 831 if(s->context){
11087086 832 for(i=0; i<count; i++){
bf1a5a46
LM
833 LOAD2;
834 STAT2;
835 WRITE2;
11087086 836 }
152ba68c
MN
837 }else{
838 for(i=0; i<count; i++){
bf1a5a46
LM
839 LOAD2;
840 WRITE2;
152ba68c
MN
841 }
842 }
0ecca7a4 843 return 0;
152ba68c 844}
b250f9c6 845#endif /* CONFIG_HUFFYUV_ENCODER || CONFIG_FFVHUFF_ENCODER */
152ba68c 846
8be6dac8 847static av_always_inline void decode_bgr_1(HYuvContext *s, int count, int decorrelate, int alpha){
11f18faf 848 int i;
8be6dac8
LM
849 for(i=0; i<count; i++){
850 int code = get_vlc2(&s->gb, s->vlc[3].table, VLC_BITS, 1);
851 if(code != -1){
852 *(uint32_t*)&s->temp[0][4*i] = s->pix_bgr_map[code];
853 }else if(decorrelate){
854 s->temp[0][4*i+G] = get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3);
855 s->temp[0][4*i+B] = get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3) + s->temp[0][4*i+G];
856 s->temp[0][4*i+R] = get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3) + s->temp[0][4*i+G];
11f18faf 857 }else{
8be6dac8
LM
858 s->temp[0][4*i+B] = get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3);
859 s->temp[0][4*i+G] = get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3);
860 s->temp[0][4*i+R] = get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3);
11f18faf 861 }
8be6dac8 862 if(alpha)
f267d3ac 863 s->temp[0][4*i+A] = get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3);
8be6dac8
LM
864 }
865}
866
867static void decode_bgr_bitstream(HYuvContext *s, int count){
868 if(s->decorrelate){
869 if(s->bitstream_bpp==24)
870 decode_bgr_1(s, count, 1, 0);
871 else
872 decode_bgr_1(s, count, 1, 1);
11f18faf 873 }else{
8be6dac8
LM
874 if(s->bitstream_bpp==24)
875 decode_bgr_1(s, count, 0, 0);
876 else
877 decode_bgr_1(s, count, 0, 1);
11f18faf
MN
878 }
879}
880
d2bfaf8a
LM
881static int encode_bgr_bitstream(HYuvContext *s, int count){
882 int i;
883
884 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < 3*4*count){
885 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
886 return -1;
887 }
888
bf1a5a46
LM
889#define LOAD3\
890 int g= s->temp[0][4*i+G];\
891 int b= (s->temp[0][4*i+B] - g) & 0xff;\
d2bfaf8a 892 int r= (s->temp[0][4*i+R] - g) & 0xff;
bf1a5a46
LM
893#define STAT3\
894 s->stats[0][b]++;\
895 s->stats[1][g]++;\
d2bfaf8a 896 s->stats[2][r]++;
bf1a5a46
LM
897#define WRITE3\
898 put_bits(&s->pb, s->len[1][g], s->bits[1][g]);\
899 put_bits(&s->pb, s->len[0][b], s->bits[0][b]);\
900 put_bits(&s->pb, s->len[2][r], s->bits[2][r]);
901
902 if((s->flags&CODEC_FLAG_PASS1) && (s->avctx->flags2&CODEC_FLAG2_NO_OUTPUT)){
903 for(i=0; i<count; i++){
904 LOAD3;
905 STAT3;
d2bfaf8a
LM
906 }
907 }else if(s->context || (s->flags&CODEC_FLAG_PASS1)){
908 for(i=0; i<count; i++){
bf1a5a46
LM
909 LOAD3;
910 STAT3;
911 WRITE3;
d2bfaf8a
LM
912 }
913 }else{
914 for(i=0; i<count; i++){
bf1a5a46
LM
915 LOAD3;
916 WRITE3;
d2bfaf8a
LM
917 }
918 }
919 return 0;
920}
921
b250f9c6 922#if CONFIG_HUFFYUV_DECODER || CONFIG_FFVHUFF_DECODER
7c5ab7b8
MN
923static void draw_slice(HYuvContext *s, int y){
924 int h, cy;
7a06ff14 925 int offset[4];
115329f1
DB
926
927 if(s->avctx->draw_horiz_band==NULL)
7c5ab7b8 928 return;
115329f1 929
7c5ab7b8
MN
930 h= y - s->last_slice_end;
931 y -= h;
115329f1 932
7c5ab7b8
MN
933 if(s->bitstream_bpp==12){
934 cy= y>>1;
935 }else{
936 cy= y;
937 }
7a06ff14
MN
938
939 offset[0] = s->picture.linesize[0]*y;
940 offset[1] = s->picture.linesize[1]*cy;
941 offset[2] = s->picture.linesize[2]*cy;
942 offset[3] = 0;
7c5ab7b8
MN
943 emms_c();
944
3bb07d61 945 s->avctx->draw_horiz_band(s->avctx, &s->picture, offset, y, 3, h);
115329f1 946
7c5ab7b8
MN
947 s->last_slice_end= y + h;
948}
949
7a00bbad
TB
950static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){
951 const uint8_t *buf = avpkt->data;
952 int buf_size = avpkt->size;
11f18faf
MN
953 HYuvContext *s = avctx->priv_data;
954 const int width= s->width;
955 const int width2= s->width>>1;
956 const int height= s->height;
22f3e8be 957 int fake_ystride, fake_ustride, fake_vstride;
492cd3a9 958 AVFrame * const p= &s->picture;
11087086 959 int table_size= 0;
11f18faf 960
492cd3a9 961 AVFrame *picture = data;
11f18faf 962
238ef6da
RD
963 av_fast_malloc(&s->bitstream_buffer, &s->bitstream_buffer_size, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
964 if (!s->bitstream_buffer)
965 return AVERROR(ENOMEM);
11f18faf 966
6ec8eadd 967 memset(s->bitstream_buffer + buf_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
fb2cf1bc 968 s->dsp.bswap_buf((uint32_t*)s->bitstream_buffer, (const uint32_t*)buf, buf_size/4);
115329f1 969
e20c4069 970 if(p->data[0])
76d8846c 971 ff_thread_release_buffer(avctx, p);
e20c4069 972
1e491e29 973 p->reference= 0;
76d8846c 974 if(ff_thread_get_buffer(avctx, p) < 0){
9b879566 975 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1e491e29 976 return -1;
22f3e8be 977 }
115329f1 978
11087086
LM
979 if(s->context){
980 table_size = read_huffman_tables(s, s->bitstream_buffer, buf_size);
981 if(table_size < 0)
982 return -1;
983 }
984
e70e7a4b
MN
985 if((unsigned)(buf_size-table_size) >= INT_MAX/8)
986 return -1;
987
11087086 988 init_get_bits(&s->gb, s->bitstream_buffer+table_size, (buf_size-table_size)*8);
1e491e29
MN
989
990 fake_ystride= s->interlaced ? p->linesize[0]*2 : p->linesize[0];
991 fake_ustride= s->interlaced ? p->linesize[1]*2 : p->linesize[1];
992 fake_vstride= s->interlaced ? p->linesize[2]*2 : p->linesize[2];
115329f1 993
7c5ab7b8 994 s->last_slice_end= 0;
115329f1 995
11f18faf 996 if(s->bitstream_bpp<24){
152ba68c 997 int y, cy;
11f18faf
MN
998 int lefty, leftu, leftv;
999 int lefttopy, lefttopu, lefttopv;
115329f1 1000
11f18faf 1001 if(s->yuy2){
1e491e29
MN
1002 p->data[0][3]= get_bits(&s->gb, 8);
1003 p->data[0][2]= get_bits(&s->gb, 8);
1004 p->data[0][1]= get_bits(&s->gb, 8);
1005 p->data[0][0]= get_bits(&s->gb, 8);
115329f1 1006
160d679c 1007 av_log(avctx, AV_LOG_ERROR, "YUY2 output is not implemented yet\n");
11f18faf
MN
1008 return -1;
1009 }else{
115329f1 1010
1e491e29
MN
1011 leftv= p->data[2][0]= get_bits(&s->gb, 8);
1012 lefty= p->data[0][1]= get_bits(&s->gb, 8);
1013 leftu= p->data[1][0]= get_bits(&s->gb, 8);
1014 p->data[0][0]= get_bits(&s->gb, 8);
115329f1 1015
11f18faf
MN
1016 switch(s->predictor){
1017 case LEFT:
1018 case PLANE:
1019 decode_422_bitstream(s, width-2);
73c6f598 1020 lefty= s->dsp.add_hfyu_left_prediction(p->data[0] + 2, s->temp[0], width-2, lefty);
11f18faf 1021 if(!(s->flags&CODEC_FLAG_GRAY)){
73c6f598
NC
1022 leftu= s->dsp.add_hfyu_left_prediction(p->data[1] + 1, s->temp[1], width2-1, leftu);
1023 leftv= s->dsp.add_hfyu_left_prediction(p->data[2] + 1, s->temp[2], width2-1, leftv);
11f18faf
MN
1024 }
1025
152ba68c 1026 for(cy=y=1; y<s->height; y++,cy++){
11f18faf 1027 uint8_t *ydst, *udst, *vdst;
115329f1 1028
152ba68c
MN
1029 if(s->bitstream_bpp==12){
1030 decode_gray_bitstream(s, width);
115329f1 1031
1e491e29 1032 ydst= p->data[0] + p->linesize[0]*y;
152ba68c 1033
73c6f598 1034 lefty= s->dsp.add_hfyu_left_prediction(ydst, s->temp[0], width, lefty);
152ba68c
MN
1035 if(s->predictor == PLANE){
1036 if(y>s->interlaced)
1037 s->dsp.add_bytes(ydst, ydst - fake_ystride, width);
1038 }
1039 y++;
1040 if(y>=s->height) break;
1041 }
115329f1 1042
7c5ab7b8 1043 draw_slice(s, y);
115329f1 1044
1e491e29
MN
1045 ydst= p->data[0] + p->linesize[0]*y;
1046 udst= p->data[1] + p->linesize[1]*cy;
1047 vdst= p->data[2] + p->linesize[2]*cy;
115329f1 1048
152ba68c 1049 decode_422_bitstream(s, width);
73c6f598 1050 lefty= s->dsp.add_hfyu_left_prediction(ydst, s->temp[0], width, lefty);
11f18faf 1051 if(!(s->flags&CODEC_FLAG_GRAY)){
73c6f598
NC
1052 leftu= s->dsp.add_hfyu_left_prediction(udst, s->temp[1], width2, leftu);
1053 leftv= s->dsp.add_hfyu_left_prediction(vdst, s->temp[2], width2, leftv);
11f18faf
MN
1054 }
1055 if(s->predictor == PLANE){
152ba68c 1056 if(cy>s->interlaced){
11f18faf
MN
1057 s->dsp.add_bytes(ydst, ydst - fake_ystride, width);
1058 if(!(s->flags&CODEC_FLAG_GRAY)){
1059 s->dsp.add_bytes(udst, udst - fake_ustride, width2);
1060 s->dsp.add_bytes(vdst, vdst - fake_vstride, width2);
1061 }
1062 }
1063 }
1064 }
7c5ab7b8 1065 draw_slice(s, height);
115329f1 1066
11f18faf
MN
1067 break;
1068 case MEDIAN:
1069 /* first line except first 2 pixels is left predicted */
1070 decode_422_bitstream(s, width-2);
73c6f598 1071 lefty= s->dsp.add_hfyu_left_prediction(p->data[0] + 2, s->temp[0], width-2, lefty);
11f18faf 1072 if(!(s->flags&CODEC_FLAG_GRAY)){
73c6f598
NC
1073 leftu= s->dsp.add_hfyu_left_prediction(p->data[1] + 1, s->temp[1], width2-1, leftu);
1074 leftv= s->dsp.add_hfyu_left_prediction(p->data[2] + 1, s->temp[2], width2-1, leftv);
11f18faf 1075 }
115329f1 1076
152ba68c 1077 cy=y=1;
115329f1 1078
11f18faf
MN
1079 /* second line is left predicted for interlaced case */
1080 if(s->interlaced){
1081 decode_422_bitstream(s, width);
73c6f598 1082 lefty= s->dsp.add_hfyu_left_prediction(p->data[0] + p->linesize[0], s->temp[0], width, lefty);
11f18faf 1083 if(!(s->flags&CODEC_FLAG_GRAY)){
73c6f598
NC
1084 leftu= s->dsp.add_hfyu_left_prediction(p->data[1] + p->linesize[2], s->temp[1], width2, leftu);
1085 leftv= s->dsp.add_hfyu_left_prediction(p->data[2] + p->linesize[1], s->temp[2], width2, leftv);
11f18faf 1086 }
152ba68c 1087 y++; cy++;
11f18faf
MN
1088 }
1089
1090 /* next 4 pixels are left predicted too */
1091 decode_422_bitstream(s, 4);
73c6f598 1092 lefty= s->dsp.add_hfyu_left_prediction(p->data[0] + fake_ystride, s->temp[0], 4, lefty);
11f18faf 1093 if(!(s->flags&CODEC_FLAG_GRAY)){
73c6f598
NC
1094 leftu= s->dsp.add_hfyu_left_prediction(p->data[1] + fake_ustride, s->temp[1], 2, leftu);
1095 leftv= s->dsp.add_hfyu_left_prediction(p->data[2] + fake_vstride, s->temp[2], 2, leftv);
11f18faf
MN
1096 }
1097
1098 /* next line except the first 4 pixels is median predicted */
1e491e29 1099 lefttopy= p->data[0][3];
11f18faf 1100 decode_422_bitstream(s, width-4);
3daa434a 1101 s->dsp.add_hfyu_median_prediction(p->data[0] + fake_ystride+4, p->data[0]+4, s->temp[0], width-4, &lefty, &lefttopy);
11f18faf 1102 if(!(s->flags&CODEC_FLAG_GRAY)){
1e491e29
MN
1103 lefttopu= p->data[1][1];
1104 lefttopv= p->data[2][1];
3daa434a
LM
1105 s->dsp.add_hfyu_median_prediction(p->data[1] + fake_ustride+2, p->data[1]+2, s->temp[1], width2-2, &leftu, &lefttopu);
1106 s->dsp.add_hfyu_median_prediction(p->data[2] + fake_vstride+2, p->data[2]+2, s->temp[2], width2-2, &leftv, &lefttopv);
11f18faf 1107 }
152ba68c 1108 y++; cy++;
115329f1 1109
152ba68c 1110 for(; y<height; y++,cy++){
11f18faf 1111 uint8_t *ydst, *udst, *vdst;
152ba68c
MN
1112
1113 if(s->bitstream_bpp==12){
1114 while(2*cy > y){
1115 decode_gray_bitstream(s, width);
1e491e29 1116 ydst= p->data[0] + p->linesize[0]*y;
3daa434a 1117 s->dsp.add_hfyu_median_prediction(ydst, ydst - fake_ystride, s->temp[0], width, &lefty, &lefttopy);
152ba68c
MN
1118 y++;
1119 }
1120 if(y>=height) break;
1121 }
7c5ab7b8 1122 draw_slice(s, y);
152ba68c 1123
11f18faf 1124 decode_422_bitstream(s, width);
152ba68c 1125
1e491e29
MN
1126 ydst= p->data[0] + p->linesize[0]*y;
1127 udst= p->data[1] + p->linesize[1]*cy;
1128 vdst= p->data[2] + p->linesize[2]*cy;
11f18faf 1129
3daa434a 1130 s->dsp.add_hfyu_median_prediction(ydst, ydst - fake_ystride, s->temp[0], width, &lefty, &lefttopy);
11f18faf 1131 if(!(s->flags&CODEC_FLAG_GRAY)){
3daa434a
LM
1132 s->dsp.add_hfyu_median_prediction(udst, udst - fake_ustride, s->temp[1], width2, &leftu, &lefttopu);
1133 s->dsp.add_hfyu_median_prediction(vdst, vdst - fake_vstride, s->temp[2], width2, &leftv, &lefttopv);
11f18faf
MN
1134 }
1135 }
7c5ab7b8
MN
1136
1137 draw_slice(s, height);
11f18faf
MN
1138 break;
1139 }
1140 }
1141 }else{
1142 int y;
f267d3ac 1143 int leftr, leftg, leftb, lefta;
1e491e29 1144 const int last_line= (height-1)*p->linesize[0];
115329f1 1145
11f18faf 1146 if(s->bitstream_bpp==32){
f267d3ac 1147 lefta= p->data[0][last_line+A]= get_bits(&s->gb, 8);
0722ccdb
MN
1148 leftr= p->data[0][last_line+R]= get_bits(&s->gb, 8);
1149 leftg= p->data[0][last_line+G]= get_bits(&s->gb, 8);
1150 leftb= p->data[0][last_line+B]= get_bits(&s->gb, 8);
11f18faf 1151 }else{
0722ccdb
MN
1152 leftr= p->data[0][last_line+R]= get_bits(&s->gb, 8);
1153 leftg= p->data[0][last_line+G]= get_bits(&s->gb, 8);
1154 leftb= p->data[0][last_line+B]= get_bits(&s->gb, 8);
f267d3ac 1155 lefta= p->data[0][last_line+A]= 255;
11f18faf
MN
1156 skip_bits(&s->gb, 8);
1157 }
115329f1 1158
11f18faf
MN
1159 if(s->bgr32){
1160 switch(s->predictor){
1161 case LEFT:
1162 case PLANE:
1163 decode_bgr_bitstream(s, width-1);
f267d3ac 1164 s->dsp.add_hfyu_left_prediction_bgr32(p->data[0] + last_line+4, s->temp[0], width-1, &leftr, &leftg, &leftb, &lefta);
11f18faf 1165
90b5b51e 1166 for(y=s->height-2; y>=0; y--){ //Yes it is stored upside down.
11f18faf 1167 decode_bgr_bitstream(s, width);
115329f1 1168
f267d3ac 1169 s->dsp.add_hfyu_left_prediction_bgr32(p->data[0] + p->linesize[0]*y, s->temp[0], width, &leftr, &leftg, &leftb, &lefta);
11f18faf 1170 if(s->predictor == PLANE){
f267d3ac 1171 if(s->bitstream_bpp!=32) lefta=0;
fc0be57f 1172 if((y&s->interlaced)==0 && y<s->height-1-s->interlaced){
115329f1 1173 s->dsp.add_bytes(p->data[0] + p->linesize[0]*y,
1e491e29 1174 p->data[0] + p->linesize[0]*y + fake_ystride, fake_ystride);
11f18faf
MN
1175 }
1176 }
1177 }
160d679c 1178 draw_slice(s, height); // just 1 large slice as this is not possible in reverse order
11f18faf
MN
1179 break;
1180 default:
9b879566 1181 av_log(avctx, AV_LOG_ERROR, "prediction type not supported!\n");
11f18faf
MN
1182 }
1183 }else{
1184
160d679c 1185 av_log(avctx, AV_LOG_ERROR, "BGR24 output is not implemented yet\n");
11f18faf
MN
1186 return -1;
1187 }
1188 }
1189 emms_c();
115329f1 1190
1e491e29 1191 *picture= *p;
492cd3a9 1192 *data_size = sizeof(AVFrame);
115329f1 1193
1adc29ee 1194 return (get_bits_count(&s->gb)+31)/32*4 + table_size;
11f18faf 1195}
b250f9c6 1196#endif /* CONFIG_HUFFYUV_DECODER || CONFIG_FFVHUFF_DECODER */
11f18faf 1197
0ecca7a4
MN
1198static int common_end(HYuvContext *s){
1199 int i;
115329f1 1200
0ecca7a4
MN
1201 for(i=0; i<3; i++){
1202 av_freep(&s->temp[i]);
1203 }
1204 return 0;
1205}
1206
b250f9c6 1207#if CONFIG_HUFFYUV_DECODER || CONFIG_FFVHUFF_DECODER
98a6fff9 1208static av_cold int decode_end(AVCodecContext *avctx)
11f18faf
MN
1209{
1210 HYuvContext *s = avctx->priv_data;
1211 int i;
115329f1 1212
0b7f39c9
CEH
1213 if (s->picture.data[0])
1214 avctx->release_buffer(avctx, &s->picture);
1215
0ecca7a4
MN
1216 common_end(s);
1217 av_freep(&s->bitstream_buffer);
115329f1 1218
7b62bb2b 1219 for(i=0; i<6; i++){
11f18faf
MN
1220 free_vlc(&s->vlc[i]);
1221 }
1e491e29 1222
11f18faf
MN
1223 return 0;
1224}
b250f9c6 1225#endif /* CONFIG_HUFFYUV_DECODER || CONFIG_FFVHUFF_DECODER */
11f18faf 1226
b250f9c6 1227#if CONFIG_HUFFYUV_ENCODER || CONFIG_FFVHUFF_ENCODER
11f18faf
MN
1228static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
1229 HYuvContext *s = avctx->priv_data;
492cd3a9 1230 AVFrame *pict = data;
11f18faf
MN
1231 const int width= s->width;
1232 const int width2= s->width>>1;
1233 const int height= s->height;
1234 const int fake_ystride= s->interlaced ? pict->linesize[0]*2 : pict->linesize[0];
1235 const int fake_ustride= s->interlaced ? pict->linesize[1]*2 : pict->linesize[1];
1236 const int fake_vstride= s->interlaced ? pict->linesize[2]*2 : pict->linesize[2];
492cd3a9 1237 AVFrame * const p= &s->picture;
11087086 1238 int i, j, size=0;
11f18faf 1239
1e491e29 1240 *p = *pict;
975a1447 1241 p->pict_type= AV_PICTURE_TYPE_I;
aaa1e4cd 1242 p->key_frame= 1;
115329f1 1243
11087086
LM
1244 if(s->context){
1245 for(i=0; i<3; i++){
95024414 1246 generate_len_table(s->len[i], s->stats[i]);
11087086
LM
1247 if(generate_bits_table(s->bits[i], s->len[i])<0)
1248 return -1;
1249 size+= store_table(s, s->len[i], &buf[size]);
1250 }
1251
1252 for(i=0; i<3; i++)
1253 for(j=0; j<256; j++)
1254 s->stats[i][j] >>= 1;
1255 }
1256
1257 init_put_bits(&s->pb, buf+size, buf_size-size);
1258
152ba68c
MN
1259 if(avctx->pix_fmt == PIX_FMT_YUV422P || avctx->pix_fmt == PIX_FMT_YUV420P){
1260 int lefty, leftu, leftv, y, cy;
11f18faf 1261
1e491e29
MN
1262 put_bits(&s->pb, 8, leftv= p->data[2][0]);
1263 put_bits(&s->pb, 8, lefty= p->data[0][1]);
1264 put_bits(&s->pb, 8, leftu= p->data[1][0]);
1265 put_bits(&s->pb, 8, p->data[0][0]);
115329f1 1266
fdb699de
JD
1267 lefty= sub_left_prediction(s, s->temp[0], p->data[0], width , 0);
1268 leftu= sub_left_prediction(s, s->temp[1], p->data[1], width2, 0);
1269 leftv= sub_left_prediction(s, s->temp[2], p->data[2], width2, 0);
115329f1 1270
fdb699de 1271 encode_422_bitstream(s, 2, width-2);
115329f1 1272
11f18faf
MN
1273 if(s->predictor==MEDIAN){
1274 int lefttopy, lefttopu, lefttopv;
152ba68c 1275 cy=y=1;
11f18faf 1276 if(s->interlaced){
1e491e29
MN
1277 lefty= sub_left_prediction(s, s->temp[0], p->data[0]+p->linesize[0], width , lefty);
1278 leftu= sub_left_prediction(s, s->temp[1], p->data[1]+p->linesize[1], width2, leftu);
1279 leftv= sub_left_prediction(s, s->temp[2], p->data[2]+p->linesize[2], width2, leftv);
115329f1 1280
aa5cfdfb 1281 encode_422_bitstream(s, 0, width);
152ba68c 1282 y++; cy++;
11f18faf 1283 }
115329f1 1284
1e491e29 1285 lefty= sub_left_prediction(s, s->temp[0], p->data[0]+fake_ystride, 4, lefty);
07140e39
MN
1286 leftu= sub_left_prediction(s, s->temp[1], p->data[1]+fake_ustride, 2, leftu);
1287 leftv= sub_left_prediction(s, s->temp[2], p->data[2]+fake_vstride, 2, leftv);
115329f1 1288
aa5cfdfb 1289 encode_422_bitstream(s, 0, 4);
152ba68c 1290
1e491e29
MN
1291 lefttopy= p->data[0][3];
1292 lefttopu= p->data[1][1];
1293 lefttopv= p->data[2][1];
84705403
MN
1294 s->dsp.sub_hfyu_median_prediction(s->temp[0], p->data[0]+4, p->data[0] + fake_ystride+4, width-4 , &lefty, &lefttopy);
1295 s->dsp.sub_hfyu_median_prediction(s->temp[1], p->data[1]+2, p->data[1] + fake_ustride+2, width2-2, &leftu, &lefttopu);
1296 s->dsp.sub_hfyu_median_prediction(s->temp[2], p->data[2]+2, p->data[2] + fake_vstride+2, width2-2, &leftv, &lefttopv);
aa5cfdfb 1297 encode_422_bitstream(s, 0, width-4);
152ba68c 1298 y++; cy++;
11f18faf 1299
152ba68c 1300 for(; y<height; y++,cy++){
11f18faf 1301 uint8_t *ydst, *udst, *vdst;
115329f1 1302
152ba68c
MN
1303 if(s->bitstream_bpp==12){
1304 while(2*cy > y){
1e491e29 1305 ydst= p->data[0] + p->linesize[0]*y;
84705403 1306 s->dsp.sub_hfyu_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy);
152ba68c
MN
1307 encode_gray_bitstream(s, width);
1308 y++;
1309 }
1310 if(y>=height) break;
1311 }
1e491e29
MN
1312 ydst= p->data[0] + p->linesize[0]*y;
1313 udst= p->data[1] + p->linesize[1]*cy;
1314 vdst= p->data[2] + p->linesize[2]*cy;
11f18faf 1315
84705403
MN
1316 s->dsp.sub_hfyu_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy);
1317 s->dsp.sub_hfyu_median_prediction(s->temp[1], udst - fake_ustride, udst, width2, &leftu, &lefttopu);
1318 s->dsp.sub_hfyu_median_prediction(s->temp[2], vdst - fake_vstride, vdst, width2, &leftv, &lefttopv);
11f18faf 1319
aa5cfdfb 1320 encode_422_bitstream(s, 0, width);
11f18faf
MN
1321 }
1322 }else{
152ba68c 1323 for(cy=y=1; y<height; y++,cy++){
11f18faf 1324 uint8_t *ydst, *udst, *vdst;
115329f1 1325
152ba68c
MN
1326 /* encode a luma only line & y++ */
1327 if(s->bitstream_bpp==12){
1e491e29 1328 ydst= p->data[0] + p->linesize[0]*y;
152ba68c
MN
1329
1330 if(s->predictor == PLANE && s->interlaced < y){
7c5ab7b8 1331 s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
152ba68c 1332
7c5ab7b8 1333 lefty= sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
152ba68c 1334 }else{
7c5ab7b8 1335 lefty= sub_left_prediction(s, s->temp[0], ydst, width , lefty);
152ba68c
MN
1336 }
1337 encode_gray_bitstream(s, width);
1338 y++;
1339 if(y>=height) break;
1340 }
115329f1 1341
1e491e29
MN
1342 ydst= p->data[0] + p->linesize[0]*y;
1343 udst= p->data[1] + p->linesize[1]*cy;
1344 vdst= p->data[2] + p->linesize[2]*cy;
11f18faf 1345
152ba68c 1346 if(s->predictor == PLANE && s->interlaced < cy){
7c5ab7b8
MN
1347 s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
1348 s->dsp.diff_bytes(s->temp[2], udst, udst - fake_ustride, width2);
ae2f1d46 1349 s->dsp.diff_bytes(s->temp[2] + width2, vdst, vdst - fake_vstride, width2);
11f18faf 1350
7c5ab7b8
MN
1351 lefty= sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
1352 leftu= sub_left_prediction(s, s->temp[1], s->temp[2], width2, leftu);
ae2f1d46 1353 leftv= sub_left_prediction(s, s->temp[2], s->temp[2] + width2, width2, leftv);
11f18faf 1354 }else{
7c5ab7b8
MN
1355 lefty= sub_left_prediction(s, s->temp[0], ydst, width , lefty);
1356 leftu= sub_left_prediction(s, s->temp[1], udst, width2, leftu);
1357 leftv= sub_left_prediction(s, s->temp[2], vdst, width2, leftv);
11f18faf
MN
1358 }
1359
aa5cfdfb 1360 encode_422_bitstream(s, 0, width);
11f18faf 1361 }
115329f1 1362 }
d2bfaf8a
LM
1363 }else if(avctx->pix_fmt == PIX_FMT_RGB32){
1364 uint8_t *data = p->data[0] + (height-1)*p->linesize[0];
1365 const int stride = -p->linesize[0];
1366 const int fake_stride = -fake_ystride;
1367 int y;
1368 int leftr, leftg, leftb;
1369
1370 put_bits(&s->pb, 8, leftr= data[R]);
1371 put_bits(&s->pb, 8, leftg= data[G]);
1372 put_bits(&s->pb, 8, leftb= data[B]);
1373 put_bits(&s->pb, 8, 0);
1374
1375 sub_left_prediction_bgr32(s, s->temp[0], data+4, width-1, &leftr, &leftg, &leftb);
1376 encode_bgr_bitstream(s, width-1);
1377
1378 for(y=1; y<s->height; y++){
1379 uint8_t *dst = data + y*stride;
1380 if(s->predictor == PLANE && s->interlaced < y){
1381 s->dsp.diff_bytes(s->temp[1], dst, dst - fake_stride, width*4);
1382 sub_left_prediction_bgr32(s, s->temp[0], s->temp[1], width, &leftr, &leftg, &leftb);
1383 }else{
1384 sub_left_prediction_bgr32(s, s->temp[0], dst, width, &leftr, &leftg, &leftb);
1385 }
1386 encode_bgr_bitstream(s, width);
1387 }
11f18faf 1388 }else{
9b879566 1389 av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
11f18faf
MN
1390 }
1391 emms_c();
115329f1 1392
11087086 1393 size+= (put_bits_count(&s->pb)+31)/8;
300a3d0d
MN
1394 put_bits(&s->pb, 16, 0);
1395 put_bits(&s->pb, 15, 0);
11087086 1396 size/= 4;
115329f1 1397
11f18faf
MN
1398 if((s->flags&CODEC_FLAG_PASS1) && (s->picture_number&31)==0){
1399 int j;
1400 char *p= avctx->stats_out;
2fc8ea24 1401 char *end= p + 1024*30;
11f18faf
MN
1402 for(i=0; i<3; i++){
1403 for(j=0; j<256; j++){
4733abcb 1404 snprintf(p, end-p, "%"PRIu64" ", s->stats[i][j]);
11f18faf
MN
1405 p+= strlen(p);
1406 s->stats[i][j]= 0;
1407 }
2fc8ea24 1408 snprintf(p, end-p, "\n");
11f18faf
MN
1409 p++;
1410 }
390f9f31
LM
1411 } else
1412 avctx->stats_out[0] = '\0';
09b0499f 1413 if(!(s->avctx->flags2 & CODEC_FLAG2_NO_OUTPUT)){
152ba68c 1414 flush_put_bits(&s->pb);
3d2e8cce 1415 s->dsp.bswap_buf((uint32_t*)buf, (uint32_t*)buf, size);
11f18faf 1416 }
115329f1 1417
11f18faf 1418 s->picture_number++;
1e491e29 1419
11f18faf
MN
1420 return size*4;
1421}
1422
98a6fff9 1423static av_cold int encode_end(AVCodecContext *avctx)
11f18faf 1424{
0ecca7a4 1425 HYuvContext *s = avctx->priv_data;
115329f1 1426
0ecca7a4 1427 common_end(s);
11f18faf
MN
1428
1429 av_freep(&avctx->extradata);
1430 av_freep(&avctx->stats_out);
115329f1 1431
11f18faf
MN
1432 return 0;
1433}
b250f9c6 1434#endif /* CONFIG_HUFFYUV_ENCODER || CONFIG_FFVHUFF_ENCODER */
11f18faf 1435
b250f9c6 1436#if CONFIG_HUFFYUV_DECODER
d36beb3f 1437AVCodec ff_huffyuv_decoder = {
ec6402b7
AK
1438 .name = "huffyuv",
1439 .type = AVMEDIA_TYPE_VIDEO,
1440 .id = CODEC_ID_HUFFYUV,
1441 .priv_data_size = sizeof(HYuvContext),
1442 .init = decode_init,
1443 .close = decode_end,
1444 .decode = decode_frame,
1445 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_FRAME_THREADS,
76d8846c 1446 .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
fe4bf374 1447 .long_name = NULL_IF_CONFIG_SMALL("Huffyuv / HuffYUV"),
11f18faf 1448};
2a43a093 1449#endif
11f18faf 1450
b250f9c6 1451#if CONFIG_FFVHUFF_DECODER
d36beb3f 1452AVCodec ff_ffvhuff_decoder = {
ec6402b7
AK
1453 .name = "ffvhuff",
1454 .type = AVMEDIA_TYPE_VIDEO,
1455 .id = CODEC_ID_FFVHUFF,
1456 .priv_data_size = sizeof(HYuvContext),
1457 .init = decode_init,
1458 .close = decode_end,
1459 .decode = decode_frame,
1460 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_FRAME_THREADS,
76d8846c 1461 .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
fe4bf374 1462 .long_name = NULL_IF_CONFIG_SMALL("Huffyuv FFmpeg variant"),
f37b9768 1463};
62bb489b 1464#endif
f37b9768 1465
b250f9c6 1466#if CONFIG_HUFFYUV_ENCODER
d36beb3f 1467AVCodec ff_huffyuv_encoder = {
ec6402b7
AK
1468 .name = "huffyuv",
1469 .type = AVMEDIA_TYPE_VIDEO,
1470 .id = CODEC_ID_HUFFYUV,
1471 .priv_data_size = sizeof(HYuvContext),
1472 .init = encode_init,
1473 .encode = encode_frame,
1474 .close = encode_end,
2ba83017 1475 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV422P, PIX_FMT_RGB32, PIX_FMT_NONE},
fe4bf374 1476 .long_name = NULL_IF_CONFIG_SMALL("Huffyuv / HuffYUV"),
11f18faf 1477};
f544a5fc 1478#endif
5fd74135 1479
b250f9c6 1480#if CONFIG_FFVHUFF_ENCODER
d36beb3f 1481AVCodec ff_ffvhuff_encoder = {
ec6402b7
AK
1482 .name = "ffvhuff",
1483 .type = AVMEDIA_TYPE_VIDEO,
1484 .id = CODEC_ID_FFVHUFF,
1485 .priv_data_size = sizeof(HYuvContext),
1486 .init = encode_init,
1487 .encode = encode_frame,
1488 .close = encode_end,
2ba83017 1489 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_RGB32, PIX_FMT_NONE},
fe4bf374 1490 .long_name = NULL_IF_CONFIG_SMALL("Huffyuv FFmpeg variant"),
f37b9768 1491};
f544a5fc 1492#endif