Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h264.c @ 727c236a

History | View | Annotate | Download (317 KB)

1 0da71265 Michael Niedermayer
/*
2
 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3
 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 0da71265 Michael Niedermayer
 *
19
 */
20 115329f1 Diego Biurrun
21 0da71265 Michael Niedermayer
/**
22
 * @file h264.c
23
 * H.264 / AVC / MPEG4 part10 codec.
24
 * @author Michael Niedermayer <michaelni@gmx.at>
25
 */
26
27
#include "common.h"
28
#include "dsputil.h"
29
#include "avcodec.h"
30
#include "mpegvideo.h"
31
#include "h264data.h"
32
#include "golomb.h"
33
34 e5017ab8 Laurent Aimar
#include "cabac.h"
35
36 2848ce84 Loren Merritt
//#undef NDEBUG
37 0da71265 Michael Niedermayer
#include <assert.h>
38
39
#define interlaced_dct interlaced_dct_is_a_bad_name
40
#define mb_intra mb_intra_isnt_initalized_see_mb_type
41
42
#define LUMA_DC_BLOCK_INDEX   25
43
#define CHROMA_DC_BLOCK_INDEX 26
44
45
#define CHROMA_DC_COEFF_TOKEN_VLC_BITS 8
46
#define COEFF_TOKEN_VLC_BITS           8
47
#define TOTAL_ZEROS_VLC_BITS           9
48
#define CHROMA_DC_TOTAL_ZEROS_VLC_BITS 3
49
#define RUN_VLC_BITS                   3
50
#define RUN7_VLC_BITS                  6
51
52
#define MAX_SPS_COUNT 32
53
#define MAX_PPS_COUNT 256
54
55
#define MAX_MMCO_COUNT 66
56
57 5d18eaad Loren Merritt
/* Compiling in interlaced support reduces the speed
58
 * of progressive decoding by about 2%. */
59
#define ALLOW_INTERLACE
60
61
#ifdef ALLOW_INTERLACE
62
#define MB_MBAFF h->mb_mbaff
63
#define MB_FIELD h->mb_field_decoding_flag
64
#define FRAME_MBAFF h->mb_aff_frame
65
#else
66
#define MB_MBAFF 0
67
#define MB_FIELD 0
68
#define FRAME_MBAFF 0
69
#undef  IS_INTERLACED
70
#define IS_INTERLACED(mb_type) 0
71
#endif
72
73 0da71265 Michael Niedermayer
/**
74
 * Sequence parameter set
75
 */
76
typedef struct SPS{
77 115329f1 Diego Biurrun
78 0da71265 Michael Niedermayer
    int profile_idc;
79
    int level_idc;
80 36940eca Loren Merritt
    int transform_bypass;              ///< qpprime_y_zero_transform_bypass_flag
81 0da71265 Michael Niedermayer
    int log2_max_frame_num;            ///< log2_max_frame_num_minus4 + 4
82
    int poc_type;                      ///< pic_order_cnt_type
83
    int log2_max_poc_lsb;              ///< log2_max_pic_order_cnt_lsb_minus4
84
    int delta_pic_order_always_zero_flag;
85
    int offset_for_non_ref_pic;
86
    int offset_for_top_to_bottom_field;
87
    int poc_cycle_length;              ///< num_ref_frames_in_pic_order_cnt_cycle
88
    int ref_frame_count;               ///< num_ref_frames
89 a15e68de Michael Niedermayer
    int gaps_in_frame_num_allowed_flag;
90 0da71265 Michael Niedermayer
    int mb_width;                      ///< frame_width_in_mbs_minus1 + 1
91
    int mb_height;                     ///< frame_height_in_mbs_minus1 + 1
92
    int frame_mbs_only_flag;
93
    int mb_aff;                        ///<mb_adaptive_frame_field_flag
94
    int direct_8x8_inference_flag;
95 a15e68de Michael Niedermayer
    int crop;                   ///< frame_cropping_flag
96
    int crop_left;              ///< frame_cropping_rect_left_offset
97
    int crop_right;             ///< frame_cropping_rect_right_offset
98
    int crop_top;               ///< frame_cropping_rect_top_offset
99
    int crop_bottom;            ///< frame_cropping_rect_bottom_offset
100 0da71265 Michael Niedermayer
    int vui_parameters_present_flag;
101 5ff85f1d Michael Niedermayer
    AVRational sar;
102 ec587a50 Måns Rullgård
    int timing_info_present_flag;
103
    uint32_t num_units_in_tick;
104
    uint32_t time_scale;
105
    int fixed_frame_rate_flag;
106 0da71265 Michael Niedermayer
    short offset_for_ref_frame[256]; //FIXME dyn aloc?
107 4e4d983e Loren Merritt
    int bitstream_restriction_flag;
108
    int num_reorder_frames;
109 239ea04c Loren Merritt
    int scaling_matrix_present;
110
    uint8_t scaling_matrix4[6][16];
111
    uint8_t scaling_matrix8[2][64];
112 0da71265 Michael Niedermayer
}SPS;
113
114
/**
115
 * Picture parameter set
116
 */
117
typedef struct PPS{
118
    int sps_id;
119
    int cabac;                  ///< entropy_coding_mode_flag
120
    int pic_order_present;      ///< pic_order_present_flag
121
    int slice_group_count;      ///< num_slice_groups_minus1 + 1
122
    int mb_slice_group_map_type;
123
    int ref_count[2];           ///< num_ref_idx_l0/1_active_minus1 + 1
124
    int weighted_pred;          ///< weighted_pred_flag
125
    int weighted_bipred_idc;
126
    int init_qp;                ///< pic_init_qp_minus26 + 26
127
    int init_qs;                ///< pic_init_qs_minus26 + 26
128
    int chroma_qp_index_offset;
129
    int deblocking_filter_parameters_present; ///< deblocking_filter_parameters_present_flag
130
    int constrained_intra_pred; ///< constrained_intra_pred_flag
131
    int redundant_pic_cnt_present; ///< redundant_pic_cnt_present_flag
132 43efd19a Loren Merritt
    int transform_8x8_mode;     ///< transform_8x8_mode_flag
133 239ea04c Loren Merritt
    uint8_t scaling_matrix4[6][16];
134
    uint8_t scaling_matrix8[2][64];
135 0da71265 Michael Niedermayer
}PPS;
136
137
/**
138
 * Memory management control operation opcode.
139
 */
140
typedef enum MMCOOpcode{
141
    MMCO_END=0,
142
    MMCO_SHORT2UNUSED,
143
    MMCO_LONG2UNUSED,
144
    MMCO_SHORT2LONG,
145
    MMCO_SET_MAX_LONG,
146 115329f1 Diego Biurrun
    MMCO_RESET,
147 0da71265 Michael Niedermayer
    MMCO_LONG,
148
} MMCOOpcode;
149
150
/**
151
 * Memory management control operation.
152
 */
153
typedef struct MMCO{
154
    MMCOOpcode opcode;
155
    int short_frame_num;
156
    int long_index;
157
} MMCO;
158
159
/**
160
 * H264Context
161
 */
162
typedef struct H264Context{
163
    MpegEncContext s;
164 115329f1 Diego Biurrun
    int nal_ref_idc;
165 0da71265 Michael Niedermayer
    int nal_unit_type;
166 bb270c08 Diego Biurrun
#define NAL_SLICE                1
167
#define NAL_DPA                  2
168
#define NAL_DPB                  3
169
#define NAL_DPC                  4
170
#define NAL_IDR_SLICE            5
171
#define NAL_SEI                  6
172
#define NAL_SPS                  7
173
#define NAL_PPS                  8
174
#define NAL_AUD                  9
175 ab470fa7 Loren Merritt
#define NAL_END_SEQUENCE        10
176
#define NAL_END_STREAM          11
177
#define NAL_FILLER_DATA         12
178
#define NAL_SPS_EXT             13
179
#define NAL_AUXILIARY_SLICE     19
180 0da71265 Michael Niedermayer
    uint8_t *rbsp_buffer;
181 f038fe8b Diego Biurrun
    unsigned int rbsp_buffer_size;
182 0da71265 Michael Niedermayer
183 4770b1b4 Roberto Togni
    /**
184
      * Used to parse AVC variant of h264
185
      */
186
    int is_avc; ///< this flag is != 0 if codec is avc1
187
    int got_avcC; ///< flag used to parse avcC data only once
188
    int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
189
190 0da71265 Michael Niedermayer
    int chroma_qp; //QPc
191
192 5d18eaad Loren Merritt
    int prev_mb_skipped;
193
    int next_mb_skipped;
194 0da71265 Michael Niedermayer
195
    //prediction stuff
196
    int chroma_pred_mode;
197
    int intra16x16_pred_mode;
198 6ba71fc4 Loïc Le Loarer
199
    int top_mb_xy;
200
    int left_mb_xy[2];
201 115329f1 Diego Biurrun
202 0da71265 Michael Niedermayer
    int8_t intra4x4_pred_mode_cache[5*8];
203
    int8_t (*intra4x4_pred_mode)[8];
204
    void (*pred4x4  [9+3])(uint8_t *src, uint8_t *topright, int stride);//FIXME move to dsp?
205 43efd19a Loren Merritt
    void (*pred8x8l [9+3])(uint8_t *src, int topleft, int topright, int stride);
206 0da71265 Michael Niedermayer
    void (*pred8x8  [4+3])(uint8_t *src, int stride);
207
    void (*pred16x16[4+3])(uint8_t *src, int stride);
208
    unsigned int topleft_samples_available;
209
    unsigned int top_samples_available;
210
    unsigned int topright_samples_available;
211
    unsigned int left_samples_available;
212 6ba71fc4 Loïc Le Loarer
    uint8_t (*top_borders[2])[16+2*8];
213
    uint8_t left_border[2*(17+2*9)];
214 0da71265 Michael Niedermayer
215
    /**
216
     * non zero coeff count cache.
217
     * is 64 if not available.
218
     */
219 68b51e58 Steve L'Homme
    DECLARE_ALIGNED_8(uint8_t, non_zero_count_cache[6*8]);
220 53c05b1e Michael Niedermayer
    uint8_t (*non_zero_count)[16];
221 0da71265 Michael Niedermayer
222
    /**
223
     * Motion vector cache.
224
     */
225 68b51e58 Steve L'Homme
    DECLARE_ALIGNED_8(int16_t, mv_cache[2][5*8][2]);
226
    DECLARE_ALIGNED_8(int8_t, ref_cache[2][5*8]);
227 0da71265 Michael Niedermayer
#define LIST_NOT_USED -1 //FIXME rename?
228
#define PART_NOT_AVAILABLE -2
229 115329f1 Diego Biurrun
230 0da71265 Michael Niedermayer
    /**
231
     * is 1 if the specific list MV&references are set to 0,0,-2.
232
     */
233
    int mv_cache_clean[2];
234
235 6867a90b Loic Le Loarer
    /**
236 43efd19a Loren Merritt
     * number of neighbors (top and/or left) that used 8x8 dct
237
     */
238
    int neighbor_transform_size;
239
240
    /**
241 6867a90b Loic Le Loarer
     * block_offset[ 0..23] for frame macroblocks
242
     * block_offset[24..47] for field macroblocks
243
     */
244
    int block_offset[2*(16+8)];
245 115329f1 Diego Biurrun
246 a55f20bd Loren Merritt
    uint32_t *mb2b_xy; //FIXME are these 4 a good idea?
247
    uint32_t *mb2b8_xy;
248 650cec0c Loren Merritt
    int b_stride; //FIXME use s->b4_stride
249 0da71265 Michael Niedermayer
    int b8_stride;
250
251 5d18eaad Loren Merritt
    int mb_linesize;   ///< may be equal to s->linesize or s->linesize*2, for mbaff
252
    int mb_uvlinesize;
253
254
    int emu_edge_width;
255
    int emu_edge_height;
256
257 8b82a956 Michael Niedermayer
    int halfpel_flag;
258
    int thirdpel_flag;
259
260 da3b9756 Mike Melanson
    int unknown_svq3_flag;
261
    int next_slice_index;
262
263 0da71265 Michael Niedermayer
    SPS sps_buffer[MAX_SPS_COUNT];
264
    SPS sps; ///< current sps
265 115329f1 Diego Biurrun
266 0da71265 Michael Niedermayer
    PPS pps_buffer[MAX_PPS_COUNT];
267
    /**
268
     * current pps
269
     */
270 3b66c4c5 Kevin Baragona
    PPS pps; //FIXME move to Picture perhaps? (->no) do we need that?
271 0da71265 Michael Niedermayer
272 239ea04c Loren Merritt
    uint32_t dequant4_buffer[6][52][16];
273
    uint32_t dequant8_buffer[2][52][64];
274
    uint32_t (*dequant4_coeff[6])[16];
275
    uint32_t (*dequant8_coeff[2])[64];
276
    int dequant_coeff_pps;     ///< reinit tables when pps changes
277 43efd19a Loren Merritt
278 0da71265 Michael Niedermayer
    int slice_num;
279
    uint8_t *slice_table_base;
280 5d18eaad Loren Merritt
    uint8_t *slice_table;      ///< slice_table_base + 2*mb_stride + 1
281 0da71265 Michael Niedermayer
    int slice_type;
282
    int slice_type_fixed;
283 115329f1 Diego Biurrun
284 0da71265 Michael Niedermayer
    //interlacing specific flags
285 6ba71fc4 Loïc Le Loarer
    int mb_aff_frame;
286 0da71265 Michael Niedermayer
    int mb_field_decoding_flag;
287 5d18eaad Loren Merritt
    int mb_mbaff;              ///< mb_aff_frame && mb_field_decoding_flag
288 115329f1 Diego Biurrun
289 0da71265 Michael Niedermayer
    int sub_mb_type[4];
290 115329f1 Diego Biurrun
291 0da71265 Michael Niedermayer
    //POC stuff
292
    int poc_lsb;
293
    int poc_msb;
294
    int delta_poc_bottom;
295
    int delta_poc[2];
296
    int frame_num;
297
    int prev_poc_msb;             ///< poc_msb of the last reference pic for POC type 0
298
    int prev_poc_lsb;             ///< poc_lsb of the last reference pic for POC type 0
299
    int frame_num_offset;         ///< for POC type 2
300
    int prev_frame_num_offset;    ///< for POC type 2
301
    int prev_frame_num;           ///< frame_num of the last pic for POC type 1/2
302
303
    /**
304
     * frame_num for frames or 2*frame_num for field pics.
305
     */
306
    int curr_pic_num;
307 115329f1 Diego Biurrun
308 0da71265 Michael Niedermayer
    /**
309
     * max_frame_num or 2*max_frame_num for field pics.
310
     */
311
    int max_pic_num;
312
313
    //Weighted pred stuff
314 9f2d1b4f Loren Merritt
    int use_weight;
315
    int use_weight_chroma;
316 0da71265 Michael Niedermayer
    int luma_log2_weight_denom;
317
    int chroma_log2_weight_denom;
318 5d18eaad Loren Merritt
    int luma_weight[2][48];
319
    int luma_offset[2][48];
320
    int chroma_weight[2][48][2];
321
    int chroma_offset[2][48][2];
322
    int implicit_weight[48][48];
323 115329f1 Diego Biurrun
324 0da71265 Michael Niedermayer
    //deblock
325 115329f1 Diego Biurrun
    int deblocking_filter;         ///< disable_deblocking_filter_idc with 1<->0
326 980a82b7 Michael Niedermayer
    int slice_alpha_c0_offset;
327
    int slice_beta_offset;
328 115329f1 Diego Biurrun
329 0da71265 Michael Niedermayer
    int redundant_pic_count;
330 115329f1 Diego Biurrun
331 0da71265 Michael Niedermayer
    int direct_spatial_mv_pred;
332 5ad984c9 Loren Merritt
    int dist_scale_factor[16];
333 5d18eaad Loren Merritt
    int dist_scale_factor_field[32];
334 2f944356 Loren Merritt
    int map_col_to_list0[2][16];
335 5d18eaad Loren Merritt
    int map_col_to_list0_field[2][32];
336 0da71265 Michael Niedermayer
337
    /**
338
     * num_ref_idx_l0/1_active_minus1 + 1
339
     */
340 5d18eaad Loren Merritt
    int ref_count[2];            ///< counts frames or fields, depending on current mb mode
341 17107065 Loren Merritt
    Picture *short_ref[32];
342
    Picture *long_ref[32];
343 0da71265 Michael Niedermayer
    Picture default_ref_list[2][32];
344 5d18eaad Loren Merritt
    Picture ref_list[2][48];     ///< 0..15: frame refs, 16..47: mbaff field refs
345 a4dae92b Loren Merritt
    Picture *delayed_pic[16]; //FIXME size?
346 4e4d983e Loren Merritt
    Picture *delayed_output_pic;
347 115329f1 Diego Biurrun
348 0da71265 Michael Niedermayer
    /**
349
     * memory management control operations buffer.
350
     */
351
    MMCO mmco[MAX_MMCO_COUNT];
352
    int mmco_index;
353 115329f1 Diego Biurrun
354 0da71265 Michael Niedermayer
    int long_ref_count;  ///< number of actual long term references
355
    int short_ref_count; ///< number of actual short term references
356 115329f1 Diego Biurrun
357 0da71265 Michael Niedermayer
    //data partitioning
358
    GetBitContext intra_gb;
359
    GetBitContext inter_gb;
360
    GetBitContext *intra_gb_ptr;
361
    GetBitContext *inter_gb_ptr;
362 115329f1 Diego Biurrun
363 68b51e58 Steve L'Homme
    DECLARE_ALIGNED_8(DCTELEM, mb[16*24]);
364 e5017ab8 Laurent Aimar
365
    /**
366
     * Cabac
367
     */
368
    CABACContext cabac;
369 43efd19a Loren Merritt
    uint8_t      cabac_state[460];
370 e5017ab8 Laurent Aimar
    int          cabac_init_idc;
371
372
    /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
373
    uint16_t     *cbp_table;
374 3981c385 Michael Niedermayer
    int top_cbp;
375
    int left_cbp;
376 693399ba Laurent Aimar
    /* chroma_pred_mode for i4x4 or i16x16, else 0 */
377 e5017ab8 Laurent Aimar
    uint8_t     *chroma_pred_mode_table;
378
    int         last_qscale_diff;
379 9e528114 Laurent Aimar
    int16_t     (*mvd_table[2])[2];
380 68b51e58 Steve L'Homme
    DECLARE_ALIGNED_8(int16_t, mvd_cache[2][5*8][2]);
381 5ad984c9 Loren Merritt
    uint8_t     *direct_table;
382
    uint8_t     direct_cache[5*8];
383 e5017ab8 Laurent Aimar
384 e4b36d44 Michael Niedermayer
    uint8_t zigzag_scan[16];
385 548a1c8a Loren Merritt
    uint8_t zigzag_scan8x8[64];
386
    uint8_t zigzag_scan8x8_cavlc[64];
387 5d18eaad Loren Merritt
    uint8_t field_scan[16];
388
    uint8_t field_scan8x8[64];
389
    uint8_t field_scan8x8_cavlc[64];
390 36940eca Loren Merritt
    const uint8_t *zigzag_scan_q0;
391 548a1c8a Loren Merritt
    const uint8_t *zigzag_scan8x8_q0;
392
    const uint8_t *zigzag_scan8x8_cavlc_q0;
393 5d18eaad Loren Merritt
    const uint8_t *field_scan_q0;
394
    const uint8_t *field_scan8x8_q0;
395
    const uint8_t *field_scan8x8_cavlc_q0;
396 115329f1 Diego Biurrun
397 cdd10689 Michael Niedermayer
    int x264_build;
398 0da71265 Michael Niedermayer
}H264Context;
399
400
static VLC coeff_token_vlc[4];
401
static VLC chroma_dc_coeff_token_vlc;
402
403
static VLC total_zeros_vlc[15];
404
static VLC chroma_dc_total_zeros_vlc[3];
405
406
static VLC run_vlc[6];
407
static VLC run7_vlc;
408
409 8b82a956 Michael Niedermayer
static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
410
static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
411 6ba71fc4 Loïc Le Loarer
static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
412 8b82a956 Michael Niedermayer
413 70abb407 Loren Merritt
static always_inline uint32_t pack16to32(int a, int b){
414 377ec888 Michael Niedermayer
#ifdef WORDS_BIGENDIAN
415
   return (b&0xFFFF) + (a<<16);
416
#else
417
   return (a&0xFFFF) + (b<<16);
418
#endif
419
}
420
421 0da71265 Michael Niedermayer
/**
422
 * fill a rectangle.
423 5175b937 Loic Le Loarer
 * @param h height of the rectangle, should be a constant
424
 * @param w width of the rectangle, should be a constant
425 0da71265 Michael Niedermayer
 * @param size the size of val (1 or 4), should be a constant
426
 */
