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

📄 mbuffer.c

📁 包含了从MPEG4的视频解码到H.264的视频编码部分的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
***********************************************************************
* 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(struct inp_par *inp, ImageParameters *img)
{
  int i;

  img->buf_cycle = inp->buf_cycle+1;

  if (img->structure != FRAME)
  {
    img->height *= 2;
    img->height_cr *= 2;
  }

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

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

  for (i=0;i<img->buf_cycle;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<img->buf_cycle;i++)
  {
    get_mem2D(&(frm->picbuf_short[i]->mref), img->height, img->width);
    get_mem3D(&(frm->picbuf_short[i]->mcef), 2, img->height_cr, img->width_cr);

    frm->picbuf_short[i]->picID = -1;
    frm->picbuf_short[i]->lt_picID = -1;
    frm->picbuf_short[i]->used = 0;
  }


  frm->short_size=img->buf_cycle;
  frm->short_used=0;
  frm->long_size=0;
  frm->long_used=0;

//field buffers
  img->buf_cycle=2*img->buf_cycle;
  if ((fld=(FrameBuffer*)calloc(1,sizeof (FrameBuffer)))==NULL) no_mem_exit("init_field_buffers: fld");

  if ((fld->picbuf_short=(Frame**)calloc(img->buf_cycle,sizeof (Frame*)))==NULL) no_mem_exit("init_field_buffers: fld->picbuf_short");

  for (i=0;i<img->buf_cycle;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<img->buf_cycle;i++)
  {
    get_mem2D(&(fld->picbuf_short[i]->mref), img->height/2, img->width);
    get_mem3D(&(fld->picbuf_short[i]->mcef), 2, img->height_cr/2, img->width_cr);
  }

//  bufsize=img->buf_cycle+1;

  fld->short_size=img->buf_cycle;
  fld->short_used=0;
  fld->long_size=0;
  fld->long_used=0;

  if (img->structure != FRAME)
  {
    img->height /= 2;
    img->height_cr /= 2;
  }
    img->buf_cycle = inp->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(struct inp_par *inp, ImageParameters *img)
{
  int i;

  img->buf_cycle = inp->buf_cycle+1;

  for (i=0;i<img->buf_cycle;i++)
  {
    free_mem2D(frm->picbuf_short[i]->mref);
    free_mem3D(frm->picbuf_short[i]->mcef,2);
//    free(frm->picbuf_short[i]->Refbuf11);
  }


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

  free (frm->picbuf_short);		//Access violation here may mean input file is empty

  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);
    } 
    for (i=0;i<frm->long_size;i++)
      free (frm->picbuf_long[i]);

  }

  free (frm);

  for (i=0;i<2 * img->buf_cycle;i++)
  {
    free_mem2D(fld->picbuf_short[i]->mref);
    free_mem3D(fld->picbuf_short[i]->mcef,2);
//    free(fld->picbuf_short[i]->Refbuf11);
  }


  for (i=0;i<2*img->buf_cycle;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);
    } 
    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;

  printf ("init long term buffer size = %d\n",size);

  /* 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_frame_buffers: 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]);
    }

    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, img->width);
      get_mem3D(&(fb->picbuf_long[i]->mcef), 2, img->height_cr, img->width_cr);
    }

    free (pb);

  }

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

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

/*!
 ************************************************************************
 * \brief
 *    assign a long term index to a short term picture
 *
 * \param shortID
 *    short term ID
 *
 * \param longID
 *    long term ID
 *    
 ************************************************************************
 */
