h264.c 159 KB
Newer Older
Michael Niedermayer's avatar
Michael Niedermayer committed
1
/*
Alex Converse's avatar
Alex Converse committed
2
 * H.26L/H.264/AVC/JVT/14496-10/... decoder
Michael Niedermayer's avatar
Michael Niedermayer committed
3 4
 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
Michael Niedermayer's avatar
Michael Niedermayer committed
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
Michael Niedermayer's avatar
Michael Niedermayer committed
11
 *
12
 * Libav is distributed in the hope that it will be useful,
Michael Niedermayer's avatar
Michael Niedermayer committed
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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
Michael Niedermayer's avatar
Michael Niedermayer committed
20
 */
21

Michael Niedermayer's avatar
Michael Niedermayer committed
22
/**
23
 * @file
Michael Niedermayer's avatar
Michael Niedermayer committed
24 25 26 27
 * H.264 / AVC / MPEG4 part10 codec.
 * @author Michael Niedermayer <michaelni@gmx.at>
 */

28
#include "libavutil/imgutils.h"
29
#include "internal.h"
30 31
#include "cabac.h"
#include "cabac_functions.h"
Michael Niedermayer's avatar
Michael Niedermayer committed
32 33 34
#include "dsputil.h"
#include "avcodec.h"
#include "mpegvideo.h"
35
#include "h264.h"
Michael Niedermayer's avatar
Michael Niedermayer committed
36
#include "h264data.h"
37
#include "h264_mvpred.h"
Michael Niedermayer's avatar
Michael Niedermayer committed
38
#include "golomb.h"
39
#include "mathops.h"
40
#include "rectangle.h"
41
#include "thread.h"
42
#include "vdpau_internal.h"
43
#include "libavutil/avassert.h"
Michael Niedermayer's avatar
Michael Niedermayer committed
44

Diego Biurrun's avatar
Diego Biurrun committed
45
// #undef NDEBUG
Michael Niedermayer's avatar
Michael Niedermayer committed
46 47
#include <assert.h>

48 49
const uint16_t ff_h264_mb_sizes[4] = { 256, 384, 512, 768 };

Diego Biurrun's avatar
Diego Biurrun committed
50 51 52 53
static const uint8_t rem6[QP_MAX_NUM + 1] = {
    0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
    3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
    0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
54 55
};

Diego Biurrun's avatar
Diego Biurrun committed
56 57 58 59
static const uint8_t div6[QP_MAX_NUM + 1] = {
    0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3,  3,  3,
    3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6,  6,  6,
    7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
60 61
};

62 63 64
static const enum PixelFormat hwaccel_pixfmt_list_h264_jpeg_420[] = {
    PIX_FMT_DXVA2_VLD,
    PIX_FMT_VAAPI_VLD,
65
    PIX_FMT_VDA_VLD,
66 67 68 69
    PIX_FMT_YUVJ420P,
    PIX_FMT_NONE
};

70
/**
71 72
 * Check if the top & left blocks are available if needed and
 * change the dc mode so it only uses the available blocks.
73
 */
Diego Biurrun's avatar
Diego Biurrun committed
74 75 76 77 78 79 80 81 82
int ff_h264_check_intra4x4_pred_mode(H264Context *h)
{
    MpegEncContext *const s     = &h->s;
    static const int8_t top[12] = {
        -1, 0, LEFT_DC_PRED, -1, -1, -1, -1, -1, 0
    };
    static const int8_t left[12] = {
        0, -1, TOP_DC_PRED, 0, -1, -1, -1, 0, -1, DC_128_PRED
    };
83 84
    int i;

Diego Biurrun's avatar
Diego Biurrun committed
85 86 87 88 89 90 91
    if (!(h->top_samples_available & 0x8000)) {
        for (i = 0; i < 4; i++) {
            int status = top[h->intra4x4_pred_mode_cache[scan8[0] + i]];
            if (status < 0) {
                av_log(h->s.avctx, AV_LOG_ERROR,
                       "top block unavailable for requested intra4x4 mode %d at %d %d\n",
                       status, s->mb_x, s->mb_y);
92
                return -1;
Diego Biurrun's avatar
Diego Biurrun committed
93 94
            } else if (status) {
                h->intra4x4_pred_mode_cache[scan8[0] + i] = status;
95 96 97 98
            }
        }
    }

Diego Biurrun's avatar
Diego Biurrun committed
99 100 101 102 103 104 105 106 107
    if ((h->left_samples_available & 0x8888) != 0x8888) {
        static const int mask[4] = { 0x8000, 0x2000, 0x80, 0x20 };
        for (i = 0; i < 4; i++)
            if (!(h->left_samples_available & mask[i])) {
                int status = left[h->intra4x4_pred_mode_cache[scan8[0] + 8 * i]];
                if (status < 0) {
                    av_log(h->s.avctx, AV_LOG_ERROR,
                           "left block unavailable for requested intra4x4 mode %d at %d %d\n",
                           status, s->mb_x, s->mb_y);
108
                    return -1;
Diego Biurrun's avatar
Diego Biurrun committed
109 110
                } else if (status) {
                    h->intra4x4_pred_mode_cache[scan8[0] + 8 * i] = status;
111 112 113 114 115
                }
            }
    }

    return 0;
Diego Biurrun's avatar
Diego Biurrun committed
116
} // FIXME cleanup like ff_h264_check_intra_pred_mode
117

Michael Niedermayer's avatar
Michael Niedermayer committed
118
/**
119 120
 * Check if the top & left blocks are available if needed and
 * change the dc mode so it only uses the available blocks.
Michael Niedermayer's avatar
Michael Niedermayer committed
121
 */
