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.

7942 lines
296KB

  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. return 0;
  2689. fail:
  2690. free_tables(h);
  2691. return -1;
  2692. }
  2693. static void common_init(H264Context *h){
  2694. MpegEncContext * const s = &h->s;
  2695. s->width = s->avctx->width;
  2696. s->height = s->avctx->height;
  2697. s->codec_id= s->avctx->codec->id;
  2698. init_pred_ptrs(h);
  2699. h->dequant_coeff_pps= -1;
  2700. s->unrestricted_mv=1;
  2701. s->decode=1; //FIXME
  2702. }
  2703. static int decode_init(AVCodecContext *avctx){
  2704. H264Context *h= avctx->priv_data;
  2705. MpegEncContext * const s = &h->s;
  2706. MPV_decode_defaults(s);
  2707. s->avctx = avctx;
  2708. common_init(h);
  2709. s->out_format = FMT_H264;
  2710. s->workaround_bugs= avctx->workaround_bugs;
  2711. // set defaults
  2712. // s->decode_mb= ff_h263_decode_mb;
  2713. s->low_delay= 1;
  2714. avctx->pix_fmt= PIX_FMT_YUV420P;
  2715. decode_init_vlc(h);
  2716. if(avctx->extradata_size > 0 && avctx->extradata &&
  2717. *(char *)avctx->extradata == 1){
  2718. h->is_avc = 1;
  2719. h->got_avcC = 0;
  2720. } else {
  2721. h->is_avc = 0;
  2722. }
  2723. return 0;
  2724. }
  2725. static void frame_start(H264Context *h){
  2726. MpegEncContext * const s = &h->s;
  2727. int i;
  2728. MPV_frame_start(s, s->avctx);
  2729. ff_er_frame_start(s);
  2730. assert(s->linesize && s->uvlinesize);
  2731. for(i=0; i<16; i++){
  2732. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  2733. h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  2734. }
  2735. for(i=0; i<4; i++){
  2736. h->block_offset[16+i]=
  2737. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2738. h->block_offset[24+16+i]=
  2739. h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2740. }
  2741. /* can't be in alloc_tables because linesize isn't known there.
  2742. * FIXME: redo bipred weight to not require extra buffer? */
  2743. if(!s->obmc_scratchpad)
  2744. s->obmc_scratchpad = av_malloc(16*s->linesize + 2*8*s->uvlinesize);
  2745. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  2746. }
  2747. static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  2748. MpegEncContext * const s = &h->s;
  2749. int i;
  2750. src_y -= linesize;
  2751. src_cb -= uvlinesize;
  2752. src_cr -= uvlinesize;
  2753. // There are two lines saved, the line above the the top macroblock of a pair,
  2754. // and the line above the bottom macroblock
  2755. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  2756. for(i=1; i<17; i++){
  2757. h->left_border[i]= src_y[15+i* linesize];
  2758. }
  2759. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
  2760. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
  2761. if(!(s->flags&CODEC_FLAG_GRAY)){
  2762. h->left_border[17 ]= h->top_borders[0][s->mb_x][16+7];
  2763. h->left_border[17+9]= h->top_borders[0][s->mb_x][24+7];
  2764. for(i=1; i<9; i++){
  2765. h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
  2766. h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
  2767. }
  2768. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
  2769. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
  2770. }
  2771. }
  2772. 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){
  2773. MpegEncContext * const s = &h->s;
  2774. int temp8, i;
  2775. uint64_t temp64;
  2776. int deblock_left = (s->mb_x > 0);
  2777. int deblock_top = (s->mb_y > 0);
  2778. src_y -= linesize + 1;
  2779. src_cb -= uvlinesize + 1;
  2780. src_cr -= uvlinesize + 1;
  2781. #define XCHG(a,b,t,xchg)\
  2782. t= a;\
  2783. if(xchg)\
  2784. a= b;\
  2785. b= t;
  2786. if(deblock_left){
  2787. for(i = !deblock_top; i<17; i++){
  2788. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  2789. }
  2790. }
  2791. if(deblock_top){
  2792. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  2793. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  2794. if(s->mb_x+1 < s->mb_width){
  2795. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  2796. }
  2797. }
  2798. if(!(s->flags&CODEC_FLAG_GRAY)){
  2799. if(deblock_left){
  2800. for(i = !deblock_top; i<9; i++){
  2801. XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
  2802. XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
  2803. }
  2804. }
  2805. if(deblock_top){
  2806. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  2807. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  2808. }
  2809. }
  2810. }
  2811. static inline void backup_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  2812. MpegEncContext * const s = &h->s;
  2813. int i;
  2814. src_y -= 2 * linesize;
  2815. src_cb -= 2 * uvlinesize;
  2816. src_cr -= 2 * uvlinesize;
  2817. // There are two lines saved, the line above the the top macroblock of a pair,
  2818. // and the line above the bottom macroblock
  2819. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  2820. h->left_border[1]= h->top_borders[1][s->mb_x][15];
  2821. for(i=2; i<34; i++){
  2822. h->left_border[i]= src_y[15+i* linesize];
  2823. }
  2824. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 32*linesize);
  2825. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+32*linesize);
  2826. *(uint64_t*)(h->top_borders[1][s->mb_x]+0)= *(uint64_t*)(src_y + 33*linesize);
  2827. *(uint64_t*)(h->top_borders[1][s->mb_x]+8)= *(uint64_t*)(src_y +8+33*linesize);
  2828. if(!(s->flags&CODEC_FLAG_GRAY)){
  2829. h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7];
  2830. h->left_border[34+ 1]= h->top_borders[1][s->mb_x][16+7];
  2831. h->left_border[34+18 ]= h->top_borders[0][s->mb_x][24+7];
  2832. h->left_border[34+18+1]= h->top_borders[1][s->mb_x][24+7];
  2833. for(i=2; i<18; i++){
  2834. h->left_border[i+34 ]= src_cb[7+i*uvlinesize];
  2835. h->left_border[i+34+18]= src_cr[7+i*uvlinesize];
  2836. }
  2837. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+16*uvlinesize);
  2838. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+16*uvlinesize);
  2839. *(uint64_t*)(h->top_borders[1][s->mb_x]+16)= *(uint64_t*)(src_cb+17*uvlinesize);
  2840. *(uint64_t*)(h->top_borders[1][s->mb_x]+24)= *(uint64_t*)(src_cr+17*uvlinesize);
  2841. }
  2842. }
  2843. 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){
  2844. MpegEncContext * const s = &h->s;
  2845. int temp8, i;
  2846. uint64_t temp64;
  2847. int deblock_left = (s->mb_x > 0);
  2848. int deblock_top = (s->mb_y > 0);
  2849. 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);
  2850. src_y -= 2 * linesize + 1;
  2851. src_cb -= 2 * uvlinesize + 1;
  2852. src_cr -= 2 * uvlinesize + 1;
  2853. #define XCHG(a,b,t,xchg)\
  2854. t= a;\
  2855. if(xchg)\
  2856. a= b;\
  2857. b= t;
  2858. if(deblock_left){
  2859. for(i = (!deblock_top)<<1; i<34; i++){
  2860. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  2861. }
  2862. }
  2863. if(deblock_top){
  2864. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  2865. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  2866. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+0), *(uint64_t*)(src_y +1 +linesize), temp64, xchg);
  2867. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+8), *(uint64_t*)(src_y +9 +linesize), temp64, 1);
  2868. }
  2869. if(!(s->flags&CODEC_FLAG_GRAY)){
  2870. if(deblock_left){
  2871. for(i = (!deblock_top) << 1; i<18; i++){
  2872. XCHG(h->left_border[i+34 ], src_cb[i*uvlinesize], temp8, xchg);
  2873. XCHG(h->left_border[i+34+18], src_cr[i*uvlinesize], temp8, xchg);
  2874. }
  2875. }
  2876. if(deblock_top){
  2877. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  2878. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  2879. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+16), *(uint64_t*)(src_cb+1 +uvlinesize), temp64, 1);
  2880. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+24), *(uint64_t*)(src_cr+1 +uvlinesize), temp64, 1);
  2881. }
  2882. }
  2883. }
  2884. static void hl_decode_mb(H264Context *h){
  2885. MpegEncContext * const s = &h->s;
  2886. const int mb_x= s->mb_x;
  2887. const int mb_y= s->mb_y;
  2888. const int mb_xy= mb_x + mb_y*s->mb_stride;
  2889. const int mb_type= s->current_picture.mb_type[mb_xy];
  2890. uint8_t *dest_y, *dest_cb, *dest_cr;
  2891. int linesize, uvlinesize /*dct_offset*/;
  2892. int i;
  2893. int *block_offset = &h->block_offset[0];
  2894. const unsigned int bottom = mb_y & 1;
  2895. const int transform_bypass = (s->qscale == 0 && h->sps.transform_bypass);
  2896. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  2897. if(!s->decode)
  2898. return;
  2899. dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  2900. dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  2901. dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  2902. if (h->mb_field_decoding_flag) {
  2903. linesize = s->linesize * 2;
  2904. uvlinesize = s->uvlinesize * 2;
  2905. block_offset = &h->block_offset[24];
  2906. if(mb_y&1){ //FIXME move out of this func?
  2907. dest_y -= s->linesize*15;
  2908. dest_cb-= s->uvlinesize*7;
  2909. dest_cr-= s->uvlinesize*7;
  2910. }
  2911. } else {
  2912. linesize = s->linesize;
  2913. uvlinesize = s->uvlinesize;
  2914. // dct_offset = s->linesize * 16;
  2915. }
  2916. idct_add = transform_bypass
  2917. ? IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4
  2918. : IS_8x8DCT(mb_type) ? s->dsp.h264_idct8_add : s->dsp.h264_idct_add;
  2919. if (IS_INTRA_PCM(mb_type)) {
  2920. unsigned int x, y;
  2921. // The pixels are stored in h->mb array in the same order as levels,
  2922. // copy them in output in the correct order.
  2923. for(i=0; i<16; i++) {
  2924. for (y=0; y<4; y++) {
  2925. for (x=0; x<4; x++) {
  2926. *(dest_y + block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x];
  2927. }
  2928. }
  2929. }
  2930. for(i=16; i<16+4; i++) {
  2931. for (y=0; y<4; y++) {
  2932. for (x=0; x<4; x++) {
  2933. *(dest_cb + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  2934. }
  2935. }
  2936. }
  2937. for(i=20; i<20+4; i++) {
  2938. for (y=0; y<4; y++) {
  2939. for (x=0; x<4; x++) {
  2940. *(dest_cr + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  2941. }
  2942. }
  2943. }
  2944. } else {
  2945. if(IS_INTRA(mb_type)){
  2946. if(h->deblocking_filter) {
  2947. if (h->mb_aff_frame) {
  2948. if (!bottom)
  2949. xchg_pair_border(h, dest_y, dest_cb, dest_cr, s->linesize, s->uvlinesize, 1);
  2950. } else {
  2951. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1);
  2952. }
  2953. }
  2954. if(!(s->flags&CODEC_FLAG_GRAY)){
  2955. h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  2956. h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  2957. }
  2958. if(IS_INTRA4x4(mb_type)){
  2959. if(!s->encoding){
  2960. if(IS_8x8DCT(mb_type)){
  2961. for(i=0; i<16; i+=4){
  2962. uint8_t * const ptr= dest_y + block_offset[i];
  2963. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  2964. h->pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  2965. (h->topright_samples_available<<(i+1))&0x8000, linesize);
  2966. if(h->non_zero_count_cache[ scan8[i] ])
  2967. idct_add(ptr, h->mb + i*16, linesize);
  2968. }
  2969. }else
  2970. for(i=0; i<16; i++){
  2971. uint8_t * const ptr= dest_y + block_offset[i];
  2972. uint8_t *topright;
  2973. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  2974. int tr;
  2975. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  2976. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  2977. assert(mb_y || linesize <= block_offset[i]);
  2978. if(!topright_avail){
  2979. tr= ptr[3 - linesize]*0x01010101;
  2980. topright= (uint8_t*) &tr;
  2981. }else
  2982. topright= ptr + 4 - linesize;
  2983. }else
  2984. topright= NULL;
  2985. h->pred4x4[ dir ](ptr, topright, linesize);
  2986. if(h->non_zero_count_cache[ scan8[i] ]){
  2987. if(s->codec_id == CODEC_ID_H264)
  2988. idct_add(ptr, h->mb + i*16, linesize);
  2989. else
  2990. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  2991. }
  2992. }
  2993. }
  2994. }else{
  2995. h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  2996. if(s->codec_id == CODEC_ID_H264){
  2997. if(!transform_bypass)
  2998. h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[IS_INTRA(mb_type) ? 0:3][s->qscale][0]);
  2999. }else
  3000. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  3001. }
  3002. if(h->deblocking_filter) {
  3003. if (h->mb_aff_frame) {
  3004. if (bottom) {
  3005. uint8_t *pair_dest_y = s->current_picture.data[0] + ((mb_y-1) * 16* s->linesize ) + mb_x * 16;
  3006. uint8_t *pair_dest_cb = s->current_picture.data[1] + ((mb_y-1) * 8 * s->uvlinesize) + mb_x * 8;
  3007. uint8_t *pair_dest_cr = s->current_picture.data[2] + ((mb_y-1) * 8 * s->uvlinesize) + mb_x * 8;
  3008. s->mb_y--;
  3009. xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0);
  3010. s->mb_y++;
  3011. }
  3012. } else {
  3013. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
  3014. }
  3015. }
  3016. }else if(s->codec_id == CODEC_ID_H264){
  3017. hl_motion(h, dest_y, dest_cb, dest_cr,
  3018. s->dsp.put_h264_qpel_pixels_tab, s->dsp.put_h264_chroma_pixels_tab,
  3019. s->dsp.avg_h264_qpel_pixels_tab, s->dsp.avg_h264_chroma_pixels_tab,
  3020. s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
  3021. }
  3022. if(!IS_INTRA4x4(mb_type)){
  3023. if(s->codec_id == CODEC_ID_H264){
  3024. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  3025. for(i=0; i<16; i+=di){
  3026. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  3027. uint8_t * const ptr= dest_y + block_offset[i];
  3028. idct_add(ptr, h->mb + i*16, linesize);
  3029. }
  3030. }
  3031. }else{
  3032. for(i=0; i<16; i++){
  3033. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  3034. uint8_t * const ptr= dest_y + block_offset[i];
  3035. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  3036. }
  3037. }
  3038. }
  3039. }
  3040. if(!(s->flags&CODEC_FLAG_GRAY)){
  3041. idct_add = transform_bypass ? s->dsp.add_pixels4 : s->dsp.h264_idct_add;
  3042. if(!transform_bypass){
  3043. 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]);
  3044. 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]);
  3045. }
  3046. if(s->codec_id == CODEC_ID_H264){
  3047. for(i=16; i<16+4; i++){
  3048. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  3049. uint8_t * const ptr= dest_cb + block_offset[i];
  3050. idct_add(ptr, h->mb + i*16, uvlinesize);
  3051. }
  3052. }
  3053. for(i=20; i<20+4; i++){
  3054. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  3055. uint8_t * const ptr= dest_cr + block_offset[i];
  3056. idct_add(ptr, h->mb + i*16, uvlinesize);
  3057. }
  3058. }
  3059. }else{
  3060. for(i=16; i<16+4; i++){
  3061. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  3062. uint8_t * const ptr= dest_cb + block_offset[i];
  3063. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  3064. }
  3065. }
  3066. for(i=20; i<20+4; i++){
  3067. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  3068. uint8_t * const ptr= dest_cr + block_offset[i];
  3069. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  3070. }
  3071. }
  3072. }
  3073. }
  3074. }
  3075. if(h->deblocking_filter) {
  3076. if (h->mb_aff_frame) {
  3077. const int mb_y = s->mb_y - 1;
  3078. uint8_t *pair_dest_y, *pair_dest_cb, *pair_dest_cr;
  3079. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3080. const int mb_type_top = s->current_picture.mb_type[mb_xy];
  3081. const int mb_type_bottom= s->current_picture.mb_type[mb_xy+s->mb_stride];
  3082. uint8_t tmp = s->current_picture.data[1][384];
  3083. if (!bottom) return;
  3084. pair_dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3085. pair_dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3086. pair_dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3087. backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize);
  3088. // TODO deblock a pair
  3089. // top
  3090. s->mb_y--;
  3091. 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);
  3092. fill_caches(h, mb_type_top, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3093. filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize);
  3094. if (tmp != s->current_picture.data[1][384]) {
  3095. tprintf("modified pixel 8,1 (1)\n");
  3096. }
  3097. // bottom
  3098. s->mb_y++;
  3099. tprintf("call mbaff filter_mb\n");
  3100. fill_caches(h, mb_type_bottom, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3101. filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3102. if (tmp != s->current_picture.data[1][384]) {
  3103. tprintf("modified pixel 8,1 (2)\n");
  3104. }
  3105. } else {
  3106. tprintf("call filter_mb\n");
  3107. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3108. fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3109. filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3110. }
  3111. }
  3112. }
  3113. /**
  3114. * fills the default_ref_list.
  3115. */
  3116. static int fill_default_ref_list(H264Context *h){
  3117. MpegEncContext * const s = &h->s;
  3118. int i;
  3119. int smallest_poc_greater_than_current = -1;
  3120. Picture sorted_short_ref[32];
  3121. if(h->slice_type==B_TYPE){
  3122. int out_i;
  3123. int limit= INT_MIN;
  3124. /* sort frame according to poc in B slice */
  3125. for(out_i=0; out_i<h->short_ref_count; out_i++){
  3126. int best_i=INT_MIN;
  3127. int best_poc=INT_MAX;
  3128. for(i=0; i<h->short_ref_count; i++){
  3129. const int poc= h->short_ref[i]->poc;
  3130. if(poc > limit && poc < best_poc){
  3131. best_poc= poc;
  3132. best_i= i;
  3133. }
  3134. }
  3135. assert(best_i != INT_MIN);
  3136. limit= best_poc;
  3137. sorted_short_ref[out_i]= *h->short_ref[best_i];
  3138. 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);
  3139. if (-1 == smallest_poc_greater_than_current) {
  3140. if (h->short_ref[best_i]->poc >= s->current_picture_ptr->poc) {
  3141. smallest_poc_greater_than_current = out_i;
  3142. }
  3143. }
  3144. }
  3145. }
  3146. if(s->picture_structure == PICT_FRAME){
  3147. if(h->slice_type==B_TYPE){
  3148. int list;
  3149. tprintf("current poc: %d, smallest_poc_greater_than_current: %d\n", s->current_picture_ptr->poc, smallest_poc_greater_than_current);
  3150. // find the largest poc
  3151. for(list=0; list<2; list++){
  3152. int index = 0;
  3153. int j= -99;
  3154. int step= list ? -1 : 1;
  3155. for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++, j+=step) {
  3156. while(j<0 || j>= h->short_ref_count){
  3157. if(j != -99 && step == (list ? -1 : 1))
  3158. return -1;
  3159. step = -step;
  3160. j= smallest_poc_greater_than_current + (step>>1);
  3161. }
  3162. if(sorted_short_ref[j].reference != 3) continue;
  3163. h->default_ref_list[list][index ]= sorted_short_ref[j];
  3164. h->default_ref_list[list][index++].pic_id= sorted_short_ref[j].frame_num;
  3165. }
  3166. for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){
  3167. if(h->long_ref[i] == NULL) continue;
  3168. if(h->long_ref[i]->reference != 3) continue;
  3169. h->default_ref_list[ list ][index ]= *h->long_ref[i];
  3170. h->default_ref_list[ list ][index++].pic_id= i;;
  3171. }
  3172. if(list && (smallest_poc_greater_than_current<=0 || smallest_poc_greater_than_current>=h->short_ref_count) && (1 < index)){
  3173. // swap the two first elements of L1 when
  3174. // L0 and L1 are identical
  3175. Picture temp= h->default_ref_list[1][0];
  3176. h->default_ref_list[1][0] = h->default_ref_list[1][1];
  3177. h->default_ref_list[1][1] = temp;
  3178. }
  3179. if(index < h->ref_count[ list ])
  3180. memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
  3181. }
  3182. }else{
  3183. int index=0;
  3184. for(i=0; i<h->short_ref_count; i++){
  3185. if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
  3186. h->default_ref_list[0][index ]= *h->short_ref[i];
  3187. h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
  3188. }
  3189. for(i = 0; i < 16; i++){
  3190. if(h->long_ref[i] == NULL) continue;
  3191. if(h->long_ref[i]->reference != 3) continue;
  3192. h->default_ref_list[0][index ]= *h->long_ref[i];
  3193. h->default_ref_list[0][index++].pic_id= i;;
  3194. }
  3195. if(index < h->ref_count[0])
  3196. memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
  3197. }
  3198. }else{ //FIELD
  3199. if(h->slice_type==B_TYPE){
  3200. }else{
  3201. //FIXME second field balh
  3202. }
  3203. }
  3204. #ifdef TRACE
  3205. for (i=0; i<h->ref_count[0]; i++) {
  3206. 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]);
  3207. }
  3208. if(h->slice_type==B_TYPE){
  3209. for (i=0; i<h->ref_count[1]; i++) {
  3210. 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]);
  3211. }
  3212. }
  3213. #endif
  3214. return 0;
  3215. }
  3216. static void print_short_term(H264Context *h);
  3217. static void print_long_term(H264Context *h);
  3218. static int decode_ref_pic_list_reordering(H264Context *h){
  3219. MpegEncContext * const s = &h->s;
  3220. int list, index;
  3221. print_short_term(h);
  3222. print_long_term(h);
  3223. if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move before func
  3224. for(list=0; list<2; list++){
  3225. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  3226. if(get_bits1(&s->gb)){
  3227. int pred= h->curr_pic_num;
  3228. for(index=0; ; index++){
  3229. int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
  3230. int pic_id;
  3231. int i;
  3232. Picture *ref = NULL;
  3233. if(reordering_of_pic_nums_idc==3)
  3234. break;
  3235. if(index >= h->ref_count[list]){
  3236. av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
  3237. return -1;
  3238. }
  3239. if(reordering_of_pic_nums_idc<3){
  3240. if(reordering_of_pic_nums_idc<2){
  3241. const int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  3242. if(abs_diff_pic_num >= h->max_pic_num){
  3243. av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  3244. return -1;
  3245. }
  3246. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  3247. else pred+= abs_diff_pic_num;
  3248. pred &= h->max_pic_num - 1;
  3249. for(i= h->short_ref_count-1; i>=0; i--){
  3250. ref = h->short_ref[i];
  3251. assert(ref->reference == 3);
  3252. assert(!ref->long_ref);
  3253. if(ref->data[0] != NULL && ref->frame_num == pred && ref->long_ref == 0) // ignore non existing pictures by testing data[0] pointer
  3254. break;
  3255. }
  3256. if(i>=0)
  3257. ref->pic_id= ref->frame_num;
  3258. }else{
  3259. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  3260. ref = h->long_ref[pic_id];
  3261. ref->pic_id= pic_id;
  3262. assert(ref->reference == 3);
  3263. assert(ref->long_ref);
  3264. i=0;
  3265. }
  3266. if (i < 0) {
  3267. av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  3268. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  3269. } else {
  3270. for(i=index; i+1<h->ref_count[list]; i++){
  3271. if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
  3272. break;
  3273. }
  3274. for(; i > index; i--){
  3275. h->ref_list[list][i]= h->ref_list[list][i-1];
  3276. }
  3277. h->ref_list[list][index]= *ref;
  3278. }
  3279. }else{
  3280. av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  3281. return -1;
  3282. }
  3283. }
  3284. }
  3285. if(h->slice_type!=B_TYPE) break;
  3286. }
  3287. for(list=0; list<2; list++){
  3288. for(index= 0; index < h->ref_count[list]; index++){
  3289. if(!h->ref_list[list][index].data[0])
  3290. h->ref_list[list][index]= s->current_picture;
  3291. }
  3292. if(h->slice_type!=B_TYPE) break;
  3293. }
  3294. if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)
  3295. direct_dist_scale_factor(h);
  3296. direct_ref_list_init(h);
  3297. return 0;
  3298. }
  3299. static int pred_weight_table(H264Context *h){
  3300. MpegEncContext * const s = &h->s;
  3301. int list, i;
  3302. int luma_def, chroma_def;
  3303. h->use_weight= 0;
  3304. h->use_weight_chroma= 0;
  3305. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  3306. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  3307. luma_def = 1<<h->luma_log2_weight_denom;
  3308. chroma_def = 1<<h->chroma_log2_weight_denom;
  3309. for(list=0; list<2; list++){
  3310. for(i=0; i<h->ref_count[list]; i++){
  3311. int luma_weight_flag, chroma_weight_flag;
  3312. luma_weight_flag= get_bits1(&s->gb);
  3313. if(luma_weight_flag){
  3314. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  3315. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  3316. if( h->luma_weight[list][i] != luma_def
  3317. || h->luma_offset[list][i] != 0)
  3318. h->use_weight= 1;
  3319. }else{
  3320. h->luma_weight[list][i]= luma_def;
  3321. h->luma_offset[list][i]= 0;
  3322. }
  3323. chroma_weight_flag= get_bits1(&s->gb);
  3324. if(chroma_weight_flag){
  3325. int j;
  3326. for(j=0; j<2; j++){
  3327. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  3328. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  3329. if( h->chroma_weight[list][i][j] != chroma_def
  3330. || h->chroma_offset[list][i][j] != 0)
  3331. h->use_weight_chroma= 1;
  3332. }
  3333. }else{
  3334. int j;
  3335. for(j=0; j<2; j++){
  3336. h->chroma_weight[list][i][j]= chroma_def;
  3337. h->chroma_offset[list][i][j]= 0;
  3338. }
  3339. }
  3340. }
  3341. if(h->slice_type != B_TYPE) break;
  3342. }
  3343. h->use_weight= h->use_weight || h->use_weight_chroma;
  3344. return 0;
  3345. }
  3346. static void implicit_weight_table(H264Context *h){
  3347. MpegEncContext * const s = &h->s;
  3348. int ref0, ref1;
  3349. int cur_poc = s->current_picture_ptr->poc;
  3350. if( h->ref_count[0] == 1 && h->ref_count[1] == 1
  3351. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  3352. h->use_weight= 0;
  3353. h->use_weight_chroma= 0;
  3354. return;
  3355. }
  3356. h->use_weight= 2;
  3357. h->use_weight_chroma= 2;
  3358. h->luma_log2_weight_denom= 5;
  3359. h->chroma_log2_weight_denom= 5;
  3360. /* FIXME: MBAFF */
  3361. for(ref0=0; ref0 < h->ref_count[0]; ref0++){
  3362. int poc0 = h->ref_list[0][ref0].poc;
  3363. for(ref1=0; ref1 < h->ref_count[1]; ref1++){
  3364. int poc1 = h->ref_list[1][ref1].poc;
  3365. int td = clip(poc1 - poc0, -128, 127);
  3366. if(td){
  3367. int tb = clip(cur_poc - poc0, -128, 127);
  3368. int tx = (16384 + (ABS(td) >> 1)) / td;
  3369. int dist_scale_factor = clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
  3370. if(dist_scale_factor < -64 || dist_scale_factor > 128)
  3371. h->implicit_weight[ref0][ref1] = 32;
  3372. else
  3373. h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
  3374. }else
  3375. h->implicit_weight[ref0][ref1] = 32;
  3376. }
  3377. }
  3378. }
  3379. static inline void unreference_pic(H264Context *h, Picture *pic){
  3380. int i;
  3381. pic->reference=0;
  3382. if(pic == h->delayed_output_pic)
  3383. pic->reference=1;
  3384. else{
  3385. for(i = 0; h->delayed_pic[i]; i++)
  3386. if(pic == h->delayed_pic[i]){
  3387. pic->reference=1;
  3388. break;
  3389. }
  3390. }
  3391. }
  3392. /**
  3393. * instantaneous decoder refresh.
  3394. */
  3395. static void idr(H264Context *h){
  3396. int i;
  3397. for(i=0; i<16; i++){
  3398. if (h->long_ref[i] != NULL) {
  3399. unreference_pic(h, h->long_ref[i]);
  3400. h->long_ref[i]= NULL;
  3401. }
  3402. }
  3403. h->long_ref_count=0;
  3404. for(i=0; i<h->short_ref_count; i++){
  3405. unreference_pic(h, h->short_ref[i]);
  3406. h->short_ref[i]= NULL;
  3407. }
  3408. h->short_ref_count=0;
  3409. }
  3410. /* forget old pics after a seek */
  3411. static void flush_dpb(AVCodecContext *avctx){
  3412. H264Context *h= avctx->priv_data;
  3413. int i;
  3414. for(i=0; i<16; i++)
  3415. h->delayed_pic[i]= NULL;
  3416. h->delayed_output_pic= NULL;
  3417. idr(h);
  3418. if(h->s.current_picture_ptr)
  3419. h->s.current_picture_ptr->reference= 0;
  3420. }
  3421. /**
  3422. *
  3423. * @return the removed picture or NULL if an error occurs
  3424. */
  3425. static Picture * remove_short(H264Context *h, int frame_num){
  3426. MpegEncContext * const s = &h->s;
  3427. int i;
  3428. if(s->avctx->debug&FF_DEBUG_MMCO)
  3429. av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  3430. for(i=0; i<h->short_ref_count; i++){
  3431. Picture *pic= h->short_ref[i];
  3432. if(s->avctx->debug&FF_DEBUG_MMCO)
  3433. av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  3434. if(pic->frame_num == frame_num){
  3435. h->short_ref[i]= NULL;
  3436. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
  3437. h->short_ref_count--;
  3438. return pic;
  3439. }
  3440. }
  3441. return NULL;
  3442. }
  3443. /**
  3444. *
  3445. * @return the removed picture or NULL if an error occurs
  3446. */
  3447. static Picture * remove_long(H264Context *h, int i){
  3448. Picture *pic;
  3449. pic= h->long_ref[i];
  3450. h->long_ref[i]= NULL;
  3451. if(pic) h->long_ref_count--;
  3452. return pic;
  3453. }
  3454. /**
  3455. * print short term list
  3456. */
  3457. static void print_short_term(H264Context *h) {
  3458. uint32_t i;
  3459. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3460. av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
  3461. for(i=0; i<h->short_ref_count; i++){
  3462. Picture *pic= h->short_ref[i];
  3463. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3464. }
  3465. }
  3466. }
  3467. /**
  3468. * print long term list
  3469. */
  3470. static void print_long_term(H264Context *h) {
  3471. uint32_t i;
  3472. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3473. av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
  3474. for(i = 0; i < 16; i++){
  3475. Picture *pic= h->long_ref[i];
  3476. if (pic) {
  3477. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3478. }
  3479. }
  3480. }
  3481. }
  3482. /**
  3483. * Executes the reference picture marking (memory management control operations).
  3484. */
  3485. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  3486. MpegEncContext * const s = &h->s;
  3487. int i, j;
  3488. int current_is_long=0;
  3489. Picture *pic;
  3490. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  3491. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  3492. for(i=0; i<mmco_count; i++){
  3493. if(s->avctx->debug&FF_DEBUG_MMCO)
  3494. 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);
  3495. switch(mmco[i].opcode){
  3496. case MMCO_SHORT2UNUSED:
  3497. pic= remove_short(h, mmco[i].short_frame_num);
  3498. if(pic)
  3499. unreference_pic(h, pic);
  3500. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3501. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_short() failure\n");
  3502. break;
  3503. case MMCO_SHORT2LONG:
  3504. pic= remove_long(h, mmco[i].long_index);
  3505. if(pic) unreference_pic(h, pic);
  3506. h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
  3507. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3508. h->long_ref_count++;
  3509. break;
  3510. case MMCO_LONG2UNUSED:
  3511. pic= remove_long(h, mmco[i].long_index);
  3512. if(pic)
  3513. unreference_pic(h, pic);
  3514. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3515. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_long() failure\n");
  3516. break;
  3517. case MMCO_LONG:
  3518. pic= remove_long(h, mmco[i].long_index);
  3519. if(pic) unreference_pic(h, pic);
  3520. h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
  3521. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3522. h->long_ref_count++;
  3523. current_is_long=1;
  3524. break;
  3525. case MMCO_SET_MAX_LONG:
  3526. assert(mmco[i].long_index <= 16);
  3527. // just remove the long term which index is greater than new max
  3528. for(j = mmco[i].long_index; j<16; j++){
  3529. pic = remove_long(h, j);
  3530. if (pic) unreference_pic(h, pic);
  3531. }
  3532. break;
  3533. case MMCO_RESET:
  3534. while(h->short_ref_count){
  3535. pic= remove_short(h, h->short_ref[0]->frame_num);
  3536. unreference_pic(h, pic);
  3537. }
  3538. for(j = 0; j < 16; j++) {
  3539. pic= remove_long(h, j);
  3540. if(pic) unreference_pic(h, pic);
  3541. }
  3542. break;
  3543. default: assert(0);
  3544. }
  3545. }
  3546. if(!current_is_long){
  3547. pic= remove_short(h, s->current_picture_ptr->frame_num);
  3548. if(pic){
  3549. unreference_pic(h, pic);
  3550. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  3551. }
  3552. if(h->short_ref_count)
  3553. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  3554. h->short_ref[0]= s->current_picture_ptr;
  3555. h->short_ref[0]->long_ref=0;
  3556. h->short_ref_count++;
  3557. }
  3558. print_short_term(h);
  3559. print_long_term(h);
  3560. return 0;
  3561. }
  3562. static int decode_ref_pic_marking(H264Context *h){
  3563. MpegEncContext * const s = &h->s;
  3564. int i;
  3565. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  3566. s->broken_link= get_bits1(&s->gb) -1;
  3567. h->mmco[0].long_index= get_bits1(&s->gb) - 1; // current_long_term_idx
  3568. if(h->mmco[0].long_index == -1)
  3569. h->mmco_index= 0;
  3570. else{
  3571. h->mmco[0].opcode= MMCO_LONG;
  3572. h->mmco_index= 1;
  3573. }
  3574. }else{
  3575. if(get_bits1(&s->gb)){ // adaptive_ref_pic_marking_mode_flag
  3576. for(i= 0; i<MAX_MMCO_COUNT; i++) {
  3577. MMCOOpcode opcode= get_ue_golomb(&s->gb);;
  3578. h->mmco[i].opcode= opcode;
  3579. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  3580. 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
  3581. /* if(h->mmco[i].short_frame_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_frame_num ] == NULL){
  3582. av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
  3583. return -1;
  3584. }*/
  3585. }
  3586. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  3587. h->mmco[i].long_index= get_ue_golomb(&s->gb);
  3588. 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){
  3589. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  3590. return -1;
  3591. }
  3592. }
  3593. if(opcode > MMCO_LONG){
  3594. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  3595. return -1;
  3596. }
  3597. if(opcode == MMCO_END)
  3598. break;
  3599. }
  3600. h->mmco_index= i;
  3601. }else{
  3602. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  3603. if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){ //FIXME fields
  3604. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  3605. h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  3606. h->mmco_index= 1;
  3607. }else
  3608. h->mmco_index= 0;
  3609. }
  3610. }
  3611. return 0;
  3612. }
  3613. static int init_poc(H264Context *h){
  3614. MpegEncContext * const s = &h->s;
  3615. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  3616. int field_poc[2];
  3617. if(h->nal_unit_type == NAL_IDR_SLICE){
  3618. h->frame_num_offset= 0;
  3619. }else{
  3620. if(h->frame_num < h->prev_frame_num)
  3621. h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
  3622. else
  3623. h->frame_num_offset= h->prev_frame_num_offset;
  3624. }
  3625. if(h->sps.poc_type==0){
  3626. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  3627. if(h->nal_unit_type == NAL_IDR_SLICE){
  3628. h->prev_poc_msb=
  3629. h->prev_poc_lsb= 0;
  3630. }
  3631. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  3632. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  3633. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  3634. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  3635. else
  3636. h->poc_msb = h->prev_poc_msb;
  3637. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  3638. field_poc[0] =
  3639. field_poc[1] = h->poc_msb + h->poc_lsb;
  3640. if(s->picture_structure == PICT_FRAME)
  3641. field_poc[1] += h->delta_poc_bottom;
  3642. }else if(h->sps.poc_type==1){
  3643. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  3644. int i;
  3645. if(h->sps.poc_cycle_length != 0)
  3646. abs_frame_num = h->frame_num_offset + h->frame_num;
  3647. else
  3648. abs_frame_num = 0;
  3649. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  3650. abs_frame_num--;
  3651. expected_delta_per_poc_cycle = 0;
  3652. for(i=0; i < h->sps.poc_cycle_length; i++)
  3653. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  3654. if(abs_frame_num > 0){
  3655. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  3656. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  3657. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  3658. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  3659. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  3660. } else
  3661. expectedpoc = 0;
  3662. if(h->nal_ref_idc == 0)
  3663. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  3664. field_poc[0] = expectedpoc + h->delta_poc[0];
  3665. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  3666. if(s->picture_structure == PICT_FRAME)
  3667. field_poc[1] += h->delta_poc[1];
  3668. }else{
  3669. int poc;
  3670. if(h->nal_unit_type == NAL_IDR_SLICE){
  3671. poc= 0;
  3672. }else{
  3673. if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
  3674. else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
  3675. }
  3676. field_poc[0]= poc;
  3677. field_poc[1]= poc;
  3678. }
  3679. if(s->picture_structure != PICT_BOTTOM_FIELD)
  3680. s->current_picture_ptr->field_poc[0]= field_poc[0];
  3681. if(s->picture_structure != PICT_TOP_FIELD)
  3682. s->current_picture_ptr->field_poc[1]= field_poc[1];
  3683. if(s->picture_structure == PICT_FRAME) // FIXME field pix?
  3684. s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]);
  3685. return 0;
  3686. }
  3687. /**
  3688. * decodes a slice header.
  3689. * this will allso call MPV_common_init() and frame_start() as needed
  3690. */
  3691. static int decode_slice_header(H264Context *h){
  3692. MpegEncContext * const s = &h->s;
  3693. int first_mb_in_slice, pps_id;
  3694. int num_ref_idx_active_override_flag;
  3695. static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
  3696. int slice_type;
  3697. int default_ref_list_done = 0;
  3698. s->current_picture.reference= h->nal_ref_idc != 0;
  3699. s->dropable= h->nal_ref_idc == 0;
  3700. first_mb_in_slice= get_ue_golomb(&s->gb);
  3701. slice_type= get_ue_golomb(&s->gb);
  3702. if(slice_type > 9){
  3703. 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);
  3704. return -1;
  3705. }
  3706. if(slice_type > 4){
  3707. slice_type -= 5;
  3708. h->slice_type_fixed=1;
  3709. }else
  3710. h->slice_type_fixed=0;
  3711. slice_type= slice_type_map[ slice_type ];
  3712. if (slice_type == I_TYPE
  3713. || (h->slice_num != 0 && slice_type == h->slice_type) ) {
  3714. default_ref_list_done = 1;
  3715. }
  3716. h->slice_type= slice_type;
  3717. s->pict_type= h->slice_type; // to make a few old func happy, it's wrong though
  3718. pps_id= get_ue_golomb(&s->gb);
  3719. if(pps_id>255){
  3720. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  3721. return -1;
  3722. }
  3723. h->pps= h->pps_buffer[pps_id];
  3724. if(h->pps.slice_group_count == 0){
  3725. av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referenced\n");
  3726. return -1;
  3727. }
  3728. h->sps= h->sps_buffer[ h->pps.sps_id ];
  3729. if(h->sps.log2_max_frame_num == 0){
  3730. av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referenced\n");
  3731. return -1;
  3732. }
  3733. if(h->dequant_coeff_pps != pps_id){
  3734. h->dequant_coeff_pps = pps_id;
  3735. init_dequant_tables(h);
  3736. }
  3737. s->mb_width= h->sps.mb_width;
  3738. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  3739. h->b_stride= s->mb_width*4 + 1;
  3740. h->b8_stride= s->mb_width*2 + 1;
  3741. s->width = 16*s->mb_width - 2*(h->sps.crop_left + h->sps.crop_right );
  3742. if(h->sps.frame_mbs_only_flag)
  3743. s->height= 16*s->mb_height - 2*(h->sps.crop_top + h->sps.crop_bottom);
  3744. else
  3745. s->height= 16*s->mb_height - 4*(h->sps.crop_top + h->sps.crop_bottom); //FIXME recheck
  3746. if (s->context_initialized
  3747. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  3748. free_tables(h);
  3749. MPV_common_end(s);
  3750. }
  3751. if (!s->context_initialized) {
  3752. if (MPV_common_init(s) < 0)
  3753. return -1;
  3754. if(s->dsp.h264_idct_add == ff_h264_idct_add_c){ //FIXME little ugly
  3755. memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
  3756. memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
  3757. }else{
  3758. int i;
  3759. for(i=0; i<16; i++){
  3760. #define T(x) (x>>2) | ((x<<2) & 0xF)
  3761. h->zigzag_scan[i] = T(zigzag_scan[i]);
  3762. h-> field_scan[i] = T( field_scan[i]);
  3763. }
  3764. }
  3765. if(h->sps.transform_bypass){ //FIXME same ugly
  3766. h->zigzag_scan_q0 = zigzag_scan;
  3767. h->field_scan_q0 = field_scan;
  3768. }else{
  3769. h->zigzag_scan_q0 = h->zigzag_scan;
  3770. h->field_scan_q0 = h->field_scan;
  3771. }
  3772. alloc_tables(h);
  3773. s->avctx->width = s->width;
  3774. s->avctx->height = s->height;
  3775. s->avctx->sample_aspect_ratio= h->sps.sar;
  3776. if(!s->avctx->sample_aspect_ratio.den)
  3777. s->avctx->sample_aspect_ratio.den = 1;
  3778. if(h->sps.timing_info_present_flag){
  3779. s->avctx->time_base= (AVRational){h->sps.num_units_in_tick, h->sps.time_scale};
  3780. }
  3781. }
  3782. if(h->slice_num == 0){
  3783. frame_start(h);
  3784. }
  3785. s->current_picture_ptr->frame_num= //FIXME frame_num cleanup
  3786. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  3787. h->mb_aff_frame = 0;
  3788. if(h->sps.frame_mbs_only_flag){
  3789. s->picture_structure= PICT_FRAME;
  3790. }else{
  3791. if(get_bits1(&s->gb)) { //field_pic_flag
  3792. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  3793. } else {
  3794. s->picture_structure= PICT_FRAME;
  3795. first_mb_in_slice <<= h->sps.mb_aff;
  3796. h->mb_aff_frame = h->sps.mb_aff;
  3797. }
  3798. }
  3799. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  3800. s->resync_mb_y = s->mb_y = first_mb_in_slice / s->mb_width;
  3801. if(s->mb_y >= s->mb_height){
  3802. return -1;
  3803. }
  3804. if(s->picture_structure==PICT_FRAME){
  3805. h->curr_pic_num= h->frame_num;
  3806. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  3807. }else{
  3808. h->curr_pic_num= 2*h->frame_num;
  3809. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  3810. }
  3811. if(h->nal_unit_type == NAL_IDR_SLICE){
  3812. get_ue_golomb(&s->gb); /* idr_pic_id */
  3813. }
  3814. if(h->sps.poc_type==0){
  3815. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  3816. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  3817. h->delta_poc_bottom= get_se_golomb(&s->gb);
  3818. }
  3819. }
  3820. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  3821. h->delta_poc[0]= get_se_golomb(&s->gb);
  3822. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  3823. h->delta_poc[1]= get_se_golomb(&s->gb);
  3824. }
  3825. init_poc(h);
  3826. if(h->pps.redundant_pic_cnt_present){
  3827. h->redundant_pic_count= get_ue_golomb(&s->gb);
  3828. }
  3829. //set defaults, might be overriden a few line later
  3830. h->ref_count[0]= h->pps.ref_count[0];
  3831. h->ref_count[1]= h->pps.ref_count[1];
  3832. if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){
  3833. if(h->slice_type == B_TYPE){
  3834. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  3835. }
  3836. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  3837. if(num_ref_idx_active_override_flag){
  3838. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  3839. if(h->slice_type==B_TYPE)
  3840. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  3841. if(h->ref_count[0] > 32 || h->ref_count[1] > 32){
  3842. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  3843. return -1;
  3844. }
  3845. }
  3846. }
  3847. if(!default_ref_list_done){
  3848. fill_default_ref_list(h);
  3849. }
  3850. if(decode_ref_pic_list_reordering(h) < 0)
  3851. return -1;
  3852. if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
  3853. || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
  3854. pred_weight_table(h);
  3855. else if(h->pps.weighted_bipred_idc==2 && h->slice_type==B_TYPE)
  3856. implicit_weight_table(h);
  3857. else
  3858. h->use_weight = 0;
  3859. if(s->current_picture.reference)
  3860. decode_ref_pic_marking(h);
  3861. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac )
  3862. h->cabac_init_idc = get_ue_golomb(&s->gb);
  3863. h->last_qscale_diff = 0;
  3864. s->qscale = h->pps.init_qp + get_se_golomb(&s->gb);
  3865. if(s->qscale<0 || s->qscale>51){
  3866. av_log(s->avctx, AV_LOG_ERROR, "QP %d out of range\n", s->qscale);
  3867. return -1;
  3868. }
  3869. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  3870. //FIXME qscale / qp ... stuff
  3871. if(h->slice_type == SP_TYPE){
  3872. get_bits1(&s->gb); /* sp_for_switch_flag */
  3873. }
  3874. if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
  3875. get_se_golomb(&s->gb); /* slice_qs_delta */
  3876. }
  3877. h->deblocking_filter = 1;
  3878. h->slice_alpha_c0_offset = 0;
  3879. h->slice_beta_offset = 0;
  3880. if( h->pps.deblocking_filter_parameters_present ) {
  3881. h->deblocking_filter= get_ue_golomb(&s->gb);
  3882. if(h->deblocking_filter < 2)
  3883. h->deblocking_filter^= 1; // 1<->0
  3884. if( h->deblocking_filter ) {
  3885. h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
  3886. h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
  3887. }
  3888. }
  3889. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  3890. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type != I_TYPE)
  3891. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type == B_TYPE)
  3892. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  3893. h->deblocking_filter= 0;
  3894. #if 0 //FMO
  3895. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  3896. slice_group_change_cycle= get_bits(&s->gb, ?);
  3897. #endif
  3898. h->slice_num++;
  3899. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  3900. 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",
  3901. h->slice_num,
  3902. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  3903. first_mb_in_slice,
  3904. av_get_pict_type_char(h->slice_type),
  3905. pps_id, h->frame_num,
  3906. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  3907. h->ref_count[0], h->ref_count[1],
  3908. s->qscale,
  3909. h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2,
  3910. h->use_weight,
  3911. h->use_weight==1 && h->use_weight_chroma ? "c" : ""
  3912. );
  3913. }
  3914. return 0;
  3915. }
  3916. /**
  3917. *
  3918. */
  3919. static inline int get_level_prefix(GetBitContext *gb){
  3920. unsigned int buf;
  3921. int log;
  3922. OPEN_READER(re, gb);
  3923. UPDATE_CACHE(re, gb);
  3924. buf=GET_CACHE(re, gb);
  3925. log= 32 - av_log2(buf);
  3926. #ifdef TRACE
  3927. print_bin(buf>>(32-log), log);
  3928. 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__);
  3929. #endif
  3930. LAST_SKIP_BITS(re, gb, log);
  3931. CLOSE_READER(re, gb);
  3932. return log-1;
  3933. }
  3934. static inline int get_dct8x8_allowed(H264Context *h){
  3935. int i;
  3936. for(i=0; i<4; i++){
  3937. if(!IS_SUB_8X8(h->sub_mb_type[i])
  3938. || (!h->sps.direct_8x8_inference_flag && IS_DIRECT(h->sub_mb_type[i])))
  3939. return 0;
  3940. }
  3941. return 1;
  3942. }
  3943. /**
  3944. * decodes a residual block.
  3945. * @param n block index
  3946. * @param scantable scantable
  3947. * @param max_coeff number of coefficients in the block
  3948. * @return <0 if an error occured
  3949. */
  3950. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
  3951. MpegEncContext * const s = &h->s;
  3952. 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};
  3953. int level[16];
  3954. int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
  3955. //FIXME put trailing_onex into the context
  3956. if(n == CHROMA_DC_BLOCK_INDEX){
  3957. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  3958. total_coeff= coeff_token>>2;
  3959. }else{
  3960. if(n == LUMA_DC_BLOCK_INDEX){
  3961. total_coeff= pred_non_zero_count(h, 0);
  3962. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  3963. total_coeff= coeff_token>>2;
  3964. }else{
  3965. total_coeff= pred_non_zero_count(h, n);
  3966. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  3967. total_coeff= coeff_token>>2;
  3968. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  3969. }
  3970. }
  3971. //FIXME set last_non_zero?
  3972. if(total_coeff==0)
  3973. return 0;
  3974. trailing_ones= coeff_token&3;
  3975. tprintf("trailing:%d, total:%d\n", trailing_ones, total_coeff);
  3976. assert(total_coeff<=16);
  3977. for(i=0; i<trailing_ones; i++){
  3978. level[i]= 1 - 2*get_bits1(gb);
  3979. }
  3980. if(i<total_coeff) {
  3981. int level_code, mask;
  3982. int suffix_length = total_coeff > 10 && trailing_ones < 3;
  3983. int prefix= get_level_prefix(gb);
  3984. //first coefficient has suffix_length equal to 0 or 1
  3985. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  3986. if(suffix_length)
  3987. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  3988. else
  3989. level_code= (prefix<<suffix_length); //part
  3990. }else if(prefix==14){
  3991. if(suffix_length)
  3992. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  3993. else
  3994. level_code= prefix + get_bits(gb, 4); //part
  3995. }else if(prefix==15){
  3996. level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part
  3997. if(suffix_length==0) level_code+=15; //FIXME doesn't make (much)sense
  3998. }else{
  3999. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4000. return -1;
  4001. }
  4002. if(trailing_ones < 3) level_code += 2;
  4003. suffix_length = 1;
  4004. if(level_code > 5)
  4005. suffix_length++;
  4006. mask= -(level_code&1);
  4007. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4008. i++;
  4009. //remaining coefficients have suffix_length > 0
  4010. for(;i<total_coeff;i++) {
  4011. static const int suffix_limit[7] = {0,5,11,23,47,95,INT_MAX };
  4012. prefix = get_level_prefix(gb);
  4013. if(prefix<15){
  4014. level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
  4015. }else if(prefix==15){
  4016. level_code = (prefix<<suffix_length) + get_bits(gb, 12);
  4017. }else{
  4018. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4019. return -1;
  4020. }
  4021. mask= -(level_code&1);
  4022. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4023. if(level_code > suffix_limit[suffix_length])
  4024. suffix_length++;
  4025. }
  4026. }
  4027. if(total_coeff == max_coeff)
  4028. zeros_left=0;
  4029. else{
  4030. if(n == CHROMA_DC_BLOCK_INDEX)
  4031. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  4032. else
  4033. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  4034. }
  4035. coeff_num = zeros_left + total_coeff - 1;
  4036. j = scantable[coeff_num];
  4037. if(n > 24){
  4038. block[j] = level[0];
  4039. for(i=1;i<total_coeff;i++) {
  4040. if(zeros_left <= 0)
  4041. run_before = 0;
  4042. else if(zeros_left < 7){
  4043. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4044. }else{
  4045. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4046. }
  4047. zeros_left -= run_before;
  4048. coeff_num -= 1 + run_before;
  4049. j= scantable[ coeff_num ];
  4050. block[j]= level[i];
  4051. }
  4052. }else{
  4053. block[j] = (level[0] * qmul[j] + 32)>>6;
  4054. for(i=1;i<total_coeff;i++) {
  4055. if(zeros_left <= 0)
  4056. run_before = 0;
  4057. else if(zeros_left < 7){
  4058. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4059. }else{
  4060. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4061. }
  4062. zeros_left -= run_before;
  4063. coeff_num -= 1 + run_before;
  4064. j= scantable[ coeff_num ];
  4065. block[j]= (level[i] * qmul[j] + 32)>>6;
  4066. }
  4067. }
  4068. if(zeros_left<0){
  4069. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  4070. return -1;
  4071. }
  4072. return 0;
  4073. }
  4074. /**
  4075. * decodes a P_SKIP or B_SKIP macroblock
  4076. */
  4077. static void decode_mb_skip(H264Context *h){
  4078. MpegEncContext * const s = &h->s;
  4079. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4080. int mb_type=0;
  4081. memset(h->non_zero_count[mb_xy], 0, 16);
  4082. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  4083. if(h->mb_aff_frame && s->mb_skip_run==0 && (s->mb_y&1)==0){
  4084. h->mb_field_decoding_flag= get_bits1(&s->gb);
  4085. }
  4086. if(h->mb_field_decoding_flag)
  4087. mb_type|= MB_TYPE_INTERLACED;
  4088. if( h->slice_type == B_TYPE )
  4089. {
  4090. // just for fill_caches. pred_direct_motion will set the real mb_type
  4091. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
  4092. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4093. pred_direct_motion(h, &mb_type);
  4094. if(h->pps.cabac){
  4095. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  4096. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  4097. }
  4098. }
  4099. else
  4100. {
  4101. int mx, my;
  4102. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
  4103. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4104. pred_pskip_motion(h, &mx, &my);
  4105. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  4106. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  4107. if(h->pps.cabac)
  4108. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  4109. }
  4110. write_back_motion(h, mb_type);
  4111. s->current_picture.mb_type[mb_xy]= mb_type|MB_TYPE_SKIP;
  4112. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4113. h->slice_table[ mb_xy ]= h->slice_num;
  4114. h->prev_mb_skipped= 1;
  4115. }
  4116. /**
  4117. * decodes a macroblock
  4118. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  4119. */
  4120. static int decode_mb_cavlc(H264Context *h){
  4121. MpegEncContext * const s = &h->s;
  4122. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4123. int mb_type, partition_count, cbp;
  4124. int dct8x8_allowed= h->pps.transform_8x8_mode;
  4125. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?
  4126. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  4127. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  4128. down the code */
  4129. if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){
  4130. if(s->mb_skip_run==-1)
  4131. s->mb_skip_run= get_ue_golomb(&s->gb);
  4132. if (s->mb_skip_run--) {
  4133. decode_mb_skip(h);
  4134. return 0;
  4135. }
  4136. }
  4137. if(h->mb_aff_frame){
  4138. if ( ((s->mb_y&1) == 0) || h->prev_mb_skipped)
  4139. h->mb_field_decoding_flag = get_bits1(&s->gb);
  4140. }else
  4141. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  4142. h->prev_mb_skipped= 0;
  4143. mb_type= get_ue_golomb(&s->gb);
  4144. if(h->slice_type == B_TYPE){
  4145. if(mb_type < 23){
  4146. partition_count= b_mb_type_info[mb_type].partition_count;
  4147. mb_type= b_mb_type_info[mb_type].type;
  4148. }else{
  4149. mb_type -= 23;
  4150. goto decode_intra_mb;
  4151. }
  4152. }else if(h->slice_type == P_TYPE /*|| h->slice_type == SP_TYPE */){
  4153. if(mb_type < 5){
  4154. partition_count= p_mb_type_info[mb_type].partition_count;
  4155. mb_type= p_mb_type_info[mb_type].type;
  4156. }else{
  4157. mb_type -= 5;
  4158. goto decode_intra_mb;
  4159. }
  4160. }else{
  4161. assert(h->slice_type == I_TYPE);
  4162. decode_intra_mb:
  4163. if(mb_type > 25){
  4164. 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);
  4165. return -1;
  4166. }
  4167. partition_count=0;
  4168. cbp= i_mb_type_info[mb_type].cbp;
  4169. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  4170. mb_type= i_mb_type_info[mb_type].type;
  4171. }
  4172. if(h->mb_field_decoding_flag)
  4173. mb_type |= MB_TYPE_INTERLACED;
  4174. h->slice_table[ mb_xy ]= h->slice_num;
  4175. if(IS_INTRA_PCM(mb_type)){
  4176. unsigned int x, y;
  4177. // we assume these blocks are very rare so we dont optimize it
  4178. align_get_bits(&s->gb);
  4179. // The pixels are stored in the same order as levels in h->mb array.
  4180. for(y=0; y<16; y++){
  4181. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  4182. for(x=0; x<16; x++){
  4183. tprintf("LUMA ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4184. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= get_bits(&s->gb, 8);
  4185. }
  4186. }
  4187. for(y=0; y<8; y++){
  4188. const int index= 256 + 4*(y&3) + 32*(y>>2);
  4189. for(x=0; x<8; x++){
  4190. tprintf("CHROMA U ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4191. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4192. }
  4193. }
  4194. for(y=0; y<8; y++){
  4195. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  4196. for(x=0; x<8; x++){
  4197. tprintf("CHROMA V 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. // In deblocking, the quantizer is 0
  4202. s->current_picture.qscale_table[mb_xy]= 0;
  4203. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  4204. // All coeffs are present
  4205. memset(h->non_zero_count[mb_xy], 16, 16);
  4206. s->current_picture.mb_type[mb_xy]= mb_type;
  4207. return 0;
  4208. }
  4209. fill_caches(h, mb_type, 0);
  4210. //mb_pred
  4211. if(IS_INTRA(mb_type)){
  4212. // init_top_left_availability(h);
  4213. if(IS_INTRA4x4(mb_type)){
  4214. int i;
  4215. int di = 1;
  4216. if(dct8x8_allowed && get_bits1(&s->gb)){
  4217. mb_type |= MB_TYPE_8x8DCT;
  4218. di = 4;
  4219. }
  4220. // fill_intra4x4_pred_table(h);
  4221. for(i=0; i<16; i+=di){
  4222. const int mode_coded= !get_bits1(&s->gb);
  4223. const int predicted_mode= pred_intra_mode(h, i);
  4224. int mode;
  4225. if(mode_coded){
  4226. const int rem_mode= get_bits(&s->gb, 3);
  4227. if(rem_mode<predicted_mode)
  4228. mode= rem_mode;
  4229. else
  4230. mode= rem_mode + 1;
  4231. }else{
  4232. mode= predicted_mode;
  4233. }
  4234. if(di==4)
  4235. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  4236. else
  4237. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  4238. }
  4239. write_back_intra_pred_mode(h);
  4240. if( check_intra4x4_pred_mode(h) < 0)
  4241. return -1;
  4242. }else{
  4243. h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
  4244. if(h->intra16x16_pred_mode < 0)
  4245. return -1;
  4246. }
  4247. h->chroma_pred_mode= get_ue_golomb(&s->gb);
  4248. h->chroma_pred_mode= check_intra_pred_mode(h, h->chroma_pred_mode);
  4249. if(h->chroma_pred_mode < 0)
  4250. return -1;
  4251. }else if(partition_count==4){
  4252. int i, j, sub_partition_count[4], list, ref[2][4];
  4253. if(h->slice_type == B_TYPE){
  4254. for(i=0; i<4; i++){
  4255. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4256. if(h->sub_mb_type[i] >=13){
  4257. 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);
  4258. return -1;
  4259. }
  4260. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4261. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4262. }
  4263. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  4264. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3]))
  4265. pred_direct_motion(h, &mb_type);
  4266. }else{
  4267. assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE); //FIXME SP correct ?
  4268. for(i=0; i<4; i++){
  4269. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4270. if(h->sub_mb_type[i] >=4){
  4271. 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);
  4272. return -1;
  4273. }
  4274. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4275. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4276. }
  4277. }
  4278. for(list=0; list<2; list++){
  4279. int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4280. if(ref_count == 0) continue;
  4281. if (h->mb_aff_frame && h->mb_field_decoding_flag) {
  4282. ref_count <<= 1;
  4283. }
  4284. for(i=0; i<4; i++){
  4285. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  4286. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4287. ref[list][i] = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
  4288. }else{
  4289. //FIXME
  4290. ref[list][i] = -1;
  4291. }
  4292. }
  4293. }
  4294. if(dct8x8_allowed)
  4295. dct8x8_allowed = get_dct8x8_allowed(h);
  4296. for(list=0; list<2; list++){
  4297. const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4298. if(ref_count == 0) continue;
  4299. for(i=0; i<4; i++){
  4300. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  4301. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  4302. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  4303. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4304. const int sub_mb_type= h->sub_mb_type[i];
  4305. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  4306. for(j=0; j<sub_partition_count[i]; j++){
  4307. int mx, my;
  4308. const int index= 4*i + block_width*j;
  4309. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  4310. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  4311. mx += get_se_golomb(&s->gb);
  4312. my += get_se_golomb(&s->gb);
  4313. tprintf("final mv:%d %d\n", mx, my);
  4314. if(IS_SUB_8X8(sub_mb_type)){
  4315. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  4316. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  4317. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  4318. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  4319. }else if(IS_SUB_8X4(sub_mb_type)){
  4320. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  4321. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  4322. }else if(IS_SUB_4X8(sub_mb_type)){
  4323. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  4324. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  4325. }else{
  4326. assert(IS_SUB_4X4(sub_mb_type));
  4327. mv_cache[ 0 ][0]= mx;
  4328. mv_cache[ 0 ][1]= my;
  4329. }
  4330. }
  4331. }else{
  4332. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  4333. p[0] = p[1]=
  4334. p[8] = p[9]= 0;
  4335. }
  4336. }
  4337. }
  4338. }else if(IS_DIRECT(mb_type)){
  4339. pred_direct_motion(h, &mb_type);
  4340. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  4341. }else{
  4342. int list, mx, my, i;
  4343. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  4344. if(IS_16X16(mb_type)){
  4345. for(list=0; list<2; list++){
  4346. if(h->ref_count[list]>0){
  4347. if(IS_DIR(mb_type, 0, list)){
  4348. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4349. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  4350. }else
  4351. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4352. }
  4353. }
  4354. for(list=0; list<2; list++){
  4355. if(IS_DIR(mb_type, 0, list)){
  4356. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  4357. mx += get_se_golomb(&s->gb);
  4358. my += get_se_golomb(&s->gb);
  4359. tprintf("final mv:%d %d\n", mx, my);
  4360. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  4361. }else
  4362. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  4363. }
  4364. }
  4365. else if(IS_16X8(mb_type)){
  4366. for(list=0; list<2; list++){
  4367. if(h->ref_count[list]>0){
  4368. for(i=0; i<2; i++){
  4369. if(IS_DIR(mb_type, i, list)){
  4370. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4371. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  4372. }else
  4373. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  4374. }
  4375. }
  4376. }
  4377. for(list=0; list<2; list++){
  4378. for(i=0; i<2; i++){
  4379. if(IS_DIR(mb_type, i, list)){
  4380. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  4381. mx += get_se_golomb(&s->gb);
  4382. my += get_se_golomb(&s->gb);
  4383. tprintf("final mv:%d %d\n", mx, my);
  4384. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  4385. }else
  4386. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  4387. }
  4388. }
  4389. }else{
  4390. assert(IS_8X16(mb_type));
  4391. for(list=0; list<2; list++){
  4392. if(h->ref_count[list]>0){
  4393. for(i=0; i<2; i++){
  4394. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  4395. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4396. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  4397. }else
  4398. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4399. }
  4400. }
  4401. }
  4402. for(list=0; list<2; list++){
  4403. for(i=0; i<2; i++){
  4404. if(IS_DIR(mb_type, i, list)){
  4405. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  4406. mx += get_se_golomb(&s->gb);
  4407. my += get_se_golomb(&s->gb);
  4408. tprintf("final mv:%d %d\n", mx, my);
  4409. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  4410. }else
  4411. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  4412. }
  4413. }
  4414. }
  4415. }
  4416. if(IS_INTER(mb_type))
  4417. write_back_motion(h, mb_type);
  4418. if(!IS_INTRA16x16(mb_type)){
  4419. cbp= get_ue_golomb(&s->gb);
  4420. if(cbp > 47){
  4421. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%d) at %d %d\n", cbp, s->mb_x, s->mb_y);
  4422. return -1;
  4423. }
  4424. if(IS_INTRA4x4(mb_type))
  4425. cbp= golomb_to_intra4x4_cbp[cbp];
  4426. else
  4427. cbp= golomb_to_inter_cbp[cbp];
  4428. }
  4429. if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
  4430. if(get_bits1(&s->gb))
  4431. mb_type |= MB_TYPE_8x8DCT;
  4432. }
  4433. s->current_picture.mb_type[mb_xy]= mb_type;
  4434. if(cbp || IS_INTRA16x16(mb_type)){
  4435. int i8x8, i4x4, chroma_idx;
  4436. int chroma_qp, dquant;
  4437. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  4438. const uint8_t *scan, *dc_scan;
  4439. // fill_non_zero_count_cache(h);
  4440. if(IS_INTERLACED(mb_type)){
  4441. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  4442. dc_scan= luma_dc_field_scan;
  4443. }else{
  4444. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  4445. dc_scan= luma_dc_zigzag_scan;
  4446. }
  4447. dquant= get_se_golomb(&s->gb);
  4448. if( dquant > 25 || dquant < -26 ){
  4449. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  4450. return -1;
  4451. }
  4452. s->qscale += dquant;
  4453. if(((unsigned)s->qscale) > 51){
  4454. if(s->qscale<0) s->qscale+= 52;
  4455. else s->qscale-= 52;
  4456. }
  4457. h->chroma_qp= chroma_qp= get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4458. if(IS_INTRA16x16(mb_type)){
  4459. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){
  4460. return -1; //FIXME continue if partitioned and other return -1 too
  4461. }
  4462. assert((cbp&15) == 0 || (cbp&15) == 15);
  4463. if(cbp&15){
  4464. for(i8x8=0; i8x8<4; i8x8++){
  4465. for(i4x4=0; i4x4<4; i4x4++){
  4466. const int index= i4x4 + 4*i8x8;
  4467. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){
  4468. return -1;
  4469. }
  4470. }
  4471. }
  4472. }else{
  4473. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  4474. }
  4475. }else{
  4476. for(i8x8=0; i8x8<4; i8x8++){
  4477. if(cbp & (1<<i8x8)){
  4478. if(IS_8x8DCT(mb_type)){
  4479. DCTELEM *buf = &h->mb[64*i8x8];
  4480. uint8_t *nnz;
  4481. for(i4x4=0; i4x4<4; i4x4++){
  4482. if( decode_residual(h, gb, buf, i4x4+4*i8x8, zigzag_scan8x8_cavlc+16*i4x4,
  4483. h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )
  4484. return -1;
  4485. }
  4486. nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4487. nnz[0] |= nnz[1] | nnz[8] | nnz[9];
  4488. }else{
  4489. for(i4x4=0; i4x4<4; i4x4++){
  4490. const int index= i4x4 + 4*i8x8;
  4491. if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){
  4492. return -1;
  4493. }
  4494. }
  4495. }
  4496. }else{
  4497. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4498. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  4499. }
  4500. }
  4501. }
  4502. if(cbp&0x30){
  4503. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  4504. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){
  4505. return -1;
  4506. }
  4507. }
  4508. if(cbp&0x20){
  4509. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  4510. for(i4x4=0; i4x4<4; i4x4++){
  4511. const int index= 16 + 4*chroma_idx + i4x4;
  4512. 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){
  4513. return -1;
  4514. }
  4515. }
  4516. }
  4517. }else{
  4518. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4519. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4520. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4521. }
  4522. }else{
  4523. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4524. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  4525. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4526. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4527. }
  4528. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4529. write_back_non_zero_count(h);
  4530. return 0;
  4531. }
  4532. static int decode_cabac_field_decoding_flag(H264Context *h) {
  4533. MpegEncContext * const s = &h->s;
  4534. const int mb_x = s->mb_x;
  4535. const int mb_y = s->mb_y & ~1;
  4536. const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
  4537. const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
  4538. unsigned int ctx = 0;
  4539. if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
  4540. ctx += 1;
  4541. }
  4542. if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
  4543. ctx += 1;
  4544. }
  4545. return get_cabac( &h->cabac, &h->cabac_state[70 + ctx] );
  4546. }
  4547. static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
  4548. uint8_t *state= &h->cabac_state[ctx_base];
  4549. int mb_type;
  4550. if(intra_slice){
  4551. MpegEncContext * const s = &h->s;
  4552. const int mba_xy = h->left_mb_xy[0];
  4553. const int mbb_xy = h->top_mb_xy;
  4554. int ctx=0;
  4555. if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
  4556. ctx++;
  4557. if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
  4558. ctx++;
  4559. if( get_cabac( &h->cabac, &state[ctx] ) == 0 )
  4560. return 0; /* I4x4 */
  4561. state += 2;
  4562. }else{
  4563. if( get_cabac( &h->cabac, &state[0] ) == 0 )
  4564. return 0; /* I4x4 */
  4565. }
  4566. if( get_cabac_terminate( &h->cabac ) )
  4567. return 25; /* PCM */
  4568. mb_type = 1; /* I16x16 */
  4569. if( get_cabac( &h->cabac, &state[1] ) )
  4570. mb_type += 12; /* cbp_luma != 0 */
  4571. if( get_cabac( &h->cabac, &state[2] ) ) {
  4572. if( get_cabac( &h->cabac, &state[2+intra_slice] ) )
  4573. mb_type += 4 * 2; /* cbp_chroma == 2 */
  4574. else
  4575. mb_type += 4 * 1; /* cbp_chroma == 1 */
  4576. }
  4577. if( get_cabac( &h->cabac, &state[3+intra_slice] ) )
  4578. mb_type += 2;
  4579. if( get_cabac( &h->cabac, &state[3+2*intra_slice] ) )
  4580. mb_type += 1;
  4581. return mb_type;
  4582. }
  4583. static int decode_cabac_mb_type( H264Context *h ) {
  4584. MpegEncContext * const s = &h->s;
  4585. if( h->slice_type == I_TYPE ) {
  4586. return decode_cabac_intra_mb_type(h, 3, 1);
  4587. } else if( h->slice_type == P_TYPE ) {
  4588. if( get_cabac( &h->cabac, &h->cabac_state[14] ) == 0 ) {
  4589. /* P-type */
  4590. if( get_cabac( &h->cabac, &h->cabac_state[15] ) == 0 ) {
  4591. if( get_cabac( &h->cabac, &h->cabac_state[16] ) == 0 )
  4592. return 0; /* P_L0_D16x16; */
  4593. else
  4594. return 3; /* P_8x8; */
  4595. } else {
  4596. if( get_cabac( &h->cabac, &h->cabac_state[17] ) == 0 )
  4597. return 2; /* P_L0_D8x16; */
  4598. else
  4599. return 1; /* P_L0_D16x8; */
  4600. }
  4601. } else {
  4602. return decode_cabac_intra_mb_type(h, 17, 0) + 5;
  4603. }
  4604. } else if( h->slice_type == B_TYPE ) {
  4605. const int mba_xy = h->left_mb_xy[0];
  4606. const int mbb_xy = h->top_mb_xy;
  4607. int ctx = 0;
  4608. int bits;
  4609. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] )
  4610. && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
  4611. ctx++;
  4612. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] )
  4613. && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
  4614. ctx++;
  4615. if( !get_cabac( &h->cabac, &h->cabac_state[27+ctx] ) )
  4616. return 0; /* B_Direct_16x16 */
  4617. if( !get_cabac( &h->cabac, &h->cabac_state[27+3] ) ) {
  4618. return 1 + get_cabac( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
  4619. }
  4620. bits = get_cabac( &h->cabac, &h->cabac_state[27+4] ) << 3;
  4621. bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] ) << 2;
  4622. bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] ) << 1;
  4623. bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] );
  4624. if( bits < 8 )
  4625. return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
  4626. else if( bits == 13 ) {
  4627. return decode_cabac_intra_mb_type(h, 32, 0) + 23;
  4628. } else if( bits == 14 )
  4629. return 11; /* B_L1_L0_8x16 */
  4630. else if( bits == 15 )
  4631. return 22; /* B_8x8 */
  4632. bits= ( bits<<1 ) | get_cabac( &h->cabac, &h->cabac_state[27+5] );
  4633. return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
  4634. } else {
  4635. /* TODO SI/SP frames? */
  4636. return -1;
  4637. }
  4638. }
  4639. static int decode_cabac_mb_skip( H264Context *h) {
  4640. MpegEncContext * const s = &h->s;
  4641. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  4642. const int mba_xy = mb_xy - 1;
  4643. const int mbb_xy = mb_xy - s->mb_stride;
  4644. int ctx = 0;
  4645. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
  4646. ctx++;
  4647. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
  4648. ctx++;
  4649. if( h->slice_type == P_TYPE || h->slice_type == SP_TYPE)
  4650. return get_cabac( &h->cabac, &h->cabac_state[11+ctx] );
  4651. else /* B-frame */
  4652. return get_cabac( &h->cabac, &h->cabac_state[24+ctx] );
  4653. }
  4654. static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
  4655. int mode = 0;
  4656. if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
  4657. return pred_mode;
  4658. if( get_cabac( &h->cabac, &h->cabac_state[69] ) )
  4659. mode += 1;
  4660. if( get_cabac( &h->cabac, &h->cabac_state[69] ) )
  4661. mode += 2;
  4662. if( get_cabac( &h->cabac, &h->cabac_state[69] ) )
  4663. mode += 4;
  4664. if( mode >= pred_mode )
  4665. return mode + 1;
  4666. else
  4667. return mode;
  4668. }
  4669. static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
  4670. const int mba_xy = h->left_mb_xy[0];
  4671. const int mbb_xy = h->top_mb_xy;
  4672. int ctx = 0;
  4673. /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
  4674. if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
  4675. ctx++;
  4676. if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
  4677. ctx++;
  4678. if( get_cabac( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
  4679. return 0;
  4680. if( get_cabac( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  4681. return 1;
  4682. if( get_cabac( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  4683. return 2;
  4684. else
  4685. return 3;
  4686. }
  4687. static const uint8_t block_idx_x[16] = {
  4688. 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
  4689. };
  4690. static const uint8_t block_idx_y[16] = {
  4691. 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
  4692. };
  4693. static const uint8_t block_idx_xy[4][4] = {
  4694. { 0, 2, 8, 10},
  4695. { 1, 3, 9, 11},
  4696. { 4, 6, 12, 14},
  4697. { 5, 7, 13, 15}
  4698. };
  4699. static int decode_cabac_mb_cbp_luma( H264Context *h) {
  4700. MpegEncContext * const s = &h->s;
  4701. int cbp = 0;
  4702. int i8x8;
  4703. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  4704. int cbp_a = -1;
  4705. int cbp_b = -1;
  4706. int x, y;
  4707. int ctx = 0;
  4708. x = block_idx_x[4*i8x8];
  4709. y = block_idx_y[4*i8x8];
  4710. if( x > 0 )
  4711. cbp_a = cbp;
  4712. else if( s->mb_x > 0 && (h->slice_table[h->left_mb_xy[0]] == h->slice_num)) {
  4713. cbp_a = h->left_cbp;
  4714. tprintf("cbp_a = left_cbp = %x\n", cbp_a);
  4715. }
  4716. if( y > 0 )
  4717. cbp_b = cbp;
  4718. else if( s->mb_y > 0 && (h->slice_table[h->top_mb_xy] == h->slice_num)) {
  4719. cbp_b = h->top_cbp;
  4720. tprintf("cbp_b = top_cbp = %x\n", cbp_b);
  4721. }
  4722. /* No need to test for skip as we put 0 for skip block */
  4723. /* No need to test for IPCM as we put 1 for IPCM block */
  4724. if( cbp_a >= 0 ) {
  4725. int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
  4726. if( ((cbp_a >> i8x8a)&0x01) == 0 )
  4727. ctx++;
  4728. }
  4729. if( cbp_b >= 0 ) {
  4730. int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
  4731. if( ((cbp_b >> i8x8b)&0x01) == 0 )
  4732. ctx += 2;
  4733. }
  4734. if( get_cabac( &h->cabac, &h->cabac_state[73 + ctx] ) ) {
  4735. cbp |= 1 << i8x8;
  4736. }
  4737. }
  4738. return cbp;
  4739. }
  4740. static int decode_cabac_mb_cbp_chroma( H264Context *h) {
  4741. int ctx;
  4742. int cbp_a, cbp_b;
  4743. cbp_a = (h->left_cbp>>4)&0x03;
  4744. cbp_b = (h-> top_cbp>>4)&0x03;
  4745. ctx = 0;
  4746. if( cbp_a > 0 ) ctx++;
  4747. if( cbp_b > 0 ) ctx += 2;
  4748. if( get_cabac( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
  4749. return 0;
  4750. ctx = 4;
  4751. if( cbp_a == 2 ) ctx++;
  4752. if( cbp_b == 2 ) ctx += 2;
  4753. return 1 + get_cabac( &h->cabac, &h->cabac_state[77 + ctx] );
  4754. }
  4755. static int decode_cabac_mb_dqp( H264Context *h) {
  4756. MpegEncContext * const s = &h->s;
  4757. int mbn_xy;
  4758. int ctx = 0;
  4759. int val = 0;
  4760. if( s->mb_x > 0 )
  4761. mbn_xy = s->mb_x + s->mb_y*s->mb_stride - 1;
  4762. else
  4763. mbn_xy = s->mb_width - 1 + (s->mb_y-1)*s->mb_stride;
  4764. if( h->last_qscale_diff != 0 && ( IS_INTRA16x16(s->current_picture.mb_type[mbn_xy] ) || (h->cbp_table[mbn_xy]&0x3f) ) )
  4765. ctx++;
  4766. while( get_cabac( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
  4767. if( ctx < 2 )
  4768. ctx = 2;
  4769. else
  4770. ctx = 3;
  4771. val++;
  4772. if(val > 52) //prevent infinite loop
  4773. return INT_MIN;
  4774. }
  4775. if( val&0x01 )
  4776. return (val + 1)/2;
  4777. else
  4778. return -(val + 1)/2;
  4779. }
  4780. static int decode_cabac_p_mb_sub_type( H264Context *h ) {
  4781. if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
  4782. return 0; /* 8x8 */
  4783. if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
  4784. return 1; /* 8x4 */
  4785. if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
  4786. return 2; /* 4x8 */
  4787. return 3; /* 4x4 */
  4788. }
  4789. static int decode_cabac_b_mb_sub_type( H264Context *h ) {
  4790. int type;
  4791. if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
  4792. return 0; /* B_Direct_8x8 */
  4793. if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
  4794. return 1 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */
  4795. type = 3;
  4796. if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
  4797. if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
  4798. return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
  4799. type += 4;
  4800. }
  4801. type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
  4802. type += get_cabac( &h->cabac, &h->cabac_state[39] );
  4803. return type;
  4804. }
  4805. static inline int decode_cabac_mb_transform_size( H264Context *h ) {
  4806. return get_cabac( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
  4807. }
  4808. static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
  4809. int refa = h->ref_cache[list][scan8[n] - 1];
  4810. int refb = h->ref_cache[list][scan8[n] - 8];
  4811. int ref = 0;
  4812. int ctx = 0;
  4813. if( h->slice_type == B_TYPE) {
  4814. if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
  4815. ctx++;
  4816. if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
  4817. ctx += 2;
  4818. } else {
  4819. if( refa > 0 )
  4820. ctx++;
  4821. if( refb > 0 )
  4822. ctx += 2;
  4823. }
  4824. while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
  4825. ref++;
  4826. if( ctx < 4 )
  4827. ctx = 4;
  4828. else
  4829. ctx = 5;
  4830. }
  4831. return ref;
  4832. }
  4833. static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
  4834. int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
  4835. abs( h->mvd_cache[list][scan8[n] - 8][l] );
  4836. int ctxbase = (l == 0) ? 40 : 47;
  4837. int ctx, mvd;
  4838. if( amvd < 3 )
  4839. ctx = 0;
  4840. else if( amvd > 32 )
  4841. ctx = 2;
  4842. else
  4843. ctx = 1;
  4844. if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
  4845. return 0;
  4846. mvd= 1;
  4847. ctx= 3;
  4848. while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
  4849. mvd++;
  4850. if( ctx < 6 )
  4851. ctx++;
  4852. }
  4853. if( mvd >= 9 ) {
  4854. int k = 3;
  4855. while( get_cabac_bypass( &h->cabac ) ) {
  4856. mvd += 1 << k;
  4857. k++;
  4858. }
  4859. while( k-- ) {
  4860. if( get_cabac_bypass( &h->cabac ) )
  4861. mvd += 1 << k;
  4862. }
  4863. }
  4864. if( get_cabac_bypass( &h->cabac ) ) return -mvd;
  4865. else return mvd;
  4866. }
  4867. static int inline get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) {
  4868. int nza, nzb;
  4869. int ctx = 0;
  4870. if( cat == 0 ) {
  4871. nza = h->left_cbp&0x100;
  4872. nzb = h-> top_cbp&0x100;
  4873. } else if( cat == 1 || cat == 2 ) {
  4874. nza = h->non_zero_count_cache[scan8[idx] - 1];
  4875. nzb = h->non_zero_count_cache[scan8[idx] - 8];
  4876. } else if( cat == 3 ) {
  4877. nza = (h->left_cbp>>(6+idx))&0x01;
  4878. nzb = (h-> top_cbp>>(6+idx))&0x01;
  4879. } else {
  4880. assert(cat == 4);
  4881. nza = h->non_zero_count_cache[scan8[16+idx] - 1];
  4882. nzb = h->non_zero_count_cache[scan8[16+idx] - 8];
  4883. }
  4884. if( nza > 0 )
  4885. ctx++;
  4886. if( nzb > 0 )
  4887. ctx += 2;
  4888. return ctx + 4 * cat;
  4889. }
  4890. 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) {
  4891. const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
  4892. static const int significant_coeff_flag_field_offset[2] = { 105, 277 };
  4893. static const int last_significant_coeff_flag_field_offset[2] = { 166, 338 };
  4894. static const int significant_coeff_flag_offset[6] = { 0, 15, 29, 44, 47, 297 };
  4895. static const int last_significant_coeff_flag_offset[6] = { 0, 15, 29, 44, 47, 251 };
  4896. static const int coeff_abs_level_m1_offset[6] = { 227+0, 227+10, 227+20, 227+30, 227+39, 426 };
  4897. static const int significant_coeff_flag_offset_8x8[63] = {
  4898. 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
  4899. 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
  4900. 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
  4901. 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12
  4902. };
  4903. static const int last_coeff_flag_offset_8x8[63] = {
  4904. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  4905. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  4906. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  4907. 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
  4908. };
  4909. int index[64];
  4910. int i, last;
  4911. int coeff_count = 0;
  4912. int abslevel1 = 1;
  4913. int abslevelgt1 = 0;
  4914. uint8_t *significant_coeff_ctx_base;
  4915. uint8_t *last_coeff_ctx_base;
  4916. uint8_t *abs_level_m1_ctx_base;
  4917. /* cat: 0-> DC 16x16 n = 0
  4918. * 1-> AC 16x16 n = luma4x4idx
  4919. * 2-> Luma4x4 n = luma4x4idx
  4920. * 3-> DC Chroma n = iCbCr
  4921. * 4-> AC Chroma n = 4 * iCbCr + chroma4x4idx
  4922. * 5-> Luma8x8 n = 4 * luma8x8idx
  4923. */
  4924. /* read coded block flag */
  4925. if( cat != 5 ) {
  4926. if( get_cabac( &h->cabac, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n ) ] ) == 0 ) {
  4927. if( cat == 1 || cat == 2 )
  4928. h->non_zero_count_cache[scan8[n]] = 0;
  4929. else if( cat == 4 )
  4930. h->non_zero_count_cache[scan8[16+n]] = 0;
  4931. return 0;
  4932. }
  4933. }
  4934. significant_coeff_ctx_base = h->cabac_state
  4935. + significant_coeff_flag_offset[cat]
  4936. + significant_coeff_flag_field_offset[h->mb_field_decoding_flag];
  4937. last_coeff_ctx_base = h->cabac_state
  4938. + last_significant_coeff_flag_offset[cat]
  4939. + last_significant_coeff_flag_field_offset[h->mb_field_decoding_flag];
  4940. abs_level_m1_ctx_base = h->cabac_state
  4941. + coeff_abs_level_m1_offset[cat];
  4942. if( cat == 5 ) {
  4943. #define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \
  4944. for(last= 0; last < coefs; last++) { \
  4945. uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \
  4946. if( get_cabac( &h->cabac, sig_ctx )) { \
  4947. uint8_t *last_ctx = last_coeff_ctx_base + last_off; \
  4948. index[coeff_count++] = last; \
  4949. if( get_cabac( &h->cabac, last_ctx ) ) { \
  4950. last= max_coeff; \
  4951. break; \
  4952. } \
  4953. } \
  4954. }
  4955. DECODE_SIGNIFICANCE( 63, significant_coeff_flag_offset_8x8[last],
  4956. last_coeff_flag_offset_8x8[last] );
  4957. } else {
  4958. DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
  4959. }
  4960. if( last == max_coeff -1 ) {
  4961. index[coeff_count++] = last;
  4962. }
  4963. assert(coeff_count > 0);
  4964. if( cat == 0 )
  4965. h->cbp_table[mb_xy] |= 0x100;
  4966. else if( cat == 1 || cat == 2 )
  4967. h->non_zero_count_cache[scan8[n]] = coeff_count;
  4968. else if( cat == 3 )
  4969. h->cbp_table[mb_xy] |= 0x40 << n;
  4970. else if( cat == 4 )
  4971. h->non_zero_count_cache[scan8[16+n]] = coeff_count;
  4972. else {
  4973. assert( cat == 5 );
  4974. fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, 1, 1);
  4975. }
  4976. for( i = coeff_count - 1; i >= 0; i-- ) {
  4977. uint8_t *ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 )) + abs_level_m1_ctx_base;
  4978. int j= scantable[index[i]];
  4979. if( get_cabac( &h->cabac, ctx ) == 0 ) {
  4980. if( !qmul ) {
  4981. if( get_cabac_bypass( &h->cabac ) ) block[j] = -1;
  4982. else block[j] = 1;
  4983. }else{
  4984. if( get_cabac_bypass( &h->cabac ) ) block[j] = (-qmul[j] + 32) >> 6;
  4985. else block[j] = ( qmul[j] + 32) >> 6;
  4986. }
  4987. abslevel1++;
  4988. } else {
  4989. int coeff_abs = 2;
  4990. ctx = 5 + FFMIN( 4, abslevelgt1 ) + abs_level_m1_ctx_base;
  4991. while( coeff_abs < 15 && get_cabac( &h->cabac, ctx ) ) {
  4992. coeff_abs++;
  4993. }
  4994. if( coeff_abs >= 15 ) {
  4995. int j = 0;
  4996. while( get_cabac_bypass( &h->cabac ) ) {
  4997. coeff_abs += 1 << j;
  4998. j++;
  4999. }
  5000. while( j-- ) {
  5001. if( get_cabac_bypass( &h->cabac ) )
  5002. coeff_abs += 1 << j ;
  5003. }
  5004. }
  5005. if( !qmul ) {
  5006. if( get_cabac_bypass( &h->cabac ) ) block[j] = -coeff_abs;
  5007. else block[j] = coeff_abs;
  5008. }else{
  5009. if( get_cabac_bypass( &h->cabac ) ) block[j] = (-coeff_abs * qmul[j] + 32) >> 6;
  5010. else block[j] = ( coeff_abs * qmul[j] + 32) >> 6;
  5011. }
  5012. abslevelgt1++;
  5013. }
  5014. }
  5015. return 0;
  5016. }
  5017. void inline compute_mb_neighboors(H264Context *h)
  5018. {
  5019. MpegEncContext * const s = &h->s;
  5020. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  5021. h->top_mb_xy = mb_xy - s->mb_stride;
  5022. h->left_mb_xy[0] = mb_xy - 1;
  5023. if(h->mb_aff_frame){
  5024. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  5025. const int top_pair_xy = pair_xy - s->mb_stride;
  5026. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  5027. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  5028. const int curr_mb_frame_flag = !h->mb_field_decoding_flag;
  5029. const int bottom = (s->mb_y & 1);
  5030. if (bottom
  5031. ? !curr_mb_frame_flag // bottom macroblock
  5032. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  5033. ) {
  5034. h->top_mb_xy -= s->mb_stride;
  5035. }
  5036. if (left_mb_frame_flag != curr_mb_frame_flag) {
  5037. h->left_mb_xy[0] = pair_xy - 1;
  5038. }
  5039. }
  5040. return;
  5041. }
  5042. /**
  5043. * decodes a macroblock
  5044. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  5045. */
  5046. static int decode_mb_cabac(H264Context *h) {
  5047. MpegEncContext * const s = &h->s;
  5048. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  5049. int mb_type, partition_count, cbp = 0;
  5050. int dct8x8_allowed= h->pps.transform_8x8_mode;
  5051. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?)
  5052. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  5053. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) {
  5054. /* read skip flags */
  5055. if( decode_cabac_mb_skip( h ) ) {
  5056. decode_mb_skip(h);
  5057. h->cbp_table[mb_xy] = 0;
  5058. h->chroma_pred_mode_table[mb_xy] = 0;
  5059. h->last_qscale_diff = 0;
  5060. return 0;
  5061. }
  5062. }
  5063. if(h->mb_aff_frame){
  5064. if ( ((s->mb_y&1) == 0) || h->prev_mb_skipped)
  5065. h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5066. }else
  5067. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  5068. h->prev_mb_skipped = 0;
  5069. compute_mb_neighboors(h);
  5070. if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
  5071. av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
  5072. return -1;
  5073. }
  5074. if( h->slice_type == B_TYPE ) {
  5075. if( mb_type < 23 ){
  5076. partition_count= b_mb_type_info[mb_type].partition_count;
  5077. mb_type= b_mb_type_info[mb_type].type;
  5078. }else{
  5079. mb_type -= 23;
  5080. goto decode_intra_mb;
  5081. }
  5082. } else if( h->slice_type == P_TYPE ) {
  5083. if( mb_type < 5) {
  5084. partition_count= p_mb_type_info[mb_type].partition_count;
  5085. mb_type= p_mb_type_info[mb_type].type;
  5086. } else {
  5087. mb_type -= 5;
  5088. goto decode_intra_mb;
  5089. }
  5090. } else {
  5091. assert(h->slice_type == I_TYPE);
  5092. decode_intra_mb:
  5093. partition_count = 0;
  5094. cbp= i_mb_type_info[mb_type].cbp;
  5095. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  5096. mb_type= i_mb_type_info[mb_type].type;
  5097. }
  5098. if(h->mb_field_decoding_flag)
  5099. mb_type |= MB_TYPE_INTERLACED;
  5100. h->slice_table[ mb_xy ]= h->slice_num;
  5101. if(IS_INTRA_PCM(mb_type)) {
  5102. const uint8_t *ptr;
  5103. unsigned int x, y;
  5104. // We assume these blocks are very rare so we dont optimize it.
  5105. // FIXME The two following lines get the bitstream position in the cabac
  5106. // decode, I think it should be done by a function in cabac.h (or cabac.c).
  5107. ptr= h->cabac.bytestream;
  5108. if (h->cabac.low&0x1) ptr-=CABAC_BITS/8;
  5109. // The pixels are stored in the same order as levels in h->mb array.
  5110. for(y=0; y<16; y++){
  5111. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  5112. for(x=0; x<16; x++){
  5113. tprintf("LUMA ICPM LEVEL (%3d)\n", *ptr);
  5114. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= *ptr++;
  5115. }
  5116. }
  5117. for(y=0; y<8; y++){
  5118. const int index= 256 + 4*(y&3) + 32*(y>>2);
  5119. for(x=0; x<8; x++){
  5120. tprintf("CHROMA U ICPM LEVEL (%3d)\n", *ptr);
  5121. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5122. }
  5123. }
  5124. for(y=0; y<8; y++){
  5125. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  5126. for(x=0; x<8; x++){
  5127. tprintf("CHROMA V ICPM LEVEL (%3d)\n", *ptr);
  5128. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5129. }
  5130. }
  5131. ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);
  5132. // All blocks are present
  5133. h->cbp_table[mb_xy] = 0x1ef;
  5134. h->chroma_pred_mode_table[mb_xy] = 0;
  5135. // In deblocking, the quantizer is 0
  5136. s->current_picture.qscale_table[mb_xy]= 0;
  5137. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  5138. // All coeffs are present
  5139. memset(h->non_zero_count[mb_xy], 16, 16);
  5140. s->current_picture.mb_type[mb_xy]= mb_type;
  5141. return 0;
  5142. }
  5143. fill_caches(h, mb_type, 0);
  5144. if( IS_INTRA( mb_type ) ) {
  5145. int i;
  5146. if( IS_INTRA4x4( mb_type ) ) {
  5147. if( dct8x8_allowed && decode_cabac_mb_transform_size( h ) ) {
  5148. mb_type |= MB_TYPE_8x8DCT;
  5149. for( i = 0; i < 16; i+=4 ) {
  5150. int pred = pred_intra_mode( h, i );
  5151. int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5152. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  5153. }
  5154. } else {
  5155. for( i = 0; i < 16; i++ ) {
  5156. int pred = pred_intra_mode( h, i );
  5157. h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5158. //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
  5159. }
  5160. }
  5161. write_back_intra_pred_mode(h);
  5162. if( check_intra4x4_pred_mode(h) < 0 ) return -1;
  5163. } else {
  5164. h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );
  5165. if( h->intra16x16_pred_mode < 0 ) return -1;
  5166. }
  5167. h->chroma_pred_mode_table[mb_xy] =
  5168. h->chroma_pred_mode = decode_cabac_mb_chroma_pre_mode( h );
  5169. h->chroma_pred_mode= check_intra_pred_mode( h, h->chroma_pred_mode );
  5170. if( h->chroma_pred_mode < 0 ) return -1;
  5171. } else if( partition_count == 4 ) {
  5172. int i, j, sub_partition_count[4], list, ref[2][4];
  5173. if( h->slice_type == B_TYPE ) {
  5174. for( i = 0; i < 4; i++ ) {
  5175. h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
  5176. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5177. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5178. }
  5179. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  5180. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
  5181. pred_direct_motion(h, &mb_type);
  5182. if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
  5183. for( i = 0; i < 4; i++ )
  5184. if( IS_DIRECT(h->sub_mb_type[i]) )
  5185. fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );
  5186. }
  5187. }
  5188. } else {
  5189. for( i = 0; i < 4; i++ ) {
  5190. h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
  5191. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5192. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5193. }
  5194. }
  5195. for( list = 0; list < 2; list++ ) {
  5196. if( h->ref_count[list] > 0 ) {
  5197. for( i = 0; i < 4; i++ ) {
  5198. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  5199. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  5200. if( h->ref_count[list] > 1 )
  5201. ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );
  5202. else
  5203. ref[list][i] = 0;
  5204. } else {
  5205. ref[list][i] = -1;
  5206. }
  5207. h->ref_cache[list][ scan8[4*i]+1 ]=
  5208. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  5209. }
  5210. }
  5211. }
  5212. if(dct8x8_allowed)
  5213. dct8x8_allowed = get_dct8x8_allowed(h);
  5214. for(list=0; list<2; list++){
  5215. for(i=0; i<4; i++){
  5216. if(IS_DIRECT(h->sub_mb_type[i])){
  5217. fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);
  5218. continue;
  5219. }
  5220. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];
  5221. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  5222. const int sub_mb_type= h->sub_mb_type[i];
  5223. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  5224. for(j=0; j<sub_partition_count[i]; j++){
  5225. int mpx, mpy;
  5226. int mx, my;
  5227. const int index= 4*i + block_width*j;
  5228. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  5229. int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];
  5230. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);
  5231. mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );
  5232. my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );
  5233. tprintf("final mv:%d %d\n", mx, my);
  5234. if(IS_SUB_8X8(sub_mb_type)){
  5235. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  5236. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  5237. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  5238. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  5239. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]=
  5240. mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;
  5241. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]=
  5242. mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;
  5243. }else if(IS_SUB_8X4(sub_mb_type)){
  5244. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  5245. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  5246. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]= mx- mpx;
  5247. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]= my - mpy;
  5248. }else if(IS_SUB_4X8(sub_mb_type)){
  5249. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  5250. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  5251. mvd_cache[ 0 ][0]= mvd_cache[ 8 ][0]= mx - mpx;
  5252. mvd_cache[ 0 ][1]= mvd_cache[ 8 ][1]= my - mpy;
  5253. }else{
  5254. assert(IS_SUB_4X4(sub_mb_type));
  5255. mv_cache[ 0 ][0]= mx;
  5256. mv_cache[ 0 ][1]= my;
  5257. mvd_cache[ 0 ][0]= mx - mpx;
  5258. mvd_cache[ 0 ][1]= my - mpy;
  5259. }
  5260. }
  5261. }else{
  5262. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  5263. uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];
  5264. p[0] = p[1] = p[8] = p[9] = 0;
  5265. pd[0]= pd[1]= pd[8]= pd[9]= 0;
  5266. }
  5267. }
  5268. }
  5269. } else if( IS_DIRECT(mb_type) ) {
  5270. pred_direct_motion(h, &mb_type);
  5271. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  5272. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  5273. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  5274. } else {
  5275. int list, mx, my, i, mpx, mpy;
  5276. if(IS_16X16(mb_type)){
  5277. for(list=0; list<2; list++){
  5278. if(IS_DIR(mb_type, 0, list)){
  5279. if(h->ref_count[list] > 0 ){
  5280. const int ref = h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 0 ) : 0;
  5281. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
  5282. }
  5283. }else
  5284. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  5285. }
  5286. for(list=0; list<2; list++){
  5287. if(IS_DIR(mb_type, 0, list)){
  5288. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);
  5289. mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );
  5290. my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );
  5291. tprintf("final mv:%d %d\n", mx, my);
  5292. fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5293. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  5294. }else
  5295. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  5296. }
  5297. }
  5298. else if(IS_16X8(mb_type)){
  5299. for(list=0; list<2; list++){
  5300. if(h->ref_count[list]>0){
  5301. for(i=0; i<2; i++){
  5302. if(IS_DIR(mb_type, i, list)){
  5303. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 8*i ) : 0;
  5304. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
  5305. }else
  5306. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  5307. }
  5308. }
  5309. }
  5310. for(list=0; list<2; list++){
  5311. for(i=0; i<2; i++){
  5312. if(IS_DIR(mb_type, i, list)){
  5313. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);
  5314. mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );
  5315. my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );
  5316. tprintf("final mv:%d %d\n", mx, my);
  5317. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);
  5318. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  5319. }else{
  5320. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5321. fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5322. }
  5323. }
  5324. }
  5325. }else{
  5326. assert(IS_8X16(mb_type));
  5327. for(list=0; list<2; list++){
  5328. if(h->ref_count[list]>0){
  5329. for(i=0; i<2; i++){
  5330. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  5331. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 4*i ) : 0;
  5332. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
  5333. }else
  5334. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  5335. }
  5336. }
  5337. }
  5338. for(list=0; list<2; list++){
  5339. for(i=0; i<2; i++){
  5340. if(IS_DIR(mb_type, i, list)){
  5341. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);
  5342. mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );
  5343. my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );
  5344. tprintf("final mv:%d %d\n", mx, my);
  5345. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5346. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  5347. }else{
  5348. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5349. fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5350. }
  5351. }
  5352. }
  5353. }
  5354. }
  5355. if( IS_INTER( mb_type ) ) {
  5356. h->chroma_pred_mode_table[mb_xy] = 0;
  5357. write_back_motion( h, mb_type );
  5358. }
  5359. if( !IS_INTRA16x16( mb_type ) ) {
  5360. cbp = decode_cabac_mb_cbp_luma( h );
  5361. cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
  5362. }
  5363. h->cbp_table[mb_xy] = cbp;
  5364. if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
  5365. if( decode_cabac_mb_transform_size( h ) )
  5366. mb_type |= MB_TYPE_8x8DCT;
  5367. }
  5368. s->current_picture.mb_type[mb_xy]= mb_type;
  5369. if( cbp || IS_INTRA16x16( mb_type ) ) {
  5370. const uint8_t *scan, *dc_scan;
  5371. int dqp;
  5372. if(IS_INTERLACED(mb_type)){
  5373. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  5374. dc_scan= luma_dc_field_scan;
  5375. }else{
  5376. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  5377. dc_scan= luma_dc_zigzag_scan;
  5378. }
  5379. h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
  5380. if( dqp == INT_MIN ){
  5381. av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", s->mb_x, s->mb_y);
  5382. return -1;
  5383. }
  5384. s->qscale += dqp;
  5385. if(((unsigned)s->qscale) > 51){
  5386. if(s->qscale<0) s->qscale+= 52;
  5387. else s->qscale-= 52;
  5388. }
  5389. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  5390. if( IS_INTRA16x16( mb_type ) ) {
  5391. int i;
  5392. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
  5393. if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16) < 0)
  5394. return -1;
  5395. if( cbp&15 ) {
  5396. for( i = 0; i < 16; i++ ) {
  5397. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
  5398. if( decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 )
  5399. return -1;
  5400. }
  5401. } else {
  5402. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  5403. }
  5404. } else {
  5405. int i8x8, i4x4;
  5406. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5407. if( cbp & (1<<i8x8) ) {
  5408. if( IS_8x8DCT(mb_type) ) {
  5409. if( decode_cabac_residual(h, h->mb + 64*i8x8, 5, 4*i8x8,
  5410. zigzag_scan8x8, h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 64) < 0 )
  5411. return -1;
  5412. } else
  5413. for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
  5414. const int index = 4*i8x8 + i4x4;
  5415. //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
  5416. 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 )
  5417. return -1;
  5418. }
  5419. } else {
  5420. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  5421. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  5422. }
  5423. }
  5424. }
  5425. if( cbp&0x30 ){
  5426. int c;
  5427. for( c = 0; c < 2; c++ ) {
  5428. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
  5429. if( decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, NULL, 4) < 0)
  5430. return -1;
  5431. }
  5432. }
  5433. if( cbp&0x20 ) {
  5434. int c, i;
  5435. for( c = 0; c < 2; c++ ) {
  5436. for( i = 0; i < 4; i++ ) {
  5437. const int index = 16 + 4 * c + i;
  5438. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
  5439. 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)
  5440. return -1;
  5441. }
  5442. }
  5443. } else {
  5444. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5445. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5446. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5447. }
  5448. } else {
  5449. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5450. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  5451. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5452. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5453. }
  5454. s->current_picture.qscale_table[mb_xy]= s->qscale;
  5455. write_back_non_zero_count(h);
  5456. return 0;
  5457. }
  5458. static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  5459. int i, d;
  5460. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  5461. const int alpha = alpha_table[index_a];
  5462. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  5463. if( bS[0] < 4 ) {
  5464. int8_t tc[4];
  5465. for(i=0; i<4; i++)
  5466. tc[i] = bS[i] ? tc0_table[index_a][bS[i] - 1] : -1;
  5467. h->s.dsp.h264_h_loop_filter_luma(pix, stride, alpha, beta, tc);
  5468. } else {
  5469. /* 16px edge length, because bS=4 is triggered by being at
  5470. * the edge of an intra MB, so all 4 bS are the same */
  5471. for( d = 0; d < 16; d++ ) {
  5472. const int p0 = pix[-1];
  5473. const int p1 = pix[-2];
  5474. const int p2 = pix[-3];
  5475. const int q0 = pix[0];
  5476. const int q1 = pix[1];
  5477. const int q2 = pix[2];
  5478. if( ABS( p0 - q0 ) < alpha &&
  5479. ABS( p1 - p0 ) < beta &&
  5480. ABS( q1 - q0 ) < beta ) {
  5481. if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5482. if( ABS( p2 - p0 ) < beta)
  5483. {
  5484. const int p3 = pix[-4];
  5485. /* p0', p1', p2' */
  5486. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5487. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5488. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5489. } else {
  5490. /* p0' */
  5491. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5492. }
  5493. if( ABS( q2 - q0 ) < beta)
  5494. {
  5495. const int q3 = pix[3];
  5496. /* q0', q1', q2' */
  5497. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5498. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5499. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5500. } else {
  5501. /* q0' */
  5502. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5503. }
  5504. }else{
  5505. /* p0', q0' */
  5506. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5507. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5508. }
  5509. 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]);
  5510. }
  5511. pix += stride;
  5512. }
  5513. }
  5514. }
  5515. static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  5516. int i;
  5517. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  5518. const int alpha = alpha_table[index_a];
  5519. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  5520. if( bS[0] < 4 ) {
  5521. int8_t tc[4];
  5522. for(i=0; i<4; i++)
  5523. tc[i] = bS[i] ? tc0_table[index_a][bS[i] - 1] + 1 : 0;
  5524. h->s.dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
  5525. } else {
  5526. h->s.dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
  5527. }
  5528. }
  5529. static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int bS[8], int qp[2] ) {
  5530. int i;
  5531. for( i = 0; i < 16; i++, pix += stride) {
  5532. int index_a;
  5533. int alpha;
  5534. int beta;
  5535. int qp_index;
  5536. int bS_index = (i >> 1);
  5537. if (h->mb_field_decoding_flag) {
  5538. bS_index &= ~1;
  5539. bS_index |= (i & 1);
  5540. }
  5541. if( bS[bS_index] == 0 ) {
  5542. continue;
  5543. }
  5544. qp_index = h->mb_field_decoding_flag ? (i & 1) : (i >> 3);
  5545. index_a = clip( qp[qp_index] + h->slice_alpha_c0_offset, 0, 51 );
  5546. alpha = alpha_table[index_a];
  5547. beta = beta_table[clip( qp[qp_index] + h->slice_beta_offset, 0, 51 )];
  5548. if( bS[bS_index] < 4 ) {
  5549. const int tc0 = tc0_table[index_a][bS[bS_index] - 1];
  5550. /* 4px edge length */
  5551. const int p0 = pix[-1];
  5552. const int p1 = pix[-2];
  5553. const int p2 = pix[-3];
  5554. const int q0 = pix[0];
  5555. const int q1 = pix[1];
  5556. const int q2 = pix[2];
  5557. if( ABS( p0 - q0 ) < alpha &&
  5558. ABS( p1 - p0 ) < beta &&
  5559. ABS( q1 - q0 ) < beta ) {
  5560. int tc = tc0;
  5561. int i_delta;
  5562. if( ABS( p2 - p0 ) < beta ) {
  5563. pix[-2] = p1 + clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  5564. tc++;
  5565. }
  5566. if( ABS( q2 - q0 ) < beta ) {
  5567. pix[1] = q1 + clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  5568. tc++;
  5569. }
  5570. i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  5571. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  5572. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  5573. 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);
  5574. }
  5575. }else{
  5576. /* 4px edge length */
  5577. const int p0 = pix[-1];
  5578. const int p1 = pix[-2];
  5579. const int p2 = pix[-3];
  5580. const int q0 = pix[0];
  5581. const int q1 = pix[1];
  5582. const int q2 = pix[2];
  5583. if( ABS( p0 - q0 ) < alpha &&
  5584. ABS( p1 - p0 ) < beta &&
  5585. ABS( q1 - q0 ) < beta ) {
  5586. if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5587. if( ABS( p2 - p0 ) < beta)
  5588. {
  5589. const int p3 = pix[-4];
  5590. /* p0', p1', p2' */
  5591. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5592. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5593. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5594. } else {
  5595. /* p0' */
  5596. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5597. }
  5598. if( ABS( q2 - q0 ) < beta)
  5599. {
  5600. const int q3 = pix[3];
  5601. /* q0', q1', q2' */
  5602. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5603. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5604. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5605. } else {
  5606. /* q0' */
  5607. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5608. }
  5609. }else{
  5610. /* p0', q0' */
  5611. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5612. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5613. }
  5614. 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]);
  5615. }
  5616. }
  5617. }
  5618. }
  5619. static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp[2] ) {
  5620. int i;
  5621. for( i = 0; i < 8; i++, pix += stride) {
  5622. int index_a;
  5623. int alpha;
  5624. int beta;
  5625. int qp_index;
  5626. int bS_index = i;
  5627. if( bS[bS_index] == 0 ) {
  5628. continue;
  5629. }
  5630. qp_index = h->mb_field_decoding_flag ? (i & 1) : (i >> 3);
  5631. index_a = clip( qp[qp_index] + h->slice_alpha_c0_offset, 0, 51 );
  5632. alpha = alpha_table[index_a];
  5633. beta = beta_table[clip( qp[qp_index] + h->slice_beta_offset, 0, 51 )];
  5634. if( bS[bS_index] < 4 ) {
  5635. const int tc = tc0_table[index_a][bS[bS_index] - 1] + 1;
  5636. /* 2px edge length (because we use same bS than the one for luma) */
  5637. const int p0 = pix[-1];
  5638. const int p1 = pix[-2];
  5639. const int q0 = pix[0];
  5640. const int q1 = pix[1];
  5641. if( ABS( p0 - q0 ) < alpha &&
  5642. ABS( p1 - p0 ) < beta &&
  5643. ABS( q1 - q0 ) < beta ) {
  5644. const int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  5645. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  5646. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  5647. 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);
  5648. }
  5649. }else{
  5650. const int p0 = pix[-1];
  5651. const int p1 = pix[-2];
  5652. const int q0 = pix[0];
  5653. const int q1 = pix[1];
  5654. if( ABS( p0 - q0 ) < alpha &&
  5655. ABS( p1 - p0 ) < beta &&
  5656. ABS( q1 - q0 ) < beta ) {
  5657. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  5658. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  5659. 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]);
  5660. }
  5661. }
  5662. }
  5663. }
  5664. static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  5665. int i, d;
  5666. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  5667. const int alpha = alpha_table[index_a];
  5668. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  5669. const int pix_next = stride;
  5670. if( bS[0] < 4 ) {
  5671. int8_t tc[4];
  5672. for(i=0; i<4; i++)
  5673. tc[i] = bS[i] ? tc0_table[index_a][bS[i] - 1] : -1;
  5674. h->s.dsp.h264_v_loop_filter_luma(pix, stride, alpha, beta, tc);
  5675. } else {
  5676. /* 16px edge length, see filter_mb_edgev */
  5677. for( d = 0; d < 16; d++ ) {
  5678. const int p0 = pix[-1*pix_next];
  5679. const int p1 = pix[-2*pix_next];
  5680. const int p2 = pix[-3*pix_next];
  5681. const int q0 = pix[0];
  5682. const int q1 = pix[1*pix_next];
  5683. const int q2 = pix[2*pix_next];
  5684. if( ABS( p0 - q0 ) < alpha &&
  5685. ABS( p1 - p0 ) < beta &&
  5686. ABS( q1 - q0 ) < beta ) {
  5687. const int p3 = pix[-4*pix_next];
  5688. const int q3 = pix[ 3*pix_next];
  5689. if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5690. if( ABS( p2 - p0 ) < beta) {
  5691. /* p0', p1', p2' */
  5692. pix[-1*pix_next] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5693. pix[-2*pix_next] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5694. pix[-3*pix_next] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5695. } else {
  5696. /* p0' */
  5697. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5698. }
  5699. if( ABS( q2 - q0 ) < beta) {
  5700. /* q0', q1', q2' */
  5701. pix[0*pix_next] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5702. pix[1*pix_next] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5703. pix[2*pix_next] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5704. } else {
  5705. /* q0' */
  5706. pix[0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5707. }
  5708. }else{
  5709. /* p0', q0' */
  5710. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5711. pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5712. }
  5713. 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]);
  5714. }
  5715. pix++;
  5716. }
  5717. }
  5718. }
  5719. static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  5720. int i;
  5721. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  5722. const int alpha = alpha_table[index_a];
  5723. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  5724. if( bS[0] < 4 ) {
  5725. int8_t tc[4];
  5726. for(i=0; i<4; i++)
  5727. tc[i] = bS[i] ? tc0_table[index_a][bS[i] - 1] + 1 : 0;
  5728. h->s.dsp.h264_v_loop_filter_chroma(pix, stride, alpha, beta, tc);
  5729. } else {
  5730. h->s.dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);
  5731. }
  5732. }
  5733. 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) {
  5734. MpegEncContext * const s = &h->s;
  5735. const int mb_xy= mb_x + mb_y*s->mb_stride;
  5736. int first_vertical_edge_done = 0;
  5737. int dir;
  5738. /* FIXME: A given frame may occupy more than one position in
  5739. * the reference list. So ref2frm should be populated with
  5740. * frame numbers, not indices. */
  5741. static const int ref2frm[18] = {-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  5742. if (h->mb_aff_frame
  5743. // left mb is in picture
  5744. && h->slice_table[mb_xy-1] != 255
  5745. // and current and left pair do not have the same interlaced type
  5746. && (IS_INTERLACED(s->current_picture.mb_type[mb_xy]) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
  5747. // and left mb is in the same slice if deblocking_filter == 2
  5748. && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
  5749. /* First vertical edge is different in MBAFF frames
  5750. * There are 8 different bS to compute and 2 different Qp
  5751. */
  5752. int bS[8];
  5753. int qp[2];
  5754. int chroma_qp[2];
  5755. int i;
  5756. first_vertical_edge_done = 1;
  5757. for( i = 0; i < 8; i++ ) {
  5758. int y = i>>1;
  5759. int b_idx= 8 + 4 + 8*y;
  5760. int bn_idx= b_idx - 1;
  5761. int mbn_xy = h->mb_field_decoding_flag ? h->left_mb_xy[i>>2] : h->left_mb_xy[i&1];
  5762. if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) ||
  5763. IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) {
  5764. bS[i] = 4;
  5765. } else if( h->non_zero_count_cache[b_idx] != 0 ||
  5766. /* FIXME: with 8x8dct + cavlc, should check cbp instead of nnz */
  5767. h->non_zero_count_cache[bn_idx] != 0 ) {
  5768. bS[i] = 2;
  5769. } else {
  5770. int l;
  5771. bS[i] = 0;
  5772. for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
  5773. if( ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  5774. ABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  5775. ABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= 4 ) {
  5776. bS[i] = 1;
  5777. break;
  5778. }
  5779. }
  5780. }
  5781. }
  5782. if(bS[0]+bS[1]+bS[2]+bS[3] != 0) {
  5783. // Do not use s->qscale as luma quantizer because it has not the same
  5784. // value in IPCM macroblocks.
  5785. qp[0] = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[h->left_mb_xy[0]] + 1 ) >> 1;
  5786. chroma_qp[0] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy] ) +
  5787. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[h->left_mb_xy[0]] ) + 1 ) >> 1;
  5788. qp[1] = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[h->left_mb_xy[1]] + 1 ) >> 1;
  5789. chroma_qp[1] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy] ) +
  5790. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[h->left_mb_xy[1]] ) + 1 ) >> 1;
  5791. /* Filter edge */
  5792. 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);
  5793. { int i; for (i = 0; i < 8; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  5794. filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp );
  5795. filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, chroma_qp );
  5796. filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, chroma_qp );
  5797. }
  5798. }
  5799. /* dir : 0 -> vertical edge, 1 -> horizontal edge */
  5800. for( dir = 0; dir < 2; dir++ )
  5801. {
  5802. int edge;
  5803. const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
  5804. int start = h->slice_table[mbm_xy] == 255 ? 1 : 0;
  5805. if (first_vertical_edge_done) {
  5806. start = 1;
  5807. first_vertical_edge_done = 0;
  5808. }
  5809. if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
  5810. start = 1;
  5811. /* Calculate bS */
  5812. for( edge = start; edge < 4; edge++ ) {
  5813. /* mbn_xy: neighbor macroblock */
  5814. int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
  5815. int bS[4];
  5816. int qp;
  5817. if( (edge&1) && IS_8x8DCT(s->current_picture.mb_type[mb_xy]) )
  5818. continue;
  5819. if (h->mb_aff_frame && (dir == 1) && (edge == 0) && ((mb_y & 1) == 0)
  5820. && !IS_INTERLACED(s->current_picture.mb_type[mb_xy])
  5821. && IS_INTERLACED(s->current_picture.mb_type[mbn_xy])
  5822. ) {
  5823. // This is a special case in the norm where the filtering must
  5824. // be done twice (one each of the field) even if we are in a
  5825. // frame macroblock.
  5826. //
  5827. unsigned int tmp_linesize = 2 * linesize;
  5828. unsigned int tmp_uvlinesize = 2 * uvlinesize;
  5829. int mbn_xy = mb_xy - 2 * s->mb_stride;
  5830. int qp, chroma_qp;
  5831. // first filtering
  5832. if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) ||
  5833. IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) {
  5834. bS[0] = bS[1] = bS[2] = bS[3] = 3;
  5835. } else {
  5836. // TODO
  5837. av_log(h->s.avctx, AV_LOG_ERROR, "both non intra (TODO)\n");
  5838. }
  5839. /* Filter edge */
  5840. // Do not use s->qscale as luma quantizer because it has not the same
  5841. // value in IPCM macroblocks.
  5842. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  5843. 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);
  5844. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  5845. filter_mb_edgeh( h, &img_y[0], tmp_linesize, bS, qp );
  5846. chroma_qp = ( h->chroma_qp +
  5847. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  5848. filter_mb_edgech( h, &img_cb[0], tmp_uvlinesize, bS, chroma_qp );
  5849. filter_mb_edgech( h, &img_cr[0], tmp_uvlinesize, bS, chroma_qp );
  5850. // second filtering
  5851. mbn_xy += s->mb_stride;
  5852. if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) ||
  5853. IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) {
  5854. bS[0] = bS[1] = bS[2] = bS[3] = 3;
  5855. } else {
  5856. // TODO
  5857. av_log(h->s.avctx, AV_LOG_ERROR, "both non intra (TODO)\n");
  5858. }
  5859. /* Filter edge */
  5860. // Do not use s->qscale as luma quantizer because it has not the same
  5861. // value in IPCM macroblocks.
  5862. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  5863. 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);
  5864. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  5865. filter_mb_edgeh( h, &img_y[linesize], tmp_linesize, bS, qp );
  5866. chroma_qp = ( h->chroma_qp +
  5867. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  5868. filter_mb_edgech( h, &img_cb[uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  5869. filter_mb_edgech( h, &img_cr[uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  5870. continue;
  5871. }
  5872. if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) ||
  5873. IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) {
  5874. int value;
  5875. if (edge == 0) {
  5876. if ( (!IS_INTERLACED(s->current_picture.mb_type[mb_xy]) && !IS_INTERLACED(s->current_picture.mb_type[mbm_xy]))
  5877. || ((h->mb_aff_frame || (s->picture_structure != PICT_FRAME)) && (dir == 0))
  5878. ) {
  5879. value = 4;
  5880. } else {
  5881. value = 3;
  5882. }
  5883. } else {
  5884. value = 3;
  5885. }
  5886. bS[0] = bS[1] = bS[2] = bS[3] = value;
  5887. } else {
  5888. int i;
  5889. for( i = 0; i < 4; i++ ) {
  5890. int x = dir == 0 ? edge : i;
  5891. int y = dir == 0 ? i : edge;
  5892. int b_idx= 8 + 4 + x + 8*y;
  5893. int bn_idx= b_idx - (dir ? 8:1);
  5894. if( h->non_zero_count_cache[b_idx] != 0 ||
  5895. h->non_zero_count_cache[bn_idx] != 0 ) {
  5896. bS[i] = 2;
  5897. }
  5898. else
  5899. {
  5900. int l;
  5901. bS[i] = 0;
  5902. for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
  5903. if( ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  5904. ABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  5905. ABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= 4 ) {
  5906. bS[i] = 1;
  5907. break;
  5908. }
  5909. }
  5910. }
  5911. }
  5912. if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
  5913. continue;
  5914. }
  5915. /* Filter edge */
  5916. // Do not use s->qscale as luma quantizer because it has not the same
  5917. // value in IPCM macroblocks.
  5918. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  5919. //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]);
  5920. tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
  5921. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  5922. if( dir == 0 ) {
  5923. filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
  5924. if( (edge&1) == 0 ) {
  5925. int chroma_qp = ( h->chroma_qp +
  5926. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  5927. filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp );
  5928. filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp );
  5929. }
  5930. } else {
  5931. filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
  5932. if( (edge&1) == 0 ) {
  5933. int chroma_qp = ( h->chroma_qp +
  5934. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  5935. filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  5936. filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  5937. }
  5938. }
  5939. }
  5940. }
  5941. }
  5942. static int decode_slice(H264Context *h){
  5943. MpegEncContext * const s = &h->s;
  5944. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  5945. s->mb_skip_run= -1;
  5946. if( h->pps.cabac ) {
  5947. int i;
  5948. /* realign */
  5949. align_get_bits( &s->gb );
  5950. /* init cabac */
  5951. ff_init_cabac_states( &h->cabac, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64 );
  5952. ff_init_cabac_decoder( &h->cabac,
  5953. s->gb.buffer + get_bits_count(&s->gb)/8,
  5954. ( s->gb.size_in_bits - get_bits_count(&s->gb) + 7)/8);
  5955. /* calculate pre-state */
  5956. for( i= 0; i < 460; i++ ) {
  5957. int pre;
  5958. if( h->slice_type == I_TYPE )
  5959. pre = clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
  5960. else
  5961. 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 );
  5962. if( pre <= 63 )
  5963. h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
  5964. else
  5965. h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
  5966. }
  5967. for(;;){
  5968. int ret = decode_mb_cabac(h);
  5969. int eos;
  5970. if(ret>=0) hl_decode_mb(h);
  5971. /* XXX: useless as decode_mb_cabac it doesn't support that ... */
  5972. if( ret >= 0 && h->mb_aff_frame ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  5973. s->mb_y++;
  5974. if(ret>=0) ret = decode_mb_cabac(h);
  5975. if(ret>=0) hl_decode_mb(h);
  5976. s->mb_y--;
  5977. }
  5978. eos = get_cabac_terminate( &h->cabac );
  5979. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 1) {
  5980. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  5981. 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);
  5982. return -1;
  5983. }
  5984. if( ++s->mb_x >= s->mb_width ) {
  5985. s->mb_x = 0;
  5986. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  5987. ++s->mb_y;
  5988. if(h->mb_aff_frame) {
  5989. ++s->mb_y;
  5990. }
  5991. }
  5992. if( eos || s->mb_y >= s->mb_height ) {
  5993. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  5994. 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);
  5995. return 0;
  5996. }
  5997. }
  5998. } else {
  5999. for(;;){
  6000. int ret = decode_mb_cavlc(h);
  6001. if(ret>=0) hl_decode_mb(h);
  6002. if(ret>=0 && h->mb_aff_frame){ //FIXME optimal? or let mb_decode decode 16x32 ?
  6003. s->mb_y++;
  6004. ret = decode_mb_cavlc(h);
  6005. if(ret>=0) hl_decode_mb(h);
  6006. s->mb_y--;
  6007. }
  6008. if(ret<0){
  6009. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6010. 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);
  6011. return -1;
  6012. }
  6013. if(++s->mb_x >= s->mb_width){
  6014. s->mb_x=0;
  6015. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6016. ++s->mb_y;
  6017. if(h->mb_aff_frame) {
  6018. ++s->mb_y;
  6019. }
  6020. if(s->mb_y >= s->mb_height){
  6021. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6022. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  6023. 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);
  6024. return 0;
  6025. }else{
  6026. 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);
  6027. return -1;
  6028. }
  6029. }
  6030. }
  6031. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  6032. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6033. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  6034. 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);
  6035. return 0;
  6036. }else{
  6037. 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);
  6038. return -1;
  6039. }
  6040. }
  6041. }
  6042. }
  6043. #if 0
  6044. for(;s->mb_y < s->mb_height; s->mb_y++){
  6045. for(;s->mb_x < s->mb_width; s->mb_x++){
  6046. int ret= decode_mb(h);
  6047. hl_decode_mb(h);
  6048. if(ret<0){
  6049. fprintf(stderr, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6050. 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);
  6051. return -1;
  6052. }
  6053. if(++s->mb_x >= s->mb_width){
  6054. s->mb_x=0;
  6055. if(++s->mb_y >= s->mb_height){
  6056. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6057. 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);
  6058. return 0;
  6059. }else{
  6060. 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);
  6061. return -1;
  6062. }
  6063. }
  6064. }
  6065. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  6066. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6067. 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);
  6068. return 0;
  6069. }else{
  6070. 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);
  6071. return -1;
  6072. }
  6073. }
  6074. }
  6075. s->mb_x=0;
  6076. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6077. }
  6078. #endif
  6079. return -1; //not reached
  6080. }
  6081. static int decode_unregistered_user_data(H264Context *h, int size){
  6082. MpegEncContext * const s = &h->s;
  6083. uint8_t user_data[16+256];
  6084. int e, build, i;
  6085. if(size<16)
  6086. return -1;
  6087. for(i=0; i<sizeof(user_data)-1 && i<size; i++){
  6088. user_data[i]= get_bits(&s->gb, 8);
  6089. }
  6090. user_data[i]= 0;
  6091. e= sscanf(user_data+16, "x264 - core %d"/*%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html*/, &build);
  6092. if(e==1 && build>=0)
  6093. h->x264_build= build;
  6094. if(s->avctx->debug & FF_DEBUG_BUGS)
  6095. av_log(s->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data+16);
  6096. for(; i<size; i++)
  6097. skip_bits(&s->gb, 8);
  6098. return 0;
  6099. }
  6100. static int decode_sei(H264Context *h){
  6101. MpegEncContext * const s = &h->s;
  6102. while(get_bits_count(&s->gb) + 16 < s->gb.size_in_bits){
  6103. int size, type;
  6104. type=0;
  6105. do{
  6106. type+= show_bits(&s->gb, 8);
  6107. }while(get_bits(&s->gb, 8) == 255);
  6108. size=0;
  6109. do{
  6110. size+= show_bits(&s->gb, 8);
  6111. }while(get_bits(&s->gb, 8) == 255);
  6112. switch(type){
  6113. case 5:
  6114. if(decode_unregistered_user_data(h, size) < 0);
  6115. return -1;
  6116. break;
  6117. default:
  6118. skip_bits(&s->gb, 8*size);
  6119. }
  6120. //FIXME check bits here
  6121. align_get_bits(&s->gb);
  6122. }
  6123. return 0;
  6124. }
  6125. static inline void decode_hrd_parameters(H264Context *h, SPS *sps){
  6126. MpegEncContext * const s = &h->s;
  6127. int cpb_count, i;
  6128. cpb_count = get_ue_golomb(&s->gb) + 1;
  6129. get_bits(&s->gb, 4); /* bit_rate_scale */
  6130. get_bits(&s->gb, 4); /* cpb_size_scale */
  6131. for(i=0; i<cpb_count; i++){
  6132. get_ue_golomb(&s->gb); /* bit_rate_value_minus1 */
  6133. get_ue_golomb(&s->gb); /* cpb_size_value_minus1 */
  6134. get_bits1(&s->gb); /* cbr_flag */
  6135. }
  6136. get_bits(&s->gb, 5); /* initial_cpb_removal_delay_length_minus1 */
  6137. get_bits(&s->gb, 5); /* cpb_removal_delay_length_minus1 */
  6138. get_bits(&s->gb, 5); /* dpb_output_delay_length_minus1 */
  6139. get_bits(&s->gb, 5); /* time_offset_length */
  6140. }
  6141. static inline int decode_vui_parameters(H264Context *h, SPS *sps){
  6142. MpegEncContext * const s = &h->s;
  6143. int aspect_ratio_info_present_flag, aspect_ratio_idc;
  6144. int nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag;
  6145. aspect_ratio_info_present_flag= get_bits1(&s->gb);
  6146. if( aspect_ratio_info_present_flag ) {
  6147. aspect_ratio_idc= get_bits(&s->gb, 8);
  6148. if( aspect_ratio_idc == EXTENDED_SAR ) {
  6149. sps->sar.num= get_bits(&s->gb, 16);
  6150. sps->sar.den= get_bits(&s->gb, 16);
  6151. }else if(aspect_ratio_idc < 16){
  6152. sps->sar= pixel_aspect[aspect_ratio_idc];
  6153. }else{
  6154. av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
  6155. return -1;
  6156. }
  6157. }else{
  6158. sps->sar.num=
  6159. sps->sar.den= 0;
  6160. }
  6161. // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
  6162. if(get_bits1(&s->gb)){ /* overscan_info_present_flag */
  6163. get_bits1(&s->gb); /* overscan_appropriate_flag */
  6164. }
  6165. if(get_bits1(&s->gb)){ /* video_signal_type_present_flag */
  6166. get_bits(&s->gb, 3); /* video_format */
  6167. get_bits1(&s->gb); /* video_full_range_flag */
  6168. if(get_bits1(&s->gb)){ /* colour_description_present_flag */
  6169. get_bits(&s->gb, 8); /* colour_primaries */
  6170. get_bits(&s->gb, 8); /* transfer_characteristics */
  6171. get_bits(&s->gb, 8); /* matrix_coefficients */
  6172. }
  6173. }
  6174. if(get_bits1(&s->gb)){ /* chroma_location_info_present_flag */
  6175. get_ue_golomb(&s->gb); /* chroma_sample_location_type_top_field */
  6176. get_ue_golomb(&s->gb); /* chroma_sample_location_type_bottom_field */
  6177. }
  6178. sps->timing_info_present_flag = get_bits1(&s->gb);
  6179. if(sps->timing_info_present_flag){
  6180. sps->num_units_in_tick = get_bits_long(&s->gb, 32);
  6181. sps->time_scale = get_bits_long(&s->gb, 32);
  6182. sps->fixed_frame_rate_flag = get_bits1(&s->gb);
  6183. }
  6184. nal_hrd_parameters_present_flag = get_bits1(&s->gb);
  6185. if(nal_hrd_parameters_present_flag)
  6186. decode_hrd_parameters(h, sps);
  6187. vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
  6188. if(vcl_hrd_parameters_present_flag)
  6189. decode_hrd_parameters(h, sps);
  6190. if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag)
  6191. get_bits1(&s->gb); /* low_delay_hrd_flag */
  6192. get_bits1(&s->gb); /* pic_struct_present_flag */
  6193. sps->bitstream_restriction_flag = get_bits1(&s->gb);
  6194. if(sps->bitstream_restriction_flag){
  6195. get_bits1(&s->gb); /* motion_vectors_over_pic_boundaries_flag */
  6196. get_ue_golomb(&s->gb); /* max_bytes_per_pic_denom */
  6197. get_ue_golomb(&s->gb); /* max_bits_per_mb_denom */
  6198. get_ue_golomb(&s->gb); /* log2_max_mv_length_horizontal */
  6199. get_ue_golomb(&s->gb); /* log2_max_mv_length_vertical */
  6200. sps->num_reorder_frames = get_ue_golomb(&s->gb);
  6201. get_ue_golomb(&s->gb); /* max_dec_frame_buffering */
  6202. }
  6203. return 0;
  6204. }
  6205. static void decode_scaling_list(H264Context *h, uint8_t *factors, int size, const uint8_t *default_list){
  6206. MpegEncContext * const s = &h->s;
  6207. int i, last = 8, next = 8;
  6208. const uint8_t *scan = size == 16 ? zigzag_scan : zigzag_scan8x8;
  6209. if(!get_bits1(&s->gb)) /* matrix not written, we use the default one */
  6210. memcpy(factors, default_list, size*sizeof(uint8_t));
  6211. else
  6212. for(i=0;i<size;i++){
  6213. if(next)
  6214. next = (last + get_se_golomb(&s->gb)) & 0xff;
  6215. if(!i && !next){ /* matrix not written, we use the default one */
  6216. memcpy(factors, default_list, size*sizeof(uint8_t));
  6217. break;
  6218. }
  6219. last = factors[scan[i]] = next ? next : last;
  6220. }
  6221. }
  6222. static void decode_scaling_matrices(H264Context *h, SPS *sps, PPS *pps, int is_sps,
  6223. uint8_t (*scaling_matrix4)[16], uint8_t (*scaling_matrix8)[64]){
  6224. MpegEncContext * const s = &h->s;
  6225. int fallback_sps = !is_sps && sps->scaling_matrix_present;
  6226. const uint8_t *fallback[4] = {
  6227. fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
  6228. fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
  6229. fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
  6230. fallback_sps ? sps->scaling_matrix8[1] : default_scaling8[1]
  6231. };
  6232. if(get_bits1(&s->gb)){
  6233. sps->scaling_matrix_present |= is_sps;
  6234. decode_scaling_list(h,scaling_matrix4[0],16,fallback[0]); // Intra, Y
  6235. decode_scaling_list(h,scaling_matrix4[1],16,scaling_matrix4[0]); // Intra, Cr
  6236. decode_scaling_list(h,scaling_matrix4[2],16,scaling_matrix4[1]); // Intra, Cb
  6237. decode_scaling_list(h,scaling_matrix4[3],16,fallback[1]); // Inter, Y
  6238. decode_scaling_list(h,scaling_matrix4[4],16,scaling_matrix4[3]); // Inter, Cr
  6239. decode_scaling_list(h,scaling_matrix4[5],16,scaling_matrix4[4]); // Inter, Cb
  6240. if(is_sps || pps->transform_8x8_mode){
  6241. decode_scaling_list(h,scaling_matrix8[0],64,fallback[2]); // Intra, Y
  6242. decode_scaling_list(h,scaling_matrix8[1],64,fallback[3]); // Inter, Y
  6243. }
  6244. } else if(fallback_sps) {
  6245. memcpy(scaling_matrix4, sps->scaling_matrix4, 6*16*sizeof(uint8_t));
  6246. memcpy(scaling_matrix8, sps->scaling_matrix8, 2*64*sizeof(uint8_t));
  6247. }
  6248. }
  6249. static inline int decode_seq_parameter_set(H264Context *h){
  6250. MpegEncContext * const s = &h->s;
  6251. int profile_idc, level_idc;
  6252. int sps_id, i;
  6253. SPS *sps;
  6254. profile_idc= get_bits(&s->gb, 8);
  6255. get_bits1(&s->gb); //constraint_set0_flag
  6256. get_bits1(&s->gb); //constraint_set1_flag
  6257. get_bits1(&s->gb); //constraint_set2_flag
  6258. get_bits1(&s->gb); //constraint_set3_flag
  6259. get_bits(&s->gb, 4); // reserved
  6260. level_idc= get_bits(&s->gb, 8);
  6261. sps_id= get_ue_golomb(&s->gb);
  6262. sps= &h->sps_buffer[ sps_id ];
  6263. sps->profile_idc= profile_idc;
  6264. sps->level_idc= level_idc;
  6265. if(sps->profile_idc >= 100){ //high profile
  6266. if(get_ue_golomb(&s->gb) == 3) //chroma_format_idc
  6267. get_bits1(&s->gb); //residual_color_transform_flag
  6268. get_ue_golomb(&s->gb); //bit_depth_luma_minus8
  6269. get_ue_golomb(&s->gb); //bit_depth_chroma_minus8
  6270. sps->transform_bypass = get_bits1(&s->gb);
  6271. decode_scaling_matrices(h, sps, NULL, 1, sps->scaling_matrix4, sps->scaling_matrix8);
  6272. }else
  6273. sps->scaling_matrix_present = 0;
  6274. sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
  6275. sps->poc_type= get_ue_golomb(&s->gb);
  6276. if(sps->poc_type == 0){ //FIXME #define
  6277. sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
  6278. } else if(sps->poc_type == 1){//FIXME #define
  6279. sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
  6280. sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
  6281. sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
  6282. sps->poc_cycle_length= get_ue_golomb(&s->gb);
  6283. for(i=0; i<sps->poc_cycle_length; i++)
  6284. sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
  6285. }
  6286. if(sps->poc_type > 2){
  6287. av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
  6288. return -1;
  6289. }
  6290. sps->ref_frame_count= get_ue_golomb(&s->gb);
  6291. if(sps->ref_frame_count > MAX_PICTURE_COUNT-2){
  6292. av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");
  6293. }
  6294. sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
  6295. sps->mb_width= get_ue_golomb(&s->gb) + 1;
  6296. sps->mb_height= get_ue_golomb(&s->gb) + 1;
  6297. if((unsigned)sps->mb_width >= INT_MAX/16 || (unsigned)sps->mb_height >= INT_MAX/16 ||
  6298. avcodec_check_dimensions(NULL, 16*sps->mb_width, 16*sps->mb_height))
  6299. return -1;
  6300. sps->frame_mbs_only_flag= get_bits1(&s->gb);
  6301. if(!sps->frame_mbs_only_flag)
  6302. sps->mb_aff= get_bits1(&s->gb);
  6303. else
  6304. sps->mb_aff= 0;
  6305. sps->direct_8x8_inference_flag= get_bits1(&s->gb);
  6306. sps->crop= get_bits1(&s->gb);
  6307. if(sps->crop){
  6308. sps->crop_left = get_ue_golomb(&s->gb);
  6309. sps->crop_right = get_ue_golomb(&s->gb);
  6310. sps->crop_top = get_ue_golomb(&s->gb);
  6311. sps->crop_bottom= get_ue_golomb(&s->gb);
  6312. if(sps->crop_left || sps->crop_top){
  6313. av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ...\n");
  6314. }
  6315. }else{
  6316. sps->crop_left =
  6317. sps->crop_right =
  6318. sps->crop_top =
  6319. sps->crop_bottom= 0;
  6320. }
  6321. sps->vui_parameters_present_flag= get_bits1(&s->gb);
  6322. if( sps->vui_parameters_present_flag )
  6323. decode_vui_parameters(h, sps);
  6324. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6325. 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",
  6326. sps_id, sps->profile_idc, sps->level_idc,
  6327. sps->poc_type,
  6328. sps->ref_frame_count,
  6329. sps->mb_width, sps->mb_height,
  6330. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  6331. sps->direct_8x8_inference_flag ? "8B8" : "",
  6332. sps->crop_left, sps->crop_right,
  6333. sps->crop_top, sps->crop_bottom,
  6334. sps->vui_parameters_present_flag ? "VUI" : ""
  6335. );
  6336. }
  6337. return 0;
  6338. }
  6339. static inline int decode_picture_parameter_set(H264Context *h, int bit_length){
  6340. MpegEncContext * const s = &h->s;
  6341. int pps_id= get_ue_golomb(&s->gb);
  6342. PPS *pps= &h->pps_buffer[pps_id];
  6343. pps->sps_id= get_ue_golomb(&s->gb);
  6344. pps->cabac= get_bits1(&s->gb);
  6345. pps->pic_order_present= get_bits1(&s->gb);
  6346. pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
  6347. if(pps->slice_group_count > 1 ){
  6348. pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
  6349. av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
  6350. switch(pps->mb_slice_group_map_type){
  6351. case 0:
  6352. #if 0
  6353. | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
  6354. | run_length[ i ] |1 |ue(v) |
  6355. #endif
  6356. break;
  6357. case 2:
  6358. #if 0
  6359. | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
  6360. |{ | | |
  6361. | top_left_mb[ i ] |1 |ue(v) |
  6362. | bottom_right_mb[ i ] |1 |ue(v) |
  6363. | } | | |
  6364. #endif
  6365. break;
  6366. case 3:
  6367. case 4:
  6368. case 5:
  6369. #if 0
  6370. | slice_group_change_direction_flag |1 |u(1) |
  6371. | slice_group_change_rate_minus1 |1 |ue(v) |
  6372. #endif
  6373. break;
  6374. case 6:
  6375. #if 0
  6376. | slice_group_id_cnt_minus1 |1 |ue(v) |
  6377. | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
  6378. |) | | |
  6379. | slice_group_id[ i ] |1 |u(v) |
  6380. #endif
  6381. break;
  6382. }
  6383. }
  6384. pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  6385. pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  6386. if(pps->ref_count[0] > 32 || pps->ref_count[1] > 32){
  6387. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
  6388. return -1;
  6389. }
  6390. pps->weighted_pred= get_bits1(&s->gb);
  6391. pps->weighted_bipred_idc= get_bits(&s->gb, 2);
  6392. pps->init_qp= get_se_golomb(&s->gb) + 26;
  6393. pps->init_qs= get_se_golomb(&s->gb) + 26;
  6394. pps->chroma_qp_index_offset= get_se_golomb(&s->gb);
  6395. pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
  6396. pps->constrained_intra_pred= get_bits1(&s->gb);
  6397. pps->redundant_pic_cnt_present = get_bits1(&s->gb);
  6398. memset(pps->scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  6399. memset(pps->scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  6400. if(get_bits_count(&s->gb) < bit_length){
  6401. pps->transform_8x8_mode= get_bits1(&s->gb);
  6402. decode_scaling_matrices(h, &h->sps_buffer[pps->sps_id], pps, 0, pps->scaling_matrix4, pps->scaling_matrix8);
  6403. get_se_golomb(&s->gb); //second_chroma_qp_index_offset
  6404. }
  6405. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6406. 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",
  6407. pps_id, pps->sps_id,
  6408. pps->cabac ? "CABAC" : "CAVLC",
  6409. pps->slice_group_count,
  6410. pps->ref_count[0], pps->ref_count[1],
  6411. pps->weighted_pred ? "weighted" : "",
  6412. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset,
  6413. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  6414. pps->constrained_intra_pred ? "CONSTR" : "",
  6415. pps->redundant_pic_cnt_present ? "REDU" : "",
  6416. pps->transform_8x8_mode ? "8x8DCT" : ""
  6417. );
  6418. }
  6419. return 0;
  6420. }
  6421. /**
  6422. * finds the end of the current frame in the bitstream.
  6423. * @return the position of the first byte of the next frame, or -1
  6424. */
  6425. static int find_frame_end(H264Context *h, const uint8_t *buf, int buf_size){
  6426. int i;
  6427. uint32_t state;
  6428. ParseContext *pc = &(h->s.parse_context);
  6429. //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
  6430. // mb_addr= pc->mb_addr - 1;
  6431. state= pc->state;
  6432. for(i=0; i<=buf_size; i++){
  6433. if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  6434. tprintf("find_frame_end new startcode = %08x, frame_start_found = %d, pos = %d\n", state, pc->frame_start_found, i);
  6435. if(pc->frame_start_found){
  6436. // If there isn't one more byte in the buffer
  6437. // the test on first_mb_in_slice cannot be done yet
  6438. // do it at next call.
  6439. if (i >= buf_size) break;
  6440. if (buf[i] & 0x80) {
  6441. // first_mb_in_slice is 0, probably the first nal of a new
  6442. // slice
  6443. tprintf("find_frame_end frame_end_found, state = %08x, pos = %d\n", state, i);
  6444. pc->state=-1;
  6445. pc->frame_start_found= 0;
  6446. return i-4;
  6447. }
  6448. }
  6449. pc->frame_start_found = 1;
  6450. }
  6451. if((state&0xFFFFFF1F) == 0x107 || (state&0xFFFFFF1F) == 0x108 || (state&0xFFFFFF1F) == 0x109){
  6452. if(pc->frame_start_found){
  6453. pc->state=-1;
  6454. pc->frame_start_found= 0;
  6455. return i-4;
  6456. }
  6457. }
  6458. if (i<buf_size)
  6459. state= (state<<8) | buf[i];
  6460. }
  6461. pc->state= state;
  6462. return END_NOT_FOUND;
  6463. }
  6464. static int h264_parse(AVCodecParserContext *s,
  6465. AVCodecContext *avctx,
  6466. uint8_t **poutbuf, int *poutbuf_size,
  6467. const uint8_t *buf, int buf_size)
  6468. {
  6469. H264Context *h = s->priv_data;
  6470. ParseContext *pc = &h->s.parse_context;
  6471. int next;
  6472. next= find_frame_end(h, buf, buf_size);
  6473. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  6474. *poutbuf = NULL;
  6475. *poutbuf_size = 0;
  6476. return buf_size;
  6477. }
  6478. *poutbuf = (uint8_t *)buf;
  6479. *poutbuf_size = buf_size;
  6480. return next;
  6481. }
  6482. static int h264_split(AVCodecContext *avctx,
  6483. const uint8_t *buf, int buf_size)
  6484. {
  6485. int i;
  6486. uint32_t state = -1;
  6487. int has_sps= 0;
  6488. for(i=0; i<=buf_size; i++){
  6489. if((state&0xFFFFFF1F) == 0x107)
  6490. has_sps=1;
  6491. /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  6492. }*/
  6493. if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
  6494. if(has_sps){
  6495. while(i>4 && buf[i-5]==0) i--;
  6496. return i-4;
  6497. }
  6498. }
  6499. if (i<buf_size)
  6500. state= (state<<8) | buf[i];
  6501. }
  6502. return 0;
  6503. }
  6504. static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){
  6505. MpegEncContext * const s = &h->s;
  6506. AVCodecContext * const avctx= s->avctx;
  6507. int buf_index=0;
  6508. #if 0
  6509. int i;
  6510. for(i=0; i<50; i++){
  6511. av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
  6512. }
  6513. #endif
  6514. h->slice_num = 0;
  6515. s->current_picture_ptr= NULL;
  6516. for(;;){
  6517. int consumed;
  6518. int dst_length;
  6519. int bit_length;
  6520. uint8_t *ptr;
  6521. int i, nalsize = 0;
  6522. if(h->is_avc) {
  6523. if(buf_index >= buf_size) break;
  6524. nalsize = 0;
  6525. for(i = 0; i < h->nal_length_size; i++)
  6526. nalsize = (nalsize << 8) | buf[buf_index++];
  6527. } else {
  6528. // start code prefix search
  6529. for(; buf_index + 3 < buf_size; buf_index++){
  6530. // this should allways succeed in the first iteration
  6531. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  6532. break;
  6533. }
  6534. if(buf_index+3 >= buf_size) break;
  6535. buf_index+=3;
  6536. }
  6537. ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, h->is_avc ? nalsize : buf_size - buf_index);
  6538. if(ptr[dst_length - 1] == 0) dst_length--;
  6539. bit_length= 8*dst_length - decode_rbsp_trailing(ptr + dst_length - 1);
  6540. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  6541. 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);
  6542. }
  6543. if (h->is_avc && (nalsize != consumed))
  6544. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  6545. buf_index += consumed;
  6546. if( (s->hurry_up == 1 && h->nal_ref_idc == 0) //FIXME dont discard SEI id
  6547. ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  6548. continue;
  6549. switch(h->nal_unit_type){
  6550. case NAL_IDR_SLICE:
  6551. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  6552. case NAL_SLICE:
  6553. init_get_bits(&s->gb, ptr, bit_length);
  6554. h->intra_gb_ptr=
  6555. h->inter_gb_ptr= &s->gb;
  6556. s->data_partitioning = 0;
  6557. if(decode_slice_header(h) < 0){
  6558. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  6559. break;
  6560. }
  6561. if(h->redundant_pic_count==0 && s->hurry_up < 5
  6562. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  6563. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  6564. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  6565. && avctx->skip_frame < AVDISCARD_ALL)
  6566. decode_slice(h);
  6567. break;
  6568. case NAL_DPA:
  6569. init_get_bits(&s->gb, ptr, bit_length);
  6570. h->intra_gb_ptr=
  6571. h->inter_gb_ptr= NULL;
  6572. s->data_partitioning = 1;
  6573. if(decode_slice_header(h) < 0){
  6574. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  6575. }
  6576. break;
  6577. case NAL_DPB:
  6578. init_get_bits(&h->intra_gb, ptr, bit_length);
  6579. h->intra_gb_ptr= &h->intra_gb;
  6580. break;
  6581. case NAL_DPC:
  6582. init_get_bits(&h->inter_gb, ptr, bit_length);
  6583. h->inter_gb_ptr= &h->inter_gb;
  6584. if(h->redundant_pic_count==0 && h->intra_gb_ptr && s->data_partitioning
  6585. && s->hurry_up < 5
  6586. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  6587. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  6588. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  6589. && avctx->skip_frame < AVDISCARD_ALL)
  6590. decode_slice(h);
  6591. break;
  6592. case NAL_SEI:
  6593. init_get_bits(&s->gb, ptr, bit_length);
  6594. decode_sei(h);
  6595. break;
  6596. case NAL_SPS:
  6597. init_get_bits(&s->gb, ptr, bit_length);
  6598. decode_seq_parameter_set(h);
  6599. if(s->flags& CODEC_FLAG_LOW_DELAY)
  6600. s->low_delay=1;
  6601. if(avctx->has_b_frames < 2)
  6602. avctx->has_b_frames= !s->low_delay;
  6603. break;
  6604. case NAL_PPS:
  6605. init_get_bits(&s->gb, ptr, bit_length);
  6606. decode_picture_parameter_set(h, bit_length);
  6607. break;
  6608. case NAL_PICTURE_DELIMITER:
  6609. break;
  6610. case NAL_FILTER_DATA:
  6611. break;
  6612. default:
  6613. av_log(avctx, AV_LOG_ERROR, "Unknown NAL code: %d\n", h->nal_unit_type);
  6614. }
  6615. }
  6616. if(!s->current_picture_ptr) return buf_index; //no frame
  6617. s->current_picture_ptr->pict_type= s->pict_type;
  6618. s->current_picture_ptr->key_frame= s->pict_type == I_TYPE && h->nal_unit_type == NAL_IDR_SLICE;
  6619. h->prev_frame_num_offset= h->frame_num_offset;
  6620. h->prev_frame_num= h->frame_num;
  6621. if(s->current_picture_ptr->reference){
  6622. h->prev_poc_msb= h->poc_msb;
  6623. h->prev_poc_lsb= h->poc_lsb;
  6624. }
  6625. if(s->current_picture_ptr->reference)
  6626. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  6627. ff_er_frame_end(s);
  6628. MPV_frame_end(s);
  6629. return buf_index;
  6630. }
  6631. /**
  6632. * returns the number of bytes consumed for building the current frame
  6633. */
  6634. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  6635. if(s->flags&CODEC_FLAG_TRUNCATED){
  6636. pos -= s->parse_context.last_index;
  6637. if(pos<0) pos=0; // FIXME remove (unneeded?)
  6638. return pos;
  6639. }else{
  6640. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  6641. if(pos+10>buf_size) pos=buf_size; // oops ;)
  6642. return pos;
  6643. }
  6644. }
  6645. static int decode_frame(AVCodecContext *avctx,
  6646. void *data, int *data_size,
  6647. uint8_t *buf, int buf_size)
  6648. {
  6649. H264Context *h = avctx->priv_data;
  6650. MpegEncContext *s = &h->s;
  6651. AVFrame *pict = data;
  6652. int buf_index;
  6653. s->flags= avctx->flags;
  6654. s->flags2= avctx->flags2;
  6655. /* no supplementary picture */
  6656. if (buf_size == 0) {
  6657. return 0;
  6658. }
  6659. if(s->flags&CODEC_FLAG_TRUNCATED){
  6660. int next= find_frame_end(h, buf, buf_size);
  6661. if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
  6662. return buf_size;
  6663. //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);
  6664. }
  6665. if(h->is_avc && !h->got_avcC) {
  6666. int i, cnt, nalsize;
  6667. unsigned char *p = avctx->extradata;
  6668. if(avctx->extradata_size < 7) {
  6669. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  6670. return -1;
  6671. }
  6672. if(*p != 1) {
  6673. av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
  6674. return -1;
  6675. }
  6676. /* sps and pps in the avcC always have length coded with 2 bytes,
  6677. so put a fake nal_length_size = 2 while parsing them */
  6678. h->nal_length_size = 2;
  6679. // Decode sps from avcC
  6680. cnt = *(p+5) & 0x1f; // Number of sps
  6681. p += 6;
  6682. for (i = 0; i < cnt; i++) {
  6683. nalsize = BE_16(p) + 2;
  6684. if(decode_nal_units(h, p, nalsize) < 0) {
  6685. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  6686. return -1;
  6687. }
  6688. p += nalsize;
  6689. }
  6690. // Decode pps from avcC
  6691. cnt = *(p++); // Number of pps
  6692. for (i = 0; i < cnt; i++) {
  6693. nalsize = BE_16(p) + 2;
  6694. if(decode_nal_units(h, p, nalsize) != nalsize) {
  6695. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  6696. return -1;
  6697. }
  6698. p += nalsize;
  6699. }
  6700. // Now store right nal length size, that will be use to parse all other nals
  6701. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  6702. // Do not reparse avcC
  6703. h->got_avcC = 1;
  6704. }
  6705. if(!h->is_avc && s->avctx->extradata_size && s->picture_number==0){
  6706. if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
  6707. return -1;
  6708. }
  6709. buf_index=decode_nal_units(h, buf, buf_size);
  6710. if(buf_index < 0)
  6711. return -1;
  6712. //FIXME do something with unavailable reference frames
  6713. // if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_index, buf_size);
  6714. if(!s->current_picture_ptr){
  6715. av_log(h->s.avctx, AV_LOG_DEBUG, "error, NO frame\n");
  6716. return -1;
  6717. }
  6718. {
  6719. Picture *out = s->current_picture_ptr;
  6720. #if 0 //decode order
  6721. *data_size = sizeof(AVFrame);
  6722. #else
  6723. /* Sort B-frames into display order */
  6724. Picture *cur = s->current_picture_ptr;
  6725. Picture *prev = h->delayed_output_pic;
  6726. int out_idx = 0;
  6727. int pics = 0;
  6728. int out_of_order;
  6729. int cross_idr = 0;
  6730. int dropped_frame = 0;
  6731. int i;
  6732. if(h->sps.bitstream_restriction_flag
  6733. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  6734. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  6735. s->low_delay = 0;
  6736. }
  6737. while(h->delayed_pic[pics]) pics++;
  6738. h->delayed_pic[pics++] = cur;
  6739. if(cur->reference == 0)
  6740. cur->reference = 1;
  6741. for(i=0; h->delayed_pic[i]; i++)
  6742. if(h->delayed_pic[i]->key_frame || h->delayed_pic[i]->poc==0)
  6743. cross_idr = 1;
  6744. out = h->delayed_pic[0];
  6745. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
  6746. if(h->delayed_pic[i]->poc < out->poc){
  6747. out = h->delayed_pic[i];
  6748. out_idx = i;
  6749. }
  6750. out_of_order = !cross_idr && prev && out->poc < prev->poc;
  6751. if(prev && pics <= s->avctx->has_b_frames)
  6752. out = prev;
  6753. else if((out_of_order && pics-1 == s->avctx->has_b_frames)
  6754. || (s->low_delay &&
  6755. ((!cross_idr && prev && out->poc > prev->poc + 2)
  6756. || cur->pict_type == B_TYPE)))
  6757. {
  6758. s->low_delay = 0;
  6759. s->avctx->has_b_frames++;
  6760. out = prev;
  6761. }
  6762. else if(out_of_order)
  6763. out = prev;
  6764. if(out_of_order || pics > s->avctx->has_b_frames){
  6765. dropped_frame = (out != h->delayed_pic[out_idx]);
  6766. for(i=out_idx; h->delayed_pic[i]; i++)
  6767. h->delayed_pic[i] = h->delayed_pic[i+1];
  6768. }
  6769. if(prev == out && !dropped_frame)
  6770. *data_size = 0;
  6771. else
  6772. *data_size = sizeof(AVFrame);
  6773. if(prev && prev != out && prev->reference == 1)
  6774. prev->reference = 0;
  6775. h->delayed_output_pic = out;
  6776. #endif
  6777. *pict= *(AVFrame*)out;
  6778. }
  6779. assert(pict->data[0]);
  6780. ff_print_debug_info(s, pict);
  6781. //printf("out %d\n", (int)pict->data[0]);
  6782. #if 0 //?
  6783. /* Return the Picture timestamp as the frame number */
  6784. /* we substract 1 because it is added on utils.c */
  6785. avctx->frame_number = s->picture_number - 1;
  6786. #endif
  6787. return get_consumed_bytes(s, buf_index, buf_size);
  6788. }
  6789. #if 0
  6790. static inline void fill_mb_avail(H264Context *h){
  6791. MpegEncContext * const s = &h->s;
  6792. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  6793. if(s->mb_y){
  6794. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  6795. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  6796. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  6797. }else{
  6798. h->mb_avail[0]=
  6799. h->mb_avail[1]=
  6800. h->mb_avail[2]= 0;
  6801. }
  6802. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  6803. h->mb_avail[4]= 1; //FIXME move out
  6804. h->mb_avail[5]= 0; //FIXME move out
  6805. }
  6806. #endif
  6807. #if 0 //selftest
  6808. #define COUNT 8000
  6809. #define SIZE (COUNT*40)
  6810. int main(){
  6811. int i;
  6812. uint8_t temp[SIZE];
  6813. PutBitContext pb;
  6814. GetBitContext gb;
  6815. // int int_temp[10000];
  6816. DSPContext dsp;
  6817. AVCodecContext avctx;
  6818. dsputil_init(&dsp, &avctx);
  6819. init_put_bits(&pb, temp, SIZE);
  6820. printf("testing unsigned exp golomb\n");
  6821. for(i=0; i<COUNT; i++){
  6822. START_TIMER
  6823. set_ue_golomb(&pb, i);
  6824. STOP_TIMER("set_ue_golomb");
  6825. }
  6826. flush_put_bits(&pb);
  6827. init_get_bits(&gb, temp, 8*SIZE);
  6828. for(i=0; i<COUNT; i++){
  6829. int j, s;
  6830. s= show_bits(&gb, 24);
  6831. START_TIMER
  6832. j= get_ue_golomb(&gb);
  6833. if(j != i){
  6834. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  6835. // return -1;
  6836. }
  6837. STOP_TIMER("get_ue_golomb");
  6838. }
  6839. init_put_bits(&pb, temp, SIZE);
  6840. printf("testing signed exp golomb\n");
  6841. for(i=0; i<COUNT; i++){
  6842. START_TIMER
  6843. set_se_golomb(&pb, i - COUNT/2);
  6844. STOP_TIMER("set_se_golomb");
  6845. }
  6846. flush_put_bits(&pb);
  6847. init_get_bits(&gb, temp, 8*SIZE);
  6848. for(i=0; i<COUNT; i++){
  6849. int j, s;
  6850. s= show_bits(&gb, 24);
  6851. START_TIMER
  6852. j= get_se_golomb(&gb);
  6853. if(j != i - COUNT/2){
  6854. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  6855. // return -1;
  6856. }
  6857. STOP_TIMER("get_se_golomb");
  6858. }
  6859. printf("testing 4x4 (I)DCT\n");
  6860. DCTELEM block[16];
  6861. uint8_t src[16], ref[16];
  6862. uint64_t error= 0, max_error=0;
  6863. for(i=0; i<COUNT; i++){
  6864. int j;
  6865. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  6866. for(j=0; j<16; j++){
  6867. ref[j]= random()%255;
  6868. src[j]= random()%255;
  6869. }
  6870. h264_diff_dct_c(block, src, ref, 4);
  6871. //normalize
  6872. for(j=0; j<16; j++){
  6873. // printf("%d ", block[j]);
  6874. block[j]= block[j]*4;
  6875. if(j&1) block[j]= (block[j]*4 + 2)/5;
  6876. if(j&4) block[j]= (block[j]*4 + 2)/5;
  6877. }
  6878. // printf("\n");
  6879. s->dsp.h264_idct_add(ref, block, 4);
  6880. /* for(j=0; j<16; j++){
  6881. printf("%d ", ref[j]);
  6882. }
  6883. printf("\n");*/
  6884. for(j=0; j<16; j++){
  6885. int diff= ABS(src[j] - ref[j]);
  6886. error+= diff*diff;
  6887. max_error= FFMAX(max_error, diff);
  6888. }
  6889. }
  6890. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  6891. #if 0
  6892. printf("testing quantizer\n");
  6893. for(qp=0; qp<52; qp++){
  6894. for(i=0; i<16; i++)
  6895. src1_block[i]= src2_block[i]= random()%255;
  6896. }
  6897. #endif
  6898. printf("Testing NAL layer\n");
  6899. uint8_t bitstream[COUNT];
  6900. uint8_t nal[COUNT*2];
  6901. H264Context h;
  6902. memset(&h, 0, sizeof(H264Context));
  6903. for(i=0; i<COUNT; i++){
  6904. int zeros= i;
  6905. int nal_length;
  6906. int consumed;
  6907. int out_length;
  6908. uint8_t *out;
  6909. int j;
  6910. for(j=0; j<COUNT; j++){
  6911. bitstream[j]= (random() % 255) + 1;
  6912. }
  6913. for(j=0; j<zeros; j++){
  6914. int pos= random() % COUNT;
  6915. while(bitstream[pos] == 0){
  6916. pos++;
  6917. pos %= COUNT;
  6918. }
  6919. bitstream[pos]=0;
  6920. }
  6921. START_TIMER
  6922. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  6923. if(nal_length<0){
  6924. printf("encoding failed\n");
  6925. return -1;
  6926. }
  6927. out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
  6928. STOP_TIMER("NAL")
  6929. if(out_length != COUNT){
  6930. printf("incorrect length %d %d\n", out_length, COUNT);
  6931. return -1;
  6932. }
  6933. if(consumed != nal_length){
  6934. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  6935. return -1;
  6936. }
  6937. if(memcmp(bitstream, out, COUNT)){
  6938. printf("missmatch\n");
  6939. return -1;
  6940. }
  6941. }
  6942. printf("Testing RBSP\n");
  6943. return 0;
  6944. }
  6945. #endif
  6946. static int decode_end(AVCodecContext *avctx)
  6947. {
  6948. H264Context *h = avctx->priv_data;
  6949. MpegEncContext *s = &h->s;
  6950. av_freep(&h->rbsp_buffer);
  6951. free_tables(h); //FIXME cleanup init stuff perhaps
  6952. MPV_common_end(s);
  6953. // memset(h, 0, sizeof(H264Context));
  6954. return 0;
  6955. }
  6956. AVCodec h264_decoder = {
  6957. "h264",
  6958. CODEC_TYPE_VIDEO,
  6959. CODEC_ID_H264,
  6960. sizeof(H264Context),
  6961. decode_init,
  6962. NULL,
  6963. decode_end,
  6964. decode_frame,
  6965. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  6966. .flush= flush_dpb,
  6967. };
  6968. AVCodecParser h264_parser = {
  6969. { CODEC_ID_H264 },
  6970. sizeof(H264Context),
  6971. NULL,
  6972. h264_parse,
  6973. ff_parse_close,
  6974. h264_split,
  6975. };
  6976. #include "svq3.c"