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