void assign_long_term_id(int shortID, int longID, ImageParameters *img)
{
  int i, uv;
  int idx=-1;
  int j,id, idx1;
  Frame *f;

  printf ("assign long term long term id %d to short term id %d \n",longID, shortID);

  // try to find short term picture in buffer
  for (i=0;i<fb->short_used;i++)
  {
    if (fb->picbuf_short[i]->picID==shortID)
      idx=i;
  }

  // short term picture not found: error
  if (idx==-1)
    error ("Trying to assign a long term ID to a not existent short term picture",400);

  // return if reassignment (no error)
  if (fb->picbuf_short[idx]->lt_picID==longID)
    return;

  // copy the frame to the long term buffer
  // this may not be the best solution, but avoids messing up pointers

  /* delete frames with same long term ID */
  remove_long_term(longID);


  /* cycle frame buffer */
  f=fb->picbuf_long[fb->long_size-1];

  for (i=fb->long_size-2;i>=0;i--)
    fb->picbuf_long[i+1]=fb->picbuf_long[i];

  fb->picbuf_long[0]=f;


  fb->picbuf_long[0]->used=1;
  fb->picbuf_long[0]->picID=shortID;
  fb->picbuf_long[0]->lt_picID=longID;

  fb->picbuf_short[idx]->lt_picID=longID;

  (fb->long_used)++;
  if (fb->long_used>fb->long_size)
    fb->long_used=fb->long_size;
  
  memcpy (fb->picbuf_long[0]->mref[0], fb->picbuf_short[idx]->mref[0], img->width*img->height); 
  
  for (uv=0; uv < 2; uv++)
    memcpy (fb->picbuf_long[0]->mcef[uv][0], fb->picbuf_short[idx]->mcef[uv][0],img->width_cr*img->height_cr); 

  // sort the long term buffer by lt_IDs
  // Note: Just a  simple bubble sort implementation. The buffer is not very large
  //       so this should not take too much time. Feel free to implement something better.

  for (i=0,idx1=0;i<fb->long_used-1;i++)
  {
    id=fb->picbuf_long[i]->lt_picID;

    for (j=i+1;j<fb->long_used;j++)
    {
      if (fb->picbuf_long[j]->lt_picID<id) 
      {
        id=fb->picbuf_long[j]->lt_picID;
        idx1=j;
      }
    }
    if (id<fb->picbuf_long[i]->lt_picID)
    {
      f=fb->picbuf_long[i];
      fb->picbuf_long[i]=fb->picbuf_long[idx1];
      fb->picbuf_long[idx1]=f;
    }
  }
  
}

/*!
 ************************************************************************
 * \brief
 *    remove long term frame from buffer
 *
 * \param longID
 *    long term ID
 *    
 ************************************************************************
 */
void remove_long_term(int longID)
{
  int i,j;
  Frame *f;


  for (i=0;i<fb->long_used;i++)
  {
    if ((fb->picbuf_long[i]->lt_picID)==longID)
    {
      printf("removing long term ID %d\n",longID);

      fb->picbuf_long[i]->used=0;
      fb->picbuf_long[i]->lt_picID=-1;
      fb->picbuf_long[i]->picID=-1;
      
      fb->long_used--;

      if (i<fb->long_used) 
      {
        f=fb->picbuf_long[i];
        for (j=i;j<fb->long_used;j++)
          fb->picbuf_long[j]=fb->picbuf_long[j+1];
        fb->picbuf_long[fb->long_used]=f;
      } 
    }
  }
}

/*!
 ************************************************************************
 * \brief
 *    remove short term frame from buffer
 *
 * \param shortID
 *    short term ID
 *    
 ************************************************************************
 */
void remove_short_term(int shortID)
{
  int i,j;
  Frame *f;


  for (i=0;i<fb->short_used;i++)
  {
    if ((fb->picbuf_short[i]->picID)==shortID)
    {
      fb->picbuf_short[i]->used=0;
      fb->picbuf_short[i]->lt_picID=-1;
      fb->picbuf_short[i]->picID=-1;
      
      fb->short_used--;

      // Tian Dong: June 15, 2002
      // Use short_size to replace short_used.
      if (i<fb->short_size) 
      {
        f=fb->picbuf_short[i];
        for (j=i;j<fb->short_size-1;j++)
          fb->picbuf_short[j]=fb->picbuf_short[j+1];
        fb->picbuf_short[fb->short_size-1]=f;
      }
      // old lines:
/*      if (i<fb->short_used) 
      {
        f=fb->picbuf_short[i];
        for (j=i;j<fb->short_used-1;j++);
          fb->picbuf_short[j]=fb->picbuf_short[j+1];
        fb->picbuf_short[fb->short_used-1]=f;
      } */
    }
  }
}

/*!
 ************************************************************************
 * \brief
 *    (re)-allocate memory for mref pointer structure
 ************************************************************************
 */
void alloc_mref(ImageParameters *img)
{
  if (mref_frm) 
    free (mref_frm);

  if((mref_frm = (byte***)calloc(frm->short_size+frm->long_size,sizeof(byte**))) == NULL)
    no_mem_exit("alloc_mref: mref_frm");

  if (mcef_frm) 
    free (mcef_frm);

⌨️ 快捷键说明

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