427 70abb407 Loren Merritt
static always_inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){
428 af6e2fed Michael Niedermayer
    uint8_t *p= (uint8_t*)vp;
429 0da71265 Michael Niedermayer
    assert(size==1 || size==4);
430 67a82086 Loren Merritt
    assert(w<=4);
431 115329f1 Diego Biurrun
432 0da71265 Michael Niedermayer
    w      *= size;
433
    stride *= size;
434 115329f1 Diego Biurrun
435 4733abcb Måns Rullgård
    assert((((long)vp)&(FFMIN(w, STRIDE_ALIGN)-1)) == 0);
436 d9c780a8 Michael Niedermayer
    assert((stride&(w-1))==0);
437 67a82086 Loren Merritt
    if(w==2){
438
        const uint16_t v= size==4 ? val : val*0x0101;
439
        *(uint16_t*)(p + 0*stride)= v;
440
        if(h==1) return;
441
        *(uint16_t*)(p + 1*stride)= v;
442
        if(h==2) return;
443 0da71265 Michael Niedermayer
        *(uint16_t*)(p + 2*stride)=
444 67a82086 Loren Merritt
        *(uint16_t*)(p + 3*stride)= v;
445
    }else if(w==4){
446
        const uint32_t v= size==4 ? val : val*0x01010101;
447
        *(uint32_t*)(p + 0*stride)= v;
448
        if(h==1) return;
449
        *(uint32_t*)(p + 1*stride)= v;
450
        if(h==2) return;
451 0da71265 Michael Niedermayer
        *(uint32_t*)(p + 2*stride)=
452 67a82086 Loren Merritt
        *(uint32_t*)(p + 3*stride)= v;
453
    }else if(w==8){
454
    //gcc can't optimize 64bit math on x86_32
455
#if defined(ARCH_X86_64) || (defined(MP_WORDSIZE) && MP_WORDSIZE >= 64)
456
        const uint64_t v= val*0x0100000001ULL;
457
        *(uint64_t*)(p + 0*stride)= v;
458
        if(h==1) return;
459
        *(uint64_t*)(p + 1*stride)= v;
460
        if(h==2) return;
461 0da71265 Michael Niedermayer
        *(uint64_t*)(p + 2*stride)=
462 67a82086 Loren Merritt
        *(uint64_t*)(p + 3*stride)= v;
463
    }else if(w==16){
464
        const uint64_t v= val*0x0100000001ULL;
465 0da71265 Michael Niedermayer
        *(uint64_t*)(p + 0+0*stride)=
466
        *(uint64_t*)(p + 8+0*stride)=
467
        *(uint64_t*)(p + 0+1*stride)=
468 67a82086 Loren Merritt
        *(uint64_t*)(p + 8+1*stride)= v;
469
        if(h==2) return;
470 0da71265 Michael Niedermayer
        *(uint64_t*)(p + 0+2*stride)=
471
        *(uint64_t*)(p + 8+2*stride)=
472
        *(uint64_t*)(p + 0+3*stride)=
473 67a82086 Loren Merritt
        *(uint64_t*)(p + 8+3*stride)= v;
474
#else
475
        *(uint32_t*)(p + 0+0*stride)=
476
        *(uint32_t*)(p + 4+0*stride)= val;
477
        if(h==1) return;
478
        *(uint32_t*)(p + 0+1*stride)=
479
        *(uint32_t*)(p + 4+1*stride)= val;
480
        if(h==2) return;
481
        *(uint32_t*)(p + 0+2*stride)=
482
        *(uint32_t*)(p + 4+2*stride)=
483
        *(uint32_t*)(p + 0+3*stride)=
484
        *(uint32_t*)(p + 4+3*stride)= val;
485
    }else if(w==16){
486
        *(uint32_t*)(p + 0+0*stride)=
487
        *(uint32_t*)(p + 4+0*stride)=
488
        *(uint32_t*)(p + 8+0*stride)=
489
        *(uint32_t*)(p +12+0*stride)=
490
        *(uint32_t*)(p + 0+1*stride)=
491
        *(uint32_t*)(p + 4+1*stride)=
492
        *(uint32_t*)(p + 8+1*stride)=
493
        *(uint32_t*)(p +12+1*stride)= val;
494
        if(h==2) return;
495
        *(uint32_t*)(p + 0+2*stride)=
496
        *(uint32_t*)(p + 4+2*stride)=
497
        *(uint32_t*)(p + 8+2*stride)=
498
        *(uint32_t*)(p +12+2*stride)=
499
        *(uint32_t*)(p + 0+3*stride)=
500
        *(uint32_t*)(p + 4+3*stride)=
501
        *(uint32_t*)(p + 8+3*stride)=
502
        *(uint32_t*)(p +12+3*stride)= val;
503
#endif
504 0da71265 Michael Niedermayer
    }else
505
        assert(0);
506 67a82086 Loren Merritt
    assert(h==4);
507 0da71265 Michael Niedermayer
}
508
509 70abb407 Loren Merritt
static void fill_caches(H264Context *h, int mb_type, int for_deblock){
510 0da71265 Michael Niedermayer
    MpegEncContext * const s = &h->s;
511 7bc9090a Michael Niedermayer
    const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
512 0da71265 Michael Niedermayer
    int topleft_xy, top_xy, topright_xy, left_xy[2];
513
    int topleft_type, top_type, topright_type, left_type[2];
514 6867a90b Loic Le Loarer
    int left_block[8];
515 0da71265 Michael Niedermayer
    int i;
516
517 717b1733 Loren Merritt
    //FIXME deblocking could skip the intra and nnz parts.
518
    if(for_deblock && (h->slice_num == 1 || h->slice_table[mb_xy] == h->slice_table[mb_xy-s->mb_stride]) && !FRAME_MBAFF)
519 e2e5894a Loren Merritt
        return;
520
521 115329f1 Diego Biurrun
    //wow what a mess, why didn't they simplify the interlacing&intra stuff, i can't imagine that these complex rules are worth it
522
523 6867a90b Loic Le Loarer
    top_xy     = mb_xy  - s->mb_stride;
524
    topleft_xy = top_xy - 1;
525
    topright_xy= top_xy + 1;
526
    left_xy[1] = left_xy[0] = mb_xy-1;
527
    left_block[0]= 0;
528
    left_block[1]= 1;
529
    left_block[2]= 2;
530
    left_block[3]= 3;
531
    left_block[4]= 7;
532
    left_block[5]= 10;
533
    left_block[6]= 8;
534
    left_block[7]= 11;
535 5d18eaad Loren Merritt
    if(FRAME_MBAFF){
536 6867a90b Loic Le Loarer
        const int pair_xy          = s->mb_x     + (s->mb_y & ~1)*s->mb_stride;
537
        const int top_pair_xy      = pair_xy     - s->mb_stride;
538
        const int topleft_pair_xy  = top_pair_xy - 1;
539
        const int topright_pair_xy = top_pair_xy + 1;
540
        const int topleft_mb_frame_flag  = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
541
        const int top_mb_frame_flag      = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
542
        const int topright_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
543
        const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
544
        const int curr_mb_frame_flag = !IS_INTERLACED(mb_type);
545
        const int bottom = (s->mb_y & 1);
546
        tprintf("fill_caches: curr_mb_frame_flag:%d, left_mb_frame_flag:%d, topleft_mb_frame_flag:%d, top_mb_frame_flag:%d, topright_mb_frame_flag:%d\n", curr_mb_frame_flag, left_mb_frame_flag, topleft_mb_frame_flag, top_mb_frame_flag, topright_mb_frame_flag);
547
        if (bottom
548
                ? !curr_mb_frame_flag // bottom macroblock
549
                : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
550
                ) {
551
            top_xy -= s->mb_stride;
552
        }
553
        if (bottom
554
                ? !curr_mb_frame_flag // bottom macroblock
555
                : (!curr_mb_frame_flag && !topleft_mb_frame_flag) // top macroblock
556
                ) {
557
            topleft_xy -= s->mb_stride;
558
        }
559
        if (bottom
560
                ? !curr_mb_frame_flag // bottom macroblock
561
                : (!curr_mb_frame_flag && !topright_mb_frame_flag) // top macroblock
562
                ) {
563
            topright_xy -= s->mb_stride;
564
        }
565
        if (left_mb_frame_flag != curr_mb_frame_flag) {
566
            left_xy[1] = left_xy[0] = pair_xy - 1;
567
            if (curr_mb_frame_flag) {
568
                if (bottom) {
569
                    left_block[0]= 2;
570
                    left_block[1]= 2;
571
                    left_block[2]= 3;
572
                    left_block[3]= 3;
573
                    left_block[4]= 8;
574
                    left_block[5]= 11;
575
                    left_block[6]= 8;
576
                    left_block[7]= 11;
577
                } else {
578
                    left_block[0]= 0;
579
                    left_block[1]= 0;
580
                    left_block[2]= 1;
581
                    left_block[3]= 1;
582
                    left_block[4]= 7;
583
                    left_block[5]= 10;
584
                    left_block[6]= 7;
585
                    left_block[7]= 10;
586
                }
587
            } else {
588
                left_xy[1] += s->mb_stride;
589
                //left_block[0]= 0;
590
                left_block[1]= 2;
591
                left_block[2]= 0;
592
                left_block[3]= 2;
593
                //left_block[4]= 7;
594
                left_block[5]= 10;
595
                left_block[6]= 7;
596
                left_block[7]= 10;
597
            }
598
        }
599 0da71265 Michael Niedermayer
    }
600
601 826de46e Loïc Le Loarer
    h->top_mb_xy = top_xy;
602
    h->left_mb_xy[0] = left_xy[0];
603
    h->left_mb_xy[1] = left_xy[1];
604 6ba71fc4 Loïc Le Loarer
    if(for_deblock){
605 717b1733 Loren Merritt
        topleft_type = 0;
606
        topright_type = 0;
607 46f2f05f Michael Niedermayer
        top_type     = h->slice_table[top_xy     ] < 255 ? s->current_picture.mb_type[top_xy]     : 0;
608
        left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
609
        left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
610 5d18eaad Loren Merritt
611
        if(FRAME_MBAFF && !IS_INTRA(mb_type)){
612
            int list;
613
            int v = *(uint16_t*)&h->non_zero_count[mb_xy][14];
614
            for(i=0; i<16; i++)
615
                h->non_zero_count_cache[scan8[i]] = (v>>i)&1;
616
            for(list=0; list<1+(h->slice_type==B_TYPE); list++){
617
                if(USES_LIST(mb_type,list)){
618
                    uint32_t *src = (uint32_t*)s->current_picture.motion_val[list][h->mb2b_xy[mb_xy]];
619
                    uint32_t *dst = (uint32_t*)h->mv_cache[list][scan8[0]];
620
                    uint8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
621
                    for(i=0; i<4; i++, dst+=8, src+=h->b_stride){
622
                        dst[0] = src[0];
623
                        dst[1] = src[1];
624
                        dst[2] = src[2];
625
                        dst[3] = src[3];
626
                    }
627
                    *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =
628
                    *(uint32_t*)&h->ref_cache[list][scan8[ 2]] = pack16to32(ref[0],ref[1])*0x0101;
629
                    ref += h->b8_stride;
630
                    *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =
631
                    *(uint32_t*)&h->ref_cache[list][scan8[10]] = pack16to32(ref[0],ref[1])*0x0101;
632
                }else{
633
                    fill_rectangle(&h-> mv_cache[list][scan8[ 0]], 4, 4, 8, 0, 4);
634
                    fill_rectangle(&h->ref_cache[list][scan8[ 0]], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
635
                }
636
            }
637
        }
638 46f2f05f Michael Niedermayer
    }else{
639
        topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
640
        top_type     = h->slice_table[top_xy     ] == h->slice_num ? s->current_picture.mb_type[top_xy]     : 0;
641
        topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
642
        left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
643
        left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
644
    }
645 0da71265 Michael Niedermayer
646
    if(IS_INTRA(mb_type)){
647 115329f1 Diego Biurrun
        h->topleft_samples_available=
648
        h->top_samples_available=
649 0da71265 Michael Niedermayer
        h->left_samples_available= 0xFFFF;
650
        h->topright_samples_available= 0xEEEA;
651
652
        if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
653
            h->topleft_samples_available= 0xB3FF;
654
            h->top_samples_available= 0x33FF;
655
            h->topright_samples_available= 0x26EA;
656
        }
657
        for(i=0; i<2; i++){
658
            if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
659
                h->topleft_samples_available&= 0xDF5F;
660
                h->left_samples_available&= 0x5F5F;
661
            }
662
        }
663 115329f1 Diego Biurrun
664 0da71265 Michael Niedermayer
        if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
665
            h->topleft_samples_available&= 0x7FFF;
666 115329f1 Diego Biurrun
667 0da71265 Michael Niedermayer
        if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
668
            h->topright_samples_available&= 0xFBFF;
669 115329f1 Diego Biurrun
670 0da71265 Michael Niedermayer
        if(IS_INTRA4x4(mb_type)){
671
            if(IS_INTRA4x4(top_type)){
672
                h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
673
                h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
674
                h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
675
                h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
676
            }else{
677
                int pred;
678 6fbcaaa0 Loic Le Loarer
                if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred))
679 0da71265 Michael Niedermayer
                    pred= -1;
680 6fbcaaa0 Loic Le Loarer
                else{
681
                    pred= 2;
682 0da71265 Michael Niedermayer
                }
683
                h->intra4x4_pred_mode_cache[4+8*0]=
684
                h->intra4x4_pred_mode_cache[5+8*0]=
685
                h->intra4x4_pred_mode_cache[6+8*0]=
686
                h->intra4x4_pred_mode_cache[7+8*0]= pred;
687
            }
