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.

8503 lines
317KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. */
  20. /**
  21. * @file h264.c
  22. * H.264 / AVC / MPEG4 part10 codec.
  23. * @author Michael Niedermayer <michaelni@gmx.at>
  24. */
  25. #include "common.h"
  26. #include "dsputil.h"
  27. #include "avcodec.h"
  28. #include "mpegvideo.h"
  29. #include "h264data.h"
  30. #include "golomb.h"
  31. #include "cabac.h"
  32. //#undef NDEBUG
  33. #include <assert.h>
  34. #define interlaced_dct interlaced_dct_is_a_bad_name
  35. #define mb_intra mb_intra_isnt_initalized_see_mb_type
  36. #define LUMA_DC_BLOCK_INDEX 25
  37. #define CHROMA_DC_BLOCK_INDEX 26
  38. #define CHROMA_DC_COEFF_TOKEN_VLC_BITS 8
  39. #define COEFF_TOKEN_VLC_BITS 8
  40. #define TOTAL_ZEROS_VLC_BITS 9
  41. #define CHROMA_DC_TOTAL_ZEROS_VLC_BITS 3
  42. #define RUN_VLC_BITS 3
  43. #define RUN7_VLC_BITS 6
  44. #define MAX_SPS_COUNT 32
  45. #define MAX_PPS_COUNT 256
  46. #define MAX_MMCO_COUNT 66
  47. /* Compiling in interlaced support reduces the speed
  48. * of progressive decoding by about 2%. */
  49. #define ALLOW_INTERLACE
  50. #ifdef ALLOW_INTERLACE
  51. #define MB_MBAFF h->mb_mbaff
  52. #define MB_FIELD h->mb_field_decoding_flag
  53. #define FRAME_MBAFF h->mb_aff_frame
  54. #else
  55. #define MB_MBAFF 0
  56. #define MB_FIELD 0
  57. #define FRAME_MBAFF 0
  58. #undef IS_INTERLACED
  59. #define IS_INTERLACED(mb_type) 0
  60. #endif
  61. /**
  62. * Sequence parameter set
  63. */
  64. typedef struct SPS{
  65. int profile_idc;
  66. int level_idc;
  67. int transform_bypass; ///< qpprime_y_zero_transform_bypass_flag
  68. int log2_max_frame_num; ///< log2_max_frame_num_minus4 + 4
  69. int poc_type; ///< pic_order_cnt_type
  70. int log2_max_poc_lsb; ///< log2_max_pic_order_cnt_lsb_minus4
  71. int delta_pic_order_always_zero_flag;
  72. int offset_for_non_ref_pic;
  73. int offset_for_top_to_bottom_field;
  74. int poc_cycle_length; ///< num_ref_frames_in_pic_order_cnt_cycle
  75. int ref_frame_count; ///< num_ref_frames
  76. int gaps_in_frame_num_allowed_flag;
  77. int mb_width; ///< frame_width_in_mbs_minus1 + 1
  78. int mb_height; ///< frame_height_in_mbs_minus1 + 1
  79. int frame_mbs_only_flag;
  80. int mb_aff; ///<mb_adaptive_frame_field_flag
  81. int direct_8x8_inference_flag;
  82. int crop; ///< frame_cropping_flag
  83. int crop_left; ///< frame_cropping_rect_left_offset
  84. int crop_right; ///< frame_cropping_rect_right_offset
  85. int crop_top; ///< frame_cropping_rect_top_offset
  86. int crop_bottom; ///< frame_cropping_rect_bottom_offset
  87. int vui_parameters_present_flag;
  88. AVRational sar;
  89. int timing_info_present_flag;
  90. uint32_t num_units_in_tick;
  91. uint32_t time_scale;
  92. int fixed_frame_rate_flag;
  93. short offset_for_ref_frame[256]; //FIXME dyn aloc?
  94. int bitstream_restriction_flag;
  95. int num_reorder_frames;
  96. int scaling_matrix_present;
  97. uint8_t scaling_matrix4[6][16];
  98. uint8_t scaling_matrix8[2][64];
  99. }SPS;
  100. /**
  101. * Picture parameter set
  102. */
  103. typedef struct PPS{
  104. int sps_id;
  105. int cabac; ///< entropy_coding_mode_flag
  106. int pic_order_present; ///< pic_order_present_flag
  107. int slice_group_count; ///< num_slice_groups_minus1 + 1
  108. int mb_slice_group_map_type;
  109. int ref_count[2]; ///< num_ref_idx_l0/1_active_minus1 + 1
  110. int weighted_pred; ///< weighted_pred_flag
  111. int weighted_bipred_idc;
  112. int init_qp; ///< pic_init_qp_minus26 + 26
  113. int init_qs; ///< pic_init_qs_minus26 + 26
  114. int chroma_qp_index_offset;
  115. int deblocking_filter_parameters_present; ///< deblocking_filter_parameters_present_flag
  116. int constrained_intra_pred; ///< constrained_intra_pred_flag
  117. int redundant_pic_cnt_present; ///< redundant_pic_cnt_present_flag
  118. int transform_8x8_mode; ///< transform_8x8_mode_flag
  119. uint8_t scaling_matrix4[6][16];
  120. uint8_t scaling_matrix8[2][64];
  121. }PPS;
  122. /**
  123. * Memory management control operation opcode.
  124. */
  125. typedef enum MMCOOpcode{
  126. MMCO_END=0,
  127. MMCO_SHORT2UNUSED,
  128. MMCO_LONG2UNUSED,
  129. MMCO_SHORT2LONG,
  130. MMCO_SET_MAX_LONG,
  131. MMCO_RESET,
  132. MMCO_LONG,
  133. } MMCOOpcode;
  134. /**
  135. * Memory management control operation.
  136. */
  137. typedef struct MMCO{
  138. MMCOOpcode opcode;
  139. int short_frame_num;
  140. int long_index;
  141. } MMCO;
  142. /**
  143. * H264Context
  144. */
  145. typedef struct H264Context{
  146. MpegEncContext s;
  147. int nal_ref_idc;
  148. int nal_unit_type;
  149. #define NAL_SLICE 1
  150. #define NAL_DPA 2
  151. #define NAL_DPB 3
  152. #define NAL_DPC 4
  153. #define NAL_IDR_SLICE 5
  154. #define NAL_SEI 6
  155. #define NAL_SPS 7
  156. #define NAL_PPS 8
  157. #define NAL_AUD 9
  158. #define NAL_END_SEQUENCE 10
  159. #define NAL_END_STREAM 11
  160. #define NAL_FILLER_DATA 12
  161. #define NAL_SPS_EXT 13
  162. #define NAL_AUXILIARY_SLICE 19
  163. uint8_t *rbsp_buffer;
  164. unsigned int rbsp_buffer_size;
  165. /**
  166. * Used to parse AVC variant of h264
  167. */
  168. int is_avc; ///< this flag is != 0 if codec is avc1
  169. int got_avcC; ///< flag used to parse avcC data only once
  170. int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
  171. int chroma_qp; //QPc
  172. int prev_mb_skipped;
  173. int next_mb_skipped;
  174. //prediction stuff
  175. int chroma_pred_mode;
  176. int intra16x16_pred_mode;
  177. int top_mb_xy;
  178. int left_mb_xy[2];
  179. int8_t intra4x4_pred_mode_cache[5*8];
  180. int8_t (*intra4x4_pred_mode)[8];
  181. void (*pred4x4 [9+3])(uint8_t *src, uint8_t *topright, int stride);//FIXME move to dsp?
  182. void (*pred8x8l [9+3])(uint8_t *src, int topleft, int topright, int stride);
  183. void (*pred8x8 [4+3])(uint8_t *src, int stride);
  184. void (*pred16x16[4+3])(uint8_t *src, int stride);
  185. unsigned int topleft_samples_available;
  186. unsigned int top_samples_available;
  187. unsigned int topright_samples_available;
  188. unsigned int left_samples_available;
  189. uint8_t (*top_borders[2])[16+2*8];
  190. uint8_t left_border[2*(17+2*9)];
  191. /**
  192. * non zero coeff count cache.
  193. * is 64 if not available.
  194. */
  195. DECLARE_ALIGNED_8(uint8_t, non_zero_count_cache[6*8]);
  196. uint8_t (*non_zero_count)[16];
  197. /**
  198. * Motion vector cache.
  199. */
  200. DECLARE_ALIGNED_8(int16_t, mv_cache[2][5*8][2]);
  201. DECLARE_ALIGNED_8(int8_t, ref_cache[2][5*8]);
  202. #define LIST_NOT_USED -1 //FIXME rename?
  203. #define PART_NOT_AVAILABLE -2
  204. /**
  205. * is 1 if the specific list MV&references are set to 0,0,-2.
  206. */
  207. int mv_cache_clean[2];
  208. /**
  209. * number of neighbors (top and/or left) that used 8x8 dct
  210. */
  211. int neighbor_transform_size;
  212. /**
  213. * block_offset[ 0..23] for frame macroblocks
  214. * block_offset[24..47] for field macroblocks
  215. */
  216. int block_offset[2*(16+8)];
  217. uint32_t *mb2b_xy; //FIXME are these 4 a good idea?
  218. uint32_t *mb2b8_xy;
  219. int b_stride; //FIXME use s->b4_stride
  220. int b8_stride;
  221. int mb_linesize; ///< may be equal to s->linesize or s->linesize*2, for mbaff
  222. int mb_uvlinesize;
  223. int emu_edge_width;
  224. int emu_edge_height;
  225. int halfpel_flag;
  226. int thirdpel_flag;
  227. int unknown_svq3_flag;
  228. int next_slice_index;
  229. SPS sps_buffer[MAX_SPS_COUNT];
  230. SPS sps; ///< current sps
  231. PPS pps_buffer[MAX_PPS_COUNT];
  232. /**
  233. * current pps
  234. */
  235. PPS pps; //FIXME move to Picture perhaps? (->no) do we need that?
  236. uint32_t dequant4_buffer[6][52][16];
  237. uint32_t dequant8_buffer[2][52][64];
  238. uint32_t (*dequant4_coeff[6])[16];
  239. uint32_t (*dequant8_coeff[2])[64];
  240. int dequant_coeff_pps; ///< reinit tables when pps changes
  241. int slice_num;
  242. uint8_t *slice_table_base;
  243. uint8_t *slice_table; ///< slice_table_base + 2*mb_stride + 1
  244. int slice_type;
  245. int slice_type_fixed;
  246. //interlacing specific flags
  247. int mb_aff_frame;
  248. int mb_field_decoding_flag;
  249. int mb_mbaff; ///< mb_aff_frame && mb_field_decoding_flag
  250. int sub_mb_type[4];
  251. //POC stuff
  252. int poc_lsb;
  253. int poc_msb;
  254. int delta_poc_bottom;
  255. int delta_poc[2];
  256. int frame_num;
  257. int prev_poc_msb; ///< poc_msb of the last reference pic for POC type 0
  258. int prev_poc_lsb; ///< poc_lsb of the last reference pic for POC type 0
  259. int frame_num_offset; ///< for POC type 2
  260. int prev_frame_num_offset; ///< for POC type 2
  261. int prev_frame_num; ///< frame_num of the last pic for POC type 1/2
  262. /**
  263. * frame_num for frames or 2*frame_num for field pics.
  264. */
  265. int curr_pic_num;
  266. /**
  267. * max_frame_num or 2*max_frame_num for field pics.
  268. */
  269. int max_pic_num;
  270. //Weighted pred stuff
  271. int use_weight;
  272. int use_weight_chroma;
  273. int luma_log2_weight_denom;
  274. int chroma_log2_weight_denom;
  275. int luma_weight[2][48];
  276. int luma_offset[2][48];
  277. int chroma_weight[2][48][2];
  278. int chroma_offset[2][48][2];
  279. int implicit_weight[48][48];
  280. //deblock
  281. int deblocking_filter; ///< disable_deblocking_filter_idc with 1<->0
  282. int slice_alpha_c0_offset;
  283. int slice_beta_offset;
  284. int redundant_pic_count;
  285. int direct_spatial_mv_pred;
  286. int dist_scale_factor[16];
  287. int dist_scale_factor_field[32];
  288. int map_col_to_list0[2][16];
  289. int map_col_to_list0_field[2][32];
  290. /**
  291. * num_ref_idx_l0/1_active_minus1 + 1
  292. */
  293. int ref_count[2]; ///< counts frames or fields, depending on current mb mode
  294. Picture *short_ref[32];
  295. Picture *long_ref[32];
  296. Picture default_ref_list[2][32];
  297. Picture ref_list[2][48]; ///< 0..15: frame refs, 16..47: mbaff field refs
  298. Picture *delayed_pic[16]; //FIXME size?
  299. Picture *delayed_output_pic;
  300. /**
  301. * memory management control operations buffer.
  302. */
  303. MMCO mmco[MAX_MMCO_COUNT];
  304. int mmco_index;
  305. int long_ref_count; ///< number of actual long term references
  306. int short_ref_count; ///< number of actual short term references
  307. //data partitioning
  308. GetBitContext intra_gb;
  309. GetBitContext inter_gb;
  310. GetBitContext *intra_gb_ptr;
  311. GetBitContext *inter_gb_ptr;
  312. DECLARE_ALIGNED_8(DCTELEM, mb[16*24]);
  313. /**
  314. * Cabac
  315. */
  316. CABACContext cabac;
  317. uint8_t cabac_state[460];
  318. int cabac_init_idc;
  319. /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
  320. uint16_t *cbp_table;
  321. int top_cbp;
  322. int left_cbp;
  323. /* chroma_pred_mode for i4x4 or i16x16, else 0 */
  324. uint8_t *chroma_pred_mode_table;
  325. int last_qscale_diff;
  326. int16_t (*mvd_table[2])[2];
  327. DECLARE_ALIGNED_8(int16_t, mvd_cache[2][5*8][2]);
  328. uint8_t *direct_table;
  329. uint8_t direct_cache[5*8];
  330. uint8_t zigzag_scan[16];
  331. uint8_t zigzag_scan8x8[64];
  332. uint8_t zigzag_scan8x8_cavlc[64];
  333. uint8_t field_scan[16];
  334. uint8_t field_scan8x8[64];
  335. uint8_t field_scan8x8_cavlc[64];
  336. const uint8_t *zigzag_scan_q0;
  337. const uint8_t *zigzag_scan8x8_q0;
  338. const uint8_t *zigzag_scan8x8_cavlc_q0;
  339. const uint8_t *field_scan_q0;
  340. const uint8_t *field_scan8x8_q0;
  341. const uint8_t *field_scan8x8_cavlc_q0;
  342. int x264_build;
  343. }H264Context;
  344. static VLC coeff_token_vlc[4];
  345. static VLC chroma_dc_coeff_token_vlc;
  346. static VLC total_zeros_vlc[15];
  347. static VLC chroma_dc_total_zeros_vlc[3];
  348. static VLC run_vlc[6];
  349. static VLC run7_vlc;
  350. static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
  351. static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
  352. 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);
  353. static always_inline uint32_t pack16to32(int a, int b){
  354. #ifdef WORDS_BIGENDIAN
  355. return (b&0xFFFF) + (a<<16);
  356. #else
  357. return (a&0xFFFF) + (b<<16);
  358. #endif
  359. }
  360. /**
  361. * fill a rectangle.
  362. * @param h height of the rectangle, should be a constant
  363. * @param w width of the rectangle, should be a constant
  364. * @param size the size of val (1 or 4), should be a constant
  365. */
  366. static always_inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){
  367. uint8_t *p= (uint8_t*)vp;
  368. assert(size==1 || size==4);
  369. assert(w<=4);
  370. w *= size;
  371. stride *= size;
  372. assert((((long)vp)&(FFMIN(w, STRIDE_ALIGN)-1)) == 0);
  373. assert((stride&(w-1))==0);
  374. if(w==2){
  375. const uint16_t v= size==4 ? val : val*0x0101;
  376. *(uint16_t*)(p + 0*stride)= v;
  377. if(h==1) return;
  378. *(uint16_t*)(p + 1*stride)= v;
  379. if(h==2) return;
  380. *(uint16_t*)(p + 2*stride)=
  381. *(uint16_t*)(p + 3*stride)= v;
  382. }else if(w==4){
  383. const uint32_t v= size==4 ? val : val*0x01010101;
  384. *(uint32_t*)(p + 0*stride)= v;
  385. if(h==1) return;
  386. *(uint32_t*)(p + 1*stride)= v;
  387. if(h==2) return;
  388. *(uint32_t*)(p + 2*stride)=
  389. *(uint32_t*)(p + 3*stride)= v;
  390. }else if(w==8){
  391. //gcc can't optimize 64bit math on x86_32
  392. #if defined(ARCH_X86_64) || (defined(MP_WORDSIZE) && MP_WORDSIZE >= 64)
  393. const uint64_t v= val*0x0100000001ULL;
  394. *(uint64_t*)(p + 0*stride)= v;
  395. if(h==1) return;
  396. *(uint64_t*)(p + 1*stride)= v;
  397. if(h==2) return;
  398. *(uint64_t*)(p + 2*stride)=
  399. *(uint64_t*)(p + 3*stride)= v;
  400. }else if(w==16){
  401. const uint64_t v= val*0x0100000001ULL;
  402. *(uint64_t*)(p + 0+0*stride)=
  403. *(uint64_t*)(p + 8+0*stride)=
  404. *(uint64_t*)(p + 0+1*stride)=
  405. *(uint64_t*)(p + 8+1*stride)= v;
  406. if(h==2) return;
  407. *(uint64_t*)(p + 0+2*stride)=
  408. *(uint64_t*)(p + 8+2*stride)=
  409. *(uint64_t*)(p + 0+3*stride)=
  410. *(uint64_t*)(p + 8+3*stride)= v;
  411. #else
  412. *(uint32_t*)(p + 0+0*stride)=
  413. *(uint32_t*)(p + 4+0*stride)= val;
  414. if(h==1) return;
  415. *(uint32_t*)(p + 0+1*stride)=
  416. *(uint32_t*)(p + 4+1*stride)= val;
  417. if(h==2) return;
  418. *(uint32_t*)(p + 0+2*stride)=
  419. *(uint32_t*)(p + 4+2*stride)=
  420. *(uint32_t*)(p + 0+3*stride)=
  421. *(uint32_t*)(p + 4+3*stride)= val;
  422. }else if(w==16){
  423. *(uint32_t*)(p + 0+0*stride)=
  424. *(uint32_t*)(p + 4+0*stride)=
  425. *(uint32_t*)(p + 8+0*stride)=
  426. *(uint32_t*)(p +12+0*stride)=
  427. *(uint32_t*)(p + 0+1*stride)=
  428. *(uint32_t*)(p + 4+1*stride)=
  429. *(uint32_t*)(p + 8+1*stride)=
  430. *(uint32_t*)(p +12+1*stride)= val;
  431. if(h==2) return;
  432. *(uint32_t*)(p + 0+2*stride)=
  433. *(uint32_t*)(p + 4+2*stride)=
  434. *(uint32_t*)(p + 8+2*stride)=
  435. *(uint32_t*)(p +12+2*stride)=
  436. *(uint32_t*)(p + 0+3*stride)=
  437. *(uint32_t*)(p + 4+3*stride)=
  438. *(uint32_t*)(p + 8+3*stride)=
  439. *(uint32_t*)(p +12+3*stride)= val;
  440. #endif
  441. }else
  442. assert(0);
  443. assert(h==4);
  444. }
  445. static void fill_caches(H264Context *h, int mb_type, int for_deblock){
  446. MpegEncContext * const s = &h->s;
  447. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  448. int topleft_xy, top_xy, topright_xy, left_xy[2];
  449. int topleft_type, top_type, topright_type, left_type[2];
  450. int left_block[8];
  451. int i;
  452. //FIXME deblocking could skip the intra and nnz parts.
  453. if(for_deblock && (h->slice_num == 1 || h->slice_table[mb_xy] == h->slice_table[mb_xy-s->mb_stride]) && !FRAME_MBAFF)
  454. return;
  455. //wow what a mess, why didn't they simplify the interlacing&intra stuff, i can't imagine that these complex rules are worth it
  456. top_xy = mb_xy - s->mb_stride;
  457. topleft_xy = top_xy - 1;
  458. topright_xy= top_xy + 1;
  459. left_xy[1] = left_xy[0] = mb_xy-1;
  460. left_block[0]= 0;
  461. left_block[1]= 1;
  462. left_block[2]= 2;
  463. left_block[3]= 3;
  464. left_block[4]= 7;
  465. left_block[5]= 10;
  466. left_block[6]= 8;
  467. left_block[7]= 11;
  468. if(FRAME_MBAFF){
  469. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  470. const int top_pair_xy = pair_xy - s->mb_stride;
  471. const int topleft_pair_xy = top_pair_xy - 1;
  472. const int topright_pair_xy = top_pair_xy + 1;
  473. const int topleft_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
  474. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  475. const int topright_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
  476. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  477. const int curr_mb_frame_flag = !IS_INTERLACED(mb_type);
  478. const int bottom = (s->mb_y & 1);
  479. 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);
  480. if (bottom
  481. ? !curr_mb_frame_flag // bottom macroblock
  482. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  483. ) {
  484. top_xy -= s->mb_stride;
  485. }
  486. if (bottom
  487. ? !curr_mb_frame_flag // bottom macroblock
  488. : (!curr_mb_frame_flag && !topleft_mb_frame_flag) // top macroblock
  489. ) {
  490. topleft_xy -= s->mb_stride;
  491. }
  492. if (bottom
  493. ? !curr_mb_frame_flag // bottom macroblock
  494. : (!curr_mb_frame_flag && !topright_mb_frame_flag) // top macroblock
  495. ) {
  496. topright_xy -= s->mb_stride;
  497. }
  498. if (left_mb_frame_flag != curr_mb_frame_flag) {
  499. left_xy[1] = left_xy[0] = pair_xy - 1;
  500. if (curr_mb_frame_flag) {
  501. if (bottom) {
  502. left_block[0]= 2;
  503. left_block[1]= 2;
  504. left_block[2]= 3;
  505. left_block[3]= 3;
  506. left_block[4]= 8;
  507. left_block[5]= 11;
  508. left_block[6]= 8;
  509. left_block[7]= 11;
  510. } else {
  511. left_block[0]= 0;
  512. left_block[1]= 0;
  513. left_block[2]= 1;
  514. left_block[3]= 1;
  515. left_block[4]= 7;
  516. left_block[5]= 10;
  517. left_block[6]= 7;
  518. left_block[7]= 10;
  519. }
  520. } else {
  521. left_xy[1] += s->mb_stride;
  522. //left_block[0]= 0;
  523. left_block[1]= 2;
  524. left_block[2]= 0;
  525. left_block[3]= 2;
  526. //left_block[4]= 7;
  527. left_block[5]= 10;
  528. left_block[6]= 7;
  529. left_block[7]= 10;
  530. }
  531. }
  532. }
  533. h->top_mb_xy = top_xy;
  534. h->left_mb_xy[0] = left_xy[0];
  535. h->left_mb_xy[1] = left_xy[1];
  536. if(for_deblock){
  537. topleft_type = 0;
  538. topright_type = 0;
  539. top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0;
  540. left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
  541. left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
  542. if(FRAME_MBAFF && !IS_INTRA(mb_type)){
  543. int list;
  544. int v = *(uint16_t*)&h->non_zero_count[mb_xy][14];
  545. for(i=0; i<16; i++)
  546. h->non_zero_count_cache[scan8[i]] = (v>>i)&1;
  547. for(list=0; list<1+(h->slice_type==B_TYPE); list++){
  548. if(USES_LIST(mb_type,list)){
  549. uint32_t *src = (uint32_t*)s->current_picture.motion_val[list][h->mb2b_xy[mb_xy]];
  550. uint32_t *dst = (uint32_t*)h->mv_cache[list][scan8[0]];
  551. uint8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
  552. for(i=0; i<4; i++, dst+=8, src+=h->b_stride){
  553. dst[0] = src[0];
  554. dst[1] = src[1];
  555. dst[2] = src[2];
  556. dst[3] = src[3];
  557. }
  558. *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =
  559. *(uint32_t*)&h->ref_cache[list][scan8[ 2]] = pack16to32(ref[0],ref[1])*0x0101;
  560. ref += h->b8_stride;
  561. *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =
  562. *(uint32_t*)&h->ref_cache[list][scan8[10]] = pack16to32(ref[0],ref[1])*0x0101;
  563. }else{
  564. fill_rectangle(&h-> mv_cache[list][scan8[ 0]], 4, 4, 8, 0, 4);
  565. fill_rectangle(&h->ref_cache[list][scan8[ 0]], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  566. }
  567. }
  568. }
  569. }else{
  570. topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
  571. top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
  572. topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
  573. left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
  574. left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
  575. }
  576. if(IS_INTRA(mb_type)){
  577. h->topleft_samples_available=
  578. h->top_samples_available=
  579. h->left_samples_available= 0xFFFF;
  580. h->topright_samples_available= 0xEEEA;
  581. if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
  582. h->topleft_samples_available= 0xB3FF;
  583. h->top_samples_available= 0x33FF;
  584. h->topright_samples_available= 0x26EA;
  585. }
  586. for(i=0; i<2; i++){
  587. if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
  588. h->topleft_samples_available&= 0xDF5F;
  589. h->left_samples_available&= 0x5F5F;
  590. }
  591. }
  592. if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
  593. h->topleft_samples_available&= 0x7FFF;
  594. if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
  595. h->topright_samples_available&= 0xFBFF;
  596. if(IS_INTRA4x4(mb_type)){
  597. if(IS_INTRA4x4(top_type)){
  598. h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
  599. h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
  600. h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
  601. h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
  602. }else{
  603. int pred;
  604. if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred))
  605. pred= -1;
  606. else{
  607. pred= 2;
  608. }
  609. h->intra4x4_pred_mode_cache[4+8*0]=
  610. h->intra4x4_pred_mode_cache[5+8*0]=
  611. h->intra4x4_pred_mode_cache[6+8*0]=
  612. h->intra4x4_pred_mode_cache[7+8*0]= pred;
  613. }
  614. for(i=0; i<2; i++){
  615. if(IS_INTRA4x4(left_type[i])){
  616. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
  617. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
  618. }else{
  619. int pred;
  620. if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred))
  621. pred= -1;
  622. else{
  623. pred= 2;
  624. }
  625. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
  626. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
  627. }
  628. }
  629. }
  630. }
  631. /*
  632. 0 . T T. T T T T
  633. 1 L . .L . . . .
  634. 2 L . .L . . . .
  635. 3 . T TL . . . .
  636. 4 L . .L . . . .
  637. 5 L . .. . . . .
  638. */
  639. //FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec)
  640. if(top_type){
  641. h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
  642. h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
  643. h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
  644. h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
  645. h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
  646. h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
  647. h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
  648. h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
  649. }else{
  650. h->non_zero_count_cache[4+8*0]=
  651. h->non_zero_count_cache[5+8*0]=
  652. h->non_zero_count_cache[6+8*0]=
  653. h->non_zero_count_cache[7+8*0]=
  654. h->non_zero_count_cache[1+8*0]=
  655. h->non_zero_count_cache[2+8*0]=
  656. h->non_zero_count_cache[1+8*3]=
  657. h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  658. }
  659. for (i=0; i<2; i++) {
  660. if(left_type[i]){
  661. h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
  662. h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
  663. h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
  664. h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
  665. }else{
  666. h->non_zero_count_cache[3+8*1 + 2*8*i]=
  667. h->non_zero_count_cache[3+8*2 + 2*8*i]=
  668. h->non_zero_count_cache[0+8*1 + 8*i]=
  669. h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  670. }
  671. }
  672. if( h->pps.cabac ) {
  673. // top_cbp
  674. if(top_type) {
  675. h->top_cbp = h->cbp_table[top_xy];
  676. } else if(IS_INTRA(mb_type)) {
  677. h->top_cbp = 0x1C0;
  678. } else {
  679. h->top_cbp = 0;
  680. }
  681. // left_cbp
  682. if (left_type[0]) {
  683. h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
  684. } else if(IS_INTRA(mb_type)) {
  685. h->left_cbp = 0x1C0;
  686. } else {
  687. h->left_cbp = 0;
  688. }
  689. if (left_type[0]) {
  690. h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
  691. }
  692. if (left_type[1]) {
  693. h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
  694. }
  695. }
  696. #if 1
  697. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  698. int list;
  699. for(list=0; list<1+(h->slice_type==B_TYPE); list++){
  700. if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
  701. /*if(!h->mv_cache_clean[list]){
  702. memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
  703. memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
  704. h->mv_cache_clean[list]= 1;
  705. }*/
  706. continue;
  707. }
  708. h->mv_cache_clean[list]= 0;
  709. if(USES_LIST(top_type, list)){
  710. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  711. const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
  712. *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
  713. *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
  714. *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
  715. *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
  716. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  717. h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
  718. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  719. h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
  720. }else{
  721. *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
  722. *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
  723. *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
  724. *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
  725. *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
  726. }
  727. //FIXME unify cleanup or sth
  728. if(USES_LIST(left_type[0], list)){
  729. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  730. const int b8_xy= h->mb2b8_xy[left_xy[0]] + 1;
  731. *(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]];
  732. *(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]];
  733. h->ref_cache[list][scan8[0] - 1 + 0*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0]>>1)];
  734. h->ref_cache[list][scan8[0] - 1 + 1*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1]>>1)];
  735. }else{
  736. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 0*8]=
  737. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 1*8]= 0;
  738. h->ref_cache[list][scan8[0] - 1 + 0*8]=
  739. h->ref_cache[list][scan8[0] - 1 + 1*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  740. }
  741. if(USES_LIST(left_type[1], list)){
  742. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  743. const int b8_xy= h->mb2b8_xy[left_xy[1]] + 1;
  744. *(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]];
  745. *(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]];
  746. h->ref_cache[list][scan8[0] - 1 + 2*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[2]>>1)];
  747. h->ref_cache[list][scan8[0] - 1 + 3*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[3]>>1)];
  748. }else{
  749. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 2*8]=
  750. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 3*8]= 0;
  751. h->ref_cache[list][scan8[0] - 1 + 2*8]=
  752. h->ref_cache[list][scan8[0] - 1 + 3*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  753. assert((!left_type[0]) == (!left_type[1]));
  754. }
  755. if((for_deblock || (IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred)) && !FRAME_MBAFF)
  756. continue;
  757. if(USES_LIST(topleft_type, list)){
  758. const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
  759. const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + h->b8_stride;
  760. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  761. h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  762. }else{
  763. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
  764. h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  765. }
  766. if(USES_LIST(topright_type, list)){
  767. const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
  768. const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
  769. *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  770. h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  771. }else{
  772. *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
  773. h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  774. }
  775. if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)
  776. continue;
  777. h->ref_cache[list][scan8[5 ]+1] =
  778. h->ref_cache[list][scan8[7 ]+1] =
  779. h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewhere else)
  780. h->ref_cache[list][scan8[4 ]] =
  781. h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
  782. *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
  783. *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
  784. *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  785. *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
  786. *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
  787. if( h->pps.cabac ) {
  788. /* XXX beurk, Load mvd */
  789. if(USES_LIST(top_type, list)){
  790. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  791. *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
  792. *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
  793. *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
  794. *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
  795. }else{
  796. *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
  797. *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
  798. *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
  799. *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
  800. }
  801. if(USES_LIST(left_type[0], list)){
  802. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  803. *(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]];
  804. *(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]];
  805. }else{
  806. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
  807. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
  808. }
  809. if(USES_LIST(left_type[1], list)){
  810. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  811. *(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]];
  812. *(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]];
  813. }else{
  814. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
  815. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
  816. }
  817. *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
  818. *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
  819. *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  820. *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
  821. *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
  822. if(h->slice_type == B_TYPE){
  823. fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
  824. if(IS_DIRECT(top_type)){
  825. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
  826. }else if(IS_8X8(top_type)){
  827. int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
  828. h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
  829. h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
  830. }else{
  831. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
  832. }
  833. if(IS_DIRECT(left_type[0]))
  834. h->direct_cache[scan8[0] - 1 + 0*8]= 1;
  835. else if(IS_8X8(left_type[0]))
  836. 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)];
  837. else
  838. h->direct_cache[scan8[0] - 1 + 0*8]= 0;
  839. if(IS_DIRECT(left_type[1]))
  840. h->direct_cache[scan8[0] - 1 + 2*8]= 1;
  841. else if(IS_8X8(left_type[1]))
  842. 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)];
  843. else
  844. h->direct_cache[scan8[0] - 1 + 2*8]= 0;
  845. }
  846. }
  847. if(FRAME_MBAFF){
  848. #define MAP_MVS\
  849. MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
  850. MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
  851. MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
  852. MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
  853. MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
  854. MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
  855. MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\
  856. MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\
  857. MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\
  858. MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
  859. if(MB_FIELD){
  860. #define MAP_F2F(idx, mb_type)\
  861. if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  862. h->ref_cache[list][idx] <<= 1;\
  863. h->mv_cache[list][idx][1] /= 2;\
  864. h->mvd_cache[list][idx][1] /= 2;\
  865. }
  866. MAP_MVS
  867. #undef MAP_F2F
  868. }else{
  869. #define MAP_F2F(idx, mb_type)\
  870. if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  871. h->ref_cache[list][idx] >>= 1;\
  872. h->mv_cache[list][idx][1] <<= 1;\
  873. h->mvd_cache[list][idx][1] <<= 1;\
  874. }
  875. MAP_MVS
  876. #undef MAP_F2F
  877. }
  878. }
  879. }
  880. }
  881. #endif
  882. h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
  883. }
  884. static inline void write_back_intra_pred_mode(H264Context *h){
  885. MpegEncContext * const s = &h->s;
  886. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  887. h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
  888. h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
  889. h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
  890. h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
  891. h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
  892. h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
  893. h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
  894. }
  895. /**
  896. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  897. */
  898. static inline int check_intra4x4_pred_mode(H264Context *h){
  899. MpegEncContext * const s = &h->s;
  900. static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
  901. static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
  902. int i;
  903. if(!(h->top_samples_available&0x8000)){
  904. for(i=0; i<4; i++){
  905. int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
  906. if(status<0){
  907. 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);
  908. return -1;
  909. } else if(status){
  910. h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
  911. }
  912. }
  913. }
  914. if(!(h->left_samples_available&0x8000)){
  915. for(i=0; i<4; i++){
  916. int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
  917. if(status<0){
  918. 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);
  919. return -1;
  920. } else if(status){
  921. h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
  922. }
  923. }
  924. }
  925. return 0;
  926. } //FIXME cleanup like next
  927. /**
  928. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  929. */
  930. static inline int check_intra_pred_mode(H264Context *h, int mode){
  931. MpegEncContext * const s = &h->s;
  932. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  933. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  934. if(mode < 0 || mode > 6) {
  935. 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);
  936. return -1;
  937. }
  938. if(!(h->top_samples_available&0x8000)){
  939. mode= top[ mode ];
  940. if(mode<0){
  941. 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);
  942. return -1;
  943. }
  944. }
  945. if(!(h->left_samples_available&0x8000)){
  946. mode= left[ mode ];
  947. if(mode<0){
  948. 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);
  949. return -1;
  950. }
  951. }
  952. return mode;
  953. }
  954. /**
  955. * gets the predicted intra4x4 prediction mode.
  956. */
  957. static inline int pred_intra_mode(H264Context *h, int n){
  958. const int index8= scan8[n];
  959. const int left= h->intra4x4_pred_mode_cache[index8 - 1];
  960. const int top = h->intra4x4_pred_mode_cache[index8 - 8];
  961. const int min= FFMIN(left, top);
  962. tprintf("mode:%d %d min:%d\n", left ,top, min);
  963. if(min<0) return DC_PRED;
  964. else return min;
  965. }
  966. static inline void write_back_non_zero_count(H264Context *h){
  967. MpegEncContext * const s = &h->s;
  968. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  969. h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
  970. h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
  971. h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
  972. h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
  973. h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
  974. h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
  975. h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
  976. h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
  977. h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
  978. h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
  979. h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
  980. h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
  981. h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
  982. if(FRAME_MBAFF){
  983. // store all luma nnzs, for deblocking
  984. int v = 0, i;
  985. for(i=0; i<16; i++)
  986. v += (!!h->non_zero_count_cache[scan8[i]]) << i;
  987. *(uint16_t*)&h->non_zero_count[mb_xy][14] = v;
  988. }
  989. }
  990. /**
  991. * gets the predicted number of non zero coefficients.
  992. * @param n block index
  993. */
  994. static inline int pred_non_zero_count(H264Context *h, int n){
  995. const int index8= scan8[n];
  996. const int left= h->non_zero_count_cache[index8 - 1];
  997. const int top = h->non_zero_count_cache[index8 - 8];
  998. int i= left + top;
  999. if(i<64) i= (i+1)>>1;
  1000. tprintf("pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
  1001. return i&31;
  1002. }
  1003. static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
  1004. const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
  1005. /* there is no consistent mapping of mvs to neighboring locations that will
  1006. * make mbaff happy, so we can't move all this logic to fill_caches */
  1007. if(FRAME_MBAFF){
  1008. MpegEncContext *s = &h->s;
  1009. const int *mb_types = s->current_picture_ptr->mb_type;
  1010. const int16_t *mv;
  1011. *(uint32_t*)h->mv_cache[list][scan8[0]-2] = 0;
  1012. *C = h->mv_cache[list][scan8[0]-2];
  1013. if(!MB_FIELD
  1014. && (s->mb_y&1) && i < scan8[0]+8 && topright_ref != PART_NOT_AVAILABLE){
  1015. int topright_xy = s->mb_x + (s->mb_y-1)*s->mb_stride + (i == scan8[0]+3);
  1016. if(IS_INTERLACED(mb_types[topright_xy])){
  1017. #define SET_DIAG_MV(MV_OP, REF_OP, X4, Y4)\
  1018. const int x4 = X4, y4 = Y4;\
  1019. const int mb_type = mb_types[(x4>>2)+(y4>>2)*s->mb_stride];\
  1020. if(!USES_LIST(mb_type,list) && !IS_8X8(mb_type))\
  1021. return LIST_NOT_USED;\
  1022. mv = s->current_picture_ptr->motion_val[list][x4 + y4*h->b_stride];\
  1023. h->mv_cache[list][scan8[0]-2][0] = mv[0];\
  1024. h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
  1025. return s->current_picture_ptr->ref_index[list][(x4>>1) + (y4>>1)*h->b8_stride] REF_OP;
  1026. SET_DIAG_MV(*2, >>1, s->mb_x*4+(i&7)-4+part_width, s->mb_y*4-1);
  1027. }
  1028. }
  1029. if(topright_ref == PART_NOT_AVAILABLE
  1030. && ((s->mb_y&1) || i >= scan8[0]+8) && (i&7)==4
  1031. && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
  1032. if(!MB_FIELD
  1033. && IS_INTERLACED(mb_types[h->left_mb_xy[0]])){
  1034. SET_DIAG_MV(*2, >>1, s->mb_x*4-1, (s->mb_y|1)*4+(s->mb_y&1)*2+(i>>4)-1);
  1035. }
  1036. if(MB_FIELD
  1037. && !IS_INTERLACED(mb_types[h->left_mb_xy[0]])
  1038. && i >= scan8[0]+8){
  1039. // leftshift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's ok.
  1040. SET_DIAG_MV(>>1, <<1, s->mb_x*4-1, (s->mb_y&~1)*4 - 1 + ((i-scan8[0])>>3)*2);
  1041. }
  1042. }
  1043. #undef SET_DIAG_MV
  1044. }
  1045. if(topright_ref != PART_NOT_AVAILABLE){
  1046. *C= h->mv_cache[list][ i - 8 + part_width ];
  1047. return topright_ref;
  1048. }else{
  1049. tprintf("topright MV not available\n");
  1050. *C= h->mv_cache[list][ i - 8 - 1 ];
  1051. return h->ref_cache[list][ i - 8 - 1 ];
  1052. }
  1053. }
  1054. /**
  1055. * gets the predicted MV.
  1056. * @param n the block index
  1057. * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
  1058. * @param mx the x component of the predicted motion vector
  1059. * @param my the y component of the predicted motion vector
  1060. */
  1061. static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
  1062. const int index8= scan8[n];
  1063. const int top_ref= h->ref_cache[list][ index8 - 8 ];
  1064. const int left_ref= h->ref_cache[list][ index8 - 1 ];
  1065. const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
  1066. const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
  1067. const int16_t * C;
  1068. int diagonal_ref, match_count;
  1069. assert(part_width==1 || part_width==2 || part_width==4);
  1070. /* mv_cache
  1071. B . . A T T T T
  1072. U . . L . . , .
  1073. U . . L . . . .
  1074. U . . L . . , .
  1075. . . . L . . . .
  1076. */
  1077. diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
  1078. match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
  1079. tprintf("pred_motion match_count=%d\n", match_count);
  1080. if(match_count > 1){ //most common
  1081. *mx= mid_pred(A[0], B[0], C[0]);
  1082. *my= mid_pred(A[1], B[1], C[1]);
  1083. }else if(match_count==1){
  1084. if(left_ref==ref){
  1085. *mx= A[0];
  1086. *my= A[1];
  1087. }else if(top_ref==ref){
  1088. *mx= B[0];
  1089. *my= B[1];
  1090. }else{
  1091. *mx= C[0];
  1092. *my= C[1];
  1093. }
  1094. }else{
  1095. if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
  1096. *mx= A[0];
  1097. *my= A[1];
  1098. }else{
  1099. *mx= mid_pred(A[0], B[0], C[0]);
  1100. *my= mid_pred(A[1], B[1], C[1]);
  1101. }
  1102. }
  1103. 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);
  1104. }
  1105. /**
  1106. * gets the directionally predicted 16x8 MV.
  1107. * @param n the block index
  1108. * @param mx the x component of the predicted motion vector
  1109. * @param my the y component of the predicted motion vector
  1110. */
  1111. static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  1112. if(n==0){
  1113. const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
  1114. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  1115. 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);
  1116. if(top_ref == ref){
  1117. *mx= B[0];
  1118. *my= B[1];
  1119. return;
  1120. }
  1121. }else{
  1122. const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
  1123. const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
  1124. 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);
  1125. if(left_ref == ref){
  1126. *mx= A[0];
  1127. *my= A[1];
  1128. return;
  1129. }
  1130. }
  1131. //RARE
  1132. pred_motion(h, n, 4, list, ref, mx, my);
  1133. }
  1134. /**
  1135. * gets the directionally predicted 8x16 MV.
  1136. * @param n the block index
  1137. * @param mx the x component of the predicted motion vector
  1138. * @param my the y component of the predicted motion vector
  1139. */
  1140. static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  1141. if(n==0){
  1142. const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
  1143. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  1144. 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);
  1145. if(left_ref == ref){
  1146. *mx= A[0];
  1147. *my= A[1];
  1148. return;
  1149. }
  1150. }else{
  1151. const int16_t * C;
  1152. int diagonal_ref;
  1153. diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
  1154. 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);
  1155. if(diagonal_ref == ref){
  1156. *mx= C[0];
  1157. *my= C[1];
  1158. return;
  1159. }
  1160. }
  1161. //RARE
  1162. pred_motion(h, n, 2, list, ref, mx, my);
  1163. }
  1164. static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
  1165. const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
  1166. const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
  1167. tprintf("pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
  1168. if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
  1169. || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
  1170. || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
  1171. *mx = *my = 0;
  1172. return;
  1173. }
  1174. pred_motion(h, 0, 4, 0, 0, mx, my);
  1175. return;
  1176. }
  1177. static inline void direct_dist_scale_factor(H264Context * const h){
  1178. const int poc = h->s.current_picture_ptr->poc;
  1179. const int poc1 = h->ref_list[1][0].poc;
  1180. int i;
  1181. for(i=0; i<h->ref_count[0]; i++){
  1182. int poc0 = h->ref_list[0][i].poc;
  1183. int td = clip(poc1 - poc0, -128, 127);
  1184. if(td == 0 /* FIXME || pic0 is a long-term ref */){
  1185. h->dist_scale_factor[i] = 256;
  1186. }else{
  1187. int tb = clip(poc - poc0, -128, 127);
  1188. int tx = (16384 + (ABS(td) >> 1)) / td;
  1189. h->dist_scale_factor[i] = clip((tb*tx + 32) >> 6, -1024, 1023);
  1190. }
  1191. }
  1192. if(FRAME_MBAFF){
  1193. for(i=0; i<h->ref_count[0]; i++){
  1194. h->dist_scale_factor_field[2*i] =
  1195. h->dist_scale_factor_field[2*i+1] = h->dist_scale_factor[i];
  1196. }
  1197. }
  1198. }
  1199. static inline void direct_ref_list_init(H264Context * const h){
  1200. MpegEncContext * const s = &h->s;
  1201. Picture * const ref1 = &h->ref_list[1][0];
  1202. Picture * const cur = s->current_picture_ptr;
  1203. int list, i, j;
  1204. if(cur->pict_type == I_TYPE)
  1205. cur->ref_count[0] = 0;
  1206. if(cur->pict_type != B_TYPE)
  1207. cur->ref_count[1] = 0;
  1208. for(list=0; list<2; list++){
  1209. cur->ref_count[list] = h->ref_count[list];
  1210. for(j=0; j<h->ref_count[list]; j++)
  1211. cur->ref_poc[list][j] = h->ref_list[list][j].poc;
  1212. }
  1213. if(cur->pict_type != B_TYPE || h->direct_spatial_mv_pred)
  1214. return;
  1215. for(list=0; list<2; list++){
  1216. for(i=0; i<ref1->ref_count[list]; i++){
  1217. const int poc = ref1->ref_poc[list][i];
  1218. h->map_col_to_list0[list][i] = 0; /* bogus; fills in for missing frames */
  1219. for(j=0; j<h->ref_count[list]; j++)
  1220. if(h->ref_list[list][j].poc == poc){
  1221. h->map_col_to_list0[list][i] = j;
  1222. break;
  1223. }
  1224. }
  1225. }
  1226. if(FRAME_MBAFF){
  1227. for(list=0; list<2; list++){
  1228. for(i=0; i<ref1->ref_count[list]; i++){
  1229. j = h->map_col_to_list0[list][i];
  1230. h->map_col_to_list0_field[list][2*i] = 2*j;
  1231. h->map_col_to_list0_field[list][2*i+1] = 2*j+1;
  1232. }
  1233. }
  1234. }
  1235. }
  1236. static inline void pred_direct_motion(H264Context * const h, int *mb_type){
  1237. MpegEncContext * const s = &h->s;
  1238. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  1239. const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1240. const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1241. const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
  1242. const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
  1243. const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[1][b4_xy];
  1244. const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
  1245. const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy];
  1246. const int is_b8x8 = IS_8X8(*mb_type);
  1247. int sub_mb_type;
  1248. int i8, i4;
  1249. #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
  1250. if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
  1251. /* FIXME save sub mb types from previous frames (or derive from MVs)
  1252. * so we know exactly what block size to use */
  1253. sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  1254. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  1255. }else if(!is_b8x8 && (mb_type_col & MB_TYPE_16x16_OR_INTRA)){
  1256. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  1257. *mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
  1258. }else{
  1259. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  1260. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  1261. }
  1262. if(!is_b8x8)
  1263. *mb_type |= MB_TYPE_DIRECT2;
  1264. if(MB_FIELD)
  1265. *mb_type |= MB_TYPE_INTERLACED;
  1266. 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);
  1267. if(h->direct_spatial_mv_pred){
  1268. int ref[2];
  1269. int mv[2][2];
  1270. int list;
  1271. /* FIXME interlacing + spatial direct uses wrong colocated block positions */
  1272. /* ref = min(neighbors) */
  1273. for(list=0; list<2; list++){
  1274. int refa = h->ref_cache[list][scan8[0] - 1];
  1275. int refb = h->ref_cache[list][scan8[0] - 8];
  1276. int refc = h->ref_cache[list][scan8[0] - 8 + 4];
  1277. if(refc == -2)
  1278. refc = h->ref_cache[list][scan8[0] - 8 - 1];
  1279. ref[list] = refa;
  1280. if(ref[list] < 0 || (refb < ref[list] && refb >= 0))
  1281. ref[list] = refb;
  1282. if(ref[list] < 0 || (refc < ref[list] && refc >= 0))
  1283. ref[list] = refc;
  1284. if(ref[list] < 0)
  1285. ref[list] = -1;
  1286. }
  1287. if(ref[0] < 0 && ref[1] < 0){
  1288. ref[0] = ref[1] = 0;
  1289. mv[0][0] = mv[0][1] =
  1290. mv[1][0] = mv[1][1] = 0;
  1291. }else{
  1292. for(list=0; list<2; list++){
  1293. if(ref[list] >= 0)
  1294. pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
  1295. else
  1296. mv[list][0] = mv[list][1] = 0;
  1297. }
  1298. }
  1299. if(ref[1] < 0){
  1300. *mb_type &= ~MB_TYPE_P0L1;
  1301. sub_mb_type &= ~MB_TYPE_P0L1;
  1302. }else if(ref[0] < 0){
  1303. *mb_type &= ~MB_TYPE_P0L0;
  1304. sub_mb_type &= ~MB_TYPE_P0L0;
  1305. }
  1306. if(IS_16X16(*mb_type)){
  1307. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  1308. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  1309. if(!IS_INTRA(mb_type_col)
  1310. && ( (l1ref0[0] == 0 && ABS(l1mv0[0][0]) <= 1 && ABS(l1mv0[0][1]) <= 1)
  1311. || (l1ref0[0] < 0 && l1ref1[0] == 0 && ABS(l1mv1[0][0]) <= 1 && ABS(l1mv1[0][1]) <= 1
  1312. && (h->x264_build>33 || !h->x264_build)))){
  1313. if(ref[0] > 0)
  1314. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1315. else
  1316. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  1317. if(ref[1] > 0)
  1318. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1319. else
  1320. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  1321. }else{
  1322. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1323. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1324. }
  1325. }else{
  1326. for(i8=0; i8<4; i8++){
  1327. const int x8 = i8&1;
  1328. const int y8 = i8>>1;
  1329. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1330. continue;
  1331. h->sub_mb_type[i8] = sub_mb_type;
  1332. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1333. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1334. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  1335. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  1336. /* col_zero_flag */
  1337. if(!IS_INTRA(mb_type_col) && ( l1ref0[x8 + y8*h->b8_stride] == 0
  1338. || (l1ref0[x8 + y8*h->b8_stride] < 0 && l1ref1[x8 + y8*h->b8_stride] == 0
  1339. && (h->x264_build>33 || !h->x264_build)))){
  1340. const int16_t (*l1mv)[2]= l1ref0[x8 + y8*h->b8_stride] == 0 ? l1mv0 : l1mv1;
  1341. if(IS_SUB_8X8(sub_mb_type)){
  1342. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1343. if(ABS(mv_col[0]) <= 1 && ABS(mv_col[1]) <= 1){
  1344. if(ref[0] == 0)
  1345. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1346. if(ref[1] == 0)
  1347. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1348. }
  1349. }else
  1350. for(i4=0; i4<4; i4++){
  1351. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1352. if(ABS(mv_col[0]) <= 1 && ABS(mv_col[1]) <= 1){
  1353. if(ref[0] == 0)
  1354. *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
  1355. if(ref[1] == 0)
  1356. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
  1357. }
  1358. }
  1359. }
  1360. }
  1361. }
  1362. }else{ /* direct temporal mv pred */
  1363. const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
  1364. const int *dist_scale_factor = h->dist_scale_factor;
  1365. if(FRAME_MBAFF){
  1366. if(IS_INTERLACED(*mb_type)){
  1367. map_col_to_list0[0] = h->map_col_to_list0_field[0];
  1368. map_col_to_list0[1] = h->map_col_to_list0_field[1];
  1369. dist_scale_factor = h->dist_scale_factor_field;
  1370. }
  1371. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col)){
  1372. /* FIXME assumes direct_8x8_inference == 1 */
  1373. const int pair_xy = s->mb_x + (s->mb_y&~1)*s->mb_stride;
  1374. int mb_types_col[2];
  1375. int y_shift;
  1376. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1
  1377. | (is_b8x8 ? 0 : MB_TYPE_DIRECT2)
  1378. | (*mb_type & MB_TYPE_INTERLACED);
  1379. sub_mb_type = MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_16x16;
  1380. if(IS_INTERLACED(*mb_type)){
  1381. /* frame to field scaling */
  1382. mb_types_col[0] = h->ref_list[1][0].mb_type[pair_xy];
  1383. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1384. if(s->mb_y&1){
  1385. l1ref0 -= 2*h->b8_stride;
  1386. l1ref1 -= 2*h->b8_stride;
  1387. l1mv0 -= 4*h->b_stride;
  1388. l1mv1 -= 4*h->b_stride;
  1389. }
  1390. y_shift = 0;
  1391. if( (mb_types_col[0] & MB_TYPE_16x16_OR_INTRA)
  1392. && (mb_types_col[1] & MB_TYPE_16x16_OR_INTRA)
  1393. && !is_b8x8)
  1394. *mb_type |= MB_TYPE_16x8;
  1395. else
  1396. *mb_type |= MB_TYPE_8x8;
  1397. }else{
  1398. /* field to frame scaling */
  1399. /* col_mb_y = (mb_y&~1) + (topAbsDiffPOC < bottomAbsDiffPOC ? 0 : 1)
  1400. * but in MBAFF, top and bottom POC are equal */
  1401. int dy = (s->mb_y&1) ? 1 : 2;
  1402. mb_types_col[0] =
  1403. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1404. l1ref0 += dy*h->b8_stride;
  1405. l1ref1 += dy*h->b8_stride;
  1406. l1mv0 += 2*dy*h->b_stride;
  1407. l1mv1 += 2*dy*h->b_stride;
  1408. y_shift = 2;
  1409. if((mb_types_col[0] & (MB_TYPE_16x16_OR_INTRA|MB_TYPE_16x8))
  1410. && !is_b8x8)
  1411. *mb_type |= MB_TYPE_16x16;
  1412. else
  1413. *mb_type |= MB_TYPE_8x8;
  1414. }
  1415. for(i8=0; i8<4; i8++){
  1416. const int x8 = i8&1;
  1417. const int y8 = i8>>1;
  1418. int ref0, scale;
  1419. const int16_t (*l1mv)[2]= l1mv0;
  1420. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1421. continue;
  1422. h->sub_mb_type[i8] = sub_mb_type;
  1423. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1424. if(IS_INTRA(mb_types_col[y8])){
  1425. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1426. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1427. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1428. continue;
  1429. }
  1430. ref0 = l1ref0[x8 + (y8*2>>y_shift)*h->b8_stride];
  1431. if(ref0 >= 0)
  1432. ref0 = map_col_to_list0[0][ref0*2>>y_shift];
  1433. else{
  1434. ref0 = map_col_to_list0[1][l1ref1[x8 + (y8*2>>y_shift)*h->b8_stride]*2>>y_shift];
  1435. l1mv= l1mv1;
  1436. }
  1437. scale = dist_scale_factor[ref0];
  1438. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1439. {
  1440. const int16_t *mv_col = l1mv[x8*3 + (y8*6>>y_shift)*h->b_stride];
  1441. int my_col = (mv_col[1]<<y_shift)/2;
  1442. int mx = (scale * mv_col[0] + 128) >> 8;
  1443. int my = (scale * my_col + 128) >> 8;
  1444. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1445. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
  1446. }
  1447. }
  1448. return;
  1449. }
  1450. }
  1451. /* one-to-one mv scaling */
  1452. if(IS_16X16(*mb_type)){
  1453. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  1454. if(IS_INTRA(mb_type_col)){
  1455. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  1456. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  1457. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  1458. }else{
  1459. const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0]]
  1460. : map_col_to_list0[1][l1ref1[0]];
  1461. const int scale = dist_scale_factor[ref0];
  1462. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  1463. int mv_l0[2];
  1464. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1465. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1466. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref0, 1);
  1467. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0],mv_l0[1]), 4);
  1468. 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);
  1469. }
  1470. }else{
  1471. for(i8=0; i8<4; i8++){
  1472. const int x8 = i8&1;
  1473. const int y8 = i8>>1;
  1474. int ref0, scale;
  1475. const int16_t (*l1mv)[2]= l1mv0;
  1476. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1477. continue;
  1478. h->sub_mb_type[i8] = sub_mb_type;
  1479. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1480. if(IS_INTRA(mb_type_col)){
  1481. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1482. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1483. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1484. continue;
  1485. }
  1486. ref0 = l1ref0[x8 + y8*h->b8_stride];
  1487. if(ref0 >= 0)
  1488. ref0 = map_col_to_list0[0][ref0];
  1489. else{
  1490. ref0 = map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]];
  1491. l1mv= l1mv1;
  1492. }
  1493. scale = dist_scale_factor[ref0];
  1494. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1495. if(IS_SUB_8X8(sub_mb_type)){
  1496. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1497. int mx = (scale * mv_col[0] + 128) >> 8;
  1498. int my = (scale * mv_col[1] + 128) >> 8;
  1499. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1500. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
  1501. }else
  1502. for(i4=0; i4<4; i4++){
  1503. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1504. int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
  1505. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1506. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1507. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
  1508. pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  1509. }
  1510. }
  1511. }
  1512. }
  1513. }
  1514. static inline void write_back_motion(H264Context *h, int mb_type){
  1515. MpegEncContext * const s = &h->s;
  1516. const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1517. const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1518. int list;
  1519. if(!USES_LIST(mb_type, 0))
  1520. fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, h->b8_stride, (uint8_t)LIST_NOT_USED, 1);
  1521. for(list=0; list<2; list++){
  1522. int y;
  1523. if(!USES_LIST(mb_type, list))
  1524. continue;
  1525. for(y=0; y<4; y++){
  1526. *(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];
  1527. *(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];
  1528. }
  1529. if( h->pps.cabac ) {
  1530. for(y=0; y<4; y++){
  1531. *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
  1532. *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
  1533. }
  1534. }
  1535. {
  1536. uint8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
  1537. ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]];
  1538. ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]];
  1539. ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]];
  1540. ref_index[1+1*h->b8_stride]= h->ref_cache[list][scan8[12]];
  1541. }
  1542. }
  1543. if(h->slice_type == B_TYPE && h->pps.cabac){
  1544. if(IS_8X8(mb_type)){
  1545. uint8_t *direct_table = &h->direct_table[b8_xy];
  1546. direct_table[1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
  1547. direct_table[0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
  1548. direct_table[1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
  1549. }
  1550. }
  1551. }
  1552. /**
  1553. * Decodes a network abstraction layer unit.
  1554. * @param consumed is the number of bytes used as input
  1555. * @param length is the length of the array
  1556. * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp tailing?
  1557. * @returns decoded bytes, might be src+1 if no escapes
  1558. */
  1559. static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){
  1560. int i, si, di;
  1561. uint8_t *dst;
  1562. // src[0]&0x80; //forbidden bit
  1563. h->nal_ref_idc= src[0]>>5;
  1564. h->nal_unit_type= src[0]&0x1F;
  1565. src++; length--;
  1566. #if 0
  1567. for(i=0; i<length; i++)
  1568. printf("%2X ", src[i]);
  1569. #endif
  1570. for(i=0; i+1<length; i+=2){
  1571. if(src[i]) continue;
  1572. if(i>0 && src[i-1]==0) i--;
  1573. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  1574. if(src[i+2]!=3){
  1575. /* startcode, so we must be past the end */
  1576. length=i;
  1577. }
  1578. break;
  1579. }
  1580. }
  1581. if(i>=length-1){ //no escaped 0
  1582. *dst_length= length;
  1583. *consumed= length+1; //+1 for the header
  1584. return src;
  1585. }
  1586. h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length);
  1587. dst= h->rbsp_buffer;
  1588. //printf("decoding esc\n");
  1589. si=di=0;
  1590. while(si<length){
  1591. //remove escapes (very rare 1:2^22)
  1592. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  1593. if(src[si+2]==3){ //escape
  1594. dst[di++]= 0;
  1595. dst[di++]= 0;
  1596. si+=3;
  1597. continue;
  1598. }else //next start code
  1599. break;
  1600. }
  1601. dst[di++]= src[si++];
  1602. }
  1603. *dst_length= di;
  1604. *consumed= si + 1;//+1 for the header
  1605. //FIXME store exact number of bits in the getbitcontext (its needed for decoding)
  1606. return dst;
  1607. }
  1608. #if 0
  1609. /**
  1610. * @param src the data which should be escaped
  1611. * @param dst the target buffer, dst+1 == src is allowed as a special case
  1612. * @param length the length of the src data
  1613. * @param dst_length the length of the dst array
  1614. * @returns length of escaped data in bytes or -1 if an error occured
  1615. */
  1616. static int encode_nal(H264Context *h, uint8_t *dst, uint8_t *src, int length, int dst_length){
  1617. int i, escape_count, si, di;
  1618. uint8_t *temp;
  1619. assert(length>=0);
  1620. assert(dst_length>0);
  1621. dst[0]= (h->nal_ref_idc<<5) + h->nal_unit_type;
  1622. if(length==0) return 1;
  1623. escape_count= 0;
  1624. for(i=0; i<length; i+=2){
  1625. if(src[i]) continue;
  1626. if(i>0 && src[i-1]==0)
  1627. i--;
  1628. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  1629. escape_count++;
  1630. i+=2;
  1631. }
  1632. }
  1633. if(escape_count==0){
  1634. if(dst+1 != src)
  1635. memcpy(dst+1, src, length);
  1636. return length + 1;
  1637. }
  1638. if(length + escape_count + 1> dst_length)
  1639. return -1;
  1640. //this should be damn rare (hopefully)
  1641. h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length + escape_count);
  1642. temp= h->rbsp_buffer;
  1643. //printf("encoding esc\n");
  1644. si= 0;
  1645. di= 0;
  1646. while(si < length){
  1647. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  1648. temp[di++]= 0; si++;
  1649. temp[di++]= 0; si++;
  1650. temp[di++]= 3;
  1651. temp[di++]= src[si++];
  1652. }
  1653. else
  1654. temp[di++]= src[si++];
  1655. }
  1656. memcpy(dst+1, temp, length+escape_count);
  1657. assert(di == length+escape_count);
  1658. return di + 1;
  1659. }
  1660. /**
  1661. * write 1,10,100,1000,... for alignment, yes its exactly inverse to mpeg4
  1662. */
  1663. static void encode_rbsp_trailing(PutBitContext *pb){
  1664. int length;
  1665. put_bits(pb, 1, 1);
  1666. length= (-put_bits_count(pb))&7;
  1667. if(length) put_bits(pb, length, 0);
  1668. }
  1669. #endif
  1670. /**
  1671. * identifies the exact end of the bitstream
  1672. * @return the length of the trailing, or 0 if damaged
  1673. */
  1674. static int decode_rbsp_trailing(uint8_t *src){
  1675. int v= *src;
  1676. int r;
  1677. tprintf("rbsp trailing %X\n", v);
  1678. for(r=1; r<9; r++){
  1679. if(v&1) return r;
  1680. v>>=1;
  1681. }
  1682. return 0;
  1683. }
  1684. /**
  1685. * idct tranforms the 16 dc values and dequantize them.
  1686. * @param qp quantization parameter
  1687. */
  1688. static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1689. #define stride 16
  1690. int i;
  1691. int temp[16]; //FIXME check if this is a good idea
  1692. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1693. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1694. //memset(block, 64, 2*256);
  1695. //return;
  1696. for(i=0; i<4; i++){
  1697. const int offset= y_offset[i];
  1698. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1699. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1700. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1701. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1702. temp[4*i+0]= z0+z3;
  1703. temp[4*i+1]= z1+z2;
  1704. temp[4*i+2]= z1-z2;
  1705. temp[4*i+3]= z0-z3;
  1706. }
  1707. for(i=0; i<4; i++){
  1708. const int offset= x_offset[i];
  1709. const int z0= temp[4*0+i] + temp[4*2+i];
  1710. const int z1= temp[4*0+i] - temp[4*2+i];
  1711. const int z2= temp[4*1+i] - temp[4*3+i];
  1712. const int z3= temp[4*1+i] + temp[4*3+i];
  1713. block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_resdual
  1714. block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
  1715. block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
  1716. block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
  1717. }
  1718. }
  1719. #if 0
  1720. /**
  1721. * dct tranforms the 16 dc values.
  1722. * @param qp quantization parameter ??? FIXME
  1723. */
  1724. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  1725. // const int qmul= dequant_coeff[qp][0];
  1726. int i;
  1727. int temp[16]; //FIXME check if this is a good idea
  1728. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1729. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1730. for(i=0; i<4; i++){
  1731. const int offset= y_offset[i];
  1732. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1733. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1734. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1735. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1736. temp[4*i+0]= z0+z3;
  1737. temp[4*i+1]= z1+z2;
  1738. temp[4*i+2]= z1-z2;
  1739. temp[4*i+3]= z0-z3;
  1740. }
  1741. for(i=0; i<4; i++){
  1742. const int offset= x_offset[i];
  1743. const int z0= temp[4*0+i] + temp[4*2+i];
  1744. const int z1= temp[4*0+i] - temp[4*2+i];
  1745. const int z2= temp[4*1+i] - temp[4*3+i];
  1746. const int z3= temp[4*1+i] + temp[4*3+i];
  1747. block[stride*0 +offset]= (z0 + z3)>>1;
  1748. block[stride*2 +offset]= (z1 + z2)>>1;
  1749. block[stride*8 +offset]= (z1 - z2)>>1;
  1750. block[stride*10+offset]= (z0 - z3)>>1;
  1751. }
  1752. }
  1753. #endif
  1754. #undef xStride
  1755. #undef stride
  1756. static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1757. const int stride= 16*2;
  1758. const int xStride= 16;
  1759. int a,b,c,d,e;
  1760. a= block[stride*0 + xStride*0];
  1761. b= block[stride*0 + xStride*1];
  1762. c= block[stride*1 + xStride*0];
  1763. d= block[stride*1 + xStride*1];
  1764. e= a-b;
  1765. a= a+b;
  1766. b= c-d;
  1767. c= c+d;
  1768. block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
  1769. block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
  1770. block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
  1771. block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
  1772. }
  1773. #if 0
  1774. static void chroma_dc_dct_c(DCTELEM *block){
  1775. const int stride= 16*2;
  1776. const int xStride= 16;
  1777. int a,b,c,d,e;
  1778. a= block[stride*0 + xStride*0];
  1779. b= block[stride*0 + xStride*1];
  1780. c= block[stride*1 + xStride*0];
  1781. d= block[stride*1 + xStride*1];
  1782. e= a-b;
  1783. a= a+b;
  1784. b= c-d;
  1785. c= c+d;
  1786. block[stride*0 + xStride*0]= (a+c);
  1787. block[stride*0 + xStride*1]= (e+b);
  1788. block[stride*1 + xStride*0]= (a-c);
  1789. block[stride*1 + xStride*1]= (e-b);
  1790. }
  1791. #endif
  1792. /**
  1793. * gets the chroma qp.
  1794. */
  1795. static inline int get_chroma_qp(int chroma_qp_index_offset, int qscale){
  1796. return chroma_qp[clip(qscale + chroma_qp_index_offset, 0, 51)];
  1797. }
  1798. #if 0
  1799. static void h264_diff_dct_c(DCTELEM *block, uint8_t *src1, uint8_t *src2, int stride){
  1800. int i;
  1801. //FIXME try int temp instead of block
  1802. for(i=0; i<4; i++){
  1803. const int d0= src1[0 + i*stride] - src2[0 + i*stride];
  1804. const int d1= src1[1 + i*stride] - src2[1 + i*stride];
  1805. const int d2= src1[2 + i*stride] - src2[2 + i*stride];
  1806. const int d3= src1[3 + i*stride] - src2[3 + i*stride];
  1807. const int z0= d0 + d3;
  1808. const int z3= d0 - d3;
  1809. const int z1= d1 + d2;
  1810. const int z2= d1 - d2;
  1811. block[0 + 4*i]= z0 + z1;
  1812. block[1 + 4*i]= 2*z3 + z2;
  1813. block[2 + 4*i]= z0 - z1;
  1814. block[3 + 4*i]= z3 - 2*z2;
  1815. }
  1816. for(i=0; i<4; i++){
  1817. const int z0= block[0*4 + i] + block[3*4 + i];
  1818. const int z3= block[0*4 + i] - block[3*4 + i];
  1819. const int z1= block[1*4 + i] + block[2*4 + i];
  1820. const int z2= block[1*4 + i] - block[2*4 + i];
  1821. block[0*4 + i]= z0 + z1;
  1822. block[1*4 + i]= 2*z3 + z2;
  1823. block[2*4 + i]= z0 - z1;
  1824. block[3*4 + i]= z3 - 2*z2;
  1825. }
  1826. }
  1827. #endif
  1828. //FIXME need to check that this doesnt overflow signed 32 bit for low qp, i am not sure, it's very close
  1829. //FIXME check that gcc inlines this (and optimizes intra & seperate_dc stuff away)
  1830. static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int seperate_dc){
  1831. int i;
  1832. const int * const quant_table= quant_coeff[qscale];
  1833. const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
  1834. const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
  1835. const unsigned int threshold2= (threshold1<<1);
  1836. int last_non_zero;
  1837. if(seperate_dc){
  1838. if(qscale<=18){
  1839. //avoid overflows
  1840. const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
  1841. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
  1842. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1843. int level= block[0]*quant_coeff[qscale+18][0];
  1844. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1845. if(level>0){
  1846. level= (dc_bias + level)>>(QUANT_SHIFT-2);
  1847. block[0]= level;
  1848. }else{
  1849. level= (dc_bias - level)>>(QUANT_SHIFT-2);
  1850. block[0]= -level;
  1851. }
  1852. // last_non_zero = i;
  1853. }else{
  1854. block[0]=0;
  1855. }
  1856. }else{
  1857. const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
  1858. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
  1859. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1860. int level= block[0]*quant_table[0];
  1861. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1862. if(level>0){
  1863. level= (dc_bias + level)>>(QUANT_SHIFT+1);
  1864. block[0]= level;
  1865. }else{
  1866. level= (dc_bias - level)>>(QUANT_SHIFT+1);
  1867. block[0]= -level;
  1868. }
  1869. // last_non_zero = i;
  1870. }else{
  1871. block[0]=0;
  1872. }
  1873. }
  1874. last_non_zero= 0;
  1875. i=1;
  1876. }else{
  1877. last_non_zero= -1;
  1878. i=0;
  1879. }
  1880. for(; i<16; i++){
  1881. const int j= scantable[i];
  1882. int level= block[j]*quant_table[j];
  1883. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  1884. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  1885. if(((unsigned)(level+threshold1))>threshold2){
  1886. if(level>0){
  1887. level= (bias + level)>>QUANT_SHIFT;
  1888. block[j]= level;
  1889. }else{
  1890. level= (bias - level)>>QUANT_SHIFT;
  1891. block[j]= -level;
  1892. }
  1893. last_non_zero = i;
  1894. }else{
  1895. block[j]=0;
  1896. }
  1897. }
  1898. return last_non_zero;
  1899. }
  1900. static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){
  1901. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1902. ((uint32_t*)(src+0*stride))[0]= a;
  1903. ((uint32_t*)(src+1*stride))[0]= a;
  1904. ((uint32_t*)(src+2*stride))[0]= a;
  1905. ((uint32_t*)(src+3*stride))[0]= a;
  1906. }
  1907. static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){
  1908. ((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101;
  1909. ((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101;
  1910. ((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101;
  1911. ((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101;
  1912. }
  1913. static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1914. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
  1915. + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
  1916. ((uint32_t*)(src+0*stride))[0]=
  1917. ((uint32_t*)(src+1*stride))[0]=
  1918. ((uint32_t*)(src+2*stride))[0]=
  1919. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1920. }
  1921. static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1922. const int dc= ( src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
  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_top_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1929. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[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_128_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1936. ((uint32_t*)(src+0*stride))[0]=
  1937. ((uint32_t*)(src+1*stride))[0]=
  1938. ((uint32_t*)(src+2*stride))[0]=
  1939. ((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U;
  1940. }
  1941. #define LOAD_TOP_RIGHT_EDGE\
  1942. const int t4= topright[0];\
  1943. const int t5= topright[1];\
  1944. const int t6= topright[2];\
  1945. const int t7= topright[3];\
  1946. #define LOAD_LEFT_EDGE\
  1947. const int l0= src[-1+0*stride];\
  1948. const int l1= src[-1+1*stride];\
  1949. const int l2= src[-1+2*stride];\
  1950. const int l3= src[-1+3*stride];\
  1951. #define LOAD_TOP_EDGE\
  1952. const int t0= src[ 0-1*stride];\
  1953. const int t1= src[ 1-1*stride];\
  1954. const int t2= src[ 2-1*stride];\
  1955. const int t3= src[ 3-1*stride];\
  1956. static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){
  1957. const int lt= src[-1-1*stride];
  1958. LOAD_TOP_EDGE
  1959. LOAD_LEFT_EDGE
  1960. src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
  1961. src[0+2*stride]=
  1962. src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
  1963. src[0+1*stride]=
  1964. src[1+2*stride]=
  1965. src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
  1966. src[0+0*stride]=
  1967. src[1+1*stride]=
  1968. src[2+2*stride]=
  1969. src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1970. src[1+0*stride]=
  1971. src[2+1*stride]=
  1972. src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1973. src[2+0*stride]=
  1974. src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1975. src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1976. }
  1977. static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){
  1978. LOAD_TOP_EDGE
  1979. LOAD_TOP_RIGHT_EDGE
  1980. // LOAD_LEFT_EDGE
  1981. src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
  1982. src[1+0*stride]=
  1983. src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
  1984. src[2+0*stride]=
  1985. src[1+1*stride]=
  1986. src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
  1987. src[3+0*stride]=
  1988. src[2+1*stride]=
  1989. src[1+2*stride]=
  1990. src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
  1991. src[3+1*stride]=
  1992. src[2+2*stride]=
  1993. src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
  1994. src[3+2*stride]=
  1995. src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
  1996. src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
  1997. }
  1998. static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
  1999. const int lt= src[-1-1*stride];
  2000. LOAD_TOP_EDGE
  2001. LOAD_LEFT_EDGE
  2002. const __attribute__((unused)) int unu= l3;
  2003. src[0+0*stride]=
  2004. src[1+2*stride]=(lt + t0 + 1)>>1;
  2005. src[1+0*stride]=
  2006. src[2+2*stride]=(t0 + t1 + 1)>>1;
  2007. src[2+0*stride]=
  2008. src[3+2*stride]=(t1 + t2 + 1)>>1;
  2009. src[3+0*stride]=(t2 + t3 + 1)>>1;
  2010. src[0+1*stride]=
  2011. src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  2012. src[1+1*stride]=
  2013. src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
  2014. src[2+1*stride]=
  2015. src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  2016. src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  2017. src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  2018. src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  2019. }
  2020. static void pred4x4_vertical_left_c(uint8_t *src, uint8_t *topright, int stride){
  2021. LOAD_TOP_EDGE
  2022. LOAD_TOP_RIGHT_EDGE
  2023. const __attribute__((unused)) int unu= t7;
  2024. src[0+0*stride]=(t0 + t1 + 1)>>1;
  2025. src[1+0*stride]=
  2026. src[0+2*stride]=(t1 + t2 + 1)>>1;
  2027. src[2+0*stride]=
  2028. src[1+2*stride]=(t2 + t3 + 1)>>1;
  2029. src[3+0*stride]=
  2030. src[2+2*stride]=(t3 + t4+ 1)>>1;
  2031. src[3+2*stride]=(t4 + t5+ 1)>>1;
  2032. src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  2033. src[1+1*stride]=
  2034. src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  2035. src[2+1*stride]=
  2036. src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
  2037. src[3+1*stride]=
  2038. src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
  2039. src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
  2040. }
  2041. static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){
  2042. LOAD_LEFT_EDGE
  2043. src[0+0*stride]=(l0 + l1 + 1)>>1;
  2044. src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  2045. src[2+0*stride]=
  2046. src[0+1*stride]=(l1 + l2 + 1)>>1;
  2047. src[3+0*stride]=
  2048. src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  2049. src[2+1*stride]=
  2050. src[0+2*stride]=(l2 + l3 + 1)>>1;
  2051. src[3+1*stride]=
  2052. src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
  2053. src[3+2*stride]=
  2054. src[1+3*stride]=
  2055. src[0+3*stride]=
  2056. src[2+2*stride]=
  2057. src[2+3*stride]=
  2058. src[3+3*stride]=l3;
  2059. }
  2060. static void pred4x4_horizontal_down_c(uint8_t *src, uint8_t *topright, int stride){
  2061. const int lt= src[-1-1*stride];
  2062. LOAD_TOP_EDGE
  2063. LOAD_LEFT_EDGE
  2064. const __attribute__((unused)) int unu= t3;
  2065. src[0+0*stride]=
  2066. src[2+1*stride]=(lt + l0 + 1)>>1;
  2067. src[1+0*stride]=
  2068. src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
  2069. src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
  2070. src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  2071. src[0+1*stride]=
  2072. src[2+2*stride]=(l0 + l1 + 1)>>1;
  2073. src[1+1*stride]=
  2074. src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  2075. src[0+2*stride]=
  2076. src[2+3*stride]=(l1 + l2+ 1)>>1;
  2077. src[1+2*stride]=
  2078. src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  2079. src[0+3*stride]=(l2 + l3 + 1)>>1;
  2080. src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  2081. }
  2082. static void pred16x16_vertical_c(uint8_t *src, int stride){
  2083. int i;
  2084. const uint32_t a= ((uint32_t*)(src-stride))[0];
  2085. const uint32_t b= ((uint32_t*)(src-stride))[1];
  2086. const uint32_t c= ((uint32_t*)(src-stride))[2];
  2087. const uint32_t d= ((uint32_t*)(src-stride))[3];
  2088. for(i=0; i<16; i++){
  2089. ((uint32_t*)(src+i*stride))[0]= a;
  2090. ((uint32_t*)(src+i*stride))[1]= b;
  2091. ((uint32_t*)(src+i*stride))[2]= c;
  2092. ((uint32_t*)(src+i*stride))[3]= d;
  2093. }
  2094. }
  2095. static void pred16x16_horizontal_c(uint8_t *src, int stride){
  2096. int i;
  2097. for(i=0; i<16; i++){
  2098. ((uint32_t*)(src+i*stride))[0]=
  2099. ((uint32_t*)(src+i*stride))[1]=
  2100. ((uint32_t*)(src+i*stride))[2]=
  2101. ((uint32_t*)(src+i*stride))[3]= src[-1+i*stride]*0x01010101;
  2102. }
  2103. }
  2104. static void pred16x16_dc_c(uint8_t *src, int stride){
  2105. int i, dc=0;
  2106. for(i=0;i<16; i++){
  2107. dc+= src[-1+i*stride];
  2108. }
  2109. for(i=0;i<16; i++){
  2110. dc+= src[i-stride];
  2111. }
  2112. dc= 0x01010101*((dc + 16)>>5);
  2113. for(i=0; i<16; i++){
  2114. ((uint32_t*)(src+i*stride))[0]=
  2115. ((uint32_t*)(src+i*stride))[1]=
  2116. ((uint32_t*)(src+i*stride))[2]=
  2117. ((uint32_t*)(src+i*stride))[3]= dc;
  2118. }
  2119. }
  2120. static void pred16x16_left_dc_c(uint8_t *src, int stride){
  2121. int i, dc=0;
  2122. for(i=0;i<16; i++){
  2123. dc+= src[-1+i*stride];
  2124. }
  2125. dc= 0x01010101*((dc + 8)>>4);
  2126. for(i=0; i<16; i++){
  2127. ((uint32_t*)(src+i*stride))[0]=
  2128. ((uint32_t*)(src+i*stride))[1]=
  2129. ((uint32_t*)(src+i*stride))[2]=
  2130. ((uint32_t*)(src+i*stride))[3]= dc;
  2131. }
  2132. }
  2133. static void pred16x16_top_dc_c(uint8_t *src, int stride){
  2134. int i, dc=0;
  2135. for(i=0;i<16; i++){
  2136. dc+= src[i-stride];
  2137. }
  2138. dc= 0x01010101*((dc + 8)>>4);
  2139. for(i=0; i<16; i++){
  2140. ((uint32_t*)(src+i*stride))[0]=
  2141. ((uint32_t*)(src+i*stride))[1]=
  2142. ((uint32_t*)(src+i*stride))[2]=
  2143. ((uint32_t*)(src+i*stride))[3]= dc;
  2144. }
  2145. }
  2146. static void pred16x16_128_dc_c(uint8_t *src, int stride){
  2147. int i;
  2148. for(i=0; i<16; i++){
  2149. ((uint32_t*)(src+i*stride))[0]=
  2150. ((uint32_t*)(src+i*stride))[1]=
  2151. ((uint32_t*)(src+i*stride))[2]=
  2152. ((uint32_t*)(src+i*stride))[3]= 0x01010101U*128U;
  2153. }
  2154. }
  2155. static inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){
  2156. int i, j, k;
  2157. int a;
  2158. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  2159. const uint8_t * const src0 = src+7-stride;
  2160. const uint8_t *src1 = src+8*stride-1;
  2161. const uint8_t *src2 = src1-2*stride; // == src+6*stride-1;
  2162. int H = src0[1] - src0[-1];
  2163. int V = src1[0] - src2[ 0];
  2164. for(k=2; k<=8; ++k) {
  2165. src1 += stride; src2 -= stride;
  2166. H += k*(src0[k] - src0[-k]);
  2167. V += k*(src1[0] - src2[ 0]);
  2168. }
  2169. if(svq3){
  2170. H = ( 5*(H/4) ) / 16;
  2171. V = ( 5*(V/4) ) / 16;
  2172. /* required for 100% accuracy */
  2173. i = H; H = V; V = i;
  2174. }else{
  2175. H = ( 5*H+32 ) >> 6;
  2176. V = ( 5*V+32 ) >> 6;
  2177. }
  2178. a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
  2179. for(j=16; j>0; --j) {
  2180. int b = a;
  2181. a += V;
  2182. for(i=-16; i<0; i+=4) {
  2183. src[16+i] = cm[ (b ) >> 5 ];
  2184. src[17+i] = cm[ (b+ H) >> 5 ];
  2185. src[18+i] = cm[ (b+2*H) >> 5 ];
  2186. src[19+i] = cm[ (b+3*H) >> 5 ];
  2187. b += 4*H;
  2188. }
  2189. src += stride;
  2190. }
  2191. }
  2192. static void pred16x16_plane_c(uint8_t *src, int stride){
  2193. pred16x16_plane_compat_c(src, stride, 0);
  2194. }
  2195. static void pred8x8_vertical_c(uint8_t *src, int stride){
  2196. int i;
  2197. const uint32_t a= ((uint32_t*)(src-stride))[0];
  2198. const uint32_t b= ((uint32_t*)(src-stride))[1];
  2199. for(i=0; i<8; i++){
  2200. ((uint32_t*)(src+i*stride))[0]= a;
  2201. ((uint32_t*)(src+i*stride))[1]= b;
  2202. }
  2203. }
  2204. static void pred8x8_horizontal_c(uint8_t *src, int stride){
  2205. int i;
  2206. for(i=0; i<8; i++){
  2207. ((uint32_t*)(src+i*stride))[0]=
  2208. ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101;
  2209. }
  2210. }
  2211. static void pred8x8_128_dc_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]= 0x01010101U*128U;
  2216. }
  2217. }
  2218. static void pred8x8_left_dc_c(uint8_t *src, int stride){
  2219. int i;
  2220. int dc0, dc2;
  2221. dc0=dc2=0;
  2222. for(i=0;i<4; i++){
  2223. dc0+= src[-1+i*stride];
  2224. dc2+= src[-1+(i+4)*stride];
  2225. }
  2226. dc0= 0x01010101*((dc0 + 2)>>2);
  2227. dc2= 0x01010101*((dc2 + 2)>>2);
  2228. for(i=0; i<4; i++){
  2229. ((uint32_t*)(src+i*stride))[0]=
  2230. ((uint32_t*)(src+i*stride))[1]= dc0;
  2231. }
  2232. for(i=4; i<8; i++){
  2233. ((uint32_t*)(src+i*stride))[0]=
  2234. ((uint32_t*)(src+i*stride))[1]= dc2;
  2235. }
  2236. }
  2237. static void pred8x8_top_dc_c(uint8_t *src, int stride){
  2238. int i;
  2239. int dc0, dc1;
  2240. dc0=dc1=0;
  2241. for(i=0;i<4; i++){
  2242. dc0+= src[i-stride];
  2243. dc1+= src[4+i-stride];
  2244. }
  2245. dc0= 0x01010101*((dc0 + 2)>>2);
  2246. dc1= 0x01010101*((dc1 + 2)>>2);
  2247. for(i=0; i<4; i++){
  2248. ((uint32_t*)(src+i*stride))[0]= dc0;
  2249. ((uint32_t*)(src+i*stride))[1]= dc1;
  2250. }
  2251. for(i=4; i<8; i++){
  2252. ((uint32_t*)(src+i*stride))[0]= dc0;
  2253. ((uint32_t*)(src+i*stride))[1]= dc1;
  2254. }
  2255. }
  2256. static void pred8x8_dc_c(uint8_t *src, int stride){
  2257. int i;
  2258. int dc0, dc1, dc2, dc3;
  2259. dc0=dc1=dc2=0;
  2260. for(i=0;i<4; i++){
  2261. dc0+= src[-1+i*stride] + src[i-stride];
  2262. dc1+= src[4+i-stride];
  2263. dc2+= src[-1+(i+4)*stride];
  2264. }
  2265. dc3= 0x01010101*((dc1 + dc2 + 4)>>3);
  2266. dc0= 0x01010101*((dc0 + 4)>>3);
  2267. dc1= 0x01010101*((dc1 + 2)>>2);
  2268. dc2= 0x01010101*((dc2 + 2)>>2);
  2269. for(i=0; i<4; i++){
  2270. ((uint32_t*)(src+i*stride))[0]= dc0;
  2271. ((uint32_t*)(src+i*stride))[1]= dc1;
  2272. }
  2273. for(i=4; i<8; i++){
  2274. ((uint32_t*)(src+i*stride))[0]= dc2;
  2275. ((uint32_t*)(src+i*stride))[1]= dc3;
  2276. }
  2277. }
  2278. static void pred8x8_plane_c(uint8_t *src, int stride){
  2279. int j, k;
  2280. int a;
  2281. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  2282. const uint8_t * const src0 = src+3-stride;
  2283. const uint8_t *src1 = src+4*stride-1;
  2284. const uint8_t *src2 = src1-2*stride; // == src+2*stride-1;
  2285. int H = src0[1] - src0[-1];
  2286. int V = src1[0] - src2[ 0];
  2287. for(k=2; k<=4; ++k) {
  2288. src1 += stride; src2 -= stride;
  2289. H += k*(src0[k] - src0[-k]);
  2290. V += k*(src1[0] - src2[ 0]);
  2291. }
  2292. H = ( 17*H+16 ) >> 5;
  2293. V = ( 17*V+16 ) >> 5;
  2294. a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
  2295. for(j=8; j>0; --j) {
  2296. int b = a;
  2297. a += V;
  2298. src[0] = cm[ (b ) >> 5 ];
  2299. src[1] = cm[ (b+ H) >> 5 ];
  2300. src[2] = cm[ (b+2*H) >> 5 ];
  2301. src[3] = cm[ (b+3*H) >> 5 ];
  2302. src[4] = cm[ (b+4*H) >> 5 ];
  2303. src[5] = cm[ (b+5*H) >> 5 ];
  2304. src[6] = cm[ (b+6*H) >> 5 ];
  2305. src[7] = cm[ (b+7*H) >> 5 ];
  2306. src += stride;
  2307. }
  2308. }
  2309. #define SRC(x,y) src[(x)+(y)*stride]
  2310. #define PL(y) \
  2311. const int l##y = (SRC(-1,y-1) + 2*SRC(-1,y) + SRC(-1,y+1) + 2) >> 2;
  2312. #define PREDICT_8x8_LOAD_LEFT \
  2313. const int l0 = ((has_topleft ? SRC(-1,-1) : SRC(-1,0)) \
  2314. + 2*SRC(-1,0) + SRC(-1,1) + 2) >> 2; \
  2315. PL(1) PL(2) PL(3) PL(4) PL(5) PL(6) \
  2316. const int l7 attribute_unused = (SRC(-1,6) + 3*SRC(-1,7) + 2) >> 2
  2317. #define PT(x) \
  2318. const int t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  2319. #define PREDICT_8x8_LOAD_TOP \
  2320. const int t0 = ((has_topleft ? SRC(-1,-1) : SRC(0,-1)) \
  2321. + 2*SRC(0,-1) + SRC(1,-1) + 2) >> 2; \
  2322. PT(1) PT(2) PT(3) PT(4) PT(5) PT(6) \
  2323. const int t7 attribute_unused = ((has_topright ? SRC(8,-1) : SRC(7,-1)) \
  2324. + 2*SRC(7,-1) + SRC(6,-1) + 2) >> 2
  2325. #define PTR(x) \
  2326. t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  2327. #define PREDICT_8x8_LOAD_TOPRIGHT \
  2328. int t8, t9, t10, t11, t12, t13, t14, t15; \
  2329. if(has_topright) { \
  2330. PTR(8) PTR(9) PTR(10) PTR(11) PTR(12) PTR(13) PTR(14) \
  2331. t15 = (SRC(14,-1) + 3*SRC(15,-1) + 2) >> 2; \
  2332. } else t8=t9=t10=t11=t12=t13=t14=t15= SRC(7,-1);
  2333. #define PREDICT_8x8_LOAD_TOPLEFT \
  2334. const int lt = (SRC(-1,0) + 2*SRC(-1,-1) + SRC(0,-1) + 2) >> 2
  2335. #define PREDICT_8x8_DC(v) \
  2336. int y; \
  2337. for( y = 0; y < 8; y++ ) { \
  2338. ((uint32_t*)src)[0] = \
  2339. ((uint32_t*)src)[1] = v; \
  2340. src += stride; \
  2341. }
  2342. static void pred8x8l_128_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2343. {
  2344. PREDICT_8x8_DC(0x80808080);
  2345. }
  2346. static void pred8x8l_left_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2347. {
  2348. PREDICT_8x8_LOAD_LEFT;
  2349. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7+4) >> 3) * 0x01010101;
  2350. PREDICT_8x8_DC(dc);
  2351. }
  2352. static void pred8x8l_top_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2353. {
  2354. PREDICT_8x8_LOAD_TOP;
  2355. const uint32_t dc = ((t0+t1+t2+t3+t4+t5+t6+t7+4) >> 3) * 0x01010101;
  2356. PREDICT_8x8_DC(dc);
  2357. }
  2358. static void pred8x8l_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2359. {
  2360. PREDICT_8x8_LOAD_LEFT;
  2361. PREDICT_8x8_LOAD_TOP;
  2362. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7
  2363. +t0+t1+t2+t3+t4+t5+t6+t7+8) >> 4) * 0x01010101;
  2364. PREDICT_8x8_DC(dc);
  2365. }
  2366. static void pred8x8l_horizontal_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2367. {
  2368. PREDICT_8x8_LOAD_LEFT;
  2369. #define ROW(y) ((uint32_t*)(src+y*stride))[0] =\
  2370. ((uint32_t*)(src+y*stride))[1] = 0x01010101 * l##y
  2371. ROW(0); ROW(1); ROW(2); ROW(3); ROW(4); ROW(5); ROW(6); ROW(7);
  2372. #undef ROW
  2373. }
  2374. static void pred8x8l_vertical_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2375. {
  2376. int y;
  2377. PREDICT_8x8_LOAD_TOP;
  2378. src[0] = t0;
  2379. src[1] = t1;
  2380. src[2] = t2;
  2381. src[3] = t3;
  2382. src[4] = t4;
  2383. src[5] = t5;
  2384. src[6] = t6;
  2385. src[7] = t7;
  2386. for( y = 1; y < 8; y++ )
  2387. *(uint64_t*)(src+y*stride) = *(uint64_t*)src;
  2388. }
  2389. static void pred8x8l_down_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2390. {
  2391. PREDICT_8x8_LOAD_TOP;
  2392. PREDICT_8x8_LOAD_TOPRIGHT;
  2393. SRC(0,0)= (t0 + 2*t1 + t2 + 2) >> 2;
  2394. SRC(0,1)=SRC(1,0)= (t1 + 2*t2 + t3 + 2) >> 2;
  2395. SRC(0,2)=SRC(1,1)=SRC(2,0)= (t2 + 2*t3 + t4 + 2) >> 2;
  2396. SRC(0,3)=SRC(1,2)=SRC(2,1)=SRC(3,0)= (t3 + 2*t4 + t5 + 2) >> 2;
  2397. SRC(0,4)=SRC(1,3)=SRC(2,2)=SRC(3,1)=SRC(4,0)= (t4 + 2*t5 + t6 + 2) >> 2;
  2398. SRC(0,5)=SRC(1,4)=SRC(2,3)=SRC(3,2)=SRC(4,1)=SRC(5,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2399. 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;
  2400. 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;
  2401. 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;
  2402. SRC(2,7)=SRC(3,6)=SRC(4,5)=SRC(5,4)=SRC(6,3)=SRC(7,2)= (t9 + 2*t10 + t11 + 2) >> 2;
  2403. SRC(3,7)=SRC(4,6)=SRC(5,5)=SRC(6,4)=SRC(7,3)= (t10 + 2*t11 + t12 + 2) >> 2;
  2404. SRC(4,7)=SRC(5,6)=SRC(6,5)=SRC(7,4)= (t11 + 2*t12 + t13 + 2) >> 2;
  2405. SRC(5,7)=SRC(6,6)=SRC(7,5)= (t12 + 2*t13 + t14 + 2) >> 2;
  2406. SRC(6,7)=SRC(7,6)= (t13 + 2*t14 + t15 + 2) >> 2;
  2407. SRC(7,7)= (t14 + 3*t15 + 2) >> 2;
  2408. }
  2409. static void pred8x8l_down_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2410. {
  2411. PREDICT_8x8_LOAD_TOP;
  2412. PREDICT_8x8_LOAD_LEFT;
  2413. PREDICT_8x8_LOAD_TOPLEFT;
  2414. SRC(0,7)= (l7 + 2*l6 + l5 + 2) >> 2;
  2415. SRC(0,6)=SRC(1,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2416. SRC(0,5)=SRC(1,6)=SRC(2,7)= (l5 + 2*l4 + l3 + 2) >> 2;
  2417. SRC(0,4)=SRC(1,5)=SRC(2,6)=SRC(3,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2418. SRC(0,3)=SRC(1,4)=SRC(2,5)=SRC(3,6)=SRC(4,7)= (l3 + 2*l2 + l1 + 2) >> 2;
  2419. SRC(0,2)=SRC(1,3)=SRC(2,4)=SRC(3,5)=SRC(4,6)=SRC(5,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2420. 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;
  2421. 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;
  2422. 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;
  2423. SRC(2,0)=SRC(3,1)=SRC(4,2)=SRC(5,3)=SRC(6,4)=SRC(7,5)= (t0 + 2*t1 + t2 + 2) >> 2;
  2424. SRC(3,0)=SRC(4,1)=SRC(5,2)=SRC(6,3)=SRC(7,4)= (t1 + 2*t2 + t3 + 2) >> 2;
  2425. SRC(4,0)=SRC(5,1)=SRC(6,2)=SRC(7,3)= (t2 + 2*t3 + t4 + 2) >> 2;
  2426. SRC(5,0)=SRC(6,1)=SRC(7,2)= (t3 + 2*t4 + t5 + 2) >> 2;
  2427. SRC(6,0)=SRC(7,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2428. SRC(7,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2429. }
  2430. static void pred8x8l_vertical_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2431. {
  2432. PREDICT_8x8_LOAD_TOP;
  2433. PREDICT_8x8_LOAD_LEFT;
  2434. PREDICT_8x8_LOAD_TOPLEFT;
  2435. SRC(0,6)= (l5 + 2*l4 + l3 + 2) >> 2;
  2436. SRC(0,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2437. SRC(0,4)=SRC(1,6)= (l3 + 2*l2 + l1 + 2) >> 2;
  2438. SRC(0,5)=SRC(1,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2439. SRC(0,2)=SRC(1,4)=SRC(2,6)= (l1 + 2*l0 + lt + 2) >> 2;
  2440. SRC(0,3)=SRC(1,5)=SRC(2,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2441. SRC(0,1)=SRC(1,3)=SRC(2,5)=SRC(3,7)= (l0 + 2*lt + t0 + 2) >> 2;
  2442. SRC(0,0)=SRC(1,2)=SRC(2,4)=SRC(3,6)= (lt + t0 + 1) >> 1;
  2443. SRC(1,1)=SRC(2,3)=SRC(3,5)=SRC(4,7)= (lt + 2*t0 + t1 + 2) >> 2;
  2444. SRC(1,0)=SRC(2,2)=SRC(3,4)=SRC(4,6)= (t0 + t1 + 1) >> 1;
  2445. SRC(2,1)=SRC(3,3)=SRC(4,5)=SRC(5,7)= (t0 + 2*t1 + t2 + 2) >> 2;
  2446. SRC(2,0)=SRC(3,2)=SRC(4,4)=SRC(5,6)= (t1 + t2 + 1) >> 1;
  2447. SRC(3,1)=SRC(4,3)=SRC(5,5)=SRC(6,7)= (t1 + 2*t2 + t3 + 2) >> 2;
  2448. SRC(3,0)=SRC(4,2)=SRC(5,4)=SRC(6,6)= (t2 + t3 + 1) >> 1;
  2449. SRC(4,1)=SRC(5,3)=SRC(6,5)=SRC(7,7)= (t2 + 2*t3 + t4 + 2) >> 2;
  2450. SRC(4,0)=SRC(5,2)=SRC(6,4)=SRC(7,6)= (t3 + t4 + 1) >> 1;
  2451. SRC(5,1)=SRC(6,3)=SRC(7,5)= (t3 + 2*t4 + t5 + 2) >> 2;
  2452. SRC(5,0)=SRC(6,2)=SRC(7,4)= (t4 + t5 + 1) >> 1;
  2453. SRC(6,1)=SRC(7,3)= (t4 + 2*t5 + t6 + 2) >> 2;
  2454. SRC(6,0)=SRC(7,2)= (t5 + t6 + 1) >> 1;
  2455. SRC(7,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2456. SRC(7,0)= (t6 + t7 + 1) >> 1;
  2457. }
  2458. static void pred8x8l_horizontal_down_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2459. {
  2460. PREDICT_8x8_LOAD_TOP;
  2461. PREDICT_8x8_LOAD_LEFT;
  2462. PREDICT_8x8_LOAD_TOPLEFT;
  2463. SRC(0,7)= (l6 + l7 + 1) >> 1;
  2464. SRC(1,7)= (l5 + 2*l6 + l7 + 2) >> 2;
  2465. SRC(0,6)=SRC(2,7)= (l5 + l6 + 1) >> 1;
  2466. SRC(1,6)=SRC(3,7)= (l4 + 2*l5 + l6 + 2) >> 2;
  2467. SRC(0,5)=SRC(2,6)=SRC(4,7)= (l4 + l5 + 1) >> 1;
  2468. SRC(1,5)=SRC(3,6)=SRC(5,7)= (l3 + 2*l4 + l5 + 2) >> 2;
  2469. SRC(0,4)=SRC(2,5)=SRC(4,6)=SRC(6,7)= (l3 + l4 + 1) >> 1;
  2470. SRC(1,4)=SRC(3,5)=SRC(5,6)=SRC(7,7)= (l2 + 2*l3 + l4 + 2) >> 2;
  2471. SRC(0,3)=SRC(2,4)=SRC(4,5)=SRC(6,6)= (l2 + l3 + 1) >> 1;
  2472. SRC(1,3)=SRC(3,4)=SRC(5,5)=SRC(7,6)= (l1 + 2*l2 + l3 + 2) >> 2;
  2473. SRC(0,2)=SRC(2,3)=SRC(4,4)=SRC(6,5)= (l1 + l2 + 1) >> 1;
  2474. SRC(1,2)=SRC(3,3)=SRC(5,4)=SRC(7,5)= (l0 + 2*l1 + l2 + 2) >> 2;
  2475. SRC(0,1)=SRC(2,2)=SRC(4,3)=SRC(6,4)= (l0 + l1 + 1) >> 1;
  2476. SRC(1,1)=SRC(3,2)=SRC(5,3)=SRC(7,4)= (lt + 2*l0 + l1 + 2) >> 2;
  2477. SRC(0,0)=SRC(2,1)=SRC(4,2)=SRC(6,3)= (lt + l0 + 1) >> 1;
  2478. SRC(1,0)=SRC(3,1)=SRC(5,2)=SRC(7,3)= (l0 + 2*lt + t0 + 2) >> 2;
  2479. SRC(2,0)=SRC(4,1)=SRC(6,2)= (t1 + 2*t0 + lt + 2) >> 2;
  2480. SRC(3,0)=SRC(5,1)=SRC(7,2)= (t2 + 2*t1 + t0 + 2) >> 2;
  2481. SRC(4,0)=SRC(6,1)= (t3 + 2*t2 + t1 + 2) >> 2;
  2482. SRC(5,0)=SRC(7,1)= (t4 + 2*t3 + t2 + 2) >> 2;
  2483. SRC(6,0)= (t5 + 2*t4 + t3 + 2) >> 2;
  2484. SRC(7,0)= (t6 + 2*t5 + t4 + 2) >> 2;
  2485. }
  2486. static void pred8x8l_vertical_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2487. {
  2488. PREDICT_8x8_LOAD_TOP;
  2489. PREDICT_8x8_LOAD_TOPRIGHT;
  2490. SRC(0,0)= (t0 + t1 + 1) >> 1;
  2491. SRC(0,1)= (t0 + 2*t1 + t2 + 2) >> 2;
  2492. SRC(0,2)=SRC(1,0)= (t1 + t2 + 1) >> 1;
  2493. SRC(0,3)=SRC(1,1)= (t1 + 2*t2 + t3 + 2) >> 2;
  2494. SRC(0,4)=SRC(1,2)=SRC(2,0)= (t2 + t3 + 1) >> 1;
  2495. SRC(0,5)=SRC(1,3)=SRC(2,1)= (t2 + 2*t3 + t4 + 2) >> 2;
  2496. SRC(0,6)=SRC(1,4)=SRC(2,2)=SRC(3,0)= (t3 + t4 + 1) >> 1;
  2497. SRC(0,7)=SRC(1,5)=SRC(2,3)=SRC(3,1)= (t3 + 2*t4 + t5 + 2) >> 2;
  2498. SRC(1,6)=SRC(2,4)=SRC(3,2)=SRC(4,0)= (t4 + t5 + 1) >> 1;
  2499. SRC(1,7)=SRC(2,5)=SRC(3,3)=SRC(4,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2500. SRC(2,6)=SRC(3,4)=SRC(4,2)=SRC(5,0)= (t5 + t6 + 1) >> 1;
  2501. SRC(2,7)=SRC(3,5)=SRC(4,3)=SRC(5,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2502. SRC(3,6)=SRC(4,4)=SRC(5,2)=SRC(6,0)= (t6 + t7 + 1) >> 1;
  2503. SRC(3,7)=SRC(4,5)=SRC(5,3)=SRC(6,1)= (t6 + 2*t7 + t8 + 2) >> 2;
  2504. SRC(4,6)=SRC(5,4)=SRC(6,2)=SRC(7,0)= (t7 + t8 + 1) >> 1;
  2505. SRC(4,7)=SRC(5,5)=SRC(6,3)=SRC(7,1)= (t7 + 2*t8 + t9 + 2) >> 2;
  2506. SRC(5,6)=SRC(6,4)=SRC(7,2)= (t8 + t9 + 1) >> 1;
  2507. SRC(5,7)=SRC(6,5)=SRC(7,3)= (t8 + 2*t9 + t10 + 2) >> 2;
  2508. SRC(6,6)=SRC(7,4)= (t9 + t10 + 1) >> 1;
  2509. SRC(6,7)=SRC(7,5)= (t9 + 2*t10 + t11 + 2) >> 2;
  2510. SRC(7,6)= (t10 + t11 + 1) >> 1;
  2511. SRC(7,7)= (t10 + 2*t11 + t12 + 2) >> 2;
  2512. }
  2513. static void pred8x8l_horizontal_up_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2514. {
  2515. PREDICT_8x8_LOAD_LEFT;
  2516. SRC(0,0)= (l0 + l1 + 1) >> 1;
  2517. SRC(1,0)= (l0 + 2*l1 + l2 + 2) >> 2;
  2518. SRC(0,1)=SRC(2,0)= (l1 + l2 + 1) >> 1;
  2519. SRC(1,1)=SRC(3,0)= (l1 + 2*l2 + l3 + 2) >> 2;
  2520. SRC(0,2)=SRC(2,1)=SRC(4,0)= (l2 + l3 + 1) >> 1;
  2521. SRC(1,2)=SRC(3,1)=SRC(5,0)= (l2 + 2*l3 + l4 + 2) >> 2;
  2522. SRC(0,3)=SRC(2,2)=SRC(4,1)=SRC(6,0)= (l3 + l4 + 1) >> 1;
  2523. SRC(1,3)=SRC(3,2)=SRC(5,1)=SRC(7,0)= (l3 + 2*l4 + l5 + 2) >> 2;
  2524. SRC(0,4)=SRC(2,3)=SRC(4,2)=SRC(6,1)= (l4 + l5 + 1) >> 1;
  2525. SRC(1,4)=SRC(3,3)=SRC(5,2)=SRC(7,1)= (l4 + 2*l5 + l6 + 2) >> 2;
  2526. SRC(0,5)=SRC(2,4)=SRC(4,3)=SRC(6,2)= (l5 + l6 + 1) >> 1;
  2527. SRC(1,5)=SRC(3,4)=SRC(5,3)=SRC(7,2)= (l5 + 2*l6 + l7 + 2) >> 2;
  2528. SRC(0,6)=SRC(2,5)=SRC(4,4)=SRC(6,3)= (l6 + l7 + 1) >> 1;
  2529. SRC(1,6)=SRC(3,5)=SRC(5,4)=SRC(7,3)= (l6 + 3*l7 + 2) >> 2;
  2530. SRC(0,7)=SRC(1,7)=SRC(2,6)=SRC(2,7)=SRC(3,6)=
  2531. SRC(3,7)=SRC(4,5)=SRC(4,6)=SRC(4,7)=SRC(5,5)=
  2532. SRC(5,6)=SRC(5,7)=SRC(6,4)=SRC(6,5)=SRC(6,6)=
  2533. SRC(6,7)=SRC(7,4)=SRC(7,5)=SRC(7,6)=SRC(7,7)= l7;
  2534. }
  2535. #undef PREDICT_8x8_LOAD_LEFT
  2536. #undef PREDICT_8x8_LOAD_TOP
  2537. #undef PREDICT_8x8_LOAD_TOPLEFT
  2538. #undef PREDICT_8x8_LOAD_TOPRIGHT
  2539. #undef PREDICT_8x8_DC
  2540. #undef PTR
  2541. #undef PT
  2542. #undef PL
  2543. #undef SRC
  2544. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  2545. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2546. int src_x_offset, int src_y_offset,
  2547. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  2548. MpegEncContext * const s = &h->s;
  2549. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  2550. int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  2551. const int luma_xy= (mx&3) + ((my&3)<<2);
  2552. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
  2553. uint8_t * src_cb, * src_cr;
  2554. int extra_width= h->emu_edge_width;
  2555. int extra_height= h->emu_edge_height;
  2556. int emu=0;
  2557. const int full_mx= mx>>2;
  2558. const int full_my= my>>2;
  2559. const int pic_width = 16*s->mb_width;
  2560. const int pic_height = 16*s->mb_height >> MB_MBAFF;
  2561. if(!pic->data[0])
  2562. return;
  2563. if(mx&7) extra_width -= 3;
  2564. if(my&7) extra_height -= 3;
  2565. if( full_mx < 0-extra_width
  2566. || full_my < 0-extra_height
  2567. || full_mx + 16/*FIXME*/ > pic_width + extra_width
  2568. || full_my + 16/*FIXME*/ > pic_height + extra_height){
  2569. 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);
  2570. src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
  2571. emu=1;
  2572. }
  2573. qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
  2574. if(!square){
  2575. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
  2576. }
  2577. if(s->flags&CODEC_FLAG_GRAY) return;
  2578. if(MB_MBAFF){
  2579. // chroma offset when predicting from a field of opposite parity
  2580. my += 2 * ((s->mb_y & 1) - (h->ref_cache[list][scan8[n]] & 1));
  2581. emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
  2582. }
  2583. src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2584. src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2585. if(emu){
  2586. 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);
  2587. src_cb= s->edge_emu_buffer;
  2588. }
  2589. chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2590. if(emu){
  2591. 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);
  2592. src_cr= s->edge_emu_buffer;
  2593. }
  2594. chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2595. }
  2596. static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
  2597. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2598. int x_offset, int y_offset,
  2599. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2600. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2601. int list0, int list1){
  2602. MpegEncContext * const s = &h->s;
  2603. qpel_mc_func *qpix_op= qpix_put;
  2604. h264_chroma_mc_func chroma_op= chroma_put;
  2605. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2606. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2607. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2608. x_offset += 8*s->mb_x;
  2609. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2610. if(list0){
  2611. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  2612. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  2613. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2614. qpix_op, chroma_op);
  2615. qpix_op= qpix_avg;
  2616. chroma_op= chroma_avg;
  2617. }
  2618. if(list1){
  2619. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  2620. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  2621. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2622. qpix_op, chroma_op);
  2623. }
  2624. }
  2625. static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
  2626. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2627. int x_offset, int y_offset,
  2628. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2629. h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
  2630. h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
  2631. int list0, int list1){
  2632. MpegEncContext * const s = &h->s;
  2633. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2634. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2635. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2636. x_offset += 8*s->mb_x;
  2637. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2638. if(list0 && list1){
  2639. /* don't optimize for luma-only case, since B-frames usually
  2640. * use implicit weights => chroma too. */
  2641. uint8_t *tmp_cb = s->obmc_scratchpad;
  2642. uint8_t *tmp_cr = s->obmc_scratchpad + 8;
  2643. uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
  2644. int refn0 = h->ref_cache[0][ scan8[n] ];
  2645. int refn1 = h->ref_cache[1][ scan8[n] ];
  2646. mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
  2647. dest_y, dest_cb, dest_cr,
  2648. x_offset, y_offset, qpix_put, chroma_put);
  2649. mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
  2650. tmp_y, tmp_cb, tmp_cr,
  2651. x_offset, y_offset, qpix_put, chroma_put);
  2652. if(h->use_weight == 2){
  2653. int weight0 = h->implicit_weight[refn0][refn1];
  2654. int weight1 = 64 - weight0;
  2655. luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
  2656. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2657. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2658. }else{
  2659. luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
  2660. h->luma_weight[0][refn0], h->luma_weight[1][refn1],
  2661. h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
  2662. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2663. h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
  2664. h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
  2665. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2666. h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
  2667. h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
  2668. }
  2669. }else{
  2670. int list = list1 ? 1 : 0;
  2671. int refn = h->ref_cache[list][ scan8[n] ];
  2672. Picture *ref= &h->ref_list[list][refn];
  2673. mc_dir_part(h, ref, n, square, chroma_height, delta, list,
  2674. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2675. qpix_put, chroma_put);
  2676. luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
  2677. h->luma_weight[list][refn], h->luma_offset[list][refn]);
  2678. if(h->use_weight_chroma){
  2679. chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2680. h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
  2681. chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2682. h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
  2683. }
  2684. }
  2685. }
  2686. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  2687. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2688. int x_offset, int y_offset,
  2689. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2690. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2691. h264_weight_func *weight_op, h264_biweight_func *weight_avg,
  2692. int list0, int list1){
  2693. if((h->use_weight==2 && list0 && list1
  2694. && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
  2695. || h->use_weight==1)
  2696. mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2697. x_offset, y_offset, qpix_put, chroma_put,
  2698. weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
  2699. else
  2700. mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2701. x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
  2702. }
  2703. static inline void prefetch_motion(H264Context *h, int list){
  2704. /* fetch pixels for estimated mv 4 macroblocks ahead
  2705. * optimized for 64byte cache lines */
  2706. MpegEncContext * const s = &h->s;
  2707. const int refn = h->ref_cache[list][scan8[0]];
  2708. if(refn >= 0){
  2709. const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
  2710. const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
  2711. uint8_t **src= h->ref_list[list][refn].data;
  2712. int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
  2713. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  2714. off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
  2715. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  2716. }
  2717. }
  2718. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2719. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  2720. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
  2721. h264_weight_func *weight_op, h264_biweight_func *weight_avg){
  2722. MpegEncContext * const s = &h->s;
  2723. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2724. const int mb_type= s->current_picture.mb_type[mb_xy];
  2725. assert(IS_INTER(mb_type));
  2726. prefetch_motion(h, 0);
  2727. if(IS_16X16(mb_type)){
  2728. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  2729. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  2730. &weight_op[0], &weight_avg[0],
  2731. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2732. }else if(IS_16X8(mb_type)){
  2733. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  2734. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2735. &weight_op[1], &weight_avg[1],
  2736. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2737. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  2738. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2739. &weight_op[1], &weight_avg[1],
  2740. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2741. }else if(IS_8X16(mb_type)){
  2742. mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
  2743. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2744. &weight_op[2], &weight_avg[2],
  2745. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2746. mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
  2747. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2748. &weight_op[2], &weight_avg[2],
  2749. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2750. }else{
  2751. int i;
  2752. assert(IS_8X8(mb_type));
  2753. for(i=0; i<4; i++){
  2754. const int sub_mb_type= h->sub_mb_type[i];
  2755. const int n= 4*i;
  2756. int x_offset= (i&1)<<2;
  2757. int y_offset= (i&2)<<1;
  2758. if(IS_SUB_8X8(sub_mb_type)){
  2759. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2760. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2761. &weight_op[3], &weight_avg[3],
  2762. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2763. }else if(IS_SUB_8X4(sub_mb_type)){
  2764. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2765. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2766. &weight_op[4], &weight_avg[4],
  2767. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2768. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  2769. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2770. &weight_op[4], &weight_avg[4],
  2771. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2772. }else if(IS_SUB_4X8(sub_mb_type)){
  2773. mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2774. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2775. &weight_op[5], &weight_avg[5],
  2776. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2777. mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
  2778. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2779. &weight_op[5], &weight_avg[5],
  2780. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2781. }else{
  2782. int j;
  2783. assert(IS_SUB_4X4(sub_mb_type));
  2784. for(j=0; j<4; j++){
  2785. int sub_x_offset= x_offset + 2*(j&1);
  2786. int sub_y_offset= y_offset + (j&2);
  2787. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  2788. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2789. &weight_op[6], &weight_avg[6],
  2790. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2791. }
  2792. }
  2793. }
  2794. }
  2795. prefetch_motion(h, 1);
  2796. }
  2797. static void decode_init_vlc(H264Context *h){
  2798. static int done = 0;
  2799. if (!done) {
  2800. int i;
  2801. done = 1;
  2802. init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
  2803. &chroma_dc_coeff_token_len [0], 1, 1,
  2804. &chroma_dc_coeff_token_bits[0], 1, 1, 1);
  2805. for(i=0; i<4; i++){
  2806. init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
  2807. &coeff_token_len [i][0], 1, 1,
  2808. &coeff_token_bits[i][0], 1, 1, 1);
  2809. }
  2810. for(i=0; i<3; i++){
  2811. init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
  2812. &chroma_dc_total_zeros_len [i][0], 1, 1,
  2813. &chroma_dc_total_zeros_bits[i][0], 1, 1, 1);
  2814. }
  2815. for(i=0; i<15; i++){
  2816. init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
  2817. &total_zeros_len [i][0], 1, 1,
  2818. &total_zeros_bits[i][0], 1, 1, 1);
  2819. }
  2820. for(i=0; i<6; i++){
  2821. init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
  2822. &run_len [i][0], 1, 1,
  2823. &run_bits[i][0], 1, 1, 1);
  2824. }
  2825. init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
  2826. &run_len [6][0], 1, 1,
  2827. &run_bits[6][0], 1, 1, 1);
  2828. }
  2829. }
  2830. /**
  2831. * Sets the intra prediction function pointers.
  2832. */
  2833. static void init_pred_ptrs(H264Context *h){
  2834. // MpegEncContext * const s = &h->s;
  2835. h->pred4x4[VERT_PRED ]= pred4x4_vertical_c;
  2836. h->pred4x4[HOR_PRED ]= pred4x4_horizontal_c;
  2837. h->pred4x4[DC_PRED ]= pred4x4_dc_c;
  2838. h->pred4x4[DIAG_DOWN_LEFT_PRED ]= pred4x4_down_left_c;
  2839. h->pred4x4[DIAG_DOWN_RIGHT_PRED]= pred4x4_down_right_c;
  2840. h->pred4x4[VERT_RIGHT_PRED ]= pred4x4_vertical_right_c;
  2841. h->pred4x4[HOR_DOWN_PRED ]= pred4x4_horizontal_down_c;
  2842. h->pred4x4[VERT_LEFT_PRED ]= pred4x4_vertical_left_c;
  2843. h->pred4x4[HOR_UP_PRED ]= pred4x4_horizontal_up_c;
  2844. h->pred4x4[LEFT_DC_PRED ]= pred4x4_left_dc_c;
  2845. h->pred4x4[TOP_DC_PRED ]= pred4x4_top_dc_c;
  2846. h->pred4x4[DC_128_PRED ]= pred4x4_128_dc_c;
  2847. h->pred8x8l[VERT_PRED ]= pred8x8l_vertical_c;
  2848. h->pred8x8l[HOR_PRED ]= pred8x8l_horizontal_c;
  2849. h->pred8x8l[DC_PRED ]= pred8x8l_dc_c;
  2850. h->pred8x8l[DIAG_DOWN_LEFT_PRED ]= pred8x8l_down_left_c;
  2851. h->pred8x8l[DIAG_DOWN_RIGHT_PRED]= pred8x8l_down_right_c;
  2852. h->pred8x8l[VERT_RIGHT_PRED ]= pred8x8l_vertical_right_c;
  2853. h->pred8x8l[HOR_DOWN_PRED ]= pred8x8l_horizontal_down_c;
  2854. h->pred8x8l[VERT_LEFT_PRED ]= pred8x8l_vertical_left_c;
  2855. h->pred8x8l[HOR_UP_PRED ]= pred8x8l_horizontal_up_c;
  2856. h->pred8x8l[LEFT_DC_PRED ]= pred8x8l_left_dc_c;
  2857. h->pred8x8l[TOP_DC_PRED ]= pred8x8l_top_dc_c;
  2858. h->pred8x8l[DC_128_PRED ]= pred8x8l_128_dc_c;
  2859. h->pred8x8[DC_PRED8x8 ]= pred8x8_dc_c;
  2860. h->pred8x8[VERT_PRED8x8 ]= pred8x8_vertical_c;
  2861. h->pred8x8[HOR_PRED8x8 ]= pred8x8_horizontal_c;
  2862. h->pred8x8[PLANE_PRED8x8 ]= pred8x8_plane_c;
  2863. h->pred8x8[LEFT_DC_PRED8x8]= pred8x8_left_dc_c;
  2864. h->pred8x8[TOP_DC_PRED8x8 ]= pred8x8_top_dc_c;
  2865. h->pred8x8[DC_128_PRED8x8 ]= pred8x8_128_dc_c;
  2866. h->pred16x16[DC_PRED8x8 ]= pred16x16_dc_c;
  2867. h->pred16x16[VERT_PRED8x8 ]= pred16x16_vertical_c;
  2868. h->pred16x16[HOR_PRED8x8 ]= pred16x16_horizontal_c;
  2869. h->pred16x16[PLANE_PRED8x8 ]= pred16x16_plane_c;
  2870. h->pred16x16[LEFT_DC_PRED8x8]= pred16x16_left_dc_c;
  2871. h->pred16x16[TOP_DC_PRED8x8 ]= pred16x16_top_dc_c;
  2872. h->pred16x16[DC_128_PRED8x8 ]= pred16x16_128_dc_c;
  2873. }
  2874. static void free_tables(H264Context *h){
  2875. av_freep(&h->intra4x4_pred_mode);
  2876. av_freep(&h->chroma_pred_mode_table);
  2877. av_freep(&h->cbp_table);
  2878. av_freep(&h->mvd_table[0]);
  2879. av_freep(&h->mvd_table[1]);
  2880. av_freep(&h->direct_table);
  2881. av_freep(&h->non_zero_count);
  2882. av_freep(&h->slice_table_base);
  2883. av_freep(&h->top_borders[1]);
  2884. av_freep(&h->top_borders[0]);
  2885. h->slice_table= NULL;
  2886. av_freep(&h->mb2b_xy);
  2887. av_freep(&h->mb2b8_xy);
  2888. av_freep(&h->s.obmc_scratchpad);
  2889. }
  2890. static void init_dequant8_coeff_table(H264Context *h){
  2891. int i,q,x;
  2892. const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
  2893. h->dequant8_coeff[0] = h->dequant8_buffer[0];
  2894. h->dequant8_coeff[1] = h->dequant8_buffer[1];
  2895. for(i=0; i<2; i++ ){
  2896. if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
  2897. h->dequant8_coeff[1] = h->dequant8_buffer[0];
  2898. break;
  2899. }
  2900. for(q=0; q<52; q++){
  2901. int shift = div6[q];
  2902. int idx = rem6[q];
  2903. for(x=0; x<64; x++)
  2904. h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
  2905. ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
  2906. h->pps.scaling_matrix8[i][x]) << shift;
  2907. }
  2908. }
  2909. }
  2910. static void init_dequant4_coeff_table(H264Context *h){
  2911. int i,j,q,x;
  2912. const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
  2913. for(i=0; i<6; i++ ){
  2914. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  2915. for(j=0; j<i; j++){
  2916. if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
  2917. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  2918. break;
  2919. }
  2920. }
  2921. if(j<i)
  2922. continue;
  2923. for(q=0; q<52; q++){
  2924. int shift = div6[q] + 2;
  2925. int idx = rem6[q];
  2926. for(x=0; x<16; x++)
  2927. h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
  2928. ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
  2929. h->pps.scaling_matrix4[i][x]) << shift;
  2930. }
  2931. }
  2932. }
  2933. static void init_dequant_tables(H264Context *h){
  2934. int i,x;
  2935. init_dequant4_coeff_table(h);
  2936. if(h->pps.transform_8x8_mode)
  2937. init_dequant8_coeff_table(h);
  2938. if(h->sps.transform_bypass){
  2939. for(i=0; i<6; i++)
  2940. for(x=0; x<16; x++)
  2941. h->dequant4_coeff[i][0][x] = 1<<6;
  2942. if(h->pps.transform_8x8_mode)
  2943. for(i=0; i<2; i++)
  2944. for(x=0; x<64; x++)
  2945. h->dequant8_coeff[i][0][x] = 1<<6;
  2946. }
  2947. }
  2948. /**
  2949. * allocates tables.
  2950. * needs width/height
  2951. */
  2952. static int alloc_tables(H264Context *h){
  2953. MpegEncContext * const s = &h->s;
  2954. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  2955. int x,y;
  2956. CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
  2957. CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
  2958. CHECKED_ALLOCZ(h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(uint8_t))
  2959. CHECKED_ALLOCZ(h->top_borders[0] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2960. CHECKED_ALLOCZ(h->top_borders[1] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2961. CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
  2962. if( h->pps.cabac ) {
  2963. CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
  2964. CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
  2965. CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t));
  2966. CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t));
  2967. }
  2968. memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(uint8_t));
  2969. h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
  2970. CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint32_t));
  2971. CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint32_t));
  2972. for(y=0; y<s->mb_height; y++){
  2973. for(x=0; x<s->mb_width; x++){
  2974. const int mb_xy= x + y*s->mb_stride;
  2975. const int b_xy = 4*x + 4*y*h->b_stride;
  2976. const int b8_xy= 2*x + 2*y*h->b8_stride;
  2977. h->mb2b_xy [mb_xy]= b_xy;
  2978. h->mb2b8_xy[mb_xy]= b8_xy;
  2979. }
  2980. }
  2981. s->obmc_scratchpad = NULL;
  2982. if(!h->dequant4_coeff[0])
  2983. init_dequant_tables(h);
  2984. return 0;
  2985. fail:
  2986. free_tables(h);
  2987. return -1;
  2988. }
  2989. static void common_init(H264Context *h){
  2990. MpegEncContext * const s = &h->s;
  2991. s->width = s->avctx->width;
  2992. s->height = s->avctx->height;
  2993. s->codec_id= s->avctx->codec->id;
  2994. init_pred_ptrs(h);
  2995. h->dequant_coeff_pps= -1;
  2996. s->unrestricted_mv=1;
  2997. s->decode=1; //FIXME
  2998. memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  2999. memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  3000. }
  3001. static int decode_init(AVCodecContext *avctx){
  3002. H264Context *h= avctx->priv_data;
  3003. MpegEncContext * const s = &h->s;
  3004. MPV_decode_defaults(s);
  3005. s->avctx = avctx;
  3006. common_init(h);
  3007. s->out_format = FMT_H264;
  3008. s->workaround_bugs= avctx->workaround_bugs;
  3009. // set defaults
  3010. // s->decode_mb= ff_h263_decode_mb;
  3011. s->low_delay= 1;
  3012. avctx->pix_fmt= PIX_FMT_YUV420P;
  3013. decode_init_vlc(h);
  3014. if(avctx->extradata_size > 0 && avctx->extradata &&
  3015. *(char *)avctx->extradata == 1){
  3016. h->is_avc = 1;
  3017. h->got_avcC = 0;
  3018. } else {
  3019. h->is_avc = 0;
  3020. }
  3021. return 0;
  3022. }
  3023. static int frame_start(H264Context *h){
  3024. MpegEncContext * const s = &h->s;
  3025. int i;
  3026. if(MPV_frame_start(s, s->avctx) < 0)
  3027. return -1;
  3028. ff_er_frame_start(s);
  3029. assert(s->linesize && s->uvlinesize);
  3030. for(i=0; i<16; i++){
  3031. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  3032. h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  3033. }
  3034. for(i=0; i<4; i++){
  3035. h->block_offset[16+i]=
  3036. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  3037. h->block_offset[24+16+i]=
  3038. h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  3039. }
  3040. /* can't be in alloc_tables because linesize isn't known there.
  3041. * FIXME: redo bipred weight to not require extra buffer? */
  3042. if(!s->obmc_scratchpad)
  3043. s->obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  3044. /* some macroblocks will be accessed before they're available */
  3045. if(FRAME_MBAFF)
  3046. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(uint8_t));
  3047. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  3048. return 0;
  3049. }
  3050. static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  3051. MpegEncContext * const s = &h->s;
  3052. int i;
  3053. src_y -= linesize;
  3054. src_cb -= uvlinesize;
  3055. src_cr -= uvlinesize;
  3056. // There are two lines saved, the line above the the top macroblock of a pair,
  3057. // and the line above the bottom macroblock
  3058. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  3059. for(i=1; i<17; i++){
  3060. h->left_border[i]= src_y[15+i* linesize];
  3061. }
  3062. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
  3063. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
  3064. if(!(s->flags&CODEC_FLAG_GRAY)){
  3065. h->left_border[17 ]= h->top_borders[0][s->mb_x][16+7];
  3066. h->left_border[17+9]= h->top_borders[0][s->mb_x][24+7];
  3067. for(i=1; i<9; i++){
  3068. h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
  3069. h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
  3070. }
  3071. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
  3072. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
  3073. }
  3074. }
  3075. 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){
  3076. MpegEncContext * const s = &h->s;
  3077. int temp8, i;
  3078. uint64_t temp64;
  3079. int deblock_left = (s->mb_x > 0);
  3080. int deblock_top = (s->mb_y > 0);
  3081. src_y -= linesize + 1;
  3082. src_cb -= uvlinesize + 1;
  3083. src_cr -= uvlinesize + 1;
  3084. #define XCHG(a,b,t,xchg)\
  3085. t= a;\
  3086. if(xchg)\
  3087. a= b;\
  3088. b= t;
  3089. if(deblock_left){
  3090. for(i = !deblock_top; i<17; i++){
  3091. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  3092. }
  3093. }
  3094. if(deblock_top){
  3095. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  3096. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  3097. if(s->mb_x+1 < s->mb_width){
  3098. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  3099. }
  3100. }
  3101. if(!(s->flags&CODEC_FLAG_GRAY)){
  3102. if(deblock_left){
  3103. for(i = !deblock_top; i<9; i++){
  3104. XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
  3105. XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
  3106. }
  3107. }
  3108. if(deblock_top){
  3109. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  3110. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  3111. }
  3112. }
  3113. }
  3114. static inline void backup_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  3115. MpegEncContext * const s = &h->s;
  3116. int i;
  3117. src_y -= 2 * linesize;
  3118. src_cb -= 2 * uvlinesize;
  3119. src_cr -= 2 * uvlinesize;
  3120. // There are two lines saved, the line above the the top macroblock of a pair,
  3121. // and the line above the bottom macroblock
  3122. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  3123. h->left_border[1]= h->top_borders[1][s->mb_x][15];
  3124. for(i=2; i<34; i++){
  3125. h->left_border[i]= src_y[15+i* linesize];
  3126. }
  3127. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 32*linesize);
  3128. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+32*linesize);
  3129. *(uint64_t*)(h->top_borders[1][s->mb_x]+0)= *(uint64_t*)(src_y + 33*linesize);
  3130. *(uint64_t*)(h->top_borders[1][s->mb_x]+8)= *(uint64_t*)(src_y +8+33*linesize);
  3131. if(!(s->flags&CODEC_FLAG_GRAY)){
  3132. h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7];
  3133. h->left_border[34+ 1]= h->top_borders[1][s->mb_x][16+7];
  3134. h->left_border[34+18 ]= h->top_borders[0][s->mb_x][24+7];
  3135. h->left_border[34+18+1]= h->top_borders[1][s->mb_x][24+7];
  3136. for(i=2; i<18; i++){
  3137. h->left_border[i+34 ]= src_cb[7+i*uvlinesize];
  3138. h->left_border[i+34+18]= src_cr[7+i*uvlinesize];
  3139. }
  3140. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+16*uvlinesize);
  3141. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+16*uvlinesize);
  3142. *(uint64_t*)(h->top_borders[1][s->mb_x]+16)= *(uint64_t*)(src_cb+17*uvlinesize);
  3143. *(uint64_t*)(h->top_borders[1][s->mb_x]+24)= *(uint64_t*)(src_cr+17*uvlinesize);
  3144. }
  3145. }
  3146. 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){
  3147. MpegEncContext * const s = &h->s;
  3148. int temp8, i;
  3149. uint64_t temp64;
  3150. int deblock_left = (s->mb_x > 0);
  3151. int deblock_top = (s->mb_y > 1);
  3152. 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);
  3153. src_y -= 2 * linesize + 1;
  3154. src_cb -= 2 * uvlinesize + 1;
  3155. src_cr -= 2 * uvlinesize + 1;
  3156. #define XCHG(a,b,t,xchg)\
  3157. t= a;\
  3158. if(xchg)\
  3159. a= b;\
  3160. b= t;
  3161. if(deblock_left){
  3162. for(i = (!deblock_top)<<1; i<34; i++){
  3163. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  3164. }
  3165. }
  3166. if(deblock_top){
  3167. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  3168. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  3169. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+0), *(uint64_t*)(src_y +1 +linesize), temp64, xchg);
  3170. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+8), *(uint64_t*)(src_y +9 +linesize), temp64, 1);
  3171. if(s->mb_x+1 < s->mb_width){
  3172. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  3173. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x+1]), *(uint64_t*)(src_y +17 +linesize), temp64, 1);
  3174. }
  3175. }
  3176. if(!(s->flags&CODEC_FLAG_GRAY)){
  3177. if(deblock_left){
  3178. for(i = (!deblock_top) << 1; i<18; i++){
  3179. XCHG(h->left_border[i+34 ], src_cb[i*uvlinesize], temp8, xchg);
  3180. XCHG(h->left_border[i+34+18], src_cr[i*uvlinesize], temp8, xchg);
  3181. }
  3182. }
  3183. if(deblock_top){
  3184. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  3185. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  3186. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+16), *(uint64_t*)(src_cb+1 +uvlinesize), temp64, 1);
  3187. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+24), *(uint64_t*)(src_cr+1 +uvlinesize), temp64, 1);
  3188. }
  3189. }
  3190. }
  3191. static void hl_decode_mb(H264Context *h){
  3192. MpegEncContext * const s = &h->s;
  3193. const int mb_x= s->mb_x;
  3194. const int mb_y= s->mb_y;
  3195. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3196. const int mb_type= s->current_picture.mb_type[mb_xy];
  3197. uint8_t *dest_y, *dest_cb, *dest_cr;
  3198. int linesize, uvlinesize /*dct_offset*/;
  3199. int i;
  3200. int *block_offset = &h->block_offset[0];
  3201. const unsigned int bottom = mb_y & 1;
  3202. const int transform_bypass = (s->qscale == 0 && h->sps.transform_bypass);
  3203. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  3204. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  3205. if(!s->decode)
  3206. return;
  3207. dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3208. dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3209. dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3210. if (MB_FIELD) {
  3211. linesize = h->mb_linesize = s->linesize * 2;
  3212. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  3213. block_offset = &h->block_offset[24];
  3214. if(mb_y&1){ //FIXME move out of this func?
  3215. dest_y -= s->linesize*15;
  3216. dest_cb-= s->uvlinesize*7;
  3217. dest_cr-= s->uvlinesize*7;
  3218. }
  3219. if(FRAME_MBAFF) {
  3220. int list;
  3221. for(list=0; list<2; list++){
  3222. if(!USES_LIST(mb_type, list))
  3223. continue;
  3224. if(IS_16X16(mb_type)){
  3225. int8_t *ref = &h->ref_cache[list][scan8[0]];
  3226. fill_rectangle(ref, 4, 4, 8, 16+*ref^(s->mb_y&1), 1);
  3227. }else{
  3228. for(i=0; i<16; i+=4){
  3229. //FIXME can refs be smaller than 8x8 when !direct_8x8_inference ?
  3230. int ref = h->ref_cache[list][scan8[i]];
  3231. if(ref >= 0)
  3232. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, 16+ref^(s->mb_y&1), 1);
  3233. }
  3234. }
  3235. }
  3236. }
  3237. } else {
  3238. linesize = h->mb_linesize = s->linesize;
  3239. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  3240. // dct_offset = s->linesize * 16;
  3241. }
  3242. if(transform_bypass){
  3243. idct_dc_add =
  3244. idct_add = IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  3245. }else if(IS_8x8DCT(mb_type)){
  3246. idct_dc_add = s->dsp.h264_idct8_dc_add;
  3247. idct_add = s->dsp.h264_idct8_add;
  3248. }else{
  3249. idct_dc_add = s->dsp.h264_idct_dc_add;
  3250. idct_add = s->dsp.h264_idct_add;
  3251. }
  3252. if(FRAME_MBAFF && h->deblocking_filter && IS_INTRA(mb_type)
  3253. && (!bottom || !IS_INTRA(s->current_picture.mb_type[mb_xy-s->mb_stride]))){
  3254. int mbt_y = mb_y&~1;
  3255. uint8_t *top_y = s->current_picture.data[0] + (mbt_y * 16* s->linesize ) + mb_x * 16;
  3256. uint8_t *top_cb = s->current_picture.data[1] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  3257. uint8_t *top_cr = s->current_picture.data[2] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  3258. xchg_pair_border(h, top_y, top_cb, top_cr, s->linesize, s->uvlinesize, 1);
  3259. }
  3260. if (IS_INTRA_PCM(mb_type)) {
  3261. unsigned int x, y;
  3262. // The pixels are stored in h->mb array in the same order as levels,
  3263. // copy them in output in the correct order.
  3264. for(i=0; i<16; i++) {
  3265. for (y=0; y<4; y++) {
  3266. for (x=0; x<4; x++) {
  3267. *(dest_y + block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x];
  3268. }
  3269. }
  3270. }
  3271. for(i=16; i<16+4; i++) {
  3272. for (y=0; y<4; y++) {
  3273. for (x=0; x<4; x++) {
  3274. *(dest_cb + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  3275. }
  3276. }
  3277. }
  3278. for(i=20; i<20+4; i++) {
  3279. for (y=0; y<4; y++) {
  3280. for (x=0; x<4; x++) {
  3281. *(dest_cr + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  3282. }
  3283. }
  3284. }
  3285. } else {
  3286. if(IS_INTRA(mb_type)){
  3287. if(h->deblocking_filter && !FRAME_MBAFF)
  3288. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1);
  3289. if(!(s->flags&CODEC_FLAG_GRAY)){
  3290. h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  3291. h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  3292. }
  3293. if(IS_INTRA4x4(mb_type)){
  3294. if(!s->encoding){
  3295. if(IS_8x8DCT(mb_type)){
  3296. for(i=0; i<16; i+=4){
  3297. uint8_t * const ptr= dest_y + block_offset[i];
  3298. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  3299. const int nnz = h->non_zero_count_cache[ scan8[i] ];
  3300. h->pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  3301. (h->topright_samples_available<<(i+1))&0x8000, linesize);
  3302. if(nnz){
  3303. if(nnz == 1 && h->mb[i*16])
  3304. idct_dc_add(ptr, h->mb + i*16, linesize);
  3305. else
  3306. idct_add(ptr, h->mb + i*16, linesize);
  3307. }
  3308. }
  3309. }else
  3310. for(i=0; i<16; i++){
  3311. uint8_t * const ptr= dest_y + block_offset[i];
  3312. uint8_t *topright;
  3313. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  3314. int nnz, tr;
  3315. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  3316. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  3317. assert(mb_y || linesize <= block_offset[i]);
  3318. if(!topright_avail){
  3319. tr= ptr[3 - linesize]*0x01010101;
  3320. topright= (uint8_t*) &tr;
  3321. }else
  3322. topright= ptr + 4 - linesize;
  3323. }else
  3324. topright= NULL;
  3325. h->pred4x4[ dir ](ptr, topright, linesize);
  3326. nnz = h->non_zero_count_cache[ scan8[i] ];
  3327. if(nnz){
  3328. if(s->codec_id == CODEC_ID_H264){
  3329. if(nnz == 1 && h->mb[i*16])
  3330. idct_dc_add(ptr, h->mb + i*16, linesize);
  3331. else
  3332. idct_add(ptr, h->mb + i*16, linesize);
  3333. }else
  3334. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  3335. }
  3336. }
  3337. }
  3338. }else{
  3339. h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  3340. if(s->codec_id == CODEC_ID_H264){
  3341. if(!transform_bypass)
  3342. h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[IS_INTRA(mb_type) ? 0:3][s->qscale][0]);
  3343. }else
  3344. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  3345. }
  3346. if(h->deblocking_filter && !FRAME_MBAFF)
  3347. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
  3348. }else if(s->codec_id == CODEC_ID_H264){
  3349. hl_motion(h, dest_y, dest_cb, dest_cr,
  3350. s->dsp.put_h264_qpel_pixels_tab, s->dsp.put_h264_chroma_pixels_tab,
  3351. s->dsp.avg_h264_qpel_pixels_tab, s->dsp.avg_h264_chroma_pixels_tab,
  3352. s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
  3353. }
  3354. if(!IS_INTRA4x4(mb_type)){
  3355. if(s->codec_id == CODEC_ID_H264){
  3356. if(IS_INTRA16x16(mb_type)){
  3357. for(i=0; i<16; i++){
  3358. if(h->non_zero_count_cache[ scan8[i] ])
  3359. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3360. else if(h->mb[i*16])
  3361. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3362. }
  3363. }else{
  3364. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  3365. for(i=0; i<16; i+=di){
  3366. int nnz = h->non_zero_count_cache[ scan8[i] ];
  3367. if(nnz){
  3368. if(nnz==1 && h->mb[i*16])
  3369. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3370. else
  3371. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3372. }
  3373. }
  3374. }
  3375. }else{
  3376. for(i=0; i<16; i++){
  3377. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  3378. uint8_t * const ptr= dest_y + block_offset[i];
  3379. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  3380. }
  3381. }
  3382. }
  3383. }
  3384. if(!(s->flags&CODEC_FLAG_GRAY)){
  3385. uint8_t *dest[2] = {dest_cb, dest_cr};
  3386. if(transform_bypass){
  3387. idct_add = idct_dc_add = s->dsp.add_pixels4;
  3388. }else{
  3389. idct_add = s->dsp.h264_idct_add;
  3390. idct_dc_add = s->dsp.h264_idct_dc_add;
  3391. 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]);
  3392. 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]);
  3393. }
  3394. if(s->codec_id == CODEC_ID_H264){
  3395. for(i=16; i<16+8; i++){
  3396. if(h->non_zero_count_cache[ scan8[i] ])
  3397. idct_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3398. else if(h->mb[i*16])
  3399. idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3400. }
  3401. }else{
  3402. for(i=16; i<16+8; i++){
  3403. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  3404. uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
  3405. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  3406. }
  3407. }
  3408. }
  3409. }
  3410. }
  3411. if(h->deblocking_filter) {
  3412. if (FRAME_MBAFF) {
  3413. //FIXME try deblocking one mb at a time?
  3414. // the reduction in load/storing mvs and such might outweigh the extra backup/xchg_border
  3415. const int mb_y = s->mb_y - 1;
  3416. uint8_t *pair_dest_y, *pair_dest_cb, *pair_dest_cr;
  3417. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3418. const int mb_type_top = s->current_picture.mb_type[mb_xy];
  3419. const int mb_type_bottom= s->current_picture.mb_type[mb_xy+s->mb_stride];
  3420. if (!bottom) return;
  3421. pair_dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3422. pair_dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3423. pair_dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3424. if(IS_INTRA(mb_type_top | mb_type_bottom))
  3425. xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0);
  3426. backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize);
  3427. // deblock a pair
  3428. // top
  3429. s->mb_y--;
  3430. 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);
  3431. fill_caches(h, mb_type_top, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3432. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy]);
  3433. filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize);
  3434. // bottom
  3435. s->mb_y++;
  3436. tprintf("call mbaff filter_mb\n");
  3437. fill_caches(h, mb_type_bottom, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3438. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy+s->mb_stride]);
  3439. filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3440. } else {
  3441. tprintf("call filter_mb\n");
  3442. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3443. fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3444. filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3445. }
  3446. }
  3447. }
  3448. /**
  3449. * fills the default_ref_list.
  3450. */
  3451. static int fill_default_ref_list(H264Context *h){
  3452. MpegEncContext * const s = &h->s;
  3453. int i;
  3454. int smallest_poc_greater_than_current = -1;
  3455. Picture sorted_short_ref[32];
  3456. if(h->slice_type==B_TYPE){
  3457. int out_i;
  3458. int limit= INT_MIN;
  3459. /* sort frame according to poc in B slice */
  3460. for(out_i=0; out_i<h->short_ref_count; out_i++){
  3461. int best_i=INT_MIN;
  3462. int best_poc=INT_MAX;
  3463. for(i=0; i<h->short_ref_count; i++){
  3464. const int poc= h->short_ref[i]->poc;
  3465. if(poc > limit && poc < best_poc){
  3466. best_poc= poc;
  3467. best_i= i;
  3468. }
  3469. }
  3470. assert(best_i != INT_MIN);
  3471. limit= best_poc;
  3472. sorted_short_ref[out_i]= *h->short_ref[best_i];
  3473. 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);
  3474. if (-1 == smallest_poc_greater_than_current) {
  3475. if (h->short_ref[best_i]->poc >= s->current_picture_ptr->poc) {
  3476. smallest_poc_greater_than_current = out_i;
  3477. }
  3478. }
  3479. }
  3480. }
  3481. if(s->picture_structure == PICT_FRAME){
  3482. if(h->slice_type==B_TYPE){
  3483. int list;
  3484. tprintf("current poc: %d, smallest_poc_greater_than_current: %d\n", s->current_picture_ptr->poc, smallest_poc_greater_than_current);
  3485. // find the largest poc
  3486. for(list=0; list<2; list++){
  3487. int index = 0;
  3488. int j= -99;
  3489. int step= list ? -1 : 1;
  3490. for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++, j+=step) {
  3491. while(j<0 || j>= h->short_ref_count){
  3492. if(j != -99 && step == (list ? -1 : 1))
  3493. return -1;
  3494. step = -step;
  3495. j= smallest_poc_greater_than_current + (step>>1);
  3496. }
  3497. if(sorted_short_ref[j].reference != 3) continue;
  3498. h->default_ref_list[list][index ]= sorted_short_ref[j];
  3499. h->default_ref_list[list][index++].pic_id= sorted_short_ref[j].frame_num;
  3500. }
  3501. for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){
  3502. if(h->long_ref[i] == NULL) continue;
  3503. if(h->long_ref[i]->reference != 3) continue;
  3504. h->default_ref_list[ list ][index ]= *h->long_ref[i];
  3505. h->default_ref_list[ list ][index++].pic_id= i;;
  3506. }
  3507. if(list && (smallest_poc_greater_than_current<=0 || smallest_poc_greater_than_current>=h->short_ref_count) && (1 < index)){
  3508. // swap the two first elements of L1 when
  3509. // L0 and L1 are identical
  3510. Picture temp= h->default_ref_list[1][0];
  3511. h->default_ref_list[1][0] = h->default_ref_list[1][1];
  3512. h->default_ref_list[1][1] = temp;
  3513. }
  3514. if(index < h->ref_count[ list ])
  3515. memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
  3516. }
  3517. }else{
  3518. int index=0;
  3519. for(i=0; i<h->short_ref_count; i++){
  3520. if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
  3521. h->default_ref_list[0][index ]= *h->short_ref[i];
  3522. h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
  3523. }
  3524. for(i = 0; i < 16; i++){
  3525. if(h->long_ref[i] == NULL) continue;
  3526. if(h->long_ref[i]->reference != 3) continue;
  3527. h->default_ref_list[0][index ]= *h->long_ref[i];
  3528. h->default_ref_list[0][index++].pic_id= i;;
  3529. }
  3530. if(index < h->ref_count[0])
  3531. memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
  3532. }
  3533. }else{ //FIELD
  3534. if(h->slice_type==B_TYPE){
  3535. }else{
  3536. //FIXME second field balh
  3537. }
  3538. }
  3539. #ifdef TRACE
  3540. for (i=0; i<h->ref_count[0]; i++) {
  3541. 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]);
  3542. }
  3543. if(h->slice_type==B_TYPE){
  3544. for (i=0; i<h->ref_count[1]; i++) {
  3545. 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]);
  3546. }
  3547. }
  3548. #endif
  3549. return 0;
  3550. }
  3551. static void print_short_term(H264Context *h);
  3552. static void print_long_term(H264Context *h);
  3553. static int decode_ref_pic_list_reordering(H264Context *h){
  3554. MpegEncContext * const s = &h->s;
  3555. int list, index;
  3556. print_short_term(h);
  3557. print_long_term(h);
  3558. if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move before func
  3559. for(list=0; list<2; list++){
  3560. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  3561. if(get_bits1(&s->gb)){
  3562. int pred= h->curr_pic_num;
  3563. for(index=0; ; index++){
  3564. int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
  3565. int pic_id;
  3566. int i;
  3567. Picture *ref = NULL;
  3568. if(reordering_of_pic_nums_idc==3)
  3569. break;
  3570. if(index >= h->ref_count[list]){
  3571. av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
  3572. return -1;
  3573. }
  3574. if(reordering_of_pic_nums_idc<3){
  3575. if(reordering_of_pic_nums_idc<2){
  3576. const int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  3577. if(abs_diff_pic_num >= h->max_pic_num){
  3578. av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  3579. return -1;
  3580. }
  3581. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  3582. else pred+= abs_diff_pic_num;
  3583. pred &= h->max_pic_num - 1;
  3584. for(i= h->short_ref_count-1; i>=0; i--){
  3585. ref = h->short_ref[i];
  3586. assert(ref->reference == 3);
  3587. assert(!ref->long_ref);
  3588. if(ref->data[0] != NULL && ref->frame_num == pred && ref->long_ref == 0) // ignore non existing pictures by testing data[0] pointer
  3589. break;
  3590. }
  3591. if(i>=0)
  3592. ref->pic_id= ref->frame_num;
  3593. }else{
  3594. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  3595. ref = h->long_ref[pic_id];
  3596. ref->pic_id= pic_id;
  3597. assert(ref->reference == 3);
  3598. assert(ref->long_ref);
  3599. i=0;
  3600. }
  3601. if (i < 0) {
  3602. av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  3603. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  3604. } else {
  3605. for(i=index; i+1<h->ref_count[list]; i++){
  3606. if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
  3607. break;
  3608. }
  3609. for(; i > index; i--){
  3610. h->ref_list[list][i]= h->ref_list[list][i-1];
  3611. }
  3612. h->ref_list[list][index]= *ref;
  3613. }
  3614. }else{
  3615. av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  3616. return -1;
  3617. }
  3618. }
  3619. }
  3620. if(h->slice_type!=B_TYPE) break;
  3621. }
  3622. for(list=0; list<2; list++){
  3623. for(index= 0; index < h->ref_count[list]; index++){
  3624. if(!h->ref_list[list][index].data[0])
  3625. h->ref_list[list][index]= s->current_picture;
  3626. }
  3627. if(h->slice_type!=B_TYPE) break;
  3628. }
  3629. if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)
  3630. direct_dist_scale_factor(h);
  3631. direct_ref_list_init(h);
  3632. return 0;
  3633. }
  3634. static void fill_mbaff_ref_list(H264Context *h){
  3635. int list, i, j;
  3636. for(list=0; list<2; list++){
  3637. for(i=0; i<h->ref_count[list]; i++){
  3638. Picture *frame = &h->ref_list[list][i];
  3639. Picture *field = &h->ref_list[list][16+2*i];
  3640. field[0] = *frame;
  3641. for(j=0; j<3; j++)
  3642. field[0].linesize[j] <<= 1;
  3643. field[1] = field[0];
  3644. for(j=0; j<3; j++)
  3645. field[1].data[j] += frame->linesize[j];
  3646. h->luma_weight[list][16+2*i] = h->luma_weight[list][16+2*i+1] = h->luma_weight[list][i];
  3647. h->luma_offset[list][16+2*i] = h->luma_offset[list][16+2*i+1] = h->luma_offset[list][i];
  3648. for(j=0; j<2; j++){
  3649. h->chroma_weight[list][16+2*i][j] = h->chroma_weight[list][16+2*i+1][j] = h->chroma_weight[list][i][j];
  3650. h->chroma_offset[list][16+2*i][j] = h->chroma_offset[list][16+2*i+1][j] = h->chroma_offset[list][i][j];
  3651. }
  3652. }
  3653. }
  3654. for(j=0; j<h->ref_count[1]; j++){
  3655. for(i=0; i<h->ref_count[0]; i++)
  3656. h->implicit_weight[j][16+2*i] = h->implicit_weight[j][16+2*i+1] = h->implicit_weight[j][i];
  3657. memcpy(h->implicit_weight[16+2*j], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3658. memcpy(h->implicit_weight[16+2*j+1], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3659. }
  3660. }
  3661. static int pred_weight_table(H264Context *h){
  3662. MpegEncContext * const s = &h->s;
  3663. int list, i;
  3664. int luma_def, chroma_def;
  3665. h->use_weight= 0;
  3666. h->use_weight_chroma= 0;
  3667. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  3668. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  3669. luma_def = 1<<h->luma_log2_weight_denom;
  3670. chroma_def = 1<<h->chroma_log2_weight_denom;
  3671. for(list=0; list<2; list++){
  3672. for(i=0; i<h->ref_count[list]; i++){
  3673. int luma_weight_flag, chroma_weight_flag;
  3674. luma_weight_flag= get_bits1(&s->gb);
  3675. if(luma_weight_flag){
  3676. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  3677. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  3678. if( h->luma_weight[list][i] != luma_def
  3679. || h->luma_offset[list][i] != 0)
  3680. h->use_weight= 1;
  3681. }else{
  3682. h->luma_weight[list][i]= luma_def;
  3683. h->luma_offset[list][i]= 0;
  3684. }
  3685. chroma_weight_flag= get_bits1(&s->gb);
  3686. if(chroma_weight_flag){
  3687. int j;
  3688. for(j=0; j<2; j++){
  3689. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  3690. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  3691. if( h->chroma_weight[list][i][j] != chroma_def
  3692. || h->chroma_offset[list][i][j] != 0)
  3693. h->use_weight_chroma= 1;
  3694. }
  3695. }else{
  3696. int j;
  3697. for(j=0; j<2; j++){
  3698. h->chroma_weight[list][i][j]= chroma_def;
  3699. h->chroma_offset[list][i][j]= 0;
  3700. }
  3701. }
  3702. }
  3703. if(h->slice_type != B_TYPE) break;
  3704. }
  3705. h->use_weight= h->use_weight || h->use_weight_chroma;
  3706. return 0;
  3707. }
  3708. static void implicit_weight_table(H264Context *h){
  3709. MpegEncContext * const s = &h->s;
  3710. int ref0, ref1;
  3711. int cur_poc = s->current_picture_ptr->poc;
  3712. if( h->ref_count[0] == 1 && h->ref_count[1] == 1
  3713. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  3714. h->use_weight= 0;
  3715. h->use_weight_chroma= 0;
  3716. return;
  3717. }
  3718. h->use_weight= 2;
  3719. h->use_weight_chroma= 2;
  3720. h->luma_log2_weight_denom= 5;
  3721. h->chroma_log2_weight_denom= 5;
  3722. for(ref0=0; ref0 < h->ref_count[0]; ref0++){
  3723. int poc0 = h->ref_list[0][ref0].poc;
  3724. for(ref1=0; ref1 < h->ref_count[1]; ref1++){
  3725. int poc1 = h->ref_list[1][ref1].poc;
  3726. int td = clip(poc1 - poc0, -128, 127);
  3727. if(td){
  3728. int tb = clip(cur_poc - poc0, -128, 127);
  3729. int tx = (16384 + (ABS(td) >> 1)) / td;
  3730. int dist_scale_factor = clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
  3731. if(dist_scale_factor < -64 || dist_scale_factor > 128)
  3732. h->implicit_weight[ref0][ref1] = 32;
  3733. else
  3734. h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
  3735. }else
  3736. h->implicit_weight[ref0][ref1] = 32;
  3737. }
  3738. }
  3739. }
  3740. static inline void unreference_pic(H264Context *h, Picture *pic){
  3741. int i;
  3742. pic->reference=0;
  3743. if(pic == h->delayed_output_pic)
  3744. pic->reference=1;
  3745. else{
  3746. for(i = 0; h->delayed_pic[i]; i++)
  3747. if(pic == h->delayed_pic[i]){
  3748. pic->reference=1;
  3749. break;
  3750. }
  3751. }
  3752. }
  3753. /**
  3754. * instantaneous decoder refresh.
  3755. */
  3756. static void idr(H264Context *h){
  3757. int i;
  3758. for(i=0; i<16; i++){
  3759. if (h->long_ref[i] != NULL) {
  3760. unreference_pic(h, h->long_ref[i]);
  3761. h->long_ref[i]= NULL;
  3762. }
  3763. }
  3764. h->long_ref_count=0;
  3765. for(i=0; i<h->short_ref_count; i++){
  3766. unreference_pic(h, h->short_ref[i]);
  3767. h->short_ref[i]= NULL;
  3768. }
  3769. h->short_ref_count=0;
  3770. }
  3771. /* forget old pics after a seek */
  3772. static void flush_dpb(AVCodecContext *avctx){
  3773. H264Context *h= avctx->priv_data;
  3774. int i;
  3775. for(i=0; i<16; i++) {
  3776. if(h->delayed_pic[i])
  3777. h->delayed_pic[i]->reference= 0;
  3778. h->delayed_pic[i]= NULL;
  3779. }
  3780. if(h->delayed_output_pic)
  3781. h->delayed_output_pic->reference= 0;
  3782. h->delayed_output_pic= NULL;
  3783. idr(h);
  3784. if(h->s.current_picture_ptr)
  3785. h->s.current_picture_ptr->reference= 0;
  3786. }
  3787. /**
  3788. *
  3789. * @return the removed picture or NULL if an error occurs
  3790. */
  3791. static Picture * remove_short(H264Context *h, int frame_num){
  3792. MpegEncContext * const s = &h->s;
  3793. int i;
  3794. if(s->avctx->debug&FF_DEBUG_MMCO)
  3795. av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  3796. for(i=0; i<h->short_ref_count; i++){
  3797. Picture *pic= h->short_ref[i];
  3798. if(s->avctx->debug&FF_DEBUG_MMCO)
  3799. av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  3800. if(pic->frame_num == frame_num){
  3801. h->short_ref[i]= NULL;
  3802. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
  3803. h->short_ref_count--;
  3804. return pic;
  3805. }
  3806. }
  3807. return NULL;
  3808. }
  3809. /**
  3810. *
  3811. * @return the removed picture or NULL if an error occurs
  3812. */
  3813. static Picture * remove_long(H264Context *h, int i){
  3814. Picture *pic;
  3815. pic= h->long_ref[i];
  3816. h->long_ref[i]= NULL;
  3817. if(pic) h->long_ref_count--;
  3818. return pic;
  3819. }
  3820. /**
  3821. * print short term list
  3822. */
  3823. static void print_short_term(H264Context *h) {
  3824. uint32_t i;
  3825. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3826. av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
  3827. for(i=0; i<h->short_ref_count; i++){
  3828. Picture *pic= h->short_ref[i];
  3829. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3830. }
  3831. }
  3832. }
  3833. /**
  3834. * print long term list
  3835. */
  3836. static void print_long_term(H264Context *h) {
  3837. uint32_t i;
  3838. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3839. av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
  3840. for(i = 0; i < 16; i++){
  3841. Picture *pic= h->long_ref[i];
  3842. if (pic) {
  3843. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3844. }
  3845. }
  3846. }
  3847. }
  3848. /**
  3849. * Executes the reference picture marking (memory management control operations).
  3850. */
  3851. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  3852. MpegEncContext * const s = &h->s;
  3853. int i, j;
  3854. int current_is_long=0;
  3855. Picture *pic;
  3856. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  3857. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  3858. for(i=0; i<mmco_count; i++){
  3859. if(s->avctx->debug&FF_DEBUG_MMCO)
  3860. 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);
  3861. switch(mmco[i].opcode){
  3862. case MMCO_SHORT2UNUSED:
  3863. pic= remove_short(h, mmco[i].short_frame_num);
  3864. if(pic)
  3865. unreference_pic(h, pic);
  3866. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3867. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_short() failure\n");
  3868. break;
  3869. case MMCO_SHORT2LONG:
  3870. pic= remove_long(h, mmco[i].long_index);
  3871. if(pic) unreference_pic(h, pic);
  3872. h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
  3873. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3874. h->long_ref_count++;
  3875. break;
  3876. case MMCO_LONG2UNUSED:
  3877. pic= remove_long(h, mmco[i].long_index);
  3878. if(pic)
  3879. unreference_pic(h, pic);
  3880. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3881. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_long() failure\n");
  3882. break;
  3883. case MMCO_LONG:
  3884. pic= remove_long(h, mmco[i].long_index);
  3885. if(pic) unreference_pic(h, pic);
  3886. h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
  3887. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3888. h->long_ref_count++;
  3889. current_is_long=1;
  3890. break;
  3891. case MMCO_SET_MAX_LONG:
  3892. assert(mmco[i].long_index <= 16);
  3893. // just remove the long term which index is greater than new max
  3894. for(j = mmco[i].long_index; j<16; j++){
  3895. pic = remove_long(h, j);
  3896. if (pic) unreference_pic(h, pic);
  3897. }
  3898. break;
  3899. case MMCO_RESET:
  3900. while(h->short_ref_count){
  3901. pic= remove_short(h, h->short_ref[0]->frame_num);
  3902. unreference_pic(h, pic);
  3903. }
  3904. for(j = 0; j < 16; j++) {
  3905. pic= remove_long(h, j);
  3906. if(pic) unreference_pic(h, pic);
  3907. }
  3908. break;
  3909. default: assert(0);
  3910. }
  3911. }
  3912. if(!current_is_long){
  3913. pic= remove_short(h, s->current_picture_ptr->frame_num);
  3914. if(pic){
  3915. unreference_pic(h, pic);
  3916. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  3917. }
  3918. if(h->short_ref_count)
  3919. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  3920. h->short_ref[0]= s->current_picture_ptr;
  3921. h->short_ref[0]->long_ref=0;
  3922. h->short_ref_count++;
  3923. }
  3924. print_short_term(h);
  3925. print_long_term(h);
  3926. return 0;
  3927. }
  3928. static int decode_ref_pic_marking(H264Context *h){
  3929. MpegEncContext * const s = &h->s;
  3930. int i;
  3931. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  3932. s->broken_link= get_bits1(&s->gb) -1;
  3933. h->mmco[0].long_index= get_bits1(&s->gb) - 1; // current_long_term_idx
  3934. if(h->mmco[0].long_index == -1)
  3935. h->mmco_index= 0;
  3936. else{
  3937. h->mmco[0].opcode= MMCO_LONG;
  3938. h->mmco_index= 1;
  3939. }
  3940. }else{
  3941. if(get_bits1(&s->gb)){ // adaptive_ref_pic_marking_mode_flag
  3942. for(i= 0; i<MAX_MMCO_COUNT; i++) {
  3943. MMCOOpcode opcode= get_ue_golomb(&s->gb);;
  3944. h->mmco[i].opcode= opcode;
  3945. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  3946. 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
  3947. /* if(h->mmco[i].short_frame_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_frame_num ] == NULL){
  3948. av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
  3949. return -1;
  3950. }*/
  3951. }
  3952. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  3953. h->mmco[i].long_index= get_ue_golomb(&s->gb);
  3954. 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){
  3955. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  3956. return -1;
  3957. }
  3958. }
  3959. if(opcode > MMCO_LONG){
  3960. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  3961. return -1;
  3962. }
  3963. if(opcode == MMCO_END)
  3964. break;
  3965. }
  3966. h->mmco_index= i;
  3967. }else{
  3968. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  3969. if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){ //FIXME fields
  3970. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  3971. h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  3972. h->mmco_index= 1;
  3973. }else
  3974. h->mmco_index= 0;
  3975. }
  3976. }
  3977. return 0;
  3978. }
  3979. static int init_poc(H264Context *h){
  3980. MpegEncContext * const s = &h->s;
  3981. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  3982. int field_poc[2];
  3983. if(h->nal_unit_type == NAL_IDR_SLICE){
  3984. h->frame_num_offset= 0;
  3985. }else{
  3986. if(h->frame_num < h->prev_frame_num)
  3987. h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
  3988. else
  3989. h->frame_num_offset= h->prev_frame_num_offset;
  3990. }
  3991. if(h->sps.poc_type==0){
  3992. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  3993. if(h->nal_unit_type == NAL_IDR_SLICE){
  3994. h->prev_poc_msb=
  3995. h->prev_poc_lsb= 0;
  3996. }
  3997. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  3998. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  3999. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  4000. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  4001. else
  4002. h->poc_msb = h->prev_poc_msb;
  4003. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  4004. field_poc[0] =
  4005. field_poc[1] = h->poc_msb + h->poc_lsb;
  4006. if(s->picture_structure == PICT_FRAME)
  4007. field_poc[1] += h->delta_poc_bottom;
  4008. }else if(h->sps.poc_type==1){
  4009. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  4010. int i;
  4011. if(h->sps.poc_cycle_length != 0)
  4012. abs_frame_num = h->frame_num_offset + h->frame_num;
  4013. else
  4014. abs_frame_num = 0;
  4015. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  4016. abs_frame_num--;
  4017. expected_delta_per_poc_cycle = 0;
  4018. for(i=0; i < h->sps.poc_cycle_length; i++)
  4019. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  4020. if(abs_frame_num > 0){
  4021. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  4022. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  4023. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  4024. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  4025. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  4026. } else
  4027. expectedpoc = 0;
  4028. if(h->nal_ref_idc == 0)
  4029. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  4030. field_poc[0] = expectedpoc + h->delta_poc[0];
  4031. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  4032. if(s->picture_structure == PICT_FRAME)
  4033. field_poc[1] += h->delta_poc[1];
  4034. }else{
  4035. int poc;
  4036. if(h->nal_unit_type == NAL_IDR_SLICE){
  4037. poc= 0;
  4038. }else{
  4039. if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
  4040. else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
  4041. }
  4042. field_poc[0]= poc;
  4043. field_poc[1]= poc;
  4044. }
  4045. if(s->picture_structure != PICT_BOTTOM_FIELD)
  4046. s->current_picture_ptr->field_poc[0]= field_poc[0];
  4047. if(s->picture_structure != PICT_TOP_FIELD)
  4048. s->current_picture_ptr->field_poc[1]= field_poc[1];
  4049. if(s->picture_structure == PICT_FRAME) // FIXME field pix?
  4050. s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]);
  4051. return 0;
  4052. }
  4053. /**
  4054. * decodes a slice header.
  4055. * this will allso call MPV_common_init() and frame_start() as needed
  4056. */
  4057. static int decode_slice_header(H264Context *h){
  4058. MpegEncContext * const s = &h->s;
  4059. int first_mb_in_slice, pps_id;
  4060. int num_ref_idx_active_override_flag;
  4061. static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
  4062. int slice_type;
  4063. int default_ref_list_done = 0;
  4064. s->current_picture.reference= h->nal_ref_idc != 0;
  4065. s->dropable= h->nal_ref_idc == 0;
  4066. first_mb_in_slice= get_ue_golomb(&s->gb);
  4067. slice_type= get_ue_golomb(&s->gb);
  4068. if(slice_type > 9){
  4069. 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);
  4070. return -1;
  4071. }
  4072. if(slice_type > 4){
  4073. slice_type -= 5;
  4074. h->slice_type_fixed=1;
  4075. }else
  4076. h->slice_type_fixed=0;
  4077. slice_type= slice_type_map[ slice_type ];
  4078. if (slice_type == I_TYPE
  4079. || (h->slice_num != 0 && slice_type == h->slice_type) ) {
  4080. default_ref_list_done = 1;
  4081. }
  4082. h->slice_type= slice_type;
  4083. s->pict_type= h->slice_type; // to make a few old func happy, it's wrong though
  4084. pps_id= get_ue_golomb(&s->gb);
  4085. if(pps_id>255){
  4086. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  4087. return -1;
  4088. }
  4089. h->pps= h->pps_buffer[pps_id];
  4090. if(h->pps.slice_group_count == 0){
  4091. av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referenced\n");
  4092. return -1;
  4093. }
  4094. h->sps= h->sps_buffer[ h->pps.sps_id ];
  4095. if(h->sps.log2_max_frame_num == 0){
  4096. av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referenced\n");
  4097. return -1;
  4098. }
  4099. if(h->dequant_coeff_pps != pps_id){
  4100. h->dequant_coeff_pps = pps_id;
  4101. init_dequant_tables(h);
  4102. }
  4103. s->mb_width= h->sps.mb_width;
  4104. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  4105. h->b_stride= s->mb_width*4;
  4106. h->b8_stride= s->mb_width*2;
  4107. s->width = 16*s->mb_width - 2*(h->sps.crop_left + h->sps.crop_right );
  4108. if(h->sps.frame_mbs_only_flag)
  4109. s->height= 16*s->mb_height - 2*(h->sps.crop_top + h->sps.crop_bottom);
  4110. else
  4111. s->height= 16*s->mb_height - 4*(h->sps.crop_top + h->sps.crop_bottom); //FIXME recheck
  4112. if (s->context_initialized
  4113. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  4114. free_tables(h);
  4115. MPV_common_end(s);
  4116. }
  4117. if (!s->context_initialized) {
  4118. if (MPV_common_init(s) < 0)
  4119. return -1;
  4120. if(s->dsp.h264_idct_add == ff_h264_idct_add_c){ //FIXME little ugly
  4121. memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
  4122. memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
  4123. }else{
  4124. int i;
  4125. for(i=0; i<16; i++){
  4126. #define T(x) (x>>2) | ((x<<2) & 0xF)
  4127. h->zigzag_scan[i] = T(zigzag_scan[i]);
  4128. h-> field_scan[i] = T( field_scan[i]);
  4129. #undef T
  4130. }
  4131. }
  4132. if(s->dsp.h264_idct8_add == ff_h264_idct8_add_c){
  4133. memcpy(h->zigzag_scan8x8, zigzag_scan8x8, 64*sizeof(uint8_t));
  4134. memcpy(h->zigzag_scan8x8_cavlc, zigzag_scan8x8_cavlc, 64*sizeof(uint8_t));
  4135. memcpy(h->field_scan8x8, field_scan8x8, 64*sizeof(uint8_t));
  4136. memcpy(h->field_scan8x8_cavlc, field_scan8x8_cavlc, 64*sizeof(uint8_t));
  4137. }else{
  4138. int i;
  4139. for(i=0; i<64; i++){
  4140. #define T(x) (x>>3) | ((x&7)<<3)
  4141. h->zigzag_scan8x8[i] = T(zigzag_scan8x8[i]);
  4142. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  4143. h->field_scan8x8[i] = T(field_scan8x8[i]);
  4144. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  4145. #undef T
  4146. }
  4147. }
  4148. if(h->sps.transform_bypass){ //FIXME same ugly
  4149. h->zigzag_scan_q0 = zigzag_scan;
  4150. h->zigzag_scan8x8_q0 = zigzag_scan8x8;
  4151. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  4152. h->field_scan_q0 = field_scan;
  4153. h->field_scan8x8_q0 = field_scan8x8;
  4154. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  4155. }else{
  4156. h->zigzag_scan_q0 = h->zigzag_scan;
  4157. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  4158. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  4159. h->field_scan_q0 = h->field_scan;
  4160. h->field_scan8x8_q0 = h->field_scan8x8;
  4161. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  4162. }
  4163. alloc_tables(h);
  4164. s->avctx->width = s->width;
  4165. s->avctx->height = s->height;
  4166. s->avctx->sample_aspect_ratio= h->sps.sar;
  4167. if(!s->avctx->sample_aspect_ratio.den)
  4168. s->avctx->sample_aspect_ratio.den = 1;
  4169. if(h->sps.timing_info_present_flag){
  4170. s->avctx->time_base= (AVRational){h->sps.num_units_in_tick * 2, h->sps.time_scale};
  4171. if(h->x264_build > 0 && h->x264_build < 44)
  4172. s->avctx->time_base.den *= 2;
  4173. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  4174. s->avctx->time_base.num, s->avctx->time_base.den, 1<<30);
  4175. }
  4176. }
  4177. if(h->slice_num == 0){
  4178. if(frame_start(h) < 0)
  4179. return -1;
  4180. }
  4181. s->current_picture_ptr->frame_num= //FIXME frame_num cleanup
  4182. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  4183. h->mb_mbaff = 0;
  4184. h->mb_aff_frame = 0;
  4185. if(h->sps.frame_mbs_only_flag){
  4186. s->picture_structure= PICT_FRAME;
  4187. }else{
  4188. if(get_bits1(&s->gb)) { //field_pic_flag
  4189. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  4190. av_log(h->s.avctx, AV_LOG_ERROR, "PAFF interlacing is not implemented\n");
  4191. } else {
  4192. s->picture_structure= PICT_FRAME;
  4193. h->mb_aff_frame = h->sps.mb_aff;
  4194. }
  4195. }
  4196. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  4197. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << h->mb_aff_frame;
  4198. if(s->mb_y >= s->mb_height){
  4199. return -1;
  4200. }
  4201. if(s->picture_structure==PICT_FRAME){
  4202. h->curr_pic_num= h->frame_num;
  4203. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  4204. }else{
  4205. h->curr_pic_num= 2*h->frame_num;
  4206. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  4207. }
  4208. if(h->nal_unit_type == NAL_IDR_SLICE){
  4209. get_ue_golomb(&s->gb); /* idr_pic_id */
  4210. }
  4211. if(h->sps.poc_type==0){
  4212. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  4213. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  4214. h->delta_poc_bottom= get_se_golomb(&s->gb);
  4215. }
  4216. }
  4217. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  4218. h->delta_poc[0]= get_se_golomb(&s->gb);
  4219. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  4220. h->delta_poc[1]= get_se_golomb(&s->gb);
  4221. }
  4222. init_poc(h);
  4223. if(h->pps.redundant_pic_cnt_present){
  4224. h->redundant_pic_count= get_ue_golomb(&s->gb);
  4225. }
  4226. //set defaults, might be overriden a few line later
  4227. h->ref_count[0]= h->pps.ref_count[0];
  4228. h->ref_count[1]= h->pps.ref_count[1];
  4229. if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){
  4230. if(h->slice_type == B_TYPE){
  4231. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  4232. if(h->sps.mb_aff && h->direct_spatial_mv_pred)
  4233. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + spatial direct mode is not implemented\n");
  4234. }
  4235. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  4236. if(num_ref_idx_active_override_flag){
  4237. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  4238. if(h->slice_type==B_TYPE)
  4239. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  4240. if(h->ref_count[0] > 32 || h->ref_count[1] > 32){
  4241. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  4242. return -1;
  4243. }
  4244. }
  4245. }
  4246. if(!default_ref_list_done){
  4247. fill_default_ref_list(h);
  4248. }
  4249. if(decode_ref_pic_list_reordering(h) < 0)
  4250. return -1;
  4251. if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
  4252. || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
  4253. pred_weight_table(h);
  4254. else if(h->pps.weighted_bipred_idc==2 && h->slice_type==B_TYPE)
  4255. implicit_weight_table(h);
  4256. else
  4257. h->use_weight = 0;
  4258. if(s->current_picture.reference)
  4259. decode_ref_pic_marking(h);
  4260. if(FRAME_MBAFF)
  4261. fill_mbaff_ref_list(h);
  4262. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac )
  4263. h->cabac_init_idc = get_ue_golomb(&s->gb);
  4264. h->last_qscale_diff = 0;
  4265. s->qscale = h->pps.init_qp + get_se_golomb(&s->gb);
  4266. if(s->qscale<0 || s->qscale>51){
  4267. av_log(s->avctx, AV_LOG_ERROR, "QP %d out of range\n", s->qscale);
  4268. return -1;
  4269. }
  4270. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4271. //FIXME qscale / qp ... stuff
  4272. if(h->slice_type == SP_TYPE){
  4273. get_bits1(&s->gb); /* sp_for_switch_flag */
  4274. }
  4275. if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
  4276. get_se_golomb(&s->gb); /* slice_qs_delta */
  4277. }
  4278. h->deblocking_filter = 1;
  4279. h->slice_alpha_c0_offset = 0;
  4280. h->slice_beta_offset = 0;
  4281. if( h->pps.deblocking_filter_parameters_present ) {
  4282. h->deblocking_filter= get_ue_golomb(&s->gb);
  4283. if(h->deblocking_filter < 2)
  4284. h->deblocking_filter^= 1; // 1<->0
  4285. if( h->deblocking_filter ) {
  4286. h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
  4287. h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
  4288. }
  4289. }
  4290. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  4291. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type != I_TYPE)
  4292. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type == B_TYPE)
  4293. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  4294. h->deblocking_filter= 0;
  4295. #if 0 //FMO
  4296. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  4297. slice_group_change_cycle= get_bits(&s->gb, ?);
  4298. #endif
  4299. h->slice_num++;
  4300. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
  4301. h->emu_edge_height= FRAME_MBAFF ? 0 : h->emu_edge_width;
  4302. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  4303. 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",
  4304. h->slice_num,
  4305. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  4306. first_mb_in_slice,
  4307. av_get_pict_type_char(h->slice_type),
  4308. pps_id, h->frame_num,
  4309. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  4310. h->ref_count[0], h->ref_count[1],
  4311. s->qscale,
  4312. h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2,
  4313. h->use_weight,
  4314. h->use_weight==1 && h->use_weight_chroma ? "c" : ""
  4315. );
  4316. }
  4317. return 0;
  4318. }
  4319. /**
  4320. *
  4321. */
  4322. static inline int get_level_prefix(GetBitContext *gb){
  4323. unsigned int buf;
  4324. int log;
  4325. OPEN_READER(re, gb);
  4326. UPDATE_CACHE(re, gb);
  4327. buf=GET_CACHE(re, gb);
  4328. log= 32 - av_log2(buf);
  4329. #ifdef TRACE
  4330. print_bin(buf>>(32-log), log);
  4331. 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__);
  4332. #endif
  4333. LAST_SKIP_BITS(re, gb, log);
  4334. CLOSE_READER(re, gb);
  4335. return log-1;
  4336. }
  4337. static inline int get_dct8x8_allowed(H264Context *h){
  4338. int i;
  4339. for(i=0; i<4; i++){
  4340. if(!IS_SUB_8X8(h->sub_mb_type[i])
  4341. || (!h->sps.direct_8x8_inference_flag && IS_DIRECT(h->sub_mb_type[i])))
  4342. return 0;
  4343. }
  4344. return 1;
  4345. }
  4346. /**
  4347. * decodes a residual block.
  4348. * @param n block index
  4349. * @param scantable scantable
  4350. * @param max_coeff number of coefficients in the block
  4351. * @return <0 if an error occured
  4352. */
  4353. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
  4354. MpegEncContext * const s = &h->s;
  4355. 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};
  4356. int level[16];
  4357. int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
  4358. //FIXME put trailing_onex into the context
  4359. if(n == CHROMA_DC_BLOCK_INDEX){
  4360. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  4361. total_coeff= coeff_token>>2;
  4362. }else{
  4363. if(n == LUMA_DC_BLOCK_INDEX){
  4364. total_coeff= pred_non_zero_count(h, 0);
  4365. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4366. total_coeff= coeff_token>>2;
  4367. }else{
  4368. total_coeff= pred_non_zero_count(h, n);
  4369. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4370. total_coeff= coeff_token>>2;
  4371. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  4372. }
  4373. }
  4374. //FIXME set last_non_zero?
  4375. if(total_coeff==0)
  4376. return 0;
  4377. trailing_ones= coeff_token&3;
  4378. tprintf("trailing:%d, total:%d\n", trailing_ones, total_coeff);
  4379. assert(total_coeff<=16);
  4380. for(i=0; i<trailing_ones; i++){
  4381. level[i]= 1 - 2*get_bits1(gb);
  4382. }
  4383. if(i<total_coeff) {
  4384. int level_code, mask;
  4385. int suffix_length = total_coeff > 10 && trailing_ones < 3;
  4386. int prefix= get_level_prefix(gb);
  4387. //first coefficient has suffix_length equal to 0 or 1
  4388. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  4389. if(suffix_length)
  4390. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4391. else
  4392. level_code= (prefix<<suffix_length); //part
  4393. }else if(prefix==14){
  4394. if(suffix_length)
  4395. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4396. else
  4397. level_code= prefix + get_bits(gb, 4); //part
  4398. }else if(prefix==15){
  4399. level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part
  4400. if(suffix_length==0) level_code+=15; //FIXME doesn't make (much)sense
  4401. }else{
  4402. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4403. return -1;
  4404. }
  4405. if(trailing_ones < 3) level_code += 2;
  4406. suffix_length = 1;
  4407. if(level_code > 5)
  4408. suffix_length++;
  4409. mask= -(level_code&1);
  4410. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4411. i++;
  4412. //remaining coefficients have suffix_length > 0
  4413. for(;i<total_coeff;i++) {
  4414. static const int suffix_limit[7] = {0,5,11,23,47,95,INT_MAX };
  4415. prefix = get_level_prefix(gb);
  4416. if(prefix<15){
  4417. level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
  4418. }else if(prefix==15){
  4419. level_code = (prefix<<suffix_length) + get_bits(gb, 12);
  4420. }else{
  4421. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4422. return -1;
  4423. }
  4424. mask= -(level_code&1);
  4425. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4426. if(level_code > suffix_limit[suffix_length])
  4427. suffix_length++;
  4428. }
  4429. }
  4430. if(total_coeff == max_coeff)
  4431. zeros_left=0;
  4432. else{
  4433. if(n == CHROMA_DC_BLOCK_INDEX)
  4434. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  4435. else
  4436. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  4437. }
  4438. coeff_num = zeros_left + total_coeff - 1;
  4439. j = scantable[coeff_num];
  4440. if(n > 24){
  4441. block[j] = level[0];
  4442. for(i=1;i<total_coeff;i++) {
  4443. if(zeros_left <= 0)
  4444. run_before = 0;
  4445. else if(zeros_left < 7){
  4446. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4447. }else{
  4448. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4449. }
  4450. zeros_left -= run_before;
  4451. coeff_num -= 1 + run_before;
  4452. j= scantable[ coeff_num ];
  4453. block[j]= level[i];
  4454. }
  4455. }else{
  4456. block[j] = (level[0] * qmul[j] + 32)>>6;
  4457. for(i=1;i<total_coeff;i++) {
  4458. if(zeros_left <= 0)
  4459. run_before = 0;
  4460. else if(zeros_left < 7){
  4461. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4462. }else{
  4463. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4464. }
  4465. zeros_left -= run_before;
  4466. coeff_num -= 1 + run_before;
  4467. j= scantable[ coeff_num ];
  4468. block[j]= (level[i] * qmul[j] + 32)>>6;
  4469. }
  4470. }
  4471. if(zeros_left<0){
  4472. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  4473. return -1;
  4474. }
  4475. return 0;
  4476. }
  4477. static void predict_field_decoding_flag(H264Context *h){
  4478. MpegEncContext * const s = &h->s;
  4479. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4480. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  4481. ? s->current_picture.mb_type[mb_xy-1]
  4482. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  4483. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  4484. : 0;
  4485. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  4486. }
  4487. /**
  4488. * decodes a P_SKIP or B_SKIP macroblock
  4489. */
  4490. static void decode_mb_skip(H264Context *h){
  4491. MpegEncContext * const s = &h->s;
  4492. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4493. int mb_type=0;
  4494. memset(h->non_zero_count[mb_xy], 0, 16);
  4495. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  4496. if(MB_FIELD)
  4497. mb_type|= MB_TYPE_INTERLACED;
  4498. if( h->slice_type == B_TYPE )
  4499. {
  4500. // just for fill_caches. pred_direct_motion will set the real mb_type
  4501. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
  4502. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4503. pred_direct_motion(h, &mb_type);
  4504. if(h->pps.cabac){
  4505. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  4506. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  4507. }
  4508. }
  4509. else
  4510. {
  4511. int mx, my;
  4512. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
  4513. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4514. pred_pskip_motion(h, &mx, &my);
  4515. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  4516. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  4517. if(h->pps.cabac)
  4518. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  4519. }
  4520. write_back_motion(h, mb_type);
  4521. s->current_picture.mb_type[mb_xy]= mb_type|MB_TYPE_SKIP;
  4522. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4523. h->slice_table[ mb_xy ]= h->slice_num;
  4524. h->prev_mb_skipped= 1;
  4525. }
  4526. /**
  4527. * decodes a macroblock
  4528. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  4529. */
  4530. static int decode_mb_cavlc(H264Context *h){
  4531. MpegEncContext * const s = &h->s;
  4532. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4533. int mb_type, partition_count, cbp;
  4534. int dct8x8_allowed= h->pps.transform_8x8_mode;
  4535. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?
  4536. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  4537. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  4538. down the code */
  4539. if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){
  4540. if(s->mb_skip_run==-1)
  4541. s->mb_skip_run= get_ue_golomb(&s->gb);
  4542. if (s->mb_skip_run--) {
  4543. if(FRAME_MBAFF && (s->mb_y&1) == 0){
  4544. if(s->mb_skip_run==0)
  4545. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4546. else
  4547. predict_field_decoding_flag(h);
  4548. }
  4549. decode_mb_skip(h);
  4550. return 0;
  4551. }
  4552. }
  4553. if(FRAME_MBAFF){
  4554. if( (s->mb_y&1) == 0 )
  4555. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4556. }else
  4557. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  4558. h->prev_mb_skipped= 0;
  4559. mb_type= get_ue_golomb(&s->gb);
  4560. if(h->slice_type == B_TYPE){
  4561. if(mb_type < 23){
  4562. partition_count= b_mb_type_info[mb_type].partition_count;
  4563. mb_type= b_mb_type_info[mb_type].type;
  4564. }else{
  4565. mb_type -= 23;
  4566. goto decode_intra_mb;
  4567. }
  4568. }else if(h->slice_type == P_TYPE /*|| h->slice_type == SP_TYPE */){
  4569. if(mb_type < 5){
  4570. partition_count= p_mb_type_info[mb_type].partition_count;
  4571. mb_type= p_mb_type_info[mb_type].type;
  4572. }else{
  4573. mb_type -= 5;
  4574. goto decode_intra_mb;
  4575. }
  4576. }else{
  4577. assert(h->slice_type == I_TYPE);
  4578. decode_intra_mb:
  4579. if(mb_type > 25){
  4580. av_log(h->s.avctx, AV_LOG_ERROR, "mb_type %d in %c slice to large at %d %d\n", mb_type, av_get_pict_type_char(h->slice_type), s->mb_x, s->mb_y);
  4581. return -1;
  4582. }
  4583. partition_count=0;
  4584. cbp= i_mb_type_info[mb_type].cbp;
  4585. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  4586. mb_type= i_mb_type_info[mb_type].type;
  4587. }
  4588. if(MB_FIELD)
  4589. mb_type |= MB_TYPE_INTERLACED;
  4590. h->slice_table[ mb_xy ]= h->slice_num;
  4591. if(IS_INTRA_PCM(mb_type)){
  4592. unsigned int x, y;
  4593. // we assume these blocks are very rare so we dont optimize it
  4594. align_get_bits(&s->gb);
  4595. // The pixels are stored in the same order as levels in h->mb array.
  4596. for(y=0; y<16; y++){
  4597. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  4598. for(x=0; x<16; x++){
  4599. tprintf("LUMA ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4600. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= get_bits(&s->gb, 8);
  4601. }
  4602. }
  4603. for(y=0; y<8; y++){
  4604. const int index= 256 + 4*(y&3) + 32*(y>>2);
  4605. for(x=0; x<8; x++){
  4606. tprintf("CHROMA U ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4607. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4608. }
  4609. }
  4610. for(y=0; y<8; y++){
  4611. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  4612. for(x=0; x<8; x++){
  4613. tprintf("CHROMA V ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4614. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4615. }
  4616. }
  4617. // In deblocking, the quantizer is 0
  4618. s->current_picture.qscale_table[mb_xy]= 0;
  4619. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  4620. // All coeffs are present
  4621. memset(h->non_zero_count[mb_xy], 16, 16);
  4622. s->current_picture.mb_type[mb_xy]= mb_type;
  4623. return 0;
  4624. }
  4625. if(MB_MBAFF){
  4626. h->ref_count[0] <<= 1;
  4627. h->ref_count[1] <<= 1;
  4628. }
  4629. fill_caches(h, mb_type, 0);
  4630. //mb_pred
  4631. if(IS_INTRA(mb_type)){
  4632. // init_top_left_availability(h);
  4633. if(IS_INTRA4x4(mb_type)){
  4634. int i;
  4635. int di = 1;
  4636. if(dct8x8_allowed && get_bits1(&s->gb)){
  4637. mb_type |= MB_TYPE_8x8DCT;
  4638. di = 4;
  4639. }
  4640. // fill_intra4x4_pred_table(h);
  4641. for(i=0; i<16; i+=di){
  4642. int mode= pred_intra_mode(h, i);
  4643. if(!get_bits1(&s->gb)){
  4644. const int rem_mode= get_bits(&s->gb, 3);
  4645. mode = rem_mode + (rem_mode >= mode);
  4646. }
  4647. if(di==4)
  4648. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  4649. else
  4650. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  4651. }
  4652. write_back_intra_pred_mode(h);
  4653. if( check_intra4x4_pred_mode(h) < 0)
  4654. return -1;
  4655. }else{
  4656. h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
  4657. if(h->intra16x16_pred_mode < 0)
  4658. return -1;
  4659. }
  4660. h->chroma_pred_mode= get_ue_golomb(&s->gb);
  4661. h->chroma_pred_mode= check_intra_pred_mode(h, h->chroma_pred_mode);
  4662. if(h->chroma_pred_mode < 0)
  4663. return -1;
  4664. }else if(partition_count==4){
  4665. int i, j, sub_partition_count[4], list, ref[2][4];
  4666. if(h->slice_type == B_TYPE){
  4667. for(i=0; i<4; i++){
  4668. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4669. if(h->sub_mb_type[i] >=13){
  4670. 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);
  4671. return -1;
  4672. }
  4673. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4674. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4675. }
  4676. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  4677. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
  4678. pred_direct_motion(h, &mb_type);
  4679. h->ref_cache[0][scan8[4]] =
  4680. h->ref_cache[1][scan8[4]] =
  4681. h->ref_cache[0][scan8[12]] =
  4682. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  4683. }
  4684. }else{
  4685. assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE); //FIXME SP correct ?
  4686. for(i=0; i<4; i++){
  4687. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4688. if(h->sub_mb_type[i] >=4){
  4689. 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);
  4690. return -1;
  4691. }
  4692. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4693. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4694. }
  4695. }
  4696. for(list=0; list<2; list++){
  4697. int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4698. if(ref_count == 0) continue;
  4699. for(i=0; i<4; i++){
  4700. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  4701. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4702. ref[list][i] = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
  4703. }else{
  4704. //FIXME
  4705. ref[list][i] = -1;
  4706. }
  4707. }
  4708. }
  4709. if(dct8x8_allowed)
  4710. dct8x8_allowed = get_dct8x8_allowed(h);
  4711. for(list=0; list<2; list++){
  4712. const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4713. if(ref_count == 0) continue;
  4714. for(i=0; i<4; i++){
  4715. if(IS_DIRECT(h->sub_mb_type[i])) {
  4716. h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];
  4717. continue;
  4718. }
  4719. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  4720. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  4721. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4722. const int sub_mb_type= h->sub_mb_type[i];
  4723. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  4724. for(j=0; j<sub_partition_count[i]; j++){
  4725. int mx, my;
  4726. const int index= 4*i + block_width*j;
  4727. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  4728. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  4729. mx += get_se_golomb(&s->gb);
  4730. my += get_se_golomb(&s->gb);
  4731. tprintf("final mv:%d %d\n", mx, my);
  4732. if(IS_SUB_8X8(sub_mb_type)){
  4733. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  4734. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  4735. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  4736. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  4737. }else if(IS_SUB_8X4(sub_mb_type)){
  4738. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  4739. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  4740. }else if(IS_SUB_4X8(sub_mb_type)){
  4741. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  4742. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  4743. }else{
  4744. assert(IS_SUB_4X4(sub_mb_type));
  4745. mv_cache[ 0 ][0]= mx;
  4746. mv_cache[ 0 ][1]= my;
  4747. }
  4748. }
  4749. }else{
  4750. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  4751. p[0] = p[1]=
  4752. p[8] = p[9]= 0;
  4753. }
  4754. }
  4755. }
  4756. }else if(IS_DIRECT(mb_type)){
  4757. pred_direct_motion(h, &mb_type);
  4758. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  4759. }else{
  4760. int list, mx, my, i;
  4761. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  4762. if(IS_16X16(mb_type)){
  4763. for(list=0; list<2; list++){
  4764. if(h->ref_count[list]>0){
  4765. if(IS_DIR(mb_type, 0, list)){
  4766. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4767. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  4768. }else
  4769. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4770. }
  4771. }
  4772. for(list=0; list<2; list++){
  4773. if(IS_DIR(mb_type, 0, list)){
  4774. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  4775. mx += get_se_golomb(&s->gb);
  4776. my += get_se_golomb(&s->gb);
  4777. tprintf("final mv:%d %d\n", mx, my);
  4778. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  4779. }else
  4780. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  4781. }
  4782. }
  4783. else if(IS_16X8(mb_type)){
  4784. for(list=0; list<2; list++){
  4785. if(h->ref_count[list]>0){
  4786. for(i=0; i<2; i++){
  4787. if(IS_DIR(mb_type, i, list)){
  4788. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4789. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  4790. }else
  4791. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  4792. }
  4793. }
  4794. }
  4795. for(list=0; list<2; list++){
  4796. for(i=0; i<2; i++){
  4797. if(IS_DIR(mb_type, i, list)){
  4798. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  4799. mx += get_se_golomb(&s->gb);
  4800. my += get_se_golomb(&s->gb);
  4801. tprintf("final mv:%d %d\n", mx, my);
  4802. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  4803. }else
  4804. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  4805. }
  4806. }
  4807. }else{
  4808. assert(IS_8X16(mb_type));
  4809. for(list=0; list<2; list++){
  4810. if(h->ref_count[list]>0){
  4811. for(i=0; i<2; i++){
  4812. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  4813. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4814. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  4815. }else
  4816. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4817. }
  4818. }
  4819. }
  4820. for(list=0; list<2; list++){
  4821. for(i=0; i<2; i++){
  4822. if(IS_DIR(mb_type, i, list)){
  4823. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  4824. mx += get_se_golomb(&s->gb);
  4825. my += get_se_golomb(&s->gb);
  4826. tprintf("final mv:%d %d\n", mx, my);
  4827. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  4828. }else
  4829. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  4830. }
  4831. }
  4832. }
  4833. }
  4834. if(IS_INTER(mb_type))
  4835. write_back_motion(h, mb_type);
  4836. if(!IS_INTRA16x16(mb_type)){
  4837. cbp= get_ue_golomb(&s->gb);
  4838. if(cbp > 47){
  4839. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%d) at %d %d\n", cbp, s->mb_x, s->mb_y);
  4840. return -1;
  4841. }
  4842. if(IS_INTRA4x4(mb_type))
  4843. cbp= golomb_to_intra4x4_cbp[cbp];
  4844. else
  4845. cbp= golomb_to_inter_cbp[cbp];
  4846. }
  4847. if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
  4848. if(get_bits1(&s->gb))
  4849. mb_type |= MB_TYPE_8x8DCT;
  4850. }
  4851. s->current_picture.mb_type[mb_xy]= mb_type;
  4852. if(cbp || IS_INTRA16x16(mb_type)){
  4853. int i8x8, i4x4, chroma_idx;
  4854. int chroma_qp, dquant;
  4855. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  4856. const uint8_t *scan, *scan8x8, *dc_scan;
  4857. // fill_non_zero_count_cache(h);
  4858. if(IS_INTERLACED(mb_type)){
  4859. scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
  4860. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  4861. dc_scan= luma_dc_field_scan;
  4862. }else{
  4863. scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
  4864. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  4865. dc_scan= luma_dc_zigzag_scan;
  4866. }
  4867. dquant= get_se_golomb(&s->gb);
  4868. if( dquant > 25 || dquant < -26 ){
  4869. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  4870. return -1;
  4871. }
  4872. s->qscale += dquant;
  4873. if(((unsigned)s->qscale) > 51){
  4874. if(s->qscale<0) s->qscale+= 52;
  4875. else s->qscale-= 52;
  4876. }
  4877. h->chroma_qp= chroma_qp= get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4878. if(IS_INTRA16x16(mb_type)){
  4879. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){
  4880. return -1; //FIXME continue if partitioned and other return -1 too
  4881. }
  4882. assert((cbp&15) == 0 || (cbp&15) == 15);
  4883. if(cbp&15){
  4884. for(i8x8=0; i8x8<4; i8x8++){
  4885. for(i4x4=0; i4x4<4; i4x4++){
  4886. const int index= i4x4 + 4*i8x8;
  4887. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){
  4888. return -1;
  4889. }
  4890. }
  4891. }
  4892. }else{
  4893. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  4894. }
  4895. }else{
  4896. for(i8x8=0; i8x8<4; i8x8++){
  4897. if(cbp & (1<<i8x8)){
  4898. if(IS_8x8DCT(mb_type)){
  4899. DCTELEM *buf = &h->mb[64*i8x8];
  4900. uint8_t *nnz;
  4901. for(i4x4=0; i4x4<4; i4x4++){
  4902. if( decode_residual(h, gb, buf, i4x4+4*i8x8, scan8x8+16*i4x4,
  4903. h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )
  4904. return -1;
  4905. }
  4906. nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4907. nnz[0] += nnz[1] + nnz[8] + nnz[9];
  4908. }else{
  4909. for(i4x4=0; i4x4<4; i4x4++){
  4910. const int index= i4x4 + 4*i8x8;
  4911. if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){
  4912. return -1;
  4913. }
  4914. }
  4915. }
  4916. }else{
  4917. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4918. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  4919. }
  4920. }
  4921. }
  4922. if(cbp&0x30){
  4923. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  4924. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){
  4925. return -1;
  4926. }
  4927. }
  4928. if(cbp&0x20){
  4929. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  4930. for(i4x4=0; i4x4<4; i4x4++){
  4931. const int index= 16 + 4*chroma_idx + i4x4;
  4932. 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){
  4933. return -1;
  4934. }
  4935. }
  4936. }
  4937. }else{
  4938. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4939. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4940. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4941. }
  4942. }else{
  4943. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4944. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  4945. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4946. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4947. }
  4948. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4949. write_back_non_zero_count(h);
  4950. if(MB_MBAFF){
  4951. h->ref_count[0] >>= 1;
  4952. h->ref_count[1] >>= 1;
  4953. }
  4954. return 0;
  4955. }
  4956. static int decode_cabac_field_decoding_flag(H264Context *h) {
  4957. MpegEncContext * const s = &h->s;
  4958. const int mb_x = s->mb_x;
  4959. const int mb_y = s->mb_y & ~1;
  4960. const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
  4961. const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
  4962. unsigned int ctx = 0;
  4963. if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
  4964. ctx += 1;
  4965. }
  4966. if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
  4967. ctx += 1;
  4968. }
  4969. return get_cabac( &h->cabac, &h->cabac_state[70 + ctx] );
  4970. }
  4971. static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
  4972. uint8_t *state= &h->cabac_state[ctx_base];
  4973. int mb_type;
  4974. if(intra_slice){
  4975. MpegEncContext * const s = &h->s;
  4976. const int mba_xy = h->left_mb_xy[0];
  4977. const int mbb_xy = h->top_mb_xy;
  4978. int ctx=0;
  4979. if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
  4980. ctx++;
  4981. if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
  4982. ctx++;
  4983. if( get_cabac( &h->cabac, &state[ctx] ) == 0 )
  4984. return 0; /* I4x4 */
  4985. state += 2;
  4986. }else{
  4987. if( get_cabac( &h->cabac, &state[0] ) == 0 )
  4988. return 0; /* I4x4 */
  4989. }
  4990. if( get_cabac_terminate( &h->cabac ) )
  4991. return 25; /* PCM */
  4992. mb_type = 1; /* I16x16 */
  4993. mb_type += 12 * get_cabac( &h->cabac, &state[1] ); /* cbp_luma != 0 */
  4994. if( get_cabac( &h->cabac, &state[2] ) ) /* cbp_chroma */
  4995. mb_type += 4 + 4 * get_cabac( &h->cabac, &state[2+intra_slice] );
  4996. mb_type += 2 * get_cabac( &h->cabac, &state[3+intra_slice] );
  4997. mb_type += 1 * get_cabac( &h->cabac, &state[3+2*intra_slice] );
  4998. return mb_type;
  4999. }
  5000. static int decode_cabac_mb_type( H264Context *h ) {
  5001. MpegEncContext * const s = &h->s;
  5002. if( h->slice_type == I_TYPE ) {
  5003. return decode_cabac_intra_mb_type(h, 3, 1);
  5004. } else if( h->slice_type == P_TYPE ) {
  5005. if( get_cabac( &h->cabac, &h->cabac_state[14] ) == 0 ) {
  5006. /* P-type */
  5007. if( get_cabac( &h->cabac, &h->cabac_state[15] ) == 0 ) {
  5008. /* P_L0_D16x16, P_8x8 */
  5009. return 3 * get_cabac( &h->cabac, &h->cabac_state[16] );
  5010. } else {
  5011. /* P_L0_D8x16, P_L0_D16x8 */
  5012. return 2 - get_cabac( &h->cabac, &h->cabac_state[17] );
  5013. }
  5014. } else {
  5015. return decode_cabac_intra_mb_type(h, 17, 0) + 5;
  5016. }
  5017. } else if( h->slice_type == B_TYPE ) {
  5018. const int mba_xy = h->left_mb_xy[0];
  5019. const int mbb_xy = h->top_mb_xy;
  5020. int ctx = 0;
  5021. int bits;
  5022. if( h->slice_table[mba_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
  5023. ctx++;
  5024. if( h->slice_table[mbb_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
  5025. ctx++;
  5026. if( !get_cabac( &h->cabac, &h->cabac_state[27+ctx] ) )
  5027. return 0; /* B_Direct_16x16 */
  5028. if( !get_cabac( &h->cabac, &h->cabac_state[27+3] ) ) {
  5029. return 1 + get_cabac( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
  5030. }
  5031. bits = get_cabac( &h->cabac, &h->cabac_state[27+4] ) << 3;
  5032. bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] ) << 2;
  5033. bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] ) << 1;
  5034. bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] );
  5035. if( bits < 8 )
  5036. return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
  5037. else if( bits == 13 ) {
  5038. return decode_cabac_intra_mb_type(h, 32, 0) + 23;
  5039. } else if( bits == 14 )
  5040. return 11; /* B_L1_L0_8x16 */
  5041. else if( bits == 15 )
  5042. return 22; /* B_8x8 */
  5043. bits= ( bits<<1 ) | get_cabac( &h->cabac, &h->cabac_state[27+5] );
  5044. return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
  5045. } else {
  5046. /* TODO SI/SP frames? */
  5047. return -1;
  5048. }
  5049. }
  5050. static int decode_cabac_mb_skip( H264Context *h, int mb_x, int mb_y ) {
  5051. MpegEncContext * const s = &h->s;
  5052. int mba_xy, mbb_xy;
  5053. int ctx = 0;
  5054. if(FRAME_MBAFF){ //FIXME merge with the stuff in fill_caches?
  5055. int mb_xy = mb_x + (mb_y&~1)*s->mb_stride;
  5056. mba_xy = mb_xy - 1;
  5057. if( (mb_y&1)
  5058. && h->slice_table[mba_xy] == h->slice_num
  5059. && MB_FIELD == !!IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) )
  5060. mba_xy += s->mb_stride;
  5061. if( MB_FIELD ){
  5062. mbb_xy = mb_xy - s->mb_stride;
  5063. if( !(mb_y&1)
  5064. && h->slice_table[mbb_xy] == h->slice_num
  5065. && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) )
  5066. mbb_xy -= s->mb_stride;
  5067. }else
  5068. mbb_xy = mb_x + (mb_y-1)*s->mb_stride;
  5069. }else{
  5070. int mb_xy = mb_x + mb_y*s->mb_stride;
  5071. mba_xy = mb_xy - 1;
  5072. mbb_xy = mb_xy - s->mb_stride;
  5073. }
  5074. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
  5075. ctx++;
  5076. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
  5077. ctx++;
  5078. if( h->slice_type == B_TYPE )
  5079. ctx += 13;
  5080. return get_cabac( &h->cabac, &h->cabac_state[11+ctx] );
  5081. }
  5082. static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
  5083. int mode = 0;
  5084. if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
  5085. return pred_mode;
  5086. mode += 1 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5087. mode += 2 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5088. mode += 4 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5089. if( mode >= pred_mode )
  5090. return mode + 1;
  5091. else
  5092. return mode;
  5093. }
  5094. static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
  5095. const int mba_xy = h->left_mb_xy[0];
  5096. const int mbb_xy = h->top_mb_xy;
  5097. int ctx = 0;
  5098. /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
  5099. if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
  5100. ctx++;
  5101. if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
  5102. ctx++;
  5103. if( get_cabac( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
  5104. return 0;
  5105. if( get_cabac( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  5106. return 1;
  5107. if( get_cabac( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  5108. return 2;
  5109. else
  5110. return 3;
  5111. }
  5112. static const uint8_t block_idx_x[16] = {
  5113. 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
  5114. };
  5115. static const uint8_t block_idx_y[16] = {
  5116. 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
  5117. };
  5118. static const uint8_t block_idx_xy[4][4] = {
  5119. { 0, 2, 8, 10},
  5120. { 1, 3, 9, 11},
  5121. { 4, 6, 12, 14},
  5122. { 5, 7, 13, 15}
  5123. };
  5124. static int decode_cabac_mb_cbp_luma( H264Context *h) {
  5125. int cbp = 0;
  5126. int cbp_b = -1;
  5127. int i8x8;
  5128. if( h->slice_table[h->top_mb_xy] == h->slice_num ) {
  5129. cbp_b = h->top_cbp;
  5130. tprintf("cbp_b = top_cbp = %x\n", cbp_b);
  5131. }
  5132. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5133. int cbp_a = -1;
  5134. int x, y;
  5135. int ctx = 0;
  5136. x = block_idx_x[4*i8x8];
  5137. y = block_idx_y[4*i8x8];
  5138. if( x > 0 )
  5139. cbp_a = cbp;
  5140. else if( h->slice_table[h->left_mb_xy[0]] == h->slice_num ) {
  5141. cbp_a = h->left_cbp;
  5142. tprintf("cbp_a = left_cbp = %x\n", cbp_a);
  5143. }
  5144. if( y > 0 )
  5145. cbp_b = cbp;
  5146. /* No need to test for skip as we put 0 for skip block */
  5147. /* No need to test for IPCM as we put 1 for IPCM block */
  5148. if( cbp_a >= 0 ) {
  5149. int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
  5150. if( ((cbp_a >> i8x8a)&0x01) == 0 )
  5151. ctx++;
  5152. }
  5153. if( cbp_b >= 0 ) {
  5154. int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
  5155. if( ((cbp_b >> i8x8b)&0x01) == 0 )
  5156. ctx += 2;
  5157. }
  5158. if( get_cabac( &h->cabac, &h->cabac_state[73 + ctx] ) ) {
  5159. cbp |= 1 << i8x8;
  5160. }
  5161. }
  5162. return cbp;
  5163. }
  5164. static int decode_cabac_mb_cbp_chroma( H264Context *h) {
  5165. int ctx;
  5166. int cbp_a, cbp_b;
  5167. cbp_a = (h->left_cbp>>4)&0x03;
  5168. cbp_b = (h-> top_cbp>>4)&0x03;
  5169. ctx = 0;
  5170. if( cbp_a > 0 ) ctx++;
  5171. if( cbp_b > 0 ) ctx += 2;
  5172. if( get_cabac( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
  5173. return 0;
  5174. ctx = 4;
  5175. if( cbp_a == 2 ) ctx++;
  5176. if( cbp_b == 2 ) ctx += 2;
  5177. return 1 + get_cabac( &h->cabac, &h->cabac_state[77 + ctx] );
  5178. }
  5179. static int decode_cabac_mb_dqp( H264Context *h) {
  5180. MpegEncContext * const s = &h->s;
  5181. int mbn_xy;
  5182. int ctx = 0;
  5183. int val = 0;
  5184. if( s->mb_x > 0 )
  5185. mbn_xy = s->mb_x + s->mb_y*s->mb_stride - 1;
  5186. else
  5187. mbn_xy = s->mb_width - 1 + (s->mb_y-1)*s->mb_stride;
  5188. if( h->last_qscale_diff != 0 )
  5189. ctx++;
  5190. while( get_cabac( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
  5191. if( ctx < 2 )
  5192. ctx = 2;
  5193. else
  5194. ctx = 3;
  5195. val++;
  5196. if(val > 102) //prevent infinite loop
  5197. return INT_MIN;
  5198. }
  5199. if( val&0x01 )
  5200. return (val + 1)/2;
  5201. else
  5202. return -(val + 1)/2;
  5203. }
  5204. static int decode_cabac_p_mb_sub_type( H264Context *h ) {
  5205. if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
  5206. return 0; /* 8x8 */
  5207. if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
  5208. return 1; /* 8x4 */
  5209. if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
  5210. return 2; /* 4x8 */
  5211. return 3; /* 4x4 */
  5212. }
  5213. static int decode_cabac_b_mb_sub_type( H264Context *h ) {
  5214. int type;
  5215. if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
  5216. return 0; /* B_Direct_8x8 */
  5217. if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
  5218. return 1 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */
  5219. type = 3;
  5220. if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
  5221. if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
  5222. return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
  5223. type += 4;
  5224. }
  5225. type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
  5226. type += get_cabac( &h->cabac, &h->cabac_state[39] );
  5227. return type;
  5228. }
  5229. static inline int decode_cabac_mb_transform_size( H264Context *h ) {
  5230. return get_cabac( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
  5231. }
  5232. static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
  5233. int refa = h->ref_cache[list][scan8[n] - 1];
  5234. int refb = h->ref_cache[list][scan8[n] - 8];
  5235. int ref = 0;
  5236. int ctx = 0;
  5237. if( h->slice_type == B_TYPE) {
  5238. if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
  5239. ctx++;
  5240. if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
  5241. ctx += 2;
  5242. } else {
  5243. if( refa > 0 )
  5244. ctx++;
  5245. if( refb > 0 )
  5246. ctx += 2;
  5247. }
  5248. while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
  5249. ref++;
  5250. if( ctx < 4 )
  5251. ctx = 4;
  5252. else
  5253. ctx = 5;
  5254. }
  5255. return ref;
  5256. }
  5257. static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
  5258. int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
  5259. abs( h->mvd_cache[list][scan8[n] - 8][l] );
  5260. int ctxbase = (l == 0) ? 40 : 47;
  5261. int ctx, mvd;
  5262. if( amvd < 3 )
  5263. ctx = 0;
  5264. else if( amvd > 32 )
  5265. ctx = 2;
  5266. else
  5267. ctx = 1;
  5268. if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
  5269. return 0;
  5270. mvd= 1;
  5271. ctx= 3;
  5272. while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
  5273. mvd++;
  5274. if( ctx < 6 )
  5275. ctx++;
  5276. }
  5277. if( mvd >= 9 ) {
  5278. int k = 3;
  5279. while( get_cabac_bypass( &h->cabac ) ) {
  5280. mvd += 1 << k;
  5281. k++;
  5282. }
  5283. while( k-- ) {
  5284. if( get_cabac_bypass( &h->cabac ) )
  5285. mvd += 1 << k;
  5286. }
  5287. }
  5288. if( get_cabac_bypass( &h->cabac ) ) return -mvd;
  5289. else return mvd;
  5290. }
  5291. static int inline get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) {
  5292. int nza, nzb;
  5293. int ctx = 0;
  5294. if( cat == 0 ) {
  5295. nza = h->left_cbp&0x100;
  5296. nzb = h-> top_cbp&0x100;
  5297. } else if( cat == 1 || cat == 2 ) {
  5298. nza = h->non_zero_count_cache[scan8[idx] - 1];
  5299. nzb = h->non_zero_count_cache[scan8[idx] - 8];
  5300. } else if( cat == 3 ) {
  5301. nza = (h->left_cbp>>(6+idx))&0x01;
  5302. nzb = (h-> top_cbp>>(6+idx))&0x01;
  5303. } else {
  5304. assert(cat == 4);
  5305. nza = h->non_zero_count_cache[scan8[16+idx] - 1];
  5306. nzb = h->non_zero_count_cache[scan8[16+idx] - 8];
  5307. }
  5308. if( nza > 0 )
  5309. ctx++;
  5310. if( nzb > 0 )
  5311. ctx += 2;
  5312. return ctx + 4 * cat;
  5313. }
  5314. static int decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff) {
  5315. const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
  5316. static const int significant_coeff_flag_offset[2][6] = {
  5317. { 105+0, 105+15, 105+29, 105+44, 105+47, 402 },
  5318. { 277+0, 277+15, 277+29, 277+44, 277+47, 436 }
  5319. };
  5320. static const int last_coeff_flag_offset[2][6] = {
  5321. { 166+0, 166+15, 166+29, 166+44, 166+47, 417 },
  5322. { 338+0, 338+15, 338+29, 338+44, 338+47, 451 }
  5323. };
  5324. static const int coeff_abs_level_m1_offset[6] = {
  5325. 227+0, 227+10, 227+20, 227+30, 227+39, 426
  5326. };
  5327. static const int significant_coeff_flag_offset_8x8[2][63] = {
  5328. { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
  5329. 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
  5330. 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
  5331. 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12 },
  5332. { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
  5333. 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
  5334. 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
  5335. 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }
  5336. };
  5337. static const int last_coeff_flag_offset_8x8[63] = {
  5338. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5339. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  5340. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  5341. 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
  5342. };
  5343. int index[64];
  5344. int i, last;
  5345. int coeff_count = 0;
  5346. int abslevel1 = 1;
  5347. int abslevelgt1 = 0;
  5348. uint8_t *significant_coeff_ctx_base;
  5349. uint8_t *last_coeff_ctx_base;
  5350. uint8_t *abs_level_m1_ctx_base;
  5351. /* cat: 0-> DC 16x16 n = 0
  5352. * 1-> AC 16x16 n = luma4x4idx
  5353. * 2-> Luma4x4 n = luma4x4idx
  5354. * 3-> DC Chroma n = iCbCr
  5355. * 4-> AC Chroma n = 4 * iCbCr + chroma4x4idx
  5356. * 5-> Luma8x8 n = 4 * luma8x8idx
  5357. */
  5358. /* read coded block flag */
  5359. if( cat != 5 ) {
  5360. if( get_cabac( &h->cabac, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n ) ] ) == 0 ) {
  5361. if( cat == 1 || cat == 2 )
  5362. h->non_zero_count_cache[scan8[n]] = 0;
  5363. else if( cat == 4 )
  5364. h->non_zero_count_cache[scan8[16+n]] = 0;
  5365. return 0;
  5366. }
  5367. }
  5368. significant_coeff_ctx_base = h->cabac_state
  5369. + significant_coeff_flag_offset[MB_FIELD][cat];
  5370. last_coeff_ctx_base = h->cabac_state
  5371. + last_coeff_flag_offset[MB_FIELD][cat];
  5372. abs_level_m1_ctx_base = h->cabac_state
  5373. + coeff_abs_level_m1_offset[cat];
  5374. if( cat == 5 ) {
  5375. #define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \
  5376. for(last= 0; last < coefs; last++) { \
  5377. uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \
  5378. if( get_cabac( &h->cabac, sig_ctx )) { \
  5379. uint8_t *last_ctx = last_coeff_ctx_base + last_off; \
  5380. index[coeff_count++] = last; \
  5381. if( get_cabac( &h->cabac, last_ctx ) ) { \
  5382. last= max_coeff; \
  5383. break; \
  5384. } \
  5385. } \
  5386. }
  5387. const int *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
  5388. DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );
  5389. } else {
  5390. DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
  5391. }
  5392. if( last == max_coeff -1 ) {
  5393. index[coeff_count++] = last;
  5394. }
  5395. assert(coeff_count > 0);
  5396. if( cat == 0 )
  5397. h->cbp_table[mb_xy] |= 0x100;
  5398. else if( cat == 1 || cat == 2 )
  5399. h->non_zero_count_cache[scan8[n]] = coeff_count;
  5400. else if( cat == 3 )
  5401. h->cbp_table[mb_xy] |= 0x40 << n;
  5402. else if( cat == 4 )
  5403. h->non_zero_count_cache[scan8[16+n]] = coeff_count;
  5404. else {
  5405. assert( cat == 5 );
  5406. fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);
  5407. }
  5408. for( i = coeff_count - 1; i >= 0; i-- ) {
  5409. uint8_t *ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 )) + abs_level_m1_ctx_base;
  5410. int j= scantable[index[i]];
  5411. if( get_cabac( &h->cabac, ctx ) == 0 ) {
  5412. if( !qmul ) {
  5413. if( get_cabac_bypass( &h->cabac ) ) block[j] = -1;
  5414. else block[j] = 1;
  5415. }else{
  5416. if( get_cabac_bypass( &h->cabac ) ) block[j] = (-qmul[j] + 32) >> 6;
  5417. else block[j] = ( qmul[j] + 32) >> 6;
  5418. }
  5419. abslevel1++;
  5420. } else {
  5421. int coeff_abs = 2;
  5422. ctx = 5 + FFMIN( 4, abslevelgt1 ) + abs_level_m1_ctx_base;
  5423. while( coeff_abs < 15 && get_cabac( &h->cabac, ctx ) ) {
  5424. coeff_abs++;
  5425. }
  5426. if( coeff_abs >= 15 ) {
  5427. int j = 0;
  5428. while( get_cabac_bypass( &h->cabac ) ) {
  5429. coeff_abs += 1 << j;
  5430. j++;
  5431. }
  5432. while( j-- ) {
  5433. if( get_cabac_bypass( &h->cabac ) )
  5434. coeff_abs += 1 << j ;
  5435. }
  5436. }
  5437. if( !qmul ) {
  5438. if( get_cabac_bypass( &h->cabac ) ) block[j] = -coeff_abs;
  5439. else block[j] = coeff_abs;
  5440. }else{
  5441. if( get_cabac_bypass( &h->cabac ) ) block[j] = (-coeff_abs * qmul[j] + 32) >> 6;
  5442. else block[j] = ( coeff_abs * qmul[j] + 32) >> 6;
  5443. }
  5444. abslevelgt1++;
  5445. }
  5446. }
  5447. return 0;
  5448. }
  5449. static void inline compute_mb_neighbors(H264Context *h)
  5450. {
  5451. MpegEncContext * const s = &h->s;
  5452. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  5453. h->top_mb_xy = mb_xy - s->mb_stride;
  5454. h->left_mb_xy[0] = mb_xy - 1;
  5455. if(FRAME_MBAFF){
  5456. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  5457. const int top_pair_xy = pair_xy - s->mb_stride;
  5458. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  5459. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  5460. const int curr_mb_frame_flag = !MB_FIELD;
  5461. const int bottom = (s->mb_y & 1);
  5462. if (bottom
  5463. ? !curr_mb_frame_flag // bottom macroblock
  5464. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  5465. ) {
  5466. h->top_mb_xy -= s->mb_stride;
  5467. }
  5468. if (left_mb_frame_flag != curr_mb_frame_flag) {
  5469. h->left_mb_xy[0] = pair_xy - 1;
  5470. }
  5471. }
  5472. return;
  5473. }
  5474. /**
  5475. * decodes a macroblock
  5476. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  5477. */
  5478. static int decode_mb_cabac(H264Context *h) {
  5479. MpegEncContext * const s = &h->s;
  5480. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  5481. int mb_type, partition_count, cbp = 0;
  5482. int dct8x8_allowed= h->pps.transform_8x8_mode;
  5483. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?)
  5484. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  5485. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) {
  5486. int skip;
  5487. /* a skipped mb needs the aff flag from the following mb */
  5488. if( FRAME_MBAFF && s->mb_x==0 && (s->mb_y&1)==0 )
  5489. predict_field_decoding_flag(h);
  5490. if( FRAME_MBAFF && (s->mb_y&1)==1 && h->prev_mb_skipped )
  5491. skip = h->next_mb_skipped;
  5492. else
  5493. skip = decode_cabac_mb_skip( h, s->mb_x, s->mb_y );
  5494. /* read skip flags */
  5495. if( skip ) {
  5496. if( FRAME_MBAFF && (s->mb_y&1)==0 ){
  5497. s->current_picture.mb_type[mb_xy] = MB_TYPE_SKIP;
  5498. h->next_mb_skipped = decode_cabac_mb_skip( h, s->mb_x, s->mb_y+1 );
  5499. if(h->next_mb_skipped)
  5500. predict_field_decoding_flag(h);
  5501. else
  5502. h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5503. }
  5504. decode_mb_skip(h);
  5505. h->cbp_table[mb_xy] = 0;
  5506. h->chroma_pred_mode_table[mb_xy] = 0;
  5507. h->last_qscale_diff = 0;
  5508. return 0;
  5509. }
  5510. }
  5511. if(FRAME_MBAFF){
  5512. if( (s->mb_y&1) == 0 )
  5513. h->mb_mbaff =
  5514. h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5515. }else
  5516. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  5517. h->prev_mb_skipped = 0;
  5518. compute_mb_neighbors(h);
  5519. if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
  5520. av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
  5521. return -1;
  5522. }
  5523. if( h->slice_type == B_TYPE ) {
  5524. if( mb_type < 23 ){
  5525. partition_count= b_mb_type_info[mb_type].partition_count;
  5526. mb_type= b_mb_type_info[mb_type].type;
  5527. }else{
  5528. mb_type -= 23;
  5529. goto decode_intra_mb;
  5530. }
  5531. } else if( h->slice_type == P_TYPE ) {
  5532. if( mb_type < 5) {
  5533. partition_count= p_mb_type_info[mb_type].partition_count;
  5534. mb_type= p_mb_type_info[mb_type].type;
  5535. } else {
  5536. mb_type -= 5;
  5537. goto decode_intra_mb;
  5538. }
  5539. } else {
  5540. assert(h->slice_type == I_TYPE);
  5541. decode_intra_mb:
  5542. partition_count = 0;
  5543. cbp= i_mb_type_info[mb_type].cbp;
  5544. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  5545. mb_type= i_mb_type_info[mb_type].type;
  5546. }
  5547. if(MB_FIELD)
  5548. mb_type |= MB_TYPE_INTERLACED;
  5549. h->slice_table[ mb_xy ]= h->slice_num;
  5550. if(IS_INTRA_PCM(mb_type)) {
  5551. const uint8_t *ptr;
  5552. unsigned int x, y;
  5553. // We assume these blocks are very rare so we dont optimize it.
  5554. // FIXME The two following lines get the bitstream position in the cabac
  5555. // decode, I think it should be done by a function in cabac.h (or cabac.c).
  5556. ptr= h->cabac.bytestream;
  5557. if (h->cabac.low&0x1) ptr-=CABAC_BITS/8;
  5558. // The pixels are stored in the same order as levels in h->mb array.
  5559. for(y=0; y<16; y++){
  5560. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  5561. for(x=0; x<16; x++){
  5562. tprintf("LUMA ICPM LEVEL (%3d)\n", *ptr);
  5563. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= *ptr++;
  5564. }
  5565. }
  5566. for(y=0; y<8; y++){
  5567. const int index= 256 + 4*(y&3) + 32*(y>>2);
  5568. for(x=0; x<8; x++){
  5569. tprintf("CHROMA U ICPM LEVEL (%3d)\n", *ptr);
  5570. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5571. }
  5572. }
  5573. for(y=0; y<8; y++){
  5574. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  5575. for(x=0; x<8; x++){
  5576. tprintf("CHROMA V ICPM LEVEL (%3d)\n", *ptr);
  5577. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5578. }
  5579. }
  5580. ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);
  5581. // All blocks are present
  5582. h->cbp_table[mb_xy] = 0x1ef;
  5583. h->chroma_pred_mode_table[mb_xy] = 0;
  5584. // In deblocking, the quantizer is 0
  5585. s->current_picture.qscale_table[mb_xy]= 0;
  5586. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  5587. // All coeffs are present
  5588. memset(h->non_zero_count[mb_xy], 16, 16);
  5589. s->current_picture.mb_type[mb_xy]= mb_type;
  5590. return 0;
  5591. }
  5592. if(MB_MBAFF){
  5593. h->ref_count[0] <<= 1;
  5594. h->ref_count[1] <<= 1;
  5595. }
  5596. fill_caches(h, mb_type, 0);
  5597. if( IS_INTRA( mb_type ) ) {
  5598. int i;
  5599. if( IS_INTRA4x4( mb_type ) ) {
  5600. if( dct8x8_allowed && decode_cabac_mb_transform_size( h ) ) {
  5601. mb_type |= MB_TYPE_8x8DCT;
  5602. for( i = 0; i < 16; i+=4 ) {
  5603. int pred = pred_intra_mode( h, i );
  5604. int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5605. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  5606. }
  5607. } else {
  5608. for( i = 0; i < 16; i++ ) {
  5609. int pred = pred_intra_mode( h, i );
  5610. h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5611. //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
  5612. }
  5613. }
  5614. write_back_intra_pred_mode(h);
  5615. if( check_intra4x4_pred_mode(h) < 0 ) return -1;
  5616. } else {
  5617. h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );
  5618. if( h->intra16x16_pred_mode < 0 ) return -1;
  5619. }
  5620. h->chroma_pred_mode_table[mb_xy] =
  5621. h->chroma_pred_mode = decode_cabac_mb_chroma_pre_mode( h );
  5622. h->chroma_pred_mode= check_intra_pred_mode( h, h->chroma_pred_mode );
  5623. if( h->chroma_pred_mode < 0 ) return -1;
  5624. } else if( partition_count == 4 ) {
  5625. int i, j, sub_partition_count[4], list, ref[2][4];
  5626. if( h->slice_type == B_TYPE ) {
  5627. for( i = 0; i < 4; i++ ) {
  5628. h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
  5629. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5630. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5631. }
  5632. if( IS_DIRECT(h->sub_mb_type[0] | h->sub_mb_type[1] |
  5633. h->sub_mb_type[2] | h->sub_mb_type[3]) ) {
  5634. pred_direct_motion(h, &mb_type);
  5635. if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
  5636. for( i = 0; i < 4; i++ )
  5637. if( IS_DIRECT(h->sub_mb_type[i]) )
  5638. fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );
  5639. }
  5640. }
  5641. } else {
  5642. for( i = 0; i < 4; i++ ) {
  5643. h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
  5644. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5645. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5646. }
  5647. }
  5648. for( list = 0; list < 2; list++ ) {
  5649. if( h->ref_count[list] > 0 ) {
  5650. for( i = 0; i < 4; i++ ) {
  5651. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  5652. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  5653. if( h->ref_count[list] > 1 )
  5654. ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );
  5655. else
  5656. ref[list][i] = 0;
  5657. } else {
  5658. ref[list][i] = -1;
  5659. }
  5660. h->ref_cache[list][ scan8[4*i]+1 ]=
  5661. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  5662. }
  5663. }
  5664. }
  5665. if(dct8x8_allowed)
  5666. dct8x8_allowed = get_dct8x8_allowed(h);
  5667. for(list=0; list<2; list++){
  5668. for(i=0; i<4; i++){
  5669. if(IS_DIRECT(h->sub_mb_type[i])){
  5670. fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);
  5671. continue;
  5672. }
  5673. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];
  5674. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  5675. const int sub_mb_type= h->sub_mb_type[i];
  5676. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  5677. for(j=0; j<sub_partition_count[i]; j++){
  5678. int mpx, mpy;
  5679. int mx, my;
  5680. const int index= 4*i + block_width*j;
  5681. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  5682. int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];
  5683. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);
  5684. mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );
  5685. my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );
  5686. tprintf("final mv:%d %d\n", mx, my);
  5687. if(IS_SUB_8X8(sub_mb_type)){
  5688. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  5689. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  5690. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  5691. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  5692. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]=
  5693. mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;
  5694. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]=
  5695. mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;
  5696. }else if(IS_SUB_8X4(sub_mb_type)){
  5697. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  5698. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  5699. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]= mx- mpx;
  5700. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]= my - mpy;
  5701. }else if(IS_SUB_4X8(sub_mb_type)){
  5702. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  5703. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  5704. mvd_cache[ 0 ][0]= mvd_cache[ 8 ][0]= mx - mpx;
  5705. mvd_cache[ 0 ][1]= mvd_cache[ 8 ][1]= my - mpy;
  5706. }else{
  5707. assert(IS_SUB_4X4(sub_mb_type));
  5708. mv_cache[ 0 ][0]= mx;
  5709. mv_cache[ 0 ][1]= my;
  5710. mvd_cache[ 0 ][0]= mx - mpx;
  5711. mvd_cache[ 0 ][1]= my - mpy;
  5712. }
  5713. }
  5714. }else{
  5715. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  5716. uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];
  5717. p[0] = p[1] = p[8] = p[9] = 0;
  5718. pd[0]= pd[1]= pd[8]= pd[9]= 0;
  5719. }
  5720. }
  5721. }
  5722. } else if( IS_DIRECT(mb_type) ) {
  5723. pred_direct_motion(h, &mb_type);
  5724. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  5725. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  5726. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  5727. } else {
  5728. int list, mx, my, i, mpx, mpy;
  5729. if(IS_16X16(mb_type)){
  5730. for(list=0; list<2; list++){
  5731. if(IS_DIR(mb_type, 0, list)){
  5732. if(h->ref_count[list] > 0 ){
  5733. const int ref = h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 0 ) : 0;
  5734. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
  5735. }
  5736. }else
  5737. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  5738. }
  5739. for(list=0; list<2; list++){
  5740. if(IS_DIR(mb_type, 0, list)){
  5741. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);
  5742. mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );
  5743. my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );
  5744. tprintf("final mv:%d %d\n", mx, my);
  5745. fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5746. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  5747. }else
  5748. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  5749. }
  5750. }
  5751. else if(IS_16X8(mb_type)){
  5752. for(list=0; list<2; list++){
  5753. if(h->ref_count[list]>0){
  5754. for(i=0; i<2; i++){
  5755. if(IS_DIR(mb_type, i, list)){
  5756. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 8*i ) : 0;
  5757. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
  5758. }else
  5759. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  5760. }
  5761. }
  5762. }
  5763. for(list=0; list<2; list++){
  5764. for(i=0; i<2; i++){
  5765. if(IS_DIR(mb_type, i, list)){
  5766. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);
  5767. mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );
  5768. my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );
  5769. tprintf("final mv:%d %d\n", mx, my);
  5770. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);
  5771. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  5772. }else{
  5773. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5774. fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5775. }
  5776. }
  5777. }
  5778. }else{
  5779. assert(IS_8X16(mb_type));
  5780. for(list=0; list<2; list++){
  5781. if(h->ref_count[list]>0){
  5782. for(i=0; i<2; i++){
  5783. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  5784. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 4*i ) : 0;
  5785. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
  5786. }else
  5787. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  5788. }
  5789. }
  5790. }
  5791. for(list=0; list<2; list++){
  5792. for(i=0; i<2; i++){
  5793. if(IS_DIR(mb_type, i, list)){
  5794. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);
  5795. mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );
  5796. my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );
  5797. tprintf("final mv:%d %d\n", mx, my);
  5798. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5799. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  5800. }else{
  5801. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5802. fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5803. }
  5804. }
  5805. }
  5806. }
  5807. }
  5808. if( IS_INTER( mb_type ) ) {
  5809. h->chroma_pred_mode_table[mb_xy] = 0;
  5810. write_back_motion( h, mb_type );
  5811. }
  5812. if( !IS_INTRA16x16( mb_type ) ) {
  5813. cbp = decode_cabac_mb_cbp_luma( h );
  5814. cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
  5815. }
  5816. h->cbp_table[mb_xy] = cbp;
  5817. if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
  5818. if( decode_cabac_mb_transform_size( h ) )
  5819. mb_type |= MB_TYPE_8x8DCT;
  5820. }
  5821. s->current_picture.mb_type[mb_xy]= mb_type;
  5822. if( cbp || IS_INTRA16x16( mb_type ) ) {
  5823. const uint8_t *scan, *scan8x8, *dc_scan;
  5824. int dqp;
  5825. if(IS_INTERLACED(mb_type)){
  5826. scan8x8= s->qscale ? h->field_scan8x8 : h->field_scan8x8_q0;
  5827. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  5828. dc_scan= luma_dc_field_scan;
  5829. }else{
  5830. scan8x8= s->qscale ? h->zigzag_scan8x8 : h->zigzag_scan8x8_q0;
  5831. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  5832. dc_scan= luma_dc_zigzag_scan;
  5833. }
  5834. h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
  5835. if( dqp == INT_MIN ){
  5836. av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", s->mb_x, s->mb_y);
  5837. return -1;
  5838. }
  5839. s->qscale += dqp;
  5840. if(((unsigned)s->qscale) > 51){
  5841. if(s->qscale<0) s->qscale+= 52;
  5842. else s->qscale-= 52;
  5843. }
  5844. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  5845. if( IS_INTRA16x16( mb_type ) ) {
  5846. int i;
  5847. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
  5848. if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16) < 0)
  5849. return -1;
  5850. if( cbp&15 ) {
  5851. for( i = 0; i < 16; i++ ) {
  5852. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
  5853. if( decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 )
  5854. return -1;
  5855. }
  5856. } else {
  5857. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  5858. }
  5859. } else {
  5860. int i8x8, i4x4;
  5861. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5862. if( cbp & (1<<i8x8) ) {
  5863. if( IS_8x8DCT(mb_type) ) {
  5864. if( decode_cabac_residual(h, h->mb + 64*i8x8, 5, 4*i8x8,
  5865. scan8x8, h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 64) < 0 )
  5866. return -1;
  5867. } else
  5868. for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
  5869. const int index = 4*i8x8 + i4x4;
  5870. //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
  5871. 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 )
  5872. return -1;
  5873. }
  5874. } else {
  5875. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  5876. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  5877. }
  5878. }
  5879. }
  5880. if( cbp&0x30 ){
  5881. int c;
  5882. for( c = 0; c < 2; c++ ) {
  5883. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
  5884. if( decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, NULL, 4) < 0)
  5885. return -1;
  5886. }
  5887. }
  5888. if( cbp&0x20 ) {
  5889. int c, i;
  5890. for( c = 0; c < 2; c++ ) {
  5891. for( i = 0; i < 4; i++ ) {
  5892. const int index = 16 + 4 * c + i;
  5893. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
  5894. 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)
  5895. return -1;
  5896. }
  5897. }
  5898. } else {
  5899. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5900. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5901. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5902. }
  5903. } else {
  5904. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5905. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  5906. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5907. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5908. h->last_qscale_diff = 0;
  5909. }
  5910. s->current_picture.qscale_table[mb_xy]= s->qscale;
  5911. write_back_non_zero_count(h);
  5912. if(MB_MBAFF){
  5913. h->ref_count[0] >>= 1;
  5914. h->ref_count[1] >>= 1;
  5915. }
  5916. return 0;
  5917. }
  5918. static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  5919. int i, d;
  5920. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  5921. const int alpha = alpha_table[index_a];
  5922. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  5923. if( bS[0] < 4 ) {
  5924. int8_t tc[4];
  5925. for(i=0; i<4; i++)
  5926. tc[i] = bS[i] ? tc0_table[index_a][bS[i] - 1] : -1;
  5927. h->s.dsp.h264_h_loop_filter_luma(pix, stride, alpha, beta, tc);
  5928. } else {
  5929. /* 16px edge length, because bS=4 is triggered by being at
  5930. * the edge of an intra MB, so all 4 bS are the same */
  5931. for( d = 0; d < 16; d++ ) {
  5932. const int p0 = pix[-1];
  5933. const int p1 = pix[-2];
  5934. const int p2 = pix[-3];
  5935. const int q0 = pix[0];
  5936. const int q1 = pix[1];
  5937. const int q2 = pix[2];
  5938. if( ABS( p0 - q0 ) < alpha &&
  5939. ABS( p1 - p0 ) < beta &&
  5940. ABS( q1 - q0 ) < beta ) {
  5941. if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5942. if( ABS( p2 - p0 ) < beta)
  5943. {
  5944. const int p3 = pix[-4];
  5945. /* p0', p1', p2' */
  5946. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5947. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5948. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5949. } else {
  5950. /* p0' */
  5951. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5952. }
  5953. if( ABS( q2 - q0 ) < beta)
  5954. {
  5955. const int q3 = pix[3];
  5956. /* q0', q1', q2' */
  5957. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5958. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5959. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5960. } else {
  5961. /* q0' */
  5962. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5963. }
  5964. }else{
  5965. /* p0', q0' */
  5966. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5967. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5968. }
  5969. 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]);
  5970. }
  5971. pix += stride;
  5972. }
  5973. }
  5974. }
  5975. static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  5976. int i;
  5977. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  5978. const int alpha = alpha_table[index_a];
  5979. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  5980. if( bS[0] < 4 ) {
  5981. int8_t tc[4];
  5982. for(i=0; i<4; i++)
  5983. tc[i] = bS[i] ? tc0_table[index_a][bS[i] - 1] + 1 : 0;
  5984. h->s.dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
  5985. } else {
  5986. h->s.dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
  5987. }
  5988. }
  5989. static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int bS[8], int qp[2] ) {
  5990. int i;
  5991. for( i = 0; i < 16; i++, pix += stride) {
  5992. int index_a;
  5993. int alpha;
  5994. int beta;
  5995. int qp_index;
  5996. int bS_index = (i >> 1);
  5997. if (!MB_FIELD) {
  5998. bS_index &= ~1;
  5999. bS_index |= (i & 1);
  6000. }
  6001. if( bS[bS_index] == 0 ) {
  6002. continue;
  6003. }
  6004. qp_index = MB_FIELD ? (i >> 3) : (i & 1);
  6005. index_a = clip( qp[qp_index] + h->slice_alpha_c0_offset, 0, 51 );
  6006. alpha = alpha_table[index_a];
  6007. beta = beta_table[clip( qp[qp_index] + h->slice_beta_offset, 0, 51 )];
  6008. if( bS[bS_index] < 4 ) {
  6009. const int tc0 = tc0_table[index_a][bS[bS_index] - 1];
  6010. const int p0 = pix[-1];
  6011. const int p1 = pix[-2];
  6012. const int p2 = pix[-3];
  6013. const int q0 = pix[0];
  6014. const int q1 = pix[1];
  6015. const int q2 = pix[2];
  6016. if( ABS( p0 - q0 ) < alpha &&
  6017. ABS( p1 - p0 ) < beta &&
  6018. ABS( q1 - q0 ) < beta ) {
  6019. int tc = tc0;
  6020. int i_delta;
  6021. if( ABS( p2 - p0 ) < beta ) {
  6022. pix[-2] = p1 + clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  6023. tc++;
  6024. }
  6025. if( ABS( q2 - q0 ) < beta ) {
  6026. pix[1] = q1 + clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  6027. tc++;
  6028. }
  6029. i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  6030. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  6031. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  6032. 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);
  6033. }
  6034. }else{
  6035. const int p0 = pix[-1];
  6036. const int p1 = pix[-2];
  6037. const int p2 = pix[-3];
  6038. const int q0 = pix[0];
  6039. const int q1 = pix[1];
  6040. const int q2 = pix[2];
  6041. if( ABS( p0 - q0 ) < alpha &&
  6042. ABS( p1 - p0 ) < beta &&
  6043. ABS( q1 - q0 ) < beta ) {
  6044. if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  6045. if( ABS( p2 - p0 ) < beta)
  6046. {
  6047. const int p3 = pix[-4];
  6048. /* p0', p1', p2' */
  6049. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  6050. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  6051. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  6052. } else {
  6053. /* p0' */
  6054. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6055. }
  6056. if( ABS( q2 - q0 ) < beta)
  6057. {
  6058. const int q3 = pix[3];
  6059. /* q0', q1', q2' */
  6060. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  6061. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  6062. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  6063. } else {
  6064. /* q0' */
  6065. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6066. }
  6067. }else{
  6068. /* p0', q0' */
  6069. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6070. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6071. }
  6072. 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]);
  6073. }
  6074. }
  6075. }
  6076. }
  6077. static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int bS[8], int qp[2] ) {
  6078. int i;
  6079. for( i = 0; i < 8; i++, pix += stride) {
  6080. int index_a;
  6081. int alpha;
  6082. int beta;
  6083. int qp_index;
  6084. int bS_index = i;
  6085. if( bS[bS_index] == 0 ) {
  6086. continue;
  6087. }
  6088. qp_index = MB_FIELD ? (i >> 2) : (i & 1);
  6089. index_a = clip( qp[qp_index] + h->slice_alpha_c0_offset, 0, 51 );
  6090. alpha = alpha_table[index_a];
  6091. beta = beta_table[clip( qp[qp_index] + h->slice_beta_offset, 0, 51 )];
  6092. if( bS[bS_index] < 4 ) {
  6093. const int tc = tc0_table[index_a][bS[bS_index] - 1] + 1;
  6094. const int p0 = pix[-1];
  6095. const int p1 = pix[-2];
  6096. const int q0 = pix[0];
  6097. const int q1 = pix[1];
  6098. if( ABS( p0 - q0 ) < alpha &&
  6099. ABS( p1 - p0 ) < beta &&
  6100. ABS( q1 - q0 ) < beta ) {
  6101. const int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  6102. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  6103. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  6104. 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);
  6105. }
  6106. }else{
  6107. const int p0 = pix[-1];
  6108. const int p1 = pix[-2];
  6109. const int q0 = pix[0];
  6110. const int q1 = pix[1];
  6111. if( ABS( p0 - q0 ) < alpha &&
  6112. ABS( p1 - p0 ) < beta &&
  6113. ABS( q1 - q0 ) < beta ) {
  6114. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  6115. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  6116. 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]);
  6117. }
  6118. }
  6119. }
  6120. }
  6121. static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  6122. int i, d;
  6123. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  6124. const int alpha = alpha_table[index_a];
  6125. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  6126. const int pix_next = stride;
  6127. if( bS[0] < 4 ) {
  6128. int8_t tc[4];
  6129. for(i=0; i<4; i++)
  6130. tc[i] = bS[i] ? tc0_table[index_a][bS[i] - 1] : -1;
  6131. h->s.dsp.h264_v_loop_filter_luma(pix, stride, alpha, beta, tc);
  6132. } else {
  6133. /* 16px edge length, see filter_mb_edgev */
  6134. for( d = 0; d < 16; d++ ) {
  6135. const int p0 = pix[-1*pix_next];
  6136. const int p1 = pix[-2*pix_next];
  6137. const int p2 = pix[-3*pix_next];
  6138. const int q0 = pix[0];
  6139. const int q1 = pix[1*pix_next];
  6140. const int q2 = pix[2*pix_next];
  6141. if( ABS( p0 - q0 ) < alpha &&
  6142. ABS( p1 - p0 ) < beta &&
  6143. ABS( q1 - q0 ) < beta ) {
  6144. const int p3 = pix[-4*pix_next];
  6145. const int q3 = pix[ 3*pix_next];
  6146. if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  6147. if( ABS( p2 - p0 ) < beta) {
  6148. /* p0', p1', p2' */
  6149. pix[-1*pix_next] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  6150. pix[-2*pix_next] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  6151. pix[-3*pix_next] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  6152. } else {
  6153. /* p0' */
  6154. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6155. }
  6156. if( ABS( q2 - q0 ) < beta) {
  6157. /* q0', q1', q2' */
  6158. pix[0*pix_next] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  6159. pix[1*pix_next] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  6160. pix[2*pix_next] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  6161. } else {
  6162. /* q0' */
  6163. pix[0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6164. }
  6165. }else{
  6166. /* p0', q0' */
  6167. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6168. pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6169. }
  6170. 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]);
  6171. }
  6172. pix++;
  6173. }
  6174. }
  6175. }
  6176. static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  6177. int i;
  6178. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  6179. const int alpha = alpha_table[index_a];
  6180. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  6181. if( bS[0] < 4 ) {
  6182. int8_t tc[4];
  6183. for(i=0; i<4; i++)
  6184. tc[i] = bS[i] ? tc0_table[index_a][bS[i] - 1] + 1 : 0;
  6185. h->s.dsp.h264_v_loop_filter_chroma(pix, stride, alpha, beta, tc);
  6186. } else {
  6187. h->s.dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);
  6188. }
  6189. }
  6190. 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) {
  6191. MpegEncContext * const s = &h->s;
  6192. const int mb_xy= mb_x + mb_y*s->mb_stride;
  6193. const int mb_type = s->current_picture.mb_type[mb_xy];
  6194. const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4;
  6195. int first_vertical_edge_done = 0;
  6196. int dir;
  6197. /* FIXME: A given frame may occupy more than one position in
  6198. * the reference list. So ref2frm should be populated with
  6199. * frame numbers, not indices. */
  6200. static const int ref2frm[34] = {-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  6201. 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
  6202. //for sufficiently low qp, filtering wouldn't do anything
  6203. //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
  6204. if(!FRAME_MBAFF){
  6205. int qp_thresh = 15 - h->slice_alpha_c0_offset - FFMAX(0, h->pps.chroma_qp_index_offset);
  6206. int qp = s->current_picture.qscale_table[mb_xy];
  6207. if(qp <= qp_thresh
  6208. && (mb_x == 0 || ((qp + s->current_picture.qscale_table[mb_xy-1] + 1)>>1) <= qp_thresh)
  6209. && (mb_y == 0 || ((qp + s->current_picture.qscale_table[h->top_mb_xy] + 1)>>1) <= qp_thresh)){
  6210. return;
  6211. }
  6212. }
  6213. if (FRAME_MBAFF
  6214. // left mb is in picture
  6215. && h->slice_table[mb_xy-1] != 255
  6216. // and current and left pair do not have the same interlaced type
  6217. && (IS_INTERLACED(mb_type) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
  6218. // and left mb is in the same slice if deblocking_filter == 2
  6219. && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
  6220. /* First vertical edge is different in MBAFF frames
  6221. * There are 8 different bS to compute and 2 different Qp
  6222. */
  6223. const int pair_xy = mb_x + (mb_y&~1)*s->mb_stride;
  6224. const int left_mb_xy[2] = { pair_xy-1, pair_xy-1+s->mb_stride };
  6225. int bS[8];
  6226. int qp[2];
  6227. int chroma_qp[2];
  6228. int mb_qp, mbn0_qp, mbn1_qp;
  6229. int i;
  6230. first_vertical_edge_done = 1;
  6231. if( IS_INTRA(mb_type) )
  6232. bS[0] = bS[1] = bS[2] = bS[3] = bS[4] = bS[5] = bS[6] = bS[7] = 4;
  6233. else {
  6234. for( i = 0; i < 8; i++ ) {
  6235. int mbn_xy = MB_FIELD ? left_mb_xy[i>>2] : left_mb_xy[i&1];
  6236. if( IS_INTRA( s->current_picture.mb_type[mbn_xy] ) )
  6237. bS[i] = 4;
  6238. else if( h->non_zero_count_cache[12+8*(i>>1)] != 0 ||
  6239. /* FIXME: with 8x8dct + cavlc, should check cbp instead of nnz */
  6240. h->non_zero_count[mbn_xy][MB_FIELD ? i&3 : (i>>2)+(mb_y&1)*2] )
  6241. bS[i] = 2;
  6242. else
  6243. bS[i] = 1;
  6244. }
  6245. }
  6246. mb_qp = s->current_picture.qscale_table[mb_xy];
  6247. mbn0_qp = s->current_picture.qscale_table[left_mb_xy[0]];
  6248. mbn1_qp = s->current_picture.qscale_table[left_mb_xy[1]];
  6249. qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1;
  6250. chroma_qp[0] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6251. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn0_qp ) + 1 ) >> 1;
  6252. qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1;
  6253. chroma_qp[1] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6254. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn1_qp ) + 1 ) >> 1;
  6255. /* Filter edge */
  6256. 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);
  6257. { int i; for (i = 0; i < 8; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6258. filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp );
  6259. filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, chroma_qp );
  6260. filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, chroma_qp );
  6261. }
  6262. /* dir : 0 -> vertical edge, 1 -> horizontal edge */
  6263. for( dir = 0; dir < 2; dir++ )
  6264. {
  6265. int edge;
  6266. const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
  6267. const int mbm_type = s->current_picture.mb_type[mbm_xy];
  6268. int start = h->slice_table[mbm_xy] == 255 ? 1 : 0;
  6269. const int edges = (mb_type & (MB_TYPE_16x16|MB_TYPE_SKIP))
  6270. == (MB_TYPE_16x16|MB_TYPE_SKIP) ? 1 : 4;
  6271. // how often to recheck mv-based bS when iterating between edges
  6272. const int mask_edge = (mb_type & (MB_TYPE_16x16 | (MB_TYPE_16x8 << dir))) ? 3 :
  6273. (mb_type & (MB_TYPE_8x16 >> dir)) ? 1 : 0;
  6274. // how often to recheck mv-based bS when iterating along each edge
  6275. const int mask_par0 = mb_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir));
  6276. if (first_vertical_edge_done) {
  6277. start = 1;
  6278. first_vertical_edge_done = 0;
  6279. }
  6280. if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
  6281. start = 1;
  6282. if (FRAME_MBAFF && (dir == 1) && ((mb_y&1) == 0) && start == 0
  6283. && !IS_INTERLACED(mb_type)
  6284. && IS_INTERLACED(mbm_type)
  6285. ) {
  6286. // This is a special case in the norm where the filtering must
  6287. // be done twice (one each of the field) even if we are in a
  6288. // frame macroblock.
  6289. //
  6290. static const int nnz_idx[4] = {4,5,6,3};
  6291. unsigned int tmp_linesize = 2 * linesize;
  6292. unsigned int tmp_uvlinesize = 2 * uvlinesize;
  6293. int mbn_xy = mb_xy - 2 * s->mb_stride;
  6294. int qp, chroma_qp;
  6295. int i, j;
  6296. int bS[4];
  6297. for(j=0; j<2; j++, mbn_xy += s->mb_stride){
  6298. if( IS_INTRA(mb_type) ||
  6299. IS_INTRA(s->current_picture.mb_type[mbn_xy]) ) {
  6300. bS[0] = bS[1] = bS[2] = bS[3] = 3;
  6301. } else {
  6302. const uint8_t *mbn_nnz = h->non_zero_count[mbn_xy];
  6303. for( i = 0; i < 4; i++ ) {
  6304. if( h->non_zero_count_cache[scan8[0]+i] != 0 ||
  6305. mbn_nnz[nnz_idx[i]] != 0 )
  6306. bS[i] = 2;
  6307. else
  6308. bS[i] = 1;
  6309. }
  6310. }
  6311. // Do not use s->qscale as luma quantizer because it has not the same
  6312. // value in IPCM macroblocks.
  6313. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6314. 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);
  6315. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6316. filter_mb_edgeh( h, &img_y[j*linesize], tmp_linesize, bS, qp );
  6317. chroma_qp = ( h->chroma_qp +
  6318. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6319. filter_mb_edgech( h, &img_cb[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6320. filter_mb_edgech( h, &img_cr[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6321. }
  6322. start = 1;
  6323. }
  6324. /* Calculate bS */
  6325. for( edge = start; edge < edges; edge++ ) {
  6326. /* mbn_xy: neighbor macroblock */
  6327. const int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
  6328. const int mbn_type = s->current_picture.mb_type[mbn_xy];
  6329. int bS[4];
  6330. int qp;
  6331. if( (edge&1) && IS_8x8DCT(mb_type) )
  6332. continue;
  6333. if( IS_INTRA(mb_type) ||
  6334. IS_INTRA(mbn_type) ) {
  6335. int value;
  6336. if (edge == 0) {
  6337. if ( (!IS_INTERLACED(mb_type) && !IS_INTERLACED(mbm_type))
  6338. || ((FRAME_MBAFF || (s->picture_structure != PICT_FRAME)) && (dir == 0))
  6339. ) {
  6340. value = 4;
  6341. } else {
  6342. value = 3;
  6343. }
  6344. } else {
  6345. value = 3;
  6346. }
  6347. bS[0] = bS[1] = bS[2] = bS[3] = value;
  6348. } else {
  6349. int i, l;
  6350. int mv_done;
  6351. if( edge & mask_edge ) {
  6352. bS[0] = bS[1] = bS[2] = bS[3] = 0;
  6353. mv_done = 1;
  6354. }
  6355. else if( FRAME_MBAFF && IS_INTERLACED(mb_type ^ mbn_type)) {
  6356. bS[0] = bS[1] = bS[2] = bS[3] = 1;
  6357. mv_done = 1;
  6358. }
  6359. else if( mask_par0 && (edge || (mbn_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir)))) ) {
  6360. int b_idx= 8 + 4 + edge * (dir ? 8:1);
  6361. int bn_idx= b_idx - (dir ? 8:1);
  6362. int v = 0;
  6363. for( l = 0; !v && l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6364. v |= ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6365. ABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6366. ABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit;
  6367. }
  6368. bS[0] = bS[1] = bS[2] = bS[3] = v;
  6369. mv_done = 1;
  6370. }
  6371. else
  6372. mv_done = 0;
  6373. for( i = 0; i < 4; i++ ) {
  6374. int x = dir == 0 ? edge : i;
  6375. int y = dir == 0 ? i : edge;
  6376. int b_idx= 8 + 4 + x + 8*y;
  6377. int bn_idx= b_idx - (dir ? 8:1);
  6378. if( h->non_zero_count_cache[b_idx] != 0 ||
  6379. h->non_zero_count_cache[bn_idx] != 0 ) {
  6380. bS[i] = 2;
  6381. }
  6382. else if(!mv_done)
  6383. {
  6384. bS[i] = 0;
  6385. for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6386. if( ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6387. ABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6388. ABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit ) {
  6389. bS[i] = 1;
  6390. break;
  6391. }
  6392. }
  6393. }
  6394. }
  6395. if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
  6396. continue;
  6397. }
  6398. /* Filter edge */
  6399. // Do not use s->qscale as luma quantizer because it has not the same
  6400. // value in IPCM macroblocks.
  6401. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6402. //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]);
  6403. tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
  6404. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6405. if( dir == 0 ) {
  6406. filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
  6407. if( (edge&1) == 0 ) {
  6408. int chroma_qp = ( h->chroma_qp +
  6409. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6410. filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp );
  6411. filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp );
  6412. }
  6413. } else {
  6414. filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
  6415. if( (edge&1) == 0 ) {
  6416. int chroma_qp = ( h->chroma_qp +
  6417. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6418. filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6419. filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6420. }
  6421. }
  6422. }
  6423. }
  6424. }
  6425. static int decode_slice(H264Context *h){
  6426. MpegEncContext * const s = &h->s;
  6427. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  6428. s->mb_skip_run= -1;
  6429. if( h->pps.cabac ) {
  6430. int i;
  6431. /* realign */
  6432. align_get_bits( &s->gb );
  6433. /* init cabac */
  6434. ff_init_cabac_states( &h->cabac, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64 );
  6435. ff_init_cabac_decoder( &h->cabac,
  6436. s->gb.buffer + get_bits_count(&s->gb)/8,
  6437. ( s->gb.size_in_bits - get_bits_count(&s->gb) + 7)/8);
  6438. /* calculate pre-state */
  6439. for( i= 0; i < 460; i++ ) {
  6440. int pre;
  6441. if( h->slice_type == I_TYPE )
  6442. pre = clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
  6443. else
  6444. 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 );
  6445. if( pre <= 63 )
  6446. h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
  6447. else
  6448. h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
  6449. }
  6450. for(;;){
  6451. int ret = decode_mb_cabac(h);
  6452. int eos;
  6453. if(ret>=0) hl_decode_mb(h);
  6454. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  6455. s->mb_y++;
  6456. if(ret>=0) ret = decode_mb_cabac(h);
  6457. if(ret>=0) hl_decode_mb(h);
  6458. s->mb_y--;
  6459. }
  6460. eos = get_cabac_terminate( &h->cabac );
  6461. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 1) {
  6462. 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);
  6463. 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);
  6464. return -1;
  6465. }
  6466. if( ++s->mb_x >= s->mb_width ) {
  6467. s->mb_x = 0;
  6468. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6469. ++s->mb_y;
  6470. if(FRAME_MBAFF) {
  6471. ++s->mb_y;
  6472. }
  6473. }
  6474. if( eos || s->mb_y >= s->mb_height ) {
  6475. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6476. 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);
  6477. return 0;
  6478. }
  6479. }
  6480. } else {
  6481. for(;;){
  6482. int ret = decode_mb_cavlc(h);
  6483. if(ret>=0) hl_decode_mb(h);
  6484. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  6485. s->mb_y++;
  6486. ret = decode_mb_cavlc(h);
  6487. if(ret>=0) hl_decode_mb(h);
  6488. s->mb_y--;
  6489. }
  6490. if(ret<0){
  6491. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6492. 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);
  6493. return -1;
  6494. }
  6495. if(++s->mb_x >= s->mb_width){
  6496. s->mb_x=0;
  6497. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6498. ++s->mb_y;
  6499. if(FRAME_MBAFF) {
  6500. ++s->mb_y;
  6501. }
  6502. if(s->mb_y >= s->mb_height){
  6503. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6504. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  6505. 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);
  6506. return 0;
  6507. }else{
  6508. 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);
  6509. return -1;
  6510. }
  6511. }
  6512. }
  6513. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  6514. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6515. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  6516. 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);
  6517. return 0;
  6518. }else{
  6519. 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);
  6520. return -1;
  6521. }
  6522. }
  6523. }
  6524. }
  6525. #if 0
  6526. for(;s->mb_y < s->mb_height; s->mb_y++){
  6527. for(;s->mb_x < s->mb_width; s->mb_x++){
  6528. int ret= decode_mb(h);
  6529. hl_decode_mb(h);
  6530. if(ret<0){
  6531. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6532. 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);
  6533. return -1;
  6534. }
  6535. if(++s->mb_x >= s->mb_width){
  6536. s->mb_x=0;
  6537. if(++s->mb_y >= s->mb_height){
  6538. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6539. 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);
  6540. return 0;
  6541. }else{
  6542. 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);
  6543. return -1;
  6544. }
  6545. }
  6546. }
  6547. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  6548. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6549. 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);
  6550. return 0;
  6551. }else{
  6552. 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);
  6553. return -1;
  6554. }
  6555. }
  6556. }
  6557. s->mb_x=0;
  6558. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6559. }
  6560. #endif
  6561. return -1; //not reached
  6562. }
  6563. static int decode_unregistered_user_data(H264Context *h, int size){
  6564. MpegEncContext * const s = &h->s;
  6565. uint8_t user_data[16+256];
  6566. int e, build, i;
  6567. if(size<16)
  6568. return -1;
  6569. for(i=0; i<sizeof(user_data)-1 && i<size; i++){
  6570. user_data[i]= get_bits(&s->gb, 8);
  6571. }
  6572. user_data[i]= 0;
  6573. e= sscanf(user_data+16, "x264 - core %d"/*%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html*/, &build);
  6574. if(e==1 && build>=0)
  6575. h->x264_build= build;
  6576. if(s->avctx->debug & FF_DEBUG_BUGS)
  6577. av_log(s->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data+16);
  6578. for(; i<size; i++)
  6579. skip_bits(&s->gb, 8);
  6580. return 0;
  6581. }
  6582. static int decode_sei(H264Context *h){
  6583. MpegEncContext * const s = &h->s;
  6584. while(get_bits_count(&s->gb) + 16 < s->gb.size_in_bits){
  6585. int size, type;
  6586. type=0;
  6587. do{
  6588. type+= show_bits(&s->gb, 8);
  6589. }while(get_bits(&s->gb, 8) == 255);
  6590. size=0;
  6591. do{
  6592. size+= show_bits(&s->gb, 8);
  6593. }while(get_bits(&s->gb, 8) == 255);
  6594. switch(type){
  6595. case 5:
  6596. if(decode_unregistered_user_data(h, size) < 0)
  6597. return -1;
  6598. break;
  6599. default:
  6600. skip_bits(&s->gb, 8*size);
  6601. }
  6602. //FIXME check bits here
  6603. align_get_bits(&s->gb);
  6604. }
  6605. return 0;
  6606. }
  6607. static inline void decode_hrd_parameters(H264Context *h, SPS *sps){
  6608. MpegEncContext * const s = &h->s;
  6609. int cpb_count, i;
  6610. cpb_count = get_ue_golomb(&s->gb) + 1;
  6611. get_bits(&s->gb, 4); /* bit_rate_scale */
  6612. get_bits(&s->gb, 4); /* cpb_size_scale */
  6613. for(i=0; i<cpb_count; i++){
  6614. get_ue_golomb(&s->gb); /* bit_rate_value_minus1 */
  6615. get_ue_golomb(&s->gb); /* cpb_size_value_minus1 */
  6616. get_bits1(&s->gb); /* cbr_flag */
  6617. }
  6618. get_bits(&s->gb, 5); /* initial_cpb_removal_delay_length_minus1 */
  6619. get_bits(&s->gb, 5); /* cpb_removal_delay_length_minus1 */
  6620. get_bits(&s->gb, 5); /* dpb_output_delay_length_minus1 */
  6621. get_bits(&s->gb, 5); /* time_offset_length */
  6622. }
  6623. static inline int decode_vui_parameters(H264Context *h, SPS *sps){
  6624. MpegEncContext * const s = &h->s;
  6625. int aspect_ratio_info_present_flag, aspect_ratio_idc;
  6626. int nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag;
  6627. aspect_ratio_info_present_flag= get_bits1(&s->gb);
  6628. if( aspect_ratio_info_present_flag ) {
  6629. aspect_ratio_idc= get_bits(&s->gb, 8);
  6630. if( aspect_ratio_idc == EXTENDED_SAR ) {
  6631. sps->sar.num= get_bits(&s->gb, 16);
  6632. sps->sar.den= get_bits(&s->gb, 16);
  6633. }else if(aspect_ratio_idc < 14){
  6634. sps->sar= pixel_aspect[aspect_ratio_idc];
  6635. }else{
  6636. av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
  6637. return -1;
  6638. }
  6639. }else{
  6640. sps->sar.num=
  6641. sps->sar.den= 0;
  6642. }
  6643. // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
  6644. if(get_bits1(&s->gb)){ /* overscan_info_present_flag */
  6645. get_bits1(&s->gb); /* overscan_appropriate_flag */
  6646. }
  6647. if(get_bits1(&s->gb)){ /* video_signal_type_present_flag */
  6648. get_bits(&s->gb, 3); /* video_format */
  6649. get_bits1(&s->gb); /* video_full_range_flag */
  6650. if(get_bits1(&s->gb)){ /* colour_description_present_flag */
  6651. get_bits(&s->gb, 8); /* colour_primaries */
  6652. get_bits(&s->gb, 8); /* transfer_characteristics */
  6653. get_bits(&s->gb, 8); /* matrix_coefficients */
  6654. }
  6655. }
  6656. if(get_bits1(&s->gb)){ /* chroma_location_info_present_flag */
  6657. get_ue_golomb(&s->gb); /* chroma_sample_location_type_top_field */
  6658. get_ue_golomb(&s->gb); /* chroma_sample_location_type_bottom_field */
  6659. }
  6660. sps->timing_info_present_flag = get_bits1(&s->gb);
  6661. if(sps->timing_info_present_flag){
  6662. sps->num_units_in_tick = get_bits_long(&s->gb, 32);
  6663. sps->time_scale = get_bits_long(&s->gb, 32);
  6664. sps->fixed_frame_rate_flag = get_bits1(&s->gb);
  6665. }
  6666. nal_hrd_parameters_present_flag = get_bits1(&s->gb);
  6667. if(nal_hrd_parameters_present_flag)
  6668. decode_hrd_parameters(h, sps);
  6669. vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
  6670. if(vcl_hrd_parameters_present_flag)
  6671. decode_hrd_parameters(h, sps);
  6672. if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag)
  6673. get_bits1(&s->gb); /* low_delay_hrd_flag */
  6674. get_bits1(&s->gb); /* pic_struct_present_flag */
  6675. sps->bitstream_restriction_flag = get_bits1(&s->gb);
  6676. if(sps->bitstream_restriction_flag){
  6677. get_bits1(&s->gb); /* motion_vectors_over_pic_boundaries_flag */
  6678. get_ue_golomb(&s->gb); /* max_bytes_per_pic_denom */
  6679. get_ue_golomb(&s->gb); /* max_bits_per_mb_denom */
  6680. get_ue_golomb(&s->gb); /* log2_max_mv_length_horizontal */
  6681. get_ue_golomb(&s->gb); /* log2_max_mv_length_vertical */
  6682. sps->num_reorder_frames = get_ue_golomb(&s->gb);
  6683. get_ue_golomb(&s->gb); /* max_dec_frame_buffering */
  6684. }
  6685. return 0;
  6686. }
  6687. static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,
  6688. const uint8_t *jvt_list, const uint8_t *fallback_list){
  6689. MpegEncContext * const s = &h->s;
  6690. int i, last = 8, next = 8;
  6691. const uint8_t *scan = size == 16 ? zigzag_scan : zigzag_scan8x8;
  6692. if(!get_bits1(&s->gb)) /* matrix not written, we use the predicted one */
  6693. memcpy(factors, fallback_list, size*sizeof(uint8_t));
  6694. else
  6695. for(i=0;i<size;i++){
  6696. if(next)
  6697. next = (last + get_se_golomb(&s->gb)) & 0xff;
  6698. if(!i && !next){ /* matrix not written, we use the preset one */
  6699. memcpy(factors, jvt_list, size*sizeof(uint8_t));
  6700. break;
  6701. }
  6702. last = factors[scan[i]] = next ? next : last;
  6703. }
  6704. }
  6705. static void decode_scaling_matrices(H264Context *h, SPS *sps, PPS *pps, int is_sps,
  6706. uint8_t (*scaling_matrix4)[16], uint8_t (*scaling_matrix8)[64]){
  6707. MpegEncContext * const s = &h->s;
  6708. int fallback_sps = !is_sps && sps->scaling_matrix_present;
  6709. const uint8_t *fallback[4] = {
  6710. fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
  6711. fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
  6712. fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
  6713. fallback_sps ? sps->scaling_matrix8[1] : default_scaling8[1]
  6714. };
  6715. if(get_bits1(&s->gb)){
  6716. sps->scaling_matrix_present |= is_sps;
  6717. decode_scaling_list(h,scaling_matrix4[0],16,default_scaling4[0],fallback[0]); // Intra, Y
  6718. decode_scaling_list(h,scaling_matrix4[1],16,default_scaling4[0],scaling_matrix4[0]); // Intra, Cr
  6719. decode_scaling_list(h,scaling_matrix4[2],16,default_scaling4[0],scaling_matrix4[1]); // Intra, Cb
  6720. decode_scaling_list(h,scaling_matrix4[3],16,default_scaling4[1],fallback[1]); // Inter, Y
  6721. decode_scaling_list(h,scaling_matrix4[4],16,default_scaling4[1],scaling_matrix4[3]); // Inter, Cr
  6722. decode_scaling_list(h,scaling_matrix4[5],16,default_scaling4[1],scaling_matrix4[4]); // Inter, Cb
  6723. if(is_sps || pps->transform_8x8_mode){
  6724. decode_scaling_list(h,scaling_matrix8[0],64,default_scaling8[0],fallback[2]); // Intra, Y
  6725. decode_scaling_list(h,scaling_matrix8[1],64,default_scaling8[1],fallback[3]); // Inter, Y
  6726. }
  6727. } else if(fallback_sps) {
  6728. memcpy(scaling_matrix4, sps->scaling_matrix4, 6*16*sizeof(uint8_t));
  6729. memcpy(scaling_matrix8, sps->scaling_matrix8, 2*64*sizeof(uint8_t));
  6730. }
  6731. }
  6732. static inline int decode_seq_parameter_set(H264Context *h){
  6733. MpegEncContext * const s = &h->s;
  6734. int profile_idc, level_idc;
  6735. int sps_id, i;
  6736. SPS *sps;
  6737. profile_idc= get_bits(&s->gb, 8);
  6738. get_bits1(&s->gb); //constraint_set0_flag
  6739. get_bits1(&s->gb); //constraint_set1_flag
  6740. get_bits1(&s->gb); //constraint_set2_flag
  6741. get_bits1(&s->gb); //constraint_set3_flag
  6742. get_bits(&s->gb, 4); // reserved
  6743. level_idc= get_bits(&s->gb, 8);
  6744. sps_id= get_ue_golomb(&s->gb);
  6745. sps= &h->sps_buffer[ sps_id ];
  6746. sps->profile_idc= profile_idc;
  6747. sps->level_idc= level_idc;
  6748. if(sps->profile_idc >= 100){ //high profile
  6749. if(get_ue_golomb(&s->gb) == 3) //chroma_format_idc
  6750. get_bits1(&s->gb); //residual_color_transform_flag
  6751. get_ue_golomb(&s->gb); //bit_depth_luma_minus8
  6752. get_ue_golomb(&s->gb); //bit_depth_chroma_minus8
  6753. sps->transform_bypass = get_bits1(&s->gb);
  6754. decode_scaling_matrices(h, sps, NULL, 1, sps->scaling_matrix4, sps->scaling_matrix8);
  6755. }else
  6756. sps->scaling_matrix_present = 0;
  6757. sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
  6758. sps->poc_type= get_ue_golomb(&s->gb);
  6759. if(sps->poc_type == 0){ //FIXME #define
  6760. sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
  6761. } else if(sps->poc_type == 1){//FIXME #define
  6762. sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
  6763. sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
  6764. sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
  6765. sps->poc_cycle_length= get_ue_golomb(&s->gb);
  6766. for(i=0; i<sps->poc_cycle_length; i++)
  6767. sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
  6768. }
  6769. if(sps->poc_type > 2){
  6770. av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
  6771. return -1;
  6772. }
  6773. sps->ref_frame_count= get_ue_golomb(&s->gb);
  6774. if(sps->ref_frame_count > MAX_PICTURE_COUNT-2){
  6775. av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");
  6776. }
  6777. sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
  6778. sps->mb_width= get_ue_golomb(&s->gb) + 1;
  6779. sps->mb_height= get_ue_golomb(&s->gb) + 1;
  6780. if((unsigned)sps->mb_width >= INT_MAX/16 || (unsigned)sps->mb_height >= INT_MAX/16 ||
  6781. avcodec_check_dimensions(NULL, 16*sps->mb_width, 16*sps->mb_height))
  6782. return -1;
  6783. sps->frame_mbs_only_flag= get_bits1(&s->gb);
  6784. if(!sps->frame_mbs_only_flag)
  6785. sps->mb_aff= get_bits1(&s->gb);
  6786. else
  6787. sps->mb_aff= 0;
  6788. sps->direct_8x8_inference_flag= get_bits1(&s->gb);
  6789. #ifndef ALLOW_INTERLACE
  6790. if(sps->mb_aff)
  6791. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF support not included; enable it compilation time\n");
  6792. #endif
  6793. if(!sps->direct_8x8_inference_flag && sps->mb_aff)
  6794. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + !direct_8x8_inference is not implemented\n");
  6795. sps->crop= get_bits1(&s->gb);
  6796. if(sps->crop){
  6797. sps->crop_left = get_ue_golomb(&s->gb);
  6798. sps->crop_right = get_ue_golomb(&s->gb);
  6799. sps->crop_top = get_ue_golomb(&s->gb);
  6800. sps->crop_bottom= get_ue_golomb(&s->gb);
  6801. if(sps->crop_left || sps->crop_top){
  6802. av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ...\n");
  6803. }
  6804. }else{
  6805. sps->crop_left =
  6806. sps->crop_right =
  6807. sps->crop_top =
  6808. sps->crop_bottom= 0;
  6809. }
  6810. sps->vui_parameters_present_flag= get_bits1(&s->gb);
  6811. if( sps->vui_parameters_present_flag )
  6812. decode_vui_parameters(h, sps);
  6813. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6814. 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",
  6815. sps_id, sps->profile_idc, sps->level_idc,
  6816. sps->poc_type,
  6817. sps->ref_frame_count,
  6818. sps->mb_width, sps->mb_height,
  6819. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  6820. sps->direct_8x8_inference_flag ? "8B8" : "",
  6821. sps->crop_left, sps->crop_right,
  6822. sps->crop_top, sps->crop_bottom,
  6823. sps->vui_parameters_present_flag ? "VUI" : ""
  6824. );
  6825. }
  6826. return 0;
  6827. }
  6828. static inline int decode_picture_parameter_set(H264Context *h, int bit_length){
  6829. MpegEncContext * const s = &h->s;
  6830. int pps_id= get_ue_golomb(&s->gb);
  6831. PPS *pps= &h->pps_buffer[pps_id];
  6832. pps->sps_id= get_ue_golomb(&s->gb);
  6833. pps->cabac= get_bits1(&s->gb);
  6834. pps->pic_order_present= get_bits1(&s->gb);
  6835. pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
  6836. if(pps->slice_group_count > 1 ){
  6837. pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
  6838. av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
  6839. switch(pps->mb_slice_group_map_type){
  6840. case 0:
  6841. #if 0
  6842. | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
  6843. | run_length[ i ] |1 |ue(v) |
  6844. #endif
  6845. break;
  6846. case 2:
  6847. #if 0
  6848. | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
  6849. |{ | | |
  6850. | top_left_mb[ i ] |1 |ue(v) |
  6851. | bottom_right_mb[ i ] |1 |ue(v) |
  6852. | } | | |
  6853. #endif
  6854. break;
  6855. case 3:
  6856. case 4:
  6857. case 5:
  6858. #if 0
  6859. | slice_group_change_direction_flag |1 |u(1) |
  6860. | slice_group_change_rate_minus1 |1 |ue(v) |
  6861. #endif
  6862. break;
  6863. case 6:
  6864. #if 0
  6865. | slice_group_id_cnt_minus1 |1 |ue(v) |
  6866. | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
  6867. |) | | |
  6868. | slice_group_id[ i ] |1 |u(v) |
  6869. #endif
  6870. break;
  6871. }
  6872. }
  6873. pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  6874. pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  6875. if(pps->ref_count[0] > 32 || pps->ref_count[1] > 32){
  6876. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
  6877. return -1;
  6878. }
  6879. pps->weighted_pred= get_bits1(&s->gb);
  6880. pps->weighted_bipred_idc= get_bits(&s->gb, 2);
  6881. pps->init_qp= get_se_golomb(&s->gb) + 26;
  6882. pps->init_qs= get_se_golomb(&s->gb) + 26;
  6883. pps->chroma_qp_index_offset= get_se_golomb(&s->gb);
  6884. pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
  6885. pps->constrained_intra_pred= get_bits1(&s->gb);
  6886. pps->redundant_pic_cnt_present = get_bits1(&s->gb);
  6887. pps->transform_8x8_mode= 0;
  6888. h->dequant_coeff_pps= -1; //contents of sps/pps can change even if id doesn't, so reinit
  6889. memset(pps->scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  6890. memset(pps->scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  6891. if(get_bits_count(&s->gb) < bit_length){
  6892. pps->transform_8x8_mode= get_bits1(&s->gb);
  6893. decode_scaling_matrices(h, &h->sps_buffer[pps->sps_id], pps, 0, pps->scaling_matrix4, pps->scaling_matrix8);
  6894. get_se_golomb(&s->gb); //second_chroma_qp_index_offset
  6895. }
  6896. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6897. 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",
  6898. pps_id, pps->sps_id,
  6899. pps->cabac ? "CABAC" : "CAVLC",
  6900. pps->slice_group_count,
  6901. pps->ref_count[0], pps->ref_count[1],
  6902. pps->weighted_pred ? "weighted" : "",
  6903. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset,
  6904. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  6905. pps->constrained_intra_pred ? "CONSTR" : "",
  6906. pps->redundant_pic_cnt_present ? "REDU" : "",
  6907. pps->transform_8x8_mode ? "8x8DCT" : ""
  6908. );
  6909. }
  6910. return 0;
  6911. }
  6912. /**
  6913. * finds the end of the current frame in the bitstream.
  6914. * @return the position of the first byte of the next frame, or -1
  6915. */
  6916. static int find_frame_end(H264Context *h, const uint8_t *buf, int buf_size){
  6917. int i;
  6918. uint32_t state;
  6919. ParseContext *pc = &(h->s.parse_context);
  6920. //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
  6921. // mb_addr= pc->mb_addr - 1;
  6922. state= pc->state;
  6923. for(i=0; i<=buf_size; i++){
  6924. if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  6925. tprintf("find_frame_end new startcode = %08x, frame_start_found = %d, pos = %d\n", state, pc->frame_start_found, i);
  6926. if(pc->frame_start_found){
  6927. // If there isn't one more byte in the buffer
  6928. // the test on first_mb_in_slice cannot be done yet
  6929. // do it at next call.
  6930. if (i >= buf_size) break;
  6931. if (buf[i] & 0x80) {
  6932. // first_mb_in_slice is 0, probably the first nal of a new
  6933. // slice
  6934. tprintf("find_frame_end frame_end_found, state = %08x, pos = %d\n", state, i);
  6935. pc->state=-1;
  6936. pc->frame_start_found= 0;
  6937. return i-4;
  6938. }
  6939. }
  6940. pc->frame_start_found = 1;
  6941. }
  6942. if((state&0xFFFFFF1F) == 0x107 || (state&0xFFFFFF1F) == 0x108 || (state&0xFFFFFF1F) == 0x109){
  6943. if(pc->frame_start_found){
  6944. pc->state=-1;
  6945. pc->frame_start_found= 0;
  6946. return i-4;
  6947. }
  6948. }
  6949. if (i<buf_size)
  6950. state= (state<<8) | buf[i];
  6951. }
  6952. pc->state= state;
  6953. return END_NOT_FOUND;
  6954. }
  6955. #ifdef CONFIG_H264_PARSER
  6956. static int h264_parse(AVCodecParserContext *s,
  6957. AVCodecContext *avctx,
  6958. uint8_t **poutbuf, int *poutbuf_size,
  6959. const uint8_t *buf, int buf_size)
  6960. {
  6961. H264Context *h = s->priv_data;
  6962. ParseContext *pc = &h->s.parse_context;
  6963. int next;
  6964. next= find_frame_end(h, buf, buf_size);
  6965. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  6966. *poutbuf = NULL;
  6967. *poutbuf_size = 0;
  6968. return buf_size;
  6969. }
  6970. *poutbuf = (uint8_t *)buf;
  6971. *poutbuf_size = buf_size;
  6972. return next;
  6973. }
  6974. static int h264_split(AVCodecContext *avctx,
  6975. const uint8_t *buf, int buf_size)
  6976. {
  6977. int i;
  6978. uint32_t state = -1;
  6979. int has_sps= 0;
  6980. for(i=0; i<=buf_size; i++){
  6981. if((state&0xFFFFFF1F) == 0x107)
  6982. has_sps=1;
  6983. /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  6984. }*/
  6985. if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
  6986. if(has_sps){
  6987. while(i>4 && buf[i-5]==0) i--;
  6988. return i-4;
  6989. }
  6990. }
  6991. if (i<buf_size)
  6992. state= (state<<8) | buf[i];
  6993. }
  6994. return 0;
  6995. }
  6996. #endif /* CONFIG_H264_PARSER */
  6997. static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){
  6998. MpegEncContext * const s = &h->s;
  6999. AVCodecContext * const avctx= s->avctx;
  7000. int buf_index=0;
  7001. #if 0
  7002. int i;
  7003. for(i=0; i<50; i++){
  7004. av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
  7005. }
  7006. #endif
  7007. h->slice_num = 0;
  7008. s->current_picture_ptr= NULL;
  7009. for(;;){
  7010. int consumed;
  7011. int dst_length;
  7012. int bit_length;
  7013. uint8_t *ptr;
  7014. int i, nalsize = 0;
  7015. if(h->is_avc) {
  7016. if(buf_index >= buf_size) break;
  7017. nalsize = 0;
  7018. for(i = 0; i < h->nal_length_size; i++)
  7019. nalsize = (nalsize << 8) | buf[buf_index++];
  7020. if(nalsize <= 1){
  7021. if(nalsize == 1){
  7022. buf_index++;
  7023. continue;
  7024. }else{
  7025. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  7026. break;
  7027. }
  7028. }
  7029. } else {
  7030. // start code prefix search
  7031. for(; buf_index + 3 < buf_size; buf_index++){
  7032. // this should allways succeed in the first iteration
  7033. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  7034. break;
  7035. }
  7036. if(buf_index+3 >= buf_size) break;
  7037. buf_index+=3;
  7038. }
  7039. ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, h->is_avc ? nalsize : buf_size - buf_index);
  7040. while(ptr[dst_length - 1] == 0 && dst_length > 1)
  7041. dst_length--;
  7042. bit_length= 8*dst_length - decode_rbsp_trailing(ptr + dst_length - 1);
  7043. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  7044. 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);
  7045. }
  7046. if (h->is_avc && (nalsize != consumed))
  7047. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  7048. buf_index += consumed;
  7049. if( (s->hurry_up == 1 && h->nal_ref_idc == 0) //FIXME dont discard SEI id
  7050. ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  7051. continue;
  7052. switch(h->nal_unit_type){
  7053. case NAL_IDR_SLICE:
  7054. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  7055. case NAL_SLICE:
  7056. init_get_bits(&s->gb, ptr, bit_length);
  7057. h->intra_gb_ptr=
  7058. h->inter_gb_ptr= &s->gb;
  7059. s->data_partitioning = 0;
  7060. if(decode_slice_header(h) < 0){
  7061. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  7062. break;
  7063. }
  7064. s->current_picture_ptr->key_frame= (h->nal_unit_type == NAL_IDR_SLICE);
  7065. if(h->redundant_pic_count==0 && s->hurry_up < 5
  7066. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  7067. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  7068. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  7069. && avctx->skip_frame < AVDISCARD_ALL)
  7070. decode_slice(h);
  7071. break;
  7072. case NAL_DPA:
  7073. init_get_bits(&s->gb, ptr, bit_length);
  7074. h->intra_gb_ptr=
  7075. h->inter_gb_ptr= NULL;
  7076. s->data_partitioning = 1;
  7077. if(decode_slice_header(h) < 0){
  7078. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  7079. }
  7080. break;
  7081. case NAL_DPB:
  7082. init_get_bits(&h->intra_gb, ptr, bit_length);
  7083. h->intra_gb_ptr= &h->intra_gb;
  7084. break;
  7085. case NAL_DPC:
  7086. init_get_bits(&h->inter_gb, ptr, bit_length);
  7087. h->inter_gb_ptr= &h->inter_gb;
  7088. if(h->redundant_pic_count==0 && h->intra_gb_ptr && s->data_partitioning
  7089. && s->hurry_up < 5
  7090. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  7091. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  7092. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  7093. && avctx->skip_frame < AVDISCARD_ALL)
  7094. decode_slice(h);
  7095. break;
  7096. case NAL_SEI:
  7097. init_get_bits(&s->gb, ptr, bit_length);
  7098. decode_sei(h);
  7099. break;
  7100. case NAL_SPS:
  7101. init_get_bits(&s->gb, ptr, bit_length);
  7102. decode_seq_parameter_set(h);
  7103. if(s->flags& CODEC_FLAG_LOW_DELAY)
  7104. s->low_delay=1;
  7105. if(avctx->has_b_frames < 2)
  7106. avctx->has_b_frames= !s->low_delay;
  7107. break;
  7108. case NAL_PPS:
  7109. init_get_bits(&s->gb, ptr, bit_length);
  7110. decode_picture_parameter_set(h, bit_length);
  7111. break;
  7112. case NAL_AUD:
  7113. case NAL_END_SEQUENCE:
  7114. case NAL_END_STREAM:
  7115. case NAL_FILLER_DATA:
  7116. case NAL_SPS_EXT:
  7117. case NAL_AUXILIARY_SLICE:
  7118. break;
  7119. default:
  7120. av_log(avctx, AV_LOG_ERROR, "Unknown NAL code: %d\n", h->nal_unit_type);
  7121. }
  7122. }
  7123. if(!s->current_picture_ptr) return buf_index; //no frame
  7124. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  7125. s->current_picture_ptr->pict_type= s->pict_type;
  7126. h->prev_frame_num_offset= h->frame_num_offset;
  7127. h->prev_frame_num= h->frame_num;
  7128. if(s->current_picture_ptr->reference){
  7129. h->prev_poc_msb= h->poc_msb;
  7130. h->prev_poc_lsb= h->poc_lsb;
  7131. }
  7132. if(s->current_picture_ptr->reference)
  7133. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  7134. ff_er_frame_end(s);
  7135. MPV_frame_end(s);
  7136. return buf_index;
  7137. }
  7138. /**
  7139. * returns the number of bytes consumed for building the current frame
  7140. */
  7141. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  7142. if(s->flags&CODEC_FLAG_TRUNCATED){
  7143. pos -= s->parse_context.last_index;
  7144. if(pos<0) pos=0; // FIXME remove (unneeded?)
  7145. return pos;
  7146. }else{
  7147. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  7148. if(pos+10>buf_size) pos=buf_size; // oops ;)
  7149. return pos;
  7150. }
  7151. }
  7152. static int decode_frame(AVCodecContext *avctx,
  7153. void *data, int *data_size,
  7154. uint8_t *buf, int buf_size)
  7155. {
  7156. H264Context *h = avctx->priv_data;
  7157. MpegEncContext *s = &h->s;
  7158. AVFrame *pict = data;
  7159. int buf_index;
  7160. s->flags= avctx->flags;
  7161. s->flags2= avctx->flags2;
  7162. /* no supplementary picture */
  7163. if (buf_size == 0) {
  7164. return 0;
  7165. }
  7166. if(s->flags&CODEC_FLAG_TRUNCATED){
  7167. int next= find_frame_end(h, buf, buf_size);
  7168. if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
  7169. return buf_size;
  7170. //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);
  7171. }
  7172. if(h->is_avc && !h->got_avcC) {
  7173. int i, cnt, nalsize;
  7174. unsigned char *p = avctx->extradata;
  7175. if(avctx->extradata_size < 7) {
  7176. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  7177. return -1;
  7178. }
  7179. if(*p != 1) {
  7180. av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
  7181. return -1;
  7182. }
  7183. /* sps and pps in the avcC always have length coded with 2 bytes,
  7184. so put a fake nal_length_size = 2 while parsing them */
  7185. h->nal_length_size = 2;
  7186. // Decode sps from avcC
  7187. cnt = *(p+5) & 0x1f; // Number of sps
  7188. p += 6;
  7189. for (i = 0; i < cnt; i++) {
  7190. nalsize = BE_16(p) + 2;
  7191. if(decode_nal_units(h, p, nalsize) < 0) {
  7192. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  7193. return -1;
  7194. }
  7195. p += nalsize;
  7196. }
  7197. // Decode pps from avcC
  7198. cnt = *(p++); // Number of pps
  7199. for (i = 0; i < cnt; i++) {
  7200. nalsize = BE_16(p) + 2;
  7201. if(decode_nal_units(h, p, nalsize) != nalsize) {
  7202. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  7203. return -1;
  7204. }
  7205. p += nalsize;
  7206. }
  7207. // Now store right nal length size, that will be use to parse all other nals
  7208. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  7209. // Do not reparse avcC
  7210. h->got_avcC = 1;
  7211. }
  7212. if(!h->is_avc && s->avctx->extradata_size && s->picture_number==0){
  7213. if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
  7214. return -1;
  7215. }
  7216. buf_index=decode_nal_units(h, buf, buf_size);
  7217. if(buf_index < 0)
  7218. return -1;
  7219. //FIXME do something with unavailable reference frames
  7220. // if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_index, buf_size);
  7221. if(!s->current_picture_ptr){
  7222. av_log(h->s.avctx, AV_LOG_DEBUG, "error, NO frame\n");
  7223. return -1;
  7224. }
  7225. {
  7226. Picture *out = s->current_picture_ptr;
  7227. #if 0 //decode order
  7228. *data_size = sizeof(AVFrame);
  7229. #else
  7230. /* Sort B-frames into display order */
  7231. Picture *cur = s->current_picture_ptr;
  7232. Picture *prev = h->delayed_output_pic;
  7233. int i, pics, cross_idr, out_of_order, out_idx;
  7234. if(h->sps.bitstream_restriction_flag
  7235. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  7236. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  7237. s->low_delay = 0;
  7238. }
  7239. pics = 0;
  7240. while(h->delayed_pic[pics]) pics++;
  7241. h->delayed_pic[pics++] = cur;
  7242. if(cur->reference == 0)
  7243. cur->reference = 1;
  7244. cross_idr = 0;
  7245. for(i=0; h->delayed_pic[i]; i++)
  7246. if(h->delayed_pic[i]->key_frame || h->delayed_pic[i]->poc==0)
  7247. cross_idr = 1;
  7248. out = h->delayed_pic[0];
  7249. out_idx = 0;
  7250. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
  7251. if(h->delayed_pic[i]->poc < out->poc){
  7252. out = h->delayed_pic[i];
  7253. out_idx = i;
  7254. }
  7255. out_of_order = !cross_idr && prev && out->poc < prev->poc;
  7256. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  7257. { }
  7258. else if(prev && pics <= s->avctx->has_b_frames)
  7259. out = prev;
  7260. else if((out_of_order && pics-1 == s->avctx->has_b_frames && pics < 15)
  7261. || (s->low_delay &&
  7262. ((!cross_idr && prev && out->poc > prev->poc + 2)
  7263. || cur->pict_type == B_TYPE)))
  7264. {
  7265. s->low_delay = 0;
  7266. s->avctx->has_b_frames++;
  7267. out = prev;
  7268. }
  7269. else if(out_of_order)
  7270. out = prev;
  7271. if(out_of_order || pics > s->avctx->has_b_frames){
  7272. for(i=out_idx; h->delayed_pic[i]; i++)
  7273. h->delayed_pic[i] = h->delayed_pic[i+1];
  7274. }
  7275. if(prev == out)
  7276. *data_size = 0;
  7277. else
  7278. *data_size = sizeof(AVFrame);
  7279. if(prev && prev != out && prev->reference == 1)
  7280. prev->reference = 0;
  7281. h->delayed_output_pic = out;
  7282. #endif
  7283. if(out)
  7284. *pict= *(AVFrame*)out;
  7285. else
  7286. av_log(avctx, AV_LOG_DEBUG, "no picture\n");
  7287. }
  7288. assert(pict->data[0] || !*data_size);
  7289. ff_print_debug_info(s, pict);
  7290. //printf("out %d\n", (int)pict->data[0]);
  7291. #if 0 //?
  7292. /* Return the Picture timestamp as the frame number */
  7293. /* we substract 1 because it is added on utils.c */
  7294. avctx->frame_number = s->picture_number - 1;
  7295. #endif
  7296. return get_consumed_bytes(s, buf_index, buf_size);
  7297. }
  7298. #if 0
  7299. static inline void fill_mb_avail(H264Context *h){
  7300. MpegEncContext * const s = &h->s;
  7301. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  7302. if(s->mb_y){
  7303. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  7304. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  7305. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  7306. }else{
  7307. h->mb_avail[0]=
  7308. h->mb_avail[1]=
  7309. h->mb_avail[2]= 0;
  7310. }
  7311. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  7312. h->mb_avail[4]= 1; //FIXME move out
  7313. h->mb_avail[5]= 0; //FIXME move out
  7314. }
  7315. #endif
  7316. #if 0 //selftest
  7317. #define COUNT 8000
  7318. #define SIZE (COUNT*40)
  7319. int main(){
  7320. int i;
  7321. uint8_t temp[SIZE];
  7322. PutBitContext pb;
  7323. GetBitContext gb;
  7324. // int int_temp[10000];
  7325. DSPContext dsp;
  7326. AVCodecContext avctx;
  7327. dsputil_init(&dsp, &avctx);
  7328. init_put_bits(&pb, temp, SIZE);
  7329. printf("testing unsigned exp golomb\n");
  7330. for(i=0; i<COUNT; i++){
  7331. START_TIMER
  7332. set_ue_golomb(&pb, i);
  7333. STOP_TIMER("set_ue_golomb");
  7334. }
  7335. flush_put_bits(&pb);
  7336. init_get_bits(&gb, temp, 8*SIZE);
  7337. for(i=0; i<COUNT; i++){
  7338. int j, s;
  7339. s= show_bits(&gb, 24);
  7340. START_TIMER
  7341. j= get_ue_golomb(&gb);
  7342. if(j != i){
  7343. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7344. // return -1;
  7345. }
  7346. STOP_TIMER("get_ue_golomb");
  7347. }
  7348. init_put_bits(&pb, temp, SIZE);
  7349. printf("testing signed exp golomb\n");
  7350. for(i=0; i<COUNT; i++){
  7351. START_TIMER
  7352. set_se_golomb(&pb, i - COUNT/2);
  7353. STOP_TIMER("set_se_golomb");
  7354. }
  7355. flush_put_bits(&pb);
  7356. init_get_bits(&gb, temp, 8*SIZE);
  7357. for(i=0; i<COUNT; i++){
  7358. int j, s;
  7359. s= show_bits(&gb, 24);
  7360. START_TIMER
  7361. j= get_se_golomb(&gb);
  7362. if(j != i - COUNT/2){
  7363. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7364. // return -1;
  7365. }
  7366. STOP_TIMER("get_se_golomb");
  7367. }
  7368. printf("testing 4x4 (I)DCT\n");
  7369. DCTELEM block[16];
  7370. uint8_t src[16], ref[16];
  7371. uint64_t error= 0, max_error=0;
  7372. for(i=0; i<COUNT; i++){
  7373. int j;
  7374. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  7375. for(j=0; j<16; j++){
  7376. ref[j]= random()%255;
  7377. src[j]= random()%255;
  7378. }
  7379. h264_diff_dct_c(block, src, ref, 4);
  7380. //normalize
  7381. for(j=0; j<16; j++){
  7382. // printf("%d ", block[j]);
  7383. block[j]= block[j]*4;
  7384. if(j&1) block[j]= (block[j]*4 + 2)/5;
  7385. if(j&4) block[j]= (block[j]*4 + 2)/5;
  7386. }
  7387. // printf("\n");
  7388. s->dsp.h264_idct_add(ref, block, 4);
  7389. /* for(j=0; j<16; j++){
  7390. printf("%d ", ref[j]);
  7391. }
  7392. printf("\n");*/
  7393. for(j=0; j<16; j++){
  7394. int diff= ABS(src[j] - ref[j]);
  7395. error+= diff*diff;
  7396. max_error= FFMAX(max_error, diff);
  7397. }
  7398. }
  7399. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  7400. #if 0
  7401. printf("testing quantizer\n");
  7402. for(qp=0; qp<52; qp++){
  7403. for(i=0; i<16; i++)
  7404. src1_block[i]= src2_block[i]= random()%255;
  7405. }
  7406. #endif
  7407. printf("Testing NAL layer\n");
  7408. uint8_t bitstream[COUNT];
  7409. uint8_t nal[COUNT*2];
  7410. H264Context h;
  7411. memset(&h, 0, sizeof(H264Context));
  7412. for(i=0; i<COUNT; i++){
  7413. int zeros= i;
  7414. int nal_length;
  7415. int consumed;
  7416. int out_length;
  7417. uint8_t *out;
  7418. int j;
  7419. for(j=0; j<COUNT; j++){
  7420. bitstream[j]= (random() % 255) + 1;
  7421. }
  7422. for(j=0; j<zeros; j++){
  7423. int pos= random() % COUNT;
  7424. while(bitstream[pos] == 0){
  7425. pos++;
  7426. pos %= COUNT;
  7427. }
  7428. bitstream[pos]=0;
  7429. }
  7430. START_TIMER
  7431. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  7432. if(nal_length<0){
  7433. printf("encoding failed\n");
  7434. return -1;
  7435. }
  7436. out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
  7437. STOP_TIMER("NAL")
  7438. if(out_length != COUNT){
  7439. printf("incorrect length %d %d\n", out_length, COUNT);
  7440. return -1;
  7441. }
  7442. if(consumed != nal_length){
  7443. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  7444. return -1;
  7445. }
  7446. if(memcmp(bitstream, out, COUNT)){
  7447. printf("missmatch\n");
  7448. return -1;
  7449. }
  7450. }
  7451. printf("Testing RBSP\n");
  7452. return 0;
  7453. }
  7454. #endif
  7455. static int decode_end(AVCodecContext *avctx)
  7456. {
  7457. H264Context *h = avctx->priv_data;
  7458. MpegEncContext *s = &h->s;
  7459. av_freep(&h->rbsp_buffer);
  7460. free_tables(h); //FIXME cleanup init stuff perhaps
  7461. MPV_common_end(s);
  7462. // memset(h, 0, sizeof(H264Context));
  7463. return 0;
  7464. }
  7465. AVCodec h264_decoder = {
  7466. "h264",
  7467. CODEC_TYPE_VIDEO,
  7468. CODEC_ID_H264,
  7469. sizeof(H264Context),
  7470. decode_init,
  7471. NULL,
  7472. decode_end,
  7473. decode_frame,
  7474. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  7475. .flush= flush_dpb,
  7476. };
  7477. #ifdef CONFIG_H264_PARSER
  7478. AVCodecParser h264_parser = {
  7479. { CODEC_ID_H264 },
  7480. sizeof(H264Context),
  7481. NULL,
  7482. h264_parse,
  7483. ff_parse_close,
  7484. h264_split,
  7485. };
  7486. #endif
  7487. #include "svq3.c"