📄 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 "elements.h"
#include "image.h"
#include "mbuffer.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;
Slice *currSlice = img->currentSlice;
#if _ERROR_CONCEALMENT_
int received_mb_nr = 0; //to record how many MBs are correctly received in error prone transmission
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
#ifdef WIN32
_ftime (&tstruct1); // start time ms
#else
ftime (&tstruct1); // start time ms
#endif
time( <ime1 ); // start time s
currSlice->next_header = 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, MAX_SLICES_PER_FRAME);
erc_mvperMB = 0;
}
// mark the start of a slice
ercStartSegment(currSlice->start_mb_nr, ((currSlice->start_mb_nr == 0) ? 0 : -1), 0 , erc_errorVar);
// decode main slice information
if ((current_header == SOP || current_header == SOS) && currSlice->ei_flag == 0)
decode_one_slice(img,inp);
#else
// decode main slice information
if (current_header == SOP || current_header == SOS)
decode_one_slice(img,inp);
#endif
#if _ERROR_CONCEALMENT_
if(!currSlice->ei_flag) // slice received
{
ercStopSegment(img->current_mb_nr-1, -1, 0, erc_errorVar); // stop the current slice
ercMarkCurrSegmentOK(currSlice->start_mb_nr, img->width, erc_errorVar);
received_mb_nr += img->current_mb_nr - currSlice->start_mb_nr;
}
else // at least one slice lost
{
ercStopSegment(currSlice->last_mb_nr, -1, 0, erc_errorVar);
ercMarkCurrSegmentLost(currSlice->start_mb_nr, img->width, erc_errorVar);
received_mb_nr += (currSlice->last_mb_nr + 1) - currSlice->start_mb_nr;
//! Should never happen, but to be sure
//! Changed TO 12.11.2001
if(received_mb_nr > img->max_mb_nr)
received_mb_nr = img->max_mb_nr;
img->current_mb_nr = currSlice->last_mb_nr + 1;
}
#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];
/* call the right error concealment function depending on the frame type.
here it is simulated that only the first frame is Intra, the rest is Inter. */
erc_mvperMB /= received_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
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 modolo_flag = 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) && !modolo_flag)
{
modulo_ctr++;
modolo_flag = 1;
}
else
modolo_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) && !modolo_flag)
{
modulo_ctr_b++;
modolo_flag = 1;
}
else
modolo_flag = 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++)
for (i=0; i < img->width_cr; i++)
imgUV_ref[uv][j][i]=fgetc(p_ref);
img->quad[0]=0;
diff_y=0;
for (i=0; i < img->width; ++i)
{
for (j=0; j < img->height; ++j)
{
diff_y += img->quad[abs(imgY[j][i]-imgY_ref[j][i])];
}
}
// Chroma
diff_u=0;
diff_v=0;
for (i=0; i < img->width_cr; ++i)
{
for (j=0; j < img->height_cr; ++j)
{
diff_u += img->quad[abs(imgUV_ref[0][j][i]-imgUV[0][j][i])];
diff_v += img->quad[abs(imgUV_ref[1][j][i]-imgUV[1][j][i])];
}
}
// Collecting SNR statistics
if (diff_y != 0)
{
snr->snr_y=(float)(10*log10(65025*(float)(img->width)*(img->height)/(float)diff_y)); // luma snr for current frame
}
if (diff_u != 0)
{
snr->snr_u=(float)(10*log10(65025*(float)(img->width)*(img->height)/(float)(4*diff_u))); // chroma snr for current frame
snr->snr_v=(float)(10*log10(65025*(float)(img->width)*(img->height)/(float)(4*diff_v))); // chroma snr for current frame
}
if (img->number == 0) // first
{
snr->snr_y1=(float)(10*log10(65025*(float)(img->width)*(img->height)/(float)diff_y)); // keep luma snr for first frame
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -