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.

8620 lines
322KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. */
  20. /**
  21. * @file h264.c
  22. * H.264 / AVC / MPEG4 part10 codec.
  23. * @author Michael Niedermayer <michaelni@gmx.at>
  24. */
  25. #include "common.h"
  26. #include "dsputil.h"
  27. #include "avcodec.h"
  28. #include "mpegvideo.h"
  29. #include "h264data.h"
  30. #include "golomb.h"
  31. #include "cabac.h"
  32. //#undef NDEBUG
  33. #include <assert.h>
  34. #define interlaced_dct interlaced_dct_is_a_bad_name
  35. #define mb_intra mb_intra_isnt_initalized_see_mb_type
  36. #define LUMA_DC_BLOCK_INDEX 25
  37. #define CHROMA_DC_BLOCK_INDEX 26
  38. #define CHROMA_DC_COEFF_TOKEN_VLC_BITS 8
  39. #define COEFF_TOKEN_VLC_BITS 8
  40. #define TOTAL_ZEROS_VLC_BITS 9
  41. #define CHROMA_DC_TOTAL_ZEROS_VLC_BITS 3
  42. #define RUN_VLC_BITS 3
  43. #define RUN7_VLC_BITS 6
  44. #define MAX_SPS_COUNT 32
  45. #define MAX_PPS_COUNT 256
  46. #define MAX_MMCO_COUNT 66
  47. /* Compiling in interlaced support reduces the speed
  48. * of progressive decoding by about 2%. */
  49. #define ALLOW_INTERLACE
  50. #ifdef ALLOW_INTERLACE
  51. #define MB_MBAFF h->mb_mbaff
  52. #define MB_FIELD h->mb_field_decoding_flag
  53. #define FRAME_MBAFF h->mb_aff_frame
  54. #else
  55. #define MB_MBAFF 0
  56. #define MB_FIELD 0
  57. #define FRAME_MBAFF 0
  58. #undef IS_INTERLACED
  59. #define IS_INTERLACED(mb_type) 0
  60. #endif
  61. /**
  62. * Sequence parameter set
  63. */
  64. typedef struct SPS{
  65. int profile_idc;
  66. int level_idc;
  67. int transform_bypass; ///< qpprime_y_zero_transform_bypass_flag
  68. int log2_max_frame_num; ///< log2_max_frame_num_minus4 + 4
  69. int poc_type; ///< pic_order_cnt_type
  70. int log2_max_poc_lsb; ///< log2_max_pic_order_cnt_lsb_minus4
  71. int delta_pic_order_always_zero_flag;
  72. int offset_for_non_ref_pic;
  73. int offset_for_top_to_bottom_field;
  74. int poc_cycle_length; ///< num_ref_frames_in_pic_order_cnt_cycle
  75. int ref_frame_count; ///< num_ref_frames
  76. int gaps_in_frame_num_allowed_flag;
  77. int mb_width; ///< frame_width_in_mbs_minus1 + 1
  78. int mb_height; ///< frame_height_in_mbs_minus1 + 1
  79. int frame_mbs_only_flag;
  80. int mb_aff; ///<mb_adaptive_frame_field_flag
  81. int direct_8x8_inference_flag;
  82. int crop; ///< frame_cropping_flag
  83. int crop_left; ///< frame_cropping_rect_left_offset
  84. int crop_right; ///< frame_cropping_rect_right_offset
  85. int crop_top; ///< frame_cropping_rect_top_offset
  86. int crop_bottom; ///< frame_cropping_rect_bottom_offset
  87. int vui_parameters_present_flag;
  88. AVRational sar;
  89. int timing_info_present_flag;
  90. uint32_t num_units_in_tick;
  91. uint32_t time_scale;
  92. int fixed_frame_rate_flag;
  93. short offset_for_ref_frame[256]; //FIXME dyn aloc?
  94. int bitstream_restriction_flag;
  95. int num_reorder_frames;
  96. int scaling_matrix_present;
  97. uint8_t scaling_matrix4[6][16];
  98. uint8_t scaling_matrix8[2][64];
  99. }SPS;
  100. /**
  101. * Picture parameter set
  102. */
  103. typedef struct PPS{
  104. int sps_id;
  105. int cabac; ///< entropy_coding_mode_flag
  106. int pic_order_present; ///< pic_order_present_flag
  107. int slice_group_count; ///< num_slice_groups_minus1 + 1
  108. int mb_slice_group_map_type;
  109. int ref_count[2]; ///< num_ref_idx_l0/1_active_minus1 + 1
  110. int weighted_pred; ///< weighted_pred_flag
  111. int weighted_bipred_idc;
  112. int init_qp; ///< pic_init_qp_minus26 + 26
  113. int init_qs; ///< pic_init_qs_minus26 + 26
  114. int chroma_qp_index_offset;
  115. int deblocking_filter_parameters_present; ///< deblocking_filter_parameters_present_flag
  116. int constrained_intra_pred; ///< constrained_intra_pred_flag
  117. int redundant_pic_cnt_present; ///< redundant_pic_cnt_present_flag
  118. int transform_8x8_mode; ///< transform_8x8_mode_flag
  119. uint8_t scaling_matrix4[6][16];
  120. uint8_t scaling_matrix8[2][64];
  121. }PPS;
  122. /**
  123. * Memory management control operation opcode.
  124. */
  125. typedef enum MMCOOpcode{
  126. MMCO_END=0,
  127. MMCO_SHORT2UNUSED,
  128. MMCO_LONG2UNUSED,
  129. MMCO_SHORT2LONG,
  130. MMCO_SET_MAX_LONG,
  131. MMCO_RESET,
  132. MMCO_LONG,
  133. } MMCOOpcode;
  134. /**
  135. * Memory management control operation.
  136. */
  137. typedef struct MMCO{
  138. MMCOOpcode opcode;
  139. int short_frame_num;
  140. int long_index;
  141. } MMCO;
  142. /**
  143. * H264Context
  144. */
  145. typedef struct H264Context{
  146. MpegEncContext s;
  147. int nal_ref_idc;
  148. int nal_unit_type;
  149. #define NAL_SLICE 1
  150. #define NAL_DPA 2
  151. #define NAL_DPB 3
  152. #define NAL_DPC 4
  153. #define NAL_IDR_SLICE 5
  154. #define NAL_SEI 6
  155. #define NAL_SPS 7
  156. #define NAL_PPS 8
  157. #define NAL_AUD 9
  158. #define NAL_END_SEQUENCE 10
  159. #define NAL_END_STREAM 11
  160. #define NAL_FILLER_DATA 12
  161. #define NAL_SPS_EXT 13
  162. #define NAL_AUXILIARY_SLICE 19
  163. uint8_t *rbsp_buffer;
  164. unsigned int rbsp_buffer_size;
  165. /**
  166. * Used to parse AVC variant of h264
  167. */
  168. int is_avc; ///< this flag is != 0 if codec is avc1
  169. int got_avcC; ///< flag used to parse avcC data only once
  170. int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
  171. int chroma_qp; //QPc
  172. int prev_mb_skipped;
  173. int next_mb_skipped;
  174. //prediction stuff
  175. int chroma_pred_mode;
  176. int intra16x16_pred_mode;
  177. int top_mb_xy;
  178. int left_mb_xy[2];
  179. int8_t intra4x4_pred_mode_cache[5*8];
  180. int8_t (*intra4x4_pred_mode)[8];
  181. void (*pred4x4 [9+3])(uint8_t *src, uint8_t *topright, int stride);//FIXME move to dsp?
  182. void (*pred8x8l [9+3])(uint8_t *src, int topleft, int topright, int stride);
  183. void (*pred8x8 [4+3])(uint8_t *src, int stride);
  184. void (*pred16x16[4+3])(uint8_t *src, int stride);
  185. unsigned int topleft_samples_available;
  186. unsigned int top_samples_available;
  187. unsigned int topright_samples_available;
  188. unsigned int left_samples_available;
  189. uint8_t (*top_borders[2])[16+2*8];
  190. uint8_t left_border[2*(17+2*9)];
  191. /**
  192. * non zero coeff count cache.
  193. * is 64 if not available.
  194. */
  195. DECLARE_ALIGNED_8(uint8_t, non_zero_count_cache[6*8]);
  196. uint8_t (*non_zero_count)[16];
  197. /**
  198. * Motion vector cache.
  199. */
  200. DECLARE_ALIGNED_8(int16_t, mv_cache[2][5*8][2]);
  201. DECLARE_ALIGNED_8(int8_t, ref_cache[2][5*8]);
  202. #define LIST_NOT_USED -1 //FIXME rename?
  203. #define PART_NOT_AVAILABLE -2
  204. /**
  205. * is 1 if the specific list MV&references are set to 0,0,-2.
  206. */
  207. int mv_cache_clean[2];
  208. /**
  209. * number of neighbors (top and/or left) that used 8x8 dct
  210. */
  211. int neighbor_transform_size;
  212. /**
  213. * block_offset[ 0..23] for frame macroblocks
  214. * block_offset[24..47] for field macroblocks
  215. */
  216. int block_offset[2*(16+8)];
  217. uint32_t *mb2b_xy; //FIXME are these 4 a good idea?
  218. uint32_t *mb2b8_xy;
  219. int b_stride; //FIXME use s->b4_stride
  220. int b8_stride;
  221. int mb_linesize; ///< may be equal to s->linesize or s->linesize*2, for mbaff
  222. int mb_uvlinesize;
  223. int emu_edge_width;
  224. int emu_edge_height;
  225. int halfpel_flag;
  226. int thirdpel_flag;
  227. int unknown_svq3_flag;
  228. int next_slice_index;
  229. SPS sps_buffer[MAX_SPS_COUNT];
  230. SPS sps; ///< current sps
  231. PPS pps_buffer[MAX_PPS_COUNT];
  232. /**
  233. * current pps
  234. */
  235. PPS pps; //FIXME move to Picture perhaps? (->no) do we need that?
  236. uint32_t dequant4_buffer[6][52][16];
  237. uint32_t dequant8_buffer[2][52][64];
  238. uint32_t (*dequant4_coeff[6])[16];
  239. uint32_t (*dequant8_coeff[2])[64];
  240. int dequant_coeff_pps; ///< reinit tables when pps changes
  241. int slice_num;
  242. uint8_t *slice_table_base;
  243. uint8_t *slice_table; ///< slice_table_base + 2*mb_stride + 1
  244. int slice_type;
  245. int slice_type_fixed;
  246. //interlacing specific flags
  247. int mb_aff_frame;
  248. int mb_field_decoding_flag;
  249. int mb_mbaff; ///< mb_aff_frame && mb_field_decoding_flag
  250. int sub_mb_type[4];
  251. //POC stuff
  252. int poc_lsb;
  253. int poc_msb;
  254. int delta_poc_bottom;
  255. int delta_poc[2];
  256. int frame_num;
  257. int prev_poc_msb; ///< poc_msb of the last reference pic for POC type 0
  258. int prev_poc_lsb; ///< poc_lsb of the last reference pic for POC type 0
  259. int frame_num_offset; ///< for POC type 2
  260. int prev_frame_num_offset; ///< for POC type 2
  261. int prev_frame_num; ///< frame_num of the last pic for POC type 1/2
  262. /**
  263. * frame_num for frames or 2*frame_num for field pics.
  264. */
  265. int curr_pic_num;
  266. /**
  267. * max_frame_num or 2*max_frame_num for field pics.
  268. */
  269. int max_pic_num;
  270. //Weighted pred stuff
  271. int use_weight;
  272. int use_weight_chroma;
  273. int luma_log2_weight_denom;
  274. int chroma_log2_weight_denom;
  275. int luma_weight[2][48];
  276. int luma_offset[2][48];
  277. int chroma_weight[2][48][2];
  278. int chroma_offset[2][48][2];
  279. int implicit_weight[48][48];
  280. //deblock
  281. int deblocking_filter; ///< disable_deblocking_filter_idc with 1<->0
  282. int slice_alpha_c0_offset;
  283. int slice_beta_offset;
  284. int redundant_pic_count;
  285. int direct_spatial_mv_pred;
  286. int dist_scale_factor[16];
  287. int dist_scale_factor_field[32];
  288. int map_col_to_list0[2][16];
  289. int map_col_to_list0_field[2][32];
  290. /**
  291. * num_ref_idx_l0/1_active_minus1 + 1
  292. */
  293. int ref_count[2]; ///< counts frames or fields, depending on current mb mode
  294. Picture *short_ref[32];
  295. Picture *long_ref[32];
  296. Picture default_ref_list[2][32];
  297. Picture ref_list[2][48]; ///< 0..15: frame refs, 16..47: mbaff field refs
  298. Picture *delayed_pic[16]; //FIXME size?
  299. Picture *delayed_output_pic;
  300. /**
  301. * memory management control operations buffer.
  302. */
  303. MMCO mmco[MAX_MMCO_COUNT];
  304. int mmco_index;
  305. int long_ref_count; ///< number of actual long term references
  306. int short_ref_count; ///< number of actual short term references
  307. //data partitioning
  308. GetBitContext intra_gb;
  309. GetBitContext inter_gb;
  310. GetBitContext *intra_gb_ptr;
  311. GetBitContext *inter_gb_ptr;
  312. DECLARE_ALIGNED_8(DCTELEM, mb[16*24]);
  313. /**
  314. * Cabac
  315. */
  316. CABACContext cabac;
  317. uint8_t cabac_state[460];
  318. int cabac_init_idc;
  319. /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
  320. uint16_t *cbp_table;
  321. int cbp;
  322. int top_cbp;
  323. int left_cbp;
  324. /* chroma_pred_mode for i4x4 or i16x16, else 0 */
  325. uint8_t *chroma_pred_mode_table;
  326. int last_qscale_diff;
  327. int16_t (*mvd_table[2])[2];
  328. DECLARE_ALIGNED_8(int16_t, mvd_cache[2][5*8][2]);
  329. uint8_t *direct_table;
  330. uint8_t direct_cache[5*8];
  331. uint8_t zigzag_scan[16];
  332. uint8_t zigzag_scan8x8[64];
  333. uint8_t zigzag_scan8x8_cavlc[64];
  334. uint8_t field_scan[16];
  335. uint8_t field_scan8x8[64];
  336. uint8_t field_scan8x8_cavlc[64];
  337. const uint8_t *zigzag_scan_q0;
  338. const uint8_t *zigzag_scan8x8_q0;
  339. const uint8_t *zigzag_scan8x8_cavlc_q0;
  340. const uint8_t *field_scan_q0;
  341. const uint8_t *field_scan8x8_q0;
  342. const uint8_t *field_scan8x8_cavlc_q0;
  343. int x264_build;
  344. }H264Context;
  345. static VLC coeff_token_vlc[4];
  346. static VLC chroma_dc_coeff_token_vlc;
  347. static VLC total_zeros_vlc[15];
  348. static VLC chroma_dc_total_zeros_vlc[3];
  349. static VLC run_vlc[6];
  350. static VLC run7_vlc;
  351. static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
  352. static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
  353. 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);
  354. static void filter_mb_fast( 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);
  355. static always_inline uint32_t pack16to32(int a, int b){
  356. #ifdef WORDS_BIGENDIAN
  357. return (b&0xFFFF) + (a<<16);
  358. #else
  359. return (a&0xFFFF) + (b<<16);
  360. #endif
  361. }
  362. /**
  363. * fill a rectangle.
  364. * @param h height of the rectangle, should be a constant
  365. * @param w width of the rectangle, should be a constant
  366. * @param size the size of val (1 or 4), should be a constant
  367. */
  368. static always_inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){
  369. uint8_t *p= (uint8_t*)vp;
  370. assert(size==1 || size==4);
  371. assert(w<=4);
  372. w *= size;
  373. stride *= size;
  374. assert((((long)vp)&(FFMIN(w, STRIDE_ALIGN)-1)) == 0);
  375. assert((stride&(w-1))==0);
  376. if(w==2){
  377. const uint16_t v= size==4 ? val : val*0x0101;
  378. *(uint16_t*)(p + 0*stride)= v;
  379. if(h==1) return;
  380. *(uint16_t*)(p + 1*stride)= v;
  381. if(h==2) return;
  382. *(uint16_t*)(p + 2*stride)=
  383. *(uint16_t*)(p + 3*stride)= v;
  384. }else if(w==4){
  385. const uint32_t v= size==4 ? val : val*0x01010101;
  386. *(uint32_t*)(p + 0*stride)= v;
  387. if(h==1) return;
  388. *(uint32_t*)(p + 1*stride)= v;
  389. if(h==2) return;
  390. *(uint32_t*)(p + 2*stride)=
  391. *(uint32_t*)(p + 3*stride)= v;
  392. }else if(w==8){
  393. //gcc can't optimize 64bit math on x86_32
  394. #if defined(ARCH_X86_64) || (defined(MP_WORDSIZE) && MP_WORDSIZE >= 64)
  395. const uint64_t v= val*0x0100000001ULL;
  396. *(uint64_t*)(p + 0*stride)= v;
  397. if(h==1) return;
  398. *(uint64_t*)(p + 1*stride)= v;
  399. if(h==2) return;
  400. *(uint64_t*)(p + 2*stride)=
  401. *(uint64_t*)(p + 3*stride)= v;
  402. }else if(w==16){
  403. const uint64_t v= val*0x0100000001ULL;
  404. *(uint64_t*)(p + 0+0*stride)=
  405. *(uint64_t*)(p + 8+0*stride)=
  406. *(uint64_t*)(p + 0+1*stride)=
  407. *(uint64_t*)(p + 8+1*stride)= v;
  408. if(h==2) return;
  409. *(uint64_t*)(p + 0+2*stride)=
  410. *(uint64_t*)(p + 8+2*stride)=
  411. *(uint64_t*)(p + 0+3*stride)=
  412. *(uint64_t*)(p + 8+3*stride)= v;
  413. #else
  414. *(uint32_t*)(p + 0+0*stride)=
  415. *(uint32_t*)(p + 4+0*stride)= val;
  416. if(h==1) return;
  417. *(uint32_t*)(p + 0+1*stride)=
  418. *(uint32_t*)(p + 4+1*stride)= val;
  419. if(h==2) return;
  420. *(uint32_t*)(p + 0+2*stride)=
  421. *(uint32_t*)(p + 4+2*stride)=
  422. *(uint32_t*)(p + 0+3*stride)=
  423. *(uint32_t*)(p + 4+3*stride)= val;
  424. }else if(w==16){
  425. *(uint32_t*)(p + 0+0*stride)=
  426. *(uint32_t*)(p + 4+0*stride)=
  427. *(uint32_t*)(p + 8+0*stride)=
  428. *(uint32_t*)(p +12+0*stride)=
  429. *(uint32_t*)(p + 0+1*stride)=
  430. *(uint32_t*)(p + 4+1*stride)=
  431. *(uint32_t*)(p + 8+1*stride)=
  432. *(uint32_t*)(p +12+1*stride)= val;
  433. if(h==2) return;
  434. *(uint32_t*)(p + 0+2*stride)=
  435. *(uint32_t*)(p + 4+2*stride)=
  436. *(uint32_t*)(p + 8+2*stride)=
  437. *(uint32_t*)(p +12+2*stride)=
  438. *(uint32_t*)(p + 0+3*stride)=
  439. *(uint32_t*)(p + 4+3*stride)=
  440. *(uint32_t*)(p + 8+3*stride)=
  441. *(uint32_t*)(p +12+3*stride)= val;
  442. #endif
  443. }else
  444. assert(0);
  445. assert(h==4);
  446. }
  447. static void fill_caches(H264Context *h, int mb_type, int for_deblock){
  448. MpegEncContext * const s = &h->s;
  449. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  450. int topleft_xy, top_xy, topright_xy, left_xy[2];
  451. int topleft_type, top_type, topright_type, left_type[2];
  452. int left_block[8];
  453. int i;
  454. //FIXME deblocking could skip the intra and nnz parts.
  455. if(for_deblock && (h->slice_num == 1 || h->slice_table[mb_xy] == h->slice_table[mb_xy-s->mb_stride]) && !FRAME_MBAFF)
  456. return;
  457. //wow what a mess, why didn't they simplify the interlacing&intra stuff, i can't imagine that these complex rules are worth it
  458. top_xy = mb_xy - s->mb_stride;
  459. topleft_xy = top_xy - 1;
  460. topright_xy= top_xy + 1;
  461. left_xy[1] = left_xy[0] = mb_xy-1;
  462. left_block[0]= 0;
  463. left_block[1]= 1;
  464. left_block[2]= 2;
  465. left_block[3]= 3;
  466. left_block[4]= 7;
  467. left_block[5]= 10;
  468. left_block[6]= 8;
  469. left_block[7]= 11;
  470. if(FRAME_MBAFF){
  471. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  472. const int top_pair_xy = pair_xy - s->mb_stride;
  473. const int topleft_pair_xy = top_pair_xy - 1;
  474. const int topright_pair_xy = top_pair_xy + 1;
  475. const int topleft_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
  476. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  477. const int topright_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
  478. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  479. const int curr_mb_frame_flag = !IS_INTERLACED(mb_type);
  480. const int bottom = (s->mb_y & 1);
  481. 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);
  482. if (bottom
  483. ? !curr_mb_frame_flag // bottom macroblock
  484. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  485. ) {
  486. top_xy -= s->mb_stride;
  487. }
  488. if (bottom
  489. ? !curr_mb_frame_flag // bottom macroblock
  490. : (!curr_mb_frame_flag && !topleft_mb_frame_flag) // top macroblock
  491. ) {
  492. topleft_xy -= s->mb_stride;
  493. }
  494. if (bottom
  495. ? !curr_mb_frame_flag // bottom macroblock
  496. : (!curr_mb_frame_flag && !topright_mb_frame_flag) // top macroblock
  497. ) {
  498. topright_xy -= s->mb_stride;
  499. }
  500. if (left_mb_frame_flag != curr_mb_frame_flag) {
  501. left_xy[1] = left_xy[0] = pair_xy - 1;
  502. if (curr_mb_frame_flag) {
  503. if (bottom) {
  504. left_block[0]= 2;
  505. left_block[1]= 2;
  506. left_block[2]= 3;
  507. left_block[3]= 3;
  508. left_block[4]= 8;
  509. left_block[5]= 11;
  510. left_block[6]= 8;
  511. left_block[7]= 11;
  512. } else {
  513. left_block[0]= 0;
  514. left_block[1]= 0;
  515. left_block[2]= 1;
  516. left_block[3]= 1;
  517. left_block[4]= 7;
  518. left_block[5]= 10;
  519. left_block[6]= 7;
  520. left_block[7]= 10;
  521. }
  522. } else {
  523. left_xy[1] += s->mb_stride;
  524. //left_block[0]= 0;
  525. left_block[1]= 2;
  526. left_block[2]= 0;
  527. left_block[3]= 2;
  528. //left_block[4]= 7;
  529. left_block[5]= 10;
  530. left_block[6]= 7;
  531. left_block[7]= 10;
  532. }
  533. }
  534. }
  535. h->top_mb_xy = top_xy;
  536. h->left_mb_xy[0] = left_xy[0];
  537. h->left_mb_xy[1] = left_xy[1];
  538. if(for_deblock){
  539. topleft_type = 0;
  540. topright_type = 0;
  541. top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0;
  542. left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
  543. left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
  544. if(FRAME_MBAFF && !IS_INTRA(mb_type)){
  545. int list;
  546. int v = *(uint16_t*)&h->non_zero_count[mb_xy][14];
  547. for(i=0; i<16; i++)
  548. h->non_zero_count_cache[scan8[i]] = (v>>i)&1;
  549. for(list=0; list<1+(h->slice_type==B_TYPE); list++){
  550. if(USES_LIST(mb_type,list)){
  551. uint32_t *src = (uint32_t*)s->current_picture.motion_val[list][h->mb2b_xy[mb_xy]];
  552. uint32_t *dst = (uint32_t*)h->mv_cache[list][scan8[0]];
  553. int8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
  554. for(i=0; i<4; i++, dst+=8, src+=h->b_stride){
  555. dst[0] = src[0];
  556. dst[1] = src[1];
  557. dst[2] = src[2];
  558. dst[3] = src[3];
  559. }
  560. *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =
  561. *(uint32_t*)&h->ref_cache[list][scan8[ 2]] = pack16to32(ref[0],ref[1])*0x0101;
  562. ref += h->b8_stride;
  563. *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =
  564. *(uint32_t*)&h->ref_cache[list][scan8[10]] = pack16to32(ref[0],ref[1])*0x0101;
  565. }else{
  566. fill_rectangle(&h-> mv_cache[list][scan8[ 0]], 4, 4, 8, 0, 4);
  567. fill_rectangle(&h->ref_cache[list][scan8[ 0]], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  568. }
  569. }
  570. }
  571. }else{
  572. topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
  573. top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
  574. topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
  575. left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
  576. left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
  577. }
  578. if(IS_INTRA(mb_type)){
  579. h->topleft_samples_available=
  580. h->top_samples_available=
  581. h->left_samples_available= 0xFFFF;
  582. h->topright_samples_available= 0xEEEA;
  583. if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
  584. h->topleft_samples_available= 0xB3FF;
  585. h->top_samples_available= 0x33FF;
  586. h->topright_samples_available= 0x26EA;
  587. }
  588. for(i=0; i<2; i++){
  589. if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
  590. h->topleft_samples_available&= 0xDF5F;
  591. h->left_samples_available&= 0x5F5F;
  592. }
  593. }
  594. if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
  595. h->topleft_samples_available&= 0x7FFF;
  596. if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
  597. h->topright_samples_available&= 0xFBFF;
  598. if(IS_INTRA4x4(mb_type)){
  599. if(IS_INTRA4x4(top_type)){
  600. h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
  601. h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
  602. h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
  603. h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
  604. }else{
  605. int pred;
  606. if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred))
  607. pred= -1;
  608. else{
  609. pred= 2;
  610. }
  611. h->intra4x4_pred_mode_cache[4+8*0]=
  612. h->intra4x4_pred_mode_cache[5+8*0]=
  613. h->intra4x4_pred_mode_cache[6+8*0]=
  614. h->intra4x4_pred_mode_cache[7+8*0]= pred;
  615. }
  616. for(i=0; i<2; i++){
  617. if(IS_INTRA4x4(left_type[i])){
  618. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
  619. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
  620. }else{
  621. int pred;
  622. if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred))
  623. pred= -1;
  624. else{
  625. pred= 2;
  626. }
  627. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
  628. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
  629. }
  630. }
  631. }
  632. }
  633. /*
  634. 0 . T T. T T T T
  635. 1 L . .L . . . .
  636. 2 L . .L . . . .
  637. 3 . T TL . . . .
  638. 4 L . .L . . . .
  639. 5 L . .. . . . .
  640. */
  641. //FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec)
  642. if(top_type){
  643. h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
  644. h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
  645. h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
  646. h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
  647. h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
  648. h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
  649. h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
  650. h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
  651. }else{
  652. h->non_zero_count_cache[4+8*0]=
  653. h->non_zero_count_cache[5+8*0]=
  654. h->non_zero_count_cache[6+8*0]=
  655. h->non_zero_count_cache[7+8*0]=
  656. h->non_zero_count_cache[1+8*0]=
  657. h->non_zero_count_cache[2+8*0]=
  658. h->non_zero_count_cache[1+8*3]=
  659. h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  660. }
  661. for (i=0; i<2; i++) {
  662. if(left_type[i]){
  663. h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
  664. h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
  665. h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
  666. h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
  667. }else{
  668. h->non_zero_count_cache[3+8*1 + 2*8*i]=
  669. h->non_zero_count_cache[3+8*2 + 2*8*i]=
  670. h->non_zero_count_cache[0+8*1 + 8*i]=
  671. h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  672. }
  673. }
  674. if( h->pps.cabac ) {
  675. // top_cbp
  676. if(top_type) {
  677. h->top_cbp = h->cbp_table[top_xy];
  678. } else if(IS_INTRA(mb_type)) {
  679. h->top_cbp = 0x1C0;
  680. } else {
  681. h->top_cbp = 0;
  682. }
  683. // left_cbp
  684. if (left_type[0]) {
  685. h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
  686. } else if(IS_INTRA(mb_type)) {
  687. h->left_cbp = 0x1C0;
  688. } else {
  689. h->left_cbp = 0;
  690. }
  691. if (left_type[0]) {
  692. h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
  693. }
  694. if (left_type[1]) {
  695. h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
  696. }
  697. }
  698. #if 1
  699. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  700. int list;
  701. for(list=0; list<1+(h->slice_type==B_TYPE); list++){
  702. if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
  703. /*if(!h->mv_cache_clean[list]){
  704. memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
  705. memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
  706. h->mv_cache_clean[list]= 1;
  707. }*/
  708. continue;
  709. }
  710. h->mv_cache_clean[list]= 0;
  711. if(USES_LIST(top_type, list)){
  712. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  713. const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
  714. *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
  715. *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
  716. *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
  717. *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
  718. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  719. h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
  720. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  721. h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
  722. }else{
  723. *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
  724. *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
  725. *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
  726. *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
  727. *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
  728. }
  729. //FIXME unify cleanup or sth
  730. if(USES_LIST(left_type[0], list)){
  731. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  732. const int b8_xy= h->mb2b8_xy[left_xy[0]] + 1;
  733. *(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]];
  734. *(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]];
  735. h->ref_cache[list][scan8[0] - 1 + 0*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0]>>1)];
  736. h->ref_cache[list][scan8[0] - 1 + 1*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1]>>1)];
  737. }else{
  738. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 0*8]=
  739. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 1*8]= 0;
  740. h->ref_cache[list][scan8[0] - 1 + 0*8]=
  741. h->ref_cache[list][scan8[0] - 1 + 1*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  742. }
  743. if(USES_LIST(left_type[1], list)){
  744. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  745. const int b8_xy= h->mb2b8_xy[left_xy[1]] + 1;
  746. *(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]];
  747. *(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]];
  748. h->ref_cache[list][scan8[0] - 1 + 2*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[2]>>1)];
  749. h->ref_cache[list][scan8[0] - 1 + 3*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[3]>>1)];
  750. }else{
  751. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 2*8]=
  752. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 3*8]= 0;
  753. h->ref_cache[list][scan8[0] - 1 + 2*8]=
  754. h->ref_cache[list][scan8[0] - 1 + 3*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  755. assert((!left_type[0]) == (!left_type[1]));
  756. }
  757. if((for_deblock || (IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred)) && !FRAME_MBAFF)
  758. continue;
  759. if(USES_LIST(topleft_type, list)){
  760. const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
  761. const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + h->b8_stride;
  762. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  763. h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  764. }else{
  765. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
  766. h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  767. }
  768. if(USES_LIST(topright_type, list)){
  769. const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
  770. const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
  771. *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  772. h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  773. }else{
  774. *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
  775. h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  776. }
  777. if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)
  778. continue;
  779. h->ref_cache[list][scan8[5 ]+1] =
  780. h->ref_cache[list][scan8[7 ]+1] =
  781. h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewhere else)
  782. h->ref_cache[list][scan8[4 ]] =
  783. h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
  784. *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
  785. *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
  786. *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  787. *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
  788. *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
  789. if( h->pps.cabac ) {
  790. /* XXX beurk, Load mvd */
  791. if(USES_LIST(top_type, list)){
  792. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  793. *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
  794. *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
  795. *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
  796. *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
  797. }else{
  798. *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
  799. *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
  800. *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
  801. *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
  802. }
  803. if(USES_LIST(left_type[0], list)){
  804. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  805. *(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]];
  806. *(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]];
  807. }else{
  808. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
  809. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
  810. }
  811. if(USES_LIST(left_type[1], list)){
  812. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  813. *(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]];
  814. *(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]];
  815. }else{
  816. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
  817. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
  818. }
  819. *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
  820. *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
  821. *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  822. *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
  823. *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
  824. if(h->slice_type == B_TYPE){
  825. fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
  826. if(IS_DIRECT(top_type)){
  827. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
  828. }else if(IS_8X8(top_type)){
  829. int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
  830. h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
  831. h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
  832. }else{
  833. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
  834. }
  835. if(IS_DIRECT(left_type[0]))
  836. h->direct_cache[scan8[0] - 1 + 0*8]= 1;
  837. else if(IS_8X8(left_type[0]))
  838. h->direct_cache[scan8[0] - 1 + 0*8]= h->direct_table[h->mb2b8_xy[left_xy[0]] + 1 + h->b8_stride*(left_block[0]>>1)];
  839. else
  840. h->direct_cache[scan8[0] - 1 + 0*8]= 0;
  841. if(IS_DIRECT(left_type[1]))
  842. h->direct_cache[scan8[0] - 1 + 2*8]= 1;
  843. else if(IS_8X8(left_type[1]))
  844. h->direct_cache[scan8[0] - 1 + 2*8]= h->direct_table[h->mb2b8_xy[left_xy[1]] + 1 + h->b8_stride*(left_block[2]>>1)];
  845. else
  846. h->direct_cache[scan8[0] - 1 + 2*8]= 0;
  847. }
  848. }
  849. if(FRAME_MBAFF){
  850. #define MAP_MVS\
  851. MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
  852. MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
  853. MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
  854. MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
  855. MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
  856. MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
  857. MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\
  858. MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\
  859. MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\
  860. MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
  861. if(MB_FIELD){
  862. #define MAP_F2F(idx, mb_type)\
  863. if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  864. h->ref_cache[list][idx] <<= 1;\
  865. h->mv_cache[list][idx][1] /= 2;\
  866. h->mvd_cache[list][idx][1] /= 2;\
  867. }
  868. MAP_MVS
  869. #undef MAP_F2F
  870. }else{
  871. #define MAP_F2F(idx, mb_type)\
  872. if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  873. h->ref_cache[list][idx] >>= 1;\
  874. h->mv_cache[list][idx][1] <<= 1;\
  875. h->mvd_cache[list][idx][1] <<= 1;\
  876. }
  877. MAP_MVS
  878. #undef MAP_F2F
  879. }
  880. }
  881. }
  882. }
  883. #endif
  884. h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
  885. }
  886. static inline void write_back_intra_pred_mode(H264Context *h){
  887. MpegEncContext * const s = &h->s;
  888. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  889. h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
  890. h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
  891. h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
  892. h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
  893. h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
  894. h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
  895. h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
  896. }
  897. /**
  898. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  899. */
  900. static inline int check_intra4x4_pred_mode(H264Context *h){
  901. MpegEncContext * const s = &h->s;
  902. static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
  903. static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
  904. int i;
  905. if(!(h->top_samples_available&0x8000)){
  906. for(i=0; i<4; i++){
  907. int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
  908. if(status<0){
  909. 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);
  910. return -1;
  911. } else if(status){
  912. h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
  913. }
  914. }
  915. }
  916. if(!(h->left_samples_available&0x8000)){
  917. for(i=0; i<4; i++){
  918. int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
  919. if(status<0){
  920. 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);
  921. return -1;
  922. } else if(status){
  923. h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
  924. }
  925. }
  926. }
  927. return 0;
  928. } //FIXME cleanup like next
  929. /**
  930. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  931. */
  932. static inline int check_intra_pred_mode(H264Context *h, int mode){
  933. MpegEncContext * const s = &h->s;
  934. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  935. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  936. if(mode < 0 || mode > 6) {
  937. 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);
  938. return -1;
  939. }
  940. if(!(h->top_samples_available&0x8000)){
  941. mode= top[ mode ];
  942. if(mode<0){
  943. 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);
  944. return -1;
  945. }
  946. }
  947. if(!(h->left_samples_available&0x8000)){
  948. mode= left[ mode ];
  949. if(mode<0){
  950. 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);
  951. return -1;
  952. }
  953. }
  954. return mode;
  955. }
  956. /**
  957. * gets the predicted intra4x4 prediction mode.
  958. */
  959. static inline int pred_intra_mode(H264Context *h, int n){
  960. const int index8= scan8[n];
  961. const int left= h->intra4x4_pred_mode_cache[index8 - 1];
  962. const int top = h->intra4x4_pred_mode_cache[index8 - 8];
  963. const int min= FFMIN(left, top);
  964. tprintf("mode:%d %d min:%d\n", left ,top, min);
  965. if(min<0) return DC_PRED;
  966. else return min;
  967. }
  968. static inline void write_back_non_zero_count(H264Context *h){
  969. MpegEncContext * const s = &h->s;
  970. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  971. h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
  972. h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
  973. h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
  974. h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
  975. h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
  976. h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
  977. h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
  978. h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
  979. h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
  980. h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
  981. h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
  982. h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
  983. h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
  984. if(FRAME_MBAFF){
  985. // store all luma nnzs, for deblocking
  986. int v = 0, i;
  987. for(i=0; i<16; i++)
  988. v += (!!h->non_zero_count_cache[scan8[i]]) << i;
  989. *(uint16_t*)&h->non_zero_count[mb_xy][14] = v;
  990. }
  991. }
  992. /**
  993. * gets the predicted number of non zero coefficients.
  994. * @param n block index
  995. */
  996. static inline int pred_non_zero_count(H264Context *h, int n){
  997. const int index8= scan8[n];
  998. const int left= h->non_zero_count_cache[index8 - 1];
  999. const int top = h->non_zero_count_cache[index8 - 8];
  1000. int i= left + top;
  1001. if(i<64) i= (i+1)>>1;
  1002. tprintf("pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
  1003. return i&31;
  1004. }
  1005. static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
  1006. const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
  1007. /* there is no consistent mapping of mvs to neighboring locations that will
  1008. * make mbaff happy, so we can't move all this logic to fill_caches */
  1009. if(FRAME_MBAFF){
  1010. MpegEncContext *s = &h->s;
  1011. const uint32_t *mb_types = s->current_picture_ptr->mb_type;
  1012. const int16_t *mv;
  1013. *(uint32_t*)h->mv_cache[list][scan8[0]-2] = 0;
  1014. *C = h->mv_cache[list][scan8[0]-2];
  1015. if(!MB_FIELD
  1016. && (s->mb_y&1) && i < scan8[0]+8 && topright_ref != PART_NOT_AVAILABLE){
  1017. int topright_xy = s->mb_x + (s->mb_y-1)*s->mb_stride + (i == scan8[0]+3);
  1018. if(IS_INTERLACED(mb_types[topright_xy])){
  1019. #define SET_DIAG_MV(MV_OP, REF_OP, X4, Y4)\
  1020. const int x4 = X4, y4 = Y4;\
  1021. const int mb_type = mb_types[(x4>>2)+(y4>>2)*s->mb_stride];\
  1022. if(!USES_LIST(mb_type,list) && !IS_8X8(mb_type))\
  1023. return LIST_NOT_USED;\
  1024. mv = s->current_picture_ptr->motion_val[list][x4 + y4*h->b_stride];\
  1025. h->mv_cache[list][scan8[0]-2][0] = mv[0];\
  1026. h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
  1027. return s->current_picture_ptr->ref_index[list][(x4>>1) + (y4>>1)*h->b8_stride] REF_OP;
  1028. SET_DIAG_MV(*2, >>1, s->mb_x*4+(i&7)-4+part_width, s->mb_y*4-1);
  1029. }
  1030. }
  1031. if(topright_ref == PART_NOT_AVAILABLE
  1032. && ((s->mb_y&1) || i >= scan8[0]+8) && (i&7)==4
  1033. && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
  1034. if(!MB_FIELD
  1035. && IS_INTERLACED(mb_types[h->left_mb_xy[0]])){
  1036. SET_DIAG_MV(*2, >>1, s->mb_x*4-1, (s->mb_y|1)*4+(s->mb_y&1)*2+(i>>4)-1);
  1037. }
  1038. if(MB_FIELD
  1039. && !IS_INTERLACED(mb_types[h->left_mb_xy[0]])
  1040. && i >= scan8[0]+8){
  1041. // leftshift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's ok.
  1042. SET_DIAG_MV(>>1, <<1, s->mb_x*4-1, (s->mb_y&~1)*4 - 1 + ((i-scan8[0])>>3)*2);
  1043. }
  1044. }
  1045. #undef SET_DIAG_MV
  1046. }
  1047. if(topright_ref != PART_NOT_AVAILABLE){
  1048. *C= h->mv_cache[list][ i - 8 + part_width ];
  1049. return topright_ref;
  1050. }else{
  1051. tprintf("topright MV not available\n");
  1052. *C= h->mv_cache[list][ i - 8 - 1 ];
  1053. return h->ref_cache[list][ i - 8 - 1 ];
  1054. }
  1055. }
  1056. /**
  1057. * gets the predicted MV.
  1058. * @param n the block index
  1059. * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
  1060. * @param mx the x component of the predicted motion vector
  1061. * @param my the y component of the predicted motion vector
  1062. */
  1063. static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
  1064. const int index8= scan8[n];
  1065. const int top_ref= h->ref_cache[list][ index8 - 8 ];
  1066. const int left_ref= h->ref_cache[list][ index8 - 1 ];
  1067. const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
  1068. const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
  1069. const int16_t * C;
  1070. int diagonal_ref, match_count;
  1071. assert(part_width==1 || part_width==2 || part_width==4);
  1072. /* mv_cache
  1073. B . . A T T T T
  1074. U . . L . . , .
  1075. U . . L . . . .
  1076. U . . L . . , .
  1077. . . . L . . . .
  1078. */
  1079. diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
  1080. match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
  1081. tprintf("pred_motion match_count=%d\n", match_count);
  1082. if(match_count > 1){ //most common
  1083. *mx= mid_pred(A[0], B[0], C[0]);
  1084. *my= mid_pred(A[1], B[1], C[1]);
  1085. }else if(match_count==1){
  1086. if(left_ref==ref){
  1087. *mx= A[0];
  1088. *my= A[1];
  1089. }else if(top_ref==ref){
  1090. *mx= B[0];
  1091. *my= B[1];
  1092. }else{
  1093. *mx= C[0];
  1094. *my= C[1];
  1095. }
  1096. }else{
  1097. if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
  1098. *mx= A[0];
  1099. *my= A[1];
  1100. }else{
  1101. *mx= mid_pred(A[0], B[0], C[0]);
  1102. *my= mid_pred(A[1], B[1], C[1]);
  1103. }
  1104. }
  1105. 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);
  1106. }
  1107. /**
  1108. * gets the directionally predicted 16x8 MV.
  1109. * @param n the block index
  1110. * @param mx the x component of the predicted motion vector
  1111. * @param my the y component of the predicted motion vector
  1112. */
  1113. static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  1114. if(n==0){
  1115. const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
  1116. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  1117. 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);
  1118. if(top_ref == ref){
  1119. *mx= B[0];
  1120. *my= B[1];
  1121. return;
  1122. }
  1123. }else{
  1124. const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
  1125. const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
  1126. 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);
  1127. if(left_ref == ref){
  1128. *mx= A[0];
  1129. *my= A[1];
  1130. return;
  1131. }
  1132. }
  1133. //RARE
  1134. pred_motion(h, n, 4, list, ref, mx, my);
  1135. }
  1136. /**
  1137. * gets the directionally predicted 8x16 MV.
  1138. * @param n the block index
  1139. * @param mx the x component of the predicted motion vector
  1140. * @param my the y component of the predicted motion vector
  1141. */
  1142. static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  1143. if(n==0){
  1144. const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
  1145. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  1146. 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);
  1147. if(left_ref == ref){
  1148. *mx= A[0];
  1149. *my= A[1];
  1150. return;
  1151. }
  1152. }else{
  1153. const int16_t * C;
  1154. int diagonal_ref;
  1155. diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
  1156. 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);
  1157. if(diagonal_ref == ref){
  1158. *mx= C[0];
  1159. *my= C[1];
  1160. return;
  1161. }
  1162. }
  1163. //RARE
  1164. pred_motion(h, n, 2, list, ref, mx, my);
  1165. }
  1166. static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
  1167. const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
  1168. const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
  1169. tprintf("pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
  1170. if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
  1171. || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
  1172. || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
  1173. *mx = *my = 0;
  1174. return;
  1175. }
  1176. pred_motion(h, 0, 4, 0, 0, mx, my);
  1177. return;
  1178. }
  1179. static inline void direct_dist_scale_factor(H264Context * const h){
  1180. const int poc = h->s.current_picture_ptr->poc;
  1181. const int poc1 = h->ref_list[1][0].poc;
  1182. int i;
  1183. for(i=0; i<h->ref_count[0]; i++){
  1184. int poc0 = h->ref_list[0][i].poc;
  1185. int td = clip(poc1 - poc0, -128, 127);
  1186. if(td == 0 /* FIXME || pic0 is a long-term ref */){
  1187. h->dist_scale_factor[i] = 256;
  1188. }else{
  1189. int tb = clip(poc - poc0, -128, 127);
  1190. int tx = (16384 + (ABS(td) >> 1)) / td;
  1191. h->dist_scale_factor[i] = clip((tb*tx + 32) >> 6, -1024, 1023);
  1192. }
  1193. }
  1194. if(FRAME_MBAFF){
  1195. for(i=0; i<h->ref_count[0]; i++){
  1196. h->dist_scale_factor_field[2*i] =
  1197. h->dist_scale_factor_field[2*i+1] = h->dist_scale_factor[i];
  1198. }
  1199. }
  1200. }
  1201. static inline void direct_ref_list_init(H264Context * const h){
  1202. MpegEncContext * const s = &h->s;
  1203. Picture * const ref1 = &h->ref_list[1][0];
  1204. Picture * const cur = s->current_picture_ptr;
  1205. int list, i, j;
  1206. if(cur->pict_type == I_TYPE)
  1207. cur->ref_count[0] = 0;
  1208. if(cur->pict_type != B_TYPE)
  1209. cur->ref_count[1] = 0;
  1210. for(list=0; list<2; list++){
  1211. cur->ref_count[list] = h->ref_count[list];
  1212. for(j=0; j<h->ref_count[list]; j++)
  1213. cur->ref_poc[list][j] = h->ref_list[list][j].poc;
  1214. }
  1215. if(cur->pict_type != B_TYPE || h->direct_spatial_mv_pred)
  1216. return;
  1217. for(list=0; list<2; list++){
  1218. for(i=0; i<ref1->ref_count[list]; i++){
  1219. const int poc = ref1->ref_poc[list][i];
  1220. h->map_col_to_list0[list][i] = 0; /* bogus; fills in for missing frames */
  1221. for(j=0; j<h->ref_count[list]; j++)
  1222. if(h->ref_list[list][j].poc == poc){
  1223. h->map_col_to_list0[list][i] = j;
  1224. break;
  1225. }
  1226. }
  1227. }
  1228. if(FRAME_MBAFF){
  1229. for(list=0; list<2; list++){
  1230. for(i=0; i<ref1->ref_count[list]; i++){
  1231. j = h->map_col_to_list0[list][i];
  1232. h->map_col_to_list0_field[list][2*i] = 2*j;
  1233. h->map_col_to_list0_field[list][2*i+1] = 2*j+1;
  1234. }
  1235. }
  1236. }
  1237. }
  1238. static inline void pred_direct_motion(H264Context * const h, int *mb_type){
  1239. MpegEncContext * const s = &h->s;
  1240. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  1241. const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1242. const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1243. const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
  1244. const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
  1245. const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[1][b4_xy];
  1246. const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
  1247. const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy];
  1248. const int is_b8x8 = IS_8X8(*mb_type);
  1249. int sub_mb_type;
  1250. int i8, i4;
  1251. #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
  1252. if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
  1253. /* FIXME save sub mb types from previous frames (or derive from MVs)
  1254. * so we know exactly what block size to use */
  1255. sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  1256. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  1257. }else if(!is_b8x8 && (mb_type_col & MB_TYPE_16x16_OR_INTRA)){
  1258. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  1259. *mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
  1260. }else{
  1261. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  1262. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  1263. }
  1264. if(!is_b8x8)
  1265. *mb_type |= MB_TYPE_DIRECT2;
  1266. if(MB_FIELD)
  1267. *mb_type |= MB_TYPE_INTERLACED;
  1268. 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);
  1269. if(h->direct_spatial_mv_pred){
  1270. int ref[2];
  1271. int mv[2][2];
  1272. int list;
  1273. /* FIXME interlacing + spatial direct uses wrong colocated block positions */
  1274. /* ref = min(neighbors) */
  1275. for(list=0; list<2; list++){
  1276. int refa = h->ref_cache[list][scan8[0] - 1];
  1277. int refb = h->ref_cache[list][scan8[0] - 8];
  1278. int refc = h->ref_cache[list][scan8[0] - 8 + 4];
  1279. if(refc == -2)
  1280. refc = h->ref_cache[list][scan8[0] - 8 - 1];
  1281. ref[list] = refa;
  1282. if(ref[list] < 0 || (refb < ref[list] && refb >= 0))
  1283. ref[list] = refb;
  1284. if(ref[list] < 0 || (refc < ref[list] && refc >= 0))
  1285. ref[list] = refc;
  1286. if(ref[list] < 0)
  1287. ref[list] = -1;
  1288. }
  1289. if(ref[0] < 0 && ref[1] < 0){
  1290. ref[0] = ref[1] = 0;
  1291. mv[0][0] = mv[0][1] =
  1292. mv[1][0] = mv[1][1] = 0;
  1293. }else{
  1294. for(list=0; list<2; list++){
  1295. if(ref[list] >= 0)
  1296. pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
  1297. else
  1298. mv[list][0] = mv[list][1] = 0;
  1299. }
  1300. }
  1301. if(ref[1] < 0){
  1302. *mb_type &= ~MB_TYPE_P0L1;
  1303. sub_mb_type &= ~MB_TYPE_P0L1;
  1304. }else if(ref[0] < 0){
  1305. *mb_type &= ~MB_TYPE_P0L0;
  1306. sub_mb_type &= ~MB_TYPE_P0L0;
  1307. }
  1308. if(IS_16X16(*mb_type)){
  1309. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  1310. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  1311. if(!IS_INTRA(mb_type_col)
  1312. && ( (l1ref0[0] == 0 && ABS(l1mv0[0][0]) <= 1 && ABS(l1mv0[0][1]) <= 1)
  1313. || (l1ref0[0] < 0 && l1ref1[0] == 0 && ABS(l1mv1[0][0]) <= 1 && ABS(l1mv1[0][1]) <= 1
  1314. && (h->x264_build>33 || !h->x264_build)))){
  1315. if(ref[0] > 0)
  1316. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1317. else
  1318. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  1319. if(ref[1] > 0)
  1320. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1321. else
  1322. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  1323. }else{
  1324. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1325. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1326. }
  1327. }else{
  1328. for(i8=0; i8<4; i8++){
  1329. const int x8 = i8&1;
  1330. const int y8 = i8>>1;
  1331. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1332. continue;
  1333. h->sub_mb_type[i8] = sub_mb_type;
  1334. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1335. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1336. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  1337. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  1338. /* col_zero_flag */
  1339. if(!IS_INTRA(mb_type_col) && ( l1ref0[x8 + y8*h->b8_stride] == 0
  1340. || (l1ref0[x8 + y8*h->b8_stride] < 0 && l1ref1[x8 + y8*h->b8_stride] == 0
  1341. && (h->x264_build>33 || !h->x264_build)))){
  1342. const int16_t (*l1mv)[2]= l1ref0[x8 + y8*h->b8_stride] == 0 ? l1mv0 : l1mv1;
  1343. if(IS_SUB_8X8(sub_mb_type)){
  1344. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1345. if(ABS(mv_col[0]) <= 1 && ABS(mv_col[1]) <= 1){
  1346. if(ref[0] == 0)
  1347. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1348. if(ref[1] == 0)
  1349. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1350. }
  1351. }else
  1352. for(i4=0; i4<4; i4++){
  1353. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1354. if(ABS(mv_col[0]) <= 1 && ABS(mv_col[1]) <= 1){
  1355. if(ref[0] == 0)
  1356. *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
  1357. if(ref[1] == 0)
  1358. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
  1359. }
  1360. }
  1361. }
  1362. }
  1363. }
  1364. }else{ /* direct temporal mv pred */
  1365. const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
  1366. const int *dist_scale_factor = h->dist_scale_factor;
  1367. if(FRAME_MBAFF){
  1368. if(IS_INTERLACED(*mb_type)){
  1369. map_col_to_list0[0] = h->map_col_to_list0_field[0];
  1370. map_col_to_list0[1] = h->map_col_to_list0_field[1];
  1371. dist_scale_factor = h->dist_scale_factor_field;
  1372. }
  1373. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col)){
  1374. /* FIXME assumes direct_8x8_inference == 1 */
  1375. const int pair_xy = s->mb_x + (s->mb_y&~1)*s->mb_stride;
  1376. int mb_types_col[2];
  1377. int y_shift;
  1378. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1
  1379. | (is_b8x8 ? 0 : MB_TYPE_DIRECT2)
  1380. | (*mb_type & MB_TYPE_INTERLACED);
  1381. sub_mb_type = MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_16x16;
  1382. if(IS_INTERLACED(*mb_type)){
  1383. /* frame to field scaling */
  1384. mb_types_col[0] = h->ref_list[1][0].mb_type[pair_xy];
  1385. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1386. if(s->mb_y&1){
  1387. l1ref0 -= 2*h->b8_stride;
  1388. l1ref1 -= 2*h->b8_stride;
  1389. l1mv0 -= 4*h->b_stride;
  1390. l1mv1 -= 4*h->b_stride;
  1391. }
  1392. y_shift = 0;
  1393. if( (mb_types_col[0] & MB_TYPE_16x16_OR_INTRA)
  1394. && (mb_types_col[1] & MB_TYPE_16x16_OR_INTRA)
  1395. && !is_b8x8)
  1396. *mb_type |= MB_TYPE_16x8;
  1397. else
  1398. *mb_type |= MB_TYPE_8x8;
  1399. }else{
  1400. /* field to frame scaling */
  1401. /* col_mb_y = (mb_y&~1) + (topAbsDiffPOC < bottomAbsDiffPOC ? 0 : 1)
  1402. * but in MBAFF, top and bottom POC are equal */
  1403. int dy = (s->mb_y&1) ? 1 : 2;
  1404. mb_types_col[0] =
  1405. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1406. l1ref0 += dy*h->b8_stride;
  1407. l1ref1 += dy*h->b8_stride;
  1408. l1mv0 += 2*dy*h->b_stride;
  1409. l1mv1 += 2*dy*h->b_stride;
  1410. y_shift = 2;
  1411. if((mb_types_col[0] & (MB_TYPE_16x16_OR_INTRA|MB_TYPE_16x8))
  1412. && !is_b8x8)
  1413. *mb_type |= MB_TYPE_16x16;
  1414. else
  1415. *mb_type |= MB_TYPE_8x8;
  1416. }
  1417. for(i8=0; i8<4; i8++){
  1418. const int x8 = i8&1;
  1419. const int y8 = i8>>1;
  1420. int ref0, scale;
  1421. const int16_t (*l1mv)[2]= l1mv0;
  1422. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1423. continue;
  1424. h->sub_mb_type[i8] = sub_mb_type;
  1425. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1426. if(IS_INTRA(mb_types_col[y8])){
  1427. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1428. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1429. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1430. continue;
  1431. }
  1432. ref0 = l1ref0[x8 + (y8*2>>y_shift)*h->b8_stride];
  1433. if(ref0 >= 0)
  1434. ref0 = map_col_to_list0[0][ref0*2>>y_shift];
  1435. else{
  1436. ref0 = map_col_to_list0[1][l1ref1[x8 + (y8*2>>y_shift)*h->b8_stride]*2>>y_shift];
  1437. l1mv= l1mv1;
  1438. }
  1439. scale = dist_scale_factor[ref0];
  1440. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1441. {
  1442. const int16_t *mv_col = l1mv[x8*3 + (y8*6>>y_shift)*h->b_stride];
  1443. int my_col = (mv_col[1]<<y_shift)/2;
  1444. int mx = (scale * mv_col[0] + 128) >> 8;
  1445. int my = (scale * my_col + 128) >> 8;
  1446. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1447. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
  1448. }
  1449. }
  1450. return;
  1451. }
  1452. }
  1453. /* one-to-one mv scaling */
  1454. if(IS_16X16(*mb_type)){
  1455. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  1456. if(IS_INTRA(mb_type_col)){
  1457. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  1458. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  1459. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  1460. }else{
  1461. const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0]]
  1462. : map_col_to_list0[1][l1ref1[0]];
  1463. const int scale = dist_scale_factor[ref0];
  1464. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  1465. int mv_l0[2];
  1466. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1467. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1468. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref0, 1);
  1469. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0],mv_l0[1]), 4);
  1470. 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);
  1471. }
  1472. }else{
  1473. for(i8=0; i8<4; i8++){
  1474. const int x8 = i8&1;
  1475. const int y8 = i8>>1;
  1476. int ref0, scale;
  1477. const int16_t (*l1mv)[2]= l1mv0;
  1478. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1479. continue;
  1480. h->sub_mb_type[i8] = sub_mb_type;
  1481. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1482. if(IS_INTRA(mb_type_col)){
  1483. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1484. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1485. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1486. continue;
  1487. }
  1488. ref0 = l1ref0[x8 + y8*h->b8_stride];
  1489. if(ref0 >= 0)
  1490. ref0 = map_col_to_list0[0][ref0];
  1491. else{
  1492. ref0 = map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]];
  1493. l1mv= l1mv1;
  1494. }
  1495. scale = dist_scale_factor[ref0];
  1496. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1497. if(IS_SUB_8X8(sub_mb_type)){
  1498. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1499. int mx = (scale * mv_col[0] + 128) >> 8;
  1500. int my = (scale * mv_col[1] + 128) >> 8;
  1501. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1502. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
  1503. }else
  1504. for(i4=0; i4<4; i4++){
  1505. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1506. int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
  1507. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1508. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1509. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
  1510. pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  1511. }
  1512. }
  1513. }
  1514. }
  1515. }
  1516. static inline void write_back_motion(H264Context *h, int mb_type){
  1517. MpegEncContext * const s = &h->s;
  1518. const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1519. const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1520. int list;
  1521. if(!USES_LIST(mb_type, 0))
  1522. fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, h->b8_stride, (uint8_t)LIST_NOT_USED, 1);
  1523. for(list=0; list<2; list++){
  1524. int y;
  1525. if(!USES_LIST(mb_type, list))
  1526. continue;
  1527. for(y=0; y<4; y++){
  1528. *(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];
  1529. *(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];
  1530. }
  1531. if( h->pps.cabac ) {
  1532. if(IS_SKIP(mb_type))
  1533. fill_rectangle(h->mvd_table[list][b_xy], 4, 4, h->b_stride, 0, 4);
  1534. else
  1535. for(y=0; y<4; y++){
  1536. *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
  1537. *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
  1538. }
  1539. }
  1540. {
  1541. int8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
  1542. ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]];
  1543. ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]];
  1544. ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]];
  1545. ref_index[1+1*h->b8_stride]= h->ref_cache[list][scan8[12]];
  1546. }
  1547. }
  1548. if(h->slice_type == B_TYPE && h->pps.cabac){
  1549. if(IS_8X8(mb_type)){
  1550. uint8_t *direct_table = &h->direct_table[b8_xy];
  1551. direct_table[1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
  1552. direct_table[0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
  1553. direct_table[1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
  1554. }
  1555. }
  1556. }
  1557. /**
  1558. * Decodes a network abstraction layer unit.
  1559. * @param consumed is the number of bytes used as input
  1560. * @param length is the length of the array
  1561. * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp tailing?
  1562. * @returns decoded bytes, might be src+1 if no escapes
  1563. */
  1564. static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){
  1565. int i, si, di;
  1566. uint8_t *dst;
  1567. // src[0]&0x80; //forbidden bit
  1568. h->nal_ref_idc= src[0]>>5;
  1569. h->nal_unit_type= src[0]&0x1F;
  1570. src++; length--;
  1571. #if 0
  1572. for(i=0; i<length; i++)
  1573. printf("%2X ", src[i]);
  1574. #endif
  1575. for(i=0; i+1<length; i+=2){
  1576. if(src[i]) continue;
  1577. if(i>0 && src[i-1]==0) i--;
  1578. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  1579. if(src[i+2]!=3){
  1580. /* startcode, so we must be past the end */
  1581. length=i;
  1582. }
  1583. break;
  1584. }
  1585. }
  1586. if(i>=length-1){ //no escaped 0
  1587. *dst_length= length;
  1588. *consumed= length+1; //+1 for the header
  1589. return src;
  1590. }
  1591. h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length);
  1592. dst= h->rbsp_buffer;
  1593. //printf("decoding esc\n");
  1594. si=di=0;
  1595. while(si<length){
  1596. //remove escapes (very rare 1:2^22)
  1597. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  1598. if(src[si+2]==3){ //escape
  1599. dst[di++]= 0;
  1600. dst[di++]= 0;
  1601. si+=3;
  1602. continue;
  1603. }else //next start code
  1604. break;
  1605. }
  1606. dst[di++]= src[si++];
  1607. }
  1608. *dst_length= di;
  1609. *consumed= si + 1;//+1 for the header
  1610. //FIXME store exact number of bits in the getbitcontext (its needed for decoding)
  1611. return dst;
  1612. }
  1613. #if 0
  1614. /**
  1615. * @param src the data which should be escaped
  1616. * @param dst the target buffer, dst+1 == src is allowed as a special case
  1617. * @param length the length of the src data
  1618. * @param dst_length the length of the dst array
  1619. * @returns length of escaped data in bytes or -1 if an error occured
  1620. */
  1621. static int encode_nal(H264Context *h, uint8_t *dst, uint8_t *src, int length, int dst_length){
  1622. int i, escape_count, si, di;
  1623. uint8_t *temp;
  1624. assert(length>=0);
  1625. assert(dst_length>0);
  1626. dst[0]= (h->nal_ref_idc<<5) + h->nal_unit_type;
  1627. if(length==0) return 1;
  1628. escape_count= 0;
  1629. for(i=0; i<length; i+=2){
  1630. if(src[i]) continue;
  1631. if(i>0 && src[i-1]==0)
  1632. i--;
  1633. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  1634. escape_count++;
  1635. i+=2;
  1636. }
  1637. }
  1638. if(escape_count==0){
  1639. if(dst+1 != src)
  1640. memcpy(dst+1, src, length);
  1641. return length + 1;
  1642. }
  1643. if(length + escape_count + 1> dst_length)
  1644. return -1;
  1645. //this should be damn rare (hopefully)
  1646. h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length + escape_count);
  1647. temp= h->rbsp_buffer;
  1648. //printf("encoding esc\n");
  1649. si= 0;
  1650. di= 0;
  1651. while(si < length){
  1652. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  1653. temp[di++]= 0; si++;
  1654. temp[di++]= 0; si++;
  1655. temp[di++]= 3;
  1656. temp[di++]= src[si++];
  1657. }
  1658. else
  1659. temp[di++]= src[si++];
  1660. }
  1661. memcpy(dst+1, temp, length+escape_count);
  1662. assert(di == length+escape_count);
  1663. return di + 1;
  1664. }
  1665. /**
  1666. * write 1,10,100,1000,... for alignment, yes its exactly inverse to mpeg4
  1667. */
  1668. static void encode_rbsp_trailing(PutBitContext *pb){
  1669. int length;
  1670. put_bits(pb, 1, 1);
  1671. length= (-put_bits_count(pb))&7;
  1672. if(length) put_bits(pb, length, 0);
  1673. }
  1674. #endif
  1675. /**
  1676. * identifies the exact end of the bitstream
  1677. * @return the length of the trailing, or 0 if damaged
  1678. */
  1679. static int decode_rbsp_trailing(uint8_t *src){
  1680. int v= *src;
  1681. int r;
  1682. tprintf("rbsp trailing %X\n", v);
  1683. for(r=1; r<9; r++){
  1684. if(v&1) return r;
  1685. v>>=1;
  1686. }
  1687. return 0;
  1688. }
  1689. /**
  1690. * idct tranforms the 16 dc values and dequantize them.
  1691. * @param qp quantization parameter
  1692. */
  1693. static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1694. #define stride 16
  1695. int i;
  1696. int temp[16]; //FIXME check if this is a good idea
  1697. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1698. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1699. //memset(block, 64, 2*256);
  1700. //return;
  1701. for(i=0; i<4; i++){
  1702. const int offset= y_offset[i];
  1703. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1704. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1705. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1706. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1707. temp[4*i+0]= z0+z3;
  1708. temp[4*i+1]= z1+z2;
  1709. temp[4*i+2]= z1-z2;
  1710. temp[4*i+3]= z0-z3;
  1711. }
  1712. for(i=0; i<4; i++){
  1713. const int offset= x_offset[i];
  1714. const int z0= temp[4*0+i] + temp[4*2+i];
  1715. const int z1= temp[4*0+i] - temp[4*2+i];
  1716. const int z2= temp[4*1+i] - temp[4*3+i];
  1717. const int z3= temp[4*1+i] + temp[4*3+i];
  1718. block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_resdual
  1719. block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
  1720. block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
  1721. block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
  1722. }
  1723. }
  1724. #if 0
  1725. /**
  1726. * dct tranforms the 16 dc values.
  1727. * @param qp quantization parameter ??? FIXME
  1728. */
  1729. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  1730. // const int qmul= dequant_coeff[qp][0];
  1731. int i;
  1732. int temp[16]; //FIXME check if this is a good idea
  1733. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1734. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1735. for(i=0; i<4; i++){
  1736. const int offset= y_offset[i];
  1737. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1738. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1739. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1740. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1741. temp[4*i+0]= z0+z3;
  1742. temp[4*i+1]= z1+z2;
  1743. temp[4*i+2]= z1-z2;
  1744. temp[4*i+3]= z0-z3;
  1745. }
  1746. for(i=0; i<4; i++){
  1747. const int offset= x_offset[i];
  1748. const int z0= temp[4*0+i] + temp[4*2+i];
  1749. const int z1= temp[4*0+i] - temp[4*2+i];
  1750. const int z2= temp[4*1+i] - temp[4*3+i];
  1751. const int z3= temp[4*1+i] + temp[4*3+i];
  1752. block[stride*0 +offset]= (z0 + z3)>>1;
  1753. block[stride*2 +offset]= (z1 + z2)>>1;
  1754. block[stride*8 +offset]= (z1 - z2)>>1;
  1755. block[stride*10+offset]= (z0 - z3)>>1;
  1756. }
  1757. }
  1758. #endif
  1759. #undef xStride
  1760. #undef stride
  1761. static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1762. const int stride= 16*2;
  1763. const int xStride= 16;
  1764. int a,b,c,d,e;
  1765. a= block[stride*0 + xStride*0];
  1766. b= block[stride*0 + xStride*1];
  1767. c= block[stride*1 + xStride*0];
  1768. d= block[stride*1 + xStride*1];
  1769. e= a-b;
  1770. a= a+b;
  1771. b= c-d;
  1772. c= c+d;
  1773. block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
  1774. block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
  1775. block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
  1776. block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
  1777. }
  1778. #if 0
  1779. static void chroma_dc_dct_c(DCTELEM *block){
  1780. const int stride= 16*2;
  1781. const int xStride= 16;
  1782. int a,b,c,d,e;
  1783. a= block[stride*0 + xStride*0];
  1784. b= block[stride*0 + xStride*1];
  1785. c= block[stride*1 + xStride*0];
  1786. d= block[stride*1 + xStride*1];
  1787. e= a-b;
  1788. a= a+b;
  1789. b= c-d;
  1790. c= c+d;
  1791. block[stride*0 + xStride*0]= (a+c);
  1792. block[stride*0 + xStride*1]= (e+b);
  1793. block[stride*1 + xStride*0]= (a-c);
  1794. block[stride*1 + xStride*1]= (e-b);
  1795. }
  1796. #endif
  1797. /**
  1798. * gets the chroma qp.
  1799. */
  1800. static inline int get_chroma_qp(int chroma_qp_index_offset, int qscale){
  1801. return chroma_qp[clip(qscale + chroma_qp_index_offset, 0, 51)];
  1802. }
  1803. #if 0
  1804. static void h264_diff_dct_c(DCTELEM *block, uint8_t *src1, uint8_t *src2, int stride){
  1805. int i;
  1806. //FIXME try int temp instead of block
  1807. for(i=0; i<4; i++){
  1808. const int d0= src1[0 + i*stride] - src2[0 + i*stride];
  1809. const int d1= src1[1 + i*stride] - src2[1 + i*stride];
  1810. const int d2= src1[2 + i*stride] - src2[2 + i*stride];
  1811. const int d3= src1[3 + i*stride] - src2[3 + i*stride];
  1812. const int z0= d0 + d3;
  1813. const int z3= d0 - d3;
  1814. const int z1= d1 + d2;
  1815. const int z2= d1 - d2;
  1816. block[0 + 4*i]= z0 + z1;
  1817. block[1 + 4*i]= 2*z3 + z2;
  1818. block[2 + 4*i]= z0 - z1;
  1819. block[3 + 4*i]= z3 - 2*z2;
  1820. }
  1821. for(i=0; i<4; i++){
  1822. const int z0= block[0*4 + i] + block[3*4 + i];
  1823. const int z3= block[0*4 + i] - block[3*4 + i];
  1824. const int z1= block[1*4 + i] + block[2*4 + i];
  1825. const int z2= block[1*4 + i] - block[2*4 + i];
  1826. block[0*4 + i]= z0 + z1;
  1827. block[1*4 + i]= 2*z3 + z2;
  1828. block[2*4 + i]= z0 - z1;
  1829. block[3*4 + i]= z3 - 2*z2;
  1830. }
  1831. }
  1832. #endif
  1833. //FIXME need to check that this doesnt overflow signed 32 bit for low qp, i am not sure, it's very close
  1834. //FIXME check that gcc inlines this (and optimizes intra & seperate_dc stuff away)
  1835. static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int seperate_dc){
  1836. int i;
  1837. const int * const quant_table= quant_coeff[qscale];
  1838. const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
  1839. const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
  1840. const unsigned int threshold2= (threshold1<<1);
  1841. int last_non_zero;
  1842. if(seperate_dc){
  1843. if(qscale<=18){
  1844. //avoid overflows
  1845. const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
  1846. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
  1847. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1848. int level= block[0]*quant_coeff[qscale+18][0];
  1849. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1850. if(level>0){
  1851. level= (dc_bias + level)>>(QUANT_SHIFT-2);
  1852. block[0]= level;
  1853. }else{
  1854. level= (dc_bias - level)>>(QUANT_SHIFT-2);
  1855. block[0]= -level;
  1856. }
  1857. // last_non_zero = i;
  1858. }else{
  1859. block[0]=0;
  1860. }
  1861. }else{
  1862. const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
  1863. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
  1864. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1865. int level= block[0]*quant_table[0];
  1866. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1867. if(level>0){
  1868. level= (dc_bias + level)>>(QUANT_SHIFT+1);
  1869. block[0]= level;
  1870. }else{
  1871. level= (dc_bias - level)>>(QUANT_SHIFT+1);
  1872. block[0]= -level;
  1873. }
  1874. // last_non_zero = i;
  1875. }else{
  1876. block[0]=0;
  1877. }
  1878. }
  1879. last_non_zero= 0;
  1880. i=1;
  1881. }else{
  1882. last_non_zero= -1;
  1883. i=0;
  1884. }
  1885. for(; i<16; i++){
  1886. const int j= scantable[i];
  1887. int level= block[j]*quant_table[j];
  1888. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  1889. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  1890. if(((unsigned)(level+threshold1))>threshold2){
  1891. if(level>0){
  1892. level= (bias + level)>>QUANT_SHIFT;
  1893. block[j]= level;
  1894. }else{
  1895. level= (bias - level)>>QUANT_SHIFT;
  1896. block[j]= -level;
  1897. }
  1898. last_non_zero = i;
  1899. }else{
  1900. block[j]=0;
  1901. }
  1902. }
  1903. return last_non_zero;
  1904. }
  1905. static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){
  1906. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1907. ((uint32_t*)(src+0*stride))[0]= a;
  1908. ((uint32_t*)(src+1*stride))[0]= a;
  1909. ((uint32_t*)(src+2*stride))[0]= a;
  1910. ((uint32_t*)(src+3*stride))[0]= a;
  1911. }
  1912. static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){
  1913. ((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101;
  1914. ((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101;
  1915. ((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101;
  1916. ((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101;
  1917. }
  1918. static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1919. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
  1920. + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
  1921. ((uint32_t*)(src+0*stride))[0]=
  1922. ((uint32_t*)(src+1*stride))[0]=
  1923. ((uint32_t*)(src+2*stride))[0]=
  1924. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1925. }
  1926. static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1927. const int dc= ( src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
  1928. ((uint32_t*)(src+0*stride))[0]=
  1929. ((uint32_t*)(src+1*stride))[0]=
  1930. ((uint32_t*)(src+2*stride))[0]=
  1931. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1932. }
  1933. static void pred4x4_top_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1934. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2;
  1935. ((uint32_t*)(src+0*stride))[0]=
  1936. ((uint32_t*)(src+1*stride))[0]=
  1937. ((uint32_t*)(src+2*stride))[0]=
  1938. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1939. }
  1940. static void pred4x4_128_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1941. ((uint32_t*)(src+0*stride))[0]=
  1942. ((uint32_t*)(src+1*stride))[0]=
  1943. ((uint32_t*)(src+2*stride))[0]=
  1944. ((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U;
  1945. }
  1946. #define LOAD_TOP_RIGHT_EDGE\
  1947. const int t4= topright[0];\
  1948. const int t5= topright[1];\
  1949. const int t6= topright[2];\
  1950. const int t7= topright[3];\
  1951. #define LOAD_LEFT_EDGE\
  1952. const int l0= src[-1+0*stride];\
  1953. const int l1= src[-1+1*stride];\
  1954. const int l2= src[-1+2*stride];\
  1955. const int l3= src[-1+3*stride];\
  1956. #define LOAD_TOP_EDGE\
  1957. const int t0= src[ 0-1*stride];\
  1958. const int t1= src[ 1-1*stride];\
  1959. const int t2= src[ 2-1*stride];\
  1960. const int t3= src[ 3-1*stride];\
  1961. static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){
  1962. const int lt= src[-1-1*stride];
  1963. LOAD_TOP_EDGE
  1964. LOAD_LEFT_EDGE
  1965. src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
  1966. src[0+2*stride]=
  1967. src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
  1968. src[0+1*stride]=
  1969. src[1+2*stride]=
  1970. src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
  1971. src[0+0*stride]=
  1972. src[1+1*stride]=
  1973. src[2+2*stride]=
  1974. src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1975. src[1+0*stride]=
  1976. src[2+1*stride]=
  1977. src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1978. src[2+0*stride]=
  1979. src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1980. src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1981. }
  1982. static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){
  1983. LOAD_TOP_EDGE
  1984. LOAD_TOP_RIGHT_EDGE
  1985. // LOAD_LEFT_EDGE
  1986. src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
  1987. src[1+0*stride]=
  1988. src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
  1989. src[2+0*stride]=
  1990. src[1+1*stride]=
  1991. src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
  1992. src[3+0*stride]=
  1993. src[2+1*stride]=
  1994. src[1+2*stride]=
  1995. src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
  1996. src[3+1*stride]=
  1997. src[2+2*stride]=
  1998. src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
  1999. src[3+2*stride]=
  2000. src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
  2001. src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
  2002. }
  2003. static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
  2004. const int lt= src[-1-1*stride];
  2005. LOAD_TOP_EDGE
  2006. LOAD_LEFT_EDGE
  2007. const __attribute__((unused)) int unu= l3;
  2008. src[0+0*stride]=
  2009. src[1+2*stride]=(lt + t0 + 1)>>1;
  2010. src[1+0*stride]=
  2011. src[2+2*stride]=(t0 + t1 + 1)>>1;
  2012. src[2+0*stride]=
  2013. src[3+2*stride]=(t1 + t2 + 1)>>1;
  2014. src[3+0*stride]=(t2 + t3 + 1)>>1;
  2015. src[0+1*stride]=
  2016. src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  2017. src[1+1*stride]=
  2018. src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
  2019. src[2+1*stride]=
  2020. src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  2021. src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  2022. src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  2023. src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  2024. }
  2025. static void pred4x4_vertical_left_c(uint8_t *src, uint8_t *topright, int stride){
  2026. LOAD_TOP_EDGE
  2027. LOAD_TOP_RIGHT_EDGE
  2028. const __attribute__((unused)) int unu= t7;
  2029. src[0+0*stride]=(t0 + t1 + 1)>>1;
  2030. src[1+0*stride]=
  2031. src[0+2*stride]=(t1 + t2 + 1)>>1;
  2032. src[2+0*stride]=
  2033. src[1+2*stride]=(t2 + t3 + 1)>>1;
  2034. src[3+0*stride]=
  2035. src[2+2*stride]=(t3 + t4+ 1)>>1;
  2036. src[3+2*stride]=(t4 + t5+ 1)>>1;
  2037. src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  2038. src[1+1*stride]=
  2039. src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  2040. src[2+1*stride]=
  2041. src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
  2042. src[3+1*stride]=
  2043. src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
  2044. src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
  2045. }
  2046. static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){
  2047. LOAD_LEFT_EDGE
  2048. src[0+0*stride]=(l0 + l1 + 1)>>1;
  2049. src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  2050. src[2+0*stride]=
  2051. src[0+1*stride]=(l1 + l2 + 1)>>1;
  2052. src[3+0*stride]=
  2053. src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  2054. src[2+1*stride]=
  2055. src[0+2*stride]=(l2 + l3 + 1)>>1;
  2056. src[3+1*stride]=
  2057. src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
  2058. src[3+2*stride]=
  2059. src[1+3*stride]=
  2060. src[0+3*stride]=
  2061. src[2+2*stride]=
  2062. src[2+3*stride]=
  2063. src[3+3*stride]=l3;
  2064. }
  2065. static void pred4x4_horizontal_down_c(uint8_t *src, uint8_t *topright, int stride){
  2066. const int lt= src[-1-1*stride];
  2067. LOAD_TOP_EDGE
  2068. LOAD_LEFT_EDGE
  2069. const __attribute__((unused)) int unu= t3;
  2070. src[0+0*stride]=
  2071. src[2+1*stride]=(lt + l0 + 1)>>1;
  2072. src[1+0*stride]=
  2073. src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
  2074. src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
  2075. src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  2076. src[0+1*stride]=
  2077. src[2+2*stride]=(l0 + l1 + 1)>>1;
  2078. src[1+1*stride]=
  2079. src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  2080. src[0+2*stride]=
  2081. src[2+3*stride]=(l1 + l2+ 1)>>1;
  2082. src[1+2*stride]=
  2083. src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  2084. src[0+3*stride]=(l2 + l3 + 1)>>1;
  2085. src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  2086. }
  2087. static void pred16x16_vertical_c(uint8_t *src, int stride){
  2088. int i;
  2089. const uint32_t a= ((uint32_t*)(src-stride))[0];
  2090. const uint32_t b= ((uint32_t*)(src-stride))[1];
  2091. const uint32_t c= ((uint32_t*)(src-stride))[2];
  2092. const uint32_t d= ((uint32_t*)(src-stride))[3];
  2093. for(i=0; i<16; i++){
  2094. ((uint32_t*)(src+i*stride))[0]= a;
  2095. ((uint32_t*)(src+i*stride))[1]= b;
  2096. ((uint32_t*)(src+i*stride))[2]= c;
  2097. ((uint32_t*)(src+i*stride))[3]= d;
  2098. }
  2099. }
  2100. static void pred16x16_horizontal_c(uint8_t *src, int stride){
  2101. int i;
  2102. for(i=0; i<16; i++){
  2103. ((uint32_t*)(src+i*stride))[0]=
  2104. ((uint32_t*)(src+i*stride))[1]=
  2105. ((uint32_t*)(src+i*stride))[2]=
  2106. ((uint32_t*)(src+i*stride))[3]= src[-1+i*stride]*0x01010101;
  2107. }
  2108. }
  2109. static void pred16x16_dc_c(uint8_t *src, int stride){
  2110. int i, dc=0;
  2111. for(i=0;i<16; i++){
  2112. dc+= src[-1+i*stride];
  2113. }
  2114. for(i=0;i<16; i++){
  2115. dc+= src[i-stride];
  2116. }
  2117. dc= 0x01010101*((dc + 16)>>5);
  2118. for(i=0; i<16; i++){
  2119. ((uint32_t*)(src+i*stride))[0]=
  2120. ((uint32_t*)(src+i*stride))[1]=
  2121. ((uint32_t*)(src+i*stride))[2]=
  2122. ((uint32_t*)(src+i*stride))[3]= dc;
  2123. }
  2124. }
  2125. static void pred16x16_left_dc_c(uint8_t *src, int stride){
  2126. int i, dc=0;
  2127. for(i=0;i<16; i++){
  2128. dc+= src[-1+i*stride];
  2129. }
  2130. dc= 0x01010101*((dc + 8)>>4);
  2131. for(i=0; i<16; i++){
  2132. ((uint32_t*)(src+i*stride))[0]=
  2133. ((uint32_t*)(src+i*stride))[1]=
  2134. ((uint32_t*)(src+i*stride))[2]=
  2135. ((uint32_t*)(src+i*stride))[3]= dc;
  2136. }
  2137. }
  2138. static void pred16x16_top_dc_c(uint8_t *src, int stride){
  2139. int i, dc=0;
  2140. for(i=0;i<16; i++){
  2141. dc+= src[i-stride];
  2142. }
  2143. dc= 0x01010101*((dc + 8)>>4);
  2144. for(i=0; i<16; i++){
  2145. ((uint32_t*)(src+i*stride))[0]=
  2146. ((uint32_t*)(src+i*stride))[1]=
  2147. ((uint32_t*)(src+i*stride))[2]=
  2148. ((uint32_t*)(src+i*stride))[3]= dc;
  2149. }
  2150. }
  2151. static void pred16x16_128_dc_c(uint8_t *src, int stride){
  2152. int i;
  2153. for(i=0; i<16; i++){
  2154. ((uint32_t*)(src+i*stride))[0]=
  2155. ((uint32_t*)(src+i*stride))[1]=
  2156. ((uint32_t*)(src+i*stride))[2]=
  2157. ((uint32_t*)(src+i*stride))[3]= 0x01010101U*128U;
  2158. }
  2159. }
  2160. static inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){
  2161. int i, j, k;
  2162. int a;
  2163. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  2164. const uint8_t * const src0 = src+7-stride;
  2165. const uint8_t *src1 = src+8*stride-1;
  2166. const uint8_t *src2 = src1-2*stride; // == src+6*stride-1;
  2167. int H = src0[1] - src0[-1];
  2168. int V = src1[0] - src2[ 0];
  2169. for(k=2; k<=8; ++k) {
  2170. src1 += stride; src2 -= stride;
  2171. H += k*(src0[k] - src0[-k]);
  2172. V += k*(src1[0] - src2[ 0]);
  2173. }
  2174. if(svq3){
  2175. H = ( 5*(H/4) ) / 16;
  2176. V = ( 5*(V/4) ) / 16;
  2177. /* required for 100% accuracy */
  2178. i = H; H = V; V = i;
  2179. }else{
  2180. H = ( 5*H+32 ) >> 6;
  2181. V = ( 5*V+32 ) >> 6;
  2182. }
  2183. a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
  2184. for(j=16; j>0; --j) {
  2185. int b = a;
  2186. a += V;
  2187. for(i=-16; i<0; i+=4) {
  2188. src[16+i] = cm[ (b ) >> 5 ];
  2189. src[17+i] = cm[ (b+ H) >> 5 ];
  2190. src[18+i] = cm[ (b+2*H) >> 5 ];
  2191. src[19+i] = cm[ (b+3*H) >> 5 ];
  2192. b += 4*H;
  2193. }
  2194. src += stride;
  2195. }
  2196. }
  2197. static void pred16x16_plane_c(uint8_t *src, int stride){
  2198. pred16x16_plane_compat_c(src, stride, 0);
  2199. }
  2200. static void pred8x8_vertical_c(uint8_t *src, int stride){
  2201. int i;
  2202. const uint32_t a= ((uint32_t*)(src-stride))[0];
  2203. const uint32_t b= ((uint32_t*)(src-stride))[1];
  2204. for(i=0; i<8; i++){
  2205. ((uint32_t*)(src+i*stride))[0]= a;
  2206. ((uint32_t*)(src+i*stride))[1]= b;
  2207. }
  2208. }
  2209. static void pred8x8_horizontal_c(uint8_t *src, int stride){
  2210. int i;
  2211. for(i=0; i<8; i++){
  2212. ((uint32_t*)(src+i*stride))[0]=
  2213. ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101;
  2214. }
  2215. }
  2216. static void pred8x8_128_dc_c(uint8_t *src, int stride){
  2217. int i;
  2218. for(i=0; i<8; i++){
  2219. ((uint32_t*)(src+i*stride))[0]=
  2220. ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
  2221. }
  2222. }
  2223. static void pred8x8_left_dc_c(uint8_t *src, int stride){
  2224. int i;
  2225. int dc0, dc2;
  2226. dc0=dc2=0;
  2227. for(i=0;i<4; i++){
  2228. dc0+= src[-1+i*stride];
  2229. dc2+= src[-1+(i+4)*stride];
  2230. }
  2231. dc0= 0x01010101*((dc0 + 2)>>2);
  2232. dc2= 0x01010101*((dc2 + 2)>>2);
  2233. for(i=0; i<4; i++){
  2234. ((uint32_t*)(src+i*stride))[0]=
  2235. ((uint32_t*)(src+i*stride))[1]= dc0;
  2236. }
  2237. for(i=4; i<8; i++){
  2238. ((uint32_t*)(src+i*stride))[0]=
  2239. ((uint32_t*)(src+i*stride))[1]= dc2;
  2240. }
  2241. }
  2242. static void pred8x8_top_dc_c(uint8_t *src, int stride){
  2243. int i;
  2244. int dc0, dc1;
  2245. dc0=dc1=0;
  2246. for(i=0;i<4; i++){
  2247. dc0+= src[i-stride];
  2248. dc1+= src[4+i-stride];
  2249. }
  2250. dc0= 0x01010101*((dc0 + 2)>>2);
  2251. dc1= 0x01010101*((dc1 + 2)>>2);
  2252. for(i=0; i<4; i++){
  2253. ((uint32_t*)(src+i*stride))[0]= dc0;
  2254. ((uint32_t*)(src+i*stride))[1]= dc1;
  2255. }
  2256. for(i=4; i<8; i++){
  2257. ((uint32_t*)(src+i*stride))[0]= dc0;
  2258. ((uint32_t*)(src+i*stride))[1]= dc1;
  2259. }
  2260. }
  2261. static void pred8x8_dc_c(uint8_t *src, int stride){
  2262. int i;
  2263. int dc0, dc1, dc2, dc3;
  2264. dc0=dc1=dc2=0;
  2265. for(i=0;i<4; i++){
  2266. dc0+= src[-1+i*stride] + src[i-stride];
  2267. dc1+= src[4+i-stride];
  2268. dc2+= src[-1+(i+4)*stride];
  2269. }
  2270. dc3= 0x01010101*((dc1 + dc2 + 4)>>3);
  2271. dc0= 0x01010101*((dc0 + 4)>>3);
  2272. dc1= 0x01010101*((dc1 + 2)>>2);
  2273. dc2= 0x01010101*((dc2 + 2)>>2);
  2274. for(i=0; i<4; i++){
  2275. ((uint32_t*)(src+i*stride))[0]= dc0;
  2276. ((uint32_t*)(src+i*stride))[1]= dc1;
  2277. }
  2278. for(i=4; i<8; i++){
  2279. ((uint32_t*)(src+i*stride))[0]= dc2;
  2280. ((uint32_t*)(src+i*stride))[1]= dc3;
  2281. }
  2282. }
  2283. static void pred8x8_plane_c(uint8_t *src, int stride){
  2284. int j, k;
  2285. int a;
  2286. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  2287. const uint8_t * const src0 = src+3-stride;
  2288. const uint8_t *src1 = src+4*stride-1;
  2289. const uint8_t *src2 = src1-2*stride; // == src+2*stride-1;
  2290. int H = src0[1] - src0[-1];
  2291. int V = src1[0] - src2[ 0];
  2292. for(k=2; k<=4; ++k) {
  2293. src1 += stride; src2 -= stride;
  2294. H += k*(src0[k] - src0[-k]);
  2295. V += k*(src1[0] - src2[ 0]);
  2296. }
  2297. H = ( 17*H+16 ) >> 5;
  2298. V = ( 17*V+16 ) >> 5;
  2299. a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
  2300. for(j=8; j>0; --j) {
  2301. int b = a;
  2302. a += V;
  2303. src[0] = cm[ (b ) >> 5 ];
  2304. src[1] = cm[ (b+ H) >> 5 ];
  2305. src[2] = cm[ (b+2*H) >> 5 ];
  2306. src[3] = cm[ (b+3*H) >> 5 ];
  2307. src[4] = cm[ (b+4*H) >> 5 ];
  2308. src[5] = cm[ (b+5*H) >> 5 ];
  2309. src[6] = cm[ (b+6*H) >> 5 ];
  2310. src[7] = cm[ (b+7*H) >> 5 ];
  2311. src += stride;
  2312. }
  2313. }
  2314. #define SRC(x,y) src[(x)+(y)*stride]
  2315. #define PL(y) \
  2316. const int l##y = (SRC(-1,y-1) + 2*SRC(-1,y) + SRC(-1,y+1) + 2) >> 2;
  2317. #define PREDICT_8x8_LOAD_LEFT \
  2318. const int l0 = ((has_topleft ? SRC(-1,-1) : SRC(-1,0)) \
  2319. + 2*SRC(-1,0) + SRC(-1,1) + 2) >> 2; \
  2320. PL(1) PL(2) PL(3) PL(4) PL(5) PL(6) \
  2321. const int l7 attribute_unused = (SRC(-1,6) + 3*SRC(-1,7) + 2) >> 2
  2322. #define PT(x) \
  2323. const int t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  2324. #define PREDICT_8x8_LOAD_TOP \
  2325. const int t0 = ((has_topleft ? SRC(-1,-1) : SRC(0,-1)) \
  2326. + 2*SRC(0,-1) + SRC(1,-1) + 2) >> 2; \
  2327. PT(1) PT(2) PT(3) PT(4) PT(5) PT(6) \
  2328. const int t7 attribute_unused = ((has_topright ? SRC(8,-1) : SRC(7,-1)) \
  2329. + 2*SRC(7,-1) + SRC(6,-1) + 2) >> 2
  2330. #define PTR(x) \
  2331. t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  2332. #define PREDICT_8x8_LOAD_TOPRIGHT \
  2333. int t8, t9, t10, t11, t12, t13, t14, t15; \
  2334. if(has_topright) { \
  2335. PTR(8) PTR(9) PTR(10) PTR(11) PTR(12) PTR(13) PTR(14) \
  2336. t15 = (SRC(14,-1) + 3*SRC(15,-1) + 2) >> 2; \
  2337. } else t8=t9=t10=t11=t12=t13=t14=t15= SRC(7,-1);
  2338. #define PREDICT_8x8_LOAD_TOPLEFT \
  2339. const int lt = (SRC(-1,0) + 2*SRC(-1,-1) + SRC(0,-1) + 2) >> 2
  2340. #define PREDICT_8x8_DC(v) \
  2341. int y; \
  2342. for( y = 0; y < 8; y++ ) { \
  2343. ((uint32_t*)src)[0] = \
  2344. ((uint32_t*)src)[1] = v; \
  2345. src += stride; \
  2346. }
  2347. static void pred8x8l_128_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2348. {
  2349. PREDICT_8x8_DC(0x80808080);
  2350. }
  2351. static void pred8x8l_left_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2352. {
  2353. PREDICT_8x8_LOAD_LEFT;
  2354. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7+4) >> 3) * 0x01010101;
  2355. PREDICT_8x8_DC(dc);
  2356. }
  2357. static void pred8x8l_top_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2358. {
  2359. PREDICT_8x8_LOAD_TOP;
  2360. const uint32_t dc = ((t0+t1+t2+t3+t4+t5+t6+t7+4) >> 3) * 0x01010101;
  2361. PREDICT_8x8_DC(dc);
  2362. }
  2363. static void pred8x8l_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2364. {
  2365. PREDICT_8x8_LOAD_LEFT;
  2366. PREDICT_8x8_LOAD_TOP;
  2367. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7
  2368. +t0+t1+t2+t3+t4+t5+t6+t7+8) >> 4) * 0x01010101;
  2369. PREDICT_8x8_DC(dc);
  2370. }
  2371. static void pred8x8l_horizontal_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2372. {
  2373. PREDICT_8x8_LOAD_LEFT;
  2374. #define ROW(y) ((uint32_t*)(src+y*stride))[0] =\
  2375. ((uint32_t*)(src+y*stride))[1] = 0x01010101 * l##y
  2376. ROW(0); ROW(1); ROW(2); ROW(3); ROW(4); ROW(5); ROW(6); ROW(7);
  2377. #undef ROW
  2378. }
  2379. static void pred8x8l_vertical_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2380. {
  2381. int y;
  2382. PREDICT_8x8_LOAD_TOP;
  2383. src[0] = t0;
  2384. src[1] = t1;
  2385. src[2] = t2;
  2386. src[3] = t3;
  2387. src[4] = t4;
  2388. src[5] = t5;
  2389. src[6] = t6;
  2390. src[7] = t7;
  2391. for( y = 1; y < 8; y++ )
  2392. *(uint64_t*)(src+y*stride) = *(uint64_t*)src;
  2393. }
  2394. static void pred8x8l_down_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2395. {
  2396. PREDICT_8x8_LOAD_TOP;
  2397. PREDICT_8x8_LOAD_TOPRIGHT;
  2398. SRC(0,0)= (t0 + 2*t1 + t2 + 2) >> 2;
  2399. SRC(0,1)=SRC(1,0)= (t1 + 2*t2 + t3 + 2) >> 2;
  2400. SRC(0,2)=SRC(1,1)=SRC(2,0)= (t2 + 2*t3 + t4 + 2) >> 2;
  2401. SRC(0,3)=SRC(1,2)=SRC(2,1)=SRC(3,0)= (t3 + 2*t4 + t5 + 2) >> 2;
  2402. SRC(0,4)=SRC(1,3)=SRC(2,2)=SRC(3,1)=SRC(4,0)= (t4 + 2*t5 + t6 + 2) >> 2;
  2403. SRC(0,5)=SRC(1,4)=SRC(2,3)=SRC(3,2)=SRC(4,1)=SRC(5,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2404. 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;
  2405. 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;
  2406. 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;
  2407. SRC(2,7)=SRC(3,6)=SRC(4,5)=SRC(5,4)=SRC(6,3)=SRC(7,2)= (t9 + 2*t10 + t11 + 2) >> 2;
  2408. SRC(3,7)=SRC(4,6)=SRC(5,5)=SRC(6,4)=SRC(7,3)= (t10 + 2*t11 + t12 + 2) >> 2;
  2409. SRC(4,7)=SRC(5,6)=SRC(6,5)=SRC(7,4)= (t11 + 2*t12 + t13 + 2) >> 2;
  2410. SRC(5,7)=SRC(6,6)=SRC(7,5)= (t12 + 2*t13 + t14 + 2) >> 2;
  2411. SRC(6,7)=SRC(7,6)= (t13 + 2*t14 + t15 + 2) >> 2;
  2412. SRC(7,7)= (t14 + 3*t15 + 2) >> 2;
  2413. }
  2414. static void pred8x8l_down_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2415. {
  2416. PREDICT_8x8_LOAD_TOP;
  2417. PREDICT_8x8_LOAD_LEFT;
  2418. PREDICT_8x8_LOAD_TOPLEFT;
  2419. SRC(0,7)= (l7 + 2*l6 + l5 + 2) >> 2;
  2420. SRC(0,6)=SRC(1,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2421. SRC(0,5)=SRC(1,6)=SRC(2,7)= (l5 + 2*l4 + l3 + 2) >> 2;
  2422. SRC(0,4)=SRC(1,5)=SRC(2,6)=SRC(3,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2423. SRC(0,3)=SRC(1,4)=SRC(2,5)=SRC(3,6)=SRC(4,7)= (l3 + 2*l2 + l1 + 2) >> 2;
  2424. SRC(0,2)=SRC(1,3)=SRC(2,4)=SRC(3,5)=SRC(4,6)=SRC(5,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2425. 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;
  2426. 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;
  2427. 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;
  2428. SRC(2,0)=SRC(3,1)=SRC(4,2)=SRC(5,3)=SRC(6,4)=SRC(7,5)= (t0 + 2*t1 + t2 + 2) >> 2;
  2429. SRC(3,0)=SRC(4,1)=SRC(5,2)=SRC(6,3)=SRC(7,4)= (t1 + 2*t2 + t3 + 2) >> 2;
  2430. SRC(4,0)=SRC(5,1)=SRC(6,2)=SRC(7,3)= (t2 + 2*t3 + t4 + 2) >> 2;
  2431. SRC(5,0)=SRC(6,1)=SRC(7,2)= (t3 + 2*t4 + t5 + 2) >> 2;
  2432. SRC(6,0)=SRC(7,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2433. SRC(7,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2434. }
  2435. static void pred8x8l_vertical_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2436. {
  2437. PREDICT_8x8_LOAD_TOP;
  2438. PREDICT_8x8_LOAD_LEFT;
  2439. PREDICT_8x8_LOAD_TOPLEFT;
  2440. SRC(0,6)= (l5 + 2*l4 + l3 + 2) >> 2;
  2441. SRC(0,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2442. SRC(0,4)=SRC(1,6)= (l3 + 2*l2 + l1 + 2) >> 2;
  2443. SRC(0,5)=SRC(1,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2444. SRC(0,2)=SRC(1,4)=SRC(2,6)= (l1 + 2*l0 + lt + 2) >> 2;
  2445. SRC(0,3)=SRC(1,5)=SRC(2,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2446. SRC(0,1)=SRC(1,3)=SRC(2,5)=SRC(3,7)= (l0 + 2*lt + t0 + 2) >> 2;
  2447. SRC(0,0)=SRC(1,2)=SRC(2,4)=SRC(3,6)= (lt + t0 + 1) >> 1;
  2448. SRC(1,1)=SRC(2,3)=SRC(3,5)=SRC(4,7)= (lt + 2*t0 + t1 + 2) >> 2;
  2449. SRC(1,0)=SRC(2,2)=SRC(3,4)=SRC(4,6)= (t0 + t1 + 1) >> 1;
  2450. SRC(2,1)=SRC(3,3)=SRC(4,5)=SRC(5,7)= (t0 + 2*t1 + t2 + 2) >> 2;
  2451. SRC(2,0)=SRC(3,2)=SRC(4,4)=SRC(5,6)= (t1 + t2 + 1) >> 1;
  2452. SRC(3,1)=SRC(4,3)=SRC(5,5)=SRC(6,7)= (t1 + 2*t2 + t3 + 2) >> 2;
  2453. SRC(3,0)=SRC(4,2)=SRC(5,4)=SRC(6,6)= (t2 + t3 + 1) >> 1;
  2454. SRC(4,1)=SRC(5,3)=SRC(6,5)=SRC(7,7)= (t2 + 2*t3 + t4 + 2) >> 2;
  2455. SRC(4,0)=SRC(5,2)=SRC(6,4)=SRC(7,6)= (t3 + t4 + 1) >> 1;
  2456. SRC(5,1)=SRC(6,3)=SRC(7,5)= (t3 + 2*t4 + t5 + 2) >> 2;
  2457. SRC(5,0)=SRC(6,2)=SRC(7,4)= (t4 + t5 + 1) >> 1;
  2458. SRC(6,1)=SRC(7,3)= (t4 + 2*t5 + t6 + 2) >> 2;
  2459. SRC(6,0)=SRC(7,2)= (t5 + t6 + 1) >> 1;
  2460. SRC(7,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2461. SRC(7,0)= (t6 + t7 + 1) >> 1;
  2462. }
  2463. static void pred8x8l_horizontal_down_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2464. {
  2465. PREDICT_8x8_LOAD_TOP;
  2466. PREDICT_8x8_LOAD_LEFT;
  2467. PREDICT_8x8_LOAD_TOPLEFT;
  2468. SRC(0,7)= (l6 + l7 + 1) >> 1;
  2469. SRC(1,7)= (l5 + 2*l6 + l7 + 2) >> 2;
  2470. SRC(0,6)=SRC(2,7)= (l5 + l6 + 1) >> 1;
  2471. SRC(1,6)=SRC(3,7)= (l4 + 2*l5 + l6 + 2) >> 2;
  2472. SRC(0,5)=SRC(2,6)=SRC(4,7)= (l4 + l5 + 1) >> 1;
  2473. SRC(1,5)=SRC(3,6)=SRC(5,7)= (l3 + 2*l4 + l5 + 2) >> 2;
  2474. SRC(0,4)=SRC(2,5)=SRC(4,6)=SRC(6,7)= (l3 + l4 + 1) >> 1;
  2475. SRC(1,4)=SRC(3,5)=SRC(5,6)=SRC(7,7)= (l2 + 2*l3 + l4 + 2) >> 2;
  2476. SRC(0,3)=SRC(2,4)=SRC(4,5)=SRC(6,6)= (l2 + l3 + 1) >> 1;
  2477. SRC(1,3)=SRC(3,4)=SRC(5,5)=SRC(7,6)= (l1 + 2*l2 + l3 + 2) >> 2;
  2478. SRC(0,2)=SRC(2,3)=SRC(4,4)=SRC(6,5)= (l1 + l2 + 1) >> 1;
  2479. SRC(1,2)=SRC(3,3)=SRC(5,4)=SRC(7,5)= (l0 + 2*l1 + l2 + 2) >> 2;
  2480. SRC(0,1)=SRC(2,2)=SRC(4,3)=SRC(6,4)= (l0 + l1 + 1) >> 1;
  2481. SRC(1,1)=SRC(3,2)=SRC(5,3)=SRC(7,4)= (lt + 2*l0 + l1 + 2) >> 2;
  2482. SRC(0,0)=SRC(2,1)=SRC(4,2)=SRC(6,3)= (lt + l0 + 1) >> 1;
  2483. SRC(1,0)=SRC(3,1)=SRC(5,2)=SRC(7,3)= (l0 + 2*lt + t0 + 2) >> 2;
  2484. SRC(2,0)=SRC(4,1)=SRC(6,2)= (t1 + 2*t0 + lt + 2) >> 2;
  2485. SRC(3,0)=SRC(5,1)=SRC(7,2)= (t2 + 2*t1 + t0 + 2) >> 2;
  2486. SRC(4,0)=SRC(6,1)= (t3 + 2*t2 + t1 + 2) >> 2;
  2487. SRC(5,0)=SRC(7,1)= (t4 + 2*t3 + t2 + 2) >> 2;
  2488. SRC(6,0)= (t5 + 2*t4 + t3 + 2) >> 2;
  2489. SRC(7,0)= (t6 + 2*t5 + t4 + 2) >> 2;
  2490. }
  2491. static void pred8x8l_vertical_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2492. {
  2493. PREDICT_8x8_LOAD_TOP;
  2494. PREDICT_8x8_LOAD_TOPRIGHT;
  2495. SRC(0,0)= (t0 + t1 + 1) >> 1;
  2496. SRC(0,1)= (t0 + 2*t1 + t2 + 2) >> 2;
  2497. SRC(0,2)=SRC(1,0)= (t1 + t2 + 1) >> 1;
  2498. SRC(0,3)=SRC(1,1)= (t1 + 2*t2 + t3 + 2) >> 2;
  2499. SRC(0,4)=SRC(1,2)=SRC(2,0)= (t2 + t3 + 1) >> 1;
  2500. SRC(0,5)=SRC(1,3)=SRC(2,1)= (t2 + 2*t3 + t4 + 2) >> 2;
  2501. SRC(0,6)=SRC(1,4)=SRC(2,2)=SRC(3,0)= (t3 + t4 + 1) >> 1;
  2502. SRC(0,7)=SRC(1,5)=SRC(2,3)=SRC(3,1)= (t3 + 2*t4 + t5 + 2) >> 2;
  2503. SRC(1,6)=SRC(2,4)=SRC(3,2)=SRC(4,0)= (t4 + t5 + 1) >> 1;
  2504. SRC(1,7)=SRC(2,5)=SRC(3,3)=SRC(4,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2505. SRC(2,6)=SRC(3,4)=SRC(4,2)=SRC(5,0)= (t5 + t6 + 1) >> 1;
  2506. SRC(2,7)=SRC(3,5)=SRC(4,3)=SRC(5,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2507. SRC(3,6)=SRC(4,4)=SRC(5,2)=SRC(6,0)= (t6 + t7 + 1) >> 1;
  2508. SRC(3,7)=SRC(4,5)=SRC(5,3)=SRC(6,1)= (t6 + 2*t7 + t8 + 2) >> 2;
  2509. SRC(4,6)=SRC(5,4)=SRC(6,2)=SRC(7,0)= (t7 + t8 + 1) >> 1;
  2510. SRC(4,7)=SRC(5,5)=SRC(6,3)=SRC(7,1)= (t7 + 2*t8 + t9 + 2) >> 2;
  2511. SRC(5,6)=SRC(6,4)=SRC(7,2)= (t8 + t9 + 1) >> 1;
  2512. SRC(5,7)=SRC(6,5)=SRC(7,3)= (t8 + 2*t9 + t10 + 2) >> 2;
  2513. SRC(6,6)=SRC(7,4)= (t9 + t10 + 1) >> 1;
  2514. SRC(6,7)=SRC(7,5)= (t9 + 2*t10 + t11 + 2) >> 2;
  2515. SRC(7,6)= (t10 + t11 + 1) >> 1;
  2516. SRC(7,7)= (t10 + 2*t11 + t12 + 2) >> 2;
  2517. }
  2518. static void pred8x8l_horizontal_up_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2519. {
  2520. PREDICT_8x8_LOAD_LEFT;
  2521. SRC(0,0)= (l0 + l1 + 1) >> 1;
  2522. SRC(1,0)= (l0 + 2*l1 + l2 + 2) >> 2;
  2523. SRC(0,1)=SRC(2,0)= (l1 + l2 + 1) >> 1;
  2524. SRC(1,1)=SRC(3,0)= (l1 + 2*l2 + l3 + 2) >> 2;
  2525. SRC(0,2)=SRC(2,1)=SRC(4,0)= (l2 + l3 + 1) >> 1;
  2526. SRC(1,2)=SRC(3,1)=SRC(5,0)= (l2 + 2*l3 + l4 + 2) >> 2;
  2527. SRC(0,3)=SRC(2,2)=SRC(4,1)=SRC(6,0)= (l3 + l4 + 1) >> 1;
  2528. SRC(1,3)=SRC(3,2)=SRC(5,1)=SRC(7,0)= (l3 + 2*l4 + l5 + 2) >> 2;
  2529. SRC(0,4)=SRC(2,3)=SRC(4,2)=SRC(6,1)= (l4 + l5 + 1) >> 1;
  2530. SRC(1,4)=SRC(3,3)=SRC(5,2)=SRC(7,1)= (l4 + 2*l5 + l6 + 2) >> 2;
  2531. SRC(0,5)=SRC(2,4)=SRC(4,3)=SRC(6,2)= (l5 + l6 + 1) >> 1;
  2532. SRC(1,5)=SRC(3,4)=SRC(5,3)=SRC(7,2)= (l5 + 2*l6 + l7 + 2) >> 2;
  2533. SRC(0,6)=SRC(2,5)=SRC(4,4)=SRC(6,3)= (l6 + l7 + 1) >> 1;
  2534. SRC(1,6)=SRC(3,5)=SRC(5,4)=SRC(7,3)= (l6 + 3*l7 + 2) >> 2;
  2535. SRC(0,7)=SRC(1,7)=SRC(2,6)=SRC(2,7)=SRC(3,6)=
  2536. SRC(3,7)=SRC(4,5)=SRC(4,6)=SRC(4,7)=SRC(5,5)=
  2537. SRC(5,6)=SRC(5,7)=SRC(6,4)=SRC(6,5)=SRC(6,6)=
  2538. SRC(6,7)=SRC(7,4)=SRC(7,5)=SRC(7,6)=SRC(7,7)= l7;
  2539. }
  2540. #undef PREDICT_8x8_LOAD_LEFT
  2541. #undef PREDICT_8x8_LOAD_TOP
  2542. #undef PREDICT_8x8_LOAD_TOPLEFT
  2543. #undef PREDICT_8x8_LOAD_TOPRIGHT
  2544. #undef PREDICT_8x8_DC
  2545. #undef PTR
  2546. #undef PT
  2547. #undef PL
  2548. #undef SRC
  2549. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  2550. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2551. int src_x_offset, int src_y_offset,
  2552. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  2553. MpegEncContext * const s = &h->s;
  2554. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  2555. int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  2556. const int luma_xy= (mx&3) + ((my&3)<<2);
  2557. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
  2558. uint8_t * src_cb, * src_cr;
  2559. int extra_width= h->emu_edge_width;
  2560. int extra_height= h->emu_edge_height;
  2561. int emu=0;
  2562. const int full_mx= mx>>2;
  2563. const int full_my= my>>2;
  2564. const int pic_width = 16*s->mb_width;
  2565. const int pic_height = 16*s->mb_height >> MB_MBAFF;
  2566. if(!pic->data[0])
  2567. return;
  2568. if(mx&7) extra_width -= 3;
  2569. if(my&7) extra_height -= 3;
  2570. if( full_mx < 0-extra_width
  2571. || full_my < 0-extra_height
  2572. || full_mx + 16/*FIXME*/ > pic_width + extra_width
  2573. || full_my + 16/*FIXME*/ > pic_height + extra_height){
  2574. ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*h->mb_linesize, h->mb_linesize, 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
  2575. src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
  2576. emu=1;
  2577. }
  2578. qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
  2579. if(!square){
  2580. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
  2581. }
  2582. if(s->flags&CODEC_FLAG_GRAY) return;
  2583. if(MB_MBAFF){
  2584. // chroma offset when predicting from a field of opposite parity
  2585. my += 2 * ((s->mb_y & 1) - (h->ref_cache[list][scan8[n]] & 1));
  2586. emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
  2587. }
  2588. src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2589. src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2590. if(emu){
  2591. ff_emulated_edge_mc(s->edge_emu_buffer, src_cb, h->mb_uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
  2592. src_cb= s->edge_emu_buffer;
  2593. }
  2594. chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2595. if(emu){
  2596. ff_emulated_edge_mc(s->edge_emu_buffer, src_cr, h->mb_uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
  2597. src_cr= s->edge_emu_buffer;
  2598. }
  2599. chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2600. }
  2601. static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
  2602. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2603. int x_offset, int y_offset,
  2604. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2605. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2606. int list0, int list1){
  2607. MpegEncContext * const s = &h->s;
  2608. qpel_mc_func *qpix_op= qpix_put;
  2609. h264_chroma_mc_func chroma_op= chroma_put;
  2610. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2611. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2612. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2613. x_offset += 8*s->mb_x;
  2614. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2615. if(list0){
  2616. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  2617. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  2618. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2619. qpix_op, chroma_op);
  2620. qpix_op= qpix_avg;
  2621. chroma_op= chroma_avg;
  2622. }
  2623. if(list1){
  2624. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  2625. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  2626. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2627. qpix_op, chroma_op);
  2628. }
  2629. }
  2630. static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
  2631. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2632. int x_offset, int y_offset,
  2633. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2634. h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
  2635. h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
  2636. int list0, int list1){
  2637. MpegEncContext * const s = &h->s;
  2638. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2639. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2640. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2641. x_offset += 8*s->mb_x;
  2642. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2643. if(list0 && list1){
  2644. /* don't optimize for luma-only case, since B-frames usually
  2645. * use implicit weights => chroma too. */
  2646. uint8_t *tmp_cb = s->obmc_scratchpad;
  2647. uint8_t *tmp_cr = s->obmc_scratchpad + 8;
  2648. uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
  2649. int refn0 = h->ref_cache[0][ scan8[n] ];
  2650. int refn1 = h->ref_cache[1][ scan8[n] ];
  2651. mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
  2652. dest_y, dest_cb, dest_cr,
  2653. x_offset, y_offset, qpix_put, chroma_put);
  2654. mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
  2655. tmp_y, tmp_cb, tmp_cr,
  2656. x_offset, y_offset, qpix_put, chroma_put);
  2657. if(h->use_weight == 2){
  2658. int weight0 = h->implicit_weight[refn0][refn1];
  2659. int weight1 = 64 - weight0;
  2660. luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
  2661. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2662. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2663. }else{
  2664. luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
  2665. h->luma_weight[0][refn0], h->luma_weight[1][refn1],
  2666. h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
  2667. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2668. h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
  2669. h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
  2670. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2671. h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
  2672. h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
  2673. }
  2674. }else{
  2675. int list = list1 ? 1 : 0;
  2676. int refn = h->ref_cache[list][ scan8[n] ];
  2677. Picture *ref= &h->ref_list[list][refn];
  2678. mc_dir_part(h, ref, n, square, chroma_height, delta, list,
  2679. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2680. qpix_put, chroma_put);
  2681. luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
  2682. h->luma_weight[list][refn], h->luma_offset[list][refn]);
  2683. if(h->use_weight_chroma){
  2684. chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2685. h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
  2686. chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2687. h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
  2688. }
  2689. }
  2690. }
  2691. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  2692. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2693. int x_offset, int y_offset,
  2694. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2695. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2696. h264_weight_func *weight_op, h264_biweight_func *weight_avg,
  2697. int list0, int list1){
  2698. if((h->use_weight==2 && list0 && list1
  2699. && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
  2700. || h->use_weight==1)
  2701. mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2702. x_offset, y_offset, qpix_put, chroma_put,
  2703. weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
  2704. else
  2705. mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2706. x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
  2707. }
  2708. static inline void prefetch_motion(H264Context *h, int list){
  2709. /* fetch pixels for estimated mv 4 macroblocks ahead
  2710. * optimized for 64byte cache lines */
  2711. MpegEncContext * const s = &h->s;
  2712. const int refn = h->ref_cache[list][scan8[0]];
  2713. if(refn >= 0){
  2714. const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
  2715. const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
  2716. uint8_t **src= h->ref_list[list][refn].data;
  2717. int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
  2718. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  2719. off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
  2720. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  2721. }
  2722. }
  2723. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2724. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  2725. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
  2726. h264_weight_func *weight_op, h264_biweight_func *weight_avg){
  2727. MpegEncContext * const s = &h->s;
  2728. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2729. const int mb_type= s->current_picture.mb_type[mb_xy];
  2730. assert(IS_INTER(mb_type));
  2731. prefetch_motion(h, 0);
  2732. if(IS_16X16(mb_type)){
  2733. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  2734. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  2735. &weight_op[0], &weight_avg[0],
  2736. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2737. }else if(IS_16X8(mb_type)){
  2738. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  2739. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2740. &weight_op[1], &weight_avg[1],
  2741. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2742. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  2743. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2744. &weight_op[1], &weight_avg[1],
  2745. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2746. }else if(IS_8X16(mb_type)){
  2747. mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
  2748. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2749. &weight_op[2], &weight_avg[2],
  2750. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2751. mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
  2752. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2753. &weight_op[2], &weight_avg[2],
  2754. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2755. }else{
  2756. int i;
  2757. assert(IS_8X8(mb_type));
  2758. for(i=0; i<4; i++){
  2759. const int sub_mb_type= h->sub_mb_type[i];
  2760. const int n= 4*i;
  2761. int x_offset= (i&1)<<2;
  2762. int y_offset= (i&2)<<1;
  2763. if(IS_SUB_8X8(sub_mb_type)){
  2764. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2765. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2766. &weight_op[3], &weight_avg[3],
  2767. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2768. }else if(IS_SUB_8X4(sub_mb_type)){
  2769. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2770. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2771. &weight_op[4], &weight_avg[4],
  2772. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2773. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  2774. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2775. &weight_op[4], &weight_avg[4],
  2776. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2777. }else if(IS_SUB_4X8(sub_mb_type)){
  2778. mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2779. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2780. &weight_op[5], &weight_avg[5],
  2781. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2782. mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
  2783. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2784. &weight_op[5], &weight_avg[5],
  2785. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2786. }else{
  2787. int j;
  2788. assert(IS_SUB_4X4(sub_mb_type));
  2789. for(j=0; j<4; j++){
  2790. int sub_x_offset= x_offset + 2*(j&1);
  2791. int sub_y_offset= y_offset + (j&2);
  2792. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  2793. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2794. &weight_op[6], &weight_avg[6],
  2795. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2796. }
  2797. }
  2798. }
  2799. }
  2800. prefetch_motion(h, 1);
  2801. }
  2802. static void decode_init_vlc(H264Context *h){
  2803. static int done = 0;
  2804. if (!done) {
  2805. int i;
  2806. done = 1;
  2807. init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
  2808. &chroma_dc_coeff_token_len [0], 1, 1,
  2809. &chroma_dc_coeff_token_bits[0], 1, 1, 1);
  2810. for(i=0; i<4; i++){
  2811. init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
  2812. &coeff_token_len [i][0], 1, 1,
  2813. &coeff_token_bits[i][0], 1, 1, 1);
  2814. }
  2815. for(i=0; i<3; i++){
  2816. init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
  2817. &chroma_dc_total_zeros_len [i][0], 1, 1,
  2818. &chroma_dc_total_zeros_bits[i][0], 1, 1, 1);
  2819. }
  2820. for(i=0; i<15; i++){
  2821. init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
  2822. &total_zeros_len [i][0], 1, 1,
  2823. &total_zeros_bits[i][0], 1, 1, 1);
  2824. }
  2825. for(i=0; i<6; i++){
  2826. init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
  2827. &run_len [i][0], 1, 1,
  2828. &run_bits[i][0], 1, 1, 1);
  2829. }
  2830. init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
  2831. &run_len [6][0], 1, 1,
  2832. &run_bits[6][0], 1, 1, 1);
  2833. }
  2834. }
  2835. /**
  2836. * Sets the intra prediction function pointers.
  2837. */
  2838. static void init_pred_ptrs(H264Context *h){
  2839. // MpegEncContext * const s = &h->s;
  2840. h->pred4x4[VERT_PRED ]= pred4x4_vertical_c;
  2841. h->pred4x4[HOR_PRED ]= pred4x4_horizontal_c;
  2842. h->pred4x4[DC_PRED ]= pred4x4_dc_c;
  2843. h->pred4x4[DIAG_DOWN_LEFT_PRED ]= pred4x4_down_left_c;
  2844. h->pred4x4[DIAG_DOWN_RIGHT_PRED]= pred4x4_down_right_c;
  2845. h->pred4x4[VERT_RIGHT_PRED ]= pred4x4_vertical_right_c;
  2846. h->pred4x4[HOR_DOWN_PRED ]= pred4x4_horizontal_down_c;
  2847. h->pred4x4[VERT_LEFT_PRED ]= pred4x4_vertical_left_c;
  2848. h->pred4x4[HOR_UP_PRED ]= pred4x4_horizontal_up_c;
  2849. h->pred4x4[LEFT_DC_PRED ]= pred4x4_left_dc_c;
  2850. h->pred4x4[TOP_DC_PRED ]= pred4x4_top_dc_c;
  2851. h->pred4x4[DC_128_PRED ]= pred4x4_128_dc_c;
  2852. h->pred8x8l[VERT_PRED ]= pred8x8l_vertical_c;
  2853. h->pred8x8l[HOR_PRED ]= pred8x8l_horizontal_c;
  2854. h->pred8x8l[DC_PRED ]= pred8x8l_dc_c;
  2855. h->pred8x8l[DIAG_DOWN_LEFT_PRED ]= pred8x8l_down_left_c;
  2856. h->pred8x8l[DIAG_DOWN_RIGHT_PRED]= pred8x8l_down_right_c;
  2857. h->pred8x8l[VERT_RIGHT_PRED ]= pred8x8l_vertical_right_c;
  2858. h->pred8x8l[HOR_DOWN_PRED ]= pred8x8l_horizontal_down_c;
  2859. h->pred8x8l[VERT_LEFT_PRED ]= pred8x8l_vertical_left_c;
  2860. h->pred8x8l[HOR_UP_PRED ]= pred8x8l_horizontal_up_c;
  2861. h->pred8x8l[LEFT_DC_PRED ]= pred8x8l_left_dc_c;
  2862. h->pred8x8l[TOP_DC_PRED ]= pred8x8l_top_dc_c;
  2863. h->pred8x8l[DC_128_PRED ]= pred8x8l_128_dc_c;
  2864. h->pred8x8[DC_PRED8x8 ]= pred8x8_dc_c;
  2865. h->pred8x8[VERT_PRED8x8 ]= pred8x8_vertical_c;
  2866. h->pred8x8[HOR_PRED8x8 ]= pred8x8_horizontal_c;
  2867. h->pred8x8[PLANE_PRED8x8 ]= pred8x8_plane_c;
  2868. h->pred8x8[LEFT_DC_PRED8x8]= pred8x8_left_dc_c;
  2869. h->pred8x8[TOP_DC_PRED8x8 ]= pred8x8_top_dc_c;
  2870. h->pred8x8[DC_128_PRED8x8 ]= pred8x8_128_dc_c;
  2871. h->pred16x16[DC_PRED8x8 ]= pred16x16_dc_c;
  2872. h->pred16x16[VERT_PRED8x8 ]= pred16x16_vertical_c;
  2873. h->pred16x16[HOR_PRED8x8 ]= pred16x16_horizontal_c;
  2874. h->pred16x16[PLANE_PRED8x8 ]= pred16x16_plane_c;
  2875. h->pred16x16[LEFT_DC_PRED8x8]= pred16x16_left_dc_c;
  2876. h->pred16x16[TOP_DC_PRED8x8 ]= pred16x16_top_dc_c;
  2877. h->pred16x16[DC_128_PRED8x8 ]= pred16x16_128_dc_c;
  2878. }
  2879. static void free_tables(H264Context *h){
  2880. av_freep(&h->intra4x4_pred_mode);
  2881. av_freep(&h->chroma_pred_mode_table);
  2882. av_freep(&h->cbp_table);
  2883. av_freep(&h->mvd_table[0]);
  2884. av_freep(&h->mvd_table[1]);
  2885. av_freep(&h->direct_table);
  2886. av_freep(&h->non_zero_count);
  2887. av_freep(&h->slice_table_base);
  2888. av_freep(&h->top_borders[1]);
  2889. av_freep(&h->top_borders[0]);
  2890. h->slice_table= NULL;
  2891. av_freep(&h->mb2b_xy);
  2892. av_freep(&h->mb2b8_xy);
  2893. av_freep(&h->s.obmc_scratchpad);
  2894. }
  2895. static void init_dequant8_coeff_table(H264Context *h){
  2896. int i,q,x;
  2897. const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
  2898. h->dequant8_coeff[0] = h->dequant8_buffer[0];
  2899. h->dequant8_coeff[1] = h->dequant8_buffer[1];
  2900. for(i=0; i<2; i++ ){
  2901. if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
  2902. h->dequant8_coeff[1] = h->dequant8_buffer[0];
  2903. break;
  2904. }
  2905. for(q=0; q<52; q++){
  2906. int shift = div6[q];
  2907. int idx = rem6[q];
  2908. for(x=0; x<64; x++)
  2909. h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
  2910. ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
  2911. h->pps.scaling_matrix8[i][x]) << shift;
  2912. }
  2913. }
  2914. }
  2915. static void init_dequant4_coeff_table(H264Context *h){
  2916. int i,j,q,x;
  2917. const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
  2918. for(i=0; i<6; i++ ){
  2919. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  2920. for(j=0; j<i; j++){
  2921. if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
  2922. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  2923. break;
  2924. }
  2925. }
  2926. if(j<i)
  2927. continue;
  2928. for(q=0; q<52; q++){
  2929. int shift = div6[q] + 2;
  2930. int idx = rem6[q];
  2931. for(x=0; x<16; x++)
  2932. h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
  2933. ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
  2934. h->pps.scaling_matrix4[i][x]) << shift;
  2935. }
  2936. }
  2937. }
  2938. static void init_dequant_tables(H264Context *h){
  2939. int i,x;
  2940. init_dequant4_coeff_table(h);
  2941. if(h->pps.transform_8x8_mode)
  2942. init_dequant8_coeff_table(h);
  2943. if(h->sps.transform_bypass){
  2944. for(i=0; i<6; i++)
  2945. for(x=0; x<16; x++)
  2946. h->dequant4_coeff[i][0][x] = 1<<6;
  2947. if(h->pps.transform_8x8_mode)
  2948. for(i=0; i<2; i++)
  2949. for(x=0; x<64; x++)
  2950. h->dequant8_coeff[i][0][x] = 1<<6;
  2951. }
  2952. }
  2953. /**
  2954. * allocates tables.
  2955. * needs width/height
  2956. */
  2957. static int alloc_tables(H264Context *h){
  2958. MpegEncContext * const s = &h->s;
  2959. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  2960. int x,y;
  2961. CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
  2962. CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
  2963. CHECKED_ALLOCZ(h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(uint8_t))
  2964. CHECKED_ALLOCZ(h->top_borders[0] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2965. CHECKED_ALLOCZ(h->top_borders[1] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2966. CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
  2967. if( h->pps.cabac ) {
  2968. CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
  2969. CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
  2970. CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t));
  2971. CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t));
  2972. }
  2973. memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(uint8_t));
  2974. h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
  2975. CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint32_t));
  2976. CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint32_t));
  2977. for(y=0; y<s->mb_height; y++){
  2978. for(x=0; x<s->mb_width; x++){
  2979. const int mb_xy= x + y*s->mb_stride;
  2980. const int b_xy = 4*x + 4*y*h->b_stride;
  2981. const int b8_xy= 2*x + 2*y*h->b8_stride;
  2982. h->mb2b_xy [mb_xy]= b_xy;
  2983. h->mb2b8_xy[mb_xy]= b8_xy;
  2984. }
  2985. }
  2986. s->obmc_scratchpad = NULL;
  2987. if(!h->dequant4_coeff[0])
  2988. init_dequant_tables(h);
  2989. return 0;
  2990. fail:
  2991. free_tables(h);
  2992. return -1;
  2993. }
  2994. static void common_init(H264Context *h){
  2995. MpegEncContext * const s = &h->s;
  2996. s->width = s->avctx->width;
  2997. s->height = s->avctx->height;
  2998. s->codec_id= s->avctx->codec->id;
  2999. init_pred_ptrs(h);
  3000. h->dequant_coeff_pps= -1;
  3001. s->unrestricted_mv=1;
  3002. s->decode=1; //FIXME
  3003. memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  3004. memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  3005. }
  3006. static int decode_init(AVCodecContext *avctx){
  3007. H264Context *h= avctx->priv_data;
  3008. MpegEncContext * const s = &h->s;
  3009. MPV_decode_defaults(s);
  3010. s->avctx = avctx;
  3011. common_init(h);
  3012. s->out_format = FMT_H264;
  3013. s->workaround_bugs= avctx->workaround_bugs;
  3014. // set defaults
  3015. // s->decode_mb= ff_h263_decode_mb;
  3016. s->low_delay= 1;
  3017. avctx->pix_fmt= PIX_FMT_YUV420P;
  3018. decode_init_vlc(h);
  3019. if(avctx->extradata_size > 0 && avctx->extradata &&
  3020. *(char *)avctx->extradata == 1){
  3021. h->is_avc = 1;
  3022. h->got_avcC = 0;
  3023. } else {
  3024. h->is_avc = 0;
  3025. }
  3026. return 0;
  3027. }
  3028. static int frame_start(H264Context *h){
  3029. MpegEncContext * const s = &h->s;
  3030. int i;
  3031. if(MPV_frame_start(s, s->avctx) < 0)
  3032. return -1;
  3033. ff_er_frame_start(s);
  3034. assert(s->linesize && s->uvlinesize);
  3035. for(i=0; i<16; i++){
  3036. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  3037. h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  3038. }
  3039. for(i=0; i<4; i++){
  3040. h->block_offset[16+i]=
  3041. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  3042. h->block_offset[24+16+i]=
  3043. h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  3044. }
  3045. /* can't be in alloc_tables because linesize isn't known there.
  3046. * FIXME: redo bipred weight to not require extra buffer? */
  3047. if(!s->obmc_scratchpad)
  3048. s->obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  3049. /* some macroblocks will be accessed before they're available */
  3050. if(FRAME_MBAFF)
  3051. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(uint8_t));
  3052. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  3053. return 0;
  3054. }
  3055. static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  3056. MpegEncContext * const s = &h->s;
  3057. int i;
  3058. src_y -= linesize;
  3059. src_cb -= uvlinesize;
  3060. src_cr -= uvlinesize;
  3061. // There are two lines saved, the line above the the top macroblock of a pair,
  3062. // and the line above the bottom macroblock
  3063. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  3064. for(i=1; i<17; i++){
  3065. h->left_border[i]= src_y[15+i* linesize];
  3066. }
  3067. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
  3068. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
  3069. if(!(s->flags&CODEC_FLAG_GRAY)){
  3070. h->left_border[17 ]= h->top_borders[0][s->mb_x][16+7];
  3071. h->left_border[17+9]= h->top_borders[0][s->mb_x][24+7];
  3072. for(i=1; i<9; i++){
  3073. h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
  3074. h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
  3075. }
  3076. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
  3077. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
  3078. }
  3079. }
  3080. 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){
  3081. MpegEncContext * const s = &h->s;
  3082. int temp8, i;
  3083. uint64_t temp64;
  3084. int deblock_left = (s->mb_x > 0);
  3085. int deblock_top = (s->mb_y > 0);
  3086. src_y -= linesize + 1;
  3087. src_cb -= uvlinesize + 1;
  3088. src_cr -= uvlinesize + 1;
  3089. #define XCHG(a,b,t,xchg)\
  3090. t= a;\
  3091. if(xchg)\
  3092. a= b;\
  3093. b= t;
  3094. if(deblock_left){
  3095. for(i = !deblock_top; i<17; i++){
  3096. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  3097. }
  3098. }
  3099. if(deblock_top){
  3100. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  3101. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  3102. if(s->mb_x+1 < s->mb_width){
  3103. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  3104. }
  3105. }
  3106. if(!(s->flags&CODEC_FLAG_GRAY)){
  3107. if(deblock_left){
  3108. for(i = !deblock_top; i<9; i++){
  3109. XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
  3110. XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
  3111. }
  3112. }
  3113. if(deblock_top){
  3114. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  3115. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  3116. }
  3117. }
  3118. }
  3119. static inline void backup_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  3120. MpegEncContext * const s = &h->s;
  3121. int i;
  3122. src_y -= 2 * linesize;
  3123. src_cb -= 2 * uvlinesize;
  3124. src_cr -= 2 * uvlinesize;
  3125. // There are two lines saved, the line above the the top macroblock of a pair,
  3126. // and the line above the bottom macroblock
  3127. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  3128. h->left_border[1]= h->top_borders[1][s->mb_x][15];
  3129. for(i=2; i<34; i++){
  3130. h->left_border[i]= src_y[15+i* linesize];
  3131. }
  3132. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 32*linesize);
  3133. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+32*linesize);
  3134. *(uint64_t*)(h->top_borders[1][s->mb_x]+0)= *(uint64_t*)(src_y + 33*linesize);
  3135. *(uint64_t*)(h->top_borders[1][s->mb_x]+8)= *(uint64_t*)(src_y +8+33*linesize);
  3136. if(!(s->flags&CODEC_FLAG_GRAY)){
  3137. h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7];
  3138. h->left_border[34+ 1]= h->top_borders[1][s->mb_x][16+7];
  3139. h->left_border[34+18 ]= h->top_borders[0][s->mb_x][24+7];
  3140. h->left_border[34+18+1]= h->top_borders[1][s->mb_x][24+7];
  3141. for(i=2; i<18; i++){
  3142. h->left_border[i+34 ]= src_cb[7+i*uvlinesize];
  3143. h->left_border[i+34+18]= src_cr[7+i*uvlinesize];
  3144. }
  3145. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+16*uvlinesize);
  3146. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+16*uvlinesize);
  3147. *(uint64_t*)(h->top_borders[1][s->mb_x]+16)= *(uint64_t*)(src_cb+17*uvlinesize);
  3148. *(uint64_t*)(h->top_borders[1][s->mb_x]+24)= *(uint64_t*)(src_cr+17*uvlinesize);
  3149. }
  3150. }
  3151. 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){
  3152. MpegEncContext * const s = &h->s;
  3153. int temp8, i;
  3154. uint64_t temp64;
  3155. int deblock_left = (s->mb_x > 0);
  3156. int deblock_top = (s->mb_y > 1);
  3157. 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);
  3158. src_y -= 2 * linesize + 1;
  3159. src_cb -= 2 * uvlinesize + 1;
  3160. src_cr -= 2 * uvlinesize + 1;
  3161. #define XCHG(a,b,t,xchg)\
  3162. t= a;\
  3163. if(xchg)\
  3164. a= b;\
  3165. b= t;
  3166. if(deblock_left){
  3167. for(i = (!deblock_top)<<1; i<34; i++){
  3168. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  3169. }
  3170. }
  3171. if(deblock_top){
  3172. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  3173. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  3174. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+0), *(uint64_t*)(src_y +1 +linesize), temp64, xchg);
  3175. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+8), *(uint64_t*)(src_y +9 +linesize), temp64, 1);
  3176. if(s->mb_x+1 < s->mb_width){
  3177. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  3178. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x+1]), *(uint64_t*)(src_y +17 +linesize), temp64, 1);
  3179. }
  3180. }
  3181. if(!(s->flags&CODEC_FLAG_GRAY)){
  3182. if(deblock_left){
  3183. for(i = (!deblock_top) << 1; i<18; i++){
  3184. XCHG(h->left_border[i+34 ], src_cb[i*uvlinesize], temp8, xchg);
  3185. XCHG(h->left_border[i+34+18], src_cr[i*uvlinesize], temp8, xchg);
  3186. }
  3187. }
  3188. if(deblock_top){
  3189. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  3190. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  3191. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+16), *(uint64_t*)(src_cb+1 +uvlinesize), temp64, 1);
  3192. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+24), *(uint64_t*)(src_cr+1 +uvlinesize), temp64, 1);
  3193. }
  3194. }
  3195. }
  3196. static void hl_decode_mb(H264Context *h){
  3197. MpegEncContext * const s = &h->s;
  3198. const int mb_x= s->mb_x;
  3199. const int mb_y= s->mb_y;
  3200. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3201. const int mb_type= s->current_picture.mb_type[mb_xy];
  3202. uint8_t *dest_y, *dest_cb, *dest_cr;
  3203. int linesize, uvlinesize /*dct_offset*/;
  3204. int i;
  3205. int *block_offset = &h->block_offset[0];
  3206. const unsigned int bottom = mb_y & 1;
  3207. const int transform_bypass = (s->qscale == 0 && h->sps.transform_bypass);
  3208. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  3209. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  3210. if(!s->decode)
  3211. return;
  3212. dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3213. dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3214. dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3215. if (MB_FIELD) {
  3216. linesize = h->mb_linesize = s->linesize * 2;
  3217. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  3218. block_offset = &h->block_offset[24];
  3219. if(mb_y&1){ //FIXME move out of this func?
  3220. dest_y -= s->linesize*15;
  3221. dest_cb-= s->uvlinesize*7;
  3222. dest_cr-= s->uvlinesize*7;
  3223. }
  3224. if(FRAME_MBAFF) {
  3225. int list;
  3226. for(list=0; list<2; list++){
  3227. if(!USES_LIST(mb_type, list))
  3228. continue;
  3229. if(IS_16X16(mb_type)){
  3230. int8_t *ref = &h->ref_cache[list][scan8[0]];
  3231. fill_rectangle(ref, 4, 4, 8, 16+*ref^(s->mb_y&1), 1);
  3232. }else{
  3233. for(i=0; i<16; i+=4){
  3234. //FIXME can refs be smaller than 8x8 when !direct_8x8_inference ?
  3235. int ref = h->ref_cache[list][scan8[i]];
  3236. if(ref >= 0)
  3237. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, 16+ref^(s->mb_y&1), 1);
  3238. }
  3239. }
  3240. }
  3241. }
  3242. } else {
  3243. linesize = h->mb_linesize = s->linesize;
  3244. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  3245. // dct_offset = s->linesize * 16;
  3246. }
  3247. if(transform_bypass){
  3248. idct_dc_add =
  3249. idct_add = IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  3250. }else if(IS_8x8DCT(mb_type)){
  3251. idct_dc_add = s->dsp.h264_idct8_dc_add;
  3252. idct_add = s->dsp.h264_idct8_add;
  3253. }else{
  3254. idct_dc_add = s->dsp.h264_idct_dc_add;
  3255. idct_add = s->dsp.h264_idct_add;
  3256. }
  3257. if(FRAME_MBAFF && h->deblocking_filter && IS_INTRA(mb_type)
  3258. && (!bottom || !IS_INTRA(s->current_picture.mb_type[mb_xy-s->mb_stride]))){
  3259. int mbt_y = mb_y&~1;
  3260. uint8_t *top_y = s->current_picture.data[0] + (mbt_y * 16* s->linesize ) + mb_x * 16;
  3261. uint8_t *top_cb = s->current_picture.data[1] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  3262. uint8_t *top_cr = s->current_picture.data[2] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  3263. xchg_pair_border(h, top_y, top_cb, top_cr, s->linesize, s->uvlinesize, 1);
  3264. }
  3265. if (IS_INTRA_PCM(mb_type)) {
  3266. unsigned int x, y;
  3267. // The pixels are stored in h->mb array in the same order as levels,
  3268. // copy them in output in the correct order.
  3269. for(i=0; i<16; i++) {
  3270. for (y=0; y<4; y++) {
  3271. for (x=0; x<4; x++) {
  3272. *(dest_y + block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x];
  3273. }
  3274. }
  3275. }
  3276. for(i=16; i<16+4; i++) {
  3277. for (y=0; y<4; y++) {
  3278. for (x=0; x<4; x++) {
  3279. *(dest_cb + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  3280. }
  3281. }
  3282. }
  3283. for(i=20; i<20+4; i++) {
  3284. for (y=0; y<4; y++) {
  3285. for (x=0; x<4; x++) {
  3286. *(dest_cr + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  3287. }
  3288. }
  3289. }
  3290. } else {
  3291. if(IS_INTRA(mb_type)){
  3292. if(h->deblocking_filter && !FRAME_MBAFF)
  3293. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1);
  3294. if(!(s->flags&CODEC_FLAG_GRAY)){
  3295. h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  3296. h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  3297. }
  3298. if(IS_INTRA4x4(mb_type)){
  3299. if(!s->encoding){
  3300. if(IS_8x8DCT(mb_type)){
  3301. for(i=0; i<16; i+=4){
  3302. uint8_t * const ptr= dest_y + block_offset[i];
  3303. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  3304. const int nnz = h->non_zero_count_cache[ scan8[i] ];
  3305. h->pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  3306. (h->topright_samples_available<<(i+1))&0x8000, linesize);
  3307. if(nnz){
  3308. if(nnz == 1 && h->mb[i*16])
  3309. idct_dc_add(ptr, h->mb + i*16, linesize);
  3310. else
  3311. idct_add(ptr, h->mb + i*16, linesize);
  3312. }
  3313. }
  3314. }else
  3315. for(i=0; i<16; i++){
  3316. uint8_t * const ptr= dest_y + block_offset[i];
  3317. uint8_t *topright;
  3318. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  3319. int nnz, tr;
  3320. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  3321. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  3322. assert(mb_y || linesize <= block_offset[i]);
  3323. if(!topright_avail){
  3324. tr= ptr[3 - linesize]*0x01010101;
  3325. topright= (uint8_t*) &tr;
  3326. }else
  3327. topright= ptr + 4 - linesize;
  3328. }else
  3329. topright= NULL;
  3330. h->pred4x4[ dir ](ptr, topright, linesize);
  3331. nnz = h->non_zero_count_cache[ scan8[i] ];
  3332. if(nnz){
  3333. if(s->codec_id == CODEC_ID_H264){
  3334. if(nnz == 1 && h->mb[i*16])
  3335. idct_dc_add(ptr, h->mb + i*16, linesize);
  3336. else
  3337. idct_add(ptr, h->mb + i*16, linesize);
  3338. }else
  3339. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  3340. }
  3341. }
  3342. }
  3343. }else{
  3344. h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  3345. if(s->codec_id == CODEC_ID_H264){
  3346. if(!transform_bypass)
  3347. h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[IS_INTRA(mb_type) ? 0:3][s->qscale][0]);
  3348. }else
  3349. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  3350. }
  3351. if(h->deblocking_filter && !FRAME_MBAFF)
  3352. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
  3353. }else if(s->codec_id == CODEC_ID_H264){
  3354. hl_motion(h, dest_y, dest_cb, dest_cr,
  3355. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  3356. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  3357. s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
  3358. }
  3359. if(!IS_INTRA4x4(mb_type)){
  3360. if(s->codec_id == CODEC_ID_H264){
  3361. if(IS_INTRA16x16(mb_type)){
  3362. for(i=0; i<16; i++){
  3363. if(h->non_zero_count_cache[ scan8[i] ])
  3364. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3365. else if(h->mb[i*16])
  3366. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3367. }
  3368. }else{
  3369. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  3370. for(i=0; i<16; i+=di){
  3371. int nnz = h->non_zero_count_cache[ scan8[i] ];
  3372. if(nnz){
  3373. if(nnz==1 && h->mb[i*16])
  3374. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3375. else
  3376. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3377. }
  3378. }
  3379. }
  3380. }else{
  3381. for(i=0; i<16; i++){
  3382. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  3383. uint8_t * const ptr= dest_y + block_offset[i];
  3384. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  3385. }
  3386. }
  3387. }
  3388. }
  3389. if(!(s->flags&CODEC_FLAG_GRAY)){
  3390. uint8_t *dest[2] = {dest_cb, dest_cr};
  3391. if(transform_bypass){
  3392. idct_add = idct_dc_add = s->dsp.add_pixels4;
  3393. }else{
  3394. idct_add = s->dsp.h264_idct_add;
  3395. idct_dc_add = s->dsp.h264_idct_dc_add;
  3396. chroma_dc_dequant_idct_c(h->mb + 16*16, h->chroma_qp, h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp][0]);
  3397. chroma_dc_dequant_idct_c(h->mb + 16*16+4*16, h->chroma_qp, h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp][0]);
  3398. }
  3399. if(s->codec_id == CODEC_ID_H264){
  3400. for(i=16; i<16+8; i++){
  3401. if(h->non_zero_count_cache[ scan8[i] ])
  3402. idct_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3403. else if(h->mb[i*16])
  3404. idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3405. }
  3406. }else{
  3407. for(i=16; i<16+8; i++){
  3408. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  3409. uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
  3410. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  3411. }
  3412. }
  3413. }
  3414. }
  3415. }
  3416. if(h->deblocking_filter) {
  3417. if (FRAME_MBAFF) {
  3418. //FIXME try deblocking one mb at a time?
  3419. // the reduction in load/storing mvs and such might outweigh the extra backup/xchg_border
  3420. const int mb_y = s->mb_y - 1;
  3421. uint8_t *pair_dest_y, *pair_dest_cb, *pair_dest_cr;
  3422. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3423. const int mb_type_top = s->current_picture.mb_type[mb_xy];
  3424. const int mb_type_bottom= s->current_picture.mb_type[mb_xy+s->mb_stride];
  3425. if (!bottom) return;
  3426. pair_dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3427. pair_dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3428. pair_dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3429. if(IS_INTRA(mb_type_top | mb_type_bottom))
  3430. xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0);
  3431. backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize);
  3432. // deblock a pair
  3433. // top
  3434. s->mb_y--;
  3435. 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);
  3436. fill_caches(h, mb_type_top, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3437. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy]);
  3438. filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize);
  3439. // bottom
  3440. s->mb_y++;
  3441. tprintf("call mbaff filter_mb\n");
  3442. fill_caches(h, mb_type_bottom, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3443. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy+s->mb_stride]);
  3444. filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3445. } else {
  3446. tprintf("call filter_mb\n");
  3447. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3448. fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3449. filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3450. }
  3451. }
  3452. }
  3453. /**
  3454. * fills the default_ref_list.
  3455. */
  3456. static int fill_default_ref_list(H264Context *h){
  3457. MpegEncContext * const s = &h->s;
  3458. int i;
  3459. int smallest_poc_greater_than_current = -1;
  3460. Picture sorted_short_ref[32];
  3461. if(h->slice_type==B_TYPE){
  3462. int out_i;
  3463. int limit= INT_MIN;
  3464. /* sort frame according to poc in B slice */
  3465. for(out_i=0; out_i<h->short_ref_count; out_i++){
  3466. int best_i=INT_MIN;
  3467. int best_poc=INT_MAX;
  3468. for(i=0; i<h->short_ref_count; i++){
  3469. const int poc= h->short_ref[i]->poc;
  3470. if(poc > limit && poc < best_poc){
  3471. best_poc= poc;
  3472. best_i= i;
  3473. }
  3474. }
  3475. assert(best_i != INT_MIN);
  3476. limit= best_poc;
  3477. sorted_short_ref[out_i]= *h->short_ref[best_i];
  3478. 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);
  3479. if (-1 == smallest_poc_greater_than_current) {
  3480. if (h->short_ref[best_i]->poc >= s->current_picture_ptr->poc) {
  3481. smallest_poc_greater_than_current = out_i;
  3482. }
  3483. }
  3484. }
  3485. }
  3486. if(s->picture_structure == PICT_FRAME){
  3487. if(h->slice_type==B_TYPE){
  3488. int list;
  3489. tprintf("current poc: %d, smallest_poc_greater_than_current: %d\n", s->current_picture_ptr->poc, smallest_poc_greater_than_current);
  3490. // find the largest poc
  3491. for(list=0; list<2; list++){
  3492. int index = 0;
  3493. int j= -99;
  3494. int step= list ? -1 : 1;
  3495. for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++, j+=step) {
  3496. while(j<0 || j>= h->short_ref_count){
  3497. if(j != -99 && step == (list ? -1 : 1))
  3498. return -1;
  3499. step = -step;
  3500. j= smallest_poc_greater_than_current + (step>>1);
  3501. }
  3502. if(sorted_short_ref[j].reference != 3) continue;
  3503. h->default_ref_list[list][index ]= sorted_short_ref[j];
  3504. h->default_ref_list[list][index++].pic_id= sorted_short_ref[j].frame_num;
  3505. }
  3506. for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){
  3507. if(h->long_ref[i] == NULL) continue;
  3508. if(h->long_ref[i]->reference != 3) continue;
  3509. h->default_ref_list[ list ][index ]= *h->long_ref[i];
  3510. h->default_ref_list[ list ][index++].pic_id= i;;
  3511. }
  3512. if(list && (smallest_poc_greater_than_current<=0 || smallest_poc_greater_than_current>=h->short_ref_count) && (1 < index)){
  3513. // swap the two first elements of L1 when
  3514. // L0 and L1 are identical
  3515. Picture temp= h->default_ref_list[1][0];
  3516. h->default_ref_list[1][0] = h->default_ref_list[1][1];
  3517. h->default_ref_list[1][1] = temp;
  3518. }
  3519. if(index < h->ref_count[ list ])
  3520. memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
  3521. }
  3522. }else{
  3523. int index=0;
  3524. for(i=0; i<h->short_ref_count; i++){
  3525. if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
  3526. h->default_ref_list[0][index ]= *h->short_ref[i];
  3527. h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
  3528. }
  3529. for(i = 0; i < 16; i++){
  3530. if(h->long_ref[i] == NULL) continue;
  3531. if(h->long_ref[i]->reference != 3) continue;
  3532. h->default_ref_list[0][index ]= *h->long_ref[i];
  3533. h->default_ref_list[0][index++].pic_id= i;;
  3534. }
  3535. if(index < h->ref_count[0])
  3536. memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
  3537. }
  3538. }else{ //FIELD
  3539. if(h->slice_type==B_TYPE){
  3540. }else{
  3541. //FIXME second field balh
  3542. }
  3543. }
  3544. #ifdef TRACE
  3545. for (i=0; i<h->ref_count[0]; i++) {
  3546. 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]);
  3547. }
  3548. if(h->slice_type==B_TYPE){
  3549. for (i=0; i<h->ref_count[1]; i++) {
  3550. 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]);
  3551. }
  3552. }
  3553. #endif
  3554. return 0;
  3555. }
  3556. static void print_short_term(H264Context *h);
  3557. static void print_long_term(H264Context *h);
  3558. static int decode_ref_pic_list_reordering(H264Context *h){
  3559. MpegEncContext * const s = &h->s;
  3560. int list, index;
  3561. print_short_term(h);
  3562. print_long_term(h);
  3563. if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move before func
  3564. for(list=0; list<2; list++){
  3565. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  3566. if(get_bits1(&s->gb)){
  3567. int pred= h->curr_pic_num;
  3568. for(index=0; ; index++){
  3569. int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
  3570. int pic_id;
  3571. int i;
  3572. Picture *ref = NULL;
  3573. if(reordering_of_pic_nums_idc==3)
  3574. break;
  3575. if(index >= h->ref_count[list]){
  3576. av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
  3577. return -1;
  3578. }
  3579. if(reordering_of_pic_nums_idc<3){
  3580. if(reordering_of_pic_nums_idc<2){
  3581. const int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  3582. if(abs_diff_pic_num >= h->max_pic_num){
  3583. av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  3584. return -1;
  3585. }
  3586. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  3587. else pred+= abs_diff_pic_num;
  3588. pred &= h->max_pic_num - 1;
  3589. for(i= h->short_ref_count-1; i>=0; i--){
  3590. ref = h->short_ref[i];
  3591. assert(ref->reference == 3);
  3592. assert(!ref->long_ref);
  3593. if(ref->data[0] != NULL && ref->frame_num == pred && ref->long_ref == 0) // ignore non existing pictures by testing data[0] pointer
  3594. break;
  3595. }
  3596. if(i>=0)
  3597. ref->pic_id= ref->frame_num;
  3598. }else{
  3599. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  3600. ref = h->long_ref[pic_id];
  3601. ref->pic_id= pic_id;
  3602. assert(ref->reference == 3);
  3603. assert(ref->long_ref);
  3604. i=0;
  3605. }
  3606. if (i < 0) {
  3607. av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  3608. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  3609. } else {
  3610. for(i=index; i+1<h->ref_count[list]; i++){
  3611. if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
  3612. break;
  3613. }
  3614. for(; i > index; i--){
  3615. h->ref_list[list][i]= h->ref_list[list][i-1];
  3616. }
  3617. h->ref_list[list][index]= *ref;
  3618. }
  3619. }else{
  3620. av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  3621. return -1;
  3622. }
  3623. }
  3624. }
  3625. if(h->slice_type!=B_TYPE) break;
  3626. }
  3627. for(list=0; list<2; list++){
  3628. for(index= 0; index < h->ref_count[list]; index++){
  3629. if(!h->ref_list[list][index].data[0])
  3630. h->ref_list[list][index]= s->current_picture;
  3631. }
  3632. if(h->slice_type!=B_TYPE) break;
  3633. }
  3634. if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)
  3635. direct_dist_scale_factor(h);
  3636. direct_ref_list_init(h);
  3637. return 0;
  3638. }
  3639. static void fill_mbaff_ref_list(H264Context *h){
  3640. int list, i, j;
  3641. for(list=0; list<2; list++){
  3642. for(i=0; i<h->ref_count[list]; i++){
  3643. Picture *frame = &h->ref_list[list][i];
  3644. Picture *field = &h->ref_list[list][16+2*i];
  3645. field[0] = *frame;
  3646. for(j=0; j<3; j++)
  3647. field[0].linesize[j] <<= 1;
  3648. field[1] = field[0];
  3649. for(j=0; j<3; j++)
  3650. field[1].data[j] += frame->linesize[j];
  3651. h->luma_weight[list][16+2*i] = h->luma_weight[list][16+2*i+1] = h->luma_weight[list][i];
  3652. h->luma_offset[list][16+2*i] = h->luma_offset[list][16+2*i+1] = h->luma_offset[list][i];
  3653. for(j=0; j<2; j++){
  3654. h->chroma_weight[list][16+2*i][j] = h->chroma_weight[list][16+2*i+1][j] = h->chroma_weight[list][i][j];
  3655. h->chroma_offset[list][16+2*i][j] = h->chroma_offset[list][16+2*i+1][j] = h->chroma_offset[list][i][j];
  3656. }
  3657. }
  3658. }
  3659. for(j=0; j<h->ref_count[1]; j++){
  3660. for(i=0; i<h->ref_count[0]; i++)
  3661. h->implicit_weight[j][16+2*i] = h->implicit_weight[j][16+2*i+1] = h->implicit_weight[j][i];
  3662. memcpy(h->implicit_weight[16+2*j], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3663. memcpy(h->implicit_weight[16+2*j+1], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3664. }
  3665. }
  3666. static int pred_weight_table(H264Context *h){
  3667. MpegEncContext * const s = &h->s;
  3668. int list, i;
  3669. int luma_def, chroma_def;
  3670. h->use_weight= 0;
  3671. h->use_weight_chroma= 0;
  3672. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  3673. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  3674. luma_def = 1<<h->luma_log2_weight_denom;
  3675. chroma_def = 1<<h->chroma_log2_weight_denom;
  3676. for(list=0; list<2; list++){
  3677. for(i=0; i<h->ref_count[list]; i++){
  3678. int luma_weight_flag, chroma_weight_flag;
  3679. luma_weight_flag= get_bits1(&s->gb);
  3680. if(luma_weight_flag){
  3681. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  3682. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  3683. if( h->luma_weight[list][i] != luma_def
  3684. || h->luma_offset[list][i] != 0)
  3685. h->use_weight= 1;
  3686. }else{
  3687. h->luma_weight[list][i]= luma_def;
  3688. h->luma_offset[list][i]= 0;
  3689. }
  3690. chroma_weight_flag= get_bits1(&s->gb);
  3691. if(chroma_weight_flag){
  3692. int j;
  3693. for(j=0; j<2; j++){
  3694. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  3695. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  3696. if( h->chroma_weight[list][i][j] != chroma_def
  3697. || h->chroma_offset[list][i][j] != 0)
  3698. h->use_weight_chroma= 1;
  3699. }
  3700. }else{
  3701. int j;
  3702. for(j=0; j<2; j++){
  3703. h->chroma_weight[list][i][j]= chroma_def;
  3704. h->chroma_offset[list][i][j]= 0;
  3705. }
  3706. }
  3707. }
  3708. if(h->slice_type != B_TYPE) break;
  3709. }
  3710. h->use_weight= h->use_weight || h->use_weight_chroma;
  3711. return 0;
  3712. }
  3713. static void implicit_weight_table(H264Context *h){
  3714. MpegEncContext * const s = &h->s;
  3715. int ref0, ref1;
  3716. int cur_poc = s->current_picture_ptr->poc;
  3717. if( h->ref_count[0] == 1 && h->ref_count[1] == 1
  3718. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  3719. h->use_weight= 0;
  3720. h->use_weight_chroma= 0;
  3721. return;
  3722. }
  3723. h->use_weight= 2;
  3724. h->use_weight_chroma= 2;
  3725. h->luma_log2_weight_denom= 5;
  3726. h->chroma_log2_weight_denom= 5;
  3727. for(ref0=0; ref0 < h->ref_count[0]; ref0++){
  3728. int poc0 = h->ref_list[0][ref0].poc;
  3729. for(ref1=0; ref1 < h->ref_count[1]; ref1++){
  3730. int poc1 = h->ref_list[1][ref1].poc;
  3731. int td = clip(poc1 - poc0, -128, 127);
  3732. if(td){
  3733. int tb = clip(cur_poc - poc0, -128, 127);
  3734. int tx = (16384 + (ABS(td) >> 1)) / td;
  3735. int dist_scale_factor = clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
  3736. if(dist_scale_factor < -64 || dist_scale_factor > 128)
  3737. h->implicit_weight[ref0][ref1] = 32;
  3738. else
  3739. h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
  3740. }else
  3741. h->implicit_weight[ref0][ref1] = 32;
  3742. }
  3743. }
  3744. }
  3745. static inline void unreference_pic(H264Context *h, Picture *pic){
  3746. int i;
  3747. pic->reference=0;
  3748. if(pic == h->delayed_output_pic)
  3749. pic->reference=1;
  3750. else{
  3751. for(i = 0; h->delayed_pic[i]; i++)
  3752. if(pic == h->delayed_pic[i]){
  3753. pic->reference=1;
  3754. break;
  3755. }
  3756. }
  3757. }
  3758. /**
  3759. * instantaneous decoder refresh.
  3760. */
  3761. static void idr(H264Context *h){
  3762. int i;
  3763. for(i=0; i<16; i++){
  3764. if (h->long_ref[i] != NULL) {
  3765. unreference_pic(h, h->long_ref[i]);
  3766. h->long_ref[i]= NULL;
  3767. }
  3768. }
  3769. h->long_ref_count=0;
  3770. for(i=0; i<h->short_ref_count; i++){
  3771. unreference_pic(h, h->short_ref[i]);
  3772. h->short_ref[i]= NULL;
  3773. }
  3774. h->short_ref_count=0;
  3775. }
  3776. /* forget old pics after a seek */
  3777. static void flush_dpb(AVCodecContext *avctx){
  3778. H264Context *h= avctx->priv_data;
  3779. int i;
  3780. for(i=0; i<16; i++) {
  3781. if(h->delayed_pic[i])
  3782. h->delayed_pic[i]->reference= 0;
  3783. h->delayed_pic[i]= NULL;
  3784. }
  3785. if(h->delayed_output_pic)
  3786. h->delayed_output_pic->reference= 0;
  3787. h->delayed_output_pic= NULL;
  3788. idr(h);
  3789. if(h->s.current_picture_ptr)
  3790. h->s.current_picture_ptr->reference= 0;
  3791. }
  3792. /**
  3793. *
  3794. * @return the removed picture or NULL if an error occurs
  3795. */
  3796. static Picture * remove_short(H264Context *h, int frame_num){
  3797. MpegEncContext * const s = &h->s;
  3798. int i;
  3799. if(s->avctx->debug&FF_DEBUG_MMCO)
  3800. av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  3801. for(i=0; i<h->short_ref_count; i++){
  3802. Picture *pic= h->short_ref[i];
  3803. if(s->avctx->debug&FF_DEBUG_MMCO)
  3804. av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  3805. if(pic->frame_num == frame_num){
  3806. h->short_ref[i]= NULL;
  3807. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
  3808. h->short_ref_count--;
  3809. return pic;
  3810. }
  3811. }
  3812. return NULL;
  3813. }
  3814. /**
  3815. *
  3816. * @return the removed picture or NULL if an error occurs
  3817. */
  3818. static Picture * remove_long(H264Context *h, int i){
  3819. Picture *pic;
  3820. pic= h->long_ref[i];
  3821. h->long_ref[i]= NULL;
  3822. if(pic) h->long_ref_count--;
  3823. return pic;
  3824. }
  3825. /**
  3826. * print short term list
  3827. */
  3828. static void print_short_term(H264Context *h) {
  3829. uint32_t i;
  3830. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3831. av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
  3832. for(i=0; i<h->short_ref_count; i++){
  3833. Picture *pic= h->short_ref[i];
  3834. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3835. }
  3836. }
  3837. }
  3838. /**
  3839. * print long term list
  3840. */
  3841. static void print_long_term(H264Context *h) {
  3842. uint32_t i;
  3843. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3844. av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
  3845. for(i = 0; i < 16; i++){
  3846. Picture *pic= h->long_ref[i];
  3847. if (pic) {
  3848. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3849. }
  3850. }
  3851. }
  3852. }
  3853. /**
  3854. * Executes the reference picture marking (memory management control operations).
  3855. */
  3856. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  3857. MpegEncContext * const s = &h->s;
  3858. int i, j;
  3859. int current_is_long=0;
  3860. Picture *pic;
  3861. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  3862. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  3863. for(i=0; i<mmco_count; i++){
  3864. if(s->avctx->debug&FF_DEBUG_MMCO)
  3865. 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);
  3866. switch(mmco[i].opcode){
  3867. case MMCO_SHORT2UNUSED:
  3868. pic= remove_short(h, mmco[i].short_frame_num);
  3869. if(pic)
  3870. unreference_pic(h, pic);
  3871. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3872. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_short() failure\n");
  3873. break;
  3874. case MMCO_SHORT2LONG:
  3875. pic= remove_long(h, mmco[i].long_index);
  3876. if(pic) unreference_pic(h, pic);
  3877. h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
  3878. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3879. h->long_ref_count++;
  3880. break;
  3881. case MMCO_LONG2UNUSED:
  3882. pic= remove_long(h, mmco[i].long_index);
  3883. if(pic)
  3884. unreference_pic(h, pic);
  3885. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3886. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_long() failure\n");
  3887. break;
  3888. case MMCO_LONG:
  3889. pic= remove_long(h, mmco[i].long_index);
  3890. if(pic) unreference_pic(h, pic);
  3891. h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
  3892. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3893. h->long_ref_count++;
  3894. current_is_long=1;
  3895. break;
  3896. case MMCO_SET_MAX_LONG:
  3897. assert(mmco[i].long_index <= 16);
  3898. // just remove the long term which index is greater than new max
  3899. for(j = mmco[i].long_index; j<16; j++){
  3900. pic = remove_long(h, j);
  3901. if (pic) unreference_pic(h, pic);
  3902. }
  3903. break;
  3904. case MMCO_RESET:
  3905. while(h->short_ref_count){
  3906. pic= remove_short(h, h->short_ref[0]->frame_num);
  3907. unreference_pic(h, pic);
  3908. }
  3909. for(j = 0; j < 16; j++) {
  3910. pic= remove_long(h, j);
  3911. if(pic) unreference_pic(h, pic);
  3912. }
  3913. break;
  3914. default: assert(0);
  3915. }
  3916. }
  3917. if(!current_is_long){
  3918. pic= remove_short(h, s->current_picture_ptr->frame_num);
  3919. if(pic){
  3920. unreference_pic(h, pic);
  3921. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  3922. }
  3923. if(h->short_ref_count)
  3924. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  3925. h->short_ref[0]= s->current_picture_ptr;
  3926. h->short_ref[0]->long_ref=0;
  3927. h->short_ref_count++;
  3928. }
  3929. print_short_term(h);
  3930. print_long_term(h);
  3931. return 0;
  3932. }
  3933. static int decode_ref_pic_marking(H264Context *h){
  3934. MpegEncContext * const s = &h->s;
  3935. int i;
  3936. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  3937. s->broken_link= get_bits1(&s->gb) -1;
  3938. h->mmco[0].long_index= get_bits1(&s->gb) - 1; // current_long_term_idx
  3939. if(h->mmco[0].long_index == -1)
  3940. h->mmco_index= 0;
  3941. else{
  3942. h->mmco[0].opcode= MMCO_LONG;
  3943. h->mmco_index= 1;
  3944. }
  3945. }else{
  3946. if(get_bits1(&s->gb)){ // adaptive_ref_pic_marking_mode_flag
  3947. for(i= 0; i<MAX_MMCO_COUNT; i++) {
  3948. MMCOOpcode opcode= get_ue_golomb(&s->gb);;
  3949. h->mmco[i].opcode= opcode;
  3950. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  3951. 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
  3952. /* if(h->mmco[i].short_frame_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_frame_num ] == NULL){
  3953. av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
  3954. return -1;
  3955. }*/
  3956. }
  3957. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  3958. h->mmco[i].long_index= get_ue_golomb(&s->gb);
  3959. 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){
  3960. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  3961. return -1;
  3962. }
  3963. }
  3964. if(opcode > MMCO_LONG){
  3965. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  3966. return -1;
  3967. }
  3968. if(opcode == MMCO_END)
  3969. break;
  3970. }
  3971. h->mmco_index= i;
  3972. }else{
  3973. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  3974. if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){ //FIXME fields
  3975. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  3976. h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  3977. h->mmco_index= 1;
  3978. }else
  3979. h->mmco_index= 0;
  3980. }
  3981. }
  3982. return 0;
  3983. }
  3984. static int init_poc(H264Context *h){
  3985. MpegEncContext * const s = &h->s;
  3986. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  3987. int field_poc[2];
  3988. if(h->nal_unit_type == NAL_IDR_SLICE){
  3989. h->frame_num_offset= 0;
  3990. }else{
  3991. if(h->frame_num < h->prev_frame_num)
  3992. h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
  3993. else
  3994. h->frame_num_offset= h->prev_frame_num_offset;
  3995. }
  3996. if(h->sps.poc_type==0){
  3997. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  3998. if(h->nal_unit_type == NAL_IDR_SLICE){
  3999. h->prev_poc_msb=
  4000. h->prev_poc_lsb= 0;
  4001. }
  4002. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  4003. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  4004. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  4005. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  4006. else
  4007. h->poc_msb = h->prev_poc_msb;
  4008. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  4009. field_poc[0] =
  4010. field_poc[1] = h->poc_msb + h->poc_lsb;
  4011. if(s->picture_structure == PICT_FRAME)
  4012. field_poc[1] += h->delta_poc_bottom;
  4013. }else if(h->sps.poc_type==1){
  4014. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  4015. int i;
  4016. if(h->sps.poc_cycle_length != 0)
  4017. abs_frame_num = h->frame_num_offset + h->frame_num;
  4018. else
  4019. abs_frame_num = 0;
  4020. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  4021. abs_frame_num--;
  4022. expected_delta_per_poc_cycle = 0;
  4023. for(i=0; i < h->sps.poc_cycle_length; i++)
  4024. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  4025. if(abs_frame_num > 0){
  4026. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  4027. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  4028. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  4029. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  4030. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  4031. } else
  4032. expectedpoc = 0;
  4033. if(h->nal_ref_idc == 0)
  4034. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  4035. field_poc[0] = expectedpoc + h->delta_poc[0];
  4036. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  4037. if(s->picture_structure == PICT_FRAME)
  4038. field_poc[1] += h->delta_poc[1];
  4039. }else{
  4040. int poc;
  4041. if(h->nal_unit_type == NAL_IDR_SLICE){
  4042. poc= 0;
  4043. }else{
  4044. if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
  4045. else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
  4046. }
  4047. field_poc[0]= poc;
  4048. field_poc[1]= poc;
  4049. }
  4050. if(s->picture_structure != PICT_BOTTOM_FIELD)
  4051. s->current_picture_ptr->field_poc[0]= field_poc[0];
  4052. if(s->picture_structure != PICT_TOP_FIELD)
  4053. s->current_picture_ptr->field_poc[1]= field_poc[1];
  4054. if(s->picture_structure == PICT_FRAME) // FIXME field pix?
  4055. s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]);
  4056. return 0;
  4057. }
  4058. /**
  4059. * decodes a slice header.
  4060. * this will allso call MPV_common_init() and frame_start() as needed
  4061. */
  4062. static int decode_slice_header(H264Context *h){
  4063. MpegEncContext * const s = &h->s;
  4064. int first_mb_in_slice, pps_id;
  4065. int num_ref_idx_active_override_flag;
  4066. static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
  4067. int slice_type;
  4068. int default_ref_list_done = 0;
  4069. s->current_picture.reference= h->nal_ref_idc != 0;
  4070. s->dropable= h->nal_ref_idc == 0;
  4071. first_mb_in_slice= get_ue_golomb(&s->gb);
  4072. slice_type= get_ue_golomb(&s->gb);
  4073. if(slice_type > 9){
  4074. 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);
  4075. return -1;
  4076. }
  4077. if(slice_type > 4){
  4078. slice_type -= 5;
  4079. h->slice_type_fixed=1;
  4080. }else
  4081. h->slice_type_fixed=0;
  4082. slice_type= slice_type_map[ slice_type ];
  4083. if (slice_type == I_TYPE
  4084. || (h->slice_num != 0 && slice_type == h->slice_type) ) {
  4085. default_ref_list_done = 1;
  4086. }
  4087. h->slice_type= slice_type;
  4088. s->pict_type= h->slice_type; // to make a few old func happy, it's wrong though
  4089. pps_id= get_ue_golomb(&s->gb);
  4090. if(pps_id>255){
  4091. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  4092. return -1;
  4093. }
  4094. h->pps= h->pps_buffer[pps_id];
  4095. if(h->pps.slice_group_count == 0){
  4096. av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referenced\n");
  4097. return -1;
  4098. }
  4099. h->sps= h->sps_buffer[ h->pps.sps_id ];
  4100. if(h->sps.log2_max_frame_num == 0){
  4101. av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referenced\n");
  4102. return -1;
  4103. }
  4104. if(h->dequant_coeff_pps != pps_id){
  4105. h->dequant_coeff_pps = pps_id;
  4106. init_dequant_tables(h);
  4107. }
  4108. s->mb_width= h->sps.mb_width;
  4109. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  4110. h->b_stride= s->mb_width*4;
  4111. h->b8_stride= s->mb_width*2;
  4112. s->width = 16*s->mb_width - 2*(h->sps.crop_left + h->sps.crop_right );
  4113. if(h->sps.frame_mbs_only_flag)
  4114. s->height= 16*s->mb_height - 2*(h->sps.crop_top + h->sps.crop_bottom);
  4115. else
  4116. s->height= 16*s->mb_height - 4*(h->sps.crop_top + h->sps.crop_bottom); //FIXME recheck
  4117. if (s->context_initialized
  4118. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  4119. free_tables(h);
  4120. MPV_common_end(s);
  4121. }
  4122. if (!s->context_initialized) {
  4123. if (MPV_common_init(s) < 0)
  4124. return -1;
  4125. if(s->dsp.h264_idct_add == ff_h264_idct_add_c){ //FIXME little ugly
  4126. memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
  4127. memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
  4128. }else{
  4129. int i;
  4130. for(i=0; i<16; i++){
  4131. #define T(x) (x>>2) | ((x<<2) & 0xF)
  4132. h->zigzag_scan[i] = T(zigzag_scan[i]);
  4133. h-> field_scan[i] = T( field_scan[i]);
  4134. #undef T
  4135. }
  4136. }
  4137. if(s->dsp.h264_idct8_add == ff_h264_idct8_add_c){
  4138. memcpy(h->zigzag_scan8x8, zigzag_scan8x8, 64*sizeof(uint8_t));
  4139. memcpy(h->zigzag_scan8x8_cavlc, zigzag_scan8x8_cavlc, 64*sizeof(uint8_t));
  4140. memcpy(h->field_scan8x8, field_scan8x8, 64*sizeof(uint8_t));
  4141. memcpy(h->field_scan8x8_cavlc, field_scan8x8_cavlc, 64*sizeof(uint8_t));
  4142. }else{
  4143. int i;
  4144. for(i=0; i<64; i++){
  4145. #define T(x) (x>>3) | ((x&7)<<3)
  4146. h->zigzag_scan8x8[i] = T(zigzag_scan8x8[i]);
  4147. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  4148. h->field_scan8x8[i] = T(field_scan8x8[i]);
  4149. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  4150. #undef T
  4151. }
  4152. }
  4153. if(h->sps.transform_bypass){ //FIXME same ugly
  4154. h->zigzag_scan_q0 = zigzag_scan;
  4155. h->zigzag_scan8x8_q0 = zigzag_scan8x8;
  4156. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  4157. h->field_scan_q0 = field_scan;
  4158. h->field_scan8x8_q0 = field_scan8x8;
  4159. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  4160. }else{
  4161. h->zigzag_scan_q0 = h->zigzag_scan;
  4162. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  4163. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  4164. h->field_scan_q0 = h->field_scan;
  4165. h->field_scan8x8_q0 = h->field_scan8x8;
  4166. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  4167. }
  4168. alloc_tables(h);
  4169. s->avctx->width = s->width;
  4170. s->avctx->height = s->height;
  4171. s->avctx->sample_aspect_ratio= h->sps.sar;
  4172. if(!s->avctx->sample_aspect_ratio.den)
  4173. s->avctx->sample_aspect_ratio.den = 1;
  4174. if(h->sps.timing_info_present_flag){
  4175. s->avctx->time_base= (AVRational){h->sps.num_units_in_tick * 2, h->sps.time_scale};
  4176. if(h->x264_build > 0 && h->x264_build < 44)
  4177. s->avctx->time_base.den *= 2;
  4178. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  4179. s->avctx->time_base.num, s->avctx->time_base.den, 1<<30);
  4180. }
  4181. }
  4182. if(h->slice_num == 0){
  4183. if(frame_start(h) < 0)
  4184. return -1;
  4185. }
  4186. s->current_picture_ptr->frame_num= //FIXME frame_num cleanup
  4187. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  4188. h->mb_mbaff = 0;
  4189. h->mb_aff_frame = 0;
  4190. if(h->sps.frame_mbs_only_flag){
  4191. s->picture_structure= PICT_FRAME;
  4192. }else{
  4193. if(get_bits1(&s->gb)) { //field_pic_flag
  4194. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  4195. av_log(h->s.avctx, AV_LOG_ERROR, "PAFF interlacing is not implemented\n");
  4196. } else {
  4197. s->picture_structure= PICT_FRAME;
  4198. h->mb_aff_frame = h->sps.mb_aff;
  4199. }
  4200. }
  4201. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  4202. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << h->mb_aff_frame;
  4203. if(s->mb_y >= s->mb_height){
  4204. return -1;
  4205. }
  4206. if(s->picture_structure==PICT_FRAME){
  4207. h->curr_pic_num= h->frame_num;
  4208. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  4209. }else{
  4210. h->curr_pic_num= 2*h->frame_num;
  4211. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  4212. }
  4213. if(h->nal_unit_type == NAL_IDR_SLICE){
  4214. get_ue_golomb(&s->gb); /* idr_pic_id */
  4215. }
  4216. if(h->sps.poc_type==0){
  4217. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  4218. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  4219. h->delta_poc_bottom= get_se_golomb(&s->gb);
  4220. }
  4221. }
  4222. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  4223. h->delta_poc[0]= get_se_golomb(&s->gb);
  4224. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  4225. h->delta_poc[1]= get_se_golomb(&s->gb);
  4226. }
  4227. init_poc(h);
  4228. if(h->pps.redundant_pic_cnt_present){
  4229. h->redundant_pic_count= get_ue_golomb(&s->gb);
  4230. }
  4231. //set defaults, might be overriden a few line later
  4232. h->ref_count[0]= h->pps.ref_count[0];
  4233. h->ref_count[1]= h->pps.ref_count[1];
  4234. if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){
  4235. if(h->slice_type == B_TYPE){
  4236. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  4237. if(h->sps.mb_aff && h->direct_spatial_mv_pred)
  4238. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + spatial direct mode is not implemented\n");
  4239. }
  4240. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  4241. if(num_ref_idx_active_override_flag){
  4242. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  4243. if(h->slice_type==B_TYPE)
  4244. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  4245. if(h->ref_count[0] > 32 || h->ref_count[1] > 32){
  4246. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  4247. return -1;
  4248. }
  4249. }
  4250. }
  4251. if(!default_ref_list_done){
  4252. fill_default_ref_list(h);
  4253. }
  4254. if(decode_ref_pic_list_reordering(h) < 0)
  4255. return -1;
  4256. if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
  4257. || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
  4258. pred_weight_table(h);
  4259. else if(h->pps.weighted_bipred_idc==2 && h->slice_type==B_TYPE)
  4260. implicit_weight_table(h);
  4261. else
  4262. h->use_weight = 0;
  4263. if(s->current_picture.reference)
  4264. decode_ref_pic_marking(h);
  4265. if(FRAME_MBAFF)
  4266. fill_mbaff_ref_list(h);
  4267. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac )
  4268. h->cabac_init_idc = get_ue_golomb(&s->gb);
  4269. h->last_qscale_diff = 0;
  4270. s->qscale = h->pps.init_qp + get_se_golomb(&s->gb);
  4271. if(s->qscale<0 || s->qscale>51){
  4272. av_log(s->avctx, AV_LOG_ERROR, "QP %d out of range\n", s->qscale);
  4273. return -1;
  4274. }
  4275. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4276. //FIXME qscale / qp ... stuff
  4277. if(h->slice_type == SP_TYPE){
  4278. get_bits1(&s->gb); /* sp_for_switch_flag */
  4279. }
  4280. if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
  4281. get_se_golomb(&s->gb); /* slice_qs_delta */
  4282. }
  4283. h->deblocking_filter = 1;
  4284. h->slice_alpha_c0_offset = 0;
  4285. h->slice_beta_offset = 0;
  4286. if( h->pps.deblocking_filter_parameters_present ) {
  4287. h->deblocking_filter= get_ue_golomb(&s->gb);
  4288. if(h->deblocking_filter < 2)
  4289. h->deblocking_filter^= 1; // 1<->0
  4290. if( h->deblocking_filter ) {
  4291. h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
  4292. h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
  4293. }
  4294. }
  4295. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  4296. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type != I_TYPE)
  4297. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type == B_TYPE)
  4298. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  4299. h->deblocking_filter= 0;
  4300. #if 0 //FMO
  4301. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  4302. slice_group_change_cycle= get_bits(&s->gb, ?);
  4303. #endif
  4304. h->slice_num++;
  4305. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
  4306. h->emu_edge_height= FRAME_MBAFF ? 0 : h->emu_edge_width;
  4307. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  4308. 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",
  4309. h->slice_num,
  4310. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  4311. first_mb_in_slice,
  4312. av_get_pict_type_char(h->slice_type),
  4313. pps_id, h->frame_num,
  4314. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  4315. h->ref_count[0], h->ref_count[1],
  4316. s->qscale,
  4317. h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2,
  4318. h->use_weight,
  4319. h->use_weight==1 && h->use_weight_chroma ? "c" : ""
  4320. );
  4321. }
  4322. if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !s->current_picture.reference){
  4323. s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
  4324. s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
  4325. }else{
  4326. s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
  4327. s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
  4328. }
  4329. return 0;
  4330. }
  4331. /**
  4332. *
  4333. */
  4334. static inline int get_level_prefix(GetBitContext *gb){
  4335. unsigned int buf;
  4336. int log;
  4337. OPEN_READER(re, gb);
  4338. UPDATE_CACHE(re, gb);
  4339. buf=GET_CACHE(re, gb);
  4340. log= 32 - av_log2(buf);
  4341. #ifdef TRACE
  4342. print_bin(buf>>(32-log), log);
  4343. 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__);
  4344. #endif
  4345. LAST_SKIP_BITS(re, gb, log);
  4346. CLOSE_READER(re, gb);
  4347. return log-1;
  4348. }
  4349. static inline int get_dct8x8_allowed(H264Context *h){
  4350. int i;
  4351. for(i=0; i<4; i++){
  4352. if(!IS_SUB_8X8(h->sub_mb_type[i])
  4353. || (!h->sps.direct_8x8_inference_flag && IS_DIRECT(h->sub_mb_type[i])))
  4354. return 0;
  4355. }
  4356. return 1;
  4357. }
  4358. /**
  4359. * decodes a residual block.
  4360. * @param n block index
  4361. * @param scantable scantable
  4362. * @param max_coeff number of coefficients in the block
  4363. * @return <0 if an error occured
  4364. */
  4365. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
  4366. MpegEncContext * const s = &h->s;
  4367. 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};
  4368. int level[16];
  4369. int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
  4370. //FIXME put trailing_onex into the context
  4371. if(n == CHROMA_DC_BLOCK_INDEX){
  4372. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  4373. total_coeff= coeff_token>>2;
  4374. }else{
  4375. if(n == LUMA_DC_BLOCK_INDEX){
  4376. total_coeff= pred_non_zero_count(h, 0);
  4377. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4378. total_coeff= coeff_token>>2;
  4379. }else{
  4380. total_coeff= pred_non_zero_count(h, n);
  4381. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4382. total_coeff= coeff_token>>2;
  4383. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  4384. }
  4385. }
  4386. //FIXME set last_non_zero?
  4387. if(total_coeff==0)
  4388. return 0;
  4389. trailing_ones= coeff_token&3;
  4390. tprintf("trailing:%d, total:%d\n", trailing_ones, total_coeff);
  4391. assert(total_coeff<=16);
  4392. for(i=0; i<trailing_ones; i++){
  4393. level[i]= 1 - 2*get_bits1(gb);
  4394. }
  4395. if(i<total_coeff) {
  4396. int level_code, mask;
  4397. int suffix_length = total_coeff > 10 && trailing_ones < 3;
  4398. int prefix= get_level_prefix(gb);
  4399. //first coefficient has suffix_length equal to 0 or 1
  4400. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  4401. if(suffix_length)
  4402. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4403. else
  4404. level_code= (prefix<<suffix_length); //part
  4405. }else if(prefix==14){
  4406. if(suffix_length)
  4407. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4408. else
  4409. level_code= prefix + get_bits(gb, 4); //part
  4410. }else if(prefix==15){
  4411. level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part
  4412. if(suffix_length==0) level_code+=15; //FIXME doesn't make (much)sense
  4413. }else{
  4414. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4415. return -1;
  4416. }
  4417. if(trailing_ones < 3) level_code += 2;
  4418. suffix_length = 1;
  4419. if(level_code > 5)
  4420. suffix_length++;
  4421. mask= -(level_code&1);
  4422. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4423. i++;
  4424. //remaining coefficients have suffix_length > 0
  4425. for(;i<total_coeff;i++) {
  4426. static const int suffix_limit[7] = {0,5,11,23,47,95,INT_MAX };
  4427. prefix = get_level_prefix(gb);
  4428. if(prefix<15){
  4429. level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
  4430. }else if(prefix==15){
  4431. level_code = (prefix<<suffix_length) + get_bits(gb, 12);
  4432. }else{
  4433. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4434. return -1;
  4435. }
  4436. mask= -(level_code&1);
  4437. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4438. if(level_code > suffix_limit[suffix_length])
  4439. suffix_length++;
  4440. }
  4441. }
  4442. if(total_coeff == max_coeff)
  4443. zeros_left=0;
  4444. else{
  4445. if(n == CHROMA_DC_BLOCK_INDEX)
  4446. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  4447. else
  4448. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  4449. }
  4450. coeff_num = zeros_left + total_coeff - 1;
  4451. j = scantable[coeff_num];
  4452. if(n > 24){
  4453. block[j] = level[0];
  4454. for(i=1;i<total_coeff;i++) {
  4455. if(zeros_left <= 0)
  4456. run_before = 0;
  4457. else if(zeros_left < 7){
  4458. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4459. }else{
  4460. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4461. }
  4462. zeros_left -= run_before;
  4463. coeff_num -= 1 + run_before;
  4464. j= scantable[ coeff_num ];
  4465. block[j]= level[i];
  4466. }
  4467. }else{
  4468. block[j] = (level[0] * qmul[j] + 32)>>6;
  4469. for(i=1;i<total_coeff;i++) {
  4470. if(zeros_left <= 0)
  4471. run_before = 0;
  4472. else if(zeros_left < 7){
  4473. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4474. }else{
  4475. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4476. }
  4477. zeros_left -= run_before;
  4478. coeff_num -= 1 + run_before;
  4479. j= scantable[ coeff_num ];
  4480. block[j]= (level[i] * qmul[j] + 32)>>6;
  4481. }
  4482. }
  4483. if(zeros_left<0){
  4484. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  4485. return -1;
  4486. }
  4487. return 0;
  4488. }
  4489. static void predict_field_decoding_flag(H264Context *h){
  4490. MpegEncContext * const s = &h->s;
  4491. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4492. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  4493. ? s->current_picture.mb_type[mb_xy-1]
  4494. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  4495. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  4496. : 0;
  4497. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  4498. }
  4499. /**
  4500. * decodes a P_SKIP or B_SKIP macroblock
  4501. */
  4502. static void decode_mb_skip(H264Context *h){
  4503. MpegEncContext * const s = &h->s;
  4504. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4505. int mb_type=0;
  4506. memset(h->non_zero_count[mb_xy], 0, 16);
  4507. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  4508. if(MB_FIELD)
  4509. mb_type|= MB_TYPE_INTERLACED;
  4510. if( h->slice_type == B_TYPE )
  4511. {
  4512. // just for fill_caches. pred_direct_motion will set the real mb_type
  4513. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
  4514. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4515. pred_direct_motion(h, &mb_type);
  4516. mb_type|= MB_TYPE_SKIP;
  4517. }
  4518. else
  4519. {
  4520. int mx, my;
  4521. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
  4522. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4523. pred_pskip_motion(h, &mx, &my);
  4524. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  4525. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  4526. }
  4527. write_back_motion(h, mb_type);
  4528. s->current_picture.mb_type[mb_xy]= mb_type;
  4529. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4530. h->slice_table[ mb_xy ]= h->slice_num;
  4531. h->prev_mb_skipped= 1;
  4532. }
  4533. /**
  4534. * decodes a macroblock
  4535. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  4536. */
  4537. static int decode_mb_cavlc(H264Context *h){
  4538. MpegEncContext * const s = &h->s;
  4539. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4540. int mb_type, partition_count, cbp;
  4541. int dct8x8_allowed= h->pps.transform_8x8_mode;
  4542. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?
  4543. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  4544. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  4545. down the code */
  4546. if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){
  4547. if(s->mb_skip_run==-1)
  4548. s->mb_skip_run= get_ue_golomb(&s->gb);
  4549. if (s->mb_skip_run--) {
  4550. if(FRAME_MBAFF && (s->mb_y&1) == 0){
  4551. if(s->mb_skip_run==0)
  4552. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4553. else
  4554. predict_field_decoding_flag(h);
  4555. }
  4556. decode_mb_skip(h);
  4557. return 0;
  4558. }
  4559. }
  4560. if(FRAME_MBAFF){
  4561. if( (s->mb_y&1) == 0 )
  4562. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4563. }else
  4564. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  4565. h->prev_mb_skipped= 0;
  4566. mb_type= get_ue_golomb(&s->gb);
  4567. if(h->slice_type == B_TYPE){
  4568. if(mb_type < 23){
  4569. partition_count= b_mb_type_info[mb_type].partition_count;
  4570. mb_type= b_mb_type_info[mb_type].type;
  4571. }else{
  4572. mb_type -= 23;
  4573. goto decode_intra_mb;
  4574. }
  4575. }else if(h->slice_type == P_TYPE /*|| h->slice_type == SP_TYPE */){
  4576. if(mb_type < 5){
  4577. partition_count= p_mb_type_info[mb_type].partition_count;
  4578. mb_type= p_mb_type_info[mb_type].type;
  4579. }else{
  4580. mb_type -= 5;
  4581. goto decode_intra_mb;
  4582. }
  4583. }else{
  4584. assert(h->slice_type == I_TYPE);
  4585. decode_intra_mb:
  4586. if(mb_type > 25){
  4587. 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);
  4588. return -1;
  4589. }
  4590. partition_count=0;
  4591. cbp= i_mb_type_info[mb_type].cbp;
  4592. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  4593. mb_type= i_mb_type_info[mb_type].type;
  4594. }
  4595. if(MB_FIELD)
  4596. mb_type |= MB_TYPE_INTERLACED;
  4597. h->slice_table[ mb_xy ]= h->slice_num;
  4598. if(IS_INTRA_PCM(mb_type)){
  4599. unsigned int x, y;
  4600. // we assume these blocks are very rare so we dont optimize it
  4601. align_get_bits(&s->gb);
  4602. // The pixels are stored in the same order as levels in h->mb array.
  4603. for(y=0; y<16; y++){
  4604. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  4605. for(x=0; x<16; x++){
  4606. tprintf("LUMA ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4607. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= get_bits(&s->gb, 8);
  4608. }
  4609. }
  4610. for(y=0; y<8; y++){
  4611. const int index= 256 + 4*(y&3) + 32*(y>>2);
  4612. for(x=0; x<8; x++){
  4613. tprintf("CHROMA U ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4614. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4615. }
  4616. }
  4617. for(y=0; y<8; y++){
  4618. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  4619. for(x=0; x<8; x++){
  4620. tprintf("CHROMA V ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4621. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4622. }
  4623. }
  4624. // In deblocking, the quantizer is 0
  4625. s->current_picture.qscale_table[mb_xy]= 0;
  4626. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  4627. // All coeffs are present
  4628. memset(h->non_zero_count[mb_xy], 16, 16);
  4629. s->current_picture.mb_type[mb_xy]= mb_type;
  4630. return 0;
  4631. }
  4632. if(MB_MBAFF){
  4633. h->ref_count[0] <<= 1;
  4634. h->ref_count[1] <<= 1;
  4635. }
  4636. fill_caches(h, mb_type, 0);
  4637. //mb_pred
  4638. if(IS_INTRA(mb_type)){
  4639. // init_top_left_availability(h);
  4640. if(IS_INTRA4x4(mb_type)){
  4641. int i;
  4642. int di = 1;
  4643. if(dct8x8_allowed && get_bits1(&s->gb)){
  4644. mb_type |= MB_TYPE_8x8DCT;
  4645. di = 4;
  4646. }
  4647. // fill_intra4x4_pred_table(h);
  4648. for(i=0; i<16; i+=di){
  4649. int mode= pred_intra_mode(h, i);
  4650. if(!get_bits1(&s->gb)){
  4651. const int rem_mode= get_bits(&s->gb, 3);
  4652. mode = rem_mode + (rem_mode >= mode);
  4653. }
  4654. if(di==4)
  4655. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  4656. else
  4657. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  4658. }
  4659. write_back_intra_pred_mode(h);
  4660. if( check_intra4x4_pred_mode(h) < 0)
  4661. return -1;
  4662. }else{
  4663. h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
  4664. if(h->intra16x16_pred_mode < 0)
  4665. return -1;
  4666. }
  4667. h->chroma_pred_mode= get_ue_golomb(&s->gb);
  4668. h->chroma_pred_mode= check_intra_pred_mode(h, h->chroma_pred_mode);
  4669. if(h->chroma_pred_mode < 0)
  4670. return -1;
  4671. }else if(partition_count==4){
  4672. int i, j, sub_partition_count[4], list, ref[2][4];
  4673. if(h->slice_type == B_TYPE){
  4674. for(i=0; i<4; i++){
  4675. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4676. if(h->sub_mb_type[i] >=13){
  4677. 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);
  4678. return -1;
  4679. }
  4680. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4681. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4682. }
  4683. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  4684. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
  4685. pred_direct_motion(h, &mb_type);
  4686. h->ref_cache[0][scan8[4]] =
  4687. h->ref_cache[1][scan8[4]] =
  4688. h->ref_cache[0][scan8[12]] =
  4689. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  4690. }
  4691. }else{
  4692. assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE); //FIXME SP correct ?
  4693. for(i=0; i<4; i++){
  4694. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4695. if(h->sub_mb_type[i] >=4){
  4696. 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);
  4697. return -1;
  4698. }
  4699. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4700. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4701. }
  4702. }
  4703. for(list=0; list<2; list++){
  4704. int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4705. if(ref_count == 0) continue;
  4706. for(i=0; i<4; i++){
  4707. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  4708. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4709. ref[list][i] = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
  4710. }else{
  4711. //FIXME
  4712. ref[list][i] = -1;
  4713. }
  4714. }
  4715. }
  4716. if(dct8x8_allowed)
  4717. dct8x8_allowed = get_dct8x8_allowed(h);
  4718. for(list=0; list<2; list++){
  4719. const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4720. if(ref_count == 0) continue;
  4721. for(i=0; i<4; i++){
  4722. if(IS_DIRECT(h->sub_mb_type[i])) {
  4723. h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];
  4724. continue;
  4725. }
  4726. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  4727. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  4728. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4729. const int sub_mb_type= h->sub_mb_type[i];
  4730. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  4731. for(j=0; j<sub_partition_count[i]; j++){
  4732. int mx, my;
  4733. const int index= 4*i + block_width*j;
  4734. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  4735. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  4736. mx += get_se_golomb(&s->gb);
  4737. my += get_se_golomb(&s->gb);
  4738. tprintf("final mv:%d %d\n", mx, my);
  4739. if(IS_SUB_8X8(sub_mb_type)){
  4740. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  4741. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  4742. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  4743. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  4744. }else if(IS_SUB_8X4(sub_mb_type)){
  4745. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  4746. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  4747. }else if(IS_SUB_4X8(sub_mb_type)){
  4748. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  4749. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  4750. }else{
  4751. assert(IS_SUB_4X4(sub_mb_type));
  4752. mv_cache[ 0 ][0]= mx;
  4753. mv_cache[ 0 ][1]= my;
  4754. }
  4755. }
  4756. }else{
  4757. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  4758. p[0] = p[1]=
  4759. p[8] = p[9]= 0;
  4760. }
  4761. }
  4762. }
  4763. }else if(IS_DIRECT(mb_type)){
  4764. pred_direct_motion(h, &mb_type);
  4765. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  4766. }else{
  4767. int list, mx, my, i;
  4768. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  4769. if(IS_16X16(mb_type)){
  4770. for(list=0; list<2; list++){
  4771. if(h->ref_count[list]>0){
  4772. if(IS_DIR(mb_type, 0, list)){
  4773. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4774. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  4775. }else
  4776. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4777. }
  4778. }
  4779. for(list=0; list<2; list++){
  4780. if(IS_DIR(mb_type, 0, list)){
  4781. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  4782. mx += get_se_golomb(&s->gb);
  4783. my += get_se_golomb(&s->gb);
  4784. tprintf("final mv:%d %d\n", mx, my);
  4785. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  4786. }else
  4787. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  4788. }
  4789. }
  4790. else if(IS_16X8(mb_type)){
  4791. for(list=0; list<2; list++){
  4792. if(h->ref_count[list]>0){
  4793. for(i=0; i<2; i++){
  4794. if(IS_DIR(mb_type, i, list)){
  4795. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4796. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  4797. }else
  4798. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  4799. }
  4800. }
  4801. }
  4802. for(list=0; list<2; list++){
  4803. for(i=0; i<2; i++){
  4804. if(IS_DIR(mb_type, i, list)){
  4805. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  4806. mx += get_se_golomb(&s->gb);
  4807. my += get_se_golomb(&s->gb);
  4808. tprintf("final mv:%d %d\n", mx, my);
  4809. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  4810. }else
  4811. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  4812. }
  4813. }
  4814. }else{
  4815. assert(IS_8X16(mb_type));
  4816. for(list=0; list<2; list++){
  4817. if(h->ref_count[list]>0){
  4818. for(i=0; i<2; i++){
  4819. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  4820. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4821. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  4822. }else
  4823. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4824. }
  4825. }
  4826. }
  4827. for(list=0; list<2; list++){
  4828. for(i=0; i<2; i++){
  4829. if(IS_DIR(mb_type, i, list)){
  4830. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  4831. mx += get_se_golomb(&s->gb);
  4832. my += get_se_golomb(&s->gb);
  4833. tprintf("final mv:%d %d\n", mx, my);
  4834. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  4835. }else
  4836. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  4837. }
  4838. }
  4839. }
  4840. }
  4841. if(IS_INTER(mb_type))
  4842. write_back_motion(h, mb_type);
  4843. if(!IS_INTRA16x16(mb_type)){
  4844. cbp= get_ue_golomb(&s->gb);
  4845. if(cbp > 47){
  4846. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%d) at %d %d\n", cbp, s->mb_x, s->mb_y);
  4847. return -1;
  4848. }
  4849. if(IS_INTRA4x4(mb_type))
  4850. cbp= golomb_to_intra4x4_cbp[cbp];
  4851. else
  4852. cbp= golomb_to_inter_cbp[cbp];
  4853. }
  4854. h->cbp = cbp;
  4855. if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
  4856. if(get_bits1(&s->gb))
  4857. mb_type |= MB_TYPE_8x8DCT;
  4858. }
  4859. s->current_picture.mb_type[mb_xy]= mb_type;
  4860. if(cbp || IS_INTRA16x16(mb_type)){
  4861. int i8x8, i4x4, chroma_idx;
  4862. int chroma_qp, dquant;
  4863. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  4864. const uint8_t *scan, *scan8x8, *dc_scan;
  4865. // fill_non_zero_count_cache(h);
  4866. if(IS_INTERLACED(mb_type)){
  4867. scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
  4868. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  4869. dc_scan= luma_dc_field_scan;
  4870. }else{
  4871. scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
  4872. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  4873. dc_scan= luma_dc_zigzag_scan;
  4874. }
  4875. dquant= get_se_golomb(&s->gb);
  4876. if( dquant > 25 || dquant < -26 ){
  4877. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  4878. return -1;
  4879. }
  4880. s->qscale += dquant;
  4881. if(((unsigned)s->qscale) > 51){
  4882. if(s->qscale<0) s->qscale+= 52;
  4883. else s->qscale-= 52;
  4884. }
  4885. h->chroma_qp= chroma_qp= get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4886. if(IS_INTRA16x16(mb_type)){
  4887. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){
  4888. return -1; //FIXME continue if partitioned and other return -1 too
  4889. }
  4890. assert((cbp&15) == 0 || (cbp&15) == 15);
  4891. if(cbp&15){
  4892. for(i8x8=0; i8x8<4; i8x8++){
  4893. for(i4x4=0; i4x4<4; i4x4++){
  4894. const int index= i4x4 + 4*i8x8;
  4895. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){
  4896. return -1;
  4897. }
  4898. }
  4899. }
  4900. }else{
  4901. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  4902. }
  4903. }else{
  4904. for(i8x8=0; i8x8<4; i8x8++){
  4905. if(cbp & (1<<i8x8)){
  4906. if(IS_8x8DCT(mb_type)){
  4907. DCTELEM *buf = &h->mb[64*i8x8];
  4908. uint8_t *nnz;
  4909. for(i4x4=0; i4x4<4; i4x4++){
  4910. if( decode_residual(h, gb, buf, i4x4+4*i8x8, scan8x8+16*i4x4,
  4911. h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )
  4912. return -1;
  4913. }
  4914. nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4915. nnz[0] += nnz[1] + nnz[8] + nnz[9];
  4916. }else{
  4917. for(i4x4=0; i4x4<4; i4x4++){
  4918. const int index= i4x4 + 4*i8x8;
  4919. if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){
  4920. return -1;
  4921. }
  4922. }
  4923. }
  4924. }else{
  4925. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4926. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  4927. }
  4928. }
  4929. }
  4930. if(cbp&0x30){
  4931. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  4932. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){
  4933. return -1;
  4934. }
  4935. }
  4936. if(cbp&0x20){
  4937. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  4938. for(i4x4=0; i4x4<4; i4x4++){
  4939. const int index= 16 + 4*chroma_idx + i4x4;
  4940. if( decode_residual(h, gb, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[chroma_idx+1+(IS_INTRA( mb_type ) ? 0:3)][chroma_qp], 15) < 0){
  4941. return -1;
  4942. }
  4943. }
  4944. }
  4945. }else{
  4946. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4947. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4948. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4949. }
  4950. }else{
  4951. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4952. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  4953. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4954. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4955. }
  4956. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4957. write_back_non_zero_count(h);
  4958. if(MB_MBAFF){
  4959. h->ref_count[0] >>= 1;
  4960. h->ref_count[1] >>= 1;
  4961. }
  4962. return 0;
  4963. }
  4964. static int decode_cabac_field_decoding_flag(H264Context *h) {
  4965. MpegEncContext * const s = &h->s;
  4966. const int mb_x = s->mb_x;
  4967. const int mb_y = s->mb_y & ~1;
  4968. const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
  4969. const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
  4970. unsigned int ctx = 0;
  4971. if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
  4972. ctx += 1;
  4973. }
  4974. if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
  4975. ctx += 1;
  4976. }
  4977. return get_cabac( &h->cabac, &h->cabac_state[70 + ctx] );
  4978. }
  4979. static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
  4980. uint8_t *state= &h->cabac_state[ctx_base];
  4981. int mb_type;
  4982. if(intra_slice){
  4983. MpegEncContext * const s = &h->s;
  4984. const int mba_xy = h->left_mb_xy[0];
  4985. const int mbb_xy = h->top_mb_xy;
  4986. int ctx=0;
  4987. if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
  4988. ctx++;
  4989. if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
  4990. ctx++;
  4991. if( get_cabac( &h->cabac, &state[ctx] ) == 0 )
  4992. return 0; /* I4x4 */
  4993. state += 2;
  4994. }else{
  4995. if( get_cabac( &h->cabac, &state[0] ) == 0 )
  4996. return 0; /* I4x4 */
  4997. }
  4998. if( get_cabac_terminate( &h->cabac ) )
  4999. return 25; /* PCM */
  5000. mb_type = 1; /* I16x16 */
  5001. mb_type += 12 * get_cabac( &h->cabac, &state[1] ); /* cbp_luma != 0 */
  5002. if( get_cabac( &h->cabac, &state[2] ) ) /* cbp_chroma */
  5003. mb_type += 4 + 4 * get_cabac( &h->cabac, &state[2+intra_slice] );
  5004. mb_type += 2 * get_cabac( &h->cabac, &state[3+intra_slice] );
  5005. mb_type += 1 * get_cabac( &h->cabac, &state[3+2*intra_slice] );
  5006. return mb_type;
  5007. }
  5008. static int decode_cabac_mb_type( H264Context *h ) {
  5009. MpegEncContext * const s = &h->s;
  5010. if( h->slice_type == I_TYPE ) {
  5011. return decode_cabac_intra_mb_type(h, 3, 1);
  5012. } else if( h->slice_type == P_TYPE ) {
  5013. if( get_cabac( &h->cabac, &h->cabac_state[14] ) == 0 ) {
  5014. /* P-type */
  5015. if( get_cabac( &h->cabac, &h->cabac_state[15] ) == 0 ) {
  5016. /* P_L0_D16x16, P_8x8 */
  5017. return 3 * get_cabac( &h->cabac, &h->cabac_state[16] );
  5018. } else {
  5019. /* P_L0_D8x16, P_L0_D16x8 */
  5020. return 2 - get_cabac( &h->cabac, &h->cabac_state[17] );
  5021. }
  5022. } else {
  5023. return decode_cabac_intra_mb_type(h, 17, 0) + 5;
  5024. }
  5025. } else if( h->slice_type == B_TYPE ) {
  5026. const int mba_xy = h->left_mb_xy[0];
  5027. const int mbb_xy = h->top_mb_xy;
  5028. int ctx = 0;
  5029. int bits;
  5030. if( h->slice_table[mba_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
  5031. ctx++;
  5032. if( h->slice_table[mbb_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
  5033. ctx++;
  5034. if( !get_cabac( &h->cabac, &h->cabac_state[27+ctx] ) )
  5035. return 0; /* B_Direct_16x16 */
  5036. if( !get_cabac( &h->cabac, &h->cabac_state[27+3] ) ) {
  5037. return 1 + get_cabac( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
  5038. }
  5039. bits = get_cabac( &h->cabac, &h->cabac_state[27+4] ) << 3;
  5040. bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] ) << 2;
  5041. bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] ) << 1;
  5042. bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] );
  5043. if( bits < 8 )
  5044. return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
  5045. else if( bits == 13 ) {
  5046. return decode_cabac_intra_mb_type(h, 32, 0) + 23;
  5047. } else if( bits == 14 )
  5048. return 11; /* B_L1_L0_8x16 */
  5049. else if( bits == 15 )
  5050. return 22; /* B_8x8 */
  5051. bits= ( bits<<1 ) | get_cabac( &h->cabac, &h->cabac_state[27+5] );
  5052. return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
  5053. } else {
  5054. /* TODO SI/SP frames? */
  5055. return -1;
  5056. }
  5057. }
  5058. static int decode_cabac_mb_skip( H264Context *h, int mb_x, int mb_y ) {
  5059. MpegEncContext * const s = &h->s;
  5060. int mba_xy, mbb_xy;
  5061. int ctx = 0;
  5062. if(FRAME_MBAFF){ //FIXME merge with the stuff in fill_caches?
  5063. int mb_xy = mb_x + (mb_y&~1)*s->mb_stride;
  5064. mba_xy = mb_xy - 1;
  5065. if( (mb_y&1)
  5066. && h->slice_table[mba_xy] == h->slice_num
  5067. && MB_FIELD == !!IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) )
  5068. mba_xy += s->mb_stride;
  5069. if( MB_FIELD ){
  5070. mbb_xy = mb_xy - s->mb_stride;
  5071. if( !(mb_y&1)
  5072. && h->slice_table[mbb_xy] == h->slice_num
  5073. && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) )
  5074. mbb_xy -= s->mb_stride;
  5075. }else
  5076. mbb_xy = mb_x + (mb_y-1)*s->mb_stride;
  5077. }else{
  5078. int mb_xy = mb_x + mb_y*s->mb_stride;
  5079. mba_xy = mb_xy - 1;
  5080. mbb_xy = mb_xy - s->mb_stride;
  5081. }
  5082. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
  5083. ctx++;
  5084. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
  5085. ctx++;
  5086. if( h->slice_type == B_TYPE )
  5087. ctx += 13;
  5088. return get_cabac( &h->cabac, &h->cabac_state[11+ctx] );
  5089. }
  5090. static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
  5091. int mode = 0;
  5092. if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
  5093. return pred_mode;
  5094. mode += 1 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5095. mode += 2 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5096. mode += 4 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5097. if( mode >= pred_mode )
  5098. return mode + 1;
  5099. else
  5100. return mode;
  5101. }
  5102. static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
  5103. const int mba_xy = h->left_mb_xy[0];
  5104. const int mbb_xy = h->top_mb_xy;
  5105. int ctx = 0;
  5106. /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
  5107. if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
  5108. ctx++;
  5109. if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
  5110. ctx++;
  5111. if( get_cabac( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
  5112. return 0;
  5113. if( get_cabac( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  5114. return 1;
  5115. if( get_cabac( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  5116. return 2;
  5117. else
  5118. return 3;
  5119. }
  5120. static const uint8_t block_idx_x[16] = {
  5121. 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
  5122. };
  5123. static const uint8_t block_idx_y[16] = {
  5124. 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
  5125. };
  5126. static const uint8_t block_idx_xy[4][4] = {
  5127. { 0, 2, 8, 10},
  5128. { 1, 3, 9, 11},
  5129. { 4, 6, 12, 14},
  5130. { 5, 7, 13, 15}
  5131. };
  5132. static int decode_cabac_mb_cbp_luma( H264Context *h) {
  5133. int cbp = 0;
  5134. int cbp_b = -1;
  5135. int i8x8;
  5136. if( h->slice_table[h->top_mb_xy] == h->slice_num ) {
  5137. cbp_b = h->top_cbp;
  5138. tprintf("cbp_b = top_cbp = %x\n", cbp_b);
  5139. }
  5140. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5141. int cbp_a = -1;
  5142. int x, y;
  5143. int ctx = 0;
  5144. x = block_idx_x[4*i8x8];
  5145. y = block_idx_y[4*i8x8];
  5146. if( x > 0 )
  5147. cbp_a = cbp;
  5148. else if( h->slice_table[h->left_mb_xy[0]] == h->slice_num ) {
  5149. cbp_a = h->left_cbp;
  5150. tprintf("cbp_a = left_cbp = %x\n", cbp_a);
  5151. }
  5152. if( y > 0 )
  5153. cbp_b = cbp;
  5154. /* No need to test for skip as we put 0 for skip block */
  5155. /* No need to test for IPCM as we put 1 for IPCM block */
  5156. if( cbp_a >= 0 ) {
  5157. int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
  5158. if( ((cbp_a >> i8x8a)&0x01) == 0 )
  5159. ctx++;
  5160. }
  5161. if( cbp_b >= 0 ) {
  5162. int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
  5163. if( ((cbp_b >> i8x8b)&0x01) == 0 )
  5164. ctx += 2;
  5165. }
  5166. if( get_cabac( &h->cabac, &h->cabac_state[73 + ctx] ) ) {
  5167. cbp |= 1 << i8x8;
  5168. }
  5169. }
  5170. return cbp;
  5171. }
  5172. static int decode_cabac_mb_cbp_chroma( H264Context *h) {
  5173. int ctx;
  5174. int cbp_a, cbp_b;
  5175. cbp_a = (h->left_cbp>>4)&0x03;
  5176. cbp_b = (h-> top_cbp>>4)&0x03;
  5177. ctx = 0;
  5178. if( cbp_a > 0 ) ctx++;
  5179. if( cbp_b > 0 ) ctx += 2;
  5180. if( get_cabac( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
  5181. return 0;
  5182. ctx = 4;
  5183. if( cbp_a == 2 ) ctx++;
  5184. if( cbp_b == 2 ) ctx += 2;
  5185. return 1 + get_cabac( &h->cabac, &h->cabac_state[77 + ctx] );
  5186. }
  5187. static int decode_cabac_mb_dqp( H264Context *h) {
  5188. MpegEncContext * const s = &h->s;
  5189. int mbn_xy;
  5190. int ctx = 0;
  5191. int val = 0;
  5192. if( s->mb_x > 0 )
  5193. mbn_xy = s->mb_x + s->mb_y*s->mb_stride - 1;
  5194. else
  5195. mbn_xy = s->mb_width - 1 + (s->mb_y-1)*s->mb_stride;
  5196. if( h->last_qscale_diff != 0 )
  5197. ctx++;
  5198. while( get_cabac( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
  5199. if( ctx < 2 )
  5200. ctx = 2;
  5201. else
  5202. ctx = 3;
  5203. val++;
  5204. if(val > 102) //prevent infinite loop
  5205. return INT_MIN;
  5206. }
  5207. if( val&0x01 )
  5208. return (val + 1)/2;
  5209. else
  5210. return -(val + 1)/2;
  5211. }
  5212. static int decode_cabac_p_mb_sub_type( H264Context *h ) {
  5213. if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
  5214. return 0; /* 8x8 */
  5215. if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
  5216. return 1; /* 8x4 */
  5217. if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
  5218. return 2; /* 4x8 */
  5219. return 3; /* 4x4 */
  5220. }
  5221. static int decode_cabac_b_mb_sub_type( H264Context *h ) {
  5222. int type;
  5223. if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
  5224. return 0; /* B_Direct_8x8 */
  5225. if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
  5226. return 1 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */
  5227. type = 3;
  5228. if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
  5229. if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
  5230. return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
  5231. type += 4;
  5232. }
  5233. type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
  5234. type += get_cabac( &h->cabac, &h->cabac_state[39] );
  5235. return type;
  5236. }
  5237. static inline int decode_cabac_mb_transform_size( H264Context *h ) {
  5238. return get_cabac( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
  5239. }
  5240. static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
  5241. int refa = h->ref_cache[list][scan8[n] - 1];
  5242. int refb = h->ref_cache[list][scan8[n] - 8];
  5243. int ref = 0;
  5244. int ctx = 0;
  5245. if( h->slice_type == B_TYPE) {
  5246. if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
  5247. ctx++;
  5248. if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
  5249. ctx += 2;
  5250. } else {
  5251. if( refa > 0 )
  5252. ctx++;
  5253. if( refb > 0 )
  5254. ctx += 2;
  5255. }
  5256. while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
  5257. ref++;
  5258. if( ctx < 4 )
  5259. ctx = 4;
  5260. else
  5261. ctx = 5;
  5262. }
  5263. return ref;
  5264. }
  5265. static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
  5266. int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
  5267. abs( h->mvd_cache[list][scan8[n] - 8][l] );
  5268. int ctxbase = (l == 0) ? 40 : 47;
  5269. int ctx, mvd;
  5270. if( amvd < 3 )
  5271. ctx = 0;
  5272. else if( amvd > 32 )
  5273. ctx = 2;
  5274. else
  5275. ctx = 1;
  5276. if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
  5277. return 0;
  5278. mvd= 1;
  5279. ctx= 3;
  5280. while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
  5281. mvd++;
  5282. if( ctx < 6 )
  5283. ctx++;
  5284. }
  5285. if( mvd >= 9 ) {
  5286. int k = 3;
  5287. while( get_cabac_bypass( &h->cabac ) ) {
  5288. mvd += 1 << k;
  5289. k++;
  5290. }
  5291. while( k-- ) {
  5292. if( get_cabac_bypass( &h->cabac ) )
  5293. mvd += 1 << k;
  5294. }
  5295. }
  5296. if( get_cabac_bypass( &h->cabac ) ) return -mvd;
  5297. else return mvd;
  5298. }
  5299. static int inline get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) {
  5300. int nza, nzb;
  5301. int ctx = 0;
  5302. if( cat == 0 ) {
  5303. nza = h->left_cbp&0x100;
  5304. nzb = h-> top_cbp&0x100;
  5305. } else if( cat == 1 || cat == 2 ) {
  5306. nza = h->non_zero_count_cache[scan8[idx] - 1];
  5307. nzb = h->non_zero_count_cache[scan8[idx] - 8];
  5308. } else if( cat == 3 ) {
  5309. nza = (h->left_cbp>>(6+idx))&0x01;
  5310. nzb = (h-> top_cbp>>(6+idx))&0x01;
  5311. } else {
  5312. assert(cat == 4);
  5313. nza = h->non_zero_count_cache[scan8[16+idx] - 1];
  5314. nzb = h->non_zero_count_cache[scan8[16+idx] - 8];
  5315. }
  5316. if( nza > 0 )
  5317. ctx++;
  5318. if( nzb > 0 )
  5319. ctx += 2;
  5320. return ctx + 4 * cat;
  5321. }
  5322. static int decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff) {
  5323. const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
  5324. static const int significant_coeff_flag_offset[2][6] = {
  5325. { 105+0, 105+15, 105+29, 105+44, 105+47, 402 },
  5326. { 277+0, 277+15, 277+29, 277+44, 277+47, 436 }
  5327. };
  5328. static const int last_coeff_flag_offset[2][6] = {
  5329. { 166+0, 166+15, 166+29, 166+44, 166+47, 417 },
  5330. { 338+0, 338+15, 338+29, 338+44, 338+47, 451 }
  5331. };
  5332. static const int coeff_abs_level_m1_offset[6] = {
  5333. 227+0, 227+10, 227+20, 227+30, 227+39, 426
  5334. };
  5335. static const int significant_coeff_flag_offset_8x8[2][63] = {
  5336. { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
  5337. 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
  5338. 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
  5339. 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12 },
  5340. { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
  5341. 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
  5342. 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
  5343. 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }
  5344. };
  5345. static const int last_coeff_flag_offset_8x8[63] = {
  5346. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5347. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  5348. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  5349. 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
  5350. };
  5351. int index[64];
  5352. int i, last;
  5353. int coeff_count = 0;
  5354. int abslevel1 = 1;
  5355. int abslevelgt1 = 0;
  5356. uint8_t *significant_coeff_ctx_base;
  5357. uint8_t *last_coeff_ctx_base;
  5358. uint8_t *abs_level_m1_ctx_base;
  5359. /* cat: 0-> DC 16x16 n = 0
  5360. * 1-> AC 16x16 n = luma4x4idx
  5361. * 2-> Luma4x4 n = luma4x4idx
  5362. * 3-> DC Chroma n = iCbCr
  5363. * 4-> AC Chroma n = 4 * iCbCr + chroma4x4idx
  5364. * 5-> Luma8x8 n = 4 * luma8x8idx
  5365. */
  5366. /* read coded block flag */
  5367. if( cat != 5 ) {
  5368. if( get_cabac( &h->cabac, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n ) ] ) == 0 ) {
  5369. if( cat == 1 || cat == 2 )
  5370. h->non_zero_count_cache[scan8[n]] = 0;
  5371. else if( cat == 4 )
  5372. h->non_zero_count_cache[scan8[16+n]] = 0;
  5373. return 0;
  5374. }
  5375. }
  5376. significant_coeff_ctx_base = h->cabac_state
  5377. + significant_coeff_flag_offset[MB_FIELD][cat];
  5378. last_coeff_ctx_base = h->cabac_state
  5379. + last_coeff_flag_offset[MB_FIELD][cat];
  5380. abs_level_m1_ctx_base = h->cabac_state
  5381. + coeff_abs_level_m1_offset[cat];
  5382. if( cat == 5 ) {
  5383. #define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \
  5384. for(last= 0; last < coefs; last++) { \
  5385. uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \
  5386. if( get_cabac( &h->cabac, sig_ctx )) { \
  5387. uint8_t *last_ctx = last_coeff_ctx_base + last_off; \
  5388. index[coeff_count++] = last; \
  5389. if( get_cabac( &h->cabac, last_ctx ) ) { \
  5390. last= max_coeff; \
  5391. break; \
  5392. } \
  5393. } \
  5394. }
  5395. const int *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
  5396. DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );
  5397. } else {
  5398. DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
  5399. }
  5400. if( last == max_coeff -1 ) {
  5401. index[coeff_count++] = last;
  5402. }
  5403. assert(coeff_count > 0);
  5404. if( cat == 0 )
  5405. h->cbp_table[mb_xy] |= 0x100;
  5406. else if( cat == 1 || cat == 2 )
  5407. h->non_zero_count_cache[scan8[n]] = coeff_count;
  5408. else if( cat == 3 )
  5409. h->cbp_table[mb_xy] |= 0x40 << n;
  5410. else if( cat == 4 )
  5411. h->non_zero_count_cache[scan8[16+n]] = coeff_count;
  5412. else {
  5413. assert( cat == 5 );
  5414. fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);
  5415. }
  5416. for( i = coeff_count - 1; i >= 0; i-- ) {
  5417. uint8_t *ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 )) + abs_level_m1_ctx_base;
  5418. int j= scantable[index[i]];
  5419. if( get_cabac( &h->cabac, ctx ) == 0 ) {
  5420. if( !qmul ) {
  5421. if( get_cabac_bypass( &h->cabac ) ) block[j] = -1;
  5422. else block[j] = 1;
  5423. }else{
  5424. if( get_cabac_bypass( &h->cabac ) ) block[j] = (-qmul[j] + 32) >> 6;
  5425. else block[j] = ( qmul[j] + 32) >> 6;
  5426. }
  5427. abslevel1++;
  5428. } else {
  5429. int coeff_abs = 2;
  5430. ctx = 5 + FFMIN( 4, abslevelgt1 ) + abs_level_m1_ctx_base;
  5431. while( coeff_abs < 15 && get_cabac( &h->cabac, ctx ) ) {
  5432. coeff_abs++;
  5433. }
  5434. if( coeff_abs >= 15 ) {
  5435. int j = 0;
  5436. while( get_cabac_bypass( &h->cabac ) ) {
  5437. coeff_abs += 1 << j;
  5438. j++;
  5439. }
  5440. while( j-- ) {
  5441. if( get_cabac_bypass( &h->cabac ) )
  5442. coeff_abs += 1 << j ;
  5443. }
  5444. }
  5445. if( !qmul ) {
  5446. if( get_cabac_bypass( &h->cabac ) ) block[j] = -coeff_abs;
  5447. else block[j] = coeff_abs;
  5448. }else{
  5449. if( get_cabac_bypass( &h->cabac ) ) block[j] = (-coeff_abs * qmul[j] + 32) >> 6;
  5450. else block[j] = ( coeff_abs * qmul[j] + 32) >> 6;
  5451. }
  5452. abslevelgt1++;
  5453. }
  5454. }
  5455. return 0;
  5456. }
  5457. static void inline compute_mb_neighbors(H264Context *h)
  5458. {
  5459. MpegEncContext * const s = &h->s;
  5460. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  5461. h->top_mb_xy = mb_xy - s->mb_stride;
  5462. h->left_mb_xy[0] = mb_xy - 1;
  5463. if(FRAME_MBAFF){
  5464. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  5465. const int top_pair_xy = pair_xy - s->mb_stride;
  5466. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  5467. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  5468. const int curr_mb_frame_flag = !MB_FIELD;
  5469. const int bottom = (s->mb_y & 1);
  5470. if (bottom
  5471. ? !curr_mb_frame_flag // bottom macroblock
  5472. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  5473. ) {
  5474. h->top_mb_xy -= s->mb_stride;
  5475. }
  5476. if (left_mb_frame_flag != curr_mb_frame_flag) {
  5477. h->left_mb_xy[0] = pair_xy - 1;
  5478. }
  5479. }
  5480. return;
  5481. }
  5482. /**
  5483. * decodes a macroblock
  5484. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  5485. */
  5486. static int decode_mb_cabac(H264Context *h) {
  5487. MpegEncContext * const s = &h->s;
  5488. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  5489. int mb_type, partition_count, cbp = 0;
  5490. int dct8x8_allowed= h->pps.transform_8x8_mode;
  5491. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?)
  5492. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  5493. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) {
  5494. int skip;
  5495. /* a skipped mb needs the aff flag from the following mb */
  5496. if( FRAME_MBAFF && s->mb_x==0 && (s->mb_y&1)==0 )
  5497. predict_field_decoding_flag(h);
  5498. if( FRAME_MBAFF && (s->mb_y&1)==1 && h->prev_mb_skipped )
  5499. skip = h->next_mb_skipped;
  5500. else
  5501. skip = decode_cabac_mb_skip( h, s->mb_x, s->mb_y );
  5502. /* read skip flags */
  5503. if( skip ) {
  5504. if( FRAME_MBAFF && (s->mb_y&1)==0 ){
  5505. s->current_picture.mb_type[mb_xy] = MB_TYPE_SKIP;
  5506. h->next_mb_skipped = decode_cabac_mb_skip( h, s->mb_x, s->mb_y+1 );
  5507. if(h->next_mb_skipped)
  5508. predict_field_decoding_flag(h);
  5509. else
  5510. h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5511. }
  5512. decode_mb_skip(h);
  5513. h->cbp_table[mb_xy] = 0;
  5514. h->chroma_pred_mode_table[mb_xy] = 0;
  5515. h->last_qscale_diff = 0;
  5516. return 0;
  5517. }
  5518. }
  5519. if(FRAME_MBAFF){
  5520. if( (s->mb_y&1) == 0 )
  5521. h->mb_mbaff =
  5522. h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5523. }else
  5524. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  5525. h->prev_mb_skipped = 0;
  5526. compute_mb_neighbors(h);
  5527. if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
  5528. av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
  5529. return -1;
  5530. }
  5531. if( h->slice_type == B_TYPE ) {
  5532. if( mb_type < 23 ){
  5533. partition_count= b_mb_type_info[mb_type].partition_count;
  5534. mb_type= b_mb_type_info[mb_type].type;
  5535. }else{
  5536. mb_type -= 23;
  5537. goto decode_intra_mb;
  5538. }
  5539. } else if( h->slice_type == P_TYPE ) {
  5540. if( mb_type < 5) {
  5541. partition_count= p_mb_type_info[mb_type].partition_count;
  5542. mb_type= p_mb_type_info[mb_type].type;
  5543. } else {
  5544. mb_type -= 5;
  5545. goto decode_intra_mb;
  5546. }
  5547. } else {
  5548. assert(h->slice_type == I_TYPE);
  5549. decode_intra_mb:
  5550. partition_count = 0;
  5551. cbp= i_mb_type_info[mb_type].cbp;
  5552. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  5553. mb_type= i_mb_type_info[mb_type].type;
  5554. }
  5555. if(MB_FIELD)
  5556. mb_type |= MB_TYPE_INTERLACED;
  5557. h->slice_table[ mb_xy ]= h->slice_num;
  5558. if(IS_INTRA_PCM(mb_type)) {
  5559. const uint8_t *ptr;
  5560. unsigned int x, y;
  5561. // We assume these blocks are very rare so we dont optimize it.
  5562. // FIXME The two following lines get the bitstream position in the cabac
  5563. // decode, I think it should be done by a function in cabac.h (or cabac.c).
  5564. ptr= h->cabac.bytestream;
  5565. if (h->cabac.low&0x1) ptr-=CABAC_BITS/8;
  5566. // The pixels are stored in the same order as levels in h->mb array.
  5567. for(y=0; y<16; y++){
  5568. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  5569. for(x=0; x<16; x++){
  5570. tprintf("LUMA ICPM LEVEL (%3d)\n", *ptr);
  5571. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= *ptr++;
  5572. }
  5573. }
  5574. for(y=0; y<8; y++){
  5575. const int index= 256 + 4*(y&3) + 32*(y>>2);
  5576. for(x=0; x<8; x++){
  5577. tprintf("CHROMA U ICPM LEVEL (%3d)\n", *ptr);
  5578. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5579. }
  5580. }
  5581. for(y=0; y<8; y++){
  5582. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  5583. for(x=0; x<8; x++){
  5584. tprintf("CHROMA V ICPM LEVEL (%3d)\n", *ptr);
  5585. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5586. }
  5587. }
  5588. ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);
  5589. // All blocks are present
  5590. h->cbp_table[mb_xy] = 0x1ef;
  5591. h->chroma_pred_mode_table[mb_xy] = 0;
  5592. // In deblocking, the quantizer is 0
  5593. s->current_picture.qscale_table[mb_xy]= 0;
  5594. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  5595. // All coeffs are present
  5596. memset(h->non_zero_count[mb_xy], 16, 16);
  5597. s->current_picture.mb_type[mb_xy]= mb_type;
  5598. return 0;
  5599. }
  5600. if(MB_MBAFF){
  5601. h->ref_count[0] <<= 1;
  5602. h->ref_count[1] <<= 1;
  5603. }
  5604. fill_caches(h, mb_type, 0);
  5605. if( IS_INTRA( mb_type ) ) {
  5606. int i;
  5607. if( IS_INTRA4x4( mb_type ) ) {
  5608. if( dct8x8_allowed && decode_cabac_mb_transform_size( h ) ) {
  5609. mb_type |= MB_TYPE_8x8DCT;
  5610. for( i = 0; i < 16; i+=4 ) {
  5611. int pred = pred_intra_mode( h, i );
  5612. int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5613. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  5614. }
  5615. } else {
  5616. for( i = 0; i < 16; i++ ) {
  5617. int pred = pred_intra_mode( h, i );
  5618. h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5619. //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
  5620. }
  5621. }
  5622. write_back_intra_pred_mode(h);
  5623. if( check_intra4x4_pred_mode(h) < 0 ) return -1;
  5624. } else {
  5625. h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );
  5626. if( h->intra16x16_pred_mode < 0 ) return -1;
  5627. }
  5628. h->chroma_pred_mode_table[mb_xy] =
  5629. h->chroma_pred_mode = decode_cabac_mb_chroma_pre_mode( h );
  5630. h->chroma_pred_mode= check_intra_pred_mode( h, h->chroma_pred_mode );
  5631. if( h->chroma_pred_mode < 0 ) return -1;
  5632. } else if( partition_count == 4 ) {
  5633. int i, j, sub_partition_count[4], list, ref[2][4];
  5634. if( h->slice_type == B_TYPE ) {
  5635. for( i = 0; i < 4; i++ ) {
  5636. h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
  5637. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5638. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5639. }
  5640. if( IS_DIRECT(h->sub_mb_type[0] | h->sub_mb_type[1] |
  5641. h->sub_mb_type[2] | h->sub_mb_type[3]) ) {
  5642. pred_direct_motion(h, &mb_type);
  5643. if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
  5644. for( i = 0; i < 4; i++ )
  5645. if( IS_DIRECT(h->sub_mb_type[i]) )
  5646. fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );
  5647. }
  5648. }
  5649. } else {
  5650. for( i = 0; i < 4; i++ ) {
  5651. h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
  5652. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5653. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5654. }
  5655. }
  5656. for( list = 0; list < 2; list++ ) {
  5657. if( h->ref_count[list] > 0 ) {
  5658. for( i = 0; i < 4; i++ ) {
  5659. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  5660. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  5661. if( h->ref_count[list] > 1 )
  5662. ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );
  5663. else
  5664. ref[list][i] = 0;
  5665. } else {
  5666. ref[list][i] = -1;
  5667. }
  5668. h->ref_cache[list][ scan8[4*i]+1 ]=
  5669. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  5670. }
  5671. }
  5672. }
  5673. if(dct8x8_allowed)
  5674. dct8x8_allowed = get_dct8x8_allowed(h);
  5675. for(list=0; list<2; list++){
  5676. for(i=0; i<4; i++){
  5677. if(IS_DIRECT(h->sub_mb_type[i])){
  5678. fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);
  5679. continue;
  5680. }
  5681. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];
  5682. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  5683. const int sub_mb_type= h->sub_mb_type[i];
  5684. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  5685. for(j=0; j<sub_partition_count[i]; j++){
  5686. int mpx, mpy;
  5687. int mx, my;
  5688. const int index= 4*i + block_width*j;
  5689. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  5690. int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];
  5691. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);
  5692. mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );
  5693. my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );
  5694. tprintf("final mv:%d %d\n", mx, my);
  5695. if(IS_SUB_8X8(sub_mb_type)){
  5696. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  5697. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  5698. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  5699. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  5700. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]=
  5701. mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;
  5702. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]=
  5703. mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;
  5704. }else if(IS_SUB_8X4(sub_mb_type)){
  5705. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  5706. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  5707. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]= mx- mpx;
  5708. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]= my - mpy;
  5709. }else if(IS_SUB_4X8(sub_mb_type)){
  5710. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  5711. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  5712. mvd_cache[ 0 ][0]= mvd_cache[ 8 ][0]= mx - mpx;
  5713. mvd_cache[ 0 ][1]= mvd_cache[ 8 ][1]= my - mpy;
  5714. }else{
  5715. assert(IS_SUB_4X4(sub_mb_type));
  5716. mv_cache[ 0 ][0]= mx;
  5717. mv_cache[ 0 ][1]= my;
  5718. mvd_cache[ 0 ][0]= mx - mpx;
  5719. mvd_cache[ 0 ][1]= my - mpy;
  5720. }
  5721. }
  5722. }else{
  5723. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  5724. uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];
  5725. p[0] = p[1] = p[8] = p[9] = 0;
  5726. pd[0]= pd[1]= pd[8]= pd[9]= 0;
  5727. }
  5728. }
  5729. }
  5730. } else if( IS_DIRECT(mb_type) ) {
  5731. pred_direct_motion(h, &mb_type);
  5732. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  5733. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  5734. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  5735. } else {
  5736. int list, mx, my, i, mpx, mpy;
  5737. if(IS_16X16(mb_type)){
  5738. for(list=0; list<2; list++){
  5739. if(IS_DIR(mb_type, 0, list)){
  5740. if(h->ref_count[list] > 0 ){
  5741. const int ref = h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 0 ) : 0;
  5742. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
  5743. }
  5744. }else
  5745. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  5746. }
  5747. for(list=0; list<2; list++){
  5748. if(IS_DIR(mb_type, 0, list)){
  5749. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);
  5750. mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );
  5751. my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );
  5752. tprintf("final mv:%d %d\n", mx, my);
  5753. fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5754. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  5755. }else
  5756. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  5757. }
  5758. }
  5759. else if(IS_16X8(mb_type)){
  5760. for(list=0; list<2; list++){
  5761. if(h->ref_count[list]>0){
  5762. for(i=0; i<2; i++){
  5763. if(IS_DIR(mb_type, i, list)){
  5764. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 8*i ) : 0;
  5765. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
  5766. }else
  5767. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  5768. }
  5769. }
  5770. }
  5771. for(list=0; list<2; list++){
  5772. for(i=0; i<2; i++){
  5773. if(IS_DIR(mb_type, i, list)){
  5774. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);
  5775. mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );
  5776. my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );
  5777. tprintf("final mv:%d %d\n", mx, my);
  5778. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);
  5779. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  5780. }else{
  5781. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5782. fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5783. }
  5784. }
  5785. }
  5786. }else{
  5787. assert(IS_8X16(mb_type));
  5788. for(list=0; list<2; list++){
  5789. if(h->ref_count[list]>0){
  5790. for(i=0; i<2; i++){
  5791. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  5792. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 4*i ) : 0;
  5793. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
  5794. }else
  5795. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  5796. }
  5797. }
  5798. }
  5799. for(list=0; list<2; list++){
  5800. for(i=0; i<2; i++){
  5801. if(IS_DIR(mb_type, i, list)){
  5802. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);
  5803. mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );
  5804. my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );
  5805. tprintf("final mv:%d %d\n", mx, my);
  5806. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5807. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  5808. }else{
  5809. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5810. fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5811. }
  5812. }
  5813. }
  5814. }
  5815. }
  5816. if( IS_INTER( mb_type ) ) {
  5817. h->chroma_pred_mode_table[mb_xy] = 0;
  5818. write_back_motion( h, mb_type );
  5819. }
  5820. if( !IS_INTRA16x16( mb_type ) ) {
  5821. cbp = decode_cabac_mb_cbp_luma( h );
  5822. cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
  5823. }
  5824. h->cbp_table[mb_xy] = h->cbp = cbp;
  5825. if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
  5826. if( decode_cabac_mb_transform_size( h ) )
  5827. mb_type |= MB_TYPE_8x8DCT;
  5828. }
  5829. s->current_picture.mb_type[mb_xy]= mb_type;
  5830. if( cbp || IS_INTRA16x16( mb_type ) ) {
  5831. const uint8_t *scan, *scan8x8, *dc_scan;
  5832. int dqp;
  5833. if(IS_INTERLACED(mb_type)){
  5834. scan8x8= s->qscale ? h->field_scan8x8 : h->field_scan8x8_q0;
  5835. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  5836. dc_scan= luma_dc_field_scan;
  5837. }else{
  5838. scan8x8= s->qscale ? h->zigzag_scan8x8 : h->zigzag_scan8x8_q0;
  5839. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  5840. dc_scan= luma_dc_zigzag_scan;
  5841. }
  5842. h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
  5843. if( dqp == INT_MIN ){
  5844. av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", s->mb_x, s->mb_y);
  5845. return -1;
  5846. }
  5847. s->qscale += dqp;
  5848. if(((unsigned)s->qscale) > 51){
  5849. if(s->qscale<0) s->qscale+= 52;
  5850. else s->qscale-= 52;
  5851. }
  5852. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  5853. if( IS_INTRA16x16( mb_type ) ) {
  5854. int i;
  5855. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
  5856. if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16) < 0)
  5857. return -1;
  5858. if( cbp&15 ) {
  5859. for( i = 0; i < 16; i++ ) {
  5860. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
  5861. if( decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 )
  5862. return -1;
  5863. }
  5864. } else {
  5865. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  5866. }
  5867. } else {
  5868. int i8x8, i4x4;
  5869. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5870. if( cbp & (1<<i8x8) ) {
  5871. if( IS_8x8DCT(mb_type) ) {
  5872. if( decode_cabac_residual(h, h->mb + 64*i8x8, 5, 4*i8x8,
  5873. scan8x8, h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 64) < 0 )
  5874. return -1;
  5875. } else
  5876. for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
  5877. const int index = 4*i8x8 + i4x4;
  5878. //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
  5879. if( decode_cabac_residual(h, h->mb + 16*index, 2, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) < 0 )
  5880. return -1;
  5881. }
  5882. } else {
  5883. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  5884. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  5885. }
  5886. }
  5887. }
  5888. if( cbp&0x30 ){
  5889. int c;
  5890. for( c = 0; c < 2; c++ ) {
  5891. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
  5892. if( decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, NULL, 4) < 0)
  5893. return -1;
  5894. }
  5895. }
  5896. if( cbp&0x20 ) {
  5897. int c, i;
  5898. for( c = 0; c < 2; c++ ) {
  5899. for( i = 0; i < 4; i++ ) {
  5900. const int index = 16 + 4 * c + i;
  5901. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
  5902. if( decode_cabac_residual(h, h->mb + 16*index, 4, index - 16, scan + 1, h->dequant4_coeff[c+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp], 15) < 0)
  5903. return -1;
  5904. }
  5905. }
  5906. } else {
  5907. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5908. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5909. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5910. }
  5911. } else {
  5912. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5913. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  5914. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5915. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5916. h->last_qscale_diff = 0;
  5917. }
  5918. s->current_picture.qscale_table[mb_xy]= s->qscale;
  5919. write_back_non_zero_count(h);
  5920. if(MB_MBAFF){
  5921. h->ref_count[0] >>= 1;
  5922. h->ref_count[1] >>= 1;
  5923. }
  5924. return 0;
  5925. }
  5926. static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5927. int i, d;
  5928. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  5929. const int alpha = alpha_table[index_a];
  5930. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  5931. if( bS[0] < 4 ) {
  5932. int8_t tc[4];
  5933. for(i=0; i<4; i++)
  5934. tc[i] = bS[i] ? tc0_table[index_a][bS[i] - 1] : -1;
  5935. h->s.dsp.h264_h_loop_filter_luma(pix, stride, alpha, beta, tc);
  5936. } else {
  5937. /* 16px edge length, because bS=4 is triggered by being at
  5938. * the edge of an intra MB, so all 4 bS are the same */
  5939. for( d = 0; d < 16; d++ ) {
  5940. const int p0 = pix[-1];
  5941. const int p1 = pix[-2];
  5942. const int p2 = pix[-3];
  5943. const int q0 = pix[0];
  5944. const int q1 = pix[1];
  5945. const int q2 = pix[2];
  5946. if( ABS( p0 - q0 ) < alpha &&
  5947. ABS( p1 - p0 ) < beta &&
  5948. ABS( q1 - q0 ) < beta ) {
  5949. if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5950. if( ABS( p2 - p0 ) < beta)
  5951. {
  5952. const int p3 = pix[-4];
  5953. /* p0', p1', p2' */
  5954. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5955. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5956. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5957. } else {
  5958. /* p0' */
  5959. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5960. }
  5961. if( ABS( q2 - q0 ) < beta)
  5962. {
  5963. const int q3 = pix[3];
  5964. /* q0', q1', q2' */
  5965. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5966. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5967. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5968. } else {
  5969. /* q0' */
  5970. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5971. }
  5972. }else{
  5973. /* p0', q0' */
  5974. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5975. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5976. }
  5977. 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]);
  5978. }
  5979. pix += stride;
  5980. }
  5981. }
  5982. }
  5983. static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5984. int i;
  5985. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  5986. const int alpha = alpha_table[index_a];
  5987. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  5988. if( bS[0] < 4 ) {
  5989. int8_t tc[4];
  5990. for(i=0; i<4; i++)
  5991. tc[i] = bS[i] ? tc0_table[index_a][bS[i] - 1] + 1 : 0;
  5992. h->s.dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
  5993. } else {
  5994. h->s.dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
  5995. }
  5996. }
  5997. static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  5998. int i;
  5999. for( i = 0; i < 16; i++, pix += stride) {
  6000. int index_a;
  6001. int alpha;
  6002. int beta;
  6003. int qp_index;
  6004. int bS_index = (i >> 1);
  6005. if (!MB_FIELD) {
  6006. bS_index &= ~1;
  6007. bS_index |= (i & 1);
  6008. }
  6009. if( bS[bS_index] == 0 ) {
  6010. continue;
  6011. }
  6012. qp_index = MB_FIELD ? (i >> 3) : (i & 1);
  6013. index_a = clip( qp[qp_index] + h->slice_alpha_c0_offset, 0, 51 );
  6014. alpha = alpha_table[index_a];
  6015. beta = beta_table[clip( qp[qp_index] + h->slice_beta_offset, 0, 51 )];
  6016. if( bS[bS_index] < 4 ) {
  6017. const int tc0 = tc0_table[index_a][bS[bS_index] - 1];
  6018. const int p0 = pix[-1];
  6019. const int p1 = pix[-2];
  6020. const int p2 = pix[-3];
  6021. const int q0 = pix[0];
  6022. const int q1 = pix[1];
  6023. const int q2 = pix[2];
  6024. if( ABS( p0 - q0 ) < alpha &&
  6025. ABS( p1 - p0 ) < beta &&
  6026. ABS( q1 - q0 ) < beta ) {
  6027. int tc = tc0;
  6028. int i_delta;
  6029. if( ABS( p2 - p0 ) < beta ) {
  6030. pix[-2] = p1 + clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  6031. tc++;
  6032. }
  6033. if( ABS( q2 - q0 ) < beta ) {
  6034. pix[1] = q1 + clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  6035. tc++;
  6036. }
  6037. i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  6038. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  6039. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  6040. 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);
  6041. }
  6042. }else{
  6043. const int p0 = pix[-1];
  6044. const int p1 = pix[-2];
  6045. const int p2 = pix[-3];
  6046. const int q0 = pix[0];
  6047. const int q1 = pix[1];
  6048. const int q2 = pix[2];
  6049. if( ABS( p0 - q0 ) < alpha &&
  6050. ABS( p1 - p0 ) < beta &&
  6051. ABS( q1 - q0 ) < beta ) {
  6052. if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  6053. if( ABS( p2 - p0 ) < beta)
  6054. {
  6055. const int p3 = pix[-4];
  6056. /* p0', p1', p2' */
  6057. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  6058. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  6059. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  6060. } else {
  6061. /* p0' */
  6062. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6063. }
  6064. if( ABS( q2 - q0 ) < beta)
  6065. {
  6066. const int q3 = pix[3];
  6067. /* q0', q1', q2' */
  6068. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  6069. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  6070. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  6071. } else {
  6072. /* q0' */
  6073. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6074. }
  6075. }else{
  6076. /* p0', q0' */
  6077. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6078. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6079. }
  6080. 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]);
  6081. }
  6082. }
  6083. }
  6084. }
  6085. static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  6086. int i;
  6087. for( i = 0; i < 8; i++, pix += stride) {
  6088. int index_a;
  6089. int alpha;
  6090. int beta;
  6091. int qp_index;
  6092. int bS_index = i;
  6093. if( bS[bS_index] == 0 ) {
  6094. continue;
  6095. }
  6096. qp_index = MB_FIELD ? (i >> 2) : (i & 1);
  6097. index_a = clip( qp[qp_index] + h->slice_alpha_c0_offset, 0, 51 );
  6098. alpha = alpha_table[index_a];
  6099. beta = beta_table[clip( qp[qp_index] + h->slice_beta_offset, 0, 51 )];
  6100. if( bS[bS_index] < 4 ) {
  6101. const int tc = tc0_table[index_a][bS[bS_index] - 1] + 1;
  6102. const int p0 = pix[-1];
  6103. const int p1 = pix[-2];
  6104. const int q0 = pix[0];
  6105. const int q1 = pix[1];
  6106. if( ABS( p0 - q0 ) < alpha &&
  6107. ABS( p1 - p0 ) < beta &&
  6108. ABS( q1 - q0 ) < beta ) {
  6109. const int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  6110. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  6111. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  6112. 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);
  6113. }
  6114. }else{
  6115. const int p0 = pix[-1];
  6116. const int p1 = pix[-2];
  6117. const int q0 = pix[0];
  6118. const int q1 = pix[1];
  6119. if( ABS( p0 - q0 ) < alpha &&
  6120. ABS( p1 - p0 ) < beta &&
  6121. ABS( q1 - q0 ) < beta ) {
  6122. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  6123. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  6124. 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]);
  6125. }
  6126. }
  6127. }
  6128. }
  6129. static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  6130. int i, d;
  6131. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  6132. const int alpha = alpha_table[index_a];
  6133. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  6134. const int pix_next = stride;
  6135. if( bS[0] < 4 ) {
  6136. int8_t tc[4];
  6137. for(i=0; i<4; i++)
  6138. tc[i] = bS[i] ? tc0_table[index_a][bS[i] - 1] : -1;
  6139. h->s.dsp.h264_v_loop_filter_luma(pix, stride, alpha, beta, tc);
  6140. } else {
  6141. /* 16px edge length, see filter_mb_edgev */
  6142. for( d = 0; d < 16; d++ ) {
  6143. const int p0 = pix[-1*pix_next];
  6144. const int p1 = pix[-2*pix_next];
  6145. const int p2 = pix[-3*pix_next];
  6146. const int q0 = pix[0];
  6147. const int q1 = pix[1*pix_next];
  6148. const int q2 = pix[2*pix_next];
  6149. if( ABS( p0 - q0 ) < alpha &&
  6150. ABS( p1 - p0 ) < beta &&
  6151. ABS( q1 - q0 ) < beta ) {
  6152. const int p3 = pix[-4*pix_next];
  6153. const int q3 = pix[ 3*pix_next];
  6154. if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  6155. if( ABS( p2 - p0 ) < beta) {
  6156. /* p0', p1', p2' */
  6157. pix[-1*pix_next] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  6158. pix[-2*pix_next] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  6159. pix[-3*pix_next] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  6160. } else {
  6161. /* p0' */
  6162. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6163. }
  6164. if( ABS( q2 - q0 ) < beta) {
  6165. /* q0', q1', q2' */
  6166. pix[0*pix_next] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  6167. pix[1*pix_next] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  6168. pix[2*pix_next] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  6169. } else {
  6170. /* q0' */
  6171. pix[0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6172. }
  6173. }else{
  6174. /* p0', q0' */
  6175. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6176. pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6177. }
  6178. 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]);
  6179. }
  6180. pix++;
  6181. }
  6182. }
  6183. }
  6184. static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  6185. int i;
  6186. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  6187. const int alpha = alpha_table[index_a];
  6188. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  6189. if( bS[0] < 4 ) {
  6190. int8_t tc[4];
  6191. for(i=0; i<4; i++)
  6192. tc[i] = bS[i] ? tc0_table[index_a][bS[i] - 1] + 1 : 0;
  6193. h->s.dsp.h264_v_loop_filter_chroma(pix, stride, alpha, beta, tc);
  6194. } else {
  6195. h->s.dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);
  6196. }
  6197. }
  6198. static void filter_mb_fast( 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) {
  6199. MpegEncContext * const s = &h->s;
  6200. int mb_xy, mb_type;
  6201. int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh;
  6202. if(mb_x==0 || mb_y==0 || !s->dsp.h264_loop_filter_strength) {
  6203. filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);
  6204. return;
  6205. }
  6206. assert(!FRAME_MBAFF);
  6207. mb_xy = mb_x + mb_y*s->mb_stride;
  6208. mb_type = s->current_picture.mb_type[mb_xy];
  6209. qp = s->current_picture.qscale_table[mb_xy];
  6210. qp0 = s->current_picture.qscale_table[mb_xy-1];
  6211. qp1 = s->current_picture.qscale_table[h->top_mb_xy];
  6212. qpc = get_chroma_qp( h->pps.chroma_qp_index_offset, qp );
  6213. qpc0 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp0 );
  6214. qpc1 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp1 );
  6215. qp0 = (qp + qp0 + 1) >> 1;
  6216. qp1 = (qp + qp1 + 1) >> 1;
  6217. qpc0 = (qpc + qpc0 + 1) >> 1;
  6218. qpc1 = (qpc + qpc1 + 1) >> 1;
  6219. qp_thresh = 15 - h->slice_alpha_c0_offset;
  6220. if(qp <= qp_thresh && qp0 <= qp_thresh && qp1 <= qp_thresh &&
  6221. qpc <= qp_thresh && qpc0 <= qp_thresh && qpc1 <= qp_thresh)
  6222. return;
  6223. if( IS_INTRA(mb_type) ) {
  6224. int16_t bS4[4] = {4,4,4,4};
  6225. int16_t bS3[4] = {3,3,3,3};
  6226. if( IS_8x8DCT(mb_type) ) {
  6227. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  6228. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  6229. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  6230. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  6231. } else {
  6232. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  6233. filter_mb_edgev( h, &img_y[4*1], linesize, bS3, qp );
  6234. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  6235. filter_mb_edgev( h, &img_y[4*3], linesize, bS3, qp );
  6236. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  6237. filter_mb_edgeh( h, &img_y[4*1*linesize], linesize, bS3, qp );
  6238. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  6239. filter_mb_edgeh( h, &img_y[4*3*linesize], linesize, bS3, qp );
  6240. }
  6241. filter_mb_edgecv( h, &img_cb[2*0], uvlinesize, bS4, qpc0 );
  6242. filter_mb_edgecv( h, &img_cb[2*2], uvlinesize, bS3, qpc );
  6243. filter_mb_edgecv( h, &img_cr[2*0], uvlinesize, bS4, qpc0 );
  6244. filter_mb_edgecv( h, &img_cr[2*2], uvlinesize, bS3, qpc );
  6245. filter_mb_edgech( h, &img_cb[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  6246. filter_mb_edgech( h, &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc );
  6247. filter_mb_edgech( h, &img_cr[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  6248. filter_mb_edgech( h, &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc );
  6249. return;
  6250. } else {
  6251. DECLARE_ALIGNED_8(int16_t, bS[2][4][4]);
  6252. uint64_t (*bSv)[4] = (uint64_t(*)[4])bS;
  6253. int edges;
  6254. if( IS_8x8DCT(mb_type) && (h->cbp&7) == 7 ) {
  6255. edges = 4;
  6256. bSv[0][0] = bSv[0][2] = bSv[1][0] = bSv[1][2] = 0x0002000200020002ULL;
  6257. } else {
  6258. int mask_edge1 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 :
  6259. (mb_type & MB_TYPE_16x8) ? 1 : 0;
  6260. int mask_edge0 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16))
  6261. && (s->current_picture.mb_type[mb_xy-1] & (MB_TYPE_16x16 | MB_TYPE_8x16))
  6262. ? 3 : 0;
  6263. int step = IS_8x8DCT(mb_type) ? 2 : 1;
  6264. edges = (mb_type & MB_TYPE_16x16) && !(h->cbp & 15) ? 1 : 4;
  6265. s->dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,
  6266. (h->slice_type == B_TYPE), edges, step, mask_edge0, mask_edge1 );
  6267. }
  6268. if( IS_INTRA(s->current_picture.mb_type[mb_xy-1]) )
  6269. bSv[0][0] = 0x0004000400040004ULL;
  6270. if( IS_INTRA(s->current_picture.mb_type[h->top_mb_xy]) )
  6271. bSv[1][0] = 0x0004000400040004ULL;
  6272. #define FILTER(hv,dir,edge)\
  6273. if(bSv[dir][edge]) {\
  6274. filter_mb_edge##hv( h, &img_y[4*edge*(dir?linesize:1)], linesize, bS[dir][edge], edge ? qp : qp##dir );\
  6275. if(!(edge&1)) {\
  6276. filter_mb_edgec##hv( h, &img_cb[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  6277. filter_mb_edgec##hv( h, &img_cr[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  6278. }\
  6279. }
  6280. if( edges == 1 ) {
  6281. FILTER(v,0,0);
  6282. FILTER(h,1,0);
  6283. } else if( IS_8x8DCT(mb_type) ) {
  6284. FILTER(v,0,0);
  6285. FILTER(v,0,2);
  6286. FILTER(h,1,0);
  6287. FILTER(h,1,2);
  6288. } else {
  6289. FILTER(v,0,0);
  6290. FILTER(v,0,1);
  6291. FILTER(v,0,2);
  6292. FILTER(v,0,3);
  6293. FILTER(h,1,0);
  6294. FILTER(h,1,1);
  6295. FILTER(h,1,2);
  6296. FILTER(h,1,3);
  6297. }
  6298. #undef FILTER
  6299. }
  6300. }
  6301. 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) {
  6302. MpegEncContext * const s = &h->s;
  6303. const int mb_xy= mb_x + mb_y*s->mb_stride;
  6304. const int mb_type = s->current_picture.mb_type[mb_xy];
  6305. const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4;
  6306. int first_vertical_edge_done = 0;
  6307. int dir;
  6308. /* FIXME: A given frame may occupy more than one position in
  6309. * the reference list. So ref2frm should be populated with
  6310. * frame numbers, not indices. */
  6311. static const int ref2frm[34] = {-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  6312. 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
  6313. //for sufficiently low qp, filtering wouldn't do anything
  6314. //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
  6315. if(!FRAME_MBAFF){
  6316. int qp_thresh = 15 - h->slice_alpha_c0_offset - FFMAX(0, h->pps.chroma_qp_index_offset);
  6317. int qp = s->current_picture.qscale_table[mb_xy];
  6318. if(qp <= qp_thresh
  6319. && (mb_x == 0 || ((qp + s->current_picture.qscale_table[mb_xy-1] + 1)>>1) <= qp_thresh)
  6320. && (mb_y == 0 || ((qp + s->current_picture.qscale_table[h->top_mb_xy] + 1)>>1) <= qp_thresh)){
  6321. return;
  6322. }
  6323. }
  6324. if (FRAME_MBAFF
  6325. // left mb is in picture
  6326. && h->slice_table[mb_xy-1] != 255
  6327. // and current and left pair do not have the same interlaced type
  6328. && (IS_INTERLACED(mb_type) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
  6329. // and left mb is in the same slice if deblocking_filter == 2
  6330. && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
  6331. /* First vertical edge is different in MBAFF frames
  6332. * There are 8 different bS to compute and 2 different Qp
  6333. */
  6334. const int pair_xy = mb_x + (mb_y&~1)*s->mb_stride;
  6335. const int left_mb_xy[2] = { pair_xy-1, pair_xy-1+s->mb_stride };
  6336. int16_t bS[8];
  6337. int qp[2];
  6338. int chroma_qp[2];
  6339. int mb_qp, mbn0_qp, mbn1_qp;
  6340. int i;
  6341. first_vertical_edge_done = 1;
  6342. if( IS_INTRA(mb_type) )
  6343. bS[0] = bS[1] = bS[2] = bS[3] = bS[4] = bS[5] = bS[6] = bS[7] = 4;
  6344. else {
  6345. for( i = 0; i < 8; i++ ) {
  6346. int mbn_xy = MB_FIELD ? left_mb_xy[i>>2] : left_mb_xy[i&1];
  6347. if( IS_INTRA( s->current_picture.mb_type[mbn_xy] ) )
  6348. bS[i] = 4;
  6349. else if( h->non_zero_count_cache[12+8*(i>>1)] != 0 ||
  6350. /* FIXME: with 8x8dct + cavlc, should check cbp instead of nnz */
  6351. h->non_zero_count[mbn_xy][MB_FIELD ? i&3 : (i>>2)+(mb_y&1)*2] )
  6352. bS[i] = 2;
  6353. else
  6354. bS[i] = 1;
  6355. }
  6356. }
  6357. mb_qp = s->current_picture.qscale_table[mb_xy];
  6358. mbn0_qp = s->current_picture.qscale_table[left_mb_xy[0]];
  6359. mbn1_qp = s->current_picture.qscale_table[left_mb_xy[1]];
  6360. qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1;
  6361. chroma_qp[0] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6362. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn0_qp ) + 1 ) >> 1;
  6363. qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1;
  6364. chroma_qp[1] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6365. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn1_qp ) + 1 ) >> 1;
  6366. /* Filter edge */
  6367. 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);
  6368. { int i; for (i = 0; i < 8; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6369. filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp );
  6370. filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, chroma_qp );
  6371. filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, chroma_qp );
  6372. }
  6373. /* dir : 0 -> vertical edge, 1 -> horizontal edge */
  6374. for( dir = 0; dir < 2; dir++ )
  6375. {
  6376. int edge;
  6377. const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
  6378. const int mbm_type = s->current_picture.mb_type[mbm_xy];
  6379. int start = h->slice_table[mbm_xy] == 255 ? 1 : 0;
  6380. const int edges = (mb_type & (MB_TYPE_16x16|MB_TYPE_SKIP))
  6381. == (MB_TYPE_16x16|MB_TYPE_SKIP) ? 1 : 4;
  6382. // how often to recheck mv-based bS when iterating between edges
  6383. const int mask_edge = (mb_type & (MB_TYPE_16x16 | (MB_TYPE_16x8 << dir))) ? 3 :
  6384. (mb_type & (MB_TYPE_8x16 >> dir)) ? 1 : 0;
  6385. // how often to recheck mv-based bS when iterating along each edge
  6386. const int mask_par0 = mb_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir));
  6387. if (first_vertical_edge_done) {
  6388. start = 1;
  6389. first_vertical_edge_done = 0;
  6390. }
  6391. if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
  6392. start = 1;
  6393. if (FRAME_MBAFF && (dir == 1) && ((mb_y&1) == 0) && start == 0
  6394. && !IS_INTERLACED(mb_type)
  6395. && IS_INTERLACED(mbm_type)
  6396. ) {
  6397. // This is a special case in the norm where the filtering must
  6398. // be done twice (one each of the field) even if we are in a
  6399. // frame macroblock.
  6400. //
  6401. static const int nnz_idx[4] = {4,5,6,3};
  6402. unsigned int tmp_linesize = 2 * linesize;
  6403. unsigned int tmp_uvlinesize = 2 * uvlinesize;
  6404. int mbn_xy = mb_xy - 2 * s->mb_stride;
  6405. int qp, chroma_qp;
  6406. int i, j;
  6407. int16_t bS[4];
  6408. for(j=0; j<2; j++, mbn_xy += s->mb_stride){
  6409. if( IS_INTRA(mb_type) ||
  6410. IS_INTRA(s->current_picture.mb_type[mbn_xy]) ) {
  6411. bS[0] = bS[1] = bS[2] = bS[3] = 3;
  6412. } else {
  6413. const uint8_t *mbn_nnz = h->non_zero_count[mbn_xy];
  6414. for( i = 0; i < 4; i++ ) {
  6415. if( h->non_zero_count_cache[scan8[0]+i] != 0 ||
  6416. mbn_nnz[nnz_idx[i]] != 0 )
  6417. bS[i] = 2;
  6418. else
  6419. bS[i] = 1;
  6420. }
  6421. }
  6422. // Do not use s->qscale as luma quantizer because it has not the same
  6423. // value in IPCM macroblocks.
  6424. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6425. 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);
  6426. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6427. filter_mb_edgeh( h, &img_y[j*linesize], tmp_linesize, bS, qp );
  6428. chroma_qp = ( h->chroma_qp +
  6429. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6430. filter_mb_edgech( h, &img_cb[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6431. filter_mb_edgech( h, &img_cr[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6432. }
  6433. start = 1;
  6434. }
  6435. /* Calculate bS */
  6436. for( edge = start; edge < edges; edge++ ) {
  6437. /* mbn_xy: neighbor macroblock */
  6438. const int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
  6439. const int mbn_type = s->current_picture.mb_type[mbn_xy];
  6440. int16_t bS[4];
  6441. int qp;
  6442. if( (edge&1) && IS_8x8DCT(mb_type) )
  6443. continue;
  6444. if( IS_INTRA(mb_type) ||
  6445. IS_INTRA(mbn_type) ) {
  6446. int value;
  6447. if (edge == 0) {
  6448. if ( (!IS_INTERLACED(mb_type) && !IS_INTERLACED(mbm_type))
  6449. || ((FRAME_MBAFF || (s->picture_structure != PICT_FRAME)) && (dir == 0))
  6450. ) {
  6451. value = 4;
  6452. } else {
  6453. value = 3;
  6454. }
  6455. } else {
  6456. value = 3;
  6457. }
  6458. bS[0] = bS[1] = bS[2] = bS[3] = value;
  6459. } else {
  6460. int i, l;
  6461. int mv_done;
  6462. if( edge & mask_edge ) {
  6463. bS[0] = bS[1] = bS[2] = bS[3] = 0;
  6464. mv_done = 1;
  6465. }
  6466. else if( FRAME_MBAFF && IS_INTERLACED(mb_type ^ mbn_type)) {
  6467. bS[0] = bS[1] = bS[2] = bS[3] = 1;
  6468. mv_done = 1;
  6469. }
  6470. else if( mask_par0 && (edge || (mbn_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir)))) ) {
  6471. int b_idx= 8 + 4 + edge * (dir ? 8:1);
  6472. int bn_idx= b_idx - (dir ? 8:1);
  6473. int v = 0;
  6474. for( l = 0; !v && l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6475. v |= ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6476. ABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6477. ABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit;
  6478. }
  6479. bS[0] = bS[1] = bS[2] = bS[3] = v;
  6480. mv_done = 1;
  6481. }
  6482. else
  6483. mv_done = 0;
  6484. for( i = 0; i < 4; i++ ) {
  6485. int x = dir == 0 ? edge : i;
  6486. int y = dir == 0 ? i : edge;
  6487. int b_idx= 8 + 4 + x + 8*y;
  6488. int bn_idx= b_idx - (dir ? 8:1);
  6489. if( h->non_zero_count_cache[b_idx] != 0 ||
  6490. h->non_zero_count_cache[bn_idx] != 0 ) {
  6491. bS[i] = 2;
  6492. }
  6493. else if(!mv_done)
  6494. {
  6495. bS[i] = 0;
  6496. for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6497. if( ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6498. ABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6499. ABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit ) {
  6500. bS[i] = 1;
  6501. break;
  6502. }
  6503. }
  6504. }
  6505. }
  6506. if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
  6507. continue;
  6508. }
  6509. /* Filter edge */
  6510. // Do not use s->qscale as luma quantizer because it has not the same
  6511. // value in IPCM macroblocks.
  6512. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6513. //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]);
  6514. tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
  6515. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6516. if( dir == 0 ) {
  6517. filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
  6518. if( (edge&1) == 0 ) {
  6519. int chroma_qp = ( h->chroma_qp +
  6520. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6521. filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp );
  6522. filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp );
  6523. }
  6524. } else {
  6525. filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
  6526. if( (edge&1) == 0 ) {
  6527. int chroma_qp = ( h->chroma_qp +
  6528. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6529. filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6530. filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6531. }
  6532. }
  6533. }
  6534. }
  6535. }
  6536. static int decode_slice(H264Context *h){
  6537. MpegEncContext * const s = &h->s;
  6538. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  6539. s->mb_skip_run= -1;
  6540. if( h->pps.cabac ) {
  6541. int i;
  6542. /* realign */
  6543. align_get_bits( &s->gb );
  6544. /* init cabac */
  6545. ff_init_cabac_states( &h->cabac, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64 );
  6546. ff_init_cabac_decoder( &h->cabac,
  6547. s->gb.buffer + get_bits_count(&s->gb)/8,
  6548. ( s->gb.size_in_bits - get_bits_count(&s->gb) + 7)/8);
  6549. /* calculate pre-state */
  6550. for( i= 0; i < 460; i++ ) {
  6551. int pre;
  6552. if( h->slice_type == I_TYPE )
  6553. pre = clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
  6554. else
  6555. 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 );
  6556. if( pre <= 63 )
  6557. h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
  6558. else
  6559. h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
  6560. }
  6561. for(;;){
  6562. int ret = decode_mb_cabac(h);
  6563. int eos;
  6564. if(ret>=0) hl_decode_mb(h);
  6565. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  6566. s->mb_y++;
  6567. if(ret>=0) ret = decode_mb_cabac(h);
  6568. if(ret>=0) hl_decode_mb(h);
  6569. s->mb_y--;
  6570. }
  6571. eos = get_cabac_terminate( &h->cabac );
  6572. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 1) {
  6573. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d, bytestream (%d)\n", s->mb_x, s->mb_y, h->cabac.bytestream_end - h->cabac.bytestream);
  6574. 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);
  6575. return -1;
  6576. }
  6577. if( ++s->mb_x >= s->mb_width ) {
  6578. s->mb_x = 0;
  6579. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6580. ++s->mb_y;
  6581. if(FRAME_MBAFF) {
  6582. ++s->mb_y;
  6583. }
  6584. }
  6585. if( eos || s->mb_y >= s->mb_height ) {
  6586. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6587. 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);
  6588. return 0;
  6589. }
  6590. }
  6591. } else {
  6592. for(;;){
  6593. int ret = decode_mb_cavlc(h);
  6594. if(ret>=0) hl_decode_mb(h);
  6595. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  6596. s->mb_y++;
  6597. ret = decode_mb_cavlc(h);
  6598. if(ret>=0) hl_decode_mb(h);
  6599. s->mb_y--;
  6600. }
  6601. if(ret<0){
  6602. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6603. 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);
  6604. return -1;
  6605. }
  6606. if(++s->mb_x >= s->mb_width){
  6607. s->mb_x=0;
  6608. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6609. ++s->mb_y;
  6610. if(FRAME_MBAFF) {
  6611. ++s->mb_y;
  6612. }
  6613. if(s->mb_y >= s->mb_height){
  6614. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6615. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  6616. 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);
  6617. return 0;
  6618. }else{
  6619. 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);
  6620. return -1;
  6621. }
  6622. }
  6623. }
  6624. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  6625. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6626. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  6627. 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);
  6628. return 0;
  6629. }else{
  6630. 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);
  6631. return -1;
  6632. }
  6633. }
  6634. }
  6635. }
  6636. #if 0
  6637. for(;s->mb_y < s->mb_height; s->mb_y++){
  6638. for(;s->mb_x < s->mb_width; s->mb_x++){
  6639. int ret= decode_mb(h);
  6640. hl_decode_mb(h);
  6641. if(ret<0){
  6642. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6643. 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);
  6644. return -1;
  6645. }
  6646. if(++s->mb_x >= s->mb_width){
  6647. s->mb_x=0;
  6648. if(++s->mb_y >= s->mb_height){
  6649. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6650. 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);
  6651. return 0;
  6652. }else{
  6653. 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);
  6654. return -1;
  6655. }
  6656. }
  6657. }
  6658. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  6659. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6660. 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);
  6661. return 0;
  6662. }else{
  6663. 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);
  6664. return -1;
  6665. }
  6666. }
  6667. }
  6668. s->mb_x=0;
  6669. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6670. }
  6671. #endif
  6672. return -1; //not reached
  6673. }
  6674. static int decode_unregistered_user_data(H264Context *h, int size){
  6675. MpegEncContext * const s = &h->s;
  6676. uint8_t user_data[16+256];
  6677. int e, build, i;
  6678. if(size<16)
  6679. return -1;
  6680. for(i=0; i<sizeof(user_data)-1 && i<size; i++){
  6681. user_data[i]= get_bits(&s->gb, 8);
  6682. }
  6683. user_data[i]= 0;
  6684. e= sscanf(user_data+16, "x264 - core %d"/*%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html*/, &build);
  6685. if(e==1 && build>=0)
  6686. h->x264_build= build;
  6687. if(s->avctx->debug & FF_DEBUG_BUGS)
  6688. av_log(s->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data+16);
  6689. for(; i<size; i++)
  6690. skip_bits(&s->gb, 8);
  6691. return 0;
  6692. }
  6693. static int decode_sei(H264Context *h){
  6694. MpegEncContext * const s = &h->s;
  6695. while(get_bits_count(&s->gb) + 16 < s->gb.size_in_bits){
  6696. int size, type;
  6697. type=0;
  6698. do{
  6699. type+= show_bits(&s->gb, 8);
  6700. }while(get_bits(&s->gb, 8) == 255);
  6701. size=0;
  6702. do{
  6703. size+= show_bits(&s->gb, 8);
  6704. }while(get_bits(&s->gb, 8) == 255);
  6705. switch(type){
  6706. case 5:
  6707. if(decode_unregistered_user_data(h, size) < 0)
  6708. return -1;
  6709. break;
  6710. default:
  6711. skip_bits(&s->gb, 8*size);
  6712. }
  6713. //FIXME check bits here
  6714. align_get_bits(&s->gb);
  6715. }
  6716. return 0;
  6717. }
  6718. static inline void decode_hrd_parameters(H264Context *h, SPS *sps){
  6719. MpegEncContext * const s = &h->s;
  6720. int cpb_count, i;
  6721. cpb_count = get_ue_golomb(&s->gb) + 1;
  6722. get_bits(&s->gb, 4); /* bit_rate_scale */
  6723. get_bits(&s->gb, 4); /* cpb_size_scale */
  6724. for(i=0; i<cpb_count; i++){
  6725. get_ue_golomb(&s->gb); /* bit_rate_value_minus1 */
  6726. get_ue_golomb(&s->gb); /* cpb_size_value_minus1 */
  6727. get_bits1(&s->gb); /* cbr_flag */
  6728. }
  6729. get_bits(&s->gb, 5); /* initial_cpb_removal_delay_length_minus1 */
  6730. get_bits(&s->gb, 5); /* cpb_removal_delay_length_minus1 */
  6731. get_bits(&s->gb, 5); /* dpb_output_delay_length_minus1 */
  6732. get_bits(&s->gb, 5); /* time_offset_length */
  6733. }
  6734. static inline int decode_vui_parameters(H264Context *h, SPS *sps){
  6735. MpegEncContext * const s = &h->s;
  6736. int aspect_ratio_info_present_flag, aspect_ratio_idc;
  6737. int nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag;
  6738. aspect_ratio_info_present_flag= get_bits1(&s->gb);
  6739. if( aspect_ratio_info_present_flag ) {
  6740. aspect_ratio_idc= get_bits(&s->gb, 8);
  6741. if( aspect_ratio_idc == EXTENDED_SAR ) {
  6742. sps->sar.num= get_bits(&s->gb, 16);
  6743. sps->sar.den= get_bits(&s->gb, 16);
  6744. }else if(aspect_ratio_idc < 14){
  6745. sps->sar= pixel_aspect[aspect_ratio_idc];
  6746. }else{
  6747. av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
  6748. return -1;
  6749. }
  6750. }else{
  6751. sps->sar.num=
  6752. sps->sar.den= 0;
  6753. }
  6754. // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
  6755. if(get_bits1(&s->gb)){ /* overscan_info_present_flag */
  6756. get_bits1(&s->gb); /* overscan_appropriate_flag */
  6757. }
  6758. if(get_bits1(&s->gb)){ /* video_signal_type_present_flag */
  6759. get_bits(&s->gb, 3); /* video_format */
  6760. get_bits1(&s->gb); /* video_full_range_flag */
  6761. if(get_bits1(&s->gb)){ /* colour_description_present_flag */
  6762. get_bits(&s->gb, 8); /* colour_primaries */
  6763. get_bits(&s->gb, 8); /* transfer_characteristics */
  6764. get_bits(&s->gb, 8); /* matrix_coefficients */
  6765. }
  6766. }
  6767. if(get_bits1(&s->gb)){ /* chroma_location_info_present_flag */
  6768. get_ue_golomb(&s->gb); /* chroma_sample_location_type_top_field */
  6769. get_ue_golomb(&s->gb); /* chroma_sample_location_type_bottom_field */
  6770. }
  6771. sps->timing_info_present_flag = get_bits1(&s->gb);
  6772. if(sps->timing_info_present_flag){
  6773. sps->num_units_in_tick = get_bits_long(&s->gb, 32);
  6774. sps->time_scale = get_bits_long(&s->gb, 32);
  6775. sps->fixed_frame_rate_flag = get_bits1(&s->gb);
  6776. }
  6777. nal_hrd_parameters_present_flag = get_bits1(&s->gb);
  6778. if(nal_hrd_parameters_present_flag)
  6779. decode_hrd_parameters(h, sps);
  6780. vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
  6781. if(vcl_hrd_parameters_present_flag)
  6782. decode_hrd_parameters(h, sps);
  6783. if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag)
  6784. get_bits1(&s->gb); /* low_delay_hrd_flag */
  6785. get_bits1(&s->gb); /* pic_struct_present_flag */
  6786. sps->bitstream_restriction_flag = get_bits1(&s->gb);
  6787. if(sps->bitstream_restriction_flag){
  6788. get_bits1(&s->gb); /* motion_vectors_over_pic_boundaries_flag */
  6789. get_ue_golomb(&s->gb); /* max_bytes_per_pic_denom */
  6790. get_ue_golomb(&s->gb); /* max_bits_per_mb_denom */
  6791. get_ue_golomb(&s->gb); /* log2_max_mv_length_horizontal */
  6792. get_ue_golomb(&s->gb); /* log2_max_mv_length_vertical */
  6793. sps->num_reorder_frames = get_ue_golomb(&s->gb);
  6794. get_ue_golomb(&s->gb); /* max_dec_frame_buffering */
  6795. }
  6796. return 0;
  6797. }
  6798. static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,
  6799. const uint8_t *jvt_list, const uint8_t *fallback_list){
  6800. MpegEncContext * const s = &h->s;
  6801. int i, last = 8, next = 8;
  6802. const uint8_t *scan = size == 16 ? zigzag_scan : zigzag_scan8x8;
  6803. if(!get_bits1(&s->gb)) /* matrix not written, we use the predicted one */
  6804. memcpy(factors, fallback_list, size*sizeof(uint8_t));
  6805. else
  6806. for(i=0;i<size;i++){
  6807. if(next)
  6808. next = (last + get_se_golomb(&s->gb)) & 0xff;
  6809. if(!i && !next){ /* matrix not written, we use the preset one */
  6810. memcpy(factors, jvt_list, size*sizeof(uint8_t));
  6811. break;
  6812. }
  6813. last = factors[scan[i]] = next ? next : last;
  6814. }
  6815. }
  6816. static void decode_scaling_matrices(H264Context *h, SPS *sps, PPS *pps, int is_sps,
  6817. uint8_t (*scaling_matrix4)[16], uint8_t (*scaling_matrix8)[64]){
  6818. MpegEncContext * const s = &h->s;
  6819. int fallback_sps = !is_sps && sps->scaling_matrix_present;
  6820. const uint8_t *fallback[4] = {
  6821. fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
  6822. fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
  6823. fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
  6824. fallback_sps ? sps->scaling_matrix8[1] : default_scaling8[1]
  6825. };
  6826. if(get_bits1(&s->gb)){
  6827. sps->scaling_matrix_present |= is_sps;
  6828. decode_scaling_list(h,scaling_matrix4[0],16,default_scaling4[0],fallback[0]); // Intra, Y
  6829. decode_scaling_list(h,scaling_matrix4[1],16,default_scaling4[0],scaling_matrix4[0]); // Intra, Cr
  6830. decode_scaling_list(h,scaling_matrix4[2],16,default_scaling4[0],scaling_matrix4[1]); // Intra, Cb
  6831. decode_scaling_list(h,scaling_matrix4[3],16,default_scaling4[1],fallback[1]); // Inter, Y
  6832. decode_scaling_list(h,scaling_matrix4[4],16,default_scaling4[1],scaling_matrix4[3]); // Inter, Cr
  6833. decode_scaling_list(h,scaling_matrix4[5],16,default_scaling4[1],scaling_matrix4[4]); // Inter, Cb
  6834. if(is_sps || pps->transform_8x8_mode){
  6835. decode_scaling_list(h,scaling_matrix8[0],64,default_scaling8[0],fallback[2]); // Intra, Y
  6836. decode_scaling_list(h,scaling_matrix8[1],64,default_scaling8[1],fallback[3]); // Inter, Y
  6837. }
  6838. } else if(fallback_sps) {
  6839. memcpy(scaling_matrix4, sps->scaling_matrix4, 6*16*sizeof(uint8_t));
  6840. memcpy(scaling_matrix8, sps->scaling_matrix8, 2*64*sizeof(uint8_t));
  6841. }
  6842. }
  6843. static inline int decode_seq_parameter_set(H264Context *h){
  6844. MpegEncContext * const s = &h->s;
  6845. int profile_idc, level_idc;
  6846. int sps_id, i;
  6847. SPS *sps;
  6848. profile_idc= get_bits(&s->gb, 8);
  6849. get_bits1(&s->gb); //constraint_set0_flag
  6850. get_bits1(&s->gb); //constraint_set1_flag
  6851. get_bits1(&s->gb); //constraint_set2_flag
  6852. get_bits1(&s->gb); //constraint_set3_flag
  6853. get_bits(&s->gb, 4); // reserved
  6854. level_idc= get_bits(&s->gb, 8);
  6855. sps_id= get_ue_golomb(&s->gb);
  6856. sps= &h->sps_buffer[ sps_id ];
  6857. sps->profile_idc= profile_idc;
  6858. sps->level_idc= level_idc;
  6859. if(sps->profile_idc >= 100){ //high profile
  6860. if(get_ue_golomb(&s->gb) == 3) //chroma_format_idc
  6861. get_bits1(&s->gb); //residual_color_transform_flag
  6862. get_ue_golomb(&s->gb); //bit_depth_luma_minus8
  6863. get_ue_golomb(&s->gb); //bit_depth_chroma_minus8
  6864. sps->transform_bypass = get_bits1(&s->gb);
  6865. decode_scaling_matrices(h, sps, NULL, 1, sps->scaling_matrix4, sps->scaling_matrix8);
  6866. }else
  6867. sps->scaling_matrix_present = 0;
  6868. sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
  6869. sps->poc_type= get_ue_golomb(&s->gb);
  6870. if(sps->poc_type == 0){ //FIXME #define
  6871. sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
  6872. } else if(sps->poc_type == 1){//FIXME #define
  6873. sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
  6874. sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
  6875. sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
  6876. sps->poc_cycle_length= get_ue_golomb(&s->gb);
  6877. for(i=0; i<sps->poc_cycle_length; i++)
  6878. sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
  6879. }
  6880. if(sps->poc_type > 2){
  6881. av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
  6882. return -1;
  6883. }
  6884. sps->ref_frame_count= get_ue_golomb(&s->gb);
  6885. if(sps->ref_frame_count > MAX_PICTURE_COUNT-2){
  6886. av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");
  6887. }
  6888. sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
  6889. sps->mb_width= get_ue_golomb(&s->gb) + 1;
  6890. sps->mb_height= get_ue_golomb(&s->gb) + 1;
  6891. if((unsigned)sps->mb_width >= INT_MAX/16 || (unsigned)sps->mb_height >= INT_MAX/16 ||
  6892. avcodec_check_dimensions(NULL, 16*sps->mb_width, 16*sps->mb_height))
  6893. return -1;
  6894. sps->frame_mbs_only_flag= get_bits1(&s->gb);
  6895. if(!sps->frame_mbs_only_flag)
  6896. sps->mb_aff= get_bits1(&s->gb);
  6897. else
  6898. sps->mb_aff= 0;
  6899. sps->direct_8x8_inference_flag= get_bits1(&s->gb);
  6900. #ifndef ALLOW_INTERLACE
  6901. if(sps->mb_aff)
  6902. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF support not included; enable it compilation time\n");
  6903. #endif
  6904. if(!sps->direct_8x8_inference_flag && sps->mb_aff)
  6905. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + !direct_8x8_inference is not implemented\n");
  6906. sps->crop= get_bits1(&s->gb);
  6907. if(sps->crop){
  6908. sps->crop_left = get_ue_golomb(&s->gb);
  6909. sps->crop_right = get_ue_golomb(&s->gb);
  6910. sps->crop_top = get_ue_golomb(&s->gb);
  6911. sps->crop_bottom= get_ue_golomb(&s->gb);
  6912. if(sps->crop_left || sps->crop_top){
  6913. av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ...\n");
  6914. }
  6915. }else{
  6916. sps->crop_left =
  6917. sps->crop_right =
  6918. sps->crop_top =
  6919. sps->crop_bottom= 0;
  6920. }
  6921. sps->vui_parameters_present_flag= get_bits1(&s->gb);
  6922. if( sps->vui_parameters_present_flag )
  6923. decode_vui_parameters(h, sps);
  6924. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6925. 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",
  6926. sps_id, sps->profile_idc, sps->level_idc,
  6927. sps->poc_type,
  6928. sps->ref_frame_count,
  6929. sps->mb_width, sps->mb_height,
  6930. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  6931. sps->direct_8x8_inference_flag ? "8B8" : "",
  6932. sps->crop_left, sps->crop_right,
  6933. sps->crop_top, sps->crop_bottom,
  6934. sps->vui_parameters_present_flag ? "VUI" : ""
  6935. );
  6936. }
  6937. return 0;
  6938. }
  6939. static inline int decode_picture_parameter_set(H264Context *h, int bit_length){
  6940. MpegEncContext * const s = &h->s;
  6941. int pps_id= get_ue_golomb(&s->gb);
  6942. PPS *pps= &h->pps_buffer[pps_id];
  6943. pps->sps_id= get_ue_golomb(&s->gb);
  6944. pps->cabac= get_bits1(&s->gb);
  6945. pps->pic_order_present= get_bits1(&s->gb);
  6946. pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
  6947. if(pps->slice_group_count > 1 ){
  6948. pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
  6949. av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
  6950. switch(pps->mb_slice_group_map_type){
  6951. case 0:
  6952. #if 0
  6953. | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
  6954. | run_length[ i ] |1 |ue(v) |
  6955. #endif
  6956. break;
  6957. case 2:
  6958. #if 0
  6959. | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
  6960. |{ | | |
  6961. | top_left_mb[ i ] |1 |ue(v) |
  6962. | bottom_right_mb[ i ] |1 |ue(v) |
  6963. | } | | |
  6964. #endif
  6965. break;
  6966. case 3:
  6967. case 4:
  6968. case 5:
  6969. #if 0
  6970. | slice_group_change_direction_flag |1 |u(1) |
  6971. | slice_group_change_rate_minus1 |1 |ue(v) |
  6972. #endif
  6973. break;
  6974. case 6:
  6975. #if 0
  6976. | slice_group_id_cnt_minus1 |1 |ue(v) |
  6977. | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
  6978. |) | | |
  6979. | slice_group_id[ i ] |1 |u(v) |
  6980. #endif
  6981. break;
  6982. }
  6983. }
  6984. pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  6985. pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  6986. if(pps->ref_count[0] > 32 || pps->ref_count[1] > 32){
  6987. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
  6988. return -1;
  6989. }
  6990. pps->weighted_pred= get_bits1(&s->gb);
  6991. pps->weighted_bipred_idc= get_bits(&s->gb, 2);
  6992. pps->init_qp= get_se_golomb(&s->gb) + 26;
  6993. pps->init_qs= get_se_golomb(&s->gb) + 26;
  6994. pps->chroma_qp_index_offset= get_se_golomb(&s->gb);
  6995. pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
  6996. pps->constrained_intra_pred= get_bits1(&s->gb);
  6997. pps->redundant_pic_cnt_present = get_bits1(&s->gb);
  6998. pps->transform_8x8_mode= 0;
  6999. h->dequant_coeff_pps= -1; //contents of sps/pps can change even if id doesn't, so reinit
  7000. memset(pps->scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  7001. memset(pps->scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  7002. if(get_bits_count(&s->gb) < bit_length){
  7003. pps->transform_8x8_mode= get_bits1(&s->gb);
  7004. decode_scaling_matrices(h, &h->sps_buffer[pps->sps_id], pps, 0, pps->scaling_matrix4, pps->scaling_matrix8);
  7005. get_se_golomb(&s->gb); //second_chroma_qp_index_offset
  7006. }
  7007. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  7008. 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",
  7009. pps_id, pps->sps_id,
  7010. pps->cabac ? "CABAC" : "CAVLC",
  7011. pps->slice_group_count,
  7012. pps->ref_count[0], pps->ref_count[1],
  7013. pps->weighted_pred ? "weighted" : "",
  7014. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset,
  7015. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  7016. pps->constrained_intra_pred ? "CONSTR" : "",
  7017. pps->redundant_pic_cnt_present ? "REDU" : "",
  7018. pps->transform_8x8_mode ? "8x8DCT" : ""
  7019. );
  7020. }
  7021. return 0;
  7022. }
  7023. /**
  7024. * finds the end of the current frame in the bitstream.
  7025. * @return the position of the first byte of the next frame, or -1
  7026. */
  7027. static int find_frame_end(H264Context *h, const uint8_t *buf, int buf_size){
  7028. int i;
  7029. uint32_t state;
  7030. ParseContext *pc = &(h->s.parse_context);
  7031. //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
  7032. // mb_addr= pc->mb_addr - 1;
  7033. state= pc->state;
  7034. for(i=0; i<=buf_size; i++){
  7035. if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  7036. tprintf("find_frame_end new startcode = %08x, frame_start_found = %d, pos = %d\n", state, pc->frame_start_found, i);
  7037. if(pc->frame_start_found){
  7038. // If there isn't one more byte in the buffer
  7039. // the test on first_mb_in_slice cannot be done yet
  7040. // do it at next call.
  7041. if (i >= buf_size) break;
  7042. if (buf[i] & 0x80) {
  7043. // first_mb_in_slice is 0, probably the first nal of a new
  7044. // slice
  7045. tprintf("find_frame_end frame_end_found, state = %08x, pos = %d\n", state, i);
  7046. pc->state=-1;
  7047. pc->frame_start_found= 0;
  7048. return i-4;
  7049. }
  7050. }
  7051. pc->frame_start_found = 1;
  7052. }
  7053. if((state&0xFFFFFF1F) == 0x107 || (state&0xFFFFFF1F) == 0x108 || (state&0xFFFFFF1F) == 0x109){
  7054. if(pc->frame_start_found){
  7055. pc->state=-1;
  7056. pc->frame_start_found= 0;
  7057. return i-4;
  7058. }
  7059. }
  7060. if (i<buf_size)
  7061. state= (state<<8) | buf[i];
  7062. }
  7063. pc->state= state;
  7064. return END_NOT_FOUND;
  7065. }
  7066. #ifdef CONFIG_H264_PARSER
  7067. static int h264_parse(AVCodecParserContext *s,
  7068. AVCodecContext *avctx,
  7069. uint8_t **poutbuf, int *poutbuf_size,
  7070. const uint8_t *buf, int buf_size)
  7071. {
  7072. H264Context *h = s->priv_data;
  7073. ParseContext *pc = &h->s.parse_context;
  7074. int next;
  7075. next= find_frame_end(h, buf, buf_size);
  7076. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  7077. *poutbuf = NULL;
  7078. *poutbuf_size = 0;
  7079. return buf_size;
  7080. }
  7081. *poutbuf = (uint8_t *)buf;
  7082. *poutbuf_size = buf_size;
  7083. return next;
  7084. }
  7085. static int h264_split(AVCodecContext *avctx,
  7086. const uint8_t *buf, int buf_size)
  7087. {
  7088. int i;
  7089. uint32_t state = -1;
  7090. int has_sps= 0;
  7091. for(i=0; i<=buf_size; i++){
  7092. if((state&0xFFFFFF1F) == 0x107)
  7093. has_sps=1;
  7094. /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  7095. }*/
  7096. if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
  7097. if(has_sps){
  7098. while(i>4 && buf[i-5]==0) i--;
  7099. return i-4;
  7100. }
  7101. }
  7102. if (i<buf_size)
  7103. state= (state<<8) | buf[i];
  7104. }
  7105. return 0;
  7106. }
  7107. #endif /* CONFIG_H264_PARSER */
  7108. static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){
  7109. MpegEncContext * const s = &h->s;
  7110. AVCodecContext * const avctx= s->avctx;
  7111. int buf_index=0;
  7112. #if 0
  7113. int i;
  7114. for(i=0; i<50; i++){
  7115. av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
  7116. }
  7117. #endif
  7118. h->slice_num = 0;
  7119. s->current_picture_ptr= NULL;
  7120. for(;;){
  7121. int consumed;
  7122. int dst_length;
  7123. int bit_length;
  7124. uint8_t *ptr;
  7125. int i, nalsize = 0;
  7126. if(h->is_avc) {
  7127. if(buf_index >= buf_size) break;
  7128. nalsize = 0;
  7129. for(i = 0; i < h->nal_length_size; i++)
  7130. nalsize = (nalsize << 8) | buf[buf_index++];
  7131. if(nalsize <= 1){
  7132. if(nalsize == 1){
  7133. buf_index++;
  7134. continue;
  7135. }else{
  7136. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  7137. break;
  7138. }
  7139. }
  7140. } else {
  7141. // start code prefix search
  7142. for(; buf_index + 3 < buf_size; buf_index++){
  7143. // this should allways succeed in the first iteration
  7144. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  7145. break;
  7146. }
  7147. if(buf_index+3 >= buf_size) break;
  7148. buf_index+=3;
  7149. }
  7150. ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, h->is_avc ? nalsize : buf_size - buf_index);
  7151. while(ptr[dst_length - 1] == 0 && dst_length > 1)
  7152. dst_length--;
  7153. bit_length= 8*dst_length - decode_rbsp_trailing(ptr + dst_length - 1);
  7154. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  7155. 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);
  7156. }
  7157. if (h->is_avc && (nalsize != consumed))
  7158. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  7159. buf_index += consumed;
  7160. if( (s->hurry_up == 1 && h->nal_ref_idc == 0) //FIXME dont discard SEI id
  7161. ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  7162. continue;
  7163. switch(h->nal_unit_type){
  7164. case NAL_IDR_SLICE:
  7165. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  7166. case NAL_SLICE:
  7167. init_get_bits(&s->gb, ptr, bit_length);
  7168. h->intra_gb_ptr=
  7169. h->inter_gb_ptr= &s->gb;
  7170. s->data_partitioning = 0;
  7171. if(decode_slice_header(h) < 0){
  7172. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  7173. break;
  7174. }
  7175. s->current_picture_ptr->key_frame= (h->nal_unit_type == NAL_IDR_SLICE);
  7176. if(h->redundant_pic_count==0 && s->hurry_up < 5
  7177. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  7178. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  7179. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  7180. && avctx->skip_frame < AVDISCARD_ALL)
  7181. decode_slice(h);
  7182. break;
  7183. case NAL_DPA:
  7184. init_get_bits(&s->gb, ptr, bit_length);
  7185. h->intra_gb_ptr=
  7186. h->inter_gb_ptr= NULL;
  7187. s->data_partitioning = 1;
  7188. if(decode_slice_header(h) < 0){
  7189. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  7190. }
  7191. break;
  7192. case NAL_DPB:
  7193. init_get_bits(&h->intra_gb, ptr, bit_length);
  7194. h->intra_gb_ptr= &h->intra_gb;
  7195. break;
  7196. case NAL_DPC:
  7197. init_get_bits(&h->inter_gb, ptr, bit_length);
  7198. h->inter_gb_ptr= &h->inter_gb;
  7199. if(h->redundant_pic_count==0 && h->intra_gb_ptr && s->data_partitioning
  7200. && s->hurry_up < 5
  7201. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  7202. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  7203. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  7204. && avctx->skip_frame < AVDISCARD_ALL)
  7205. decode_slice(h);
  7206. break;
  7207. case NAL_SEI:
  7208. init_get_bits(&s->gb, ptr, bit_length);
  7209. decode_sei(h);
  7210. break;
  7211. case NAL_SPS:
  7212. init_get_bits(&s->gb, ptr, bit_length);
  7213. decode_seq_parameter_set(h);
  7214. if(s->flags& CODEC_FLAG_LOW_DELAY)
  7215. s->low_delay=1;
  7216. if(avctx->has_b_frames < 2)
  7217. avctx->has_b_frames= !s->low_delay;
  7218. break;
  7219. case NAL_PPS:
  7220. init_get_bits(&s->gb, ptr, bit_length);
  7221. decode_picture_parameter_set(h, bit_length);
  7222. break;
  7223. case NAL_AUD:
  7224. case NAL_END_SEQUENCE:
  7225. case NAL_END_STREAM:
  7226. case NAL_FILLER_DATA:
  7227. case NAL_SPS_EXT:
  7228. case NAL_AUXILIARY_SLICE:
  7229. break;
  7230. default:
  7231. av_log(avctx, AV_LOG_ERROR, "Unknown NAL code: %d\n", h->nal_unit_type);
  7232. }
  7233. }
  7234. if(!s->current_picture_ptr) return buf_index; //no frame
  7235. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  7236. s->current_picture_ptr->pict_type= s->pict_type;
  7237. h->prev_frame_num_offset= h->frame_num_offset;
  7238. h->prev_frame_num= h->frame_num;
  7239. if(s->current_picture_ptr->reference){
  7240. h->prev_poc_msb= h->poc_msb;
  7241. h->prev_poc_lsb= h->poc_lsb;
  7242. }
  7243. if(s->current_picture_ptr->reference)
  7244. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  7245. ff_er_frame_end(s);
  7246. MPV_frame_end(s);
  7247. return buf_index;
  7248. }
  7249. /**
  7250. * returns the number of bytes consumed for building the current frame
  7251. */
  7252. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  7253. if(s->flags&CODEC_FLAG_TRUNCATED){
  7254. pos -= s->parse_context.last_index;
  7255. if(pos<0) pos=0; // FIXME remove (unneeded?)
  7256. return pos;
  7257. }else{
  7258. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  7259. if(pos+10>buf_size) pos=buf_size; // oops ;)
  7260. return pos;
  7261. }
  7262. }
  7263. static int decode_frame(AVCodecContext *avctx,
  7264. void *data, int *data_size,
  7265. uint8_t *buf, int buf_size)
  7266. {
  7267. H264Context *h = avctx->priv_data;
  7268. MpegEncContext *s = &h->s;
  7269. AVFrame *pict = data;
  7270. int buf_index;
  7271. s->flags= avctx->flags;
  7272. s->flags2= avctx->flags2;
  7273. /* no supplementary picture */
  7274. if (buf_size == 0) {
  7275. return 0;
  7276. }
  7277. if(s->flags&CODEC_FLAG_TRUNCATED){
  7278. int next= find_frame_end(h, buf, buf_size);
  7279. if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
  7280. return buf_size;
  7281. //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);
  7282. }
  7283. if(h->is_avc && !h->got_avcC) {
  7284. int i, cnt, nalsize;
  7285. unsigned char *p = avctx->extradata;
  7286. if(avctx->extradata_size < 7) {
  7287. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  7288. return -1;
  7289. }
  7290. if(*p != 1) {
  7291. av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
  7292. return -1;
  7293. }
  7294. /* sps and pps in the avcC always have length coded with 2 bytes,
  7295. so put a fake nal_length_size = 2 while parsing them */
  7296. h->nal_length_size = 2;
  7297. // Decode sps from avcC
  7298. cnt = *(p+5) & 0x1f; // Number of sps
  7299. p += 6;
  7300. for (i = 0; i < cnt; i++) {
  7301. nalsize = BE_16(p) + 2;
  7302. if(decode_nal_units(h, p, nalsize) < 0) {
  7303. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  7304. return -1;
  7305. }
  7306. p += nalsize;
  7307. }
  7308. // Decode pps from avcC
  7309. cnt = *(p++); // Number of pps
  7310. for (i = 0; i < cnt; i++) {
  7311. nalsize = BE_16(p) + 2;
  7312. if(decode_nal_units(h, p, nalsize) != nalsize) {
  7313. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  7314. return -1;
  7315. }
  7316. p += nalsize;
  7317. }
  7318. // Now store right nal length size, that will be use to parse all other nals
  7319. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  7320. // Do not reparse avcC
  7321. h->got_avcC = 1;
  7322. }
  7323. if(!h->is_avc && s->avctx->extradata_size && s->picture_number==0){
  7324. if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
  7325. return -1;
  7326. }
  7327. buf_index=decode_nal_units(h, buf, buf_size);
  7328. if(buf_index < 0)
  7329. return -1;
  7330. //FIXME do something with unavailable reference frames
  7331. // if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_index, buf_size);
  7332. if(!s->current_picture_ptr){
  7333. av_log(h->s.avctx, AV_LOG_DEBUG, "error, NO frame\n");
  7334. return -1;
  7335. }
  7336. {
  7337. Picture *out = s->current_picture_ptr;
  7338. #if 0 //decode order
  7339. *data_size = sizeof(AVFrame);
  7340. #else
  7341. /* Sort B-frames into display order */
  7342. Picture *cur = s->current_picture_ptr;
  7343. Picture *prev = h->delayed_output_pic;
  7344. int i, pics, cross_idr, out_of_order, out_idx;
  7345. if(h->sps.bitstream_restriction_flag
  7346. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  7347. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  7348. s->low_delay = 0;
  7349. }
  7350. pics = 0;
  7351. while(h->delayed_pic[pics]) pics++;
  7352. h->delayed_pic[pics++] = cur;
  7353. if(cur->reference == 0)
  7354. cur->reference = 1;
  7355. cross_idr = 0;
  7356. for(i=0; h->delayed_pic[i]; i++)
  7357. if(h->delayed_pic[i]->key_frame || h->delayed_pic[i]->poc==0)
  7358. cross_idr = 1;
  7359. out = h->delayed_pic[0];
  7360. out_idx = 0;
  7361. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
  7362. if(h->delayed_pic[i]->poc < out->poc){
  7363. out = h->delayed_pic[i];
  7364. out_idx = i;
  7365. }
  7366. out_of_order = !cross_idr && prev && out->poc < prev->poc;
  7367. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  7368. { }
  7369. else if(prev && pics <= s->avctx->has_b_frames)
  7370. out = prev;
  7371. else if((out_of_order && pics-1 == s->avctx->has_b_frames && pics < 15)
  7372. || (s->low_delay &&
  7373. ((!cross_idr && prev && out->poc > prev->poc + 2)
  7374. || cur->pict_type == B_TYPE)))
  7375. {
  7376. s->low_delay = 0;
  7377. s->avctx->has_b_frames++;
  7378. out = prev;
  7379. }
  7380. else if(out_of_order)
  7381. out = prev;
  7382. if(out_of_order || pics > s->avctx->has_b_frames){
  7383. for(i=out_idx; h->delayed_pic[i]; i++)
  7384. h->delayed_pic[i] = h->delayed_pic[i+1];
  7385. }
  7386. if(prev == out)
  7387. *data_size = 0;
  7388. else
  7389. *data_size = sizeof(AVFrame);
  7390. if(prev && prev != out && prev->reference == 1)
  7391. prev->reference = 0;
  7392. h->delayed_output_pic = out;
  7393. #endif
  7394. if(out)
  7395. *pict= *(AVFrame*)out;
  7396. else
  7397. av_log(avctx, AV_LOG_DEBUG, "no picture\n");
  7398. }
  7399. assert(pict->data[0] || !*data_size);
  7400. ff_print_debug_info(s, pict);
  7401. //printf("out %d\n", (int)pict->data[0]);
  7402. #if 0 //?
  7403. /* Return the Picture timestamp as the frame number */
  7404. /* we substract 1 because it is added on utils.c */
  7405. avctx->frame_number = s->picture_number - 1;
  7406. #endif
  7407. return get_consumed_bytes(s, buf_index, buf_size);
  7408. }
  7409. #if 0
  7410. static inline void fill_mb_avail(H264Context *h){
  7411. MpegEncContext * const s = &h->s;
  7412. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  7413. if(s->mb_y){
  7414. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  7415. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  7416. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  7417. }else{
  7418. h->mb_avail[0]=
  7419. h->mb_avail[1]=
  7420. h->mb_avail[2]= 0;
  7421. }
  7422. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  7423. h->mb_avail[4]= 1; //FIXME move out
  7424. h->mb_avail[5]= 0; //FIXME move out
  7425. }
  7426. #endif
  7427. #if 0 //selftest
  7428. #define COUNT 8000
  7429. #define SIZE (COUNT*40)
  7430. int main(){
  7431. int i;
  7432. uint8_t temp[SIZE];
  7433. PutBitContext pb;
  7434. GetBitContext gb;
  7435. // int int_temp[10000];
  7436. DSPContext dsp;
  7437. AVCodecContext avctx;
  7438. dsputil_init(&dsp, &avctx);
  7439. init_put_bits(&pb, temp, SIZE);
  7440. printf("testing unsigned exp golomb\n");
  7441. for(i=0; i<COUNT; i++){
  7442. START_TIMER
  7443. set_ue_golomb(&pb, i);
  7444. STOP_TIMER("set_ue_golomb");
  7445. }
  7446. flush_put_bits(&pb);
  7447. init_get_bits(&gb, temp, 8*SIZE);
  7448. for(i=0; i<COUNT; i++){
  7449. int j, s;
  7450. s= show_bits(&gb, 24);
  7451. START_TIMER
  7452. j= get_ue_golomb(&gb);
  7453. if(j != i){
  7454. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7455. // return -1;
  7456. }
  7457. STOP_TIMER("get_ue_golomb");
  7458. }
  7459. init_put_bits(&pb, temp, SIZE);
  7460. printf("testing signed exp golomb\n");
  7461. for(i=0; i<COUNT; i++){
  7462. START_TIMER
  7463. set_se_golomb(&pb, i - COUNT/2);
  7464. STOP_TIMER("set_se_golomb");
  7465. }
  7466. flush_put_bits(&pb);
  7467. init_get_bits(&gb, temp, 8*SIZE);
  7468. for(i=0; i<COUNT; i++){
  7469. int j, s;
  7470. s= show_bits(&gb, 24);
  7471. START_TIMER
  7472. j= get_se_golomb(&gb);
  7473. if(j != i - COUNT/2){
  7474. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7475. // return -1;
  7476. }
  7477. STOP_TIMER("get_se_golomb");
  7478. }
  7479. printf("testing 4x4 (I)DCT\n");
  7480. DCTELEM block[16];
  7481. uint8_t src[16], ref[16];
  7482. uint64_t error= 0, max_error=0;
  7483. for(i=0; i<COUNT; i++){
  7484. int j;
  7485. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  7486. for(j=0; j<16; j++){
  7487. ref[j]= random()%255;
  7488. src[j]= random()%255;
  7489. }
  7490. h264_diff_dct_c(block, src, ref, 4);
  7491. //normalize
  7492. for(j=0; j<16; j++){
  7493. // printf("%d ", block[j]);
  7494. block[j]= block[j]*4;
  7495. if(j&1) block[j]= (block[j]*4 + 2)/5;
  7496. if(j&4) block[j]= (block[j]*4 + 2)/5;
  7497. }
  7498. // printf("\n");
  7499. s->dsp.h264_idct_add(ref, block, 4);
  7500. /* for(j=0; j<16; j++){
  7501. printf("%d ", ref[j]);
  7502. }
  7503. printf("\n");*/
  7504. for(j=0; j<16; j++){
  7505. int diff= ABS(src[j] - ref[j]);
  7506. error+= diff*diff;
  7507. max_error= FFMAX(max_error, diff);
  7508. }
  7509. }
  7510. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  7511. #if 0
  7512. printf("testing quantizer\n");
  7513. for(qp=0; qp<52; qp++){
  7514. for(i=0; i<16; i++)
  7515. src1_block[i]= src2_block[i]= random()%255;
  7516. }
  7517. #endif
  7518. printf("Testing NAL layer\n");
  7519. uint8_t bitstream[COUNT];
  7520. uint8_t nal[COUNT*2];
  7521. H264Context h;
  7522. memset(&h, 0, sizeof(H264Context));
  7523. for(i=0; i<COUNT; i++){
  7524. int zeros= i;
  7525. int nal_length;
  7526. int consumed;
  7527. int out_length;
  7528. uint8_t *out;
  7529. int j;
  7530. for(j=0; j<COUNT; j++){
  7531. bitstream[j]= (random() % 255) + 1;
  7532. }
  7533. for(j=0; j<zeros; j++){
  7534. int pos= random() % COUNT;
  7535. while(bitstream[pos] == 0){
  7536. pos++;
  7537. pos %= COUNT;
  7538. }
  7539. bitstream[pos]=0;
  7540. }
  7541. START_TIMER
  7542. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  7543. if(nal_length<0){
  7544. printf("encoding failed\n");
  7545. return -1;
  7546. }
  7547. out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
  7548. STOP_TIMER("NAL")
  7549. if(out_length != COUNT){
  7550. printf("incorrect length %d %d\n", out_length, COUNT);
  7551. return -1;
  7552. }
  7553. if(consumed != nal_length){
  7554. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  7555. return -1;
  7556. }
  7557. if(memcmp(bitstream, out, COUNT)){
  7558. printf("missmatch\n");
  7559. return -1;
  7560. }
  7561. }
  7562. printf("Testing RBSP\n");
  7563. return 0;
  7564. }
  7565. #endif
  7566. static int decode_end(AVCodecContext *avctx)
  7567. {
  7568. H264Context *h = avctx->priv_data;
  7569. MpegEncContext *s = &h->s;
  7570. av_freep(&h->rbsp_buffer);
  7571. free_tables(h); //FIXME cleanup init stuff perhaps
  7572. MPV_common_end(s);
  7573. // memset(h, 0, sizeof(H264Context));
  7574. return 0;
  7575. }
  7576. AVCodec h264_decoder = {
  7577. "h264",
  7578. CODEC_TYPE_VIDEO,
  7579. CODEC_ID_H264,
  7580. sizeof(H264Context),
  7581. decode_init,
  7582. NULL,
  7583. decode_end,
  7584. decode_frame,
  7585. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  7586. .flush= flush_dpb,
  7587. };
  7588. #ifdef CONFIG_H264_PARSER
  7589. AVCodecParser h264_parser = {
  7590. { CODEC_ID_H264 },
  7591. sizeof(H264Context),
  7592. NULL,
  7593. h264_parse,
  7594. ff_parse_close,
  7595. h264_split,
  7596. };
  7597. #endif
  7598. #include "svq3.c"