00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #ifndef AVCODEC_H264_MVPRED_H
00029 #define AVCODEC_H264_MVPRED_H
00030
00031 #include "internal.h"
00032 #include "avcodec.h"
00033 #include "h264.h"
00034 #include "libavutil/avassert.h"
00035
00036
00037 static av_always_inline int fetch_diagonal_mv(H264Context *h, const int16_t **C,
00038 int i, int list, int part_width)
00039 {
00040 const int topright_ref = h->ref_cache[list][i - 8 + part_width];
00041 MpegEncContext *s = &h->s;
00042
00043
00044
00045 if (FRAME_MBAFF) {
00046 #define SET_DIAG_MV(MV_OP, REF_OP, XY, Y4) \
00047 const int xy = XY, y4 = Y4; \
00048 const int mb_type = mb_types[xy + (y4 >> 2) * s->mb_stride]; \
00049 if (!USES_LIST(mb_type, list)) \
00050 return LIST_NOT_USED; \
00051 mv = s->current_picture_ptr->f.motion_val[list][h->mb2b_xy[xy] + 3 + y4 * h->b_stride]; \
00052 h->mv_cache[list][scan8[0] - 2][0] = mv[0]; \
00053 h->mv_cache[list][scan8[0] - 2][1] = mv[1] MV_OP; \
00054 return s->current_picture_ptr->f.ref_index[list][4 * xy + 1 + (y4 & ~1)] REF_OP;
00055
00056 if (topright_ref == PART_NOT_AVAILABLE
00057 && i >= scan8[0] + 8 && (i & 7) == 4
00058 && h->ref_cache[list][scan8[0] - 1] != PART_NOT_AVAILABLE) {
00059 const uint32_t *mb_types = s->current_picture_ptr->f.mb_type;
00060 const int16_t *mv;
00061 AV_ZERO32(h->mv_cache[list][scan8[0] - 2]);
00062 *C = h->mv_cache[list][scan8[0] - 2];
00063
00064 if (!MB_FIELD && IS_INTERLACED(h->left_type[0])) {
00065 SET_DIAG_MV(* 2, >> 1, h->left_mb_xy[0] + s->mb_stride,
00066 (s->mb_y & 1) * 2 + (i >> 5));
00067 }
00068 if (MB_FIELD && !IS_INTERLACED(h->left_type[0])) {
00069
00070 SET_DIAG_MV(/ 2, << 1, h->left_mb_xy[i >= 36], ((i >> 2)) & 3);
00071 }
00072 }
00073 #undef SET_DIAG_MV
00074 }
00075
00076 if (topright_ref != PART_NOT_AVAILABLE) {
00077 *C = h->mv_cache[list][i - 8 + part_width];
00078 return topright_ref;
00079 } else {
00080 tprintf(s->avctx, "topright MV not available\n");
00081
00082 *C = h->mv_cache[list][i - 8 - 1];
00083 return h->ref_cache[list][i - 8 - 1];
00084 }
00085 }
00086
00094 static av_always_inline void pred_motion(H264Context *const h, int n,
00095 int part_width, int list, int ref,
00096 int *const mx, int *const my)
00097 {
00098 const int index8 = scan8[n];
00099 const int top_ref = h->ref_cache[list][index8 - 8];
00100 const int left_ref = h->ref_cache[list][index8 - 1];
00101 const int16_t *const A = h->mv_cache[list][index8 - 1];
00102 const int16_t *const B = h->mv_cache[list][index8 - 8];
00103 const int16_t *C;
00104 int diagonal_ref, match_count;
00105
00106 av_assert2(part_width == 1 || part_width == 2 || part_width == 4);
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 diagonal_ref = fetch_diagonal_mv(h, &C, index8, list, part_width);
00117 match_count = (diagonal_ref == ref) + (top_ref == ref) + (left_ref == ref);
00118 tprintf(h->s.avctx, "pred_motion match_count=%d\n", match_count);
00119 if (match_count > 1) {
00120 *mx = mid_pred(A[0], B[0], C[0]);
00121 *my = mid_pred(A[1], B[1], C[1]);
00122 } else if (match_count == 1) {
00123 if (left_ref == ref) {
00124 *mx = A[0];
00125 *my = A[1];
00126 } else if (top_ref == ref) {
00127 *mx = B[0];
00128 *my = B[1];
00129 } else {
00130 *mx = C[0];
00131 *my = C[1];
00132 }
00133 } else {
00134 if (top_ref == PART_NOT_AVAILABLE &&
00135 diagonal_ref == PART_NOT_AVAILABLE &&
00136 left_ref != PART_NOT_AVAILABLE) {
00137 *mx = A[0];
00138 *my = A[1];
00139 } else {
00140 *mx = mid_pred(A[0], B[0], C[0]);
00141 *my = mid_pred(A[1], B[1], C[1]);
00142 }
00143 }
00144
00145 tprintf(h->s.avctx,
00146 "pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %d\n",
00147 top_ref, B[0], B[1], diagonal_ref, C[0], C[1], left_ref,
00148 A[0], A[1], ref, *mx, *my, h->s.mb_x, h->s.mb_y, n, list);
00149 }
00150
00157 static av_always_inline void pred_16x8_motion(H264Context *const h,
00158 int n, int list, int ref,
00159 int *const mx, int *const my)
00160 {
00161 if (n == 0) {
00162 const int top_ref = h->ref_cache[list][scan8[0] - 8];
00163 const int16_t *const B = h->mv_cache[list][scan8[0] - 8];
00164
00165 tprintf(h->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n",
00166 top_ref, B[0], B[1], h->s.mb_x, h->s.mb_y, n, list);
00167
00168 if (top_ref == ref) {
00169 *mx = B[0];
00170 *my = B[1];
00171 return;
00172 }
00173 } else {
00174 const int left_ref = h->ref_cache[list][scan8[8] - 1];
00175 const int16_t *const A = h->mv_cache[list][scan8[8] - 1];
00176
00177 tprintf(h->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n",
00178 left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
00179
00180 if (left_ref == ref) {
00181 *mx = A[0];
00182 *my = A[1];
00183 return;
00184 }
00185 }
00186
00187
00188 pred_motion(h, n, 4, list, ref, mx, my);
00189 }
00190
00197 static av_always_inline void pred_8x16_motion(H264Context *const h,
00198 int n, int list, int ref,
00199 int *const mx, int *const my)
00200 {
00201 if (n == 0) {
00202 const int left_ref = h->ref_cache[list][scan8[0] - 1];
00203 const int16_t *const A = h->mv_cache[list][scan8[0] - 1];
00204
00205 tprintf(h->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n",
00206 left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
00207
00208 if (left_ref == ref) {
00209 *mx = A[0];
00210 *my = A[1];
00211 return;
00212 }
00213 } else {
00214 const int16_t *C;
00215 int diagonal_ref;
00216
00217 diagonal_ref = fetch_diagonal_mv(h, &C, scan8[4], list, 2);
00218
00219 tprintf(h->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n",
00220 diagonal_ref, C[0], C[1], h->s.mb_x, h->s.mb_y, n, list);
00221
00222 if (diagonal_ref == ref) {
00223 *mx = C[0];
00224 *my = C[1];
00225 return;
00226 }
00227 }
00228
00229
00230 pred_motion(h, n, 2, list, ref, mx, my);
00231 }
00232
00233 #define FIX_MV_MBAFF(type, refn, mvn, idx) \
00234 if (FRAME_MBAFF) { \
00235 if (MB_FIELD) { \
00236 if (!IS_INTERLACED(type)) { \
00237 refn <<= 1; \
00238 AV_COPY32(mvbuf[idx], mvn); \
00239 mvbuf[idx][1] /= 2; \
00240 mvn = mvbuf[idx]; \
00241 } \
00242 } else { \
00243 if (IS_INTERLACED(type)) { \
00244 refn >>= 1; \
00245 AV_COPY32(mvbuf[idx], mvn); \
00246 mvbuf[idx][1] <<= 1; \
00247 mvn = mvbuf[idx]; \
00248 } \
00249 } \
00250 }
00251
00252 static av_always_inline void pred_pskip_motion(H264Context *const h)
00253 {
00254 DECLARE_ALIGNED(4, static const int16_t, zeromv)[2] = { 0 };
00255 DECLARE_ALIGNED(4, int16_t, mvbuf)[3][2];
00256 MpegEncContext *const s = &h->s;
00257 int8_t *ref = s->current_picture.f.ref_index[0];
00258 int16_t(*mv)[2] = s->current_picture.f.motion_val[0];
00259 int top_ref, left_ref, diagonal_ref, match_count, mx, my;
00260 const int16_t *A, *B, *C;
00261 int b_stride = h->b_stride;
00262
00263 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
00264
00265
00266
00267
00268
00269
00270 if (USES_LIST(h->left_type[LTOP], 0)) {
00271 left_ref = ref[4 * h->left_mb_xy[LTOP] + 1 + (h->left_block[0] & ~1)];
00272 A = mv[h->mb2b_xy[h->left_mb_xy[LTOP]] + 3 + b_stride * h->left_block[0]];
00273 FIX_MV_MBAFF(h->left_type[LTOP], left_ref, A, 0);
00274 if (!(left_ref | AV_RN32A(A)))
00275 goto zeromv;
00276 } else if (h->left_type[LTOP]) {
00277 left_ref = LIST_NOT_USED;
00278 A = zeromv;
00279 } else {
00280 goto zeromv;
00281 }
00282
00283 if (USES_LIST(h->top_type, 0)) {
00284 top_ref = ref[4 * h->top_mb_xy + 2];
00285 B = mv[h->mb2b_xy[h->top_mb_xy] + 3 * b_stride];
00286 FIX_MV_MBAFF(h->top_type, top_ref, B, 1);
00287 if (!(top_ref | AV_RN32A(B)))
00288 goto zeromv;
00289 } else if (h->top_type) {
00290 top_ref = LIST_NOT_USED;
00291 B = zeromv;
00292 } else {
00293 goto zeromv;
00294 }
00295
00296 tprintf(h->s.avctx, "pred_pskip: (%d) (%d) at %2d %2d\n",
00297 top_ref, left_ref, h->s.mb_x, h->s.mb_y);
00298
00299 if (USES_LIST(h->topright_type, 0)) {
00300 diagonal_ref = ref[4 * h->topright_mb_xy + 2];
00301 C = mv[h->mb2b_xy[h->topright_mb_xy] + 3 * b_stride];
00302 FIX_MV_MBAFF(h->topright_type, diagonal_ref, C, 2);
00303 } else if (h->topright_type) {
00304 diagonal_ref = LIST_NOT_USED;
00305 C = zeromv;
00306 } else {
00307 if (USES_LIST(h->topleft_type, 0)) {
00308 diagonal_ref = ref[4 * h->topleft_mb_xy + 1 +
00309 (h->topleft_partition & 2)];
00310 C = mv[h->mb2b_xy[h->topleft_mb_xy] + 3 + b_stride +
00311 (h->topleft_partition & 2 * b_stride)];
00312 FIX_MV_MBAFF(h->topleft_type, diagonal_ref, C, 2);
00313 } else if (h->topleft_type) {
00314 diagonal_ref = LIST_NOT_USED;
00315 C = zeromv;
00316 } else {
00317 diagonal_ref = PART_NOT_AVAILABLE;
00318 C = zeromv;
00319 }
00320 }
00321
00322 match_count = !diagonal_ref + !top_ref + !left_ref;
00323 tprintf(h->s.avctx, "pred_pskip_motion match_count=%d\n", match_count);
00324 if (match_count > 1) {
00325 mx = mid_pred(A[0], B[0], C[0]);
00326 my = mid_pred(A[1], B[1], C[1]);
00327 } else if (match_count == 1) {
00328 if (!left_ref) {
00329 mx = A[0];
00330 my = A[1];
00331 } else if (!top_ref) {
00332 mx = B[0];
00333 my = B[1];
00334 } else {
00335 mx = C[0];
00336 my = C[1];
00337 }
00338 } else {
00339 mx = mid_pred(A[0], B[0], C[0]);
00340 my = mid_pred(A[1], B[1], C[1]);
00341 }
00342
00343 fill_rectangle(h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx, my), 4);
00344 return;
00345
00346 zeromv:
00347 fill_rectangle(h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
00348 return;
00349 }
00350
00351 static void fill_decode_neighbors(H264Context *h, int mb_type)
00352 {
00353 MpegEncContext *const s = &h->s;
00354 const int mb_xy = h->mb_xy;
00355 int topleft_xy, top_xy, topright_xy, left_xy[LEFT_MBS];
00356 static const uint8_t left_block_options[4][32] = {
00357 { 0, 1, 2, 3, 7, 10, 8, 11, 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4, 3 + 3 * 4, 1 + 4 * 4, 1 + 8 * 4, 1 + 5 * 4, 1 + 9 * 4 },
00358 { 2, 2, 3, 3, 8, 11, 8, 11, 3 + 2 * 4, 3 + 2 * 4, 3 + 3 * 4, 3 + 3 * 4, 1 + 5 * 4, 1 + 9 * 4, 1 + 5 * 4, 1 + 9 * 4 },
00359 { 0, 0, 1, 1, 7, 10, 7, 10, 3 + 0 * 4, 3 + 0 * 4, 3 + 1 * 4, 3 + 1 * 4, 1 + 4 * 4, 1 + 8 * 4, 1 + 4 * 4, 1 + 8 * 4 },
00360 { 0, 2, 0, 2, 7, 10, 7, 10, 3 + 0 * 4, 3 + 2 * 4, 3 + 0 * 4, 3 + 2 * 4, 1 + 4 * 4, 1 + 8 * 4, 1 + 4 * 4, 1 + 8 * 4 }
00361 };
00362
00363 h->topleft_partition = -1;
00364
00365 top_xy = mb_xy - (s->mb_stride << MB_FIELD);
00366
00367
00368
00369
00370 topleft_xy = top_xy - 1;
00371 topright_xy = top_xy + 1;
00372 left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
00373 h->left_block = left_block_options[0];
00374 if (FRAME_MBAFF) {
00375 const int left_mb_field_flag = IS_INTERLACED(s->current_picture.f.mb_type[mb_xy - 1]);
00376 const int curr_mb_field_flag = IS_INTERLACED(mb_type);
00377 if (s->mb_y & 1) {
00378 if (left_mb_field_flag != curr_mb_field_flag) {
00379 left_xy[LBOT] = left_xy[LTOP] = mb_xy - s->mb_stride - 1;
00380 if (curr_mb_field_flag) {
00381 left_xy[LBOT] += s->mb_stride;
00382 h->left_block = left_block_options[3];
00383 } else {
00384 topleft_xy += s->mb_stride;
00385
00386
00387 h->topleft_partition = 0;
00388 h->left_block = left_block_options[1];
00389 }
00390 }
00391 } else {
00392 if (curr_mb_field_flag) {
00393 topleft_xy += s->mb_stride & (((s->current_picture.f.mb_type[top_xy - 1] >> 7) & 1) - 1);
00394 topright_xy += s->mb_stride & (((s->current_picture.f.mb_type[top_xy + 1] >> 7) & 1) - 1);
00395 top_xy += s->mb_stride & (((s->current_picture.f.mb_type[top_xy] >> 7) & 1) - 1);
00396 }
00397 if (left_mb_field_flag != curr_mb_field_flag) {
00398 if (curr_mb_field_flag) {
00399 left_xy[LBOT] += s->mb_stride;
00400 h->left_block = left_block_options[3];
00401 } else {
00402 h->left_block = left_block_options[2];
00403 }
00404 }
00405 }
00406 }
00407
00408 h->topleft_mb_xy = topleft_xy;
00409 h->top_mb_xy = top_xy;
00410 h->topright_mb_xy = topright_xy;
00411 h->left_mb_xy[LTOP] = left_xy[LTOP];
00412 h->left_mb_xy[LBOT] = left_xy[LBOT];
00413
00414
00415 h->topleft_type = s->current_picture.f.mb_type[topleft_xy];
00416 h->top_type = s->current_picture.f.mb_type[top_xy];
00417 h->topright_type = s->current_picture.f.mb_type[topright_xy];
00418 h->left_type[LTOP] = s->current_picture.f.mb_type[left_xy[LTOP]];
00419 h->left_type[LBOT] = s->current_picture.f.mb_type[left_xy[LBOT]];
00420
00421 if (FMO) {
00422 if (h->slice_table[topleft_xy] != h->slice_num)
00423 h->topleft_type = 0;
00424 if (h->slice_table[top_xy] != h->slice_num)
00425 h->top_type = 0;
00426 if (h->slice_table[left_xy[LTOP]] != h->slice_num)
00427 h->left_type[LTOP] = h->left_type[LBOT] = 0;
00428 } else {
00429 if (h->slice_table[topleft_xy] != h->slice_num) {
00430 h->topleft_type = 0;
00431 if (h->slice_table[top_xy] != h->slice_num)
00432 h->top_type = 0;
00433 if (h->slice_table[left_xy[LTOP]] != h->slice_num)
00434 h->left_type[LTOP] = h->left_type[LBOT] = 0;
00435 }
00436 }
00437 if (h->slice_table[topright_xy] != h->slice_num)
00438 h->topright_type = 0;
00439 }
00440
00441 static void fill_decode_caches(H264Context *h, int mb_type)
00442 {
00443 MpegEncContext *const s = &h->s;
00444 int topleft_xy, top_xy, topright_xy, left_xy[LEFT_MBS];
00445 int topleft_type, top_type, topright_type, left_type[LEFT_MBS];
00446 const uint8_t *left_block = h->left_block;
00447 int i;
00448 uint8_t *nnz;
00449 uint8_t *nnz_cache;
00450
00451 topleft_xy = h->topleft_mb_xy;
00452 top_xy = h->top_mb_xy;
00453 topright_xy = h->topright_mb_xy;
00454 left_xy[LTOP] = h->left_mb_xy[LTOP];
00455 left_xy[LBOT] = h->left_mb_xy[LBOT];
00456 topleft_type = h->topleft_type;
00457 top_type = h->top_type;
00458 topright_type = h->topright_type;
00459 left_type[LTOP] = h->left_type[LTOP];
00460 left_type[LBOT] = h->left_type[LBOT];
00461
00462 if (!IS_SKIP(mb_type)) {
00463 if (IS_INTRA(mb_type)) {
00464 int type_mask = h->pps.constrained_intra_pred ? IS_INTRA(-1) : -1;
00465 h->topleft_samples_available =
00466 h->top_samples_available =
00467 h->left_samples_available = 0xFFFF;
00468 h->topright_samples_available = 0xEEEA;
00469
00470 if (!(top_type & type_mask)) {
00471 h->topleft_samples_available = 0xB3FF;
00472 h->top_samples_available = 0x33FF;
00473 h->topright_samples_available = 0x26EA;
00474 }
00475 if (IS_INTERLACED(mb_type) != IS_INTERLACED(left_type[LTOP])) {
00476 if (IS_INTERLACED(mb_type)) {
00477 if (!(left_type[LTOP] & type_mask)) {
00478 h->topleft_samples_available &= 0xDFFF;
00479 h->left_samples_available &= 0x5FFF;
00480 }
00481 if (!(left_type[LBOT] & type_mask)) {
00482 h->topleft_samples_available &= 0xFF5F;
00483 h->left_samples_available &= 0xFF5F;
00484 }
00485 } else {
00486 int left_typei = s->current_picture.f.mb_type[left_xy[LTOP] + s->mb_stride];
00487
00488 av_assert2(left_xy[LTOP] == left_xy[LBOT]);
00489 if (!((left_typei & type_mask) && (left_type[LTOP] & type_mask))) {
00490 h->topleft_samples_available &= 0xDF5F;
00491 h->left_samples_available &= 0x5F5F;
00492 }
00493 }
00494 } else {
00495 if (!(left_type[LTOP] & type_mask)) {
00496 h->topleft_samples_available &= 0xDF5F;
00497 h->left_samples_available &= 0x5F5F;
00498 }
00499 }
00500
00501 if (!(topleft_type & type_mask))
00502 h->topleft_samples_available &= 0x7FFF;
00503
00504 if (!(topright_type & type_mask))
00505 h->topright_samples_available &= 0xFBFF;
00506
00507 if (IS_INTRA4x4(mb_type)) {
00508 if (IS_INTRA4x4(top_type)) {
00509 AV_COPY32(h->intra4x4_pred_mode_cache + 4 + 8 * 0, h->intra4x4_pred_mode + h->mb2br_xy[top_xy]);
00510 } else {
00511 h->intra4x4_pred_mode_cache[4 + 8 * 0] =
00512 h->intra4x4_pred_mode_cache[5 + 8 * 0] =
00513 h->intra4x4_pred_mode_cache[6 + 8 * 0] =
00514 h->intra4x4_pred_mode_cache[7 + 8 * 0] = 2 - 3 * !(top_type & type_mask);
00515 }
00516 for (i = 0; i < 2; i++) {
00517 if (IS_INTRA4x4(left_type[LEFT(i)])) {
00518 int8_t *mode = h->intra4x4_pred_mode + h->mb2br_xy[left_xy[LEFT(i)]];
00519 h->intra4x4_pred_mode_cache[3 + 8 * 1 + 2 * 8 * i] = mode[6 - left_block[0 + 2 * i]];
00520 h->intra4x4_pred_mode_cache[3 + 8 * 2 + 2 * 8 * i] = mode[6 - left_block[1 + 2 * i]];
00521 } else {
00522 h->intra4x4_pred_mode_cache[3 + 8 * 1 + 2 * 8 * i] =
00523 h->intra4x4_pred_mode_cache[3 + 8 * 2 + 2 * 8 * i] = 2 - 3 * !(left_type[LEFT(i)] & type_mask);
00524 }
00525 }
00526 }
00527 }
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539 nnz_cache = h->non_zero_count_cache;
00540 if (top_type) {
00541 nnz = h->non_zero_count[top_xy];
00542 AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[4 * 3]);
00543 if (!s->chroma_y_shift) {
00544 AV_COPY32(&nnz_cache[4 + 8 * 5], &nnz[4 * 7]);
00545 AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 11]);
00546 } else {
00547 AV_COPY32(&nnz_cache[4 + 8 * 5], &nnz[4 * 5]);
00548 AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 9]);
00549 }
00550 } else {
00551 uint32_t top_empty = CABAC && !IS_INTRA(mb_type) ? 0 : 0x40404040;
00552 AV_WN32A(&nnz_cache[4 + 8 * 0], top_empty);
00553 AV_WN32A(&nnz_cache[4 + 8 * 5], top_empty);
00554 AV_WN32A(&nnz_cache[4 + 8 * 10], top_empty);
00555 }
00556
00557 for (i = 0; i < 2; i++) {
00558 if (left_type[LEFT(i)]) {
00559 nnz = h->non_zero_count[left_xy[LEFT(i)]];
00560 nnz_cache[3 + 8 * 1 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i]];
00561 nnz_cache[3 + 8 * 2 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i]];
00562 if (CHROMA444) {
00563 nnz_cache[3 + 8 * 6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 4 * 4];
00564 nnz_cache[3 + 8 * 7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 4 * 4];
00565 nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 8 * 4];
00566 nnz_cache[3 + 8 * 12 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 8 * 4];
00567 } else if (CHROMA422) {
00568 nnz_cache[3 + 8 * 6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 4 * 4];
00569 nnz_cache[3 + 8 * 7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] - 2 + 4 * 4];
00570 nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 8 * 4];
00571 nnz_cache[3 + 8 * 12 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] - 2 + 8 * 4];
00572 } else {
00573 nnz_cache[3 + 8 * 6 + 8 * i] = nnz[left_block[8 + 4 + 2 * i]];
00574 nnz_cache[3 + 8 * 11 + 8 * i] = nnz[left_block[8 + 5 + 2 * i]];
00575 }
00576 } else {
00577 nnz_cache[3 + 8 * 1 + 2 * 8 * i] =
00578 nnz_cache[3 + 8 * 2 + 2 * 8 * i] =
00579 nnz_cache[3 + 8 * 6 + 2 * 8 * i] =
00580 nnz_cache[3 + 8 * 7 + 2 * 8 * i] =
00581 nnz_cache[3 + 8 * 11 + 2 * 8 * i] =
00582 nnz_cache[3 + 8 * 12 + 2 * 8 * i] = CABAC && !IS_INTRA(mb_type) ? 0 : 64;
00583 }
00584 }
00585
00586 if (CABAC) {
00587
00588 if (top_type)
00589 h->top_cbp = h->cbp_table[top_xy];
00590 else
00591 h->top_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;
00592
00593 if (left_type[LTOP]) {
00594 h->left_cbp = (h->cbp_table[left_xy[LTOP]] & 0x7F0) |
00595 ((h->cbp_table[left_xy[LTOP]] >> (left_block[0] & (~1))) & 2) |
00596 (((h->cbp_table[left_xy[LBOT]] >> (left_block[2] & (~1))) & 2) << 2);
00597 } else {
00598 h->left_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;
00599 }
00600 }
00601 }
00602
00603 if (IS_INTER(mb_type) || (IS_DIRECT(mb_type) && h->direct_spatial_mv_pred)) {
00604 int list;
00605 int b_stride = h->b_stride;
00606 for (list = 0; list < h->list_count; list++) {
00607 int8_t *ref_cache = &h->ref_cache[list][scan8[0]];
00608 int8_t *ref = s->current_picture.f.ref_index[list];
00609 int16_t(*mv_cache)[2] = &h->mv_cache[list][scan8[0]];
00610 int16_t(*mv)[2] = s->current_picture.f.motion_val[list];
00611 if (!USES_LIST(mb_type, list))
00612 continue;
00613 av_assert2(!(IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred));
00614
00615 if (USES_LIST(top_type, list)) {
00616 const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
00617 AV_COPY128(mv_cache[0 - 1 * 8], mv[b_xy + 0]);
00618 ref_cache[0 - 1 * 8] =
00619 ref_cache[1 - 1 * 8] = ref[4 * top_xy + 2];
00620 ref_cache[2 - 1 * 8] =
00621 ref_cache[3 - 1 * 8] = ref[4 * top_xy + 3];
00622 } else {
00623 AV_ZERO128(mv_cache[0 - 1 * 8]);
00624 AV_WN32A(&ref_cache[0 - 1 * 8],
00625 ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE) & 0xFF) * 0x01010101u);
00626 }
00627
00628 if (mb_type & (MB_TYPE_16x8 | MB_TYPE_8x8)) {
00629 for (i = 0; i < 2; i++) {
00630 int cache_idx = -1 + i * 2 * 8;
00631 if (USES_LIST(left_type[LEFT(i)], list)) {
00632 const int b_xy = h->mb2b_xy[left_xy[LEFT(i)]] + 3;
00633 const int b8_xy = 4 * left_xy[LEFT(i)] + 1;
00634 AV_COPY32(mv_cache[cache_idx],
00635 mv[b_xy + b_stride * left_block[0 + i * 2]]);
00636 AV_COPY32(mv_cache[cache_idx + 8],
00637 mv[b_xy + b_stride * left_block[1 + i * 2]]);
00638 ref_cache[cache_idx] = ref[b8_xy + (left_block[0 + i * 2] & ~1)];
00639 ref_cache[cache_idx + 8] = ref[b8_xy + (left_block[1 + i * 2] & ~1)];
00640 } else {
00641 AV_ZERO32(mv_cache[cache_idx]);
00642 AV_ZERO32(mv_cache[cache_idx + 8]);
00643 ref_cache[cache_idx] =
00644 ref_cache[cache_idx + 8] = (left_type[LEFT(i)]) ? LIST_NOT_USED
00645 : PART_NOT_AVAILABLE;
00646 }
00647 }
00648 } else {
00649 if (USES_LIST(left_type[LTOP], list)) {
00650 const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
00651 const int b8_xy = 4 * left_xy[LTOP] + 1;
00652 AV_COPY32(mv_cache[-1], mv[b_xy + b_stride * left_block[0]]);
00653 ref_cache[-1] = ref[b8_xy + (left_block[0] & ~1)];
00654 } else {
00655 AV_ZERO32(mv_cache[-1]);
00656 ref_cache[-1] = left_type[LTOP] ? LIST_NOT_USED
00657 : PART_NOT_AVAILABLE;
00658 }
00659 }
00660
00661 if (USES_LIST(topright_type, list)) {
00662 const int b_xy = h->mb2b_xy[topright_xy] + 3 * b_stride;
00663 AV_COPY32(mv_cache[4 - 1 * 8], mv[b_xy]);
00664 ref_cache[4 - 1 * 8] = ref[4 * topright_xy + 2];
00665 } else {
00666 AV_ZERO32(mv_cache[4 - 1 * 8]);
00667 ref_cache[4 - 1 * 8] = topright_type ? LIST_NOT_USED
00668 : PART_NOT_AVAILABLE;
00669 }
00670 if(ref_cache[2 - 1*8] < 0 || ref_cache[4 - 1*8] < 0){
00671 if (USES_LIST(topleft_type, list)) {
00672 const int b_xy = h->mb2b_xy[topleft_xy] + 3 + b_stride +
00673 (h->topleft_partition & 2 * b_stride);
00674 const int b8_xy = 4 * topleft_xy + 1 + (h->topleft_partition & 2);
00675 AV_COPY32(mv_cache[-1 - 1 * 8], mv[b_xy]);
00676 ref_cache[-1 - 1 * 8] = ref[b8_xy];
00677 } else {
00678 AV_ZERO32(mv_cache[-1 - 1 * 8]);
00679 ref_cache[-1 - 1 * 8] = topleft_type ? LIST_NOT_USED
00680 : PART_NOT_AVAILABLE;
00681 }
00682 }
00683
00684 if ((mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2)) && !FRAME_MBAFF)
00685 continue;
00686
00687 if (!(mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2))) {
00688 uint8_t(*mvd_cache)[2] = &h->mvd_cache[list][scan8[0]];
00689 uint8_t(*mvd)[2] = h->mvd_table[list];
00690 ref_cache[2 + 8 * 0] =
00691 ref_cache[2 + 8 * 2] = PART_NOT_AVAILABLE;
00692 AV_ZERO32(mv_cache[2 + 8 * 0]);
00693 AV_ZERO32(mv_cache[2 + 8 * 2]);
00694
00695 if (CABAC) {
00696 if (USES_LIST(top_type, list)) {
00697 const int b_xy = h->mb2br_xy[top_xy];
00698 AV_COPY64(mvd_cache[0 - 1 * 8], mvd[b_xy + 0]);
00699 } else {
00700 AV_ZERO64(mvd_cache[0 - 1 * 8]);
00701 }
00702 if (USES_LIST(left_type[LTOP], list)) {
00703 const int b_xy = h->mb2br_xy[left_xy[LTOP]] + 6;
00704 AV_COPY16(mvd_cache[-1 + 0 * 8], mvd[b_xy - left_block[0]]);
00705 AV_COPY16(mvd_cache[-1 + 1 * 8], mvd[b_xy - left_block[1]]);
00706 } else {
00707 AV_ZERO16(mvd_cache[-1 + 0 * 8]);
00708 AV_ZERO16(mvd_cache[-1 + 1 * 8]);
00709 }
00710 if (USES_LIST(left_type[LBOT], list)) {
00711 const int b_xy = h->mb2br_xy[left_xy[LBOT]] + 6;
00712 AV_COPY16(mvd_cache[-1 + 2 * 8], mvd[b_xy - left_block[2]]);
00713 AV_COPY16(mvd_cache[-1 + 3 * 8], mvd[b_xy - left_block[3]]);
00714 } else {
00715 AV_ZERO16(mvd_cache[-1 + 2 * 8]);
00716 AV_ZERO16(mvd_cache[-1 + 3 * 8]);
00717 }
00718 AV_ZERO16(mvd_cache[2 + 8 * 0]);
00719 AV_ZERO16(mvd_cache[2 + 8 * 2]);
00720 if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
00721 uint8_t *direct_cache = &h->direct_cache[scan8[0]];
00722 uint8_t *direct_table = h->direct_table;
00723 fill_rectangle(direct_cache, 4, 4, 8, MB_TYPE_16x16 >> 1, 1);
00724
00725 if (IS_DIRECT(top_type)) {
00726 AV_WN32A(&direct_cache[-1 * 8],
00727 0x01010101u * (MB_TYPE_DIRECT2 >> 1));
00728 } else if (IS_8X8(top_type)) {
00729 int b8_xy = 4 * top_xy;
00730 direct_cache[0 - 1 * 8] = direct_table[b8_xy + 2];
00731 direct_cache[2 - 1 * 8] = direct_table[b8_xy + 3];
00732 } else {
00733 AV_WN32A(&direct_cache[-1 * 8],
00734 0x01010101 * (MB_TYPE_16x16 >> 1));
00735 }
00736
00737 if (IS_DIRECT(left_type[LTOP]))
00738 direct_cache[-1 + 0 * 8] = MB_TYPE_DIRECT2 >> 1;
00739 else if (IS_8X8(left_type[LTOP]))
00740 direct_cache[-1 + 0 * 8] = direct_table[4 * left_xy[LTOP] + 1 + (left_block[0] & ~1)];
00741 else
00742 direct_cache[-1 + 0 * 8] = MB_TYPE_16x16 >> 1;
00743
00744 if (IS_DIRECT(left_type[LBOT]))
00745 direct_cache[-1 + 2 * 8] = MB_TYPE_DIRECT2 >> 1;
00746 else if (IS_8X8(left_type[LBOT]))
00747 direct_cache[-1 + 2 * 8] = direct_table[4 * left_xy[LBOT] + 1 + (left_block[2] & ~1)];
00748 else
00749 direct_cache[-1 + 2 * 8] = MB_TYPE_16x16 >> 1;
00750 }
00751 }
00752 }
00753
00754 #define MAP_MVS \
00755 MAP_F2F(scan8[0] - 1 - 1 * 8, topleft_type) \
00756 MAP_F2F(scan8[0] + 0 - 1 * 8, top_type) \
00757 MAP_F2F(scan8[0] + 1 - 1 * 8, top_type) \
00758 MAP_F2F(scan8[0] + 2 - 1 * 8, top_type) \
00759 MAP_F2F(scan8[0] + 3 - 1 * 8, top_type) \
00760 MAP_F2F(scan8[0] + 4 - 1 * 8, topright_type) \
00761 MAP_F2F(scan8[0] - 1 + 0 * 8, left_type[LTOP]) \
00762 MAP_F2F(scan8[0] - 1 + 1 * 8, left_type[LTOP]) \
00763 MAP_F2F(scan8[0] - 1 + 2 * 8, left_type[LBOT]) \
00764 MAP_F2F(scan8[0] - 1 + 3 * 8, left_type[LBOT])
00765
00766 if (FRAME_MBAFF) {
00767 if (MB_FIELD) {
00768
00769 #define MAP_F2F(idx, mb_type) \
00770 if (!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0) { \
00771 h->ref_cache[list][idx] <<= 1; \
00772 h->mv_cache[list][idx][1] /= 2; \
00773 h->mvd_cache[list][idx][1] >>= 1; \
00774 }
00775
00776 MAP_MVS
00777 } else {
00778
00779 #undef MAP_F2F
00780 #define MAP_F2F(idx, mb_type) \
00781 if (IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0) { \
00782 h->ref_cache[list][idx] >>= 1; \
00783 h->mv_cache[list][idx][1] <<= 1; \
00784 h->mvd_cache[list][idx][1] <<= 1; \
00785 }
00786
00787 MAP_MVS
00788 #undef MAP_F2F
00789 }
00790 }
00791 }
00792 }
00793
00794 h->neighbor_transform_size = !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[LTOP]);
00795 }
00796
00800 static void av_unused decode_mb_skip(H264Context *h)
00801 {
00802 MpegEncContext *const s = &h->s;
00803 const int mb_xy = h->mb_xy;
00804 int mb_type = 0;
00805
00806 memset(h->non_zero_count[mb_xy], 0, 48);
00807
00808 if (MB_FIELD)
00809 mb_type |= MB_TYPE_INTERLACED;
00810
00811 if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
00812
00813 mb_type |= MB_TYPE_L0L1 | MB_TYPE_DIRECT2 | MB_TYPE_SKIP;
00814 if (h->direct_spatial_mv_pred) {
00815 fill_decode_neighbors(h, mb_type);
00816 fill_decode_caches(h, mb_type);
00817 }
00818 ff_h264_pred_direct_motion(h, &mb_type);
00819 mb_type |= MB_TYPE_SKIP;
00820 } else {
00821 mb_type |= MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P1L0 | MB_TYPE_SKIP;
00822
00823 fill_decode_neighbors(h, mb_type);
00824 pred_pskip_motion(h);
00825 }
00826
00827 write_back_motion(h, mb_type);
00828 s->current_picture.f.mb_type[mb_xy] = mb_type;
00829 s->current_picture.f.qscale_table[mb_xy] = s->qscale;
00830 h->slice_table[mb_xy] = h->slice_num;
00831 h->prev_mb_skipped = 1;
00832 }
00833
00834 #endif