Something about C

What the hell? How long since I touch with C?
What a pity, I have to work with it now.

Global variable
Better define a global.h, define variable as
extern int i;

And when U use it in another C, do as
unsigned char SysEventCounter = 0;
U can do it in one C file, if U do it in another C file, it will broadcast error.

Operator
Sometimes U encounter & and &&, what’s difference between them? & is bit operator for instance: 5 & 3 = 3; As for &&, a logical operator, 1 && 3 = 1.

Pointer
To allocation two dimension dynamic array:

#include <malloc.h>
reconstructedMVField**MVField;
MVField=(reconstructedMVField**)malloc((picSizeY>>2) * sizeof(reconstructedMVField *));
for(j = 0; j < (picSizeY >> 2); j++)
{
    MVField[j]=(reconstructedMVField*)malloc((picSizeX>>2) * sizeof(reconstructedMVField));
}  
for(j = 0; j < (picSizeY >> 2); j++)
    free(MVField[j]);
free(MVField);

This guy’s blog is not bad.

http://www.cnblogs.com/chenwenbiao/archive/2011/11/04/2236679.html

http://blog.sina.com.cn/s/blog_5d6189e50100bbnd.html

http://blog.sina.com.cn/s/blog_67299aec0100mqek.html

http://zhidao.baidu.com/link?url=4pICOIhwQAtDmMO3LYT-rKMCY-OQWsQJ2TjKGQM56I_wCit92g_s1IaXoMODJOEzpXUol_CItlPVv6AnXBOCbq

MVField **a;
MVField *temp;
a = (MVField**)malloc(10*sizeof(MVField*));

for (int i = 0; i < 10; i++)
    a[i] = (MVField*)malloc(9*sizeof(MVField));

for (int i = 0; i < 10; i++)
{
    temp = a[i];
    for (int j = 0; j < 9; j++)
    {
        (*temp).priority = i + j;
        (*temp).MVdistance = 0;
        (*temp).location.X = j;
        (*temp).location.Y = i;
        *temp++;
    }
}

http://blog.csdn.net/happen23/article/details/4710368

Type Transition

double temp_mvX = 0;
double temp_mvY = 0;
int mvX;
int mvY;    
int index_X = 0;
int index_Y = 0;
int region_cover_X = 0;
int region_cover_Y = 0;
temp_mvX = round( (refPic->mv_info[i_row][j_column]).mv[0].mv_x/4.0 );  // X MV in pixel 
temp_mvY = round( (refPic->mv_info[i_row][j_column]).mv[0].mv_y/4.0 );  // Y MV in pixel
                // to get which region the 4x4 block has covered
mvX = temp_mvX;
mvY = temp_mvY;
region_cover_X = (mvX >= 0) ? (mvX % 4) : ((4 - abs(mvX) % 4)%4);
region_cover_Y = (mvY >= 0) ? (mvY % 4) : ((4 - abs(mvY) % 4)%4);
index_X = j_column + mvX / 4;  // to index block position
index_Y = i_row + mvY / 4;

-> and .
If pointer, “->”
else “.”
But it doesn’t matter, as compiler will tell U.

Multiple Dimension
Be careful of X and Y. Hehe!

Defines like Taps

#ifdef  ERC_BYCOPY_YUV
#undef  ERC_BYCOPY_YUV
#endif
#define IntraMBinSliceAndMBNumInSlice   "D:/CY/JM18_6_RC/scene_change_p_slice.txt"
#if (DEBUG_OPEN == 1)
    FILE *erc_dec_mv_info = NULL;
    recfr.p_Vid = p_Vid;
    VideoParameters *p_Vid1 = recfr.p_Vid;
    StorablePicture* refPic = p_Vid1->ppSliceList[0]->listX[0][0];//p_Vid->ppSliceList[0]->listX[0][0];
    #if(DEBUG_INFO_FOR_MV == 1)
        erc_dec_mv_info = fopen(DEBUG_INFO_FOR_MV_TXT, "a+");
        if(p_Vid->dec_picture->frame_num == 3)  // Just look the second frame's mv information and be attention to the start frame and end frame in Concealment_CY.h 
        {
            for(i_row = 0;i_row < ((*dec_picture)->size_y) /4 ;i_row++)
            {
                for(j_column = 0;j_column < ((*dec_picture)->size_x) / 4;j_column++)
                {
                    fprintf(erc_dec_mv_info, "(%d,%d) -> (%d,%d)->(%d,%d)  ,",i_row,j_column,(refPic->mv_info[i_row][j_column]).mv[0].mv_x,(refPic->mv_info[i_row][j_column]).mv[0].mv_y,(refPic->mv_info[i_row][j_column]).ref_idx[0],(refPic->mv_info[i_row][j_column]).ref_idx[1]);
                }
                fprintf(erc_dec_mv_info,"\n");
            }
            fclose(erc_dec_mv_info);
        }
    #endif
