00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "contributors.h"
00019
00020 #include <math.h>
00021 #include <float.h>
00022
00023 #include "global.h"
00024 #include "header.h"
00025 #include "nal.h"
00026 #include "rtp.h"
00027 #include "fmo.h"
00028 #include "vlc.h"
00029 #include "image.h"
00030 #include "cabac.h"
00031 #include "biariencode.h"
00032 #include "elements.h"
00033 #include "macroblock.h"
00034 #include "memalloc.h"
00035 #include "symbol.h"
00036 #include "context_ini.h"
00037 #include "enc_statistics.h"
00038 #include "ratectl.h"
00039 #include "me_epzs.h"
00040 #include "me_epzs_int.h"
00041 #include "wp.h"
00042 #include "slice.h"
00043 #include "rdoq.h"
00044 #include "wp_mcprec.h"
00045 #include "q_offsets.h"
00046 #include "conformance.h"
00047 #include "list_reorder.h"
00048 #include "md_common.h"
00049 #include "mmco.h"
00050 #include "mv_search.h"
00051 #include "quant4x4.h"
00052 #include "quant8x8.h"
00053 #include "quantChroma.h"
00054 #include "rdopt.h"
00055 #include "rdopt_coding_state.h"
00056
00057
00058 static Slice *malloc_slice(ImageParameters *p_Img, InputParameters *p_Inp);
00059 static void free_slice (Slice *currSlice);
00060 static void set_ref_pic_num(Slice *currSlice);
00061
00062
00063
00064 int allocate_block_mem(Slice *currSlice)
00065 {
00066 int alloc_size = 0;
00067 alloc_size += get_mem2Dint(&currSlice->tblk4x4, BLOCK_SIZE, BLOCK_SIZE);
00068 alloc_size += get_mem2Dint(&currSlice->tblk16x16, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
00069 alloc_size += get_mem4Dint(&currSlice->i16blk4x4, BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
00070
00071 return (alloc_size);
00072 }
00073
00074
00075 void free_block_mem(Slice *currSlice)
00076 {
00077 free_mem4Dint(currSlice->i16blk4x4);
00078 free_mem2Dint(currSlice->tblk16x16);
00079 free_mem2Dint(currSlice->tblk4x4);
00080 }
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 static void CAVLC_init(Slice *currSlice)
00095 {
00096 memset(&currSlice->p_Img->nz_coeff[0][0][0], 0, currSlice->PicSizeInMbs * 4 * (4 + currSlice->num_blk8x8_uv)* sizeof(int));
00097 }
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 static int get_mem_mv (Slice *currSlice, short ******* mv)
00110 {
00111
00112 get_mem6Dshort(mv, 2, currSlice->max_num_references, 9, 4, 4, 2);
00113
00114 return 576 * currSlice->max_num_references * sizeof(short);
00115 }
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 static int get_mem_bipred_mv (Slice *currSlice, short******** bipred_mv)
00128 {
00129 get_mem7Dshort(bipred_mv, 2, 2, currSlice->max_num_references, 9, 4, 4, 2);
00130
00131 return 1152 * currSlice->max_num_references * sizeof(short);
00132 }
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 static void free_mem_mv (short****** mv)
00143 {
00144 free_mem6Dshort(mv);
00145 }
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 static void free_mem_bipred_mv (short******* bipred_mv)
00157 {
00158 free_mem7Dshort(bipred_mv);
00159 }
00160
00161
00162 static int alloc_rddata(Slice *currSlice, RD_DATA *rd_data)
00163 {
00164 int alloc_size = 0;
00165
00166 alloc_size += get_mem3Dpel(&(rd_data->rec_mb), 3, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
00167
00168 alloc_size += get_mem_ACcoeff (currSlice->p_Img, &(rd_data->cofAC));
00169 alloc_size += get_mem_DCcoeff (&(rd_data->cofDC));
00170
00171 if ((currSlice->slice_type != I_SLICE) && (currSlice->slice_type != SI_SLICE))
00172 {
00173 alloc_size += get_mem_mv (currSlice, &(rd_data->all_mv));
00174 }
00175
00176
00177 alloc_size += get_mem2D((byte***)&(rd_data->ipredmode), currSlice->height_blk, currSlice->width_blk);
00178 alloc_size += get_mem3D((byte****)&(rd_data->refar), 2, 4, 4);
00179
00180 return alloc_size;
00181 }
00182
00183 static void free_rddata(Slice *currSlice, RD_DATA *rd_data)
00184 {
00185 free_mem3D((byte***) rd_data->refar);
00186 free_mem2D((byte**) rd_data->ipredmode);
00187
00188 if ((currSlice->slice_type != I_SLICE) && (currSlice->slice_type != SI_SLICE))
00189 {
00190 free_mem_mv (rd_data->all_mv);
00191 }
00192
00193 free_mem_DCcoeff (rd_data->cofDC);
00194 free_mem_ACcoeff (rd_data->cofAC);
00195
00196 free_mem3Dpel(rd_data->rec_mb);
00197 }
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 static int start_slice(Slice *currSlice, StatParameters *cur_stats)
00214 {
00215 ImageParameters *p_Img = currSlice->p_Img;
00216 EncodingEnvironmentPtr eep;
00217 Bitstream *currStream;
00218 int header_len = 0;
00219 int i;
00220 int NumberOfPartitions = (currSlice->partition_mode == PAR_DP_1?1:3);
00221
00222
00223 if(currSlice->idr_flag)
00224 {
00225 NumberOfPartitions = 1;
00226 }
00227
00228 RTPUpdateTimestamp (p_Img, currSlice->frame_no);
00229
00230 for (i = 0; i < NumberOfPartitions; i++)
00231 {
00232 currStream = (currSlice->partArr[i]).bitstream;
00233
00234 currStream->write_flag = 0;
00235 if (i==0)
00236 header_len += SliceHeader (currSlice);
00237 else
00238 header_len += Partition_BC_Header(currSlice, i);
00239
00240
00241 if (currSlice->symbol_mode == CABAC)
00242 {
00243 eep = &((currSlice->partArr[i]).ee_cabac);
00244 if (currStream->bits_to_go != 8)
00245 header_len += currStream->bits_to_go;
00246 writeVlcByteAlign(p_Img, currStream, cur_stats);
00247 eep->p_Img = p_Img;
00248 arienco_start_encoding(eep, currStream->streamBuffer, &(currStream->byte_pos));
00249
00250 arienco_reset_EC(eep);
00251 }
00252 else
00253 {
00254
00255 CAVLC_init(currSlice);
00256 }
00257 }
00258
00259 if(currSlice->symbol_mode == CABAC)
00260 {
00261 init_contexts(currSlice);
00262 }
00263
00264 return header_len;
00265 }
00266
00267
00268
00269
00270
00271
00272
00273
00274 void create_slice_nalus(Slice *currSlice)
00275 {
00276
00277
00278 int buffer_size = currSlice->partArr[0].bitstream->buffer_size;
00279
00280 int part;
00281 NALU_t *nalu;
00282
00283 for (part=0; part< currSlice->max_part_nr; part++)
00284 {
00285 if (currSlice->partArr[part].bitstream->write_flag)
00286 {
00287 nalu = AllocNALU(buffer_size);
00288 currSlice->partArr[part].nal_unit = nalu;
00289 nalu->startcodeprefix_len = 1+ (currSlice->start_mb_nr == 0 && part == 0 ?ZEROBYTES_SHORTSTARTCODE+1:ZEROBYTES_SHORTSTARTCODE);
00290 nalu->forbidden_bit = 0;
00291
00292 if (currSlice->idr_flag)
00293 {
00294 nalu->nal_unit_type = NALU_TYPE_IDR;
00295 nalu->nal_reference_idc = NALU_PRIORITY_HIGHEST;
00296 }
00297 else
00298 {
00299
00300 if(currSlice->partition_mode == 0)
00301 {
00302 nalu->nal_unit_type = NALU_TYPE_SLICE;
00303 }
00304 else
00305 {
00306 nalu->nal_unit_type = (NaluType) (NALU_TYPE_DPA + part);
00307 }
00308
00309 if (currSlice->nal_reference_idc !=0)
00310 {
00311 nalu->nal_reference_idc = NALU_PRIORITY_HIGH;
00312 }
00313 else
00314 {
00315 nalu->nal_reference_idc = NALU_PRIORITY_DISPOSABLE;
00316 }
00317 }
00318 }
00319 else
00320 {
00321 currSlice->partArr[part].nal_unit = NULL;
00322 }
00323 }
00324 }
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337 static int terminate_slice(Macroblock *currMB, int lastslice, StatParameters *cur_stats )
00338 {
00339 Slice *currSlice = currMB->p_slice;
00340 ImageParameters *p_Img = currMB->p_Img;
00341 InputParameters *p_Inp = currMB->p_Inp;
00342
00343 Bitstream *currStream;
00344 NALU_t *currNalu;
00345 EncodingEnvironmentPtr eep;
00346 int part;
00347 int tmp_stuffingbits = currMB->bits.mb_stuffing;
00348
00349 if (currSlice->symbol_mode == CABAC)
00350 write_terminating_bit (currSlice, 1);
00351
00352 create_slice_nalus(currSlice);
00353
00354 if (currSlice->slice_type != I_SLICE && currSlice->slice_type != SI_SLICE)
00355 {
00356 if (p_Inp->SearchMode == EPZS)
00357 {
00358 EPZSStructDelete (currSlice);
00359 }
00360 }
00361
00362 for (part = 0; part < currSlice->max_part_nr; part++)
00363 {
00364 currStream = (currSlice->partArr[part]).bitstream;
00365 currNalu = (currSlice->partArr[part]).nal_unit;
00366 if (currStream->write_flag)
00367 {
00368 if (currSlice->symbol_mode == CAVLC)
00369 {
00370 SODBtoRBSP(currStream);
00371 currNalu->len = RBSPtoEBSP(currNalu->buf, currStream->streamBuffer, currStream->byte_pos);
00372 }
00373 else
00374 {
00375 eep = &((currSlice->partArr[part]).ee_cabac);
00376
00377 arienco_done_encoding(currMB, eep);
00378 set_pic_bin_count(p_Img, eep);
00379
00380 currStream->bits_to_go = eep->Ebits_to_go;
00381 currStream->byte_buf = 0;
00382
00383 currNalu->len = RBSPtoEBSP(currNalu->buf, currStream->streamBuffer, currStream->byte_pos);
00384
00385
00386 p_Img->bytes_in_picture += currNalu->len + 1;
00387
00388 if (lastslice && (part==(currSlice->max_part_nr - 1)))
00389 {
00390 addCabacZeroWords(p_Img, currNalu, cur_stats);
00391 }
00392 }
00393 }
00394 }
00395
00396 if( currSlice->symbol_mode == CABAC )
00397 {
00398 store_contexts(currSlice);
00399 }
00400
00401 cur_stats->bit_use_stuffingBits[currSlice->slice_type] += currMB->bits.mb_stuffing - tmp_stuffingbits;
00402
00403 if (currSlice->slice_type != I_SLICE && currSlice->slice_type != SI_SLICE)
00404 free_ref_pic_list_reordering_buffer (currSlice);
00405
00406 return 0;
00407 }
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417 int encode_one_slice (ImageParameters *p_Img, InputParameters *p_Inp, int SliceGroupId, int TotalCodedMBs)
00418 {
00419 Boolean end_of_slice = FALSE;
00420 Boolean recode_macroblock;
00421 Boolean prev_recode_mb = FALSE;
00422 int len;
00423 int NumberOfCodedMBs = 0;
00424 Macroblock* currMB = NULL;
00425 int CurrentMbAddr;
00426 StatParameters *cur_stats = &p_Img->enc_picture->stats;
00427 Slice *currSlice = NULL;
00428
00429 p_Img->Motion_Selected = 0;
00430
00431 if( IS_INDEPENDENT(p_Inp) )
00432 {
00433 change_plane_JV( p_Img, p_Img->colour_plane_id );
00434 }
00435
00436 p_Img->cod_counter = 0;
00437
00438 CurrentMbAddr = FmoGetFirstMacroblockInSlice (p_Img, SliceGroupId);
00439
00440
00441 init_slice (p_Img, p_Inp, &currSlice, CurrentMbAddr);
00442
00443
00444 init_quant_4x4 (currSlice);
00445 init_quant_8x8 (currSlice);
00446 init_quant_Chroma(currSlice);
00447
00448 currSlice->SetLagrangianMultipliers(p_Img, p_Inp);
00449
00450 if (currSlice->symbol_mode == CABAC)
00451 {
00452 SetCtxModelNumber (currSlice);
00453 }
00454
00455 p_Img->checkref = (short) (p_Inp->rdopt && p_Inp->RestrictRef && (p_Img->type==P_SLICE || p_Img->type==SP_SLICE));
00456
00457 len = start_slice (currSlice, cur_stats);
00458
00459
00460 if (p_Inp->RCEnable)
00461 rc_store_slice_header_bits( p_Img, p_Inp, len );
00462
00463
00464 p_Img->p_Stats->bit_slice += len;
00465 cur_stats->bit_use_header[currSlice->slice_type] += len;
00466
00467 if(currSlice->UseRDOQuant == 1 && currSlice->RDOQ_QP_Num > 1)
00468 get_dQP_table(currSlice);
00469
00470 while (end_of_slice == FALSE)
00471 {
00472 if (p_Img->AdaptiveRounding && p_Inp->AdaptRndPeriod && (p_Img->current_mb_nr % p_Inp->AdaptRndPeriod == 0))
00473 {
00474 CalculateOffset4x4Param(p_Img, p_Inp);
00475 if(p_Inp->Transform8x8Mode)
00476 CalculateOffset8x8Param(p_Img, p_Inp);
00477 }
00478
00479 recode_macroblock = FALSE;
00480 if(currSlice->UseRDOQuant)
00481 currSlice->rddata = &currSlice->rddata_trellis_curr;
00482 else
00483 currSlice->rddata = &currSlice->rddata_top_frame_mb;
00484
00485 start_macroblock (currSlice, &currMB, CurrentMbAddr, FALSE);
00486
00487
00488 if(currSlice->UseRDOQuant)
00489 {
00490 trellis_coding(currMB, prev_recode_mb);
00491 }
00492 else
00493 {
00494 p_Img->masterQP = p_Img->qp;
00495
00496 currSlice->encode_one_macroblock (currMB);
00497 end_encode_one_macroblock(currMB);
00498 write_macroblock (currMB, 1, prev_recode_mb);
00499 }
00500
00501 end_macroblock (currMB, &end_of_slice, &recode_macroblock);
00502 prev_recode_mb = recode_macroblock;
00503
00504
00505
00506
00507 if (recode_macroblock == FALSE)
00508 {
00509 p_Img->SumFrameQP += currMB->qp;
00510 CurrentMbAddr = FmoGetNextMBNr (p_Img, CurrentMbAddr);
00511 if (CurrentMbAddr == -1)
00512 {
00513
00514 end_of_slice = TRUE;
00515 }
00516 NumberOfCodedMBs++;
00517 next_macroblock (currMB);
00518 }
00519 else
00520 {
00521
00522 p_Img->current_mb_nr = FmoGetPreviousMBNr(p_Img, p_Img->current_mb_nr);
00523 p_Img->NumberofCodedMacroBlocks--;
00524 if(p_Img->current_mb_nr == -1 )
00525
00526 {
00527 snprintf (errortext, ET_SIZE, "Error encoding first MB with specified parameter, bits of current MB may be too big");
00528 error (errortext, 300);
00529 }
00530 }
00531 }
00532
00533
00534 if ((p_Inp->WPIterMC) && (p_Img->frameOffsetAvail == 0) && p_Img->nal_reference_idc)
00535 {
00536 compute_offset(p_Img);
00537 }
00538 p_Img->num_ref_idx_l0_active = currSlice->num_ref_idx_active[LIST_0];
00539 p_Img->num_ref_idx_l1_active = currSlice->num_ref_idx_active[LIST_1];
00540
00541 terminate_slice (currMB, (NumberOfCodedMBs + TotalCodedMBs >= (int)p_Img->PicSizeInMbs), cur_stats );
00542 return NumberOfCodedMBs;
00543 }
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554 int encode_one_slice_MBAFF (ImageParameters *p_Img, InputParameters *p_Inp, int SliceGroupId, int TotalCodedMBs)
00555 {
00556 Boolean end_of_slice = FALSE;
00557 Boolean recode_macroblock;
00558 Boolean prev_recode_mb = FALSE;
00559 int len;
00560 int NumberOfCodedMBs = 0;
00561 Macroblock* currMB = NULL;
00562 int CurrentMbAddr;
00563 double FrameRDCost = DBL_MAX, FieldRDCost = DBL_MAX;
00564 StatParameters *cur_stats = &p_Img->enc_picture->stats;
00565 Slice *currSlice = NULL;
00566
00567 p_Img->Motion_Selected = 0;
00568
00569 if( IS_INDEPENDENT(p_Inp) )
00570 {
00571 change_plane_JV( p_Img, p_Img->colour_plane_id );
00572 }
00573
00574 p_Img->cod_counter = 0;
00575
00576 CurrentMbAddr = FmoGetFirstMacroblockInSlice (p_Img, SliceGroupId);
00577
00578
00579 init_slice (p_Img, p_Inp, &currSlice, CurrentMbAddr);
00580
00581
00582 init_quant_4x4 (currSlice);
00583 init_quant_8x8 (currSlice);
00584 init_quant_Chroma(currSlice);
00585
00586 currSlice->SetLagrangianMultipliers(p_Img, p_Inp);
00587
00588 if (currSlice->symbol_mode == CABAC)
00589 {
00590 SetCtxModelNumber (currSlice);
00591 }
00592
00593 p_Img->checkref = (short) (p_Inp->rdopt && p_Inp->RestrictRef && (currSlice->slice_type == P_SLICE || currSlice->slice_type == SP_SLICE));
00594
00595 len = start_slice (currSlice, cur_stats);
00596
00597
00598 if (p_Inp->RCEnable)
00599 rc_store_slice_header_bits( p_Img, p_Inp, len );
00600
00601
00602 p_Img->p_Stats->bit_slice += len;
00603 cur_stats->bit_use_header[p_Img->type] += len;
00604
00605 while (end_of_slice == FALSE)
00606 {
00607
00608 if (p_Img->AdaptiveRounding && p_Inp->AdaptRndPeriod && (p_Img->current_mb_nr % p_Inp->AdaptRndPeriod == 0))
00609 {
00610 CalculateOffset4x4Param(p_Img, p_Inp);
00611 if(p_Inp->Transform8x8Mode)
00612 CalculateOffset8x8Param(p_Img, p_Inp);
00613 }
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644 p_Img->write_macroblock = FALSE;
00645 if (p_Inp->MbInterlace == ADAPTIVE_CODING || p_Inp->MbInterlace == FRAME_MB_PAIR_CODING)
00646 {
00647
00648
00649 recode_macroblock = FALSE;
00650
00651 update_mv_limits(p_Img, p_Inp, FALSE);
00652
00653 p_Img->field_mode = FALSE;
00654 p_Img->top_field = FALSE;
00655
00656
00657 p_Img->write_macroblock = FALSE;
00658 p_Img->bot_MB = FALSE;
00659
00660
00661 if ( p_Inp->RCEnable && p_Inp->RCUpdateMode <= MAX_RC_MODE )
00662 {
00663 if ( p_Inp->MbInterlace == ADAPTIVE_CODING
00664 && p_Img->NumberofCodedMacroBlocks > 0 && (p_Img->NumberofCodedMacroBlocks % p_Img->BasicUnit) == 0 )
00665 rc_copy_quadratic( p_Img, p_Inp, p_Img->p_rc_quad_init, p_Img->p_rc_quad );
00666 if ( p_Inp->MbInterlace == ADAPTIVE_CODING )
00667 rc_copy_generic( p_Img, p_Img->p_rc_gen_init, p_Img->p_rc_gen );
00668 }
00669
00670 start_macroblock (currSlice, &currMB, CurrentMbAddr, FALSE);
00671
00672 currSlice->rddata = &currSlice->rddata_top_frame_mb;
00673 p_Img->masterQP = p_Img->qp;
00674 currSlice->encode_one_macroblock (currMB);
00675 end_encode_one_macroblock(currMB);
00676
00677
00678 FrameRDCost = currSlice->rddata->min_rdcost;
00679
00680
00681
00682 p_Img->bot_MB = TRUE;
00683
00684
00685 p_Img->field_mode = FALSE;
00686
00687 start_macroblock (currSlice, &currMB, CurrentMbAddr + 1, FALSE);
00688 currSlice->rddata = &currSlice->rddata_bot_frame_mb;
00689 p_Img->masterQP = p_Img->qp;
00690 currSlice->encode_one_macroblock (currMB);
00691 end_encode_one_macroblock(currMB);
00692
00693 if ( p_Inp->RCEnable && p_Inp->RCUpdateMode <= MAX_RC_MODE )
00694 {
00695 if ( p_Inp->MbInterlace == ADAPTIVE_CODING
00696 && p_Img->NumberofCodedMacroBlocks > 0 && (p_Img->NumberofCodedMacroBlocks % p_Img->BasicUnit) == 0 )
00697 rc_copy_quadratic( p_Img, p_Inp, p_Img->p_rc_quad_best, p_Img->p_rc_quad );
00698
00699 if ( p_Inp->MbInterlace == ADAPTIVE_CODING )
00700 rc_copy_generic( p_Img, p_Img->p_rc_gen_best, p_Img->p_rc_gen );
00701 }
00702
00703 FrameRDCost += currSlice->rddata->min_rdcost;
00704
00705 }
00706
00707 if ((p_Inp->MbInterlace == ADAPTIVE_CODING) || (p_Inp->MbInterlace == FIELD_CODING))
00708 {
00709
00710 p_Img->bot_MB = FALSE;
00711
00712 update_mv_limits(p_Img, p_Inp, TRUE);
00713
00714
00715
00716 p_Img->field_mode = TRUE;
00717 p_Img->top_field = TRUE;
00718 p_Img->buf_cycle <<= 1;
00719 p_Inp->num_ref_frames <<= 1;
00720 currSlice->num_ref_idx_active[LIST_0] <<= 1;
00721 currSlice->num_ref_idx_active[LIST_0] += 1;
00722
00723 if ( p_Inp->RCEnable && p_Inp->RCUpdateMode <= MAX_RC_MODE )
00724 {
00725 if ( p_Inp->MbInterlace == ADAPTIVE_CODING
00726 && p_Img->NumberofCodedMacroBlocks > 0 && (p_Img->NumberofCodedMacroBlocks % p_Img->BasicUnit) == 0 )
00727 rc_copy_quadratic( p_Img, p_Inp, p_Img->p_rc_quad, p_Img->p_rc_quad_init );
00728
00729 if ( p_Inp->MbInterlace == ADAPTIVE_CODING )
00730 rc_copy_generic( p_Img, p_Img->p_rc_gen, p_Img->p_rc_gen_init );
00731 }
00732
00733 start_macroblock (currSlice, &currMB, CurrentMbAddr, TRUE);
00734
00735 currSlice->rddata = &currSlice->rddata_top_field_mb;
00736
00737 p_Img->masterQP = p_Img->qp;
00738 currSlice->encode_one_macroblock (currMB);
00739 end_encode_one_macroblock(currMB);
00740
00741 FieldRDCost = currSlice->rddata->min_rdcost;
00742
00743
00744 p_Img->bot_MB = TRUE;
00745
00746 p_Img->top_field = FALSE;
00747 start_macroblock (currSlice, &currMB, CurrentMbAddr+1, TRUE);
00748 currSlice->rddata = &currSlice->rddata_bot_field_mb;
00749 p_Img->masterQP = p_Img->qp;
00750 currSlice->encode_one_macroblock (currMB);
00751 end_encode_one_macroblock(currMB);
00752
00753 FieldRDCost += currSlice->rddata->min_rdcost;
00754
00755 }
00756
00757
00758 p_Img->write_mbaff_frame = 0;
00759
00760
00761
00762 if ( ((p_Inp->MbInterlace == ADAPTIVE_CODING) && (FrameRDCost < FieldRDCost)) || p_Inp->MbInterlace == FRAME_MB_PAIR_CODING )
00763 {
00764 p_Img->field_mode = FALSE;
00765 p_Img->MBPairIsField = FALSE;
00766 if ( p_Inp->MbInterlace != FRAME_MB_PAIR_CODING )
00767 {
00768 p_Img->buf_cycle >>= 1;
00769 p_Inp->num_ref_frames >>= 1;
00770 currSlice->num_ref_idx_active[LIST_0] -= 1;
00771 currSlice->num_ref_idx_active[LIST_0] >>= 1;
00772 }
00773
00774 if ( p_Inp->RCEnable && p_Inp->RCUpdateMode <= MAX_RC_MODE )
00775 {
00776 if ( p_Inp->MbInterlace == ADAPTIVE_CODING
00777 && p_Img->NumberofCodedMacroBlocks > 0 && (p_Img->NumberofCodedMacroBlocks % p_Img->BasicUnit) == 0 )
00778 rc_copy_quadratic( p_Img, p_Inp, p_Img->p_rc_quad, p_Img->p_rc_quad_best );
00779
00780 if ( p_Inp->MbInterlace == ADAPTIVE_CODING )
00781 rc_copy_generic( p_Img, p_Img->p_rc_gen, p_Img->p_rc_gen_best );
00782 }
00783
00784
00785 p_Img->write_mbaff_frame = 1;
00786 }
00787 else
00788 {
00789 p_Img->field_mode = TRUE;
00790 p_Img->MBPairIsField = TRUE;
00791 }
00792
00793
00794 p_Img->write_macroblock = TRUE;
00795
00796 if (p_Img->MBPairIsField)
00797 p_Img->top_field = TRUE;
00798 else
00799 p_Img->top_field = FALSE;
00800
00801
00802 p_Img->bot_MB = FALSE;
00803
00804
00805 start_macroblock (currSlice, &currMB, CurrentMbAddr, p_Img->field_mode);
00806
00807 currSlice->rddata = p_Img->field_mode ? &currSlice->rddata_top_field_mb : &currSlice->rddata_top_frame_mb;
00808 copy_rdopt_data (currMB);
00809 write_macroblock (currMB, 1, prev_recode_mb);
00810 end_macroblock (currMB, &end_of_slice, &recode_macroblock);
00811 prev_recode_mb = recode_macroblock;
00812
00813 if (recode_macroblock == FALSE)
00814 {
00815 p_Img->SumFrameQP += currMB->qp;
00816
00817 CurrentMbAddr = FmoGetNextMBNr (p_Img, CurrentMbAddr);
00818 if (CurrentMbAddr == -1)
00819 {
00820 end_of_slice = TRUE;
00821 }
00822 NumberOfCodedMBs++;
00823 next_macroblock (currMB);
00824
00825
00826 p_Img->bot_MB = TRUE;
00827
00828 p_Img->top_field = FALSE;
00829 start_macroblock (currSlice, &currMB, CurrentMbAddr, p_Img->field_mode);
00830
00831 currSlice->rddata = p_Img->field_mode ? &currSlice->rddata_bot_field_mb : &currSlice->rddata_bot_frame_mb;
00832 copy_rdopt_data (currMB);
00833
00834 write_macroblock (currMB, 0, prev_recode_mb);
00835 end_macroblock (currMB, &end_of_slice, &recode_macroblock);
00836 prev_recode_mb = recode_macroblock;
00837 if (recode_macroblock == FALSE)
00838 {
00839 p_Img->SumFrameQP += currMB->qp;
00840
00841 CurrentMbAddr = FmoGetNextMBNr (p_Img, CurrentMbAddr);
00842 if (CurrentMbAddr == -1)
00843 {
00844 end_of_slice = TRUE;
00845 }
00846 NumberOfCodedMBs++;
00847 next_macroblock (currMB);
00848 }
00849 else
00850 {
00851
00852 p_Img->current_mb_nr = FmoGetPreviousMBNr(p_Img, p_Img->current_mb_nr);
00853 p_Img->NumberofCodedMacroBlocks -= 2;
00854 if(p_Img->current_mb_nr == -1 )
00855
00856 {
00857 snprintf (errortext, ET_SIZE, "Error encoding first MB with specified parameter, bits of current MB may be too big");
00858 error (errortext, 300);
00859 }
00860 }
00861 }
00862 else
00863 {
00864
00865 p_Img->current_mb_nr = FmoGetPreviousMBNr(p_Img, p_Img->current_mb_nr);
00866 p_Img->NumberofCodedMacroBlocks--;
00867 if(p_Img->current_mb_nr == -1 )
00868
00869 {
00870 snprintf (errortext, ET_SIZE, "Error encoding first MB with specified parameter, bits of current MB may be too big");
00871 error (errortext, 300);
00872 }
00873 }
00874
00875 if (p_Img->MBPairIsField)
00876 {
00877 p_Img->buf_cycle >>= 1;
00878 p_Inp->num_ref_frames >>= 1;
00879 currSlice->num_ref_idx_active[LIST_0] -= 1;
00880 currSlice->num_ref_idx_active[LIST_0] >>= 1;
00881 }
00882
00883 p_Img->field_mode = p_Img->top_field = FALSE;
00884
00885 if ( !end_of_slice )
00886 {
00887 assert( CurrentMbAddr < (int)p_Img->PicSizeInMbs );
00888 assert( CurrentMbAddr >= 0 );
00889 if (CurrentMbAddr == FmoGetLastCodedMBOfSliceGroup (p_Img, FmoMB2SliceGroup (p_Img, CurrentMbAddr)))
00890 end_of_slice = TRUE;
00891 }
00892 }
00893
00894 p_Img->num_ref_idx_l0_active = currSlice->num_ref_idx_active[LIST_0];
00895 p_Img->num_ref_idx_l1_active = currSlice->num_ref_idx_active[LIST_1];
00896
00897 terminate_slice (currMB, (NumberOfCodedMBs + TotalCodedMBs >= (int)p_Img->PicSizeInMbs), cur_stats );
00898 return NumberOfCodedMBs;
00899 }
00900
00901 static void setup_cabac(Slice *currSlice, char *listXsize)
00902 {
00903 seq_parameter_set_rbsp_t *active_sps = currSlice->active_sps;
00904 int i;
00905
00906 if (currSlice->slice_type == I_SLICE || currSlice->slice_type == SI_SLICE)
00907 {
00908 currSlice->set_modes_and_reframe = set_modes_and_reframe;
00909 currSlice->writeMB_Skip = NULL;
00910 currSlice->writeMB_typeInfo = writeMB_I_typeInfo_CABAC;
00911 currSlice->writeB8_typeInfo = writeB8_typeInfo_CABAC;
00912 currSlice->writeMotionInfo2NAL = NULL;
00913 currSlice->write_MB_layer = writeMBLayerISlice;
00914 for (i=0; i<6; i++)
00915 {
00916 currSlice->writeRefFrame[i] = NULL;
00917 }
00918 }
00919 else if (currSlice->slice_type == B_SLICE)
00920 {
00921 currSlice->set_modes_and_reframe = set_modes_and_reframe_b_slice;
00922 currSlice->writeMB_Skip = writeMB_Bskip_flagInfo_CABAC;
00923 currSlice->writeMB_typeInfo = writeMB_B_typeInfo_CABAC;
00924 currSlice->writeB8_typeInfo = writeB8_B_typeInfo_CABAC;
00925 currSlice->writeMotionInfo2NAL = write_bslice_motion_info_to_NAL;
00926 currSlice->write_MB_layer = writeMBLayerBSlice;
00927 for (i=0; i<6; i++)
00928 {
00929 switch (listXsize[i])
00930 {
00931 case 0:
00932 currSlice->writeRefFrame[i] = NULL;
00933 break;
00934 case 1:
00935 currSlice->writeRefFrame[i] = writeSE_Dummy;
00936 break;
00937 default:
00938 currSlice->writeRefFrame[i] = writeRefPic_B_CABAC;
00939 }
00940 }
00941 }
00942 else
00943 {
00944 currSlice->set_modes_and_reframe = set_modes_and_reframe_p_slice;
00945 currSlice->writeMB_Skip = writeMB_Pskip_flagInfo_CABAC;
00946 currSlice->writeMB_typeInfo = writeMB_P_typeInfo_CABAC;
00947 currSlice->writeB8_typeInfo = writeB8_typeInfo_CABAC;
00948 currSlice->writeMotionInfo2NAL = write_pslice_motion_info_to_NAL;
00949 currSlice->write_MB_layer = writeMBLayerPSlice;
00950
00951 for (i=0; i<6; i++)
00952 {
00953 switch (listXsize[i])
00954 {
00955 case 0:
00956 currSlice->writeRefFrame[i] = NULL;
00957 break;
00958 case 1:
00959 currSlice->writeRefFrame[i] = writeSE_Dummy;
00960 break;
00961 default:
00962 currSlice->writeRefFrame[i] = writeRefPic_P_CABAC;
00963 }
00964 }
00965 }
00966
00967 currSlice->writeIntraPredMode = writeIntraPredMode_CABAC;
00968 currSlice->writeCoeff16x16 = writeCoeff16x16_CABAC;
00969 currSlice->writeMVD = writeMVD_CABAC;
00970 currSlice->writeCBP = writeCBP_CABAC;
00971 currSlice->writeDquant = writeDquant_CABAC;
00972 currSlice->writeCIPredMode = writeCIPredMode_CABAC;
00973 currSlice->writeFieldModeInfo = writeFieldModeInfo_CABAC;
00974 currSlice->writeMB_transform_size = writeMB_transform_size_CABAC;
00975
00976 if (active_sps->chroma_format_idc == YUV444)
00977 currSlice->write_and_store_CBP_block_bit = write_and_store_CBP_block_bit_444;
00978 else
00979 currSlice->write_and_store_CBP_block_bit = write_and_store_CBP_block_bit;
00980
00981 memset(currSlice->coeff, 0 , 64 * sizeof(int));
00982 currSlice->coeff_ctr = 0;
00983 currSlice->pos = 0;
00984 }
00985
00986 static void setup_cavlc(Slice *currSlice, char *listXsize)
00987 {
00988 seq_parameter_set_rbsp_t *active_sps = currSlice->active_sps;
00989
00990 int i;
00991 currSlice->writeMB_typeInfo = writeUVLC_CAVLC;
00992 currSlice->writeIntraPredMode = writeIntraPredMode_CAVLC;
00993 currSlice->writeB8_typeInfo = writeSE_UVLC;
00994 currSlice->writeCoeff16x16 = writeCoeff16x16_CAVLC;
00995 for (i=0; i<6; i++)
00996 {
00997 switch (listXsize[i])
00998 {
00999 case 0:
01000 currSlice->writeRefFrame[i] = NULL;
01001 break;
01002 case 1:
01003 currSlice->writeRefFrame[i] = writeSE_Dummy;
01004 break;
01005 case 2:
01006 currSlice->writeRefFrame[i] = writeSE_invFlag;
01007 break;
01008 default:
01009 currSlice->writeRefFrame[i] = writeSE_UVLC;
01010 break;
01011 }
01012 }
01013
01014 if (currSlice->slice_type == I_SLICE || currSlice->slice_type == SI_SLICE)
01015 {
01016 currSlice->set_modes_and_reframe = set_modes_and_reframe;
01017 currSlice->writeMotionInfo2NAL = NULL;
01018 currSlice->write_MB_layer = writeMBLayerISlice;
01019 }
01020 else if (currSlice->slice_type == B_SLICE)
01021 {
01022 currSlice->set_modes_and_reframe = set_modes_and_reframe_b_slice;
01023 currSlice->writeMotionInfo2NAL = write_bslice_motion_info_to_NAL;
01024 currSlice->write_MB_layer = writeMBLayerBSlice;
01025 }
01026 else
01027 {
01028 currSlice->set_modes_and_reframe = set_modes_and_reframe_p_slice;
01029 currSlice->writeMotionInfo2NAL = write_pslice_motion_info_to_NAL;
01030 currSlice->write_MB_layer = writeMBLayerPSlice;
01031 }
01032
01033 currSlice->writeMVD = writeSVLC_CAVLC;
01034 currSlice->writeCBP = writeCBP_VLC;
01035 currSlice->writeDquant = writeSVLC_CAVLC;
01036 currSlice->writeCIPredMode = writeUVLC_CAVLC;
01037 currSlice->writeFieldModeInfo = writeFlag_CAVLC;
01038 currSlice->writeMB_transform_size = writeFlag_CAVLC;
01039
01040
01041 if (active_sps->chroma_format_idc == YUV444)
01042 currSlice->writeCoeff4x4_CAVLC = writeCoeff4x4_CAVLC_444;
01043 else
01044 currSlice->writeCoeff4x4_CAVLC = writeCoeff4x4_CAVLC_normal;
01045 }
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058 void init_slice (ImageParameters *p_Img, InputParameters *p_Inp, Slice **currSlice, int start_mb_addr)
01059 {
01060 int i,j;
01061 seq_parameter_set_rbsp_t *active_sps = p_Img->active_sps;
01062 Picture *currPic = p_Img->currentPicture;
01063 DataPartition *dataPart;
01064 Bitstream *currStream;
01065 int active_ref_lists = (p_Img->MbaffFrameFlag) ? 6 : 2;
01066
01067 p_Img->current_mb_nr = start_mb_addr;
01068
01069
01070 assert (currPic != NULL);
01071 currPic->no_slices++;
01072
01073 if (currPic->no_slices >= MAXSLICEPERPICTURE)
01074 error ("Too many slices per picture, increase MAXSLICEPERPICTURE in global.h.", -1);
01075
01076 currPic->slices[currPic->no_slices - 1] = malloc_slice(p_Img, p_Inp);
01077 *currSlice = currPic->slices[currPic->no_slices-1];
01078
01079 p_Img->currentSlice = *currSlice;
01080
01081 (*currSlice)->p_Img = p_Img;
01082 (*currSlice)->p_Inp = p_Inp;
01083 (*currSlice)->active_sps = active_sps;
01084 (*currSlice)->active_pps = p_Img->active_pps;
01085
01086
01087
01088 (*currSlice)->picture_id = (p_Img->frame_no & 0xFF);
01089 (*currSlice)->slice_nr = p_Img->current_slice_nr;
01090 (*currSlice)->idr_flag = p_Img->currentPicture->idr_flag;
01091 (*currSlice)->slice_type = p_Img->type;
01092 (*currSlice)->frame_no = p_Img->frame_no;
01093 (*currSlice)->frame_num = p_Img->frame_num;
01094 (*currSlice)->max_frame_num = p_Img->max_frame_num;
01095 (*currSlice)->framepoc = p_Img->framepoc;
01096 (*currSlice)->ThisPOC = p_Img->ThisPOC;
01097 (*currSlice)->qp = p_Img->qp;
01098 (*currSlice)->start_mb_nr = start_mb_addr;
01099 (*currSlice)->colour_plane_id = p_Img->colour_plane_id;
01100
01101 (*currSlice)->si_frame_indicator = p_Img->si_frame_indicator;
01102 (*currSlice)->sp2_frame_indicator = p_Img->sp2_frame_indicator;
01103
01104 (*currSlice)->P444_joined = p_Img->P444_joined;
01105 (*currSlice)->disthres = p_Inp->disthres;
01106 (*currSlice)->UseRDOQuant = p_Inp->UseRDOQuant;
01107 (*currSlice)->RDOQ_QP_Num = p_Inp->RDOQ_QP_Num;
01108 (*currSlice)->Transform8x8Mode = p_Inp->Transform8x8Mode;
01109
01110 (*currSlice)->slice_too_big = dummy_slice_too_big;
01111 (*currSlice)->width_blk = p_Img->width_blk;
01112 (*currSlice)->height_blk = p_Img->height_blk;
01113 (*currSlice)->partition_mode = (short) p_Inp->partition_mode;
01114 (*currSlice)->PicSizeInMbs = p_Img->PicSizeInMbs;
01115 (*currSlice)->num_blk8x8_uv = p_Img->num_blk8x8_uv;
01116 (*currSlice)->nal_reference_idc = p_Img->nal_reference_idc;
01117 (*currSlice)->bitdepth_luma = p_Img->bitdepth_luma;
01118 (*currSlice)->bitdepth_chroma = p_Img->bitdepth_chroma;
01119
01120 for (i = 0; i < (*currSlice)->max_part_nr; i++)
01121 {
01122 dataPart = &(*currSlice)->partArr[i];
01123
01124 currStream = dataPart->bitstream;
01125 currStream->bits_to_go = 8;
01126 currStream->byte_pos = 0;
01127 currStream->byte_buf = 0;
01128 }
01129
01130 (*currSlice)->direct_spatial_mv_pred_flag = p_Img->direct_spatial_mv_pred_flag;
01131 (*currSlice)->MbaffFrameFlag = p_Img->MbaffFrameFlag;
01132 (*currSlice)->structure = p_Img->structure;
01133
01134 (*currSlice)->num_ref_idx_active[LIST_0] = p_Img->active_pps->num_ref_idx_l0_active_minus1 + 1;
01135 (*currSlice)->num_ref_idx_active[LIST_1] = p_Img->active_pps->num_ref_idx_l1_active_minus1 + 1;
01136
01137 (*currSlice)->DFDisableIdc = p_Img->DFDisableIdc;
01138 (*currSlice)->DFAlphaC0Offset = p_Img->DFAlphaC0Offset;
01139 (*currSlice)->DFBetaOffset = p_Img->DFBetaOffset;
01140
01141
01142 if(p_Inp->redundant_pic_flag)
01143 {
01144 if(!p_Img->redundant_coding)
01145 {
01146 (*currSlice)->num_ref_idx_active[LIST_0] = (char) imin(p_Img->number,p_Inp->NumRefPrimary);
01147 }
01148 else
01149 {
01150
01151 (*currSlice)->num_ref_idx_active[LIST_0] = 1;
01152 }
01153 }
01154
01155
01156 if ((p_Img->type == P_SLICE || p_Img->type == SP_SLICE) && p_Inp->P_List0_refs)
01157 {
01158 (*currSlice)->num_ref_idx_active[LIST_0] = (char) imin((*currSlice)->num_ref_idx_active[LIST_0], p_Inp->P_List0_refs * ((p_Img->structure !=0) + 1));
01159 }
01160
01161 if (p_Img->type == B_SLICE )
01162 {
01163 if (p_Inp->B_List0_refs)
01164 {
01165 (*currSlice)->num_ref_idx_active[LIST_0] = (char) imin((*currSlice)->num_ref_idx_active[LIST_0], p_Inp->B_List0_refs * ((p_Img->structure !=0) + 1));
01166 }
01167 if (p_Inp->B_List1_refs)
01168 {
01169 (*currSlice)->num_ref_idx_active[LIST_1] = (char) imin((*currSlice)->num_ref_idx_active[LIST_1], p_Inp->B_List1_refs * ((p_Img->structure !=0) + 1));
01170 }
01171 get_mem3D((byte ****)(void*)&(*currSlice)->direct_ref_idx, (*currSlice)->height_blk, (*currSlice)->width_blk, 2);
01172 get_mem2D((byte ***) (void*)&(*currSlice)->direct_pdir, (*currSlice)->height_blk, (*currSlice)->width_blk);
01173 }
01174
01175
01176 init_lists(*currSlice);
01177
01178
01179 (*currSlice)->num_ref_idx_active[LIST_0] = p_Img->listXsize[0];
01180 (*currSlice)->num_ref_idx_active[LIST_1] = p_Img->listXsize[1];
01181
01182 if ( p_Inp->WPMCPrecision && p_Inp->WPMCPrecFullRef )
01183 wpxAdaptRefNum(*currSlice);
01184
01185
01186 if (p_Inp->SetFirstAsLongTerm && p_Img->number == 0)
01187 {
01188 mmco_long_term(p_Img, p_Img->number);
01189 }
01190 else if (p_Img->nal_reference_idc && p_Inp->PocMemoryManagement)
01191 {
01192 if (p_Img->structure == FRAME && p_Img->p_Dpb->ref_frames_in_buffer == active_sps->num_ref_frames)
01193 poc_based_ref_management_frame_pic(p_Img, p_Img->frame_num);
01194 else if (p_Img->structure == TOP_FIELD && p_Img->p_Dpb->ref_frames_in_buffer== active_sps->num_ref_frames)
01195 poc_based_ref_management_field_pic(p_Img, (p_Img->frame_num << 1) + 1);
01196 else if (p_Img->structure == BOTTOM_FIELD)
01197 poc_based_ref_management_field_pic(p_Img, (p_Img->frame_num << 1) + 1);
01198 }
01199
01200 if (p_Inp->EnableOpenGOP)
01201 {
01202 for (i = 0; i<p_Img->listXsize[0]; i++)
01203 {
01204 if (p_Img->listX[0][i]->poc < p_Img->last_valid_reference && p_Img->ThisPOC > p_Img->last_valid_reference)
01205 {
01206 p_Img->listXsize[0] = (*currSlice)->num_ref_idx_active[LIST_0] = (char) imax(1, i);
01207 break;
01208 }
01209 }
01210
01211 for (i = 0; i<p_Img->listXsize[1]; i++)
01212 {
01213 if (p_Img->listX[1][i]->poc < p_Img->last_valid_reference && p_Img->ThisPOC > p_Img->last_valid_reference)
01214 {
01215 p_Img->listXsize[1] = (*currSlice)->num_ref_idx_active[LIST_1] = (char) imax(1,i);
01216 break;
01217 }
01218 }
01219 }
01220
01221 init_ref_pic_list_reordering(*currSlice);
01222
01223
01224
01225
01226 if(p_Inp->redundant_pic_flag && p_Img->redundant_coding)
01227 {
01228 (*currSlice)->ref_pic_list_reordering_flag[LIST_0] = 1;
01229 (*currSlice)->reordering_of_pic_nums_idc[LIST_0][0] = 0;
01230 (*currSlice)->reordering_of_pic_nums_idc[LIST_0][1] = 3;
01231 (*currSlice)->abs_diff_pic_num_minus1[LIST_0][0] = p_Img->redundant_ref_idx - 1;
01232 (*currSlice)->long_term_pic_idx[LIST_0][0] = 0;
01233 reorder_ref_pic_list ( *currSlice, p_Img->listX, p_Img->listXsize, LIST_0);
01234 }
01235 else if ( (p_Img->type == P_SLICE || p_Img->type == B_SLICE) && p_Inp->WPMCPrecision && p_Img->pWPX->curr_wp_rd_pass->algorithm != WP_REGULAR )
01236 wp_mcprec_reorder_lists( *currSlice );
01237 else
01238 reorder_lists( *currSlice );
01239
01240 (*currSlice)->max_num_references = (short) p_Img->max_num_references;
01241
01242 if (((*currSlice)->slice_type != I_SLICE) && ((*currSlice)->slice_type != SI_SLICE))
01243 {
01244 get_mem_mv(*currSlice, &(*currSlice)->all_mv);
01245
01246 if (p_Inp->BiPredMotionEstimation && ((*currSlice)->slice_type == B_SLICE))
01247 {
01248 get_mem_bipred_mv(*currSlice, &(*currSlice)->bipred_mv);
01249 }
01250
01251 if (p_Inp->UseRDOQuant && p_Inp->RDOQ_QP_Num > 1)
01252 {
01253 if (p_Inp->Transform8x8Mode && p_Inp->RDOQ_CP_MV)
01254 {
01255 get_mem4Dmv (&(*currSlice)->tmp_mv8, 2, (*currSlice)->max_num_references, 4, 4);
01256 get_mem3Dint(&(*currSlice)->motion_cost8, 2, (*currSlice)->max_num_references, 4);
01257 }
01258 }
01259 }
01260
01261 if (p_Img->MbaffFrameFlag)
01262 init_mbaff_lists(*currSlice);
01263
01264 InitWP(p_Img, p_Inp);
01265
01266 if ((*currSlice)->slice_type == B_SLICE)
01267 (*currSlice)->weighted_prediction = p_Img->active_pps->weighted_bipred_idc;
01268 else if (((*currSlice)->slice_type != I_SLICE) || ((*currSlice)->slice_type != SI_SLICE))
01269 (*currSlice)->weighted_prediction = p_Img->active_pps->weighted_pred_flag;
01270
01271 if (p_Img->type != I_SLICE && (p_Img->active_pps->weighted_pred_flag == 1 || (p_Img->active_pps->weighted_bipred_idc > 0 && (p_Img->type == B_SLICE))))
01272 {
01273 if (p_Img->type == P_SLICE || p_Img->type == SP_SLICE)
01274 {
01275 int wp_type = (p_Inp->GenerateMultiplePPS && p_Inp->RDPictureDecision) && (p_Img->enc_picture != p_Img->enc_frame_picture[1]);
01276 p_Img->EstimateWPPSlice (*currSlice, wp_type);
01277 }
01278 else
01279 p_Img->EstimateWPBSlice (*currSlice);
01280 }
01281
01282 set_ref_pic_num(*currSlice);
01283
01284
01285 if (p_Img->type == B_SLICE)
01286 {
01287 int PicWidthInMbs, PicHeightInMapUnits, FrameHeightInMbs;
01288 int width, height;
01289 PicWidthInMbs = (active_sps->pic_width_in_mbs_minus1 +1);
01290 PicHeightInMapUnits = (active_sps->pic_height_in_map_units_minus1 +1);
01291 FrameHeightInMbs = ( 2 - active_sps->frame_mbs_only_flag ) * PicHeightInMapUnits;
01292
01293 width = PicWidthInMbs * MB_BLOCK_SIZE;
01294 height = FrameHeightInMbs * MB_BLOCK_SIZE;
01295
01296 if( IS_INDEPENDENT(p_Inp) )
01297 {
01298 (*currSlice)->p_colocated = alloc_colocated (p_Img->width, p_Img->height, active_sps->mb_adaptive_frame_field_flag);
01299 compute_colocated_JV(*currSlice, (*currSlice)->p_colocated, p_Img->listX);
01300 }
01301 else
01302 {
01303
01304 (*currSlice)->p_colocated = alloc_colocated (p_Img->width, p_Img->height, active_sps->mb_adaptive_frame_field_flag);
01305 compute_colocated(*currSlice, (*currSlice)->p_colocated, p_Img->listX);
01306 }
01307 }
01308
01309 if (p_Img->type != I_SLICE && p_Img->type != SI_SLICE)
01310 {
01311 if (p_Inp->SearchMode == EPZS)
01312 {
01313 if (((*currSlice)->p_EPZS = (EPZSParameters*) calloc(1, sizeof(EPZSParameters)))==NULL)
01314 no_mem_exit("alloc_img: p_EPZS");
01315
01316 EPZSStructInit (*currSlice);
01317 EPZSSliceInit (*currSlice);
01318 }
01319 }
01320
01321
01322 if ((*currSlice)->symbol_mode == CAVLC)
01323 {
01324 setup_cavlc(*currSlice, p_Img->listXsize);
01325 }
01326 else
01327 {
01328 setup_cabac(*currSlice, p_Img->listXsize);
01329 }
01330
01331
01332
01333 for(i = 0; i < active_ref_lists; i++)
01334 {
01335 for(j = 0; j < p_Img->listXsize[i]; j++)
01336 {
01337 if( p_Img->listX[i][j] )
01338 {
01339 p_Img->listX[i][j]->p_curr_img = p_Img->listX[i][j]->p_img [(short) p_Img->colour_plane_id];
01340 p_Img->listX[i][j]->p_curr_img_sub = p_Img->listX[i][j]->p_img_sub[(short) p_Img->colour_plane_id];
01341 }
01342 }
01343 }
01344
01345 if (p_Inp->UseRDOQuant)
01346 {
01347 if (((*currSlice)->estBitsCabac = (estBitsCabacStruct*) calloc(NUM_BLOCK_TYPES, sizeof(estBitsCabacStruct)))==NULL)
01348 no_mem_exit("init_slice: (*currSlice)->estBitsCabac");
01349
01350 init_rdoq_slice(*currSlice);
01351
01352 alloc_rddata(*currSlice, &(*currSlice)->rddata_trellis_curr);
01353 if (p_Inp->RDOQ_QP_Num > 1)
01354 {
01355 alloc_rddata(*currSlice, &(*currSlice)->rddata_trellis_best);
01356 }
01357 }
01358
01359 if(p_Img->MbaffFrameFlag)
01360 {
01361 alloc_rddata(*currSlice, &(*currSlice)->rddata_top_frame_mb);
01362 alloc_rddata(*currSlice, &(*currSlice)->rddata_bot_frame_mb);
01363 if ( p_Inp->MbInterlace != FRAME_MB_PAIR_CODING )
01364 {
01365 alloc_rddata(*currSlice, &(*currSlice)->rddata_top_field_mb);
01366 alloc_rddata(*currSlice, &(*currSlice)->rddata_bot_field_mb);
01367 }
01368 }
01369
01370 if ((*currSlice)->slice_type == B_SLICE)
01371 {
01372 (*currSlice)->SetMotionVectorsMB = SetMotionVectorsMBBSlice;
01373 }
01374 else if ((*currSlice)->slice_type == P_SLICE || (*currSlice)->slice_type == SP_SLICE)
01375 {
01376 (*currSlice)->SetMotionVectorsMB = SetMotionVectorsMBPSlice;
01377 }
01378 else
01379 {
01380 (*currSlice)->SetMotionVectorsMB = SetMotionVectorsMBISlice;
01381 }
01382
01383
01384 if (p_Img->MbaffFrameFlag)
01385 {
01386 if (p_Img->direct_spatial_mv_pred_flag)
01387 (*currSlice)->Get_Direct_Motion_Vectors = Get_Direct_MV_Spatial_MBAFF;
01388 else
01389 (*currSlice)->Get_Direct_Motion_Vectors = Get_Direct_MV_Temporal;
01390 }
01391 else
01392 {
01393 if (p_Img->direct_spatial_mv_pred_flag)
01394 (*currSlice)->Get_Direct_Motion_Vectors = Get_Direct_MV_Spatial_Normal;
01395 else
01396 (*currSlice)->Get_Direct_Motion_Vectors = Get_Direct_MV_Temporal;
01397 }
01398
01399 get_mem3Dpel(&((*currSlice)->mb_pred), MAX_PLANE, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
01400 get_mem3Dint(&((*currSlice)->mb_rres), MAX_PLANE, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
01401 get_mem3Dint(&((*currSlice)->mb_ores), MAX_PLANE, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
01402 get_mem4Dpel(&((*currSlice)->mpr_4x4), MAX_PLANE, 9, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
01403 get_mem4Dpel(&((*currSlice)->mpr_8x8), MAX_PLANE, 9, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
01404 get_mem4Dpel(&((*currSlice)->mpr_16x16), MAX_PLANE, 5, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
01405
01406 get_mem_ACcoeff (p_Img, &((*currSlice)->cofAC));
01407 get_mem_DCcoeff (&((*currSlice)->cofDC));
01408
01409
01410 allocate_block_mem(*currSlice);
01411 init_coding_state_methods(*currSlice);
01412 init_rdopt(*currSlice);
01413
01414 }
01415
01416
01417
01418
01419
01420
01421
01422
01423
01424
01425 static Slice *malloc_slice(ImageParameters *p_Img, InputParameters *p_Inp)
01426 {
01427 int i;
01428 DataPartition *dataPart;
01429 Slice *currSlice;
01430 int cr_size = IS_INDEPENDENT( p_Inp ) ? 0 : 512;
01431
01432 int buffer_size;
01433
01434 switch (p_Inp->slice_mode)
01435 {
01436 case 2:
01437
01438 buffer_size = imax(2 * p_Inp->slice_argument, 764);
01439 break;
01440 case 1:
01441 buffer_size = 500 + p_Inp->slice_argument * (128 + 256 * p_Img->bitdepth_luma + cr_size * p_Img->bitdepth_chroma);
01442 break;
01443 default:
01444 buffer_size = 500 + p_Img->FrameSizeInMbs * (128 + 256 * p_Img->bitdepth_luma + cr_size * p_Img->bitdepth_chroma);
01445 break;
01446 }
01447
01448
01449 if ((currSlice = (Slice *) calloc(1, sizeof(Slice))) == NULL) no_mem_exit ("malloc_slice: currSlice structure");
01450
01451 currSlice->p_Img = p_Img;
01452 currSlice->p_Inp = p_Inp;
01453
01454 if (((currSlice->p_RDO) = (RDOPTStructure *) calloc(1, sizeof(RDOPTStructure)))==NULL)
01455 no_mem_exit("malloc_slice: p_RDO");
01456
01457 currSlice->symbol_mode = (char) p_Inp->symbol_mode;
01458
01459 if (currSlice->symbol_mode == CABAC)
01460 {
01461
01462 currSlice->mot_ctx = create_contexts_MotionInfo ();
01463 currSlice->tex_ctx = create_contexts_TextureInfo();
01464 }
01465
01466 currSlice->max_part_nr = p_Inp->partition_mode==0?1:3;
01467
01468
01469 if(p_Img->currentPicture->idr_flag)
01470 currSlice->max_part_nr = 1;
01471
01472 assignSE2partition[0] = assignSE2partition_NoDP;
01473
01474
01475 if(!p_Img->currentPicture->idr_flag && p_Inp->partition_mode == 1)
01476 assignSE2partition[1] = assignSE2partition_DP;
01477 else
01478 assignSE2partition[1] = assignSE2partition_NoDP;
01479
01480 currSlice->num_mb = 0;
01481
01482 if ((currSlice->partArr = (DataPartition *) calloc(currSlice->max_part_nr, sizeof(DataPartition))) == NULL)
01483 no_mem_exit ("malloc_slice: partArr");
01484 for (i=0; i<currSlice->max_part_nr; i++)
01485 {
01486 dataPart = &(currSlice->partArr[i]);
01487 if ((dataPart->bitstream = (Bitstream *) calloc(1, sizeof(Bitstream))) == NULL)
01488 no_mem_exit ("malloc_slice: Bitstream");
01489 if ((dataPart->bitstream->streamBuffer = (byte *) calloc(buffer_size, sizeof(byte))) == NULL)
01490 no_mem_exit ("malloc_slice: StreamBuffer");
01491 dataPart->bitstream->buffer_size = buffer_size;
01492
01493
01494 dataPart->p_Slice = currSlice;
01495 dataPart->p_Img = p_Img;
01496 dataPart->p_Inp = p_Inp;
01497 }
01498
01499 if (p_Inp->WeightedPrediction || p_Inp->WeightedBiprediction || p_Inp->GenerateMultiplePPS)
01500 {
01501
01502 get_mem3Dshort(&currSlice->wp_weight, 6, MAX_REFERENCE_PICTURES, 3);
01503 get_mem3Dshort(&currSlice->wp_offset, 6, MAX_REFERENCE_PICTURES, 3);
01504 get_mem4Dshort(&currSlice->wbp_weight, 6, MAX_REFERENCE_PICTURES, MAX_REFERENCE_PICTURES, 3);
01505 }
01506
01507 return currSlice;
01508 }
01509
01510
01511
01512
01513
01514
01515
01516
01517
01518 static void free_nal_unit(Picture *pic)
01519 {
01520 int partition, slice;
01521 Slice *currSlice;
01522
01523
01524 for (slice=0; slice < pic->no_slices; slice++)
01525 {
01526 currSlice = pic->slices[slice];
01527
01528
01529 if (currSlice != NULL)
01530 {
01531 for (partition=0; partition < currSlice->max_part_nr; partition++)
01532 {
01533
01534 if (currSlice->partArr[partition].bitstream->write_flag )
01535 {
01536 if (currSlice->partArr[partition].nal_unit != NULL)
01537 {
01538 FreeNALU(currSlice->partArr[partition].nal_unit);
01539 currSlice->partArr[partition].nal_unit = NULL;
01540 }
01541 }
01542 }
01543 }
01544 }
01545 }
01546
01547
01548
01549
01550
01551
01552
01553
01554
01555
01556 void free_slice_list(Picture *currPic)
01557 {
01558 int i;
01559
01560 if (currPic != NULL)
01561 {
01562 free_nal_unit(currPic);
01563
01564 for (i = 0; i < currPic->no_slices; i++)
01565 {
01566 free_slice (currPic->slices[i]);
01567 currPic->slices[i] = NULL;
01568 }
01569 }
01570 }
01571
01572
01573
01574
01575
01576
01577
01578
01579
01580
01581
01582 static void free_slice(Slice *currSlice)
01583 {
01584 if (currSlice != NULL)
01585 {
01586 ImageParameters *p_Img = currSlice->p_Img;
01587 InputParameters *p_Inp = currSlice->p_Inp;
01588
01589 int i;
01590 DataPartition *dataPart;
01591
01592 for (i=0; i<currSlice->max_part_nr; i++)
01593 {
01594 dataPart = &(currSlice->partArr[i]);
01595
01596 if (dataPart != NULL)
01597 {
01598 if (dataPart->bitstream != NULL)
01599 {
01600 if (dataPart->bitstream->streamBuffer != NULL)
01601 {
01602 free(dataPart->bitstream->streamBuffer);
01603 dataPart->bitstream->streamBuffer = NULL;
01604 }
01605 free(dataPart->bitstream);
01606 dataPart->bitstream = NULL;
01607 }
01608 }
01609 }
01610
01611
01612 clear_rdopt (currSlice);
01613 free (currSlice->p_RDO);
01614
01615 free_mem_ACcoeff (currSlice->cofAC);
01616 free_mem_DCcoeff (currSlice->cofDC);
01617
01618 free_mem3Dint(currSlice->mb_rres );
01619 free_mem3Dint(currSlice->mb_ores );
01620 free_mem3Dpel(currSlice->mb_pred );
01621 free_mem4Dpel(currSlice->mpr_16x16);
01622 free_mem4Dpel(currSlice->mpr_8x8 );
01623 free_mem4Dpel(currSlice->mpr_4x4 );
01624
01625 if (currSlice->slice_type == B_SLICE)
01626 {
01627 free_colocated(currSlice->p_colocated);
01628 }
01629
01630
01631 if (currSlice->partArr != NULL)
01632 {
01633 free(currSlice->partArr);
01634 currSlice->partArr = NULL;
01635 }
01636
01637 if (currSlice->symbol_mode == CABAC)
01638 {
01639 delete_contexts_MotionInfo(currSlice->mot_ctx);
01640 delete_contexts_TextureInfo(currSlice->tex_ctx);
01641 }
01642
01643 if (p_Inp->WeightedPrediction || p_Inp->WeightedBiprediction || p_Inp->GenerateMultiplePPS)
01644 {
01645 free_mem3Dshort(currSlice->wp_weight );
01646 free_mem3Dshort(currSlice->wp_offset );
01647 free_mem4Dshort(currSlice->wbp_weight);
01648 }
01649
01650 if (currSlice->UseRDOQuant)
01651 {
01652 free(currSlice->estBitsCabac);
01653
01654 free_rddata(currSlice, &currSlice->rddata_trellis_curr);
01655 if(currSlice->RDOQ_QP_Num > 1)
01656 {
01657 free_rddata(currSlice, &currSlice->rddata_trellis_best);
01658 }
01659 }
01660
01661 if(p_Img->MbaffFrameFlag)
01662 {
01663 free_rddata(currSlice, &currSlice->rddata_top_frame_mb);
01664 free_rddata(currSlice, &currSlice->rddata_bot_frame_mb);
01665
01666 if ( p_Inp->MbInterlace != FRAME_MB_PAIR_CODING )
01667 {
01668 free_rddata(currSlice, &currSlice->rddata_top_field_mb);
01669 free_rddata(currSlice, &currSlice->rddata_bot_field_mb);
01670 }
01671 }
01672
01673 if ((currSlice->slice_type == P_SLICE) || (currSlice->slice_type == SP_SLICE) || (currSlice->slice_type == B_SLICE))
01674 {
01675 free_mem_mv (currSlice->all_mv);
01676 if (p_Inp->BiPredMotionEstimation && (currSlice->slice_type == B_SLICE))
01677 free_mem_bipred_mv(currSlice->bipred_mv);
01678
01679 if (currSlice->UseRDOQuant && currSlice->RDOQ_QP_Num > 1)
01680 {
01681 if (p_Inp->Transform8x8Mode && p_Inp->RDOQ_CP_MV)
01682 {
01683 free_mem4Dmv (currSlice->tmp_mv8);
01684 free_mem3Dint(currSlice->motion_cost8);
01685 }
01686 }
01687 }
01688
01689 if (currSlice->slice_type == B_SLICE)
01690 {
01691 free_mem3D((byte ***)currSlice->direct_ref_idx);
01692 free_mem2D((byte **) currSlice->direct_pdir);
01693 }
01694
01695 free_block_mem(currSlice);
01696 free(currSlice);
01697 }
01698 }
01699
01700 static void set_ref_pic_num(Slice *currSlice)
01701 {
01702 int i,j;
01703 StorablePicture *this_ref;
01704 ImageParameters *p_Img = currSlice->p_Img;
01705
01706
01707
01708 for (i=0;i<p_Img->listXsize[LIST_0];i++)
01709 {
01710 this_ref = p_Img->listX[LIST_0][i];
01711 p_Img->enc_picture->ref_pic_num [LIST_0][i] = this_ref->poc * 2 + ((this_ref->structure==BOTTOM_FIELD)?1:0) ;
01712 p_Img->enc_picture->frm_ref_pic_num [LIST_0][i] = this_ref->frame_poc * 2;
01713 p_Img->enc_picture->top_ref_pic_num [LIST_0][i] = this_ref->top_poc * 2;
01714 p_Img->enc_picture->bottom_ref_pic_num [LIST_0][i] = this_ref->bottom_poc * 2 + 1;
01715 }
01716
01717 for (i=0;i<p_Img->listXsize[LIST_1];i++)
01718 {
01719 this_ref = p_Img->listX[LIST_1][i];
01720 p_Img->enc_picture->ref_pic_num [LIST_1][i] = this_ref->poc * 2 + ((this_ref->structure==BOTTOM_FIELD)?1:0);
01721 p_Img->enc_picture->frm_ref_pic_num [LIST_1][i] = this_ref->frame_poc * 2;
01722 p_Img->enc_picture->top_ref_pic_num [LIST_1][i] = this_ref->top_poc * 2;
01723 p_Img->enc_picture->bottom_ref_pic_num [LIST_1][i] = this_ref->bottom_poc * 2 + 1;
01724 }
01725
01726 if (!currSlice->active_sps->frame_mbs_only_flag && currSlice->structure==FRAME)
01727 {
01728 for (j=2;j<6;j++)
01729 {
01730 for (i=0;i<p_Img->listXsize[j];i++)
01731 {
01732 this_ref = p_Img->listX[j][i];
01733 p_Img->enc_picture->ref_pic_num[j][i] = this_ref->poc * 2 + ((this_ref->structure==BOTTOM_FIELD)?1:0);
01734 p_Img->enc_picture->frm_ref_pic_num[j][i] = this_ref->frame_poc * 2 ;
01735 p_Img->enc_picture->top_ref_pic_num[j][i] = this_ref->top_poc * 2 ;
01736 p_Img->enc_picture->bottom_ref_pic_num[j][i] = this_ref->bottom_poc * 2 + 1;
01737 }
01738 }
01739 }
01740 }
01741
01742 void UpdateMELambda(ImageParameters *p_Img, InputParameters *p_Inp)
01743 {
01744 int j, k, qp;
01745 if (p_Inp->UpdateLambdaChromaME)
01746 {
01747 for (j = 0; j < 6; j++)
01748 {
01749 for (qp = -p_Img->bitdepth_luma_qp_scale; qp < 52; qp++)
01750 {
01751 for (k = 0; k < 3; k++)
01752 {
01753 if ((p_Inp->MEErrorMetric[k] == ERROR_SAD) && (p_Inp->ChromaMEEnable))
01754 {
01755 switch(p_Img->yuv_format)
01756 {
01757 case YUV420:
01758 p_Img->lambda_mf[j][qp][k] = (3 * p_Img->lambda_mf[j][qp][k] + 1) >> 1;
01759 p_Img->lambda_me[j][qp][k] *= 1.5;
01760 break;
01761 case YUV422:
01762 p_Img->lambda_mf[j][qp][k] *= 2;
01763 p_Img->lambda_me[j][qp][k] *= 2.0;
01764 break;
01765 case YUV444:
01766 p_Img->lambda_mf[j][qp][k] *= 3;
01767 p_Img->lambda_me[j][qp][k] *= 3.0;
01768 break;
01769 default:
01770 break;
01771 }
01772 }
01773 }
01774 }
01775 }
01776 }
01777 }
01778
01779 void SetLambda(ImageParameters *p_Img, InputParameters *p_Inp, int j, int qp, double lambda_scale)
01780 {
01781 int k;
01782 p_Img->lambda_md[j][qp] *= lambda_scale;
01783
01784 for (k = F_PEL; k <= Q_PEL; k++)
01785 {
01786 p_Img->lambda_me[j][qp][k] = (p_Inp->MEErrorMetric[k] == ERROR_SSE) ? p_Img->lambda_md[j][qp] : sqrt(p_Img->lambda_md[j][qp]);
01787 p_Img->lambda_mf[j][qp][k] = LAMBDA_FACTOR (p_Img->lambda_me[j][qp][k]);
01788 }
01789 }
01790
01791 void SetLagrangianMultipliersOn(ImageParameters *p_Img, InputParameters *p_Inp)
01792 {
01793 int qp, j;
01794 double qp_temp;
01795 double lambda_scale = 1.0 - dClip3(0.0,0.5,0.05 * (double) p_Inp->jumpd);
01796
01797 if (p_Inp->UseExplicitLambdaParams == 1)
01798 {
01799 for (j = 0; j < 6; j++)
01800 {
01801 for (qp = -p_Img->bitdepth_luma_qp_scale; qp < 52; qp++)
01802 {
01803 qp_temp = (double)qp + p_Img->bitdepth_luma_qp_scale - SHIFT_QP;
01804
01805 p_Img->lambda_md[j][qp] = p_Inp->LambdaWeight[j] * pow (2, qp_temp/3.0);
01806 SetLambda(p_Img, p_Inp, j, qp, ((p_Inp->MEErrorMetric[H_PEL] == ERROR_SATD && p_Inp->MEErrorMetric[Q_PEL] == ERROR_SATD) ? 1.00 : 0.95));
01807
01808 if (j != B_SLICE)
01809 p_Img->lambda_md[j][qp] *= lambda_scale;
01810
01811 }
01812 }
01813 }
01814 else if (p_Inp->UseExplicitLambdaParams == 2)
01815 {
01816 for (j = 0; j < 6; j++)
01817 {
01818 for (qp = -p_Img->bitdepth_luma_qp_scale; qp < 52; qp++)
01819 {
01820 qp_temp = (double)qp + p_Img->bitdepth_luma_qp_scale - SHIFT_QP;
01821
01822 p_Img->lambda_md[j][qp] = p_Inp->FixedLambda[j];
01823 SetLambda(p_Img, p_Inp, j, qp, ((p_Inp->MEErrorMetric[H_PEL] == ERROR_SATD && p_Inp->MEErrorMetric[Q_PEL] == ERROR_SATD) ? 1.00 : 0.95));
01824 }
01825 }
01826 }
01827 else
01828 {
01829 for (j = 0; j < 5; j++)
01830 {
01831 for (qp = -p_Img->bitdepth_luma_qp_scale; qp < 52; qp++)
01832 {
01833 qp_temp = (double)qp + p_Img->bitdepth_luma_qp_scale - SHIFT_QP;
01834
01835 if(p_Inp->UseRDOQuant && p_Img->type == I_SLICE && p_Img->qp==qp)
01836 p_Img->lambda_md[j][qp] = 0.57 * pow (2.0, qp_temp/3.0);
01837 else if (p_Inp->NumberBFrames > 0)
01838 {
01839
01840
01841
01842
01843
01844 p_Img->lambda_md[j][qp] = 0.68 * pow (2.0, qp_temp/3.0)
01845 * (j == B_SLICE && p_Img->b_frame_to_code != 0 ? dClip3(2.00, 4.00, (qp_temp / 6.0)) : (j == SP_SLICE) ? dClip3(1.4,3.0,(qp_temp / 12.0)) : 1.0);
01846 }
01847 else
01848 p_Img->lambda_md[j][qp] = 0.85 * pow (2.0, qp_temp/3.0) * ((j == SP_SLICE) ? dClip3(1.4, 3.0,(qp_temp / 12.0)) : 1.0);
01849
01850
01851 p_Img->lambda_md[j][qp] = ((p_Inp->MEErrorMetric[H_PEL] == ERROR_SATD && p_Inp->MEErrorMetric[Q_PEL] == ERROR_SATD) ? 1.00 : 0.95) * p_Img->lambda_md[j][qp];
01852
01853 if (j == B_SLICE)
01854 {
01855 p_Img->lambda_md[5][qp] = p_Img->lambda_md[j][qp];
01856
01857 if (p_Img->b_frame_to_code != 0)
01858 {
01859 if (p_Inp->HierarchicalCoding == 2)
01860 p_Img->lambda_md[5][qp] *= (1.0 - dmin(0.4, 0.2 * (double) p_Img->gop_structure[p_Img->b_frame_to_code-1].hierarchy_layer));
01861 else
01862 p_Img->lambda_md[5][qp] *= 0.80;
01863
01864 }
01865 SetLambda(p_Img, p_Inp, 5, qp, lambda_scale);
01866 }
01867 else
01868 p_Img->lambda_md[j][qp] *= lambda_scale;
01869
01870 SetLambda(p_Img, p_Inp, j, qp, 1.0);
01871
01872 if (p_Inp->CtxAdptLagrangeMult == 1)
01873 {
01874 int lambda_qp = (qp >= 32 && !p_Inp->RCEnable) ? imax(0, qp - 4) : imax(0, qp - 6);
01875 p_Img->lambda_mf_factor[j][qp] = log (p_Img->lambda_me[j][lambda_qp][Q_PEL] + 1.0) / log (2.0);
01876 }
01877 }
01878 }
01879 }
01880
01881 UpdateMELambda(p_Img, p_Inp);
01882 }
01883
01884
01885 void SetLagrangianMultipliersOff(ImageParameters *p_Img, InputParameters *p_Inp)
01886 {
01887 int qp, j, k;
01888 double qp_temp;
01889
01890 for (j = 0; j < 6; j++)
01891 {
01892 for (qp = -p_Img->bitdepth_luma_qp_scale; qp < 52; qp++)
01893 {
01894 qp_temp = (double)qp + p_Img->bitdepth_luma_qp_scale - SHIFT_QP;
01895
01896 switch (p_Inp->UseExplicitLambdaParams)
01897 {
01898 case 1:
01899 p_Img->lambda_md[j][qp] = sqrt(p_Inp->LambdaWeight[j] * pow (2, qp_temp/3.0));
01900 break;
01901 case 2:
01902 p_Img->lambda_md[j][qp] = sqrt(p_Inp->FixedLambda[j]);
01903 break;
01904 default:
01905 p_Img->lambda_md[j][qp] = QP2QUANT[imax(0,qp - SHIFT_QP)];
01906 break;
01907 }
01908
01909 for (k = F_PEL; k <= Q_PEL; k++)
01910 {
01911 p_Img->lambda_me[j][qp][k] = (p_Inp->MEErrorMetric[k] == ERROR_SSE) ? (p_Img->lambda_md[j][qp] * p_Img->lambda_md[j][qp]) : p_Img->lambda_md[j][qp];
01912 p_Img->lambda_mf[j][qp][k] = LAMBDA_FACTOR (p_Img->lambda_me[j][qp][k]);
01913 }
01914
01915 if (p_Inp->CtxAdptLagrangeMult == 1)
01916 {
01917 int lambda_qp = (qp >= 32 && !p_Inp->RCEnable) ? imax(0, qp-4) : imax(0, qp-6);
01918 p_Img->lambda_mf_factor[j][qp] = log (p_Img->lambda_me[j][lambda_qp][Q_PEL] + 1.0) / log (2.0);
01919 }
01920 }
01921 }
01922 UpdateMELambda(p_Img, p_Inp);
01923 }
01924
01925
01926