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