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.

8088 lines
301KB

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