Revision 140cb663

View differences:

ffmpeg.c
650 650
    av_free(buf1);
651 651
}
652 652

  
653
static double psnr(double d){
654
    if(d==0) return INFINITY;
655
    return -10.0*log(d)/log(10);
656
}
657

  
653 658
static void do_video_stats(AVFormatContext *os, AVOutputStream *ost, 
654 659
                           int frame_size)
655 660
{
......
682 687
    if (enc->codec_type == CODEC_TYPE_VIDEO) {
683 688
        frame_number = ost->frame_number;
684 689
        fprintf(fvstats, "frame= %5d q= %2.1f ", frame_number, enc->coded_picture->quality);
685
        if (do_psnr)
686
            fprintf(fvstats, "PSNR= %6.2f ", enc->psnr_y);
690
        if (enc->flags&CODEC_FLAG_PSNR)
691
            fprintf(fvstats, "PSNR= %6.2f ", psnr(enc->coded_picture->error[0]/(enc->width*enc->height*255.0*255.0)));
687 692
        
688 693
        fprintf(fvstats,"f_size= %6d ", frame_size);
689 694
        /* compute pts value */
......
745 750
            frame_number = ost->frame_number;
746 751
            sprintf(buf + strlen(buf), "frame=%5d q=%2.1f ",
747 752
                    frame_number, enc->coded_picture ? enc->coded_picture->quality : 0);
748
            if (do_psnr)
749
                sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
753
            if (enc->flags&CODEC_FLAG_PSNR)
754
                sprintf(buf + strlen(buf), "PSNR= %6.2f ", psnr(enc->coded_picture->error[0]/(enc->width*enc->height*255.0*255.0)));
750 755
            vid = 1;
751 756
        }
752 757
        /* compute min output value */
......
2161 2166
                }
2162 2167
            
2163 2168
                if (do_psnr)
2164
                    video_enc->get_psnr = 1;
2165
                else
2166
                    video_enc->get_psnr = 0;
2169
                    video_enc->flags|= CODEC_FLAG_PSNR;
2167 2170
            
2168 2171
                video_enc->me_method = me_method;