688
            for(i=0; i<2; i++){
689
                if(IS_INTRA4x4(left_type[i])){
690
                    h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
691
                    h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
692
                }else{
693
                    int pred;
694 6fbcaaa0 Loic Le Loarer
                    if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred))
695 0da71265 Michael Niedermayer
                        pred= -1;
696 6fbcaaa0 Loic Le Loarer
                    else{
697
                        pred= 2;
698 0da71265 Michael Niedermayer
                    }
699
                    h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
700
                    h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
701
                }
702
            }
703
        }
704
    }
705 115329f1 Diego Biurrun
706
707 0da71265 Michael Niedermayer
/*
708 115329f1 Diego Biurrun
0 . T T. T T T T
709
1 L . .L . . . .
710
2 L . .L . . . .
711
3 . T TL . . . .
712
4 L . .L . . . .
713
5 L . .. . . . .
714 0da71265 Michael Niedermayer
*/
715
//FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec)
716
    if(top_type){
717 6867a90b Loic Le Loarer
        h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
718
        h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
719
        h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
720 53c05b1e Michael Niedermayer
        h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
721 115329f1 Diego Biurrun
722 6867a90b Loic Le Loarer
        h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
723 53c05b1e Michael Niedermayer
        h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
724 115329f1 Diego Biurrun
725 6867a90b Loic Le Loarer
        h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
726 53c05b1e Michael Niedermayer
        h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
727 115329f1 Diego Biurrun
728 0da71265 Michael Niedermayer
    }else{
729 115329f1 Diego Biurrun
        h->non_zero_count_cache[4+8*0]=
730 0da71265 Michael Niedermayer
        h->non_zero_count_cache[5+8*0]=
731
        h->non_zero_count_cache[6+8*0]=
732
        h->non_zero_count_cache[7+8*0]=
733 115329f1 Diego Biurrun
734 0da71265 Michael Niedermayer
        h->non_zero_count_cache[1+8*0]=
735
        h->non_zero_count_cache[2+8*0]=
736 115329f1 Diego Biurrun
737 0da71265 Michael Niedermayer
        h->non_zero_count_cache[1+8*3]=
738 3981c385 Michael Niedermayer
        h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
739 115329f1 Diego Biurrun
740 0da71265 Michael Niedermayer
    }
741 826de46e Loïc Le Loarer
742 6867a90b Loic Le Loarer
    for (i=0; i<2; i++) {
743
        if(left_type[i]){
744
            h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
745
            h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
746
            h->non_zero_count_cache[0+8*1 +   8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
747
            h->non_zero_count_cache[0+8*4 +   8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
748
        }else{
749 115329f1 Diego Biurrun
            h->non_zero_count_cache[3+8*1 + 2*8*i]=
750
            h->non_zero_count_cache[3+8*2 + 2*8*i]=
751
            h->non_zero_count_cache[0+8*1 +   8*i]=
752 6867a90b Loic Le Loarer
            h->non_zero_count_cache[0+8*4 +   8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
753 826de46e Loïc Le Loarer
        }
754
    }
755
756
    if( h->pps.cabac ) {
757
        // top_cbp
758
        if(top_type) {
759
            h->top_cbp = h->cbp_table[top_xy];
760
        } else if(IS_INTRA(mb_type)) {
761
            h->top_cbp = 0x1C0;
762
        } else {
763
            h->top_cbp = 0;
764
        }
765
        // left_cbp
766
        if (left_type[0]) {
767
            h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
768
        } else if(IS_INTRA(mb_type)) {
769
            h->left_cbp = 0x1C0;
770
        } else {
771
            h->left_cbp = 0;
772
        }
773
        if (left_type[0]) {
774
            h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
775
        }
776
        if (left_type[1]) {
777
            h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
778 6867a90b Loic Le Loarer
        }
779 0da71265 Michael Niedermayer
    }
780 6867a90b Loic Le Loarer
781 0da71265 Michael Niedermayer
#if 1
782 e2e5894a Loren Merritt
    if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
783 0da71265 Michael Niedermayer
        int list;
784 e2e5894a Loren Merritt
        for(list=0; list<1+(h->slice_type==B_TYPE); list++){
785
            if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
786 0da71265 Michael Niedermayer
                /*if(!h->mv_cache_clean[list]){
787
                    memset(h->mv_cache [list],  0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
788
                    memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
789
                    h->mv_cache_clean[list]= 1;
790
                }*/
791 5ad984c9 Loren Merritt
                continue;
792 0da71265 Michael Niedermayer
            }
793
            h->mv_cache_clean[list]= 0;
794 115329f1 Diego Biurrun
795 53b19144 Loren Merritt
            if(USES_LIST(top_type, list)){
796 0da71265 Michael Niedermayer
                const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
797
                const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
798
                *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
799
                *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
800
                *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
801
                *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
802
                h->ref_cache[list][scan8[0] + 0 - 1*8]=
803
                h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
804
                h->ref_cache[list][scan8[0] + 2 - 1*8]=
805
                h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
806
            }else{
807 115329f1 Diego Biurrun
                *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
808
                *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
809
                *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
810 0da71265 Michael Niedermayer
                *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
811
                *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
812
            }
813
814
            //FIXME unify cleanup or sth
815 53b19144 Loren Merritt
            if(USES_LIST(left_type[0], list)){
816 0da71265 Michael Niedermayer
                const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
817
                const int b8_xy= h->mb2b8_xy[left_xy[0]] + 1;
818
                *(uint32_t*)h->mv_cache[list][scan8[0] - 1 + 0*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[0]];
819
                *(uint32_t*)h->mv_cache[list][scan8[0] - 1 + 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[1]];
820 5d18eaad Loren Merritt
                h->ref_cache[list][scan8[0] - 1 + 0*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0]>>1)];
821
                h->ref_cache[list][scan8[0] - 1 + 1*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1]>>1)];
822 0da71265 Michael Niedermayer
            }else{
823
                *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 0*8]=
824
                *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 1*8]= 0;
825
                h->ref_cache[list][scan8[0] - 1 + 0*8]=
826
                h->ref_cache[list][scan8[0] - 1 + 1*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
827
            }
828 115329f1 Diego Biurrun
829 53b19144 Loren Merritt
            if(USES_LIST(left_type[1], list)){
830 0da71265 Michael Niedermayer
                const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
831
                const int b8_xy= h->mb2b8_xy[left_xy[1]] + 1;
832
                *(uint32_t*)h->mv_cache[list][scan8[0] - 1 + 2*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[2]];
833
                *(uint32_t*)h->mv_cache[list][scan8[0] - 1 + 3*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[3]];
834 5d18eaad Loren Merritt
                h->ref_cache[list][scan8[0] - 1 + 2*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[2]>>1)];
835
                h->ref_cache[list][scan8[0] - 1 + 3*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[3]>>1)];
836 0da71265 Michael Niedermayer
            }else{
837
                *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 2*8]=
838
                *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 3*8]= 0;
839
                h->ref_cache[list][scan8[0] - 1 + 2*8]=
840
                h->ref_cache[list][scan8[0] - 1 + 3*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
841 d9c780a8 Michael Niedermayer
                assert((!left_type[0]) == (!left_type[1]));
842 0da71265 Michael Niedermayer
            }
843
844 ae08a563 Loren Merritt
            if((for_deblock || (IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred)) && !FRAME_MBAFF)
845 46f2f05f Michael Niedermayer
                continue;
846
847 53b19144 Loren Merritt
            if(USES_LIST(topleft_type, list)){
848 e2e5894a Loren Merritt
                const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
849
                const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + h->b8_stride;
850
                *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
851
                h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
852
            }else{
853
                *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
854
                h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
855
            }
856 115329f1 Diego Biurrun
857 53b19144 Loren Merritt
            if(USES_LIST(topright_type, list)){
858 e2e5894a Loren Merritt
                const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
859
                const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
860
                *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
861
                h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
862
            }else{
863
                *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
864
                h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
865
            }
866
867 ae08a563 Loren Merritt
            if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)
868 717b1733 Loren Merritt
                continue;
869 115329f1 Diego Biurrun
870
            h->ref_cache[list][scan8[5 ]+1] =
871
            h->ref_cache[list][scan8[7 ]+1] =
872 3b66c4c5 Kevin Baragona
            h->ref_cache[list][scan8[13]+1] =  //FIXME remove past 3 (init somewhere else)
873 115329f1 Diego Biurrun
            h->ref_cache[list][scan8[4 ]] =
874 0da71265 Michael Niedermayer
            h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
875
            *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
876
            *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
877 3b66c4c5 Kevin Baragona
            *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
878 0da71265 Michael Niedermayer
            *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
879
            *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
880 9e528114 Laurent Aimar
881
            if( h->pps.cabac ) {
882
                /* XXX beurk, Load mvd */
883 53b19144 Loren Merritt
                if(USES_LIST(top_type, list)){
884 9e528114 Laurent Aimar
                    const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
885
                    *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
886
                    *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
887
                    *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
888
                    *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
889
                }else{
890 115329f1 Diego Biurrun
                    *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
891
                    *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
892
                    *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
893 9e528114 Laurent Aimar
                    *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
894
                }
895 53b19144 Loren Merritt
                if(USES_LIST(left_type[0], list)){
896 9e528114 Laurent Aimar
                    const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
897
                    *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 0*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[0]];
898
                    *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[1]];
899
                }else{
900
                    *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
901
                    *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
902
                }
903 53b19144 Loren Merritt
                if(USES_LIST(left_type[1], list)){
904 9e528114 Laurent Aimar
                    const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
905
                    *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 2*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[2]];
906
                    *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 3*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[3]];
907
                }else{
908
                    *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
909
                    *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
910
                }
911
                *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
912
                *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
913 3b66c4c5 Kevin Baragona
                *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
914 9e528114 Laurent Aimar
                *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
915
                *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
916 5ad984c9 Loren Merritt
917
                if(h->slice_type == B_TYPE){
918
                    fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
919
920
                    if(IS_DIRECT(top_type)){
921
                        *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
922
                    }else if(IS_8X8(top_type)){
923
                        int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
924
                        h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
925
                        h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
926
                    }else{
927
                        *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
928
                    }
929 115329f1 Diego Biurrun
930 5d18eaad Loren Merritt
                    if(IS_DIRECT(left_type[0]))
931
                        h->direct_cache[scan8[0] - 1 + 0*8]= 1;
932
                    else if(IS_8X8(left_type[0]))
933
                        h->direct_cache[scan8[0] - 1 + 0*8]= h->direct_table[h->mb2b8_xy[left_xy[0]] + 1 + h->b8_stride*(left_block[0]>>1)];
934
                    else
935
                        h->direct_cache[scan8[0] - 1 + 0*8]= 0;
936
937
                    if(IS_DIRECT(left_type[1]))
938 5ad984c9 Loren Merritt
                        h->direct_cache[scan8[0] - 1 + 2*8]= 1;
939 5d18eaad Loren Merritt
                    else if(IS_8X8(left_type[1]))
940
                        h->direct_cache[scan8[0] - 1 + 2*8]= h->direct_table[h->mb2b8_xy[left_xy[1]] + 1 + h->b8_stride*(left_block[2]>>1)];
941
                    else
942 5ad984c9 Loren Merritt
                        h->direct_cache[scan8[0] - 1 + 2*8]= 0;
943 5d18eaad Loren Merritt
                }
944
            }
945
946
            if(FRAME_MBAFF){
947
#define MAP_MVS\
948
                    MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
949
                    MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
950
                    MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
951
                    MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
952
                    MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
953
                    MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
954
                    MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\
955
                    MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\
956
                    MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\
957
                    MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
958
                if(MB_FIELD){
959
#define MAP_F2F(idx, mb_type)\
960
                    if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
961
                        h->ref_cache[list][idx] <<= 1;\
962
                        h->mv_cache[list][idx][1] /= 2;\
963
                        h->mvd_cache[list][idx][1] /= 2;\
964
                    }
965
                    MAP_MVS
966
#undef MAP_F2F
967
                }else{
968
#define MAP_F2F(idx, mb_type)\
969
                    if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
970
                        h->ref_cache[list][idx] >>= 1;\
971
                        h->mv_cache[list][idx][1] <<= 1;\
972
                        h->mvd_cache[list][idx][1] <<= 1;\
973 5ad984c9 Loren Merritt
                    }
974 5d18eaad Loren Merritt
                    MAP_MVS
975
#undef MAP_F2F
976 5ad984c9 Loren Merritt
                }
977 9e528114 Laurent Aimar
            }
978 0da71265 Michael Niedermayer
        }
979
    }
980
#endif
981 43efd19a Loren Merritt
982
    h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
983 0da71265 Michael Niedermayer
}
984
985
static inline void write_back_intra_pred_mode(H264Context *h){
986
    MpegEncContext * const s = &h->s;
987 7bc9090a Michael Niedermayer
    const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
988 0da71265 Michael Niedermayer
989
    h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
990
    h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
991
    h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
992
    h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
993
    h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
994
    h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
995
    h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
996
}
997
998
/**
999
 * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
1000
 */
1001
static inline int check_intra4x4_pred_mode(H264Context *h){
1002
    MpegEncContext * const s = &h->s;
1003
    static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
1004
    static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
1005
    int i;
1006 115329f1 Diego Biurrun
1007 0da71265 Michael Niedermayer
    if(!(h->top_samples_available&0x8000)){
1008
        for(i=0; i<4; i++){
1009
            int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
1010
            if(status<0){
1011 9b879566 Michel Bardiaux
                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);
1012 0da71265 Michael Niedermayer
                return -1;
1013
            } else if(status){
1014
                h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
1015
            }
1016
        }
1017
    }
1018 115329f1 Diego Biurrun
1019 0da71265 Michael Niedermayer
    if(!(h->left_samples_available&0x8000)){
1020
        for(i=0; i<4; i++){
1021
            int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
1022
            if(status<0){
1023 9b879566 Michel Bardiaux
                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);
1024 0da71265 Michael Niedermayer
                return -1;
1025
            } else if(status){
1026
                h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
1027
            }
1028
        }
1029
    }
1030
1031
    return 0;
1032
} //FIXME cleanup like next
1033
1034
/**
1035
 * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
1036
 */
1037
static inline int check_intra_pred_mode(H264Context *h, int mode){
1038
    MpegEncContext * const s = &h->s;
1039
    static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
1040
    static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
1041 115329f1 Diego Biurrun
1042 5175b937 Loic Le Loarer
    if(mode < 0 || mode > 6) {
1043
        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);
1044 7440fe83 Michael Niedermayer
        return -1;
1045 5175b937 Loic Le Loarer
    }
