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