00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
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)
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].f.reference & 3) == poc) {
00091 int cur_ref= mbafi ? (j-16)^field : j;
00092 if(ref1->mbaff)
00093 map[list][2*old_ref + (rfield^field) + 16] = cur_ref;
00094 if(rfield == field || !interl)
00095 map[list][old_ref] = cur_ref;
00096 break;
00097 }
00098 }
00099 }
00100 }
00101 }
00102
00103 void ff_h264_direct_ref_list_init(H264Context * const h){
00104 MpegEncContext * const s = &h->s;
00105 Picture * const ref1 = &h->ref_list[1][0];
00106 Picture * const cur = s->current_picture_ptr;
00107 int list, j, field;
00108 int sidx= (s->picture_structure&1)^1;
00109 int ref1sidx = (ref1->f.reference&1)^1;
00110
00111 for(list=0; list<2; list++){
00112 cur->ref_count[sidx][list] = h->ref_count[list];
00113 for(j=0; j<h->ref_count[list]; j++)
00114 cur->ref_poc[sidx][list][j] = 4 * h->ref_list[list][j].frame_num + (h->ref_list[list][j].f.reference & 3);
00115 }
00116
00117 if(s->picture_structure == PICT_FRAME){
00118 memcpy(cur->ref_count[1], cur->ref_count[0], sizeof(cur->ref_count[0]));
00119 memcpy(cur->ref_poc [1], cur->ref_poc [0], sizeof(cur->ref_poc [0]));
00120 }
00121
00122 cur->mbaff= FRAME_MBAFF;
00123
00124 h->col_fieldoff= 0;
00125 if(s->picture_structure == PICT_FRAME){
00126 int cur_poc = s->current_picture_ptr->poc;
00127 int *col_poc = h->ref_list[1]->field_poc;
00128 h->col_parity= (FFABS(col_poc[0] - cur_poc) >= FFABS(col_poc[1] - cur_poc));
00129 ref1sidx=sidx= h->col_parity;
00130 } else if (!(s->picture_structure & h->ref_list[1][0].f.reference) && !h->ref_list[1][0].mbaff) {
00131 h->col_fieldoff = 2 * h->ref_list[1][0].f.reference - 3;
00132 }
00133
00134 if (cur->f.pict_type != AV_PICTURE_TYPE_B || h->direct_spatial_mv_pred)
00135 return;
00136
00137 for(list=0; list<2; list++){
00138 fill_colmap(h, h->map_col_to_list0, list, sidx, ref1sidx, 0);
00139 if(FRAME_MBAFF)
00140 for(field=0; field<2; field++)
00141 fill_colmap(h, h->map_col_to_list0_field[field], list, field, field, 1);
00142 }
00143 }
00144
00145 static void await_reference_mb_row(H264Context * const h, Picture *ref, int mb_y)
00146 {
00147 int ref_field = ref->f.reference - 1;
00148 int ref_field_picture = ref->field_picture;
00149 int ref_height = 16*h->s.mb_height >> ref_field_picture;
00150
00151 if(!HAVE_THREADS || !(h->s.avctx->active_thread_type&FF_THREAD_FRAME))
00152 return;
00153
00154
00155
00156
00157 ff_thread_await_progress(&ref->f,
00158 FFMIN(16 * mb_y >> ref_field_picture, ref_height - 1),
00159 ref_field_picture && ref_field);
00160 }
00161
00162 static void pred_spatial_direct_motion(H264Context * const h, int *mb_type){
00163 MpegEncContext * const s = &h->s;
00164 int b8_stride = 2;
00165 int b4_stride = h->b_stride;
00166 int mb_xy = h->mb_xy, mb_y = s->mb_y;
00167 int mb_type_col[2];
00168 const int16_t (*l1mv0)[2], (*l1mv1)[2];
00169 const int8_t *l1ref0, *l1ref1;
00170 const int is_b8x8 = IS_8X8(*mb_type);
00171 unsigned int sub_mb_type= MB_TYPE_L0L1;
00172 int i8, i4;
00173 int ref[2];
00174 int mv[2];
00175 int list;
00176
00177 assert(h->ref_list[1][0].f.reference & 3);
00178
00179 await_reference_mb_row(h, &h->ref_list[1][0], s->mb_y + !!IS_INTERLACED(*mb_type));
00180
00181 #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
00182
00183
00184
00185 for(list=0; list<2; list++){
00186 int left_ref = h->ref_cache[list][scan8[0] - 1];
00187 int top_ref = h->ref_cache[list][scan8[0] - 8];
00188 int refc = h->ref_cache[list][scan8[0] - 8 + 4];
00189 const int16_t *C= h->mv_cache[list][ scan8[0] - 8 + 4];
00190 if(refc == PART_NOT_AVAILABLE){
00191 refc = h->ref_cache[list][scan8[0] - 8 - 1];
00192 C = h-> mv_cache[list][scan8[0] - 8 - 1];
00193 }
00194 ref[list] = FFMIN3((unsigned)left_ref, (unsigned)top_ref, (unsigned)refc);
00195 if(ref[list] >= 0){
00196
00197 const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
00198 const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
00199
00200 int match_count= (left_ref==ref[list]) + (top_ref==ref[list]) + (refc==ref[list]);
00201 if(match_count > 1){
00202 mv[list]= pack16to32(mid_pred(A[0], B[0], C[0]),
00203 mid_pred(A[1], B[1], C[1]) );
00204 }else {
00205 assert(match_count==1);
00206 if(left_ref==ref[list]){
00207 mv[list]= AV_RN32A(A);
00208 }else if(top_ref==ref[list]){
00209 mv[list]= AV_RN32A(B);
00210 }else{
00211 mv[list]= AV_RN32A(C);
00212 }
00213 }
00214 }else{
00215 int mask= ~(MB_TYPE_L0 << (2*list));
00216 mv[list] = 0;
00217 ref[list] = -1;
00218 if(!is_b8x8)
00219 *mb_type &= mask;
00220 sub_mb_type &= mask;
00221 }
00222 }
00223 if(ref[0] < 0 && ref[1] < 0){
00224 ref[0] = ref[1] = 0;
00225 if(!is_b8x8)
00226 *mb_type |= MB_TYPE_L0L1;
00227 sub_mb_type |= MB_TYPE_L0L1;
00228 }
00229
00230 if(!(is_b8x8|mv[0]|mv[1])){
00231 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
00232 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
00233 fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
00234 fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
00235 *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;
00236 return;
00237 }
00238
00239 if (IS_INTERLACED(h->ref_list[1][0].f.mb_type[mb_xy])) {
00240 if (!IS_INTERLACED(*mb_type)) {
00241 mb_y = (s->mb_y&~1) + h->col_parity;
00242 mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;
00243 b8_stride = 0;
00244 }else{
00245 mb_y += h->col_fieldoff;
00246 mb_xy += s->mb_stride*h->col_fieldoff;
00247 }
00248 goto single_col;
00249 }else{
00250 if(IS_INTERLACED(*mb_type)){
00251 mb_y = s->mb_y&~1;
00252 mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
00253 mb_type_col[0] = h->ref_list[1][0].f.mb_type[mb_xy];
00254 mb_type_col[1] = h->ref_list[1][0].f.mb_type[mb_xy + s->mb_stride];
00255 b8_stride = 2+4*s->mb_stride;
00256 b4_stride *= 6;
00257 if (IS_INTERLACED(mb_type_col[0]) != IS_INTERLACED(mb_type_col[1])) {
00258 mb_type_col[0] &= ~MB_TYPE_INTERLACED;
00259 mb_type_col[1] &= ~MB_TYPE_INTERLACED;
00260 }
00261
00262 sub_mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2;
00263 if( (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
00264 && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
00265 && !is_b8x8){
00266 *mb_type |= MB_TYPE_16x8 |MB_TYPE_DIRECT2;
00267 }else{
00268 *mb_type |= MB_TYPE_8x8;
00269 }
00270 }else{
00271 single_col:
00272 mb_type_col[0] =
00273 mb_type_col[1] = h->ref_list[1][0].f.mb_type[mb_xy];
00274
00275 sub_mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2;
00276 if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
00277 *mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2;
00278 }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
00279 *mb_type |= MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
00280 }else{
00281 if(!h->sps.direct_8x8_inference_flag){
00282
00283
00284 sub_mb_type += (MB_TYPE_8x8-MB_TYPE_16x16);
00285 }
00286 *mb_type |= MB_TYPE_8x8;
00287 }
00288 }
00289 }
00290
00291 await_reference_mb_row(h, &h->ref_list[1][0], mb_y);
00292
00293 l1mv0 = &h->ref_list[1][0].f.motion_val[0][h->mb2b_xy [mb_xy]];
00294 l1mv1 = &h->ref_list[1][0].f.motion_val[1][h->mb2b_xy [mb_xy]];
00295 l1ref0 = &h->ref_list[1][0].f.ref_index [0][4 * mb_xy];
00296 l1ref1 = &h->ref_list[1][0].f.ref_index [1][4 * mb_xy];
00297 if(!b8_stride){
00298 if(s->mb_y&1){
00299 l1ref0 += 2;
00300 l1ref1 += 2;
00301 l1mv0 += 2*b4_stride;
00302 l1mv1 += 2*b4_stride;
00303 }
00304 }
00305
00306
00307 if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
00308 int n=0;
00309 for(i8=0; i8<4; i8++){
00310 int x8 = i8&1;
00311 int y8 = i8>>1;
00312 int xy8 = x8+y8*b8_stride;
00313 int xy4 = 3*x8+y8*b4_stride;
00314 int a,b;
00315
00316 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
00317 continue;
00318 h->sub_mb_type[i8] = sub_mb_type;
00319
00320 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
00321 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
00322 if(!IS_INTRA(mb_type_col[y8]) && !h->ref_list[1][0].long_ref
00323 && ( (l1ref0[xy8] == 0 && FFABS(l1mv0[xy4][0]) <= 1 && FFABS(l1mv0[xy4][1]) <= 1)
00324 || (l1ref0[xy8] < 0 && l1ref1[xy8] == 0 && FFABS(l1mv1[xy4][0]) <= 1 && FFABS(l1mv1[xy4][1]) <= 1))){
00325 a=b=0;
00326 if(ref[0] > 0)
00327 a= mv[0];
00328 if(ref[1] > 0)
00329 b= mv[1];
00330 n++;
00331 }else{
00332 a= mv[0];
00333 b= mv[1];
00334 }
00335 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, a, 4);
00336 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, b, 4);
00337 }
00338 if(!is_b8x8 && !(n&3))
00339 *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;
00340 }else if(IS_16X16(*mb_type)){
00341 int a,b;
00342
00343 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
00344 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
00345 if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref
00346 && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
00347 || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
00348 && h->x264_build>33U))){
00349 a=b=0;
00350 if(ref[0] > 0)
00351 a= mv[0];
00352 if(ref[1] > 0)
00353 b= mv[1];
00354 }else{
00355 a= mv[0];
00356 b= mv[1];
00357 }
00358 fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
00359 fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
00360 }else{
00361 int n=0;
00362 for(i8=0; i8<4; i8++){
00363 const int x8 = i8&1;
00364 const int y8 = i8>>1;
00365
00366 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
00367 continue;
00368 h->sub_mb_type[i8] = sub_mb_type;
00369
00370 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, mv[0], 4);
00371 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, mv[1], 4);
00372 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
00373 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
00374
00375 assert(b8_stride==2);
00376
00377 if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref && ( l1ref0[i8] == 0
00378 || (l1ref0[i8] < 0 && l1ref1[i8] == 0
00379 && h->x264_build>33U))){
00380 const int16_t (*l1mv)[2]= l1ref0[i8] == 0 ? l1mv0 : l1mv1;
00381 if(IS_SUB_8X8(sub_mb_type)){
00382 const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
00383 if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
00384 if(ref[0] == 0)
00385 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
00386 if(ref[1] == 0)
00387 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
00388 n+=4;
00389 }
00390 }else{
00391 int m=0;
00392 for(i4=0; i4<4; i4++){
00393 const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
00394 if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
00395 if(ref[0] == 0)
00396 AV_ZERO32(h->mv_cache[0][scan8[i8*4+i4]]);
00397 if(ref[1] == 0)
00398 AV_ZERO32(h->mv_cache[1][scan8[i8*4+i4]]);
00399 m++;
00400 }
00401 }
00402 if(!(m&3))
00403 h->sub_mb_type[i8]+= MB_TYPE_16x16 - MB_TYPE_8x8;
00404 n+=m;
00405 }
00406 }
00407 }
00408 if(!is_b8x8 && !(n&15))
00409 *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;
00410 }
00411 }
00412
00413 static void pred_temp_direct_motion(H264Context * const h, int *mb_type){
00414 MpegEncContext * const s = &h->s;
00415 int b8_stride = 2;
00416 int b4_stride = h->b_stride;
00417 int mb_xy = h->mb_xy, mb_y = s->mb_y;
00418 int mb_type_col[2];
00419 const int16_t (*l1mv0)[2], (*l1mv1)[2];
00420 const int8_t *l1ref0, *l1ref1;
00421 const int is_b8x8 = IS_8X8(*mb_type);
00422 unsigned int sub_mb_type;
00423 int i8, i4;
00424
00425 assert(h->ref_list[1][0].f.reference & 3);
00426
00427 await_reference_mb_row(h, &h->ref_list[1][0], s->mb_y + !!IS_INTERLACED(*mb_type));
00428
00429 if (IS_INTERLACED(h->ref_list[1][0].f.mb_type[mb_xy])) {
00430 if (!IS_INTERLACED(*mb_type)) {
00431 mb_y = (s->mb_y&~1) + h->col_parity;
00432 mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;
00433 b8_stride = 0;
00434 }else{
00435 mb_y += h->col_fieldoff;
00436 mb_xy += s->mb_stride*h->col_fieldoff;
00437 }
00438 goto single_col;
00439 }else{
00440 if(IS_INTERLACED(*mb_type)){
00441 mb_y = s->mb_y&~1;
00442 mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
00443 mb_type_col[0] = h->ref_list[1][0].f.mb_type[mb_xy];
00444 mb_type_col[1] = h->ref_list[1][0].f.mb_type[mb_xy + s->mb_stride];
00445 b8_stride = 2+4*s->mb_stride;
00446 b4_stride *= 6;
00447 if (IS_INTERLACED(mb_type_col[0]) != IS_INTERLACED(mb_type_col[1])) {
00448 mb_type_col[0] &= ~MB_TYPE_INTERLACED;
00449 mb_type_col[1] &= ~MB_TYPE_INTERLACED;
00450 }
00451
00452 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2;
00453
00454 if( (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
00455 && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
00456 && !is_b8x8){
00457 *mb_type |= MB_TYPE_16x8 |MB_TYPE_L0L1|MB_TYPE_DIRECT2;
00458 }else{
00459 *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
00460 }
00461 }else{
00462 single_col:
00463 mb_type_col[0] =
00464 mb_type_col[1] = h->ref_list[1][0].f.mb_type[mb_xy];
00465
00466 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2;
00467 if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
00468 *mb_type |= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2;
00469 }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
00470 *mb_type |= MB_TYPE_L0L1|MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
00471 }else{
00472 if(!h->sps.direct_8x8_inference_flag){
00473
00474
00475 sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2;
00476 }
00477 *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
00478 }
00479 }
00480 }
00481
00482 await_reference_mb_row(h, &h->ref_list[1][0], mb_y);
00483
00484 l1mv0 = &h->ref_list[1][0].f.motion_val[0][h->mb2b_xy [mb_xy]];
00485 l1mv1 = &h->ref_list[1][0].f.motion_val[1][h->mb2b_xy [mb_xy]];
00486 l1ref0 = &h->ref_list[1][0].f.ref_index [0][4 * mb_xy];
00487 l1ref1 = &h->ref_list[1][0].f.ref_index [1][4 * mb_xy];
00488 if(!b8_stride){
00489 if(s->mb_y&1){
00490 l1ref0 += 2;
00491 l1ref1 += 2;
00492 l1mv0 += 2*b4_stride;
00493 l1mv1 += 2*b4_stride;
00494 }
00495 }
00496
00497 {
00498 const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
00499 const int *dist_scale_factor = h->dist_scale_factor;
00500 int ref_offset;
00501
00502 if(FRAME_MBAFF && IS_INTERLACED(*mb_type)){
00503 map_col_to_list0[0] = h->map_col_to_list0_field[s->mb_y&1][0];
00504 map_col_to_list0[1] = h->map_col_to_list0_field[s->mb_y&1][1];
00505 dist_scale_factor =h->dist_scale_factor_field[s->mb_y&1];
00506 }
00507 ref_offset = (h->ref_list[1][0].mbaff<<4) & (mb_type_col[0]>>3);
00508
00509 if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
00510 int y_shift = 2*!IS_INTERLACED(*mb_type);
00511 assert(h->sps.direct_8x8_inference_flag);
00512
00513 for(i8=0; i8<4; i8++){
00514 const int x8 = i8&1;
00515 const int y8 = i8>>1;
00516 int ref0, scale;
00517 const int16_t (*l1mv)[2]= l1mv0;
00518
00519 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
00520 continue;
00521 h->sub_mb_type[i8] = sub_mb_type;
00522
00523 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
00524 if(IS_INTRA(mb_type_col[y8])){
00525 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
00526 fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
00527 fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
00528 continue;
00529 }
00530
00531 ref0 = l1ref0[x8 + y8*b8_stride];
00532 if(ref0 >= 0)
00533 ref0 = map_col_to_list0[0][ref0 + ref_offset];
00534 else{
00535 ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
00536 l1mv= l1mv1;
00537 }
00538 scale = dist_scale_factor[ref0];
00539 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
00540
00541 {
00542 const int16_t *mv_col = l1mv[x8*3 + y8*b4_stride];
00543 int my_col = (mv_col[1]<<y_shift)/2;
00544 int mx = (scale * mv_col[0] + 128) >> 8;
00545 int my = (scale * my_col + 128) >> 8;
00546 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
00547 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
00548 }
00549 }
00550 return;
00551 }
00552
00553
00554
00555 if(IS_16X16(*mb_type)){
00556 int ref, mv0, mv1;
00557
00558 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
00559 if(IS_INTRA(mb_type_col[0])){
00560 ref=mv0=mv1=0;
00561 }else{
00562 const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
00563 : map_col_to_list0[1][l1ref1[0] + ref_offset];
00564 const int scale = dist_scale_factor[ref0];
00565 const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
00566 int mv_l0[2];
00567 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
00568 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
00569 ref= ref0;
00570 mv0= pack16to32(mv_l0[0],mv_l0[1]);
00571 mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
00572 }
00573 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
00574 fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
00575 fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
00576 }else{
00577 for(i8=0; i8<4; i8++){
00578 const int x8 = i8&1;
00579 const int y8 = i8>>1;
00580 int ref0, scale;
00581 const int16_t (*l1mv)[2]= l1mv0;
00582
00583 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
00584 continue;
00585 h->sub_mb_type[i8] = sub_mb_type;
00586 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
00587 if(IS_INTRA(mb_type_col[0])){
00588 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
00589 fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
00590 fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
00591 continue;
00592 }
00593
00594 assert(b8_stride == 2);
00595 ref0 = l1ref0[i8];
00596 if(ref0 >= 0)
00597 ref0 = map_col_to_list0[0][ref0 + ref_offset];
00598 else{
00599 ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
00600 l1mv= l1mv1;
00601 }
00602 scale = dist_scale_factor[ref0];
00603
00604 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
00605 if(IS_SUB_8X8(sub_mb_type)){
00606 const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
00607 int mx = (scale * mv_col[0] + 128) >> 8;
00608 int my = (scale * mv_col[1] + 128) >> 8;
00609 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
00610 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
00611 }else
00612 for(i4=0; i4<4; i4++){
00613 const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
00614 int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
00615 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
00616 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
00617 AV_WN32A(h->mv_cache[1][scan8[i8*4+i4]],
00618 pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]));
00619 }
00620 }
00621 }
00622 }
00623 }
00624
00625 void ff_h264_pred_direct_motion(H264Context * const h, int *mb_type){
00626 if(h->direct_spatial_mv_pred){
00627 pred_spatial_direct_motion(h, mb_type);
00628 }else{
00629 pred_temp_direct_motion(h, mb_type);
00630 }
00631 }