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