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.

7834 lines
291KB

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