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

📄 erc_do_p.c

📁 H264视频编解码程序
💻 C
📖 第 1 页 / 共 4 页
字号:
        for(uv=0;uv<2;uv++)
        {
            joff = subblk_offset_y[yuv][0][0];
            j4=img->pix_c_y+joff;
            ioff = subblk_offset_x[yuv][0][0];
            i4=img->pix_c_x+ioff;

            for(jj=0;jj<2;jj++)
            {
                jf=(j4+jj)/(img->mb_cr_size_y/4);     // jf  = Subblock_y-coordinate
                for(ii=0;ii<2;ii++)
                {
                    ifx=(i4+ii)/(img->mb_cr_size_x/4);  // ifx = Subblock_x-coordinate

                    i1=(i4+ii)*f1_x + mv[0];
                    j1=(j4+jj)*f1_y + mv[1];

                    ii0=max (0, min (i1/f1_x,   dec_picture->size_x_cr-1));
                    jj0=max (0, min (j1/f1_y,   dec_picture->size_y_cr-1));
                    ii1=max (0, min ((i1+f2_x)/f1_x, dec_picture->size_x_cr-1));
                    jj1=max (0, min ((j1+f2_y)/f1_y, dec_picture->size_y_cr-1));

                    if1=(i1 & f2_x);
                    jf1=(j1 & f2_y);
                    if0=f1_x-if1;
                    jf0=f1_y-jf1;

                    img->mpr[ii][jj]=(if0*jf0*listX[list][ref_frame]->imgUV[uv][jj0][ii0]+
                        if1*jf0*listX[list][ref_frame]->imgUV[uv][jj0][ii1]+
                        if0*jf1*listX[list][ref_frame]->imgUV[uv][jj1][ii0]+
                        if1*jf1*listX[list][ref_frame]->imgUV[uv][jj1][ii1]+f4)/f3;
                }
            }

            for (i = 0; i < 2; i++)
            {
                for (j = 0; j < 2; j++)
                {
                    pMB[i*2+j] = img->mpr[j][i];
                }
            }
            pMB += 4;

        }
    }
}

/*!
************************************************************************
* \brief
*    compares two stored pictures by picture number for qsort in descending order
*
************************************************************************
*/
static int compare_pic_by_pic_num_desc( const void *arg1, const void *arg2 )
{
    if ( (*(StorablePicture**)arg1)->pic_num < (*(StorablePicture**)arg2)->pic_num)
        return 1;
    if ( (*(StorablePicture**)arg1)->pic_num > (*(StorablePicture**)arg2)->pic_num)
        return -1;
    else
        return 0;
}

/*!
************************************************************************
* \brief
*    compares two stored pictures by picture number for qsort in descending order
*
************************************************************************
*/
static int compare_pic_by_lt_pic_num_asc( const void *arg1, const void *arg2 )
{
    if ( (*(StorablePicture**)arg1)->long_term_pic_num < (*(StorablePicture**)arg2)->long_term_pic_num)
        return -1;
    if ( (*(StorablePicture**)arg1)->long_term_pic_num > (*(StorablePicture**)arg2)->long_term_pic_num)
        return 1;
    else
        return 0;
}

/*!
************************************************************************
* \brief
*    compares two stored pictures by poc for qsort in ascending order
*
************************************************************************
*/
static int compare_pic_by_poc_asc( const void *arg1, const void *arg2 )
{
    if ( (*(StorablePicture**)arg1)->poc < (*(StorablePicture**)arg2)->poc)
        return -1;
    if ( (*(StorablePicture**)arg1)->poc > (*(StorablePicture**)arg2)->poc)
        return 1;
    else
        return 0;
}


/*!
************************************************************************
* \brief
*    compares two stored pictures by poc for qsort in descending order
*
************************************************************************
*/
static int compare_pic_by_poc_desc( const void *arg1, const void *arg2 )
{
    if ( (*(StorablePicture**)arg1)->poc < (*(StorablePicture**)arg2)->poc)
        return 1;
    if ( (*(StorablePicture**)arg1)->poc > (*(StorablePicture**)arg2)->poc)
        return -1;
    else
        return 0;
}

/*!
************************************************************************
* \brief
*    Copy image data from one array to another array
************************************************************************
*/

static
void CopyImgData(imgpel **inputY, imgpel ***inputUV, imgpel **outputY, 
                 imgpel ***outputUV, int img_width, int img_height)
{
    int x, y;

    for (y=0; y<img_height; y++)
        for (x=0; x<img_width; x++)
            outputY[y][x] = inputY[y][x];

    for (y=0; y<img_height/2; y++)
        for (x=0; x<img_width/2; x++)
        {
            outputUV[0][y][x] = inputUV[0][y][x];
            outputUV[1][y][x] = inputUV[1][y][x];
        }
}

/*!
************************************************************************
* \brief
*    Copies the last reference frame for concealing reference frame loss. 
************************************************************************
*/

