You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

6694 lines
240KB

  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. * @file h264.c
  22. * H.264 / AVC / MPEG4 part10 codec.
  23. * @author Michael Niedermayer <michaelni@gmx.at>
  24. */
  25. #include "common.h"
  26. #include "dsputil.h"
  27. #include "avcodec.h"
  28. #include "mpegvideo.h"
  29. #include "h264data.h"
  30. #include "golomb.h"
  31. #include "cabac.h"
  32. #undef NDEBUG
  33. #include <assert.h>
  34. #define interlaced_dct interlaced_dct_is_a_bad_name
  35. #define mb_intra mb_intra_isnt_initalized_see_mb_type
  36. #define LUMA_DC_BLOCK_INDEX 25
  37. #define CHROMA_DC_BLOCK_INDEX 26
  38. #define CHROMA_DC_COEFF_TOKEN_VLC_BITS 8
  39. #define COEFF_TOKEN_VLC_BITS 8
  40. #define TOTAL_ZEROS_VLC_BITS 9
  41. #define CHROMA_DC_TOTAL_ZEROS_VLC_BITS 3
  42. #define RUN_VLC_BITS 3
  43. #define RUN7_VLC_BITS 6
  44. #define MAX_SPS_COUNT 32
  45. #define MAX_PPS_COUNT 256
  46. #define MAX_MMCO_COUNT 66
  47. /**
  48. * Sequence parameter set
  49. */
  50. typedef struct SPS{
  51. int profile_idc;
  52. int level_idc;
  53. int log2_max_frame_num; ///< log2_max_frame_num_minus4 + 4
  54. int poc_type; ///< pic_order_cnt_type
  55. int log2_max_poc_lsb; ///< log2_max_pic_order_cnt_lsb_minus4
  56. int delta_pic_order_always_zero_flag;
  57. int offset_for_non_ref_pic;
  58. int offset_for_top_to_bottom_field;
  59. int poc_cycle_length; ///< num_ref_frames_in_pic_order_cnt_cycle
  60. int ref_frame_count; ///< num_ref_frames
  61. int gaps_in_frame_num_allowed_flag;
  62. int mb_width; ///< frame_width_in_mbs_minus1 + 1
  63. int mb_height; ///< frame_height_in_mbs_minus1 + 1
  64. int frame_mbs_only_flag;
  65. int mb_aff; ///<mb_adaptive_frame_field_flag
  66. int direct_8x8_inference_flag;
  67. int crop; ///< frame_cropping_flag
  68. int crop_left; ///< frame_cropping_rect_left_offset
  69. int crop_right; ///< frame_cropping_rect_right_offset
  70. int crop_top; ///< frame_cropping_rect_top_offset
  71. int crop_bottom; ///< frame_cropping_rect_bottom_offset
  72. int vui_parameters_present_flag;
  73. AVRational sar;
  74. int timing_info_present_flag;
  75. uint32_t num_units_in_tick;
  76. uint32_t time_scale;
  77. int fixed_frame_rate_flag;
  78. short offset_for_ref_frame[256]; //FIXME dyn aloc?
  79. }SPS;
  80. /**
  81. * Picture parameter set
  82. */
  83. typedef struct PPS{
  84. int sps_id;
  85. int cabac; ///< entropy_coding_mode_flag
  86. int pic_order_present; ///< pic_order_present_flag
  87. int slice_group_count; ///< num_slice_groups_minus1 + 1
  88. int mb_slice_group_map_type;
  89. int ref_count[2]; ///< num_ref_idx_l0/1_active_minus1 + 1
  90. int weighted_pred; ///< weighted_pred_flag
  91. int weighted_bipred_idc;
  92. int init_qp; ///< pic_init_qp_minus26 + 26
  93. int init_qs; ///< pic_init_qs_minus26 + 26
  94. int chroma_qp_index_offset;
  95. int deblocking_filter_parameters_present; ///< deblocking_filter_parameters_present_flag
  96. int constrained_intra_pred; ///< constrained_intra_pred_flag
  97. int redundant_pic_cnt_present; ///< redundant_pic_cnt_present_flag
  98. }PPS;
  99. /**
  100. * Memory management control operation opcode.
  101. */
  102. typedef enum MMCOOpcode{
  103. MMCO_END=0,
  104. MMCO_SHORT2UNUSED,
  105. MMCO_LONG2UNUSED,
  106. MMCO_SHORT2LONG,
  107. MMCO_SET_MAX_LONG,
  108. MMCO_RESET,
  109. MMCO_LONG,
  110. } MMCOOpcode;
  111. /**
  112. * Memory management control operation.
  113. */
  114. typedef struct MMCO{
  115. MMCOOpcode opcode;
  116. int short_frame_num;
  117. int long_index;
  118. } MMCO;
  119. /**
  120. * H264Context
  121. */
  122. typedef struct H264Context{
  123. MpegEncContext s;
  124. int nal_ref_idc;
  125. int nal_unit_type;
  126. #define NAL_SLICE 1
  127. #define NAL_DPA 2
  128. #define NAL_DPB 3
  129. #define NAL_DPC 4
  130. #define NAL_IDR_SLICE 5
  131. #define NAL_SEI 6
  132. #define NAL_SPS 7
  133. #define NAL_PPS 8
  134. #define NAL_PICTURE_DELIMITER 9
  135. #define NAL_FILTER_DATA 10
  136. uint8_t *rbsp_buffer;
  137. int rbsp_buffer_size;
  138. /**
  139. * Used to parse AVC variant of h264
  140. */
  141. int is_avc; ///< this flag is != 0 if codec is avc1
  142. int got_avcC; ///< flag used to parse avcC data only once
  143. int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
  144. int chroma_qp; //QPc
  145. int prev_mb_skiped; //FIXME remove (IMHO not used)
  146. //prediction stuff
  147. int chroma_pred_mode;
  148. int intra16x16_pred_mode;
  149. int8_t intra4x4_pred_mode_cache[5*8];
  150. int8_t (*intra4x4_pred_mode)[8];
  151. void (*pred4x4 [9+3])(uint8_t *src, uint8_t *topright, int stride);//FIXME move to dsp?
  152. void (*pred8x8 [4+3])(uint8_t *src, int stride);
  153. void (*pred16x16[4+3])(uint8_t *src, int stride);
  154. unsigned int topleft_samples_available;
  155. unsigned int top_samples_available;
  156. unsigned int topright_samples_available;
  157. unsigned int left_samples_available;
  158. uint8_t (*top_border)[16+2*8];
  159. uint8_t left_border[17+2*9];
  160. /**
  161. * non zero coeff count cache.
  162. * is 64 if not available.
  163. */
  164. uint8_t non_zero_count_cache[6*8] __align8;
  165. uint8_t (*non_zero_count)[16];
  166. /**
  167. * Motion vector cache.
  168. */
  169. int16_t mv_cache[2][5*8][2] __align8;
  170. int8_t ref_cache[2][5*8] __align8;
  171. #define LIST_NOT_USED -1 //FIXME rename?
  172. #define PART_NOT_AVAILABLE -2
  173. /**
  174. * is 1 if the specific list MV&references are set to 0,0,-2.
  175. */
  176. int mv_cache_clean[2];
  177. int block_offset[16+8];
  178. int chroma_subblock_offset[16]; //FIXME remove
  179. uint16_t *mb2b_xy; //FIXME are these 4 a good idea?
  180. uint16_t *mb2b8_xy;
  181. int b_stride; //FIXME use s->b4_stride
  182. int b8_stride;
  183. int halfpel_flag;
  184. int thirdpel_flag;
  185. int unknown_svq3_flag;
  186. int next_slice_index;
  187. SPS sps_buffer[MAX_SPS_COUNT];
  188. SPS sps; ///< current sps
  189. PPS pps_buffer[MAX_PPS_COUNT];
  190. /**
  191. * current pps
  192. */
  193. PPS pps; //FIXME move tp Picture perhaps? (->no) do we need that?
  194. int slice_num;
  195. uint8_t *slice_table_base;
  196. uint8_t *slice_table; ///< slice_table_base + mb_stride + 1
  197. int slice_type;
  198. int slice_type_fixed;
  199. //interlacing specific flags
  200. int mb_field_decoding_flag;
  201. int sub_mb_type[4];
  202. //POC stuff
  203. int poc_lsb;
  204. int poc_msb;
  205. int delta_poc_bottom;
  206. int delta_poc[2];
  207. int frame_num;
  208. int prev_poc_msb; ///< poc_msb of the last reference pic for POC type 0
  209. int prev_poc_lsb; ///< poc_lsb of the last reference pic for POC type 0
  210. int frame_num_offset; ///< for POC type 2
  211. int prev_frame_num_offset; ///< for POC type 2
  212. int prev_frame_num; ///< frame_num of the last pic for POC type 1/2
  213. /**
  214. * frame_num for frames or 2*frame_num for field pics.
  215. */
  216. int curr_pic_num;
  217. /**
  218. * max_frame_num or 2*max_frame_num for field pics.
  219. */
  220. int max_pic_num;
  221. //Weighted pred stuff
  222. int use_weight;
  223. int use_weight_chroma;
  224. int luma_log2_weight_denom;
  225. int chroma_log2_weight_denom;
  226. int luma_weight[2][16];
  227. int luma_offset[2][16];
  228. int chroma_weight[2][16][2];
  229. int chroma_offset[2][16][2];
  230. int implicit_weight[16][16];
  231. //deblock
  232. int deblocking_filter; ///< disable_deblocking_filter_idc with 1<->0
  233. int slice_alpha_c0_offset;
  234. int slice_beta_offset;
  235. int redundant_pic_count;
  236. int direct_spatial_mv_pred;
  237. int dist_scale_factor[16];
  238. /**
  239. * num_ref_idx_l0/1_active_minus1 + 1
  240. */
  241. int ref_count[2];// FIXME split for AFF
  242. Picture *short_ref[16];
  243. Picture *long_ref[16];
  244. Picture default_ref_list[2][32];
  245. Picture ref_list[2][32]; //FIXME size?
  246. Picture field_ref_list[2][32]; //FIXME size?
  247. Picture *delayed_pic[16]; //FIXME size?
  248. /**
  249. * memory management control operations buffer.
  250. */
  251. MMCO mmco[MAX_MMCO_COUNT];
  252. int mmco_index;
  253. int long_ref_count; ///< number of actual long term references
  254. int short_ref_count; ///< number of actual short term references
  255. //data partitioning
  256. GetBitContext intra_gb;
  257. GetBitContext inter_gb;
  258. GetBitContext *intra_gb_ptr;
  259. GetBitContext *inter_gb_ptr;
  260. DCTELEM mb[16*24] __align8;
  261. /**
  262. * Cabac
  263. */
  264. CABACContext cabac;
  265. uint8_t cabac_state[399];
  266. int cabac_init_idc;
  267. /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
  268. uint16_t *cbp_table;
  269. int top_cbp;
  270. int left_cbp;
  271. /* chroma_pred_mode for i4x4 or i16x16, else 0 */
  272. uint8_t *chroma_pred_mode_table;
  273. int last_qscale_diff;
  274. int16_t (*mvd_table[2])[2];
  275. int16_t mvd_cache[2][5*8][2] __align8;
  276. uint8_t *direct_table;
  277. uint8_t direct_cache[5*8];
  278. }H264Context;
  279. static VLC coeff_token_vlc[4];
  280. static VLC chroma_dc_coeff_token_vlc;
  281. static VLC total_zeros_vlc[15];
  282. static VLC chroma_dc_total_zeros_vlc[3];
  283. static VLC run_vlc[6];
  284. static VLC run7_vlc;
  285. static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
  286. static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
  287. static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr);
  288. static inline uint32_t pack16to32(int a, int b){
  289. #ifdef WORDS_BIGENDIAN
  290. return (b&0xFFFF) + (a<<16);
  291. #else
  292. return (a&0xFFFF) + (b<<16);
  293. #endif
  294. }
  295. /**
  296. * fill a rectangle.
  297. * @param h height of the rectangle, should be a constant
  298. * @param w width of the rectangle, should be a constant
  299. * @param size the size of val (1 or 4), should be a constant
  300. */
  301. static inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){ //FIXME ensure this IS inlined
  302. uint8_t *p= (uint8_t*)vp;
  303. assert(size==1 || size==4);
  304. w *= size;
  305. stride *= size;
  306. assert((((int)vp)&(FFMIN(w, STRIDE_ALIGN)-1)) == 0);
  307. //FIXME check what gcc generates for 64 bit on x86 and possible write a 32 bit ver of it
  308. if(w==2 && h==2){
  309. *(uint16_t*)(p + 0)=
  310. *(uint16_t*)(p + stride)= size==4 ? val : val*0x0101;
  311. }else if(w==2 && h==4){
  312. *(uint16_t*)(p + 0*stride)=
  313. *(uint16_t*)(p + 1*stride)=
  314. *(uint16_t*)(p + 2*stride)=
  315. *(uint16_t*)(p + 3*stride)= size==4 ? val : val*0x0101;
  316. }else if(w==4 && h==1){
  317. *(uint32_t*)(p + 0*stride)= size==4 ? val : val*0x01010101;
  318. }else if(w==4 && h==2){
  319. *(uint32_t*)(p + 0*stride)=
  320. *(uint32_t*)(p + 1*stride)= size==4 ? val : val*0x01010101;
  321. }else if(w==4 && h==4){
  322. *(uint32_t*)(p + 0*stride)=
  323. *(uint32_t*)(p + 1*stride)=
  324. *(uint32_t*)(p + 2*stride)=
  325. *(uint32_t*)(p + 3*stride)= size==4 ? val : val*0x01010101;
  326. }else if(w==8 && h==1){
  327. *(uint32_t*)(p + 0)=
  328. *(uint32_t*)(p + 4)= size==4 ? val : val*0x01010101;
  329. }else if(w==8 && h==2){
  330. *(uint32_t*)(p + 0 + 0*stride)=
  331. *(uint32_t*)(p + 4 + 0*stride)=
  332. *(uint32_t*)(p + 0 + 1*stride)=
  333. *(uint32_t*)(p + 4 + 1*stride)= size==4 ? val : val*0x01010101;
  334. }else if(w==8 && h==4){
  335. *(uint64_t*)(p + 0*stride)=
  336. *(uint64_t*)(p + 1*stride)=
  337. *(uint64_t*)(p + 2*stride)=
  338. *(uint64_t*)(p + 3*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
  339. }else if(w==16 && h==2){
  340. *(uint64_t*)(p + 0+0*stride)=
  341. *(uint64_t*)(p + 8+0*stride)=
  342. *(uint64_t*)(p + 0+1*stride)=
  343. *(uint64_t*)(p + 8+1*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
  344. }else if(w==16 && h==4){
  345. *(uint64_t*)(p + 0+0*stride)=
  346. *(uint64_t*)(p + 8+0*stride)=
  347. *(uint64_t*)(p + 0+1*stride)=
  348. *(uint64_t*)(p + 8+1*stride)=
  349. *(uint64_t*)(p + 0+2*stride)=
  350. *(uint64_t*)(p + 8+2*stride)=
  351. *(uint64_t*)(p + 0+3*stride)=
  352. *(uint64_t*)(p + 8+3*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
  353. }else
  354. assert(0);
  355. }
  356. static inline void fill_caches(H264Context *h, int mb_type, int for_deblock){
  357. MpegEncContext * const s = &h->s;
  358. const int mb_xy= s->mb_x + s->mb_y*s->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. //wow what a mess, why didnt they simplify the interlacing&intra stuff, i cant imagine that these complex rules are worth it
  364. if(h->sps.mb_aff){
  365. //FIXME
  366. topleft_xy = 0; /* avoid warning */
  367. top_xy = 0; /* avoid warning */
  368. topright_xy = 0; /* avoid warning */
  369. }else{
  370. topleft_xy = mb_xy-1 - s->mb_stride;
  371. top_xy = mb_xy - s->mb_stride;
  372. topright_xy= mb_xy+1 - s->mb_stride;
  373. left_xy[0] = mb_xy-1;
  374. left_xy[1] = mb_xy-1;
  375. left_block[0]= 0;
  376. left_block[1]= 1;
  377. left_block[2]= 2;
  378. left_block[3]= 3;
  379. }
  380. if(for_deblock){
  381. topleft_type = h->slice_table[topleft_xy ] < 255 ? s->current_picture.mb_type[topleft_xy] : 0;
  382. top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0;
  383. topright_type= h->slice_table[topright_xy] < 255 ? s->current_picture.mb_type[topright_xy]: 0;
  384. left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
  385. left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
  386. }else{
  387. topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
  388. top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
  389. topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
  390. left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
  391. left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
  392. }
  393. if(IS_INTRA(mb_type)){
  394. h->topleft_samples_available=
  395. h->top_samples_available=
  396. h->left_samples_available= 0xFFFF;
  397. h->topright_samples_available= 0xEEEA;
  398. if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
  399. h->topleft_samples_available= 0xB3FF;
  400. h->top_samples_available= 0x33FF;
  401. h->topright_samples_available= 0x26EA;
  402. }
  403. for(i=0; i<2; i++){
  404. if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
  405. h->topleft_samples_available&= 0xDF5F;
  406. h->left_samples_available&= 0x5F5F;
  407. }
  408. }
  409. if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
  410. h->topleft_samples_available&= 0x7FFF;
  411. if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
  412. h->topright_samples_available&= 0xFBFF;
  413. if(IS_INTRA4x4(mb_type)){
  414. if(IS_INTRA4x4(top_type)){
  415. h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
  416. h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
  417. h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
  418. h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
  419. }else{
  420. int pred;
  421. if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred))
  422. pred= -1;
  423. else{
  424. pred= 2;
  425. }
  426. h->intra4x4_pred_mode_cache[4+8*0]=
  427. h->intra4x4_pred_mode_cache[5+8*0]=
  428. h->intra4x4_pred_mode_cache[6+8*0]=
  429. h->intra4x4_pred_mode_cache[7+8*0]= pred;
  430. }
  431. for(i=0; i<2; i++){
  432. if(IS_INTRA4x4(left_type[i])){
  433. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
  434. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
  435. }else{
  436. int pred;
  437. if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred))
  438. pred= -1;
  439. else{
  440. pred= 2;
  441. }
  442. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
  443. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
  444. }
  445. }
  446. }
  447. }
  448. /*
  449. 0 . T T. T T T T
  450. 1 L . .L . . . .
  451. 2 L . .L . . . .
  452. 3 . T TL . . . .
  453. 4 L . .L . . . .
  454. 5 L . .. . . . .
  455. */
  456. //FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec)
  457. if(top_type){
  458. h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][0];
  459. h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][1];
  460. h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][2];
  461. h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
  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. h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][10];
  465. h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
  466. h->top_cbp= h->cbp_table[top_xy];
  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. h->non_zero_count_cache[1+8*0]=
  473. h->non_zero_count_cache[2+8*0]=
  474. h->non_zero_count_cache[1+8*3]=
  475. h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  476. if(IS_INTRA(mb_type)) h->top_cbp= 0x1C0;
  477. else h->top_cbp= 0;
  478. }
  479. if(left_type[0]){
  480. h->non_zero_count_cache[3+8*1]= h->non_zero_count[left_xy[0]][6];
  481. h->non_zero_count_cache[3+8*2]= h->non_zero_count[left_xy[0]][5];
  482. h->non_zero_count_cache[0+8*1]= h->non_zero_count[left_xy[0]][9]; //FIXME left_block
  483. h->non_zero_count_cache[0+8*4]= h->non_zero_count[left_xy[0]][12];
  484. h->left_cbp= h->cbp_table[left_xy[0]]; //FIXME interlacing
  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]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  490. if(IS_INTRA(mb_type)) h->left_cbp= 0x1C0;//FIXME interlacing
  491. else h->left_cbp= 0;
  492. }
  493. if(left_type[1]){
  494. h->non_zero_count_cache[3+8*3]= h->non_zero_count[left_xy[1]][4];
  495. h->non_zero_count_cache[3+8*4]= h->non_zero_count[left_xy[1]][3];
  496. h->non_zero_count_cache[0+8*2]= h->non_zero_count[left_xy[1]][8];
  497. h->non_zero_count_cache[0+8*5]= h->non_zero_count[left_xy[1]][11];
  498. }else{
  499. h->non_zero_count_cache[3+8*3]=
  500. h->non_zero_count_cache[3+8*4]=
  501. h->non_zero_count_cache[0+8*2]=
  502. h->non_zero_count_cache[0+8*5]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  503. }
  504. #if 1
  505. //FIXME direct mb can skip much of this
  506. if(IS_INTER(mb_type) || (IS_DIRECT(mb_type) && h->direct_spatial_mv_pred)){
  507. int list;
  508. for(list=0; list<2; list++){
  509. if((!IS_8X8(mb_type)) && !USES_LIST(mb_type, list) && !IS_DIRECT(mb_type)){
  510. /*if(!h->mv_cache_clean[list]){
  511. memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
  512. memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
  513. h->mv_cache_clean[list]= 1;
  514. }*/
  515. continue;
  516. }
  517. h->mv_cache_clean[list]= 0;
  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. if(IS_INTER(top_type)){
  528. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  529. const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
  530. *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
  531. *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
  532. *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
  533. *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
  534. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  535. h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
  536. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  537. h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
  538. }else{
  539. *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
  540. *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
  541. *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
  542. *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
  543. *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
  544. }
  545. if(IS_INTER(topright_type)){
  546. const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
  547. const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
  548. *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  549. h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  550. }else{
  551. *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
  552. h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  553. }
  554. //FIXME unify cleanup or sth
  555. if(IS_INTER(left_type[0])){
  556. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  557. const int b8_xy= h->mb2b8_xy[left_xy[0]] + 1;
  558. *(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]];
  559. *(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]];
  560. h->ref_cache[list][scan8[0] - 1 + 0*8]=
  561. h->ref_cache[list][scan8[0] - 1 + 1*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0]>>1)];
  562. }else{
  563. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 0*8]=
  564. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 1*8]= 0;
  565. h->ref_cache[list][scan8[0] - 1 + 0*8]=
  566. h->ref_cache[list][scan8[0] - 1 + 1*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  567. }
  568. if(IS_INTER(left_type[1])){
  569. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  570. const int b8_xy= h->mb2b8_xy[left_xy[1]] + 1;
  571. *(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]];
  572. *(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]];
  573. h->ref_cache[list][scan8[0] - 1 + 2*8]=
  574. h->ref_cache[list][scan8[0] - 1 + 3*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[2]>>1)];
  575. }else{
  576. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 2*8]=
  577. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 3*8]= 0;
  578. h->ref_cache[list][scan8[0] - 1 + 2*8]=
  579. h->ref_cache[list][scan8[0] - 1 + 3*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  580. }
  581. if(for_deblock)
  582. continue;
  583. h->ref_cache[list][scan8[5 ]+1] =
  584. h->ref_cache[list][scan8[7 ]+1] =
  585. h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewher else)
  586. h->ref_cache[list][scan8[4 ]] =
  587. h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
  588. *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
  589. *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
  590. *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewher else)
  591. *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
  592. *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
  593. if( h->pps.cabac ) {
  594. /* XXX beurk, Load mvd */
  595. if(IS_INTER(topleft_type)){
  596. const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
  597. *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy];
  598. }else{
  599. *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 - 1*8]= 0;
  600. }
  601. if(IS_INTER(top_type)){
  602. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  603. *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
  604. *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
  605. *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
  606. *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
  607. }else{
  608. *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
  609. *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
  610. *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
  611. *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
  612. }
  613. if(IS_INTER(left_type[0])){
  614. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  615. *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 0*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[0]];
  616. *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[1]];
  617. }else{
  618. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
  619. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
  620. }
  621. if(IS_INTER(left_type[1])){
  622. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  623. *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 2*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[2]];
  624. *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 3*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[3]];
  625. }else{
  626. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
  627. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
  628. }
  629. *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
  630. *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
  631. *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewher else)
  632. *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
  633. *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
  634. if(h->slice_type == B_TYPE){
  635. fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
  636. if(IS_DIRECT(top_type)){
  637. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
  638. }else if(IS_8X8(top_type)){
  639. int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
  640. h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
  641. h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
  642. }else{
  643. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
  644. }
  645. //FIXME interlacing
  646. if(IS_DIRECT(left_type[0])){
  647. h->direct_cache[scan8[0] - 1 + 0*8]=
  648. h->direct_cache[scan8[0] - 1 + 2*8]= 1;
  649. }else if(IS_8X8(left_type[0])){
  650. int b8_xy = h->mb2b8_xy[left_xy[0]] + 1;
  651. h->direct_cache[scan8[0] - 1 + 0*8]= h->direct_table[b8_xy];
  652. h->direct_cache[scan8[0] - 1 + 2*8]= h->direct_table[b8_xy + h->b8_stride];
  653. }else{
  654. h->direct_cache[scan8[0] - 1 + 0*8]=
  655. h->direct_cache[scan8[0] - 1 + 2*8]= 0;
  656. }
  657. }
  658. }
  659. }
  660. }
  661. #endif
  662. }
  663. static inline void write_back_intra_pred_mode(H264Context *h){
  664. MpegEncContext * const s = &h->s;
  665. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  666. h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
  667. h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
  668. h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
  669. h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
  670. h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
  671. h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
  672. h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
  673. }
  674. /**
  675. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  676. */
  677. static inline int check_intra4x4_pred_mode(H264Context *h){
  678. MpegEncContext * const s = &h->s;
  679. static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
  680. static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
  681. int i;
  682. if(!(h->top_samples_available&0x8000)){
  683. for(i=0; i<4; i++){
  684. int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
  685. if(status<0){
  686. av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
  687. return -1;
  688. } else if(status){
  689. h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
  690. }
  691. }
  692. }
  693. if(!(h->left_samples_available&0x8000)){
  694. for(i=0; i<4; i++){
  695. int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
  696. if(status<0){
  697. av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
  698. return -1;
  699. } else if(status){
  700. h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
  701. }
  702. }
  703. }
  704. return 0;
  705. } //FIXME cleanup like next
  706. /**
  707. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  708. */
  709. static inline int check_intra_pred_mode(H264Context *h, int mode){
  710. MpegEncContext * const s = &h->s;
  711. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  712. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  713. if(mode < 0 || mode > 6) {
  714. av_log(h->s.avctx, AV_LOG_ERROR, "out of range intra chroma pred mode at %d %d\n", s->mb_x, s->mb_y);
  715. return -1;
  716. }
  717. if(!(h->top_samples_available&0x8000)){
  718. mode= top[ mode ];
  719. if(mode<0){
  720. av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
  721. return -1;
  722. }
  723. }
  724. if(!(h->left_samples_available&0x8000)){
  725. mode= left[ mode ];
  726. if(mode<0){
  727. av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
  728. return -1;
  729. }
  730. }
  731. return mode;
  732. }
  733. /**
  734. * gets the predicted intra4x4 prediction mode.
  735. */
  736. static inline int pred_intra_mode(H264Context *h, int n){
  737. const int index8= scan8[n];
  738. const int left= h->intra4x4_pred_mode_cache[index8 - 1];
  739. const int top = h->intra4x4_pred_mode_cache[index8 - 8];
  740. const int min= FFMIN(left, top);
  741. tprintf("mode:%d %d min:%d\n", left ,top, min);
  742. if(min<0) return DC_PRED;
  743. else return min;
  744. }
  745. static inline void write_back_non_zero_count(H264Context *h){
  746. MpegEncContext * const s = &h->s;
  747. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  748. h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[4+8*4];
  749. h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[5+8*4];
  750. h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[6+8*4];
  751. h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
  752. h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[7+8*3];
  753. h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[7+8*2];
  754. h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[7+8*1];
  755. h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[1+8*2];
  756. h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
  757. h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[2+8*1];
  758. h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[1+8*5];
  759. h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
  760. h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[2+8*4];
  761. }
  762. /**
  763. * gets the predicted number of non zero coefficients.
  764. * @param n block index
  765. */
  766. static inline int pred_non_zero_count(H264Context *h, int n){
  767. const int index8= scan8[n];
  768. const int left= h->non_zero_count_cache[index8 - 1];
  769. const int top = h->non_zero_count_cache[index8 - 8];
  770. int i= left + top;
  771. if(i<64) i= (i+1)>>1;
  772. tprintf("pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
  773. return i&31;
  774. }
  775. static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
  776. const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
  777. if(topright_ref != PART_NOT_AVAILABLE){
  778. *C= h->mv_cache[list][ i - 8 + part_width ];
  779. return topright_ref;
  780. }else{
  781. tprintf("topright MV not available\n");
  782. *C= h->mv_cache[list][ i - 8 - 1 ];
  783. return h->ref_cache[list][ i - 8 - 1 ];
  784. }
  785. }
  786. /**
  787. * gets the predicted MV.
  788. * @param n the block index
  789. * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
  790. * @param mx the x component of the predicted motion vector
  791. * @param my the y component of the predicted motion vector
  792. */
  793. static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
  794. const int index8= scan8[n];
  795. const int top_ref= h->ref_cache[list][ index8 - 8 ];
  796. const int left_ref= h->ref_cache[list][ index8 - 1 ];
  797. const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
  798. const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
  799. const int16_t * C;
  800. int diagonal_ref, match_count;
  801. assert(part_width==1 || part_width==2 || part_width==4);
  802. /* mv_cache
  803. B . . A T T T T
  804. U . . L . . , .
  805. U . . L . . . .
  806. U . . L . . , .
  807. . . . L . . . .
  808. */
  809. diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
  810. match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
  811. tprintf("pred_motion match_count=%d\n", match_count);
  812. if(match_count > 1){ //most common
  813. *mx= mid_pred(A[0], B[0], C[0]);
  814. *my= mid_pred(A[1], B[1], C[1]);
  815. }else if(match_count==1){
  816. if(left_ref==ref){
  817. *mx= A[0];
  818. *my= A[1];
  819. }else if(top_ref==ref){
  820. *mx= B[0];
  821. *my= B[1];
  822. }else{
  823. *mx= C[0];
  824. *my= C[1];
  825. }
  826. }else{
  827. if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
  828. *mx= A[0];
  829. *my= A[1];
  830. }else{
  831. *mx= mid_pred(A[0], B[0], C[0]);
  832. *my= mid_pred(A[1], B[1], C[1]);
  833. }
  834. }
  835. tprintf("pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], diagonal_ref, C[0], C[1], left_ref, A[0], A[1], ref, *mx, *my, h->s.mb_x, h->s.mb_y, n, list);
  836. }
  837. /**
  838. * gets the directionally predicted 16x8 MV.
  839. * @param n the block index
  840. * @param mx the x component of the predicted motion vector
  841. * @param my the y component of the predicted motion vector
  842. */
  843. static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  844. if(n==0){
  845. const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
  846. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  847. tprintf("pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], h->s.mb_x, h->s.mb_y, n, list);
  848. if(top_ref == ref){
  849. *mx= B[0];
  850. *my= B[1];
  851. return;
  852. }
  853. }else{
  854. const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
  855. const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
  856. tprintf("pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
  857. if(left_ref == ref){
  858. *mx= A[0];
  859. *my= A[1];
  860. return;
  861. }
  862. }
  863. //RARE
  864. pred_motion(h, n, 4, list, ref, mx, my);
  865. }
  866. /**
  867. * gets the directionally predicted 8x16 MV.
  868. * @param n the block index
  869. * @param mx the x component of the predicted motion vector
  870. * @param my the y component of the predicted motion vector
  871. */
  872. static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  873. if(n==0){
  874. const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
  875. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  876. tprintf("pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
  877. if(left_ref == ref){
  878. *mx= A[0];
  879. *my= A[1];
  880. return;
  881. }
  882. }else{
  883. const int16_t * C;
  884. int diagonal_ref;
  885. diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
  886. tprintf("pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", diagonal_ref, C[0], C[1], h->s.mb_x, h->s.mb_y, n, list);
  887. if(diagonal_ref == ref){
  888. *mx= C[0];
  889. *my= C[1];
  890. return;
  891. }
  892. }
  893. //RARE
  894. pred_motion(h, n, 2, list, ref, mx, my);
  895. }
  896. static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
  897. const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
  898. const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
  899. tprintf("pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
  900. if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
  901. || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
  902. || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
  903. *mx = *my = 0;
  904. return;
  905. }
  906. pred_motion(h, 0, 4, 0, 0, mx, my);
  907. return;
  908. }
  909. static inline void direct_dist_scale_factor(H264Context * const h){
  910. const int poc = h->s.current_picture_ptr->poc;
  911. const int poc1 = h->ref_list[1][0].poc;
  912. int i;
  913. for(i=0; i<h->ref_count[0]; i++){
  914. int poc0 = h->ref_list[0][i].poc;
  915. int td = clip(poc1 - poc0, -128, 127);
  916. if(td == 0 /* FIXME || pic0 is a long-term ref */){
  917. h->dist_scale_factor[i] = 256;
  918. }else{
  919. int tb = clip(poc - poc0, -128, 127);
  920. int tx = (16384 + (ABS(td) >> 1)) / td;
  921. h->dist_scale_factor[i] = clip((tb*tx + 32) >> 6, -1024, 1023);
  922. }
  923. }
  924. }
  925. static inline void pred_direct_motion(H264Context * const h, int *mb_type){
  926. MpegEncContext * const s = &h->s;
  927. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  928. const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  929. const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  930. const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
  931. const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
  932. const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
  933. const int is_b8x8 = IS_8X8(*mb_type);
  934. int sub_mb_type;
  935. int i8, i4;
  936. if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
  937. /* FIXME save sub mb types from previous frames (or derive from MVs)
  938. * so we know exactly what block size to use */
  939. sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  940. *mb_type = MB_TYPE_8x8;
  941. }else if(!is_b8x8 && (IS_16X16(mb_type_col) || IS_INTRA(mb_type_col))){
  942. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  943. *mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
  944. }else{
  945. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  946. *mb_type = MB_TYPE_8x8;
  947. }
  948. if(!is_b8x8)
  949. *mb_type |= MB_TYPE_DIRECT2;
  950. tprintf("mb_type = %08x, sub_mb_type = %08x, is_b8x8 = %d, mb_type_col = %08x\n", *mb_type, sub_mb_type, is_b8x8, mb_type_col);
  951. if(h->direct_spatial_mv_pred){
  952. int ref[2];
  953. int mv[2][2];
  954. int list;
  955. /* ref = min(neighbors) */
  956. for(list=0; list<2; list++){
  957. int refa = h->ref_cache[list][scan8[0] - 1];
  958. int refb = h->ref_cache[list][scan8[0] - 8];
  959. int refc = h->ref_cache[list][scan8[0] - 8 + 4];
  960. if(refc == -2)
  961. refc = h->ref_cache[list][scan8[0] - 8 - 1];
  962. ref[list] = refa;
  963. if(ref[list] < 0 || (refb < ref[list] && refb >= 0))
  964. ref[list] = refb;
  965. if(ref[list] < 0 || (refc < ref[list] && refc >= 0))
  966. ref[list] = refc;
  967. if(ref[list] < 0)
  968. ref[list] = -1;
  969. }
  970. if(ref[0] < 0 && ref[1] < 0){
  971. ref[0] = ref[1] = 0;
  972. mv[0][0] = mv[0][1] =
  973. mv[1][0] = mv[1][1] = 0;
  974. }else{
  975. for(list=0; list<2; list++){
  976. if(ref[list] >= 0)
  977. pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
  978. else
  979. mv[list][0] = mv[list][1] = 0;
  980. }
  981. }
  982. if(ref[1] < 0){
  983. *mb_type &= ~MB_TYPE_P0L1;
  984. sub_mb_type &= ~MB_TYPE_P0L1;
  985. }else if(ref[0] < 0){
  986. *mb_type &= ~MB_TYPE_P0L0;
  987. sub_mb_type &= ~MB_TYPE_P0L0;
  988. }
  989. if(IS_16X16(*mb_type)){
  990. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref[0], 1);
  991. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, ref[1], 1);
  992. if(!IS_INTRA(mb_type_col) && l1ref0[0] == 0 &&
  993. ABS(l1mv0[0][0]) <= 1 && ABS(l1mv0[0][1]) <= 1){
  994. if(ref[0] > 0)
  995. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  996. else
  997. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  998. if(ref[1] > 0)
  999. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1000. else
  1001. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  1002. }else{
  1003. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1004. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1005. }
  1006. }else{
  1007. for(i8=0; i8<4; i8++){
  1008. const int x8 = i8&1;
  1009. const int y8 = i8>>1;
  1010. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1011. continue;
  1012. h->sub_mb_type[i8] = sub_mb_type;
  1013. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1014. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1015. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref[0], 1);
  1016. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, ref[1], 1);
  1017. /* col_zero_flag */
  1018. if(!IS_INTRA(mb_type_col) && l1ref0[x8 + y8*h->b8_stride] == 0){
  1019. for(i4=0; i4<4; i4++){
  1020. const int16_t *mv_col = l1mv0[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1021. if(ABS(mv_col[0]) <= 1 && ABS(mv_col[1]) <= 1){
  1022. if(ref[0] == 0)
  1023. *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
  1024. if(ref[1] == 0)
  1025. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
  1026. }
  1027. }
  1028. }
  1029. }
  1030. }
  1031. }else{ /* direct temporal mv pred */
  1032. /* FIXME assumes that L1ref0 used the same ref lists as current frame */
  1033. if(IS_16X16(*mb_type)){
  1034. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  1035. if(IS_INTRA(mb_type_col)){
  1036. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  1037. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  1038. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  1039. }else{
  1040. const int ref0 = l1ref0[0];
  1041. const int dist_scale_factor = h->dist_scale_factor[ref0];
  1042. const int16_t *mv_col = l1mv0[0];
  1043. int mv_l0[2];
  1044. mv_l0[0] = (dist_scale_factor * mv_col[0] + 128) >> 8;
  1045. mv_l0[1] = (dist_scale_factor * mv_col[1] + 128) >> 8;
  1046. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref0, 1);
  1047. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0],mv_l0[1]), 4);
  1048. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]), 4);
  1049. }
  1050. }else{
  1051. for(i8=0; i8<4; i8++){
  1052. const int x8 = i8&1;
  1053. const int y8 = i8>>1;
  1054. int ref0, dist_scale_factor;
  1055. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1056. continue;
  1057. h->sub_mb_type[i8] = sub_mb_type;
  1058. if(IS_INTRA(mb_type_col)){
  1059. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1060. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1061. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1062. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1063. continue;
  1064. }
  1065. ref0 = l1ref0[x8 + y8*h->b8_stride];
  1066. dist_scale_factor = h->dist_scale_factor[ref0];
  1067. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1068. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1069. for(i4=0; i4<4; i4++){
  1070. const int16_t *mv_col = l1mv0[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1071. int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
  1072. mv_l0[0] = (dist_scale_factor * mv_col[0] + 128) >> 8;
  1073. mv_l0[1] = (dist_scale_factor * mv_col[1] + 128) >> 8;
  1074. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
  1075. pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  1076. }
  1077. }
  1078. }
  1079. }
  1080. }
  1081. static inline void write_back_motion(H264Context *h, int mb_type){
  1082. MpegEncContext * const s = &h->s;
  1083. const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1084. const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1085. int list;
  1086. for(list=0; list<2; list++){
  1087. int y;
  1088. if((!IS_8X8(mb_type)) && !USES_LIST(mb_type, list)){
  1089. if(1){ //FIXME skip or never read if mb_type doesnt use it
  1090. for(y=0; y<4; y++){
  1091. *(uint64_t*)s->current_picture.motion_val[list][b_xy + 0 + y*h->b_stride]=
  1092. *(uint64_t*)s->current_picture.motion_val[list][b_xy + 2 + y*h->b_stride]= 0;
  1093. }
  1094. if( h->pps.cabac ) {
  1095. /* FIXME needed ? */
  1096. for(y=0; y<4; y++){
  1097. *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]=
  1098. *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= 0;
  1099. }
  1100. }
  1101. for(y=0; y<2; y++){
  1102. *(uint16_t*)&s->current_picture.ref_index[list][b8_xy + y*h->b8_stride]= (LIST_NOT_USED&0xFF)*0x0101;
  1103. }
  1104. }
  1105. continue;
  1106. }
  1107. for(y=0; y<4; y++){
  1108. *(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];
  1109. *(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];
  1110. }
  1111. if( h->pps.cabac ) {
  1112. for(y=0; y<4; y++){
  1113. *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
  1114. *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
  1115. }
  1116. }
  1117. for(y=0; y<2; y++){
  1118. s->current_picture.ref_index[list][b8_xy + 0 + y*h->b8_stride]= h->ref_cache[list][scan8[0]+0 + 16*y];
  1119. s->current_picture.ref_index[list][b8_xy + 1 + y*h->b8_stride]= h->ref_cache[list][scan8[0]+2 + 16*y];
  1120. }
  1121. }
  1122. if(h->slice_type == B_TYPE && h->pps.cabac){
  1123. if(IS_8X8(mb_type)){
  1124. h->direct_table[b8_xy+1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
  1125. h->direct_table[b8_xy+0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
  1126. h->direct_table[b8_xy+1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
  1127. }
  1128. }
  1129. }
  1130. /**
  1131. * Decodes a network abstraction layer unit.
  1132. * @param consumed is the number of bytes used as input
  1133. * @param length is the length of the array
  1134. * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp ttailing?
  1135. * @returns decoded bytes, might be src+1 if no escapes
  1136. */
  1137. static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){
  1138. int i, si, di;
  1139. uint8_t *dst;
  1140. // src[0]&0x80; //forbidden bit
  1141. h->nal_ref_idc= src[0]>>5;
  1142. h->nal_unit_type= src[0]&0x1F;
  1143. src++; length--;
  1144. #if 0
  1145. for(i=0; i<length; i++)
  1146. printf("%2X ", src[i]);
  1147. #endif
  1148. for(i=0; i+1<length; i+=2){
  1149. if(src[i]) continue;
  1150. if(i>0 && src[i-1]==0) i--;
  1151. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  1152. if(src[i+2]!=3){
  1153. /* startcode, so we must be past the end */
  1154. length=i;
  1155. }
  1156. break;
  1157. }
  1158. }
  1159. if(i>=length-1){ //no escaped 0
  1160. *dst_length= length;
  1161. *consumed= length+1; //+1 for the header
  1162. return src;
  1163. }
  1164. h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length);
  1165. dst= h->rbsp_buffer;
  1166. //printf("deoding esc\n");
  1167. si=di=0;
  1168. while(si<length){
  1169. //remove escapes (very rare 1:2^22)
  1170. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  1171. if(src[si+2]==3){ //escape
  1172. dst[di++]= 0;
  1173. dst[di++]= 0;
  1174. si+=3;
  1175. continue;
  1176. }else //next start code
  1177. break;
  1178. }
  1179. dst[di++]= src[si++];
  1180. }
  1181. *dst_length= di;
  1182. *consumed= si + 1;//+1 for the header
  1183. //FIXME store exact number of bits in the getbitcontext (its needed for decoding)
  1184. return dst;
  1185. }
  1186. #if 0
  1187. /**
  1188. * @param src the data which should be escaped
  1189. * @param dst the target buffer, dst+1 == src is allowed as a special case
  1190. * @param length the length of the src data
  1191. * @param dst_length the length of the dst array
  1192. * @returns length of escaped data in bytes or -1 if an error occured
  1193. */
  1194. static int encode_nal(H264Context *h, uint8_t *dst, uint8_t *src, int length, int dst_length){
  1195. int i, escape_count, si, di;
  1196. uint8_t *temp;
  1197. assert(length>=0);
  1198. assert(dst_length>0);
  1199. dst[0]= (h->nal_ref_idc<<5) + h->nal_unit_type;
  1200. if(length==0) return 1;
  1201. escape_count= 0;
  1202. for(i=0; i<length; i+=2){
  1203. if(src[i]) continue;
  1204. if(i>0 && src[i-1]==0)
  1205. i--;
  1206. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  1207. escape_count++;
  1208. i+=2;
  1209. }
  1210. }
  1211. if(escape_count==0){
  1212. if(dst+1 != src)
  1213. memcpy(dst+1, src, length);
  1214. return length + 1;
  1215. }
  1216. if(length + escape_count + 1> dst_length)
  1217. return -1;
  1218. //this should be damn rare (hopefully)
  1219. h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length + escape_count);
  1220. temp= h->rbsp_buffer;
  1221. //printf("encoding esc\n");
  1222. si= 0;
  1223. di= 0;
  1224. while(si < length){
  1225. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  1226. temp[di++]= 0; si++;
  1227. temp[di++]= 0; si++;
  1228. temp[di++]= 3;
  1229. temp[di++]= src[si++];
  1230. }
  1231. else
  1232. temp[di++]= src[si++];
  1233. }
  1234. memcpy(dst+1, temp, length+escape_count);
  1235. assert(di == length+escape_count);
  1236. return di + 1;
  1237. }
  1238. /**
  1239. * write 1,10,100,1000,... for alignment, yes its exactly inverse to mpeg4
  1240. */
  1241. static void encode_rbsp_trailing(PutBitContext *pb){
  1242. int length;
  1243. put_bits(pb, 1, 1);
  1244. length= (-put_bits_count(pb))&7;
  1245. if(length) put_bits(pb, length, 0);
  1246. }
  1247. #endif
  1248. /**
  1249. * identifies the exact end of the bitstream
  1250. * @return the length of the trailing, or 0 if damaged
  1251. */
  1252. static int decode_rbsp_trailing(uint8_t *src){
  1253. int v= *src;
  1254. int r;
  1255. tprintf("rbsp trailing %X\n", v);
  1256. for(r=1; r<9; r++){
  1257. if(v&1) return r;
  1258. v>>=1;
  1259. }
  1260. return 0;
  1261. }
  1262. /**
  1263. * idct tranforms the 16 dc values and dequantize them.
  1264. * @param qp quantization parameter
  1265. */
  1266. static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp){
  1267. const int qmul= dequant_coeff[qp][0];
  1268. #define stride 16
  1269. int i;
  1270. int temp[16]; //FIXME check if this is a good idea
  1271. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1272. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1273. //memset(block, 64, 2*256);
  1274. //return;
  1275. for(i=0; i<4; i++){
  1276. const int offset= y_offset[i];
  1277. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1278. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1279. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1280. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1281. temp[4*i+0]= z0+z3;
  1282. temp[4*i+1]= z1+z2;
  1283. temp[4*i+2]= z1-z2;
  1284. temp[4*i+3]= z0-z3;
  1285. }
  1286. for(i=0; i<4; i++){
  1287. const int offset= x_offset[i];
  1288. const int z0= temp[4*0+i] + temp[4*2+i];
  1289. const int z1= temp[4*0+i] - temp[4*2+i];
  1290. const int z2= temp[4*1+i] - temp[4*3+i];
  1291. const int z3= temp[4*1+i] + temp[4*3+i];
  1292. block[stride*0 +offset]= ((z0 + z3)*qmul + 2)>>2; //FIXME think about merging this into decode_resdual
  1293. block[stride*2 +offset]= ((z1 + z2)*qmul + 2)>>2;
  1294. block[stride*8 +offset]= ((z1 - z2)*qmul + 2)>>2;
  1295. block[stride*10+offset]= ((z0 - z3)*qmul + 2)>>2;
  1296. }
  1297. }
  1298. #if 0
  1299. /**
  1300. * dct tranforms the 16 dc values.
  1301. * @param qp quantization parameter ??? FIXME
  1302. */
  1303. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  1304. // const int qmul= dequant_coeff[qp][0];
  1305. int i;
  1306. int temp[16]; //FIXME check if this is a good idea
  1307. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1308. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1309. for(i=0; i<4; i++){
  1310. const int offset= y_offset[i];
  1311. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1312. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1313. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1314. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1315. temp[4*i+0]= z0+z3;
  1316. temp[4*i+1]= z1+z2;
  1317. temp[4*i+2]= z1-z2;
  1318. temp[4*i+3]= z0-z3;
  1319. }
  1320. for(i=0; i<4; i++){
  1321. const int offset= x_offset[i];
  1322. const int z0= temp[4*0+i] + temp[4*2+i];
  1323. const int z1= temp[4*0+i] - temp[4*2+i];
  1324. const int z2= temp[4*1+i] - temp[4*3+i];
  1325. const int z3= temp[4*1+i] + temp[4*3+i];
  1326. block[stride*0 +offset]= (z0 + z3)>>1;
  1327. block[stride*2 +offset]= (z1 + z2)>>1;
  1328. block[stride*8 +offset]= (z1 - z2)>>1;
  1329. block[stride*10+offset]= (z0 - z3)>>1;
  1330. }
  1331. }
  1332. #endif
  1333. #undef xStride
  1334. #undef stride
  1335. static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp){
  1336. const int qmul= dequant_coeff[qp][0];
  1337. const int stride= 16*2;
  1338. const int xStride= 16;
  1339. int a,b,c,d,e;
  1340. a= block[stride*0 + xStride*0];
  1341. b= block[stride*0 + xStride*1];
  1342. c= block[stride*1 + xStride*0];
  1343. d= block[stride*1 + xStride*1];
  1344. e= a-b;
  1345. a= a+b;
  1346. b= c-d;
  1347. c= c+d;
  1348. block[stride*0 + xStride*0]= ((a+c)*qmul + 0)>>1;
  1349. block[stride*0 + xStride*1]= ((e+b)*qmul + 0)>>1;
  1350. block[stride*1 + xStride*0]= ((a-c)*qmul + 0)>>1;
  1351. block[stride*1 + xStride*1]= ((e-b)*qmul + 0)>>1;
  1352. }
  1353. #if 0
  1354. static void chroma_dc_dct_c(DCTELEM *block){
  1355. const int stride= 16*2;
  1356. const int xStride= 16;
  1357. int a,b,c,d,e;
  1358. a= block[stride*0 + xStride*0];
  1359. b= block[stride*0 + xStride*1];
  1360. c= block[stride*1 + xStride*0];
  1361. d= block[stride*1 + xStride*1];
  1362. e= a-b;
  1363. a= a+b;
  1364. b= c-d;
  1365. c= c+d;
  1366. block[stride*0 + xStride*0]= (a+c);
  1367. block[stride*0 + xStride*1]= (e+b);
  1368. block[stride*1 + xStride*0]= (a-c);
  1369. block[stride*1 + xStride*1]= (e-b);
  1370. }
  1371. #endif
  1372. /**
  1373. * gets the chroma qp.
  1374. */
  1375. static inline int get_chroma_qp(H264Context *h, int qscale){
  1376. return chroma_qp[clip(qscale + h->pps.chroma_qp_index_offset, 0, 51)];
  1377. }
  1378. #if 0
  1379. static void h264_diff_dct_c(DCTELEM *block, uint8_t *src1, uint8_t *src2, int stride){
  1380. int i;
  1381. //FIXME try int temp instead of block
  1382. for(i=0; i<4; i++){
  1383. const int d0= src1[0 + i*stride] - src2[0 + i*stride];
  1384. const int d1= src1[1 + i*stride] - src2[1 + i*stride];
  1385. const int d2= src1[2 + i*stride] - src2[2 + i*stride];
  1386. const int d3= src1[3 + i*stride] - src2[3 + i*stride];
  1387. const int z0= d0 + d3;
  1388. const int z3= d0 - d3;
  1389. const int z1= d1 + d2;
  1390. const int z2= d1 - d2;
  1391. block[0 + 4*i]= z0 + z1;
  1392. block[1 + 4*i]= 2*z3 + z2;
  1393. block[2 + 4*i]= z0 - z1;
  1394. block[3 + 4*i]= z3 - 2*z2;
  1395. }
  1396. for(i=0; i<4; i++){
  1397. const int z0= block[0*4 + i] + block[3*4 + i];
  1398. const int z3= block[0*4 + i] - block[3*4 + i];
  1399. const int z1= block[1*4 + i] + block[2*4 + i];
  1400. const int z2= block[1*4 + i] - block[2*4 + i];
  1401. block[0*4 + i]= z0 + z1;
  1402. block[1*4 + i]= 2*z3 + z2;
  1403. block[2*4 + i]= z0 - z1;
  1404. block[3*4 + i]= z3 - 2*z2;
  1405. }
  1406. }
  1407. #endif
  1408. //FIXME need to check that this doesnt overflow signed 32 bit for low qp, iam not sure, its very close
  1409. //FIXME check that gcc inlines this (and optimizes intra & seperate_dc stuff away)
  1410. static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int seperate_dc){
  1411. int i;
  1412. const int * const quant_table= quant_coeff[qscale];
  1413. const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
  1414. const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
  1415. const unsigned int threshold2= (threshold1<<1);
  1416. int last_non_zero;
  1417. if(seperate_dc){
  1418. if(qscale<=18){
  1419. //avoid overflows
  1420. const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
  1421. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
  1422. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1423. int level= block[0]*quant_coeff[qscale+18][0];
  1424. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1425. if(level>0){
  1426. level= (dc_bias + level)>>(QUANT_SHIFT-2);
  1427. block[0]= level;
  1428. }else{
  1429. level= (dc_bias - level)>>(QUANT_SHIFT-2);
  1430. block[0]= -level;
  1431. }
  1432. // last_non_zero = i;
  1433. }else{
  1434. block[0]=0;
  1435. }
  1436. }else{
  1437. const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
  1438. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
  1439. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1440. int level= block[0]*quant_table[0];
  1441. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1442. if(level>0){
  1443. level= (dc_bias + level)>>(QUANT_SHIFT+1);
  1444. block[0]= level;
  1445. }else{
  1446. level= (dc_bias - level)>>(QUANT_SHIFT+1);
  1447. block[0]= -level;
  1448. }
  1449. // last_non_zero = i;
  1450. }else{
  1451. block[0]=0;
  1452. }
  1453. }
  1454. last_non_zero= 0;
  1455. i=1;
  1456. }else{
  1457. last_non_zero= -1;
  1458. i=0;
  1459. }
  1460. for(; i<16; i++){
  1461. const int j= scantable[i];
  1462. int level= block[j]*quant_table[j];
  1463. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  1464. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  1465. if(((unsigned)(level+threshold1))>threshold2){
  1466. if(level>0){
  1467. level= (bias + level)>>QUANT_SHIFT;
  1468. block[j]= level;
  1469. }else{
  1470. level= (bias - level)>>QUANT_SHIFT;
  1471. block[j]= -level;
  1472. }
  1473. last_non_zero = i;
  1474. }else{
  1475. block[j]=0;
  1476. }
  1477. }
  1478. return last_non_zero;
  1479. }
  1480. static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){
  1481. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1482. ((uint32_t*)(src+0*stride))[0]= a;
  1483. ((uint32_t*)(src+1*stride))[0]= a;
  1484. ((uint32_t*)(src+2*stride))[0]= a;
  1485. ((uint32_t*)(src+3*stride))[0]= a;
  1486. }
  1487. static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){
  1488. ((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101;
  1489. ((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101;
  1490. ((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101;
  1491. ((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101;
  1492. }
  1493. static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1494. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
  1495. + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
  1496. ((uint32_t*)(src+0*stride))[0]=
  1497. ((uint32_t*)(src+1*stride))[0]=
  1498. ((uint32_t*)(src+2*stride))[0]=
  1499. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1500. }
  1501. static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1502. const int dc= ( src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
  1503. ((uint32_t*)(src+0*stride))[0]=
  1504. ((uint32_t*)(src+1*stride))[0]=
  1505. ((uint32_t*)(src+2*stride))[0]=
  1506. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1507. }
  1508. static void pred4x4_top_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1509. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2;
  1510. ((uint32_t*)(src+0*stride))[0]=
  1511. ((uint32_t*)(src+1*stride))[0]=
  1512. ((uint32_t*)(src+2*stride))[0]=
  1513. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1514. }
  1515. static void pred4x4_128_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1516. ((uint32_t*)(src+0*stride))[0]=
  1517. ((uint32_t*)(src+1*stride))[0]=
  1518. ((uint32_t*)(src+2*stride))[0]=
  1519. ((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U;
  1520. }
  1521. #define LOAD_TOP_RIGHT_EDGE\
  1522. const int t4= topright[0];\
  1523. const int t5= topright[1];\
  1524. const int t6= topright[2];\
  1525. const int t7= topright[3];\
  1526. #define LOAD_LEFT_EDGE\
  1527. const int l0= src[-1+0*stride];\
  1528. const int l1= src[-1+1*stride];\
  1529. const int l2= src[-1+2*stride];\
  1530. const int l3= src[-1+3*stride];\
  1531. #define LOAD_TOP_EDGE\
  1532. const int t0= src[ 0-1*stride];\
  1533. const int t1= src[ 1-1*stride];\
  1534. const int t2= src[ 2-1*stride];\
  1535. const int t3= src[ 3-1*stride];\
  1536. static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){
  1537. const int lt= src[-1-1*stride];
  1538. LOAD_TOP_EDGE
  1539. LOAD_LEFT_EDGE
  1540. src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
  1541. src[0+2*stride]=
  1542. src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
  1543. src[0+1*stride]=
  1544. src[1+2*stride]=
  1545. src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
  1546. src[0+0*stride]=
  1547. src[1+1*stride]=
  1548. src[2+2*stride]=
  1549. src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1550. src[1+0*stride]=
  1551. src[2+1*stride]=
  1552. src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1553. src[2+0*stride]=
  1554. src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1555. src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1556. }
  1557. static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){
  1558. LOAD_TOP_EDGE
  1559. LOAD_TOP_RIGHT_EDGE
  1560. // LOAD_LEFT_EDGE
  1561. src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
  1562. src[1+0*stride]=
  1563. src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
  1564. src[2+0*stride]=
  1565. src[1+1*stride]=
  1566. src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
  1567. src[3+0*stride]=
  1568. src[2+1*stride]=
  1569. src[1+2*stride]=
  1570. src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
  1571. src[3+1*stride]=
  1572. src[2+2*stride]=
  1573. src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
  1574. src[3+2*stride]=
  1575. src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
  1576. src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
  1577. }
  1578. static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
  1579. const int lt= src[-1-1*stride];
  1580. LOAD_TOP_EDGE
  1581. LOAD_LEFT_EDGE
  1582. const __attribute__((unused)) int unu= l3;
  1583. src[0+0*stride]=
  1584. src[1+2*stride]=(lt + t0 + 1)>>1;
  1585. src[1+0*stride]=
  1586. src[2+2*stride]=(t0 + t1 + 1)>>1;
  1587. src[2+0*stride]=
  1588. src[3+2*stride]=(t1 + t2 + 1)>>1;
  1589. src[3+0*stride]=(t2 + t3 + 1)>>1;
  1590. src[0+1*stride]=
  1591. src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1592. src[1+1*stride]=
  1593. src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1594. src[2+1*stride]=
  1595. src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1596. src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1597. src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1598. src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1599. }
  1600. static void pred4x4_vertical_left_c(uint8_t *src, uint8_t *topright, int stride){
  1601. LOAD_TOP_EDGE
  1602. LOAD_TOP_RIGHT_EDGE
  1603. const __attribute__((unused)) int unu= t7;
  1604. src[0+0*stride]=(t0 + t1 + 1)>>1;
  1605. src[1+0*stride]=
  1606. src[0+2*stride]=(t1 + t2 + 1)>>1;
  1607. src[2+0*stride]=
  1608. src[1+2*stride]=(t2 + t3 + 1)>>1;
  1609. src[3+0*stride]=
  1610. src[2+2*stride]=(t3 + t4+ 1)>>1;
  1611. src[3+2*stride]=(t4 + t5+ 1)>>1;
  1612. src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1613. src[1+1*stride]=
  1614. src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1615. src[2+1*stride]=
  1616. src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
  1617. src[3+1*stride]=
  1618. src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
  1619. src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
  1620. }
  1621. static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){
  1622. LOAD_LEFT_EDGE
  1623. src[0+0*stride]=(l0 + l1 + 1)>>1;
  1624. src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1625. src[2+0*stride]=
  1626. src[0+1*stride]=(l1 + l2 + 1)>>1;
  1627. src[3+0*stride]=
  1628. src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1629. src[2+1*stride]=
  1630. src[0+2*stride]=(l2 + l3 + 1)>>1;
  1631. src[3+1*stride]=
  1632. src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
  1633. src[3+2*stride]=
  1634. src[1+3*stride]=
  1635. src[0+3*stride]=
  1636. src[2+2*stride]=
  1637. src[2+3*stride]=
  1638. src[3+3*stride]=l3;
  1639. }
  1640. static void pred4x4_horizontal_down_c(uint8_t *src, uint8_t *topright, int stride){
  1641. const int lt= src[-1-1*stride];
  1642. LOAD_TOP_EDGE
  1643. LOAD_LEFT_EDGE
  1644. const __attribute__((unused)) int unu= t3;
  1645. src[0+0*stride]=
  1646. src[2+1*stride]=(lt + l0 + 1)>>1;
  1647. src[1+0*stride]=
  1648. src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1649. src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1650. src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1651. src[0+1*stride]=
  1652. src[2+2*stride]=(l0 + l1 + 1)>>1;
  1653. src[1+1*stride]=
  1654. src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1655. src[0+2*stride]=
  1656. src[2+3*stride]=(l1 + l2+ 1)>>1;
  1657. src[1+2*stride]=
  1658. src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1659. src[0+3*stride]=(l2 + l3 + 1)>>1;
  1660. src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1661. }
  1662. static void pred16x16_vertical_c(uint8_t *src, int stride){
  1663. int i;
  1664. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1665. const uint32_t b= ((uint32_t*)(src-stride))[1];
  1666. const uint32_t c= ((uint32_t*)(src-stride))[2];
  1667. const uint32_t d= ((uint32_t*)(src-stride))[3];
  1668. for(i=0; i<16; i++){
  1669. ((uint32_t*)(src+i*stride))[0]= a;
  1670. ((uint32_t*)(src+i*stride))[1]= b;
  1671. ((uint32_t*)(src+i*stride))[2]= c;
  1672. ((uint32_t*)(src+i*stride))[3]= d;
  1673. }
  1674. }
  1675. static void pred16x16_horizontal_c(uint8_t *src, int stride){
  1676. int i;
  1677. for(i=0; i<16; i++){
  1678. ((uint32_t*)(src+i*stride))[0]=
  1679. ((uint32_t*)(src+i*stride))[1]=
  1680. ((uint32_t*)(src+i*stride))[2]=
  1681. ((uint32_t*)(src+i*stride))[3]= src[-1+i*stride]*0x01010101;
  1682. }
  1683. }
  1684. static void pred16x16_dc_c(uint8_t *src, int stride){
  1685. int i, dc=0;
  1686. for(i=0;i<16; i++){
  1687. dc+= src[-1+i*stride];
  1688. }
  1689. for(i=0;i<16; i++){
  1690. dc+= src[i-stride];
  1691. }
  1692. dc= 0x01010101*((dc + 16)>>5);
  1693. for(i=0; i<16; i++){
  1694. ((uint32_t*)(src+i*stride))[0]=
  1695. ((uint32_t*)(src+i*stride))[1]=
  1696. ((uint32_t*)(src+i*stride))[2]=
  1697. ((uint32_t*)(src+i*stride))[3]= dc;
  1698. }
  1699. }
  1700. static void pred16x16_left_dc_c(uint8_t *src, int stride){
  1701. int i, dc=0;
  1702. for(i=0;i<16; i++){
  1703. dc+= src[-1+i*stride];
  1704. }
  1705. dc= 0x01010101*((dc + 8)>>4);
  1706. for(i=0; i<16; i++){
  1707. ((uint32_t*)(src+i*stride))[0]=
  1708. ((uint32_t*)(src+i*stride))[1]=
  1709. ((uint32_t*)(src+i*stride))[2]=
  1710. ((uint32_t*)(src+i*stride))[3]= dc;
  1711. }
  1712. }
  1713. static void pred16x16_top_dc_c(uint8_t *src, int stride){
  1714. int i, dc=0;
  1715. for(i=0;i<16; i++){
  1716. dc+= src[i-stride];
  1717. }
  1718. dc= 0x01010101*((dc + 8)>>4);
  1719. for(i=0; i<16; i++){
  1720. ((uint32_t*)(src+i*stride))[0]=
  1721. ((uint32_t*)(src+i*stride))[1]=
  1722. ((uint32_t*)(src+i*stride))[2]=
  1723. ((uint32_t*)(src+i*stride))[3]= dc;
  1724. }
  1725. }
  1726. static void pred16x16_128_dc_c(uint8_t *src, int stride){
  1727. int i;
  1728. for(i=0; i<16; i++){
  1729. ((uint32_t*)(src+i*stride))[0]=
  1730. ((uint32_t*)(src+i*stride))[1]=
  1731. ((uint32_t*)(src+i*stride))[2]=
  1732. ((uint32_t*)(src+i*stride))[3]= 0x01010101U*128U;
  1733. }
  1734. }
  1735. static inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){
  1736. int i, j, k;
  1737. int a;
  1738. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  1739. const uint8_t * const src0 = src+7-stride;
  1740. const uint8_t *src1 = src+8*stride-1;
  1741. const uint8_t *src2 = src1-2*stride; // == src+6*stride-1;
  1742. int H = src0[1] - src0[-1];
  1743. int V = src1[0] - src2[ 0];
  1744. for(k=2; k<=8; ++k) {
  1745. src1 += stride; src2 -= stride;
  1746. H += k*(src0[k] - src0[-k]);
  1747. V += k*(src1[0] - src2[ 0]);
  1748. }
  1749. if(svq3){
  1750. H = ( 5*(H/4) ) / 16;
  1751. V = ( 5*(V/4) ) / 16;
  1752. /* required for 100% accuracy */
  1753. i = H; H = V; V = i;
  1754. }else{
  1755. H = ( 5*H+32 ) >> 6;
  1756. V = ( 5*V+32 ) >> 6;
  1757. }
  1758. a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
  1759. for(j=16; j>0; --j) {
  1760. int b = a;
  1761. a += V;
  1762. for(i=-16; i<0; i+=4) {
  1763. src[16+i] = cm[ (b ) >> 5 ];
  1764. src[17+i] = cm[ (b+ H) >> 5 ];
  1765. src[18+i] = cm[ (b+2*H) >> 5 ];
  1766. src[19+i] = cm[ (b+3*H) >> 5 ];
  1767. b += 4*H;
  1768. }
  1769. src += stride;
  1770. }
  1771. }
  1772. static void pred16x16_plane_c(uint8_t *src, int stride){
  1773. pred16x16_plane_compat_c(src, stride, 0);
  1774. }
  1775. static void pred8x8_vertical_c(uint8_t *src, int stride){
  1776. int i;
  1777. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1778. const uint32_t b= ((uint32_t*)(src-stride))[1];
  1779. for(i=0; i<8; i++){
  1780. ((uint32_t*)(src+i*stride))[0]= a;
  1781. ((uint32_t*)(src+i*stride))[1]= b;
  1782. }
  1783. }
  1784. static void pred8x8_horizontal_c(uint8_t *src, int stride){
  1785. int i;
  1786. for(i=0; i<8; i++){
  1787. ((uint32_t*)(src+i*stride))[0]=
  1788. ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101;
  1789. }
  1790. }
  1791. static void pred8x8_128_dc_c(uint8_t *src, int stride){
  1792. int i;
  1793. for(i=0; i<4; i++){
  1794. ((uint32_t*)(src+i*stride))[0]=
  1795. ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
  1796. }
  1797. for(i=4; i<8; i++){
  1798. ((uint32_t*)(src+i*stride))[0]=
  1799. ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
  1800. }
  1801. }
  1802. static void pred8x8_left_dc_c(uint8_t *src, int stride){
  1803. int i;
  1804. int dc0, dc2;
  1805. dc0=dc2=0;
  1806. for(i=0;i<4; i++){
  1807. dc0+= src[-1+i*stride];
  1808. dc2+= src[-1+(i+4)*stride];
  1809. }
  1810. dc0= 0x01010101*((dc0 + 2)>>2);
  1811. dc2= 0x01010101*((dc2 + 2)>>2);
  1812. for(i=0; i<4; i++){
  1813. ((uint32_t*)(src+i*stride))[0]=
  1814. ((uint32_t*)(src+i*stride))[1]= dc0;
  1815. }
  1816. for(i=4; i<8; i++){
  1817. ((uint32_t*)(src+i*stride))[0]=
  1818. ((uint32_t*)(src+i*stride))[1]= dc2;
  1819. }
  1820. }
  1821. static void pred8x8_top_dc_c(uint8_t *src, int stride){
  1822. int i;
  1823. int dc0, dc1;
  1824. dc0=dc1=0;
  1825. for(i=0;i<4; i++){
  1826. dc0+= src[i-stride];
  1827. dc1+= src[4+i-stride];
  1828. }
  1829. dc0= 0x01010101*((dc0 + 2)>>2);
  1830. dc1= 0x01010101*((dc1 + 2)>>2);
  1831. for(i=0; i<4; i++){
  1832. ((uint32_t*)(src+i*stride))[0]= dc0;
  1833. ((uint32_t*)(src+i*stride))[1]= dc1;
  1834. }
  1835. for(i=4; i<8; i++){
  1836. ((uint32_t*)(src+i*stride))[0]= dc0;
  1837. ((uint32_t*)(src+i*stride))[1]= dc1;
  1838. }
  1839. }
  1840. static void pred8x8_dc_c(uint8_t *src, int stride){
  1841. int i;
  1842. int dc0, dc1, dc2, dc3;
  1843. dc0=dc1=dc2=0;
  1844. for(i=0;i<4; i++){
  1845. dc0+= src[-1+i*stride] + src[i-stride];
  1846. dc1+= src[4+i-stride];
  1847. dc2+= src[-1+(i+4)*stride];
  1848. }
  1849. dc3= 0x01010101*((dc1 + dc2 + 4)>>3);
  1850. dc0= 0x01010101*((dc0 + 4)>>3);
  1851. dc1= 0x01010101*((dc1 + 2)>>2);
  1852. dc2= 0x01010101*((dc2 + 2)>>2);
  1853. for(i=0; i<4; i++){
  1854. ((uint32_t*)(src+i*stride))[0]= dc0;
  1855. ((uint32_t*)(src+i*stride))[1]= dc1;
  1856. }
  1857. for(i=4; i<8; i++){
  1858. ((uint32_t*)(src+i*stride))[0]= dc2;
  1859. ((uint32_t*)(src+i*stride))[1]= dc3;
  1860. }
  1861. }
  1862. static void pred8x8_plane_c(uint8_t *src, int stride){
  1863. int j, k;
  1864. int a;
  1865. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  1866. const uint8_t * const src0 = src+3-stride;
  1867. const uint8_t *src1 = src+4*stride-1;
  1868. const uint8_t *src2 = src1-2*stride; // == src+2*stride-1;
  1869. int H = src0[1] - src0[-1];
  1870. int V = src1[0] - src2[ 0];
  1871. for(k=2; k<=4; ++k) {
  1872. src1 += stride; src2 -= stride;
  1873. H += k*(src0[k] - src0[-k]);
  1874. V += k*(src1[0] - src2[ 0]);
  1875. }
  1876. H = ( 17*H+16 ) >> 5;
  1877. V = ( 17*V+16 ) >> 5;
  1878. a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
  1879. for(j=8; j>0; --j) {
  1880. int b = a;
  1881. a += V;
  1882. src[0] = cm[ (b ) >> 5 ];
  1883. src[1] = cm[ (b+ H) >> 5 ];
  1884. src[2] = cm[ (b+2*H) >> 5 ];
  1885. src[3] = cm[ (b+3*H) >> 5 ];
  1886. src[4] = cm[ (b+4*H) >> 5 ];
  1887. src[5] = cm[ (b+5*H) >> 5 ];
  1888. src[6] = cm[ (b+6*H) >> 5 ];
  1889. src[7] = cm[ (b+7*H) >> 5 ];
  1890. src += stride;
  1891. }
  1892. }
  1893. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  1894. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1895. int src_x_offset, int src_y_offset,
  1896. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  1897. MpegEncContext * const s = &h->s;
  1898. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  1899. const int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  1900. const int luma_xy= (mx&3) + ((my&3)<<2);
  1901. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*s->linesize;
  1902. uint8_t * src_cb= pic->data[1] + (mx>>3) + (my>>3)*s->uvlinesize;
  1903. uint8_t * src_cr= pic->data[2] + (mx>>3) + (my>>3)*s->uvlinesize;
  1904. int extra_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16; //FIXME increase edge?, IMHO not worth it
  1905. int extra_height= extra_width;
  1906. int emu=0;
  1907. const int full_mx= mx>>2;
  1908. const int full_my= my>>2;
  1909. assert(pic->data[0]);
  1910. if(mx&7) extra_width -= 3;
  1911. if(my&7) extra_height -= 3;
  1912. if( full_mx < 0-extra_width
  1913. || full_my < 0-extra_height
  1914. || full_mx + 16/*FIXME*/ > s->width + extra_width
  1915. || full_my + 16/*FIXME*/ > s->height + extra_height){
  1916. ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*s->linesize, s->linesize, 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, s->width, s->height);
  1917. src_y= s->edge_emu_buffer + 2 + 2*s->linesize;
  1918. emu=1;
  1919. }
  1920. qpix_op[luma_xy](dest_y, src_y, s->linesize); //FIXME try variable height perhaps?
  1921. if(!square){
  1922. qpix_op[luma_xy](dest_y + delta, src_y + delta, s->linesize);
  1923. }
  1924. if(s->flags&CODEC_FLAG_GRAY) return;
  1925. if(emu){
  1926. ff_emulated_edge_mc(s->edge_emu_buffer, src_cb, s->uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), s->width>>1, s->height>>1);
  1927. src_cb= s->edge_emu_buffer;
  1928. }
  1929. chroma_op(dest_cb, src_cb, s->uvlinesize, chroma_height, mx&7, my&7);
  1930. if(emu){
  1931. ff_emulated_edge_mc(s->edge_emu_buffer, src_cr, s->uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), s->width>>1, s->height>>1);
  1932. src_cr= s->edge_emu_buffer;
  1933. }
  1934. chroma_op(dest_cr, src_cr, s->uvlinesize, chroma_height, mx&7, my&7);
  1935. }
  1936. static inline void mc_part_std(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. dest_y += 2*x_offset + 2*y_offset*s-> linesize;
  1946. dest_cb += x_offset + y_offset*s->uvlinesize;
  1947. dest_cr += x_offset + y_offset*s->uvlinesize;
  1948. x_offset += 8*s->mb_x;
  1949. y_offset += 8*s->mb_y;
  1950. if(list0){
  1951. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  1952. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  1953. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1954. qpix_op, chroma_op);
  1955. qpix_op= qpix_avg;
  1956. chroma_op= chroma_avg;
  1957. }
  1958. if(list1){
  1959. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  1960. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  1961. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1962. qpix_op, chroma_op);
  1963. }
  1964. }
  1965. static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
  1966. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1967. int x_offset, int y_offset,
  1968. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  1969. h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
  1970. h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
  1971. int list0, int list1){
  1972. MpegEncContext * const s = &h->s;
  1973. dest_y += 2*x_offset + 2*y_offset*s-> linesize;
  1974. dest_cb += x_offset + y_offset*s->uvlinesize;
  1975. dest_cr += x_offset + y_offset*s->uvlinesize;
  1976. x_offset += 8*s->mb_x;
  1977. y_offset += 8*s->mb_y;
  1978. if(list0 && list1){
  1979. /* don't optimize for luma-only case, since B-frames usually
  1980. * use implicit weights => chroma too. */
  1981. uint8_t *tmp_cb = s->obmc_scratchpad;
  1982. uint8_t *tmp_cr = tmp_cb + 8*s->uvlinesize;
  1983. uint8_t *tmp_y = tmp_cr + 8*s->uvlinesize;
  1984. int refn0 = h->ref_cache[0][ scan8[n] ];
  1985. int refn1 = h->ref_cache[1][ scan8[n] ];
  1986. mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
  1987. dest_y, dest_cb, dest_cr,
  1988. x_offset, y_offset, qpix_put, chroma_put);
  1989. mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
  1990. tmp_y, tmp_cb, tmp_cr,
  1991. x_offset, y_offset, qpix_put, chroma_put);
  1992. if(h->use_weight == 2){
  1993. int weight0 = h->implicit_weight[refn0][refn1];
  1994. int weight1 = 64 - weight0;
  1995. luma_weight_avg( dest_y, tmp_y, s-> linesize, 5, weight0, weight1, 0, 0);
  1996. chroma_weight_avg(dest_cb, tmp_cb, s->uvlinesize, 5, weight0, weight1, 0, 0);
  1997. chroma_weight_avg(dest_cr, tmp_cr, s->uvlinesize, 5, weight0, weight1, 0, 0);
  1998. }else{
  1999. luma_weight_avg(dest_y, tmp_y, s->linesize, h->luma_log2_weight_denom,
  2000. h->luma_weight[0][refn0], h->luma_weight[1][refn1],
  2001. h->luma_offset[0][refn0], h->luma_offset[1][refn1]);
  2002. chroma_weight_avg(dest_cb, tmp_cb, s->uvlinesize, h->chroma_log2_weight_denom,
  2003. h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
  2004. h->chroma_offset[0][refn0][0], h->chroma_offset[1][refn1][0]);
  2005. chroma_weight_avg(dest_cr, tmp_cr, s->uvlinesize, h->chroma_log2_weight_denom,
  2006. h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
  2007. h->chroma_offset[0][refn0][1], h->chroma_offset[1][refn1][1]);
  2008. }
  2009. }else{
  2010. int list = list1 ? 1 : 0;
  2011. int refn = h->ref_cache[list][ scan8[n] ];
  2012. Picture *ref= &h->ref_list[list][refn];
  2013. mc_dir_part(h, ref, n, square, chroma_height, delta, list,
  2014. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2015. qpix_put, chroma_put);
  2016. luma_weight_op(dest_y, s->linesize, h->luma_log2_weight_denom,
  2017. h->luma_weight[list][refn], h->luma_offset[list][refn]);
  2018. if(h->use_weight_chroma){
  2019. chroma_weight_op(dest_cb, s->uvlinesize, h->chroma_log2_weight_denom,
  2020. h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
  2021. chroma_weight_op(dest_cr, s->uvlinesize, h->chroma_log2_weight_denom,
  2022. h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
  2023. }
  2024. }
  2025. }
  2026. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  2027. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2028. int x_offset, int y_offset,
  2029. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2030. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2031. h264_weight_func *weight_op, h264_biweight_func *weight_avg,
  2032. int list0, int list1){
  2033. if((h->use_weight==2 && list0 && list1
  2034. && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
  2035. || h->use_weight==1)
  2036. mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2037. x_offset, y_offset, qpix_put, chroma_put,
  2038. weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
  2039. else
  2040. mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2041. x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
  2042. }
  2043. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2044. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  2045. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
  2046. h264_weight_func *weight_op, h264_biweight_func *weight_avg){
  2047. MpegEncContext * const s = &h->s;
  2048. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2049. const int mb_type= s->current_picture.mb_type[mb_xy];
  2050. assert(IS_INTER(mb_type));
  2051. if(IS_16X16(mb_type)){
  2052. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  2053. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  2054. &weight_op[0], &weight_avg[0],
  2055. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2056. }else if(IS_16X8(mb_type)){
  2057. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  2058. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2059. &weight_op[1], &weight_avg[1],
  2060. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2061. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  2062. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2063. &weight_op[1], &weight_avg[1],
  2064. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2065. }else if(IS_8X16(mb_type)){
  2066. mc_part(h, 0, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 0, 0,
  2067. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2068. &weight_op[2], &weight_avg[2],
  2069. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2070. mc_part(h, 4, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 4, 0,
  2071. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2072. &weight_op[2], &weight_avg[2],
  2073. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2074. }else{
  2075. int i;
  2076. assert(IS_8X8(mb_type));
  2077. for(i=0; i<4; i++){
  2078. const int sub_mb_type= h->sub_mb_type[i];
  2079. const int n= 4*i;
  2080. int x_offset= (i&1)<<2;
  2081. int y_offset= (i&2)<<1;
  2082. if(IS_SUB_8X8(sub_mb_type)){
  2083. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2084. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2085. &weight_op[3], &weight_avg[3],
  2086. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2087. }else if(IS_SUB_8X4(sub_mb_type)){
  2088. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2089. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2090. &weight_op[4], &weight_avg[4],
  2091. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2092. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  2093. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2094. &weight_op[4], &weight_avg[4],
  2095. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2096. }else if(IS_SUB_4X8(sub_mb_type)){
  2097. mc_part(h, n , 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2098. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2099. &weight_op[5], &weight_avg[5],
  2100. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2101. mc_part(h, n+1, 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
  2102. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2103. &weight_op[5], &weight_avg[5],
  2104. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2105. }else{
  2106. int j;
  2107. assert(IS_SUB_4X4(sub_mb_type));
  2108. for(j=0; j<4; j++){
  2109. int sub_x_offset= x_offset + 2*(j&1);
  2110. int sub_y_offset= y_offset + (j&2);
  2111. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  2112. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2113. &weight_op[6], &weight_avg[6],
  2114. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2115. }
  2116. }
  2117. }
  2118. }
  2119. }
  2120. static void decode_init_vlc(H264Context *h){
  2121. static int done = 0;
  2122. if (!done) {
  2123. int i;
  2124. done = 1;
  2125. init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
  2126. &chroma_dc_coeff_token_len [0], 1, 1,
  2127. &chroma_dc_coeff_token_bits[0], 1, 1, 1);
  2128. for(i=0; i<4; i++){
  2129. init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
  2130. &coeff_token_len [i][0], 1, 1,
  2131. &coeff_token_bits[i][0], 1, 1, 1);
  2132. }
  2133. for(i=0; i<3; i++){
  2134. init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
  2135. &chroma_dc_total_zeros_len [i][0], 1, 1,
  2136. &chroma_dc_total_zeros_bits[i][0], 1, 1, 1);
  2137. }
  2138. for(i=0; i<15; i++){
  2139. init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
  2140. &total_zeros_len [i][0], 1, 1,
  2141. &total_zeros_bits[i][0], 1, 1, 1);
  2142. }
  2143. for(i=0; i<6; i++){
  2144. init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
  2145. &run_len [i][0], 1, 1,
  2146. &run_bits[i][0], 1, 1, 1);
  2147. }
  2148. init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
  2149. &run_len [6][0], 1, 1,
  2150. &run_bits[6][0], 1, 1, 1);
  2151. }
  2152. }
  2153. /**
  2154. * Sets the intra prediction function pointers.
  2155. */
  2156. static void init_pred_ptrs(H264Context *h){
  2157. // MpegEncContext * const s = &h->s;
  2158. h->pred4x4[VERT_PRED ]= pred4x4_vertical_c;
  2159. h->pred4x4[HOR_PRED ]= pred4x4_horizontal_c;
  2160. h->pred4x4[DC_PRED ]= pred4x4_dc_c;
  2161. h->pred4x4[DIAG_DOWN_LEFT_PRED ]= pred4x4_down_left_c;
  2162. h->pred4x4[DIAG_DOWN_RIGHT_PRED]= pred4x4_down_right_c;
  2163. h->pred4x4[VERT_RIGHT_PRED ]= pred4x4_vertical_right_c;
  2164. h->pred4x4[HOR_DOWN_PRED ]= pred4x4_horizontal_down_c;
  2165. h->pred4x4[VERT_LEFT_PRED ]= pred4x4_vertical_left_c;
  2166. h->pred4x4[HOR_UP_PRED ]= pred4x4_horizontal_up_c;
  2167. h->pred4x4[LEFT_DC_PRED ]= pred4x4_left_dc_c;
  2168. h->pred4x4[TOP_DC_PRED ]= pred4x4_top_dc_c;
  2169. h->pred4x4[DC_128_PRED ]= pred4x4_128_dc_c;
  2170. h->pred8x8[DC_PRED8x8 ]= pred8x8_dc_c;
  2171. h->pred8x8[VERT_PRED8x8 ]= pred8x8_vertical_c;
  2172. h->pred8x8[HOR_PRED8x8 ]= pred8x8_horizontal_c;
  2173. h->pred8x8[PLANE_PRED8x8 ]= pred8x8_plane_c;
  2174. h->pred8x8[LEFT_DC_PRED8x8]= pred8x8_left_dc_c;
  2175. h->pred8x8[TOP_DC_PRED8x8 ]= pred8x8_top_dc_c;
  2176. h->pred8x8[DC_128_PRED8x8 ]= pred8x8_128_dc_c;
  2177. h->pred16x16[DC_PRED8x8 ]= pred16x16_dc_c;
  2178. h->pred16x16[VERT_PRED8x8 ]= pred16x16_vertical_c;
  2179. h->pred16x16[HOR_PRED8x8 ]= pred16x16_horizontal_c;
  2180. h->pred16x16[PLANE_PRED8x8 ]= pred16x16_plane_c;
  2181. h->pred16x16[LEFT_DC_PRED8x8]= pred16x16_left_dc_c;
  2182. h->pred16x16[TOP_DC_PRED8x8 ]= pred16x16_top_dc_c;
  2183. h->pred16x16[DC_128_PRED8x8 ]= pred16x16_128_dc_c;
  2184. }
  2185. static void free_tables(H264Context *h){
  2186. av_freep(&h->intra4x4_pred_mode);
  2187. av_freep(&h->chroma_pred_mode_table);
  2188. av_freep(&h->cbp_table);
  2189. av_freep(&h->mvd_table[0]);
  2190. av_freep(&h->mvd_table[1]);
  2191. av_freep(&h->direct_table);
  2192. av_freep(&h->non_zero_count);
  2193. av_freep(&h->slice_table_base);
  2194. av_freep(&h->top_border);
  2195. h->slice_table= NULL;
  2196. av_freep(&h->mb2b_xy);
  2197. av_freep(&h->mb2b8_xy);
  2198. av_freep(&h->s.obmc_scratchpad);
  2199. }
  2200. /**
  2201. * allocates tables.
  2202. * needs widzh/height
  2203. */
  2204. static int alloc_tables(H264Context *h){
  2205. MpegEncContext * const s = &h->s;
  2206. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  2207. int x,y;
  2208. CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
  2209. CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
  2210. CHECKED_ALLOCZ(h->slice_table_base , big_mb_num * sizeof(uint8_t))
  2211. CHECKED_ALLOCZ(h->top_border , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2212. CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
  2213. if( h->pps.cabac ) {
  2214. CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
  2215. CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
  2216. CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t));
  2217. CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t));
  2218. }
  2219. memset(h->slice_table_base, -1, big_mb_num * sizeof(uint8_t));
  2220. h->slice_table= h->slice_table_base + s->mb_stride + 1;
  2221. CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint16_t));
  2222. CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint16_t));
  2223. for(y=0; y<s->mb_height; y++){
  2224. for(x=0; x<s->mb_width; x++){
  2225. const int mb_xy= x + y*s->mb_stride;
  2226. const int b_xy = 4*x + 4*y*h->b_stride;
  2227. const int b8_xy= 2*x + 2*y*h->b8_stride;
  2228. h->mb2b_xy [mb_xy]= b_xy;
  2229. h->mb2b8_xy[mb_xy]= b8_xy;
  2230. }
  2231. }
  2232. s->obmc_scratchpad = NULL;
  2233. return 0;
  2234. fail:
  2235. free_tables(h);
  2236. return -1;
  2237. }
  2238. static void common_init(H264Context *h){
  2239. MpegEncContext * const s = &h->s;
  2240. s->width = s->avctx->width;
  2241. s->height = s->avctx->height;
  2242. s->codec_id= s->avctx->codec->id;
  2243. init_pred_ptrs(h);
  2244. s->unrestricted_mv=1;
  2245. s->decode=1; //FIXME
  2246. }
  2247. static int decode_init(AVCodecContext *avctx){
  2248. H264Context *h= avctx->priv_data;
  2249. MpegEncContext * const s = &h->s;
  2250. MPV_decode_defaults(s);
  2251. s->avctx = avctx;
  2252. common_init(h);
  2253. s->out_format = FMT_H264;
  2254. s->workaround_bugs= avctx->workaround_bugs;
  2255. // set defaults
  2256. // s->decode_mb= ff_h263_decode_mb;
  2257. s->low_delay= 1;
  2258. avctx->pix_fmt= PIX_FMT_YUV420P;
  2259. decode_init_vlc(h);
  2260. if(avctx->codec_tag != 0x31637661 && avctx->codec_tag != 0x31435641) // avc1
  2261. h->is_avc = 0;
  2262. else {
  2263. if((avctx->extradata_size == 0) || (avctx->extradata == NULL)) {
  2264. av_log(avctx, AV_LOG_ERROR, "AVC codec requires avcC data\n");
  2265. return -1;
  2266. }
  2267. h->is_avc = 1;
  2268. h->got_avcC = 0;
  2269. }
  2270. return 0;
  2271. }
  2272. static void frame_start(H264Context *h){
  2273. MpegEncContext * const s = &h->s;
  2274. int i;
  2275. MPV_frame_start(s, s->avctx);
  2276. ff_er_frame_start(s);
  2277. assert(s->linesize && s->uvlinesize);
  2278. for(i=0; i<16; i++){
  2279. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  2280. h->chroma_subblock_offset[i]= 2*((scan8[i] - scan8[0])&7) + 2*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2281. }
  2282. for(i=0; i<4; i++){
  2283. h->block_offset[16+i]=
  2284. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2285. }
  2286. /* can't be in alloc_tables because linesize isn't known there.
  2287. * FIXME: redo bipred weight to not require extra buffer? */
  2288. if(!s->obmc_scratchpad)
  2289. s->obmc_scratchpad = av_malloc(16*s->linesize + 2*8*s->uvlinesize);
  2290. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  2291. }
  2292. static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  2293. MpegEncContext * const s = &h->s;
  2294. int i;
  2295. src_y -= linesize;
  2296. src_cb -= uvlinesize;
  2297. src_cr -= uvlinesize;
  2298. h->left_border[0]= h->top_border[s->mb_x][15];
  2299. for(i=1; i<17; i++){
  2300. h->left_border[i]= src_y[15+i* linesize];
  2301. }
  2302. *(uint64_t*)(h->top_border[s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
  2303. *(uint64_t*)(h->top_border[s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
  2304. if(!(s->flags&CODEC_FLAG_GRAY)){
  2305. h->left_border[17 ]= h->top_border[s->mb_x][16+7];
  2306. h->left_border[17+9]= h->top_border[s->mb_x][24+7];
  2307. for(i=1; i<9; i++){
  2308. h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
  2309. h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
  2310. }
  2311. *(uint64_t*)(h->top_border[s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
  2312. *(uint64_t*)(h->top_border[s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
  2313. }
  2314. }
  2315. static inline void xchg_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg){
  2316. MpegEncContext * const s = &h->s;
  2317. int temp8, i;
  2318. uint64_t temp64;
  2319. int deblock_left = (s->mb_x > 0);
  2320. int deblock_top = (s->mb_y > 0);
  2321. src_y -= linesize + 1;
  2322. src_cb -= uvlinesize + 1;
  2323. src_cr -= uvlinesize + 1;
  2324. #define XCHG(a,b,t,xchg)\
  2325. t= a;\
  2326. if(xchg)\
  2327. a= b;\
  2328. b= t;
  2329. if(deblock_left){
  2330. for(i = !deblock_top; i<17; i++){
  2331. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  2332. }
  2333. }
  2334. if(deblock_top){
  2335. XCHG(*(uint64_t*)(h->top_border[s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  2336. XCHG(*(uint64_t*)(h->top_border[s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  2337. }
  2338. if(!(s->flags&CODEC_FLAG_GRAY)){
  2339. if(deblock_left){
  2340. for(i = !deblock_top; i<9; i++){
  2341. XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
  2342. XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
  2343. }
  2344. }
  2345. if(deblock_top){
  2346. XCHG(*(uint64_t*)(h->top_border[s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  2347. XCHG(*(uint64_t*)(h->top_border[s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  2348. }
  2349. }
  2350. }
  2351. static void hl_decode_mb(H264Context *h){
  2352. MpegEncContext * const s = &h->s;
  2353. const int mb_x= s->mb_x;
  2354. const int mb_y= s->mb_y;
  2355. const int mb_xy= mb_x + mb_y*s->mb_stride;
  2356. const int mb_type= s->current_picture.mb_type[mb_xy];
  2357. uint8_t *dest_y, *dest_cb, *dest_cr;
  2358. int linesize, uvlinesize /*dct_offset*/;
  2359. int i;
  2360. if(!s->decode)
  2361. return;
  2362. if(s->mb_skiped){
  2363. }
  2364. dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  2365. dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  2366. dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  2367. if (h->mb_field_decoding_flag) {
  2368. linesize = s->linesize * 2;
  2369. uvlinesize = s->uvlinesize * 2;
  2370. if(mb_y&1){ //FIXME move out of this func?
  2371. dest_y -= s->linesize*15;
  2372. dest_cb-= s->linesize*7;
  2373. dest_cr-= s->linesize*7;
  2374. }
  2375. } else {
  2376. linesize = s->linesize;
  2377. uvlinesize = s->uvlinesize;
  2378. // dct_offset = s->linesize * 16;
  2379. }
  2380. if (IS_INTRA_PCM(mb_type)) {
  2381. unsigned int x, y;
  2382. // The pixels are stored in h->mb array in the same order as levels,
  2383. // copy them in output in the correct order.
  2384. for(i=0; i<16; i++) {
  2385. for (y=0; y<4; y++) {
  2386. for (x=0; x<4; x++) {
  2387. *(dest_y + h->block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x];
  2388. }
  2389. }
  2390. }
  2391. for(i=16; i<16+4; i++) {
  2392. for (y=0; y<4; y++) {
  2393. for (x=0; x<4; x++) {
  2394. *(dest_cb + h->block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  2395. }
  2396. }
  2397. }
  2398. for(i=20; i<20+4; i++) {
  2399. for (y=0; y<4; y++) {
  2400. for (x=0; x<4; x++) {
  2401. *(dest_cr + h->block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  2402. }
  2403. }
  2404. }
  2405. goto deblock;
  2406. }
  2407. if(IS_INTRA(mb_type)){
  2408. if(h->deblocking_filter)
  2409. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1);
  2410. if(!(s->flags&CODEC_FLAG_GRAY)){
  2411. h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  2412. h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  2413. }
  2414. if(IS_INTRA4x4(mb_type)){
  2415. if(!s->encoding){
  2416. for(i=0; i<16; i++){
  2417. uint8_t * const ptr= dest_y + h->block_offset[i];
  2418. uint8_t *topright;
  2419. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  2420. int tr;
  2421. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  2422. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  2423. assert(mb_y || linesize <= h->block_offset[i]);
  2424. if(!topright_avail){
  2425. tr= ptr[3 - linesize]*0x01010101;
  2426. topright= (uint8_t*) &tr;
  2427. }else if(i==5 && h->deblocking_filter){
  2428. tr= *(uint32_t*)h->top_border[mb_x+1];
  2429. topright= (uint8_t*) &tr;
  2430. }else
  2431. topright= ptr + 4 - linesize;
  2432. }else
  2433. topright= NULL;
  2434. h->pred4x4[ dir ](ptr, topright, linesize);
  2435. if(h->non_zero_count_cache[ scan8[i] ]){
  2436. if(s->codec_id == CODEC_ID_H264)
  2437. s->dsp.h264_idct_add(ptr, h->mb + i*16, linesize);
  2438. else
  2439. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  2440. }
  2441. }
  2442. }
  2443. }else{
  2444. h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  2445. if(s->codec_id == CODEC_ID_H264)
  2446. h264_luma_dc_dequant_idct_c(h->mb, s->qscale);
  2447. else
  2448. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  2449. }
  2450. if(h->deblocking_filter)
  2451. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
  2452. }else if(s->codec_id == CODEC_ID_H264){
  2453. hl_motion(h, dest_y, dest_cb, dest_cr,
  2454. s->dsp.put_h264_qpel_pixels_tab, s->dsp.put_h264_chroma_pixels_tab,
  2455. s->dsp.avg_h264_qpel_pixels_tab, s->dsp.avg_h264_chroma_pixels_tab,
  2456. s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
  2457. }
  2458. if(!IS_INTRA4x4(mb_type)){
  2459. if(s->codec_id == CODEC_ID_H264){
  2460. for(i=0; i<16; i++){
  2461. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  2462. uint8_t * const ptr= dest_y + h->block_offset[i];
  2463. s->dsp.h264_idct_add(ptr, h->mb + i*16, linesize);
  2464. }
  2465. }
  2466. }else{
  2467. for(i=0; i<16; i++){
  2468. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  2469. uint8_t * const ptr= dest_y + h->block_offset[i];
  2470. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  2471. }
  2472. }
  2473. }
  2474. }
  2475. if(!(s->flags&CODEC_FLAG_GRAY)){
  2476. chroma_dc_dequant_idct_c(h->mb + 16*16, h->chroma_qp);
  2477. chroma_dc_dequant_idct_c(h->mb + 16*16+4*16, h->chroma_qp);
  2478. if(s->codec_id == CODEC_ID_H264){
  2479. for(i=16; i<16+4; i++){
  2480. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  2481. uint8_t * const ptr= dest_cb + h->block_offset[i];
  2482. s->dsp.h264_idct_add(ptr, h->mb + i*16, uvlinesize);
  2483. }
  2484. }
  2485. for(i=20; i<20+4; i++){
  2486. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  2487. uint8_t * const ptr= dest_cr + h->block_offset[i];
  2488. s->dsp.h264_idct_add(ptr, h->mb + i*16, uvlinesize);
  2489. }
  2490. }
  2491. }else{
  2492. for(i=16; i<16+4; i++){
  2493. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  2494. uint8_t * const ptr= dest_cb + h->block_offset[i];
  2495. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  2496. }
  2497. }
  2498. for(i=20; i<20+4; i++){
  2499. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  2500. uint8_t * const ptr= dest_cr + h->block_offset[i];
  2501. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  2502. }
  2503. }
  2504. }
  2505. }
  2506. deblock:
  2507. if(h->deblocking_filter) {
  2508. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  2509. fill_caches(h, mb_type, 1); //FIXME dont fill stuff which isnt used by filter_mb
  2510. filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr);
  2511. }
  2512. }
  2513. /**
  2514. * fills the default_ref_list.
  2515. */
  2516. static int fill_default_ref_list(H264Context *h){
  2517. MpegEncContext * const s = &h->s;
  2518. int i;
  2519. int smallest_poc_greater_than_current = -1;
  2520. Picture sorted_short_ref[16];
  2521. if(h->slice_type==B_TYPE){
  2522. int out_i;
  2523. int limit= -1;
  2524. /* sort frame according to poc in B slice */
  2525. for(out_i=0; out_i<h->short_ref_count; out_i++){
  2526. int best_i=-1;
  2527. int best_poc=INT_MAX;
  2528. for(i=0; i<h->short_ref_count; i++){
  2529. const int poc= h->short_ref[i]->poc;
  2530. if(poc > limit && poc < best_poc){
  2531. best_poc= poc;
  2532. best_i= i;
  2533. }
  2534. }
  2535. assert(best_i != -1);
  2536. limit= best_poc;
  2537. sorted_short_ref[out_i]= *h->short_ref[best_i];
  2538. tprintf("sorted poc: %d->%d poc:%d fn:%d\n", best_i, out_i, sorted_short_ref[out_i].poc, sorted_short_ref[out_i].frame_num);
  2539. if (-1 == smallest_poc_greater_than_current) {
  2540. if (h->short_ref[best_i]->poc >= s->current_picture_ptr->poc) {
  2541. smallest_poc_greater_than_current = out_i;
  2542. }
  2543. }
  2544. }
  2545. }
  2546. if(s->picture_structure == PICT_FRAME){
  2547. if(h->slice_type==B_TYPE){
  2548. int list;
  2549. tprintf("current poc: %d, smallest_poc_greater_than_current: %d\n", s->current_picture_ptr->poc, smallest_poc_greater_than_current);
  2550. // find the largest poc
  2551. for(list=0; list<2; list++){
  2552. int index = 0;
  2553. int j= -99;
  2554. int step= list ? -1 : 1;
  2555. for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++, j+=step) {
  2556. while(j<0 || j>= h->short_ref_count){
  2557. step = -step;
  2558. j= smallest_poc_greater_than_current + (step>>1);
  2559. }
  2560. if(sorted_short_ref[j].reference != 3) continue;
  2561. h->default_ref_list[list][index ]= sorted_short_ref[j];
  2562. h->default_ref_list[list][index++].pic_id= sorted_short_ref[j].frame_num;
  2563. }
  2564. for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){
  2565. if(h->long_ref[i] == NULL) continue;
  2566. if(h->long_ref[i]->reference != 3) continue;
  2567. h->default_ref_list[ list ][index ]= *h->long_ref[i];
  2568. h->default_ref_list[ list ][index++].pic_id= i;;
  2569. }
  2570. if(list && (smallest_poc_greater_than_current<=0 || smallest_poc_greater_than_current>=h->short_ref_count) && (1 < index)){
  2571. // swap the two first elements of L1 when
  2572. // L0 and L1 are identical
  2573. Picture temp= h->default_ref_list[1][0];
  2574. h->default_ref_list[1][0] = h->default_ref_list[1][1];
  2575. h->default_ref_list[1][0] = temp;
  2576. }
  2577. if(index < h->ref_count[ list ])
  2578. memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
  2579. }
  2580. }else{
  2581. int index=0;
  2582. for(i=0; i<h->short_ref_count && index < h->ref_count[0]; i++){
  2583. if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
  2584. h->default_ref_list[0][index ]= *h->short_ref[i];
  2585. h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
  2586. }
  2587. for(i = 0; i < 16 && index < h->ref_count[0]; i++){
  2588. if(h->long_ref[i] == NULL) continue;
  2589. if(h->long_ref[i]->reference != 3) continue;
  2590. h->default_ref_list[0][index ]= *h->long_ref[i];
  2591. h->default_ref_list[0][index++].pic_id= i;;
  2592. }
  2593. if(index < h->ref_count[0])
  2594. memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
  2595. }
  2596. }else{ //FIELD
  2597. if(h->slice_type==B_TYPE){
  2598. }else{
  2599. //FIXME second field balh
  2600. }
  2601. }
  2602. #ifdef TRACE
  2603. for (i=0; i<h->ref_count[0]; i++) {
  2604. tprintf("List0: %s fn:%d 0x%p\n", (h->default_ref_list[0][i].long_ref ? "LT" : "ST"), h->default_ref_list[0][i].pic_id, h->default_ref_list[0][i].data[0]);
  2605. }
  2606. if(h->slice_type==B_TYPE){
  2607. for (i=0; i<h->ref_count[1]; i++) {
  2608. tprintf("List1: %s fn:%d 0x%p\n", (h->default_ref_list[1][i].long_ref ? "LT" : "ST"), h->default_ref_list[1][i].pic_id, h->default_ref_list[0][i].data[0]);
  2609. }
  2610. }
  2611. #endif
  2612. return 0;
  2613. }
  2614. static void print_short_term(H264Context *h);
  2615. static void print_long_term(H264Context *h);
  2616. static int decode_ref_pic_list_reordering(H264Context *h){
  2617. MpegEncContext * const s = &h->s;
  2618. int list;
  2619. print_short_term(h);
  2620. print_long_term(h);
  2621. if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move beofre func
  2622. for(list=0; list<2; list++){
  2623. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  2624. if(get_bits1(&s->gb)){
  2625. int pred= h->curr_pic_num;
  2626. int index;
  2627. for(index=0; ; index++){
  2628. int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
  2629. int pic_id;
  2630. int i;
  2631. if(reordering_of_pic_nums_idc==3)
  2632. break;
  2633. if(index >= h->ref_count[list]){
  2634. av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
  2635. return -1;
  2636. }
  2637. if(reordering_of_pic_nums_idc<3){
  2638. if(reordering_of_pic_nums_idc<2){
  2639. const int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  2640. if(abs_diff_pic_num >= h->max_pic_num){
  2641. av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  2642. return -1;
  2643. }
  2644. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  2645. else pred+= abs_diff_pic_num;
  2646. pred &= h->max_pic_num - 1;
  2647. for(i= h->ref_count[list]-1; i>=0; i--){
  2648. if(h->ref_list[list][i].data[0] != NULL && h->ref_list[list][i].pic_id == pred && h->ref_list[list][i].long_ref==0) // ignore non existing pictures by testing data[0] pointer
  2649. break;
  2650. }
  2651. }else{
  2652. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  2653. for(i= h->ref_count[list]-1; i>=0; i--){
  2654. if(h->ref_list[list][i].pic_id == pic_id && h->ref_list[list][i].long_ref==1) // no need to ignore non existing pictures as non existing pictures have long_ref==0
  2655. break;
  2656. }
  2657. }
  2658. if (i < 0) {
  2659. av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  2660. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  2661. } else if (i != index) /* this test is not necessary, it is only an optimisation to skip double copy of Picture structure in this case */ {
  2662. Picture tmp= h->ref_list[list][i];
  2663. if (i < index) {
  2664. i = h->ref_count[list];
  2665. }
  2666. for(; i > index; i--){
  2667. h->ref_list[list][i]= h->ref_list[list][i-1];
  2668. }
  2669. h->ref_list[list][index]= tmp;
  2670. }
  2671. }else{
  2672. av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  2673. return -1;
  2674. }
  2675. }
  2676. }
  2677. if(h->slice_type!=B_TYPE) break;
  2678. }
  2679. if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)
  2680. direct_dist_scale_factor(h);
  2681. return 0;
  2682. }
  2683. static int pred_weight_table(H264Context *h){
  2684. MpegEncContext * const s = &h->s;
  2685. int list, i;
  2686. int luma_def, chroma_def;
  2687. h->use_weight= 0;
  2688. h->use_weight_chroma= 0;
  2689. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  2690. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  2691. luma_def = 1<<h->luma_log2_weight_denom;
  2692. chroma_def = 1<<h->chroma_log2_weight_denom;
  2693. for(list=0; list<2; list++){
  2694. for(i=0; i<h->ref_count[list]; i++){
  2695. int luma_weight_flag, chroma_weight_flag;
  2696. luma_weight_flag= get_bits1(&s->gb);
  2697. if(luma_weight_flag){
  2698. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  2699. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  2700. if( h->luma_weight[list][i] != luma_def
  2701. || h->luma_offset[list][i] != 0)
  2702. h->use_weight= 1;
  2703. }else{
  2704. h->luma_weight[list][i]= luma_def;
  2705. h->luma_offset[list][i]= 0;
  2706. }
  2707. chroma_weight_flag= get_bits1(&s->gb);
  2708. if(chroma_weight_flag){
  2709. int j;
  2710. for(j=0; j<2; j++){
  2711. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  2712. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  2713. if( h->chroma_weight[list][i][j] != chroma_def
  2714. || h->chroma_offset[list][i][j] != 0)
  2715. h->use_weight_chroma= 1;
  2716. }
  2717. }else{
  2718. int j;
  2719. for(j=0; j<2; j++){
  2720. h->chroma_weight[list][i][j]= chroma_def;
  2721. h->chroma_offset[list][i][j]= 0;
  2722. }
  2723. }
  2724. }
  2725. if(h->slice_type != B_TYPE) break;
  2726. }
  2727. h->use_weight= h->use_weight || h->use_weight_chroma;
  2728. return 0;
  2729. }
  2730. static void implicit_weight_table(H264Context *h){
  2731. MpegEncContext * const s = &h->s;
  2732. int ref0, ref1;
  2733. int cur_poc = s->current_picture_ptr->poc;
  2734. if( h->ref_count[0] == 1 && h->ref_count[1] == 1
  2735. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  2736. h->use_weight= 0;
  2737. h->use_weight_chroma= 0;
  2738. return;
  2739. }
  2740. h->use_weight= 2;
  2741. h->use_weight_chroma= 2;
  2742. h->luma_log2_weight_denom= 5;
  2743. h->chroma_log2_weight_denom= 5;
  2744. /* FIXME: MBAFF */
  2745. for(ref0=0; ref0 < h->ref_count[0]; ref0++){
  2746. int poc0 = h->ref_list[0][ref0].poc;
  2747. for(ref1=0; ref1 < h->ref_count[1]; ref1++){
  2748. int poc1 = h->ref_list[0][ref1].poc;
  2749. int td = clip(poc1 - poc0, -128, 127);
  2750. if(td){
  2751. int tb = clip(cur_poc - poc0, -128, 127);
  2752. int tx = (16384 + (ABS(td) >> 1)) / td;
  2753. int dist_scale_factor = clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
  2754. if(dist_scale_factor < -64 || dist_scale_factor > 128)
  2755. h->implicit_weight[ref0][ref1] = 32;
  2756. else
  2757. h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
  2758. }else
  2759. h->implicit_weight[ref0][ref1] = 32;
  2760. }
  2761. }
  2762. }
  2763. /**
  2764. * instantaneous decoder refresh.
  2765. */
  2766. static void idr(H264Context *h){
  2767. int i,j;
  2768. #define CHECK_DELAY(pic) \
  2769. for(j = 0; h->delayed_pic[j]; j++) \
  2770. if(pic == h->delayed_pic[j]){ \
  2771. pic->reference=1; \
  2772. break; \
  2773. }
  2774. for(i=0; i<16; i++){
  2775. if (h->long_ref[i] != NULL) {
  2776. h->long_ref[i]->reference=0;
  2777. CHECK_DELAY(h->long_ref[i]);
  2778. h->long_ref[i]= NULL;
  2779. }
  2780. }
  2781. h->long_ref_count=0;
  2782. for(i=0; i<h->short_ref_count; i++){
  2783. h->short_ref[i]->reference=0;
  2784. CHECK_DELAY(h->short_ref[i]);
  2785. h->short_ref[i]= NULL;
  2786. }
  2787. h->short_ref_count=0;
  2788. }
  2789. #undef CHECK_DELAY
  2790. /**
  2791. *
  2792. * @return the removed picture or NULL if an error occures
  2793. */
  2794. static Picture * remove_short(H264Context *h, int frame_num){
  2795. MpegEncContext * const s = &h->s;
  2796. int i;
  2797. if(s->avctx->debug&FF_DEBUG_MMCO)
  2798. av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  2799. for(i=0; i<h->short_ref_count; i++){
  2800. Picture *pic= h->short_ref[i];
  2801. if(s->avctx->debug&FF_DEBUG_MMCO)
  2802. av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  2803. if(pic->frame_num == frame_num){
  2804. h->short_ref[i]= NULL;
  2805. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
  2806. h->short_ref_count--;
  2807. return pic;
  2808. }
  2809. }
  2810. return NULL;
  2811. }
  2812. /**
  2813. *
  2814. * @return the removed picture or NULL if an error occures
  2815. */
  2816. static Picture * remove_long(H264Context *h, int i){
  2817. Picture *pic;
  2818. pic= h->long_ref[i];
  2819. h->long_ref[i]= NULL;
  2820. if(pic) h->long_ref_count--;
  2821. return pic;
  2822. }
  2823. /**
  2824. * print short term list
  2825. */
  2826. static void print_short_term(H264Context *h) {
  2827. uint32_t i;
  2828. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  2829. av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
  2830. for(i=0; i<h->short_ref_count; i++){
  2831. Picture *pic= h->short_ref[i];
  2832. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  2833. }
  2834. }
  2835. }
  2836. /**
  2837. * print long term list
  2838. */
  2839. static void print_long_term(H264Context *h) {
  2840. uint32_t i;
  2841. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  2842. av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
  2843. for(i = 0; i < 16; i++){
  2844. Picture *pic= h->long_ref[i];
  2845. if (pic) {
  2846. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  2847. }
  2848. }
  2849. }
  2850. }
  2851. /**
  2852. * Executes the reference picture marking (memory management control operations).
  2853. */
  2854. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  2855. MpegEncContext * const s = &h->s;
  2856. int i, j;
  2857. int current_is_long=0;
  2858. Picture *pic;
  2859. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  2860. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  2861. for(i=0; i<mmco_count; i++){
  2862. if(s->avctx->debug&FF_DEBUG_MMCO)
  2863. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco:%d %d %d\n", h->mmco[i].opcode, h->mmco[i].short_frame_num, h->mmco[i].long_index);
  2864. switch(mmco[i].opcode){
  2865. case MMCO_SHORT2UNUSED:
  2866. pic= remove_short(h, mmco[i].short_frame_num);
  2867. if(pic==NULL) return -1;
  2868. pic->reference= 0;
  2869. break;
  2870. case MMCO_SHORT2LONG:
  2871. pic= remove_long(h, mmco[i].long_index);
  2872. if(pic) pic->reference=0;
  2873. h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
  2874. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  2875. h->long_ref_count++;
  2876. break;
  2877. case MMCO_LONG2UNUSED:
  2878. pic= remove_long(h, mmco[i].long_index);
  2879. if(pic==NULL) return -1;
  2880. pic->reference= 0;
  2881. break;
  2882. case MMCO_LONG:
  2883. pic= remove_long(h, mmco[i].long_index);
  2884. if(pic) pic->reference=0;
  2885. h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
  2886. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  2887. h->long_ref_count++;
  2888. current_is_long=1;
  2889. break;
  2890. case MMCO_SET_MAX_LONG:
  2891. assert(mmco[i].long_index <= 16);
  2892. // just remove the long term which index is greater than new max
  2893. for(j = mmco[i].long_index; j<16; j++){
  2894. pic = remove_long(h, j);
  2895. if (pic) pic->reference=0;
  2896. }
  2897. break;
  2898. case MMCO_RESET:
  2899. while(h->short_ref_count){
  2900. pic= remove_short(h, h->short_ref[0]->frame_num);
  2901. pic->reference=0;
  2902. }
  2903. for(j = 0; j < 16; j++) {
  2904. pic= remove_long(h, j);
  2905. if(pic) pic->reference=0;
  2906. }
  2907. break;
  2908. default: assert(0);
  2909. }
  2910. }
  2911. if(!current_is_long){
  2912. pic= remove_short(h, s->current_picture_ptr->frame_num);
  2913. if(pic){
  2914. pic->reference=0;
  2915. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  2916. }
  2917. if(h->short_ref_count)
  2918. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  2919. h->short_ref[0]= s->current_picture_ptr;
  2920. h->short_ref[0]->long_ref=0;
  2921. h->short_ref_count++;
  2922. }
  2923. print_short_term(h);
  2924. print_long_term(h);
  2925. return 0;
  2926. }
  2927. static int decode_ref_pic_marking(H264Context *h){
  2928. MpegEncContext * const s = &h->s;
  2929. int i;
  2930. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  2931. s->broken_link= get_bits1(&s->gb) -1;
  2932. h->mmco[0].long_index= get_bits1(&s->gb) - 1; // current_long_term_idx
  2933. if(h->mmco[0].long_index == -1)
  2934. h->mmco_index= 0;
  2935. else{
  2936. h->mmco[0].opcode= MMCO_LONG;
  2937. h->mmco_index= 1;
  2938. }
  2939. }else{
  2940. if(get_bits1(&s->gb)){ // adaptive_ref_pic_marking_mode_flag
  2941. for(i= 0; i<MAX_MMCO_COUNT; i++) {
  2942. MMCOOpcode opcode= get_ue_golomb(&s->gb);;
  2943. h->mmco[i].opcode= opcode;
  2944. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  2945. 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
  2946. /* if(h->mmco[i].short_frame_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_frame_num ] == NULL){
  2947. fprintf(stderr, "illegal short ref in memory management control operation %d\n", mmco);
  2948. return -1;
  2949. }*/
  2950. }
  2951. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  2952. h->mmco[i].long_index= get_ue_golomb(&s->gb);
  2953. 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){
  2954. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  2955. return -1;
  2956. }
  2957. }
  2958. if(opcode > MMCO_LONG){
  2959. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  2960. return -1;
  2961. }
  2962. if(opcode == MMCO_END)
  2963. break;
  2964. }
  2965. h->mmco_index= i;
  2966. }else{
  2967. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  2968. if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){ //FIXME fields
  2969. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  2970. h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  2971. h->mmco_index= 1;
  2972. }else
  2973. h->mmco_index= 0;
  2974. }
  2975. }
  2976. return 0;
  2977. }
  2978. static int init_poc(H264Context *h){
  2979. MpegEncContext * const s = &h->s;
  2980. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  2981. int field_poc[2];
  2982. if(h->nal_unit_type == NAL_IDR_SLICE){
  2983. h->frame_num_offset= 0;
  2984. }else{
  2985. if(h->frame_num < h->prev_frame_num)
  2986. h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
  2987. else
  2988. h->frame_num_offset= h->prev_frame_num_offset;
  2989. }
  2990. if(h->sps.poc_type==0){
  2991. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  2992. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  2993. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  2994. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  2995. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  2996. else
  2997. h->poc_msb = h->prev_poc_msb;
  2998. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  2999. field_poc[0] =
  3000. field_poc[1] = h->poc_msb + h->poc_lsb;
  3001. if(s->picture_structure == PICT_FRAME)
  3002. field_poc[1] += h->delta_poc_bottom;
  3003. }else if(h->sps.poc_type==1){
  3004. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  3005. int i;
  3006. if(h->sps.poc_cycle_length != 0)
  3007. abs_frame_num = h->frame_num_offset + h->frame_num;
  3008. else
  3009. abs_frame_num = 0;
  3010. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  3011. abs_frame_num--;
  3012. expected_delta_per_poc_cycle = 0;
  3013. for(i=0; i < h->sps.poc_cycle_length; i++)
  3014. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  3015. if(abs_frame_num > 0){
  3016. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  3017. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  3018. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  3019. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  3020. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  3021. } else
  3022. expectedpoc = 0;
  3023. if(h->nal_ref_idc == 0)
  3024. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  3025. field_poc[0] = expectedpoc + h->delta_poc[0];
  3026. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  3027. if(s->picture_structure == PICT_FRAME)
  3028. field_poc[1] += h->delta_poc[1];
  3029. }else{
  3030. int poc;
  3031. if(h->nal_unit_type == NAL_IDR_SLICE){
  3032. poc= 0;
  3033. }else{
  3034. if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
  3035. else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
  3036. }
  3037. field_poc[0]= poc;
  3038. field_poc[1]= poc;
  3039. }
  3040. if(s->picture_structure != PICT_BOTTOM_FIELD)
  3041. s->current_picture_ptr->field_poc[0]= field_poc[0];
  3042. if(s->picture_structure != PICT_TOP_FIELD)
  3043. s->current_picture_ptr->field_poc[1]= field_poc[1];
  3044. if(s->picture_structure == PICT_FRAME) // FIXME field pix?
  3045. s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]);
  3046. return 0;
  3047. }
  3048. /**
  3049. * decodes a slice header.
  3050. * this will allso call MPV_common_init() and frame_start() as needed
  3051. */
  3052. static int decode_slice_header(H264Context *h){
  3053. MpegEncContext * const s = &h->s;
  3054. int first_mb_in_slice, pps_id;
  3055. int num_ref_idx_active_override_flag;
  3056. static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
  3057. int slice_type;
  3058. int default_ref_list_done = 0;
  3059. s->current_picture.reference= h->nal_ref_idc != 0;
  3060. first_mb_in_slice= get_ue_golomb(&s->gb);
  3061. slice_type= get_ue_golomb(&s->gb);
  3062. if(slice_type > 9){
  3063. av_log(h->s.avctx, AV_LOG_ERROR, "slice type too large (%d) at %d %d\n", h->slice_type, s->mb_x, s->mb_y);
  3064. return -1;
  3065. }
  3066. if(slice_type > 4){
  3067. slice_type -= 5;
  3068. h->slice_type_fixed=1;
  3069. }else
  3070. h->slice_type_fixed=0;
  3071. slice_type= slice_type_map[ slice_type ];
  3072. if (slice_type == I_TYPE
  3073. || (h->slice_num != 0 && slice_type == h->slice_type) ) {
  3074. default_ref_list_done = 1;
  3075. }
  3076. h->slice_type= slice_type;
  3077. s->pict_type= h->slice_type; // to make a few old func happy, its wrong though
  3078. pps_id= get_ue_golomb(&s->gb);
  3079. if(pps_id>255){
  3080. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  3081. return -1;
  3082. }
  3083. h->pps= h->pps_buffer[pps_id];
  3084. if(h->pps.slice_group_count == 0){
  3085. av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referenced\n");
  3086. return -1;
  3087. }
  3088. h->sps= h->sps_buffer[ h->pps.sps_id ];
  3089. if(h->sps.log2_max_frame_num == 0){
  3090. av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referenced\n");
  3091. return -1;
  3092. }
  3093. s->mb_width= h->sps.mb_width;
  3094. s->mb_height= h->sps.mb_height;
  3095. h->b_stride= s->mb_width*4 + 1;
  3096. h->b8_stride= s->mb_width*2 + 1;
  3097. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  3098. s->resync_mb_y = s->mb_y = first_mb_in_slice / s->mb_width; //FIXME AFFW
  3099. s->width = 16*s->mb_width - 2*(h->sps.crop_left + h->sps.crop_right );
  3100. if(h->sps.frame_mbs_only_flag)
  3101. s->height= 16*s->mb_height - 2*(h->sps.crop_top + h->sps.crop_bottom);
  3102. else
  3103. s->height= 16*s->mb_height - 4*(h->sps.crop_top + h->sps.crop_bottom); //FIXME recheck
  3104. if (s->context_initialized
  3105. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  3106. free_tables(h);
  3107. MPV_common_end(s);
  3108. }
  3109. if (!s->context_initialized) {
  3110. if (MPV_common_init(s) < 0)
  3111. return -1;
  3112. alloc_tables(h);
  3113. s->avctx->width = s->width;
  3114. s->avctx->height = s->height;
  3115. s->avctx->sample_aspect_ratio= h->sps.sar;
  3116. if(!s->avctx->sample_aspect_ratio.den)
  3117. s->avctx->sample_aspect_ratio.den = 1;
  3118. if(h->sps.timing_info_present_flag && h->sps.fixed_frame_rate_flag){
  3119. s->avctx->frame_rate = h->sps.time_scale;
  3120. s->avctx->frame_rate_base = h->sps.num_units_in_tick;
  3121. }
  3122. }
  3123. if(h->slice_num == 0){
  3124. frame_start(h);
  3125. }
  3126. s->current_picture_ptr->frame_num= //FIXME frame_num cleanup
  3127. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  3128. if(h->sps.frame_mbs_only_flag){
  3129. s->picture_structure= PICT_FRAME;
  3130. }else{
  3131. if(get_bits1(&s->gb)) //field_pic_flag
  3132. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  3133. else
  3134. s->picture_structure= PICT_FRAME;
  3135. }
  3136. if(s->picture_structure==PICT_FRAME){
  3137. h->curr_pic_num= h->frame_num;
  3138. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  3139. }else{
  3140. h->curr_pic_num= 2*h->frame_num;
  3141. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  3142. }
  3143. if(h->nal_unit_type == NAL_IDR_SLICE){
  3144. get_ue_golomb(&s->gb); /* idr_pic_id */
  3145. }
  3146. if(h->sps.poc_type==0){
  3147. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  3148. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  3149. h->delta_poc_bottom= get_se_golomb(&s->gb);
  3150. }
  3151. }
  3152. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  3153. h->delta_poc[0]= get_se_golomb(&s->gb);
  3154. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  3155. h->delta_poc[1]= get_se_golomb(&s->gb);
  3156. }
  3157. init_poc(h);
  3158. if(h->pps.redundant_pic_cnt_present){
  3159. h->redundant_pic_count= get_ue_golomb(&s->gb);
  3160. }
  3161. //set defaults, might be overriden a few line later
  3162. h->ref_count[0]= h->pps.ref_count[0];
  3163. h->ref_count[1]= h->pps.ref_count[1];
  3164. if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){
  3165. if(h->slice_type == B_TYPE){
  3166. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  3167. }
  3168. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  3169. if(num_ref_idx_active_override_flag){
  3170. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  3171. if(h->slice_type==B_TYPE)
  3172. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  3173. if(h->ref_count[0] > 32 || h->ref_count[1] > 32){
  3174. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  3175. return -1;
  3176. }
  3177. }
  3178. }
  3179. if(!default_ref_list_done){
  3180. fill_default_ref_list(h);
  3181. }
  3182. decode_ref_pic_list_reordering(h);
  3183. if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
  3184. || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
  3185. pred_weight_table(h);
  3186. else if(h->pps.weighted_bipred_idc==2 && h->slice_type==B_TYPE)
  3187. implicit_weight_table(h);
  3188. else
  3189. h->use_weight = 0;
  3190. if(s->current_picture.reference)
  3191. decode_ref_pic_marking(h);
  3192. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac )
  3193. h->cabac_init_idc = get_ue_golomb(&s->gb);
  3194. h->last_qscale_diff = 0;
  3195. s->qscale = h->pps.init_qp + get_se_golomb(&s->gb);
  3196. if(s->qscale<0 || s->qscale>51){
  3197. av_log(s->avctx, AV_LOG_ERROR, "QP %d out of range\n", s->qscale);
  3198. return -1;
  3199. }
  3200. h->chroma_qp = get_chroma_qp(h, s->qscale);
  3201. //FIXME qscale / qp ... stuff
  3202. if(h->slice_type == SP_TYPE){
  3203. get_bits1(&s->gb); /* sp_for_switch_flag */
  3204. }
  3205. if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
  3206. get_se_golomb(&s->gb); /* slice_qs_delta */
  3207. }
  3208. h->deblocking_filter = 1;
  3209. h->slice_alpha_c0_offset = 0;
  3210. h->slice_beta_offset = 0;
  3211. if( h->pps.deblocking_filter_parameters_present ) {
  3212. h->deblocking_filter= get_ue_golomb(&s->gb);
  3213. if(h->deblocking_filter < 2)
  3214. h->deblocking_filter^= 1; // 1<->0
  3215. if( h->deblocking_filter ) {
  3216. h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
  3217. h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
  3218. }
  3219. }
  3220. #if 0 //FMO
  3221. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  3222. slice_group_change_cycle= get_bits(&s->gb, ?);
  3223. #endif
  3224. h->slice_num++;
  3225. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  3226. av_log(h->s.avctx, AV_LOG_DEBUG, "slice:%d mb:%d %c pps:%d frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d weight:%d%s\n",
  3227. h->slice_num, first_mb_in_slice,
  3228. av_get_pict_type_char(h->slice_type),
  3229. pps_id, h->frame_num,
  3230. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  3231. h->ref_count[0], h->ref_count[1],
  3232. s->qscale,
  3233. h->deblocking_filter,
  3234. h->use_weight,
  3235. h->use_weight==1 && h->use_weight_chroma ? "c" : ""
  3236. );
  3237. }
  3238. return 0;
  3239. }
  3240. /**
  3241. *
  3242. */
  3243. static inline int get_level_prefix(GetBitContext *gb){
  3244. unsigned int buf;
  3245. int log;
  3246. OPEN_READER(re, gb);
  3247. UPDATE_CACHE(re, gb);
  3248. buf=GET_CACHE(re, gb);
  3249. log= 32 - av_log2(buf);
  3250. #ifdef TRACE
  3251. print_bin(buf>>(32-log), log);
  3252. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d lpr @%5d in %s get_level_prefix\n", buf>>(32-log), log, log-1, get_bits_count(gb), __FILE__);
  3253. #endif
  3254. LAST_SKIP_BITS(re, gb, log);
  3255. CLOSE_READER(re, gb);
  3256. return log-1;
  3257. }
  3258. /**
  3259. * decodes a residual block.
  3260. * @param n block index
  3261. * @param scantable scantable
  3262. * @param max_coeff number of coefficients in the block
  3263. * @return <0 if an error occured
  3264. */
  3265. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, int qp, int max_coeff){
  3266. MpegEncContext * const s = &h->s;
  3267. const uint16_t *qmul= dequant_coeff[qp];
  3268. 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};
  3269. int level[16], run[16];
  3270. int suffix_length, zeros_left, coeff_num, coeff_token, total_coeff, i, trailing_ones;
  3271. //FIXME put trailing_onex into the context
  3272. if(n == CHROMA_DC_BLOCK_INDEX){
  3273. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  3274. total_coeff= coeff_token>>2;
  3275. }else{
  3276. if(n == LUMA_DC_BLOCK_INDEX){
  3277. total_coeff= pred_non_zero_count(h, 0);
  3278. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  3279. total_coeff= coeff_token>>2;
  3280. }else{
  3281. total_coeff= pred_non_zero_count(h, n);
  3282. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  3283. total_coeff= coeff_token>>2;
  3284. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  3285. }
  3286. }
  3287. //FIXME set last_non_zero?
  3288. if(total_coeff==0)
  3289. return 0;
  3290. trailing_ones= coeff_token&3;
  3291. tprintf("trailing:%d, total:%d\n", trailing_ones, total_coeff);
  3292. assert(total_coeff<=16);
  3293. for(i=0; i<trailing_ones; i++){
  3294. level[i]= 1 - 2*get_bits1(gb);
  3295. }
  3296. suffix_length= total_coeff > 10 && trailing_ones < 3;
  3297. for(; i<total_coeff; i++){
  3298. const int prefix= get_level_prefix(gb);
  3299. int level_code, mask;
  3300. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  3301. if(suffix_length)
  3302. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  3303. else
  3304. level_code= (prefix<<suffix_length); //part
  3305. }else if(prefix==14){
  3306. if(suffix_length)
  3307. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  3308. else
  3309. level_code= prefix + get_bits(gb, 4); //part
  3310. }else if(prefix==15){
  3311. level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part
  3312. if(suffix_length==0) level_code+=15; //FIXME doesnt make (much)sense
  3313. }else{
  3314. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  3315. return -1;
  3316. }
  3317. if(i==trailing_ones && i<3) level_code+= 2; //FIXME split first iteration
  3318. mask= -(level_code&1);
  3319. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  3320. if(suffix_length==0) suffix_length=1; //FIXME split first iteration
  3321. #if 1
  3322. if(ABS(level[i]) > (3<<(suffix_length-1)) && suffix_length<6) suffix_length++;
  3323. #else
  3324. if((2+level_code)>>1) > (3<<(suffix_length-1)) && suffix_length<6) suffix_length++;
  3325. /* ? == prefix > 2 or sth */
  3326. #endif
  3327. tprintf("level: %d suffix_length:%d\n", level[i], suffix_length);
  3328. }
  3329. if(total_coeff == max_coeff)
  3330. zeros_left=0;
  3331. else{
  3332. if(n == CHROMA_DC_BLOCK_INDEX)
  3333. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  3334. else
  3335. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  3336. }
  3337. for(i=0; i<total_coeff-1; i++){
  3338. if(zeros_left <=0)
  3339. break;
  3340. else if(zeros_left < 7){
  3341. run[i]= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  3342. }else{
  3343. run[i]= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  3344. }
  3345. zeros_left -= run[i];
  3346. }
  3347. if(zeros_left<0){
  3348. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  3349. return -1;
  3350. }
  3351. for(; i<total_coeff-1; i++){
  3352. run[i]= 0;
  3353. }
  3354. run[i]= zeros_left;
  3355. coeff_num=-1;
  3356. if(n > 24){
  3357. for(i=total_coeff-1; i>=0; i--){ //FIXME merge into rundecode?
  3358. int j;
  3359. coeff_num += run[i] + 1; //FIXME add 1 earlier ?
  3360. j= scantable[ coeff_num ];
  3361. block[j]= level[i];
  3362. }
  3363. }else{
  3364. for(i=total_coeff-1; i>=0; i--){ //FIXME merge into rundecode?
  3365. int j;
  3366. coeff_num += run[i] + 1; //FIXME add 1 earlier ?
  3367. j= scantable[ coeff_num ];
  3368. block[j]= level[i] * qmul[j];
  3369. // printf("%d %d ", block[j], qmul[j]);
  3370. }
  3371. }
  3372. return 0;
  3373. }
  3374. /**
  3375. * decodes a P_SKIP or B_SKIP macroblock
  3376. */
  3377. static void decode_mb_skip(H264Context *h){
  3378. MpegEncContext * const s = &h->s;
  3379. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  3380. int mb_type;
  3381. memset(h->non_zero_count[mb_xy], 0, 16);
  3382. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  3383. if( h->slice_type == B_TYPE )
  3384. {
  3385. // just for fill_caches. pred_direct_motion will set the real mb_type
  3386. mb_type= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
  3387. //FIXME mbaff
  3388. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  3389. pred_direct_motion(h, &mb_type);
  3390. if(h->pps.cabac){
  3391. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  3392. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  3393. }
  3394. }
  3395. else
  3396. {
  3397. int mx, my;
  3398. mb_type= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
  3399. if(h->sps.mb_aff && s->mb_skip_run==0 && (s->mb_y&1)==0){
  3400. h->mb_field_decoding_flag= get_bits1(&s->gb);
  3401. }
  3402. if(h->mb_field_decoding_flag)
  3403. mb_type|= MB_TYPE_INTERLACED;
  3404. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  3405. pred_pskip_motion(h, &mx, &my);
  3406. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  3407. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  3408. if(h->pps.cabac)
  3409. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  3410. }
  3411. write_back_motion(h, mb_type);
  3412. s->current_picture.mb_type[mb_xy]= mb_type|MB_TYPE_SKIP;
  3413. s->current_picture.qscale_table[mb_xy]= s->qscale;
  3414. h->slice_table[ mb_xy ]= h->slice_num;
  3415. h->prev_mb_skiped= 1;
  3416. }
  3417. /**
  3418. * decodes a macroblock
  3419. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  3420. */
  3421. static int decode_mb_cavlc(H264Context *h){
  3422. MpegEncContext * const s = &h->s;
  3423. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  3424. int mb_type, partition_count, cbp;
  3425. s->dsp.clear_blocks(h->mb); //FIXME avoid if allready clear (move after skip handlong?
  3426. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  3427. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  3428. down the code */
  3429. if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){
  3430. if(s->mb_skip_run==-1)
  3431. s->mb_skip_run= get_ue_golomb(&s->gb);
  3432. if (s->mb_skip_run--) {
  3433. decode_mb_skip(h);
  3434. return 0;
  3435. }
  3436. }
  3437. if(h->sps.mb_aff /* && !field pic FIXME needed? */){
  3438. if((s->mb_y&1)==0)
  3439. h->mb_field_decoding_flag = get_bits1(&s->gb);
  3440. }else
  3441. h->mb_field_decoding_flag=0; //FIXME som ed note ?!
  3442. h->prev_mb_skiped= 0;
  3443. mb_type= get_ue_golomb(&s->gb);
  3444. if(h->slice_type == B_TYPE){
  3445. if(mb_type < 23){
  3446. partition_count= b_mb_type_info[mb_type].partition_count;
  3447. mb_type= b_mb_type_info[mb_type].type;
  3448. }else{
  3449. mb_type -= 23;
  3450. goto decode_intra_mb;
  3451. }
  3452. }else if(h->slice_type == P_TYPE /*|| h->slice_type == SP_TYPE */){
  3453. if(mb_type < 5){
  3454. partition_count= p_mb_type_info[mb_type].partition_count;
  3455. mb_type= p_mb_type_info[mb_type].type;
  3456. }else{
  3457. mb_type -= 5;
  3458. goto decode_intra_mb;
  3459. }
  3460. }else{
  3461. assert(h->slice_type == I_TYPE);
  3462. decode_intra_mb:
  3463. if(mb_type > 25){
  3464. av_log(h->s.avctx, AV_LOG_ERROR, "mb_type %d in %c slice to large at %d %d\n", mb_type, av_get_pict_type_char(h->slice_type), s->mb_x, s->mb_y);
  3465. return -1;
  3466. }
  3467. partition_count=0;
  3468. cbp= i_mb_type_info[mb_type].cbp;
  3469. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  3470. mb_type= i_mb_type_info[mb_type].type;
  3471. }
  3472. if(h->mb_field_decoding_flag)
  3473. mb_type |= MB_TYPE_INTERLACED;
  3474. s->current_picture.mb_type[mb_xy]= mb_type;
  3475. h->slice_table[ mb_xy ]= h->slice_num;
  3476. if(IS_INTRA_PCM(mb_type)){
  3477. unsigned int x, y;
  3478. // we assume these blocks are very rare so we dont optimize it
  3479. align_get_bits(&s->gb);
  3480. // The pixels are stored in the same order as levels in h->mb array.
  3481. for(y=0; y<16; y++){
  3482. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  3483. for(x=0; x<16; x++){
  3484. tprintf("LUMA ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  3485. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= get_bits(&s->gb, 8);
  3486. }
  3487. }
  3488. for(y=0; y<8; y++){
  3489. const int index= 256 + 4*(y&3) + 32*(y>>2);
  3490. for(x=0; x<8; x++){
  3491. tprintf("CHROMA U ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  3492. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  3493. }
  3494. }
  3495. for(y=0; y<8; y++){
  3496. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  3497. for(x=0; x<8; x++){
  3498. tprintf("CHROMA V ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  3499. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  3500. }
  3501. }
  3502. // In deblocking, the quantiser is 0
  3503. s->current_picture.qscale_table[mb_xy]= 0;
  3504. h->chroma_qp = get_chroma_qp(h, 0);
  3505. // All coeffs are presents
  3506. memset(h->non_zero_count[mb_xy], 16, 16);
  3507. return 0;
  3508. }
  3509. fill_caches(h, mb_type, 0);
  3510. //mb_pred
  3511. if(IS_INTRA(mb_type)){
  3512. // init_top_left_availability(h);
  3513. if(IS_INTRA4x4(mb_type)){
  3514. int i;
  3515. // fill_intra4x4_pred_table(h);
  3516. for(i=0; i<16; i++){
  3517. const int mode_coded= !get_bits1(&s->gb);
  3518. const int predicted_mode= pred_intra_mode(h, i);
  3519. int mode;
  3520. if(mode_coded){
  3521. const int rem_mode= get_bits(&s->gb, 3);
  3522. if(rem_mode<predicted_mode)
  3523. mode= rem_mode;
  3524. else
  3525. mode= rem_mode + 1;
  3526. }else{
  3527. mode= predicted_mode;
  3528. }
  3529. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  3530. }
  3531. write_back_intra_pred_mode(h);
  3532. if( check_intra4x4_pred_mode(h) < 0)
  3533. return -1;
  3534. }else{
  3535. h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
  3536. if(h->intra16x16_pred_mode < 0)
  3537. return -1;
  3538. }
  3539. h->chroma_pred_mode= get_ue_golomb(&s->gb);
  3540. h->chroma_pred_mode= check_intra_pred_mode(h, h->chroma_pred_mode);
  3541. if(h->chroma_pred_mode < 0)
  3542. return -1;
  3543. }else if(partition_count==4){
  3544. int i, j, sub_partition_count[4], list, ref[2][4];
  3545. if(h->slice_type == B_TYPE){
  3546. for(i=0; i<4; i++){
  3547. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  3548. if(h->sub_mb_type[i] >=13){
  3549. av_log(h->s.avctx, AV_LOG_ERROR, "B sub_mb_type %d out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
  3550. return -1;
  3551. }
  3552. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  3553. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  3554. }
  3555. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  3556. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3]))
  3557. pred_direct_motion(h, &mb_type);
  3558. }else{
  3559. assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE); //FIXME SP correct ?
  3560. for(i=0; i<4; i++){
  3561. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  3562. if(h->sub_mb_type[i] >=4){
  3563. av_log(h->s.avctx, AV_LOG_ERROR, "P sub_mb_type %d out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
  3564. return -1;
  3565. }
  3566. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  3567. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  3568. }
  3569. }
  3570. for(list=0; list<2; list++){
  3571. const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  3572. if(ref_count == 0) continue;
  3573. for(i=0; i<4; i++){
  3574. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  3575. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  3576. ref[list][i] = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
  3577. }else{
  3578. //FIXME
  3579. ref[list][i] = -1;
  3580. }
  3581. }
  3582. }
  3583. for(list=0; list<2; list++){
  3584. const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  3585. if(ref_count == 0) continue;
  3586. for(i=0; i<4; i++){
  3587. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  3588. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  3589. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  3590. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  3591. const int sub_mb_type= h->sub_mb_type[i];
  3592. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  3593. for(j=0; j<sub_partition_count[i]; j++){
  3594. int mx, my;
  3595. const int index= 4*i + block_width*j;
  3596. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  3597. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  3598. mx += get_se_golomb(&s->gb);
  3599. my += get_se_golomb(&s->gb);
  3600. tprintf("final mv:%d %d\n", mx, my);
  3601. if(IS_SUB_8X8(sub_mb_type)){
  3602. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  3603. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  3604. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  3605. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  3606. }else if(IS_SUB_8X4(sub_mb_type)){
  3607. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  3608. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  3609. }else if(IS_SUB_4X8(sub_mb_type)){
  3610. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  3611. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  3612. }else{
  3613. assert(IS_SUB_4X4(sub_mb_type));
  3614. mv_cache[ 0 ][0]= mx;
  3615. mv_cache[ 0 ][1]= my;
  3616. }
  3617. }
  3618. }else{
  3619. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  3620. p[0] = p[1]=
  3621. p[8] = p[9]= 0;
  3622. }
  3623. }
  3624. }
  3625. }else if(IS_DIRECT(mb_type)){
  3626. pred_direct_motion(h, &mb_type);
  3627. s->current_picture.mb_type[mb_xy]= mb_type;
  3628. }else{
  3629. int list, mx, my, i;
  3630. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  3631. if(IS_16X16(mb_type)){
  3632. for(list=0; list<2; list++){
  3633. if(h->ref_count[list]>0){
  3634. if(IS_DIR(mb_type, 0, list)){
  3635. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  3636. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  3637. }
  3638. }
  3639. }
  3640. for(list=0; list<2; list++){
  3641. if(IS_DIR(mb_type, 0, list)){
  3642. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  3643. mx += get_se_golomb(&s->gb);
  3644. my += get_se_golomb(&s->gb);
  3645. tprintf("final mv:%d %d\n", mx, my);
  3646. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  3647. }
  3648. }
  3649. }
  3650. else if(IS_16X8(mb_type)){
  3651. for(list=0; list<2; list++){
  3652. if(h->ref_count[list]>0){
  3653. for(i=0; i<2; i++){
  3654. if(IS_DIR(mb_type, i, list)){
  3655. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  3656. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  3657. }else // needed only for mixed refs (e.g. B_L0_L1_16x8)
  3658. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  3659. }
  3660. }
  3661. }
  3662. for(list=0; list<2; list++){
  3663. for(i=0; i<2; i++){
  3664. if(IS_DIR(mb_type, i, list)){
  3665. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  3666. mx += get_se_golomb(&s->gb);
  3667. my += get_se_golomb(&s->gb);
  3668. tprintf("final mv:%d %d\n", mx, my);
  3669. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  3670. }else
  3671. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  3672. }
  3673. }
  3674. }else{
  3675. assert(IS_8X16(mb_type));
  3676. for(list=0; list<2; list++){
  3677. if(h->ref_count[list]>0){
  3678. for(i=0; i<2; i++){
  3679. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  3680. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  3681. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  3682. }else // needed only for mixed refs
  3683. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  3684. }
  3685. }
  3686. }
  3687. for(list=0; list<2; list++){
  3688. for(i=0; i<2; i++){
  3689. if(IS_DIR(mb_type, i, list)){
  3690. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  3691. mx += get_se_golomb(&s->gb);
  3692. my += get_se_golomb(&s->gb);
  3693. tprintf("final mv:%d %d\n", mx, my);
  3694. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  3695. }else
  3696. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  3697. }
  3698. }
  3699. }
  3700. }
  3701. if(IS_INTER(mb_type))
  3702. write_back_motion(h, mb_type);
  3703. if(!IS_INTRA16x16(mb_type)){
  3704. cbp= get_ue_golomb(&s->gb);
  3705. if(cbp > 47){
  3706. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%d) at %d %d\n", cbp, s->mb_x, s->mb_y);
  3707. return -1;
  3708. }
  3709. if(IS_INTRA4x4(mb_type))
  3710. cbp= golomb_to_intra4x4_cbp[cbp];
  3711. else
  3712. cbp= golomb_to_inter_cbp[cbp];
  3713. }
  3714. if(cbp || IS_INTRA16x16(mb_type)){
  3715. int i8x8, i4x4, chroma_idx;
  3716. int chroma_qp, dquant;
  3717. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  3718. const uint8_t *scan, *dc_scan;
  3719. // fill_non_zero_count_cache(h);
  3720. if(IS_INTERLACED(mb_type)){
  3721. scan= field_scan;
  3722. dc_scan= luma_dc_field_scan;
  3723. }else{
  3724. scan= zigzag_scan;
  3725. dc_scan= luma_dc_zigzag_scan;
  3726. }
  3727. dquant= get_se_golomb(&s->gb);
  3728. if( dquant > 25 || dquant < -26 ){
  3729. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  3730. return -1;
  3731. }
  3732. s->qscale += dquant;
  3733. if(((unsigned)s->qscale) > 51){
  3734. if(s->qscale<0) s->qscale+= 52;
  3735. else s->qscale-= 52;
  3736. }
  3737. h->chroma_qp= chroma_qp= get_chroma_qp(h, s->qscale);
  3738. if(IS_INTRA16x16(mb_type)){
  3739. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, s->qscale, 16) < 0){
  3740. return -1; //FIXME continue if partotioned and other retirn -1 too
  3741. }
  3742. assert((cbp&15) == 0 || (cbp&15) == 15);
  3743. if(cbp&15){
  3744. for(i8x8=0; i8x8<4; i8x8++){
  3745. for(i4x4=0; i4x4<4; i4x4++){
  3746. const int index= i4x4 + 4*i8x8;
  3747. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, s->qscale, 15) < 0 ){
  3748. return -1;
  3749. }
  3750. }
  3751. }
  3752. }else{
  3753. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  3754. }
  3755. }else{
  3756. for(i8x8=0; i8x8<4; i8x8++){
  3757. if(cbp & (1<<i8x8)){
  3758. for(i4x4=0; i4x4<4; i4x4++){
  3759. const int index= i4x4 + 4*i8x8;
  3760. if( decode_residual(h, gb, h->mb + 16*index, index, scan, s->qscale, 16) <0 ){
  3761. return -1;
  3762. }
  3763. }
  3764. }else{
  3765. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  3766. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  3767. }
  3768. }
  3769. }
  3770. if(cbp&0x30){
  3771. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  3772. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, chroma_qp, 4) < 0){
  3773. return -1;
  3774. }
  3775. }
  3776. if(cbp&0x20){
  3777. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  3778. for(i4x4=0; i4x4<4; i4x4++){
  3779. const int index= 16 + 4*chroma_idx + i4x4;
  3780. if( decode_residual(h, gb, h->mb + 16*index, index, scan + 1, chroma_qp, 15) < 0){
  3781. return -1;
  3782. }
  3783. }
  3784. }
  3785. }else{
  3786. uint8_t * const nnz= &h->non_zero_count_cache[0];
  3787. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  3788. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  3789. }
  3790. }else{
  3791. uint8_t * const nnz= &h->non_zero_count_cache[0];
  3792. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  3793. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  3794. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  3795. }
  3796. s->current_picture.qscale_table[mb_xy]= s->qscale;
  3797. write_back_non_zero_count(h);
  3798. return 0;
  3799. }
  3800. static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
  3801. uint8_t *state= &h->cabac_state[ctx_base];
  3802. int mb_type;
  3803. if(intra_slice){
  3804. MpegEncContext * const s = &h->s;
  3805. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  3806. const int mba_xy = mb_xy - 1;
  3807. const int mbb_xy = mb_xy - s->mb_stride;
  3808. int ctx=0;
  3809. if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
  3810. ctx++;
  3811. if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
  3812. ctx++;
  3813. if( get_cabac( &h->cabac, &state[ctx] ) == 0 )
  3814. return 0; /* I4x4 */
  3815. state += 2;
  3816. }else{
  3817. if( get_cabac( &h->cabac, &state[0] ) == 0 )
  3818. return 0; /* I4x4 */
  3819. }
  3820. if( get_cabac_terminate( &h->cabac ) )
  3821. return 25; /* PCM */
  3822. mb_type = 1; /* I16x16 */
  3823. if( get_cabac( &h->cabac, &state[1] ) )
  3824. mb_type += 12; /* cbp_luma != 0 */
  3825. if( get_cabac( &h->cabac, &state[2] ) ) {
  3826. if( get_cabac( &h->cabac, &state[2+intra_slice] ) )
  3827. mb_type += 4 * 2; /* cbp_chroma == 2 */
  3828. else
  3829. mb_type += 4 * 1; /* cbp_chroma == 1 */
  3830. }
  3831. if( get_cabac( &h->cabac, &state[3+intra_slice] ) )
  3832. mb_type += 2;
  3833. if( get_cabac( &h->cabac, &state[3+2*intra_slice] ) )
  3834. mb_type += 1;
  3835. return mb_type;
  3836. }
  3837. static int decode_cabac_mb_type( H264Context *h ) {
  3838. MpegEncContext * const s = &h->s;
  3839. if( h->slice_type == I_TYPE ) {
  3840. return decode_cabac_intra_mb_type(h, 3, 1);
  3841. } else if( h->slice_type == P_TYPE ) {
  3842. if( get_cabac( &h->cabac, &h->cabac_state[14] ) == 0 ) {
  3843. /* P-type */
  3844. if( get_cabac( &h->cabac, &h->cabac_state[15] ) == 0 ) {
  3845. if( get_cabac( &h->cabac, &h->cabac_state[16] ) == 0 )
  3846. return 0; /* P_L0_D16x16; */
  3847. else
  3848. return 3; /* P_8x8; */
  3849. } else {
  3850. if( get_cabac( &h->cabac, &h->cabac_state[17] ) == 0 )
  3851. return 2; /* P_L0_D8x16; */
  3852. else
  3853. return 1; /* P_L0_D16x8; */
  3854. }
  3855. } else {
  3856. return decode_cabac_intra_mb_type(h, 17, 0) + 5;
  3857. }
  3858. } else if( h->slice_type == B_TYPE ) {
  3859. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  3860. const int mba_xy = mb_xy - 1;
  3861. const int mbb_xy = mb_xy - s->mb_stride;
  3862. int ctx = 0;
  3863. int bits;
  3864. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] )
  3865. && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
  3866. ctx++;
  3867. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] )
  3868. && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
  3869. ctx++;
  3870. if( !get_cabac( &h->cabac, &h->cabac_state[27+ctx] ) )
  3871. return 0; /* B_Direct_16x16 */
  3872. if( !get_cabac( &h->cabac, &h->cabac_state[27+3] ) ) {
  3873. return 1 + get_cabac( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
  3874. }
  3875. bits = get_cabac( &h->cabac, &h->cabac_state[27+4] ) << 3;
  3876. bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] ) << 2;
  3877. bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] ) << 1;
  3878. bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] );
  3879. if( bits < 8 )
  3880. return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
  3881. else if( bits == 13 ) {
  3882. return decode_cabac_intra_mb_type(h, 32, 0) + 23;
  3883. } else if( bits == 14 )
  3884. return 11; /* B_L1_L0_8x16 */
  3885. else if( bits == 15 )
  3886. return 22; /* B_8x8 */
  3887. bits= ( bits<<1 ) | get_cabac( &h->cabac, &h->cabac_state[27+5] );
  3888. return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
  3889. } else {
  3890. /* TODO SI/SP frames? */
  3891. return -1;
  3892. }
  3893. }
  3894. static int decode_cabac_mb_skip( H264Context *h) {
  3895. MpegEncContext * const s = &h->s;
  3896. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  3897. const int mba_xy = mb_xy - 1;
  3898. const int mbb_xy = mb_xy - s->mb_stride;
  3899. int ctx = 0;
  3900. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
  3901. ctx++;
  3902. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
  3903. ctx++;
  3904. if( h->slice_type == P_TYPE || h->slice_type == SP_TYPE)
  3905. return get_cabac( &h->cabac, &h->cabac_state[11+ctx] );
  3906. else /* B-frame */
  3907. return get_cabac( &h->cabac, &h->cabac_state[24+ctx] );
  3908. }
  3909. static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
  3910. int mode = 0;
  3911. if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
  3912. return pred_mode;
  3913. if( get_cabac( &h->cabac, &h->cabac_state[69] ) )
  3914. mode += 1;
  3915. if( get_cabac( &h->cabac, &h->cabac_state[69] ) )
  3916. mode += 2;
  3917. if( get_cabac( &h->cabac, &h->cabac_state[69] ) )
  3918. mode += 4;
  3919. if( mode >= pred_mode )
  3920. return mode + 1;
  3921. else
  3922. return mode;
  3923. }
  3924. static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
  3925. MpegEncContext * const s = &h->s;
  3926. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  3927. const int mba_xy = mb_xy - 1;
  3928. const int mbb_xy = mb_xy - s->mb_stride;
  3929. int ctx = 0;
  3930. /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
  3931. if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
  3932. ctx++;
  3933. if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
  3934. ctx++;
  3935. if( get_cabac( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
  3936. return 0;
  3937. if( get_cabac( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  3938. return 1;
  3939. if( get_cabac( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  3940. return 2;
  3941. else
  3942. return 3;
  3943. }
  3944. static const uint8_t block_idx_x[16] = {
  3945. 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
  3946. };
  3947. static const uint8_t block_idx_y[16] = {
  3948. 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
  3949. };
  3950. static const uint8_t block_idx_xy[4][4] = {
  3951. { 0, 2, 8, 10},
  3952. { 1, 3, 9, 11},
  3953. { 4, 6, 12, 14},
  3954. { 5, 7, 13, 15}
  3955. };
  3956. static int decode_cabac_mb_cbp_luma( H264Context *h) {
  3957. MpegEncContext * const s = &h->s;
  3958. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  3959. int cbp = 0;
  3960. int i8x8;
  3961. h->cbp_table[mb_xy] = 0; /* FIXME aaahahahah beurk */
  3962. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  3963. int mba_xy = -1;
  3964. int mbb_xy = -1;
  3965. int x, y;
  3966. int ctx = 0;
  3967. x = block_idx_x[4*i8x8];
  3968. y = block_idx_y[4*i8x8];
  3969. if( x > 0 )
  3970. mba_xy = mb_xy;
  3971. else if( s->mb_x > 0 ) {
  3972. mba_xy = mb_xy - 1;
  3973. if (h->slice_table[mba_xy] != h->slice_num) {
  3974. mba_xy = -1;
  3975. }
  3976. }
  3977. if( y > 0 )
  3978. mbb_xy = mb_xy;
  3979. else if( s->mb_y > 0 ) {
  3980. mbb_xy = mb_xy - s->mb_stride;
  3981. if (h->slice_table[mbb_xy] != h->slice_num) {
  3982. mbb_xy = -1;
  3983. }
  3984. }
  3985. /* No need to test for skip as we put 0 for skip block */
  3986. if( mba_xy >= 0 ) {
  3987. int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
  3988. if( ((h->cbp_table[mba_xy] >> i8x8a)&0x01) == 0 )
  3989. ctx++;
  3990. }
  3991. if( mbb_xy >= 0 ) {
  3992. int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
  3993. if( ((h->cbp_table[mbb_xy] >> i8x8b)&0x01) == 0 )
  3994. ctx += 2;
  3995. }
  3996. if( get_cabac( &h->cabac, &h->cabac_state[73 + ctx] ) ) {
  3997. cbp |= 1 << i8x8;
  3998. h->cbp_table[mb_xy] = cbp; /* FIXME aaahahahah beurk */
  3999. }
  4000. }
  4001. return cbp;
  4002. }
  4003. static int decode_cabac_mb_cbp_chroma( H264Context *h) {
  4004. int ctx;
  4005. int cbp_a, cbp_b;
  4006. cbp_a = (h->left_cbp>>4)&0x03;
  4007. cbp_b = (h-> top_cbp>>4)&0x03;
  4008. ctx = 0;
  4009. if( cbp_a > 0 ) ctx++;
  4010. if( cbp_b > 0 ) ctx += 2;
  4011. if( get_cabac( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
  4012. return 0;
  4013. ctx = 4;
  4014. if( cbp_a == 2 ) ctx++;
  4015. if( cbp_b == 2 ) ctx += 2;
  4016. return 1 + get_cabac( &h->cabac, &h->cabac_state[77 + ctx] );
  4017. }
  4018. static int decode_cabac_mb_dqp( H264Context *h) {
  4019. MpegEncContext * const s = &h->s;
  4020. int mbn_xy;
  4021. int ctx = 0;
  4022. int val = 0;
  4023. if( s->mb_x > 0 )
  4024. mbn_xy = s->mb_x + s->mb_y*s->mb_stride - 1;
  4025. else
  4026. mbn_xy = s->mb_width - 1 + (s->mb_y-1)*s->mb_stride;
  4027. if( h->last_qscale_diff != 0 && ( IS_INTRA16x16(s->current_picture.mb_type[mbn_xy] ) || (h->cbp_table[mbn_xy]&0x3f) ) )
  4028. ctx++;
  4029. while( get_cabac( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
  4030. if( ctx < 2 )
  4031. ctx = 2;
  4032. else
  4033. ctx = 3;
  4034. val++;
  4035. }
  4036. if( val&0x01 )
  4037. return (val + 1)/2;
  4038. else
  4039. return -(val + 1)/2;
  4040. }
  4041. static int decode_cabac_p_mb_sub_type( H264Context *h ) {
  4042. if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
  4043. return 0; /* 8x8 */
  4044. if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
  4045. return 1; /* 8x4 */
  4046. if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
  4047. return 2; /* 4x8 */
  4048. return 3; /* 4x4 */
  4049. }
  4050. static int decode_cabac_b_mb_sub_type( H264Context *h ) {
  4051. int type;
  4052. if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
  4053. return 0; /* B_Direct_8x8 */
  4054. if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
  4055. return 1 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */
  4056. type = 3;
  4057. if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
  4058. if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
  4059. return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
  4060. type += 4;
  4061. }
  4062. type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
  4063. type += get_cabac( &h->cabac, &h->cabac_state[39] );
  4064. return type;
  4065. }
  4066. static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
  4067. int refa = h->ref_cache[list][scan8[n] - 1];
  4068. int refb = h->ref_cache[list][scan8[n] - 8];
  4069. int ref = 0;
  4070. int ctx = 0;
  4071. if( h->slice_type == B_TYPE) {
  4072. if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
  4073. ctx++;
  4074. if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
  4075. ctx += 2;
  4076. } else {
  4077. if( refa > 0 )
  4078. ctx++;
  4079. if( refb > 0 )
  4080. ctx += 2;
  4081. }
  4082. while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
  4083. ref++;
  4084. if( ctx < 4 )
  4085. ctx = 4;
  4086. else
  4087. ctx = 5;
  4088. }
  4089. return ref;
  4090. }
  4091. static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
  4092. int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
  4093. abs( h->mvd_cache[list][scan8[n] - 8][l] );
  4094. int ctxbase = (l == 0) ? 40 : 47;
  4095. int ctx, mvd;
  4096. if( amvd < 3 )
  4097. ctx = 0;
  4098. else if( amvd > 32 )
  4099. ctx = 2;
  4100. else
  4101. ctx = 1;
  4102. if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
  4103. return 0;
  4104. mvd= 1;
  4105. ctx= 3;
  4106. while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
  4107. mvd++;
  4108. if( ctx < 6 )
  4109. ctx++;
  4110. }
  4111. if( mvd >= 9 ) {
  4112. int k = 3;
  4113. while( get_cabac_bypass( &h->cabac ) ) {
  4114. mvd += 1 << k;
  4115. k++;
  4116. }
  4117. while( k-- ) {
  4118. if( get_cabac_bypass( &h->cabac ) )
  4119. mvd += 1 << k;
  4120. }
  4121. }
  4122. if( get_cabac_bypass( &h->cabac ) ) return -mvd;
  4123. else return mvd;
  4124. }
  4125. static int inline get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) {
  4126. int nza, nzb;
  4127. int ctx = 0;
  4128. if( cat == 0 ) {
  4129. nza = h->left_cbp&0x100;
  4130. nzb = h-> top_cbp&0x100;
  4131. } else if( cat == 1 || cat == 2 ) {
  4132. nza = h->non_zero_count_cache[scan8[idx] - 1];
  4133. nzb = h->non_zero_count_cache[scan8[idx] - 8];
  4134. } else if( cat == 3 ) {
  4135. nza = (h->left_cbp>>(6+idx))&0x01;
  4136. nzb = (h-> top_cbp>>(6+idx))&0x01;
  4137. } else {
  4138. assert(cat == 4);
  4139. nza = h->non_zero_count_cache[scan8[16+idx] - 1];
  4140. nzb = h->non_zero_count_cache[scan8[16+idx] - 8];
  4141. }
  4142. if( nza > 0 )
  4143. ctx++;
  4144. if( nzb > 0 )
  4145. ctx += 2;
  4146. return ctx + 4 * cat;
  4147. }
  4148. static int inline decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, int qp, int max_coeff) {
  4149. const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
  4150. const uint16_t *qmul= dequant_coeff[qp];
  4151. static const int significant_coeff_flag_offset[5] = { 0, 15, 29, 44, 47 };
  4152. static const int coeff_abs_level_m1_offset[5] = {227+ 0, 227+10, 227+20, 227+30, 227+39 };
  4153. int index[16];
  4154. int i, last;
  4155. int coeff_count = 0;
  4156. int abslevel1 = 1;
  4157. int abslevelgt1 = 0;
  4158. /* cat: 0-> DC 16x16 n = 0
  4159. * 1-> AC 16x16 n = luma4x4idx
  4160. * 2-> Luma4x4 n = luma4x4idx
  4161. * 3-> DC Chroma n = iCbCr
  4162. * 4-> AC Chroma n = 4 * iCbCr + chroma4x4idx
  4163. */
  4164. /* read coded block flag */
  4165. if( get_cabac( &h->cabac, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n ) ] ) == 0 ) {
  4166. if( cat == 1 || cat == 2 )
  4167. h->non_zero_count_cache[scan8[n]] = 0;
  4168. else if( cat == 4 )
  4169. h->non_zero_count_cache[scan8[16+n]] = 0;
  4170. return 0;
  4171. }
  4172. for(last= 0; last < max_coeff - 1; last++) {
  4173. if( get_cabac( &h->cabac, &h->cabac_state[105+significant_coeff_flag_offset[cat]+last] )) {
  4174. index[coeff_count++] = last;
  4175. if( get_cabac( &h->cabac, &h->cabac_state[166+significant_coeff_flag_offset[cat]+last] ) ) {
  4176. last= max_coeff;
  4177. break;
  4178. }
  4179. }
  4180. }
  4181. if( last == max_coeff -1 ) {
  4182. index[coeff_count++] = last;
  4183. }
  4184. assert(coeff_count > 0);
  4185. if( cat == 0 )
  4186. h->cbp_table[mb_xy] |= 0x100;
  4187. else if( cat == 1 || cat == 2 )
  4188. h->non_zero_count_cache[scan8[n]] = coeff_count;
  4189. else if( cat == 3 )
  4190. h->cbp_table[mb_xy] |= 0x40 << n;
  4191. else {
  4192. assert( cat == 4 );
  4193. h->non_zero_count_cache[scan8[16+n]] = coeff_count;
  4194. }
  4195. for( i = coeff_count - 1; i >= 0; i-- ) {
  4196. int ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 )) + coeff_abs_level_m1_offset[cat];
  4197. int j= scantable[index[i]];
  4198. if( get_cabac( &h->cabac, &h->cabac_state[ctx] ) == 0 ) {
  4199. if( cat == 0 || cat == 3 ) {
  4200. if( get_cabac_bypass( &h->cabac ) ) block[j] = -1;
  4201. else block[j] = 1;
  4202. }else{
  4203. if( get_cabac_bypass( &h->cabac ) ) block[j] = -qmul[j];
  4204. else block[j] = qmul[j];
  4205. }
  4206. abslevel1++;
  4207. } else {
  4208. int coeff_abs = 2;
  4209. ctx = 5 + FFMIN( 4, abslevelgt1 ) + coeff_abs_level_m1_offset[cat];
  4210. while( coeff_abs < 15 && get_cabac( &h->cabac, &h->cabac_state[ctx] ) ) {
  4211. coeff_abs++;
  4212. }
  4213. if( coeff_abs >= 15 ) {
  4214. int j = 0;
  4215. while( get_cabac_bypass( &h->cabac ) ) {
  4216. coeff_abs += 1 << j;
  4217. j++;
  4218. }
  4219. while( j-- ) {
  4220. if( get_cabac_bypass( &h->cabac ) )
  4221. coeff_abs += 1 << j ;
  4222. }
  4223. }
  4224. if( cat == 0 || cat == 3 ) {
  4225. if( get_cabac_bypass( &h->cabac ) ) block[j] = -coeff_abs;
  4226. else block[j] = coeff_abs;
  4227. }else{
  4228. if( get_cabac_bypass( &h->cabac ) ) block[j] = -coeff_abs * qmul[j];
  4229. else block[j] = coeff_abs * qmul[j];
  4230. }
  4231. abslevelgt1++;
  4232. }
  4233. }
  4234. return 0;
  4235. }
  4236. /**
  4237. * decodes a macroblock
  4238. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  4239. */
  4240. static int decode_mb_cabac(H264Context *h) {
  4241. MpegEncContext * const s = &h->s;
  4242. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4243. int mb_type, partition_count, cbp = 0;
  4244. s->dsp.clear_blocks(h->mb); //FIXME avoid if allready clear (move after skip handlong?)
  4245. if( h->sps.mb_aff ) {
  4246. av_log( h->s.avctx, AV_LOG_ERROR, "Fields not supported with CABAC\n" );
  4247. return -1;
  4248. }
  4249. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  4250. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) {
  4251. /* read skip flags */
  4252. if( decode_cabac_mb_skip( h ) ) {
  4253. decode_mb_skip(h);
  4254. h->cbp_table[mb_xy] = 0;
  4255. h->chroma_pred_mode_table[mb_xy] = 0;
  4256. h->last_qscale_diff = 0;
  4257. return 0;
  4258. }
  4259. }
  4260. h->prev_mb_skiped = 0;
  4261. if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
  4262. av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
  4263. return -1;
  4264. }
  4265. if( h->slice_type == B_TYPE ) {
  4266. if( mb_type < 23 ){
  4267. partition_count= b_mb_type_info[mb_type].partition_count;
  4268. mb_type= b_mb_type_info[mb_type].type;
  4269. }else{
  4270. mb_type -= 23;
  4271. goto decode_intra_mb;
  4272. }
  4273. } else if( h->slice_type == P_TYPE ) {
  4274. if( mb_type < 5) {
  4275. partition_count= p_mb_type_info[mb_type].partition_count;
  4276. mb_type= p_mb_type_info[mb_type].type;
  4277. } else {
  4278. mb_type -= 5;
  4279. goto decode_intra_mb;
  4280. }
  4281. } else {
  4282. assert(h->slice_type == I_TYPE);
  4283. decode_intra_mb:
  4284. partition_count = 0;
  4285. cbp= i_mb_type_info[mb_type].cbp;
  4286. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  4287. mb_type= i_mb_type_info[mb_type].type;
  4288. }
  4289. #if 0
  4290. if(h->mb_field_decoding_flag)
  4291. mb_type |= MB_TYPE_INTERLACED;
  4292. #endif
  4293. s->current_picture.mb_type[mb_xy]= mb_type;
  4294. h->slice_table[ mb_xy ]= h->slice_num;
  4295. if(IS_INTRA_PCM(mb_type)) {
  4296. const uint8_t *ptr;
  4297. unsigned int x, y;
  4298. // We assume these blocks are very rare so we dont optimize it.
  4299. // FIXME The two following lines get the bitstream position in the cabac
  4300. // decode, I think it should be done by a function in cabac.h (or cabac.c).
  4301. ptr= h->cabac.bytestream;
  4302. if (h->cabac.low&0x1) ptr-=CABAC_BITS/8;
  4303. // The pixels are stored in the same order as levels in h->mb array.
  4304. for(y=0; y<16; y++){
  4305. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  4306. for(x=0; x<16; x++){
  4307. tprintf("LUMA ICPM LEVEL (%3d)\n", *ptr);
  4308. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= *ptr++;
  4309. }
  4310. }
  4311. for(y=0; y<8; y++){
  4312. const int index= 256 + 4*(y&3) + 32*(y>>2);
  4313. for(x=0; x<8; x++){
  4314. tprintf("CHROMA U ICPM LEVEL (%3d)\n", *ptr);
  4315. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  4316. }
  4317. }
  4318. for(y=0; y<8; y++){
  4319. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  4320. for(x=0; x<8; x++){
  4321. tprintf("CHROMA V ICPM LEVEL (%3d)\n", *ptr);
  4322. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  4323. }
  4324. }
  4325. ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);
  4326. // All blocks are presents
  4327. h->cbp_table[mb_xy] = 0x1ef;
  4328. h->chroma_pred_mode_table[mb_xy] = 0;
  4329. // In deblocking, the quantiser is 0
  4330. s->current_picture.qscale_table[mb_xy]= 0;
  4331. h->chroma_qp = get_chroma_qp(h, 0);
  4332. // All coeffs are presents
  4333. memset(h->non_zero_count[mb_xy], 16, 16);
  4334. return 0;
  4335. }
  4336. fill_caches(h, mb_type, 0);
  4337. if( IS_INTRA( mb_type ) ) {
  4338. if( IS_INTRA4x4( mb_type ) ) {
  4339. int i;
  4340. for( i = 0; i < 16; i++ ) {
  4341. int pred = pred_intra_mode( h, i );
  4342. h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  4343. //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
  4344. }
  4345. write_back_intra_pred_mode(h);
  4346. if( check_intra4x4_pred_mode(h) < 0 ) return -1;
  4347. } else {
  4348. h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );
  4349. if( h->intra16x16_pred_mode < 0 ) return -1;
  4350. }
  4351. h->chroma_pred_mode_table[mb_xy] =
  4352. h->chroma_pred_mode = decode_cabac_mb_chroma_pre_mode( h );
  4353. h->chroma_pred_mode= check_intra_pred_mode( h, h->chroma_pred_mode );
  4354. if( h->chroma_pred_mode < 0 ) return -1;
  4355. } else if( partition_count == 4 ) {
  4356. int i, j, sub_partition_count[4], list, ref[2][4];
  4357. if( h->slice_type == B_TYPE ) {
  4358. for( i = 0; i < 4; i++ ) {
  4359. h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
  4360. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4361. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4362. }
  4363. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  4364. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
  4365. pred_direct_motion(h, &mb_type);
  4366. if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
  4367. for( i = 0; i < 4; i++ )
  4368. if( IS_DIRECT(h->sub_mb_type[i]) )
  4369. fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );
  4370. }
  4371. }
  4372. } else {
  4373. for( i = 0; i < 4; i++ ) {
  4374. h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
  4375. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4376. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4377. }
  4378. }
  4379. for( list = 0; list < 2; list++ ) {
  4380. if( h->ref_count[list] > 0 ) {
  4381. for( i = 0; i < 4; i++ ) {
  4382. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  4383. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4384. if( h->ref_count[list] > 1 )
  4385. ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );
  4386. else
  4387. ref[list][i] = 0;
  4388. } else {
  4389. ref[list][i] = -1;
  4390. }
  4391. h->ref_cache[list][ scan8[4*i]+1 ]=
  4392. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  4393. }
  4394. }
  4395. }
  4396. for(list=0; list<2; list++){
  4397. for(i=0; i<4; i++){
  4398. if(IS_DIRECT(h->sub_mb_type[i])){
  4399. fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);
  4400. continue;
  4401. }
  4402. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];
  4403. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  4404. const int sub_mb_type= h->sub_mb_type[i];
  4405. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  4406. for(j=0; j<sub_partition_count[i]; j++){
  4407. int mpx, mpy;
  4408. int mx, my;
  4409. const int index= 4*i + block_width*j;
  4410. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  4411. int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];
  4412. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);
  4413. mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );
  4414. my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );
  4415. tprintf("final mv:%d %d\n", mx, my);
  4416. if(IS_SUB_8X8(sub_mb_type)){
  4417. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  4418. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  4419. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  4420. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  4421. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]=
  4422. mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;
  4423. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]=
  4424. mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;
  4425. }else if(IS_SUB_8X4(sub_mb_type)){
  4426. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  4427. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  4428. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]= mx- mpx;
  4429. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]= my - mpy;
  4430. }else if(IS_SUB_4X8(sub_mb_type)){
  4431. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  4432. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  4433. mvd_cache[ 0 ][0]= mvd_cache[ 8 ][0]= mx - mpx;
  4434. mvd_cache[ 0 ][1]= mvd_cache[ 8 ][1]= my - mpy;
  4435. }else{
  4436. assert(IS_SUB_4X4(sub_mb_type));
  4437. mv_cache[ 0 ][0]= mx;
  4438. mv_cache[ 0 ][1]= my;
  4439. mvd_cache[ 0 ][0]= mx - mpx;
  4440. mvd_cache[ 0 ][1]= my - mpy;
  4441. }
  4442. }
  4443. }else{
  4444. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  4445. uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];
  4446. p[0] = p[1] = p[8] = p[9] = 0;
  4447. pd[0]= pd[1]= pd[8]= pd[9]= 0;
  4448. }
  4449. }
  4450. }
  4451. } else if( IS_DIRECT(mb_type) ) {
  4452. pred_direct_motion(h, &mb_type);
  4453. s->current_picture.mb_type[mb_xy]= mb_type;
  4454. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  4455. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  4456. } else {
  4457. int list, mx, my, i, mpx, mpy;
  4458. if(IS_16X16(mb_type)){
  4459. for(list=0; list<2; list++){
  4460. if(IS_DIR(mb_type, 0, list)){
  4461. if(h->ref_count[list] > 0 ){
  4462. const int ref = h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 0 ) : 0;
  4463. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
  4464. }
  4465. }
  4466. }
  4467. for(list=0; list<2; list++){
  4468. if(IS_DIR(mb_type, 0, list)){
  4469. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);
  4470. mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );
  4471. my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );
  4472. tprintf("final mv:%d %d\n", mx, my);
  4473. fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  4474. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  4475. }
  4476. }
  4477. }
  4478. else if(IS_16X8(mb_type)){
  4479. for(list=0; list<2; list++){
  4480. if(h->ref_count[list]>0){
  4481. for(i=0; i<2; i++){
  4482. if(IS_DIR(mb_type, i, list)){
  4483. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 8*i ) : 0;
  4484. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
  4485. }else
  4486. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  4487. }
  4488. }
  4489. }
  4490. for(list=0; list<2; list++){
  4491. for(i=0; i<2; i++){
  4492. if(IS_DIR(mb_type, i, list)){
  4493. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);
  4494. mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );
  4495. my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );
  4496. tprintf("final mv:%d %d\n", mx, my);
  4497. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);
  4498. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  4499. }else{ // needed only for mixed refs
  4500. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  4501. fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  4502. }
  4503. }
  4504. }
  4505. }else{
  4506. assert(IS_8X16(mb_type));
  4507. for(list=0; list<2; list++){
  4508. if(h->ref_count[list]>0){
  4509. for(i=0; i<2; i++){
  4510. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  4511. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 4*i ) : 0;
  4512. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
  4513. }else
  4514. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4515. }
  4516. }
  4517. }
  4518. for(list=0; list<2; list++){
  4519. for(i=0; i<2; i++){
  4520. if(IS_DIR(mb_type, i, list)){
  4521. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);
  4522. mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );
  4523. my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );
  4524. tprintf("final mv:%d %d\n", mx, my);
  4525. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  4526. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  4527. }else{ // needed only for mixed refs
  4528. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  4529. fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  4530. }
  4531. }
  4532. }
  4533. }
  4534. }
  4535. if( IS_INTER( mb_type ) ) {
  4536. h->chroma_pred_mode_table[mb_xy] = 0;
  4537. write_back_motion( h, mb_type );
  4538. }
  4539. if( !IS_INTRA16x16( mb_type ) ) {
  4540. cbp = decode_cabac_mb_cbp_luma( h );
  4541. cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
  4542. }
  4543. h->cbp_table[mb_xy] = cbp;
  4544. if( cbp || IS_INTRA16x16( mb_type ) ) {
  4545. const uint8_t *scan, *dc_scan;
  4546. int dqp;
  4547. if(IS_INTERLACED(mb_type)){
  4548. scan= field_scan;
  4549. dc_scan= luma_dc_field_scan;
  4550. }else{
  4551. scan= zigzag_scan;
  4552. dc_scan= luma_dc_zigzag_scan;
  4553. }
  4554. h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
  4555. s->qscale += dqp;
  4556. if(((unsigned)s->qscale) > 51){
  4557. if(s->qscale<0) s->qscale+= 52;
  4558. else s->qscale-= 52;
  4559. }
  4560. h->chroma_qp = get_chroma_qp(h, s->qscale);
  4561. if( IS_INTRA16x16( mb_type ) ) {
  4562. int i;
  4563. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
  4564. if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, s->qscale, 16) < 0)
  4565. return -1;
  4566. if( cbp&15 ) {
  4567. for( i = 0; i < 16; i++ ) {
  4568. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
  4569. if( decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, s->qscale, 15) < 0 )
  4570. return -1;
  4571. }
  4572. } else {
  4573. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  4574. }
  4575. } else {
  4576. int i8x8, i4x4;
  4577. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  4578. if( cbp & (1<<i8x8) ) {
  4579. for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
  4580. const int index = 4*i8x8 + i4x4;
  4581. //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
  4582. if( decode_cabac_residual(h, h->mb + 16*index, 2, index, scan, s->qscale, 16) < 0 )
  4583. return -1;
  4584. }
  4585. } else {
  4586. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4587. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  4588. }
  4589. }
  4590. }
  4591. if( cbp&0x30 ){
  4592. int c;
  4593. for( c = 0; c < 2; c++ ) {
  4594. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
  4595. if( decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, h->chroma_qp, 4) < 0)
  4596. return -1;
  4597. }
  4598. }
  4599. if( cbp&0x20 ) {
  4600. int c, i;
  4601. for( c = 0; c < 2; c++ ) {
  4602. for( i = 0; i < 4; i++ ) {
  4603. const int index = 16 + 4 * c + i;
  4604. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
  4605. if( decode_cabac_residual(h, h->mb + 16*index, 4, index - 16, scan + 1, h->chroma_qp, 15) < 0)
  4606. return -1;
  4607. }
  4608. }
  4609. } else {
  4610. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4611. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4612. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4613. }
  4614. } else {
  4615. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4616. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  4617. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4618. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4619. }
  4620. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4621. write_back_non_zero_count(h);
  4622. return 0;
  4623. }
  4624. static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  4625. int i, d;
  4626. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  4627. const int alpha = alpha_table[index_a];
  4628. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  4629. for( i = 0; i < 4; i++ ) {
  4630. if( bS[i] == 0 ) {
  4631. pix += 4 * stride;
  4632. continue;
  4633. }
  4634. if( bS[i] < 4 ) {
  4635. const int tc0 = tc0_table[index_a][bS[i] - 1];
  4636. /* 4px edge length */
  4637. for( d = 0; d < 4; d++ ) {
  4638. const int p0 = pix[-1];
  4639. const int p1 = pix[-2];
  4640. const int p2 = pix[-3];
  4641. const int q0 = pix[0];
  4642. const int q1 = pix[1];
  4643. const int q2 = pix[2];
  4644. if( ABS( p0 - q0 ) < alpha &&
  4645. ABS( p1 - p0 ) < beta &&
  4646. ABS( q1 - q0 ) < beta ) {
  4647. int tc = tc0;
  4648. int i_delta;
  4649. if( ABS( p2 - p0 ) < beta ) {
  4650. pix[-2] = p1 + clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  4651. tc++;
  4652. }
  4653. if( ABS( q2 - q0 ) < beta ) {
  4654. pix[1] = q1 + clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  4655. tc++;
  4656. }
  4657. i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  4658. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  4659. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  4660. }
  4661. pix += stride;
  4662. }
  4663. }else{
  4664. /* 4px edge length */
  4665. for( d = 0; d < 4; d++ ) {
  4666. const int p0 = pix[-1];
  4667. const int p1 = pix[-2];
  4668. const int p2 = pix[-3];
  4669. const int q0 = pix[0];
  4670. const int q1 = pix[1];
  4671. const int q2 = pix[2];
  4672. if( ABS( p0 - q0 ) < alpha &&
  4673. ABS( p1 - p0 ) < beta &&
  4674. ABS( q1 - q0 ) < beta ) {
  4675. if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  4676. if( ABS( p2 - p0 ) < beta)
  4677. {
  4678. const int p3 = pix[-4];
  4679. /* p0', p1', p2' */
  4680. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  4681. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  4682. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  4683. } else {
  4684. /* p0' */
  4685. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  4686. }
  4687. if( ABS( q2 - q0 ) < beta)
  4688. {
  4689. const int q3 = pix[3];
  4690. /* q0', q1', q2' */
  4691. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  4692. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  4693. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  4694. } else {
  4695. /* q0' */
  4696. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  4697. }
  4698. }else{
  4699. /* p0', q0' */
  4700. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  4701. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  4702. }
  4703. }
  4704. pix += stride;
  4705. }
  4706. }
  4707. }
  4708. }
  4709. static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  4710. int i, d;
  4711. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  4712. const int alpha = alpha_table[index_a];
  4713. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  4714. for( i = 0; i < 4; i++ ) {
  4715. if( bS[i] == 0 ) {
  4716. pix += 2 * stride;
  4717. continue;
  4718. }
  4719. if( bS[i] < 4 ) {
  4720. const int tc = tc0_table[index_a][bS[i] - 1] + 1;
  4721. /* 2px edge length (because we use same bS than the one for luma) */
  4722. for( d = 0; d < 2; d++ ){
  4723. const int p0 = pix[-1];
  4724. const int p1 = pix[-2];
  4725. const int q0 = pix[0];
  4726. const int q1 = pix[1];
  4727. if( ABS( p0 - q0 ) < alpha &&
  4728. ABS( p1 - p0 ) < beta &&
  4729. ABS( q1 - q0 ) < beta ) {
  4730. const int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  4731. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  4732. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  4733. //tprintf("filter_mb_edgecv i:%d d:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, qp, index_a, alpha, beta, tc, bS[i], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);
  4734. }
  4735. pix += stride;
  4736. }
  4737. }else{
  4738. /* 2px edge length (because we use same bS than the one for luma) */
  4739. for( d = 0; d < 2; d++ ){
  4740. const int p0 = pix[-1];
  4741. const int p1 = pix[-2];
  4742. const int q0 = pix[0];
  4743. const int q1 = pix[1];
  4744. if( ABS( p0 - q0 ) < alpha &&
  4745. ABS( p1 - p0 ) < beta &&
  4746. ABS( q1 - q0 ) < beta ) {
  4747. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  4748. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  4749. //tprintf("filter_mb_edgecv i:%d d:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);
  4750. }
  4751. pix += stride;
  4752. }
  4753. }
  4754. }
  4755. }
  4756. static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  4757. int i, d;
  4758. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  4759. const int alpha = alpha_table[index_a];
  4760. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  4761. const int pix_next = stride;
  4762. for( i = 0; i < 4; i++ ) {
  4763. if( bS[i] == 0 ) {
  4764. pix += 4;
  4765. continue;
  4766. }
  4767. if( bS[i] < 4 ) {
  4768. const int tc0 = tc0_table[index_a][bS[i] - 1];
  4769. /* 4px edge length */
  4770. for( d = 0; d < 4; d++ ) {
  4771. const int p0 = pix[-1*pix_next];
  4772. const int p1 = pix[-2*pix_next];
  4773. const int p2 = pix[-3*pix_next];
  4774. const int q0 = pix[0];
  4775. const int q1 = pix[1*pix_next];
  4776. const int q2 = pix[2*pix_next];
  4777. if( ABS( p0 - q0 ) < alpha &&
  4778. ABS( p1 - p0 ) < beta &&
  4779. ABS( q1 - q0 ) < beta ) {
  4780. int tc = tc0;
  4781. int i_delta;
  4782. if( ABS( p2 - p0 ) < beta ) {
  4783. pix[-2*pix_next] = p1 + clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  4784. tc++;
  4785. }
  4786. if( ABS( q2 - q0 ) < beta ) {
  4787. pix[pix_next] = q1 + clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  4788. tc++;
  4789. }
  4790. i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  4791. pix[-pix_next] = clip_uint8( p0 + i_delta ); /* p0' */
  4792. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  4793. }
  4794. pix++;
  4795. }
  4796. }else{
  4797. /* 4px edge length */
  4798. for( d = 0; d < 4; d++ ) {
  4799. const int p0 = pix[-1*pix_next];
  4800. const int p1 = pix[-2*pix_next];
  4801. const int p2 = pix[-3*pix_next];
  4802. const int q0 = pix[0];
  4803. const int q1 = pix[1*pix_next];
  4804. const int q2 = pix[2*pix_next];
  4805. if( ABS( p0 - q0 ) < alpha &&
  4806. ABS( p1 - p0 ) < beta &&
  4807. ABS( q1 - q0 ) < beta ) {
  4808. const int p3 = pix[-4*pix_next];
  4809. const int q3 = pix[ 3*pix_next];
  4810. if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  4811. if( ABS( p2 - p0 ) < beta) {
  4812. /* p0', p1', p2' */
  4813. pix[-1*pix_next] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  4814. pix[-2*pix_next] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  4815. pix[-3*pix_next] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  4816. } else {
  4817. /* p0' */
  4818. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  4819. }
  4820. if( ABS( q2 - q0 ) < beta) {
  4821. /* q0', q1', q2' */
  4822. pix[0*pix_next] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  4823. pix[1*pix_next] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  4824. pix[2*pix_next] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  4825. } else {
  4826. /* q0' */
  4827. pix[0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  4828. }
  4829. }else{
  4830. /* p0', q0' */
  4831. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  4832. pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  4833. }
  4834. }
  4835. pix++;
  4836. }
  4837. }
  4838. }
  4839. }
  4840. static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  4841. int i, d;
  4842. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  4843. const int alpha = alpha_table[index_a];
  4844. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  4845. const int pix_next = stride;
  4846. for( i = 0; i < 4; i++ )
  4847. {
  4848. if( bS[i] == 0 ) {
  4849. pix += 2;
  4850. continue;
  4851. }
  4852. if( bS[i] < 4 ) {
  4853. int tc = tc0_table[index_a][bS[i] - 1] + 1;
  4854. /* 2px edge length (see deblocking_filter_edgecv) */
  4855. for( d = 0; d < 2; d++ ) {
  4856. const int p0 = pix[-1*pix_next];
  4857. const int p1 = pix[-2*pix_next];
  4858. const int q0 = pix[0];
  4859. const int q1 = pix[1*pix_next];
  4860. if( ABS( p0 - q0 ) < alpha &&
  4861. ABS( p1 - p0 ) < beta &&
  4862. ABS( q1 - q0 ) < beta ) {
  4863. int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  4864. pix[-pix_next] = clip_uint8( p0 + i_delta ); /* p0' */
  4865. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  4866. }
  4867. pix++;
  4868. }
  4869. }else{
  4870. /* 2px edge length (see deblocking_filter_edgecv) */
  4871. for( d = 0; d < 2; d++ ) {
  4872. const int p0 = pix[-1*pix_next];
  4873. const int p1 = pix[-2*pix_next];
  4874. const int q0 = pix[0];
  4875. const int q1 = pix[1*pix_next];
  4876. if( ABS( p0 - q0 ) < alpha &&
  4877. ABS( p1 - p0 ) < beta &&
  4878. ABS( q1 - q0 ) < beta ) {
  4879. pix[-pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  4880. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  4881. }
  4882. pix++;
  4883. }
  4884. }
  4885. }
  4886. }
  4887. static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr) {
  4888. MpegEncContext * const s = &h->s;
  4889. const int mb_xy= mb_x + mb_y*s->mb_stride;
  4890. int linesize, uvlinesize;
  4891. int dir;
  4892. /* FIXME Implement deblocking filter for field MB */
  4893. if( h->sps.mb_aff ) {
  4894. return;
  4895. }
  4896. linesize = s->linesize;
  4897. uvlinesize = s->uvlinesize;
  4898. /* dir : 0 -> vertical edge, 1 -> horizontal edge */
  4899. for( dir = 0; dir < 2; dir++ )
  4900. {
  4901. int edge;
  4902. const int mbm_xy = dir == 0 ? mb_xy -1 : mb_xy - s->mb_stride;
  4903. int start = h->slice_table[mbm_xy] == 255 ? 1 : 0;
  4904. if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
  4905. start = 1;
  4906. /* Calculate bS */
  4907. for( edge = start; edge < 4; edge++ ) {
  4908. /* mbn_xy: neighbour macroblock (how that works for field ?) */
  4909. int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
  4910. int bS[4];
  4911. int qp;
  4912. if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) ||
  4913. IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) {
  4914. bS[0] = bS[1] = bS[2] = bS[3] = ( edge == 0 ? 4 : 3 );
  4915. } else {
  4916. int i;
  4917. for( i = 0; i < 4; i++ ) {
  4918. int x = dir == 0 ? edge : i;
  4919. int y = dir == 0 ? i : edge;
  4920. int b_idx= 8 + 4 + x + 8*y;
  4921. int bn_idx= b_idx - (dir ? 8:1);
  4922. if( h->non_zero_count_cache[b_idx] != 0 ||
  4923. h->non_zero_count_cache[bn_idx] != 0 ) {
  4924. bS[i] = 2;
  4925. }
  4926. else if( h->slice_type == P_TYPE ) {
  4927. if( h->ref_cache[0][b_idx] != h->ref_cache[0][bn_idx] ||
  4928. ABS( h->mv_cache[0][b_idx][0] - h->mv_cache[0][bn_idx][0] ) >= 4 ||
  4929. ABS( h->mv_cache[0][b_idx][1] - h->mv_cache[0][bn_idx][1] ) >= 4 )
  4930. bS[i] = 1;
  4931. else
  4932. bS[i] = 0;
  4933. } else {
  4934. /* FIXME Add support for B frame */
  4935. return;
  4936. }
  4937. }
  4938. if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
  4939. continue;
  4940. }
  4941. /* Filter edge */
  4942. // Do not use s->qscale as luma quantiser because it has not the same
  4943. // value in IPCM macroblocks.
  4944. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  4945. //tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d, QPc:%d, QPcn:%d\n", mb_x, mb_y, dir, edge, qp, h->chroma_qp, s->current_picture.qscale_table[mbn_xy]);
  4946. if( dir == 0 ) {
  4947. filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
  4948. if( (edge&1) == 0 ) {
  4949. int chroma_qp = ( h->chroma_qp +
  4950. get_chroma_qp( h, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  4951. filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp );
  4952. filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp );
  4953. }
  4954. } else {
  4955. filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
  4956. if( (edge&1) == 0 ) {
  4957. int chroma_qp = ( h->chroma_qp +
  4958. get_chroma_qp( h, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  4959. filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  4960. filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  4961. }
  4962. }
  4963. }
  4964. }
  4965. }
  4966. static int decode_slice(H264Context *h){
  4967. MpegEncContext * const s = &h->s;
  4968. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  4969. s->mb_skip_run= -1;
  4970. if( h->pps.cabac ) {
  4971. int i;
  4972. /* realign */
  4973. align_get_bits( &s->gb );
  4974. /* init cabac */
  4975. ff_init_cabac_states( &h->cabac, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64 );
  4976. ff_init_cabac_decoder( &h->cabac,
  4977. s->gb.buffer + get_bits_count(&s->gb)/8,
  4978. ( s->gb.size_in_bits - get_bits_count(&s->gb) + 7)/8);
  4979. /* calculate pre-state */
  4980. for( i= 0; i < 399; i++ ) {
  4981. int pre;
  4982. if( h->slice_type == I_TYPE )
  4983. pre = clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
  4984. else
  4985. pre = clip( ((cabac_context_init_PB[h->cabac_init_idc][i][0] * s->qscale) >>4 ) + cabac_context_init_PB[h->cabac_init_idc][i][1], 1, 126 );
  4986. if( pre <= 63 )
  4987. h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
  4988. else
  4989. h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
  4990. }
  4991. for(;;){
  4992. int ret = decode_mb_cabac(h);
  4993. int eos = get_cabac_terminate( &h->cabac ); /* End of Slice flag */
  4994. if(ret>=0) hl_decode_mb(h);
  4995. /* XXX: useless as decode_mb_cabac it doesn't support that ... */
  4996. if( ret >= 0 && h->sps.mb_aff ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  4997. s->mb_y++;
  4998. if(ret>=0) ret = decode_mb_cabac(h);
  4999. eos = get_cabac_terminate( &h->cabac );
  5000. hl_decode_mb(h);
  5001. s->mb_y--;
  5002. }
  5003. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 1) {
  5004. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  5005. 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);
  5006. return -1;
  5007. }
  5008. if( ++s->mb_x >= s->mb_width ) {
  5009. s->mb_x = 0;
  5010. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  5011. ++s->mb_y;
  5012. }
  5013. if( eos || s->mb_y >= s->mb_height ) {
  5014. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  5015. 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);
  5016. return 0;
  5017. }
  5018. #if 0
  5019. /* TODO test over-reading in cabac code */
  5020. else if( read too much in h->cabac ) {
  5021. 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);
  5022. return -1;
  5023. }
  5024. #endif
  5025. }
  5026. } else {
  5027. for(;;){
  5028. int ret = decode_mb_cavlc(h);
  5029. if(ret>=0) hl_decode_mb(h);
  5030. if(ret>=0 && h->sps.mb_aff){ //FIXME optimal? or let mb_decode decode 16x32 ?
  5031. s->mb_y++;
  5032. ret = decode_mb_cavlc(h);
  5033. if(ret>=0) hl_decode_mb(h);
  5034. s->mb_y--;
  5035. }
  5036. if(ret<0){
  5037. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  5038. 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);
  5039. return -1;
  5040. }
  5041. if(++s->mb_x >= s->mb_width){
  5042. s->mb_x=0;
  5043. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  5044. if(++s->mb_y >= s->mb_height){
  5045. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  5046. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  5047. 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);
  5048. return 0;
  5049. }else{
  5050. 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);
  5051. return -1;
  5052. }
  5053. }
  5054. }
  5055. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  5056. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  5057. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  5058. 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);
  5059. return 0;
  5060. }else{
  5061. 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);
  5062. return -1;
  5063. }
  5064. }
  5065. }
  5066. }
  5067. #if 0
  5068. for(;s->mb_y < s->mb_height; s->mb_y++){
  5069. for(;s->mb_x < s->mb_width; s->mb_x++){
  5070. int ret= decode_mb(h);
  5071. hl_decode_mb(h);
  5072. if(ret<0){
  5073. fprintf(stderr, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  5074. 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);
  5075. return -1;
  5076. }
  5077. if(++s->mb_x >= s->mb_width){
  5078. s->mb_x=0;
  5079. if(++s->mb_y >= s->mb_height){
  5080. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  5081. 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);
  5082. return 0;
  5083. }else{
  5084. 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);
  5085. return -1;
  5086. }
  5087. }
  5088. }
  5089. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  5090. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  5091. 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);
  5092. return 0;
  5093. }else{
  5094. 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);
  5095. return -1;
  5096. }
  5097. }
  5098. }
  5099. s->mb_x=0;
  5100. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  5101. }
  5102. #endif
  5103. return -1; //not reached
  5104. }
  5105. static inline int decode_vui_parameters(H264Context *h, SPS *sps){
  5106. MpegEncContext * const s = &h->s;
  5107. int aspect_ratio_info_present_flag, aspect_ratio_idc;
  5108. aspect_ratio_info_present_flag= get_bits1(&s->gb);
  5109. if( aspect_ratio_info_present_flag ) {
  5110. aspect_ratio_idc= get_bits(&s->gb, 8);
  5111. if( aspect_ratio_idc == EXTENDED_SAR ) {
  5112. sps->sar.num= get_bits(&s->gb, 16);
  5113. sps->sar.den= get_bits(&s->gb, 16);
  5114. }else if(aspect_ratio_idc < 16){
  5115. sps->sar= pixel_aspect[aspect_ratio_idc];
  5116. }else{
  5117. av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
  5118. return -1;
  5119. }
  5120. }else{
  5121. sps->sar.num=
  5122. sps->sar.den= 0;
  5123. }
  5124. // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
  5125. if(get_bits1(&s->gb)){ /* overscan_info_present_flag */
  5126. get_bits1(&s->gb); /* overscan_appropriate_flag */
  5127. }
  5128. if(get_bits1(&s->gb)){ /* video_signal_type_present_flag */
  5129. get_bits(&s->gb, 3); /* video_format */
  5130. get_bits1(&s->gb); /* video_full_range_flag */
  5131. if(get_bits1(&s->gb)){ /* colour_description_present_flag */
  5132. get_bits(&s->gb, 8); /* colour_primaries */
  5133. get_bits(&s->gb, 8); /* transfer_characteristics */
  5134. get_bits(&s->gb, 8); /* matrix_coefficients */
  5135. }
  5136. }
  5137. if(get_bits1(&s->gb)){ /* chroma_location_info_present_flag */
  5138. get_ue_golomb(&s->gb); /* chroma_sample_location_type_top_field */
  5139. get_ue_golomb(&s->gb); /* chroma_sample_location_type_bottom_field */
  5140. }
  5141. sps->timing_info_present_flag = get_bits1(&s->gb);
  5142. if(sps->timing_info_present_flag){
  5143. sps->num_units_in_tick = get_bits_long(&s->gb, 32);
  5144. sps->time_scale = get_bits_long(&s->gb, 32);
  5145. sps->fixed_frame_rate_flag = get_bits1(&s->gb);
  5146. }
  5147. #if 0
  5148. | nal_hrd_parameters_present_flag |0 |u(1) |
  5149. | if( nal_hrd_parameters_present_flag = = 1) | | |
  5150. | hrd_parameters( ) | | |
  5151. | vcl_hrd_parameters_present_flag |0 |u(1) |
  5152. | if( vcl_hrd_parameters_present_flag = = 1) | | |
  5153. | hrd_parameters( ) | | |
  5154. | if( ( nal_hrd_parameters_present_flag = = 1 | || | |
  5155. | | | |
  5156. |( vcl_hrd_parameters_present_flag = = 1 ) ) | | |
  5157. | low_delay_hrd_flag |0 |u(1) |
  5158. | bitstream_restriction_flag |0 |u(1) |
  5159. | if( bitstream_restriction_flag ) { |0 |u(1) |
  5160. | motion_vectors_over_pic_boundaries_flag |0 |u(1) |
  5161. | max_bytes_per_pic_denom |0 |ue(v) |
  5162. | max_bits_per_mb_denom |0 |ue(v) |
  5163. | log2_max_mv_length_horizontal |0 |ue(v) |
  5164. | log2_max_mv_length_vertical |0 |ue(v) |
  5165. | num_reorder_frames |0 |ue(v) |
  5166. | max_dec_frame_buffering |0 |ue(v) |
  5167. | } | | |
  5168. |} | | |
  5169. #endif
  5170. return 0;
  5171. }
  5172. static inline int decode_seq_parameter_set(H264Context *h){
  5173. MpegEncContext * const s = &h->s;
  5174. int profile_idc, level_idc;
  5175. int sps_id, i;
  5176. SPS *sps;
  5177. profile_idc= get_bits(&s->gb, 8);
  5178. get_bits1(&s->gb); //constraint_set0_flag
  5179. get_bits1(&s->gb); //constraint_set1_flag
  5180. get_bits1(&s->gb); //constraint_set2_flag
  5181. get_bits1(&s->gb); //constraint_set3_flag
  5182. get_bits(&s->gb, 4); // reserved
  5183. level_idc= get_bits(&s->gb, 8);
  5184. sps_id= get_ue_golomb(&s->gb);
  5185. sps= &h->sps_buffer[ sps_id ];
  5186. sps->profile_idc= profile_idc;
  5187. sps->level_idc= level_idc;
  5188. sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
  5189. sps->poc_type= get_ue_golomb(&s->gb);
  5190. if(sps->poc_type == 0){ //FIXME #define
  5191. sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
  5192. } else if(sps->poc_type == 1){//FIXME #define
  5193. sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
  5194. sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
  5195. sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
  5196. sps->poc_cycle_length= get_ue_golomb(&s->gb);
  5197. for(i=0; i<sps->poc_cycle_length; i++)
  5198. sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
  5199. }
  5200. if(sps->poc_type > 2){
  5201. av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
  5202. return -1;
  5203. }
  5204. sps->ref_frame_count= get_ue_golomb(&s->gb);
  5205. if(sps->ref_frame_count > MAX_PICTURE_COUNT-2){
  5206. av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");
  5207. }
  5208. sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
  5209. sps->mb_width= get_ue_golomb(&s->gb) + 1;
  5210. sps->mb_height= get_ue_golomb(&s->gb) + 1;
  5211. if((unsigned)sps->mb_width >= INT_MAX/16 || (unsigned)sps->mb_height >= INT_MAX/16 ||
  5212. avcodec_check_dimensions(NULL, 16*sps->mb_width, 16*sps->mb_height))
  5213. return -1;
  5214. sps->frame_mbs_only_flag= get_bits1(&s->gb);
  5215. if(!sps->frame_mbs_only_flag)
  5216. sps->mb_aff= get_bits1(&s->gb);
  5217. else
  5218. sps->mb_aff= 0;
  5219. sps->direct_8x8_inference_flag= get_bits1(&s->gb);
  5220. sps->crop= get_bits1(&s->gb);
  5221. if(sps->crop){
  5222. sps->crop_left = get_ue_golomb(&s->gb);
  5223. sps->crop_right = get_ue_golomb(&s->gb);
  5224. sps->crop_top = get_ue_golomb(&s->gb);
  5225. sps->crop_bottom= get_ue_golomb(&s->gb);
  5226. if(sps->crop_left || sps->crop_top){
  5227. av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completly supported, this could look slightly wrong ...\n");
  5228. }
  5229. }else{
  5230. sps->crop_left =
  5231. sps->crop_right =
  5232. sps->crop_top =
  5233. sps->crop_bottom= 0;
  5234. }
  5235. sps->vui_parameters_present_flag= get_bits1(&s->gb);
  5236. if( sps->vui_parameters_present_flag )
  5237. decode_vui_parameters(h, sps);
  5238. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  5239. av_log(h->s.avctx, AV_LOG_DEBUG, "sps:%d profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%d/%d/%d/%d %s\n",
  5240. sps_id, sps->profile_idc, sps->level_idc,
  5241. sps->poc_type,
  5242. sps->ref_frame_count,
  5243. sps->mb_width, sps->mb_height,
  5244. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  5245. sps->direct_8x8_inference_flag ? "8B8" : "",
  5246. sps->crop_left, sps->crop_right,
  5247. sps->crop_top, sps->crop_bottom,
  5248. sps->vui_parameters_present_flag ? "VUI" : ""
  5249. );
  5250. }
  5251. return 0;
  5252. }
  5253. static inline int decode_picture_parameter_set(H264Context *h){
  5254. MpegEncContext * const s = &h->s;
  5255. int pps_id= get_ue_golomb(&s->gb);
  5256. PPS *pps= &h->pps_buffer[pps_id];
  5257. pps->sps_id= get_ue_golomb(&s->gb);
  5258. pps->cabac= get_bits1(&s->gb);
  5259. pps->pic_order_present= get_bits1(&s->gb);
  5260. pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
  5261. if(pps->slice_group_count > 1 ){
  5262. pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
  5263. av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
  5264. switch(pps->mb_slice_group_map_type){
  5265. case 0:
  5266. #if 0
  5267. | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
  5268. | run_length[ i ] |1 |ue(v) |
  5269. #endif
  5270. break;
  5271. case 2:
  5272. #if 0
  5273. | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
  5274. |{ | | |
  5275. | top_left_mb[ i ] |1 |ue(v) |
  5276. | bottom_right_mb[ i ] |1 |ue(v) |
  5277. | } | | |
  5278. #endif
  5279. break;
  5280. case 3:
  5281. case 4:
  5282. case 5:
  5283. #if 0
  5284. | slice_group_change_direction_flag |1 |u(1) |
  5285. | slice_group_change_rate_minus1 |1 |ue(v) |
  5286. #endif
  5287. break;
  5288. case 6:
  5289. #if 0
  5290. | slice_group_id_cnt_minus1 |1 |ue(v) |
  5291. | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
  5292. |) | | |
  5293. | slice_group_id[ i ] |1 |u(v) |
  5294. #endif
  5295. break;
  5296. }
  5297. }
  5298. pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  5299. pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  5300. if(pps->ref_count[0] > 32 || pps->ref_count[1] > 32){
  5301. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
  5302. return -1;
  5303. }
  5304. pps->weighted_pred= get_bits1(&s->gb);
  5305. pps->weighted_bipred_idc= get_bits(&s->gb, 2);
  5306. pps->init_qp= get_se_golomb(&s->gb) + 26;
  5307. pps->init_qs= get_se_golomb(&s->gb) + 26;
  5308. pps->chroma_qp_index_offset= get_se_golomb(&s->gb);
  5309. pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
  5310. pps->constrained_intra_pred= get_bits1(&s->gb);
  5311. pps->redundant_pic_cnt_present = get_bits1(&s->gb);
  5312. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  5313. av_log(h->s.avctx, AV_LOG_DEBUG, "pps:%d sps:%d %s slice_groups:%d ref:%d/%d %s qp:%d/%d/%d %s %s %s\n",
  5314. pps_id, pps->sps_id,
  5315. pps->cabac ? "CABAC" : "CAVLC",
  5316. pps->slice_group_count,
  5317. pps->ref_count[0], pps->ref_count[1],
  5318. pps->weighted_pred ? "weighted" : "",
  5319. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset,
  5320. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  5321. pps->constrained_intra_pred ? "CONSTR" : "",
  5322. pps->redundant_pic_cnt_present ? "REDU" : ""
  5323. );
  5324. }
  5325. return 0;
  5326. }
  5327. /**
  5328. * finds the end of the current frame in the bitstream.
  5329. * @return the position of the first byte of the next frame, or -1
  5330. */
  5331. static int find_frame_end(H264Context *h, const uint8_t *buf, int buf_size){
  5332. int i;
  5333. uint32_t state;
  5334. ParseContext *pc = &(h->s.parse_context);
  5335. //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
  5336. // mb_addr= pc->mb_addr - 1;
  5337. state= pc->state;
  5338. for(i=0; i<=buf_size; i++){
  5339. if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  5340. tprintf("find_frame_end new startcode = %08x, frame_start_found = %d, pos = %d\n", state, pc->frame_start_found, i);
  5341. if(pc->frame_start_found){
  5342. // If there isn't one more byte in the buffer
  5343. // the test on first_mb_in_slice cannot be done yet
  5344. // do it at next call.
  5345. if (i >= buf_size) break;
  5346. if (buf[i] & 0x80) {
  5347. // first_mb_in_slice is 0, probably the first nal of a new
  5348. // slice
  5349. tprintf("find_frame_end frame_end_found, state = %08x, pos = %d\n", state, i);
  5350. pc->state=-1;
  5351. pc->frame_start_found= 0;
  5352. return i-4;
  5353. }
  5354. }
  5355. pc->frame_start_found = 1;
  5356. }
  5357. if (i<buf_size)
  5358. state= (state<<8) | buf[i];
  5359. }
  5360. pc->state= state;
  5361. return END_NOT_FOUND;
  5362. }
  5363. static int h264_parse(AVCodecParserContext *s,
  5364. AVCodecContext *avctx,
  5365. uint8_t **poutbuf, int *poutbuf_size,
  5366. const uint8_t *buf, int buf_size)
  5367. {
  5368. H264Context *h = s->priv_data;
  5369. ParseContext *pc = &h->s.parse_context;
  5370. int next;
  5371. next= find_frame_end(h, buf, buf_size);
  5372. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  5373. *poutbuf = NULL;
  5374. *poutbuf_size = 0;
  5375. return buf_size;
  5376. }
  5377. *poutbuf = (uint8_t *)buf;
  5378. *poutbuf_size = buf_size;
  5379. return next;
  5380. }
  5381. static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){
  5382. MpegEncContext * const s = &h->s;
  5383. AVCodecContext * const avctx= s->avctx;
  5384. int buf_index=0;
  5385. #if 0
  5386. int i;
  5387. for(i=0; i<32; i++){
  5388. printf("%X ", buf[i]);
  5389. }
  5390. #endif
  5391. h->slice_num = 0;
  5392. for(;;){
  5393. int consumed;
  5394. int dst_length;
  5395. int bit_length;
  5396. uint8_t *ptr;
  5397. int i, nalsize = 0;
  5398. if(h->is_avc) {
  5399. if(buf_index >= buf_size) break;
  5400. nalsize = 0;
  5401. for(i = 0; i < h->nal_length_size; i++)
  5402. nalsize = (nalsize << 8) | buf[buf_index++];
  5403. } else {
  5404. // start code prefix search
  5405. for(; buf_index + 3 < buf_size; buf_index++){
  5406. // this should allways succeed in the first iteration
  5407. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  5408. break;
  5409. }
  5410. if(buf_index+3 >= buf_size) break;
  5411. buf_index+=3;
  5412. }
  5413. ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, h->is_avc ? nalsize : buf_size - buf_index);
  5414. if(ptr[dst_length - 1] == 0) dst_length--;
  5415. bit_length= 8*dst_length - decode_rbsp_trailing(ptr + dst_length - 1);
  5416. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  5417. av_log(h->s.avctx, AV_LOG_DEBUG, "NAL %d at %d/%d length %d\n", h->nal_unit_type, buf_index, buf_size, dst_length);
  5418. }
  5419. if (h->is_avc && (nalsize != consumed))
  5420. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  5421. buf_index += consumed;
  5422. if( s->hurry_up == 1 && h->nal_ref_idc == 0 )
  5423. continue;
  5424. switch(h->nal_unit_type){
  5425. case NAL_IDR_SLICE:
  5426. idr(h); //FIXME ensure we dont loose some frames if there is reordering
  5427. case NAL_SLICE:
  5428. init_get_bits(&s->gb, ptr, bit_length);
  5429. h->intra_gb_ptr=
  5430. h->inter_gb_ptr= &s->gb;
  5431. s->data_partitioning = 0;
  5432. if(decode_slice_header(h) < 0) return -1;
  5433. if(h->redundant_pic_count==0 && s->hurry_up < 5 )
  5434. decode_slice(h);
  5435. break;
  5436. case NAL_DPA:
  5437. init_get_bits(&s->gb, ptr, bit_length);
  5438. h->intra_gb_ptr=
  5439. h->inter_gb_ptr= NULL;
  5440. s->data_partitioning = 1;
  5441. if(decode_slice_header(h) < 0) return -1;
  5442. break;
  5443. case NAL_DPB:
  5444. init_get_bits(&h->intra_gb, ptr, bit_length);
  5445. h->intra_gb_ptr= &h->intra_gb;
  5446. break;
  5447. case NAL_DPC:
  5448. init_get_bits(&h->inter_gb, ptr, bit_length);
  5449. h->inter_gb_ptr= &h->inter_gb;
  5450. if(h->redundant_pic_count==0 && h->intra_gb_ptr && s->data_partitioning && s->hurry_up < 5 )
  5451. decode_slice(h);
  5452. break;
  5453. case NAL_SEI:
  5454. break;
  5455. case NAL_SPS:
  5456. init_get_bits(&s->gb, ptr, bit_length);
  5457. decode_seq_parameter_set(h);
  5458. if(s->flags& CODEC_FLAG_LOW_DELAY)
  5459. s->low_delay=1;
  5460. avctx->has_b_frames= !s->low_delay;
  5461. break;
  5462. case NAL_PPS:
  5463. init_get_bits(&s->gb, ptr, bit_length);
  5464. decode_picture_parameter_set(h);
  5465. break;
  5466. case NAL_PICTURE_DELIMITER:
  5467. break;
  5468. case NAL_FILTER_DATA:
  5469. break;
  5470. default:
  5471. av_log(avctx, AV_LOG_ERROR, "Unknown NAL code: %d\n", h->nal_unit_type);
  5472. }
  5473. //FIXME move after where irt is set
  5474. s->current_picture.pict_type= s->pict_type;
  5475. s->current_picture.key_frame= s->pict_type == I_TYPE;
  5476. }
  5477. if(!s->current_picture_ptr) return buf_index; //no frame
  5478. h->prev_frame_num_offset= h->frame_num_offset;
  5479. h->prev_frame_num= h->frame_num;
  5480. if(s->current_picture_ptr->reference){
  5481. h->prev_poc_msb= h->poc_msb;
  5482. h->prev_poc_lsb= h->poc_lsb;
  5483. }
  5484. if(s->current_picture_ptr->reference)
  5485. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  5486. ff_er_frame_end(s);
  5487. MPV_frame_end(s);
  5488. return buf_index;
  5489. }
  5490. /**
  5491. * retunrs the number of bytes consumed for building the current frame
  5492. */
  5493. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  5494. if(s->flags&CODEC_FLAG_TRUNCATED){
  5495. pos -= s->parse_context.last_index;
  5496. if(pos<0) pos=0; // FIXME remove (uneeded?)
  5497. return pos;
  5498. }else{
  5499. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  5500. if(pos+10>buf_size) pos=buf_size; // oops ;)
  5501. return pos;
  5502. }
  5503. }
  5504. static int decode_frame(AVCodecContext *avctx,
  5505. void *data, int *data_size,
  5506. uint8_t *buf, int buf_size)
  5507. {
  5508. H264Context *h = avctx->priv_data;
  5509. MpegEncContext *s = &h->s;
  5510. AVFrame *pict = data;
  5511. int buf_index;
  5512. s->flags= avctx->flags;
  5513. s->flags2= avctx->flags2;
  5514. /* no supplementary picture */
  5515. if (buf_size == 0) {
  5516. return 0;
  5517. }
  5518. if(s->flags&CODEC_FLAG_TRUNCATED){
  5519. int next= find_frame_end(h, buf, buf_size);
  5520. if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
  5521. return buf_size;
  5522. //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);
  5523. }
  5524. if(h->is_avc && !h->got_avcC) {
  5525. int i, cnt, nalsize;
  5526. unsigned char *p = avctx->extradata;
  5527. if(avctx->extradata_size < 7) {
  5528. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  5529. return -1;
  5530. }
  5531. if(*p != 1) {
  5532. av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
  5533. return -1;
  5534. }
  5535. /* sps and pps in the avcC always have length coded with 2 bytes,
  5536. so put a fake nal_length_size = 2 while parsing them */
  5537. h->nal_length_size = 2;
  5538. // Decode sps from avcC
  5539. cnt = *(p+5) & 0x1f; // Number of sps
  5540. p += 6;
  5541. for (i = 0; i < cnt; i++) {
  5542. nalsize = BE_16(p) + 2;
  5543. if(decode_nal_units(h, p, nalsize) != nalsize) {
  5544. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  5545. return -1;
  5546. }
  5547. p += nalsize;
  5548. }
  5549. // Decode pps from avcC
  5550. cnt = *(p++); // Number of pps
  5551. for (i = 0; i < cnt; i++) {
  5552. nalsize = BE_16(p) + 2;
  5553. if(decode_nal_units(h, p, nalsize) != nalsize) {
  5554. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  5555. return -1;
  5556. }
  5557. p += nalsize;
  5558. }
  5559. // Now store right nal length size, that will be use to parse all other nals
  5560. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  5561. // Do not reparse avcC
  5562. h->got_avcC = 1;
  5563. }
  5564. if(!h->is_avc && s->avctx->extradata_size && s->picture_number==0){
  5565. if(0 < decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) )
  5566. return -1;
  5567. }
  5568. buf_index=decode_nal_units(h, buf, buf_size);
  5569. if(buf_index < 0)
  5570. return -1;
  5571. //FIXME do something with unavailable reference frames
  5572. // if(ret==FRAME_SKIPED) return get_consumed_bytes(s, buf_index, buf_size);
  5573. if(!s->current_picture_ptr){
  5574. av_log(h->s.avctx, AV_LOG_DEBUG, "error, NO frame\n");
  5575. return -1;
  5576. }
  5577. {
  5578. //#define DECODE_ORDER
  5579. Picture *out = s->current_picture_ptr;
  5580. #ifndef DECODE_ORDER
  5581. /* Sort B-frames into display order
  5582. * FIXME doesn't allow for multiple delayed frames */
  5583. Picture *cur = s->current_picture_ptr;
  5584. Picture *prev = h->delayed_pic[0];
  5585. if(s->low_delay
  5586. && (cur->pict_type == B_TYPE
  5587. || (!h->sps.gaps_in_frame_num_allowed_flag
  5588. && prev && cur->poc - prev->poc > 2))){
  5589. s->low_delay = 0;
  5590. s->avctx->has_b_frames = 1;
  5591. if(prev && prev->poc > cur->poc)
  5592. // too late to display this frame
  5593. cur = prev;
  5594. }
  5595. if(s->low_delay || !prev || cur->pict_type == B_TYPE)
  5596. out = cur;
  5597. else
  5598. out = prev;
  5599. if(s->low_delay || !prev || out == prev){
  5600. if(prev && prev->reference == 1)
  5601. prev->reference = 0;
  5602. h->delayed_pic[0] = cur;
  5603. }
  5604. #endif
  5605. *pict= *(AVFrame*)out;
  5606. }
  5607. ff_print_debug_info(s, pict);
  5608. assert(pict->data[0]);
  5609. //printf("out %d\n", (int)pict->data[0]);
  5610. #if 0 //?
  5611. /* Return the Picture timestamp as the frame number */
  5612. /* we substract 1 because it is added on utils.c */
  5613. avctx->frame_number = s->picture_number - 1;
  5614. #endif
  5615. #if 0
  5616. /* dont output the last pic after seeking */
  5617. if(s->last_picture_ptr || s->low_delay)
  5618. //Note this isnt a issue as a IDR pic should flush the buffers
  5619. #endif
  5620. *data_size = sizeof(AVFrame);
  5621. return get_consumed_bytes(s, buf_index, buf_size);
  5622. }
  5623. #if 0
  5624. static inline void fill_mb_avail(H264Context *h){
  5625. MpegEncContext * const s = &h->s;
  5626. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  5627. if(s->mb_y){
  5628. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  5629. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  5630. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  5631. }else{
  5632. h->mb_avail[0]=
  5633. h->mb_avail[1]=
  5634. h->mb_avail[2]= 0;
  5635. }
  5636. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  5637. h->mb_avail[4]= 1; //FIXME move out
  5638. h->mb_avail[5]= 0; //FIXME move out
  5639. }
  5640. #endif
  5641. #if 0 //selftest
  5642. #define COUNT 8000
  5643. #define SIZE (COUNT*40)
  5644. int main(){
  5645. int i;
  5646. uint8_t temp[SIZE];
  5647. PutBitContext pb;
  5648. GetBitContext gb;
  5649. // int int_temp[10000];
  5650. DSPContext dsp;
  5651. AVCodecContext avctx;
  5652. dsputil_init(&dsp, &avctx);
  5653. init_put_bits(&pb, temp, SIZE);
  5654. printf("testing unsigned exp golomb\n");
  5655. for(i=0; i<COUNT; i++){
  5656. START_TIMER
  5657. set_ue_golomb(&pb, i);
  5658. STOP_TIMER("set_ue_golomb");
  5659. }
  5660. flush_put_bits(&pb);
  5661. init_get_bits(&gb, temp, 8*SIZE);
  5662. for(i=0; i<COUNT; i++){
  5663. int j, s;
  5664. s= show_bits(&gb, 24);
  5665. START_TIMER
  5666. j= get_ue_golomb(&gb);
  5667. if(j != i){
  5668. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  5669. // return -1;
  5670. }
  5671. STOP_TIMER("get_ue_golomb");
  5672. }
  5673. init_put_bits(&pb, temp, SIZE);
  5674. printf("testing signed exp golomb\n");
  5675. for(i=0; i<COUNT; i++){
  5676. START_TIMER
  5677. set_se_golomb(&pb, i - COUNT/2);
  5678. STOP_TIMER("set_se_golomb");
  5679. }
  5680. flush_put_bits(&pb);
  5681. init_get_bits(&gb, temp, 8*SIZE);
  5682. for(i=0; i<COUNT; i++){
  5683. int j, s;
  5684. s= show_bits(&gb, 24);
  5685. START_TIMER
  5686. j= get_se_golomb(&gb);
  5687. if(j != i - COUNT/2){
  5688. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  5689. // return -1;
  5690. }
  5691. STOP_TIMER("get_se_golomb");
  5692. }
  5693. printf("testing 4x4 (I)DCT\n");
  5694. DCTELEM block[16];
  5695. uint8_t src[16], ref[16];
  5696. uint64_t error= 0, max_error=0;
  5697. for(i=0; i<COUNT; i++){
  5698. int j;
  5699. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  5700. for(j=0; j<16; j++){
  5701. ref[j]= random()%255;
  5702. src[j]= random()%255;
  5703. }
  5704. h264_diff_dct_c(block, src, ref, 4);
  5705. //normalize
  5706. for(j=0; j<16; j++){
  5707. // printf("%d ", block[j]);
  5708. block[j]= block[j]*4;
  5709. if(j&1) block[j]= (block[j]*4 + 2)/5;
  5710. if(j&4) block[j]= (block[j]*4 + 2)/5;
  5711. }
  5712. // printf("\n");
  5713. s->dsp.h264_idct_add(ref, block, 4);
  5714. /* for(j=0; j<16; j++){
  5715. printf("%d ", ref[j]);
  5716. }
  5717. printf("\n");*/
  5718. for(j=0; j<16; j++){
  5719. int diff= ABS(src[j] - ref[j]);
  5720. error+= diff*diff;
  5721. max_error= FFMAX(max_error, diff);
  5722. }
  5723. }
  5724. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  5725. #if 0
  5726. printf("testing quantizer\n");
  5727. for(qp=0; qp<52; qp++){
  5728. for(i=0; i<16; i++)
  5729. src1_block[i]= src2_block[i]= random()%255;
  5730. }
  5731. #endif
  5732. printf("Testing NAL layer\n");
  5733. uint8_t bitstream[COUNT];
  5734. uint8_t nal[COUNT*2];
  5735. H264Context h;
  5736. memset(&h, 0, sizeof(H264Context));
  5737. for(i=0; i<COUNT; i++){
  5738. int zeros= i;
  5739. int nal_length;
  5740. int consumed;
  5741. int out_length;
  5742. uint8_t *out;
  5743. int j;
  5744. for(j=0; j<COUNT; j++){
  5745. bitstream[j]= (random() % 255) + 1;
  5746. }
  5747. for(j=0; j<zeros; j++){
  5748. int pos= random() % COUNT;
  5749. while(bitstream[pos] == 0){
  5750. pos++;
  5751. pos %= COUNT;
  5752. }
  5753. bitstream[pos]=0;
  5754. }
  5755. START_TIMER
  5756. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  5757. if(nal_length<0){
  5758. printf("encoding failed\n");
  5759. return -1;
  5760. }
  5761. out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
  5762. STOP_TIMER("NAL")
  5763. if(out_length != COUNT){
  5764. printf("incorrect length %d %d\n", out_length, COUNT);
  5765. return -1;
  5766. }
  5767. if(consumed != nal_length){
  5768. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  5769. return -1;
  5770. }
  5771. if(memcmp(bitstream, out, COUNT)){
  5772. printf("missmatch\n");
  5773. return -1;
  5774. }
  5775. }
  5776. printf("Testing RBSP\n");
  5777. return 0;
  5778. }
  5779. #endif
  5780. static int decode_end(AVCodecContext *avctx)
  5781. {
  5782. H264Context *h = avctx->priv_data;
  5783. MpegEncContext *s = &h->s;
  5784. free_tables(h); //FIXME cleanup init stuff perhaps
  5785. MPV_common_end(s);
  5786. // memset(h, 0, sizeof(H264Context));
  5787. return 0;
  5788. }
  5789. AVCodec h264_decoder = {
  5790. "h264",
  5791. CODEC_TYPE_VIDEO,
  5792. CODEC_ID_H264,
  5793. sizeof(H264Context),
  5794. decode_init,
  5795. NULL,
  5796. decode_end,
  5797. decode_frame,
  5798. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  5799. };
  5800. AVCodecParser h264_parser = {
  5801. { CODEC_ID_H264 },
  5802. sizeof(H264Context),
  5803. NULL,
  5804. h264_parse,
  5805. ff_parse_close,
  5806. };
  5807. #include "svq3.c"