#endif

Bad Code

#define  _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <math.h>
#include <malloc.h>

#define LENCorrupetedBLKMVList sizeof(CorrupetedBLKMVList)
#define AddrNull  0

typedef struct motion_vector
{
    int mvX;
    int mvY;
} ercMV;

// This struct means that the reconstructed MV of each 4x4 block
typedef struct important_POS
{
    int Block_X;  // Here is just for P frame. 
    int Block_Y;            // 1 means the best suit, next 2, then 3, then 4, then 5 and other number means this mv is empty.
}Position;

void simpletest(ercMV Cst_mv){
    printf("%d  %d\n",Cst_mv.mvX,Cst_mv.mvY);
}

typedef struct Corrupted_Per_Block_Ref_MV_Field_Information
{
    Position PPixle;
    ercMV   estiMV;
    int *  pixel;   // With chroma and luma 
}RefBLKMVInfor;

// This struct is used for every corrupted block  
typedef struct Corrupted_Block_MV_List
{
    RefBLKMVInfor  blkInfor;
    struct Corrupted_Block_MV_List* next;
}CorrupetedBLKMVList;

/*!
 ************************************************************************
 * \brief
 *      To create MV list for block in corrupted frameMV field.
 * \return
 *      Pointer of struct CorrupetedBLKMVList.
 * \param NONE
 * \2015/10/9 14:56:30
 ************************************************************************
 */
CorrupetedBLKMVList* CreateMVFieldPerBlockMVListHead(void)
{
    CorrupetedBLKMVList* head;   
    head = (CorrupetedBLKMVList*)malloc(LENCorrupetedBLKMVList);
    head -> next = AddrNull;

    return head;
}

/*!
 ************************************************************************
 * \brief
 *      To insert MV block node  for block in corrupted frameMV field.
 * \return
 *      Pointer head of struct CorrupetedBLKMVList.
 * \param NONE
 * \2015/10/9 14:56:30
 ************************************************************************
 */
void InsertMVNodeIntoBlockList(CorrupetedBLKMVList* head, RefBLKMVInfor  insertData, int blockSize)
{
    CorrupetedBLKMVList*temp;
    int i;
    temp = AddrNull;
    temp = (CorrupetedBLKMVList*)malloc(LENCorrupetedBLKMVList);
    if(temp == AddrNull)
    {
        printf("Error:In malloc memory for CorrupetedBLKMVList\n");
        return; 
    }   

    temp -> next = AddrNull;
    temp -> blkInfor.PPixle.Block_X =  insertData.PPixle.Block_X;
    temp -> blkInfor.PPixle.Block_Y =  insertData.PPixle.Block_Y;
    temp -> blkInfor.estiMV.mvX =  insertData.estiMV.mvX;
    temp -> blkInfor.estiMV.mvY = insertData.estiMV.mvY;
    temp -> blkInfor.pixel = (int*)malloc(blockSize*sizeof(int));

    for(i = 0; i < blockSize; i++)
    {
        temp -> blkInfor.pixel[i] = insertData.pixel[i];
    }

    head -> next = temp;
}

