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

📄 mbuffer.c

📁 包含了从MPEG4的视频解码到H.264的视频编码部分的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
***********************************************************************
* COPYRIGHT AND WARRANTY INFORMATION
*
* Copyright 2001, International Telecommunications Union, Geneva
*
* DISCLAIMER OF WARRANTY
*
* These software programs are available to the user without any
* license fee or royalty on an "as is" basis. The ITU disclaims
* any and all warranties, whether express, implied, or
* statutory, including any implied warranties of merchantability
* or of fitness for a particular purpose.  In no event shall the
* contributor or the ITU be liable for any incidental, punitive, or
* consequential damages of any kind whatsoever arising from the
* use of these programs.
*
* This disclaimer of warranty extends to the user of these programs
* and user's customers, employees, agents, transferees, successors,
* and assigns.
*
* The ITU does not represent or warrant that the programs furnished
* hereunder are free of infringement of any third-party patents.
* Commercial implementations of ITU-T Recommendations, including
* shareware, may be subject to royalty fees to patent holders.
* Information regarding the ITU-T patent policy is available from
* the ITU Web site at http://www.itu.int.
*
* THIS IS NOT A GRANT OF PATENT RIGHTS - SEE THE ITU-T PATENT POLICY.
************************************************************************
*/

/*!
 ***********************************************************************
 *  \file
 *      mbuffer.c
 *
 *  \brief
 *      Frame buffer functions
 *
 *  \author
 *      Main contributors (see contributors.h for copyright, address and affiliation details)
 *      - Karsten S黨ring          <suehring@hhi.de>
 ***********************************************************************
 */

#include <stdlib.h>
#include <memory.h>
#include <assert.h>

#include "global.h"
#include "mbuffer.h"
#include "memalloc.h"

/*!
 ************************************************************************
 * \brief
 *    allocate memory for frame buffer
 *
 * \par Input:
 *    Input Parameters struct inp_par *inp, Image Parameters struct img_par *img
 *
 ************************************************************************
 */
