00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "contributors.h"
00022
00023 #include <limits.h>
00024 #include <math.h>
00025
00026 #include "global.h"
00027 #include "enc_statistics.h"
00028
00029 #include "block.h"
00030 #include "elements.h"
00031 #include "macroblock.h"
00032 #include "memalloc.h"
00033 #include "mc_prediction.h"
00034 #include "fmo.h"
00035 #include "vlc.h"
00036 #include "image.h"
00037 #include "mb_access.h"
00038 #include "ratectl.h"
00039 #include "cabac.h"
00040 #include "biariencode.h"
00041 #include "me_fullsearch.h"
00042 #include "me_fullfast.h"
00043 #include "symbol.h"
00044 #include "conformance.h"
00045 #include "md_common.h"
00046 #include "mv_prediction.h"
00047 #include "rdopt.h"
00048 #include "transform.h"
00049
00050
00051 #if TRACE
00052 #define TRACE_SE(trace,str) snprintf(trace,TRACESTRING_SIZE,str)
00053 #else
00054 #define TRACE_SE(trace,str)
00055 #endif
00056
00057 static const byte QP_SCALE_CR[52]=
00058 {
00059 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,
00060 28,29,29,30,31,32,32,33,34,34,35,35,36,36,37,37, 37,38,38,38,39,39,39,39
00061 };
00062
00063 static const int block8x8_idx[3][4][4] =
00064 {
00065 {
00066 {0, 1, 0, 0},
00067 {2, 3, 0, 0},
00068 {0, 0, 0, 0},
00069 {0, 0, 0, 0}
00070 },
00071 {
00072 {0, 1, 0, 0},
00073 {0, 1, 0, 0},
00074 {2, 3, 0, 0},
00075 {2, 3, 0, 0}
00076 },
00077 {
00078 {0, 0, 1, 1},
00079 {0, 0, 1, 1},
00080 {2, 2, 3, 3},
00081 {2, 2, 3, 3}
00082 }
00083 };
00084
00085
00086 static int slice_too_big (Slice *currSlice, int rlc_bits);
00087 static int writeChromaIntraPredMode (Macroblock* currMB);
00088 static int writeChromaCoeff (Macroblock* currMB);
00089 static int writeCBPandDquant (Macroblock* currMB);
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 void set_MB_parameters (Slice *currSlice, Macroblock *currMB)
00103 {
00104 ImageParameters *p_Img = currSlice->p_Img;
00105 InputParameters *p_Inp = currSlice->p_Inp;
00106
00107 int mb_addr = currMB->mbAddrX;
00108 p_Img->current_mb_nr = mb_addr;
00109
00110 p_Img->get_mb_block_pos(currMB->mbAddrX, &currMB->mb_x, &currMB->mb_y);
00111
00112 currMB->block_x = currMB->mb_x << 2;
00113 currMB->block_y = currMB->mb_y << 2;
00114
00115 currMB->pix_x = currMB->block_x << 2;
00116 currMB->pix_y = currMB->block_y << 2;
00117
00118 if (currSlice->MbaffFrameFlag)
00119 {
00120 if (currMB->mb_field)
00121 {
00122 p_Img->pCurImg = (mb_addr & 0x01) ? p_Img->imgData.bot_data[0] : p_Img->imgData.top_data[0];
00123 p_Img->pImgOrg[0] = p_Img->pCurImg;
00124
00125 if ((p_Img->yuv_format != YUV400) && !IS_INDEPENDENT(p_Inp))
00126 {
00127 if (mb_addr & 0x01)
00128 {
00129 p_Img->pImgOrg[1] = p_Img->imgData.bot_data[1];
00130 p_Img->pImgOrg[2] = p_Img->imgData.bot_data[2];
00131 }
00132 else
00133 {
00134 p_Img->pImgOrg[1] = p_Img->imgData.top_data[1];
00135 p_Img->pImgOrg[2] = p_Img->imgData.top_data[2];
00136 }
00137 }
00138
00139 currMB->opix_y = (currMB->mb_y >> 1 ) << 4;
00140 currMB->list_offset = (mb_addr & 0x01) ? 4 : 2;
00141 }
00142 else
00143 {
00144 p_Img->pCurImg = p_Img->imgData.frm_data[0];
00145 p_Img->pImgOrg[0] = p_Img->imgData.frm_data[0];
00146
00147 if ((p_Img->yuv_format != YUV400) && !IS_INDEPENDENT(p_Inp))
00148 {
00149 p_Img->pImgOrg[1] = p_Img->imgData.frm_data[1];
00150 p_Img->pImgOrg[2] = p_Img->imgData.frm_data[2];
00151 }
00152
00153 currMB->opix_y = currMB->block_y << 2;
00154 currMB->list_offset = 0;
00155 }
00156 }
00157 else
00158 {
00159 currMB->opix_y = currMB->block_y << 2;
00160 currMB->list_offset = 0;
00161 }
00162
00163 if (p_Img->yuv_format != YUV400)
00164 {
00165 currMB->pix_c_x = (p_Img->mb_cr_size_x * currMB->pix_x) >> 4;
00166 currMB->pix_c_y = (p_Img->mb_cr_size_y * currMB->pix_y) >> 4;
00167
00168 currMB->opix_c_y = (p_Img->mb_cr_size_y * currMB->opix_y) >> 4;
00169 }
00170
00171
00172 }
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182 void next_macroblock(Macroblock *currMB)
00183 {
00184 ImageParameters *p_Img = currMB->p_Img;
00185 InputParameters *p_Inp = currMB->p_Inp;
00186 Slice *currSlice = currMB->p_slice;
00187 int slice_type = currSlice->slice_type;
00188 #if TRACE
00189 int use_bitstream_backing = (p_Inp->slice_mode == FIXED_RATE || p_Inp->slice_mode == CALL_BACK);
00190 #endif
00191 BitCounter *mbBits = &currMB->bits;
00192 int i;
00193 StatParameters *cur_stats = &p_Img->enc_picture->stats;
00194
00195 if (mbBits->mb_total > p_Img->max_bitCount)
00196 printf("Warning!!! Number of bits (%d) of macroblock_layer() data seems to exceed defined limit (%d).\n", mbBits->mb_total,p_Img->max_bitCount);
00197
00198
00199 cur_stats->bit_use_mb_type[slice_type] += mbBits->mb_mode;
00200 cur_stats->tmp_bit_use_cbp[slice_type] += mbBits->mb_cbp;
00201 cur_stats->bit_use_coeffC[slice_type] += mbBits->mb_uv_coeff;
00202 cur_stats->bit_use_coeff[0][slice_type] += mbBits->mb_y_coeff;
00203 cur_stats->bit_use_coeff[1][slice_type] += mbBits->mb_cb_coeff;
00204 cur_stats->bit_use_coeff[2][slice_type] += mbBits->mb_cr_coeff;
00205 cur_stats->bit_use_delta_quant[slice_type] += mbBits->mb_delta_quant;
00206 cur_stats->bit_use_stuffingBits[slice_type] += mbBits->mb_stuffing;
00207
00208 if (IS_INTRA(currMB))
00209 {
00210 ++cur_stats->intra_chroma_mode[ (short) currMB->c_ipred_mode];
00211
00212
00213 if ((currMB->cbp&15) != 0)
00214 {
00215 ++cur_stats->mode_use_transform[slice_type][currMB->mb_type][currMB->luma_transform_size_8x8_flag];
00216 }
00217 }
00218
00219 ++cur_stats->mode_use[slice_type][currMB->mb_type];
00220 cur_stats->bit_use_mode[slice_type][currMB->mb_type] += mbBits->mb_inter;
00221
00222 if (slice_type != I_SLICE)
00223 {
00224 if (currMB->mb_type == P8x8)
00225 {
00226 for(i=0;i<4;++i)
00227 {
00228 if (currMB->b8x8[i].mode > 0)
00229 ++cur_stats->mode_use[slice_type][(short) currMB->b8x8[i].mode];
00230 else
00231 ++cur_stats->b8_mode_0_use[slice_type][currMB->luma_transform_size_8x8_flag];
00232
00233 if (currMB->b8x8[i].mode ==4)
00234 {
00235 if ((currMB->luma_transform_size_8x8_flag && (currMB->cbp&15) != 0) || p_Inp->Transform8x8Mode == 2)
00236 ++cur_stats->mode_use_transform[slice_type][4][1];
00237 else
00238 ++cur_stats->mode_use_transform[slice_type][4][0];
00239 }
00240 }
00241 }
00242 else if (currMB->mb_type >= 0 && currMB->mb_type <=3 && ((currMB->cbp&15) != 0))
00243 {
00244 ++cur_stats->mode_use_transform[slice_type][currMB->mb_type][currMB->luma_transform_size_8x8_flag];
00245 }
00246 }
00247
00248
00249 cur_stats->quant[slice_type] += currMB->qp;
00250 ++cur_stats->num_macroblocks[slice_type];
00251 }
00252
00253 static void set_chroma_qp(Macroblock* currMB)
00254 {
00255 ImageParameters *p_Img = currMB->p_Img;
00256 int i;
00257 for (i=0; i<2; ++i)
00258 {
00259 currMB->qpc[i] = (short) iClip3 ( -p_Img->bitdepth_chroma_qp_scale, 51, currMB->qp + p_Img->chroma_qp_offset[i] );
00260 currMB->qpc[i] = (short) (currMB->qpc[i] < 0 ? currMB->qpc[i] : QP_SCALE_CR[currMB->qpc[i]]);
00261 currMB->qp_scaled[i + 1] = (short) (currMB->qpc[i] + p_Img->bitdepth_chroma_qp_scale);
00262 }
00263 }
00264
00265
00266
00267
00268
00269
00270
00271 void update_qp(Macroblock *currMB)
00272 {
00273 currMB->qp_scaled[0] = (short) (currMB->qp + currMB->p_Img->bitdepth_luma_qp_scale);
00274 set_chroma_qp(currMB);
00275
00276 select_dct(currMB);
00277 }
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288 void reset_macroblock(Macroblock *currMB)
00289 {
00290 ImageParameters *p_Img = currMB->p_Img;
00291 InputParameters *p_Inp = currMB->p_Inp;
00292 Slice *currSlice = currMB->p_slice;
00293 PicMotionParams *motion = &p_Img->enc_picture->motion;
00294 int i,j,l;
00295
00296
00297 for (l=0; l<2; ++l)
00298 {
00299 for (j=currMB->block_y; j < currMB->block_y + BLOCK_MULTIPLE; ++j)
00300 {
00301 memset(&motion->ref_idx[l][j][currMB->block_x], -1, BLOCK_MULTIPLE * sizeof(char));
00302 memset(motion->mv [l][j][currMB->block_x], 0, 2 * BLOCK_MULTIPLE * sizeof(short));
00303 for (i=currMB->block_x; i < currMB->block_x + BLOCK_MULTIPLE; ++i)
00304 motion->ref_pic_id[l][j][i]= -1;
00305 }
00306 }
00307
00308
00309 currMB->mb_type = 0;
00310 currMB->cbp_blk = 0;
00311 currMB->cbp = 0;
00312 currMB->c_ipred_mode = DC_PRED_8;
00313
00314 currSlice->cmp_cbp[1] = currSlice->cmp_cbp[2] = currSlice->curr_cbp[0] = currSlice->curr_cbp[1] = 0;
00315
00316 memset(currMB->cbp_bits , 0, 3 * sizeof(int64));
00317 memset(currMB->cbp_bits_8x8, 0, 3 * sizeof(int64));
00318
00319 memset (currMB->mvd, 0, BLOCK_CONTEXT * sizeof(short));
00320 memset (currMB->intra_pred_modes, DC_PRED, MB_BLOCK_PARTITIONS * sizeof(char));
00321 memset (currMB->intra_pred_modes8x8, DC_PRED, MB_BLOCK_PARTITIONS * sizeof(char));
00322
00323 currMB->b8x8[0].bipred = 0;
00324 currMB->b8x8[1].bipred = 0;
00325 currMB->b8x8[2].bipred = 0;
00326 currMB->b8x8[3].bipred = 0;
00327
00328 currMB->write_mb = FALSE;
00329
00330
00331
00332 if (p_Inp->UseConstrainedIntraPred)
00333 {
00334 p_Img->intra_block[currMB->mbAddrX] = 1;
00335 }
00336
00337
00338 currMB->bits.mb_mode = 0;
00339 currMB->bits.mb_inter = 0;
00340 currMB->bits.mb_cbp = 0;
00341 currMB->bits.mb_y_coeff = 0;
00342 currMB->bits.mb_uv_coeff = 0;
00343 currMB->bits.mb_cb_coeff = 0;
00344 currMB->bits.mb_cr_coeff = 0;
00345 currMB->bits.mb_delta_quant = 0;
00346 currMB->bits.mb_stuffing = 0;
00347
00348 currMB->min_rdcost = 1e30;
00349 currMB->min_dcost = 1e30;
00350 currMB->min_rate = 1e30;
00351
00352 currMB->best_mode = 0;
00353 currMB->best_c_imode = 0;
00354 currMB->best_i16offset = 0;
00355 currMB->best_cbp = 0;
00356 }
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374 void start_macroblock(Slice *currSlice, Macroblock **currMB, int mb_addr, Boolean mb_field)
00375 {
00376 ImageParameters *p_Img = currSlice->p_Img;
00377 InputParameters *p_Inp = currSlice->p_Inp;
00378 PicMotionParams *motion = &p_Img->enc_picture->motion;
00379 int i, mb_qp;
00380 int use_bitstream_backing = (p_Inp->slice_mode == FIXED_RATE || p_Inp->slice_mode == CALL_BACK);
00381
00382 static Macroblock *last_coded_mb = NULL;
00383
00384 DataPartition *dataPart;
00385 Bitstream *currStream;
00386 int prev_mb;
00387
00388 *currMB = &p_Img->mb_data[mb_addr];
00389 (*currMB)->p_slice = currSlice;
00390 (*currMB)->p_Img = p_Img;
00391 (*currMB)->p_Inp = p_Inp;
00392
00393 (*currMB)->mbAddrX = mb_addr;
00394
00395 (*currMB)->is_intra_block = currSlice->slice_type == I_SLICE ? TRUE : FALSE;
00396
00397
00398 (*currMB)->mb_field = (byte) mb_field;
00399 motion->mb_field[mb_addr] = (byte) mb_field;
00400
00401 (*currMB)->is_field_mode = (byte) (p_Img->field_picture || ( currSlice->MbaffFrameFlag && (*currMB)->mb_field));
00402
00403 set_MB_parameters (currSlice, *currMB);
00404
00405 prev_mb = FmoGetPreviousMBNr(p_Img, mb_addr);
00406
00407 if(use_bitstream_backing)
00408 {
00409 if ((!p_Inp->MbInterlace)||((mb_addr & 0x01)==0))
00410 {
00411
00412 if(!p_Img->cod_counter)
00413 {
00414 for (i=0; i<currSlice->max_part_nr; ++i)
00415 {
00416 dataPart = &(currSlice->partArr[i]);
00417 currStream = dataPart->bitstream;
00418 currStream->stored_bits_to_go = currStream->bits_to_go;
00419 currStream->stored_byte_pos = currStream->byte_pos;
00420 currStream->stored_byte_buf = currStream->byte_buf;
00421 p_Img->p_Stats->stored_bit_slice = p_Img->p_Stats->bit_slice;
00422
00423 if (currSlice->symbol_mode ==CABAC)
00424 {
00425 dataPart->ee_recode = dataPart->ee_cabac;
00426 }
00427 }
00428 }
00429 }
00430 }
00431
00432
00433
00434 (*currMB)->slice_nr = currSlice->slice_nr;
00435
00436
00437
00438 (*currMB)->qpsp = (short) p_Img->qpsp;
00439
00440 if (prev_mb > -1 && (p_Img->mb_data[prev_mb].slice_nr == currSlice->slice_nr))
00441 {
00442 (*currMB)->PrevMB = &p_Img->mb_data[prev_mb];
00443 (*currMB)->prev_qp = (*currMB)->PrevMB->qp;
00444 (*currMB)->prev_dqp = (*currMB)->prev_qp - (*currMB)->PrevMB->prev_qp;
00445 }
00446 else
00447 {
00448 (*currMB)->PrevMB = NULL;
00449 (*currMB)->prev_qp = (short) currSlice->qp;
00450 (*currMB)->prev_dqp = 0;
00451 }
00452
00453 if(p_Inp->RCEnable && (last_coded_mb != *currMB))
00454 {
00455 mb_qp = rc_handle_mb( *currMB, prev_mb);
00456 }
00457 else
00458 {
00459 mb_qp = p_Img->qp;
00460
00461 }
00462 last_coded_mb = *currMB;
00463
00464 mb_qp = iClip3(-p_Img->bitdepth_luma_qp_scale, 51, mb_qp);
00465 (*currMB)->qp = (short) mb_qp;
00466 p_Img->qp = mb_qp;
00467
00468 update_qp (*currMB);
00469
00470
00471 if (p_Img->active_pps->deblocking_filter_control_present_flag)
00472 {
00473 (*currMB)->DFDisableIdc = currSlice->DFDisableIdc;
00474 (*currMB)->DFAlphaC0Offset = currSlice->DFAlphaC0Offset;
00475 (*currMB)->DFBetaOffset = currSlice->DFBetaOffset;
00476 }
00477 else
00478 {
00479 (*currMB)->DFDisableIdc = 0;
00480 (*currMB)->DFAlphaC0Offset = 0;
00481 (*currMB)->DFBetaOffset = 0;
00482 }
00483
00484 (*currMB)->min_rdcost = 1e30;
00485 (*currMB)->min_dcost = 1e30;
00486 (*currMB)->min_rate = 1<<20;
00487
00488 (*currMB)->i16mode = 0;
00489 (*currMB)->best_mode = 10;
00490
00491 if ((p_Img->yuv_format!=0) && (p_Img->yuv_format!=3))
00492 {
00493 (*currMB)->cbp_linfo_intra = cbp_linfo_intra_normal;
00494 (*currMB)->cbp_linfo_inter = cbp_linfo_inter_normal;
00495 }
00496 else
00497 {
00498 (*currMB)->cbp_linfo_intra = cbp_linfo_intra_other;
00499 (*currMB)->cbp_linfo_inter = cbp_linfo_inter_other;
00500 }
00501
00502
00503 if (currSlice->slice_type != I_SLICE && currSlice->slice_type != SI_SLICE)
00504 {
00505 InitMotionVectorPrediction(*currMB, currSlice->MbaffFrameFlag);
00506 init_ME_engine(*currMB);
00507 }
00508
00509
00510 CheckAvailabilityOfNeighbors(*currMB);
00511
00512 if (currSlice->symbol_mode == CABAC)
00513 CheckAvailabilityOfNeighborsCABAC(*currMB);
00514
00515 reset_macroblock(*currMB);
00516
00517 if ((p_Inp->SearchMode == FAST_FULL_SEARCH) && (!p_Inp->IntraProfile))
00518 ResetFastFullIntegerSearch (p_Img);
00519
00520
00521 #if TRACE
00522 for (i=0; i<currSlice->max_part_nr; ++i )
00523 {
00524 currSlice->partArr[i].bitstream->trace_enabled = FALSE;
00525 }
00526 #endif
00527 }
00528
00529
00530
00531
00532
00533
00534
00535
00536 void end_macroblock(Macroblock *currMB,
00537 Boolean *end_of_slice,
00538 Boolean *recode_macroblock
00539 )
00540 {
00541 Slice *currSlice = currMB->p_slice;
00542 ImageParameters *p_Img = currMB->p_Img;
00543 InputParameters *p_Inp = currMB->p_Inp;
00544 int i;
00545 SyntaxElement se;
00546 const int *partMap = assignSE2partition[currSlice->partition_mode];
00547 DataPartition *dataPart;
00548 Bitstream *currStream;
00549 int rlc_bits=0;
00550 int use_bitstream_backing = (p_Inp->slice_mode == FIXED_RATE || p_Inp->slice_mode == CALL_BACK);
00551 int skip = FALSE;
00552 int new_slice = 0;
00553
00554
00555
00556 if ((currMB->mbAddrX == 0) || currMB->PrevMB == NULL || ( currMB->PrevMB->slice_nr != currSlice->slice_nr ))
00557 new_slice=1;
00558
00559 *recode_macroblock=FALSE;
00560
00561 switch(p_Inp->slice_mode)
00562 {
00563 case NO_SLICES:
00564 ++currSlice->num_mb;
00565 *recode_macroblock = FALSE;
00566 if ((currSlice->num_mb) == (int)p_Img->PicSizeInMbs)
00567 *end_of_slice = TRUE;
00568
00569
00570 *end_of_slice = (Boolean) (*end_of_slice | (currMB->mbAddrX == FmoGetLastCodedMBOfSliceGroup (p_Img, FmoMB2SliceGroup (p_Img, currMB->mbAddrX))));
00571
00572 break;
00573 case FIXED_MB:
00574
00575 ++currSlice->num_mb;
00576 *recode_macroblock = FALSE;
00577
00578 *end_of_slice = (Boolean) (currMB->mbAddrX == FmoGetLastCodedMBOfSliceGroup (p_Img, FmoMB2SliceGroup (p_Img, currMB->mbAddrX)));
00579
00580 *end_of_slice = (Boolean) (*end_of_slice | (currSlice->num_mb >= p_Inp->slice_argument));
00581
00582 break;
00583
00584
00585
00586
00587 case FIXED_RATE:
00588
00589
00590 if(p_Img->cod_counter)
00591 {
00592
00593 se.value1 = p_Img->cod_counter;
00594 se.value2 = 0;
00595 se.type = SE_MBTYPE;
00596 dataPart = &(currSlice->partArr[partMap[se.type]]);
00597
00598 TRACE_SE (se.tracestring, "mb_skip_run");
00599 writeSE_UVLC(&se, dataPart);
00600 rlc_bits=se.len;
00601
00602 currStream = dataPart->bitstream;
00603
00604 currStream->bits_to_go_skip = currStream->bits_to_go;
00605 currStream->byte_pos_skip = currStream->byte_pos;
00606 currStream->byte_buf_skip = currStream->byte_buf;
00607
00608 currStream->bits_to_go = currStream->stored_bits_to_go;
00609 currStream->byte_pos = currStream->stored_byte_pos;
00610 currStream->byte_buf = currStream->stored_byte_buf;
00611 skip = TRUE;
00612 }
00613
00614
00615 if (!new_slice)
00616 {
00617 if(slice_too_big(currSlice, rlc_bits))
00618 {
00619 *recode_macroblock = TRUE;
00620 *end_of_slice = TRUE;
00621 }
00622 else if(!p_Img->cod_counter)
00623 skip = FALSE;
00624 }
00625
00626
00627
00628 if ((*recode_macroblock == FALSE) && (currMB->mbAddrX == FmoGetLastCodedMBOfSliceGroup (p_Img, FmoMB2SliceGroup (p_Img, currMB->mbAddrX))))
00629 {
00630 *end_of_slice = TRUE;
00631 if(!p_Img->cod_counter)
00632 skip = FALSE;
00633 }
00634
00635
00636 if (new_slice && slice_too_big(currSlice, rlc_bits))
00637 {
00638 *end_of_slice = TRUE;
00639 if(!p_Img->cod_counter)
00640 skip = FALSE;
00641 }
00642 if (!*recode_macroblock)
00643 ++currSlice->num_mb;
00644 break;
00645
00646 case CALL_BACK:
00647 if (currMB->mbAddrX > 0 && !new_slice)
00648 {
00649 if (currSlice->slice_too_big(rlc_bits))
00650 {
00651 *recode_macroblock = TRUE;
00652 *end_of_slice = TRUE;
00653 }
00654 }
00655
00656 if ( (*recode_macroblock == FALSE) && (currMB->mbAddrX == FmoGetLastCodedMBOfSliceGroup (p_Img, FmoMB2SliceGroup (p_Img, currMB->mbAddrX))))
00657 *end_of_slice = TRUE;
00658 break;
00659
00660 default:
00661 snprintf(errortext, ET_SIZE, "Slice Mode %d not supported", p_Inp->slice_mode);
00662 error(errortext, 600);
00663 }
00664
00665 if (*recode_macroblock == TRUE)
00666 {
00667
00668 for (i=0; i<currSlice->max_part_nr; ++i)
00669 {
00670 dataPart = &(currSlice->partArr[i]);
00671 currStream = dataPart->bitstream;
00672 currStream->bits_to_go = currStream->stored_bits_to_go;
00673 currStream->byte_pos = currStream->stored_byte_pos;
00674 currStream->byte_buf = currStream->stored_byte_buf;
00675 p_Img->p_Stats->bit_slice = p_Img->p_Stats->stored_bit_slice;
00676
00677 if (currSlice->symbol_mode == CABAC)
00678 {
00679 dataPart->ee_cabac = dataPart->ee_recode;
00680 }
00681 }
00682 }
00683
00684 if (currSlice->symbol_mode == CAVLC)
00685 {
00686
00687 dataPart = &(currSlice->partArr[partMap[SE_MBTYPE]]);
00688 if(*end_of_slice == TRUE && skip == TRUE)
00689 {
00690
00691
00692 if(p_Img->cod_counter && *recode_macroblock == TRUE)
00693 {
00694
00695
00696 p_Img->cod_counter--;
00697 if(p_Img->cod_counter)
00698 {
00699 se.value1 = p_Img->cod_counter;
00700 se.value2 = 0;
00701 se.type = SE_MBTYPE;
00702 #if TRACE
00703 snprintf(se.tracestring, TRACESTRING_SIZE, "Final MB runlength = %3d",p_Img->cod_counter);
00704 #endif
00705 writeSE_UVLC(&se, dataPart);
00706 rlc_bits=se.len;
00707 currMB->bits.mb_mode = currMB->bits.mb_mode + (unsigned short) rlc_bits;
00708 p_Img->cod_counter = 0;
00709 }
00710 }
00711 else
00712 {
00713 currStream = dataPart->bitstream;
00714
00715 currStream->bits_to_go = currStream->bits_to_go_skip;
00716 currStream->byte_pos = currStream->byte_pos_skip;
00717 currStream->byte_buf = currStream->byte_buf_skip;
00718
00719
00720 p_Img->cod_counter = 0;
00721 skip = FALSE;
00722 }
00723 }
00724
00725
00726 if(*end_of_slice == TRUE && p_Img->cod_counter && !use_bitstream_backing)
00727 {
00728 se.value1 = p_Img->cod_counter;
00729 se.value2 = 0;
00730 se.type = SE_MBTYPE;
00731
00732 TRACE_SE (se.tracestring, "mb_skip_run");
00733 writeSE_UVLC(&se, dataPart);
00734
00735 rlc_bits=se.len;
00736 currMB->bits.mb_mode = currMB->bits.mb_mode + (unsigned short) rlc_bits;
00737 p_Img->cod_counter = 0;
00738 }
00739 }
00740 }
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763 int slice_too_big(Slice *currSlice, int rlc_bits)
00764 {
00765 InputParameters *p_Inp = currSlice->p_Inp;
00766 DataPartition *dataPart;
00767 Bitstream *currStream;
00768 EncodingEnvironmentPtr eep;
00769 int i;
00770 int size_in_bytes;
00771
00772
00773 if (currSlice->symbol_mode == CAVLC)
00774 {
00775 for (i = 0; i < currSlice->max_part_nr; ++i)
00776 {
00777 dataPart = &(currSlice->partArr[i]);
00778 currStream = dataPart->bitstream;
00779 size_in_bytes = currStream->byte_pos ;
00780
00781 if (currStream->bits_to_go < 8)
00782 ++size_in_bytes;
00783 if (currStream->bits_to_go < rlc_bits)
00784 ++size_in_bytes;
00785 if(size_in_bytes > p_Inp->slice_argument)
00786 return TRUE;
00787 }
00788 }
00789 else
00790 {
00791 for (i=0; i<currSlice->max_part_nr; ++i)
00792 {
00793 dataPart= &(currSlice->partArr[i]);
00794 eep = &(dataPart->ee_cabac);
00795
00796 if( arienco_bits_written(eep) > (p_Inp->slice_argument*8))
00797 return TRUE;
00798 }
00799 }
00800
00801 return FALSE;
00802 }
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814 int luma_residual_coding_16x16 (Macroblock* currMB,
00815 int block8x8,
00816 int list_mode[2]
00817 )
00818 {
00819
00820 int *cbp = &(currMB->cbp);
00821 int64 *cbp_blk = &(currMB->cbp_blk);
00822 Slice *currSlice = currMB->p_slice;
00823 ImageParameters *p_Img = currMB->p_Img;
00824 int i, j, nonzero = 0, cbp_blk_mask;
00825 int coeff_cost = 0;
00826 int mb_y = (block8x8 >> 1) << 3;
00827 int mb_x = (block8x8 & 0x01) << 3;
00828 int cbp_mask = 1 << block8x8;
00829
00830 int skipped = (list_mode[0] == 0 && list_mode[1] == 0 && (currSlice->slice_type != B_SLICE));
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841 if(!currMB->luma_transform_size_8x8_flag)
00842 {
00843
00844 if (!skipped && ( (currSlice->NoResidueDirect != 1) || (currMB->qp_scaled[0] == 0 && p_Img->lossless_qpprime_flag == 1) ))
00845 {
00846 int block_y, block_x;
00847
00848 for (block_y=mb_y; block_y<mb_y + 8; block_y += 4)
00849 {
00850 for (block_x=mb_x; block_x<mb_x + 8; block_x += 4)
00851 {
00852
00853 nonzero = currMB->trans_4x4 (currMB, PLANE_Y, block_x, block_y, &coeff_cost, 0);
00854
00855 if (nonzero)
00856 {
00857 cbp_blk_mask = (block_x >> 2) + block_y;
00858 (*cbp_blk) |= (int64) 1 << cbp_blk_mask;
00859 (*cbp) |= cbp_mask;
00860 }
00861 }
00862 }
00863 }
00864 }
00865 else
00866 {
00867 if (currSlice->NoResidueDirect != 1 && !skipped)
00868 {
00869 if (currSlice->slice_type != SP_SLICE)
00870 nonzero = currMB->trans_8x8 (currMB, PLANE_Y, block8x8, &coeff_cost, 0);
00871
00872 if (nonzero)
00873 {
00874 (*cbp_blk) |= 51 << (4*block8x8 - 2*(block8x8 & 0x01));
00875 (*cbp) |= cbp_mask;
00876 }
00877 }
00878 }
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900 if (currSlice->NoResidueDirect != 1 && !skipped && coeff_cost <= _LUMA_COEFF_COST_ &&
00901 ((currMB->qp_scaled[0])!=0 || p_Img->lossless_qpprime_flag==0)&&
00902 !(currSlice->slice_type == SP_SLICE && (p_Img->si_frame_indicator == TRUE || p_Img->sp2_frame_indicator == TRUE )))
00903
00904 {
00905 coeff_cost = 0;
00906 (*cbp) &= (63 - cbp_mask);
00907 (*cbp_blk) &= ~(51 << (4 * block8x8 - 2 * (block8x8 & 0x01)));
00908
00909 memset( currSlice->cofAC[block8x8][0][0], 0, 4 * 2 * 65 * sizeof(int));
00910
00911 copy_image_data_8x8(&p_Img->enc_picture->imgY[currMB->pix_y + mb_y], &currSlice->mb_pred[0][mb_y], currMB->pix_x + mb_x, mb_x);
00912
00913 if (currSlice->slice_type == SP_SLICE)
00914 {
00915 for (i = mb_x; i < mb_x + BLOCK_SIZE_8x8; i += BLOCK_SIZE)
00916 for (j = mb_y; j < mb_y + BLOCK_SIZE_8x8; j += BLOCK_SIZE)
00917 copyblock_sp(currMB, PLANE_Y, i, j);
00918 }
00919 }
00920
00921 return coeff_cost;
00922 }
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933 int luma_residual_coding_8x8 (Macroblock* currMB,
00934 int *cbp,
00935 int64 *cbp_blk,
00936 int block8x8,
00937 short p_dir,
00938 int list_mode[2],
00939 char *ref_idx)
00940 {
00941 Slice *currSlice = currMB->p_slice;
00942 ImageParameters *p_Img = currSlice->p_Img;
00943 int block_y, block_x, pic_pix_y, pic_pix_x, i, j, nonzero = 0, cbp_blk_mask;
00944 int coeff_cost = 0;
00945 int mb_y = (block8x8 >> 1) << 3;
00946 int mb_x = (block8x8 & 0x01) << 3;
00947 int cbp_mask = 1 << block8x8;
00948 int skipped = (list_mode[0] == 0 && list_mode[1] == 0 && (currSlice->slice_type != B_SLICE));
00949
00950 char bipred_me = currMB->b8x8[block8x8].bipred;
00951 currSlice->coeff_cost_cr[1] = currSlice->coeff_cost_cr[2] = 0;
00952
00953
00954
00955
00956 if(!currMB->luma_transform_size_8x8_flag)
00957 {
00958 int bxx, byy;
00959 if (((p_dir == 0 || p_dir == 2 )&& list_mode[0] < 5) || ((p_dir == 1 || p_dir == 2 ) && list_mode[1] < 5))
00960 {
00961 luma_prediction (currMB, mb_x, mb_y, 8, 8, p_dir, list_mode, ref_idx, bipred_me);
00962
00963
00964 compute_residue (&p_Img->pCurImg[currMB->opix_y + mb_y], &currSlice->mb_pred[0][mb_y], &currSlice->mb_ores[0][mb_y], mb_x, currMB->pix_x + mb_x, 8, 8);
00965 }
00966
00967 for (byy=0, block_y=mb_y; block_y<mb_y+8; byy+=4, block_y+=4)
00968 {
00969 for (bxx=0, block_x=mb_x; block_x<mb_x+8; bxx+=4, block_x+=4)
00970 {
00971 pic_pix_x = currMB->pix_x + block_x;
00972 cbp_blk_mask = (block_x >> 2) + block_y;
00973
00974
00975 if (!(((p_dir == 0 || p_dir == 2 )&& list_mode[0] < 5) || ((p_dir == 1 || p_dir == 2 ) && list_mode[1] < 5)))
00976 {
00977 luma_prediction (currMB, block_x, block_y, 4, 4, p_dir, list_mode, ref_idx, bipred_me);
00978
00979
00980 compute_residue(&p_Img->pCurImg[currMB->opix_y + block_y], &currSlice->mb_pred[0][block_y], &currSlice->mb_ores[0][block_y], block_x, pic_pix_x, 4, 4);
00981 }
00982
00983
00984 if (!skipped && ( (currSlice->NoResidueDirect != 1) || (currMB->qp_scaled[0] == 0 && p_Img->lossless_qpprime_flag == 1) ))
00985 {
00986
00987 nonzero = currMB->trans_4x4 (currMB, PLANE_Y, block_x, block_y, &coeff_cost, 0);
00988
00989 if (nonzero)
00990 {
00991 (*cbp_blk) |= (int64)1 << cbp_blk_mask;
00992 (*cbp) |= cbp_mask;
00993 }
00994 }
00995 }
00996 }
00997 }
00998 else
00999 {
01000 block_y = mb_y;
01001 pic_pix_y = currMB->opix_y + block_y;
01002
01003 block_x = mb_x;
01004 pic_pix_x = currMB->pix_x + block_x;
01005
01006 cbp_blk_mask = (block_x>>2) + block_y;
01007
01008
01009 luma_prediction (currMB, block_x, block_y, 8, 8, p_dir, list_mode, ref_idx, bipred_me);
01010
01011
01012 compute_residue (&p_Img->pCurImg[currMB->opix_y + block_y], &currSlice->mb_pred[0][block_y], &currSlice->mb_ores[0][block_y], block_x, pic_pix_x, 8, 8);
01013
01014 if (currSlice->NoResidueDirect != 1 && !skipped)
01015 {
01016 if (currSlice->slice_type != SP_SLICE)
01017 nonzero = currMB->trans_8x8 (currMB, PLANE_Y, block8x8, &coeff_cost, 0);
01018
01019 if (nonzero)
01020 {
01021 (*cbp_blk) |= 51 << (4*block8x8 - 2*(block8x8 & 0x01));
01022 (*cbp) |= cbp_mask;
01023 }
01024 }
01025 }
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047 if (currSlice->NoResidueDirect != 1 && !skipped && coeff_cost <= _LUMA_COEFF_COST_ &&
01048 ((currMB->qp_scaled[0])!=0 || p_Img->lossless_qpprime_flag==0)&&
01049 !(currSlice->slice_type == SP_SLICE && (p_Img->si_frame_indicator == TRUE || p_Img->sp2_frame_indicator == TRUE )))
01050
01051 {
01052 coeff_cost = 0;
01053 (*cbp) &= (63 - cbp_mask);
01054 (*cbp_blk) &= ~(51 << (4 * block8x8 - 2 * (block8x8 & 0x01)));
01055
01056 memset( currSlice->cofAC[block8x8][0][0], 0, 4 * 2 * 65 * sizeof(int));
01057
01058 copy_image_data_8x8(&p_Img->enc_picture->imgY[currMB->pix_y + mb_y], &currSlice->mb_pred[0][mb_y], currMB->pix_x + mb_x, mb_x);
01059
01060 if (currSlice->slice_type == SP_SLICE)
01061 {
01062 for (i=mb_x; i < mb_x + BLOCK_SIZE_8x8; i+=BLOCK_SIZE)
01063 for (j=mb_y; j < mb_y + BLOCK_SIZE_8x8; j+=BLOCK_SIZE)
01064 copyblock_sp(currMB, PLANE_Y, i, j);
01065 }
01066 }
01067
01068 return coeff_cost;
01069 }
01070
01071
01072
01073
01074
01075
01076
01077 void set_modes_and_reframe (Macroblock* currMB, int b8, short* p_dir, int list_mode[2], char *list_ref_idx)
01078 {
01079 ImageParameters *p_Img = currMB->p_Img;
01080 Slice *currSlice = currMB->p_slice;
01081 PicMotionParams *motion = &p_Img->enc_picture->motion;
01082 int j = 2*(b8>>1);
01083 int i = 2*(b8 & 0x01);
01084
01085 list_mode[0] = list_mode[1] = list_ref_idx[0] = list_ref_idx[1] = -1;
01086
01087 *p_dir = currMB->b8x8[b8].pdir;
01088
01089 if (currSlice->slice_type != B_SLICE)
01090 {
01091 list_ref_idx[0] = motion->ref_idx[LIST_0][currMB->block_y + j][currMB->block_x + i];
01092 list_ref_idx[1] = 0;
01093 list_mode[0] = currMB->b8x8[b8].mode;
01094 list_mode[1] = 0;
01095 }
01096 else
01097 {
01098 if (*p_dir == -1)
01099 {
01100 list_ref_idx[0] = -1;
01101 list_ref_idx[1] = -1;
01102 list_mode[0] = 0;
01103 list_mode[1] = 0;
01104 }
01105 else if (*p_dir == 0)
01106 {
01107 list_ref_idx[0] = motion->ref_idx[LIST_0][currMB->block_y + j][currMB->block_x + i];
01108 list_ref_idx[1] = 0;
01109 list_mode[0] = currMB->b8x8[b8].mode;
01110 list_mode[1] = 0;
01111 }
01112 else if (*p_dir == 1)
01113 {
01114 list_ref_idx[0] = 0;
01115 list_ref_idx[1] = motion->ref_idx[LIST_1][currMB->block_y + j][currMB->block_x + i];
01116 list_mode[0] = 0;
01117 list_mode[1] = currMB->b8x8[b8].mode;
01118 }
01119 else
01120 {
01121 list_ref_idx[0] = motion->ref_idx[LIST_0][currMB->block_y + j][currMB->block_x + i];
01122 list_ref_idx[1] = motion->ref_idx[LIST_1][currMB->block_y + j][currMB->block_x + i];
01123 list_mode[0] = currMB->b8x8[b8].mode;
01124 list_mode[1] = currMB->b8x8[b8].mode;
01125 }
01126 }
01127 }
01128
01129
01130
01131
01132
01133
01134
01135
01136 void set_modes_and_reframe_p_slice (Macroblock* currMB, int b8, short* p_dir, int list_mode[2], char *list_ref_idx)
01137 {
01138 ImageParameters *p_Img = currMB->p_Img;
01139 PicMotionParams *motion = &p_Img->enc_picture->motion;
01140 int j = 2*(b8>>1);
01141 int i = 2*(b8 & 0x01);
01142
01143 list_mode[0] = list_mode[1] = list_ref_idx[0] = list_ref_idx[1] = -1;
01144
01145 *p_dir = currMB->b8x8[b8].pdir;
01146
01147 list_ref_idx[0] = motion->ref_idx[LIST_0][currMB->block_y + j][currMB->block_x + i];
01148 list_ref_idx[1] = 0;
01149 list_mode[0] = currMB->b8x8[b8].mode;
01150 list_mode[1] = 0;
01151 }
01152
01153
01154
01155
01156
01157
01158
01159
01160
01161 void set_modes_and_reframe_b_slice (Macroblock* currMB, int b8, short* p_dir, int list_mode[2], char *list_ref_idx)
01162 {
01163 ImageParameters *p_Img = currMB->p_Img;
01164 PicMotionParams *motion = &p_Img->enc_picture->motion;
01165 int j = 2*(b8>>1);
01166 int i = 2*(b8 & 0x01);
01167
01168 list_mode[0] = list_mode[1] = list_ref_idx[0] = list_ref_idx[1] = -1;
01169
01170 *p_dir = currMB->b8x8[b8].pdir;
01171
01172 if (*p_dir == -1)
01173 {
01174 list_ref_idx[0] = -1;
01175 list_ref_idx[1] = -1;
01176 list_mode[0] = 0;
01177 list_mode[1] = 0;
01178 }
01179 else if (*p_dir == 0)
01180 {
01181 list_ref_idx[0] = motion->ref_idx[LIST_0][currMB->block_y + j][currMB->block_x + i];
01182 list_ref_idx[1] = 0;
01183 list_mode[0] = currMB->b8x8[b8].mode;
01184 list_mode[1] = 0;
01185 }
01186 else if (*p_dir == 1)
01187 {
01188 list_ref_idx[0] = 0;
01189 list_ref_idx[1] = motion->ref_idx[LIST_1][currMB->block_y + j][currMB->block_x + i];
01190 list_mode[0] = 0;
01191 list_mode[1] = currMB->b8x8[b8].mode;
01192 }
01193 else
01194 {
01195 list_ref_idx[0] = motion->ref_idx[LIST_0][currMB->block_y + j][currMB->block_x + i];
01196 list_ref_idx[1] = motion->ref_idx[LIST_1][currMB->block_y + j][currMB->block_x + i];
01197 list_mode[0] = currMB->b8x8[b8].mode;
01198 list_mode[1] = currMB->b8x8[b8].mode;
01199 }
01200 }
01201
01202
01203
01204
01205
01206
01207
01208
01209 void luma_residual_coding (Macroblock *currMB)
01210 {
01211 Slice *currSlice = currMB->p_slice;
01212 ImageParameters *p_Img = currMB->p_Img;
01213
01214 int i,j,block8x8,b8_x,b8_y;
01215 int list_mode[2];
01216 char list_ref_idx[2];
01217 short p_dir = 0;
01218 int sum_cnt_nonz = 0;
01219
01220 int is_skip = currSlice->slice_type == P_SLICE && currMB->mb_type == PSKIP;
01221
01222 currMB->cbp = 0;
01223 currMB->cbp_blk = 0;
01224
01225 currSlice->cmp_cbp[1] = currSlice->cmp_cbp[2] = 0;
01226 currSlice->cur_cbp_blk[1] = currSlice->cur_cbp_blk[2] = 0;
01227
01228 if (is_skip || currMB->mb_type == 1)
01229 {
01230 for (block8x8=0; block8x8<4; ++block8x8)
01231 {
01232 currSlice->set_modes_and_reframe (currMB, block8x8, &p_dir, list_mode, list_ref_idx);
01233 }
01234
01235
01236 luma_prediction (currMB, 0, 0, 16, 16, p_dir, list_mode, list_ref_idx, currMB->b8x8[0].bipred);
01237
01238
01239
01240 compute_residue (&p_Img->pCurImg[currMB->opix_y], &currSlice->mb_pred[0][0], &currSlice->mb_ores[0][0], 0, currMB->pix_x, 16, 16);
01241
01242
01243 if (!is_skip)
01244 {
01245 for (block8x8=0; block8x8<4; ++block8x8)
01246 {
01247 sum_cnt_nonz += luma_residual_coding_16x16 (currMB, block8x8, list_mode);
01248 }
01249 }
01250 }
01251 else
01252 {
01253 for (block8x8=0; block8x8<4; ++block8x8)
01254 {
01255 currSlice->set_modes_and_reframe (currMB, block8x8, &p_dir, list_mode, list_ref_idx);
01256 sum_cnt_nonz += luma_residual_coding_8x8 (currMB, &(currMB->cbp), &(currMB->cbp_blk), block8x8, p_dir, list_mode, list_ref_idx);
01257 }
01258 }
01259
01260
01261 if ((is_skip ||
01262 (sum_cnt_nonz <= _LUMA_MB_COEFF_COST_ && ((currMB->qp_scaled[0])!=0 || p_Img->lossless_qpprime_flag == 0))) &&
01263 !(currSlice->slice_type == SP_SLICE && (p_Img->si_frame_indicator == TRUE || p_Img->sp2_frame_indicator == TRUE)))
01264 {
01265 currMB->cbp &= 0xfffff0 ;
01266 currMB->cbp_blk &= 0xff0000 ;
01267
01268 copy_image_data_16x16(&p_Img->enc_picture->imgY[currMB->pix_y], currSlice->mb_pred[0], currMB->pix_x, 0);
01269
01270 memset( currSlice->cofAC[0][0][0], 0, 2080 * sizeof(int));
01271
01272 if (currSlice->slice_type == SP_SLICE)
01273 {
01274 for(block8x8 = 0;block8x8 < 4; ++block8x8)
01275 {
01276 b8_x=(block8x8&1)<<3;
01277 b8_y=(block8x8&2)<<2;
01278 for (i = b8_x; i < b8_x + BLOCK_SIZE_8x8; i += 4)
01279 for (j = b8_y; j < b8_y + BLOCK_SIZE_8x8;j += 4)
01280 copyblock_sp(currMB, PLANE_Y, i, j);
01281 }
01282 }
01283 }
01284 }
01285
01286
01287
01288
01289
01290
01291
01292
01293 byte TransformDecision (Macroblock *currMB, int block_check, int *cost)
01294 {
01295 if(currMB->p_Inp->Transform8x8Mode == 2)
01296 {
01297 return 1;
01298 }
01299 else
01300 {
01301 ImageParameters *p_Img = currMB->p_Img;
01302 Slice *currSlice = currMB->p_slice;
01303
01304 int block_y, block_x, i, j, k;
01305 int mb_y, mb_x, block8x8;
01306 int list_mode[2];
01307 char list_ref_idx[2];
01308 short p_dir;
01309 int num_blks;
01310 int cost8x8=0, cost4x4=0;
01311 int *diff_ptr;
01312 char bipred_me;
01313 imgpel **mb_pred = currSlice->mb_pred[0];
01314 int diff64[64];
01315
01316 if(block_check == -1)
01317 {
01318 block8x8 = 0;
01319 num_blks = 4;
01320 }
01321 else
01322 {
01323 block8x8 = block_check;
01324 num_blks = block_check + 1;
01325 }
01326
01327 for (; block8x8<num_blks; ++block8x8)
01328 {
01329 currSlice->set_modes_and_reframe (currMB, block8x8, &p_dir, list_mode, list_ref_idx);
01330 bipred_me = currMB->b8x8[block8x8].bipred;
01331 mb_y = (block8x8 >> 1) << 3;
01332 mb_x = (block8x8 & 0x01) << 3;
01333
01334
01335 luma_prediction (currMB, mb_x, mb_y, 8, 8, p_dir, list_mode, list_ref_idx, bipred_me);
01336
01337
01338 for (k=0, block_y=mb_y; block_y<mb_y+8; block_y+=4)
01339 {
01340 for (block_x=mb_x; block_x<mb_x+8; block_x+=4)
01341 {
01342
01343 diff_ptr=&diff64[k];
01344 for (j=block_y; j<block_y + 4; ++j)
01345 {
01346 for (i = block_x; i < block_x + 4; ++i, ++k)
01347 diff64[k] = p_Img->pCurImg[currMB->opix_y + j][currMB->pix_x + i] - mb_pred[j][i];
01348 }
01349 cost4x4 += p_Img->distortion4x4 (diff_ptr, INT_MAX);
01350 }
01351 }
01352 cost8x8 += p_Img->distortion8x8 (diff64, INT_MAX);
01353 }
01354
01355
01356 if(cost8x8 < cost4x4)
01357 {
01358 return 1;
01359 }
01360 else
01361 {
01362 *cost += (cost4x4 - cost8x8);
01363 return 0;
01364 }
01365 }
01366 }
01367
01368
01369
01370
01371
01372
01373
01374
01375
01376 void chroma_residual_coding (Macroblock *currMB)
01377 {
01378 Slice *currSlice = currMB->p_slice;
01379 ImageParameters *p_Img = currSlice->p_Img;
01380 int chroma_cbp;
01381 int uv, block8, block_y, block_x, j;
01382 int list_mode[2];
01383 char list_ref_idx[2];
01384 short p_dir;
01385 int skipped = (currMB->mb_type == 0 && (currSlice->slice_type == P_SLICE || currSlice->slice_type == SP_SLICE));
01386 int yuv = p_Img->yuv_format - 1;
01387 char bipred_me;
01388
01389 for (chroma_cbp = 0, uv=0; uv<2; ++uv)
01390 {
01391
01392 block8 = 0;
01393 for (block_y=0; block_y < p_Img->mb_cr_size_y; block_y+=4)
01394 {
01395 for (block_x=0; block_x < p_Img->mb_cr_size_x; block_x+=4)
01396 {
01397 block8 = block8x8_idx[yuv][block_y>>2][block_x>>2];
01398 bipred_me = currMB->b8x8[block8].bipred;
01399 currSlice->set_modes_and_reframe (currMB, block8, &p_dir, list_mode, list_ref_idx);
01400
01401 chroma_prediction_4x4 (currMB, uv, block_x, block_y, p_dir, list_mode[0], list_mode[1], list_ref_idx[0], list_ref_idx[1], bipred_me);
01402
01403 }
01404 }
01405
01406
01407 if (currSlice->NoResidueDirect)
01408 {
01409 copy_image_data(&p_Img->enc_picture->imgUV[uv][currMB->pix_c_y], currSlice->mb_pred[ uv + 1], currMB->pix_c_x, 0, p_Img->mb_cr_size_x, p_Img->mb_cr_size_y);
01410 }
01411 else if (skipped && currSlice->slice_type == SP_SLICE)
01412 {
01413 for (j=0; j<p_Img->mb_cr_size_y; ++j)
01414 memset(currSlice->mb_ores[uv + 1][j], 0 , p_Img->mb_cr_size_x * sizeof(int));
01415 for (j=0; j<p_Img->mb_cr_size_y; ++j)
01416 memset(currSlice->mb_rres[uv + 1][j], 0 , p_Img->mb_cr_size_x * sizeof(int));
01417 }
01418 else if (skipped)
01419 {
01420 copy_image_data(&p_Img->enc_picture->imgUV[uv][currMB->pix_c_y], currSlice->mb_pred[ uv + 1], currMB->pix_c_x, 0, p_Img->mb_cr_size_x, p_Img->mb_cr_size_y);
01421 }
01422 else
01423 {
01424 compute_residue(&p_Img->pImgOrg[uv + 1][currMB->opix_c_y ], &currSlice->mb_pred[ uv + 1][0], &currSlice->mb_ores[uv + 1][0], 0, currMB->pix_c_x, p_Img->mb_cr_size_y, p_Img->mb_cr_size_x);
01425 }
01426
01427
01428
01429
01430 if (skipped && currSlice->slice_type == SP_SLICE)
01431 {
01432 if(p_Img->si_frame_indicator || p_Img->sp2_frame_indicator)
01433 chroma_cbp = dct_chroma_sp2(currMB, uv,chroma_cbp);
01434 else
01435 chroma_cbp = dct_chroma_sp(currMB, uv,chroma_cbp);
01436 }
01437 else
01438 {
01439 if (!currSlice->NoResidueDirect && !skipped)
01440 {
01441 if (currSlice->slice_type != SP_SLICE || (currMB->mb_type==I16MB ))
01442 {
01443
01444 chroma_cbp = currMB->trans_cr_4x4[uv] (currMB, uv,chroma_cbp);
01445 }
01446 else
01447 {
01448 if(p_Img->si_frame_indicator||p_Img->sp2_frame_indicator)
01449 chroma_cbp=dct_chroma_sp2(currMB, uv,chroma_cbp);
01450 else
01451 chroma_cbp=dct_chroma_sp(currMB, uv,chroma_cbp);
01452 }
01453 }
01454 }
01455 }
01456
01457
01458 currMB->cbp += ((chroma_cbp)<<4);
01459 }
01460
01461
01462
01463
01464
01465
01466
01467
01468 void intra_chroma_RD_decision (Macroblock *currMB, RD_PARAMS enc_mb)
01469 {
01470 Slice *currSlice = currMB->p_slice;
01471 ImageParameters *p_Img = currMB->p_Img;
01472 InputParameters *p_Inp = currMB->p_Inp;
01473 int i, j, k;
01474 imgpel** image;
01475 int block_x, block_y;
01476 int mb_available_up;
01477 int mb_available_left[2];
01478 int mb_available_up_left;
01479 int uv;
01480 int mode;
01481 int best_mode = DC_PRED_8;
01482 int cost;
01483 int min_cost;
01484 PixelPos up;
01485 PixelPos left[17];
01486 int cr_MB_x = p_Img->mb_cr_size_x;
01487 int cr_MB_y = p_Img->mb_cr_size_y;
01488 imgpel *img_org, *img_prd;
01489 imgpel ***curr_mpr_16x16;
01490 int diff [16];
01491
01492 for (i=0;i<cr_MB_y+1;++i)
01493 {
01494 p_Img->getNeighbour(currMB, -1 , i-1 , p_Img->mb_size[IS_CHROMA], &left[i]);
01495 }
01496 p_Img->getNeighbour(currMB, 0 , -1 , p_Img->mb_size[IS_CHROMA], &up);
01497
01498 mb_available_up = up.available;
01499 mb_available_up_left = left[0].available;
01500 mb_available_left[0] = mb_available_left[1] = left[1].available;
01501
01502 if(p_Inp->UseConstrainedIntraPred)
01503 {
01504 mb_available_up = up.available ? p_Img->intra_block[up.mb_addr] : 0;
01505 for (i=1, mb_available_left[0] = 1; i < (cr_MB_y>>1) + 1; ++i)
01506 mb_available_left[0] &= left[i].available ? p_Img->intra_block[left[i].mb_addr]: 0;
01507
01508 for (i=(cr_MB_y>>1) + 1, mb_available_left[1]=1; i < cr_MB_y + 1;++i)
01509 mb_available_left[1] &= left[i].available ? p_Img->intra_block[left[i].mb_addr]: 0;
01510
01511 mb_available_up_left = left[0].available ? p_Img->intra_block[left[0].mb_addr]: 0;
01512 }
01513
01514
01515 min_cost = INT_MAX;
01516 for (i=0;i<cr_MB_y;++i)
01517 {
01518 p_Img->getNeighbour(currMB, 0, i, p_Img->mb_size[IS_CHROMA], &left[i]);
01519 }
01520 if ( currSlice->MbaffFrameFlag && p_Img->field_mode )
01521 {
01522 for (i=0;i<cr_MB_y;++i)
01523 {
01524 left[i].pos_y = left[i].pos_y >> 1;
01525 }
01526 }
01527
01528 for (mode=DC_PRED_8; mode<=PLANE_8; ++mode)
01529 {
01530 if ((mode==VERT_PRED_8 && !mb_available_up) ||
01531 (mode==HOR_PRED_8 && (!mb_available_left[0] || !mb_available_left[1])) ||
01532 (mode==PLANE_8 && (!mb_available_left[0] || !mb_available_left[1] || !mb_available_up || !mb_available_up_left)))
01533 continue;
01534
01535 cost = 0;
01536 for (uv = 1; uv < 3; ++uv)
01537 {
01538 image = p_Img->pImgOrg[uv];
01539 curr_mpr_16x16 = currSlice->mpr_16x16[uv];
01540 for (block_y=0; block_y<cr_MB_y; block_y+=4)
01541 {
01542 for (block_x=0; block_x<cr_MB_x; block_x+=4)
01543 {
01544 for (k=0,j=block_y; j<block_y+4; ++j)
01545 {
01546 img_prd = curr_mpr_16x16[mode][j];
01547 img_org = &image[left[j].pos_y][left[j].pos_x];
01548
01549 for (i = block_x; i < block_x + 4; ++i)
01550 diff[k++] = img_org[i] - img_prd[i];
01551 }
01552
01553 cost += p_Img->distortion4x4(diff, min_cost);
01554 }
01555 if (cost > min_cost) break;
01556 }
01557 if (cost > min_cost) break;
01558 }
01559
01560
01561 cost += weight_cost(enc_mb.lambda_mf[Q_PEL], p_Img->mvbits[ mode ]);
01562
01563 if (cost < min_cost)
01564 {
01565 best_mode = mode;
01566 min_cost = cost;
01567 }
01568 }
01569 currMB->c_ipred_mode = (char) best_mode;
01570 }
01571
01572
01573
01574
01575
01576
01577
01578
01579 int ZeroRef (Macroblock* currMB)
01580 {
01581 PicMotionParams *motion = &currMB->p_Img->enc_picture->motion;
01582 int i,j;
01583
01584 for (j=currMB->block_y; j<currMB->block_y + BLOCK_MULTIPLE; ++j)
01585 {
01586 for (i=currMB->block_x; i<currMB->block_x + BLOCK_MULTIPLE; ++i)
01587 {
01588 if (motion->ref_idx[LIST_0][j][i] != 0)
01589 return 0;
01590 }
01591 }
01592 return 1;
01593 }
01594
01595
01596
01597
01598
01599
01600
01601
01602 int MBType2Value (Macroblock* currMB)
01603 {
01604 Slice *currSlice = currMB->p_slice;
01605 static const int dir1offset[3] = { 1, 2, 3};
01606 static const int dir2offset[3][3] = {{ 0, 4, 8},
01607 { 6, 2, 10},
01608 {12, 14, 16}};
01609
01610 int mbtype, pdir0, pdir1;
01611
01612 if (currSlice->slice_type != B_SLICE)
01613 {
01614 if (currMB->mb_type==I8MB ||currMB->mb_type==I4MB)
01615 return (currSlice->slice_type == I_SLICE ? 0 : 6);
01616 else if (currMB->mb_type==I16MB) return (currSlice->slice_type == I_SLICE ? 0 : 6) + currMB->i16offset;
01617 else if (currMB->mb_type==IPCM) return (currSlice->slice_type == I_SLICE ? 25 : 31);
01618 else if (currMB->mb_type==P8x8)
01619 {
01620 if (currSlice->symbol_mode==CAVLC
01621 && ZeroRef (currMB)) return 5;
01622 else return 4;
01623 }
01624 else return currMB->mb_type;
01625 }
01626 else
01627 {
01628 mbtype = currMB->mb_type;
01629 pdir0 = currMB->b8x8[0].pdir;
01630 pdir1 = currMB->b8x8[3].pdir;
01631
01632 if (mbtype==0) return 0;
01633 else if (mbtype==I4MB || mbtype==I8MB) return 23;
01634 else if (mbtype==I16MB) return 23 + currMB->i16offset;
01635 else if (mbtype==IPCM) return 48;
01636 else if (mbtype==P8x8) return 22;
01637 else if (mbtype==1) return dir1offset[pdir0];
01638 else if (mbtype==2) return 4 + dir2offset[pdir0][pdir1];
01639 else return 5 + dir2offset[pdir0][pdir1];
01640 }
01641 }
01642
01643
01644
01645
01646
01647
01648
01649 static int writeIntra4x4Modes(Macroblock *currMB)
01650 {
01651 Slice *currSlice = currMB->p_slice;
01652 int i;
01653 SyntaxElement se;
01654 BitCounter *mbBits = &currMB->bits;
01655 const int *partMap = assignSE2partition[currSlice->partition_mode];
01656 DataPartition *dataPart = &(currSlice->partArr[partMap[SE_INTRAPREDMODE]]);
01657
01658 int rate = 0;
01659
01660 currMB->IntraChromaPredModeFlag = 1;
01661
01662 for(i = 0; i < 16; ++i)
01663 {
01664 se.context = i;
01665 se.value1 = currMB->intra_pred_modes[i];
01666 se.value2 = 0;
01667
01668 #if TRACE
01669 if (se.value1 < 0 )
01670 snprintf(se.tracestring, TRACESTRING_SIZE, "Intra 4x4 mode = predicted (context: %d)",se.context);
01671 else
01672 snprintf(se.tracestring, TRACESTRING_SIZE, "Intra 4x4 mode = %3d (context: %d)",se.value1,se.context);
01673 #endif
01674
01675
01676 se.type = SE_INTRAPREDMODE;
01677
01678
01679 currSlice->writeIntraPredMode (&se, dataPart);
01680
01681 rate += se.len;
01682 }
01683
01684 mbBits->mb_y_coeff += rate;
01685
01686 return rate;
01687 }
01688
01689
01690
01691
01692
01693
01694
01695 static int writeIntra8x8Modes(Macroblock *currMB)
01696 {
01697 Slice *currSlice = currMB->p_slice;
01698 int block8x8;
01699 SyntaxElement se;
01700 BitCounter *mbBits = &currMB->bits;
01701 const int *partMap = assignSE2partition[currSlice->partition_mode];
01702 DataPartition *dataPart = &(currSlice->partArr[partMap[SE_INTRAPREDMODE]]);
01703
01704 int rate = 0;
01705
01706 currMB->IntraChromaPredModeFlag = 1;
01707
01708 for(block8x8 = 0; block8x8 < 16; block8x8 += 4)
01709 {
01710
01711 se.context = block8x8;
01712 se.value1 = currMB->intra_pred_modes8x8[block8x8];
01713 se.value2 = 0;
01714
01715 #if TRACE
01716 if (se.value1 < 0 )
01717 snprintf(se.tracestring, TRACESTRING_SIZE, "Intra 8x8 mode = predicted (context: %d)",se.context);
01718 else
01719 snprintf(se.tracestring, TRACESTRING_SIZE, "Intra 8x8 mode = %3d (context: %d)",se.value1,se.context);
01720 #endif
01721
01722
01723 se.type = SE_INTRAPREDMODE;
01724
01725
01726 currSlice->writeIntraPredMode (&se, dataPart);
01727
01728 rate += se.len;
01729 }
01730 mbBits->mb_y_coeff += rate;
01731
01732 return rate;
01733 }
01734
01735 static int writeIntraModes(Macroblock *currMB)
01736 {
01737 switch (currMB->mb_type)
01738 {
01739 case I4MB:
01740 return writeIntra4x4Modes(currMB);
01741 break;
01742 case I8MB:
01743 return writeIntra8x8Modes(currMB);
01744 break;
01745 default:
01746 return 0;
01747 break;
01748 }
01749 }
01750
01751
01752
01753
01754
01755
01756
01757 int B8Mode2Value (Slice *currSlice, short b8mode, short b8pdir)
01758 {
01759 static const int b8start[8] = {0,0,0,0, 1, 4, 5, 10};
01760 static const int b8inc [8] = {0,0,0,0, 1, 2, 2, 1};
01761
01762 return (currSlice->slice_type!=B_SLICE) ? (b8mode - 4) : b8start[b8mode] + b8inc[b8mode] * b8pdir;
01763 }
01764
01765
01766 static int writeIPCMLuma (Macroblock *currMB, imgpel **imgY, Bitstream *currStream, SyntaxElement *se, int *bitCount)
01767 {
01768 ImageParameters *p_Img = currMB->p_Img;
01769 int i, j;
01770 int bit_size = MB_BLOCK_SIZE * MB_BLOCK_SIZE * p_Img->bitdepth_luma;
01771
01772 *bitCount += bit_size;
01773 se->len = p_Img->bitdepth_luma;
01774 se->type = SE_MBTYPE;
01775
01776 for (j = currMB->pix_y; j < currMB->pix_y + MB_BLOCK_SIZE; ++j)
01777 {
01778 for (i = currMB->pix_x; i < currMB->pix_x + MB_BLOCK_SIZE; ++i)
01779 {
01780 se->bitpattern = imax(p_Img->min_IPCM_value, imgY[j][i]);
01781 se->value1 = se->bitpattern;
01782 #if TRACE
01783 snprintf(se->tracestring, TRACESTRING_SIZE, "pcm_sample_luma (%d %d) = %d", j, i, se->bitpattern);
01784 #endif
01785 writeSE_Fix(se, currStream);
01786 }
01787 }
01788 return bit_size;
01789 }
01790
01791 static int writeIPCMChroma (Macroblock *currMB, imgpel **imgUV, Bitstream *currStream, SyntaxElement *se, int *bitCount)
01792 {
01793 ImageParameters *p_Img = currMB->p_Img;
01794 int i, j;
01795 int bit_size = p_Img->mb_cr_size_y * p_Img->mb_cr_size_x * p_Img->bitdepth_luma;
01796 *bitCount += bit_size;
01797
01798 se->len = p_Img->bitdepth_chroma;
01799 se->type = SE_MBTYPE;
01800
01801 for (j = currMB->pix_c_y; j < currMB->pix_c_y + p_Img->mb_cr_size_y; ++j)
01802 {
01803 for (i = currMB->pix_c_x; i < currMB->pix_c_x + p_Img->mb_cr_size_x; ++i)
01804 {
01805 se->bitpattern = imax(p_Img->min_IPCM_value, imgUV[j][i]);
01806 se->value1 = se->bitpattern;
01807 #if TRACE
01808
01809 snprintf(se->tracestring, TRACESTRING_SIZE, "pcm_sample_chroma (%d %d) = %d", j, i, se->bitpattern);
01810 #endif
01811 writeSE_Fix(se, currStream);
01812 }
01813 }
01814 return bit_size;
01815 }
01816
01817 static int writeIPCMByteAlign(Bitstream *currStream, SyntaxElement *se, int *bitCount)
01818 {
01819 if (currStream->bits_to_go < 8)
01820 {
01821
01822 se->type = SE_MBTYPE;
01823 se->len = currStream->bits_to_go;
01824 *bitCount += se->len;
01825 se->bitpattern = 0;
01826 #if TRACE
01827 snprintf(se->tracestring, TRACESTRING_SIZE, "pcm_alignment_zero_bits = %d", se->len);
01828 #endif
01829 writeSE_Fix(se, currStream);
01830
01831 return se->len;
01832 }
01833 else
01834 return 0;
01835 }
01836
01837
01838
01839
01840
01841
01842
01843 static int writeIPCMData(Macroblock *currMB, DataPartition* dataPart)
01844 {
01845 Slice *currSlice = currMB->p_slice;
01846 ImageParameters *p_Img = currMB->p_Img;
01847 InputParameters *p_Inp = currMB->p_Inp;
01848 int no_bits = 0;
01849 SyntaxElement se;
01850 BitCounter *mbBits = &currMB->bits;
01851
01852 if (currSlice->symbol_mode == CABAC)
01853 {
01854 int len;
01855 EncodingEnvironmentPtr eep = &dataPart->ee_cabac;
01856 len = arienco_bits_written(eep);
01857 arienco_done_encoding(currMB, eep);
01858 len = arienco_bits_written(eep) - len;
01859 no_bits += len;
01860
01861 arienco_start_encoding(eep, dataPart->bitstream->streamBuffer, &(dataPart->bitstream->byte_pos));
01862 }
01863
01864 writeIPCMByteAlign(dataPart->bitstream, &se, &(currMB->bits.mb_y_coeff));
01865
01866 no_bits += writeIPCMLuma (currMB, p_Img->enc_picture->imgY, dataPart->bitstream, &se, &mbBits->mb_y_coeff);
01867
01868 if ((p_Img->active_sps->chroma_format_idc != YUV400) && !IS_INDEPENDENT(p_Inp))
01869 {
01870 int uv;
01871 for (uv = 0; uv < 2; ++uv )
01872 {
01873 no_bits += writeIPCMChroma (currMB, p_Img->enc_picture->imgUV[uv], dataPart->bitstream, &se, &mbBits->mb_uv_coeff);
01874 }
01875 }
01876 return no_bits;
01877 }
01878
01879
01880
01881
01882
01883
01884
01885
01886
01887
01888
01889
01890
01891 int writeMBLayerBSlice (Macroblock *currMB, int rdopt, int *coeff_rate)
01892 {
01893 ImageParameters *p_Img = currMB->p_Img;
01894 InputParameters *p_Inp = currMB->p_Inp;
01895 Slice *currSlice = currMB->p_slice;
01896
01897 int i;
01898 int mb_nr = currMB->mbAddrX;
01899 Macroblock *prevMB = currMB->PrevMB;
01900 int prev_mb_nr = (NULL == prevMB) ? -1 : prevMB->mbAddrX;
01901
01902 SyntaxElement se;
01903 BitCounter *mbBits = &currMB->bits;
01904 const int* partMap = assignSE2partition[currSlice->partition_mode];
01905
01906 DataPartition* dataPart = &(currSlice->partArr[partMap[SE_MBTYPE]]);
01907
01908 int no_bits = 0;
01909 int skip = currMB->mb_type ? 0 : !currMB->cbp;
01910 int prevMbSkipped = 0;
01911 int WriteFrameFieldMBInHeader = 0;
01912
01913 if (currSlice->MbaffFrameFlag)
01914 {
01915 if ((mb_nr & 0x01) == 0)
01916 {
01917 WriteFrameFieldMBInHeader = 1;
01918
01919 prevMbSkipped = 0;
01920 }
01921 else
01922 {
01923 if (prevMB->mb_type ? 0: !prevMB->cbp)
01924 {
01925 WriteFrameFieldMBInHeader = 1;
01926 }
01927
01928 prevMbSkipped = p_Img->mb_data[prev_mb_nr].skip_flag;
01929 }
01930 }
01931 currMB->IntraChromaPredModeFlag = (char) IS_INTRA(currMB);
01932
01933 if (currSlice->symbol_mode == CABAC)
01934 {
01935 int mb_type = MBType2Value (currMB);
01936 if (currSlice->MbaffFrameFlag && ((p_Img->current_mb_nr & 0x01) == 0 || prevMbSkipped))
01937 {
01938 byte mb_field_tmp = currMB->mb_field;
01939 currMB->mb_field = field_flag_inference(currMB);
01940 CheckAvailabilityOfNeighborsCABAC(currMB);
01941 currMB->mb_field = mb_field_tmp;
01942 }
01943
01944
01945 se.value1 = mb_type;
01946 se.value2 = currMB->cbp;
01947 se.type = SE_MBTYPE;
01948
01949 TRACE_SE (se.tracestring, "mb_skip_flag");
01950 currSlice->writeMB_Skip(currMB, &se, dataPart);
01951
01952 no_bits += se.len;
01953
01954 CheckAvailabilityOfNeighborsCABAC(currMB);
01955
01956
01957 if(currSlice->MbaffFrameFlag && !skip)
01958 {
01959 if(WriteFrameFieldMBInHeader)
01960 {
01961 se.value1 = currMB->mb_field;
01962 se.value2 = 0;
01963 se.type = SE_MBTYPE;
01964
01965 TRACE_SE(se.tracestring, "mb_field_decoding_flag");
01966 currSlice->writeFieldModeInfo(currMB, &se, dataPart);
01967
01968 no_bits += se.len;
01969 }
01970 }
01971
01972
01973 if (currMB->mb_type != 0 || ((currMB->cbp != 0 || currSlice->cmp_cbp[1] !=0 || currSlice->cmp_cbp[2]!=0 )))
01974 {
01975 se.value1 = mb_type;
01976 se.value2 = 0;
01977 se.type = SE_MBTYPE;
01978
01979 #if TRACE
01980 snprintf(se.tracestring, TRACESTRING_SIZE, "mb_type (B_SLICE) (%2d,%2d) = %3d",currMB->mb_x, currMB->mb_y, currMB->mb_type);
01981 #endif
01982 currSlice->writeMB_typeInfo(currMB, &se, dataPart);
01983
01984 no_bits += se.len;
01985 }
01986 }
01987
01988 else if (currMB->mb_type != 0 || ((currMB->cbp != 0 || currSlice->cmp_cbp[1] != 0 || currSlice->cmp_cbp[2]!=0 )))
01989 {
01990
01991 se.value1 = p_Img->cod_counter;
01992 se.value2 = 0;
01993 se.type = SE_MBTYPE;
01994
01995 TRACE_SE (se.tracestring, "mb_skip_run");
01996 writeSE_UVLC(&se, dataPart);
01997
01998 no_bits += se.len;
01999
02000
02001 p_Img->cod_counter = 0;
02002
02003
02004 if(currSlice->MbaffFrameFlag && !skip)
02005 {
02006 if(WriteFrameFieldMBInHeader)
02007 {
02008 se.value1 = currMB->mb_field;
02009 se.type = SE_MBTYPE;
02010
02011 TRACE_SE(se.tracestring, "mb_field_decoding_flag");
02012 writeSE_Flag (&se, dataPart);
02013
02014 no_bits += se.len;
02015 }
02016 }
02017
02018 se.value1 = MBType2Value (currMB);
02019
02020 se.type = SE_MBTYPE;
02021 se.value2 = 0;
02022
02023 #if TRACE
02024 snprintf(se.tracestring, TRACESTRING_SIZE, "mb_type (B_SLICE) (%2d,%2d) = %3d",currMB->mb_x, currMB->mb_y, currMB->mb_type);
02025 #endif
02026
02027 currSlice->writeMB_typeInfo(currMB, &se, dataPart);
02028
02029 no_bits += se.len;
02030 }
02031 else
02032 {
02033
02034 ++(p_Img->cod_counter);
02035
02036 currMB->skip_flag = 1;
02037
02038 reset_mb_nz_coeff(p_Img, currMB->mbAddrX);
02039
02040 if(FmoGetNextMBNr(p_Img, p_Img->current_mb_nr) == -1 && p_Img->cod_counter > 0)
02041 {
02042
02043 se.value1 = p_Img->cod_counter;
02044 se.value2 = 0;
02045 se.type = SE_MBTYPE;
02046
02047 TRACE_SE(se.tracestring, "mb_skip_run");
02048 writeSE_UVLC(&se, dataPart);
02049
02050 no_bits += se.len;
02051
02052
02053 p_Img->cod_counter = 0;
02054 }
02055 }
02056 mbBits->mb_mode = mbBits->mb_mode + (unsigned short) no_bits;;
02057
02058
02059 currMB->NoMbPartLessThan8x8Flag = (currMB->mb_type == 0 && !(p_Img->active_sps->direct_8x8_inference_flag))? FALSE : TRUE;
02060
02061 if (currMB->mb_type == IPCM)
02062 {
02063 no_bits += writeIPCMData(currMB, &(currSlice->partArr[partMap[SE_LUM_DC_INTRA]]));
02064 return no_bits;
02065 }
02066
02067 dataPart = &(currSlice->partArr[partMap[SE_MBTYPE]]);
02068
02069
02070 if (currMB->mb_type == P8x8)
02071 {
02072 for (i=0; i<4; ++i)
02073 {
02074 se.value1 = B8Mode2Value (currSlice, currMB->b8x8[i].mode, currMB->b8x8[i].pdir);
02075 se.value2 = 0;
02076 se.type = SE_MBTYPE;
02077 #if TRACE
02078 snprintf(se.tracestring, TRACESTRING_SIZE, "8x8 mode/pdir(%2d) = %3d/%d", i, currMB->b8x8[i].mode, currMB->b8x8[i].pdir);
02079 #endif
02080 currSlice->writeB8_typeInfo (&se, dataPart);
02081 mbBits->mb_mode = mbBits->mb_mode + (unsigned short) se.len;;
02082 no_bits += se.len;
02083
02084
02085 currMB->NoMbPartLessThan8x8Flag &= (Boolean) ((currMB->b8x8[i].mode == 0 && p_Img->active_sps->direct_8x8_inference_flag)
02086 || (currMB->b8x8[i].mode ==4));
02087 }
02088
02089 no_bits += currSlice->writeMotionInfo2NAL (currMB);
02090 }
02091
02092
02093
02094
02095 if ((currMB->mb_type == I8MB || currMB->mb_type == I4MB) && p_Inp->Transform8x8Mode)
02096 {
02097 se.value1 = currMB->luma_transform_size_8x8_flag;
02098 se.type = SE_MBTYPE;
02099
02100 #if TRACE
02101 snprintf(se.tracestring, TRACESTRING_SIZE, "transform_size_8x8_flag = %3d", currMB->luma_transform_size_8x8_flag);
02102 #endif
02103 currSlice->writeMB_transform_size(currMB, &se, dataPart);
02104
02105 mbBits->mb_mode = mbBits->mb_mode + (unsigned short) se.len;;
02106 no_bits += se.len;
02107 }
02108
02109
02110 no_bits += writeIntraModes(currMB);
02111
02112 if (currMB->IntraChromaPredModeFlag && ((p_Img->active_sps->chroma_format_idc != YUV400) && (p_Img->active_sps->chroma_format_idc != YUV444)))
02113 no_bits += writeChromaIntraPredMode(currMB);
02114 else if(!rdopt)
02115 {
02116 currMB->c_ipred_mode = DC_PRED_8;
02117 }
02118
02119
02120
02121
02122
02123
02124 if (currMB->mb_type !=0 && currMB->mb_type != P8x8 && (!IS_INTRA(currMB)))
02125 {
02126 no_bits += currSlice->writeMotionInfo2NAL (currMB);
02127 }
02128
02129 if ((currMB->mb_type!=0) || ((currMB->cbp!=0 || currSlice->cmp_cbp[1]!=0 || currSlice->cmp_cbp[2]!=0)))
02130 {
02131 *coeff_rate = writeCBPandDquant (currMB);
02132 *coeff_rate += currSlice->writeCoeff16x16 (currMB, PLANE_Y);
02133
02134 if ((p_Img->active_sps->chroma_format_idc != YUV400) && (!IS_INDEPENDENT(p_Inp)))
02135 {
02136 if(p_Img->active_sps->chroma_format_idc == YUV444)
02137 {
02138 *coeff_rate += currSlice->writeCoeff16x16(currMB, PLANE_U);
02139 *coeff_rate += currSlice->writeCoeff16x16(currMB, PLANE_V);
02140 }
02141 else
02142 *coeff_rate += writeChromaCoeff (currMB);
02143 }
02144 no_bits += *coeff_rate;
02145 }
02146
02147 return no_bits;
02148 }
02149
02150
02151
02152
02153
02154
02155
02156
02157
02158
02159
02160
02161
02162 int writeMBLayerPSlice (Macroblock *currMB, int rdopt, int *coeff_rate)
02163 {
02164 ImageParameters *p_Img = currMB->p_Img;
02165 InputParameters *p_Inp = currMB->p_Inp;
02166 Slice *currSlice = currMB->p_slice;
02167
02168 int i;
02169 int mb_nr = currMB->mbAddrX;
02170 Macroblock *prevMB = currMB->PrevMB;
02171 int prev_mb_nr = (NULL == prevMB) ? -1 : prevMB->mbAddrX;
02172
02173 SyntaxElement se;
02174 BitCounter *mbBits = &currMB->bits;
02175 const int* partMap = assignSE2partition[currSlice->partition_mode];
02176
02177 DataPartition* dataPart = &(currSlice->partArr[partMap[SE_MBTYPE]]);
02178
02179 int no_bits = 0;
02180 int skip = currMB->mb_type ? 0 : 1;
02181 int prevMbSkipped = 0;
02182 int WriteFrameFieldMBInHeader = 0;
02183
02184 if (currSlice->MbaffFrameFlag)
02185 {
02186 if ((mb_nr & 0x01) == 0)
02187 {
02188 WriteFrameFieldMBInHeader = 1;
02189
02190 prevMbSkipped = 0;
02191 }
02192 else
02193 {
02194 if (prevMB->mb_type ? 0 : 1)
02195 {
02196 WriteFrameFieldMBInHeader = 1;
02197 }
02198
02199 prevMbSkipped = p_Img->mb_data[prev_mb_nr].skip_flag;
02200 }
02201 }
02202 currMB->IntraChromaPredModeFlag = (char) IS_INTRA(currMB);
02203
02204 if (currSlice->symbol_mode == CABAC)
02205 {
02206 int mb_type = MBType2Value (currMB);
02207 if (currSlice->MbaffFrameFlag && ((p_Img->current_mb_nr & 0x01) == 0 || prevMbSkipped))
02208 {
02209 byte mb_field_tmp = currMB->mb_field;
02210 currMB->mb_field = field_flag_inference(currMB);
02211 CheckAvailabilityOfNeighborsCABAC(currMB);
02212 currMB->mb_field = mb_field_tmp;
02213 }
02214
02215
02216 se.value1 = mb_type;
02217 se.value2 = currMB->cbp;
02218 se.type = SE_MBTYPE;
02219
02220 TRACE_SE (se.tracestring, "mb_skip_flag");
02221 currSlice->writeMB_Skip(currMB, &se, dataPart);
02222
02223 no_bits += se.len;
02224
02225 CheckAvailabilityOfNeighborsCABAC(currMB);
02226
02227
02228 if(currSlice->MbaffFrameFlag && !skip)
02229 {
02230 if(WriteFrameFieldMBInHeader)
02231 {
02232 se.value1 = currMB->mb_field;
02233 se.value2 = 0;
02234 se.type = SE_MBTYPE;
02235
02236 TRACE_SE(se.tracestring, "mb_field_decoding_flag");
02237 currSlice->writeFieldModeInfo(currMB, &se, dataPart);
02238
02239 no_bits += se.len;
02240 }
02241 }
02242
02243
02244 if (currMB->mb_type != 0)
02245 {
02246 se.value1 = mb_type;
02247 se.value2 = 0;
02248 se.type = SE_MBTYPE;
02249
02250 #if TRACE
02251 snprintf(se.tracestring, TRACESTRING_SIZE, "mb_type (P_SLICE) (%2d,%2d) = %3d",currMB->mb_x, currMB->mb_y, currMB->mb_type);
02252 #endif
02253 currSlice->writeMB_typeInfo(currMB, &se, dataPart);
02254
02255 no_bits += se.len;
02256 }
02257 }
02258
02259 else if (currMB->mb_type != 0)
02260 {
02261
02262 se.value1 = p_Img->cod_counter;
02263 se.value2 = 0;
02264 se.type = SE_MBTYPE;
02265
02266 TRACE_SE (se.tracestring, "mb_skip_run");
02267 writeSE_UVLC(&se, dataPart);
02268
02269 no_bits += se.len;
02270
02271
02272 p_Img->cod_counter = 0;
02273
02274
02275 if(currSlice->MbaffFrameFlag && !skip)
02276 {
02277 if(WriteFrameFieldMBInHeader)
02278 {
02279 se.value1 = currMB->mb_field;
02280 se.type = SE_MBTYPE;
02281
02282 TRACE_SE(se.tracestring, "mb_field_decoding_flag");
02283 writeSE_Flag (&se, dataPart);
02284
02285 no_bits += se.len;
02286 }
02287 }
02288
02289 se.value1 = MBType2Value (currMB);
02290 se.value1--;
02291
02292 se.type = SE_MBTYPE;
02293 se.value2 = 0;
02294
02295 #if TRACE
02296 snprintf(se.tracestring, TRACESTRING_SIZE, "mb_type (P_SLICE) (%2d,%2d) = %3d",currMB->mb_x, currMB->mb_y, currMB->mb_type);
02297 #endif
02298
02299 currSlice->writeMB_typeInfo(currMB, &se, dataPart);
02300
02301 no_bits += se.len;
02302 }
02303 else
02304 {
02305
02306 ++(p_Img->cod_counter);
02307
02308 currMB->skip_flag = 1;
02309
02310 reset_mb_nz_coeff(p_Img, currMB->mbAddrX);
02311
02312 if(FmoGetNextMBNr(p_Img, p_Img->current_mb_nr) == -1 && p_Img->cod_counter > 0)
02313 {
02314
02315 se.value1 = p_Img->cod_counter;
02316 se.value2 = 0;
02317 se.type = SE_MBTYPE;
02318
02319 TRACE_SE(se.tracestring, "mb_skip_run");
02320 writeSE_UVLC(&se, dataPart);
02321
02322 no_bits += se.len;
02323
02324
02325 p_Img->cod_counter = 0;
02326 }
02327 }
02328 mbBits->mb_mode = mbBits->mb_mode + (unsigned short) no_bits;;
02329
02330
02331 currMB->NoMbPartLessThan8x8Flag = TRUE;
02332
02333 if (currMB->mb_type == IPCM)
02334 {
02335 no_bits += writeIPCMData(currMB, &(currSlice->partArr[partMap[SE_LUM_DC_INTRA]]));
02336 return no_bits;
02337 }
02338
02339 dataPart = &(currSlice->partArr[partMap[SE_MBTYPE]]);
02340
02341
02342 if (currMB->mb_type == P8x8)
02343 {
02344 for (i=0; i<4; ++i)
02345 {
02346 se.value1 = B8Mode2Value (currSlice, currMB->b8x8[i].mode, currMB->b8x8[i].pdir);
02347 se.value2 = 0;
02348 se.type = SE_MBTYPE;
02349 #if TRACE
02350 snprintf(se.tracestring, TRACESTRING_SIZE, "8x8 mode/pdir(%2d) = %3d/%d", i, currMB->b8x8[i].mode, currMB->b8x8[i].pdir);
02351 #endif
02352 currSlice->writeB8_typeInfo (&se, dataPart);
02353 mbBits->mb_mode = mbBits->mb_mode + (unsigned short) se.len;;
02354 no_bits += se.len;
02355
02356
02357 currMB->NoMbPartLessThan8x8Flag &= (Boolean) ((currMB->b8x8[i].mode == 0 && p_Img->active_sps->direct_8x8_inference_flag)
02358 || (currMB->b8x8[i].mode ==4));
02359 }
02360
02361 no_bits += currSlice->writeMotionInfo2NAL (currMB);
02362 }
02363
02364
02365
02366
02367 if ((currMB->mb_type == I8MB || currMB->mb_type == I4MB) && p_Inp->Transform8x8Mode)
02368 {
02369 se.value1 = currMB->luma_transform_size_8x8_flag;
02370 se.type = SE_MBTYPE;
02371
02372 #if TRACE
02373 snprintf(se.tracestring, TRACESTRING_SIZE, "transform_size_8x8_flag = %3d", currMB->luma_transform_size_8x8_flag);
02374 #endif
02375 currSlice->writeMB_transform_size(currMB, &se, dataPart);
02376
02377 mbBits->mb_mode = mbBits->mb_mode + (unsigned short) se.len;;
02378 no_bits += se.len;
02379 }
02380
02381
02382 no_bits += writeIntraModes(currMB);
02383
02384 if (currMB->IntraChromaPredModeFlag && ((p_Img->active_sps->chroma_format_idc != YUV400) && (p_Img->active_sps->chroma_format_idc != YUV444)))
02385 no_bits += writeChromaIntraPredMode(currMB);
02386 else if(!rdopt)
02387 {
02388 currMB->c_ipred_mode = DC_PRED_8;
02389 }
02390
02391
02392
02393
02394
02395
02396 if (currMB->mb_type !=0 && currMB->mb_type != P8x8 && (!IS_INTRA(currMB)))
02397 {
02398 no_bits += currSlice->writeMotionInfo2NAL (currMB);
02399 }
02400
02401 if (currMB->mb_type!=0)
02402 {
02403 *coeff_rate = writeCBPandDquant (currMB);
02404 *coeff_rate += currSlice->writeCoeff16x16 (currMB, PLANE_Y);
02405
02406 if ((p_Img->active_sps->chroma_format_idc != YUV400) && (!IS_INDEPENDENT(p_Inp)))
02407 {
02408 if(p_Img->active_sps->chroma_format_idc == YUV444)
02409 {
02410 *coeff_rate += currSlice->writeCoeff16x16(currMB, PLANE_U);
02411 *coeff_rate += currSlice->writeCoeff16x16(currMB, PLANE_V);
02412 }
02413 else
02414 *coeff_rate += writeChromaCoeff (currMB);
02415 }
02416 no_bits += *coeff_rate;
02417 }
02418
02419 return no_bits;
02420 }
02421
02422
02423 int writeMBLayerISlice (Macroblock *currMB, int rdopt, int *coeff_rate)
02424 {
02425 ImageParameters *p_Img = currMB->p_Img;
02426 InputParameters *p_Inp = currMB->p_Inp;
02427 Slice *currSlice = currMB->p_slice;
02428
02429 int mb_nr = currMB->mbAddrX;
02430 Macroblock *prevMB = currMB->PrevMB;
02431
02432 SyntaxElement se;
02433 BitCounter *mbBits = &currMB->bits;
02434 const int* partMap = assignSE2partition[currSlice->partition_mode];
02435
02436 DataPartition* dataPart = &(currSlice->partArr[partMap[SE_MBTYPE]]);
02437
02438 int no_bits = 0;
02439 int WriteFrameFieldMBInHeader = 0;
02440
02441 if (currSlice->MbaffFrameFlag)
02442 {
02443 if ((mb_nr & 0x01) == 0)
02444 {
02445 WriteFrameFieldMBInHeader = 1;
02446 }
02447 else
02448 {
02449 if (prevMB->mb_type ? 0: 1)
02450 {
02451 WriteFrameFieldMBInHeader = 1;
02452 }
02453 }
02454
02455
02456 if(WriteFrameFieldMBInHeader)
02457 {
02458 se.value1 = currMB->mb_field;
02459 se.value2 = 0;
02460 se.type = SE_MBTYPE;
02461
02462 TRACE_SE (se.tracestring, "mb_field_decoding_flag");
02463 currSlice->writeFieldModeInfo(currMB, &se, dataPart);
02464
02465 no_bits += se.len;
02466 }
02467 }
02468 currMB->IntraChromaPredModeFlag = (char) IS_INTRA(currMB);
02469
02470
02471
02472 se.value1 = MBType2Value (currMB);
02473 se.value2 = 0;
02474 se.type = SE_MBTYPE;
02475
02476 #if TRACE
02477 snprintf(se.tracestring, TRACESTRING_SIZE, "mb_type (I_SLICE) (%2d,%2d) = %3d",currMB->mb_x, currMB->mb_y, currMB->mb_type);
02478 #endif
02479
02480 currSlice->writeMB_typeInfo (currMB, &se, dataPart);
02481
02482 no_bits += se.len;
02483
02484 mbBits->mb_mode = mbBits->mb_mode + (unsigned short) no_bits;
02485
02486
02487 currMB->NoMbPartLessThan8x8Flag = TRUE;
02488
02489 if (currMB->mb_type == IPCM)
02490 {
02491 no_bits += writeIPCMData(currMB, &(currSlice->partArr[partMap[SE_LUM_DC_INTRA]]));
02492 return no_bits;
02493 }
02494
02495 dataPart = &(currSlice->partArr[partMap[SE_MBTYPE]]);
02496
02497
02498
02499
02500 if ((currMB->mb_type == I8MB || currMB->mb_type == I4MB) && p_Inp->Transform8x8Mode)
02501 {
02502 se.value1 = currMB->luma_transform_size_8x8_flag;
02503 se.type = SE_MBTYPE;
02504
02505 #if TRACE
02506 snprintf(se.tracestring, TRACESTRING_SIZE, "transform_size_8x8_flag = %3d", currMB->luma_transform_size_8x8_flag);
02507 #endif
02508 currSlice->writeMB_transform_size(currMB, &se, dataPart);
02509
02510 mbBits->mb_mode = mbBits->mb_mode + (unsigned short) se.len;;
02511 no_bits += se.len;
02512 }
02513
02514
02515 no_bits += writeIntraModes(currMB);
02516
02517 if (currMB->IntraChromaPredModeFlag && ((p_Img->active_sps->chroma_format_idc != YUV400) && (p_Img->active_sps->chroma_format_idc != YUV444)))
02518 no_bits += writeChromaIntraPredMode(currMB);
02519 else if(!rdopt)
02520 {
02521 currMB->c_ipred_mode = DC_PRED_8;
02522 }
02523
02524
02525
02526
02527
02528 *coeff_rate = writeCBPandDquant (currMB);
02529 *coeff_rate += currSlice->writeCoeff16x16 (currMB, PLANE_Y);
02530
02531 if ((p_Img->active_sps->chroma_format_idc != YUV400) && (!IS_INDEPENDENT(p_Inp)))
02532 {
02533 if(p_Img->active_sps->chroma_format_idc == YUV444)
02534 {
02535 *coeff_rate += currSlice->writeCoeff16x16(currMB, PLANE_U);
02536 *coeff_rate += currSlice->writeCoeff16x16(currMB, PLANE_V);
02537 }
02538 else
02539 *coeff_rate += writeChromaCoeff (currMB);
02540 }
02541
02542 no_bits += *coeff_rate;
02543
02544 return no_bits;
02545 }
02546
02547 void write_terminating_bit (Slice *currSlice, short bit)
02548 {
02549 const int* partMap = assignSE2partition[currSlice->partition_mode];
02550
02551 DataPartition* dataPart = &(currSlice->partArr[partMap[SE_MBTYPE]]);
02552 dataPart->bitstream->write_flag = 1;
02553
02554 biari_encode_symbol_final(&(dataPart->ee_cabac), bit);
02555 #if TRACE
02556 fprintf (p_Enc->p_trace, " CABAC terminating bit = %d\n",bit);
02557 #endif
02558 }
02559
02560
02561
02562
02563
02564
02565
02566
02567 int writeChromaIntraPredMode(Macroblock* currMB)
02568 {
02569 Slice* currSlice = currMB->p_slice;
02570 SyntaxElement se;
02571 const int* partMap = assignSE2partition[currSlice->partition_mode];
02572 DataPartition* dataPart = &(currSlice->partArr[partMap[SE_INTRAPREDMODE]]);
02573 int rate = 0;
02574
02575
02576
02577
02578 se.value1 = currMB->c_ipred_mode;
02579 se.value2 = 0;
02580 se.type = SE_INTRAPREDMODE;
02581
02582 TRACE_SE(se.tracestring, "intra_chroma_pred_mode");
02583 currSlice->writeCIPredMode(currMB, &se, dataPart);
02584
02585 currMB->bits.mb_uv_coeff += se.len;
02586 rate += se.len;
02587
02588 return rate;
02589 }
02590
02591
02592
02593
02594
02595
02596
02597
02598
02599 void write_macroblock (Macroblock* currMB, int eos_bit, Boolean prev_recode_mb)
02600 {
02601 Slice *currSlice = currMB->p_slice;
02602 ImageParameters *p_Img = currMB->p_Img;
02603 InputParameters *p_Inp = currMB->p_Inp;
02604 BitCounter *mbBits = &currMB->bits;
02605 int i;
02606
02607
02608 #if TRACE
02609 if ( prev_recode_mb == FALSE )
02610 {
02611 for (i=0; i<currSlice->max_part_nr; ++i)
02612 {
02613 currSlice->partArr[i].bitstream->trace_enabled = TRUE;
02614 }
02615 }
02616 #endif
02617
02618
02619 if(p_Inp->UseConstrainedIntraPred && (currSlice->slice_type==P_SLICE || currSlice->slice_type==B_SLICE))
02620 {
02621 p_Img->intra_block[currMB->mbAddrX] = IS_INTRA(currMB);
02622 }
02623
02624
02625 if (currMB->mbAddrX == 0)
02626 p_Img->intras=0;
02627
02628 if (IS_INTRA(currMB))
02629 ++p_Img->intras;
02630
02631
02632 if (currSlice->symbol_mode==CABAC && currMB->mbAddrX != currSlice->start_mb_nr && eos_bit)
02633 {
02634 write_terminating_bit (currSlice, 0);
02635 }
02636
02637 #if TRACE
02638
02639 if (p_Enc->p_trace)
02640 {
02641 fprintf(p_Enc->p_trace, "\n*********** Pic: %i (I/P) MB: %i Slice: %i **********\n\n", p_Img->frame_no, currMB->mbAddrX, currSlice->slice_nr);
02642 }
02643 #endif
02644
02645 p_Img->cabac_encoding = 1;
02646
02647 currMB->write_mb = TRUE;
02648
02649 currSlice->write_MB_layer (currMB, 0, &i);
02650
02651 if (!((currMB->mb_type !=0 ) || ((currSlice->slice_type==B_SLICE) && currMB->cbp != 0) ))
02652 {
02653 reset_mb_nz_coeff(p_Img, currMB->mbAddrX);
02654 }
02655
02656
02657 mbBits->mb_total = mbBits->mb_mode
02658 + mbBits->mb_y_coeff
02659 + mbBits->mb_inter
02660 + mbBits->mb_cbp
02661 + mbBits->mb_delta_quant
02662 + mbBits->mb_uv_coeff
02663 + mbBits->mb_cb_coeff
02664 + mbBits->mb_cr_coeff;
02665
02666 if ( p_Inp->RCEnable )
02667 rc_update_mb_stats(currMB);
02668
02669
02670 ++(p_Img->NumberofCodedMacroBlocks);
02671
02672 p_Img->p_Stats->bit_slice += mbBits->mb_total;
02673
02674 p_Img->cabac_encoding = 0;
02675 }
02676
02677
02678
02679
02680
02681
02682
02683
02684 int writeReferenceFrame (Macroblock* currMB, int i, int j, int list_idx, int ref)
02685 {
02686 BitCounter *mbBits = &currMB->bits;
02687 Slice *currSlice = currMB->p_slice;
02688 const int* partMap = assignSE2partition[currSlice->partition_mode];
02689 DataPartition* dataPart = &(currSlice->partArr[partMap[SE_REFFRAME]]);
02690 int list = list_idx + currMB->list_offset;
02691 SyntaxElement se;
02692
02693 se.value1 = ref;
02694 se.type = SE_REFFRAME;
02695 se.value2 = list_idx;
02696
02697 currMB->subblock_x = (short) (i << 2);
02698 currMB->subblock_y = (short) (j << 2);
02699
02700 #if TRACE
02701 if (fwd_flag)
02702 snprintf(se.tracestring, TRACESTRING_SIZE, "ref_idx_l0 = %d", se.value1);
02703 else
02704 snprintf(se.tracestring, TRACESTRING_SIZE, "ref_idx_l1 = %d", se.value1);
02705 #endif
02706
02707 currSlice->writeRefFrame[list](&se, dataPart);
02708
02709 mbBits->mb_inter = mbBits->mb_inter + (unsigned short) se.len;
02710
02711 return (se.len);
02712 }
02713
02714
02715
02716
02717
02718
02719
02720
02721 int writeMotionVector8x8 (Macroblock *currMB,
02722 int i0,
02723 int j0,
02724 int i1,
02725 int j1,
02726 int refframe,
02727 int list_idx,
02728 int mv_mode,
02729 short bipred_me
02730 )
02731 {
02732 Slice *currSlice = currMB->p_slice;
02733 ImageParameters *p_Img = currMB->p_Img;
02734 InputParameters *p_Inp = currMB->p_Inp;
02735
02736 PixelPos block[4];
02737 int i, j, k, l, m;
02738 int curr_mvd;
02739
02740 int rate = 0;
02741 int step_h = part_size[mv_mode][0];
02742 int step_v = part_size[mv_mode][1];
02743 SyntaxElement se;
02744 BitCounter *mbBits = &currMB->bits;
02745
02746 const int* partMap = assignSE2partition[currSlice->partition_mode];
02747 DataPartition* dataPart = &(currSlice->partArr[partMap[SE_MVD]]);
02748 int refindex = refframe;
02749
02750 short*** all_mv = currSlice->all_mv[list_idx][refindex][mv_mode];
02751 short *cur_mv;
02752 short predMV[2];
02753 PicMotionParams *motion = &p_Img->enc_picture->motion;
02754 short ***final_mv = &motion->mv[list_idx][currMB->block_y];
02755 short (*currMB_mvd)[4][2] = currMB->mvd[list_idx];
02756 se.type = SE_MVD;
02757
02758 if (bipred_me && is_bipred_enabled(p_Inp, mv_mode) && refindex == 0)
02759 all_mv = currSlice->bipred_mv[bipred_me - 1][list_idx][refindex][mv_mode];
02760
02761 for (j = j0; j < j1; j += step_v)
02762 {
02763 currMB->subblock_y = (short) (j << 2);
02764 for (i = i0; i < i1; i += step_h)
02765 {
02766 currMB->subblock_x = (short) (i << 2);
02767
02768 if (currMB->write_mb)
02769 cur_mv = final_mv[j][currMB->block_x + i];
02770 else
02771 cur_mv = all_mv[j][i];
02772
02773
02774 get_neighbors(currMB, block, i<<2, j<<2, step_h<<2);
02775 currMB->GetMVPredictor (currMB, block, predMV, (short) refindex, motion->ref_idx[list_idx], motion->mv[list_idx], (i<<2), (j<<2), step_h<<2, step_v<<2);
02776
02777
02778 for (k=0; k<2; ++k)
02779 {
02780 curr_mvd = cur_mv[k] - predMV[k];
02781
02782
02783 for (l = j; l < j + step_v; ++l)
02784 {
02785 for (m = i; m < i + step_h; ++m)
02786 {
02787 currMB_mvd[l][m][k] = (short) curr_mvd;
02788 }
02789 }
02790
02791 se.value1 = curr_mvd;
02792 se.value2 = (k << 1) + list_idx;
02793 #if TRACE
02794 snprintf(se.tracestring, TRACESTRING_SIZE, "mvd_l%d (%d) = %3d (org_mv %3d pred_mv %3d)",list_idx, k, curr_mvd, cur_mv[k], predMV[k]);
02795 #endif
02796 currSlice->writeMVD (currMB, &se, dataPart);
02797
02798 rate += se.len;
02799 }
02800 }
02801 }
02802
02803 mbBits->mb_inter = mbBits->mb_inter + (unsigned short) rate;
02804 return rate;
02805 }
02806
02807
02808
02809
02810
02811
02812
02813
02814 int write_bslice_motion_info_to_NAL (Macroblock* currMB)
02815 {
02816 int no_bits = 0;
02817
02818 if (currMB->mb_type != I4MB && currMB->mb_type != I16MB && currMB->mb_type != I8MB && currMB->mb_type != 0)
02819 {
02820 Slice* currSlice = currMB->p_slice;
02821 ImageParameters *p_Img = currSlice->p_Img;
02822 PicMotionParams *motion = &p_Img->enc_picture->motion;
02823 int k, j0, i0;
02824
02825 int step_h0 = (block_size[(currMB->mb_type == P8x8) ? 4 : currMB->mb_type][0] >> 2);
02826 int step_v0 = (block_size[(currMB->mb_type == P8x8) ? 4 : currMB->mb_type][1] >> 2);
02827 char *cref_idx;
02828
02829
02830
02831 {
02832 for (j0 = 0; j0 < 4; j0 += step_v0)
02833 {
02834 cref_idx = &motion->ref_idx[LIST_0][currMB->block_y + j0][currMB->block_x];
02835 for (i0 = 0; i0 < 4; i0 += step_h0)
02836 {
02837 k = j0 + (i0 >> 1);
02838 if (currMB->b8x8[k].mode !=0 && (currMB->b8x8[k].pdir == 0 || currMB->b8x8[k].pdir == 2))
02839 {
02840 no_bits += writeReferenceFrame (currMB, i0, j0, LIST_0, cref_idx[i0]);
02841 }
02842 }
02843 }
02844 }
02845
02846
02847 {
02848 for (j0 = 0; j0 < 4; j0 += step_v0)
02849 {
02850 cref_idx = &motion->ref_idx[LIST_1][currMB->block_y + j0][currMB->block_x];
02851 for (i0 = 0; i0 < 4; i0 += step_h0)
02852 {
02853 k = j0 + (i0 >> 1);
02854 if (currMB->b8x8[k].mode !=0 && (currMB->b8x8[k].pdir == 1 || currMB->b8x8[k].pdir == 2))
02855 {
02856 no_bits += writeReferenceFrame (currMB, i0, j0, LIST_1, cref_idx[i0]);
02857 }
02858 }
02859 }
02860 }
02861
02862
02863 for (j0=0; j0<4; j0+=step_v0)
02864 {
02865 cref_idx = &motion->ref_idx[LIST_0][currMB->block_y + j0][currMB->block_x];
02866 for (i0=0; i0<4; i0+=step_h0)
02867 {
02868 k = j0 + (i0 >> 1);
02869 if (currMB->b8x8[k].mode !=0 && (currMB->b8x8[k].pdir == 0 || currMB->b8x8[k].pdir == 2))
02870 {
02871 no_bits += writeMotionVector8x8 (currMB, i0, j0, i0 + step_h0, j0 + step_v0, cref_idx[i0], LIST_0, currMB->b8x8[k].mode, currMB->b8x8[k].bipred);
02872 }
02873 }
02874 }
02875
02876
02877
02878 for (j0=0; j0<4; j0+=step_v0)
02879 {
02880 cref_idx = &motion->ref_idx[LIST_1][currMB->block_y + j0][currMB->block_x];
02881 for (i0 = 0; i0 < 4; i0 += step_h0)
02882 {
02883 k=j0+(i0 >> 1);
02884 if (currMB->b8x8[k].mode !=0 && (currMB->b8x8[k].pdir == 1 || currMB->b8x8[k].pdir == 2))
02885 {
02886 no_bits += writeMotionVector8x8 (currMB, i0, j0, i0+step_h0, j0+step_v0, cref_idx[i0], LIST_1, currMB->b8x8[k].mode, currMB->b8x8[k].bipred);
02887 }
02888 }
02889 }
02890 }
02891
02892 return no_bits;
02893 }
02894
02895
02896
02897
02898
02899
02900
02901
02902 int write_pslice_motion_info_to_NAL (Macroblock* currMB)
02903 {
02904 int no_bits = 0;
02905
02906 if (currMB->mb_type != I4MB && currMB->mb_type != I16MB && currMB->mb_type != I8MB && currMB->mb_type != 0)
02907 {
02908 Slice* currSlice = currMB->p_slice;
02909 ImageParameters *p_Img = currSlice->p_Img;
02910 PicMotionParams *motion = &p_Img->enc_picture->motion;
02911 int k, j0, i0;
02912 int step_h0 = (block_size[(currMB->mb_type == P8x8) ? 4 : currMB->mb_type][0] >> 2);
02913 int step_v0 = (block_size[(currMB->mb_type == P8x8) ? 4 : currMB->mb_type][1] >> 2);
02914 char *cref_idx;
02915
02916
02917 if ((currMB->mb_type != P8x8) || !ZeroRef (currMB) || currSlice->symbol_mode==CABAC)
02918 {
02919 for (j0 = 0; j0 < 4; j0 += step_v0)
02920 {
02921 cref_idx = &motion->ref_idx[LIST_0][currMB->block_y + j0][currMB->block_x];
02922 for (i0 = 0; i0 < 4; i0 += step_h0)
02923 {
02924 k = j0 + (i0 >> 1);
02925 if (currMB->b8x8[k].mode !=0 && (currMB->b8x8[k].pdir == 0 || currMB->b8x8[k].pdir == 2))
02926 {
02927 no_bits += writeReferenceFrame (currMB, i0, j0, LIST_0, cref_idx[i0]);
02928 }
02929 }
02930 }
02931 }
02932
02933
02934 for (j0=0; j0<4; j0+=step_v0)
02935 {
02936 cref_idx = &motion->ref_idx[LIST_0][currMB->block_y + j0][currMB->block_x];
02937 for (i0=0; i0<4; i0+=step_h0)
02938 {
02939 k = j0 + (i0 >> 1);
02940 if (currMB->b8x8[k].mode !=0 && (currMB->b8x8[k].pdir == 0 || currMB->b8x8[k].pdir == 2))
02941 {
02942 no_bits += writeMotionVector8x8 (currMB, i0, j0, i0 + step_h0, j0 + step_v0, cref_idx[i0], LIST_0, currMB->b8x8[k].mode, currMB->b8x8[k].bipred);
02943 }
02944 }
02945 }
02946 }
02947
02948 return no_bits;
02949 }
02950
02951
02952
02953
02954
02955
02956
02957 int writeMotionInfo2NAL (Macroblock* currMB)
02958 {
02959 int no_bits = 0;
02960
02961 if (!IS_INTRA (currMB))
02962 {
02963 Slice* currSlice = currMB->p_slice;
02964 ImageParameters *p_Img = currSlice->p_Img;
02965 PicMotionParams *motion = &p_Img->enc_picture->motion;
02966 int k, j0, i0;
02967 int bframe = (currSlice->slice_type == B_SLICE);
02968 int step_h0 = (block_size[(currMB->mb_type == P8x8) ? 4 : currMB->mb_type][0] >> 2);
02969 int step_v0 = (block_size[(currMB->mb_type == P8x8) ? 4 : currMB->mb_type][1] >> 2);
02970 char *cref_idx;
02971
02972
02973 if ((currMB->mb_type != P8x8) || !ZeroRef (currMB) || currSlice->symbol_mode==CABAC || bframe)
02974 {
02975 for (j0 = 0; j0 < 4; j0 += step_v0)
02976 {
02977 cref_idx = &motion->ref_idx[LIST_0][currMB->block_y + j0][currMB->block_x];
02978 for (i0 = 0; i0 < 4; i0 += step_h0)
02979 {
02980 k = j0 + (i0 >> 1);
02981 if (currMB->b8x8[k].mode !=0 && (currMB->b8x8[k].pdir == 0 || currMB->b8x8[k].pdir == 2))
02982 {
02983 no_bits += writeReferenceFrame (currMB, i0, j0, 1, cref_idx[i0]);
02984 }
02985 }
02986 }
02987
02988 for (j0 = 0; j0 < 4; j0 += step_v0)
02989 {
02990 cref_idx = &motion->ref_idx[LIST_1][currMB->block_y + j0][currMB->block_x];
02991 for (i0 = 0; i0 < 4; i0 += step_h0)
02992 {
02993 k = j0 + (i0 >> 1);
02994 if (currMB->b8x8[k].mode !=0 && (currMB->b8x8[k].pdir == 1 || currMB->b8x8[k].pdir == 2))
02995 {
02996 no_bits += writeReferenceFrame (currMB, i0, j0, 0, cref_idx[i0]);
02997 }
02998 }
02999 }
03000 }
03001
03002
03003 for (j0=0; j0<4; j0+=step_v0)
03004 {
03005 cref_idx = &motion->ref_idx[LIST_0][currMB->block_y + j0][currMB->block_x];
03006 for (i0=0; i0<4; i0+=step_h0)
03007 {
03008 k = j0 + (i0 >> 1);
03009 if (currMB->b8x8[k].mode !=0 && (currMB->b8x8[k].pdir == 0 || currMB->b8x8[k].pdir == 2))
03010 {
03011 no_bits += writeMotionVector8x8 (currMB, i0, j0, i0 + step_h0, j0 + step_v0, cref_idx[i0], LIST_0, currMB->b8x8[k].mode, currMB->b8x8[k].bipred);
03012 }
03013 }
03014 }
03015
03016
03017 if (bframe)
03018 {
03019 for (j0=0; j0<4; j0+=step_v0)
03020 {
03021 cref_idx = &motion->ref_idx[LIST_1][currMB->block_y + j0][currMB->block_x];
03022 for (i0 = 0; i0 < 4; i0 += step_h0)
03023 {
03024 k=j0+(i0 >> 1);
03025 if (currMB->b8x8[k].mode !=0 && (currMB->b8x8[k].pdir == 1 || currMB->b8x8[k].pdir == 2))
03026 {
03027 no_bits += writeMotionVector8x8 (currMB, i0, j0, i0+step_h0, j0+step_v0, cref_idx[i0], LIST_1, currMB->b8x8[k].mode, currMB->b8x8[k].bipred);
03028 }
03029 }
03030 }
03031 }
03032 }
03033 return no_bits;
03034 }
03035
03036
03037
03038
03039
03040
03041
03042
03043
03044 static int writeChromaCoeff (Macroblock* currMB)
03045 {
03046 Slice* currSlice = currMB->p_slice;
03047 ImageParameters *p_Img = currSlice->p_Img;
03048
03049 int rate = 0;
03050 int block_rate = 0;
03051 SyntaxElement se;
03052 const int* partMap = assignSE2partition[currSlice->partition_mode];
03053 int cbp = currMB->cbp;
03054 DataPartition* dataPart;
03055
03056 int level;
03057 int k, uv;
03058 int b8, b4;
03059 int* ACLevel;
03060 int* ACRun;
03061 int* DCLevel;
03062 int* DCRun;
03063
03064 static const int chroma_dc_context[3]=
03065 {
03066 CHROMA_DC, CHROMA_DC_2x4, CHROMA_DC_4x4
03067 };
03068
03069 static const unsigned char chroma_ac_param[3][8][4] =
03070 {
03071 {{ 4, 20, 5, 21},
03072 {36, 52, 37, 53},
03073 { 0, 0, 0, 0},
03074 { 0, 0, 0, 0},
03075 { 0, 0, 0, 0},
03076 { 0, 0, 0, 0},
03077 { 0, 0, 0, 0},
03078 { 0, 0, 0, 0}},
03079 {{ 4, 20, 5, 21},
03080 { 6, 22, 7, 23},
03081 {36, 52, 37, 53},
03082 {38, 54, 39, 55},
03083 { 0, 0, 0, 0},
03084 { 0, 0, 0, 0},
03085 { 0, 0, 0, 0},
03086 { 0, 0, 0, 0}},
03087 {{ 4, 20, 5, 21},
03088 {36, 52, 37, 53},
03089 { 6, 22, 7, 23},
03090 {38, 54, 39, 55},
03091 { 8, 24, 9, 25},
03092 {40, 56, 41, 57},
03093 {10, 26, 11, 27},
03094 {42, 58, 43, 59}}
03095 };
03096
03097 int yuv = p_Img->yuv_format - 1;
03098
03099
03100
03101 if (cbp > 15)
03102 {
03103 if (currSlice->symbol_mode == CAVLC)
03104 {
03105 rate += currSlice->writeCoeff4x4_CAVLC (currMB, CHROMA_DC, 0, 0, 0);
03106 rate += currSlice->writeCoeff4x4_CAVLC (currMB, CHROMA_DC, 0, 0, 1);
03107 }
03108 else
03109 {
03110 currMB->is_intra_block = (byte) IS_INTRA(currMB);
03111 se.type = (currMB->is_intra_block ? SE_CHR_DC_INTRA : SE_CHR_DC_INTER);
03112
03113 dataPart = &(currSlice->partArr[partMap[se.type]]);
03114 se.context = chroma_dc_context[yuv];
03115
03116 for (uv=0; uv < 2; ++uv)
03117 {
03118 p_Img->is_v_block = uv;
03119
03120 DCLevel = currSlice->cofDC[uv+1][0];
03121 DCRun = currSlice->cofDC[uv+1][1];
03122
03123 level = 1;
03124 for (k=0; k <= p_Img->num_cdc_coeff && level != 0; ++k)
03125 {
03126 level = se.value1 = DCLevel[k];
03127 se.value2 = DCRun [k];
03128
03129 #if TRACE
03130 snprintf(se.tracestring, TRACESTRING_SIZE, "DC Chroma %2d: level =%3d run =%2d",k, level, se.value2);
03131 #endif
03132 writeRunLevel_CABAC(currMB, &se, dataPart);
03133
03134 block_rate += se.len;
03135 }
03136 }
03137 currMB->bits.mb_uv_coeff += block_rate;
03138 rate += block_rate;
03139 }
03140 }
03141
03142
03143
03144
03145 uv=-1;
03146 if (cbp >> 4 == 2)
03147 {
03148 if (currSlice->symbol_mode == CAVLC)
03149 {
03150 for (b8=4; b8 < (4 + p_Img->num_blk8x8_uv); b8++)
03151 {
03152 rate += currSlice->writeCoeff4x4_CAVLC (currMB, CHROMA_AC, b8, 0, chroma_ac_param[yuv][b8 - 4][0]);
03153 rate += currSlice->writeCoeff4x4_CAVLC (currMB, CHROMA_AC, b8, 1, chroma_ac_param[yuv][b8 - 4][1]);
03154 rate += currSlice->writeCoeff4x4_CAVLC (currMB, CHROMA_AC, b8, 2, chroma_ac_param[yuv][b8 - 4][2]);
03155 rate += currSlice->writeCoeff4x4_CAVLC (currMB, CHROMA_AC, b8, 3, chroma_ac_param[yuv][b8 - 4][3]);
03156 }
03157 }
03158 else
03159 {
03160 block_rate = 0;
03161 currMB->is_intra_block = (byte) IS_INTRA(currMB);
03162 se.context = CHROMA_AC;
03163 se.type = (currMB->is_intra_block ? SE_CHR_AC_INTRA : SE_CHR_AC_INTER);
03164
03165 dataPart = &(currSlice->partArr[partMap[se.type]]);
03166
03167 for (b8=4; b8 < (4+p_Img->num_blk8x8_uv); b8++)
03168 {
03169 for (b4=0; b4 < 4; b4++)
03170 {
03171 ACLevel = currSlice->cofAC[b8][b4][0];
03172 ACRun = currSlice->cofAC[b8][b4][1];
03173
03174 level=1;
03175 uv++;
03176 p_Img->is_v_block = (uv >= (p_Img->num_blk8x8_uv << 1));
03177
03178 currMB->subblock_y = subblk_offset_y[yuv][b8 - 4][b4];
03179 currMB->subblock_x = subblk_offset_x[yuv][b8 - 4][b4];
03180
03181 for (k=0; k < 16 && level != 0; k++)
03182 {
03183 level = se.value1 = ACLevel[k];
03184 se.value2 = ACRun [k];
03185
03186 #if TRACE
03187 snprintf(se.tracestring, TRACESTRING_SIZE, "AC Chroma %2d: level =%3d run =%2d",k, level, se.value2);
03188 #endif
03189 writeRunLevel_CABAC(currMB, &se, dataPart);
03190 block_rate += se.len;
03191 }
03192 }
03193 }
03194 currMB->bits.mb_uv_coeff += block_rate;
03195 rate += block_rate;
03196 }
03197 }
03198
03199 return rate;
03200 }
03201
03202 #if TRACE
03203 static inline void trace_coeff(SyntaxElement *se, int plane, char *type, int k, int level, int run)
03204 {
03205 if (plane == 0)
03206 snprintf(se->tracestring, TRACESTRING_SIZE, "Luma%s sng(%2d) level =%3d run =%2d", type, k, level,run);
03207 else if (plane == 1)
03208 snprintf(se->tracestring, TRACESTRING_SIZE, "Cb%s sng(%2d) level =%3d run =%2d", type, k, level,run);
03209 else
03210 snprintf(se->tracestring, TRACESTRING_SIZE, "Cr%s sng(%2d) level =%3d run =%2d", type, k, level,run);
03211 }
03212 #endif
03213
03214
03215
03216
03217
03218
03219
03220 int writeCoeff4x4_CABAC (Macroblock* currMB, ColorPlane plane, int b8, int b4, int intra4x4mode)
03221 {
03222 Slice* currSlice = currMB->p_slice;
03223
03224 int rate = 0;
03225 SyntaxElement se;
03226 const int* partMap = assignSE2partition[currSlice->partition_mode];
03227 DataPartition* dataPart;
03228
03229 int level;
03230 int k;
03231 int pl_off=plane<<2;
03232
03233 int *mb_bits_coeff = ((plane==0) ? &currMB->bits.mb_y_coeff : ((plane==1) ? &currMB->bits.mb_cb_coeff : &currMB->bits.mb_cr_coeff));
03234 int* ACLevel = currSlice->cofAC[b8+pl_off][b4][0];
03235 int* ACRun = currSlice->cofAC[b8+pl_off][b4][1];
03236
03237 currMB->subblock_x = ((b8&0x1)==0) ? (((b4&0x1)==0)? 0: 4) : (((b4&0x1)==0)? 8: 12);
03238 currMB->subblock_y = (b8<2) ? ((b4<2) ? 0: 4) : ((b4<2) ? 8: 12);
03239 currMB->is_intra_block = (byte) intra4x4mode;
03240 se.context = ((plane == 0) ? (LUMA_4x4) : ((plane==1) ? CB_4x4 : CR_4x4));
03241
03242
03243 se.type = (intra4x4mode ? SE_LUM_DC_INTRA : SE_LUM_DC_INTER);
03244
03245
03246 dataPart = &(currSlice->partArr[partMap[se.type]]);
03247
03248 level = se.value1 = ACLevel[0];
03249 se.value2 = ACRun [0];
03250
03251 #if TRACE
03252 trace_coeff(&se, plane, "4x4", 0, level, se.value2);
03253 #endif
03254
03255 writeRunLevel_CABAC(currMB, &se, dataPart);
03256 rate += se.len;
03257
03258
03259 se.type = (intra4x4mode ? SE_LUM_AC_INTRA : SE_LUM_AC_INTER);
03260
03261 dataPart = &(currSlice->partArr[partMap[se.type]]);
03262
03263 for(k = 1; k <= 16 && level != 0; k++)
03264 {
03265 level = se.value1 = ACLevel[k];
03266 se.value2 = ACRun [k];
03267 #if TRACE
03268 trace_coeff(&se, plane, "4x4", k, level, se.value2);
03269 #endif
03270 writeRunLevel_CABAC(currMB, &se, dataPart);
03271
03272 rate += se.len;
03273 }
03274
03275 *mb_bits_coeff += rate;
03276
03277 return rate;
03278 }
03279
03280
03281
03282
03283
03284
03285
03286 int writeCoeff8x8_CABAC (Macroblock* currMB, ColorPlane plane, int b8, int intra_mode)
03287 {
03288 Slice* currSlice = currMB->p_slice;
03289
03290 int rate = 0;
03291 SyntaxElement se;
03292 const int* partMap = assignSE2partition[currSlice->partition_mode];
03293 DataPartition* dataPart;
03294
03295 int level;
03296 int k;
03297
03298 int pl_off = plane<<2;
03299 int *mb_bits_coeff = ((plane==0) ? &currMB->bits.mb_y_coeff : ((plane==1) ? &currMB->bits.mb_cb_coeff : &currMB->bits.mb_cr_coeff));
03300 int* ACLevel = currSlice->cofAC[b8+pl_off][0][0];
03301 int* ACRun = currSlice->cofAC[b8+pl_off][0][1];
03302
03303 currMB->subblock_x = ((b8&0x1) == 0) ? 0 : 8;
03304 currMB->subblock_y = (b8 < 2) ? 0 : 8;
03305 currMB->is_intra_block = (byte) intra_mode;
03306
03307 se.context = ((plane==0) ? (LUMA_8x8) : ((plane == 1)? CB_8x8 : CR_8x8));
03308
03309
03310 level = se.value1 = ACLevel[0];
03311 se.value2 = ACRun [0];
03312
03313 se.type = (intra_mode ? SE_LUM_DC_INTRA : SE_LUM_DC_INTER);
03314
03315
03316 dataPart = &(currSlice->partArr[partMap[currSlice->slice_type != B_SLICE ? se.type : SE_BFRAME]]);
03317
03318 #if TRACE
03319 trace_coeff(&se, plane, "8x8", 0, level, se.value2);
03320 #endif
03321
03322 writeRunLevel_CABAC(currMB, &se, dataPart);
03323 rate += se.len;
03324
03325
03326 se.type = (intra_mode ? SE_LUM_AC_INTRA : SE_LUM_AC_INTER);
03327
03328 dataPart = &(currSlice->partArr[partMap[currSlice->slice_type != B_SLICE ? se.type : SE_BFRAME]]);
03329
03330 for(k=1; k<=64 && level !=0; k++)
03331 {
03332 level = se.value1 = ACLevel[k];
03333 se.value2 = ACRun [k];
03334
03335 #if TRACE
03336 trace_coeff(&se, plane, "8x8", k, level, se.value2);
03337 #endif
03338
03339 writeRunLevel_CABAC(currMB, &se, dataPart);
03340 rate += se.len;
03341 }
03342
03343 *mb_bits_coeff += rate;
03344 return rate;
03345 }
03346
03347
03348
03349
03350
03351
03352
03353 int writeCoeff8x8 (Macroblock* currMB, ColorPlane pl, int block8x8, int block_mode, int transform_size_flag)
03354 {
03355 Slice* currSlice = currMB->p_slice;
03356 int rate = 0;
03357 int is_intra = (block_mode == IBLOCK) || (block_mode == I8MB);
03358
03359
03360
03361
03362 if (currSlice->symbol_mode == CAVLC )
03363 {
03364 int block_color_type = ((pl == 0) ? LUMA : ((pl == 1) ? CB : CR));
03365
03366 rate += currSlice->writeCoeff4x4_CAVLC (currMB, block_color_type, block8x8, 0, is_intra);
03367 rate += currSlice->writeCoeff4x4_CAVLC (currMB, block_color_type, block8x8, 1, is_intra);
03368 rate += currSlice->writeCoeff4x4_CAVLC (currMB, block_color_type, block8x8, 2, is_intra);
03369 rate += currSlice->writeCoeff4x4_CAVLC (currMB, block_color_type, block8x8, 3, is_intra);
03370 }
03371 else
03372 {
03373 if(transform_size_flag)
03374 rate += writeCoeff8x8_CABAC (currMB, pl, block8x8, is_intra);
03375 else
03376 {
03377 rate += writeCoeff4x4_CABAC (currMB, pl, block8x8, 0, is_intra);
03378 rate += writeCoeff4x4_CABAC (currMB, pl, block8x8, 1, is_intra);
03379 rate += writeCoeff4x4_CABAC (currMB, pl, block8x8, 2, is_intra);
03380 rate += writeCoeff4x4_CABAC (currMB, pl, block8x8, 3, is_intra);
03381 }
03382 }
03383
03384 return rate;
03385 }
03386
03387
03388
03389
03390
03391
03392
03393 int writeCBPandDquant (Macroblock* currMB)
03394 {
03395 Slice* currSlice = currMB->p_slice;
03396
03397 int rate = 0;
03398 BitCounter *mbBits = &currMB->bits;
03399 SyntaxElement se;
03400 const int* partMap = assignSE2partition[currSlice->partition_mode];
03401 int cbp = currMB->cbp;
03402 DataPartition* dataPart;
03403
03404 se.value2 = 0;
03405
03406 if (currMB->mb_type != I16MB)
03407 {
03408 ImageParameters *p_Img = currMB->p_Img;
03409 int need_transform_size_flag;
03410
03411
03412 se.value1 = cbp;
03413 se.type = SE_CBP;
03414
03415
03416 dataPart = &(currSlice->partArr[partMap[se.type]]);
03417
03418 #if TRACE
03419 snprintf(se.tracestring, TRACESTRING_SIZE, "CBP (%2d,%2d) = %3d",currMB->mb_x, currMB->mb_y, cbp);
03420 #endif
03421 currSlice->writeCBP (currMB, &se, dataPart);
03422
03423 currMB->bits.mb_cbp = currMB->bits.mb_cbp + (unsigned short) se.len;
03424 rate += se.len;
03425
03426
03427
03428 need_transform_size_flag = (((currMB->mb_type >= 1 && currMB->mb_type <= 3)||
03429 (currSlice->slice_type == B_SLICE && currMB->mb_type == 0 && p_Img->active_sps->direct_8x8_inference_flag) ||
03430 (currMB->NoMbPartLessThan8x8Flag))
03431 && currMB->mb_type != I8MB && currMB->mb_type != I4MB
03432 && (currMB->cbp&15 || currSlice->cmp_cbp[1]&15 || currSlice->cmp_cbp[2] & 15)
03433 && currSlice->p_Inp->Transform8x8Mode);
03434
03435 if (need_transform_size_flag)
03436 {
03437 se.value1 = currMB->luma_transform_size_8x8_flag;
03438 se.type = SE_MBTYPE;
03439
03440 #if TRACE
03441 snprintf(se.tracestring, TRACESTRING_SIZE, "transform_size_8x8_flag = %3d", currMB->luma_transform_size_8x8_flag);
03442 #endif
03443 currSlice->writeMB_transform_size(currMB, &se, dataPart);
03444
03445 mbBits->mb_mode = mbBits->mb_mode + (unsigned short) se.len;
03446 rate += se.len;
03447 }
03448 }
03449
03450
03451
03452 if (cbp!=0 || (currMB->mb_type == I16MB))
03453 {
03454 se.value1 = currMB->qp - currMB->prev_qp;
03455 se.type = SE_DELTA_QUANT;
03456
03457
03458 dataPart = &(currSlice->partArr[partMap[se.type]]);
03459 #if TRACE
03460 snprintf(se.tracestring, TRACESTRING_SIZE, "Delta QP (%2d,%2d) = %3d",currMB->mb_x, currMB->mb_y, se.value1);
03461 #endif
03462 currSlice->writeDquant (currMB, &se, dataPart);
03463 currMB->bits.mb_delta_quant = currMB->bits.mb_delta_quant + (unsigned short) se.len;
03464 rate += se.len;
03465 }
03466
03467 return rate;
03468 }
03469
03470
03471
03472
03473
03474
03475
03476
03477 int writeCoeff16x16_CAVLC (Macroblock* currMB, ColorPlane plane)
03478 {
03479 Slice *currSlice = currMB->p_slice;
03480 ImageParameters *p_Img = currMB->p_Img;
03481
03482 int cbp = currMB->cbp;
03483
03484 int i, k;
03485 int rate = 0;
03486
03487 int b8, data_type;
03488 int *mb_bits_coeff = ((plane==0) ? &currMB->bits.mb_y_coeff : ((plane==1) ? &currMB->bits.mb_cb_coeff : &currMB->bits.mb_cr_coeff));
03489
03490 if (currSlice->P444_joined)
03491 {
03492 for (i=0; i < 4; i++)
03493 for (k=4*plane; k<4*(plane+1); k++)
03494 p_Img->nz_coeff [currMB->mbAddrX][i][k] = 0;
03495 }
03496 else
03497 {
03498 reset_mb_nz_coeff(p_Img, currMB->mbAddrX);
03499 }
03500
03501
03502 if (currMB->mb_type != I16MB)
03503 {
03504
03505
03506 for (i=0; i<4; i++)
03507 {
03508 if (cbp & (1<<i))
03509 {
03510 rate += writeCoeff8x8 (currMB, plane, i, currMB->b8x8[i].mode, currMB->luma_transform_size_8x8_flag);
03511 }
03512 }
03513 }
03514 else
03515 {
03516
03517
03518
03519 switch (plane)
03520 {
03521 case 0:
03522 default:
03523 data_type = LUMA_INTRA16x16DC;
03524 break;
03525 case 1:
03526 data_type = CB_INTRA16x16DC;
03527 break;
03528 case 2:
03529 data_type = CR_INTRA16x16DC;
03530 break;
03531 }
03532 rate += currSlice->writeCoeff4x4_CAVLC (currMB, data_type, 0, 0, 0);
03533
03534
03535
03536 if (cbp & 15)
03537 {
03538 switch (plane)
03539 {
03540 case 0:
03541 default:
03542 data_type = LUMA_INTRA16x16AC;
03543 break;
03544 case 1:
03545 data_type = CB_INTRA16x16AC;
03546 break;
03547 case 2:
03548 data_type = CR_INTRA16x16AC;
03549 break;
03550 }
03551
03552 for (b8 = 0; b8 < 4; b8 ++)
03553 {
03554 rate += currSlice->writeCoeff4x4_CAVLC (currMB, data_type, b8, 0, 0);
03555 rate += currSlice->writeCoeff4x4_CAVLC (currMB, data_type, b8, 1, 0);
03556 rate += currSlice->writeCoeff4x4_CAVLC (currMB, data_type, b8, 2, 0);
03557 rate += currSlice->writeCoeff4x4_CAVLC (currMB, data_type, b8, 3, 0);
03558 }
03559 }
03560 }
03561 mb_bits_coeff += rate;
03562 return rate;
03563 }
03564
03565
03566
03567
03568
03569
03570
03571
03572
03573 int writeCoeff16x16_CABAC (Macroblock* currMB, ColorPlane plane)
03574 {
03575 Slice *currSlice = currMB->p_slice;
03576 ImageParameters *p_Img = currSlice->p_Img;
03577
03578 int cbp = currMB->cbp;
03579
03580 int mb_x, mb_y, i, j, k;
03581 int level;
03582 int rate = 0;
03583 int block_rate = 0;
03584 SyntaxElement se;
03585 const int* partMap = assignSE2partition[currSlice->partition_mode];
03586 int pl_off = plane<<2;
03587 DataPartition* dataPart;
03588
03589 int b8, b4;
03590 int* DCLevel = currSlice->cofDC[plane][0];
03591 int* DCRun = currSlice->cofDC[plane][1];
03592 int* ACLevel;
03593 int* ACRun;
03594 int data_type;
03595 int* mb_bits_coeff = ((plane==0) ? &currMB->bits.mb_y_coeff : ((plane==1) ? &currMB->bits.mb_cb_coeff : &currMB->bits.mb_cr_coeff));
03596
03597 if (currSlice->P444_joined)
03598 {
03599 for (i=0; i < 4; i++)
03600 for (k=4*plane; k<4*(plane+1); k++)
03601 p_Img->nz_coeff [currMB->mbAddrX][i][k] = 0;
03602 }
03603 else
03604 {
03605 reset_mb_nz_coeff(p_Img, currMB->mbAddrX);
03606 }
03607
03608
03609 if (currMB->mb_type != I16MB)
03610 {
03611
03612
03613 if (cbp != 0)
03614 {
03615 for (i=0; i<4; i++)
03616 {
03617 if (cbp & (1<<i))
03618 {
03619 rate += writeCoeff8x8 (currMB, plane, i, currMB->b8x8[i].mode, currMB->luma_transform_size_8x8_flag);
03620 }
03621 }
03622 }
03623 }
03624 else
03625 {
03626
03627
03628
03629 currMB->is_intra_block = TRUE;
03630 switch (plane)
03631 {
03632 case 0:
03633 default:
03634 data_type = LUMA_16DC;
03635 break;
03636 case 1:
03637 data_type = CB_16DC;
03638 break;
03639 case 2:
03640 data_type = CR_16DC;
03641 break;
03642 }
03643
03644 se.context = data_type;
03645 se.type = SE_LUM_DC_INTRA;
03646
03647
03648 dataPart = &(currSlice->partArr[partMap[se.type]]);
03649
03650 level = 1;
03651 for (k=0; k<=16 && level!=0; k++)
03652 {
03653 level = se.value1 = DCLevel[k];
03654 se.value2 = DCRun [k];
03655 #if TRACE
03656 trace_coeff(&se, plane, "16x16 DC", k, level, se.value2);
03657 #endif
03658 writeRunLevel_CABAC(currMB, &se, dataPart);
03659 block_rate += se.len;
03660 }
03661 *mb_bits_coeff += block_rate;
03662 rate += block_rate;
03663
03664
03665 if (cbp & 15)
03666 {
03667 switch (plane)
03668 {
03669 case 0:
03670 default:
03671 data_type = LUMA_16AC;
03672 break;
03673 case 1:
03674 data_type = CB_16AC;
03675 break;
03676 case 2:
03677 data_type = CR_16AC;
03678 break;
03679 }
03680
03681 se.context = data_type;
03682 se.type = SE_LUM_AC_INTRA;
03683
03684
03685 dataPart = &(currSlice->partArr[partMap[se.type]]);
03686 block_rate = 0;
03687
03688 for (mb_y = 0; mb_y < 4; mb_y += 2)
03689 {
03690 for (mb_x = 0; mb_x < 4; mb_x += 2)
03691 {
03692 for (j = mb_y; j < mb_y + 2; j++)
03693 {
03694 int j1 = 2*(j >> 1);
03695 int j2 = 2*(j & 0x01);
03696 currMB->subblock_y = (short) (j << 2);
03697
03698 for (i=mb_x; i < mb_x+2; i++)
03699 {
03700 currMB->subblock_x = (short) (i << 2);
03701 b8 = j1 + (i >> 1) + pl_off;
03702 b4 = j2 + (i & 0x01);
03703
03704 ACLevel = currSlice->cofAC[b8][b4][0];
03705 ACRun = currSlice->cofAC[b8][b4][1];
03706
03707 level=1;
03708
03709 for (k=0; k < 16 && level !=0; k++)
03710 {
03711 level = se.value1 = ACLevel[k];
03712 se.value2 = ACRun [k];
03713 #if TRACE
03714 trace_coeff(&se, plane, "16x16 AC", k, level, se.value2);
03715 #endif
03716 writeRunLevel_CABAC(currMB, &se, dataPart);
03717 block_rate += se.len;
03718 }
03719 }
03720 }
03721 }
03722 }
03723
03724 rate += block_rate;
03725 *mb_bits_coeff += block_rate;
03726 }
03727 }
03728
03729 return rate;
03730 }
03731
03732
03733
03734
03735
03736
03737
03738
03739
03740 int predict_nnz(Macroblock *currMB, int block_type, int i,int j)
03741 {
03742 Slice *currSlice = currMB->p_slice;
03743 ImageParameters *p_Img = currMB->p_Img;
03744 PixelPos pix;
03745
03746 int pred_nnz = 0;
03747 int cnt = 0;
03748
03749
03750 get4x4Neighbour(currMB, (i << 2) - 1, (j << 2), p_Img->mb_size[IS_LUMA], &pix);
03751
03752 if (IS_INTRA(currMB) && pix.available && p_Img->active_pps->constrained_intra_pred_flag && ((currSlice->partition_mode != 0) && !currSlice->idr_flag))
03753 {
03754 pix.available &= p_Img->intra_block[pix.mb_addr];
03755 if (!pix.available)
03756 cnt++;
03757 }
03758
03759 if (pix.available)
03760 {
03761 switch (block_type)
03762 {
03763 case LUMA:
03764 pred_nnz += p_Img->nz_coeff [pix.mb_addr ][pix.x][pix.y];
03765 cnt++;
03766 break;
03767 case CB:
03768 pred_nnz += p_Img->nz_coeff [pix.mb_addr ][pix.x][4+pix.y];
03769 cnt++;
03770 break;
03771 case CR:
03772 pred_nnz += p_Img->nz_coeff [pix.mb_addr ][pix.x][8+pix.y];
03773 cnt++;
03774 break;
03775 default:
03776 error("writeCoeff4x4_CAVLC: Invalid block type", 600);
03777 break;
03778 }
03779 }
03780
03781
03782 get4x4Neighbour(currMB, (i<<2), (j<<2) - 1, p_Img->mb_size[IS_LUMA], &pix);
03783
03784 if (IS_INTRA(currMB) && pix.available && p_Img->active_pps->constrained_intra_pred_flag && ((currSlice->partition_mode != 0) && !currSlice->idr_flag))
03785 {
03786 pix.available &= p_Img->intra_block[pix.mb_addr];
03787 if (!pix.available)
03788 cnt++;
03789 }
03790
03791 if (pix.available)
03792 {
03793 switch (block_type)
03794 {
03795 case LUMA:
03796 pred_nnz += p_Img->nz_coeff [pix.mb_addr ][pix.x][pix.y];
03797 cnt++;
03798 break;
03799 case CB:
03800 pred_nnz += p_Img->nz_coeff [pix.mb_addr ][pix.x][4+pix.y];
03801 cnt++;
03802 break;
03803 case CR:
03804 pred_nnz += p_Img->nz_coeff [pix.mb_addr ][pix.x][8+pix.y];
03805 cnt++;
03806 break;
03807 default:
03808 error("writeCoeff4x4_CAVLC: Invalid block type", 600);
03809 break;
03810 }
03811 }
03812
03813 if (cnt==2)
03814 {
03815 pred_nnz++;
03816 pred_nnz>>=1;
03817 }
03818
03819 return pred_nnz;
03820 }
03821
03822
03823
03824
03825
03826
03827
03828
03829
03830
03831 int predict_nnz_chroma(Macroblock *currMB, int i,int j)
03832 {
03833 Slice *currSlice = currMB->p_slice;
03834 ImageParameters *p_Img = currMB->p_Img;
03835 PixelPos pix;
03836
03837 int pred_nnz = 0;
03838 int cnt = 0;
03839
03840 if (p_Img->yuv_format != YUV444)
03841 {
03842
03843
03844 get4x4Neighbour(currMB, ((i & 0x01)<<2) - 1, ((j-4)<<2), p_Img->mb_size[IS_CHROMA], &pix);
03845
03846 if (IS_INTRA(currMB) && pix.available && p_Img->active_pps->constrained_intra_pred_flag && ((currSlice->partition_mode != 0) && !currSlice->idr_flag))
03847 {
03848 pix.available &= p_Img->intra_block[pix.mb_addr];
03849 if (!pix.available)
03850 cnt++;
03851 }
03852
03853 if (pix.available)
03854 {
03855 pred_nnz = p_Img->nz_coeff [pix.mb_addr ][2 * (i >> 1) + pix.x][4 + pix.y];
03856 cnt++;
03857 }
03858
03859
03860 get4x4Neighbour(currMB, ((i & 0x01)<<2), ((j-4)<<2) -1, p_Img->mb_size[IS_CHROMA], &pix);
03861
03862 if (IS_INTRA(currMB) && pix.available && p_Img->active_pps->constrained_intra_pred_flag && ((currSlice->partition_mode != 0) && !currSlice->idr_flag))
03863 {
03864 pix.available &= p_Img->intra_block[pix.mb_addr];
03865 if (!pix.available)
03866 cnt++;
03867 }
03868
03869 if (pix.available)
03870 {
03871 pred_nnz += p_Img->nz_coeff [pix.mb_addr ][2 * (i >> 1) + pix.x][4 + pix.y];
03872 cnt++;
03873 }
03874 }
03875
03876
03877 if (cnt==2)
03878 {
03879 pred_nnz++;
03880 pred_nnz>>=1;
03881 }
03882
03883 return pred_nnz;
03884 }
03885
03886
03887
03888
03889
03890
03891
03892
03893
03894
03895
03896 int writeCoeff4x4_CAVLC_normal (Macroblock* currMB, int block_type, int b8, int b4, int param)
03897 {
03898 Slice* currSlice = currMB->p_slice;
03899 ImageParameters *p_Img = currSlice->p_Img;
03900 int no_bits = 0;
03901 SyntaxElement se;
03902 DataPartition *dataPart;
03903 const int *partMap = assignSE2partition[currSlice->partition_mode];
03904
03905 int k,level = 1,run = 0, vlcnum;
03906 int numcoeff = 0, lastcoeff = 0, numtrailingones = 0;
03907 int numones = 0, totzeros = 0, zerosleft, numcoef;
03908 int numcoeff_vlc;
03909 int code, level_two_or_higher;
03910 int dptype = 0;
03911 int nnz, max_coeff_num = 0, cdc = 0, cac = 0;
03912 int subblock_x, subblock_y;
03913 int *mb_bits_coeff = &currMB->bits.mb_y_coeff;
03914 #if TRACE
03915 char type[15];
03916 #endif
03917
03918 static const int incVlc[] = {0, 3, 6, 12, 24, 48, 32768};
03919
03920
03921 int* pLevel = NULL;
03922 int* pRun = NULL;
03923
03924 switch (block_type)
03925 {
03926 case LUMA:
03927 max_coeff_num = 16;
03928
03929 pLevel = currSlice->cofAC[b8][b4][0];
03930 pRun = currSlice->cofAC[b8][b4][1];
03931 #if TRACE
03932 sprintf(type, "%s", "Luma");
03933 #endif
03934 dptype = (IS_INTRA (currMB)) ? SE_LUM_AC_INTRA : SE_LUM_AC_INTER;
03935 break;
03936 case LUMA_INTRA16x16DC:
03937 max_coeff_num = 16;
03938
03939 pLevel = currSlice->cofDC[0][0];
03940 pRun = currSlice->cofDC[0][1];
03941 #if TRACE
03942 sprintf(type, "%s", "Lum16DC");
03943 #endif
03944 dptype = SE_LUM_DC_INTRA;
03945 break;
03946 case LUMA_INTRA16x16AC:
03947 max_coeff_num = 15;
03948
03949 pLevel = currSlice->cofAC[b8][b4][0];
03950 pRun = currSlice->cofAC[b8][b4][1];
03951 #if TRACE
03952 sprintf(type, "%s", "Lum16AC");
03953 #endif
03954 dptype = SE_LUM_AC_INTRA;
03955 break;
03956 case CHROMA_DC:
03957 max_coeff_num = p_Img->num_cdc_coeff;
03958 mb_bits_coeff = &currMB->bits.mb_uv_coeff;
03959 cdc = 1;
03960
03961 pLevel = currSlice->cofDC[param + 1][0];
03962 pRun = currSlice->cofDC[param + 1][1];
03963 #if TRACE
03964 sprintf(type, "%s", "ChrDC");
03965 #endif
03966 dptype = (IS_INTRA (currMB)) ? SE_CHR_DC_INTRA : SE_CHR_DC_INTER;
03967 break;
03968 case CHROMA_AC:
03969 max_coeff_num = 15;
03970 mb_bits_coeff = &currMB->bits.mb_uv_coeff;
03971 cac = 1;
03972
03973 pLevel = currSlice->cofAC[b8][b4][0];
03974 pRun = currSlice->cofAC[b8][b4][1];
03975 #if TRACE
03976 sprintf(type, "%s", "ChrAC");
03977 #endif
03978 dptype = (IS_INTRA (currMB)) ? SE_CHR_AC_INTRA : SE_CHR_AC_INTER;
03979 break;
03980 default:
03981 error("writeCoeff4x4_CAVLC: Invalid block type", 600);
03982 break;
03983 }
03984
03985 dataPart = &(currSlice->partArr[partMap[dptype]]);
03986
03987 for(k = 0; (k <= ((cdc) ? p_Img->num_cdc_coeff : 16)) && level != 0; k++)
03988 {
03989 level = pLevel[k];
03990 run = pRun[k];
03991
03992 if (level)
03993 {
03994
03995 totzeros += run;
03996 if (iabs(level) == 1)
03997 {
03998 numones ++;
03999 numtrailingones ++;
04000 numtrailingones = imin(numtrailingones, 3);
04001 }
04002 else
04003 {
04004 numtrailingones = 0;
04005 }
04006 numcoeff ++;
04007 lastcoeff = k;
04008 }
04009 }
04010
04011 if (!cdc)
04012 {
04013 if (!cac)
04014 {
04015
04016 subblock_x = ((b8 & 0x1) == 0) ? (((b4 & 0x1) == 0) ? 0 : 1) : (((b4 & 0x1) == 0) ? 2 : 3);
04017
04018 subblock_y = (b8 < 2) ? ((b4 < 2) ? 0 : 1) : ((b4 < 2) ? 2 : 3);
04019
04020 nnz = predict_nnz(currMB, LUMA, subblock_x,subblock_y);
04021 }
04022 else
04023 {
04024
04025 subblock_x = param >> 4;
04026 subblock_y = param & 15;
04027 nnz = predict_nnz_chroma(currMB, subblock_x, subblock_y);
04028 }
04029 p_Img->nz_coeff [currMB->mbAddrX ][subblock_x][subblock_y] = numcoeff;
04030
04031 if (nnz < 2)
04032 {
04033 numcoeff_vlc = 0;
04034 }
04035 else if (nnz < 4)
04036 {
04037 numcoeff_vlc = 1;
04038 }
04039 else if (nnz < 8)
04040 {
04041 numcoeff_vlc = 2;
04042 }
04043 else
04044 {
04045 numcoeff_vlc = 3;
04046 }
04047
04048 }
04049 else
04050 {
04051
04052
04053 numcoeff_vlc = 0;
04054
04055 subblock_x = param;
04056 subblock_y = param;
04057 }
04058
04059 se.type = dptype;
04060
04061 se.value1 = numcoeff;
04062 se.value2 = numtrailingones;
04063 se.len = numcoeff_vlc;
04064
04065 #if TRACE
04066 snprintf(se.tracestring,
04067 TRACESTRING_SIZE, "%s # c & tr.1s(%d,%d) vlc=%d #c=%d #t1=%d",
04068 type, subblock_x, subblock_y, numcoeff_vlc, numcoeff, numtrailingones);
04069 #endif
04070
04071 if (!cdc)
04072 writeSyntaxElement_NumCoeffTrailingOnes(&se, dataPart);
04073 else
04074 writeSyntaxElement_NumCoeffTrailingOnesChromaDC(p_Img, &se, dataPart);
04075
04076 *mb_bits_coeff += se.len;
04077 no_bits += se.len;
04078
04079 if (!numcoeff)
04080 return no_bits;
04081
04082 if (numcoeff)
04083 {
04084 code = 0;
04085 for (k = lastcoeff; k > lastcoeff - numtrailingones; k--)
04086 {
04087 level = pLevel[k];
04088
04089 if (iabs(level) > 1)
04090 {
04091 printf("ERROR: level > 1\n");
04092 exit(-1);
04093 }
04094
04095 code <<= 1;
04096
04097 if (level < 0)
04098 {
04099 code |= 0x1;
04100 }
04101 }
04102
04103 if (numtrailingones)
04104 {
04105 se.type = dptype;
04106
04107 se.value2 = numtrailingones;
04108 se.value1 = code;
04109
04110 #if TRACE
04111 snprintf(se.tracestring,
04112 TRACESTRING_SIZE, "%s trailing ones sign (%d,%d)",
04113 type, subblock_x, subblock_y);
04114 #endif
04115
04116 writeSyntaxElement_VLC (&se, dataPart);
04117 *mb_bits_coeff += se.len;
04118 no_bits += se.len;
04119
04120 }
04121
04122
04123 level_two_or_higher = (numcoeff > 3 && numtrailingones == 3) ? 0 : 1;
04124
04125 vlcnum = (numcoeff > 10 && numtrailingones < 3) ? 1 : 0;
04126
04127 for (k = lastcoeff - numtrailingones; k >= 0; k--)
04128 {
04129 level = pLevel[k];
04130
04131 se.value1 = level;
04132 se.type = dptype;
04133
04134 #if TRACE
04135 snprintf(se.tracestring,
04136 TRACESTRING_SIZE, "%s lev (%d,%d) k=%d vlc=%d lev=%3d",
04137 type, subblock_x, subblock_y, k, vlcnum, level);
04138 #endif
04139
04140 if (level_two_or_higher)
04141 {
04142 level_two_or_higher = 0;
04143
04144 if (se.value1 > 0)
04145 se.value1 --;
04146 else
04147 se.value1 ++;
04148 }
04149
04150
04151
04152 if (vlcnum == 0)
04153 writeSyntaxElement_Level_VLC1(&se, dataPart, p_Img->active_sps->profile_idc);
04154 else
04155 writeSyntaxElement_Level_VLCN(&se, vlcnum, dataPart, p_Img->active_sps->profile_idc);
04156
04157
04158 if (iabs(level) > incVlc[vlcnum])
04159 vlcnum++;
04160
04161 if ((k == lastcoeff - numtrailingones) && iabs(level) > 3)
04162 vlcnum = 2;
04163
04164 *mb_bits_coeff += se.len;
04165 no_bits += se.len;
04166 }
04167
04168
04169 if (numcoeff < max_coeff_num)
04170 {
04171
04172 se.type = dptype;
04173 se.value1 = totzeros;
04174
04175 vlcnum = numcoeff - 1;
04176
04177 se.len = vlcnum;
04178
04179 #if TRACE
04180 snprintf(se.tracestring,
04181 TRACESTRING_SIZE, "%s totalrun (%d,%d) vlc=%d totzeros=%3d",
04182 type, subblock_x, subblock_y, vlcnum, totzeros);
04183 #endif
04184 if (!cdc)
04185 writeSyntaxElement_TotalZeros(&se, dataPart);
04186 else
04187 writeSyntaxElement_TotalZerosChromaDC(p_Img, &se, dataPart);
04188
04189 *mb_bits_coeff += se.len;
04190 no_bits += se.len;
04191 }
04192
04193
04194 zerosleft = totzeros;
04195 numcoef = numcoeff;
04196 for (k = lastcoeff; k >= 0; k--)
04197 {
04198 run = pRun[k];
04199
04200 se.value1 = run;
04201 se.type = dptype;
04202
04203
04204
04205 if ((!zerosleft) || (numcoeff <= 1 ))
04206 break;
04207
04208 if (numcoef > 1 && zerosleft)
04209 {
04210 vlcnum = imin(zerosleft - 1, RUNBEFORE_NUM_M1);
04211 se.len = vlcnum;
04212
04213 #if TRACE
04214 snprintf(se.tracestring,
04215 TRACESTRING_SIZE, "%s run (%d,%d) k=%d vlc=%d run=%2d",
04216 type, subblock_x, subblock_y, k, vlcnum, run);
04217 #endif
04218
04219 writeSyntaxElement_Run(&se, dataPart);
04220
04221 *mb_bits_coeff += se.len;
04222 no_bits += se.len;
04223
04224 zerosleft -= run;
04225 numcoef --;
04226 }
04227 }
04228 }
04229
04230 return no_bits;
04231 }
04232
04233
04234
04235
04236
04237
04238
04239
04240
04241
04242
04243 int writeCoeff4x4_CAVLC_444 (Macroblock* currMB, int block_type, int b8, int b4, int param)
04244 {
04245 Slice* currSlice = currMB->p_slice;
04246 ImageParameters *p_Img = currSlice->p_Img;
04247 int no_bits = 0;
04248 SyntaxElement se;
04249 DataPartition *dataPart;
04250 const int *partMap = assignSE2partition[currSlice->partition_mode];
04251
04252 int k,level = 1,run = 0, vlcnum;
04253 int numcoeff = 0, lastcoeff = 0, numtrailingones = 0;
04254 int numones = 0, totzeros = 0, zerosleft, numcoef;
04255 int numcoeff_vlc;
04256 int code, level_two_or_higher;
04257 int dptype = 0;
04258 int nnz, max_coeff_num = 0, cdc = 0, cac = 0;
04259 int subblock_x, subblock_y;
04260 int *mb_bits_coeff = &currMB->bits.mb_y_coeff;
04261 #if TRACE
04262 char type[15];
04263 #endif
04264
04265 static const int incVlc[] = {0, 3, 6, 12, 24, 48, 32768};
04266
04267 int* pLevel = NULL;
04268 int* pRun = NULL;
04269
04270 switch (block_type)
04271 {
04272 case LUMA:
04273 max_coeff_num = 16;
04274
04275 pLevel = currSlice->cofAC[b8][b4][0];
04276 pRun = currSlice->cofAC[b8][b4][1];
04277 #if TRACE
04278 sprintf(type, "%s", "Luma");
04279 #endif
04280 dptype = (IS_INTRA (currMB)) ? SE_LUM_AC_INTRA : SE_LUM_AC_INTER;
04281 break;
04282 case LUMA_INTRA16x16DC:
04283 max_coeff_num = 16;
04284
04285 pLevel = currSlice->cofDC[0][0];
04286 pRun = currSlice->cofDC[0][1];
04287 #if TRACE
04288 sprintf(type, "%s", "Lum16DC");
04289 #endif
04290 dptype = SE_LUM_DC_INTRA;
04291 break;
04292 case LUMA_INTRA16x16AC:
04293 max_coeff_num = 15;
04294
04295 pLevel = currSlice->cofAC[b8][b4][0];
04296 pRun = currSlice->cofAC[b8][b4][1];
04297 #if TRACE
04298 sprintf(type, "%s", "Lum16AC");
04299 #endif
04300 dptype = SE_LUM_AC_INTRA;
04301 break;
04302 case CB:
04303 max_coeff_num = 16;
04304 mb_bits_coeff = &currMB->bits.mb_cb_coeff;
04305
04306 pLevel = currSlice->cofAC[4+b8][b4][0];
04307 pRun = currSlice->cofAC[4+b8][b4][1];
04308 #if TRACE
04309 sprintf(type, "%s", "CB");
04310 #endif
04311 dptype = (IS_INTRA (currMB)) ? SE_LUM_AC_INTRA : SE_LUM_AC_INTER;
04312 break;
04313 case CB_INTRA16x16DC:
04314 max_coeff_num = 16;
04315 mb_bits_coeff = &currMB->bits.mb_cb_coeff;
04316
04317 pLevel = currSlice->cofDC[1][0];
04318 pRun = currSlice->cofDC[1][1];
04319 #if TRACE
04320 sprintf(type, "%s", "CB_16DC");
04321 #endif
04322 dptype = SE_LUM_DC_INTRA;
04323 break;
04324 case CB_INTRA16x16AC:
04325 max_coeff_num = 15;
04326 mb_bits_coeff = &currMB->bits.mb_cb_coeff;
04327
04328 pLevel = currSlice->cofAC[4+b8][b4][0];
04329 pRun = currSlice->cofAC[4+b8][b4][1];
04330 #if TRACE
04331 sprintf(type, "%s", "CB_16AC");
04332 #endif
04333 dptype = SE_LUM_AC_INTRA;
04334 break;
04335
04336 case CR:
04337 max_coeff_num = 16;
04338 mb_bits_coeff = &currMB->bits.mb_cr_coeff;
04339
04340 pLevel = currSlice->cofAC[8+b8][b4][0];
04341 pRun = currSlice->cofAC[8+b8][b4][1];
04342 #if TRACE
04343 sprintf(type, "%s", "CR");
04344 #endif
04345 dptype = (IS_INTRA (currMB)) ? SE_LUM_AC_INTRA : SE_LUM_AC_INTER;
04346 break;
04347 case CR_INTRA16x16DC:
04348 max_coeff_num = 16;
04349 mb_bits_coeff = &currMB->bits.mb_cr_coeff;
04350
04351 pLevel = currSlice->cofDC[2][0];
04352 pRun = currSlice->cofDC[2][1];
04353 #if TRACE
04354 sprintf(type, "%s", "CR_16DC");
04355 #endif
04356 dptype = SE_LUM_DC_INTRA;
04357 break;
04358 case CR_INTRA16x16AC:
04359 max_coeff_num = 15;
04360 mb_bits_coeff = &currMB->bits.mb_cr_coeff;
04361
04362 pLevel = currSlice->cofAC[8+b8][b4][0];
04363 pRun = currSlice->cofAC[8+b8][b4][1];
04364 #if TRACE
04365 sprintf(type, "%s", "CR_16AC");
04366 #endif
04367 dptype = SE_LUM_AC_INTRA;
04368 break;
04369
04370 case CHROMA_DC:
04371 max_coeff_num = p_Img->num_cdc_coeff;
04372 mb_bits_coeff = &currMB->bits.mb_uv_coeff;
04373 cdc = 1;
04374
04375 pLevel = currSlice->cofDC[param+1][0];
04376 pRun = currSlice->cofDC[param+1][1];
04377 #if TRACE
04378 sprintf(type, "%s", "ChrDC");
04379 #endif
04380 dptype = (IS_INTRA (currMB)) ? SE_CHR_DC_INTRA : SE_CHR_DC_INTER;
04381 break;
04382 case CHROMA_AC:
04383 max_coeff_num = 15;
04384 mb_bits_coeff = &currMB->bits.mb_uv_coeff;
04385 cac = 1;
04386
04387 pLevel = currSlice->cofAC[b8][b4][0];
04388 pRun = currSlice->cofAC[b8][b4][1];
04389 #if TRACE
04390 sprintf(type, "%s", "ChrAC");
04391 #endif
04392 dptype = (IS_INTRA (currMB)) ? SE_CHR_AC_INTRA : SE_CHR_AC_INTER;
04393 break;
04394 default:
04395 error("writeCoeff4x4_CAVLC: Invalid block type", 600);
04396 break;
04397 }
04398
04399 dataPart = &(currSlice->partArr[partMap[dptype]]);
04400
04401 for(k = 0; (k <= ((cdc) ? p_Img->num_cdc_coeff : 16)) && level != 0; k++)
04402 {
04403 level = pLevel[k];
04404 run = pRun[k];
04405
04406 if (level)
04407 {
04408
04409 totzeros += run;
04410 if (iabs(level) == 1)
04411 {
04412 numones ++;
04413 numtrailingones ++;
04414 numtrailingones = imin(numtrailingones, 3);
04415 }
04416 else
04417 {
04418 numtrailingones = 0;
04419 }
04420 numcoeff ++;
04421 lastcoeff = k;
04422 }
04423 }
04424
04425 if (!cdc)
04426 {
04427 if(block_type==LUMA || block_type==LUMA_INTRA16x16DC || block_type==LUMA_INTRA16x16AC
04428 ||block_type==CHROMA_AC)
04429 {
04430 if (!cac)
04431 {
04432
04433 subblock_x = ((b8 & 0x1) == 0) ? (((b4 & 0x1) == 0) ? 0 : 1) : (((b4 & 0x1) == 0) ? 2 : 3);
04434
04435 subblock_y = (b8 < 2) ? ((b4 < 2) ? 0 : 1) : ((b4 < 2) ? 2 : 3);
04436
04437 nnz = predict_nnz(currMB, LUMA, subblock_x,subblock_y);
04438 }
04439 else
04440 {
04441
04442 subblock_x = param >> 4;
04443 subblock_y = param & 15;
04444 nnz = predict_nnz_chroma(currMB, subblock_x, subblock_y);
04445 }
04446 p_Img->nz_coeff [currMB->mbAddrX ][subblock_x][subblock_y] = numcoeff;
04447 }
04448 else if (block_type==CB || block_type==CB_INTRA16x16DC
04449 || block_type==CB_INTRA16x16AC)
04450 {
04451 subblock_x = ((b8 & 0x1) == 0)?(((b4 & 0x1) == 0) ? 0 : 1):(((b4 & 0x1) == 0) ? 2 : 3);
04452
04453 subblock_y = (b8 < 2) ? ((b4 < 2) ? 0 : 1) : ((b4 < 2) ? 2 : 3);
04454
04455 nnz = predict_nnz(currMB, CB, subblock_x,subblock_y);
04456 p_Img->nz_coeff [currMB->mbAddrX ][subblock_x][4+subblock_y] = numcoeff;
04457 }
04458 else
04459 {
04460 subblock_x = ((b8 & 0x1) == 0)?(((b4 & 0x1) == 0) ? 0 : 1) : (((b4 & 0x1) == 0) ? 2 : 3);
04461
04462 subblock_y = (b8 < 2) ? ((b4 < 2) ? 0 : 1) : ((b4 < 2) ? 2 : 3);
04463
04464 nnz = predict_nnz(currMB, CR, subblock_x,subblock_y);
04465 p_Img->nz_coeff [currMB->mbAddrX ][subblock_x][8+subblock_y] = numcoeff;
04466 }
04467
04468 if (nnz < 2)
04469 {
04470 numcoeff_vlc = 0;
04471 }
04472 else if (nnz < 4)
04473 {
04474 numcoeff_vlc = 1;
04475 }
04476 else if (nnz < 8)
04477 {
04478 numcoeff_vlc = 2;
04479 }
04480 else
04481 {
04482 numcoeff_vlc = 3;
04483 }
04484
04485 }
04486 else
04487 {
04488
04489
04490 numcoeff_vlc = 0;
04491
04492 subblock_x = param;
04493 subblock_y = param;
04494 }
04495
04496 se.type = dptype;
04497
04498 se.value1 = numcoeff;
04499 se.value2 = numtrailingones;
04500 se.len = numcoeff_vlc;
04501
04502 #if TRACE
04503 snprintf(se.tracestring,
04504 TRACESTRING_SIZE, "%s # c & tr.1s(%d,%d) vlc=%d #c=%d #t1=%d",
04505 type, subblock_x, subblock_y, numcoeff_vlc, numcoeff, numtrailingones);
04506 #endif
04507
04508 if (!cdc)
04509 writeSyntaxElement_NumCoeffTrailingOnes(&se, dataPart);
04510 else
04511 writeSyntaxElement_NumCoeffTrailingOnesChromaDC(p_Img, &se, dataPart);
04512
04513 *mb_bits_coeff += se.len;
04514 no_bits += se.len;
04515
04516 if (!numcoeff)
04517 return no_bits;
04518
04519 if (numcoeff)
04520 {
04521 code = 0;
04522 for (k = lastcoeff; k > lastcoeff - numtrailingones; k--)
04523 {
04524 level = pLevel[k];
04525
04526 if (iabs(level) > 1)
04527 {
04528 printf("ERROR: level > 1\n");
04529 exit(-1);
04530 }
04531
04532 code <<= 1;
04533
04534 if (level < 0)
04535 {
04536 code |= 0x1;
04537 }
04538 }
04539
04540 if (numtrailingones)
04541 {
04542 se.type = dptype;
04543
04544 se.value2 = numtrailingones;
04545 se.value1 = code;
04546
04547 #if TRACE
04548 snprintf(se.tracestring,
04549 TRACESTRING_SIZE, "%s trailing ones sign (%d,%d)",
04550 type, subblock_x, subblock_y);
04551 #endif
04552
04553 writeSyntaxElement_VLC (&se, dataPart);
04554 *mb_bits_coeff += se.len;
04555 no_bits += se.len;
04556
04557 }
04558
04559
04560 level_two_or_higher = (numcoeff > 3 && numtrailingones == 3) ? 0 : 1;
04561
04562 vlcnum = (numcoeff > 10 && numtrailingones < 3) ? 1 : 0;
04563
04564 for (k = lastcoeff - numtrailingones; k >= 0; k--)
04565 {
04566 level = pLevel[k];
04567
04568 se.value1 = level;
04569 se.type = dptype;
04570
04571 #if TRACE
04572 snprintf(se.tracestring,
04573 TRACESTRING_SIZE, "%s lev (%d,%d) k=%d vlc=%d lev=%3d",
04574 type, subblock_x, subblock_y, k, vlcnum, level);
04575 #endif
04576
04577 if (level_two_or_higher)
04578 {
04579 level_two_or_higher = 0;
04580
04581 if (se.value1 > 0)
04582 se.value1 --;
04583 else
04584 se.value1 ++;
04585 }
04586
04587
04588
04589 if (vlcnum == 0)
04590 writeSyntaxElement_Level_VLC1(&se, dataPart, p_Img->active_sps->profile_idc);
04591 else
04592 writeSyntaxElement_Level_VLCN(&se, vlcnum, dataPart, p_Img->active_sps->profile_idc);
04593
04594
04595 if (iabs(level) > incVlc[vlcnum])
04596 vlcnum++;
04597
04598 if ((k == lastcoeff - numtrailingones) && iabs(level) > 3)
04599 vlcnum = 2;
04600
04601 *mb_bits_coeff += se.len;
04602 no_bits += se.len;
04603 }
04604
04605
04606 if (numcoeff < max_coeff_num)
04607 {
04608
04609 se.type = dptype;
04610 se.value1 = totzeros;
04611
04612 vlcnum = numcoeff - 1;
04613
04614 se.len = vlcnum;
04615
04616 #if TRACE
04617 snprintf(se.tracestring,
04618 TRACESTRING_SIZE, "%s totalrun (%d,%d) vlc=%d totzeros=%3d",
04619 type, subblock_x, subblock_y, vlcnum, totzeros);
04620 #endif
04621 if (!cdc)
04622 writeSyntaxElement_TotalZeros(&se, dataPart);
04623 else
04624 writeSyntaxElement_TotalZerosChromaDC(p_Img, &se, dataPart);
04625
04626 *mb_bits_coeff += se.len;
04627 no_bits += se.len;
04628 }
04629
04630
04631 zerosleft = totzeros;
04632 numcoef = numcoeff;
04633 for (k = lastcoeff; k >= 0; k--)
04634 {
04635 run = pRun[k];
04636
04637 se.value1 = run;
04638 se.type = dptype;
04639
04640
04641
04642 if ((!zerosleft) || (numcoeff <= 1 ))
04643 break;
04644
04645 if (numcoef > 1 && zerosleft)
04646 {
04647 vlcnum = imin(zerosleft - 1, RUNBEFORE_NUM_M1);
04648 se.len = vlcnum;
04649
04650 #if TRACE
04651 snprintf(se.tracestring,
04652 TRACESTRING_SIZE, "%s run (%d,%d) k=%d vlc=%d run=%2d",
04653 type, subblock_x, subblock_y, k, vlcnum, run);
04654 #endif
04655
04656 writeSyntaxElement_Run(&se, dataPart);
04657
04658 *mb_bits_coeff += se.len;
04659 no_bits += se.len;
04660
04661 zerosleft -= run;
04662 numcoef --;
04663 }
04664 }
04665 }
04666
04667 return no_bits;
04668 }
04669
04670 int distI16x16_satd(Macroblock *currMB, imgpel **img_org, imgpel **pred_img, double min_cost)
04671 {
04672 int **M7 = NULL;
04673 int **tblk4x4 = currMB->p_slice->tblk4x4;
04674 int ****i16blk4x4 = currMB->p_slice->i16blk4x4;
04675 imgpel *cur_img, *prd_img;
04676 int current_intra_sad_2 = 0;
04677 int ii, jj, i, j;
04678
04679 for (j = 0; j < MB_BLOCK_SIZE; j++)
04680 {
04681 cur_img = &img_org[currMB->opix_y + j][currMB->pix_x];
04682 prd_img = pred_img[j];
04683 for (i = 0; i < MB_BLOCK_SIZE; i++)
04684 {
04685 i16blk4x4[j >> 2][i >> 2][j & 0x03][i & 0x03] = cur_img[i] - prd_img[i];
04686 }
04687 }
04688
04689 for (jj=0;jj<4;jj++)
04690 {
04691 for (ii=0;ii<4;ii++)
04692 {
04693 M7 = i16blk4x4[jj][ii];
04694 hadamard4x4(M7, M7);
04695
04696 current_intra_sad_2 += iabs(M7[0][1]);
04697 current_intra_sad_2 += iabs(M7[0][2]);
04698 current_intra_sad_2 += iabs(M7[0][3]);
04699
04700 if (current_intra_sad_2 > min_cost)
04701 return (current_intra_sad_2);
04702
04703 for (j=1;j<4;j++)
04704 {
04705 for (i=0;i<4;i++)
04706 {
04707 current_intra_sad_2 += iabs(M7[j][i]);
04708 }
04709 if (current_intra_sad_2 > min_cost)
04710 return (current_intra_sad_2);
04711 }
04712 }
04713 }
04714
04715 for (j=0;j<4;j++)
04716 {
04717 for (i=0;i<4;i++)
04718 tblk4x4[j][i] = (i16blk4x4[j][i][0][0] >> 1);
04719 }
04720
04721 hadamard4x4(tblk4x4, tblk4x4);
04722 for (j=0;j<4;j++)
04723 {
04724 for (i=0;i<4;i++)
04725 {
04726 current_intra_sad_2 += iabs(tblk4x4[j][i]);
04727 }
04728
04729 if (current_intra_sad_2 > min_cost)
04730 return (current_intra_sad_2);
04731 }
04732
04733 return current_intra_sad_2;
04734 }
04735
04736 int distI16x16_sad(Macroblock *currMB, imgpel **img_org, imgpel **pred_img, double min_cost)
04737 {
04738 imgpel *cur_img, *prd_img;
04739 int current_intra_sad_2 = 0;
04740 int i, j;
04741
04742 for (j=0;j<16;j++)
04743 {
04744 cur_img = &img_org[currMB->opix_y + j][currMB->pix_x];
04745 prd_img = pred_img[j];
04746 for (i=0;i<16;i++)
04747 {
04748 current_intra_sad_2 += iabs( *cur_img++ - *prd_img++ );
04749 }
04750
04751 if (current_intra_sad_2 > min_cost)
04752 return (current_intra_sad_2);
04753 }
04754 return current_intra_sad_2;
04755 }
04756
04757 int distI16x16_sse(Macroblock *currMB, imgpel **img_org, imgpel **pred_img, double min_cost)
04758 {
04759 imgpel *cur_img, *prd_img;
04760 int current_intra_sad_2 = 0;
04761 int i, j;
04762
04763 for (j=0;j<16;j++)
04764 {
04765 cur_img = &img_org[currMB->opix_y + j][currMB->pix_x];
04766 prd_img = pred_img[j];
04767 for (i=0;i<16;i++)
04768 {
04769 current_intra_sad_2 += iabs2( *cur_img++ - *prd_img++ );
04770 }
04771
04772 if (current_intra_sad_2 > min_cost)
04773 return (current_intra_sad_2);
04774 }
04775 return current_intra_sad_2;
04776 }
04777
04778
04779
04780
04781
04782
04783
04784
04785
04786
04787
04788
04789 double find_sad_16x16_JM(Macroblock *currMB)
04790 {
04791 Slice *currSlice = currMB->p_slice;
04792 ImageParameters *p_Img = currMB->p_Img;
04793 InputParameters *p_Inp = currMB->p_Inp;
04794
04795 double current_intra_sad_2, best_intra_sad2 = 1e30;
04796
04797 int i,k;
04798 imgpel ***curr_mpr_16x16 = currSlice->mpr_16x16[0];
04799
04800 PixelPos up;
04801 PixelPos left[17];
04802
04803 int up_avail, left_avail, left_up_avail;
04804
04805 for (i=0;i<17;i++)
04806 {
04807 p_Img->getNeighbour(currMB, -1 , i - 1 , p_Img->mb_size[IS_LUMA], &left[i]);
04808 }
04809
04810 p_Img->getNeighbour(currMB, 0 , -1 , p_Img->mb_size[IS_LUMA], &up);
04811
04812 if (!(p_Inp->UseConstrainedIntraPred))
04813 {
04814 up_avail = up.available;
04815 left_avail = left[1].available;
04816 left_up_avail = left[0].available;
04817 }
04818 else
04819 {
04820 up_avail = up.available ? p_Img->intra_block[up.mb_addr] : 0;
04821 for (i = 1, left_avail = 1; i < 17;i++)
04822 left_avail &= left[i].available ? p_Img->intra_block[left[i].mb_addr]: 0;
04823 left_up_avail = left[0].available ? p_Img->intra_block[left[0].mb_addr]: 0;
04824 }
04825
04826 currMB->i16mode = DC_PRED_16;
04827
04828 for (k = 0;k < 4; k++)
04829 {
04830 if (p_Inp->IntraDisableInterOnly == 0 || currSlice->slice_type != I_SLICE)
04831 {
04832 if (p_Inp->Intra16x16ParDisable && (k==VERT_PRED_16||k==HOR_PRED_16))
04833 continue;
04834
04835 if (p_Inp->Intra16x16PlaneDisable && k==PLANE_16)
04836 continue;
04837 }
04838
04839 if (!((k==0 && !up_avail) || (k==1 && !left_avail) || (k==3 && (!left_avail || !up_avail || !left_up_avail))))
04840 {
04841 current_intra_sad_2 = currSlice->distI16x16(currMB, p_Img->pCurImg, curr_mpr_16x16[k], best_intra_sad2);
04842 if (currSlice->P444_joined)
04843 {
04844 current_intra_sad_2 += currSlice->distI16x16(currMB, p_Img->pImgOrg[1], currSlice->mpr_16x16[1][k], best_intra_sad2);
04845 current_intra_sad_2 += currSlice->distI16x16(currMB, p_Img->pImgOrg[2], currSlice->mpr_16x16[2][k], best_intra_sad2);
04846 }
04847
04848 if(current_intra_sad_2 < best_intra_sad2)
04849 {
04850 best_intra_sad2 = current_intra_sad_2;
04851 currMB->i16mode = (char) k;
04852 }
04853 }
04854 }
04855
04856 return best_intra_sad2;
04857 }
04858
04859
04860
04861
04862
04863
04864
04865
04866
04867
04868 void change_plane_JV( ImageParameters *p_Img, int nplane )
04869 {
04870 p_Img->colour_plane_id = (char) nplane;
04871 p_Img->mb_data = p_Img->mb_data_JV[nplane];
04872 p_Img->enc_picture = p_Img->enc_frame_picture_JV[nplane];
04873 p_Img->pCurImg = p_Img->imgData.frm_data[nplane];
04874 }
04875
04876
04877
04878
04879
04880
04881 void make_frame_picture_JV(ImageParameters *p_Img, InputParameters *p_Inp)
04882 {
04883 int uv, line;
04884 int nsize;
04885 int nplane;
04886 p_Img->enc_frame_picture[0] = p_Img->enc_frame_picture_JV[0];
04887
04888 for( nplane=0; nplane<MAX_PLANE; nplane++ )
04889 {
04890 copy_storable_param_JV( p_Img, nplane, p_Img->enc_frame_picture[0], p_Img->enc_frame_picture_JV[nplane] );
04891 }
04892
04893 for( uv=0; uv<2; uv++ )
04894 {
04895 for( line=0; line<p_Img->height; line++ )
04896 {
04897 nsize = sizeof(imgpel) * p_Img->width;
04898 memcpy( p_Img->enc_frame_picture[0]->imgUV[uv][line], p_Img->enc_frame_picture_JV[uv+1]->imgY[line], nsize );
04899 }
04900 free_storable_picture(p_Img, p_Inp, p_Img->enc_frame_picture_JV[uv+1]);
04901 }
04902 }
04903