]> git.sesse.net Git - ffmpeg/blob - libavcodec/h264_mb.c
avcodec/dcadec: use a constant instead of assuming every compiler can optimize pow...
[ffmpeg] / 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 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 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_down = (raw_my & 3) ? 3 : 0;
43     int full_my            = (raw_my >> 2) + y_offset;
44     int bottom             = full_my + filter_height_down + height;
45
46     av_assert2(height >= 0);
47
48     return FFMAX(0, 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         av_assert2(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                 av_assert2(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)+64)<<pixel_shift) + ((my>>1) + (h->mb_x&7))*h->uvlinesize;
490             h->vdsp.prefetch(src[1] + off, src[2] - src[1], 2);
491         }
492     }
493 }
494
495 static av_always_inline void xchg_mb_border(H264Context *h, uint8_t *src_y,
496                                             uint8_t *src_cb, uint8_t *src_cr,
497                                             int linesize, int uvlinesize,
498                                             int xchg, int chroma444,
499                                             int simple, int pixel_shift)
500 {
501     int deblock_topleft;
502     int deblock_top;
503     int top_idx = 1;
504     uint8_t *top_border_m1;
505     uint8_t *top_border;
506
507     if (!simple && FRAME_MBAFF(h)) {
508         if (h->mb_y & 1) {
509             if (!MB_MBAFF(h))
510                 return;
511         } else {
512             top_idx = MB_MBAFF(h) ? 0 : 1;
513         }
514     }
515
516     if (h->deblocking_filter == 2) {
517         deblock_topleft = h->slice_table[h->mb_xy - 1 - h->mb_stride] == h->slice_num;
518         deblock_top     = h->top_type;
519     } else {
520         deblock_topleft = (h->mb_x > 0);
521         deblock_top     = (h->mb_y > !!MB_FIELD(h));
522     }
523
524     src_y  -= linesize   + 1 + pixel_shift;
525     src_cb -= uvlinesize + 1 + pixel_shift;
526     src_cr -= uvlinesize + 1 + pixel_shift;
527
528     top_border_m1 = h->top_borders[top_idx][h->mb_x - 1];
529     top_border    = h->top_borders[top_idx][h->mb_x];
530
531 #define XCHG(a, b, xchg)                        \
532     if (pixel_shift) {                          \
533         if (xchg) {                             \
534             AV_SWAP64(b + 0, a + 0);            \
535             AV_SWAP64(b + 8, a + 8);            \
536         } else {                                \
537             AV_COPY128(b, a);                   \
538         }                                       \
539     } else if (xchg)                            \
540         AV_SWAP64(b, a);                        \
541     else                                        \
542         AV_COPY64(b, a);
543
544     if (deblock_top) {
545         if (deblock_topleft) {
546             XCHG(top_border_m1 + (8 << pixel_shift),
547                  src_y - (7 << pixel_shift), 1);
548         }
549         XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
550         XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
551         if (h->mb_x + 1 < h->mb_width) {
552             XCHG(h->top_borders[top_idx][h->mb_x + 1],
553                  src_y + (17 << pixel_shift), 1);
554         }
555         if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
556             if (chroma444) {
557                 if (deblock_topleft) {
558                     XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1);
559                     XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1);
560                 }
561                 XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg);
562                 XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1);
563                 XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg);
564                 XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1);
565                 if (h->mb_x + 1 < h->mb_width) {
566                     XCHG(h->top_borders[top_idx][h->mb_x + 1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1);
567                     XCHG(h->top_borders[top_idx][h->mb_x + 1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1);
568                 }
569             } else {
570                 if (deblock_topleft) {
571                     XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1);
572                     XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1);
573                 }
574                 XCHG(top_border + (16 << pixel_shift), src_cb + 1 + pixel_shift, 1);
575                 XCHG(top_border + (24 << pixel_shift), src_cr + 1 + pixel_shift, 1);
576             }
577         }
578     }
579 }
580
581 static av_always_inline int dctcoef_get(int16_t *mb, int high_bit_depth,
582                                         int index)
583 {
584     if (high_bit_depth) {
585         return AV_RN32A(((int32_t *)mb) + index);
586     } else
587         return AV_RN16A(mb + index);
588 }
589
590 static av_always_inline void dctcoef_set(int16_t *mb, int high_bit_depth,
591                                          int index, int value)
592 {
593     if (high_bit_depth) {
594         AV_WN32A(((int32_t *)mb) + index, value);
595     } else
596         AV_WN16A(mb + index, value);
597 }
598
599 static av_always_inline void hl_decode_mb_predict_luma(H264Context *h,
600                                                        int mb_type, int is_h264,
601                                                        int simple,
602                                                        int transform_bypass,
603                                                        int pixel_shift,
604                                                        int *block_offset,
605                                                        int linesize,
606                                                        uint8_t *dest_y, int p)
607 {
608     void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
609     void (*idct_dc_add)(uint8_t *dst, int16_t *block, int stride);
610     int i;
611     int qscale = p == 0 ? h->qscale : h->chroma_qp[p - 1];
612     block_offset += 16 * p;
613     if (IS_INTRA4x4(mb_type)) {
614         if (IS_8x8DCT(mb_type)) {
615             if (transform_bypass) {
616                 idct_dc_add =
617                 idct_add    = h->h264dsp.h264_add_pixels8_clear;
618             } else {
619                 idct_dc_add = h->h264dsp.h264_idct8_dc_add;
620                 idct_add    = h->h264dsp.h264_idct8_add;
621             }
622             for (i = 0; i < 16; i += 4) {
623                 uint8_t *const ptr = dest_y + block_offset[i];
624                 const int dir      = h->intra4x4_pred_mode_cache[scan8[i]];
625                 if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
626                     if (h->x264_build != -1) {
627                         h->hpc.pred8x8l_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
628                     } else
629                         h->hpc.pred8x8l_filter_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift),
630                                                         (h-> topleft_samples_available << i) & 0x8000,
631                                                         (h->topright_samples_available << i) & 0x4000, linesize);
632                 } else {
633                     const int nnz = h->non_zero_count_cache[scan8[i + p * 16]];
634                     h->hpc.pred8x8l[dir](ptr, (h->topleft_samples_available << i) & 0x8000,
635                                          (h->topright_samples_available << i) & 0x4000, linesize);
636                     if (nnz) {
637                         if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
638                             idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
639                         else
640                             idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
641                     }
642                 }
643             }
644         } else {
645             if (transform_bypass) {
646                 idct_dc_add  =
647                 idct_add     = h->h264dsp.h264_add_pixels4_clear;
648             } else {
649                 idct_dc_add = h->h264dsp.h264_idct_dc_add;
650                 idct_add    = h->h264dsp.h264_idct_add;
651             }
652             for (i = 0; i < 16; i++) {
653                 uint8_t *const ptr = dest_y + block_offset[i];
654                 const int dir      = h->intra4x4_pred_mode_cache[scan8[i]];
655
656                 if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
657                     h->hpc.pred4x4_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
658                 } else {
659                     uint8_t *topright;
660                     int nnz, tr;
661                     uint64_t tr_high;
662                     if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
663                         const int topright_avail = (h->topright_samples_available << i) & 0x8000;
664                         av_assert2(h->mb_y || linesize <= block_offset[i]);
665                         if (!topright_avail) {
666                             if (pixel_shift) {
667                                 tr_high  = ((uint16_t *)ptr)[3 - linesize / 2] * 0x0001000100010001ULL;
668                                 topright = (uint8_t *)&tr_high;
669                             } else {
670                                 tr       = ptr[3 - linesize] * 0x01010101u;
671                                 topright = (uint8_t *)&tr;
672                             }
673                         } else
674                             topright = ptr + (4 << pixel_shift) - linesize;
675                     } else
676                         topright = NULL;
677
678                     h->hpc.pred4x4[dir](ptr, topright, linesize);
679                     nnz = h->non_zero_count_cache[scan8[i + p * 16]];
680                     if (nnz) {
681                         if (is_h264) {
682                             if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
683                                 idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
684                             else
685                                 idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
686                         } else if (CONFIG_SVQ3_DECODER)
687                             ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize, qscale, 0);
688                     }
689                 }
690             }
691         }
692     } else {
693         h->hpc.pred16x16[h->intra16x16_pred_mode](dest_y, linesize);
694         if (is_h264) {
695             if (h->non_zero_count_cache[scan8[LUMA_DC_BLOCK_INDEX + p]]) {
696                 if (!transform_bypass)
697                     h->h264dsp.h264_luma_dc_dequant_idct(h->mb + (p * 256 << pixel_shift),
698                                                          h->mb_luma_dc[p],
699                                                          h->dequant4_coeff[p][qscale][0]);
700                 else {
701                     static const uint8_t dc_mapping[16] = {
702                          0 * 16,  1 * 16,  4 * 16,  5 * 16,
703                          2 * 16,  3 * 16,  6 * 16,  7 * 16,
704                          8 * 16,  9 * 16, 12 * 16, 13 * 16,
705                         10 * 16, 11 * 16, 14 * 16, 15 * 16
706                     };
707                     for (i = 0; i < 16; i++)
708                         dctcoef_set(h->mb + (p * 256 << pixel_shift),
709                                     pixel_shift, dc_mapping[i],
710                                     dctcoef_get(h->mb_luma_dc[p],
711                                                 pixel_shift, i));
712                 }
713             }
714         } else if (CONFIG_SVQ3_DECODER)
715             ff_svq3_luma_dc_dequant_idct_c(h->mb + p * 256,
716                                            h->mb_luma_dc[p], qscale);
717     }
718 }
719
720 static av_always_inline void hl_decode_mb_idct_luma(H264Context *h, int mb_type,
721                                                     int is_h264, int simple,
722                                                     int transform_bypass,
723                                                     int pixel_shift,
724                                                     int *block_offset,
725                                                     int linesize,
726                                                     uint8_t *dest_y, int p)
727 {
728     void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
729     int i;
730     block_offset += 16 * p;
731     if (!IS_INTRA4x4(mb_type)) {
732         if (is_h264) {
733             if (IS_INTRA16x16(mb_type)) {
734                 if (transform_bypass) {
735                     if (h->sps.profile_idc == 244 &&
736                         (h->intra16x16_pred_mode == VERT_PRED8x8 ||
737                          h->intra16x16_pred_mode == HOR_PRED8x8)) {
738                         h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset,
739                                                                       h->mb + (p * 256 << pixel_shift),
740                                                                       linesize);
741                     } else {
742                         for (i = 0; i < 16; i++)
743                             if (h->non_zero_count_cache[scan8[i + p * 16]] ||
744                                 dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
745                                 h->h264dsp.h264_add_pixels4_clear(dest_y + block_offset[i],
746                                                                   h->mb + (i * 16 + p * 256 << pixel_shift),
747                                                                   linesize);
748                     }
749                 } else {
750                     h->h264dsp.h264_idct_add16intra(dest_y, block_offset,
751                                                     h->mb + (p * 256 << pixel_shift),
752                                                     linesize,
753                                                     h->non_zero_count_cache + p * 5 * 8);
754                 }
755             } else if (h->cbp & 15) {
756                 if (transform_bypass) {
757                     const int di = IS_8x8DCT(mb_type) ? 4 : 1;
758                     idct_add = IS_8x8DCT(mb_type) ? h->h264dsp.h264_add_pixels8_clear
759                                                   : h->h264dsp.h264_add_pixels4_clear;
760                     for (i = 0; i < 16; i += di)
761                         if (h->non_zero_count_cache[scan8[i + p * 16]])
762                             idct_add(dest_y + block_offset[i],
763                                      h->mb + (i * 16 + p * 256 << pixel_shift),
764                                      linesize);
765                 } else {
766                     if (IS_8x8DCT(mb_type))
767                         h->h264dsp.h264_idct8_add4(dest_y, block_offset,
768                                                    h->mb + (p * 256 << pixel_shift),
769                                                    linesize,
770                                                    h->non_zero_count_cache + p * 5 * 8);
771                     else
772                         h->h264dsp.h264_idct_add16(dest_y, block_offset,
773                                                    h->mb + (p * 256 << pixel_shift),
774                                                    linesize,
775                                                    h->non_zero_count_cache + p * 5 * 8);
776                 }
777             }
778         } else if (CONFIG_SVQ3_DECODER) {
779             for (i = 0; i < 16; i++)
780                 if (h->non_zero_count_cache[scan8[i + p * 16]] || h->mb[i * 16 + p * 256]) {
781                     // FIXME benchmark weird rule, & below
782                     uint8_t *const ptr = dest_y + block_offset[i];
783                     ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize,
784                                        h->qscale, IS_INTRA(mb_type) ? 1 : 0);
785                 }
786         }
787     }
788 }
789
790 #define BITS   8
791 #define SIMPLE 1
792 #include "h264_mb_template.c"
793
794 #undef  BITS
795 #define BITS   16
796 #include "h264_mb_template.c"
797
798 #undef  SIMPLE
799 #define SIMPLE 0
800 #include "h264_mb_template.c"
801
802 void ff_h264_hl_decode_mb(H264Context *h)
803 {
804     const int mb_xy   = h->mb_xy;
805     const int mb_type = h->cur_pic.mb_type[mb_xy];
806     int is_complex    = CONFIG_SMALL || h->is_complex ||
807                         IS_INTRA_PCM(mb_type) || h->qscale == 0;
808
809     if (CHROMA444(h)) {
810         if (is_complex || h->pixel_shift)
811             hl_decode_mb_444_complex(h);
812         else
813             hl_decode_mb_444_simple_8(h);
814     } else if (is_complex) {
815         hl_decode_mb_complex(h);
816     } else if (h->pixel_shift) {
817         hl_decode_mb_simple_16(h);
818     } else
819         hl_decode_mb_simple_8(h);
820 }