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.

6744 lines
242KB

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