Libav 0.7.1
libavcodec/h264_direct.c
Go to the documentation of this file.
00001 /*
00002  * H.26L/H.264/AVC/JVT/14496-10/... direct mb/block decoding
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 #include "internal.h"
00029 #include "dsputil.h"
00030 #include "avcodec.h"
00031 #include "mpegvideo.h"
00032 #include "h264.h"
00033 #include "rectangle.h"
00034 #include "thread.h"
00035 
00036 //#undef NDEBUG
00037 #include <assert.h>
00038 
00039 
00040 static int get_scale_factor(H264Context * const h, int poc, int poc1, int i){
00041     int poc0 = h->ref_list[0][i].poc;
00042     int td = av_clip(poc1 - poc0, -128, 127);
00043     if(td == 0 || h->ref_list[0][i].long_ref){
00044         return 256;
00045     }else{
00046         int tb = av_clip(poc - poc0, -128, 127);
00047         int tx = (16384 + (FFABS(td) >> 1)) / td;
00048         return av_clip((tb*tx + 32) >> 6, -1024, 1023);
00049     }
00050 }
00051 
00052 void ff_h264_direct_dist_scale_factor(H264Context * const h){
00053     MpegEncContext * const s = &h->s;
00054     const int poc = h->s.current_picture_ptr->field_poc[ s->picture_structure == PICT_BOTTOM_FIELD ];
00055     const int poc1 = h->ref_list[1][0].poc;
00056     int i, field;
00057     for(field=0; field<2; field++){
00058         const int poc  = h->s.current_picture_ptr->field_poc[field];
00059         const int poc1 = h->ref_list[1][0].field_poc[field];
00060         for(i=0; i < 2*h->ref_count[0]; i++)
00061             h->dist_scale_factor_field[field][i^field] = get_scale_factor(h, poc, poc1, i+16);
00062     }
00063 
00064     for(i=0; i<h->ref_count[0]; i++){
00065         h->dist_scale_factor[i] = get_scale_factor(h, poc, poc1, i);
00066     }
00067 }
00068 
00069 static void fill_colmap(H264Context *h, int map[2][16+32], int list, int field, int colfield, int mbafi){
00070     MpegEncContext * const s = &h->s;
00071     Picture * const ref1 = &h->ref_list[1][0];
00072     int j, old_ref, rfield;
00073     int start= mbafi ? 16                      : 0;
00074     int end  = mbafi ? 16+2*h->ref_count[0]    : h->ref_count[0];
00075     int interl= mbafi || s->picture_structure != PICT_FRAME;
00076 
00077     /* bogus; fills in for missing frames */
00078     memset(map[list], 0, sizeof(map[list]));
00079 
00080     for(rfield=0; rfield<2; rfield++){
00081         for(old_ref=0; old_ref<ref1->ref_count[colfield][list]; old_ref++){
00082             int poc = ref1->ref_poc[colfield][list][old_ref];
00083 
00084             if     (!interl)
00085                 poc |= 3;
00086             else if( interl && (poc&3) == 3) //FIXME store all MBAFF references so this isnt needed
00087                 poc= (poc&~3) + rfield + 1;
00088 
00089             for(j=start; j<end; j++){
00090                 if(4*h->ref_list[0][j].frame_num + (h->ref_list[0][j].reference&3) == poc){
00091                     int cur_ref= mbafi ? (j-16)^field : j;
00092                     map[list][2*old_ref + (rfield^field) + 16] = cur_ref;
00093                     if(rfield == field || !interl)
00094                         map[list][old_ref] = cur_ref;
00095                     break;
00096                 }
00097             }
00098         }
00099     }
00100 }
00101 
00102 void ff_h264_direct_ref_list_init(H264Context * const h){
00103     MpegEncContext * const s = &h->s;
00104     Picture * const ref1 = &h->ref_list[1][0];
00105     Picture * const cur = s->current_picture_ptr;
00106     int list, j, field;
00107     int sidx= (s->picture_structure&1)^1;
00108     int ref1sidx= (ref1->reference&1)^1;
00109 
00110     for(list=0; list<2; list++){
00111         cur->ref_count[sidx][list] = h->ref_count[list];
00112         for(j=0; j<h->ref_count[list]; j++)
00113             cur->ref_poc[sidx][list][j] = 4*h->ref_list[list][j].frame_num + (h->ref_list[list][j].reference&3);
00114     }
00115 
00116     if(s->picture_structure == PICT_FRAME){
00117         memcpy(cur->ref_count[1], cur->ref_count[0], sizeof(cur->ref_count[0]));
00118         memcpy(cur->ref_poc  [1], cur->ref_poc  [0], sizeof(cur->ref_poc  [0]));
00119     }
00120 
00121     cur->mbaff= FRAME_MBAFF;
00122 
00123     h->col_fieldoff= 0;
00124     if(s->picture_structure == PICT_FRAME){
00125         int cur_poc = s->current_picture_ptr->poc;
00126         int *col_poc = h->ref_list[1]->field_poc;
00127         h->col_parity= (FFABS(col_poc[0] - cur_poc) >= FFABS(col_poc[1] - cur_poc));
00128         ref1sidx=sidx= h->col_parity;
00129     }else if(!(s->picture_structure & h->ref_list[1][0].reference) && !h->ref_list[1][0].mbaff){ // FL -> FL & differ parity
00130         h->col_fieldoff= 2*(h->ref_list[1][0].reference) - 3;
00131     }
00132 
00133     if(cur->pict_type != AV_PICTURE_TYPE_B || h->direct_spatial_mv_pred)
00134         return;
00135 
00136     for(list=0; list<2; list++){
00137         fill_colmap(h, h->map_col_to_list0, list, sidx, ref1sidx, 0);
00138         if(FRAME_MBAFF)
00139         for(field=0; field<2; field++)
00140             fill_colmap(h, h->map_col_to_list0_field[field], list, field, field, 1);
00141     }
00142 }
00143 
00144 static void await_reference_mb_row(H264Context * const h, Picture *ref, int mb_y)
00145 {
00146     int ref_field = ref->reference - 1;
00147     int ref_field_picture = ref->field_picture;
00148     int ref_height = 16*h->s.mb_height >> ref_field_picture;
00149 
00150     if(!HAVE_PTHREADS || !(h->s.avctx->active_thread_type&FF_THREAD_FRAME))
00151         return;
00152 
00153     //FIXME it can be safe to access mb stuff
00154     //even if pixels aren't deblocked yet
00155 
00156     ff_thread_await_progress((AVFrame*)ref, FFMIN(16*mb_y >> ref_field_picture, ref_height-1),
00157                              ref_field_picture && ref_field);
00158 }
00159 
00160 static void pred_spatial_direct_motion(H264Context * const h, int *mb_type){
00161     MpegEncContext * const s = &h->s;
00162     int b8_stride = 2;
00163     int b4_stride = h->b_stride;
00164     int mb_xy = h->mb_xy, mb_y = s->mb_y;
00165     int mb_type_col[2];
00166     const int16_t (*l1mv0)[2], (*l1mv1)[2];
00167     const int8_t *l1ref0, *l1ref1;
00168     const int is_b8x8 = IS_8X8(*mb_type);
00169     unsigned int sub_mb_type= MB_TYPE_L0L1;
00170     int i8, i4;
00171     int ref[2];
00172     int mv[2];
00173     int list;
00174 
00175     assert(h->ref_list[1][0].reference&3);
00176 
00177     await_reference_mb_row(h, &h->ref_list[1][0], s->mb_y + !!IS_INTERLACED(*mb_type));
00178 
00179 #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
00180 
00181 
00182     /* ref = min(neighbors) */
00183     for(list=0; list<2; list++){
00184         int left_ref = h->ref_cache[list][scan8[0] - 1];
00185         int top_ref  = h->ref_cache[list][scan8[0] - 8];
00186         int refc = h->ref_cache[list][scan8[0] - 8 + 4];
00187         const int16_t *C= h->mv_cache[list][ scan8[0] - 8 + 4];
00188         if(refc == PART_NOT_AVAILABLE){
00189             refc = h->ref_cache[list][scan8[0] - 8 - 1];
00190             C    = h-> mv_cache[list][scan8[0] - 8 - 1];
00191         }
00192         ref[list] = FFMIN3((unsigned)left_ref, (unsigned)top_ref, (unsigned)refc);
00193         if(ref[list] >= 0){
00194             //this is just pred_motion() but with the cases removed that cannot happen for direct blocks
00195             const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
00196             const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
00197 
00198             int match_count= (left_ref==ref[list]) + (top_ref==ref[list]) + (refc==ref[list]);
00199             if(match_count > 1){ //most common
00200                 mv[list]= pack16to32(mid_pred(A[0], B[0], C[0]),
00201                                      mid_pred(A[1], B[1], C[1]) );
00202             }else {
00203                 assert(match_count==1);
00204                 if(left_ref==ref[list]){
00205                     mv[list]= AV_RN32A(A);
00206                 }else if(top_ref==ref[list]){
00207                     mv[list]= AV_RN32A(B);
00208                 }else{
00209                     mv[list]= AV_RN32A(C);
00210                 }
00211             }
00212         }else{
00213             int mask= ~(MB_TYPE_L0 << (2*list));
00214             mv[list] = 0;
00215             ref[list] = -1;
00216             if(!is_b8x8)
00217                 *mb_type &= mask;
00218             sub_mb_type &= mask;
00219         }
00220     }
00221     if(ref[0] < 0 && ref[1] < 0){
00222         ref[0] = ref[1] = 0;
00223         if(!is_b8x8)
00224             *mb_type |= MB_TYPE_L0L1;
00225         sub_mb_type |= MB_TYPE_L0L1;
00226     }
00227 
00228     if(!(is_b8x8|mv[0]|mv[1])){
00229         fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
00230         fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
00231         fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
00232         fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
00233         *mb_type= (*mb_type & ~(MB_TYPE_8x8|MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_P1L0|MB_TYPE_P1L1))|MB_TYPE_16x16|MB_TYPE_DIRECT2;
00234         return;
00235     }
00236 
00237     if(IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])){ // AFL/AFR/FR/FL -> AFL/FL
00238         if(!IS_INTERLACED(*mb_type)){                    //     AFR/FR    -> AFL/FL
00239             mb_y = (s->mb_y&~1) + h->col_parity;
00240             mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;
00241             b8_stride = 0;
00242         }else{
00243             mb_y  += h->col_fieldoff;
00244             mb_xy += s->mb_stride*h->col_fieldoff; // non zero for FL -> FL & differ parity
00245         }
00246         goto single_col;
00247     }else{                                               // AFL/AFR/FR/FL -> AFR/FR
00248         if(IS_INTERLACED(*mb_type)){                     // AFL       /FL -> AFR/FR
00249             mb_y = s->mb_y&~1;
00250             mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
00251             mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
00252             mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + s->mb_stride];
00253             b8_stride = 2+4*s->mb_stride;
00254             b4_stride *= 6;
00255 
00256             sub_mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
00257             if(    (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
00258                 && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
00259                 && !is_b8x8){
00260                 *mb_type   |= MB_TYPE_16x8 |MB_TYPE_DIRECT2; /* B_16x8 */
00261             }else{
00262                 *mb_type   |= MB_TYPE_8x8;
00263             }
00264         }else{                                           //     AFR/FR    -> AFR/FR
00265 single_col:
00266             mb_type_col[0] =
00267             mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy];
00268 
00269             sub_mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
00270             if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
00271                 *mb_type   |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_16x16 */
00272             }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
00273                 *mb_type   |= MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
00274             }else{
00275                 if(!h->sps.direct_8x8_inference_flag){
00276                     /* FIXME save sub mb types from previous frames (or derive from MVs)
00277                     * so we know exactly what block size to use */
00278                     sub_mb_type += (MB_TYPE_8x8-MB_TYPE_16x16); /* B_SUB_4x4 */
00279                 }
00280                 *mb_type   |= MB_TYPE_8x8;
00281             }
00282         }
00283     }
00284 
00285     await_reference_mb_row(h, &h->ref_list[1][0], mb_y);
00286 
00287     l1mv0  = &h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
00288     l1mv1  = &h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
00289     l1ref0 = &h->ref_list[1][0].ref_index [0][4*mb_xy];
00290     l1ref1 = &h->ref_list[1][0].ref_index [1][4*mb_xy];
00291     if(!b8_stride){
00292         if(s->mb_y&1){
00293             l1ref0 += 2;
00294             l1ref1 += 2;
00295             l1mv0  +=  2*b4_stride;
00296             l1mv1  +=  2*b4_stride;
00297         }
00298     }
00299 
00300 
00301         if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
00302             int n=0;
00303             for(i8=0; i8<4; i8++){
00304                 int x8 = i8&1;
00305                 int y8 = i8>>1;
00306                 int xy8 = x8+y8*b8_stride;
00307                 int xy4 = 3*x8+y8*b4_stride;
00308                 int a,b;
00309 
00310                 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
00311                     continue;
00312                 h->sub_mb_type[i8] = sub_mb_type;
00313 
00314                 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
00315                 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
00316                 if(!IS_INTRA(mb_type_col[y8]) && !h->ref_list[1][0].long_ref
00317                    && (   (l1ref0[xy8] == 0 && FFABS(l1mv0[xy4][0]) <= 1 && FFABS(l1mv0[xy4][1]) <= 1)
00318                        || (l1ref0[xy8]  < 0 && l1ref1[xy8] == 0 && FFABS(l1mv1[xy4][0]) <= 1 && FFABS(l1mv1[xy4][1]) <= 1))){
00319                     a=b=0;
00320                     if(ref[0] > 0)
00321                         a= mv[0];
00322                     if(ref[1] > 0)
00323                         b= mv[1];
00324                     n++;
00325                 }else{
00326                     a= mv[0];
00327                     b= mv[1];
00328                 }
00329                 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, a, 4);
00330                 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, b, 4);
00331             }
00332             if(!is_b8x8 && !(n&3))
00333                 *mb_type= (*mb_type & ~(MB_TYPE_8x8|MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_P1L0|MB_TYPE_P1L1))|MB_TYPE_16x16|MB_TYPE_DIRECT2;
00334         }else if(IS_16X16(*mb_type)){
00335             int a,b;
00336 
00337             fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
00338             fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
00339             if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref
00340                && (   (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
00341                    || (l1ref0[0]  < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
00342                        && h->x264_build>33U))){
00343                 a=b=0;
00344                 if(ref[0] > 0)
00345                     a= mv[0];
00346                 if(ref[1] > 0)
00347                     b= mv[1];
00348             }else{
00349                 a= mv[0];
00350                 b= mv[1];
00351             }
00352             fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
00353             fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
00354         }else{
00355             int n=0;
00356             for(i8=0; i8<4; i8++){
00357                 const int x8 = i8&1;
00358                 const int y8 = i8>>1;
00359 
00360                 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
00361                     continue;
00362                 h->sub_mb_type[i8] = sub_mb_type;
00363 
00364                 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, mv[0], 4);
00365                 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, mv[1], 4);
00366                 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
00367                 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
00368 
00369                 assert(b8_stride==2);
00370                 /* col_zero_flag */
00371                 if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref && (   l1ref0[i8] == 0
00372                                               || (l1ref0[i8] < 0 && l1ref1[i8] == 0
00373                                                   && h->x264_build>33U))){
00374                     const int16_t (*l1mv)[2]= l1ref0[i8] == 0 ? l1mv0 : l1mv1;
00375                     if(IS_SUB_8X8(sub_mb_type)){
00376                         const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
00377                         if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
00378                             if(ref[0] == 0)
00379                                 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
00380                             if(ref[1] == 0)
00381                                 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
00382                             n+=4;
00383                         }
00384                     }else{
00385                         int m=0;
00386                     for(i4=0; i4<4; i4++){
00387                         const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
00388                         if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
00389                             if(ref[0] == 0)
00390                                 AV_ZERO32(h->mv_cache[0][scan8[i8*4+i4]]);
00391                             if(ref[1] == 0)
00392                                 AV_ZERO32(h->mv_cache[1][scan8[i8*4+i4]]);
00393                             m++;
00394                         }
00395                     }
00396                     if(!(m&3))
00397                         h->sub_mb_type[i8]+= MB_TYPE_16x16 - MB_TYPE_8x8;
00398                     n+=m;
00399                     }
00400                 }
00401             }
00402             if(!is_b8x8 && !(n&15))
00403                 *mb_type= (*mb_type & ~(MB_TYPE_8x8|MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_P1L0|MB_TYPE_P1L1))|MB_TYPE_16x16|MB_TYPE_DIRECT2;
00404         }
00405 }
00406 
00407 static void pred_temp_direct_motion(H264Context * const h, int *mb_type){
00408     MpegEncContext * const s = &h->s;
00409     int b8_stride = 2;
00410     int b4_stride = h->b_stride;
00411     int mb_xy = h->mb_xy, mb_y = s->mb_y;
00412     int mb_type_col[2];
00413     const int16_t (*l1mv0)[2], (*l1mv1)[2];
00414     const int8_t *l1ref0, *l1ref1;
00415     const int is_b8x8 = IS_8X8(*mb_type);
00416     unsigned int sub_mb_type;
00417     int i8, i4;
00418 
00419     assert(h->ref_list[1][0].reference&3);
00420 
00421     await_reference_mb_row(h, &h->ref_list[1][0], s->mb_y + !!IS_INTERLACED(*mb_type));
00422 
00423     if(IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])){ // AFL/AFR/FR/FL -> AFL/FL
00424         if(!IS_INTERLACED(*mb_type)){                    //     AFR/FR    -> AFL/FL
00425             mb_y = (s->mb_y&~1) + h->col_parity;
00426             mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;
00427             b8_stride = 0;
00428         }else{
00429             mb_y  += h->col_fieldoff;
00430             mb_xy += s->mb_stride*h->col_fieldoff; // non zero for FL -> FL & differ parity
00431         }
00432         goto single_col;
00433     }else{                                               // AFL/AFR/FR/FL -> AFR/FR
00434         if(IS_INTERLACED(*mb_type)){                     // AFL       /FL -> AFR/FR
00435             mb_y = s->mb_y&~1;
00436             mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
00437             mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
00438             mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + s->mb_stride];
00439             b8_stride = 2+4*s->mb_stride;
00440             b4_stride *= 6;
00441 
00442             sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
00443 
00444             if(    (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
00445                 && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
00446                 && !is_b8x8){
00447                 *mb_type   |= MB_TYPE_16x8 |MB_TYPE_L0L1|MB_TYPE_DIRECT2; /* B_16x8 */
00448             }else{
00449                 *mb_type   |= MB_TYPE_8x8|MB_TYPE_L0L1;
00450             }
00451         }else{                                           //     AFR/FR    -> AFR/FR
00452 single_col:
00453             mb_type_col[0] =
00454             mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy];
00455 
00456             sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
00457             if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
00458                 *mb_type   |= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
00459             }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
00460                 *mb_type   |= MB_TYPE_L0L1|MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
00461             }else{
00462                 if(!h->sps.direct_8x8_inference_flag){
00463                     /* FIXME save sub mb types from previous frames (or derive from MVs)
00464                     * so we know exactly what block size to use */
00465                     sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
00466                 }
00467                 *mb_type   |= MB_TYPE_8x8|MB_TYPE_L0L1;
00468             }
00469         }
00470     }
00471 
00472     await_reference_mb_row(h, &h->ref_list[1][0], mb_y);
00473 
00474     l1mv0  = &h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
00475     l1mv1  = &h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
00476     l1ref0 = &h->ref_list[1][0].ref_index [0][4*mb_xy];
00477     l1ref1 = &h->ref_list[1][0].ref_index [1][4*mb_xy];
00478     if(!b8_stride){
00479         if(s->mb_y&1){
00480             l1ref0 += 2;
00481             l1ref1 += 2;
00482             l1mv0  +=  2*b4_stride;
00483             l1mv1  +=  2*b4_stride;
00484         }
00485     }
00486 
00487     {
00488         const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
00489         const int *dist_scale_factor = h->dist_scale_factor;
00490         int ref_offset;
00491 
00492         if(FRAME_MBAFF && IS_INTERLACED(*mb_type)){
00493             map_col_to_list0[0] = h->map_col_to_list0_field[s->mb_y&1][0];
00494             map_col_to_list0[1] = h->map_col_to_list0_field[s->mb_y&1][1];
00495             dist_scale_factor   =h->dist_scale_factor_field[s->mb_y&1];
00496         }
00497         ref_offset = (h->ref_list[1][0].mbaff<<4) & (mb_type_col[0]>>3); //if(h->ref_list[1][0].mbaff && IS_INTERLACED(mb_type_col[0])) ref_offset=16 else 0
00498 
00499         if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
00500             int y_shift  = 2*!IS_INTERLACED(*mb_type);
00501             assert(h->sps.direct_8x8_inference_flag);
00502 
00503             for(i8=0; i8<4; i8++){
00504                 const int x8 = i8&1;
00505                 const int y8 = i8>>1;
00506                 int ref0, scale;
00507                 const int16_t (*l1mv)[2]= l1mv0;
00508 
00509                 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
00510                     continue;
00511                 h->sub_mb_type[i8] = sub_mb_type;
00512 
00513                 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
00514                 if(IS_INTRA(mb_type_col[y8])){
00515                     fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
00516                     fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
00517                     fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
00518                     continue;
00519                 }
00520 
00521                 ref0 = l1ref0[x8 + y8*b8_stride];
00522                 if(ref0 >= 0)
00523                     ref0 = map_col_to_list0[0][ref0 + ref_offset];
00524                 else{
00525                     ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
00526                     l1mv= l1mv1;
00527                 }
00528                 scale = dist_scale_factor[ref0];
00529                 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
00530 
00531                 {
00532                     const int16_t *mv_col = l1mv[x8*3 + y8*b4_stride];
00533                     int my_col = (mv_col[1]<<y_shift)/2;
00534                     int mx = (scale * mv_col[0] + 128) >> 8;
00535                     int my = (scale * my_col + 128) >> 8;
00536                     fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
00537                     fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
00538                 }
00539             }
00540             return;
00541         }
00542 
00543         /* one-to-one mv scaling */
00544 
00545         if(IS_16X16(*mb_type)){
00546             int ref, mv0, mv1;
00547 
00548             fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
00549             if(IS_INTRA(mb_type_col[0])){
00550                 ref=mv0=mv1=0;
00551             }else{
00552                 const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
00553                                                 : map_col_to_list0[1][l1ref1[0] + ref_offset];
00554                 const int scale = dist_scale_factor[ref0];
00555                 const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
00556                 int mv_l0[2];
00557                 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
00558                 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
00559                 ref= ref0;
00560                 mv0= pack16to32(mv_l0[0],mv_l0[1]);
00561                 mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
00562             }
00563             fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
00564             fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
00565             fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
00566         }else{
00567             for(i8=0; i8<4; i8++){
00568                 const int x8 = i8&1;
00569                 const int y8 = i8>>1;
00570                 int ref0, scale;
00571                 const int16_t (*l1mv)[2]= l1mv0;
00572 
00573                 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
00574                     continue;
00575                 h->sub_mb_type[i8] = sub_mb_type;
00576                 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
00577                 if(IS_INTRA(mb_type_col[0])){
00578                     fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
00579                     fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
00580                     fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
00581                     continue;
00582                 }
00583 
00584                 assert(b8_stride == 2);
00585                 ref0 = l1ref0[i8];
00586                 if(ref0 >= 0)
00587                     ref0 = map_col_to_list0[0][ref0 + ref_offset];
00588                 else{
00589                     ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
00590                     l1mv= l1mv1;
00591                 }
00592                 scale = dist_scale_factor[ref0];
00593 
00594                 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
00595                 if(IS_SUB_8X8(sub_mb_type)){
00596                     const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
00597                     int mx = (scale * mv_col[0] + 128) >> 8;
00598                     int my = (scale * mv_col[1] + 128) >> 8;
00599                     fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
00600                     fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
00601                 }else
00602                 for(i4=0; i4<4; i4++){
00603                     const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
00604                     int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
00605                     mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
00606                     mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
00607                     AV_WN32A(h->mv_cache[1][scan8[i8*4+i4]],
00608                         pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]));
00609                 }
00610             }
00611         }
00612     }
00613 }
00614 
00615 void ff_h264_pred_direct_motion(H264Context * const h, int *mb_type){
00616     if(h->direct_spatial_mv_pred){
00617         pred_spatial_direct_motion(h, mb_type);
00618     }else{
00619         pred_temp_direct_motion(h, mb_type);
00620     }
00621 }