decode H.264 with 8x8 transform.
[libav.git] / libavcodec / h264.c
1 /*
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 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21 /**
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 #include "cabac.h"
35
36 #undef NDEBUG
37 #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 /**
58 * Sequence parameter set
59 */
60 typedef struct SPS{
61
62 int profile_idc;
63 int level_idc;
64 int log2_max_frame_num; ///< log2_max_frame_num_minus4 + 4
65 int poc_type; ///< pic_order_cnt_type
66 int log2_max_poc_lsb; ///< log2_max_pic_order_cnt_lsb_minus4
67 int delta_pic_order_always_zero_flag;
68 int offset_for_non_ref_pic;
69 int offset_for_top_to_bottom_field;
70 int poc_cycle_length; ///< num_ref_frames_in_pic_order_cnt_cycle
71 int ref_frame_count; ///< num_ref_frames
72 int gaps_in_frame_num_allowed_flag;
73 int mb_width; ///< frame_width_in_mbs_minus1 + 1
74 int mb_height; ///< frame_height_in_mbs_minus1 + 1
75 int frame_mbs_only_flag;
76 int mb_aff; ///<mb_adaptive_frame_field_flag
77 int direct_8x8_inference_flag;
78 int crop; ///< frame_cropping_flag
79 int crop_left; ///< frame_cropping_rect_left_offset
80 int crop_right; ///< frame_cropping_rect_right_offset
81 int crop_top; ///< frame_cropping_rect_top_offset
82 int crop_bottom; ///< frame_cropping_rect_bottom_offset
83 int vui_parameters_present_flag;
84 AVRational sar;
85 int timing_info_present_flag;
86 uint32_t num_units_in_tick;
87 uint32_t time_scale;
88 int fixed_frame_rate_flag;
89 short offset_for_ref_frame[256]; //FIXME dyn aloc?
90 int bitstream_restriction_flag;
91 int num_reorder_frames;
92 }SPS;
93
94 /**
95 * Picture parameter set
96 */
97 typedef struct PPS{
98 int sps_id;
99 int cabac; ///< entropy_coding_mode_flag
100 int pic_order_present; ///< pic_order_present_flag
101 int slice_group_count; ///< num_slice_groups_minus1 + 1
102 int mb_slice_group_map_type;
103 int ref_count[2]; ///< num_ref_idx_l0/1_active_minus1 + 1
104 int weighted_pred; ///< weighted_pred_flag
105 int weighted_bipred_idc;
106 int init_qp; ///< pic_init_qp_minus26 + 26
107 int init_qs; ///< pic_init_qs_minus26 + 26
108 int chroma_qp_index_offset;
109 int deblocking_filter_parameters_present; ///< deblocking_filter_parameters_present_flag
110 int constrained_intra_pred; ///< constrained_intra_pred_flag
111 int redundant_pic_cnt_present; ///< redundant_pic_cnt_present_flag
112 int transform_8x8_mode; ///< transform_8x8_mode_flag
113 }PPS;
114
115 /**
116 * Memory management control operation opcode.
117 */
118 typedef enum MMCOOpcode{
119 MMCO_END=0,
120 MMCO_SHORT2UNUSED,
121 MMCO_LONG2UNUSED,
122 MMCO_SHORT2LONG,
123 MMCO_SET_MAX_LONG,
124 MMCO_RESET,
125 MMCO_LONG,
126 } MMCOOpcode;
127
128 /**
129 * Memory management control operation.
130 */
131 typedef struct MMCO{
132 MMCOOpcode opcode;
133 int short_frame_num;
134 int long_index;
135 } MMCO;
136
137 /**
138 * H264Context
139 */
140 typedef struct H264Context{
141 MpegEncContext s;
142 int nal_ref_idc;
143 int nal_unit_type;
144 #define NAL_SLICE 1
145 #define NAL_DPA 2
146 #define NAL_DPB 3
147 #define NAL_DPC 4
148 #define NAL_IDR_SLICE 5
149 #define NAL_SEI 6
150 #define NAL_SPS 7
151 #define NAL_PPS 8
152 #define NAL_PICTURE_DELIMITER 9
153 #define NAL_FILTER_DATA 10
154 uint8_t *rbsp_buffer;
155 int rbsp_buffer_size;
156
157 /**
158 * Used to parse AVC variant of h264
159 */
160 int is_avc; ///< this flag is != 0 if codec is avc1
161 int got_avcC; ///< flag used to parse avcC data only once
162 int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
163
164 int chroma_qp; //QPc
165
166 int prev_mb_skipped; //FIXME remove (IMHO not used)
167
168 //prediction stuff
169 int chroma_pred_mode;
170 int intra16x16_pred_mode;
171
172 int top_mb_xy;
173 int left_mb_xy[2];
174
175 int8_t intra4x4_pred_mode_cache[5*8];
176 int8_t (*intra4x4_pred_mode)[8];
177 void (*pred4x4 [9+3])(uint8_t *src, uint8_t *topright, int stride);//FIXME move to dsp?
178 void (*pred8x8l [9+3])(uint8_t *src, int topleft, int topright, int stride);
179 void (*pred8x8 [4+3])(uint8_t *src, int stride);
180 void (*pred16x16[4+3])(uint8_t *src, int stride);
181 unsigned int topleft_samples_available;
182 unsigned int top_samples_available;
183 unsigned int topright_samples_available;
184 unsigned int left_samples_available;
185 uint8_t (*top_borders[2])[16+2*8];
186 uint8_t left_border[2*(17+2*9)];
187
188 /**
189 * non zero coeff count cache.
190 * is 64 if not available.
191 */
192 uint8_t non_zero_count_cache[6*8] __align8;
193 uint8_t (*non_zero_count)[16];
194
195 /**
196 * Motion vector cache.
197 */
198 int16_t mv_cache[2][5*8][2] __align8;
199 int8_t ref_cache[2][5*8] __align8;
200 #define LIST_NOT_USED -1 //FIXME rename?
201 #define PART_NOT_AVAILABLE -2
202
203 /**
204 * is 1 if the specific list MV&references are set to 0,0,-2.
205 */
206 int mv_cache_clean[2];
207
208 /**
209 * number of neighbors (top and/or left) that used 8x8 dct
210 */
211 int neighbor_transform_size;
212
213 /**
214 * block_offset[ 0..23] for frame macroblocks
215 * block_offset[24..47] for field macroblocks
216 */
217 int block_offset[2*(16+8)];
218
219 uint32_t *mb2b_xy; //FIXME are these 4 a good idea?
220 uint32_t *mb2b8_xy;
221 int b_stride; //FIXME use s->b4_stride
222 int b8_stride;
223
224 int halfpel_flag;
225 int thirdpel_flag;
226
227 int unknown_svq3_flag;
228 int next_slice_index;
229
230 SPS sps_buffer[MAX_SPS_COUNT];
231 SPS sps; ///< current sps
232
233 PPS pps_buffer[MAX_PPS_COUNT];
234 /**
235 * current pps
236 */
237 PPS pps; //FIXME move to Picture perhaps? (->no) do we need that?
238
239 uint16_t (*dequant8_coeff)[64];
240
241 int slice_num;
242 uint8_t *slice_table_base;
243 uint8_t *slice_table; ///< slice_table_base + mb_stride + 1
244 int slice_type;
245 int slice_type_fixed;
246
247 //interlacing specific flags
248 int mb_aff_frame;
249 int mb_field_decoding_flag;
250
251 int sub_mb_type[4];
252
253 //POC stuff
254 int poc_lsb;
255 int poc_msb;
256 int delta_poc_bottom;
257 int delta_poc[2];
258 int frame_num;
259 int prev_poc_msb; ///< poc_msb of the last reference pic for POC type 0
260 int prev_poc_lsb; ///< poc_lsb of the last reference pic for POC type 0
261 int frame_num_offset; ///< for POC type 2
262 int prev_frame_num_offset; ///< for POC type 2
263 int prev_frame_num; ///< frame_num of the last pic for POC type 1/2
264
265 /**
266 * frame_num for frames or 2*frame_num for field pics.
267 */
268 int curr_pic_num;
269
270 /**
271 * max_frame_num or 2*max_frame_num for field pics.
272 */
273 int max_pic_num;
274
275 //Weighted pred stuff
276 int use_weight;
277 int use_weight_chroma;
278 int luma_log2_weight_denom;
279 int chroma_log2_weight_denom;
280 int luma_weight[2][16];
281 int luma_offset[2][16];
282 int chroma_weight[2][16][2];
283 int chroma_offset[2][16][2];
284 int implicit_weight[16][16];
285
286 //deblock
287 int deblocking_filter; ///< disable_deblocking_filter_idc with 1<->0
288 int slice_alpha_c0_offset;
289 int slice_beta_offset;
290
291 int redundant_pic_count;
292
293 int direct_spatial_mv_pred;
294 int dist_scale_factor[16];
295 int map_col_to_list0[2][16];
296
297 /**
298 * num_ref_idx_l0/1_active_minus1 + 1
299 */
300 int ref_count[2];// FIXME split for AFF
301 Picture *short_ref[32];
302 Picture *long_ref[32];
303 Picture default_ref_list[2][32];
304 Picture ref_list[2][32]; //FIXME size?
305 Picture field_ref_list[2][32]; //FIXME size?
306 Picture *delayed_pic[16]; //FIXME size?
307 Picture *delayed_output_pic;
308
309 /**
310 * memory management control operations buffer.
311 */
312 MMCO mmco[MAX_MMCO_COUNT];
313 int mmco_index;
314
315 int long_ref_count; ///< number of actual long term references
316 int short_ref_count; ///< number of actual short term references
317
318 //data partitioning
319 GetBitContext intra_gb;
320 GetBitContext inter_gb;
321 GetBitContext *intra_gb_ptr;
322 GetBitContext *inter_gb_ptr;
323
324 DCTELEM mb[16*24] __align8;
325
326 /**
327 * Cabac
328 */
329 CABACContext cabac;
330 uint8_t cabac_state[460];
331 int cabac_init_idc;
332
333 /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
334 uint16_t *cbp_table;
335 int top_cbp;
336 int left_cbp;
337 /* chroma_pred_mode for i4x4 or i16x16, else 0 */
338 uint8_t *chroma_pred_mode_table;
339 int last_qscale_diff;
340 int16_t (*mvd_table[2])[2];
341 int16_t mvd_cache[2][5*8][2] __align8;
342 uint8_t *direct_table;
343 uint8_t direct_cache[5*8];
344
345 uint8_t zigzag_scan[16];
346 uint8_t field_scan[16];
347 }H264Context;
348
349 static VLC coeff_token_vlc[4];
350 static VLC chroma_dc_coeff_token_vlc;
351
352 static VLC total_zeros_vlc[15];
353 static VLC chroma_dc_total_zeros_vlc[3];
354
355 static VLC run_vlc[6];
356 static VLC run7_vlc;
357
358 static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
359 static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
360 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);
361
362 static inline uint32_t pack16to32(int a, int b){
363 #ifdef WORDS_BIGENDIAN
364 return (b&0xFFFF) + (a<<16);
365 #else
366 return (a&0xFFFF) + (b<<16);
367 #endif
368 }
369
370 /**
371 * fill a rectangle.
372 * @param h height of the rectangle, should be a constant
373 * @param w width of the rectangle, should be a constant
374 * @param size the size of val (1 or 4), should be a constant
375 */
376 static inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){ //FIXME ensure this IS inlined
377 uint8_t *p= (uint8_t*)vp;
378 assert(size==1 || size==4);
379
380 w *= size;
381 stride *= size;
382
383 assert((((int)vp)&(FFMIN(w, STRIDE_ALIGN)-1)) == 0);
384 assert((stride&(w-1))==0);
385 //FIXME check what gcc generates for 64 bit on x86 and possibly write a 32 bit ver of it
386 if(w==2 && h==2){
387 *(uint16_t*)(p + 0)=
388 *(uint16_t*)(p + stride)= size==4 ? val : val*0x0101;
389 }else if(w==2 && h==4){
390 *(uint16_t*)(p + 0*stride)=
391 *(uint16_t*)(p + 1*stride)=
392 *(uint16_t*)(p + 2*stride)=
393 *(uint16_t*)(p + 3*stride)= size==4 ? val : val*0x0101;
394 }else if(w==4 && h==1){
395 *(uint32_t*)(p + 0*stride)= size==4 ? val : val*0x01010101;
396 }else if(w==4 && h==2){
397 *(uint32_t*)(p + 0*stride)=
398 *(uint32_t*)(p + 1*stride)= size==4 ? val : val*0x01010101;
399 }else if(w==4 && h==4){
400 *(uint32_t*)(p + 0*stride)=
401 *(uint32_t*)(p + 1*stride)=
402 *(uint32_t*)(p + 2*stride)=
403 *(uint32_t*)(p + 3*stride)= size==4 ? val : val*0x01010101;
404 }else if(w==8 && h==1){
405 *(uint32_t*)(p + 0)=
406 *(uint32_t*)(p + 4)= size==4 ? val : val*0x01010101;
407 }else if(w==8 && h==2){
408 *(uint32_t*)(p + 0 + 0*stride)=
409 *(uint32_t*)(p + 4 + 0*stride)=
410 *(uint32_t*)(p + 0 + 1*stride)=
411 *(uint32_t*)(p + 4 + 1*stride)= size==4 ? val : val*0x01010101;
412 }else if(w==8 && h==4){
413 *(uint64_t*)(p + 0*stride)=
414 *(uint64_t*)(p + 1*stride)=
415 *(uint64_t*)(p + 2*stride)=
416 *(uint64_t*)(p + 3*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
417 }else if(w==16 && h==2){
418 *(uint64_t*)(p + 0+0*stride)=
419 *(uint64_t*)(p + 8+0*stride)=
420 *(uint64_t*)(p + 0+1*stride)=
421 *(uint64_t*)(p + 8+1*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
422 }else if(w==16 && h==4){
423 *(uint64_t*)(p + 0+0*stride)=
424 *(uint64_t*)(p + 8+0*stride)=
425 *(uint64_t*)(p + 0+1*stride)=
426 *(uint64_t*)(p + 8+1*stride)=
427 *(uint64_t*)(p + 0+2*stride)=
428 *(uint64_t*)(p + 8+2*stride)=
429 *(uint64_t*)(p + 0+3*stride)=
430 *(uint64_t*)(p + 8+3*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
431 }else
432 assert(0);
433 }
434
435 static inline void fill_caches(H264Context *h, int mb_type, int for_deblock){
436 MpegEncContext * const s = &h->s;
437 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
438 int topleft_xy, top_xy, topright_xy, left_xy[2];
439 int topleft_type, top_type, topright_type, left_type[2];
440 int left_block[8];
441 int i;
442
443 //FIXME deblocking can skip fill_caches much of the time with multiple slices too.
444 // the actual condition is whether we're on the edge of a slice,
445 // and even then the intra and nnz parts are unnecessary.
446 if(for_deblock && h->slice_num == 1)
447 return;
448
449 //wow what a mess, why didn't they simplify the interlacing&intra stuff, i can't imagine that these complex rules are worth it
450
451 top_xy = mb_xy - s->mb_stride;
452 topleft_xy = top_xy - 1;
453 topright_xy= top_xy + 1;
454 left_xy[1] = left_xy[0] = mb_xy-1;
455 left_block[0]= 0;
456 left_block[1]= 1;
457 left_block[2]= 2;
458 left_block[3]= 3;
459 left_block[4]= 7;
460 left_block[5]= 10;
461 left_block[6]= 8;
462 left_block[7]= 11;
463 if(h->mb_aff_frame){
464 const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
465 const int top_pair_xy = pair_xy - s->mb_stride;
466 const int topleft_pair_xy = top_pair_xy - 1;
467 const int topright_pair_xy = top_pair_xy + 1;
468 const int topleft_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
469 const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
470 const int topright_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
471 const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
472 const int curr_mb_frame_flag = !IS_INTERLACED(mb_type);
473 const int bottom = (s->mb_y & 1);
474 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);
475 if (bottom
476 ? !curr_mb_frame_flag // bottom macroblock
477 : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
478 ) {
479 top_xy -= s->mb_stride;
480 }
481 if (bottom
482 ? !curr_mb_frame_flag // bottom macroblock
483 : (!curr_mb_frame_flag && !topleft_mb_frame_flag) // top macroblock
484 ) {
485 topleft_xy -= s->mb_stride;
486 }
487 if (bottom
488 ? !curr_mb_frame_flag // bottom macroblock
489 : (!curr_mb_frame_flag && !topright_mb_frame_flag) // top macroblock
490 ) {
491 topright_xy -= s->mb_stride;
492 }
493 if (left_mb_frame_flag != curr_mb_frame_flag) {
494 left_xy[1] = left_xy[0] = pair_xy - 1;
495 if (curr_mb_frame_flag) {
496 if (bottom) {
497 left_block[0]= 2;
498 left_block[1]= 2;
499 left_block[2]= 3;
500 left_block[3]= 3;
501 left_block[4]= 8;
502 left_block[5]= 11;
503 left_block[6]= 8;
504 left_block[7]= 11;
505 } else {
506 left_block[0]= 0;
507 left_block[1]= 0;
508 left_block[2]= 1;
509 left_block[3]= 1;
510 left_block[4]= 7;
511 left_block[5]= 10;
512 left_block[6]= 7;
513 left_block[7]= 10;
514 }
515 } else {
516 left_xy[1] += s->mb_stride;
517 //left_block[0]= 0;
518 left_block[1]= 2;
519 left_block[2]= 0;
520 left_block[3]= 2;
521 //left_block[4]= 7;
522 left_block[5]= 10;
523 left_block[6]= 7;
524 left_block[7]= 10;
525 }
526 }
527 }
528
529 h->top_mb_xy = top_xy;
530 h->left_mb_xy[0] = left_xy[0];
531 h->left_mb_xy[1] = left_xy[1];
532 if(for_deblock){
533 topleft_type = h->slice_table[topleft_xy ] < 255 ? s->current_picture.mb_type[topleft_xy] : 0;
534 top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0;
535 topright_type= h->slice_table[topright_xy] < 255 ? s->current_picture.mb_type[topright_xy]: 0;
536 left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
537 left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
538 }else{
539 topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
540 top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
541 topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
542 left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
543 left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
544 }
545
546 if(IS_INTRA(mb_type)){
547 h->topleft_samples_available=
548 h->top_samples_available=
549 h->left_samples_available= 0xFFFF;
550 h->topright_samples_available= 0xEEEA;
551
552 if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
553 h->topleft_samples_available= 0xB3FF;
554 h->top_samples_available= 0x33FF;
555 h->topright_samples_available= 0x26EA;
556 }
557 for(i=0; i<2; i++){
558 if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
559 h->topleft_samples_available&= 0xDF5F;
560 h->left_samples_available&= 0x5F5F;
561 }
562 }
563
564 if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
565 h->topleft_samples_available&= 0x7FFF;
566
567 if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
568 h->topright_samples_available&= 0xFBFF;
569
570 if(IS_INTRA4x4(mb_type)){
571 if(IS_INTRA4x4(top_type)){
572 h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
573 h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
574 h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
575 h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
576 }else{
577 int pred;
578 if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred))
579 pred= -1;
580 else{
581 pred= 2;
582 }
583 h->intra4x4_pred_mode_cache[4+8*0]=
584 h->intra4x4_pred_mode_cache[5+8*0]=
585 h->intra4x4_pred_mode_cache[6+8*0]=
586 h->intra4x4_pred_mode_cache[7+8*0]= pred;
587 }
588 for(i=0; i<2; i++){
589 if(IS_INTRA4x4(left_type[i])){
590 h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
591 h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
592 }else{
593 int pred;
594 if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred))
595 pred= -1;
596 else{
597 pred= 2;
598 }
599 h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
600 h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
601 }
602 }
603 }
604 }
605
606
607 /*
608 0 . T T. T T T T
609 1 L . .L . . . .
610 2 L . .L . . . .
611 3 . T TL . . . .
612 4 L . .L . . . .
613 5 L . .. . . . .
614 */
615 //FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec)
616 if(top_type){
617 h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
618 h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
619 h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
620 h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
621
622 h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
623 h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
624
625 h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
626 h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
627
628 }else{
629 h->non_zero_count_cache[4+8*0]=
630 h->non_zero_count_cache[5+8*0]=
631 h->non_zero_count_cache[6+8*0]=
632 h->non_zero_count_cache[7+8*0]=
633
634 h->non_zero_count_cache[1+8*0]=
635 h->non_zero_count_cache[2+8*0]=
636
637 h->non_zero_count_cache[1+8*3]=
638 h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
639
640 }
641
642 for (i=0; i<2; i++) {
643 if(left_type[i]){
644 h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
645 h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
646 h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
647 h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
648 }else{
649 h->non_zero_count_cache[3+8*1 + 2*8*i]=
650 h->non_zero_count_cache[3+8*2 + 2*8*i]=
651 h->non_zero_count_cache[0+8*1 + 8*i]=
652 h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
653 }
654 }
655
656 if( h->pps.cabac ) {
657 // top_cbp
658 if(top_type) {
659 h->top_cbp = h->cbp_table[top_xy];
660 } else if(IS_INTRA(mb_type)) {
661 h->top_cbp = 0x1C0;
662 } else {
663 h->top_cbp = 0;
664 }
665 // left_cbp
666 if (left_type[0]) {
667 h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
668 } else if(IS_INTRA(mb_type)) {
669 h->left_cbp = 0x1C0;
670 } else {
671 h->left_cbp = 0;
672 }
673 if (left_type[0]) {
674 h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
675 }
676 if (left_type[1]) {
677 h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
678 }
679 }
680
681 #if 1
682 //FIXME direct mb can skip much of this
683 if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
684 int list;
685 for(list=0; list<1+(h->slice_type==B_TYPE); list++){
686 if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
687 /*if(!h->mv_cache_clean[list]){
688 memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
689 memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
690 h->mv_cache_clean[list]= 1;
691 }*/
692 continue;
693 }
694 h->mv_cache_clean[list]= 0;
695
696 if(IS_INTER(top_type)){
697 const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
698 const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
699 *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
700 *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
701 *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
702 *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
703 h->ref_cache[list][scan8[0] + 0 - 1*8]=
704 h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
705 h->ref_cache[list][scan8[0] + 2 - 1*8]=
706 h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
707 }else{
708 *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
709 *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
710 *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
711 *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
712 *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
713 }
714
715 //FIXME unify cleanup or sth
716 if(IS_INTER(left_type[0])){
717 const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
718 const int b8_xy= h->mb2b8_xy[left_xy[0]] + 1;
719 *(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]];
720 *(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]];
721 h->ref_cache[list][scan8[0] - 1 + 0*8]=
722 h->ref_cache[list][scan8[0] - 1 + 1*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0]>>1)];
723 }else{
724 *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 0*8]=
725 *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 1*8]= 0;
726 h->ref_cache[list][scan8[0] - 1 + 0*8]=
727 h->ref_cache[list][scan8[0] - 1 + 1*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
728 }
729
730 if(IS_INTER(left_type[1])){
731 const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
732 const int b8_xy= h->mb2b8_xy[left_xy[1]] + 1;
733 *(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]];
734 *(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]];
735 h->ref_cache[list][scan8[0] - 1 + 2*8]=
736 h->ref_cache[list][scan8[0] - 1 + 3*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[2]>>1)];
737 }else{
738 *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 2*8]=
739 *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 3*8]= 0;
740 h->ref_cache[list][scan8[0] - 1 + 2*8]=
741 h->ref_cache[list][scan8[0] - 1 + 3*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
742 assert((!left_type[0]) == (!left_type[1]));
743 }
744
745 if(for_deblock || (IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred))
746 continue;
747
748 if(IS_INTER(topleft_type)){
749 const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
750 const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + h->b8_stride;
751 *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
752 h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
753 }else{
754 *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
755 h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
756 }
757
758 if(IS_INTER(topright_type)){
759 const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
760 const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
761 *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
762 h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
763 }else{
764 *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
765 h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
766 }
767
768
769 h->ref_cache[list][scan8[5 ]+1] =
770 h->ref_cache[list][scan8[7 ]+1] =
771 h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewhere else)
772 h->ref_cache[list][scan8[4 ]] =
773 h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
774 *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
775 *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
776 *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
777 *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
778 *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
779
780 if( h->pps.cabac ) {
781 /* XXX beurk, Load mvd */
782 if(IS_INTER(topleft_type)){
783 const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
784 *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy];
785 }else{
786 *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 - 1*8]= 0;
787 }
788
789 if(IS_INTER(top_type)){
790 const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
791 *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
792 *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
793 *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
794 *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
795 }else{
796 *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
797 *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
798 *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
799 *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
800 }
801 if(IS_INTER(left_type[0])){
802 const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
803 *(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]];
804 *(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]];
805 }else{
806 *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
807 *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
808 }
809 if(IS_INTER(left_type[1])){
810 const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
811 *(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]];
812 *(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]];
813 }else{
814 *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
815 *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
816 }
817 *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
818 *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
819 *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
820 *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
821 *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
822
823 if(h->slice_type == B_TYPE){
824 fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
825
826 if(IS_DIRECT(top_type)){
827 *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
828 }else if(IS_8X8(top_type)){
829 int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
830 h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
831 h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
832 }else{
833 *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
834 }
835
836 //FIXME interlacing
837 if(IS_DIRECT(left_type[0])){
838 h->direct_cache[scan8[0] - 1 + 0*8]=
839 h->direct_cache[scan8[0] - 1 + 2*8]= 1;
840 }else if(IS_8X8(left_type[0])){
841 int b8_xy = h->mb2b8_xy[left_xy[0]] + 1;
842 h->direct_cache[scan8[0] - 1 + 0*8]= h->direct_table[b8_xy];
843 h->direct_cache[scan8[0] - 1 + 2*8]= h->direct_table[b8_xy + h->b8_stride];
844 }else{
845 h->direct_cache[scan8[0] - 1 + 0*8]=
846 h->direct_cache[scan8[0] - 1 + 2*8]= 0;
847 }
848 }
849 }
850 }
851 }
852 #endif
853
854 h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
855 }
856
857 static inline void write_back_intra_pred_mode(H264Context *h){
858 MpegEncContext * const s = &h->s;
859 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
860
861 h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
862 h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
863 h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
864 h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
865 h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
866 h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
867 h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
868 }
869
870 /**
871 * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
872 */
873 static inline int check_intra4x4_pred_mode(H264Context *h){
874 MpegEncContext * const s = &h->s;
875 static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
876 static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
877 int i;
878
879 if(!(h->top_samples_available&0x8000)){
880 for(i=0; i<4; i++){
881 int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
882 if(status<0){
883 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);
884 return -1;
885 } else if(status){
886 h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
887 }
888 }
889 }
890
891 if(!(h->left_samples_available&0x8000)){
892 for(i=0; i<4; i++){
893 int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
894 if(status<0){
895 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);
896 return -1;
897 } else if(status){
898 h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
899 }
900 }
901 }
902
903 return 0;
904 } //FIXME cleanup like next
905
906 /**
907 * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
908 */
909 static inline int check_intra_pred_mode(H264Context *h, int mode){
910 MpegEncContext * const s = &h->s;
911 static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
912 static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
913
914 if(mode < 0 || mode > 6) {
915 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);
916 return -1;
917 }
918
919 if(!(h->top_samples_available&0x8000)){
920 mode= top[ mode ];
921 if(mode<0){
922 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);
923 return -1;
924 }
925 }
926
927 if(!(h->left_samples_available&0x8000)){
928 mode= left[ mode ];
929 if(mode<0){
930 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);
931 return -1;
932 }
933 }
934
935 return mode;
936 }
937
938 /**
939 * gets the predicted intra4x4 prediction mode.
940 */
941 static inline int pred_intra_mode(H264Context *h, int n){
942 const int index8= scan8[n];
943 const int left= h->intra4x4_pred_mode_cache[index8 - 1];
944 const int top = h->intra4x4_pred_mode_cache[index8 - 8];
945 const int min= FFMIN(left, top);
946
947 tprintf("mode:%d %d min:%d\n", left ,top, min);
948
949 if(min<0) return DC_PRED;
950 else return min;
951 }
952
953 static inline void write_back_non_zero_count(H264Context *h){
954 MpegEncContext * const s = &h->s;
955 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
956
957 h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
958 h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
959 h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
960 h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
961 h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
962 h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
963 h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
964
965 h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
966 h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
967 h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
968
969 h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
970 h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
971 h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
972 }
973
974 /**
975 * gets the predicted number of non zero coefficients.
976 * @param n block index
977 */
978 static inline int pred_non_zero_count(H264Context *h, int n){
979 const int index8= scan8[n];
980 const int left= h->non_zero_count_cache[index8 - 1];
981 const int top = h->non_zero_count_cache[index8 - 8];
982 int i= left + top;
983
984 if(i<64) i= (i+1)>>1;
985
986 tprintf("pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
987
988 return i&31;
989 }
990
991 static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
992 const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
993
994 if(topright_ref != PART_NOT_AVAILABLE){
995 *C= h->mv_cache[list][ i - 8 + part_width ];
996 return topright_ref;
997 }else{
998 tprintf("topright MV not available\n");
999
1000 *C= h->mv_cache[list][ i - 8 - 1 ];
1001 return h->ref_cache[list][ i - 8 - 1 ];
1002 }
1003 }
1004
1005 /**
1006 * gets the predicted MV.
1007 * @param n the block index
1008 * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
1009 * @param mx the x component of the predicted motion vector
1010 * @param my the y component of the predicted motion vector
1011 */
1012 static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
1013 const int index8= scan8[n];
1014 const int top_ref= h->ref_cache[list][ index8 - 8 ];
1015 const int left_ref= h->ref_cache[list][ index8 - 1 ];
1016 const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
1017 const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
1018 const int16_t * C;
1019 int diagonal_ref, match_count;
1020
1021 assert(part_width==1 || part_width==2 || part_width==4);
1022
1023 /* mv_cache
1024 B . . A T T T T
1025 U . . L . . , .
1026 U . . L . . . .
1027 U . . L . . , .
1028 . . . L . . . .
1029 */
1030
1031 diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
1032 match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
1033 tprintf("pred_motion match_count=%d\n", match_count);
1034 if(match_count > 1){ //most common
1035 *mx= mid_pred(A[0], B[0], C[0]);
1036 *my= mid_pred(A[1], B[1], C[1]);
1037 }else if(match_count==1){
1038 if(left_ref==ref){
1039 *mx= A[0];
1040 *my= A[1];
1041 }else if(top_ref==ref){
1042 *mx= B[0];
1043 *my= B[1];
1044 }else{
1045 *mx= C[0];
1046 *my= C[1];
1047 }
1048 }else{
1049 if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
1050 *mx= A[0];
1051 *my= A[1];
1052 }else{
1053 *mx= mid_pred(A[0], B[0], C[0]);
1054 *my= mid_pred(A[1], B[1], C[1]);
1055 }
1056 }
1057
1058 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);
1059 }
1060
1061 /**
1062 * gets the directionally predicted 16x8 MV.
1063 * @param n the block index
1064 * @param mx the x component of the predicted motion vector
1065 * @param my the y component of the predicted motion vector
1066 */
1067 static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
1068 if(n==0){
1069 const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
1070 const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
1071
1072 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);
1073
1074 if(top_ref == ref){
1075 *mx= B[0];
1076 *my= B[1];
1077 return;
1078 }
1079 }else{
1080 const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
1081 const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
1082
1083 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);
1084
1085 if(left_ref == ref){
1086 *mx= A[0];
1087 *my= A[1];
1088 return;
1089 }
1090 }
1091
1092 //RARE
1093 pred_motion(h, n, 4, list, ref, mx, my);
1094 }
1095
1096 /**
1097 * gets the directionally predicted 8x16 MV.
1098 * @param n the block index
1099 * @param mx the x component of the predicted motion vector
1100 * @param my the y component of the predicted motion vector
1101 */
1102 static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
1103 if(n==0){
1104 const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
1105 const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
1106
1107 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);
1108
1109 if(left_ref == ref){
1110 *mx= A[0];
1111 *my= A[1];
1112 return;
1113 }
1114 }else{
1115 const int16_t * C;
1116 int diagonal_ref;
1117
1118 diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
1119
1120 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);
1121
1122 if(diagonal_ref == ref){
1123 *mx= C[0];
1124 *my= C[1];
1125 return;
1126 }
1127 }
1128
1129 //RARE
1130 pred_motion(h, n, 2, list, ref, mx, my);
1131 }
1132
1133 static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
1134 const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
1135 const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
1136
1137 tprintf("pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
1138
1139 if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
1140 || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
1141 || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
1142
1143 *mx = *my = 0;
1144 return;
1145 }
1146
1147 pred_motion(h, 0, 4, 0, 0, mx, my);
1148
1149 return;
1150 }
1151
1152 static inline void direct_dist_scale_factor(H264Context * const h){
1153 const int poc = h->s.current_picture_ptr->poc;
1154 const int poc1 = h->ref_list[1][0].poc;
1155 int i;
1156 for(i=0; i<h->ref_count[0]; i++){
1157 int poc0 = h->ref_list[0][i].poc;
1158 int td = clip(poc1 - poc0, -128, 127);
1159 if(td == 0 /* FIXME || pic0 is a long-term ref */){
1160 h->dist_scale_factor[i] = 256;
1161 }else{
1162 int tb = clip(poc - poc0, -128, 127);
1163 int tx = (16384 + (ABS(td) >> 1)) / td;
1164 h->dist_scale_factor[i] = clip((tb*tx + 32) >> 6, -1024, 1023);
1165 }
1166 }
1167 }
1168 static inline void direct_ref_list_init(H264Context * const h){
1169 MpegEncContext * const s = &h->s;
1170 Picture * const ref1 = &h->ref_list[1][0];
1171 Picture * const cur = s->current_picture_ptr;
1172 int list, i, j;
1173 if(cur->pict_type == I_TYPE)
1174 cur->ref_count[0] = 0;
1175 if(cur->pict_type != B_TYPE)
1176 cur->ref_count[1] = 0;
1177 for(list=0; list<2; list++){
1178 cur->ref_count[list] = h->ref_count[list];
1179 for(j=0; j<h->ref_count[list]; j++)
1180 cur->ref_poc[list][j] = h->ref_list[list][j].poc;
1181 }
1182 if(cur->pict_type != B_TYPE || h->direct_spatial_mv_pred)
1183 return;
1184 for(list=0; list<2; list++){
1185 for(i=0; i<ref1->ref_count[list]; i++){
1186 const int poc = ref1->ref_poc[list][i];
1187 h->map_col_to_list0[list][i] = PART_NOT_AVAILABLE;
1188 for(j=0; j<h->ref_count[list]; j++)
1189 if(h->ref_list[list][j].poc == poc){
1190 h->map_col_to_list0[list][i] = j;
1191 break;
1192 }
1193 }
1194 }
1195 }
1196
1197 static inline void pred_direct_motion(H264Context * const h, int *mb_type){
1198 MpegEncContext * const s = &h->s;
1199 const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
1200 const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
1201 const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
1202 const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
1203 const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
1204 const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
1205 const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy];
1206 const int is_b8x8 = IS_8X8(*mb_type);
1207 int sub_mb_type;
1208 int i8, i4;
1209
1210 if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
1211 /* FIXME save sub mb types from previous frames (or derive from MVs)
1212 * so we know exactly what block size to use */
1213 sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
1214 *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
1215 }else if(!is_b8x8 && (IS_16X16(mb_type_col) || IS_INTRA(mb_type_col))){
1216 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
1217 *mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
1218 }else{
1219 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
1220 *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
1221 }
1222 if(!is_b8x8)
1223 *mb_type |= MB_TYPE_DIRECT2;
1224
1225 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);
1226
1227 if(h->direct_spatial_mv_pred){
1228 int ref[2];
1229 int mv[2][2];
1230 int list;
1231
1232 /* ref = min(neighbors) */
1233 for(list=0; list<2; list++){
1234 int refa = h->ref_cache[list][scan8[0] - 1];
1235 int refb = h->ref_cache[list][scan8[0] - 8];
1236 int refc = h->ref_cache[list][scan8[0] - 8 + 4];
1237 if(refc == -2)
1238 refc = h->ref_cache[list][scan8[0] - 8 - 1];
1239 ref[list] = refa;
1240 if(ref[list] < 0 || (refb < ref[list] && refb >= 0))
1241 ref[list] = refb;
1242 if(ref[list] < 0 || (refc < ref[list] && refc >= 0))
1243 ref[list] = refc;
1244 if(ref[list] < 0)
1245 ref[list] = -1;
1246 }
1247
1248 if(ref[0] < 0 && ref[1] < 0){
1249 ref[0] = ref[1] = 0;
1250 mv[0][0] = mv[0][1] =
1251 mv[1][0] = mv[1][1] = 0;
1252 }else{
1253 for(list=0; list<2; list++){
1254 if(ref[list] >= 0)
1255 pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
1256 else
1257 mv[list][0] = mv[list][1] = 0;
1258 }
1259 }
1260
1261 if(ref[1] < 0){
1262 *mb_type &= ~MB_TYPE_P0L1;
1263 sub_mb_type &= ~MB_TYPE_P0L1;
1264 }else if(ref[0] < 0){
1265 *mb_type &= ~MB_TYPE_P0L0;
1266 sub_mb_type &= ~MB_TYPE_P0L0;
1267 }
1268
1269 if(IS_16X16(*mb_type)){
1270 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref[0], 1);
1271 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, ref[1], 1);
1272 if(!IS_INTRA(mb_type_col) && l1ref0[0] == 0 &&
1273 ABS(l1mv0[0][0]) <= 1 && ABS(l1mv0[0][1]) <= 1){
1274 if(ref[0] > 0)
1275 fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
1276 else
1277 fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
1278 if(ref[1] > 0)
1279 fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
1280 else
1281 fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
1282 }else{
1283 fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
1284 fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
1285 }
1286 }else{
1287 for(i8=0; i8<4; i8++){
1288 const int x8 = i8&1;
1289 const int y8 = i8>>1;
1290
1291 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
1292 continue;
1293 h->sub_mb_type[i8] = sub_mb_type;
1294
1295 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
1296 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
1297 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref[0], 1);
1298 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, ref[1], 1);
1299
1300 /* col_zero_flag */
1301 if(!IS_INTRA(mb_type_col) && l1ref0[x8 + y8*h->b8_stride] == 0){
1302 for(i4=0; i4<4; i4++){
1303 const int16_t *mv_col = l1mv0[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
1304 if(ABS(mv_col[0]) <= 1 && ABS(mv_col[1]) <= 1){
1305 if(ref[0] == 0)
1306 *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
1307 if(ref[1] == 0)
1308 *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
1309 }
1310 }
1311 }
1312 }
1313 }
1314 }else{ /* direct temporal mv pred */
1315 if(IS_16X16(*mb_type)){
1316 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
1317 if(IS_INTRA(mb_type_col)){
1318 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
1319 fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
1320 fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
1321 }else{
1322 const int ref0 = l1ref0[0] >= 0 ? h->map_col_to_list0[0][l1ref0[0]]
1323 : h->map_col_to_list0[1][l1ref1[0]];
1324 const int dist_scale_factor = h->dist_scale_factor[ref0];
1325 const int16_t *mv_col = l1mv0[0];
1326 int mv_l0[2];
1327 mv_l0[0] = (dist_scale_factor * mv_col[0] + 128) >> 8;
1328 mv_l0[1] = (dist_scale_factor * mv_col[1] + 128) >> 8;
1329 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref0, 1);
1330 fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0],mv_l0[1]), 4);
1331 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);
1332 }
1333 }else{
1334 for(i8=0; i8<4; i8++){
1335 const int x8 = i8&1;
1336 const int y8 = i8>>1;
1337 int ref0, dist_scale_factor;
1338
1339 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
1340 continue;
1341 h->sub_mb_type[i8] = sub_mb_type;
1342 if(IS_INTRA(mb_type_col)){
1343 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
1344 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
1345 fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
1346 fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
1347 continue;
1348 }
1349
1350 ref0 = l1ref0[x8 + y8*h->b8_stride];
1351 if(ref0 >= 0)
1352 ref0 = h->map_col_to_list0[0][ref0];
1353 else
1354 ref0 = h->map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]];
1355 dist_scale_factor = h->dist_scale_factor[ref0];
1356
1357 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
1358 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
1359 for(i4=0; i4<4; i4++){
1360 const int16_t *mv_col = l1mv0[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
1361 int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
1362 mv_l0[0] = (dist_scale_factor * mv_col[0] + 128) >> 8;
1363 mv_l0[1] = (dist_scale_factor * mv_col[1] + 128) >> 8;
1364 *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
1365 pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
1366 }
1367 }
1368 }
1369 }
1370 }
1371
1372 static inline void write_back_motion(H264Context *h, int mb_type){
1373 MpegEncContext * const s = &h->s;
1374 const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
1375 const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
1376 int list;
1377
1378 for(list=0; list<2; list++){
1379 int y;
1380 if(!USES_LIST(mb_type, list)){
1381 if(1){ //FIXME skip or never read if mb_type doesn't use it
1382 for(y=0; y<4; y++){
1383 *(uint64_t*)s->current_picture.motion_val[list][b_xy + 0 + y*h->b_stride]=
1384 *(uint64_t*)s->current_picture.motion_val[list][b_xy + 2 + y*h->b_stride]= 0;
1385 }
1386 if( h->pps.cabac ) {
1387 /* FIXME needed ? */
1388 for(y=0; y<4; y++){
1389 *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]=
1390 *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= 0;
1391 }
1392 }
1393 for(y=0; y<2; y++){
1394 s->current_picture.ref_index[list][b8_xy + 0 + y*h->b8_stride]=
1395 s->current_picture.ref_index[list][b8_xy + 1 + y*h->b8_stride]= LIST_NOT_USED;
1396 }
1397 }
1398 continue;
1399 }
1400
1401 for(y=0; y<4; y++){
1402 *(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];
1403 *(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];
1404 }
1405 if( h->pps.cabac ) {
1406 for(y=0; y<4; y++){
1407 *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
1408 *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
1409 }
1410 }
1411 for(y=0; y<2; y++){
1412 s->current_picture.ref_index[list][b8_xy + 0 + y*h->b8_stride]= h->ref_cache[list][scan8[0]+0 + 16*y];
1413 s->current_picture.ref_index[list][b8_xy + 1 + y*h->b8_stride]= h->ref_cache[list][scan8[0]+2 + 16*y];
1414 }
1415 }
1416
1417 if(h->slice_type == B_TYPE && h->pps.cabac){
1418 if(IS_8X8(mb_type)){
1419 h->direct_table[b8_xy+1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
1420 h->direct_table[b8_xy+0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
1421 h->direct_table[b8_xy+1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
1422 }
1423 }
1424 }
1425
1426 /**
1427 * Decodes a network abstraction layer unit.
1428 * @param consumed is the number of bytes used as input
1429 * @param length is the length of the array
1430 * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp tailing?
1431 * @returns decoded bytes, might be src+1 if no escapes
1432 */
1433 static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){
1434 int i, si, di;
1435 uint8_t *dst;
1436
1437 // src[0]&0x80; //forbidden bit
1438 h->nal_ref_idc= src[0]>>5;
1439 h->nal_unit_type= src[0]&0x1F;
1440
1441 src++; length--;
1442 #if 0
1443 for(i=0; i<length; i++)
1444 printf("%2X ", src[i]);
1445 #endif
1446 for(i=0; i+1<length; i+=2){
1447 if(src[i]) continue;
1448 if(i>0 && src[i-1]==0) i--;
1449 if(i+2<length && src[i+1]==0 && src[i+2]<=3){
1450 if(src[i+2]!=3){
1451 /* startcode, so we must be past the end */
1452 length=i;
1453 }
1454 break;
1455 }
1456 }
1457
1458 if(i>=length-1){ //no escaped 0
1459 *dst_length= length;
1460 *consumed= length+1; //+1 for the header
1461 return src;
1462 }
1463
1464 h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length);
1465 dst= h->rbsp_buffer;
1466
1467 //printf("decoding esc\n");
1468 si=di=0;
1469 while(si<length){
1470 //remove escapes (very rare 1:2^22)
1471 if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
1472 if(src[si+2]==3){ //escape
1473 dst[di++]= 0;
1474 dst[di++]= 0;
1475 si+=3;
1476 continue;
1477 }else //next start code
1478 break;
1479 }
1480
1481 dst[di++]= src[si++];
1482 }
1483
1484 *dst_length= di;
1485 *consumed= si + 1;//+1 for the header
1486 //FIXME store exact number of bits in the getbitcontext (its needed for decoding)
1487 return dst;
1488 }
1489
1490 #if 0
1491 /**
1492 * @param src the data which should be escaped
1493 * @param dst the target buffer, dst+1 == src is allowed as a special case
1494 * @param length the length of the src data
1495 * @param dst_length the length of the dst array
1496 * @returns length of escaped data in bytes or -1 if an error occured
1497 */
1498 static int encode_nal(H264Context *h, uint8_t *dst, uint8_t *src, int length, int dst_length){
1499 int i, escape_count, si, di;
1500 uint8_t *temp;
1501
1502 assert(length>=0);
1503 assert(dst_length>0);
1504
1505 dst[0]= (h->nal_ref_idc<<5) + h->nal_unit_type;
1506
1507 if(length==0) return 1;
1508
1509 escape_count= 0;
1510 for(i=0; i<length; i+=2){
1511 if(src[i]) continue;
1512 if(i>0 && src[i-1]==0)
1513 i--;
1514 if(i+2<length && src[i+1]==0 && src[i+2]<=3){
1515 escape_count++;
1516 i+=2;
1517 }
1518 }
1519
1520 if(escape_count==0){
1521 if(dst+1 != src)
1522 memcpy(dst+1, src, length);
1523 return length + 1;
1524 }
1525
1526 if(length + escape_count + 1> dst_length)
1527 return -1;
1528
1529 //this should be damn rare (hopefully)
1530
1531 h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length + escape_count);
1532 temp= h->rbsp_buffer;
1533 //printf("encoding esc\n");
1534
1535 si= 0;
1536 di= 0;
1537 while(si < length){
1538 if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
1539 temp[di++]= 0; si++;
1540 temp[di++]= 0; si++;
1541 temp[di++]= 3;
1542 temp[di++]= src[si++];
1543 }
1544 else
1545 temp[di++]= src[si++];
1546 }
1547 memcpy(dst+1, temp, length+escape_count);
1548
1549 assert(di == length+escape_count);
1550
1551 return di + 1;
1552 }
1553
1554 /**
1555 * write 1,10,100,1000,... for alignment, yes its exactly inverse to mpeg4
1556 */
1557 static void encode_rbsp_trailing(PutBitContext *pb){
1558 int length;
1559 put_bits(pb, 1, 1);
1560 length= (-put_bits_count(pb))&7;
1561 if(length) put_bits(pb, length, 0);
1562 }
1563 #endif
1564
1565 /**
1566 * identifies the exact end of the bitstream
1567 * @return the length of the trailing, or 0 if damaged
1568 */
1569 static int decode_rbsp_trailing(uint8_t *src){
1570 int v= *src;
1571 int r;
1572
1573 tprintf("rbsp trailing %X\n", v);
1574
1575 for(r=1; r<9; r++){
1576 if(v&1) return r;
1577 v>>=1;
1578 }
1579 return 0;
1580 }
1581
1582 /**
1583 * idct tranforms the 16 dc values and dequantize them.
1584 * @param qp quantization parameter
1585 */
1586 static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp){
1587 const int qmul= dequant_coeff[qp][0];
1588 #define stride 16
1589 int i;
1590 int temp[16]; //FIXME check if this is a good idea
1591 static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
1592 static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
1593
1594 //memset(block, 64, 2*256);
1595 //return;
1596 for(i=0; i<4; i++){
1597 const int offset= y_offset[i];
1598 const int z0= block[offset+stride*0] + block[offset+stride*4];
1599 const int z1= block[offset+stride*0] - block[offset+stride*4];
1600 const int z2= block[offset+stride*1] - block[offset+stride*5];
1601 const int z3= block[offset+stride*1] + block[offset+stride*5];
1602
1603 temp[4*i+0]= z0+z3;
1604 temp[4*i+1]= z1+z2;
1605 temp[4*i+2]= z1-z2;
1606 temp[4*i+3]= z0-z3;
1607 }
1608
1609 for(i=0; i<4; i++){
1610 const int offset= x_offset[i];
1611 const int z0= temp[4*0+i] + temp[4*2+i];
1612 const int z1= temp[4*0+i] - temp[4*2+i];
1613 const int z2= temp[4*1+i] - temp[4*3+i];
1614 const int z3= temp[4*1+i] + temp[4*3+i];
1615
1616 block[stride*0 +offset]= ((z0 + z3)*qmul + 2)>>2; //FIXME think about merging this into decode_resdual
1617 block[stride*2 +offset]= ((z1 + z2)*qmul + 2)>>2;
1618 block[stride*8 +offset]= ((z1 - z2)*qmul + 2)>>2;
1619 block[stride*10+offset]= ((z0 - z3)*qmul + 2)>>2;
1620 }
1621 }
1622
1623 #if 0
1624 /**
1625 * dct tranforms the 16 dc values.
1626 * @param qp quantization parameter ??? FIXME
1627 */
1628 static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
1629 // const int qmul= dequant_coeff[qp][0];
1630 int i;
1631 int temp[16]; //FIXME check if this is a good idea
1632 static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
1633 static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
1634
1635 for(i=0; i<4; i++){
1636 const int offset= y_offset[i];
1637 const int z0= block[offset+stride*0] + block[offset+stride*4];
1638 const int z1= block[offset+stride*0] - block[offset+stride*4];
1639 const int z2= block[offset+stride*1] - block[offset+stride*5];
1640 const int z3= block[offset+stride*1] + block[offset+stride*5];
1641
1642 temp[4*i+0]= z0+z3;
1643 temp[4*i+1]= z1+z2;
1644 temp[4*i+2]= z1-z2;
1645 temp[4*i+3]= z0-z3;
1646 }
1647
1648 for(i=0; i<4; i++){
1649 const int offset= x_offset[i];
1650 const int z0= temp[4*0+i] + temp[4*2+i];
1651 const int z1= temp[4*0+i] - temp[4*2+i];
1652 const int z2= temp[4*1+i] - temp[4*3+i];
1653 const int z3= temp[4*1+i] + temp[4*3+i];
1654
1655 block[stride*0 +offset]= (z0 + z3)>>1;
1656 block[stride*2 +offset]= (z1 + z2)>>1;
1657 block[stride*8 +offset]= (z1 - z2)>>1;
1658 block[stride*10+offset]= (z0 - z3)>>1;
1659 }
1660 }
1661 #endif
1662
1663 #undef xStride
1664 #undef stride
1665
1666 static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp){
1667 const int qmul= dequant_coeff[qp][0];
1668 const int stride= 16*2;
1669 const int xStride= 16;
1670 int a,b,c,d,e;
1671
1672 a= block[stride*0 + xStride*0];
1673 b= block[stride*0 + xStride*1];
1674 c= block[stride*1 + xStride*0];
1675 d= block[stride*1 + xStride*1];
1676
1677 e= a-b;
1678 a= a+b;
1679 b= c-d;
1680 c= c+d;
1681
1682 block[stride*0 + xStride*0]= ((a+c)*qmul + 0)>>1;
1683 block[stride*0 + xStride*1]= ((e+b)*qmul + 0)>>1;
1684 block[stride*1 + xStride*0]= ((a-c)*qmul + 0)>>1;
1685 block[stride*1 + xStride*1]= ((e-b)*qmul + 0)>>1;
1686 }
1687
1688 #if 0
1689 static void chroma_dc_dct_c(DCTELEM *block){
1690 const int stride= 16*2;
1691 const int xStride= 16;
1692 int a,b,c,d,e;
1693
1694 a= block[stride*0 + xStride*0];
1695 b= block[stride*0 + xStride*1];
1696 c= block[stride*1 + xStride*0];
1697 d= block[stride*1 + xStride*1];
1698
1699 e= a-b;
1700 a= a+b;
1701 b= c-d;
1702 c= c+d;
1703
1704 block[stride*0 + xStride*0]= (a+c);
1705 block[stride*0 + xStride*1]= (e+b);
1706 block[stride*1 + xStride*0]= (a-c);
1707 block[stride*1 + xStride*1]= (e-b);
1708 }
1709 #endif
1710
1711 /**
1712 * gets the chroma qp.
1713 */
1714 static inline int get_chroma_qp(int chroma_qp_index_offset, int qscale){
1715
1716 return chroma_qp[clip(qscale + chroma_qp_index_offset, 0, 51)];
1717 }
1718
1719
1720 #if 0
1721 static void h264_diff_dct_c(DCTELEM *block, uint8_t *src1, uint8_t *src2, int stride){
1722 int i;
1723 //FIXME try int temp instead of block
1724
1725 for(i=0; i<4; i++){
1726 const int d0= src1[0 + i*stride] - src2[0 + i*stride];
1727 const int d1= src1[1 + i*stride] - src2[1 + i*stride];
1728 const int d2= src1[2 + i*stride] - src2[2 + i*stride];
1729 const int d3= src1[3 + i*stride] - src2[3 + i*stride];
1730 const int z0= d0 + d3;
1731 const int z3= d0 - d3;
1732 const int z1= d1 + d2;
1733 const int z2= d1 - d2;
1734
1735 block[0 + 4*i]= z0 + z1;
1736 block[1 + 4*i]= 2*z3 + z2;
1737 block[2 + 4*i]= z0 - z1;
1738 block[3 + 4*i]= z3 - 2*z2;
1739 }
1740
1741 for(i=0; i<4; i++){
1742 const int z0= block[0*4 + i] + block[3*4 + i];
1743 const int z3= block[0*4 + i] - block[3*4 + i];
1744 const int z1= block[1*4 + i] + block[2*4 + i];
1745 const int z2= block[1*4 + i] - block[2*4 + i];
1746
1747 block[0*4 + i]= z0 + z1;
1748 block[1*4 + i]= 2*z3 + z2;
1749 block[2*4 + i]= z0 - z1;
1750 block[3*4 + i]= z3 - 2*z2;
1751 }
1752 }
1753 #endif
1754
1755 //FIXME need to check that this doesnt overflow signed 32 bit for low qp, i am not sure, it's very close
1756 //FIXME check that gcc inlines this (and optimizes intra & seperate_dc stuff away)
1757 static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int seperate_dc){
1758 int i;
1759 const int * const quant_table= quant_coeff[qscale];
1760 const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
1761 const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
1762 const unsigned int threshold2= (threshold1<<1);
1763 int last_non_zero;
1764
1765 if(seperate_dc){
1766 if(qscale<=18){
1767 //avoid overflows
1768 const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
1769 const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
1770 const unsigned int dc_threshold2= (dc_threshold1<<1);
1771
1772 int level= block[0]*quant_coeff[qscale+18][0];
1773 if(((unsigned)(level+dc_threshold1))>dc_threshold2){
1774 if(level>0){
1775 level= (dc_bias + level)>>(QUANT_SHIFT-2);
1776 block[0]= level;
1777 }else{
1778 level= (dc_bias - level)>>(QUANT_SHIFT-2);
1779 block[0]= -level;
1780 }
1781 // last_non_zero = i;
1782 }else{
1783 block[0]=0;
1784 }
1785 }else{
1786 const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
1787 const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
1788 const unsigned int dc_threshold2= (dc_threshold1<<1);
1789
1790 int level= block[0]*quant_table[0];
1791 if(((unsigned)(level+dc_threshold1))>dc_threshold2){
1792 if(level>0){
1793 level= (dc_bias + level)>>(QUANT_SHIFT+1);
1794 block[0]= level;
1795 }else{
1796 level= (dc_bias - level)>>(QUANT_SHIFT+1);
1797 block[0]= -level;
1798 }
1799 // last_non_zero = i;
1800 }else{
1801 block[0]=0;
1802 }
1803 }
1804 last_non_zero= 0;
1805 i=1;
1806 }else{
1807 last_non_zero= -1;
1808 i=0;
1809 }
1810
1811 for(; i<16; i++){
1812 const int j= scantable[i];
1813 int level= block[j]*quant_table[j];
1814
1815 // if( bias+level >= (1<<(QMAT_SHIFT - 3))
1816 // || bias-level >= (1<<(QMAT_SHIFT - 3))){
1817 if(((unsigned)(level+threshold1))>threshold2){
1818 if(level>0){
1819 level= (bias + level)>>QUANT_SHIFT;
1820 block[j]= level;
1821 }else{
1822 level= (bias - level)>>QUANT_SHIFT;
1823 block[j]= -level;
1824 }
1825 last_non_zero = i;
1826 }else{
1827 block[j]=0;
1828 }
1829 }
1830
1831 return last_non_zero;
1832 }
1833
1834 static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){
1835 const uint32_t a= ((uint32_t*)(src-stride))[0];
1836 ((uint32_t*)(src+0*stride))[0]= a;
1837 ((uint32_t*)(src+1*stride))[0]= a;
1838 ((uint32_t*)(src+2*stride))[0]= a;
1839 ((uint32_t*)(src+3*stride))[0]= a;
1840 }
1841
1842 static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){
1843 ((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101;
1844 ((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101;
1845 ((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101;
1846 ((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101;
1847 }
1848
1849 static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride){
1850 const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
1851 + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
1852
1853 ((uint32_t*)(src+0*stride))[0]=
1854 ((uint32_t*)(src+1*stride))[0]=
1855 ((uint32_t*)(src+2*stride))[0]=
1856 ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
1857 }
1858
1859 static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride){
1860 const int dc= ( src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
1861
1862 ((uint32_t*)(src+0*stride))[0]=
1863 ((uint32_t*)(src+1*stride))[0]=
1864 ((uint32_t*)(src+2*stride))[0]=
1865 ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
1866 }
1867
1868 static void pred4x4_top_dc_c(uint8_t *src, uint8_t *topright, int stride){
1869 const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2;
1870
1871 ((uint32_t*)(src+0*stride))[0]=
1872 ((uint32_t*)(src+1*stride))[0]=
1873 ((uint32_t*)(src+2*stride))[0]=
1874 ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
1875 }
1876
1877 static void pred4x4_128_dc_c(uint8_t *src, uint8_t *topright, int stride){
1878 ((uint32_t*)(src+0*stride))[0]=
1879 ((uint32_t*)(src+1*stride))[0]=
1880 ((uint32_t*)(src+2*stride))[0]=
1881 ((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U;
1882 }
1883
1884
1885 #define LOAD_TOP_RIGHT_EDGE\
1886 const int t4= topright[0];\
1887 const int t5= topright[1];\
1888 const int t6= topright[2];\
1889 const int t7= topright[3];\
1890
1891 #define LOAD_LEFT_EDGE\
1892 const int l0= src[-1+0*stride];\
1893 const int l1= src[-1+1*stride];\
1894 const int l2= src[-1+2*stride];\
1895 const int l3= src[-1+3*stride];\
1896
1897 #define LOAD_TOP_EDGE\
1898 const int t0= src[ 0-1*stride];\
1899 const int t1= src[ 1-1*stride];\
1900 const int t2= src[ 2-1*stride];\
1901 const int t3= src[ 3-1*stride];\
1902
1903 static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){
1904 const int lt= src[-1-1*stride];
1905 LOAD_TOP_EDGE
1906 LOAD_LEFT_EDGE
1907
1908 src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
1909 src[0+2*stride]=
1910 src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
1911 src[0+1*stride]=
1912 src[1+2*stride]=
1913 src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
1914 src[0+0*stride]=
1915 src[1+1*stride]=
1916 src[2+2*stride]=
1917 src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
1918 src[1+0*stride]=
1919 src[2+1*stride]=
1920 src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
1921 src[2+0*stride]=
1922 src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
1923 src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
1924 }
1925
1926 static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){
1927 LOAD_TOP_EDGE
1928 LOAD_TOP_RIGHT_EDGE
1929 // LOAD_LEFT_EDGE
1930
1931 src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
1932 src[1+0*stride]=
1933 src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
1934 src[2+0*stride]=
1935 src[1+1*stride]=
1936 src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
1937 src[3+0*stride]=
1938 src[2+1*stride]=
1939 src[1+2*stride]=
1940 src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
1941 src[3+1*stride]=
1942 src[2+2*stride]=
1943 src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
1944 src[3+2*stride]=
1945 src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
1946 src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
1947 }
1948
1949 static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
1950 const int lt= src[-1-1*stride];
1951 LOAD_TOP_EDGE
1952 LOAD_LEFT_EDGE
1953 const __attribute__((unused)) int unu= l3;
1954
1955 src[0+0*stride]=
1956 src[1+2*stride]=(lt + t0 + 1)>>1;
1957 src[1+0*stride]=
1958 src[2+2*stride]=(t0 + t1 + 1)>>1;
1959 src[2+0*stride]=
1960 src[3+2*stride]=(t1 + t2 + 1)>>1;
1961 src[3+0*stride]=(t2 + t3 + 1)>>1;
1962 src[0+1*stride]=
1963 src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
1964 src[1+1*stride]=
1965 src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
1966 src[2+1*stride]=
1967 src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
1968 src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
1969 src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
1970 src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
1971 }
1972
1973 static void pred4x4_vertical_left_c(uint8_t *src, uint8_t *topright, int stride){
1974 LOAD_TOP_EDGE
1975 LOAD_TOP_RIGHT_EDGE
1976 const __attribute__((unused)) int unu= t7;
1977
1978 src[0+0*stride]=(t0 + t1 + 1)>>1;
1979 src[1+0*stride]=
1980 src[0+2*stride]=(t1 + t2 + 1)>>1;
1981 src[2+0*stride]=
1982 src[1+2*stride]=(t2 + t3 + 1)>>1;
1983 src[3+0*stride]=
1984 src[2+2*stride]=(t3 + t4+ 1)>>1;
1985 src[3+2*stride]=(t4 + t5+ 1)>>1;
1986 src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
1987 src[1+1*stride]=
1988 src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
1989 src[2+1*stride]=
1990 src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
1991 src[3+1*stride]=
1992 src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
1993 src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
1994 }
1995
1996 static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){
1997 LOAD_LEFT_EDGE
1998
1999 src[0+0*stride]=(l0 + l1 + 1)>>1;
2000 src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
2001 src[2+0*stride]=
2002 src[0+1*stride]=(l1 + l2 + 1)>>1;
2003 src[3+0*stride]=
2004 src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
2005 src[2+1*stride]=
2006 src[0+2*stride]=(l2 + l3 + 1)>>1;
2007 src[3+1*stride]=
2008 src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
2009 src[3+2*stride]=
2010 src[1+3*stride]=
2011 src[0+3*stride]=
2012 src[2+2*stride]=
2013 src[2+3*stride]=
2014 src[3+3*stride]=l3;
2015 }
2016
2017 static void pred4x4_horizontal_down_c(uint8_t *src, uint8_t *topright, int stride){
2018 const int lt= src[-1-1*stride];
2019 LOAD_TOP_EDGE
2020 LOAD_LEFT_EDGE
2021 const __attribute__((unused)) int unu= t3;
2022
2023 src[0+0*stride]=
2024 src[2+1*stride]=(lt + l0 + 1)>>1;
2025 src[1+0*stride]=
2026 src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
2027 src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
2028 src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
2029 src[0+1*stride]=
2030 src[2+2*stride]=(l0 + l1 + 1)>>1;
2031 src[1+1*stride]=
2032 src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
2033 src[0+2*stride]=
2034 src[2+3*stride]=(l1 + l2+ 1)>>1;
2035 src[1+2*stride]=
2036 src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
2037 src[0+3*stride]=(l2 + l3 + 1)>>1;
2038 src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
2039 }
2040
2041 static void pred16x16_vertical_c(uint8_t *src, int stride){
2042 int i;
2043 const uint32_t a= ((uint32_t*)(src-stride))[0];
2044 const uint32_t b= ((uint32_t*)(src-stride))[1];
2045 const uint32_t c= ((uint32_t*)(src-stride))[2];
2046 const uint32_t d= ((uint32_t*)(src-stride))[3];
2047
2048 for(i=0; i<16; i++){
2049 ((uint32_t*)(src+i*stride))[0]= a;
2050 ((uint32_t*)(src+i*stride))[1]= b;
2051 ((uint32_t*)(src+i*stride))[2]= c;
2052 ((uint32_t*)(src+i*stride))[3]= d;
2053 }
2054 }
2055
2056 static void pred16x16_horizontal_c(uint8_t *src, int stride){
2057 int i;
2058
2059 for(i=0; i<16; i++){
2060 ((uint32_t*)(src+i*stride))[0]=
2061 ((uint32_t*)(src+i*stride))[1]=
2062 ((uint32_t*)(src+i*stride))[2]=
2063 ((uint32_t*)(src+i*stride))[3]= src[-1+i*stride]*0x01010101;
2064 }
2065 }
2066
2067 static void pred16x16_dc_c(uint8_t *src, int stride){
2068 int i, dc=0;
2069
2070 for(i=0;i<16; i++){
2071 dc+= src[-1+i*stride];
2072 }
2073
2074 for(i=0;i<16; i++){
2075 dc+= src[i-stride];
2076 }
2077
2078 dc= 0x01010101*((dc + 16)>>5);
2079
2080 for(i=0; i<16; i++){
2081 ((uint32_t*)(src+i*stride))[0]=
2082 ((uint32_t*)(src+i*stride))[1]=
2083 ((uint32_t*)(src+i*stride))[2]=
2084 ((uint32_t*)(src+i*stride))[3]= dc;
2085 }
2086 }
2087
2088 static void pred16x16_left_dc_c(uint8_t *src, int stride){
2089 int i, dc=0;
2090
2091 for(i=0;i<16; i++){
2092 dc+= src[-1+i*stride];
2093 }
2094
2095 dc= 0x01010101*((dc + 8)>>4);
2096
2097 for(i=0; i<16; i++){
2098 ((uint32_t*)(src+i*stride))[0]=
2099 ((uint32_t*)(src+i*stride))[1]=
2100 ((uint32_t*)(src+i*stride))[2]=
2101 ((uint32_t*)(src+i*stride))[3]= dc;
2102 }
2103 }
2104
2105 static void pred16x16_top_dc_c(uint8_t *src, int stride){
2106 int i, dc=0;
2107
2108 for(i=0;i<16; i++){
2109 dc+= src[i-stride];
2110 }
2111 dc= 0x01010101*((dc + 8)>>4);
2112
2113 for(i=0; i<16; i++){
2114 ((uint32_t*)(src+i*stride))[0]=
2115 ((uint32_t*)(src+i*stride))[1]=
2116 ((uint32_t*)(src+i*stride))[2]=
2117 ((uint32_t*)(src+i*stride))[3]= dc;
2118 }
2119 }
2120
2121 static void pred16x16_128_dc_c(uint8_t *src, int stride){
2122 int i;
2123
2124 for(i=0; i<16; i++){
2125 ((uint32_t*)(src+i*stride))[0]=
2126 ((uint32_t*)(src+i*stride))[1]=
2127 ((uint32_t*)(src+i*stride))[2]=
2128 ((uint32_t*)(src+i*stride))[3]= 0x01010101U*128U;
2129 }
2130 }
2131
2132 static inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){
2133 int i, j, k;
2134 int a;
2135 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2136 const uint8_t * const src0 = src+7-stride;
2137 const uint8_t *src1 = src+8*stride-1;
2138 const uint8_t *src2 = src1-2*stride; // == src+6*stride-1;
2139 int H = src0[1] - src0[-1];
2140 int V = src1[0] - src2[ 0];
2141 for(k=2; k<=8; ++k) {
2142 src1 += stride; src2 -= stride;
2143 H += k*(src0[k] - src0[-k]);
2144 V += k*(src1[0] - src2[ 0]);
2145 }
2146 if(svq3){
2147 H = ( 5*(H/4) ) / 16;
2148 V = ( 5*(V/4) ) / 16;
2149
2150 /* required for 100% accuracy */
2151 i = H; H = V; V = i;
2152 }else{
2153 H = ( 5*H+32 ) >> 6;
2154 V = ( 5*V+32 ) >> 6;
2155 }
2156
2157 a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
2158 for(j=16; j>0; --j) {
2159 int b = a;
2160 a += V;
2161 for(i=-16; i<0; i+=4) {
2162 src[16+i] = cm[ (b ) >> 5 ];
2163 src[17+i] = cm[ (b+ H) >> 5 ];
2164 src[18+i] = cm[ (b+2*H) >> 5 ];
2165 src[19+i] = cm[ (b+3*H) >> 5 ];
2166 b += 4*H;
2167 }
2168 src += stride;
2169 }
2170 }
2171
2172 static void pred16x16_plane_c(uint8_t *src, int stride){
2173 pred16x16_plane_compat_c(src, stride, 0);
2174 }
2175
2176 static void pred8x8_vertical_c(uint8_t *src, int stride){
2177 int i;
2178 const uint32_t a= ((uint32_t*)(src-stride))[0];
2179 const uint32_t b= ((uint32_t*)(src-stride))[1];
2180
2181 for(i=0; i<8; i++){
2182 ((uint32_t*)(src+i*stride))[0]= a;
2183 ((uint32_t*)(src+i*stride))[1]= b;
2184 }
2185 }
2186
2187 static void pred8x8_horizontal_c(uint8_t *src, int stride){
2188 int i;
2189
2190 for(i=0; i<8; i++){
2191 ((uint32_t*)(src+i*stride))[0]=
2192 ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101;
2193 }
2194 }
2195
2196 static void pred8x8_128_dc_c(uint8_t *src, int stride){
2197 int i;
2198
2199 for(i=0; i<8; i++){
2200 ((uint32_t*)(src+i*stride))[0]=
2201 ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
2202 }
2203 }
2204
2205 static void pred8x8_left_dc_c(uint8_t *src, int stride){
2206 int i;
2207 int dc0, dc2;
2208
2209 dc0=dc2=0;
2210 for(i=0;i<4; i++){
2211 dc0+= src[-1+i*stride];
2212 dc2+= src[-1+(i+4)*stride];
2213 }
2214 dc0= 0x01010101*((dc0 + 2)>>2);
2215 dc2= 0x01010101*((dc2 + 2)>>2);
2216
2217 for(i=0; i<4; i++){
2218 ((uint32_t*)(src+i*stride))[0]=
2219 ((uint32_t*)(src+i*stride))[1]= dc0;
2220 }
2221 for(i=4; i<8; i++){
2222 ((uint32_t*)(src+i*stride))[0]=
2223 ((uint32_t*)(src+i*stride))[1]= dc2;
2224 }
2225 }
2226
2227 static void pred8x8_top_dc_c(uint8_t *src, int stride){
2228 int i;
2229 int dc0, dc1;
2230
2231 dc0=dc1=0;
2232 for(i=0;i<4; i++){
2233 dc0+= src[i-stride];
2234 dc1+= src[4+i-stride];
2235 }
2236 dc0= 0x01010101*((dc0 + 2)>>2);
2237 dc1= 0x01010101*((dc1 + 2)>>2);
2238
2239 for(i=0; i<4; i++){
2240 ((uint32_t*)(src+i*stride))[0]= dc0;
2241 ((uint32_t*)(src+i*stride))[1]= dc1;
2242 }
2243 for(i=4; i<8; i++){
2244 ((uint32_t*)(src+i*stride))[0]= dc0;
2245 ((uint32_t*)(src+i*stride))[1]= dc1;
2246 }
2247 }
2248
2249
2250 static void pred8x8_dc_c(uint8_t *src, int stride){
2251 int i;
2252 int dc0, dc1, dc2, dc3;
2253
2254 dc0=dc1=dc2=0;
2255 for(i=0;i<4; i++){
2256 dc0+= src[-1+i*stride] + src[i-stride];
2257 dc1+= src[4+i-stride];
2258 dc2+= src[-1+(i+4)*stride];
2259 }
2260 dc3= 0x01010101*((dc1 + dc2 + 4)>>3);
2261 dc0= 0x01010101*((dc0 + 4)>>3);
2262 dc1= 0x01010101*((dc1 + 2)>>2);
2263 dc2= 0x01010101*((dc2 + 2)>>2);
2264
2265 for(i=0; i<4; i++){
2266 ((uint32_t*)(src+i*stride))[0]= dc0;
2267 ((uint32_t*)(src+i*stride))[1]= dc1;
2268 }
2269 for(i=4; i<8; i++){
2270 ((uint32_t*)(src+i*stride))[0]= dc2;
2271 ((uint32_t*)(src+i*stride))[1]= dc3;
2272 }
2273 }
2274
2275 static void pred8x8_plane_c(uint8_t *src, int stride){
2276 int j, k;
2277 int a;
2278 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2279 const uint8_t * const src0 = src+3-stride;
2280 const uint8_t *src1 = src+4*stride-1;
2281 const uint8_t *src2 = src1-2*stride; // == src+2*stride-1;
2282 int H = src0[1] - src0[-1];
2283 int V = src1[0] - src2[ 0];
2284 for(k=2; k<=4; ++k) {
2285 src1 += stride; src2 -= stride;
2286 H += k*(src0[k] - src0[-k]);
2287 V += k*(src1[0] - src2[ 0]);
2288 }
2289 H = ( 17*H+16 ) >> 5;
2290 V = ( 17*V+16 ) >> 5;
2291
2292 a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
2293 for(j=8; j>0; --j) {
2294 int b = a;
2295 a += V;
2296 src[0] = cm[ (b ) >> 5 ];
2297 src[1] = cm[ (b+ H) >> 5 ];
2298 src[2] = cm[ (b+2*H) >> 5 ];
2299 src[3] = cm[ (b+3*H) >> 5 ];
2300 src[4] = cm[ (b+4*H) >> 5 ];
2301 src[5] = cm[ (b+5*H) >> 5 ];
2302 src[6] = cm[ (b+6*H) >> 5 ];
2303 src[7] = cm[ (b+7*H) >> 5 ];
2304 src += stride;
2305 }
2306 }
2307
2308 #define SRC(x,y) src[(x)+(y)*stride]
2309 #define PL(y) \
2310 const int l##y = (SRC(-1,y-1) + 2*SRC(-1,y) + SRC(-1,y+1) + 2) >> 2;
2311 #define PREDICT_8x8_LOAD_LEFT \
2312 const int l0 = ((has_topleft ? SRC(-1,-1) : SRC(-1,0)) \
2313 + 2*SRC(-1,0) + SRC(-1,1) + 2) >> 2; \
2314 PL(1) PL(2) PL(3) PL(4) PL(5) PL(6) \
2315 const int l7 = (SRC(-1,6) + 3*SRC(-1,7) + 2) >> 2;
2316
2317 #define PT(x) \
2318 const int t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
2319 #define PREDICT_8x8_LOAD_TOP \
2320 const int t0 = ((has_topleft ? SRC(-1,-1) : SRC(0,-1)) \
2321 + 2*SRC(0,-1) + SRC(1,-1) + 2) >> 2; \
2322 PT(1) PT(2) PT(3) PT(4) PT(5) PT(6) \
2323 const int t7 = ((has_topright ? SRC(8,-1) : SRC(7,-1)) \
2324 + 2*SRC(7,-1) + SRC(6,-1) + 2) >> 2; \
2325
2326 #define PTR(x) \
2327 t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
2328 #define PREDICT_8x8_LOAD_TOPRIGHT \
2329 int t8, t9, t10, t11, t12, t13, t14, t15; \
2330 if(has_topright) { \
2331 PTR(8) PTR(9) PTR(10) PTR(11) PTR(12) PTR(13) PTR(14) \
2332 t15 = (SRC(14,-1) + 3*SRC(15,-1) + 2) >> 2; \
2333 } else t8=t9=t10=t11=t12=t13=t14=t15= SRC(7,-1);
2334
2335 #define PREDICT_8x8_LOAD_TOPLEFT \
2336 const int lt = (SRC(-1,0) + 2*SRC(-1,-1) + SRC(0,-1) + 2) >> 2;
2337
2338 #define PREDICT_8x8_DC(v) \
2339 int y; \
2340 for( y = 0; y < 8; y++ ) { \
2341 ((uint32_t*)src)[0] = \
2342 ((uint32_t*)src)[1] = v; \
2343 src += stride; \
2344 }
2345
2346 static void pred8x8l_128_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
2347 {
2348 PREDICT_8x8_DC(0x80808080);
2349 }
2350 static void pred8x8l_left_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
2351 {
2352 PREDICT_8x8_LOAD_LEFT;
2353 const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7+4) >> 3) * 0x01010101;
2354 PREDICT_8x8_DC(dc);
2355 }
2356 static void pred8x8l_top_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
2357 {
2358 PREDICT_8x8_LOAD_TOP;
2359 const uint32_t dc = ((t0+t1+t2+t3+t4+t5+t6+t7+4) >> 3) * 0x01010101;
2360 PREDICT_8x8_DC(dc);
2361 }
2362 static void pred8x8l_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
2363 {
2364 PREDICT_8x8_LOAD_LEFT;
2365 PREDICT_8x8_LOAD_TOP;
2366 const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7
2367 +t0+t1+t2+t3+t4+t5+t6+t7+8) >> 4) * 0x01010101;
2368 PREDICT_8x8_DC(dc);
2369 }
2370 static void pred8x8l_horizontal_c(uint8_t *src, int has_topleft, int has_topright, int stride)
2371 {
2372 PREDICT_8x8_LOAD_LEFT;
2373 #define ROW(y) ((uint32_t*)(src+y*stride))[0] =\
2374 ((uint32_t*)(src+y*stride))[1] = 0x01010101 * l##y
2375 ROW(0); ROW(1); ROW(2); ROW(3); ROW(4); ROW(5); ROW(6); ROW(7);
2376 #undef ROW
2377 }
2378 static void pred8x8l_vertical_c(uint8_t *src, int has_topleft, int has_topright, int stride)
2379 {
2380 int y;
2381 PREDICT_8x8_LOAD_TOP;
2382 src[0] = t0;
2383 src[1] = t1;
2384 src[2] = t2;
2385 src[3] = t3;
2386 src[4] = t4;
2387 src[5] = t5;
2388 src[6] = t6;
2389 src[7] = t7;
2390 for( y = 1; y < 8; y++ )
2391 *(uint64_t*)(src+y*stride) = *(uint64_t*)src;
2392 }
2393 static void pred8x8l_down_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
2394 {
2395 PREDICT_8x8_LOAD_TOP;
2396 PREDICT_8x8_LOAD_TOPRIGHT;
2397 SRC(0,0)= (t0 + 2*t1 + t2 + 2) >> 2;
2398 SRC(0,1)=SRC(1,0)= (t1 + 2*t2 + t3 + 2) >> 2;
2399 SRC(0,2)=SRC(1,1)=SRC(2,0)= (t2 + 2*t3 + t4 + 2) >> 2;
2400 SRC(0,3)=SRC(1,2)=SRC(2,1)=SRC(3,0)= (t3 + 2*t4 + t5 + 2) >> 2;
2401 SRC(0,4)=SRC(1,3)=SRC(2,2)=SRC(3,1)=SRC(4,0)= (t4 + 2*t5 + t6 + 2) >> 2;
2402 SRC(0,5)=SRC(1,4)=SRC(2,3)=SRC(3,2)=SRC(4,1)=SRC(5,0)= (t5 + 2*t6 + t7 + 2) >> 2;
2403 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;
2404 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;
2405 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;
2406 SRC(2,7)=SRC(3,6)=SRC(4,5)=SRC(5,4)=SRC(6,3)=SRC(7,2)= (t9 + 2*t10 + t11 + 2) >> 2;
2407 SRC(3,7)=SRC(4,6)=SRC(5,5)=SRC(6,4)=SRC(7,3)= (t10 + 2*t11 + t12 + 2) >> 2;
2408 SRC(4,7)=SRC(5,6)=SRC(6,5)=SRC(7,4)= (t11 + 2*t12 + t13 + 2) >> 2;
2409 SRC(5,7)=SRC(6,6)=SRC(7,5)= (t12 + 2*t13 + t14 + 2) >> 2;
2410 SRC(6,7)=SRC(7,6)= (t13 + 2*t14 + t15 + 2) >> 2;
2411 SRC(7,7)= (t14 + 3*t15 + 2) >> 2;
2412 }
2413 static void pred8x8l_down_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
2414 {
2415 PREDICT_8x8_LOAD_TOP;
2416 PREDICT_8x8_LOAD_LEFT;
2417 PREDICT_8x8_LOAD_TOPLEFT;
2418 SRC(0,7)= (l7 + 2*l6 + l5 + 2) >> 2;
2419 SRC(0,6)=SRC(1,7)= (l6 + 2*l5 + l4 + 2) >> 2;
2420 SRC(0,5)=SRC(1,6)=SRC(2,7)= (l5 + 2*l4 + l3 + 2) >> 2;
2421 SRC(0,4)=SRC(1,5)=SRC(2,6)=SRC(3,7)= (l4 + 2*l3 + l2 + 2) >> 2;
2422 SRC(0,3)=SRC(1,4)=SRC(2,5)=SRC(3,6)=SRC(4,7)= (l3 + 2*l2 + l1 + 2) >> 2;
2423 SRC(0,2)=SRC(1,3)=SRC(2,4)=SRC(3,5)=SRC(4,6)=SRC(5,7)= (l2 + 2*l1 + l0 + 2) >> 2;
2424 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;
2425 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;
2426 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;
2427 SRC(2,0)=SRC(3,1)=SRC(4,2)=SRC(5,3)=SRC(6,4)=SRC(7,5)= (t0 + 2*t1 + t2 + 2) >> 2;
2428 SRC(3,0)=SRC(4,1)=SRC(5,2)=SRC(6,3)=SRC(7,4)= (t1 + 2*t2 + t3 + 2) >> 2;
2429 SRC(4,0)=SRC(5,1)=SRC(6,2)=SRC(7,3)= (t2 + 2*t3 + t4 + 2) >> 2;
2430 SRC(5,0)=SRC(6,1)=SRC(7,2)= (t3 + 2*t4 + t5 + 2) >> 2;
2431 SRC(6,0)=SRC(7,1)= (t4 + 2*t5 + t6 + 2) >> 2;
2432 SRC(7,0)= (t5 + 2*t6 + t7 + 2) >> 2;
2433
2434 }
2435 static void pred8x8l_vertical_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
2436 {
2437 PREDICT_8x8_LOAD_TOP;
2438 PREDICT_8x8_LOAD_LEFT;
2439 PREDICT_8x8_LOAD_TOPLEFT;
2440 SRC(0,6)= (l5 + 2*l4 + l3 + 2) >> 2;
2441 SRC(0,7)= (l6 + 2*l5 + l4 + 2) >> 2;
2442 SRC(0,4)=SRC(1,6)= (l3 + 2*l2 + l1 + 2) >> 2;
2443 SRC(0,5)=SRC(1,7)= (l4 + 2*l3 + l2 + 2) >> 2;
2444 SRC(0,2)=SRC(1,4)=SRC(2,6)= (l1 + 2*l0 + lt + 2) >> 2;
2445 SRC(0,3)=SRC(1,5)=SRC(2,7)= (l2 + 2*l1 + l0 + 2) >> 2;
2446 SRC(0,1)=SRC(1,3)=SRC(2,5)=SRC(3,7)= (l0 + 2*lt + t0 + 2) >> 2;
2447 SRC(0,0)=SRC(1,2)=SRC(2,4)=SRC(3,6)= (lt + t0 + 1) >> 1;
2448 SRC(1,1)=SRC(2,3)=SRC(3,5)=SRC(4,7)= (lt + 2*t0 + t1 + 2) >> 2;
2449 SRC(1,0)=SRC(2,2)=SRC(3,4)=SRC(4,6)= (t0 + t1 + 1) >> 1;
2450 SRC(2,1)=SRC(3,3)=SRC(4,5)=SRC(5,7)= (t0 + 2*t1 + t2 + 2) >> 2;
2451 SRC(2,0)=SRC(3,2)=SRC(4,4)=SRC(5,6)= (t1 + t2 + 1) >> 1;
2452 SRC(3,1)=SRC(4,3)=SRC(5,5)=SRC(6,7)= (t1 + 2*t2 + t3 + 2) >> 2;
2453 SRC(3,0)=SRC(4,2)=SRC(5,4)=SRC(6,6)= (t2 + t3 + 1) >> 1;
2454 SRC(4,1)=SRC(5,3)=SRC(6,5)=SRC(7,7)= (t2 + 2*t3 + t4 + 2) >> 2;
2455 SRC(4,0)=SRC(5,2)=SRC(6,4)=SRC(7,6)= (t3 + t4 + 1) >> 1;
2456 SRC(5,1)=SRC(6,3)=SRC(7,5)= (t3 + 2*t4 + t5 + 2) >> 2;
2457 SRC(5,0)=SRC(6,2)=SRC(7,4)= (t4 + t5 + 1) >> 1;
2458 SRC(6,1)=SRC(7,3)= (t4 + 2*t5 + t6 + 2) >> 2;
2459 SRC(6,0)=SRC(7,2)= (t5 + t6 + 1) >> 1;
2460 SRC(7,1)= (t5 + 2*t6 + t7 + 2) >> 2;
2461 SRC(7,0)= (t6 + t7 + 1) >> 1;
2462 }
2463 static void pred8x8l_horizontal_down_c(uint8_t *src, int has_topleft, int has_topright, int stride)
2464 {
2465 PREDICT_8x8_LOAD_TOP;
2466 PREDICT_8x8_LOAD_LEFT;
2467 PREDICT_8x8_LOAD_TOPLEFT;
2468 SRC(0,7)= (l6 + l7 + 1) >> 1;
2469 SRC(1,7)= (l5 + 2*l6 + l7 + 2) >> 2;
2470 SRC(0,6)=SRC(2,7)= (l5 + l6 + 1) >> 1;
2471 SRC(1,6)=SRC(3,7)= (l4 + 2*l5 + l6 + 2) >> 2;
2472 SRC(0,5)=SRC(2,6)=SRC(4,7)= (l4 + l5 + 1) >> 1;
2473 SRC(1,5)=SRC(3,6)=SRC(5,7)= (l3 + 2*l4 + l5 + 2) >> 2;
2474 SRC(0,4)=SRC(2,5)=SRC(4,6)=SRC(6,7)= (l3 + l4 + 1) >> 1;
2475 SRC(1,4)=SRC(3,5)=SRC(5,6)=SRC(7,7)= (l2 + 2*l3 + l4 + 2) >> 2;
2476 SRC(0,3)=SRC(2,4)=SRC(4,5)=SRC(6,6)= (l2 + l3 + 1) >> 1;
2477 SRC(1,3)=SRC(3,4)=SRC(5,5)=SRC(7,6)= (l1 + 2*l2 + l3 + 2) >> 2;
2478 SRC(0,2)=SRC(2,3)=SRC(4,4)=SRC(6,5)= (l1 + l2 + 1) >> 1;
2479 SRC(1,2)=SRC(3,3)=SRC(5,4)=SRC(7,5)= (l0 + 2*l1 + l2 + 2) >> 2;
2480 SRC(0,1)=SRC(2,2)=SRC(4,3)=SRC(6,4)= (l0 + l1 + 1) >> 1;
2481 SRC(1,1)=SRC(3,2)=SRC(5,3)=SRC(7,4)= (lt + 2*l0 + l1 + 2) >> 2;
2482 SRC(0,0)=SRC(2,1)=SRC(4,2)=SRC(6,3)= (lt + l0 + 1) >> 1;
2483 SRC(1,0)=SRC(3,1)=SRC(5,2)=SRC(7,3)= (l0 + 2*lt + t0 + 2) >> 2;
2484 SRC(2,0)=SRC(4,1)=SRC(6,2)= (t1 + 2*t0 + lt + 2) >> 2;
2485 SRC(3,0)=SRC(5,1)=SRC(7,2)= (t2 + 2*t1 + t0 + 2) >> 2;
2486 SRC(4,0)=SRC(6,1)= (t3 + 2*t2 + t1 + 2) >> 2;
2487 SRC(5,0)=SRC(7,1)= (t4 + 2*t3 + t2 + 2) >> 2;
2488 SRC(6,0)= (t5 + 2*t4 + t3 + 2) >> 2;
2489 SRC(7,0)= (t6 + 2*t5 + t4 + 2) >> 2;
2490 }
2491 static void pred8x8l_vertical_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
2492 {
2493 PREDICT_8x8_LOAD_TOP;
2494 PREDICT_8x8_LOAD_TOPRIGHT;
2495 SRC(0,0)= (t0 + t1 + 1) >> 1;
2496 SRC(0,1)= (t0 + 2*t1 + t2 + 2) >> 2;
2497 SRC(0,2)=SRC(1,0)= (t1 + t2 + 1) >> 1;
2498 SRC(0,3)=SRC(1,1)= (t1 + 2*t2 + t3 + 2) >> 2;
2499 SRC(0,4)=SRC(1,2)=SRC(2,0)= (t2 + t3 + 1) >> 1;
2500 SRC(0,5)=SRC(1,3)=SRC(2,1)= (t2 + 2*t3 + t4 + 2) >> 2;
2501 SRC(0,6)=SRC(1,4)=SRC(2,2)=SRC(3,0)= (t3 + t4 + 1) >> 1;
2502 SRC(0,7)=SRC(1,5)=SRC(2,3)=SRC(3,1)= (t3 + 2*t4 + t5 + 2) >> 2;
2503 SRC(1,6)=SRC(2,4)=SRC(3,2)=SRC(4,0)= (t4 + t5 + 1) >> 1;
2504 SRC(1,7)=SRC(2,5)=SRC(3,3)=SRC(4,1)= (t4 + 2*t5 + t6 + 2) >> 2;
2505 SRC(2,6)=SRC(3,4)=SRC(4,2)=SRC(5,0)= (t5 + t6 + 1) >> 1;
2506 SRC(2,7)=SRC(3,5)=SRC(4,3)=SRC(5,1)= (t5 + 2*t6 + t7 + 2) >> 2;
2507 SRC(3,6)=SRC(4,4)=SRC(5,2)=SRC(6,0)= (t6 + t7 + 1) >> 1;
2508 SRC(3,7)=SRC(4,5)=SRC(5,3)=SRC(6,1)= (t6 + 2*t7 + t8 + 2) >> 2;
2509 SRC(4,6)=SRC(5,4)=SRC(6,2)=SRC(7,0)= (