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.

6317 lines
223KB

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