int main()
{
    int i = 0;
    const int blockSize = 16;

    RefBLKMVInfor  testInsertData;
    RefBLKMVInfor  testInsertData2;

    // Create node
    CorrupetedBLKMVList* test_create = AddrNull;
    CorrupetedBLKMVList* search = AddrNull;
    test_create = CreateMVFieldPerBlockMVListHead();
    if (test_create->next == AddrNull)
        printf("Good!\n");
    if(test_create != AddrNull)
        printf("Create OK:%d \n",test_create);

    //  Insert node
    testInsertData.pixel = (int*)malloc(blockSize*sizeof(int));

    for(i = 0; i < blockSize; i++)
    {
        testInsertData.pixel[i] = i;
    }
    testInsertData.PPixle.Block_X = 1;
    testInsertData.PPixle.Block_Y = 2;
    testInsertData.estiMV.mvX = 3;
    testInsertData.estiMV.mvY = 5;

    testInsertData2.pixel = (int*)malloc(blockSize*sizeof(int));

    for (i = 0; i < blockSize; i++)
    {
        testInsertData2.pixel[i] = i+1;
    }
    testInsertData2.PPixle.Block_X = 100;
    testInsertData2.PPixle.Block_Y = 21;
    testInsertData2.estiMV.mvX = 23;
    testInsertData2.estiMV.mvY = 15;

    InsertMVNodeIntoBlockList(test_create, testInsertData, blockSize);
    InsertMVNodeIntoBlockList(test_create, testInsertData2, blockSize);
    InsertMVNodeIntoBlockList(test_create, testInsertData, blockSize);
    search = test_create;

    while (search != AddrNull)
    {
        printf("%d\n", search);
        printf("%d\n", search->blkInfor.PPixle.Block_X);
        printf("%d\n", search->blkInfor.PPixle.Block_Y);
        printf("%d\n", search->blkInfor.estiMV.mvX);
        printf("%d\n", search->blkInfor.estiMV.mvY);
//      printf("%d\n", search->blkInfor.pixel[5]);
        search = search->next;
    }

    search = test_create;

    while (search->next != AddrNull)
    {
    //  free(search->blkInfor.pixel);
        search = search->next;
    }

    free(testInsertData.pixel); 
    free(testInsertData2.pixel);
    system("pause");
    return 0;
}

Good List

#define  _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <math.h>
#include <malloc.h>

#define LENCorrupetedBLKMVList sizeof(CorrupetedBLKMVList)
#define AddrNull  0

typedef struct motion_vector
{
    int mvX;
    int mvY;
} ercMV;

// This struct means that the reconstructed MV of each 4x4 block
typedef struct important_POS
{
    int Block_X;  // Here is just for P frame. 
    int Block_Y;            // 1 means the best suit, next 2, then 3, then 4, then 5 and other number means this mv is empty.
}Position;

void simpletest(ercMV Cst_mv){
    printf("%d  %d\n",Cst_mv.mvX,Cst_mv.mvY);
}

typedef struct Corrupted_Per_Block_Ref_MV_Field_Information
{
    Position PPixle;
    ercMV   estiMV;
    int *  pixel;   // With chroma and luma 
}RefBLKMVInfor;

// This struct is used for every corrupted block  
typedef struct Corrupted_Block_MV_List
{
    RefBLKMVInfor  blkInfor;
    struct Corrupted_Block_MV_List* next;
}CorrupetedBLKMVList;

/*!
 ************************************************************************
 * \brief
 *      To create MV list for block in corrupted frameMV field.
 * \return
 *      Pointer of struct CorrupetedBLKMVList.
 * \param NONE
 * \2015/10/9 14:56:30
 ************************************************************************
 */
CorrupetedBLKMVList* CreateMVFieldPerBlockMVListHead(void)
{
    CorrupetedBLKMVList* head;   
    head = (CorrupetedBLKMVList*)malloc(LENCorrupetedBLKMVList);
    head -> next = AddrNull;

    return head;
}

/*!
 ************************************************************************
 * \brief
 *      To insert MV block node  for block in corrupted frameMV field.
 * \return
 *      Pointer head of struct CorrupetedBLKMVList.
 * \param NONE
 * \2015/10/9 14:56:30
 ************************************************************************
 */
