00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #include "contributors.h"
00046
00047 #include <time.h>
00048 #include <math.h>
00049 #include <sys/timeb.h>
00050 #include "global.h"
00051
00052 #include "cconv_yuv2rgb.h"
00053 #include "configfile.h"
00054 #include "context_ini.h"
00055 #include "explicit_gop.h"
00056 #include "explicit_seq.h"
00057 #include "filehandle.h"
00058 #include "image.h"
00059 #include "input.h"
00060 #include "img_io.h"
00061 #include "slice.h"
00062 #include "intrarefresh.h"
00063 #include "leaky_bucket.h"
00064 #include "memalloc.h"
00065 #include "me_epzs.h"
00066 #include "me_umhex.h"
00067 #include "me_umhexsmp.h"
00068 #include "output.h"
00069 #include "parset.h"
00070 #include "q_matrix.h"
00071 #include "q_offsets.h"
00072 #include "ratectl.h"
00073 #include "report.h"
00074 #include "rdoq.h"
00075 #include "errdo.h"
00076 #include "rdopt.h"
00077 #include "wp_mcprec.h"
00078 #include "mv-search.h"
00079
00080 InputParameters *params = NULL;
00081 ImageParameters *img = NULL;
00082
00083
00084 int initial_Bframes;
00085
00086 int cabac_encoding;
00087 int frame_statistic_start;
00088 extern ColocatedParams *Co_located;
00089 extern ColocatedParams *Co_located_JV[MAX_PLANE];
00090 extern double *mb16x16_cost_frame;
00091
00092 extern int allocate_block_mem(void);
00093 extern void free_block_mem(void);
00094 extern int allocate_mb_mem(void);
00095 extern void free_mb_mem(void);
00096
00097 static void SetLevelIndices(void);
00098 static void chroma_mc_setup(void);
00099 static int get_mem_mv (short*******);
00100 static int get_mem_bipred_mv (short********);
00101 static void free_mem_bipred_mv (short*******);
00102 static void free_mem_mv (short******);
00103 static void init_img( ImageParameters *img, InputParameters *params);
00104 static void init_poc(void);
00105
00106 static void init_encoder(ImageParameters *img);
00107 static void encode_sequence(ImageParameters *img);
00108
00109
00110 void init_stats (StatParameters *stats)
00111 {
00112 memset(stats, 0, sizeof(StatParameters));
00113 stats->NumberBFrames = params->NumberBFrames;
00114 }
00115
00116 void init_dstats (DistortionParams *dist)
00117 {
00118 dist->frame_ctr = 0;
00119 memset(dist->metric, 0, TOTAL_DIST_TYPES * sizeof(DistMetric));
00120 }
00121
00122
00123
00124
00125
00126
00127
00128 static void init_frame_params(ImageParameters *p_img)
00129 {
00130 int base_mul = 0;
00131
00132 if (params->idr_period)
00133 {
00134 if (!params->adaptive_idr_period && ( p_img->frm_number - p_img->lastIDRnumber ) % params->idr_period == 0 )
00135 p_img->nal_reference_idc = NALU_PRIORITY_HIGHEST;
00136
00137 if (params->adaptive_idr_period == 1 && ( p_img->frm_number - imax(p_img->lastIntraNumber, p_img->lastIDRnumber) ) % params->idr_period == 0 )
00138 p_img->nal_reference_idc = NALU_PRIORITY_HIGHEST;
00139 else
00140 p_img->nal_reference_idc = (params->DisposableP) ? (p_img->frm_number + 1)% 2 : NALU_PRIORITY_LOW;
00141 }
00142 else
00143 p_img->nal_reference_idc = (p_img->frm_number && params->DisposableP) ? (p_img->frm_number + 1)% 2 : NALU_PRIORITY_LOW;
00144
00145
00146
00147 if (params->idr_period)
00148 {
00149 if (!params->adaptive_idr_period)
00150 base_mul = ( p_img->frm_number - p_img->lastIDRnumber ) % params->idr_period;
00151 else if (params->adaptive_idr_period == 1)
00152 base_mul = (( p_img->frm_number - imax(p_img->lastIntraNumber, p_img->lastIDRnumber) ) % params->idr_period == 0) ? 0 : ( p_img->frm_number - p_img->lastIDRnumber );
00153 }
00154 else
00155 base_mul = ( p_img->frm_number - p_img->lastIDRnumber );
00156
00157 if ((p_img->frm_number - p_img->lastIDRnumber) <= params->intra_delay)
00158 {
00159 base_mul = -base_mul;
00160 }
00161 else
00162 {
00163 base_mul -= ( base_mul ? params->intra_delay : 0);
00164 }
00165
00166 p_img->toppoc = base_mul * (2 * p_img->base_dist);
00167
00168 if ((params->PicInterlace==FRAME_CODING) && (params->MbInterlace==FRAME_CODING))
00169 p_img->bottompoc = p_img->toppoc;
00170 else
00171 p_img->bottompoc = p_img->toppoc + 1;
00172
00173 p_img->framepoc = imin (p_img->toppoc, p_img->bottompoc);
00174
00175
00176 p_img->delta_pic_order_cnt[0] = 0;
00177
00178 if ((params->BRefPictures == 1) && (p_img->frm_number))
00179 {
00180 p_img->delta_pic_order_cnt[0] = 2 * params->NumberBFrames;
00181 }
00182
00183 if (params->NumberBFrames && params->last_frame && ((p_img->gop_number) + 1) == params->no_frm_base)
00184 {
00185 int bi = (int)((float)p_img->base_dist / (initial_Bframes + 1.0) + 0.499999);
00186 int new_bframes = ((params->last_frame - (p_img->frm_number - 1) * p_img->base_dist) / bi) - 1;
00187
00188
00189 p_img->delta_pic_order_cnt[0]= -2*(initial_Bframes - new_bframes);
00190 p_img->toppoc += p_img->delta_pic_order_cnt[0];
00191 p_img->bottompoc += p_img->delta_pic_order_cnt[0];
00192 p_img->framepoc = imin (p_img->toppoc, p_img->bottompoc);
00193 }
00194
00195
00196 if (params->idr_period && ((!params->adaptive_idr_period && ( p_img->frm_number - p_img->lastIDRnumber ) % params->idr_period == 0)
00197 || (params->adaptive_idr_period == 1 && ( p_img->frm_number - imax(p_img->lastIntraNumber, p_img->lastIDRnumber) ) % params->idr_period == 0)) )
00198 {
00199 p_img->frame_num = 0;
00200 }
00201 }
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211 static void alloc_img( ImageParameters **p_img)
00212 {
00213 if ((*p_img = (ImageParameters *) calloc(1, sizeof(ImageParameters)))==NULL)
00214 no_mem_exit("alloc_img: p_img");
00215
00216 get_mem3Dint(&((*p_img)->mb_rres), MAX_PLANE, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
00217 get_mem3Dint(&((*p_img)->mb_ores), MAX_PLANE, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
00218 get_mem4Dpel(&((*p_img)->mpr_4x4), MAX_PLANE, 9, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
00219 get_mem4Dpel(&((*p_img)->mpr_8x8), MAX_PLANE, 9, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
00220 get_mem4Dpel(&((*p_img)->mpr_16x16), MAX_PLANE, 5, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
00221 get_mem3Dpel(&((*p_img)->mb_pred), MAX_PLANE, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
00222 }
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237 int main(int argc, char **argv)
00238 {
00239 p_dec = -1;
00240
00241 alloc_img( &img);
00242 allocate_block_mem();
00243 allocate_mb_mem();
00244
00245 params = (InputParameters *) malloc(sizeof(InputParameters));
00246
00247 Configure (argc, argv);
00248
00249
00250 init_encoder(img);
00251
00252
00253 encode_sequence(img);
00254
00255
00256 free_mb_mem();
00257 free_block_mem();
00258 free_encoder_memory(img);
00259
00260 if ( params != NULL )
00261 free( params );
00262
00263 return 0;
00264 }
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274 static void init_encoder(ImageParameters *img)
00275 {
00276 giRDOpt_B8OnlyFlag = 0;
00277
00278 p_log = NULL;
00279
00280 cabac_encoding = 0;
00281
00282 frame_statistic_start = 1;
00283
00284
00285 OpenFiles(¶ms->input_file1);
00286
00287 Init_QMatrix();
00288 Init_QOffsetMatrix();
00289
00290 init_poc();
00291 GenerateParameterSets();
00292 SetLevelIndices();
00293
00294 init_img(img, params);
00295 init_rdopt(params);
00296
00297
00298 if (params->rdopt == 3)
00299 {
00300 init_error_conceal(params->ErrorConcealment);
00301 }
00302
00303 #ifdef _LEAKYBUCKET_
00304 initial_Bframes = 0;
00305 Bit_Buffer = (long *)malloc((params->no_frames + 1) * sizeof(long));
00306 total_frame_buffer = 0;
00307 #endif
00308
00309
00310
00311 if (params->HierarchicalCoding)
00312 {
00313 init_gop_structure(params);
00314 if (params->NumberBFrames && params->HierarchicalCoding == 3)
00315 interpret_gop_structure();
00316 else
00317 create_hierarchy(params);
00318 }
00319
00320 dpb.init_done = 0;
00321 init_dpb();
00322 init_out_buffer();
00323 init_stats (stats);
00324 init_dstats(dist);
00325
00326 enc_picture = NULL;
00327
00328 init_global_buffers();
00329
00330 if ( params->WPMCPrecision )
00331 {
00332 wpxInitWPXPasses(params);
00333 }
00334
00335 create_context_memory ();
00336 Init_Motion_Search_Module ();
00337 information_init(img, params, stats);
00338
00339 if(params->DistortionYUVtoRGB)
00340 init_YUVtoRGB();
00341
00342
00343 if (params->RCEnable)
00344 rc_init_sequence();
00345
00346 img->last_valid_reference = 0;
00347 tot_time = 0;
00348
00349 initial_Bframes = params->NumberBFrames;
00350
00351 PatchInputNoFrames();
00352
00353 img->type = I_SLICE;
00354
00355 stats->bit_ctr_parametersets = 0;
00356 stats->bit_slice = start_sequence();
00357
00358 if (params->UseRDOQuant)
00359 precalculate_unary_exp_golomb_level();
00360
00361 if (params->ExplicitSeqCoding)
00362 OpenExplicitSeqFile(params);
00363 }
00364
00365
00366
00367
00368
00369
00370
00371 static int determine_coding_level(ImageParameters *p_img, InputParameters *pparams, int curr_frame)
00372 {
00373 int coding_level = 0;
00374
00375 if (curr_frame - p_img->last_idr_number == 0)
00376 return coding_level;
00377 else
00378 coding_level = (curr_frame - p_img->last_idr_number - 1) % (1 + pparams->NumberBFrames);
00379
00380 return coding_level;
00381 }
00382
00383
00384
00385
00386
00387
00388
00389 static void SetImgType(ImageParameters *img, int gop_frame_num)
00390 {
00391 if (gop_frame_num == 0)
00392 {
00393 int intra_refresh = (params->intra_period == 0) ? (img->gop_number == 0) : (( ( img->frm_number - img->lastIntraNumber) % params->intra_period ) == 0);
00394 int idr_refresh;
00395
00396 if ( params->idr_period && !params->adaptive_idr_period )
00397 idr_refresh = (( ( img->frm_number - img->lastIDRnumber ) % params->idr_period ) == 0);
00398 else if ( params->idr_period && params->adaptive_idr_period == 1 )
00399 idr_refresh = (( ( img->frm_number - imax(img->lastIntraNumber, img->lastIDRnumber) ) % params->idr_period ) == 0);
00400 else
00401 idr_refresh = (img->gop_number == 0);
00402
00403 if (intra_refresh || idr_refresh)
00404 {
00405 set_slice_type( img, I_SLICE );
00406 }
00407 else
00408 {
00409 set_slice_type(img, (params->sp_periodicity && ((img->gop_number % params->sp_periodicity) == 0))
00410 ? SP_SLICE : ((params->BRefPictures == 2) ? B_SLICE : P_SLICE) );
00411 }
00412 }
00413 else
00414 {
00415 if (params->HierarchicalCoding)
00416 set_slice_type( img, gop_structure[gop_frame_num - 1].slice_type);
00417 else
00418 set_slice_type( img, ( params->PReplaceBSlice ) ? P_SLICE : B_SLICE);
00419 }
00420 }
00421
00422 static void set_poc(ImageParameters *img, double frame_to_code)
00423 {
00424 img->toppoc = (int) (img->frame_interval * frame_to_code);
00425 if (img->gop_number && (img->gop_number <= params->intra_delay))
00426 {
00427 if(params->idr_period && ((!params->adaptive_idr_period && ( img->frm_number - img->lastIDRnumber ) % params->idr_period == 0)
00428 || (params->adaptive_idr_period == 1 && ( img->frm_number - imax(img->lastIntraNumber, img->lastIDRnumber) ) % params->idr_period == 0)) )
00429 img->toppoc = img->toppoc;
00430 else
00431 {
00432 img->toppoc += (-img->frm_number + img->lastIDRnumber) * img->base_dist;
00433 img->toppoc *= 2;
00434 }
00435 }
00436 else
00437 {
00438 if(params->idr_period && !params->adaptive_idr_period)
00439 img->toppoc += ((( img->frm_number - img->lastIDRnumber - params->intra_delay) % params->idr_period ) - 1) * img->base_dist;
00440 else
00441 img->toppoc += (( img->frm_number - img->lastIDRnumber - params->intra_delay - 1 ) * img->base_dist);
00442 img->toppoc *= 2;
00443 }
00444 }
00445
00446
00447
00448
00449
00450
00451
00452 static void prepare_first_layer(ImageParameters *p_img, int curr_frame_to_code)
00453 {
00454 p_img->number ++;
00455 p_img->gop_number = (p_img->number - p_img->start_frame_no);
00456 p_img->frm_number = p_img->number;
00457
00458 p_img->frm_no_in_file = CalculateFrameNumber(p_img);
00459
00460 if (params->last_frame != 0 && (params->last_frame == p_img->frm_no_in_file) && params->HierarchicalCoding)
00461 {
00462 int numberBFrames = params->NumberBFrames;
00463 params->HierarchicalCoding = imin( 2, params->HierarchicalCoding);
00464 params->NumberBFrames = params->no_frames - curr_frame_to_code - 1;
00465
00466 clear_gop_structure();
00467 init_gop_structure(params);
00468 create_hierarchy(params);
00469
00470 params->NumberBFrames = numberBFrames;
00471 }
00472 SetImgType(p_img, 0);
00473
00474 init_frame_params(p_img);
00475
00476
00477 if (params->RCEnable && p_img->type == I_SLICE)
00478 rc_init_gop_params();
00479
00480
00481 p_img->layer = (p_img->gop_number % (params->NumFramesInELSubSeq + 1)) ? 0 : 1;
00482 }
00483
00484
00485
00486
00487
00488
00489
00490 static void prepare_second_layer(ImageParameters *p_img, int enh_frame_to_code)
00491 {
00492 p_img->layer = (params->NumFramesInELSubSeq == 0) ? 0 : 1;
00493 SetImgType(p_img, enh_frame_to_code);
00494
00495 if ((p_img->gop_number > 0) && (params->EnableIDRGOP == 0 || p_img->idr_gop_number))
00496 {
00497 if (params->HierarchicalCoding)
00498 {
00499 p_img->nal_reference_idc = gop_structure[enh_frame_to_code - 1].reference_idc;
00500 set_poc(p_img, (double)(1 + gop_structure[enh_frame_to_code - 1].display_no));
00501
00502 if (p_img->gop_number && (p_img->gop_number <= params->intra_delay))
00503 {
00504 if (enh_frame_to_code == 1)
00505 p_img->delta_pic_order_cnt[0] = p_img->toppoc - 2*(p_img->start_tr_gop + (params->intra_delay - p_img->gop_number)*(p_img->base_dist));
00506 else
00507 p_img->delta_pic_order_cnt[0] = p_img->toppoc - 2*(p_img->start_tr_gop + (params->intra_delay - p_img->gop_number)*(p_img->base_dist)
00508 + (int) (2.0 * p_img->frame_interval * (double) (1 + gop_structure[enh_frame_to_code - 2].display_no)));
00509 }
00510 else
00511 {
00512 if (enh_frame_to_code == 1)
00513 p_img->delta_pic_order_cnt[0] = p_img->toppoc - 2*(p_img->start_tr_gop + (p_img->frm_number - p_img->lastIDRnumber)*(p_img->base_dist));
00514 else
00515 p_img->delta_pic_order_cnt[0] = p_img->toppoc - 2*(p_img->start_tr_gop + (p_img->frm_number - p_img->lastIDRnumber - 1)*(p_img->base_dist)
00516 + (int) (2.0 * p_img->frame_interval * (double) (1+ gop_structure[enh_frame_to_code - 2].display_no)));
00517 }
00518 }
00519 else
00520 {
00521 p_img->nal_reference_idc = (params->BRefPictures == 1 ) ? NALU_PRIORITY_LOW : NALU_PRIORITY_DISPOSABLE;
00522 set_poc(p_img, (double)enh_frame_to_code);
00523
00524
00525 if (params->BRefPictures != 1)
00526 {
00527 p_img->delta_pic_order_cnt[0]= 2 * (enh_frame_to_code - 1);
00528 }
00529 else
00530 {
00531 p_img->delta_pic_order_cnt[0]= -2;
00532 }
00533 }
00534
00535 p_img->delta_pic_order_cnt[1]= 0;
00536
00537 if ((params->PicInterlace==FRAME_CODING)&&(params->MbInterlace==FRAME_CODING))
00538 p_img->bottompoc = p_img->toppoc;
00539 else
00540 p_img->bottompoc = p_img->toppoc + 1;
00541
00542 p_img->framepoc = imin (p_img->toppoc, p_img->bottompoc);
00543 p_img->frm_no_in_file = CalculateFrameNumber(p_img);
00544 }
00545 }
00546
00547
00548
00549
00550
00551
00552
00553 static void encode_sequence(ImageParameters *img)
00554 {
00555 int HierarchicalCoding = params->HierarchicalCoding;
00556 int NumberBFrames = params->NumberBFrames;
00557 int jumpd = params->jumpd;
00558 int curr_frame_to_code = 0;
00559 int enh_frame_to_code = 0;
00560
00561 for (curr_frame_to_code = 0; curr_frame_to_code < params->no_frames; curr_frame_to_code++)
00562 {
00563
00564 if (img->last_ref_idc == 1)
00565 {
00566 img->frame_num++;
00567 img->frame_num %= max_frame_num;
00568 }
00569
00570
00571 if (params->ExplicitSeqCoding)
00572 {
00573 ExpFrameInfo *info = &expSeq->info[curr_frame_to_code % expSeq->no_frames];
00574 ReadExplicitSeqFile(expSeq, curr_frame_to_code);
00575 ExplicitUpdateImgParams(info, img);
00576 img->b_frame_to_code = 0;
00577 }
00578 else
00579 {
00580 enh_frame_to_code = determine_coding_level(img, params, curr_frame_to_code);
00581 img->b_frame_to_code = enh_frame_to_code;
00582
00583 if (enh_frame_to_code == 0)
00584 prepare_first_layer(img, curr_frame_to_code);
00585 else
00586 {
00587 prepare_second_layer(img, enh_frame_to_code);
00588 }
00589 }
00590
00591
00592 if (params->redundant_pic_flag)
00593 {
00594 Init_redundant_frame();
00595 Set_redundant_frame();
00596 }
00597
00598 encode_one_frame(img);
00599
00600 img->last_ref_idc = img->nal_reference_idc ? 1 : 0;
00601
00602
00603 if (params->redundant_pic_flag && key_frame)
00604 {
00605 encode_one_redundant_frame();
00606 }
00607
00608 if (img->type == I_SLICE && params->EnableOpenGOP)
00609 img->last_valid_reference = img->ThisPOC;
00610
00611 if (img->currentPicture->idr_flag)
00612 {
00613 img->idr_gop_number = 0;
00614 }
00615 else
00616 img->idr_gop_number ++;
00617
00618 if (params->ReportFrameStats)
00619 report_frame_statistic();
00620 }
00621
00622 params->HierarchicalCoding = HierarchicalCoding;
00623 params->NumberBFrames = NumberBFrames;
00624 params->jumpd = jumpd;
00625 }
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635 void free_encoder_memory(ImageParameters *img)
00636 {
00637 int nplane;
00638 terminate_sequence();
00639
00640 flush_dpb();
00641
00642 CloseFiles(¶ms->input_file1);
00643
00644 if (-1 != p_dec)
00645 close(p_dec);
00646 if (p_trace)
00647 fclose(p_trace);
00648
00649 Clear_Motion_Search_Module ();
00650
00651 RandomIntraUninit();
00652 FmoUninit();
00653
00654 if (params->HierarchicalCoding)
00655 clear_gop_structure ();
00656
00657
00658 clear_rdopt (params);
00659
00660 #ifdef _LEAKYBUCKET_
00661 calc_buffer();
00662 #endif
00663
00664
00665 report(img, params, stats);
00666
00667 #ifdef _LEAKYBUCKET_
00668 if (Bit_Buffer != NULL)
00669 {
00670 free(Bit_Buffer);
00671 Bit_Buffer = NULL;
00672 }
00673 #endif
00674
00675 free_dpb();
00676
00677 if( IS_INDEPENDENT(params) )
00678 {
00679 for( nplane=0; nplane<MAX_PLANE; nplane++ )
00680 {
00681 free_colocated(Co_located_JV[nplane]);
00682 }
00683 }
00684 else
00685 {
00686 free_colocated(Co_located);
00687 }
00688
00689 uninit_out_buffer();
00690
00691 free_global_buffers();
00692
00693
00694 free_img ();
00695 free_context_memory ();
00696 FreeParameterSets();
00697 if (params->ExplicitSeqCoding)
00698 CloseExplicitSeqFile();
00699 }
00700
00701
00702
00703
00704
00705
00706
00707
00708 void init_poc()
00709 {
00710
00711
00712
00713 img->pic_order_cnt_type=params->pic_order_cnt_type;
00714
00715 img->delta_pic_order_always_zero_flag = FALSE;
00716 img->num_ref_frames_in_pic_order_cnt_cycle= 1;
00717
00718 if (params->BRefPictures == 1)
00719 {
00720 img->offset_for_non_ref_pic = 0;
00721 img->offset_for_ref_frame[0] = 2;
00722 }
00723 else
00724 {
00725 img->offset_for_non_ref_pic = -2*(params->NumberBFrames);
00726 img->offset_for_ref_frame[0] = 2*(params->NumberBFrames + 1);
00727 }
00728
00729 if ((params->PicInterlace==FRAME_CODING) && (params->MbInterlace==FRAME_CODING))
00730 {
00731 img->offset_for_top_to_bottom_field = 0;
00732 img->pic_order_present_flag = FALSE;
00733 img->delta_pic_order_cnt_bottom = 0;
00734 }
00735 else
00736 {
00737 img->offset_for_top_to_bottom_field = 1;
00738 img->pic_order_present_flag = TRUE;
00739 img->delta_pic_order_cnt_bottom = 1;
00740 }
00741 }
00742
00743
00744 static inline int alloc_rddata(ImageParameters *img, RD_DATA *rd_data)
00745 {
00746 int alloc_size = 0;
00747
00748 alloc_size += get_mem2Dpel(&(rd_data->rec_mbY), MB_BLOCK_SIZE, MB_BLOCK_SIZE);
00749 alloc_size += get_mem3Dpel(&(rd_data->rec_mb_cr), 2, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
00750
00751 alloc_size += get_mem_ACcoeff (&(rd_data->cofAC));
00752 alloc_size += get_mem_DCcoeff (&(rd_data->cofDC));
00753
00754 if (!params->IntraProfile)
00755 {
00756 alloc_size += get_mem_mv (&(rd_data->all_mv));
00757 }
00758
00759 alloc_size += get_mem2D((byte***)&(rd_data->ipredmode), img->height_blk, img->width_blk);
00760 alloc_size += get_mem3D((byte****)&(rd_data->refar), 2, 4, 4);
00761
00762 return alloc_size;
00763 }
00764
00765 static inline void free_rddata(RD_DATA *rd_data)
00766 {
00767 free_mem3D((byte***) rd_data->refar);
00768 free_mem2D((byte**) rd_data->ipredmode);
00769
00770 if (!params->IntraProfile)
00771 {
00772 free_mem_mv (rd_data->all_mv);
00773 }
00774 free_mem_DCcoeff (rd_data->cofDC);
00775 free_mem_ACcoeff (rd_data->cofAC);
00776
00777 free_mem3Dpel(rd_data->rec_mb_cr);
00778 free_mem2Dpel(rd_data->rec_mbY);
00779 }
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793 void CAVLC_init(void)
00794 {
00795 memset(&img->nz_coeff[0][0][0], 0, img->PicSizeInMbs * 4 * (4 + img->num_blk8x8_uv)* sizeof(int));
00796 }
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809 static void init_img( ImageParameters *img, InputParameters *params)
00810 {
00811 int i, j;
00812 int byte_abs_range;
00813
00814 static int mb_width_cr[4] = {0,8, 8,16};
00815 static int mb_height_cr[4]= {0,8,16,16};
00816
00817 img->number = -1;
00818 img->start_frame_no = 0;
00819 img->gop_number = (img->number - img->start_frame_no);
00820 img->start_tr_gop = 0;
00821
00822 img->last_idr_number = 0;
00823
00824 img->yuv_format = params->output.yuv_format;
00825 img->P444_joined = (img->yuv_format == YUV444 && !IS_INDEPENDENT(params));
00826
00827
00828 img->bitdepth_luma = params->output.bit_depth[0];
00829 img->bitdepth_scale[0] = 1 << (img->bitdepth_luma - 8);
00830 img->bitdepth_lambda_scale = 2 * (img->bitdepth_luma - 8);
00831 img->bitdepth_luma_qp_scale = 3 * img->bitdepth_lambda_scale;
00832 img->dc_pred_value_comp[0] = 1<<(img->bitdepth_luma - 1);
00833 img->max_imgpel_value_comp[0] = (1<<img->bitdepth_luma) - 1;
00834 img->max_imgpel_value_comp_sq[0] = img->max_imgpel_value_comp[0] * img->max_imgpel_value_comp[0];
00835
00836 img->dc_pred_value = img->dc_pred_value_comp[0];
00837 img->max_imgpel_value = img->max_imgpel_value_comp[0];
00838 img->mb_size[0][0] = img->mb_size[0][1] = MB_BLOCK_SIZE;
00839
00840
00841 img->RCMinQP = params->RCMinQP[P_SLICE];
00842 img->RCMaxQP = params->RCMaxQP[P_SLICE];
00843
00844
00845 img->curr_res = img->mb_rres[0];
00846 img->curr_prd = img->mb_pred[0];
00847 if (active_sps->profile_idc == 66 || active_sps->profile_idc == 77 || active_sps->profile_idc == 88)
00848 img->min_IPCM_value = 1;
00849 else
00850 img->min_IPCM_value = 0;
00851
00852 if (img->yuv_format != YUV400)
00853 {
00854 img->bitdepth_chroma = params->output.bit_depth[1];
00855 img->bitdepth_scale[1] = 1 << (img->bitdepth_chroma - 8);
00856 img->dc_pred_value_comp[1] = 1<<(img->bitdepth_chroma - 1);
00857 img->dc_pred_value_comp[2] = img->dc_pred_value_comp[1];
00858 img->max_imgpel_value_comp[1] = (1<<img->bitdepth_chroma) - 1;
00859 img->max_imgpel_value_comp[2] = img->max_imgpel_value_comp[1];
00860 img->max_imgpel_value_comp_sq[1] = img->max_imgpel_value_comp[1] * img->max_imgpel_value_comp[1];
00861 img->max_imgpel_value_comp_sq[2] = img->max_imgpel_value_comp[2] * img->max_imgpel_value_comp[2];
00862 img->num_blk8x8_uv = (1<<img->yuv_format)&(~(0x1));
00863 img->num_cdc_coeff = img->num_blk8x8_uv << 1;
00864
00865 img->mb_size[1][0] = img->mb_size[2][0] = img->mb_cr_size_x = (img->yuv_format == YUV420 || img->yuv_format == YUV422) ? 8 : 16;
00866 img->mb_size[1][1] = img->mb_size[2][1] = img->mb_cr_size_y = (img->yuv_format == YUV444 || img->yuv_format == YUV422) ? 16 : 8;
00867
00868 img->bitdepth_chroma_qp_scale = 6*(img->bitdepth_chroma - 8);
00869
00870 img->chroma_qp_offset[0] = active_pps->cb_qp_index_offset;
00871 img->chroma_qp_offset[1] = active_pps->cr_qp_index_offset;
00872 }
00873 else
00874 {
00875 img->bitdepth_chroma = 0;
00876 img->bitdepth_scale[1] = 0;
00877 img->max_imgpel_value_comp[1] = 0;
00878 img->max_imgpel_value_comp[2] = img->max_imgpel_value_comp[1];
00879 img->max_imgpel_value_comp_sq[1] = img->max_imgpel_value_comp[1] * img->max_imgpel_value_comp[1];
00880 img->max_imgpel_value_comp_sq[2] = img->max_imgpel_value_comp[2] * img->max_imgpel_value_comp[2];
00881 img->num_blk8x8_uv = 0;
00882 img->num_cdc_coeff = 0;
00883 img->mb_size[1][0] = img->mb_size[2][0] = img->mb_cr_size_x = 0;
00884 img->mb_size[1][1] = img->mb_size[2][1] = img->mb_cr_size_y = 0;
00885
00886 img->bitdepth_chroma_qp_scale = 0;
00887 img->bitdepth_chroma_qp_scale = 0;
00888
00889 img->chroma_qp_offset[0] = 0;
00890 img->chroma_qp_offset[1] = 0;
00891 }
00892
00893
00894 img->pic_unit_size_on_disk = (imax(params->source.bit_depth[0], params->source.bit_depth[1]) > 8) ? 16 : 8;
00895 img->out_unit_size_on_disk = (imax(params->output.bit_depth[0], params->output.bit_depth[1]) > 8) ? 16 : 8;
00896
00897 img->max_bitCount = 128 + 256 * img->bitdepth_luma + 2 * img->mb_cr_size_y * img->mb_cr_size_x * img->bitdepth_chroma;
00898
00899
00900 img->max_qp_delta = (25 + (img->bitdepth_luma_qp_scale>>1));
00901 img->min_qp_delta = img->max_qp_delta + 1;
00902
00903 img->num_ref_frames = active_sps->num_ref_frames;
00904 img->max_num_references = active_sps->frame_mbs_only_flag ? active_sps->num_ref_frames : 2 * active_sps->num_ref_frames;
00905
00906 img->buf_cycle = params->num_ref_frames;
00907 img->base_dist = params->jumpd + 1;
00908
00909
00910 img->lastIntraNumber = 0;
00911 img->lastINTRA = 0;
00912 img->lastIDRnumber = 0;
00913 img->last_ref_idc = 0;
00914 img->idr_refresh = 0;
00915 img->idr_gop_number = 0;
00916 img->rewind_frame = 0;
00917
00918 img->DeblockCall = 0;
00919 img->framerate = (float) params->output.frame_rate;
00920
00921
00922 if (!params->IntraProfile)
00923 {
00924 get_mem_mv (&(img->all_mv));
00925
00926 if (params->BiPredMotionEstimation)
00927 {
00928 get_mem_bipred_mv(&(img->bipred_mv));
00929 }
00930 }
00931
00932 get_mem_ACcoeff (&(img->cofAC));
00933 get_mem_DCcoeff (&(img->cofDC));
00934
00935 if (params->AdaptiveRounding)
00936 {
00937 if (img->yuv_format != 0)
00938 {
00939 get_mem4Dint(&(img->ARCofAdj4x4), 3, MAXMODE, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
00940 get_mem4Dint(&(img->ARCofAdj8x8), img->P444_joined ? 3 : 1, MAXMODE, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
00941 }
00942 else
00943 {
00944 get_mem4Dint(&(img->ARCofAdj4x4), 1, MAXMODE, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
00945 get_mem4Dint(&(img->ARCofAdj8x8), 1, MAXMODE, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
00946 }
00947 }
00948
00949 byte_abs_range = (imax(img->max_imgpel_value_comp[0], img->max_imgpel_value_comp[1]) + 1) * 2;
00950
00951 if ((img->quad = (int*)calloc (byte_abs_range, sizeof(int))) == NULL)
00952 no_mem_exit ("init_img: img->quad");
00953
00954 img->quad += byte_abs_range/2;
00955 for (i=0; i < byte_abs_range/2; ++i)
00956 {
00957 img->quad[i] = img->quad[-i] = i * i;
00958 }
00959
00960 img->width = (params->output.width + img->auto_crop_right);
00961 img->height = (params->output.height + img->auto_crop_bottom);
00962 img->width_blk = img->width / BLOCK_SIZE;
00963 img->height_blk = img->height / BLOCK_SIZE;
00964 img->width_padded = img->width + 2 * IMG_PAD_SIZE;
00965 img->height_padded = img->height + 2 * IMG_PAD_SIZE;
00966
00967 if (img->yuv_format != YUV400)
00968 {
00969 img->width_cr = img->width * mb_width_cr [img->yuv_format] / 16;
00970 img->height_cr= img->height * mb_height_cr[img->yuv_format] / 16;
00971 }
00972 else
00973 {
00974 img->width_cr = 0;
00975 img->height_cr= 0;
00976 }
00977
00978 img->height_cr_frame = img->height_cr;
00979
00980 img->size = img->width * img->height;
00981 img->size_cr = img->width_cr * img->height_cr;
00982
00983 img->PicWidthInMbs = img->width / MB_BLOCK_SIZE;
00984 img->FrameHeightInMbs = img->height / MB_BLOCK_SIZE;
00985 img->FrameSizeInMbs = img->PicWidthInMbs * img->FrameHeightInMbs;
00986
00987 img->PicHeightInMapUnits = ( active_sps->frame_mbs_only_flag ? img->FrameHeightInMbs : img->FrameHeightInMbs/2 );
00988
00989 if(params->MbInterlace)
00990 {
00991 alloc_rddata(img, &rddata_top_frame_mb);
00992 alloc_rddata(img, &rddata_bot_frame_mb);
00993 if ( params->MbInterlace != FRAME_MB_PAIR_CODING )
00994 {
00995 alloc_rddata(img, &rddata_top_field_mb);
00996 alloc_rddata(img, &rddata_bot_field_mb);
00997 }
00998 }
00999
01000 if (params->UseRDOQuant && params->RDOQ_QP_Num > 1)
01001 {
01002 alloc_rddata(img, &rddata_trellis_curr);
01003 alloc_rddata(img, &rddata_trellis_best);
01004
01005 if (!params->IntraProfile)
01006 {
01007 if (params->Transform8x8Mode && params->RDOQ_CP_MV)
01008 {
01009 get_mem5Dshort(&tmp_mv8, 2, img->max_num_references, 4, 4, 2);
01010 get_mem3Dint (&motion_cost8, 2, img->max_num_references, 4);
01011 }
01012 }
01013 }
01014
01015 if ((img->b8x8info = (Block8x8Info *) calloc(1, sizeof(Block8x8Info))) == NULL)
01016 no_mem_exit("init_img: img->block8x8info");
01017
01018 if( IS_INDEPENDENT(params) )
01019 {
01020 for( i = 0; i < MAX_PLANE; i++ ){
01021 if ((img->mb_data_JV[i] = (Macroblock *) calloc(img->FrameSizeInMbs,sizeof(Macroblock))) == NULL)
01022 no_mem_exit("init_img: img->mb_data_JV");
01023 }
01024 img->mb_data = NULL;
01025 }
01026 else
01027 {
01028 if ((img->mb_data = (Macroblock *) calloc(img->FrameSizeInMbs, sizeof(Macroblock))) == NULL)
01029 no_mem_exit("init_img: img->mb_data");
01030 }
01031
01032 if (params->UseConstrainedIntraPred)
01033 {
01034 if ((img->intra_block = (int*)calloc(img->FrameSizeInMbs, sizeof(int))) == NULL)
01035 no_mem_exit("init_img: img->intra_block");
01036 }
01037
01038 if (params->CtxAdptLagrangeMult == 1)
01039 {
01040 if ((mb16x16_cost_frame = (double*)calloc(img->FrameSizeInMbs, sizeof(double))) == NULL)
01041 {
01042 no_mem_exit("init mb16x16_cost_frame");
01043 }
01044 }
01045 get_mem2D((byte***)&(img->ipredmode), img->height_blk, img->width_blk);
01046 get_mem2D((byte***)&(img->ipredmode8x8), img->height_blk, img->width_blk);
01047 memset(&(img->ipredmode[0][0]) , -1, img->height_blk * img->width_blk *sizeof(char));
01048 memset(&(img->ipredmode8x8[0][0]), -1, img->height_blk * img->width_blk *sizeof(char));
01049
01050
01051 if (params->MbInterlace)
01052 {
01053 get_mem2D((byte***)&(rddata_bot_frame_mb.ipredmode), img->height_blk, img->width_blk);
01054 get_mem2D((byte***)&(rddata_top_field_mb.ipredmode), img->height_blk, img->width_blk);
01055 get_mem2D((byte***)&(rddata_bot_field_mb.ipredmode), img->height_blk, img->width_blk);
01056 }
01057
01058
01059 get_mem3Dint(&(img->nz_coeff), img->FrameSizeInMbs, 4, 4+img->num_blk8x8_uv);
01060
01061 get_mem2Dolm (&(img->lambda), 10, 52 + img->bitdepth_luma_qp_scale, img->bitdepth_luma_qp_scale);
01062 get_mem2Dodouble (&(img->lambda_md), 10, 52 + img->bitdepth_luma_qp_scale, img->bitdepth_luma_qp_scale);
01063 get_mem3Dodouble (&(img->lambda_me), 10, 52 + img->bitdepth_luma_qp_scale, 3, img->bitdepth_luma_qp_scale);
01064 get_mem3Doint (&(img->lambda_mf), 10, 52 + img->bitdepth_luma_qp_scale, 3, img->bitdepth_luma_qp_scale);
01065
01066 if (params->CtxAdptLagrangeMult == 1)
01067 {
01068 get_mem2Dodouble(&(img->lambda_mf_factor), 10, 52 + img->bitdepth_luma_qp_scale, img->bitdepth_luma_qp_scale);
01069 }
01070
01071 img->b_frame_to_code = 0;
01072 img->GopLevels = (params->NumberBFrames) ? 1 : 0;
01073 img->mb_y_upd=0;
01074
01075 RandomIntraInit (img->PicWidthInMbs, img->FrameHeightInMbs, params->RandomIntraMBRefresh);
01076
01077 InitSEIMessages();
01078
01079 initInput(¶ms->source, ¶ms->output);
01080
01081
01082 AllocateFrameMemory(img, params, params->source.size);
01083
01084
01085
01086
01087 if (params->DFSendParameters)
01088 {
01089 for (j = 0; j < 2; j++)
01090 {
01091 for (i = 0; i < NUM_SLICE_TYPES; i++)
01092 {
01093 params->DFAlpha[j][i] <<= 1;
01094 params->DFBeta [j][i] <<= 1;
01095 }
01096 }
01097 }
01098 else
01099 {
01100 for (j = 0; j < 2; j++)
01101 {
01102 for (i = 0; i < NUM_SLICE_TYPES; i++)
01103 {
01104 params->DFDisableIdc[j][i] = 0;
01105 params->DFAlpha [j][i] = 0;
01106 params->DFBeta [j][i] = 0;
01107 }
01108 }
01109 }
01110
01111 img->ChromaArrayType = params->separate_colour_plane_flag ? 0 : params->output.yuv_format;
01112 img->colour_plane_id = 0;
01113
01114 if (params->RDPictureDecision)
01115 img->frm_iter = 3;
01116 else
01117 img->frm_iter = 1;
01118
01119 img->frame_interval = (double) (params->frame_skip + 1);
01120 }
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131 void free_img ()
01132 {
01133
01134 DeleteFrameMemory();
01135
01136 CloseSEIMessages();
01137 if (!params->IntraProfile)
01138 {
01139 free_mem_mv (img->all_mv);
01140
01141 if (params->BiPredMotionEstimation)
01142 {
01143 free_mem_bipred_mv (img->bipred_mv);
01144 }
01145 }
01146
01147 free_mem_ACcoeff (img->cofAC);
01148 free_mem_DCcoeff (img->cofDC);
01149
01150 if (params->AdaptiveRounding)
01151 {
01152 free_mem4Dint(img->ARCofAdj4x4);
01153 free_mem4Dint(img->ARCofAdj8x8);
01154 }
01155
01156
01157 if (params->MbInterlace)
01158 {
01159 free_rddata(&rddata_top_frame_mb);
01160 free_rddata(&rddata_bot_frame_mb);
01161
01162 if ( params->MbInterlace != FRAME_MB_PAIR_CODING )
01163 {
01164 free_rddata(&rddata_top_field_mb);
01165 free_rddata(&rddata_bot_field_mb);
01166 }
01167 }
01168
01169 if (params->UseRDOQuant && params->RDOQ_QP_Num > 1)
01170 {
01171 free_rddata(&rddata_trellis_curr);
01172 free_rddata(&rddata_trellis_best);
01173
01174 if (!params->IntraProfile)
01175 {
01176 if (params->Transform8x8Mode && params->RDOQ_CP_MV)
01177 {
01178 free_mem5Dshort(tmp_mv8);
01179 free_mem3Dint(motion_cost8);
01180 }
01181 }
01182 }
01183
01184 free (img->quad - (imax(img->max_imgpel_value_comp[0],img->max_imgpel_value_comp[1]) + 1));
01185
01186 free_mem3Dpel(img->mb_pred );
01187 free_mem4Dpel(img->mpr_16x16);
01188 free_mem4Dpel(img->mpr_8x8 );
01189 free_mem4Dpel(img->mpr_4x4 );
01190 free_mem3Dint(img->mb_ores );
01191 free_mem3Dint(img->mb_rres );
01192 free (img);
01193 }
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205 Picture *malloc_picture()
01206 {
01207 Picture *pic;
01208 if ((pic = calloc (1, sizeof (Picture))) == NULL) no_mem_exit ("malloc_picture: Picture structure");
01209
01210 return pic;
01211 }
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221 void free_picture(Picture *pic)
01222 {
01223 if (pic != NULL)
01224 {
01225 free_slice_list(pic);
01226 free (pic);
01227 }
01228 }
01229
01230
01231
01232
01233
01234
01235
01236
01237 int init_orig_buffers(ImageData *imgData)
01238 {
01239 int memory_size = 0;
01240 int nplane;
01241
01242
01243 if( IS_INDEPENDENT(params) )
01244 {
01245 for( nplane=0; nplane<MAX_PLANE; nplane++ )
01246 {
01247 imgData->format = params->output;
01248 imgData->format.width = img->width;
01249 imgData->format.height = img->height;
01250 imgData->format.width_cr = img->width_cr;
01251 imgData->format.height_cr = img->height_cr;
01252 memory_size += get_mem2Dpel(&(imgData->frm_data[nplane]), img->height, img->width);
01253 }
01254 }
01255 else
01256 {
01257
01258 imgData->format = params->output;
01259 imgData->format.width = img->width;
01260 imgData->format.height = img->height;
01261 imgData->format.width_cr = img->width_cr;
01262 imgData->format.height_cr = img->height_cr;
01263 memory_size += get_mem2Dpel(&(imgData->frm_data[0]), img->height, img->width);
01264
01265 if (img->yuv_format != YUV400)
01266 {
01267 int i, j, k;
01268 memory_size += get_mem2Dpel(&(imgData->frm_data[1]), img->height_cr, img->width_cr);
01269 memory_size += get_mem2Dpel(&(imgData->frm_data[2]), img->height_cr, img->width_cr);
01270
01271 if (sizeof(imgpel) == sizeof(unsigned char))
01272 {
01273 for (k = 1; k < 3; k++)
01274 memset(&(imgData->frm_data[k][0][0]), 128, img->height_cr * img->width_cr * sizeof(imgpel));
01275 }
01276 else
01277 {
01278 for (k = 1; k < 3; k++)
01279 for (j = 0; j < img->height_cr; j++)
01280 for (i = 0; i < img->width_cr; i++)
01281 imgData->frm_data[k][j][i] = 128;
01282 }
01283 }
01284 }
01285
01286 if (!active_sps->frame_mbs_only_flag)
01287 {
01288
01289 memory_size += init_top_bot_planes(imgData->frm_data[0], img->height, img->width, &(imgData->top_data[0]), &(imgData->bot_data[0]));
01290
01291 if (img->yuv_format != YUV400)
01292 {
01293
01294 memory_size += 4*(sizeof(imgpel**));
01295
01296 memory_size += init_top_bot_planes(imgData->frm_data[1], img->height_cr, img->width_cr, &(imgData->top_data[1]), &(imgData->bot_data[1]));
01297 memory_size += init_top_bot_planes(imgData->frm_data[2], img->height_cr, img->width_cr, &(imgData->top_data[2]), &(imgData->bot_data[2]));
01298 }
01299 }
01300 return memory_size;
01301 }
01302
01303
01304
01305
01306
01307
01308
01309
01310
01311
01312
01313
01314
01315 int init_global_buffers(void)
01316 {
01317 int j, memory_size=0;
01318
01319 if ((enc_frame_picture = (StorablePicture**)malloc(6 * sizeof(StorablePicture*))) == NULL)
01320 no_mem_exit("init_global_buffers: *enc_frame_picture");
01321
01322 for (j = 0; j < 6; j++)
01323 enc_frame_picture[j] = NULL;
01324
01325 if ((enc_field_picture = (StorablePicture**)malloc(2 * sizeof(StorablePicture*))) == NULL)
01326 no_mem_exit("init_global_buffers: *enc_field_picture");
01327
01328 for (j = 0; j < 2; j++)
01329 enc_field_picture[j] = NULL;
01330
01331 if ((frame_pic = (Picture**)malloc(img->frm_iter * sizeof(Picture*))) == NULL)
01332 no_mem_exit("init_global_buffers: *frame_pic");
01333
01334 for (j = 0; j < img->frm_iter; j++)
01335 frame_pic[j] = malloc_picture();
01336
01337 if (params->si_frame_indicator || params->sp_periodicity)
01338 {
01339 si_frame_indicator=0;
01340 number_sp2_frames=0;
01341
01342 frame_pic_si = malloc_picture();
01343
01344 get_mem2Dint (&lrec, img->height, img->width);
01345 get_mem3Dint (&lrec_uv, 2, img->height, img->width);
01346 }
01347
01348
01349 if (params->PicInterlace != FRAME_CODING)
01350 {
01351 if ((field_pic = (Picture**)malloc(2 * sizeof(Picture*))) == NULL)
01352 no_mem_exit("init_global_buffers: *field_pic");
01353
01354 for (j = 0; j < 2; j++)
01355 field_pic[j] = malloc_picture();
01356 }
01357
01358
01359 memory_size += init_orig_buffers(&imgData);
01360 memory_size += init_orig_buffers(&imgData1);
01361
01362 memory_size += get_mem2Dint(&PicPos, img->FrameSizeInMbs + 1, 2);
01363
01364 for (j = 0; j < (int) img->FrameSizeInMbs + 1; j++)
01365 {
01366 PicPos[j][0] = (j % img->PicWidthInMbs);
01367 PicPos[j][1] = (j / img->PicWidthInMbs);
01368 }
01369
01370 if (params->WeightedPrediction || params->WeightedBiprediction || params->GenerateMultiplePPS)
01371 {
01372
01373 memory_size += get_mem3Dint(&wp_weight, 6, MAX_REFERENCE_PICTURES, 3);
01374 memory_size += get_mem3Dint(&wp_offset, 6, MAX_REFERENCE_PICTURES, 3);
01375
01376 memory_size += get_mem4Dint(&wbp_weight, 6, MAX_REFERENCE_PICTURES, MAX_REFERENCE_PICTURES, 3);
01377 }
01378
01379
01380
01381 if ((params->NumberBFrames != 0) || (params->BRefPictures > 0) || (params->ProfileIDC != 66))
01382 {
01383 memory_size += get_mem3D((byte ****)(void*)&direct_ref_idx, 2, img->height_blk, img->width_blk);
01384 memory_size += get_mem2D((byte ***) (void*)&direct_pdir , img->height_blk, img->width_blk);
01385 }
01386
01387 if (params->rdopt == 3)
01388 {
01389 memory_size += allocate_errdo_mem(params);
01390 }
01391
01392 if (params->RestrictRef)
01393 {
01394 memory_size += get_mem2D(&pixel_map, img->height, img->width);
01395 memory_size += get_mem2D(&refresh_map, img->height/8, img->width/8);
01396 }
01397
01398 if (!active_sps->frame_mbs_only_flag)
01399 {
01400 memory_size += get_mem2Dpel(&imgY_com, img->height, img->width);
01401
01402 if (img->yuv_format != YUV400)
01403 {
01404 memory_size += get_mem3Dpel(&imgUV_com, 2, img->height_cr, img->width_cr);
01405 }
01406 }
01407
01408
01409 if (!params->IntraProfile)
01410 {
01411 if (params->SearchMode == UM_HEX)
01412 {
01413 memory_size += UMHEX_get_mem();
01414 }
01415 else if (params->SearchMode == UM_HEX_SIMPLE)
01416 {
01417 smpUMHEX_init();
01418 memory_size += smpUMHEX_get_mem();
01419 }
01420 else if (params->SearchMode == EPZS)
01421 memory_size += EPZSInit(params, img);
01422 }
01423
01424 if (params->RCEnable)
01425 rc_allocate_memory();
01426
01427 if (params->redundant_pic_flag)
01428 {
01429 memory_size += get_mem2Dpel(&imgY_tmp, img->height, img->width);
01430 memory_size += get_mem2Dpel(&imgUV_tmp[0], img->height_cr, img->width_cr);
01431 memory_size += get_mem2Dpel(&imgUV_tmp[1], img->height_cr, img->width_cr);
01432 }
01433
01434 memory_size += get_mem2Dint (&imgY_sub_tmp, img->height_padded, img->width_padded);
01435
01436 if ( params->ChromaMCBuffer )
01437 chroma_mc_setup();
01438
01439 img_padded_size_x = (img->width + 2 * IMG_PAD_SIZE);
01440 img_padded_size_x2 = (img_padded_size_x << 1);
01441 img_padded_size_x4 = (img_padded_size_x << 2);
01442 img_padded_size_x_m8 = (img_padded_size_x - 8);
01443 img_padded_size_x_m8x8 = (img_padded_size_x - BLOCK_SIZE_8x8);
01444 img_padded_size_x_m4x4 = (img_padded_size_x - BLOCK_SIZE);
01445 img_cr_padded_size_x = (img->width_cr + 2 * img_pad_size_uv_x);
01446 img_cr_padded_size_x2 = (img_cr_padded_size_x << 1);
01447 img_cr_padded_size_x4 = (img_cr_padded_size_x << 2);
01448 img_cr_padded_size_x_m8 = (img_cr_padded_size_x - 8);
01449
01450
01451
01452
01453 if(params->DistortionYUVtoRGB)
01454 {
01455 memory_size += create_RGB_memory(img);
01456 }
01457
01458 pWPX = NULL;
01459 if ( params->WPMCPrecision )
01460 {
01461 wpxInitWPXObject();
01462 }
01463
01464 return memory_size;
01465 }
01466
01467
01468
01469
01470
01471
01472
01473
01474 void free_orig_planes(ImageData *imgData)
01475 {
01476 if( IS_INDEPENDENT(params) )
01477 {
01478 int nplane;
01479 for( nplane=0; nplane<MAX_PLANE; nplane++ )
01480 {
01481 free_mem2Dpel(imgData->frm_data[nplane]);
01482 }
01483 }
01484 else
01485 {
01486 free_mem2Dpel(imgData->frm_data[0]);
01487
01488 if (img->yuv_format != YUV400)
01489 {
01490 free_mem2Dpel(imgData->frm_data[1]);
01491 free_mem2Dpel(imgData->frm_data[2]);
01492 }
01493 }
01494
01495 if (!active_sps->frame_mbs_only_flag)
01496 {
01497 free_top_bot_planes(imgData->top_data[0], imgData->bot_data[0]);
01498
01499 if (img->yuv_format != YUV400)
01500 {
01501 free_top_bot_planes(imgData->top_data[1], imgData->bot_data[1]);
01502 free_top_bot_planes(imgData->top_data[2], imgData->bot_data[2]);
01503 }
01504 }
01505 }
01506
01507
01508
01509
01510
01511
01512
01513
01514
01515
01516
01517
01518
01519
01520
01521 void free_global_buffers(void)
01522 {
01523 int i,j;
01524
01525 if (enc_frame_picture)
01526 free (enc_frame_picture);
01527 if (frame_pic)
01528 {
01529 for (j = 0; j < img->frm_iter; j++)
01530 {
01531 if (frame_pic[j])
01532 free_picture (frame_pic[j]);
01533 }
01534 free (frame_pic);
01535 }
01536
01537 if (enc_field_picture)
01538 free (enc_field_picture);
01539 if (field_pic)
01540 {
01541 for (j = 0; j < 2; j++)
01542 {
01543 if (field_pic[j])
01544 free_picture (field_pic[j]);
01545 }
01546 free (field_pic);
01547 }
01548
01549
01550 if (params->si_frame_indicator || params->sp_periodicity)
01551 {
01552 free_picture (frame_pic_si);
01553
01554 free_mem2Dint (lrec);
01555 free_mem3Dint (lrec_uv);
01556 }
01557
01558 free_orig_planes(&imgData);
01559 free_orig_planes(&imgData1);
01560
01561
01562 free_mem2Dint(PicPos);
01563
01564 free_QMatrix();
01565 free_QOffsets();
01566
01567 if ( params->WPMCPrecision )
01568 {
01569 wpxFreeWPXObject();
01570 }
01571
01572 if (params->WeightedPrediction || params->WeightedBiprediction || params->GenerateMultiplePPS)
01573 {
01574 free_mem3Dint(wp_weight );
01575 free_mem3Dint(wp_offset );
01576 free_mem4Dint(wbp_weight);
01577 }
01578
01579 if ((stats->NumberBFrames != 0) || (params->BRefPictures > 0)||(params->ProfileIDC != 66))
01580 {
01581 free_mem3D((byte ***)direct_ref_idx);
01582 free_mem2D((byte **) direct_pdir);
01583 }
01584
01585 if (imgY_sub_tmp)
01586 {
01587 free_mem2Dint (imgY_sub_tmp);
01588 imgY_sub_tmp=NULL;
01589 }
01590
01591
01592
01593 free_mem2D((byte**)img->ipredmode);
01594 free_mem2D((byte**)img->ipredmode8x8);
01595 free(img->b8x8info);
01596
01597 if( IS_INDEPENDENT(params) )
01598 {
01599 for( i=0; i<MAX_PLANE; i++ ){
01600 free(img->mb_data_JV[i]);
01601 }
01602 }
01603 else
01604 {
01605 free(img->mb_data);
01606 }
01607
01608 if(params->UseConstrainedIntraPred)
01609 {
01610 free (img->intra_block);
01611 }
01612
01613 if (params->CtxAdptLagrangeMult == 1)
01614 {
01615 free(mb16x16_cost_frame);
01616 }
01617
01618 if (params->rdopt == 3)
01619 {
01620 free_errdo_mem();
01621 }
01622
01623 if (params->RestrictRef)
01624 {
01625 free(pixel_map[0]);
01626 free(pixel_map);
01627 free(refresh_map[0]);
01628 free(refresh_map);
01629 }
01630
01631 if (!active_sps->frame_mbs_only_flag)
01632 {
01633 free_mem2Dpel(imgY_com);
01634
01635 if (img->yuv_format != YUV400)
01636 {
01637 free_mem3Dpel(imgUV_com);
01638 }
01639 }
01640
01641 free_mem3Dint(img->nz_coeff);
01642
01643 free_mem2Dolm (img->lambda, img->bitdepth_luma_qp_scale);
01644 free_mem2Dodouble (img->lambda_md, img->bitdepth_luma_qp_scale);
01645 free_mem3Dodouble (img->lambda_me, 10, 52 + img->bitdepth_luma_qp_scale, img->bitdepth_luma_qp_scale);
01646 free_mem3Doint (img->lambda_mf, 10, 52 + img->bitdepth_luma_qp_scale, img->bitdepth_luma_qp_scale);
01647
01648 if (params->CtxAdptLagrangeMult == 1)
01649 {
01650 free_mem2Dodouble(img->lambda_mf_factor, img->bitdepth_luma_qp_scale);
01651 }
01652
01653 if (!params->IntraProfile)
01654 {
01655 if (params->SearchMode == UM_HEX)
01656 {
01657 UMHEX_free_mem();
01658 }
01659 else if (params->SearchMode == UM_HEX_SIMPLE)
01660 {
01661 smpUMHEX_free_mem();
01662 }
01663 else if (params->SearchMode == EPZS)
01664 {
01665 EPZSDelete(params);
01666 }
01667 }
01668
01669
01670 if (params->RCEnable)
01671 rc_free_memory();
01672
01673 if (params->redundant_pic_flag)
01674 {
01675 free_mem2Dpel(imgY_tmp);
01676 free_mem2Dpel(imgUV_tmp[0]);
01677 free_mem2Dpel(imgUV_tmp[1]);
01678 }
01679
01680
01681
01682 if(params->DistortionYUVtoRGB)
01683 {
01684 delete_RGB_memory();
01685 }
01686 }
01687
01688
01689
01690
01691
01692
01693
01694
01695
01696
01697
01698 static int get_mem_mv (short ******* mv)
01699 {
01700
01701 get_mem6Dshort(mv, 2, img->max_num_references, 9, 4, 4, 2);
01702
01703 return 2 * img->max_num_references * 9 * 4 * 4 * 2 * sizeof(short);
01704 }
01705
01706
01707
01708
01709
01710
01711
01712
01713
01714
01715
01716 static int get_mem_bipred_mv (short******** bipred_mv)
01717 {
01718 get_mem7Dshort(bipred_mv, 2, 2, img->max_num_references, 9, 4, 4, 2);
01719
01720 return 2 * 2 * img->max_num_references * 9 * 4 * 4 * 2 * sizeof(short);
01721 }
01722
01723
01724
01725
01726
01727
01728
01729
01730
01731 static void free_mem_mv (short****** mv)
01732 {
01733 free_mem6Dshort(mv);
01734 }
01735
01736
01737
01738
01739
01740
01741
01742
01743
01744
01745 static void free_mem_bipred_mv (short******* bipred_mv)
01746 {
01747 free_mem7Dshort(bipred_mv);
01748 }
01749
01750
01751
01752
01753
01754
01755
01756 int get_mem_ACcoeff (int***** cofAC)
01757 {
01758 int num_blk8x8 = BLOCK_SIZE + img->num_blk8x8_uv;
01759
01760 get_mem4Dint(cofAC, num_blk8x8, BLOCK_SIZE, 2, 65);
01761 return num_blk8x8 * BLOCK_SIZE * 2 * 65 * sizeof(int);
01762 }
01763
01764
01765
01766
01767
01768
01769
01770 int get_mem_ACcoeff_new (int****** cofAC, int chroma)
01771 {
01772 get_mem5Dint(cofAC, BLOCK_SIZE, chroma, BLOCK_SIZE, 2, 65);
01773 return chroma * BLOCK_SIZE * BLOCK_SIZE * 2 * 65 * sizeof(int);
01774 }
01775
01776
01777
01778
01779
01780
01781
01782 int get_mem_DCcoeff (int**** cofDC)
01783 {
01784 get_mem3Dint(cofDC, 3, 2, 18);
01785 return 3 * 2 * 18 * sizeof(int);
01786 }
01787
01788
01789
01790
01791
01792
01793
01794
01795 void free_mem_ACcoeff (int**** cofAC)
01796 {
01797 free_mem4Dint(cofAC);
01798 }
01799
01800
01801
01802
01803
01804
01805
01806 void free_mem_ACcoeff_new (int***** cofAC)
01807 {
01808 free_mem5Dint(cofAC);
01809 }
01810
01811
01812
01813
01814
01815
01816
01817 void free_mem_DCcoeff (int*** cofDC)
01818 {
01819 free_mem3Dint(cofDC);
01820 }
01821
01822
01823
01824
01825
01826
01827
01828
01829 static void SetLevelIndices(void)
01830 {
01831 switch(active_sps->level_idc)
01832 {
01833 case 9:
01834 img->LevelIndex=1;
01835 break;
01836 case 10:
01837 img->LevelIndex=0;
01838 break;
01839 case 11:
01840 if (!IS_FREXT_PROFILE(active_sps->profile_idc) && (active_sps->constrained_set3_flag == 0))
01841 img->LevelIndex=2;
01842 else
01843 img->LevelIndex=1;
01844 break;
01845 case 12:
01846 img->LevelIndex=3;
01847 break;
01848 case 13:
01849 img->LevelIndex=4;
01850 break;
01851 case 20:
01852 img->LevelIndex=5;
01853 break;
01854 case 21:
01855 img->LevelIndex=6;
01856 break;
01857 case 22:
01858 img->LevelIndex=7;
01859 break;
01860 case 30:
01861 img->LevelIndex=8;
01862 break;
01863 case 31:
01864 img->LevelIndex=9;
01865 break;
01866 case 32:
01867 img->LevelIndex=10;
01868 break;
01869 case 40:
01870 img->LevelIndex=11;
01871 break;
01872 case 41:
01873 img->LevelIndex=12;
01874 break;
01875 case 42:
01876 if (!IS_FREXT_PROFILE(active_sps->profile_idc))
01877 img->LevelIndex=13;
01878 else
01879 img->LevelIndex=14;
01880 break;
01881 case 50:
01882 img->LevelIndex=15;
01883 break;
01884 case 51:
01885 img->LevelIndex=16;
01886 break;
01887 default:
01888 fprintf ( stderr, "Warning: unknown LevelIDC, using maximum level 5.1 \n" );
01889 img->LevelIndex=16;
01890 break;
01891 }
01892 }
01893
01894
01895
01896
01897
01898
01899
01900 void Init_redundant_frame()
01901 {
01902 if (params->redundant_pic_flag)
01903 {
01904 if (params->NumberBFrames)
01905 {
01906 error("B frame not supported when redundant picture used!", 100);
01907 }
01908
01909 if (params->PicInterlace)
01910 {
01911 error("Interlace not supported when redundant picture used!", 100);
01912 }
01913
01914 if (params->num_ref_frames < params->PrimaryGOPLength)
01915 {
01916 error("NumberReferenceFrames must be no less than PrimaryGOPLength", 100);
01917 }
01918
01919 if ((1<<params->NumRedundantHierarchy) > params->PrimaryGOPLength)
01920 {
01921 error("PrimaryGOPLength must be greater than 2^NumRedundantHeirarchy", 100);
01922 }
01923
01924 if (params->Verbose != 1)
01925 {
01926 error("Redundant slices not supported when Verbose != 1", 100);
01927 }
01928 }
01929
01930 key_frame = 0;
01931 redundant_coding = 0;
01932 img->redundant_pic_cnt = 0;
01933 frameNuminGOP = img->frm_number % params->PrimaryGOPLength;
01934 if (img->frm_number == 0)
01935 {
01936 frameNuminGOP = -1;
01937 }
01938 }
01939
01940
01941
01942
01943
01944
01945
01946 void Set_redundant_frame()
01947 {
01948 int GOPlength = params->PrimaryGOPLength;
01949
01950
01951 if (frameNuminGOP == 0)
01952 {
01953 redundant_coding = 0;
01954 key_frame = 1;
01955 redundant_ref_idx = GOPlength;
01956 }
01957
01958
01959 if (params->NumRedundantHierarchy > 0)
01960 {
01961 if (frameNuminGOP == GOPlength/2)
01962 {
01963 redundant_coding = 0;
01964 key_frame = 1;
01965 redundant_ref_idx = GOPlength/2;
01966 }
01967 }
01968
01969
01970 if (params->NumRedundantHierarchy > 1)
01971 {
01972 if (frameNuminGOP == GOPlength/4 || frameNuminGOP == GOPlength*3/4)
01973 {
01974 redundant_coding = 0;
01975 key_frame = 1;
01976 redundant_ref_idx = GOPlength/4;
01977 }
01978 }
01979
01980
01981 if (params->NumRedundantHierarchy > 2)
01982 {
01983 if (frameNuminGOP == GOPlength/8 || frameNuminGOP == GOPlength*3/8
01984 || frameNuminGOP == GOPlength*5/8 || frameNuminGOP == GOPlength*7/8)
01985 {
01986 redundant_coding = 0;
01987 key_frame = 1;
01988 redundant_ref_idx = GOPlength/8;
01989 }
01990 }
01991
01992
01993 if (params->NumRedundantHierarchy > 3)
01994 {
01995 if (frameNuminGOP == GOPlength/16 || frameNuminGOP == GOPlength*3/16
01996 || frameNuminGOP == GOPlength*5/16 || frameNuminGOP == GOPlength*7/16
01997 || frameNuminGOP == GOPlength*9/16 || frameNuminGOP == GOPlength*11/16
01998 || frameNuminGOP == GOPlength*13/16)
01999 {
02000 redundant_coding = 0;
02001 key_frame = 1;
02002 redundant_ref_idx = GOPlength/16;
02003 }
02004 }
02005 }
02006
02007
02008
02009
02010
02011
02012
02013 void encode_one_redundant_frame()
02014 {
02015 key_frame = 0;
02016 redundant_coding = 1;
02017 img->redundant_pic_cnt = 1;
02018
02019 if (!img->currentPicture->idr_flag)
02020 {
02021 if (img->type == I_SLICE)
02022 {
02023 set_slice_type( img, P_SLICE );
02024 }
02025 }
02026
02027 encode_one_frame(img);
02028 }
02029
02030
02031
02032
02033
02034
02035
02036 static void chroma_mc_setup(void)
02037 {
02038
02039 if ( img->yuv_format == YUV420 )
02040 {
02041 img_pad_size_uv_x = IMG_PAD_SIZE >> 1;
02042 img_pad_size_uv_y = IMG_PAD_SIZE >> 1;
02043 chroma_mask_mv_y = 7;
02044 chroma_mask_mv_x = 7;
02045 chroma_shift_x = 3;
02046 chroma_shift_y = 3;
02047 }
02048 else if ( img->yuv_format == YUV422 )
02049 {
02050 img_pad_size_uv_x = IMG_PAD_SIZE >> 1;
02051 img_pad_size_uv_y = IMG_PAD_SIZE;
02052 chroma_mask_mv_y = 3;
02053 chroma_mask_mv_x = 7;
02054 chroma_shift_y = 2;
02055 chroma_shift_x = 3;
02056 }
02057 else
02058 {
02059 img_pad_size_uv_x = IMG_PAD_SIZE;
02060 img_pad_size_uv_y = IMG_PAD_SIZE;
02061 chroma_mask_mv_y = 3;
02062 chroma_mask_mv_x = 3;
02063 chroma_shift_y = 2;
02064 chroma_shift_x = 2;
02065 }
02066 shift_cr_y = chroma_shift_y - 2;
02067 shift_cr_x = chroma_shift_x - 2;
02068 }
02069