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.

7983 lines
298KB

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