CorrupetedBLKMVList* InsertMVNodeIntoBlockList(CorrupetedBLKMVList* head, RefBLKMVInfor  insertData, int blockSize)
{
    CorrupetedBLKMVList*temp = AddrNull;
    int i;

    if (head == AddrNull)  // first node
    {
        head = (CorrupetedBLKMVList*)malloc(LENCorrupetedBLKMVList);
        head->next = AddrNull;
        head->blkInfor.PPixle.Block_X = insertData.PPixle.Block_X;
        head->blkInfor.PPixle.Block_Y = insertData.PPixle.Block_Y;
        head->blkInfor.estiMV.mvX = insertData.estiMV.mvX;
        head->blkInfor.estiMV.mvY = insertData.estiMV.mvY;
        head->blkInfor.pixel = (int*)malloc(blockSize*sizeof(int));

        for (i = 0; i < blockSize; i++)
        {
            head->blkInfor.pixel[i] = insertData.pixel[i];
        }
    }
    else
    {
        temp = (CorrupetedBLKMVList*)malloc(LENCorrupetedBLKMVList);

        if (temp == AddrNull)
        {
            printf("Error:In malloc memory for CorrupetedBLKMVList\n");
            return;
        }

        temp->blkInfor.PPixle.Block_X = insertData.PPixle.Block_X;
        temp->blkInfor.PPixle.Block_Y = insertData.PPixle.Block_Y;
        temp->blkInfor.estiMV.mvX = insertData.estiMV.mvX;
        temp->blkInfor.estiMV.mvY = insertData.estiMV.mvY;
        temp->blkInfor.pixel = (int*)malloc(blockSize*sizeof(int));

        for (i = 0; i < blockSize; i++)
        {
            temp->blkInfor.pixel[i] = insertData.pixel[i];
        }

        temp->next = head;
        head = temp;
    }
    return head;
}

int main()
{
    int i = 0;
    const int blockSize = 16;

    RefBLKMVInfor  testInsertData;
    RefBLKMVInfor  testInsertData2;

    // Create node
    CorrupetedBLKMVList* test_create = AddrNull;
    CorrupetedBLKMVList* search = AddrNull;

    if(test_create != AddrNull)
        printf("Create OK:%d \n",test_create);

    //  Insert node
    testInsertData.pixel = (int*)malloc(blockSize*sizeof(int));

    for(i = 0; i < blockSize; i++)
    {
        testInsertData.pixel[i] = i;
    }
    testInsertData.PPixle.Block_X = 1;
    testInsertData.PPixle.Block_Y = 2;
    testInsertData.estiMV.mvX = 3;
    testInsertData.estiMV.mvY = 5;

    testInsertData2.pixel = (int*)malloc(blockSize*sizeof(int));

    for (i = 0; i < blockSize; i++)
    {
        testInsertData2.pixel[i] = i+1;
    }
    testInsertData2.PPixle.Block_X = 100;
    testInsertData2.PPixle.Block_Y = 21;
    testInsertData2.estiMV.mvX = 23;
    testInsertData2.estiMV.mvY = 15;

    test_create = InsertMVNodeIntoBlockList(test_create, testInsertData, blockSize);
    test_create = InsertMVNodeIntoBlockList(test_create, testInsertData2, blockSize);
    test_create = InsertMVNodeIntoBlockList(test_create, testInsertData, blockSize);
    search = test_create;

    while (search != AddrNull)
    {
        printf("%d\n", search);
        printf("%d\n", search->blkInfor.PPixle.Block_X);
        printf("%d\n", search->blkInfor.PPixle.Block_Y);
        printf("%d\n", search->blkInfor.estiMV.mvX);
        printf("%d\n", search->blkInfor.estiMV.mvY);
//      printf("%d\n", search->blkInfor.pixel[5]);
        search = search->next;
    }

    search = test_create;

    while (search->next != AddrNull)
    {
    //  free(search->blkInfor.pixel);
        search = search->next;
    }

    free(testInsertData.pixel); 
    free(testInsertData2.pixel);
    system("pause");
    return 0;
}

Malloc Two Dimension Pointer
I think the code I write is so beautiful!

    CorrupetedBLKMVList*** Pic;
    Pic = (CorrupetedBLKMVList***)malloc(height*sizeof(CorrupetedBLKMVList**));

    for(j = 0; j < height;j++)
    {
        Pic[j] = (CorrupetedBLKMVList**)malloc(width*sizeof(CorrupetedBLKMVList*));
    }   
    for(j = 0; j < height;j++)
    {
        free(Pic[j]);
    }
    free(Pic);

