2 * huffyuv codec for libavcodec
4 * Copyright (c) 2002-2003 Michael Niedermayer <michaelni@gmx.at>
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.
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.
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
20 * see http://www.pcisys.net/~melanson/codecs/huffyuv.txt for a description of
26 * huffyuv codec for libavcodec.
34 #define INT64_MAX 9223372036854775807LL
39 typedef enum Predictor{
45 typedef struct HYuvContext{
46 AVCodecContext *avctx;
54 int yuy2; //use yuy2 instead of 422P
55 int bgr32; //use bgr32 instead of bgr24
60 uint8_t __align8 temp[3][2500];
61 uint64_t stats[3][256];
63 uint32_t bits[3][256];
66 uint8_t __align8 bitstream_buffer[1024*1024*3]; //FIXME dynamic alloc or some other solution
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,
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
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,
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,
120 static inline int add_left_prediction(uint8_t *dst, uint8_t *src, int w, int acc){
123 for(i=0; i<w-1; i++){
139 static inline void add_median_prediction(uint8_t *dst, uint8_t *src1, uint8_t *diff, int w, int *left, int *left_top){
147 l= mid_pred(l, src1[i], (l + src1[i] - lt)&0xFF) + diff[i];
156 #ifdef CONFIG_ENCODERS
158 static inline void sub_median_prediction(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w, int *left, int *left_top){
166 const int pred= mid_pred(l, src1[i], (l + src1[i] - lt)&0xFF);
176 #endif //CONFIG_ENCODERS
178 static inline void add_left_prediction_bgr32(uint8_t *dst, uint8_t *src, int w, int *red, int *green, int *blue){
200 #ifdef CONFIG_ENCODERS
201 static inline int sub_left_prediction(HYuvContext *s, uint8_t *dst, uint8_t *src, int w, int left){
205 const int temp= src[i];
212 const int temp= src[i];
216 s->dsp.diff_bytes(dst+16, src+16, src+15, w-16);
220 #endif //CONFIG_ENCODERS
221 static void read_len_table(uint8_t *dst, GetBitContext *gb){
225 repeat= get_bits(gb, 3);
226 val = get_bits(gb, 5);
228 repeat= get_bits(gb, 8);
229 //printf("%d %d\n", val, repeat);
235 static int generate_bits_table(uint32_t *dst, uint8_t *len_table){
239 for(len=32; len>0; len--){
240 for(index=0; index<256; index++){
241 if(len_table[index]==len)
245 fprintf(stderr, "Error generating huffman table\n");
253 #ifdef CONFIG_ENCODERS
255 static void generate_len_table(uint8_t *dst, uint64_t *stats, int size){
256 uint64_t counts[2*size];
260 for(offset=1; ; offset<<=1){
261 for(i=0; i<size; i++){
262 counts[i]= stats[i] + offset - 1;
265 for(next=size; next<size*2; next++){
269 min1=min2= INT64_MAX;
272 for(i=0; i<next; i++){
273 if(min2 > counts[i]){
274 if(min1 > counts[i]){
286 if(min2==INT64_MAX) break;
288 counts[next]= min1 + min2;
290 counts[min2_i]= INT64_MAX;
296 for(i=0; i<size; i++){
300 for(len=0; up[index] != -1; len++)
311 #endif //CONFIG_ENCODERS
313 static int read_huffman_tables(HYuvContext *s, uint8_t *src, int length){
317 init_get_bits(&gb, src, length*8);
320 read_len_table(s->len[i], &gb);
322 if(generate_bits_table(s->bits[i], s->len[i])<0){
326 for(j=0; j<256; j++){
327 printf("%6X, %2d, %3d\n", s->bits[i][j], s->len[i][j], j);
330 init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4);
336 static int read_old_huffman_tables(HYuvContext *s){
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);
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];
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));
353 memcpy(s->bits[2], s->bits[1], 256*sizeof(uint32_t));
354 memcpy(s->len[2] , s->len [1], 256*sizeof(uint8_t));
357 init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4);
361 fprintf(stderr, "v1 huffyuv is not supported \n");
366 static int decode_init(AVCodecContext *avctx)
368 HYuvContext *s = avctx->priv_data;
372 s->flags= avctx->flags;
374 dsputil_init(&s->dsp, avctx);
376 width= s->width= avctx->width;
377 height= s->height= avctx->height;
378 avctx->coded_frame= &s->picture;
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?
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;
402 if(read_huffman_tables(s, ((uint8_t*)avctx->extradata)+4, avctx->extradata_size) < 0)
405 switch(avctx->bits_per_sample&7){
416 s->decorrelate= avctx->bits_per_sample >= 24;
419 s->predictor= MEDIAN;
423 s->predictor= LEFT; //OLD
427 s->bitstream_bpp= avctx->bits_per_sample & ~7;
429 if(read_old_huffman_tables(s) < 0)
433 s->interlaced= height > 288;
435 switch(s->bitstream_bpp){
437 avctx->pix_fmt = PIX_FMT_YUV420P;
441 avctx->pix_fmt = PIX_FMT_YUV422;
443 avctx->pix_fmt = PIX_FMT_YUV422P;
449 avctx->pix_fmt = PIX_FMT_RGBA32;
451 avctx->pix_fmt = PIX_FMT_BGR24;
458 // printf("pred:%d bpp:%d hbpp:%d il:%d\n", s->predictor, s->bitstream_bpp, avctx->bits_per_sample, s->interlaced);
463 #ifdef CONFIG_ENCODERS
465 static void store_table(HYuvContext *s, uint8_t *len){
467 int index= s->avctx->extradata_size;
474 for(; i<256 && len[i]==val; i++);
479 ((uint8_t*)s->avctx->extradata)[index++]= val;
480 ((uint8_t*)s->avctx->extradata)[index++]= repeat;
482 ((uint8_t*)s->avctx->extradata)[index++]= val | (repeat<<5);
486 s->avctx->extradata_size= index;
489 static int encode_init(AVCodecContext *avctx)
491 HYuvContext *s = avctx->priv_data;
492 int i, j, width, height;
495 s->flags= avctx->flags;
497 dsputil_init(&s->dsp, avctx);
499 width= s->width= avctx->width;
500 height= s->height= avctx->height;
502 assert(width && height);
504 avctx->extradata= av_mallocz(1024*10);
505 avctx->stats_out= av_mallocz(1024*10);
508 avctx->coded_frame= &s->picture;
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");
516 s->bitstream_bpp= 12;
518 case PIX_FMT_YUV422P:
519 s->bitstream_bpp= 16;
522 fprintf(stderr, "format not supported\n");
525 avctx->bits_per_sample= s->bitstream_bpp;
526 s->decorrelate= s->bitstream_bpp >= 24;
527 s->predictor= avctx->prediction_method;
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;
536 char *p= avctx->stats_in;
546 for(j=0; j<256; j++){
547 s->stats[i][j]+= strtol(p, &next, 0);
548 if(next==p) return -1;
552 if(p[0]==0 || p[1]==0 || p[2]==0) break;
556 for(j=0; j<256; j++){
557 int d= FFMIN(j, 256-j);
559 s->stats[i][j]= 100000000/(d+1);
564 generate_len_table(s->len[i], s->stats[i], 256);
566 if(generate_bits_table(s->bits[i], s->len[i])<0){
570 store_table(s, s->len[i]);
577 s->interlaced= height > 288;
579 // printf("pred:%d bpp:%d hbpp:%d il:%d\n", s->predictor, s->bitstream_bpp, avctx->bits_per_sample, s->interlaced);
586 #endif //CONFIG_ENCODERS
588 static void decode_422_bitstream(HYuvContext *s, int count){
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);
601 static void decode_gray_bitstream(HYuvContext *s, int count){
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);
612 #ifdef CONFIG_ENCODERS
614 static void encode_422_bitstream(HYuvContext *s, int count){
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 ] ]++;
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 ] ]);
635 static void encode_gray_bitstream(HYuvContext *s, int count){
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] ]++;
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] ]);
652 #endif //CONFIG_ENCODERS
654 static void decode_bgr_bitstream(HYuvContext *s, int count){
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];
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); //?!
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);
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); //?!
690 static void draw_slice(HYuvContext *s, int y){
694 if(s->avctx->draw_horiz_band==NULL)
697 h= y - s->last_slice_end;
700 if(s->bitstream_bpp==12){
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;
711 s->avctx->draw_horiz_band(s->avctx, src_ptr, s->picture.linesize[0], y, s->width, h);
713 s->last_slice_end= y + h;
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;
724 AVFrame *picture = data;
728 /* no supplementary picture */
732 s->dsp.bswap_buf((uint32_t*)s->bitstream_buffer, (uint32_t*)buf, buf_size/4);
734 init_get_bits(&s->gb, s->bitstream_buffer, buf_size*8);
737 avctx->release_buffer(avctx, p);
740 if(avctx->get_buffer(avctx, p) < 0){
741 fprintf(stderr, "get_buffer() failed\n");
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];
749 s->last_slice_end= 0;
751 if(s->bitstream_bpp<24){
753 int lefty, leftu, leftv;
754 int lefttopy, lefttopu, lefttopv;
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);
762 fprintf(stderr, "YUY2 output isnt implemenetd yet\n");
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);
771 switch(s->predictor){
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);
781 for(cy=y=1; y<s->height; y++,cy++){
782 uint8_t *ydst, *udst, *vdst;
784 if(s->bitstream_bpp==12){
785 decode_gray_bitstream(s, width);
787 ydst= p->data[0] + p->linesize[0]*y;
789 lefty= add_left_prediction(ydst, s->temp[0], width, lefty);
790 if(s->predictor == PLANE){
792 s->dsp.add_bytes(ydst, ydst - fake_ystride, width);
795 if(y>=s->height) break;
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;
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);
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);
820 draw_slice(s, height);
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);
834 /* second line is left predicted for interlaced case */
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);
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);
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);
865 for(; y<height; y++,cy++){
866 uint8_t *ydst, *udst, *vdst;
868 if(s->bitstream_bpp==12){
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);
879 decode_422_bitstream(s, width);
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;
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);
892 draw_slice(s, height);
898 int leftr, leftg, leftb;
899 const int last_line= (height-1)*p->linesize[0];
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);
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);
914 switch(s->predictor){
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);
920 for(y=s->height-2; y>=0; y--){ //yes its stored upside down
921 decode_bgr_bitstream(s, width);
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);
931 draw_slice(s, height); // just 1 large slice as this isnt possible in reverse order
934 fprintf(stderr, "prediction type not supported!\n");
938 fprintf(stderr, "BGR24 output isnt implemenetd yet\n");
945 *data_size = sizeof(AVFrame);
947 return (get_bits_count(&s->gb)+31)/32*4;
950 static int decode_end(AVCodecContext *avctx)
952 HYuvContext *s = avctx->priv_data;
956 free_vlc(&s->vlc[i]);
959 avcodec_default_free_buffers(avctx);
964 #ifdef CONFIG_ENCODERS
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;
978 init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
981 p->pict_type= FF_I_TYPE;
984 if(avctx->pix_fmt == PIX_FMT_YUV422P || avctx->pix_fmt == PIX_FMT_YUV420P){
985 int lefty, leftu, leftv, y, cy;
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]);
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);
996 encode_422_bitstream(s, width-2);
998 if(s->predictor==MEDIAN){
999 int lefttopy, lefttopu, lefttopv;
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);
1006 encode_422_bitstream(s, width);
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);
1014 encode_422_bitstream(s, 4);
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);
1025 for(; y<height; y++,cy++){
1026 uint8_t *ydst, *udst, *vdst;
1028 if(s->bitstream_bpp==12){
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);
1035 if(y>=height) break;
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;
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);
1045 encode_422_bitstream(s, width);
1048 for(cy=y=1; y<height; y++,cy++){
1049 uint8_t *ydst, *udst, *vdst;
1051 /* encode a luma only line & y++ */
1052 if(s->bitstream_bpp==12){
1053 ydst= p->data[0] + p->linesize[0]*y;
1055 if(s->predictor == PLANE && s->interlaced < y){
1056 s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
1058 lefty= sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
1060 lefty= sub_left_prediction(s, s->temp[0], ydst, width , lefty);
1062 encode_gray_bitstream(s, width);
1064 if(y>=height) break;
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;
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);
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);
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);
1085 encode_422_bitstream(s, width);
1089 fprintf(stderr, "Format not supported!\n");
1093 size= (get_bit_count(&s->pb)+31)/32;
1095 if((s->flags&CODEC_FLAG_PASS1) && (s->picture_number&31)==0){
1097 char *p= avctx->stats_out;
1099 for(j=0; j<256; j++){
1100 sprintf(p, "%Ld ", s->stats[i][j]);
1108 flush_put_bits(&s->pb);
1109 s->dsp.bswap_buf((uint32_t*)buf, (uint32_t*)buf, size);
1112 s->picture_number++;
1117 static int encode_end(AVCodecContext *avctx)
1119 // HYuvContext *s = avctx->priv_data;
1121 av_freep(&avctx->extradata);
1122 av_freep(&avctx->stats_out);
1127 #endif //CONFIG_ENCODERS
1129 static const AVOption huffyuv_options[] =
1131 AVOPTION_CODEC_INT("prediction_method", "prediction_method", prediction_method, 0, 2, 0),
1135 AVCodec huffyuv_decoder = {
1139 sizeof(HYuvContext),
1144 CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND,
1148 #ifdef CONFIG_ENCODERS
1150 AVCodec huffyuv_encoder = {
1154 sizeof(HYuvContext),
1158 .options = huffyuv_options,
1161 #endif //CONFIG_ENCODERS