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