comparison ffv1.c @ 1593:5495691106c3 libavcodec

ffv1 rgb support
author michael
date Sat, 01 Nov 2003 17:38:25 +0000
parents b340e83b8d0d
children 932d306bf1dc
comparison
equal deleted inserted replaced
1592:63009885ca88 1593:5495691106c3
175 AVFrame picture; 175 AVFrame picture;
176 int plane_count; 176 int plane_count;
177 int ac; ///< 1-> CABAC 0-> golomb rice 177 int ac; ///< 1-> CABAC 0-> golomb rice
178 PlaneContext plane[MAX_PLANES]; 178 PlaneContext plane[MAX_PLANES];
179 int16_t quant_table[5][256]; 179 int16_t quant_table[5][256];
180 int run_index;
181 int colorspace;
180 182
181 DSPContext dsp; 183 DSPContext dsp;
182 }FFV1Context; 184 }FFV1Context;
183 185
184 static inline int predict(uint8_t *src, uint8_t *last){ 186 static always_inline int fold(int diff, int bits){
187 if(bits==8)
188 diff= (int8_t)diff;
189 else{
190 diff+= 1<<(bits-1);
191 diff&=(1<<bits)-1;
192 diff-= 1<<(bits-1);
193 }
194
195 return diff;
196 }
197
198 static inline int predict(int_fast16_t *src, int_fast16_t *last){
185 const int LT= last[-1]; 199 const int LT= last[-1];
186 const int T= last[ 0]; 200 const int T= last[ 0];
187 const int L = src[-1]; 201 const int L = src[-1];
188 202
189 return mid_pred(L, L + T - LT, T); 203 return mid_pred(L, L + T - LT, T);
190 } 204 }
191 205
192 static inline int get_context(FFV1Context *f, uint8_t *src, uint8_t *last, uint8_t *last2){ 206 static inline int get_context(FFV1Context *f, int_fast16_t *src, int_fast16_t *last, int_fast16_t *last2){
193 const int LT= last[-1]; 207 const int LT= last[-1];
194 const int T= last[ 0]; 208 const int T= last[ 0];
195 const int RT= last[ 1]; 209 const int RT= last[ 1];
196 const int L = src[-1]; 210 const int L = src[-1];
197 211
287 301
288 state->drift= drift; 302 state->drift= drift;
289 state->count= count; 303 state->count= count;
290 } 304 }
291 305
292 static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v){ 306 static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v, int bits){
293 int i, k, code; 307 int i, k, code;
294 //printf("final: %d ", v); 308 //printf("final: %d ", v);
295 v = (int8_t)(v - state->bias); 309 v = fold(v - state->bias, bits);
296 310
297 i= state->count; 311 i= state->count;
298 k=0; 312 k=0;
299 while(i < state->error_sum){ //FIXME optimize 313 while(i < state->error_sum){ //FIXME optimize
300 k++; 314 k++;
301 i += i; 315 i += i;
311 #endif 325 #endif
312 326
313 code = -2*code-1; 327 code = -2*code-1;
314 code^= (code>>31); 328 code^= (code>>31);
315 //printf("v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code, state->bias, state->error_sum, state->drift, state->count, k); 329 //printf("v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code, state->bias, state->error_sum, state->drift, state->count, k);
316 set_ur_golomb(pb, code, k, 12, 8); 330 set_ur_golomb(pb, code, k, 12, bits);
317 331
318 update_vlc_state(state, v); 332 update_vlc_state(state, v);
319 } 333 }
320 334
321 static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state){ 335 static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int bits){
322 int k, i, v, ret; 336 int k, i, v, ret;
323 337
324 i= state->count; 338 i= state->count;
325 k=0; 339 k=0;
326 while(i < state->error_sum){ //FIXME optimize 340 while(i < state->error_sum){ //FIXME optimize
328 i += i; 342 i += i;
329 } 343 }
330 344
331 assert(k<=8); 345 assert(k<=8);
332 346
333 v= get_ur_golomb(gb, k, 12, 8); 347 v= get_ur_golomb(gb, k, 12, bits);
334 //printf("v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k); 348 //printf("v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k);
335 349
336 v++; 350 v++;
337 if(v&1) v= (v>>1); 351 if(v&1) v= (v>>1);
338 else v= -(v>>1); 352 else v= -(v>>1);
341 if(k==0 && 2*state->drift <= - state->count) v ^= (-1); 355 if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
342 #else 356 #else
343 v ^= ((2*state->drift + state->count)>>31); 357 v ^= ((2*state->drift + state->count)>>31);
344 #endif 358 #endif
345 359
346 ret= (int8_t)(v + state->bias); 360 ret= fold(v + state->bias, bits);
347 361
348 update_vlc_state(state, v); 362 update_vlc_state(state, v);
349 //printf("final: %d\n", ret); 363 //printf("final: %d\n", ret);
350 return ret; 364 return ret;
351 } 365 }
352 366
353 367 static always_inline void encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
354
355 static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
356 PlaneContext * const p= &s->plane[plane_index]; 368 PlaneContext * const p= &s->plane[plane_index];
357 CABACContext * const c= &s->c; 369 CABACContext * const c= &s->c;
370 int x;
371 int run_index= s->run_index;
372 int run_count=0;
373 int run_mode=0;
374
375 for(x=0; x<w; x++){
376 int diff, context;
377
378 context= get_context(s, sample[1]+x, sample[0]+x, sample[1]+x);
379 diff= sample[1][x] - predict(sample[1]+x, sample[0]+x);
380
381 if(context < 0){
382 context = -context;
383 diff= -diff;
384 }
385
386 diff= fold(diff, bits);
387
388 if(s->ac){
389 put_symbol(c, p->state[context], diff, 1, bits-1);
390 }else{
391 if(context == 0) run_mode=1;
392
393 if(run_mode){
394
395 if(diff){
396 while(run_count >= 1<<log2_run[run_index]){
397 run_count -= 1<<log2_run[run_index];
398 run_index++;
399 put_bits(&s->pb, 1, 1);
400 }
401
402 put_bits(&s->pb, 1 + log2_run[run_index], run_count);
403 if(run_index) run_index--;
404 run_count=0;
405 run_mode=0;
406 if(diff>0) diff--;
407 }else{
408 run_count++;
409 }
410 }
411
412 // printf("count:%d index:%d, mode:%d, x:%d y:%d pos:%d\n", run_count, run_index, run_mode, x, y, (int)get_bit_count(&s->pb));
413
414 if(run_mode == 0)
415 put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
416 }
417 }
418 if(run_mode){
419 while(run_count >= 1<<log2_run[run_index]){
420 run_count -= 1<<log2_run[run_index];
421 run_index++;
422 put_bits(&s->pb, 1, 1);
423 }
424
425 if(run_count)
426 put_bits(&s->pb, 1, 1);
427 }
428 s->run_index= run_index;
429 }
430
431 static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
358 int x,y; 432 int x,y;
359 uint8_t sample_buffer[2][w+6]; 433 int_fast16_t sample_buffer[2][w+6];
360 uint8_t *sample[2]= {sample_buffer[0]+3, sample_buffer[1]+3}; 434 int_fast16_t *sample[2]= {sample_buffer[0]+3, sample_buffer[1]+3};
361 int run_index=0; 435 s->run_index=0;
362 436
363 memset(sample_buffer, 0, sizeof(sample_buffer)); 437 memset(sample_buffer, 0, sizeof(sample_buffer));
364 438
365 for(y=0; y<h; y++){ 439 for(y=0; y<h; y++){
366 uint8_t *temp= sample[0]; //FIXME try a normal buffer 440 int_fast16_t *temp= sample[0]; //FIXME try a normal buffer
367 int run_count=0;
368 int run_mode=0;
369 441
370 sample[0]= sample[1]; 442 sample[0]= sample[1];
371 sample[1]= temp; 443 sample[1]= temp;
372 444
373 sample[1][-1]= sample[0][0 ]; 445 sample[1][-1]= sample[0][0 ];
374 sample[0][ w]= sample[0][w-1]; 446 sample[0][ w]= sample[0][w-1];
375 447 //{START_TIMER
376 for(x=0; x<w; x++){ 448 for(x=0; x<w; x++){
377 uint8_t *temp_src= src + x + stride*y; 449 sample[1][x]= src[x + stride*y];
378 int diff, context; 450 }
451 encode_line(s, w, sample, plane_index, 8);
452 //STOP_TIMER("encode line")}
453 }
454 }
455
456 static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
457 int x, y, p;
458 int_fast16_t sample_buffer[3][2][w+6];
459 int_fast16_t *sample[3][2]= {
460 {sample_buffer[0][0]+3, sample_buffer[0][1]+3},
461 {sample_buffer[1][0]+3, sample_buffer[1][1]+3},
462 {sample_buffer[2][0]+3, sample_buffer[2][1]+3}};
463 s->run_index=0;
464
465 memset(sample_buffer, 0, sizeof(sample_buffer));
466
467 for(y=0; y<h; y++){
468 for(x=0; x<w; x++){
469 int v= src[x + stride*y];
470 int b= v&0xFF;
471 int g= (v>>8)&0xFF;
472 int r= (v>>16)&0xFF;
379 473
380 context= get_context(s, sample[1]+x, sample[0]+x, sample[1]+x); 474 b -= g;
381 diff= temp_src[0] - predict(sample[1]+x, sample[0]+x); 475 r -= g;
382 476 g += (b + r)>>2;
383 if(context < 0){ 477 b += 0x100;
384 context = -context; 478 r += 0x100;
385 diff= -diff; 479
386 } 480 // assert(g>=0 && b>=0 && r>=0);
387 481 // assert(g<256 && b<512 && r<512);
388 diff= (int8_t)diff; 482 sample[0][0][x]= g;
389 483 sample[1][0][x]= b;
390 if(s->ac){ 484 sample[2][0][x]= r;
391 put_symbol(c, p->state[context], diff, 1, 7); 485 }
392 }else{ 486 for(p=0; p<3; p++){
393 if(context == 0) run_mode=1; 487 int_fast16_t *temp= sample[p][0]; //FIXME try a normal buffer
394 488
395 if(run_mode){ 489 sample[p][0]= sample[p][1];
396 490 sample[p][1]= temp;
397 if(diff){ 491
398 while(run_count >= 1<<log2_run[run_index]){ 492 sample[p][1][-1]= sample[p][0][0 ];
399 run_count -= 1<<log2_run[run_index]; 493 sample[p][0][ w]= sample[p][0][w-1];
400 run_index++; 494 encode_line(s, w, sample[p], FFMIN(p, 1), 9);
401 put_bits(&s->pb, 1, 1);
402 }
403
404 put_bits(&s->pb, 1 + log2_run[run_index], run_count);
405 if(run_index) run_index--;
406 run_count=0;
407 run_mode=0;
408 if(diff>0) diff--;
409 }else{
410 run_count++;
411 }
412 }
413
414 // printf("count:%d index:%d, mode:%d, x:%d y:%d pos:%d\n", run_count, run_index, run_mode, x, y, (int)get_bit_count(&s->pb));
415
416 if(run_mode == 0)
417 put_vlc_symbol(&s->pb, &p->vlc_state[context], diff);
418 }
419
420 sample[1][x]= temp_src[0];
421 }
422 if(run_mode){
423 while(run_count >= 1<<log2_run[run_index]){
424 run_count -= 1<<log2_run[run_index];
425 run_index++;
426 put_bits(&s->pb, 1, 1);
427 }
428
429 if(run_count)
430 put_bits(&s->pb, 1, 1);
431 } 495 }
432 } 496 }
433 } 497 }
434 498
435 static void write_quant_table(CABACContext *c, int16_t *quant_table){ 499 static void write_quant_table(CABACContext *c, int16_t *quant_table){
451 int i; 515 int i;
452 CABACContext * const c= &f->c; 516 CABACContext * const c= &f->c;
453 517
454 put_symbol(c, state, f->version, 0, 7); 518 put_symbol(c, state, f->version, 0, 7);
455 put_symbol(c, state, f->avctx->coder_type, 0, 7); 519 put_symbol(c, state, f->avctx->coder_type, 0, 7);
456 put_symbol(c, state, 0, 0, 7); //YUV cs type 520 put_symbol(c, state, f->colorspace, 0, 7); //YUV cs type
457 put_cabac(c, state, 1); //chroma planes 521 put_cabac(c, state, 1); //chroma planes
458 put_symbol(c, state, f->chroma_h_shift, 0, 7); 522 put_symbol(c, state, f->chroma_h_shift, 0, 7);
459 put_symbol(c, state, f->chroma_v_shift, 0, 7); 523 put_symbol(c, state, f->chroma_v_shift, 0, 7);
460 put_cabac(c, state, 0); //no transparency plane 524 put_cabac(c, state, 0); //no transparency plane
461 525
526 case PIX_FMT_YUV444P: 590 case PIX_FMT_YUV444P:
527 case PIX_FMT_YUV422P: 591 case PIX_FMT_YUV422P:
528 case PIX_FMT_YUV420P: 592 case PIX_FMT_YUV420P:
529 case PIX_FMT_YUV411P: 593 case PIX_FMT_YUV411P:
530 case PIX_FMT_YUV410P: 594 case PIX_FMT_YUV410P:
531 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift); 595 s->colorspace= 0;
596 break;
597 case PIX_FMT_RGBA32:
598 s->colorspace= 1;
532 break; 599 break;
533 default: 600 default:
534 fprintf(stderr, "format not supported\n"); 601 fprintf(stderr, "format not supported\n");
535 return -1; 602 return -1;
536 } 603 }
537 604 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
538 605
539 s->picture_number=0; 606 s->picture_number=0;
540 607
541 return 0; 608 return 0;
542 } 609 }
543 610
600 used_count += put_cabac_terminate(c, 1); 667 used_count += put_cabac_terminate(c, 1);
601 //printf("pos=%d\n", used_count); 668 //printf("pos=%d\n", used_count);
602 init_put_bits(&f->pb, buf + used_count, buf_size - used_count); 669 init_put_bits(&f->pb, buf + used_count, buf_size - used_count);
603 } 670 }
604 671
605 if(1){ 672 if(f->colorspace==0){
606 const int chroma_width = -((-width )>>f->chroma_h_shift); 673 const int chroma_width = -((-width )>>f->chroma_h_shift);
607 const int chroma_height= -((-height)>>f->chroma_v_shift); 674 const int chroma_height= -((-height)>>f->chroma_v_shift);
608 675
609 encode_plane(f, p->data[0], width, height, p->linesize[0], 0); 676 encode_plane(f, p->data[0], width, height, p->linesize[0], 0);
610 677
611 encode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1); 678 encode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
612 encode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1); 679 encode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
680 }else{
681 encode_rgb_frame(f, (uint32_t*)(p->data[0]), width, height, p->linesize[0]/4);
613 } 682 }
614 emms_c(); 683 emms_c();
615 684
616 f->picture_number++; 685 f->picture_number++;
617 686
640 common_end(s); 709 common_end(s);
641 710
642 return 0; 711 return 0;
643 } 712 }
644 713
645 static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){ 714 static always_inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
646 PlaneContext * const p= &s->plane[plane_index]; 715 PlaneContext * const p= &s->plane[plane_index];
647 CABACContext * const c= &s->c; 716 CABACContext * const c= &s->c;
648 int x,y; 717 int x;
649 uint8_t sample_buffer[2][w+6]; 718 int run_count=0;
650 uint8_t *sample[2]= {sample_buffer[0]+3, sample_buffer[1]+3}; 719 int run_mode=0;
651 int run_index=0; 720 int run_index= s->run_index;
721
722 for(x=0; x<w; x++){
723 int diff, context, sign;
724
725 context= get_context(s, sample[1] + x, sample[0] + x, sample[1] + x);
726 if(context < 0){
727 context= -context;
728 sign=1;
729 }else
730 sign=0;
731
732
733 if(s->ac)
734 diff= get_symbol(c, p->state[context], 1, bits-1);
735 else{
736 if(context == 0 && run_mode==0) run_mode=1;
737
738 if(run_mode){
739 if(run_count==0 && run_mode==1){
740 if(get_bits1(&s->gb)){
741 run_count = 1<<log2_run[run_index];
742 if(x + run_count <= w) run_index++;
743 }else{
744 if(log2_run[run_index]) run_count = get_bits(&s->gb, log2_run[run_index]);
745 else run_count=0;
746 if(run_index) run_index--;
747 run_mode=2;
748 }
749 }
750 run_count--;
751 if(run_count < 0){
752 run_mode=0;
753 run_count=0;
754 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
755 if(diff>=0) diff++;
756 }else
757 diff=0;
758 }else
759 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
760
761 // printf("count:%d index:%d, mode:%d, x:%d y:%d pos:%d\n", run_count, run_index, run_mode, x, y, get_bits_count(&s->gb));
762 }
763
764 if(sign) diff= -diff;
765
766 sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
767 }
768 s->run_index= run_index;
769 }
770
771 static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
772 int x, y;
773 int_fast16_t sample_buffer[2][w+6];
774 int_fast16_t *sample[2]= {sample_buffer[0]+3, sample_buffer[1]+3};
775
776 s->run_index=0;
652 777
653 memset(sample_buffer, 0, sizeof(sample_buffer)); 778 memset(sample_buffer, 0, sizeof(sample_buffer));
654 779
655 for(y=0; y<h; y++){ 780 for(y=0; y<h; y++){
656 uint8_t *temp= sample[0]; //FIXME try a normal buffer 781 int_fast16_t *temp= sample[0]; //FIXME try a normal buffer
657 int run_count=0;
658 int run_mode=0;
659 782
660 sample[0]= sample[1]; 783 sample[0]= sample[1];
661 sample[1]= temp; 784 sample[1]= temp;
662 785
663 sample[1][-1]= sample[0][0 ]; 786 sample[1][-1]= sample[0][0 ];
664 sample[0][ w]= sample[0][w-1]; 787 sample[0][ w]= sample[0][w-1];
665 788
789 //{START_TIMER
790 decode_line(s, w, sample, plane_index, 8);
666 for(x=0; x<w; x++){ 791 for(x=0; x<w; x++){
667 uint8_t *temp_src= src + x + stride*y; 792 src[x + stride*y]= sample[1][x];
668 int diff, context, sign; 793 }
669 794 //STOP_TIMER("decode-line")}
670 context= get_context(s, sample[1] + x, sample[0] + x, sample[1] + x); 795 }
671 if(context < 0){ 796 }
672 context= -context; 797
673 sign=1; 798 static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
674 }else 799 int x, y, p;
675 sign=0; 800 int_fast16_t sample_buffer[3][2][w+6];
801 int_fast16_t *sample[3][2]= {
802 {sample_buffer[0][0]+3, sample_buffer[0][1]+3},
803 {sample_buffer[1][0]+3, sample_buffer[1][1]+3},
804 {sample_buffer[2][0]+3, sample_buffer[2][1]+3}};
805
806 s->run_index=0;
807
808 memset(sample_buffer, 0, sizeof(sample_buffer));
809
810 for(y=0; y<h; y++){
811 for(p=0; p<3; p++){
812 int_fast16_t *temp= sample[p][0]; //FIXME try a normal buffer
813
814 sample[p][0]= sample[p][1];
815 sample[p][1]= temp;
816
817 sample[p][1][-1]= sample[p][0][0 ];
818 sample[p][0][ w]= sample[p][0][w-1];
819 decode_line(s, w, sample[p], FFMIN(p, 1), 9);
820 }
821 for(x=0; x<w; x++){
822 int g= sample[0][1][x];
823 int b= sample[1][1][x];
824 int r= sample[2][1][x];
825
826 // assert(g>=0 && b>=0 && r>=0);
827 // assert(g<256 && b<512 && r<512);
676 828
677 829 b -= 0x100;
678 if(s->ac) 830 r -= 0x100;
679 diff= get_symbol(c, p->state[context], 1, 7); 831 g -= (b + r)>>2;
680 else{ 832 b += g;
681 if(context == 0 && run_mode==0) run_mode=1; 833 r += g;
682
683 if(run_mode){
684 if(run_count==0 && run_mode==1){
685 if(get_bits1(&s->gb)){
686 run_count = 1<<log2_run[run_index];
687 if(x + run_count <= w) run_index++;
688 }else{
689 if(log2_run[run_index]) run_count = get_bits(&s->gb, log2_run[run_index]);
690 else run_count=0;
691 if(run_index) run_index--;
692 run_mode=2;
693 }
694 }
695 run_count--;
696 if(run_count < 0){
697 run_mode=0;
698 run_count=0;
699 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context]);
700 if(diff>=0) diff++;
701 }else
702 diff=0;
703 }else
704 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context]);
705
706 // printf("count:%d index:%d, mode:%d, x:%d y:%d pos:%d\n", run_count, run_index, run_mode, x, y, get_bits_count(&s->gb));
707 }
708
709 if(sign) diff= (int8_t)(-diff); //FIXME remove cast
710
711 sample[1][x]=
712 temp_src[0] = predict(sample[1] + x, sample[0] + x) + diff;
713 834
714 assert(diff>= -128 && diff <= 127); 835 src[x + stride*y]= b + (g<<8) + (r<<16);
715 } 836 }
716 } 837 }
717 } 838 }
718 839
719 static int read_quant_table(CABACContext *c, int16_t *quant_table, int scale){ 840 static int read_quant_table(CABACContext *c, int16_t *quant_table, int scale){
747 int i, context_count; 868 int i, context_count;
748 CABACContext * const c= &f->c; 869 CABACContext * const c= &f->c;
749 870
750 f->version= get_symbol(c, state, 0, 7); 871 f->version= get_symbol(c, state, 0, 7);
751 f->ac= f->avctx->coder_type= get_symbol(c, state, 0, 7); 872 f->ac= f->avctx->coder_type= get_symbol(c, state, 0, 7);
752 get_symbol(c, state, 0, 7); //YUV cs type 873 f->colorspace= get_symbol(c, state, 0, 7); //YUV cs type
753 get_cabac(c, state); //no chroma = false 874 get_cabac(c, state); //no chroma = false
754 f->chroma_h_shift= get_symbol(c, state, 0, 7); 875 f->chroma_h_shift= get_symbol(c, state, 0, 7);
755 f->chroma_v_shift= get_symbol(c, state, 0, 7); 876 f->chroma_v_shift= get_symbol(c, state, 0, 7);
756 get_cabac(c, state); //transparency plane 877 get_cabac(c, state); //transparency plane
757 f->plane_count= 2; 878 f->plane_count= 2;
758 879
759 switch(16*f->chroma_h_shift + f->chroma_v_shift){ 880 if(f->colorspace==0){
760 case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break; 881 switch(16*f->chroma_h_shift + f->chroma_v_shift){
761 case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break; 882 case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
762 case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break; 883 case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
763 case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break; 884 case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
764 case 0x33: f->avctx->pix_fmt= PIX_FMT_YUV410P; break; 885 case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
765 default: 886 case 0x33: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
766 fprintf(stderr, "format not supported\n"); 887 default:
888 fprintf(stderr, "format not supported\n");
889 return -1;
890 }
891 }else if(f->colorspace==1){
892 if(f->chroma_h_shift || f->chroma_v_shift){
893 fprintf(stderr, "chroma subsampling not supported in this colorspace\n");
894 return -1;
895 }
896 f->avctx->pix_fmt= PIX_FMT_RGBA32;
897 }else{
898 fprintf(stderr, "colorspace not supported\n");
767 return -1; 899 return -1;
768 } 900 }
901
769 //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt); 902 //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
770 903
771 context_count=1; 904 context_count=1;
772 for(i=0; i<5; i++){ 905 for(i=0; i<5; i++){
773 context_count*= read_quant_table(c, f->quant_table[i], context_count); 906 context_count*= read_quant_table(c, f->quant_table[i], context_count);
796 static int decode_init(AVCodecContext *avctx) 929 static int decode_init(AVCodecContext *avctx)
797 { 930 {
798 // FFV1Context *s = avctx->priv_data; 931 // FFV1Context *s = avctx->priv_data;
799 932
800 common_init(avctx); 933 common_init(avctx);
801
802 #if 0
803 switch(s->bitstream_bpp){
804 case 12:
805 avctx->pix_fmt = PIX_FMT_YUV420P;
806 break;
807 case 16:
808 avctx->pix_fmt = PIX_FMT_YUV422P;
809 break;
810 case 24:
811 case 32:
812 if(s->bgr32){
813 avctx->pix_fmt = PIX_FMT_RGBA32;
814 }else{
815 avctx->pix_fmt = PIX_FMT_BGR24;
816 }
817 break;
818 default:
819 assert(0);
820 }
821 #endif
822 934
823 return 0; 935 return 0;
824 } 936 }
825 937
826 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){ 938 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){
867 init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read); 979 init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read);
868 } else { 980 } else {
869 bytes_read = 0; /* avoid warning */ 981 bytes_read = 0; /* avoid warning */
870 } 982 }
871 983
872 if(1){ 984 if(f->colorspace==0){
873 const int chroma_width = -((-width )>>f->chroma_h_shift); 985 const int chroma_width = -((-width )>>f->chroma_h_shift);
874 const int chroma_height= -((-height)>>f->chroma_v_shift); 986 const int chroma_height= -((-height)>>f->chroma_v_shift);
875 decode_plane(f, p->data[0], width, height, p->linesize[0], 0); 987 decode_plane(f, p->data[0], width, height, p->linesize[0], 0);
876 988
877 decode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1); 989 decode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
878 decode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1); 990 decode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
991 }else{
992 decode_rgb_frame(f, (uint32_t*)p->data[0], width, height, p->linesize[0]/4);
879 } 993 }
880 994
881 emms_c(); 995 emms_c();
882 996
883 f->picture_number++; 997 f->picture_number++;