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.

8018 lines
298KB

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