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