1046 115329f1 Diego Biurrun
1047 0da71265 Michael Niedermayer
    if(!(h->top_samples_available&0x8000)){
1048
        mode= top[ mode ];
1049
        if(mode<0){
1050 9b879566 Michel Bardiaux
            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);
1051 0da71265 Michael Niedermayer
            return -1;
1052
        }
1053
    }
1054 115329f1 Diego Biurrun
1055 0da71265 Michael Niedermayer
    if(!(h->left_samples_available&0x8000)){
1056
        mode= left[ mode ];
1057
        if(mode<0){
1058 9b879566 Michel Bardiaux
            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);
1059 0da71265 Michael Niedermayer
            return -1;
1060 115329f1 Diego Biurrun
        }
1061 0da71265 Michael Niedermayer
    }
1062
1063
    return mode;
1064
}
1065
1066
/**
1067
 * gets the predicted intra4x4 prediction mode.
1068
 */
1069
static inline int pred_intra_mode(H264Context *h, int n){
1070
    const int index8= scan8[n];
1071
    const int left= h->intra4x4_pred_mode_cache[index8 - 1];
1072
    const int top = h->intra4x4_pred_mode_cache[index8 - 8];
1073
    const int min= FFMIN(left, top);
1074
1075 95c26348 Michael Niedermayer
    tprintf("mode:%d %d min:%d\n", left ,top, min);
1076 0da71265 Michael Niedermayer
1077
    if(min<0) return DC_PRED;
1078
    else      return min;
1079
}
1080
1081
static inline void write_back_non_zero_count(H264Context *h){
1082
    MpegEncContext * const s = &h->s;
1083 7bc9090a Michael Niedermayer
    const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
1084 0da71265 Michael Niedermayer
1085 6867a90b Loic Le Loarer
    h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
1086
    h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
1087
    h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
1088 53c05b1e Michael Niedermayer
    h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
1089 6867a90b Loic Le Loarer
    h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
1090
    h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
1091
    h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
1092 115329f1 Diego Biurrun
1093 6867a90b Loic Le Loarer
    h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
1094 53c05b1e Michael Niedermayer
    h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
1095 6867a90b Loic Le Loarer
    h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
1096 53c05b1e Michael Niedermayer
1097 6867a90b Loic Le Loarer
    h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
1098 53c05b1e Michael Niedermayer
    h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
1099 6867a90b Loic Le Loarer
    h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
1100 5d18eaad Loren Merritt
1101
    if(FRAME_MBAFF){
1102
        // store all luma nnzs, for deblocking
1103
        int v = 0, i;
1104
        for(i=0; i<16; i++)
1105
            v += (!!h->non_zero_count_cache[scan8[i]]) << i;
1106
        *(uint16_t*)&h->non_zero_count[mb_xy][14] = v;
1107
    }
1108 0da71265 Michael Niedermayer
}
1109
1110
/**
1111
 * gets the predicted number of non zero coefficients.
1112
 * @param n block index
1113
 */
1114
static inline int pred_non_zero_count(H264Context *h, int n){
1115
    const int index8= scan8[n];
1116
    const int left= h->non_zero_count_cache[index8 - 1];
1117
    const int top = h->non_zero_count_cache[index8 - 8];
1118
    int i= left + top;
1119 115329f1 Diego Biurrun
1120 0da71265 Michael Niedermayer
    if(i<64) i= (i+1)>>1;
1121
1122 95c26348 Michael Niedermayer
    tprintf("pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
1123 0da71265 Michael Niedermayer
1124
    return i&31;
1125
}
1126
1127 1924f3ce Michael Niedermayer
static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
1128
    const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
1129
1130 5d18eaad Loren Merritt
    /* there is no consistent mapping of mvs to neighboring locations that will
1131
     * make mbaff happy, so we can't move all this logic to fill_caches */
1132
    if(FRAME_MBAFF){
1133
        MpegEncContext *s = &h->s;
1134
        const int *mb_types = s->current_picture_ptr->mb_type;
1135
        const int16_t *mv;
1136
        *(uint32_t*)h->mv_cache[list][scan8[0]-2] = 0;
1137
        *C = h->mv_cache[list][scan8[0]-2];
1138
1139
        if(!MB_FIELD
1140
           && (s->mb_y&1) && i < scan8[0]+8 && topright_ref != PART_NOT_AVAILABLE){
1141
            int topright_xy = s->mb_x + (s->mb_y-1)*s->mb_stride + (i == scan8[0]+3);
1142
            if(IS_INTERLACED(mb_types[topright_xy])){
1143
#define SET_DIAG_MV(MV_OP, REF_OP, X4, Y4)\
1144
                const int x4 = X4, y4 = Y4;\
1145
                const int mb_type = mb_types[(x4>>2)+(y4>>2)*s->mb_stride];\
1146
                if(!USES_LIST(mb_type,list) && !IS_8X8(mb_type))\
1147
                    return LIST_NOT_USED;\
1148
                mv = s->current_picture_ptr->motion_val[list][x4 + y4*h->b_stride];\
1149
                h->mv_cache[list][scan8[0]-2][0] = mv[0];\
1150
                h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
1151
                return s->current_picture_ptr->ref_index[list][(x4>>1) + (y4>>1)*h->b8_stride] REF_OP;
1152
1153
                SET_DIAG_MV(*2, >>1, s->mb_x*4+(i&7)-4+part_width, s->mb_y*4-1);
1154
            }
1155
        }
1156
        if(topright_ref == PART_NOT_AVAILABLE
1157
           && ((s->mb_y&1) || i >= scan8[0]+8) && (i&7)==4
1158
           && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
1159
            if(!MB_FIELD
1160
               && IS_INTERLACED(mb_types[h->left_mb_xy[0]])){
1161
                SET_DIAG_MV(*2, >>1, s->mb_x*4-1, (s->mb_y|1)*4+(s->mb_y&1)*2+(i>>4)-1);
1162
            }
1163
            if(MB_FIELD
1164
               && !IS_INTERLACED(mb_types[h->left_mb_xy[0]])
1165
               && i >= scan8[0]+8){
1166
                // leftshift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's ok.
1167
                SET_DIAG_MV(>>1, <<1, s->mb_x*4-1, (s->mb_y&~1)*4 - 1 + ((i-scan8[0])>>3)*2);
1168
            }
1169
        }
1170
#undef SET_DIAG_MV
1171
    }
1172
1173 1924f3ce Michael Niedermayer
    if(topright_ref != PART_NOT_AVAILABLE){
1174
        *C= h->mv_cache[list][ i - 8 + part_width ];
1175
        return topright_ref;
1176
    }else{
1177 95c26348 Michael Niedermayer
        tprintf("topright MV not available\n");
1178
1179 1924f3ce Michael Niedermayer
        *C= h->mv_cache[list][ i - 8 - 1 ];
1180
        return h->ref_cache[list][ i - 8 - 1 ];
1181
    }
1182
}
1183
1184 0da71265 Michael Niedermayer
/**
1185
 * gets the predicted MV.
1186
 * @param n the block index
1187
 * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
1188
 * @param mx the x component of the predicted motion vector
1189
 * @param my the y component of the predicted motion vector
1190
 */
1191
static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
1192
    const int index8= scan8[n];
1193
    const int top_ref=      h->ref_cache[list][ index8 - 8 ];
1194
    const int left_ref=     h->ref_cache[list][ index8 - 1 ];
1195
    const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
1196
    const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
1197 1924f3ce Michael Niedermayer
    const int16_t * C;
1198
    int diagonal_ref, match_count;
1199
1200 0da71265 Michael Niedermayer
    assert(part_width==1 || part_width==2 || part_width==4);
1201 1924f3ce Michael Niedermayer
1202 0da71265 Michael Niedermayer
/* mv_cache
1203 115329f1 Diego Biurrun
  B . . A T T T T
1204 0da71265 Michael Niedermayer
  U . . L . . , .
1205
  U . . L . . . .
1206
  U . . L . . , .
1207
  . . . L . . . .
1208
*/
1209 1924f3ce Michael Niedermayer
1210
    diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
1211
    match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
1212 827c91bf Loic Le Loarer
    tprintf("pred_motion match_count=%d\n", match_count);
1213 1924f3ce Michael Niedermayer
    if(match_count > 1){ //most common
1214
        *mx= mid_pred(A[0], B[0], C[0]);
1215
        *my= mid_pred(A[1], B[1], C[1]);
1216
    }else if(match_count==1){
1217
        if(left_ref==ref){
1218
            *mx= A[0];
1219 115329f1 Diego Biurrun
            *my= A[1];
1220 1924f3ce Michael Niedermayer
        }else if(top_ref==ref){
1221
            *mx= B[0];
1222 115329f1 Diego Biurrun
            *my= B[1];
1223 0da71265 Michael Niedermayer
        }else{
1224 1924f3ce Michael Niedermayer
            *mx= C[0];
1225 115329f1 Diego Biurrun
            *my= C[1];
1226 0da71265 Michael Niedermayer
        }
1227
    }else{
1228 1924f3ce Michael Niedermayer
        if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
1229 0da71265 Michael Niedermayer
            *mx= A[0];
1230 115329f1 Diego Biurrun
            *my= A[1];
1231 0da71265 Michael Niedermayer
        }else{
1232 1924f3ce Michael Niedermayer
            *mx= mid_pred(A[0], B[0], C[0]);
1233
            *my= mid_pred(A[1], B[1], C[1]);
1234 0da71265 Michael Niedermayer
        }
1235
    }
1236 115329f1 Diego Biurrun
1237 af6e2fed Michael Niedermayer
    tprintf("pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1],                    diagonal_ref, C[0], C[1], left_ref, A[0], A[1], ref, *mx, *my, h->s.mb_x, h->s.mb_y, n, list);
1238 0da71265 Michael Niedermayer
}
1239
1240
/**
1241
 * gets the directionally predicted 16x8 MV.
1242
 * @param n the block index
1243
 * @param mx the x component of the predicted motion vector
1244
 * @param my the y component of the predicted motion vector
1245
 */
1246
static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
1247
    if(n==0){
1248
        const int top_ref=      h->ref_cache[list][ scan8[0] - 8 ];
1249
        const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
1250
1251 579448a0 Loïc Le Loarer
        tprintf("pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], h->s.mb_x, h->s.mb_y, n, list);
1252 115329f1 Diego Biurrun
1253 0da71265 Michael Niedermayer
        if(top_ref == ref){
1254
            *mx= B[0];
1255
            *my= B[1];
1256
            return;
1257
        }
1258
    }else{
1259
        const int left_ref=     h->ref_cache[list][ scan8[8] - 1 ];
1260
        const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
1261 115329f1 Diego Biurrun
1262 579448a0 Loïc Le Loarer
        tprintf("pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
1263 0da71265 Michael Niedermayer
1264
        if(left_ref == ref){
1265
            *mx= A[0];
1266
            *my= A[1];
1267
            return;
1268
        }
1269
    }
1270
1271
    //RARE
1272
    pred_motion(h, n, 4, list, ref, mx, my);
1273
}
1274
1275
/**
1276
 * gets the directionally predicted 8x16 MV.
1277
 * @param n the block index
1278
 * @param mx the x component of the predicted motion vector
1279
 * @param my the y component of the predicted motion vector
1280
 */
1281
static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
1282
    if(n==0){
1283
        const int left_ref=      h->ref_cache[list][ scan8[0] - 1 ];
1284
        const int16_t * const A=  h->mv_cache[list][ scan8[0] - 1 ];
1285 115329f1 Diego Biurrun
1286 579448a0 Loïc Le Loarer
        tprintf("pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
1287 0da71265 Michael Niedermayer
1288
        if(left_ref == ref){
1289
            *mx= A[0];
1290
            *my= A[1];
1291
            return;
1292
        }
1293
    }else{
1294 1924f3ce Michael Niedermayer
        const int16_t * C;
1295
        int diagonal_ref;
1296
1297
        diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
1298 115329f1 Diego Biurrun
1299 579448a0 Loïc Le Loarer
        tprintf("pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", diagonal_ref, C[0], C[1], h->s.mb_x, h->s.mb_y, n, list);
1300 0da71265 Michael Niedermayer
1301 115329f1 Diego Biurrun
        if(diagonal_ref == ref){
1302 0da71265 Michael Niedermayer
            *mx= C[0];
1303
            *my= C[1];
1304
            return;
1305
        }
1306
    }
1307
1308
    //RARE
1309
    pred_motion(h, n, 2, list, ref, mx, my);
1310
}
1311
1312
static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
1313
    const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
1314
    const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
1315
1316 5175b937 Loic Le Loarer
    tprintf("pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
1317 0da71265 Michael Niedermayer
1318
    if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
1319
       || (top_ref == 0  && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
1320
       || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
1321 115329f1 Diego Biurrun
1322 0da71265 Michael Niedermayer
        *mx = *my = 0;
1323
        return;
1324
    }
1325 115329f1 Diego Biurrun
1326 0da71265 Michael Niedermayer
    pred_motion(h, 0, 4, 0, 0, mx, my);
1327
1328
    return;
1329
}
1330
1331 5ad984c9 Loren Merritt
static inline void direct_dist_scale_factor(H264Context * const h){
1332
    const int poc = h->s.current_picture_ptr->poc;
1333
    const int poc1 = h->ref_list[1][0].poc;
1334
    int i;
1335
    for(i=0; i<h->ref_count[0]; i++){
1336
        int poc0 = h->ref_list[0][i].poc;
1337
        int td = clip(poc1 - poc0, -128, 127);
1338
        if(td == 0 /* FIXME || pic0 is a long-term ref */){
1339
            h->dist_scale_factor[i] = 256;
1340
        }else{
1341
            int tb = clip(poc - poc0, -128, 127);
1342
            int tx = (16384 + (ABS(td) >> 1)) / td;
1343
            h->dist_scale_factor[i] = clip((tb*tx + 32) >> 6, -1024, 1023);
1344
        }
1345
    }
1346 5d18eaad Loren Merritt
    if(FRAME_MBAFF){
1347
        for(i=0; i<h->ref_count[0]; i++){
1348
            h->dist_scale_factor_field[2*i] =
1349
            h->dist_scale_factor_field[2*i+1] = h->dist_scale_factor[i];
1350
        }
1351
    }
1352 5ad984c9 Loren Merritt
}
1353 2f944356 Loren Merritt
static inline void direct_ref_list_init(H264Context * const h){
1354
    MpegEncContext * const s = &h->s;
1355
    Picture * const ref1 = &h->ref_list[1][0];
1356
    Picture * const cur = s->current_picture_ptr;
1357
    int list, i, j;
1358
    if(cur->pict_type == I_TYPE)
1359
        cur->ref_count[0] = 0;
1360
    if(cur->pict_type != B_TYPE)
1361
        cur->ref_count[1] = 0;
1362
    for(list=0; list<2; list++){
1363
        cur->ref_count[list] = h->ref_count[list];
1364
        for(j=0; j<h->ref_count[list]; j++)
1365
            cur->ref_poc[list][j] = h->ref_list[list][j].poc;
1366
    }
1367
    if(cur->pict_type != B_TYPE || h->direct_spatial_mv_pred)
1368
        return;
1369
    for(list=0; list<2; list++){
1370
        for(i=0; i<ref1->ref_count[list]; i++){
1371
            const int poc = ref1->ref_poc[list][i];
1372 171c4076 Loren Merritt
            h->map_col_to_list0[list][i] = 0; /* bogus; fills in for missing frames */
1373 2f944356 Loren Merritt
            for(j=0; j<h->ref_count[list]; j++)
1374
                if(h->ref_list[list][j].poc == poc){
1375
                    h->map_col_to_list0[list][i] = j;
1376
                    break;
1377
                }
1378
        }
1379
    }
1380 5d18eaad Loren Merritt
    if(FRAME_MBAFF){
1381
        for(list=0; list<2; list++){
1382
            for(i=0; i<ref1->ref_count[list]; i++){
1383
                j = h->map_col_to_list0[list][i];
1384
                h->map_col_to_list0_field[list][2*i] = 2*j;
1385
                h->map_col_to_list0_field[list][2*i+1] = 2*j+1;
1386
            }
1387
        }
1388
    }
1389 2f944356 Loren Merritt
}
1390 5ad984c9 Loren Merritt
1391
static inline void pred_direct_motion(H264Context * const h, int *mb_type){
1392
    MpegEncContext * const s = &h->s;
1393
    const int mb_xy =   s->mb_x +   s->mb_y*s->mb_stride;
1394
    const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
1395
    const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
1396
    const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
1397
    const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
1398 4866bd2b Michael Niedermayer
    const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[1][b4_xy];
1399 5ad984c9 Loren Merritt
    const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
1400 2f944356 Loren Merritt
    const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy];
1401 5ad984c9 Loren Merritt
    const int is_b8x8 = IS_8X8(*mb_type);
1402
    int sub_mb_type;
1403
    int i8, i4;
1404
1405 5d18eaad Loren Merritt
#define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
1406 5ad984c9 Loren Merritt
    if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
1407
        /* FIXME save sub mb types from previous frames (or derive from MVs)
1408
         * so we know exactly what block size to use */
1409
        sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
1410 3622988f Loren Merritt
        *mb_type =    MB_TYPE_8x8|MB_TYPE_L0L1;
1411 5d18eaad Loren Merritt
    }else if(!is_b8x8 && (mb_type_col & MB_TYPE_16x16_OR_INTRA)){
1412 5ad984c9 Loren Merritt
        sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
1413
        *mb_type =    MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
1414
    }else{
1415
        sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
1416 3622988f Loren Merritt
        *mb_type =    MB_TYPE_8x8|MB_TYPE_L0L1;
1417 5ad984c9 Loren Merritt
    }
