comparison huffyuv.c @ 903:22ee74da2cd3 libavcodec

cleanup adding AVVideoFrame moving quality, pict_type, key_frame, qscale_table, ... to AVVideoFrame removing obsolete variables in AVCodecContext skiping of MBs in b frames correctly initalizing AVCodecContext picture buffer cleanup
author michaelni
date Wed, 04 Dec 2002 10:04:03 +0000
parents efe2d41df15e
children 8ae1e4c24e91
comparison
equal deleted inserted replaced
902:6acc8394960d 903:22ee74da2cd3
28 #ifndef INT64_MAX 28 #ifndef INT64_MAX
29 #define INT64_MAX 9223372036854775807LL 29 #define INT64_MAX 9223372036854775807LL
30 #endif 30 #endif
31 31
32 #define VLC_BITS 11 32 #define VLC_BITS 11
33 33
34 typedef enum Predictor{ 34 typedef enum Predictor{
35 LEFT= 0, 35 LEFT= 0,
36 PLANE, 36 PLANE,
37 MEDIAN, 37 MEDIAN,
38 } Predictor; 38 } Predictor;
50 int bgr32; //use bgr32 instead of bgr24 50 int bgr32; //use bgr32 instead of bgr24
51 int width, height; 51 int width, height;
52 int flags; 52 int flags;
53 int picture_number; 53 int picture_number;
54 int last_slice_end; 54 int last_slice_end;
55 int linesize[3];
56 uint8_t __align8 temp[3][2500]; 55 uint8_t __align8 temp[3][2500];
57 uint64_t stats[3][256]; 56 uint64_t stats[3][256];
58 uint8_t len[3][256]; 57 uint8_t len[3][256];
59 uint32_t bits[3][256]; 58 uint32_t bits[3][256];
60 VLC vlc[3]; 59 VLC vlc[3];
61 uint8_t __align8 *picture[3]; 60 AVVideoFrame picture;
62 uint8_t __align8 bitstream_buffer[1024*1024*3]; //FIXME dynamic alloc or some other solution 61 uint8_t __align8 bitstream_buffer[1024*1024*3]; //FIXME dynamic alloc or some other solution
63 DSPContext dsp; 62 DSPContext dsp;
64 }HYuvContext; 63 }HYuvContext;
65 64
66 static inline void bswap_buf(uint32_t *dst, uint32_t *src, int w){ 65 static inline void bswap_buf(uint32_t *dst, uint32_t *src, int w){
322 } 321 }
323 322
324 static int decode_init(AVCodecContext *avctx) 323 static int decode_init(AVCodecContext *avctx)
325 { 324 {
326 HYuvContext *s = avctx->priv_data; 325 HYuvContext *s = avctx->priv_data;
327 int width, height, y_size, c_size, stride; 326 int width, height;
328 327
329 s->avctx= avctx; 328 s->avctx= avctx;
330 s->flags= avctx->flags; 329 s->flags= avctx->flags;
331 330
332 dsputil_init(&s->dsp, avctx->dsp_mask); 331 dsputil_init(&s->dsp, avctx->dsp_mask);
333 332
334 width= s->width= avctx->width; 333 width= s->width= avctx->width;
335 height= s->height= avctx->height; 334 height= s->height= avctx->height;
335 avctx->coded_picture= &s->picture;
336
336 s->bgr32=1; 337 s->bgr32=1;
337 assert(width && height); 338 assert(width && height);
338 //if(avctx->extradata) 339 //if(avctx->extradata)
339 // printf("extradata:%X, extradata_size:%d\n", *(uint32_t*)avctx->extradata, avctx->extradata_size); 340 // printf("extradata:%X, extradata_size:%d\n", *(uint32_t*)avctx->extradata, avctx->extradata_size);
340 if(avctx->extradata_size){ 341 if(avctx->extradata_size){
386 return -1; 387 return -1;
387 } 388 }
388 389
389 s->interlaced= height > 288; 390 s->interlaced= height > 288;
390 391
391 c_size= 0;
392 switch(s->bitstream_bpp){ 392 switch(s->bitstream_bpp){
393 case 12: 393 case 12:
394 avctx->pix_fmt = PIX_FMT_YUV420P; 394 avctx->pix_fmt = PIX_FMT_YUV420P;
395 stride= (width+15)&~15;
396 c_size= height*stride/4;
397 break; 395 break;
398 case 16: 396 case 16:
399 if(s->yuy2){ 397 if(s->yuy2){
400 avctx->pix_fmt = PIX_FMT_YUV422; 398 avctx->pix_fmt = PIX_FMT_YUV422;
401 stride= (width*2+15)&~15;
402 }else{ 399 }else{
403 avctx->pix_fmt = PIX_FMT_YUV422P; 400 avctx->pix_fmt = PIX_FMT_YUV422P;
404 stride= (width+15)&~15;
405 c_size= height*stride/2;
406 } 401 }
407 break; 402 break;
408 case 24: 403 case 24:
409 case 32: 404 case 32:
410 if(s->bgr32){ 405 if(s->bgr32){
411 avctx->pix_fmt = PIX_FMT_BGRA32; 406 avctx->pix_fmt = PIX_FMT_BGRA32;
412 stride= (width*4+15)&~15;
413 }else{ 407 }else{
414 avctx->pix_fmt = PIX_FMT_BGR24; 408 avctx->pix_fmt = PIX_FMT_BGR24;
415 stride= (width*3+15)&~15;
416 } 409 }
417 break; 410 break;
418 default: 411 default:
419 assert(0); 412 assert(0);
420 stride=0; //gcc fix
421 }
422
423 y_size= height*stride;
424
425 if(!(avctx->flags&CODEC_FLAG_DR1)){
426 s->linesize[0]= stride;
427 s->picture[0]= av_mallocz(y_size);
428
429 if(c_size){
430 s->picture[1]= av_mallocz(c_size);
431 s->picture[2]= av_mallocz(c_size);
432 s->linesize[1]= s->linesize[2]= stride/2;
433
434 memset(s->picture[1], 128, c_size);
435 memset(s->picture[2], 128, c_size);
436 }
437 } 413 }
438 414
439 // printf("pred:%d bpp:%d hbpp:%d il:%d\n", s->predictor, s->bitstream_bpp, avctx->bits_per_sample, s->interlaced); 415 // printf("pred:%d bpp:%d hbpp:%d il:%d\n", s->predictor, s->bitstream_bpp, avctx->bits_per_sample, s->interlaced);
440 416
441 return 0; 417 return 0;
481 assert(width && height); 457 assert(width && height);
482 458
483 avctx->extradata= av_mallocz(1024*10); 459 avctx->extradata= av_mallocz(1024*10);
484 avctx->stats_out= av_mallocz(1024*10); 460 avctx->stats_out= av_mallocz(1024*10);
485 s->version=2; 461 s->version=2;
462
463 avctx->coded_picture= &s->picture;
464 s->picture.pict_type= I_TYPE;
465 s->picture.key_frame= 1;
486 466
487 switch(avctx->pix_fmt){ 467 switch(avctx->pix_fmt){
488 case PIX_FMT_YUV420P: 468 case PIX_FMT_YUV420P:
489 if(avctx->strict_std_compliance>=0){ 469 if(avctx->strict_std_compliance>=0){
490 fprintf(stderr, "YV12-huffyuv is experimental, there WILL be no compatbility! (use (v)strict=-1)\n"); 470 fprintf(stderr, "YV12-huffyuv is experimental, there WILL be no compatbility! (use (v)strict=-1)\n");
672 cy= y>>1; 652 cy= y>>1;
673 }else{ 653 }else{
674 cy= y; 654 cy= y;
675 } 655 }
676 656
677 src_ptr[0] = s->picture[0] + s->linesize[0]*y; 657 src_ptr[0] = s->picture.data[0] + s->picture.linesize[0]*y;
678 src_ptr[1] = s->picture[1] + s->linesize[1]*cy; 658 src_ptr[1] = s->picture.data[1] + s->picture.linesize[1]*cy;
679 src_ptr[2] = s->picture[2] + s->linesize[2]*cy; 659 src_ptr[2] = s->picture.data[2] + s->picture.linesize[2]*cy;
680 emms_c(); 660 emms_c();
681 661
682 s->avctx->draw_horiz_band(s->avctx, src_ptr, s->linesize[0], y, s->width, h); 662 s->avctx->draw_horiz_band(s->avctx, src_ptr, s->picture.linesize[0], y, s->width, h);
683 663
684 s->last_slice_end= y + h; 664 s->last_slice_end= y + h;
685 } 665 }
686 666
687 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){ 667 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){
688 HYuvContext *s = avctx->priv_data; 668 HYuvContext *s = avctx->priv_data;
689 const int width= s->width; 669 const int width= s->width;
690 const int width2= s->width>>1; 670 const int width2= s->width>>1;
691 const int height= s->height; 671 const int height= s->height;
692 int fake_ystride, fake_ustride, fake_vstride; 672 int fake_ystride, fake_ustride, fake_vstride;
693 int i; 673 AVVideoFrame * const p= &s->picture;
694 674
695 AVPicture *picture = data; 675 AVVideoFrame *picture = data;
696 676
697 *data_size = 0; 677 *data_size = 0;
698 678
699 /* no supplementary picture */ 679 /* no supplementary picture */
700 if (buf_size == 0) 680 if (buf_size == 0)
702 682
703 bswap_buf((uint32_t*)s->bitstream_buffer, (uint32_t*)buf, buf_size/4); 683 bswap_buf((uint32_t*)s->bitstream_buffer, (uint32_t*)buf, buf_size/4);
704 684
705 init_get_bits(&s->gb, s->bitstream_buffer, buf_size); 685 init_get_bits(&s->gb, s->bitstream_buffer, buf_size);
706 686
707 if(avctx->flags&CODEC_FLAG_DR1){ 687 p->reference= 0;
708 if(avctx->get_buffer_callback(avctx, s->width, s->height, I_TYPE) < 0){ 688 if(avctx->get_buffer(avctx, p) < 0){
709 fprintf(stderr, "get_buffer() failed\n"); 689 fprintf(stderr, "get_buffer() failed\n");
710 return -1; 690 return -1;
711 } 691 }
712 692
713 s->linesize[0]= avctx->dr_stride; 693 fake_ystride= s->interlaced ? p->linesize[0]*2 : p->linesize[0];
714 s->linesize[1]= 694 fake_ustride= s->interlaced ? p->linesize[1]*2 : p->linesize[1];
715 s->linesize[2]= avctx->dr_uvstride; 695 fake_vstride= s->interlaced ? p->linesize[2]*2 : p->linesize[2];
716
717 for(i=0; i<3;i++)
718 s->picture[i]= avctx->dr_buffer[i];
719 }
720 fake_ystride= s->interlaced ? s->linesize[0]*2 : s->linesize[0];
721 fake_ustride= s->interlaced ? s->linesize[1]*2 : s->linesize[1];
722 fake_vstride= s->interlaced ? s->linesize[2]*2 : s->linesize[2];
723 696
724 s->last_slice_end= 0; 697 s->last_slice_end= 0;
725 698
726 if(s->bitstream_bpp<24){ 699 if(s->bitstream_bpp<24){
727 int y, cy; 700 int y, cy;
728 int lefty, leftu, leftv; 701 int lefty, leftu, leftv;
729 int lefttopy, lefttopu, lefttopv; 702 int lefttopy, lefttopu, lefttopv;
730 703
731 if(s->yuy2){ 704 if(s->yuy2){
732 s->picture[0][3]= get_bits(&s->gb, 8); 705 p->data[0][3]= get_bits(&s->gb, 8);
733 s->picture[0][2]= get_bits(&s->gb, 8); 706 p->data[0][2]= get_bits(&s->gb, 8);
734 s->picture[0][1]= get_bits(&s->gb, 8); 707 p->data[0][1]= get_bits(&s->gb, 8);
735 s->picture[0][0]= get_bits(&s->gb, 8); 708 p->data[0][0]= get_bits(&s->gb, 8);
736 709
737 fprintf(stderr, "YUY2 output isnt implemenetd yet\n"); 710 fprintf(stderr, "YUY2 output isnt implemenetd yet\n");
738 return -1; 711 return -1;
739 }else{ 712 }else{
740 713
741 leftv= s->picture[2][0]= get_bits(&s->gb, 8); 714 leftv= p->data[2][0]= get_bits(&s->gb, 8);
742 lefty= s->picture[0][1]= get_bits(&s->gb, 8); 715 lefty= p->data[0][1]= get_bits(&s->gb, 8);
743 leftu= s->picture[1][0]= get_bits(&s->gb, 8); 716 leftu= p->data[1][0]= get_bits(&s->gb, 8);
744 s->picture[0][0]= get_bits(&s->gb, 8); 717 p->data[0][0]= get_bits(&s->gb, 8);
745 718
746 switch(s->predictor){ 719 switch(s->predictor){
747 case LEFT: 720 case LEFT:
748 case PLANE: 721 case PLANE:
749 decode_422_bitstream(s, width-2); 722 decode_422_bitstream(s, width-2);
750 lefty= add_left_prediction(s->picture[0] + 2, s->temp[0], width-2, lefty); 723 lefty= add_left_prediction(p->data[0] + 2, s->temp[0], width-2, lefty);
751 if(!(s->flags&CODEC_FLAG_GRAY)){ 724 if(!(s->flags&CODEC_FLAG_GRAY)){
752 leftu= add_left_prediction(s->picture[1] + 1, s->temp[1], width2-1, leftu); 725 leftu= add_left_prediction(p->data[1] + 1, s->temp[1], width2-1, leftu);
753 leftv= add_left_prediction(s->picture[2] + 1, s->temp[2], width2-1, leftv); 726 leftv= add_left_prediction(p->data[2] + 1, s->temp[2], width2-1, leftv);
754 } 727 }
755 728
756 for(cy=y=1; y<s->height; y++,cy++){ 729 for(cy=y=1; y<s->height; y++,cy++){
757 uint8_t *ydst, *udst, *vdst; 730 uint8_t *ydst, *udst, *vdst;
758 731
759 if(s->bitstream_bpp==12){ 732 if(s->bitstream_bpp==12){
760 decode_gray_bitstream(s, width); 733 decode_gray_bitstream(s, width);
761 734
762 ydst= s->picture[0] + s->linesize[0]*y; 735 ydst= p->data[0] + p->linesize[0]*y;
763 736
764 lefty= add_left_prediction(ydst, s->temp[0], width, lefty); 737 lefty= add_left_prediction(ydst, s->temp[0], width, lefty);
765 if(s->predictor == PLANE){ 738 if(s->predictor == PLANE){
766 if(y>s->interlaced) 739 if(y>s->interlaced)
767 s->dsp.add_bytes(ydst, ydst - fake_ystride, width); 740 s->dsp.add_bytes(ydst, ydst - fake_ystride, width);
770 if(y>=s->height) break; 743 if(y>=s->height) break;
771 } 744 }
772 745
773 draw_slice(s, y); 746 draw_slice(s, y);
774 747
775 ydst= s->picture[0] + s->linesize[0]*y; 748 ydst= p->data[0] + p->linesize[0]*y;
776 udst= s->picture[1] + s->linesize[1]*cy; 749 udst= p->data[1] + p->linesize[1]*cy;
777 vdst= s->picture[2] + s->linesize[2]*cy; 750 vdst= p->data[2] + p->linesize[2]*cy;
778 751
779 decode_422_bitstream(s, width); 752 decode_422_bitstream(s, width);
780
781 lefty= add_left_prediction(ydst, s->temp[0], width, lefty); 753 lefty= add_left_prediction(ydst, s->temp[0], width, lefty);
782 if(!(s->flags&CODEC_FLAG_GRAY)){ 754 if(!(s->flags&CODEC_FLAG_GRAY)){
783 leftu= add_left_prediction(udst, s->temp[1], width2, leftu); 755 leftu= add_left_prediction(udst, s->temp[1], width2, leftu);
784 leftv= add_left_prediction(vdst, s->temp[2], width2, leftv); 756 leftv= add_left_prediction(vdst, s->temp[2], width2, leftv);
785 } 757 }
797 769
798 break; 770 break;
799 case MEDIAN: 771 case MEDIAN:
800 /* first line except first 2 pixels is left predicted */ 772 /* first line except first 2 pixels is left predicted */
801 decode_422_bitstream(s, width-2); 773 decode_422_bitstream(s, width-2);
802 lefty= add_left_prediction(s->picture[0] + 2, s->temp[0], width-2, lefty); 774 lefty= add_left_prediction(p->data[0] + 2, s->temp[0], width-2, lefty);
803 if(!(s->flags&CODEC_FLAG_GRAY)){ 775 if(!(s->flags&CODEC_FLAG_GRAY)){
804 leftu= add_left_prediction(s->picture[1] + 1, s->temp[1], width2-1, leftu); 776 leftu= add_left_prediction(p->data[1] + 1, s->temp[1], width2-1, leftu);
805 leftv= add_left_prediction(s->picture[2] + 1, s->temp[2], width2-1, leftv); 777 leftv= add_left_prediction(p->data[2] + 1, s->temp[2], width2-1, leftv);
806 } 778 }
807 779
808 cy=y=1; 780 cy=y=1;
809 781
810 /* second line is left predicted for interlaced case */ 782 /* second line is left predicted for interlaced case */
811 if(s->interlaced){ 783 if(s->interlaced){
812 decode_422_bitstream(s, width); 784 decode_422_bitstream(s, width);
813 lefty= add_left_prediction(s->picture[0] + s->linesize[0], s->temp[0], width, lefty); 785 lefty= add_left_prediction(p->data[0] + p->linesize[0], s->temp[0], width, lefty);
814 if(!(s->flags&CODEC_FLAG_GRAY)){ 786 if(!(s->flags&CODEC_FLAG_GRAY)){
815 leftu= add_left_prediction(s->picture[1] + s->linesize[2], s->temp[1], width2, leftu); 787 leftu= add_left_prediction(p->data[1] + p->linesize[2], s->temp[1], width2, leftu);
816 leftv= add_left_prediction(s->picture[2] + s->linesize[1], s->temp[2], width2, leftv); 788 leftv= add_left_prediction(p->data[2] + p->linesize[1], s->temp[2], width2, leftv);
817 } 789 }
818 y++; cy++; 790 y++; cy++;
819 } 791 }
820 792
821 /* next 4 pixels are left predicted too */ 793 /* next 4 pixels are left predicted too */
822 decode_422_bitstream(s, 4); 794 decode_422_bitstream(s, 4);
823 lefty= add_left_prediction(s->picture[0] + fake_ystride, s->temp[0], 4, lefty); 795 lefty= add_left_prediction(p->data[0] + fake_ystride, s->temp[0], 4, lefty);
824 if(!(s->flags&CODEC_FLAG_GRAY)){ 796 if(!(s->flags&CODEC_FLAG_GRAY)){
825 leftu= add_left_prediction(s->picture[1] + fake_ustride, s->temp[1], 2, leftu); 797 leftu= add_left_prediction(p->data[1] + fake_ustride, s->temp[1], 2, leftu);
826 leftv= add_left_prediction(s->picture[2] + fake_vstride, s->temp[2], 2, leftv); 798 leftv= add_left_prediction(p->data[2] + fake_vstride, s->temp[2], 2, leftv);
827 } 799 }
828 800
829 /* next line except the first 4 pixels is median predicted */ 801 /* next line except the first 4 pixels is median predicted */
830 lefttopy= s->picture[0][3]; 802 lefttopy= p->data[0][3];
831 decode_422_bitstream(s, width-4); 803 decode_422_bitstream(s, width-4);
832 add_median_prediction(s->picture[0] + fake_ystride+4, s->picture[0]+4, s->temp[0], width-4, &lefty, &lefttopy); 804 add_median_prediction(p->data[0] + fake_ystride+4, p->data[0]+4, s->temp[0], width-4, &lefty, &lefttopy);
833 if(!(s->flags&CODEC_FLAG_GRAY)){ 805 if(!(s->flags&CODEC_FLAG_GRAY)){
834 lefttopu= s->picture[1][1]; 806 lefttopu= p->data[1][1];
835 lefttopv= s->picture[2][1]; 807 lefttopv= p->data[2][1];
836 add_median_prediction(s->picture[1] + fake_ustride+2, s->picture[1]+2, s->temp[1], width2-2, &leftu, &lefttopu); 808 add_median_prediction(p->data[1] + fake_ustride+2, p->data[1]+2, s->temp[1], width2-2, &leftu, &lefttopu);
837 add_median_prediction(s->picture[2] + fake_vstride+2, s->picture[2]+2, s->temp[2], width2-2, &leftv, &lefttopv); 809 add_median_prediction(p->data[2] + fake_vstride+2, p->data[2]+2, s->temp[2], width2-2, &leftv, &lefttopv);
838 } 810 }
839 y++; cy++; 811 y++; cy++;
840 812
841 for(; y<height; y++,cy++){ 813 for(; y<height; y++,cy++){
842 uint8_t *ydst, *udst, *vdst; 814 uint8_t *ydst, *udst, *vdst;
843 815
844 if(s->bitstream_bpp==12){ 816 if(s->bitstream_bpp==12){
845 while(2*cy > y){ 817 while(2*cy > y){
846 decode_gray_bitstream(s, width); 818 decode_gray_bitstream(s, width);
847 ydst= s->picture[0] + s->linesize[0]*y; 819 ydst= p->data[0] + p->linesize[0]*y;
848 add_median_prediction(ydst, ydst - fake_ystride, s->temp[0], width, &lefty, &lefttopy); 820 add_median_prediction(ydst, ydst - fake_ystride, s->temp[0], width, &lefty, &lefttopy);
849 y++; 821 y++;
850 } 822 }
851 if(y>=height) break; 823 if(y>=height) break;
852 } 824 }
853 draw_slice(s, y); 825 draw_slice(s, y);
854 826
855 decode_422_bitstream(s, width); 827 decode_422_bitstream(s, width);
856 828
857 ydst= s->picture[0] + s->linesize[0]*y; 829 ydst= p->data[0] + p->linesize[0]*y;
858 udst= s->picture[1] + s->linesize[1]*cy; 830 udst= p->data[1] + p->linesize[1]*cy;
859 vdst= s->picture[2] + s->linesize[2]*cy; 831 vdst= p->data[2] + p->linesize[2]*cy;
860 832
861 add_median_prediction(ydst, ydst - fake_ystride, s->temp[0], width, &lefty, &lefttopy); 833 add_median_prediction(ydst, ydst - fake_ystride, s->temp[0], width, &lefty, &lefttopy);
862 if(!(s->flags&CODEC_FLAG_GRAY)){ 834 if(!(s->flags&CODEC_FLAG_GRAY)){
863 add_median_prediction(udst, udst - fake_ustride, s->temp[1], width2, &leftu, &lefttopu); 835 add_median_prediction(udst, udst - fake_ustride, s->temp[1], width2, &leftu, &lefttopu);
864 add_median_prediction(vdst, vdst - fake_vstride, s->temp[2], width2, &leftv, &lefttopv); 836 add_median_prediction(vdst, vdst - fake_vstride, s->temp[2], width2, &leftv, &lefttopv);
870 } 842 }
871 } 843 }
872 }else{ 844 }else{
873 int y; 845 int y;
874 int leftr, leftg, leftb; 846 int leftr, leftg, leftb;
875 const int last_line= (height-1)*s->linesize[0]; 847 const int last_line= (height-1)*p->linesize[0];
876 848
877 if(s->bitstream_bpp==32){ 849 if(s->bitstream_bpp==32){
878 s->picture[0][last_line+3]= get_bits(&s->gb, 8); 850 p->data[0][last_line+3]= get_bits(&s->gb, 8);
879 leftr= s->picture[0][last_line+2]= get_bits(&s->gb, 8); 851 leftr= p->data[0][last_line+2]= get_bits(&s->gb, 8);
880 leftg= s->picture[0][last_line+1]= get_bits(&s->gb, 8); 852 leftg= p->data[0][last_line+1]= get_bits(&s->gb, 8);
881 leftb= s->picture[0][last_line+0]= get_bits(&s->gb, 8); 853 leftb= p->data[0][last_line+0]= get_bits(&s->gb, 8);
882 }else{ 854 }else{
883 leftr= s->picture[0][last_line+2]= get_bits(&s->gb, 8); 855 leftr= p->data[0][last_line+2]= get_bits(&s->gb, 8);
884 leftg= s->picture[0][last_line+1]= get_bits(&s->gb, 8); 856 leftg= p->data[0][last_line+1]= get_bits(&s->gb, 8);
885 leftb= s->picture[0][last_line+0]= get_bits(&s->gb, 8); 857 leftb= p->data[0][last_line+0]= get_bits(&s->gb, 8);
886 skip_bits(&s->gb, 8); 858 skip_bits(&s->gb, 8);
887 } 859 }
888 860
889 if(s->bgr32){ 861 if(s->bgr32){
890 switch(s->predictor){ 862 switch(s->predictor){
891 case LEFT: 863 case LEFT:
892 case PLANE: 864 case PLANE:
893 decode_bgr_bitstream(s, width-1); 865 decode_bgr_bitstream(s, width-1);
894 add_left_prediction_bgr32(s->picture[0] + last_line+4, s->temp[0], width-1, &leftr, &leftg, &leftb); 866 add_left_prediction_bgr32(p->data[0] + last_line+4, s->temp[0], width-1, &leftr, &leftg, &leftb);
895 867
896 for(y=s->height-2; y>=0; y--){ //yes its stored upside down 868 for(y=s->height-2; y>=0; y--){ //yes its stored upside down
897 decode_bgr_bitstream(s, width); 869 decode_bgr_bitstream(s, width);
898 870
899 add_left_prediction_bgr32(s->picture[0] + s->linesize[0]*y, s->temp[0], width, &leftr, &leftg, &leftb); 871 add_left_prediction_bgr32(p->data[0] + p->linesize[0]*y, s->temp[0], width, &leftr, &leftg, &leftb);
900 if(s->predictor == PLANE){ 872 if(s->predictor == PLANE){
901 if((y&s->interlaced)==0){ 873 if((y&s->interlaced)==0){
902 s->dsp.add_bytes(s->picture[0] + s->linesize[0]*y, 874 s->dsp.add_bytes(p->data[0] + p->linesize[0]*y,
903 s->picture[0] + s->linesize[0]*y + fake_ystride, fake_ystride); 875 p->data[0] + p->linesize[0]*y + fake_ystride, fake_ystride);
904 } 876 }
905 } 877 }
906 } 878 }
907 draw_slice(s, height); // just 1 large slice as this isnt possible in reverse order 879 draw_slice(s, height); // just 1 large slice as this isnt possible in reverse order
908 break; 880 break;
915 return -1; 887 return -1;
916 } 888 }
917 } 889 }
918 emms_c(); 890 emms_c();
919 891
920 for(i=0;i<3;i++) { 892 *picture= *p;
921 picture->data[i] = s->picture[i]; 893
922 picture->linesize[i]= s->linesize[i]; 894 avctx->release_buffer(avctx, p);
923 } 895
924 896 *data_size = sizeof(AVVideoFrame);
925 *data_size = sizeof(AVPicture);
926 897
927 return (get_bits_count(&s->gb)+7)>>3; 898 return (get_bits_count(&s->gb)+7)>>3;
928 } 899 }
929 900
930 static int decode_end(AVCodecContext *avctx) 901 static int decode_end(AVCodecContext *avctx)
931 { 902 {
932 HYuvContext *s = avctx->priv_data; 903 HYuvContext *s = avctx->priv_data;
933 int i; 904 int i;
934 905
935 for(i=0; i<3; i++){ 906 for(i=0; i<3; i++){
936 if(!(avctx->flags&CODEC_FLAG_DR1))
937 av_freep(&s->picture[i]);
938
939 free_vlc(&s->vlc[i]); 907 free_vlc(&s->vlc[i]);
908 }
909
910 if(avctx->get_buffer == avcodec_default_get_buffer){
911 for(i=0; i<4; i++){
912 av_freep(&s->picture.base[i]);
913 s->picture.data[i]= NULL;
914 }
915 av_freep(&s->picture.opaque);
940 } 916 }
941 917
942 return 0; 918 return 0;
943 } 919 }
944 920
945 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){ 921 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
946 HYuvContext *s = avctx->priv_data; 922 HYuvContext *s = avctx->priv_data;
947 AVPicture *pict = data; 923 AVVideoFrame *pict = data;
948 const int width= s->width; 924 const int width= s->width;
949 const int width2= s->width>>1; 925 const int width2= s->width>>1;
950 const int height= s->height; 926 const int height= s->height;
951 const int fake_ystride= s->interlaced ? pict->linesize[0]*2 : pict->linesize[0]; 927 const int fake_ystride= s->interlaced ? pict->linesize[0]*2 : pict->linesize[0];
952 const int fake_ustride= s->interlaced ? pict->linesize[1]*2 : pict->linesize[1]; 928 const int fake_ustride= s->interlaced ? pict->linesize[1]*2 : pict->linesize[1];
953 const int fake_vstride= s->interlaced ? pict->linesize[2]*2 : pict->linesize[2]; 929 const int fake_vstride= s->interlaced ? pict->linesize[2]*2 : pict->linesize[2];
930 AVVideoFrame * const p= &s->picture;
954 int i, size; 931 int i, size;
955 932
956 init_put_bits(&s->pb, buf, buf_size, NULL, NULL); 933 init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
957 934
958 for(i=0; i<3; i++){ 935 *p = *pict;
959 s->picture[i]= pict->data[i];
960 s->linesize[i]= pict->linesize[i];
961 }
962 936
963 if(avctx->pix_fmt == PIX_FMT_YUV422P || avctx->pix_fmt == PIX_FMT_YUV420P){ 937 if(avctx->pix_fmt == PIX_FMT_YUV422P || avctx->pix_fmt == PIX_FMT_YUV420P){
964 int lefty, leftu, leftv, y, cy; 938 int lefty, leftu, leftv, y, cy;
965 939
966 put_bits(&s->pb, 8, leftv= s->picture[2][0]); 940 put_bits(&s->pb, 8, leftv= p->data[2][0]);
967 put_bits(&s->pb, 8, lefty= s->picture[0][1]); 941 put_bits(&s->pb, 8, lefty= p->data[0][1]);
968 put_bits(&s->pb, 8, leftu= s->picture[1][0]); 942 put_bits(&s->pb, 8, leftu= p->data[1][0]);
969 put_bits(&s->pb, 8, s->picture[0][0]); 943 put_bits(&s->pb, 8, p->data[0][0]);
970 944
971 lefty= sub_left_prediction(s, s->temp[0], s->picture[0]+2, width-2 , lefty); 945 lefty= sub_left_prediction(s, s->temp[0], p->data[0]+2, width-2 , lefty);
972 leftu= sub_left_prediction(s, s->temp[1], s->picture[1]+1, width2-1, leftu); 946 leftu= sub_left_prediction(s, s->temp[1], p->data[1]+1, width2-1, leftu);
973 leftv= sub_left_prediction(s, s->temp[2], s->picture[2]+1, width2-1, leftv); 947 leftv= sub_left_prediction(s, s->temp[2], p->data[2]+1, width2-1, leftv);
974 948
975 encode_422_bitstream(s, width-2); 949 encode_422_bitstream(s, width-2);
976 950
977 if(s->predictor==MEDIAN){ 951 if(s->predictor==MEDIAN){
978 int lefttopy, lefttopu, lefttopv; 952 int lefttopy, lefttopu, lefttopv;
979 cy=y=1; 953 cy=y=1;
980 if(s->interlaced){ 954 if(s->interlaced){
981 lefty= sub_left_prediction(s, s->temp[0], s->picture[0]+s->linesize[0], width , lefty); 955 lefty= sub_left_prediction(s, s->temp[0], p->data[0]+p->linesize[0], width , lefty);
982 leftu= sub_left_prediction(s, s->temp[1], s->picture[1]+s->linesize[1], width2, leftu); 956 leftu= sub_left_prediction(s, s->temp[1], p->data[1]+p->linesize[1], width2, leftu);
983 leftv= sub_left_prediction(s, s->temp[2], s->picture[2]+s->linesize[2], width2, leftv); 957 leftv= sub_left_prediction(s, s->temp[2], p->data[2]+p->linesize[2], width2, leftv);
984 958
985 encode_422_bitstream(s, width); 959 encode_422_bitstream(s, width);
986 y++; cy++; 960 y++; cy++;
987 } 961 }
988 962
989 lefty= sub_left_prediction(s, s->temp[0], s->picture[0]+fake_ystride, 4, lefty); 963 lefty= sub_left_prediction(s, s->temp[0], p->data[0]+fake_ystride, 4, lefty);
990 leftu= sub_left_prediction(s, s->temp[1], s->picture[1]+fake_ystride, 2, leftu); 964 leftu= sub_left_prediction(s, s->temp[1], p->data[1]+fake_ystride, 2, leftu);
991 leftv= sub_left_prediction(s, s->temp[2], s->picture[2]+fake_ystride, 2, leftv); 965 leftv= sub_left_prediction(s, s->temp[2], p->data[2]+fake_ystride, 2, leftv);
992 966
993 encode_422_bitstream(s, 4); 967 encode_422_bitstream(s, 4);
994 968
995 lefttopy= s->picture[0][3]; 969 lefttopy= p->data[0][3];
996 lefttopu= s->picture[1][1]; 970 lefttopu= p->data[1][1];
997 lefttopv= s->picture[2][1]; 971 lefttopv= p->data[2][1];
998 sub_median_prediction(s->temp[0], s->picture[0]+4, s->picture[0] + fake_ystride+4, width-4 , &lefty, &lefttopy); 972 sub_median_prediction(s->temp[0], p->data[0]+4, p->data[0] + fake_ystride+4, width-4 , &lefty, &lefttopy);
999 sub_median_prediction(s->temp[1], s->picture[1]+2, s->picture[1] + fake_ustride+2, width2-2, &leftu, &lefttopu); 973 sub_median_prediction(s->temp[1], p->data[1]+2, p->data[1] + fake_ustride+2, width2-2, &leftu, &lefttopu);
1000 sub_median_prediction(s->temp[2], s->picture[2]+2, s->picture[2] + fake_vstride+2, width2-2, &leftv, &lefttopv); 974 sub_median_prediction(s->temp[2], p->data[2]+2, p->data[2] + fake_vstride+2, width2-2, &leftv, &lefttopv);
1001 encode_422_bitstream(s, width-4); 975 encode_422_bitstream(s, width-4);
1002 y++; cy++; 976 y++; cy++;
1003 977
1004 for(; y<height; y++,cy++){ 978 for(; y<height; y++,cy++){
1005 uint8_t *ydst, *udst, *vdst; 979 uint8_t *ydst, *udst, *vdst;
1006 980
1007 if(s->bitstream_bpp==12){ 981 if(s->bitstream_bpp==12){
1008 while(2*cy > y){ 982 while(2*cy > y){
1009 ydst= s->picture[0] + s->linesize[0]*y; 983 ydst= p->data[0] + p->linesize[0]*y;
1010 sub_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy); 984 sub_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy);
1011 encode_gray_bitstream(s, width); 985 encode_gray_bitstream(s, width);
1012 y++; 986 y++;
1013 } 987 }
1014 if(y>=height) break; 988 if(y>=height) break;
1015 } 989 }
1016 ydst= s->picture[0] + s->linesize[0]*y; 990 ydst= p->data[0] + p->linesize[0]*y;
1017 udst= s->picture[1] + s->linesize[1]*cy; 991 udst= p->data[1] + p->linesize[1]*cy;
1018 vdst= s->picture[2] + s->linesize[2]*cy; 992 vdst= p->data[2] + p->linesize[2]*cy;
1019 993
1020 sub_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy); 994 sub_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy);
1021 sub_median_prediction(s->temp[1], udst - fake_ustride, udst, width2, &leftu, &lefttopu); 995 sub_median_prediction(s->temp[1], udst - fake_ustride, udst, width2, &leftu, &lefttopu);
1022 sub_median_prediction(s->temp[2], vdst - fake_vstride, vdst, width2, &leftv, &lefttopv); 996 sub_median_prediction(s->temp[2], vdst - fake_vstride, vdst, width2, &leftv, &lefttopv);
1023 997
1027 for(cy=y=1; y<height; y++,cy++){ 1001 for(cy=y=1; y<height; y++,cy++){
1028 uint8_t *ydst, *udst, *vdst; 1002 uint8_t *ydst, *udst, *vdst;
1029 1003
1030 /* encode a luma only line & y++ */ 1004 /* encode a luma only line & y++ */
1031 if(s->bitstream_bpp==12){ 1005 if(s->bitstream_bpp==12){
1032 ydst= s->picture[0] + s->linesize[0]*y; 1006 ydst= p->data[0] + p->linesize[0]*y;
1033 1007
1034 if(s->predictor == PLANE && s->interlaced < y){ 1008 if(s->predictor == PLANE && s->interlaced < y){
1035 s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width); 1009 s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
1036 1010
1037 lefty= sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty); 1011 lefty= sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
1041 encode_gray_bitstream(s, width); 1015 encode_gray_bitstream(s, width);
1042 y++; 1016 y++;
1043 if(y>=height) break; 1017 if(y>=height) break;
1044 } 1018 }
1045 1019
1046 ydst= s->picture[0] + s->linesize[0]*y; 1020 ydst= p->data[0] + p->linesize[0]*y;
1047 udst= s->picture[1] + s->linesize[1]*cy; 1021 udst= p->data[1] + p->linesize[1]*cy;
1048 vdst= s->picture[2] + s->linesize[2]*cy; 1022 vdst= p->data[2] + p->linesize[2]*cy;
1049 1023
1050 if(s->predictor == PLANE && s->interlaced < cy){ 1024 if(s->predictor == PLANE && s->interlaced < cy){
1051 s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width); 1025 s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
1052 s->dsp.diff_bytes(s->temp[2], udst, udst - fake_ustride, width2); 1026 s->dsp.diff_bytes(s->temp[2], udst, udst - fake_ustride, width2);
1053 s->dsp.diff_bytes(s->temp[3], vdst, vdst - fake_vstride, width2); 1027 s->dsp.diff_bytes(s->temp[3], vdst, vdst - fake_vstride, width2);
1086 }else{ 1060 }else{
1087 flush_put_bits(&s->pb); 1061 flush_put_bits(&s->pb);
1088 bswap_buf((uint32_t*)buf, (uint32_t*)buf, size); 1062 bswap_buf((uint32_t*)buf, (uint32_t*)buf, size);
1089 } 1063 }
1090 1064
1091 avctx->key_frame= 1;
1092 avctx->pict_type= I_TYPE;
1093
1094 s->picture_number++; 1065 s->picture_number++;
1095 1066
1096 return size*4; 1067 return size*4;
1097 } 1068 }
1098 1069
1099 static int encode_end(AVCodecContext *avctx) 1070 static int encode_end(AVCodecContext *avctx)
1100 { 1071 {