Diego Biurrun's avatar
Diego Biurrun committed
122 123 124 125 126 127 128 129 130 131
int ff_h264_check_intra_pred_mode(H264Context *h, int mode, int is_chroma)
{
    MpegEncContext *const s     = &h->s;
    static const int8_t top[7]  = { LEFT_DC_PRED8x8, 1, -1, -1 };
    static const int8_t left[7] = { TOP_DC_PRED8x8, -1, 2, -1, DC_128_PRED8x8 };

    if (mode > 6U) {
        av_log(h->s.avctx, AV_LOG_ERROR,
               "out of range intra chroma pred mode at %d %d\n",
               s->mb_x, s->mb_y);
Michael Niedermayer's avatar
Michael Niedermayer committed
132
        return -1;
Loic Le Loarer's avatar
Loic Le Loarer committed
133
    }
134

Diego Biurrun's avatar
Diego Biurrun committed
135 136 137 138 139 140
    if (!(h->top_samples_available & 0x8000)) {
        mode = top[mode];
        if (mode < 0) {
            av_log(h->s.avctx, AV_LOG_ERROR,
                   "top block unavailable for requested intra mode at %d %d\n",
                   s->mb_x, s->mb_y);
Michael Niedermayer's avatar
Michael Niedermayer committed
141 142 143
            return -1;
        }
    }
144

Diego Biurrun's avatar
Diego Biurrun committed
145 146 147 148 149 150 151
    if ((h->left_samples_available & 0x8080) != 0x8080) {
        mode = left[mode];
        if (is_chroma && (h->left_samples_available & 0x8080)) {
            // mad cow disease mode, aka MBAFF + constrained_intra_pred
            mode = ALZHEIMER_DC_L0T_PRED8x8 +
                   (!(h->left_samples_available & 0x8000)) +
                   2 * (mode == DC_128_PRED8x8);
152
        }
Diego Biurrun's avatar
Diego Biurrun committed
153 154 155 156
        if (mode < 0) {
            av_log(h->s.avctx, AV_LOG_ERROR,
                   "left block unavailable for requested intra mode at %d %d\n",
                   s->mb_x, s->mb_y);
Michael Niedermayer's avatar
Michael Niedermayer committed
157
            return -1;
158
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
159 160 161 162 163
    }

    return mode;
}

Diego Biurrun's avatar
Diego Biurrun committed
164 165 166
const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src,
                                  int *dst_length, int *consumed, int length)
{
Michael Niedermayer's avatar
Michael Niedermayer committed
167 168
    int i, si, di;
    uint8_t *dst;
169
    int bufidx;
Michael Niedermayer's avatar
Michael Niedermayer committed
170

Diego Biurrun's avatar
Diego Biurrun committed
171 172 173
    // src[0]&0x80; // forbidden bit
    h->nal_ref_idc   = src[0] >> 5;
    h->nal_unit_type = src[0] & 0x1F;
Michael Niedermayer's avatar
Michael Niedermayer committed
174

Diego Biurrun's avatar
Diego Biurrun committed
175 176
    src++;
    length--;
177

178 179 180 181 182 183 184 185
#define STARTCODE_TEST                                                  \
        if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) {     \
            if (src[i + 2] != 3) {                                      \
                /* startcode, so we must be past the end */             \
                length = i;                                             \
            }                                                           \
            break;                                                      \
        }
186
#if HAVE_FAST_UNALIGNED
187 188 189 190 191
#define FIND_FIRST_ZERO                                                 \
        if (i > 0 && !src[i])                                           \
            i--;                                                        \
        while (src[i])                                                  \
            i++
Diego Biurrun's avatar
Diego Biurrun committed
192 193 194 195 196
#if HAVE_FAST_64BIT
    for (i = 0; i + 1 < length; i += 9) {
        if (!((~AV_RN64A(src + i) &
               (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
              0x8000800080008080ULL))
197 198 199 200 201
            continue;
        FIND_FIRST_ZERO;
        STARTCODE_TEST;
        i -= 7;
    }
Diego Biurrun's avatar
Diego Biurrun committed
202 203 204 205 206
#else
    for (i = 0; i + 1 < length; i += 5) {
        if (!((~AV_RN32A(src + i) &
               (AV_RN32A(src + i) - 0x01000101U)) &
              0x80008080U))
207
            continue;
208 209 210 211 212
        FIND_FIRST_ZERO;
        STARTCODE_TEST;
        i -= 3;
    }
#endif
213
#else
Diego Biurrun's avatar
Diego Biurrun committed
214 215 216 217 218
    for (i = 0; i + 1 < length; i += 2) {
        if (src[i])
            continue;
        if (i > 0 && src[i - 1] == 0)
            i--;
219
        STARTCODE_TEST;
Michael Niedermayer's avatar
Michael Niedermayer committed
220
    }
221
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
222

Diego Biurrun's avatar
Diego Biurrun committed
223 224 225
    if (i >= length - 1) { // no escaped 0
        *dst_length = length;
        *consumed   = length + 1; // +1 for the header
226
        return src;
Michael Niedermayer's avatar
Michael Niedermayer committed
227 228
    }

Diego Biurrun's avatar
Diego Biurrun committed
229 230 231 232 233
    // use second escape buffer for inter data
    bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0;
    av_fast_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx],
                   length + FF_INPUT_BUFFER_PADDING_SIZE);
    dst = h->rbsp_buffer[bufidx];
Michael Niedermayer's avatar
Michael Niedermayer committed
234

Diego Biurrun's avatar
Diego Biurrun committed
235
    if (dst == NULL)
236 237
        return NULL;

Diego Biurrun's avatar
Diego Biurrun committed
238
    // printf("decoding esc\n");
239
    memcpy(dst, src, i);
