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