static StorablePicture *
get_last_ref_pic_from_dpb()
{
    int used_size = dpb.used_size - 1;
    int i;

    for(i = used_size; i >= 0; i--)
    {
        if (dpb.fs[i]->is_used==3)
        {
            if (((dpb.fs[i]->frame->used_for_reference) && 
                (!dpb.fs[i]->frame->is_long_term)) /*||  ((dpb.fs[i]->frame->used_for_reference==0)
                                                   && (dpb.fs[i]->frame->slice_type == P_SLICE))*/ )
            {
                return dpb.fs[i]->frame;
            }
        }
    }

    return NULL;
}

/*!
************************************************************************
* \brief
* Conceals the lost reference or non reference frame by either frame copy 
* or motion vector copy concealment. 
*
************************************************************************
*/

static void copy_to_conceal(StorablePicture *src, StorablePicture *dst, ImageParameters *img)
{
    int i=0;
    int mv[3];
    int multiplier;
    imgpel *predMB, *storeYUV;
    int j, y, x, mb_height, mb_width, ii=0, jj=0;
    int uv;
    int mm, nn;
    int scale = 1;
    // struct inp_par *test;

    img->current_mb_nr = 0;

    dst->PicSizeInMbs  = src->PicSizeInMbs;

    dst->slice_type = src->slice_type = img->conceal_slice_type;

    dst->idr_flag = 0; //since we do not want to clears the ref list

    dst->no_output_of_prior_pics_flag = src->no_output_of_prior_pics_flag;
    dst->long_term_reference_flag = src->long_term_reference_flag;
    dst->adaptive_ref_pic_buffering_flag = src->adaptive_ref_pic_buffering_flag = 0;
    dst->chroma_format_idc = src->chroma_format_idc;
    dst->frame_mbs_only_flag = src->frame_mbs_only_flag;
    dst->frame_cropping_flag = src->frame_cropping_flag;
    dst->frame_cropping_rect_left_offset = src->frame_cropping_rect_left_offset;
    dst->frame_cropping_rect_right_offset = src->frame_cropping_rect_right_offset;
    dst->frame_cropping_rect_bottom_offset = src->frame_cropping_rect_bottom_offset;
    dst->frame_cropping_rect_top_offset = src->frame_cropping_rect_top_offset;
    dst->qp = src->qp;
    dst->slice_qp_delta = src->slice_qp_delta;

    dec_picture = src;

    // Conceals the missing frame by frame copy concealment
    if (img->conceal_mode==1)
    {     
        // We need these initializations for using deblocking filter for frame copy
        // concealment as well.
        dst->PicWidthInMbs = src->PicWidthInMbs;
        dst->PicSizeInMbs = src->PicSizeInMbs;

        CopyImgData(src->imgY, src->imgUV, 
            dst->imgY, dst->imgUV,
            img->width, img->height);

    }

    // Conceals the missing frame by motion vector copy concealment
    if (img->conceal_mode==2)
    {
        if (dec_picture->chroma_format_idc != YUV400)
        {
            storeYUV = (imgpel *) malloc ( (16 + (img->mb_cr_size_x*img->mb_cr_size_y)*2/16) * sizeof (imgpel));
        }
        else
        {
            storeYUV = (imgpel *) malloc (16  * sizeof (imgpel));
        }

        erc_img = img;

        dst->PicWidthInMbs = src->PicWidthInMbs;
        dst->PicSizeInMbs = src->PicSizeInMbs;
        mb_width = dst->PicWidthInMbs;
        mb_height = (dst->PicSizeInMbs)/(dst->PicWidthInMbs);
        scale = (img->conceal_slice_type == B_SLICE) ? 2 : 1;

        if(img->conceal_slice_type == B_SLICE)
            init_lists_for_non_reference_loss(dst->slice_type, img->currentSlice->structure);
        else
            init_lists(dst->slice_type, img->currentSlice->structure);

        multiplier = BLOCK_SIZE;

        for(i=0;i<mb_height*4;i++)
        {
            mm = i*BLOCK_SIZE;
            for(j=0;j<mb_width*4;j++)
            {                       
                nn = j*BLOCK_SIZE;

                mv[0] = src->mv[LIST_0][i][j][0] / scale;
                mv[1] = src->mv[LIST_0][i][j][1] / scale;
                mv[2] = src->ref_idx[LIST_0][i][j];


                if(mv[2]<0)
                    mv[2]=0;

                dst->mv[LIST_0][i][j][0] = mv[0];
                dst->mv[LIST_0][i][j][1] = mv[1];
                dst->ref_idx[LIST_0][i][j] = mv[2];

                x = (j)*multiplier;
                y = (i)*multiplier;

                if ((mm%16==0) && (nn%16==0))
                    img->current_mb_nr++;

                buildPredblockRegionYUV(erc_img, mv, x, y, storeYUV, LIST_0);

                predMB = storeYUV;

                for(ii=0;ii<multiplier;ii++)
                {
                    for(jj=0;jj<multiplier;jj++)
                    {
                        dst->imgY[i*multiplier+ii][j*multiplier+jj] = predMB[ii*(multiplier)+jj];
                    }
                }

                predMB = predMB + (multiplier*multiplier);

                if (dec_picture->chroma_format_idc != YUV400)
                {

                    for(uv=0;uv<2;uv++)
                    {
                        for(ii=0;ii< (multiplier/2);ii++)
                        {
                            for(jj=0;jj< (multiplier/2);jj++)
                            {
                                dst->imgUV[uv][i*multiplier/2 +ii][j*multiplier/2 +jj] = predMB[ii*(multiplier/2)+jj];
                            }
                        }
                        predMB = predMB + (multiplier*multiplier/4);
                    }
                }
            }
        }
        free(storeYUV);
    }
}