Diego Biurrun's avatar
Diego Biurrun committed
240 241 242 243 244 245 246 247 248 249 250
    si = di = i;
    while (si + 2 < length) {
        // remove escapes (very rare 1:2^22)
        if (src[si + 2] > 3) {
            dst[di++] = src[si++];
            dst[di++] = src[si++];
        } else if (src[si] == 0 && src[si + 1] == 0) {
            if (src[si + 2] == 3) { // escape
                dst[di++]  = 0;
                dst[di++]  = 0;
                si        += 3;
251
                continue;
Diego Biurrun's avatar
Diego Biurrun committed
252
            } else // next start code
253
                goto nsc;
Michael Niedermayer's avatar
Michael Niedermayer committed
254 255
        }

Diego Biurrun's avatar
Diego Biurrun committed
256
        dst[di++] = src[si++];
Michael Niedermayer's avatar
Michael Niedermayer committed
257
    }
Diego Biurrun's avatar
Diego Biurrun committed
258 259
    while (si < length)
        dst[di++] = src[si++];
260
nsc:
Michael Niedermayer's avatar
Michael Niedermayer committed
261

Diego Biurrun's avatar
Diego Biurrun committed
262
    memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
263

Diego Biurrun's avatar
Diego Biurrun committed
264 265 266 267
    *dst_length = di;
    *consumed   = si + 1; // +1 for the header
    /* FIXME store exact number of bits in the getbitcontext
     * (it is needed for decoding) */
Michael Niedermayer's avatar
Michael Niedermayer committed
268 269 270
    return dst;
}

271 272 273 274
/**
 * Identify the exact end of the bitstream
 * @return the length of the trailing, or 0 if damaged
 */
275
static int decode_rbsp_trailing(H264Context *h, const uint8_t *src)
Diego Biurrun's avatar
Diego Biurrun committed
276 277
{
    int v = *src;
Michael Niedermayer's avatar
Michael Niedermayer committed
278 279
    int r;

Michel Bardiaux's avatar
Michel Bardiaux committed
280
    tprintf(h->s.avctx, "rbsp trailing %X\n", v);
Michael Niedermayer's avatar
Michael Niedermayer committed
281

Diego Biurrun's avatar
Diego Biurrun committed
282 283 284 285
    for (r = 1; r < 9; r++) {
        if (v & 1)
            return r;
        v >>= 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
286 287 288 289
    }
    return 0;
}

Diego Biurrun's avatar
Diego Biurrun committed
290 291 292 293 294 295 296 297
static inline int get_lowest_part_list_y(H264Context *h, Picture *pic, int n,
                                         int height, int y_offset, int list)
{
    int raw_my        = h->mv_cache[list][scan8[n]][1];
    int filter_height = (raw_my & 3) ? 2 : 0;
    int full_my       = (raw_my >> 2) + y_offset;
    int top           = full_my - filter_height;
    int bottom        = full_my + filter_height + height;
298 299 300 301

    return FFMAX(abs(top), bottom);
}

Diego Biurrun's avatar
Diego Biurrun committed
302 303 304 305 306
static inline void get_lowest_part_y(H264Context *h, int refs[2][48], int n,
                                     int height, int y_offset, int list0,
                                     int list1, int *nrefs)
{
    MpegEncContext *const s = &h->s;
307 308
    int my;

Diego Biurrun's avatar
Diego Biurrun committed
309
    y_offset += 16 * (s->mb_y >> MB_FIELD);
310

Diego Biurrun's avatar
Diego Biurrun committed
311 312 313
    if (list0) {
        int ref_n    = h->ref_cache[0][scan8[n]];
        Picture *ref = &h->ref_list[0][ref_n];
314 315 316 317

        // Error resilience puts the current picture in the ref list.
        // Don't try to wait on these as it will cause a deadlock.
        // Fields can wait on each other, though.
Diego Biurrun's avatar
Diego Biurrun committed
318 319
        if (ref->f.thread_opaque   != s->current_picture.f.thread_opaque ||
            (ref->f.reference & 3) != s->picture_structure) {
320
            my = get_lowest_part_list_y(h, ref, n, height, y_offset, 0);
Diego Biurrun's avatar
Diego Biurrun committed
321 322
            if (refs[0][ref_n] < 0)
                nrefs[0] += 1;
323 324 325 326
            refs[0][ref_n] = FFMAX(refs[0][ref_n], my);
        }
    }

Diego Biurrun's avatar
Diego Biurrun committed
327 328 329
    if (list1) {
        int ref_n    = h->ref_cache[1][scan8[n]];
        Picture *ref = &h->ref_list[1][ref_n];
330

Diego Biurrun's avatar
Diego Biurrun committed
331 332
        if (ref->f.thread_opaque   != s->current_picture.f.thread_opaque ||
            (ref->f.reference & 3) != s->picture_structure) {
333
            my = get_lowest_part_list_y(h, ref, n, height, y_offset, 1);
Diego Biurrun's avatar
Diego Biurrun committed
334 335
            if (refs[1][ref_n] < 0)
                nrefs[1] += 1;
336 337 338 339 340 341 342 343 344 345
            refs[1][ref_n] = FFMAX(refs[1][ref_n], my);
        }
    }
}

/**
 * Wait until all reference frames are available for MC operations.
 *
 * @param h the H264 context
 */
