00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "contributors.h"
00028
00029 #include <math.h>
00030 #include <time.h>
00031 #include <sys/timeb.h>
00032
00033 #include "global.h"
00034
00035 #include "filehandle.h"
00036 #include "mbuffer.h"
00037 #include "img_luma.h"
00038 #include "img_chroma.h"
00039 #include "img_distortion.h"
00040 #include "intrarefresh.h"
00041 #include "slice.h"
00042 #include "fmo.h"
00043 #include "sei.h"
00044 #include "memalloc.h"
00045 #include "nalu.h"
00046 #include "ratectl.h"
00047 #include "mb_access.h"
00048 #include "context_ini.h"
00049 #include "biariencode.h"
00050 #include "enc_statistics.h"
00051 #include "conformance.h"
00052 #include "report.h"
00053
00054 #include "q_matrix.h"
00055 #include "q_offsets.h"
00056 #include "wp.h"
00057 #include "input.h"
00058 #include "image.h"
00059 #include "errdo.h"
00060 #include "img_process.h"
00061
00062
00063 extern pic_parameter_set_rbsp_t *PicParSet[MAXPPS];
00064
00065 extern void DeblockFrame(ImageParameters *img, imgpel **, imgpel ***);
00066
00067 static void code_a_picture(Picture *pic);
00068 static void field_picture(Picture *top, Picture *bottom);
00069 static void prepare_enc_frame_picture (StorablePicture **stored_pic);
00070 static void writeout_picture(Picture *pic);
00071 static byte picture_structure_decision(Picture *frame, Picture *top, Picture *bot);
00072 static void distortion_fld (Picture *field_pic, ImageData *imgData);
00073
00074 static void field_mode_buffer (ImageParameters *img, InputParameters *params);
00075 static void frame_mode_buffer (ImageParameters *img, InputParameters *params);
00076 static void init_frame(ImageParameters *img);
00077 static void init_field(ImageParameters *img);
00078 static void put_buffer_frame(ImageParameters *img);
00079 static void put_buffer_top (ImageParameters *img);
00080 static void put_buffer_bot (ImageParameters *img);
00081 static void PaddAutoCropBorders (FrameFormat output, int img_size_x, int img_size_y, int img_size_x_cr, int img_size_y_cr, imgpel **pImage[3]);
00082 static void rdPictureCoding(void);
00083
00084 static void ReportFirstframe(int64 tmp_time, int64 me_time);
00085 static void ReportIntra(int64 tmp_time, int64 me_time);
00086 static void ReportP(int64 tmp_time, int64 me_time);
00087 static void ReportB(int64 tmp_time, int64 me_time);
00088 static void ReportNALNonVLCBits(int64 tmp_time, int64 me_time);
00089
00090 StorablePicture *enc_picture;
00091 StorablePicture **enc_frame_picture;
00092 StorablePicture **enc_field_picture;
00093 StorablePicture *enc_frame_picture_JV[MAX_PLANE];
00094
00095 void MbAffPostProc(void)
00096 {
00097 imgpel temp[32][16];
00098
00099 imgpel ** imgY = enc_picture->imgY;
00100 imgpel ***imgUV = enc_picture->imgUV;
00101 int i, y, x0, y0, uv;
00102
00103 if (img->yuv_format != YUV400)
00104 {
00105 for (i=0; i<(int)img->PicSizeInMbs; i+=2)
00106 {
00107 if (enc_picture->motion.mb_field[i])
00108 {
00109 get_mb_pos(i, img->mb_size[IS_LUMA], &x0, &y0);
00110 for (y=0; y<(2*MB_BLOCK_SIZE);y++)
00111 memcpy(&temp[y],&imgY[y0+y][x0], MB_BLOCK_SIZE * sizeof(imgpel));
00112
00113 for (y=0; y<MB_BLOCK_SIZE;y++)
00114 {
00115 memcpy(&imgY[y0+(2*y)][x0],temp[y], MB_BLOCK_SIZE * sizeof(imgpel));
00116 memcpy(&imgY[y0+(2*y + 1)][x0],temp[y+ MB_BLOCK_SIZE], MB_BLOCK_SIZE * sizeof(imgpel));
00117 }
00118
00119 x0 = x0 / (16/img->mb_cr_size_x);
00120 y0 = y0 / (16/img->mb_cr_size_y);
00121
00122 for (uv=0; uv<2; uv++)
00123 {
00124 for (y=0; y < (2 * img->mb_cr_size_y); y++)
00125 memcpy(&temp[y],&imgUV[uv][y0+y][x0], img->mb_cr_size_x * sizeof(imgpel));
00126
00127 for (y=0; y<img->mb_cr_size_y;y++)
00128 {
00129 memcpy(&imgUV[uv][y0+(2*y)][x0],temp[y], img->mb_cr_size_x * sizeof(imgpel));
00130 memcpy(&imgUV[uv][y0+(2*y + 1)][x0],temp[y+ img->mb_cr_size_y], img->mb_cr_size_x * sizeof(imgpel));
00131 }
00132 }
00133 }
00134 }
00135 }
00136 else
00137 {
00138 for (i=0; i<(int)img->PicSizeInMbs; i+=2)
00139 {
00140 if (enc_picture->motion.mb_field[i])
00141 {
00142 get_mb_pos(i, img->mb_size[IS_LUMA], &x0, &y0);
00143 for (y=0; y<(2*MB_BLOCK_SIZE);y++)
00144 memcpy(&temp[y],&imgY[y0+y][x0], MB_BLOCK_SIZE * sizeof(imgpel));
00145
00146 for (y=0; y<MB_BLOCK_SIZE;y++)
00147 {
00148 memcpy(&imgY[y0+(2*y)][x0],temp[y], MB_BLOCK_SIZE * sizeof(imgpel));
00149 memcpy(&imgY[y0+(2*y + 1)][x0],temp[y+ MB_BLOCK_SIZE], MB_BLOCK_SIZE * sizeof(imgpel));
00150 }
00151 }
00152 }
00153 }
00154 }
00155
00156
00157
00158
00159
00160
00161
00162
00163 void set_slice_type(ImageParameters *p_img, int slice_type)
00164 {
00165 p_img->type = slice_type;
00166 p_img->RCMinQP = params->RCMinQP[p_img->type];
00167 p_img->RCMaxQP = params->RCMaxQP[p_img->type];
00168 }
00169
00170 void code_a_plane(ImageParameters *img, Picture *pic)
00171 {
00172 unsigned int NumberOfCodedMBs = 0;
00173 int SliceGroup = 0;
00174
00175
00176 img->slice_group_change_cycle=1;
00177 FmoInit(img, active_pps, active_sps);
00178 FmoStartPicture ();
00179
00180 CalculateQuantParam();
00181 CalculateOffsetParam();
00182
00183 if(params->Transform8x8Mode)
00184 {
00185 CalculateQuant8Param();
00186 CalculateOffset8Param();
00187 }
00188
00189 reset_pic_bin_count();
00190 img->bytes_in_picture = 0;
00191
00192 while (NumberOfCodedMBs < img->PicSizeInMbs)
00193 {
00194
00195 while (!FmoSliceGroupCompletelyCoded (SliceGroup))
00196 {
00197
00198 if (!img->MbaffFrameFlag)
00199 NumberOfCodedMBs += encode_one_slice (SliceGroup, pic, NumberOfCodedMBs);
00200 else
00201 NumberOfCodedMBs += encode_one_slice_MBAFF (SliceGroup, pic, NumberOfCodedMBs);
00202 FmoSetLastMacroblockInSlice (img->current_mb_nr);
00203
00204 img->current_slice_nr++;
00205 stats->bit_slice = 0;
00206 }
00207
00208 SliceGroup++;
00209 }
00210 FmoEndPicture ();
00211
00212 if ((params->SkipDeBlockNonRef == 0) || (img->nal_reference_idc != 0))
00213 DeblockFrame (img, enc_picture->imgY, enc_picture->imgUV);
00214 }
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 static void code_a_picture(Picture *pic)
00227 {
00228 int pl;
00229
00230 img->currentPicture = pic;
00231 img->currentPicture->idr_flag = get_idr_flag();
00232
00233 if (img->currentPicture->idr_flag && params->EnableIDRGOP && img->frm_number)
00234 {
00235 img->last_idr_number = img->frm_no_in_file;
00236 }
00237
00238 pic->no_slices = 0;
00239
00240 RandomIntraNewPicture ();
00241
00242 if( IS_INDEPENDENT( params ) )
00243 {
00244 for( pl=0; pl<MAX_PLANE; pl++ )
00245 {
00246 img->current_mb_nr = 0;
00247 img->current_slice_nr = 0;
00248 img->SumFrameQP = 0;
00249 img->colour_plane_id = pl;
00250
00251 code_a_plane(img, pic);
00252 }
00253 }
00254 else
00255 {
00256 code_a_plane(img, pic);
00257 }
00258
00259 if (img->MbaffFrameFlag)
00260 MbAffPostProc();
00261 }
00262
00263
00264
00265
00266
00267
00268
00269 int get_idr_flag( void )
00270 {
00271 int idr_flag;
00272 int idr_refresh;
00273
00274
00275 if ( params->idr_period && !params->adaptive_idr_period )
00276 {
00277 idr_refresh = (( ( img->frm_number - img->lastIDRnumber ) % params->idr_period ) == 0);
00278 }
00279 else if ( params->idr_period && params->adaptive_idr_period == 1 )
00280 {
00281 idr_refresh = (( ( img->frm_number - imax(img->lastIntraNumber, img->lastIDRnumber) ) % params->idr_period ) == 0);
00282 }
00283 else
00284 {
00285 idr_refresh = (img->frm_number == 0);
00286 }
00287
00288 idr_flag = ((!img->gop_number) && (!(img->structure==BOTTOM_FIELD)))
00289 || (idr_refresh && (img->type == I_SLICE || img->type==SI_SLICE)&& (!(img->structure==BOTTOM_FIELD)));
00290
00291 return idr_flag;
00292 }
00293
00294
00295
00296
00297
00298
00299
00300 void update_global_stats(StatParameters *gl_stats, StatParameters *cur_stats)
00301 {
00302 int i, j, k;
00303
00304 if (params->skip_gl_stats == 0)
00305 {
00306 for (i = 0; i < 4; i++)
00307 {
00308 gl_stats->intra_chroma_mode[i] += cur_stats->intra_chroma_mode[i];
00309 }
00310
00311 for (i = 0; i < 5; i++)
00312 {
00313 gl_stats->quant[i] += cur_stats->quant[i];
00314 gl_stats->num_macroblocks[i] += cur_stats->num_macroblocks[i];
00315 gl_stats->bit_use_mb_type [i] += cur_stats->bit_use_mb_type[i];
00316 gl_stats->bit_use_header [i] += cur_stats->bit_use_header[i];
00317 gl_stats->tmp_bit_use_cbp [i] += cur_stats->tmp_bit_use_cbp[i];
00318 gl_stats->bit_use_coeffC [i] += cur_stats->bit_use_coeffC[i];
00319 gl_stats->bit_use_coeff[0][i] += cur_stats->bit_use_coeff[0][i];
00320 gl_stats->bit_use_coeff[1][i] += cur_stats->bit_use_coeff[1][i];
00321 gl_stats->bit_use_coeff[2][i] += cur_stats->bit_use_coeff[2][i];
00322 gl_stats->bit_use_delta_quant[i] += cur_stats->bit_use_delta_quant[i];
00323 gl_stats->bit_use_stuffingBits[i] += cur_stats->bit_use_stuffingBits[i];
00324
00325 for (k = 0; k < 2; k++)
00326 gl_stats->b8_mode_0_use[i][k] += cur_stats->b8_mode_0_use[i][k];
00327
00328 for (j = 0; j < 15; j++)
00329 {
00330 gl_stats->mode_use[i][j] += cur_stats->mode_use[i][j];
00331 gl_stats->bit_use_mode[i][j] += cur_stats->bit_use_mode[i][j];
00332 for (k = 0; k < 2; k++)
00333 gl_stats->mode_use_transform[i][j][k] += cur_stats->mode_use_transform[i][j][k];
00334 }
00335 }
00336 }
00337 }
00338
00339
00340
00341
00342
00343
00344
00345 void free_pictures(int stored_pic)
00346 {
00347 int i;
00348 for (i = 0; i < 6; i++)
00349 {
00350 if (i != stored_pic)
00351 free_storable_picture(enc_frame_picture[i]);
00352 }
00353 }
00354
00355
00356
00357
00358
00359
00360
00361 int encode_one_frame (ImageParameters *img)
00362 {
00363 static int prev_frame_no = 0;
00364 static int consecutive_non_reference_pictures = 0;
00365 int i, j;
00366 int nplane;
00367
00368
00369 int bits = 0;
00370
00371 #ifdef _LEAKYBUCKET_
00372
00373 extern unsigned long total_frame_buffer;
00374 #endif
00375
00376 TIME_T start_time;
00377 TIME_T end_time;
00378 int64 tmp_time;
00379
00380 me_time = 0;
00381 img->rd_pass = 0;
00382
00383 if( IS_INDEPENDENT(params) )
00384 {
00385 for( nplane=0; nplane<MAX_PLANE; nplane++ ){
00386 enc_frame_picture_JV[nplane] = NULL;
00387 }
00388 }
00389
00390 for (i = 0; i < 6; i++)
00391 enc_frame_picture[i] = NULL;
00392
00393 gettime(&start_time);
00394
00395
00396 img->write_macroblock = FALSE;
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410 put_buffer_frame (img);
00411
00412 init_frame (img);
00413
00414 if(params->WPIterMC)
00415 img->frameOffsetAvail = 0;
00416
00417 ReadOneFrame (¶ms->input_file1, img->frm_no_in_file, params->infile_header, ¶ms->source, ¶ms->output, imgData1.frm_data);
00418 PaddAutoCropBorders (params->output, img->width, img->height, img->width_cr, img->height_cr, imgData1.frm_data);
00419
00420 ProcessImage( params );
00421
00422
00423
00424
00425 img->direct_spatial_mv_pred_flag = params->direct_spatial_mv_pred_flag;
00426
00427 img->DFDisableIdc = params->DFDisableIdc[img->nal_reference_idc > 0][img->type];
00428 img->DFAlphaC0Offset = params->DFAlpha [img->nal_reference_idc > 0][img->type];
00429 img->DFBetaOffset = params->DFBeta [img->nal_reference_idc > 0][img->type];
00430
00431 img->AdaptiveRounding = params->AdaptiveRounding;
00432
00433
00434
00435
00436 dist->frame_ctr++;
00437
00438 if(img->type == SP_SLICE)
00439 {
00440 if(params->sp2_frame_indicator)
00441 {
00442 sp2_frame_indicator=1;
00443 read_SP_coefficients();
00444 }
00445 }
00446 else
00447 {
00448 sp2_frame_indicator=0;
00449 }
00450 if ( params->WPMCPrecision )
00451 {
00452 wpxInitWPXPasses(params);
00453 pWPX->curr_wp_rd_pass = pWPX->wp_rd_passes;
00454 pWPX->curr_wp_rd_pass->algorithm = WP_REGULAR;
00455 }
00456
00457 if (params->PicInterlace == FIELD_CODING)
00458 {
00459
00460 if ( params->RCEnable && params->RCUpdateMode <= MAX_RC_MODE )
00461 generic_RC->FieldControl = 1;
00462
00463 img->field_picture = 1;
00464 field_picture (field_pic[0], field_pic[1]);
00465 img->fld_flag = TRUE;
00466 }
00467 else
00468 {
00469 int tmpFrameQP;
00470
00471 if ( params->RCEnable && params->RCUpdateMode <= MAX_RC_MODE )
00472 generic_RC->FieldControl = 0;
00473
00474 img->field_picture = 0;
00475
00476
00477 if(params->RCEnable)
00478 rc_init_frame(img->frm_no_in_file);
00479
00480 if (params->GenerateMultiplePPS)
00481 active_pps = PicParSet[0];
00482
00483 frame_picture (frame_pic[0], &imgData, 0);
00484
00485 if(params->WPIterMC)
00486 img->frameOffsetAvail = 1;
00487
00488 if ((params->RDPictureIntra || img->type!=I_SLICE) && params->RDPictureDecision)
00489 {
00490 rdPictureCoding();
00491 }
00492
00493 tmpFrameQP = img->SumFrameQP;
00494
00495 if ((img->type==SP_SLICE) && (si_frame_indicator==0) && (params->si_frame_indicator))
00496 {
00497
00498 si_frame_indicator=1;
00499 frame_picture (frame_pic_si, &imgData, 0);
00500 }
00501
00502 if ((img->type == SP_SLICE) && (params->sp_output_indicator))
00503 {
00504
00505 output_SP_coefficients();
00506 }
00507
00508 if (params->PicInterlace == ADAPTIVE_CODING)
00509 {
00510
00511 if ( params->RCEnable && params->RCUpdateMode <= MAX_RC_MODE )
00512 generic_RC->FieldControl=1;
00513 img->write_macroblock = FALSE;
00514 img->bot_MB = FALSE;
00515
00516 img->field_picture = 1;
00517 field_picture (field_pic[0], field_pic[1]);
00518
00519 if(img->rd_pass == 0)
00520 img->fld_flag = picture_structure_decision (frame_pic[0], field_pic[0], field_pic[1]);
00521 else if(img->rd_pass == 1)
00522 img->fld_flag = picture_structure_decision (frame_pic[1], field_pic[0], field_pic[1]);
00523 else
00524 img->fld_flag = picture_structure_decision (frame_pic[2], field_pic[0], field_pic[1]);
00525
00526 if ( img->fld_flag )
00527 tmpFrameQP = img->SumFrameQP;
00528
00529 update_field_frame_contexts (img->fld_flag);
00530
00531
00532 if ( params->RCEnable && params->RCUpdateMode <= MAX_RC_MODE )
00533 generic_RC->FieldFrame = !(img->fld_flag) ? 1 : 0;
00534 }
00535 else
00536 img->fld_flag = FALSE;
00537
00538 img->SumFrameQP = tmpFrameQP;
00539 }
00540
00541 stats->frame_counter++;
00542 stats->frame_ctr[img->type]++;
00543
00544
00545
00546
00547
00548 if (img->fld_flag)
00549 {
00550 field_mode_buffer (img, params);
00551 write_non_vcl_nalu();
00552 writeout_picture (field_pic[0]);
00553 writeout_picture (field_pic[1]);
00554 }
00555 else
00556 {
00557 frame_mode_buffer (img, params);
00558
00559 if (img->type==SP_SLICE && si_frame_indicator == 1)
00560 {
00561 write_non_vcl_nalu();
00562 writeout_picture (frame_pic_si);
00563 si_frame_indicator=0;
00564 }
00565 else
00566 {
00567 write_non_vcl_nalu();
00568 writeout_picture (frame_pic[img->rd_pass]);
00569 }
00570 }
00571
00572 if (frame_pic_si)
00573 {
00574 free_slice_list(frame_pic_si);
00575 }
00576
00577 for (i = 0; i < img->frm_iter; i++)
00578 {
00579 if (frame_pic[i])
00580 {
00581 free_slice_list(frame_pic[i]);
00582 }
00583 }
00584
00585 if (field_pic)
00586 {
00587 for (i = 0; i < 2; i++)
00588 {
00589 if (field_pic[i])
00590 free_slice_list(field_pic[i]);
00591 }
00592 }
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604 if(params->RCEnable)
00605 {
00606
00607 bits = (int) (stats->bit_ctr - stats->bit_ctr_n);
00608 if ( params->RCUpdateMode <= MAX_RC_MODE )
00609 rc_update_pict_frame_ptr(quadratic_RC, bits);
00610 }
00611
00612 if (params->PicInterlace == FRAME_CODING)
00613 {
00614 if ((params->rdopt == 3) && (img->nal_reference_idc != 0))
00615 {
00616 UpdateDecoders (params, img, enc_picture);
00617 }
00618
00619 if (params->RestrictRef)
00620 UpdatePixelMap ();
00621 }
00622
00623 compute_distortion(&imgData);
00624
00625
00626
00627 if(params->redundant_pic_flag)
00628 {
00629 int k;
00630 if(key_frame)
00631 {
00632 for(j=0; j<img->height; j++)
00633 {
00634 memcpy(imgY_tmp[j], enc_frame_picture[0]->imgY[j], img->width * sizeof(imgpel));
00635 }
00636
00637 for (k = 0; k < 2; k++)
00638 {
00639 for(j=0; j<img->height_cr; j++)
00640 {
00641 memcpy(imgUV_tmp[k][j], enc_frame_picture[0]->imgUV[k][j], img->width_cr * sizeof(imgpel));
00642 }
00643 }
00644 }
00645
00646 if(redundant_coding)
00647 {
00648 for(j=0; j<img->height; j++)
00649 {
00650 memcpy(enc_frame_picture[0]->imgY[j], imgY_tmp[j], img->width * sizeof(imgpel));
00651 }
00652 for (k = 0; k < 2; k++)
00653 {
00654 for(j=0; j<img->height_cr; j++)
00655 {
00656 memcpy(enc_frame_picture[0]->imgUV[k][j], imgUV_tmp[k][j], img->width_cr * sizeof(imgpel));
00657 }
00658 }
00659 }
00660 }
00661
00662 if (params->PicInterlace == ADAPTIVE_CODING)
00663 {
00664 if (img->fld_flag)
00665 {
00666 update_global_stats(&statistics, &enc_field_picture[0]->stats);
00667 update_global_stats(&statistics, &enc_field_picture[1]->stats);
00668
00669 store_picture_in_dpb(enc_field_picture[1]);
00670 free_storable_picture(enc_frame_picture[0]);
00671 free_storable_picture(enc_frame_picture[1]);
00672 free_storable_picture(enc_frame_picture[2]);
00673 }
00674 else
00675 {
00676 update_global_stats(&statistics, &enc_frame_picture[img->rd_pass]->stats);
00677
00678 if (img->rd_pass==2)
00679 {
00680 replace_top_pic_with_frame(enc_frame_picture[2]);
00681 free_storable_picture(enc_frame_picture[0]);
00682 free_storable_picture(enc_frame_picture[1]);
00683 }
00684 else if (img->rd_pass==1)
00685 {
00686 replace_top_pic_with_frame(enc_frame_picture[1]);
00687 free_storable_picture(enc_frame_picture[0]);
00688 free_storable_picture(enc_frame_picture[2]);
00689 }
00690 else
00691 {
00692 if(params->redundant_pic_flag==0 || (key_frame==0))
00693 {
00694 replace_top_pic_with_frame(enc_frame_picture[0]);
00695 free_storable_picture(enc_frame_picture[1]);
00696 free_storable_picture(enc_frame_picture[2]);
00697 }
00698 }
00699 free_storable_picture(enc_field_picture[1]);
00700 }
00701 }
00702 else
00703 {
00704 if (img->fld_flag)
00705 {
00706 update_global_stats(&statistics, &enc_field_picture[0]->stats);
00707 update_global_stats(&statistics, &enc_field_picture[1]->stats);
00708 store_picture_in_dpb(enc_field_picture[1]);
00709 }
00710 else
00711 {
00712 if ((params->redundant_pic_flag != 1) || (key_frame == 0))
00713 {
00714 update_global_stats(&statistics, &enc_frame_picture[img->rd_pass]->stats);
00715 store_picture_in_dpb (enc_frame_picture[img->rd_pass]);
00716 free_pictures(img->rd_pass);
00717 }
00718 }
00719 }
00720
00721 img->AverageFrameQP = isign(img->SumFrameQP) * ((iabs(img->SumFrameQP) + (int) (img->FrameSizeInMbs >> 1))/ (int) img->FrameSizeInMbs);
00722
00723 if ( params->RCEnable && params->RCUpdateMode <= MAX_RC_MODE && img->type != B_SLICE && params->basicunit < img->FrameSizeInMbs )
00724 quadratic_RC->CurrLastQP = img->AverageFrameQP;
00725
00726 #ifdef _LEAKYBUCKET_
00727
00728 if (!redundant_coding)
00729 {
00730 Bit_Buffer[total_frame_buffer] = (long) (stats->bit_ctr - stats->bit_ctr_n);
00731 total_frame_buffer++;
00732 }
00733 #endif
00734
00735
00736
00737 if (img->pic_order_cnt_type == 2)
00738 {
00739 if (!img->nal_reference_idc) consecutive_non_reference_pictures++;
00740 else consecutive_non_reference_pictures = 0;
00741
00742 if (img->frame_no < prev_frame_no || consecutive_non_reference_pictures>1)
00743 error("POC type 2 cannot be applied for the coding pattern where the encoding /decoding order of pictures are different from the output order.\n", -1);
00744 prev_frame_no = img->frame_no;
00745 }
00746
00747 gettime(&end_time);
00748 tmp_time = timediff(&start_time, &end_time);
00749 tot_time += tmp_time;
00750 tmp_time = timenorm(tmp_time);
00751 me_time = timenorm(me_time);
00752
00753 if (stats->bit_ctr_parametersets_n!=0 && params->Verbose != 3)
00754 ReportNALNonVLCBits(tmp_time, me_time);
00755
00756 if (img->frm_number == 0)
00757 ReportFirstframe(tmp_time,me_time);
00758 else
00759 {
00760
00761 if(params->RCEnable)
00762 {
00763 if ((!params->PicInterlace) && (!params->MbInterlace))
00764 bits = (int) (stats->bit_ctr - stats->bit_ctr_n);
00765 else if ( params->RCUpdateMode <= MAX_RC_MODE )
00766 {
00767 bits = (int)(stats->bit_ctr - (quadratic_RC->Pprev_bits));
00768 quadratic_RC->Pprev_bits = stats->bit_ctr;
00769 }
00770 }
00771
00772 stats->bit_counter[img->type] += stats->bit_ctr - stats->bit_ctr_n;
00773
00774 switch (img->type)
00775 {
00776 case I_SLICE:
00777 case SI_SLICE:
00778 ReportIntra(tmp_time,me_time);
00779 break;
00780 case B_SLICE:
00781 ReportB(tmp_time,me_time);
00782 break;
00783 default:
00784 ReportP(tmp_time,me_time);
00785 }
00786 }
00787
00788 if (params->Verbose == 0)
00789 {
00790
00791
00792
00793 printf("Completed Encoding Frame %05d.\r", img->frame_no);
00794 }
00795
00796 fflush(stdout);
00797
00798
00799 if(params->RCEnable)
00800 rc_update_picture_ptr( bits );
00801
00802 stats->bit_ctr_n = stats->bit_ctr;
00803
00804 stats->bit_ctr_parametersets_n = 0;
00805
00806 if ( img->type == I_SLICE && img->nal_reference_idc)
00807 {
00808
00809
00810 if ( !(img->b_frame_to_code) )
00811 {
00812 img->lastINTRA = imax(img->lastINTRA, img->frame_no);
00813 img->lastIntraNumber = img->frm_number;
00814 }
00815 if ( img->currentPicture->idr_flag )
00816 {
00817 img->lastIDRnumber = img->frm_number;
00818 }
00819 }
00820
00821 return ((img->gop_number == 0)? 0 : 1);
00822 }
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835 static void writeout_picture(Picture *pic)
00836 {
00837 int partition, slice;
00838 Slice *currSlice;
00839
00840 img->currentPicture = pic;
00841
00842
00843 for (slice=0; slice < pic->no_slices; slice++)
00844 {
00845 currSlice = pic->slices[slice];
00846
00847
00848 for (partition=0; partition < currSlice->max_part_nr; partition++)
00849 {
00850
00851 if (currSlice->partArr[partition].bitstream->write_flag )
00852 {
00853 stats->bit_ctr += WriteNALU (currSlice->partArr[partition].nal_unit);
00854 }
00855 }
00856 }
00857 }
00858
00859
00860 void copy_params(StorablePicture *enc_picture, seq_parameter_set_rbsp_t *active_sps)
00861 {
00862 enc_picture->frame_mbs_only_flag = active_sps->frame_mbs_only_flag;
00863 enc_picture->frame_cropping_flag = active_sps->frame_cropping_flag;
00864 enc_picture->chroma_format_idc = active_sps->chroma_format_idc;
00865
00866 if (active_sps->frame_cropping_flag)
00867 {
00868 enc_picture->frame_cropping_rect_left_offset = active_sps->frame_cropping_rect_left_offset;
00869 enc_picture->frame_cropping_rect_right_offset = active_sps->frame_cropping_rect_right_offset;
00870 enc_picture->frame_cropping_rect_top_offset = active_sps->frame_cropping_rect_top_offset;
00871 enc_picture->frame_cropping_rect_bottom_offset = active_sps->frame_cropping_rect_bottom_offset;
00872 }
00873 else
00874 {
00875 enc_picture->frame_cropping_rect_left_offset = 0;
00876 enc_picture->frame_cropping_rect_right_offset = 0;
00877 enc_picture->frame_cropping_rect_top_offset = 0;
00878 enc_picture->frame_cropping_rect_bottom_offset = 0;
00879 }
00880 }
00881
00882
00883
00884
00885
00886
00887
00888 static void prepare_enc_frame_picture (StorablePicture **stored_pic)
00889 {
00890 (*stored_pic) = alloc_storable_picture ((PictureStructure) img->structure, img->width, img->height, img->width_cr, img->height_cr);
00891
00892 img->ThisPOC = img->framepoc;
00893 (*stored_pic)->poc = img->framepoc;
00894 (*stored_pic)->top_poc = img->toppoc;
00895 (*stored_pic)->bottom_poc = img->bottompoc;
00896 (*stored_pic)->frame_poc = img->framepoc;
00897 (*stored_pic)->pic_num = img->frame_num;
00898 (*stored_pic)->frame_num = img->frame_num;
00899 (*stored_pic)->coded_frame = 1;
00900 (*stored_pic)->MbaffFrameFlag = img->MbaffFrameFlag = (params->MbInterlace != FRAME_CODING);
00901
00902 get_mb_block_pos = img->MbaffFrameFlag ? get_mb_block_pos_mbaff : get_mb_block_pos_normal;
00903 getNeighbour = img->MbaffFrameFlag ? getAffNeighbour : getNonAffNeighbour;
00904 enc_picture = *stored_pic;
00905
00906 copy_params(enc_picture, active_sps);
00907 }
00908
00909 static void calc_picture_bits(Picture *frame)
00910 {
00911 int i, j;
00912 Slice *thisSlice = NULL;
00913
00914 frame->bits_per_picture = 0;
00915
00916 for ( i = 0; i < frame->no_slices; i++ )
00917 {
00918 thisSlice = frame->slices[i];
00919
00920 for ( j = 0; j < thisSlice->max_part_nr; j++ )
00921 frame->bits_per_picture += 8 * ((thisSlice->partArr[j]).bitstream)->byte_pos;
00922 }
00923 }
00924
00925
00926
00927
00928
00929
00930 void frame_picture (Picture *frame, ImageData *imgData, int rd_pass)
00931 {
00932 int nplane;
00933 img->SumFrameQP = 0;
00934 img->structure = FRAME;
00935 img->PicSizeInMbs = img->FrameSizeInMbs;
00936
00937 update_mv_limits(img, FALSE);
00938
00939
00940 if( IS_INDEPENDENT(params) )
00941 {
00942 for( nplane=0; nplane<MAX_PLANE; nplane++ )
00943 {
00944 prepare_enc_frame_picture( &enc_frame_picture_JV[nplane] );
00945 }
00946 }
00947 else
00948 {
00949 prepare_enc_frame_picture( &enc_frame_picture[rd_pass] );
00950 }
00951
00952
00953 img->fld_flag = FALSE;
00954 code_a_picture(frame);
00955
00956 if( IS_INDEPENDENT(params) )
00957 {
00958 make_frame_picture_JV();
00959 }
00960
00961 calc_picture_bits(frame);
00962
00963 if (img->structure==FRAME)
00964 {
00965 find_distortion (imgData);
00966 frame->distortion = dist->metric[SSE];
00967 }
00968 }
00969
00970
00971
00972
00973
00974
00975
00976
00977 static void field_picture (Picture *top, Picture *bottom)
00978 {
00979
00980 int old_pic_type;
00981 int TopFieldBits;
00982 img->SumFrameQP = 0;
00983
00984 update_mv_limits(img, TRUE);
00985
00986
00987 old_pic_type = img->type;
00988
00989 img->number *= 2;
00990 img->gop_number = (img->number - img->start_frame_no);
00991 img->buf_cycle *= 2;
00992 img->height = (params->output.height + img->auto_crop_bottom) / 2;
00993 img->height_cr = img->height_cr_frame / 2;
00994 img->fld_flag = TRUE;
00995 img->PicSizeInMbs = img->FrameSizeInMbs/2;
00996
00997
00998 enc_field_picture[0] = alloc_storable_picture ((PictureStructure) img->structure, img->width, img->height, img->width_cr, img->height_cr);
00999 enc_field_picture[0]->poc = img->toppoc;
01000 enc_field_picture[0]->frame_poc = img->toppoc;
01001 enc_field_picture[0]->pic_num = img->frame_num;
01002 enc_field_picture[0]->frame_num = img->frame_num;
01003 enc_field_picture[0]->coded_frame = 0;
01004 enc_field_picture[0]->MbaffFrameFlag = img->MbaffFrameFlag = FALSE;
01005 get_mb_block_pos = get_mb_block_pos_normal;
01006 getNeighbour = getNonAffNeighbour;
01007 img->ThisPOC = img->toppoc;
01008
01009 img->structure = TOP_FIELD;
01010 enc_picture = enc_field_picture[0];
01011 copy_params(enc_picture, active_sps);
01012
01013 put_buffer_top (img);
01014 init_field (img);
01015
01016 img->fld_flag = TRUE;
01017
01018
01019 if(params->RCEnable && params->RCUpdateMode <= MAX_RC_MODE)
01020 rc_init_top_field();
01021
01022 code_a_picture(field_pic[0]);
01023 enc_picture->structure = TOP_FIELD;
01024
01025 store_picture_in_dpb(enc_field_picture[0]);
01026
01027 calc_picture_bits(top);
01028
01029
01030 TopFieldBits=top->bits_per_picture;
01031
01032
01033 enc_field_picture[1] = alloc_storable_picture ((PictureStructure) img->structure, img->width, img->height, img->width_cr, img->height_cr);
01034 enc_field_picture[1]->poc=img->bottompoc;
01035 enc_field_picture[1]->frame_poc = img->bottompoc;
01036 enc_field_picture[1]->pic_num = img->frame_num;
01037 enc_field_picture[1]->frame_num = img->frame_num;
01038 enc_field_picture[1]->coded_frame = 0;
01039 enc_field_picture[1]->MbaffFrameFlag = img->MbaffFrameFlag = FALSE;
01040 get_mb_block_pos = get_mb_block_pos_normal;
01041 getNeighbour = getNonAffNeighbour;
01042
01043 img->ThisPOC = img->bottompoc;
01044 img->structure = BOTTOM_FIELD;
01045 enc_picture = enc_field_picture[1];
01046 copy_params(enc_picture, active_sps);
01047 put_buffer_bot (img);
01048 img->number++;
01049 img->gop_number = (img->number - img->start_frame_no);
01050
01051 init_field (img);
01052
01053 if (img->type == I_SLICE && params->IntraBottom!=1)
01054 set_slice_type(img, (params->BRefPictures == 2) ? B_SLICE : P_SLICE);
01055
01056 img->fld_flag = TRUE;
01057
01058
01059 if(params->RCEnable && params->RCUpdateMode <= MAX_RC_MODE)
01060 rc_init_bottom_field( TopFieldBits );
01061
01062 enc_picture->structure = BOTTOM_FIELD;
01063 code_a_picture(field_pic[1]);
01064
01065 calc_picture_bits(bottom);
01066
01067
01068
01069 distortion_fld (top, &imgData);
01070 }
01071
01072
01073
01074
01075
01076
01077
01078 static void combine_field(void)
01079 {
01080 int i, k;
01081
01082 for (i = 0; i < (img->height >> 1); i++)
01083 {
01084 memcpy(imgY_com[i*2], enc_field_picture[0]->imgY[i], img->width*sizeof(imgpel));
01085 memcpy(imgY_com[i*2 + 1], enc_field_picture[1]->imgY[i], img->width*sizeof(imgpel));
01086 }
01087
01088 if (img->yuv_format != YUV400)
01089 {
01090 for (k = 0; k < 2; k++)
01091 {
01092 for (i = 0; i < (img->height_cr >> 1); i++)
01093 {
01094 memcpy(imgUV_com[k][i*2], enc_field_picture[0]->imgUV[k][i], img->width_cr*sizeof(imgpel));
01095 memcpy(imgUV_com[k][i*2 + 1], enc_field_picture[1]->imgUV[k][i], img->width_cr*sizeof(imgpel));
01096 }
01097 }
01098 }
01099 }
01100
01101
01102
01103
01104
01105
01106
01107 static void distortion_fld (Picture *field_pic, ImageData *imgData)
01108 {
01109
01110 img->number /= 2;
01111 img->gop_number = (img->number - img->start_frame_no);
01112 img->buf_cycle /= 2;
01113 img->height = (params->output.height + img->auto_crop_bottom);
01114 img->height_cr = img->height_cr_frame;
01115
01116 combine_field ();
01117
01118 pCurImg = imgData->frm_data[0];
01119 pImgOrg[0] = imgData->frm_data[0];
01120
01121 if (params->output.yuv_format != YUV400)
01122 {
01123 pImgOrg[1] = imgData->frm_data[1];
01124 pImgOrg[2] = imgData->frm_data[2];
01125 }
01126
01127 find_distortion (imgData);
01128 field_pic->distortion = dist->metric[SSE];
01129 }
01130
01131
01132
01133
01134
01135
01136
01137
01138 static byte decide_fld_frame(float snr_frame_Y, float snr_field_Y, int bit_field, int bit_frame, double lambda_picture)
01139 {
01140 double cost_frame, cost_field;
01141
01142 cost_frame = bit_frame * lambda_picture + snr_frame_Y;
01143 cost_field = bit_field * lambda_picture + snr_field_Y;
01144
01145 if (cost_field > cost_frame)
01146 return FALSE;
01147 else
01148 return TRUE;
01149 }
01150
01151
01152
01153
01154
01155
01156
01157 static byte picture_structure_decision (Picture *frame, Picture *top, Picture *bot)
01158 {
01159 double lambda_picture;
01160 int bframe = (img->type == B_SLICE);
01161 float sse_frame, sse_field;
01162 int bit_frame, bit_field;
01163
01164 lambda_picture = 0.68 * pow (2, img->bitdepth_lambda_scale + ((img->qp - SHIFT_QP) / 3.0)) * (bframe ? 1 : 1);
01165
01166 sse_frame = frame->distortion.value[0] + frame->distortion.value[1] + frame->distortion.value[2];
01167
01168 sse_field = top->distortion.value[0] + top->distortion.value[1] + top->distortion.value[2];
01169
01170 bit_field = top->bits_per_picture + bot->bits_per_picture;
01171 bit_frame = frame->bits_per_picture;
01172 return decide_fld_frame (sse_frame, sse_field, bit_field, bit_frame, lambda_picture);
01173 }
01174
01175
01176
01177
01178
01179
01180
01181
01182 static void field_mode_buffer (ImageParameters *img, InputParameters *params)
01183 {
01184 put_buffer_frame (img);
01185 }
01186
01187
01188
01189
01190
01191
01192
01193
01194 static void frame_mode_buffer (ImageParameters *img, InputParameters *params)
01195 {
01196 put_buffer_frame (img);
01197
01198 if ((params->PicInterlace != FRAME_CODING)||(params->MbInterlace != FRAME_CODING))
01199 {
01200 img->height = img->height / 2;
01201 img->height_cr = img->height_cr / 2;
01202 img->number *= 2;
01203 img->gop_number = (img->number - img->start_frame_no);
01204
01205 put_buffer_top (img);
01206
01207 img->number++;
01208 img->gop_number = (img->number - img->start_frame_no);
01209 put_buffer_bot (img);
01210
01211 img->number /= 2;
01212 img->gop_number = (img->number - img->start_frame_no);
01213 img->height = (params->output.height + img->auto_crop_bottom);
01214 img->height_cr = img->height_cr_frame;
01215
01216 put_buffer_frame (img);
01217 }
01218 }
01219
01220
01221
01222
01223
01224
01225
01226
01227 static void init_dec_ref_pic_marking_buffer(void)
01228 {
01229 img->dec_ref_pic_marking_buffer=NULL;
01230 }
01231
01232
01233
01234
01235
01236
01237
01238
01239 static void init_frame (ImageParameters *img)
01240 {
01241 int i, j;
01242
01243 img->current_mb_nr = 0;
01244 img->current_slice_nr = 0;
01245 stats->bit_slice = 0;
01246
01247 img->mb_y = img->mb_x = 0;
01248 img->block_y = img->pix_y = img->pix_c_y = 0;
01249 img->block_x = img->pix_x = img->block_c_x = img->pix_c_x = 0;
01250
01251
01252
01253
01254 if( IS_INDEPENDENT(params) )
01255 {
01256 for( j=0; j<MAX_PLANE; j++ ){
01257 for(i=0;i< ((int) (img->FrameSizeInMbs));i++)
01258 img->mb_data_JV[j][i].slice_nr=-1;
01259 }
01260 }
01261 else
01262 {
01263 for(i = 0; i < ((int) (img->FrameSizeInMbs)); i++)
01264 img->mb_data[i].slice_nr = -1;
01265 }
01266
01267 if (img->b_frame_to_code == 0)
01268 {
01269
01270 if(!params->RCEnable)
01271 {
01272 if (img->type == I_SLICE)
01273 {
01274
01275 if ((params->qp2start > 0 && img->frame_no >= params->qp2start && params->sp2_frame_indicator==0)||
01276 ( (params->qp2start > 0) && ( ( (img->frame_no ) % (2*params->qp2start) ) >=params->qp2start ) && (params->sp2_frame_indicator==1)))
01277 img->qp = params->qp[1][I_SLICE];
01278 else
01279 img->qp = params->qp[0][I_SLICE];
01280 if (redundant_coding)
01281 {
01282
01283 img->qp = imin(img->qp + 5, 51);
01284 }
01285 }
01286 else
01287 {
01288
01289 if ((params->qp2start > 0 && img->frame_no >= params->qp2start && params->sp2_frame_indicator==0)||
01290 ( (params->qp2start > 0) && ( ( (img->frame_no ) % (2*params->qp2start) ) >=params->qp2start ) && (params->sp2_frame_indicator==1)))
01291 img->qp = params->qp[1][P_SLICE] + (img->nal_reference_idc ? 0 : params->DispPQPOffset);
01292 else
01293 img->qp = params->qp[0][P_SLICE] + (img->nal_reference_idc ? 0 : params->DispPQPOffset);
01294
01295 if (img->type == SP_SLICE)
01296 {
01297 if ( (params->qp2start > 0) && ( ( (img->frame_no ) % (2*params->qp2start) ) >=params->qp2start ))
01298 {
01299 img->qp = params->qp[1][SP_SLICE];
01300 img->qpsp = params->qpsp[1];
01301 }
01302 else
01303 {
01304 img->qp = params->qp[0][SP_SLICE];
01305 img->qpsp = params->qpsp[0];
01306 }
01307 }
01308 }
01309 }
01310
01311 img->mb_y_intra = img->mb_y_upd;
01312
01313 if (params->intra_upd > 0)
01314 {
01315 img->mb_y_upd = (img->frm_number / params->intra_upd) % (img->height / MB_BLOCK_SIZE);
01316 }
01317 }
01318 else
01319 {
01320
01321 if(!params->RCEnable && params->HierarchicalCoding == 0)
01322 {
01323
01324 if ((params->qp2start > 0 && img->frame_no >= params->qp2start && params->sp2_frame_indicator==0)||
01325 ( (params->qp2start > 0) && ( ( (img->frame_no ) % (2*params->qp2start) ) >=params->qp2start ) && (params->sp2_frame_indicator==1)))
01326 {
01327 img->qp = params->qp[1][B_SLICE];
01328 }
01329 else
01330 {
01331 img->qp = params->qp[0][B_SLICE];
01332 }
01333
01334 if (img->nal_reference_idc)
01335 {
01336
01337 if ((params->qp2start > 0 && img->frame_no >= params->qp2start && params->sp2_frame_indicator==0)||
01338 ( (params->qp2start > 0) && ( ( (img->frame_no ) % (2*params->qp2start) ) >=params->qp2start ) && (params->sp2_frame_indicator==1)))
01339 {
01340 img->qp = iClip3(-img->bitdepth_luma_qp_scale,51,params->qp[1][B_SLICE] + params->qpBRSOffset[1]);
01341 }
01342 else
01343 {
01344 img->qp = iClip3(-img->bitdepth_luma_qp_scale, 51, params->qp[0][B_SLICE] + params->qpBRSOffset[0]);
01345 }
01346 }
01347 }
01348 else if (!params->RCEnable && params->HierarchicalCoding !=0)
01349 {
01350
01351 img->qp = gop_structure[img->b_frame_to_code - 1].slice_qp;
01352 }
01353 }
01354
01355 img->no_output_of_prior_pics_flag = 0;
01356 img->long_term_reference_flag = 0;
01357
01358 init_dec_ref_pic_marking_buffer();
01359
01360 }
01361
01362
01363
01364
01365
01366
01367
01368 static void init_field (ImageParameters *img)
01369 {
01370 img->current_mb_nr = 0;
01371 img->current_slice_nr = 0;
01372 stats->bit_slice = 0;
01373
01374 params->jumpd *= 2;
01375 params->NumberBFrames *= 2;
01376 img->number /= 2;
01377 img->gop_number = (img->number - img->start_frame_no);
01378 img->buf_cycle /= 2;
01379
01380 img->mb_y = img->mb_x = 0;
01381 img->block_y = img->pix_y = img->pix_c_y = 0;
01382 img->block_x = img->pix_x = img->block_c_x = img->pix_c_x = 0;
01383
01384 if (!img->b_frame_to_code)
01385 {
01386
01387 if(!params->RCEnable)
01388 {
01389 if (img->type == I_SLICE)
01390 {
01391
01392 if ((params->qp2start > 0 && img->frame_no >= params->qp2start && params->sp2_frame_indicator==0)||
01393 ( (params->qp2start > 0) && ( ( (img->frame_no ) % (2*params->qp2start) ) >=params->qp2start ) && (params->sp2_frame_indicator==1)))
01394 img->qp = params->qp[1][I_SLICE];
01395 else
01396 img->qp = params->qp[0][I_SLICE];
01397 }
01398 else
01399 {
01400
01401 if ((params->qp2start > 0 && img->frame_no >= params->qp2start && params->sp2_frame_indicator==0)||
01402 ( (params->qp2start > 0) && ( ( (img->frame_no ) % (2*params->qp2start) ) >=params->qp2start ) && (params->sp2_frame_indicator==1)))
01403 img->qp = params->qp[1][P_SLICE] + (img->nal_reference_idc ? 0 : params->DispPQPOffset);
01404 else
01405 img->qp = params->qp[0][P_SLICE] + (img->nal_reference_idc ? 0 : params->DispPQPOffset);
01406
01407 if (img->type == SP_SLICE)
01408 {
01409 if ( (params->qp2start > 0) && ( ( (img->frame_no ) % (2*params->qp2start) ) >=params->qp2start ))
01410 {
01411 img->qp = params->qp[1][SP_SLICE];
01412 img->qpsp = params->qpsp[1];
01413 }
01414 else
01415 {
01416 img->qp = params->qp[0][SP_SLICE];
01417 img->qpsp = params->qpsp[0];
01418 }
01419 }
01420 }
01421 }
01422 img->mb_y_intra = img->mb_y_upd;
01423
01424 if (params->intra_upd > 0)
01425 {
01426 img->mb_y_upd =
01427 (img->number / params->intra_upd) % (img->width / MB_BLOCK_SIZE);
01428 }
01429 }
01430 else
01431 {
01432
01433 if(!params->RCEnable && params->HierarchicalCoding == 0)
01434 {
01435
01436 if ((params->qp2start > 0 && img->frame_no >= params->qp2start && params->sp2_frame_indicator==0)||
01437 ( (params->qp2start > 0) && ( ( (img->frame_no ) % (2*params->qp2start) ) >=params->qp2start ) && (params->sp2_frame_indicator==1)))
01438 {
01439 img->qp = params->qp[1][B_SLICE];
01440 }
01441 else
01442 img->qp = params->qp[0][B_SLICE];
01443 if (img->nal_reference_idc)
01444 {
01445
01446 if ((params->qp2start > 0 && img->frame_no >= params->qp2start && params->sp2_frame_indicator==0)||
01447 ( (params->qp2start > 0) && ( ( (img->frame_no ) % (2*params->qp2start) ) >=params->qp2start ) && (params->sp2_frame_indicator==1)))
01448 {
01449 img->qp = iClip3(-img->bitdepth_luma_qp_scale,51,params->qp[1][B_SLICE] + params->qpBRSOffset[1]);
01450 }
01451 else
01452 img->qp = iClip3(-img->bitdepth_luma_qp_scale,51,params->qp[0][B_SLICE] + params->qpBRSOffset[0]);
01453
01454 }
01455 }
01456 else if (!params->RCEnable && params->HierarchicalCoding != 0)
01457 {
01458 img->qp = gop_structure[img->b_frame_to_code - 1].slice_qp;
01459 }
01460 }
01461 params->jumpd /= 2;
01462 params->NumberBFrames /= 2;
01463 img->buf_cycle *= 2;
01464 img->number = 2 * img->number + img->fld_type;
01465 img->gop_number = (img->number - img->start_frame_no);
01466 }
01467
01468
01469
01470
01471
01472
01473
01474
01475
01476
01477
01478
01479
01480
01481
01482 void UnifiedOneForthPix (StorablePicture *s)
01483 {
01484 int ypadded_size = s->size_y_padded;
01485 int xpadded_size = s->size_x_padded;
01486
01487
01488 if (s->imgY_sub)
01489 return;
01490
01491 get_mem4Dpel (&(s->imgY_sub), 4, 4, ypadded_size, xpadded_size);
01492 if (NULL == s->imgY_sub)
01493 no_mem_exit("alloc_storable_picture: s->imgY_sub");
01494
01495 s->p_img_sub[0] = s->imgY_sub;
01496 s->p_curr_img_sub = s->p_img_sub[0];
01497
01498 if ( params->ChromaMCBuffer || img->P444_joined)
01499 {
01500
01501 if ( img->yuv_format != YUV400 )
01502 {
01503 if ( img->yuv_format == YUV420 )
01504 {
01505 get_mem5Dpel (&(s->imgUV_sub), 2, 8, 8, ypadded_size>>1, xpadded_size>>1);
01506 }
01507 else if ( img->yuv_format == YUV422 )
01508 {
01509 get_mem5Dpel (&(s->imgUV_sub), 2, 4, 8, ypadded_size, xpadded_size>>1);
01510 }
01511 else
01512 {
01513 get_mem5Dpel (&(s->imgUV_sub), 2, 4, 4, ypadded_size, xpadded_size);
01514 }
01515 s->p_img_sub[1] = s->imgUV_sub[0];
01516 s->p_img_sub[2] = s->imgUV_sub[1];
01517 }
01518 else
01519 {
01520 s->p_img_sub[1] = NULL;
01521 s->p_img_sub[2] = NULL;
01522 }
01523 }
01524 else
01525 {
01526 s->p_img_sub[1] = NULL;
01527 s->p_img_sub[2] = NULL;
01528 }
01529 s->p_curr_img = s->imgY;
01530 s->p_curr_img_sub = s->imgY_sub;
01531
01532
01533
01534 if (params->intra_period != 1)
01535 {
01536 getSubImagesLuma ( s );
01537
01538
01539 if ( (img->yuv_format != YUV400) && (params->ChromaMCBuffer) )
01540 {
01541 if (img->P444_joined)
01542 {
01543
01544 select_plane(PLANE_U);
01545 getSubImagesLuma (s);
01546
01547 select_plane(PLANE_V);
01548 getSubImagesLuma (s);
01549
01550 select_plane(PLANE_Y);
01551 }
01552 else
01553 getSubImagesChroma( s );
01554 }
01555 }
01556 }
01557
01558
01559
01560
01561
01562
01563
01564
01565
01566
01567
01568 void UnifiedOneForthPix_JV (int nplane, StorablePicture *s)
01569 {
01570 int ypadded_size = s->size_y_padded;
01571 int xpadded_size = s->size_x_padded;
01572
01573 if( nplane == 0 )
01574 {
01575
01576 if (s->imgY_sub)
01577 return;
01578
01579 get_mem4Dpel (&(s->imgY_sub), 4, 4, ypadded_size, xpadded_size);
01580 if (NULL == s->imgY_sub)
01581 no_mem_exit("UnifiedOneForthPix_JV: s->imgY_sub");
01582
01583
01584 if (s->imgUV_sub)
01585 return;
01586
01587 get_mem5Dpel (&(s->imgUV_sub), 2, 4, 4, ypadded_size, xpadded_size);
01588 if (NULL == s->imgUV_sub)
01589 no_mem_exit("UnifiedOneForthPix_JV: s->imgUV_sub");
01590
01591 s->p_img[0] = s->imgY;
01592 s->p_img[1] = s->imgUV[0];
01593 s->p_img[2] = s->imgUV[1];
01594
01595 s->p_img_sub[0] = s->imgY_sub;
01596 s->p_img_sub[1] = s->imgUV_sub[0];
01597 s->p_img_sub[2] = s->imgUV_sub[1];
01598 }
01599
01600
01601 s->colour_plane_id = nplane;
01602 s->p_curr_img = s->p_img[nplane];
01603 s->p_curr_img_sub = s->p_img_sub[nplane];
01604
01605 getSubImagesLuma ( s );
01606 }
01607
01608
01609
01610
01611
01612
01613
01614
01615
01616 Boolean dummy_slice_too_big (int bits_slice)
01617 {
01618 return FALSE;
01619 }
01620
01621
01622 static inline void copy_motion_vectors_MB (ImageParameters *img, RD_DATA *rdopt)
01623 {
01624 memcpy(&img->all_mv [LIST_0][0][0][0][0][0], &rdopt->all_mv [LIST_0][0][0][0][0][0], 2 * img->max_num_references * 9 * 4 * 4 * 2 * sizeof(short));
01625 }
01626
01627
01628
01629
01630
01631
01632
01633 void copy_rdopt_data (Macroblock *currMB, int bot_block)
01634 {
01635 int i, j;
01636
01637 int bframe = (img->type == B_SLICE);
01638 int mode;
01639 short b8mode, b8pdir;
01640 int block_y;
01641
01642 int list_offset = currMB->list_offset;
01643
01644 mode = rdopt->mode;
01645 currMB->mb_type = rdopt->mb_type;
01646 currMB->cbp = rdopt->cbp;
01647 currMB->cbp_blk = rdopt->cbp_blk;
01648 img->i16offset = rdopt->i16offset;
01649
01650 currMB->prev_qp = rdopt->prev_qp;
01651 currMB->prev_dqp = rdopt->prev_dqp;
01652 currMB->prev_cbp = rdopt->prev_cbp;
01653 currMB->delta_qp = rdopt->delta_qp;
01654 currMB->qp = rdopt->qp;
01655 update_qp (img, currMB);
01656
01657 currMB->c_ipred_mode = rdopt->c_ipred_mode;
01658
01659 memcpy(img->cofAC[0][0][0],rdopt->cofAC[0][0][0], (4 + img->num_blk8x8_uv) * 4 * 2 * 65 * sizeof(int));
01660 memcpy(img->cofDC[0][0],rdopt->cofDC[0][0], 3 * 2 * 18 * sizeof(int));
01661
01662 for (j = 0; j < BLOCK_MULTIPLE; j++)
01663 {
01664 block_y = img->block_y + j;
01665 memcpy(&enc_picture->motion.ref_idx[LIST_0][block_y][img->block_x], rdopt->refar[LIST_0][j], BLOCK_MULTIPLE * sizeof(char));
01666 for (i = 0; i < BLOCK_MULTIPLE; i++)
01667 enc_picture->motion.ref_pic_id [LIST_0][block_y][img->block_x + i] =
01668 enc_picture->ref_pic_num[LIST_0 + list_offset][(short)enc_picture->motion.ref_idx[LIST_0][block_y][img->block_x+i]];
01669 }
01670 if (bframe)
01671 {
01672 for (j = 0; j < BLOCK_MULTIPLE; j++)
01673 {
01674 block_y = img->block_y + j;
01675 memcpy(&enc_picture->motion.ref_idx[LIST_1][block_y][img->block_x], rdopt->refar[LIST_1][j], BLOCK_MULTIPLE * sizeof(char));
01676 for (i = 0; i < BLOCK_MULTIPLE; i++)
01677 enc_picture->motion.ref_pic_id [LIST_1][block_y][img->block_x + i] =
01678 enc_picture->ref_pic_num[LIST_1 + list_offset][(short)enc_picture->motion.ref_idx[LIST_1][block_y][img->block_x + i]];
01679 }
01680 }
01681
01682
01683 for (j = 0; j < MB_BLOCK_SIZE; j++)
01684 memcpy(&enc_picture->imgY[img->pix_y + j][img->pix_x],rdopt->rec_mbY[j], MB_BLOCK_SIZE * sizeof(imgpel));
01685
01686 if (img->yuv_format != YUV400)
01687 {
01688 for (j = 0; j < img->mb_cr_size_y; j++)
01689 {
01690 memcpy(&enc_picture->imgUV[0][img->pix_c_y + j][img->pix_c_x], rdopt->rec_mb_cr[0][j], img->mb_cr_size_x * sizeof(imgpel));
01691 memcpy(&enc_picture->imgUV[1][img->pix_c_y + j][img->pix_c_x], rdopt->rec_mb_cr[1][j], img->mb_cr_size_x * sizeof(imgpel));
01692 }
01693 }
01694
01695 memcpy(currMB->b8mode, rdopt->b8mode, 4 * sizeof(short));
01696 memcpy(currMB->b8pdir, rdopt->b8pdir, 4 * sizeof(short));
01697
01698
01699 currMB->luma_transform_size_8x8_flag = rdopt->luma_transform_size_8x8_flag;
01700
01701
01702 if (mode == P8x8)
01703 {
01704 memcpy(currMB->intra_pred_modes,rdopt->intra_pred_modes, MB_BLOCK_PARTITIONS * sizeof(char));
01705 for (j = img->block_y; j < img->block_y + BLOCK_MULTIPLE; j++)
01706 memcpy(&img->ipredmode[j][img->block_x],&rdopt->ipredmode[j][img->block_x], BLOCK_MULTIPLE * sizeof(char));
01707 }
01708 else if (mode != I4MB && mode != I8MB)
01709 {
01710 memset(currMB->intra_pred_modes,DC_PRED, MB_BLOCK_PARTITIONS * sizeof(char));
01711 for (j = img->block_y; j < img->block_y + BLOCK_MULTIPLE; j++)
01712 memset(&img->ipredmode[j][img->block_x],DC_PRED, BLOCK_MULTIPLE * sizeof(char));
01713 }
01714 else if (mode == I4MB || mode == I8MB)
01715 {
01716 memcpy(currMB->intra_pred_modes,rdopt->intra_pred_modes, MB_BLOCK_PARTITIONS * sizeof(char));
01717 memcpy(currMB->intra_pred_modes8x8,rdopt->intra_pred_modes8x8, MB_BLOCK_PARTITIONS * sizeof(char));
01718 for (j = img->block_y; j < img->block_y + BLOCK_MULTIPLE; j++)
01719 {
01720 memcpy(&img->ipredmode[j][img->block_x],&rdopt->ipredmode[j][img->block_x], BLOCK_MULTIPLE * sizeof(char));
01721 }
01722 }
01723
01724 if (img->MbaffFrameFlag || (params->UseRDOQuant && params->RDOQ_QP_Num > 1))
01725 {
01726
01727 copy_motion_vectors_MB (img, rdopt);
01728
01729 if (!IS_INTRA(currMB))
01730 {
01731 memset(currMB->bipred_me, 0, 4* sizeof(short));
01732
01733 for (j = 0; j < 4; j++)
01734 for (i = 0; i < 4; i++)
01735 {
01736 b8mode = currMB->b8mode[i/2+2*(j/2)];
01737 b8pdir = currMB->b8pdir[i/2+2*(j/2)];
01738
01739 if (b8pdir!=1)
01740 {
01741 enc_picture->motion.mv[LIST_0][j+img->block_y][i+img->block_x][0] = rdopt->all_mv[LIST_0][(short)rdopt->refar[LIST_0][j][i]][b8mode][j][i][0];
01742 enc_picture->motion.mv[LIST_0][j+img->block_y][i+img->block_x][1] = rdopt->all_mv[LIST_0][(short)rdopt->refar[LIST_0][j][i]][b8mode][j][i][1];
01743 }
01744 else
01745 {
01746 enc_picture->motion.mv[LIST_0][j+img->block_y][i+img->block_x][0] = 0;
01747 enc_picture->motion.mv[LIST_0][j+img->block_y][i+img->block_x][1] = 0;
01748 }
01749 if (bframe)
01750 {
01751 if (b8pdir!=0)
01752 {
01753 enc_picture->motion.mv[LIST_1][j+img->block_y][i+img->block_x][0] = rdopt->all_mv[LIST_1][(short)rdopt->refar[LIST_1][j][i]][b8mode][j][i][0];
01754 enc_picture->motion.mv[LIST_1][j+img->block_y][i+img->block_x][1] = rdopt->all_mv[LIST_1][(short)rdopt->refar[LIST_1][j][i]][b8mode][j][i][1];
01755 }
01756 else
01757 {
01758 enc_picture->motion.mv[LIST_1][j+img->block_y][i+img->block_x][0] = 0;
01759 enc_picture->motion.mv[LIST_1][j+img->block_y][i+img->block_x][1] = 0;
01760 }
01761 }
01762 }
01763 }
01764 else
01765 {
01766 for (j = 0; j < 4; j++)
01767 memset(enc_picture->motion.mv[LIST_0][j+img->block_y][img->block_x], 0, 2 * BLOCK_MULTIPLE * sizeof(short));
01768 if (bframe)
01769 {
01770 for (j = 0; j < 4; j++)
01771 memset(enc_picture->motion.mv[LIST_1][j+img->block_y][img->block_x], 0, 2 * BLOCK_MULTIPLE * sizeof(short));
01772 }
01773 }
01774 }
01775 }
01776
01777
01778 static void ReportNALNonVLCBits(int64 tmp_time, int64 me_time)
01779 {
01780
01781 if (params->Verbose != 0)
01782 printf ("%05d(NVB)%8d \n", img->frame_no, stats->bit_ctr_parametersets_n);
01783 }
01784
01785
01786 static void ReportSimple(ImageParameters *cur_img, char *pic_type, int cur_bits, DistMetric *metric, int tmp_time, int me_time)
01787 {
01788 printf ("%05d(%3s)%8d %2d %7.3f %7.3f %7.3f %9d %7d %3s %d\n",
01789 cur_img->frame_no, pic_type, cur_bits,
01790 cur_img->AverageFrameQP,
01791 metric->value[0], metric->value[1], metric->value[2],
01792 tmp_time, me_time,
01793 cur_img->fld_flag ? "FLD" : "FRM",
01794 cur_img->nal_reference_idc);
01795 }
01796
01797 static void ReportVerbose(ImageParameters *cur_img, char *pic_type, int cur_bits, int wp_method, int lambda, DistMetric *mPSNR, int tmp_time, int me_time, int direct_mode)
01798 {
01799 printf ("%05d(%3s)%8d %1d %2d %2d %7.3f %7.3f %7.3f %9d %7d %3s %5d %1d %2d %2d %d %d\n",
01800 cur_img->frame_no, pic_type, cur_bits, wp_method,
01801 cur_img->AverageFrameQP, lambda,
01802 mPSNR->value[0], mPSNR->value[1], mPSNR->value[2],
01803 tmp_time, me_time,
01804 cur_img->fld_flag ? "FLD" : "FRM", intras, direct_mode,
01805 cur_img->num_ref_idx_l0_active, cur_img->num_ref_idx_l1_active, cur_img->rd_pass, cur_img->nal_reference_idc);
01806 }
01807
01808 static void ReportVerboseNVB(ImageParameters *cur_img, char *pic_type, int cur_bits, int nvb_bits, int wp_method, int lambda, DistMetric *mPSNR, int tmp_time, int me_time, int direct_mode)
01809 {
01810 printf ("%05d(%3s)%8d %3d %1d %2d %2d %7.3f %7.3f %7.3f %9d %7d %3s %5d %1d %2d %2d %d %d\n",
01811 cur_img->frame_no, pic_type, cur_bits, nvb_bits, wp_method,
01812 cur_img->AverageFrameQP, lambda,
01813 mPSNR->value[0], mPSNR->value[1], mPSNR->value[2],
01814 tmp_time, me_time,
01815 cur_img->fld_flag ? "FLD" : "FRM", intras, direct_mode,
01816 cur_img->num_ref_idx_l0_active, cur_img->num_ref_idx_l1_active, cur_img->rd_pass, cur_img->nal_reference_idc);
01817 }
01818
01819 static void ReportVerboseSSIM(ImageParameters *cur_img, char *pic_type, int cur_bits, int wp_method, int lambda, DistMetric *mPSNR, DistMetric *mSSIM,int tmp_time, int me_time, int direct_mode)
01820 {
01821 printf ("%05d(%3s)%8d %1d %2d %2d %7.3f %7.3f %7.3f %7.4f %7.4f %7.4f %9d %7d %3s %5d %1d %2d %2d %d %d\n",
01822 cur_img->frame_no, pic_type, cur_bits, wp_method,
01823 cur_img->AverageFrameQP, lambda,
01824 mPSNR->value[0], mPSNR->value[1], mPSNR->value[2],
01825 mSSIM->value[0], mSSIM->value[1], mSSIM->value[2],
01826 tmp_time, me_time,
01827 img->fld_flag ? "FLD" : "FRM", intras, direct_mode,
01828 img->num_ref_idx_l0_active, img->num_ref_idx_l1_active,img->rd_pass, img->nal_reference_idc);
01829 }
01830
01831 static void ReportVerboseNVBSSIM(ImageParameters *cur_img, char *pic_type, int cur_bits, int nvb_bits, int wp_method, int lambda, DistMetric *mPSNR, DistMetric *mSSIM,int tmp_time, int me_time, int direct_mode)
01832 {
01833 printf ("%05d(%3s)%8d %3d %1d %2d %2d %7.3f %7.3f %7.3f %7.4f %7.4f %7.4f %9d %7d %3s %5d %1d %2d %2d %d %d\n",
01834 cur_img->frame_no, pic_type, cur_bits, nvb_bits, wp_method,
01835 cur_img->AverageFrameQP, lambda,
01836 mPSNR->value[0], mPSNR->value[1], mPSNR->value[2],
01837 mSSIM->value[0], mSSIM->value[1], mSSIM->value[2],
01838 tmp_time, me_time,
01839 img->fld_flag ? "FLD" : "FRM", intras, direct_mode,
01840 img->num_ref_idx_l0_active, img->num_ref_idx_l1_active,img->rd_pass, img->nal_reference_idc);
01841 }
01842
01843 static void ReportFirstframe(int64 tmp_time, int64 me_time)
01844 {
01845 if (params->Verbose == 1)
01846 {
01847 ReportSimple(img, "IDR", (int) (stats->bit_ctr - stats->bit_ctr_n), &dist->metric[PSNR], (int) tmp_time, (int) me_time);
01848 }
01849 else if (params->Verbose == 2)
01850 {
01851 int lambda = (int) img->lambda_me[I_SLICE][img->AverageFrameQP][0];
01852 if (params->Distortion[SSIM] == 1)
01853 ReportVerboseSSIM(img, "IDR", (int) (stats->bit_ctr - stats->bit_ctr_n), 0, lambda, &dist->metric[PSNR], &dist->metric[SSIM], (int) tmp_time, (int) me_time, 0);
01854 else
01855 ReportVerbose(img, "IDR", (int) (stats->bit_ctr - stats->bit_ctr_n), 0, lambda, &dist->metric[PSNR], (int) tmp_time, (int) me_time, 0);
01856 }
01857 else if (params->Verbose == 3)
01858 {
01859 int lambda = (int) img->lambda_me[I_SLICE][img->AverageFrameQP][0];
01860 if (params->Distortion[SSIM] == 1)
01861 ReportVerboseNVBSSIM(img, "IDR", (int) (stats->bit_ctr - stats->bit_ctr_n) + stats->bit_ctr_parametersets_n, stats->bit_ctr_parametersets_n, 0, lambda, &dist->metric[PSNR], &dist->metric[SSIM], (int) tmp_time, (int) me_time, 0);
01862 else
01863 ReportVerboseNVB(img, "IDR", (int) (stats->bit_ctr - stats->bit_ctr_n) + stats->bit_ctr_parametersets_n, stats->bit_ctr_parametersets_n, 0, lambda, &dist->metric[PSNR], (int) tmp_time, (int) me_time, 0);
01864 }
01865
01866 stats->bit_counter[I_SLICE] = stats->bit_ctr;
01867 stats->bit_ctr = 0;
01868 }
01869
01870 static void ReportIntra(int64 tmp_time, int64 me_time)
01871 {
01872 char pic_type[4];
01873
01874 if ((params->redundant_pic_flag == 0) || !redundant_coding )
01875 {
01876 if (img->currentPicture->idr_flag == TRUE)
01877 strcpy(pic_type,"IDR");
01878 else
01879 strcpy(pic_type," I ");
01880 }
01881 else
01882 strcpy(pic_type,"R");
01883
01884 if (params->Verbose == 1)
01885 {
01886 ReportSimple(img, pic_type, (int) (stats->bit_ctr - stats->bit_ctr_n), &dist->metric[PSNR], (int) tmp_time, (int) me_time);
01887 }
01888 else if (params->Verbose == 2)
01889 {
01890 int lambda = (int) img->lambda_me[I_SLICE][img->AverageFrameQP][0];
01891 if (params->Distortion[SSIM] == 1)
01892 {
01893 ReportVerboseSSIM(img, pic_type, (int) (stats->bit_ctr - stats->bit_ctr_n), 0, lambda, &dist->metric[PSNR], &dist->metric[SSIM], (int) tmp_time, (int) me_time, 0);
01894 }
01895 else
01896 {
01897 ReportVerbose(img, pic_type, (int) (stats->bit_ctr - stats->bit_ctr_n), 0, lambda, &dist->metric[PSNR], (int) tmp_time, (int) me_time, 0);
01898 }
01899 }
01900 else if (params->Verbose == 3)
01901 {
01902 int lambda = (int) img->lambda_me[I_SLICE][img->AverageFrameQP][0];
01903 if (params->Distortion[SSIM] == 1)
01904 {
01905 ReportVerboseNVBSSIM(img, pic_type, (int) (stats->bit_ctr - stats->bit_ctr_n) + stats->bit_ctr_parametersets_n, stats->bit_ctr_parametersets_n, 0, lambda, &dist->metric[PSNR], &dist->metric[SSIM], (int) tmp_time, (int) me_time, 0);
01906 }
01907 else
01908 {
01909 ReportVerboseNVB(img, pic_type, (int) (stats->bit_ctr - stats->bit_ctr_n) + stats->bit_ctr_parametersets_n, stats->bit_ctr_parametersets_n, 0, lambda, &dist->metric[PSNR], (int) tmp_time, (int) me_time, 0);
01910 }
01911 }
01912 }
01913
01914 static void ReportB(int64 tmp_time, int64 me_time)
01915 {
01916 if (params->Verbose == 1)
01917 {
01918 ReportSimple(img, " B ", (int) (stats->bit_ctr - stats->bit_ctr_n), &dist->metric[PSNR], (int) tmp_time, (int) me_time);
01919 }
01920 else if (params->Verbose == 2)
01921 {
01922 int lambda = (int) img->lambda_me[img->nal_reference_idc ? 5 : B_SLICE][img->AverageFrameQP][0];
01923 if (params->Distortion[SSIM] == 1)
01924 ReportVerboseSSIM(img, " B ", (int) (stats->bit_ctr - stats->bit_ctr_n), active_pps->weighted_bipred_idc, lambda, &dist->metric[PSNR], &dist->metric[SSIM], (int) tmp_time, (int) me_time, img->direct_spatial_mv_pred_flag);
01925 else
01926 ReportVerbose(img, " B ", (int) (stats->bit_ctr - stats->bit_ctr_n), active_pps->weighted_bipred_idc, lambda, &dist->metric[PSNR], (int) tmp_time, (int) me_time, img->direct_spatial_mv_pred_flag);
01927 }
01928 else if (params->Verbose == 3)
01929 {
01930 int lambda = (int) img->lambda_me[img->nal_reference_idc ? 5 : B_SLICE][img->AverageFrameQP][0];
01931 if (params->Distortion[SSIM] == 1)
01932 ReportVerboseNVBSSIM(img, " B ", (int) (stats->bit_ctr - stats->bit_ctr_n) + stats->bit_ctr_parametersets_n, stats->bit_ctr_parametersets_n, active_pps->weighted_bipred_idc, lambda, &dist->metric[PSNR], &dist->metric[SSIM], (int) tmp_time, (int) me_time, img->direct_spatial_mv_pred_flag);
01933 else
01934 ReportVerboseNVB(img, " B ", (int) (stats->bit_ctr - stats->bit_ctr_n) + stats->bit_ctr_parametersets_n, stats->bit_ctr_parametersets_n, active_pps->weighted_bipred_idc, lambda, &dist->metric[PSNR], (int) tmp_time, (int) me_time, img->direct_spatial_mv_pred_flag);
01935 }
01936 }
01937
01938 static void ReportP(int64 tmp_time, int64 me_time)
01939 {
01940 char pic_type[4];
01941
01942 if (img->type == SP_SLICE)
01943 strcpy(pic_type,"SP ");
01944 else if ((params->redundant_pic_flag == 0) || !redundant_coding )
01945 strcpy(pic_type," P ");
01946 else
01947 strcpy(pic_type," R ");
01948
01949 if (params->Verbose == 1)
01950 {
01951 ReportSimple(img, pic_type, (int) (stats->bit_ctr - stats->bit_ctr_n), &dist->metric[PSNR], (int) tmp_time, (int) me_time);
01952 }
01953 else if (params->Verbose == 2)
01954 {
01955 int lambda = (int) img->lambda_me[P_SLICE][img->AverageFrameQP][0];
01956 if (params->Distortion[SSIM] == 1)
01957 ReportVerboseSSIM(img, pic_type, (int) (stats->bit_ctr - stats->bit_ctr_n), active_pps->weighted_pred_flag, lambda, &dist->metric[PSNR], &dist->metric[SSIM], (int) tmp_time, (int) me_time, 0);
01958 else
01959 ReportVerbose(img, pic_type, (int) (stats->bit_ctr - stats->bit_ctr_n), active_pps->weighted_pred_flag, lambda, &dist->metric[PSNR], (int) tmp_time, (int) me_time, 0);
01960 }
01961 else if (params->Verbose == 3)
01962 {
01963 int lambda = (int) img->lambda_me[P_SLICE][img->AverageFrameQP][0];
01964 if (params->Distortion[SSIM] == 1)
01965 ReportVerboseNVBSSIM(img, pic_type, (int) (stats->bit_ctr - stats->bit_ctr_n) + stats->bit_ctr_parametersets_n, stats->bit_ctr_parametersets_n, active_pps->weighted_pred_flag, lambda, &dist->metric[PSNR], &dist->metric[SSIM], (int) tmp_time, (int) me_time, 0);
01966 else
01967 ReportVerboseNVB(img, pic_type, (int) (stats->bit_ctr - stats->bit_ctr_n) + stats->bit_ctr_parametersets_n, stats->bit_ctr_parametersets_n, active_pps->weighted_pred_flag, lambda, &dist->metric[PSNR], (int) tmp_time, (int) me_time, 0);
01968 }
01969 }
01970
01971
01972
01973
01974
01975
01976
01977
01978
01979
01980
01981
01982
01983
01984
01985
01986
01987
01988
01989
01990
01991 static void PaddAutoCropBorders (FrameFormat output, int img_size_x, int img_size_y, int img_size_x_cr, int img_size_y_cr, imgpel **pImage[3])
01992 {
01993 int x, y;
01994
01995
01996
01997 if (output.width < img_size_x)
01998 for (y=0; y < output.height; y++)
01999 for (x = output.width; x < img_size_x; x++)
02000 pImage[0] [y][x] = pImage[0][y][x-1];
02001
02002
02003 if (output.height < img_size_y)
02004 for (y = output.height; y<img_size_y; y++)
02005 memcpy(pImage[0][y], pImage[0][y - 1], img_size_x * sizeof(imgpel));
02006
02007
02008 if (img->yuv_format != YUV400)
02009 {
02010 int k;
02011
02012 for (k = 1; k < 3; k++)
02013 {
02014
02015 if (output.width_cr < img_size_x_cr)
02016 for (y=0; y < output.height_cr; y++)
02017 for (x = output.width_cr; x < img_size_x_cr; x++)
02018 pImage [k][y][x] = pImage[k][y][x-1];
02019
02020
02021 if (output.height_cr < img_size_y_cr)
02022 for (y = output.height_cr; y < img_size_y_cr; y++)
02023 memcpy(pImage[k][y], pImage[k][y - 1], img_size_x_cr * sizeof(imgpel));
02024 }
02025 }
02026 }
02027
02028
02029
02030
02031
02032
02033
02034
02035
02036
02037
02038
02039
02040
02041 int CalculateFrameNumber(ImageParameters *p_img)
02042 {
02043 int frm_sign = (p_img->gop_number && (p_img->gop_number <= params->intra_delay)) ? -1 : 1;
02044 int delay = (p_img->gop_number <= params->intra_delay) ? params->intra_delay : 0;
02045
02046 if (p_img->b_frame_to_code)
02047 {
02048 if ((p_img->gop_number && (p_img->gop_number <= params->intra_delay)))
02049 {
02050 if (params->HierarchicalCoding)
02051 p_img->frame_no = p_img->start_tr_gop + (params->intra_delay - p_img->gop_number) * p_img->base_dist
02052 + (int) (p_img->frame_interval * (double) (1 + gop_structure[p_img->b_frame_to_code - 1].display_no));
02053 else
02054 p_img->frame_no = p_img->start_tr_gop + (params->intra_delay - p_img->gop_number) * p_img->base_dist
02055 + (int) (p_img->frame_interval * (double) p_img->b_frame_to_code);
02056 }
02057 else
02058 {
02059 if (params->HierarchicalCoding)
02060 p_img->frame_no = p_img->start_tr_gop + (p_img->gop_number - 1) * p_img->base_dist
02061 + (int) (p_img->frame_interval * (double) (1 + gop_structure[p_img->b_frame_to_code - 1].display_no));
02062 else
02063 p_img->frame_no = p_img->start_tr_gop + (p_img->gop_number - 1) * p_img->base_dist
02064 + (int) (p_img->frame_interval * (double) p_img->b_frame_to_code);
02065 }
02066
02067 }
02068 else
02069 {
02070 if (params->idr_period && params->EnableIDRGOP && p_img->frm_number &&
02071 ((!params->adaptive_idr_period && (p_img->frm_number - p_img->lastIDRnumber ) % params->idr_period == 0)
02072 || (params->adaptive_idr_period == 1 && (p_img->frm_number - imax(p_img->lastIntraNumber, p_img->lastIDRnumber) ) % params->idr_period == 0)) )
02073 {
02074 delay = params->intra_delay;
02075 p_img->rewind_frame += params->NumberBFrames;
02076 p_img->start_frame_no = p_img->frm_number;
02077 p_img->gop_number = (p_img->number - p_img->start_frame_no);
02078 p_img->start_tr_gop = (p_img->frm_number) * p_img->base_dist - p_img->rewind_frame;
02079 }
02080
02081 p_img->frame_no = p_img->start_tr_gop + (frm_sign * p_img->gop_number + delay) * p_img->base_dist;
02082
02083 if (p_img->frame_no > params->last_frame)
02084 p_img->frame_no = imin(p_img->frame_no, (params->no_frames - 1) * (params->frame_skip + 1));
02085
02086 }
02087
02088 return p_img->frame_no;
02089 }
02090
02091
02092
02093
02094
02095
02096
02097 static void put_buffer_frame(ImageParameters *p_img)
02098 {
02099 pCurImg = imgData.frm_data[0];
02100 pImgOrg[0] = imgData.frm_data[0];
02101
02102 if (p_img->yuv_format != YUV400)
02103 {
02104 pImgOrg[1] = imgData.frm_data[1];
02105 pImgOrg[2] = imgData.frm_data[2];
02106 }
02107 }
02108
02109
02110
02111
02112
02113
02114
02115 static void put_buffer_top(ImageParameters *p_img)
02116 {
02117 p_img->fld_type = 0;
02118
02119 pCurImg = imgData.top_data[0];
02120 pImgOrg[0] = imgData.top_data[0];
02121
02122 if (p_img->yuv_format != YUV400)
02123 {
02124 pImgOrg[1] = imgData.top_data[1];
02125 pImgOrg[2] = imgData.top_data[2];
02126 }
02127 }
02128
02129
02130
02131
02132
02133
02134
02135 static void put_buffer_bot(ImageParameters *p_img)
02136 {
02137 p_img->fld_type = 1;
02138
02139 pCurImg = imgData.bot_data[0];
02140 pImgOrg[0] = imgData.bot_data[0];
02141
02142 if (p_img->yuv_format != YUV400)
02143 {
02144 pImgOrg[1] = imgData.bot_data[1];
02145 pImgOrg[2] = imgData.bot_data[2];
02146 }
02147 }
02148
02149
02150
02151
02152
02153
02154
02155
02156
02157 static void rdPictureCoding(void)
02158 {
02159 int second_qp = img->qp, rd_qp = img->qp;
02160 int previntras = intras;
02161 int prevtype = img->type;
02162 int skip_encode = 0;
02163 pic_parameter_set_rbsp_t *sec_pps;
02164 int tmpFrameQP = img->SumFrameQP;
02165 float rateRatio = 1.0F;
02166
02167 if ( params->RCEnable )
02168 rc_save_state();
02169 if ( params->WPMCPrecision )
02170 pWPX->curr_wp_rd_pass = pWPX->wp_rd_passes + 1;
02171
02172 if (img->type!=I_SLICE && params->GenerateMultiplePPS)
02173 {
02174 if (img->type==P_SLICE)
02175 {
02176 if ((params->RDPSliceWeightOnly != 2) && (TestWPPSlice(img, params, 0) == 1))
02177 {
02178 active_pps = PicParSet[1];
02179 if ( params->WPMCPrecision )
02180 pWPX->curr_wp_rd_pass->algorithm = WP_REGULAR;
02181 }
02182 else if ( params->WPMCPrecision )
02183 active_pps = PicParSet[1];
02184 else
02185 {
02186 skip_encode = params->RDPSliceWeightOnly;
02187 active_pps = PicParSet[0];
02188 if (!img->AdaptiveRounding)
02189 {
02190 img->qp-=1;
02191 if ( params->RCEnable )
02192 rateRatio = 1.15F;
02193 }
02194 }
02195 }
02196 else
02197 {
02198 active_pps = PicParSet[2];
02199 if ( params->WPMCPrecision )
02200 pWPX->curr_wp_rd_pass->algorithm = WP_REGULAR;
02201 }
02202 }
02203 else
02204 {
02205 if (!img->AdaptiveRounding)
02206 {
02207 img->qp-=1;
02208 if ( params->RCEnable )
02209 rateRatio = 1.15F;
02210 }
02211 }
02212
02213 sec_pps = active_pps;
02214 second_qp = img->qp;
02215
02216 img->write_macroblock = FALSE;
02217
02218 if (skip_encode)
02219 {
02220 img->rd_pass = 0;
02221 enc_frame_picture[1] = NULL;
02222 }
02223 else
02224 {
02225 if(params->RCEnable)
02226 rc_init_frame_rdpic( rateRatio );
02227
02228 img->qp = iClip3( img->RCMinQP, img->RCMaxQP, img->qp );
02229 frame_picture (frame_pic[1], &imgData, 1);
02230 img->rd_pass=picture_coding_decision(frame_pic[0], frame_pic[1], rd_qp);
02231 }
02232
02233
02234 if (img->rd_pass==0)
02235 {
02236 enc_picture=enc_frame_picture[0];
02237 if (img->type!=I_SLICE && params->GenerateMultiplePPS)
02238 {
02239 img->qp = rd_qp;
02240 active_pps = PicParSet[0];
02241 }
02242 else
02243 {
02244 img->qp = rd_qp;
02245 }
02246 intras = previntras;
02247 p_frame_pic = frame_pic[0];
02248 }
02249 else
02250 {
02251 previntras = intras;
02252 p_frame_pic = frame_pic[1];
02253 tmpFrameQP = img->SumFrameQP;
02254
02255 if(params->RCEnable)
02256 rc_save_state();
02257 }
02258
02259
02260 if ( params->RCEnable )
02261 rateRatio = 1.0F;
02262
02263 if ( params->WPMCPrecision )
02264 pWPX->curr_wp_rd_pass = pWPX->wp_rd_passes + 2;
02265
02266 if (img->type != I_SLICE )
02267 {
02268 skip_encode = 0;
02269 img->qp = rd_qp;
02270
02271 if (img->type == P_SLICE && (intras * 100 )/img->FrameSizeInMbs >=75)
02272 {
02273 set_slice_type(img, I_SLICE );
02274 active_pps = PicParSet[0];
02275 }
02276 else if (img->type==P_SLICE)
02277 {
02278 if (params->GenerateMultiplePPS)
02279 {
02280 if ((params->RDPSliceWeightOnly != 2) && (TestWPPSlice(img, params, 1) == 1))
02281 {
02282 active_pps = PicParSet[1];
02283 if ( params->WPMCPrecision )
02284 pWPX->curr_wp_rd_pass->algorithm = WP_REGULAR;
02285 }
02286 else if ( params->WPMCPrecision == 2 )
02287 active_pps = PicParSet[1];
02288 else if (params->RDPSliceBTest && active_sps->profile_idc != 66)
02289 {
02290 set_slice_type(img, B_SLICE );
02291 active_pps = PicParSet[0];
02292 }
02293 else
02294 {
02295 skip_encode = params->RDPSliceWeightOnly;
02296 active_pps = PicParSet[0];
02297 if (!img->AdaptiveRounding)
02298 {
02299 img->qp+=1;
02300 if ( params->RCEnable )
02301 rateRatio = 0.85F;
02302 }
02303 }
02304 }
02305 }
02306 else
02307 {
02308 if (params->GenerateMultiplePPS && (params->RDBSliceWeightOnly != 2) && TestWPBSlice(img, params, 0) == 1)
02309 {
02310 active_pps = PicParSet[1];
02311 if ( params->WPMCPrecision )
02312 pWPX->curr_wp_rd_pass->algorithm = WP_REGULAR;
02313 }
02314 else if ( params->WPMCPrecision == 2 && (params->WPMCPrecBSlice == 2 || (params->WPMCPrecBSlice == 1 && img->nal_reference_idc) ) )
02315 active_pps = PicParSet[1];
02316 else
02317 {
02318 skip_encode = (params->RDBSliceWeightOnly == 1);
02319 img->qp = rd_qp + (img->nal_reference_idc ? - 1 : 1);
02320 if ( params->RCEnable )
02321 rateRatio = img->nal_reference_idc ? 1.15F : 0.85F;
02322 if ( params->WPMCPrecision )
02323 pWPX->curr_wp_rd_pass->algorithm = WP_REGULAR;
02324 }
02325 }
02326 }
02327 else
02328 {
02329 active_pps = PicParSet[0];
02330 if (!img->AdaptiveRounding)
02331 img->qp = (rd_qp + 1);
02332 }
02333
02334 img->write_macroblock = FALSE;
02335
02336 if (skip_encode)
02337 {
02338 enc_frame_picture[2] = NULL;
02339 img->qp = rd_qp;
02340 }
02341 else
02342 {
02343 if(params->RCEnable)
02344 rc_init_frame_rdpic( rateRatio );
02345
02346 img->qp = iClip3( img->RCMinQP, img->RCMaxQP, img->qp );
02347 frame_picture (frame_pic[2], &imgData, 2);
02348
02349 if (img->rd_pass==0)
02350 img->rd_pass = 2 * picture_coding_decision(frame_pic[0], frame_pic[2], rd_qp);
02351 else
02352 img->rd_pass += picture_coding_decision(frame_pic[1], frame_pic[2], rd_qp);
02353
02354 if ( params->RCEnable && img->rd_pass == 2 )
02355 rc_save_state();
02356
02357 if ( img->rd_pass == 2 )
02358 tmpFrameQP = img->SumFrameQP;
02359 }
02360
02361
02362 if (img->rd_pass==0)
02363 {
02364 enc_picture = enc_frame_picture[0];
02365 set_slice_type( img, prevtype );
02366 active_pps = PicParSet[0];
02367 img->qp = rd_qp;
02368 intras = previntras;
02369 }
02370 else if (img->rd_pass==1)
02371 {
02372 enc_picture = enc_frame_picture[1];
02373 set_slice_type( img, prevtype );
02374 active_pps = sec_pps;
02375 img->qp = second_qp;
02376 intras = previntras;
02377 }
02378 if ( params->RCEnable )
02379 rc_restore_state();
02380 img->SumFrameQP = tmpFrameQP;
02381 }
02382
02383
02384
02385
02386
02387
02388
02389 void output_SP_coefficients()
02390 {
02391 int i,k;
02392 FILE *SP_coeff_file;
02393 int ret;
02394 if(number_sp2_frames==0)
02395 {
02396 if ((SP_coeff_file = fopen(params->sp_output_filename,"wb")) == NULL)
02397 {
02398 printf ("Fatal: cannot open SP output file '%s', exit (-1)\n", params->sp_output_filename);
02399 exit (-1);
02400 }
02401 number_sp2_frames++;
02402 }
02403 else
02404 {
02405 if ((SP_coeff_file = fopen(params->sp_output_filename,"ab")) == NULL)
02406 {
02407 printf ("Fatal: cannot open SP output file '%s', exit (-1)\n", params->sp_output_filename);
02408 exit (-1);
02409 }
02410 }
02411
02412 for(i=0;i<img->height;i++)
02413 {
02414 ret = fwrite(lrec[i],sizeof(int),img->width,SP_coeff_file);
02415 if (ret != img->width)
02416 {
02417 error ("cannot write to SP output file", -1);
02418 }
02419 }
02420
02421 for(k=0;k<2;k++)
02422 {
02423 for(i=0;i<img->height_cr;i++)
02424 {
02425 ret = fwrite(lrec_uv[k][i],sizeof(int),img->width_cr,SP_coeff_file);
02426 if (ret != img->width_cr)
02427 {
02428 error ("cannot write to SP output file", -1);
02429 }
02430 }
02431 }
02432 fclose(SP_coeff_file);
02433 }
02434
02435
02436
02437
02438
02439
02440
02441 void read_SP_coefficients()
02442 {
02443 int i,k;
02444 FILE *SP_coeff_file;
02445
02446 if ( (params->qp2start > 0) && ( ( (img->frame_no ) % (2*params->qp2start) ) >=params->qp2start ))
02447 {
02448 if ((SP_coeff_file = fopen(params->sp2_input_filename1,"rb")) == NULL)
02449 {
02450 printf ("Fatal: cannot open SP input file '%s', exit (-1)\n", params->sp2_input_filename2);
02451 exit (-1);
02452 }
02453 }
02454 else
02455 {
02456 if ((SP_coeff_file = fopen(params->sp2_input_filename2,"rb")) == NULL)
02457 {
02458 printf ("Fatal: cannot open SP input file '%s', exit (-1)\n", params->sp2_input_filename1);
02459 exit (-1);
02460 }
02461 }
02462
02463 if (0 != fseek (SP_coeff_file, img->size * 3/2*number_sp2_frames*sizeof(int), SEEK_SET))
02464 {
02465 printf ("Fatal: cannot seek in SP input file, exit (-1)\n");
02466 exit (-1);
02467 }
02468 number_sp2_frames++;
02469
02470 for(i=0;i<img->height;i++)
02471 {
02472 if(img->width!=(int)fread(lrec[i],sizeof(int),img->width,SP_coeff_file))
02473 {
02474 printf ("Fatal: cannot read in SP input file, exit (-1)\n");
02475 exit (-1);
02476 }
02477 }
02478
02479 for(k=0;k<2;k++)
02480 {
02481 for(i=0;i<img->height_cr;i++)
02482 {
02483 if(img->width_cr!=(int)fread(lrec_uv[k][i],sizeof(int),img->width_cr,SP_coeff_file))
02484 {
02485 printf ("Fatal: cannot read in SP input file, exit (-1)\n");
02486 exit (-1);
02487 }
02488 }
02489 }
02490 fclose(SP_coeff_file);
02491 }
02492
02493
02494
02495
02496
02497
02498
02499
02500 void select_plane(ColorPlane color_plane)
02501 {
02502 pCurImg = pImgOrg[color_plane];
02503 enc_picture->p_curr_img = enc_picture->p_img[color_plane];
02504 enc_picture->p_curr_img_sub = enc_picture->p_img_sub[color_plane];
02505 img->max_imgpel_value = img->max_imgpel_value_comp[color_plane];
02506 img->dc_pred_value = img->dc_pred_value_comp[color_plane];
02507 img->curr_res = img->mb_rres [color_plane];
02508 img->curr_prd = img->mb_pred [color_plane];
02509 }
02510
02511
02512
02513
02514
02515
02516
02517 static int is_gop_first_unit( void )
02518 {
02519 return ( get_idr_flag() || ( img->type == I_SLICE && params->EnableOpenGOP ) );
02520 }
02521
02522
02523
02524
02525
02526
02527
02528 void write_non_vcl_nalu( void )
02529 {
02530
02531 if (params->ResendSPS == 3 && is_gop_first_unit() && img->number)
02532 {
02533 stats->bit_slice = rewrite_paramsets();
02534 }
02535
02536 if (params->ResendSPS == 2 && get_idr_flag() && img->number)
02537 {
02538 stats->bit_slice = rewrite_paramsets();
02539 }
02540 if (params->ResendSPS == 1 && img->type == I_SLICE && img->frm_number != 0)
02541 {
02542 stats->bit_slice = rewrite_paramsets();
02543 }
02544
02545 if ( params->ResendPPS && img->frm_number != 0
02546 && (img->type != I_SLICE || params->ResendSPS != 1)
02547 && (!(params->ResendSPS == 2 && get_idr_flag()))
02548 && (!(params->ResendPPS == 3 && is_gop_first_unit())) )
02549 {
02550
02551 if ( params->SendAUD )
02552 {
02553 stats->bit_ctr_parametersets_n = Write_AUD_NALU();
02554 stats->bit_ctr_parametersets_n += write_PPS(0, 0);
02555 }
02556 else
02557 {
02558 stats->bit_ctr_parametersets_n = write_PPS(0, 0);
02559 }
02560 }
02561
02562 if ( params->SendAUD
02563 && (img->type != I_SLICE || params->ResendSPS != 1)
02564 && (!(params->ResendSPS == 2 && get_idr_flag()))
02565 && (!(params->ResendPPS == 3 && is_gop_first_unit())) )
02566 {
02567 stats->bit_ctr_parametersets_n += Write_AUD_NALU();
02568 }
02569
02570 UpdateSubseqInfo (img->layer);
02571 UpdateSceneInformation (FALSE, 0, 0, -1);
02572
02573
02574
02575
02576
02577 if (params->ToneMappingSEIPresentFlag)
02578 {
02579 UpdateToneMapping();
02580 }
02581
02582 PrepareAggregationSEIMessage();
02583 stats->bit_ctr_parametersets_n += Write_SEI_NALU(0);
02584
02585 stats->bit_ctr_parametersets += stats->bit_ctr_parametersets_n;
02586 }
02587