📄 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 <wangy@cs.tut.fi>
* - 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 "global.h"
#include "errorconcealment.h"
#include "image.h"
#include "mbuffer.h"
#include "decodeiff.h"
#if _ERROR_CONCEALMENT_
#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;
#endif
extern int getBitsPos();
extern int setBitsPos(int);
#ifdef _ADAPT_LAST_GROUP_
int *last_P_no;
#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;
int i;
Slice *currSlice = img->currentSlice;
extern FILE* bits;
#if _ERROR_CONCEALMENT_
int ercStartMB;
int ercSegment;
frame recfr;
#endif
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
if ( inp->of_mode == PAR_OF_IFF ) // for Interim File Format
rdPictureInfo( bits );
#ifdef WIN32
_ftime (&tstruct1); // start time ms
#else
ftime (&tstruct1); // start time ms
#endif
time( <ime1 ); // start time s
currSlice->next_header = 0;
currSlice->next_eiflag = 0;
while (currSlice->next_header != EOS && currSlice->next_header != SOP)
{
// set the corresponding read functions
start_slice(img, inp);
// read new slice
current_header = read_new_slice(img, inp);
if (current_header == EOS)
return EOS;
if (inp->symbol_mode == CABAC)
{
init_contexts_MotionInfo(img, currSlice->mot_ctx, 1);
init_contexts_TextureInfo(img,currSlice->tex_ctx, 1);
}
// init new frame
if (current_header == SOP)
init_frame(img, inp);
// do reference frame buffer reordering
reorder_mref(img);
#if _ERROR_CONCEALMENT_
if (current_header == SOP)
{
if (img->number == 0)
ercInit(img->width, img->height, 1);
// reset all variables of the error concealmnet instance before decoding of every frame.
// here the third parameter should, if perfectly, be equal to the number of slices per frame.
// using little value is ok, the code will alloc more memory if the slice number is larger
ercReset(erc_errorVar, img->max_mb_nr, img->max_mb_nr, img->width);
erc_mvperMB = 0;
}
// decode main slice information
if ((current_header == SOP || current_header == SOS) && currSlice->ei_flag == 0)
decode_one_slice(img,inp);
// setMB-Nr in case this slice was lost
if(currSlice->ei_flag)
img->current_mb_nr = currSlice->last_mb_nr + 1;
#else
// decode main slice information
if (current_header == SOP || current_header == SOS)
decode_one_slice(img,inp);
#endif
if(currSlice->next_eiflag && img->current_mb_nr != img->max_mb_nr)
currSlice->next_header = SOS;
img->current_slice_nr++;
}
#if _ERROR_CONCEALMENT_
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;
//! 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, 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 == INTRA_IMG) // I-frame
ercConcealIntraFrame(&recfr, img->width, img->height, erc_errorVar);
else
ercConcealInterFrame(&recfr, erc_object_list, img->width, img->height, erc_errorVar);
#endif
DeblockFrame( img, imgY, imgUV ) ;
if (p_ref)
find_snr(snr,img,p_ref,inp->postfilter); // 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 == INTRA_IMG) // 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 == INTER_IMG_1 || img->type == INTER_IMG_MULT) // 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_IMG_1 || img->type == SP_IMG_MULT) // 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 // 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 == INTRA_IMG || img->type == INTER_IMG_1 || img->type == INTER_IMG_MULT) // I or P pictures
copy_Pframe(img, inp->postfilter); // imgY-->imgY_prev, imgUV-->imgUV_prev
else if(img->type == SP_IMG_1 || img->type == SP_IMG_MULT) // SP pictures
copy_Pframe(img, inp->postfilter); // imgY-->imgY_prev, imgUV-->imgUV_prev
else // B pictures
write_frame(img,inp->postfilter,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 == INTRA_IMG)
img->type = INTER_IMG_1;
//! End TO 19.11.2001
if(img->type <= INTRA_IMG || img->type >= SP_IMG_1) // I or P pictures
img->number++;
else
Bframe_ctr++; // B pictures
exit_frame(img, inp);
img->current_mb_nr = 0;
img->current_slice_nr = 0;
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 postfilter) //!< postfilterin on (=1) or off (=1)
{
int i,j;
int diff_y,diff_u,diff_v;
int uv;
int status;
static int modulo_ctr = 0;
static int modulo_ctr_b = 0;
static int modulo_flag = 0;
static int modulo_flag_b = 0;
static int pic_id_old = 0, pic_id_old_b = 0;
Slice *currSlice = img->currentSlice;
#ifndef _ADAPT_LAST_GROUP_
byte diff;
#endif
#ifndef _ADAPT_LAST_GROUP_
if(img->type<=INTRA_IMG || img->type >= SP_IMG_1) // 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.
if (img->type <= INTRA_IMG || img->type >= SP_IMG_1) // I, P
{
if (img->number > 0)
{
if ((currSlice->picture_id < pic_id_old) && !modulo_flag)//module_flag is used for P frames
{
modulo_ctr++;
modulo_flag = 1;
}
else
modulo_flag = 0;
frame_no = currSlice->picture_id + (256*modulo_ctr);
pic_id_old = currSlice->picture_id;
}
else
frame_no = 0;
}
else // B
{
if ((currSlice->picture_id < pic_id_old_b) && !modulo_flag_b ) //modulo_flag_b is used for B frames
{
modulo_ctr_b++;
modulo_flag_b = 1;
}
else
modulo_flag_b = 0;
frame_no = currSlice->picture_id + (256*modulo_ctr_b);
pic_id_old_b = currSlice->picture_id;
}
#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++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -