Mercurial > libavcodec.hg
annotate huffyuv.c @ 870:2781c060bb35 libavcodec
dr1
author | michaelni |
---|---|
date | Fri, 15 Nov 2002 00:07:06 +0000 |
parents | 028638e65523 |
children | efe2d41df15e |
rev | line source |
---|---|
866 | 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 | |
869 | 28 #ifndef INT64_MAX |
29 #define INT64_MAX 9223372036854775807LL | |
866 | 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]= | |
869 | 240 counts[min2_i]= INT64_MAX; |
866 | 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){ | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
331 if((avctx->bits_per_sample&7) && avctx->bits_per_sample != 12) |
866 | 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 | |
870 | 415 if(!(avctx->flags&CODEC_FLAG_DR1)){ |
416 s->linesize[0]= stride; | |
417 s->picture[0]= av_mallocz(y_size); | |
866 | 418 |
870 | 419 if(c_size){ |
420 s->picture[1]= av_mallocz(c_size); | |
421 s->picture[2]= av_mallocz(c_size); | |
422 s->linesize[1]= s->linesize[2]= stride/2; | |
866 | 423 |
870 | 424 memset(s->picture[1], 128, c_size); |
425 memset(s->picture[2], 128, c_size); | |
426 } | |
866 | 427 } |
428 | |
429 // printf("pred:%d bpp:%d hbpp:%d il:%d\n", s->predictor, s->bitstream_bpp, avctx->bits_per_sample, s->interlaced); | |
430 | |
431 return 0; | |
432 } | |
433 | |
434 static void store_table(HYuvContext *s, uint8_t *len){ | |
435 int i; | |
436 int index= s->avctx->extradata_size; | |
437 | |
438 for(i=0; i<256;){ | |
439 int cur=i; | |
440 int val= len[i]; | |
441 int repeat; | |
442 | |
443 for(; i<256 && len[i]==val; i++); | |
444 | |
445 repeat= i - cur; | |
446 | |
447 if(repeat>7){ | |
448 ((uint8_t*)s->avctx->extradata)[index++]= val; | |
449 ((uint8_t*)s->avctx->extradata)[index++]= repeat; | |
450 }else{ | |
451 ((uint8_t*)s->avctx->extradata)[index++]= val | (repeat<<5); | |
452 } | |
453 } | |
454 | |
455 s->avctx->extradata_size= index; | |
456 } | |
457 | |
458 static int encode_init(AVCodecContext *avctx) | |
459 { | |
460 HYuvContext *s = avctx->priv_data; | |
461 int i, j, width, height; | |
462 | |
463 s->avctx= avctx; | |
464 s->flags= avctx->flags; | |
465 | |
466 dsputil_init(&s->dsp, avctx->dsp_mask); | |
467 | |
468 width= s->width= avctx->width; | |
469 height= s->height= avctx->height; | |
470 | |
471 assert(width && height); | |
472 | |
473 avctx->extradata= av_mallocz(1024*10); | |
474 avctx->stats_out= av_mallocz(1024*10); | |
475 s->version=2; | |
476 | |
477 switch(avctx->pix_fmt){ | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
478 case PIX_FMT_YUV420P: |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
479 if(avctx->strict_std_compliance>=0){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
480 fprintf(stderr, "YV12-huffyuv is experimental, there WILL be no compatbility! (use (v)strict=-1)\n"); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
481 return -1; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
482 } |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
483 s->bitstream_bpp= 12; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
484 break; |
866 | 485 case PIX_FMT_YUV422P: |
486 s->bitstream_bpp= 16; | |
487 break; | |
488 default: | |
489 fprintf(stderr, "format not supported\n"); | |
490 return -1; | |
491 } | |
492 avctx->bits_per_sample= s->bitstream_bpp; | |
493 s->decorrelate= s->bitstream_bpp >= 24; | |
494 s->predictor= avctx->prediction_method; | |
495 | |
496 ((uint8_t*)avctx->extradata)[0]= s->predictor; | |
497 ((uint8_t*)avctx->extradata)[1]= s->bitstream_bpp; | |
498 ((uint8_t*)avctx->extradata)[2]= | |
499 ((uint8_t*)avctx->extradata)[3]= 0; | |
500 s->avctx->extradata_size= 4; | |
501 | |
502 if(avctx->stats_in){ | |
503 char *p= avctx->stats_in; | |
504 | |
505 for(i=0; i<3; i++) | |
506 for(j=0; j<256; j++) | |
507 s->stats[i][j]= 1; | |
508 | |
509 for(;;){ | |
510 for(i=0; i<3; i++){ | |
511 char *next; | |
512 | |
513 for(j=0; j<256; j++){ | |
514 s->stats[i][j]+= strtol(p, &next, 0); | |
515 if(next==p) return -1; | |
516 p=next; | |
517 } | |
518 } | |
519 if(p[0]==0 || p[1]==0 || p[2]==0) break; | |
520 } | |
521 }else{ | |
522 for(i=0; i<3; i++) | |
523 for(j=0; j<256; j++){ | |
524 int d= FFMIN(j, 256-j); | |
525 | |
526 s->stats[i][j]= 100000000/(d+1); | |
527 } | |
528 } | |
529 | |
530 for(i=0; i<3; i++){ | |
531 generate_len_table(s->len[i], s->stats[i], 256); | |
532 | |
533 if(generate_bits_table(s->bits[i], s->len[i])<0){ | |
534 return -1; | |
535 } | |
536 | |
537 store_table(s, s->len[i]); | |
538 } | |
539 | |
540 for(i=0; i<3; i++) | |
541 for(j=0; j<256; j++) | |
542 s->stats[i][j]= 0; | |
543 | |
544 s->interlaced= height > 288; | |
545 | |
546 // printf("pred:%d bpp:%d hbpp:%d il:%d\n", s->predictor, s->bitstream_bpp, avctx->bits_per_sample, s->interlaced); | |
547 | |
548 s->picture_number=0; | |
549 | |
550 return 0; | |
551 } | |
552 | |
553 static void decode_422_bitstream(HYuvContext *s, int count){ | |
554 int i; | |
555 | |
556 count/=2; | |
557 | |
558 for(i=0; i<count; i++){ | |
559 s->temp[0][2*i ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); | |
560 s->temp[1][ i ]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); | |
561 s->temp[0][2*i+1]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); | |
562 s->temp[2][ i ]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); | |
563 } | |
564 } | |
565 | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
566 static void decode_gray_bitstream(HYuvContext *s, int count){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
567 int i; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
568 |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
569 count/=2; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
570 |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
571 for(i=0; i<count; i++){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
572 s->temp[0][2*i ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
573 s->temp[0][2*i+1]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
574 } |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
575 } |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
576 |
866 | 577 static void encode_422_bitstream(HYuvContext *s, int count){ |
578 int i; | |
579 | |
580 count/=2; | |
581 if(s->flags&CODEC_FLAG_PASS1){ | |
582 for(i=0; i<count; i++){ | |
583 s->stats[0][ s->temp[0][2*i ] ]++; | |
584 s->stats[1][ s->temp[1][ i ] ]++; | |
585 s->stats[0][ s->temp[0][2*i+1] ]++; | |
586 s->stats[2][ s->temp[2][ i ] ]++; | |
587 } | |
588 }else{ | |
589 for(i=0; i<count; i++){ | |
590 put_bits(&s->pb, s->len[0][ s->temp[0][2*i ] ], s->bits[0][ s->temp[0][2*i ] ]); | |
591 put_bits(&s->pb, s->len[1][ s->temp[1][ i ] ], s->bits[1][ s->temp[1][ i ] ]); | |
592 put_bits(&s->pb, s->len[0][ s->temp[0][2*i+1] ], s->bits[0][ s->temp[0][2*i+1] ]); | |
593 put_bits(&s->pb, s->len[2][ s->temp[2][ i ] ], s->bits[2][ s->temp[2][ i ] ]); | |
594 } | |
595 } | |
596 } | |
597 | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
598 static void encode_gray_bitstream(HYuvContext *s, int count){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
599 int i; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
600 |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
601 count/=2; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
602 if(s->flags&CODEC_FLAG_PASS1){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
603 for(i=0; i<count; i++){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
604 s->stats[0][ s->temp[0][2*i ] ]++; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
605 s->stats[0][ s->temp[0][2*i+1] ]++; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
606 } |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
607 }else{ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
608 for(i=0; i<count; i++){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
609 put_bits(&s->pb, s->len[0][ s->temp[0][2*i ] ], s->bits[0][ s->temp[0][2*i ] ]); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
610 put_bits(&s->pb, s->len[0][ s->temp[0][2*i+1] ], s->bits[0][ s->temp[0][2*i+1] ]); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
611 } |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
612 } |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
613 } |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
614 |
866 | 615 static void decode_bgr_bitstream(HYuvContext *s, int count){ |
616 int i; | |
617 | |
618 if(s->decorrelate){ | |
619 if(s->bitstream_bpp==24){ | |
620 for(i=0; i<count; i++){ | |
621 s->temp[0][4*i+1]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); | |
622 s->temp[0][4*i ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3) + s->temp[0][4*i+1]; | |
623 s->temp[0][4*i+2]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3) + s->temp[0][4*i+1]; | |
624 } | |
625 }else{ | |
626 for(i=0; i<count; i++){ | |
627 s->temp[0][4*i+1]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); | |
628 s->temp[0][4*i ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3) + s->temp[0][4*i+1]; | |
629 s->temp[0][4*i+2]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3) + s->temp[0][4*i+1]; | |
630 get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); //?! | |
631 } | |
632 } | |
633 }else{ | |
634 if(s->bitstream_bpp==24){ | |
635 for(i=0; i<count; i++){ | |
636 s->temp[0][4*i ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); | |
637 s->temp[0][4*i+1]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); | |
638 s->temp[0][4*i+2]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); | |
639 } | |
640 }else{ | |
641 for(i=0; i<count; i++){ | |
642 s->temp[0][4*i ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); | |
643 s->temp[0][4*i+1]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); | |
644 s->temp[0][4*i+2]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); | |
645 get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); //?! | |
646 } | |
647 } | |
648 } | |
649 } | |
650 | |
651 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){ | |
652 HYuvContext *s = avctx->priv_data; | |
653 const int width= s->width; | |
654 const int width2= s->width>>1; | |
655 const int height= s->height; | |
870 | 656 int fake_ystride, fake_ustride, fake_vstride; |
866 | 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); | |
870 | 670 |
671 if(avctx->flags&CODEC_FLAG_DR1){ | |
672 if(avctx->get_buffer_callback(avctx, s->width, s->height, I_TYPE) < 0){ | |
673 fprintf(stderr, "get_buffer() failed\n"); | |
674 return -1; | |
675 } | |
676 | |
677 s->linesize[0]= avctx->dr_stride; | |
678 s->linesize[1]= | |
679 s->linesize[2]= avctx->dr_uvstride; | |
680 | |
681 for(i=0; i<3;i++) | |
682 s->picture[i]= avctx->dr_buffer[i]; | |
683 } | |
684 fake_ystride= s->interlaced ? s->linesize[0]*2 : s->linesize[0]; | |
685 fake_ustride= s->interlaced ? s->linesize[1]*2 : s->linesize[1]; | |
686 fake_vstride= s->interlaced ? s->linesize[2]*2 : s->linesize[2]; | |
687 | |
866 | 688 if(s->bitstream_bpp<24){ |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
689 int y, cy; |
866 | 690 int lefty, leftu, leftv; |
691 int lefttopy, lefttopu, lefttopv; | |
692 | |
693 if(s->yuy2){ | |
694 s->picture[0][3]= get_bits(&s->gb, 8); | |
695 s->picture[0][2]= get_bits(&s->gb, 8); | |
696 s->picture[0][1]= get_bits(&s->gb, 8); | |
697 s->picture[0][0]= get_bits(&s->gb, 8); | |
698 | |
699 fprintf(stderr, "YUY2 output isnt implemenetd yet\n"); | |
700 return -1; | |
701 }else{ | |
702 | |
703 leftv= s->picture[2][0]= get_bits(&s->gb, 8); | |
704 lefty= s->picture[0][1]= get_bits(&s->gb, 8); | |
705 leftu= s->picture[1][0]= get_bits(&s->gb, 8); | |
706 s->picture[0][0]= get_bits(&s->gb, 8); | |
707 | |
708 switch(s->predictor){ | |
709 case LEFT: | |
710 case PLANE: | |
711 decode_422_bitstream(s, width-2); | |
712 lefty= add_left_prediction(s->picture[0] + 2, s->temp[0], width-2, lefty); | |
713 if(!(s->flags&CODEC_FLAG_GRAY)){ | |
714 leftu= add_left_prediction(s->picture[1] + 1, s->temp[1], width2-1, leftu); | |
715 leftv= add_left_prediction(s->picture[2] + 1, s->temp[2], width2-1, leftv); | |
716 } | |
717 | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
718 for(cy=y=1; y<s->height; y++,cy++){ |
866 | 719 uint8_t *ydst, *udst, *vdst; |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
720 |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
721 if(s->bitstream_bpp==12){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
722 decode_gray_bitstream(s, width); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
723 |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
724 ydst= s->picture[0] + s->linesize[0]*y; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
725 |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
726 lefty= add_left_prediction(ydst, s->temp[0], width, lefty); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
727 if(s->predictor == PLANE){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
728 if(y>s->interlaced) |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
729 s->dsp.add_bytes(ydst, ydst - fake_ystride, width); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
730 } |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
731 y++; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
732 if(y>=s->height) break; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
733 } |
866 | 734 |
735 ydst= s->picture[0] + s->linesize[0]*y; | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
736 udst= s->picture[1] + s->linesize[1]*cy; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
737 vdst= s->picture[2] + s->linesize[2]*cy; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
738 |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
739 decode_422_bitstream(s, width); |
866 | 740 |
741 lefty= add_left_prediction(ydst, s->temp[0], width, lefty); | |
742 if(!(s->flags&CODEC_FLAG_GRAY)){ | |
743 leftu= add_left_prediction(udst, s->temp[1], width2, leftu); | |
744 leftv= add_left_prediction(vdst, s->temp[2], width2, leftv); | |
745 } | |
746 if(s->predictor == PLANE){ | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
747 if(cy>s->interlaced){ |
866 | 748 s->dsp.add_bytes(ydst, ydst - fake_ystride, width); |
749 if(!(s->flags&CODEC_FLAG_GRAY)){ | |
750 s->dsp.add_bytes(udst, udst - fake_ustride, width2); | |
751 s->dsp.add_bytes(vdst, vdst - fake_vstride, width2); | |
752 } | |
753 } | |
754 } | |
755 } | |
756 break; | |
757 case MEDIAN: | |
758 /* first line except first 2 pixels is left predicted */ | |
759 decode_422_bitstream(s, width-2); | |
760 lefty= add_left_prediction(s->picture[0] + 2, s->temp[0], width-2, lefty); | |
761 if(!(s->flags&CODEC_FLAG_GRAY)){ | |
762 leftu= add_left_prediction(s->picture[1] + 1, s->temp[1], width2-1, leftu); | |
763 leftv= add_left_prediction(s->picture[2] + 1, s->temp[2], width2-1, leftv); | |
764 } | |
765 | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
766 cy=y=1; |
866 | 767 |
768 /* second line is left predicted for interlaced case */ | |
769 if(s->interlaced){ | |
770 decode_422_bitstream(s, width); | |
771 lefty= add_left_prediction(s->picture[0] + s->linesize[0], s->temp[0], width, lefty); | |
772 if(!(s->flags&CODEC_FLAG_GRAY)){ | |
773 leftu= add_left_prediction(s->picture[1] + s->linesize[2], s->temp[1], width2, leftu); | |
774 leftv= add_left_prediction(s->picture[2] + s->linesize[1], s->temp[2], width2, leftv); | |
775 } | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
776 y++; cy++; |
866 | 777 } |
778 | |
779 /* next 4 pixels are left predicted too */ | |
780 decode_422_bitstream(s, 4); | |
781 lefty= add_left_prediction(s->picture[0] + fake_ystride, s->temp[0], 4, lefty); | |
782 if(!(s->flags&CODEC_FLAG_GRAY)){ | |
783 leftu= add_left_prediction(s->picture[1] + fake_ustride, s->temp[1], 2, leftu); | |
784 leftv= add_left_prediction(s->picture[2] + fake_vstride, s->temp[2], 2, leftv); | |
785 } | |
786 | |
787 /* next line except the first 4 pixels is median predicted */ | |
788 lefttopy= s->picture[0][3]; | |
789 decode_422_bitstream(s, width-4); | |
790 add_median_prediction(s->picture[0] + fake_ystride+4, s->picture[0]+4, s->temp[0], width-4, &lefty, &lefttopy); | |
791 if(!(s->flags&CODEC_FLAG_GRAY)){ | |
792 lefttopu= s->picture[1][1]; | |
793 lefttopv= s->picture[2][1]; | |
794 add_median_prediction(s->picture[1] + fake_ustride+2, s->picture[1]+2, s->temp[1], width2-2, &leftu, &lefttopu); | |
795 add_median_prediction(s->picture[2] + fake_vstride+2, s->picture[2]+2, s->temp[2], width2-2, &leftv, &lefttopv); | |
796 } | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
797 y++; cy++; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
798 |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
799 for(; y<height; y++,cy++){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
800 uint8_t *ydst, *udst, *vdst; |
866 | 801 |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
802 if(s->bitstream_bpp==12){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
803 while(2*cy > y){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
804 decode_gray_bitstream(s, width); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
805 ydst= s->picture[0] + s->linesize[0]*y; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
806 add_median_prediction(ydst, ydst - fake_ystride, s->temp[0], width, &lefty, &lefttopy); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
807 y++; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
808 } |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
809 if(y>=height) break; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
810 } |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
811 |
866 | 812 decode_422_bitstream(s, width); |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
813 |
866 | 814 ydst= s->picture[0] + s->linesize[0]*y; |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
815 udst= s->picture[1] + s->linesize[1]*cy; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
816 vdst= s->picture[2] + s->linesize[2]*cy; |
866 | 817 |
818 add_median_prediction(ydst, ydst - fake_ystride, s->temp[0], width, &lefty, &lefttopy); | |
819 if(!(s->flags&CODEC_FLAG_GRAY)){ | |
820 add_median_prediction(udst, udst - fake_ustride, s->temp[1], width2, &leftu, &lefttopu); | |
821 add_median_prediction(vdst, vdst - fake_vstride, s->temp[2], width2, &leftv, &lefttopv); | |
822 } | |
823 } | |
824 break; | |
825 } | |
826 } | |
827 }else{ | |
828 int y; | |
829 int leftr, leftg, leftb; | |
830 const int last_line= (height-1)*s->linesize[0]; | |
831 | |
832 if(s->bitstream_bpp==32){ | |
833 s->picture[0][last_line+3]= get_bits(&s->gb, 8); | |
834 leftr= s->picture[0][last_line+2]= get_bits(&s->gb, 8); | |
835 leftg= s->picture[0][last_line+1]= get_bits(&s->gb, 8); | |
836 leftb= s->picture[0][last_line+0]= get_bits(&s->gb, 8); | |
837 }else{ | |
838 leftr= s->picture[0][last_line+2]= get_bits(&s->gb, 8); | |
839 leftg= s->picture[0][last_line+1]= get_bits(&s->gb, 8); | |
840 leftb= s->picture[0][last_line+0]= get_bits(&s->gb, 8); | |
841 skip_bits(&s->gb, 8); | |
842 } | |
843 | |
844 if(s->bgr32){ | |
845 switch(s->predictor){ | |
846 case LEFT: | |
847 case PLANE: | |
848 decode_bgr_bitstream(s, width-1); | |
849 add_left_prediction_bgr32(s->picture[0] + last_line+4, s->temp[0], width-1, &leftr, &leftg, &leftb); | |
850 | |
851 for(y=s->height-2; y>=0; y--){ //yes its stored upside down | |
852 decode_bgr_bitstream(s, width); | |
853 | |
854 add_left_prediction_bgr32(s->picture[0] + s->linesize[0]*y, s->temp[0], width, &leftr, &leftg, &leftb); | |
855 if(s->predictor == PLANE){ | |
856 if((y&s->interlaced)==0){ | |
857 s->dsp.add_bytes(s->picture[0] + s->linesize[0]*y, | |
858 s->picture[0] + s->linesize[0]*y + fake_ystride, fake_ystride); | |
859 } | |
860 } | |
861 } | |
862 break; | |
863 default: | |
864 fprintf(stderr, "prediction type not supported!\n"); | |
865 } | |
866 }else{ | |
867 | |
868 fprintf(stderr, "BGR24 output isnt implemenetd yet\n"); | |
869 return -1; | |
870 } | |
871 } | |
872 emms_c(); | |
873 | |
874 for(i=0;i<3;i++) { | |
875 picture->data[i] = s->picture[i]; | |
876 picture->linesize[i]= s->linesize[i]; | |
877 } | |
878 | |
879 *data_size = sizeof(AVPicture); | |
880 | |
881 return (get_bits_count(&s->gb)+7)>>3; | |
882 } | |
883 | |
884 static int decode_end(AVCodecContext *avctx) | |
885 { | |
886 HYuvContext *s = avctx->priv_data; | |
887 int i; | |
888 | |
889 for(i=0; i<3; i++){ | |
870 | 890 if(!(avctx->flags&CODEC_FLAG_DR1)) |
891 av_freep(&s->picture[i]); | |
892 | |
866 | 893 free_vlc(&s->vlc[i]); |
894 } | |
895 | |
896 return 0; | |
897 } | |
898 | |
899 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){ | |
900 HYuvContext *s = avctx->priv_data; | |
901 AVPicture *pict = data; | |
902 const int width= s->width; | |
903 const int width2= s->width>>1; | |
904 const int height= s->height; | |
905 const int fake_ystride= s->interlaced ? pict->linesize[0]*2 : pict->linesize[0]; | |
906 const int fake_ustride= s->interlaced ? pict->linesize[1]*2 : pict->linesize[1]; | |
907 const int fake_vstride= s->interlaced ? pict->linesize[2]*2 : pict->linesize[2]; | |
908 int i, size; | |
909 | |
910 init_put_bits(&s->pb, buf, buf_size, NULL, NULL); | |
911 | |
912 for(i=0; i<3; i++){ | |
913 s->picture[i]= pict->data[i]; | |
914 s->linesize[i]= pict->linesize[i]; | |
915 } | |
916 | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
917 if(avctx->pix_fmt == PIX_FMT_YUV422P || avctx->pix_fmt == PIX_FMT_YUV420P){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
918 int lefty, leftu, leftv, y, cy; |
866 | 919 |
920 put_bits(&s->pb, 8, leftv= s->picture[2][0]); | |
921 put_bits(&s->pb, 8, lefty= s->picture[0][1]); | |
922 put_bits(&s->pb, 8, leftu= s->picture[1][0]); | |
923 put_bits(&s->pb, 8, s->picture[0][0]); | |
924 | |
925 lefty= sub_left_prediction(s->temp[0], s->picture[0]+2, width-2 , lefty); | |
926 leftu= sub_left_prediction(s->temp[1], s->picture[1]+1, width2-1, leftu); | |
927 leftv= sub_left_prediction(s->temp[2], s->picture[2]+1, width2-1, leftv); | |
928 | |
929 encode_422_bitstream(s, width-2); | |
930 | |
931 if(s->predictor==MEDIAN){ | |
932 int lefttopy, lefttopu, lefttopv; | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
933 cy=y=1; |
866 | 934 if(s->interlaced){ |
935 lefty= sub_left_prediction(s->temp[0], s->picture[0]+s->linesize[0], width , lefty); | |
936 leftu= sub_left_prediction(s->temp[1], s->picture[1]+s->linesize[1], width2, leftu); | |
937 leftv= sub_left_prediction(s->temp[2], s->picture[2]+s->linesize[2], width2, leftv); | |
938 | |
939 encode_422_bitstream(s, width); | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
940 y++; cy++; |
866 | 941 } |
942 | |
943 lefty= sub_left_prediction(s->temp[0], s->picture[0]+fake_ystride, 4, lefty); | |
944 leftu= sub_left_prediction(s->temp[1], s->picture[1]+fake_ystride, 2, leftu); | |
945 leftv= sub_left_prediction(s->temp[2], s->picture[2]+fake_ystride, 2, leftv); | |
946 | |
947 encode_422_bitstream(s, 4); | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
948 |
866 | 949 lefttopy= s->picture[0][3]; |
950 lefttopu= s->picture[1][1]; | |
951 lefttopv= s->picture[2][1]; | |
952 sub_median_prediction(s->temp[0], s->picture[0]+4, s->picture[0] + fake_ystride+4, width-4 , &lefty, &lefttopy); | |
953 sub_median_prediction(s->temp[1], s->picture[1]+2, s->picture[1] + fake_ustride+2, width2-2, &leftu, &lefttopu); | |
954 sub_median_prediction(s->temp[2], s->picture[2]+2, s->picture[2] + fake_vstride+2, width2-2, &leftv, &lefttopv); | |
955 encode_422_bitstream(s, width-4); | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
956 y++; cy++; |
866 | 957 |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
958 for(; y<height; y++,cy++){ |
866 | 959 uint8_t *ydst, *udst, *vdst; |
960 | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
961 if(s->bitstream_bpp==12){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
962 while(2*cy > y){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
963 ydst= s->picture[0] + s->linesize[0]*y; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
964 sub_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
965 encode_gray_bitstream(s, width); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
966 y++; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
967 } |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
968 if(y>=height) break; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
969 } |
866 | 970 ydst= s->picture[0] + s->linesize[0]*y; |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
971 udst= s->picture[1] + s->linesize[1]*cy; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
972 vdst= s->picture[2] + s->linesize[2]*cy; |
866 | 973 |
974 sub_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy); | |
975 sub_median_prediction(s->temp[1], udst - fake_ustride, udst, width2, &leftu, &lefttopu); | |
976 sub_median_prediction(s->temp[2], vdst - fake_vstride, vdst, width2, &leftv, &lefttopv); | |
977 | |
978 encode_422_bitstream(s, width); | |
979 } | |
980 }else{ | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
981 for(cy=y=1; y<height; y++,cy++){ |
866 | 982 uint8_t *ydst, *udst, *vdst; |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
983 |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
984 /* encode a luma only line & y++ */ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
985 if(s->bitstream_bpp==12){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
986 ydst= s->picture[0] + s->linesize[0]*y; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
987 |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
988 if(s->predictor == PLANE && s->interlaced < y){ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
989 s->dsp.diff_bytes(s->temp[0], ydst, ydst - fake_ystride, width); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
990 |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
991 lefty= sub_left_prediction(s->temp[0], s->temp[0], width , lefty); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
992 }else{ |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
993 lefty= sub_left_prediction(s->temp[0], ydst, width , lefty); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
994 } |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
995 encode_gray_bitstream(s, width); |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
996 y++; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
997 if(y>=height) break; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
998 } |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
999 |
866 | 1000 ydst= s->picture[0] + s->linesize[0]*y; |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1001 udst= s->picture[1] + s->linesize[1]*cy; |
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1002 vdst= s->picture[2] + s->linesize[2]*cy; |
866 | 1003 |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1004 if(s->predictor == PLANE && s->interlaced < cy){ |
866 | 1005 s->dsp.diff_bytes(s->temp[0], ydst, ydst - fake_ystride, width); |
1006 s->dsp.diff_bytes(s->temp[1], udst, udst - fake_ustride, width2); | |
1007 s->dsp.diff_bytes(s->temp[2], vdst, vdst - fake_vstride, width2); | |
1008 | |
1009 lefty= sub_left_prediction(s->temp[0], s->temp[0], width , lefty); | |
1010 leftu= sub_left_prediction(s->temp[1], s->temp[1], width2, leftu); | |
1011 leftv= sub_left_prediction(s->temp[2], s->temp[2], width2, leftv); | |
1012 }else{ | |
1013 lefty= sub_left_prediction(s->temp[0], ydst, width , lefty); | |
1014 leftu= sub_left_prediction(s->temp[1], udst, width2, leftu); | |
1015 leftv= sub_left_prediction(s->temp[2], vdst, width2, leftv); | |
1016 } | |
1017 | |
1018 encode_422_bitstream(s, width); | |
1019 } | |
1020 } | |
1021 }else{ | |
1022 fprintf(stderr, "Format not supported!\n"); | |
1023 } | |
1024 emms_c(); | |
1025 | |
1026 size= (get_bit_count(&s->pb)+31)/32; | |
1027 | |
1028 if((s->flags&CODEC_FLAG_PASS1) && (s->picture_number&31)==0){ | |
1029 int j; | |
1030 char *p= avctx->stats_out; | |
1031 for(i=0; i<3; i++){ | |
1032 for(j=0; j<256; j++){ | |
1033 sprintf(p, "%Ld ", s->stats[i][j]); | |
1034 p+= strlen(p); | |
1035 s->stats[i][j]= 0; | |
1036 } | |
1037 sprintf(p, "\n"); | |
1038 p++; | |
1039 } | |
1040 }else{ | |
868
dfe7d66da6f0
YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain)
michaelni
parents:
866
diff
changeset
|
1041 flush_put_bits(&s->pb); |
866 | 1042 bswap_buf((uint32_t*)buf, (uint32_t*)buf, size); |
1043 } | |
1044 | |
1045 avctx->key_frame= 1; | |
1046 avctx->pict_type= I_TYPE; | |
1047 | |
1048 s->picture_number++; | |
1049 | |
1050 return size*4; | |
1051 } | |
1052 | |
1053 static int encode_end(AVCodecContext *avctx) | |
1054 { | |
1055 // HYuvContext *s = avctx->priv_data; | |
1056 | |
1057 av_freep(&avctx->extradata); | |
1058 av_freep(&avctx->stats_out); | |
1059 | |
1060 return 0; | |
1061 } | |
1062 | |
1063 AVCodec huffyuv_decoder = { | |
1064 "huffyuv", | |
1065 CODEC_TYPE_VIDEO, | |
1066 CODEC_ID_HUFFYUV, | |
1067 sizeof(HYuvContext), | |
1068 decode_init, | |
1069 NULL, | |
1070 decode_end, | |
1071 decode_frame, | |
870 | 1072 CODEC_CAP_DR1, |
866 | 1073 NULL |
1074 }; | |
1075 | |
1076 AVCodec huffyuv_encoder = { | |
1077 "huffyuv", | |
1078 CODEC_TYPE_VIDEO, | |
1079 CODEC_ID_HUFFYUV, | |
1080 sizeof(HYuvContext), | |
1081 encode_init, | |
1082 encode_frame, | |
1083 encode_end, | |
1084 }; |