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.

6624 lines
237KB

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