Diego Biurrun's avatar
Diego Biurrun committed
346 347 348 349
static void await_references(H264Context *h)
{
    MpegEncContext *const s = &h->s;
    const int mb_xy   = h->mb_xy;
350
    const int mb_type = s->current_picture.f.mb_type[mb_xy];
351
    int refs[2][48];
Diego Biurrun's avatar
Diego Biurrun committed
352
    int nrefs[2] = { 0 };
353 354 355 356
    int ref, list;

    memset(refs, -1, sizeof(refs));

Diego Biurrun's avatar
Diego Biurrun committed
357
    if (IS_16X16(mb_type)) {
358
        get_lowest_part_y(h, refs, 0, 16, 0,
Diego Biurrun's avatar
Diego Biurrun committed
359 360
                          IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
    } else if (IS_16X8(mb_type)) {
361
        get_lowest_part_y(h, refs, 0, 8, 0,
Diego Biurrun's avatar
Diego Biurrun committed
362
                          IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
363
        get_lowest_part_y(h, refs, 8, 8, 8,
Diego Biurrun's avatar
Diego Biurrun committed
364 365
                          IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
    } else if (IS_8X16(mb_type)) {
366
        get_lowest_part_y(h, refs, 0, 16, 0,
Diego Biurrun's avatar
Diego Biurrun committed
367
                          IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
368
        get_lowest_part_y(h, refs, 4, 16, 0,
Diego Biurrun's avatar
Diego Biurrun committed
369 370
                          IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
    } else {
371 372 373 374
        int i;

        assert(IS_8X8(mb_type));

Diego Biurrun's avatar
Diego Biurrun committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
        for (i = 0; i < 4; i++) {
            const int sub_mb_type = h->sub_mb_type[i];
            const int n           = 4 * i;
            int y_offset          = (i & 2) << 2;

            if (IS_SUB_8X8(sub_mb_type)) {
                get_lowest_part_y(h, refs, n, 8, y_offset,
                                  IS_DIR(sub_mb_type, 0, 0),
                                  IS_DIR(sub_mb_type, 0, 1),
                                  nrefs);
            } else if (IS_SUB_8X4(sub_mb_type)) {
                get_lowest_part_y(h, refs, n, 4, y_offset,
                                  IS_DIR(sub_mb_type, 0, 0),
                                  IS_DIR(sub_mb_type, 0, 1),
                                  nrefs);
                get_lowest_part_y(h, refs, n + 2, 4, y_offset + 4,
                                  IS_DIR(sub_mb_type, 0, 0),
                                  IS_DIR(sub_mb_type, 0, 1),
                                  nrefs);
            } else if (IS_SUB_4X8(sub_mb_type)) {
                get_lowest_part_y(h, refs, n, 8, y_offset,
                                  IS_DIR(sub_mb_type, 0, 0),
                                  IS_DIR(sub_mb_type, 0, 1),
                                  nrefs);
                get_lowest_part_y(h, refs, n + 1, 8, y_offset,
                                  IS_DIR(sub_mb_type, 0, 0),
                                  IS_DIR(sub_mb_type, 0, 1),
                                  nrefs);
            } else {
404 405
                int j;
                assert(IS_SUB_4X4(sub_mb_type));
Diego Biurrun's avatar
Diego Biurrun committed
406 407 408 409 410 411
                for (j = 0; j < 4; j++) {
                    int sub_y_offset = y_offset + 2 * (j & 2);
                    get_lowest_part_y(h, refs, n + j, 4, sub_y_offset,
                                      IS_DIR(sub_mb_type, 0, 0),
                                      IS_DIR(sub_mb_type, 0, 1),
                                      nrefs);
412 413 414 415 416
                }
            }
        }
    }

Diego Biurrun's avatar
Diego Biurrun committed
417 418
    for (list = h->list_count - 1; list >= 0; list--)
        for (ref = 0; ref < 48 && nrefs[list]; ref++) {
419
            int row = refs[list][ref];
Diego Biurrun's avatar
Diego Biurrun committed
420 421 422
            if (row >= 0) {
                Picture *ref_pic      = &h->ref_list[list][ref];
                int ref_field         = ref_pic->f.reference - 1;
423
                int ref_field_picture = ref_pic->field_picture;
Diego Biurrun's avatar
Diego Biurrun committed
424
                int pic_height        = 16 * s->mb_height >> ref_field_picture;
425 426 427 428

                row <<= MB_MBAFF;
                nrefs[list]--;

Diego Biurrun's avatar
Diego Biurrun committed
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
                if (!FIELD_PICTURE && ref_field_picture) { // frame referencing two fields
                    ff_thread_await_progress(&ref_pic->f,
                                             FFMIN((row >> 1) - !(row & 1),
                                                   pic_height - 1),
                                             1);
                    ff_thread_await_progress(&ref_pic->f,
                                             FFMIN((row >> 1), pic_height - 1),
                                             0);
                } else if (FIELD_PICTURE && !ref_field_picture) { // field referencing one field of a frame
                    ff_thread_await_progress(&ref_pic->f,
                                             FFMIN(row * 2 + ref_field,
                                                   pic_height - 1),
                                             0);
                } else if (FIELD_PICTURE) {
                    ff_thread_await_progress(&ref_pic->f,
                                             FFMIN(row, pic_height - 1),
                                             ref_field);
                } else {
                    ff_thread_await_progress(&ref_pic->f,
                                             FFMIN(row, pic_height - 1),
                                             0);
450 451 452
                }
            }
        }
453 454
}

Diego Biurrun's avatar
Diego Biurrun committed
455 456 457 458 459 460 461 462 463
static av_always_inline void mc_dir_part(H264Context *h, Picture *pic,
                                         int n, int square, int height,
                                         int delta, int list,
                                         uint8_t *dest_y, uint8_t *dest_cb,
                                         uint8_t *dest_cr,
                                         int src_x_offset, int src_y_offset,
                                         qpel_mc_func *qpix_op,
                                         h264_chroma_mc_func chroma_op,
                                         int pixel_shift, int chroma_idc)
464
{
Diego Biurrun's avatar
Diego Biurrun committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478
    MpegEncContext *const s = &h->s;
    const int mx      = h->mv_cache[list][scan8[n]][0] + src_x_offset * 8;
    int my            = h->mv_cache[list][scan8[n]][1] + src_y_offset * 8;
    const int luma_xy = (mx & 3) + ((my & 3) << 2);
    int offset        = ((mx >> 2) << pixel_shift) + (my >> 2) * h->mb_linesize;
    uint8_t *src_y    = pic->f.data[0] + offset;
    uint8_t *src_cb, *src_cr;
    int extra_width  = h->emu_edge_width;
    int extra_height = h->emu_edge_height;
    int emu = 0;
    const int full_mx    = mx >> 2;
    const int full_my    = my >> 2;
    const int pic_width  = 16 * s->mb_width;
    const int pic_height = 16 * s->mb_height >> MB_FIELD;
479
    int ysh;
480

Diego Biurrun's avatar
Diego Biurrun committed
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
    if (mx & 7)
        extra_width -= 3;
    if (my & 7)
        extra_height -= 3;

    if (full_mx                <          0 - extra_width  ||
        full_my                <          0 - extra_height ||
        full_mx + 16 /*FIXME*/ > pic_width  + extra_width  ||
        full_my + 16 /*FIXME*/ > pic_height + extra_height) {
        s->dsp.emulated_edge_mc(s->edge_emu_buffer,
                                src_y - (2 << pixel_shift) - 2 * h->mb_linesize,
                                h->mb_linesize,
                                16 + 5, 16 + 5 /*FIXME*/, full_mx - 2,
                                full_my - 2, pic_width, pic_height);
        src_y = s->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
        emu   = 1;
    }

    qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); // FIXME try variable height perhaps?
    if (!square)
501
        qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
502

Diego Biurrun's avatar
Diego Biurrun committed
503 504
    if (CONFIG_GRAY && s->flags & CODEC_FLAG_GRAY)
        return;
505

Diego Biurrun's avatar
Diego Biurrun committed
506
    if (chroma_idc == 3 /* yuv444 */) {
507
        src_cb = pic->f.data[1] + offset;
Diego Biurrun's avatar
Diego Biurrun committed
508 509 510 511 512 513 514 515
        if (emu) {
            s->dsp.emulated_edge_mc(s->edge_emu_buffer,
                                    src_cb - (2 << pixel_shift) - 2 * h->mb_linesize,
                                    h->mb_linesize,
                                    16 + 5, 16 + 5 /*FIXME*/,
                                    full_mx - 2, full_my - 2,
                                    pic_width, pic_height);
            src_cb = s->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
516
        }
Diego Biurrun's avatar
Diego Biurrun committed
517 518
        qpix_op[luma_xy](dest_cb, src_cb, h->mb_linesize); // FIXME try variable height perhaps?
        if (!square)
519 520
            qpix_op[luma_xy](dest_cb + delta, src_cb + delta, h->mb_linesize);

521
        src_cr = pic->f.data[2] + offset;
Diego Biurrun's avatar
Diego Biurrun committed
522 523 524 525 526 527 528 529
        if (emu) {
            s->dsp.emulated_edge_mc(s->edge_emu_buffer,
                                    src_cr - (2 << pixel_shift) - 2 * h->mb_linesize,
                                    h->mb_linesize,
                                    16 + 5, 16 + 5 /*FIXME*/,
                                    full_mx - 2, full_my - 2,
                                    pic_width, pic_height);
            src_cr = s->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
530
        }
Diego Biurrun's avatar
Diego Biurrun committed
531 532
        qpix_op[luma_xy](dest_cr, src_cr, h->mb_linesize); // FIXME try variable height perhaps?
        if (!square)
533 534 535 536
            qpix_op[luma_xy](dest_cr + delta, src_cr + delta, h->mb_linesize);
        return;
    }

537
    ysh = 3 - (chroma_idc == 2 /* yuv422 */);
Diego Biurrun's avatar
Diego Biurrun committed
538
    if (chroma_idc == 1 /* yuv420 */ && MB_FIELD) {
539
        // chroma offset when predicting from a field of opposite parity
Diego Biurrun's avatar
Diego Biurrun committed
540 541
        my  += 2 * ((s->mb_y & 1) - (pic->f.reference - 1));
        emu |= (my >> 3) < 0 || (my >> 3) + 8 >= (pic_height >> 1);
542
    }
543

Diego Biurrun's avatar
Diego Biurrun committed
544 545 546 547
    src_cb = pic->f.data[1] + ((mx >> 3) << pixel_shift) +
             (my >> ysh) * h->mb_uvlinesize;
    src_cr = pic->f.data[2] + ((mx >> 3) << pixel_shift) +
             (my >> ysh) * h->mb_uvlinesize;
548

Diego Biurrun's avatar
Diego Biurrun committed
549
    if (emu) {
550 551 552
        s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_cb, h->mb_uvlinesize,
                                9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
                                pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
Diego Biurrun's avatar
Diego Biurrun committed
553
        src_cb = s->edge_emu_buffer;
Michael Niedermayer's avatar
Michael Niedermayer committed
554
    }
Diego Biurrun's avatar
Diego Biurrun committed
555 556 557
    chroma_op(dest_cb, src_cb, h->mb_uvlinesize,
              height >> (chroma_idc == 1 /* yuv420 */),
              mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
Michael Niedermayer's avatar
Michael Niedermayer committed
558

Diego Biurrun's avatar
Diego Biurrun committed
559
    if (emu) {
560 561 562
        s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_cr, h->mb_uvlinesize,
                                9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
                                pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
Diego Biurrun's avatar
Diego Biurrun committed
563
        src_cr = s->edge_emu_buffer;
Michael Niedermayer's avatar
Michael Niedermayer committed
564
    }
565
    chroma_op(dest_cr, src_cr, h->mb_uvlinesize, height >> (chroma_idc == 1 /* yuv420 */),
Diego Biurrun's avatar
Diego Biurrun committed
566
              mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
Michael Niedermayer's avatar
Michael Niedermayer committed
567 568
}

Diego Biurrun's avatar
Diego Biurrun committed
569 570 571 572 573 574 575 576 577 578 579
static av_always_inline void mc_part_std(H264Context *h, int n, int square,
                                         int height, int delta,
                                         uint8_t *dest_y, uint8_t *dest_cb,
                                         uint8_t *dest_cr,
                                         int x_offset, int y_offset,
                                         qpel_mc_func *qpix_put,
                                         h264_chroma_mc_func chroma_put,
                                         qpel_mc_func *qpix_avg,
                                         h264_chroma_mc_func chroma_avg,
                                         int list0, int list1,
                                         int pixel_shift, int chroma_idc)
580
{
Diego Biurrun's avatar
Diego Biurrun committed
581 582 583
    MpegEncContext *const s       = &h->s;
    qpel_mc_func *qpix_op         = qpix_put;
    h264_chroma_mc_func chroma_op = chroma_put;
584

Diego Biurrun's avatar
Diego Biurrun committed
585
    dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
586
    if (chroma_idc == 3 /* yuv444 */) {
Diego Biurrun's avatar
Diego Biurrun committed
587 588
        dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
        dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
589
    } else if (chroma_idc == 2 /* yuv422 */) {
Diego Biurrun's avatar
Diego Biurrun committed
590 591 592 593 594
        dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
        dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
    } else { /* yuv420 */
        dest_cb += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
        dest_cr += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
595
    }
Diego Biurrun's avatar
Diego Biurrun committed
596 597
    x_offset += 8 * s->mb_x;
    y_offset += 8 * (s->mb_y >> MB_FIELD);
598

Diego Biurrun's avatar
Diego Biurrun committed
599 600
    if (list0) {
        Picture *ref = &h->ref_list[0][h->ref_cache[0][scan8[n]]];
601
        mc_dir_part(h, ref, n, square, height, delta, 0,
Diego Biurrun's avatar
Diego Biurrun committed
602 603
                    dest_y, dest_cb, dest_cr, x_offset, y_offset,
                    qpix_op, chroma_op, pixel_shift, chroma_idc);
Michael Niedermayer's avatar
Michael Niedermayer committed
604

Diego Biurrun's avatar
Diego Biurrun committed
605 606
        qpix_op   = qpix_avg;
        chroma_op = chroma_avg;
Michael Niedermayer's avatar
Michael Niedermayer committed
607 608
    }

Diego Biurrun's avatar
Diego Biurrun committed
609 610
    if (list1) {
        Picture *ref = &h->ref_list[1][h->ref_cache[1][scan8[n]]];
611
        mc_dir_part(h, ref, n, square, height, delta, 1,
Diego Biurrun's avatar
Diego Biurrun committed
612 613
                    dest_y, dest_cb, dest_cr, x_offset, y_offset,
                    qpix_op, chroma_op, pixel_shift, chroma_idc);
Michael Niedermayer's avatar
Michael Niedermayer committed
614 615 616
    }
}

Diego Biurrun's avatar
Diego Biurrun committed
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
static av_always_inline void mc_part_weighted(H264Context *h, int n, int square,
                                              int height, int delta,
                                              uint8_t *dest_y, uint8_t *dest_cb,
                                              uint8_t *dest_cr,
                                              int x_offset, int y_offset,
                                              qpel_mc_func *qpix_put,
                                              h264_chroma_mc_func chroma_put,
                                              h264_weight_func luma_weight_op,
                                              h264_weight_func chroma_weight_op,
                                              h264_biweight_func luma_weight_avg,
                                              h264_biweight_func chroma_weight_avg,
                                              int list0, int list1,
                                              int pixel_shift, int chroma_idc)
{
    MpegEncContext *const s = &h->s;
632
    int chroma_height;
Loren Merritt's avatar
Loren Merritt committed
633

Diego Biurrun's avatar
Diego Biurrun committed
634
    dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
635
    if (chroma_idc == 3 /* yuv444 */) {
Diego Biurrun's avatar
Diego Biurrun committed
636
        chroma_height     = height;
637
        chroma_weight_avg = luma_weight_avg;
Diego Biurrun's avatar
Diego Biurrun committed
638 639 640
        chroma_weight_op  = luma_weight_op;
        dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
        dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
641
    } else if (chroma_idc == 2 /* yuv422 */) {
642
        chroma_height = height;
Diego Biurrun's avatar
Diego Biurrun committed
643 644 645
        dest_cb      += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
        dest_cr      += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
    } else { /* yuv420 */
646
        chroma_height = height >> 1;
Diego Biurrun's avatar
Diego Biurrun committed
647 648
        dest_cb      += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
        dest_cr      += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
649
    }
Diego Biurrun's avatar
Diego Biurrun committed
650 651
    x_offset += 8 * s->mb_x;
    y_offset += 8 * (s->mb_y >> MB_FIELD);
652

Diego Biurrun's avatar
Diego Biurrun committed
653
    if (list0 && list1) {
Loren Merritt's avatar
Loren Merritt committed
654 655 656
        /* don't optimize for luma-only case, since B-frames usually
         * use implicit weights => chroma too. */
        uint8_t *tmp_cb = s->obmc_scratchpad;
657
        uint8_t *tmp_cr = s->obmc_scratchpad + (16 << pixel_shift);
Diego Biurrun's avatar
Diego Biurrun committed
658 659 660
        uint8_t *tmp_y  = s->obmc_scratchpad + 16 * h->mb_uvlinesize;
        int refn0       = h->ref_cache[0][scan8[n]];
        int refn1       = h->ref_cache[1][scan8[n]];
Loren Merritt's avatar
Loren Merritt committed
661

662
        mc_dir_part(h, &h->ref_list[0][refn0], n, square, height, delta, 0,
Loren Merritt's avatar
Loren Merritt committed
663
                    dest_y, dest_cb, dest_cr,
664 665
                    x_offset, y_offset, qpix_put, chroma_put,
                    pixel_shift, chroma_idc);
666
        mc_dir_part(h, &h->ref_list[1][refn1], n, square, height, delta, 1,
Loren Merritt's avatar
Loren Merritt committed
667
                    tmp_y, tmp_cb, tmp_cr,
668 669
                    x_offset, y_offset, qpix_put, chroma_put,
                    pixel_shift, chroma_idc);
Loren Merritt's avatar
Loren Merritt committed
670

Diego Biurrun's avatar
Diego Biurrun committed
671 672
        if (h->use_weight == 2) {
            int weight0 = h->implicit_weight[refn0][refn1][s->mb_y & 1];
Loren Merritt's avatar
Loren Merritt committed
673
            int weight1 = 64 - weight0;
Diego Biurrun's avatar
Diego Biurrun committed
674 675
            luma_weight_avg(dest_y, tmp_y, h->mb_linesize,
                            height, 5, weight0, weight1, 0);
676 677 678 679
            chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize,
                              chroma_height, 5, weight0, weight1, 0);
            chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize,
                              chroma_height, 5, weight0, weight1, 0);
Diego Biurrun's avatar
Diego Biurrun committed
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
        } else {
            luma_weight_avg(dest_y, tmp_y, h->mb_linesize, height,
                            h->luma_log2_weight_denom,
                            h->luma_weight[refn0][0][0],
                            h->luma_weight[refn1][1][0],
                            h->luma_weight[refn0][0][1] +
                            h->luma_weight[refn1][1][1]);
            chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, chroma_height,
                              h->chroma_log2_weight_denom,
                              h->chroma_weight[refn0][0][0][0],
                              h->chroma_weight[refn1][1][0][0],
                              h->chroma_weight[refn0][0][0][1] +
                              h->chroma_weight[refn1][1][0][1]);
            chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, chroma_height,
                              h->chroma_log2_weight_denom,
                              h->chroma_weight[refn0][0][1][0],
                              h->chroma_weight[refn1][1][1][0],
                              h->chroma_weight[refn0][0][1][1] +
                              h->chroma_weight[refn1][1][1][1]);
Loren Merritt's avatar
Loren Merritt committed
699
        }
Diego Biurrun's avatar
Diego Biurrun committed
700 701 702 703
    } else {
        int list     = list1 ? 1 : 0;
        int refn     = h->ref_cache[list][scan8[n]];
        Picture *ref = &h->ref_list[list][refn];
704
        mc_dir_part(h, ref, n, square, height, delta, list,
Loren Merritt's avatar
Loren Merritt committed
705
                    dest_y, dest_cb, dest_cr, x_offset, y_offset,
706
                    qpix_put, chroma_put, pixel_shift, chroma_idc);
Loren Merritt's avatar
Loren Merritt committed
707

Diego Biurrun's avatar
Diego Biurrun committed
708 709 710 711 712 713 714 715 716 717 718 719 720
        luma_weight_op(dest_y, h->mb_linesize, height,
                       h->luma_log2_weight_denom,
                       h->luma_weight[refn][list][0],
                       h->luma_weight[refn][list][1]);
        if (h->use_weight_chroma) {
            chroma_weight_op(dest_cb, h->mb_uvlinesize, chroma_height,
                             h->chroma_log2_weight_denom,
                             h->chroma_weight[refn][list][0][0],
                             h->chroma_weight[refn][list][0][1]);
            chroma_weight_op(dest_cr, h->mb_uvlinesize, chroma_height,
                             h->chroma_log2_weight_denom,
                             h->chroma_weight[refn][list][1][0],
                             h->chroma_weight[refn][list][1][1]);
Loren Merritt's avatar
Loren Merritt committed
721 722 723 724
        }
    }
}

Diego Biurrun's avatar
Diego Biurrun committed
725 726
static av_always_inline void prefetch_motion(H264Context *h, int list,
                                             int pixel_shift, int chroma_idc)
727
{
728 729
    /* fetch pixels for estimated mv 4 macroblocks ahead
     * optimized for 64byte cache lines */
Diego Biurrun's avatar
Diego Biurrun committed
730
    MpegEncContext *const s = &h->s;
731
    const int refn = h->ref_cache[list][scan8[0]];
Diego Biurrun's avatar
Diego Biurrun committed
732 733 734
    if (refn >= 0) {
        const int mx  = (h->mv_cache[list][scan8[0]][0] >> 2) + 16 * s->mb_x + 8;
        const int my  = (h->mv_cache[list][scan8[0]][1] >> 2) + 16 * s->mb_y;
735
        uint8_t **src = h->ref_list[list][refn].f.data;
Diego Biurrun's avatar
Diego Biurrun committed
736 737 738 739
        int off       = (mx << pixel_shift) +
                        (my + (s->mb_x & 3) * 4) * h->mb_linesize +
                        (64 << pixel_shift);
        s->dsp.prefetch(src[0] + off, s->linesize, 4);
740
        if (chroma_idc == 3 /* yuv444 */) {
Diego Biurrun's avatar
Diego Biurrun committed
741 742 743 744 745 746 747
            s->dsp.prefetch(src[1] + off, s->linesize, 4);
            s->dsp.prefetch(src[2] + off, s->linesize, 4);
        } else {
            off = ((mx >> 1) << pixel_shift) +
                  ((my >> 1) + (s->mb_x & 7)) * s->uvlinesize +
                  (64 << pixel_shift);
            s->dsp.prefetch(src[1] + off, src[2] - src[1], 2);
748
        }
749 750 751
    }
}

Diego Biurrun's avatar
Diego Biurrun committed
752 753
static void free_tables(H264Context *h, int free_rbsp)
{
754
    int i;
755
    H264Context *hx;
756

Michael Niedermayer's avatar
Michael Niedermayer committed
757
    av_freep(&h->intra4x4_pred_mode);
758 759
    av_freep(&h->chroma_pred_mode_table);
    av_freep(&h->cbp_table);
760 761
    av_freep(&h->mvd_table[0]);
    av_freep(&h->mvd_table[1]);
762
    av_freep(&h->direct_table);
Michael Niedermayer's avatar
Michael Niedermayer committed
763 764
    av_freep(&h->non_zero_count);
    av_freep(&h->slice_table_base);
Diego Biurrun's avatar
Diego Biurrun committed
765
    h->slice_table = NULL;
766
    av_freep(&h->list_counts);
767

Michael Niedermayer's avatar
Michael Niedermayer committed
768
    av_freep(&h->mb2b_xy);
769
    av_freep(&h->mb2br_xy);
Loren Merritt's avatar
Loren Merritt committed
770

Diego Biurrun's avatar
Diego Biurrun committed
771
    for (i = 0; i < MAX_THREADS; i++) {
772
        hx = h->thread_context[i];
Diego Biurrun's avatar
Diego Biurrun committed
773 774
        if (!hx)
            continue;
775 776 777
        av_freep(&hx->top_borders[1]);
        av_freep(&hx->top_borders[0]);
        av_freep(&hx->s.obmc_scratchpad);
Diego Biurrun's avatar
Diego Biurrun committed
778
        if (free_rbsp) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
779 780 781 782
            av_freep(&hx->rbsp_buffer[1]);
            av_freep(&hx->rbsp_buffer[0]);
            hx->rbsp_buffer_size[0] = 0;
            hx->rbsp_buffer_size[1] = 0;
783
        }
Diego Biurrun's avatar
Diego Biurrun committed
784 785
        if (i)
            av_freep(&h->thread_context[i]);
786
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
787 788
}

Diego Biurrun's avatar
Diego Biurrun committed
789 790 791 792
static void init_dequant8_coeff_table(H264Context *h)
{
    int i, j, q, x;
    const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8);
Loren Merritt's avatar
Loren Merritt committed
793

Diego Biurrun's avatar
Diego Biurrun committed
794
    for (i = 0; i < 6; i++) {
795
        h->dequant8_coeff[i] = h->dequant8_buffer[i];
Diego Biurrun's avatar
Diego Biurrun committed
796 797 798
        for (j = 0; j < i; j++)
            if (!memcmp(h->pps.scaling_matrix8[j], h->pps.scaling_matrix8[i],
                        64 * sizeof(uint8_t))) {
799 800 801
                h->dequant8_coeff[i] = h->dequant8_buffer[j];
                break;
            }
Diego Biurrun's avatar
Diego Biurrun committed
802
        if (j < i)
803
            continue;
Loren Merritt's avatar
Loren Merritt committed
804

Diego Biurrun's avatar
Diego Biurrun committed
805
        for (q = 0; q < max_qp + 1; q++) {
806
            int shift = div6[q];
Diego Biurrun's avatar
Diego Biurrun committed
807 808 809 810 811
            int idx   = rem6[q];
            for (x = 0; x < 64; x++)
                h->dequant8_coeff[i][q][(x >> 3) | ((x & 7) << 3)] =
                    ((uint32_t)dequant8_coeff_init[idx][dequant8_coeff_init_scan[((x >> 1) & 12) | (x & 3)]] *
                     h->pps.scaling_matrix8[i][x]) << shift;
Loren Merritt's avatar
Loren Merritt committed
812 813 814 815
        }
    }
}

Diego Biurrun's avatar
Diego Biurrun committed
816 817 818 819 820
static void init_dequant4_coeff_table(H264Context *h)
{
    int i, j, q, x;
    const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8);
    for (i = 0; i < 6; i++) {
Loren Merritt's avatar
Loren Merritt committed
821
        h->dequant4_coeff[i] = h->dequant4_buffer[i];
Diego Biurrun's avatar
Diego Biurrun committed
822 823 824
        for (j = 0; j < i; j++)
            if (!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i],
                        16 * sizeof(uint8_t))) {
Loren Merritt's avatar
Loren Merritt committed
825 826 827
                h->dequant4_coeff[i] = h->dequant4_buffer[j];
                break;
            }
