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