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.

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