one bugfix and a few gcc4 bug workaorunds by (Gianluigi Tiesi: mplayer, netfarm it)
[libav.git] / libavcodec / h264.c
CommitLineData
0da71265
MN
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
e5017ab8
LA
34#include "cabac.h"
35
0da71265
MN
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 */
60typedef struct SPS{
61
62 int profile_idc;
63 int level_idc;
0da71265
MN
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
a15e68de 72 int gaps_in_frame_num_allowed_flag;
0da71265
MN
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;
a15e68de
MN
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
0da71265 83 int vui_parameters_present_flag;
5ff85f1d 84 AVRational sar;
ec587a50
MR
85 int timing_info_present_flag;
86 uint32_t num_units_in_tick;
87 uint32_t time_scale;
88 int fixed_frame_rate_flag;
0da71265 89 short offset_for_ref_frame[256]; //FIXME dyn aloc?
4e4d983e
LM
90 int bitstream_restriction_flag;
91 int num_reorder_frames;
0da71265
MN
92}SPS;
93
94/**
95 * Picture parameter set
96 */
97typedef 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
0da71265
MN
112}PPS;
113
114/**
115 * Memory management control operation opcode.
116 */
117typedef enum MMCOOpcode{
118 MMCO_END=0,
119 MMCO_SHORT2UNUSED,
120 MMCO_LONG2UNUSED,
121 MMCO_SHORT2LONG,
122 MMCO_SET_MAX_LONG,
123 MMCO_RESET,
124 MMCO_LONG,
125} MMCOOpcode;
126
127/**
128 * Memory management control operation.
129 */
130typedef struct MMCO{
131 MMCOOpcode opcode;
132 int short_frame_num;
133 int long_index;
134} MMCO;
135
136/**
137 * H264Context
138 */
139typedef struct H264Context{
140 MpegEncContext s;
141 int nal_ref_idc;
142 int nal_unit_type;
143#define NAL_SLICE 1
144#define NAL_DPA 2
145#define NAL_DPB 3
146#define NAL_DPC 4
147#define NAL_IDR_SLICE 5
148#define NAL_SEI 6
149#define NAL_SPS 7
150#define NAL_PPS 8
151#define NAL_PICTURE_DELIMITER 9
152#define NAL_FILTER_DATA 10
153 uint8_t *rbsp_buffer;
154 int rbsp_buffer_size;
155
4770b1b4
RT
156 /**
157 * Used to parse AVC variant of h264
158 */
159 int is_avc; ///< this flag is != 0 if codec is avc1
160 int got_avcC; ///< flag used to parse avcC data only once
161 int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
162
0da71265
MN
163 int chroma_qp; //QPc
164
160d679c 165 int prev_mb_skipped; //FIXME remove (IMHO not used)
0da71265
MN
166
167 //prediction stuff
168 int chroma_pred_mode;
169 int intra16x16_pred_mode;
6ba71fc4
LLL
170
171 int top_mb_xy;
172 int left_mb_xy[2];
0da71265
MN
173
174 int8_t intra4x4_pred_mode_cache[5*8];
175 int8_t (*intra4x4_pred_mode)[8];
176 void (*pred4x4 [9+3])(uint8_t *src, uint8_t *topright, int stride);//FIXME move to dsp?
177 void (*pred8x8 [4+3])(uint8_t *src, int stride);
178 void (*pred16x16[4+3])(uint8_t *src, int stride);
179 unsigned int topleft_samples_available;
180 unsigned int top_samples_available;
181 unsigned int topright_samples_available;
182 unsigned int left_samples_available;
6ba71fc4
LLL
183 uint8_t (*top_borders[2])[16+2*8];
184 uint8_t left_border[2*(17+2*9)];
0da71265
MN
185
186 /**
187 * non zero coeff count cache.
188 * is 64 if not available.
189 */
bd9ea6a4 190 uint8_t non_zero_count_cache[6*8] __align8;
53c05b1e 191 uint8_t (*non_zero_count)[16];
0da71265
MN
192
193 /**
194 * Motion vector cache.
195 */
bd9ea6a4
MN
196 int16_t mv_cache[2][5*8][2] __align8;
197 int8_t ref_cache[2][5*8] __align8;
0da71265
MN
198#define LIST_NOT_USED -1 //FIXME rename?
199#define PART_NOT_AVAILABLE -2
200
201 /**
202 * is 1 if the specific list MV&references are set to 0,0,-2.
203 */
204 int mv_cache_clean[2];
205
6867a90b
LLL
206 /**
207 * block_offset[ 0..23] for frame macroblocks
208 * block_offset[24..47] for field macroblocks
209 */
210 int block_offset[2*(16+8)];
0da71265 211
a55f20bd
LM
212 uint32_t *mb2b_xy; //FIXME are these 4 a good idea?
213 uint32_t *mb2b8_xy;
650cec0c 214 int b_stride; //FIXME use s->b4_stride
0da71265
MN
215 int b8_stride;
216
8b82a956
MN
217 int halfpel_flag;
218 int thirdpel_flag;
219
da3b9756
MM
220 int unknown_svq3_flag;
221 int next_slice_index;
222
0da71265
MN
223 SPS sps_buffer[MAX_SPS_COUNT];
224 SPS sps; ///< current sps
225
226 PPS pps_buffer[MAX_PPS_COUNT];
227 /**
228 * current pps
229 */
3b66c4c5 230 PPS pps; //FIXME move to Picture perhaps? (->no) do we need that?
0da71265
MN
231
232 int slice_num;
233 uint8_t *slice_table_base;
234 uint8_t *slice_table; ///< slice_table_base + mb_stride + 1
235 int slice_type;
236 int slice_type_fixed;
237
238 //interlacing specific flags
6ba71fc4 239 int mb_aff_frame;
0da71265
MN
240 int mb_field_decoding_flag;
241
242 int sub_mb_type[4];
243
244 //POC stuff
245 int poc_lsb;
246 int poc_msb;
247 int delta_poc_bottom;
248 int delta_poc[2];
249 int frame_num;
250 int prev_poc_msb; ///< poc_msb of the last reference pic for POC type 0
251 int prev_poc_lsb; ///< poc_lsb of the last reference pic for POC type 0
252 int frame_num_offset; ///< for POC type 2
253 int prev_frame_num_offset; ///< for POC type 2
254 int prev_frame_num; ///< frame_num of the last pic for POC type 1/2
255
256 /**
257 * frame_num for frames or 2*frame_num for field pics.
258 */
259 int curr_pic_num;
260
261 /**
262 * max_frame_num or 2*max_frame_num for field pics.
263 */
264 int max_pic_num;
265
266 //Weighted pred stuff
9f2d1b4f
LM
267 int use_weight;
268 int use_weight_chroma;
0da71265
MN
269 int luma_log2_weight_denom;
270 int chroma_log2_weight_denom;
271 int luma_weight[2][16];
272 int luma_offset[2][16];
273 int chroma_weight[2][16][2];
274 int chroma_offset[2][16][2];
9f2d1b4f 275 int implicit_weight[16][16];
0da71265
MN
276
277 //deblock
53c05b1e 278 int deblocking_filter; ///< disable_deblocking_filter_idc with 1<->0
980a82b7
MN
279 int slice_alpha_c0_offset;
280 int slice_beta_offset;
0da71265
MN
281
282 int redundant_pic_count;
283
284 int direct_spatial_mv_pred;
5ad984c9 285 int dist_scale_factor[16];
2f944356 286 int map_col_to_list0[2][16];
0da71265
MN
287
288 /**
289 * num_ref_idx_l0/1_active_minus1 + 1
290 */
291 int ref_count[2];// FIXME split for AFF
17107065
LM
292 Picture *short_ref[32];
293 Picture *long_ref[32];
0da71265
MN
294 Picture default_ref_list[2][32];
295 Picture ref_list[2][32]; //FIXME size?
296 Picture field_ref_list[2][32]; //FIXME size?
a4dae92b 297 Picture *delayed_pic[16]; //FIXME size?
4e4d983e 298 Picture *delayed_output_pic;
0da71265
MN
299
300 /**
301 * memory management control operations buffer.
302 */
303 MMCO mmco[MAX_MMCO_COUNT];
304 int mmco_index;
305
306 int long_ref_count; ///< number of actual long term references
307 int short_ref_count; ///< number of actual short term references
308
309 //data partitioning
310 GetBitContext intra_gb;
311 GetBitContext inter_gb;
312 GetBitContext *intra_gb_ptr;
313 GetBitContext *inter_gb_ptr;
314
315 DCTELEM mb[16*24] __align8;
e5017ab8
LA
316
317 /**
318 * Cabac
319 */
320 CABACContext cabac;
321 uint8_t cabac_state[399];
322 int cabac_init_idc;
323
324 /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
325 uint16_t *cbp_table;
3981c385
MN
326 int top_cbp;
327 int left_cbp;
693399ba 328 /* chroma_pred_mode for i4x4 or i16x16, else 0 */
e5017ab8
LA
329 uint8_t *chroma_pred_mode_table;
330 int last_qscale_diff;
9e528114 331 int16_t (*mvd_table[2])[2];
bd9ea6a4 332 int16_t mvd_cache[2][5*8][2] __align8;
5ad984c9
LM
333 uint8_t *direct_table;
334 uint8_t direct_cache[5*8];
e5017ab8 335
e4b36d44
MN
336 uint8_t zigzag_scan[16];
337 uint8_t field_scan[16];
0da71265
MN
338}H264Context;
339
340static VLC coeff_token_vlc[4];
341static VLC chroma_dc_coeff_token_vlc;
342
343static VLC total_zeros_vlc[15];
344static VLC chroma_dc_total_zeros_vlc[3];
345
346static VLC run_vlc[6];
347static VLC run7_vlc;
348
8b82a956
MN
349static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
350static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
6ba71fc4 351static 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);
8b82a956 352
377ec888
MN
353static inline uint32_t pack16to32(int a, int b){
354#ifdef WORDS_BIGENDIAN
355 return (b&0xFFFF) + (a<<16);
356#else
357 return (a&0xFFFF) + (b<<16);
358#endif
359}
360
0da71265
MN
361/**
362 * fill a rectangle.
5175b937
LLL
363 * @param h height of the rectangle, should be a constant
364 * @param w width of the rectangle, should be a constant
0da71265
MN
365 * @param size the size of val (1 or 4), should be a constant
366 */
af6e2fed
MN
367static inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){ //FIXME ensure this IS inlined
368 uint8_t *p= (uint8_t*)vp;
0da71265
MN
369 assert(size==1 || size==4);
370
371 w *= size;
372 stride *= size;
373
bd9ea6a4 374 assert((((int)vp)&(FFMIN(w, STRIDE_ALIGN)-1)) == 0);
d9c780a8 375 assert((stride&(w-1))==0);
3b66c4c5 376//FIXME check what gcc generates for 64 bit on x86 and possibly write a 32 bit ver of it
0da71265
MN
377 if(w==2 && h==2){
378 *(uint16_t*)(p + 0)=
379 *(uint16_t*)(p + stride)= size==4 ? val : val*0x0101;
380 }else if(w==2 && h==4){
381 *(uint16_t*)(p + 0*stride)=
382 *(uint16_t*)(p + 1*stride)=
383 *(uint16_t*)(p + 2*stride)=
384 *(uint16_t*)(p + 3*stride)= size==4 ? val : val*0x0101;
f7a8c179
MN
385 }else if(w==4 && h==1){
386 *(uint32_t*)(p + 0*stride)= size==4 ? val : val*0x01010101;
0da71265
MN
387 }else if(w==4 && h==2){
388 *(uint32_t*)(p + 0*stride)=
389 *(uint32_t*)(p + 1*stride)= size==4 ? val : val*0x01010101;
390 }else if(w==4 && h==4){
391 *(uint32_t*)(p + 0*stride)=
392 *(uint32_t*)(p + 1*stride)=
393 *(uint32_t*)(p + 2*stride)=
394 *(uint32_t*)(p + 3*stride)= size==4 ? val : val*0x01010101;
395 }else if(w==8 && h==1){
396 *(uint32_t*)(p + 0)=
397 *(uint32_t*)(p + 4)= size==4 ? val : val*0x01010101;
398 }else if(w==8 && h==2){
399 *(uint32_t*)(p + 0 + 0*stride)=
400 *(uint32_t*)(p + 4 + 0*stride)=
401 *(uint32_t*)(p + 0 + 1*stride)=
402 *(uint32_t*)(p + 4 + 1*stride)= size==4 ? val : val*0x01010101;
403 }else if(w==8 && h==4){
404 *(uint64_t*)(p + 0*stride)=
405 *(uint64_t*)(p + 1*stride)=
406 *(uint64_t*)(p + 2*stride)=
407 *(uint64_t*)(p + 3*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
408 }else if(w==16 && h==2){
409 *(uint64_t*)(p + 0+0*stride)=
410 *(uint64_t*)(p + 8+0*stride)=
411 *(uint64_t*)(p + 0+1*stride)=
412 *(uint64_t*)(p + 8+1*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
413 }else if(w==16 && h==4){
414 *(uint64_t*)(p + 0+0*stride)=
415 *(uint64_t*)(p + 8+0*stride)=
416 *(uint64_t*)(p + 0+1*stride)=
417 *(uint64_t*)(p + 8+1*stride)=
418 *(uint64_t*)(p + 0+2*stride)=
419 *(uint64_t*)(p + 8+2*stride)=
420 *(uint64_t*)(p + 0+3*stride)=
421 *(uint64_t*)(p + 8+3*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
422 }else
423 assert(0);
424}
425
46f2f05f 426static inline void fill_caches(H264Context *h, int mb_type, int for_deblock){
0da71265 427 MpegEncContext * const s = &h->s;
7bc9090a 428 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
0da71265
MN
429 int topleft_xy, top_xy, topright_xy, left_xy[2];
430 int topleft_type, top_type, topright_type, left_type[2];
6867a90b 431 int left_block[8];
0da71265
MN
432 int i;
433
e2e5894a
LM
434 //FIXME deblocking can skip fill_caches much of the time with multiple slices too.
435 // the actual condition is whether we're on the edge of a slice,
436 // and even then the intra and nnz parts are unnecessary.
437 if(for_deblock && h->slice_num == 1)
438 return;
439
3b66c4c5 440 //wow what a mess, why didn't they simplify the interlacing&intra stuff, i can't imagine that these complex rules are worth it
0da71265 441
6867a90b
LLL
442 top_xy = mb_xy - s->mb_stride;
443 topleft_xy = top_xy - 1;
444 topright_xy= top_xy + 1;
445 left_xy[1] = left_xy[0] = mb_xy-1;
446 left_block[0]= 0;
447 left_block[1]= 1;
448 left_block[2]= 2;
449 left_block[3]= 3;
450 left_block[4]= 7;
451 left_block[5]= 10;
452 left_block[6]= 8;
453 left_block[7]= 11;
6ba71fc4 454 if(h->mb_aff_frame){
6867a90b
LLL
455 const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
456 const int top_pair_xy = pair_xy - s->mb_stride;
457 const int topleft_pair_xy = top_pair_xy - 1;
458 const int topright_pair_xy = top_pair_xy + 1;
459 const int topleft_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
460 const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
461 const int topright_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
462 const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
463 const int curr_mb_frame_flag = !IS_INTERLACED(mb_type);
464 const int bottom = (s->mb_y & 1);
465 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);
466 if (bottom
467 ? !curr_mb_frame_flag // bottom macroblock
468 : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
469 ) {
470 top_xy -= s->mb_stride;
471 }
472 if (bottom
473 ? !curr_mb_frame_flag // bottom macroblock
474 : (!curr_mb_frame_flag && !topleft_mb_frame_flag) // top macroblock
475 ) {
476 topleft_xy -= s->mb_stride;
477 }
478 if (bottom
479 ? !curr_mb_frame_flag // bottom macroblock
480 : (!curr_mb_frame_flag && !topright_mb_frame_flag) // top macroblock
481 ) {
482 topright_xy -= s->mb_stride;
483 }
484 if (left_mb_frame_flag != curr_mb_frame_flag) {
485 left_xy[1] = left_xy[0] = pair_xy - 1;
486 if (curr_mb_frame_flag) {
487 if (bottom) {
488 left_block[0]= 2;
489 left_block[1]= 2;
490 left_block[2]= 3;
491 left_block[3]= 3;
492 left_block[4]= 8;
493 left_block[5]= 11;
494 left_block[6]= 8;
495 left_block[7]= 11;
496 } else {
497 left_block[0]= 0;
498 left_block[1]= 0;
499 left_block[2]= 1;
500 left_block[3]= 1;
501 left_block[4]= 7;
502 left_block[5]= 10;
503 left_block[6]= 7;
504 left_block[7]= 10;
505 }
506 } else {
507 left_xy[1] += s->mb_stride;
508 //left_block[0]= 0;
509 left_block[1]= 2;
510 left_block[2]= 0;
511 left_block[3]= 2;
512 //left_block[4]= 7;
513 left_block[5]= 10;
514 left_block[6]= 7;
515 left_block[7]= 10;
516 }
517 }
0da71265
MN
518 }
519
826de46e
LLL
520 h->top_mb_xy = top_xy;
521 h->left_mb_xy[0] = left_xy[0];
522 h->left_mb_xy[1] = left_xy[1];
6ba71fc4 523 if(for_deblock){
46f2f05f
MN
524 topleft_type = h->slice_table[topleft_xy ] < 255 ? s->current_picture.mb_type[topleft_xy] : 0;
525 top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0;
526 topright_type= h->slice_table[topright_xy] < 255 ? s->current_picture.mb_type[topright_xy]: 0;
527 left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
528 left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
529 }else{
530 topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
531 top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
532 topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
533 left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
534 left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
535 }
0da71265
MN
536
537 if(IS_INTRA(mb_type)){
538 h->topleft_samples_available=
539 h->top_samples_available=
540 h->left_samples_available= 0xFFFF;
541 h->topright_samples_available= 0xEEEA;
542
543 if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
544 h->topleft_samples_available= 0xB3FF;
545 h->top_samples_available= 0x33FF;
546 h->topright_samples_available= 0x26EA;
547 }
548 for(i=0; i<2; i++){
549 if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
550 h->topleft_samples_available&= 0xDF5F;
551 h->left_samples_available&= 0x5F5F;
552 }
553 }
554
555 if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
556 h->topleft_samples_available&= 0x7FFF;
557
558 if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
559 h->topright_samples_available&= 0xFBFF;
560
561 if(IS_INTRA4x4(mb_type)){
562 if(IS_INTRA4x4(top_type)){
563 h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
564 h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
565 h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
566 h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
567 }else{
568 int pred;
6fbcaaa0 569 if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred))
0da71265 570 pred= -1;
6fbcaaa0
LLL
571 else{
572 pred= 2;
0da71265
MN
573 }
574 h->intra4x4_pred_mode_cache[4+8*0]=
575 h->intra4x4_pred_mode_cache[5+8*0]=
576 h->intra4x4_pred_mode_cache[6+8*0]=
577 h->intra4x4_pred_mode_cache[7+8*0]= pred;
578 }
579 for(i=0; i<2; i++){
580 if(IS_INTRA4x4(left_type[i])){
581 h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
582 h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
583 }else{
584 int pred;
6fbcaaa0 585 if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred))
0da71265 586 pred= -1;
6fbcaaa0
LLL
587 else{
588 pred= 2;
0da71265
MN
589 }
590 h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
591 h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
592 }
593 }
594 }
595 }
596
597
598/*
5990 . T T. T T T T
6001 L . .L . . . .
6012 L . .L . . . .
6023 . T TL . . . .
6034 L . .L . . . .
6045 L . .. . . . .
605*/
606//FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec)
607 if(top_type){
6867a90b
LLL
608 h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
609 h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
610 h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
53c05b1e 611 h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
0da71265 612
6867a90b 613 h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
53c05b1e 614 h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
0da71265 615
6867a90b 616 h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
53c05b1e 617 h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
3981c385 618
0da71265
MN
619 }else{
620 h->non_zero_count_cache[4+8*0]=
621 h->non_zero_count_cache[5+8*0]=
622 h->non_zero_count_cache[6+8*0]=
623 h->non_zero_count_cache[7+8*0]=
624
625 h->non_zero_count_cache[1+8*0]=
626 h->non_zero_count_cache[2+8*0]=
627
628 h->non_zero_count_cache[1+8*3]=
3981c385
MN
629 h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
630
0da71265 631 }
826de46e 632
6867a90b
LLL
633 for (i=0; i<2; i++) {
634 if(left_type[i]){
635 h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
636 h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
637 h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
638 h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
6867a90b
LLL
639 }else{
640 h->non_zero_count_cache[3+8*1 + 2*8*i]=
641 h->non_zero_count_cache[3+8*2 + 2*8*i]=
642 h->non_zero_count_cache[0+8*1 + 8*i]=
643 h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
826de46e
LLL
644 }
645 }
646
647 if( h->pps.cabac ) {
648 // top_cbp
649 if(top_type) {
650 h->top_cbp = h->cbp_table[top_xy];
651 } else if(IS_INTRA(mb_type)) {
652 h->top_cbp = 0x1C0;
653 } else {
654 h->top_cbp = 0;
655 }
656 // left_cbp
657 if (left_type[0]) {
658 h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
659 } else if(IS_INTRA(mb_type)) {
660 h->left_cbp = 0x1C0;
661 } else {
662 h->left_cbp = 0;
663 }
664 if (left_type[0]) {
665 h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
666 }
667 if (left_type[1]) {
668 h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
6867a90b 669 }
0da71265 670 }
6867a90b 671
0da71265 672#if 1
5ad984c9 673 //FIXME direct mb can skip much of this
e2e5894a 674 if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
0da71265 675 int list;
e2e5894a
LM
676 for(list=0; list<1+(h->slice_type==B_TYPE); list++){
677 if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
0da71265
MN
678 /*if(!h->mv_cache_clean[list]){
679 memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
680 memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
681 h->mv_cache_clean[list]= 1;
682 }*/
5ad984c9 683 continue;
0da71265
MN
684 }
685 h->mv_cache_clean[list]= 0;
686
0da71265
MN
687 if(IS_INTER(top_type)){
688 const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
689 const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
690 *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
691 *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
692 *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
693 *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
694 h->ref_cache[list][scan8[0] + 0 - 1*8]=
695 h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
696 h->ref_cache[list][scan8[0] + 2 - 1*8]=
697 h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
698 }else{
699 *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
700 *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
701 *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
702 *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
703 *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
704 }
705
0da71265
MN
706 //FIXME unify cleanup or sth
707 if(IS_INTER(left_type[0])){
708 const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
709 const int b8_xy= h->mb2b8_xy[left_xy[0]] + 1;
710 *(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]];
711 *(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]];
712 h->ref_cache[list][scan8[0] - 1 + 0*8]=
713 h->ref_cache[list][scan8[0] - 1 + 1*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0]>>1)];
714 }else{
715 *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 0*8]=
716 *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 1*8]= 0;
717 h->ref_cache[list][scan8[0] - 1 + 0*8]=
718 h->ref_cache[list][scan8[0] - 1 + 1*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
719 }
720
721 if(IS_INTER(left_type[1])){
722 const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
723 const int b8_xy= h->mb2b8_xy[left_xy[1]] + 1;
724 *(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]];
725 *(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]];
726 h->ref_cache[list][scan8[0] - 1 + 2*8]=
727 h->ref_cache[list][scan8[0] - 1 + 3*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[2]>>1)];
728 }else{
729 *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 2*8]=
730 *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 3*8]= 0;
731 h->ref_cache[list][scan8[0] - 1 + 2*8]=
732 h->ref_cache[list][scan8[0] - 1 + 3*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
d9c780a8 733 assert((!left_type[0]) == (!left_type[1]));
0da71265
MN
734 }
735
e2e5894a 736 if(for_deblock || (IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred))
46f2f05f
MN
737 continue;
738
e2e5894a
LM
739 if(IS_INTER(topleft_type)){
740 const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
741 const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + h->b8_stride;
742 *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
743 h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
744 }else{
745 *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
746 h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
747 }
748
749 if(IS_INTER(topright_type)){
750 const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
751 const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
752 *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
753 h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
754 }else{
755 *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
756 h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
757 }
758
759
0da71265
MN
760 h->ref_cache[list][scan8[5 ]+1] =
761 h->ref_cache[list][scan8[7 ]+1] =
3b66c4c5 762 h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewhere else)
0da71265
MN
763 h->ref_cache[list][scan8[4 ]] =
764 h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
765 *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
766 *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
3b66c4c5 767 *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
0da71265
MN
768 *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
769 *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
9e528114
LA
770
771 if( h->pps.cabac ) {
772 /* XXX beurk, Load mvd */
773 if(IS_INTER(topleft_type)){
774 const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
775 *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy];
776 }else{
777 *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 - 1*8]= 0;
778 }
779
780 if(IS_INTER(top_type)){
781 const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
782 *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
783 *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
784 *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
785 *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
786 }else{
787 *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
788 *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
789 *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
790 *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
791 }
792 if(IS_INTER(left_type[0])){
793 const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
794 *(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]];
795 *(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]];
796 }else{
797 *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
798 *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
799 }
800 if(IS_INTER(left_type[1])){
801 const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
802 *(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]];
803 *(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]];
804 }else{
805 *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
806 *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
807 }
808 *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
809 *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
3b66c4c5 810 *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
9e528114
LA
811 *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
812 *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
5ad984c9
LM
813
814 if(h->slice_type == B_TYPE){
815 fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
816
817 if(IS_DIRECT(top_type)){
818 *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
819 }else if(IS_8X8(top_type)){
820 int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
821 h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
822 h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
823 }else{
824 *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
825 }
826
827 //FIXME interlacing
828 if(IS_DIRECT(left_type[0])){
829 h->direct_cache[scan8[0] - 1 + 0*8]=
830 h->direct_cache[scan8[0] - 1 + 2*8]= 1;
831 }else if(IS_8X8(left_type[0])){
832 int b8_xy = h->mb2b8_xy[left_xy[0]] + 1;
833 h->direct_cache[scan8[0] - 1 + 0*8]= h->direct_table[b8_xy];
834 h->direct_cache[scan8[0] - 1 + 2*8]= h->direct_table[b8_xy + h->b8_stride];
835 }else{
836 h->direct_cache[scan8[0] - 1 + 0*8]=
837 h->direct_cache[scan8[0] - 1 + 2*8]= 0;
838 }
839 }
9e528114 840 }
0da71265 841 }
0da71265
MN
842 }
843#endif
844}
845
846static inline void write_back_intra_pred_mode(H264Context *h){
847 MpegEncContext * const s = &h->s;
7bc9090a 848 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
0da71265
MN
849
850 h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
851 h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
852 h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
853 h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
854 h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
855 h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
856 h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
857}
858
859/**
860 * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
861 */
862static inline int check_intra4x4_pred_mode(H264Context *h){
863 MpegEncContext * const s = &h->s;
864 static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
865 static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
866 int i;
867
868 if(!(h->top_samples_available&0x8000)){
869 for(i=0; i<4; i++){
870 int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
871 if(status<0){
9b879566 872 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);
0da71265
MN
873 return -1;
874 } else if(status){
875 h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
876 }
877 }
878 }
879
880 if(!(h->left_samples_available&0x8000)){
881 for(i=0; i<4; i++){
882 int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
883 if(status<0){
9b879566 884 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);
0da71265
MN
885 return -1;
886 } else if(status){
887 h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
888 }
889 }
890 }
891
892 return 0;
893} //FIXME cleanup like next
894
895/**
896 * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
897 */
898static inline int check_intra_pred_mode(H264Context *h, int mode){
899 MpegEncContext * const s = &h->s;
900 static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
901 static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
902
5175b937
LLL
903 if(mode < 0 || mode > 6) {
904 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);
7440fe83 905 return -1;
5175b937 906 }
7440fe83 907
0da71265
MN
908 if(!(h->top_samples_available&0x8000)){
909 mode= top[ mode ];
910 if(mode<0){
9b879566 911 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);
0da71265
MN
912 return -1;
913 }
914 }
915
916 if(!(h->left_samples_available&0x8000)){
917 mode= left[ mode ];
918 if(mode<0){
9b879566 919 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);
0da71265
MN
920 return -1;
921 }
922 }
923
924 return mode;
925}
926
927/**
928 * gets the predicted intra4x4 prediction mode.
929 */
930static inline int pred_intra_mode(H264Context *h, int n){
931 const int index8= scan8[n];
932 const int left= h->intra4x4_pred_mode_cache[index8 - 1];
933 const int top = h->intra4x4_pred_mode_cache[index8 - 8];
934 const int min= FFMIN(left, top);
935
95c26348 936 tprintf("mode:%d %d min:%d\n", left ,top, min);
0da71265
MN
937
938 if(min<0) return DC_PRED;
939 else return min;
940}
941
942static inline void write_back_non_zero_count(H264Context *h){
943 MpegEncContext * const s = &h->s;
7bc9090a 944 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
0da71265 945
6867a90b
LLL
946 h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
947 h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
948 h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
53c05b1e 949 h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
6867a90b
LLL
950 h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
951 h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
952 h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
53c05b1e 953
6867a90b 954 h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
53c05b1e 955 h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
6867a90b 956 h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
53c05b1e 957
6867a90b 958 h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
53c05b1e 959 h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
6867a90b 960 h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
0da71265
MN
961}
962
963/**
964 * gets the predicted number of non zero coefficients.
965 * @param n block index
966 */
967static inline int pred_non_zero_count(H264Context *h, int n){
968 const int index8= scan8[n];
969 const int left= h->non_zero_count_cache[index8 - 1];
970 const int top = h->non_zero_count_cache[index8 - 8];
971 int i= left + top;
972
973 if(i<64) i= (i+1)>>1;
974
95c26348 975 tprintf("pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
0da71265
MN
976
977 return i&31;
978}
979
1924f3ce
MN
980static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
981 const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
982
983 if(topright_ref != PART_NOT_AVAILABLE){
984 *C= h->mv_cache[list][ i - 8 + part_width ];
985 return topright_ref;
986 }else{
95c26348
MN
987 tprintf("topright MV not available\n");
988
1924f3ce
MN
989 *C= h->mv_cache[list][ i - 8 - 1 ];
990 return h->ref_cache[list][ i - 8 - 1 ];
991 }
992}
993
0da71265
MN
994/**
995 * gets the predicted MV.
996 * @param n the block index
997 * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
998 * @param mx the x component of the predicted motion vector
999 * @param my the y component of the predicted motion vector
1000 */
1001static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
0da71265
MN
1002 const int index8= scan8[n];
1003 const int top_ref= h->ref_cache[list][ index8 - 8 ];
0da71265
MN
1004 const int left_ref= h->ref_cache[list][ index8 - 1 ];
1005 const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
1006 const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
1924f3ce
MN
1007 const int16_t * C;
1008 int diagonal_ref, match_count;
1009
0da71265 1010 assert(part_width==1 || part_width==2 || part_width==4);
1924f3ce 1011
0da71265
MN
1012/* mv_cache
1013 B . . A T T T T
1014 U . . L . . , .
1015 U . . L . . . .
1016 U . . L . . , .
1017 . . . L . . . .
1018*/
1924f3ce
MN
1019
1020 diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
1021 match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
827c91bf 1022 tprintf("pred_motion match_count=%d\n", match_count);
1924f3ce
MN
1023 if(match_count > 1){ //most common
1024 *mx= mid_pred(A[0], B[0], C[0]);
1025 *my= mid_pred(A[1], B[1], C[1]);
1026 }else if(match_count==1){
1027 if(left_ref==ref){
1028 *mx= A[0];
1029 *my= A[1];
1030 }else if(top_ref==ref){
1031 *mx= B[0];
1032 *my= B[1];
0da71265 1033 }else{
1924f3ce
MN
1034 *mx= C[0];
1035 *my= C[1];
0da71265
MN
1036 }
1037 }else{
1924f3ce 1038 if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
0da71265 1039 *mx= A[0];
1924f3ce 1040 *my= A[1];
0da71265 1041 }else{
1924f3ce
MN
1042 *mx= mid_pred(A[0], B[0], C[0]);
1043 *my= mid_pred(A[1], B[1], C[1]);
0da71265 1044 }
0da71265 1045 }
1924f3ce 1046
af6e2fed 1047 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);
0da71265
MN
1048}
1049
1050/**
1051 * gets the directionally predicted 16x8 MV.
1052 * @param n the block index
1053 * @param mx the x component of the predicted motion vector
1054 * @param my the y component of the predicted motion vector
1055 */
1056static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
0da71265
MN
1057 if(n==0){
1058 const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
1059 const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
1060
579448a0 1061 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);
0da71265
MN
1062
1063 if(top_ref == ref){
1064 *mx= B[0];
1065 *my= B[1];
1066 return;
1067 }
1068 }else{
1069 const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
1070 const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
1071
579448a0 1072 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);
0da71265
MN
1073
1074 if(left_ref == ref){
1075 *mx= A[0];
1076 *my= A[1];
1077 return;
1078 }
1079 }
1080
1081 //RARE
1082 pred_motion(h, n, 4, list, ref, mx, my);
1083}
1084
1085/**
1086 * gets the directionally predicted 8x16 MV.
1087 * @param n the block index
1088 * @param mx the x component of the predicted motion vector
1089 * @param my the y component of the predicted motion vector
1090 */
1091static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
0da71265
MN
1092 if(n==0){
1093 const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
1094 const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
1095
579448a0 1096 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);
0da71265
MN
1097
1098 if(left_ref == ref){
1099 *mx= A[0];
1100 *my= A[1];
1101 return;
1102 }
1103 }else{
1924f3ce
MN
1104 const int16_t * C;
1105 int diagonal_ref;
1106
1107 diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
0da71265 1108
579448a0 1109 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);
0da71265 1110
1924f3ce 1111 if(diagonal_ref == ref){
0da71265
MN
1112 *mx= C[0];
1113 *my= C[1];
1114 return;
1115 }
0da71265
MN
1116 }
1117
1118 //RARE
1119 pred_motion(h, n, 2, list, ref, mx, my);
1120}
1121
1122static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
0da71265
MN
1123 const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
1124 const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
1125
5175b937 1126 tprintf("pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
0da71265
MN
1127
1128 if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
1129 || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
1130 || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
1131
1132 *mx = *my = 0;
1133 return;
1134 }
1135
1136 pred_motion(h, 0, 4, 0, 0, mx, my);
1137
1138 return;
1139}
1140
5ad984c9
LM
1141static inline void direct_dist_scale_factor(H264Context * const h){
1142 const int poc = h->s.current_picture_ptr->poc;
1143 const int poc1 = h->ref_list[1][0].poc;
1144 int i;
1145 for(i=0; i<h->ref_count[0]; i++){
1146 int poc0 = h->ref_list[0][i].poc;
1147 int td = clip(poc1 - poc0, -128, 127);
1148 if(td == 0 /* FIXME || pic0 is a long-term ref */){
1149 h->dist_scale_factor[i] = 256;
1150 }else{
1151 int tb = clip(poc - poc0, -128, 127);
1152 int tx = (16384 + (ABS(td) >> 1)) / td;
1153 h->dist_scale_factor[i] = clip((tb*tx + 32) >> 6, -1024, 1023);
1154 }
1155 }
1156}
2f944356
LM
1157static inline void direct_ref_list_init(H264Context * const h){
1158 MpegEncContext * const s = &h->s;
1159 Picture * const ref1 = &h->ref_list[1][0];
1160 Picture * const cur = s->current_picture_ptr;
1161 int list, i, j;
1162 if(cur->pict_type == I_TYPE)
1163 cur->ref_count[0] = 0;
1164 if(cur->pict_type != B_TYPE)
1165 cur->ref_count[1] = 0;
1166 for(list=0; list<2; list++){
1167 cur->ref_count[list] = h->ref_count[list];
1168 for(j=0; j<h->ref_count[list]; j++)
1169 cur->ref_poc[list][j] = h->ref_list[list][j].poc;
1170 }
1171 if(cur->pict_type != B_TYPE || h->direct_spatial_mv_pred)
1172 return;
1173 for(list=0; list<2; list++){
1174 for(i=0; i<ref1->ref_count[list]; i++){
1175 const int poc = ref1->ref_poc[list][i];
1176 h->map_col_to_list0[list][i] = PART_NOT_AVAILABLE;
1177 for(j=0; j<h->ref_count[list]; j++)
1178 if(h->ref_list[list][j].poc == poc){
1179 h->map_col_to_list0[list][i] = j;
1180 break;
1181 }
1182 }
1183 }
1184}
5ad984c9
LM
1185
1186static inline void pred_direct_motion(H264Context * const h, int *mb_type){
1187 MpegEncContext * const s = &h->s;
1188 const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
1189 const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
1190 const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
1191 const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
1192 const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
1193 const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
2f944356 1194 const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy];
5ad984c9
LM
1195 const int is_b8x8 = IS_8X8(*mb_type);
1196 int sub_mb_type;
1197 int i8, i4;
1198
1199 if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
1200 /* FIXME save sub mb types from previous frames (or derive from MVs)
1201 * so we know exactly what block size to use */
1202 sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
3622988f 1203 *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
5ad984c9
LM
1204 }else if(!is_b8x8 && (IS_16X16(mb_type_col) || IS_INTRA(mb_type_col))){
1205 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
1206 *mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
1207 }else{
1208 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
3622988f 1209 *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
5ad984c9
LM
1210 }
1211 if(!is_b8x8)
1212 *mb_type |= MB_TYPE_DIRECT2;
1213
827c91bf
LLL
1214 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);
1215
5ad984c9
LM
1216 if(h->direct_spatial_mv_pred){
1217 int ref[2];
1218 int mv[2][2];
1219 int list;
1220
1221 /* ref = min(neighbors) */
1222 for(list=0; list<2; list++){
1223 int refa = h->ref_cache[list][scan8[0] - 1];
1224 int refb = h->ref_cache[list][scan8[0] - 8];
1225 int refc = h->ref_cache[list][scan8[0] - 8 + 4];
1226 if(refc == -2)
1227 refc = h->ref_cache[list][scan8[0] - 8 - 1];
1228 ref[list] = refa;
1229 if(ref[list] < 0 || (refb < ref[list] && refb >= 0))
1230 ref[list] = refb;
1231 if(ref[list] < 0 || (refc < ref[list] && refc >= 0))
1232 ref[list] = refc;
1233 if(ref[list] < 0)
1234 ref[list] = -1;
1235 }
1236
1237 if(ref[0] < 0 && ref[1] < 0){
1238 ref[0] = ref[1] = 0;
1239 mv[0][0] = mv[0][1] =
1240 mv[1][0] = mv[1][1] = 0;
1241 }else{
1242 for(list=0; list<2; list++){
1243 if(ref[list] >= 0)
1244 pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
1245 else
1246 mv[list][0] = mv[list][1] = 0;
1247 }
1248 }
1249
1250 if(ref[1] < 0){
1251 *mb_type &= ~MB_TYPE_P0L1;
1252 sub_mb_type &= ~MB_TYPE_P0L1;
1253 }else if(ref[0] < 0){
1254 *mb_type &= ~MB_TYPE_P0L0;
1255 sub_mb_type &= ~MB_TYPE_P0L0;
1256 }
1257
1258 if(IS_16X16(*mb_type)){
1259 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref[0], 1);
1260 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, ref[1], 1);
1261 if(!IS_INTRA(mb_type_col) && l1ref0[0] == 0 &&
1262 ABS(l1mv0[0][0]) <= 1 && ABS(l1mv0[0][1]) <= 1){
1263 if(ref[0] > 0)
1264 fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
1265 else
1266 fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
1267 if(ref[1] > 0)
1268 fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
1269 else
1270 fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
1271 }else{
1272 fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
1273 fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
1274 }
1275 }else{
1276 for(i8=0; i8<4; i8++){
1277 const int x8 = i8&1;
1278 const int y8 = i8>>1;
1279
1280 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
1281 continue;
1282 h->sub_mb_type[i8] = sub_mb_type;
1283
1284 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
1285 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
1286 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref[0], 1);
1287 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, ref[1], 1);
1288
1289 /* col_zero_flag */
1290 if(!IS_INTRA(mb_type_col) && l1ref0[x8 + y8*h->b8_stride] == 0){
1291 for(i4=0; i4<4; i4++){
1292 const int16_t *mv_col = l1mv0[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
1293 if(ABS(mv_col[0]) <= 1 && ABS(mv_col[1]) <= 1){
1294 if(ref[0] == 0)
1295 *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
1296 if(ref[1] == 0)
1297 *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
1298 }
1299 }
1300 }
1301 }
1302 }
1303 }else{ /* direct temporal mv pred */
5ad984c9
LM
1304 if(IS_16X16(*mb_type)){
1305 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
1306 if(IS_INTRA(mb_type_col)){
1307 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
1308 fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
1309 fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
1310 }else{
2f944356
LM
1311 const int ref0 = l1ref0[0] >= 0 ? h->map_col_to_list0[0][l1ref0[0]]
1312 : h->map_col_to_list0[1][l1ref1[0]];
5ad984c9
LM
1313 const int dist_scale_factor = h->dist_scale_factor[ref0];
1314 const int16_t *mv_col = l1mv0[0];
1315 int mv_l0[2];
1316 mv_l0[0] = (dist_scale_factor * mv_col[0] + 128) >> 8;
1317 mv_l0[1] = (dist_scale_factor * mv_col[1] + 128) >> 8;
1318 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref0, 1);
1319 fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0],mv_l0[1]), 4);
1320 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);
1321 }
1322 }else{
1323 for(i8=0; i8<4; i8++){
1324 const int x8 = i8&1;
1325 const int y8 = i8>>1;
1326 int ref0, dist_scale_factor;
1327
1328 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
1329 continue;
1330 h->sub_mb_type[i8] = sub_mb_type;
1331 if(IS_INTRA(mb_type_col)){
1332 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
1333 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
1334 fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
1335 fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
1336 continue;
1337 }
1338
1339 ref0 = l1ref0[x8 + y8*h->b8_stride];
2f944356
LM
1340 if(ref0 >= 0)
1341 ref0 = h->map_col_to_list0[0][ref0];
1342 else
1343 ref0 = h->map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]];
5ad984c9
LM
1344 dist_scale_factor = h->dist_scale_factor[ref0];
1345
1346 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
1347 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
1348 for(i4=0; i4<4; i4++){
1349 const int16_t *mv_col = l1mv0[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
1350 int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
1351 mv_l0[0] = (dist_scale_factor * mv_col[0] + 128) >> 8;
1352 mv_l0[1] = (dist_scale_factor * mv_col[1] + 128) >> 8;
1353 *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
1354 pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
1355 }
1356 }
1357 }
1358 }
1359}
1360
0da71265
MN
1361static inline void write_back_motion(H264Context *h, int mb_type){
1362 MpegEncContext * const s = &h->s;
0da71265
MN
1363 const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
1364 const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
1365 int list;
1366
1367 for(list=0; list<2; list++){
1368 int y;
b75e10d5 1369 if(!USES_LIST(mb_type, list)){
3b66c4c5 1370 if(1){ //FIXME skip or never read if mb_type doesn't use it
0da71265
MN
1371 for(y=0; y<4; y++){
1372 *(uint64_t*)s->current_picture.motion_val[list][b_xy + 0 + y*h->b_stride]=
1373 *(uint64_t*)s->current_picture.motion_val[list][b_xy + 2 + y*h->b_stride]= 0;
1374 }
9e528114
LA
1375 if( h->pps.cabac ) {
1376 /* FIXME needed ? */
1377 for(y=0; y<4; y++){
1378 *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]=
1379 *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= 0;
1380 }
1381 }
0da71265 1382 for(y=0; y<2; y++){
0444b023
MN
1383 s->current_picture.ref_index[list][b8_xy + 0 + y*h->b8_stride]=
1384 s->current_picture.ref_index[list][b8_xy + 1 + y*h->b8_stride]= LIST_NOT_USED;
0da71265
MN
1385 }
1386 }
5ad984c9 1387 continue;
0da71265
MN
1388 }
1389
1390 for(y=0; y<4; y++){
1391 *(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];
1392 *(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];
1393 }
9e528114
LA
1394 if( h->pps.cabac ) {
1395 for(y=0; y<4; y++){
1396 *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
1397 *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
1398 }
1399 }
0da71265
MN
1400 for(y=0; y<2; y++){
1401 s->current_picture.ref_index[list][b8_xy + 0 + y*h->b8_stride]= h->ref_cache[list][scan8[0]+0 + 16*y];
1402 s->current_picture.ref_index[list][b8_xy + 1 + y*h->b8_stride]= h->ref_cache[list][scan8[0]+2 + 16*y];
1403 }
1404 }
5ad984c9
LM
1405
1406 if(h->slice_type == B_TYPE && h->pps.cabac){
1407 if(IS_8X8(mb_type)){
1408 h->direct_table[b8_xy+1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
1409 h->direct_table[b8_xy+0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
1410 h->direct_table[b8_xy+1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
1411 }
1412 }
0da71265
MN
1413}
1414
1415/**
1416 * Decodes a network abstraction layer unit.
1417 * @param consumed is the number of bytes used as input
1418 * @param length is the length of the array
3b66c4c5 1419 * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp tailing?
0da71265
MN
1420 * @returns decoded bytes, might be src+1 if no escapes
1421 */
1422static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){
1423 int i, si, di;
1424 uint8_t *dst;
1425
1426// src[0]&0x80; //forbidden bit
1427 h->nal_ref_idc= src[0]>>5;
1428 h->nal_unit_type= src[0]&0x1F;
1429
1430 src++; length--;
1431#if 0
1432 for(i=0; i<length; i++)
1433 printf("%2X ", src[i]);
1434#endif
1435 for(i=0; i+1<length; i+=2){
1436 if(src[i]) continue;
1437 if(i>0 && src[i-1]==0) i--;
1438 if(i+2<length && src[i+1]==0 && src[i+2]<=3){
1439 if(src[i+2]!=3){
1440 /* startcode, so we must be past the end */
1441 length=i;
1442 }
1443 break;
1444 }
1445 }
1446
1447 if(i>=length-1){ //no escaped 0
1448 *dst_length= length;
1449 *consumed= length+1; //+1 for the header
1450 return src;
1451 }
1452
1453 h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length);
1454 dst= h->rbsp_buffer;
1455
3b66c4c5 1456//printf("decoding esc\n");
0da71265
MN
1457 si=di=0;
1458 while(si<length){
1459 //remove escapes (very rare 1:2^22)
1460 if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
1461 if(src[si+2]==3){ //escape
1462 dst[di++]= 0;
1463 dst[di++]= 0;
1464 si+=3;
c8470cc1 1465 continue;
0da71265
MN
1466 }else //next start code
1467 break;
1468 }
1469
1470 dst[di++]= src[si++];
1471 }
1472
1473 *dst_length= di;
1474 *consumed= si + 1;//+1 for the header
1475//FIXME store exact number of bits in the getbitcontext (its needed for decoding)
1476 return dst;
1477}
1478
e5017ab8 1479#if 0
0da71265
MN
1480/**
1481 * @param src the data which should be escaped
1482 * @param dst the target buffer, dst+1 == src is allowed as a special case
1483 * @param length the length of the src data
1484 * @param dst_length the length of the dst array
1485 * @returns length of escaped data in bytes or -1 if an error occured
1486 */
1487static int encode_nal(H264Context *h, uint8_t *dst, uint8_t *src, int length, int dst_length){
1488 int i, escape_count, si, di;
1489 uint8_t *temp;
1490
1491 assert(length>=0);
1492 assert(dst_length>0);
1493
1494 dst[0]= (h->nal_ref_idc<<5) + h->nal_unit_type;
1495
1496 if(length==0) return 1;
1497
1498 escape_count= 0;
1499 for(i=0; i<length; i+=2){
1500 if(src[i]) continue;
1501 if(i>0 && src[i-1]==0)
1502 i--;
1503 if(i+2<length && src[i+1]==0 && src[i+2]<=3){
1504 escape_count++;
1505 i+=2;
1506 }
1507 }
1508
1509 if(escape_count==0){
1510 if(dst+1 != src)
1511 memcpy(dst+1, src, length);
1512 return length + 1;
1513 }
1514
1515 if(length + escape_count + 1> dst_length)
1516 return -1;
1517
1518 //this should be damn rare (hopefully)
1519
1520 h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length + escape_count);
1521 temp= h->rbsp_buffer;
1522//printf("encoding esc\n");
1523
1524 si= 0;
1525 di= 0;
1526 while(si < length){
1527 if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
1528 temp[di++]= 0; si++;
1529 temp[di++]= 0; si++;
1530 temp[di++]= 3;
1531 temp[di++]= src[si++];
1532 }
1533 else
1534 temp[di++]= src[si++];
1535 }
1536 memcpy(dst+1, temp, length+escape_count);
1537
1538 assert(di == length+escape_count);
1539
1540 return di + 1;
1541}
1542
1543/**
1544 * write 1,10,100,1000,... for alignment, yes its exactly inverse to mpeg4
1545 */
1546static void encode_rbsp_trailing(PutBitContext *pb){
1547 int length;
1548 put_bits(pb, 1, 1);
fe455f33 1549 length= (-put_bits_count(pb))&7;
0da71265
MN
1550 if(length) put_bits(pb, length, 0);
1551}
e5017ab8 1552#endif
0da71265
MN
1553
1554/**
1555 * identifies the exact end of the bitstream
1556 * @return the length of the trailing, or 0 if damaged
1557 */
1558static int decode_rbsp_trailing(uint8_t *src){
1559 int v= *src;
1560 int r;
1561
95c26348 1562 tprintf("rbsp trailing %X\n", v);
0da71265
MN
1563
1564 for(r=1; r<9; r++){
1565 if(v&1) return r;
1566 v>>=1;
1567 }
1568 return 0;
1569}
1570
1571/**
1572 * idct tranforms the 16 dc values and dequantize them.
1573 * @param qp quantization parameter
1574 */
1575static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp){
1576 const int qmul= dequant_coeff[qp][0];
1577#define stride 16
1578 int i;
1579 int temp[16]; //FIXME check if this is a good idea
1580 static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
1581 static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
1582
1583//memset(block, 64, 2*256);
1584//return;
1585 for(i=0; i<4; i++){
1586 const int offset= y_offset[i];
1587 const int z0= block[offset+stride*0] + block[offset+stride*4];
1588 const int z1= block[offset+stride*0] - block[offset+stride*4];
1589 const int z2= block[offset+stride*1] - block[offset+stride*5];
1590 const int z3= block[offset+stride*1] + block[offset+stride*5];
1591
1592 temp[4*i+0]= z0+z3;
1593 temp[4*i+1]= z1+z2;
1594 temp[4*i+2]= z1-z2;
1595 temp[4*i+3]= z0-z3;
1596 }
1597
1598 for(i=0; i<4; i++){
1599 const int offset= x_offset[i];
1600 const int z0= temp[4*0+i] + temp[4*2+i];
1601 const int z1= temp[4*0+i] - temp[4*2+i];
1602 const int z2= temp[4*1+i] - temp[4*3+i];
1603 const int z3= temp[4*1+i] + temp[4*3+i];
1604
1605 block[stride*0 +offset]= ((z0 + z3)*qmul + 2)>>2; //FIXME think about merging this into decode_resdual
1606 block[stride*2 +offset]= ((z1 + z2)*qmul + 2)>>2;
1607 block[stride*8 +offset]= ((z1 - z2)*qmul + 2)>>2;
1608 block[stride*10+offset]= ((z0 - z3)*qmul + 2)>>2;
1609 }
1610}
1611
e5017ab8 1612#if 0
0da71265
MN
1613/**
1614 * dct tranforms the 16 dc values.
1615 * @param qp quantization parameter ??? FIXME
1616 */
1617static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
1618// const int qmul= dequant_coeff[qp][0];
1619 int i;
1620 int temp[16]; //FIXME check if this is a good idea
1621 static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
1622 static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
1623
1624 for(i=0; i<4; i++){
1625 const int offset= y_offset[i];
1626 const int z0= block[offset+stride*0] + block[offset+stride*4];
1627 const int z1= block[offset+stride*0] - block[offset+stride*4];
1628 const int z2= block[offset+stride*1] - block[offset+stride*5];
1629 const int z3= block[offset+stride*1] + block[offset+stride*5];
1630
1631 temp[4*i+0]= z0+z3;
1632 temp[4*i+1]= z1+z2;
1633 temp[4*i+2]= z1-z2;
1634 temp[4*i+3]= z0-z3;
1635 }
1636
1637 for(i=0; i<4; i++){
1638 const int offset= x_offset[i];
1639 const int z0= temp[4*0+i] + temp[4*2+i];
1640 const int z1= temp[4*0+i] - temp[4*2+i];
1641 const int z2= temp[4*1+i] - temp[4*3+i];
1642 const int z3= temp[4*1+i] + temp[4*3+i];
1643
1644 block[stride*0 +offset]= (z0 + z3)>>1;
1645 block[stride*2 +offset]= (z1 + z2)>>1;
1646 block[stride*8 +offset]= (z1 - z2)>>1;
1647 block[stride*10+offset]= (z0 - z3)>>1;
1648 }
1649}
e5017ab8
LA
1650#endif
1651
0da71265
MN
1652#undef xStride
1653#undef stride
1654
1655static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp){
1656 const int qmul= dequant_coeff[qp][0];
1657 const int stride= 16*2;
1658 const int xStride= 16;
1659 int a,b,c,d,e;
1660
1661 a= block[stride*0 + xStride*0];
1662 b= block[stride*0 + xStride*1];
1663 c= block[stride*1 + xStride*0];
1664 d= block[stride*1 + xStride*1];
1665
1666 e= a-b;
1667 a= a+b;
1668 b= c-d;
1669 c= c+d;
1670
1671 block[stride*0 + xStride*0]= ((a+c)*qmul + 0)>>1;
1672 block[stride*0 + xStride*1]= ((e+b)*qmul + 0)>>1;
1673 block[stride*1 + xStride*0]= ((a-c)*qmul + 0)>>1;
1674 block[stride*1 + xStride*1]= ((e-b)*qmul + 0)>>1;
1675}
1676
e5017ab8 1677#if 0
0da71265
MN
1678static void chroma_dc_dct_c(DCTELEM *block){
1679 const int stride= 16*2;
1680 const int xStride= 16;
1681 int a,b,c,d,e;
1682
1683 a= block[stride*0 + xStride*0];
1684 b= block[stride*0 + xStride*1];
1685 c= block[stride*1 + xStride*0];
1686 d= block[stride*1 + xStride*1];
1687
1688 e= a-b;
1689 a= a+b;
1690 b= c-d;
1691 c= c+d;
1692
1693 block[stride*0 + xStride*0]= (a+c);
1694 block[stride*0 + xStride*1]= (e+b);
1695 block[stride*1 + xStride*0]= (a-c);
1696 block[stride*1 + xStride*1]= (e-b);
1697}
e5017ab8 1698#endif
0da71265
MN
1699
1700/**
1701 * gets the chroma qp.
1702 */
6ba71fc4 1703static inline int get_chroma_qp(int chroma_qp_index_offset, int qscale){
0da71265 1704
6ba71fc4 1705 return chroma_qp[clip(qscale + chroma_qp_index_offset, 0, 51)];
0da71265
MN
1706}
1707
1708
e5017ab8 1709#if 0
0da71265
MN
1710static void h264_diff_dct_c(DCTELEM *block, uint8_t *src1, uint8_t *src2, int stride){
1711 int i;
1712 //FIXME try int temp instead of block
1713
1714 for(i=0; i<4; i++){
1715 const int d0= src1[0 + i*stride] - src2[0 + i*stride];
1716 const int d1= src1[1 + i*stride] - src2[1 + i*stride];
1717 const int d2= src1[2 + i*stride] - src2[2 + i*stride];
1718 const int d3= src1[3 + i*stride] - src2[3 + i*stride];
1719 const int z0= d0 + d3;
1720 const int z3= d0 - d3;
1721 const int z1= d1 + d2;
1722 const int z2= d1 - d2;
1723
1724 block[0 + 4*i]= z0 + z1;
1725 block[1 + 4*i]= 2*z3 + z2;
1726 block[2 + 4*i]= z0 - z1;
1727 block[3 + 4*i]= z3 - 2*z2;
1728 }
1729
1730 for(i=0; i<4; i++){
1731 const int z0= block[0*4 + i] + block[3*4 + i];
1732 const int z3= block[0*4 + i] - block[3*4 + i];
1733 const int z1= block[1*4 + i] + block[2*4 + i];
1734 const int z2= block[1*4 + i] - block[2*4 + i];
1735
1736 block[0*4 + i]= z0 + z1;
1737 block[1*4 + i]= 2*z3 + z2;
1738 block[2*4 + i]= z0 - z1;
1739 block[3*4 + i]= z3 - 2*z2;
1740 }
1741}
e5017ab8 1742#endif
0da71265 1743
3b66c4c5 1744//FIXME need to check that this doesnt overflow signed 32 bit for low qp, i am not sure, it's very close
0da71265
MN
1745//FIXME check that gcc inlines this (and optimizes intra & seperate_dc stuff away)
1746static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int seperate_dc){
1747 int i;
1748 const int * const quant_table= quant_coeff[qscale];
1749 const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
1750 const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
1751 const unsigned int threshold2= (threshold1<<1);
1752 int last_non_zero;
1753
1754 if(seperate_dc){
1755 if(qscale<=18){
1756 //avoid overflows
1757 const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
1758 const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
1759 const unsigned int dc_threshold2= (dc_threshold1<<1);
1760
1761 int level= block[0]*quant_coeff[qscale+18][0];
1762 if(((unsigned)(level+dc_threshold1))>dc_threshold2){
1763 if(level>0){
1764 level= (dc_bias + level)>>(QUANT_SHIFT-2);
1765 block[0]= level;
1766 }else{
1767 level= (dc_bias - level)>>(QUANT_SHIFT-2);
1768 block[0]= -level;
1769 }
1770// last_non_zero = i;
1771 }else{
1772 block[0]=0;
1773 }
1774 }else{
1775 const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
1776 const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
1777 const unsigned int dc_threshold2= (dc_threshold1<<1);
1778
1779 int level= block[0]*quant_table[0];
1780 if(((unsigned)(level+dc_threshold1))>dc_threshold2){
1781 if(level>0){
1782 level= (dc_bias + level)>>(QUANT_SHIFT+1);
1783 block[0]= level;
1784 }else{
1785 level= (dc_bias - level)>>(QUANT_SHIFT+1);
1786 block[0]= -level;
1787 }
1788// last_non_zero = i;
1789 }else{
1790 block[0]=0;
1791 }
1792 }
1793 last_non_zero= 0;
1794 i=1;
1795 }else{
1796 last_non_zero= -1;
1797 i=0;
1798 }
1799
1800 for(; i<16; i++){
1801 const int j= scantable[i];
1802 int level= block[j]*quant_table[j];
1803
1804// if( bias+level >= (1<<(QMAT_SHIFT - 3))
1805// || bias-level >= (1<<(QMAT_SHIFT - 3))){
1806 if(((unsigned)(level+threshold1))>threshold2){
1807 if(level>0){
1808 level= (bias + level)>>QUANT_SHIFT;
1809 block[j]= level;
1810 }else{
1811 level= (bias - level)>>QUANT_SHIFT;
1812 block[j]= -level;
1813 }
1814 last_non_zero = i;
1815 }else{
1816 block[j]=0;
1817 }
1818 }
1819
1820 return last_non_zero;
1821}
1822
1823static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){
1824 const uint32_t a= ((uint32_t*)(src-stride))[0];
1825 ((uint32_t*)(src+0*stride))[0]= a;
1826 ((uint32_t*)(src+1*stride))[0]= a;
1827 ((uint32_t*)(src+2*stride))[0]= a;
1828 ((uint32_t*)(src+3*stride))[0]= a;
1829}
1830
1831static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){
1832 ((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101;
1833 ((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101;
1834 ((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101;
1835 ((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101;
1836}
1837
1838static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride){
1839 const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
1840 + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
1841
1842 ((uint32_t*)(src+0*stride))[0]=
1843 ((uint32_t*)(src+1*stride))[0]=
1844 ((uint32_t*)(src+2*stride))[0]=
1845 ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
1846}
1847
1848static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride){
1849 const int dc= ( src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
1850
1851 ((uint32_t*)(src+0*stride))[0]=
1852 ((uint32_t*)(src+1*stride))[0]=
1853 ((uint32_t*)(src+2*stride))[0]=
1854 ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
1855}
1856
1857static void pred4x4_top_dc_c(uint8_t *src, uint8_t *topright, int stride){
1858 const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2;
1859
1860 ((uint32_t*)(src+0*stride))[0]=
1861 ((uint32_t*)(src+1*stride))[0]=
1862 ((uint32_t*)(src+2*stride))[0]=
1863 ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
1864}
1865
1866static void pred4x4_128_dc_c(uint8_t *src, uint8_t *topright, int stride){
1867 ((uint32_t*)(src+0*stride))[0]=
1868 ((uint32_t*)(src+1*stride))[0]=
1869 ((uint32_t*)(src+2*stride))[0]=
1870 ((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U;
1871}
1872
1873
1874#define LOAD_TOP_RIGHT_EDGE\
1875 const int t4= topright[0];\
1876 const int t5= topright[1];\
1877 const int t6= topright[2];\
1878 const int t7= topright[3];\
1879
1880#define LOAD_LEFT_EDGE\
1881 const int l0= src[-1+0*stride];\
1882 const int l1= src[-1+1*stride];\
1883 const int l2= src[-1+2*stride];\
1884 const int l3= src[-1+3*stride];\
1885
1886#define LOAD_TOP_EDGE\
1887 const int t0= src[ 0-1*stride];\
1888 const int t1= src[ 1-1*stride];\
1889 const int t2= src[ 2-1*stride];\
1890 const int t3= src[ 3-1*stride];\
1891
1892static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){
1893 const int lt= src[-1-1*stride];
1894 LOAD_TOP_EDGE
1895 LOAD_LEFT_EDGE
1896
1897 src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
1898 src[0+2*stride]=
1899 src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
1900 src[0+1*stride]=
1901 src[1+2*stride]=
1902 src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
1903 src[0+0*stride]=
1904 src[1+1*stride]=
1905 src[2+2*stride]=
1906 src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
1907 src[1+0*stride]=
1908 src[2+1*stride]=
1909 src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
1910 src[2+0*stride]=
1911 src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
1912 src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
4cfbf61b 1913}
0da71265
MN
1914
1915static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){
1916 LOAD_TOP_EDGE
1917 LOAD_TOP_RIGHT_EDGE
1918// LOAD_LEFT_EDGE
1919
1920 src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
1921 src[1+0*stride]=
1922 src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
1923 src[2+0*stride]=
1924 src[1+1*stride]=
1925 src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
1926 src[3+0*stride]=
1927 src[2+1*stride]=
1928 src[1+2*stride]=
1929 src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
1930 src[3+1*stride]=
1931 src[2+2*stride]=
1932 src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
1933 src[3+2*stride]=
1934 src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
1935 src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
4cfbf61b 1936}
0da71265
MN
1937
1938static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
1939 const int lt= src[-1-1*stride];
1940 LOAD_TOP_EDGE
1941 LOAD_LEFT_EDGE
1942 const __attribute__((unused)) int unu= l3;
1943
1944 src[0+0*stride]=
1945 src[1+2*stride]=(lt + t0 + 1)>>1;
1946 src[1+0*stride]=
1947 src[2+2*stride]=(t0 + t1 + 1)>>1;
1948 src[2+0*stride]=
1949 src[3+2*stride]=(t1 + t2 + 1)>>1;
1950 src[3+0*stride]=(t2 + t3 + 1)>>1;
1951 src[0+1*stride]=
1952 src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
1953 src[1+1*stride]=
1954 src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
1955 src[2+1*stride]=
1956 src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
1957 src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
1958 src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
1959 src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
4cfbf61b 1960}
0da71265
MN
1961
1962static void pred4x4_vertical_left_c(uint8_t *src, uint8_t *topright, int stride){
1963 LOAD_TOP_EDGE
1964 LOAD_TOP_RIGHT_EDGE
1965 const __attribute__((unused)) int unu= t7;
1966
1967 src[0+0*stride]=(t0 + t1 + 1)>>1;
1968 src[1+0*stride]=
1969 src[0+2*stride]=(t1 + t2 + 1)>>1;
1970 src[2+0*stride]=
1971 src[1+2*stride]=(t2 + t3 + 1)>>1;
1972 src[3+0*stride]=
1973 src[2+2*stride]=(t3 + t4+ 1)>>1;
1974 src[3+2*stride]=(t4 + t5+ 1)>>1;
1975 src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
1976 src[1+1*stride]=
1977 src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
1978 src[2+1*stride]=
1979 src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
1980 src[3+1*stride]=
1981 src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
1982 src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
4cfbf61b 1983}
0da71265
MN
1984
1985static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){
1986 LOAD_LEFT_EDGE
1987
1988 src[0+0*stride]=(l0 + l1 + 1)>>1;
1989 src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
1990 src[2+0*stride]=
1991 src[0+1*stride]=(l1 + l2 + 1)>>1;
1992 src[3+0*stride]=
1993 src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
1994 src[2+1*stride]=
1995 src[0+2*stride]=(l2 + l3 + 1)>>1;
1996 src[3+1*stride]=
1997 src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
1998 src[3+2*stride]=
1999 src[1+3*stride]=
2000 src[0+3*stride]=
2001 src[2+2*stride]=
2002 src[2+3*stride]=
2003 src[3+3*stride]=l3;
4cfbf61b 2004}
0da71265
MN
2005
2006static void pred4x4_horizontal_down_c(uint8_t *src, uint8_t *topright, int stride){
2007 const int lt= src[-1-1*stride];
2008 LOAD_TOP_EDGE
2009 LOAD_LEFT_EDGE
2010 const __attribute__((unused)) int unu= t3;
2011
2012 src[0+0*stride]=
2013 src[2+1*stride]=(lt + l0 + 1)>>1;
2014 src[1+0*stride]=
2015 src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
2016 src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
2017 src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
2018 src[0+1*stride]=
2019 src[2+2*stride]=(l0 + l1 + 1)>>1;
2020 src[1+1*stride]=
2021 src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
2022 src[0+2*stride]=
2023 src[2+3*stride]=(l1 + l2+ 1)>>1;
2024 src[1+2*stride]=
2025 src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
2026 src[0+3*stride]=(l2 + l3 + 1)>>1;
2027 src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
4cfbf61b 2028}
0da71265
MN
2029
2030static void pred16x16_vertical_c(uint8_t *src, int stride){
2031 int i;
2032 const uint32_t a= ((uint32_t*)(src-stride))[0];
2033 const uint32_t b= ((uint32_t*)(src-stride))[1];
2034 const uint32_t c= ((uint32_t*)(src-stride))[2];
2035 const uint32_t d= ((uint32_t*)(src-stride))[3];
2036
2037 for(i=0; i<16; i++){
2038 ((uint32_t*)(src+i*stride))[0]= a;
2039 ((uint32_t*)(src+i*stride))[1]= b;
2040 ((uint32_t*)(src+i*stride))[2]= c;
2041 ((uint32_t*)(src+i*stride))[3]= d;
2042 }
2043}
2044
2045static void pred16x16_horizontal_c(uint8_t *src, int stride){
2046 int i;
2047
2048 for(i=0; i<16; i++){
2049 ((uint32_t*)(src+i*stride))[0]=
2050 ((uint32_t*)(src+i*stride))[1]=
2051 ((uint32_t*)(src+i*stride))[2]=
2052 ((uint32_t*)(src+i*stride))[3]= src[-1+i*stride]*0x01010101;
2053 }
2054}
2055
2056static void pred16x16_dc_c(uint8_t *src, int stride){
2057 int i, dc=0;
2058
2059 for(i=0;i<16; i++){
2060 dc+= src[-1+i*stride];
2061 }
2062
2063 for(i=0;i<16; i++){
2064 dc+= src[i-stride];
2065 }
2066
2067 dc= 0x01010101*((dc + 16)>>5);
2068
2069 for(i=0; i<16; i++){
2070 ((uint32_t*)(src+i*stride))[0]=
2071 ((uint32_t*)(src+i*stride))[1]=
2072 ((uint32_t*)(src+i*stride))[2]=
2073 ((uint32_t*)(src+i*stride))[3]= dc;
2074 }
2075}
2076
2077static void pred16x16_left_dc_c(uint8_t *src, int stride){
2078 int i, dc=0;
2079
2080 for(i=0;i<16; i++){
2081 dc+= src[-1+i*stride];
2082 }
2083
2084 dc= 0x01010101*((dc + 8)>>4);
2085
2086 for(i=0; i<16; i++){
2087 ((uint32_t*)(src+i*stride))[0]=
2088 ((uint32_t*)(src+i*stride))[1]=
2089 ((uint32_t*)(src+i*stride))[2]=
2090 ((uint32_t*)(src+i*stride))[3]= dc;
2091 }
2092}
2093
2094static void pred16x16_top_dc_c(uint8_t *src, int stride){
2095 int i, dc=0;
2096
2097 for(i=0;i<16; i++){
2098 dc+= src[i-stride];
2099 }
2100 dc= 0x01010101*((dc + 8)>>4);
2101
2102 for(i=0; i<16; i++){
2103 ((uint32_t*)(src+i*stride))[0]=
2104 ((uint32_t*)(src+i*stride))[1]=
2105 ((uint32_t*)(src+i*stride))[2]=
2106 ((uint32_t*)(src+i*stride))[3]= dc;
2107 }
2108}
2109
2110static void pred16x16_128_dc_c(uint8_t *src, int stride){
2111 int i;
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]= 0x01010101U*128U;
2118 }
2119}
2120
8b82a956 2121static inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){
30f73fc7
MN
2122 int i, j, k;
2123 int a;
2124 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2125 const uint8_t * const src0 = src+7-stride;
2126 const uint8_t *src1 = src+8*stride-1;
2127 const uint8_t *src2 = src1-2*stride; // == src+6*stride-1;
2128 int H = src0[1] - src0[-1];
2129 int V = src1[0] - src2[ 0];
2130 for(k=2; k<=8; ++k) {
2131 src1 += stride; src2 -= stride;
2132 H += k*(src0[k] - src0[-k]);
2133 V += k*(src1[0] - src2[ 0]);
2134 }
8b82a956
MN
2135 if(svq3){
2136 H = ( 5*(H/4) ) / 16;
2137 V = ( 5*(V/4) ) / 16;
2e26c8d2
MM
2138
2139 /* required for 100% accuracy */
2140 i = H; H = V; V = i;
8b82a956
MN
2141 }else{
2142 H = ( 5*H+32 ) >> 6;
2143 V = ( 5*V+32 ) >> 6;
2144 }
30f73fc7
MN
2145
2146 a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
2147 for(j=16; j>0; --j) {
2148 int b = a;
2149 a += V;
2150 for(i=-16; i<0; i+=4) {
2151 src[16+i] = cm[ (b ) >> 5 ];
2152 src[17+i] = cm[ (b+ H) >> 5 ];
2153 src[18+i] = cm[ (b+2*H) >> 5 ];
2154 src[19+i] = cm[ (b+3*H) >> 5 ];
2155 b += 4*H;
0da71265 2156 }
30f73fc7
MN
2157 src += stride;
2158 }
0da71265
MN
2159}
2160
8b82a956
MN
2161static void pred16x16_plane_c(uint8_t *src, int stride){
2162 pred16x16_plane_compat_c(src, stride, 0);
2163}
2164
0da71265
MN
2165static void pred8x8_vertical_c(uint8_t *src, int stride){
2166 int i;
2167 const uint32_t a= ((uint32_t*)(src-stride))[0];
2168 const uint32_t b= ((uint32_t*)(src-stride))[1];
2169
2170 for(i=0; i<8; i++){
2171 ((uint32_t*)(src+i*stride))[0]= a;
2172 ((uint32_t*)(src+i*stride))[1]= b;
2173 }
2174}
2175
2176static void pred8x8_horizontal_c(uint8_t *src, int stride){
2177 int i;
2178
2179 for(i=0; i<8; i++){
2180 ((uint32_t*)(src+i*stride))[0]=
2181 ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101;
2182 }
2183}
2184
2185static void pred8x8_128_dc_c(uint8_t *src, int stride){
2186 int i;
2187
2188 for(i=0; i<4; i++){
2189 ((uint32_t*)(src+i*stride))[0]=
2190 ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
2191 }
2192 for(i=4; i<8; i++){
2193 ((uint32_t*)(src+i*stride))[0]=
2194 ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
2195 }
2196}
2197
2198static void pred8x8_left_dc_c(uint8_t *src, int stride){
2199 int i;
2200 int dc0, dc2;
2201
2202 dc0=dc2=0;
2203 for(i=0;i<4; i++){
2204 dc0+= src[-1+i*stride];
2205 dc2+= src[-1+(i+4)*stride];
2206 }
2207 dc0= 0x01010101*((dc0 + 2)>>2);
2208 dc2= 0x01010101*((dc2 + 2)>>2);
2209
2210 for(i=0; i<4; i++){
2211 ((uint32_t*)(src+i*stride))[0]=
2212 ((uint32_t*)(src+i*stride))[1]= dc0;
2213 }
2214 for(i=4; i<8; i++){
2215 ((uint32_t*)(src+i*stride))[0]=
2216 ((uint32_t*)(src+i*stride))[1]= dc2;
2217 }
2218}
2219
2220static void pred8x8_top_dc_c(uint8_t *src, int stride){
2221 int i;
2222 int dc0, dc1;
2223
2224 dc0=dc1=0;
2225 for(i=0;i<4; i++){
2226 dc0+= src[i-stride];
2227 dc1+= src[4+i-stride];
2228 }
2229 dc0= 0x01010101*((dc0 + 2)>>2);
2230 dc1= 0x01010101*((dc1 + 2)>>2);
2231
2232 for(i=0; i<4; i++){
2233 ((uint32_t*)(src+i*stride))[0]= dc0;
2234 ((uint32_t*)(src+i*stride))[1]= dc1;
2235 }
2236 for(i=4; i<8; i++){
2237 ((uint32_t*)(src+i*stride))[0]= dc0;
2238 ((uint32_t*)(src+i*stride))[1]= dc1;
2239 }
2240}
2241
2242
2243static void pred8x8_dc_c(uint8_t *src, int stride){
2244 int i;
2245 int dc0, dc1, dc2, dc3;
2246
2247 dc0=dc1=dc2=0;
2248 for(i=0;i<4; i++){
2249 dc0+= src[-1+i*stride] + src[i-stride];
2250 dc1+= src[4+i-stride];
2251 dc2+= src[-1+(i+4)*stride];
2252 }
2253 dc3= 0x01010101*((dc1 + dc2 + 4)>>3);
2254 dc0= 0x01010101*((dc0 + 4)>>3);
2255 dc1= 0x01010101*((dc1 + 2)>>2);
2256 dc2= 0x01010101*((dc2 + 2)>>2);
2257
2258 for(i=0; i<4; i++){
2259 ((uint32_t*)(src+i*stride))[0]= dc0;
2260 ((uint32_t*)(src+i*stride))[1]= dc1;
2261 }
2262 for(i=4; i<8; i++){
2263 ((uint32_t*)(src+i*stride))[0]= dc2;
2264 ((uint32_t*)(src+i*stride))[1]= dc3;
2265 }
2266}
2267
2268static void pred8x8_plane_c(uint8_t *src, int stride){
30f73fc7
MN
2269 int j, k;
2270 int a;
2271 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2272 const uint8_t * const src0 = src+3-stride;
2273 const uint8_t *src1 = src+4*stride-1;
2274 const uint8_t *src2 = src1-2*stride; // == src+2*stride-1;
2275 int H = src0[1] - src0[-1];
2276 int V = src1[0] - src2[ 0];
2277 for(k=2; k<=4; ++k) {
2278 src1 += stride; src2 -= stride;
2279 H += k*(src0[k] - src0[-k]);
2280 V += k*(src1[0] - src2[ 0]);
2281 }
2282 H = ( 17*H+16 ) >> 5;
2283 V = ( 17*V+16 ) >> 5;
2284
2285 a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
2286 for(j=8; j>0; --j) {
2287 int b = a;
2288 a += V;
2289 src[0] = cm[ (b ) >> 5 ];
2290 src[1] = cm[ (b+ H) >> 5 ];
2291 src[2] = cm[ (b+2*H) >> 5 ];
2292 src[3] = cm[ (b+3*H) >> 5 ];
2293 src[4] = cm[ (b+4*H) >> 5 ];
2294 src[5] = cm[ (b+5*H) >> 5 ];
2295 src[6] = cm[ (b+6*H) >> 5 ];
2296 src[7] = cm[ (b+7*H) >> 5 ];
2297 src += stride;
2298 }
0da71265
MN
2299}
2300
2301static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
2302 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2303 int src_x_offset, int src_y_offset,
2304 qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
2305 MpegEncContext * const s = &h->s;
2306 const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
2307 const int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
2308 const int luma_xy= (mx&3) + ((my&3)<<2);
2309 uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*s->linesize;
2310 uint8_t * src_cb= pic->data[1] + (mx>>3) + (my>>3)*s->uvlinesize;
2311 uint8_t * src_cr= pic->data[2] + (mx>>3) + (my>>3)*s->uvlinesize;
2312 int extra_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16; //FIXME increase edge?, IMHO not worth it
2313 int extra_height= extra_width;
2314 int emu=0;
2315 const int full_mx= mx>>2;
2316 const int full_my= my>>2;
2317
2318 assert(pic->data[0]);
2319
2320 if(mx&7) extra_width -= 3;
2321 if(my&7) extra_height -= 3;
2322
2323 if( full_mx < 0-extra_width
2324 || full_my < 0-extra_height
2325 || full_mx + 16/*FIXME*/ > s->width + extra_width
2326 || full_my + 16/*FIXME*/ > s->height + extra_height){
c009df3f 2327 ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*s->linesize, s->linesize, 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, s->width, s->height);
0da71265
MN
2328 src_y= s->edge_emu_buffer + 2 + 2*s->linesize;
2329 emu=1;
2330 }
2331
2332 qpix_op[luma_xy](dest_y, src_y, s->linesize); //FIXME try variable height perhaps?
2333 if(!square){
2334 qpix_op[luma_xy](dest_y + delta, src_y + delta, s->linesize);
2335 }
2336
2337 if(s->flags&CODEC_FLAG_GRAY) return;
2338
2339 if(emu){
c009df3f 2340 ff_emulated_edge_mc(s->edge_emu_buffer, src_cb, s->uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), s->width>>1, s->height>>1);
0da71265
MN
2341 src_cb= s->edge_emu_buffer;
2342 }
2343 chroma_op(dest_cb, src_cb, s->uvlinesize, chroma_height, mx&7, my&7);
2344
2345 if(emu){
c009df3f 2346 ff_emulated_edge_mc(s->edge_emu_buffer, src_cr, s->uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), s->width>>1, s->height>>1);
0da71265
MN
2347 src_cr= s->edge_emu_buffer;
2348 }
2349 chroma_op(dest_cr, src_cr, s->uvlinesize, chroma_height, mx&7, my&7);
2350}
2351
9f2d1b4f 2352static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
0da71265
MN
2353 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2354 int x_offset, int y_offset,
2355 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
2356 qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
2357 int list0, int list1){
2358 MpegEncContext * const s = &h->s;
2359 qpel_mc_func *qpix_op= qpix_put;
2360 h264_chroma_mc_func chroma_op= chroma_put;
2361
2362 dest_y += 2*x_offset + 2*y_offset*s-> linesize;
2363 dest_cb += x_offset + y_offset*s->uvlinesize;
2364 dest_cr += x_offset + y_offset*s->uvlinesize;
2365 x_offset += 8*s->mb_x;
2366 y_offset += 8*s->mb_y;
2367
2368 if(list0){
1924f3ce 2369 Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
0da71265
MN
2370 mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
2371 dest_y, dest_cb, dest_cr, x_offset, y_offset,
2372 qpix_op, chroma_op);
2373
2374 qpix_op= qpix_avg;
2375 chroma_op= chroma_avg;
2376 }
2377
2378 if(list1){
1924f3ce 2379 Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
0da71265
MN
2380 mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
2381 dest_y, dest_cb, dest_cr, x_offset, y_offset,
2382 qpix_op, chroma_op);
2383 }
2384}
2385
9f2d1b4f
LM
2386static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
2387 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2388 int x_offset, int y_offset,
2389 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
2390 h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
2391 h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
2392 int list0, int list1){
2393 MpegEncContext * const s = &h->s;
2394
2395 dest_y += 2*x_offset + 2*y_offset*s-> linesize;
2396 dest_cb += x_offset + y_offset*s->uvlinesize;
2397 dest_cr += x_offset + y_offset*s->uvlinesize;
2398 x_offset += 8*s->mb_x;
2399 y_offset += 8*s->mb_y;
2400
2401 if(list0 && list1){
2402 /* don't optimize for luma-only case, since B-frames usually
2403 * use implicit weights => chroma too. */
2404 uint8_t *tmp_cb = s->obmc_scratchpad;
2405 uint8_t *tmp_cr = tmp_cb + 8*s->uvlinesize;
2406 uint8_t *tmp_y = tmp_cr + 8*s->uvlinesize;
2407 int refn0 = h->ref_cache[0][ scan8[n] ];
2408 int refn1 = h->ref_cache[1][ scan8[n] ];
2409
2410 mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
2411 dest_y, dest_cb, dest_cr,
2412 x_offset, y_offset, qpix_put, chroma_put);
2413 mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
2414 tmp_y, tmp_cb, tmp_cr,
2415 x_offset, y_offset, qpix_put, chroma_put);
2416
2417 if(h->use_weight == 2){
2418 int weight0 = h->implicit_weight[refn0][refn1];
2419 int weight1 = 64 - weight0;
2420 luma_weight_avg( dest_y, tmp_y, s-> linesize, 5, weight0, weight1, 0, 0);
2421 chroma_weight_avg(dest_cb, tmp_cb, s->uvlinesize, 5, weight0, weight1, 0, 0);
2422 chroma_weight_avg(dest_cr, tmp_cr, s->uvlinesize, 5, weight0, weight1, 0, 0);
2423 }else{
2424 luma_weight_avg(dest_y, tmp_y, s->linesize, h->luma_log2_weight_denom,
2425 h->luma_weight[0][refn0], h->luma_weight[1][refn1],
2426 h->luma_offset[0][refn0], h->luma_offset[1][refn1]);
2427 chroma_weight_avg(dest_cb, tmp_cb, s->uvlinesize, h->chroma_log2_weight_denom,
2428 h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
2429 h->chroma_offset[0][refn0][0], h->chroma_offset[1][refn1][0]);
2430 chroma_weight_avg(dest_cr, tmp_cr, s->uvlinesize, h->chroma_log2_weight_denom,
2431 h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
2432 h->chroma_offset[0][refn0][1], h->chroma_offset[1][refn1][1]);
2433 }
2434 }else{
2435 int list = list1 ? 1 : 0;
2436 int refn = h->ref_cache[list][ scan8[n] ];
2437 Picture *ref= &h->ref_list[list][refn];
2438 mc_dir_part(h, ref, n, square, chroma_height, delta, list,
2439 dest_y, dest_cb, dest_cr, x_offset, y_offset,
2440 qpix_put, chroma_put);
2441
2442 luma_weight_op(dest_y, s->linesize, h->luma_log2_weight_denom,
2443 h->luma_weight[list][refn], h->luma_offset[list][refn]);
2444 if(h->use_weight_chroma){
2445 chroma_weight_op(dest_cb, s->uvlinesize, h->chroma_log2_weight_denom,
2446 h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
2447 chroma_weight_op(dest_cr, s->uvlinesize, h->chroma_log2_weight_denom,
2448 h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
2449 }
2450 }
2451}
2452
2453static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
2454 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2455 int x_offset, int y_offset,
2456 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
2457 qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
2458 h264_weight_func *weight_op, h264_biweight_func *weight_avg,
2459 int list0, int list1){
2460 if((h->use_weight==2 && list0 && list1
2461 && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
2462 || h->use_weight==1)
2463 mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
2464 x_offset, y_offset, qpix_put, chroma_put,
2465 weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
2466 else
2467 mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
2468 x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
2469}
2470
0da71265
MN
2471static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2472 qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
9f2d1b4f
LM
2473 qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
2474 h264_weight_func *weight_op, h264_biweight_func *weight_avg){
0da71265 2475 MpegEncContext * const s = &h->s;
7bc9090a 2476 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
0da71265
MN
2477 const int mb_type= s->current_picture.mb_type[mb_xy];
2478
2479 assert(IS_INTER(mb_type));
2480
2481 if(IS_16X16(mb_type)){
2482 mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
2483 qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
9f2d1b4f 2484 &weight_op[0], &weight_avg[0],
0da71265
MN
2485 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
2486 }else if(IS_16X8(mb_type)){
2487 mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
2488 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
9f2d1b4f 2489 &weight_op[1], &weight_avg[1],
0da71265
MN
2490 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
2491 mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
2492 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
9f2d1b4f 2493 &weight_op[1], &weight_avg[1],
0da71265
MN
2494 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
2495 }else if(IS_8X16(mb_type)){
2496 mc_part(h, 0, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 0, 0,
2497 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
9f2d1b4f 2498 &weight_op[2], &weight_avg[2],
0da71265
MN
2499 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
2500 mc_part(h, 4, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 4, 0,
2501 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
9f2d1b4f 2502 &weight_op[2], &weight_avg[2],
0da71265
MN
2503 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
2504 }else{
2505 int i;
2506
2507 assert(IS_8X8(mb_type));
2508
2509 for(i=0; i<4; i++){
2510 const int sub_mb_type= h->sub_mb_type[i];
2511 const int n= 4*i;
2512 int x_offset= (i&1)<<2;
2513 int y_offset= (i&2)<<1;
2514
2515 if(IS_SUB_8X8(sub_mb_type)){
2516 mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
2517 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
9f2d1b4f 2518 &weight_op[3], &weight_avg[3],
0da71265
MN
2519 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2520 }else if(IS_SUB_8X4(sub_mb_type)){
2521 mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
2522 qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
9f2d1b4f 2523 &weight_op[4], &weight_avg[4],
0da71265
MN
2524 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2525 mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
2526 qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
9f2d1b4f 2527 &weight_op[4], &weight_avg[4],
0da71265
MN
2528 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2529 }else if(IS_SUB_4X8(sub_mb_type)){
2530 mc_part(h, n , 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
2531 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
9f2d1b4f 2532 &weight_op[5], &weight_avg[5],
0da71265
MN
2533 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2534 mc_part(h, n+1, 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
2535 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
9f2d1b4f 2536 &weight_op[5], &weight_avg[5],
0da71265
MN
2537 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2538 }else{
2539 int j;
2540 assert(IS_SUB_4X4(sub_mb_type));
2541 for(j=0; j<4; j++){
2542 int sub_x_offset= x_offset + 2*(j&1);
2543 int sub_y_offset= y_offset + (j&2);
2544 mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
2545 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
9f2d1b4f 2546 &weight_op[6], &weight_avg[6],
0da71265
MN
2547 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2548 }
2549 }
2550 }
2551 }
2552}
2553
2554static void decode_init_vlc(H264Context *h){
2555 static int done = 0;
2556
2557 if (!done) {
2558 int i;
2559 done = 1;
2560
2561 init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
2562 &chroma_dc_coeff_token_len [0], 1, 1,
073c2593 2563 &chroma_dc_coeff_token_bits[0], 1, 1, 1);
0da71265
MN
2564
2565 for(i=0; i<4; i++){
2566 init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
2567 &coeff_token_len [i][0], 1, 1,
073c2593 2568 &coeff_token_bits[i][0], 1, 1, 1);
0da71265
MN
2569 }
2570
2571 for(i=0; i<3; i++){
2572 init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
2573 &chroma_dc_total_zeros_len [i][0], 1, 1,
073c2593 2574 &chroma_dc_total_zeros_bits[i][0], 1, 1, 1);
0da71265
MN
2575 }
2576 for(i=0; i<15; i++){
2577 init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
2578 &total_zeros_len [i][0], 1, 1,
073c2593 2579 &total_zeros_bits[i][0], 1, 1, 1);
0da71265
MN
2580 }
2581
2582 for(i=0; i<6; i++){
2583 init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
2584 &run_len [i][0], 1, 1,
073c2593 2585 &run_bits[i][0], 1, 1, 1);
0da71265
MN
2586 }
2587 init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
2588 &run_len [6][0], 1, 1,
073c2593 2589 &run_bits[6][0], 1, 1, 1);
0da71265
MN
2590 }
2591}
2592
2593/**
2594 * Sets the intra prediction function pointers.
2595 */
2596static void init_pred_ptrs(H264Context *h){
2597// MpegEncContext * const s = &h->s;
2598
2599 h->pred4x4[VERT_PRED ]= pred4x4_vertical_c;
2600 h->pred4x4[HOR_PRED ]= pred4x4_horizontal_c;
2601 h->pred4x4[DC_PRED ]= pred4x4_dc_c;
2602 h->pred4x4[DIAG_DOWN_LEFT_PRED ]= pred4x4_down_left_c;
2603 h->pred4x4[DIAG_DOWN_RIGHT_PRED]= pred4x4_down_right_c;
2604 h->pred4x4[VERT_RIGHT_PRED ]= pred4x4_vertical_right_c;
2605 h->pred4x4[HOR_DOWN_PRED ]= pred4x4_horizontal_down_c;
2606 h->pred4x4[VERT_LEFT_PRED ]= pred4x4_vertical_left_c;
2607 h->pred4x4[HOR_UP_PRED ]= pred4x4_horizontal_up_c;
2608 h->pred4x4[LEFT_DC_PRED ]= pred4x4_left_dc_c;
2609 h->pred4x4[TOP_DC_PRED ]= pred4x4_top_dc_c;
2610 h->pred4x4[DC_128_PRED ]= pred4x4_128_dc_c;
2611
2612 h->pred8x8[DC_PRED8x8 ]= pred8x8_dc_c;
2613 h->pred8x8[VERT_PRED8x8 ]= pred8x8_vertical_c;
2614 h->pred8x8[HOR_PRED8x8 ]= pred8x8_horizontal_c;
2615 h->pred8x8[PLANE_PRED8x8 ]= pred8x8_plane_c;
2616 h->pred8x8[LEFT_DC_PRED8x8]= pred8x8_left_dc_c;
2617 h->pred8x8[TOP_DC_PRED8x8 ]= pred8x8_top_dc_c;
2618 h->pred8x8[DC_128_PRED8x8 ]= pred8x8_128_dc_c;
2619
2620 h->pred16x16[DC_PRED8x8 ]= pred16x16_dc_c;
2621 h->pred16x16[VERT_PRED8x8 ]= pred16x16_vertical_c;
2622 h->pred16x16[HOR_PRED8x8 ]= pred16x16_horizontal_c;
2623 h->pred16x16[PLANE_PRED8x8 ]= pred16x16_plane_c;
2624 h->pred16x16[LEFT_DC_PRED8x8]= pred16x16_left_dc_c;
2625 h->pred16x16[TOP_DC_PRED8x8 ]= pred16x16_top_dc_c;
2626 h->pred16x16[DC_128_PRED8x8 ]= pred16x16_128_dc_c;
2627}
2628
0da71265 2629static void free_tables(H264Context *h){
0da71265 2630 av_freep(&h->intra4x4_pred_mode);
e5017ab8
LA
2631 av_freep(&h->chroma_pred_mode_table);
2632 av_freep(&h->cbp_table);
9e528114
LA
2633 av_freep(&h->mvd_table[0]);
2634 av_freep(&h->mvd_table[1]);
5ad984c9 2635 av_freep(&h->direct_table);
0da71265
MN
2636 av_freep(&h->non_zero_count);
2637 av_freep(&h->slice_table_base);
6ba71fc4
LLL
2638 av_freep(&h->top_borders[1]);
2639 av_freep(&h->top_borders[0]);
0da71265 2640 h->slice_table= NULL;
e5017ab8 2641
0da71265
MN
2642 av_freep(&h->mb2b_xy);
2643 av_freep(&h->mb2b8_xy);
9f2d1b4f
LM
2644
2645 av_freep(&h->s.obmc_scratchpad);
0da71265
MN
2646}
2647
2648/**
2649 * allocates tables.
3b66c4c5 2650 * needs width/height
0da71265
MN
2651 */
2652static int alloc_tables(H264Context *h){
2653 MpegEncContext * const s = &h->s;
7bc9090a 2654 const int big_mb_num= s->mb_stride * (s->mb_height+1);
0da71265
MN
2655 int x,y;
2656
2657 CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
e5017ab8 2658
53c05b1e 2659 CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
0da71265 2660 CHECKED_ALLOCZ(h->slice_table_base , big_mb_num * sizeof(uint8_t))
6ba71fc4
LLL
2661 CHECKED_ALLOCZ(h->top_borders[0] , s->mb_width * (16+8+8) * sizeof(uint8_t))
2662 CHECKED_ALLOCZ(h->top_borders[1] , s->mb_width * (16+8+8) * sizeof(uint8_t))
5d0e4cb8 2663 CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
0da71265 2664
e5017ab8
LA
2665 if( h->pps.cabac ) {
2666 CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
9e528114
LA
2667 CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
2668 CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t));
5ad984c9 2669 CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t));
e5017ab8
LA
2670 }
2671
0da71265 2672 memset(h->slice_table_base, -1, big_mb_num * sizeof(uint8_t));
7bc9090a 2673 h->slice_table= h->slice_table_base + s->mb_stride + 1;
0da71265 2674
a55f20bd
LM
2675 CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint32_t));
2676 CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint32_t));
0da71265
MN
2677 for(y=0; y<s->mb_height; y++){
2678 for(x=0; x<s->mb_width; x++){
7bc9090a 2679 const int mb_xy= x + y*s->mb_stride;
0da71265
MN
2680 const int b_xy = 4*x + 4*y*h->b_stride;
2681 const int b8_xy= 2*x + 2*y*h->b8_stride;
2682
2683 h->mb2b_xy [mb_xy]= b_xy;
2684 h->mb2b8_xy[mb_xy]= b8_xy;
2685 }
2686 }
9f2d1b4f 2687
9c6221ae
GV
2688 s->obmc_scratchpad = NULL;
2689
0da71265
MN
2690 return 0;
2691fail:
2692 free_tables(h);
2693 return -1;
2694}
2695
2696static void common_init(H264Context *h){
2697 MpegEncContext * const s = &h->s;
0da71265
MN
2698
2699 s->width = s->avctx->width;
2700 s->height = s->avctx->height;
2701 s->codec_id= s->avctx->codec->id;
2702
2703 init_pred_ptrs(h);
2704
9a41c2c7 2705 s->unrestricted_mv=1;
0da71265
MN
2706 s->decode=1; //FIXME
2707}
2708
2709static int decode_init(AVCodecContext *avctx){
2710 H264Context *h= avctx->priv_data;
2711 MpegEncContext * const s = &h->s;
2712
3edcacde
MN
2713 MPV_decode_defaults(s);
2714
0da71265
MN
2715 s->avctx = avctx;
2716 common_init(h);
2717
2718 s->out_format = FMT_H264;
2719 s->workaround_bugs= avctx->workaround_bugs;
2720
2721 // set defaults
0da71265
MN
2722// s->decode_mb= ff_h263_decode_mb;
2723 s->low_delay= 1;
2724 avctx->pix_fmt= PIX_FMT_YUV420P;
2725
e4b36d44
MN
2726 if(s->dsp.h264_idct_add == ff_h264_idct_add_c){ //FIXME little ugly
2727 memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
2728 memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
2729 }else{
2730 int i;
2731 for(i=0; i<16; i++){
2732#define T(x) (x>>2) | ((x<<2) & 0xF)
2733 h->zigzag_scan[i] = T(zigzag_scan[i]);
2734 h-> field_scan[i] = T( field_scan[i]);
2735 }
2736 }
2737
0da71265
MN
2738 decode_init_vlc(h);
2739
26165f99
MR
2740 if(avctx->extradata_size > 0 && avctx->extradata &&
2741 *(char *)avctx->extradata == 1){
4770b1b4
RT
2742 h->is_avc = 1;
2743 h->got_avcC = 0;
26165f99
MR
2744 } else {
2745 h->is_avc = 0;
4770b1b4
RT
2746 }
2747
0da71265
MN
2748 return 0;
2749}
2750
2751static void frame_start(H264Context *h){
2752 MpegEncContext * const s = &h->s;
2753 int i;
2754
2755 MPV_frame_start(s, s->avctx);
2756 ff_er_frame_start(s);
0da71265
MN
2757
2758 assert(s->linesize && s->uvlinesize);
2759
2760 for(i=0; i<16; i++){
2761 h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
6867a90b 2762 h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
0da71265
MN
2763 }
2764 for(i=0; i<4; i++){
2765 h->block_offset[16+i]=
2766 h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
6867a90b
LLL
2767 h->block_offset[24+16+i]=
2768 h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
0da71265
MN
2769 }
2770
934b0821
LM
2771 /* can't be in alloc_tables because linesize isn't known there.
2772 * FIXME: redo bipred weight to not require extra buffer? */
2773 if(!s->obmc_scratchpad)
2774 s->obmc_scratchpad = av_malloc(16*s->linesize + 2*8*s->uvlinesize);
2775
0da71265
MN
2776// s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
2777}
2778
53c05b1e
MN
2779static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
2780 MpegEncContext * const s = &h->s;
2781 int i;
2782
2783 src_y -= linesize;
2784 src_cb -= uvlinesize;
2785 src_cr -= uvlinesize;
2786
3b66c4c5 2787 // There are two lines saved, the line above the the top macroblock of a pair,
6867a90b 2788 // and the line above the bottom macroblock
6ba71fc4 2789 h->left_border[0]= h->top_borders[0][s->mb_x][15];
53c05b1e
MN
2790 for(i=1; i<17; i++){
2791 h->left_border[i]= src_y[15+i* linesize];
2792 }
2793
6ba71fc4
LLL
2794 *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
2795 *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
53c05b1e
MN
2796
2797 if(!(s->flags&CODEC_FLAG_GRAY)){
6ba71fc4
LLL
2798 h->left_border[17 ]= h->top_borders[0][s->mb_x][16+7];
2799 h->left_border[17+9]= h->top_borders[0][s->mb_x][24+7];
53c05b1e
MN
2800 for(i=1; i<9; i++){
2801 h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
2802 h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
2803 }
6ba71fc4
LLL
2804 *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
2805 *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
53c05b1e
MN
2806 }
2807}
2808
2809static inline void xchg_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg){
2810 MpegEncContext * const s = &h->s;
2811 int temp8, i;
2812 uint64_t temp64;
d89dc06a
LM
2813 int deblock_left = (s->mb_x > 0);
2814 int deblock_top = (s->mb_y > 0);
53c05b1e
MN
2815
2816 src_y -= linesize + 1;
2817 src_cb -= uvlinesize + 1;
2818 src_cr -= uvlinesize + 1;
2819
2820#define XCHG(a,b,t,xchg)\
2821t= a;\
2822if(xchg)\
2823 a= b;\
2824b= t;
d89dc06a
LM
2825
2826 if(deblock_left){
2827 for(i = !deblock_top; i<17; i++){
2828 XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
2829 }
2830 }
2831
2832 if(deblock_top){
6ba71fc4
LLL
2833 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
2834 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
53c05b1e 2835 }
53c05b1e
MN
2836
2837 if(!(s->flags&CODEC_FLAG_GRAY)){
d89dc06a
LM
2838 if(deblock_left){
2839 for(i = !deblock_top; i<9; i++){
2840 XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
2841 XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
2842 }
2843 }
2844 if(deblock_top){
6ba71fc4
LLL
2845 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
2846 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
2847 }
2848 }
2849}
2850
2851static inline void backup_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
2852 MpegEncContext * const s = &h->s;
2853 int i;
2854
2855 src_y -= 2 * linesize;
2856 src_cb -= 2 * uvlinesize;
2857 src_cr -= 2 * uvlinesize;
2858
3b66c4c5 2859 // There are two lines saved, the line above the the top macroblock of a pair,
6ba71fc4
LLL
2860 // and the line above the bottom macroblock
2861 h->left_border[0]= h->top_borders[0][s->mb_x][15];
2862 h->left_border[1]= h->top_borders[1][s->mb_x][15];
2863 for(i=2; i<34; i++){
2864 h->left_border[i]= src_y[15+i* linesize];
2865 }
2866
2867 *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 32*linesize);
2868 *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+32*linesize);
2869 *(uint64_t*)(h->top_borders[1][s->mb_x]+0)= *(uint64_t*)(src_y + 33*linesize);
2870 *(uint64_t*)(h->top_borders[1][s->mb_x]+8)= *(uint64_t*)(src_y +8+33*linesize);
2871
2872 if(!(s->flags&CODEC_FLAG_GRAY)){
2873 h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7];
2874 h->left_border[34+ 1]= h->top_borders[1][s->mb_x][16+7];
2875 h->left_border[34+18 ]= h->top_borders[0][s->mb_x][24+7];
2876 h->left_border[34+18+1]= h->top_borders[1][s->mb_x][24+7];
2877 for(i=2; i<18; i++){
2878 h->left_border[i+34 ]= src_cb[7+i*uvlinesize];
2879 h->left_border[i+34+18]= src_cr[7+i*uvlinesize];
2880 }
2881 *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+16*uvlinesize);
2882 *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+16*uvlinesize);
2883 *(uint64_t*)(h->top_borders[1][s->mb_x]+16)= *(uint64_t*)(src_cb+17*uvlinesize);
2884 *(uint64_t*)(h->top_borders[1][s->mb_x]+24)= *(uint64_t*)(src_cr+17*uvlinesize);
2885 }
2886}
2887
2888static inline void xchg_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg){
2889 MpegEncContext * const s = &h->s;
2890 int temp8, i;
2891 uint64_t temp64;
2892 int deblock_left = (s->mb_x > 0);
2893 int deblock_top = (s->mb_y > 0);
2894
2895 tprintf("xchg_pair_border: src_y:%p src_cb:%p src_cr:%p ls:%d uvls:%d\n", src_y, src_cb, src_cr, linesize, uvlinesize);
2896
2897 src_y -= 2 * linesize + 1;
2898 src_cb -= 2 * uvlinesize + 1;
2899 src_cr -= 2 * uvlinesize + 1;
2900
2901#define XCHG(a,b,t,xchg)\
2902t= a;\
2903if(xchg)\
2904 a= b;\
2905b= t;
2906
2907 if(deblock_left){
2908 for(i = (!deblock_top)<<1; i<34; i++){
2909 XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
2910 }
2911 }
2912
2913 if(deblock_top){
2914 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
2915 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
2916 XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+0), *(uint64_t*)(src_y +1 +linesize), temp64, xchg);
2917 XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+8), *(uint64_t*)(src_y +9 +linesize), temp64, 1);
2918 }
2919
2920 if(!(s->flags&CODEC_FLAG_GRAY)){
2921 if(deblock_left){
2922 for(i = (!deblock_top) << 1; i<18; i++){
2923 XCHG(h->left_border[i+34 ], src_cb[i*uvlinesize], temp8, xchg);
2924 XCHG(h->left_border[i+34+18], src_cr[i*uvlinesize], temp8, xchg);
2925 }
2926 }
2927 if(deblock_top){
2928 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
2929 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
2930 XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+16), *(uint64_t*)(src_cb+1 +uvlinesize), temp64, 1);
2931 XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+24), *(uint64_t*)(src_cr+1 +uvlinesize), temp64, 1);
53c05b1e 2932 }
53c05b1e
MN
2933 }
2934}
2935
0da71265
MN
2936static void hl_decode_mb(H264Context *h){
2937 MpegEncContext * const s = &h->s;
2938 const int mb_x= s->mb_x;
2939 const int mb_y= s->mb_y;
7bc9090a 2940 const int mb_xy= mb_x + mb_y*s->mb_stride;
0da71265
MN
2941 const int mb_type= s->current_picture.mb_type[mb_xy];
2942 uint8_t *dest_y, *dest_cb, *dest_cr;
2943 int linesize, uvlinesize /*dct_offset*/;
2944 int i;
6867a90b 2945 int *block_offset = &h->block_offset[0];
6ba71fc4 2946 const unsigned int bottom = mb_y & 1;
0da71265
MN
2947
2948 if(!s->decode)
2949 return;
2950
0da71265
MN
2951 dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
2952 dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
2953 dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
2954
2955 if (h->mb_field_decoding_flag) {
2956 linesize = s->linesize * 2;
2957 uvlinesize = s->uvlinesize * 2;
6867a90b 2958 block_offset = &h->block_offset[24];
0da71265
MN
2959 if(mb_y&1){ //FIXME move out of this func?
2960 dest_y -= s->linesize*15;
6867a90b
LLL
2961 dest_cb-= s->uvlinesize*7;
2962 dest_cr-= s->uvlinesize*7;
0da71265
MN
2963 }
2964 } else {
2965 linesize = s->linesize;
2966 uvlinesize = s->uvlinesize;
2967// dct_offset = s->linesize * 16;
2968 }
2969
6fbcaaa0
LLL
2970 if (IS_INTRA_PCM(mb_type)) {
2971 unsigned int x, y;
2972
2973 // The pixels are stored in h->mb array in the same order as levels,
2974 // copy them in output in the correct order.
2975 for(i=0; i<16; i++) {
2976 for (y=0; y<4; y++) {
2977 for (x=0; x<4; x++) {
6867a90b 2978 *(dest_y + block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x];
6fbcaaa0
LLL
2979 }
2980 }
2981 }
2982 for(i=16; i<16+4; i++) {
2983 for (y=0; y<4; y++) {
2984 for (x=0; x<4; x++) {
6867a90b 2985 *(dest_cb + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
6fbcaaa0
LLL
2986 }
2987 }
2988 }
2989 for(i=20; i<20+4; i++) {
2990 for (y=0; y<4; y++) {
2991 for (x=0; x<4; x++) {
6867a90b 2992 *(dest_cr + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
6fbcaaa0
LLL
2993 }
2994 }
2995 }
e7e09b49
LLL
2996 } else {
2997 if(IS_INTRA(mb_type)){
6ba71fc4
LLL
2998 if(h->deblocking_filter) {
2999 if (h->mb_aff_frame) {
3000 if (!bottom)
3001 xchg_pair_border(h, dest_y, dest_cb, dest_cr, s->linesize, s->uvlinesize, 1);
3002 } else {
3003 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1);
3004 }
3005 }
53c05b1e 3006
e7e09b49
LLL
3007 if(!(s->flags&CODEC_FLAG_GRAY)){
3008 h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
3009 h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
3010 }
0da71265 3011
e7e09b49
LLL
3012 if(IS_INTRA4x4(mb_type)){
3013 if(!s->encoding){
3014 for(i=0; i<16; i++){
6867a90b 3015 uint8_t * const ptr= dest_y + block_offset[i];
e7e09b49
LLL
3016 uint8_t *topright;
3017 const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
3018 int tr;
3019
3020 if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
3021 const int topright_avail= (h->topright_samples_available<<i)&0x8000;
6867a90b 3022 assert(mb_y || linesize <= block_offset[i]);
e7e09b49
LLL
3023 if(!topright_avail){
3024 tr= ptr[3 - linesize]*0x01010101;
3025 topright= (uint8_t*) &tr;
3026 }else if(i==5 && h->deblocking_filter){
6ba71fc4 3027 tr= *(uint32_t*)h->top_borders[h->mb_aff_frame ? IS_INTERLACED(mb_type) ? bottom : 1 : 0][mb_x+1];
e7e09b49
LLL
3028 topright= (uint8_t*) &tr;
3029 }else
3030 topright= ptr + 4 - linesize;
a9799653 3031 }else
e7e09b49
LLL
3032 topright= NULL;
3033
3034 h->pred4x4[ dir ](ptr, topright, linesize);
3035 if(h->non_zero_count_cache[ scan8[i] ]){
3036 if(s->codec_id == CODEC_ID_H264)
3037 s->dsp.h264_idct_add(ptr, h->mb + i*16, linesize);
3038 else
3039 svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
3040 }
8b82a956 3041 }
0da71265 3042 }
e7e09b49
LLL
3043 }else{
3044 h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
3045 if(s->codec_id == CODEC_ID_H264)
3046 h264_luma_dc_dequant_idct_c(h->mb, s->qscale);
3047 else
3048 svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
0da71265 3049 }
6ba71fc4
LLL
3050 if(h->deblocking_filter) {
3051 if (h->mb_aff_frame) {
3052 if (bottom) {
3053 uint8_t *pair_dest_y = s->current_picture.data[0] + ((mb_y-1) * 16* s->linesize ) + mb_x * 16;
3054 uint8_t *pair_dest_cb = s->current_picture.data[1] + ((mb_y-1) * 8 * s->uvlinesize) + mb_x * 8;
3055 uint8_t *pair_dest_cr = s->current_picture.data[2] + ((mb_y-1) * 8 * s->uvlinesize) + mb_x * 8;
3056 s->mb_y--;
3057 xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0);
3058 s->mb_y++;
3059 }
3060 } else {
3061 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
3062 }
3063 }
e7e09b49
LLL
3064 }else if(s->codec_id == CODEC_ID_H264){
3065 hl_motion(h, dest_y, dest_cb, dest_cr,
3066 s->dsp.put_h264_qpel_pixels_tab, s->dsp.put_h264_chroma_pixels_tab,
3067 s->dsp.avg_h264_qpel_pixels_tab, s->dsp.avg_h264_chroma_pixels_tab,
3068 s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
0da71265 3069 }
e7e09b49
LLL
3070
3071
3072 if(!IS_INTRA4x4(mb_type)){
3073 if(s->codec_id == CODEC_ID_H264){
3074 for(i=0; i<16; i++){
3075 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
6867a90b 3076 uint8_t * const ptr= dest_y + block_offset[i];
e7e09b49
LLL
3077 s->dsp.h264_idct_add(ptr, h->mb + i*16, linesize);
3078 }
4704097a 3079 }
e7e09b49
LLL
3080 }else{
3081 for(i=0; i<16; i++){
3082 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
6867a90b 3083 uint8_t * const ptr= dest_y + block_offset[i];
e7e09b49
LLL
3084 svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
3085 }
4704097a 3086 }
0da71265
MN
3087 }
3088 }
0da71265 3089
e7e09b49
LLL
3090 if(!(s->flags&CODEC_FLAG_GRAY)){
3091 chroma_dc_dequant_idct_c(h->mb + 16*16, h->chroma_qp);
3092 chroma_dc_dequant_idct_c(h->mb + 16*16+4*16, h->chroma_qp);
3093 if(s->codec_id == CODEC_ID_H264){
3094 for(i=16; i<16+4; i++){
3095 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
6867a90b 3096 uint8_t * const ptr= dest_cb + block_offset[i];
e7e09b49
LLL
3097 s->dsp.h264_idct_add(ptr, h->mb + i*16, uvlinesize);
3098 }
4704097a 3099 }
e7e09b49
LLL
3100 for(i=20; i<20+4; i++){
3101 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
6867a90b 3102 uint8_t * const ptr= dest_cr + block_offset[i];
e7e09b49
LLL
3103 s->dsp.h264_idct_add(ptr, h->mb + i*16, uvlinesize);
3104 }
4704097a 3105 }
e7e09b49
LLL
3106 }else{
3107 for(i=16; i<16+4; i++){
3108 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
6867a90b 3109 uint8_t * const ptr= dest_cb + block_offset[i];
e7e09b49
LLL
3110 svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
3111 }
4704097a 3112 }
e7e09b49
LLL
3113 for(i=20; i<20+4; i++){
3114 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
6867a90b 3115 uint8_t * const ptr= dest_cr + block_offset[i];
e7e09b49
LLL
3116 svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
3117 }
4704097a 3118 }
0da71265
MN
3119 }
3120 }
3121 }
53c05b1e 3122 if(h->deblocking_filter) {
6ba71fc4
LLL
3123 if (h->mb_aff_frame) {
3124 const int mb_y = s->mb_y - 1;
3125 uint8_t *pair_dest_y, *pair_dest_cb, *pair_dest_cr;
3126 const int mb_xy= mb_x + mb_y*s->mb_stride;
3127 const int mb_type_top = s->current_picture.mb_type[mb_xy];
3128 const int mb_type_bottom= s->current_picture.mb_type[mb_xy+s->mb_stride];
3129 uint8_t tmp = s->current_picture.data[1][384];
3130 if (!bottom) return;
3131 pair_dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
3132 pair_dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
3133 pair_dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
3134
3135 backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize);
3136 // TODO deblock a pair
3137 // top
3138 s->mb_y--;
3139 tprintf("call mbaff filter_mb mb_x:%d mb_y:%d pair_dest_y = %p, dest_y = %p\n", mb_x, mb_y, pair_dest_y, dest_y);
3b66c4c5 3140 fill_caches(h, mb_type_top, 1); //FIXME don't fill stuff which isn't used by filter_mb
6ba71fc4
LLL
3141 filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize);
3142 if (tmp != s->current_picture.data[1][384]) {
3143 tprintf("modified pixel 8,1 (1)\n");
3144 }
3145 // bottom
3146 s->mb_y++;
3147 tprintf("call mbaff filter_mb\n");
3b66c4c5 3148 fill_caches(h, mb_type_bottom, 1); //FIXME don't fill stuff which isn't used by filter_mb
6ba71fc4
LLL
3149 filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
3150 if (tmp != s->current_picture.data[1][384]) {
3151 tprintf("modified pixel 8,1 (2)\n");
3152 }
3153 } else {
3154 tprintf("call filter_mb\n");
3155 backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
3b66c4c5 3156 fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
6ba71fc4
LLL
3157 filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
3158 }
53c05b1e 3159 }
0da71265
MN
3160}
3161
0da71265
MN
3162/**
3163 * fills the default_ref_list.
3164 */
3165static int fill_default_ref_list(H264Context *h){
3166 MpegEncContext * const s = &h->s;
3167 int i;
827c91bf 3168 int smallest_poc_greater_than_current = -1;
17107065 3169 Picture sorted_short_ref[32];
0da71265
MN
3170
3171 if(h->slice_type==B_TYPE){
3172 int out_i;
29860cc8 3173 int limit= INT_MIN;
0da71265 3174
827c91bf 3175 /* sort frame according to poc in B slice */
0da71265 3176 for(out_i=0; out_i<h->short_ref_count; out_i++){
29860cc8 3177 int best_i=INT_MIN;
792bb815 3178 int best_poc=INT_MAX;
0da71265
MN
3179
3180 for(i=0; i<h->short_ref_count; i++){
3181 const int poc= h->short_ref[i]->poc;
3182 if(poc > limit && poc < best_poc){
3183 best_poc= poc;
3184 best_i= i;
3185 }
3186 }
3187
29860cc8 3188 assert(best_i != INT_MIN);
0da71265
MN
3189
3190 limit= best_poc;
3191 sorted_short_ref[out_i]= *h->short_ref[best_i];
827c91bf
LLL
3192 tprintf("sorted poc: %d->%d poc:%d fn:%d\n", best_i, out_i, sorted_short_ref[out_i].poc, sorted_short_ref[out_i].frame_num);
3193 if (-1 == smallest_poc_greater_than_current) {
3194 if (h->short_ref[best_i]->poc >= s->current_picture_ptr->poc) {
3195 smallest_poc_greater_than_current = out_i;
3196 }
3197 }
0da71265
MN
3198 }
3199 }
3200
3201 if(s->picture_structure == PICT_FRAME){
3202 if(h->slice_type==B_TYPE){
0da71265 3203 int list;
827c91bf 3204 tprintf("current poc: %d, smallest_poc_greater_than_current: %d\n", s->current_picture_ptr->poc, smallest_poc_greater_than_current);
0da71265 3205
827c91bf 3206 // find the largest poc
0da71265 3207 for(list=0; list<2; list++){
827c91bf 3208 int index = 0;
3b33943e
MN
3209 int j= -99;
3210 int step= list ? -1 : 1;
827c91bf 3211
3b33943e
MN
3212 for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++, j+=step) {
3213 while(j<0 || j>= h->short_ref_count){
6ab87211
MN
3214 if(j != -99 && step == (list ? -1 : 1))
3215 return -1;
3b33943e
MN
3216 step = -step;
3217 j= smallest_poc_greater_than_current + (step>>1);
0da71265 3218 }
3b33943e
MN
3219 if(sorted_short_ref[j].reference != 3) continue;
3220 h->default_ref_list[list][index ]= sorted_short_ref[j];
3221 h->default_ref_list[list][index++].pic_id= sorted_short_ref[j].frame_num;
0da71265
MN
3222 }
3223
827c91bf
LLL
3224 for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){
3225 if(h->long_ref[i] == NULL) continue;
0da71265
MN
3226 if(h->long_ref[i]->reference != 3) continue;
3227
3228 h->default_ref_list[ list ][index ]= *h->long_ref[i];
3229 h->default_ref_list[ list ][index++].pic_id= i;;
3230 }
3231
3b33943e 3232 if(list && (smallest_poc_greater_than_current<=0 || smallest_poc_greater_than_current>=h->short_ref_count) && (1 < index)){
827c91bf
LLL
3233 // swap the two first elements of L1 when
3234 // L0 and L1 are identical
0da71265
MN
3235 Picture temp= h->default_ref_list[1][0];
3236 h->default_ref_list[1][0] = h->default_ref_list[1][1];
5cb46bc7 3237 h->default_ref_list[1][1] = temp;
0da71265
MN
3238 }
3239
3240 if(index < h->ref_count[ list ])
3241 memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
3242 }
3243 }else{
3244 int index=0;
2f944356 3245 for(i=0; i<h->short_ref_count; i++){
0da71265
MN
3246 if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
3247 h->default_ref_list[0][index ]= *h->short_ref[i];
3248 h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
3249 }
2f944356 3250 for(i = 0; i < 16; i++){
827c91bf 3251 if(h->long_ref[i] == NULL) continue;
0da71265
MN
3252 if(h->long_ref[i]->reference != 3) continue;
3253 h->default_ref_list[0][index ]= *h->long_ref[i];
3254 h->default_ref_list[0][index++].pic_id= i;;
3255 }
3256 if(index < h->ref_count[0])
3257 memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
3258 }
3259 }else{ //FIELD
3260 if(h->slice_type==B_TYPE){
3261 }else{
3262 //FIXME second field balh
3263 }
3264 }
827c91bf
LLL
3265#ifdef TRACE
3266 for (i=0; i<h->ref_count[0]; i++) {
3267 tprintf("List0: %s fn:%d 0x%p\n", (h->default_ref_list[0][i].long_ref ? "LT" : "ST"), h->default_ref_list[0][i].pic_id, h->default_ref_list[0][i].data[0]);
3268 }
3269 if(h->slice_type==B_TYPE){
3270 for (i=0; i<h->ref_count[1]; i++) {
3271 tprintf("List1: %s fn:%d 0x%p\n", (h->default_ref_list[1][i].long_ref ? "LT" : "ST"), h->default_ref_list[1][i].pic_id, h->default_ref_list[0][i].data[0]);
3272 }
3273 }
3274#endif
0da71265
MN
3275 return 0;
3276}
3277
827c91bf
LLL
3278static void print_short_term(H264Context *h);
3279static void print_long_term(H264Context *h);
3280
0da71265
MN
3281static int decode_ref_pic_list_reordering(H264Context *h){
3282 MpegEncContext * const s = &h->s;
6ab87211 3283 int list, index;
0da71265 3284
827c91bf
LLL
3285 print_short_term(h);
3286 print_long_term(h);
3b66c4c5 3287 if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move before func
0da71265
MN
3288
3289 for(list=0; list<2; list++){
3290 memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
3291
3292 if(get_bits1(&s->gb)){
3293 int pred= h->curr_pic_num;
0da71265
MN
3294
3295 for(index=0; ; index++){
3296 int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
3297 int pic_id;
3298 int i;
2f944356 3299 Picture *ref = NULL;
0da71265 3300
0bc42cad
LM
3301 if(reordering_of_pic_nums_idc==3)
3302 break;
0da71265
MN
3303
3304 if(index >= h->ref_count[list]){
9b879566 3305 av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
0da71265
MN
3306 return -1;
3307 }
3308
3309 if(reordering_of_pic_nums_idc<3){
3310 if(reordering_of_pic_nums_idc<2){
3311 const int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
3312
3313 if(abs_diff_pic_num >= h->max_pic_num){
9b879566 3314 av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
0da71265
MN
3315 return -1;
3316 }
3317
3318 if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
3319 else pred+= abs_diff_pic_num;
3320 pred &= h->max_pic_num - 1;
3321
0d175622
MN
3322 for(i= h->short_ref_count-1; i>=0; i--){
3323 ref = h->short_ref[i];
3324 assert(ref->reference == 3);
3325 assert(!ref->long_ref);
3326 if(ref->data[0] != NULL && ref->frame_num == pred && ref->long_ref == 0) // ignore non existing pictures by testing data[0] pointer
0da71265
MN
3327 break;
3328 }
0d175622
MN
3329 if(i>=0)
3330 ref->pic_id= ref->frame_num;
0da71265
MN
3331 }else{
3332 pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
0d175622
MN
3333 ref = h->long_ref[pic_id];
3334 ref->pic_id= pic_id;
3335 assert(ref->reference == 3);
3336 assert(ref->long_ref);
3337 i=0;
0da71265
MN
3338 }
3339
0d315f28 3340 if (i < 0) {
9b879566 3341 av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
0da71265 3342 memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
0d175622
MN
3343 } else {
3344 for(i=index; i+1<h->ref_count[list]; i++){
3345 if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
3346 break;
21be92bf
MN
3347 }
3348 for(; i > index; i--){
3349 h->ref_list[list][i]= h->ref_list[list][i-1];
3350 }
0d175622 3351 h->ref_list[list][index]= *ref;
0da71265 3352 }
0bc42cad 3353 }else{
9b879566 3354 av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
0da71265
MN
3355 return -1;
3356 }
3357 }
3358 }
3359
3360 if(h->slice_type!=B_TYPE) break;
3361 }
6ab87211
MN
3362 for(list=0; list<2; list++){
3363 for(index= 0; index < h->ref_count[list]; index++){
3364 if(!h->ref_list[list][index].data[0])
3365 h->ref_list[list][index]= s->current_picture;
3366 }
3367 if(h->slice_type!=B_TYPE) break;
3368 }
5ad984c9
LM
3369
3370 if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)
3371 direct_dist_scale_factor(h);
2f944356 3372 direct_ref_list_init(h);
0da71265
MN
3373 return 0;
3374}
3375
3376static int pred_weight_table(H264Context *h){
3377 MpegEncContext * const s = &h->s;
3378 int list, i;
9f2d1b4f 3379 int luma_def, chroma_def;
0da71265 3380
9f2d1b4f
LM
3381 h->use_weight= 0;
3382 h->use_weight_chroma= 0;
0da71265
MN
3383 h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
3384 h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
9f2d1b4f
LM
3385 luma_def = 1<<h->luma_log2_weight_denom;
3386 chroma_def = 1<<h->chroma_log2_weight_denom;
0da71265
MN
3387
3388 for(list=0; list<2; list++){
3389 for(i=0; i<h->ref_count[list]; i++){
3390 int luma_weight_flag, chroma_weight_flag;
3391
3392 luma_weight_flag= get_bits1(&s->gb);
3393 if(luma_weight_flag){
3394 h->luma_weight[list][i]= get_se_golomb(&s->gb);
3395 h->luma_offset[list][i]= get_se_golomb(&s->gb);
9f2d1b4f
LM
3396 if( h->luma_weight[list][i] != luma_def
3397 || h->luma_offset[list][i] != 0)
3398 h->use_weight= 1;
3399 }else{
3400 h->luma_weight[list][i]= luma_def;
3401 h->luma_offset[list][i]= 0;
0da71265
MN
3402 }
3403
3404 chroma_weight_flag= get_bits1(&s->gb);
3405 if(chroma_weight_flag){
3406 int j;
3407 for(j=0; j<2; j++){
3408 h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
3409 h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
9f2d1b4f
LM
3410 if( h->chroma_weight[list][i][j] != chroma_def
3411 || h->chroma_offset[list][i][j] != 0)
3412 h->use_weight_chroma= 1;
3413 }
3414 }else{
3415 int j;
3416 for(j=0; j<2; j++){
3417 h->chroma_weight[list][i][j]= chroma_def;
3418 h->chroma_offset[list][i][j]= 0;
0da71265
MN
3419 }
3420 }
3421 }
3422 if(h->slice_type != B_TYPE) break;
3423 }
9f2d1b4f 3424 h->use_weight= h->use_weight || h->use_weight_chroma;
0da71265
MN
3425 return 0;
3426}
3427
9f2d1b4f
LM
3428static void implicit_weight_table(H264Context *h){
3429 MpegEncContext * const s = &h->s;
9f2d1b4f
LM
3430 int ref0, ref1;
3431 int cur_poc = s->current_picture_ptr->poc;
3432
3433 if( h->ref_count[0] == 1 && h->ref_count[1] == 1
3434 && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
3435 h->use_weight= 0;
3436 h->use_weight_chroma= 0;
3437 return;
3438 }
3439
3440 h->use_weight= 2;
3441 h->use_weight_chroma= 2;
3442 h->luma_log2_weight_denom= 5;
3443 h->chroma_log2_weight_denom= 5;
3444
3445 /* FIXME: MBAFF */
3446 for(ref0=0; ref0 < h->ref_count[0]; ref0++){
3447 int poc0 = h->ref_list[0][ref0].poc;
3448 for(ref1=0; ref1 < h->ref_count[1]; ref1++){
738386a5 3449 int poc1 = h->ref_list[1][ref1].poc;
9f2d1b4f
LM
3450 int td = clip(poc1 - poc0, -128, 127);
3451 if(td){
3452 int tb = clip(cur_poc - poc0, -128, 127);
3453 int tx = (16384 + (ABS(td) >> 1)) / td;
3454 int dist_scale_factor = clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
3455 if(dist_scale_factor < -64 || dist_scale_factor > 128)
3456 h->implicit_weight[ref0][ref1] = 32;
3457 else
3458 h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
3459 }else
3460 h->implicit_weight[ref0][ref1] = 32;
3461 }
3462 }
3463}
3464
4e4d983e
LM
3465static inline void unreference_pic(H264Context *h, Picture *pic){
3466 int i;
3467 pic->reference=0;
3468 if(pic == h->delayed_output_pic)
3469 pic->reference=1;
3470 else{
3471 for(i = 0; h->delayed_pic[i]; i++)
3472 if(pic == h->delayed_pic[i]){
3473 pic->reference=1;
3474 break;
3475 }
3476 }
3477}
3478
0da71265 3479/**
5175b937 3480 * instantaneous decoder refresh.
0da71265
MN
3481 */
3482static void idr(H264Context *h){
4e4d983e 3483 int i;
0da71265 3484
dc032f33
LLL
3485 for(i=0; i<16; i++){
3486 if (h->long_ref[i] != NULL) {
4e4d983e 3487 unreference_pic(h, h->long_ref[i]);
dc032f33
LLL
3488 h->long_ref[i]= NULL;
3489 }
0da71265
MN
3490 }
3491 h->long_ref_count=0;
3492
3493 for(i=0; i<h->short_ref_count; i++){
4e4d983e 3494 unreference_pic(h, h->short_ref[i]);
0da71265
MN
3495 h->short_ref[i]= NULL;
3496 }
3497 h->short_ref_count=0;
3498}
3499
7c33ad19
LM
3500/* forget old pics after a seek */
3501static void flush_dpb(AVCodecContext *avctx){
3502 H264Context *h= avctx->priv_data;
3503 int i;
3504 for(i=0; i<16; i++)
3505 h->delayed_pic[i]= NULL;
3506 h->delayed_output_pic= NULL;
3507 idr(h);
ca159196
MR
3508 if(h->s.current_picture_ptr)
3509 h->s.current_picture_ptr->reference= 0;
7c33ad19
LM
3510}
3511
0da71265
MN
3512/**
3513 *
3b66c4c5 3514 * @return the removed picture or NULL if an error occurs
0da71265
MN
3515 */
3516static Picture * remove_short(H264Context *h, int frame_num){
1924f3ce 3517 MpegEncContext * const s = &h->s;
0da71265
MN
3518 int i;
3519
1924f3ce 3520 if(s->avctx->debug&FF_DEBUG_MMCO)
9b879566 3521 av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
1924f3ce 3522
0da71265
MN
3523 for(i=0; i<h->short_ref_count; i++){
3524 Picture *pic= h->short_ref[i];
1924f3ce 3525 if(s->avctx->debug&FF_DEBUG_MMCO)
9b879566 3526 av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
0da71265
MN
3527 if(pic->frame_num == frame_num){
3528 h->short_ref[i]= NULL;
3529 memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
3530 h->short_ref_count--;
3531 return pic;
3532 }
3533 }
3534 return NULL;
3535}
3536
3537/**
3538 *
3b66c4c5 3539 * @return the removed picture or NULL if an error occurs
0da71265
MN
3540 */
3541static Picture * remove_long(H264Context *h, int i){
3542 Picture *pic;
3543
0da71265 3544 pic= h->long_ref[i];
0da71265 3545 h->long_ref[i]= NULL;
827c91bf 3546 if(pic) h->long_ref_count--;
0da71265
MN
3547
3548 return pic;
3549}
3550
3551/**
827c91bf
LLL
3552 * print short term list
3553 */
3554static void print_short_term(H264Context *h) {
3555 uint32_t i;
3556 if(h->s.avctx->debug&FF_DEBUG_MMCO) {
3557 av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
3558 for(i=0; i<h->short_ref_count; i++){
3559 Picture *pic= h->short_ref[i];
3560 av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
3561 }
3562 }
3563}
3564
3565/**
3566 * print long term list
3567 */
3568static void print_long_term(H264Context *h) {
3569 uint32_t i;
3570 if(h->s.avctx->debug&FF_DEBUG_MMCO) {
3571 av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
3572 for(i = 0; i < 16; i++){
3573 Picture *pic= h->long_ref[i];
3574 if (pic) {
3575 av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
3576 }
3577 }
3578 }
3579}
3580
3581/**
0da71265
MN
3582 * Executes the reference picture marking (memory management control operations).
3583 */
3584static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
3585 MpegEncContext * const s = &h->s;
827c91bf 3586 int i, j;
0da71265
MN
3587 int current_is_long=0;
3588 Picture *pic;
3589
3590 if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
9b879566 3591 av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
0da71265
MN
3592
3593 for(i=0; i<mmco_count; i++){
3594 if(s->avctx->debug&FF_DEBUG_MMCO)
9b879566 3595 av_log(h->s.avctx, AV_LOG_DEBUG, "mmco:%d %d %d\n", h->mmco[i].opcode, h->mmco[i].short_frame_num, h->mmco[i].long_index);
0da71265
MN
3596
3597 switch(mmco[i].opcode){
3598 case MMCO_SHORT2UNUSED:
3599 pic= remove_short(h, mmco[i].short_frame_num);
3600 if(pic==NULL) return -1;
4e4d983e 3601 unreference_pic(h, pic);
0da71265
MN
3602 break;
3603 case MMCO_SHORT2LONG:
3604 pic= remove_long(h, mmco[i].long_index);
4e4d983e 3605 if(pic) unreference_pic(h, pic);
0da71265
MN
3606
3607 h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
3608 h->long_ref[ mmco[i].long_index ]->long_ref=1;
dc032f33 3609 h->long_ref_count++;
0da71265
MN
3610 break;
3611 case MMCO_LONG2UNUSED:
3612 pic= remove_long(h, mmco[i].long_index);
3613 if(pic==NULL) return -1;
4e4d983e 3614 unreference_pic(h, pic);
0da71265
MN
3615 break;
3616 case MMCO_LONG:
3617 pic= remove_long(h, mmco[i].long_index);
4e4d983e 3618 if(pic) unreference_pic(h, pic);
0da71265
MN
3619
3620 h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
3621 h->long_ref[ mmco[i].long_index ]->long_ref=1;
3622 h->long_ref_count++;
3623
3624 current_is_long=1;
3625 break;
3626 case MMCO_SET_MAX_LONG:
3627 assert(mmco[i].long_index <= 16);
827c91bf
LLL
3628 // just remove the long term which index is greater than new max
3629 for(j = mmco[i].long_index; j<16; j++){
3630 pic = remove_long(h, j);
4e4d983e 3631 if (pic) unreference_pic(h, pic);
0da71265
MN
3632 }
3633 break;
3634 case MMCO_RESET: