• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/h264_mvpred.h

Go to the documentation of this file.
00001 /*
00002  * H.26L/H.264/AVC/JVT/14496-10/... motion vector predicion
00003  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 
00035 //#undef NDEBUG
00036 #include <assert.h>
00037 
00038 static av_always_inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
00039     const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
00040     MpegEncContext *s = &h->s;
00041 
00042     /* there is no consistent mapping of mvs to neighboring locations that will
00043      * make mbaff happy, so we can't move all this logic to fill_caches */
00044     if(FRAME_MBAFF){
00045 
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
00065                && IS_INTERLACED(h->left_type[0])){
00066                 SET_DIAG_MV(*2, >>1, h->left_mb_xy[0]+s->mb_stride, (s->mb_y&1)*2+(i>>5));
00067             }
00068             if(MB_FIELD
00069                && !IS_INTERLACED(h->left_type[0])){
00070                 // left shift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's OK.
00071                 SET_DIAG_MV(/2, <<1, h->left_mb_xy[i>=36], ((i>>2))&3);
00072             }
00073         }
00074 #undef SET_DIAG_MV
00075     }
00076 
00077     if(topright_ref != PART_NOT_AVAILABLE){
00078         *C= h->mv_cache[list][ i - 8 + part_width ];
00079         return topright_ref;
00080     }else{
00081         tprintf(s->avctx, "topright MV not available\n");
00082 
00083         *C= h->mv_cache[list][ i - 8 - 1 ];
00084         return h->ref_cache[list][ i - 8 - 1 ];
00085     }
00086 }
00087 
00095 static av_always_inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
00096     const int index8= scan8[n];
00097     const int top_ref=      h->ref_cache[list][ index8 - 8 ];
00098     const int left_ref=     h->ref_cache[list][ index8 - 1 ];
00099     const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
00100     const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
00101     const int16_t * C;
00102     int diagonal_ref, match_count;
00103 
00104     assert(part_width==1 || part_width==2 || part_width==4);
00105 
00106 /* mv_cache
00107   B . . A T T T T
00108   U . . L . . , .
00109   U . . L . . . .
00110   U . . L . . , .
00111   . . . L . . . .
00112 */
00113 
00114     diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
00115     match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
00116     tprintf(h->s.avctx, "pred_motion match_count=%d\n", match_count);
00117     if(match_count > 1){ //most common
00118         *mx= mid_pred(A[0], B[0], C[0]);
00119         *my= mid_pred(A[1], B[1], C[1]);
00120     }else if(match_count==1){
00121         if(left_ref==ref){
00122             *mx= A[0];
00123             *my= A[1];
00124         }else if(top_ref==ref){
00125             *mx= B[0];
00126             *my= B[1];
00127         }else{
00128             *mx= C[0];
00129             *my= C[1];
00130         }
00131     }else{
00132         if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
00133             *mx= A[0];
00134             *my= A[1];
00135         }else{
00136             *mx= mid_pred(A[0], B[0], C[0]);
00137             *my= mid_pred(A[1], B[1], C[1]);
00138         }
00139     }
00140 
00141     tprintf(h->s.avctx, "pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1],                    diagonal_ref, C[0], C[1], left_ref, A[0], A[1], ref, *mx, *my, h->s.mb_x, h->s.mb_y, n, list);
00142 }
00143 
00150 static av_always_inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
00151     if(n==0){
00152         const int top_ref=      h->ref_cache[list][ scan8[0] - 8 ];
00153         const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
00154 
00155         tprintf(h->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], h->s.mb_x, h->s.mb_y, n, list);
00156 
00157         if(top_ref == ref){
00158             *mx= B[0];
00159             *my= B[1];
00160             return;
00161         }
00162     }else{
00163         const int left_ref=     h->ref_cache[list][ scan8[8] - 1 ];
00164         const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
00165 
00166         tprintf(h->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
00167 
00168         if(left_ref == ref){
00169             *mx= A[0];
00170             *my= A[1];
00171             return;
00172         }
00173     }
00174 
00175     //RARE
00176     pred_motion(h, n, 4, list, ref, mx, my);
00177 }
00178 
00185 static av_always_inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
00186     if(n==0){
00187         const int left_ref=      h->ref_cache[list][ scan8[0] - 1 ];
00188         const int16_t * const A=  h->mv_cache[list][ scan8[0] - 1 ];
00189 
00190         tprintf(h->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
00191 
00192         if(left_ref == ref){
00193             *mx= A[0];
00194             *my= A[1];
00195             return;
00196         }
00197     }else{
00198         const int16_t * C;
00199         int diagonal_ref;
00200 
00201         diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
00202 
00203         tprintf(h->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", diagonal_ref, C[0], C[1], h->s.mb_x, h->s.mb_y, n, list);
00204 
00205         if(diagonal_ref == ref){
00206             *mx= C[0];
00207             *my= C[1];
00208             return;
00209         }
00210     }
00211 
00212     //RARE
00213     pred_motion(h, n, 2, list, ref, mx, my);
00214 }
00215 
00216 #define FIX_MV_MBAFF(type, refn, mvn, idx)\
00217     if(FRAME_MBAFF){\
00218         if(MB_FIELD){\
00219             if(!IS_INTERLACED(type)){\
00220                 refn <<= 1;\
00221                 AV_COPY32(mvbuf[idx], mvn);\
00222                 mvbuf[idx][1] /= 2;\
00223                 mvn = mvbuf[idx];\
00224             }\
00225         }else{\
00226             if(IS_INTERLACED(type)){\
00227                 refn >>= 1;\
00228                 AV_COPY32(mvbuf[idx], mvn);\
00229                 mvbuf[idx][1] <<= 1;\
00230                 mvn = mvbuf[idx];\
00231             }\
00232         }\
00233     }
00234 
00235 static av_always_inline void pred_pskip_motion(H264Context * const h){
00236     DECLARE_ALIGNED(4, static const int16_t, zeromv)[2] = {0};
00237     DECLARE_ALIGNED(4, int16_t, mvbuf)[3][2];
00238     MpegEncContext * const s = &h->s;
00239     int8_t *ref      = s->current_picture.f.ref_index[0];
00240     int16_t (*mv)[2] = s->current_picture.f.motion_val[0];
00241     int top_ref, left_ref, diagonal_ref, match_count, mx, my;
00242     const int16_t *A, *B, *C;
00243     int b_stride = h->b_stride;
00244 
00245     fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
00246 
00247     /* To avoid doing an entire fill_decode_caches, we inline the relevant parts here.
00248      * FIXME: this is a partial duplicate of the logic in fill_decode_caches, but it's
00249      * faster this way.  Is there a way to avoid this duplication?
00250      */
00251     if(USES_LIST(h->left_type[LTOP], 0)){
00252         left_ref = ref[4*h->left_mb_xy[LTOP] + 1 + (h->left_block[0]&~1)];
00253         A = mv[h->mb2b_xy[h->left_mb_xy[LTOP]] + 3 + b_stride*h->left_block[0]];
00254         FIX_MV_MBAFF(h->left_type[LTOP], left_ref, A, 0);
00255         if(!(left_ref | AV_RN32A(A))){
00256             goto zeromv;
00257         }
00258     }else if(h->left_type[LTOP]){
00259         left_ref = LIST_NOT_USED;
00260         A = zeromv;
00261     }else{
00262         goto zeromv;
00263     }
00264 
00265     if(USES_LIST(h->top_type, 0)){
00266         top_ref = ref[4*h->top_mb_xy + 2];
00267         B = mv[h->mb2b_xy[h->top_mb_xy] + 3*b_stride];
00268         FIX_MV_MBAFF(h->top_type, top_ref, B, 1);
00269         if(!(top_ref | AV_RN32A(B))){
00270             goto zeromv;
00271         }
00272     }else if(h->top_type){
00273         top_ref = LIST_NOT_USED;
00274         B = zeromv;
00275     }else{
00276         goto zeromv;
00277     }
00278 
00279     tprintf(h->s.avctx, "pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
00280 
00281     if(USES_LIST(h->topright_type, 0)){
00282         diagonal_ref = ref[4*h->topright_mb_xy + 2];
00283         C = mv[h->mb2b_xy[h->topright_mb_xy] + 3*b_stride];
00284         FIX_MV_MBAFF(h->topright_type, diagonal_ref, C, 2);
00285     }else if(h->topright_type){
00286         diagonal_ref = LIST_NOT_USED;
00287         C = zeromv;
00288     }else{
00289         if(USES_LIST(h->topleft_type, 0)){
00290             diagonal_ref = ref[4*h->topleft_mb_xy + 1 + (h->topleft_partition & 2)];
00291             C = mv[h->mb2b_xy[h->topleft_mb_xy] + 3 + b_stride + (h->topleft_partition & 2*b_stride)];
00292             FIX_MV_MBAFF(h->topleft_type, diagonal_ref, C, 2);
00293         }else if(h->topleft_type){
00294             diagonal_ref = LIST_NOT_USED;
00295             C = zeromv;
00296         }else{
00297             diagonal_ref = PART_NOT_AVAILABLE;
00298             C = zeromv;
00299         }
00300     }
00301 
00302     match_count= !diagonal_ref + !top_ref + !left_ref;
00303     tprintf(h->s.avctx, "pred_pskip_motion match_count=%d\n", match_count);
00304     if(match_count > 1){
00305         mx = mid_pred(A[0], B[0], C[0]);
00306         my = mid_pred(A[1], B[1], C[1]);
00307     }else if(match_count==1){
00308         if(!left_ref){
00309             mx = A[0];
00310             my = A[1];
00311         }else if(!top_ref){
00312             mx = B[0];
00313             my = B[1];
00314         }else{
00315             mx = C[0];
00316             my = C[1];
00317         }
00318     }else{
00319         mx = mid_pred(A[0], B[0], C[0]);
00320         my = mid_pred(A[1], B[1], C[1]);
00321     }
00322 
00323     fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
00324     return;
00325 zeromv:
00326     fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
00327     return;
00328 }
00329 
00330 static void fill_decode_neighbors(H264Context *h, int mb_type){
00331     MpegEncContext * const s = &h->s;
00332     const int mb_xy= h->mb_xy;
00333     int topleft_xy, top_xy, topright_xy, left_xy[LEFT_MBS];
00334     static const uint8_t left_block_options[4][32]={
00335         {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},
00336         {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},
00337         {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},
00338         {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}
00339     };
00340 
00341     h->topleft_partition= -1;
00342 
00343     top_xy     = mb_xy  - (s->mb_stride << MB_FIELD);
00344 
00345     /* Wow, what a mess, why didn't they simplify the interlacing & intra
00346      * stuff, I can't imagine that these complex rules are worth it. */
00347 
00348     topleft_xy = top_xy - 1;
00349     topright_xy= top_xy + 1;
00350     left_xy[LBOT] = left_xy[LTOP] = mb_xy-1;
00351     h->left_block = left_block_options[0];
00352     if(FRAME_MBAFF){
00353         const int left_mb_field_flag = IS_INTERLACED(s->current_picture.f.mb_type[mb_xy - 1]);
00354         const int curr_mb_field_flag = IS_INTERLACED(mb_type);
00355         if(s->mb_y&1){
00356             if (left_mb_field_flag != curr_mb_field_flag) {
00357                 left_xy[LBOT] = left_xy[LTOP] = mb_xy - s->mb_stride - 1;
00358                 if (curr_mb_field_flag) {
00359                     left_xy[LBOT] += s->mb_stride;
00360                     h->left_block = left_block_options[3];
00361                 } else {
00362                     topleft_xy += s->mb_stride;
00363                     // take top left mv from the middle of the mb, as opposed to all other modes which use the bottom right partition
00364                     h->topleft_partition = 0;
00365                     h->left_block = left_block_options[1];
00366                 }
00367             }
00368         }else{
00369             if(curr_mb_field_flag){
00370                 topleft_xy  += s->mb_stride & (((s->current_picture.f.mb_type[top_xy - 1] >> 7) & 1) - 1);
00371                 topright_xy += s->mb_stride & (((s->current_picture.f.mb_type[top_xy + 1] >> 7) & 1) - 1);
00372                 top_xy      += s->mb_stride & (((s->current_picture.f.mb_type[top_xy    ] >> 7) & 1) - 1);
00373             }
00374             if (left_mb_field_flag != curr_mb_field_flag) {
00375                 if (curr_mb_field_flag) {
00376                     left_xy[LBOT] += s->mb_stride;
00377                     h->left_block = left_block_options[3];
00378                 } else {
00379                     h->left_block = left_block_options[2];
00380                 }
00381             }
00382         }
00383     }
00384 
00385     h->topleft_mb_xy = topleft_xy;
00386     h->top_mb_xy     = top_xy;
00387     h->topright_mb_xy= topright_xy;
00388     h->left_mb_xy[LTOP] = left_xy[LTOP];
00389     h->left_mb_xy[LBOT] = left_xy[LBOT];
00390     //FIXME do we need all in the context?
00391 
00392     h->topleft_type    = s->current_picture.f.mb_type[topleft_xy];
00393     h->top_type        = s->current_picture.f.mb_type[top_xy];
00394     h->topright_type   = s->current_picture.f.mb_type[topright_xy];
00395     h->left_type[LTOP] = s->current_picture.f.mb_type[left_xy[LTOP]];
00396     h->left_type[LBOT] = s->current_picture.f.mb_type[left_xy[LBOT]];
00397 
00398     if(FMO){
00399     if(h->slice_table[topleft_xy    ] != h->slice_num) h->topleft_type = 0;
00400     if(h->slice_table[top_xy        ] != h->slice_num) h->top_type     = 0;
00401     if(h->slice_table[left_xy[LTOP] ] != h->slice_num) h->left_type[LTOP] = h->left_type[LBOT] = 0;
00402     }else{
00403         if(h->slice_table[topleft_xy ] != h->slice_num){
00404             h->topleft_type = 0;
00405             if(h->slice_table[top_xy        ] != h->slice_num) h->top_type     = 0;
00406             if(h->slice_table[left_xy[LTOP] ] != h->slice_num) h->left_type[LTOP] = h->left_type[LBOT] = 0;
00407         }
00408     }
00409     if(h->slice_table[topright_xy] != h->slice_num) h->topright_type= 0;
00410 }
00411 
00412 static void fill_decode_caches(H264Context *h, int mb_type){
00413     MpegEncContext * const s = &h->s;
00414     int topleft_xy, top_xy, topright_xy, left_xy[LEFT_MBS];
00415     int topleft_type, top_type, topright_type, left_type[LEFT_MBS];
00416     const uint8_t * left_block= h->left_block;
00417     int i;
00418     uint8_t *nnz;
00419     uint8_t *nnz_cache;
00420 
00421     topleft_xy     = h->topleft_mb_xy;
00422     top_xy         = h->top_mb_xy;
00423     topright_xy    = h->topright_mb_xy;
00424     left_xy[LTOP]  = h->left_mb_xy[LTOP];
00425     left_xy[LBOT]  = h->left_mb_xy[LBOT];
00426     topleft_type   = h->topleft_type;
00427     top_type       = h->top_type;
00428     topright_type  = h->topright_type;
00429     left_type[LTOP]= h->left_type[LTOP];
00430     left_type[LBOT]= h->left_type[LBOT];
00431 
00432     if(!IS_SKIP(mb_type)){
00433         if(IS_INTRA(mb_type)){
00434             int type_mask= h->pps.constrained_intra_pred ? IS_INTRA(-1) : -1;
00435             h->topleft_samples_available=
00436             h->top_samples_available=
00437             h->left_samples_available= 0xFFFF;
00438             h->topright_samples_available= 0xEEEA;
00439 
00440             if(!(top_type & type_mask)){
00441                 h->topleft_samples_available= 0xB3FF;
00442                 h->top_samples_available= 0x33FF;
00443                 h->topright_samples_available= 0x26EA;
00444             }
00445             if(IS_INTERLACED(mb_type) != IS_INTERLACED(left_type[LTOP])){
00446                 if(IS_INTERLACED(mb_type)){
00447                     if(!(left_type[LTOP] & type_mask)){
00448                         h->topleft_samples_available&= 0xDFFF;
00449                         h->left_samples_available&= 0x5FFF;
00450                     }
00451                     if(!(left_type[LBOT] & type_mask)){
00452                         h->topleft_samples_available&= 0xFF5F;
00453                         h->left_samples_available&= 0xFF5F;
00454                     }
00455                 }else{
00456                     int left_typei = s->current_picture.f.mb_type[left_xy[LTOP] + s->mb_stride];
00457 
00458                     assert(left_xy[LTOP] == left_xy[LBOT]);
00459                     if(!((left_typei & type_mask) && (left_type[LTOP] & type_mask))){
00460                         h->topleft_samples_available&= 0xDF5F;
00461                         h->left_samples_available&= 0x5F5F;
00462                     }
00463                 }
00464             }else{
00465                 if(!(left_type[LTOP] & type_mask)){
00466                     h->topleft_samples_available&= 0xDF5F;
00467                     h->left_samples_available&= 0x5F5F;
00468                 }
00469             }
00470 
00471             if(!(topleft_type & type_mask))
00472                 h->topleft_samples_available&= 0x7FFF;
00473 
00474             if(!(topright_type & type_mask))
00475                 h->topright_samples_available&= 0xFBFF;
00476 
00477             if(IS_INTRA4x4(mb_type)){
00478                 if(IS_INTRA4x4(top_type)){
00479                     AV_COPY32(h->intra4x4_pred_mode_cache+4+8*0, h->intra4x4_pred_mode + h->mb2br_xy[top_xy]);
00480                 }else{
00481                     h->intra4x4_pred_mode_cache[4+8*0]=
00482                     h->intra4x4_pred_mode_cache[5+8*0]=
00483                     h->intra4x4_pred_mode_cache[6+8*0]=
00484                     h->intra4x4_pred_mode_cache[7+8*0]= 2 - 3*!(top_type & type_mask);
00485                 }
00486                 for(i=0; i<2; i++){
00487                     if(IS_INTRA4x4(left_type[LEFT(i)])){
00488                         int8_t *mode= h->intra4x4_pred_mode + h->mb2br_xy[left_xy[LEFT(i)]];
00489                         h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= mode[6-left_block[0+2*i]];
00490                         h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= mode[6-left_block[1+2*i]];
00491                     }else{
00492                         h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
00493                         h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= 2 - 3*!(left_type[LEFT(i)] & type_mask);
00494                     }
00495                 }
00496             }
00497         }
00498 
00499 
00500 /*
00501 0 . T T. T T T T
00502 1 L . .L . . . .
00503 2 L . .L . . . .
00504 3 . T TL . . . .
00505 4 L . .L . . . .
00506 5 L . .. . . . .
00507 */
00508 //FIXME constraint_intra_pred & partitioning & nnz (let us hope this is just a typo in the spec)
00509     nnz_cache = h->non_zero_count_cache;
00510     if(top_type){
00511         nnz = h->non_zero_count[top_xy];
00512         AV_COPY32(&nnz_cache[4+8* 0], &nnz[4*3]);
00513         if(!s->chroma_y_shift){
00514             AV_COPY32(&nnz_cache[4+8* 5], &nnz[4* 7]);
00515             AV_COPY32(&nnz_cache[4+8*10], &nnz[4*11]);
00516         }else{
00517             AV_COPY32(&nnz_cache[4+8* 5], &nnz[4* 5]);
00518             AV_COPY32(&nnz_cache[4+8*10], &nnz[4* 9]);
00519         }
00520     }else{
00521         uint32_t top_empty = CABAC && !IS_INTRA(mb_type) ? 0 : 0x40404040;
00522         AV_WN32A(&nnz_cache[4+8* 0], top_empty);
00523         AV_WN32A(&nnz_cache[4+8* 5], top_empty);
00524         AV_WN32A(&nnz_cache[4+8*10], top_empty);
00525     }
00526 
00527     for (i=0; i<2; i++) {
00528         if(left_type[LEFT(i)]){
00529             nnz = h->non_zero_count[left_xy[LEFT(i)]];
00530             nnz_cache[3+8* 1 + 2*8*i]= nnz[left_block[8+0+2*i]];
00531             nnz_cache[3+8* 2 + 2*8*i]= nnz[left_block[8+1+2*i]];
00532             if(CHROMA444){
00533                 nnz_cache[3+8* 6 + 2*8*i]= nnz[left_block[8+0+2*i]+4*4];
00534                 nnz_cache[3+8* 7 + 2*8*i]= nnz[left_block[8+1+2*i]+4*4];
00535                 nnz_cache[3+8*11 + 2*8*i]= nnz[left_block[8+0+2*i]+8*4];
00536                 nnz_cache[3+8*12 + 2*8*i]= nnz[left_block[8+1+2*i]+8*4];
00537             }else if(CHROMA422) {
00538                 nnz_cache[3+8* 6 + 2*8*i]= nnz[left_block[8+0+2*i]-2+4*4];
00539                 nnz_cache[3+8* 7 + 2*8*i]= nnz[left_block[8+1+2*i]-2+4*4];
00540                 nnz_cache[3+8*11 + 2*8*i]= nnz[left_block[8+0+2*i]-2+8*4];
00541                 nnz_cache[3+8*12 + 2*8*i]= nnz[left_block[8+1+2*i]-2+8*4];
00542             }else{
00543                 nnz_cache[3+8* 6 +   8*i]= nnz[left_block[8+4+2*i]];
00544                 nnz_cache[3+8*11 +   8*i]= nnz[left_block[8+5+2*i]];
00545             }
00546         }else{
00547             nnz_cache[3+8* 1 + 2*8*i]=
00548             nnz_cache[3+8* 2 + 2*8*i]=
00549             nnz_cache[3+8* 6 + 2*8*i]=
00550             nnz_cache[3+8* 7 + 2*8*i]=
00551             nnz_cache[3+8*11 + 2*8*i]=
00552             nnz_cache[3+8*12 + 2*8*i]= CABAC && !IS_INTRA(mb_type) ? 0 : 64;
00553         }
00554     }
00555 
00556     if( CABAC ) {
00557         // top_cbp
00558         if(top_type) {
00559             h->top_cbp = h->cbp_table[top_xy];
00560         } else {
00561             h->top_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;
00562         }
00563         // left_cbp
00564         if (left_type[LTOP]) {
00565             h->left_cbp =   (h->cbp_table[left_xy[LTOP]] & 0x7F0)
00566                         |  ((h->cbp_table[left_xy[LTOP]]>>(left_block[0]&(~1)))&2)
00567                         | (((h->cbp_table[left_xy[LBOT]]>>(left_block[2]&(~1)))&2) << 2);
00568         } else {
00569             h->left_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;
00570         }
00571     }
00572     }
00573 
00574     if(IS_INTER(mb_type) || (IS_DIRECT(mb_type) && h->direct_spatial_mv_pred)){
00575         int list;
00576         int b_stride = h->b_stride;
00577         for(list=0; list<h->list_count; list++){
00578             int8_t *ref_cache = &h->ref_cache[list][scan8[0]];
00579             int8_t *ref       = s->current_picture.f.ref_index[list];
00580             int16_t (*mv_cache)[2] = &h->mv_cache[list][scan8[0]];
00581             int16_t (*mv)[2]       = s->current_picture.f.motion_val[list];
00582             if(!USES_LIST(mb_type, list)){
00583                 continue;
00584             }
00585             assert(!(IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred));
00586 
00587             if(USES_LIST(top_type, list)){
00588                 const int b_xy= h->mb2b_xy[top_xy] + 3*b_stride;
00589                 AV_COPY128(mv_cache[0 - 1*8], mv[b_xy + 0]);
00590                 ref_cache[0 - 1*8]=
00591                 ref_cache[1 - 1*8]= ref[4*top_xy + 2];
00592                 ref_cache[2 - 1*8]=
00593                 ref_cache[3 - 1*8]= ref[4*top_xy + 3];
00594             }else{
00595                 AV_ZERO128(mv_cache[0 - 1*8]);
00596                 AV_WN32A(&ref_cache[0 - 1*8], ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101u);
00597             }
00598 
00599             if(mb_type & (MB_TYPE_16x8|MB_TYPE_8x8)){
00600             for(i=0; i<2; i++){
00601                 int cache_idx = -1 + i*2*8;
00602                 if(USES_LIST(left_type[LEFT(i)], list)){
00603                     const int b_xy= h->mb2b_xy[left_xy[LEFT(i)]] + 3;
00604                     const int b8_xy= 4*left_xy[LEFT(i)] + 1;
00605                     AV_COPY32(mv_cache[cache_idx  ], mv[b_xy + b_stride*left_block[0+i*2]]);
00606                     AV_COPY32(mv_cache[cache_idx+8], mv[b_xy + b_stride*left_block[1+i*2]]);
00607                     ref_cache[cache_idx  ]= ref[b8_xy + (left_block[0+i*2]&~1)];
00608                     ref_cache[cache_idx+8]= ref[b8_xy + (left_block[1+i*2]&~1)];
00609                 }else{
00610                     AV_ZERO32(mv_cache[cache_idx  ]);
00611                     AV_ZERO32(mv_cache[cache_idx+8]);
00612                     ref_cache[cache_idx  ]=
00613                     ref_cache[cache_idx+8]= (left_type[LEFT(i)]) ? LIST_NOT_USED : PART_NOT_AVAILABLE;
00614                 }
00615             }
00616             }else{
00617                 if(USES_LIST(left_type[LTOP], list)){
00618                     const int b_xy= h->mb2b_xy[left_xy[LTOP]] + 3;
00619                     const int b8_xy= 4*left_xy[LTOP] + 1;
00620                     AV_COPY32(mv_cache[-1], mv[b_xy + b_stride*left_block[0]]);
00621                     ref_cache[-1]= ref[b8_xy + (left_block[0]&~1)];
00622                 }else{
00623                     AV_ZERO32(mv_cache[-1]);
00624                     ref_cache[-1]= left_type[LTOP] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
00625                 }
00626             }
00627 
00628             if(USES_LIST(topright_type, list)){
00629                 const int b_xy= h->mb2b_xy[topright_xy] + 3*b_stride;
00630                 AV_COPY32(mv_cache[4 - 1*8], mv[b_xy]);
00631                 ref_cache[4 - 1*8]= ref[4*topright_xy + 2];
00632             }else{
00633                 AV_ZERO32(mv_cache[4 - 1*8]);
00634                 ref_cache[4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
00635             }
00636             if(ref_cache[4 - 1*8] < 0){
00637                 if(USES_LIST(topleft_type, list)){
00638                     const int b_xy = h->mb2b_xy[topleft_xy] + 3 + b_stride + (h->topleft_partition & 2*b_stride);
00639                     const int b8_xy= 4*topleft_xy + 1 + (h->topleft_partition & 2);
00640                     AV_COPY32(mv_cache[-1 - 1*8], mv[b_xy]);
00641                     ref_cache[-1 - 1*8]= ref[b8_xy];
00642                 }else{
00643                     AV_ZERO32(mv_cache[-1 - 1*8]);
00644                     ref_cache[-1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
00645                 }
00646             }
00647 
00648             if((mb_type&(MB_TYPE_SKIP|MB_TYPE_DIRECT2)) && !FRAME_MBAFF)
00649                 continue;
00650 
00651             if(!(mb_type&(MB_TYPE_SKIP|MB_TYPE_DIRECT2))){
00652                 uint8_t (*mvd_cache)[2] = &h->mvd_cache[list][scan8[0]];
00653                 uint8_t (*mvd)[2] = h->mvd_table[list];
00654                 ref_cache[2+8*0] =
00655                 ref_cache[2+8*2] = PART_NOT_AVAILABLE;
00656                 AV_ZERO32(mv_cache[2+8*0]);
00657                 AV_ZERO32(mv_cache[2+8*2]);
00658 
00659                 if( CABAC ) {
00660                     if(USES_LIST(top_type, list)){
00661                         const int b_xy= h->mb2br_xy[top_xy];
00662                         AV_COPY64(mvd_cache[0 - 1*8], mvd[b_xy + 0]);
00663                     }else{
00664                         AV_ZERO64(mvd_cache[0 - 1*8]);
00665                     }
00666                     if(USES_LIST(left_type[LTOP], list)){
00667                         const int b_xy= h->mb2br_xy[left_xy[LTOP]] + 6;
00668                         AV_COPY16(mvd_cache[-1 + 0*8], mvd[b_xy - left_block[0]]);
00669                         AV_COPY16(mvd_cache[-1 + 1*8], mvd[b_xy - left_block[1]]);
00670                     }else{
00671                         AV_ZERO16(mvd_cache[-1 + 0*8]);
00672                         AV_ZERO16(mvd_cache[-1 + 1*8]);
00673                     }
00674                     if(USES_LIST(left_type[LBOT], list)){
00675                         const int b_xy= h->mb2br_xy[left_xy[LBOT]] + 6;
00676                         AV_COPY16(mvd_cache[-1 + 2*8], mvd[b_xy - left_block[2]]);
00677                         AV_COPY16(mvd_cache[-1 + 3*8], mvd[b_xy - left_block[3]]);
00678                     }else{
00679                         AV_ZERO16(mvd_cache[-1 + 2*8]);
00680                         AV_ZERO16(mvd_cache[-1 + 3*8]);
00681                     }
00682                     AV_ZERO16(mvd_cache[2+8*0]);
00683                     AV_ZERO16(mvd_cache[2+8*2]);
00684                     if(h->slice_type_nos == AV_PICTURE_TYPE_B){
00685                         uint8_t *direct_cache = &h->direct_cache[scan8[0]];
00686                         uint8_t *direct_table = h->direct_table;
00687                         fill_rectangle(direct_cache, 4, 4, 8, MB_TYPE_16x16>>1, 1);
00688 
00689                         if(IS_DIRECT(top_type)){
00690                             AV_WN32A(&direct_cache[-1*8], 0x01010101u*(MB_TYPE_DIRECT2>>1));
00691                         }else if(IS_8X8(top_type)){
00692                             int b8_xy = 4*top_xy;
00693                             direct_cache[0 - 1*8]= direct_table[b8_xy + 2];
00694                             direct_cache[2 - 1*8]= direct_table[b8_xy + 3];
00695                         }else{
00696                             AV_WN32A(&direct_cache[-1*8], 0x01010101*(MB_TYPE_16x16>>1));
00697                         }
00698 
00699                         if(IS_DIRECT(left_type[LTOP]))
00700                             direct_cache[-1 + 0*8]= MB_TYPE_DIRECT2>>1;
00701                         else if(IS_8X8(left_type[LTOP]))
00702                             direct_cache[-1 + 0*8]= direct_table[4*left_xy[LTOP] + 1 + (left_block[0]&~1)];
00703                         else
00704                             direct_cache[-1 + 0*8]= MB_TYPE_16x16>>1;
00705 
00706                         if(IS_DIRECT(left_type[LBOT]))
00707                             direct_cache[-1 + 2*8]= MB_TYPE_DIRECT2>>1;
00708                         else if(IS_8X8(left_type[LBOT]))
00709                             direct_cache[-1 + 2*8]= direct_table[4*left_xy[LBOT] + 1 + (left_block[2]&~1)];
00710                         else
00711                             direct_cache[-1 + 2*8]= MB_TYPE_16x16>>1;
00712                     }
00713                 }
00714             }
00715             if(FRAME_MBAFF){
00716 #define MAP_MVS\
00717                     MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
00718                     MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
00719                     MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
00720                     MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
00721                     MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
00722                     MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
00723                     MAP_F2F(scan8[0] - 1 + 0*8, left_type[LTOP])\
00724                     MAP_F2F(scan8[0] - 1 + 1*8, left_type[LTOP])\
00725                     MAP_F2F(scan8[0] - 1 + 2*8, left_type[LBOT])\
00726                     MAP_F2F(scan8[0] - 1 + 3*8, left_type[LBOT])
00727                 if(MB_FIELD){
00728 #define MAP_F2F(idx, mb_type)\
00729                     if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
00730                         h->ref_cache[list][idx] <<= 1;\
00731                         h->mv_cache[list][idx][1] /= 2;\
00732                         h->mvd_cache[list][idx][1] >>=1;\
00733                     }
00734                     MAP_MVS
00735 #undef MAP_F2F
00736                 }else{
00737 #define MAP_F2F(idx, mb_type)\
00738                     if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
00739                         h->ref_cache[list][idx] >>= 1;\
00740                         h->mv_cache[list][idx][1] <<= 1;\
00741                         h->mvd_cache[list][idx][1] <<= 1;\
00742                     }
00743                     MAP_MVS
00744 #undef MAP_F2F
00745                 }
00746             }
00747         }
00748     }
00749 
00750         h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[LTOP]);
00751 }
00752 
00756 static void av_unused decode_mb_skip(H264Context *h){
00757     MpegEncContext * const s = &h->s;
00758     const int mb_xy= h->mb_xy;
00759     int mb_type=0;
00760 
00761     memset(h->non_zero_count[mb_xy], 0, 48);
00762 
00763     if(MB_FIELD)
00764         mb_type|= MB_TYPE_INTERLACED;
00765 
00766     if( h->slice_type_nos == AV_PICTURE_TYPE_B )
00767     {
00768         // just for fill_caches. pred_direct_motion will set the real mb_type
00769         mb_type|= MB_TYPE_L0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
00770         if(h->direct_spatial_mv_pred){
00771             fill_decode_neighbors(h, mb_type);
00772         fill_decode_caches(h, mb_type); //FIXME check what is needed and what not ...
00773         }
00774         ff_h264_pred_direct_motion(h, &mb_type);
00775         mb_type|= MB_TYPE_SKIP;
00776     }
00777     else
00778     {
00779         mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
00780 
00781         fill_decode_neighbors(h, mb_type);
00782         pred_pskip_motion(h);
00783     }
00784 
00785     write_back_motion(h, mb_type);
00786     s->current_picture.f.mb_type[mb_xy]      = mb_type;
00787     s->current_picture.f.qscale_table[mb_xy] = s->qscale;
00788     h->slice_table[ mb_xy ]= h->slice_num;
00789     h->prev_mb_skipped= 1;
00790 }
00791 
00792 #endif /* AVCODEC_H264_MVPRED_H */
Generated on Thu Jul 11 2013 15:38:19 for Libav by doxygen 1.7.1