⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 analyse.c.svn-base

📁 H.264 source codes
💻 SVN-BASE
📖 第 1 页 / 共 5 页
字号:
/***************************************************************************** * analyse.c: h264 encoder library ***************************************************************************** * Copyright (C) 2003 Laurent Aimar * $Id: analyse.c,v 1.1 2004/06/03 19:27:08 fenrir Exp $ * * Authors: Laurent Aimar <fenrir@via.ecp.fr> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA. *****************************************************************************/#include <stdlib.h>#include <stdio.h>#include <string.h>#include <math.h>#include <limits.h>#include "common/common.h"#include "common/macroblock.h"#include "macroblock.h"#include "me.h"#include "ratecontrol.h"typedef struct{    /* 16x16 */    int i_ref;    x264_me_t me16x16;    /* 8x8 */    int       i_cost8x8;    x264_me_t me8x8[4];    /* Sub 4x4 */    int       i_cost4x4[4]; /* cost per 8x8 partition */    x264_me_t me4x4[4][4];    /* Sub 8x4 */    int       i_cost8x4[4]; /* cost per 8x8 partition */    x264_me_t me8x4[4][2];    /* Sub 4x8 */    int       i_cost4x8[4]; /* cost per 8x8 partition */    x264_me_t me4x8[4][4];    /* 16x8 */    int       i_cost16x8;    x264_me_t me16x8[2];    /* 8x16 */    int       i_cost8x16;    x264_me_t me8x16[2];} x264_mb_analysis_list_t;typedef struct{    /* conduct the analysis using this lamda and QP */    int i_lambda;    int i_qp;    int16_t *p_cost_mv;    /* I: Intra part */    /* Take some shortcuts in intra search if intra is deemed unlikely */    int b_fast_intra;    /* Luma part 16x16 and 4x4 modes stats */    int i_sad_i16x16;    int i_predict16x16;    int i_sad_i4x4;    int i_predict4x4[4][4];    /* Chroma part */    int i_sad_i8x8;    int i_predict8x8;    /* II: Inter part P/B frame */    x264_mb_analysis_list_t l0;    x264_mb_analysis_list_t l1;    int i_cost16x16bi; /* used the same ref and mv as l0 and l1 (at least for now) */    int i_cost16x16direct;    int i_cost8x8bi;    int i_cost8x8direct[4];    int i_cost16x8bi;    int i_cost8x16bi;    int i_mb_partition16x8[2]; /* mb_partition_e */    int i_mb_partition8x16[2];    int i_mb_type16x8; /* mb_class_e */    int i_mb_type8x16;    int b_direct_available;} x264_mb_analysis_t;static const int i_qp0_cost_table[52] = {   1, 1, 1, 1, 1, 1, 1, 1,  /*  0-7 */   1, 1, 1, 1,              /*  8-11 */   1, 1, 1, 1, 2, 2, 2, 2,  /* 12-19 */   3, 3, 3, 4, 4, 4, 5, 6,  /* 20-27 */   6, 7, 8, 9,10,11,13,14,  /* 28-35 */  16,18,20,23,25,29,32,36,  /* 36-43 */  40,45,51,57,64,72,81,91   /* 44-51 */};static const uint8_t block_idx_x[16] = {    0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3};static const uint8_t block_idx_y[16] = {    0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3};/* TODO: calculate CABAC costs */static const int i_mb_b_cost_table[18] = {    9, 9, 9, 0, 0, 0, 1, 3, 7, 7, 7, 3, 7, 7, 7, 5, 9, 0};static const int i_mb_b16x8_cost_table[16] = {    0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 7, 5, 7, 9, 9, 9};static const int i_sub_mb_b_cost_table[13] = {    7, 5, 5, 3, 7, 5, 7, 3, 7, 7, 7, 5, 1};static const int i_sub_mb_p_cost_table[4] = {    5, 3, 3, 1};/* initialize an array of lambda*nbits for all possible mvs */static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a ){    static int16_t *p_cost_mv[52];    if( !p_cost_mv[a->i_qp] )    {        /* could be faster, but isn't called many times */        /* factor of 4 from qpel, 2 from sign, and 2 because mv can be opposite from mvp */        int i;        p_cost_mv[a->i_qp] = x264_malloc( (4*4*h->param.analyse.i_mv_range + 1) * sizeof(int16_t) );        p_cost_mv[a->i_qp] += 2*4*h->param.analyse.i_mv_range;        for( i = 0; i <= 2*4*h->param.analyse.i_mv_range; i++ )        {            p_cost_mv[a->i_qp][-i] =            p_cost_mv[a->i_qp][i]  = a->i_lambda * bs_size_se( i );        }    }    a->p_cost_mv = p_cost_mv[a->i_qp];}static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp ){    memset( a, 0, sizeof( x264_mb_analysis_t ) );    /* conduct the analysis using this lamda and QP */    a->i_qp = i_qp;    a->i_lambda = i_qp0_cost_table[i_qp];    h->mb.i_me_method = h->param.analyse.i_me_method;    h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;    h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P                        && h->mb.i_subpel_refine >= 5;    a->b_fast_intra = 0;    /* I: Intra part */    a->i_sad_i16x16 =    a->i_sad_i4x4   =    a->i_sad_i8x8   = COST_MAX;    /* II: Inter part P/B frame */    if( h->sh.i_type != SLICE_TYPE_I )    {        int i;        int i_fmv_range = h->param.analyse.i_mv_range - 16;        /* Calculate max allowed MV range */#define CLIP_FMV(mv) x264_clip3( mv, -i_fmv_range, i_fmv_range )        h->mb.mv_min_fpel[0] = CLIP_FMV( -16*h->mb.i_mb_x - 8 );        h->mb.mv_max_fpel[0] = CLIP_FMV( 16*( h->sps->i_mb_width - h->mb.i_mb_x ) - 8 );        h->mb.mv_min[0] = 4*( h->mb.mv_min_fpel[0] - 16 );        h->mb.mv_max[0] = 4*( h->mb.mv_max_fpel[0] + 16 );        if( h->mb.i_mb_x == 0)        {            h->mb.mv_min_fpel[1] = CLIP_FMV( -16*h->mb.i_mb_y - 8 );            h->mb.mv_max_fpel[1] = CLIP_FMV( 16*( h->sps->i_mb_height - h->mb.i_mb_y ) - 8 );            h->mb.mv_min[1] = 4*( h->mb.mv_min_fpel[1] - 16 );            h->mb.mv_max[1] = 4*( h->mb.mv_max_fpel[1] + 16 );        }#undef CLIP_FMV        a->l0.me16x16.cost =        a->l0.i_cost8x8    = COST_MAX;        for( i = 0; i < 4; i++ )        {            a->l0.i_cost4x4[i] =            a->l0.i_cost8x4[i] =            a->l0.i_cost4x8[i] = COST_MAX;        }        a->l0.i_cost16x8   =        a->l0.i_cost8x16   = COST_MAX;        if( h->sh.i_type == SLICE_TYPE_B )        {            a->l1.me16x16.cost =            a->l1.i_cost8x8    = COST_MAX;            for( i = 0; i < 4; i++ )            {                a->l1.i_cost4x4[i] =                a->l1.i_cost8x4[i] =                a->l1.i_cost4x8[i] =                a->i_cost8x8direct[i] = COST_MAX;            }            a->l1.i_cost16x8   =            a->l1.i_cost8x16   =            a->i_cost16x16bi   =            a->i_cost16x16direct =            a->i_cost8x8bi     =            a->i_cost16x8bi    =            a->i_cost8x16bi    = COST_MAX;        }        /* Fast intra decision */        if( h->mb.i_mb_xy > 4 )        {            const unsigned int i_neighbour = h->mb.i_neighbour;            if(   ((i_neighbour&MB_LEFT) && IS_INTRA( h->mb.type[h->mb.i_mb_xy - 1] ))               || ((i_neighbour&MB_TOP) && IS_INTRA( h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride] ))               || (((i_neighbour&(MB_TOP|MB_LEFT)) == (MB_TOP|MB_LEFT)) && IS_INTRA( h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride-1 ] ))               || ((i_neighbour&MB_TOPRIGHT) && IS_INTRA( h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride+1 ] ))               || (h->sh.i_type == SLICE_TYPE_P && IS_INTRA( h->fref0[0]->mb_type[h->mb.i_mb_xy] ))               || (h->mb.i_mb_xy < 3*(h->stat.frame.i_mb_count[I_4x4] + h->stat.frame.i_mb_count[I_16x16])) )            { /* intra is likely */ }            else            {                a->b_fast_intra = 1;            }        }    }}/* * Handle intra mb *//* Max = 4 */static void predict_16x16_mode_available( unsigned int i_neighbour, int *mode, int *pi_count ){    if( ( i_neighbour & (MB_LEFT|MB_TOP) ) == (MB_LEFT|MB_TOP) )    {        /* top and left avaible */        *mode++ = I_PRED_16x16_V;        *mode++ = I_PRED_16x16_H;        *mode++ = I_PRED_16x16_DC;        *mode++ = I_PRED_16x16_P;        *pi_count = 4;    }    else if( ( i_neighbour & MB_LEFT ) )    {        /* left available*/        *mode++ = I_PRED_16x16_DC_LEFT;        *mode++ = I_PRED_16x16_H;        *pi_count = 2;    }    else if( ( i_neighbour & MB_TOP ) )    {        /* top available*/        *mode++ = I_PRED_16x16_DC_TOP;        *mode++ = I_PRED_16x16_V;        *pi_count = 2;    }    else    {        /* none avaible */        *mode = I_PRED_16x16_DC_128;        *pi_count = 1;    }}/* Max = 4 */static void predict_8x8_mode_available( unsigned int i_neighbour, int *mode, int *pi_count ){    if( ( i_neighbour & (MB_LEFT|MB_TOP) ) == (MB_LEFT|MB_TOP) )    {        /* top and left avaible */        *mode++ = I_PRED_CHROMA_V;        *mode++ = I_PRED_CHROMA_H;        *mode++ = I_PRED_CHROMA_DC;        *mode++ = I_PRED_CHROMA_P;        *pi_count = 4;    }    else if( ( i_neighbour & MB_LEFT ) )    {        /* left available*/        *mode++ = I_PRED_CHROMA_DC_LEFT;        *mode++ = I_PRED_CHROMA_H;        *pi_count = 2;    }    else if( ( i_neighbour & MB_TOP ) )    {        /* top available*/        *mode++ = I_PRED_CHROMA_DC_TOP;        *mode++ = I_PRED_CHROMA_V;        *pi_count = 2;    }    else    {        /* none avaible */        *mode = I_PRED_CHROMA_DC_128;        *pi_count = 1;    }}/* MAX = 8 */static void predict_4x4_mode_available( unsigned int i_neighbour, int idx, int *mode, int *pi_count ){    int b_a, b_b, b_c;    static const unsigned int needmb[16] =    {        MB_LEFT|MB_TOP, MB_TOP,        MB_LEFT,        MB_PRIVATE,        MB_TOP,         MB_TOP|MB_TOPRIGHT,        0,              MB_PRIVATE,        MB_LEFT,        0,        MB_LEFT,        MB_PRIVATE,        0,              MB_PRIVATE,        0,              MB_PRIVATE    };    /* FIXME even when b_c == 0 there is some case where missing pixels     * are emulated and thus more mode are available TODO     * analysis and encode should be fixed too */    b_a = (needmb[idx]&i_neighbour&MB_LEFT) == (needmb[idx]&MB_LEFT);    b_b = (needmb[idx]&i_neighbour&MB_TOP) == (needmb[idx]&MB_TOP);    b_c = (needmb[idx]&i_neighbour&(MB_TOPRIGHT|MB_PRIVATE)) == (needmb[idx]&(MB_TOPRIGHT|MB_PRIVATE));    if( b_a && b_b )    {        *mode++ = I_PRED_4x4_DC;        *mode++ = I_PRED_4x4_H;        *mode++ = I_PRED_4x4_V;        *mode++ = I_PRED_4x4_DDR;        *mode++ = I_PRED_4x4_VR;        *mode++ = I_PRED_4x4_HD;        *mode++ = I_PRED_4x4_HU;        *pi_count = 7;        if( b_c )        {            *mode++ = I_PRED_4x4_DDL;            *mode++ = I_PRED_4x4_VL;            (*pi_count) += 2;        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -