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