00001
00024 #include "contributors.h"
00025
00026 #include <math.h>
00027
00028 #include "block.h"
00029 #include "global.h"
00030 #include "mbuffer.h"
00031 #include "elements.h"
00032 #include "errorconcealment.h"
00033 #include "macroblock.h"
00034 #include "fmo.h"
00035 #include "cabac.h"
00036 #include "vlc.h"
00037 #include "image.h"
00038 #include "mb_access.h"
00039 #include "biaridecod.h"
00040 #include "transform8x8.h"
00041 #include "transform.h"
00042 #include "mc_prediction.h"
00043 #include "quant.h"
00044 #include "intra4x4_pred.h"
00045 #include "intra8x8_pred.h"
00046 #include "intra16x16_pred.h"
00047 #include "mv_prediction.h"
00048
00049 #if TRACE
00050 #define TRACE_STRING(s) strncpy(currSE.tracestring, s, TRACESTRING_SIZE)
00051 #define TRACE_DECBITS(i) dectracebitcnt(1)
00052 #define TRACE_PRINTF(s) sprintf(type, "%s", s);
00053 #define TRACE_STRING_P(s) strncpy(currSE->tracestring, s, TRACESTRING_SIZE)
00054 #else
00055 #define TRACE_STRING(s)
00056 #define TRACE_DECBITS(i)
00057 #define TRACE_PRINTF(s)
00058 #define TRACE_STRING_P(s)
00059 #endif
00060
00062 void dectracebitcnt(int count);
00063
00064 static void read_motion_info_from_NAL_p_slice (Macroblock *currMB);
00065 static void read_motion_info_from_NAL_b_slice (Macroblock *currMB);
00066 static void read_ipred_modes (Macroblock *currMB);
00067 static void read_CBP_and_coeffs_from_NAL_CABAC (Macroblock *currMB);
00068 static void read_CBP_and_coeffs_from_NAL_CAVLC (Macroblock *currMB);
00069 static void read_IPCM_coeffs_from_NAL (Slice *currSlice, struct datapartition *dP);
00070 static void read_one_macroblock_i_slice (Macroblock *currMB);
00071 static void read_one_macroblock_p_slice (Macroblock *currMB);
00072 static void read_one_macroblock_b_slice (Macroblock *currMB);
00073 static int decode_one_component_i_slice (Macroblock *currMB, ColorPlane curr_plane, imgpel **currImg, StorablePicture *dec_picture, MotionParams *colocated, int list_offset);
00074 static int decode_one_component_p_slice (Macroblock *currMB, ColorPlane curr_plane, imgpel **currImg, StorablePicture *dec_picture, MotionParams *colocated, int list_offset);
00075 static int decode_one_component_b_slice (Macroblock *currMB, ColorPlane curr_plane, imgpel **currImg, StorablePicture *dec_picture, MotionParams *colocated, int list_offset);
00076
00083 static inline int BType2CtxRef (int btype)
00084 {
00085 return (btype >= 4);
00086 }
00087
00094 static char readRefPictureIdx_VLC(Macroblock *currMB, SyntaxElement *currSE, DataPartition *dP, char b8mode, int list)
00095 {
00096 #if TRACE
00097 static char tstring[20];
00098 sprintf( tstring, "ref_idx_l%d", list);
00099 strncpy(currSE->tracestring, tstring, TRACESTRING_SIZE);
00100 #endif
00101 currSE->context = BType2CtxRef (b8mode);
00102 currSE->value2 = list;
00103 dP->readSyntaxElement (currSE, currMB->p_Img, dP);
00104 return (char) currSE->value1;
00105 }
00106
00113 static char readRefPictureIdx_FLC(Macroblock *currMB, SyntaxElement *currSE, DataPartition *dP, char b8mode, int list)
00114 {
00115 #if TRACE
00116 char tstring[20];
00117 sprintf( tstring, "ref_idx_l%d", list);
00118 strncpy(currSE->tracestring, tstring, TRACESTRING_SIZE);
00119 #endif
00120
00121 currSE->context = BType2CtxRef (b8mode);
00122 currSE->len = 1;
00123 readSyntaxElement_FLC(currSE, dP->bitstream);
00124 currSE->value1 = 1 - currSE->value1;
00125
00126 return (char) currSE->value1;
00127 }
00128
00135 static char readRefPictureIdx_Null(Macroblock *currMB, SyntaxElement *currSE, DataPartition *dP, char b8mode, int list)
00136 {
00137 return 0;
00138 }
00139
00146 static inline void prepareListforRefIdx ( Macroblock *currMB, SyntaxElement *currSE, DataPartition *dP, int num_ref_idx_active, int refidx_present)
00147 {
00148 currMB->readRefPictureIdx = readRefPictureIdx_Null;
00149 if(num_ref_idx_active > 1)
00150 {
00151 if (currMB->p_Img->active_pps->entropy_coding_mode_flag == CAVLC || dP->bitstream->ei_flag)
00152 {
00153 currSE->mapping = linfo_ue;
00154 if (refidx_present)
00155 {
00156 if (num_ref_idx_active == 2)
00157 currMB->readRefPictureIdx = readRefPictureIdx_FLC;
00158 else
00159 currMB->readRefPictureIdx = readRefPictureIdx_VLC;
00160 }
00161 }
00162 else
00163 {
00164 currSE->reading = readRefFrame_CABAC;
00165 if (refidx_present)
00166 currMB->readRefPictureIdx = readRefPictureIdx_VLC;
00167 }
00168 }
00169 }
00170
00171 void set_chroma_qp(Macroblock* currMB)
00172 {
00173 ImageParameters *p_Img = currMB->p_Img;
00174 StorablePicture *dec_picture = p_Img->dec_picture;
00175 int i;
00176 for (i=0; i<2; ++i)
00177 {
00178 currMB->qpc[i] = iClip3 ( -p_Img->bitdepth_chroma_qp_scale, 51, currMB->qp + dec_picture->chroma_qp_offset[i] );
00179 currMB->qpc[i] = currMB->qpc[i] < 0 ? currMB->qpc[i] : QP_SCALE_CR[currMB->qpc[i]];
00180 currMB->qp_scaled[i + 1] = currMB->qpc[i] + p_Img->bitdepth_chroma_qp_scale;
00181 }
00182 }
00183
00190 void update_qp(Macroblock *currMB, int qp)
00191 {
00192 ImageParameters *p_Img = currMB->p_Img;
00193 currMB->qp = qp;
00194 currMB->qp_scaled[0] = qp + p_Img->bitdepth_luma_qp_scale;
00195 set_chroma_qp(currMB);
00196 currMB->is_lossless = (Boolean) ((currMB->qp_scaled[0] == 0) && (p_Img->lossless_qpprime_flag == 1));
00197 }
00198
00199 static void read_delta_quant(SyntaxElement *currSE, DataPartition *dP, Macroblock *currMB, const byte *partMap, int type)
00200 {
00201 Slice *currSlice = currMB->p_Slice;
00202 ImageParameters *p_Img = currMB->p_Img;
00203
00204 currSE->type = type;
00205
00206 dP = &(currSlice->partArr[partMap[currSE->type]]);
00207
00208 if (p_Img->active_pps->entropy_coding_mode_flag == CAVLC || dP->bitstream->ei_flag)
00209 {
00210 currSE->mapping = linfo_se;
00211 }
00212 else
00213 currSE->reading= readDquant_CABAC;
00214
00215 TRACE_STRING_P("mb_qp_delta");
00216
00217 dP->readSyntaxElement(currSE,p_Img,dP);
00218 currMB->delta_quant = (short) currSE->value1;
00219 if ((currMB->delta_quant < -(26 + p_Img->bitdepth_luma_qp_scale/2)) || (currMB->delta_quant > (25 + p_Img->bitdepth_luma_qp_scale/2)))
00220 error ("mb_qp_delta is out of range", 500);
00221
00222 p_Img->qp = ((p_Img->qp + currMB->delta_quant + 52 + 2*p_Img->bitdepth_luma_qp_scale)%(52+p_Img->bitdepth_luma_qp_scale)) -
00223 p_Img->bitdepth_luma_qp_scale;
00224 update_qp(currMB, p_Img->qp);
00225 }
00226
00233 static void readMBRefPictureIdx (SyntaxElement *currSE, DataPartition *dP, Macroblock *currMB, char **cur_ref_idx, int list, int step_v0, int step_h0)
00234 {
00235 int k, j, j0, i0;
00236 char refframe;
00237
00238 for (j0 = 0; j0 < 4; j0 += step_v0)
00239 {
00240 currMB->subblock_y = j0 << 2;
00241 for (i0 = 0; i0 < 4; i0 += step_h0)
00242 {
00243 currMB->subblock_x = i0 << 2;
00244 k = 2 * (j0 >> 1) + (i0 >> 1);
00245
00246 if ((currMB->b8pdir[k] == list || currMB->b8pdir[k] == BI_PRED) && currMB->b8mode[k] != 0)
00247 {
00248 refframe = currMB->readRefPictureIdx(currMB, currSE, dP, currMB->b8mode[k], list);
00249
00250 for (j = j0; j < j0 + step_v0; ++j)
00251 memset(&cur_ref_idx[j][currMB->block_x + i0], refframe, step_h0 * sizeof(char));
00252 }
00253 }
00254 }
00255 }
00256
00263 static inline void readMBMotionVectors (SyntaxElement *currSE, DataPartition *dP, Macroblock *currMB, int list, int step_h0, int step_v0)
00264 {
00265 int i, j, k, i4, j4, ii, jj, kk, i0, j0;
00266 short curr_mvd[2], curr_mv[2], pred_mv[2];
00267 short (*mvd)[4][2];
00268 short ***mv;
00269 int mv_mode, step_h, step_v;
00270 char cur_ref_idx;
00271 ImageParameters *p_Img = currMB->p_Img;
00272 StorablePicture *dec_picture = p_Img->dec_picture;
00273 PicMotionParams *motion = &dec_picture->motion;
00274 PixelPos block[4];
00275
00276
00277 for (j0=0; j0<4; j0+=step_v0)
00278 {
00279 for (i0=0; i0<4; i0+=step_h0)
00280 {
00281 kk = 2 * (j0 >> 1) + (i0 >> 1);
00282 if ((currMB->b8pdir[kk]== list || currMB->b8pdir[kk]== BI_PRED) && (currMB->b8mode[kk] !=0))
00283 {
00284 cur_ref_idx = motion->ref_idx[list][currMB->block_y+j0][currMB->block_x+i0];
00285 mv_mode = currMB->b8mode[kk];
00286 step_h = BLOCK_STEP [mv_mode][0];
00287 step_v = BLOCK_STEP [mv_mode][1];
00288
00289 for (j = j0; j < j0 + step_v0; j += step_v)
00290 {
00291 currMB->subblock_y = j << 2;
00292 j4 = currMB->block_y + j;
00293 mv = &motion->mv [list][j4];
00294 mvd = &currMB->mvd [list][j];
00295 for (i = i0; i < i0 + step_h0; i += step_h)
00296 {
00297 currMB->subblock_x = i << 2;
00298 i4 = currMB->block_x + i;
00299
00300 get_neighbors(currMB, block, BLOCK_SIZE * i, BLOCK_SIZE * j, 4 * step_h);
00301
00302
00303 currMB->GetMVPredictor (currMB, block, pred_mv, cur_ref_idx, motion->ref_idx[list], motion->mv[list], BLOCK_SIZE * i, BLOCK_SIZE * j, 4 * step_h, 4 * step_v);
00304
00305 for (k=0; k < 2; ++k)
00306 {
00307 #if TRACE
00308 char tstring[20];
00309 sprintf( tstring, "mvd_l%d", list);
00310 strncpy(currSE->tracestring, tstring, TRACESTRING_SIZE);
00311 #endif
00312 currSE->value2 = (k << 1) + list;
00313 dP->readSyntaxElement(currSE,p_Img,dP);
00314 curr_mvd[k] = (short) currSE->value1;
00315 curr_mv [k] = (short)(curr_mvd[k] + pred_mv[k]);
00316 }
00317
00318
00319 for(ii = i4; ii < i4 + step_h; ++ii)
00320 {
00321 memcpy(&mv[0][ii][0], curr_mv, 2 * sizeof(short));
00322 }
00323
00324 for(jj = 1; jj < step_v; ++jj)
00325 {
00326 memcpy(&mv[jj][i4][0], &mv[0][i4][0], 2 * step_h * sizeof(short));
00327 }
00328
00329
00330 for(ii = i; ii < i + step_h; ++ii)
00331 {
00332 memcpy(&mvd[0][ii][0], curr_mvd, 2 * sizeof(short));
00333 }
00334
00335
00336 for(jj = 1; jj < step_v; ++jj)
00337 {
00338 memcpy(&mvd[jj][i][0], &mvd[0][i][0], 2 * step_h * sizeof(short));
00339 }
00340 }
00341 }
00342 }
00343 }
00344 }
00345 }
00346
00347 void invScaleCoeff(Macroblock *currMB, int level, int run, int qp_per, int i, int j, int i0, int j0, int coef_ctr, const byte (*pos_scan4x4)[2], int (*InvLevelScale4x4)[4])
00348 {
00349 Slice *currSlice = currMB->p_Slice;
00350 if (level != 0)
00351 {
00352 coef_ctr += run + 1;
00353
00354 i0=pos_scan4x4[coef_ctr][0];
00355 j0=pos_scan4x4[coef_ctr][1];
00356
00357 currMB->cbp_blk[0] |= (int64)1 << ((j << 2) + i) ;
00358 currSlice->cof[0][(j<<2) + j0][(i<<2) + i0]= rshift_rnd_sf((level * InvLevelScale4x4[j0][i0]) << qp_per, 4);
00359 }
00360 }
00361
00368 void start_macroblock(Slice *currSlice, Macroblock **currMB)
00369 {
00370 ImageParameters *p_Img = currSlice->p_Img;
00371 StorablePicture *dec_picture = p_Img->dec_picture;
00372 int mb_nr = p_Img->current_mb_nr;
00373
00374 *currMB = &p_Img->mb_data[mb_nr];
00375
00376 (*currMB)->p_Img = p_Img;
00377 (*currMB)->p_Slice = currSlice;
00378 (*currMB)->mbAddrX = mb_nr;
00379
00380
00381
00382
00383 if (currSlice->MbaffFrameFlag)
00384 {
00385 (*currMB)->mb_x = (mb_nr) % ((2*p_Img->width) / MB_BLOCK_SIZE);
00386 (*currMB)->mb_y = 2*((mb_nr) / ((2*p_Img->width) / MB_BLOCK_SIZE));
00387
00388 (*currMB)->mb_y += ((*currMB)->mb_x & 0x01);
00389 (*currMB)->mb_x >>= 1;
00390 }
00391 else
00392 {
00393 (*currMB)->mb_x = PicPos[mb_nr][0];
00394 (*currMB)->mb_y = PicPos[mb_nr][1];
00395 }
00396
00397
00398 (*currMB)->block_y = (*currMB)->mb_y * BLOCK_SIZE;
00399 (*currMB)->block_y_aff = (*currMB)->block_y;
00400 (*currMB)->pix_y = (*currMB)->mb_y * MB_BLOCK_SIZE;
00401 (*currMB)->pix_c_y = (*currMB)->mb_y * p_Img->mb_cr_size_y;
00402
00403
00404 (*currMB)->block_x = (*currMB)->mb_x * BLOCK_SIZE;
00405 (*currMB)->pix_x = (*currMB)->mb_x * MB_BLOCK_SIZE;
00406 (*currMB)->pix_c_x = (*currMB)->mb_x * p_Img->mb_cr_size_x;
00407
00408
00409
00410 (*currMB)->slice_nr = (short) p_Img->current_slice_nr;
00411
00412 if (p_Img->current_slice_nr >= MAX_NUM_SLICES)
00413 {
00414 error ("Maximum number of supported slices exceeded. \nPlease recompile with increased value for MAX_NUM_SLICES", 200);
00415 }
00416
00417 dec_picture->slice_id[(*currMB)->mb_y][(*currMB)->mb_x] = (short) p_Img->current_slice_nr;
00418 dec_picture->max_slice_id = (short) imax(p_Img->current_slice_nr, dec_picture->max_slice_id);
00419
00420 CheckAvailabilityOfNeighbors(*currMB);
00421
00422
00423 InitMotionVectorPrediction(*currMB, currSlice->MbaffFrameFlag);
00424
00425
00426 update_qp(*currMB, p_Img->qp);
00427 (*currMB)->mb_type = 0;
00428 (*currMB)->delta_quant = 0;
00429 (*currMB)->cbp = 0;
00430 memset((*currMB)->cbp_blk, 0, 3 * sizeof(int64));
00431 (*currMB)->c_ipred_mode = DC_PRED_8;
00432
00433 if (currSlice->slice_type != I_SLICE)
00434 memset(&((*currMB)->mvd[0][0][0][0]),0, 2 * BLOCK_MULTIPLE * BLOCK_MULTIPLE * 2 * sizeof(short));
00435
00436 memset((*currMB)->cbp_bits, 0, 3 * sizeof(int64));
00437 memset((*currMB)->cbp_bits_8x8, 0, 3 * sizeof(int64));
00438
00439
00440 memset(&(currSlice->mb_rres[0][0][0]), 0, 3 * MB_PIXELS * sizeof(int));
00441
00442
00443 (*currMB)->DFDisableIdc = currSlice->DFDisableIdc;
00444 (*currMB)->DFAlphaC0Offset = currSlice->DFAlphaC0Offset;
00445 (*currMB)->DFBetaOffset = currSlice->DFBetaOffset;
00446
00447 }
00448
00456 Boolean exit_macroblock(Slice *currSlice, int eos_bit)
00457 {
00458 ImageParameters *p_Img = currSlice->p_Img;
00459
00464
00465
00466 ++(p_Img->num_dec_mb);
00467
00468 if (p_Img->num_dec_mb == p_Img->PicSizeInMbs)
00469 {
00470 return TRUE;
00471 }
00472
00473 else
00474 {
00475
00476 p_Img->current_mb_nr = FmoGetNextMBNr (p_Img, p_Img->current_mb_nr);
00477
00478 if (p_Img->current_mb_nr == -1)
00479 {
00480 assert (currSlice->nal_startcode_follows (currSlice, eos_bit) == TRUE);
00481 return TRUE;
00482 }
00483
00484 if(currSlice->nal_startcode_follows(currSlice, eos_bit) == FALSE)
00485 return FALSE;
00486
00487 if(currSlice->slice_type == I_SLICE || currSlice->slice_type == SI_SLICE || p_Img->active_pps->entropy_coding_mode_flag == CABAC)
00488 return TRUE;
00489 if(p_Img->cod_counter <= 0)
00490 return TRUE;
00491 return FALSE;
00492 }
00493 }
00494
00501 static void interpret_mb_mode_P(Macroblock *currMB)
00502 {
00503 ImageParameters *p_Img = currMB->p_Img;
00504
00505 static const int ICBPTAB[6] = {0,16,32,15,31,47};
00506 int mbmode = currMB->mb_type;
00507
00508 #define ZERO_P8x8 (mbmode==5)
00509 #define MODE_IS_P8x8 (mbmode==4 || mbmode==5)
00510 #define MODE_IS_I4x4 (mbmode==6)
00511 #define I16OFFSET (mbmode-7)
00512 #define MODE_IS_IPCM (mbmode==31)
00513
00514 if(mbmode <4)
00515 {
00516 currMB->mb_type = mbmode;
00517 memset(&currMB->b8mode[0],mbmode,4 * sizeof(char));
00518 memset(&currMB->b8pdir[0], 0, 4 * sizeof(char));
00519 }
00520 else if(MODE_IS_P8x8)
00521 {
00522 currMB->mb_type = P8x8;
00523 p_Img->allrefzero = ZERO_P8x8;
00524 }
00525 else if(MODE_IS_I4x4)
00526 {
00527 currMB->mb_type = I4MB;
00528 memset(&currMB->b8mode[0],IBLOCK, 4 * sizeof(char));
00529 memset(&currMB->b8pdir[0], -1, 4 * sizeof(char));
00530 }
00531 else if(MODE_IS_IPCM)
00532 {
00533 currMB->mb_type = IPCM;
00534 currMB->cbp = -1;
00535 currMB->i16mode = 0;
00536
00537 memset(&currMB->b8mode[0], 0, 4 * sizeof(char));
00538 memset(&currMB->b8pdir[0],-1, 4 * sizeof(char));
00539 }
00540 else
00541 {
00542 currMB->mb_type = I16MB;
00543 currMB->cbp = ICBPTAB[(I16OFFSET)>>2];
00544 currMB->i16mode = (I16OFFSET) & 0x03;
00545 memset(&currMB->b8mode[0], 0, 4 * sizeof(char));
00546 memset(&currMB->b8pdir[0],-1, 4 * sizeof(char));
00547 }
00548 }
00549
00556 static void interpret_mb_mode_I(Macroblock *currMB)
00557 {
00558 static const int ICBPTAB[6] = {0,16,32,15,31,47};
00559 int mbmode = currMB->mb_type;
00560
00561 if (mbmode==0)
00562 {
00563 currMB->mb_type = I4MB;
00564 memset(&currMB->b8mode[0],IBLOCK,4 * sizeof(char));
00565 memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
00566 }
00567 else if(mbmode==25)
00568 {
00569 currMB->mb_type=IPCM;
00570 currMB->cbp= -1;
00571 currMB->i16mode = 0;
00572
00573 memset(&currMB->b8mode[0],0,4 * sizeof(char));
00574 memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
00575 }
00576 else
00577 {
00578 currMB->mb_type = I16MB;
00579 currMB->cbp= ICBPTAB[(mbmode-1)>>2];
00580 currMB->i16mode = (mbmode-1) & 0x03;
00581 memset(&currMB->b8mode[0], 0, 4 * sizeof(char));
00582 memset(&currMB->b8pdir[0],-1, 4 * sizeof(char));
00583 }
00584 }
00585
00592 static void interpret_mb_mode_B(Macroblock *currMB)
00593 {
00594 static const int offset2pdir16x16[12] = {0, 0, 1, 2, 0,0,0,0,0,0,0,0};
00595 static const int offset2pdir16x8[22][2] = {{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{1,1},{0,0},{0,1},{0,0},{1,0},
00596 {0,0},{0,2},{0,0},{1,2},{0,0},{2,0},{0,0},{2,1},{0,0},{2,2},{0,0}};
00597 static const int offset2pdir8x16[22][2] = {{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{1,1},{0,0},{0,1},{0,0},
00598 {1,0},{0,0},{0,2},{0,0},{1,2},{0,0},{2,0},{0,0},{2,1},{0,0},{2,2}};
00599
00600 static const int ICBPTAB[6] = {0,16,32,15,31,47};
00601
00602 int i, mbmode;
00603 int mbtype = currMB->mb_type;
00604
00605
00606 if (mbtype==0)
00607 {
00608 mbmode=0;
00609 memset(&currMB->b8mode[0],0,4 * sizeof(char));
00610 memset(&currMB->b8pdir[0],2,4 * sizeof(char));
00611 }
00612 else if (mbtype==23)
00613 {
00614 mbmode=I4MB;
00615 memset(&currMB->b8mode[0],IBLOCK,4 * sizeof(char));
00616 memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
00617 }
00618 else if ((mbtype>23) && (mbtype<48) )
00619 {
00620 mbmode=I16MB;
00621 memset(&currMB->b8mode[0],0,4 * sizeof(char));
00622 memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
00623
00624 currMB->cbp = ICBPTAB[(mbtype-24)>>2];
00625 currMB->i16mode = (mbtype-24) & 0x03;
00626 }
00627 else if (mbtype==22)
00628 {
00629 mbmode=P8x8;
00630 }
00631 else if (mbtype<4)
00632 {
00633 mbmode=1;
00634 memset(&currMB->b8mode[0], 1,4 * sizeof(char));
00635 memset(&currMB->b8pdir[0],offset2pdir16x16[mbtype],4 * sizeof(char));
00636 }
00637 else if(mbtype==48)
00638 {
00639 mbmode=IPCM;
00640 memset(&currMB->b8mode[0], 0,4 * sizeof(char));
00641 memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
00642
00643 currMB->cbp= -1;
00644 currMB->i16mode = 0;
00645 }
00646
00647 else if ((mbtype&0x01)==0)
00648 {
00649 mbmode=2;
00650 memset(&currMB->b8mode[0], 2,4 * sizeof(char));
00651 for(i=0;i<4;++i)
00652 {
00653 currMB->b8pdir[i] = (char) offset2pdir16x8 [mbtype][i>>1];
00654 }
00655 }
00656 else
00657 {
00658 mbmode=3;
00659 memset(&currMB->b8mode[0], 3,4 * sizeof(char));
00660 for(i=0;i<4; ++i)
00661 {
00662 currMB->b8pdir[i] = (char) offset2pdir8x16 [mbtype][i&0x01];
00663 }
00664 }
00665 currMB->mb_type = mbmode;
00666 }
00673 static void interpret_mb_mode_SI(Macroblock *currMB)
00674 {
00675 ImageParameters *p_Img = currMB->p_Img;
00676 const int ICBPTAB[6] = {0,16,32,15,31,47};
00677 int mbmode = currMB->mb_type;
00678
00679 if (mbmode==0)
00680 {
00681 currMB->mb_type = SI4MB;
00682 memset(&currMB->b8mode[0],IBLOCK,4 * sizeof(char));
00683 memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
00684 p_Img->siblock[currMB->mb_y][currMB->mb_x]=1;
00685 }
00686 else if (mbmode==1)
00687 {
00688 currMB->mb_type = I4MB;
00689 memset(&currMB->b8mode[0],IBLOCK,4 * sizeof(char));
00690 memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
00691 }
00692 else if(mbmode==26)
00693 {
00694 currMB->mb_type=IPCM;
00695 currMB->cbp= -1;
00696 currMB->i16mode = 0;
00697 memset(&currMB->b8mode[0],0,4 * sizeof(char));
00698 memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
00699 }
00700
00701 else
00702 {
00703 currMB->mb_type = I16MB;
00704 currMB->cbp= ICBPTAB[(mbmode-1)>>2];
00705 currMB->i16mode = (mbmode-2) & 0x03;
00706 memset(&currMB->b8mode[0],0,4 * sizeof(char));
00707 memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
00708 }
00709 }
00710
00717 void setup_slice_methods(Slice *currSlice)
00718 {
00719 switch (currSlice->slice_type)
00720 {
00721 case P_SLICE: case SP_SLICE:
00722 currSlice->interpret_mb_mode = interpret_mb_mode_P;
00723 currSlice->read_motion_info_from_NAL = read_motion_info_from_NAL_p_slice;
00724 currSlice->read_one_macroblock = read_one_macroblock_p_slice;
00725 currSlice->decode_one_component = decode_one_component_p_slice;
00726 break;
00727 case B_SLICE:
00728 currSlice->interpret_mb_mode = interpret_mb_mode_B;
00729 currSlice->read_motion_info_from_NAL = read_motion_info_from_NAL_b_slice;
00730 currSlice->read_one_macroblock = read_one_macroblock_b_slice;
00731 currSlice->decode_one_component = decode_one_component_b_slice;
00732 break;
00733 case I_SLICE:
00734 currSlice->interpret_mb_mode = interpret_mb_mode_I;
00735 currSlice->read_motion_info_from_NAL = NULL;
00736 currSlice->read_one_macroblock = read_one_macroblock_i_slice;
00737 currSlice->decode_one_component = decode_one_component_i_slice;
00738 break;
00739 case SI_SLICE:
00740 currSlice->interpret_mb_mode = interpret_mb_mode_SI;
00741 currSlice->read_motion_info_from_NAL = NULL;
00742 currSlice->read_one_macroblock = read_one_macroblock_i_slice;
00743 currSlice->decode_one_component = decode_one_component_i_slice;
00744 break;
00745 default:
00746 printf("Unsupported slice type\n");
00747 break;
00748 }
00749
00750 switch(currSlice->p_Img->active_pps->entropy_coding_mode_flag)
00751 {
00752 case CABAC:
00753 currSlice->read_CBP_and_coeffs_from_NAL = read_CBP_and_coeffs_from_NAL_CABAC;
00754 break;
00755 case CAVLC:
00756 currSlice->read_CBP_and_coeffs_from_NAL = read_CBP_and_coeffs_from_NAL_CAVLC;
00757 break;
00758 default:
00759 printf("Unsupported entropy coding mode\n");
00760 break;
00761 }
00762
00763 }
00770 static void init_macroblock(Macroblock *currMB)
00771 {
00772 ImageParameters *p_Img = currMB->p_Img;
00773 StorablePicture *dec_picture = p_Img->dec_picture;
00774 int i, j, k;
00775 PicMotionParams *motion = &dec_picture->motion;
00776 for(j = currMB->block_y; j < currMB->block_y + BLOCK_SIZE; ++j)
00777 memset(&p_Img->ipredmode[j][currMB->block_x], DC_PRED, BLOCK_SIZE * sizeof(char));
00778
00779 for (k = LIST_0; k <= LIST_1; ++k)
00780 {
00781
00782 for(j = currMB->block_y; j < currMB->block_y + BLOCK_SIZE; ++j)
00783 {
00784 memset(&motion->mv[k][j][currMB->block_x][0], 0, 2 * BLOCK_SIZE * sizeof(short));
00785 }
00786
00787 for(j = currMB->block_y; j < currMB->block_y + BLOCK_SIZE; ++j)
00788 {
00789 memset(&motion->ref_idx[k][j][currMB->block_x], -1, BLOCK_SIZE * sizeof(char));
00790 }
00791
00792 for(j = currMB->block_y; j < currMB->block_y + BLOCK_SIZE; ++j)
00793 {
00794 for (i = currMB->block_x; i < currMB->block_x + BLOCK_SIZE; ++i)
00795 {
00796 motion->ref_pic_id[k][j][i] = INT64_MIN;
00797 }
00798 }
00799 }
00800 }
00801
00802
00809 void SetB8Mode (Macroblock* currMB, int value, int i)
00810 {
00811 Slice* currSlice = currMB->p_Slice;
00812 static const char p_v2b8 [ 5] = {4, 5, 6, 7, IBLOCK};
00813 static const char p_v2pd [ 5] = {0, 0, 0, 0, -1};
00814 static const char b_v2b8 [14] = {0, 4, 4, 4, 5, 6, 5, 6, 5, 6, 7, 7, 7, IBLOCK};
00815 static const char b_v2pd [14] = {2, 0, 1, 2, 0, 0, 1, 1, 2, 2, 0, 1, 2, -1};
00816
00817 if (currSlice->slice_type==B_SLICE)
00818 {
00819 currMB->b8mode[i] = b_v2b8[value];
00820 currMB->b8pdir[i] = b_v2pd[value];
00821 }
00822 else
00823 {
00824 currMB->b8mode[i] = p_v2b8[value];
00825 currMB->b8pdir[i] = p_v2pd[value];
00826 }
00827 }
00828
00829
00830 void reset_coeffs(Slice *currSlice)
00831 {
00832 ImageParameters *p_Img = currSlice->p_Img;
00833
00834
00835
00836 memset(&currSlice->cof[0][0][0], 0, 3 * MB_PIXELS * sizeof(int));
00837
00838
00839 if (p_Img->active_pps->entropy_coding_mode_flag == CAVLC)
00840 memset(&p_Img->nz_coeff[p_Img->current_mb_nr][0][0][0], 0, 3 * BLOCK_SIZE * BLOCK_SIZE * sizeof(byte));
00841 }
00842
00843 void field_flag_inference(Macroblock *currMB)
00844 {
00845 ImageParameters *p_Img = currMB->p_Img;
00846 if (currMB->mbAvailA)
00847 {
00848 currMB->mb_field = p_Img->mb_data[currMB->mbAddrA].mb_field;
00849 }
00850 else
00851 {
00852
00853 currMB->mb_field = currMB->mbAvailB ? p_Img->mb_data[currMB->mbAddrB].mb_field : FALSE;
00854 }
00855 }
00856
00857
00858 void skip_macroblock(Macroblock *currMB)
00859 {
00860 short pred_mv[2];
00861 int zeroMotionAbove;
00862 int zeroMotionLeft;
00863 PixelPos mb[4];
00864 int i, j;
00865 int a_mv_y = 0;
00866 int a_ref_idx = 0;
00867 int b_mv_y = 0;
00868 int b_ref_idx = 0;
00869 int img_block_y = currMB->block_y;
00870 ImageParameters *p_Img = currMB->p_Img;
00871 Slice *currSlice = currMB->p_Slice;
00872 int list_offset = ((currSlice->MbaffFrameFlag) && (currMB->mb_field)) ? (currMB->mbAddrX & 0x01) ? 4 : 2 : 0;
00873 StorablePicture *dec_picture = p_Img->dec_picture;
00874 PicMotionParams *motion = &dec_picture->motion;
00875 short *a_mv = NULL;
00876 short *b_mv = NULL;
00877
00878 get_neighbors(currMB, mb, 0, 0, MB_BLOCK_SIZE);
00879
00880 if (mb[0].available)
00881 {
00882 a_mv = motion->mv[LIST_0][mb[0].pos_y][mb[0].pos_x];
00883 a_mv_y = a_mv[1];
00884 a_ref_idx = motion->ref_idx[LIST_0][mb[0].pos_y][mb[0].pos_x];
00885
00886 if (currMB->mb_field && !p_Img->mb_data[mb[0].mb_addr].mb_field)
00887 {
00888 a_mv_y /=2;
00889 a_ref_idx *=2;
00890 }
00891 if (!currMB->mb_field && p_Img->mb_data[mb[0].mb_addr].mb_field)
00892 {
00893 a_mv_y *=2;
00894 a_ref_idx >>=1;
00895 }
00896 }
00897
00898 if (mb[1].available)
00899 {
00900 b_mv = motion->mv[LIST_0][mb[1].pos_y][mb[1].pos_x];
00901 b_mv_y = b_mv[1];
00902 b_ref_idx = motion->ref_idx[LIST_0][mb[1].pos_y][mb[1].pos_x];
00903
00904 if (currMB->mb_field && !p_Img->mb_data[mb[1].mb_addr].mb_field)
00905 {
00906 b_mv_y /=2;
00907 b_ref_idx *=2;
00908 }
00909 if (!currMB->mb_field && p_Img->mb_data[mb[1].mb_addr].mb_field)
00910 {
00911 b_mv_y *=2;
00912 b_ref_idx >>=1;
00913 }
00914 }
00915
00916 zeroMotionLeft = !mb[0].available ? 1 : a_ref_idx==0 && a_mv[0]==0 && a_mv_y==0 ? 1 : 0;
00917 zeroMotionAbove = !mb[1].available ? 1 : b_ref_idx==0 && b_mv[0]==0 && b_mv_y==0 ? 1 : 0;
00918
00919 currMB->cbp = 0;
00920 reset_coeffs(currSlice);
00921
00922 if (zeroMotionAbove || zeroMotionLeft)
00923 {
00924 for(j = img_block_y; j < img_block_y + BLOCK_SIZE; ++j)
00925 {
00926 memset(&motion->mv[LIST_0][j][currMB->block_x][0], 0, 2 * BLOCK_SIZE * sizeof(short));
00927 }
00928 }
00929 else
00930 {
00931 currMB->GetMVPredictor (currMB, mb, pred_mv, 0, motion->ref_idx[LIST_0], motion->mv[LIST_0], 0, 0, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
00932
00933
00934 for(i=currMB->block_x;i<currMB->block_x + BLOCK_SIZE; ++i)
00935 memcpy(&motion->mv[LIST_0][img_block_y][i][0], &pred_mv[0], 2 * sizeof(short));
00936
00937
00938 for(j=img_block_y; j < img_block_y + BLOCK_SIZE - 1; ++j)
00939 {
00940 memcpy(&motion->mv[LIST_0][j + 1][currMB->block_x][0], &motion->mv[LIST_0][j][currMB->block_x][0], 2 * BLOCK_SIZE * sizeof(short));
00941 }
00942 }
00943
00944
00945 for(j=img_block_y;j< img_block_y + BLOCK_SIZE;++j)
00946 {
00947 memset(&motion->ref_idx[LIST_0][j][currMB->block_x], 0, BLOCK_SIZE * sizeof(char));
00948 }
00949
00950
00951
00952 for(i=currMB->block_x;i<currMB->block_x + BLOCK_SIZE;++i)
00953 {
00954 motion->ref_pic_id[LIST_0][img_block_y][i] =
00955 dec_picture->ref_pic_num[p_Img->current_slice_nr][LIST_0 + list_offset][0];
00956 }
00957
00958 for(j=img_block_y;j< img_block_y + BLOCK_SIZE - 1;++j)
00959 {
00960 memcpy(&motion->ref_pic_id[LIST_0][j + 1][currMB->block_x], &motion->ref_pic_id[LIST_0][j][currMB->block_x], BLOCK_SIZE * sizeof(int64));
00961 }
00962 }
00963
00964 static void concealIPCMcoeffs(Macroblock *currMB)
00965 {
00966 Slice *currSlice = currMB->p_Slice;
00967 ImageParameters *p_Img = currMB->p_Img;
00968 StorablePicture *dec_picture = p_Img->dec_picture;
00969 int i, j, k;
00970
00971 for(i=0;i<MB_BLOCK_SIZE;++i)
00972 {
00973 for(j=0;j<MB_BLOCK_SIZE;++j)
00974 {
00975 currSlice->cof[0][i][j] = p_Img->dc_pred_value_comp[0];
00976
00977 }
00978 }
00979
00980 if ((dec_picture->chroma_format_idc != YUV400) && !IS_INDEPENDENT(p_Img))
00981 {
00982 for (k = 0; k < 2; ++k)
00983 {
00984 for(i=0;i<p_Img->mb_cr_size_y;++i)
00985 {
00986 for(j=0;j<p_Img->mb_cr_size_x;++j)
00987 {
00988 currSlice->cof[k][i][j] = p_Img->dc_pred_value_comp[k];
00989
00990 }
00991 }
00992 }
00993 }
00994 }
00995
01002 static void read_one_macroblock_i_slice(Macroblock *currMB)
01003 {
01004 Slice *currSlice = currMB->p_Slice;
01005 ImageParameters *p_Img = currMB->p_Img;
01006
01007 SyntaxElement currSE;
01008 int mb_nr = currMB->mbAddrX;
01009
01010 DataPartition *dP;
01011 const byte *partMap = assignSE2partition[currSlice->dp_mode];
01012 StorablePicture *dec_picture = p_Img->dec_picture;
01013 PicMotionParams *motion = &dec_picture->motion;
01014
01015 currMB->mb_field = ((mb_nr&0x01) == 0)? FALSE : p_Img->mb_data[mb_nr-1].mb_field;
01016
01017 update_qp(currMB, p_Img->qp);
01018 currSE.type = SE_MBTYPE;
01019
01020
01021 dP = &(currSlice->partArr[partMap[currSE.type]]);
01022
01023 if (p_Img->active_pps->entropy_coding_mode_flag == CAVLC || dP->bitstream->ei_flag)
01024 currSE.mapping = linfo_ue;
01025
01026
01027 if (currSlice->MbaffFrameFlag && (mb_nr&0x01)==0)
01028 {
01029 TRACE_STRING("mb_field_decoding_flag");
01030 if (p_Img->active_pps->entropy_coding_mode_flag == CAVLC || dP->bitstream->ei_flag)
01031 {
01032 currSE.len = (int64) 1;
01033 readSyntaxElement_FLC(&currSE, dP->bitstream);
01034 }
01035 else
01036 {
01037 currSE.reading = readFieldModeInfo_CABAC;
01038 dP->readSyntaxElement(&currSE,p_Img,dP);
01039 }
01040 currMB->mb_field = (Boolean) currSE.value1;
01041 }
01042
01043 if(p_Img->active_pps->entropy_coding_mode_flag == CABAC)
01044 CheckAvailabilityOfNeighborsCABAC(currMB);
01045
01046
01047 TRACE_STRING("mb_type");
01048 currSE.reading = readMB_typeInfo_CABAC;
01049 dP->readSyntaxElement(&currSE,p_Img,dP);
01050
01051 currMB->mb_type = currSE.value1;
01052 if(!dP->bitstream->ei_flag)
01053 currMB->ei_flag = 0;
01054
01055 motion->mb_field[mb_nr] = (byte) currMB->mb_field;
01056
01057 currMB->block_y_aff = ((currSlice->MbaffFrameFlag) && (currMB->mb_field)) ? (mb_nr&0x01) ? (currMB->block_y - 4)>>1 : currMB->block_y >> 1 : currMB->block_y;
01058
01059 p_Img->siblock[currMB->mb_y][currMB->mb_x] = 0;
01060
01061 currSlice->interpret_mb_mode(currMB);
01062
01063
01064 currMB->NoMbPartLessThan8x8Flag = TRUE;
01065
01066
01067
01068
01069 if (currMB->mb_type == I4MB && p_Img->Transform8x8Mode)
01070 {
01071 currSE.type = SE_HEADER;
01072 dP = &(currSlice->partArr[partMap[SE_HEADER]]);
01073 currSE.reading = readMB_transform_size_flag_CABAC;
01074 TRACE_STRING("transform_size_8x8_flag");
01075
01076
01077 if (p_Img->active_pps->entropy_coding_mode_flag == CAVLC || dP->bitstream->ei_flag)
01078 {
01079 currSE.len = (int64) 1;
01080 readSyntaxElement_FLC(&currSE, dP->bitstream);
01081 }
01082 else
01083 {
01084 dP->readSyntaxElement(&currSE,p_Img,dP);
01085 }
01086
01087 currMB->luma_transform_size_8x8_flag = (Boolean) currSE.value1;
01088
01089 if (currMB->luma_transform_size_8x8_flag)
01090 {
01091 currMB->mb_type = I8MB;
01092 memset(&currMB->b8mode, I8MB, 4 * sizeof(char));
01093 memset(&currMB->b8pdir, -1, 4 * sizeof(char));
01094 }
01095 }
01096 else
01097 {
01098 currMB->luma_transform_size_8x8_flag = FALSE;
01099 }
01100
01107
01108
01109
01110
01111
01112
01113
01114
01115
01116
01118
01119
01120
01121 init_macroblock(currMB);
01122
01123 if(currMB->mb_type != IPCM)
01124 {
01125
01126 read_ipred_modes(currMB);
01127
01128
01129 currSlice->read_CBP_and_coeffs_from_NAL (currMB);
01130 }
01131 else
01132 {
01133
01134
01135
01136
01137 if ( currSlice->dp_mode && currSlice->dpB_NotPresent )
01138 {
01139 concealIPCMcoeffs(currMB);
01140 }
01141 else
01142 {
01143 dP = &(currSlice->partArr[partMap[SE_LUM_DC_INTRA]]);
01144 read_IPCM_coeffs_from_NAL(currSlice, dP);
01145 }
01146 }
01147
01148 return;
01149 }
01150
01157 static void read_one_macroblock_p_slice(Macroblock *currMB)
01158 {
01159 Slice *currSlice = currMB->p_Slice;
01160 ImageParameters *p_Img = currMB->p_Img;
01161
01162 int i;
01163
01164 SyntaxElement currSE;
01165 int mb_nr = currMB->mbAddrX;
01166
01167 DataPartition *dP;
01168 const byte *partMap = assignSE2partition[currSlice->dp_mode];
01169 Macroblock *topMB = NULL;
01170 int prevMbSkipped = 0;
01171 int check_bottom, read_bottom, read_top;
01172 StorablePicture *dec_picture = p_Img->dec_picture;
01173 PicMotionParams *motion = &dec_picture->motion;
01174
01175 if (currSlice->MbaffFrameFlag)
01176 {
01177 if (mb_nr&0x01)
01178 {
01179 topMB= &p_Img->mb_data[mb_nr-1];
01180 if(!(currSlice->slice_type == B_SLICE))
01181 prevMbSkipped = (topMB->mb_type == 0);
01182 else
01183 prevMbSkipped = topMB->skip_flag;
01184 }
01185 else
01186 prevMbSkipped = 0;
01187 }
01188
01189 currMB->mb_field = ((mb_nr&0x01) == 0)? FALSE : p_Img->mb_data[mb_nr-1].mb_field;
01190
01191 update_qp(currMB, p_Img->qp);
01192 currSE.type = SE_MBTYPE;
01193
01194
01195 dP = &(currSlice->partArr[partMap[currSE.type]]);
01196
01197 if (p_Img->active_pps->entropy_coding_mode_flag == CAVLC || dP->bitstream->ei_flag)
01198 currSE.mapping = linfo_ue;
01199
01200 if (p_Img->active_pps->entropy_coding_mode_flag == CABAC)
01201 {
01202
01203 if (currSlice->MbaffFrameFlag && ((mb_nr&0x01) == 0||prevMbSkipped))
01204 field_flag_inference(currMB);
01205
01206 CheckAvailabilityOfNeighborsCABAC(currMB);
01207 TRACE_STRING("mb_skip_flag");
01208 currSE.reading = readMB_skip_flagInfo_CABAC;
01209 dP->readSyntaxElement(&currSE,p_Img,dP);
01210
01211 currMB->mb_type = currSE.value1;
01212 currMB->skip_flag = !(currSE.value1);
01213
01214 if (!dP->bitstream->ei_flag)
01215 currMB->ei_flag = 0;
01216
01217
01218 if (currSlice->MbaffFrameFlag)
01219 {
01220 check_bottom=read_bottom=read_top=0;
01221 if ((mb_nr&0x01)==0)
01222 {
01223 check_bottom = currMB->skip_flag;
01224 read_top = !check_bottom;
01225 }
01226 else
01227 {
01228 read_bottom = (topMB->skip_flag && (!currMB->skip_flag));
01229 }
01230
01231 if (read_bottom || read_top)
01232 {
01233 TRACE_STRING("mb_field_decoding_flag");
01234 currSE.reading = readFieldModeInfo_CABAC;
01235 dP->readSyntaxElement(&currSE,p_Img,dP);
01236 currMB->mb_field = (Boolean) currSE.value1;
01237 }
01238 if (check_bottom)
01239 check_next_mb_and_get_field_mode_CABAC(currSlice, &currSE, dP);
01240 }
01241
01242 CheckAvailabilityOfNeighborsCABAC(currMB);
01243
01244
01245 if (currMB->mb_type != 0 )
01246 {
01247 currSE.reading = readMB_typeInfo_CABAC;
01248 TRACE_STRING("mb_type");
01249 dP->readSyntaxElement(&currSE,p_Img,dP);
01250 currMB->mb_type = currSE.value1;
01251 if(!dP->bitstream->ei_flag)
01252 currMB->ei_flag = 0;
01253 }
01254 }
01255
01256 else
01257 {
01258 if(p_Img->cod_counter == -1)
01259 {
01260 TRACE_STRING("mb_skip_run");
01261 dP->readSyntaxElement(&currSE,p_Img,dP);
01262 p_Img->cod_counter = currSE.value1;
01263 }
01264 if (p_Img->cod_counter==0)
01265 {
01266
01267 if ((currSlice->MbaffFrameFlag) && (((mb_nr&0x01)==0) || ((mb_nr&0x01) && prevMbSkipped)))
01268 {
01269 TRACE_STRING("mb_field_decoding_flag");
01270 currSE.len = (int64) 1;
01271 readSyntaxElement_FLC(&currSE, dP->bitstream);
01272 currMB->mb_field = (Boolean) currSE.value1;
01273 }
01274
01275
01276 TRACE_STRING("mb_type");
01277 dP->readSyntaxElement(&currSE,p_Img,dP);
01278 if(currSlice->slice_type == P_SLICE || currSlice->slice_type == SP_SLICE)
01279 ++(currSE.value1);
01280 currMB->mb_type = currSE.value1;
01281 if(!dP->bitstream->ei_flag)
01282 currMB->ei_flag = 0;
01283 p_Img->cod_counter--;
01284 currMB->skip_flag = 0;
01285 }
01286 else
01287 {
01288 p_Img->cod_counter--;
01289 currMB->mb_type = 0;
01290 currMB->ei_flag = 0;
01291 currMB->skip_flag = 1;
01292
01293
01294 if(currSlice->MbaffFrameFlag)
01295 {
01296 if(p_Img->cod_counter == 0 && ((mb_nr&0x01) == 0))
01297 {
01298 TRACE_STRING("mb_field_decoding_flag (of coded bottom mb)");
01299 currSE.len = (int64) 1;
01300 readSyntaxElement_FLC(&currSE, dP->bitstream);
01301 dP->bitstream->frame_bitoffset--;
01302 TRACE_DECBITS(1);
01303 currMB->mb_field = (Boolean) currSE.value1;
01304 }
01305 else if (p_Img->cod_counter > 0 && ((mb_nr & 0x01) == 0))
01306 {
01307
01308 if (mb_is_available(mb_nr - 2, currMB) && ((mb_nr % (p_Img->PicWidthInMbs * 2))!=0))
01309 {
01310 currMB->mb_field = p_Img->mb_data[mb_nr-2].mb_field;
01311 }
01312 else
01313 {
01314
01315 if (mb_is_available(mb_nr - 2*p_Img->PicWidthInMbs, currMB))
01316 {
01317 currMB->mb_field = p_Img->mb_data[mb_nr-2*p_Img->PicWidthInMbs].mb_field;
01318 }
01319 else
01320 currMB->mb_field = FALSE;
01321 }
01322 }
01323 }
01324 }
01325 }
01326
01327 motion->mb_field[mb_nr] = (byte) currMB->mb_field;
01328
01329 currMB->block_y_aff = ((currSlice->MbaffFrameFlag) && (currMB->mb_field)) ? (mb_nr&0x01) ? (currMB->block_y - 4)>>1 : currMB->block_y >> 1 : currMB->block_y;
01330
01331 p_Img->siblock[currMB->mb_y][currMB->mb_x] = 0;
01332
01333 currSlice->interpret_mb_mode(currMB);
01334
01335 if(currSlice->MbaffFrameFlag)
01336 {
01337 if(currMB->mb_field)
01338 {
01339 currSlice->num_ref_idx_l0_active <<=1;
01340 currSlice->num_ref_idx_l1_active <<=1;
01341 }
01342 }
01343
01344
01345 currMB->NoMbPartLessThan8x8Flag = (IS_DIRECT(currMB) && !(p_Img->active_sps->direct_8x8_inference_flag))? FALSE: TRUE;
01346
01347
01348 if (currMB->mb_type == P8x8)
01349 {
01350 currSE.type = SE_MBTYPE;
01351 dP = &(currSlice->partArr[partMap[SE_MBTYPE]]);
01352
01353 if (p_Img->active_pps->entropy_coding_mode_flag ==CAVLC || dP->bitstream->ei_flag)
01354 currSE.mapping = linfo_ue;
01355 else
01356 currSE.reading = readB8_typeInfo_CABAC;
01357
01358 for (i = 0; i < 4; ++i)
01359 {
01360 TRACE_STRING("sub_mb_type");
01361 dP->readSyntaxElement (&currSE, p_Img, dP);
01362 SetB8Mode (currMB, currSE.value1, i);
01363
01364
01365 currMB->NoMbPartLessThan8x8Flag &= (currMB->b8mode[i]==0 && p_Img->active_sps->direct_8x8_inference_flag) ||
01366 (currMB->b8mode[i]==4);
01367 }
01368
01369 init_macroblock (currMB);
01370 currSlice->read_motion_info_from_NAL (currMB);
01371 }
01372
01373
01374
01375
01376 if (currMB->mb_type == I4MB && p_Img->Transform8x8Mode)
01377 {
01378 currSE.type = SE_HEADER;
01379 dP = &(currSlice->partArr[partMap[SE_HEADER]]);
01380 currSE.reading = readMB_transform_size_flag_CABAC;
01381 TRACE_STRING("transform_size_8x8_flag");
01382
01383
01384 if (p_Img->active_pps->entropy_coding_mode_flag == CAVLC || dP->bitstream->ei_flag)
01385 {
01386 currSE.len = (int64) 1;
01387 readSyntaxElement_FLC(&currSE, dP->bitstream);
01388 }
01389 else
01390 {
01391 dP->readSyntaxElement(&currSE,p_Img,dP);
01392 }
01393
01394 currMB->luma_transform_size_8x8_flag = (Boolean) currSE.value1;
01395
01396 if (currMB->luma_transform_size_8x8_flag)
01397 {
01398 currMB->mb_type = I8MB;
01399 memset(&currMB->b8mode, I8MB, 4 * sizeof(char));
01400 memset(&currMB->b8pdir, -1, 4 * sizeof(char));
01401 }
01402 }
01403 else
01404 {
01405 currMB->luma_transform_size_8x8_flag = FALSE;
01406 }
01407
01408 if(p_Img->active_pps->constrained_intra_pred_flag)
01409 {
01410 if( !IS_INTRA(currMB) )
01411 {
01412 p_Img->intra_block[mb_nr] = 0;
01413 }
01414 }
01415
01422
01423
01424
01425
01426
01427
01428
01429
01430
01431
01433
01434
01435
01436 if (currMB->mb_type != P8x8)
01437 init_macroblock(currMB);
01438
01439 if (IS_SKIP (currMB))
01440 {
01441 skip_macroblock(currMB);
01442 }
01443 else if(currMB->mb_type != IPCM)
01444 {
01445
01446 if (IS_INTRA(currMB))
01447 read_ipred_modes(currMB);
01448
01449
01450 if (IS_INTERMV (currMB) && (currMB->mb_type != P8x8))
01451 {
01452 currSlice->read_motion_info_from_NAL (currMB);
01453 }
01454
01455 currSlice->read_CBP_and_coeffs_from_NAL (currMB);
01456 }
01457 else
01458 {
01459
01460
01461
01462
01463 if ( currSlice->dp_mode && currSlice->dpB_NotPresent )
01464 {
01465 concealIPCMcoeffs(currMB);
01466 }
01467 else
01468 {
01469 dP = &(currSlice->partArr[partMap[SE_LUM_DC_INTRA]]);
01470 read_IPCM_coeffs_from_NAL(currSlice, dP);
01471 }
01472 }
01473
01474 return;
01475 }
01476
01483 static void read_one_macroblock_b_slice(Macroblock *currMB)
01484 {
01485 Slice *currSlice = currMB->p_Slice;
01486 ImageParameters *p_Img = currMB->p_Img;
01487 int i;
01488
01489 SyntaxElement currSE;
01490 int mb_nr = currMB->mbAddrX;
01491
01492 DataPartition *dP;
01493 const byte *partMap = assignSE2partition[currSlice->dp_mode];
01494 Macroblock *topMB = NULL;
01495 int prevMbSkipped = 0;
01496 int check_bottom, read_bottom, read_top;
01497 StorablePicture *dec_picture = p_Img->dec_picture;
01498 PicMotionParams *motion = &dec_picture->motion;
01499
01500 if (currSlice->MbaffFrameFlag)
01501 {
01502 if (mb_nr&0x01)
01503 {
01504 topMB= &p_Img->mb_data[mb_nr-1];
01505 if(!(currSlice->slice_type == B_SLICE))
01506 prevMbSkipped = (topMB->mb_type == 0);
01507 else
01508 prevMbSkipped = topMB->skip_flag;
01509 }
01510 else
01511 prevMbSkipped = 0;
01512 }
01513
01514 currMB->mb_field = ((mb_nr&0x01) == 0)? FALSE : p_Img->mb_data[mb_nr-1].mb_field;
01515
01516 update_qp(currMB, p_Img->qp);
01517 currSE.type = SE_MBTYPE;
01518
01519
01520 dP = &(currSlice->partArr[partMap[currSE.type]]);
01521
01522 if (p_Img->active_pps->entropy_coding_mode_flag == CAVLC || dP->bitstream->ei_flag)
01523 currSE.mapping = linfo_ue;
01524
01525 if (p_Img->active_pps->entropy_coding_mode_flag == CABAC)
01526 {
01527
01528 if (currSlice->MbaffFrameFlag && ((mb_nr&0x01) == 0||prevMbSkipped))
01529 field_flag_inference(currMB);
01530
01531 CheckAvailabilityOfNeighborsCABAC(currMB);
01532 TRACE_STRING("mb_skip_flag");
01533 currSE.reading = readMB_skip_flagInfo_CABAC;
01534 dP->readSyntaxElement(&currSE,p_Img,dP);
01535
01536 currMB->mb_type = currSE.value1;
01537 currMB->skip_flag = !(currSE.value1);
01538
01539 currMB->cbp = currSE.value2;
01540
01541 if (!dP->bitstream->ei_flag)
01542 currMB->ei_flag = 0;
01543
01544 if (currSE.value1 == 0 && currSE.value2 == 0)
01545 p_Img->cod_counter=0;
01546
01547
01548 if (currSlice->MbaffFrameFlag)
01549 {
01550 check_bottom=read_bottom=read_top=0;
01551 if ((mb_nr&0x01)==0)
01552 {
01553 check_bottom = currMB->skip_flag;
01554 read_top = !check_bottom;
01555 }
01556 else
01557 {
01558 read_bottom = (topMB->skip_flag && (!currMB->skip_flag));
01559 }
01560
01561 if (read_bottom || read_top)
01562 {
01563 TRACE_STRING("mb_field_decoding_flag");
01564 currSE.reading = readFieldModeInfo_CABAC;
01565 dP->readSyntaxElement(&currSE,p_Img,dP);
01566 currMB->mb_field = (Boolean) currSE.value1;
01567 }
01568 if (check_bottom)
01569 check_next_mb_and_get_field_mode_CABAC(currSlice, &currSE, dP);
01570 }
01571
01572 CheckAvailabilityOfNeighborsCABAC(currMB);
01573
01574
01575 if (currMB->mb_type != 0 )
01576 {
01577 currSE.reading = readMB_typeInfo_CABAC;
01578 TRACE_STRING("mb_type");
01579 dP->readSyntaxElement(&currSE,p_Img,dP);
01580 currMB->mb_type = currSE.value1;
01581 if(!dP->bitstream->ei_flag)
01582 currMB->ei_flag = 0;
01583 }
01584 }
01585
01586 else
01587 {
01588 if(p_Img->cod_counter == -1)
01589 {
01590 TRACE_STRING("mb_skip_run");
01591 dP->readSyntaxElement(&currSE,p_Img,dP);
01592 p_Img->cod_counter = currSE.value1;
01593 }
01594 if (p_Img->cod_counter==0)
01595 {
01596
01597 if ((currSlice->MbaffFrameFlag) && (((mb_nr&0x01)==0) || ((mb_nr&0x01) && prevMbSkipped)))
01598 {
01599 TRACE_STRING("mb_field_decoding_flag");
01600 currSE.len = (int64) 1;
01601 readSyntaxElement_FLC(&currSE, dP->bitstream);
01602 currMB->mb_field = (Boolean) currSE.value1;
01603 }
01604
01605
01606 TRACE_STRING("mb_type");
01607 dP->readSyntaxElement(&currSE,p_Img,dP);
01608 if(currSlice->slice_type == P_SLICE || currSlice->slice_type == SP_SLICE)
01609 ++(currSE.value1);
01610 currMB->mb_type = currSE.value1;
01611 if(!dP->bitstream->ei_flag)
01612 currMB->ei_flag = 0;
01613 p_Img->cod_counter--;
01614 currMB->skip_flag = 0;
01615 }
01616 else
01617 {
01618 p_Img->cod_counter--;
01619 currMB->mb_type = 0;
01620 currMB->ei_flag = 0;
01621 currMB->skip_flag = 1;
01622
01623
01624 if(currSlice->MbaffFrameFlag)
01625 {
01626 if(p_Img->cod_counter == 0 && ((mb_nr&0x01) == 0))
01627 {
01628 TRACE_STRING("mb_field_decoding_flag (of coded bottom mb)");
01629 currSE.len = (int64) 1;
01630 readSyntaxElement_FLC(&currSE, dP->bitstream);
01631 dP->bitstream->frame_bitoffset--;
01632 TRACE_DECBITS(1);
01633 currMB->mb_field = (Boolean) currSE.value1;
01634 }
01635 else if (p_Img->cod_counter > 0 && ((mb_nr & 0x01) == 0))
01636 {
01637
01638 if (mb_is_available(mb_nr - 2, currMB) && ((mb_nr % (p_Img->PicWidthInMbs * 2))!=0))
01639 {
01640 currMB->mb_field = p_Img->mb_data[mb_nr-2].mb_field;
01641 }
01642 else
01643 {
01644
01645 if (mb_is_available(mb_nr - 2*p_Img->PicWidthInMbs, currMB))
01646 {
01647 currMB->mb_field = p_Img->mb_data[mb_nr-2*p_Img->PicWidthInMbs].mb_field;
01648 }
01649 else
01650 currMB->mb_field = FALSE;
01651 }
01652 }
01653 }
01654 }
01655 }
01656
01657 motion->mb_field[mb_nr] = (byte) currMB->mb_field;
01658
01659 currMB->block_y_aff = ((currSlice->MbaffFrameFlag) && (currMB->mb_field)) ? (mb_nr&0x01) ? (currMB->block_y - 4)>>1 : currMB->block_y >> 1 : currMB->block_y;
01660
01661 p_Img->siblock[currMB->mb_y][currMB->mb_x] = 0;
01662
01663 currSlice->interpret_mb_mode(currMB);
01664
01665 if(currSlice->MbaffFrameFlag)
01666 {
01667 if(currMB->mb_field)
01668 {
01669 currSlice->num_ref_idx_l0_active <<=1;
01670 currSlice->num_ref_idx_l1_active <<=1;
01671 }
01672 }
01673
01674
01675 currMB->NoMbPartLessThan8x8Flag = (IS_DIRECT(currMB) && !(p_Img->active_sps->direct_8x8_inference_flag))? FALSE: TRUE;
01676
01677
01678 if (currMB->mb_type == P8x8)
01679 {
01680 currSE.type = SE_MBTYPE;
01681 dP = &(currSlice->partArr[partMap[SE_MBTYPE]]);
01682
01683 if (p_Img->active_pps->entropy_coding_mode_flag ==CAVLC || dP->bitstream->ei_flag)
01684 currSE.mapping = linfo_ue;
01685 else
01686 currSE.reading = readB8_typeInfo_CABAC;
01687
01688 for (i = 0; i < 4; ++i)
01689 {
01690 TRACE_STRING("sub_mb_type");
01691 dP->readSyntaxElement (&currSE, p_Img, dP);
01692 SetB8Mode (currMB, currSE.value1, i);
01693
01694
01695 currMB->NoMbPartLessThan8x8Flag &= (currMB->b8mode[i]==0 && p_Img->active_sps->direct_8x8_inference_flag) ||
01696 (currMB->b8mode[i]==4);
01697 }
01698
01699 init_macroblock (currMB);
01700 currSlice->read_motion_info_from_NAL (currMB);
01701 }
01702
01703
01704
01705
01706 if (currMB->mb_type == I4MB && p_Img->Transform8x8Mode)
01707 {
01708 currSE.type = SE_HEADER;
01709 dP = &(currSlice->partArr[partMap[SE_HEADER]]);
01710 currSE.reading = readMB_transform_size_flag_CABAC;
01711 TRACE_STRING("transform_size_8x8_flag");
01712
01713
01714 if (p_Img->active_pps->entropy_coding_mode_flag == CAVLC || dP->bitstream->ei_flag)
01715 {
01716 currSE.len = (int64) 1;
01717 readSyntaxElement_FLC(&currSE, dP->bitstream);
01718 }
01719 else
01720 {
01721 dP->readSyntaxElement(&currSE,p_Img,dP);
01722 }
01723
01724 currMB->luma_transform_size_8x8_flag = (Boolean) currSE.value1;
01725
01726 if (currMB->luma_transform_size_8x8_flag)
01727 {
01728 currMB->mb_type = I8MB;
01729 memset(&currMB->b8mode, I8MB, 4 * sizeof(char));
01730 memset(&currMB->b8pdir, -1, 4 * sizeof(char));
01731 }
01732 }
01733 else
01734 {
01735 currMB->luma_transform_size_8x8_flag = FALSE;
01736 }
01737
01738 if(p_Img->active_pps->constrained_intra_pred_flag)
01739 {
01740 if( !IS_INTRA(currMB) )
01741 {
01742 p_Img->intra_block[mb_nr] = 0;
01743 }
01744 }
01745
01752
01753
01754
01755
01756
01757
01758
01759
01760
01761
01763
01764
01765
01766 if (currMB->mb_type != P8x8)
01767 init_macroblock(currMB);
01768
01769 if (IS_DIRECT (currMB) && p_Img->cod_counter >= 0)
01770 {
01771 currMB->cbp = 0;
01772 reset_coeffs(currSlice);
01773
01774 if (p_Img->active_pps->entropy_coding_mode_flag ==CABAC)
01775 p_Img->cod_counter=-1;
01776 }
01777 else if (IS_SKIP (currMB))
01778 {
01779 skip_macroblock(currMB);
01780 }
01781 else if(currMB->mb_type != IPCM)
01782 {
01783
01784 if (IS_INTRA(currMB))
01785 read_ipred_modes(currMB);
01786
01787
01788 if (IS_INTERMV (currMB) && (currMB->mb_type != P8x8))
01789 {
01790 currSlice->read_motion_info_from_NAL (currMB);
01791 }
01792
01793 currSlice->read_CBP_and_coeffs_from_NAL (currMB);
01794 }
01795 else
01796 {
01797
01798
01799
01800
01801 if ( currSlice->dp_mode && currSlice->dpB_NotPresent )
01802 {
01803 concealIPCMcoeffs(currMB);
01804 }
01805 else
01806 {
01807 dP = &(currSlice->partArr[partMap[SE_LUM_DC_INTRA]]);
01808 read_IPCM_coeffs_from_NAL(currSlice, dP);
01809 }
01810 }
01811
01812 return;
01813 }
01814
01815
01826 static void init_decoding_engine_IPCM(Slice *currSlice)
01827 {
01828 Bitstream *currStream;
01829 int ByteStartPosition;
01830 int PartitionNumber;
01831 int i;
01832
01833 if(currSlice->dp_mode==PAR_DP_1)
01834 PartitionNumber=1;
01835 else if(currSlice->dp_mode==PAR_DP_3)
01836 PartitionNumber=3;
01837 else
01838 {
01839 printf("Partition Mode is not supported\n");
01840 exit(1);
01841 }
01842
01843 for(i=0;i<PartitionNumber;++i)
01844 {
01845 currStream = currSlice->partArr[i].bitstream;
01846 ByteStartPosition = currStream->read_len;
01847
01848 arideco_start_decoding (&currSlice->partArr[i].de_cabac, currStream->streamBuffer, ByteStartPosition, &currStream->read_len);
01849 }
01850 }
01851
01852
01853
01854
01866 static void read_IPCM_coeffs_from_NAL(Slice *currSlice, struct datapartition *dP)
01867 {
01868 ImageParameters *p_Img = currSlice->p_Img;
01869
01870 StorablePicture *dec_picture = p_Img->dec_picture;
01871 SyntaxElement currSE;
01872 int i,j;
01873
01874
01875
01876 if(p_Img->active_pps->entropy_coding_mode_flag == CABAC)
01877 {
01878 readIPCM_CABAC(currSlice, dP);
01879 init_decoding_engine_IPCM(currSlice);
01880 }
01881 else
01882 {
01883
01884
01885 if(((dP->bitstream->frame_bitoffset) & 0x07) != 0)
01886 {
01887 TRACE_STRING("pcm_alignment_zero_bit");
01888 currSE.len = (8 - ((dP->bitstream->frame_bitoffset) & 0x07));
01889 readSyntaxElement_FLC(&currSE, dP->bitstream);
01890 }
01891
01892
01893 currSE.len=p_Img->bitdepth_luma;
01894 TRACE_STRING("pcm_sample_luma");
01895
01896 for(i=0;i<MB_BLOCK_SIZE;++i)
01897 {
01898 for(j=0;j<MB_BLOCK_SIZE;++j)
01899 {
01900 readSyntaxElement_FLC(&currSE, dP->bitstream);
01901 currSlice->cof[0][i][j] = currSE.value1;
01902
01903 }
01904 }
01905 currSE.len=p_Img->bitdepth_chroma;
01906 if ((dec_picture->chroma_format_idc != YUV400) && !IS_INDEPENDENT(p_Img))
01907 {
01908 TRACE_STRING("pcm_sample_chroma (u)");
01909 for(i=0;i<p_Img->mb_cr_size_y;++i)
01910 {
01911 for(j=0;j<p_Img->mb_cr_size_x;++j)
01912 {
01913 readSyntaxElement_FLC(&currSE, dP->bitstream);
01914 currSlice->cof[1][i][j] = currSE.value1;
01915
01916 }
01917 }
01918 TRACE_STRING("pcm_sample_chroma (v)");
01919 for(i=0;i<p_Img->mb_cr_size_y;++i)
01920 {
01921 for(j=0;j<p_Img->mb_cr_size_x;++j)
01922 {
01923 readSyntaxElement_FLC(&currSE, dP->bitstream);
01924 currSlice->cof[2][i][j] = currSE.value1;
01925
01926 }
01927 }
01928 }
01929 }
01930 }
01931
01932
01942 static void read_ipred_modes(Macroblock *currMB)
01943 {
01944 int b8,i,j,bi,bj,bx,by,dec;
01945 SyntaxElement currSE;
01946 DataPartition *dP;
01947 Slice *currSlice = currMB->p_Slice;
01948 const byte *partMap = assignSE2partition[currSlice->dp_mode];
01949 ImageParameters *p_Img = currMB->p_Img;
01950
01951 StorablePicture *dec_picture = p_Img->dec_picture;
01952 int ts, ls;
01953 int mostProbableIntraPredMode;
01954 int upIntraPredMode;
01955 int leftIntraPredMode;
01956 char IntraChromaPredModeFlag = IS_INTRA(currMB);
01957 int bs_x, bs_y;
01958 int ii,jj;
01959
01960 PixelPos left_block, top_block;
01961
01962 currSE.type = SE_INTRAPREDMODE;
01963
01964 TRACE_STRING("intra4x4_pred_mode");
01965 dP = &(currSlice->partArr[partMap[currSE.type]]);
01966
01967 if (!(p_Img->active_pps->entropy_coding_mode_flag == CAVLC || dP->bitstream->ei_flag))
01968 currSE.reading = readIntraPredMode_CABAC;
01969
01970 for(b8 = 0; b8 < 4; ++b8)
01971 {
01972 if((currMB->b8mode[b8]==IBLOCK )||(currMB->b8mode[b8]==I8MB))
01973 {
01974 bs_x = bs_y = (currMB->b8mode[b8] == I8MB)?8:4;
01975
01976 IntraChromaPredModeFlag = 1;
01977
01978 ii = (bs_x>>2);
01979 jj = (bs_y>>2);
01980
01981 for(j=0;j<2;j+=jj)
01982 {
01983 by = (b8&2) + j;
01984 bj = currMB->block_y + by;
01985 for(i=0;i<2;i+=ii)
01986 {
01987 bx = ((b8&1)<<1) + i;
01988 bi = currMB->block_x + bx;
01989
01990 if (p_Img->active_pps->entropy_coding_mode_flag == CAVLC || dP->bitstream->ei_flag)
01991 readSyntaxElement_Intra4x4PredictionMode(&currSE,p_Img,dP->bitstream);
01992 else
01993 {
01994 currSE.context=(b8<<2)+(j<<1) +i;
01995 dP->readSyntaxElement(&currSE,p_Img,dP);
01996 }
01997
01998 get4x4Neighbour(currMB, (bx<<2) - 1, (by<<2), p_Img->mb_size[IS_LUMA], &left_block);
01999 get4x4Neighbour(currMB, (bx<<2), (by<<2) - 1, p_Img->mb_size[IS_LUMA], &top_block );
02000
02001
02002
02003 if (p_Img->active_pps->constrained_intra_pred_flag)
02004 {
02005 left_block.available = left_block.available ? p_Img->intra_block[left_block.mb_addr] : 0;
02006 top_block.available = top_block.available ? p_Img->intra_block[top_block.mb_addr] : 0;
02007 }
02008
02009
02010 ts = ls = 0;
02011 if (currMB->mb_type == I4MB && currSlice->slice_type == SI_SLICE)
02012 {
02013 if (left_block.available)
02014 if (p_Img->siblock [left_block.pos_y][left_block.pos_x])
02015 ls=1;
02016
02017 if (top_block.available)
02018 if (p_Img->siblock [top_block.pos_y][top_block.pos_x])
02019 ts=1;
02020 }
02021
02022 upIntraPredMode = (top_block.available &&(ts == 0)) ? p_Img->ipredmode[top_block.pos_y ][top_block.pos_x ] : -1;
02023 leftIntraPredMode = (left_block.available &&(ls == 0)) ? p_Img->ipredmode[left_block.pos_y][left_block.pos_x] : -1;
02024
02025 mostProbableIntraPredMode = (upIntraPredMode < 0 || leftIntraPredMode < 0) ? DC_PRED : upIntraPredMode < leftIntraPredMode ? upIntraPredMode : leftIntraPredMode;
02026
02027 dec = (currSE.value1 == -1) ? mostProbableIntraPredMode : currSE.value1 + (currSE.value1 >= mostProbableIntraPredMode);
02028
02029
02030 for(jj = 0; jj < (bs_y >> 2); ++jj)
02031 {
02032 memset(&(p_Img->ipredmode[bj + jj][bi]), dec, (bs_x>>2) * sizeof(char));
02033 }
02034 }
02035 }
02036 }
02037 }
02038
02039 if (IntraChromaPredModeFlag && (dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444))
02040 {
02041 currSE.type = SE_INTRAPREDMODE;
02042 TRACE_STRING("intra_chroma_pred_mode");
02043 dP = &(currSlice->partArr[partMap[currSE.type]]);
02044
02045 if (p_Img->active_pps->entropy_coding_mode_flag == CAVLC || dP->bitstream->ei_flag)
02046 {
02047 currSE.mapping = linfo_ue;
02048 }
02049 else
02050 currSE.reading = readCIPredMode_CABAC;
02051
02052 dP->readSyntaxElement(&currSE,p_Img,dP);
02053 currMB->c_ipred_mode = currSE.value1;
02054
02055 if (currMB->c_ipred_mode < DC_PRED_8 || currMB->c_ipred_mode > PLANE_8)
02056 {
02057 error("illegal chroma intra pred mode!\n", 600);
02058 }
02059 }
02060 }
02061
02062
02069 void get_neighbors(Macroblock *currMB,
02070 PixelPos *block,
02071 int mb_x,
02072 int mb_y,
02073 int blockshape_x
02074 )
02075 {
02076 ImageParameters *p_Img = currMB->p_Img;
02077 int *mb_size = p_Img->mb_size[IS_LUMA];
02078
02079 get4x4Neighbour(currMB, mb_x - 1, mb_y , mb_size, &block[0]);
02080 get4x4Neighbour(currMB, mb_x, mb_y - 1, mb_size, &block[1]);
02081 get4x4Neighbour(currMB, mb_x + blockshape_x, mb_y - 1, mb_size, &block[2]);
02082 get4x4Neighbour(currMB, mb_x - 1, mb_y - 1, mb_size, &block[3]);
02083
02084 if (mb_y > 0)
02085 {
02086 if (mb_x < 8)
02087 {
02088 if (mb_y == 8 )
02089 {
02090 if (blockshape_x == MB_BLOCK_SIZE)
02091 block[2].available = 0;
02092 }
02093 else if (mb_x+blockshape_x == 8)
02094 {
02095 block[2].available = 0;
02096 }
02097 }
02098 else if (mb_x + blockshape_x == MB_BLOCK_SIZE)
02099 {
02100 block[2].available = 0;
02101 }
02102 }
02103
02104 if (!block[2].available)
02105 {
02106 block[2] = block[3];
02107 }
02108 }
02109
02116 static void read_motion_info_from_NAL_p_slice (Macroblock *currMB)
02117 {
02118 ImageParameters *p_Img = currMB->p_Img;
02119 Slice *currSlice = currMB->p_Slice;
02120
02121 int mb_nr = currMB->mbAddrX;
02122 SyntaxElement currSE;
02123 DataPartition *dP = NULL;
02124 const byte *partMap = assignSE2partition[currSlice->dp_mode];
02125 int partmode = ((currMB->mb_type == P8x8) ? 4 : currMB->mb_type);
02126 int step_h0 = BLOCK_STEP [partmode][0];
02127 int step_v0 = BLOCK_STEP [partmode][1];
02128
02129 int j4, i4;
02130 StorablePicture *dec_picture = p_Img->dec_picture;
02131 PicMotionParams *motion = &dec_picture->motion;
02132
02133 int list_offset = ((currSlice->MbaffFrameFlag)&&(currMB->mb_field))? (mb_nr&0x01) ? 4 : 2 : 0;
02134
02135
02136 currSE.type = SE_REFFRAME;
02137 dP = &(currSlice->partArr[partMap[SE_REFFRAME]]);
02138
02139 prepareListforRefIdx (currMB, &currSE, dP, currSlice->num_ref_idx_l0_active, (currMB->mb_type != P8x8) || (!p_Img->allrefzero));
02140 readMBRefPictureIdx (&currSE, dP, currMB, &motion->ref_idx[LIST_0][currMB->block_y], LIST_0, step_v0, step_h0);
02141
02142
02143 prepareListforRefIdx (currMB, &currSE, dP, currSlice->num_ref_idx_l1_active, (currMB->mb_type != P8x8) || (!p_Img->allrefzero));
02144 readMBRefPictureIdx (&currSE, dP, currMB, &motion->ref_idx[LIST_1][currMB->block_y], LIST_1, step_v0, step_h0);
02145
02146
02147 currSE.type = SE_MVD;
02148 dP = &(currSlice->partArr[partMap[SE_MVD]]);
02149
02150 if (p_Img->active_pps->entropy_coding_mode_flag == CAVLC || dP->bitstream->ei_flag)
02151 currSE.mapping = linfo_se;
02152 else
02153 currSE.reading = readMVD_CABAC;
02154
02155
02156 readMBMotionVectors (&currSE, dP, currMB, LIST_0, step_h0, step_v0);
02157
02158
02159 for(j4 = currMB->block_y; j4 < (currMB->block_y +4);++j4)
02160 {
02161 for(i4 = currMB->block_x; i4 < (currMB->block_x + 4);++i4)
02162 {
02163 if (motion->ref_idx[LIST_0][j4][i4] >= 0)
02164 motion->ref_pic_id[LIST_0][j4][i4] = dec_picture->ref_pic_num[p_Img->current_slice_nr][LIST_0 + list_offset][(short)motion->ref_idx[LIST_0][j4][i4]];
02165 else
02166 motion->ref_pic_id[LIST_0][j4][i4] = INT64_MIN;
02167 }
02168 }
02169 }
02170
02177 static void read_motion_info_from_NAL_b_slice (Macroblock *currMB)
02178 {
02179 ImageParameters *p_Img = currMB->p_Img;
02180 Slice *currSlice = currMB->p_Slice;
02181 int i,j,k;
02182 int mb_nr = currMB->mbAddrX;
02183 SyntaxElement currSE;
02184 DataPartition *dP = NULL;
02185 const byte *partMap = assignSE2partition[currSlice->dp_mode];
02186 int partmode = ((currMB->mb_type == P8x8) ? 4 : currMB->mb_type);
02187 int step_h0 = BLOCK_STEP [partmode][0];
02188 int step_v0 = BLOCK_STEP [partmode][1];
02189
02190 int i0, j0, j6;
02191
02192 int j4, i4, ii;
02193 StorablePicture *dec_picture = p_Img->dec_picture;
02194 PicMotionParams *motion = &dec_picture->motion;
02195 MotionParams *colocated;
02196
02197 int mv_scale = 0;
02198
02199 int list_offset = ((currSlice->MbaffFrameFlag)&&(currMB->mb_field))? (mb_nr&0x01) ? 4 : 2 : 0;
02200
02201
02202 if ((currSlice->MbaffFrameFlag) && (currMB->mb_field))
02203 {
02204 if(mb_nr&0x01)
02205 {
02206 colocated = &currSlice->p_colocated->bottom;
02207 }
02208 else
02209 {
02210 colocated = &currSlice->p_colocated->top;
02211 }
02212 }
02213 else
02214 {
02215 colocated = &currSlice->p_colocated->frame;
02216 }
02217
02218 if (currMB->mb_type == P8x8)
02219 {
02220 if (currSlice->direct_spatial_mv_pred_flag)
02221 {
02222 char l0_rFrame, l1_rFrame;
02223 short pmvl0[2]={0,0}, pmvl1[2]={0,0};
02224
02225 prepare_direct_params(currMB, dec_picture, pmvl0, pmvl1, &l0_rFrame, &l1_rFrame);
02226
02227 for (k = 0; k < 4; ++k)
02228 {
02229 if (currMB->b8mode[k] == 0)
02230 {
02231 i = currMB->block_x + 2 * (k & 0x01);
02232 for(j = 2 * (k >> 1); j < 2 * (k >> 1)+2;++j)
02233 {
02234 j6 = currMB->block_y_aff + j;
02235 j4 = currMB->block_y + j;
02236 for(i4 = i; i4 < i + 2; ++i4)
02237 {
02238 if (l0_rFrame >= 0)
02239 {
02240 if (!l0_rFrame && ((!colocated->moving_block[j6][i4]) && (!p_Img->listX[LIST_1 + list_offset][0]->is_long_term)))
02241 {
02242 motion->mv [LIST_0][j4][i4][0] = 0;
02243 motion->mv [LIST_0][j4][i4][1] = 0;
02244 motion->ref_idx[LIST_0][j4][i4] = 0;
02245 }
02246 else
02247 {
02248 motion->mv [LIST_0][j4][i4][0] = pmvl0[0];
02249 motion->mv [LIST_0][j4][i4][1] = pmvl0[1];
02250 motion->ref_idx[LIST_0][j4][i4] = l0_rFrame;
02251 }
02252 }
02253 else
02254 {
02255 motion->mv [LIST_0][j4][i4][0] = 0;
02256 motion->mv [LIST_0][j4][i4][1] = 0;
02257 motion->ref_idx[LIST_0][j4][i4] = -1;
02258 }
02259
02260 if (l1_rFrame >= 0)
02261 {
02262 if (l1_rFrame==0 && ((!colocated->moving_block[j6][i4])&& (!p_Img->listX[LIST_1 + list_offset][0]->is_long_term)))
02263 {
02264 motion->mv [LIST_1][j4][i4][0] = 0;
02265 motion->mv [LIST_1][j4][i4][1] = 0;
02266 motion->ref_idx[LIST_1][j4][i4] = 0;
02267 }
02268 else
02269 {
02270 motion->mv [LIST_1][j4][i4][0] = pmvl1[0];
02271 motion->mv [LIST_1][j4][i4][1] = pmvl1[1];
02272 motion->ref_idx[LIST_1][j4][i4] = l1_rFrame;
02273 }
02274 }
02275 else
02276 {
02277 motion->mv [LIST_1][j4][i4][0] = 0;
02278 motion->mv [LIST_1][j4][i4][1] = 0;
02279 motion->ref_idx[LIST_1][j4][i4] = -1;
02280 }
02281
02282 if (l0_rFrame <0 && l1_rFrame <0)
02283 {
02284 motion->ref_idx[LIST_0][j4][i4] = 0;
02285 motion->ref_idx[LIST_1][j4][i4] = 0;
02286 }
02287 }
02288 }
02289 }
02290 }
02291 }
02292 else
02293 {
02294 for (k = 0; k < 4; ++k)
02295 {
02296 if (currMB->b8mode[k] == 0)
02297 {
02298 for(j0 = 2 * (k >> 1); j0 < 2 * (k >> 1) + 2; j0 += step_v0)
02299 {
02300 for(i0 = currMB->block_x + 2*(k & 0x01); i0 < currMB->block_x + 2 * (k & 0x01)+2; i0 += step_h0)
02301 {
02302 int refList = colocated->ref_idx[LIST_0 ][currMB->block_y_aff + j0][i0]== -1 ? LIST_1 : LIST_0;
02303 int ref_idx = colocated->ref_idx[refList][currMB->block_y_aff + j0][i0];
02304 int mapped_idx = -1, iref;
02305
02306 if (ref_idx == -1)
02307 {
02308 for (j4 = currMB->block_y + j0; j4 < currMB->block_y + j0 + step_v0; ++j4)
02309 {
02310 memset(&motion->ref_idx [LIST_0][j4][i0], 0, step_h0 * sizeof(char));
02311 memset(&motion->ref_idx [LIST_1][j4][i0], 0, step_h0 * sizeof(char));
02312 memset(&motion->mv [LIST_0][j4][i0][0], 0, 2 * step_h0 * sizeof(short));
02313 memset(&motion->mv [LIST_1][j4][i0][0], 0, 2 * step_h0 * sizeof(short));
02314 }
02315 }
02316 else
02317 {
02318 for (iref = 0; iref < imin(currSlice->num_ref_idx_l0_active, p_Img->listXsize[LIST_0 + list_offset]); ++iref)
02319 {
02320 int curr_mb_field = ((currSlice->MbaffFrameFlag)&&(currMB->mb_field));
02321
02322 if(p_Img->structure==0 && curr_mb_field==0)
02323 {
02324
02325
02326
02327 if(p_Img->listX[0][iref]->top_poc * 2 == colocated->ref_pic_id[refList][currMB->block_y_aff + j0][i0]
02328 || p_Img->listX[0][iref]->bottom_poc * 2 == colocated->ref_pic_id[refList][currMB->block_y_aff + j0][i0])
02329 {
02330 mapped_idx=iref;
02331 break;
02332 }
02333 else
02334 mapped_idx=INVALIDINDEX;
02335 continue;
02336 }
02337 if (dec_picture->ref_pic_num[p_Img->current_slice_nr][LIST_0 + list_offset][iref]==colocated->ref_pic_id[refList][currMB->block_y_aff + j0][i0])
02338 {
02339 mapped_idx=iref;
02340 break;
02341 }
02342 else
02343 mapped_idx=INVALIDINDEX;
02344 }
02345
02346 if (INVALIDINDEX == mapped_idx)
02347 {
02348 error("temporal direct error: colocated block has ref that is unavailable",-1111);
02349 }
02350
02351 for (j = j0; j < j0 + step_v0; ++j)
02352 {
02353 j4 = currMB->block_y + j;
02354 j6 = currMB->block_y_aff + j;
02355
02356 for (i4 = i0; i4 < i0 + step_h0; ++i4)
02357 {
02358 mv_scale = currSlice->mvscale[LIST_0 + list_offset][mapped_idx];
02359
02360 motion->ref_idx [LIST_0][j4][i4] = (char) mapped_idx;
02361 motion->ref_idx [LIST_1][j4][i4] = 0;
02362
02363 if (mv_scale == 9999 || p_Img->listX[LIST_0+list_offset][mapped_idx]->is_long_term)
02364 {
02365 for (ii=0; ii < 2; ++ii)
02366 {
02367 motion->mv [LIST_0][j4][i4][ii] = colocated->mv[refList][j6][i4][ii];
02368 motion->mv [LIST_1][j4][i4][ii] = 0;
02369 }
02370 }
02371 else
02372 {
02373 for (ii=0; ii < 2; ++ii)
02374 {
02375 motion->mv [LIST_0][j4][i4][ii] = (short) ((mv_scale * colocated->mv[refList][j6][i4][ii] + 128 ) >> 8);
02376 motion->mv [LIST_1][j4][i4][ii] = (short) (motion->mv[LIST_0][j4][i4][ii] - colocated->mv[refList][j6][i4][ii]);
02377 }
02378 }
02379 }
02380 }
02381 }
02382 }
02383 }
02384 }
02385 }
02386 }
02387 }
02388
02389
02390 currSE.type = SE_REFFRAME;
02391 dP = &(currSlice->partArr[partMap[SE_REFFRAME]]);
02392
02393 prepareListforRefIdx (currMB, &currSE, dP, currSlice->num_ref_idx_l0_active, TRUE);
02394 readMBRefPictureIdx (&currSE, dP, currMB, &motion->ref_idx[LIST_0][currMB->block_y], LIST_0, step_v0, step_h0);
02395
02396
02397 prepareListforRefIdx (currMB, &currSE, dP, currSlice->num_ref_idx_l1_active, TRUE);
02398 readMBRefPictureIdx (&currSE, dP, currMB, &motion->ref_idx[LIST_1][currMB->block_y], LIST_1, step_v0, step_h0);
02399
02400
02401 currSE.type = SE_MVD;
02402 dP = &(currSlice->partArr[partMap[SE_MVD]]);
02403
02404 if (p_Img->active_pps->entropy_coding_mode_flag == CAVLC || dP->bitstream->ei_flag)
02405 currSE.mapping = linfo_se;
02406 else
02407 currSE.reading = readMVD_CABAC;
02408
02409
02410 readMBMotionVectors (&currSE, dP, currMB, LIST_0, step_h0, step_v0);
02411
02412 readMBMotionVectors (&currSE, dP, currMB, LIST_1, step_h0, step_v0);
02413
02414
02415
02416 for (k = LIST_0; k <= LIST_1; ++k)
02417 {
02418 for(j4 = currMB->block_y; j4 < (currMB->block_y +4);++j4)
02419 {
02420 for(i4 = currMB->block_x; i4 < (currMB->block_x + 4);++i4)
02421 {
02422 if (motion->ref_idx[k][j4][i4]>=0)
02423 motion->ref_pic_id[k][j4][i4] = dec_picture->ref_pic_num[p_Img->current_slice_nr][k + list_offset][(short)motion->ref_idx[k][j4][i4]];
02424 else
02425 motion->ref_pic_id[k][j4][i4] = INT64_MIN;
02426 }
02427 }
02428 }
02429 }
02430
02440 int predict_nnz(Macroblock *currMB, int block_type, int i,int j)
02441 {
02442 ImageParameters *p_Img = currMB->p_Img;
02443
02444 PixelPos pix;
02445
02446 int pred_nnz = 0;
02447 int cnt = 0;
02448
02449
02450 get4x4Neighbour(currMB, i - 1, j, p_Img->mb_size[IS_LUMA], &pix);
02451
02452 if (IS_INTRA(currMB) && pix.available && p_Img->active_pps->constrained_intra_pred_flag && (p_Img->currentSlice->dp_mode==PAR_DP_3))
02453 {
02454 pix.available &= p_Img->intra_block[pix.mb_addr];
02455 if (!pix.available)
02456 ++cnt;
02457 }
02458
02459 if (pix.available)
02460 {
02461 switch (block_type)
02462 {
02463 case LUMA:
02464 pred_nnz = p_Img->nz_coeff [pix.mb_addr ][0][pix.y][pix.x];
02465 ++cnt;
02466 break;
02467 case CB:
02468 pred_nnz = p_Img->nz_coeff [pix.mb_addr ][1][pix.y][pix.x];
02469 ++cnt;
02470 break;
02471 case CR:
02472 pred_nnz = p_Img->nz_coeff [pix.mb_addr ][2][pix.y][pix.x];
02473 ++cnt;
02474 break;
02475 default:
02476 error("writeCoeff4x4_CAVLC: Invalid block type", 600);
02477 break;
02478 }
02479 }
02480
02481
02482 get4x4Neighbour(currMB, i, j - 1, p_Img->mb_size[IS_LUMA], &pix);
02483
02484 if (IS_INTRA(currMB) && pix.available && p_Img->active_pps->constrained_intra_pred_flag && (p_Img->currentSlice->dp_mode==PAR_DP_3))
02485 {
02486 pix.available &= p_Img->intra_block[pix.mb_addr];
02487 if (!pix.available)
02488 ++cnt;
02489 }
02490
02491 if (pix.available)
02492 {
02493 switch (block_type)
02494 {
02495 case LUMA:
02496 pred_nnz += p_Img->nz_coeff [pix.mb_addr ][0][pix.y][pix.x];
02497 ++cnt;
02498 break;
02499 case CB:
02500 pred_nnz += p_Img->nz_coeff [pix.mb_addr ][1][pix.y][pix.x];
02501 ++cnt;
02502 break;
02503 case CR:
02504 pred_nnz += p_Img->nz_coeff [pix.mb_addr ][2][pix.y][pix.x];
02505 ++cnt;
02506 break;
02507 default:
02508 error("writeCoeff4x4_CAVLC: Invalid block type", 600);
02509 break;
02510 }
02511 }
02512
02513 if (cnt==2)
02514 {
02515 ++pred_nnz;
02516 pred_nnz>>=1;
02517 }
02518
02519 return pred_nnz;
02520 }
02521
02522
02532 int predict_nnz_chroma(Macroblock *currMB, int i,int j)
02533 {
02534 ImageParameters *p_Img = currMB->p_Img;
02535 StorablePicture *dec_picture = p_Img->dec_picture;
02536 PixelPos pix;
02537
02538 int pred_nnz = 0;
02539 int cnt = 0;
02540
02541 if (dec_picture->chroma_format_idc != YUV444)
02542 {
02543
02544
02545 get4x4Neighbour(currMB, ((i&0x01)<<2) - 1, j, p_Img->mb_size[IS_CHROMA], &pix);
02546
02547 if (IS_INTRA(currMB) && pix.available && p_Img->active_pps->constrained_intra_pred_flag && (p_Img->currentSlice->dp_mode==PAR_DP_3))
02548 {
02549 pix.available &= p_Img->intra_block[pix.mb_addr];
02550 if (!pix.available)
02551 ++cnt;
02552 }
02553
02554 if (pix.available)
02555 {
02556 pred_nnz = p_Img->nz_coeff [pix.mb_addr ][1][pix.y][2 * (i>>1) + pix.x];
02557 ++cnt;
02558 }
02559
02560
02561 get4x4Neighbour(currMB, ((i&0x01)<<2), j - 1, p_Img->mb_size[IS_CHROMA], &pix);
02562
02563 if (IS_INTRA(currMB) && pix.available && p_Img->active_pps->constrained_intra_pred_flag && (p_Img->currentSlice->dp_mode==PAR_DP_3))
02564 {
02565 pix.available &= p_Img->intra_block[pix.mb_addr];
02566 if (!pix.available)
02567 ++cnt;
02568 }
02569
02570 if (pix.available)
02571 {
02572 pred_nnz += p_Img->nz_coeff [pix.mb_addr ][1][pix.y][2 * (i>>1) + pix.x];
02573 ++cnt;
02574 }
02575
02576 if (cnt==2)
02577 {
02578 ++pred_nnz;
02579 pred_nnz >>= 1;
02580 }
02581 }
02582
02583 return pred_nnz;
02584 }
02585
02586
02597 static void readCoeff4x4_CAVLC (Macroblock *currMB,
02598 int block_type,
02599 int i, int j, int levarr[16], int runarr[16],
02600 int *number_coefficients)
02601 {
02602 Slice *currSlice = currMB->p_Slice;
02603 ImageParameters *p_Img = currMB->p_Img;
02604 int mb_nr = currMB->mbAddrX;
02605 SyntaxElement currSE;
02606 DataPartition *dP;
02607 const byte *partMap = assignSE2partition[currSlice->dp_mode];
02608 Bitstream *currStream;
02609
02610 int k, code, vlcnum;
02611 int numcoeff, numtrailingones, numcoeff_vlc;
02612 int level_two_or_higher;
02613 int numones, totzeros, abslevel, cdc=0, cac=0;
02614 int zerosleft, ntr, dptype = 0;
02615 int max_coeff_num = 0, nnz;
02616 char type[15];
02617 static const int incVlc[] = {0,3,6,12,24,48,32768};
02618
02619 numcoeff = 0;
02620
02621 switch (block_type)
02622 {
02623 case LUMA:
02624 max_coeff_num = 16;
02625 TRACE_PRINTF("Luma");
02626 dptype = IS_INTRA (currMB) ? SE_LUM_AC_INTRA : SE_LUM_AC_INTER;
02627 p_Img->nz_coeff[mb_nr][0][j][i] = 0;
02628 break;
02629 case LUMA_INTRA16x16DC:
02630 max_coeff_num = 16;
02631 TRACE_PRINTF("Lum16DC");
02632 dptype = SE_LUM_DC_INTRA;
02633 p_Img->nz_coeff[mb_nr][0][j][i] = 0;
02634 break;
02635 case LUMA_INTRA16x16AC:
02636 max_coeff_num = 15;
02637 TRACE_PRINTF("Lum16AC");
02638 dptype = SE_LUM_AC_INTRA;
02639 p_Img->nz_coeff[mb_nr][0][j][i] = 0;
02640 break;
02641 case CB:
02642 max_coeff_num = 16;
02643 TRACE_PRINTF("Luma_add1");
02644 dptype = (IS_INTRA (currMB)) ? SE_LUM_AC_INTRA : SE_LUM_AC_INTER;
02645 p_Img->nz_coeff[mb_nr][1][j][i] = 0;
02646 break;
02647 case CB_INTRA16x16DC:
02648 max_coeff_num = 16;
02649 TRACE_PRINTF("Luma_add1_16DC");
02650 dptype = SE_LUM_DC_INTRA;
02651 p_Img->nz_coeff[mb_nr][1][j][i] = 0;
02652 break;
02653 case CB_INTRA16x16AC:
02654 max_coeff_num = 15;
02655 TRACE_PRINTF("Luma_add1_16AC");
02656 dptype = SE_LUM_AC_INTRA;
02657 p_Img->nz_coeff[mb_nr][1][j][i] = 0;
02658 break;
02659 case CR:
02660 max_coeff_num = 16;
02661 TRACE_PRINTF("Luma_add2");
02662 dptype = (IS_INTRA (currMB)) ? SE_LUM_AC_INTRA : SE_LUM_AC_INTER;
02663 p_Img->nz_coeff[mb_nr][2][j][i] = 0;
02664 break;
02665 case CR_INTRA16x16DC:
02666 max_coeff_num = 16;
02667 TRACE_PRINTF("Luma_add2_16DC");
02668 dptype = SE_LUM_DC_INTRA;
02669 p_Img->nz_coeff[mb_nr][2][j][i] = 0;
02670 break;
02671 case CR_INTRA16x16AC:
02672 max_coeff_num = 15;
02673 TRACE_PRINTF("Luma_add1_16AC");
02674 dptype = SE_LUM_AC_INTRA;
02675 p_Img->nz_coeff[mb_nr][2][j][i] = 0;
02676 break;
02677 case CHROMA_DC:
02678 max_coeff_num = p_Img->num_cdc_coeff;
02679 cdc = 1;
02680 TRACE_PRINTF("ChrDC");
02681 dptype = IS_INTRA (currMB) ? SE_CHR_DC_INTRA : SE_CHR_DC_INTER;
02682 p_Img->nz_coeff[mb_nr][0][j][i] = 0;
02683 break;
02684 case CHROMA_AC:
02685 max_coeff_num = 15;
02686 cac = 1;
02687 TRACE_PRINTF("ChrDC");
02688 dptype = IS_INTRA (currMB) ? SE_CHR_AC_INTRA : SE_CHR_AC_INTER;
02689 p_Img->nz_coeff[mb_nr][0][j][i] = 0;
02690 break;
02691 default:
02692 error ("readCoeff4x4_CAVLC: invalid block type", 600);
02693 p_Img->nz_coeff[mb_nr][0][j][i] = 0;
02694 break;
02695 }
02696
02697 currSE.type = dptype;
02698 dP = &(currSlice->partArr[partMap[dptype]]);
02699 currStream = dP->bitstream;
02700
02701 if (!cdc)
02702 {
02703
02704 if(block_type==LUMA || block_type==LUMA_INTRA16x16DC || block_type==LUMA_INTRA16x16AC ||block_type==CHROMA_AC)
02705 {
02706 nnz = (!cac) ? predict_nnz(currMB, LUMA, i<<2, j<<2) : predict_nnz_chroma(currMB, i, ((j-4)<<2));
02707 }
02708 else if (block_type==CB || block_type==CB_INTRA16x16DC || block_type==CB_INTRA16x16AC)
02709 {
02710 nnz = predict_nnz(currMB, CB, i<<2, j<<2);
02711 }
02712 else
02713 {
02714 nnz = predict_nnz(currMB, CR, i<<2, j<<2);
02715 }
02716
02717 if (nnz < 2)
02718 {
02719 numcoeff_vlc = 0;
02720 }
02721 else if (nnz < 4)
02722 {
02723 numcoeff_vlc = 1;
02724 }
02725 else if (nnz < 8)
02726 {
02727 numcoeff_vlc = 2;
02728 }
02729 else
02730 {
02731 numcoeff_vlc = 3;
02732 }
02733
02734 currSE.value1 = numcoeff_vlc;
02735
02736 readSyntaxElement_NumCoeffTrailingOnes(&currSE, currStream, type);
02737
02738 numcoeff = currSE.value1;
02739 numtrailingones = currSE.value2;
02740
02741 if(block_type==LUMA || block_type==LUMA_INTRA16x16DC || block_type==LUMA_INTRA16x16AC ||block_type==CHROMA_AC)
02742 p_Img->nz_coeff[mb_nr][0][j][i] = numcoeff;
02743 else if (block_type==CB || block_type==CB_INTRA16x16DC || block_type==CB_INTRA16x16AC)
02744 p_Img->nz_coeff[mb_nr][1][j][i] = numcoeff;
02745 else
02746 p_Img->nz_coeff[mb_nr][2][j][i] = numcoeff;
02747 }
02748 else
02749 {
02750
02751 readSyntaxElement_NumCoeffTrailingOnesChromaDC(p_Img, &currSE, currStream);
02752
02753 numcoeff = currSE.value1;
02754 numtrailingones = currSE.value2;
02755 }
02756
02757 memset(levarr, 0, max_coeff_num * sizeof(int));
02758 memset(runarr, 0, max_coeff_num * sizeof(int));
02759
02760 numones = numtrailingones;
02761 *number_coefficients = numcoeff;
02762
02763 if (numcoeff)
02764 {
02765 if (numtrailingones)
02766 {
02767 currSE.len = numtrailingones;
02768
02769 #if TRACE
02770 snprintf(currSE.tracestring,
02771 TRACESTRING_SIZE, "%s trailing ones sign (%d,%d)", type, i, j);
02772 #endif
02773
02774 readSyntaxElement_FLC (&currSE, currStream);
02775
02776 code = currSE.inf;
02777 ntr = numtrailingones;
02778 for (k = numcoeff - 1; k > numcoeff - 1 - numtrailingones; k--)
02779 {
02780 ntr --;
02781 levarr[k] = (code>>ntr)&1 ? -1 : 1;
02782 }
02783 }
02784
02785
02786 level_two_or_higher = (numcoeff > 3 && numtrailingones == 3)? 0 : 1;
02787 vlcnum = (numcoeff > 10 && numtrailingones < 3) ? 1 : 0;
02788
02789 for (k = numcoeff - 1 - numtrailingones; k >= 0; k--)
02790 {
02791
02792 #if TRACE
02793 snprintf(currSE.tracestring,
02794 TRACESTRING_SIZE, "%s lev (%d,%d) k=%d vlc=%d ", type, i, j, k, vlcnum);
02795 #endif
02796
02797 if (vlcnum == 0)
02798 readSyntaxElement_Level_VLC0(&currSE, currStream);
02799 else
02800 readSyntaxElement_Level_VLCN(&currSE, vlcnum, currStream);
02801
02802 if (level_two_or_higher)
02803 {
02804 currSE.inf += (currSE.inf > 0) ? 1 : -1;
02805 level_two_or_higher = 0;
02806 }
02807
02808 levarr[k] = currSE.inf;
02809 abslevel = iabs(levarr[k]);
02810 if (abslevel == 1)
02811 ++numones;
02812
02813
02814 if (abslevel > incVlc[vlcnum])
02815 ++vlcnum;
02816
02817 if (k == numcoeff - 1 - numtrailingones && abslevel >3)
02818 vlcnum = 2;
02819 }
02820
02821 if (numcoeff < max_coeff_num)
02822 {
02823
02824 vlcnum = numcoeff - 1;
02825 currSE.value1 = vlcnum;
02826
02827 #if TRACE
02828 snprintf(currSE.tracestring,
02829 TRACESTRING_SIZE, "%s totalrun (%d,%d) vlc=%d ", type, i,j, vlcnum);
02830 #endif
02831 if (cdc)
02832 readSyntaxElement_TotalZerosChromaDC(p_Img, &currSE, currStream);
02833 else
02834 readSyntaxElement_TotalZeros(&currSE, currStream);
02835
02836 totzeros = currSE.value1;
02837 }
02838 else
02839 {
02840 totzeros = 0;
02841 }
02842
02843
02844 zerosleft = totzeros;
02845 i = numcoeff - 1;
02846
02847 if (zerosleft > 0 && i > 0)
02848 {
02849 do
02850 {
02851
02852 vlcnum = imin(zerosleft - 1, RUNBEFORE_NUM_M1);
02853
02854 currSE.value1 = vlcnum;
02855 #if TRACE
02856 snprintf(currSE.tracestring,
02857 TRACESTRING_SIZE, "%s run (%d,%d) k=%d vlc=%d ",
02858 type, i, j, i, vlcnum);
02859 #endif
02860
02861 readSyntaxElement_Run(&currSE, currStream);
02862 runarr[i] = currSE.value1;
02863
02864 zerosleft -= runarr[i];
02865 i --;
02866 } while (zerosleft != 0 && i != 0);
02867 }
02868 runarr[i] = zerosleft;
02869 }
02870 }
02871
02879 static void readCompCoeff4x4SMB_CABAC (Macroblock *currMB, SyntaxElement *currSE, ColorPlane pl, int block_y, int block_x, int start_scan, int64 *cbp_blk)
02880 {
02881 int i,j,k;
02882 int i0, j0;
02883 int level = 1;
02884 DataPartition *dP;
02885 ImageParameters *p_Img = currMB->p_Img;
02886 Slice *currSlice = currMB->p_Slice;
02887 const byte *partMap = assignSE2partition[currSlice->dp_mode];
02888
02889 const byte (*pos_scan4x4)[2] = ((p_Img->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN : FIELD_SCAN;
02890 const byte *pos_scan_4x4 = pos_scan4x4[0];
02891 int **cof = currSlice->cof[pl];
02892
02893 for (j = block_y; j < block_y + BLOCK_SIZE_8x8; j += 4)
02894 {
02895 currMB->subblock_y = j;
02896
02897 for (i = block_x; i < block_x + BLOCK_SIZE_8x8; i += 4)
02898 {
02899 currMB->subblock_x = i;
02900 pos_scan_4x4 = pos_scan4x4[start_scan];
02901 level = 1;
02902
02903 if (start_scan == 0)
02904 {
02905
02906
02907
02908
02909 currSE->type = (currMB->is_intra_block ? SE_LUM_DC_INTRA : SE_LUM_DC_INTER);
02910 dP = &(currSlice->partArr[partMap[currSE->type]]);
02911 if (dP->bitstream->ei_flag)
02912 currSE->mapping = linfo_levrun_inter;
02913 else
02914 currSE->reading = readRunLevel_CABAC;
02915
02916 #if TRACE
02917 if (pl == PLANE_Y)
02918 sprintf(currSE->tracestring, "Luma sng ");
02919 else if (pl == PLANE_U)
02920 sprintf(currSE->tracestring, "Cb sng ");
02921 else
02922 sprintf(currSE->tracestring, "Cr sng ");
02923 #endif
02924
02925 dP->readSyntaxElement(currSE,p_Img,dP);
02926 level = currSE->value1;
02927
02928 if (level != 0)
02929 {
02930 pos_scan_4x4 += 2 * currSE->value2;
02931
02932 i0 = *pos_scan_4x4++;
02933 j0 = *pos_scan_4x4++;
02934
02935 *cbp_blk |= (int64) 1 << (j + (i >> 2)) ;
02936
02937 cof[j + j0][i + i0]= level;
02938
02939 }
02940 }
02941
02942 if (level != 0)
02943 {
02944
02945 currSE->type = (currMB->is_intra_block ? SE_LUM_AC_INTRA : SE_LUM_AC_INTER);
02946 dP = &(currSlice->partArr[partMap[currSE->type]]);
02947
02948 if (dP->bitstream->ei_flag)
02949 currSE->mapping = linfo_levrun_inter;
02950 else
02951 currSE->reading = readRunLevel_CABAC;
02952
02953 for(k = 1; (k < 17) && (level != 0); ++k)
02954 {
02955 #if TRACE
02956 if (pl == PLANE_Y)
02957 sprintf(currSE->tracestring, "Luma sng ");
02958 else if (pl == PLANE_U)
02959 sprintf(currSE->tracestring, "Cb sng ");
02960 else
02961 sprintf(currSE->tracestring, "Cr sng ");
02962 #endif
02963
02964 dP->readSyntaxElement(currSE, p_Img, dP);
02965 level = currSE->value1;
02966
02967 if (level != 0)
02968 {
02969 pos_scan_4x4 += 2 * currSE->value2;
02970
02971 i0 = *pos_scan_4x4++;
02972 j0 = *pos_scan_4x4++;
02973
02974
02975 cof[j + j0][i + i0]= level;
02976
02977 }
02978 }
02979 }
02980 }
02981 }
02982 }
02983
02991 static void readCompCoeff4x4MB_CABAC (Macroblock *currMB, SyntaxElement *currSE, ColorPlane pl, int intra, int (*InvLevelScale4x4)[4], int qp_per, int cbp)
02992 {
02993 Slice *currSlice = currMB->p_Slice;
02994 ImageParameters *p_Img = currMB->p_Img;
02995 int start_scan = IS_I16MB (currMB)? 1 : 0;
02996 int block_y, block_x;
02997 int i, j;
02998 int64 *cbp_blk = &currMB->cbp_blk[pl];
02999
03000 currMB->is_intra_block = intra;
03001
03002 if( pl == PLANE_Y || IS_INDEPENDENT(p_Img) )
03003 currSE->context = (IS_I16MB(currMB) ? LUMA_16AC: LUMA_4x4);
03004 else if (pl == PLANE_U)
03005 currSE->context = (IS_I16MB(currMB) ? CB_16AC: CB_4x4);
03006 else
03007 currSE->context = (IS_I16MB(currMB) ? CR_16AC: CR_4x4);
03008
03009 if (currMB->is_lossless == FALSE)
03010 {
03011 for (block_y = 0; block_y < MB_BLOCK_SIZE; block_y += BLOCK_SIZE_8x8)
03012 {
03013 int **cof = &currSlice->cof[pl][block_y];
03014 for (block_x = 0; block_x < MB_BLOCK_SIZE; block_x += BLOCK_SIZE_8x8)
03015 {
03016 if (cbp & (1 << ((block_y >> 2) + (block_x >> 3))))
03017 {
03018 readCompCoeff4x4SMB_CABAC (currMB, currSE, pl, block_y, block_x, start_scan, cbp_blk);
03019
03020 if (start_scan == 0)
03021 {
03022 for (j = 0; j < BLOCK_SIZE_8x8; ++j)
03023 {
03024 for (i = 0; i < BLOCK_SIZE_8x8; ++i)
03025 {
03026 if (cof[j][block_x + i])
03027 cof[j][block_x + i]= rshift_rnd_sf((cof[j][block_x + i] * InvLevelScale4x4[j & 0x03][i & 0x03]) << qp_per, 4);
03028 }
03029 }
03030 }
03031 else
03032 {
03033 for (j = 0; j < BLOCK_SIZE_8x8; ++j)
03034 {
03035 for (i = 0; i < BLOCK_SIZE_8x8; ++i)
03036 {
03037 if (((i & 0x03) != 0 || (j & 0x03) != 0) && cof[j][block_x + i])
03038 {
03039 cof[j][block_x + i]= rshift_rnd_sf((cof[j][block_x + i] * InvLevelScale4x4[j & 0x03][i & 0x03]) << qp_per, 4);
03040 }
03041 }
03042 }
03043 }
03044 }
03045
03046 }
03047 }
03048 }
03049 else
03050 {
03051 for (block_y = 0; block_y < MB_BLOCK_SIZE; block_y += BLOCK_SIZE_8x8)
03052 {
03053 for (block_x = 0; block_x < MB_BLOCK_SIZE; block_x += BLOCK_SIZE_8x8)
03054 {
03055 if (cbp & (1 << ((block_y >> 2) + (block_x >> 3))))
03056 {
03057 readCompCoeff4x4SMB_CABAC (currMB, currSE, pl, block_y, block_x, start_scan, cbp_blk);
03058 }
03059 }
03060 }
03061 }
03062 }
03063
03071 static void readCompCoeff8x8_CABAC (Macroblock *currMB, SyntaxElement *currSE, ColorPlane pl, int b8)
03072 {
03073 if (currMB->cbp & (1<<b8))
03074 {
03075 ImageParameters *p_Img = currMB->p_Img;
03076 int transform_pl = IS_INDEPENDENT(p_Img) ? p_Img->colour_plane_id : pl;
03077
03078 int **tcoeffs;
03079 int i,j,k;
03080 int level = 1;
03081
03082 DataPartition *dP;
03083 Slice *currSlice = currMB->p_Slice;
03084 const byte *partMap = assignSE2partition[currSlice->dp_mode];
03085 int boff_x, boff_y;
03086
03087 int64 cbp_mask = (int64) 51 << (4 * b8 - 2 * (b8 & 0x01));
03088 int64 *cur_cbp = &currMB->cbp_blk[pl];
03089
03090
03091 const byte (*pos_scan8x8) = ((p_Img->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN8x8[0] : FIELD_SCAN8x8[0];
03092
03093 int qp_per = p_Img->qp_per_matrix[ currMB->qp_scaled[pl] ];
03094 int qp_rem = p_Img->qp_rem_matrix[ currMB->qp_scaled[pl] ];
03095
03096 int (*InvLevelScale8x8)[8] = IS_INTRA(currMB)? currSlice->InvLevelScale8x8_Intra[transform_pl][qp_rem] : currSlice->InvLevelScale8x8_Inter[transform_pl][qp_rem];
03097
03098 currMB->is_intra_block = IS_INTRA(currMB);
03099
03100
03101 boff_x = (b8&0x01) << 3;
03102 boff_y = (b8 >> 1) << 3;
03103 tcoeffs = &currSlice->mb_rres[pl][boff_y];
03104
03105 currMB->subblock_x = boff_x;
03106 currMB->subblock_y = boff_y;
03107
03108 if (pl==PLANE_Y || IS_INDEPENDENT(p_Img))
03109 currSE->context = LUMA_8x8;
03110 else if (pl==PLANE_U)
03111 currSE->context = CB_8x8;
03112 else
03113 currSE->context = CR_8x8;
03114
03115 currSE->reading = readRunLevel_CABAC;
03116
03117 if(currMB->is_lossless == FALSE)
03118 {
03119
03120 currSE->type = ((currMB->is_intra_block == 1) ? SE_LUM_DC_INTRA : SE_LUM_DC_INTER );
03121 dP = &(currSlice->partArr[partMap[currSE->type]]);
03122
03123 #if TRACE
03124 if (pl==PLANE_Y)
03125 sprintf(currSE->tracestring, "Luma8x8 DC sng ");
03126 else if (pl==PLANE_U)
03127 sprintf(currSE->tracestring, "Cb 8x8 DC sng ");
03128 else
03129 sprintf(currSE->tracestring, "Cr 8x8 DC sng ");
03130 #endif
03131
03132 dP->readSyntaxElement(currSE,p_Img,dP);
03133 level = currSE->value1;
03134
03135
03136 if (level != 0)
03137 {
03138 *cur_cbp |= cbp_mask;
03139
03140 pos_scan8x8 += 2 * (currSE->value2);
03141
03142 i = *pos_scan8x8++;
03143 j = *pos_scan8x8++;
03144
03145 tcoeffs[j][boff_x + i] = rshift_rnd_sf((level * InvLevelScale8x8[j][i]) << qp_per, 6);
03146
03147
03148 currSE->type = ((currMB->is_intra_block == 1) ? SE_LUM_AC_INTRA : SE_LUM_AC_INTER);
03149 dP = &(currSlice->partArr[partMap[currSE->type]]);
03150
03151 for(k = 1;(k < 65) && (level != 0);++k)
03152 {
03153 #if TRACE
03154 if (pl==PLANE_Y)
03155 sprintf(currSE->tracestring, "Luma8x8 sng ");
03156 else if (pl==PLANE_U)
03157 sprintf(currSE->tracestring, "Cb 8x8 sng ");
03158 else
03159 sprintf(currSE->tracestring, "Cr 8x8 sng ");
03160 #endif
03161
03162 dP->readSyntaxElement(currSE,p_Img,dP);
03163 level = currSE->value1;
03164
03165
03166 if (level != 0)
03167 {
03168 pos_scan8x8 += 2 * (currSE->value2);
03169
03170 i = *pos_scan8x8++;
03171 j = *pos_scan8x8++;
03172
03173 tcoeffs[ j][boff_x + i] = rshift_rnd_sf((level * InvLevelScale8x8[j][i]) << qp_per, 6);
03174 }
03175 }
03176 }
03177 }
03178 else
03179 {
03180
03181 for(k=0; (k < 65) && (level != 0);++k)
03182 {
03183
03184
03185
03186
03187
03188
03189 currSE->type = ((currMB->is_intra_block == 1)
03190 ? (k==0 ? SE_LUM_DC_INTRA : SE_LUM_AC_INTRA)
03191 : (k==0 ? SE_LUM_DC_INTER : SE_LUM_AC_INTER));
03192
03193 #if TRACE
03194 if (pl==PLANE_Y)
03195 sprintf(currSE->tracestring, "Luma8x8 sng ");
03196 else if (pl==PLANE_U)
03197 sprintf(currSE->tracestring, "Cb 8x8 sng ");
03198 else
03199 sprintf(currSE->tracestring, "Cr 8x8 sng ");
03200 #endif
03201
03202 dP = &(currSlice->partArr[partMap[currSE->type]]);
03203 currSE->reading = readRunLevel_CABAC;
03204
03205 dP->readSyntaxElement(currSE,p_Img,dP);
03206 level = currSE->value1;
03207
03208
03209 if (level != 0)
03210 {
03211 pos_scan8x8 += 2 * (currSE->value2);
03212
03213 i = *pos_scan8x8++;
03214 j = *pos_scan8x8++;
03215
03216 *cur_cbp |= cbp_mask;
03217
03218 tcoeffs[j][boff_x + i] = level;
03219 }
03220 }
03221 }
03222 }
03223 }
03224
03225
03233 static void readCompCoeff8x8MB_CABAC (Macroblock *currMB, SyntaxElement *currSE, ColorPlane pl)
03234 {
03235
03236 readCompCoeff8x8_CABAC (currMB, currSE, pl, 0);
03237 readCompCoeff8x8_CABAC (currMB, currSE, pl, 1);
03238 readCompCoeff8x8_CABAC (currMB, currSE, pl, 2);
03239 readCompCoeff8x8_CABAC (currMB, currSE, pl, 3);
03240 }
03241
03249 static void readCompCoeff4x4MB_CAVLC (Macroblock *currMB, ColorPlane pl, int (*InvLevelScale4x4)[4], int qp_per, int cbp, byte **nzcoeff)
03250 {
03251 int block_y, block_x, b8;
03252 int i, j, k;
03253 int i0, j0;
03254 int levarr[16] = {0}, runarr[16] = {0}, numcoeff;
03255 Slice *currSlice = currMB->p_Slice;
03256 ImageParameters *p_Img = currMB->p_Img;
03257 const byte (*pos_scan4x4)[2] = ((p_Img->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN : FIELD_SCAN;
03258 const byte *pos_scan_4x4 = pos_scan4x4[0];
03259 int start_scan = IS_I16MB(currMB) ? 1 : 0;
03260 int64 *cur_cbp = &currMB->cbp_blk[pl];
03261 int coef_ctr, cur_context;
03262
03263 if (IS_I16MB(currMB))
03264 {
03265 if (pl == PLANE_Y)
03266 cur_context = LUMA_INTRA16x16AC;
03267 else if (pl == PLANE_U)
03268 cur_context = CB_INTRA16x16AC;
03269 else
03270 cur_context = CR_INTRA16x16AC;
03271 }
03272 else
03273 {
03274 if (pl == PLANE_Y)
03275 cur_context = LUMA;
03276 else if (pl == PLANE_U)
03277 cur_context = CB;
03278 else
03279 cur_context = CR;
03280 }
03281
03282 if (currMB->is_lossless == FALSE)
03283 {
03284 for (block_y = 0; block_y < 4; block_y += 2)
03285 {
03286 for (block_x = 0; block_x < 4; block_x += 2)
03287 {
03288 b8 = (block_y + (block_x >> 1));
03289
03290 if (cbp & (1 << b8))
03291 {
03292 for (j=block_y << 2; j < (block_y + 2) << 2; j += BLOCK_SIZE)
03293 {
03294 for (i=block_x << 2; i < (block_x + 2) << 2; i += BLOCK_SIZE)
03295 {
03296 readCoeff4x4_CAVLC(currMB, cur_context, i >> 2, j >> 2, levarr, runarr, &numcoeff);
03297 pos_scan_4x4 = pos_scan4x4[start_scan];
03298
03299 for (k = 0; k < numcoeff; ++k)
03300 {
03301 if (levarr[k] != 0)
03302 {
03303 pos_scan_4x4 += (runarr[k] << 1);
03304
03305 i0 = *pos_scan_4x4++;
03306 j0 = *pos_scan_4x4++;
03307
03308
03309 *cur_cbp |= (int64) 1 << (j + (i >> 2));
03310
03311 currSlice->cof[pl][j + j0][i + i0]= rshift_rnd_sf((levarr[k] * InvLevelScale4x4[j0][i0])<<qp_per, 4);
03312
03313 }
03314 }
03315 }
03316 }
03317 }
03318 else
03319 {
03320 for (j=block_y; j < block_y + 2; ++j)
03321 {
03322 memset(&nzcoeff[j][block_x], 0, 2 * sizeof(byte));
03323 }
03324 }
03325 }
03326 }
03327 }
03328 else
03329 {
03330 for (block_y=0; block_y < 4; block_y += 2)
03331 {
03332 for (block_x=0; block_x < 4; block_x += 2)
03333 {
03334 b8 = 2*(block_y>>1) + (block_x>>1);
03335
03336 if (cbp & (1<<b8))
03337 {
03338 for (j=block_y; j < block_y+2; ++j)
03339 {
03340 for (i=block_x; i < block_x+2; ++i)
03341 {
03342 readCoeff4x4_CAVLC(currMB, cur_context, i, j, levarr, runarr, &numcoeff);
03343
03344 coef_ctr = start_scan - 1;
03345
03346 for (k = 0; k < numcoeff; ++k)
03347 {
03348 if (levarr[k] != 0)
03349 {
03350 coef_ctr += runarr[k]+1;
03351
03352 i0=pos_scan4x4[coef_ctr][0];
03353 j0=pos_scan4x4[coef_ctr][1];
03354
03355 *cur_cbp |= (int64) 1 << ((j<<2) + i);
03356 currSlice->cof[pl][(j<<2) + j0][(i<<2) + i0]= levarr[k];
03357
03358 }
03359 }
03360 }
03361 }
03362 }
03363 else
03364 {
03365 for (j=block_y; j < block_y+2; ++j)
03366 {
03367 memset(&nzcoeff[j][block_x], 0, 2 * sizeof(byte));
03368 }
03369 }
03370 }
03371 }
03372 }
03373 }
03374
03375
03383 static void readCompCoeff8x8MB_CAVLC (Macroblock *currMB, ColorPlane pl, int (*InvLevelScale8x8)[8], int qp_per, int cbp, byte **nzcoeff)
03384 {
03385 int block_y, block_x, b4, b8;
03386 int i, j, k;
03387 int i0, j0;
03388 int levarr[16] = {0}, runarr[16] = {0}, numcoeff;
03389 Slice *currSlice = currMB->p_Slice;
03390 ImageParameters *p_Img = currMB->p_Img;
03391 const byte (*pos_scan8x8)[2] = ((p_Img->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN8x8 : FIELD_SCAN8x8;
03392 int start_scan = IS_I16MB(currMB) ? 1 : 0;
03393 int64 *cur_cbp = &currMB->cbp_blk[pl];
03394 int coef_ctr, cur_context;
03395
03396 if (IS_I16MB(currMB))
03397 {
03398 if (pl == PLANE_Y)
03399 cur_context = LUMA_INTRA16x16AC;
03400 else if (pl == PLANE_U)
03401 cur_context = CB_INTRA16x16AC;
03402 else
03403 cur_context = CR_INTRA16x16AC;
03404 }
03405 else
03406 {
03407 if (pl == PLANE_Y)
03408 cur_context = LUMA;
03409 else if (pl == PLANE_U)
03410 cur_context = CB;
03411 else
03412 cur_context = CR;
03413 }
03414
03415 if (currMB->is_lossless == FALSE)
03416 {
03417
03418 for (block_y=0; block_y < 4; block_y += 2)
03419 {
03420 for (block_x=0; block_x < 4; block_x += 2)
03421 {
03422 b8 = block_y + (block_x>>1);
03423
03424 if (cbp & (1<<b8))
03425 {
03426 for (j=block_y; j < block_y+2; ++j)
03427 {
03428 for (i=block_x; i < block_x+2; ++i)
03429 {
03430 readCoeff4x4_CAVLC(currMB, cur_context, i, j, levarr, runarr, &numcoeff);
03431
03432 coef_ctr = start_scan - 1;
03433
03434 for (k = 0; k < numcoeff; ++k)
03435 {
03436 if (levarr[k] != 0)
03437 {
03438 coef_ctr += runarr[k]+1;
03439
03440
03441
03442 *cur_cbp |= 51 << ((block_y<<2) + block_x);
03443
03444 b4 = (coef_ctr << 2) + 2*(j - block_y)+(i - block_x);
03445
03446 i0 = pos_scan8x8[b4][0];
03447 j0 = pos_scan8x8[b4][1];
03448
03449 currSlice->mb_rres[pl][block_y*4 +j0][block_x*4 +i0] = rshift_rnd_sf((levarr[k] * InvLevelScale8x8[j0][i0])<<qp_per, 6);
03450 }
03451 }
03452 }
03453 }
03454 }
03455 else
03456 {
03457 for (j=block_y; j < block_y+2; ++j)
03458 {
03459 memset(&nzcoeff[j][block_x], 0, 2 * sizeof(byte));
03460 }
03461 }
03462 }
03463 }
03464 }
03465 else
03466 {
03467 for (block_y=0; block_y < 4; block_y += 2)
03468 {
03469 for (block_x=0; block_x < 4; block_x += 2)
03470 {
03471 b8 = 2*(block_y>>1) + (block_x>>1);
03472
03473 if (cbp & (1<<b8))
03474 {
03475 int iz, jz;
03476
03477 for (j=block_y; j < block_y+2; ++j)
03478 {
03479 for (i=block_x; i < block_x+2; ++i)
03480 {
03481
03482 readCoeff4x4_CAVLC(currMB, cur_context, i, j, levarr, runarr, &numcoeff);
03483
03484 coef_ctr = start_scan - 1;
03485
03486 for (k = 0; k < numcoeff; ++k)
03487 {
03488 if (levarr[k] != 0)
03489 {
03490 coef_ctr += runarr[k]+1;
03491
03492
03493
03494 *cur_cbp |= 51 << ((block_y<<2) + block_x);
03495
03496 b4 = 2*(j-block_y)+(i-block_x);
03497
03498 iz=pos_scan8x8[coef_ctr*4+b4][0];
03499 jz=pos_scan8x8[coef_ctr*4+b4][1];
03500
03501 currSlice->mb_rres[pl][block_y*4 +jz][block_x*4 +iz] = levarr[k];
03502 }
03503 }
03504 }
03505 }
03506 }
03507 else
03508 {
03509 for (j=block_y; j < block_y+2; ++j)
03510 {
03511 memset(&nzcoeff[j][block_x], 0, 2 * sizeof(byte));
03512 }
03513 }
03514 }
03515 }
03516 }
03517 }
03518
03527 static void check_dp_neighbors (Macroblock *currMB)
03528 {
03529 ImageParameters *p_Img = currMB->p_Img;
03530 PixelPos up, left;
03531
03532 p_Img->getNeighbour(currMB, -1, 0, p_Img->mb_size[1], &left);
03533 p_Img->getNeighbour(currMB, 0, -1, p_Img->mb_size[1], &up);
03534
03535 if (IS_INTER (currMB) || (IS_INTRA (currMB) && !(p_Img->active_pps->constrained_intra_pred_flag)) )
03536 {
03537 if (left.available)
03538 {
03539 currMB->dpl_flag |= p_Img->mb_data[left.mb_addr].dpl_flag;
03540 }
03541 if (up.available)
03542 {
03543 currMB->dpl_flag |= p_Img->mb_data[up.mb_addr].dpl_flag;
03544 }
03545 }
03546 }
03547
03548
03556 static void read_CBP_and_coeffs_from_NAL_CABAC(Macroblock *currMB)
03557 {
03558 int i,j,k;
03559 int level;
03560 int cbp;
03561 SyntaxElement currSE;
03562 DataPartition *dP = NULL;
03563 Slice *currSlice = currMB->p_Slice;
03564 const byte *partMap = assignSE2partition[currSlice->dp_mode];
03565 int coef_ctr, i0, j0, b8;
03566 int ll;
03567
03568 int qp_per, qp_rem;
03569 ImageParameters *p_Img = currMB->p_Img;
03570 int smb = (currSlice->slice_type==SP_SLICE && currMB->mb_type!=I16MB ) || (((currSlice->slice_type==SP_SLICE) && IS_INTER (currMB)) || (currSlice->slice_type == SI_SLICE && currMB->mb_type == SI4MB));
03571
03572 int uv;
03573 int qp_per_uv[2];
03574 int qp_rem_uv[2];
03575
03576 int intra = IS_INTRA (currMB);
03577 int temp[4];
03578
03579 int b4;
03580 StorablePicture *dec_picture = p_Img->dec_picture;
03581 int yuv = dec_picture->chroma_format_idc - 1;
03582 int m6[4];
03583
03584 int need_transform_size_flag;
03585
03586 int (*InvLevelScale4x4)[4] = NULL;
03587 int (*InvLevelScale8x8)[8] = NULL;
03588
03589 const byte (*pos_scan4x4)[2] = ((p_Img->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN : FIELD_SCAN;
03590 const byte *pos_scan_4x4 = pos_scan4x4[0];
03591
03592
03593
03594 if (dec_picture->chroma_format_idc != YUV400)
03595 {
03596 for (i=0; i<2; ++i)
03597 {
03598 qp_per_uv[i] = p_Img->qp_per_matrix[ currMB->qp_scaled[i + 1] ];
03599 qp_rem_uv[i] = p_Img->qp_rem_matrix[ currMB->qp_scaled[i + 1] ];
03600 }
03601 }
03602
03603
03604 if (!IS_I16MB (currMB))
03605 {
03606
03607
03608 currSE.type = (currMB->mb_type == I4MB || currMB->mb_type == SI4MB || currMB->mb_type == I8MB)
03609 ? SE_CBP_INTRA
03610 : SE_CBP_INTER;
03611
03612 dP = &(currSlice->partArr[partMap[currSE.type]]);
03613
03614 if (dP->bitstream->ei_flag)
03615 {
03616 currSE.mapping = (currMB->mb_type == I4MB || currMB->mb_type == SI4MB || currMB->mb_type == I8MB)
03617 ? currSlice->linfo_cbp_intra
03618 : currSlice->linfo_cbp_inter;
03619 }
03620 else
03621 {
03622 currSE.reading = readCBP_CABAC;
03623 }
03624
03625 TRACE_STRING("coded_block_pattern");
03626 dP->readSyntaxElement(&currSE, p_Img, dP);
03627 currMB->cbp = cbp = currSE.value1;
03628
03629
03630
03631
03632 need_transform_size_flag = (((currMB->mb_type >= 1 && currMB->mb_type <= 3)||
03633 (IS_DIRECT(currMB) && p_Img->active_sps->direct_8x8_inference_flag) ||
03634 (currMB->NoMbPartLessThan8x8Flag))
03635 && currMB->mb_type != I8MB && currMB->mb_type != I4MB
03636 && (currMB->cbp&15)
03637 && p_Img->Transform8x8Mode);
03638
03639 if (need_transform_size_flag)
03640 {
03641 currSE.type = SE_HEADER;
03642 dP = &(currSlice->partArr[partMap[SE_HEADER]]);
03643 currSE.reading = readMB_transform_size_flag_CABAC;
03644 TRACE_STRING("transform_size_8x8_flag");
03645
03646
03647 if (dP->bitstream->ei_flag)
03648 {
03649 currSE.len = 1;
03650 readSyntaxElement_FLC(&currSE, dP->bitstream);
03651 }
03652 else
03653 {
03654 dP->readSyntaxElement(&currSE,p_Img,dP);
03655 }
03656 currMB->luma_transform_size_8x8_flag = (Boolean) currSE.value1;
03657 }
03658
03659
03660
03661
03662 if (cbp !=0)
03663 {
03664 read_delta_quant(&currSE, dP, currMB, partMap, (IS_INTER (currMB)) ? SE_DELTA_QUANT_INTER : SE_DELTA_QUANT_INTRA);
03665
03666 if (currSlice->dp_mode)
03667 {
03668 if (IS_INTER (currMB) && currSlice->dpC_NotPresent )
03669 currMB->dpl_flag = 1;
03670
03671 if( intra && currSlice->dpB_NotPresent )
03672 {
03673 currMB->ei_flag = 1;
03674 currMB->dpl_flag = 1;
03675 }
03676
03677
03678 check_dp_neighbors (currMB);
03679 if (currMB->dpl_flag)
03680 {
03681 cbp = 0;
03682 currMB->cbp = cbp;
03683 }
03684 }
03685 }
03686 }
03687 else
03688 {
03689 cbp = currMB->cbp;
03690 }
03691
03692 memset(&currSlice->cof[0][0][0], 0, MB_PIXELS * sizeof(int));
03693
03694
03695 if (IS_I16MB (currMB))
03696 {
03697 read_delta_quant(&currSE, dP, currMB, partMap, SE_DELTA_QUANT_INTRA);
03698
03699 for (j=0;j<BLOCK_SIZE;++j)
03700 memset(&p_Img->ipredmode[currMB->block_y+j][currMB->block_x], DC_PRED, BLOCK_SIZE * sizeof(byte));
03701
03702 if (currSlice->dp_mode)
03703 {
03704 if (currSlice->dpB_NotPresent)
03705 {
03706 currMB->ei_flag = 1;
03707 currMB->dpl_flag = 1;
03708 }
03709 check_dp_neighbors (currMB);
03710 if (currMB->dpl_flag)
03711 {
03712 currMB->cbp = cbp = 0;
03713 }
03714 }
03715
03716 if (!currMB->dpl_flag)
03717 {
03718 pos_scan_4x4 = pos_scan4x4[0];
03719
03720 {
03721 currSE.type = SE_LUM_DC_INTRA;
03722 dP = &(currSlice->partArr[partMap[currSE.type]]);
03723
03724 currSE.context = LUMA_16DC;
03725 currSE.type = SE_LUM_DC_INTRA;
03726 currMB->is_intra_block = 1;
03727
03728 if (dP->bitstream->ei_flag)
03729 {
03730 currSE.mapping = linfo_levrun_inter;
03731 }
03732 else
03733 {
03734 currSE.reading = readRunLevel_CABAC;
03735 }
03736
03737 level = 1;
03738
03739 for(k = 0; (k < 17) && (level != 0); ++k)
03740 {
03741 #if TRACE
03742 snprintf(currSE.tracestring, TRACESTRING_SIZE, "DC luma 16x16 ");
03743 #endif
03744 dP->readSyntaxElement(&currSE, p_Img, dP);
03745 level = currSE.value1;
03746
03747 if (level != 0)
03748 {
03749 pos_scan_4x4 += (2 * currSE.value2);
03750
03751 i0 = ((*pos_scan_4x4++) << 2);
03752 j0 = ((*pos_scan_4x4++) << 2);
03753
03754 currSlice->cof[0][j0][i0] = level;
03755
03756 }
03757 }
03758 }
03759
03760 if(currMB->is_lossless == FALSE)
03761 itrans_2(currMB, (ColorPlane) p_Img->colour_plane_id);
03762 }
03763 }
03764
03765 update_qp(currMB, p_Img->qp);
03766
03767 qp_per = p_Img->qp_per_matrix[ currMB->qp_scaled[p_Img->colour_plane_id] ];
03768 qp_rem = p_Img->qp_rem_matrix[ currMB->qp_scaled[p_Img->colour_plane_id] ];
03769
03770
03771 if (dec_picture->chroma_format_idc != YUV400)
03772 {
03773 for(i=0; i < 2; ++i)
03774 {
03775 qp_per_uv[i] = p_Img->qp_per_matrix[ currMB->qp_scaled[i + 1] ];
03776 qp_rem_uv[i] = p_Img->qp_rem_matrix[ currMB->qp_scaled[i + 1] ];
03777 }
03778 }
03779
03780 InvLevelScale4x4 = intra? currSlice->InvLevelScale4x4_Intra[p_Img->colour_plane_id][qp_rem] : currSlice->InvLevelScale4x4_Inter[p_Img->colour_plane_id][qp_rem];
03781 InvLevelScale8x8 = intra? currSlice->InvLevelScale8x8_Intra[p_Img->colour_plane_id][qp_rem] : currSlice->InvLevelScale8x8_Inter[p_Img->colour_plane_id][qp_rem];
03782
03783
03784 {
03785
03786
03787 if (cbp)
03788 {
03789 if(currMB->luma_transform_size_8x8_flag)
03790 {
03791
03792 readCompCoeff8x8MB_CABAC (currMB, &currSE, PLANE_Y);
03793 }
03794 else
03795 {
03796 readCompCoeff4x4MB_CABAC (currMB, &currSE, PLANE_Y, intra, InvLevelScale4x4, qp_per, cbp);
03797 }
03798 }
03799 }
03800
03801 if ( p_Img->active_sps->chroma_format_idc==YUV444 && !IS_INDEPENDENT(p_Img) )
03802 {
03803 for (uv = 0; uv < 2; ++uv )
03804 {
03805 memset(&currSlice->cof[uv + 1][0][0], 0, MB_PIXELS * sizeof(int));
03806
03807
03808
03809 if (IS_I16MB (currMB))
03810 {
03811 for (j = 0; j < BLOCK_SIZE; ++j)
03812 memset(&p_Img->ipredmode[currMB->block_y + j][currMB->block_x], DC_PRED, BLOCK_SIZE * sizeof(byte));
03813
03814 {
03815 currSE.type = SE_LUM_DC_INTRA;
03816 dP = &(currSlice->partArr[partMap[currSE.type]]);
03817
03818 if( IS_INDEPENDENT(p_Img) )
03819 currSE.context = LUMA_16DC;
03820 else
03821 currSE.context = (uv==0) ? CB_16DC : CR_16DC;
03822
03823 currMB->is_intra_block = 1;
03824
03825 if (dP->bitstream->ei_flag)
03826 {
03827 currSE.mapping = linfo_levrun_inter;
03828 }
03829 else
03830 {
03831 currSE.reading = readRunLevel_CABAC;
03832 }
03833
03834 coef_ctr = -1;
03835 level = 1;
03836
03837 for(k=0;(k<17) && (level!=0);++k)
03838 {
03839 #if TRACE
03840 if (uv == 0)
03841 snprintf(currSE.tracestring, TRACESTRING_SIZE, "DC Cb 16x16 ");
03842 else
03843 snprintf(currSE.tracestring, TRACESTRING_SIZE, "DC Cr 16x16 ");
03844 #endif
03845
03846 dP->readSyntaxElement(&currSE,p_Img,dP);
03847 level = currSE.value1;
03848
03849 if (level != 0)
03850 {
03851 coef_ctr += currSE.value2 + 1;
03852
03853 i0 = pos_scan4x4[coef_ctr][0];
03854 j0 = pos_scan4x4[coef_ctr][1];
03855 currSlice->cof[uv + 1][j0<<2][i0<<2] = level;
03856
03857 }
03858 }
03859 }
03860
03861 if(currMB->is_lossless == FALSE)
03862 {
03863 itrans_2(currMB, (ColorPlane) (uv + 1));
03864 }
03865 }
03866
03867 update_qp(currMB, p_Img->qp);
03868
03869 qp_per = p_Img->qp_per_matrix[ (p_Img->qp + p_Img->bitdepth_luma_qp_scale) ];
03870 qp_rem = p_Img->qp_rem_matrix[ (p_Img->qp + p_Img->bitdepth_luma_qp_scale) ];
03871
03872
03873 qp_per_uv[uv] = p_Img->qp_per_matrix[ (currMB->qpc[uv] + p_Img->bitdepth_chroma_qp_scale) ];
03874 qp_rem_uv[uv] = p_Img->qp_rem_matrix[ (currMB->qpc[uv] + p_Img->bitdepth_chroma_qp_scale) ];
03875
03876 InvLevelScale4x4 = intra? currSlice->InvLevelScale4x4_Intra[uv + 1][qp_rem_uv[uv]] : currSlice->InvLevelScale4x4_Inter[uv + 1][qp_rem_uv[uv]];
03877 InvLevelScale8x8 = intra? currSlice->InvLevelScale8x8_Intra[uv + 1][qp_rem_uv[uv]] : currSlice->InvLevelScale8x8_Inter[uv + 1][qp_rem_uv[uv]];
03878
03879 {
03880 if (cbp)
03881 {
03882 if(currMB->luma_transform_size_8x8_flag)
03883 {
03884
03885 readCompCoeff8x8MB_CABAC (currMB, &currSE, (ColorPlane) (PLANE_U + uv));
03886 }
03887 else
03888 {
03889 readCompCoeff4x4MB_CABAC (currMB, &currSE, (ColorPlane) (PLANE_U + uv), intra, InvLevelScale4x4, qp_per_uv[uv], cbp);
03890 }
03891 }
03892 }
03893 }
03894 }
03895 else if ((dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444))
03896 {
03897 memset(&currSlice->cof[1][0][0], 0, 2 * MB_PIXELS * sizeof(int));
03898
03899
03900
03901
03902
03903 if(cbp>15)
03904 {
03905 if (dec_picture->chroma_format_idc == YUV420)
03906 {
03907 for (ll=0;ll<3;ll+=2)
03908 {
03909 uv = ll>>1;
03910
03911 InvLevelScale4x4 = intra ? currSlice->InvLevelScale4x4_Intra[uv + 1][qp_rem_uv[uv]] : currSlice->InvLevelScale4x4_Inter[uv + 1][qp_rem_uv[uv]];
03912
03913 memset(&currSlice->cofu[0], 0, 4 *sizeof(int));
03914 coef_ctr=-1;
03915
03916 {
03917 level=1;
03918 currMB->is_intra_block = intra;
03919 currMB->is_v_block = ll;
03920 currSE.context = CHROMA_DC;
03921 currSE.type = (intra ? SE_CHR_DC_INTRA : SE_CHR_DC_INTER);
03922
03923 dP = &(currSlice->partArr[partMap[currSE.type]]);
03924
03925 if (dP->bitstream->ei_flag)
03926 currSE.mapping = linfo_levrun_c2x2;
03927 else
03928 currSE.reading = readRunLevel_CABAC;
03929
03930 for(k = 0; (k < (p_Img->num_cdc_coeff + 1))&&(level!=0);++k)
03931 {
03932 #if TRACE
03933 snprintf(currSE.tracestring, TRACESTRING_SIZE, "2x2 DC Chroma ");
03934 #endif
03935
03936 dP->readSyntaxElement(&currSE,p_Img,dP);
03937 level = currSE.value1;
03938
03939 if (level != 0)
03940 {
03941 currMB->cbp_blk[0] |= 0xf0000 << (ll<<1) ;
03942 coef_ctr += currSE.value2 + 1;
03943
03944
03945
03946
03947
03948
03949
03950
03951
03952 assert (coef_ctr < p_Img->num_cdc_coeff);
03953 currSlice->cofu[coef_ctr]=level;
03954 }
03955 }
03956 }
03957
03958 if (smb || (currMB->is_lossless == TRUE))
03959 {
03960 currSlice->cof[uv + 1][0][0] = currSlice->cofu[0];
03961 currSlice->cof[uv + 1][4][0] = currSlice->cofu[1];
03962 currSlice->cof[uv + 1][0][4] = currSlice->cofu[2];
03963 currSlice->cof[uv + 1][4][4] = currSlice->cofu[3];
03964
03965
03966
03967
03968 }
03969 else
03970 {
03971 ihadamard2x2(currSlice->cofu, temp);
03972
03973
03974
03975
03976
03977 currSlice->cof[uv + 1][0][0] = (((temp[0] * InvLevelScale4x4[0][0])<<qp_per_uv[uv])>>5);
03978 currSlice->cof[uv + 1][0][4] = (((temp[1] * InvLevelScale4x4[0][0])<<qp_per_uv[uv])>>5);
03979 currSlice->cof[uv + 1][4][0] = (((temp[2] * InvLevelScale4x4[0][0])<<qp_per_uv[uv])>>5);
03980 currSlice->cof[uv + 1][4][4] = (((temp[3] * InvLevelScale4x4[0][0])<<qp_per_uv[uv])>>5);
03981 }
03982 }
03983 }
03984 else if (dec_picture->chroma_format_idc == YUV422)
03985 {
03986 for (ll=0;ll<3;ll+=2)
03987 {
03988 int (*InvLevelScale4x4)[4] = NULL;
03989 uv = ll>>1;
03990 {
03991 int **imgcof = currSlice->cof[uv + 1];
03992 int m3[2][4] = {{0,0,0,0},{0,0,0,0}};
03993 int m4[2][4] = {{0,0,0,0},{0,0,0,0}};
03994 int qp_per_uv_dc = p_Img->qp_per_matrix[ (currMB->qpc[uv] + 3 + p_Img->bitdepth_chroma_qp_scale) ];
03995 int qp_rem_uv_dc = p_Img->qp_rem_matrix[ (currMB->qpc[uv] + 3 + p_Img->bitdepth_chroma_qp_scale) ];
03996 if (intra)
03997 InvLevelScale4x4 = currSlice->InvLevelScale4x4_Intra[uv + 1][qp_rem_uv_dc];
03998 else
03999 InvLevelScale4x4 = currSlice->InvLevelScale4x4_Inter[uv + 1][qp_rem_uv_dc];
04000
04001
04002
04003 {
04004 coef_ctr=-1;
04005 level=1;
04006 for(k=0;(k<9)&&(level!=0);++k)
04007 {
04008 currSE.context = CHROMA_DC_2x4;
04009 currSE.type = (IS_INTRA(currMB) ? SE_CHR_DC_INTRA : SE_CHR_DC_INTER);
04010 currMB->is_intra_block = IS_INTRA(currMB);
04011 currMB->is_v_block = ll;
04012
04013 #if TRACE
04014 snprintf(currSE.tracestring, TRACESTRING_SIZE, "2x4 DC Chroma ");
04015 #endif
04016 dP = &(currSlice->partArr[partMap[currSE.type]]);
04017
04018 if (dP->bitstream->ei_flag)
04019 currSE.mapping = linfo_levrun_c2x2;
04020 else
04021 currSE.reading = readRunLevel_CABAC;
04022
04023 dP->readSyntaxElement(&currSE,p_Img,dP);
04024
04025 level = currSE.value1;
04026
04027 if (level != 0)
04028 {
04029 currMB->cbp_blk[0] |= ((int64)0xff0000) << (ll<<2) ;
04030 coef_ctr += currSE.value2 + 1;
04031 assert (coef_ctr < p_Img->num_cdc_coeff);
04032 i0=SCAN_YUV422[coef_ctr][0];
04033 j0=SCAN_YUV422[coef_ctr][1];
04034
04035 m3[i0][j0]=level;
04036 }
04037 }
04038 }
04039
04040
04041 if(currMB->is_lossless == FALSE)
04042 {
04043 m4[0][0] = m3[0][0] + m3[1][0];
04044 m4[0][1] = m3[0][1] + m3[1][1];
04045 m4[0][2] = m3[0][2] + m3[1][2];
04046 m4[0][3] = m3[0][3] + m3[1][3];
04047
04048 m4[1][0] = m3[0][0] - m3[1][0];
04049 m4[1][1] = m3[0][1] - m3[1][1];
04050 m4[1][2] = m3[0][2] - m3[1][2];
04051 m4[1][3] = m3[0][3] - m3[1][3];
04052
04053 for (i = 0; i < 2; ++i)
04054 {
04055 m6[0] = m4[i][0] + m4[i][2];
04056 m6[1] = m4[i][0] - m4[i][2];
04057 m6[2] = m4[i][1] - m4[i][3];
04058 m6[3] = m4[i][1] + m4[i][3];
04059
04060 imgcof[ 0][i<<2] = m6[0] + m6[3];
04061 imgcof[ 4][i<<2] = m6[1] + m6[2];
04062 imgcof[ 8][i<<2] = m6[1] - m6[2];
04063 imgcof[12][i<<2] = m6[0] - m6[3];
04064 }
04065 }
04066 else
04067 {
04068 for(j=0;j<4;++j)
04069 {
04070 for(i=0;i<2;++i)
04071 {
04072 currSlice->cof[uv + 1][j<<2][i<<2] = m3[i][j];
04073
04074 }
04075 }
04076 }
04077
04078 for(j = 0;j < p_Img->mb_cr_size_y; j += BLOCK_SIZE)
04079 {
04080 for(i=0;i < p_Img->mb_cr_size_x;i+=BLOCK_SIZE)
04081 {
04082 imgcof[j][i] = rshift_rnd_sf((imgcof[j][i] * InvLevelScale4x4[0][0]) << qp_per_uv_dc, 6);
04083 }
04084 }
04085 }
04086 }
04087 }
04088 }
04089
04090
04091
04092
04093 if (cbp<=31)
04094 {
04095 }
04096 else
04097 {
04098 {
04099 currMB->is_intra_block = IS_INTRA(currMB);
04100 currSE.context = CHROMA_AC;
04101 currSE.type = (currMB->is_intra_block ? SE_CHR_AC_INTRA : SE_CHR_AC_INTER);
04102
04103 dP = &(currSlice->partArr[partMap[currSE.type]]);
04104
04105 if (dP->bitstream->ei_flag)
04106 currSE.mapping = linfo_levrun_inter;
04107 else
04108 currSE.reading = readRunLevel_CABAC;
04109
04110 if(currMB->is_lossless == FALSE)
04111 {
04112 for (b8=0; b8 < p_Img->num_blk8x8_uv; ++b8)
04113 {
04114 currMB->is_v_block = uv = (b8 > ((p_Img->num_uv_blocks) - 1 ));
04115 InvLevelScale4x4 = intra ? currSlice->InvLevelScale4x4_Intra[uv + 1][qp_rem_uv[uv]] : currSlice->InvLevelScale4x4_Inter[uv + 1][qp_rem_uv[uv]];
04116
04117 for (b4 = 0; b4 < 4; ++b4)
04118 {
04119 i = cofuv_blk_x[yuv][b8][b4];
04120 j = cofuv_blk_y[yuv][b8][b4];
04121
04122 currMB->subblock_y = subblk_offset_y[yuv][b8][b4];
04123 currMB->subblock_x = subblk_offset_x[yuv][b8][b4];
04124
04125 pos_scan_4x4 = pos_scan4x4[1];
04126 level=1;
04127
04128 for(k = 0; (k < 16) && (level != 0);++k)
04129 {
04130 #if TRACE
04131 snprintf(currSE.tracestring, TRACESTRING_SIZE, "AC Chroma ");
04132 #endif
04133
04134 dP->readSyntaxElement(&currSE,p_Img,dP);
04135 level = currSE.value1;
04136
04137 if (level != 0)
04138 {
04139 currMB->cbp_blk[0] |= ((int64)1) << cbp_blk_chroma[b8][b4];
04140 pos_scan_4x4 += (currSE.value2 << 1);
04141
04142 i0 = *pos_scan_4x4++;
04143 j0 = *pos_scan_4x4++;
04144
04145 currSlice->cof[uv + 1][(j<<2) + j0][(i<<2) + i0] = rshift_rnd_sf((level * InvLevelScale4x4[j0][i0])<<qp_per_uv[uv], 4);
04146
04147 }
04148 }
04149 }
04150 }
04151 }
04152 else
04153 {
04154 for (b8=0; b8 < p_Img->num_blk8x8_uv; ++b8)
04155 {
04156 currMB->is_v_block = uv = (b8 > ((p_Img->num_uv_blocks) - 1 ));
04157
04158 for (b4=0; b4 < 4; ++b4)
04159 {
04160 i = cofuv_blk_x[yuv][b8][b4];
04161 j = cofuv_blk_y[yuv][b8][b4];
04162
04163 pos_scan_4x4 = pos_scan4x4[1];
04164 level=1;
04165
04166 currMB->subblock_y = subblk_offset_y[yuv][b8][b4];
04167 currMB->subblock_x = subblk_offset_x[yuv][b8][b4];
04168
04169 for(k=0;(k<16)&&(level!=0);++k)
04170 {
04171 #if TRACE
04172 snprintf(currSE.tracestring, TRACESTRING_SIZE, "AC Chroma ");
04173 #endif
04174 dP->readSyntaxElement(&currSE,p_Img,dP);
04175 level = currSE.value1;
04176
04177 if (level != 0)
04178 {
04179 currMB->cbp_blk[0] |= ((int64)1) << cbp_blk_chroma[b8][b4];
04180 pos_scan_4x4 += (currSE.value2 << 1);
04181
04182 i0 = *pos_scan_4x4++;
04183 j0 = *pos_scan_4x4++;
04184
04185 currSlice->cof[uv + 1][(j<<2) + j0][(i<<2) + i0] = level;
04186
04187 }
04188 }
04189 }
04190 }
04191 }
04192 }
04193 }
04194 }
04195 }
04196
04204 static void read_CBP_and_coeffs_from_NAL_CAVLC(Macroblock *currMB)
04205 {
04206 int i,j,k;
04207 int level;
04208 int mb_nr = currMB->mbAddrX;
04209 int cbp;
04210 SyntaxElement currSE;
04211 DataPartition *dP = NULL;
04212 Slice *currSlice = currMB->p_Slice;
04213 const byte *partMap = assignSE2partition[currSlice->dp_mode];
04214 int coef_ctr, i0, j0, b8;
04215 int ll;
04216 int levarr[16], runarr[16], numcoeff;
04217
04218 int qp_per, qp_rem;
04219 ImageParameters *p_Img = currMB->p_Img;
04220 int smb = (currSlice->slice_type==SP_SLICE && currMB->mb_type!=I16MB ) || (((currSlice->slice_type==SP_SLICE) && IS_INTER (currMB)) || (currSlice->slice_type == SI_SLICE && currMB->mb_type == SI4MB));
04221
04222 int uv;
04223 int qp_per_uv[2];
04224 int qp_rem_uv[2];
04225
04226 int intra = IS_INTRA (currMB);
04227 int temp[4];
04228
04229 int b4;
04230 StorablePicture *dec_picture = p_Img->dec_picture;
04231 int yuv = dec_picture->chroma_format_idc - 1;
04232 int m6[4];
04233
04234 int need_transform_size_flag;
04235
04236 int (*InvLevelScale4x4)[4] = NULL;
04237 int (*InvLevelScale8x8)[8] = NULL;
04238
04239 const byte (*pos_scan4x4)[2] = ((p_Img->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN : FIELD_SCAN;
04240 const byte *pos_scan_4x4 = pos_scan4x4[0];
04241
04242
04243
04244 if (dec_picture->chroma_format_idc != YUV400)
04245 {
04246 for (i=0; i<2; ++i)
04247 {
04248 qp_per_uv[i] = p_Img->qp_per_matrix[ currMB->qp_scaled[i + 1] ];
04249 qp_rem_uv[i] = p_Img->qp_rem_matrix[ currMB->qp_scaled[i + 1] ];
04250 }
04251 }
04252
04253
04254 if (!IS_I16MB (currMB))
04255 {
04256
04257
04258 currSE.type = (currMB->mb_type == I4MB || currMB->mb_type == SI4MB || currMB->mb_type == I8MB)
04259 ? SE_CBP_INTRA
04260 : SE_CBP_INTER;
04261
04262 dP = &(currSlice->partArr[partMap[currSE.type]]);
04263
04264 currSE.mapping = (currMB->mb_type == I4MB || currMB->mb_type == SI4MB || currMB->mb_type == I8MB)
04265 ? currSlice->linfo_cbp_intra
04266 : currSlice->linfo_cbp_inter;
04267
04268 TRACE_STRING("coded_block_pattern");
04269 dP->readSyntaxElement(&currSE, p_Img, dP);
04270 currMB->cbp = cbp = currSE.value1;
04271
04272
04273
04274
04275 need_transform_size_flag = (((currMB->mb_type >= 1 && currMB->mb_type <= 3)||
04276 (IS_DIRECT(currMB) && p_Img->active_sps->direct_8x8_inference_flag) ||
04277 (currMB->NoMbPartLessThan8x8Flag))
04278 && currMB->mb_type != I8MB && currMB->mb_type != I4MB
04279 && (currMB->cbp&15)
04280 && p_Img->Transform8x8Mode);
04281
04282 if (need_transform_size_flag)
04283 {
04284 currSE.type = SE_HEADER;
04285 dP = &(currSlice->partArr[partMap[SE_HEADER]]);
04286 currSE.reading = readMB_transform_size_flag_CABAC;
04287 TRACE_STRING("transform_size_8x8_flag");
04288
04289
04290 currSE.len = 1;
04291 readSyntaxElement_FLC(&currSE, dP->bitstream);
04292
04293 currMB->luma_transform_size_8x8_flag = (Boolean) currSE.value1;
04294 }
04295
04296
04297
04298
04299 if (cbp !=0)
04300 {
04301 read_delta_quant(&currSE, dP, currMB, partMap, (IS_INTER (currMB)) ? SE_DELTA_QUANT_INTER : SE_DELTA_QUANT_INTRA);
04302
04303 if (currSlice->dp_mode)
04304 {
04305 if (IS_INTER (currMB) && currSlice->dpC_NotPresent )
04306 currMB->dpl_flag = 1;
04307
04308 if( intra && currSlice->dpB_NotPresent )
04309 {
04310 currMB->ei_flag = 1;
04311 currMB->dpl_flag = 1;
04312 }
04313
04314
04315 check_dp_neighbors (currMB);
04316 if (currMB->dpl_flag)
04317 {
04318 cbp = 0;
04319 currMB->cbp = cbp;
04320 }
04321 }
04322 }
04323 }
04324 else
04325 {
04326 cbp = currMB->cbp;
04327 }
04328
04329 memset(&currSlice->cof[0][0][0], 0, MB_PIXELS * sizeof(int));
04330
04331
04332 if (IS_I16MB (currMB))
04333 {
04334 read_delta_quant(&currSE, dP, currMB, partMap, SE_DELTA_QUANT_INTRA);
04335
04336 for (j=0;j<BLOCK_SIZE;++j)
04337 memset(&p_Img->ipredmode[currMB->block_y+j][currMB->block_x], DC_PRED, BLOCK_SIZE * sizeof(byte));
04338
04339 if (currSlice->dp_mode)
04340 {
04341 if (currSlice->dpB_NotPresent)
04342 {
04343 currMB->ei_flag = 1;
04344 currMB->dpl_flag = 1;
04345 }
04346 check_dp_neighbors (currMB);
04347 if (currMB->dpl_flag)
04348 {
04349 currMB->cbp = cbp = 0;
04350 }
04351 }
04352
04353 if (!currMB->dpl_flag)
04354 {
04355 pos_scan_4x4 = pos_scan4x4[0];
04356
04357 readCoeff4x4_CAVLC(currMB, LUMA_INTRA16x16DC, 0, 0, levarr, runarr, &numcoeff);
04358
04359 for(k = 0; k < numcoeff; ++k)
04360 {
04361 if (levarr[k] != 0)
04362 {
04363 pos_scan_4x4 += 2 * runarr[k];
04364
04365 i0 = ((*pos_scan_4x4++) << 2);
04366 j0 = ((*pos_scan_4x4++) << 2);
04367
04368 currSlice->cof[0][j0][i0] = levarr[k];
04369
04370 }
04371 }
04372
04373
04374 if(currMB->is_lossless == FALSE)
04375 itrans_2(currMB, (ColorPlane) p_Img->colour_plane_id);
04376 }
04377 }
04378
04379 update_qp(currMB, p_Img->qp);
04380
04381 qp_per = p_Img->qp_per_matrix[ currMB->qp_scaled[p_Img->colour_plane_id] ];
04382 qp_rem = p_Img->qp_rem_matrix[ currMB->qp_scaled[p_Img->colour_plane_id] ];
04383
04384
04385 if (dec_picture->chroma_format_idc != YUV400)
04386 {
04387 for(i=0; i < 2; ++i)
04388 {
04389 qp_per_uv[i] = p_Img->qp_per_matrix[ currMB->qp_scaled[i + 1] ];
04390 qp_rem_uv[i] = p_Img->qp_rem_matrix[ currMB->qp_scaled[i + 1] ];
04391 }
04392 }
04393
04394 InvLevelScale4x4 = intra? currSlice->InvLevelScale4x4_Intra[p_Img->colour_plane_id][qp_rem] : currSlice->InvLevelScale4x4_Inter[p_Img->colour_plane_id][qp_rem];
04395 InvLevelScale8x8 = intra? currSlice->InvLevelScale8x8_Intra[p_Img->colour_plane_id][qp_rem] : currSlice->InvLevelScale8x8_Inter[p_Img->colour_plane_id][qp_rem];
04396
04397
04398 if (cbp)
04399 {
04400 if (!currMB->luma_transform_size_8x8_flag)
04401 {
04402 readCompCoeff4x4MB_CAVLC (currMB, PLANE_Y, InvLevelScale4x4, qp_per, cbp, p_Img->nz_coeff[mb_nr][PLANE_Y]);
04403 }
04404 else
04405 {
04406 readCompCoeff8x8MB_CAVLC (currMB, PLANE_Y, InvLevelScale8x8, qp_per, cbp, p_Img->nz_coeff[mb_nr][PLANE_Y]);
04407 }
04408 }
04409 else
04410 {
04411 memset(&p_Img->nz_coeff[mb_nr][0][0][0], 0, BLOCK_SIZE * BLOCK_SIZE * sizeof(byte));
04412 }
04413
04414 if ( p_Img->active_sps->chroma_format_idc==YUV444 && !IS_INDEPENDENT(p_Img) )
04415 {
04416 for (uv = 0; uv < 2; ++uv )
04417 {
04418 memset(&currSlice->cof[uv + 1][0][0], 0, MB_PIXELS * sizeof(int));
04419
04420
04421
04422 if (IS_I16MB (currMB))
04423 {
04424 for (j = 0; j < BLOCK_SIZE; ++j)
04425 memset(&p_Img->ipredmode[currMB->block_y + j][currMB->block_x], DC_PRED, BLOCK_SIZE * sizeof(byte));
04426
04427 if (uv == 0)
04428 readCoeff4x4_CAVLC(currMB, CB_INTRA16x16DC, 0, 0, levarr, runarr, &numcoeff);
04429 else
04430 readCoeff4x4_CAVLC(currMB, CR_INTRA16x16DC, 0, 0, levarr, runarr, &numcoeff);
04431
04432 coef_ctr=-1;
04433 level = 1;
04434
04435 for(k = 0; k < numcoeff; ++k)
04436 {
04437 if (levarr[k] != 0)
04438 {
04439 coef_ctr += runarr[k] + 1;
04440
04441 i0 = pos_scan4x4[coef_ctr][0];
04442 j0 = pos_scan4x4[coef_ctr][1];
04443 currSlice->cof[uv + 1][j0<<2][i0<<2] = levarr[k];
04444
04445 }
04446 }
04447
04448 if(currMB->is_lossless == FALSE)
04449 {
04450 itrans_2(currMB, (ColorPlane) (uv + 1));
04451 }
04452 }
04453
04454 update_qp(currMB, p_Img->qp);
04455
04456 qp_per = p_Img->qp_per_matrix[ (p_Img->qp + p_Img->bitdepth_luma_qp_scale) ];
04457 qp_rem = p_Img->qp_rem_matrix[ (p_Img->qp + p_Img->bitdepth_luma_qp_scale) ];
04458
04459
04460 qp_per_uv[uv] = p_Img->qp_per_matrix[ (currMB->qpc[uv] + p_Img->bitdepth_chroma_qp_scale) ];
04461 qp_rem_uv[uv] = p_Img->qp_rem_matrix[ (currMB->qpc[uv] + p_Img->bitdepth_chroma_qp_scale) ];
04462
04463 InvLevelScale4x4 = intra? currSlice->InvLevelScale4x4_Intra[uv + 1][qp_rem_uv[uv]] : currSlice->InvLevelScale4x4_Inter[uv + 1][qp_rem_uv[uv]];
04464 InvLevelScale8x8 = intra? currSlice->InvLevelScale8x8_Intra[uv + 1][qp_rem_uv[uv]] : currSlice->InvLevelScale8x8_Inter[uv + 1][qp_rem_uv[uv]];
04465
04466 if (!currMB->luma_transform_size_8x8_flag)
04467 {
04468 readCompCoeff4x4MB_CAVLC (currMB, (ColorPlane) (PLANE_U + uv), InvLevelScale4x4, qp_per_uv[uv], cbp, p_Img->nz_coeff[mb_nr][PLANE_U + uv]);
04469 }
04470 else
04471 {
04472 readCompCoeff8x8MB_CAVLC (currMB, (ColorPlane) (PLANE_U + uv), InvLevelScale8x8, qp_per_uv[uv], cbp, p_Img->nz_coeff[mb_nr][PLANE_U + uv]);
04473 }
04474 }
04475 }
04476 else if ((dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444))
04477 {
04478 memset(&currSlice->cof[1][0][0], 0, 2 * MB_PIXELS * sizeof(int));
04479
04480
04481
04482
04483
04484 if(cbp>15)
04485 {
04486 if (dec_picture->chroma_format_idc == YUV420)
04487 {
04488 for (ll=0;ll<3;ll+=2)
04489 {
04490 uv = ll>>1;
04491
04492 InvLevelScale4x4 = intra ? currSlice->InvLevelScale4x4_Intra[uv + 1][qp_rem_uv[uv]] : currSlice->InvLevelScale4x4_Inter[uv + 1][qp_rem_uv[uv]];
04493
04494 memset(&currSlice->cofu[0], 0, 4 *sizeof(int));
04495 coef_ctr=-1;
04496
04497 readCoeff4x4_CAVLC(currMB, CHROMA_DC, 0, 0, levarr, runarr, &numcoeff);
04498
04499 for(k = 0; k < numcoeff; ++k)
04500 {
04501 if (levarr[k] != 0)
04502 {
04503 currMB->cbp_blk[0] |= 0xf0000 << (ll<<1) ;
04504 coef_ctr += runarr[k] + 1;
04505 currSlice->cofu[coef_ctr]=levarr[k];
04506 }
04507 }
04508
04509
04510 if (smb || (currMB->is_lossless == TRUE))
04511 {
04512 currSlice->cof[uv + 1][0][0] = currSlice->cofu[0];
04513 currSlice->cof[uv + 1][4][0] = currSlice->cofu[1];
04514 currSlice->cof[uv + 1][0][4] = currSlice->cofu[2];
04515 currSlice->cof[uv + 1][4][4] = currSlice->cofu[3];
04516
04517
04518
04519
04520 }
04521 else
04522 {
04523 ihadamard2x2(currSlice->cofu, temp);
04524
04525
04526
04527
04528
04529 currSlice->cof[uv + 1][0][0] = (((temp[0] * InvLevelScale4x4[0][0])<<qp_per_uv[uv])>>5);
04530 currSlice->cof[uv + 1][0][4] = (((temp[1] * InvLevelScale4x4[0][0])<<qp_per_uv[uv])>>5);
04531 currSlice->cof[uv + 1][4][0] = (((temp[2] * InvLevelScale4x4[0][0])<<qp_per_uv[uv])>>5);
04532 currSlice->cof[uv + 1][4][4] = (((temp[3] * InvLevelScale4x4[0][0])<<qp_per_uv[uv])>>5);
04533 }
04534 }
04535 }
04536 else if (dec_picture->chroma_format_idc == YUV422)
04537 {
04538 for (ll=0;ll<3;ll+=2)
04539 {
04540 int (*InvLevelScale4x4)[4] = NULL;
04541 uv = ll>>1;
04542 {
04543 int **imgcof = currSlice->cof[uv + 1];
04544 int m3[2][4] = {{0,0,0,0},{0,0,0,0}};
04545 int m4[2][4] = {{0,0,0,0},{0,0,0,0}};
04546 int qp_per_uv_dc = p_Img->qp_per_matrix[ (currMB->qpc[uv] + 3 + p_Img->bitdepth_chroma_qp_scale) ];
04547 int qp_rem_uv_dc = p_Img->qp_rem_matrix[ (currMB->qpc[uv] + 3 + p_Img->bitdepth_chroma_qp_scale) ];
04548 if (intra)
04549 InvLevelScale4x4 = currSlice->InvLevelScale4x4_Intra[uv + 1][qp_rem_uv_dc];
04550 else
04551 InvLevelScale4x4 = currSlice->InvLevelScale4x4_Inter[uv + 1][qp_rem_uv_dc];
04552
04553
04554
04555 readCoeff4x4_CAVLC(currMB, CHROMA_DC, 0, 0, levarr, runarr, &numcoeff);
04556 coef_ctr=-1;
04557 level=1;
04558 for(k = 0; k < numcoeff; ++k)
04559 {
04560 if (levarr[k] != 0)
04561 {
04562 currMB->cbp_blk[0] |= ((int64)0xff0000) << (ll<<2);
04563 coef_ctr += runarr[k]+1;
04564 i0 = SCAN_YUV422[coef_ctr][0];
04565 j0 = SCAN_YUV422[coef_ctr][1];
04566
04567 m3[i0][j0]=levarr[k];
04568 }
04569 }
04570
04571
04572
04573 if(currMB->is_lossless == FALSE)
04574 {
04575 m4[0][0] = m3[0][0] + m3[1][0];
04576 m4[0][1] = m3[0][1] + m3[1][1];
04577 m4[0][2] = m3[0][2] + m3[1][2];
04578 m4[0][3] = m3[0][3] + m3[1][3];
04579
04580 m4[1][0] = m3[0][0] - m3[1][0];
04581 m4[1][1] = m3[0][1] - m3[1][1];
04582 m4[1][2] = m3[0][2] - m3[1][2];
04583 m4[1][3] = m3[0][3] - m3[1][3];
04584
04585 for (i = 0; i < 2; ++i)
04586 {
04587 m6[0] = m4[i][0] + m4[i][2];
04588 m6[1] = m4[i][0] - m4[i][2];
04589 m6[2] = m4[i][1] - m4[i][3];
04590 m6[3] = m4[i][1] + m4[i][3];
04591
04592 imgcof[ 0][i<<2] = m6[0] + m6[3];
04593 imgcof[ 4][i<<2] = m6[1] + m6[2];
04594 imgcof[ 8][i<<2] = m6[1] - m6[2];
04595 imgcof[12][i<<2] = m6[0] - m6[3];
04596 }
04597 }
04598 else
04599 {
04600 for(j=0;j<4;++j)
04601 {
04602 for(i=0;i<2;++i)
04603 {
04604 currSlice->cof[uv + 1][j<<2][i<<2] = m3[i][j];
04605
04606 }
04607 }
04608 }
04609
04610 for(j = 0;j < p_Img->mb_cr_size_y; j += BLOCK_SIZE)
04611 {
04612 for(i=0;i < p_Img->mb_cr_size_x;i+=BLOCK_SIZE)
04613 {
04614 imgcof[j][i] = rshift_rnd_sf((imgcof[j][i] * InvLevelScale4x4[0][0]) << qp_per_uv_dc, 6);
04615 }
04616 }
04617 }
04618 }
04619 }
04620 }
04621
04622
04623
04624
04625 if (cbp<=31)
04626 {
04627 memset(&p_Img->nz_coeff [mb_nr ][1][0][0], 0, 2 * BLOCK_SIZE * BLOCK_SIZE * sizeof(byte));
04628 }
04629 else
04630 {
04631 if(currMB->is_lossless == FALSE)
04632 {
04633 for (b8=0; b8 < p_Img->num_blk8x8_uv; ++b8)
04634 {
04635 currMB->is_v_block = uv = (b8 > ((p_Img->num_uv_blocks) - 1 ));
04636 InvLevelScale4x4 = intra ? currSlice->InvLevelScale4x4_Intra[uv + 1][qp_rem_uv[uv]] : currSlice->InvLevelScale4x4_Inter[uv + 1][qp_rem_uv[uv]];
04637
04638 for (b4=0; b4 < 4; ++b4)
04639 {
04640 i = cofuv_blk_x[yuv][b8][b4];
04641 j = cofuv_blk_y[yuv][b8][b4];
04642
04643 readCoeff4x4_CAVLC(currMB, CHROMA_AC, i + 2*uv, j + 4, levarr, runarr, &numcoeff);
04644 coef_ctr = 0;
04645
04646 for(k = 0; k < numcoeff;++k)
04647 {
04648 if (levarr[k] != 0)
04649 {
04650 currMB->cbp_blk[0] |= ((int64)1) << cbp_blk_chroma[b8][b4];
04651 coef_ctr += runarr[k] + 1;
04652
04653 i0=pos_scan4x4[coef_ctr][0];
04654 j0=pos_scan4x4[coef_ctr][1];
04655
04656 currSlice->cof[uv + 1][(j<<2) + j0][(i<<2) + i0] = rshift_rnd_sf((levarr[k] * InvLevelScale4x4[j0][i0])<<qp_per_uv[uv], 4);
04657
04658 }
04659 }
04660 }
04661 }
04662 }
04663 else
04664 {
04665 currMB->is_intra_block = IS_INTRA(currMB);
04666 currSE.context = CHROMA_AC;
04667 currSE.type = (currMB->is_intra_block ? SE_CHR_AC_INTRA : SE_CHR_AC_INTER);
04668
04669 dP = &(currSlice->partArr[partMap[currSE.type]]);
04670
04671 if (dP->bitstream->ei_flag)
04672 currSE.mapping = linfo_levrun_inter;
04673 else
04674 currSE.reading = readRunLevel_CABAC;
04675
04676 if(currMB->is_lossless == FALSE)
04677 {
04678 for (b8=0; b8 < p_Img->num_blk8x8_uv; ++b8)
04679 {
04680 currMB->is_v_block = uv = (b8 > ((p_Img->num_uv_blocks) - 1 ));
04681 InvLevelScale4x4 = intra ? currSlice->InvLevelScale4x4_Intra[uv + 1][qp_rem_uv[uv]] : currSlice->InvLevelScale4x4_Inter[uv + 1][qp_rem_uv[uv]];
04682
04683 for (b4 = 0; b4 < 4; ++b4)
04684 {
04685 i = cofuv_blk_x[yuv][b8][b4];
04686 j = cofuv_blk_y[yuv][b8][b4];
04687
04688 currMB->subblock_y = subblk_offset_y[yuv][b8][b4];
04689 currMB->subblock_x = subblk_offset_x[yuv][b8][b4];
04690
04691 pos_scan_4x4 = pos_scan4x4[1];
04692 level=1;
04693
04694 for(k = 0; (k < 16) && (level != 0);++k)
04695 {
04696 #if TRACE
04697 snprintf(currSE.tracestring, TRACESTRING_SIZE, "AC Chroma ");
04698 #endif
04699
04700 dP->readSyntaxElement(&currSE,p_Img,dP);
04701 level = currSE.value1;
04702
04703 if (level != 0)
04704 {
04705 currMB->cbp_blk[0] |= ((int64)1) << cbp_blk_chroma[b8][b4];
04706 pos_scan_4x4 += (currSE.value2 << 1);
04707
04708 i0 = *pos_scan_4x4++;
04709 j0 = *pos_scan_4x4++;
04710
04711 currSlice->cof[uv + 1][(j<<2) + j0][(i<<2) + i0] = rshift_rnd_sf((level * InvLevelScale4x4[j0][i0])<<qp_per_uv[uv], 4);
04712
04713 }
04714 }
04715 }
04716 }
04717 }
04718 else
04719 {
04720 for (b8=0; b8 < p_Img->num_blk8x8_uv; ++b8)
04721 {
04722 currMB->is_v_block = uv = (b8 > ((p_Img->num_uv_blocks) - 1 ));
04723
04724 for (b4=0; b4 < 4; ++b4)
04725 {
04726 i = cofuv_blk_x[yuv][b8][b4];
04727 j = cofuv_blk_y[yuv][b8][b4];
04728
04729 pos_scan_4x4 = pos_scan4x4[1];
04730 level=1;
04731
04732 currMB->subblock_y = subblk_offset_y[yuv][b8][b4];
04733 currMB->subblock_x = subblk_offset_x[yuv][b8][b4];
04734
04735 for(k=0;(k<16)&&(level!=0);++k)
04736 {
04737 #if TRACE
04738 snprintf(currSE.tracestring, TRACESTRING_SIZE, "AC Chroma ");
04739 #endif
04740 dP->readSyntaxElement(&currSE,p_Img,dP);
04741 level = currSE.value1;
04742
04743 if (level != 0)
04744 {
04745 currMB->cbp_blk[0] |= ((int64)1) << cbp_blk_chroma[b8][b4];
04746 pos_scan_4x4 += (currSE.value2 << 1);
04747
04748 i0 = *pos_scan_4x4++;
04749 j0 = *pos_scan_4x4++;
04750
04751 currSlice->cof[uv + 1][(j<<2) + j0][(i<<2) + i0] = level;
04752
04753 }
04754 }
04755 }
04756 }
04757 }
04758 }
04759 }
04760 }
04761 }
04762
04774 void decode_ipcm_mb(Macroblock *currMB)
04775 {
04776 int i, j, k;
04777 Slice *currSlice = currMB->p_Slice;
04778 ImageParameters *p_Img = currMB->p_Img;
04779 StorablePicture *dec_picture = p_Img->dec_picture;
04780 int mb_nr = currMB->mbAddrX;
04781
04782
04783
04784
04785 for(i = 0; i < MB_BLOCK_SIZE; ++i)
04786 {
04787 for(j = 0;j < MB_BLOCK_SIZE ; ++j)
04788 {
04789 dec_picture->imgY[currMB->pix_y + i][currMB->pix_x + j] = (imgpel) currSlice->cof[0][i][j];
04790 }
04791 }
04792
04793 if ((dec_picture->chroma_format_idc != YUV400) && !IS_INDEPENDENT(p_Img))
04794 {
04795 for (k = 0; k < 2; ++k)
04796 {
04797 for(i = 0; i < p_Img->mb_cr_size_y; ++i)
04798 {
04799 for(j = 0;j < p_Img->mb_cr_size_x; ++j)
04800 {
04801 dec_picture->imgUV[k][currMB->pix_c_y+i][currMB->pix_c_x + j] = (imgpel) currSlice->cof[k + 1][i][j];
04802 }
04803 }
04804 }
04805 }
04806
04807
04808 update_qp(currMB, 0);
04809
04810
04811
04812 memset(&p_Img->nz_coeff[mb_nr][0][0][0], 16, 3 * BLOCK_SIZE * BLOCK_SIZE * sizeof(byte));
04813
04814
04815 currMB->skip_flag = 0;
04816
04817
04818 currMB->cbp_blk[0] = 0xFFFF;
04819
04820
04821 currSlice->last_dquant = 0;
04822 }
04823
04831 static int decode_one_component_i_slice(Macroblock *currMB, ColorPlane curr_plane, imgpel **currImg, StorablePicture *dec_picture, MotionParams *colocated, int list_offset)
04832 {
04833 Slice *currSlice = currMB->p_Slice;
04834 ImageParameters *p_Img = currMB->p_Img;
04835 int i=0, j=0,k, j4=0,i4=0;
04836 int ioff,joff;
04837 int block8x8;
04838 int j_pos, i_pos;
04839
04840 static const byte decode_block_scan[16] = {0,1,4,5,2,3,6,7,8,9,12,13,10,11,14,15};
04841
04842 int smb = ((currSlice->slice_type==SP_SLICE) && currMB->mb_type!=I16MB) || (currSlice->slice_type == SI_SLICE && currMB->mb_type == SI4MB);
04843
04844 int yuv = dec_picture->chroma_format_idc - 1;
04845
04846
04847 p_Img->ipmode_DPCM = NO_INTRA_PMODE;
04848
04849 if (IS_I16MB (currMB))
04850 {
04851 intrapred16x16(currMB, curr_plane, currMB->i16mode);
04852 p_Img->ipmode_DPCM = currMB->i16mode;
04853
04854
04855 iMBtrans4x4(currMB, curr_plane, smb);
04856
04857
04858 if ((dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444))
04859 {
04860 intra_cr_decoding(currMB, yuv, smb);
04861 }
04862 }
04863 else if (currMB->mb_type == I4MB)
04864 {
04865 currMB->itrans_4x4 = (currMB->is_lossless == FALSE) ? itrans4x4 : Inv_Residual_trans_4x4;
04866
04867 for (block8x8 = 0; block8x8 < 4; block8x8++)
04868 {
04869 for (k = block8x8 * 4; k < block8x8 * 4 + 4; k ++)
04870 {
04871 i = (decode_block_scan[k] & 3);
04872 j = ((decode_block_scan[k] >> 2) & 3);
04873
04874 ioff = (i << 2);
04875 joff = (j << 2);
04876 i4 = currMB->block_x + i;
04877 j4 = currMB->block_y + j;
04878 j_pos = j4 * BLOCK_SIZE;
04879 i_pos = i4 * BLOCK_SIZE;
04880
04881
04882
04883 if (intrapred(currMB, curr_plane, ioff,joff,i4,j4) == SEARCH_SYNC)
04884 return SEARCH_SYNC;
04885
04886
04887 currMB->itrans_4x4 (currMB, curr_plane, ioff, joff);
04888
04889 copy_image_data_4x4(&currImg[j_pos], &currSlice->mb_rec[curr_plane][joff], i_pos, ioff);
04890 }
04891 }
04892
04893
04894 if ((dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444))
04895 {
04896 intra_cr_decoding(currMB, yuv, smb);
04897 }
04898 }
04899 else if (currMB->mb_type == I8MB)
04900 {
04901 currMB->itrans_8x8 = (currMB->is_lossless == FALSE) ? itrans8x8 : Inv_Residual_trans_8x8;
04902
04903 for (block8x8 = 0; block8x8 < 4; block8x8++)
04904 {
04905
04906 ioff = (block8x8 & 0x01) << 3;
04907 joff = (block8x8 >> 1 ) << 3;
04908
04909
04910 intrapred8x8(currMB, curr_plane, ioff, joff);
04911 currMB->itrans_8x8 (currMB, curr_plane, ioff,joff);
04912
04913 copy_image_data_8x8(&currImg[currMB->pix_y + joff], &currSlice->mb_rec[curr_plane][joff], currMB->pix_x + ioff, ioff);
04914 }
04915
04916 if ((dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444))
04917 {
04918 intra_cr_decoding(currMB, yuv, smb);
04919 }
04920 }
04921
04922 return 1;
04923 }
04924
04933 static int decode_one_component_p_slice(Macroblock *currMB, ColorPlane curr_plane, imgpel **currImg, StorablePicture *dec_picture, MotionParams *colocated, int list_offset)
04934 {
04935 Slice *currSlice = currMB->p_Slice;
04936 ImageParameters *p_Img = currMB->p_Img;
04937 int i=0, j=0,k, j4=0,i4=0;
04938 int ioff,joff;
04939 int block8x8;
04940 int j_pos, i_pos;
04941
04942 static const byte decode_block_scan[16] = {0,1,4,5,2,3,6,7,8,9,12,13,10,11,14,15};
04943
04944 int mv_mode, pred_dir;
04945
04946 int block_size_x, block_size_y;
04947
04948 int smb = ((currSlice->slice_type==SP_SLICE) && currMB->mb_type!=I16MB) || (currSlice->slice_type == SI_SLICE && currMB->mb_type == SI4MB);
04949
04950
04951 int curr_mb_field = ((currSlice->MbaffFrameFlag)&&(currMB->mb_field));
04952
04953 int need_4x4_transform = (!currMB->luma_transform_size_8x8_flag);
04954 int yuv = dec_picture->chroma_format_idc - 1;
04955
04956
04957 p_Img->ipmode_DPCM = NO_INTRA_PMODE;
04958
04959 if (IS_I16MB (currMB))
04960 {
04961 intrapred16x16(currMB, curr_plane, currMB->i16mode);
04962 p_Img->ipmode_DPCM = currMB->i16mode;
04963
04964
04965 iMBtrans4x4(currMB, curr_plane, smb);
04966
04967
04968 if ((dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444))
04969 {
04970 intra_cr_decoding(currMB, yuv, smb);
04971 }
04972 }
04973 else if (currMB->mb_type == I4MB)
04974 {
04975 currMB->itrans_4x4 = (currMB->is_lossless == FALSE) ? itrans4x4 : Inv_Residual_trans_4x4;
04976
04977 for (block8x8 = 0; block8x8 < 4; block8x8++)
04978 {
04979 for (k = block8x8 * 4; k < block8x8 * 4 + 4; k ++)
04980 {
04981 i = (decode_block_scan[k] & 3);
04982 j = ((decode_block_scan[k] >> 2) & 3);
04983
04984 ioff = (i << 2);
04985 joff = (j << 2);
04986 i4 = currMB->block_x + i;
04987 j4 = currMB->block_y + j;
04988 j_pos = j4 * BLOCK_SIZE;
04989 i_pos = i4 * BLOCK_SIZE;
04990
04991
04992
04993 if (intrapred(currMB, curr_plane, ioff,joff,i4,j4) == SEARCH_SYNC)
04994 return SEARCH_SYNC;
04995
04996
04997 currMB->itrans_4x4 (currMB, curr_plane, ioff, joff);
04998
04999 copy_image_data_4x4(&currImg[j_pos], &currSlice->mb_rec[curr_plane][joff], i_pos, ioff);
05000 }
05001 }
05002
05003
05004 if ((dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444))
05005 {
05006 intra_cr_decoding(currMB, yuv, smb);
05007 }
05008 }
05009 else if (currMB->mb_type == I8MB)
05010 {
05011 currMB->itrans_8x8 = (currMB->is_lossless == FALSE) ? itrans8x8 : Inv_Residual_trans_8x8;
05012
05013 for (block8x8 = 0; block8x8 < 4; block8x8++)
05014 {
05015
05016 ioff = (block8x8 & 0x01) << 3;
05017 joff = (block8x8 >> 1 ) << 3;
05018
05019
05020 intrapred8x8(currMB, curr_plane, ioff, joff);
05021 currMB->itrans_8x8 (currMB, curr_plane, ioff,joff);
05022
05023 copy_image_data_8x8(&currImg[currMB->pix_y + joff], &currSlice->mb_rec[curr_plane][joff], currMB->pix_x + ioff, ioff);
05024 }
05025
05026 if ((dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444))
05027 {
05028 intra_cr_decoding(currMB, yuv, smb);
05029 }
05030 }
05031 else if (currMB->mb_type == PSKIP)
05032 {
05033 imgpel **curComp;
05034 imgpel **mpr;
05035
05036 perform_mc(currMB, curr_plane, dec_picture, LIST_0, 0, 0, list_offset, MB_BLOCK_SIZE, MB_BLOCK_SIZE, curr_mb_field);
05037
05038 mpr = currSlice->mb_pred[curr_plane];
05039 curComp = &currImg[currMB->pix_y];
05040
05041 copy_image_data_16x16(&currImg[currMB->pix_y], currSlice->mb_pred[curr_plane], currMB->pix_x, 0);
05042
05043 if ((dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444))
05044 {
05045 copy_image_data(&dec_picture->imgUV[0][currMB->pix_c_y], currSlice->mb_pred[1], currMB->pix_c_x, 0, p_Img->mb_size[1][0], p_Img->mb_size[1][1]);
05046 copy_image_data(&dec_picture->imgUV[1][currMB->pix_c_y], currSlice->mb_pred[2], currMB->pix_c_x, 0, p_Img->mb_size[1][0], p_Img->mb_size[1][1]);
05047 }
05048 }
05049 else if (currMB->mb_type == P16x16)
05050 {
05051 perform_mc(currMB, curr_plane, dec_picture, currMB->b8pdir[0], 0, 0, list_offset, MB_BLOCK_SIZE, MB_BLOCK_SIZE, curr_mb_field);
05052 iTransform(currMB, curr_plane, need_4x4_transform, smb);
05053 }
05054 else if (currMB->mb_type == P16x8)
05055 {
05056 perform_mc(currMB, curr_plane, dec_picture, currMB->b8pdir[0], 0, 0, list_offset, MB_BLOCK_SIZE, BLOCK_SIZE_8x8, curr_mb_field);
05057 perform_mc(currMB, curr_plane, dec_picture, currMB->b8pdir[2], 0, 2, list_offset, MB_BLOCK_SIZE, BLOCK_SIZE_8x8, curr_mb_field);
05058 iTransform(currMB, curr_plane, need_4x4_transform, smb);
05059 }
05060 else if (currMB->mb_type == P8x16)
05061 {
05062 perform_mc(currMB, curr_plane, dec_picture, currMB->b8pdir[0], 0, 0, list_offset, BLOCK_SIZE_8x8, MB_BLOCK_SIZE, curr_mb_field);
05063 perform_mc(currMB, curr_plane, dec_picture, currMB->b8pdir[1], 2, 0, list_offset, BLOCK_SIZE_8x8, MB_BLOCK_SIZE, curr_mb_field);
05064 iTransform(currMB, curr_plane, need_4x4_transform, smb);
05065 }
05066 else
05067 {
05068 for (block8x8=0; block8x8<4; block8x8++)
05069 {
05070 mv_mode = currMB->b8mode[block8x8];
05071 pred_dir = currMB->b8pdir[block8x8];
05072
05073
05074 if ( mv_mode != 0 )
05075 {
05076 int k_start = (block8x8 << 2);
05077 int k_inc = (mv_mode == SMB8x4) ? 2 : 1;
05078 int k_end = (mv_mode == SMB8x8) ? k_start + 1 : ((mv_mode == SMB4x4) ? k_start + 4 : k_start + k_inc + 1);
05079
05080 block_size_x = ( mv_mode == SMB8x4 || mv_mode == SMB8x8 ) ? SMB_BLOCK_SIZE : BLOCK_SIZE;
05081 block_size_y = ( mv_mode == SMB4x8 || mv_mode == SMB8x8 ) ? SMB_BLOCK_SIZE : BLOCK_SIZE;
05082
05083 for (k = k_start; k < k_end; k += k_inc)
05084 {
05085 i = (decode_block_scan[k] & 3);
05086 j = ((decode_block_scan[k] >> 2) & 3);
05087 perform_mc(currMB, curr_plane, dec_picture, pred_dir, i, j, list_offset, block_size_x, block_size_y, curr_mb_field);
05088 }
05089 }
05090 }
05091
05092 iTransform(currMB, curr_plane, need_4x4_transform, smb);
05093 }
05094 return 1;
05095 }
05096
05104 static int decode_one_component_b_slice(Macroblock *currMB, ColorPlane curr_plane, imgpel **currImg, StorablePicture *dec_picture, MotionParams *colocated, int list_offset)
05105 {
05106 Slice *currSlice = currMB->p_Slice;
05107 ImageParameters *p_Img = currMB->p_Img;
05108
05109 int i=0, j=0,k, j4=0,i4=0, j6;
05110 int refList;
05111 int ioff,joff;
05112 int block8x8;
05113 int j_pos, i_pos;
05114 PicMotionParams *motion = &dec_picture->motion;
05115
05116 static const byte decode_block_scan[16] = {0,1,4,5,2,3,6,7,8,9,12,13,10,11,14,15};
05117
05118 short ref_idx;
05119 int mv_mode, pred_dir;
05120
05121 int block_size_x, block_size_y;
05122
05123 int mv_scale;
05124
05125 int smb = ((currSlice->slice_type==SP_SLICE) && currMB->mb_type!=I16MB) || (currSlice->slice_type == SI_SLICE && currMB->mb_type == SI4MB);
05126
05127 char l0_rFrame = -1, l1_rFrame = -1;
05128
05129 short pmvl0[2]={0,0}, pmvl1[2]={0,0};
05130
05131 int direct_pdir=-1;
05132
05133 int curr_mb_field = ((currSlice->MbaffFrameFlag)&&(currMB->mb_field));
05134
05135 int need_4x4_transform = (!currMB->luma_transform_size_8x8_flag);
05136 int yuv = dec_picture->chroma_format_idc - 1;
05137
05138
05139 p_Img->ipmode_DPCM = NO_INTRA_PMODE;
05140
05141 if (IS_I16MB (currMB))
05142 {
05143 intrapred16x16(currMB, curr_plane, currMB->i16mode);
05144 p_Img->ipmode_DPCM = currMB->i16mode;
05145
05146
05147 iMBtrans4x4(currMB, curr_plane, smb);
05148
05149
05150 if ((dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444))
05151 {
05152 intra_cr_decoding(currMB, yuv, smb);
05153 }
05154 }
05155 else if (currMB->mb_type == I4MB)
05156 {
05157 currMB->itrans_4x4 = (currMB->is_lossless == FALSE) ? itrans4x4 : Inv_Residual_trans_4x4;
05158
05159 for (block8x8 = 0; block8x8 < 4; block8x8++)
05160 {
05161 for (k = block8x8 * 4; k < block8x8 * 4 + 4; k ++)
05162 {
05163 i = (decode_block_scan[k] & 3);
05164 j = ((decode_block_scan[k] >> 2) & 3);
05165
05166 ioff = (i << 2);
05167 joff = (j << 2);
05168 i4 = currMB->block_x + i;
05169 j4 = currMB->block_y + j;
05170 j_pos = j4 * BLOCK_SIZE;
05171 i_pos = i4 * BLOCK_SIZE;
05172
05173
05174
05175 if (intrapred(currMB, curr_plane, ioff,joff,i4,j4) == SEARCH_SYNC)
05176 return SEARCH_SYNC;
05177
05178
05179 currMB->itrans_4x4 (currMB, curr_plane, ioff, joff);
05180
05181 copy_image_data_4x4(&currImg[j_pos], &currSlice->mb_rec[curr_plane][joff], i_pos, ioff);
05182 }
05183 }
05184
05185
05186 if ((dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444))
05187 {
05188 intra_cr_decoding(currMB, yuv, smb);
05189 }
05190 }
05191 else if (currMB->mb_type == I8MB)
05192 {
05193 currMB->itrans_8x8 = (currMB->is_lossless == FALSE) ? itrans8x8 : Inv_Residual_trans_8x8;
05194
05195 for (block8x8 = 0; block8x8 < 4; block8x8++)
05196 {
05197
05198 ioff = (block8x8 & 0x01) << 3;
05199 joff = (block8x8 >> 1 ) << 3;
05200
05201
05202 intrapred8x8(currMB, curr_plane, ioff, joff);
05203 currMB->itrans_8x8 (currMB, curr_plane, ioff,joff);
05204
05205 copy_image_data_8x8(&currImg[currMB->pix_y + joff], &currSlice->mb_rec[curr_plane][joff], currMB->pix_x + ioff, ioff);
05206 }
05207
05208 if ((dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444))
05209 {
05210 intra_cr_decoding(currMB, yuv, smb);
05211 }
05212 }
05213 else if (currMB->mb_type == P16x16)
05214 {
05215 perform_mc(currMB, curr_plane, dec_picture, currMB->b8pdir[0], 0, 0, list_offset, MB_BLOCK_SIZE, MB_BLOCK_SIZE, curr_mb_field);
05216 iTransform(currMB, curr_plane, need_4x4_transform, smb);
05217 }
05218 else if (currMB->mb_type == P16x8)
05219 {
05220 perform_mc(currMB, curr_plane, dec_picture, currMB->b8pdir[0], 0, 0, list_offset, MB_BLOCK_SIZE, BLOCK_SIZE_8x8, curr_mb_field);
05221 perform_mc(currMB, curr_plane, dec_picture, currMB->b8pdir[2], 0, 2, list_offset, MB_BLOCK_SIZE, BLOCK_SIZE_8x8, curr_mb_field);
05222 iTransform(currMB, curr_plane, need_4x4_transform, smb);
05223 }
05224 else if (currMB->mb_type == P8x16)
05225 {
05226 perform_mc(currMB, curr_plane, dec_picture, currMB->b8pdir[0], 0, 0, list_offset, BLOCK_SIZE_8x8, MB_BLOCK_SIZE, curr_mb_field);
05227 perform_mc(currMB, curr_plane, dec_picture, currMB->b8pdir[1], 2, 0, list_offset, BLOCK_SIZE_8x8, MB_BLOCK_SIZE, curr_mb_field);
05228 iTransform(currMB, curr_plane, need_4x4_transform, smb);
05229 }
05230 else
05231 {
05232
05233 if (currSlice->slice_type==B_SLICE && currSlice->direct_spatial_mv_pred_flag && (IS_DIRECT (currMB) ||
05234 ((currMB->mb_type == P8x8) && !(currMB->b8mode[0] && currMB->b8mode[1] && currMB->b8mode[2] && currMB->b8mode[3]))))
05235 prepare_direct_params(currMB, dec_picture, pmvl0, pmvl1, &l0_rFrame, &l1_rFrame);
05236
05237 for (block8x8=0; block8x8<4; block8x8++)
05238 {
05239 mv_mode = currMB->b8mode[block8x8];
05240 pred_dir = currMB->b8pdir[block8x8];
05241
05242
05243 if ( mv_mode != 0 )
05244 {
05245 int k_start = (block8x8 << 2);
05246 int k_inc = (mv_mode == SMB8x4) ? 2 : 1;
05247 int k_end = (mv_mode == SMB8x8) ? k_start + 1 : ((mv_mode == SMB4x4) ? k_start + 4 : k_start + k_inc + 1);
05248
05249 block_size_x = ( mv_mode == SMB8x4 || mv_mode == SMB8x8 ) ? SMB_BLOCK_SIZE : BLOCK_SIZE;
05250 block_size_y = ( mv_mode == SMB4x8 || mv_mode == SMB8x8 ) ? SMB_BLOCK_SIZE : BLOCK_SIZE;
05251
05252 for (k = k_start; k < k_end; k += k_inc)
05253 {
05254 i = (decode_block_scan[k] & 3);
05255 j = ((decode_block_scan[k] >> 2) & 3);
05256 perform_mc(currMB, curr_plane, dec_picture, pred_dir, i, j, list_offset, block_size_x, block_size_y, curr_mb_field);
05257 }
05258 }
05259 else
05260 {
05261 int k_start = (block8x8 << 2);
05262 int k_end = k_start;
05263
05264 if (p_Img->active_sps->direct_8x8_inference_flag)
05265 {
05266 block_size_x = SMB_BLOCK_SIZE;
05267 block_size_y = SMB_BLOCK_SIZE;
05268 k_end ++;
05269 }
05270 else
05271 {
05272 block_size_x = BLOCK_SIZE;
05273 block_size_y = BLOCK_SIZE;
05274 k_end += BLOCK_MULTIPLE;
05275 }
05276
05277
05278 if (currSlice->direct_spatial_mv_pred_flag)
05279 {
05280 for (k = k_start; k < k_start + BLOCK_MULTIPLE; k ++)
05281 {
05282
05283 i = (decode_block_scan[k] & 3);
05284 j = ((decode_block_scan[k] >> 2) & 3);
05285
05286 ioff = (i << 2);
05287 i4 = currMB->block_x + i;
05288
05289 joff = (j << 2);
05290 j4 = currMB->block_y + j;
05291
05292 assert (pred_dir<=2);
05293
05294 j6 = currMB->block_y_aff + j;
05295
05296
05297
05298 if (l0_rFrame >=0)
05299 {
05300 if (!l0_rFrame && ((!colocated->moving_block[j6][i4]) && (!p_Img->listX[LIST_1 + list_offset][0]->is_long_term)))
05301 {
05302 motion->mv [LIST_0][j4][i4][0] = 0;
05303 motion->mv [LIST_0][j4][i4][1] = 0;
05304 motion->ref_idx[LIST_0][j4][i4] = 0;
05305 }
05306 else
05307 {
05308 motion->mv [LIST_0][j4][i4][0] = pmvl0[0];
05309 motion->mv [LIST_0][j4][i4][1] = pmvl0[1];
05310 motion->ref_idx[LIST_0][j4][i4] = l0_rFrame;
05311 }
05312 }
05313 else
05314 {
05315 motion->ref_idx[LIST_0][j4][i4] = -1;
05316 motion->mv [LIST_0][j4][i4][0] = 0;
05317 motion->mv [LIST_0][j4][i4][1] = 0;
05318 }
05319
05320 if (l1_rFrame >=0)
05321 {
05322 if (l1_rFrame==0 && ((!colocated->moving_block[j6][i4]) && (!p_Img->listX[LIST_1 + list_offset][0]->is_long_term)))
05323 {
05324
05325 motion->mv [LIST_1][j4][i4][0] = 0;
05326 motion->mv [LIST_1][j4][i4][1] = 0;
05327 motion->ref_idx[LIST_1][j4][i4] = l1_rFrame;
05328 }
05329 else
05330 {
05331 motion->mv [LIST_1][j4][i4][0] = pmvl1[0];
05332 motion->mv [LIST_1][j4][i4][1] = pmvl1[1];
05333 motion->ref_idx[LIST_1][j4][i4] = l1_rFrame;
05334 }
05335 }
05336 else
05337 {
05338 motion->mv [LIST_1][j4][i4][0] = 0;
05339 motion->mv [LIST_1][j4][i4][1] = 0;
05340 motion->ref_idx[LIST_1][j4][i4] = -1;
05341 }
05342
05343 if (l0_rFrame < 0 && l1_rFrame < 0)
05344 {
05345 motion->ref_idx[LIST_0][j4][i4] = 0;
05346 motion->ref_idx[LIST_1][j4][i4] = 0;
05347 }
05348
05349 if (motion->ref_idx[LIST_1][j4][i4]==-1)
05350 {
05351 direct_pdir = 0;
05352 ref_idx = (motion->ref_idx[LIST_0][j4][i4] != -1) ? motion->ref_idx[LIST_0][j4][i4] : 0;
05353 }
05354 else if (motion->ref_idx[LIST_0][j4][i4]==-1)
05355 {
05356 direct_pdir = 1;
05357 ref_idx = (motion->ref_idx[LIST_1][j4][i4] != -1) ? motion->ref_idx[LIST_1][j4][i4] : 0;
05358 }
05359 else
05360 direct_pdir = 2;
05361
05362 pred_dir = direct_pdir;
05363
05364 motion->ref_pic_id[LIST_0][j4][i4] = dec_picture->ref_pic_num[p_Img->current_slice_nr][LIST_0 + list_offset][(short)motion->ref_idx[LIST_0][j4][i4]];
05365 motion->ref_pic_id[LIST_1][j4][i4] = dec_picture->ref_pic_num[p_Img->current_slice_nr][LIST_1 + list_offset][(short)motion->ref_idx[LIST_1][j4][i4]];
05366 }
05367 }
05368 else
05369 {
05370 for (k = k_start; k < k_start + BLOCK_MULTIPLE; k ++)
05371 {
05372
05373 i = (decode_block_scan[k] & 3);
05374 j = ((decode_block_scan[k] >> 2) & 3);
05375
05376 ioff = (i << 2);
05377 i4 = currMB->block_x + i;
05378
05379 joff = (j << 2);
05380 j4 = currMB->block_y + j;
05381
05382 assert (pred_dir<=2);
05383
05384 j6 = currMB->block_y_aff + j;
05385
05386 refList = (colocated->ref_idx[LIST_0][j6][i4]== -1 ? LIST_1 : LIST_0);
05387 ref_idx = colocated->ref_idx[refList][j6][i4];
05388
05389 if(ref_idx==-1)
05390 {
05391 memset( &motion->mv [LIST_0][j4][i4][0], 0, 2* sizeof(short));
05392 memset( &motion->mv [LIST_1][j4][i4][0], 0, 2* sizeof(short));
05393
05394 motion->ref_idx[LIST_0][j4][i4] = 0;
05395 motion->ref_idx[LIST_1][j4][i4] = 0;
05396 }
05397 else
05398 {
05399 int mapped_idx=0;
05400 int iref;
05401
05402 for (iref=0;iref<imin(currSlice->num_ref_idx_l0_active,p_Img->listXsize[LIST_0 + list_offset]);iref++)
05403 {
05404 if(p_Img->structure==0 && curr_mb_field==0)
05405 {
05406
05407
05408
05409 if(p_Img->listX[0][iref]->top_poc*2 == colocated->ref_pic_id[refList][j6][i4] || p_Img->listX[0][iref]->bottom_poc*2 == colocated->ref_pic_id[refList][j6][i4])
05410 {
05411 mapped_idx=iref;
05412 break;
05413 }
05414 else
05415 mapped_idx=INVALIDINDEX;
05416 continue;
05417 }
05418
05419 if (dec_picture->ref_pic_num[p_Img->current_slice_nr][LIST_0 + list_offset][iref]==colocated->ref_pic_id[refList][j6][i4])
05420 {
05421 mapped_idx=iref;
05422 break;
05423 }
05424 else
05425 {
05426 mapped_idx=INVALIDINDEX;
05427 }
05428 }
05429 if (INVALIDINDEX == mapped_idx)
05430 {
05431 error("temporal direct error: colocated block has ref that is unavailable",-1111);
05432 }
05433
05434 mv_scale = currSlice->mvscale[LIST_0 + list_offset][mapped_idx];
05435
05437 if (mv_scale == 9999 || p_Img->listX[LIST_0+list_offset][mapped_idx]->is_long_term)
05438 {
05439 memcpy(&motion->mv [LIST_0][j4][i4][0], &colocated->mv[refList][j6][i4][0], 2 * sizeof(short));
05440 memset(&motion->mv [LIST_1][j4][i4][0], 0, 2 * sizeof(short));
05441 }
05442 else
05443 {
05444 motion->mv [LIST_0][j4][i4][0]=(mv_scale * colocated->mv[refList][j6][i4][0] + 128 ) >> 8;
05445 motion->mv [LIST_0][j4][i4][1]=(mv_scale * colocated->mv[refList][j6][i4][1] + 128 ) >> 8;
05446
05447 motion->mv [LIST_1][j4][i4][0]=motion->mv[LIST_0][j4][i4][0] - colocated->mv[refList][j6][i4][0] ;
05448 motion->mv [LIST_1][j4][i4][1]=motion->mv[LIST_0][j4][i4][1] - colocated->mv[refList][j6][i4][1] ;
05449 }
05450
05451 motion->ref_idx[LIST_0][j4][i4] = (char) mapped_idx;
05452 motion->ref_idx[LIST_1][j4][i4] = 0;
05453 }
05454
05455 motion->ref_pic_id[LIST_0][j4][i4] = dec_picture->ref_pic_num[p_Img->current_slice_nr][LIST_0 + list_offset][(short)motion->ref_idx[LIST_0][j4][i4]];
05456 motion->ref_pic_id[LIST_1][j4][i4] = dec_picture->ref_pic_num[p_Img->current_slice_nr][LIST_1 + list_offset][(short)motion->ref_idx[LIST_1][j4][i4]];
05457 }
05458 }
05459
05460 for (k = k_start; k < k_end; k ++)
05461 {
05462 i = (decode_block_scan[k] & 3);
05463 j = ((decode_block_scan[k] >> 2) & 3);
05464 perform_mc(currMB, curr_plane, dec_picture, pred_dir, i, j, list_offset, block_size_x, block_size_y, curr_mb_field);
05465 }
05466 }
05467 }
05468
05469 iTransform(currMB, curr_plane, need_4x4_transform, smb);
05470 }
05471 return 1;
05472 }
05473
05481 int decode_one_macroblock(Macroblock *currMB, StorablePicture *dec_picture)
05482 {
05483 Slice *currSlice = currMB->p_Slice;
05484 ImageParameters *p_Img = currMB->p_Img;
05485 int list_offset = 0;
05486
05487 MotionParams *colocated = &currSlice->p_colocated->frame;
05488
05489
05490 if(currMB->mb_type == IPCM)
05491 {
05492
05493 decode_ipcm_mb(currMB);
05494 return 0;
05495 }
05496
05497 p_Img->max_mb_vmv_r = (p_Img->structure != FRAME || (currSlice->MbaffFrameFlag && currMB->mb_field)) ? p_Img->max_vmv_r >> 1 : p_Img->max_vmv_r;
05498
05499 if (!currSlice->MbaffFrameFlag)
05500 {
05501 if(p_Img->structure == TOP_FIELD)
05502 {
05503 int k,l;
05504 for (l = LIST_0; l <= (LIST_1); l++)
05505 {
05506 for(k = 0; k < p_Img->listXsize[l]; k++)
05507 {
05508 if(p_Img->structure != p_Img->listX[l][k]->structure)
05509 p_Img->listX[l][k]->chroma_vector_adjustment = -2;
05510 else
05511 p_Img->listX[l][k]->chroma_vector_adjustment= 0;
05512 }
05513 }
05514 }
05515 else if(p_Img->structure == BOTTOM_FIELD)
05516 {
05517 int k,l;
05518 for (l = LIST_0; l <= (LIST_1); l++)
05519 {
05520 for(k = 0; k < p_Img->listXsize[l]; k++)
05521 {
05522 if (p_Img->structure != p_Img->listX[l][k]->structure)
05523 p_Img->listX[l][k]->chroma_vector_adjustment = 2;
05524 else
05525 p_Img->listX[l][k]->chroma_vector_adjustment= 0;
05526 }
05527 }
05528 }
05529 else
05530 {
05531 int k,l;
05532 for (l = LIST_0; l <= (LIST_1); l++)
05533 {
05534 for(k = 0; k < p_Img->listXsize[l]; k++)
05535 {
05536 p_Img->listX[l][k]->chroma_vector_adjustment= 0;
05537 }
05538 }
05539 }
05540 }
05541 else
05542 {
05543 int mb_nr = currMB->mbAddrX;
05544 int k,l;
05545
05547
05548 if (currMB->mb_field)
05549 {
05550 if(mb_nr&0x01)
05551 {
05552 list_offset = 4;
05553 colocated = &currSlice->p_colocated->bottom;
05554 }
05555 else
05556 {
05557 list_offset = 2;
05558 colocated = &currSlice->p_colocated->top;
05559 }
05560
05561 for (l = LIST_0 + list_offset; l <= (LIST_1 + list_offset); l++)
05562 {
05563 for(k = 0; k < p_Img->listXsize[l]; k++)
05564 {
05565 p_Img->listX[l][k]->chroma_vector_adjustment= 0;
05566 if(mb_nr % 2 == 0 && p_Img->listX[l][k]->structure == BOTTOM_FIELD)
05567 p_Img->listX[l][k]->chroma_vector_adjustment = -2;
05568 if(mb_nr % 2 == 1 && p_Img->listX[l][k]->structure == TOP_FIELD)
05569 p_Img->listX[l][k]->chroma_vector_adjustment = 2;
05570 }
05571 }
05572 }
05573 else
05574 {
05575 for (l = LIST_0 + list_offset; l <= (LIST_1 + list_offset); l++)
05576 {
05577 for(k = 0; k < p_Img->listXsize[l]; k++)
05578 {
05579 p_Img->listX[l][k]->chroma_vector_adjustment= 0;
05580 }
05581 }
05582 }
05583 }
05584
05585
05586 currSlice->decode_one_component(currMB, PLANE_Y, dec_picture->imgY, dec_picture, colocated, list_offset);
05587
05588 if ((p_Img->active_sps->chroma_format_idc==YUV444)&&(!IS_INDEPENDENT(p_Img)))
05589 {
05590 ColorPlane curr_plane = PLANE_Y;
05591 int uv;
05592 for (uv = 0; uv < 2; uv++ )
05593 {
05594 curr_plane = (ColorPlane) (uv + 1);
05595
05596 currSlice->decode_one_component(currMB, curr_plane, dec_picture->imgUV[uv], dec_picture, colocated, list_offset);
05597 }
05598 }
05599 return 0;
05600 }
05601
05602
05610 void change_plane_JV( ImageParameters *p_Img, int nplane )
05611 {
05612 Slice *currSlice = p_Img->currentSlice;
05613 p_Img->colour_plane_id = nplane;
05614 p_Img->mb_data = p_Img->mb_data_JV[nplane];
05615 p_Img->dec_picture = p_Img->dec_picture_JV[nplane];
05616 currSlice->p_colocated = currSlice->Co_located_JV[nplane];
05617 }
05618
05626 void make_frame_picture_JV(ImageParameters *p_Img)
05627 {
05628 int uv, line;
05629 int nsize;
05630 int nplane;
05631 p_Img->dec_picture = p_Img->dec_picture_JV[0];
05632
05633
05634 for( nplane=0; nplane<MAX_PLANE; nplane++ )
05635 {
05636 copy_storable_param_JV( p_Img, &p_Img->dec_picture->JVmotion[nplane], &p_Img->dec_picture_JV[nplane]->motion );
05637 }
05638
05639
05640 for( uv=0; uv<2; uv++ )
05641 {
05642 for( line=0; line<p_Img->height; line++ )
05643 {
05644 nsize = sizeof(imgpel) * p_Img->width;
05645 memcpy( p_Img->dec_picture->imgUV[uv][line], p_Img->dec_picture_JV[uv+1]->imgY[line], nsize );
05646 }
05647 free_storable_picture(p_Img, p_Img->dec_picture_JV[uv+1]);
05648 }
05649 }
05650
05651