Flexible Pointer Using

static void GetLumaBlock10(imgpel **block, imgpel **imgY, int blockSizeX, int blockSizeY, int XPosPixel, int YPosPixel, int maxImgpelValue)
{
    imgpel *p0, *p1, *p2, *p3, *p4, *p5;
    imgpel *orig_line, *cur_line;
    int i, j;
    int result;

    for (j = 0; j < blockSizeY; j++)
    {
        cur_line = &(cur_imgY[j][XPosPixel]);
        p0 = &cur_imgY[j][XPosPixel - 2];
        p1 = p0 + 1;
        p2 = p1 + 1;
        p3 = p2 + 1;
        p4 = p3 + 1;
        p5 = p4 + 1;
        orig_line = block[j];            

        for (i = 0; i < blockSizeX; i++)
        {        
            result  = (*(p0++) + *(p5++)) - 5 * (*(p1++) + *(p4++)) + 20 * (*(p2++) + *(p3++));
            *orig_line = (imgpel) iClip1(maxImgpelValue, ((result + 16)>>5));
            *orig_line = (imgpel) ((*orig_line + *(cur_line++) + 1 ) >> 1);
            orig_line++;
        }
    }
}

Divide
‘&’ is a excellent operation. Just think that (-5) & 3 = 3, you will get the right result of U want.
‘%’ is another operation that (-5) % 4 = -1
‘a & b’ can be get by ‘b < 0? (b - abs(a%b)) : (a%b)’
I found that the operation is excellent.
(-3) >> 2 = -1 <=> (int)floor((-3.0)/4.0)
(-3)/4 = 0 <=> (int)ceil((-3.0)/4.0)

Create Files and Copy String

const char* s = "../../new3";

is the same as

#define s "../../new3"
#include <stdio.h>
#include <math.h>
#include <malloc.h>
#include <process.h>
#include <direct.h>
#include <sys/stat.h>
#include <string.h>

static void try_create_dir(const char* dest_dir)
{
    if (_access(dest_dir, 0) != 0)
    {
        _mkdir(dest_dir);
    }
}

int main()
{
    const char* s = "../../new3";
    char s1[100];
    FILE *f1 = NULL;

    try_create_dir(s);

    strcpy(s1, s); 
    strcat(s1,"/file1.txt" ); 

    if ((f1 = fopen(s1, "w+")) == NULL)
    {
        printf("Error in create log file of reconstructed MV field.\n");
    }

    fprintf(f1, "----------------------Successful-------------------------------------\n");

    fclose(f1);

    system("pause");
    return 0;
}

VS Skills
1、improve Ur VS running speed:

http://www.jb51.net/os/windows/73851.html
2、Condition Breakpoint:
Double click then tight click on the breakpoint and set condition:
for instance:
pxlLocation.Y == 0x110

MEMCPY
For two dynamic arrays copy by memcpy, be careful of data type.

// Y component assignment.
        frame_ref->imgY_ref = (imgPel**)malloc(tempHeight * sizeof(imgPel*));

        for(i = 0; i < tempHeight; i++)
        {
            frame_ref->imgY_ref[i]=(imgPel*)malloc(tempWidth * sizeof(imgPel));
            if(refPic->imgY[i] != NULL)
            {   
                memcpy(frame_ref->imgY_ref[i], refPic->imgY[i], tempWidth * sizeof(imgPel));
            }
        }  

C-free using and debug skills

http://www.programarts.com/cfree_ch/doc/help/UsingCF/Debug.htm
http://www.cnblogs.com/lidabo/p/3631224.html

C File Operetion

http://blog.csdn.net/gneveek/article/details/6848473
http://www.cppblog.com/Tongy0/archive/2014/03/23/206305.html

C Using Skill As 默认参数

http://blog.sina.com.cn/s/blog_933d4eec0100vdk0.html

Stack Overflow

http://www.cnblogs.com/fanzhidongyzby/archive/2013/08/10/3250405.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值