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