void init_frame_buffers(InputParameters *inp, ImageParameters *img)
{
  int i;
  int bufsize=img->buf_cycle+1;    // Tian: PLUS1, June 6, 2002

  // Tian: the following line should be deleted! June 6, 2002
//  if ((fb=(FrameBuffer*)calloc(1,sizeof (FrameBuffer)))==NULL) no_mem_exit("init_frame_buffers: fb");

//frame buffers
  if ((frm=(FrameBuffer*)calloc(1,sizeof (FrameBuffer)))==NULL) no_mem_exit("init_frame_buffers: frm");

  if ((frm->picbuf_short=(Frame**)calloc(bufsize,sizeof (Frame*)))==NULL) no_mem_exit("init_frame_buffers: frm->picbuf_short");

  for (i=0;i<bufsize;i++)
    if ((frm->picbuf_short[i]=(Frame*)calloc(1,sizeof (Frame)))==NULL) no_mem_exit("init_frame_buffers: frm->picbuf_short");

  for (i=0;i<bufsize;i++)
  {
    get_mem2D(&(frm->picbuf_short[i]->mref), (img->height+2*IMG_PAD_SIZE)*4, (img->width+2*IMG_PAD_SIZE)*4);
    get_mem3Dwithextend(&(frm->picbuf_short[i]->mcef), 2, img->height_cr, img->width_cr,16,16);//zdd
    get_mem2Dwithextend(&(frm->picbuf_short[i]->Refbuf11),img->height,img->width,16,16);

    if (input->WeightedPrediction || input->WeightedBiprediction)
    {
      get_mem2D(&(frm->picbuf_short[i]->mref_w), (img->height+2*IMG_PAD_SIZE)*4, (img->width+2*IMG_PAD_SIZE)*4);

      if (NULL == (frm->picbuf_short[i]->Refbuf11_w = malloc ((img->width * img->height) * sizeof (pel_t))))
        no_mem_exit ("init_frame_buffers: Refbuf11_w");
    }
  }
  get_mem2Dwithextend(&tmpImgY, img->height, img->width,16,16);


  if (input->WeightedPrediction > 0 || input->WeightedBiprediction > 0)
  {
    get_mem3Dint(&wp_weight, 2, MAX_REFERENCE_PICTURES, 3);
    get_mem3Dint(&wp_offset, 2, MAX_REFERENCE_PICTURES, 3);
    get_mem4Dint(&wbp_weight, 2, MAX_REFERENCE_PICTURES, MAX_REFERENCE_PICTURES, 3);

    get_mem2Dint(&weight,  MAX_REFERENCE_PICTURES, 3);
    get_mem2Dint(&offset, MAX_REFERENCE_PICTURES, 3);
  }

  frm->short_size=bufsize;
  frm->short_used=0;
  frm->long_size=0;
  frm->long_used=0;

//field buffers
  if(input->InterlaceCodingOption != FRAME_CODING)
  {
    bufsize *=2;
    if ((fld=(FrameBuffer*)calloc(1,sizeof (FrameBuffer)))==NULL) no_mem_exit("init_field_buffers: fld");
    
    if ((fld->picbuf_short=(Frame**)calloc(bufsize,sizeof (Frame*)))==NULL) no_mem_exit("init_field_buffers: fld->picbuf_short");
    
    for (i=0;i<bufsize;i++)
      if ((fld->picbuf_short[i]=(Frame*)calloc(1,sizeof (Frame)))==NULL) no_mem_exit("init_field_buffers: fld->picbuf_short");
      
    for (i=0;i<bufsize;i++)
    {
      get_mem2D(&(fld->picbuf_short[i]->mref), (img->height/2+2*IMG_PAD_SIZE)*4, (img->width+2*IMG_PAD_SIZE)*4);
      get_mem3D(&(fld->picbuf_short[i]->mcef), 2, img->height_cr/2, img->width_cr);
      if (NULL == (fld->picbuf_short[i]->Refbuf11 = malloc ((img->width * img->height / 2) * sizeof (pel_t))))
        no_mem_exit ("init_field_buffers: Refbuf11");
      if (input->WeightedPrediction || input->WeightedBiprediction)
      {
        get_mem2D(&(fld->picbuf_short[i]->mref_w), (img->height/2+2*IMG_PAD_SIZE)*4, (img->width+2*IMG_PAD_SIZE)*4);
        if (NULL == (fld->picbuf_short[i]->Refbuf11_w = malloc ((img->width * img->height / 2) * sizeof (pel_t))))
          no_mem_exit ("init_field_buffers: Refbuf11_w");
      }
    }
    
    fld->short_size=bufsize;
    fld->short_used=0;
    fld->long_size=0;
    fld->long_used=0;
    
    if(input->InterlaceCodingOption >= MB_CODING)
    {
      if ((mref_mbfld = (byte***)calloc(fld->short_size+fld->long_size,sizeof(byte**))) == NULL)
        no_mem_exit("alloc_mref: mref_mbfld");
      for(i=0;i<bufsize;i++)
        get_mem2D(&mref_mbfld[i], (img->height/2+2*IMG_PAD_SIZE)*4, (img->width+2*IMG_PAD_SIZE)*4);
      if (input->WeightedPrediction || input->WeightedBiprediction)
      {
        if ((mref_mbfld_w = (byte***)calloc(fld->short_size+fld->long_size,sizeof(byte**))) == NULL)
          no_mem_exit("alloc_mref: mref_mbfld_w");
        for(i=0;i<bufsize;i++)
          get_mem2D(&mref_mbfld_w[i], (img->height/2+2*IMG_PAD_SIZE)*4, (img->width+2*IMG_PAD_SIZE)*4);
      }
    }
    //  bufsize=img->buf_cycle+1;
  }
}

/*!
 ************************************************************************
 * \brief
 *    free frame buffer memory
 *
 * \par Input:
 *    Input Parameters struct inp_par *inp, Image Parameters struct img_par *img
 *
 ************************************************************************
 */
