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

📄 image.c

📁 JM 11.0 KTA 2.1 Source Code
💻 C
📖 第 1 页 / 共 5 页
字号:

/*!
***********************************************************************
* \file image.c
*
* \brief
*    Decode a Slice
*
* \author
*    Main contributors (see contributors.h for copyright, address and affiliation details)
*    - Inge Lille-Langoy               <inge.lille-langoy@telenor.com>
*    - Rickard Sjoberg                 <rickard.sjoberg@era.ericsson.se>
*    - Jani Lainema                    <jani.lainema@nokia.com>
*    - Sebastian Purreiter             <sebastian.purreiter@mch.siemens.de>
*    - Byeong-Moon Jeon                <jeonbm@lge.com>
*    - Thomas Wedi                     <wedi@tnt.uni-hannover.de>
*    - Gabi Blaettermann               <blaetter@hhi.de>
*    - Ye-Kui Wang                     <wyk@ieee.org>
*    - Antti Hallapuro                 <antti.hallapuro@nokia.com>
*    - Alexis Tourapis                 <alexismt@ieee.org>
*    - Jill Boyce                      <jill.boyce@thomson.net>
*    - Saurav K Bandyopadhyay          <saurav@ieee.org>
*    - Zhenyu Wu                       <Zhenyu.Wu@thomson.net
*    - Purvin Pandit                   <Purvin.Pandit@thomson.net>
*
***********************************************************************
*/

#include "contributors.h"

#include <math.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#ifdef WIN32
#include <io.h>
#else
#include <unistd.h>
#endif

#include "global.h"
#include "errorconcealment.h"
#include "image.h"
#include "mbuffer.h"
#include "fmo.h"
#include "nalu.h"
#include "parsetcommon.h"
#include "parset.h"
#include "header.h"
#include "rtp.h"
#include "sei.h"
#include "output.h"
#include "biaridecod.h"
#include "mb_access.h"
#include "memalloc.h"
#include "annexb.h"

#include "context_ini.h"
#include "cabac.h"
#include "loopfilter.h"

#include "vlc.h"

#ifdef MV_COMPETITION
#include "mv_competition.h"
extern MV_Competition mv_comp;    // to each possible predictor
#endif


#ifdef ADAPTIVE_FILTER
#include "adaptive_filter.h"
#endif
#include "erc_api.h"

#ifdef USE_POST_FILTER
#include "postprocessing.h"
#endif

#ifdef SWITCHED_FILTERS
#include "switched_filters.h"
#endif

extern objectBuffer_t *erc_object_list;
extern ercVariables_t *erc_errorVar;
extern frame erc_recfr;
extern int erc_mvperMB;
extern struct img_par *erc_img;

//extern FILE *p_out2;

extern StorablePicture **listX[6];
extern ColocatedParams *Co_located;

extern StorablePicture *no_reference_picture;
int non_conforming_stream;

StorablePicture *dec_picture;

OldSliceParams old_slice;

#ifdef USE_INTRA_MDDT
static void InitScanOrderForSlice();
#endif 

void MbAffPostProc()
{
  imgpel temp[16][32];

  imgpel ** imgY  = dec_picture->imgY;
  imgpel ***imgUV = dec_picture->imgUV;

  int i, x, y, x0, y0, uv;
  for (i=0; i<(int)dec_picture->PicSizeInMbs; i+=2)
  {
    if (dec_picture->mb_field[i])
    {
      get_mb_pos(i, &x0, &y0);
      for (y=0; y<(2*MB_BLOCK_SIZE);y++)
        for (x=0; x<MB_BLOCK_SIZE; x++)
          temp[x][y] = imgY[y0+y][x0+x];

      for (y=0; y<MB_BLOCK_SIZE;y++)
        for (x=0; x<MB_BLOCK_SIZE; x++)
        {
          imgY[y0+(2*y)][x0+x]   = temp[x][y];
          imgY[y0+(2*y+1)][x0+x] = temp[x][y+MB_BLOCK_SIZE];
        }

        if (dec_picture->chroma_format_idc != YUV400)
        {
          x0 = x0 / (16/img->mb_cr_size_x);
          y0 = y0 / (16/img->mb_cr_size_y);

          for (uv=0; uv<2; uv++)
          {
            for (y=0; y<(2*img->mb_cr_size_y);y++)
              for (x=0; x<img->mb_cr_size_x; x++)
                temp[x][y] = imgUV[uv][y0+y][x0+x];

            for (y=0; y<img->mb_cr_size_y;y++)
              for (x=0; x<img->mb_cr_size_x; x++)
              {
                imgUV[uv][y0+(2*y)][x0+x]   = temp[x][y];
                imgUV[uv][y0+(2*y+1)][x0+x] = temp[x][y+img->mb_cr_size_y];
              }
          }
        }
    }
  }
}

