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