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