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