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