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.

7770 lines
289KB

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