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.

8656 lines
323KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. /**
  23. * @file h264.c
  24. * H.264 / AVC / MPEG4 part10 codec.
  25. * @author Michael Niedermayer <michaelni@gmx.at>
  26. */
  27. #include "common.h"
  28. #include "dsputil.h"
  29. #include "avcodec.h"
  30. #include "mpegvideo.h"
  31. #include "h264data.h"
  32. #include "golomb.h"
  33. #include "cabac.h"
  34. //#undef NDEBUG
  35. #include <assert.h>
  36. #define interlaced_dct interlaced_dct_is_a_bad_name
  37. #define mb_intra mb_intra_isnt_initalized_see_mb_type
  38. #define LUMA_DC_BLOCK_INDEX 25
  39. #define CHROMA_DC_BLOCK_INDEX 26
  40. #define CHROMA_DC_COEFF_TOKEN_VLC_BITS 8
  41. #define COEFF_TOKEN_VLC_BITS 8
  42. #define TOTAL_ZEROS_VLC_BITS 9
  43. #define CHROMA_DC_TOTAL_ZEROS_VLC_BITS 3
  44. #define RUN_VLC_BITS 3
  45. #define RUN7_VLC_BITS 6
  46. #define MAX_SPS_COUNT 32
  47. #define MAX_PPS_COUNT 256
  48. #define MAX_MMCO_COUNT 66
  49. /* Compiling in interlaced support reduces the speed
  50. * of progressive decoding by about 2%. */
  51. #define ALLOW_INTERLACE
  52. #ifdef ALLOW_INTERLACE
  53. #define MB_MBAFF h->mb_mbaff
  54. #define MB_FIELD h->mb_field_decoding_flag
  55. #define FRAME_MBAFF h->mb_aff_frame
  56. #else
  57. #define MB_MBAFF 0
  58. #define MB_FIELD 0
  59. #define FRAME_MBAFF 0
  60. #undef IS_INTERLACED
  61. #define IS_INTERLACED(mb_type) 0
  62. #endif
  63. /**
  64. * Sequence parameter set
  65. */
  66. typedef struct SPS{
  67. int profile_idc;
  68. int level_idc;
  69. int transform_bypass; ///< qpprime_y_zero_transform_bypass_flag
  70. int log2_max_frame_num; ///< log2_max_frame_num_minus4 + 4
  71. int poc_type; ///< pic_order_cnt_type
  72. int log2_max_poc_lsb; ///< log2_max_pic_order_cnt_lsb_minus4
  73. int delta_pic_order_always_zero_flag;
  74. int offset_for_non_ref_pic;
  75. int offset_for_top_to_bottom_field;
  76. int poc_cycle_length; ///< num_ref_frames_in_pic_order_cnt_cycle
  77. int ref_frame_count; ///< num_ref_frames
  78. int gaps_in_frame_num_allowed_flag;
  79. int mb_width; ///< frame_width_in_mbs_minus1 + 1
  80. int mb_height; ///< frame_height_in_mbs_minus1 + 1
  81. int frame_mbs_only_flag;
  82. int mb_aff; ///<mb_adaptive_frame_field_flag
  83. int direct_8x8_inference_flag;
  84. int crop; ///< frame_cropping_flag
  85. int crop_left; ///< frame_cropping_rect_left_offset
  86. int crop_right; ///< frame_cropping_rect_right_offset
  87. int crop_top; ///< frame_cropping_rect_top_offset
  88. int crop_bottom; ///< frame_cropping_rect_bottom_offset
  89. int vui_parameters_present_flag;
  90. AVRational sar;
  91. int timing_info_present_flag;
  92. uint32_t num_units_in_tick;
  93. uint32_t time_scale;
  94. int fixed_frame_rate_flag;
  95. short offset_for_ref_frame[256]; //FIXME dyn aloc?
  96. int bitstream_restriction_flag;
  97. int num_reorder_frames;
  98. int scaling_matrix_present;
  99. uint8_t scaling_matrix4[6][16];
  100. uint8_t scaling_matrix8[2][64];
  101. }SPS;
  102. /**
  103. * Picture parameter set
  104. */
  105. typedef struct PPS{
  106. int sps_id;
  107. int cabac; ///< entropy_coding_mode_flag
  108. int pic_order_present; ///< pic_order_present_flag
  109. int slice_group_count; ///< num_slice_groups_minus1 + 1
  110. int mb_slice_group_map_type;
  111. int ref_count[2]; ///< num_ref_idx_l0/1_active_minus1 + 1
  112. int weighted_pred; ///< weighted_pred_flag
  113. int weighted_bipred_idc;
  114. int init_qp; ///< pic_init_qp_minus26 + 26
  115. int init_qs; ///< pic_init_qs_minus26 + 26
  116. int chroma_qp_index_offset;
  117. int deblocking_filter_parameters_present; ///< deblocking_filter_parameters_present_flag
  118. int constrained_intra_pred; ///< constrained_intra_pred_flag
  119. int redundant_pic_cnt_present; ///< redundant_pic_cnt_present_flag
  120. int transform_8x8_mode; ///< transform_8x8_mode_flag
  121. uint8_t scaling_matrix4[6][16];
  122. uint8_t scaling_matrix8[2][64];
  123. }PPS;
  124. /**
  125. * Memory management control operation opcode.
  126. */
  127. typedef enum MMCOOpcode{
  128. MMCO_END=0,
  129. MMCO_SHORT2UNUSED,
  130. MMCO_LONG2UNUSED,
  131. MMCO_SHORT2LONG,
  132. MMCO_SET_MAX_LONG,
  133. MMCO_RESET,
  134. MMCO_LONG,
  135. } MMCOOpcode;
  136. /**
  137. * Memory management control operation.
  138. */
  139. typedef struct MMCO{
  140. MMCOOpcode opcode;
  141. int short_frame_num;
  142. int long_index;
  143. } MMCO;
  144. /**
  145. * H264Context
  146. */
  147. typedef struct H264Context{
  148. MpegEncContext s;
  149. int nal_ref_idc;
  150. int nal_unit_type;
  151. #define NAL_SLICE 1
  152. #define NAL_DPA 2
  153. #define NAL_DPB 3
  154. #define NAL_DPC 4
  155. #define NAL_IDR_SLICE 5
  156. #define NAL_SEI 6
  157. #define NAL_SPS 7
  158. #define NAL_PPS 8
  159. #define NAL_AUD 9
  160. #define NAL_END_SEQUENCE 10
  161. #define NAL_END_STREAM 11
  162. #define NAL_FILLER_DATA 12
  163. #define NAL_SPS_EXT 13
  164. #define NAL_AUXILIARY_SLICE 19
  165. uint8_t *rbsp_buffer;
  166. unsigned int rbsp_buffer_size;
  167. /**
  168. * Used to parse AVC variant of h264
  169. */
  170. int is_avc; ///< this flag is != 0 if codec is avc1
  171. int got_avcC; ///< flag used to parse avcC data only once
  172. int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
  173. int chroma_qp; //QPc
  174. int prev_mb_skipped;
  175. int next_mb_skipped;
  176. //prediction stuff
  177. int chroma_pred_mode;
  178. int intra16x16_pred_mode;
  179. int top_mb_xy;
  180. int left_mb_xy[2];
  181. int8_t intra4x4_pred_mode_cache[5*8];
  182. int8_t (*intra4x4_pred_mode)[8];
  183. void (*pred4x4 [9+3])(uint8_t *src, uint8_t *topright, int stride);//FIXME move to dsp?
  184. void (*pred8x8l [9+3])(uint8_t *src, int topleft, int topright, int stride);
  185. void (*pred8x8 [4+3])(uint8_t *src, int stride);
  186. void (*pred16x16[4+3])(uint8_t *src, int stride);
  187. unsigned int topleft_samples_available;
  188. unsigned int top_samples_available;
  189. unsigned int topright_samples_available;
  190. unsigned int left_samples_available;
  191. uint8_t (*top_borders[2])[16+2*8];
  192. uint8_t left_border[2*(17+2*9)];
  193. /**
  194. * non zero coeff count cache.
  195. * is 64 if not available.
  196. */
  197. DECLARE_ALIGNED_8(uint8_t, non_zero_count_cache[6*8]);
  198. uint8_t (*non_zero_count)[16];
  199. /**
  200. * Motion vector cache.
  201. */
  202. DECLARE_ALIGNED_8(int16_t, mv_cache[2][5*8][2]);
  203. DECLARE_ALIGNED_8(int8_t, ref_cache[2][5*8]);
  204. #define LIST_NOT_USED -1 //FIXME rename?
  205. #define PART_NOT_AVAILABLE -2
  206. /**
  207. * is 1 if the specific list MV&references are set to 0,0,-2.
  208. */
  209. int mv_cache_clean[2];
  210. /**
  211. * number of neighbors (top and/or left) that used 8x8 dct
  212. */
  213. int neighbor_transform_size;
  214. /**
  215. * block_offset[ 0..23] for frame macroblocks
  216. * block_offset[24..47] for field macroblocks
  217. */
  218. int block_offset[2*(16+8)];
  219. uint32_t *mb2b_xy; //FIXME are these 4 a good idea?
  220. uint32_t *mb2b8_xy;
  221. int b_stride; //FIXME use s->b4_stride
  222. int b8_stride;
  223. int mb_linesize; ///< may be equal to s->linesize or s->linesize*2, for mbaff
  224. int mb_uvlinesize;
  225. int emu_edge_width;
  226. int emu_edge_height;
  227. int halfpel_flag;
  228. int thirdpel_flag;
  229. int unknown_svq3_flag;
  230. int next_slice_index;
  231. SPS sps_buffer[MAX_SPS_COUNT];
  232. SPS sps; ///< current sps
  233. PPS pps_buffer[MAX_PPS_COUNT];
  234. /**
  235. * current pps
  236. */
  237. PPS pps; //FIXME move to Picture perhaps? (->no) do we need that?
  238. uint32_t dequant4_buffer[6][52][16];
  239. uint32_t dequant8_buffer[2][52][64];
  240. uint32_t (*dequant4_coeff[6])[16];
  241. uint32_t (*dequant8_coeff[2])[64];
  242. int dequant_coeff_pps; ///< reinit tables when pps changes
  243. int slice_num;
  244. uint8_t *slice_table_base;
  245. uint8_t *slice_table; ///< slice_table_base + 2*mb_stride + 1
  246. int slice_type;
  247. int slice_type_fixed;
  248. //interlacing specific flags
  249. int mb_aff_frame;
  250. int mb_field_decoding_flag;
  251. int mb_mbaff; ///< mb_aff_frame && mb_field_decoding_flag
  252. int sub_mb_type[4];
  253. //POC stuff
  254. int poc_lsb;
  255. int poc_msb;
  256. int delta_poc_bottom;
  257. int delta_poc[2];
  258. int frame_num;
  259. int prev_poc_msb; ///< poc_msb of the last reference pic for POC type 0
  260. int prev_poc_lsb; ///< poc_lsb of the last reference pic for POC type 0
  261. int frame_num_offset; ///< for POC type 2
  262. int prev_frame_num_offset; ///< for POC type 2
  263. int prev_frame_num; ///< frame_num of the last pic for POC type 1/2
  264. /**
  265. * frame_num for frames or 2*frame_num for field pics.
  266. */
  267. int curr_pic_num;
  268. /**
  269. * max_frame_num or 2*max_frame_num for field pics.
  270. */
  271. int max_pic_num;
  272. //Weighted pred stuff
  273. int use_weight;
  274. int use_weight_chroma;
  275. int luma_log2_weight_denom;
  276. int chroma_log2_weight_denom;
  277. int luma_weight[2][48];
  278. int luma_offset[2][48];
  279. int chroma_weight[2][48][2];
  280. int chroma_offset[2][48][2];
  281. int implicit_weight[48][48];
  282. //deblock
  283. int deblocking_filter; ///< disable_deblocking_filter_idc with 1<->0
  284. int slice_alpha_c0_offset;
  285. int slice_beta_offset;
  286. int redundant_pic_count;
  287. int direct_spatial_mv_pred;
  288. int dist_scale_factor[16];
  289. int dist_scale_factor_field[32];
  290. int map_col_to_list0[2][16];
  291. int map_col_to_list0_field[2][32];
  292. /**
  293. * num_ref_idx_l0/1_active_minus1 + 1
  294. */
  295. int ref_count[2]; ///< counts frames or fields, depending on current mb mode
  296. Picture *short_ref[32];
  297. Picture *long_ref[32];
  298. Picture default_ref_list[2][32];
  299. Picture ref_list[2][48]; ///< 0..15: frame refs, 16..47: mbaff field refs
  300. Picture *delayed_pic[16]; //FIXME size?
  301. Picture *delayed_output_pic;
  302. /**
  303. * memory management control operations buffer.
  304. */
  305. MMCO mmco[MAX_MMCO_COUNT];
  306. int mmco_index;
  307. int long_ref_count; ///< number of actual long term references
  308. int short_ref_count; ///< number of actual short term references
  309. //data partitioning
  310. GetBitContext intra_gb;
  311. GetBitContext inter_gb;
  312. GetBitContext *intra_gb_ptr;
  313. GetBitContext *inter_gb_ptr;
  314. DECLARE_ALIGNED_8(DCTELEM, mb[16*24]);
  315. /**
  316. * Cabac
  317. */
  318. CABACContext cabac;
  319. uint8_t cabac_state[460];
  320. int cabac_init_idc;
  321. /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
  322. uint16_t *cbp_table;
  323. int cbp;
  324. int top_cbp;
  325. int left_cbp;
  326. /* chroma_pred_mode for i4x4 or i16x16, else 0 */
  327. uint8_t *chroma_pred_mode_table;
  328. int last_qscale_diff;
  329. int16_t (*mvd_table[2])[2];
  330. DECLARE_ALIGNED_8(int16_t, mvd_cache[2][5*8][2]);
  331. uint8_t *direct_table;
  332. uint8_t direct_cache[5*8];
  333. uint8_t zigzag_scan[16];
  334. uint8_t zigzag_scan8x8[64];
  335. uint8_t zigzag_scan8x8_cavlc[64];
  336. uint8_t field_scan[16];
  337. uint8_t field_scan8x8[64];
  338. uint8_t field_scan8x8_cavlc[64];
  339. const uint8_t *zigzag_scan_q0;
  340. const uint8_t *zigzag_scan8x8_q0;
  341. const uint8_t *zigzag_scan8x8_cavlc_q0;
  342. const uint8_t *field_scan_q0;
  343. const uint8_t *field_scan8x8_q0;
  344. const uint8_t *field_scan8x8_cavlc_q0;
  345. int x264_build;
  346. }H264Context;
  347. static VLC coeff_token_vlc[4];
  348. static VLC chroma_dc_coeff_token_vlc;
  349. static VLC total_zeros_vlc[15];
  350. static VLC chroma_dc_total_zeros_vlc[3];
  351. static VLC run_vlc[6];
  352. static VLC run7_vlc;
  353. static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
  354. static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
  355. 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);
  356. 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);
  357. static always_inline uint32_t pack16to32(int a, int b){
  358. #ifdef WORDS_BIGENDIAN
  359. return (b&0xFFFF) + (a<<16);
  360. #else
  361. return (a&0xFFFF) + (b<<16);
  362. #endif
  363. }
  364. /**
  365. * fill a rectangle.
  366. * @param h height of the rectangle, should be a constant
  367. * @param w width of the rectangle, should be a constant
  368. * @param size the size of val (1 or 4), should be a constant
  369. */
  370. static always_inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){
  371. uint8_t *p= (uint8_t*)vp;
  372. assert(size==1 || size==4);
  373. assert(w<=4);
  374. w *= size;
  375. stride *= size;
  376. assert((((long)vp)&(FFMIN(w, STRIDE_ALIGN)-1)) == 0);
  377. assert((stride&(w-1))==0);
  378. if(w==2){
  379. const uint16_t v= size==4 ? val : val*0x0101;
  380. *(uint16_t*)(p + 0*stride)= v;
  381. if(h==1) return;
  382. *(uint16_t*)(p + 1*stride)= v;
  383. if(h==2) return;
  384. *(uint16_t*)(p + 2*stride)=
  385. *(uint16_t*)(p + 3*stride)= v;
  386. }else if(w==4){
  387. const uint32_t v= size==4 ? val : val*0x01010101;
  388. *(uint32_t*)(p + 0*stride)= v;
  389. if(h==1) return;
  390. *(uint32_t*)(p + 1*stride)= v;
  391. if(h==2) return;
  392. *(uint32_t*)(p + 2*stride)=
  393. *(uint32_t*)(p + 3*stride)= v;
  394. }else if(w==8){
  395. //gcc can't optimize 64bit math on x86_32
  396. #if defined(ARCH_X86_64) || (defined(MP_WORDSIZE) && MP_WORDSIZE >= 64)
  397. const uint64_t v= val*0x0100000001ULL;
  398. *(uint64_t*)(p + 0*stride)= v;
  399. if(h==1) return;
  400. *(uint64_t*)(p + 1*stride)= v;
  401. if(h==2) return;
  402. *(uint64_t*)(p + 2*stride)=
  403. *(uint64_t*)(p + 3*stride)= v;
  404. }else if(w==16){
  405. const uint64_t v= val*0x0100000001ULL;
  406. *(uint64_t*)(p + 0+0*stride)=
  407. *(uint64_t*)(p + 8+0*stride)=
  408. *(uint64_t*)(p + 0+1*stride)=
  409. *(uint64_t*)(p + 8+1*stride)= v;
  410. if(h==2) return;
  411. *(uint64_t*)(p + 0+2*stride)=
  412. *(uint64_t*)(p + 8+2*stride)=
  413. *(uint64_t*)(p + 0+3*stride)=
  414. *(uint64_t*)(p + 8+3*stride)= v;
  415. #else
  416. *(uint32_t*)(p + 0+0*stride)=
  417. *(uint32_t*)(p + 4+0*stride)= val;
  418. if(h==1) return;
  419. *(uint32_t*)(p + 0+1*stride)=
  420. *(uint32_t*)(p + 4+1*stride)= val;
  421. if(h==2) return;
  422. *(uint32_t*)(p + 0+2*stride)=
  423. *(uint32_t*)(p + 4+2*stride)=
  424. *(uint32_t*)(p + 0+3*stride)=
  425. *(uint32_t*)(p + 4+3*stride)= val;
  426. }else if(w==16){
  427. *(uint32_t*)(p + 0+0*stride)=
  428. *(uint32_t*)(p + 4+0*stride)=
  429. *(uint32_t*)(p + 8+0*stride)=
  430. *(uint32_t*)(p +12+0*stride)=
  431. *(uint32_t*)(p + 0+1*stride)=
  432. *(uint32_t*)(p + 4+1*stride)=
  433. *(uint32_t*)(p + 8+1*stride)=
  434. *(uint32_t*)(p +12+1*stride)= val;
  435. if(h==2) return;
  436. *(uint32_t*)(p + 0+2*stride)=
  437. *(uint32_t*)(p + 4+2*stride)=
  438. *(uint32_t*)(p + 8+2*stride)=
  439. *(uint32_t*)(p +12+2*stride)=
  440. *(uint32_t*)(p + 0+3*stride)=
  441. *(uint32_t*)(p + 4+3*stride)=
  442. *(uint32_t*)(p + 8+3*stride)=
  443. *(uint32_t*)(p +12+3*stride)= val;
  444. #endif
  445. }else
  446. assert(0);
  447. assert(h==4);
  448. }
  449. static void fill_caches(H264Context *h, int mb_type, int for_deblock){
  450. MpegEncContext * const s = &h->s;
  451. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  452. int topleft_xy, top_xy, topright_xy, left_xy[2];
  453. int topleft_type, top_type, topright_type, left_type[2];
  454. int left_block[8];
  455. int i;
  456. //FIXME deblocking could skip the intra and nnz parts.
  457. if(for_deblock && (h->slice_num == 1 || h->slice_table[mb_xy] == h->slice_table[mb_xy-s->mb_stride]) && !FRAME_MBAFF)
  458. return;
  459. //wow what a mess, why didn't they simplify the interlacing&intra stuff, i can't imagine that these complex rules are worth it
  460. top_xy = mb_xy - s->mb_stride;
  461. topleft_xy = top_xy - 1;
  462. topright_xy= top_xy + 1;
  463. left_xy[1] = left_xy[0] = mb_xy-1;
  464. left_block[0]= 0;
  465. left_block[1]= 1;
  466. left_block[2]= 2;
  467. left_block[3]= 3;
  468. left_block[4]= 7;
  469. left_block[5]= 10;
  470. left_block[6]= 8;
  471. left_block[7]= 11;
  472. if(FRAME_MBAFF){
  473. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  474. const int top_pair_xy = pair_xy - s->mb_stride;
  475. const int topleft_pair_xy = top_pair_xy - 1;
  476. const int topright_pair_xy = top_pair_xy + 1;
  477. const int topleft_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
  478. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  479. const int topright_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
  480. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  481. const int curr_mb_frame_flag = !IS_INTERLACED(mb_type);
  482. const int bottom = (s->mb_y & 1);
  483. 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);
  484. if (bottom
  485. ? !curr_mb_frame_flag // bottom macroblock
  486. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  487. ) {
  488. top_xy -= s->mb_stride;
  489. }
  490. if (bottom
  491. ? !curr_mb_frame_flag // bottom macroblock
  492. : (!curr_mb_frame_flag && !topleft_mb_frame_flag) // top macroblock
  493. ) {
  494. topleft_xy -= s->mb_stride;
  495. }
  496. if (bottom
  497. ? !curr_mb_frame_flag // bottom macroblock
  498. : (!curr_mb_frame_flag && !topright_mb_frame_flag) // top macroblock
  499. ) {
  500. topright_xy -= s->mb_stride;
  501. }
  502. if (left_mb_frame_flag != curr_mb_frame_flag) {
  503. left_xy[1] = left_xy[0] = pair_xy - 1;
  504. if (curr_mb_frame_flag) {
  505. if (bottom) {
  506. left_block[0]= 2;
  507. left_block[1]= 2;
  508. left_block[2]= 3;
  509. left_block[3]= 3;
  510. left_block[4]= 8;
  511. left_block[5]= 11;
  512. left_block[6]= 8;
  513. left_block[7]= 11;
  514. } else {
  515. left_block[0]= 0;
  516. left_block[1]= 0;
  517. left_block[2]= 1;
  518. left_block[3]= 1;
  519. left_block[4]= 7;
  520. left_block[5]= 10;
  521. left_block[6]= 7;
  522. left_block[7]= 10;
  523. }
  524. } else {
  525. left_xy[1] += s->mb_stride;
  526. //left_block[0]= 0;
  527. left_block[1]= 2;
  528. left_block[2]= 0;
  529. left_block[3]= 2;
  530. //left_block[4]= 7;
  531. left_block[5]= 10;
  532. left_block[6]= 7;
  533. left_block[7]= 10;
  534. }
  535. }
  536. }
  537. h->top_mb_xy = top_xy;
  538. h->left_mb_xy[0] = left_xy[0];
  539. h->left_mb_xy[1] = left_xy[1];
  540. if(for_deblock){
  541. topleft_type = 0;
  542. topright_type = 0;
  543. top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0;
  544. left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
  545. left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
  546. if(FRAME_MBAFF && !IS_INTRA(mb_type)){
  547. int list;
  548. int v = *(uint16_t*)&h->non_zero_count[mb_xy][14];
  549. for(i=0; i<16; i++)
  550. h->non_zero_count_cache[scan8[i]] = (v>>i)&1;
  551. for(list=0; list<1+(h->slice_type==B_TYPE); list++){
  552. if(USES_LIST(mb_type,list)){
  553. uint32_t *src = (uint32_t*)s->current_picture.motion_val[list][h->mb2b_xy[mb_xy]];
  554. uint32_t *dst = (uint32_t*)h->mv_cache[list][scan8[0]];
  555. int8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
  556. for(i=0; i<4; i++, dst+=8, src+=h->b_stride){
  557. dst[0] = src[0];
  558. dst[1] = src[1];
  559. dst[2] = src[2];
  560. dst[3] = src[3];
  561. }
  562. *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =
  563. *(uint32_t*)&h->ref_cache[list][scan8[ 2]] = pack16to32(ref[0],ref[1])*0x0101;
  564. ref += h->b8_stride;
  565. *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =
  566. *(uint32_t*)&h->ref_cache[list][scan8[10]] = pack16to32(ref[0],ref[1])*0x0101;
  567. }else{
  568. fill_rectangle(&h-> mv_cache[list][scan8[ 0]], 4, 4, 8, 0, 4);
  569. fill_rectangle(&h->ref_cache[list][scan8[ 0]], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  570. }
  571. }
  572. }
  573. }else{
  574. topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
  575. top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
  576. topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
  577. left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
  578. left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
  579. }
  580. if(IS_INTRA(mb_type)){
  581. h->topleft_samples_available=
  582. h->top_samples_available=
  583. h->left_samples_available= 0xFFFF;
  584. h->topright_samples_available= 0xEEEA;
  585. if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
  586. h->topleft_samples_available= 0xB3FF;
  587. h->top_samples_available= 0x33FF;
  588. h->topright_samples_available= 0x26EA;
  589. }
  590. for(i=0; i<2; i++){
  591. if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
  592. h->topleft_samples_available&= 0xDF5F;
  593. h->left_samples_available&= 0x5F5F;
  594. }
  595. }
  596. if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
  597. h->topleft_samples_available&= 0x7FFF;
  598. if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
  599. h->topright_samples_available&= 0xFBFF;
  600. if(IS_INTRA4x4(mb_type)){
  601. if(IS_INTRA4x4(top_type)){
  602. h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
  603. h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
  604. h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
  605. h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
  606. }else{
  607. int pred;
  608. if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred))
  609. pred= -1;
  610. else{
  611. pred= 2;
  612. }
  613. h->intra4x4_pred_mode_cache[4+8*0]=
  614. h->intra4x4_pred_mode_cache[5+8*0]=
  615. h->intra4x4_pred_mode_cache[6+8*0]=
  616. h->intra4x4_pred_mode_cache[7+8*0]= pred;
  617. }
  618. for(i=0; i<2; i++){
  619. if(IS_INTRA4x4(left_type[i])){
  620. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
  621. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
  622. }else{
  623. int pred;
  624. if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred))
  625. pred= -1;
  626. else{
  627. pred= 2;
  628. }
  629. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
  630. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
  631. }
  632. }
  633. }
  634. }
  635. /*
  636. 0 . T T. T T T T
  637. 1 L . .L . . . .
  638. 2 L . .L . . . .
  639. 3 . T TL . . . .
  640. 4 L . .L . . . .
  641. 5 L . .. . . . .
  642. */
  643. //FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec)
  644. if(top_type){
  645. h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
  646. h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
  647. h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
  648. h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
  649. h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
  650. h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
  651. h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
  652. h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
  653. }else{
  654. h->non_zero_count_cache[4+8*0]=
  655. h->non_zero_count_cache[5+8*0]=
  656. h->non_zero_count_cache[6+8*0]=
  657. h->non_zero_count_cache[7+8*0]=
  658. h->non_zero_count_cache[1+8*0]=
  659. h->non_zero_count_cache[2+8*0]=
  660. h->non_zero_count_cache[1+8*3]=
  661. h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  662. }
  663. for (i=0; i<2; i++) {
  664. if(left_type[i]){
  665. h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
  666. h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
  667. h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
  668. h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
  669. }else{
  670. h->non_zero_count_cache[3+8*1 + 2*8*i]=
  671. h->non_zero_count_cache[3+8*2 + 2*8*i]=
  672. h->non_zero_count_cache[0+8*1 + 8*i]=
  673. h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  674. }
  675. }
  676. if( h->pps.cabac ) {
  677. // top_cbp
  678. if(top_type) {
  679. h->top_cbp = h->cbp_table[top_xy];
  680. } else if(IS_INTRA(mb_type)) {
  681. h->top_cbp = 0x1C0;
  682. } else {
  683. h->top_cbp = 0;
  684. }
  685. // left_cbp
  686. if (left_type[0]) {
  687. h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
  688. } else if(IS_INTRA(mb_type)) {
  689. h->left_cbp = 0x1C0;
  690. } else {
  691. h->left_cbp = 0;
  692. }
  693. if (left_type[0]) {
  694. h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
  695. }
  696. if (left_type[1]) {
  697. h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
  698. }
  699. }
  700. #if 1
  701. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  702. int list;
  703. for(list=0; list<1+(h->slice_type==B_TYPE); list++){
  704. if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
  705. /*if(!h->mv_cache_clean[list]){
  706. memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
  707. memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
  708. h->mv_cache_clean[list]= 1;
  709. }*/
  710. continue;
  711. }
  712. h->mv_cache_clean[list]= 0;
  713. if(USES_LIST(top_type, list)){
  714. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  715. const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
  716. *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
  717. *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
  718. *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
  719. *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
  720. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  721. h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
  722. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  723. h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
  724. }else{
  725. *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
  726. *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
  727. *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
  728. *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
  729. *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
  730. }
  731. //FIXME unify cleanup or sth
  732. if(USES_LIST(left_type[0], list)){
  733. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  734. const int b8_xy= h->mb2b8_xy[left_xy[0]] + 1;
  735. *(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]];
  736. *(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]];
  737. h->ref_cache[list][scan8[0] - 1 + 0*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0]>>1)];
  738. h->ref_cache[list][scan8[0] - 1 + 1*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1]>>1)];
  739. }else{
  740. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 0*8]=
  741. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 1*8]= 0;
  742. h->ref_cache[list][scan8[0] - 1 + 0*8]=
  743. h->ref_cache[list][scan8[0] - 1 + 1*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  744. }
  745. if(USES_LIST(left_type[1], list)){
  746. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  747. const int b8_xy= h->mb2b8_xy[left_xy[1]] + 1;
  748. *(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]];
  749. *(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]];
  750. h->ref_cache[list][scan8[0] - 1 + 2*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[2]>>1)];
  751. h->ref_cache[list][scan8[0] - 1 + 3*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[3]>>1)];
  752. }else{
  753. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 2*8]=
  754. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 3*8]= 0;
  755. h->ref_cache[list][scan8[0] - 1 + 2*8]=
  756. h->ref_cache[list][scan8[0] - 1 + 3*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  757. assert((!left_type[0]) == (!left_type[1]));
  758. }
  759. if((for_deblock || (IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred)) && !FRAME_MBAFF)
  760. continue;
  761. if(USES_LIST(topleft_type, list)){
  762. const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
  763. const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + h->b8_stride;
  764. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  765. h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  766. }else{
  767. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
  768. h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  769. }
  770. if(USES_LIST(topright_type, list)){
  771. const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
  772. const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
  773. *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  774. h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  775. }else{
  776. *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
  777. h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  778. }
  779. if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)
  780. continue;
  781. h->ref_cache[list][scan8[5 ]+1] =
  782. h->ref_cache[list][scan8[7 ]+1] =
  783. h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewhere else)
  784. h->ref_cache[list][scan8[4 ]] =
  785. h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
  786. *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
  787. *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
  788. *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  789. *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
  790. *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
  791. if( h->pps.cabac ) {
  792. /* XXX beurk, Load mvd */
  793. if(USES_LIST(top_type, list)){
  794. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  795. *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
  796. *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
  797. *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
  798. *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
  799. }else{
  800. *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
  801. *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
  802. *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
  803. *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
  804. }
  805. if(USES_LIST(left_type[0], list)){
  806. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  807. *(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]];
  808. *(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]];
  809. }else{
  810. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
  811. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
  812. }
  813. if(USES_LIST(left_type[1], list)){
  814. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  815. *(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]];
  816. *(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]];
  817. }else{
  818. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
  819. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
  820. }
  821. *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
  822. *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
  823. *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  824. *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
  825. *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
  826. if(h->slice_type == B_TYPE){
  827. fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
  828. if(IS_DIRECT(top_type)){
  829. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
  830. }else if(IS_8X8(top_type)){
  831. int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
  832. h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
  833. h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
  834. }else{
  835. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
  836. }
  837. if(IS_DIRECT(left_type[0]))
  838. h->direct_cache[scan8[0] - 1 + 0*8]= 1;
  839. else if(IS_8X8(left_type[0]))
  840. 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)];
  841. else
  842. h->direct_cache[scan8[0] - 1 + 0*8]= 0;
  843. if(IS_DIRECT(left_type[1]))
  844. h->direct_cache[scan8[0] - 1 + 2*8]= 1;
  845. else if(IS_8X8(left_type[1]))
  846. 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)];
  847. else
  848. h->direct_cache[scan8[0] - 1 + 2*8]= 0;
  849. }
  850. }
  851. if(FRAME_MBAFF){
  852. #define MAP_MVS\
  853. MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
  854. MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
  855. MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
  856. MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
  857. MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
  858. MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
  859. MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\
  860. MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\
  861. MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\
  862. MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
  863. if(MB_FIELD){
  864. #define MAP_F2F(idx, mb_type)\
  865. if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  866. h->ref_cache[list][idx] <<= 1;\
  867. h->mv_cache[list][idx][1] /= 2;\
  868. h->mvd_cache[list][idx][1] /= 2;\
  869. }
  870. MAP_MVS
  871. #undef MAP_F2F
  872. }else{
  873. #define MAP_F2F(idx, mb_type)\
  874. if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  875. h->ref_cache[list][idx] >>= 1;\
  876. h->mv_cache[list][idx][1] <<= 1;\
  877. h->mvd_cache[list][idx][1] <<= 1;\
  878. }
  879. MAP_MVS
  880. #undef MAP_F2F
  881. }
  882. }
  883. }
  884. }
  885. #endif
  886. h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
  887. }
  888. static inline void write_back_intra_pred_mode(H264Context *h){
  889. MpegEncContext * const s = &h->s;
  890. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  891. h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
  892. h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
  893. h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
  894. h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
  895. h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
  896. h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
  897. h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
  898. }
  899. /**
  900. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  901. */
  902. static inline int check_intra4x4_pred_mode(H264Context *h){
  903. MpegEncContext * const s = &h->s;
  904. static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
  905. static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
  906. int i;
  907. if(!(h->top_samples_available&0x8000)){
  908. for(i=0; i<4; i++){
  909. int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
  910. if(status<0){
  911. 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);
  912. return -1;
  913. } else if(status){
  914. h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
  915. }
  916. }
  917. }
  918. if(!(h->left_samples_available&0x8000)){
  919. for(i=0; i<4; i++){
  920. int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
  921. if(status<0){
  922. 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);
  923. return -1;
  924. } else if(status){
  925. h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
  926. }
  927. }
  928. }
  929. return 0;
  930. } //FIXME cleanup like next
  931. /**
  932. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  933. */
  934. static inline int check_intra_pred_mode(H264Context *h, int mode){
  935. MpegEncContext * const s = &h->s;
  936. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  937. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  938. if(mode < 0 || mode > 6) {
  939. 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);
  940. return -1;
  941. }
  942. if(!(h->top_samples_available&0x8000)){
  943. mode= top[ mode ];
  944. if(mode<0){
  945. 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);
  946. return -1;
  947. }
  948. }
  949. if(!(h->left_samples_available&0x8000)){
  950. mode= left[ mode ];
  951. if(mode<0){
  952. 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);
  953. return -1;
  954. }
  955. }
  956. return mode;
  957. }
  958. /**
  959. * gets the predicted intra4x4 prediction mode.
  960. */
  961. static inline int pred_intra_mode(H264Context *h, int n){
  962. const int index8= scan8[n];
  963. const int left= h->intra4x4_pred_mode_cache[index8 - 1];
  964. const int top = h->intra4x4_pred_mode_cache[index8 - 8];
  965. const int min= FFMIN(left, top);
  966. tprintf("mode:%d %d min:%d\n", left ,top, min);
  967. if(min<0) return DC_PRED;
  968. else return min;
  969. }
  970. static inline void write_back_non_zero_count(H264Context *h){
  971. MpegEncContext * const s = &h->s;
  972. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  973. h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
  974. h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
  975. h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
  976. h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
  977. h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
  978. h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
  979. h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
  980. h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
  981. h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
  982. h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
  983. h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
  984. h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
  985. h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
  986. if(FRAME_MBAFF){
  987. // store all luma nnzs, for deblocking
  988. int v = 0, i;
  989. for(i=0; i<16; i++)
  990. v += (!!h->non_zero_count_cache[scan8[i]]) << i;
  991. *(uint16_t*)&h->non_zero_count[mb_xy][14] = v;
  992. }
  993. }
  994. /**
  995. * gets the predicted number of non zero coefficients.
  996. * @param n block index
  997. */
  998. static inline int pred_non_zero_count(H264Context *h, int n){
  999. const int index8= scan8[n];
  1000. const int left= h->non_zero_count_cache[index8 - 1];
  1001. const int top = h->non_zero_count_cache[index8 - 8];
  1002. int i= left + top;
  1003. if(i<64) i= (i+1)>>1;
  1004. tprintf("pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
  1005. return i&31;
  1006. }
  1007. static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
  1008. const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
  1009. /* there is no consistent mapping of mvs to neighboring locations that will
  1010. * make mbaff happy, so we can't move all this logic to fill_caches */
  1011. if(FRAME_MBAFF){
  1012. MpegEncContext *s = &h->s;
  1013. const uint32_t *mb_types = s->current_picture_ptr->mb_type;
  1014. const int16_t *mv;
  1015. *(uint32_t*)h->mv_cache[list][scan8[0]-2] = 0;
  1016. *C = h->mv_cache[list][scan8[0]-2];
  1017. if(!MB_FIELD
  1018. && (s->mb_y&1) && i < scan8[0]+8 && topright_ref != PART_NOT_AVAILABLE){
  1019. int topright_xy = s->mb_x + (s->mb_y-1)*s->mb_stride + (i == scan8[0]+3);
  1020. if(IS_INTERLACED(mb_types[topright_xy])){
  1021. #define SET_DIAG_MV(MV_OP, REF_OP, X4, Y4)\
  1022. const int x4 = X4, y4 = Y4;\
  1023. const int mb_type = mb_types[(x4>>2)+(y4>>2)*s->mb_stride];\
  1024. if(!USES_LIST(mb_type,list) && !IS_8X8(mb_type))\
  1025. return LIST_NOT_USED;\
  1026. mv = s->current_picture_ptr->motion_val[list][x4 + y4*h->b_stride];\
  1027. h->mv_cache[list][scan8[0]-2][0] = mv[0];\
  1028. h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
  1029. return s->current_picture_ptr->ref_index[list][(x4>>1) + (y4>>1)*h->b8_stride] REF_OP;
  1030. SET_DIAG_MV(*2, >>1, s->mb_x*4+(i&7)-4+part_width, s->mb_y*4-1);
  1031. }
  1032. }
  1033. if(topright_ref == PART_NOT_AVAILABLE
  1034. && ((s->mb_y&1) || i >= scan8[0]+8) && (i&7)==4
  1035. && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
  1036. if(!MB_FIELD
  1037. && IS_INTERLACED(mb_types[h->left_mb_xy[0]])){
  1038. SET_DIAG_MV(*2, >>1, s->mb_x*4-1, (s->mb_y|1)*4+(s->mb_y&1)*2+(i>>4)-1);
  1039. }
  1040. if(MB_FIELD
  1041. && !IS_INTERLACED(mb_types[h->left_mb_xy[0]])
  1042. && i >= scan8[0]+8){
  1043. // leftshift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's ok.
  1044. SET_DIAG_MV(>>1, <<1, s->mb_x*4-1, (s->mb_y&~1)*4 - 1 + ((i-scan8[0])>>3)*2);
  1045. }
  1046. }
  1047. #undef SET_DIAG_MV
  1048. }
  1049. if(topright_ref != PART_NOT_AVAILABLE){
  1050. *C= h->mv_cache[list][ i - 8 + part_width ];
  1051. return topright_ref;
  1052. }else{
  1053. tprintf("topright MV not available\n");
  1054. *C= h->mv_cache[list][ i - 8 - 1 ];
  1055. return h->ref_cache[list][ i - 8 - 1 ];
  1056. }
  1057. }
  1058. /**
  1059. * gets the predicted MV.
  1060. * @param n the block index
  1061. * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
  1062. * @param mx the x component of the predicted motion vector
  1063. * @param my the y component of the predicted motion vector
  1064. */
  1065. static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
  1066. const int index8= scan8[n];
  1067. const int top_ref= h->ref_cache[list][ index8 - 8 ];
  1068. const int left_ref= h->ref_cache[list][ index8 - 1 ];
  1069. const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
  1070. const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
  1071. const int16_t * C;
  1072. int diagonal_ref, match_count;
  1073. assert(part_width==1 || part_width==2 || part_width==4);
  1074. /* mv_cache
  1075. B . . A T T T T
  1076. U . . L . . , .
  1077. U . . L . . . .
  1078. U . . L . . , .
  1079. . . . L . . . .
  1080. */
  1081. diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
  1082. match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
  1083. tprintf("pred_motion match_count=%d\n", match_count);
  1084. if(match_count > 1){ //most common
  1085. *mx= mid_pred(A[0], B[0], C[0]);
  1086. *my= mid_pred(A[1], B[1], C[1]);
  1087. }else if(match_count==1){
  1088. if(left_ref==ref){
  1089. *mx= A[0];
  1090. *my= A[1];
  1091. }else if(top_ref==ref){
  1092. *mx= B[0];
  1093. *my= B[1];
  1094. }else{
  1095. *mx= C[0];
  1096. *my= C[1];
  1097. }
  1098. }else{
  1099. if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
  1100. *mx= A[0];
  1101. *my= A[1];
  1102. }else{
  1103. *mx= mid_pred(A[0], B[0], C[0]);
  1104. *my= mid_pred(A[1], B[1], C[1]);
  1105. }
  1106. }
  1107. 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);
  1108. }
  1109. /**
  1110. * gets the directionally predicted 16x8 MV.
  1111. * @param n the block index
  1112. * @param mx the x component of the predicted motion vector
  1113. * @param my the y component of the predicted motion vector
  1114. */
  1115. static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  1116. if(n==0){
  1117. const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
  1118. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  1119. 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);
  1120. if(top_ref == ref){
  1121. *mx= B[0];
  1122. *my= B[1];
  1123. return;
  1124. }
  1125. }else{
  1126. const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
  1127. const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
  1128. 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);
  1129. if(left_ref == ref){
  1130. *mx= A[0];
  1131. *my= A[1];
  1132. return;
  1133. }
  1134. }
  1135. //RARE
  1136. pred_motion(h, n, 4, list, ref, mx, my);
  1137. }
  1138. /**
  1139. * gets the directionally predicted 8x16 MV.
  1140. * @param n the block index
  1141. * @param mx the x component of the predicted motion vector
  1142. * @param my the y component of the predicted motion vector
  1143. */
  1144. static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  1145. if(n==0){
  1146. const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
  1147. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  1148. 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);
  1149. if(left_ref == ref){
  1150. *mx= A[0];
  1151. *my= A[1];
  1152. return;
  1153. }
  1154. }else{
  1155. const int16_t * C;
  1156. int diagonal_ref;
  1157. diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
  1158. 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);
  1159. if(diagonal_ref == ref){
  1160. *mx= C[0];
  1161. *my= C[1];
  1162. return;
  1163. }
  1164. }
  1165. //RARE
  1166. pred_motion(h, n, 2, list, ref, mx, my);
  1167. }
  1168. static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
  1169. const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
  1170. const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
  1171. tprintf("pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
  1172. if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
  1173. || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
  1174. || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
  1175. *mx = *my = 0;
  1176. return;
  1177. }
  1178. pred_motion(h, 0, 4, 0, 0, mx, my);
  1179. return;
  1180. }
  1181. static inline void direct_dist_scale_factor(H264Context * const h){
  1182. const int poc = h->s.current_picture_ptr->poc;
  1183. const int poc1 = h->ref_list[1][0].poc;
  1184. int i;
  1185. for(i=0; i<h->ref_count[0]; i++){
  1186. int poc0 = h->ref_list[0][i].poc;
  1187. int td = clip(poc1 - poc0, -128, 127);
  1188. if(td == 0 /* FIXME || pic0 is a long-term ref */){
  1189. h->dist_scale_factor[i] = 256;
  1190. }else{
  1191. int tb = clip(poc - poc0, -128, 127);
  1192. int tx = (16384 + (FFABS(td) >> 1)) / td;
  1193. h->dist_scale_factor[i] = clip((tb*tx + 32) >> 6, -1024, 1023);
  1194. }
  1195. }
  1196. if(FRAME_MBAFF){
  1197. for(i=0; i<h->ref_count[0]; i++){
  1198. h->dist_scale_factor_field[2*i] =
  1199. h->dist_scale_factor_field[2*i+1] = h->dist_scale_factor[i];
  1200. }
  1201. }
  1202. }
  1203. static inline void direct_ref_list_init(H264Context * const h){
  1204. MpegEncContext * const s = &h->s;
  1205. Picture * const ref1 = &h->ref_list[1][0];
  1206. Picture * const cur = s->current_picture_ptr;
  1207. int list, i, j;
  1208. if(cur->pict_type == I_TYPE)
  1209. cur->ref_count[0] = 0;
  1210. if(cur->pict_type != B_TYPE)
  1211. cur->ref_count[1] = 0;
  1212. for(list=0; list<2; list++){
  1213. cur->ref_count[list] = h->ref_count[list];
  1214. for(j=0; j<h->ref_count[list]; j++)
  1215. cur->ref_poc[list][j] = h->ref_list[list][j].poc;
  1216. }
  1217. if(cur->pict_type != B_TYPE || h->direct_spatial_mv_pred)
  1218. return;
  1219. for(list=0; list<2; list++){
  1220. for(i=0; i<ref1->ref_count[list]; i++){
  1221. const int poc = ref1->ref_poc[list][i];
  1222. h->map_col_to_list0[list][i] = 0; /* bogus; fills in for missing frames */
  1223. for(j=0; j<h->ref_count[list]; j++)
  1224. if(h->ref_list[list][j].poc == poc){
  1225. h->map_col_to_list0[list][i] = j;
  1226. break;
  1227. }
  1228. }
  1229. }
  1230. if(FRAME_MBAFF){
  1231. for(list=0; list<2; list++){
  1232. for(i=0; i<ref1->ref_count[list]; i++){
  1233. j = h->map_col_to_list0[list][i];
  1234. h->map_col_to_list0_field[list][2*i] = 2*j;
  1235. h->map_col_to_list0_field[list][2*i+1] = 2*j+1;
  1236. }
  1237. }
  1238. }
  1239. }
  1240. static inline void pred_direct_motion(H264Context * const h, int *mb_type){
  1241. MpegEncContext * const s = &h->s;
  1242. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  1243. const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1244. const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1245. const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
  1246. const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
  1247. const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[1][b4_xy];
  1248. const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
  1249. const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy];
  1250. const int is_b8x8 = IS_8X8(*mb_type);
  1251. int sub_mb_type;
  1252. int i8, i4;
  1253. #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
  1254. if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
  1255. /* FIXME save sub mb types from previous frames (or derive from MVs)
  1256. * so we know exactly what block size to use */
  1257. sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  1258. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  1259. }else if(!is_b8x8 && (mb_type_col & MB_TYPE_16x16_OR_INTRA)){
  1260. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  1261. *mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
  1262. }else{
  1263. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  1264. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  1265. }
  1266. if(!is_b8x8)
  1267. *mb_type |= MB_TYPE_DIRECT2;
  1268. if(MB_FIELD)
  1269. *mb_type |= MB_TYPE_INTERLACED;
  1270. 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);
  1271. if(h->direct_spatial_mv_pred){
  1272. int ref[2];
  1273. int mv[2][2];
  1274. int list;
  1275. /* FIXME interlacing + spatial direct uses wrong colocated block positions */
  1276. /* ref = min(neighbors) */
  1277. for(list=0; list<2; list++){
  1278. int refa = h->ref_cache[list][scan8[0] - 1];
  1279. int refb = h->ref_cache[list][scan8[0] - 8];
  1280. int refc = h->ref_cache[list][scan8[0] - 8 + 4];
  1281. if(refc == -2)
  1282. refc = h->ref_cache[list][scan8[0] - 8 - 1];
  1283. ref[list] = refa;
  1284. if(ref[list] < 0 || (refb < ref[list] && refb >= 0))
  1285. ref[list] = refb;
  1286. if(ref[list] < 0 || (refc < ref[list] && refc >= 0))
  1287. ref[list] = refc;
  1288. if(ref[list] < 0)
  1289. ref[list] = -1;
  1290. }
  1291. if(ref[0] < 0 && ref[1] < 0){
  1292. ref[0] = ref[1] = 0;
  1293. mv[0][0] = mv[0][1] =
  1294. mv[1][0] = mv[1][1] = 0;
  1295. }else{
  1296. for(list=0; list<2; list++){
  1297. if(ref[list] >= 0)
  1298. pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
  1299. else
  1300. mv[list][0] = mv[list][1] = 0;
  1301. }
  1302. }
  1303. if(ref[1] < 0){
  1304. *mb_type &= ~MB_TYPE_P0L1;
  1305. sub_mb_type &= ~MB_TYPE_P0L1;
  1306. }else if(ref[0] < 0){
  1307. *mb_type &= ~MB_TYPE_P0L0;
  1308. sub_mb_type &= ~MB_TYPE_P0L0;
  1309. }
  1310. if(IS_16X16(*mb_type)){
  1311. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  1312. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  1313. if(!IS_INTRA(mb_type_col)
  1314. && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
  1315. || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
  1316. && (h->x264_build>33 || !h->x264_build)))){
  1317. if(ref[0] > 0)
  1318. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1319. else
  1320. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  1321. if(ref[1] > 0)
  1322. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1323. else
  1324. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  1325. }else{
  1326. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1327. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1328. }
  1329. }else{
  1330. for(i8=0; i8<4; i8++){
  1331. const int x8 = i8&1;
  1332. const int y8 = i8>>1;
  1333. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1334. continue;
  1335. h->sub_mb_type[i8] = sub_mb_type;
  1336. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1337. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1338. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  1339. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  1340. /* col_zero_flag */
  1341. if(!IS_INTRA(mb_type_col) && ( l1ref0[x8 + y8*h->b8_stride] == 0
  1342. || (l1ref0[x8 + y8*h->b8_stride] < 0 && l1ref1[x8 + y8*h->b8_stride] == 0
  1343. && (h->x264_build>33 || !h->x264_build)))){
  1344. const int16_t (*l1mv)[2]= l1ref0[x8 + y8*h->b8_stride] == 0 ? l1mv0 : l1mv1;
  1345. if(IS_SUB_8X8(sub_mb_type)){
  1346. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1347. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  1348. if(ref[0] == 0)
  1349. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1350. if(ref[1] == 0)
  1351. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1352. }
  1353. }else
  1354. for(i4=0; i4<4; i4++){
  1355. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1356. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  1357. if(ref[0] == 0)
  1358. *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
  1359. if(ref[1] == 0)
  1360. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
  1361. }
  1362. }
  1363. }
  1364. }
  1365. }
  1366. }else{ /* direct temporal mv pred */
  1367. const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
  1368. const int *dist_scale_factor = h->dist_scale_factor;
  1369. if(FRAME_MBAFF){
  1370. if(IS_INTERLACED(*mb_type)){
  1371. map_col_to_list0[0] = h->map_col_to_list0_field[0];
  1372. map_col_to_list0[1] = h->map_col_to_list0_field[1];
  1373. dist_scale_factor = h->dist_scale_factor_field;
  1374. }
  1375. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col)){
  1376. /* FIXME assumes direct_8x8_inference == 1 */
  1377. const int pair_xy = s->mb_x + (s->mb_y&~1)*s->mb_stride;
  1378. int mb_types_col[2];
  1379. int y_shift;
  1380. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1
  1381. | (is_b8x8 ? 0 : MB_TYPE_DIRECT2)
  1382. | (*mb_type & MB_TYPE_INTERLACED);
  1383. sub_mb_type = MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_16x16;
  1384. if(IS_INTERLACED(*mb_type)){
  1385. /* frame to field scaling */
  1386. mb_types_col[0] = h->ref_list[1][0].mb_type[pair_xy];
  1387. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1388. if(s->mb_y&1){
  1389. l1ref0 -= 2*h->b8_stride;
  1390. l1ref1 -= 2*h->b8_stride;
  1391. l1mv0 -= 4*h->b_stride;
  1392. l1mv1 -= 4*h->b_stride;
  1393. }
  1394. y_shift = 0;
  1395. if( (mb_types_col[0] & MB_TYPE_16x16_OR_INTRA)
  1396. && (mb_types_col[1] & MB_TYPE_16x16_OR_INTRA)
  1397. && !is_b8x8)
  1398. *mb_type |= MB_TYPE_16x8;
  1399. else
  1400. *mb_type |= MB_TYPE_8x8;
  1401. }else{
  1402. /* field to frame scaling */
  1403. /* col_mb_y = (mb_y&~1) + (topAbsDiffPOC < bottomAbsDiffPOC ? 0 : 1)
  1404. * but in MBAFF, top and bottom POC are equal */
  1405. int dy = (s->mb_y&1) ? 1 : 2;
  1406. mb_types_col[0] =
  1407. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1408. l1ref0 += dy*h->b8_stride;
  1409. l1ref1 += dy*h->b8_stride;
  1410. l1mv0 += 2*dy*h->b_stride;
  1411. l1mv1 += 2*dy*h->b_stride;
  1412. y_shift = 2;
  1413. if((mb_types_col[0] & (MB_TYPE_16x16_OR_INTRA|MB_TYPE_16x8))
  1414. && !is_b8x8)
  1415. *mb_type |= MB_TYPE_16x16;
  1416. else
  1417. *mb_type |= MB_TYPE_8x8;
  1418. }
  1419. for(i8=0; i8<4; i8++){
  1420. const int x8 = i8&1;
  1421. const int y8 = i8>>1;
  1422. int ref0, scale;
  1423. const int16_t (*l1mv)[2]= l1mv0;
  1424. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1425. continue;
  1426. h->sub_mb_type[i8] = sub_mb_type;
  1427. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1428. if(IS_INTRA(mb_types_col[y8])){
  1429. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1430. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1431. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1432. continue;
  1433. }
  1434. ref0 = l1ref0[x8 + (y8*2>>y_shift)*h->b8_stride];
  1435. if(ref0 >= 0)
  1436. ref0 = map_col_to_list0[0][ref0*2>>y_shift];
  1437. else{
  1438. ref0 = map_col_to_list0[1][l1ref1[x8 + (y8*2>>y_shift)*h->b8_stride]*2>>y_shift];
  1439. l1mv= l1mv1;
  1440. }
  1441. scale = dist_scale_factor[ref0];
  1442. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1443. {
  1444. const int16_t *mv_col = l1mv[x8*3 + (y8*6>>y_shift)*h->b_stride];
  1445. int my_col = (mv_col[1]<<y_shift)/2;
  1446. int mx = (scale * mv_col[0] + 128) >> 8;
  1447. int my = (scale * my_col + 128) >> 8;
  1448. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1449. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
  1450. }
  1451. }
  1452. return;
  1453. }
  1454. }
  1455. /* one-to-one mv scaling */
  1456. if(IS_16X16(*mb_type)){
  1457. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  1458. if(IS_INTRA(mb_type_col)){
  1459. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  1460. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  1461. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  1462. }else{
  1463. const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0]]
  1464. : map_col_to_list0[1][l1ref1[0]];
  1465. const int scale = dist_scale_factor[ref0];
  1466. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  1467. int mv_l0[2];
  1468. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1469. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1470. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref0, 1);
  1471. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0],mv_l0[1]), 4);
  1472. 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);
  1473. }
  1474. }else{
  1475. for(i8=0; i8<4; i8++){
  1476. const int x8 = i8&1;
  1477. const int y8 = i8>>1;
  1478. int ref0, scale;
  1479. const int16_t (*l1mv)[2]= l1mv0;
  1480. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1481. continue;
  1482. h->sub_mb_type[i8] = sub_mb_type;
  1483. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1484. if(IS_INTRA(mb_type_col)){
  1485. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1486. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1487. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1488. continue;
  1489. }
  1490. ref0 = l1ref0[x8 + y8*h->b8_stride];
  1491. if(ref0 >= 0)
  1492. ref0 = map_col_to_list0[0][ref0];
  1493. else{
  1494. ref0 = map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]];
  1495. l1mv= l1mv1;
  1496. }
  1497. scale = dist_scale_factor[ref0];
  1498. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1499. if(IS_SUB_8X8(sub_mb_type)){
  1500. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1501. int mx = (scale * mv_col[0] + 128) >> 8;
  1502. int my = (scale * mv_col[1] + 128) >> 8;
  1503. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1504. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
  1505. }else
  1506. for(i4=0; i4<4; i4++){
  1507. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1508. int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
  1509. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1510. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1511. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
  1512. pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  1513. }
  1514. }
  1515. }
  1516. }
  1517. }
  1518. static inline void write_back_motion(H264Context *h, int mb_type){
  1519. MpegEncContext * const s = &h->s;
  1520. const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1521. const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1522. int list;
  1523. if(!USES_LIST(mb_type, 0))
  1524. fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, h->b8_stride, (uint8_t)LIST_NOT_USED, 1);
  1525. for(list=0; list<2; list++){
  1526. int y;
  1527. if(!USES_LIST(mb_type, list))
  1528. continue;
  1529. for(y=0; y<4; y++){
  1530. *(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];
  1531. *(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];
  1532. }
  1533. if( h->pps.cabac ) {
  1534. if(IS_SKIP(mb_type))
  1535. fill_rectangle(h->mvd_table[list][b_xy], 4, 4, h->b_stride, 0, 4);
  1536. else
  1537. for(y=0; y<4; y++){
  1538. *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
  1539. *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
  1540. }
  1541. }
  1542. {
  1543. int8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
  1544. ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]];
  1545. ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]];
  1546. ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]];
  1547. ref_index[1+1*h->b8_stride]= h->ref_cache[list][scan8[12]];
  1548. }
  1549. }
  1550. if(h->slice_type == B_TYPE && h->pps.cabac){
  1551. if(IS_8X8(mb_type)){
  1552. uint8_t *direct_table = &h->direct_table[b8_xy];
  1553. direct_table[1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
  1554. direct_table[0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
  1555. direct_table[1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
  1556. }
  1557. }
  1558. }
  1559. /**
  1560. * Decodes a network abstraction layer unit.
  1561. * @param consumed is the number of bytes used as input
  1562. * @param length is the length of the array
  1563. * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp tailing?
  1564. * @returns decoded bytes, might be src+1 if no escapes
  1565. */
  1566. static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){
  1567. int i, si, di;
  1568. uint8_t *dst;
  1569. // src[0]&0x80; //forbidden bit
  1570. h->nal_ref_idc= src[0]>>5;
  1571. h->nal_unit_type= src[0]&0x1F;
  1572. src++; length--;
  1573. #if 0
  1574. for(i=0; i<length; i++)
  1575. printf("%2X ", src[i]);
  1576. #endif
  1577. for(i=0; i+1<length; i+=2){
  1578. if(src[i]) continue;
  1579. if(i>0 && src[i-1]==0) i--;
  1580. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  1581. if(src[i+2]!=3){
  1582. /* startcode, so we must be past the end */
  1583. length=i;
  1584. }
  1585. break;
  1586. }
  1587. }
  1588. if(i>=length-1){ //no escaped 0
  1589. *dst_length= length;
  1590. *consumed= length+1; //+1 for the header
  1591. return src;
  1592. }
  1593. h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length);
  1594. dst= h->rbsp_buffer;
  1595. //printf("decoding esc\n");
  1596. si=di=0;
  1597. while(si<length){
  1598. //remove escapes (very rare 1:2^22)
  1599. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  1600. if(src[si+2]==3){ //escape
  1601. dst[di++]= 0;
  1602. dst[di++]= 0;
  1603. si+=3;
  1604. continue;
  1605. }else //next start code
  1606. break;
  1607. }
  1608. dst[di++]= src[si++];
  1609. }
  1610. *dst_length= di;
  1611. *consumed= si + 1;//+1 for the header
  1612. //FIXME store exact number of bits in the getbitcontext (its needed for decoding)
  1613. return dst;
  1614. }
  1615. #if 0
  1616. /**
  1617. * @param src the data which should be escaped
  1618. * @param dst the target buffer, dst+1 == src is allowed as a special case
  1619. * @param length the length of the src data
  1620. * @param dst_length the length of the dst array
  1621. * @returns length of escaped data in bytes or -1 if an error occured
  1622. */
  1623. static int encode_nal(H264Context *h, uint8_t *dst, uint8_t *src, int length, int dst_length){
  1624. int i, escape_count, si, di;
  1625. uint8_t *temp;
  1626. assert(length>=0);
  1627. assert(dst_length>0);
  1628. dst[0]= (h->nal_ref_idc<<5) + h->nal_unit_type;
  1629. if(length==0) return 1;
  1630. escape_count= 0;
  1631. for(i=0; i<length; i+=2){
  1632. if(src[i]) continue;
  1633. if(i>0 && src[i-1]==0)
  1634. i--;
  1635. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  1636. escape_count++;
  1637. i+=2;
  1638. }
  1639. }
  1640. if(escape_count==0){
  1641. if(dst+1 != src)
  1642. memcpy(dst+1, src, length);
  1643. return length + 1;
  1644. }
  1645. if(length + escape_count + 1> dst_length)
  1646. return -1;
  1647. //this should be damn rare (hopefully)
  1648. h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length + escape_count);
  1649. temp= h->rbsp_buffer;
  1650. //printf("encoding esc\n");
  1651. si= 0;
  1652. di= 0;
  1653. while(si < length){
  1654. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  1655. temp[di++]= 0; si++;
  1656. temp[di++]= 0; si++;
  1657. temp[di++]= 3;
  1658. temp[di++]= src[si++];
  1659. }
  1660. else
  1661. temp[di++]= src[si++];
  1662. }
  1663. memcpy(dst+1, temp, length+escape_count);
  1664. assert(di == length+escape_count);
  1665. return di + 1;
  1666. }
  1667. /**
  1668. * write 1,10,100,1000,... for alignment, yes its exactly inverse to mpeg4
  1669. */
  1670. static void encode_rbsp_trailing(PutBitContext *pb){
  1671. int length;
  1672. put_bits(pb, 1, 1);
  1673. length= (-put_bits_count(pb))&7;
  1674. if(length) put_bits(pb, length, 0);
  1675. }
  1676. #endif
  1677. /**
  1678. * identifies the exact end of the bitstream
  1679. * @return the length of the trailing, or 0 if damaged
  1680. */
  1681. static int decode_rbsp_trailing(uint8_t *src){
  1682. int v= *src;
  1683. int r;
  1684. tprintf("rbsp trailing %X\n", v);
  1685. for(r=1; r<9; r++){
  1686. if(v&1) return r;
  1687. v>>=1;
  1688. }
  1689. return 0;
  1690. }
  1691. /**
  1692. * idct tranforms the 16 dc values and dequantize them.
  1693. * @param qp quantization parameter
  1694. */
  1695. static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1696. #define stride 16
  1697. int i;
  1698. int temp[16]; //FIXME check if this is a good idea
  1699. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1700. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1701. //memset(block, 64, 2*256);
  1702. //return;
  1703. for(i=0; i<4; i++){
  1704. const int offset= y_offset[i];
  1705. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1706. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1707. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1708. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1709. temp[4*i+0]= z0+z3;
  1710. temp[4*i+1]= z1+z2;
  1711. temp[4*i+2]= z1-z2;
  1712. temp[4*i+3]= z0-z3;
  1713. }
  1714. for(i=0; i<4; i++){
  1715. const int offset= x_offset[i];
  1716. const int z0= temp[4*0+i] + temp[4*2+i];
  1717. const int z1= temp[4*0+i] - temp[4*2+i];
  1718. const int z2= temp[4*1+i] - temp[4*3+i];
  1719. const int z3= temp[4*1+i] + temp[4*3+i];
  1720. block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_resdual
  1721. block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
  1722. block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
  1723. block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
  1724. }
  1725. }
  1726. #if 0
  1727. /**
  1728. * dct tranforms the 16 dc values.
  1729. * @param qp quantization parameter ??? FIXME
  1730. */
  1731. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  1732. // const int qmul= dequant_coeff[qp][0];
  1733. int i;
  1734. int temp[16]; //FIXME check if this is a good idea
  1735. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1736. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1737. for(i=0; i<4; i++){
  1738. const int offset= y_offset[i];
  1739. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1740. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1741. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1742. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1743. temp[4*i+0]= z0+z3;
  1744. temp[4*i+1]= z1+z2;
  1745. temp[4*i+2]= z1-z2;
  1746. temp[4*i+3]= z0-z3;
  1747. }
  1748. for(i=0; i<4; i++){
  1749. const int offset= x_offset[i];
  1750. const int z0= temp[4*0+i] + temp[4*2+i];
  1751. const int z1= temp[4*0+i] - temp[4*2+i];
  1752. const int z2= temp[4*1+i] - temp[4*3+i];
  1753. const int z3= temp[4*1+i] + temp[4*3+i];
  1754. block[stride*0 +offset]= (z0 + z3)>>1;
  1755. block[stride*2 +offset]= (z1 + z2)>>1;
  1756. block[stride*8 +offset]= (z1 - z2)>>1;
  1757. block[stride*10+offset]= (z0 - z3)>>1;
  1758. }
  1759. }
  1760. #endif
  1761. #undef xStride
  1762. #undef stride
  1763. static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1764. const int stride= 16*2;
  1765. const int xStride= 16;
  1766. int a,b,c,d,e;
  1767. a= block[stride*0 + xStride*0];
  1768. b= block[stride*0 + xStride*1];
  1769. c= block[stride*1 + xStride*0];
  1770. d= block[stride*1 + xStride*1];
  1771. e= a-b;
  1772. a= a+b;
  1773. b= c-d;
  1774. c= c+d;
  1775. block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
  1776. block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
  1777. block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
  1778. block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
  1779. }
  1780. #if 0
  1781. static void chroma_dc_dct_c(DCTELEM *block){
  1782. const int stride= 16*2;
  1783. const int xStride= 16;
  1784. int a,b,c,d,e;
  1785. a= block[stride*0 + xStride*0];
  1786. b= block[stride*0 + xStride*1];
  1787. c= block[stride*1 + xStride*0];
  1788. d= block[stride*1 + xStride*1];
  1789. e= a-b;
  1790. a= a+b;
  1791. b= c-d;
  1792. c= c+d;
  1793. block[stride*0 + xStride*0]= (a+c);
  1794. block[stride*0 + xStride*1]= (e+b);
  1795. block[stride*1 + xStride*0]= (a-c);
  1796. block[stride*1 + xStride*1]= (e-b);
  1797. }
  1798. #endif
  1799. /**
  1800. * gets the chroma qp.
  1801. */
  1802. static inline int get_chroma_qp(int chroma_qp_index_offset, int qscale){
  1803. return chroma_qp[clip(qscale + chroma_qp_index_offset, 0, 51)];
  1804. }
  1805. #if 0
  1806. static void h264_diff_dct_c(DCTELEM *block, uint8_t *src1, uint8_t *src2, int stride){
  1807. int i;
  1808. //FIXME try int temp instead of block
  1809. for(i=0; i<4; i++){
  1810. const int d0= src1[0 + i*stride] - src2[0 + i*stride];
  1811. const int d1= src1[1 + i*stride] - src2[1 + i*stride];
  1812. const int d2= src1[2 + i*stride] - src2[2 + i*stride];
  1813. const int d3= src1[3 + i*stride] - src2[3 + i*stride];
  1814. const int z0= d0 + d3;
  1815. const int z3= d0 - d3;
  1816. const int z1= d1 + d2;
  1817. const int z2= d1 - d2;
  1818. block[0 + 4*i]= z0 + z1;
  1819. block[1 + 4*i]= 2*z3 + z2;
  1820. block[2 + 4*i]= z0 - z1;
  1821. block[3 + 4*i]= z3 - 2*z2;
  1822. }
  1823. for(i=0; i<4; i++){
  1824. const int z0= block[0*4 + i] + block[3*4 + i];
  1825. const int z3= block[0*4 + i] - block[3*4 + i];
  1826. const int z1= block[1*4 + i] + block[2*4 + i];
  1827. const int z2= block[1*4 + i] - block[2*4 + i];
  1828. block[0*4 + i]= z0 + z1;
  1829. block[1*4 + i]= 2*z3 + z2;
  1830. block[2*4 + i]= z0 - z1;
  1831. block[3*4 + i]= z3 - 2*z2;
  1832. }
  1833. }
  1834. #endif
  1835. //FIXME need to check that this doesnt overflow signed 32 bit for low qp, i am not sure, it's very close
  1836. //FIXME check that gcc inlines this (and optimizes intra & seperate_dc stuff away)
  1837. static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int seperate_dc){
  1838. int i;
  1839. const int * const quant_table= quant_coeff[qscale];
  1840. const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
  1841. const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
  1842. const unsigned int threshold2= (threshold1<<1);
  1843. int last_non_zero;
  1844. if(seperate_dc){
  1845. if(qscale<=18){
  1846. //avoid overflows
  1847. const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
  1848. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
  1849. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1850. int level= block[0]*quant_coeff[qscale+18][0];
  1851. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1852. if(level>0){
  1853. level= (dc_bias + level)>>(QUANT_SHIFT-2);
  1854. block[0]= level;
  1855. }else{
  1856. level= (dc_bias - level)>>(QUANT_SHIFT-2);
  1857. block[0]= -level;
  1858. }
  1859. // last_non_zero = i;
  1860. }else{
  1861. block[0]=0;
  1862. }
  1863. }else{
  1864. const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
  1865. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
  1866. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1867. int level= block[0]*quant_table[0];
  1868. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1869. if(level>0){
  1870. level= (dc_bias + level)>>(QUANT_SHIFT+1);
  1871. block[0]= level;
  1872. }else{
  1873. level= (dc_bias - level)>>(QUANT_SHIFT+1);
  1874. block[0]= -level;
  1875. }
  1876. // last_non_zero = i;
  1877. }else{
  1878. block[0]=0;
  1879. }
  1880. }
  1881. last_non_zero= 0;
  1882. i=1;
  1883. }else{
  1884. last_non_zero= -1;
  1885. i=0;
  1886. }
  1887. for(; i<16; i++){
  1888. const int j= scantable[i];
  1889. int level= block[j]*quant_table[j];
  1890. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  1891. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  1892. if(((unsigned)(level+threshold1))>threshold2){
  1893. if(level>0){
  1894. level= (bias + level)>>QUANT_SHIFT;
  1895. block[j]= level;
  1896. }else{
  1897. level= (bias - level)>>QUANT_SHIFT;
  1898. block[j]= -level;
  1899. }
  1900. last_non_zero = i;
  1901. }else{
  1902. block[j]=0;
  1903. }
  1904. }
  1905. return last_non_zero;
  1906. }
  1907. static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){
  1908. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1909. ((uint32_t*)(src+0*stride))[0]= a;
  1910. ((uint32_t*)(src+1*stride))[0]= a;
  1911. ((uint32_t*)(src+2*stride))[0]= a;
  1912. ((uint32_t*)(src+3*stride))[0]= a;
  1913. }
  1914. static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){
  1915. ((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101;
  1916. ((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101;
  1917. ((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101;
  1918. ((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101;
  1919. }
  1920. static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1921. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
  1922. + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
  1923. ((uint32_t*)(src+0*stride))[0]=
  1924. ((uint32_t*)(src+1*stride))[0]=
  1925. ((uint32_t*)(src+2*stride))[0]=
  1926. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1927. }
  1928. static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1929. const int dc= ( src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
  1930. ((uint32_t*)(src+0*stride))[0]=
  1931. ((uint32_t*)(src+1*stride))[0]=
  1932. ((uint32_t*)(src+2*stride))[0]=
  1933. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1934. }
  1935. static void pred4x4_top_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1936. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2;
  1937. ((uint32_t*)(src+0*stride))[0]=
  1938. ((uint32_t*)(src+1*stride))[0]=
  1939. ((uint32_t*)(src+2*stride))[0]=
  1940. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1941. }
  1942. static void pred4x4_128_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1943. ((uint32_t*)(src+0*stride))[0]=
  1944. ((uint32_t*)(src+1*stride))[0]=
  1945. ((uint32_t*)(src+2*stride))[0]=
  1946. ((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U;
  1947. }
  1948. #define LOAD_TOP_RIGHT_EDGE\
  1949. const int t4= topright[0];\
  1950. const int t5= topright[1];\
  1951. const int t6= topright[2];\
  1952. const int t7= topright[3];\
  1953. #define LOAD_LEFT_EDGE\
  1954. const int l0= src[-1+0*stride];\
  1955. const int l1= src[-1+1*stride];\
  1956. const int l2= src[-1+2*stride];\
  1957. const int l3= src[-1+3*stride];\
  1958. #define LOAD_TOP_EDGE\
  1959. const int t0= src[ 0-1*stride];\
  1960. const int t1= src[ 1-1*stride];\
  1961. const int t2= src[ 2-1*stride];\
  1962. const int t3= src[ 3-1*stride];\
  1963. static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){
  1964. const int lt= src[-1-1*stride];
  1965. LOAD_TOP_EDGE
  1966. LOAD_LEFT_EDGE
  1967. src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
  1968. src[0+2*stride]=
  1969. src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
  1970. src[0+1*stride]=
  1971. src[1+2*stride]=
  1972. src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
  1973. src[0+0*stride]=
  1974. src[1+1*stride]=
  1975. src[2+2*stride]=
  1976. src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1977. src[1+0*stride]=
  1978. src[2+1*stride]=
  1979. src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1980. src[2+0*stride]=
  1981. src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1982. src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1983. }
  1984. static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){
  1985. LOAD_TOP_EDGE
  1986. LOAD_TOP_RIGHT_EDGE
  1987. // LOAD_LEFT_EDGE
  1988. src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
  1989. src[1+0*stride]=
  1990. src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
  1991. src[2+0*stride]=
  1992. src[1+1*stride]=
  1993. src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
  1994. src[3+0*stride]=
  1995. src[2+1*stride]=
  1996. src[1+2*stride]=
  1997. src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
  1998. src[3+1*stride]=
  1999. src[2+2*stride]=
  2000. src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
  2001. src[3+2*stride]=
  2002. src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
  2003. src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
  2004. }
  2005. static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
  2006. const int lt= src[-1-1*stride];
  2007. LOAD_TOP_EDGE
  2008. LOAD_LEFT_EDGE
  2009. const __attribute__((unused)) int unu= l3;
  2010. src[0+0*stride]=
  2011. src[1+2*stride]=(lt + t0 + 1)>>1;
  2012. src[1+0*stride]=
  2013. src[2+2*stride]=(t0 + t1 + 1)>>1;
  2014. src[2+0*stride]=
  2015. src[3+2*stride]=(t1 + t2 + 1)>>1;
  2016. src[3+0*stride]=(t2 + t3 + 1)>>1;
  2017. src[0+1*stride]=
  2018. src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  2019. src[1+1*stride]=
  2020. src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
  2021. src[2+1*stride]=
  2022. src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  2023. src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  2024. src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  2025. src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  2026. }
  2027. static void pred4x4_vertical_left_c(uint8_t *src, uint8_t *topright, int stride){
  2028. LOAD_TOP_EDGE
  2029. LOAD_TOP_RIGHT_EDGE
  2030. const __attribute__((unused)) int unu= t7;
  2031. src[0+0*stride]=(t0 + t1 + 1)>>1;
  2032. src[1+0*stride]=
  2033. src[0+2*stride]=(t1 + t2 + 1)>>1;
  2034. src[2+0*stride]=
  2035. src[1+2*stride]=(t2 + t3 + 1)>>1;
  2036. src[3+0*stride]=
  2037. src[2+2*stride]=(t3 + t4+ 1)>>1;
  2038. src[3+2*stride]=(t4 + t5+ 1)>>1;
  2039. src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  2040. src[1+1*stride]=
  2041. src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  2042. src[2+1*stride]=
  2043. src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
  2044. src[3+1*stride]=
  2045. src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
  2046. src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
  2047. }
  2048. static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){
  2049. LOAD_LEFT_EDGE
  2050. src[0+0*stride]=(l0 + l1 + 1)>>1;
  2051. src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  2052. src[2+0*stride]=
  2053. src[0+1*stride]=(l1 + l2 + 1)>>1;
  2054. src[3+0*stride]=
  2055. src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  2056. src[2+1*stride]=
  2057. src[0+2*stride]=(l2 + l3 + 1)>>1;
  2058. src[3+1*stride]=
  2059. src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
  2060. src[3+2*stride]=
  2061. src[1+3*stride]=
  2062. src[0+3*stride]=
  2063. src[2+2*stride]=
  2064. src[2+3*stride]=
  2065. src[3+3*stride]=l3;
  2066. }
  2067. static void pred4x4_horizontal_down_c(uint8_t *src, uint8_t *topright, int stride){
  2068. const int lt= src[-1-1*stride];
  2069. LOAD_TOP_EDGE
  2070. LOAD_LEFT_EDGE
  2071. const __attribute__((unused)) int unu= t3;
  2072. src[0+0*stride]=
  2073. src[2+1*stride]=(lt + l0 + 1)>>1;
  2074. src[1+0*stride]=
  2075. src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
  2076. src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
  2077. src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  2078. src[0+1*stride]=
  2079. src[2+2*stride]=(l0 + l1 + 1)>>1;
  2080. src[1+1*stride]=
  2081. src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  2082. src[0+2*stride]=
  2083. src[2+3*stride]=(l1 + l2+ 1)>>1;
  2084. src[1+2*stride]=
  2085. src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  2086. src[0+3*stride]=(l2 + l3 + 1)>>1;
  2087. src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  2088. }
  2089. static void pred16x16_vertical_c(uint8_t *src, int stride){
  2090. int i;
  2091. const uint32_t a= ((uint32_t*)(src-stride))[0];
  2092. const uint32_t b= ((uint32_t*)(src-stride))[1];
  2093. const uint32_t c= ((uint32_t*)(src-stride))[2];
  2094. const uint32_t d= ((uint32_t*)(src-stride))[3];
  2095. for(i=0; i<16; i++){
  2096. ((uint32_t*)(src+i*stride))[0]= a;
  2097. ((uint32_t*)(src+i*stride))[1]= b;
  2098. ((uint32_t*)(src+i*stride))[2]= c;
  2099. ((uint32_t*)(src+i*stride))[3]= d;
  2100. }
  2101. }
  2102. static void pred16x16_horizontal_c(uint8_t *src, int stride){
  2103. int i;
  2104. for(i=0; i<16; i++){
  2105. ((uint32_t*)(src+i*stride))[0]=
  2106. ((uint32_t*)(src+i*stride))[1]=
  2107. ((uint32_t*)(src+i*stride))[2]=
  2108. ((uint32_t*)(src+i*stride))[3]= src[-1+i*stride]*0x01010101;
  2109. }
  2110. }
  2111. static void pred16x16_dc_c(uint8_t *src, int stride){
  2112. int i, dc=0;
  2113. for(i=0;i<16; i++){
  2114. dc+= src[-1+i*stride];
  2115. }
  2116. for(i=0;i<16; i++){
  2117. dc+= src[i-stride];
  2118. }
  2119. dc= 0x01010101*((dc + 16)>>5);
  2120. for(i=0; i<16; i++){
  2121. ((uint32_t*)(src+i*stride))[0]=
  2122. ((uint32_t*)(src+i*stride))[1]=
  2123. ((uint32_t*)(src+i*stride))[2]=
  2124. ((uint32_t*)(src+i*stride))[3]= dc;
  2125. }
  2126. }
  2127. static void pred16x16_left_dc_c(uint8_t *src, int stride){
  2128. int i, dc=0;
  2129. for(i=0;i<16; i++){
  2130. dc+= src[-1+i*stride];
  2131. }
  2132. dc= 0x01010101*((dc + 8)>>4);
  2133. for(i=0; i<16; i++){
  2134. ((uint32_t*)(src+i*stride))[0]=
  2135. ((uint32_t*)(src+i*stride))[1]=
  2136. ((uint32_t*)(src+i*stride))[2]=
  2137. ((uint32_t*)(src+i*stride))[3]= dc;
  2138. }
  2139. }
  2140. static void pred16x16_top_dc_c(uint8_t *src, int stride){
  2141. int i, dc=0;
  2142. for(i=0;i<16; i++){
  2143. dc+= src[i-stride];
  2144. }
  2145. dc= 0x01010101*((dc + 8)>>4);
  2146. for(i=0; i<16; i++){
  2147. ((uint32_t*)(src+i*stride))[0]=
  2148. ((uint32_t*)(src+i*stride))[1]=
  2149. ((uint32_t*)(src+i*stride))[2]=
  2150. ((uint32_t*)(src+i*stride))[3]= dc;
  2151. }
  2152. }
  2153. static void pred16x16_128_dc_c(uint8_t *src, int stride){
  2154. int i;
  2155. for(i=0; i<16; i++){
  2156. ((uint32_t*)(src+i*stride))[0]=
  2157. ((uint32_t*)(src+i*stride))[1]=
  2158. ((uint32_t*)(src+i*stride))[2]=
  2159. ((uint32_t*)(src+i*stride))[3]= 0x01010101U*128U;
  2160. }
  2161. }
  2162. static inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){
  2163. int i, j, k;
  2164. int a;
  2165. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  2166. const uint8_t * const src0 = src+7-stride;
  2167. const uint8_t *src1 = src+8*stride-1;
  2168. const uint8_t *src2 = src1-2*stride; // == src+6*stride-1;
  2169. int H = src0[1] - src0[-1];
  2170. int V = src1[0] - src2[ 0];
  2171. for(k=2; k<=8; ++k) {
  2172. src1 += stride; src2 -= stride;
  2173. H += k*(src0[k] - src0[-k]);
  2174. V += k*(src1[0] - src2[ 0]);
  2175. }
  2176. if(svq3){
  2177. H = ( 5*(H/4) ) / 16;
  2178. V = ( 5*(V/4) ) / 16;
  2179. /* required for 100% accuracy */
  2180. i = H; H = V; V = i;
  2181. }else{
  2182. H = ( 5*H+32 ) >> 6;
  2183. V = ( 5*V+32 ) >> 6;
  2184. }
  2185. a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
  2186. for(j=16; j>0; --j) {
  2187. int b = a;
  2188. a += V;
  2189. for(i=-16; i<0; i+=4) {
  2190. src[16+i] = cm[ (b ) >> 5 ];
  2191. src[17+i] = cm[ (b+ H) >> 5 ];
  2192. src[18+i] = cm[ (b+2*H) >> 5 ];
  2193. src[19+i] = cm[ (b+3*H) >> 5 ];
  2194. b += 4*H;
  2195. }
  2196. src += stride;
  2197. }
  2198. }
  2199. static void pred16x16_plane_c(uint8_t *src, int stride){
  2200. pred16x16_plane_compat_c(src, stride, 0);
  2201. }
  2202. static void pred8x8_vertical_c(uint8_t *src, int stride){
  2203. int i;
  2204. const uint32_t a= ((uint32_t*)(src-stride))[0];
  2205. const uint32_t b= ((uint32_t*)(src-stride))[1];
  2206. for(i=0; i<8; i++){
  2207. ((uint32_t*)(src+i*stride))[0]= a;
  2208. ((uint32_t*)(src+i*stride))[1]= b;
  2209. }
  2210. }
  2211. static void pred8x8_horizontal_c(uint8_t *src, int stride){
  2212. int i;
  2213. for(i=0; i<8; i++){
  2214. ((uint32_t*)(src+i*stride))[0]=
  2215. ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101;
  2216. }
  2217. }
  2218. static void pred8x8_128_dc_c(uint8_t *src, int stride){
  2219. int i;
  2220. for(i=0; i<8; i++){
  2221. ((uint32_t*)(src+i*stride))[0]=
  2222. ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
  2223. }
  2224. }
  2225. static void pred8x8_left_dc_c(uint8_t *src, int stride){
  2226. int i;
  2227. int dc0, dc2;
  2228. dc0=dc2=0;
  2229. for(i=0;i<4; i++){
  2230. dc0+= src[-1+i*stride];
  2231. dc2+= src[-1+(i+4)*stride];
  2232. }
  2233. dc0= 0x01010101*((dc0 + 2)>>2);
  2234. dc2= 0x01010101*((dc2 + 2)>>2);
  2235. for(i=0; i<4; i++){
  2236. ((uint32_t*)(src+i*stride))[0]=
  2237. ((uint32_t*)(src+i*stride))[1]= dc0;
  2238. }
  2239. for(i=4; i<8; i++){
  2240. ((uint32_t*)(src+i*stride))[0]=
  2241. ((uint32_t*)(src+i*stride))[1]= dc2;
  2242. }
  2243. }
  2244. static void pred8x8_top_dc_c(uint8_t *src, int stride){
  2245. int i;
  2246. int dc0, dc1;
  2247. dc0=dc1=0;
  2248. for(i=0;i<4; i++){
  2249. dc0+= src[i-stride];
  2250. dc1+= src[4+i-stride];
  2251. }
  2252. dc0= 0x01010101*((dc0 + 2)>>2);
  2253. dc1= 0x01010101*((dc1 + 2)>>2);
  2254. for(i=0; i<4; i++){
  2255. ((uint32_t*)(src+i*stride))[0]= dc0;
  2256. ((uint32_t*)(src+i*stride))[1]= dc1;
  2257. }
  2258. for(i=4; i<8; i++){
  2259. ((uint32_t*)(src+i*stride))[0]= dc0;
  2260. ((uint32_t*)(src+i*stride))[1]= dc1;
  2261. }
  2262. }
  2263. static void pred8x8_dc_c(uint8_t *src, int stride){
  2264. int i;
  2265. int dc0, dc1, dc2, dc3;
  2266. dc0=dc1=dc2=0;
  2267. for(i=0;i<4; i++){
  2268. dc0+= src[-1+i*stride] + src[i-stride];
  2269. dc1+= src[4+i-stride];
  2270. dc2+= src[-1+(i+4)*stride];
  2271. }
  2272. dc3= 0x01010101*((dc1 + dc2 + 4)>>3);
  2273. dc0= 0x01010101*((dc0 + 4)>>3);
  2274. dc1= 0x01010101*((dc1 + 2)>>2);
  2275. dc2= 0x01010101*((dc2 + 2)>>2);
  2276. for(i=0; i<4; i++){
  2277. ((uint32_t*)(src+i*stride))[0]= dc0;
  2278. ((uint32_t*)(src+i*stride))[1]= dc1;
  2279. }
  2280. for(i=4; i<8; i++){
  2281. ((uint32_t*)(src+i*stride))[0]= dc2;
  2282. ((uint32_t*)(src+i*stride))[1]= dc3;
  2283. }
  2284. }
  2285. static void pred8x8_plane_c(uint8_t *src, int stride){
  2286. int j, k;
  2287. int a;
  2288. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  2289. const uint8_t * const src0 = src+3-stride;
  2290. const uint8_t *src1 = src+4*stride-1;
  2291. const uint8_t *src2 = src1-2*stride; // == src+2*stride-1;
  2292. int H = src0[1] - src0[-1];
  2293. int V = src1[0] - src2[ 0];
  2294. for(k=2; k<=4; ++k) {
  2295. src1 += stride; src2 -= stride;
  2296. H += k*(src0[k] - src0[-k]);
  2297. V += k*(src1[0] - src2[ 0]);
  2298. }
  2299. H = ( 17*H+16 ) >> 5;
  2300. V = ( 17*V+16 ) >> 5;
  2301. a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
  2302. for(j=8; j>0; --j) {
  2303. int b = a;
  2304. a += V;
  2305. src[0] = cm[ (b ) >> 5 ];
  2306. src[1] = cm[ (b+ H) >> 5 ];
  2307. src[2] = cm[ (b+2*H) >> 5 ];
  2308. src[3] = cm[ (b+3*H) >> 5 ];
  2309. src[4] = cm[ (b+4*H) >> 5 ];
  2310. src[5] = cm[ (b+5*H) >> 5 ];
  2311. src[6] = cm[ (b+6*H) >> 5 ];
  2312. src[7] = cm[ (b+7*H) >> 5 ];
  2313. src += stride;
  2314. }
  2315. }
  2316. #define SRC(x,y) src[(x)+(y)*stride]
  2317. #define PL(y) \
  2318. const int l##y = (SRC(-1,y-1) + 2*SRC(-1,y) + SRC(-1,y+1) + 2) >> 2;
  2319. #define PREDICT_8x8_LOAD_LEFT \
  2320. const int l0 = ((has_topleft ? SRC(-1,-1) : SRC(-1,0)) \
  2321. + 2*SRC(-1,0) + SRC(-1,1) + 2) >> 2; \
  2322. PL(1) PL(2) PL(3) PL(4) PL(5) PL(6) \
  2323. const int l7 attribute_unused = (SRC(-1,6) + 3*SRC(-1,7) + 2) >> 2
  2324. #define PT(x) \
  2325. const int t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  2326. #define PREDICT_8x8_LOAD_TOP \
  2327. const int t0 = ((has_topleft ? SRC(-1,-1) : SRC(0,-1)) \
  2328. + 2*SRC(0,-1) + SRC(1,-1) + 2) >> 2; \
  2329. PT(1) PT(2) PT(3) PT(4) PT(5) PT(6) \
  2330. const int t7 attribute_unused = ((has_topright ? SRC(8,-1) : SRC(7,-1)) \
  2331. + 2*SRC(7,-1) + SRC(6,-1) + 2) >> 2
  2332. #define PTR(x) \
  2333. t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  2334. #define PREDICT_8x8_LOAD_TOPRIGHT \
  2335. int t8, t9, t10, t11, t12, t13, t14, t15; \
  2336. if(has_topright) { \
  2337. PTR(8) PTR(9) PTR(10) PTR(11) PTR(12) PTR(13) PTR(14) \
  2338. t15 = (SRC(14,-1) + 3*SRC(15,-1) + 2) >> 2; \
  2339. } else t8=t9=t10=t11=t12=t13=t14=t15= SRC(7,-1);
  2340. #define PREDICT_8x8_LOAD_TOPLEFT \
  2341. const int lt = (SRC(-1,0) + 2*SRC(-1,-1) + SRC(0,-1) + 2) >> 2
  2342. #define PREDICT_8x8_DC(v) \
  2343. int y; \
  2344. for( y = 0; y < 8; y++ ) { \
  2345. ((uint32_t*)src)[0] = \
  2346. ((uint32_t*)src)[1] = v; \
  2347. src += stride; \
  2348. }
  2349. static void pred8x8l_128_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2350. {
  2351. PREDICT_8x8_DC(0x80808080);
  2352. }
  2353. static void pred8x8l_left_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2354. {
  2355. PREDICT_8x8_LOAD_LEFT;
  2356. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7+4) >> 3) * 0x01010101;
  2357. PREDICT_8x8_DC(dc);
  2358. }
  2359. static void pred8x8l_top_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2360. {
  2361. PREDICT_8x8_LOAD_TOP;
  2362. const uint32_t dc = ((t0+t1+t2+t3+t4+t5+t6+t7+4) >> 3) * 0x01010101;
  2363. PREDICT_8x8_DC(dc);
  2364. }
  2365. static void pred8x8l_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2366. {
  2367. PREDICT_8x8_LOAD_LEFT;
  2368. PREDICT_8x8_LOAD_TOP;
  2369. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7
  2370. +t0+t1+t2+t3+t4+t5+t6+t7+8) >> 4) * 0x01010101;
  2371. PREDICT_8x8_DC(dc);
  2372. }
  2373. static void pred8x8l_horizontal_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2374. {
  2375. PREDICT_8x8_LOAD_LEFT;
  2376. #define ROW(y) ((uint32_t*)(src+y*stride))[0] =\
  2377. ((uint32_t*)(src+y*stride))[1] = 0x01010101 * l##y
  2378. ROW(0); ROW(1); ROW(2); ROW(3); ROW(4); ROW(5); ROW(6); ROW(7);
  2379. #undef ROW
  2380. }
  2381. static void pred8x8l_vertical_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2382. {
  2383. int y;
  2384. PREDICT_8x8_LOAD_TOP;
  2385. src[0] = t0;
  2386. src[1] = t1;
  2387. src[2] = t2;
  2388. src[3] = t3;
  2389. src[4] = t4;
  2390. src[5] = t5;
  2391. src[6] = t6;
  2392. src[7] = t7;
  2393. for( y = 1; y < 8; y++ )
  2394. *(uint64_t*)(src+y*stride) = *(uint64_t*)src;
  2395. }
  2396. static void pred8x8l_down_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2397. {
  2398. PREDICT_8x8_LOAD_TOP;
  2399. PREDICT_8x8_LOAD_TOPRIGHT;
  2400. SRC(0,0)= (t0 + 2*t1 + t2 + 2) >> 2;
  2401. SRC(0,1)=SRC(1,0)= (t1 + 2*t2 + t3 + 2) >> 2;
  2402. SRC(0,2)=SRC(1,1)=SRC(2,0)= (t2 + 2*t3 + t4 + 2) >> 2;
  2403. SRC(0,3)=SRC(1,2)=SRC(2,1)=SRC(3,0)= (t3 + 2*t4 + t5 + 2) >> 2;
  2404. SRC(0,4)=SRC(1,3)=SRC(2,2)=SRC(3,1)=SRC(4,0)= (t4 + 2*t5 + t6 + 2) >> 2;
  2405. SRC(0,5)=SRC(1,4)=SRC(2,3)=SRC(3,2)=SRC(4,1)=SRC(5,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2406. 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;
  2407. 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;
  2408. 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;
  2409. SRC(2,7)=SRC(3,6)=SRC(4,5)=SRC(5,4)=SRC(6,3)=SRC(7,2)= (t9 + 2*t10 + t11 + 2) >> 2;
  2410. SRC(3,7)=SRC(4,6)=SRC(5,5)=SRC(6,4)=SRC(7,3)= (t10 + 2*t11 + t12 + 2) >> 2;
  2411. SRC(4,7)=SRC(5,6)=SRC(6,5)=SRC(7,4)= (t11 + 2*t12 + t13 + 2) >> 2;
  2412. SRC(5,7)=SRC(6,6)=SRC(7,5)= (t12 + 2*t13 + t14 + 2) >> 2;
  2413. SRC(6,7)=SRC(7,6)= (t13 + 2*t14 + t15 + 2) >> 2;
  2414. SRC(7,7)= (t14 + 3*t15 + 2) >> 2;
  2415. }
  2416. static void pred8x8l_down_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2417. {
  2418. PREDICT_8x8_LOAD_TOP;
  2419. PREDICT_8x8_LOAD_LEFT;
  2420. PREDICT_8x8_LOAD_TOPLEFT;
  2421. SRC(0,7)= (l7 + 2*l6 + l5 + 2) >> 2;
  2422. SRC(0,6)=SRC(1,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2423. SRC(0,5)=SRC(1,6)=SRC(2,7)= (l5 + 2*l4 + l3 + 2) >> 2;
  2424. SRC(0,4)=SRC(1,5)=SRC(2,6)=SRC(3,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2425. SRC(0,3)=SRC(1,4)=SRC(2,5)=SRC(3,6)=SRC(4,7)= (l3 + 2*l2 + l1 + 2) >> 2;
  2426. SRC(0,2)=SRC(1,3)=SRC(2,4)=SRC(3,5)=SRC(4,6)=SRC(5,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2427. 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;
  2428. 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;
  2429. 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;
  2430. SRC(2,0)=SRC(3,1)=SRC(4,2)=SRC(5,3)=SRC(6,4)=SRC(7,5)= (t0 + 2*t1 + t2 + 2) >> 2;
  2431. SRC(3,0)=SRC(4,1)=SRC(5,2)=SRC(6,3)=SRC(7,4)= (t1 + 2*t2 + t3 + 2) >> 2;
  2432. SRC(4,0)=SRC(5,1)=SRC(6,2)=SRC(7,3)= (t2 + 2*t3 + t4 + 2) >> 2;
  2433. SRC(5,0)=SRC(6,1)=SRC(7,2)= (t3 + 2*t4 + t5 + 2) >> 2;
  2434. SRC(6,0)=SRC(7,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2435. SRC(7,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2436. }
  2437. static void pred8x8l_vertical_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2438. {
  2439. PREDICT_8x8_LOAD_TOP;
  2440. PREDICT_8x8_LOAD_LEFT;
  2441. PREDICT_8x8_LOAD_TOPLEFT;
  2442. SRC(0,6)= (l5 + 2*l4 + l3 + 2) >> 2;
  2443. SRC(0,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2444. SRC(0,4)=SRC(1,6)= (l3 + 2*l2 + l1 + 2) >> 2;
  2445. SRC(0,5)=SRC(1,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2446. SRC(0,2)=SRC(1,4)=SRC(2,6)= (l1 + 2*l0 + lt + 2) >> 2;
  2447. SRC(0,3)=SRC(1,5)=SRC(2,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2448. SRC(0,1)=SRC(1,3)=SRC(2,5)=SRC(3,7)= (l0 + 2*lt + t0 + 2) >> 2;
  2449. SRC(0,0)=SRC(1,2)=SRC(2,4)=SRC(3,6)= (lt + t0 + 1) >> 1;
  2450. SRC(1,1)=SRC(2,3)=SRC(3,5)=SRC(4,7)= (lt + 2*t0 + t1 + 2) >> 2;
  2451. SRC(1,0)=SRC(2,2)=SRC(3,4)=SRC(4,6)= (t0 + t1 + 1) >> 1;
  2452. SRC(2,1)=SRC(3,3)=SRC(4,5)=SRC(5,7)= (t0 + 2*t1 + t2 + 2) >> 2;
  2453. SRC(2,0)=SRC(3,2)=SRC(4,4)=SRC(5,6)= (t1 + t2 + 1) >> 1;
  2454. SRC(3,1)=SRC(4,3)=SRC(5,5)=SRC(6,7)= (t1 + 2*t2 + t3 + 2) >> 2;
  2455. SRC(3,0)=SRC(4,2)=SRC(5,4)=SRC(6,6)= (t2 + t3 + 1) >> 1;
  2456. SRC(4,1)=SRC(5,3)=SRC(6,5)=SRC(7,7)= (t2 + 2*t3 + t4 + 2) >> 2;
  2457. SRC(4,0)=SRC(5,2)=SRC(6,4)=SRC(7,6)= (t3 + t4 + 1) >> 1;
  2458. SRC(5,1)=SRC(6,3)=SRC(7,5)= (t3 + 2*t4 + t5 + 2) >> 2;
  2459. SRC(5,0)=SRC(6,2)=SRC(7,4)= (t4 + t5 + 1) >> 1;
  2460. SRC(6,1)=SRC(7,3)= (t4 + 2*t5 + t6 + 2) >> 2;
  2461. SRC(6,0)=SRC(7,2)= (t5 + t6 + 1) >> 1;
  2462. SRC(7,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2463. SRC(7,0)= (t6 + t7 + 1) >> 1;
  2464. }
  2465. static void pred8x8l_horizontal_down_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2466. {
  2467. PREDICT_8x8_LOAD_TOP;
  2468. PREDICT_8x8_LOAD_LEFT;
  2469. PREDICT_8x8_LOAD_TOPLEFT;
  2470. SRC(0,7)= (l6 + l7 + 1) >> 1;
  2471. SRC(1,7)= (l5 + 2*l6 + l7 + 2) >> 2;
  2472. SRC(0,6)=SRC(2,7)= (l5 + l6 + 1) >> 1;
  2473. SRC(1,6)=SRC(3,7)= (l4 + 2*l5 + l6 + 2) >> 2;
  2474. SRC(0,5)=SRC(2,6)=SRC(4,7)= (l4 + l5 + 1) >> 1;
  2475. SRC(1,5)=SRC(3,6)=SRC(5,7)= (l3 + 2*l4 + l5 + 2) >> 2;
  2476. SRC(0,4)=SRC(2,5)=SRC(4,6)=SRC(6,7)= (l3 + l4 + 1) >> 1;
  2477. SRC(1,4)=SRC(3,5)=SRC(5,6)=SRC(7,7)= (l2 + 2*l3 + l4 + 2) >> 2;
  2478. SRC(0,3)=SRC(2,4)=SRC(4,5)=SRC(6,6)= (l2 + l3 + 1) >> 1;
  2479. SRC(1,3)=SRC(3,4)=SRC(5,5)=SRC(7,6)= (l1 + 2*l2 + l3 + 2) >> 2;
  2480. SRC(0,2)=SRC(2,3)=SRC(4,4)=SRC(6,5)= (l1 + l2 + 1) >> 1;
  2481. SRC(1,2)=SRC(3,3)=SRC(5,4)=SRC(7,5)= (l0 + 2*l1 + l2 + 2) >> 2;
  2482. SRC(0,1)=SRC(2,2)=SRC(4,3)=SRC(6,4)= (l0 + l1 + 1) >> 1;
  2483. SRC(1,1)=SRC(3,2)=SRC(5,3)=SRC(7,4)= (lt + 2*l0 + l1 + 2) >> 2;
  2484. SRC(0,0)=SRC(2,1)=SRC(4,2)=SRC(6,3)= (lt + l0 + 1) >> 1;
  2485. SRC(1,0)=SRC(3,1)=SRC(5,2)=SRC(7,3)= (l0 + 2*lt + t0 + 2) >> 2;
  2486. SRC(2,0)=SRC(4,1)=SRC(6,2)= (t1 + 2*t0 + lt + 2) >> 2;
  2487. SRC(3,0)=SRC(5,1)=SRC(7,2)= (t2 + 2*t1 + t0 + 2) >> 2;
  2488. SRC(4,0)=SRC(6,1)= (t3 + 2*t2 + t1 + 2) >> 2;
  2489. SRC(5,0)=SRC(7,1)= (t4 + 2*t3 + t2 + 2) >> 2;
  2490. SRC(6,0)= (t5 + 2*t4 + t3 + 2) >> 2;
  2491. SRC(7,0)= (t6 + 2*t5 + t4 + 2) >> 2;
  2492. }
  2493. static void pred8x8l_vertical_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2494. {
  2495. PREDICT_8x8_LOAD_TOP;
  2496. PREDICT_8x8_LOAD_TOPRIGHT;
  2497. SRC(0,0)= (t0 + t1 + 1) >> 1;
  2498. SRC(0,1)= (t0 + 2*t1 + t2 + 2) >> 2;
  2499. SRC(0,2)=SRC(1,0)= (t1 + t2 + 1) >> 1;
  2500. SRC(0,3)=SRC(1,1)= (t1 + 2*t2 + t3 + 2) >> 2;
  2501. SRC(0,4)=SRC(1,2)=SRC(2,0)= (t2 + t3 + 1) >> 1;
  2502. SRC(0,5)=SRC(1,3)=SRC(2,1)= (t2 + 2*t3 + t4 + 2) >> 2;
  2503. SRC(0,6)=SRC(1,4)=SRC(2,2)=SRC(3,0)= (t3 + t4 + 1) >> 1;
  2504. SRC(0,7)=SRC(1,5)=SRC(2,3)=SRC(3,1)= (t3 + 2*t4 + t5 + 2) >> 2;
  2505. SRC(1,6)=SRC(2,4)=SRC(3,2)=SRC(4,0)= (t4 + t5 + 1) >> 1;
  2506. SRC(1,7)=SRC(2,5)=SRC(3,3)=SRC(4,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2507. SRC(2,6)=SRC(3,4)=SRC(4,2)=SRC(5,0)= (t5 + t6 + 1) >> 1;
  2508. SRC(2,7)=SRC(3,5)=SRC(4,3)=SRC(5,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2509. SRC(3,6)=SRC(4,4)=SRC(5,2)=SRC(6,0)= (t6 + t7 + 1) >> 1;
  2510. SRC(3,7)=SRC(4,5)=SRC(5,3)=SRC(6,1)= (t6 + 2*t7 + t8 + 2) >> 2;
  2511. SRC(4,6)=SRC(5,4)=SRC(6,2)=SRC(7,0)= (t7 + t8 + 1) >> 1;
  2512. SRC(4,7)=SRC(5,5)=SRC(6,3)=SRC(7,1)= (t7 + 2*t8 + t9 + 2) >> 2;
  2513. SRC(5,6)=SRC(6,4)=SRC(7,2)= (t8 + t9 + 1) >> 1;
  2514. SRC(5,7)=SRC(6,5)=SRC(7,3)= (t8 + 2*t9 + t10 + 2) >> 2;
  2515. SRC(6,6)=SRC(7,4)= (t9 + t10 + 1) >> 1;
  2516. SRC(6,7)=SRC(7,5)= (t9 + 2*t10 + t11 + 2) >> 2;
  2517. SRC(7,6)= (t10 + t11 + 1) >> 1;
  2518. SRC(7,7)= (t10 + 2*t11 + t12 + 2) >> 2;
  2519. }
  2520. static void pred8x8l_horizontal_up_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2521. {
  2522. PREDICT_8x8_LOAD_LEFT;
  2523. SRC(0,0)= (l0 + l1 + 1) >> 1;
  2524. SRC(1,0)= (l0 + 2*l1 + l2 + 2) >> 2;
  2525. SRC(0,1)=SRC(2,0)= (l1 + l2 + 1) >> 1;
  2526. SRC(1,1)=SRC(3,0)= (l1 + 2*l2 + l3 + 2) >> 2;
  2527. SRC(0,2)=SRC(2,1)=SRC(4,0)= (l2 + l3 + 1) >> 1;
  2528. SRC(1,2)=SRC(3,1)=SRC(5,0)= (l2 + 2*l3 + l4 + 2) >> 2;
  2529. SRC(0,3)=SRC(2,2)=SRC(4,1)=SRC(6,0)= (l3 + l4 + 1) >> 1;
  2530. SRC(1,3)=SRC(3,2)=SRC(5,1)=SRC(7,0)= (l3 + 2*l4 + l5 + 2) >> 2;
  2531. SRC(0,4)=SRC(2,3)=SRC(4,2)=SRC(6,1)= (l4 + l5 + 1) >> 1;
  2532. SRC(1,4)=SRC(3,3)=SRC(5,2)=SRC(7,1)= (l4 + 2*l5 + l6 + 2) >> 2;
  2533. SRC(0,5)=SRC(2,4)=SRC(4,3)=SRC(6,2)= (l5 + l6 + 1) >> 1;
  2534. SRC(1,5)=SRC(3,4)=SRC(5,3)=SRC(7,2)= (l5 + 2*l6 + l7 + 2) >> 2;
  2535. SRC(0,6)=SRC(2,5)=SRC(4,4)=SRC(6,3)= (l6 + l7 + 1) >> 1;
  2536. SRC(1,6)=SRC(3,5)=SRC(5,4)=SRC(7,3)= (l6 + 3*l7 + 2) >> 2;
  2537. SRC(0,7)=SRC(1,7)=SRC(2,6)=SRC(2,7)=SRC(3,6)=
  2538. SRC(3,7)=SRC(4,5)=SRC(4,6)=SRC(4,7)=SRC(5,5)=
  2539. SRC(5,6)=SRC(5,7)=SRC(6,4)=SRC(6,5)=SRC(6,6)=
  2540. SRC(6,7)=SRC(7,4)=SRC(7,5)=SRC(7,6)=SRC(7,7)= l7;
  2541. }
  2542. #undef PREDICT_8x8_LOAD_LEFT
  2543. #undef PREDICT_8x8_LOAD_TOP
  2544. #undef PREDICT_8x8_LOAD_TOPLEFT
  2545. #undef PREDICT_8x8_LOAD_TOPRIGHT
  2546. #undef PREDICT_8x8_DC
  2547. #undef PTR
  2548. #undef PT
  2549. #undef PL
  2550. #undef SRC
  2551. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  2552. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2553. int src_x_offset, int src_y_offset,
  2554. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  2555. MpegEncContext * const s = &h->s;
  2556. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  2557. int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  2558. const int luma_xy= (mx&3) + ((my&3)<<2);
  2559. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
  2560. uint8_t * src_cb, * src_cr;
  2561. int extra_width= h->emu_edge_width;
  2562. int extra_height= h->emu_edge_height;
  2563. int emu=0;
  2564. const int full_mx= mx>>2;
  2565. const int full_my= my>>2;
  2566. const int pic_width = 16*s->mb_width;
  2567. const int pic_height = 16*s->mb_height >> MB_MBAFF;
  2568. if(!pic->data[0])
  2569. return;
  2570. if(mx&7) extra_width -= 3;
  2571. if(my&7) extra_height -= 3;
  2572. if( full_mx < 0-extra_width
  2573. || full_my < 0-extra_height
  2574. || full_mx + 16/*FIXME*/ > pic_width + extra_width
  2575. || full_my + 16/*FIXME*/ > pic_height + extra_height){
  2576. 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);
  2577. src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
  2578. emu=1;
  2579. }
  2580. qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
  2581. if(!square){
  2582. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
  2583. }
  2584. if(s->flags&CODEC_FLAG_GRAY) return;
  2585. if(MB_MBAFF){
  2586. // chroma offset when predicting from a field of opposite parity
  2587. my += 2 * ((s->mb_y & 1) - (h->ref_cache[list][scan8[n]] & 1));
  2588. emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
  2589. }
  2590. src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2591. src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2592. if(emu){
  2593. 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);
  2594. src_cb= s->edge_emu_buffer;
  2595. }
  2596. chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2597. if(emu){
  2598. 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);
  2599. src_cr= s->edge_emu_buffer;
  2600. }
  2601. chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2602. }
  2603. static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
  2604. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2605. int x_offset, int y_offset,
  2606. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2607. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2608. int list0, int list1){
  2609. MpegEncContext * const s = &h->s;
  2610. qpel_mc_func *qpix_op= qpix_put;
  2611. h264_chroma_mc_func chroma_op= chroma_put;
  2612. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2613. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2614. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2615. x_offset += 8*s->mb_x;
  2616. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2617. if(list0){
  2618. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  2619. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  2620. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2621. qpix_op, chroma_op);
  2622. qpix_op= qpix_avg;
  2623. chroma_op= chroma_avg;
  2624. }
  2625. if(list1){
  2626. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  2627. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  2628. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2629. qpix_op, chroma_op);
  2630. }
  2631. }
  2632. static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
  2633. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2634. int x_offset, int y_offset,
  2635. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2636. h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
  2637. h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
  2638. int list0, int list1){
  2639. MpegEncContext * const s = &h->s;
  2640. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2641. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2642. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2643. x_offset += 8*s->mb_x;
  2644. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2645. if(list0 && list1){
  2646. /* don't optimize for luma-only case, since B-frames usually
  2647. * use implicit weights => chroma too. */
  2648. uint8_t *tmp_cb = s->obmc_scratchpad;
  2649. uint8_t *tmp_cr = s->obmc_scratchpad + 8;
  2650. uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
  2651. int refn0 = h->ref_cache[0][ scan8[n] ];
  2652. int refn1 = h->ref_cache[1][ scan8[n] ];
  2653. mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
  2654. dest_y, dest_cb, dest_cr,
  2655. x_offset, y_offset, qpix_put, chroma_put);
  2656. mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
  2657. tmp_y, tmp_cb, tmp_cr,
  2658. x_offset, y_offset, qpix_put, chroma_put);
  2659. if(h->use_weight == 2){
  2660. int weight0 = h->implicit_weight[refn0][refn1];
  2661. int weight1 = 64 - weight0;
  2662. luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
  2663. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2664. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2665. }else{
  2666. luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
  2667. h->luma_weight[0][refn0], h->luma_weight[1][refn1],
  2668. h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
  2669. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2670. h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
  2671. h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
  2672. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2673. h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
  2674. h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
  2675. }
  2676. }else{
  2677. int list = list1 ? 1 : 0;
  2678. int refn = h->ref_cache[list][ scan8[n] ];
  2679. Picture *ref= &h->ref_list[list][refn];
  2680. mc_dir_part(h, ref, n, square, chroma_height, delta, list,
  2681. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2682. qpix_put, chroma_put);
  2683. luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
  2684. h->luma_weight[list][refn], h->luma_offset[list][refn]);
  2685. if(h->use_weight_chroma){
  2686. chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2687. h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
  2688. chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2689. h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
  2690. }
  2691. }
  2692. }
  2693. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  2694. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2695. int x_offset, int y_offset,
  2696. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2697. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2698. h264_weight_func *weight_op, h264_biweight_func *weight_avg,
  2699. int list0, int list1){
  2700. if((h->use_weight==2 && list0 && list1
  2701. && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
  2702. || h->use_weight==1)
  2703. mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2704. x_offset, y_offset, qpix_put, chroma_put,
  2705. weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
  2706. else
  2707. mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2708. x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
  2709. }
  2710. static inline void prefetch_motion(H264Context *h, int list){
  2711. /* fetch pixels for estimated mv 4 macroblocks ahead
  2712. * optimized for 64byte cache lines */
  2713. MpegEncContext * const s = &h->s;
  2714. const int refn = h->ref_cache[list][scan8[0]];
  2715. if(refn >= 0){
  2716. const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
  2717. const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
  2718. uint8_t **src= h->ref_list[list][refn].data;
  2719. int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
  2720. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  2721. off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
  2722. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  2723. }
  2724. }
  2725. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2726. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  2727. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
  2728. h264_weight_func *weight_op, h264_biweight_func *weight_avg){
  2729. MpegEncContext * const s = &h->s;
  2730. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2731. const int mb_type= s->current_picture.mb_type[mb_xy];
  2732. assert(IS_INTER(mb_type));
  2733. prefetch_motion(h, 0);
  2734. if(IS_16X16(mb_type)){
  2735. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  2736. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  2737. &weight_op[0], &weight_avg[0],
  2738. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2739. }else if(IS_16X8(mb_type)){
  2740. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  2741. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2742. &weight_op[1], &weight_avg[1],
  2743. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2744. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  2745. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2746. &weight_op[1], &weight_avg[1],
  2747. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2748. }else if(IS_8X16(mb_type)){
  2749. mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
  2750. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2751. &weight_op[2], &weight_avg[2],
  2752. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2753. mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
  2754. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2755. &weight_op[2], &weight_avg[2],
  2756. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2757. }else{
  2758. int i;
  2759. assert(IS_8X8(mb_type));
  2760. for(i=0; i<4; i++){
  2761. const int sub_mb_type= h->sub_mb_type[i];
  2762. const int n= 4*i;
  2763. int x_offset= (i&1)<<2;
  2764. int y_offset= (i&2)<<1;
  2765. if(IS_SUB_8X8(sub_mb_type)){
  2766. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2767. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2768. &weight_op[3], &weight_avg[3],
  2769. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2770. }else if(IS_SUB_8X4(sub_mb_type)){
  2771. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2772. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2773. &weight_op[4], &weight_avg[4],
  2774. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2775. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  2776. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2777. &weight_op[4], &weight_avg[4],
  2778. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2779. }else if(IS_SUB_4X8(sub_mb_type)){
  2780. mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2781. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2782. &weight_op[5], &weight_avg[5],
  2783. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2784. mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
  2785. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2786. &weight_op[5], &weight_avg[5],
  2787. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2788. }else{
  2789. int j;
  2790. assert(IS_SUB_4X4(sub_mb_type));
  2791. for(j=0; j<4; j++){
  2792. int sub_x_offset= x_offset + 2*(j&1);
  2793. int sub_y_offset= y_offset + (j&2);
  2794. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  2795. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2796. &weight_op[6], &weight_avg[6],
  2797. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2798. }
  2799. }
  2800. }
  2801. }
  2802. prefetch_motion(h, 1);
  2803. }
  2804. static void decode_init_vlc(){
  2805. static int done = 0;
  2806. if (!done) {
  2807. int i;
  2808. done = 1;
  2809. init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
  2810. &chroma_dc_coeff_token_len [0], 1, 1,
  2811. &chroma_dc_coeff_token_bits[0], 1, 1, 1);
  2812. for(i=0; i<4; i++){
  2813. init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
  2814. &coeff_token_len [i][0], 1, 1,
  2815. &coeff_token_bits[i][0], 1, 1, 1);
  2816. }
  2817. for(i=0; i<3; i++){
  2818. init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
  2819. &chroma_dc_total_zeros_len [i][0], 1, 1,
  2820. &chroma_dc_total_zeros_bits[i][0], 1, 1, 1);
  2821. }
  2822. for(i=0; i<15; i++){
  2823. init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
  2824. &total_zeros_len [i][0], 1, 1,
  2825. &total_zeros_bits[i][0], 1, 1, 1);
  2826. }
  2827. for(i=0; i<6; i++){
  2828. init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
  2829. &run_len [i][0], 1, 1,
  2830. &run_bits[i][0], 1, 1, 1);
  2831. }
  2832. init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
  2833. &run_len [6][0], 1, 1,
  2834. &run_bits[6][0], 1, 1, 1);
  2835. }
  2836. }
  2837. /**
  2838. * Sets the intra prediction function pointers.
  2839. */
  2840. static void init_pred_ptrs(H264Context *h){
  2841. // MpegEncContext * const s = &h->s;
  2842. h->pred4x4[VERT_PRED ]= pred4x4_vertical_c;
  2843. h->pred4x4[HOR_PRED ]= pred4x4_horizontal_c;
  2844. h->pred4x4[DC_PRED ]= pred4x4_dc_c;
  2845. h->pred4x4[DIAG_DOWN_LEFT_PRED ]= pred4x4_down_left_c;
  2846. h->pred4x4[DIAG_DOWN_RIGHT_PRED]= pred4x4_down_right_c;
  2847. h->pred4x4[VERT_RIGHT_PRED ]= pred4x4_vertical_right_c;
  2848. h->pred4x4[HOR_DOWN_PRED ]= pred4x4_horizontal_down_c;
  2849. h->pred4x4[VERT_LEFT_PRED ]= pred4x4_vertical_left_c;
  2850. h->pred4x4[HOR_UP_PRED ]= pred4x4_horizontal_up_c;
  2851. h->pred4x4[LEFT_DC_PRED ]= pred4x4_left_dc_c;
  2852. h->pred4x4[TOP_DC_PRED ]= pred4x4_top_dc_c;
  2853. h->pred4x4[DC_128_PRED ]= pred4x4_128_dc_c;
  2854. h->pred8x8l[VERT_PRED ]= pred8x8l_vertical_c;
  2855. h->pred8x8l[HOR_PRED ]= pred8x8l_horizontal_c;
  2856. h->pred8x8l[DC_PRED ]= pred8x8l_dc_c;
  2857. h->pred8x8l[DIAG_DOWN_LEFT_PRED ]= pred8x8l_down_left_c;
  2858. h->pred8x8l[DIAG_DOWN_RIGHT_PRED]= pred8x8l_down_right_c;
  2859. h->pred8x8l[VERT_RIGHT_PRED ]= pred8x8l_vertical_right_c;
  2860. h->pred8x8l[HOR_DOWN_PRED ]= pred8x8l_horizontal_down_c;
  2861. h->pred8x8l[VERT_LEFT_PRED ]= pred8x8l_vertical_left_c;
  2862. h->pred8x8l[HOR_UP_PRED ]= pred8x8l_horizontal_up_c;
  2863. h->pred8x8l[LEFT_DC_PRED ]= pred8x8l_left_dc_c;
  2864. h->pred8x8l[TOP_DC_PRED ]= pred8x8l_top_dc_c;
  2865. h->pred8x8l[DC_128_PRED ]= pred8x8l_128_dc_c;
  2866. h->pred8x8[DC_PRED8x8 ]= pred8x8_dc_c;
  2867. h->pred8x8[VERT_PRED8x8 ]= pred8x8_vertical_c;
  2868. h->pred8x8[HOR_PRED8x8 ]= pred8x8_horizontal_c;
  2869. h->pred8x8[PLANE_PRED8x8 ]= pred8x8_plane_c;
  2870. h->pred8x8[LEFT_DC_PRED8x8]= pred8x8_left_dc_c;
  2871. h->pred8x8[TOP_DC_PRED8x8 ]= pred8x8_top_dc_c;
  2872. h->pred8x8[DC_128_PRED8x8 ]= pred8x8_128_dc_c;
  2873. h->pred16x16[DC_PRED8x8 ]= pred16x16_dc_c;
  2874. h->pred16x16[VERT_PRED8x8 ]= pred16x16_vertical_c;
  2875. h->pred16x16[HOR_PRED8x8 ]= pred16x16_horizontal_c;
  2876. h->pred16x16[PLANE_PRED8x8 ]= pred16x16_plane_c;
  2877. h->pred16x16[LEFT_DC_PRED8x8]= pred16x16_left_dc_c;
  2878. h->pred16x16[TOP_DC_PRED8x8 ]= pred16x16_top_dc_c;
  2879. h->pred16x16[DC_128_PRED8x8 ]= pred16x16_128_dc_c;
  2880. }
  2881. static void free_tables(H264Context *h){
  2882. av_freep(&h->intra4x4_pred_mode);
  2883. av_freep(&h->chroma_pred_mode_table);
  2884. av_freep(&h->cbp_table);
  2885. av_freep(&h->mvd_table[0]);
  2886. av_freep(&h->mvd_table[1]);
  2887. av_freep(&h->direct_table);
  2888. av_freep(&h->non_zero_count);
  2889. av_freep(&h->slice_table_base);
  2890. av_freep(&h->top_borders[1]);
  2891. av_freep(&h->top_borders[0]);
  2892. h->slice_table= NULL;
  2893. av_freep(&h->mb2b_xy);
  2894. av_freep(&h->mb2b8_xy);
  2895. av_freep(&h->s.obmc_scratchpad);
  2896. }
  2897. static void init_dequant8_coeff_table(H264Context *h){
  2898. int i,q,x;
  2899. const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
  2900. h->dequant8_coeff[0] = h->dequant8_buffer[0];
  2901. h->dequant8_coeff[1] = h->dequant8_buffer[1];
  2902. for(i=0; i<2; i++ ){
  2903. if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
  2904. h->dequant8_coeff[1] = h->dequant8_buffer[0];
  2905. break;
  2906. }
  2907. for(q=0; q<52; q++){
  2908. int shift = div6[q];
  2909. int idx = rem6[q];
  2910. for(x=0; x<64; x++)
  2911. h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
  2912. ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
  2913. h->pps.scaling_matrix8[i][x]) << shift;
  2914. }
  2915. }
  2916. }
  2917. static void init_dequant4_coeff_table(H264Context *h){
  2918. int i,j,q,x;
  2919. const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
  2920. for(i=0; i<6; i++ ){
  2921. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  2922. for(j=0; j<i; j++){
  2923. if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
  2924. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  2925. break;
  2926. }
  2927. }
  2928. if(j<i)
  2929. continue;
  2930. for(q=0; q<52; q++){
  2931. int shift = div6[q] + 2;
  2932. int idx = rem6[q];
  2933. for(x=0; x<16; x++)
  2934. h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
  2935. ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
  2936. h->pps.scaling_matrix4[i][x]) << shift;
  2937. }
  2938. }
  2939. }
  2940. static void init_dequant_tables(H264Context *h){
  2941. int i,x;
  2942. init_dequant4_coeff_table(h);
  2943. if(h->pps.transform_8x8_mode)
  2944. init_dequant8_coeff_table(h);
  2945. if(h->sps.transform_bypass){
  2946. for(i=0; i<6; i++)
  2947. for(x=0; x<16; x++)
  2948. h->dequant4_coeff[i][0][x] = 1<<6;
  2949. if(h->pps.transform_8x8_mode)
  2950. for(i=0; i<2; i++)
  2951. for(x=0; x<64; x++)
  2952. h->dequant8_coeff[i][0][x] = 1<<6;
  2953. }
  2954. }
  2955. /**
  2956. * allocates tables.
  2957. * needs width/height
  2958. */
  2959. static int alloc_tables(H264Context *h){
  2960. MpegEncContext * const s = &h->s;
  2961. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  2962. int x,y;
  2963. CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
  2964. CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
  2965. CHECKED_ALLOCZ(h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(uint8_t))
  2966. CHECKED_ALLOCZ(h->top_borders[0] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2967. CHECKED_ALLOCZ(h->top_borders[1] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2968. CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
  2969. if( h->pps.cabac ) {
  2970. CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
  2971. CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
  2972. CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t));
  2973. CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t));
  2974. }
  2975. memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(uint8_t));
  2976. h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
  2977. CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint32_t));
  2978. CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint32_t));
  2979. for(y=0; y<s->mb_height; y++){
  2980. for(x=0; x<s->mb_width; x++){
  2981. const int mb_xy= x + y*s->mb_stride;
  2982. const int b_xy = 4*x + 4*y*h->b_stride;
  2983. const int b8_xy= 2*x + 2*y*h->b8_stride;
  2984. h->mb2b_xy [mb_xy]= b_xy;
  2985. h->mb2b8_xy[mb_xy]= b8_xy;
  2986. }
  2987. }
  2988. s->obmc_scratchpad = NULL;
  2989. if(!h->dequant4_coeff[0])
  2990. init_dequant_tables(h);
  2991. return 0;
  2992. fail:
  2993. free_tables(h);
  2994. return -1;
  2995. }
  2996. static void common_init(H264Context *h){
  2997. MpegEncContext * const s = &h->s;
  2998. s->width = s->avctx->width;
  2999. s->height = s->avctx->height;
  3000. s->codec_id= s->avctx->codec->id;
  3001. init_pred_ptrs(h);
  3002. h->dequant_coeff_pps= -1;
  3003. s->unrestricted_mv=1;
  3004. s->decode=1; //FIXME
  3005. memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  3006. memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  3007. }
  3008. static int decode_init(AVCodecContext *avctx){
  3009. H264Context *h= avctx->priv_data;
  3010. MpegEncContext * const s = &h->s;
  3011. MPV_decode_defaults(s);
  3012. s->avctx = avctx;
  3013. common_init(h);
  3014. s->out_format = FMT_H264;
  3015. s->workaround_bugs= avctx->workaround_bugs;
  3016. // set defaults
  3017. // s->decode_mb= ff_h263_decode_mb;
  3018. s->low_delay= 1;
  3019. avctx->pix_fmt= PIX_FMT_YUV420P;
  3020. decode_init_vlc();
  3021. if(avctx->extradata_size > 0 && avctx->extradata &&
  3022. *(char *)avctx->extradata == 1){
  3023. h->is_avc = 1;
  3024. h->got_avcC = 0;
  3025. } else {
  3026. h->is_avc = 0;
  3027. }
  3028. return 0;
  3029. }
  3030. static int frame_start(H264Context *h){
  3031. MpegEncContext * const s = &h->s;
  3032. int i;
  3033. if(MPV_frame_start(s, s->avctx) < 0)
  3034. return -1;
  3035. ff_er_frame_start(s);
  3036. assert(s->linesize && s->uvlinesize);
  3037. for(i=0; i<16; i++){
  3038. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  3039. h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  3040. }
  3041. for(i=0; i<4; i++){
  3042. h->block_offset[16+i]=
  3043. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  3044. h->block_offset[24+16+i]=
  3045. h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  3046. }
  3047. /* can't be in alloc_tables because linesize isn't known there.
  3048. * FIXME: redo bipred weight to not require extra buffer? */
  3049. if(!s->obmc_scratchpad)
  3050. s->obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  3051. /* some macroblocks will be accessed before they're available */
  3052. if(FRAME_MBAFF)
  3053. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(uint8_t));
  3054. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  3055. return 0;
  3056. }
  3057. static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  3058. MpegEncContext * const s = &h->s;
  3059. int i;
  3060. src_y -= linesize;
  3061. src_cb -= uvlinesize;
  3062. src_cr -= uvlinesize;
  3063. // There are two lines saved, the line above the the top macroblock of a pair,
  3064. // and the line above the bottom macroblock
  3065. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  3066. for(i=1; i<17; i++){
  3067. h->left_border[i]= src_y[15+i* linesize];
  3068. }
  3069. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
  3070. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
  3071. if(!(s->flags&CODEC_FLAG_GRAY)){
  3072. h->left_border[17 ]= h->top_borders[0][s->mb_x][16+7];
  3073. h->left_border[17+9]= h->top_borders[0][s->mb_x][24+7];
  3074. for(i=1; i<9; i++){
  3075. h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
  3076. h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
  3077. }
  3078. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
  3079. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
  3080. }
  3081. }
  3082. 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){
  3083. MpegEncContext * const s = &h->s;
  3084. int temp8, i;
  3085. uint64_t temp64;
  3086. int deblock_left = (s->mb_x > 0);
  3087. int deblock_top = (s->mb_y > 0);
  3088. src_y -= linesize + 1;
  3089. src_cb -= uvlinesize + 1;
  3090. src_cr -= uvlinesize + 1;
  3091. #define XCHG(a,b,t,xchg)\
  3092. t= a;\
  3093. if(xchg)\
  3094. a= b;\
  3095. b= t;
  3096. if(deblock_left){
  3097. for(i = !deblock_top; i<17; i++){
  3098. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  3099. }
  3100. }
  3101. if(deblock_top){
  3102. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  3103. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  3104. if(s->mb_x+1 < s->mb_width){
  3105. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  3106. }
  3107. }
  3108. if(!(s->flags&CODEC_FLAG_GRAY)){
  3109. if(deblock_left){
  3110. for(i = !deblock_top; i<9; i++){
  3111. XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
  3112. XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
  3113. }
  3114. }
  3115. if(deblock_top){
  3116. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  3117. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  3118. }
  3119. }
  3120. }
  3121. static inline void backup_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  3122. MpegEncContext * const s = &h->s;
  3123. int i;
  3124. src_y -= 2 * linesize;
  3125. src_cb -= 2 * uvlinesize;
  3126. src_cr -= 2 * uvlinesize;
  3127. // There are two lines saved, the line above the the top macroblock of a pair,
  3128. // and the line above the bottom macroblock
  3129. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  3130. h->left_border[1]= h->top_borders[1][s->mb_x][15];
  3131. for(i=2; i<34; i++){
  3132. h->left_border[i]= src_y[15+i* linesize];
  3133. }
  3134. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 32*linesize);
  3135. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+32*linesize);
  3136. *(uint64_t*)(h->top_borders[1][s->mb_x]+0)= *(uint64_t*)(src_y + 33*linesize);
  3137. *(uint64_t*)(h->top_borders[1][s->mb_x]+8)= *(uint64_t*)(src_y +8+33*linesize);
  3138. if(!(s->flags&CODEC_FLAG_GRAY)){
  3139. h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7];
  3140. h->left_border[34+ 1]= h->top_borders[1][s->mb_x][16+7];
  3141. h->left_border[34+18 ]= h->top_borders[0][s->mb_x][24+7];
  3142. h->left_border[34+18+1]= h->top_borders[1][s->mb_x][24+7];
  3143. for(i=2; i<18; i++){
  3144. h->left_border[i+34 ]= src_cb[7+i*uvlinesize];
  3145. h->left_border[i+34+18]= src_cr[7+i*uvlinesize];
  3146. }
  3147. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+16*uvlinesize);
  3148. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+16*uvlinesize);
  3149. *(uint64_t*)(h->top_borders[1][s->mb_x]+16)= *(uint64_t*)(src_cb+17*uvlinesize);
  3150. *(uint64_t*)(h->top_borders[1][s->mb_x]+24)= *(uint64_t*)(src_cr+17*uvlinesize);
  3151. }
  3152. }
  3153. 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){
  3154. MpegEncContext * const s = &h->s;
  3155. int temp8, i;
  3156. uint64_t temp64;
  3157. int deblock_left = (s->mb_x > 0);
  3158. int deblock_top = (s->mb_y > 1);
  3159. 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);
  3160. src_y -= 2 * linesize + 1;
  3161. src_cb -= 2 * uvlinesize + 1;
  3162. src_cr -= 2 * uvlinesize + 1;
  3163. #define XCHG(a,b,t,xchg)\
  3164. t= a;\
  3165. if(xchg)\
  3166. a= b;\
  3167. b= t;
  3168. if(deblock_left){
  3169. for(i = (!deblock_top)<<1; i<34; i++){
  3170. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  3171. }
  3172. }
  3173. if(deblock_top){
  3174. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  3175. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  3176. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+0), *(uint64_t*)(src_y +1 +linesize), temp64, xchg);
  3177. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+8), *(uint64_t*)(src_y +9 +linesize), temp64, 1);
  3178. if(s->mb_x+1 < s->mb_width){
  3179. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  3180. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x+1]), *(uint64_t*)(src_y +17 +linesize), temp64, 1);
  3181. }
  3182. }
  3183. if(!(s->flags&CODEC_FLAG_GRAY)){
  3184. if(deblock_left){
  3185. for(i = (!deblock_top) << 1; i<18; i++){
  3186. XCHG(h->left_border[i+34 ], src_cb[i*uvlinesize], temp8, xchg);
  3187. XCHG(h->left_border[i+34+18], src_cr[i*uvlinesize], temp8, xchg);
  3188. }
  3189. }
  3190. if(deblock_top){
  3191. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  3192. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  3193. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+16), *(uint64_t*)(src_cb+1 +uvlinesize), temp64, 1);
  3194. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+24), *(uint64_t*)(src_cr+1 +uvlinesize), temp64, 1);
  3195. }
  3196. }
  3197. }
  3198. static void hl_decode_mb(H264Context *h){
  3199. MpegEncContext * const s = &h->s;
  3200. const int mb_x= s->mb_x;
  3201. const int mb_y= s->mb_y;
  3202. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3203. const int mb_type= s->current_picture.mb_type[mb_xy];
  3204. uint8_t *dest_y, *dest_cb, *dest_cr;
  3205. int linesize, uvlinesize /*dct_offset*/;
  3206. int i;
  3207. int *block_offset = &h->block_offset[0];
  3208. const unsigned int bottom = mb_y & 1;
  3209. const int transform_bypass = (s->qscale == 0 && h->sps.transform_bypass);
  3210. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  3211. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  3212. if(!s->decode)
  3213. return;
  3214. dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3215. dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3216. dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3217. s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
  3218. s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
  3219. if (MB_FIELD) {
  3220. linesize = h->mb_linesize = s->linesize * 2;
  3221. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  3222. block_offset = &h->block_offset[24];
  3223. if(mb_y&1){ //FIXME move out of this func?
  3224. dest_y -= s->linesize*15;
  3225. dest_cb-= s->uvlinesize*7;
  3226. dest_cr-= s->uvlinesize*7;
  3227. }
  3228. if(FRAME_MBAFF) {
  3229. int list;
  3230. for(list=0; list<2; list++){
  3231. if(!USES_LIST(mb_type, list))
  3232. continue;
  3233. if(IS_16X16(mb_type)){
  3234. int8_t *ref = &h->ref_cache[list][scan8[0]];
  3235. fill_rectangle(ref, 4, 4, 8, 16+*ref^(s->mb_y&1), 1);
  3236. }else{
  3237. for(i=0; i<16; i+=4){
  3238. //FIXME can refs be smaller than 8x8 when !direct_8x8_inference ?
  3239. int ref = h->ref_cache[list][scan8[i]];
  3240. if(ref >= 0)
  3241. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, 16+ref^(s->mb_y&1), 1);
  3242. }
  3243. }
  3244. }
  3245. }
  3246. } else {
  3247. linesize = h->mb_linesize = s->linesize;
  3248. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  3249. // dct_offset = s->linesize * 16;
  3250. }
  3251. if(transform_bypass){
  3252. idct_dc_add =
  3253. idct_add = IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  3254. }else if(IS_8x8DCT(mb_type)){
  3255. idct_dc_add = s->dsp.h264_idct8_dc_add;
  3256. idct_add = s->dsp.h264_idct8_add;
  3257. }else{
  3258. idct_dc_add = s->dsp.h264_idct_dc_add;
  3259. idct_add = s->dsp.h264_idct_add;
  3260. }
  3261. if(FRAME_MBAFF && h->deblocking_filter && IS_INTRA(mb_type)
  3262. && (!bottom || !IS_INTRA(s->current_picture.mb_type[mb_xy-s->mb_stride]))){
  3263. int mbt_y = mb_y&~1;
  3264. uint8_t *top_y = s->current_picture.data[0] + (mbt_y * 16* s->linesize ) + mb_x * 16;
  3265. uint8_t *top_cb = s->current_picture.data[1] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  3266. uint8_t *top_cr = s->current_picture.data[2] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  3267. xchg_pair_border(h, top_y, top_cb, top_cr, s->linesize, s->uvlinesize, 1);
  3268. }
  3269. if (IS_INTRA_PCM(mb_type)) {
  3270. unsigned int x, y;
  3271. // The pixels are stored in h->mb array in the same order as levels,
  3272. // copy them in output in the correct order.
  3273. for(i=0; i<16; i++) {
  3274. for (y=0; y<4; y++) {
  3275. for (x=0; x<4; x++) {
  3276. *(dest_y + block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x];
  3277. }
  3278. }
  3279. }
  3280. for(i=16; i<16+4; i++) {
  3281. for (y=0; y<4; y++) {
  3282. for (x=0; x<4; x++) {
  3283. *(dest_cb + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  3284. }
  3285. }
  3286. }
  3287. for(i=20; i<20+4; i++) {
  3288. for (y=0; y<4; y++) {
  3289. for (x=0; x<4; x++) {
  3290. *(dest_cr + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  3291. }
  3292. }
  3293. }
  3294. } else {
  3295. if(IS_INTRA(mb_type)){
  3296. if(h->deblocking_filter && !FRAME_MBAFF)
  3297. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1);
  3298. if(!(s->flags&CODEC_FLAG_GRAY)){
  3299. h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  3300. h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  3301. }
  3302. if(IS_INTRA4x4(mb_type)){
  3303. if(!s->encoding){
  3304. if(IS_8x8DCT(mb_type)){
  3305. for(i=0; i<16; i+=4){
  3306. uint8_t * const ptr= dest_y + block_offset[i];
  3307. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  3308. const int nnz = h->non_zero_count_cache[ scan8[i] ];
  3309. h->pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  3310. (h->topright_samples_available<<(i+1))&0x8000, linesize);
  3311. if(nnz){
  3312. if(nnz == 1 && h->mb[i*16])
  3313. idct_dc_add(ptr, h->mb + i*16, linesize);
  3314. else
  3315. idct_add(ptr, h->mb + i*16, linesize);
  3316. }
  3317. }
  3318. }else
  3319. for(i=0; i<16; i++){
  3320. uint8_t * const ptr= dest_y + block_offset[i];
  3321. uint8_t *topright;
  3322. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  3323. int nnz, tr;
  3324. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  3325. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  3326. assert(mb_y || linesize <= block_offset[i]);
  3327. if(!topright_avail){
  3328. tr= ptr[3 - linesize]*0x01010101;
  3329. topright= (uint8_t*) &tr;
  3330. }else
  3331. topright= ptr + 4 - linesize;
  3332. }else
  3333. topright= NULL;
  3334. h->pred4x4[ dir ](ptr, topright, linesize);
  3335. nnz = h->non_zero_count_cache[ scan8[i] ];
  3336. if(nnz){
  3337. if(s->codec_id == CODEC_ID_H264){
  3338. if(nnz == 1 && h->mb[i*16])
  3339. idct_dc_add(ptr, h->mb + i*16, linesize);
  3340. else
  3341. idct_add(ptr, h->mb + i*16, linesize);
  3342. }else
  3343. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  3344. }
  3345. }
  3346. }
  3347. }else{
  3348. h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  3349. if(s->codec_id == CODEC_ID_H264){
  3350. if(!transform_bypass)
  3351. h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[IS_INTRA(mb_type) ? 0:3][s->qscale][0]);
  3352. }else
  3353. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  3354. }
  3355. if(h->deblocking_filter && !FRAME_MBAFF)
  3356. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
  3357. }else if(s->codec_id == CODEC_ID_H264){
  3358. hl_motion(h, dest_y, dest_cb, dest_cr,
  3359. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  3360. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  3361. s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
  3362. }
  3363. if(!IS_INTRA4x4(mb_type)){
  3364. if(s->codec_id == CODEC_ID_H264){
  3365. if(IS_INTRA16x16(mb_type)){
  3366. for(i=0; i<16; i++){
  3367. if(h->non_zero_count_cache[ scan8[i] ])
  3368. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3369. else if(h->mb[i*16])
  3370. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3371. }
  3372. }else{
  3373. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  3374. for(i=0; i<16; i+=di){
  3375. int nnz = h->non_zero_count_cache[ scan8[i] ];
  3376. if(nnz){
  3377. if(nnz==1 && h->mb[i*16])
  3378. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3379. else
  3380. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3381. }
  3382. }
  3383. }
  3384. }else{
  3385. for(i=0; i<16; i++){
  3386. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  3387. uint8_t * const ptr= dest_y + block_offset[i];
  3388. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  3389. }
  3390. }
  3391. }
  3392. }
  3393. if(!(s->flags&CODEC_FLAG_GRAY)){
  3394. uint8_t *dest[2] = {dest_cb, dest_cr};
  3395. if(transform_bypass){
  3396. idct_add = idct_dc_add = s->dsp.add_pixels4;
  3397. }else{
  3398. idct_add = s->dsp.h264_idct_add;
  3399. idct_dc_add = s->dsp.h264_idct_dc_add;
  3400. 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]);
  3401. 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]);
  3402. }
  3403. if(s->codec_id == CODEC_ID_H264){
  3404. for(i=16; i<16+8; i++){
  3405. if(h->non_zero_count_cache[ scan8[i] ])
  3406. idct_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3407. else if(h->mb[i*16])
  3408. idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3409. }
  3410. }else{
  3411. for(i=16; i<16+8; i++){
  3412. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  3413. uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
  3414. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  3415. }
  3416. }
  3417. }
  3418. }
  3419. }
  3420. if(h->deblocking_filter) {
  3421. if (FRAME_MBAFF) {
  3422. //FIXME try deblocking one mb at a time?
  3423. // the reduction in load/storing mvs and such might outweigh the extra backup/xchg_border
  3424. const int mb_y = s->mb_y - 1;
  3425. uint8_t *pair_dest_y, *pair_dest_cb, *pair_dest_cr;
  3426. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3427. const int mb_type_top = s->current_picture.mb_type[mb_xy];
  3428. const int mb_type_bottom= s->current_picture.mb_type[mb_xy+s->mb_stride];
  3429. if (!bottom) return;
  3430. pair_dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3431. pair_dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3432. pair_dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3433. if(IS_INTRA(mb_type_top | mb_type_bottom))
  3434. xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0);
  3435. backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize);
  3436. // deblock a pair
  3437. // top
  3438. s->mb_y--;
  3439. 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);
  3440. fill_caches(h, mb_type_top, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3441. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy]);
  3442. filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize);
  3443. // bottom
  3444. s->mb_y++;
  3445. tprintf("call mbaff filter_mb\n");
  3446. fill_caches(h, mb_type_bottom, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3447. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy+s->mb_stride]);
  3448. filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3449. } else {
  3450. tprintf("call filter_mb\n");
  3451. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3452. fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3453. filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3454. }
  3455. }
  3456. }
  3457. /**
  3458. * fills the default_ref_list.
  3459. */
  3460. static int fill_default_ref_list(H264Context *h){
  3461. MpegEncContext * const s = &h->s;
  3462. int i;
  3463. int smallest_poc_greater_than_current = -1;
  3464. Picture sorted_short_ref[32];
  3465. if(h->slice_type==B_TYPE){
  3466. int out_i;
  3467. int limit= INT_MIN;
  3468. /* sort frame according to poc in B slice */
  3469. for(out_i=0; out_i<h->short_ref_count; out_i++){
  3470. int best_i=INT_MIN;
  3471. int best_poc=INT_MAX;
  3472. for(i=0; i<h->short_ref_count; i++){
  3473. const int poc= h->short_ref[i]->poc;
  3474. if(poc > limit && poc < best_poc){
  3475. best_poc= poc;
  3476. best_i= i;
  3477. }
  3478. }
  3479. assert(best_i != INT_MIN);
  3480. limit= best_poc;
  3481. sorted_short_ref[out_i]= *h->short_ref[best_i];
  3482. 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);
  3483. if (-1 == smallest_poc_greater_than_current) {
  3484. if (h->short_ref[best_i]->poc >= s->current_picture_ptr->poc) {
  3485. smallest_poc_greater_than_current = out_i;
  3486. }
  3487. }
  3488. }
  3489. }
  3490. if(s->picture_structure == PICT_FRAME){
  3491. if(h->slice_type==B_TYPE){
  3492. int list;
  3493. tprintf("current poc: %d, smallest_poc_greater_than_current: %d\n", s->current_picture_ptr->poc, smallest_poc_greater_than_current);
  3494. // find the largest poc
  3495. for(list=0; list<2; list++){
  3496. int index = 0;
  3497. int j= -99;
  3498. int step= list ? -1 : 1;
  3499. for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++, j+=step) {
  3500. while(j<0 || j>= h->short_ref_count){
  3501. if(j != -99 && step == (list ? -1 : 1))
  3502. return -1;
  3503. step = -step;
  3504. j= smallest_poc_greater_than_current + (step>>1);
  3505. }
  3506. if(sorted_short_ref[j].reference != 3) continue;
  3507. h->default_ref_list[list][index ]= sorted_short_ref[j];
  3508. h->default_ref_list[list][index++].pic_id= sorted_short_ref[j].frame_num;
  3509. }
  3510. for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){
  3511. if(h->long_ref[i] == NULL) continue;
  3512. if(h->long_ref[i]->reference != 3) continue;
  3513. h->default_ref_list[ list ][index ]= *h->long_ref[i];
  3514. h->default_ref_list[ list ][index++].pic_id= i;;
  3515. }
  3516. if(list && (smallest_poc_greater_than_current<=0 || smallest_poc_greater_than_current>=h->short_ref_count) && (1 < index)){
  3517. // swap the two first elements of L1 when
  3518. // L0 and L1 are identical
  3519. Picture temp= h->default_ref_list[1][0];
  3520. h->default_ref_list[1][0] = h->default_ref_list[1][1];
  3521. h->default_ref_list[1][1] = temp;
  3522. }
  3523. if(index < h->ref_count[ list ])
  3524. memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
  3525. }
  3526. }else{
  3527. int index=0;
  3528. for(i=0; i<h->short_ref_count; i++){
  3529. if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
  3530. h->default_ref_list[0][index ]= *h->short_ref[i];
  3531. h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
  3532. }
  3533. for(i = 0; i < 16; i++){
  3534. if(h->long_ref[i] == NULL) continue;
  3535. if(h->long_ref[i]->reference != 3) continue;
  3536. h->default_ref_list[0][index ]= *h->long_ref[i];
  3537. h->default_ref_list[0][index++].pic_id= i;;
  3538. }
  3539. if(index < h->ref_count[0])
  3540. memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
  3541. }
  3542. }else{ //FIELD
  3543. if(h->slice_type==B_TYPE){
  3544. }else{
  3545. //FIXME second field balh
  3546. }
  3547. }
  3548. #ifdef TRACE
  3549. for (i=0; i<h->ref_count[0]; i++) {
  3550. 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]);
  3551. }
  3552. if(h->slice_type==B_TYPE){
  3553. for (i=0; i<h->ref_count[1]; i++) {
  3554. 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]);
  3555. }
  3556. }
  3557. #endif
  3558. return 0;
  3559. }
  3560. static void print_short_term(H264Context *h);
  3561. static void print_long_term(H264Context *h);
  3562. static int decode_ref_pic_list_reordering(H264Context *h){
  3563. MpegEncContext * const s = &h->s;
  3564. int list, index;
  3565. print_short_term(h);
  3566. print_long_term(h);
  3567. if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move before func
  3568. for(list=0; list<2; list++){
  3569. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  3570. if(get_bits1(&s->gb)){
  3571. int pred= h->curr_pic_num;
  3572. for(index=0; ; index++){
  3573. int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
  3574. int pic_id;
  3575. int i;
  3576. Picture *ref = NULL;
  3577. if(reordering_of_pic_nums_idc==3)
  3578. break;
  3579. if(index >= h->ref_count[list]){
  3580. av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
  3581. return -1;
  3582. }
  3583. if(reordering_of_pic_nums_idc<3){
  3584. if(reordering_of_pic_nums_idc<2){
  3585. const int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  3586. if(abs_diff_pic_num >= h->max_pic_num){
  3587. av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  3588. return -1;
  3589. }
  3590. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  3591. else pred+= abs_diff_pic_num;
  3592. pred &= h->max_pic_num - 1;
  3593. for(i= h->short_ref_count-1; i>=0; i--){
  3594. ref = h->short_ref[i];
  3595. assert(ref->reference == 3);
  3596. assert(!ref->long_ref);
  3597. if(ref->data[0] != NULL && ref->frame_num == pred && ref->long_ref == 0) // ignore non existing pictures by testing data[0] pointer
  3598. break;
  3599. }
  3600. if(i>=0)
  3601. ref->pic_id= ref->frame_num;
  3602. }else{
  3603. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  3604. ref = h->long_ref[pic_id];
  3605. ref->pic_id= pic_id;
  3606. assert(ref->reference == 3);
  3607. assert(ref->long_ref);
  3608. i=0;
  3609. }
  3610. if (i < 0) {
  3611. av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  3612. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  3613. } else {
  3614. for(i=index; i+1<h->ref_count[list]; i++){
  3615. if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
  3616. break;
  3617. }
  3618. for(; i > index; i--){
  3619. h->ref_list[list][i]= h->ref_list[list][i-1];
  3620. }
  3621. h->ref_list[list][index]= *ref;
  3622. }
  3623. }else{
  3624. av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  3625. return -1;
  3626. }
  3627. }
  3628. }
  3629. if(h->slice_type!=B_TYPE) break;
  3630. }
  3631. for(list=0; list<2; list++){
  3632. for(index= 0; index < h->ref_count[list]; index++){
  3633. if(!h->ref_list[list][index].data[0])
  3634. h->ref_list[list][index]= s->current_picture;
  3635. }
  3636. if(h->slice_type!=B_TYPE) break;
  3637. }
  3638. if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)
  3639. direct_dist_scale_factor(h);
  3640. direct_ref_list_init(h);
  3641. return 0;
  3642. }
  3643. static void fill_mbaff_ref_list(H264Context *h){
  3644. int list, i, j;
  3645. for(list=0; list<2; list++){
  3646. for(i=0; i<h->ref_count[list]; i++){
  3647. Picture *frame = &h->ref_list[list][i];
  3648. Picture *field = &h->ref_list[list][16+2*i];
  3649. field[0] = *frame;
  3650. for(j=0; j<3; j++)
  3651. field[0].linesize[j] <<= 1;
  3652. field[1] = field[0];
  3653. for(j=0; j<3; j++)
  3654. field[1].data[j] += frame->linesize[j];
  3655. h->luma_weight[list][16+2*i] = h->luma_weight[list][16+2*i+1] = h->luma_weight[list][i];
  3656. h->luma_offset[list][16+2*i] = h->luma_offset[list][16+2*i+1] = h->luma_offset[list][i];
  3657. for(j=0; j<2; j++){
  3658. h->chroma_weight[list][16+2*i][j] = h->chroma_weight[list][16+2*i+1][j] = h->chroma_weight[list][i][j];
  3659. h->chroma_offset[list][16+2*i][j] = h->chroma_offset[list][16+2*i+1][j] = h->chroma_offset[list][i][j];
  3660. }
  3661. }
  3662. }
  3663. for(j=0; j<h->ref_count[1]; j++){
  3664. for(i=0; i<h->ref_count[0]; i++)
  3665. h->implicit_weight[j][16+2*i] = h->implicit_weight[j][16+2*i+1] = h->implicit_weight[j][i];
  3666. memcpy(h->implicit_weight[16+2*j], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3667. memcpy(h->implicit_weight[16+2*j+1], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3668. }
  3669. }
  3670. static int pred_weight_table(H264Context *h){
  3671. MpegEncContext * const s = &h->s;
  3672. int list, i;
  3673. int luma_def, chroma_def;
  3674. h->use_weight= 0;
  3675. h->use_weight_chroma= 0;
  3676. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  3677. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  3678. luma_def = 1<<h->luma_log2_weight_denom;
  3679. chroma_def = 1<<h->chroma_log2_weight_denom;
  3680. for(list=0; list<2; list++){
  3681. for(i=0; i<h->ref_count[list]; i++){
  3682. int luma_weight_flag, chroma_weight_flag;
  3683. luma_weight_flag= get_bits1(&s->gb);
  3684. if(luma_weight_flag){
  3685. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  3686. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  3687. if( h->luma_weight[list][i] != luma_def
  3688. || h->luma_offset[list][i] != 0)
  3689. h->use_weight= 1;
  3690. }else{
  3691. h->luma_weight[list][i]= luma_def;
  3692. h->luma_offset[list][i]= 0;
  3693. }
  3694. chroma_weight_flag= get_bits1(&s->gb);
  3695. if(chroma_weight_flag){
  3696. int j;
  3697. for(j=0; j<2; j++){
  3698. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  3699. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  3700. if( h->chroma_weight[list][i][j] != chroma_def
  3701. || h->chroma_offset[list][i][j] != 0)
  3702. h->use_weight_chroma= 1;
  3703. }
  3704. }else{
  3705. int j;
  3706. for(j=0; j<2; j++){
  3707. h->chroma_weight[list][i][j]= chroma_def;
  3708. h->chroma_offset[list][i][j]= 0;
  3709. }
  3710. }
  3711. }
  3712. if(h->slice_type != B_TYPE) break;
  3713. }
  3714. h->use_weight= h->use_weight || h->use_weight_chroma;
  3715. return 0;
  3716. }
  3717. static void implicit_weight_table(H264Context *h){
  3718. MpegEncContext * const s = &h->s;
  3719. int ref0, ref1;
  3720. int cur_poc = s->current_picture_ptr->poc;
  3721. if( h->ref_count[0] == 1 && h->ref_count[1] == 1
  3722. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  3723. h->use_weight= 0;
  3724. h->use_weight_chroma= 0;
  3725. return;
  3726. }
  3727. h->use_weight= 2;
  3728. h->use_weight_chroma= 2;
  3729. h->luma_log2_weight_denom= 5;
  3730. h->chroma_log2_weight_denom= 5;
  3731. for(ref0=0; ref0 < h->ref_count[0]; ref0++){
  3732. int poc0 = h->ref_list[0][ref0].poc;
  3733. for(ref1=0; ref1 < h->ref_count[1]; ref1++){
  3734. int poc1 = h->ref_list[1][ref1].poc;
  3735. int td = clip(poc1 - poc0, -128, 127);
  3736. if(td){
  3737. int tb = clip(cur_poc - poc0, -128, 127);
  3738. int tx = (16384 + (FFABS(td) >> 1)) / td;
  3739. int dist_scale_factor = clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
  3740. if(dist_scale_factor < -64 || dist_scale_factor > 128)
  3741. h->implicit_weight[ref0][ref1] = 32;
  3742. else
  3743. h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
  3744. }else
  3745. h->implicit_weight[ref0][ref1] = 32;
  3746. }
  3747. }
  3748. }
  3749. static inline void unreference_pic(H264Context *h, Picture *pic){
  3750. int i;
  3751. pic->reference=0;
  3752. if(pic == h->delayed_output_pic)
  3753. pic->reference=1;
  3754. else{
  3755. for(i = 0; h->delayed_pic[i]; i++)
  3756. if(pic == h->delayed_pic[i]){
  3757. pic->reference=1;
  3758. break;
  3759. }
  3760. }
  3761. }
  3762. /**
  3763. * instantaneous decoder refresh.
  3764. */
  3765. static void idr(H264Context *h){
  3766. int i;
  3767. for(i=0; i<16; i++){
  3768. if (h->long_ref[i] != NULL) {
  3769. unreference_pic(h, h->long_ref[i]);
  3770. h->long_ref[i]= NULL;
  3771. }
  3772. }
  3773. h->long_ref_count=0;
  3774. for(i=0; i<h->short_ref_count; i++){
  3775. unreference_pic(h, h->short_ref[i]);
  3776. h->short_ref[i]= NULL;
  3777. }
  3778. h->short_ref_count=0;
  3779. }
  3780. /* forget old pics after a seek */
  3781. static void flush_dpb(AVCodecContext *avctx){
  3782. H264Context *h= avctx->priv_data;
  3783. int i;
  3784. for(i=0; i<16; i++) {
  3785. if(h->delayed_pic[i])
  3786. h->delayed_pic[i]->reference= 0;
  3787. h->delayed_pic[i]= NULL;
  3788. }
  3789. if(h->delayed_output_pic)
  3790. h->delayed_output_pic->reference= 0;
  3791. h->delayed_output_pic= NULL;
  3792. idr(h);
  3793. if(h->s.current_picture_ptr)
  3794. h->s.current_picture_ptr->reference= 0;
  3795. }
  3796. /**
  3797. *
  3798. * @return the removed picture or NULL if an error occurs
  3799. */
  3800. static Picture * remove_short(H264Context *h, int frame_num){
  3801. MpegEncContext * const s = &h->s;
  3802. int i;
  3803. if(s->avctx->debug&FF_DEBUG_MMCO)
  3804. av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  3805. for(i=0; i<h->short_ref_count; i++){
  3806. Picture *pic= h->short_ref[i];
  3807. if(s->avctx->debug&FF_DEBUG_MMCO)
  3808. av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  3809. if(pic->frame_num == frame_num){
  3810. h->short_ref[i]= NULL;
  3811. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
  3812. h->short_ref_count--;
  3813. return pic;
  3814. }
  3815. }
  3816. return NULL;
  3817. }
  3818. /**
  3819. *
  3820. * @return the removed picture or NULL if an error occurs
  3821. */
  3822. static Picture * remove_long(H264Context *h, int i){
  3823. Picture *pic;
  3824. pic= h->long_ref[i];
  3825. h->long_ref[i]= NULL;
  3826. if(pic) h->long_ref_count--;
  3827. return pic;
  3828. }
  3829. /**
  3830. * print short term list
  3831. */
  3832. static void print_short_term(H264Context *h) {
  3833. uint32_t i;
  3834. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3835. av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
  3836. for(i=0; i<h->short_ref_count; i++){
  3837. Picture *pic= h->short_ref[i];
  3838. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3839. }
  3840. }
  3841. }
  3842. /**
  3843. * print long term list
  3844. */
  3845. static void print_long_term(H264Context *h) {
  3846. uint32_t i;
  3847. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3848. av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
  3849. for(i = 0; i < 16; i++){
  3850. Picture *pic= h->long_ref[i];
  3851. if (pic) {
  3852. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3853. }
  3854. }
  3855. }
  3856. }
  3857. /**
  3858. * Executes the reference picture marking (memory management control operations).
  3859. */
  3860. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  3861. MpegEncContext * const s = &h->s;
  3862. int i, j;
  3863. int current_is_long=0;
  3864. Picture *pic;
  3865. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  3866. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  3867. for(i=0; i<mmco_count; i++){
  3868. if(s->avctx->debug&FF_DEBUG_MMCO)
  3869. 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);
  3870. switch(mmco[i].opcode){
  3871. case MMCO_SHORT2UNUSED:
  3872. pic= remove_short(h, mmco[i].short_frame_num);
  3873. if(pic)
  3874. unreference_pic(h, pic);
  3875. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3876. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_short() failure\n");
  3877. break;
  3878. case MMCO_SHORT2LONG:
  3879. pic= remove_long(h, mmco[i].long_index);
  3880. if(pic) unreference_pic(h, pic);
  3881. h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
  3882. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3883. h->long_ref_count++;
  3884. break;
  3885. case MMCO_LONG2UNUSED:
  3886. pic= remove_long(h, mmco[i].long_index);
  3887. if(pic)
  3888. unreference_pic(h, pic);
  3889. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3890. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_long() failure\n");
  3891. break;
  3892. case MMCO_LONG:
  3893. pic= remove_long(h, mmco[i].long_index);
  3894. if(pic) unreference_pic(h, pic);
  3895. h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
  3896. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3897. h->long_ref_count++;
  3898. current_is_long=1;
  3899. break;
  3900. case MMCO_SET_MAX_LONG:
  3901. assert(mmco[i].long_index <= 16);
  3902. // just remove the long term which index is greater than new max
  3903. for(j = mmco[i].long_index; j<16; j++){
  3904. pic = remove_long(h, j);
  3905. if (pic) unreference_pic(h, pic);
  3906. }
  3907. break;
  3908. case MMCO_RESET:
  3909. while(h->short_ref_count){
  3910. pic= remove_short(h, h->short_ref[0]->frame_num);
  3911. unreference_pic(h, pic);
  3912. }
  3913. for(j = 0; j < 16; j++) {
  3914. pic= remove_long(h, j);
  3915. if(pic) unreference_pic(h, pic);
  3916. }
  3917. break;
  3918. default: assert(0);
  3919. }
  3920. }
  3921. if(!current_is_long){
  3922. pic= remove_short(h, s->current_picture_ptr->frame_num);
  3923. if(pic){
  3924. unreference_pic(h, pic);
  3925. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  3926. }
  3927. if(h->short_ref_count)
  3928. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  3929. h->short_ref[0]= s->current_picture_ptr;
  3930. h->short_ref[0]->long_ref=0;
  3931. h->short_ref_count++;
  3932. }
  3933. print_short_term(h);
  3934. print_long_term(h);
  3935. return 0;
  3936. }
  3937. static int decode_ref_pic_marking(H264Context *h){
  3938. MpegEncContext * const s = &h->s;
  3939. int i;
  3940. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  3941. s->broken_link= get_bits1(&s->gb) -1;
  3942. h->mmco[0].long_index= get_bits1(&s->gb) - 1; // current_long_term_idx
  3943. if(h->mmco[0].long_index == -1)
  3944. h->mmco_index= 0;
  3945. else{
  3946. h->mmco[0].opcode= MMCO_LONG;
  3947. h->mmco_index= 1;
  3948. }
  3949. }else{
  3950. if(get_bits1(&s->gb)){ // adaptive_ref_pic_marking_mode_flag
  3951. for(i= 0; i<MAX_MMCO_COUNT; i++) {
  3952. MMCOOpcode opcode= get_ue_golomb(&s->gb);;
  3953. h->mmco[i].opcode= opcode;
  3954. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  3955. 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
  3956. /* if(h->mmco[i].short_frame_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_frame_num ] == NULL){
  3957. av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
  3958. return -1;
  3959. }*/
  3960. }
  3961. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  3962. h->mmco[i].long_index= get_ue_golomb(&s->gb);
  3963. 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){
  3964. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  3965. return -1;
  3966. }
  3967. }
  3968. if(opcode > MMCO_LONG){
  3969. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  3970. return -1;
  3971. }
  3972. if(opcode == MMCO_END)
  3973. break;
  3974. }
  3975. h->mmco_index= i;
  3976. }else{
  3977. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  3978. if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){ //FIXME fields
  3979. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  3980. h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  3981. h->mmco_index= 1;
  3982. }else
  3983. h->mmco_index= 0;
  3984. }
  3985. }
  3986. return 0;
  3987. }
  3988. static int init_poc(H264Context *h){
  3989. MpegEncContext * const s = &h->s;
  3990. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  3991. int field_poc[2];
  3992. if(h->nal_unit_type == NAL_IDR_SLICE){
  3993. h->frame_num_offset= 0;
  3994. }else{
  3995. if(h->frame_num < h->prev_frame_num)
  3996. h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
  3997. else
  3998. h->frame_num_offset= h->prev_frame_num_offset;
  3999. }
  4000. if(h->sps.poc_type==0){
  4001. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  4002. if(h->nal_unit_type == NAL_IDR_SLICE){
  4003. h->prev_poc_msb=
  4004. h->prev_poc_lsb= 0;
  4005. }
  4006. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  4007. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  4008. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  4009. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  4010. else
  4011. h->poc_msb = h->prev_poc_msb;
  4012. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  4013. field_poc[0] =
  4014. field_poc[1] = h->poc_msb + h->poc_lsb;
  4015. if(s->picture_structure == PICT_FRAME)
  4016. field_poc[1] += h->delta_poc_bottom;
  4017. }else if(h->sps.poc_type==1){
  4018. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  4019. int i;
  4020. if(h->sps.poc_cycle_length != 0)
  4021. abs_frame_num = h->frame_num_offset + h->frame_num;
  4022. else
  4023. abs_frame_num = 0;
  4024. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  4025. abs_frame_num--;
  4026. expected_delta_per_poc_cycle = 0;
  4027. for(i=0; i < h->sps.poc_cycle_length; i++)
  4028. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  4029. if(abs_frame_num > 0){
  4030. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  4031. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  4032. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  4033. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  4034. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  4035. } else
  4036. expectedpoc = 0;
  4037. if(h->nal_ref_idc == 0)
  4038. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  4039. field_poc[0] = expectedpoc + h->delta_poc[0];
  4040. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  4041. if(s->picture_structure == PICT_FRAME)
  4042. field_poc[1] += h->delta_poc[1];
  4043. }else{
  4044. int poc;
  4045. if(h->nal_unit_type == NAL_IDR_SLICE){
  4046. poc= 0;
  4047. }else{
  4048. if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
  4049. else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
  4050. }
  4051. field_poc[0]= poc;
  4052. field_poc[1]= poc;
  4053. }
  4054. if(s->picture_structure != PICT_BOTTOM_FIELD)
  4055. s->current_picture_ptr->field_poc[0]= field_poc[0];
  4056. if(s->picture_structure != PICT_TOP_FIELD)
  4057. s->current_picture_ptr->field_poc[1]= field_poc[1];
  4058. if(s->picture_structure == PICT_FRAME) // FIXME field pix?
  4059. s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]);
  4060. return 0;
  4061. }
  4062. /**
  4063. * decodes a slice header.
  4064. * this will allso call MPV_common_init() and frame_start() as needed
  4065. */
  4066. static int decode_slice_header(H264Context *h){
  4067. MpegEncContext * const s = &h->s;
  4068. int first_mb_in_slice, pps_id;
  4069. int num_ref_idx_active_override_flag;
  4070. static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
  4071. int slice_type;
  4072. int default_ref_list_done = 0;
  4073. s->current_picture.reference= h->nal_ref_idc != 0;
  4074. s->dropable= h->nal_ref_idc == 0;
  4075. first_mb_in_slice= get_ue_golomb(&s->gb);
  4076. slice_type= get_ue_golomb(&s->gb);
  4077. if(slice_type > 9){
  4078. 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);
  4079. return -1;
  4080. }
  4081. if(slice_type > 4){
  4082. slice_type -= 5;
  4083. h->slice_type_fixed=1;
  4084. }else
  4085. h->slice_type_fixed=0;
  4086. slice_type= slice_type_map[ slice_type ];
  4087. if (slice_type == I_TYPE
  4088. || (h->slice_num != 0 && slice_type == h->slice_type) ) {
  4089. default_ref_list_done = 1;
  4090. }
  4091. h->slice_type= slice_type;
  4092. s->pict_type= h->slice_type; // to make a few old func happy, it's wrong though
  4093. pps_id= get_ue_golomb(&s->gb);
  4094. if(pps_id>255){
  4095. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  4096. return -1;
  4097. }
  4098. h->pps= h->pps_buffer[pps_id];
  4099. if(h->pps.slice_group_count == 0){
  4100. av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referenced\n");
  4101. return -1;
  4102. }
  4103. h->sps= h->sps_buffer[ h->pps.sps_id ];
  4104. if(h->sps.log2_max_frame_num == 0){
  4105. av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referenced\n");
  4106. return -1;
  4107. }
  4108. if(h->dequant_coeff_pps != pps_id){
  4109. h->dequant_coeff_pps = pps_id;
  4110. init_dequant_tables(h);
  4111. }
  4112. s->mb_width= h->sps.mb_width;
  4113. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  4114. h->b_stride= s->mb_width*4;
  4115. h->b8_stride= s->mb_width*2;
  4116. s->width = 16*s->mb_width - 2*(h->sps.crop_left + h->sps.crop_right );
  4117. if(h->sps.frame_mbs_only_flag)
  4118. s->height= 16*s->mb_height - 2*(h->sps.crop_top + h->sps.crop_bottom);
  4119. else
  4120. s->height= 16*s->mb_height - 4*(h->sps.crop_top + h->sps.crop_bottom); //FIXME recheck
  4121. if (s->context_initialized
  4122. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  4123. free_tables(h);
  4124. MPV_common_end(s);
  4125. }
  4126. if (!s->context_initialized) {
  4127. if (MPV_common_init(s) < 0)
  4128. return -1;
  4129. if(s->dsp.h264_idct_add == ff_h264_idct_add_c){ //FIXME little ugly
  4130. memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
  4131. memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
  4132. }else{
  4133. int i;
  4134. for(i=0; i<16; i++){
  4135. #define T(x) (x>>2) | ((x<<2) & 0xF)
  4136. h->zigzag_scan[i] = T(zigzag_scan[i]);
  4137. h-> field_scan[i] = T( field_scan[i]);
  4138. #undef T
  4139. }
  4140. }
  4141. if(s->dsp.h264_idct8_add == ff_h264_idct8_add_c){
  4142. memcpy(h->zigzag_scan8x8, zigzag_scan8x8, 64*sizeof(uint8_t));
  4143. memcpy(h->zigzag_scan8x8_cavlc, zigzag_scan8x8_cavlc, 64*sizeof(uint8_t));
  4144. memcpy(h->field_scan8x8, field_scan8x8, 64*sizeof(uint8_t));
  4145. memcpy(h->field_scan8x8_cavlc, field_scan8x8_cavlc, 64*sizeof(uint8_t));
  4146. }else{
  4147. int i;
  4148. for(i=0; i<64; i++){
  4149. #define T(x) (x>>3) | ((x&7)<<3)
  4150. h->zigzag_scan8x8[i] = T(zigzag_scan8x8[i]);
  4151. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  4152. h->field_scan8x8[i] = T(field_scan8x8[i]);
  4153. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  4154. #undef T
  4155. }
  4156. }
  4157. if(h->sps.transform_bypass){ //FIXME same ugly
  4158. h->zigzag_scan_q0 = zigzag_scan;
  4159. h->zigzag_scan8x8_q0 = zigzag_scan8x8;
  4160. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  4161. h->field_scan_q0 = field_scan;
  4162. h->field_scan8x8_q0 = field_scan8x8;
  4163. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  4164. }else{
  4165. h->zigzag_scan_q0 = h->zigzag_scan;
  4166. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  4167. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  4168. h->field_scan_q0 = h->field_scan;
  4169. h->field_scan8x8_q0 = h->field_scan8x8;
  4170. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  4171. }
  4172. alloc_tables(h);
  4173. s->avctx->width = s->width;
  4174. s->avctx->height = s->height;
  4175. s->avctx->sample_aspect_ratio= h->sps.sar;
  4176. if(!s->avctx->sample_aspect_ratio.den)
  4177. s->avctx->sample_aspect_ratio.den = 1;
  4178. if(h->sps.timing_info_present_flag){
  4179. s->avctx->time_base= (AVRational){h->sps.num_units_in_tick * 2, h->sps.time_scale};
  4180. if(h->x264_build > 0 && h->x264_build < 44)
  4181. s->avctx->time_base.den *= 2;
  4182. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  4183. s->avctx->time_base.num, s->avctx->time_base.den, 1<<30);
  4184. }
  4185. }
  4186. if(h->slice_num == 0){
  4187. if(frame_start(h) < 0)
  4188. return -1;
  4189. }
  4190. s->current_picture_ptr->frame_num= //FIXME frame_num cleanup
  4191. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  4192. h->mb_mbaff = 0;
  4193. h->mb_aff_frame = 0;
  4194. if(h->sps.frame_mbs_only_flag){
  4195. s->picture_structure= PICT_FRAME;
  4196. }else{
  4197. if(get_bits1(&s->gb)) { //field_pic_flag
  4198. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  4199. av_log(h->s.avctx, AV_LOG_ERROR, "PAFF interlacing is not implemented\n");
  4200. } else {
  4201. s->picture_structure= PICT_FRAME;
  4202. h->mb_aff_frame = h->sps.mb_aff;
  4203. }
  4204. }
  4205. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  4206. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << h->mb_aff_frame;
  4207. if(s->mb_y >= s->mb_height){
  4208. return -1;
  4209. }
  4210. if(s->picture_structure==PICT_FRAME){
  4211. h->curr_pic_num= h->frame_num;
  4212. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  4213. }else{
  4214. h->curr_pic_num= 2*h->frame_num;
  4215. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  4216. }
  4217. if(h->nal_unit_type == NAL_IDR_SLICE){
  4218. get_ue_golomb(&s->gb); /* idr_pic_id */
  4219. }
  4220. if(h->sps.poc_type==0){
  4221. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  4222. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  4223. h->delta_poc_bottom= get_se_golomb(&s->gb);
  4224. }
  4225. }
  4226. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  4227. h->delta_poc[0]= get_se_golomb(&s->gb);
  4228. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  4229. h->delta_poc[1]= get_se_golomb(&s->gb);
  4230. }
  4231. init_poc(h);
  4232. if(h->pps.redundant_pic_cnt_present){
  4233. h->redundant_pic_count= get_ue_golomb(&s->gb);
  4234. }
  4235. //set defaults, might be overriden a few line later
  4236. h->ref_count[0]= h->pps.ref_count[0];
  4237. h->ref_count[1]= h->pps.ref_count[1];
  4238. if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){
  4239. if(h->slice_type == B_TYPE){
  4240. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  4241. if(h->sps.mb_aff && h->direct_spatial_mv_pred)
  4242. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + spatial direct mode is not implemented\n");
  4243. }
  4244. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  4245. if(num_ref_idx_active_override_flag){
  4246. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  4247. if(h->slice_type==B_TYPE)
  4248. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  4249. if(h->ref_count[0] > 32 || h->ref_count[1] > 32){
  4250. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  4251. return -1;
  4252. }
  4253. }
  4254. }
  4255. if(!default_ref_list_done){
  4256. fill_default_ref_list(h);
  4257. }
  4258. if(decode_ref_pic_list_reordering(h) < 0)
  4259. return -1;
  4260. if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
  4261. || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
  4262. pred_weight_table(h);
  4263. else if(h->pps.weighted_bipred_idc==2 && h->slice_type==B_TYPE)
  4264. implicit_weight_table(h);
  4265. else
  4266. h->use_weight = 0;
  4267. if(s->current_picture.reference)
  4268. decode_ref_pic_marking(h);
  4269. if(FRAME_MBAFF)
  4270. fill_mbaff_ref_list(h);
  4271. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac )
  4272. h->cabac_init_idc = get_ue_golomb(&s->gb);
  4273. h->last_qscale_diff = 0;
  4274. s->qscale = h->pps.init_qp + get_se_golomb(&s->gb);
  4275. if(s->qscale<0 || s->qscale>51){
  4276. av_log(s->avctx, AV_LOG_ERROR, "QP %d out of range\n", s->qscale);
  4277. return -1;
  4278. }
  4279. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4280. //FIXME qscale / qp ... stuff
  4281. if(h->slice_type == SP_TYPE){
  4282. get_bits1(&s->gb); /* sp_for_switch_flag */
  4283. }
  4284. if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
  4285. get_se_golomb(&s->gb); /* slice_qs_delta */
  4286. }
  4287. h->deblocking_filter = 1;
  4288. h->slice_alpha_c0_offset = 0;
  4289. h->slice_beta_offset = 0;
  4290. if( h->pps.deblocking_filter_parameters_present ) {
  4291. h->deblocking_filter= get_ue_golomb(&s->gb);
  4292. if(h->deblocking_filter < 2)
  4293. h->deblocking_filter^= 1; // 1<->0
  4294. if( h->deblocking_filter ) {
  4295. h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
  4296. h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
  4297. }
  4298. }
  4299. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  4300. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type != I_TYPE)
  4301. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type == B_TYPE)
  4302. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  4303. h->deblocking_filter= 0;
  4304. #if 0 //FMO
  4305. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  4306. slice_group_change_cycle= get_bits(&s->gb, ?);
  4307. #endif
  4308. h->slice_num++;
  4309. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
  4310. h->emu_edge_height= FRAME_MBAFF ? 0 : h->emu_edge_width;
  4311. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  4312. 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",
  4313. h->slice_num,
  4314. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  4315. first_mb_in_slice,
  4316. av_get_pict_type_char(h->slice_type),
  4317. pps_id, h->frame_num,
  4318. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  4319. h->ref_count[0], h->ref_count[1],
  4320. s->qscale,
  4321. h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2,
  4322. h->use_weight,
  4323. h->use_weight==1 && h->use_weight_chroma ? "c" : ""
  4324. );
  4325. }
  4326. if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !s->current_picture.reference){
  4327. s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
  4328. s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
  4329. }else{
  4330. s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
  4331. s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
  4332. }
  4333. return 0;
  4334. }
  4335. /**
  4336. *
  4337. */
  4338. static inline int get_level_prefix(GetBitContext *gb){
  4339. unsigned int buf;
  4340. int log;
  4341. OPEN_READER(re, gb);
  4342. UPDATE_CACHE(re, gb);
  4343. buf=GET_CACHE(re, gb);
  4344. log= 32 - av_log2(buf);
  4345. #ifdef TRACE
  4346. print_bin(buf>>(32-log), log);
  4347. 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__);
  4348. #endif
  4349. LAST_SKIP_BITS(re, gb, log);
  4350. CLOSE_READER(re, gb);
  4351. return log-1;
  4352. }
  4353. static inline int get_dct8x8_allowed(H264Context *h){
  4354. int i;
  4355. for(i=0; i<4; i++){
  4356. if(!IS_SUB_8X8(h->sub_mb_type[i])
  4357. || (!h->sps.direct_8x8_inference_flag && IS_DIRECT(h->sub_mb_type[i])))
  4358. return 0;
  4359. }
  4360. return 1;
  4361. }
  4362. /**
  4363. * decodes a residual block.
  4364. * @param n block index
  4365. * @param scantable scantable
  4366. * @param max_coeff number of coefficients in the block
  4367. * @return <0 if an error occured
  4368. */
  4369. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
  4370. MpegEncContext * const s = &h->s;
  4371. 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};
  4372. int level[16];
  4373. int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
  4374. //FIXME put trailing_onex into the context
  4375. if(n == CHROMA_DC_BLOCK_INDEX){
  4376. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  4377. total_coeff= coeff_token>>2;
  4378. }else{
  4379. if(n == LUMA_DC_BLOCK_INDEX){
  4380. total_coeff= pred_non_zero_count(h, 0);
  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. }else{
  4384. total_coeff= pred_non_zero_count(h, n);
  4385. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4386. total_coeff= coeff_token>>2;
  4387. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  4388. }
  4389. }
  4390. //FIXME set last_non_zero?
  4391. if(total_coeff==0)
  4392. return 0;
  4393. trailing_ones= coeff_token&3;
  4394. tprintf("trailing:%d, total:%d\n", trailing_ones, total_coeff);
  4395. assert(total_coeff<=16);
  4396. for(i=0; i<trailing_ones; i++){
  4397. level[i]= 1 - 2*get_bits1(gb);
  4398. }
  4399. if(i<total_coeff) {
  4400. int level_code, mask;
  4401. int suffix_length = total_coeff > 10 && trailing_ones < 3;
  4402. int prefix= get_level_prefix(gb);
  4403. //first coefficient has suffix_length equal to 0 or 1
  4404. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  4405. if(suffix_length)
  4406. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4407. else
  4408. level_code= (prefix<<suffix_length); //part
  4409. }else if(prefix==14){
  4410. if(suffix_length)
  4411. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4412. else
  4413. level_code= prefix + get_bits(gb, 4); //part
  4414. }else if(prefix==15){
  4415. level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part
  4416. if(suffix_length==0) level_code+=15; //FIXME doesn't make (much)sense
  4417. }else{
  4418. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4419. return -1;
  4420. }
  4421. if(trailing_ones < 3) level_code += 2;
  4422. suffix_length = 1;
  4423. if(level_code > 5)
  4424. suffix_length++;
  4425. mask= -(level_code&1);
  4426. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4427. i++;
  4428. //remaining coefficients have suffix_length > 0
  4429. for(;i<total_coeff;i++) {
  4430. static const int suffix_limit[7] = {0,5,11,23,47,95,INT_MAX };
  4431. prefix = get_level_prefix(gb);
  4432. if(prefix<15){
  4433. level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
  4434. }else if(prefix==15){
  4435. level_code = (prefix<<suffix_length) + get_bits(gb, 12);
  4436. }else{
  4437. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4438. return -1;
  4439. }
  4440. mask= -(level_code&1);
  4441. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4442. if(level_code > suffix_limit[suffix_length])
  4443. suffix_length++;
  4444. }
  4445. }
  4446. if(total_coeff == max_coeff)
  4447. zeros_left=0;
  4448. else{
  4449. if(n == CHROMA_DC_BLOCK_INDEX)
  4450. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  4451. else
  4452. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  4453. }
  4454. coeff_num = zeros_left + total_coeff - 1;
  4455. j = scantable[coeff_num];
  4456. if(n > 24){
  4457. block[j] = level[0];
  4458. for(i=1;i<total_coeff;i++) {
  4459. if(zeros_left <= 0)
  4460. run_before = 0;
  4461. else if(zeros_left < 7){
  4462. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4463. }else{
  4464. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4465. }
  4466. zeros_left -= run_before;
  4467. coeff_num -= 1 + run_before;
  4468. j= scantable[ coeff_num ];
  4469. block[j]= level[i];
  4470. }
  4471. }else{
  4472. block[j] = (level[0] * qmul[j] + 32)>>6;
  4473. for(i=1;i<total_coeff;i++) {
  4474. if(zeros_left <= 0)
  4475. run_before = 0;
  4476. else if(zeros_left < 7){
  4477. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4478. }else{
  4479. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4480. }
  4481. zeros_left -= run_before;
  4482. coeff_num -= 1 + run_before;
  4483. j= scantable[ coeff_num ];
  4484. block[j]= (level[i] * qmul[j] + 32)>>6;
  4485. }
  4486. }
  4487. if(zeros_left<0){
  4488. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  4489. return -1;
  4490. }
  4491. return 0;
  4492. }
  4493. static void predict_field_decoding_flag(H264Context *h){
  4494. MpegEncContext * const s = &h->s;
  4495. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4496. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  4497. ? s->current_picture.mb_type[mb_xy-1]
  4498. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  4499. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  4500. : 0;
  4501. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  4502. }
  4503. /**
  4504. * decodes a P_SKIP or B_SKIP macroblock
  4505. */
  4506. static void decode_mb_skip(H264Context *h){
  4507. MpegEncContext * const s = &h->s;
  4508. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4509. int mb_type=0;
  4510. memset(h->non_zero_count[mb_xy], 0, 16);
  4511. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  4512. if(MB_FIELD)
  4513. mb_type|= MB_TYPE_INTERLACED;
  4514. if( h->slice_type == B_TYPE )
  4515. {
  4516. // just for fill_caches. pred_direct_motion will set the real mb_type
  4517. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
  4518. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4519. pred_direct_motion(h, &mb_type);
  4520. mb_type|= MB_TYPE_SKIP;
  4521. }
  4522. else
  4523. {
  4524. int mx, my;
  4525. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
  4526. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4527. pred_pskip_motion(h, &mx, &my);
  4528. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  4529. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  4530. }
  4531. write_back_motion(h, mb_type);
  4532. s->current_picture.mb_type[mb_xy]= mb_type;
  4533. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4534. h->slice_table[ mb_xy ]= h->slice_num;
  4535. h->prev_mb_skipped= 1;
  4536. }
  4537. /**
  4538. * decodes a macroblock
  4539. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  4540. */
  4541. static int decode_mb_cavlc(H264Context *h){
  4542. MpegEncContext * const s = &h->s;
  4543. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4544. int mb_type, partition_count, cbp;
  4545. int dct8x8_allowed= h->pps.transform_8x8_mode;
  4546. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?
  4547. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  4548. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  4549. down the code */
  4550. if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){
  4551. if(s->mb_skip_run==-1)
  4552. s->mb_skip_run= get_ue_golomb(&s->gb);
  4553. if (s->mb_skip_run--) {
  4554. if(FRAME_MBAFF && (s->mb_y&1) == 0){
  4555. if(s->mb_skip_run==0)
  4556. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4557. else
  4558. predict_field_decoding_flag(h);
  4559. }
  4560. decode_mb_skip(h);
  4561. return 0;
  4562. }
  4563. }
  4564. if(FRAME_MBAFF){
  4565. if( (s->mb_y&1) == 0 )
  4566. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4567. }else
  4568. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  4569. h->prev_mb_skipped= 0;
  4570. mb_type= get_ue_golomb(&s->gb);
  4571. if(h->slice_type == B_TYPE){
  4572. if(mb_type < 23){
  4573. partition_count= b_mb_type_info[mb_type].partition_count;
  4574. mb_type= b_mb_type_info[mb_type].type;
  4575. }else{
  4576. mb_type -= 23;
  4577. goto decode_intra_mb;
  4578. }
  4579. }else if(h->slice_type == P_TYPE /*|| h->slice_type == SP_TYPE */){
  4580. if(mb_type < 5){
  4581. partition_count= p_mb_type_info[mb_type].partition_count;
  4582. mb_type= p_mb_type_info[mb_type].type;
  4583. }else{
  4584. mb_type -= 5;
  4585. goto decode_intra_mb;
  4586. }
  4587. }else{
  4588. assert(h->slice_type == I_TYPE);
  4589. decode_intra_mb:
  4590. if(mb_type > 25){
  4591. av_log(h->s.avctx, AV_LOG_ERROR, "mb_type %d in %c slice too large at %d %d\n", mb_type, av_get_pict_type_char(h->slice_type), s->mb_x, s->mb_y);
  4592. return -1;
  4593. }
  4594. partition_count=0;
  4595. cbp= i_mb_type_info[mb_type].cbp;
  4596. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  4597. mb_type= i_mb_type_info[mb_type].type;
  4598. }
  4599. if(MB_FIELD)
  4600. mb_type |= MB_TYPE_INTERLACED;
  4601. h->slice_table[ mb_xy ]= h->slice_num;
  4602. if(IS_INTRA_PCM(mb_type)){
  4603. unsigned int x, y;
  4604. // we assume these blocks are very rare so we dont optimize it
  4605. align_get_bits(&s->gb);
  4606. // The pixels are stored in the same order as levels in h->mb array.
  4607. for(y=0; y<16; y++){
  4608. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  4609. for(x=0; x<16; x++){
  4610. tprintf("LUMA ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4611. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= get_bits(&s->gb, 8);
  4612. }
  4613. }
  4614. for(y=0; y<8; y++){
  4615. const int index= 256 + 4*(y&3) + 32*(y>>2);
  4616. for(x=0; x<8; x++){
  4617. tprintf("CHROMA U ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4618. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4619. }
  4620. }
  4621. for(y=0; y<8; y++){
  4622. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  4623. for(x=0; x<8; x++){
  4624. tprintf("CHROMA V ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4625. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4626. }
  4627. }
  4628. // In deblocking, the quantizer is 0
  4629. s->current_picture.qscale_table[mb_xy]= 0;
  4630. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  4631. // All coeffs are present
  4632. memset(h->non_zero_count[mb_xy], 16, 16);
  4633. s->current_picture.mb_type[mb_xy]= mb_type;
  4634. return 0;
  4635. }
  4636. if(MB_MBAFF){
  4637. h->ref_count[0] <<= 1;
  4638. h->ref_count[1] <<= 1;
  4639. }
  4640. fill_caches(h, mb_type, 0);
  4641. //mb_pred
  4642. if(IS_INTRA(mb_type)){
  4643. // init_top_left_availability(h);
  4644. if(IS_INTRA4x4(mb_type)){
  4645. int i;
  4646. int di = 1;
  4647. if(dct8x8_allowed && get_bits1(&s->gb)){
  4648. mb_type |= MB_TYPE_8x8DCT;
  4649. di = 4;
  4650. }
  4651. // fill_intra4x4_pred_table(h);
  4652. for(i=0; i<16; i+=di){
  4653. int mode= pred_intra_mode(h, i);
  4654. if(!get_bits1(&s->gb)){
  4655. const int rem_mode= get_bits(&s->gb, 3);
  4656. mode = rem_mode + (rem_mode >= mode);
  4657. }
  4658. if(di==4)
  4659. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  4660. else
  4661. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  4662. }
  4663. write_back_intra_pred_mode(h);
  4664. if( check_intra4x4_pred_mode(h) < 0)
  4665. return -1;
  4666. }else{
  4667. h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
  4668. if(h->intra16x16_pred_mode < 0)
  4669. return -1;
  4670. }
  4671. h->chroma_pred_mode= get_ue_golomb(&s->gb);
  4672. h->chroma_pred_mode= check_intra_pred_mode(h, h->chroma_pred_mode);
  4673. if(h->chroma_pred_mode < 0)
  4674. return -1;
  4675. }else if(partition_count==4){
  4676. int i, j, sub_partition_count[4], list, ref[2][4];
  4677. if(h->slice_type == B_TYPE){
  4678. for(i=0; i<4; i++){
  4679. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4680. if(h->sub_mb_type[i] >=13){
  4681. 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);
  4682. return -1;
  4683. }
  4684. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4685. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4686. }
  4687. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  4688. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
  4689. pred_direct_motion(h, &mb_type);
  4690. h->ref_cache[0][scan8[4]] =
  4691. h->ref_cache[1][scan8[4]] =
  4692. h->ref_cache[0][scan8[12]] =
  4693. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  4694. }
  4695. }else{
  4696. assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE); //FIXME SP correct ?
  4697. for(i=0; i<4; i++){
  4698. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4699. if(h->sub_mb_type[i] >=4){
  4700. 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);
  4701. return -1;
  4702. }
  4703. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4704. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4705. }
  4706. }
  4707. for(list=0; list<2; list++){
  4708. int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4709. if(ref_count == 0) continue;
  4710. for(i=0; i<4; i++){
  4711. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  4712. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4713. ref[list][i] = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
  4714. }else{
  4715. //FIXME
  4716. ref[list][i] = -1;
  4717. }
  4718. }
  4719. }
  4720. if(dct8x8_allowed)
  4721. dct8x8_allowed = get_dct8x8_allowed(h);
  4722. for(list=0; list<2; list++){
  4723. const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4724. if(ref_count == 0) continue;
  4725. for(i=0; i<4; i++){
  4726. if(IS_DIRECT(h->sub_mb_type[i])) {
  4727. h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];
  4728. continue;
  4729. }
  4730. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  4731. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  4732. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4733. const int sub_mb_type= h->sub_mb_type[i];
  4734. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  4735. for(j=0; j<sub_partition_count[i]; j++){
  4736. int mx, my;
  4737. const int index= 4*i + block_width*j;
  4738. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  4739. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  4740. mx += get_se_golomb(&s->gb);
  4741. my += get_se_golomb(&s->gb);
  4742. tprintf("final mv:%d %d\n", mx, my);
  4743. if(IS_SUB_8X8(sub_mb_type)){
  4744. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  4745. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  4746. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  4747. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  4748. }else if(IS_SUB_8X4(sub_mb_type)){
  4749. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  4750. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  4751. }else if(IS_SUB_4X8(sub_mb_type)){
  4752. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  4753. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  4754. }else{
  4755. assert(IS_SUB_4X4(sub_mb_type));
  4756. mv_cache[ 0 ][0]= mx;
  4757. mv_cache[ 0 ][1]= my;
  4758. }
  4759. }
  4760. }else{
  4761. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  4762. p[0] = p[1]=
  4763. p[8] = p[9]= 0;
  4764. }
  4765. }
  4766. }
  4767. }else if(IS_DIRECT(mb_type)){
  4768. pred_direct_motion(h, &mb_type);
  4769. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  4770. }else{
  4771. int list, mx, my, i;
  4772. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  4773. if(IS_16X16(mb_type)){
  4774. for(list=0; list<2; list++){
  4775. if(h->ref_count[list]>0){
  4776. if(IS_DIR(mb_type, 0, list)){
  4777. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4778. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  4779. }else
  4780. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4781. }
  4782. }
  4783. for(list=0; list<2; list++){
  4784. if(IS_DIR(mb_type, 0, list)){
  4785. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  4786. mx += get_se_golomb(&s->gb);
  4787. my += get_se_golomb(&s->gb);
  4788. tprintf("final mv:%d %d\n", mx, my);
  4789. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  4790. }else
  4791. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  4792. }
  4793. }
  4794. else if(IS_16X8(mb_type)){
  4795. for(list=0; list<2; list++){
  4796. if(h->ref_count[list]>0){
  4797. for(i=0; i<2; i++){
  4798. if(IS_DIR(mb_type, i, list)){
  4799. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4800. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  4801. }else
  4802. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  4803. }
  4804. }
  4805. }
  4806. for(list=0; list<2; list++){
  4807. for(i=0; i<2; i++){
  4808. if(IS_DIR(mb_type, i, list)){
  4809. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  4810. mx += get_se_golomb(&s->gb);
  4811. my += get_se_golomb(&s->gb);
  4812. tprintf("final mv:%d %d\n", mx, my);
  4813. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  4814. }else
  4815. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  4816. }
  4817. }
  4818. }else{
  4819. assert(IS_8X16(mb_type));
  4820. for(list=0; list<2; list++){
  4821. if(h->ref_count[list]>0){
  4822. for(i=0; i<2; i++){
  4823. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  4824. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4825. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  4826. }else
  4827. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4828. }
  4829. }
  4830. }
  4831. for(list=0; list<2; list++){
  4832. for(i=0; i<2; i++){
  4833. if(IS_DIR(mb_type, i, list)){
  4834. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  4835. mx += get_se_golomb(&s->gb);
  4836. my += get_se_golomb(&s->gb);
  4837. tprintf("final mv:%d %d\n", mx, my);
  4838. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  4839. }else
  4840. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  4841. }
  4842. }
  4843. }
  4844. }
  4845. if(IS_INTER(mb_type))
  4846. write_back_motion(h, mb_type);
  4847. if(!IS_INTRA16x16(mb_type)){
  4848. cbp= get_ue_golomb(&s->gb);
  4849. if(cbp > 47){
  4850. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%d) at %d %d\n", cbp, s->mb_x, s->mb_y);
  4851. return -1;
  4852. }
  4853. if(IS_INTRA4x4(mb_type))
  4854. cbp= golomb_to_intra4x4_cbp[cbp];
  4855. else
  4856. cbp= golomb_to_inter_cbp[cbp];
  4857. }
  4858. h->cbp = cbp;
  4859. if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
  4860. if(get_bits1(&s->gb))
  4861. mb_type |= MB_TYPE_8x8DCT;
  4862. }
  4863. s->current_picture.mb_type[mb_xy]= mb_type;
  4864. if(cbp || IS_INTRA16x16(mb_type)){
  4865. int i8x8, i4x4, chroma_idx;
  4866. int chroma_qp, dquant;
  4867. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  4868. const uint8_t *scan, *scan8x8, *dc_scan;
  4869. // fill_non_zero_count_cache(h);
  4870. if(IS_INTERLACED(mb_type)){
  4871. scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
  4872. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  4873. dc_scan= luma_dc_field_scan;
  4874. }else{
  4875. scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
  4876. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  4877. dc_scan= luma_dc_zigzag_scan;
  4878. }
  4879. dquant= get_se_golomb(&s->gb);
  4880. if( dquant > 25 || dquant < -26 ){
  4881. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  4882. return -1;
  4883. }
  4884. s->qscale += dquant;
  4885. if(((unsigned)s->qscale) > 51){
  4886. if(s->qscale<0) s->qscale+= 52;
  4887. else s->qscale-= 52;
  4888. }
  4889. h->chroma_qp= chroma_qp= get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4890. if(IS_INTRA16x16(mb_type)){
  4891. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){
  4892. return -1; //FIXME continue if partitioned and other return -1 too
  4893. }
  4894. assert((cbp&15) == 0 || (cbp&15) == 15);
  4895. if(cbp&15){
  4896. for(i8x8=0; i8x8<4; i8x8++){
  4897. for(i4x4=0; i4x4<4; i4x4++){
  4898. const int index= i4x4 + 4*i8x8;
  4899. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){
  4900. return -1;
  4901. }
  4902. }
  4903. }
  4904. }else{
  4905. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  4906. }
  4907. }else{
  4908. for(i8x8=0; i8x8<4; i8x8++){
  4909. if(cbp & (1<<i8x8)){
  4910. if(IS_8x8DCT(mb_type)){
  4911. DCTELEM *buf = &h->mb[64*i8x8];
  4912. uint8_t *nnz;
  4913. for(i4x4=0; i4x4<4; i4x4++){
  4914. if( decode_residual(h, gb, buf, i4x4+4*i8x8, scan8x8+16*i4x4,
  4915. h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )
  4916. return -1;
  4917. }
  4918. nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4919. nnz[0] += nnz[1] + nnz[8] + nnz[9];
  4920. }else{
  4921. for(i4x4=0; i4x4<4; i4x4++){
  4922. const int index= i4x4 + 4*i8x8;
  4923. if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){
  4924. return -1;
  4925. }
  4926. }
  4927. }
  4928. }else{
  4929. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4930. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  4931. }
  4932. }
  4933. }
  4934. if(cbp&0x30){
  4935. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  4936. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){
  4937. return -1;
  4938. }
  4939. }
  4940. if(cbp&0x20){
  4941. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  4942. for(i4x4=0; i4x4<4; i4x4++){
  4943. const int index= 16 + 4*chroma_idx + i4x4;
  4944. 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){
  4945. return -1;
  4946. }
  4947. }
  4948. }
  4949. }else{
  4950. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4951. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4952. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4953. }
  4954. }else{
  4955. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4956. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  4957. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4958. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4959. }
  4960. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4961. write_back_non_zero_count(h);
  4962. if(MB_MBAFF){
  4963. h->ref_count[0] >>= 1;
  4964. h->ref_count[1] >>= 1;
  4965. }
  4966. return 0;
  4967. }
  4968. static int decode_cabac_field_decoding_flag(H264Context *h) {
  4969. MpegEncContext * const s = &h->s;
  4970. const int mb_x = s->mb_x;
  4971. const int mb_y = s->mb_y & ~1;
  4972. const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
  4973. const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
  4974. unsigned int ctx = 0;
  4975. if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
  4976. ctx += 1;
  4977. }
  4978. if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
  4979. ctx += 1;
  4980. }
  4981. return get_cabac_noinline( &h->cabac, &h->cabac_state[70 + ctx] );
  4982. }
  4983. static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
  4984. uint8_t *state= &h->cabac_state[ctx_base];
  4985. int mb_type;
  4986. if(intra_slice){
  4987. MpegEncContext * const s = &h->s;
  4988. const int mba_xy = h->left_mb_xy[0];
  4989. const int mbb_xy = h->top_mb_xy;
  4990. int ctx=0;
  4991. if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
  4992. ctx++;
  4993. if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
  4994. ctx++;
  4995. if( get_cabac_noinline( &h->cabac, &state[ctx] ) == 0 )
  4996. return 0; /* I4x4 */
  4997. state += 2;
  4998. }else{
  4999. if( get_cabac_noinline( &h->cabac, &state[0] ) == 0 )
  5000. return 0; /* I4x4 */
  5001. }
  5002. if( get_cabac_terminate( &h->cabac ) )
  5003. return 25; /* PCM */
  5004. mb_type = 1; /* I16x16 */
  5005. mb_type += 12 * get_cabac_noinline( &h->cabac, &state[1] ); /* cbp_luma != 0 */
  5006. if( get_cabac_noinline( &h->cabac, &state[2] ) ) /* cbp_chroma */
  5007. mb_type += 4 + 4 * get_cabac_noinline( &h->cabac, &state[2+intra_slice] );
  5008. mb_type += 2 * get_cabac_noinline( &h->cabac, &state[3+intra_slice] );
  5009. mb_type += 1 * get_cabac_noinline( &h->cabac, &state[3+2*intra_slice] );
  5010. return mb_type;
  5011. }
  5012. static int decode_cabac_mb_type( H264Context *h ) {
  5013. MpegEncContext * const s = &h->s;
  5014. if( h->slice_type == I_TYPE ) {
  5015. return decode_cabac_intra_mb_type(h, 3, 1);
  5016. } else if( h->slice_type == P_TYPE ) {
  5017. if( get_cabac_noinline( &h->cabac, &h->cabac_state[14] ) == 0 ) {
  5018. /* P-type */
  5019. if( get_cabac_noinline( &h->cabac, &h->cabac_state[15] ) == 0 ) {
  5020. /* P_L0_D16x16, P_8x8 */
  5021. return 3 * get_cabac_noinline( &h->cabac, &h->cabac_state[16] );
  5022. } else {
  5023. /* P_L0_D8x16, P_L0_D16x8 */
  5024. return 2 - get_cabac_noinline( &h->cabac, &h->cabac_state[17] );
  5025. }
  5026. } else {
  5027. return decode_cabac_intra_mb_type(h, 17, 0) + 5;
  5028. }
  5029. } else if( h->slice_type == B_TYPE ) {
  5030. const int mba_xy = h->left_mb_xy[0];
  5031. const int mbb_xy = h->top_mb_xy;
  5032. int ctx = 0;
  5033. int bits;
  5034. if( h->slice_table[mba_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
  5035. ctx++;
  5036. if( h->slice_table[mbb_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
  5037. ctx++;
  5038. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+ctx] ) )
  5039. return 0; /* B_Direct_16x16 */
  5040. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+3] ) ) {
  5041. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
  5042. }
  5043. bits = get_cabac_noinline( &h->cabac, &h->cabac_state[27+4] ) << 3;
  5044. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 2;
  5045. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 1;
  5046. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  5047. if( bits < 8 )
  5048. return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
  5049. else if( bits == 13 ) {
  5050. return decode_cabac_intra_mb_type(h, 32, 0) + 23;
  5051. } else if( bits == 14 )
  5052. return 11; /* B_L1_L0_8x16 */
  5053. else if( bits == 15 )
  5054. return 22; /* B_8x8 */
  5055. bits= ( bits<<1 ) | get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  5056. return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
  5057. } else {
  5058. /* TODO SI/SP frames? */
  5059. return -1;
  5060. }
  5061. }
  5062. static int decode_cabac_mb_skip( H264Context *h, int mb_x, int mb_y ) {
  5063. MpegEncContext * const s = &h->s;
  5064. int mba_xy, mbb_xy;
  5065. int ctx = 0;
  5066. if(FRAME_MBAFF){ //FIXME merge with the stuff in fill_caches?
  5067. int mb_xy = mb_x + (mb_y&~1)*s->mb_stride;
  5068. mba_xy = mb_xy - 1;
  5069. if( (mb_y&1)
  5070. && h->slice_table[mba_xy] == h->slice_num
  5071. && MB_FIELD == !!IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) )
  5072. mba_xy += s->mb_stride;
  5073. if( MB_FIELD ){
  5074. mbb_xy = mb_xy - s->mb_stride;
  5075. if( !(mb_y&1)
  5076. && h->slice_table[mbb_xy] == h->slice_num
  5077. && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) )
  5078. mbb_xy -= s->mb_stride;
  5079. }else
  5080. mbb_xy = mb_x + (mb_y-1)*s->mb_stride;
  5081. }else{
  5082. int mb_xy = mb_x + mb_y*s->mb_stride;
  5083. mba_xy = mb_xy - 1;
  5084. mbb_xy = mb_xy - s->mb_stride;
  5085. }
  5086. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
  5087. ctx++;
  5088. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
  5089. ctx++;
  5090. if( h->slice_type == B_TYPE )
  5091. ctx += 13;
  5092. return get_cabac_noinline( &h->cabac, &h->cabac_state[11+ctx] );
  5093. }
  5094. static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
  5095. int mode = 0;
  5096. if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
  5097. return pred_mode;
  5098. mode += 1 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5099. mode += 2 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5100. mode += 4 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5101. if( mode >= pred_mode )
  5102. return mode + 1;
  5103. else
  5104. return mode;
  5105. }
  5106. static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
  5107. const int mba_xy = h->left_mb_xy[0];
  5108. const int mbb_xy = h->top_mb_xy;
  5109. int ctx = 0;
  5110. /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
  5111. if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
  5112. ctx++;
  5113. if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
  5114. ctx++;
  5115. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
  5116. return 0;
  5117. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  5118. return 1;
  5119. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  5120. return 2;
  5121. else
  5122. return 3;
  5123. }
  5124. static const uint8_t block_idx_x[16] = {
  5125. 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
  5126. };
  5127. static const uint8_t block_idx_y[16] = {
  5128. 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
  5129. };
  5130. static const uint8_t block_idx_xy[4][4] = {
  5131. { 0, 2, 8, 10},
  5132. { 1, 3, 9, 11},
  5133. { 4, 6, 12, 14},
  5134. { 5, 7, 13, 15}
  5135. };
  5136. static int decode_cabac_mb_cbp_luma( H264Context *h) {
  5137. int cbp = 0;
  5138. int cbp_b = -1;
  5139. int i8x8;
  5140. if( h->slice_table[h->top_mb_xy] == h->slice_num ) {
  5141. cbp_b = h->top_cbp;
  5142. tprintf("cbp_b = top_cbp = %x\n", cbp_b);
  5143. }
  5144. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5145. int cbp_a = -1;
  5146. int x, y;
  5147. int ctx = 0;
  5148. x = block_idx_x[4*i8x8];
  5149. y = block_idx_y[4*i8x8];
  5150. if( x > 0 )
  5151. cbp_a = cbp;
  5152. else if( h->slice_table[h->left_mb_xy[0]] == h->slice_num ) {
  5153. cbp_a = h->left_cbp;
  5154. tprintf("cbp_a = left_cbp = %x\n", cbp_a);
  5155. }
  5156. if( y > 0 )
  5157. cbp_b = cbp;
  5158. /* No need to test for skip as we put 0 for skip block */
  5159. /* No need to test for IPCM as we put 1 for IPCM block */
  5160. if( cbp_a >= 0 ) {
  5161. int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
  5162. if( ((cbp_a >> i8x8a)&0x01) == 0 )
  5163. ctx++;
  5164. }
  5165. if( cbp_b >= 0 ) {
  5166. int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
  5167. if( ((cbp_b >> i8x8b)&0x01) == 0 )
  5168. ctx += 2;
  5169. }
  5170. if( get_cabac( &h->cabac, &h->cabac_state[73 + ctx] ) ) {
  5171. cbp |= 1 << i8x8;
  5172. }
  5173. }
  5174. return cbp;
  5175. }
  5176. static int decode_cabac_mb_cbp_chroma( H264Context *h) {
  5177. int ctx;
  5178. int cbp_a, cbp_b;
  5179. cbp_a = (h->left_cbp>>4)&0x03;
  5180. cbp_b = (h-> top_cbp>>4)&0x03;
  5181. ctx = 0;
  5182. if( cbp_a > 0 ) ctx++;
  5183. if( cbp_b > 0 ) ctx += 2;
  5184. if( get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
  5185. return 0;
  5186. ctx = 4;
  5187. if( cbp_a == 2 ) ctx++;
  5188. if( cbp_b == 2 ) ctx += 2;
  5189. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] );
  5190. }
  5191. static int decode_cabac_mb_dqp( H264Context *h) {
  5192. MpegEncContext * const s = &h->s;
  5193. int mbn_xy;
  5194. int ctx = 0;
  5195. int val = 0;
  5196. if( s->mb_x > 0 )
  5197. mbn_xy = s->mb_x + s->mb_y*s->mb_stride - 1;
  5198. else
  5199. mbn_xy = s->mb_width - 1 + (s->mb_y-1)*s->mb_stride;
  5200. if( h->last_qscale_diff != 0 )
  5201. ctx++;
  5202. while( get_cabac_noinline( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
  5203. if( ctx < 2 )
  5204. ctx = 2;
  5205. else
  5206. ctx = 3;
  5207. val++;
  5208. if(val > 102) //prevent infinite loop
  5209. return INT_MIN;
  5210. }
  5211. if( val&0x01 )
  5212. return (val + 1)/2;
  5213. else
  5214. return -(val + 1)/2;
  5215. }
  5216. static int decode_cabac_p_mb_sub_type( H264Context *h ) {
  5217. if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
  5218. return 0; /* 8x8 */
  5219. if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
  5220. return 1; /* 8x4 */
  5221. if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
  5222. return 2; /* 4x8 */
  5223. return 3; /* 4x4 */
  5224. }
  5225. static int decode_cabac_b_mb_sub_type( H264Context *h ) {
  5226. int type;
  5227. if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
  5228. return 0; /* B_Direct_8x8 */
  5229. if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
  5230. return 1 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */
  5231. type = 3;
  5232. if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
  5233. if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
  5234. return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
  5235. type += 4;
  5236. }
  5237. type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
  5238. type += get_cabac( &h->cabac, &h->cabac_state[39] );
  5239. return type;
  5240. }
  5241. static inline int decode_cabac_mb_transform_size( H264Context *h ) {
  5242. return get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
  5243. }
  5244. static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
  5245. int refa = h->ref_cache[list][scan8[n] - 1];
  5246. int refb = h->ref_cache[list][scan8[n] - 8];
  5247. int ref = 0;
  5248. int ctx = 0;
  5249. if( h->slice_type == B_TYPE) {
  5250. if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
  5251. ctx++;
  5252. if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
  5253. ctx += 2;
  5254. } else {
  5255. if( refa > 0 )
  5256. ctx++;
  5257. if( refb > 0 )
  5258. ctx += 2;
  5259. }
  5260. while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
  5261. ref++;
  5262. if( ctx < 4 )
  5263. ctx = 4;
  5264. else
  5265. ctx = 5;
  5266. }
  5267. return ref;
  5268. }
  5269. static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
  5270. int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
  5271. abs( h->mvd_cache[list][scan8[n] - 8][l] );
  5272. int ctxbase = (l == 0) ? 40 : 47;
  5273. int ctx, mvd;
  5274. if( amvd < 3 )
  5275. ctx = 0;
  5276. else if( amvd > 32 )
  5277. ctx = 2;
  5278. else
  5279. ctx = 1;
  5280. if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
  5281. return 0;
  5282. mvd= 1;
  5283. ctx= 3;
  5284. while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
  5285. mvd++;
  5286. if( ctx < 6 )
  5287. ctx++;
  5288. }
  5289. if( mvd >= 9 ) {
  5290. int k = 3;
  5291. while( get_cabac_bypass( &h->cabac ) ) {
  5292. mvd += 1 << k;
  5293. k++;
  5294. }
  5295. while( k-- ) {
  5296. if( get_cabac_bypass( &h->cabac ) )
  5297. mvd += 1 << k;
  5298. }
  5299. }
  5300. return get_cabac_bypass_sign( &h->cabac, -mvd );
  5301. }
  5302. static int inline get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) {
  5303. int nza, nzb;
  5304. int ctx = 0;
  5305. if( cat == 0 ) {
  5306. nza = h->left_cbp&0x100;
  5307. nzb = h-> top_cbp&0x100;
  5308. } else if( cat == 1 || cat == 2 ) {
  5309. nza = h->non_zero_count_cache[scan8[idx] - 1];
  5310. nzb = h->non_zero_count_cache[scan8[idx] - 8];
  5311. } else if( cat == 3 ) {
  5312. nza = (h->left_cbp>>(6+idx))&0x01;
  5313. nzb = (h-> top_cbp>>(6+idx))&0x01;
  5314. } else {
  5315. assert(cat == 4);
  5316. nza = h->non_zero_count_cache[scan8[16+idx] - 1];
  5317. nzb = h->non_zero_count_cache[scan8[16+idx] - 8];
  5318. }
  5319. if( nza > 0 )
  5320. ctx++;
  5321. if( nzb > 0 )
  5322. ctx += 2;
  5323. return ctx + 4 * cat;
  5324. }
  5325. static const __attribute((used)) uint8_t last_coeff_flag_offset_8x8[63] = {
  5326. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5327. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  5328. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  5329. 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
  5330. };
  5331. static int decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff) {
  5332. const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
  5333. static const int significant_coeff_flag_offset[2][6] = {
  5334. { 105+0, 105+15, 105+29, 105+44, 105+47, 402 },
  5335. { 277+0, 277+15, 277+29, 277+44, 277+47, 436 }
  5336. };
  5337. static const int last_coeff_flag_offset[2][6] = {
  5338. { 166+0, 166+15, 166+29, 166+44, 166+47, 417 },
  5339. { 338+0, 338+15, 338+29, 338+44, 338+47, 451 }
  5340. };
  5341. static const int coeff_abs_level_m1_offset[6] = {
  5342. 227+0, 227+10, 227+20, 227+30, 227+39, 426
  5343. };
  5344. static const uint8_t significant_coeff_flag_offset_8x8[2][63] = {
  5345. { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
  5346. 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
  5347. 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
  5348. 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12 },
  5349. { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
  5350. 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
  5351. 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
  5352. 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }
  5353. };
  5354. int index[64];
  5355. int last;
  5356. int coeff_count = 0;
  5357. int abslevel1 = 1;
  5358. int abslevelgt1 = 0;
  5359. uint8_t *significant_coeff_ctx_base;
  5360. uint8_t *last_coeff_ctx_base;
  5361. uint8_t *abs_level_m1_ctx_base;
  5362. #ifndef ARCH_X86
  5363. #define CABAC_ON_STACK
  5364. #endif
  5365. #ifdef CABAC_ON_STACK
  5366. #define CC &cc
  5367. CABACContext cc;
  5368. cc.range = h->cabac.range;
  5369. cc.low = h->cabac.low;
  5370. cc.bytestream= h->cabac.bytestream;
  5371. #else
  5372. #define CC &h->cabac
  5373. #endif
  5374. /* cat: 0-> DC 16x16 n = 0
  5375. * 1-> AC 16x16 n = luma4x4idx
  5376. * 2-> Luma4x4 n = luma4x4idx
  5377. * 3-> DC Chroma n = iCbCr
  5378. * 4-> AC Chroma n = 4 * iCbCr + chroma4x4idx
  5379. * 5-> Luma8x8 n = 4 * luma8x8idx
  5380. */
  5381. /* read coded block flag */
  5382. if( cat != 5 ) {
  5383. if( get_cabac( CC, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n ) ] ) == 0 ) {
  5384. if( cat == 1 || cat == 2 )
  5385. h->non_zero_count_cache[scan8[n]] = 0;
  5386. else if( cat == 4 )
  5387. h->non_zero_count_cache[scan8[16+n]] = 0;
  5388. #ifdef CABAC_ON_STACK
  5389. h->cabac.range = cc.range ;
  5390. h->cabac.low = cc.low ;
  5391. h->cabac.bytestream= cc.bytestream;
  5392. #endif
  5393. return 0;
  5394. }
  5395. }
  5396. significant_coeff_ctx_base = h->cabac_state
  5397. + significant_coeff_flag_offset[MB_FIELD][cat];
  5398. last_coeff_ctx_base = h->cabac_state
  5399. + last_coeff_flag_offset[MB_FIELD][cat];
  5400. abs_level_m1_ctx_base = h->cabac_state
  5401. + coeff_abs_level_m1_offset[cat];
  5402. if( cat == 5 ) {
  5403. #define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \
  5404. for(last= 0; last < coefs; last++) { \
  5405. uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \
  5406. if( get_cabac( CC, sig_ctx )) { \
  5407. uint8_t *last_ctx = last_coeff_ctx_base + last_off; \
  5408. index[coeff_count++] = last; \
  5409. if( get_cabac( CC, last_ctx ) ) { \
  5410. last= max_coeff; \
  5411. break; \
  5412. } \
  5413. } \
  5414. }\
  5415. if( last == max_coeff -1 ) {\
  5416. index[coeff_count++] = last;\
  5417. }
  5418. const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
  5419. #if defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__))
  5420. coeff_count= decode_significance_8x8_x86(CC, significant_coeff_ctx_base, index, sig_off);
  5421. } else {
  5422. coeff_count= decode_significance_x86(CC, max_coeff, significant_coeff_ctx_base, index);
  5423. #else
  5424. DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );
  5425. } else {
  5426. DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
  5427. #endif
  5428. }
  5429. assert(coeff_count > 0);
  5430. if( cat == 0 )
  5431. h->cbp_table[mb_xy] |= 0x100;
  5432. else if( cat == 1 || cat == 2 )
  5433. h->non_zero_count_cache[scan8[n]] = coeff_count;
  5434. else if( cat == 3 )
  5435. h->cbp_table[mb_xy] |= 0x40 << n;
  5436. else if( cat == 4 )
  5437. h->non_zero_count_cache[scan8[16+n]] = coeff_count;
  5438. else {
  5439. assert( cat == 5 );
  5440. fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);
  5441. }
  5442. for( coeff_count--; coeff_count >= 0; coeff_count-- ) {
  5443. uint8_t *ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 )) + abs_level_m1_ctx_base;
  5444. int j= scantable[index[coeff_count]];
  5445. if( get_cabac( CC, ctx ) == 0 ) {
  5446. if( !qmul ) {
  5447. block[j] = get_cabac_bypass_sign( CC, -1);
  5448. }else{
  5449. block[j] = (get_cabac_bypass_sign( CC, -qmul[j]) + 32) >> 6;;
  5450. }
  5451. abslevel1++;
  5452. } else {
  5453. int coeff_abs = 2;
  5454. ctx = 5 + FFMIN( 4, abslevelgt1 ) + abs_level_m1_ctx_base;
  5455. while( coeff_abs < 15 && get_cabac( CC, ctx ) ) {
  5456. coeff_abs++;
  5457. }
  5458. if( coeff_abs >= 15 ) {
  5459. int j = 0;
  5460. while( get_cabac_bypass( CC ) ) {
  5461. j++;
  5462. }
  5463. coeff_abs=1;
  5464. while( j-- ) {
  5465. coeff_abs += coeff_abs + get_cabac_bypass( CC );
  5466. }
  5467. coeff_abs+= 14;
  5468. }
  5469. if( !qmul ) {
  5470. if( get_cabac_bypass( CC ) ) block[j] = -coeff_abs;
  5471. else block[j] = coeff_abs;
  5472. }else{
  5473. if( get_cabac_bypass( CC ) ) block[j] = (-coeff_abs * qmul[j] + 32) >> 6;
  5474. else block[j] = ( coeff_abs * qmul[j] + 32) >> 6;
  5475. }
  5476. abslevelgt1++;
  5477. }
  5478. }
  5479. #ifdef CABAC_ON_STACK
  5480. h->cabac.range = cc.range ;
  5481. h->cabac.low = cc.low ;
  5482. h->cabac.bytestream= cc.bytestream;
  5483. #endif
  5484. return 0;
  5485. }
  5486. static void inline compute_mb_neighbors(H264Context *h)
  5487. {
  5488. MpegEncContext * const s = &h->s;
  5489. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  5490. h->top_mb_xy = mb_xy - s->mb_stride;
  5491. h->left_mb_xy[0] = mb_xy - 1;
  5492. if(FRAME_MBAFF){
  5493. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  5494. const int top_pair_xy = pair_xy - s->mb_stride;
  5495. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  5496. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  5497. const int curr_mb_frame_flag = !MB_FIELD;
  5498. const int bottom = (s->mb_y & 1);
  5499. if (bottom
  5500. ? !curr_mb_frame_flag // bottom macroblock
  5501. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  5502. ) {
  5503. h->top_mb_xy -= s->mb_stride;
  5504. }
  5505. if (left_mb_frame_flag != curr_mb_frame_flag) {
  5506. h->left_mb_xy[0] = pair_xy - 1;
  5507. }
  5508. }
  5509. return;
  5510. }
  5511. /**
  5512. * decodes a macroblock
  5513. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  5514. */
  5515. static int decode_mb_cabac(H264Context *h) {
  5516. MpegEncContext * const s = &h->s;
  5517. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  5518. int mb_type, partition_count, cbp = 0;
  5519. int dct8x8_allowed= h->pps.transform_8x8_mode;
  5520. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?)
  5521. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  5522. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) {
  5523. int skip;
  5524. /* a skipped mb needs the aff flag from the following mb */
  5525. if( FRAME_MBAFF && s->mb_x==0 && (s->mb_y&1)==0 )
  5526. predict_field_decoding_flag(h);
  5527. if( FRAME_MBAFF && (s->mb_y&1)==1 && h->prev_mb_skipped )
  5528. skip = h->next_mb_skipped;
  5529. else
  5530. skip = decode_cabac_mb_skip( h, s->mb_x, s->mb_y );
  5531. /* read skip flags */
  5532. if( skip ) {
  5533. if( FRAME_MBAFF && (s->mb_y&1)==0 ){
  5534. s->current_picture.mb_type[mb_xy] = MB_TYPE_SKIP;
  5535. h->next_mb_skipped = decode_cabac_mb_skip( h, s->mb_x, s->mb_y+1 );
  5536. if(h->next_mb_skipped)
  5537. predict_field_decoding_flag(h);
  5538. else
  5539. h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5540. }
  5541. decode_mb_skip(h);
  5542. h->cbp_table[mb_xy] = 0;
  5543. h->chroma_pred_mode_table[mb_xy] = 0;
  5544. h->last_qscale_diff = 0;
  5545. return 0;
  5546. }
  5547. }
  5548. if(FRAME_MBAFF){
  5549. if( (s->mb_y&1) == 0 )
  5550. h->mb_mbaff =
  5551. h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5552. }else
  5553. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  5554. h->prev_mb_skipped = 0;
  5555. compute_mb_neighbors(h);
  5556. if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
  5557. av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
  5558. return -1;
  5559. }
  5560. if( h->slice_type == B_TYPE ) {
  5561. if( mb_type < 23 ){
  5562. partition_count= b_mb_type_info[mb_type].partition_count;
  5563. mb_type= b_mb_type_info[mb_type].type;
  5564. }else{
  5565. mb_type -= 23;
  5566. goto decode_intra_mb;
  5567. }
  5568. } else if( h->slice_type == P_TYPE ) {
  5569. if( mb_type < 5) {
  5570. partition_count= p_mb_type_info[mb_type].partition_count;
  5571. mb_type= p_mb_type_info[mb_type].type;
  5572. } else {
  5573. mb_type -= 5;
  5574. goto decode_intra_mb;
  5575. }
  5576. } else {
  5577. assert(h->slice_type == I_TYPE);
  5578. decode_intra_mb:
  5579. partition_count = 0;
  5580. cbp= i_mb_type_info[mb_type].cbp;
  5581. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  5582. mb_type= i_mb_type_info[mb_type].type;
  5583. }
  5584. if(MB_FIELD)
  5585. mb_type |= MB_TYPE_INTERLACED;
  5586. h->slice_table[ mb_xy ]= h->slice_num;
  5587. if(IS_INTRA_PCM(mb_type)) {
  5588. const uint8_t *ptr;
  5589. unsigned int x, y;
  5590. // We assume these blocks are very rare so we dont optimize it.
  5591. // FIXME The two following lines get the bitstream position in the cabac
  5592. // decode, I think it should be done by a function in cabac.h (or cabac.c).
  5593. ptr= h->cabac.bytestream;
  5594. if (h->cabac.low&0x1) ptr-=CABAC_BITS/8;
  5595. // The pixels are stored in the same order as levels in h->mb array.
  5596. for(y=0; y<16; y++){
  5597. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  5598. for(x=0; x<16; x++){
  5599. tprintf("LUMA ICPM LEVEL (%3d)\n", *ptr);
  5600. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= *ptr++;
  5601. }
  5602. }
  5603. for(y=0; y<8; y++){
  5604. const int index= 256 + 4*(y&3) + 32*(y>>2);
  5605. for(x=0; x<8; x++){
  5606. tprintf("CHROMA U ICPM LEVEL (%3d)\n", *ptr);
  5607. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5608. }
  5609. }
  5610. for(y=0; y<8; y++){
  5611. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  5612. for(x=0; x<8; x++){
  5613. tprintf("CHROMA V ICPM LEVEL (%3d)\n", *ptr);
  5614. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5615. }
  5616. }
  5617. ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);
  5618. // All blocks are present
  5619. h->cbp_table[mb_xy] = 0x1ef;
  5620. h->chroma_pred_mode_table[mb_xy] = 0;
  5621. // In deblocking, the quantizer is 0
  5622. s->current_picture.qscale_table[mb_xy]= 0;
  5623. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  5624. // All coeffs are present
  5625. memset(h->non_zero_count[mb_xy], 16, 16);
  5626. s->current_picture.mb_type[mb_xy]= mb_type;
  5627. return 0;
  5628. }
  5629. if(MB_MBAFF){
  5630. h->ref_count[0] <<= 1;
  5631. h->ref_count[1] <<= 1;
  5632. }
  5633. fill_caches(h, mb_type, 0);
  5634. if( IS_INTRA( mb_type ) ) {
  5635. int i;
  5636. if( IS_INTRA4x4( mb_type ) ) {
  5637. if( dct8x8_allowed && decode_cabac_mb_transform_size( h ) ) {
  5638. mb_type |= MB_TYPE_8x8DCT;
  5639. for( i = 0; i < 16; i+=4 ) {
  5640. int pred = pred_intra_mode( h, i );
  5641. int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5642. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  5643. }
  5644. } else {
  5645. for( i = 0; i < 16; i++ ) {
  5646. int pred = pred_intra_mode( h, i );
  5647. h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5648. //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
  5649. }
  5650. }
  5651. write_back_intra_pred_mode(h);
  5652. if( check_intra4x4_pred_mode(h) < 0 ) return -1;
  5653. } else {
  5654. h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );
  5655. if( h->intra16x16_pred_mode < 0 ) return -1;
  5656. }
  5657. h->chroma_pred_mode_table[mb_xy] =
  5658. h->chroma_pred_mode = decode_cabac_mb_chroma_pre_mode( h );
  5659. h->chroma_pred_mode= check_intra_pred_mode( h, h->chroma_pred_mode );
  5660. if( h->chroma_pred_mode < 0 ) return -1;
  5661. } else if( partition_count == 4 ) {
  5662. int i, j, sub_partition_count[4], list, ref[2][4];
  5663. if( h->slice_type == B_TYPE ) {
  5664. for( i = 0; i < 4; i++ ) {
  5665. h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
  5666. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5667. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5668. }
  5669. if( IS_DIRECT(h->sub_mb_type[0] | h->sub_mb_type[1] |
  5670. h->sub_mb_type[2] | h->sub_mb_type[3]) ) {
  5671. pred_direct_motion(h, &mb_type);
  5672. if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
  5673. for( i = 0; i < 4; i++ )
  5674. if( IS_DIRECT(h->sub_mb_type[i]) )
  5675. fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );
  5676. }
  5677. }
  5678. } else {
  5679. for( i = 0; i < 4; i++ ) {
  5680. h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
  5681. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5682. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5683. }
  5684. }
  5685. for( list = 0; list < 2; list++ ) {
  5686. if( h->ref_count[list] > 0 ) {
  5687. for( i = 0; i < 4; i++ ) {
  5688. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  5689. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  5690. if( h->ref_count[list] > 1 )
  5691. ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );
  5692. else
  5693. ref[list][i] = 0;
  5694. } else {
  5695. ref[list][i] = -1;
  5696. }
  5697. h->ref_cache[list][ scan8[4*i]+1 ]=
  5698. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  5699. }
  5700. }
  5701. }
  5702. if(dct8x8_allowed)
  5703. dct8x8_allowed = get_dct8x8_allowed(h);
  5704. for(list=0; list<2; list++){
  5705. for(i=0; i<4; i++){
  5706. if(IS_DIRECT(h->sub_mb_type[i])){
  5707. fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);
  5708. continue;
  5709. }
  5710. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];
  5711. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  5712. const int sub_mb_type= h->sub_mb_type[i];
  5713. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  5714. for(j=0; j<sub_partition_count[i]; j++){
  5715. int mpx, mpy;
  5716. int mx, my;
  5717. const int index= 4*i + block_width*j;
  5718. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  5719. int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];
  5720. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);
  5721. mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );
  5722. my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );
  5723. tprintf("final mv:%d %d\n", mx, my);
  5724. if(IS_SUB_8X8(sub_mb_type)){
  5725. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  5726. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  5727. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  5728. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  5729. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]=
  5730. mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;
  5731. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]=
  5732. mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;
  5733. }else if(IS_SUB_8X4(sub_mb_type)){
  5734. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  5735. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  5736. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]= mx- mpx;
  5737. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]= my - mpy;
  5738. }else if(IS_SUB_4X8(sub_mb_type)){
  5739. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  5740. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  5741. mvd_cache[ 0 ][0]= mvd_cache[ 8 ][0]= mx - mpx;
  5742. mvd_cache[ 0 ][1]= mvd_cache[ 8 ][1]= my - mpy;
  5743. }else{
  5744. assert(IS_SUB_4X4(sub_mb_type));
  5745. mv_cache[ 0 ][0]= mx;
  5746. mv_cache[ 0 ][1]= my;
  5747. mvd_cache[ 0 ][0]= mx - mpx;
  5748. mvd_cache[ 0 ][1]= my - mpy;
  5749. }
  5750. }
  5751. }else{
  5752. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  5753. uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];
  5754. p[0] = p[1] = p[8] = p[9] = 0;
  5755. pd[0]= pd[1]= pd[8]= pd[9]= 0;
  5756. }
  5757. }
  5758. }
  5759. } else if( IS_DIRECT(mb_type) ) {
  5760. pred_direct_motion(h, &mb_type);
  5761. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  5762. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  5763. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  5764. } else {
  5765. int list, mx, my, i, mpx, mpy;
  5766. if(IS_16X16(mb_type)){
  5767. for(list=0; list<2; list++){
  5768. if(IS_DIR(mb_type, 0, list)){
  5769. if(h->ref_count[list] > 0 ){
  5770. const int ref = h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 0 ) : 0;
  5771. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
  5772. }
  5773. }else
  5774. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  5775. }
  5776. for(list=0; list<2; list++){
  5777. if(IS_DIR(mb_type, 0, list)){
  5778. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);
  5779. mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );
  5780. my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );
  5781. tprintf("final mv:%d %d\n", mx, my);
  5782. fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5783. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  5784. }else
  5785. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  5786. }
  5787. }
  5788. else if(IS_16X8(mb_type)){
  5789. for(list=0; list<2; list++){
  5790. if(h->ref_count[list]>0){
  5791. for(i=0; i<2; i++){
  5792. if(IS_DIR(mb_type, i, list)){
  5793. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 8*i ) : 0;
  5794. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
  5795. }else
  5796. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  5797. }
  5798. }
  5799. }
  5800. for(list=0; list<2; list++){
  5801. for(i=0; i<2; i++){
  5802. if(IS_DIR(mb_type, i, list)){
  5803. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);
  5804. mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );
  5805. my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );
  5806. tprintf("final mv:%d %d\n", mx, my);
  5807. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);
  5808. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  5809. }else{
  5810. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5811. fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5812. }
  5813. }
  5814. }
  5815. }else{
  5816. assert(IS_8X16(mb_type));
  5817. for(list=0; list<2; list++){
  5818. if(h->ref_count[list]>0){
  5819. for(i=0; i<2; i++){
  5820. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  5821. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 4*i ) : 0;
  5822. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
  5823. }else
  5824. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  5825. }
  5826. }
  5827. }
  5828. for(list=0; list<2; list++){
  5829. for(i=0; i<2; i++){
  5830. if(IS_DIR(mb_type, i, list)){
  5831. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);
  5832. mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );
  5833. my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );
  5834. tprintf("final mv:%d %d\n", mx, my);
  5835. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5836. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  5837. }else{
  5838. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5839. fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5840. }
  5841. }
  5842. }
  5843. }
  5844. }
  5845. if( IS_INTER( mb_type ) ) {
  5846. h->chroma_pred_mode_table[mb_xy] = 0;
  5847. write_back_motion( h, mb_type );
  5848. }
  5849. if( !IS_INTRA16x16( mb_type ) ) {
  5850. cbp = decode_cabac_mb_cbp_luma( h );
  5851. cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
  5852. }
  5853. h->cbp_table[mb_xy] = h->cbp = cbp;
  5854. if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
  5855. if( decode_cabac_mb_transform_size( h ) )
  5856. mb_type |= MB_TYPE_8x8DCT;
  5857. }
  5858. s->current_picture.mb_type[mb_xy]= mb_type;
  5859. if( cbp || IS_INTRA16x16( mb_type ) ) {
  5860. const uint8_t *scan, *scan8x8, *dc_scan;
  5861. int dqp;
  5862. if(IS_INTERLACED(mb_type)){
  5863. scan8x8= s->qscale ? h->field_scan8x8 : h->field_scan8x8_q0;
  5864. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  5865. dc_scan= luma_dc_field_scan;
  5866. }else{
  5867. scan8x8= s->qscale ? h->zigzag_scan8x8 : h->zigzag_scan8x8_q0;
  5868. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  5869. dc_scan= luma_dc_zigzag_scan;
  5870. }
  5871. h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
  5872. if( dqp == INT_MIN ){
  5873. av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", s->mb_x, s->mb_y);
  5874. return -1;
  5875. }
  5876. s->qscale += dqp;
  5877. if(((unsigned)s->qscale) > 51){
  5878. if(s->qscale<0) s->qscale+= 52;
  5879. else s->qscale-= 52;
  5880. }
  5881. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  5882. if( IS_INTRA16x16( mb_type ) ) {
  5883. int i;
  5884. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
  5885. if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16) < 0)
  5886. return -1;
  5887. if( cbp&15 ) {
  5888. for( i = 0; i < 16; i++ ) {
  5889. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
  5890. if( decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 )
  5891. return -1;
  5892. }
  5893. } else {
  5894. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  5895. }
  5896. } else {
  5897. int i8x8, i4x4;
  5898. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5899. if( cbp & (1<<i8x8) ) {
  5900. if( IS_8x8DCT(mb_type) ) {
  5901. if( decode_cabac_residual(h, h->mb + 64*i8x8, 5, 4*i8x8,
  5902. scan8x8, h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 64) < 0 )
  5903. return -1;
  5904. } else
  5905. for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
  5906. const int index = 4*i8x8 + i4x4;
  5907. //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
  5908. //START_TIMER
  5909. 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 )
  5910. return -1;
  5911. //STOP_TIMER("decode_residual")
  5912. }
  5913. } else {
  5914. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  5915. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  5916. }
  5917. }
  5918. }
  5919. if( cbp&0x30 ){
  5920. int c;
  5921. for( c = 0; c < 2; c++ ) {
  5922. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
  5923. if( decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, NULL, 4) < 0)
  5924. return -1;
  5925. }
  5926. }
  5927. if( cbp&0x20 ) {
  5928. int c, i;
  5929. for( c = 0; c < 2; c++ ) {
  5930. for( i = 0; i < 4; i++ ) {
  5931. const int index = 16 + 4 * c + i;
  5932. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
  5933. 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)
  5934. return -1;
  5935. }
  5936. }
  5937. } else {
  5938. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5939. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5940. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5941. }
  5942. } else {
  5943. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5944. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  5945. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5946. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5947. h->last_qscale_diff = 0;
  5948. }
  5949. s->current_picture.qscale_table[mb_xy]= s->qscale;
  5950. write_back_non_zero_count(h);
  5951. if(MB_MBAFF){
  5952. h->ref_count[0] >>= 1;
  5953. h->ref_count[1] >>= 1;
  5954. }
  5955. return 0;
  5956. }
  5957. static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5958. int i, d;
  5959. const int index_a = qp + h->slice_alpha_c0_offset;
  5960. const int alpha = (alpha_table+52)[index_a];
  5961. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5962. if( bS[0] < 4 ) {
  5963. int8_t tc[4];
  5964. for(i=0; i<4; i++)
  5965. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  5966. h->s.dsp.h264_h_loop_filter_luma(pix, stride, alpha, beta, tc);
  5967. } else {
  5968. /* 16px edge length, because bS=4 is triggered by being at
  5969. * the edge of an intra MB, so all 4 bS are the same */
  5970. for( d = 0; d < 16; d++ ) {
  5971. const int p0 = pix[-1];
  5972. const int p1 = pix[-2];
  5973. const int p2 = pix[-3];
  5974. const int q0 = pix[0];
  5975. const int q1 = pix[1];
  5976. const int q2 = pix[2];
  5977. if( FFABS( p0 - q0 ) < alpha &&
  5978. FFABS( p1 - p0 ) < beta &&
  5979. FFABS( q1 - q0 ) < beta ) {
  5980. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5981. if( FFABS( p2 - p0 ) < beta)
  5982. {
  5983. const int p3 = pix[-4];
  5984. /* p0', p1', p2' */
  5985. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5986. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5987. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5988. } else {
  5989. /* p0' */
  5990. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5991. }
  5992. if( FFABS( q2 - q0 ) < beta)
  5993. {
  5994. const int q3 = pix[3];
  5995. /* q0', q1', q2' */
  5996. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5997. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5998. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5999. } else {
  6000. /* q0' */
  6001. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6002. }
  6003. }else{
  6004. /* p0', q0' */
  6005. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6006. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6007. }
  6008. 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]);
  6009. }
  6010. pix += stride;
  6011. }
  6012. }
  6013. }
  6014. static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  6015. int i;
  6016. const int index_a = qp + h->slice_alpha_c0_offset;
  6017. const int alpha = (alpha_table+52)[index_a];
  6018. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  6019. if( bS[0] < 4 ) {
  6020. int8_t tc[4];
  6021. for(i=0; i<4; i++)
  6022. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  6023. h->s.dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
  6024. } else {
  6025. h->s.dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
  6026. }
  6027. }
  6028. static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  6029. int i;
  6030. for( i = 0; i < 16; i++, pix += stride) {
  6031. int index_a;
  6032. int alpha;
  6033. int beta;
  6034. int qp_index;
  6035. int bS_index = (i >> 1);
  6036. if (!MB_FIELD) {
  6037. bS_index &= ~1;
  6038. bS_index |= (i & 1);
  6039. }
  6040. if( bS[bS_index] == 0 ) {
  6041. continue;
  6042. }
  6043. qp_index = MB_FIELD ? (i >> 3) : (i & 1);
  6044. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  6045. alpha = (alpha_table+52)[index_a];
  6046. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  6047. if( bS[bS_index] < 4 ) {
  6048. const int tc0 = (tc0_table+52)[index_a][bS[bS_index] - 1];
  6049. const int p0 = pix[-1];
  6050. const int p1 = pix[-2];
  6051. const int p2 = pix[-3];
  6052. const int q0 = pix[0];
  6053. const int q1 = pix[1];
  6054. const int q2 = pix[2];
  6055. if( FFABS( p0 - q0 ) < alpha &&
  6056. FFABS( p1 - p0 ) < beta &&
  6057. FFABS( q1 - q0 ) < beta ) {
  6058. int tc = tc0;
  6059. int i_delta;
  6060. if( FFABS( p2 - p0 ) < beta ) {
  6061. pix[-2] = p1 + clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  6062. tc++;
  6063. }
  6064. if( FFABS( q2 - q0 ) < beta ) {
  6065. pix[1] = q1 + clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  6066. tc++;
  6067. }
  6068. i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  6069. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  6070. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  6071. 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);
  6072. }
  6073. }else{
  6074. const int p0 = pix[-1];
  6075. const int p1 = pix[-2];
  6076. const int p2 = pix[-3];
  6077. const int q0 = pix[0];
  6078. const int q1 = pix[1];
  6079. const int q2 = pix[2];
  6080. if( FFABS( p0 - q0 ) < alpha &&
  6081. FFABS( p1 - p0 ) < beta &&
  6082. FFABS( q1 - q0 ) < beta ) {
  6083. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  6084. if( FFABS( p2 - p0 ) < beta)
  6085. {
  6086. const int p3 = pix[-4];
  6087. /* p0', p1', p2' */
  6088. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  6089. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  6090. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  6091. } else {
  6092. /* p0' */
  6093. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6094. }
  6095. if( FFABS( q2 - q0 ) < beta)
  6096. {
  6097. const int q3 = pix[3];
  6098. /* q0', q1', q2' */
  6099. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  6100. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  6101. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  6102. } else {
  6103. /* q0' */
  6104. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6105. }
  6106. }else{
  6107. /* p0', q0' */
  6108. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6109. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6110. }
  6111. 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]);
  6112. }
  6113. }
  6114. }
  6115. }
  6116. static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  6117. int i;
  6118. for( i = 0; i < 8; i++, pix += stride) {
  6119. int index_a;
  6120. int alpha;
  6121. int beta;
  6122. int qp_index;
  6123. int bS_index = i;
  6124. if( bS[bS_index] == 0 ) {
  6125. continue;
  6126. }
  6127. qp_index = MB_FIELD ? (i >> 2) : (i & 1);
  6128. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  6129. alpha = (alpha_table+52)[index_a];
  6130. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  6131. if( bS[bS_index] < 4 ) {
  6132. const int tc = (tc0_table+52)[index_a][bS[bS_index] - 1] + 1;
  6133. const int p0 = pix[-1];
  6134. const int p1 = pix[-2];
  6135. const int q0 = pix[0];
  6136. const int q1 = pix[1];
  6137. if( FFABS( p0 - q0 ) < alpha &&
  6138. FFABS( p1 - p0 ) < beta &&
  6139. FFABS( q1 - q0 ) < beta ) {
  6140. const int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  6141. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  6142. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  6143. 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);
  6144. }
  6145. }else{
  6146. const int p0 = pix[-1];
  6147. const int p1 = pix[-2];
  6148. const int q0 = pix[0];
  6149. const int q1 = pix[1];
  6150. if( FFABS( p0 - q0 ) < alpha &&
  6151. FFABS( p1 - p0 ) < beta &&
  6152. FFABS( q1 - q0 ) < beta ) {
  6153. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  6154. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  6155. 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]);
  6156. }
  6157. }
  6158. }
  6159. }
  6160. static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  6161. int i, d;
  6162. const int index_a = qp + h->slice_alpha_c0_offset;
  6163. const int alpha = (alpha_table+52)[index_a];
  6164. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  6165. const int pix_next = stride;
  6166. if( bS[0] < 4 ) {
  6167. int8_t tc[4];
  6168. for(i=0; i<4; i++)
  6169. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  6170. h->s.dsp.h264_v_loop_filter_luma(pix, stride, alpha, beta, tc);
  6171. } else {
  6172. /* 16px edge length, see filter_mb_edgev */
  6173. for( d = 0; d < 16; d++ ) {
  6174. const int p0 = pix[-1*pix_next];
  6175. const int p1 = pix[-2*pix_next];
  6176. const int p2 = pix[-3*pix_next];
  6177. const int q0 = pix[0];
  6178. const int q1 = pix[1*pix_next];
  6179. const int q2 = pix[2*pix_next];
  6180. if( FFABS( p0 - q0 ) < alpha &&
  6181. FFABS( p1 - p0 ) < beta &&
  6182. FFABS( q1 - q0 ) < beta ) {
  6183. const int p3 = pix[-4*pix_next];
  6184. const int q3 = pix[ 3*pix_next];
  6185. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  6186. if( FFABS( p2 - p0 ) < beta) {
  6187. /* p0', p1', p2' */
  6188. pix[-1*pix_next] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  6189. pix[-2*pix_next] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  6190. pix[-3*pix_next] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  6191. } else {
  6192. /* p0' */
  6193. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6194. }
  6195. if( FFABS( q2 - q0 ) < beta) {
  6196. /* q0', q1', q2' */
  6197. pix[0*pix_next] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  6198. pix[1*pix_next] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  6199. pix[2*pix_next] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  6200. } else {
  6201. /* q0' */
  6202. pix[0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6203. }
  6204. }else{
  6205. /* p0', q0' */
  6206. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6207. pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6208. }
  6209. 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]);
  6210. }
  6211. pix++;
  6212. }
  6213. }
  6214. }
  6215. static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  6216. int i;
  6217. const int index_a = qp + h->slice_alpha_c0_offset;
  6218. const int alpha = (alpha_table+52)[index_a];
  6219. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  6220. if( bS[0] < 4 ) {
  6221. int8_t tc[4];
  6222. for(i=0; i<4; i++)
  6223. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  6224. h->s.dsp.h264_v_loop_filter_chroma(pix, stride, alpha, beta, tc);
  6225. } else {
  6226. h->s.dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);
  6227. }
  6228. }
  6229. 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) {
  6230. MpegEncContext * const s = &h->s;
  6231. int mb_xy, mb_type;
  6232. int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh;
  6233. if(mb_x==0 || mb_y==0 || !s->dsp.h264_loop_filter_strength) {
  6234. filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);
  6235. return;
  6236. }
  6237. assert(!FRAME_MBAFF);
  6238. mb_xy = mb_x + mb_y*s->mb_stride;
  6239. mb_type = s->current_picture.mb_type[mb_xy];
  6240. qp = s->current_picture.qscale_table[mb_xy];
  6241. qp0 = s->current_picture.qscale_table[mb_xy-1];
  6242. qp1 = s->current_picture.qscale_table[h->top_mb_xy];
  6243. qpc = get_chroma_qp( h->pps.chroma_qp_index_offset, qp );
  6244. qpc0 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp0 );
  6245. qpc1 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp1 );
  6246. qp0 = (qp + qp0 + 1) >> 1;
  6247. qp1 = (qp + qp1 + 1) >> 1;
  6248. qpc0 = (qpc + qpc0 + 1) >> 1;
  6249. qpc1 = (qpc + qpc1 + 1) >> 1;
  6250. qp_thresh = 15 - h->slice_alpha_c0_offset;
  6251. if(qp <= qp_thresh && qp0 <= qp_thresh && qp1 <= qp_thresh &&
  6252. qpc <= qp_thresh && qpc0 <= qp_thresh && qpc1 <= qp_thresh)
  6253. return;
  6254. if( IS_INTRA(mb_type) ) {
  6255. int16_t bS4[4] = {4,4,4,4};
  6256. int16_t bS3[4] = {3,3,3,3};
  6257. if( IS_8x8DCT(mb_type) ) {
  6258. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  6259. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  6260. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  6261. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  6262. } else {
  6263. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  6264. filter_mb_edgev( h, &img_y[4*1], linesize, bS3, qp );
  6265. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  6266. filter_mb_edgev( h, &img_y[4*3], linesize, bS3, qp );
  6267. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  6268. filter_mb_edgeh( h, &img_y[4*1*linesize], linesize, bS3, qp );
  6269. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  6270. filter_mb_edgeh( h, &img_y[4*3*linesize], linesize, bS3, qp );
  6271. }
  6272. filter_mb_edgecv( h, &img_cb[2*0], uvlinesize, bS4, qpc0 );
  6273. filter_mb_edgecv( h, &img_cb[2*2], uvlinesize, bS3, qpc );
  6274. filter_mb_edgecv( h, &img_cr[2*0], uvlinesize, bS4, qpc0 );
  6275. filter_mb_edgecv( h, &img_cr[2*2], uvlinesize, bS3, qpc );
  6276. filter_mb_edgech( h, &img_cb[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  6277. filter_mb_edgech( h, &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc );
  6278. filter_mb_edgech( h, &img_cr[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  6279. filter_mb_edgech( h, &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc );
  6280. return;
  6281. } else {
  6282. DECLARE_ALIGNED_8(int16_t, bS[2][4][4]);
  6283. uint64_t (*bSv)[4] = (uint64_t(*)[4])bS;
  6284. int edges;
  6285. if( IS_8x8DCT(mb_type) && (h->cbp&7) == 7 ) {
  6286. edges = 4;
  6287. bSv[0][0] = bSv[0][2] = bSv[1][0] = bSv[1][2] = 0x0002000200020002ULL;
  6288. } else {
  6289. int mask_edge1 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 :
  6290. (mb_type & MB_TYPE_16x8) ? 1 : 0;
  6291. int mask_edge0 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16))
  6292. && (s->current_picture.mb_type[mb_xy-1] & (MB_TYPE_16x16 | MB_TYPE_8x16))
  6293. ? 3 : 0;
  6294. int step = IS_8x8DCT(mb_type) ? 2 : 1;
  6295. edges = (mb_type & MB_TYPE_16x16) && !(h->cbp & 15) ? 1 : 4;
  6296. s->dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,
  6297. (h->slice_type == B_TYPE), edges, step, mask_edge0, mask_edge1 );
  6298. }
  6299. if( IS_INTRA(s->current_picture.mb_type[mb_xy-1]) )
  6300. bSv[0][0] = 0x0004000400040004ULL;
  6301. if( IS_INTRA(s->current_picture.mb_type[h->top_mb_xy]) )
  6302. bSv[1][0] = 0x0004000400040004ULL;
  6303. #define FILTER(hv,dir,edge)\
  6304. if(bSv[dir][edge]) {\
  6305. filter_mb_edge##hv( h, &img_y[4*edge*(dir?linesize:1)], linesize, bS[dir][edge], edge ? qp : qp##dir );\
  6306. if(!(edge&1)) {\
  6307. filter_mb_edgec##hv( h, &img_cb[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  6308. filter_mb_edgec##hv( h, &img_cr[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  6309. }\
  6310. }
  6311. if( edges == 1 ) {
  6312. FILTER(v,0,0);
  6313. FILTER(h,1,0);
  6314. } else if( IS_8x8DCT(mb_type) ) {
  6315. FILTER(v,0,0);
  6316. FILTER(v,0,2);
  6317. FILTER(h,1,0);
  6318. FILTER(h,1,2);
  6319. } else {
  6320. FILTER(v,0,0);
  6321. FILTER(v,0,1);
  6322. FILTER(v,0,2);
  6323. FILTER(v,0,3);
  6324. FILTER(h,1,0);
  6325. FILTER(h,1,1);
  6326. FILTER(h,1,2);
  6327. FILTER(h,1,3);
  6328. }
  6329. #undef FILTER
  6330. }
  6331. }
  6332. 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) {
  6333. MpegEncContext * const s = &h->s;
  6334. const int mb_xy= mb_x + mb_y*s->mb_stride;
  6335. const int mb_type = s->current_picture.mb_type[mb_xy];
  6336. const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4;
  6337. int first_vertical_edge_done = 0;
  6338. int dir;
  6339. /* FIXME: A given frame may occupy more than one position in
  6340. * the reference list. So ref2frm should be populated with
  6341. * frame numbers, not indices. */
  6342. static const int ref2frm[34] = {-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  6343. 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
  6344. //for sufficiently low qp, filtering wouldn't do anything
  6345. //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
  6346. if(!FRAME_MBAFF){
  6347. int qp_thresh = 15 - h->slice_alpha_c0_offset - FFMAX(0, h->pps.chroma_qp_index_offset);
  6348. int qp = s->current_picture.qscale_table[mb_xy];
  6349. if(qp <= qp_thresh
  6350. && (mb_x == 0 || ((qp + s->current_picture.qscale_table[mb_xy-1] + 1)>>1) <= qp_thresh)
  6351. && (mb_y == 0 || ((qp + s->current_picture.qscale_table[h->top_mb_xy] + 1)>>1) <= qp_thresh)){
  6352. return;
  6353. }
  6354. }
  6355. if (FRAME_MBAFF
  6356. // left mb is in picture
  6357. && h->slice_table[mb_xy-1] != 255
  6358. // and current and left pair do not have the same interlaced type
  6359. && (IS_INTERLACED(mb_type) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
  6360. // and left mb is in the same slice if deblocking_filter == 2
  6361. && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
  6362. /* First vertical edge is different in MBAFF frames
  6363. * There are 8 different bS to compute and 2 different Qp
  6364. */
  6365. const int pair_xy = mb_x + (mb_y&~1)*s->mb_stride;
  6366. const int left_mb_xy[2] = { pair_xy-1, pair_xy-1+s->mb_stride };
  6367. int16_t bS[8];
  6368. int qp[2];
  6369. int chroma_qp[2];
  6370. int mb_qp, mbn0_qp, mbn1_qp;
  6371. int i;
  6372. first_vertical_edge_done = 1;
  6373. if( IS_INTRA(mb_type) )
  6374. bS[0] = bS[1] = bS[2] = bS[3] = bS[4] = bS[5] = bS[6] = bS[7] = 4;
  6375. else {
  6376. for( i = 0; i < 8; i++ ) {
  6377. int mbn_xy = MB_FIELD ? left_mb_xy[i>>2] : left_mb_xy[i&1];
  6378. if( IS_INTRA( s->current_picture.mb_type[mbn_xy] ) )
  6379. bS[i] = 4;
  6380. else if( h->non_zero_count_cache[12+8*(i>>1)] != 0 ||
  6381. /* FIXME: with 8x8dct + cavlc, should check cbp instead of nnz */
  6382. h->non_zero_count[mbn_xy][MB_FIELD ? i&3 : (i>>2)+(mb_y&1)*2] )
  6383. bS[i] = 2;
  6384. else
  6385. bS[i] = 1;
  6386. }
  6387. }
  6388. mb_qp = s->current_picture.qscale_table[mb_xy];
  6389. mbn0_qp = s->current_picture.qscale_table[left_mb_xy[0]];
  6390. mbn1_qp = s->current_picture.qscale_table[left_mb_xy[1]];
  6391. qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1;
  6392. chroma_qp[0] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6393. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn0_qp ) + 1 ) >> 1;
  6394. qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1;
  6395. chroma_qp[1] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6396. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn1_qp ) + 1 ) >> 1;
  6397. /* Filter edge */
  6398. 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);
  6399. { int i; for (i = 0; i < 8; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6400. filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp );
  6401. filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, chroma_qp );
  6402. filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, chroma_qp );
  6403. }
  6404. /* dir : 0 -> vertical edge, 1 -> horizontal edge */
  6405. for( dir = 0; dir < 2; dir++ )
  6406. {
  6407. int edge;
  6408. const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
  6409. const int mbm_type = s->current_picture.mb_type[mbm_xy];
  6410. int start = h->slice_table[mbm_xy] == 255 ? 1 : 0;
  6411. const int edges = (mb_type & (MB_TYPE_16x16|MB_TYPE_SKIP))
  6412. == (MB_TYPE_16x16|MB_TYPE_SKIP) ? 1 : 4;
  6413. // how often to recheck mv-based bS when iterating between edges
  6414. const int mask_edge = (mb_type & (MB_TYPE_16x16 | (MB_TYPE_16x8 << dir))) ? 3 :
  6415. (mb_type & (MB_TYPE_8x16 >> dir)) ? 1 : 0;
  6416. // how often to recheck mv-based bS when iterating along each edge
  6417. const int mask_par0 = mb_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir));
  6418. if (first_vertical_edge_done) {
  6419. start = 1;
  6420. first_vertical_edge_done = 0;
  6421. }
  6422. if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
  6423. start = 1;
  6424. if (FRAME_MBAFF && (dir == 1) && ((mb_y&1) == 0) && start == 0
  6425. && !IS_INTERLACED(mb_type)
  6426. && IS_INTERLACED(mbm_type)
  6427. ) {
  6428. // This is a special case in the norm where the filtering must
  6429. // be done twice (one each of the field) even if we are in a
  6430. // frame macroblock.
  6431. //
  6432. static const int nnz_idx[4] = {4,5,6,3};
  6433. unsigned int tmp_linesize = 2 * linesize;
  6434. unsigned int tmp_uvlinesize = 2 * uvlinesize;
  6435. int mbn_xy = mb_xy - 2 * s->mb_stride;
  6436. int qp, chroma_qp;
  6437. int i, j;
  6438. int16_t bS[4];
  6439. for(j=0; j<2; j++, mbn_xy += s->mb_stride){
  6440. if( IS_INTRA(mb_type) ||
  6441. IS_INTRA(s->current_picture.mb_type[mbn_xy]) ) {
  6442. bS[0] = bS[1] = bS[2] = bS[3] = 3;
  6443. } else {
  6444. const uint8_t *mbn_nnz = h->non_zero_count[mbn_xy];
  6445. for( i = 0; i < 4; i++ ) {
  6446. if( h->non_zero_count_cache[scan8[0]+i] != 0 ||
  6447. mbn_nnz[nnz_idx[i]] != 0 )
  6448. bS[i] = 2;
  6449. else
  6450. bS[i] = 1;
  6451. }
  6452. }
  6453. // Do not use s->qscale as luma quantizer because it has not the same
  6454. // value in IPCM macroblocks.
  6455. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6456. 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);
  6457. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6458. filter_mb_edgeh( h, &img_y[j*linesize], tmp_linesize, bS, qp );
  6459. chroma_qp = ( h->chroma_qp +
  6460. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6461. filter_mb_edgech( h, &img_cb[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6462. filter_mb_edgech( h, &img_cr[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6463. }
  6464. start = 1;
  6465. }
  6466. /* Calculate bS */
  6467. for( edge = start; edge < edges; edge++ ) {
  6468. /* mbn_xy: neighbor macroblock */
  6469. const int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
  6470. const int mbn_type = s->current_picture.mb_type[mbn_xy];
  6471. int16_t bS[4];
  6472. int qp;
  6473. if( (edge&1) && IS_8x8DCT(mb_type) )
  6474. continue;
  6475. if( IS_INTRA(mb_type) ||
  6476. IS_INTRA(mbn_type) ) {
  6477. int value;
  6478. if (edge == 0) {
  6479. if ( (!IS_INTERLACED(mb_type) && !IS_INTERLACED(mbm_type))
  6480. || ((FRAME_MBAFF || (s->picture_structure != PICT_FRAME)) && (dir == 0))
  6481. ) {
  6482. value = 4;
  6483. } else {
  6484. value = 3;
  6485. }
  6486. } else {
  6487. value = 3;
  6488. }
  6489. bS[0] = bS[1] = bS[2] = bS[3] = value;
  6490. } else {
  6491. int i, l;
  6492. int mv_done;
  6493. if( edge & mask_edge ) {
  6494. bS[0] = bS[1] = bS[2] = bS[3] = 0;
  6495. mv_done = 1;
  6496. }
  6497. else if( FRAME_MBAFF && IS_INTERLACED(mb_type ^ mbn_type)) {
  6498. bS[0] = bS[1] = bS[2] = bS[3] = 1;
  6499. mv_done = 1;
  6500. }
  6501. else if( mask_par0 && (edge || (mbn_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir)))) ) {
  6502. int b_idx= 8 + 4 + edge * (dir ? 8:1);
  6503. int bn_idx= b_idx - (dir ? 8:1);
  6504. int v = 0;
  6505. for( l = 0; !v && l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6506. v |= ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6507. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6508. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit;
  6509. }
  6510. bS[0] = bS[1] = bS[2] = bS[3] = v;
  6511. mv_done = 1;
  6512. }
  6513. else
  6514. mv_done = 0;
  6515. for( i = 0; i < 4; i++ ) {
  6516. int x = dir == 0 ? edge : i;
  6517. int y = dir == 0 ? i : edge;
  6518. int b_idx= 8 + 4 + x + 8*y;
  6519. int bn_idx= b_idx - (dir ? 8:1);
  6520. if( h->non_zero_count_cache[b_idx] != 0 ||
  6521. h->non_zero_count_cache[bn_idx] != 0 ) {
  6522. bS[i] = 2;
  6523. }
  6524. else if(!mv_done)
  6525. {
  6526. bS[i] = 0;
  6527. for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6528. if( ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6529. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6530. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit ) {
  6531. bS[i] = 1;
  6532. break;
  6533. }
  6534. }
  6535. }
  6536. }
  6537. if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
  6538. continue;
  6539. }
  6540. /* Filter edge */
  6541. // Do not use s->qscale as luma quantizer because it has not the same
  6542. // value in IPCM macroblocks.
  6543. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6544. //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]);
  6545. tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
  6546. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6547. if( dir == 0 ) {
  6548. filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
  6549. if( (edge&1) == 0 ) {
  6550. int chroma_qp = ( h->chroma_qp +
  6551. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6552. filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp );
  6553. filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp );
  6554. }
  6555. } else {
  6556. filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
  6557. if( (edge&1) == 0 ) {
  6558. int chroma_qp = ( h->chroma_qp +
  6559. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6560. filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6561. filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6562. }
  6563. }
  6564. }
  6565. }
  6566. }
  6567. static int decode_slice(H264Context *h){
  6568. MpegEncContext * const s = &h->s;
  6569. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  6570. s->mb_skip_run= -1;
  6571. if( h->pps.cabac ) {
  6572. int i;
  6573. /* realign */
  6574. align_get_bits( &s->gb );
  6575. /* init cabac */
  6576. ff_init_cabac_states( &h->cabac);
  6577. ff_init_cabac_decoder( &h->cabac,
  6578. s->gb.buffer + get_bits_count(&s->gb)/8,
  6579. ( s->gb.size_in_bits - get_bits_count(&s->gb) + 7)/8);
  6580. /* calculate pre-state */
  6581. for( i= 0; i < 460; i++ ) {
  6582. int pre;
  6583. if( h->slice_type == I_TYPE )
  6584. pre = clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
  6585. else
  6586. 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 );
  6587. if( pre <= 63 )
  6588. h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
  6589. else
  6590. h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
  6591. }
  6592. for(;;){
  6593. //START_TIMER
  6594. int ret = decode_mb_cabac(h);
  6595. int eos;
  6596. //STOP_TIMER("decode_mb_cabac")
  6597. if(ret>=0) hl_decode_mb(h);
  6598. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  6599. s->mb_y++;
  6600. if(ret>=0) ret = decode_mb_cabac(h);
  6601. if(ret>=0) hl_decode_mb(h);
  6602. s->mb_y--;
  6603. }
  6604. eos = get_cabac_terminate( &h->cabac );
  6605. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  6606. 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);
  6607. 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);
  6608. return -1;
  6609. }
  6610. if( ++s->mb_x >= s->mb_width ) {
  6611. s->mb_x = 0;
  6612. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6613. ++s->mb_y;
  6614. if(FRAME_MBAFF) {
  6615. ++s->mb_y;
  6616. }
  6617. }
  6618. if( eos || s->mb_y >= s->mb_height ) {
  6619. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6620. 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);
  6621. return 0;
  6622. }
  6623. }
  6624. } else {
  6625. for(;;){
  6626. int ret = decode_mb_cavlc(h);
  6627. if(ret>=0) hl_decode_mb(h);
  6628. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  6629. s->mb_y++;
  6630. ret = decode_mb_cavlc(h);
  6631. if(ret>=0) hl_decode_mb(h);
  6632. s->mb_y--;
  6633. }
  6634. if(ret<0){
  6635. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6636. 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);
  6637. return -1;
  6638. }
  6639. if(++s->mb_x >= s->mb_width){
  6640. s->mb_x=0;
  6641. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6642. ++s->mb_y;
  6643. if(FRAME_MBAFF) {
  6644. ++s->mb_y;
  6645. }
  6646. if(s->mb_y >= s->mb_height){
  6647. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6648. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  6649. 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);
  6650. return 0;
  6651. }else{
  6652. 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);
  6653. return -1;
  6654. }
  6655. }
  6656. }
  6657. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  6658. tprintf("slice end %d %d\n", 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. }
  6669. #if 0
  6670. for(;s->mb_y < s->mb_height; s->mb_y++){
  6671. for(;s->mb_x < s->mb_width; s->mb_x++){
  6672. int ret= decode_mb(h);
  6673. hl_decode_mb(h);
  6674. if(ret<0){
  6675. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6676. 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);
  6677. return -1;
  6678. }
  6679. if(++s->mb_x >= s->mb_width){
  6680. s->mb_x=0;
  6681. if(++s->mb_y >= s->mb_height){
  6682. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6683. 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);
  6684. return 0;
  6685. }else{
  6686. 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);
  6687. return -1;
  6688. }
  6689. }
  6690. }
  6691. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  6692. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6693. 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);
  6694. return 0;
  6695. }else{
  6696. 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);
  6697. return -1;
  6698. }
  6699. }
  6700. }
  6701. s->mb_x=0;
  6702. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6703. }
  6704. #endif
  6705. return -1; //not reached
  6706. }
  6707. static int decode_unregistered_user_data(H264Context *h, int size){
  6708. MpegEncContext * const s = &h->s;
  6709. uint8_t user_data[16+256];
  6710. int e, build, i;
  6711. if(size<16)
  6712. return -1;
  6713. for(i=0; i<sizeof(user_data)-1 && i<size; i++){
  6714. user_data[i]= get_bits(&s->gb, 8);
  6715. }
  6716. user_data[i]= 0;
  6717. e= sscanf(user_data+16, "x264 - core %d"/*%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html*/, &build);
  6718. if(e==1 && build>=0)
  6719. h->x264_build= build;
  6720. if(s->avctx->debug & FF_DEBUG_BUGS)
  6721. av_log(s->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data+16);
  6722. for(; i<size; i++)
  6723. skip_bits(&s->gb, 8);
  6724. return 0;
  6725. }
  6726. static int decode_sei(H264Context *h){
  6727. MpegEncContext * const s = &h->s;
  6728. while(get_bits_count(&s->gb) + 16 < s->gb.size_in_bits){
  6729. int size, type;
  6730. type=0;
  6731. do{
  6732. type+= show_bits(&s->gb, 8);
  6733. }while(get_bits(&s->gb, 8) == 255);
  6734. size=0;
  6735. do{
  6736. size+= show_bits(&s->gb, 8);
  6737. }while(get_bits(&s->gb, 8) == 255);
  6738. switch(type){
  6739. case 5:
  6740. if(decode_unregistered_user_data(h, size) < 0)
  6741. return -1;
  6742. break;
  6743. default:
  6744. skip_bits(&s->gb, 8*size);
  6745. }
  6746. //FIXME check bits here
  6747. align_get_bits(&s->gb);
  6748. }
  6749. return 0;
  6750. }
  6751. static inline void decode_hrd_parameters(H264Context *h, SPS *sps){
  6752. MpegEncContext * const s = &h->s;
  6753. int cpb_count, i;
  6754. cpb_count = get_ue_golomb(&s->gb) + 1;
  6755. get_bits(&s->gb, 4); /* bit_rate_scale */
  6756. get_bits(&s->gb, 4); /* cpb_size_scale */
  6757. for(i=0; i<cpb_count; i++){
  6758. get_ue_golomb(&s->gb); /* bit_rate_value_minus1 */
  6759. get_ue_golomb(&s->gb); /* cpb_size_value_minus1 */
  6760. get_bits1(&s->gb); /* cbr_flag */
  6761. }
  6762. get_bits(&s->gb, 5); /* initial_cpb_removal_delay_length_minus1 */
  6763. get_bits(&s->gb, 5); /* cpb_removal_delay_length_minus1 */
  6764. get_bits(&s->gb, 5); /* dpb_output_delay_length_minus1 */
  6765. get_bits(&s->gb, 5); /* time_offset_length */
  6766. }
  6767. static inline int decode_vui_parameters(H264Context *h, SPS *sps){
  6768. MpegEncContext * const s = &h->s;
  6769. int aspect_ratio_info_present_flag, aspect_ratio_idc;
  6770. int nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag;
  6771. aspect_ratio_info_present_flag= get_bits1(&s->gb);
  6772. if( aspect_ratio_info_present_flag ) {
  6773. aspect_ratio_idc= get_bits(&s->gb, 8);
  6774. if( aspect_ratio_idc == EXTENDED_SAR ) {
  6775. sps->sar.num= get_bits(&s->gb, 16);
  6776. sps->sar.den= get_bits(&s->gb, 16);
  6777. }else if(aspect_ratio_idc < 14){
  6778. sps->sar= pixel_aspect[aspect_ratio_idc];
  6779. }else{
  6780. av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
  6781. return -1;
  6782. }
  6783. }else{
  6784. sps->sar.num=
  6785. sps->sar.den= 0;
  6786. }
  6787. // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
  6788. if(get_bits1(&s->gb)){ /* overscan_info_present_flag */
  6789. get_bits1(&s->gb); /* overscan_appropriate_flag */
  6790. }
  6791. if(get_bits1(&s->gb)){ /* video_signal_type_present_flag */
  6792. get_bits(&s->gb, 3); /* video_format */
  6793. get_bits1(&s->gb); /* video_full_range_flag */
  6794. if(get_bits1(&s->gb)){ /* colour_description_present_flag */
  6795. get_bits(&s->gb, 8); /* colour_primaries */
  6796. get_bits(&s->gb, 8); /* transfer_characteristics */
  6797. get_bits(&s->gb, 8); /* matrix_coefficients */
  6798. }
  6799. }
  6800. if(get_bits1(&s->gb)){ /* chroma_location_info_present_flag */
  6801. get_ue_golomb(&s->gb); /* chroma_sample_location_type_top_field */
  6802. get_ue_golomb(&s->gb); /* chroma_sample_location_type_bottom_field */
  6803. }
  6804. sps->timing_info_present_flag = get_bits1(&s->gb);
  6805. if(sps->timing_info_present_flag){
  6806. sps->num_units_in_tick = get_bits_long(&s->gb, 32);
  6807. sps->time_scale = get_bits_long(&s->gb, 32);
  6808. sps->fixed_frame_rate_flag = get_bits1(&s->gb);
  6809. }
  6810. nal_hrd_parameters_present_flag = get_bits1(&s->gb);
  6811. if(nal_hrd_parameters_present_flag)
  6812. decode_hrd_parameters(h, sps);
  6813. vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
  6814. if(vcl_hrd_parameters_present_flag)
  6815. decode_hrd_parameters(h, sps);
  6816. if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag)
  6817. get_bits1(&s->gb); /* low_delay_hrd_flag */
  6818. get_bits1(&s->gb); /* pic_struct_present_flag */
  6819. sps->bitstream_restriction_flag = get_bits1(&s->gb);
  6820. if(sps->bitstream_restriction_flag){
  6821. get_bits1(&s->gb); /* motion_vectors_over_pic_boundaries_flag */
  6822. get_ue_golomb(&s->gb); /* max_bytes_per_pic_denom */
  6823. get_ue_golomb(&s->gb); /* max_bits_per_mb_denom */
  6824. get_ue_golomb(&s->gb); /* log2_max_mv_length_horizontal */
  6825. get_ue_golomb(&s->gb); /* log2_max_mv_length_vertical */
  6826. sps->num_reorder_frames = get_ue_golomb(&s->gb);
  6827. get_ue_golomb(&s->gb); /* max_dec_frame_buffering */
  6828. }
  6829. return 0;
  6830. }
  6831. static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,
  6832. const uint8_t *jvt_list, const uint8_t *fallback_list){
  6833. MpegEncContext * const s = &h->s;
  6834. int i, last = 8, next = 8;
  6835. const uint8_t *scan = size == 16 ? zigzag_scan : zigzag_scan8x8;
  6836. if(!get_bits1(&s->gb)) /* matrix not written, we use the predicted one */
  6837. memcpy(factors, fallback_list, size*sizeof(uint8_t));
  6838. else
  6839. for(i=0;i<size;i++){
  6840. if(next)
  6841. next = (last + get_se_golomb(&s->gb)) & 0xff;
  6842. if(!i && !next){ /* matrix not written, we use the preset one */
  6843. memcpy(factors, jvt_list, size*sizeof(uint8_t));
  6844. break;
  6845. }
  6846. last = factors[scan[i]] = next ? next : last;
  6847. }
  6848. }
  6849. static void decode_scaling_matrices(H264Context *h, SPS *sps, PPS *pps, int is_sps,
  6850. uint8_t (*scaling_matrix4)[16], uint8_t (*scaling_matrix8)[64]){
  6851. MpegEncContext * const s = &h->s;
  6852. int fallback_sps = !is_sps && sps->scaling_matrix_present;
  6853. const uint8_t *fallback[4] = {
  6854. fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
  6855. fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
  6856. fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
  6857. fallback_sps ? sps->scaling_matrix8[1] : default_scaling8[1]
  6858. };
  6859. if(get_bits1(&s->gb)){
  6860. sps->scaling_matrix_present |= is_sps;
  6861. decode_scaling_list(h,scaling_matrix4[0],16,default_scaling4[0],fallback[0]); // Intra, Y
  6862. decode_scaling_list(h,scaling_matrix4[1],16,default_scaling4[0],scaling_matrix4[0]); // Intra, Cr
  6863. decode_scaling_list(h,scaling_matrix4[2],16,default_scaling4[0],scaling_matrix4[1]); // Intra, Cb
  6864. decode_scaling_list(h,scaling_matrix4[3],16,default_scaling4[1],fallback[1]); // Inter, Y
  6865. decode_scaling_list(h,scaling_matrix4[4],16,default_scaling4[1],scaling_matrix4[3]); // Inter, Cr
  6866. decode_scaling_list(h,scaling_matrix4[5],16,default_scaling4[1],scaling_matrix4[4]); // Inter, Cb
  6867. if(is_sps || pps->transform_8x8_mode){
  6868. decode_scaling_list(h,scaling_matrix8[0],64,default_scaling8[0],fallback[2]); // Intra, Y
  6869. decode_scaling_list(h,scaling_matrix8[1],64,default_scaling8[1],fallback[3]); // Inter, Y
  6870. }
  6871. } else if(fallback_sps) {
  6872. memcpy(scaling_matrix4, sps->scaling_matrix4, 6*16*sizeof(uint8_t));
  6873. memcpy(scaling_matrix8, sps->scaling_matrix8, 2*64*sizeof(uint8_t));
  6874. }
  6875. }
  6876. static inline int decode_seq_parameter_set(H264Context *h){
  6877. MpegEncContext * const s = &h->s;
  6878. int profile_idc, level_idc;
  6879. int sps_id, i;
  6880. SPS *sps;
  6881. profile_idc= get_bits(&s->gb, 8);
  6882. get_bits1(&s->gb); //constraint_set0_flag
  6883. get_bits1(&s->gb); //constraint_set1_flag
  6884. get_bits1(&s->gb); //constraint_set2_flag
  6885. get_bits1(&s->gb); //constraint_set3_flag
  6886. get_bits(&s->gb, 4); // reserved
  6887. level_idc= get_bits(&s->gb, 8);
  6888. sps_id= get_ue_golomb(&s->gb);
  6889. sps= &h->sps_buffer[ sps_id ];
  6890. sps->profile_idc= profile_idc;
  6891. sps->level_idc= level_idc;
  6892. if(sps->profile_idc >= 100){ //high profile
  6893. if(get_ue_golomb(&s->gb) == 3) //chroma_format_idc
  6894. get_bits1(&s->gb); //residual_color_transform_flag
  6895. get_ue_golomb(&s->gb); //bit_depth_luma_minus8
  6896. get_ue_golomb(&s->gb); //bit_depth_chroma_minus8
  6897. sps->transform_bypass = get_bits1(&s->gb);
  6898. decode_scaling_matrices(h, sps, NULL, 1, sps->scaling_matrix4, sps->scaling_matrix8);
  6899. }else
  6900. sps->scaling_matrix_present = 0;
  6901. sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
  6902. sps->poc_type= get_ue_golomb(&s->gb);
  6903. if(sps->poc_type == 0){ //FIXME #define
  6904. sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
  6905. } else if(sps->poc_type == 1){//FIXME #define
  6906. sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
  6907. sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
  6908. sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
  6909. sps->poc_cycle_length= get_ue_golomb(&s->gb);
  6910. for(i=0; i<sps->poc_cycle_length; i++)
  6911. sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
  6912. }
  6913. if(sps->poc_type > 2){
  6914. av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
  6915. return -1;
  6916. }
  6917. sps->ref_frame_count= get_ue_golomb(&s->gb);
  6918. if(sps->ref_frame_count > MAX_PICTURE_COUNT-2){
  6919. av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");
  6920. }
  6921. sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
  6922. sps->mb_width= get_ue_golomb(&s->gb) + 1;
  6923. sps->mb_height= get_ue_golomb(&s->gb) + 1;
  6924. if((unsigned)sps->mb_width >= INT_MAX/16 || (unsigned)sps->mb_height >= INT_MAX/16 ||
  6925. avcodec_check_dimensions(NULL, 16*sps->mb_width, 16*sps->mb_height))
  6926. return -1;
  6927. sps->frame_mbs_only_flag= get_bits1(&s->gb);
  6928. if(!sps->frame_mbs_only_flag)
  6929. sps->mb_aff= get_bits1(&s->gb);
  6930. else
  6931. sps->mb_aff= 0;
  6932. sps->direct_8x8_inference_flag= get_bits1(&s->gb);
  6933. #ifndef ALLOW_INTERLACE
  6934. if(sps->mb_aff)
  6935. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF support not included; enable it at compile-time.\n");
  6936. #endif
  6937. if(!sps->direct_8x8_inference_flag && sps->mb_aff)
  6938. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + !direct_8x8_inference is not implemented\n");
  6939. sps->crop= get_bits1(&s->gb);
  6940. if(sps->crop){
  6941. sps->crop_left = get_ue_golomb(&s->gb);
  6942. sps->crop_right = get_ue_golomb(&s->gb);
  6943. sps->crop_top = get_ue_golomb(&s->gb);
  6944. sps->crop_bottom= get_ue_golomb(&s->gb);
  6945. if(sps->crop_left || sps->crop_top){
  6946. av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ...\n");
  6947. }
  6948. }else{
  6949. sps->crop_left =
  6950. sps->crop_right =
  6951. sps->crop_top =
  6952. sps->crop_bottom= 0;
  6953. }
  6954. sps->vui_parameters_present_flag= get_bits1(&s->gb);
  6955. if( sps->vui_parameters_present_flag )
  6956. decode_vui_parameters(h, sps);
  6957. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6958. 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",
  6959. sps_id, sps->profile_idc, sps->level_idc,
  6960. sps->poc_type,
  6961. sps->ref_frame_count,
  6962. sps->mb_width, sps->mb_height,
  6963. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  6964. sps->direct_8x8_inference_flag ? "8B8" : "",
  6965. sps->crop_left, sps->crop_right,
  6966. sps->crop_top, sps->crop_bottom,
  6967. sps->vui_parameters_present_flag ? "VUI" : ""
  6968. );
  6969. }
  6970. return 0;
  6971. }
  6972. static inline int decode_picture_parameter_set(H264Context *h, int bit_length){
  6973. MpegEncContext * const s = &h->s;
  6974. int pps_id= get_ue_golomb(&s->gb);
  6975. PPS *pps= &h->pps_buffer[pps_id];
  6976. pps->sps_id= get_ue_golomb(&s->gb);
  6977. pps->cabac= get_bits1(&s->gb);
  6978. pps->pic_order_present= get_bits1(&s->gb);
  6979. pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
  6980. if(pps->slice_group_count > 1 ){
  6981. pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
  6982. av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
  6983. switch(pps->mb_slice_group_map_type){
  6984. case 0:
  6985. #if 0
  6986. | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
  6987. | run_length[ i ] |1 |ue(v) |
  6988. #endif
  6989. break;
  6990. case 2:
  6991. #if 0
  6992. | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
  6993. |{ | | |
  6994. | top_left_mb[ i ] |1 |ue(v) |
  6995. | bottom_right_mb[ i ] |1 |ue(v) |
  6996. | } | | |
  6997. #endif
  6998. break;
  6999. case 3:
  7000. case 4:
  7001. case 5:
  7002. #if 0
  7003. | slice_group_change_direction_flag |1 |u(1) |
  7004. | slice_group_change_rate_minus1 |1 |ue(v) |
  7005. #endif
  7006. break;
  7007. case 6:
  7008. #if 0
  7009. | slice_group_id_cnt_minus1 |1 |ue(v) |
  7010. | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
  7011. |) | | |
  7012. | slice_group_id[ i ] |1 |u(v) |
  7013. #endif
  7014. break;
  7015. }
  7016. }
  7017. pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  7018. pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  7019. if(pps->ref_count[0] > 32 || pps->ref_count[1] > 32){
  7020. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
  7021. return -1;
  7022. }
  7023. pps->weighted_pred= get_bits1(&s->gb);
  7024. pps->weighted_bipred_idc= get_bits(&s->gb, 2);
  7025. pps->init_qp= get_se_golomb(&s->gb) + 26;
  7026. pps->init_qs= get_se_golomb(&s->gb) + 26;
  7027. pps->chroma_qp_index_offset= get_se_golomb(&s->gb);
  7028. pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
  7029. pps->constrained_intra_pred= get_bits1(&s->gb);
  7030. pps->redundant_pic_cnt_present = get_bits1(&s->gb);
  7031. pps->transform_8x8_mode= 0;
  7032. h->dequant_coeff_pps= -1; //contents of sps/pps can change even if id doesn't, so reinit
  7033. memset(pps->scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  7034. memset(pps->scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  7035. if(get_bits_count(&s->gb) < bit_length){
  7036. pps->transform_8x8_mode= get_bits1(&s->gb);
  7037. decode_scaling_matrices(h, &h->sps_buffer[pps->sps_id], pps, 0, pps->scaling_matrix4, pps->scaling_matrix8);
  7038. get_se_golomb(&s->gb); //second_chroma_qp_index_offset
  7039. }
  7040. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  7041. 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",
  7042. pps_id, pps->sps_id,
  7043. pps->cabac ? "CABAC" : "CAVLC",
  7044. pps->slice_group_count,
  7045. pps->ref_count[0], pps->ref_count[1],
  7046. pps->weighted_pred ? "weighted" : "",
  7047. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset,
  7048. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  7049. pps->constrained_intra_pred ? "CONSTR" : "",
  7050. pps->redundant_pic_cnt_present ? "REDU" : "",
  7051. pps->transform_8x8_mode ? "8x8DCT" : ""
  7052. );
  7053. }
  7054. return 0;
  7055. }
  7056. /**
  7057. * finds the end of the current frame in the bitstream.
  7058. * @return the position of the first byte of the next frame, or -1
  7059. */
  7060. static int find_frame_end(H264Context *h, const uint8_t *buf, int buf_size){
  7061. int i;
  7062. uint32_t state;
  7063. ParseContext *pc = &(h->s.parse_context);
  7064. //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
  7065. // mb_addr= pc->mb_addr - 1;
  7066. state= pc->state;
  7067. for(i=0; i<=buf_size; i++){
  7068. if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  7069. tprintf("find_frame_end new startcode = %08x, frame_start_found = %d, pos = %d\n", state, pc->frame_start_found, i);
  7070. if(pc->frame_start_found){
  7071. // If there isn't one more byte in the buffer
  7072. // the test on first_mb_in_slice cannot be done yet
  7073. // do it at next call.
  7074. if (i >= buf_size) break;
  7075. if (buf[i] & 0x80) {
  7076. // first_mb_in_slice is 0, probably the first nal of a new
  7077. // slice
  7078. tprintf("find_frame_end frame_end_found, state = %08x, pos = %d\n", state, i);
  7079. pc->state=-1;
  7080. pc->frame_start_found= 0;
  7081. return i-4;
  7082. }
  7083. }
  7084. pc->frame_start_found = 1;
  7085. }
  7086. if((state&0xFFFFFF1F) == 0x107 || (state&0xFFFFFF1F) == 0x108 || (state&0xFFFFFF1F) == 0x109){
  7087. if(pc->frame_start_found){
  7088. pc->state=-1;
  7089. pc->frame_start_found= 0;
  7090. return i-4;
  7091. }
  7092. }
  7093. if (i<buf_size)
  7094. state= (state<<8) | buf[i];
  7095. }
  7096. pc->state= state;
  7097. return END_NOT_FOUND;
  7098. }
  7099. #ifdef CONFIG_H264_PARSER
  7100. static int h264_parse(AVCodecParserContext *s,
  7101. AVCodecContext *avctx,
  7102. uint8_t **poutbuf, int *poutbuf_size,
  7103. const uint8_t *buf, int buf_size)
  7104. {
  7105. H264Context *h = s->priv_data;
  7106. ParseContext *pc = &h->s.parse_context;
  7107. int next;
  7108. next= find_frame_end(h, buf, buf_size);
  7109. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  7110. *poutbuf = NULL;
  7111. *poutbuf_size = 0;
  7112. return buf_size;
  7113. }
  7114. *poutbuf = (uint8_t *)buf;
  7115. *poutbuf_size = buf_size;
  7116. return next;
  7117. }
  7118. static int h264_split(AVCodecContext *avctx,
  7119. const uint8_t *buf, int buf_size)
  7120. {
  7121. int i;
  7122. uint32_t state = -1;
  7123. int has_sps= 0;
  7124. for(i=0; i<=buf_size; i++){
  7125. if((state&0xFFFFFF1F) == 0x107)
  7126. has_sps=1;
  7127. /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  7128. }*/
  7129. if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
  7130. if(has_sps){
  7131. while(i>4 && buf[i-5]==0) i--;
  7132. return i-4;
  7133. }
  7134. }
  7135. if (i<buf_size)
  7136. state= (state<<8) | buf[i];
  7137. }
  7138. return 0;
  7139. }
  7140. #endif /* CONFIG_H264_PARSER */
  7141. static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){
  7142. MpegEncContext * const s = &h->s;
  7143. AVCodecContext * const avctx= s->avctx;
  7144. int buf_index=0;
  7145. #if 0
  7146. int i;
  7147. for(i=0; i<50; i++){
  7148. av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
  7149. }
  7150. #endif
  7151. h->slice_num = 0;
  7152. s->current_picture_ptr= NULL;
  7153. for(;;){
  7154. int consumed;
  7155. int dst_length;
  7156. int bit_length;
  7157. uint8_t *ptr;
  7158. int i, nalsize = 0;
  7159. if(h->is_avc) {
  7160. if(buf_index >= buf_size) break;
  7161. nalsize = 0;
  7162. for(i = 0; i < h->nal_length_size; i++)
  7163. nalsize = (nalsize << 8) | buf[buf_index++];
  7164. if(nalsize <= 1){
  7165. if(nalsize == 1){
  7166. buf_index++;
  7167. continue;
  7168. }else{
  7169. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  7170. break;
  7171. }
  7172. }
  7173. } else {
  7174. // start code prefix search
  7175. for(; buf_index + 3 < buf_size; buf_index++){
  7176. // this should allways succeed in the first iteration
  7177. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  7178. break;
  7179. }
  7180. if(buf_index+3 >= buf_size) break;
  7181. buf_index+=3;
  7182. }
  7183. ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, h->is_avc ? nalsize : buf_size - buf_index);
  7184. while(ptr[dst_length - 1] == 0 && dst_length > 1)
  7185. dst_length--;
  7186. bit_length= 8*dst_length - decode_rbsp_trailing(ptr + dst_length - 1);
  7187. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  7188. 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);
  7189. }
  7190. if (h->is_avc && (nalsize != consumed))
  7191. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  7192. buf_index += consumed;
  7193. if( (s->hurry_up == 1 && h->nal_ref_idc == 0) //FIXME dont discard SEI id
  7194. ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  7195. continue;
  7196. switch(h->nal_unit_type){
  7197. case NAL_IDR_SLICE:
  7198. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  7199. case NAL_SLICE:
  7200. init_get_bits(&s->gb, ptr, bit_length);
  7201. h->intra_gb_ptr=
  7202. h->inter_gb_ptr= &s->gb;
  7203. s->data_partitioning = 0;
  7204. if(decode_slice_header(h) < 0){
  7205. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  7206. break;
  7207. }
  7208. s->current_picture_ptr->key_frame= (h->nal_unit_type == NAL_IDR_SLICE);
  7209. if(h->redundant_pic_count==0 && s->hurry_up < 5
  7210. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  7211. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  7212. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  7213. && avctx->skip_frame < AVDISCARD_ALL)
  7214. decode_slice(h);
  7215. break;
  7216. case NAL_DPA:
  7217. init_get_bits(&s->gb, ptr, bit_length);
  7218. h->intra_gb_ptr=
  7219. h->inter_gb_ptr= NULL;
  7220. s->data_partitioning = 1;
  7221. if(decode_slice_header(h) < 0){
  7222. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  7223. }
  7224. break;
  7225. case NAL_DPB:
  7226. init_get_bits(&h->intra_gb, ptr, bit_length);
  7227. h->intra_gb_ptr= &h->intra_gb;
  7228. break;
  7229. case NAL_DPC:
  7230. init_get_bits(&h->inter_gb, ptr, bit_length);
  7231. h->inter_gb_ptr= &h->inter_gb;
  7232. if(h->redundant_pic_count==0 && h->intra_gb_ptr && s->data_partitioning
  7233. && s->hurry_up < 5
  7234. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  7235. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  7236. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  7237. && avctx->skip_frame < AVDISCARD_ALL)
  7238. decode_slice(h);
  7239. break;
  7240. case NAL_SEI:
  7241. init_get_bits(&s->gb, ptr, bit_length);
  7242. decode_sei(h);
  7243. break;
  7244. case NAL_SPS:
  7245. init_get_bits(&s->gb, ptr, bit_length);
  7246. decode_seq_parameter_set(h);
  7247. if(s->flags& CODEC_FLAG_LOW_DELAY)
  7248. s->low_delay=1;
  7249. if(avctx->has_b_frames < 2)
  7250. avctx->has_b_frames= !s->low_delay;
  7251. break;
  7252. case NAL_PPS:
  7253. init_get_bits(&s->gb, ptr, bit_length);
  7254. decode_picture_parameter_set(h, bit_length);
  7255. break;
  7256. case NAL_AUD:
  7257. case NAL_END_SEQUENCE:
  7258. case NAL_END_STREAM:
  7259. case NAL_FILLER_DATA:
  7260. case NAL_SPS_EXT:
  7261. case NAL_AUXILIARY_SLICE:
  7262. break;
  7263. default:
  7264. av_log(avctx, AV_LOG_ERROR, "Unknown NAL code: %d\n", h->nal_unit_type);
  7265. }
  7266. }
  7267. if(!s->current_picture_ptr) return buf_index; //no frame
  7268. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  7269. s->current_picture_ptr->pict_type= s->pict_type;
  7270. h->prev_frame_num_offset= h->frame_num_offset;
  7271. h->prev_frame_num= h->frame_num;
  7272. if(s->current_picture_ptr->reference){
  7273. h->prev_poc_msb= h->poc_msb;
  7274. h->prev_poc_lsb= h->poc_lsb;
  7275. }
  7276. if(s->current_picture_ptr->reference)
  7277. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  7278. ff_er_frame_end(s);
  7279. MPV_frame_end(s);
  7280. return buf_index;
  7281. }
  7282. /**
  7283. * returns the number of bytes consumed for building the current frame
  7284. */
  7285. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  7286. if(s->flags&CODEC_FLAG_TRUNCATED){
  7287. pos -= s->parse_context.last_index;
  7288. if(pos<0) pos=0; // FIXME remove (unneeded?)
  7289. return pos;
  7290. }else{
  7291. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  7292. if(pos+10>buf_size) pos=buf_size; // oops ;)
  7293. return pos;
  7294. }
  7295. }
  7296. static int decode_frame(AVCodecContext *avctx,
  7297. void *data, int *data_size,
  7298. uint8_t *buf, int buf_size)
  7299. {
  7300. H264Context *h = avctx->priv_data;
  7301. MpegEncContext *s = &h->s;
  7302. AVFrame *pict = data;
  7303. int buf_index;
  7304. s->flags= avctx->flags;
  7305. s->flags2= avctx->flags2;
  7306. /* no supplementary picture */
  7307. if (buf_size == 0) {
  7308. return 0;
  7309. }
  7310. if(s->flags&CODEC_FLAG_TRUNCATED){
  7311. int next= find_frame_end(h, buf, buf_size);
  7312. if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
  7313. return buf_size;
  7314. //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);
  7315. }
  7316. if(h->is_avc && !h->got_avcC) {
  7317. int i, cnt, nalsize;
  7318. unsigned char *p = avctx->extradata;
  7319. if(avctx->extradata_size < 7) {
  7320. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  7321. return -1;
  7322. }
  7323. if(*p != 1) {
  7324. av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
  7325. return -1;
  7326. }
  7327. /* sps and pps in the avcC always have length coded with 2 bytes,
  7328. so put a fake nal_length_size = 2 while parsing them */
  7329. h->nal_length_size = 2;
  7330. // Decode sps from avcC
  7331. cnt = *(p+5) & 0x1f; // Number of sps
  7332. p += 6;
  7333. for (i = 0; i < cnt; i++) {
  7334. nalsize = BE_16(p) + 2;
  7335. if(decode_nal_units(h, p, nalsize) < 0) {
  7336. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  7337. return -1;
  7338. }
  7339. p += nalsize;
  7340. }
  7341. // Decode pps from avcC
  7342. cnt = *(p++); // Number of pps
  7343. for (i = 0; i < cnt; i++) {
  7344. nalsize = BE_16(p) + 2;
  7345. if(decode_nal_units(h, p, nalsize) != nalsize) {
  7346. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  7347. return -1;
  7348. }
  7349. p += nalsize;
  7350. }
  7351. // Now store right nal length size, that will be use to parse all other nals
  7352. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  7353. // Do not reparse avcC
  7354. h->got_avcC = 1;
  7355. }
  7356. if(!h->is_avc && s->avctx->extradata_size && s->picture_number==0){
  7357. if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
  7358. return -1;
  7359. }
  7360. buf_index=decode_nal_units(h, buf, buf_size);
  7361. if(buf_index < 0)
  7362. return -1;
  7363. //FIXME do something with unavailable reference frames
  7364. // if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_index, buf_size);
  7365. if(!s->current_picture_ptr){
  7366. av_log(h->s.avctx, AV_LOG_DEBUG, "error, NO frame\n");
  7367. return -1;
  7368. }
  7369. {
  7370. Picture *out = s->current_picture_ptr;
  7371. #if 0 //decode order
  7372. *data_size = sizeof(AVFrame);
  7373. #else
  7374. /* Sort B-frames into display order */
  7375. Picture *cur = s->current_picture_ptr;
  7376. Picture *prev = h->delayed_output_pic;
  7377. int i, pics, cross_idr, out_of_order, out_idx;
  7378. if(h->sps.bitstream_restriction_flag
  7379. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  7380. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  7381. s->low_delay = 0;
  7382. }
  7383. pics = 0;
  7384. while(h->delayed_pic[pics]) pics++;
  7385. h->delayed_pic[pics++] = cur;
  7386. if(cur->reference == 0)
  7387. cur->reference = 1;
  7388. cross_idr = 0;
  7389. for(i=0; h->delayed_pic[i]; i++)
  7390. if(h->delayed_pic[i]->key_frame || h->delayed_pic[i]->poc==0)
  7391. cross_idr = 1;
  7392. out = h->delayed_pic[0];
  7393. out_idx = 0;
  7394. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
  7395. if(h->delayed_pic[i]->poc < out->poc){
  7396. out = h->delayed_pic[i];
  7397. out_idx = i;
  7398. }
  7399. out_of_order = !cross_idr && prev && out->poc < prev->poc;
  7400. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  7401. { }
  7402. else if(prev && pics <= s->avctx->has_b_frames)
  7403. out = prev;
  7404. else if((out_of_order && pics-1 == s->avctx->has_b_frames && pics < 15)
  7405. || (s->low_delay &&
  7406. ((!cross_idr && prev && out->poc > prev->poc + 2)
  7407. || cur->pict_type == B_TYPE)))
  7408. {
  7409. s->low_delay = 0;
  7410. s->avctx->has_b_frames++;
  7411. out = prev;
  7412. }
  7413. else if(out_of_order)
  7414. out = prev;
  7415. if(out_of_order || pics > s->avctx->has_b_frames){
  7416. for(i=out_idx; h->delayed_pic[i]; i++)
  7417. h->delayed_pic[i] = h->delayed_pic[i+1];
  7418. }
  7419. if(prev == out)
  7420. *data_size = 0;
  7421. else
  7422. *data_size = sizeof(AVFrame);
  7423. if(prev && prev != out && prev->reference == 1)
  7424. prev->reference = 0;
  7425. h->delayed_output_pic = out;
  7426. #endif
  7427. if(out)
  7428. *pict= *(AVFrame*)out;
  7429. else
  7430. av_log(avctx, AV_LOG_DEBUG, "no picture\n");
  7431. }
  7432. assert(pict->data[0] || !*data_size);
  7433. ff_print_debug_info(s, pict);
  7434. //printf("out %d\n", (int)pict->data[0]);
  7435. #if 0 //?
  7436. /* Return the Picture timestamp as the frame number */
  7437. /* we substract 1 because it is added on utils.c */
  7438. avctx->frame_number = s->picture_number - 1;
  7439. #endif
  7440. return get_consumed_bytes(s, buf_index, buf_size);
  7441. }
  7442. #if 0
  7443. static inline void fill_mb_avail(H264Context *h){
  7444. MpegEncContext * const s = &h->s;
  7445. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  7446. if(s->mb_y){
  7447. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  7448. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  7449. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  7450. }else{
  7451. h->mb_avail[0]=
  7452. h->mb_avail[1]=
  7453. h->mb_avail[2]= 0;
  7454. }
  7455. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  7456. h->mb_avail[4]= 1; //FIXME move out
  7457. h->mb_avail[5]= 0; //FIXME move out
  7458. }
  7459. #endif
  7460. #if 0 //selftest
  7461. #define COUNT 8000
  7462. #define SIZE (COUNT*40)
  7463. int main(){
  7464. int i;
  7465. uint8_t temp[SIZE];
  7466. PutBitContext pb;
  7467. GetBitContext gb;
  7468. // int int_temp[10000];
  7469. DSPContext dsp;
  7470. AVCodecContext avctx;
  7471. dsputil_init(&dsp, &avctx);
  7472. init_put_bits(&pb, temp, SIZE);
  7473. printf("testing unsigned exp golomb\n");
  7474. for(i=0; i<COUNT; i++){
  7475. START_TIMER
  7476. set_ue_golomb(&pb, i);
  7477. STOP_TIMER("set_ue_golomb");
  7478. }
  7479. flush_put_bits(&pb);
  7480. init_get_bits(&gb, temp, 8*SIZE);
  7481. for(i=0; i<COUNT; i++){
  7482. int j, s;
  7483. s= show_bits(&gb, 24);
  7484. START_TIMER
  7485. j= get_ue_golomb(&gb);
  7486. if(j != i){
  7487. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7488. // return -1;
  7489. }
  7490. STOP_TIMER("get_ue_golomb");
  7491. }
  7492. init_put_bits(&pb, temp, SIZE);
  7493. printf("testing signed exp golomb\n");
  7494. for(i=0; i<COUNT; i++){
  7495. START_TIMER
  7496. set_se_golomb(&pb, i - COUNT/2);
  7497. STOP_TIMER("set_se_golomb");
  7498. }
  7499. flush_put_bits(&pb);
  7500. init_get_bits(&gb, temp, 8*SIZE);
  7501. for(i=0; i<COUNT; i++){
  7502. int j, s;
  7503. s= show_bits(&gb, 24);
  7504. START_TIMER
  7505. j= get_se_golomb(&gb);
  7506. if(j != i - COUNT/2){
  7507. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7508. // return -1;
  7509. }
  7510. STOP_TIMER("get_se_golomb");
  7511. }
  7512. printf("testing 4x4 (I)DCT\n");
  7513. DCTELEM block[16];
  7514. uint8_t src[16], ref[16];
  7515. uint64_t error= 0, max_error=0;
  7516. for(i=0; i<COUNT; i++){
  7517. int j;
  7518. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  7519. for(j=0; j<16; j++){
  7520. ref[j]= random()%255;
  7521. src[j]= random()%255;
  7522. }
  7523. h264_diff_dct_c(block, src, ref, 4);
  7524. //normalize
  7525. for(j=0; j<16; j++){
  7526. // printf("%d ", block[j]);
  7527. block[j]= block[j]*4;
  7528. if(j&1) block[j]= (block[j]*4 + 2)/5;
  7529. if(j&4) block[j]= (block[j]*4 + 2)/5;
  7530. }
  7531. // printf("\n");
  7532. s->dsp.h264_idct_add(ref, block, 4);
  7533. /* for(j=0; j<16; j++){
  7534. printf("%d ", ref[j]);
  7535. }
  7536. printf("\n");*/
  7537. for(j=0; j<16; j++){
  7538. int diff= FFABS(src[j] - ref[j]);
  7539. error+= diff*diff;
  7540. max_error= FFMAX(max_error, diff);
  7541. }
  7542. }
  7543. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  7544. #if 0
  7545. printf("testing quantizer\n");
  7546. for(qp=0; qp<52; qp++){
  7547. for(i=0; i<16; i++)
  7548. src1_block[i]= src2_block[i]= random()%255;
  7549. }
  7550. #endif
  7551. printf("Testing NAL layer\n");
  7552. uint8_t bitstream[COUNT];
  7553. uint8_t nal[COUNT*2];
  7554. H264Context h;
  7555. memset(&h, 0, sizeof(H264Context));
  7556. for(i=0; i<COUNT; i++){
  7557. int zeros= i;
  7558. int nal_length;
  7559. int consumed;
  7560. int out_length;
  7561. uint8_t *out;
  7562. int j;
  7563. for(j=0; j<COUNT; j++){
  7564. bitstream[j]= (random() % 255) + 1;
  7565. }
  7566. for(j=0; j<zeros; j++){
  7567. int pos= random() % COUNT;
  7568. while(bitstream[pos] == 0){
  7569. pos++;
  7570. pos %= COUNT;
  7571. }
  7572. bitstream[pos]=0;
  7573. }
  7574. START_TIMER
  7575. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  7576. if(nal_length<0){
  7577. printf("encoding failed\n");
  7578. return -1;
  7579. }
  7580. out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
  7581. STOP_TIMER("NAL")
  7582. if(out_length != COUNT){
  7583. printf("incorrect length %d %d\n", out_length, COUNT);
  7584. return -1;
  7585. }
  7586. if(consumed != nal_length){
  7587. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  7588. return -1;
  7589. }
  7590. if(memcmp(bitstream, out, COUNT)){
  7591. printf("missmatch\n");
  7592. return -1;
  7593. }
  7594. }
  7595. printf("Testing RBSP\n");
  7596. return 0;
  7597. }
  7598. #endif
  7599. static int decode_end(AVCodecContext *avctx)
  7600. {
  7601. H264Context *h = avctx->priv_data;
  7602. MpegEncContext *s = &h->s;
  7603. av_freep(&h->rbsp_buffer);
  7604. free_tables(h); //FIXME cleanup init stuff perhaps
  7605. MPV_common_end(s);
  7606. // memset(h, 0, sizeof(H264Context));
  7607. return 0;
  7608. }
  7609. AVCodec h264_decoder = {
  7610. "h264",
  7611. CODEC_TYPE_VIDEO,
  7612. CODEC_ID_H264,
  7613. sizeof(H264Context),
  7614. decode_init,
  7615. NULL,
  7616. decode_end,
  7617. decode_frame,
  7618. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  7619. .flush= flush_dpb,
  7620. };
  7621. #ifdef CONFIG_H264_PARSER
  7622. AVCodecParser h264_parser = {
  7623. { CODEC_ID_H264 },
  7624. sizeof(H264Context),
  7625. NULL,
  7626. h264_parse,
  7627. ff_parse_close,
  7628. h264_split,
  7629. };
  7630. #endif
  7631. #include "svq3.c"