/*!
************************************************************************
* \brief
* Uses the previous reference pic for concealment of reference frames
*
************************************************************************
*/

static void
copy_prev_pic_to_concealed_pic(StorablePicture *picture, ImageParameters *img)
{

    StorablePicture *ref_pic;
    /* get the last ref pic in dpb */
    ref_pic = get_last_ref_pic_from_dpb();

    assert(ref_pic != NULL);

    /* copy all the struc from this to current concealment pic */
    img->conceal_slice_type = P_SLICE;
    copy_to_conceal(ref_pic, picture, img);
}


/*!
************************************************************************
* \brief
* This function conceals a missing reference frame. The routine is called
* based on the difference in frame number. It conceals an IDR frame loss
* based on the sudden decrease in frame number.
*
************************************************************************
*/

void conceal_lost_frames(ImageParameters *img)
{
    int CurrFrameNum;
    int UnusedShortTermFrameNum;
    StorablePicture *picture = NULL;
    int tmp1 = img->delta_pic_order_cnt[0];
    int tmp2 = img->delta_pic_order_cnt[1];

    img->delta_pic_order_cnt[0] = img->delta_pic_order_cnt[1] = 0;

    // printf("A gap in frame number is found, try to fill it.\n");

    if(img->IDR_concealment_flag == 1)
    {
        // Conceals an IDR frame loss. Uses the reference frame in the previous 
        // GOP for concealment.
        UnusedShortTermFrameNum = 0;
        img->last_ref_pic_poc = -img->poc_gap;
        img->earlier_missing_poc = 0;
    }
    else
        UnusedShortTermFrameNum = (img->pre_frame_num + 1) % img->MaxFrameNum;

    CurrFrameNum = img->frame_num;

    while (CurrFrameNum != UnusedShortTermFrameNum)
    {
        picture = alloc_storable_picture (FRAME, img->width, img->height, img->width_cr, img->height_cr);

        picture->coded_frame = 1;
        picture->pic_num = UnusedShortTermFrameNum;
        picture->frame_num = UnusedShortTermFrameNum;
        picture->non_existing = 0;
        picture->is_output = 0;
        picture->used_for_reference = 1;
        picture->concealed_pic = 1;

        picture->adaptive_ref_pic_buffering_flag = 0;

        img->frame_num = UnusedShortTermFrameNum;

        picture->top_poc=img->last_ref_pic_poc + img->ref_poc_gap;
        picture->bottom_poc=picture->top_poc;
        picture->frame_poc=picture->top_poc;
        picture->poc=picture->top_poc;
        img->last_ref_pic_poc = picture->poc;

        copy_prev_pic_to_concealed_pic(picture, img);

        //if (UnusedShortTermFrameNum == 0)
        if(img->IDR_concealment_flag == 1)
        {
            picture->slice_type = I_SLICE;
            picture->idr_flag = 1;
            flush_dpb();
            picture->top_poc= 0;
            picture->bottom_poc=picture->top_poc;
            picture->frame_poc=picture->top_poc;
            picture->poc=picture->top_poc;
            img->last_ref_pic_poc = picture->poc;
        }

        store_picture_in_dpb(picture);

        picture=NULL;

        img->pre_frame_num = UnusedShortTermFrameNum;
        UnusedShortTermFrameNum = (UnusedShortTermFrameNum + 1) % img->MaxFrameNum;
    }
    img->delta_pic_order_cnt[0] = tmp1;
    img->delta_pic_order_cnt[1] = tmp2;
    img->frame_num = CurrFrameNum;
}

/*!
************************************************************************
* \brief
* Updates the reference list for motion vector copy concealment for non-
* reference frame loss.
*
************************************************************************
*/

void update_ref_list_for_concealment()
{
    unsigned i, j;
    for (i=0, j=0; i<dpb.used_size; i++)
    {
        if (dpb.fs[i]->concealment_reference)
        {
            dpb.fs_ref[j++]=dpb.fs[i];
        }
    }

    dpb.ref_frames_in_buffer = active_pps->num_ref_idx_l0_active_minus1;
}

/*!
************************************************************************
* \brief
*    Initialize the list based on the B frame or non reference 'p' frame 
*    to be concealed. The function initialize listX[0] and list 1 depending 
*    on current picture type
*
************************************************************************
*/
void init_lists_for_non_reference_loss(int currSliceType, PictureStructure currPicStructure)

⌨️ 快捷键说明

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