📄 image.c
字号:
/*
***********************************************************************
* 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 image.c
*
* \brief
* Decode a Slice
*
* \author
* Main contributors (see contributors.h for copyright, address and affiliation details)
* - Inge Lille-Lang鴜 <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>
***********************************************************************
*/
#include "contributors.h"
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <sys/timeb.h>
#include <string.h>
#include <assert.h>
#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 "context_ini.h"
#include "erc_api.h"
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;
#ifdef _ADAPT_LAST_GROUP_
int *last_P_no;
int *last_P_no_frm;
int *last_P_no_fld;
#endif
/*!
***********************************************************************
* \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 ercStartMB;
int ercSegment;
frame recfr;
time_t ltime1; // for time measurement
time_t ltime2;
#ifdef WIN32
struct _timeb tstruct1;
struct _timeb tstruct2;
#else
struct timeb tstruct1;
struct timeb tstruct2;
#endif
int tmp_time; // time used by decoding the last frame
#ifdef WIN32
_ftime (&tstruct1); // start time ms
#else
ftime (&tstruct1); // start time ms
#endif
time( <ime1 ); // start time s
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
currSlice->next_eiflag = 0;
img->num_dec_mb = 0;
while ((currSlice->next_header != EOS && currSlice->next_header != SOP))
{
// printf ("decode_one_frame: currSlice->next_header %d\n", currSlice->next_header);
current_header = read_new_slice();
// img->current_mb_nr = img->map_mb_nr = currSlice->start_mb_nr;//GB
// printf ("Now processing mb %d\n", img->current_mb_nr);
if (current_header == EOS)
return EOS;
if (img->structure == FRAME)
decode_frame_slice(img, inp, current_header);
else
decode_field_slice(img, inp, current_header);
img->current_slice_nr++;
}
//deblocking for frame or top/bottom field
DeblockFrame( img, imgY, imgUV ) ;
if(img->structure != FRAME) //if the previous pict is top or bottom field,
{
img->current_slice_nr = 0;
currSlice->next_header = -8889;
currSlice->next_eiflag = 0;
img->num_dec_mb = 0;
while ((currSlice->next_header != EOS && currSlice->next_header != SOP))
{
// read new slice
current_header = read_new_slice();
// img->current_mb_nr = currSlice->start_mb_nr;
if (current_header == EOS)
return EOS;
decode_field_slice(img, inp, current_header);
img->current_slice_nr++;
}
//deblocking bottom/top
DeblockFrame( img, imgY, imgUV ) ;
}
recfr.yptr = &imgY[0][0];
recfr.uptr = &imgUV[0][0][0];
recfr.vptr = &imgUV[1][0][0];
//! this is always true at the beginning of a frame
ercStartMB = 0;
ercSegment = 0;
/* !KS: This needs to be fixed for multiple slices
//! mark the start of the first segment
ercStartSegment(0, ercSegment, 0 , erc_errorVar);
//! generate the segments according to the macroblock map
for(i = 1; i<img->max_mb_nr; i++)
{
if(img->mb_data[i].ei_flag != img->mb_data[i-1].ei_flag)
{
ercStopSegment(i-1, ercSegment, 0, erc_errorVar); //! stop current segment
//! mark current segment as lost or OK
if(img->mb_data[i-1].ei_flag)
ercMarkCurrSegmentLost(img->width, erc_errorVar);
else
ercMarkCurrSegmentOK(img->width, erc_errorVar);
ercSegment++; //! next segment
ercStartSegment(i, ercSegment, 0 , erc_errorVar); //! start new segment
ercStartMB = i;//! save start MB for this segment
}
}
//! mark end of the last segent
ercStopSegment(img->max_mb_nr-1, ercSegment, 0, erc_errorVar);
if(img->mb_data[i-1].ei_flag)
ercMarkCurrSegmentLost(img->width, erc_errorVar);
else
ercMarkCurrSegmentOK(img->width, erc_errorVar);
//! call the right error concealment function depending on the frame type.
erc_mvperMB /= img->max_mb_nr;
erc_img = img;
if(img->type == I_SLICE || img->type == SI_SLICE) // I-frame
ercConcealIntraFrame(&recfr, img->width, img->height, erc_errorVar);
else
ercConcealInterFrame(&recfr, erc_object_list, img->width, img->height, erc_errorVar);
*/
if (img->structure == FRAME) // buffer mgt. for frame mode
frame_postprocessing(img, inp);
else
field_postprocessing(img, inp); // reset all interlaced variables
post_poc( img ); // POC200301
if((img->type==B_SLICE) && !img->disposable_flag)
copy_stored_B_motion_info(img);
store_field_MV(img);
if (p_ref)
find_snr(snr,img,p_ref); // if ref sequence exist
#ifdef WIN32
_ftime (&tstruct2); // end time ms
#else
ftime (&tstruct2); // end time ms
#endif
time( <ime2 ); // end time sec
tmp_time=(ltime2*1000+tstruct2.millitm) - (ltime1*1000+tstruct1.millitm);
tot_time=tot_time + tmp_time;
if(img->type == I_SLICE) // I picture
fprintf(stdout,"%3d(I) %3d %5d %7.4f %7.4f %7.4f %5d\n",
frame_no, img->tr, img->qp,snr->snr_y,snr->snr_u,snr->snr_v,tmp_time);
else if(img->type == P_SLICE) // P pictures
fprintf(stdout,"%3d(P) %3d %5d %7.4f %7.4f %7.4f %5d\n",
frame_no, img->tr, img->qp,snr->snr_y,snr->snr_u,snr->snr_v,tmp_time);
else if(img->type == SP_SLICE) // SP pictures
fprintf(stdout,"%3d(SP) %3d %5d %7.4f %7.4f %7.4f %5d\n",
frame_no, img->tr, img->qp,snr->snr_y,snr->snr_u,snr->snr_v,tmp_time);
else if (img->type == SI_SLICE)
fprintf(stdout,"%3d(SI) %3d %5d %7.4f %7.4f %7.4f %5d\n",
frame_no, img->tr, img->qp,snr->snr_y,snr->snr_u,snr->snr_v,tmp_time);
else if(!img->disposable_flag) // stored B pictures
fprintf(stdout,"%3d(BS) %3d %5d %7.4f %7.4f %7.4f %5d\n",
frame_no, img->tr, img->qp,snr->snr_y,snr->snr_u,snr->snr_v,tmp_time);
else // B pictures
fprintf(stdout,"%3d(B) %3d %5d %7.4f %7.4f %7.4f %5d\n",
frame_no, img->tr, img->qp,snr->snr_y,snr->snr_u,snr->snr_v,tmp_time);
fflush(stdout);
if(img->type == I_SLICE || img->type == P_SLICE) // I or P pictures
copy_Pframe(img); // imgY-->imgY_prev, imgUV-->imgUV_prev
else if(img->type == SP_SLICE || img->type == SI_SLICE) // SP pictures
copy_Pframe(img); // imgY-->imgY_prev, imgUV-->imgUV_prev
else if(!img->disposable_flag) // stored B pictures
copy_Pframe(img); // imgY-->imgY_prev, imgUV-->imgUV_prev
else // B pictures
write_frame(img,p_out); // write image to output YUV file
//! TO 19.11.2001 Known Problem: for init_frame we have to know the picture type of the actual frame
//! in case the first slice of the P-Frame following the I-Frame was lost we decode this P-Frame but
//! do not write it because it was assumed to be an I-Frame in init_frame. So we force the decoder to
//! guess the right picture type. This is a hack a should be removed by the time there is a clean
//! solution where we do not have to know the picture type for the function init_frame.
if(img->type == I_SLICE)
img->type = P_SLICE;
//! End TO 19.11.2001
if(img->type == I_SLICE || img->type == SI_SLICE || img->type == P_SLICE || !img->disposable_flag) // I or P pictures
img->number++;
else
Bframe_ctr++; // B pictures
exit_frame(img, inp);
if (img->structure != FRAME)
{
img->height /= 2;
img->height_cr /= 2;
}
img->current_mb_nr = -4712; // impossible value for debugging, StW
img->current_slice_nr = 0;
img->last_decoded_pic_id = img->tr; // JVT-D101
return (SOP);
}
/*!
************************************************************************
* \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
struct img_par *img, //!< pointer to image parameters
FILE *p_ref) //!< filestream to reference YUV file
{
int i,j;
int diff_y,diff_u,diff_v;
int uv;
int status;
Slice *currSlice = img->currentSlice;
#ifndef _ADAPT_LAST_GROUP_
byte diff;
#endif
#ifndef _ADAPT_LAST_GROUP_
if(img->type==I_SLICE || img->type==P_SLICE || img->type == SI_SLICE|| !img->disposable_flag) // I, P pictures
frame_no=img->number*P_interval;
else // B pictures
{
diff=nextP_tr-img->tr;
frame_no=(img->number-1)*P_interval-diff;
}
#else
// TO 5.11.2001 We do have some problems finding the correct frame in the original sequence
// if errors appear. In this case the method of using this p_frame_no, nextP_tr, prevP_tr
// variables does not work. So I use the picture_id instead.
// POC200301 The following modifications are done to make the decoder can get right frame_no
// in case of more than one IDR pictures. I have not found any reasons to use something like
// 256*modulo_ctr_xxx.
//calculate frame number
if (img->structure == FRAME)
frame_no = currSlice->picture_id;
else
frame_no = currSlice->picture_id/2;
#endif
rewind(p_ref);
status = fseek (p_ref, frame_no*img->height*img->width*3/2, 0);
if (status != 0)
{
snprintf(errortext, ET_SIZE, "Error in seeking img->tr: %d", img->tr);
error(errortext, 500);
}
for (j=0; j < img->height; j++)
for (i=0; i < img->width; i++)
imgY_ref[j][i]=fgetc(p_ref);
for (uv=0; uv < 2; uv++)
for (j=0; j < img->height_cr ; j++)
for (i=0; i < img->width_cr; i++)
imgUV_ref[uv][j][i]=fgetc(p_ref);
img->quad[0]=0;
diff_y=0;
for (j=0; j < img->height; ++j)
{
for (i=0; i < img->width; ++i)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -