📄 image.c
字号:
/*!
***********************************************************************
* \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>
* - Alexis Tourapis <alexismt@ieee.org>
***********************************************************************
*/
#include "contributors.h"
#include <math.h>
#include <limits.h>
#include <stdlib.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 "output.h"
#include "biaridecod.h"
#include "mb_access.h"
#include "annexb.h"
#include "context_ini.h"
#include "cabac.h"
#include "loopfilter.h"
#include "vlc.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;
//extern FILE *p_out2;
extern StorablePicture **listX[6];
extern ColocatedParams *Co_located;
StorablePicture *dec_picture;
OldSliceParams old_slice;
void MbAffPostProc()
{
byte temp[16][32];
byte ** imgY = dec_picture->imgY;
byte ***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];
}
x0 = x0/2;
y0 = y0/2;
for (uv=0; uv<2; uv++)
{
for (y=0; y<(2*MB_BLOCK_SIZE/2);y++)
for (x=0; x<MB_BLOCK_SIZE/2; x++)
temp[x][y] = imgUV[uv][y0+y][x0+x];
for (y=0; y<MB_BLOCK_SIZE/2;y++)
for (x=0; x<MB_BLOCK_SIZE/2; x++)
{
imgUV[uv][y0+(2*y)][x0+x] = temp[x][y];
imgUV[uv][y0+(2*y+1)][x0+x] = temp[x][y+MB_BLOCK_SIZE/2];
}
}
}
}
}
/*!
***********************************************************************
* \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;
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();
if (current_header == EOS)
{
exit_picture();
return EOS;
}
decode_slice(img, inp, current_header);
img->newframe = 0;
img->current_slice_nr++;
}
exit_picture();
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
StorablePicture *p, //!< picture to be compared
FILE *p_ref) //!< open reference YUV file
{
int i,j;
int diff_y,diff_u,diff_v;
int uv;
int status;
// calculate frame number
int psnrPOC = active_sps->mb_adaptive_frame_field_flag ? p->poc /(input->poc_scale) : p->poc/(3-input->poc_scale);
// int psnrPOC = p->MbaffFrameFlag ? p->poc /(input->poc_scale) : p->poc/(3-input->poc_scale);
// KS: Code below might work better if you have fields and a large (>1) poc offset between them
// int poc_diff=max(1,(p->bottom_poc - p->top_poc));
// int psnrPOC = active_sps->mb_adaptive_frame_field_flag ? p->poc /(input->poc_scale*poc_diff) : p->poc/((3-input->poc_scale)*poc_diff);
if (psnrPOC==0 && img->psnr_number)
img->idr_psnr_number=img->psnr_number + 1;
img->psnr_number=max(img->psnr_number,img->idr_psnr_number+psnrPOC);
frame_no = img->idr_psnr_number+psnrPOC;
rewind(p_ref);
for (i=0; i<frame_no; i++)
{
status = fseek (p_ref, (long) p->size_y* (long) (p->size_x*3/2), SEEK_CUR);
if (status != 0)
{
snprintf(errortext, ET_SIZE, "Error in seeking frame number: %d", frame_no);
fprintf(stderr, "%s", errortext);
return;
// snprintf(errortext, ET_SIZE, "Error in seeking frame number: %d", frame_no);
// error(errortext, 500);
}
}
for (j=0; j < p->size_y; j++)
for (i=0; i < p->size_x; i++)
imgY_ref[j][i]=fgetc(p_ref);
for (uv=0; uv < 2; uv++)
for (j=0; j < p->size_y_cr ; j++)
for (i=0; i < p->size_x_cr; i++)
imgUV_ref[uv][j][i]=fgetc(p_ref);
img->quad[0]=0;
diff_y=0;
for (j=0; j < p->size_y; ++j)
{
for (i=0; i < p->size_x; ++i)
{
diff_y += img->quad[abs(p->imgY[j][i]-imgY_ref[j][i])];
}
}
// Chroma
diff_u=0;
diff_v=0;
for (j=0; j < p->size_y_cr; ++j)
{
for (i=0; i < p->size_x_cr; ++i)
{
diff_u += img->quad[abs(imgUV_ref[0][j][i]-p->imgUV[0][j][i])];
diff_v += img->quad[abs(imgUV_ref[1][j][i]-p->imgUV[1][j][i])];
}
}
/* if (diff_y == 0)
diff_y = 1;
if (diff_u == 0)
diff_u = 1;
if (diff_v == 0)
diff_v = 1; */
// Collecting SNR statistics
if (diff_y != 0)
snr->snr_y=(float)(10*log10(65025*(float)(p->size_x)*(p->size_y)/(float)diff_y)); // luma snr for current frame
else
snr->snr_y=0;
if (diff_u != 0)
snr->snr_u=(float)(10*log10(65025*(float)(p->size_x)*(p->size_y)/(float)(4*diff_u))); // chroma snr for current frame
else
snr->snr_u=0;
if (diff_v != 0)
snr->snr_v=(float)(10*log10(65025*(float)(p->size_x)*(p->size_y)/(float)(4*diff_v))); // chroma snr for current frame
else
snr->snr_v=0;
if (img->number == 0) // first
{
snr->snr_ya=snr->snr_y1=snr->snr_y; // keep luma snr for first frame
snr->snr_ua=snr->snr_u1=snr->snr_u; // keep chroma snr for first frame
snr->snr_va=snr->snr_v1=snr->snr_v; // keep chroma snr for first frame
}
else
{
snr->snr_ya=(float)(snr->snr_ya*(img->number+Bframe_ctr)+snr->snr_y)/(img->number+Bframe_ctr+1); // average snr chroma for all frames
snr->snr_ua=(float)(snr->snr_ua*(img->number+Bframe_ctr)+snr->snr_u)/(img->number+Bframe_ctr+1); // average snr luma for all frames
snr->snr_va=(float)(snr->snr_va*(img->number+Bframe_ctr)+snr->snr_v)/(img->number+Bframe_ctr+1); // average snr luma for all frames
}
}
/*!
************************************************************************
* \brief
* Interpolation of 1/4 subpixel
************************************************************************
*/
void get_block(int ref_frame, StorablePicture **list, int x_pos, int y_pos, struct img_par *img, int block[BLOCK_SIZE][BLOCK_SIZE])
{
int dx, dy;
int x, y;
int i, j;
int maxold_x,maxold_y;
int result;
int pres_x;
int pres_y;
int tmp_res[4][9];
static const int COEF[6] = { 1, -5, 20, 20, -5, 1 };
dx = x_pos&3;
dy = y_pos&3;
x_pos = (x_pos-dx)/4;
y_pos = (y_pos-dy)/4;
maxold_x = dec_picture->size_x-1;
maxold_y = dec_picture->size_y-1;
if (dec_picture->mb_field[img->current_mb_nr])
maxold_y = dec_picture->size_y/2 - 1;
if (dx == 0 && dy == 0) { /* fullpel position */
for (j = 0; j < BLOCK_SIZE; j++)
for (i = 0; i < BLOCK_SIZE; i++)
block[i][j] = list[ref_frame]->imgY[max(0,min(maxold_y,y_pos+j))][max(0,min(maxold_x,x_pos+i))];
}
else { /* other positions */
if (dy == 0) { /* No vertical interpolation */
for (j = 0; j < BLOCK_SIZE; j++) {
for (i = 0; i < BLOCK_SIZE; i++) {
for (result = 0, x = -2; x < 4; x++)
result += list[ref_frame]->imgY[max(0,min(maxold_y,y_pos+j))][max(0,min(maxold_x,x_pos+i+x))]*COEF[x+2];
block[i][j] = max(0, min(255, (result+16)/32));
}
}
if ((dx&1) == 1) {
for (j = 0; j < BLOCK_SIZE; j++)
for (i = 0; i < BLOCK_SIZE; i++)
block[i][j] = (block[i][j] + list[ref_frame]->imgY[max(0,min(maxold_y,y_pos+j))][max(0,min(maxold_x,x_pos+i+dx/2))] +1 )/2;
}
}
else if (dx == 0) { /* No horizontal interpolation */
for (j = 0; j < BLOCK_SIZE; j++) {
for (i = 0; i < BLOCK_SIZE; i++) {
for (result = 0, y = -2; y < 4; y++)
result += list[ref_frame]->imgY[max(0,min(maxold_y,y_pos+j+y))][max(0,min(maxold_x,x_pos+i))]*COEF[y+2];
block[i][j] = max(0, min(255, (result+16)/32));
}
}
if ((dy&1) == 1) {
for (j = 0; j < BLOCK_SIZE; j++)
for (i = 0; i < BLOCK_SIZE; i++)
block[i][j] = (block[i][j] + list[ref_frame]->imgY[max(0,min(maxold_y,y_pos+j+dy/2))][max(0,min(maxold_x,x_pos+i))] +1 )/2;
}
}
else if (dx == 2) { /* Vertical & horizontal interpolation */
for (j = -2; j < BLOCK_SIZE+3; j++) {
for (i = 0; i < BLOCK_SIZE; i++)
for (tmp_res[i][j+2] = 0, x = -2; x < 4; x++)
tmp_res[i][j+2] += list[ref_frame]->imgY[max(0,min(maxold_y,y_pos+j))][max(0,min(maxold_x,x_pos+i+x))]*COEF[x+2];
}
for (j = 0; j < BLOCK_SIZE; j++) {
for (i = 0; i < BLOCK_SIZE; i++) {
for (result = 0, y = -2; y < 4; y++)
result += tmp_res[i][j+y+2]*COEF[y+2];
block[i][j] = max(0, min(255, (result+512)/1024));
}
}
if ((dy&1) == 1) {
for (j = 0; j < BLOCK_SIZE; j++)
for (i = 0; i < BLOCK_SIZE; i++)
block[i][j] = (block[i][j] + max(0, min(255, (tmp_res[i][j+2+dy/2]+16)/32)) +1 )/2;
}
}
else if (dy == 2) { /* Horizontal & vertical interpolation */
for (j = 0; j < BLOCK_SIZE; j++) {
for (i = -2; i < BLOCK_SIZE+3; i++)
for (tmp_res[j][i+2] = 0, y = -2; y < 4; y++)
tmp_res[j][i+2] += list[ref_frame]->imgY[max(0,min(maxold_y,y_pos+j+y))][max(0,min(maxold_x,x_pos+i))]*COEF[y+2];
}
for (j = 0; j < BLOCK_SIZE; j++) {
for (i = 0; i < BLOCK_SIZE; i++) {
for (result = 0, x = -2; x < 4; x++)
result += tmp_res[j][i+x+2]*COEF[x+2];
block[i][j] = max(0, min(255, (result+512)/1024));
}
}
if ((dx&1) == 1) {
for (j = 0; j < BLOCK_SIZE; j++)
for (i = 0; i < BLOCK_SIZE; i++)
block[i][j] = (block[i][j] + max(0, min(255, (tmp_res[j][i+2+dx/2]+16)/32))+1)/2;
}
}
else { /* Diagonal interpolation */
for (j = 0; j < BLOCK_SIZE; j++) {
for (i = 0; i < BLOCK_SIZE; i++) {
pres_y = dy == 1 ? y_pos+j : y_pos+j+1;
pres_y = max(0,min(maxold_y,pres_y));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -