Mercurial > libavcodec.hg
comparison mpegvideo.c @ 909:8ae1e4c24e91 libavcodec
new PSNR code (now works with chroma, b frames, ...)
rename *_TYPE to FF_*_TYPE for the external API
allow user specified pict_type
author | michaelni |
---|---|
date | Wed, 04 Dec 2002 21:13:02 +0000 |
parents | 22ee74da2cd3 |
children | eb448df811be |
comparison
equal
deleted
inserted
replaced
908:2ac4caad5ca6 | 909:8ae1e4c24e91 |
---|---|
983 s->reordered_input_picture[i-1]= s->reordered_input_picture[i]; | 983 s->reordered_input_picture[i-1]= s->reordered_input_picture[i]; |
984 s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL; | 984 s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL; |
985 | 985 |
986 /* set next picture types & ordering */ | 986 /* set next picture types & ordering */ |
987 if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){ | 987 if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){ |
988 if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture.data[0]==NULL || s->intra_only){ | 988 if(s->input_picture[0]->pict_type){ |
989 s->reordered_input_picture[0]= s->input_picture[0]; | 989 /* user selected pict_type */ |
990 s->reordered_input_picture[0]->pict_type= I_TYPE; | 990 if(s->input_picture[0]->pict_type == I_TYPE){ |
991 s->reordered_input_picture[0]->coded_picture_number= coded_pic_num; | 991 s->reordered_input_picture[0]= s->input_picture[0]; |
992 s->reordered_input_picture[0]->coded_picture_number= coded_pic_num; | |
993 }else{ | |
994 int b_frames; | |
995 | |
996 for(b_frames=0; b_frames<s->max_b_frames+1; b_frames++){ | |
997 if(s->input_picture[b_frames]->pict_type!=B_TYPE) break; | |
998 } | |
999 | |
1000 if(b_frames > s->max_b_frames){ | |
1001 fprintf(stderr, "warning, too many bframes in a row\n"); | |
1002 b_frames = s->max_b_frames; | |
1003 s->input_picture[b_frames]->pict_type= I_TYPE; | |
1004 } | |
1005 | |
1006 s->reordered_input_picture[0]= s->input_picture[b_frames]; | |
1007 s->reordered_input_picture[0]->coded_picture_number= coded_pic_num; | |
1008 for(i=0; i<b_frames; i++){ | |
1009 coded_pic_num++; | |
1010 s->reordered_input_picture[i+1]= s->input_picture[i]; | |
1011 s->reordered_input_picture[i+1]->coded_picture_number= coded_pic_num; | |
1012 } | |
1013 } | |
992 }else{ | 1014 }else{ |
993 s->reordered_input_picture[0]= s->input_picture[s->max_b_frames]; | 1015 if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture.data[0]==NULL || s->intra_only){ |
994 if(s->picture_in_gop_number + s->max_b_frames >= s->gop_size) | 1016 s->reordered_input_picture[0]= s->input_picture[0]; |
995 s->reordered_input_picture[0]->pict_type= I_TYPE; | 1017 s->reordered_input_picture[0]->pict_type= I_TYPE; |
996 else | 1018 s->reordered_input_picture[0]->coded_picture_number= coded_pic_num; |
997 s->reordered_input_picture[0]->pict_type= P_TYPE; | 1019 }else{ |
998 s->reordered_input_picture[0]->coded_picture_number= coded_pic_num; | 1020 s->reordered_input_picture[0]= s->input_picture[s->max_b_frames]; |
999 for(i=0; i<s->max_b_frames; i++){ | 1021 if(s->picture_in_gop_number + s->max_b_frames >= s->gop_size) |
1000 coded_pic_num++; | 1022 s->reordered_input_picture[0]->pict_type= I_TYPE; |
1001 s->reordered_input_picture[i+1]= s->input_picture[i]; | 1023 else |
1002 s->reordered_input_picture[i+1]->pict_type= B_TYPE; | 1024 s->reordered_input_picture[0]->pict_type= P_TYPE; |
1003 s->reordered_input_picture[i+1]->coded_picture_number= coded_pic_num; | 1025 s->reordered_input_picture[0]->coded_picture_number= coded_pic_num; |
1026 for(i=0; i<s->max_b_frames; i++){ | |
1027 coded_pic_num++; | |
1028 s->reordered_input_picture[i+1]= s->input_picture[i]; | |
1029 s->reordered_input_picture[i+1]->pict_type= B_TYPE; | |
1030 s->reordered_input_picture[i+1]->coded_picture_number= coded_pic_num; | |
1031 } | |
1004 } | 1032 } |
1005 } | 1033 } |
1006 } | 1034 } |
1007 | 1035 |
1008 if(s->reordered_input_picture[0]){ | 1036 if(s->reordered_input_picture[0]){ |
1025 int MPV_encode_picture(AVCodecContext *avctx, | 1053 int MPV_encode_picture(AVCodecContext *avctx, |
1026 unsigned char *buf, int buf_size, void *data) | 1054 unsigned char *buf, int buf_size, void *data) |
1027 { | 1055 { |
1028 MpegEncContext *s = avctx->priv_data; | 1056 MpegEncContext *s = avctx->priv_data; |
1029 AVVideoFrame *pic_arg = data; | 1057 AVVideoFrame *pic_arg = data; |
1058 int i; | |
1030 | 1059 |
1031 init_put_bits(&s->pb, buf, buf_size, NULL, NULL); | 1060 init_put_bits(&s->pb, buf, buf_size, NULL, NULL); |
1032 | 1061 |
1033 s->picture_in_gop_number++; | 1062 s->picture_in_gop_number++; |
1034 | 1063 |
1074 flush_put_bits(&s->pb); | 1103 flush_put_bits(&s->pb); |
1075 s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8; | 1104 s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8; |
1076 | 1105 |
1077 s->total_bits += s->frame_bits; | 1106 s->total_bits += s->frame_bits; |
1078 avctx->frame_bits = s->frame_bits; | 1107 avctx->frame_bits = s->frame_bits; |
1079 //printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n", | 1108 |
1080 //s->f_code, avctx->key_frame, s->header_bits, s->mv_bits, s->misc_bits, s->frame_bits, s->i_tex_bits, s->p_tex_bits); | 1109 for(i=0; i<4; i++){ |
1081 #if 0 //dump some stats to stats.txt for testing/debuging | 1110 avctx->error[i] += s->current_picture.error[i]; |
1082 if(s->max_b_frames==0) | 1111 } |
1083 { | 1112 |
1084 static FILE *f=NULL; | |
1085 if(!f) f= fopen("stats.txt", "wb"); | |
1086 get_psnr(pict->data, s->current_picture, | |
1087 pict->linesize, s->linesize, avctx); | |
1088 fprintf(f, "%7d, %7d, %2.4f\n", pbBufPtr(&s->pb) - s->pb.buf, s->qscale, avctx->psnr_y); | |
1089 } | |
1090 #endif | |
1091 #if 0 | |
1092 if (avctx->get_psnr) { | |
1093 /* At this point pict->data should have the original frame */ | |
1094 /* an s->current_picture should have the coded/decoded frame */ | |
1095 get_psnr(pict->data, s->current_picture.data, | |
1096 pict->linesize, s->linesize, avctx); | |
1097 // printf("%f\n", avctx->psnr_y); | |
1098 } | |
1099 #endif | |
1100 | |
1101 return pbBufPtr(&s->pb) - s->pb.buf; | 1113 return pbBufPtr(&s->pb) - s->pb.buf; |
1102 } | 1114 } |
1103 | 1115 |
1104 static inline void gmc1_motion(MpegEncContext *s, | 1116 static inline void gmc1_motion(MpegEncContext *s, |
1105 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | 1117 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, |
1817 s->motion_val[xy + 1 + wrap][0] = motion_x; | 1829 s->motion_val[xy + 1 + wrap][0] = motion_x; |
1818 s->motion_val[xy + 1 + wrap][1] = motion_y; | 1830 s->motion_val[xy + 1 + wrap][1] = motion_y; |
1819 } | 1831 } |
1820 } | 1832 } |
1821 | 1833 |
1822 if (!(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { | 1834 if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc |
1823 UINT8 *dest_y, *dest_cb, *dest_cr; | 1835 UINT8 *dest_y, *dest_cb, *dest_cr; |
1824 int dct_linesize, dct_offset; | 1836 int dct_linesize, dct_offset; |
1825 op_pixels_func (*op_pix)[4]; | 1837 op_pixels_func (*op_pix)[4]; |
1826 qpel_mc_func (*op_qpix)[16]; | 1838 qpel_mc_func (*op_qpix)[16]; |
1827 | 1839 |
2541 *dmin= bits_count; | 2553 *dmin= bits_count; |
2542 *next_block^=1; | 2554 *next_block^=1; |
2543 | 2555 |
2544 copy_context_after_encode(best, s, type); | 2556 copy_context_after_encode(best, s, type); |
2545 } | 2557 } |
2558 } | |
2559 | |
2560 static inline int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){ | |
2561 uint32_t *sq = squareTbl + 256; | |
2562 int acc=0; | |
2563 int x,y; | |
2564 | |
2565 if(w==16 && h==16) | |
2566 return s->dsp.pix_norm(src1, src2, stride); | |
2567 | |
2568 for(y=0; y<h; y++){ | |
2569 for(x=0; x<w; x++){ | |
2570 acc+= sq[src1[x + y*stride] - src2[x + y*stride]]; | |
2571 } | |
2572 } | |
2573 return acc; | |
2546 } | 2574 } |
2547 | 2575 |
2548 static void encode_picture(MpegEncContext *s, int picture_number) | 2576 static void encode_picture(MpegEncContext *s, int picture_number) |
2549 { | 2577 { |
2550 int mb_x, mb_y, pdif = 0; | 2578 int mb_x, mb_y, pdif = 0; |
2721 s->i_count=0; | 2749 s->i_count=0; |
2722 s->f_count=0; | 2750 s->f_count=0; |
2723 s->b_count=0; | 2751 s->b_count=0; |
2724 s->skip_count=0; | 2752 s->skip_count=0; |
2725 | 2753 |
2726 /* init last dc values */ | 2754 for(i=0; i<3; i++){ |
2727 /* note: quant matrix value (8) is implied here */ | 2755 /* init last dc values */ |
2728 s->last_dc[0] = 128; | 2756 /* note: quant matrix value (8) is implied here */ |
2729 s->last_dc[1] = 128; | 2757 s->last_dc[i] = 128; |
2730 s->last_dc[2] = 128; | 2758 |
2759 s->current_picture.error[i] = 0; | |
2760 } | |
2731 s->mb_incr = 1; | 2761 s->mb_incr = 1; |
2732 s->last_mv[0][0][0] = 0; | 2762 s->last_mv[0][0][0] = 0; |
2733 s->last_mv[0][0][1] = 0; | 2763 s->last_mv[0][0][1] = 0; |
2734 | 2764 |
2735 if (s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P) | 2765 if (s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P) |
2990 s->p_mv_table[xy][0]=0; | 3020 s->p_mv_table[xy][0]=0; |
2991 s->p_mv_table[xy][1]=0; | 3021 s->p_mv_table[xy][1]=0; |
2992 } | 3022 } |
2993 | 3023 |
2994 MPV_decode_mb(s, s->block); | 3024 MPV_decode_mb(s, s->block); |
3025 | |
3026 if(s->flags&CODEC_FLAG_PSNR){ | |
3027 int w= 16; | |
3028 int h= 16; | |
3029 | |
3030 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16; | |
3031 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16; | |
3032 | |
3033 s->current_picture.error[0] += sse( | |
3034 s, | |
3035 s->new_picture .data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, | |
3036 s->current_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, | |
3037 w, h, s->linesize); | |
3038 s->current_picture.error[1] += sse( | |
3039 s, | |
3040 s->new_picture .data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8, | |
3041 s->current_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8, | |
3042 w>>1, h>>1, s->uvlinesize); | |
3043 s->current_picture.error[2] += sse( | |
3044 s, | |
3045 s->new_picture .data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8, | |
3046 s->current_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8, | |
3047 w>>1, h>>1, s->uvlinesize); | |
3048 } | |
2995 //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb)); | 3049 //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb)); |
2996 } | 3050 } |
2997 | 3051 |
2998 | 3052 |
2999 /* Obtain average mb_row size for RTP */ | 3053 /* Obtain average mb_row size for RTP */ |