帧内预测代码详解

/***********************************************************************
 * predict.c: h264 encoder
 ***********************************************************************
 * Copyright (C) 2003 Laurent Aimar
 * $Id: predict.c,v 1.1 2004/06/03 19:27:07 fenrir Exp $
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *          Loren Merritt <lorenm@u.washington.edu>
 *
 * 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.
 *********************************************************************
/* predict4x4 are inspired from ffmpeg h264 decoder */
#include "common.h"
#include "clip1.h"

#ifdef _MSC_VER
#undef HAVE_MMXEXT  /* not finished now */
#endif
#ifdef HAVE_MMXEXT
#   include "i386/predict.h"
#endif
/********************************************************************
 * 16x16 prediction for intra luma block
 ********************************************************************

#define PREDICT_16x16_DC(v) \ 本函数用v对16x16的所有像素进行赋值,循环一次赋值一行16个像素(宏定义)
    for( i = 0; i < 16; i++ )\
    {\  第一句既表明p指向的是32位无符号整型变量,也表明其指向的是src(像素起始位置)正因为是32位所以才有后面一次能给4个像素赋值
        uint32_t *p = (uint32_t*)src;\ 
        *p++ = v;\  指针加1 移四个字节即32位 ,这一句可以给四个像素点赋值,一个像素占一个字节
        *p++ = v;\
        *p++ = v;\
        *p++ = v;\
        src += FDEC_STRIDE;\  FDEC_STRIDE=32 和 FENC_STRIDE=16 是定义的。这里32,16 是字节数。(根据存储空间的使用规则不难理解)
    }

static void predict_16x16_dc( uint8_t *src ) 本函数是用当前宏块上方和左方共32个像素的和的均值进行预测,预测后所有16x16个像素的值都被该均值替代
{
    uint32_t dc = 0;-------------------------------------定义dc为32 位无符号整型数 (目的同上)
    int i; 

    for( i = 0; i < 16; i++ )
    {
        dc += src[-1 + i * FDEC_STRIDE];--------当前宏块的第i行左方的像素值
        dc += src[i - FDEC_STRIDE]; -------------此时dc为(宏块第i行左方的像素值)+(宏块第i 列上方的像素值)
    }-------------------------------------------------------循环16次后dc为当前宏块左方和上方所有32 个像素的和 
    dc = (( dc + 16 ) >> 5) * 0x01010101;-----------所有32个像素取均值:+16是为了四舍五入,右移5表示除以2的5次方即32,* 0x01010101为了一次能给4个像素赋值。
                                          (例如0x12* 0x01010101=0x12121212,用windows自带的计算器可查看)对照PREDICT_16x16_DC(v)即可明白其作用
    PREDICT_16x16_DC(dc);------------------------将得出来的dc带入该赋值函数所有16x16个像素的值相同
}
static void predict_16x16_dc_left( uint8_t *src ) 本函数用左边的16个像素的均值对当前宏块进行预测,预测后当前宏块的所有16x16个像素的值都被该均值替代
{
    uint32_t dc = 0;
    int i;

    for( i = 0; i < 16; i++ )
    {
        dc += src[-1 + i * FDEC_STRIDE];----------当前宏块的第i行的左边像素的值
    }----------------------------------------------------------循环16次后dc为当前宏块左方的16个像素值的和
    dc = (( dc + 8 ) >> 4) * 0x01010101;---------------对16个像素取均值

   PREDICT_16x16_DC(dc);--------------------------对16x16所有像素赋值
   }
static void predict_16x16_dc_top( uint8_t *src ) 本函数用上方16个像素点的均值覆盖当前16x16块的所有像素值
{
    uint32_t dc = 0;
    int i;

    for( i = 0; i < 16; i++ )
    {
        dc += src[i - FDEC_STRIDE];------------当前宏块第i 列上方的像素值 
    }
    dc = (( dc + 8 ) >> 4) * 0x01010101;-----------求均值

    PREDICT_16x16_DC(dc);
}
static void predict_16x16_dc_128( uint8_t *src ) 本函数用固定值128即0x80对16x16块的所有像素进行覆盖
{
    int i;
    PREDICT_16x16_DC(0x80808080);
}
static void predict_16x16_h( uint8_t *src ) 本函数用当前块的左方像素进行水平方向的预测(宏块的第i 行所有像素值都等于其左边的像素值)
{
    int i;

    for( i = 0; i < 16; i++ )
    {
        const uint32_t v = 0x01010101 * src[-1]; ------------- src[-1]是当前宏块第i行左方的像素值( i = 0; i < 16; i++ )
        uint32_t *p = (uint32_t*)src;

        *p++ = v;
        *p++ = v;
        *p++ = v;
        *p++ = v; 

        src += FDEC_STRIDE;-----------------------------------循环一次,指针向下移一行

    }---------------------------------------------------------------------循环一次预测一行,共循环十六次
}
static void predict_16x16_v( uint8_t *src ) 本函数用当前块的上方像素进行垂直方向预测(宏块的第i列像素都等于其上方的像素值)
{
 (因为v是32位,四字节无符号整型数,可以将src指向的连续四个字节的单元内的值赋给它)
   uint32_t v0 = *(uint32_t*)&src[ 0-FDEC_STRIDE];------v0为当前块上方第0-3个像素值
    uint32_t v1 = *(uint32_t*)&src[ 4-FDEC_STRIDE];------v1为当前块上方第4-7个像素值
    uint32_t v2 = *(uint32_t*)&src[ 8-FDEC_STRIDE];------v2为当前块上方第8-11个像素值
    uint32_t v3 = *(uint32_t*)&src[12-FDEC_STRIDE];------v3为当前块上方第12-15个像素值
    int i;

    for( i = 0; i < 16; i++ )
    {
        uint32_t *p = (uint32_t*)src;----p指向当前块的第一个像素单元
        *p++ = v0;-------------------------将v0赋给当前块第i行第0-3个像素
        *p++ = v1;-------------------------将v1赋给当前块第i行第4-7个像素
        *p++ = v2;-------------------------将v2赋给当前块第i行第8-11个像素
        *p++ = v3;-------------------------将v3赋给当前块第i行第12-15个像素
        src += FDEC_STRIDE;----------使src指向下一行开始位置
    }
}
static void predict_16x16_p( uint8_t *src ) 用特定的计算方法对宏块进行预测
{
    int x, y, i;                                                                        
   int a, b, c;                                                                        
   int H = 0;                                                                        
    int V = 0;                                                                        
    int i00;

    /* calcule H and V */
    for( i = 0; i <= 7; i++ )-------循环8次
    { 
        H += ( i + 1 ) * ( src[ 8 + i - FDEC_STRIDE ] - src[6 -i -FDEC_STRIDE] ); ---------宏块上方对应位置的16个像素点相减再求和(用到了左上角的点而舍弃了中间点)
        V += ( i + 1 ) * ( src[-1 + (8+i)*FDEC_STRIDE] - src[-1 + (6-i)*FDEC_STRIDE] );------宏块左方对应位置的16个像素点相减再求和
    }
