67d9202d0797a2cfea5f5abb85e1c940ae114219
[ffmpeg.git] / libavcodec / h264_mb.c
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... decoder
3  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 macroblock decoding
25  */
26
27 #include <stdint.h>
28
29 #include "config.h"
30
31 #include "libavutil/common.h"
32 #include "libavutil/intreadwrite.h"
33 #include "avcodec.h"
34 #include "h264.h"
35 #include "svq3.h"
36 #include "thread.h"
37
38 static inline int get_lowest_part_list_y(H264Context *h, H264Picture *pic, int n,
39                                          int height, int y_offset, int list)
40 {
41     int raw_my             = h->mv_cache[list][scan8[n]][1];
42     int filter_height_up   = (raw_my & 3) ? 2 : 0;
43     int filter_height_down = (raw_my & 3) ? 3 : 0;
44     int full_my            = (raw_my >> 2) + y_offset;
45     int top                = full_my - filter_height_up;
46     int bottom             = full_my + filter_height_down + height;
47
48     return FFMAX(abs(top), bottom);
49 }
50
51 static inline void get_lowest_part_y(H264Context *h, int refs[2][48], int n,
52                                      int height, int y_offset, int list0,
53                                      int list1, int *nrefs)
54 {
55     int my;
56
57     y_offset += 16 * (h->mb_y >> MB_FIELD(h));
58
59     if (list0) {
60         int ref_n = h->ref_cache[0][scan8[n]];
61         H264Picture *ref = &h->ref_list[0][ref_n];
62
63         // Error resilience puts the current picture in the ref list.
64         // Don't try to wait on these as it will cause a deadlock.
65         // Fields can wait on each other, though.
66         if (ref->tf.progress->data != h->cur_pic.tf.progress->data ||
67             (ref->reference & 3) != h->picture_structure) {
68             my = get_lowest_part_list_y(h, ref, n, height, y_offset, 0);
69             if (refs[0][ref_n] < 0)
70                 nrefs[0] += 1;
71             refs[0][ref_n] = FFMAX(refs[0][ref_n], my);
72         }
73     }
74
75     if (list1) {
76         int ref_n    = h->ref_cache[1][scan8[n]];
77         H264Picture *ref = &h->ref_list[1][ref_n];
78
79         if (ref->tf.progress->data != h->cur_pic.tf.progress->data ||
80             (ref->reference & 3) != h->picture_structure) {
81             my = get_lowest_part_list_y(h, ref, n, height, y_offset, 1);
82             if (refs[1][ref_n] < 0)
83                 nrefs[1] += 1;
84             refs[1][ref_n] = FFMAX(refs[1][ref_n], my);
85         }
86     }
87 }
88
89 /**
90  * Wait until all reference frames are available for MC operations.
91  *
92  * @param h the H264 context
93  */
94 static void await_references(H264Context *h)
95 {
96     const int mb_xy   = h->mb_xy;
97     const int mb_type = h->cur_pic.mb_type[mb_xy];
98     int refs[2][48];
99     int nrefs[2] = { 0 };
100     int ref, list;
101
102     memset(refs, -1, sizeof(refs));
103
104     if (IS_16X16(mb_type)) {
105         get_lowest_part_y(h, refs, 0, 16, 0,
106                           IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
107     } else if (IS_16X8(mb_type)) {
108         get_lowest_part_y(h, refs, 0, 8, 0,
109                           IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
110         get_lowest_part_y(h, refs, 8, 8, 8,
111                           IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
112     } else if (IS_8X16(mb_type)) {
113         get_lowest_part_y(h, refs, 0, 16, 0,
114                           IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
115         get_lowest_part_y(h, refs, 4, 16, 0,
116                           IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
117     } else {
118         int i;
119
120         assert(IS_8X8(mb_type));
121
122         for (i = 0; i < 4; i++) {
123             const int sub_mb_type = h->sub_mb_type[i];
124             const int n           = 4 * i;
125             int y_offset          = (i & 2) << 2;
126
127             if (IS_SUB_8X8(sub_mb_type)) {
128                 get_lowest_part_y(h, refs, n, 8, y_offset,
129                                   IS_DIR(sub_mb_type, 0, 0),
130                                   IS_DIR(sub_mb_type, 0, 1),
131                                   nrefs);
132             } else if (IS_SUB_8X4(sub_mb_type)) {
133                 get_lowest_part_y(h, refs, n, 4, y_offset,
134                                   IS_DIR(sub_mb_type, 0, 0),
135                                   IS_DIR(sub_mb_type, 0, 1),
136                                   nrefs);
137                 get_lowest_part_y(h, refs, n + 2, 4, y_offset + 4,
138                                   IS_DIR(sub_mb_type, 0, 0),
139                                   IS_DIR(sub_mb_type, 0, 1),
140                                   nrefs);
141             } else if (IS_SUB_4X8(sub_mb_type)) {
142                 get_lowest_part_y(h, refs, n, 8, y_offset,
143                                   IS_DIR(sub_mb_type, 0, 0),
144                                   IS_DIR(sub_mb_type, 0, 1),
145                                   nrefs);
146                 get_lowest_part_y(h, refs, n + 1, 8, y_offset,
147                                   IS_DIR(sub_mb_type, 0, 0),
148                                   IS_DIR(sub_mb_type, 0, 1),
149                                   nrefs);
150             } else {
151                 int j;
152                 assert(IS_SUB_4X4(sub_mb_type));
153                 for (j = 0; j < 4; j++) {
154                     int sub_y_offset = y_offset + 2 * (j & 2);
155                     get_lowest_part_y(h, refs, n + j, 4, sub_y_offset,
156                                       IS_DIR(sub_mb_type, 0, 0),
157                                       IS_DIR(sub_mb_type, 0, 1),
158                                       nrefs);
159                 }
160             }
161         }
162     }
163
164     for (list = h->list_count - 1; list >= 0; list--)
165         for (ref = 0; ref < 48 && nrefs[list]; ref++) {
166             int row = refs[list][ref];
167             if (row >= 0) {
168                 H264Picture *ref_pic  = &h->ref_list[list][ref];
169                 int ref_field         = ref_pic->reference - 1;
170                 int ref_field_picture = ref_pic->field_picture;
171                 int pic_height        = 16 * h->mb_height >> ref_field_picture;
172
173                 row <<= MB_MBAFF(h);
174                 nrefs[list]--;
175
176                 if (!FIELD_PICTURE(h) && ref_field_picture) { // frame referencing two fields
177                     ff_thread_await_progress(&ref_pic->tf,
178                                              FFMIN((row >> 1) - !(row & 1),
179                                                    pic_height - 1),
180                                              1);
181                     ff_thread_await_progress(&ref_pic->tf,
182                                              FFMIN((row >> 1), pic_height - 1),
183                                              0);
184                 } else if (FIELD_PICTURE(h) && !ref_field_picture) { // field referencing one field of a frame
185                     ff_thread_await_progress(&ref_pic->tf,
186                                              FFMIN(row * 2 + ref_field,
187                                                    pic_height - 1),
188                                              0);
189                 } else if (FIELD_PICTURE(h)) {
190                     ff_thread_await_progress(&ref_pic->tf,
191                                              FFMIN(row, pic_height - 1),
192                                              ref_field);
193                 } else {
194                     ff_thread_await_progress(&ref_pic->tf,
195                                              FFMIN(row, pic_height - 1),
196                                              0);
197                 }
198             }
199         }
200 }
201
202 static av_always_inline void mc_dir_part(H264Context *h, H264Picture *pic,
203                                          int n, int square, int height,
204                                          int delta, int list,
205                                          uint8_t *dest_y, uint8_t *dest_cb,
206                                          uint8_t *dest_cr,
207                                          int src_x_offset, int src_y_offset,
208                                          qpel_mc_func *qpix_op,
209                                          h264_chroma_mc_func chroma_op,
210                                          int pixel_shift, int chroma_idc)
211 {
212     const int mx      = h->mv_cache[list][scan8[n]][0] + src_x_offset * 8;
213     int my            = h->mv_cache[list][scan8[n]][1] + src_y_offset * 8;
214     const int luma_xy = (mx & 3) + ((my & 3) << 2);
215     ptrdiff_t offset  = ((mx >> 2) << pixel_shift) + (my >> 2) * h->mb_linesize;
216     uint8_t *src_y    = pic->f.data[0] + offset;
217     uint8_t *src_cb, *src_cr;
218     int extra_width  = 0;
219     int extra_height = 0;
220     int emu = 0;
221     const int full_mx    = mx >> 2;
222     const int full_my    = my >> 2;
223     const int pic_width  = 16 * h->mb_width;
224     const int pic_height = 16 * h->mb_height >> MB_FIELD(h);
225     int ysh;
226
227     if (mx & 7)
228         extra_width -= 3;
229     if (my & 7)
230         extra_height -= 3;
231
232     if (full_mx                <          0 - extra_width  ||
233         full_my                <          0 - extra_height ||
234         full_mx + 16 /*FIXME*/ > pic_width  + extra_width  ||
235         full_my + 16 /*FIXME*/ > pic_height + extra_height) {
236         h->vdsp.emulated_edge_mc(h->edge_emu_buffer,
237                                  src_y - (2 << pixel_shift) - 2 * h->mb_linesize,
238                                  h->mb_linesize, h->mb_linesize,
239                                  16 + 5, 16 + 5 /*FIXME*/, full_mx - 2,
240                                  full_my - 2, pic_width, pic_height);
241         src_y = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
242         emu   = 1;
243     }
244
245     qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); // FIXME try variable height perhaps?
246     if (!square)
247         qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
248
249     if (CONFIG_GRAY && h->flags & CODEC_FLAG_GRAY)
250         return;
251
252     if (chroma_idc == 3 /* yuv444 */) {
253         src_cb = pic->f.data[1] + offset;
254         if (emu) {
255             h->vdsp.emulated_edge_mc(h->edge_emu_buffer,
256                                      src_cb - (2 << pixel_shift) - 2 * h->mb_linesize,
257                                      h->mb_linesize, h->mb_linesize,
258                                      16 + 5, 16 + 5 /*FIXME*/,
259                                      full_mx - 2, full_my - 2,
260                                      pic_width, pic_height);
261             src_cb = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
262         }
263         qpix_op[luma_xy](dest_cb, src_cb, h->mb_linesize); // FIXME try variable height perhaps?
264         if (!square)
265             qpix_op[luma_xy](dest_cb + delta, src_cb + delta, h->mb_linesize);
266
267         src_cr = pic->f.data[2] + offset;
268         if (emu) {
269             h->vdsp.emulated_edge_mc(h->edge_emu_buffer,
270                                      src_cr - (2 << pixel_shift) - 2 * h->mb_linesize,
271                                      h->mb_linesize, h->mb_linesize,
272                                      16 + 5, 16 + 5 /*FIXME*/,
273                                      full_mx - 2, full_my - 2,
274                                      pic_width, pic_height);
275             src_cr = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
276         }
277         qpix_op[luma_xy](dest_cr, src_cr, h->mb_linesize); // FIXME try variable height perhaps?
278         if (!square)
279             qpix_op[luma_xy](dest_cr + delta, src_cr + delta, h->mb_linesize);
280         return;
281     }
282
283     ysh = 3 - (chroma_idc == 2 /* yuv422 */);
284     if (chroma_idc == 1 /* yuv420 */ && MB_FIELD(h)) {
285         // chroma offset when predicting from a field of opposite parity
286         my  += 2 * ((h->mb_y & 1) - (pic->reference - 1));
287         emu |= (my >> 3) < 0 || (my >> 3) + 8 >= (pic_height >> 1);
288     }
289
290     src_cb = pic->f.data[1] + ((mx >> 3) << pixel_shift) +
291              (my >> ysh) * h->mb_uvlinesize;
292     src_cr = pic->f.data[2] + ((mx >> 3) << pixel_shift) +
293              (my >> ysh) * h->mb_uvlinesize;
294
295     if (emu) {
296         h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src_cb,
297                                  h->mb_uvlinesize, h->mb_uvlinesize,
298                                  9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
299                                  pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
300         src_cb = h->edge_emu_buffer;
301     }
302     chroma_op(dest_cb, src_cb, h->mb_uvlinesize,
303               height >> (chroma_idc == 1 /* yuv420 */),
304               mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
305
306     if (emu) {
307         h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src_cr,
308                                  h->mb_uvlinesize, h->mb_uvlinesize,
309                                  9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
310                                  pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
311         src_cr = h->edge_emu_buffer;
312     }
313     chroma_op(dest_cr, src_cr, h->mb_uvlinesize, height >> (chroma_idc == 1 /* yuv420 */),
314               mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
315 }
316
317 static av_always_inline void mc_part_std(H264Context *h, int n, int square,
318                                          int height, int delta,
319                                          uint8_t *dest_y, uint8_t *dest_cb,
320                                          uint8_t *dest_cr,
321                                          int x_offset, int y_offset,
322                                          qpel_mc_func *qpix_put,
323                                          h264_chroma_mc_func chroma_put,
324                                          qpel_mc_func *qpix_avg,
325                                          h264_chroma_mc_func chroma_avg,
326                                          int list0, int list1,
327                                          int pixel_shift, int chroma_idc)
328 {
329     qpel_mc_func *qpix_op         = qpix_put;
330     h264_chroma_mc_func chroma_op = chroma_put;
331
332     dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
333     if (chroma_idc == 3 /* yuv444 */) {
334         dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
335         dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
336     } else if (chroma_idc == 2 /* yuv422 */) {
337         dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
338         dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
339     } else { /* yuv420 */
340         dest_cb += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
341         dest_cr += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
342     }
343     x_offset += 8 * h->mb_x;
344     y_offset += 8 * (h->mb_y >> MB_FIELD(h));
345
346     if (list0) {
347         H264Picture *ref = &h->ref_list[0][h->ref_cache[0][scan8[n]]];
348         mc_dir_part(h, ref, n, square, height, delta, 0,
349                     dest_y, dest_cb, dest_cr, x_offset, y_offset,
350                     qpix_op, chroma_op, pixel_shift, chroma_idc);
351
352         qpix_op   = qpix_avg;
353         chroma_op = chroma_avg;
354     }
355
356     if (list1) {
357         H264Picture *ref = &h->ref_list[1][h->ref_cache[1][scan8[n]]];
358         mc_dir_part(h, ref, n, square, height, delta, 1,
359                     dest_y, dest_cb, dest_cr, x_offset, y_offset,
360                     qpix_op, chroma_op, pixel_shift, chroma_idc);
361     }
362 }
363
364 static av_always_inline void mc_part_weighted(H264Context *h, int n, int square,
365                                               int height, int delta,
366                                               uint8_t *dest_y, uint8_t *dest_cb,
367                                               uint8_t *dest_cr,
368                                               int x_offset, int y_offset,
369                                               qpel_mc_func *qpix_put,
370                                               h264_chroma_mc_func chroma_put,
371                                               h264_weight_func luma_weight_op,
372                                               h264_weight_func chroma_weight_op,
373                                               h264_biweight_func luma_weight_avg,
374                                               h264_biweight_func chroma_weight_avg,
375                                               int list0, int list1,
376                                               int pixel_shift, int chroma_idc)
377 {
378     int chroma_height;
379
380     dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
381     if (chroma_idc == 3 /* yuv444 */) {
382         chroma_height     = height;
383         chroma_weight_avg = luma_weight_avg;
384         chroma_weight_op  = luma_weight_op;
385         dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
386         dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
387     } else if (chroma_idc == 2 /* yuv422 */) {
388         chroma_height = height;
389         dest_cb      += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
390         dest_cr      += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
391     } else { /* yuv420 */
392         chroma_height = height >> 1;
393         dest_cb      += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
394         dest_cr      += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
395     }
396     x_offset += 8 * h->mb_x;
397     y_offset += 8 * (h->mb_y >> MB_FIELD(h));
398
399     if (list0 && list1) {
400         /* don't optimize for luma-only case, since B-frames usually
401          * use implicit weights => chroma too. */
402         uint8_t *tmp_cb = h->bipred_scratchpad;
403         uint8_t *tmp_cr = h->bipred_scratchpad + (16 << pixel_shift);
404         uint8_t *tmp_y  = h->bipred_scratchpad + 16 * h->mb_uvlinesize;
405         int refn0       = h->ref_cache[0][scan8[n]];
406         int refn1       = h->ref_cache[1][scan8[n]];
407
408         mc_dir_part(h, &h->ref_list[0][refn0], n, square, height, delta, 0,
409                     dest_y, dest_cb, dest_cr,
410                     x_offset, y_offset, qpix_put, chroma_put,
411                     pixel_shift, chroma_idc);
412         mc_dir_part(h, &h->ref_list[1][refn1], n, square, height, delta, 1,
413                     tmp_y, tmp_cb, tmp_cr,
414                     x_offset, y_offset, qpix_put, chroma_put,
415                     pixel_shift, chroma_idc);
416
417         if (h->use_weight == 2) {
418             int weight0 = h->implicit_weight[refn0][refn1][h->mb_y & 1];
419             int weight1 = 64 - weight0;
420             luma_weight_avg(dest_y, tmp_y, h->mb_linesize,
421                             height, 5, weight0, weight1, 0);
422             chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize,
423                               chroma_height, 5, weight0, weight1, 0);
424             chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize,
425                               chroma_height, 5, weight0, weight1, 0);
426         } else {
427             luma_weight_avg(dest_y, tmp_y, h->mb_linesize, height,
428                             h->luma_log2_weight_denom,
429                             h->luma_weight[refn0][0][0],
430                             h->luma_weight[refn1][1][0],
431                             h->luma_weight[refn0][0][1] +
432                             h->luma_weight[refn1][1][1]);
433             chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, chroma_height,
434                               h->chroma_log2_weight_denom,
435                               h->chroma_weight[refn0][0][0][0],
436                               h->chroma_weight[refn1][1][0][0],
437                               h->chroma_weight[refn0][0][0][1] +
438                               h->chroma_weight[refn1][1][0][1]);
439             chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, chroma_height,
440                               h->chroma_log2_weight_denom,
441                               h->chroma_weight[refn0][0][1][0],
442                               h->chroma_weight[refn1][1][1][0],
443                               h->chroma_weight[refn0][0][1][1] +
444                               h->chroma_weight[refn1][1][1][1]);
445         }
446     } else {
447         int list     = list1 ? 1 : 0;
448         int refn     = h->ref_cache[list][scan8[n]];
449         H264Picture *ref = &h->ref_list[list][refn];
450         mc_dir_part(h, ref, n, square, height, delta, list,
451                     dest_y, dest_cb, dest_cr, x_offset, y_offset,
452                     qpix_put, chroma_put, pixel_shift, chroma_idc);
453
454         luma_weight_op(dest_y, h->mb_linesize, height,
455                        h->luma_log2_weight_denom,
456                        h->luma_weight[refn][list][0],
457                        h->luma_weight[refn][list][1]);
458         if (h->use_weight_chroma) {
459             chroma_weight_op(dest_cb, h->mb_uvlinesize, chroma_height,
460                              h->chroma_log2_weight_denom,
461                              h->chroma_weight[refn][list][0][0],
462                              h->chroma_weight[refn][list][0][1]);
463             chroma_weight_op(dest_cr, h->mb_uvlinesize, chroma_height,
464                              h->chroma_log2_weight_denom,
465                              h->chroma_weight[refn][list][1][0],
466                              h->chroma_weight[refn][list][1][1]);
467         }
468     }
469 }
470
471 static av_always_inline void prefetch_motion(H264Context *h, int list,
472                                              int pixel_shift, int chroma_idc)
473 {
474     /* fetch pixels for estimated mv 4 macroblocks ahead
475      * optimized for 64byte cache lines */
476     const int refn = h->ref_cache[list][scan8[0]];
477     if (refn >= 0) {
478         const int mx  = (h->mv_cache[list][scan8[0]][0] >> 2) + 16 * h->mb_x + 8;
479         const int my  = (h->mv_cache[list][scan8[0]][1] >> 2) + 16 * h->mb_y;
480         uint8_t **src = h->ref_list[list][refn].f.data;
481         int off       = (mx << pixel_shift) +
482                         (my + (h->mb_x & 3) * 4) * h->mb_linesize +
483                         (64 << pixel_shift);
484         h->vdsp.prefetch(src[0] + off, h->linesize, 4);
485         if (chroma_idc == 3 /* yuv444 */) {
486             h->vdsp.prefetch(src[1] + off, h->linesize, 4);
487             h->vdsp.prefetch(src[2] + off, h->linesize, 4);
488         } else {
489             off = ((mx >> 1) << pixel_shift) +
490                   ((my >> 1) + (h->mb_x & 7)) * h->uvlinesize +
491                   (64 << pixel_shift);
492             h->vdsp.prefetch(src[1] + off, src[2] - src[1], 2);
493         }
494     }
495 }
496
497 static av_always_inline void xchg_mb_border(H264Context *h, uint8_t *src_y,
498                                             uint8_t *src_cb, uint8_t *src_cr,
499                                             int linesize, int uvlinesize,
500                                             int xchg, int chroma444,
501                                             int simple, int pixel_shift)
502 {
503     int deblock_topleft;
504     int deblock_top;
505     int top_idx = 1;
506     uint8_t *top_border_m1;
507     uint8_t *top_border;
508
509     if (!simple && FRAME_MBAFF(h)) {
510         if (h->mb_y & 1) {
511             if (!MB_MBAFF(h))
512                 return;
513         } else {
514             top_idx = MB_MBAFF(h) ? 0 : 1;
515         }
516     }
517
518     if (h->deblocking_filter == 2) {
519         deblock_topleft = h->slice_table[h->mb_xy - 1 - h->mb_stride] == h->slice_num;
520         deblock_top     = h->top_type;
521     } else {
522         deblock_topleft = (h->mb_x > 0);
523         deblock_top     = (h->mb_y > !!MB_FIELD(h));
524     }
525
526     src_y  -= linesize   + 1 + pixel_shift;
527     src_cb -= uvlinesize + 1 + pixel_shift;
528     src_cr -= uvlinesize + 1 + pixel_shift;
529
530     top_border_m1 = h->top_borders[top_idx][h->mb_x - 1];
531     top_border    = h->top_borders[top_idx][h->mb_x];
532
533 #define XCHG(a, b, xchg)                        \
534     if (pixel_shift) {                          \
535         if (xchg) {                             \
536             AV_SWAP64(b + 0, a + 0);            \
537             AV_SWAP64(b + 8, a + 8);            \
538         } else {                                \
539             AV_COPY128(b, a);                   \
540         }                                       \
541     } else if (xchg)                            \
542         AV_SWAP64(b, a);                        \
543     else                                        \
544         AV_COPY64(b, a);
545
546     if (deblock_top) {
547         if (deblock_topleft) {
548             XCHG(top_border_m1 + (8 << pixel_shift),
549                  src_y - (7 << pixel_shift), 1);
550         }
551         XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
552         XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
553         if (h->mb_x + 1 < h->mb_width) {
554             XCHG(h->top_borders[top_idx][h->mb_x + 1],
555                  src_y + (17 << pixel_shift), 1);
556         }
557     }
558     if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
559         if (chroma444) {
560             if (deblock_top) {
561                 if (deblock_topleft) {
562                     XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1);
563                     XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1);
564                 }
565                 XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg);
566                 XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1);
567                 XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg);
568                 XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1);
569                 if (h->mb_x + 1 < h->mb_width) {
570                     XCHG(h->top_borders[top_idx][h->mb_x + 1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1);
571                     XCHG(h->top_borders[top_idx][h->mb_x + 1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1);
572                 }
573             }
574         } else {
575             if (deblock_top) {
576                 if (deblock_topleft) {
577                     XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1);
578                     XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1);
579                 }
580                 XCHG(top_border + (16 << pixel_shift), src_cb + 1 + pixel_shift, 1);
581                 XCHG(top_border + (24 << pixel_shift), src_cr + 1 + pixel_shift, 1);
582             }
583         }
584     }
585 }
586
587 static av_always_inline int dctcoef_get(int16_t *mb, int high_bit_depth,
588                                         int index)
589 {
590     if (high_bit_depth) {
591         return AV_RN32A(((int32_t *)mb) + index);
592     } else
593         return AV_RN16A(mb + index);
594 }
595
596 static av_always_inline void dctcoef_set(int16_t *mb, int high_bit_depth,
597                                          int index, int value)
598 {
599     if (high_bit_depth) {
600         AV_WN32A(((int32_t *)mb) + index, value);
601     } else
602         AV_WN16A(mb + index, value);
603 }
604
605 static av_always_inline void hl_decode_mb_predict_luma(H264Context *h,
606                                                        int mb_type, int is_h264,
607                                                        int simple,
608                                                        int transform_bypass,
609                                                        int pixel_shift,
610                                                        int *block_offset,
611                                                        int linesize,
612                                                        uint8_t *dest_y, int p)
613 {
614     void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
615     void (*idct_dc_add)(uint8_t *dst, int16_t *block, int stride);
616     int i;
617     int qscale = p == 0 ? h->qscale : h->chroma_qp[p - 1];
618     block_offset += 16 * p;
619     if (IS_INTRA4x4(mb_type)) {
620         if (IS_8x8DCT(mb_type)) {
621             if (transform_bypass) {
622                 idct_dc_add =
623                 idct_add    = h->h264dsp.h264_add_pixels8_clear;
624             } else {
625                 idct_dc_add = h->h264dsp.h264_idct8_dc_add;
626                 idct_add    = h->h264dsp.h264_idct8_add;
627             }
628             for (i = 0; i < 16; i += 4) {
629                 uint8_t *const ptr = dest_y + block_offset[i];
630                 const int dir      = h->intra4x4_pred_mode_cache[scan8[i]];
631                 if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
632                     h->hpc.pred8x8l_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
633                 } else {
634                     const int nnz = h->non_zero_count_cache[scan8[i + p * 16]];
635                     h->hpc.pred8x8l[dir](ptr, (h->topleft_samples_available << i) & 0x8000,
636                                          (h->topright_samples_available << i) & 0x4000, linesize);
637                     if (nnz) {
638                         if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
639                             idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
640                         else
641                             idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
642                     }
643                 }
644             }
645         } else {
646             if (transform_bypass) {
647                 idct_dc_add  =
648                 idct_add     = h->h264dsp.h264_add_pixels4_clear;
649             } else {
650                 idct_dc_add = h->h264dsp.h264_idct_dc_add;
651                 idct_add    = h->h264dsp.h264_idct_add;
652             }
653             for (i = 0; i < 16; i++) {
654                 uint8_t *const ptr = dest_y + block_offset[i];
655                 const int dir      = h->intra4x4_pred_mode_cache[scan8[i]];
656
657                 if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
658                     h->hpc.pred4x4_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
659                 } else {
660                     uint8_t *topright;
661                     int nnz, tr;
662                     uint64_t tr_high;
663                     if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
664                         const int topright_avail = (h->topright_samples_available << i) & 0x8000;
665                         assert(h->mb_y || linesize <= block_offset[i]);
666                         if (!topright_avail) {
667                             if (pixel_shift) {
668                                 tr_high  = ((uint16_t *)ptr)[3 - linesize / 2] * 0x0001000100010001ULL;
669                                 topright = (uint8_t *)&tr_high;
670                             } else {
671                                 tr       = ptr[3 - linesize] * 0x01010101u;
672                                 topright = (uint8_t *)&tr;
673                             }
674                         } else
675                             topright = ptr + (4 << pixel_shift) - linesize;
676                     } else
677                         topright = NULL;
678
679                     h->hpc.pred4x4[dir](ptr, topright, linesize);
680                     nnz = h->non_zero_count_cache[scan8[i + p * 16]];
681                     if (nnz) {
682                         if (is_h264) {
683                             if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
684                                 idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
685                             else
686                                 idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
687                         } else if (CONFIG_SVQ3_DECODER)
688                             ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize, qscale, 0);
689                     }
690                 }
691             }
692         }
693     } else {
694         h->hpc.pred16x16[h->intra16x16_pred_mode](dest_y, linesize);
695         if (is_h264) {
696             if (h->non_zero_count_cache[scan8[LUMA_DC_BLOCK_INDEX + p]]) {
697                 if (!transform_bypass)
698                     h->h264dsp.h264_luma_dc_dequant_idct(h->mb + (p * 256 << pixel_shift),
699                                                          h->mb_luma_dc[p],
700                                                          h->dequant4_coeff[p][qscale][0]);
701                 else {
702                     static const uint8_t dc_mapping[16] = {
703                          0 * 16,  1 * 16,  4 * 16,  5 * 16,
704                          2 * 16,  3 * 16,  6 * 16,  7 * 16,
705                          8 * 16,  9 * 16, 12 * 16, 13 * 16,
706                         10 * 16, 11 * 16, 14 * 16, 15 * 16
707                     };
708                     for (i = 0; i < 16; i++)
709                         dctcoef_set(h->mb + (p * 256 << pixel_shift),
710                                     pixel_shift, dc_mapping[i],
711                                     dctcoef_get(h->mb_luma_dc[p],
712                                                 pixel_shift, i));
713                 }
714             }
715         } else if (CONFIG_SVQ3_DECODER)
716             ff_svq3_luma_dc_dequant_idct_c(h->mb + p * 256,
717                                            h->mb_luma_dc[p], qscale);
718     }
719 }
720
721 static av_always_inline void hl_decode_mb_idct_luma(H264Context *h, int mb_type,
722                                                     int is_h264, int simple,
723                                                     int transform_bypass,
724                                                     int pixel_shift,
725                                                     int *block_offset,
726                                                     int linesize,
727                                                     uint8_t *dest_y, int p)
728 {
729     void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
730     int i;
731     block_offset += 16 * p;
732     if (!IS_INTRA4x4(mb_type)) {
733         if (is_h264) {
734             if (IS_INTRA16x16(mb_type)) {
735                 if (transform_bypass) {
736                     if (h->sps.profile_idc == 244 &&
737                         (h->intra16x16_pred_mode == VERT_PRED8x8 ||
738                          h->intra16x16_pred_mode == HOR_PRED8x8)) {
739                         h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset,
740                                                                       h->mb + (p * 256 << pixel_shift),
741                                                                       linesize);
742                     } else {
743                         for (i = 0; i < 16; i++)
744                             if (h->non_zero_count_cache[scan8[i + p * 16]] ||
745                                 dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
746                                 h->h264dsp.h264_add_pixels4_clear(dest_y + block_offset[i],
747                                                                   h->mb + (i * 16 + p * 256 << pixel_shift),
748                                                                   linesize);
749                     }
750                 } else {
751                     h->h264dsp.h264_idct_add16intra(dest_y, block_offset,
752                                                     h->mb + (p * 256 << pixel_shift),
753                                                     linesize,
754                                                     h->non_zero_count_cache + p * 5 * 8);
755                 }
756             } else if (h->cbp & 15) {
757                 if (transform_bypass) {
758                     const int di = IS_8x8DCT(mb_type) ? 4 : 1;
759                     idct_add = IS_8x8DCT(mb_type) ? h->h264dsp.h264_add_pixels8_clear
760                                                   : h->h264dsp.h264_add_pixels4_clear;
761                     for (i = 0; i < 16; i += di)
762                         if (h->non_zero_count_cache[scan8[i + p * 16]])
763                             idct_add(dest_y + block_offset[i],
764                                      h->mb + (i * 16 + p * 256 << pixel_shift),
765                                      linesize);
766                 } else {
767                     if (IS_8x8DCT(mb_type))
768                         h->h264dsp.h264_idct8_add4(dest_y, block_offset,
769                                                    h->mb + (p * 256 << pixel_shift),
770                                                    linesize,
771                                                    h->non_zero_count_cache + p * 5 * 8);
772                     else
773                         h->h264dsp.h264_idct_add16(dest_y, block_offset,
774                                                    h->mb + (p * 256 << pixel_shift),
775                                                    linesize,
776                                                    h->non_zero_count_cache + p * 5 * 8);
777                 }
778             }
779         } else if (CONFIG_SVQ3_DECODER) {
780             for (i = 0; i < 16; i++)
781                 if (h->non_zero_count_cache[scan8[i + p * 16]] || h->mb[i * 16 + p * 256]) {
782                     // FIXME benchmark weird rule, & below
783                     uint8_t *const ptr = dest_y + block_offset[i];
784                     ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize,
785                                        h->qscale, IS_INTRA(mb_type) ? 1 : 0);
786                 }
787         }
788     }
789 }
790
791 #define BITS   8
792 #define SIMPLE 1
793 #include "h264_mb_template.c"
794
795 #undef  BITS
796 #define BITS   16
797 #include "h264_mb_template.c"
798
799 #undef  SIMPLE
800 #define SIMPLE 0
801 #include "h264_mb_template.c"
802
803 void ff_h264_hl_decode_mb(H264Context *h)
804 {
805     const int mb_xy   = h->mb_xy;
806     const int mb_type = h->cur_pic.mb_type[mb_xy];
807     int is_complex    = CONFIG_SMALL || h->is_complex ||
808                         IS_INTRA_PCM(mb_type) || h->qscale == 0;
809
810     if (CHROMA444(h)) {
811         if (is_complex || h->pixel_shift)
812             hl_decode_mb_444_complex(h);
813         else
814             hl_decode_mb_444_simple_8(h);
815     } else if (is_complex) {
816         hl_decode_mb_complex(h);
817     } else if (h->pixel_shift) {
818         hl_decode_mb_simple_16(h);
819     } else
820         hl_decode_mb_simple_8(h);
821 }