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