00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00031 #include "avcodec.h"
00032 #include "bitstream.h"
00033 #include "dsputil.h"
00034
00035 #define VLC_BITS 11
00036
00037 #ifdef WORDS_BIGENDIAN
00038 #define B 3
00039 #define G 2
00040 #define R 1
00041 #else
00042 #define B 0
00043 #define G 1
00044 #define R 2
00045 #endif
00046
00047 typedef enum Predictor{
00048 LEFT= 0,
00049 PLANE,
00050 MEDIAN,
00051 } Predictor;
00052
00053 typedef struct HYuvContext{
00054 AVCodecContext *avctx;
00055 Predictor predictor;
00056 GetBitContext gb;
00057 PutBitContext pb;
00058 int interlaced;
00059 int decorrelate;
00060 int bitstream_bpp;
00061 int version;
00062 int yuy2;
00063 int bgr32;
00064 int width, height;
00065 int flags;
00066 int context;
00067 int picture_number;
00068 int last_slice_end;
00069 uint8_t *temp[3];
00070 uint64_t stats[3][256];
00071 uint8_t len[3][256];
00072 uint32_t bits[3][256];
00073 uint32_t pix_bgr_map[1<<VLC_BITS];
00074 VLC vlc[6];
00075 AVFrame picture;
00076 uint8_t *bitstream_buffer;
00077 unsigned int bitstream_buffer_size;
00078 DSPContext dsp;
00079 }HYuvContext;
00080
00081 static const unsigned char classic_shift_luma[] = {
00082 34,36,35,69,135,232,9,16,10,24,11,23,12,16,13,10,14,8,15,8,
00083 16,8,17,20,16,10,207,206,205,236,11,8,10,21,9,23,8,8,199,70,
00084 69,68, 0
00085 };
00086
00087 static const unsigned char classic_shift_chroma[] = {
00088 66,36,37,38,39,40,41,75,76,77,110,239,144,81,82,83,84,85,118,183,
00089 56,57,88,89,56,89,154,57,58,57,26,141,57,56,58,57,58,57,184,119,
00090 214,245,116,83,82,49,80,79,78,77,44,75,41,40,39,38,37,36,34, 0
00091 };
00092
00093 static const unsigned char classic_add_luma[256] = {
00094 3, 9, 5, 12, 10, 35, 32, 29, 27, 50, 48, 45, 44, 41, 39, 37,
00095 73, 70, 68, 65, 64, 61, 58, 56, 53, 50, 49, 46, 44, 41, 38, 36,
00096 68, 65, 63, 61, 58, 55, 53, 51, 48, 46, 45, 43, 41, 39, 38, 36,
00097 35, 33, 32, 30, 29, 27, 26, 25, 48, 47, 46, 44, 43, 41, 40, 39,
00098 37, 36, 35, 34, 32, 31, 30, 28, 27, 26, 24, 23, 22, 20, 19, 37,
00099 35, 34, 33, 31, 30, 29, 27, 26, 24, 23, 21, 20, 18, 17, 15, 29,
00100 27, 26, 24, 22, 21, 19, 17, 16, 14, 26, 25, 23, 21, 19, 18, 16,
00101 15, 27, 25, 23, 21, 19, 17, 16, 14, 26, 25, 23, 21, 18, 17, 14,
00102 12, 17, 19, 13, 4, 9, 2, 11, 1, 7, 8, 0, 16, 3, 14, 6,
00103 12, 10, 5, 15, 18, 11, 10, 13, 15, 16, 19, 20, 22, 24, 27, 15,
00104 18, 20, 22, 24, 26, 14, 17, 20, 22, 24, 27, 15, 18, 20, 23, 25,
00105 28, 16, 19, 22, 25, 28, 32, 36, 21, 25, 29, 33, 38, 42, 45, 49,
00106 28, 31, 34, 37, 40, 42, 44, 47, 49, 50, 52, 54, 56, 57, 59, 60,
00107 62, 64, 66, 67, 69, 35, 37, 39, 40, 42, 43, 45, 47, 48, 51, 52,
00108 54, 55, 57, 59, 60, 62, 63, 66, 67, 69, 71, 72, 38, 40, 42, 43,
00109 46, 47, 49, 51, 26, 28, 30, 31, 33, 34, 18, 19, 11, 13, 7, 8,
00110 };
00111
00112 static const unsigned char classic_add_chroma[256] = {
00113 3, 1, 2, 2, 2, 2, 3, 3, 7, 5, 7, 5, 8, 6, 11, 9,
00114 7, 13, 11, 10, 9, 8, 7, 5, 9, 7, 6, 4, 7, 5, 8, 7,
00115 11, 8, 13, 11, 19, 15, 22, 23, 20, 33, 32, 28, 27, 29, 51, 77,
00116 43, 45, 76, 81, 46, 82, 75, 55, 56,144, 58, 80, 60, 74,147, 63,
00117 143, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
00118 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 27, 30, 21, 22,
00119 17, 14, 5, 6,100, 54, 47, 50, 51, 53,106,107,108,109,110,111,
00120 112,113,114,115, 4,117,118, 92, 94,121,122, 3,124,103, 2, 1,
00121 0,129,130,131,120,119,126,125,136,137,138,139,140,141,142,134,
00122 135,132,133,104, 64,101, 62, 57,102, 95, 93, 59, 61, 28, 97, 96,
00123 52, 49, 48, 29, 32, 25, 24, 46, 23, 98, 45, 44, 43, 20, 42, 41,
00124 19, 18, 99, 40, 15, 39, 38, 16, 13, 12, 11, 37, 10, 9, 8, 36,
00125 7,128,127,105,123,116, 35, 34, 33,145, 31, 79, 42,146, 78, 26,
00126 83, 48, 49, 50, 44, 47, 26, 31, 30, 18, 17, 19, 21, 24, 25, 13,
00127 14, 16, 17, 18, 20, 21, 12, 14, 15, 9, 10, 6, 9, 6, 5, 8,
00128 6, 12, 8, 10, 7, 9, 6, 4, 6, 2, 2, 3, 3, 3, 3, 2,
00129 };
00130
00131 static inline int add_left_prediction(uint8_t *dst, uint8_t *src, int w, int acc){
00132 int i;
00133
00134 for(i=0; i<w-1; i++){
00135 acc+= src[i];
00136 dst[i]= acc;
00137 i++;
00138 acc+= src[i];
00139 dst[i]= acc;
00140 }
00141
00142 for(; i<w; i++){
00143 acc+= src[i];
00144 dst[i]= acc;
00145 }
00146
00147 return acc;
00148 }
00149
00150 static inline void add_left_prediction_bgr32(uint8_t *dst, uint8_t *src, int w, int *red, int *green, int *blue){
00151 int i;
00152 int r,g,b;
00153 r= *red;
00154 g= *green;
00155 b= *blue;
00156
00157 for(i=0; i<w; i++){
00158 b+= src[4*i+B];
00159 g+= src[4*i+G];
00160 r+= src[4*i+R];
00161
00162 dst[4*i+B]= b;
00163 dst[4*i+G]= g;
00164 dst[4*i+R]= r;
00165 }
00166
00167 *red= r;
00168 *green= g;
00169 *blue= b;
00170 }
00171
00172 static inline int sub_left_prediction(HYuvContext *s, uint8_t *dst, uint8_t *src, int w, int left){
00173 int i;
00174 if(w<32){
00175 for(i=0; i<w; i++){
00176 const int temp= src[i];
00177 dst[i]= temp - left;
00178 left= temp;
00179 }
00180 return left;
00181 }else{
00182 for(i=0; i<16; i++){
00183 const int temp= src[i];
00184 dst[i]= temp - left;
00185 left= temp;
00186 }
00187 s->dsp.diff_bytes(dst+16, src+16, src+15, w-16);
00188 return src[w-1];
00189 }
00190 }
00191
00192 static inline void sub_left_prediction_bgr32(HYuvContext *s, uint8_t *dst, uint8_t *src, int w, int *red, int *green, int *blue){
00193 int i;
00194 int r,g,b;
00195 r= *red;
00196 g= *green;
00197 b= *blue;
00198 for(i=0; i<FFMIN(w,4); i++){
00199 const int rt= src[i*4+R];
00200 const int gt= src[i*4+G];
00201 const int bt= src[i*4+B];
00202 dst[i*4+R]= rt - r;
00203 dst[i*4+G]= gt - g;
00204 dst[i*4+B]= bt - b;
00205 r = rt;
00206 g = gt;
00207 b = bt;
00208 }
00209 s->dsp.diff_bytes(dst+16, src+16, src+12, w*4-16);
00210 *red= src[(w-1)*4+R];
00211 *green= src[(w-1)*4+G];
00212 *blue= src[(w-1)*4+B];
00213 }
00214
00215 static void read_len_table(uint8_t *dst, GetBitContext *gb){
00216 int i, val, repeat;
00217
00218 for(i=0; i<256;){
00219 repeat= get_bits(gb, 3);
00220 val = get_bits(gb, 5);
00221 if(repeat==0)
00222 repeat= get_bits(gb, 8);
00223
00224 while (repeat--)
00225 dst[i++] = val;
00226 }
00227 }
00228
00229 static int generate_bits_table(uint32_t *dst, uint8_t *len_table){
00230 int len, index;
00231 uint32_t bits=0;
00232
00233 for(len=32; len>0; len--){
00234 for(index=0; index<256; index++){
00235 if(len_table[index]==len)
00236 dst[index]= bits++;
00237 }
00238 if(bits & 1){
00239 av_log(NULL, AV_LOG_ERROR, "Error generating huffman table\n");
00240 return -1;
00241 }
00242 bits >>= 1;
00243 }
00244 return 0;
00245 }
00246
00247 #if CONFIG_HUFFYUV_ENCODER || CONFIG_FFVHUFF_ENCODER
00248 typedef struct {
00249 uint64_t val;
00250 int name;
00251 } HeapElem;
00252
00253 static void heap_sift(HeapElem *h, int root, int size)
00254 {
00255 while(root*2+1 < size) {
00256 int child = root*2+1;
00257 if(child < size-1 && h[child].val > h[child+1].val)
00258 child++;
00259 if(h[root].val > h[child].val) {
00260 FFSWAP(HeapElem, h[root], h[child]);
00261 root = child;
00262 } else
00263 break;
00264 }
00265 }
00266
00267 static void generate_len_table(uint8_t *dst, uint64_t *stats, int size){
00268 HeapElem h[size];
00269 int up[2*size];
00270 int len[2*size];
00271 int offset, i, next;
00272
00273 for(offset=1; ; offset<<=1){
00274 for(i=0; i<size; i++){
00275 h[i].name = i;
00276 h[i].val = (stats[i] << 8) + offset;
00277 }
00278 for(i=size/2-1; i>=0; i--)
00279 heap_sift(h, i, size);
00280
00281 for(next=size; next<size*2-1; next++){
00282
00283 uint64_t min1v = h[0].val;
00284 up[h[0].name] = next;
00285 h[0].val = INT64_MAX;
00286 heap_sift(h, 0, size);
00287 up[h[0].name] = next;
00288 h[0].name = next;
00289 h[0].val += min1v;
00290 heap_sift(h, 0, size);
00291 }
00292
00293 len[2*size-2] = 0;
00294 for(i=2*size-3; i>=size; i--)
00295 len[i] = len[up[i]] + 1;
00296 for(i=0; i<size; i++) {
00297 dst[i] = len[up[i]] + 1;
00298 if(dst[i] >= 32) break;
00299 }
00300 if(i==size) break;
00301 }
00302 }
00303 #endif
00304
00305 static void generate_joint_tables(HYuvContext *s){
00306 uint16_t symbols[1<<VLC_BITS];
00307 uint16_t bits[1<<VLC_BITS];
00308 uint8_t len[1<<VLC_BITS];
00309 if(s->bitstream_bpp < 24){
00310 int p, i, y, u;
00311 for(p=0; p<3; p++){
00312 for(i=y=0; y<256; y++){
00313 int len0 = s->len[0][y];
00314 int limit = VLC_BITS - len0;
00315 if(limit <= 0)
00316 continue;
00317 for(u=0; u<256; u++){
00318 int len1 = s->len[p][u];
00319 if(len1 > limit)
00320 continue;
00321 len[i] = len0 + len1;
00322 bits[i] = (s->bits[0][y] << len1) + s->bits[p][u];
00323 symbols[i] = (y<<8) + u;
00324 if(symbols[i] != 0xffff)
00325 i++;
00326 }
00327 }
00328 free_vlc(&s->vlc[3+p]);
00329 init_vlc_sparse(&s->vlc[3+p], VLC_BITS, i, len, 1, 1, bits, 2, 2, symbols, 2, 2, 0);
00330 }
00331 }else{
00332 uint8_t (*map)[4] = (uint8_t(*)[4])s->pix_bgr_map;
00333 int i, b, g, r, code;
00334 int p0 = s->decorrelate;
00335 int p1 = !s->decorrelate;
00336
00337
00338
00339 for(i=0, g=-16; g<16; g++){
00340 int len0 = s->len[p0][g&255];
00341 int limit0 = VLC_BITS - len0;
00342 if(limit0 < 2)
00343 continue;
00344 for(b=-16; b<16; b++){
00345 int len1 = s->len[p1][b&255];
00346 int limit1 = limit0 - len1;
00347 if(limit1 < 1)
00348 continue;
00349 code = (s->bits[p0][g&255] << len1) + s->bits[p1][b&255];
00350 for(r=-16; r<16; r++){
00351 int len2 = s->len[2][r&255];
00352 if(len2 > limit1)
00353 continue;
00354 len[i] = len0 + len1 + len2;
00355 bits[i] = (code << len2) + s->bits[2][r&255];
00356 if(s->decorrelate){
00357 map[i][G] = g;
00358 map[i][B] = g+b;
00359 map[i][R] = g+r;
00360 }else{
00361 map[i][B] = g;
00362 map[i][G] = b;
00363 map[i][R] = r;
00364 }
00365 i++;
00366 }
00367 }
00368 }
00369 free_vlc(&s->vlc[3]);
00370 init_vlc(&s->vlc[3], VLC_BITS, i, len, 1, 1, bits, 2, 2, 0);
00371 }
00372 }
00373
00374 static int read_huffman_tables(HYuvContext *s, uint8_t *src, int length){
00375 GetBitContext gb;
00376 int i;
00377
00378 init_get_bits(&gb, src, length*8);
00379
00380 for(i=0; i<3; i++){
00381 read_len_table(s->len[i], &gb);
00382
00383 if(generate_bits_table(s->bits[i], s->len[i])<0){
00384 return -1;
00385 }
00386 #if 0
00387 for(j=0; j<256; j++){
00388 printf("%6X, %2d, %3d\n", s->bits[i][j], s->len[i][j], j);
00389 }
00390 #endif
00391 free_vlc(&s->vlc[i]);
00392 init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4, 0);
00393 }
00394
00395 generate_joint_tables(s);
00396
00397 return (get_bits_count(&gb)+7)/8;
00398 }
00399
00400 static int read_old_huffman_tables(HYuvContext *s){
00401 #if 1
00402 GetBitContext gb;
00403 int i;
00404
00405 init_get_bits(&gb, classic_shift_luma, sizeof(classic_shift_luma)*8);
00406 read_len_table(s->len[0], &gb);
00407 init_get_bits(&gb, classic_shift_chroma, sizeof(classic_shift_chroma)*8);
00408 read_len_table(s->len[1], &gb);
00409
00410 for(i=0; i<256; i++) s->bits[0][i] = classic_add_luma [i];
00411 for(i=0; i<256; i++) s->bits[1][i] = classic_add_chroma[i];
00412
00413 if(s->bitstream_bpp >= 24){
00414 memcpy(s->bits[1], s->bits[0], 256*sizeof(uint32_t));
00415 memcpy(s->len[1] , s->len [0], 256*sizeof(uint8_t));
00416 }
00417 memcpy(s->bits[2], s->bits[1], 256*sizeof(uint32_t));
00418 memcpy(s->len[2] , s->len [1], 256*sizeof(uint8_t));
00419
00420 for(i=0; i<3; i++){
00421 free_vlc(&s->vlc[i]);
00422 init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4, 0);
00423 }
00424
00425 generate_joint_tables(s);
00426
00427 return 0;
00428 #else
00429 av_log(s->avctx, AV_LOG_DEBUG, "v1 huffyuv is not supported \n");
00430 return -1;
00431 #endif
00432 }
00433
00434 static av_cold void alloc_temp(HYuvContext *s){
00435 int i;
00436
00437 if(s->bitstream_bpp<24){
00438 for(i=0; i<3; i++){
00439 s->temp[i]= av_malloc(s->width + 16);
00440 }
00441 }else{
00442 for(i=0; i<2; i++){
00443 s->temp[i]= av_malloc(4*s->width + 16);
00444 }
00445 }
00446 }
00447
00448 static av_cold int common_init(AVCodecContext *avctx){
00449 HYuvContext *s = avctx->priv_data;
00450
00451 s->avctx= avctx;
00452 s->flags= avctx->flags;
00453
00454 dsputil_init(&s->dsp, avctx);
00455
00456 s->width= avctx->width;
00457 s->height= avctx->height;
00458 assert(s->width>0 && s->height>0);
00459
00460 return 0;
00461 }
00462
00463 #if CONFIG_HUFFYUV_DECODER || CONFIG_FFVHUFF_DECODER
00464 static av_cold int decode_init(AVCodecContext *avctx)
00465 {
00466 HYuvContext *s = avctx->priv_data;
00467
00468 common_init(avctx);
00469 memset(s->vlc, 0, 3*sizeof(VLC));
00470
00471 avctx->coded_frame= &s->picture;
00472 s->interlaced= s->height > 288;
00473
00474 s->bgr32=1;
00475
00476
00477 if(avctx->extradata_size){
00478 if((avctx->bits_per_coded_sample&7) && avctx->bits_per_coded_sample != 12)
00479 s->version=1;
00480 else
00481 s->version=2;
00482 }else
00483 s->version=0;
00484
00485 if(s->version==2){
00486 int method, interlace;
00487
00488 method= ((uint8_t*)avctx->extradata)[0];
00489 s->decorrelate= method&64 ? 1 : 0;
00490 s->predictor= method&63;
00491 s->bitstream_bpp= ((uint8_t*)avctx->extradata)[1];
00492 if(s->bitstream_bpp==0)
00493 s->bitstream_bpp= avctx->bits_per_coded_sample&~7;
00494 interlace= (((uint8_t*)avctx->extradata)[2] & 0x30) >> 4;
00495 s->interlaced= (interlace==1) ? 1 : (interlace==2) ? 0 : s->interlaced;
00496 s->context= ((uint8_t*)avctx->extradata)[2] & 0x40 ? 1 : 0;
00497
00498 if(read_huffman_tables(s, ((uint8_t*)avctx->extradata)+4, avctx->extradata_size) < 0)
00499 return -1;
00500 }else{
00501 switch(avctx->bits_per_coded_sample&7){
00502 case 1:
00503 s->predictor= LEFT;
00504 s->decorrelate= 0;
00505 break;
00506 case 2:
00507 s->predictor= LEFT;
00508 s->decorrelate= 1;
00509 break;
00510 case 3:
00511 s->predictor= PLANE;
00512 s->decorrelate= avctx->bits_per_coded_sample >= 24;
00513 break;
00514 case 4:
00515 s->predictor= MEDIAN;
00516 s->decorrelate= 0;
00517 break;
00518 default:
00519 s->predictor= LEFT;
00520 s->decorrelate= 0;
00521 break;
00522 }
00523 s->bitstream_bpp= avctx->bits_per_coded_sample & ~7;
00524 s->context= 0;
00525
00526 if(read_old_huffman_tables(s) < 0)
00527 return -1;
00528 }
00529
00530 switch(s->bitstream_bpp){
00531 case 12:
00532 avctx->pix_fmt = PIX_FMT_YUV420P;
00533 break;
00534 case 16:
00535 if(s->yuy2){
00536 avctx->pix_fmt = PIX_FMT_YUYV422;
00537 }else{
00538 avctx->pix_fmt = PIX_FMT_YUV422P;
00539 }
00540 break;
00541 case 24:
00542 case 32:
00543 if(s->bgr32){
00544 avctx->pix_fmt = PIX_FMT_RGB32;
00545 }else{
00546 avctx->pix_fmt = PIX_FMT_BGR24;
00547 }
00548 break;
00549 default:
00550 assert(0);
00551 }
00552
00553 alloc_temp(s);
00554
00555
00556
00557 return 0;
00558 }
00559 #endif
00560
00561 #if CONFIG_HUFFYUV_ENCODER || CONFIG_FFVHUFF_ENCODER
00562 static int store_table(HYuvContext *s, uint8_t *len, uint8_t *buf){
00563 int i;
00564 int index= 0;
00565
00566 for(i=0; i<256;){
00567 int val= len[i];
00568 int repeat=0;
00569
00570 for(; i<256 && len[i]==val && repeat<255; i++)
00571 repeat++;
00572
00573 assert(val < 32 && val >0 && repeat<256 && repeat>0);
00574 if(repeat>7){
00575 buf[index++]= val;
00576 buf[index++]= repeat;
00577 }else{
00578 buf[index++]= val | (repeat<<5);
00579 }
00580 }
00581
00582 return index;
00583 }
00584
00585 static av_cold int encode_init(AVCodecContext *avctx)
00586 {
00587 HYuvContext *s = avctx->priv_data;
00588 int i, j;
00589
00590 common_init(avctx);
00591
00592 avctx->extradata= av_mallocz(1024*30);
00593 avctx->stats_out= av_mallocz(1024*30);
00594 s->version=2;
00595
00596 avctx->coded_frame= &s->picture;
00597
00598 switch(avctx->pix_fmt){
00599 case PIX_FMT_YUV420P:
00600 s->bitstream_bpp= 12;
00601 break;
00602 case PIX_FMT_YUV422P:
00603 s->bitstream_bpp= 16;
00604 break;
00605 case PIX_FMT_RGB32:
00606 s->bitstream_bpp= 24;
00607 break;
00608 default:
00609 av_log(avctx, AV_LOG_ERROR, "format not supported\n");
00610 return -1;
00611 }
00612 avctx->bits_per_coded_sample= s->bitstream_bpp;
00613 s->decorrelate= s->bitstream_bpp >= 24;
00614 s->predictor= avctx->prediction_method;
00615 s->interlaced= avctx->flags&CODEC_FLAG_INTERLACED_ME ? 1 : 0;
00616 if(avctx->context_model==1){
00617 s->context= avctx->context_model;
00618 if(s->flags & (CODEC_FLAG_PASS1|CODEC_FLAG_PASS2)){
00619 av_log(avctx, AV_LOG_ERROR, "context=1 is not compatible with 2 pass huffyuv encoding\n");
00620 return -1;
00621 }
00622 }else s->context= 0;
00623
00624 if(avctx->codec->id==CODEC_ID_HUFFYUV){
00625 if(avctx->pix_fmt==PIX_FMT_YUV420P){
00626 av_log(avctx, AV_LOG_ERROR, "Error: YV12 is not supported by huffyuv; use vcodec=ffvhuff or format=422p\n");
00627 return -1;
00628 }
00629 if(avctx->context_model){
00630 av_log(avctx, AV_LOG_ERROR, "Error: per-frame huffman tables are not supported by huffyuv; use vcodec=ffvhuff\n");
00631 return -1;
00632 }
00633 if(s->interlaced != ( s->height > 288 ))
00634 av_log(avctx, AV_LOG_INFO, "using huffyuv 2.2.0 or newer interlacing flag\n");
00635 }
00636
00637 if(s->bitstream_bpp>=24 && s->predictor==MEDIAN){
00638 av_log(avctx, AV_LOG_ERROR, "Error: RGB is incompatible with median predictor\n");
00639 return -1;
00640 }
00641
00642 ((uint8_t*)avctx->extradata)[0]= s->predictor | (s->decorrelate << 6);
00643 ((uint8_t*)avctx->extradata)[1]= s->bitstream_bpp;
00644 ((uint8_t*)avctx->extradata)[2]= s->interlaced ? 0x10 : 0x20;
00645 if(s->context)
00646 ((uint8_t*)avctx->extradata)[2]|= 0x40;
00647 ((uint8_t*)avctx->extradata)[3]= 0;
00648 s->avctx->extradata_size= 4;
00649
00650 if(avctx->stats_in){
00651 char *p= avctx->stats_in;
00652
00653 for(i=0; i<3; i++)
00654 for(j=0; j<256; j++)
00655 s->stats[i][j]= 1;
00656
00657 for(;;){
00658 for(i=0; i<3; i++){
00659 char *next;
00660
00661 for(j=0; j<256; j++){
00662 s->stats[i][j]+= strtol(p, &next, 0);
00663 if(next==p) return -1;
00664 p=next;
00665 }
00666 }
00667 if(p[0]==0 || p[1]==0 || p[2]==0) break;
00668 }
00669 }else{
00670 for(i=0; i<3; i++)
00671 for(j=0; j<256; j++){
00672 int d= FFMIN(j, 256-j);
00673
00674 s->stats[i][j]= 100000000/(d+1);
00675 }
00676 }
00677
00678 for(i=0; i<3; i++){
00679 generate_len_table(s->len[i], s->stats[i], 256);
00680
00681 if(generate_bits_table(s->bits[i], s->len[i])<0){
00682 return -1;
00683 }
00684
00685 s->avctx->extradata_size+=
00686 store_table(s, s->len[i], &((uint8_t*)s->avctx->extradata)[s->avctx->extradata_size]);
00687 }
00688
00689 if(s->context){
00690 for(i=0; i<3; i++){
00691 int pels = s->width*s->height / (i?40:10);
00692 for(j=0; j<256; j++){
00693 int d= FFMIN(j, 256-j);
00694 s->stats[i][j]= pels/(d+1);
00695 }
00696 }
00697 }else{
00698 for(i=0; i<3; i++)
00699 for(j=0; j<256; j++)
00700 s->stats[i][j]= 0;
00701 }
00702
00703
00704
00705 alloc_temp(s);
00706
00707 s->picture_number=0;
00708
00709 return 0;
00710 }
00711 #endif
00712
00713
00714
00715 #define READ_2PIX(dst0, dst1, plane1){\
00716 uint16_t code = get_vlc2(&s->gb, s->vlc[3+plane1].table, VLC_BITS, 1);\
00717 if(code != 0xffff){\
00718 dst0 = code>>8;\
00719 dst1 = code;\
00720 }else{\
00721 dst0 = get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3);\
00722 dst1 = get_vlc2(&s->gb, s->vlc[plane1].table, VLC_BITS, 3);\
00723 }\
00724 }
00725
00726 static void decode_422_bitstream(HYuvContext *s, int count){
00727 int i;
00728
00729 count/=2;
00730
00731 if(count >= (s->gb.size_in_bits - get_bits_count(&s->gb))/(31*4)){
00732 for(i=0; i<count && get_bits_count(&s->gb) < s->gb.size_in_bits; i++){
00733 READ_2PIX(s->temp[0][2*i ], s->temp[1][i], 1);
00734 READ_2PIX(s->temp[0][2*i+1], s->temp[2][i], 2);
00735 }
00736 }else{
00737 for(i=0; i<count; i++){
00738 READ_2PIX(s->temp[0][2*i ], s->temp[1][i], 1);
00739 READ_2PIX(s->temp[0][2*i+1], s->temp[2][i], 2);
00740 }
00741 }
00742 }
00743
00744 static void decode_gray_bitstream(HYuvContext *s, int count){
00745 int i;
00746
00747 count/=2;
00748
00749 if(count >= (s->gb.size_in_bits - get_bits_count(&s->gb))/(31*2)){
00750 for(i=0; i<count && get_bits_count(&s->gb) < s->gb.size_in_bits; i++){
00751 READ_2PIX(s->temp[0][2*i ], s->temp[0][2*i+1], 0);
00752 }
00753 }else{
00754 for(i=0; i<count; i++){
00755 READ_2PIX(s->temp[0][2*i ], s->temp[0][2*i+1], 0);
00756 }
00757 }
00758 }
00759
00760 #if CONFIG_HUFFYUV_ENCODER || CONFIG_FFVHUFF_ENCODER
00761 static int encode_422_bitstream(HYuvContext *s, int count){
00762 int i;
00763
00764 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < 2*4*count){
00765 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00766 return -1;
00767 }
00768
00769 #define LOAD4\
00770 int y0 = s->temp[0][2*i];\
00771 int y1 = s->temp[0][2*i+1];\
00772 int u0 = s->temp[1][i];\
00773 int v0 = s->temp[2][i];
00774
00775 count/=2;
00776 if(s->flags&CODEC_FLAG_PASS1){
00777 for(i=0; i<count; i++){
00778 LOAD4;
00779 s->stats[0][y0]++;
00780 s->stats[1][u0]++;
00781 s->stats[0][y1]++;
00782 s->stats[2][v0]++;
00783 }
00784 }
00785 if(s->avctx->flags2&CODEC_FLAG2_NO_OUTPUT)
00786 return 0;
00787 if(s->context){
00788 for(i=0; i<count; i++){
00789 LOAD4;
00790 s->stats[0][y0]++;
00791 put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
00792 s->stats[1][u0]++;
00793 put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
00794 s->stats[0][y1]++;
00795 put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
00796 s->stats[2][v0]++;
00797 put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
00798 }
00799 }else{
00800 for(i=0; i<count; i++){
00801 LOAD4;
00802 put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
00803 put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
00804 put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
00805 put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
00806 }
00807 }
00808 return 0;
00809 }
00810
00811 static int encode_gray_bitstream(HYuvContext *s, int count){
00812 int i;
00813
00814 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < 4*count){
00815 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00816 return -1;
00817 }
00818
00819 #define LOAD2\
00820 int y0 = s->temp[0][2*i];\
00821 int y1 = s->temp[0][2*i+1];
00822 #define STAT2\
00823 s->stats[0][y0]++;\
00824 s->stats[0][y1]++;
00825 #define WRITE2\
00826 put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);\
00827 put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
00828
00829 count/=2;
00830 if(s->flags&CODEC_FLAG_PASS1){
00831 for(i=0; i<count; i++){
00832 LOAD2;
00833 STAT2;
00834 }
00835 }
00836 if(s->avctx->flags2&CODEC_FLAG2_NO_OUTPUT)
00837 return 0;
00838
00839 if(s->context){
00840 for(i=0; i<count; i++){
00841 LOAD2;
00842 STAT2;
00843 WRITE2;
00844 }
00845 }else{
00846 for(i=0; i<count; i++){
00847 LOAD2;
00848 WRITE2;
00849 }
00850 }
00851 return 0;
00852 }
00853 #endif
00854
00855 static av_always_inline void decode_bgr_1(HYuvContext *s, int count, int decorrelate, int alpha){
00856 int i;
00857 for(i=0; i<count; i++){
00858 int code = get_vlc2(&s->gb, s->vlc[3].table, VLC_BITS, 1);
00859 if(code != -1){
00860 *(uint32_t*)&s->temp[0][4*i] = s->pix_bgr_map[code];
00861 }else if(decorrelate){
00862 s->temp[0][4*i+G] = get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3);
00863 s->temp[0][4*i+B] = get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3) + s->temp[0][4*i+G];
00864 s->temp[0][4*i+R] = get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3) + s->temp[0][4*i+G];
00865 }else{
00866 s->temp[0][4*i+B] = get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3);
00867 s->temp[0][4*i+G] = get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3);
00868 s->temp[0][4*i+R] = get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3);
00869 }
00870 if(alpha)
00871 get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3);
00872 }
00873 }
00874
00875 static void decode_bgr_bitstream(HYuvContext *s, int count){
00876 if(s->decorrelate){
00877 if(s->bitstream_bpp==24)
00878 decode_bgr_1(s, count, 1, 0);
00879 else
00880 decode_bgr_1(s, count, 1, 1);
00881 }else{
00882 if(s->bitstream_bpp==24)
00883 decode_bgr_1(s, count, 0, 0);
00884 else
00885 decode_bgr_1(s, count, 0, 1);
00886 }
00887 }
00888
00889 static int encode_bgr_bitstream(HYuvContext *s, int count){
00890 int i;
00891
00892 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < 3*4*count){
00893 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00894 return -1;
00895 }
00896
00897 #define LOAD3\
00898 int g= s->temp[0][4*i+G];\
00899 int b= (s->temp[0][4*i+B] - g) & 0xff;\
00900 int r= (s->temp[0][4*i+R] - g) & 0xff;
00901 #define STAT3\
00902 s->stats[0][b]++;\
00903 s->stats[1][g]++;\
00904 s->stats[2][r]++;
00905 #define WRITE3\
00906 put_bits(&s->pb, s->len[1][g], s->bits[1][g]);\
00907 put_bits(&s->pb, s->len[0][b], s->bits[0][b]);\
00908 put_bits(&s->pb, s->len[2][r], s->bits[2][r]);
00909
00910 if((s->flags&CODEC_FLAG_PASS1) && (s->avctx->flags2&CODEC_FLAG2_NO_OUTPUT)){
00911 for(i=0; i<count; i++){
00912 LOAD3;
00913 STAT3;
00914 }
00915 }else if(s->context || (s->flags&CODEC_FLAG_PASS1)){
00916 for(i=0; i<count; i++){
00917 LOAD3;
00918 STAT3;
00919 WRITE3;
00920 }
00921 }else{
00922 for(i=0; i<count; i++){
00923 LOAD3;
00924 WRITE3;
00925 }
00926 }
00927 return 0;
00928 }
00929
00930 #if CONFIG_HUFFYUV_DECODER || CONFIG_FFVHUFF_DECODER
00931 static void draw_slice(HYuvContext *s, int y){
00932 int h, cy;
00933 int offset[4];
00934
00935 if(s->avctx->draw_horiz_band==NULL)
00936 return;
00937
00938 h= y - s->last_slice_end;
00939 y -= h;
00940
00941 if(s->bitstream_bpp==12){
00942 cy= y>>1;
00943 }else{
00944 cy= y;
00945 }
00946
00947 offset[0] = s->picture.linesize[0]*y;
00948 offset[1] = s->picture.linesize[1]*cy;
00949 offset[2] = s->picture.linesize[2]*cy;
00950 offset[3] = 0;
00951 emms_c();
00952
00953 s->avctx->draw_horiz_band(s->avctx, &s->picture, offset, y, 3, h);
00954
00955 s->last_slice_end= y + h;
00956 }
00957
00958 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size){
00959 HYuvContext *s = avctx->priv_data;
00960 const int width= s->width;
00961 const int width2= s->width>>1;
00962 const int height= s->height;
00963 int fake_ystride, fake_ustride, fake_vstride;
00964 AVFrame * const p= &s->picture;
00965 int table_size= 0;
00966
00967 AVFrame *picture = data;
00968
00969 s->bitstream_buffer= av_fast_realloc(s->bitstream_buffer, &s->bitstream_buffer_size, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
00970
00971 s->dsp.bswap_buf((uint32_t*)s->bitstream_buffer, (const uint32_t*)buf, buf_size/4);
00972
00973 if(p->data[0])
00974 avctx->release_buffer(avctx, p);
00975
00976 p->reference= 0;
00977 if(avctx->get_buffer(avctx, p) < 0){
00978 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00979 return -1;
00980 }
00981
00982 if(s->context){
00983 table_size = read_huffman_tables(s, s->bitstream_buffer, buf_size);
00984 if(table_size < 0)
00985 return -1;
00986 }
00987
00988 if((unsigned)(buf_size-table_size) >= INT_MAX/8)
00989 return -1;
00990
00991 init_get_bits(&s->gb, s->bitstream_buffer+table_size, (buf_size-table_size)*8);
00992
00993 fake_ystride= s->interlaced ? p->linesize[0]*2 : p->linesize[0];
00994 fake_ustride= s->interlaced ? p->linesize[1]*2 : p->linesize[1];
00995 fake_vstride= s->interlaced ? p->linesize[2]*2 : p->linesize[2];
00996
00997 s->last_slice_end= 0;
00998
00999 if(s->bitstream_bpp<24){
01000 int y, cy;
01001 int lefty, leftu, leftv;
01002 int lefttopy, lefttopu, lefttopv;
01003
01004 if(s->yuy2){
01005 p->data[0][3]= get_bits(&s->gb, 8);
01006 p->data[0][2]= get_bits(&s->gb, 8);
01007 p->data[0][1]= get_bits(&s->gb, 8);
01008 p->data[0][0]= get_bits(&s->gb, 8);
01009
01010 av_log(avctx, AV_LOG_ERROR, "YUY2 output is not implemented yet\n");
01011 return -1;
01012 }else{
01013
01014 leftv= p->data[2][0]= get_bits(&s->gb, 8);
01015 lefty= p->data[0][1]= get_bits(&s->gb, 8);
01016 leftu= p->data[1][0]= get_bits(&s->gb, 8);
01017 p->data[0][0]= get_bits(&s->gb, 8);
01018
01019 switch(s->predictor){
01020 case LEFT:
01021 case PLANE:
01022 decode_422_bitstream(s, width-2);
01023 lefty= add_left_prediction(p->data[0] + 2, s->temp[0], width-2, lefty);
01024 if(!(s->flags&CODEC_FLAG_GRAY)){
01025 leftu= add_left_prediction(p->data[1] + 1, s->temp[1], width2-1, leftu);
01026 leftv= add_left_prediction(p->data[2] + 1, s->temp[2], width2-1, leftv);
01027 }
01028
01029 for(cy=y=1; y<s->height; y++,cy++){
01030 uint8_t *ydst, *udst, *vdst;
01031
01032 if(s->bitstream_bpp==12){
01033 decode_gray_bitstream(s, width);
01034
01035 ydst= p->data[0] + p->linesize[0]*y;
01036
01037 lefty= add_left_prediction(ydst, s->temp[0], width, lefty);
01038 if(s->predictor == PLANE){
01039 if(y>s->interlaced)
01040 s->dsp.add_bytes(ydst, ydst - fake_ystride, width);
01041 }
01042 y++;
01043 if(y>=s->height) break;
01044 }
01045
01046 draw_slice(s, y);
01047
01048 ydst= p->data[0] + p->linesize[0]*y;
01049 udst= p->data[1] + p->linesize[1]*cy;
01050 vdst= p->data[2] + p->linesize[2]*cy;
01051
01052 decode_422_bitstream(s, width);
01053 lefty= add_left_prediction(ydst, s->temp[0], width, lefty);
01054 if(!(s->flags&CODEC_FLAG_GRAY)){
01055 leftu= add_left_prediction(udst, s->temp[1], width2, leftu);
01056 leftv= add_left_prediction(vdst, s->temp[2], width2, leftv);
01057 }
01058 if(s->predictor == PLANE){
01059 if(cy>s->interlaced){
01060 s->dsp.add_bytes(ydst, ydst - fake_ystride, width);
01061 if(!(s->flags&CODEC_FLAG_GRAY)){
01062 s->dsp.add_bytes(udst, udst - fake_ustride, width2);
01063 s->dsp.add_bytes(vdst, vdst - fake_vstride, width2);
01064 }
01065 }
01066 }
01067 }
01068 draw_slice(s, height);
01069
01070 break;
01071 case MEDIAN:
01072
01073 decode_422_bitstream(s, width-2);
01074 lefty= add_left_prediction(p->data[0] + 2, s->temp[0], width-2, lefty);
01075 if(!(s->flags&CODEC_FLAG_GRAY)){
01076 leftu= add_left_prediction(p->data[1] + 1, s->temp[1], width2-1, leftu);
01077 leftv= add_left_prediction(p->data[2] + 1, s->temp[2], width2-1, leftv);
01078 }
01079
01080 cy=y=1;
01081
01082
01083 if(s->interlaced){
01084 decode_422_bitstream(s, width);
01085 lefty= add_left_prediction(p->data[0] + p->linesize[0], s->temp[0], width, lefty);
01086 if(!(s->flags&CODEC_FLAG_GRAY)){
01087 leftu= add_left_prediction(p->data[1] + p->linesize[2], s->temp[1], width2, leftu);
01088 leftv= add_left_prediction(p->data[2] + p->linesize[1], s->temp[2], width2, leftv);
01089 }
01090 y++; cy++;
01091 }
01092
01093
01094 decode_422_bitstream(s, 4);
01095 lefty= add_left_prediction(p->data[0] + fake_ystride, s->temp[0], 4, lefty);
01096 if(!(s->flags&CODEC_FLAG_GRAY)){
01097 leftu= add_left_prediction(p->data[1] + fake_ustride, s->temp[1], 2, leftu);
01098 leftv= add_left_prediction(p->data[2] + fake_vstride, s->temp[2], 2, leftv);
01099 }
01100
01101
01102 lefttopy= p->data[0][3];
01103 decode_422_bitstream(s, width-4);
01104 s->dsp.add_hfyu_median_prediction(p->data[0] + fake_ystride+4, p->data[0]+4, s->temp[0], width-4, &lefty, &lefttopy);
01105 if(!(s->flags&CODEC_FLAG_GRAY)){
01106 lefttopu= p->data[1][1];
01107 lefttopv= p->data[2][1];
01108 s->dsp.add_hfyu_median_prediction(p->data[1] + fake_ustride+2, p->data[1]+2, s->temp[1], width2-2, &leftu, &lefttopu);
01109 s->dsp.add_hfyu_median_prediction(p->data[2] + fake_vstride+2, p->data[2]+2, s->temp[2], width2-2, &leftv, &lefttopv);
01110 }
01111 y++; cy++;
01112
01113 for(; y<height; y++,cy++){
01114 uint8_t *ydst, *udst, *vdst;
01115
01116 if(s->bitstream_bpp==12){
01117 while(2*cy > y){
01118 decode_gray_bitstream(s, width);
01119 ydst= p->data[0] + p->linesize[0]*y;
01120 s->dsp.add_hfyu_median_prediction(ydst, ydst - fake_ystride, s->temp[0], width, &lefty, &lefttopy);
01121 y++;
01122 }
01123 if(y>=height) break;
01124 }
01125 draw_slice(s, y);
01126
01127 decode_422_bitstream(s, width);
01128
01129 ydst= p->data[0] + p->linesize[0]*y;
01130 udst= p->data[1] + p->linesize[1]*cy;
01131 vdst= p->data[2] + p->linesize[2]*cy;
01132
01133 s->dsp.add_hfyu_median_prediction(ydst, ydst - fake_ystride, s->temp[0], width, &lefty, &lefttopy);
01134 if(!(s->flags&CODEC_FLAG_GRAY)){
01135 s->dsp.add_hfyu_median_prediction(udst, udst - fake_ustride, s->temp[1], width2, &leftu, &lefttopu);
01136 s->dsp.add_hfyu_median_prediction(vdst, vdst - fake_vstride, s->temp[2], width2, &leftv, &lefttopv);
01137 }
01138 }
01139
01140 draw_slice(s, height);
01141 break;
01142 }
01143 }
01144 }else{
01145 int y;
01146 int leftr, leftg, leftb;
01147 const int last_line= (height-1)*p->linesize[0];
01148
01149 if(s->bitstream_bpp==32){
01150 skip_bits(&s->gb, 8);
01151 leftr= p->data[0][last_line+R]= get_bits(&s->gb, 8);
01152 leftg= p->data[0][last_line+G]= get_bits(&s->gb, 8);
01153 leftb= p->data[0][last_line+B]= get_bits(&s->gb, 8);
01154 }else{
01155 leftr= p->data[0][last_line+R]= get_bits(&s->gb, 8);
01156 leftg= p->data[0][last_line+G]= get_bits(&s->gb, 8);
01157 leftb= p->data[0][last_line+B]= get_bits(&s->gb, 8);
01158 skip_bits(&s->gb, 8);
01159 }
01160
01161 if(s->bgr32){
01162 switch(s->predictor){
01163 case LEFT:
01164 case PLANE:
01165 decode_bgr_bitstream(s, width-1);
01166 add_left_prediction_bgr32(p->data[0] + last_line+4, s->temp[0], width-1, &leftr, &leftg, &leftb);
01167
01168 for(y=s->height-2; y>=0; y--){
01169 decode_bgr_bitstream(s, width);
01170
01171 add_left_prediction_bgr32(p->data[0] + p->linesize[0]*y, s->temp[0], width, &leftr, &leftg, &leftb);
01172 if(s->predictor == PLANE){
01173 if((y&s->interlaced)==0 && y<s->height-1-s->interlaced){
01174 s->dsp.add_bytes(p->data[0] + p->linesize[0]*y,
01175 p->data[0] + p->linesize[0]*y + fake_ystride, fake_ystride);
01176 }
01177 }
01178 }
01179 draw_slice(s, height);
01180 break;
01181 default:
01182 av_log(avctx, AV_LOG_ERROR, "prediction type not supported!\n");
01183 }
01184 }else{
01185
01186 av_log(avctx, AV_LOG_ERROR, "BGR24 output is not implemented yet\n");
01187 return -1;
01188 }
01189 }
01190 emms_c();
01191
01192 *picture= *p;
01193 *data_size = sizeof(AVFrame);
01194
01195 return (get_bits_count(&s->gb)+31)/32*4 + table_size;
01196 }
01197 #endif
01198
01199 static int common_end(HYuvContext *s){
01200 int i;
01201
01202 for(i=0; i<3; i++){
01203 av_freep(&s->temp[i]);
01204 }
01205 return 0;
01206 }
01207
01208 #if CONFIG_HUFFYUV_DECODER || CONFIG_FFVHUFF_DECODER
01209 static av_cold int decode_end(AVCodecContext *avctx)
01210 {
01211 HYuvContext *s = avctx->priv_data;
01212 int i;
01213
01214 common_end(s);
01215 av_freep(&s->bitstream_buffer);
01216
01217 for(i=0; i<6; i++){
01218 free_vlc(&s->vlc[i]);
01219 }
01220
01221 return 0;
01222 }
01223 #endif
01224
01225 #if CONFIG_HUFFYUV_ENCODER || CONFIG_FFVHUFF_ENCODER
01226 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
01227 HYuvContext *s = avctx->priv_data;
01228 AVFrame *pict = data;
01229 const int width= s->width;
01230 const int width2= s->width>>1;
01231 const int height= s->height;
01232 const int fake_ystride= s->interlaced ? pict->linesize[0]*2 : pict->linesize[0];
01233 const int fake_ustride= s->interlaced ? pict->linesize[1]*2 : pict->linesize[1];
01234 const int fake_vstride= s->interlaced ? pict->linesize[2]*2 : pict->linesize[2];
01235 AVFrame * const p= &s->picture;
01236 int i, j, size=0;
01237
01238 *p = *pict;
01239 p->pict_type= FF_I_TYPE;
01240 p->key_frame= 1;
01241
01242 if(s->context){
01243 for(i=0; i<3; i++){
01244 generate_len_table(s->len[i], s->stats[i], 256);
01245 if(generate_bits_table(s->bits[i], s->len[i])<0)
01246 return -1;
01247 size+= store_table(s, s->len[i], &buf[size]);
01248 }
01249
01250 for(i=0; i<3; i++)
01251 for(j=0; j<256; j++)
01252 s->stats[i][j] >>= 1;
01253 }
01254
01255 init_put_bits(&s->pb, buf+size, buf_size-size);
01256
01257 if(avctx->pix_fmt == PIX_FMT_YUV422P || avctx->pix_fmt == PIX_FMT_YUV420P){
01258 int lefty, leftu, leftv, y, cy;
01259
01260 put_bits(&s->pb, 8, leftv= p->data[2][0]);
01261 put_bits(&s->pb, 8, lefty= p->data[0][1]);
01262 put_bits(&s->pb, 8, leftu= p->data[1][0]);
01263 put_bits(&s->pb, 8, p->data[0][0]);
01264
01265 lefty= sub_left_prediction(s, s->temp[0], p->data[0]+2, width-2 , lefty);
01266 leftu= sub_left_prediction(s, s->temp[1], p->data[1]+1, width2-1, leftu);
01267 leftv= sub_left_prediction(s, s->temp[2], p->data[2]+1, width2-1, leftv);
01268
01269 encode_422_bitstream(s, width-2);
01270
01271 if(s->predictor==MEDIAN){
01272 int lefttopy, lefttopu, lefttopv;
01273 cy=y=1;
01274 if(s->interlaced){
01275 lefty= sub_left_prediction(s, s->temp[0], p->data[0]+p->linesize[0], width , lefty);
01276 leftu= sub_left_prediction(s, s->temp[1], p->data[1]+p->linesize[1], width2, leftu);
01277 leftv= sub_left_prediction(s, s->temp[2], p->data[2]+p->linesize[2], width2, leftv);
01278
01279 encode_422_bitstream(s, width);
01280 y++; cy++;
01281 }
01282
01283 lefty= sub_left_prediction(s, s->temp[0], p->data[0]+fake_ystride, 4, lefty);
01284 leftu= sub_left_prediction(s, s->temp[1], p->data[1]+fake_ustride, 2, leftu);
01285 leftv= sub_left_prediction(s, s->temp[2], p->data[2]+fake_vstride, 2, leftv);
01286
01287 encode_422_bitstream(s, 4);
01288
01289 lefttopy= p->data[0][3];
01290 lefttopu= p->data[1][1];
01291 lefttopv= p->data[2][1];
01292 s->dsp.sub_hfyu_median_prediction(s->temp[0], p->data[0]+4, p->data[0] + fake_ystride+4, width-4 , &lefty, &lefttopy);
01293 s->dsp.sub_hfyu_median_prediction(s->temp[1], p->data[1]+2, p->data[1] + fake_ustride+2, width2-2, &leftu, &lefttopu);
01294 s->dsp.sub_hfyu_median_prediction(s->temp[2], p->data[2]+2, p->data[2] + fake_vstride+2, width2-2, &leftv, &lefttopv);
01295 encode_422_bitstream(s, width-4);
01296 y++; cy++;
01297
01298 for(; y<height; y++,cy++){
01299 uint8_t *ydst, *udst, *vdst;
01300
01301 if(s->bitstream_bpp==12){
01302 while(2*cy > y){
01303 ydst= p->data[0] + p->linesize[0]*y;
01304 s->dsp.sub_hfyu_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy);
01305 encode_gray_bitstream(s, width);
01306 y++;
01307 }
01308 if(y>=height) break;
01309 }
01310 ydst= p->data[0] + p->linesize[0]*y;
01311 udst= p->data[1] + p->linesize[1]*cy;
01312 vdst= p->data[2] + p->linesize[2]*cy;
01313
01314 s->dsp.sub_hfyu_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy);
01315 s->dsp.sub_hfyu_median_prediction(s->temp[1], udst - fake_ustride, udst, width2, &leftu, &lefttopu);
01316 s->dsp.sub_hfyu_median_prediction(s->temp[2], vdst - fake_vstride, vdst, width2, &leftv, &lefttopv);
01317
01318 encode_422_bitstream(s, width);
01319 }
01320 }else{
01321 for(cy=y=1; y<height; y++,cy++){
01322 uint8_t *ydst, *udst, *vdst;
01323
01324
01325 if(s->bitstream_bpp==12){
01326 ydst= p->data[0] + p->linesize[0]*y;
01327
01328 if(s->predictor == PLANE && s->interlaced < y){
01329 s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
01330
01331 lefty= sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
01332 }else{
01333 lefty= sub_left_prediction(s, s->temp[0], ydst, width , lefty);
01334 }
01335 encode_gray_bitstream(s, width);
01336 y++;
01337 if(y>=height) break;
01338 }
01339
01340 ydst= p->data[0] + p->linesize[0]*y;
01341 udst= p->data[1] + p->linesize[1]*cy;
01342 vdst= p->data[2] + p->linesize[2]*cy;
01343
01344 if(s->predictor == PLANE && s->interlaced < cy){
01345 s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
01346 s->dsp.diff_bytes(s->temp[2], udst, udst - fake_ustride, width2);
01347 s->dsp.diff_bytes(s->temp[2] + width2, vdst, vdst - fake_vstride, width2);
01348
01349 lefty= sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
01350 leftu= sub_left_prediction(s, s->temp[1], s->temp[2], width2, leftu);
01351 leftv= sub_left_prediction(s, s->temp[2], s->temp[2] + width2, width2, leftv);
01352 }else{
01353 lefty= sub_left_prediction(s, s->temp[0], ydst, width , lefty);
01354 leftu= sub_left_prediction(s, s->temp[1], udst, width2, leftu);
01355 leftv= sub_left_prediction(s, s->temp[2], vdst, width2, leftv);
01356 }
01357
01358 encode_422_bitstream(s, width);
01359 }
01360 }
01361 }else if(avctx->pix_fmt == PIX_FMT_RGB32){
01362 uint8_t *data = p->data[0] + (height-1)*p->linesize[0];
01363 const int stride = -p->linesize[0];
01364 const int fake_stride = -fake_ystride;
01365 int y;
01366 int leftr, leftg, leftb;
01367
01368 put_bits(&s->pb, 8, leftr= data[R]);
01369 put_bits(&s->pb, 8, leftg= data[G]);
01370 put_bits(&s->pb, 8, leftb= data[B]);
01371 put_bits(&s->pb, 8, 0);
01372
01373 sub_left_prediction_bgr32(s, s->temp[0], data+4, width-1, &leftr, &leftg, &leftb);
01374 encode_bgr_bitstream(s, width-1);
01375
01376 for(y=1; y<s->height; y++){
01377 uint8_t *dst = data + y*stride;
01378 if(s->predictor == PLANE && s->interlaced < y){
01379 s->dsp.diff_bytes(s->temp[1], dst, dst - fake_stride, width*4);
01380 sub_left_prediction_bgr32(s, s->temp[0], s->temp[1], width, &leftr, &leftg, &leftb);
01381 }else{
01382 sub_left_prediction_bgr32(s, s->temp[0], dst, width, &leftr, &leftg, &leftb);
01383 }
01384 encode_bgr_bitstream(s, width);
01385 }
01386 }else{
01387 av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
01388 }
01389 emms_c();
01390
01391 size+= (put_bits_count(&s->pb)+31)/8;
01392 size/= 4;
01393
01394 if((s->flags&CODEC_FLAG_PASS1) && (s->picture_number&31)==0){
01395 int j;
01396 char *p= avctx->stats_out;
01397 char *end= p + 1024*30;
01398 for(i=0; i<3; i++){
01399 for(j=0; j<256; j++){
01400 snprintf(p, end-p, "%"PRIu64" ", s->stats[i][j]);
01401 p+= strlen(p);
01402 s->stats[i][j]= 0;
01403 }
01404 snprintf(p, end-p, "\n");
01405 p++;
01406 }
01407 } else
01408 avctx->stats_out[0] = '\0';
01409 if(!(s->avctx->flags2 & CODEC_FLAG2_NO_OUTPUT)){
01410 flush_put_bits(&s->pb);
01411 s->dsp.bswap_buf((uint32_t*)buf, (uint32_t*)buf, size);
01412 }
01413
01414 s->picture_number++;
01415
01416 return size*4;
01417 }
01418
01419 static av_cold int encode_end(AVCodecContext *avctx)
01420 {
01421 HYuvContext *s = avctx->priv_data;
01422
01423 common_end(s);
01424
01425 av_freep(&avctx->extradata);
01426 av_freep(&avctx->stats_out);
01427
01428 return 0;
01429 }
01430 #endif
01431
01432 #if CONFIG_HUFFYUV_DECODER
01433 AVCodec huffyuv_decoder = {
01434 "huffyuv",
01435 CODEC_TYPE_VIDEO,
01436 CODEC_ID_HUFFYUV,
01437 sizeof(HYuvContext),
01438 decode_init,
01439 NULL,
01440 decode_end,
01441 decode_frame,
01442 CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND,
01443 NULL,
01444 .long_name = NULL_IF_CONFIG_SMALL("Huffyuv / HuffYUV"),
01445 };
01446 #endif
01447
01448 #if CONFIG_FFVHUFF_DECODER
01449 AVCodec ffvhuff_decoder = {
01450 "ffvhuff",
01451 CODEC_TYPE_VIDEO,
01452 CODEC_ID_FFVHUFF,
01453 sizeof(HYuvContext),
01454 decode_init,
01455 NULL,
01456 decode_end,
01457 decode_frame,
01458 CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND,
01459 NULL,
01460 .long_name = NULL_IF_CONFIG_SMALL("Huffyuv FFmpeg variant"),
01461 };
01462 #endif
01463
01464 #if CONFIG_HUFFYUV_ENCODER
01465 AVCodec huffyuv_encoder = {
01466 "huffyuv",
01467 CODEC_TYPE_VIDEO,
01468 CODEC_ID_HUFFYUV,
01469 sizeof(HYuvContext),
01470 encode_init,
01471 encode_frame,
01472 encode_end,
01473 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV422P, PIX_FMT_RGB32, PIX_FMT_NONE},
01474 .long_name = NULL_IF_CONFIG_SMALL("Huffyuv / HuffYUV"),
01475 };
01476 #endif
01477
01478 #if CONFIG_FFVHUFF_ENCODER
01479 AVCodec ffvhuff_encoder = {
01480 "ffvhuff",
01481 CODEC_TYPE_VIDEO,
01482 CODEC_ID_FFVHUFF,
01483 sizeof(HYuvContext),
01484 encode_init,
01485 encode_frame,
01486 encode_end,
01487 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_RGB32, PIX_FMT_NONE},
01488 .long_name = NULL_IF_CONFIG_SMALL("Huffyuv FFmpeg variant"),
01489 };
01490 #endif