/*!
***********************************************************************
* \brief
*    decodes one I- or P-frame
*
***********************************************************************
*/

int decode_one_frame(struct img_par *img,struct inp_par *inp, struct snr_par *snr)
{
  int current_header;
  Slice *currSlice = img->currentSlice;
  int i;

  img->current_slice_nr = 0;
  img->current_mb_nr = -4711;     // initialized to an impossible value for debugging -- correct value is taken from slice header
  currSlice->next_header = -8888; // initialized to an impossible value for debugging -- correct value is taken from slice header
  img->num_dec_mb = 0;
  img->newframe = 1;

  while ((currSlice->next_header != EOS && currSlice->next_header != SOP))
  {

    current_header = read_new_slice();

#ifdef MV_COMPETITION
    reinit_MV_Competition();
    init_MV_Competition_mv_previous_tab();
#endif

    // error tracking of primary and redundant slices.
    Error_tracking();

    // If primary and redundant are received and primary is correct, discard the redundant
    // else, primary slice will be replaced with redundant slice.
    if(img->frame_num == previous_frame_num && img->redundant_pic_cnt !=0
      && Is_primary_correct !=0 && current_header != EOS)
    {
      continue;
    }

    // update reference flags and set current ref_flag
    if(!(img->redundant_pic_cnt != 0 && previous_frame_num == img->frame_num))
    {
      for(i=16;i>0;i--)
      {
        ref_flag[i] = ref_flag[i-1];
      }
    }
    ref_flag[0] = img->redundant_pic_cnt==0 ? Is_primary_correct : Is_redundant_correct;
    previous_frame_num = img->frame_num;

    if (current_header == EOS)
    {
      exit_picture();
      return EOS;
    }

    decode_slice(img, inp, current_header);

#ifdef MV_COMPETITION
    if((img->type == B_SLICE) && (mv_comp.mv_competition > 0))
    {   
      if(successive_Bframe > 1)
        Copy_MV_B_frame(dec_picture->mv, dec_picture->ref_idx);
    }
#endif

    img->newframe = 0;
    img->current_slice_nr++;
  }

  exit_picture();

  return (SOP);
}


/*!
************************************************************************
* \brief
*    Convert file read buffer to source picture structure
* \param imgX
*    Pointer to image plane
* \param buf
*    Buffer for file output
* \param size_x
*    horizontal image size in pixel
* \param size_y
*    vertical image size in pixel
* \param symbol_size_in_bytes
*    number of bytes used per pel
************************************************************************
*/
void buf2img (imgpel** imgX, unsigned char* buf, int size_x, int size_y, int symbol_size_in_bytes)
{
  int i,j;

  unsigned short tmp16, ui16;
  unsigned long  tmp32, ui32;

  if (symbol_size_in_bytes> sizeof(imgpel))
  {
    error ("Source picture has higher bit depth than imgpel data type. Please recompile with larger data type for imgpel.", 500);
  }

  if (( sizeof(char) == sizeof (imgpel)) && ( sizeof(char) == symbol_size_in_bytes))
  {
    // imgpel == pixel_in_file == 1 byte -> simple copy
    for(j=0;j<size_y;j++)
      memcpy(imgX[j], buf+j*size_x, size_x);
  }
  else
  {
    // sizeof (imgpel) > sizeof(char)
    if (testEndian())
    {
      // big endian
      switch (symbol_size_in_bytes)
      {
      case 1:
        {
          for(j=0;j<size_y;j++)
            for(i=0;i<size_x;i++)
            {
              imgX[j][i]= buf[i+j*size_x];
            }
            break;
        }
      case 2:
        {
          for(j=0;j<size_y;j++)
            for(i=0;i<size_x;i++)
            {
              memcpy(&tmp16, buf+((i+j*size_x)*2), 2);
              ui16  = (tmp16 >> 8) | ((tmp16&0xFF)<<8);
              imgX[j][i] = (imgpel) ui16;
            }
            break;
        }
      case 4:
        {
          for(j=0;j<size_y;j++)
            for(i=0;i<size_x;i++)
            {
              memcpy(&tmp32, buf+((i+j*size_x)*4), 4);
              ui32  = ((tmp32&0xFF00)<<8) | ((tmp32&0xFF)<<24) | ((tmp32&0xFF0000)>>8) | ((tmp32&0xFF000000)>>24);
              imgX[j][i] = (imgpel) ui32;
            }
        }
      default:
        {
          error ("reading only from formats of 8, 16 or 32 bit allowed on big endian architecture", 500);
          break;
        }
      }

    }
    else
    {
      // little endian
      for (j=0; j < size_y; j++)
        for (i=0; i < size_x; i++)
        {
          imgX[j][i]=0;
          memcpy(&(imgX[j][i]), buf +((i+j*size_x)*symbol_size_in_bytes), symbol_size_in_bytes);
        }

    }
  }
}


