00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "contributors.h"
00023
00024 #include <limits.h>
00025 #include <math.h>
00026
00027 #include "global.h"
00028 #include "enc_statistics.h"
00029
00030 #include "elements.h"
00031 #include "macroblock.h"
00032 #include "memalloc.h"
00033 #include "mc_prediction.h"
00034 #include "refbuf.h"
00035 #include "fmo.h"
00036 #include "vlc.h"
00037 #include "image.h"
00038 #include "mb_access.h"
00039 #include "ratectl.h"
00040 #include "cabac.h"
00041 #include "biariencode.h"
00042 #include "transform8x8.h"
00043 #include "transform.h"
00044 #include "me_fullsearch.h"
00045 #include "me_fullfast.h"
00046 #include "symbol.h"
00047 #include "rdoq.h"
00048 #include "mv-search.h"
00049 #include "conformance.h"
00050
00051
00052 #if TRACE
00053 #define TRACE_SE(trace,str) snprintf(trace,TRACESTRING_SIZE,str)
00054 #else
00055 #define TRACE_SE(trace,str)
00056 #endif
00057
00058 const byte QP_SCALE_CR[52]=
00059 {
00060 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,
00061 12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,
00062 28,29,29,30,31,32,32,33,34,34,35,35,36,36,37,37,
00063 37,38,38,38,39,39,39,39
00064 };
00065
00066 static const int block8x8_idx[3][4][4] =
00067 {
00068 { {0, 1, 0, 0},
00069 {2, 3, 0, 0},
00070 {0, 0, 0, 0},
00071 {0, 0, 0, 0},
00072 },
00073 { {0, 1, 0, 0},
00074 {0, 1, 0, 0},
00075 {2, 3, 0, 0},
00076 {2, 3, 0, 0},
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 static int slice_too_big(Slice *currSlice, int rlc_bits);
00086
00087 static int writeChromaIntraPredMode (Slice* currSlice, Macroblock* currMB);
00088 static int writeChromaCoeff (Slice *currSlice, Macroblock* currMB);
00089 static int writeCBPandDquant (Slice* currSlice, Macroblock* currMB);
00090 static int writeMotionInfo2NAL (Slice* currSlice, Macroblock* currMB);
00091
00092
00093 static int **M7 = NULL;
00094 static int **M4 = NULL;
00095 static int ****M0 = NULL;
00096
00097 int allocate_mb_mem(void)
00098 {
00099 int alloc_size = 0;
00100 alloc_size += get_mem2Dint(&M4, BLOCK_SIZE, BLOCK_SIZE);
00101 alloc_size += get_mem4Dint(&M0, BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
00102
00103 return (alloc_size);
00104 }
00105
00106 void free_mb_mem(void)
00107 {
00108 free_mem4Dint(M0);
00109 free_mem2Dint(M4);
00110 }
00111
00112 static int diff [16];
00113 static int diff64[64];
00114
00115 const unsigned char subblk_offset_x[3][8][4] =
00116 {
00117 {
00118 {0, 4, 0, 4},
00119 {0, 4, 0, 4},
00120 {0, 0, 0, 0},
00121 {0, 0, 0, 0},
00122 {0, 0, 0, 0},
00123 {0, 0, 0, 0},
00124 {0, 0, 0, 0},
00125 {0, 0, 0, 0},
00126 },
00127 {
00128 {0, 4, 0, 4},
00129 {0, 4, 0, 4},
00130 {0, 4, 0, 4},
00131 {0, 4, 0, 4},
00132 {0, 0, 0, 0},
00133 {0, 0, 0, 0},
00134 {0, 0, 0, 0},
00135 {0, 0, 0, 0},
00136 },
00137 {
00138 {0, 4, 0, 4},
00139 {8,12, 8,12},
00140 {0, 4, 0, 4},
00141 {8,12, 8,12},
00142 {0, 4, 0, 4},
00143 {8,12, 8,12},
00144 {0, 4, 0, 4},
00145 {8,12, 8,12}
00146 }
00147 };
00148
00149
00150 const unsigned char subblk_offset_y[3][8][4] =
00151 {
00152 {
00153 {0, 0, 4, 4},
00154 {0, 0, 4, 4},
00155 {0, 0, 0, 0},
00156 {0, 0, 0, 0},
00157 {0, 0, 0, 0},
00158 {0, 0, 0, 0},
00159 {0, 0, 0, 0},
00160 {0, 0, 0, 0},
00161 },
00162 {
00163 {0, 0, 4, 4},
00164 {8, 8,12,12},
00165 {0, 0, 4, 4},
00166 {8, 8,12,12},
00167 {0, 0, 0, 0},
00168 {0, 0, 0, 0},
00169 {0, 0, 0, 0},
00170 {0, 0, 0, 0},
00171 },
00172 {
00173 {0, 0, 4, 4},
00174 {0, 0, 4, 4},
00175 {8, 8,12,12},
00176 {8, 8,12,12},
00177 {0, 0, 4, 4},
00178 {0, 0, 4, 4},
00179 {8, 8,12,12},
00180 {8, 8,12,12},
00181 }
00182 };
00183
00184 extern ColocatedParams *Co_located;
00185 extern ColocatedParams *Co_located_JV[MAX_PLANE];
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 void set_MB_parameters (int mb_addr)
00197 {
00198 img->current_mb_nr = mb_addr;
00199
00200 get_mb_block_pos(mb_addr, &img->mb_x, &img->mb_y);
00201
00202 img->block_x = img->mb_x << 2;
00203 img->block_y = img->mb_y << 2;
00204 img->pix_x = img->block_x << 2;
00205 img->pix_y = img->block_y << 2;
00206 img->opix_x = img->pix_x;
00207
00208 if (img->MbaffFrameFlag)
00209 {
00210 if (img->mb_data[mb_addr].mb_field)
00211 {
00212 pCurImg = (mb_addr & 0x01) ? imgData.bot_data[0] : imgData.top_data[0];
00213 pImgOrg[0] = pCurImg;
00214 if ((img->yuv_format != YUV400) && !IS_INDEPENDENT(params))
00215 {
00216 if (mb_addr & 0x01)
00217 {
00218 pImgOrg[1] = imgData.bot_data[1];
00219 pImgOrg[2] = imgData.bot_data[2];
00220 }
00221 else
00222 {
00223 pImgOrg[1] = imgData.top_data[1];
00224 pImgOrg[2] = imgData.top_data[2];
00225 }
00226 }
00227
00228 img->opix_y = (img->mb_y >> 1 ) << 4;
00229 img->mb_data[mb_addr].list_offset = (mb_addr % 2) ? 4 : 2;
00230 }
00231 else
00232 {
00233 pCurImg = imgData.frm_data[0];
00234 pImgOrg[0] = imgData.frm_data[0];
00235
00236 if ((img->yuv_format != YUV400) && !IS_INDEPENDENT(params))
00237 {
00238 pImgOrg[1] = imgData.frm_data[1];
00239 pImgOrg[2] = imgData.frm_data[2];
00240 }
00241
00242 img->opix_y = img->block_y << 2;
00243 img->mb_data[mb_addr].list_offset = 0;
00244 }
00245 }
00246 else
00247 {
00248 img->opix_y = img->block_y << 2;
00249 img->mb_data[mb_addr].list_offset = 0;
00250 }
00251
00252 if (img->yuv_format != YUV400)
00253 {
00254 img->pix_c_x = (img->mb_cr_size_x * img->pix_x) >> 4;
00255 img->pix_c_y = (img->mb_cr_size_y * img->pix_y) >> 4;
00256
00257 img->opix_c_x = (img->mb_cr_size_x * img->opix_x) >> 4;
00258 img->opix_c_y = (img->mb_cr_size_y * img->opix_y) >> 4;
00259 }
00260
00261 }
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271 void next_macroblock(ImageParameters *img, Macroblock *currMB)
00272 {
00273 #if TRACE
00274 int use_bitstream_backing = (params->slice_mode == FIXED_RATE || params->slice_mode == CALL_BACK);
00275 #endif
00276 int *bitCount = currMB->bitcounter;
00277 int i;
00278 StatParameters *cur_stats = &enc_picture->stats;
00279
00280 if (bitCount[BITS_TOTAL_MB] > img->max_bitCount)
00281 printf("Warning!!! Number of bits (%d) of macroblock_layer() data seems to exceed defined limit (%d).\n", bitCount[BITS_TOTAL_MB],img->max_bitCount);
00282
00283
00284 cur_stats->bit_use_mb_type[img->type] += bitCount[BITS_MB_MODE ];
00285 cur_stats->tmp_bit_use_cbp[img->type] += bitCount[BITS_CBP_MB ];
00286 cur_stats->bit_use_coeffC[img->type] += bitCount[BITS_COEFF_UV_MB ];
00287 cur_stats->bit_use_coeff[0][img->type] += bitCount[BITS_COEFF_Y_MB ];
00288 cur_stats->bit_use_coeff[1][img->type] += bitCount[BITS_COEFF_CB_MB ];
00289 cur_stats->bit_use_coeff[2][img->type] += bitCount[BITS_COEFF_CR_MB ];
00290 cur_stats->bit_use_delta_quant[img->type] += bitCount[BITS_DELTA_QUANT_MB];
00291 cur_stats->bit_use_stuffingBits[img->type] += bitCount[BITS_STUFFING ];
00292
00293 if (IS_INTRA(currMB))
00294 {
00295 ++cur_stats->intra_chroma_mode[currMB->c_ipred_mode];
00296
00297
00298 if ((currMB->cbp&15) != 0)
00299 {
00300 ++cur_stats->mode_use_transform[img->type][currMB->mb_type][currMB->luma_transform_size_8x8_flag];
00301 }
00302 }
00303
00304 ++cur_stats->mode_use[img->type][currMB->mb_type];
00305 cur_stats->bit_use_mode[img->type][currMB->mb_type] += bitCount[BITS_INTER_MB];
00306
00307 if (img->type != I_SLICE)
00308 {
00309 if (currMB->mb_type == P8x8)
00310 {
00311 for(i=0;i<4;i++)
00312 {
00313 if (currMB->b8mode[i] > 0)
00314 ++cur_stats->mode_use[img->type][currMB->b8mode[i]];
00315 else
00316 ++cur_stats->b8_mode_0_use[img->type][currMB->luma_transform_size_8x8_flag];
00317
00318 if (currMB->b8mode[i]==4)
00319 {
00320 if ((currMB->luma_transform_size_8x8_flag && (currMB->cbp&15) != 0) || params->Transform8x8Mode == 2)
00321 ++cur_stats->mode_use_transform[img->type][4][1];
00322 else
00323 ++cur_stats->mode_use_transform[img->type][4][0];
00324 }
00325 }
00326 }
00327 else if (currMB->mb_type >= 0 && currMB->mb_type <=3 && ((currMB->cbp&15) != 0))
00328 {
00329 ++cur_stats->mode_use_transform[img->type][currMB->mb_type][currMB->luma_transform_size_8x8_flag];
00330 }
00331 }
00332
00333
00334 cur_stats->quant[img->type] += currMB->qp;
00335 cur_stats->num_macroblocks[img->type]++;
00336 }
00337
00338 void set_chroma_qp(ImageParameters *img, Macroblock* currMB)
00339 {
00340 int i;
00341 for (i=0; i<2; i++)
00342 {
00343 currMB->qpc[i] = iClip3 ( -img->bitdepth_chroma_qp_scale, 51, currMB->qp + img->chroma_qp_offset[i] );
00344 currMB->qpc[i] = currMB->qpc[i] < 0 ? currMB->qpc[i] : QP_SCALE_CR[currMB->qpc[i]];
00345 currMB->qp_scaled[i + 1] = currMB->qpc[i] + img->bitdepth_chroma_qp_scale;
00346 }
00347 }
00348
00349
00350
00351
00352
00353
00354
00355 void update_qp(ImageParameters *img, Macroblock *currMB)
00356 {
00357 currMB->qp_scaled[0] = currMB->qp + img->bitdepth_luma_qp_scale;
00358 set_chroma_qp(img, currMB);
00359
00360 select_dct(img, currMB);
00361 }
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374 void reset_macroblock(ImageParameters *img, Macroblock *currMB, int prev_mb)
00375 {
00376 int i,j,l;
00377
00378
00379 for (l=0; l<2; l++)
00380 {
00381 for (j=img->block_y; j < img->block_y + BLOCK_MULTIPLE; j++)
00382 {
00383 memset(&enc_picture->motion.ref_idx[l][j][img->block_x], -1, BLOCK_MULTIPLE * sizeof(char));
00384 memset(enc_picture->motion.mv [l][j][img->block_x], 0, 2 * BLOCK_MULTIPLE * sizeof(short));
00385 for (i=img->block_x; i < img->block_x + BLOCK_MULTIPLE; i++)
00386 enc_picture->motion.ref_pic_id[l][j][i]= -1;
00387 }
00388 }
00389
00390
00391 currMB->mb_type = 0;
00392 currMB->cbp_blk = 0;
00393 currMB->cbp = 0;
00394 currMB->c_ipred_mode = DC_PRED_8;
00395
00396 cmp_cbp[1] = cmp_cbp[2] = curr_cbp[0] = curr_cbp[1] = 0;
00397
00398 memset(currMB->cbp_bits , 0, 3 * sizeof(int64));
00399 memset(currMB->cbp_bits_8x8, 0, 3 * sizeof(int64));
00400
00401 memset (currMB->mvd, 0, BLOCK_CONTEXT * sizeof(short));
00402 memset (currMB->intra_pred_modes, DC_PRED, MB_BLOCK_PARTITIONS * sizeof(char));
00403 memset (currMB->intra_pred_modes8x8, DC_PRED, MB_BLOCK_PARTITIONS * sizeof(char));
00404 memset (currMB->bipred_me, 0, 4* sizeof(short));
00405
00406 currMB->write_mb = FALSE;
00407
00408
00409
00410 if (params->UseConstrainedIntraPred)
00411 {
00412 img->intra_block[img->current_mb_nr] = 1;
00413 }
00414
00415
00416
00417 if ((prev_mb < 0) || (currMB->slice_nr == img->mb_data[prev_mb].slice_nr))
00418 {
00419 currMB->bitcounter[BITS_HEADER] = 0;
00420 }
00421
00422 currMB->bitcounter[BITS_MB_MODE ] = 0;
00423 currMB->bitcounter[BITS_INTER_MB ] = 0;
00424 currMB->bitcounter[BITS_CBP_MB ] = 0;
00425 currMB->bitcounter[BITS_COEFF_Y_MB ] = 0;
00426 currMB->bitcounter[BITS_COEFF_UV_MB ] = 0;
00427 currMB->bitcounter[BITS_COEFF_CB_MB ] = 0;
00428 currMB->bitcounter[BITS_COEFF_CR_MB ] = 0;
00429 currMB->bitcounter[BITS_DELTA_QUANT_MB] = 0;
00430 currMB->bitcounter[BITS_STUFFING ] = 0;
00431 }
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449 void start_macroblock(ImageParameters *img, Slice *currSlice, Macroblock **currMB, int mb_addr, byte mb_field)
00450 {
00451 int i, mb_qp;
00452 int use_bitstream_backing = (params->slice_mode == FIXED_RATE || params->slice_mode == CALL_BACK);
00453
00454 static Macroblock * last_coded_mb = NULL;
00455
00456 DataPartition *dataPart;
00457 Bitstream *currStream;
00458 int prev_mb;
00459
00460 *currMB = &img->mb_data[mb_addr];
00461 (*currMB)->mb_nr = mb_addr;
00462 (*currMB)->mbAddrX = mb_addr;
00463
00464
00465 (*currMB)->mb_field = mb_field;
00466
00467 enc_picture->motion.mb_field[mb_addr] = mb_field;
00468 (*currMB)->is_field_mode = (img->field_picture || ( img->MbaffFrameFlag && (*currMB)->mb_field));
00469
00470 set_MB_parameters (mb_addr);
00471
00472 prev_mb = FmoGetPreviousMBNr(mb_addr);
00473
00474 if(use_bitstream_backing)
00475 {
00476 if ((!params->MbInterlace)||((mb_addr & 0x01)==0))
00477 {
00478
00479 if(!img->cod_counter)
00480 {
00481 for (i=0; i<currSlice->max_part_nr; i++)
00482 {
00483 dataPart = &(currSlice->partArr[i]);
00484 currStream = dataPart->bitstream;
00485 currStream->stored_bits_to_go = currStream->bits_to_go;
00486 currStream->stored_byte_pos = currStream->byte_pos;
00487 currStream->stored_byte_buf = currStream->byte_buf;
00488 stats->stored_bit_slice = stats->bit_slice;
00489
00490 if (currSlice->symbol_mode ==CABAC)
00491 {
00492 dataPart->ee_recode = dataPart->ee_cabac;
00493 }
00494 }
00495 }
00496 }
00497 }
00498
00499
00500
00501 (*currMB)->slice_nr = img->current_slice_nr;
00502
00503
00504
00505 (*currMB)->qpsp = img->qpsp;
00506
00507 if (prev_mb > -1 && (img->mb_data[prev_mb].slice_nr == img->current_slice_nr))
00508 {
00509 (*currMB)->PrevMB = &img->mb_data[prev_mb];
00510 (*currMB)->prev_qp = (*currMB)->PrevMB->qp;
00511 (*currMB)->prev_dqp = (*currMB)->PrevMB->delta_qp;
00512 }
00513 else
00514 {
00515 (*currMB)->PrevMB = NULL;
00516 (*currMB)->prev_qp = currSlice->qp;
00517 (*currMB)->prev_dqp = 0;
00518 }
00519
00520 if(params->RCEnable && (last_coded_mb != *currMB))
00521 {
00522 mb_qp = rc_handle_mb( prev_mb, *currMB, currSlice );
00523 }
00524 else
00525 {
00526 mb_qp = img->qp;
00527
00528 }
00529 last_coded_mb = *currMB;
00530
00531 mb_qp = iClip3(-img->bitdepth_luma_qp_scale, 51, mb_qp);
00532 (*currMB)->qp = img->qp = mb_qp;
00533 (*currMB)->delta_qp = (*currMB)->qp - (*currMB)->prev_qp;
00534
00535 update_qp (img, *currMB);
00536
00537
00538 if (active_pps->deblocking_filter_control_present_flag)
00539 {
00540 (*currMB)->DFDisableIdc = img->DFDisableIdc;
00541 (*currMB)->DFAlphaC0Offset = img->DFAlphaC0Offset;
00542 (*currMB)->DFBetaOffset = img->DFBetaOffset;
00543 }
00544 else
00545 {
00546 (*currMB)->DFDisableIdc = 0;
00547 (*currMB)->DFAlphaC0Offset = 0;
00548 (*currMB)->DFBetaOffset = 0;
00549 }
00550
00551
00552 CheckAvailabilityOfNeighbors(*currMB);
00553
00554 if (currSlice->symbol_mode == CABAC)
00555 CheckAvailabilityOfNeighborsCABAC(*currMB);
00556
00557 reset_macroblock(img, *currMB, prev_mb);
00558
00559 if ((params->SearchMode == FAST_FULL_SEARCH) && (!params->IntraProfile))
00560 ResetFastFullIntegerSearch ();
00561
00562
00563
00564 #if TRACE
00565 for (i=0; i<currSlice->max_part_nr; i++ )
00566 {
00567 currSlice->partArr[i].bitstream->trace_enabled = FALSE;
00568 }
00569 #endif
00570 }
00571
00572
00573
00574
00575
00576
00577
00578
00579 void end_macroblock(ImageParameters *img,
00580 Slice *currSlice,
00581 Macroblock *currMB,
00582 Boolean *end_of_slice,
00583 Boolean *recode_macroblock
00584 )
00585 {
00586 int i;
00587 SyntaxElement se;
00588 int *partMap = assignSE2partition[params->partition_mode];
00589 DataPartition *dataPart;
00590 Bitstream *currStream;
00591 int rlc_bits=0;
00592 int use_bitstream_backing = (params->slice_mode == FIXED_RATE || params->slice_mode == CALL_BACK);
00593 static int skip = FALSE;
00594 int new_slice = 0;
00595
00596
00597
00598 if ((currMB->mb_nr == 0) || currMB->PrevMB == NULL || ( currMB->PrevMB->slice_nr != img->current_slice_nr ))
00599 new_slice=1;
00600
00601 *recode_macroblock=FALSE;
00602
00603 switch(params->slice_mode)
00604 {
00605 case NO_SLICES:
00606 currSlice->num_mb++;
00607 *recode_macroblock = FALSE;
00608 if ((currSlice->num_mb) == (int)img->PicSizeInMbs)
00609 *end_of_slice = TRUE;
00610
00611
00612 *end_of_slice = (Boolean) (*end_of_slice | (currMB->mb_nr == FmoGetLastCodedMBOfSliceGroup (FmoMB2SliceGroup (currMB->mb_nr))));
00613
00614 break;
00615 case FIXED_MB:
00616
00617 currSlice->num_mb++;
00618 *recode_macroblock = FALSE;
00619
00620 *end_of_slice = (Boolean) (currMB->mb_nr == FmoGetLastCodedMBOfSliceGroup (FmoMB2SliceGroup (currMB->mb_nr)));
00621
00622 *end_of_slice = (Boolean) (*end_of_slice | (currSlice->num_mb >= params->slice_argument));
00623
00624 break;
00625
00626
00627
00628
00629 case FIXED_RATE:
00630
00631
00632 if(img->cod_counter)
00633 {
00634
00635 se.value1 = img->cod_counter;
00636 se.value2 = 0;
00637 se.type = SE_MBTYPE;
00638 dataPart = &(currSlice->partArr[partMap[se.type]]);
00639
00640 TRACE_SE (se.tracestring, "mb_skip_run");
00641 writeSE_UVLC(&se, dataPart);
00642 rlc_bits=se.len;
00643
00644 currStream = dataPart->bitstream;
00645
00646 currStream->bits_to_go_skip = currStream->bits_to_go;
00647 currStream->byte_pos_skip = currStream->byte_pos;
00648 currStream->byte_buf_skip = currStream->byte_buf;
00649
00650 currStream->bits_to_go = currStream->stored_bits_to_go;
00651 currStream->byte_pos = currStream->stored_byte_pos;
00652 currStream->byte_buf = currStream->stored_byte_buf;
00653 skip = TRUE;
00654 }
00655
00656
00657 if (!new_slice)
00658 {
00659 if(slice_too_big(currSlice, rlc_bits))
00660 {
00661 *recode_macroblock = TRUE;
00662 *end_of_slice = TRUE;
00663 }
00664 else if(!img->cod_counter)
00665 skip = FALSE;
00666 }
00667
00668
00669
00670 if ((*recode_macroblock == FALSE) && (currMB->mb_nr == FmoGetLastCodedMBOfSliceGroup (FmoMB2SliceGroup (currMB->mb_nr))))
00671 {
00672 *end_of_slice = TRUE;
00673 if(!img->cod_counter)
00674 skip = FALSE;
00675 }
00676
00677
00678 if (new_slice && slice_too_big(currSlice, rlc_bits))
00679 {
00680 *end_of_slice = TRUE;
00681 if(!img->cod_counter)
00682 skip = FALSE;
00683 }
00684 if (!*recode_macroblock)
00685 currSlice->num_mb++;
00686 break;
00687
00688 case CALL_BACK:
00689 if (currMB->mb_nr > 0 && !new_slice)
00690 {
00691 if (currSlice->slice_too_big(rlc_bits))
00692 {
00693 *recode_macroblock = TRUE;
00694 *end_of_slice = TRUE;
00695 }
00696 }
00697
00698 if ( (*recode_macroblock == FALSE) && (currMB->mb_nr == FmoGetLastCodedMBOfSliceGroup (FmoMB2SliceGroup (currMB->mb_nr))))
00699 *end_of_slice = TRUE;
00700 break;
00701
00702 default:
00703 snprintf(errortext, ET_SIZE, "Slice Mode %d not supported", params->slice_mode);
00704 error(errortext, 600);
00705 }
00706
00707 if (*recode_macroblock == TRUE)
00708 {
00709
00710 for (i=0; i<currSlice->max_part_nr; i++)
00711 {
00712 dataPart = &(currSlice->partArr[i]);
00713 currStream = dataPart->bitstream;
00714 currStream->bits_to_go = currStream->stored_bits_to_go;
00715 currStream->byte_pos = currStream->stored_byte_pos;
00716 currStream->byte_buf = currStream->stored_byte_buf;
00717 stats->bit_slice = stats->stored_bit_slice;
00718
00719 if (currSlice->symbol_mode == CABAC)
00720 {
00721 dataPart->ee_cabac = dataPart->ee_recode;
00722 }
00723 }
00724 }
00725
00726 if (currSlice->symbol_mode == CAVLC)
00727 {
00728
00729 dataPart = &(currSlice->partArr[partMap[SE_MBTYPE]]);
00730 if(*end_of_slice == TRUE && skip == TRUE)
00731 {
00732
00733
00734 if(img->cod_counter && *recode_macroblock == TRUE)
00735 {
00736
00737
00738 img->cod_counter--;
00739 if(img->cod_counter)
00740 {
00741 se.value1 = img->cod_counter;
00742 se.value2 = 0;
00743 se.type = SE_MBTYPE;
00744 #if TRACE
00745 snprintf(se.tracestring, TRACESTRING_SIZE, "Final MB runlength = %3d",img->cod_counter);
00746 #endif
00747 writeSE_UVLC(&se, dataPart);
00748 rlc_bits=se.len;
00749 currMB->bitcounter[BITS_MB_MODE]+=rlc_bits;
00750 img->cod_counter = 0;
00751 }
00752 }
00753 else
00754 {
00755 currStream = dataPart->bitstream;
00756
00757 currStream->bits_to_go = currStream->bits_to_go_skip;
00758 currStream->byte_pos = currStream->byte_pos_skip;
00759 currStream->byte_buf = currStream->byte_buf_skip;
00760
00761
00762 img->cod_counter = 0;
00763 skip = FALSE;
00764 }
00765 }
00766
00767
00768 if(*end_of_slice == TRUE && img->cod_counter && !use_bitstream_backing)
00769 {
00770 se.value1 = img->cod_counter;
00771 se.value2 = 0;
00772 se.type = SE_MBTYPE;
00773
00774 TRACE_SE (se.tracestring, "mb_skip_run");
00775 writeSE_UVLC(&se, dataPart);
00776
00777 rlc_bits=se.len;
00778 currMB->bitcounter[BITS_MB_MODE]+=rlc_bits;
00779 img->cod_counter = 0;
00780 }
00781 }
00782 }
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805 int slice_too_big(Slice *currSlice, int rlc_bits)
00806 {
00807 DataPartition *dataPart;
00808 Bitstream *currStream;
00809 EncodingEnvironmentPtr eep;
00810 int i;
00811 int size_in_bytes;
00812
00813
00814 if (currSlice->symbol_mode == CAVLC)
00815 {
00816 for (i = 0; i < currSlice->max_part_nr; i++)
00817 {
00818 dataPart = &(currSlice->partArr[i]);
00819 currStream = dataPart->bitstream;
00820 size_in_bytes = currStream->byte_pos ;
00821
00822 if (currStream->bits_to_go < 8)
00823 size_in_bytes++;
00824 if (currStream->bits_to_go < rlc_bits)
00825 size_in_bytes++;
00826 if(size_in_bytes > params->slice_argument)
00827 return TRUE;
00828 }
00829 }
00830 else
00831 {
00832 for (i=0; i<currSlice->max_part_nr; i++)
00833 {
00834 dataPart= &(currSlice->partArr[i]);
00835 eep = &(dataPart->ee_cabac);
00836
00837 if( arienco_bits_written(eep) > (params->slice_argument*8))
00838 return TRUE;
00839 }
00840 }
00841
00842 return FALSE;
00843 }
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855 int LumaResidualCoding8x8 ( Macroblock* currMB,
00856 int *cbp,
00857 int64 *cbp_blk,
00858 int block8x8,
00859 short p_dir,
00860 int l0_mode,
00861 int l1_mode,
00862 short l0_ref_idx,
00863 short l1_ref_idx,
00864 int is_cavlc
00865 )
00866 {
00867 int block_y, block_x, pic_pix_y, pic_pix_x, i, j, nonzero = 0, cbp_blk_mask;
00868 int coeff_cost = 0;
00869 int mb_y = (block8x8 >> 1) << 3;
00870 int mb_x = (block8x8 & 0x01) << 3;
00871 int cbp_mask = 1 << block8x8;
00872 int bxx, byy;
00873 int skipped = (l0_mode == 0 && l1_mode == 0 && (img->type != B_SLICE));
00874
00875
00876 int need_8x8_transform = currMB->luma_transform_size_8x8_flag;
00877 int uv, nonzerocr[3]={0,0,0};
00878 short bipred_me = currMB->bipred_me[block8x8];
00879 coeff_cost_cr[1] = coeff_cost_cr[2] = 0;
00880
00881
00882 if(!need_8x8_transform)
00883 {
00884 if (((p_dir == 0 || p_dir == 2 )&& l0_mode < 5) || ((p_dir == 1 || p_dir == 2 ) && l1_mode < 5))
00885 {
00886 LumaPrediction (currMB, mb_x, mb_y, 8, 8, p_dir, l0_mode, l1_mode, l0_ref_idx, l1_ref_idx, bipred_me);
00887
00888
00889 ComputeResidue (pCurImg, img->mb_pred[0], img->mb_ores[0], mb_y, mb_x, img->opix_y, img->opix_x + mb_x, 8, 8);
00890 }
00891
00892 for (byy=0, block_y=mb_y; block_y<mb_y+8; byy+=4, block_y+=4)
00893 {
00894 for (bxx=0, block_x=mb_x; block_x<mb_x+8; bxx+=4, block_x+=4)
00895 {
00896 pic_pix_x = img->opix_x + block_x;
00897 cbp_blk_mask = (block_x >> 2) + block_y;
00898
00899
00900 if (!(((p_dir == 0 || p_dir == 2 )&& l0_mode < 5) || ((p_dir == 1 || p_dir == 2 ) && l1_mode < 5)))
00901 {
00902 LumaPrediction (currMB, block_x, block_y, 4, 4, p_dir, l0_mode, l1_mode, l0_ref_idx, l1_ref_idx, bipred_me);
00903
00904
00905 ComputeResidue(pCurImg, img->mb_pred[0], img->mb_ores[0], block_y, block_x, img->opix_y, pic_pix_x, 4, 4);
00906 }
00907
00908 if (img->P444_joined)
00909 {
00910 for (uv = PLANE_U; uv <= PLANE_V; uv++)
00911 {
00912 select_plane((ColorPlane) (uv));
00913 ChromaPrediction (currMB, uv - 1, block_x, block_y, 4, 4, p_dir, l0_mode, l1_mode, l0_ref_idx, l1_ref_idx, bipred_me);
00914
00915
00916 ComputeResidue(pImgOrg[uv], img->mb_pred[uv], img->mb_ores[uv], block_y, block_x, img->opix_y, pic_pix_x, 4, 4);
00917 }
00918 select_plane(PLANE_Y);
00919 }
00920
00921
00922 if (!skipped && ( (img->NoResidueDirect != 1) || (currMB->qp_scaled[0] == 0 && img->lossless_qpprime_flag == 1) ))
00923 {
00924
00925 nonzero = pDCT_4x4 (currMB, PLANE_Y, block_x, block_y, &coeff_cost, 0, is_cavlc);
00926
00927 if (nonzero)
00928 {
00929 (*cbp_blk) |= (int64)1 << cbp_blk_mask;
00930 (*cbp) |= cbp_mask;
00931 }
00932
00933 if (img->P444_joined)
00934 {
00935 if (img->type!=SP_SLICE)
00936 {
00937 for (uv = PLANE_U; uv <= PLANE_V; uv++)
00938 {
00939 select_plane((ColorPlane) uv);
00940 nonzerocr[uv] = pDCT_4x4( currMB, (ColorPlane) uv, block_x, block_y, &coeff_cost_cr[uv], 0, is_cavlc);
00941 if (nonzerocr[uv])
00942 {
00943 (cur_cbp_blk[uv]) |= (int64) 1 << cbp_blk_mask;
00944 (cmp_cbp[uv]) |= cbp_mask;
00945 }
00946 }
00947 select_plane(PLANE_Y);
00948 }
00949 else
00950 {
00951 assert(img->type==SP_SLICE);
00952 }
00953 }
00954 }
00955 }
00956 }
00957 }
00958 else
00959 {
00960 for (block_y = mb_y; block_y < mb_y + 8; block_y += 8)
00961 {
00962 pic_pix_y = img->opix_y + block_y;
00963
00964 for (block_x = mb_x; block_x < mb_x + 8; block_x += 8)
00965 {
00966 pic_pix_x = img->opix_x + block_x;
00967
00968 cbp_blk_mask = (block_x>>2) + block_y;
00969
00970
00971 LumaPrediction (currMB, block_x, block_y, 8, 8, p_dir, l0_mode, l1_mode, l0_ref_idx, l1_ref_idx, bipred_me);
00972
00973
00974
00975 ComputeResidue (pCurImg, img->mb_pred[0], img->mb_ores[0], block_y, block_x, img->opix_y, pic_pix_x, 8, 8);
00976
00977 if (img->P444_joined)
00978 {
00979 for (uv = PLANE_U; uv <= PLANE_V; uv++)
00980 {
00981 select_plane((ColorPlane) uv);
00982 ChromaPrediction (currMB, uv - 1, block_x, block_y, 8, 8, p_dir, l0_mode, l1_mode, l0_ref_idx, l1_ref_idx, bipred_me);
00983
00984
00985 ComputeResidue (pImgOrg[uv], img->mb_pred[uv], img->mb_ores[uv], block_y, block_x, img->opix_y, pic_pix_x, 8, 8);
00986 }
00987 select_plane(PLANE_Y);
00988 }
00989 }
00990 }
00991
00992 if (img->NoResidueDirect != 1 && !skipped)
00993 {
00994 if (img->type!=SP_SLICE)
00995 nonzero = pDCT_8x8 (currMB, PLANE_Y, block8x8, &coeff_cost, 0);
00996
00997 if (nonzero)
00998 {
00999 (*cbp_blk) |= 51 << (4*block8x8 - 2*(block8x8 & 0x01));
01000 (*cbp) |= cbp_mask;
01001 }
01002
01003 if(img->P444_joined)
01004 {
01005 if (img->type!=SP_SLICE)
01006 {
01007 for (uv = PLANE_U; uv <= PLANE_V; uv++)
01008 {
01009 select_plane((ColorPlane) uv);
01010 nonzerocr[uv] = pDCT_8x8( currMB, (ColorPlane) uv, block8x8, &coeff_cost_cr[uv], 0);
01011 if (nonzerocr[uv])
01012 {
01013 (cur_cbp_blk[uv]) |= 51 << (4*block8x8-2*(block8x8 & 0x01));
01014 (cmp_cbp[uv]) |= cbp_mask;
01015 }
01016 }
01017 select_plane(PLANE_Y);
01018 }
01019 }
01020 }
01021 }
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043 if (img->NoResidueDirect != 1 && !skipped && coeff_cost <= _LUMA_COEFF_COST_ &&
01044 ((currMB->qp_scaled[0])!=0 || img->lossless_qpprime_flag==0)&&
01045 !(img->type==SP_SLICE && (si_frame_indicator==1 || sp2_frame_indicator==1 )))
01046
01047 {
01048 coeff_cost = 0;
01049 (*cbp) &= (63 - cbp_mask);
01050 (*cbp_blk) &= ~(51 << (4*block8x8-2*(block8x8 & 0x01)));
01051
01052 memset( img->cofAC[block8x8][0][0], 0, 4 * 2 * 65 * sizeof(int));
01053
01054 for (j=mb_y; j<mb_y+8; j++)
01055 memcpy(&enc_picture->imgY[img->pix_y + j][img->pix_x + mb_x], &img->mb_pred[0][j][mb_x], 2 * BLOCK_SIZE * sizeof(imgpel));
01056
01057 if (img->type==SP_SLICE)
01058 {
01059 for (i=mb_x; i < mb_x + BLOCK_SIZE_8x8; i+=BLOCK_SIZE)
01060 for (j=mb_y; j < mb_y + BLOCK_SIZE_8x8; j+=BLOCK_SIZE)
01061 copyblock_sp(currMB, PLANE_Y, i, j);
01062 }
01063 }
01064
01065 if(img->P444_joined)
01066 {
01067 for (uv = PLANE_U; uv <= PLANE_V; uv++)
01068 {
01069 if (img->NoResidueDirect != 1 && !skipped && coeff_cost_cr[uv] <= _LUMA_COEFF_COST_ &&
01070 (currMB->qp_scaled[uv]!=0 || img->lossless_qpprime_flag==0))
01071 {
01072 coeff_cost_cr[uv] = 0;
01073 cmp_cbp[uv] &= (63 - cbp_mask);
01074 cur_cbp_blk[uv] &= ~(51 << (4*block8x8-2*(block8x8 & 0x01)));
01075
01076 memset( img->cofAC[block8x8 + 4 * uv][0][0], 0, 4 * 2 * 65 * sizeof(int));
01077
01078 for (j=mb_y; j<mb_y+8; j++)
01079 memcpy(&enc_picture->imgUV[uv - 1][img->pix_y + j][img->pix_x + mb_x], &img->mb_pred[uv][j][mb_x], 2 * BLOCK_SIZE * sizeof(imgpel));
01080 }
01081 }
01082 }
01083
01084 return coeff_cost;
01085 }
01086
01087
01088
01089
01090
01091
01092
01093 void SetModesAndRefframe (Macroblock* currMB, int b8, short* p_dir, int list_mode[2], short list_ref_idx[2])
01094 {
01095 int j = 2*(b8>>1);
01096 int i = 2*(b8 & 0x01);
01097
01098 list_mode[0] = list_mode[1] = list_ref_idx[0] = list_ref_idx[1] = -1;
01099
01100 *p_dir = currMB->b8pdir[b8];
01101
01102 if (img->type!=B_SLICE)
01103 {
01104 list_ref_idx[0] = enc_picture->motion.ref_idx[LIST_0][img->block_y+j][img->block_x+i];
01105 list_ref_idx[1] = 0;
01106 list_mode[0] = currMB->b8mode[b8];
01107 list_mode[1] = 0;
01108 }
01109 else
01110 {
01111 if (currMB->b8pdir[b8]==-1)
01112 {
01113 list_ref_idx[0] = -1;
01114 list_ref_idx[1] = -1;
01115 list_mode[0] = 0;
01116 list_mode[1] = 0;
01117 }
01118 else if (currMB->b8pdir[b8]==0)
01119 {
01120 list_ref_idx[0] = enc_picture->motion.ref_idx[LIST_0][img->block_y+j][img->block_x+i];
01121 list_ref_idx[1] = 0;
01122 list_mode[0] = currMB->b8mode[b8];
01123 list_mode[1] = 0;
01124 }
01125 else if (currMB->b8pdir[b8]==1)
01126 {
01127 list_ref_idx[0] = 0;
01128 list_ref_idx[1] = enc_picture->motion.ref_idx[LIST_1][img->block_y+j][img->block_x+i];
01129 list_mode[0] = 0;
01130 list_mode[1] = currMB->b8mode[b8];
01131 }
01132 else
01133 {
01134 list_ref_idx[0] = enc_picture->motion.ref_idx[LIST_0][img->block_y+j][img->block_x+i];
01135 list_ref_idx[1] = enc_picture->motion.ref_idx[LIST_1][img->block_y+j][img->block_x+i];
01136 list_mode[0] = currMB->b8mode[b8];
01137 list_mode[1] = currMB->b8mode[b8];
01138 }
01139 }
01140 }
01141
01142
01143
01144
01145
01146
01147
01148
01149 void LumaResidualCoding (Macroblock *currMB, int is_cavlc)
01150 {
01151 int uv, i,j,block8x8,b8_x,b8_y;
01152 int list_mode[2];
01153 short list_ref_idx[2];
01154 short p_dir;
01155 int sum_cnt_nonz[3] = {0 ,0, 0};
01156 int is_skip = img->type == P_SLICE && currMB->mb_type == PSKIP;
01157
01158
01159 currMB->cbp = 0;
01160 currMB->cbp_blk = 0;
01161 cmp_cbp[1] = cmp_cbp[2] = 0;
01162 cur_cbp_blk[1] = cur_cbp_blk[2] = 0;
01163
01164 for (block8x8=0; block8x8<4; block8x8++)
01165 {
01166 SetModesAndRefframe (currMB, block8x8, &p_dir, list_mode, list_ref_idx);
01167
01168 sum_cnt_nonz[0] += LumaResidualCoding8x8 (currMB, &(currMB->cbp), &(currMB->cbp_blk), block8x8,
01169 p_dir, list_mode[0], list_mode[1], list_ref_idx[0], list_ref_idx[1], is_cavlc);
01170
01171 if(img->P444_joined)
01172 {
01173 sum_cnt_nonz[1] += coeff_cost_cr[1];
01174 sum_cnt_nonz[2] += coeff_cost_cr[2];
01175 }
01176 }
01177
01178 if ((is_skip ||
01179 (sum_cnt_nonz[0] <= _LUMA_MB_COEFF_COST_ && ((currMB->qp_scaled[0])!=0 || img->lossless_qpprime_flag==0))) &&
01180 !(img->type==SP_SLICE && (si_frame_indicator==1 || sp2_frame_indicator==1)))
01181
01182 {
01183 currMB->cbp &= 0xfffff0 ;
01184 currMB->cbp_blk &= 0xff0000 ;
01185 for (j=0; j < MB_BLOCK_SIZE; j++)
01186 memcpy(&enc_picture->imgY[img->pix_y+j][img->pix_x], img->mb_pred[0][j], MB_BLOCK_SIZE * sizeof (imgpel));
01187
01188 memset( img->cofAC[0][0][0], 0, 4 * 4 * 2 * 65 * sizeof(int));
01189
01190 if (img->type==SP_SLICE)
01191 {
01192 for(block8x8=0;block8x8<4;block8x8++)
01193 {
01194 b8_x=(block8x8&1)<<3;
01195 b8_y=(block8x8&2)<<2;
01196 for (i = b8_x; i < b8_x + BLOCK_SIZE_8x8; i += 4)
01197 for (j = b8_y; j < b8_y + BLOCK_SIZE_8x8;j += 4)
01198 copyblock_sp(currMB, PLANE_Y, i, j);
01199 }
01200 }
01201 }
01202
01203 if (img->P444_joined)
01204 {
01205 for (uv = PLANE_U; uv <= PLANE_V; uv++)
01206 {
01207 if(is_skip || (sum_cnt_nonz[uv] <= _LUMA_MB_COEFF_COST_ &&
01208 ((currMB->qp_scaled[uv])!=0 ||img->lossless_qpprime_flag==0)))
01209 {
01210 cmp_cbp[uv] &= 0xfffff0 ;
01211 cur_cbp_blk[uv] &= 0xff0000 ;
01212 for (j=0; j < MB_BLOCK_SIZE; j++)
01213 memcpy(&enc_picture->p_img[uv][img->pix_y+j][img->pix_x], img->mb_pred[uv][j], MB_BLOCK_SIZE * sizeof (imgpel));
01214
01215 memset( img->cofAC[4 * uv][0][0], 0, 4 * 4 * 2 * 65 * sizeof(int));
01216 }
01217 currMB->cbp |= cmp_cbp[uv];
01218 }
01219 }
01220 }
01221
01222
01223
01224
01225
01226
01227
01228
01229 int TransformDecision (Macroblock *currMB, int block_check, int *cost)
01230 {
01231 int block_y, block_x, i, j, k;
01232 int mb_y, mb_x, block8x8;
01233 int list_mode[2];
01234 short list_ref_idx[2], p_dir;
01235 int num_blks;
01236 int cost8x8=0, cost4x4=0;
01237 int *diff_ptr;
01238 short bipred_me;
01239 imgpel **mb_pred = img->mb_pred[0];
01240
01241 if(block_check==-1)
01242 {
01243 block8x8 = 0;
01244 num_blks = 4;
01245 }
01246 else
01247 {
01248 block8x8 = block_check;
01249 num_blks = block_check + 1;
01250 }
01251
01252 for (; block8x8<num_blks; block8x8++)
01253 {
01254 SetModesAndRefframe (currMB, block8x8, &p_dir, list_mode, list_ref_idx);
01255 bipred_me = currMB->bipred_me[block8x8];
01256 mb_y = (block8x8 >> 1) << 3;
01257 mb_x = (block8x8 & 0x01) << 3;
01258
01259
01260 LumaPrediction (currMB, mb_x, mb_y, 8, 8, p_dir, list_mode[0], list_mode[1], list_ref_idx[0], list_ref_idx[1], bipred_me);
01261
01262
01263 for (k=0, block_y=mb_y; block_y<mb_y+8; block_y+=4)
01264 {
01265 for (block_x=mb_x; block_x<mb_x+8; block_x+=4)
01266 {
01267
01268 diff_ptr=&diff64[k];
01269 for (j=block_y; j<block_y + 4; j++)
01270 {
01271 for (i = block_x; i < block_x + 4; i++, k++)
01272 diff64[k] = pCurImg[img->opix_y+j][img->opix_x + i] - mb_pred[j][i];
01273 }
01274 cost4x4 += distortion4x4 (diff_ptr);
01275 }
01276 }
01277 cost8x8 += distortion8x8 (diff64);
01278 }
01279
01280 if(params->Transform8x8Mode==2)
01281 return 1;
01282 else if(cost8x8 < cost4x4)
01283 return 1;
01284 else
01285 {
01286 *cost += (cost4x4 - cost8x8);
01287 return 0;
01288 }
01289 }
01290
01291
01292
01293
01294
01295
01296
01297
01298
01299 void ChromaResidualCoding (Macroblock *currMB, int is_cavlc)
01300 {
01301 int chroma_cbp;
01302 static int uv, block8, block_y, block_x, j, i;
01303 static int list_mode[2];
01304 static short p_dir, list_ref_idx[2];
01305 int skipped = (currMB->mb_type == 0 && (img->type == P_SLICE || img->type == SP_SLICE));
01306 int yuv = img->yuv_format - 1;
01307 short bipred_me;
01308 imgpel **mb_pred = NULL;
01309
01310 for (chroma_cbp = 0, uv=0; uv<2; uv++)
01311 {
01312 mb_pred = img->mb_pred[ uv + 1];
01313
01314 block8 = 0;
01315 for (block_y=0; block_y < img->mb_cr_size_y; block_y+=4)
01316 for (block_x=0; block_x < img->mb_cr_size_x; block_x+=4)
01317 {
01318 block8 = block8x8_idx[yuv][block_y>>2][block_x>>2];
01319 bipred_me = currMB->bipred_me[block8];
01320 SetModesAndRefframe (currMB, block8, &p_dir, list_mode, list_ref_idx);
01321
01322 ChromaPrediction4x4 (currMB, uv, block_x, block_y, p_dir, list_mode[0], list_mode[1], list_ref_idx[0], list_ref_idx[1], bipred_me);
01323
01324 }
01325
01326
01327 if (img->NoResidueDirect)
01328 {
01329 for (j=0; j<img->mb_cr_size_y; j++)
01330 memcpy(&enc_picture->imgUV[uv][img->pix_c_y+j][img->pix_c_x], mb_pred[j], img->mb_cr_size_x * sizeof(imgpel));
01331 }
01332 else if (skipped && img->type==SP_SLICE)
01333 {
01334 for (j=0; j<img->mb_cr_size_y; j++)
01335 memset(img->mb_ores[uv + 1][j], 0 , img->mb_cr_size_x * sizeof(int));
01336 for (j=0; j<img->mb_cr_size_y; j++)
01337 memset(img->mb_rres[uv + 1][j], 0 , img->mb_cr_size_x * sizeof(int));
01338 }
01339 else if (skipped)
01340 {
01341 for (j=0; j<img->mb_cr_size_y; j++)
01342 memcpy(&enc_picture->imgUV[uv][img->pix_c_y+j][img->pix_c_x], mb_pred[j], img->mb_cr_size_x * sizeof(imgpel));
01343 }
01344 else
01345 {
01346 for (j=0; j<img->mb_cr_size_y; j++)
01347 {
01348 for (i=0; i<img->mb_cr_size_x; i++)
01349 {
01350 img->mb_ores[uv + 1][j][i] = pImgOrg[uv + 1][img->opix_c_y+j][img->opix_c_x+i] - mb_pred[j][i];
01351 }
01352 }
01353 }
01354
01355
01356
01357
01358 if (skipped && img->type==SP_SLICE)
01359 {
01360 if(si_frame_indicator || sp2_frame_indicator)
01361 chroma_cbp = dct_chroma_sp2(currMB, uv,chroma_cbp, is_cavlc);
01362 else
01363 chroma_cbp = dct_chroma_sp(currMB, uv,chroma_cbp, is_cavlc);
01364 }
01365 else
01366 {
01367 if (!img->NoResidueDirect && !skipped)
01368 {
01369 if (img->type!=SP_SLICE || (currMB->mb_type==I16MB ))
01370 {
01371
01372 chroma_cbp = dct_cr_4x4[uv] (currMB, uv,chroma_cbp, is_cavlc);
01373 }
01374 else
01375 {
01376 if(si_frame_indicator||sp2_frame_indicator)
01377 chroma_cbp=dct_chroma_sp2(currMB, uv,chroma_cbp, is_cavlc);
01378 else
01379 chroma_cbp=dct_chroma_sp(currMB, uv,chroma_cbp, is_cavlc);
01380 }
01381 }
01382 }
01383 }
01384
01385
01386 currMB->cbp += ((chroma_cbp)<<4);
01387
01388 }
01389
01390
01391
01392
01393
01394
01395
01396
01397 void IntraChromaRDDecision (Macroblock *currMB, RD_PARAMS enc_mb)
01398 {
01399 int i, j, k;
01400 imgpel** image;
01401 int block_x, block_y;
01402 int mb_available_up;
01403 int mb_available_left[2];
01404 int mb_available_up_left;
01405 int uv;
01406 int mode;
01407 int best_mode = DC_PRED_8;
01408 int cost;
01409 int min_cost;
01410 PixelPos up;
01411 PixelPos left[17];
01412 int cr_MB_x = img->mb_cr_size_x;
01413 int cr_MB_y = img->mb_cr_size_y;
01414 static imgpel *img_org, *img_prd;
01415 static imgpel ***curr_mpr_16x16;
01416
01417 for (i=0;i<cr_MB_y+1;i++)
01418 {
01419 getNeighbour(currMB, -1 , i-1 , img->mb_size[IS_CHROMA], &left[i]);
01420 }
01421 getNeighbour(currMB, 0 , -1 , img->mb_size[IS_CHROMA], &up);
01422
01423 mb_available_up = up.available;
01424 mb_available_up_left = left[0].available;
01425 mb_available_left[0] = mb_available_left[1] = left[1].available;
01426
01427 if(params->UseConstrainedIntraPred)
01428 {
01429 mb_available_up = up.available ? img->intra_block[up.mb_addr] : 0;
01430 for (i=1, mb_available_left[0] = 1; i < (cr_MB_y>>1) + 1; i++)
01431 mb_available_left[0] &= left[i].available ? img->intra_block[left[i].mb_addr]: 0;
01432
01433 for (i=(cr_MB_y>>1) + 1, mb_available_left[1]=1; i < cr_MB_y + 1;i++)
01434 mb_available_left[1] &= left[i].available ? img->intra_block[left[i].mb_addr]: 0;
01435
01436 mb_available_up_left = left[0].available ? img->intra_block[left[0].mb_addr]: 0;
01437 }
01438
01439
01440 min_cost = INT_MAX;
01441 for (i=0;i<cr_MB_y;i++)
01442 {
01443 getNeighbour(currMB, 0, i, img->mb_size[IS_CHROMA], &left[i]);
01444 }
01445 if ( img->MbaffFrameFlag && img->field_mode )
01446 {
01447 for (i=0;i<cr_MB_y;i++)
01448 {
01449 left[i].pos_y = left[i].pos_y >> 1;
01450 }
01451 }
01452
01453 for (mode=DC_PRED_8; mode<=PLANE_8; mode++)
01454 {
01455 if ((mode==VERT_PRED_8 && !mb_available_up) ||
01456 (mode==HOR_PRED_8 && (!mb_available_left[0] || !mb_available_left[1])) ||
01457 (mode==PLANE_8 && (!mb_available_left[0] || !mb_available_left[1] || !mb_available_up || !mb_available_up_left)))
01458 continue;
01459
01460 cost = 0;
01461 for (uv = 1; uv < 3; uv++)
01462 {
01463 image = pImgOrg[uv];
01464 curr_mpr_16x16 = img->mpr_16x16[uv];
01465 for (block_y=0; block_y<cr_MB_y; block_y+=4)
01466 {
01467 for (block_x=0; block_x<cr_MB_x; block_x+=4)
01468 {
01469 for (k=0,j=block_y; j<block_y+4; j++)
01470 {
01471 img_prd = curr_mpr_16x16[mode][j];
01472 img_org = &image[left[j].pos_y][left[j].pos_x];
01473
01474 for (i = block_x; i < block_x + 4; i++)
01475 diff[k++] = img_org[i] - img_prd[i];
01476 }
01477
01478 cost += distortion4x4(diff);
01479 }
01480 if (cost > min_cost) break;
01481 }
01482 if (cost > min_cost) break;
01483 }
01484
01485 cost += (int) (enc_mb.lambda_me[Q_PEL] * mvbits[ mode ]);
01486
01487 if (cost < min_cost)
01488 {
01489 best_mode = mode;
01490 min_cost = cost;
01491 }
01492 }
01493 currMB->c_ipred_mode = best_mode;
01494 }
01495
01496
01497
01498
01499
01500
01501
01502
01503 int ZeroRef (Macroblock* currMB)
01504 {
01505 int i,j;
01506
01507 for (j=img->block_y; j<img->block_y + BLOCK_MULTIPLE; j++)
01508 {
01509 for (i=img->block_x; i<img->block_x + BLOCK_MULTIPLE; i++)
01510 {
01511 if (enc_picture->motion.ref_idx[LIST_0][j][i]!=0)
01512 return 0;
01513 }
01514 }
01515 return 1;
01516 }
01517
01518
01519
01520
01521
01522
01523
01524
01525 int MBType2Value (Macroblock* currMB)
01526 {
01527 static const int dir1offset[3] = { 1, 2, 3};
01528 static const int dir2offset[3][3] = {{ 0, 4, 8},
01529 { 6, 2, 10},
01530 {12, 14, 16}};
01531
01532 int mbtype, pdir0, pdir1;
01533
01534 if (img->type!=B_SLICE)
01535 {
01536 if (currMB->mb_type==I8MB ||currMB->mb_type==I4MB)
01537 return (img->type==I_SLICE ? 0 : 6);
01538 else if (currMB->mb_type==I16MB) return (img->type==I_SLICE ? 0 : 6) + img->i16offset;
01539 else if (currMB->mb_type==IPCM) return (img->type==I_SLICE ? 25 : 31);
01540 else if (currMB->mb_type==P8x8)
01541 {
01542 if (img->currentSlice->symbol_mode==CAVLC
01543 && ZeroRef (currMB)) return 5;
01544 else return 4;
01545 }
01546 else return currMB->mb_type;
01547 }
01548 else
01549 {
01550 mbtype = currMB->mb_type;
01551 pdir0 = currMB->b8pdir[0];
01552 pdir1 = currMB->b8pdir[3];
01553
01554 if (mbtype==0) return 0;
01555 else if (mbtype==I4MB || mbtype==I8MB) return 23;
01556 else if (mbtype==I16MB) return 23 + img->i16offset;
01557 else if (mbtype==IPCM) return 48;
01558 else if (mbtype==P8x8) return 22;
01559 else if (mbtype==1) return dir1offset[pdir0];
01560 else if (mbtype==2) return 4 + dir2offset[pdir0][pdir1];
01561 else return 5 + dir2offset[pdir0][pdir1];
01562 }
01563 }
01564
01565
01566
01567
01568
01569
01570
01571
01572 int writeIntra4x4Modes(Slice *currSlice, Macroblock *currMB)
01573 {
01574 int i;
01575 SyntaxElement se;
01576 int *bitCount = currMB->bitcounter;
01577 const int *partMap = assignSE2partition[params->partition_mode];
01578 DataPartition *dataPart = &(currSlice->partArr[partMap[SE_INTRAPREDMODE]]);
01579
01580 int rate = 0;
01581
01582 currMB->IntraChromaPredModeFlag = 1;
01583
01584 for(i=0;i<16;i++)
01585 {
01586 se.context = i;
01587 se.value1 = currMB->intra_pred_modes[i];
01588 se.value2 = 0;
01589
01590 #if TRACE
01591 if (se.value1 < 0 )
01592 snprintf(se.tracestring, TRACESTRING_SIZE, "Intra 4x4 mode = predicted (context: %d)",se.context);
01593 else
01594 snprintf(se.tracestring, TRACESTRING_SIZE, "Intra 4x4 mode = %3d (context: %d)",se.value1,se.context);
01595 #endif
01596
01597
01598 se.type = SE_INTRAPREDMODE;
01599
01600
01601 writeIntraPredMode (&se, dataPart);
01602
01603 rate += se.len;
01604 }
01605
01606 bitCount[BITS_COEFF_Y_MB] += rate;
01607
01608 return rate;
01609 }
01610
01611
01612
01613
01614
01615
01616
01617 int writeIntra8x8Modes(Slice *currSlice, Macroblock *currMB)
01618 {
01619 int block8x8;
01620 SyntaxElement se;
01621 int *bitCount = currMB->bitcounter;
01622 const int *partMap = assignSE2partition[params->partition_mode];
01623 DataPartition *dataPart = &(currSlice->partArr[partMap[SE_INTRAPREDMODE]]);
01624
01625 int rate = 0;
01626
01627 currMB->IntraChromaPredModeFlag = 1;
01628
01629 for(block8x8 = 0; block8x8 < 16; block8x8 += 4)
01630 {
01631
01632 se.context = block8x8;
01633 se.value1 = currMB->intra_pred_modes8x8[block8x8];
01634 se.value2 = 0;
01635
01636 #if TRACE
01637 if (se.value1 < 0 )
01638 snprintf(se.tracestring, TRACESTRING_SIZE, "Intra 8x8 mode = predicted (context: %d)",se.context);
01639 else
01640 snprintf(se.tracestring, TRACESTRING_SIZE, "Intra 8x8 mode = %3d (context: %d)",se.value1,se.context);
01641 #endif
01642
01643
01644 se.type = SE_INTRAPREDMODE;
01645
01646
01647 writeIntraPredMode (&se, dataPart);
01648
01649 rate += se.len;
01650 }
01651 bitCount[BITS_COEFF_Y_MB] += rate;
01652
01653 return rate;
01654 }
01655
01656 int writeIntraModes(Slice *currSlice, Macroblock *currMB)
01657 {
01658 switch (currMB->mb_type)
01659 {
01660 case I4MB:
01661 return writeIntra4x4Modes(currSlice, currMB);
01662 break;
01663 case I8MB:
01664 return writeIntra8x8Modes(currSlice, currMB);
01665 break;
01666 default:
01667 return 0;
01668 break;
01669 }
01670 }
01671
01672
01673
01674
01675
01676
01677
01678 int B8Mode2Value (short b8mode, short b8pdir)
01679 {
01680 static const int b8start[8] = {0,0,0,0, 1, 4, 5, 10};
01681 static const int b8inc [8] = {0,0,0,0, 1, 2, 2, 1};
01682
01683 return (img->type!=B_SLICE) ? (b8mode - 4) : b8start[b8mode] + b8inc[b8mode] * b8pdir;
01684 }
01685
01686
01687 static int writeIPCMLuma (ImageParameters *img, imgpel **imgY, Bitstream *currStream, SyntaxElement *se, int *bitCount)
01688 {
01689 int i, j;
01690 int bit_size = MB_BLOCK_SIZE * MB_BLOCK_SIZE * img->bitdepth_luma;
01691 bitCount[BITS_COEFF_Y_MB] += bit_size;
01692 se->len = img->bitdepth_luma;
01693 se->type = SE_MBTYPE;
01694
01695 for (j = img->pix_y; j < img->pix_y + MB_BLOCK_SIZE; j++)
01696 {
01697 for (i = img->pix_x; i < img->pix_x + MB_BLOCK_SIZE; i++)
01698 {
01699 se->bitpattern = imax(img->min_IPCM_value, imgY[j][i]);
01700 se->value1 = se->bitpattern;
01701 #if TRACE
01702 snprintf(se->tracestring, TRACESTRING_SIZE, "pcm_sample_luma (%d %d) = %d", j, i, se->bitpattern);
01703 #endif
01704 writeSE_Fix(se, currStream);
01705 }
01706 }
01707 return bit_size;
01708 }
01709
01710 static int writeIPCMChroma (ImageParameters *img, imgpel **imgUV, Bitstream *currStream, SyntaxElement *se, int *bitCount)
01711 {
01712 int i, j;
01713 int bit_size = img->mb_cr_size_y * img->mb_cr_size_x * img->bitdepth_luma;
01714 bitCount[BITS_COEFF_UV_MB] += bit_size;
01715 se->len = img->bitdepth_chroma;
01716 se->type = SE_MBTYPE;
01717
01718 for (j = img->pix_c_y; j < img->pix_c_y + img->mb_cr_size_y; j++)
01719 {
01720 for (i = img->pix_c_x; i < img->pix_c_x + img->mb_cr_size_x; i++)
01721 {
01722 se->bitpattern = imax(img->min_IPCM_value, imgUV[j][i]);
01723 se->value1 = se->bitpattern;
01724 #if TRACE
01725
01726 snprintf(se->tracestring, TRACESTRING_SIZE, "pcm_sample_chroma (%d %d) = %d", j, i, se->bitpattern);
01727 #endif
01728 writeSE_Fix(se, currStream);
01729 }
01730 }
01731 return bit_size;
01732 }
01733
01734 static int writeIPCMByteAlign(Bitstream *currStream, SyntaxElement *se, int *bitCount)
01735 {
01736 if (currStream->bits_to_go < 8)
01737 {
01738
01739 se->type = SE_MBTYPE;
01740 se->len = currStream->bits_to_go;
01741 bitCount[BITS_COEFF_Y_MB]+= se->len;
01742 se->bitpattern = 0;
01743 #if TRACE
01744 snprintf(se->tracestring, TRACESTRING_SIZE, "pcm_alignment_zero_bits = %d", se->len);
01745 #endif
01746 writeSE_Fix(se, currStream);
01747
01748 return se->len;
01749 }
01750 else
01751 return 0;
01752 }
01753
01754
01755
01756
01757
01758
01759
01760
01761
01762
01763
01764
01765
01766
01767
01768 int writeMBLayer (Slice* currSlice, Macroblock *currMB, int rdopt, int *coeff_rate)
01769 {
01770 int i;
01771 int mb_nr = currMB->mb_nr;
01772 int prev_mb_nr = FmoGetPreviousMBNr(img->current_mb_nr);
01773 Macroblock *prevMB = currMB->PrevMB;
01774 SyntaxElement se;
01775 int* bitCount = currMB->bitcounter;
01776 DataPartition* dataPart;
01777 const int* partMap = assignSE2partition[params->partition_mode];
01778 int no_bits = 0;
01779 int skip = currMB->mb_type ? 0:((img->type == B_SLICE) ? !currMB->cbp:1);
01780 int mb_type;
01781 int prevMbSkipped = 0;
01782 int mb_field_tmp;
01783 Macroblock *topMB = NULL;
01784
01785 int WriteFrameFieldMBInHeader = 0;
01786
01787 if (img->MbaffFrameFlag)
01788 {
01789 if ((mb_nr & 0x01) == 0)
01790 {
01791 WriteFrameFieldMBInHeader = 1;
01792
01793 prevMbSkipped = 0;
01794 }
01795 else
01796 {
01797 if (prevMB->mb_type ? 0:((img->type == B_SLICE) ? !prevMB->cbp:1))
01798 {
01799 WriteFrameFieldMBInHeader = 1;
01800 }
01801
01802 topMB= &img->mb_data[prev_mb_nr];
01803 prevMbSkipped = topMB->skip_flag;
01804 }
01805 }
01806 currMB->IntraChromaPredModeFlag = IS_INTRA(currMB);
01807
01808
01809 dataPart = &(currSlice->partArr[partMap[SE_MBTYPE]]);
01810
01811 if(img->type == I_SLICE)
01812 {
01813
01814 if(WriteFrameFieldMBInHeader)
01815 {
01816 se.value1 = currMB->mb_field;
01817 se.value2 = 0;
01818 se.type = SE_MBTYPE;
01819
01820 TRACE_SE (se.tracestring, "mb_field_decoding_flag");
01821 writeFieldModeInfo(&se, dataPart);
01822
01823 bitCount[BITS_MB_MODE] += se.len;
01824 no_bits += se.len;
01825 }
01826
01827
01828 se.value1 = MBType2Value (currMB);
01829 se.value2 = 0;
01830 se.type = SE_MBTYPE;
01831
01832 #if TRACE
01833 snprintf(se.tracestring, TRACESTRING_SIZE, "mb_type (I_SLICE) (%2d,%2d) = %3d",img->mb_x, img->mb_y, currMB->mb_type);
01834 #endif
01835
01836 writeMB_typeInfo (&se, dataPart);
01837
01838 bitCount[BITS_MB_MODE] += se.len;
01839 no_bits += se.len;
01840 }
01841 else if (currSlice->symbol_mode == CABAC)
01842 {
01843 if (img->MbaffFrameFlag && ((img->current_mb_nr & 0x01) == 0 || prevMbSkipped))
01844 {
01845 mb_field_tmp = currMB->mb_field;
01846 currMB->mb_field = field_flag_inference(currMB);
01847 CheckAvailabilityOfNeighborsCABAC(currMB);
01848 currMB->mb_field = mb_field_tmp;
01849 }
01850
01851
01852 mb_type = MBType2Value (currMB);
01853 se.value1 = mb_type;
01854 se.value2 = currMB->cbp;
01855 se.type = SE_MBTYPE;
01856
01857 TRACE_SE (se.tracestring, "mb_skip_flag");
01858 writeMB_skip_flagInfo_CABAC(currMB, &se, dataPart);
01859
01860 bitCount[BITS_MB_MODE] += se.len;
01861 no_bits += se.len;
01862
01863 CheckAvailabilityOfNeighborsCABAC(currMB);
01864
01865
01866 if(img->MbaffFrameFlag && !skip)
01867 {
01868 if(WriteFrameFieldMBInHeader)
01869 {
01870 se.value1 = currMB->mb_field;
01871 se.value2 = 0;
01872 se.type = SE_MBTYPE;
01873
01874 TRACE_SE(se.tracestring, "mb_field_decoding_flag");
01875 writeFieldModeInfo(&se, dataPart);
01876
01877 bitCount[BITS_MB_MODE] += se.len;
01878 no_bits += se.len;
01879 }
01880 }
01881
01882
01883 if (currMB->mb_type != 0 || ((img->type == B_SLICE) && (currMB->cbp != 0 || cmp_cbp[1] !=0 || cmp_cbp[2]!=0 )))
01884 {
01885 se.value1 = mb_type;
01886 se.value2 = 0;
01887 se.type = SE_MBTYPE;
01888
01889 #if TRACE
01890 if (img->type == B_SLICE)
01891 snprintf(se.tracestring, TRACESTRING_SIZE, "mb_type (B_SLICE) (%2d,%2d) = %3d",img->mb_x, img->mb_y, currMB->mb_type);
01892 else
01893 snprintf(se.tracestring, TRACESTRING_SIZE, "mb_type (P_SLICE) (%2d,%2d) = %3d",img->mb_x, img->mb_y, currMB->mb_type);
01894 #endif
01895 writeMB_typeInfo( &se, dataPart);
01896
01897 bitCount[BITS_MB_MODE] += se.len;
01898 no_bits += se.len;
01899 }
01900 }
01901
01902 else if (currMB->mb_type != 0 || ((img->type == B_SLICE) && (currMB->cbp != 0 || cmp_cbp[1] != 0 || cmp_cbp[2]!=0 )))
01903 {
01904
01905 se.value1 = img->cod_counter;
01906 se.value2 = 0;
01907 se.type = SE_MBTYPE;
01908
01909 TRACE_SE (se.tracestring, "mb_skip_run");
01910 writeSE_UVLC(&se, dataPart);
01911
01912 bitCount[BITS_MB_MODE] += se.len;
01913 no_bits += se.len;
01914
01915
01916 img->cod_counter = 0;
01917
01918
01919 if(img->MbaffFrameFlag && !skip)
01920 {
01921 if(WriteFrameFieldMBInHeader)
01922 {
01923 se.value1 = currMB->mb_field;
01924 se.type = SE_MBTYPE;
01925
01926 TRACE_SE(se.tracestring, "mb_field_decoding_flag");
01927 writeSE_Flag (&se, dataPart);
01928
01929 bitCount[BITS_MB_MODE] += se.len;
01930 no_bits += se.len;
01931 }
01932 }
01933
01934 se.value1 = MBType2Value (currMB);
01935
01936 if (img->type != B_SLICE)
01937 {
01938 se.value1--;
01939 }
01940 se.type = SE_MBTYPE;
01941 se.value2 = 0;
01942
01943 #if TRACE
01944 if (img->type == B_SLICE)
01945 snprintf(se.tracestring, TRACESTRING_SIZE, "mb_type (B_SLICE) (%2d,%2d) = %3d",img->mb_x, img->mb_y, currMB->mb_type);
01946 else
01947 snprintf(se.tracestring, TRACESTRING_SIZE, "mb_type (P_SLICE) (%2d,%2d) = %3d",img->mb_x, img->mb_y, currMB->mb_type);
01948 #endif
01949
01950 writeMB_typeInfo(&se, dataPart);
01951
01952 bitCount[BITS_MB_MODE] += se.len;
01953 no_bits += se.len;
01954 }
01955 else
01956 {
01957
01958 img->cod_counter++;
01959
01960 currMB->skip_flag = 1;
01961
01962 reset_mb_nz_coeff(img->current_mb_nr);
01963
01964 if(FmoGetNextMBNr(img->current_mb_nr) == -1 && img->cod_counter > 0)
01965 {
01966
01967 se.value1 = img->cod_counter;
01968 se.value2 = 0;
01969 se.type = SE_MBTYPE;
01970
01971 TRACE_SE(se.tracestring, "mb_skip_run");
01972 writeSE_UVLC(&se, dataPart);
01973
01974 bitCount[BITS_MB_MODE] += se.len;
01975 no_bits += se.len;
01976
01977
01978 img->cod_counter = 0;
01979 }
01980 }
01981
01982
01983 currMB->NoMbPartLessThan8x8Flag = (IS_DIRECT(currMB) && !(active_sps->direct_8x8_inference_flag))? 0: 1;
01984
01985 if (currMB->mb_type == IPCM)
01986 {
01987 dataPart = &(currSlice->partArr[partMap[SE_LUM_DC_INTRA]]);
01988 if (currSlice->symbol_mode == CABAC)
01989 {
01990 int len;
01991 EncodingEnvironmentPtr eep = &dataPart->ee_cabac;
01992 len = arienco_bits_written(eep);
01993 arienco_done_encoding(eep);
01994 len = arienco_bits_written(eep) - len;
01995 no_bits += len;
01996
01997 arienco_start_encoding(eep, dataPart->bitstream->streamBuffer, &(dataPart->bitstream->byte_pos));
01998 }
01999
02000 writeIPCMByteAlign(dataPart->bitstream, &se, bitCount);
02001
02002 no_bits += writeIPCMLuma (img, enc_picture->imgY, dataPart->bitstream, &se, bitCount);
02003
02004 if ((active_sps->chroma_format_idc != YUV400) && !IS_INDEPENDENT(params))
02005 {
02006 int uv;
02007 for (uv = 0; uv < 2; uv ++)
02008 {
02009 no_bits += writeIPCMChroma (img, enc_picture->imgUV[uv], dataPart->bitstream, &se, bitCount);
02010 }
02011 }
02012 return no_bits;
02013 }
02014
02015 dataPart = &(currSlice->partArr[partMap[SE_MBTYPE]]);
02016
02017
02018 if (currMB->mb_type==P8x8)
02019 {
02020 for (i=0; i<4; i++)
02021 {
02022 se.value1 = B8Mode2Value (currMB->b8mode[i], currMB->b8pdir[i]);
02023 se.value2 = 0;
02024 se.type = SE_MBTYPE;
02025 #if TRACE
02026 snprintf(se.tracestring, TRACESTRING_SIZE, "8x8 mode/pdir(%2d) = %3d/%d", i, currMB->b8mode[i], currMB->b8pdir[i]);
02027 #endif
02028 writeB8_typeInfo (&se, dataPart);
02029 bitCount[BITS_MB_MODE]+= se.len;
02030 no_bits += se.len;
02031
02032
02033 currMB->NoMbPartLessThan8x8Flag &= (currMB->b8mode[i]==0 && active_sps->direct_8x8_inference_flag) ||
02034 (currMB->b8mode[i]==4);
02035 }
02036 no_bits += writeMotionInfo2NAL (currSlice, currMB);
02037 }
02038
02039
02040
02041
02042 if ((currMB->mb_type == I8MB || currMB->mb_type == I4MB) && params->Transform8x8Mode)
02043 {
02044 se.value1 = currMB->luma_transform_size_8x8_flag;
02045 se.type = SE_MBTYPE;
02046
02047 #if TRACE
02048 snprintf(se.tracestring, TRACESTRING_SIZE, "transform_size_8x8_flag = %3d", currMB->luma_transform_size_8x8_flag);
02049 #endif
02050 writeMB_transform_size(&se, dataPart);
02051
02052 bitCount[BITS_MB_MODE] += se.len;
02053 no_bits += se.len;
02054 }
02055
02056
02057 no_bits += writeIntraModes(currSlice, currMB);
02058
02059 if (currMB->IntraChromaPredModeFlag && ((active_sps->chroma_format_idc != YUV400) && (active_sps->chroma_format_idc != YUV444)))
02060 no_bits += writeChromaIntraPredMode(currSlice, currMB);
02061 else if(!rdopt)
02062 {
02063 currMB->c_ipred_mode = DC_PRED_8;
02064 }
02065
02066
02067
02068
02069
02070
02071 if (currMB->mb_type !=0 && currMB->mb_type != P8x8)
02072 {
02073 no_bits += writeMotionInfo2NAL (currSlice, currMB);
02074 }
02075
02076 if ((currMB->mb_type!=0) || (img->type==B_SLICE && (currMB->cbp!=0 || cmp_cbp[1]!=0 || cmp_cbp[2]!=0)))
02077 {
02078 *coeff_rate = writeCBPandDquant (currSlice, currMB);
02079 *coeff_rate += writeCoeff16x16 (currSlice, currMB, PLANE_Y, currMB->cbp );
02080
02081 if ((active_sps->chroma_format_idc != YUV400) && (!IS_INDEPENDENT(params)))
02082 {
02083 if(active_sps->chroma_format_idc == YUV444)
02084 {
02085 *coeff_rate += writeCoeff16x16(currSlice, currMB,PLANE_U, currMB->cbp);
02086 *coeff_rate += writeCoeff16x16(currSlice, currMB,PLANE_V, currMB->cbp);
02087 }
02088 else
02089 *coeff_rate += writeChromaCoeff (currSlice, currMB);
02090 }
02091 no_bits += *coeff_rate;
02092 }
02093
02094 return no_bits;
02095 }
02096
02097 void write_terminating_bit (Slice *currSlice, short bit)
02098 {
02099 DataPartition* dataPart;
02100 const int* partMap = assignSE2partition[params->partition_mode];
02101
02102
02103 dataPart = &(currSlice->partArr[partMap[SE_MBTYPE]]);
02104 dataPart->bitstream->write_flag = 1;
02105
02106 biari_encode_symbol_final(&(dataPart->ee_cabac), bit);
02107 #if TRACE
02108 fprintf (p_trace, " CABAC terminating bit = %d\n",bit);
02109 #endif
02110
02111 }
02112
02113
02114
02115
02116
02117
02118
02119
02120 int writeChromaIntraPredMode(Slice* currSlice, Macroblock* currMB)
02121 {
02122 SyntaxElement se;
02123 int* bitCount = currMB->bitcounter;
02124 const int* partMap = assignSE2partition[params->partition_mode];
02125 int rate = 0;
02126 DataPartition* dataPart;
02127
02128
02129
02130 se.value1 = currMB->c_ipred_mode;
02131 se.value2 = 0;
02132 se.type = SE_INTRAPREDMODE;
02133 dataPart = &(currSlice->partArr[partMap[SE_INTRAPREDMODE]]);
02134
02135 TRACE_SE(se.tracestring, "intra_chroma_pred_mode");
02136 writeCIPredMode(&se, dataPart);
02137
02138 bitCount[BITS_COEFF_UV_MB] += se.len;
02139 rate += se.len;
02140
02141 return rate;
02142 }
02143
02144
02145
02146
02147
02148
02149
02150
02151
02152
02153 void write_macroblock (ImageParameters *img, Slice *currSlice, Macroblock* currMB, int eos_bit, Boolean prev_recode_mb)
02154 {
02155 int* bitCount = currMB->bitcounter;
02156 int i;
02157
02158 extern int cabac_encoding;
02159
02160
02161 #if TRACE
02162 if ( prev_recode_mb == FALSE )
02163 {
02164 for (i=0; i<currSlice->max_part_nr; i++)
02165 {
02166 currSlice->partArr[i].bitstream->trace_enabled = TRUE;
02167 }
02168 }
02169 #endif
02170
02171
02172 if(params->UseConstrainedIntraPred && (img->type==P_SLICE || img->type==B_SLICE))
02173 {
02174 img->intra_block[img->current_mb_nr] = IS_INTRA(currMB);
02175 }
02176
02177
02178 if (currMB->mb_nr == 0)
02179 intras=0;
02180
02181 if (IS_INTRA(currMB))
02182 intras++;
02183
02184
02185 if (currSlice->symbol_mode==CABAC && currMB->mb_nr != currSlice->start_mb_nr && eos_bit)
02186 {
02187 write_terminating_bit (currSlice, 0);
02188 }
02189
02190 #if TRACE
02191
02192 if (p_trace)
02193 {
02194 fprintf(p_trace, "\n*********** Pic: %i (I/P) MB: %i Slice: %i **********\n\n", img->frame_no, currMB->mb_nr, img->current_slice_nr);
02195 }
02196 #endif
02197
02198 cabac_encoding = 1;
02199
02200
02201 currMB->write_mb = TRUE;
02202
02203 writeMBLayer (currSlice, currMB, 0, &i);
02204
02205 if (!((currMB->mb_type !=0 ) || ((img->type==B_SLICE) && currMB->cbp != 0) ))
02206 {
02207 reset_mb_nz_coeff(currMB->mb_nr);
02208 }
02209
02210
02211 bitCount[BITS_TOTAL_MB] = bitCount[BITS_MB_MODE ]
02212 + bitCount[BITS_COEFF_Y_MB ]
02213 + bitCount[BITS_INTER_MB ]
02214 + bitCount[BITS_CBP_MB ]
02215 + bitCount[BITS_DELTA_QUANT_MB]
02216 + bitCount[BITS_COEFF_UV_MB ]
02217 + bitCount[BITS_COEFF_CB_MB ]
02218 + bitCount[BITS_COEFF_CR_MB ];
02219
02220 if ( params->RCEnable )
02221 rc_update_mb_stats( currMB, bitCount );
02222
02223
02224 img->NumberofCodedMacroBlocks++;
02225
02226 stats->bit_slice += bitCount[BITS_TOTAL_MB];
02227
02228 cabac_encoding = 0;
02229 }
02230
02231
02232
02233
02234
02235
02236
02237
02238 int writeReferenceFrame (Slice* currSlice, Macroblock* currMB, int mode, int i, int j, int fwd_flag, int ref)
02239 {
02240 SyntaxElement se;
02241 int* bitCount = currMB->bitcounter;
02242 const int* partMap = assignSE2partition[params->partition_mode];
02243 int rate = 0;
02244 DataPartition* dataPart = &(currSlice->partArr[partMap[SE_REFFRAME]]);
02245 int list = ( fwd_flag ? LIST_0 + currMB->list_offset: LIST_1 + currMB->list_offset);
02246
02247 se.value1 = ref;
02248 se.type = SE_REFFRAME;
02249 se.value2 = (fwd_flag)? LIST_0: LIST_1;
02250
02251 img->subblock_x = i;
02252 img->subblock_y = j;
02253
02254 #if TRACE
02255 if (fwd_flag)
02256 snprintf(se.tracestring, TRACESTRING_SIZE, "ref_idx_l0 = %d", se.value1);
02257 else
02258 snprintf(se.tracestring, TRACESTRING_SIZE, "ref_idx_l1 = %d", se.value1);
02259 #endif
02260
02261 writeRefFrame[list](&se, dataPart);
02262
02263 bitCount[BITS_INTER_MB] += se.len;
02264 rate += se.len;
02265
02266 return rate;
02267 }
02268
02269
02270
02271
02272
02273
02274
02275
02276 int writeMotionVector8x8 (Slice* currSlice,
02277 Macroblock *currMB,
02278 int i0,
02279 int j0,
02280 int i1,
02281 int j1,
02282 int refframe,
02283 int list_idx,
02284 int mv_mode,
02285 short bipred_me
02286 )
02287 {
02288 static PixelPos block_a, block_b, block_c, block_d;
02289 int i, j, k, l, m;
02290 int curr_mvd;
02291
02292 int rate = 0;
02293 int step_h = params->part_size[mv_mode][0];
02294 int step_v = params->part_size[mv_mode][1];
02295 SyntaxElement se;
02296 int* bitCount = currMB->bitcounter;
02297
02298 const int* partMap = assignSE2partition[params->partition_mode];
02299 DataPartition* dataPart = &(currSlice->partArr[partMap[SE_MVD]]);
02300 int refindex = refframe;
02301
02302 short*** all_mv = img->all_mv[list_idx][refindex][mv_mode];
02303 short *cur_mv;
02304 short predMV[2];
02305 PicMotionParams *motion = &enc_picture->motion;
02306 short ***final_mv = &motion->mv[list_idx][img->block_y];
02307 short (*currMB_mvd)[4][2] = currMB->mvd[list_idx];
02308 se.type = SE_MVD;
02309
02310 if (bipred_me && is_bipred_enabled(mv_mode) && refindex == 0)
02311 all_mv = img->bipred_mv[bipred_me - 1][list_idx][refindex][mv_mode];
02312
02313 for (j = j0; j < j1; j += step_v)
02314 {
02315 img->subblock_y = j;
02316 for (i = i0; i < i1; i += step_h)
02317 {
02318 img->subblock_x = i;
02319
02320 if (currMB->write_mb)
02321 cur_mv = final_mv[j][img->block_x + i];
02322 else
02323 cur_mv = all_mv[j][i];
02324
02325
02326 get_neighbors(currMB, &block_a, &block_b, &block_c, &block_d, i<<2, j<<2, step_h<<2);
02327 GetMotionVectorPredictor (currMB, &block_a, &block_b, &block_c, predMV, motion->ref_idx[list_idx], motion->mv[list_idx], refindex, i<<2, j<<2, step_h<<2, step_v<<2);
02328
02329
02330 for (k=0; k<2; k++)
02331 {
02332 curr_mvd = cur_mv[k] - predMV[k];
02333
02334
02335 for (l = j; l < j + step_v; l++)
02336 {
02337 for (m = i; m < i + step_h; m++)
02338 {
02339 currMB_mvd[l][m][k] = curr_mvd;
02340 }
02341 }
02342
02343 se.value1 = curr_mvd;
02344 se.value2 = 2 * k + list_idx;
02345 #if TRACE
02346 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]);
02347 #endif
02348 writeMVD (&se, dataPart);
02349 rate += se.len;
02350 }
02351 }
02352 }
02353
02354 bitCount[BITS_INTER_MB] += rate;
02355 return rate;
02356 }
02357
02358
02359
02360
02361
02362
02363
02364
02365 int writeMotionInfo2NAL (Slice* currSlice, Macroblock* currMB)
02366 {
02367 int k, j0, i0;
02368 int no_bits = 0;
02369 int bframe = (img->type==B_SLICE);
02370 int step_h0 = (params->blc_size[(currMB->mb_type == P8x8) ? 4 : currMB->mb_type][0] >> 2);
02371 int step_v0 = (params->blc_size[(currMB->mb_type == P8x8) ? 4 : currMB->mb_type][1] >> 2);
02372 char *cref_idx;
02373
02374
02375 if (IS_INTERMV (currMB))
02376 {
02377
02378 if ((currMB->mb_type != P8x8) || !ZeroRef (currMB) || currSlice->symbol_mode==CABAC || bframe)
02379 {
02380 for (j0 = 0; j0 < 4; j0 += step_v0)
02381 {
02382 cref_idx = &enc_picture->motion.ref_idx[LIST_0][img->block_y + j0][img->block_x];
02383 for (i0 = 0; i0 < 4; i0 += step_h0)
02384 {
02385 k = j0 + (i0 >> 1);
02386 if (currMB->b8mode[k]!=0 && (currMB->b8pdir[k]==0 || currMB->b8pdir[k]==2))
02387 {
02388 no_bits += writeReferenceFrame (currSlice, currMB, currMB->b8mode[k], i0, j0, 1, cref_idx[i0]);
02389 }
02390 }
02391 }
02392
02393 for (j0 = 0; j0 < 4; j0 += step_v0)
02394 {
02395 cref_idx = &enc_picture->motion.ref_idx[LIST_1][img->block_y + j0][img->block_x];
02396 for (i0 = 0; i0 < 4; i0 += step_h0)
02397 {
02398 k = j0 + (i0 >> 1);
02399 if (currMB->b8mode[k]!=0 && (currMB->b8pdir[k] == 1 || currMB->b8pdir[k] == 2))
02400 {
02401 no_bits += writeReferenceFrame (currSlice, currMB, currMB->b8mode[k], i0, j0, 0, cref_idx[i0]);
02402 }
02403 }
02404 }
02405 }
02406 }
02407
02408
02409 if (IS_INTERMV (currMB))
02410 {
02411 for (j0=0; j0<4; j0+=step_v0)
02412 {
02413 cref_idx = &enc_picture->motion.ref_idx[LIST_0][img->block_y + j0][img->block_x];
02414 for (i0=0; i0<4; i0+=step_h0)
02415 {
02416 k = j0 + (i0 >> 1);
02417 if (currMB->b8mode[k]!=0 && (currMB->b8pdir[k]==0 || currMB->b8pdir[k]==2))
02418 {
02419 no_bits += writeMotionVector8x8 (currSlice, currMB, i0, j0, i0 + step_h0, j0 + step_v0, cref_idx[i0], LIST_0, currMB->b8mode[k], currMB->bipred_me[k]);
02420 }
02421 }
02422 }
02423
02424
02425 if (bframe)
02426 {
02427 for (j0=0; j0<4; j0+=step_v0)
02428 {
02429 cref_idx = &enc_picture->motion.ref_idx[LIST_1][img->block_y + j0][img->block_x];
02430 for (i0 = 0; i0 < 4; i0 += step_h0)
02431 {
02432 k=j0+(i0 >> 1);
02433 if (currMB->b8mode[k]!=0 && (currMB->b8pdir[k]==1 || currMB->b8pdir[k]==2))
02434 {
02435 no_bits += writeMotionVector8x8 (currSlice, currMB, i0, j0, i0+step_h0, j0+step_v0, cref_idx[i0], LIST_1, currMB->b8mode[k], currMB->bipred_me[k]);
02436 }
02437 }
02438 }
02439 }
02440 }
02441 return no_bits;
02442 }
02443
02444
02445
02446
02447
02448
02449
02450
02451
02452 static int writeChromaCoeff (Slice *currSlice, Macroblock* currMB)
02453 {
02454 int rate = 0;
02455 int block_rate = 0;
02456 SyntaxElement se;
02457 int* bitCount = currMB->bitcounter;
02458 const int* partMap = assignSE2partition[params->partition_mode];
02459 int cbp = currMB->cbp;
02460 DataPartition* dataPart;
02461
02462 int level, run;
02463 int k, uv;
02464 int b8, b4;
02465 int* ACLevel;
02466 int* ACRun;
02467 int* DCLevel;
02468 int* DCRun;
02469
02470 static const int chroma_dc_context[3]={CHROMA_DC, CHROMA_DC_2x4, CHROMA_DC_4x4};
02471 int yuv = img->yuv_format - 1;
02472
02473 static const unsigned char chroma_ac_param[3][8][4] =
02474 {
02475 {{ 4, 20, 5, 21},
02476 {36, 52, 37, 53},
02477 { 0, 0, 0, 0},
02478 { 0, 0, 0, 0},
02479 { 0, 0, 0, 0},
02480 { 0, 0, 0, 0},
02481 { 0, 0, 0, 0},
02482 { 0, 0, 0, 0}},
02483 {{ 4, 20, 5, 21},
02484 { 6, 22, 7, 23},
02485 {36, 52, 37, 53},
02486 {38, 54, 39, 55},
02487 { 0, 0, 0, 0},
02488 { 0, 0, 0, 0},
02489 { 0, 0, 0, 0},
02490 { 0, 0, 0, 0}},
02491 {{ 4, 20, 5, 21},
02492 {36, 52, 37, 53},
02493 { 6, 22, 7, 23},
02494 {38, 54, 39, 55},
02495 { 8, 24, 9, 25},
02496 {40, 56, 41, 57},
02497 {10, 26, 11, 27},
02498 {42, 58, 43, 59}}
02499 };
02500
02501
02502
02503
02504 if (cbp > 15)
02505 {
02506 if (currSlice->symbol_mode == CAVLC)
02507 {
02508 rate += writeCoeff4x4_CAVLC (currSlice, currMB, CHROMA_DC, 0, 0, 0);
02509 rate += writeCoeff4x4_CAVLC (currSlice, currMB, CHROMA_DC, 0, 0, 1);
02510 }
02511 else
02512 {
02513 img->is_intra_block = IS_INTRA(currMB);
02514 se.type = (img->is_intra_block ? SE_CHR_DC_INTRA : SE_CHR_DC_INTER);
02515
02516 dataPart = &(currSlice->partArr[partMap[se.type]]);
02517 se.context = chroma_dc_context[yuv];
02518
02519 for (uv=0; uv < 2; uv++)
02520 {
02521 img->is_v_block = uv;
02522
02523 DCLevel = img->cofDC[uv+1][0];
02524 DCRun = img->cofDC[uv+1][1];
02525
02526 level = 1;
02527 for (k=0; k <= img->num_cdc_coeff && level != 0; ++k)
02528 {
02529 level = se.value1 = DCLevel[k];
02530 run = se.value2 = DCRun [k];
02531
02532 #if TRACE
02533 snprintf(se.tracestring, TRACESTRING_SIZE, "DC Chroma %2d: level =%3d run =%2d",k, level, run);
02534 #endif
02535 writeRunLevel_CABAC(currMB, &se, dataPart);
02536
02537 block_rate += se.len;
02538 }
02539 }
02540 bitCount[BITS_COEFF_UV_MB] += block_rate;
02541 rate += block_rate;
02542 }
02543 }
02544
02545
02546
02547
02548 uv=-1;
02549 if (cbp >> 4 == 2)
02550 {
02551 if (currSlice->symbol_mode == CAVLC)
02552 {
02553 for (b8=4; b8 < (4 + img->num_blk8x8_uv); b8++)
02554 {
02555 rate += writeCoeff4x4_CAVLC (currSlice, currMB, CHROMA_AC, b8, 0, chroma_ac_param[yuv][b8-4][0]);
02556 rate += writeCoeff4x4_CAVLC (currSlice, currMB, CHROMA_AC, b8, 1, chroma_ac_param[yuv][b8-4][1]);
02557 rate += writeCoeff4x4_CAVLC (currSlice, currMB, CHROMA_AC, b8, 2, chroma_ac_param[yuv][b8-4][2]);
02558 rate += writeCoeff4x4_CAVLC (currSlice, currMB, CHROMA_AC, b8, 3, chroma_ac_param[yuv][b8-4][3]);
02559 }
02560 }
02561 else
02562 {
02563 block_rate = 0;
02564 img->is_intra_block = IS_INTRA(currMB);
02565 se.context = CHROMA_AC;
02566 se.type = (img->is_intra_block ? SE_CHR_AC_INTRA : SE_CHR_AC_INTER);
02567
02568 dataPart = &(currSlice->partArr[partMap[se.type]]);
02569
02570 for (b8=4; b8 < (4+img->num_blk8x8_uv); b8++)
02571 {
02572 for (b4=0; b4 < 4; b4++)
02573 {
02574 ACLevel = img->cofAC[b8][b4][0];
02575 ACRun = img->cofAC[b8][b4][1];
02576
02577 level=1;
02578 uv++;
02579 img->is_v_block = (uv >= (img->num_blk8x8_uv << 1));
02580
02581 img->subblock_y = (subblk_offset_y[yuv][b8-4][b4] >> 2);
02582 img->subblock_x = (subblk_offset_x[yuv][b8-4][b4] >> 2);
02583
02584 for (k=0; k < 16 && level != 0; k++)
02585 {
02586 level = se.value1 = ACLevel[k];
02587 run = se.value2 = ACRun [k];
02588
02589 #if TRACE
02590 snprintf(se.tracestring, TRACESTRING_SIZE, "AC Chroma %2d: level =%3d run =%2d",k, level, run);
02591 #endif
02592 writeRunLevel_CABAC(currMB, &se, dataPart);
02593 block_rate += se.len;
02594 }
02595 }
02596 }
02597 bitCount[BITS_COEFF_UV_MB] += block_rate;
02598 rate += block_rate;
02599 }
02600 }
02601
02602 return rate;
02603 }
02604
02605 #if TRACE
02606 static inline void trace_coeff(SyntaxElement *se, int plane, char *type, int k, int level, int run)
02607 {
02608 if (plane == 0)
02609 snprintf(se->tracestring, TRACESTRING_SIZE, "Luma%s sng(%2d) level =%3d run =%2d", type, k, level,run);
02610 else if (plane == 1)
02611 snprintf(se->tracestring, TRACESTRING_SIZE, "Cb%s sng(%2d) level =%3d run =%2d", type, k, level,run);
02612 else
02613 snprintf(se->tracestring, TRACESTRING_SIZE, "Cr%s sng(%2d) level =%3d run =%2d", type, k, level,run);
02614 }
02615 #endif
02616
02617
02618
02619
02620
02621
02622
02623 int writeCoeff4x4_CABAC (Slice* currSlice, Macroblock* currMB, ColorPlane plane, int b8, int b4, int intra4x4mode)
02624 {
02625 int rate = 0;
02626 SyntaxElement se;
02627 const int* partMap = assignSE2partition[params->partition_mode];
02628 int* bitCount = currMB->bitcounter;
02629 DataPartition* dataPart;
02630
02631 int level, run;
02632 int k;
02633 int pl_off=plane<<2;
02634 int bits_coeff_mb = ((plane==0) ? (BITS_COEFF_Y_MB) : ((plane==1) ? BITS_COEFF_CB_MB : BITS_COEFF_CR_MB));
02635 int* ACLevel = img->cofAC[b8+pl_off][b4][0];
02636 int* ACRun = img->cofAC[b8+pl_off][b4][1];
02637
02638 img->subblock_x = ((b8&0x1)==0) ? (((b4&0x1)==0)? 0: 1) : (((b4&0x1)==0)? 2: 3);
02639 img->subblock_y = (b8<2) ? ((b4<2) ? 0: 1) : ((b4<2) ? 2: 3);
02640 img->is_intra_block = intra4x4mode;
02641 se.context = ((plane == 0) ? (LUMA_4x4) : ((plane==1) ? CB_4x4 : CR_4x4));
02642
02643
02644 se.type = (intra4x4mode ? SE_LUM_DC_INTRA : SE_LUM_DC_INTER);
02645
02646
02647 dataPart = &(currSlice->partArr[partMap[se.type]]);
02648
02649 level = se.value1 = ACLevel[0];
02650 run = se.value2 = ACRun [0];
02651
02652 #if TRACE
02653 trace_coeff(&se, plane, "4x4", 0, level, run);
02654 #endif
02655
02656 writeRunLevel_CABAC(currMB, &se, dataPart);
02657 rate += se.len;
02658
02659
02660 se.type = (intra4x4mode ? SE_LUM_AC_INTRA : SE_LUM_AC_INTER);
02661
02662 dataPart = &(currSlice->partArr[partMap[se.type]]);
02663
02664 for(k = 1; k <= 16 && level != 0; k++)
02665 {
02666 level = se.value1 = ACLevel[k];
02667 run = se.value2 = ACRun [k];
02668 #if TRACE
02669 trace_coeff(&se, plane, "4x4", k, level, run);
02670 #endif
02671 writeRunLevel_CABAC(currMB, &se, dataPart);
02672
02673 rate += se.len;
02674 }
02675
02676 bitCount[bits_coeff_mb] += rate;
02677
02678 return rate;
02679 }
02680
02681
02682
02683
02684
02685
02686
02687 int writeCoeff8x8_CABAC (Slice* currSlice, Macroblock* currMB, ColorPlane plane, int b8, int intra_mode)
02688 {
02689 int rate = 0;
02690 SyntaxElement se;
02691 const int* partMap = assignSE2partition[params->partition_mode];
02692 int* bitCount = currMB->bitcounter;
02693 DataPartition* dataPart;
02694
02695 int level, run;
02696 int k;
02697
02698 int pl_off = plane<<2;
02699 int bits_coeff_mb = ((plane == 0) ? (BITS_COEFF_Y_MB) : ((plane == 1) ? BITS_COEFF_CB_MB : BITS_COEFF_CR_MB));
02700 int* ACLevel = img->cofAC[b8+pl_off][0][0];
02701 int* ACRun = img->cofAC[b8+pl_off][0][1];
02702
02703 img->subblock_x = ((b8&0x1) == 0) ? 0 : 2;
02704 img->subblock_y = (b8 < 2) ? 0 : 2;
02705 img->is_intra_block = intra_mode;
02706
02707 se.context = ((plane==0) ? (LUMA_8x8) : ((plane == 1)? CB_8x8 : CR_8x8));
02708
02709
02710 level = se.value1 = ACLevel[0];
02711 run = se.value2 = ACRun [0];
02712
02713 se.type = (intra_mode ? SE_LUM_DC_INTRA : SE_LUM_DC_INTER);
02714
02715
02716 dataPart = &(currSlice->partArr[partMap[img->type != B_SLICE ? se.type : SE_BFRAME]]);
02717
02718 #if TRACE
02719 trace_coeff(&se, plane, "8x8", 0, level, run);
02720 #endif
02721
02722 writeRunLevel_CABAC(currMB, &se, dataPart);
02723 rate += se.len;
02724
02725
02726 se.type = (intra_mode ? SE_LUM_AC_INTRA : SE_LUM_AC_INTER);
02727
02728 dataPart = &(currSlice->partArr[partMap[img->type != B_SLICE ? se.type : SE_BFRAME]]);
02729
02730 for(k=1; k<=64 && level !=0; k++)
02731 {
02732 level = se.value1 = ACLevel[k];
02733 run = se.value2 = ACRun [k];
02734
02735 #if TRACE
02736 trace_coeff(&se, plane, "8x8", k, level, run);
02737 #endif
02738
02739 writeRunLevel_CABAC(currMB, &se, dataPart);
02740 rate += se.len;
02741 }
02742
02743 bitCount[bits_coeff_mb] += rate;
02744 return rate;
02745 }
02746
02747
02748
02749
02750
02751
02752
02753 int writeCoeff8x8 (Slice* currSlice, Macroblock* currMB, ColorPlane pl, int block8x8, int block_mode, int transform_size_flag)
02754 {
02755 int rate = 0;
02756 int is_intra = (block_mode == IBLOCK) || (block_mode == I8MB);
02757
02758
02759
02760
02761 if (currSlice->symbol_mode == CAVLC )
02762 {
02763 int block_color_type = ((pl == 0) ? LUMA : ((pl == 1) ? CB : CR));
02764
02765 rate += writeCoeff4x4_CAVLC (currSlice, currMB, block_color_type, block8x8, 0, is_intra);
02766 rate += writeCoeff4x4_CAVLC (currSlice, currMB, block_color_type, block8x8, 1, is_intra);
02767 rate += writeCoeff4x4_CAVLC (currSlice, currMB, block_color_type, block8x8, 2, is_intra);
02768 rate += writeCoeff4x4_CAVLC (currSlice, currMB, block_color_type, block8x8, 3, is_intra);
02769 }
02770 else
02771 {
02772 if(transform_size_flag)
02773 rate += writeCoeff8x8_CABAC (currSlice, currMB, pl, block8x8, is_intra);
02774 else
02775 {
02776 rate += writeCoeff4x4_CABAC (currSlice, currMB, pl, block8x8, 0, is_intra);
02777 rate += writeCoeff4x4_CABAC (currSlice, currMB, pl, block8x8, 1, is_intra);
02778 rate += writeCoeff4x4_CABAC (currSlice, currMB, pl, block8x8, 2, is_intra);
02779 rate += writeCoeff4x4_CABAC (currSlice, currMB, pl, block8x8, 3, is_intra);
02780 }
02781 }
02782
02783 return rate;
02784 }
02785
02786
02787
02788
02789
02790
02791
02792 int writeCBPandDquant (Slice* currSlice, Macroblock* currMB)
02793 {
02794
02795 int rate = 0;
02796 int* bitCount = currMB->bitcounter;
02797 SyntaxElement se;
02798 const int* partMap = assignSE2partition[params->partition_mode];
02799 int cbp = currMB->cbp;
02800 DataPartition* dataPart;
02801 int need_transform_size_flag;
02802
02803 se.value2 = 0;
02804
02805 if (currMB->mb_type != I16MB)
02806 {
02807
02808
02809 se.value1 = cbp;
02810 se.type = SE_CBP;
02811
02812
02813 dataPart = &(currSlice->partArr[partMap[se.type]]);
02814
02815 #if TRACE
02816 snprintf(se.tracestring, TRACESTRING_SIZE, "CBP (%2d,%2d) = %3d",img->mb_x, img->mb_y, cbp);
02817 #endif
02818 writeCBP (currMB, &se, dataPart);
02819
02820 bitCount[BITS_CBP_MB] += se.len;
02821 rate += se.len;
02822
02823
02824
02825 need_transform_size_flag = (((currMB->mb_type >= 1 && currMB->mb_type <= 3)||
02826 (IS_DIRECT(currMB) && active_sps->direct_8x8_inference_flag) ||
02827 (currMB->NoMbPartLessThan8x8Flag))
02828 && currMB->mb_type != I8MB && currMB->mb_type != I4MB
02829 && (currMB->cbp&15 || cmp_cbp[1]&15 || cmp_cbp[2] & 15)
02830 && params->Transform8x8Mode);
02831
02832 if (need_transform_size_flag)
02833 {
02834 se.value1 = currMB->luma_transform_size_8x8_flag;
02835 se.type = SE_MBTYPE;
02836
02837 #if TRACE
02838 snprintf(se.tracestring, TRACESTRING_SIZE, "transform_size_8x8_flag = %3d", currMB->luma_transform_size_8x8_flag);
02839 #endif
02840 writeMB_transform_size(&se, dataPart);
02841
02842 bitCount[BITS_MB_MODE] += se.len;
02843 rate += se.len;
02844 }
02845 }
02846
02847
02848
02849 if (cbp!=0 || (currMB->mb_type == I16MB))
02850 {
02851 se.value1 = currMB->delta_qp;
02852 se.type = SE_DELTA_QUANT;
02853
02854
02855 dataPart = &(currSlice->partArr[partMap[se.type]]);
02856 #if TRACE
02857 snprintf(se.tracestring, TRACESTRING_SIZE, "Delta QP (%2d,%2d) = %3d",img->mb_x, img->mb_y, currMB->delta_qp);
02858 #endif
02859 writeDquant (&se, dataPart);
02860 bitCount[BITS_DELTA_QUANT_MB] += se.len;
02861 rate += se.len;
02862 }
02863
02864 return rate;
02865 }
02866
02867
02868
02869
02870
02871
02872
02873
02874 int writeCoeff16x16 (Slice* currSlice, Macroblock* currMB, ColorPlane plane, int cbp)
02875 {
02876 int mb_x, mb_y, i, j, k;
02877 int level, run;
02878 int rate = 0;
02879 int block_rate = 0;
02880 int* bitCount = currMB->bitcounter;
02881 SyntaxElement se;
02882 const int* partMap = assignSE2partition[params->partition_mode];
02883 int pl_off = plane<<2;
02884 DataPartition* dataPart;
02885
02886 int b8, b4;
02887 int* DCLevel = img->cofDC[plane][0];
02888 int* DCRun = img->cofDC[plane][1];
02889 int* ACLevel;
02890 int* ACRun;
02891 int data_type;
02892 int bits_coeff_mb = ((plane == 0) ? (BITS_COEFF_Y_MB) : ((plane == 1) ? (BITS_COEFF_CB_MB) : (BITS_COEFF_CR_MB)));
02893
02894 if (img->P444_joined)
02895 {
02896 for (i=0; i < 4; i++)
02897 for (k=4*plane; k<4*(plane+1); k++)
02898 img->nz_coeff [currMB->mb_nr][i][k] = 0;
02899 }
02900 else
02901 {
02902 reset_mb_nz_coeff(currMB->mb_nr);
02903 }
02904
02905
02906 if (currMB->mb_type != I16MB)
02907 {
02908
02909
02910 for (i=0; i<4; i++)
02911 {
02912 if (cbp & (1<<i))
02913 {
02914 rate += writeCoeff8x8 (currSlice, currMB, plane, i, currMB->b8mode[i], currMB->luma_transform_size_8x8_flag);
02915 }
02916 }
02917 }
02918 else
02919 {
02920
02921
02922
02923 if (currSlice->symbol_mode == CAVLC)
02924 {
02925 switch (plane)
02926 {
02927 case 0:
02928 default:
02929 data_type = LUMA_INTRA16x16DC;
02930 break;
02931 case 1:
02932 data_type = CB_INTRA16x16DC;
02933 break;
02934 case 2:
02935 data_type = CR_INTRA16x16DC;
02936 break;
02937 }
02938 rate += writeCoeff4x4_CAVLC (currSlice, currMB, data_type, 0, 0, 0);
02939 }
02940 else
02941 {
02942 level=1;
02943 img->is_intra_block = TRUE;
02944 switch (plane)
02945 {
02946 case 0:
02947 default:
02948 data_type = LUMA_16DC;
02949 break;
02950 case 1:
02951 data_type = CB_16DC;
02952 break;
02953 case 2:
02954 data_type = CR_16DC;
02955 break;
02956 }
02957
02958 se.context = data_type;
02959 se.type = SE_LUM_DC_INTRA;
02960
02961
02962 dataPart = &(currSlice->partArr[partMap[se.type]]);
02963
02964 for (k=0; k<=16 && level!=0; k++)
02965 {
02966 level = se.value1 = DCLevel[k];
02967 run = se.value2 = DCRun [k];
02968 #if TRACE
02969 trace_coeff(&se, plane, "16x16 DC", k, level, run);
02970 #endif
02971 writeRunLevel_CABAC(currMB, &se, dataPart);
02972 bitCount[bits_coeff_mb] += se.len;
02973 rate += se.len;
02974 }
02975 }
02976
02977
02978 if (cbp & 15)
02979 {
02980 if (currSlice->symbol_mode == CAVLC)
02981 {
02982 switch (plane)
02983 {
02984 case 0:
02985 default:
02986 data_type = LUMA_INTRA16x16AC;
02987 break;
02988 case 1:
02989 data_type = CB_INTRA16x16AC;
02990 break;
02991 case 2:
02992 data_type = CR_INTRA16x16AC;
02993 break;
02994 }
02995
02996 for (b8 = 0; b8 < 4; b8 ++)
02997 {
02998 rate += writeCoeff4x4_CAVLC (currSlice, currMB, data_type, b8, 0, 0);
02999 rate += writeCoeff4x4_CAVLC (currSlice, currMB, data_type, b8, 1, 0);
03000 rate += writeCoeff4x4_CAVLC (currSlice, currMB, data_type, b8, 2, 0);
03001 rate += writeCoeff4x4_CAVLC (currSlice, currMB, data_type, b8, 3, 0);
03002 }
03003 }
03004 else
03005 {
03006 img->is_intra_block = TRUE;
03007 switch (plane)
03008 {
03009 case 0:
03010 default:
03011 data_type = LUMA_16AC;
03012 break;
03013 case 1:
03014 data_type = CB_16AC;
03015 break;
03016 case 2:
03017 data_type = CR_16AC;
03018 break;
03019 }
03020
03021 se.context = data_type;
03022 se.type = SE_LUM_AC_INTRA;
03023
03024
03025 dataPart = &(currSlice->partArr[partMap[se.type]]);
03026 block_rate = 0;
03027
03028 for (mb_y=0; mb_y < 4; mb_y += 2)
03029 {
03030 for (mb_x=0; mb_x < 4; mb_x += 2)
03031 {
03032 for (j=mb_y; j < mb_y+2; j++)
03033 {
03034 int j1 = 2*(j >> 1);
03035 int j2 = 2*(j & 0x01);
03036 img->subblock_y = j;
03037
03038 for (i=mb_x; i < mb_x+2; i++)
03039 {
03040 img->subblock_x = i;
03041 b8 = j1 + (i >> 1) + pl_off;
03042 b4 = j2 + (i & 0x01);
03043
03044 ACLevel = img->cofAC[b8][b4][0];
03045 ACRun = img->cofAC[b8][b4][1];
03046
03047 level=1;
03048
03049 for (k=0; k < 16 && level !=0; k++)
03050 {
03051 level = se.value1 = ACLevel[k];
03052 run = se.value2 = ACRun [k];
03053 #if TRACE
03054 trace_coeff(&se, plane, "16x16 AC", k, level, run);
03055 #endif
03056 writeRunLevel_CABAC(currMB, &se, dataPart);
03057 block_rate += se.len;
03058 }
03059 }
03060 }
03061 }
03062 }
03063
03064 rate += block_rate;
03065 bitCount[bits_coeff_mb] += block_rate;
03066 }
03067 }
03068 }
03069
03070 return rate;
03071 }
03072
03073
03074
03075
03076
03077
03078
03079
03080
03081 int predict_nnz(Macroblock *currMB, int block_type, int i,int j)
03082 {
03083 PixelPos pix;
03084
03085 int pred_nnz = 0;
03086 int cnt = 0;
03087
03088
03089 get4x4Neighbour(currMB, (i << 2) - 1, (j << 2), img->mb_size[IS_LUMA], &pix);
03090
03091 if (IS_INTRA(currMB) && pix.available && active_pps->constrained_intra_pred_flag && ((params->partition_mode != 0) && !img->currentPicture->idr_flag))
03092 {
03093 pix.available &= img->intra_block[pix.mb_addr];
03094 if (!pix.available)
03095 cnt++;
03096 }
03097
03098 if (pix.available)
03099 {
03100 switch (block_type)
03101 {
03102 case LUMA:
03103 pred_nnz += img->nz_coeff [pix.mb_addr ][pix.x][pix.y];
03104 cnt++;
03105 break;
03106 case CB:
03107 pred_nnz += img->nz_coeff [pix.mb_addr ][pix.x][4+pix.y];
03108 cnt++;
03109 break;
03110 case CR:
03111 pred_nnz += img->nz_coeff [pix.mb_addr ][pix.x][8+pix.y];
03112 cnt++;
03113 break;
03114 default:
03115 error("writeCoeff4x4_CAVLC: Invalid block type", 600);
03116 break;
03117 }
03118 }
03119
03120
03121 get4x4Neighbour(currMB, (i<<2), (j<<2) - 1, img->mb_size[IS_LUMA], &pix);
03122
03123 if (IS_INTRA(currMB) && pix.available && active_pps->constrained_intra_pred_flag && ((params->partition_mode != 0) && !img->currentPicture->idr_flag))
03124 {
03125 pix.available &= img->intra_block[pix.mb_addr];
03126 if (!pix.available)
03127 cnt++;
03128 }
03129
03130 if (pix.available)
03131 {
03132 switch (block_type)
03133 {
03134 case LUMA:
03135 pred_nnz += img->nz_coeff [pix.mb_addr ][pix.x][pix.y];
03136 cnt++;
03137 break;
03138 case CB:
03139 pred_nnz += img->nz_coeff [pix.mb_addr ][pix.x][4+pix.y];
03140 cnt++;
03141 break;
03142 case CR:
03143 pred_nnz += img->nz_coeff [pix.mb_addr ][pix.x][8+pix.y];
03144 cnt++;
03145 break;
03146 default:
03147 error("writeCoeff4x4_CAVLC: Invalid block type", 600);
03148 break;
03149 }
03150 }
03151
03152 if (cnt==2)
03153 {
03154 pred_nnz++;
03155 pred_nnz>>=1;
03156 }
03157
03158 return pred_nnz;
03159 }
03160
03161
03162
03163
03164
03165
03166
03167
03168
03169
03170 int predict_nnz_chroma(Macroblock *currMB, int i,int j)
03171 {
03172 PixelPos pix;
03173
03174 int pred_nnz = 0;
03175 int cnt = 0;
03176
03177 if (img->yuv_format != YUV444)
03178 {
03179
03180
03181 get4x4Neighbour(currMB, ((i & 0x01)<<2) - 1, ((j-4)<<2), img->mb_size[IS_CHROMA], &pix);
03182
03183 if (IS_INTRA(currMB) && pix.available && active_pps->constrained_intra_pred_flag && ((params->partition_mode != 0) && !img->currentPicture->idr_flag))
03184 {
03185 pix.available &= img->intra_block[pix.mb_addr];
03186 if (!pix.available)
03187 cnt++;
03188 }
03189
03190 if (pix.available)
03191 {
03192 pred_nnz = img->nz_coeff [pix.mb_addr ][2 * (i >> 1) + pix.x][4 + pix.y];
03193 cnt++;
03194 }
03195
03196
03197 get4x4Neighbour(currMB, ((i & 0x01)<<2), ((j-4)<<2) -1, img->mb_size[IS_CHROMA], &pix);
03198
03199 if (IS_INTRA(currMB) && pix.available && active_pps->constrained_intra_pred_flag && ((params->partition_mode != 0) && !img->currentPicture->idr_flag))
03200 {
03201 pix.available &= img->intra_block[pix.mb_addr];
03202 if (!pix.available)
03203 cnt++;
03204 }
03205
03206 if (pix.available)
03207 {
03208 pred_nnz += img->nz_coeff [pix.mb_addr ][2 * (i >> 1) + pix.x][4 + pix.y];
03209 cnt++;
03210 }
03211 }
03212
03213
03214 if (cnt==2)
03215 {
03216 pred_nnz++;
03217 pred_nnz>>=1;
03218 }
03219
03220 return pred_nnz;
03221 }
03222
03223
03224
03225
03226
03227
03228
03229
03230
03231
03232
03233
03234 int writeCoeff4x4_CAVLC (Slice* currSlice, Macroblock* currMB, int block_type, int b8, int b4, int param)
03235 {
03236 int no_bits = 0;
03237 SyntaxElement se;
03238 int *bitCount = currMB->bitcounter;
03239 DataPartition *dataPart;
03240 int *partMap = assignSE2partition[params->partition_mode];
03241
03242 int k,level = 1,run,vlcnum;
03243 int numcoeff = 0, lastcoeff = 0, numtrailingones = 0;
03244 int numones = 0, totzeros = 0, zerosleft, numcoef;
03245 int numcoeff_vlc;
03246 int code, level_two_or_higher;
03247 int dptype = 0, bitcounttype = 0;
03248 int nnz, max_coeff_num = 0, cdc = 0, cac = 0;
03249 int subblock_x, subblock_y;
03250 #if TRACE
03251 char type[15];
03252 #endif
03253
03254 static const int incVlc[] = {0, 3, 6, 12, 24, 48, 32768};
03255
03256
03257 int* pLevel = NULL;
03258 int* pRun = NULL;
03259
03260 switch (block_type)
03261 {
03262 case LUMA:
03263 max_coeff_num = 16;
03264 bitcounttype = BITS_COEFF_Y_MB;
03265
03266 pLevel = img->cofAC[b8][b4][0];
03267 pRun = img->cofAC[b8][b4][1];
03268 #if TRACE
03269 sprintf(type, "%s", "Luma");
03270 #endif
03271 dptype = (IS_INTRA (currMB)) ? SE_LUM_AC_INTRA : SE_LUM_AC_INTER;
03272 break;
03273 case LUMA_INTRA16x16DC:
03274 max_coeff_num = 16;
03275 bitcounttype = BITS_COEFF_Y_MB;
03276
03277 pLevel = img->cofDC[0][0];
03278 pRun = img->cofDC[0][1];
03279 #if TRACE
03280 sprintf(type, "%s", "Lum16DC");
03281 #endif
03282 dptype = SE_LUM_DC_INTRA;
03283 break;
03284 case LUMA_INTRA16x16AC:
03285 max_coeff_num = 15;
03286 bitcounttype = BITS_COEFF_Y_MB;
03287
03288 pLevel = img->cofAC[b8][b4][0];
03289 pRun = img->cofAC[b8][b4][1];
03290 #if TRACE
03291 sprintf(type, "%s", "Lum16AC");
03292 #endif
03293 dptype = SE_LUM_AC_INTRA;
03294 break;
03295 case CB:
03296 max_coeff_num = 16;
03297 bitcounttype = BITS_COEFF_CB_MB;
03298
03299 pLevel = img->cofAC[4+b8][b4][0];
03300 pRun = img->cofAC[4+b8][b4][1];
03301 #if TRACE
03302 sprintf(type, "%s", "CB");
03303 #endif
03304 dptype = (IS_INTRA (currMB)) ? SE_LUM_AC_INTRA : SE_LUM_AC_INTER;
03305 break;
03306 case CB_INTRA16x16DC:
03307 max_coeff_num = 16;
03308 bitcounttype = BITS_COEFF_CB_MB;
03309
03310 pLevel = img->cofDC[1][0];
03311 pRun = img->cofDC[1][1];
03312 #if TRACE
03313 sprintf(type, "%s", "CB_16DC");
03314 #endif
03315 dptype = SE_LUM_DC_INTRA;
03316 break;
03317 case CB_INTRA16x16AC:
03318 max_coeff_num = 15;
03319 bitcounttype = BITS_COEFF_CB_MB;
03320
03321 pLevel = img->cofAC[4+b8][b4][0];
03322 pRun = img->cofAC[4+b8][b4][1];
03323 #if TRACE
03324 sprintf(type, "%s", "CB_16AC");
03325 #endif
03326 dptype = SE_LUM_AC_INTRA;
03327 break;
03328
03329 case CR:
03330 max_coeff_num = 16;
03331 bitcounttype = BITS_COEFF_CR_MB;
03332
03333 pLevel = img->cofAC[8+b8][b4][0];
03334 pRun = img->cofAC[8+b8][b4][1];
03335 #if TRACE
03336 sprintf(type, "%s", "CR");
03337 #endif
03338 dptype = (IS_INTRA (currMB)) ? SE_LUM_AC_INTRA : SE_LUM_AC_INTER;
03339 break;
03340 case CR_INTRA16x16DC:
03341 max_coeff_num = 16;
03342 bitcounttype = BITS_COEFF_CR_MB;
03343
03344 pLevel = img->cofDC[2][0];
03345 pRun = img->cofDC[2][1];
03346 #if TRACE
03347 sprintf(type, "%s", "CR_16DC");
03348 #endif
03349 dptype = SE_LUM_DC_INTRA;
03350 break;
03351 case CR_INTRA16x16AC:
03352 max_coeff_num = 15;
03353 bitcounttype = BITS_COEFF_CR_MB;
03354
03355 pLevel = img->cofAC[8+b8][b4][0];
03356 pRun = img->cofAC[8+b8][b4][1];
03357 #if TRACE
03358 sprintf(type, "%s", "CR_16AC");
03359 #endif
03360 dptype = SE_LUM_AC_INTRA;
03361 break;
03362
03363 case CHROMA_DC:
03364 max_coeff_num = img->num_cdc_coeff;
03365 bitcounttype = BITS_COEFF_UV_MB;
03366 cdc = 1;
03367
03368 pLevel = img->cofDC[param+1][0];
03369 pRun = img->cofDC[param+1][1];
03370 #if TRACE
03371 sprintf(type, "%s", "ChrDC");
03372 #endif
03373 dptype = (IS_INTRA (currMB)) ? SE_CHR_DC_INTRA : SE_CHR_DC_INTER;
03374 break;
03375 case CHROMA_AC:
03376 max_coeff_num = 15;
03377 bitcounttype = BITS_COEFF_UV_MB;
03378 cac = 1;
03379
03380 pLevel = img->cofAC[b8][b4][0];
03381 pRun = img->cofAC[b8][b4][1];
03382 #if TRACE
03383 sprintf(type, "%s", "ChrAC");
03384 #endif
03385 dptype = (IS_INTRA (currMB)) ? SE_CHR_AC_INTRA : SE_CHR_AC_INTER;
03386 break;
03387 default:
03388 error("writeCoeff4x4_CAVLC: Invalid block type", 600);
03389 break;
03390 }
03391
03392 dataPart = &(currSlice->partArr[partMap[dptype]]);
03393
03394 for(k = 0; (k <= ((cdc) ? img->num_cdc_coeff : 16)) && level != 0; k++)
03395 {
03396 level = pLevel[k];
03397 run = pRun[k];
03398
03399 if (level)
03400 {
03401
03402 totzeros += run;
03403 if (iabs(level) == 1)
03404 {
03405 numones ++;
03406 numtrailingones ++;
03407 numtrailingones = imin(numtrailingones, 3);
03408 }
03409 else
03410 {
03411 numtrailingones = 0;
03412 }
03413 numcoeff ++;
03414 lastcoeff = k;
03415 }
03416 }
03417
03418 if (!cdc)
03419 {
03420 if(block_type==LUMA || block_type==LUMA_INTRA16x16DC || block_type==LUMA_INTRA16x16AC
03421 ||block_type==CHROMA_AC)
03422 {
03423 if (!cac)
03424 {
03425
03426 subblock_x = ((b8 & 0x1) == 0) ? (((b4 & 0x1) == 0) ? 0 : 1) : (((b4 & 0x1) == 0) ? 2 : 3);
03427
03428 subblock_y = (b8 < 2) ? ((b4 < 2) ? 0 : 1) : ((b4 < 2) ? 2 : 3);
03429
03430 nnz = predict_nnz(currMB, LUMA, subblock_x,subblock_y);
03431 }
03432 else
03433 {
03434
03435 subblock_x = param >> 4;
03436 subblock_y = param & 15;
03437 nnz = predict_nnz_chroma(currMB, subblock_x, subblock_y);
03438 }
03439 img->nz_coeff [currMB->mb_nr ][subblock_x][subblock_y] = numcoeff;
03440 }
03441 else if (block_type==CB || block_type==CB_INTRA16x16DC
03442 || block_type==CB_INTRA16x16AC)
03443 {
03444 subblock_x = ((b8 & 0x1) == 0)?(((b4 & 0x1) == 0) ? 0 : 1):(((b4 & 0x1) == 0) ? 2 : 3);
03445
03446 subblock_y = (b8 < 2) ? ((b4 < 2) ? 0 : 1) : ((b4 < 2) ? 2 : 3);
03447
03448 nnz = predict_nnz(currMB, CB, subblock_x,subblock_y);
03449 img->nz_coeff [currMB->mb_nr ][subblock_x][4+subblock_y] = numcoeff;
03450 }
03451 else
03452 {
03453 subblock_x = ((b8 & 0x1) == 0)?(((b4 & 0x1) == 0) ? 0 : 1) : (((b4 & 0x1) == 0) ? 2 : 3);
03454
03455 subblock_y = (b8 < 2) ? ((b4 < 2) ? 0 : 1) : ((b4 < 2) ? 2 : 3);
03456
03457 nnz = predict_nnz(currMB, CR, subblock_x,subblock_y);
03458 img->nz_coeff [currMB->mb_nr ][subblock_x][8+subblock_y] = numcoeff;
03459 }
03460
03461 if (nnz < 2)
03462 {
03463 numcoeff_vlc = 0;
03464 }
03465 else if (nnz < 4)
03466 {
03467 numcoeff_vlc = 1;
03468 }
03469 else if (nnz < 8)
03470 {
03471 numcoeff_vlc = 2;
03472 }
03473 else
03474 {
03475 numcoeff_vlc = 3;
03476 }
03477
03478 }
03479 else
03480 {
03481
03482
03483 numcoeff_vlc = 0;
03484
03485 subblock_x = param;
03486 subblock_y = param;
03487 }
03488
03489 se.type = dptype;
03490
03491 se.value1 = numcoeff;
03492 se.value2 = numtrailingones;
03493 se.len = numcoeff_vlc;
03494
03495 #if TRACE
03496 snprintf(se.tracestring,
03497 TRACESTRING_SIZE, "%s # c & tr.1s(%d,%d) vlc=%d #c=%d #t1=%d",
03498 type, subblock_x, subblock_y, numcoeff_vlc, numcoeff, numtrailingones);
03499 #endif
03500
03501 if (!cdc)
03502 writeSyntaxElement_NumCoeffTrailingOnes(&se, dataPart);
03503 else
03504 writeSyntaxElement_NumCoeffTrailingOnesChromaDC(&se, dataPart);
03505
03506 bitCount[bitcounttype] += se.len;
03507 no_bits += se.len;
03508
03509 if (!numcoeff)
03510 return no_bits;
03511
03512 if (numcoeff)
03513 {
03514 code = 0;
03515 for (k = lastcoeff; k > lastcoeff - numtrailingones; k--)
03516 {
03517 level = pLevel[k];
03518
03519 if (iabs(level) > 1)
03520 {
03521 printf("ERROR: level > 1\n");
03522 exit(-1);
03523 }
03524
03525 code <<= 1;
03526
03527 if (level < 0)
03528 {
03529 code |= 0x1;
03530 }
03531 }
03532
03533 if (numtrailingones)
03534 {
03535 se.type = dptype;
03536
03537 se.value2 = numtrailingones;
03538 se.value1 = code;
03539
03540 #if TRACE
03541 snprintf(se.tracestring,
03542 TRACESTRING_SIZE, "%s trailing ones sign (%d,%d)",
03543 type, subblock_x, subblock_y);
03544 #endif
03545
03546 writeSyntaxElement_VLC (&se, dataPart);
03547 bitCount[bitcounttype] += se.len;
03548 no_bits += se.len;
03549
03550 }
03551
03552
03553 level_two_or_higher = (numcoeff > 3 && numtrailingones == 3) ? 0 : 1;
03554
03555 vlcnum = (numcoeff > 10 && numtrailingones < 3) ? 1 : 0;
03556
03557 for (k = lastcoeff - numtrailingones; k >= 0; k--)
03558 {
03559 level = pLevel[k];
03560
03561 se.value1 = level;
03562 se.type = dptype;
03563
03564 #if TRACE
03565 snprintf(se.tracestring,
03566 TRACESTRING_SIZE, "%s lev (%d,%d) k=%d vlc=%d lev=%3d",
03567 type, subblock_x, subblock_y, k, vlcnum, level);
03568 #endif
03569
03570 if (level_two_or_higher)
03571 {
03572 level_two_or_higher = 0;
03573
03574 if (se.value1 > 0)
03575 se.value1 --;
03576 else
03577 se.value1 ++;
03578 }
03579
03580
03581
03582 if (vlcnum == 0)
03583 writeSyntaxElement_Level_VLC1(&se, dataPart, active_sps->profile_idc);
03584 else
03585 writeSyntaxElement_Level_VLCN(&se, vlcnum, dataPart, active_sps->profile_idc);
03586
03587
03588 if (iabs(level) > incVlc[vlcnum])
03589 vlcnum++;
03590
03591 if ((k == lastcoeff - numtrailingones) && iabs(level) > 3)
03592 vlcnum = 2;
03593
03594 bitCount[bitcounttype] += se.len;
03595 no_bits += se.len;
03596 }
03597
03598
03599 if (numcoeff < max_coeff_num)
03600 {
03601
03602 se.type = dptype;
03603 se.value1 = totzeros;
03604
03605 vlcnum = numcoeff - 1;
03606
03607 se.len = vlcnum;
03608
03609 #if TRACE
03610 snprintf(se.tracestring,
03611 TRACESTRING_SIZE, "%s totalrun (%d,%d) vlc=%d totzeros=%3d",
03612 type, subblock_x, subblock_y, vlcnum, totzeros);
03613 #endif
03614 if (!cdc)
03615 writeSyntaxElement_TotalZeros(&se, dataPart);
03616 else
03617 writeSyntaxElement_TotalZerosChromaDC(&se, dataPart);
03618
03619 bitCount[bitcounttype] += se.len;
03620 no_bits += se.len;
03621 }
03622
03623
03624 zerosleft = totzeros;
03625 numcoef = numcoeff;
03626 for (k = lastcoeff; k >= 0; k--)
03627 {
03628 run = pRun[k];
03629
03630 se.value1 = run;
03631 se.type = dptype;
03632
03633
03634
03635 if ((!zerosleft) || (numcoeff <= 1 ))
03636 break;
03637
03638 if (numcoef > 1 && zerosleft)
03639 {
03640 vlcnum = imin(zerosleft - 1, RUNBEFORE_NUM_M1);
03641 se.len = vlcnum;
03642
03643 #if TRACE
03644 snprintf(se.tracestring,
03645 TRACESTRING_SIZE, "%s run (%d,%d) k=%d vlc=%d run=%2d",
03646 type, subblock_x, subblock_y, k, vlcnum, run);
03647 #endif
03648
03649 writeSyntaxElement_Run(&se, dataPart);
03650
03651 bitCount[bitcounttype] += se.len;
03652 no_bits += se.len;
03653
03654 zerosleft -= run;
03655 numcoef --;
03656 }
03657 }
03658 }
03659
03660 return no_bits;
03661 }
03662
03663 int distortion_hadamard(imgpel **img_org, imgpel **pred_img)
03664 {
03665 imgpel *cur_img, *prd_img;
03666 int current_intra_sad_2 = 0;
03667 int ii, jj, i, j;
03668
03669 for (j = 0; j < MB_BLOCK_SIZE; j++)
03670 {
03671 cur_img = &img_org[img->opix_y + j][img->opix_x];
03672 prd_img = pred_img[j];
03673 for (i = 0; i < MB_BLOCK_SIZE; i++)
03674 {
03675 M0[j >> 2][i >> 2][j & 0x03][i & 0x03] = cur_img[i] - prd_img[i];
03676 }
03677 }
03678
03679 for (jj=0;jj<4;jj++)
03680 {
03681 for (ii=0;ii<4;ii++)
03682 {
03683 M7 = M0[jj][ii];
03684 hadamard4x4(M7, M7);
03685 for (j=0;j<4;j++)
03686 {
03687 for (i=0;i<4;i++)
03688 {
03689 if((i + j) != 0)
03690 current_intra_sad_2 += iabs(M7[j][i]);
03691 }
03692 }
03693 }
03694 }
03695
03696 for (j=0;j<4;j++)
03697 {
03698 for (i=0;i<4;i++)
03699 M4[j][i] = (M0[j][i][0][0] >> 1);
03700 }
03701
03702 hadamard4x4(M4, M4);
03703 for (j=0;j<4;j++)
03704 {
03705 for (i=0;i<4;i++)
03706 {
03707 current_intra_sad_2 += iabs(M4[j][i]);
03708 }
03709 }
03710
03711 return current_intra_sad_2;
03712 }
03713
03714 int distortion_sad(imgpel **img_org, imgpel **pred_img)
03715 {
03716 imgpel *cur_img, *prd_img;
03717 int current_intra_sad_2 = 0;
03718 int i, j;
03719
03720 for (j=0;j<16;j++)
03721 {
03722 cur_img = &img_org[img->opix_y + j][img->opix_x];
03723 prd_img = pred_img[j];
03724 for (i=0;i<16;i++)
03725 {
03726 current_intra_sad_2 += iabs( *cur_img++ - *prd_img++ );
03727 }
03728 }
03729 return current_intra_sad_2;
03730 }
03731
03732 int distortion_sse(imgpel **img_org, imgpel **pred_img)
03733 {
03734 imgpel *cur_img, *prd_img;
03735 int current_intra_sad_2 = 0;
03736 int i, j;
03737
03738 for (j=0;j<16;j++)
03739 {
03740 cur_img = &img_org[img->opix_y + j][img->opix_x];
03741 prd_img = pred_img[j];
03742 for (i=0;i<16;i++)
03743 {
03744 current_intra_sad_2 += img->quad[ *cur_img++ - *prd_img++ ];
03745 }
03746 }
03747 return current_intra_sad_2;
03748 }
03749
03750
03751
03752
03753
03754
03755
03756
03757
03758
03759
03760
03761 double find_sad_16x16_JM(Macroblock *currMB, int *intra_mode)
03762 {
03763 double current_intra_sad_2, best_intra_sad2 = 1e30;
03764
03765 int i,k;
03766 imgpel ***curr_mpr_16x16 = img->mpr_16x16[0];
03767
03768 PixelPos up;
03769 PixelPos left[17];
03770
03771 int up_avail, left_avail, left_up_avail;
03772
03773 for (i=0;i<17;i++)
03774 {
03775 getNeighbour(currMB, -1 , i - 1 , img->mb_size[IS_LUMA], &left[i]);
03776 }
03777
03778 getNeighbour(currMB, 0 , -1 , img->mb_size[IS_LUMA], &up);
03779
03780 if (!(params->UseConstrainedIntraPred))
03781 {
03782 up_avail = up.available;
03783 left_avail = left[1].available;
03784 left_up_avail = left[0].available;
03785 }
03786 else
03787 {
03788 up_avail = up.available ? img->intra_block[up.mb_addr] : 0;
03789 for (i = 1, left_avail = 1; i < 17;i++)
03790 left_avail &= left[i].available ? img->intra_block[left[i].mb_addr]: 0;
03791 left_up_avail = left[0].available ? img->intra_block[left[0].mb_addr]: 0;
03792 }
03793
03794 *intra_mode = DC_PRED_16;
03795
03796 for (k = 0;k < 4; k++)
03797 {
03798 if (params->IntraDisableInterOnly == 0 || img->type != I_SLICE)
03799 {
03800 if (params->Intra16x16ParDisable && (k==VERT_PRED_16||k==HOR_PRED_16))
03801 continue;
03802
03803 if (params->Intra16x16PlaneDisable && k==PLANE_16)
03804 continue;
03805 }
03806
03807 if (!((k==0 && !up_avail) || (k==1 && !left_avail) || (k==3 && (!left_avail || !up_avail || !left_up_avail))))
03808 {
03809 switch(params->ModeDecisionMetric)
03810 {
03811 case ERROR_SAD:
03812 current_intra_sad_2 = distortion_sad(pCurImg, curr_mpr_16x16[k]);
03813 if (img->P444_joined)
03814 {
03815 current_intra_sad_2 += distortion_sad(pImgOrg[1], img->mpr_16x16[1][k]);
03816 current_intra_sad_2 += distortion_sad(pImgOrg[2], img->mpr_16x16[2][k]);
03817 }
03818 break;
03819 case ERROR_SSE:
03820 current_intra_sad_2 = distortion_sse(pCurImg, curr_mpr_16x16[k]);
03821 if (img->P444_joined)
03822 {
03823 current_intra_sad_2 += distortion_sse(pImgOrg[1], img->mpr_16x16[1][k]);
03824 current_intra_sad_2 += distortion_sse(pImgOrg[2], img->mpr_16x16[2][k]);
03825 }
03826 break;
03827 case ERROR_SATD :
03828 default:
03829 current_intra_sad_2 = distortion_hadamard(pCurImg, curr_mpr_16x16[k]);
03830 if (img->P444_joined)
03831 {
03832 current_intra_sad_2 += distortion_hadamard(pImgOrg[1], img->mpr_16x16[1][k]);
03833 current_intra_sad_2 += distortion_hadamard(pImgOrg[2], img->mpr_16x16[2][k]);
03834 }
03835 break;
03836 }
03837
03838 if(current_intra_sad_2 < best_intra_sad2)
03839 {
03840 best_intra_sad2 = current_intra_sad_2;
03841 *intra_mode = k;
03842 }
03843 }
03844 }
03845
03846 return best_intra_sad2;
03847 }
03848
03849
03850
03851
03852
03853
03854
03855
03856
03857
03858 void change_plane_JV( int nplane )
03859 {
03860 img->colour_plane_id = nplane;
03861 img->mb_data = img->mb_data_JV[nplane];
03862 enc_picture = enc_frame_picture_JV[nplane];
03863 pCurImg = imgData.frm_data[nplane];
03864 Co_located = Co_located_JV[nplane];
03865 }
03866
03867
03868
03869
03870
03871
03872 void make_frame_picture_JV()
03873 {
03874 int uv, line;
03875 int nsize;
03876 int nplane;
03877 enc_frame_picture[0] = enc_frame_picture_JV[0];
03878
03879 for( nplane=0; nplane<MAX_PLANE; nplane++ )
03880 {
03881 copy_storable_param_JV( nplane, enc_frame_picture[0], enc_frame_picture_JV[nplane] );
03882 }
03883
03884 for( uv=0; uv<2; uv++ )
03885 {
03886 for( line=0; line<img->height; line++ )
03887 {
03888 nsize = sizeof(imgpel) * img->width;
03889 memcpy( enc_frame_picture[0]->imgUV[uv][line], enc_frame_picture_JV[uv+1]->imgY[line], nsize );
03890 }
03891 free_storable_picture(enc_frame_picture_JV[uv+1]);
03892 }
03893 }
03894