FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
h264_direct.c
Go to the documentation of this file.
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... direct mb/block decoding
3  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * H.264 / AVC / MPEG4 part10 direct mb/block decoding.
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27 
28 #include "internal.h"
29 #include "avcodec.h"
30 #include "h264.h"
31 #include "mpegutils.h"
32 #include "rectangle.h"
33 #include "thread.h"
34 
35 #include <assert.h>
36 
37 
38 static int get_scale_factor(H264Context * const h, int poc, int poc1, int i){
39  int poc0 = h->ref_list[0][i].poc;
40  int td = av_clip(poc1 - poc0, -128, 127);
41  if(td == 0 || h->ref_list[0][i].long_ref){
42  return 256;
43  }else{
44  int tb = av_clip(poc - poc0, -128, 127);
45  int tx = (16384 + (FFABS(td) >> 1)) / td;
46  return av_clip((tb*tx + 32) >> 6, -1024, 1023);
47  }
48 }
49 
51  const int poc = h->picture_structure == PICT_FRAME ?
52  h->cur_pic_ptr->poc :
54  const int poc1 = h->ref_list[1][0].poc;
55  int i, field;
56 
57  if (FRAME_MBAFF(h))
58  for (field = 0; field < 2; field++){
59  const int poc = h->cur_pic_ptr->field_poc[field];
60  const int poc1 = h->ref_list[1][0].field_poc[field];
61  for (i = 0; i < 2 * h->ref_count[0]; i++)
62  h->dist_scale_factor_field[field][i^field] =
63  get_scale_factor(h, poc, poc1, i+16);
64  }
65 
66  for (i = 0; i < h->ref_count[0]; i++){
67  h->dist_scale_factor[i] = get_scale_factor(h, poc, poc1, i);
68  }
69 }
70 
71 static void fill_colmap(H264Context *h, int map[2][16+32], int list, int field, int colfield, int mbafi){
72  H264Picture * const ref1 = &h->ref_list[1][0];
73  int j, old_ref, rfield;
74  int start= mbafi ? 16 : 0;
75  int end = mbafi ? 16+2*h->ref_count[0] : h->ref_count[0];
76  int interl= mbafi || h->picture_structure != PICT_FRAME;
77 
78  /* bogus; fills in for missing frames */
79  memset(map[list], 0, sizeof(map[list]));
80 
81  for(rfield=0; rfield<2; rfield++){
82  for(old_ref=0; old_ref<ref1->ref_count[colfield][list]; old_ref++){
83  int poc = ref1->ref_poc[colfield][list][old_ref];
84 
85  if (!interl)
86  poc |= 3;
87  else if( interl && (poc&3) == 3) // FIXME: store all MBAFF references so this is not needed
88  poc= (poc&~3) + rfield + 1;
89 
90  for(j=start; j<end; j++){
91  if (4 * h->ref_list[0][j].frame_num + (h->ref_list[0][j].reference & 3) == poc) {
92  int cur_ref= mbafi ? (j-16)^field : j;
93  if (ref1->mbaff)
94  map[list][2 * old_ref + (rfield^field) + 16] = cur_ref;
95  if(rfield == field || !interl)
96  map[list][old_ref] = cur_ref;
97  break;
98  }
99  }
100  }
101  }
102 }
103 
105  H264Picture * const ref1 = &h->ref_list[1][0];
106  H264Picture * const cur = h->cur_pic_ptr;
107  int list, j, field;
108  int sidx= (h->picture_structure&1)^1;
109  int ref1sidx = (ref1->reference&1)^1;
110 
111  for(list=0; list<2; list++){
112  cur->ref_count[sidx][list] = h->ref_count[list];
113  for(j=0; j<h->ref_count[list]; j++)
114  cur->ref_poc[sidx][list][j] = 4 * h->ref_list[list][j].frame_num + (h->ref_list[list][j].reference & 3);
115  }
116 
117  if(h->picture_structure == PICT_FRAME){
118  memcpy(cur->ref_count[1], cur->ref_count[0], sizeof(cur->ref_count[0]));
119  memcpy(cur->ref_poc [1], cur->ref_poc [0], sizeof(cur->ref_poc [0]));
120  }
121 
122  cur->mbaff = FRAME_MBAFF(h);
123 
124  h->col_fieldoff= 0;
125  if(h->picture_structure == PICT_FRAME){
126  int cur_poc = h->cur_pic_ptr->poc;
127  int *col_poc = h->ref_list[1]->field_poc;
128  h->col_parity= (FFABS(col_poc[0] - cur_poc) >= FFABS(col_poc[1] - cur_poc));
129  ref1sidx=sidx= h->col_parity;
130  } else if (!(h->picture_structure & h->ref_list[1][0].reference) && !h->ref_list[1][0].mbaff) { // FL -> FL & differ parity
131  h->col_fieldoff = 2 * h->ref_list[1][0].reference - 3;
132  }
133 
135  return;
136 
137  for(list=0; list<2; list++){
138  fill_colmap(h, h->map_col_to_list0, list, sidx, ref1sidx, 0);
139  if (FRAME_MBAFF(h))
140  for(field=0; field<2; field++)
141  fill_colmap(h, h->map_col_to_list0_field[field], list, field, field, 1);
142  }
143 }
144 
145 static void await_reference_mb_row(H264Context * const h, H264Picture *ref, int mb_y)
146 {
147  int ref_field = ref->reference - 1;
148  int ref_field_picture = ref->field_picture;
149  int ref_height = 16*h->mb_height >> ref_field_picture;
150 
151  if(!HAVE_THREADS || !(h->avctx->active_thread_type&FF_THREAD_FRAME))
152  return;
153 
154  //FIXME it can be safe to access mb stuff
155  //even if pixels aren't deblocked yet
156 
158  FFMIN(16 * mb_y >> ref_field_picture, ref_height - 1),
159  ref_field_picture && ref_field);
160 }
161 
162 static void pred_spatial_direct_motion(H264Context * const h, int *mb_type){
163  int b8_stride = 2;
164  int b4_stride = h->b_stride;
165  int mb_xy = h->mb_xy, mb_y = h->mb_y;
166  int mb_type_col[2];
167  const int16_t (*l1mv0)[2], (*l1mv1)[2];
168  const int8_t *l1ref0, *l1ref1;
169  const int is_b8x8 = IS_8X8(*mb_type);
170  unsigned int sub_mb_type= MB_TYPE_L0L1;
171  int i8, i4;
172  int ref[2];
173  int mv[2];
174  int list;
175 
176  assert(h->ref_list[1][0].reference & 3);
177 
178  await_reference_mb_row(h, &h->ref_list[1][0], h->mb_y + !!IS_INTERLACED(*mb_type));
179 
180 #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
181 
182 
183  /* ref = min(neighbors) */
184  for(list=0; list<2; list++){
185  int left_ref = h->ref_cache[list][scan8[0] - 1];
186  int top_ref = h->ref_cache[list][scan8[0] - 8];
187  int refc = h->ref_cache[list][scan8[0] - 8 + 4];
188  const int16_t *C= h->mv_cache[list][ scan8[0] - 8 + 4];
189  if(refc == PART_NOT_AVAILABLE){
190  refc = h->ref_cache[list][scan8[0] - 8 - 1];
191  C = h-> mv_cache[list][scan8[0] - 8 - 1];
192  }
193  ref[list] = FFMIN3((unsigned)left_ref, (unsigned)top_ref, (unsigned)refc);
194  if(ref[list] >= 0){
195  //this is just pred_motion() but with the cases removed that cannot happen for direct blocks
196  const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
197  const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
198 
199  int match_count= (left_ref==ref[list]) + (top_ref==ref[list]) + (refc==ref[list]);
200  if(match_count > 1){ //most common
201  mv[list]= pack16to32(mid_pred(A[0], B[0], C[0]),
202  mid_pred(A[1], B[1], C[1]) );
203  }else {
204  assert(match_count==1);
205  if(left_ref==ref[list]){
206  mv[list]= AV_RN32A(A);
207  }else if(top_ref==ref[list]){
208  mv[list]= AV_RN32A(B);
209  }else{
210  mv[list]= AV_RN32A(C);
211  }
212  }
213  av_assert2(ref[list] < (h->ref_count[list] << !!FRAME_MBAFF(h)));
214  }else{
215  int mask= ~(MB_TYPE_L0 << (2*list));
216  mv[list] = 0;
217  ref[list] = -1;
218  if(!is_b8x8)
219  *mb_type &= mask;
220  sub_mb_type &= mask;
221  }
222  }
223  if(ref[0] < 0 && ref[1] < 0){
224  ref[0] = ref[1] = 0;
225  if(!is_b8x8)
226  *mb_type |= MB_TYPE_L0L1;
227  sub_mb_type |= MB_TYPE_L0L1;
228  }
229 
230  if(!(is_b8x8|mv[0]|mv[1])){
231  fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
232  fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
233  fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
234  fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
236  return;
237  }
238 
239  if (IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
240  if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
241  mb_y = (h->mb_y&~1) + h->col_parity;
242  mb_xy= h->mb_x + ((h->mb_y&~1) + h->col_parity)*h->mb_stride;
243  b8_stride = 0;
244  }else{
245  mb_y += h->col_fieldoff;
246  mb_xy += h->mb_stride*h->col_fieldoff; // non zero for FL -> FL & differ parity
247  }
248  goto single_col;
249  }else{ // AFL/AFR/FR/FL -> AFR/FR
250  if(IS_INTERLACED(*mb_type)){ // AFL /FL -> AFR/FR
251  mb_y = h->mb_y&~1;
252  mb_xy= h->mb_x + (h->mb_y&~1)*h->mb_stride;
253  mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
254  mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + h->mb_stride];
255  b8_stride = 2+4*h->mb_stride;
256  b4_stride *= 6;
257  if (IS_INTERLACED(mb_type_col[0]) != IS_INTERLACED(mb_type_col[1])) {
258  mb_type_col[0] &= ~MB_TYPE_INTERLACED;
259  mb_type_col[1] &= ~MB_TYPE_INTERLACED;
260  }
261 
262  sub_mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
263  if( (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
264  && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
265  && !is_b8x8){
266  *mb_type |= MB_TYPE_16x8 |MB_TYPE_DIRECT2; /* B_16x8 */
267  }else{
268  *mb_type |= MB_TYPE_8x8;
269  }
270  }else{ // AFR/FR -> AFR/FR
271 single_col:
272  mb_type_col[0] =
273  mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy];
274 
275  sub_mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
276  if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
277  *mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_16x16 */
278  }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
279  *mb_type |= MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
280  }else{
282  /* FIXME save sub mb types from previous frames (or derive from MVs)
283  * so we know exactly what block size to use */
284  sub_mb_type += (MB_TYPE_8x8-MB_TYPE_16x16); /* B_SUB_4x4 */
285  }
286  *mb_type |= MB_TYPE_8x8;
287  }
288  }
289  }
290 
291  await_reference_mb_row(h, &h->ref_list[1][0], mb_y);
292 
293  l1mv0 = (void*)&h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
294  l1mv1 = (void*)&h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
295  l1ref0 = &h->ref_list[1][0].ref_index [0][4 * mb_xy];
296  l1ref1 = &h->ref_list[1][0].ref_index [1][4 * mb_xy];
297  if(!b8_stride){
298  if(h->mb_y&1){
299  l1ref0 += 2;
300  l1ref1 += 2;
301  l1mv0 += 2*b4_stride;
302  l1mv1 += 2*b4_stride;
303  }
304  }
305 
306 
307  if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
308  int n=0;
309  for(i8=0; i8<4; i8++){
310  int x8 = i8&1;
311  int y8 = i8>>1;
312  int xy8 = x8+y8*b8_stride;
313  int xy4 = 3*x8+y8*b4_stride;
314  int a,b;
315 
316  if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
317  continue;
318  h->sub_mb_type[i8] = sub_mb_type;
319 
320  fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
321  fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
322  if(!IS_INTRA(mb_type_col[y8]) && !h->ref_list[1][0].long_ref
323  && ( (l1ref0[xy8] == 0 && FFABS(l1mv0[xy4][0]) <= 1 && FFABS(l1mv0[xy4][1]) <= 1)
324  || (l1ref0[xy8] < 0 && l1ref1[xy8] == 0 && FFABS(l1mv1[xy4][0]) <= 1 && FFABS(l1mv1[xy4][1]) <= 1))){
325  a=b=0;
326  if(ref[0] > 0)
327  a= mv[0];
328  if(ref[1] > 0)
329  b= mv[1];
330  n++;
331  }else{
332  a= mv[0];
333  b= mv[1];
334  }
335  fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, a, 4);
336  fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, b, 4);
337  }
338  if(!is_b8x8 && !(n&3))
340  }else if(IS_16X16(*mb_type)){
341  int a,b;
342 
343  fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
344  fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
345  if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref
346  && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
347  || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
348  && h->x264_build>33U))){
349  a=b=0;
350  if(ref[0] > 0)
351  a= mv[0];
352  if(ref[1] > 0)
353  b= mv[1];
354  }else{
355  a= mv[0];
356  b= mv[1];
357  }
358  fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
359  fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
360  }else{
361  int n=0;
362  for(i8=0; i8<4; i8++){
363  const int x8 = i8&1;
364  const int y8 = i8>>1;
365 
366  if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
367  continue;
368  h->sub_mb_type[i8] = sub_mb_type;
369 
370  fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, mv[0], 4);
371  fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, mv[1], 4);
372  fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
373  fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
374 
375  assert(b8_stride==2);
376  /* col_zero_flag */
377  if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref && ( l1ref0[i8] == 0
378  || (l1ref0[i8] < 0 && l1ref1[i8] == 0
379  && h->x264_build>33U))){
380  const int16_t (*l1mv)[2]= l1ref0[i8] == 0 ? l1mv0 : l1mv1;
381  if(IS_SUB_8X8(sub_mb_type)){
382  const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
383  if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
384  if(ref[0] == 0)
385  fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
386  if(ref[1] == 0)
387  fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
388  n+=4;
389  }
390  }else{
391  int m=0;
392  for(i4=0; i4<4; i4++){
393  const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
394  if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
395  if(ref[0] == 0)
396  AV_ZERO32(h->mv_cache[0][scan8[i8*4+i4]]);
397  if(ref[1] == 0)
398  AV_ZERO32(h->mv_cache[1][scan8[i8*4+i4]]);
399  m++;
400  }
401  }
402  if(!(m&3))
404  n+=m;
405  }
406  }
407  }
408  if(!is_b8x8 && !(n&15))
410  }
411 }
412 
413 static void pred_temp_direct_motion(H264Context * const h, int *mb_type){
414  int b8_stride = 2;
415  int b4_stride = h->b_stride;
416  int mb_xy = h->mb_xy, mb_y = h->mb_y;
417  int mb_type_col[2];
418  const int16_t (*l1mv0)[2], (*l1mv1)[2];
419  const int8_t *l1ref0, *l1ref1;
420  const int is_b8x8 = IS_8X8(*mb_type);
421  unsigned int sub_mb_type;
422  int i8, i4;
423 
424  assert(h->ref_list[1][0].reference & 3);
425 
426  await_reference_mb_row(h, &h->ref_list[1][0], h->mb_y + !!IS_INTERLACED(*mb_type));
427 
428  if (IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
429  if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
430  mb_y = (h->mb_y&~1) + h->col_parity;
431  mb_xy= h->mb_x + ((h->mb_y&~1) + h->col_parity)*h->mb_stride;
432  b8_stride = 0;
433  }else{
434  mb_y += h->col_fieldoff;
435  mb_xy += h->mb_stride*h->col_fieldoff; // non zero for FL -> FL & differ parity
436  }
437  goto single_col;
438  }else{ // AFL/AFR/FR/FL -> AFR/FR
439  if(IS_INTERLACED(*mb_type)){ // AFL /FL -> AFR/FR
440  mb_y = h->mb_y&~1;
441  mb_xy= h->mb_x + (h->mb_y&~1)*h->mb_stride;
442  mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
443  mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + h->mb_stride];
444  b8_stride = 2+4*h->mb_stride;
445  b4_stride *= 6;
446  if (IS_INTERLACED(mb_type_col[0]) != IS_INTERLACED(mb_type_col[1])) {
447  mb_type_col[0] &= ~MB_TYPE_INTERLACED;
448  mb_type_col[1] &= ~MB_TYPE_INTERLACED;
449  }
450 
451  sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
452 
453  if( (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
454  && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
455  && !is_b8x8){
456  *mb_type |= MB_TYPE_16x8 |MB_TYPE_L0L1|MB_TYPE_DIRECT2; /* B_16x8 */
457  }else{
458  *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
459  }
460  }else{ // AFR/FR -> AFR/FR
461 single_col:
462  mb_type_col[0] =
463  mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy];
464 
465  sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
466  if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
467  *mb_type |= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
468  }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
469  *mb_type |= MB_TYPE_L0L1|MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
470  }else{
472  /* FIXME save sub mb types from previous frames (or derive from MVs)
473  * so we know exactly what block size to use */
474  sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
475  }
476  *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
477  }
478  }
479  }
480 
481  await_reference_mb_row(h, &h->ref_list[1][0], mb_y);
482 
483  l1mv0 = (void*)&h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
484  l1mv1 = (void*)&h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
485  l1ref0 = &h->ref_list[1][0].ref_index [0][4 * mb_xy];
486  l1ref1 = &h->ref_list[1][0].ref_index [1][4 * mb_xy];
487  if(!b8_stride){
488  if(h->mb_y&1){
489  l1ref0 += 2;
490  l1ref1 += 2;
491  l1mv0 += 2*b4_stride;
492  l1mv1 += 2*b4_stride;
493  }
494  }
495 
496  {
497  const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
498  const int *dist_scale_factor = h->dist_scale_factor;
499  int ref_offset;
500 
501  if (FRAME_MBAFF(h) && IS_INTERLACED(*mb_type)) {
502  map_col_to_list0[0] = h->map_col_to_list0_field[h->mb_y&1][0];
503  map_col_to_list0[1] = h->map_col_to_list0_field[h->mb_y&1][1];
504  dist_scale_factor =h->dist_scale_factor_field[h->mb_y&1];
505  }
506  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
507 
508  if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
509  int y_shift = 2*!IS_INTERLACED(*mb_type);
510  assert(h->sps.direct_8x8_inference_flag);
511 
512  for(i8=0; i8<4; i8++){
513  const int x8 = i8&1;
514  const int y8 = i8>>1;
515  int ref0, scale;
516  const int16_t (*l1mv)[2]= l1mv0;
517 
518  if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
519  continue;
520  h->sub_mb_type[i8] = sub_mb_type;
521 
522  fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
523  if(IS_INTRA(mb_type_col[y8])){
524  fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
525  fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
526  fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
527  continue;
528  }
529 
530  ref0 = l1ref0[x8 + y8*b8_stride];
531  if(ref0 >= 0)
532  ref0 = map_col_to_list0[0][ref0 + ref_offset];
533  else{
534  ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
535  l1mv= l1mv1;
536  }
537  scale = dist_scale_factor[ref0];
538  fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
539 
540  {
541  const int16_t *mv_col = l1mv[x8*3 + y8*b4_stride];
542  int my_col = (mv_col[1]<<y_shift)/2;
543  int mx = (scale * mv_col[0] + 128) >> 8;
544  int my = (scale * my_col + 128) >> 8;
545  fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
546  fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
547  }
548  }
549  return;
550  }
551 
552  /* one-to-one mv scaling */
553 
554  if(IS_16X16(*mb_type)){
555  int ref, mv0, mv1;
556 
557  fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
558  if(IS_INTRA(mb_type_col[0])){
559  ref=mv0=mv1=0;
560  }else{
561  const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
562  : map_col_to_list0[1][l1ref1[0] + ref_offset];
563  const int scale = dist_scale_factor[ref0];
564  const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
565  int mv_l0[2];
566  mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
567  mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
568  ref= ref0;
569  mv0= pack16to32(mv_l0[0],mv_l0[1]);
570  mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
571  }
572  fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
573  fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
574  fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
575  }else{
576  for(i8=0; i8<4; i8++){
577  const int x8 = i8&1;
578  const int y8 = i8>>1;
579  int ref0, scale;
580  const int16_t (*l1mv)[2]= l1mv0;
581 
582  if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
583  continue;
584  h->sub_mb_type[i8] = sub_mb_type;
585  fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
586  if(IS_INTRA(mb_type_col[0])){
587  fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
588  fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
589  fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
590  continue;
591  }
592 
593  assert(b8_stride == 2);
594  ref0 = l1ref0[i8];
595  if(ref0 >= 0)
596  ref0 = map_col_to_list0[0][ref0 + ref_offset];
597  else{
598  ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
599  l1mv= l1mv1;
600  }
601  scale = dist_scale_factor[ref0];
602 
603  fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
604  if(IS_SUB_8X8(sub_mb_type)){
605  const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
606  int mx = (scale * mv_col[0] + 128) >> 8;
607  int my = (scale * mv_col[1] + 128) >> 8;
608  fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
609  fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
610  }else
611  for(i4=0; i4<4; i4++){
612  const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
613  int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
614  mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
615  mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
616  AV_WN32A(h->mv_cache[1][scan8[i8*4+i4]],
617  pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]));
618  }
619  }
620  }
621  }
622 }
623 
624 void ff_h264_pred_direct_motion(H264Context * const h, int *mb_type){
625  if(h->direct_spatial_mv_pred){
626  pred_spatial_direct_motion(h, mb_type);
627  }else{
628  pred_temp_direct_motion(h, mb_type);
629  }
630 }