2169 2172

  
libavcodec/avcodec.h
5 5

  
6 6
#define LIBAVCODEC_VERSION_INT 0x000406
7 7
#define LIBAVCODEC_VERSION     "0.4.6"
8
#define LIBAVCODEC_BUILD       4642
9
#define LIBAVCODEC_BUILD_STR   "4642"
8
#define LIBAVCODEC_BUILD       4643
9
#define LIBAVCODEC_BUILD_STR   "4643"
10 10

  
11 11
enum CodecID {
12 12
    CODEC_ID_NONE, 
......
140 140
#define CODEC_FLAG_EXTERN_HUFF 0x1000 /* use external huffman table (for mjpeg) */
141 141
#define CODEC_FLAG_GRAY  0x2000   /* only decode/encode grayscale */
142 142
#define CODEC_FLAG_EMU_EDGE 0x4000/* dont draw edges */
143
#define CODEC_FLAG_PSNR           0x8000 /* error[?] variables will be set during encoding */
143 144
#define CODEC_FLAG_TRUNCATED  0x00010000 /* input bitstream might be truncated at a random location instead 
144 145
                                            of only at frame boundaries */
145 146
#define CODEC_FLAG_NORMALIZE_AQP  0x00020000 /* normalize adaptive quantization */
......
252 253
     * decoding: set by user\
253 254
     */\
254 255
    void *opaque;\
256
\
257
    /**\
258
     * error\
259
     * encoding: set by lavc if flags&CODEC_FLAG_PSNR\
260
     * decoding: unused\
261
     */\
262
    uint64_t error[4];\
255 263

  
256
/* FIXME: these should have FF_ */
257
#define I_TYPE 1 // Intra
258
#define P_TYPE 2 // Predicted
259
#define B_TYPE 3 // Bi-dir predicted
260
#define S_TYPE 4 // S(GMC)-VOP MPEG4
264
#define FF_I_TYPE 1 // Intra
265
#define FF_P_TYPE 2 // Predicted
266
#define FF_B_TYPE 3 // Bi-dir predicted
267
#define FF_S_TYPE 4 // S(GMC)-VOP MPEG4
261 268

  
262 269
typedef struct AVVideoFrame {
263 270
    FF_COMMON_PICTURE
......
464 471
    /* with a Start Code (it should) H.263 does   */
465 472
    void (*rtp_callback)(void *data, int size, int packet_number); 
466 473

  
467
    /**
468
     * if you set get_psnr to 1 then after encoding you will have the 
469
     * PSNR on psnr_y/cb/cr
470
     * encoding: set by user (1-> on, 0-> off)
471
     * decoding: unused
472
     */
473
    int get_psnr;
474
    float psnr_y;
475
    float psnr_cb;
476
    float psnr_cr;
477
    
478 474
    /* statistics, used for 2-pass encoding */
479 475
    int mv_bits;
480 476
    int header_bits;
......
826 822
#define FF_DEBUG_QP        16
827 823
#define FF_DEBUG_MV        32
828 824
#define FF_DEBUG_VIS_MV    64
825
    
826
    /**
827
     * error
828
     * encoding: set by lavc if flags&CODEC_FLAG_PSNR
829
     * decoding: unused
830
     */
831
    uint64_t error[4];
829 832
} AVCodecContext;
830 833

  
831 834
typedef struct AVCodec {
libavcodec/dsputil.c
1528 1528
//    dsputil_set_bit_exact_mmx();
1529 1529
#endif
1530 1530
}
1531

  
1532
void get_psnr(UINT8 *orig_image[3], UINT8 *coded_image[3],
1533
              int orig_linesize[3], int coded_linesize,
1534
              AVCodecContext *avctx)
1535
{
1536
    int quad, diff, x, y;
1537
    UINT8 *orig, *coded;
1538
    UINT32 *sq = squareTbl + 256;
1539
    
1540
    quad = 0;
1541
    diff = 0;
1542
    
1543
    /* Luminance */
1544
    orig = orig_image[0];
1545
    coded = coded_image[0];
1546
    
1547
    for (y=0;y<avctx->height;y++) {
1548
        for (x=0;x<avctx->width;x++) {
1549
            diff = *(orig + x) - *(coded + x);
1550
            quad += sq[diff];
1551
        }
1552
        orig += orig_linesize[0];
1553
        coded += coded_linesize;
1554
    }
1555
   
1556
    avctx->psnr_y = (float) quad / (float) (avctx->width * avctx->height);
1557
    
1558
    if (avctx->psnr_y) {
1559
        avctx->psnr_y = (float) (255 * 255) / avctx->psnr_y;
1560
        avctx->psnr_y = 10 * (float) log10 (avctx->psnr_y); 
1561
    } else
1562
        avctx->psnr_y = 99.99;
1563
}
1564

  
libavcodec/huffyuv.c
461 461
    s->version=2;
462 462
    
463 463
    avctx->coded_picture= &s->picture;
464
    s->picture.pict_type= I_TYPE;
464
    s->picture.pict_type= FF_I_TYPE;
465 465
    s->picture.key_frame= 1;
466 466
    
467 467
    switch(avctx->pix_fmt){
libavcodec/mpegvideo.c
985 985

  
986 986
    /* set next picture types & ordering */
987 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){
989
            s->reordered_input_picture[0]= s->input_picture[0];
990
            s->reordered_input_picture[0]->pict_type= I_TYPE;
991
            s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
988
        if(s->input_picture[0]->pict_type){
989
            /* user selected pict_type */
990
            if(s->input_picture[0]->pict_type == I_TYPE){
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 1014
        }else{
993
            s->reordered_input_picture[0]= s->input_picture[s->max_b_frames];
994
            if(s->picture_in_gop_number + s->max_b_frames >= s->gop_size)
1015
            if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture.data[0]==NULL || s->intra_only){
1016
                s->reordered_input_picture[0]= s->input_picture[0];
995 1017
                s->reordered_input_picture[0]->pict_type= I_TYPE;
996
            else
997
                s->reordered_input_picture[0]->pict_type= P_TYPE;
998
            s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
999
            for(i=0; i<s->max_b_frames; i++){
1000
                coded_pic_num++;
1001
                s->reordered_input_picture[i+1]= s->input_picture[i];
1002
                s->reordered_input_picture[i+1]->pict_type= B_TYPE;
1003
                s->reordered_input_picture[i+1]->coded_picture_number= coded_pic_num;
1018
                s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
1019
            }else{
1020
                s->reordered_input_picture[0]= s->input_picture[s->max_b_frames];
1021
                if(s->picture_in_gop_number + s->max_b_frames >= s->gop_size)
1022
                    s->reordered_input_picture[0]->pict_type= I_TYPE;
1023
                else
1024
                    s->reordered_input_picture[0]->pict_type= P_TYPE;
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
    }
......
1027 1055
{
1028 1056
    MpegEncContext *s = avctx->priv_data;
1029 1057
    AVVideoFrame *pic_arg = data;
1058
    int i;
1030 1059

  
1031 1060
    init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
1032 1061

  
......
1076 1105
    
1077 1106
    s->total_bits += s->frame_bits;
1078 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", 
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);
1081
#if 0 //dump some stats to stats.txt for testing/debuging
1082
if(s->max_b_frames==0)
1083
{
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 1108

  
1109
    for(i=0; i<4; i++){
1110
        avctx->error[i] += s->current_picture.error[i];
1111
    }
1112
    
1101 1113
    return pbBufPtr(&s->pb) - s->pb.buf;
1102 1114
}
1103 1115

  
......
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 1835
        UINT8 *dest_y, *dest_cb, *dest_cr;
1824 1836
        int dct_linesize, dct_offset;
1825 1837
        op_pixels_func (*op_pix)[4];
......
2544 2556
        copy_context_after_encode(best, s, type);
2545 2557
    }
2546 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;
2574
}
2547 2575

  
2548 2576
static void encode_picture(MpegEncContext *s, int picture_number)
2549 2577
{
......
2723 2751
    s->b_count=0;
2724 2752
    s->skip_count=0;
2725 2753

  
2726
    /* init last dc values */
2727
    /* note: quant matrix value (8) is implied here */
2728
    s->last_dc[0] = 128;
2729
    s->last_dc[1] = 128;
2730
    s->last_dc[2] = 128;
2754
    for(i=0; i<3; i++){
2755
        /* init last dc values */
2756
        /* note: quant matrix value (8) is implied here */
2757
        s->last_dc[i] = 128;
2758
        
2759
        s->current_picture.error[i] = 0;
2760
    }
2731 2761
    s->mb_incr = 1;
2732 2762
    s->last_mv[0][0][0] = 0;
2733 2763
    s->last_mv[0][0][1] = 0;
......
2992 3022
            }
2993 3023

  
2994 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 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

  
libavcodec/mpegvideo.h
48 48
#define MAX_RUN    64
49 49
#define MAX_LEVEL  64
50 50

  
51
#define I_TYPE FF_I_TYPE  // Intra
52
#define P_TYPE FF_P_TYPE  // Predicted
53
#define B_TYPE FF_B_TYPE  // Bi-dir predicted
54
#define S_TYPE FF_S_TYPE  // S(GMC)-VOP MPEG4
55

  
51 56
typedef struct Predictor{
52 57
    double coeff;
53 58
    double count;

Also available in: Unified diff