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