---------------根据公式计算H和V以及a、b、c的值
    a = 16 * ( src[-1 + 15*FDEC_STRIDE] + src[15 - FDEC_STRIDE] );
    b = ( 5 * H + 32 ) >> 6;
    c = ( 5 * V + 32 ) >> 6;

    i00 = a - b * 7 - c * 7 + 16;

    for( y = 0; y < 16; y++ )
    {
        int pix = i00; 
        for( x = 0; x < 16; x++ )--------------------循环一次对一列像素点附值
        {
            src[x] = x264_clip_uint8( pix>>5 );
            pix += b;------------------------循环一次增加( 5 * H + 32 ) >> 6
        }-----Pred(x,j) =clip((a+b(-7+x)+c(-7+j))+16)>>5)第j行所有像素的预测值
        src += FDEC_STRIDE;---------------------指向下一行
        i00 += c;--------------------------------循环一次增加( 5 * V + 32 ) >> 6
    }----Pred(x,y) = clip((a+b(-7+x)+c(-7+y))+16)>>5)   16*16所有像素的预测值
}
分析:A、255=2e8=1111 1111,(~255)= 0xffffff00
a&0xffffff00,如果不等于0,分两种情况
1.a<0,那么最高位肯定是1,-a变为正数,移位31后必然全为0,即0x00000000,结果为0
2.a>0,那么最高位肯定是0,另外a肯定>255,-a变为负数,移位31后必然全为1,即0xffffffff,结果为255
所以通过(-a)>>31这样的运算避免了判断:if(a<0)return 0;
else if(a>255) return 255;
else return a;
B、a&0xffffff00,如果等于0,说明0<a<255,不需要处理,直接输出.
/*****************************************************************
 * 8x8 prediction for intra chroma block
 *****************************************************************

static void predict_8x8c_dc_128( uint8_t *src )
{
    int y;

    for( y = 0; y < 8; y++ )
    {
        uint32_t *p = (uint32_t*)src;
        *p++ = 0x80808080;
        *p++ = 0x80808080;
        src += FDEC_STRIDE;
    }
}
static void predict_8x8c_dc_left( uint8_t *src )
{
    int y;
    uint32_t dc0 = 0, dc1 = 0;

    for( y = 0; y < 4; y++ )
    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值