/*!
************************************************************************
* \brief
*    Find PSNR for all three components.Compare decoded frame with
*    the original sequence. Read inp->jumpd frames to reflect frame skipping.
************************************************************************
*/
void find_snr(
struct snr_par  *snr,   //!< pointer to snr parameters
  StorablePicture *p,     //!< picture to be compared
  int p_ref)              //!< open reference YUV file
{
  int SubWidthC  [4]= { 1, 2, 2, 1};
  int SubHeightC [4]= { 1, 2, 1, 1};
  int crop_left, crop_right, crop_top, crop_bottom;

  int i,j;
  int64 diff_y,diff_u,diff_v;
  int uv;
  int64  status;
  int symbol_size_in_bytes = img->pic_unit_bitsize_on_disk/8;
  int size_x, size_y;
  int size_x_cr, size_y_cr;
  int64 framesize_in_bytes;
#ifdef  INTERNAL_BIT_DEPTH_INCREASE
  unsigned int max_pix_value_sqd = (img->BitDepthIncrease)? ((1<<(img->bitdepth_luma-img->BitDepthIncrease))-1)*((1<<(img->bitdepth_luma-img->BitDepthIncrease))-1)
    : img->max_imgpel_value * img->max_imgpel_value;
  unsigned int max_pix_value_sqd_uv = (img->BitDepthIncreaseChroma)? ((1<<(img->bitdepth_chroma-img->BitDepthIncreaseChroma))-1)*((1<<(img->bitdepth_chroma-img->BitDepthIncreaseChroma))-1)
    : img->max_imgpel_value_uv * img->max_imgpel_value_uv;
#else
  unsigned int max_pix_value_sqd = img->max_imgpel_value * img->max_imgpel_value;
  unsigned int max_pix_value_sqd_uv = img->max_imgpel_value_uv * img->max_imgpel_value_uv;
#endif
  Boolean rgb_output = (Boolean)(active_sps->vui_seq_parameters.matrix_coefficients==0);
  unsigned char *buf;

  // picture error concealment
  char yuv_types[4][6]= {"4:0:0","4:2:0","4:2:2","4:4:4"};

  // calculate frame number
  int  psnrPOC = active_sps->mb_adaptive_frame_field_flag ? p->poc /(input->poc_scale) : p->poc/(input->poc_scale);

  // cropping for luma
  if (p->frame_cropping_flag)
  {
    crop_left   = SubWidthC[p->chroma_format_idc] * p->frame_cropping_rect_left_offset;
    crop_right  = SubWidthC[p->chroma_format_idc] * p->frame_cropping_rect_right_offset;
    crop_top    = SubHeightC[p->chroma_format_idc]*( 2 - p->frame_mbs_only_flag ) *  p->frame_cropping_rect_top_offset;
    crop_bottom = SubHeightC[p->chroma_format_idc]*( 2 - p->frame_mbs_only_flag ) *   p->frame_cropping_rect_bottom_offset;
  }
  else
  {
    crop_left = crop_right = crop_top = crop_bottom = 0;
  }

  size_x = p->size_x - crop_left - crop_right;
  size_y = p->size_y - crop_top - crop_bottom;

⌨️ 快捷键说明

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