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