void free_frame_buffers(InputParameters *inp, ImageParameters *img)
{
  int i;

  for (i=0;i<frm->short_size;i++)
  {
    free_mem2D(frm->picbuf_short[i]->mref);
   	free_mem3Dwithextend(frm->picbuf_short[i]->mcef,2,img->width_cr,16,16);//zdd
	free_mem2Dwithextend(frm->picbuf_short[i]->Refbuf11,img->width,16,16);//zdd
 
    if (input->WeightedPrediction || input->WeightedBiprediction)
    {
      free_mem2D(frm->picbuf_short[i]->mref_w);
      free(frm->picbuf_short[i]->Refbuf11_w);
    }
  }


  for (i=0;i<frm->short_size;i++)
    free (frm->picbuf_short[i]);

  free (frm->picbuf_short);

  if (frm->picbuf_long)
  {
    for (i=0;i<frm->long_size;i++)
    {
      free_mem2D(frm->picbuf_long[i]->mref);
      free_mem3D(frm->picbuf_long[i]->mcef,2);
      free(frm->picbuf_long[i]->Refbuf11);
      if (input->WeightedPrediction || input->WeightedBiprediction)
      {
        free_mem2D(frm->picbuf_long[i]->mref_w);
        free(frm->picbuf_long[i]->Refbuf11_w);
      }
    } 
    for (i=0;i<frm->long_size;i++)
      free (frm->picbuf_long[i]);

  }
  free_mem2Dwithextend(tmpImgY, img->width,16,16);
  free (frm);

  if(input->InterlaceCodingOption != FRAME_CODING)
  {
    for (i=0;i<fld->short_size;i++)
    {
      free_mem2D(fld->picbuf_short[i]->mref);
      free_mem3D(fld->picbuf_short[i]->mcef,2);
      free(fld->picbuf_short[i]->Refbuf11);
      if (input->WeightedPrediction || input->WeightedBiprediction)
      {
        free_mem2D(fld->picbuf_short[i]->mref_w);
        free(fld->picbuf_short[i]->Refbuf11_w);
      }
    }


    for (i=0;i<fld->short_size;i++)
      free (fld->picbuf_short[i]);

    free (fld->picbuf_short);

    if (fld->picbuf_long)
    {
      for (i=0;i<fld->long_size;i++)
      {
        free_mem2D(fld->picbuf_long[i]->mref);
        free_mem3D(fld->picbuf_long[i]->mcef,2);
        free(fld->picbuf_long[i]->Refbuf11);
        if (input->WeightedPrediction || input->WeightedBiprediction)
        {
          free_mem2D(fld->picbuf_long[i]->mref_w);
          free(fld->picbuf_long[i]->Refbuf11_w);
        }
      } 
      for (i=0;i<fld->long_size;i++)
        free (fld->picbuf_long[i]);
    }

    free (fld);
  }
}

/*!
 ************************************************************************
 * \brief
 *    allocate or reallocate long term buffer memory
 *
 * \param size
 *    number of frames
 *
 ************************************************************************
 */
void init_long_term_buffer(int size, ImageParameters *img)
{
  Frame **pb;
  int i;

  /* nothing to do if size unchanged */
  if (fb->long_size==size) return;

  if (fb->long_size>size)
  {
    /* shrink size */

    pb=fb->picbuf_long;
    if ((fb->picbuf_long=(Frame**)calloc(size,sizeof (Frame*)))==NULL) no_mem_exit("init_long_term_buffer: fb->picbuf_long");

    for (i=0;i<size;i++)
      fb->picbuf_long[i]=pb[i];

    for (i=size;i<fb->long_size;i++)
    {
      free_mem2D(pb[i]->mref);
      free_mem3D(pb[i]->mcef, 2);
      free(pb[i]->Refbuf11);
      if (input->WeightedPrediction || input->WeightedBiprediction)
      {
        free_mem2D(pb[i]->mref_w);
        free(pb[i]->Refbuf11_w);
      }
      free (pb[i]);
    }

    free (pb);

  } else
  {
    /* grow size */

    pb=fb->picbuf_long;

    if ((fb->picbuf_long=(Frame**)calloc(size,sizeof (Frame*)))==NULL) no_mem_exit("init_frame_buffers: fb->picbuf_long");

    for (i=0;i<fb->long_size;i++)
      fb->picbuf_long[i]=pb[i];

    for (i=fb->long_size;i<size;i++)
    {
      if ((fb->picbuf_long[i]=(Frame*)calloc(1,sizeof (Frame)))==NULL) no_mem_exit("init_frame_buffers: fb->picbuf_long");
      get_mem2D(&(fb->picbuf_long[i]->mref), (img->height+2*IMG_PAD_SIZE)*4, (img->width+2*IMG_PAD_SIZE)*4);
      if (input->WeightedPrediction || input->WeightedBiprediction)
        get_mem2D(&(fb->picbuf_long[i]->mref_w), (img->height+2*IMG_PAD_SIZE)*4, (img->width+2*IMG_PAD_SIZE)*4);
      get_mem3D(&(fb->picbuf_long[i]->mcef), 2, img->height_cr, img->width_cr);
      get_mem2Dwithextend(&(fb->picbuf_long[i]->Refbuf11),img->height,img->width,16,16);
      if (input->WeightedPrediction || input->WeightedBiprediction)
        if (NULL == (fb->picbuf_long[i]->Refbuf11_w = malloc ((img->width * img->height) * sizeof (pel_t))))
          no_mem_exit ("init_long_term_buffer: Refbuf11_w");
    }

    free (pb);

  }

  // finally set the size
  fb->long_size=size;

  // and correct the size of mref/mcef, Refbuf11
  alloc_mref(img);
  alloc_Refbuf(img);
}

/*!
 ************************************************************************
 * \brief
 *    assign a long term index to a short term picture
 *
 * \param shortID
 *    short term ID
 *
 * \param longID
 *    long term ID
 *    
 ************************************************************************
 */

⌨️ 快捷键说明

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