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