Diego Biurrun's avatar
Diego Biurrun committed
828
        if (j < i)
Loren Merritt's avatar
Loren Merritt committed
829 830
            continue;

Diego Biurrun's avatar
Diego Biurrun committed
831
        for (q = 0; q < max_qp + 1; q++) {
832
            int shift = div6[q] + 2;
Diego Biurrun's avatar
Diego Biurrun committed
833 834 835 836 837
            int idx   = rem6[q];
            for (x = 0; x < 16; x++)
                h->dequant4_coeff[i][q][(x >> 2) | ((x << 2) & 0xF)] =
                    ((uint32_t)dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] *
                     h->pps.scaling_matrix4[i][x]) << shift;
Loren Merritt's avatar
Loren Merritt committed
838 839 840 841
        }
    }
}

Diego Biurrun's avatar
Diego Biurrun committed
842 843 844
static void init_dequant_tables(H264Context *h)
{
    int i, x;
Loren Merritt's avatar
Loren Merritt committed
845
    init_dequant4_coeff_table(h);
Diego Biurrun's avatar
Diego Biurrun committed
846
    if (h->pps.transform_8x8_mode)
Loren Merritt's avatar
Loren Merritt committed
847
        init_dequant8_coeff_table(h);
Diego Biurrun's avatar
Diego Biurrun committed
848 849 850 851 852 853 854 855
    if (h->sps.transform_bypass) {
        for (i = 0; i < 6; i++)
            for (x = 0; x < 16; x++)
                h->dequant4_coeff[i][0][x] = 1 << 6;
        if (h->pps.transform_8x8_mode)
            for (i = 0; i < 6; i++)
                for (x = 0; x < 64; x++)
                    h->dequant8_coeff[i][0][x] = 1 << 6;
Loren Merritt's avatar
Loren Merritt committed
856 857 858
    }
}