1418
    if(!is_b8x8)
1419
        *mb_type |= MB_TYPE_DIRECT2;
1420 5d18eaad Loren Merritt
    if(MB_FIELD)
1421
        *mb_type |= MB_TYPE_INTERLACED;
1422 5ad984c9 Loren Merritt
1423 827c91bf Loic Le Loarer
    tprintf("mb_type = %08x, sub_mb_type = %08x, is_b8x8 = %d, mb_type_col = %08x\n", *mb_type, sub_mb_type, is_b8x8, mb_type_col);
1424 115329f1 Diego Biurrun
1425 5ad984c9 Loren Merritt
    if(h->direct_spatial_mv_pred){
1426
        int ref[2];
1427
        int mv[2][2];
1428
        int list;
1429
1430 5d18eaad Loren Merritt
        /* FIXME interlacing + spatial direct uses wrong colocated block positions */
1431
1432 5ad984c9 Loren Merritt
        /* ref = min(neighbors) */
1433
        for(list=0; list<2; list++){
1434
            int refa = h->ref_cache[list][scan8[0] - 1];
1435
            int refb = h->ref_cache[list][scan8[0] - 8];
1436
            int refc = h->ref_cache[list][scan8[0] - 8 + 4];
1437
            if(refc == -2)
1438
                refc = h->ref_cache[list][scan8[0] - 8 - 1];
1439
            ref[list] = refa;
1440
            if(ref[list] < 0 || (refb < ref[list] && refb >= 0))
1441
                ref[list] = refb;
1442
            if(ref[list] < 0 || (refc < ref[list] && refc >= 0))
1443
                ref[list] = refc;
1444
            if(ref[list] < 0)
1445
                ref[list] = -1;
1446
        }
1447
1448
        if(ref[0] < 0 && ref[1] < 0){
1449
            ref[0] = ref[1] = 0;
1450
            mv[0][0] = mv[0][1] =
1451
            mv[1][0] = mv[1][1] = 0;
1452
        }else{
1453
            for(list=0; list<2; list++){
1454
                if(ref[list] >= 0)
1455
                    pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
1456
                else
1457
                    mv[list][0] = mv[list][1] = 0;
1458
            }
1459
        }
1460
1461
        if(ref[1] < 0){
1462
            *mb_type &= ~MB_TYPE_P0L1;
1463
            sub_mb_type &= ~MB_TYPE_P0L1;
1464
        }else if(ref[0] < 0){
1465
            *mb_type &= ~MB_TYPE_P0L0;
1466
            sub_mb_type &= ~MB_TYPE_P0L0;
1467
        }
1468
1469
        if(IS_16X16(*mb_type)){
1470 cec93959 Loren Merritt
            fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
1471
            fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
1472 115329f1 Diego Biurrun
            if(!IS_INTRA(mb_type_col)
1473 bf4e3bd2 Måns Rullgård
               && (   (l1ref0[0] == 0 && ABS(l1mv0[0][0]) <= 1 && ABS(l1mv0[0][1]) <= 1)
1474
                   || (l1ref0[0]  < 0 && l1ref1[0] == 0 && ABS(l1mv1[0][0]) <= 1 && ABS(l1mv1[0][1]) <= 1
1475
                       && (h->x264_build>33 || !h->x264_build)))){
1476 5ad984c9 Loren Merritt
                if(ref[0] > 0)
1477
                    fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
1478
                else
1479
                    fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
1480
                if(ref[1] > 0)
1481
                    fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
1482
                else
1483
                    fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
1484
            }else{
1485
                fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
1486
                fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
1487
            }
1488
        }else{
1489
            for(i8=0; i8<4; i8++){
1490
                const int x8 = i8&1;
1491
                const int y8 = i8>>1;
1492 115329f1 Diego Biurrun
1493 5ad984c9 Loren Merritt
                if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
1494
                    continue;
1495
                h->sub_mb_type[i8] = sub_mb_type;
1496 115329f1 Diego Biurrun
1497 5ad984c9 Loren Merritt
                fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
1498
                fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
1499 cec93959 Loren Merritt
                fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
1500
                fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
1501 115329f1 Diego Biurrun
1502 5ad984c9 Loren Merritt
                /* col_zero_flag */
1503 115329f1 Diego Biurrun
                if(!IS_INTRA(mb_type_col) && (   l1ref0[x8 + y8*h->b8_stride] == 0
1504
                                              || (l1ref0[x8 + y8*h->b8_stride] < 0 && l1ref1[x8 + y8*h->b8_stride] == 0
1505 bf4e3bd2 Måns Rullgård
                                                  && (h->x264_build>33 || !h->x264_build)))){
1506 4866bd2b Michael Niedermayer
                    const int16_t (*l1mv)[2]= l1ref0[x8 + y8*h->b8_stride] == 0 ? l1mv0 : l1mv1;
1507 f1f17e54 Loren Merritt
                    if(IS_SUB_8X8(sub_mb_type)){
1508
                        const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
1509
                        if(ABS(mv_col[0]) <= 1 && ABS(mv_col[1]) <= 1){
1510
                            if(ref[0] == 0)
1511
                                fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
1512
                            if(ref[1] == 0)
1513
                                fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
1514
                        }
1515
                    }else
1516 5ad984c9 Loren Merritt
                    for(i4=0; i4<4; i4++){
1517 4866bd2b Michael Niedermayer
                        const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
1518 5ad984c9 Loren Merritt
                        if(ABS(mv_col[0]) <= 1 && ABS(mv_col[1]) <= 1){
1519
                            if(ref[0] == 0)
1520
                                *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
1521
                            if(ref[1] == 0)
1522
                                *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
1523
                        }
1524
                    }
1525
                }
1526
            }
1527
        }
1528
    }else{ /* direct temporal mv pred */
1529 5d18eaad Loren Merritt
        const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
1530
        const int *dist_scale_factor = h->dist_scale_factor;
1531
1532
        if(FRAME_MBAFF){
1533
            if(IS_INTERLACED(*mb_type)){
1534
                map_col_to_list0[0] = h->map_col_to_list0_field[0];
1535
                map_col_to_list0[1] = h->map_col_to_list0_field[1];
1536
                dist_scale_factor = h->dist_scale_factor_field;
1537
            }
1538
            if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col)){
1539
                /* FIXME assumes direct_8x8_inference == 1 */
1540
                const int pair_xy = s->mb_x + (s->mb_y&~1)*s->mb_stride;
1541
                int mb_types_col[2];
1542
                int y_shift;
1543
1544
                *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1
1545
                         | (is_b8x8 ? 0 : MB_TYPE_DIRECT2)
1546
                         | (*mb_type & MB_TYPE_INTERLACED);
1547
                sub_mb_type = MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_16x16;
1548
1549
                if(IS_INTERLACED(*mb_type)){
1550
                    /* frame to field scaling */
1551
                    mb_types_col[0] = h->ref_list[1][0].mb_type[pair_xy];
1552
                    mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
1553
                    if(s->mb_y&1){
1554
                        l1ref0 -= 2*h->b8_stride;
1555
                        l1ref1 -= 2*h->b8_stride;
1556
                        l1mv0 -= 4*h->b_stride;
1557
                        l1mv1 -= 4*h->b_stride;
1558
                    }
1559
                    y_shift = 0;
1560
1561
                    if(   (mb_types_col[0] & MB_TYPE_16x16_OR_INTRA)
1562
                       && (mb_types_col[1] & MB_TYPE_16x16_OR_INTRA)
1563
                       && !is_b8x8)
1564
                        *mb_type |= MB_TYPE_16x8;
1565
                    else
1566
                        *mb_type |= MB_TYPE_8x8;
1567
                }else{
1568
                    /* field to frame scaling */
1569
                    /* col_mb_y = (mb_y&~1) + (topAbsDiffPOC < bottomAbsDiffPOC ? 0 : 1)
1570
                     * but in MBAFF, top and bottom POC are equal */
1571
                    int dy = (s->mb_y&1) ? 1 : 2;
1572
                    mb_types_col[0] =
1573
                    mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
1574
                    l1ref0 += dy*h->b8_stride;
1575
                    l1ref1 += dy*h->b8_stride;
1576
                    l1mv0 += 2*dy*h->b_stride;
1577
                    l1mv1 += 2*dy*h->b_stride;
1578
                    y_shift = 2;
1579
1580
                    if((mb_types_col[0] & (MB_TYPE_16x16_OR_INTRA|MB_TYPE_16x8))
1581
                       && !is_b8x8)
1582
                        *mb_type |= MB_TYPE_16x16;
1583
                    else
1584
                        *mb_type |= MB_TYPE_8x8;
1585
                }
1586
1587
                for(i8=0; i8<4; i8++){
1588
                    const int x8 = i8&1;
1589
                    const int y8 = i8>>1;
1590
                    int ref0, scale;
1591
                    const int16_t (*l1mv)[2]= l1mv0;
1592
1593
                    if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
1594
                        continue;
1595
                    h->sub_mb_type[i8] = sub_mb_type;
1596
1597
                    fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
1598
                    if(IS_INTRA(mb_types_col[y8])){
1599
                        fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
1600
                        fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
1601
                        fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
1602
                        continue;
1603
                    }
1604
1605
                    ref0 = l1ref0[x8 + (y8*2>>y_shift)*h->b8_stride];
1606
                    if(ref0 >= 0)
1607
                        ref0 = map_col_to_list0[0][ref0*2>>y_shift];
1608
                    else{
1609
                        ref0 = map_col_to_list0[1][l1ref1[x8 + (y8*2>>y_shift)*h->b8_stride]*2>>y_shift];
1610
                        l1mv= l1mv1;
1611
                    }
1612
                    scale = dist_scale_factor[ref0];
1613
                    fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
1614
1615
                    {
1616
                        const int16_t *mv_col = l1mv[x8*3 + (y8*6>>y_shift)*h->b_stride];
1617
                        int my_col = (mv_col[1]<<y_shift)/2;
1618
                        int mx = (scale * mv_col[0] + 128) >> 8;
1619
                        int my = (scale * my_col + 128) >> 8;
1620
                        fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
1621
                        fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
1622
                    }
1623
                }
1624
                return;
1625
            }
1626
        }
1627
1628
        /* one-to-one mv scaling */
1629
1630 5ad984c9 Loren Merritt
        if(IS_16X16(*mb_type)){
1631
            fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
1632
            if(IS_INTRA(mb_type_col)){
1633
                fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
1634
                fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
1635
                fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
1636
            }else{
1637 5d18eaad Loren Merritt
                const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0]]
1638
                                                : map_col_to_list0[1][l1ref1[0]];
1639
                const int scale = dist_scale_factor[ref0];
1640 8583bef8 Michael Niedermayer
                const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
1641 5ad984c9 Loren Merritt
                int mv_l0[2];
1642 5d18eaad Loren Merritt
                mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
1643
                mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
1644 5ad984c9 Loren Merritt
                fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref0, 1);
1645
                fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0],mv_l0[1]), 4);
1646
                fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]), 4);
1647
            }
1648
        }else{
1649
            for(i8=0; i8<4; i8++){
1650
                const int x8 = i8&1;
1651
                const int y8 = i8>>1;
1652 5d18eaad Loren Merritt
                int ref0, scale;
1653 bf4e3bd2 Måns Rullgård
                const int16_t (*l1mv)[2]= l1mv0;
1654 8583bef8 Michael Niedermayer
1655 5ad984c9 Loren Merritt
                if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
1656
                    continue;
1657
                h->sub_mb_type[i8] = sub_mb_type;
1658 5d18eaad Loren Merritt
                fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
1659 5ad984c9 Loren Merritt
                if(IS_INTRA(mb_type_col)){
1660
                    fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
1661
                    fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
1662
                    fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
1663
                    continue;
1664
                }
1665 115329f1 Diego Biurrun
1666 5ad984c9 Loren Merritt
                ref0 = l1ref0[x8 + y8*h->b8_stride];
1667 2f944356 Loren Merritt
                if(ref0 >= 0)
1668 5d18eaad Loren Merritt
                    ref0 = map_col_to_list0[0][ref0];
1669 8583bef8 Michael Niedermayer
                else{
1670 5d18eaad Loren Merritt
                    ref0 = map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]];
1671 8583bef8 Michael Niedermayer
                    l1mv= l1mv1;
1672
                }
1673 5d18eaad Loren Merritt
                scale = dist_scale_factor[ref0];
1674 115329f1 Diego Biurrun
1675 5ad984c9 Loren Merritt
                fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
1676 f1f17e54 Loren Merritt
                if(IS_SUB_8X8(sub_mb_type)){
1677
                    const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
1678 5d18eaad Loren Merritt
                    int mx = (scale * mv_col[0] + 128) >> 8;
1679
                    int my = (scale * mv_col[1] + 128) >> 8;
1680 f1f17e54 Loren Merritt
                    fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
1681
                    fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
1682
                }else
1683 5ad984c9 Loren Merritt
                for(i4=0; i4<4; i4++){
1684 8583bef8 Michael Niedermayer
                    const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
1685 5ad984c9 Loren Merritt
                    int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
1686 5d18eaad Loren Merritt
                    mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
1687
                    mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
1688 5ad984c9 Loren Merritt
                    *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
1689
                        pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
1690
                }
1691
            }
1692
        }
1693
    }
1694
}
1695
1696 0da71265 Michael Niedermayer
static inline void write_back_motion(H264Context *h, int mb_type){
1697
    MpegEncContext * const s = &h->s;
1698
    const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
1699
    const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
1700
    int list;
1701
1702 2ea39252 Loren Merritt
    if(!USES_LIST(mb_type, 0))
1703
        fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, h->b8_stride, (uint8_t)LIST_NOT_USED, 1);
1704
1705 0da71265 Michael Niedermayer
    for(list=0; list<2; list++){
1706
        int y;
1707 53b19144 Loren Merritt
        if(!USES_LIST(mb_type, list))
1708 5ad984c9 Loren Merritt
            continue;
1709 115329f1 Diego Biurrun
1710 0da71265 Michael Niedermayer
        for(y=0; y<4; y++){
1711
            *(uint64_t*)s->current_picture.motion_val[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mv_cache[list][scan8[0]+0 + 8*y];
1712
            *(uint64_t*)s->current_picture.motion_val[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mv_cache[list][scan8[0]+2 + 8*y];
1713
        }
1714 9e528114 Laurent Aimar
        if( h->pps.cabac ) {
1715
            for(y=0; y<4; y++){
1716
                *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
1717
                *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
1718
            }
1719
        }
1720 53b19144 Loren Merritt
1721
        {
1722
            uint8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
1723
            ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]];
1724
            ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]];
1725
            ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]];
1726
            ref_index[1+1*h->b8_stride]= h->ref_cache[list][scan8[12]];
1727 0da71265 Michael Niedermayer
        }
