comparison huffyuv.c @ 868:dfe7d66da6f0 libavcodec

YV12 support (warning this is experimental & wont work with offical huffyuv but there is a approx. 20% speed & compression gain) 10l flush_put_bits()
author michaelni
date Thu, 14 Nov 2002 22:25:20 +0000
parents 725ef4ea3ecc
children 028638e65523
comparison
equal deleted inserted replaced
867:48215b2c3888 868:dfe7d66da6f0
326 s->bgr32=1; 326 s->bgr32=1;
327 assert(width && height); 327 assert(width && height);
328 //if(avctx->extradata) 328 //if(avctx->extradata)
329 // printf("extradata:%X, extradata_size:%d\n", *(uint32_t*)avctx->extradata, avctx->extradata_size); 329 // printf("extradata:%X, extradata_size:%d\n", *(uint32_t*)avctx->extradata, avctx->extradata_size);
330 if(avctx->extradata_size){ 330 if(avctx->extradata_size){
331 if(avctx->bits_per_sample&7) 331 if((avctx->bits_per_sample&7) && avctx->bits_per_sample != 12)
332 s->version=1; // do such files exist at all? 332 s->version=1; // do such files exist at all?
333 else 333 else
334 s->version=2; 334 s->version=2;
335 }else 335 }else
336 s->version=0; 336 s->version=0;
471 avctx->extradata= av_mallocz(1024*10); 471 avctx->extradata= av_mallocz(1024*10);
472 avctx->stats_out= av_mallocz(1024*10); 472 avctx->stats_out= av_mallocz(1024*10);
473 s->version=2; 473 s->version=2;
474 474
475 switch(avctx->pix_fmt){ 475 switch(avctx->pix_fmt){
476 case PIX_FMT_YUV420P:
477 if(avctx->strict_std_compliance>=0){
478 fprintf(stderr, "YV12-huffyuv is experimental, there WILL be no compatbility! (use (v)strict=-1)\n");
479 return -1;
480 }
481 s->bitstream_bpp= 12;
482 break;
476 case PIX_FMT_YUV422P: 483 case PIX_FMT_YUV422P:
477 s->bitstream_bpp= 16; 484 s->bitstream_bpp= 16;
478 break; 485 break;
479 default: 486 default:
480 fprintf(stderr, "format not supported\n"); 487 fprintf(stderr, "format not supported\n");
549 for(i=0; i<count; i++){ 556 for(i=0; i<count; i++){
550 s->temp[0][2*i ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); 557 s->temp[0][2*i ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3);
551 s->temp[1][ i ]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3); 558 s->temp[1][ i ]= get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3);
552 s->temp[0][2*i+1]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3); 559 s->temp[0][2*i+1]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3);
553 s->temp[2][ i ]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); 560 s->temp[2][ i ]= get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3);
561 }
562 }
563
564 static void decode_gray_bitstream(HYuvContext *s, int count){
565 int i;
566
567 count/=2;
568
569 for(i=0; i<count; i++){
570 s->temp[0][2*i ]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3);
571 s->temp[0][2*i+1]= get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3);
554 } 572 }
555 } 573 }
556 574
557 static void encode_422_bitstream(HYuvContext *s, int count){ 575 static void encode_422_bitstream(HYuvContext *s, int count){
558 int i; 576 int i;
569 for(i=0; i<count; i++){ 587 for(i=0; i<count; i++){
570 put_bits(&s->pb, s->len[0][ s->temp[0][2*i ] ], s->bits[0][ s->temp[0][2*i ] ]); 588 put_bits(&s->pb, s->len[0][ s->temp[0][2*i ] ], s->bits[0][ s->temp[0][2*i ] ]);
571 put_bits(&s->pb, s->len[1][ s->temp[1][ i ] ], s->bits[1][ s->temp[1][ i ] ]); 589 put_bits(&s->pb, s->len[1][ s->temp[1][ i ] ], s->bits[1][ s->temp[1][ i ] ]);
572 put_bits(&s->pb, s->len[0][ s->temp[0][2*i+1] ], s->bits[0][ s->temp[0][2*i+1] ]); 590 put_bits(&s->pb, s->len[0][ s->temp[0][2*i+1] ], s->bits[0][ s->temp[0][2*i+1] ]);
573 put_bits(&s->pb, s->len[2][ s->temp[2][ i ] ], s->bits[2][ s->temp[2][ i ] ]); 591 put_bits(&s->pb, s->len[2][ s->temp[2][ i ] ], s->bits[2][ s->temp[2][ i ] ]);
592 }
593 }
594 }
595
596 static void encode_gray_bitstream(HYuvContext *s, int count){
597 int i;
598
599 count/=2;
600 if(s->flags&CODEC_FLAG_PASS1){
601 for(i=0; i<count; i++){
602 s->stats[0][ s->temp[0][2*i ] ]++;
603 s->stats[0][ s->temp[0][2*i+1] ]++;
604 }
605 }else{
606 for(i=0; i<count; i++){
607 put_bits(&s->pb, s->len[0][ s->temp[0][2*i ] ], s->bits[0][ s->temp[0][2*i ] ]);
608 put_bits(&s->pb, s->len[0][ s->temp[0][2*i+1] ], s->bits[0][ s->temp[0][2*i+1] ]);
574 } 609 }
575 } 610 }
576 } 611 }
577 612
578 static void decode_bgr_bitstream(HYuvContext *s, int count){ 613 static void decode_bgr_bitstream(HYuvContext *s, int count){
632 bswap_buf((uint32_t*)s->bitstream_buffer, (uint32_t*)buf, buf_size/4); 667 bswap_buf((uint32_t*)s->bitstream_buffer, (uint32_t*)buf, buf_size/4);
633 668
634 init_get_bits(&s->gb, s->bitstream_buffer, buf_size); 669 init_get_bits(&s->gb, s->bitstream_buffer, buf_size);
635 670
636 if(s->bitstream_bpp<24){ 671 if(s->bitstream_bpp<24){
637 int y; 672 int y, cy;
638 int lefty, leftu, leftv; 673 int lefty, leftu, leftv;
639 int lefttopy, lefttopu, lefttopv; 674 int lefttopy, lefttopu, lefttopv;
640 675
641 if(s->yuy2){ 676 if(s->yuy2){
642 s->picture[0][3]= get_bits(&s->gb, 8); 677 s->picture[0][3]= get_bits(&s->gb, 8);
661 if(!(s->flags&CODEC_FLAG_GRAY)){ 696 if(!(s->flags&CODEC_FLAG_GRAY)){
662 leftu= add_left_prediction(s->picture[1] + 1, s->temp[1], width2-1, leftu); 697 leftu= add_left_prediction(s->picture[1] + 1, s->temp[1], width2-1, leftu);
663 leftv= add_left_prediction(s->picture[2] + 1, s->temp[2], width2-1, leftv); 698 leftv= add_left_prediction(s->picture[2] + 1, s->temp[2], width2-1, leftv);
664 } 699 }
665 700
666 for(y=1; y<s->height; y++){ 701 for(cy=y=1; y<s->height; y++,cy++){
667 uint8_t *ydst, *udst, *vdst; 702 uint8_t *ydst, *udst, *vdst;
668 decode_422_bitstream(s, width); 703
704 if(s->bitstream_bpp==12){
705 decode_gray_bitstream(s, width);
706
707 ydst= s->picture[0] + s->linesize[0]*y;
708
709 lefty= add_left_prediction(ydst, s->temp[0], width, lefty);
710 if(s->predictor == PLANE){
711 if(y>s->interlaced)
712 s->dsp.add_bytes(ydst, ydst - fake_ystride, width);
713 }
714 y++;
715 if(y>=s->height) break;
716 }
669 717
670 ydst= s->picture[0] + s->linesize[0]*y; 718 ydst= s->picture[0] + s->linesize[0]*y;
671 udst= s->picture[1] + s->linesize[1]*y; 719 udst= s->picture[1] + s->linesize[1]*cy;
672 vdst= s->picture[2] + s->linesize[2]*y; 720 vdst= s->picture[2] + s->linesize[2]*cy;
721
722 decode_422_bitstream(s, width);
673 723
674 lefty= add_left_prediction(ydst, s->temp[0], width, lefty); 724 lefty= add_left_prediction(ydst, s->temp[0], width, lefty);
675 if(!(s->flags&CODEC_FLAG_GRAY)){ 725 if(!(s->flags&CODEC_FLAG_GRAY)){
676 leftu= add_left_prediction(udst, s->temp[1], width2, leftu); 726 leftu= add_left_prediction(udst, s->temp[1], width2, leftu);
677 leftv= add_left_prediction(vdst, s->temp[2], width2, leftv); 727 leftv= add_left_prediction(vdst, s->temp[2], width2, leftv);
678 } 728 }
679 if(s->predictor == PLANE){ 729 if(s->predictor == PLANE){
680 if(y>s->interlaced){ 730 if(cy>s->interlaced){
681 s->dsp.add_bytes(ydst, ydst - fake_ystride, width); 731 s->dsp.add_bytes(ydst, ydst - fake_ystride, width);
682 if(!(s->flags&CODEC_FLAG_GRAY)){ 732 if(!(s->flags&CODEC_FLAG_GRAY)){
683 s->dsp.add_bytes(udst, udst - fake_ustride, width2); 733 s->dsp.add_bytes(udst, udst - fake_ustride, width2);
684 s->dsp.add_bytes(vdst, vdst - fake_vstride, width2); 734 s->dsp.add_bytes(vdst, vdst - fake_vstride, width2);
685 } 735 }
694 if(!(s->flags&CODEC_FLAG_GRAY)){ 744 if(!(s->flags&CODEC_FLAG_GRAY)){
695 leftu= add_left_prediction(s->picture[1] + 1, s->temp[1], width2-1, leftu); 745 leftu= add_left_prediction(s->picture[1] + 1, s->temp[1], width2-1, leftu);
696 leftv= add_left_prediction(s->picture[2] + 1, s->temp[2], width2-1, leftv); 746 leftv= add_left_prediction(s->picture[2] + 1, s->temp[2], width2-1, leftv);
697 } 747 }
698 748
699 y=1; 749 cy=y=1;
700 750
701 /* second line is left predicted for interlaced case */ 751 /* second line is left predicted for interlaced case */
702 if(s->interlaced){ 752 if(s->interlaced){
703 decode_422_bitstream(s, width); 753 decode_422_bitstream(s, width);
704 lefty= add_left_prediction(s->picture[0] + s->linesize[0], s->temp[0], width, lefty); 754 lefty= add_left_prediction(s->picture[0] + s->linesize[0], s->temp[0], width, lefty);
705 if(!(s->flags&CODEC_FLAG_GRAY)){ 755 if(!(s->flags&CODEC_FLAG_GRAY)){
706 leftu= add_left_prediction(s->picture[1] + s->linesize[2], s->temp[1], width2, leftu); 756 leftu= add_left_prediction(s->picture[1] + s->linesize[2], s->temp[1], width2, leftu);
707 leftv= add_left_prediction(s->picture[2] + s->linesize[1], s->temp[2], width2, leftv); 757 leftv= add_left_prediction(s->picture[2] + s->linesize[1], s->temp[2], width2, leftv);
708 } 758 }
709 y++; 759 y++; cy++;
710 } 760 }
711 761
712 /* next 4 pixels are left predicted too */ 762 /* next 4 pixels are left predicted too */
713 decode_422_bitstream(s, 4); 763 decode_422_bitstream(s, 4);
714 lefty= add_left_prediction(s->picture[0] + fake_ystride, s->temp[0], 4, lefty); 764 lefty= add_left_prediction(s->picture[0] + fake_ystride, s->temp[0], 4, lefty);
725 lefttopu= s->picture[1][1]; 775 lefttopu= s->picture[1][1];
726 lefttopv= s->picture[2][1]; 776 lefttopv= s->picture[2][1];
727 add_median_prediction(s->picture[1] + fake_ustride+2, s->picture[1]+2, s->temp[1], width2-2, &leftu, &lefttopu); 777 add_median_prediction(s->picture[1] + fake_ustride+2, s->picture[1]+2, s->temp[1], width2-2, &leftu, &lefttopu);
728 add_median_prediction(s->picture[2] + fake_vstride+2, s->picture[2]+2, s->temp[2], width2-2, &leftv, &lefttopv); 778 add_median_prediction(s->picture[2] + fake_vstride+2, s->picture[2]+2, s->temp[2], width2-2, &leftv, &lefttopv);
729 } 779 }
730 y++; 780 y++; cy++;
731 781
732 for(; y<height; y++){ 782 for(; y<height; y++,cy++){
733 uint8_t *ydst, *udst, *vdst; 783 uint8_t *ydst, *udst, *vdst;
784
785 if(s->bitstream_bpp==12){
786 while(2*cy > y){
787 decode_gray_bitstream(s, width);
788 ydst= s->picture[0] + s->linesize[0]*y;
789 add_median_prediction(ydst, ydst - fake_ystride, s->temp[0], width, &lefty, &lefttopy);
790 y++;
791 }
792 if(y>=height) break;
793 }
794
734 decode_422_bitstream(s, width); 795 decode_422_bitstream(s, width);
735 796
736 ydst= s->picture[0] + s->linesize[0]*y; 797 ydst= s->picture[0] + s->linesize[0]*y;
737 udst= s->picture[1] + s->linesize[1]*y; 798 udst= s->picture[1] + s->linesize[1]*cy;
738 vdst= s->picture[2] + s->linesize[2]*y; 799 vdst= s->picture[2] + s->linesize[2]*cy;
739 800
740 add_median_prediction(ydst, ydst - fake_ystride, s->temp[0], width, &lefty, &lefttopy); 801 add_median_prediction(ydst, ydst - fake_ystride, s->temp[0], width, &lefty, &lefttopy);
741 if(!(s->flags&CODEC_FLAG_GRAY)){ 802 if(!(s->flags&CODEC_FLAG_GRAY)){
742 add_median_prediction(udst, udst - fake_ustride, s->temp[1], width2, &leftu, &lefttopu); 803 add_median_prediction(udst, udst - fake_ustride, s->temp[1], width2, &leftu, &lefttopu);
743 add_median_prediction(vdst, vdst - fake_vstride, s->temp[2], width2, &leftv, &lefttopv); 804 add_median_prediction(vdst, vdst - fake_vstride, s->temp[2], width2, &leftv, &lefttopv);
832 for(i=0; i<3; i++){ 893 for(i=0; i<3; i++){
833 s->picture[i]= pict->data[i]; 894 s->picture[i]= pict->data[i];
834 s->linesize[i]= pict->linesize[i]; 895 s->linesize[i]= pict->linesize[i];
835 } 896 }
836 897
837 if(avctx->pix_fmt == PIX_FMT_YUV422P){ 898 if(avctx->pix_fmt == PIX_FMT_YUV422P || avctx->pix_fmt == PIX_FMT_YUV420P){
838 int lefty, leftu, leftv, y; 899 int lefty, leftu, leftv, y, cy;
839 900
840 put_bits(&s->pb, 8, leftv= s->picture[2][0]); 901 put_bits(&s->pb, 8, leftv= s->picture[2][0]);
841 put_bits(&s->pb, 8, lefty= s->picture[0][1]); 902 put_bits(&s->pb, 8, lefty= s->picture[0][1]);
842 put_bits(&s->pb, 8, leftu= s->picture[1][0]); 903 put_bits(&s->pb, 8, leftu= s->picture[1][0]);
843 put_bits(&s->pb, 8, s->picture[0][0]); 904 put_bits(&s->pb, 8, s->picture[0][0]);
848 909
849 encode_422_bitstream(s, width-2); 910 encode_422_bitstream(s, width-2);
850 911
851 if(s->predictor==MEDIAN){ 912 if(s->predictor==MEDIAN){
852 int lefttopy, lefttopu, lefttopv; 913 int lefttopy, lefttopu, lefttopv;
853 y=1; 914 cy=y=1;
854 if(s->interlaced){ 915 if(s->interlaced){
855 lefty= sub_left_prediction(s->temp[0], s->picture[0]+s->linesize[0], width , lefty); 916 lefty= sub_left_prediction(s->temp[0], s->picture[0]+s->linesize[0], width , lefty);
856 leftu= sub_left_prediction(s->temp[1], s->picture[1]+s->linesize[1], width2, leftu); 917 leftu= sub_left_prediction(s->temp[1], s->picture[1]+s->linesize[1], width2, leftu);
857 leftv= sub_left_prediction(s->temp[2], s->picture[2]+s->linesize[2], width2, leftv); 918 leftv= sub_left_prediction(s->temp[2], s->picture[2]+s->linesize[2], width2, leftv);
858 919
859 encode_422_bitstream(s, width); 920 encode_422_bitstream(s, width);
860 y++; 921 y++; cy++;
861 } 922 }
862 923
863 lefty= sub_left_prediction(s->temp[0], s->picture[0]+fake_ystride, 4, lefty); 924 lefty= sub_left_prediction(s->temp[0], s->picture[0]+fake_ystride, 4, lefty);
864 leftu= sub_left_prediction(s->temp[1], s->picture[1]+fake_ystride, 2, leftu); 925 leftu= sub_left_prediction(s->temp[1], s->picture[1]+fake_ystride, 2, leftu);
865 leftv= sub_left_prediction(s->temp[2], s->picture[2]+fake_ystride, 2, leftv); 926 leftv= sub_left_prediction(s->temp[2], s->picture[2]+fake_ystride, 2, leftv);
866 927
867 encode_422_bitstream(s, 4); 928 encode_422_bitstream(s, 4);
868 929
869 lefttopy= s->picture[0][3]; 930 lefttopy= s->picture[0][3];
870 lefttopu= s->picture[1][1]; 931 lefttopu= s->picture[1][1];
871 lefttopv= s->picture[2][1]; 932 lefttopv= s->picture[2][1];
872 sub_median_prediction(s->temp[0], s->picture[0]+4, s->picture[0] + fake_ystride+4, width-4 , &lefty, &lefttopy); 933 sub_median_prediction(s->temp[0], s->picture[0]+4, s->picture[0] + fake_ystride+4, width-4 , &lefty, &lefttopy);
873 sub_median_prediction(s->temp[1], s->picture[1]+2, s->picture[1] + fake_ustride+2, width2-2, &leftu, &lefttopu); 934 sub_median_prediction(s->temp[1], s->picture[1]+2, s->picture[1] + fake_ustride+2, width2-2, &leftu, &lefttopu);
874 sub_median_prediction(s->temp[2], s->picture[2]+2, s->picture[2] + fake_vstride+2, width2-2, &leftv, &lefttopv); 935 sub_median_prediction(s->temp[2], s->picture[2]+2, s->picture[2] + fake_vstride+2, width2-2, &leftv, &lefttopv);
875 encode_422_bitstream(s, width-4); 936 encode_422_bitstream(s, width-4);
876 y++; 937 y++; cy++;
877 938
878 for(; y<height; y++){ 939 for(; y<height; y++,cy++){
879 uint8_t *ydst, *udst, *vdst; 940 uint8_t *ydst, *udst, *vdst;
880 941
942 if(s->bitstream_bpp==12){
943 while(2*cy > y){
944 ydst= s->picture[0] + s->linesize[0]*y;
945 sub_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy);
946 encode_gray_bitstream(s, width);
947 y++;
948 }
949 if(y>=height) break;
950 }
881 ydst= s->picture[0] + s->linesize[0]*y; 951 ydst= s->picture[0] + s->linesize[0]*y;
882 udst= s->picture[1] + s->linesize[1]*y; 952 udst= s->picture[1] + s->linesize[1]*cy;
883 vdst= s->picture[2] + s->linesize[2]*y; 953 vdst= s->picture[2] + s->linesize[2]*cy;
884 954
885 sub_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy); 955 sub_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy);
886 sub_median_prediction(s->temp[1], udst - fake_ustride, udst, width2, &leftu, &lefttopu); 956 sub_median_prediction(s->temp[1], udst - fake_ustride, udst, width2, &leftu, &lefttopu);
887 sub_median_prediction(s->temp[2], vdst - fake_vstride, vdst, width2, &leftv, &lefttopv); 957 sub_median_prediction(s->temp[2], vdst - fake_vstride, vdst, width2, &leftv, &lefttopv);
888 958
889 encode_422_bitstream(s, width); 959 encode_422_bitstream(s, width);
890 } 960 }
891 }else{ 961 }else{
892 for(y=1; y<height; y++){ 962 for(cy=y=1; y<height; y++,cy++){
893 uint8_t *ydst, *udst, *vdst; 963 uint8_t *ydst, *udst, *vdst;
894 964
965 /* encode a luma only line & y++ */
966 if(s->bitstream_bpp==12){
967 ydst= s->picture[0] + s->linesize[0]*y;
968
969 if(s->predictor == PLANE && s->interlaced < y){
970 s->dsp.diff_bytes(s->temp[0], ydst, ydst - fake_ystride, width);
971
972 lefty= sub_left_prediction(s->temp[0], s->temp[0], width , lefty);
973 }else{
974 lefty= sub_left_prediction(s->temp[0], ydst, width , lefty);
975 }
976 encode_gray_bitstream(s, width);
977 y++;
978 if(y>=height) break;
979 }
980
895 ydst= s->picture[0] + s->linesize[0]*y; 981 ydst= s->picture[0] + s->linesize[0]*y;
896 udst= s->picture[1] + s->linesize[1]*y; 982 udst= s->picture[1] + s->linesize[1]*cy;
897 vdst= s->picture[2] + s->linesize[2]*y; 983 vdst= s->picture[2] + s->linesize[2]*cy;
898 984
899 if(s->predictor == PLANE && s->interlaced < y){ 985 if(s->predictor == PLANE && s->interlaced < cy){
900 s->dsp.diff_bytes(s->temp[0], ydst, ydst - fake_ystride, width); 986 s->dsp.diff_bytes(s->temp[0], ydst, ydst - fake_ystride, width);
901 s->dsp.diff_bytes(s->temp[1], udst, udst - fake_ustride, width2); 987 s->dsp.diff_bytes(s->temp[1], udst, udst - fake_ustride, width2);
902 s->dsp.diff_bytes(s->temp[2], vdst, vdst - fake_vstride, width2); 988 s->dsp.diff_bytes(s->temp[2], vdst, vdst - fake_vstride, width2);
903 989
904 lefty= sub_left_prediction(s->temp[0], s->temp[0], width , lefty); 990 lefty= sub_left_prediction(s->temp[0], s->temp[0], width , lefty);
931 } 1017 }
932 sprintf(p, "\n"); 1018 sprintf(p, "\n");
933 p++; 1019 p++;
934 } 1020 }
935 }else{ 1021 }else{
1022 flush_put_bits(&s->pb);
936 bswap_buf((uint32_t*)buf, (uint32_t*)buf, size); 1023 bswap_buf((uint32_t*)buf, (uint32_t*)buf, size);
937 } 1024 }
938 1025
939 avctx->key_frame= 1; 1026 avctx->key_frame= 1;
940 avctx->pict_type= I_TYPE; 1027 avctx->pict_type= I_TYPE;