00001
00029 #include "contributors.h"
00030
00031 #include <math.h>
00032 #include <limits.h>
00033
00034 #include "global.h"
00035 #include "image.h"
00036 #include "fmo.h"
00037 #include "annexb.h"
00038 #include "nalu.h"
00039 #include "parset.h"
00040 #include "header.h"
00041
00042 #include "sei.h"
00043 #include "output.h"
00044 #include "mb_access.h"
00045 #include "memalloc.h"
00046 #include "macroblock.h"
00047
00048 #include "loopfilter.h"
00049
00050 #include "biaridecod.h"
00051 #include "context_ini.h"
00052 #include "cabac.h"
00053 #include "vlc.h"
00054 #include "quant.h"
00055
00056 #include "errorconcealment.h"
00057 #include "erc_api.h"
00058
00059 extern objectBuffer_t *erc_object_list;
00060 extern ercVariables_t *erc_errorVar;
00061 extern frame erc_recfr;
00062 extern int erc_mvperMB;
00063 extern ImageParameters *erc_img;
00064
00065 extern StorablePicture **listX[6];
00066 extern ColocatedParams *Co_located;
00067
00068 extern StorablePicture *no_reference_picture;
00069 int non_conforming_stream;
00070
00071 StorablePicture *dec_picture;
00072 StorablePicture *dec_picture_JV[MAX_PLANE];
00073
00074 OldSliceParams old_slice;
00075
00082 static void init_picture(ImageParameters *img, Slice *currSlice, struct inp_par *inp)
00083 {
00084 int i;
00085 int nplane;
00086
00087 if (dec_picture)
00088 {
00089
00090 exit_picture(&dec_picture);
00091 }
00092 if (img->recovery_point)
00093 img->recovery_frame_num = (img->frame_num + img->recovery_frame_cnt) % img->MaxFrameNum;
00094
00095 if (img->idr_flag)
00096 img->recovery_frame_num = img->frame_num;
00097
00098 if (img->recovery_point == 0 &&
00099 img->frame_num != img->pre_frame_num &&
00100 img->frame_num != (img->pre_frame_num + 1) % img->MaxFrameNum)
00101 {
00102 if (active_sps->gaps_in_frame_num_value_allowed_flag == 0)
00103 {
00104
00105 if(inp->conceal_mode !=0)
00106 {
00107 if((img->frame_num) < ((img->pre_frame_num + 1) % img->MaxFrameNum))
00108 {
00109
00110
00111
00112 img->conceal_mode = 1;
00113 img->IDR_concealment_flag = 1;
00114 conceal_lost_frames(img);
00115
00116 img->conceal_mode = inp->conceal_mode;
00117 }
00118 else
00119 {
00120
00121 img->conceal_mode = inp->conceal_mode;
00122
00123 img->IDR_concealment_flag = 0;
00124 conceal_lost_frames(img);
00125 }
00126 }
00127 else
00128 {
00129 error("An unintentional loss of pictures occurs! Exit\n", 100);
00130 }
00131 }
00132 if(img->conceal_mode == 0)
00133 fill_frame_num_gap(img);
00134 }
00135
00136 if(img->nal_reference_idc)
00137 {
00138 img->pre_frame_num = img->frame_num;
00139 }
00140
00141 img->num_dec_mb = 0;
00142
00143
00144 decode_poc(img);
00145
00146 if (img->recovery_frame_num == img->frame_num &&
00147 img->recovery_poc == 0x7fffffff)
00148 img->recovery_poc = img->framepoc;
00149
00150 if(img->nal_reference_idc)
00151 img->last_ref_pic_poc = img->framepoc;
00152
00153
00154
00155 if (img->structure==FRAME ||img->structure==TOP_FIELD)
00156 {
00157 gettime (&(img->start_time));
00158 }
00159
00160 dec_picture = alloc_storable_picture ((PictureStructure) img->structure, img->width, img->height, img->width_cr, img->height_cr);
00161 dec_picture->top_poc=img->toppoc;
00162 dec_picture->bottom_poc=img->bottompoc;
00163 dec_picture->frame_poc=img->framepoc;
00164 dec_picture->qp = img->qp;
00165 dec_picture->slice_qp_delta = currSlice->slice_qp_delta;
00166 dec_picture->chroma_qp_offset[0] = active_pps->chroma_qp_index_offset;
00167 dec_picture->chroma_qp_offset[1] = active_pps->second_chroma_qp_index_offset;
00168
00169
00170
00171
00172 ercReset(erc_errorVar, img->PicSizeInMbs, img->PicSizeInMbs, dec_picture->size_x);
00173 erc_mvperMB = 0;
00174
00175 switch (img->structure )
00176 {
00177 case TOP_FIELD:
00178 {
00179 dec_picture->poc=img->toppoc;
00180 img->number *= 2;
00181 break;
00182 }
00183 case BOTTOM_FIELD:
00184 {
00185 dec_picture->poc=img->bottompoc;
00186 img->number = img->number * 2 + 1;
00187 break;
00188 }
00189 case FRAME:
00190 {
00191 dec_picture->poc=img->framepoc;
00192 break;
00193 }
00194 default:
00195 error("img->structure not initialized", 235);
00196 }
00197
00198 img->current_slice_nr=0;
00199
00200 if (img->type > SI_SLICE)
00201 {
00202 set_ec_flag(SE_PTYPE);
00203 img->type = P_SLICE;
00204 }
00205
00206
00207 if (active_pps->entropy_coding_mode_flag == UVLC)
00208 {
00209 memset(&img->nz_coeff[0][0][0][0], -1, img->PicSizeInMbs * 3 * 4 * 4 *sizeof(byte));
00210 }
00211
00212 if(active_pps->constrained_intra_pred_flag)
00213 {
00214 for (i=0; i<(int)img->PicSizeInMbs; i++)
00215 {
00216 img->intra_block[i] = 1;
00217 }
00218 }
00219
00220
00221
00222 if( IS_INDEPENDENT(img) )
00223 {
00224 for( nplane=0; nplane<MAX_PLANE; nplane++ )
00225 {
00226 for(i=0; i<(int)img->PicSizeInMbs; i++)
00227 {
00228 img->mb_data_JV[nplane][i].slice_nr = -1;
00229 img->mb_data_JV[nplane][i].ei_flag = 1;
00230 img->mb_data_JV[nplane][i].dpl_flag = 0;
00231 }
00232 }
00233 }
00234 else
00235 {
00236 for(i=0; i<(int)img->PicSizeInMbs; i++)
00237 {
00238 img->mb_data[i].slice_nr = -1;
00239 img->mb_data[i].ei_flag = 1;
00240 img->mb_data[i].dpl_flag = 0;
00241 }
00242 }
00243
00244 img->mb_y = img->mb_x = 0;
00245 img->block_y_aff = img->block_y = img->pix_y = img->pix_c_y = 0;
00246 img->block_x = img->pix_x = img->pix_c_x = 0;
00247
00248 dec_picture->slice_type = img->type;
00249 dec_picture->used_for_reference = (img->nal_reference_idc != 0);
00250 dec_picture->idr_flag = img->idr_flag;
00251 dec_picture->no_output_of_prior_pics_flag = img->no_output_of_prior_pics_flag;
00252 dec_picture->long_term_reference_flag = img->long_term_reference_flag;
00253 dec_picture->adaptive_ref_pic_buffering_flag = img->adaptive_ref_pic_buffering_flag;
00254
00255 dec_picture->dec_ref_pic_marking_buffer = img->dec_ref_pic_marking_buffer;
00256 img->dec_ref_pic_marking_buffer = NULL;
00257
00258 dec_picture->MbaffFrameFlag = img->MbaffFrameFlag;
00259 dec_picture->PicWidthInMbs = img->PicWidthInMbs;
00260
00261 get_mb_block_pos = dec_picture->MbaffFrameFlag ? get_mb_block_pos_mbaff : get_mb_block_pos_normal;
00262 getNeighbour = dec_picture->MbaffFrameFlag ? getAffNeighbour : getNonAffNeighbour;
00263
00264 dec_picture->pic_num = img->frame_num;
00265 dec_picture->frame_num = img->frame_num;
00266
00267 dec_picture->recovery_frame = (img->frame_num == img->recovery_frame_num);
00268
00269 dec_picture->coded_frame = (img->structure==FRAME);
00270
00271 dec_picture->chroma_format_idc = active_sps->chroma_format_idc;
00272
00273 dec_picture->frame_mbs_only_flag = active_sps->frame_mbs_only_flag;
00274 dec_picture->frame_cropping_flag = active_sps->frame_cropping_flag;
00275
00276 if (dec_picture->frame_cropping_flag)
00277 {
00278 dec_picture->frame_cropping_rect_left_offset = active_sps->frame_cropping_rect_left_offset;
00279 dec_picture->frame_cropping_rect_right_offset = active_sps->frame_cropping_rect_right_offset;
00280 dec_picture->frame_cropping_rect_top_offset = active_sps->frame_cropping_rect_top_offset;
00281 dec_picture->frame_cropping_rect_bottom_offset = active_sps->frame_cropping_rect_bottom_offset;
00282 }
00283
00284 #if (ENABLE_OUTPUT_TONEMAPPING)
00285
00286 dec_picture->seiHasTone_mapping = 0;
00287 if (seiToneMapping.seiHasTone_mapping)
00288 {
00289 dec_picture->seiHasTone_mapping = 1;
00290 dec_picture->tone_mapping_model_id = seiToneMapping.model_id;
00291 dec_picture->tonemapped_bit_depth = seiToneMapping.sei_bit_depth;
00292 dec_picture->tone_mapping_lut = malloc(sizeof(int)*(1<<seiToneMapping.coded_data_bit_depth));
00293 if (NULL == dec_picture->tone_mapping_lut)
00294 {
00295 no_mem_exit("init_picture: tone_mapping_lut");
00296 }
00297 memcpy(dec_picture->tone_mapping_lut, seiToneMapping.lut, sizeof(imgpel)*(1<<seiToneMapping.coded_data_bit_depth));
00298 update_tone_mapping_sei();
00299 }
00300 #endif
00301
00302 if( IS_INDEPENDENT(img) )
00303 {
00304 dec_picture_JV[0] = dec_picture;
00305 dec_picture_JV[1] = alloc_storable_picture ((PictureStructure) img->structure, img->width, img->height, img->width_cr, img->height_cr);
00306 copy_dec_picture_JV( dec_picture_JV[1], dec_picture_JV[0] );
00307 dec_picture_JV[2] = alloc_storable_picture ((PictureStructure) img->structure, img->width, img->height, img->width_cr, img->height_cr);
00308 copy_dec_picture_JV( dec_picture_JV[2], dec_picture_JV[0] );
00309 }
00310 }
00311
00312 void MbAffPostProc(void)
00313 {
00314 imgpel temp[32][16];
00315
00316 imgpel ** imgY = dec_picture->imgY;
00317 imgpel ***imgUV = dec_picture->imgUV;
00318
00319 int i, y, x0, y0, uv;
00320 for (i=0; i<(int)dec_picture->PicSizeInMbs; i+=2)
00321 {
00322 if (dec_picture->motion.mb_field[i])
00323 {
00324 get_mb_pos(i, img->mb_size[IS_LUMA], &x0, &y0);
00325 for (y=0; y<(2*MB_BLOCK_SIZE);y++)
00326 memcpy(temp[y], &imgY[y0+y][x0], MB_BLOCK_SIZE * sizeof(imgpel));
00327
00328 for (y=0; y<MB_BLOCK_SIZE;y++)
00329 {
00330 memcpy(&imgY[y0+(2*y )][x0], temp[y ], MB_BLOCK_SIZE * sizeof(imgpel));
00331 memcpy(&imgY[y0+(2*y+1)][x0], temp[y+MB_BLOCK_SIZE], MB_BLOCK_SIZE * sizeof(imgpel));
00332 }
00333
00334
00335 if (dec_picture->chroma_format_idc != YUV400)
00336 {
00337 x0 = x0 / (16/img->mb_cr_size_x);
00338 y0 = y0 / (16/img->mb_cr_size_y);
00339
00340 for (uv=0; uv<2; uv++)
00341 {
00342 for (y=0; y<(2*img->mb_cr_size_y);y++)
00343 memcpy(temp[y], &imgUV[uv][y0+y][x0], img->mb_cr_size_x * sizeof(imgpel));
00344
00345 for (y=0; y<img->mb_cr_size_y;y++)
00346 {
00347 memcpy(&imgUV[uv][y0+(2*y )][x0], temp[y ], img->mb_cr_size_x * sizeof(imgpel));
00348 memcpy(&imgUV[uv][y0+(2*y+1)][x0], temp[y+img->mb_cr_size_y], img->mb_cr_size_x * sizeof(imgpel));
00349 }
00350 }
00351 }
00352 }
00353 }
00354 }
00355
00356 static void fill_wp_params(ImageParameters *img)
00357 {
00358 int i, j, k;
00359 int comp;
00360 int log_weight_denom;
00361 int tb, td;
00362 int bframe = (img->type==B_SLICE);
00363 int tx,DistScaleFactor;
00364
00365 int max_l0_ref = img->num_ref_idx_l0_active;
00366 int max_l1_ref = img->num_ref_idx_l1_active;
00367
00368 if (active_pps->weighted_bipred_idc == 2 && bframe)
00369 {
00370 img->luma_log2_weight_denom = 5;
00371 img->chroma_log2_weight_denom = 5;
00372 img->wp_round_luma = 16;
00373 img->wp_round_chroma = 16;
00374
00375 for (i=0; i<MAX_REFERENCE_PICTURES; i++)
00376 {
00377 for (comp=0; comp<3; comp++)
00378 {
00379 log_weight_denom = (comp == 0) ? img->luma_log2_weight_denom : img->chroma_log2_weight_denom;
00380 img->wp_weight[0][i][comp] = 1<<log_weight_denom;
00381 img->wp_weight[1][i][comp] = 1<<log_weight_denom;
00382 img->wp_offset[0][i][comp] = 0;
00383 img->wp_offset[1][i][comp] = 0;
00384 }
00385 }
00386 }
00387
00388 if (bframe)
00389 {
00390 for (i=0; i<max_l0_ref; i++)
00391 {
00392 for (j=0; j<max_l1_ref; j++)
00393 {
00394 for (comp = 0; comp<3; comp++)
00395 {
00396 log_weight_denom = (comp == 0) ? img->luma_log2_weight_denom : img->chroma_log2_weight_denom;
00397 if (active_pps->weighted_bipred_idc == 1)
00398 {
00399 img->wbp_weight[0][i][j][comp] = img->wp_weight[0][i][comp];
00400 img->wbp_weight[1][i][j][comp] = img->wp_weight[1][j][comp];
00401 }
00402 else if (active_pps->weighted_bipred_idc == 2)
00403 {
00404 td = iClip3(-128,127,listX[LIST_1][j]->poc - listX[LIST_0][i]->poc);
00405 if (td == 0 || listX[LIST_1][j]->is_long_term || listX[LIST_0][i]->is_long_term)
00406 {
00407 img->wbp_weight[0][i][j][comp] = 32;
00408 img->wbp_weight[1][i][j][comp] = 32;
00409 }
00410 else
00411 {
00412 tb = iClip3(-128,127,img->ThisPOC - listX[LIST_0][i]->poc);
00413
00414 tx = (16384 + iabs(td/2))/td;
00415 DistScaleFactor = iClip3(-1024, 1023, (tx*tb + 32 )>>6);
00416
00417 img->wbp_weight[1][i][j][comp] = DistScaleFactor >> 2;
00418 img->wbp_weight[0][i][j][comp] = 64 - img->wbp_weight[1][i][j][comp];
00419 if (img->wbp_weight[1][i][j][comp] < -64 || img->wbp_weight[1][i][j][comp] > 128)
00420 {
00421 img->wbp_weight[0][i][j][comp] = 32;
00422 img->wbp_weight[1][i][j][comp] = 32;
00423 img->wp_offset[0][i][comp] = 0;
00424 img->wp_offset[1][j][comp] = 0;
00425 }
00426 }
00427 }
00428 }
00429 }
00430 }
00431 }
00432
00433 if (bframe && img->MbaffFrameFlag)
00434 {
00435 for (i=0; i<2*max_l0_ref; i++)
00436 {
00437 for (j=0; j<2*max_l1_ref; j++)
00438 {
00439 for (comp = 0; comp<3; comp++)
00440 {
00441 for (k=2; k<6; k+=2)
00442 {
00443 img->wp_offset[k+0][i][comp] = img->wp_offset[0][i>>1][comp];
00444 img->wp_offset[k+1][j][comp] = img->wp_offset[1][j>>1][comp];
00445
00446 log_weight_denom = (comp == 0) ? img->luma_log2_weight_denom : img->chroma_log2_weight_denom;
00447 if (active_pps->weighted_bipred_idc == 1)
00448 {
00449 img->wbp_weight[k+0][i][j][comp] = img->wp_weight[0][i>>1][comp];
00450 img->wbp_weight[k+1][i][j][comp] = img->wp_weight[1][j>>1][comp];
00451 }
00452 else if (active_pps->weighted_bipred_idc == 2)
00453 {
00454 td = iClip3(-128,127,listX[k+LIST_1][j]->poc - listX[k+LIST_0][i]->poc);
00455 if (td == 0 || listX[k+LIST_1][j]->is_long_term || listX[k+LIST_0][i]->is_long_term)
00456 {
00457 img->wbp_weight[k+0][i][j][comp] = 32;
00458 img->wbp_weight[k+1][i][j][comp] = 32;
00459 }
00460 else
00461 {
00462 tb = iClip3(-128,127,((k==2)?img->toppoc:img->bottompoc) - listX[k+LIST_0][i]->poc);
00463
00464 tx = (16384 + iabs(td/2))/td;
00465 DistScaleFactor = iClip3(-1024, 1023, (tx*tb + 32 )>>6);
00466
00467 img->wbp_weight[k+1][i][j][comp] = DistScaleFactor >> 2;
00468 img->wbp_weight[k+0][i][j][comp] = 64 - img->wbp_weight[k+1][i][j][comp];
00469 if (img->wbp_weight[k+1][i][j][comp] < -64 || img->wbp_weight[k+1][i][j][comp] > 128)
00470 {
00471 img->wbp_weight[k+1][i][j][comp] = 32;
00472 img->wbp_weight[k+0][i][j][comp] = 32;
00473 img->wp_offset[k+0][i][comp] = 0;
00474 img->wp_offset[k+1][j][comp] = 0;
00475 }
00476 }
00477 }
00478 }
00479 }
00480 }
00481 }
00482 }
00483 }
00484
00485 static void decode_slice(ImageParameters *img, Slice *currSlice, struct inp_par *inp, int current_header)
00486 {
00487 if (active_pps->entropy_coding_mode_flag)
00488 {
00489 init_contexts (img);
00490 cabac_new_slice();
00491 }
00492
00493 if ( (active_pps->weighted_bipred_idc > 0 && (img->type == B_SLICE)) || (active_pps->weighted_pred_flag && img->type !=I_SLICE))
00494 fill_wp_params(img);
00495
00496
00497
00498
00499 if ((current_header == SOP || current_header == SOS) && currSlice->ei_flag == 0)
00500 decode_one_slice(img, currSlice, inp);
00501
00502
00503
00504
00505 }
00506
00507
00515 static void Error_tracking(void)
00516 {
00517 int i;
00518
00519 if(img->redundant_pic_cnt == 0)
00520 {
00521 Is_primary_correct = Is_redundant_correct = 1;
00522 }
00523
00524 if(img->redundant_pic_cnt == 0 && img->type != I_SLICE)
00525 {
00526 for(i=0;i<img->num_ref_idx_l0_active;i++)
00527 {
00528 if(ref_flag[i] == 0)
00529 {
00530 Is_primary_correct = 0;
00531 }
00532 }
00533 }
00534 else if(img->redundant_pic_cnt != 0 && img->type != I_SLICE)
00535 {
00536 if(ref_flag[redundant_slice_ref_idx] == 0)
00537 {
00538 Is_redundant_correct = 0;
00539 }
00540 }
00541 }
00542
00551 int decode_one_frame(ImageParameters *img, struct inp_par *inp, struct snr_par *snr)
00552 {
00553 int current_header;
00554 Slice *currSlice = img->currentSlice;
00555 int i;
00556
00557 img->current_slice_nr = 0;
00558 img->current_mb_nr = -4711;
00559 currSlice->next_header = -8888;
00560 img->num_dec_mb = 0;
00561 img->newframe = 1;
00562
00563 while ((currSlice->next_header != EOS && currSlice->next_header != SOP))
00564 {
00565 current_header = read_new_slice(img->currentSlice);
00566
00567
00568 Error_tracking();
00569
00570
00571
00572 if(img->frame_num == previous_frame_num && img->redundant_pic_cnt !=0
00573 && Is_primary_correct !=0 && current_header != EOS)
00574 {
00575 continue;
00576 }
00577
00578
00579 if(!(img->redundant_pic_cnt != 0 && previous_frame_num == img->frame_num))
00580 {
00581 for(i=16;i>0;i--)
00582 {
00583 ref_flag[i] = ref_flag[i-1];
00584 }
00585 }
00586 ref_flag[0] = img->redundant_pic_cnt==0 ? Is_primary_correct : Is_redundant_correct;
00587 previous_frame_num = img->frame_num;
00588
00589 if (current_header == EOS)
00590 {
00591 exit_picture(&dec_picture);
00592 return EOS;
00593 }
00594
00595 decode_slice(img, currSlice, inp, current_header);
00596
00597 img->newframe = 0;
00598 img->current_slice_nr++;
00599 }
00600
00601 exit_picture(&dec_picture);
00602
00603 return (SOP);
00604 }
00605
00606
00623 void buf2img (imgpel** imgX, unsigned char* buf, int size_x, int size_y, int symbol_size_in_bytes)
00624 {
00625 int i,j;
00626
00627 unsigned short tmp16, ui16;
00628 unsigned long tmp32, ui32;
00629
00630 if (symbol_size_in_bytes> sizeof(imgpel))
00631 {
00632 error ("Source picture has higher bit depth than imgpel data type. \nPlease recompile with larger data type for imgpel.", 500);
00633 }
00634
00635 if (( sizeof(char) == sizeof (imgpel)) && ( sizeof(char) == symbol_size_in_bytes))
00636 {
00637
00638 memcpy(&imgX[0][0], buf, size_x * size_y);
00639
00640
00641 }
00642 else
00643 {
00644
00645 if (testEndian())
00646 {
00647
00648 switch (symbol_size_in_bytes)
00649 {
00650 case 1:
00651 {
00652 for(j=0;j<size_y;j++)
00653 for(i=0;i<size_x;i++)
00654 {
00655 imgX[j][i]= buf[i+j*size_x];
00656 }
00657 break;
00658 }
00659 case 2:
00660 {
00661 for(j=0;j<size_y;j++)
00662 for(i=0;i<size_x;i++)
00663 {
00664 memcpy(&tmp16, buf+((i+j*size_x)*2), 2);
00665 ui16 = (unsigned short) ((tmp16 >> 8) | ((tmp16&0xFF)<<8));
00666 imgX[j][i] = (imgpel) ui16;
00667 }
00668 break;
00669 }
00670 case 4:
00671 {
00672 for(j=0;j<size_y;j++)
00673 for(i=0;i<size_x;i++)
00674 {
00675 memcpy(&tmp32, buf+((i+j*size_x)*4), 4);
00676 ui32 = ((tmp32&0xFF00)<<8) | ((tmp32&0xFF)<<24) | ((tmp32&0xFF0000)>>8) | ((tmp32&0xFF000000)>>24);
00677 imgX[j][i] = (imgpel) ui32;
00678 }
00679 }
00680 default:
00681 {
00682 error ("reading only from formats of 8, 16 or 32 bit allowed on big endian architecture", 500);
00683 break;
00684 }
00685 }
00686
00687 }
00688 else
00689 {
00690
00691 if (symbol_size_in_bytes == 1)
00692 {
00693 for (j=0; j < size_y; j++)
00694 {
00695 for (i=0; i < size_x; i++)
00696 {
00697 imgX[j][i]=*(buf++);
00698 }
00699 }
00700 }
00701 else
00702 {
00703 for (j=0; j < size_y; j++)
00704 {
00705 int jpos = j*size_x;
00706 for (i=0; i < size_x; i++)
00707 {
00708 imgX[j][i]=0;
00709 memcpy(&(imgX[j][i]), buf +((i+jpos)*symbol_size_in_bytes), symbol_size_in_bytes);
00710 }
00711 }
00712 }
00713
00714 }
00715 }
00716 }
00717
00718
00725 void calculate_frame_no(StorablePicture *p)
00726 {
00727
00728 int psnrPOC = active_sps->mb_adaptive_frame_field_flag ? p->poc /(params->poc_scale) : p->poc/(params->poc_scale);
00729
00730 if (psnrPOC==0)
00731 img->idr_psnr_number = img->number*img->ref_poc_gap/(params->poc_scale);
00732
00733 img->psnr_number=imax(img->psnr_number,img->idr_psnr_number+psnrPOC);
00734
00735 frame_no = img->idr_psnr_number + psnrPOC;
00736 }
00737
00738
00746 void find_snr(struct snr_par *snr,
00747 StorablePicture *p,
00748 int *p_ref)
00749 {
00750 static const int SubWidthC [4]= { 1, 2, 2, 1};
00751 static const int SubHeightC [4]= { 1, 2, 1, 1};
00752 int crop_left, crop_right, crop_top, crop_bottom;
00753
00754 int i,j, k;
00755 int ret;
00756 int64 diff_comp[3] = {0};
00757 int64 status;
00758 int symbol_size_in_bytes = img->pic_unit_bitsize_on_disk/8;
00759 int comp_size_x[3], comp_size_y[3];
00760 int64 framesize_in_bytes;
00761
00762 unsigned int max_pix_value_sqd[3] = {iabs2(img->max_imgpel_value_comp[0]), iabs2(img->max_imgpel_value_comp[1]), iabs2(img->max_imgpel_value_comp[2])};
00763
00764 Boolean rgb_output = (Boolean) (active_sps->vui_seq_parameters.matrix_coefficients==0);
00765 unsigned char *buf;
00766 imgpel **cur_ref[3] = {imgY_ref, p->chroma_format_idc != YUV400 ? imgUV_ref[0] : NULL, p->chroma_format_idc != YUV400 ? imgUV_ref[1] : NULL};
00767 imgpel **cur_comp[3] = {p->imgY, p->chroma_format_idc != YUV400 ? p->imgUV[0] : NULL , p->chroma_format_idc!= YUV400 ? p->imgUV[1] : NULL};
00768
00769 char yuv_types[4][6]= {"4:0:0","4:2:0","4:2:2","4:4:4"};
00770
00771
00772 if (p->frame_cropping_flag)
00773 {
00774 crop_left = SubWidthC[p->chroma_format_idc] * p->frame_cropping_rect_left_offset;
00775 crop_right = SubWidthC[p->chroma_format_idc] * p->frame_cropping_rect_right_offset;
00776 crop_top = SubHeightC[p->chroma_format_idc]*( 2 - p->frame_mbs_only_flag ) * p->frame_cropping_rect_top_offset;
00777 crop_bottom = SubHeightC[p->chroma_format_idc]*( 2 - p->frame_mbs_only_flag ) * p->frame_cropping_rect_bottom_offset;
00778 }
00779 else
00780 {
00781 crop_left = crop_right = crop_top = crop_bottom = 0;
00782 }
00783
00784 comp_size_x[0] = p->size_x - crop_left - crop_right;
00785 comp_size_y[0] = p->size_y - crop_top - crop_bottom;
00786
00787
00788 if (p->frame_cropping_flag)
00789 {
00790 crop_left = p->frame_cropping_rect_left_offset;
00791 crop_right = p->frame_cropping_rect_right_offset;
00792 crop_top = ( 2 - p->frame_mbs_only_flag ) * p->frame_cropping_rect_top_offset;
00793 crop_bottom = ( 2 - p->frame_mbs_only_flag ) * p->frame_cropping_rect_bottom_offset;
00794 }
00795 else
00796 {
00797 crop_left = crop_right = crop_top = crop_bottom = 0;
00798 }
00799
00800 if ((p->chroma_format_idc==YUV400) && params->write_uv)
00801 {
00802 comp_size_x[1] = comp_size_x[2] = (p->size_x >> 1);
00803 comp_size_y[1] = comp_size_y[2] = (p->size_y >> 1);
00804 }
00805 else
00806 {
00807
00808 comp_size_x[1] = comp_size_x[2] = (p->size_x_cr - crop_left - crop_right);
00809 comp_size_y[1] = comp_size_y[2] = (p->size_y_cr - crop_top - crop_bottom);
00810 }
00811
00812 framesize_in_bytes = (((int64) comp_size_x[0] * comp_size_y[0]) + ((int64) comp_size_x[1] * comp_size_y[1] ) * 2) * symbol_size_in_bytes;
00813
00814
00815 buf = malloc ( comp_size_x[0] * comp_size_y[0] * symbol_size_in_bytes );
00816
00817 if (NULL == buf)
00818 {
00819 no_mem_exit("find_snr: buf");
00820 }
00821
00822 status = lseek (*p_ref, framesize_in_bytes * frame_no, SEEK_SET);
00823 if (status == -1)
00824 {
00825 fprintf(stderr, "Warning: Could not seek to frame number %d in reference file. Shown PSNR might be wrong.\n", frame_no);
00826 free (buf);
00827 return;
00828 }
00829
00830 if(rgb_output)
00831 lseek (*p_ref, framesize_in_bytes/3, SEEK_CUR);
00832
00833 for (k = 0; k < ((p->chroma_format_idc != YUV400) ? 3 : 1); k++)
00834 {
00835
00836 if(rgb_output && k == 2)
00837 lseek (*p_ref, -framesize_in_bytes, SEEK_CUR);
00838
00839 ret = read(*p_ref, buf, comp_size_x[k] * comp_size_y[k] * symbol_size_in_bytes);
00840 if (ret != comp_size_x[k] * comp_size_y[k] * symbol_size_in_bytes)
00841 {
00842 printf ("Warning: could not read from reconstructed file\n");
00843 memset (buf, 0, comp_size_x[k] * comp_size_y[k] * symbol_size_in_bytes);
00844 close(*p_ref);
00845 *p_ref = -1;
00846 break;
00847 }
00848 buf2img(cur_ref[k], buf, comp_size_x[k], comp_size_y[k], symbol_size_in_bytes);
00849
00850 for (j=0; j < comp_size_y[k]; ++j)
00851 {
00852 for (i=0; i < comp_size_x[k]; ++i)
00853 {
00854 diff_comp[k] += iabs2(cur_comp[k][j][i] - cur_ref[k][j][i]);
00855 }
00856 }
00857
00858
00859 snr->snr[k] = psnr( max_pix_value_sqd[k], comp_size_x[k] * comp_size_y[k], (float) diff_comp[k]);
00860
00861 if (img->number == 0)
00862 {
00863 snr->snra[k] = snr->snr[k];
00864 }
00865 else
00866 {
00867 snr->snra[k] = (float)(snr->snra[k]*(snr->frame_ctr)+snr->snr[k])/(snr->frame_ctr + 1);
00868 }
00869 }
00870
00871 if(rgb_output)
00872 lseek (*p_ref, framesize_in_bytes*2/3, SEEK_CUR);
00873
00874 free (buf);
00875
00876
00877 if(p->concealed_pic)
00878 {
00879 fprintf(stdout,"%04d(P) %8d %5d %5d %7.4f %7.4f %7.4f %s %5d\n",
00880 frame_no, p->frame_poc, p->pic_num, p->qp,
00881 snr->snr[0], snr->snr[1], snr->snr[2], yuv_types[p->chroma_format_idc], 0);
00882 }
00883 }
00884
00885
00886 void reorder_lists(int currSliceType, Slice * currSlice)
00887 {
00888 if ((currSliceType != I_SLICE)&&(currSliceType != SI_SLICE))
00889 {
00890 if (currSlice->ref_pic_list_reordering_flag_l0)
00891 {
00892 reorder_ref_pic_list(listX[0], &listXsize[0],
00893 img->num_ref_idx_l0_active - 1,
00894 currSlice->reordering_of_pic_nums_idc_l0,
00895 currSlice->abs_diff_pic_num_minus1_l0,
00896 currSlice->long_term_pic_idx_l0);
00897 }
00898 if (no_reference_picture == listX[0][img->num_ref_idx_l0_active-1])
00899 {
00900 if (non_conforming_stream)
00901 printf("RefPicList0[ num_ref_idx_l0_active_minus1 ] is equal to 'no reference picture'\n");
00902 else
00903 error("RefPicList0[ num_ref_idx_l0_active_minus1 ] is equal to 'no reference picture', invalid bitstream",500);
00904 }
00905
00906 listXsize[0] = img->num_ref_idx_l0_active;
00907 }
00908 if (currSliceType == B_SLICE)
00909 {
00910 if (currSlice->ref_pic_list_reordering_flag_l1)
00911 {
00912 reorder_ref_pic_list(listX[1], &listXsize[1],
00913 img->num_ref_idx_l1_active - 1,
00914 currSlice->reordering_of_pic_nums_idc_l1,
00915 currSlice->abs_diff_pic_num_minus1_l1,
00916 currSlice->long_term_pic_idx_l1);
00917 }
00918 if (no_reference_picture == listX[1][img->num_ref_idx_l1_active-1])
00919 {
00920 if (non_conforming_stream)
00921 printf("RefPicList1[ num_ref_idx_l1_active_minus1 ] is equal to 'no reference picture'\n");
00922 else
00923 error("RefPicList1[ num_ref_idx_l1_active_minus1 ] is equal to 'no reference picture', invalid bitstream",500);
00924 }
00925
00926 listXsize[1] = img->num_ref_idx_l1_active;
00927 }
00928
00929 free_ref_pic_list_reordering_buffer(currSlice);
00930 }
00931
00932
00939 void set_ref_pic_num(void)
00940 {
00941 int i,j;
00942
00943 int slice_id=img->current_slice_nr;
00944
00945 for (i=0;i<listXsize[LIST_0];i++)
00946 {
00947 dec_picture->ref_pic_num [slice_id][LIST_0][i] = listX[LIST_0][i]->poc * 2 + ((listX[LIST_0][i]->structure==BOTTOM_FIELD)?1:0) ;
00948 dec_picture->frm_ref_pic_num [slice_id][LIST_0][i] = listX[LIST_0][i]->frame_poc * 2;
00949 dec_picture->top_ref_pic_num [slice_id][LIST_0][i] = listX[LIST_0][i]->top_poc * 2;
00950 dec_picture->bottom_ref_pic_num [slice_id][LIST_0][i] = listX[LIST_0][i]->bottom_poc * 2 + 1;
00951
00952
00953 }
00954
00955 for (i=0;i<listXsize[LIST_1];i++)
00956 {
00957 dec_picture->ref_pic_num [slice_id][LIST_1][i] = listX[LIST_1][i]->poc *2 + ((listX[LIST_1][i]->structure==BOTTOM_FIELD)?1:0);
00958 dec_picture->frm_ref_pic_num [slice_id][LIST_1][i] = listX[LIST_1][i]->frame_poc * 2;
00959 dec_picture->top_ref_pic_num [slice_id][LIST_1][i] = listX[LIST_1][i]->top_poc * 2;
00960 dec_picture->bottom_ref_pic_num [slice_id][LIST_1][i] = listX[LIST_1][i]->bottom_poc * 2 + 1;
00961 }
00962
00963 if (!active_sps->frame_mbs_only_flag)
00964 {
00965 if (img->structure==FRAME)
00966 {
00967 for (j=2;j<6;j++)
00968 {
00969 for (i=0;i<listXsize[j];i++)
00970 {
00971 dec_picture->ref_pic_num [slice_id][j][i] = listX[j][i]->poc * 2 + ((listX[j][i]->structure==BOTTOM_FIELD)?1:0);
00972 dec_picture->frm_ref_pic_num [slice_id][j][i] = listX[j][i]->frame_poc * 2 ;
00973 dec_picture->top_ref_pic_num [slice_id][j][i] = listX[j][i]->top_poc * 2 ;
00974 dec_picture->bottom_ref_pic_num [slice_id][j][i] = listX[j][i]->bottom_poc * 2 + 1;
00975 }
00976 }
00977 }
00978 }
00979 }
00980
00981
00988 int read_new_slice(Slice *currSlice)
00989 {
00990 NALU_t *nalu = AllocNALU(MAX_CODED_FRAME_SIZE);
00991 int current_header = 0;
00992 int BitsUsedByHeader;
00993 Bitstream *currStream;
00994
00995 int slice_id_a, slice_id_b, slice_id_c;
00996 int redundant_pic_cnt_b, redundant_pic_cnt_c;
00997
00998 while (1)
00999 {
01000 if (0 == read_next_nalu(nalu))
01001 return EOS;
01002
01003 process_nalu:
01004
01005 switch (nalu->nal_unit_type)
01006 {
01007 case NALU_TYPE_SLICE:
01008 case NALU_TYPE_IDR:
01009
01010 if (img->recovery_point || nalu->nal_unit_type == NALU_TYPE_IDR)
01011 {
01012 if (img->recovery_point_found == 0)
01013 {
01014 if (nalu->nal_unit_type != NALU_TYPE_IDR)
01015 {
01016 printf("Warning: Decoding does not start with an IDR picture.\n");
01017 non_conforming_stream = 1;
01018 }
01019 else
01020 non_conforming_stream = 0;
01021 }
01022 img->recovery_point_found = 1;
01023 }
01024
01025 if (img->recovery_point_found == 0)
01026 break;
01027
01028 img->idr_flag = (nalu->nal_unit_type == NALU_TYPE_IDR);
01029 img->nal_reference_idc = nalu->nal_reference_idc;
01030 currSlice->dp_mode = PAR_DP_1;
01031 currSlice->max_part_nr = 1;
01032 currSlice->ei_flag = 0;
01033 currStream = currSlice->partArr[0].bitstream;
01034 currStream->ei_flag = 0;
01035 currStream->frame_bitoffset = currStream->read_len = 0;
01036 memcpy (currStream->streamBuffer, &nalu->buf[1], nalu->len-1);
01037 currStream->code_len = currStream->bitstream_length = RBSPtoSODB(currStream->streamBuffer, nalu->len-1);
01038
01039
01040
01041
01042
01043 BitsUsedByHeader = FirstPartOfSliceHeader(currSlice);
01044 UseParameterSet (currSlice->pic_parameter_set_id);
01045 BitsUsedByHeader+= RestOfSliceHeader (currSlice);
01046
01047 FmoInit (active_pps, active_sps);
01048
01049 AssignQuantParam (active_pps, active_sps);
01050
01051
01052 if(img->redundant_pic_cnt && Is_primary_correct==0 && Is_redundant_correct)
01053 {
01054 dec_picture->slice_type=img->type;
01055 }
01056
01057 if(is_new_picture(dec_picture, currSlice, &old_slice))
01058 {
01059 init_picture(img, currSlice, params);
01060
01061 current_header = SOP;
01062
01063 CheckZeroByteVCL(nalu);
01064 }
01065 else
01066 current_header = SOS;
01067
01068 init_lists(img->type, img->currentSlice->structure);
01069 reorder_lists (img->type, img->currentSlice);
01070
01071 if (img->structure==FRAME)
01072 {
01073 init_mbaff_lists();
01074 }
01075
01076
01077 if (img->MbaffFrameFlag)
01078 img->current_mb_nr = currSlice->start_mb_nr << 1;
01079 else
01080 img->current_mb_nr = currSlice->start_mb_nr;
01081
01082 if (active_pps->entropy_coding_mode_flag)
01083 {
01084 int ByteStartPosition = currStream->frame_bitoffset/8;
01085 if (currStream->frame_bitoffset%8 != 0)
01086 {
01087 ByteStartPosition++;
01088 }
01089 arideco_start_decoding (&currSlice->partArr[0].de_cabac, currStream->streamBuffer, ByteStartPosition, &currStream->read_len);
01090 }
01091
01092 FreeNALU(nalu);
01093 img->recovery_point = 0;
01094 return current_header;
01095 break;
01096 case NALU_TYPE_DPA:
01097
01098 currSlice->dpB_NotPresent =1;
01099 currSlice->dpC_NotPresent =1;
01100
01101 img->idr_flag = FALSE;
01102 img->nal_reference_idc = nalu->nal_reference_idc;
01103 currSlice->dp_mode = PAR_DP_3;
01104 currSlice->max_part_nr = 3;
01105 currSlice->ei_flag = 0;
01106 currStream = currSlice->partArr[0].bitstream;
01107 currStream->ei_flag = 0;
01108 currStream->frame_bitoffset = currStream->read_len = 0;
01109 memcpy (currStream->streamBuffer, &nalu->buf[1], nalu->len-1);
01110 currStream->code_len = currStream->bitstream_length = RBSPtoSODB(currStream->streamBuffer, nalu->len-1);
01111
01112 BitsUsedByHeader = FirstPartOfSliceHeader(currSlice);
01113 UseParameterSet (currSlice->pic_parameter_set_id);
01114 BitsUsedByHeader += RestOfSliceHeader (currSlice);
01115
01116 FmoInit (active_pps, active_sps);
01117
01118 if(is_new_picture(dec_picture, currSlice, &old_slice))
01119 {
01120 init_picture(img, currSlice, params);
01121 current_header = SOP;
01122 CheckZeroByteVCL(nalu);
01123 }
01124 else
01125 current_header = SOS;
01126
01127 init_lists(img->type, img->currentSlice->structure);
01128 reorder_lists (img->type, img->currentSlice);
01129
01130 if (img->structure==FRAME)
01131 {
01132 init_mbaff_lists();
01133 }
01134
01135
01136 if (img->MbaffFrameFlag)
01137 img->current_mb_nr = currSlice->start_mb_nr << 1;
01138 else
01139 img->current_mb_nr = currSlice->start_mb_nr;
01140
01141
01142
01143
01144 slice_id_a = ue_v("NALU: DP_A slice_id", currStream);
01145
01146 if (active_pps->entropy_coding_mode_flag)
01147 error ("received data partition with CABAC, this is not allowed", 500);
01148
01149
01150 if (0 == read_next_nalu(nalu))
01151 return current_header;
01152
01153 if ( NALU_TYPE_DPB == nalu->nal_unit_type)
01154 {
01155
01156 currStream = currSlice->partArr[1].bitstream;
01157 currStream->ei_flag = 0;
01158 currStream->frame_bitoffset = currStream->read_len = 0;
01159
01160 memcpy (currStream->streamBuffer, &nalu->buf[1], nalu->len-1);
01161 currStream->code_len = currStream->bitstream_length = RBSPtoSODB(currStream->streamBuffer, nalu->len-1);
01162
01163 slice_id_b = ue_v("NALU: DP_B slice_id", currStream);
01164
01165 currSlice->dpB_NotPresent = 0;
01166
01167 if ((slice_id_b != slice_id_a) || (nalu->lost_packets))
01168 {
01169 printf ("Waning: got a data partition B which does not match DP_A (DP loss!)\n");
01170 currSlice->dpB_NotPresent =1;
01171 currSlice->dpC_NotPresent =1;
01172 }
01173 else
01174 {
01175 if (active_pps->redundant_pic_cnt_present_flag)
01176 redundant_pic_cnt_b = ue_v("NALU: DP_B redudant_pic_cnt", currStream);
01177 else
01178 redundant_pic_cnt_b = 0;
01179
01180
01181 if (0 == read_next_nalu(nalu))
01182 return current_header;
01183 }
01184 }
01185 else
01186 {
01187 currSlice->dpB_NotPresent =1;
01188 }
01189
01190
01191 if ( NALU_TYPE_DPC == nalu->nal_unit_type)
01192 {
01193 currStream = currSlice->partArr[2].bitstream;
01194 currStream->ei_flag = 0;
01195 currStream->frame_bitoffset = currStream->read_len = 0;
01196
01197 memcpy (currStream->streamBuffer, &nalu->buf[1], nalu->len-1);
01198 currStream->code_len = currStream->bitstream_length = RBSPtoSODB(currStream->streamBuffer, nalu->len-1);
01199
01200 currSlice->dpC_NotPresent = 0;
01201
01202 slice_id_c = ue_v("NALU: DP_C slice_id", currStream);
01203 if ((slice_id_c != slice_id_a)|| (nalu->lost_packets))
01204 {
01205 printf ("Warning: got a data partition C which does not match DP_A(DP loss!)\n");
01206
01207 currSlice->dpC_NotPresent =1;
01208 }
01209
01210 if (active_pps->redundant_pic_cnt_present_flag)
01211 redundant_pic_cnt_c = ue_v("NALU:SLICE_C redudand_pic_cnt", currStream);
01212 else
01213 redundant_pic_cnt_c = 0;
01214 }
01215 else
01216 {
01217 currSlice->dpC_NotPresent =1;
01218 }
01219
01220
01221 if ((nalu->nal_unit_type != NALU_TYPE_DPB) && (nalu->nal_unit_type != NALU_TYPE_DPC))
01222 {
01223
01224 goto process_nalu;
01225
01226
01227 }
01228
01229 FreeNALU(nalu);
01230 return current_header;
01231
01232 break;
01233 case NALU_TYPE_DPB:
01234 printf ("found data partition B without matching DP A, discarding\n");
01235 break;
01236 case NALU_TYPE_DPC:
01237 printf ("found data partition C without matching DP A, discarding\n");
01238 break;
01239 case NALU_TYPE_SEI:
01240
01241 InterpretSEIMessage(nalu->buf,nalu->len,img);
01242 break;
01243 case NALU_TYPE_PPS:
01244 ProcessPPS(nalu);
01245 break;
01246 case NALU_TYPE_SPS:
01247 ProcessSPS(nalu);
01248 break;
01249 case NALU_TYPE_AUD:
01250
01251 break;
01252 case NALU_TYPE_EOSEQ:
01253
01254 break;
01255 case NALU_TYPE_EOSTREAM:
01256
01257 break;
01258 case NALU_TYPE_FILL:
01259 printf ("read_new_slice: Found NALU_TYPE_FILL, len %d\n", (int) nalu->len);
01260 printf ("Skipping these filling bits, proceeding w/ next NALU\n");
01261 break;
01262 default:
01263 printf ("Found NALU type %d, len %d undefined, ignore NALU, moving on\n", (int) nalu->nal_unit_type, (int) nalu->len);
01264 break;
01265 }
01266 }
01267 FreeNALU(nalu);
01268
01269 return current_header;
01270 }
01271
01272
01273
01281 void exit_picture(StorablePicture **dec_picture)
01282 {
01283 char yuv_types[4][6]= {"4:0:0","4:2:0","4:2:2","4:4:4"};
01284 int ercStartMB;
01285 int ercSegment;
01286 frame recfr;
01287 unsigned int i;
01288 int structure, frame_poc, slice_type, refpic, qp, pic_num, chroma_format_idc, is_idr;
01289
01290 static char cslice_type[9];
01291
01292 int64 tmp_time;
01293 char yuvFormat[10];
01294 int nplane;
01295
01296
01297 if (*dec_picture==NULL)
01298 {
01299 return;
01300 }
01301
01302
01303 if( IS_INDEPENDENT(img) )
01304 {
01305 int colour_plane_id = img->colour_plane_id;
01306 for( nplane=0; nplane<MAX_PLANE; nplane++ )
01307 {
01308 change_plane_JV( nplane );
01309 DeblockPicture( img, *dec_picture );
01310 }
01311 img->colour_plane_id = colour_plane_id;
01312 make_frame_picture_JV();
01313 }
01314 else
01315 {
01316 DeblockPicture( img, *dec_picture );
01317 }
01318
01319 if ((*dec_picture)->MbaffFrameFlag)
01320 MbAffPostProc();
01321
01322 recfr.yptr = &(*dec_picture)->imgY[0][0];
01323 if ((*dec_picture)->chroma_format_idc != YUV400)
01324 {
01325 recfr.uptr = &(*dec_picture)->imgUV[0][0][0];
01326 recfr.vptr = &(*dec_picture)->imgUV[1][0][0];
01327 }
01328
01330 ercStartMB = 0;
01331 ercSegment = 0;
01332
01334 if (!(*dec_picture)->MbaffFrameFlag)
01335 {
01336 ercStartSegment(0, ercSegment, 0 , erc_errorVar);
01338 for(i = 1; i<(*dec_picture)->PicSizeInMbs; i++)
01339 {
01340 if(img->mb_data[i].ei_flag != img->mb_data[i-1].ei_flag)
01341 {
01342 ercStopSegment(i-1, ercSegment, 0, erc_errorVar);
01343
01345 if(img->mb_data[i-1].ei_flag)
01346 ercMarkCurrSegmentLost((*dec_picture)->size_x, erc_errorVar);
01347 else
01348 ercMarkCurrSegmentOK((*dec_picture)->size_x, erc_errorVar);
01349
01350 ercSegment++;
01351 ercStartSegment(i, ercSegment, 0 , erc_errorVar);
01352 ercStartMB = i;
01353 }
01354 }
01356 ercStopSegment((*dec_picture)->PicSizeInMbs-1, ercSegment, 0, erc_errorVar);
01357 if(img->mb_data[i-1].ei_flag)
01358 ercMarkCurrSegmentLost((*dec_picture)->size_x, erc_errorVar);
01359 else
01360 ercMarkCurrSegmentOK((*dec_picture)->size_x, erc_errorVar);
01361
01363 erc_mvperMB /= (*dec_picture)->PicSizeInMbs;
01364
01365 erc_img = img;
01366 if((*dec_picture)->slice_type == I_SLICE || (*dec_picture)->slice_type == SI_SLICE)
01367 ercConcealIntraFrame(&recfr, (*dec_picture)->size_x, (*dec_picture)->size_y, erc_errorVar);
01368 else
01369 ercConcealInterFrame(&recfr, erc_object_list, (*dec_picture)->size_x, (*dec_picture)->size_y, erc_errorVar, (*dec_picture)->chroma_format_idc);
01370 }
01371
01372 if (img->structure == FRAME)
01373 frame_postprocessing(img);
01374 else
01375 field_postprocessing(img);
01376
01377 structure = (*dec_picture)->structure;
01378 slice_type = (*dec_picture)->slice_type;
01379 frame_poc = (*dec_picture)->frame_poc;
01380 refpic = (*dec_picture)->used_for_reference;
01381 qp = (*dec_picture)->qp;
01382 pic_num = (*dec_picture)->pic_num;
01383 is_idr = (*dec_picture)->idr_flag;
01384
01385 chroma_format_idc = (*dec_picture)->chroma_format_idc;
01386
01387 store_picture_in_dpb(*dec_picture);
01388 *dec_picture=NULL;
01389
01390 if (img->last_has_mmco_5)
01391 {
01392 img->pre_frame_num = 0;
01393 }
01394
01395 if (params->silent == FALSE)
01396 {
01397 if (structure==TOP_FIELD || structure==FRAME)
01398 {
01399 if(slice_type == I_SLICE && is_idr)
01400 strcpy(cslice_type,"IDR");
01401 else if(slice_type == I_SLICE)
01402 strcpy(cslice_type," I ");
01403 else if(slice_type == P_SLICE)
01404 strcpy(cslice_type," P ");
01405 else if(slice_type == SP_SLICE)
01406 strcpy(cslice_type,"SP ");
01407 else if (slice_type == SI_SLICE)
01408 strcpy(cslice_type,"SI ");
01409 else if(refpic)
01410 strcpy(cslice_type," B ");
01411 else
01412 strcpy(cslice_type," b ");
01413 if (structure==FRAME)
01414 {
01415 strncat(cslice_type,") ",8-strlen(cslice_type));
01416 }
01417 }
01418 else if (structure==BOTTOM_FIELD)
01419 {
01420 if(slice_type == I_SLICE && is_idr)
01421 strncat(cslice_type,"|IDR)",8-strlen(cslice_type));
01422 else if(slice_type == I_SLICE)
01423 strncat(cslice_type,"| I )",8-strlen(cslice_type));
01424 else if(slice_type == P_SLICE)
01425 strncat(cslice_type,"| P )",8-strlen(cslice_type));
01426 else if(slice_type == SP_SLICE)
01427 strncat(cslice_type,"|SP )",8-strlen(cslice_type));
01428 else if (slice_type == SI_SLICE)
01429 strncat(cslice_type,"|SI )",8-strlen(cslice_type));
01430 else if(refpic)
01431 strncat(cslice_type,"| B )",8-strlen(cslice_type));
01432 else
01433 strncat(cslice_type,"| b )",8-strlen(cslice_type));
01434 }
01435 }
01436
01437 if ((structure==FRAME)||structure==BOTTOM_FIELD)
01438 {
01439 gettime (&(img->end_time));
01440
01441 tmp_time = timediff(&(img->start_time), &(img->end_time));
01442 tot_time += tmp_time;
01443 tmp_time = timenorm(tmp_time);
01444
01445 sprintf(yuvFormat,"%s", yuv_types[chroma_format_idc]);
01446
01447 if (params->silent == FALSE)
01448 {
01449 if (p_ref != -1)
01450 fprintf(stdout,"%05d(%s%5d %5d %5d %8.4f %8.4f %8.4f %s %7d\n",
01451 frame_no, cslice_type, frame_poc, pic_num, qp, snr->snr[0], snr->snr[1], snr->snr[2], yuvFormat, (int) tmp_time);
01452 else
01453 fprintf(stdout,"%05d(%s%5d %5d %5d %s %7d\n",
01454 frame_no, cslice_type, frame_poc, pic_num, qp, yuvFormat, (int)tmp_time);
01455 }
01456 else
01457 fprintf(stdout,"Completed Decoding frame %05d.\r",snr->frame_ctr);
01458
01459 fflush(stdout);
01460
01461 if(slice_type == I_SLICE || slice_type == SI_SLICE || slice_type == P_SLICE || refpic)
01462 img->number++;
01463 else
01464 Bframe_ctr++;
01465 snr->frame_ctr++;
01466
01467 g_nFrame++;
01468 }
01469
01470 img->current_mb_nr = -4712;
01471 img->current_slice_nr = 0;
01472 }
01473
01482 void ercWriteMBMODEandMV(Macroblock *currMB, ImageParameters *img,struct inp_par *inp)
01483 {
01484 extern objectBuffer_t *erc_object_list;
01485 int i, ii, jj, currMBNum = img->current_mb_nr;
01486 int mbx = xPosMB(currMBNum,dec_picture->size_x), mby = yPosMB(currMBNum,dec_picture->size_x);
01487 objectBuffer_t *currRegion, *pRegion;
01488 short*** mv;
01489
01490 currRegion = erc_object_list + (currMBNum<<2);
01491
01492 if(img->type != B_SLICE)
01493 {
01494 for (i=0; i<4; i++)
01495 {
01496 pRegion = currRegion + i;
01497 pRegion->regionMode = (currMB->mb_type ==I16MB ? REGMODE_INTRA :
01498 currMB->b8mode[i]==IBLOCK ? REGMODE_INTRA_8x8 :
01499 currMB->b8mode[i]==0 ? REGMODE_INTER_COPY :
01500 currMB->b8mode[i]==1 ? REGMODE_INTER_PRED : REGMODE_INTER_PRED_8x8);
01501 if (currMB->b8mode[i]==0 || currMB->b8mode[i]==IBLOCK)
01502 {
01503 pRegion->mv[0] = 0;
01504 pRegion->mv[1] = 0;
01505 pRegion->mv[2] = 0;
01506 }
01507 else
01508 {
01509 ii = 4*mbx + (i & 0x01)*2;
01510 jj = 4*mby + (i >> 1 )*2;
01511 if (currMB->b8mode[i]>=5 && currMB->b8mode[i]<=7)
01512 {
01513 pRegion->mv[0] = (dec_picture->motion.mv[LIST_0][jj][ii][0] + dec_picture->motion.mv[LIST_0][jj][ii+1][0] + dec_picture->motion.mv[LIST_0][jj+1][ii][0] + dec_picture->motion.mv[LIST_0][jj+1][ii+1][0] + 2)/4;
01514 pRegion->mv[1] = (dec_picture->motion.mv[LIST_0][jj][ii][1] + dec_picture->motion.mv[LIST_0][jj][ii+1][1] + dec_picture->motion.mv[LIST_0][jj+1][ii][1] + dec_picture->motion.mv[LIST_0][jj+1][ii+1][1] + 2)/4;
01515 }
01516 else
01517 {
01518 pRegion->mv[0] = dec_picture->motion.mv[LIST_0][jj][ii][0];
01519 pRegion->mv[1] = dec_picture->motion.mv[LIST_0][jj][ii][1];
01520
01521
01522 }
01523 erc_mvperMB += iabs(pRegion->mv[0]) + iabs(pRegion->mv[1]);
01524 pRegion->mv[2] = dec_picture->motion.ref_idx[LIST_0][jj][ii];
01525 }
01526 }
01527 }
01528 else
01529 {
01530 for (i=0; i<4; i++)
01531 {
01532 ii = 4*mbx + (i%2)*2;
01533 jj = 4*mby + (i/2)*2;
01534 pRegion = currRegion + i;
01535 pRegion->regionMode = (currMB->mb_type ==I16MB ? REGMODE_INTRA :
01536 currMB->b8mode[i]==IBLOCK ? REGMODE_INTRA_8x8 : REGMODE_INTER_PRED_8x8);
01537 if (currMB->mb_type==I16MB || currMB->b8mode[i]==IBLOCK)
01538 {
01539 pRegion->mv[0] = 0;
01540 pRegion->mv[1] = 0;
01541 pRegion->mv[2] = 0;
01542 }
01543 else
01544 {
01545 int idx = (dec_picture->motion.ref_idx[0][jj][ii]<0)?1:0;
01546
01547
01548 mv = dec_picture->motion.mv[idx];
01549 pRegion->mv[0] = (mv[jj][ii][0] + mv[jj][ii+1][0] + mv[jj+1][ii][0] + mv[jj+1][ii+1][0] + 2)/4;
01550 pRegion->mv[1] = (mv[jj][ii][1] + mv[jj][ii+1][1] + mv[jj+1][ii][1] + mv[jj+1][ii+1][1] + 2)/4;
01551 erc_mvperMB += iabs(pRegion->mv[0]) + iabs(pRegion->mv[1]);
01552
01553 pRegion->mv[2] = (dec_picture->motion.ref_idx[idx][jj][ii]);
01554
01555
01556
01557
01559
01560
01561
01562
01563
01564
01565
01566 }
01567 }
01568 }
01569 }
01570
01578 void init_old_slice(OldSliceParams *p_old_slice)
01579 {
01580 p_old_slice->field_pic_flag = 0;
01581
01582 p_old_slice->pps_id = INT_MAX;
01583
01584 p_old_slice->frame_num = INT_MAX;
01585
01586 p_old_slice->nal_ref_idc = INT_MAX;
01587
01588 p_old_slice->idr_flag = FALSE;
01589
01590 p_old_slice->pic_oder_cnt_lsb = UINT_MAX;
01591 p_old_slice->delta_pic_oder_cnt_bottom = INT_MAX;
01592
01593 p_old_slice->delta_pic_order_cnt[0] = INT_MAX;
01594 p_old_slice->delta_pic_order_cnt[1] = INT_MAX;
01595 }
01596
01604 void exit_slice(Slice *currSlice, OldSliceParams *p_old_slice)
01605 {
01606
01607 p_old_slice->pps_id = currSlice->pic_parameter_set_id;
01608
01609 p_old_slice->frame_num = img->frame_num;
01610
01611 p_old_slice->field_pic_flag = img->field_pic_flag;
01612
01613 if(img->field_pic_flag)
01614 {
01615 p_old_slice->bottom_field_flag = img->bottom_field_flag;
01616 }
01617
01618 p_old_slice->nal_ref_idc = img->nal_reference_idc;
01619
01620 p_old_slice->idr_flag = img->idr_flag;
01621 if (img->idr_flag)
01622 {
01623 p_old_slice->idr_pic_id = img->idr_pic_id;
01624 }
01625
01626 if (active_sps->pic_order_cnt_type == 0)
01627 {
01628 p_old_slice->pic_oder_cnt_lsb = img->pic_order_cnt_lsb;
01629 p_old_slice->delta_pic_oder_cnt_bottom = img->delta_pic_order_cnt_bottom;
01630 }
01631
01632 if (active_sps->pic_order_cnt_type == 1)
01633 {
01634 p_old_slice->delta_pic_order_cnt[0] = img->delta_pic_order_cnt[0];
01635 p_old_slice->delta_pic_order_cnt[1] = img->delta_pic_order_cnt[1];
01636 }
01637 }
01638
01645 int is_new_picture(StorablePicture *dec_picture, Slice *currSlice, OldSliceParams *p_old_slice)
01646 {
01647 int result=0;
01648
01649 result |= (NULL==dec_picture);
01650
01651 result |= (p_old_slice->pps_id != currSlice->pic_parameter_set_id);
01652
01653 result |= (p_old_slice->frame_num != img->frame_num);
01654
01655 result |= (p_old_slice->field_pic_flag != img->field_pic_flag);
01656
01657 if(img->field_pic_flag && p_old_slice->field_pic_flag)
01658 {
01659 result |= (p_old_slice->bottom_field_flag != img->bottom_field_flag);
01660 }
01661
01662 result |= (p_old_slice->nal_ref_idc != img->nal_reference_idc) && ((p_old_slice->nal_ref_idc == 0) || (img->nal_reference_idc == 0));
01663
01664 result |= ( p_old_slice->idr_flag != img->idr_flag);
01665
01666 if (img->idr_flag && p_old_slice->idr_flag)
01667 {
01668 result |= (p_old_slice->idr_pic_id != img->idr_pic_id);
01669 }
01670
01671 if (active_sps->pic_order_cnt_type == 0)
01672 {
01673 result |= (p_old_slice->pic_oder_cnt_lsb != img->pic_order_cnt_lsb);
01674 result |= (p_old_slice->delta_pic_oder_cnt_bottom != img->delta_pic_order_cnt_bottom);
01675 }
01676
01677 if (active_sps->pic_order_cnt_type == 1)
01678 {
01679 result |= (p_old_slice->delta_pic_order_cnt[0] != img->delta_pic_order_cnt[0]);
01680 result |= (p_old_slice->delta_pic_order_cnt[1] != img->delta_pic_order_cnt[1]);
01681 }
01682
01683 return result;
01684 }
01685
01686
01687
01694 void frame_postprocessing(ImageParameters *img)
01695 {
01696 }
01697
01704 void field_postprocessing(ImageParameters *img)
01705 {
01706 img->number /= 2;
01707 }
01708
01709
01710
01718 void copy_dec_picture_JV( StorablePicture *dst, StorablePicture *src )
01719 {
01720 dst->top_poc = src->top_poc;
01721 dst->bottom_poc = src->bottom_poc;
01722 dst->frame_poc = src->frame_poc;
01723 dst->qp = src->qp;
01724 dst->slice_qp_delta = src->slice_qp_delta;
01725 dst->chroma_qp_offset[0] = src->chroma_qp_offset[0];
01726 dst->chroma_qp_offset[1] = src->chroma_qp_offset[1];
01727
01728 dst->poc = src->poc;
01729
01730 dst->slice_type = src->slice_type;
01731 dst->used_for_reference = src->used_for_reference;
01732 dst->idr_flag = src->idr_flag;
01733 dst->no_output_of_prior_pics_flag = src->no_output_of_prior_pics_flag;
01734 dst->long_term_reference_flag = src->long_term_reference_flag;
01735 dst->adaptive_ref_pic_buffering_flag = src->adaptive_ref_pic_buffering_flag;
01736
01737 dst->dec_ref_pic_marking_buffer = src->dec_ref_pic_marking_buffer;
01738
01739 dst->MbaffFrameFlag = src->MbaffFrameFlag;
01740 dst->PicWidthInMbs = src->PicWidthInMbs;
01741 dst->pic_num = src->pic_num;
01742 dst->frame_num = src->frame_num;
01743 dst->recovery_frame = src->recovery_frame;
01744 dst->coded_frame = src->coded_frame;
01745
01746 dst->chroma_format_idc = src->chroma_format_idc;
01747
01748 dst->frame_mbs_only_flag = src->frame_mbs_only_flag;
01749 dst->frame_cropping_flag = src->frame_cropping_flag;
01750
01751 dst->frame_cropping_rect_left_offset = src->frame_cropping_rect_left_offset;
01752 dst->frame_cropping_rect_right_offset = src->frame_cropping_rect_right_offset;
01753 dst->frame_cropping_rect_top_offset = src->frame_cropping_rect_top_offset;
01754 dst->frame_cropping_rect_bottom_offset = src->frame_cropping_rect_bottom_offset;
01755
01756 #if (ENABLE_OUTPUT_TONEMAPPING)
01757
01758 dst->seiHasTone_mapping = src->seiHasTone_mapping;
01759
01760 dst->seiHasTone_mapping = src->seiHasTone_mapping;
01761 dst->tone_mapping_model_id = src->tone_mapping_model_id;
01762 dst->tonemapped_bit_depth = src->tonemapped_bit_depth;
01763 if( src->tone_mapping_lut )
01764 {
01765 dst->tone_mapping_lut = malloc(sizeof(int)*(1<<seiToneMapping.coded_data_bit_depth));
01766 if (NULL == dst->tone_mapping_lut)
01767 {
01768 no_mem_exit("copy_dec_picture_JV: tone_mapping_lut");
01769 }
01770 memcpy(dst->tone_mapping_lut, src->tone_mapping_lut, sizeof(imgpel)*(1<<seiToneMapping.coded_data_bit_depth));
01771 }
01772 #endif
01773 }
01774
01775
01782 void decode_one_slice(ImageParameters *img, Slice *currSlice, struct inp_par *inp)
01783 {
01784 Boolean end_of_slice = FALSE;
01785 Macroblock *currMB = NULL;
01786 img->cod_counter=-1;
01787
01788 set_interpret_mb_mode(img->type);
01789
01790 if( IS_INDEPENDENT(img) )
01791 {
01792 change_plane_JV( img->colour_plane_id );
01793 }
01794
01795 set_ref_pic_num();
01796
01797 if (img->type == B_SLICE)
01798 {
01799 if( IS_INDEPENDENT(img) )
01800 {
01801 compute_colocated_JV(Co_located, listX);
01802 }
01803 else
01804 {
01805 compute_colocated(Co_located, listX);
01806 }
01807 }
01808
01809
01810
01811 while (end_of_slice == FALSE)
01812 {
01813
01814 #if TRACE
01815 fprintf(p_trace,"\n*********** POC: %i (I/P) MB: %i Slice: %i Type %d **********\n", img->ThisPOC, img->current_mb_nr, img->current_slice_nr, img->type);
01816 #endif
01817
01818
01819 start_macroblock(img, &currMB);
01820
01821 read_one_macroblock(img, currSlice, currMB);
01822 decode_one_macroblock(img, currMB, dec_picture);
01823
01824 if(img->MbaffFrameFlag && dec_picture->motion.mb_field[img->current_mb_nr])
01825 {
01826 img->num_ref_idx_l0_active >>= 1;
01827 img->num_ref_idx_l1_active >>= 1;
01828 }
01829
01830 ercWriteMBMODEandMV(currMB, img,inp);
01831
01832 end_of_slice=exit_macroblock(img,(!img->MbaffFrameFlag||img->current_mb_nr%2));
01833 }
01834
01835 exit_slice(currSlice, &old_slice);
01836
01837 }