1728
    }
1729 115329f1 Diego Biurrun
1730 5ad984c9 Loren Merritt
    if(h->slice_type == B_TYPE && h->pps.cabac){
1731
        if(IS_8X8(mb_type)){
1732 53b19144 Loren Merritt
            uint8_t *direct_table = &h->direct_table[b8_xy];
1733
            direct_table[1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
1734
            direct_table[0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
1735
            direct_table[1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
1736 5ad984c9 Loren Merritt
        }
1737
    }
1738 0da71265 Michael Niedermayer
}
1739
1740
/**
1741
 * Decodes a network abstraction layer unit.
1742
 * @param consumed is the number of bytes used as input
1743
 * @param length is the length of the array
1744 3b66c4c5 Kevin Baragona
 * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp tailing?
1745 115329f1 Diego Biurrun
 * @returns decoded bytes, might be src+1 if no escapes
1746 0da71265 Michael Niedermayer
 */
1747
static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){
1748
    int i, si, di;
1749
    uint8_t *dst;
1750
1751 bb270c08 Diego Biurrun
//    src[0]&0x80;                //forbidden bit
1752 0da71265 Michael Niedermayer
    h->nal_ref_idc= src[0]>>5;
1753
    h->nal_unit_type= src[0]&0x1F;
1754
1755
    src++; length--;
1756 115329f1 Diego Biurrun
#if 0
1757 0da71265 Michael Niedermayer
    for(i=0; i<length; i++)
1758
        printf("%2X ", src[i]);
1759
#endif
1760
    for(i=0; i+1<length; i+=2){
1761
        if(src[i]) continue;
1762
        if(i>0 && src[i-1]==0) i--;
1763
        if(i+2<length && src[i+1]==0 && src[i+2]<=3){
1764
            if(src[i+2]!=3){
1765
                /* startcode, so we must be past the end */
1766
                length=i;
1767
            }
1768
            break;
1769
        }
1770
    }
1771
1772
    if(i>=length-1){ //no escaped 0
1773
        *dst_length= length;
1774
        *consumed= length+1; //+1 for the header
1775 115329f1 Diego Biurrun
        return src;
1776 0da71265 Michael Niedermayer
    }
1777
1778
    h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length);
1779
    dst= h->rbsp_buffer;
1780
1781 3b66c4c5 Kevin Baragona
//printf("decoding esc\n");
1782 0da71265 Michael Niedermayer
    si=di=0;
1783 115329f1 Diego Biurrun
    while(si<length){
1784 0da71265 Michael Niedermayer
        //remove escapes (very rare 1:2^22)
1785
        if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
1786
            if(src[si+2]==3){ //escape
1787
                dst[di++]= 0;
1788
                dst[di++]= 0;
1789
                si+=3;
1790 c8470cc1 Michael Niedermayer
                continue;
1791 0da71265 Michael Niedermayer
            }else //next start code
1792
                break;
1793
        }
1794
1795
        dst[di++]= src[si++];
1796
    }
1797
1798
    *dst_length= di;
1799
    *consumed= si + 1;//+1 for the header
1800
//FIXME store exact number of bits in the getbitcontext (its needed for decoding)
1801
    return dst;
1802
}
1803
1804 e5017ab8 Laurent Aimar
#if 0
1805 0da71265 Michael Niedermayer
/**
1806
 * @param src the data which should be escaped
1807
 * @param dst the target buffer, dst+1 == src is allowed as a special case
1808
 * @param length the length of the src data
1809
 * @param dst_length the length of the dst array
1810
 * @returns length of escaped data in bytes or -1 if an error occured
1811
 */
1812
static int encode_nal(H264Context *h, uint8_t *dst, uint8_t *src, int length, int dst_length){
1813
    int i, escape_count, si, di;
1814
    uint8_t *temp;
1815 115329f1 Diego Biurrun

1816 0da71265 Michael Niedermayer
    assert(length>=0);
1817
    assert(dst_length>0);
1818 115329f1 Diego Biurrun

1819 0da71265 Michael Niedermayer
    dst[0]= (h->nal_ref_idc<<5) + h->nal_unit_type;
1820

1821
    if(length==0) return 1;
1822

1823
    escape_count= 0;
1824
    for(i=0; i<length; i+=2){
1825
        if(src[i]) continue;
1826 115329f1 Diego Biurrun
        if(i>0 && src[i-1]==0)
1827 0da71265 Michael Niedermayer
            i--;
1828
        if(i+2<length && src[i+1]==0 && src[i+2]<=3){
1829
            escape_count++;
1830
            i+=2;
1831
        }
1832
    }
1833 115329f1 Diego Biurrun

1834
    if(escape_count==0){
1835 0da71265 Michael Niedermayer
        if(dst+1 != src)
1836
            memcpy(dst+1, src, length);
1837
        return length + 1;
1838
    }
1839 115329f1 Diego Biurrun

1840 0da71265 Michael Niedermayer
    if(length + escape_count + 1> dst_length)
1841
        return -1;
1842

1843
    //this should be damn rare (hopefully)
1844

1845
    h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length + escape_count);
1846
    temp= h->rbsp_buffer;
1847
//printf("encoding esc\n");
1848 115329f1 Diego Biurrun

1849 0da71265 Michael Niedermayer
    si= 0;
1850
    di= 0;
1851
    while(si < length){
1852
        if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
1853
            temp[di++]= 0; si++;
1854
            temp[di++]= 0; si++;
1855 115329f1 Diego Biurrun
            temp[di++]= 3;
1856 0da71265 Michael Niedermayer
            temp[di++]= src[si++];
1857
        }
1858
        else
1859
            temp[di++]= src[si++];
1860
    }
1861
    memcpy(dst+1, temp, length+escape_count);
1862 115329f1 Diego Biurrun

1863 0da71265 Michael Niedermayer
    assert(di == length+escape_count);
1864 115329f1 Diego Biurrun

1865 0da71265 Michael Niedermayer
    return di + 1;
1866
}
1867

1868
/**
1869
 * write 1,10,100,1000,... for alignment, yes its exactly inverse to mpeg4
1870
 */
1871
static void encode_rbsp_trailing(PutBitContext *pb){
1872
    int length;
1873
    put_bits(pb, 1, 1);
1874 fe455f33 Alex Beregszaszi
    length= (-put_bits_count(pb))&7;
1875 0da71265 Michael Niedermayer
    if(length) put_bits(pb, length, 0);
1876
}
1877 e5017ab8 Laurent Aimar
#endif
1878 0da71265 Michael Niedermayer
1879
/**
1880
 * identifies the exact end of the bitstream
1881
 * @return the length of the trailing, or 0 if damaged
1882
 */
1883
static int decode_rbsp_trailing(uint8_t *src){
1884
    int v= *src;
1885
    int r;
1886
1887 95c26348 Michael Niedermayer
    tprintf("rbsp trailing %X\n", v);
1888 0da71265 Michael Niedermayer
1889
    for(r=1; r<9; r++){
1890
        if(v&1) return r;
1891
        v>>=1;
1892
    }
1893
    return 0;
1894
}
1895
1896
/**
1897
 * idct tranforms the 16 dc values and dequantize them.
1898
 * @param qp quantization parameter
1899
 */
1900 239ea04c Loren Merritt
static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
1901 0da71265 Michael Niedermayer
#define stride 16
1902
    int i;
1903
    int temp[16]; //FIXME check if this is a good idea
1904
    static const int x_offset[4]={0, 1*stride, 4* stride,  5*stride};
1905
    static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
1906
1907
//memset(block, 64, 2*256);
1908
//return;
1909
    for(i=0; i<4; i++){
1910
        const int offset= y_offset[i];
1911
        const int z0= block[offset+stride*0] + block[offset+stride*4];
1912
        const int z1= block[offset+stride*0] - block[offset+stride*4];
1913
        const int z2= block[offset+stride*1] - block[offset+stride*5];
1914
        const int z3= block[offset+stride*1] + block[offset+stride*5];
1915
1916
        temp[4*i+0]= z0+z3;
1917
        temp[4*i+1]= z1+z2;
1918
        temp[4*i+2]= z1-z2;
1919
        temp[4*i+3]= z0-z3;
1920
    }
1921
1922
    for(i=0; i<4; i++){
1923
        const int offset= x_offset[i];
1924
        const int z0= temp[4*0+i] + temp[4*2+i];
1925
        const int z1= temp[4*0+i] - temp[4*2+i];
1926
        const int z2= temp[4*1+i] - temp[4*3+i];
1927
        const int z3= temp[4*1+i] + temp[4*3+i];
1928
1929 239ea04c Loren Merritt
        block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_resdual
1930
        block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
1931
        block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
1932
        block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
1933 0da71265 Michael Niedermayer
    }
1934
}
1935
1936 e5017ab8 Laurent Aimar
#if 0
1937 0da71265 Michael Niedermayer
/**
1938
 * dct tranforms the 16 dc values.
1939
 * @param qp quantization parameter ??? FIXME
1940
 */
1941
static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
1942
//    const int qmul= dequant_coeff[qp][0];
1943
    int i;
1944
    int temp[16]; //FIXME check if this is a good idea
1945
    static const int x_offset[4]={0, 1*stride, 4* stride,  5*stride};
1946
    static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
1947

1948
    for(i=0; i<4; i++){
1949
        const int offset= y_offset[i];
1950
        const int z0= block[offset+stride*0] + block[offset+stride*4];
1951
        const int z1= block[offset+stride*0] - block[offset+stride*4];
1952
        const int z2= block[offset+stride*1] - block[offset+stride*5];
1953
        const int z3= block[offset+stride*1] + block[offset+stride*5];
1954

1955
        temp[4*i+0]= z0+z3;
1956
        temp[4*i+1]= z1+z2;
1957
        temp[4*i+2]= z1-z2;
1958
        temp[4*i+3]= z0-z3;
1959
    }
1960

1961
    for(i=0; i<4; i++){
1962
        const int offset= x_offset[i];
1963
        const int z0= temp[4*0+i] + temp[4*2+i];
1964
        const int z1= temp[4*0+i] - temp[4*2+i];
1965
        const int z2= temp[4*1+i] - temp[4*3+i];
1966
        const int z3= temp[4*1+i] + temp[4*3+i];
1967

1968
        block[stride*0 +offset]= (z0 + z3)>>1;
1969
        block[stride*2 +offset]= (z1 + z2)>>1;
1970
        block[stride*8 +offset]= (z1 - z2)>>1;
1971
        block[stride*10+offset]= (z0 - z3)>>1;
1972
    }
1973
}
1974 e5017ab8 Laurent Aimar
#endif
1975
1976 0da71265 Michael Niedermayer
#undef xStride
1977
#undef stride
1978
1979 239ea04c Loren Merritt
static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
1980 0da71265 Michael Niedermayer
    const int stride= 16*2;
1981
    const int xStride= 16;
1982
    int a,b,c,d,e;
1983
1984
    a= block[stride*0 + xStride*0];
1985
    b= block[stride*0 + xStride*1];
1986
    c= block[stride*1 + xStride*0];
1987
    d= block[stride*1 + xStride*1];
1988
1989
    e= a-b;
1990
    a= a+b;
1991
    b= c-d;
1992
    c= c+d;
1993
1994 239ea04c Loren Merritt
    block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
1995
    block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
1996
    block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
1997
    block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
1998 0da71265 Michael Niedermayer
}
1999
2000 e5017ab8 Laurent Aimar
#if 0
2001 0da71265 Michael Niedermayer
static void chroma_dc_dct_c(DCTELEM *block){
2002
    const int stride= 16*2;
2003
    const int xStride= 16;
2004
    int a,b,c,d,e;
2005

2006
    a= block[stride*0 + xStride*0];
2007
    b= block[stride*0 + xStride*1];
2008
    c= block[stride*1 + xStride*0];
2009
    d= block[stride*1 + xStride*1];
2010

2011
    e= a-b;
2012
    a= a+b;
2013
    b= c-d;
2014
    c= c+d;
2015

2016
    block[stride*0 + xStride*0]= (a+c);
2017
    block[stride*0 + xStride*1]= (e+b);
2018
    block[stride*1 + xStride*0]= (a-c);
2019
    block[stride*1 + xStride*1]= (e-b);
2020
}
2021 e5017ab8 Laurent Aimar
#endif
2022 0da71265 Michael Niedermayer
2023
/**
2024
 * gets the chroma qp.
2025
 */
