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