00001
00045 #include "contributors.h"
00046
00047 #include <sys/stat.h>
00048
00049 #include "global.h"
00050 #include "annexb.h"
00051 #include "image.h"
00052 #include "memalloc.h"
00053 #include "mc_prediction.h"
00054 #include "mbuffer.h"
00055 #include "leaky_bucket.h"
00056 #include "fmo.h"
00057 #include "output.h"
00058 #include "cabac.h"
00059 #include "parset.h"
00060 #include "sei.h"
00061 #include "erc_api.h"
00062 #include "quant.h"
00063 #include "block.h"
00064 #include "nalu.h"
00065 #include "img_io.h"
00066
00067 #define LOGFILE "log.dec"
00068 #define DATADECFILE "dataDec.txt"
00069 #define TRACEFILE "trace_dec.txt"
00070
00071
00072
00073 DecoderParams *p_Dec;
00074
00075
00076 static void init_conf (ImageParameters *p_Img, InputParameters *p_Inp, char *config_filename);
00077 static void Report (ImageParameters *p_Img);
00078 static void init (ImageParameters *p_Img);
00079 static void malloc_slice(InputParameters *p_Inp, ImageParameters *p_Img);
00080 static void free_slice (Slice *currSlice);
00081
00093 void error(char *text, int code)
00094 {
00095 fprintf(stderr, "%s\n", text);
00096 flush_dpb(p_Dec->p_Img);
00097 exit(code);
00098 }
00099
00100
00107 void JMDecHelpExit (void)
00108 {
00109 fprintf( stderr, "\n ldecod [-h] {[defdec.cfg] | {[-p pocScale][-i bitstream.264]...[-o output.yuv] [-r reference.yuv] [-uv]}}\n\n"
00110 "## Parameters\n\n"
00111
00112 "## Options\n"
00113 " -h : prints function usage\n"
00114 " : parse <defdec.cfg> for decoder operation.\n"
00115 " -i : Input file name. \n"
00116 " -o : Output file name. If not specified default output is set as test_dec.yuv\n\n"
00117 " -r : Reference file name. If not specified default output is set as test_rec.yuv\n\n"
00118 " -p : Poc Scale. \n"
00119 " -uv : write chroma components for monochrome streams(4:2:0)\n"
00120 " -lp : By default the deblocking filter for High Intra-Only profile is off \n\t regardless of the flags in the bitstream. In the presence of\n\t this option, the loop filter usage will then be determined \n\t by the flags and parameters in the bitstream.\n\n"
00121
00122 "## Supported video file formats\n"
00123 " Input : .264 -> H.264 bitstream files. \n"
00124 " Output: .yuv -> RAW file. Format depends on bitstream information. \n\n"
00125
00126 "## Examples of usage:\n"
00127 " ldecod\n"
00128 " ldecod -h\n"
00129 " ldecod default.cfg\n"
00130 " ldecod -i bitstream.264 -o output.yuv -r reference.yuv\n");
00131
00132 exit(-1);
00133 }
00134
00135
00136 void Configure(ImageParameters *p_Img, InputParameters *p_Inp, int ac, char *av[])
00137 {
00138 int CLcount = 1;
00139 char *config_filename=NULL;
00140
00141 p_Img->p_Inp = p_Inp;
00142
00143 strcpy(p_Inp->infile,"test.264");
00144 strcpy(p_Inp->outfile,"test_dec.yuv");
00145 strcpy(p_Inp->reffile,"test_rec.yuv");
00146
00147 p_Inp->FileFormat = PAR_OF_ANNEXB;
00148 p_Inp->ref_offset=0;
00149 p_Inp->poc_scale=2;
00150 p_Inp->silent = FALSE;
00151 p_Inp->intra_profile_deblocking = 0;
00152
00153 #ifdef _LEAKYBUCKET_
00154 p_Inp->R_decoder=500000;
00155 p_Inp->B_decoder=104000;
00156 p_Inp->F_decoder=73000;
00157 strcpy(p_Inp->LeakyBucketParamFile,"leakybucketparam.cfg");
00158 #endif
00159
00160 if (ac==2)
00161 {
00162 if (0 == strncmp (av[1], "-h", 2))
00163 {
00164 JMDecHelpExit();
00165 }
00166 else if (0 == strncmp (av[1], "-s", 2))
00167 {
00168 p_Inp->silent = TRUE;
00169 }
00170 else
00171 {
00172 config_filename=av[1];
00173 init_conf(p_Img, p_Inp, av[1]);
00174 }
00175 CLcount=2;
00176 }
00177
00178 if (ac>=3)
00179 {
00180 if (0 == strncmp (av[1], "-i", 2))
00181 {
00182 strcpy(p_Inp->infile,av[2]);
00183 CLcount = 3;
00184 }
00185 if (0 == strncmp (av[1], "-h", 2))
00186 {
00187 JMDecHelpExit();
00188 }
00189 if (0 == strncmp (av[1], "-s", 2))
00190 {
00191 p_Inp->silent = TRUE;
00192 }
00193 }
00194
00195
00196
00197 while (CLcount < ac)
00198 {
00199 if (0 == strncmp (av[CLcount], "-h", 2))
00200 {
00201 JMDecHelpExit();
00202 }
00203 else if (0 == strncmp (av[CLcount], "-s", 2))
00204 {
00205 p_Inp->silent = TRUE;
00206 ++CLcount;
00207 }
00208 else if (0 == strncmp (av[CLcount], "-i", 2))
00209 {
00210 strcpy(p_Inp->infile,av[CLcount+1]);
00211 CLcount += 2;
00212 }
00213 else if (0 == strncmp (av[CLcount], "-o", 2))
00214 {
00215 strcpy(p_Inp->outfile,av[CLcount+1]);
00216 CLcount += 2;
00217 }
00218 else if (0 == strncmp (av[CLcount], "-r", 2))
00219 {
00220 strcpy(p_Inp->reffile,av[CLcount+1]);
00221 CLcount += 2;
00222 }
00223 else if (0 == strncmp (av[CLcount], "-p", 2))
00224 {
00225 sscanf (av[CLcount+1], "%d", &p_Inp->poc_scale);
00226 CLcount += 2;
00227 }
00228 else if (0 == strncmp (av[CLcount], "-uv", 3))
00229 {
00230 p_Inp->write_uv = 1;
00231 ++CLcount;
00232 }
00233 else if (0 == strncmp (av[CLcount], "-lp", 3))
00234 {
00235 p_Inp->intra_profile_deblocking = 1;
00236 ++CLcount;
00237 }
00238 else
00239 {
00240 snprintf(errortext, ET_SIZE, "Invalid syntax. Use ldecod -h for proper usage");
00241 error(errortext, 300);
00242 }
00243 }
00244
00245 #if TRACE
00246 if ((p_Dec->p_trace = fopen(TRACEFILE,"w"))==0)
00247 {
00248 snprintf(errortext, ET_SIZE, "Error open file %s!",TRACEFILE);
00249 error(errortext,500);
00250 }
00251 #endif
00252
00253 if ((p_Img->p_out = open(p_Inp->outfile, OPENFLAGS_WRITE, OPEN_PERMISSIONS))==-1)
00254 {
00255 snprintf(errortext, ET_SIZE, "Error open file %s ",p_Inp->outfile);
00256 error(errortext,500);
00257 }
00258
00259 fprintf(stdout,"----------------------------- JM %s %s -----------------------------\n", VERSION, EXT_VERSION);
00260 fprintf(stdout," Decoder config file : %s \n",config_filename);
00261 fprintf(stdout,"--------------------------------------------------------------------------\n");
00262 fprintf(stdout," Input H.264 bitstream : %s \n",p_Inp->infile);
00263 fprintf(stdout," Output decoded YUV : %s \n",p_Inp->outfile);
00264 fprintf(stdout," Output status file : %s \n",LOGFILE);
00265
00266
00267 if ((p_Img->p_ref = open(p_Inp->reffile,OPENFLAGS_READ))==-1)
00268 {
00269 fprintf(stdout," Input reference file : %s does not exist \n",p_Inp->reffile);
00270 fprintf(stdout," SNR values are not available\n");
00271 }
00272 else
00273 fprintf(stdout," Input reference file : %s \n",p_Inp->reffile);
00274
00275 fprintf(stdout,"--------------------------------------------------------------------------\n");
00276 #ifdef _LEAKYBUCKET_
00277 fprintf(stdout," Rate_decoder : %8ld \n",p_Inp->R_decoder);
00278 fprintf(stdout," B_decoder : %8ld \n",p_Inp->B_decoder);
00279 fprintf(stdout," F_decoder : %8ld \n",p_Inp->F_decoder);
00280 fprintf(stdout," LeakyBucketParamFile: %s \n",p_Inp->LeakyBucketParamFile);
00281 calc_buffer(p_Inp);
00282 fprintf(stdout,"--------------------------------------------------------------------------\n");
00283 #endif
00284 if (!p_Inp->silent)
00285 {
00286 fprintf(stdout,"POC must = frame# or field# for SNRs to be correct\n");
00287 fprintf(stdout,"--------------------------------------------------------------------------\n");
00288 fprintf(stdout," Frame POC Pic# QP SnrY SnrU SnrV Y:U:V Time(ms)\n");
00289 fprintf(stdout,"--------------------------------------------------------------------------\n");
00290 }
00291
00292 }
00293
00302 static void alloc_img( ImageParameters **p_Img)
00303 {
00304 if ((*p_Img = (ImageParameters *) calloc(1, sizeof(ImageParameters)))==NULL)
00305 no_mem_exit("alloc_img: p_Img");
00306
00307 if (((*p_Img)->old_slice = (OldSliceParams *) calloc(1, sizeof(OldSliceParams)))==NULL)
00308 no_mem_exit("alloc_img: p_Img->old_slice");
00309
00310 if (((*p_Img)->snr = (SNRParameters *)calloc(1, sizeof(SNRParameters)))==NULL)
00311 no_mem_exit("alloc_img: p_Img->snr");
00312
00313 if (((*p_Img)->p_Dpb = (DecodedPictureBuffer*)calloc(1, sizeof(DecodedPictureBuffer)))==NULL)
00314 no_mem_exit("alloc_img: p_Img->p_Dpb");
00315
00316 (*p_Img)->p_Dpb->init_done = 0;
00317
00318 (*p_Img)->global_init_done = 0;
00319
00320 #if (ENABLE_OUTPUT_TONEMAPPING)
00321 if (((*p_Img)->seiToneMapping = (ToneMappingSEI*)calloc(1, sizeof(ToneMappingSEI)))==NULL)
00322 no_mem_exit("alloc_img: (*p_Img)->seiToneMapping");
00323 #endif
00324
00325 }
00326
00327
00336 static void alloc_params( InputParameters **p_Inp )
00337 {
00338 if ((*p_Inp = (InputParameters *) calloc(1, sizeof(InputParameters)))==NULL)
00339 no_mem_exit("alloc_params: p_Inp");
00340 }
00341
00350 static void alloc_decoder( DecoderParams **p_Dec)
00351 {
00352 if ((*p_Dec = (DecoderParams *) calloc(1, sizeof(DecoderParams)))==NULL)
00353 no_mem_exit("alloc_decoder: p_Dec");
00354
00355 alloc_img(&((*p_Dec)->p_Img));
00356 alloc_params(&((*p_Dec)->p_Inp));
00357 (*p_Dec)->p_trace = NULL;
00358 (*p_Dec)->bufferSize = 0;
00359 (*p_Dec)->bitcounter = 0;
00360 }
00361
00370 static void free_img( ImageParameters *p_Img)
00371 {
00372
00373 if (p_Img != NULL)
00374 {
00375 free_annex_b (p_Img);
00376 #if (ENABLE_OUTPUT_TONEMAPPING)
00377 if (p_Img->seiToneMapping != NULL)
00378 {
00379 free (p_Img->seiToneMapping);
00380 p_Img->seiToneMapping = NULL;
00381 }
00382 #endif
00383
00384 if (p_Img->bitsfile != NULL)
00385 {
00386 free (p_Img->bitsfile);
00387 p_Img->bitsfile = NULL;
00388 }
00389
00390 if (p_Img->p_Dpb != NULL)
00391 {
00392 free (p_Img->p_Dpb);
00393 p_Img->p_Dpb = NULL;
00394 }
00395 if (p_Img->snr != NULL)
00396 {
00397 free (p_Img->snr);
00398 p_Img->snr = NULL;
00399 }
00400 if (p_Img->old_slice != NULL)
00401 {
00402 free (p_Img->old_slice);
00403 p_Img->old_slice = NULL;
00404 }
00405
00406 free (p_Img);
00407 p_Img = NULL;
00408 }
00409 }
00416 int main(int argc, char **argv)
00417 {
00418 alloc_decoder(&p_Dec);
00419
00420 Configure (p_Dec->p_Img, p_Dec->p_Inp, argc, argv);
00421
00422 initBitsFile(p_Dec->p_Img, p_Dec->p_Inp->FileFormat);
00423
00424 p_Dec->p_Img->bitsfile->OpenBitsFile(p_Dec->p_Img, p_Dec->p_Inp->infile);
00425
00426
00427 malloc_slice(p_Dec->p_Inp, p_Dec->p_Img);
00428 init_old_slice(p_Dec->p_Img->old_slice);
00429
00430 init(p_Dec->p_Img);
00431
00432 init_out_buffer(p_Dec->p_Img);
00433
00434 while (decode_one_frame(p_Dec->p_Img) != EOS)
00435 ;
00436
00437 Report(p_Dec->p_Img);
00438 free_slice(p_Dec->p_Img->currentSlice);
00439 FmoFinit(p_Dec->p_Img);
00440
00441 free_global_buffers(p_Dec->p_Img);
00442 flush_dpb(p_Dec->p_Img);
00443
00444 #if (PAIR_FIELDS_IN_OUTPUT)
00445 flush_pending_output(p_Dec->p_Img, p_Dec->p_Img->p_out);
00446 #endif
00447
00448 p_Dec->p_Img->bitsfile->CloseBitsFile(p_Dec->p_Img);
00449
00450 close(p_Dec->p_Img->p_out);
00451
00452 if (p_Dec->p_Img->p_ref != -1)
00453 close(p_Dec->p_Img->p_ref);
00454
00455 #if TRACE
00456 fclose(p_Dec->p_trace);
00457 #endif
00458
00459 ercClose(p_Dec->p_Img, p_Dec->p_Img->erc_errorVar);
00460
00461 CleanUpPPS(p_Dec->p_Img);
00462 free_dpb(p_Dec->p_Img);
00463 uninit_out_buffer(p_Dec->p_Img);
00464
00465 free (p_Dec->p_Inp);
00466 free_img (p_Dec->p_Img);
00467 free(p_Dec);
00468
00469 return 0;
00470 }
00471
00472
00479 static void init(ImageParameters *p_Img)
00480 {
00481 int i;
00482 InputParameters *p_Inp = p_Img->p_Inp;
00483 p_Img->oldFrameSizeInMbs = -1;
00484
00485 p_Img->imgY_ref = NULL;
00486 p_Img->imgUV_ref = NULL;
00487
00488 p_Img->recovery_point = 0;
00489 p_Img->recovery_point_found = 0;
00490 p_Img->recovery_poc = 0x7fffffff;
00491
00492 p_Img->idr_psnr_number = p_Inp->ref_offset;
00493 p_Img->psnr_number=0;
00494
00495 p_Img->number = 0;
00496 p_Img->type = I_SLICE;
00497
00498 p_Img->dec_ref_pic_marking_buffer = NULL;
00499
00500 p_Img->g_nFrame = 0;
00501
00502 p_Img->Bframe_ctr = p_Img->snr->frame_ctr = 0;
00503
00504
00505 p_Img->tot_time = 0;
00506
00507 p_Img->dec_picture = NULL;
00508
00509 for(i=0;i<17;++i)
00510 {
00511 p_Img->ref_flag[i] = 1;
00512 }
00513
00514 p_Img->MbToSliceGroupMap = NULL;
00515 p_Img->MapUnitToSliceGroupMap = NULL;
00516
00517 p_Img->LastAccessUnitExists = 0;
00518 p_Img->NALUCount = 0;
00519
00520
00521 p_Img->out_buffer = NULL;
00522 p_Img->pending_output = NULL;
00523 p_Img->pending_output_state = FRAME;
00524 p_Img->recovery_flag = 0;
00525
00526
00527 #if (ENABLE_OUTPUT_TONEMAPPING)
00528 init_tone_mapping_sei(p_Img->seiToneMapping);
00529 #endif
00530
00531 }
00532
00539 void init_frext(ImageParameters *p_Img)
00540 {
00541
00542 p_Img->bitdepth_luma_qp_scale = 6 * (p_Img->bitdepth_luma - 8);
00543
00544 if(p_Img->bitdepth_luma > p_Img->bitdepth_chroma || p_Img->active_sps->chroma_format_idc == YUV400)
00545 p_Img->pic_unit_bitsize_on_disk = (p_Img->bitdepth_luma > 8)? 16:8;
00546 else
00547 p_Img->pic_unit_bitsize_on_disk = (p_Img->bitdepth_chroma > 8)? 16:8;
00548 p_Img->dc_pred_value_comp[0] = 1<<(p_Img->bitdepth_luma - 1);
00549 p_Img->max_imgpel_value_comp[0] = (1<<p_Img->bitdepth_luma) - 1;
00550 p_Img->mb_size[0][0] = p_Img->mb_size[0][1] = MB_BLOCK_SIZE;
00551
00552 if (p_Img->active_sps->chroma_format_idc != YUV400)
00553 {
00554
00555 p_Img->bitdepth_chroma_qp_scale = 6 * (p_Img->bitdepth_chroma - 8);
00556 p_Img->dc_pred_value_comp[1] = (1 << (p_Img->bitdepth_chroma - 1));
00557 p_Img->dc_pred_value_comp[2] = p_Img->dc_pred_value_comp[1];
00558 p_Img->max_imgpel_value_comp[1] = (1 << p_Img->bitdepth_chroma) - 1;
00559 p_Img->max_imgpel_value_comp[2] = (1 << p_Img->bitdepth_chroma) - 1;
00560 p_Img->num_blk8x8_uv = (1 << p_Img->active_sps->chroma_format_idc) & (~(0x1));
00561 p_Img->num_uv_blocks = (p_Img->num_blk8x8_uv >> 1);
00562 p_Img->num_cdc_coeff = (p_Img->num_blk8x8_uv << 1);
00563 p_Img->mb_size[1][0] = p_Img->mb_size[2][0] = p_Img->mb_cr_size_x = (p_Img->active_sps->chroma_format_idc==YUV420 || p_Img->active_sps->chroma_format_idc==YUV422)? 8 : 16;
00564 p_Img->mb_size[1][1] = p_Img->mb_size[2][1] = p_Img->mb_cr_size_y = (p_Img->active_sps->chroma_format_idc==YUV444 || p_Img->active_sps->chroma_format_idc==YUV422)? 16 : 8;
00565 }
00566 else
00567 {
00568 p_Img->bitdepth_chroma_qp_scale = 0;
00569 p_Img->max_imgpel_value_comp[1] = 0;
00570 p_Img->max_imgpel_value_comp[2] = 0;
00571 p_Img->num_blk8x8_uv = 0;
00572 p_Img->num_uv_blocks = 0;
00573 p_Img->num_cdc_coeff = 0;
00574 p_Img->mb_size[1][0] = p_Img->mb_size[2][0] = p_Img->mb_cr_size_x = 0;
00575 p_Img->mb_size[1][1] = p_Img->mb_size[2][1] = p_Img->mb_cr_size_y = 0;
00576 }
00577 p_Img->mb_size_blk[0][0] = p_Img->mb_size_blk[0][1] = p_Img->mb_size[0][0] >> 2;
00578 p_Img->mb_size_blk[1][0] = p_Img->mb_size_blk[2][0] = p_Img->mb_size[1][0] >> 2;
00579 p_Img->mb_size_blk[1][1] = p_Img->mb_size_blk[2][1] = p_Img->mb_size[1][1] >> 2;
00580
00581 p_Img->mb_size_shift[0][0] = p_Img->mb_size_shift[0][1] = CeilLog2_sf (p_Img->mb_size[0][0]);
00582 p_Img->mb_size_shift[1][0] = p_Img->mb_size_shift[2][0] = CeilLog2_sf (p_Img->mb_size[1][0]);
00583 p_Img->mb_size_shift[1][1] = p_Img->mb_size_shift[2][1] = CeilLog2_sf (p_Img->mb_size[1][1]);
00584 }
00585
00592 static inline void conf_read_check (int val, int expected)
00593 {
00594 if (val != expected)
00595 {
00596 error ("init_conf: error reading from config file", 500);
00597 }
00598 }
00599
00612 static void init_conf(ImageParameters *p_Img, InputParameters *p_Inp, char *config_filename)
00613 {
00614 FILE *fd;
00615 int NAL_mode;
00616
00617
00618 long int temp;
00619 char tempval[100];
00620
00621
00622 if((fd=fopen(config_filename,"r")) == NULL)
00623 {
00624 snprintf(errortext, ET_SIZE, "Error: Control file %s not found\n",config_filename);
00625 error(errortext, 300);
00626 }
00627
00628 conf_read_check (fscanf(fd,"%s",p_Inp->infile), 1);
00629 conf_read_check (fscanf(fd,"%*[^\n]"), 0);
00630
00631 conf_read_check (fscanf(fd,"%s",p_Inp->outfile), 1);
00632 conf_read_check (fscanf(fd,"%*[^\n]"), 0);
00633
00634 conf_read_check (fscanf(fd,"%s",p_Inp->reffile), 1);
00635 conf_read_check (fscanf(fd,"%*[^\n]"), 0);
00636
00637 conf_read_check (fscanf(fd,"%d",&(p_Inp->write_uv)), 1);
00638 conf_read_check (fscanf(fd,"%*[^\n]"), 0);
00639
00640 conf_read_check (fscanf(fd,"%d",&(NAL_mode)), 1);
00641 conf_read_check (fscanf(fd,"%*[^\n]"), 0);
00642
00643 switch(NAL_mode)
00644 {
00645 case 0:
00646 p_Inp->FileFormat = PAR_OF_ANNEXB;
00647 break;
00648 case 1:
00649 p_Inp->FileFormat = PAR_OF_RTP;
00650 break;
00651 default:
00652 snprintf(errortext, ET_SIZE, "NAL mode %i is not supported", NAL_mode);
00653 error(errortext,400);
00654 }
00655
00656 conf_read_check (fscanf(fd,"%d,",&p_Inp->ref_offset), 1);
00657 conf_read_check (fscanf(fd,"%*[^\n]"), 0);
00658
00659 conf_read_check (fscanf(fd,"%d,",&p_Inp->poc_scale), 1);
00660 conf_read_check (fscanf(fd,"%*[^\n]"), 0);
00661
00662
00663 if (p_Inp->poc_scale < 1 || p_Inp->poc_scale > 10)
00664 {
00665 snprintf(errortext, ET_SIZE, "Poc Scale is %d. It has to be within range 1 to 10",p_Inp->poc_scale);
00666 error(errortext,1);
00667 }
00668
00669 p_Inp->write_uv=1;
00670
00671
00672 p_Img->conceal_mode = p_Inp->conceal_mode = 0;
00673 p_Img->ref_poc_gap = p_Inp->ref_poc_gap = 2;
00674 p_Img->poc_gap = p_Inp->poc_gap = 2;
00675
00676 #ifdef _LEAKYBUCKET_
00677 conf_read_check (fscanf(fd,"%ld,",&p_Inp->R_decoder), 1);
00678 conf_read_check (fscanf(fd, "%*[^\n]"), 0);
00679 conf_read_check (fscanf(fd,"%ld,",&p_Inp->B_decoder), 1);
00680 conf_read_check (fscanf(fd, "%*[^\n]"), 0);
00681 conf_read_check (fscanf(fd,"%ld,",&p_Inp->F_decoder), 1);
00682 conf_read_check (fscanf(fd, "%*[^\n]"), 0);
00683 conf_read_check (fscanf(fd,"%s",p_Inp->LeakyBucketParamFile), 1);
00684 conf_read_check (fscanf(fd,"%*[^\n]"), 0);
00685 #endif
00686
00687
00688
00689
00690 #ifndef _LEAKYBUCKET_
00691 conf_read_check (fscanf(fd,"%ld,",&temp), 1);
00692 conf_read_check (fscanf(fd, "%*[^\n]"), 0);
00693 conf_read_check (fscanf(fd,"%ld,",&temp), 1);
00694 conf_read_check (fscanf(fd, "%*[^\n]"), 0);
00695 conf_read_check (fscanf(fd,"%ld,",&temp), 1);
00696 conf_read_check (fscanf(fd, "%*[^\n]"), 0);
00697 conf_read_check (fscanf(fd,"%s",tempval), 1);
00698 conf_read_check (fscanf(fd,"%*[^\n]"), 0);
00699 #endif
00700
00701 conf_read_check (fscanf(fd,"%d",&p_Inp->conceal_mode), 1);
00702 conf_read_check (fscanf(fd,"%*[^\n]"), 0);
00703 p_Img->conceal_mode = p_Inp->conceal_mode;
00704 conf_read_check (fscanf(fd,"%d",&p_Inp->ref_poc_gap), 1);
00705 conf_read_check (fscanf(fd,"%*[^\n]"), 0);
00706 p_Img->ref_poc_gap = p_Inp->ref_poc_gap;
00707 conf_read_check (fscanf(fd,"%d",&p_Inp->poc_gap), 1);
00708 conf_read_check (fscanf(fd,"%*[^\n]"), 0);
00709 p_Img->poc_gap = p_Inp->poc_gap;
00710 conf_read_check (fscanf(fd,"%d,",&p_Inp->silent), 1);
00711 conf_read_check (fscanf(fd,"%*[^\n]"), 0);
00712 conf_read_check (fscanf(fd,"%d,",&p_Inp->intra_profile_deblocking), 1);
00713 conf_read_check (fscanf(fd,"%*[^\n]"), 0);
00714
00715 fclose (fd);
00716 }
00717
00732 static void Report(ImageParameters *p_Img)
00733 {
00734 pic_parameter_set_rbsp_t *active_pps = p_Img->active_pps;
00735 InputParameters *p_Inp = p_Img->p_Inp;
00736 SNRParameters *snr = p_Img->snr;
00737 #define OUTSTRING_SIZE 255
00738 char string[OUTSTRING_SIZE];
00739 FILE *p_log;
00740 static const char yuv_formats[4][4]= { {"400"}, {"420"}, {"422"}, {"444"} };
00741
00742 #ifndef WIN32
00743 time_t now;
00744 struct tm *l_time;
00745 #else
00746 char timebuf[128];
00747 #endif
00748
00749
00750 p_Img->tot_time = timenorm(p_Img->tot_time);
00751
00752 if (p_Inp->silent == FALSE)
00753 {
00754 fprintf(stdout,"-------------------- Average SNR all frames ------------------------------\n");
00755 fprintf(stdout," SNR Y(dB) : %5.2f\n",snr->snra[0]);
00756 fprintf(stdout," SNR U(dB) : %5.2f\n",snr->snra[1]);
00757 fprintf(stdout," SNR V(dB) : %5.2f\n",snr->snra[2]);
00758 fprintf(stdout," Total decoding time : %.3f sec (%.3f fps)\n",p_Img->tot_time*0.001,(snr->frame_ctr ) * 1000.0 / p_Img->tot_time);
00759 fprintf(stdout,"--------------------------------------------------------------------------\n");
00760 fprintf(stdout," Exit JM %s decoder, ver %s ",JM, VERSION);
00761 fprintf(stdout,"\n");
00762 }
00763 else
00764 {
00765 fprintf(stdout,"\n----------------------- Decoding Completed -------------------------------\n");
00766 fprintf(stdout," Total decoding time : %.3f sec (%.3f fps)\n",p_Img->tot_time*0.001, (snr->frame_ctr) * 1000.0 / p_Img->tot_time);
00767 fprintf(stdout,"--------------------------------------------------------------------------\n");
00768 fprintf(stdout," Exit JM %s decoder, ver %s ",JM, VERSION);
00769 fprintf(stdout,"\n");
00770 }
00771
00772
00773
00774 snprintf(string, OUTSTRING_SIZE, "%s", LOGFILE);
00775
00776 if ((p_log=fopen(string,"r"))==0)
00777 {
00778 if ((p_log=fopen(string,"a"))==0)
00779 {
00780 snprintf(errortext, ET_SIZE, "Error open file %s for appending",string);
00781 error(errortext, 500);
00782 }
00783 else
00784 {
00785 fprintf(p_log," -------------------------------------------------------------------------------------------------------------------\n");
00786 fprintf(p_log,"| Decoder statistics. This file is made first time, later runs are appended |\n");
00787 fprintf(p_log," ------------------------------------------------------------------------------------------------------------------- \n");
00788 fprintf(p_log,"| ver | Date | Time | Sequence |#Img| Format | YUV |Coding|SNRY 1|SNRU 1|SNRV 1|SNRY N|SNRU N|SNRV N|\n");
00789 fprintf(p_log," -------------------------------------------------------------------------------------------------------------------\n");
00790 }
00791 }
00792 else
00793 {
00794 fclose(p_log);
00795 p_log=fopen(string,"a");
00796 }
00797
00798 fprintf(p_log,"|%s/%-4s", VERSION, EXT_VERSION);
00799
00800 #ifdef WIN32
00801 _strdate( timebuf );
00802 fprintf(p_log,"| %1.5s |",timebuf );
00803
00804 _strtime( timebuf);
00805 fprintf(p_log," % 1.5s |",timebuf);
00806 #else
00807 now = time ((time_t *) NULL);
00808 time (&now);
00809 l_time = localtime (&now);
00810 strftime (string, sizeof string, "%d-%b-%Y", l_time);
00811 fprintf(p_log,"| %1.5s |",string );
00812
00813 strftime (string, sizeof string, "%H:%M:%S", l_time);
00814 fprintf(p_log,"| %1.5s |",string );
00815 #endif
00816
00817 fprintf(p_log,"%20.20s|",p_Inp->infile);
00818
00819 fprintf(p_log,"%3d |",p_Img->number);
00820 fprintf(p_log,"%4dx%-4d|", p_Img->width, p_Img->height);
00821 fprintf(p_log," %s |", &(yuv_formats[p_Img->yuv_format][0]));
00822
00823 if (active_pps)
00824 {
00825 if (active_pps->entropy_coding_mode_flag == CAVLC)
00826 fprintf(p_log," CAVLC|");
00827 else
00828 fprintf(p_log," CABAC|");
00829 }
00830
00831 fprintf(p_log,"%6.3f|",snr->snr1[0]);
00832 fprintf(p_log,"%6.3f|",snr->snr1[1]);
00833 fprintf(p_log,"%6.3f|",snr->snr1[2]);
00834 fprintf(p_log,"%6.3f|",snr->snra[0]);
00835 fprintf(p_log,"%6.3f|",snr->snra[1]);
00836 fprintf(p_log,"%6.3f|",snr->snra[2]);
00837 fprintf(p_log,"\n");
00838 fclose(p_log);
00839
00840 snprintf(string, OUTSTRING_SIZE,"%s", DATADECFILE);
00841 p_log=fopen(string,"a");
00842
00843 if(p_Img->Bframe_ctr != 0)
00844 {
00845 fprintf(p_log, "%3d %2d %2d %2.2f %2.2f %2.2f %5d "
00846 "%2.2f %2.2f %2.2f %5d "
00847 "%2.2f %2.2f %2.2f %5d %.3f\n",
00848 p_Img->number, 0, p_Img->qp,
00849 snr->snr1[0],
00850 snr->snr1[1],
00851 snr->snr1[2],
00852 0,
00853 0.0,
00854 0.0,
00855 0.0,
00856 0,
00857 snr->snra[0],
00858 snr->snra[1],
00859 snr->snra[2],
00860 0,
00861 (double)0.001*p_Img->tot_time/(p_Img->number + p_Img->Bframe_ctr - 1));
00862 }
00863 else
00864 {
00865 fprintf(p_log, "%3d %2d %2d %2.2f %2.2f %2.2f %5d "
00866 "%2.2f %2.2f %2.2f %5d "
00867 "%2.2f %2.2f %2.2f %5d %.3f\n",
00868 p_Img->number, 0, p_Img->qp,
00869 snr->snr1[0],
00870 snr->snr1[1],
00871 snr->snr1[2],
00872 0,
00873 0.0,
00874 0.0,
00875 0.0,
00876 0,
00877 snr->snra[0],
00878 snr->snra[1],
00879 snr->snra[2],
00880 0,
00881 (double)0.001*p_Img->tot_time/p_Img->number);
00882 }
00883 fclose(p_log);
00884 }
00885
00900 DataPartition *AllocPartition(int n)
00901 {
00902 DataPartition *partArr, *dataPart;
00903 int i;
00904
00905 partArr = (DataPartition *) calloc(n, sizeof(DataPartition));
00906 if (partArr == NULL)
00907 {
00908 snprintf(errortext, ET_SIZE, "AllocPartition: Memory allocation for Data Partition failed");
00909 error(errortext, 100);
00910 }
00911
00912 for (i=0; i<n; ++i)
00913 {
00914 dataPart = &(partArr[i]);
00915 dataPart->bitstream = (Bitstream *) calloc(1, sizeof(Bitstream));
00916 if (dataPart->bitstream == NULL)
00917 {
00918 snprintf(errortext, ET_SIZE, "AllocPartition: Memory allocation for Bitstream failed");
00919 error(errortext, 100);
00920 }
00921 dataPart->bitstream->streamBuffer = (byte *) calloc(MAX_CODED_FRAME_SIZE, sizeof(byte));
00922 if (dataPart->bitstream->streamBuffer == NULL)
00923 {
00924 snprintf(errortext, ET_SIZE, "AllocPartition: Memory allocation for streamBuffer failed");
00925 error(errortext, 100);
00926 }
00927 }
00928 return partArr;
00929 }
00930
00931
00932
00933
00951 void FreePartition (DataPartition *dp, int n)
00952 {
00953 int i;
00954
00955 assert (dp != NULL);
00956 assert (dp->bitstream != NULL);
00957 assert (dp->bitstream->streamBuffer != NULL);
00958 for (i=0; i<n; ++i)
00959 {
00960 free (dp[i].bitstream->streamBuffer);
00961 free (dp[i].bitstream);
00962 }
00963 free (dp);
00964 }
00965
00966
00977 static void malloc_slice(InputParameters *p_Inp, ImageParameters *p_Img)
00978 {
00979 int memory_size = 0;
00980 Slice *currSlice;
00981
00982 p_Img->currentSlice = (Slice *) calloc(1, sizeof(Slice));
00983 if ( (currSlice = p_Img->currentSlice) == NULL)
00984 {
00985 snprintf(errortext, ET_SIZE, "Memory allocation for Slice datastruct in NAL-mode %d failed", p_Inp->FileFormat);
00986 error(errortext,100);
00987 }
00988
00990
00991
00992
00993 currSlice->mot_ctx = create_contexts_MotionInfo();
00994 currSlice->tex_ctx = create_contexts_TextureInfo();
00995
00996
00997 currSlice->max_part_nr = 3;
00998 currSlice->partArr = AllocPartition(currSlice->max_part_nr);
00999 currSlice->p_colocated = NULL;
01000
01001 memory_size += get_mem3Dint(&(currSlice->wp_weight), 2, MAX_REFERENCE_PICTURES, 3);
01002 memory_size += get_mem3Dint(&(currSlice->wp_offset), 6, MAX_REFERENCE_PICTURES, 3);
01003 memory_size += get_mem4Dint(&(currSlice->wbp_weight), 6, MAX_REFERENCE_PICTURES, MAX_REFERENCE_PICTURES, 3);
01004
01005 memory_size += get_mem3Dpel(&(currSlice->mb_pred), MAX_PLANE, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
01006 memory_size += get_mem3Dpel(&(currSlice->mb_rec ), MAX_PLANE, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
01007 memory_size += get_mem3Dint(&(currSlice->mb_rres), MAX_PLANE, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
01008 memory_size += get_mem3Dint(&(currSlice->cof ), MAX_PLANE, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
01009
01010
01011 allocate_pred_mem(currSlice);
01012 }
01013
01014
01025 static void free_slice(Slice *currSlice)
01026 {
01027 free_pred_mem(currSlice);
01028
01029 free_mem3Dint(currSlice->cof );
01030 free_mem3Dint(currSlice->mb_rres);
01031 free_mem3Dpel(currSlice->mb_rec );
01032 free_mem3Dpel(currSlice->mb_pred);
01033
01034
01035 free_mem3Dint(currSlice->wp_weight );
01036 free_mem3Dint(currSlice->wp_offset );
01037 free_mem4Dint(currSlice->wbp_weight);
01038
01039 FreePartition (currSlice->partArr, 3);
01040
01041 if (1)
01042 {
01043
01044 delete_contexts_MotionInfo(currSlice->mot_ctx);
01045 delete_contexts_TextureInfo(currSlice->tex_ctx);
01046 }
01047 free(currSlice);
01048
01049 currSlice = NULL;
01050 }
01051
01066 int init_global_buffers(ImageParameters *p_Img)
01067 {
01068 int memory_size=0;
01069 int i;
01070
01071 if (p_Img->global_init_done)
01072 {
01073 free_global_buffers(p_Img);
01074 }
01075
01076
01077 memory_size += get_mem2Dpel(&p_Img->imgY_ref, p_Img->height, p_Img->width);
01078
01079 if (p_Img->active_sps->chroma_format_idc != YUV400)
01080 memory_size += get_mem3Dpel(&p_Img->imgUV_ref, 2, p_Img->height_cr, p_Img->width_cr);
01081 else
01082 p_Img->imgUV_ref=NULL;
01083
01084
01085 if( IS_INDEPENDENT(p_Img) )
01086 {
01087 for( i=0; i<MAX_PLANE; ++i )
01088 {
01089 if(((p_Img->mb_data_JV[i]) = (Macroblock *) calloc(p_Img->FrameSizeInMbs, sizeof(Macroblock))) == NULL)
01090 no_mem_exit("init_global_buffers: p_Img->mb_data");
01091 }
01092 p_Img->mb_data = NULL;
01093 }
01094 else
01095 {
01096 if(((p_Img->mb_data) = (Macroblock *) calloc(p_Img->FrameSizeInMbs, sizeof(Macroblock))) == NULL)
01097 no_mem_exit("init_global_buffers: p_Img->mb_data");
01098 }
01099
01100 if(((p_Img->intra_block) = (int*)calloc(p_Img->FrameSizeInMbs, sizeof(int))) == NULL)
01101 no_mem_exit("init_global_buffers: p_Img->intra_block");
01102
01103 memory_size += get_mem2Dint(&PicPos,p_Img->FrameSizeInMbs + 1,2);
01104
01105 for (i = 0; i < (int) p_Img->FrameSizeInMbs + 1;++i)
01106 {
01107 PicPos[i][0] = (i % p_Img->PicWidthInMbs);
01108 PicPos[i][1] = (i / p_Img->PicWidthInMbs);
01109 }
01110
01111 memory_size += get_mem2D(&(p_Img->ipredmode), 4*p_Img->FrameHeightInMbs, 4*p_Img->PicWidthInMbs);
01112
01113
01114 memory_size += get_mem4D(&(p_Img->nz_coeff), p_Img->FrameSizeInMbs, 3, BLOCK_SIZE, BLOCK_SIZE);
01115
01116 memory_size += get_mem2Dint(&(p_Img->siblock), p_Img->FrameHeightInMbs, p_Img->PicWidthInMbs);
01117
01118 init_qp_process(p_Img);
01119
01120 p_Img->global_init_done = 1;
01121
01122 p_Img->oldFrameSizeInMbs = p_Img->FrameSizeInMbs;
01123
01124 return (memory_size);
01125 }
01126
01142 void free_global_buffers(ImageParameters *p_Img)
01143 {
01144 free_mem2Dpel (p_Img->imgY_ref);
01145
01146 if (p_Img->imgUV_ref)
01147 free_mem3Dpel (p_Img->imgUV_ref);
01148
01149
01150 free_mem4D(p_Img->nz_coeff);
01151
01152 free_mem2Dint(p_Img->siblock);
01153
01154
01155 if (p_Img->mb_data != NULL)
01156 free(p_Img->mb_data);
01157
01158 free_mem2Dint(PicPos);
01159
01160 free (p_Img->intra_block);
01161 free_mem2D(p_Img->ipredmode);
01162
01163 free_qp_matrices(p_Img);
01164
01165 p_Img->global_init_done = 0;
01166
01167 }
01168
01169 void report_stats_on_error(void)
01170 {
01171
01172 exit (-1);
01173 }