2026 6ba71fc4 Loïc Le Loarer
static inline int get_chroma_qp(int chroma_qp_index_offset, int qscale){
2027 115329f1 Diego Biurrun
2028 6ba71fc4 Loïc Le Loarer
    return chroma_qp[clip(qscale + chroma_qp_index_offset, 0, 51)];
2029 0da71265 Michael Niedermayer
}
2030
2031
2032 e5017ab8 Laurent Aimar
#if 0
2033 0da71265 Michael Niedermayer
static void h264_diff_dct_c(DCTELEM *block, uint8_t *src1, uint8_t *src2, int stride){
2034
    int i;
2035
    //FIXME try int temp instead of block
2036 115329f1 Diego Biurrun

2037 0da71265 Michael Niedermayer
    for(i=0; i<4; i++){
2038
        const int d0= src1[0 + i*stride] - src2[0 + i*stride];
2039
        const int d1= src1[1 + i*stride] - src2[1 + i*stride];
2040
        const int d2= src1[2 + i*stride] - src2[2 + i*stride];
2041
        const int d3= src1[3 + i*stride] - src2[3 + i*stride];
2042
        const int z0= d0 + d3;
2043
        const int z3= d0 - d3;
2044
        const int z1= d1 + d2;
2045
        const int z2= d1 - d2;
2046 115329f1 Diego Biurrun

2047 0da71265 Michael Niedermayer
        block[0 + 4*i]=   z0 +   z1;
2048
        block[1 + 4*i]= 2*z3 +   z2;
2049
        block[2 + 4*i]=   z0 -   z1;
2050
        block[3 + 4*i]=   z3 - 2*z2;
2051 115329f1 Diego Biurrun
    }
2052 0da71265 Michael Niedermayer

2053
    for(i=0; i<4; i++){
2054
        const int z0= block[0*4 + i] + block[3*4 + i];
2055
        const int z3= block[0*4 + i] - block[3*4 + i];
2056
        const int z1= block[1*4 + i] + block[2*4 + i];
2057
        const int z2= block[1*4 + i] - block[2*4 + i];
2058 115329f1 Diego Biurrun

2059 0da71265 Michael Niedermayer
        block[0*4 + i]=   z0 +   z1;
2060
        block[1*4 + i]= 2*z3 +   z2;
2061
        block[2*4 + i]=   z0 -   z1;
2062
        block[3*4 + i]=   z3 - 2*z2;
2063
    }
2064
}
2065 e5017ab8 Laurent Aimar
#endif
2066 0da71265 Michael Niedermayer
2067 3b66c4c5 Kevin Baragona
//FIXME need to check that this doesnt overflow signed 32 bit for low qp, i am not sure, it's very close
2068 0da71265 Michael Niedermayer
//FIXME check that gcc inlines this (and optimizes intra & seperate_dc stuff away)
2069
static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int seperate_dc){
2070
    int i;
2071
    const int * const quant_table= quant_coeff[qscale];
2072
    const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
2073
    const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
2074
    const unsigned int threshold2= (threshold1<<1);
2075
    int last_non_zero;
2076
2077
    if(seperate_dc){
2078
        if(qscale<=18){
2079
            //avoid overflows
2080
            const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
2081
            const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
2082
            const unsigned int dc_threshold2= (dc_threshold1<<1);
2083
2084
            int level= block[0]*quant_coeff[qscale+18][0];
2085
            if(((unsigned)(level+dc_threshold1))>dc_threshold2){
2086
                if(level>0){
2087
                    level= (dc_bias + level)>>(QUANT_SHIFT-2);
2088
                    block[0]= level;
2089
                }else{
2090
                    level= (dc_bias - level)>>(QUANT_SHIFT-2);
2091
                    block[0]= -level;
2092
                }
2093
//                last_non_zero = i;
2094
            }else{
2095
                block[0]=0;
2096
            }
2097
        }else{
2098
            const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
2099
            const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
2100
            const unsigned int dc_threshold2= (dc_threshold1<<1);
2101
2102
            int level= block[0]*quant_table[0];
2103
            if(((unsigned)(level+dc_threshold1))>dc_threshold2){
2104
                if(level>0){
2105
                    level= (dc_bias + level)>>(QUANT_SHIFT+1);
2106
                    block[0]= level;
2107
                }else{
2108
                    level= (dc_bias - level)>>(QUANT_SHIFT+1);
2109
                    block[0]= -level;
2110
                }
2111
//                last_non_zero = i;
2112
            }else{
2113
                block[0]=0;
2114
            }
2115
        }
2116
        last_non_zero= 0;
2117
        i=1;
2118
    }else{
2119
        last_non_zero= -1;
2120
        i=0;
2121
    }
2122
2123
    for(; i<16; i++){
2124
        const int j= scantable[i];
2125
        int level= block[j]*quant_table[j];
2126
2127
//        if(   bias+level >= (1<<(QMAT_SHIFT - 3))
2128
//           || bias-level >= (1<<(QMAT_SHIFT - 3))){
2129
        if(((unsigned)(level+threshold1))>threshold2){
2130
            if(level>0){
2131
                level= (bias + level)>>QUANT_SHIFT;
2132
                block[j]= level;
2133
            }else{
2134
                level= (bias - level)>>QUANT_SHIFT;
2135
                block[j]= -level;
2136
            }
2137
            last_non_zero = i;
2138
        }else{
2139
            block[j]=0;
2140
        }
2141
    }
2142
2143
    return last_non_zero;
2144
}
2145
2146
static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){
2147
    const uint32_t a= ((uint32_t*)(src-stride))[0];
2148
    ((uint32_t*)(src+0*stride))[0]= a;
2149
    ((uint32_t*)(src+1*stride))[0]= a;
2150
    ((uint32_t*)(src+2*stride))[0]= a;
2151
    ((uint32_t*)(src+3*stride))[0]= a;
2152
}
2153
2154
static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){
2155
    ((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101;
2156
    ((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101;
2157
    ((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101;
2158
    ((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101;
2159
}
2160
2161
static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride){
2162
    const int dc= (  src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
2163
                   + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
2164 115329f1 Diego Biurrun
2165
    ((uint32_t*)(src+0*stride))[0]=
2166
    ((uint32_t*)(src+1*stride))[0]=
2167
    ((uint32_t*)(src+2*stride))[0]=
2168
    ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
2169 0da71265 Michael Niedermayer
}
2170
2171
static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride){
2172
    const int dc= (  src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
2173 115329f1 Diego Biurrun
2174
    ((uint32_t*)(src+0*stride))[0]=
2175
    ((uint32_t*)(src+1*stride))[0]=
2176
    ((uint32_t*)(src+2*stride))[0]=
2177
    ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
2178 0da71265 Michael Niedermayer
}
2179
2180
static void pred4x4_top_dc_c(uint8_t *src, uint8_t *topright, int stride){
2181
    const int dc= (  src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2;
2182 115329f1 Diego Biurrun
2183
    ((uint32_t*)(src+0*stride))[0]=
2184
    ((uint32_t*)(src+1*stride))[0]=
2185
    ((uint32_t*)(src+2*stride))[0]=
2186
    ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
2187 0da71265 Michael Niedermayer
}
2188
2189
static void pred4x4_128_dc_c(uint8_t *src, uint8_t *topright, int stride){
2190 115329f1 Diego Biurrun
    ((uint32_t*)(src+0*stride))[0]=
2191
    ((uint32_t*)(src+1*stride))[0]=
2192
    ((uint32_t*)(src+2*stride))[0]=
2193 0da71265 Michael Niedermayer
    ((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U;
2194
}
2195
2196
2197
#define LOAD_TOP_RIGHT_EDGE\
2198
    const int t4= topright[0];\
2199
    const int t5= topright[1];\
2200
    const int t6= topright[2];\
2201
    const int t7= topright[3];\
2202
2203
#define LOAD_LEFT_EDGE\
2204
    const int l0= src[-1+0*stride];\
2205
    const int l1= src[-1+1*stride];\
2206
    const int l2= src[-1+2*stride];\
2207
    const int l3= src[-1+3*stride];\
2208
2209
#define LOAD_TOP_EDGE\
2210
    const int t0= src[ 0-1*stride];\
2211
    const int t1= src[ 1-1*stride];\
2212
    const int t2= src[ 2-1*stride];\
2213
    const int t3= src[ 3-1*stride];\
2214
2215
static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){
2216
    const int lt= src[-1-1*stride];
2217
    LOAD_TOP_EDGE
2218
    LOAD_LEFT_EDGE
2219
2220 115329f1 Diego Biurrun
    src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
2221 0da71265 Michael Niedermayer
    src[0+2*stride]=
2222 115329f1 Diego Biurrun
    src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
2223 0da71265 Michael Niedermayer
    src[0+1*stride]=
2224
    src[1+2*stride]=
2225 115329f1 Diego Biurrun
    src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
2226 0da71265 Michael Niedermayer
    src[0+0*stride]=
2227
    src[1+1*stride]=
2228
    src[2+2*stride]=
2229 115329f1 Diego Biurrun
    src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
2230 0da71265 Michael Niedermayer
    src[1+0*stride]=
2231
    src[2+1*stride]=
2232
    src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
2233
    src[2+0*stride]=
2234
    src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
2235
    src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
2236 4cfbf61b Falk Hüffner
}
2237 0da71265 Michael Niedermayer
2238
static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){
2239 115329f1 Diego Biurrun
    LOAD_TOP_EDGE
2240
    LOAD_TOP_RIGHT_EDGE
2241
//    LOAD_LEFT_EDGE
2242 0da71265 Michael Niedermayer
2243
    src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
2244
    src[1+0*stride]=
2245
    src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
2246
    src[2+0*stride]=
2247
    src[1+1*stride]=
2248
    src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
2249
    src[3+0*stride]=
2250
    src[2+1*stride]=
2251
    src[1+2*stride]=
2252
    src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
2253
    src[3+1*stride]=
2254
    src[2+2*stride]=
2255
    src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
2256
    src[3+2*stride]=
2257
    src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
2258
    src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
2259 4cfbf61b Falk Hüffner
}
2260 0da71265 Michael Niedermayer
2261
static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
2262
    const int lt= src[-1-1*stride];
2263 115329f1 Diego Biurrun
    LOAD_TOP_EDGE
2264
    LOAD_LEFT_EDGE
2265 0da71265 Michael Niedermayer
    const __attribute__((unused)) int unu= l3;
2266
2267
    src[0+0*stride]=
2268
    src[1+2*stride]=(lt + t0 + 1)>>1;
2269
    src[1+0*stride]=
2270
    src[2+2*stride]=(t0 + t1 + 1)>>1;
2271
    src[2+0*stride]=
2272
    src[3+2*stride]=(t1 + t2 + 1)>>1;
2273
    src[3+0*stride]=(t2 + t3 + 1)>>1;
2274
    src[0+1*stride]=
2275
    src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
2276
    src[1+1*stride]=
2277
    src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
2278
    src[2+1*stride]=
2279
    src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
2280
    src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
2281
    src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
2282
    src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
2283 4cfbf61b Falk Hüffner
}
2284 0da71265 Michael Niedermayer
2285
static void pred4x4_vertical_left_c(uint8_t *src, uint8_t *topright, int stride){
2286 115329f1 Diego Biurrun
    LOAD_TOP_EDGE
2287
    LOAD_TOP_RIGHT_EDGE
2288 0da71265 Michael Niedermayer
    const __attribute__((unused)) int unu= t7;
2289
2290
    src[0+0*stride]=(t0 + t1 + 1)>>1;
2291
    src[1+0*stride]=
2292
    src[0+2*stride]=(t1 + t2 + 1)>>1;
2293
    src[2+0*stride]=
2294
    src[1+2*stride]=(t2 + t3 + 1)>>1;
2295
    src[3+0*stride]=
2296
    src[2+2*stride]=(t3 + t4+ 1)>>1;
2297
    src[3+2*stride]=(t4 + t5+ 1)>>1;
2298
    src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
2299
    src[1+1*stride]=
2300
    src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
2301
    src[2+1*stride]=
2302
    src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
2303
    src[3+1*stride]=
2304
    src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
2305
    src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
2306 4cfbf61b Falk Hüffner
}
2307 0da71265 Michael Niedermayer
2308
static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){
2309 115329f1 Diego Biurrun
    LOAD_LEFT_EDGE
2310 0da71265 Michael Niedermayer
2311
    src[0+0*stride]=(l0 + l1 + 1)>>1;
2312
    src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
2313
    src[2+0*stride]=
2314
    src[0+1*stride]=(l1 + l2 + 1)>>1;
2315
    src[3+0*stride]=
2316
    src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
2317
    src[2+1*stride]=
2318
    src[0+2*stride]=(l2 + l3 + 1)>>1;
2319
    src[3+1*stride]=
2320
    src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
2321
    src[3+2*stride]=
2322
    src[1+3*stride]=
2323
    src[0+3*stride]=
2324
    src[2+2*stride]=
2325
    src[2+3*stride]=
2326
    src[3+3*stride]=l3;
2327 4cfbf61b Falk Hüffner
}
2328 115329f1 Diego Biurrun
2329 0da71265 Michael Niedermayer
static void pred4x4_horizontal_down_c(uint8_t *src, uint8_t *topright, int stride){
2330
    const int lt= src[-1-1*stride];
2331 115329f1 Diego Biurrun
    LOAD_TOP_EDGE
2332
    LOAD_LEFT_EDGE
2333 0da71265 Michael Niedermayer
    const __attribute__((unused)) int unu= t3;
2334
2335
    src[0+0*stride]=
2336
    src[2+1*stride]=(lt + l0 + 1)>>1;
2337
    src[1+0*stride]=
2338
    src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
2339
    src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
2340
    src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
2341
    src[0+1*stride]=
2342
    src[2+2*stride]=(l0 + l1 + 1)>>1;
2343
    src[1+1*stride]=
2344
    src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
2345
    src[0+2*stride]=
2346
    src[2+3*stride]=(l1 + l2+ 1)>>1;
2347
    src[1+2*stride]=
2348
    src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
2349
    src[0+3*stride]=(l2 + l3 + 1)>>1;
2350
    src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
2351 4cfbf61b Falk Hüffner
}
2352 0da71265 Michael Niedermayer
2353
static void pred16x16_vertical_c(uint8_t *src, int stride){
2354
    int i;
2355
    const uint32_t a= ((uint32_t*)(src-stride))[0];
2356
    const uint32_t b= ((uint32_t*)(src-stride))[1];
2357
    const uint32_t c= ((uint32_t*)(src-stride))[2];
2358
    const uint32_t d= ((uint32_t*)(src-stride))[3];
2359 115329f1 Diego Biurrun
2360 0da71265 Michael Niedermayer
    for(i=0; i<16; i++){
2361
        ((uint32_t*)(src+i*stride))[0]= a;
2362
        ((uint32_t*)(src+i*stride))[1]= b;
2363
        ((uint32_t*)(src+i*stride))[2]= c;
2364
        ((uint32_t*)(src+i*stride))[3]= d;
2365
    }
2366
}
2367
2368
static void pred16x16_horizontal_c(uint8_t *src, int stride){
2369
    int i;
2370
2371
    for(i=0; i<16; i++){
2372
        ((uint32_t*)(src+i*stride))[0]=
2373
        ((uint32_t*)(src+i*stride))[1]=
2374
        ((uint32_t*)(src+i*stride))[2]=
2375
        ((uint32_t*)(src+i*stride))[3]= src[-1+i*stride]*0x01010101;
2376
    }
2377
}
2378
2379
static void pred16x16_dc_c(uint8_t *src, int stride){
2380
    int i, dc=0;
2381
2382
    for(i=0;i<16; i++){
2383
        dc+= src[-1+i*stride];
2384
    }
2385 115329f1 Diego Biurrun
2386 0da71265 Michael Niedermayer
    for(i=0;i<16; i++){
2387
        dc+= src[i-stride];
2388
    }
2389
2390
    dc= 0x01010101*((dc + 16)>>5);
2391
2392
    for(i=0; i<16; i++){
2393
        ((uint32_t*)(src+i*stride))[0]=
2394
        ((uint32_t*)(src+i*stride))[1]=
2395
        ((uint32_t*)(src+i*stride))[2]=
2396
        ((uint32_t*)(src+i*stride))[3]= dc;
2397
    }
2398
}
2399
2400
static void pred16x16_left_dc_c(uint8_t *src, int stride){
2401
    int i, dc=0;
2402
2403
    for(i=0;i<16; i++){
2404
        dc+= src[-1+i*stride];
2405
    }
2406 115329f1 Diego Biurrun
2407 0da71265 Michael Niedermayer
    dc= 0x01010101*((dc + 8)>>4);
2408
2409
    for(i=0; i<16; i++){
2410
        ((uint32_t*)(src+i*stride))[0]=
2411
        ((uint32_t*)(src+i*stride))[1]=
2412
        ((uint32_t*)(src+i*stride))[2]=
2413
        ((uint32_t*)(src+i*stride))[3]= dc;
2414
    }
2415
}
2416
2417
static void pred16x16_top_dc_c(uint8_t *src, int stride){
2418
    int i, dc=0;
2419
2420
    for(i=0;i<16; i++){
2421
        dc+= src[i-stride];
2422
    }
2423
    dc= 0x01010101*((dc + 8)>>4);
2424
2425
    for(i=0; i<16; i++){
2426
        ((uint32_t*)(src+i*stride))[0]=
2427
        ((uint32_t*)(src+i*stride))[1]=
2428
        ((uint32_t*)(src+i*stride))[2]=
2429
        ((uint32_t*)(src+i*stride))[3]= dc;
2430
    }
2431
}
2432
2433
static void pred16x16_128_dc_c(uint8_t *src, int stride){
2434
    int i;
2435
2436
    for(i=0; i<16; i++){
2437
        ((uint32_t*)(src+i*stride))[0]=
2438
        ((uint32_t*)(src+i*stride))[1]=
2439
        ((uint32_t*)(src+i*stride))[2]=
2440
        ((uint32_t*)(src+i*stride))[3]= 0x01010101U*128U;
2441
    }
2442
}
2443
2444 8b82a956 Michael Niedermayer
static inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){
2445 30f73fc7 Michael Niedermayer
  int i, j, k;
2446
  int a;
2447
  uint8_t *cm = cropTbl + MAX_NEG_CROP;
2448
  const uint8_t * const src0 = src+7-stride;
2449
  const uint8_t *src1 = src+8*stride-1;
2450
  const uint8_t *src2 = src1-2*stride;      // == src+6*stride-1;
2451
  int H = src0[1] - src0[-1];
2452
  int V = src1[0] - src2[ 0];
2453
  for(k=2; k<=8; ++k) {
2454
    src1 += stride; src2 -= stride;
2455
    H += k*(src0[k] - src0[-k]);
2456
    V += k*(src1[0] - src2[ 0]);
2457
  }
2458 8b82a956 Michael Niedermayer
  if(svq3){
2459
    H = ( 5*(H/4) ) / 16;
2460
    V = ( 5*(V/4) ) / 16;
2461 2e26c8d2 Mike Melanson
2462
    /* required for 100% accuracy */
2463
    i = H; H = V; V = i;
2464 8b82a956 Michael Niedermayer
  }else{
2465
    H = ( 5*H+32 ) >> 6;
2466
    V = ( 5*V+32 ) >> 6;
2467
  }
2468 30f73fc7 Michael Niedermayer
2469
  a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
2470
  for(j=16; j>0; --j) {
2471
    int b = a;
2472
    a += V;
2473
    for(i=-16; i<0; i+=4) {
2474
      src[16+i] = cm[ (b    ) >> 5 ];
2475
      src[17+i] = cm[ (b+  H) >> 5 ];
2476
      src[18+i] = cm[ (b+2*H) >> 5 ];
2477
      src[19+i] = cm[ (b+3*H) >> 5 ];
2478
      b += 4*H;
2479 0da71265 Michael Niedermayer
    }
2480 30f73fc7 Michael Niedermayer
    src += stride;
2481
  }
2482 0da71265 Michael Niedermayer
}
2483
2484 8b82a956 Michael Niedermayer
static void pred16x16_plane_c(uint8_t *src, int stride){
2485
    pred16x16_plane_compat_c(src, stride, 0);
2486
}
2487
2488 0da71265 Michael Niedermayer
static void pred8x8_vertical_c(uint8_t *src, int stride){
2489
    int i;
2490
    const uint32_t a= ((uint32_t*)(src-stride))[0];
2491
    const uint32_t b= ((uint32_t*)(src-stride))[1];
2492 115329f1 Diego Biurrun
2493 0da71265 Michael Niedermayer
    for(i=0; i<8; i++){
2494
        ((uint32_t*)(src+i*stride))[0]= a;
2495
        ((uint32_t*)(src+i*stride))[1]= b;
2496
    }
2497
}
2498
2499
static void pred8x8_horizontal_c(uint8_t *src, int stride){
2500
    int i;
2501
2502
    for(i=0; i<8; i++){
2503
        ((uint32_t*)(src+i*stride))[0]=
2504
        ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101;
2505
    }
2506
}
2507
2508
static void pred8x8_128_dc_c(uint8_t *src, int stride){
2509
    int i;
2510
2511 43efd19a Loren Merritt
    for(i=0; i<8; i++){
2512 115329f1 Diego Biurrun
        ((uint32_t*)(src+i*stride))[0]=
2513 0da71265 Michael Niedermayer
        ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
2514
    }
2515
}
2516
2517
static void pred8x8_left_dc_c(uint8_t *src, int stride){
2518
    int i;
2519
    int dc0, dc2;
2520
2521
    dc0=dc2=0;
2522
    for(i=0;i<4; i++){
2523
        dc0+= src[-1+i*stride];
2524
        dc2+= src[-1+(i+4)*stride];
2525
    }
2526
    dc0= 0x01010101*((dc0 + 2)>>2);
2527
    dc2= 0x01010101*((dc2 + 2)>>2);
2528
2529
    for(i=0; i<4; i++){
2530
        ((uint32_t*)(src+i*stride))[0]=
2531
        ((uint32_t*)(src+i*stride))[1]= dc0;
2532
    }
2533
    for(i=4; i<8; i++){
2534
        ((uint32_t*)(src+i*stride))[0]=
2535
        ((uint32_t*)(src+i*stride))[1]= dc2;
2536
    }
2537
}
2538
2539
static void pred8x8_top_dc_c(uint8_t *src, int stride){
2540
    int i;
2541
    int dc0, dc1;
2542
2543
    dc0=dc1=0;
2544
    for(i=0;i<4; i++){
2545
        dc0+= src[i-stride];
2546
        dc1+= src[4+i-stride];
2547
    }
2548
    dc0= 0x01010101*((dc0 + 2)>>2);
2549
    dc1= 0x01010101*((dc1 + 2)>>2);
2550
2551
    for(i=0; i<4; i++){
2552
        ((uint32_t*)(src+i*stride))[0]= dc0;
2553
        ((uint32_t*)(src+i*stride))[1]= dc1;
2554
    }
2555
    for(i=4; i<8; i++){
2556
        ((uint32_t*)(src+i*stride))[0]= dc0;
2557
        ((uint32_t*)(src+i*stride))[1]= dc1;
2558
    }
2559
}
2560
2561
2562
static void pred8x8_dc_c(uint8_t *src, int stride){
2563
    int i;
2564
    int dc0, dc1, dc2, dc3;
2565
2566
    dc0=dc1=dc2=0;
2567
    for(i=0;i<4; i++){
2568
        dc0+= src[-1+i*stride] + src[i-stride];
2569
        dc1+= src[4+i-stride];
2570
        dc2+= src[-1+(i+4)*stride];
2571
    }
2572
    dc3= 0x01010101*((dc1 + dc2 + 4)>>3);
2573
    dc0= 0x01010101*((dc0 + 4)>>3);
2574
    dc1= 0x01010101*((dc1 + 2)>>2);
2575
    dc2= 0x01010101*((dc2 + 2)>>2);
2576
2577
    for(i=0; i<4; i++){
2578
        ((uint32_t*)(src+i*stride))[0]= dc0;
2579
        ((uint32_t*)(src+i*stride))[1]= dc1;
2580
    }
2581
    for(i=4; i<8; i++){
2582
        ((uint32_t*)(src+i*stride))[0]= dc2;
2583
        ((uint32_t*)(src+i*stride))[1]= dc3;
2584
    }
2585
}
2586
2587
static void pred8x8_plane_c(uint8_t *src, int stride){
2588 30f73fc7 Michael Niedermayer
  int j, k;
2589
  int a;
2590
  uint8_t *cm = cropTbl + MAX_NEG_CROP;
2591
  const uint8_t * const src0 = src+3-stride;
2592
  const uint8_t *src1 = src+4*stride-1;
2593
  const uint8_t *src2 = src1-2*stride;      // == src+2*stride-1;
2594
  int H = src0[1] - src0[-1];
2595
  int V = src1[0] - src2[ 0];
2596
  for(k=2; k<=4; ++k) {
2597
    src1 += stride; src2 -= stride;
2598
    H += k*(src0[k] - src0[-k]);
2599
    V += k*(src1[0] - src2[ 0]);
2600
  }
2601
  H = ( 17*H+16 ) >> 5;
2602
  V = ( 17*V+16 ) >> 5;
2603
2604
  a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
2605
  for(j=8; j>0; --j) {
2606
    int b = a;
2607
    a += V;
2608
    src[0] = cm[ (b    ) >> 5 ];
2609
    src[1] = cm[ (b+  H) >> 5 ];
2610
    src[2] = cm[ (b+2*H) >> 5 ];
2611
    src[3] = cm[ (b+3*H) >> 5 ];
2612
    src[4] = cm[ (b+4*H) >> 5 ];
2613
    src[5] = cm[ (b+5*H) >> 5 ];
2614
    src[6] = cm[ (b+6*H) >> 5 ];
2615
    src[7] = cm[ (b+7*H) >> 5 ];
2616
    src += stride;
2617
  }
2618 0da71265 Michael Niedermayer
}
2619
2620 43efd19a Loren Merritt
#define SRC(x,y) src[(x)+(y)*stride]
2621
#define PL(y) \
2622
    const int l##y = (SRC(-1,y-1) + 2*SRC(-1,y) + SRC(-1,y+1) + 2) >> 2;
2623
#define PREDICT_8x8_LOAD_LEFT \
2624
    const int l0 = ((has_topleft ? SRC(-1,-1) : SRC(-1,0)) \
2625
                     + 2*SRC(-1,0) + SRC(-1,1) + 2) >> 2; \
2626
    PL(1) PL(2) PL(3) PL(4) PL(5) PL(6) \
2627 bf4e3bd2 Måns Rullgård
    const int l7 attribute_unused = (SRC(-1,6) + 3*SRC(-1,7) + 2) >> 2
2628 43efd19a Loren Merritt
2629
#define PT(x) \
2630
    const int t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
2631
#define PREDICT_8x8_LOAD_TOP \
2632
    const int t0 = ((has_topleft ? SRC(-1,-1) : SRC(0,-1)) \
2633
                     + 2*SRC(0,-1) + SRC(1,-1) + 2) >> 2; \
2634
    PT(1) PT(2) PT(3) PT(4) PT(5) PT(6) \
2635 bf4e3bd2 Måns Rullgård
    const int t7 attribute_unused = ((has_topright ? SRC(8,-1) : SRC(7,-1)) \
2636 fb3ca6ba Måns Rullgård
                     + 2*SRC(7,-1) + SRC(6,-1) + 2) >> 2
2637 43efd19a Loren Merritt
2638
#define PTR(x) \
2639
    t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
2640
#define PREDICT_8x8_LOAD_TOPRIGHT \
2641
    int t8, t9, t10, t11, t12, t13, t14, t15; \
2642
    if(has_topright) { \
2643
        PTR(8) PTR(9) PTR(10) PTR(11) PTR(12) PTR(13) PTR(14) \
2644
        t15 = (SRC(14,-1) + 3*SRC(15,-1) + 2) >> 2; \
2645
    } else t8=t9=t10=t11=t12=t13=t14=t15= SRC(7,-1);
2646
2647
#define PREDICT_8x8_LOAD_TOPLEFT \
2648 fb3ca6ba Måns Rullgård
    const int lt = (SRC(-1,0) + 2*SRC(-1,-1) + SRC(0,-1) + 2) >> 2
2649 43efd19a Loren Merritt
2650
#define PREDICT_8x8_DC(v) \
2651
    int y; \
2652
    for( y = 0; y < 8; y++ ) { \
2653
        ((uint32_t*)src)[0] = \
2654
        ((uint32_t*)src)[1] = v; \
2655
        src += stride; \
2656
    }
2657
2658
static void pred8x8l_128_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
2659
{
2660
    PREDICT_8x8_DC(0x80808080);
2661
}
2662
static void pred8x8l_left_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
2663
{
2664
    PREDICT_8x8_LOAD_LEFT;
2665
    const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7+4) >> 3) * 0x01010101;
2666
    PREDICT_8x8_DC(dc);
2667
}
2668
static void pred8x8l_top_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
2669
{
2670
    PREDICT_8x8_LOAD_TOP;
2671
    const uint32_t dc = ((t0+t1+t2+t3+t4+t5+t6+t7+4) >> 3) * 0x01010101;
2672
    PREDICT_8x8_DC(dc);
2673
}
2674
static void pred8x8l_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
2675
{
2676
    PREDICT_8x8_LOAD_LEFT;
2677
    PREDICT_8x8_LOAD_TOP;
2678
    const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7
2679
                         +t0+t1+t2+t3+t4+t5+t6+t7+8) >> 4) * 0x01010101;
2680
    PREDICT_8x8_DC(dc);
2681
}
2682
static void pred8x8l_horizontal_c(uint8_t *src, int has_topleft, int has_topright, int stride)
2683
{
2684
    PREDICT_8x8_LOAD_LEFT;
2685
#define ROW(y) ((uint32_t*)(src+y*stride))[0] =\
2686
               ((uint32_t*)(src+y*stride))[1] = 0x01010101 * l##y
2687
    ROW(0); ROW(1); ROW(2); ROW(3); ROW(4); ROW(5); ROW(6); ROW(7);
2688
#undef ROW
2689
}
2690
static void pred8x8l_vertical_c(uint8_t *src, int has_topleft, int has_topright, int stride)
2691
{
2692
    int y;
2693
    PREDICT_8x8_LOAD_TOP;
2694
    src[0] = t0;
2695
    src[1] = t1;
2696
    src[2] = t2;
2697
    src[3] = t3;
2698
    src[4] = t4;
2699
    src[5] = t5;
2700
    src[6] = t6;
2701
    src[7] = t7;
2702
    for( y = 1; y < 8; y++ )
2703
        *(uint64_t*)(src+y*stride) = *(uint64_t*)src;
2704
}
2705
static void pred8x8l_down_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
2706
{
2707
    PREDICT_8x8_LOAD_TOP;
2708
    PREDICT_8x8_LOAD_TOPRIGHT;
2709
    SRC(0,0)= (t0 + 2*t1 + t2 + 2) >> 2;
2710
    SRC(0,1)=SRC(1,0)= (t1 + 2*t2 + t3 + 2) >> 2;
2711
    SRC(0,2)=SRC(1,1)=SRC(2,0)= (t2 + 2*t3 + t4 + 2) >> 2;
2712
    SRC(0,3)=SRC(1,2)=SRC(2,1)=SRC(3,0)= (t3 + 2*t4 + t5 + 2) >> 2;
2713
    SRC(0,4)=SRC(1,3)=SRC(2,2)=SRC(3,1)=SRC(4,0)= (t4 + 2*t5 + t6 + 2) >> 2;
2714
    SRC(0,5)=SRC(1,4)=SRC(2,3)=SRC(3,2)=SRC(4,1)=SRC(5,0)= (t5 + 2*t6 + t7 + 2) >> 2;
2715
    SRC(0,6)=SRC(1,5)=SRC(2,4)=SRC(3,3)=SRC(4,2)=SRC(5,1)=SRC(6,0)= (t6 + 2*t7 + t8 + 2) >> 2;
2716
    SRC(0,7)=SRC(1,6)=SRC(2,5)=SRC(3,4)=SRC(4,3)=SRC(5,2)=SRC(6,1)=SRC(7,0)= (t7 + 2*t8 + t9 + 2) >> 2;
2717
    SRC(1,7)=SRC(2,6)=SRC(3,5)=SRC(4,4)=SRC(5,3)=SRC(6,2)=SRC(7,1)= (t8 + 2*t9 + t10 + 2) >> 2;
2718
    SRC(2,7)=SRC(3,6)=SRC(4,5)=SRC(5,4)=SRC(6,3)=SRC(7,2)= (t9 + 2*t10 + t11 + 2) >> 2;
2719
    SRC(3,7)=SRC(4,6)=SRC(5,5)=SRC(6,4)=SRC(7,3)= (t10 + 2*t11 + t12 + 2) >> 2;
2720
    SRC(4,7)=SRC(5,6)=SRC(6,5)=SRC(7,4)= (t11 + 2*t12 + t13 + 2) >> 2;
2721
    SRC(5,7)=SRC(6,6)=SRC(7,5)= (t12 + 2*t13 + t14 + 2) >> 2;
2722
    SRC(6,7)=SRC(7,6)= (t13 + 2*t14 + t15 + 2) >> 2;
2723
    SRC(7,7)= (t14 + 3*t15 + 2) >> 2;
2724
}
2725
static void pred8x8l_down_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
2726
{
2727
    PREDICT_8x8_LOAD_TOP;
2728
    PREDICT_8x8_LOAD_LEFT;
2729
    PREDICT_8x8_LOAD_TOPLEFT;
2730
    SRC(0,7)= (l7 + 2*l6 + l5 + 2) >> 2;
2731
    SRC(0,6)=SRC(1,7)= (l6 + 2*l5 + l4 + 2) >> 2;
2732
    SRC(0,5)=SRC(1,6)=SRC(2,7)= (l5 + 2*l4 + l3 + 2) >> 2;
2733
    SRC(0,4)=SRC(1,5)=SRC(2,6)=SRC(3,7)= (l4 + 2*l3 + l2 + 2) >> 2;
2734
    SRC(0,3)=SRC(1,4)=SRC(2,5)=SRC(3,6)=SRC(4,7)= (l3 + 2*l2 + l1 + 2) >> 2;
2735
    SRC(0,2)=SRC(1,3)=SRC(2,4)=SRC(3,5)=SRC(4,6)=SRC(5,7)= (l2 + 2*l1 + l0 + 2) >> 2;
2736
    SRC(0,1)=SRC(1,2)=SRC(2,3)=SRC(3,4)=SRC(4,5)=SRC(5,6)=SRC(6,7)= (l1 + 2*l0 + lt + 2) >> 2;
2737
    SRC(0,0)=SRC(1,1)=SRC(2,2)=SRC(3,3)=SRC(4,4)=SRC(5,5)=SRC(6,6)=SRC(7,7)= (l0 + 2*lt + t0 + 2) >> 2;
2738
    SRC(1,0)=SRC(2,1)=SRC(3,2)=SRC(4,3)=SRC(5,4)=SRC(6,5)=SRC(7,6)= (lt + 2*t0 + t1 + 2) >> 2;
2739
    SRC(2,0)=SRC(3,1)=SRC(4,2)=SRC(5,3)=SRC(6,4)=SRC(7,5)= (t0 + 2*t1 + t2 + 2) >> 2;
2740
    SRC(3,0)=SRC(4,1)=SRC(5,2)=SRC(6,3)=SRC(7,4)= (t1 + 2*t2 + t3 + 2) >> 2;
2741
    SRC(4,0)=SRC(5,1)=SRC(6,2)=SRC(7,3)= (t2 + 2*t3 + t4 + 2) >> 2;
2742
    SRC(5,0)=SRC(6,1)=SRC(7,2)= (t3 + 2*t4 + t5 + 2) >> 2;
2743
    SRC(6,0)=SRC(7,1)= (t4 + 2*t5 + t6 + 2) >> 2;
2744
    SRC(7,0)= (t5 + 2*t6 + t7 + 2) >> 2;
2745 115329f1 Diego Biurrun
2746 43efd19a Loren Merritt
}
2747
static void pred8x8l_vertical_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
2748
{
2749
    PREDICT_8x8_LOAD_TOP;
2750
    PREDICT_8x8_LOAD_LEFT;
2751