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.

8642 lines
323KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. /**
  23. * @file h264.c
  24. * H.264 / AVC / MPEG4 part10 codec.
  25. * @author Michael Niedermayer <michaelni@gmx.at>
  26. */
  27. #include "common.h"
  28. #include "dsputil.h"
  29. #include "avcodec.h"
  30. #include "mpegvideo.h"
  31. #include "h264data.h"
  32. #include "golomb.h"
  33. #include "cabac.h"
  34. //#undef NDEBUG
  35. #include <assert.h>
  36. #define interlaced_dct interlaced_dct_is_a_bad_name
  37. #define mb_intra mb_intra_isnt_initalized_see_mb_type
  38. #define LUMA_DC_BLOCK_INDEX 25
  39. #define CHROMA_DC_BLOCK_INDEX 26
  40. #define CHROMA_DC_COEFF_TOKEN_VLC_BITS 8
  41. #define COEFF_TOKEN_VLC_BITS 8
  42. #define TOTAL_ZEROS_VLC_BITS 9
  43. #define CHROMA_DC_TOTAL_ZEROS_VLC_BITS 3
  44. #define RUN_VLC_BITS 3
  45. #define RUN7_VLC_BITS 6
  46. #define MAX_SPS_COUNT 32
  47. #define MAX_PPS_COUNT 256
  48. #define MAX_MMCO_COUNT 66
  49. /* Compiling in interlaced support reduces the speed
  50. * of progressive decoding by about 2%. */
  51. #define ALLOW_INTERLACE
  52. #ifdef ALLOW_INTERLACE
  53. #define MB_MBAFF h->mb_mbaff
  54. #define MB_FIELD h->mb_field_decoding_flag
  55. #define FRAME_MBAFF h->mb_aff_frame
  56. #else
  57. #define MB_MBAFF 0
  58. #define MB_FIELD 0
  59. #define FRAME_MBAFF 0
  60. #undef IS_INTERLACED
  61. #define IS_INTERLACED(mb_type) 0
  62. #endif
  63. /**
  64. * Sequence parameter set
  65. */
  66. typedef struct SPS{
  67. int profile_idc;
  68. int level_idc;
  69. int transform_bypass; ///< qpprime_y_zero_transform_bypass_flag
  70. int log2_max_frame_num; ///< log2_max_frame_num_minus4 + 4
  71. int poc_type; ///< pic_order_cnt_type
  72. int log2_max_poc_lsb; ///< log2_max_pic_order_cnt_lsb_minus4
  73. int delta_pic_order_always_zero_flag;
  74. int offset_for_non_ref_pic;
  75. int offset_for_top_to_bottom_field;
  76. int poc_cycle_length; ///< num_ref_frames_in_pic_order_cnt_cycle
  77. int ref_frame_count; ///< num_ref_frames
  78. int gaps_in_frame_num_allowed_flag;
  79. int mb_width; ///< frame_width_in_mbs_minus1 + 1
  80. int mb_height; ///< frame_height_in_mbs_minus1 + 1
  81. int frame_mbs_only_flag;
  82. int mb_aff; ///<mb_adaptive_frame_field_flag
  83. int direct_8x8_inference_flag;
  84. int crop; ///< frame_cropping_flag
  85. int crop_left; ///< frame_cropping_rect_left_offset
  86. int crop_right; ///< frame_cropping_rect_right_offset
  87. int crop_top; ///< frame_cropping_rect_top_offset
  88. int crop_bottom; ///< frame_cropping_rect_bottom_offset
  89. int vui_parameters_present_flag;
  90. AVRational sar;
  91. int timing_info_present_flag;
  92. uint32_t num_units_in_tick;
  93. uint32_t time_scale;
  94. int fixed_frame_rate_flag;
  95. short offset_for_ref_frame[256]; //FIXME dyn aloc?
  96. int bitstream_restriction_flag;
  97. int num_reorder_frames;
  98. int scaling_matrix_present;
  99. uint8_t scaling_matrix4[6][16];
  100. uint8_t scaling_matrix8[2][64];
  101. }SPS;
  102. /**
  103. * Picture parameter set
  104. */
  105. typedef struct PPS{
  106. int sps_id;
  107. int cabac; ///< entropy_coding_mode_flag
  108. int pic_order_present; ///< pic_order_present_flag
  109. int slice_group_count; ///< num_slice_groups_minus1 + 1
  110. int mb_slice_group_map_type;
  111. int ref_count[2]; ///< num_ref_idx_l0/1_active_minus1 + 1
  112. int weighted_pred; ///< weighted_pred_flag
  113. int weighted_bipred_idc;
  114. int init_qp; ///< pic_init_qp_minus26 + 26
  115. int init_qs; ///< pic_init_qs_minus26 + 26
  116. int chroma_qp_index_offset;
  117. int deblocking_filter_parameters_present; ///< deblocking_filter_parameters_present_flag
  118. int constrained_intra_pred; ///< constrained_intra_pred_flag
  119. int redundant_pic_cnt_present; ///< redundant_pic_cnt_present_flag
  120. int transform_8x8_mode; ///< transform_8x8_mode_flag
  121. uint8_t scaling_matrix4[6][16];
  122. uint8_t scaling_matrix8[2][64];
  123. }PPS;
  124. /**
  125. * Memory management control operation opcode.
  126. */
  127. typedef enum MMCOOpcode{
  128. MMCO_END=0,
  129. MMCO_SHORT2UNUSED,
  130. MMCO_LONG2UNUSED,
  131. MMCO_SHORT2LONG,
  132. MMCO_SET_MAX_LONG,
  133. MMCO_RESET,
  134. MMCO_LONG,
  135. } MMCOOpcode;
  136. /**
  137. * Memory management control operation.
  138. */
  139. typedef struct MMCO{
  140. MMCOOpcode opcode;
  141. int short_frame_num;
  142. int long_index;
  143. } MMCO;
  144. /**
  145. * H264Context
  146. */
  147. typedef struct H264Context{
  148. MpegEncContext s;
  149. int nal_ref_idc;
  150. int nal_unit_type;
  151. uint8_t *rbsp_buffer;
  152. unsigned int rbsp_buffer_size;
  153. /**
  154. * Used to parse AVC variant of h264
  155. */
  156. int is_avc; ///< this flag is != 0 if codec is avc1
  157. int got_avcC; ///< flag used to parse avcC data only once
  158. int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
  159. int chroma_qp; //QPc
  160. int prev_mb_skipped;
  161. int next_mb_skipped;
  162. //prediction stuff
  163. int chroma_pred_mode;
  164. int intra16x16_pred_mode;
  165. int top_mb_xy;
  166. int left_mb_xy[2];
  167. int8_t intra4x4_pred_mode_cache[5*8];
  168. int8_t (*intra4x4_pred_mode)[8];
  169. void (*pred4x4 [9+3])(uint8_t *src, uint8_t *topright, int stride);//FIXME move to dsp?
  170. void (*pred8x8l [9+3])(uint8_t *src, int topleft, int topright, int stride);
  171. void (*pred8x8 [4+3])(uint8_t *src, int stride);
  172. void (*pred16x16[4+3])(uint8_t *src, int stride);
  173. unsigned int topleft_samples_available;
  174. unsigned int top_samples_available;
  175. unsigned int topright_samples_available;
  176. unsigned int left_samples_available;
  177. uint8_t (*top_borders[2])[16+2*8];
  178. uint8_t left_border[2*(17+2*9)];
  179. /**
  180. * non zero coeff count cache.
  181. * is 64 if not available.
  182. */
  183. DECLARE_ALIGNED_8(uint8_t, non_zero_count_cache[6*8]);
  184. uint8_t (*non_zero_count)[16];
  185. /**
  186. * Motion vector cache.
  187. */
  188. DECLARE_ALIGNED_8(int16_t, mv_cache[2][5*8][2]);
  189. DECLARE_ALIGNED_8(int8_t, ref_cache[2][5*8]);
  190. #define LIST_NOT_USED -1 //FIXME rename?
  191. #define PART_NOT_AVAILABLE -2
  192. /**
  193. * is 1 if the specific list MV&references are set to 0,0,-2.
  194. */
  195. int mv_cache_clean[2];
  196. /**
  197. * number of neighbors (top and/or left) that used 8x8 dct
  198. */
  199. int neighbor_transform_size;
  200. /**
  201. * block_offset[ 0..23] for frame macroblocks
  202. * block_offset[24..47] for field macroblocks
  203. */
  204. int block_offset[2*(16+8)];
  205. uint32_t *mb2b_xy; //FIXME are these 4 a good idea?
  206. uint32_t *mb2b8_xy;
  207. int b_stride; //FIXME use s->b4_stride
  208. int b8_stride;
  209. int mb_linesize; ///< may be equal to s->linesize or s->linesize*2, for mbaff
  210. int mb_uvlinesize;
  211. int emu_edge_width;
  212. int emu_edge_height;
  213. int halfpel_flag;
  214. int thirdpel_flag;
  215. int unknown_svq3_flag;
  216. int next_slice_index;
  217. SPS sps_buffer[MAX_SPS_COUNT];
  218. SPS sps; ///< current sps
  219. PPS pps_buffer[MAX_PPS_COUNT];
  220. /**
  221. * current pps
  222. */
  223. PPS pps; //FIXME move to Picture perhaps? (->no) do we need that?
  224. uint32_t dequant4_buffer[6][52][16];
  225. uint32_t dequant8_buffer[2][52][64];
  226. uint32_t (*dequant4_coeff[6])[16];
  227. uint32_t (*dequant8_coeff[2])[64];
  228. int dequant_coeff_pps; ///< reinit tables when pps changes
  229. int slice_num;
  230. uint8_t *slice_table_base;
  231. uint8_t *slice_table; ///< slice_table_base + 2*mb_stride + 1
  232. int slice_type;
  233. int slice_type_fixed;
  234. //interlacing specific flags
  235. int mb_aff_frame;
  236. int mb_field_decoding_flag;
  237. int mb_mbaff; ///< mb_aff_frame && mb_field_decoding_flag
  238. int sub_mb_type[4];
  239. //POC stuff
  240. int poc_lsb;
  241. int poc_msb;
  242. int delta_poc_bottom;
  243. int delta_poc[2];
  244. int frame_num;
  245. int prev_poc_msb; ///< poc_msb of the last reference pic for POC type 0
  246. int prev_poc_lsb; ///< poc_lsb of the last reference pic for POC type 0
  247. int frame_num_offset; ///< for POC type 2
  248. int prev_frame_num_offset; ///< for POC type 2
  249. int prev_frame_num; ///< frame_num of the last pic for POC type 1/2
  250. /**
  251. * frame_num for frames or 2*frame_num for field pics.
  252. */
  253. int curr_pic_num;
  254. /**
  255. * max_frame_num or 2*max_frame_num for field pics.
  256. */
  257. int max_pic_num;
  258. //Weighted pred stuff
  259. int use_weight;
  260. int use_weight_chroma;
  261. int luma_log2_weight_denom;
  262. int chroma_log2_weight_denom;
  263. int luma_weight[2][48];
  264. int luma_offset[2][48];
  265. int chroma_weight[2][48][2];
  266. int chroma_offset[2][48][2];
  267. int implicit_weight[48][48];
  268. //deblock
  269. int deblocking_filter; ///< disable_deblocking_filter_idc with 1<->0
  270. int slice_alpha_c0_offset;
  271. int slice_beta_offset;
  272. int redundant_pic_count;
  273. int direct_spatial_mv_pred;
  274. int dist_scale_factor[16];
  275. int dist_scale_factor_field[32];
  276. int map_col_to_list0[2][16];
  277. int map_col_to_list0_field[2][32];
  278. /**
  279. * num_ref_idx_l0/1_active_minus1 + 1
  280. */
  281. int ref_count[2]; ///< counts frames or fields, depending on current mb mode
  282. Picture *short_ref[32];
  283. Picture *long_ref[32];
  284. Picture default_ref_list[2][32];
  285. Picture ref_list[2][48]; ///< 0..15: frame refs, 16..47: mbaff field refs
  286. Picture *delayed_pic[16]; //FIXME size?
  287. Picture *delayed_output_pic;
  288. /**
  289. * memory management control operations buffer.
  290. */
  291. MMCO mmco[MAX_MMCO_COUNT];
  292. int mmco_index;
  293. int long_ref_count; ///< number of actual long term references
  294. int short_ref_count; ///< number of actual short term references
  295. //data partitioning
  296. GetBitContext intra_gb;
  297. GetBitContext inter_gb;
  298. GetBitContext *intra_gb_ptr;
  299. GetBitContext *inter_gb_ptr;
  300. DECLARE_ALIGNED_8(DCTELEM, mb[16*24]);
  301. /**
  302. * Cabac
  303. */
  304. CABACContext cabac;
  305. uint8_t cabac_state[460];
  306. int cabac_init_idc;
  307. /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
  308. uint16_t *cbp_table;
  309. int cbp;
  310. int top_cbp;
  311. int left_cbp;
  312. /* chroma_pred_mode for i4x4 or i16x16, else 0 */
  313. uint8_t *chroma_pred_mode_table;
  314. int last_qscale_diff;
  315. int16_t (*mvd_table[2])[2];
  316. DECLARE_ALIGNED_8(int16_t, mvd_cache[2][5*8][2]);
  317. uint8_t *direct_table;
  318. uint8_t direct_cache[5*8];
  319. uint8_t zigzag_scan[16];
  320. uint8_t zigzag_scan8x8[64];
  321. uint8_t zigzag_scan8x8_cavlc[64];
  322. uint8_t field_scan[16];
  323. uint8_t field_scan8x8[64];
  324. uint8_t field_scan8x8_cavlc[64];
  325. const uint8_t *zigzag_scan_q0;
  326. const uint8_t *zigzag_scan8x8_q0;
  327. const uint8_t *zigzag_scan8x8_cavlc_q0;
  328. const uint8_t *field_scan_q0;
  329. const uint8_t *field_scan8x8_q0;
  330. const uint8_t *field_scan8x8_cavlc_q0;
  331. int x264_build;
  332. }H264Context;
  333. static VLC coeff_token_vlc[4];
  334. static VLC chroma_dc_coeff_token_vlc;
  335. static VLC total_zeros_vlc[15];
  336. static VLC chroma_dc_total_zeros_vlc[3];
  337. static VLC run_vlc[6];
  338. static VLC run7_vlc;
  339. static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
  340. static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
  341. 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);
  342. static void filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
  343. static always_inline uint32_t pack16to32(int a, int b){
  344. #ifdef WORDS_BIGENDIAN
  345. return (b&0xFFFF) + (a<<16);
  346. #else
  347. return (a&0xFFFF) + (b<<16);
  348. #endif
  349. }
  350. /**
  351. * fill a rectangle.
  352. * @param h height of the rectangle, should be a constant
  353. * @param w width of the rectangle, should be a constant
  354. * @param size the size of val (1 or 4), should be a constant
  355. */
  356. static always_inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){
  357. uint8_t *p= (uint8_t*)vp;
  358. assert(size==1 || size==4);
  359. assert(w<=4);
  360. w *= size;
  361. stride *= size;
  362. assert((((long)vp)&(FFMIN(w, STRIDE_ALIGN)-1)) == 0);
  363. assert((stride&(w-1))==0);
  364. if(w==2){
  365. const uint16_t v= size==4 ? val : val*0x0101;
  366. *(uint16_t*)(p + 0*stride)= v;
  367. if(h==1) return;
  368. *(uint16_t*)(p + 1*stride)= v;
  369. if(h==2) return;
  370. *(uint16_t*)(p + 2*stride)=
  371. *(uint16_t*)(p + 3*stride)= v;
  372. }else if(w==4){
  373. const uint32_t v= size==4 ? val : val*0x01010101;
  374. *(uint32_t*)(p + 0*stride)= v;
  375. if(h==1) return;
  376. *(uint32_t*)(p + 1*stride)= v;
  377. if(h==2) return;
  378. *(uint32_t*)(p + 2*stride)=
  379. *(uint32_t*)(p + 3*stride)= v;
  380. }else if(w==8){
  381. //gcc can't optimize 64bit math on x86_32
  382. #if defined(ARCH_X86_64) || (defined(MP_WORDSIZE) && MP_WORDSIZE >= 64)
  383. const uint64_t v= val*0x0100000001ULL;
  384. *(uint64_t*)(p + 0*stride)= v;
  385. if(h==1) return;
  386. *(uint64_t*)(p + 1*stride)= v;
  387. if(h==2) return;
  388. *(uint64_t*)(p + 2*stride)=
  389. *(uint64_t*)(p + 3*stride)= v;
  390. }else if(w==16){
  391. const uint64_t v= val*0x0100000001ULL;
  392. *(uint64_t*)(p + 0+0*stride)=
  393. *(uint64_t*)(p + 8+0*stride)=
  394. *(uint64_t*)(p + 0+1*stride)=
  395. *(uint64_t*)(p + 8+1*stride)= v;
  396. if(h==2) return;
  397. *(uint64_t*)(p + 0+2*stride)=
  398. *(uint64_t*)(p + 8+2*stride)=
  399. *(uint64_t*)(p + 0+3*stride)=
  400. *(uint64_t*)(p + 8+3*stride)= v;
  401. #else
  402. *(uint32_t*)(p + 0+0*stride)=
  403. *(uint32_t*)(p + 4+0*stride)= val;
  404. if(h==1) return;
  405. *(uint32_t*)(p + 0+1*stride)=
  406. *(uint32_t*)(p + 4+1*stride)= val;
  407. if(h==2) return;
  408. *(uint32_t*)(p + 0+2*stride)=
  409. *(uint32_t*)(p + 4+2*stride)=
  410. *(uint32_t*)(p + 0+3*stride)=
  411. *(uint32_t*)(p + 4+3*stride)= val;
  412. }else if(w==16){
  413. *(uint32_t*)(p + 0+0*stride)=
  414. *(uint32_t*)(p + 4+0*stride)=
  415. *(uint32_t*)(p + 8+0*stride)=
  416. *(uint32_t*)(p +12+0*stride)=
  417. *(uint32_t*)(p + 0+1*stride)=
  418. *(uint32_t*)(p + 4+1*stride)=
  419. *(uint32_t*)(p + 8+1*stride)=
  420. *(uint32_t*)(p +12+1*stride)= val;
  421. if(h==2) return;
  422. *(uint32_t*)(p + 0+2*stride)=
  423. *(uint32_t*)(p + 4+2*stride)=
  424. *(uint32_t*)(p + 8+2*stride)=
  425. *(uint32_t*)(p +12+2*stride)=
  426. *(uint32_t*)(p + 0+3*stride)=
  427. *(uint32_t*)(p + 4+3*stride)=
  428. *(uint32_t*)(p + 8+3*stride)=
  429. *(uint32_t*)(p +12+3*stride)= val;
  430. #endif
  431. }else
  432. assert(0);
  433. assert(h==4);
  434. }
  435. static void fill_caches(H264Context *h, int mb_type, int for_deblock){
  436. MpegEncContext * const s = &h->s;
  437. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  438. int topleft_xy, top_xy, topright_xy, left_xy[2];
  439. int topleft_type, top_type, topright_type, left_type[2];
  440. int left_block[8];
  441. int i;
  442. //FIXME deblocking could skip the intra and nnz parts.
  443. if(for_deblock && (h->slice_num == 1 || h->slice_table[mb_xy] == h->slice_table[mb_xy-s->mb_stride]) && !FRAME_MBAFF)
  444. return;
  445. //wow what a mess, why didn't they simplify the interlacing&intra stuff, i can't imagine that these complex rules are worth it
  446. top_xy = mb_xy - s->mb_stride;
  447. topleft_xy = top_xy - 1;
  448. topright_xy= top_xy + 1;
  449. left_xy[1] = left_xy[0] = mb_xy-1;
  450. left_block[0]= 0;
  451. left_block[1]= 1;
  452. left_block[2]= 2;
  453. left_block[3]= 3;
  454. left_block[4]= 7;
  455. left_block[5]= 10;
  456. left_block[6]= 8;
  457. left_block[7]= 11;
  458. if(FRAME_MBAFF){
  459. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  460. const int top_pair_xy = pair_xy - s->mb_stride;
  461. const int topleft_pair_xy = top_pair_xy - 1;
  462. const int topright_pair_xy = top_pair_xy + 1;
  463. const int topleft_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
  464. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  465. const int topright_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
  466. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  467. const int curr_mb_frame_flag = !IS_INTERLACED(mb_type);
  468. const int bottom = (s->mb_y & 1);
  469. 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);
  470. if (bottom
  471. ? !curr_mb_frame_flag // bottom macroblock
  472. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  473. ) {
  474. top_xy -= s->mb_stride;
  475. }
  476. if (bottom
  477. ? !curr_mb_frame_flag // bottom macroblock
  478. : (!curr_mb_frame_flag && !topleft_mb_frame_flag) // top macroblock
  479. ) {
  480. topleft_xy -= s->mb_stride;
  481. }
  482. if (bottom
  483. ? !curr_mb_frame_flag // bottom macroblock
  484. : (!curr_mb_frame_flag && !topright_mb_frame_flag) // top macroblock
  485. ) {
  486. topright_xy -= s->mb_stride;
  487. }
  488. if (left_mb_frame_flag != curr_mb_frame_flag) {
  489. left_xy[1] = left_xy[0] = pair_xy - 1;
  490. if (curr_mb_frame_flag) {
  491. if (bottom) {
  492. left_block[0]= 2;
  493. left_block[1]= 2;
  494. left_block[2]= 3;
  495. left_block[3]= 3;
  496. left_block[4]= 8;
  497. left_block[5]= 11;
  498. left_block[6]= 8;
  499. left_block[7]= 11;
  500. } else {
  501. left_block[0]= 0;
  502. left_block[1]= 0;
  503. left_block[2]= 1;
  504. left_block[3]= 1;
  505. left_block[4]= 7;
  506. left_block[5]= 10;
  507. left_block[6]= 7;
  508. left_block[7]= 10;
  509. }
  510. } else {
  511. left_xy[1] += s->mb_stride;
  512. //left_block[0]= 0;
  513. left_block[1]= 2;
  514. left_block[2]= 0;
  515. left_block[3]= 2;
  516. //left_block[4]= 7;
  517. left_block[5]= 10;
  518. left_block[6]= 7;
  519. left_block[7]= 10;
  520. }
  521. }
  522. }
  523. h->top_mb_xy = top_xy;
  524. h->left_mb_xy[0] = left_xy[0];
  525. h->left_mb_xy[1] = left_xy[1];
  526. if(for_deblock){
  527. topleft_type = 0;
  528. topright_type = 0;
  529. top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0;
  530. left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
  531. left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
  532. if(FRAME_MBAFF && !IS_INTRA(mb_type)){
  533. int list;
  534. int v = *(uint16_t*)&h->non_zero_count[mb_xy][14];
  535. for(i=0; i<16; i++)
  536. h->non_zero_count_cache[scan8[i]] = (v>>i)&1;
  537. for(list=0; list<1+(h->slice_type==B_TYPE); list++){
  538. if(USES_LIST(mb_type,list)){
  539. uint32_t *src = (uint32_t*)s->current_picture.motion_val[list][h->mb2b_xy[mb_xy]];
  540. uint32_t *dst = (uint32_t*)h->mv_cache[list][scan8[0]];
  541. int8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
  542. for(i=0; i<4; i++, dst+=8, src+=h->b_stride){
  543. dst[0] = src[0];
  544. dst[1] = src[1];
  545. dst[2] = src[2];
  546. dst[3] = src[3];
  547. }
  548. *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =
  549. *(uint32_t*)&h->ref_cache[list][scan8[ 2]] = pack16to32(ref[0],ref[1])*0x0101;
  550. ref += h->b8_stride;
  551. *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =
  552. *(uint32_t*)&h->ref_cache[list][scan8[10]] = pack16to32(ref[0],ref[1])*0x0101;
  553. }else{
  554. fill_rectangle(&h-> mv_cache[list][scan8[ 0]], 4, 4, 8, 0, 4);
  555. fill_rectangle(&h->ref_cache[list][scan8[ 0]], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  556. }
  557. }
  558. }
  559. }else{
  560. topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
  561. top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
  562. topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
  563. left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
  564. left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
  565. }
  566. if(IS_INTRA(mb_type)){
  567. h->topleft_samples_available=
  568. h->top_samples_available=
  569. h->left_samples_available= 0xFFFF;
  570. h->topright_samples_available= 0xEEEA;
  571. if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
  572. h->topleft_samples_available= 0xB3FF;
  573. h->top_samples_available= 0x33FF;
  574. h->topright_samples_available= 0x26EA;
  575. }
  576. for(i=0; i<2; i++){
  577. if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
  578. h->topleft_samples_available&= 0xDF5F;
  579. h->left_samples_available&= 0x5F5F;
  580. }
  581. }
  582. if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
  583. h->topleft_samples_available&= 0x7FFF;
  584. if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
  585. h->topright_samples_available&= 0xFBFF;
  586. if(IS_INTRA4x4(mb_type)){
  587. if(IS_INTRA4x4(top_type)){
  588. h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
  589. h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
  590. h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
  591. h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
  592. }else{
  593. int pred;
  594. if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred))
  595. pred= -1;
  596. else{
  597. pred= 2;
  598. }
  599. h->intra4x4_pred_mode_cache[4+8*0]=
  600. h->intra4x4_pred_mode_cache[5+8*0]=
  601. h->intra4x4_pred_mode_cache[6+8*0]=
  602. h->intra4x4_pred_mode_cache[7+8*0]= pred;
  603. }
  604. for(i=0; i<2; i++){
  605. if(IS_INTRA4x4(left_type[i])){
  606. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
  607. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
  608. }else{
  609. int pred;
  610. if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred))
  611. pred= -1;
  612. else{
  613. pred= 2;
  614. }
  615. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
  616. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
  617. }
  618. }
  619. }
  620. }
  621. /*
  622. 0 . T T. T T T T
  623. 1 L . .L . . . .
  624. 2 L . .L . . . .
  625. 3 . T TL . . . .
  626. 4 L . .L . . . .
  627. 5 L . .. . . . .
  628. */
  629. //FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec)
  630. if(top_type){
  631. h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
  632. h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
  633. h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
  634. h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
  635. h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
  636. h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
  637. h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
  638. h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
  639. }else{
  640. h->non_zero_count_cache[4+8*0]=
  641. h->non_zero_count_cache[5+8*0]=
  642. h->non_zero_count_cache[6+8*0]=
  643. h->non_zero_count_cache[7+8*0]=
  644. h->non_zero_count_cache[1+8*0]=
  645. h->non_zero_count_cache[2+8*0]=
  646. h->non_zero_count_cache[1+8*3]=
  647. h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  648. }
  649. for (i=0; i<2; i++) {
  650. if(left_type[i]){
  651. h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
  652. h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
  653. h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
  654. h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
  655. }else{
  656. h->non_zero_count_cache[3+8*1 + 2*8*i]=
  657. h->non_zero_count_cache[3+8*2 + 2*8*i]=
  658. h->non_zero_count_cache[0+8*1 + 8*i]=
  659. h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  660. }
  661. }
  662. if( h->pps.cabac ) {
  663. // top_cbp
  664. if(top_type) {
  665. h->top_cbp = h->cbp_table[top_xy];
  666. } else if(IS_INTRA(mb_type)) {
  667. h->top_cbp = 0x1C0;
  668. } else {
  669. h->top_cbp = 0;
  670. }
  671. // left_cbp
  672. if (left_type[0]) {
  673. h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
  674. } else if(IS_INTRA(mb_type)) {
  675. h->left_cbp = 0x1C0;
  676. } else {
  677. h->left_cbp = 0;
  678. }
  679. if (left_type[0]) {
  680. h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
  681. }
  682. if (left_type[1]) {
  683. h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
  684. }
  685. }
  686. #if 1
  687. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  688. int list;
  689. for(list=0; list<1+(h->slice_type==B_TYPE); list++){
  690. if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
  691. /*if(!h->mv_cache_clean[list]){
  692. memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
  693. memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
  694. h->mv_cache_clean[list]= 1;
  695. }*/
  696. continue;
  697. }
  698. h->mv_cache_clean[list]= 0;
  699. if(USES_LIST(top_type, list)){
  700. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  701. const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
  702. *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
  703. *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
  704. *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
  705. *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
  706. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  707. h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
  708. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  709. h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
  710. }else{
  711. *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
  712. *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
  713. *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
  714. *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
  715. *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
  716. }
  717. //FIXME unify cleanup or sth
  718. if(USES_LIST(left_type[0], list)){
  719. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  720. const int b8_xy= h->mb2b8_xy[left_xy[0]] + 1;
  721. *(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]];
  722. *(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]];
  723. h->ref_cache[list][scan8[0] - 1 + 0*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0]>>1)];
  724. h->ref_cache[list][scan8[0] - 1 + 1*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1]>>1)];
  725. }else{
  726. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 0*8]=
  727. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 1*8]= 0;
  728. h->ref_cache[list][scan8[0] - 1 + 0*8]=
  729. h->ref_cache[list][scan8[0] - 1 + 1*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  730. }
  731. if(USES_LIST(left_type[1], list)){
  732. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  733. const int b8_xy= h->mb2b8_xy[left_xy[1]] + 1;
  734. *(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]];
  735. *(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]];
  736. h->ref_cache[list][scan8[0] - 1 + 2*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[2]>>1)];
  737. h->ref_cache[list][scan8[0] - 1 + 3*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[3]>>1)];
  738. }else{
  739. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 2*8]=
  740. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 3*8]= 0;
  741. h->ref_cache[list][scan8[0] - 1 + 2*8]=
  742. h->ref_cache[list][scan8[0] - 1 + 3*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  743. assert((!left_type[0]) == (!left_type[1]));
  744. }
  745. if((for_deblock || (IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred)) && !FRAME_MBAFF)
  746. continue;
  747. if(USES_LIST(topleft_type, list)){
  748. const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
  749. const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + h->b8_stride;
  750. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  751. h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  752. }else{
  753. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
  754. h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  755. }
  756. if(USES_LIST(topright_type, list)){
  757. const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
  758. const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
  759. *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  760. h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  761. }else{
  762. *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
  763. h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  764. }
  765. if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)
  766. continue;
  767. h->ref_cache[list][scan8[5 ]+1] =
  768. h->ref_cache[list][scan8[7 ]+1] =
  769. h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewhere else)
  770. h->ref_cache[list][scan8[4 ]] =
  771. h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
  772. *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
  773. *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
  774. *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  775. *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
  776. *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
  777. if( h->pps.cabac ) {
  778. /* XXX beurk, Load mvd */
  779. if(USES_LIST(top_type, list)){
  780. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  781. *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
  782. *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
  783. *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
  784. *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
  785. }else{
  786. *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
  787. *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
  788. *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
  789. *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
  790. }
  791. if(USES_LIST(left_type[0], list)){
  792. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  793. *(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]];
  794. *(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]];
  795. }else{
  796. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
  797. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
  798. }
  799. if(USES_LIST(left_type[1], list)){
  800. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  801. *(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]];
  802. *(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]];
  803. }else{
  804. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
  805. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
  806. }
  807. *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
  808. *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
  809. *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  810. *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
  811. *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
  812. if(h->slice_type == B_TYPE){
  813. fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
  814. if(IS_DIRECT(top_type)){
  815. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
  816. }else if(IS_8X8(top_type)){
  817. int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
  818. h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
  819. h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
  820. }else{
  821. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
  822. }
  823. if(IS_DIRECT(left_type[0]))
  824. h->direct_cache[scan8[0] - 1 + 0*8]= 1;
  825. else if(IS_8X8(left_type[0]))
  826. 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)];
  827. else
  828. h->direct_cache[scan8[0] - 1 + 0*8]= 0;
  829. if(IS_DIRECT(left_type[1]))
  830. h->direct_cache[scan8[0] - 1 + 2*8]= 1;
  831. else if(IS_8X8(left_type[1]))
  832. 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)];
  833. else
  834. h->direct_cache[scan8[0] - 1 + 2*8]= 0;
  835. }
  836. }
  837. if(FRAME_MBAFF){
  838. #define MAP_MVS\
  839. MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
  840. MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
  841. MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
  842. MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
  843. MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
  844. MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
  845. MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\
  846. MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\
  847. MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\
  848. MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
  849. if(MB_FIELD){
  850. #define MAP_F2F(idx, mb_type)\
  851. if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  852. h->ref_cache[list][idx] <<= 1;\
  853. h->mv_cache[list][idx][1] /= 2;\
  854. h->mvd_cache[list][idx][1] /= 2;\
  855. }
  856. MAP_MVS
  857. #undef MAP_F2F
  858. }else{
  859. #define MAP_F2F(idx, mb_type)\
  860. if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  861. h->ref_cache[list][idx] >>= 1;\
  862. h->mv_cache[list][idx][1] <<= 1;\
  863. h->mvd_cache[list][idx][1] <<= 1;\
  864. }
  865. MAP_MVS
  866. #undef MAP_F2F
  867. }
  868. }
  869. }
  870. }
  871. #endif
  872. h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
  873. }
  874. static inline void write_back_intra_pred_mode(H264Context *h){
  875. MpegEncContext * const s = &h->s;
  876. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  877. h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
  878. h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
  879. h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
  880. h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
  881. h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
  882. h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
  883. h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
  884. }
  885. /**
  886. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  887. */
  888. static inline int check_intra4x4_pred_mode(H264Context *h){
  889. MpegEncContext * const s = &h->s;
  890. static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
  891. static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
  892. int i;
  893. if(!(h->top_samples_available&0x8000)){
  894. for(i=0; i<4; i++){
  895. int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
  896. if(status<0){
  897. 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);
  898. return -1;
  899. } else if(status){
  900. h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
  901. }
  902. }
  903. }
  904. if(!(h->left_samples_available&0x8000)){
  905. for(i=0; i<4; i++){
  906. int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
  907. if(status<0){
  908. 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);
  909. return -1;
  910. } else if(status){
  911. h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
  912. }
  913. }
  914. }
  915. return 0;
  916. } //FIXME cleanup like next
  917. /**
  918. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  919. */
  920. static inline int check_intra_pred_mode(H264Context *h, int mode){
  921. MpegEncContext * const s = &h->s;
  922. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  923. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  924. if(mode < 0 || mode > 6) {
  925. 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);
  926. return -1;
  927. }
  928. if(!(h->top_samples_available&0x8000)){
  929. mode= top[ mode ];
  930. if(mode<0){
  931. 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);
  932. return -1;
  933. }
  934. }
  935. if(!(h->left_samples_available&0x8000)){
  936. mode= left[ mode ];
  937. if(mode<0){
  938. 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);
  939. return -1;
  940. }
  941. }
  942. return mode;
  943. }
  944. /**
  945. * gets the predicted intra4x4 prediction mode.
  946. */
  947. static inline int pred_intra_mode(H264Context *h, int n){
  948. const int index8= scan8[n];
  949. const int left= h->intra4x4_pred_mode_cache[index8 - 1];
  950. const int top = h->intra4x4_pred_mode_cache[index8 - 8];
  951. const int min= FFMIN(left, top);
  952. tprintf("mode:%d %d min:%d\n", left ,top, min);
  953. if(min<0) return DC_PRED;
  954. else return min;
  955. }
  956. static inline void write_back_non_zero_count(H264Context *h){
  957. MpegEncContext * const s = &h->s;
  958. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  959. h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
  960. h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
  961. h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
  962. h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
  963. h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
  964. h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
  965. h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
  966. h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
  967. h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
  968. h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
  969. h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
  970. h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
  971. h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
  972. if(FRAME_MBAFF){
  973. // store all luma nnzs, for deblocking
  974. int v = 0, i;
  975. for(i=0; i<16; i++)
  976. v += (!!h->non_zero_count_cache[scan8[i]]) << i;
  977. *(uint16_t*)&h->non_zero_count[mb_xy][14] = v;
  978. }
  979. }
  980. /**
  981. * gets the predicted number of non zero coefficients.
  982. * @param n block index
  983. */
  984. static inline int pred_non_zero_count(H264Context *h, int n){
  985. const int index8= scan8[n];
  986. const int left= h->non_zero_count_cache[index8 - 1];
  987. const int top = h->non_zero_count_cache[index8 - 8];
  988. int i= left + top;
  989. if(i<64) i= (i+1)>>1;
  990. tprintf("pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
  991. return i&31;
  992. }
  993. static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
  994. const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
  995. /* there is no consistent mapping of mvs to neighboring locations that will
  996. * make mbaff happy, so we can't move all this logic to fill_caches */
  997. if(FRAME_MBAFF){
  998. MpegEncContext *s = &h->s;
  999. const uint32_t *mb_types = s->current_picture_ptr->mb_type;
  1000. const int16_t *mv;
  1001. *(uint32_t*)h->mv_cache[list][scan8[0]-2] = 0;
  1002. *C = h->mv_cache[list][scan8[0]-2];
  1003. if(!MB_FIELD
  1004. && (s->mb_y&1) && i < scan8[0]+8 && topright_ref != PART_NOT_AVAILABLE){
  1005. int topright_xy = s->mb_x + (s->mb_y-1)*s->mb_stride + (i == scan8[0]+3);
  1006. if(IS_INTERLACED(mb_types[topright_xy])){
  1007. #define SET_DIAG_MV(MV_OP, REF_OP, X4, Y4)\
  1008. const int x4 = X4, y4 = Y4;\
  1009. const int mb_type = mb_types[(x4>>2)+(y4>>2)*s->mb_stride];\
  1010. if(!USES_LIST(mb_type,list) && !IS_8X8(mb_type))\
  1011. return LIST_NOT_USED;\
  1012. mv = s->current_picture_ptr->motion_val[list][x4 + y4*h->b_stride];\
  1013. h->mv_cache[list][scan8[0]-2][0] = mv[0];\
  1014. h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
  1015. return s->current_picture_ptr->ref_index[list][(x4>>1) + (y4>>1)*h->b8_stride] REF_OP;
  1016. SET_DIAG_MV(*2, >>1, s->mb_x*4+(i&7)-4+part_width, s->mb_y*4-1);
  1017. }
  1018. }
  1019. if(topright_ref == PART_NOT_AVAILABLE
  1020. && ((s->mb_y&1) || i >= scan8[0]+8) && (i&7)==4
  1021. && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
  1022. if(!MB_FIELD
  1023. && IS_INTERLACED(mb_types[h->left_mb_xy[0]])){
  1024. SET_DIAG_MV(*2, >>1, s->mb_x*4-1, (s->mb_y|1)*4+(s->mb_y&1)*2+(i>>4)-1);
  1025. }
  1026. if(MB_FIELD
  1027. && !IS_INTERLACED(mb_types[h->left_mb_xy[0]])
  1028. && i >= scan8[0]+8){
  1029. // leftshift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's ok.
  1030. SET_DIAG_MV(>>1, <<1, s->mb_x*4-1, (s->mb_y&~1)*4 - 1 + ((i-scan8[0])>>3)*2);
  1031. }
  1032. }
  1033. #undef SET_DIAG_MV
  1034. }
  1035. if(topright_ref != PART_NOT_AVAILABLE){
  1036. *C= h->mv_cache[list][ i - 8 + part_width ];
  1037. return topright_ref;
  1038. }else{
  1039. tprintf("topright MV not available\n");
  1040. *C= h->mv_cache[list][ i - 8 - 1 ];
  1041. return h->ref_cache[list][ i - 8 - 1 ];
  1042. }
  1043. }
  1044. /**
  1045. * gets the predicted MV.
  1046. * @param n the block index
  1047. * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
  1048. * @param mx the x component of the predicted motion vector
  1049. * @param my the y component of the predicted motion vector
  1050. */
  1051. static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
  1052. const int index8= scan8[n];
  1053. const int top_ref= h->ref_cache[list][ index8 - 8 ];
  1054. const int left_ref= h->ref_cache[list][ index8 - 1 ];
  1055. const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
  1056. const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
  1057. const int16_t * C;
  1058. int diagonal_ref, match_count;
  1059. assert(part_width==1 || part_width==2 || part_width==4);
  1060. /* mv_cache
  1061. B . . A T T T T
  1062. U . . L . . , .
  1063. U . . L . . . .
  1064. U . . L . . , .
  1065. . . . L . . . .
  1066. */
  1067. diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
  1068. match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
  1069. tprintf("pred_motion match_count=%d\n", match_count);
  1070. if(match_count > 1){ //most common
  1071. *mx= mid_pred(A[0], B[0], C[0]);
  1072. *my= mid_pred(A[1], B[1], C[1]);
  1073. }else if(match_count==1){
  1074. if(left_ref==ref){
  1075. *mx= A[0];
  1076. *my= A[1];
  1077. }else if(top_ref==ref){
  1078. *mx= B[0];
  1079. *my= B[1];
  1080. }else{
  1081. *mx= C[0];
  1082. *my= C[1];
  1083. }
  1084. }else{
  1085. if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
  1086. *mx= A[0];
  1087. *my= A[1];
  1088. }else{
  1089. *mx= mid_pred(A[0], B[0], C[0]);
  1090. *my= mid_pred(A[1], B[1], C[1]);
  1091. }
  1092. }
  1093. 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);
  1094. }
  1095. /**
  1096. * gets the directionally predicted 16x8 MV.
  1097. * @param n the block index
  1098. * @param mx the x component of the predicted motion vector
  1099. * @param my the y component of the predicted motion vector
  1100. */
  1101. static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  1102. if(n==0){
  1103. const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
  1104. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  1105. 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);
  1106. if(top_ref == ref){
  1107. *mx= B[0];
  1108. *my= B[1];
  1109. return;
  1110. }
  1111. }else{
  1112. const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
  1113. const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
  1114. 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);
  1115. if(left_ref == ref){
  1116. *mx= A[0];
  1117. *my= A[1];
  1118. return;
  1119. }
  1120. }
  1121. //RARE
  1122. pred_motion(h, n, 4, list, ref, mx, my);
  1123. }
  1124. /**
  1125. * gets the directionally predicted 8x16 MV.
  1126. * @param n the block index
  1127. * @param mx the x component of the predicted motion vector
  1128. * @param my the y component of the predicted motion vector
  1129. */
  1130. static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  1131. if(n==0){
  1132. const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
  1133. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  1134. 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);
  1135. if(left_ref == ref){
  1136. *mx= A[0];
  1137. *my= A[1];
  1138. return;
  1139. }
  1140. }else{
  1141. const int16_t * C;
  1142. int diagonal_ref;
  1143. diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
  1144. 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);
  1145. if(diagonal_ref == ref){
  1146. *mx= C[0];
  1147. *my= C[1];
  1148. return;
  1149. }
  1150. }
  1151. //RARE
  1152. pred_motion(h, n, 2, list, ref, mx, my);
  1153. }
  1154. static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
  1155. const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
  1156. const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
  1157. tprintf("pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
  1158. if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
  1159. || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
  1160. || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
  1161. *mx = *my = 0;
  1162. return;
  1163. }
  1164. pred_motion(h, 0, 4, 0, 0, mx, my);
  1165. return;
  1166. }
  1167. static inline void direct_dist_scale_factor(H264Context * const h){
  1168. const int poc = h->s.current_picture_ptr->poc;
  1169. const int poc1 = h->ref_list[1][0].poc;
  1170. int i;
  1171. for(i=0; i<h->ref_count[0]; i++){
  1172. int poc0 = h->ref_list[0][i].poc;
  1173. int td = clip(poc1 - poc0, -128, 127);
  1174. if(td == 0 /* FIXME || pic0 is a long-term ref */){
  1175. h->dist_scale_factor[i] = 256;
  1176. }else{
  1177. int tb = clip(poc - poc0, -128, 127);
  1178. int tx = (16384 + (FFABS(td) >> 1)) / td;
  1179. h->dist_scale_factor[i] = clip((tb*tx + 32) >> 6, -1024, 1023);
  1180. }
  1181. }
  1182. if(FRAME_MBAFF){
  1183. for(i=0; i<h->ref_count[0]; i++){
  1184. h->dist_scale_factor_field[2*i] =
  1185. h->dist_scale_factor_field[2*i+1] = h->dist_scale_factor[i];
  1186. }
  1187. }
  1188. }
  1189. static inline void direct_ref_list_init(H264Context * const h){
  1190. MpegEncContext * const s = &h->s;
  1191. Picture * const ref1 = &h->ref_list[1][0];
  1192. Picture * const cur = s->current_picture_ptr;
  1193. int list, i, j;
  1194. if(cur->pict_type == I_TYPE)
  1195. cur->ref_count[0] = 0;
  1196. if(cur->pict_type != B_TYPE)
  1197. cur->ref_count[1] = 0;
  1198. for(list=0; list<2; list++){
  1199. cur->ref_count[list] = h->ref_count[list];
  1200. for(j=0; j<h->ref_count[list]; j++)
  1201. cur->ref_poc[list][j] = h->ref_list[list][j].poc;
  1202. }
  1203. if(cur->pict_type != B_TYPE || h->direct_spatial_mv_pred)
  1204. return;
  1205. for(list=0; list<2; list++){
  1206. for(i=0; i<ref1->ref_count[list]; i++){
  1207. const int poc = ref1->ref_poc[list][i];
  1208. h->map_col_to_list0[list][i] = 0; /* bogus; fills in for missing frames */
  1209. for(j=0; j<h->ref_count[list]; j++)
  1210. if(h->ref_list[list][j].poc == poc){
  1211. h->map_col_to_list0[list][i] = j;
  1212. break;
  1213. }
  1214. }
  1215. }
  1216. if(FRAME_MBAFF){
  1217. for(list=0; list<2; list++){
  1218. for(i=0; i<ref1->ref_count[list]; i++){
  1219. j = h->map_col_to_list0[list][i];
  1220. h->map_col_to_list0_field[list][2*i] = 2*j;
  1221. h->map_col_to_list0_field[list][2*i+1] = 2*j+1;
  1222. }
  1223. }
  1224. }
  1225. }
  1226. static inline void pred_direct_motion(H264Context * const h, int *mb_type){
  1227. MpegEncContext * const s = &h->s;
  1228. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  1229. const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1230. const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1231. const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
  1232. const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
  1233. const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[1][b4_xy];
  1234. const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
  1235. const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy];
  1236. const int is_b8x8 = IS_8X8(*mb_type);
  1237. int sub_mb_type;
  1238. int i8, i4;
  1239. #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
  1240. if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
  1241. /* FIXME save sub mb types from previous frames (or derive from MVs)
  1242. * so we know exactly what block size to use */
  1243. sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  1244. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  1245. }else if(!is_b8x8 && (mb_type_col & MB_TYPE_16x16_OR_INTRA)){
  1246. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  1247. *mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
  1248. }else{
  1249. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  1250. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  1251. }
  1252. if(!is_b8x8)
  1253. *mb_type |= MB_TYPE_DIRECT2;
  1254. if(MB_FIELD)
  1255. *mb_type |= MB_TYPE_INTERLACED;
  1256. 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);
  1257. if(h->direct_spatial_mv_pred){
  1258. int ref[2];
  1259. int mv[2][2];
  1260. int list;
  1261. /* FIXME interlacing + spatial direct uses wrong colocated block positions */
  1262. /* ref = min(neighbors) */
  1263. for(list=0; list<2; list++){
  1264. int refa = h->ref_cache[list][scan8[0] - 1];
  1265. int refb = h->ref_cache[list][scan8[0] - 8];
  1266. int refc = h->ref_cache[list][scan8[0] - 8 + 4];
  1267. if(refc == -2)
  1268. refc = h->ref_cache[list][scan8[0] - 8 - 1];
  1269. ref[list] = refa;
  1270. if(ref[list] < 0 || (refb < ref[list] && refb >= 0))
  1271. ref[list] = refb;
  1272. if(ref[list] < 0 || (refc < ref[list] && refc >= 0))
  1273. ref[list] = refc;
  1274. if(ref[list] < 0)
  1275. ref[list] = -1;
  1276. }
  1277. if(ref[0] < 0 && ref[1] < 0){
  1278. ref[0] = ref[1] = 0;
  1279. mv[0][0] = mv[0][1] =
  1280. mv[1][0] = mv[1][1] = 0;
  1281. }else{
  1282. for(list=0; list<2; list++){
  1283. if(ref[list] >= 0)
  1284. pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
  1285. else
  1286. mv[list][0] = mv[list][1] = 0;
  1287. }
  1288. }
  1289. if(ref[1] < 0){
  1290. *mb_type &= ~MB_TYPE_P0L1;
  1291. sub_mb_type &= ~MB_TYPE_P0L1;
  1292. }else if(ref[0] < 0){
  1293. *mb_type &= ~MB_TYPE_P0L0;
  1294. sub_mb_type &= ~MB_TYPE_P0L0;
  1295. }
  1296. if(IS_16X16(*mb_type)){
  1297. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  1298. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  1299. if(!IS_INTRA(mb_type_col)
  1300. && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
  1301. || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
  1302. && (h->x264_build>33 || !h->x264_build)))){
  1303. if(ref[0] > 0)
  1304. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1305. else
  1306. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  1307. if(ref[1] > 0)
  1308. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1309. else
  1310. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  1311. }else{
  1312. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1313. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1314. }
  1315. }else{
  1316. for(i8=0; i8<4; i8++){
  1317. const int x8 = i8&1;
  1318. const int y8 = i8>>1;
  1319. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1320. continue;
  1321. h->sub_mb_type[i8] = sub_mb_type;
  1322. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1323. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1324. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  1325. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  1326. /* col_zero_flag */
  1327. if(!IS_INTRA(mb_type_col) && ( l1ref0[x8 + y8*h->b8_stride] == 0
  1328. || (l1ref0[x8 + y8*h->b8_stride] < 0 && l1ref1[x8 + y8*h->b8_stride] == 0
  1329. && (h->x264_build>33 || !h->x264_build)))){
  1330. const int16_t (*l1mv)[2]= l1ref0[x8 + y8*h->b8_stride] == 0 ? l1mv0 : l1mv1;
  1331. if(IS_SUB_8X8(sub_mb_type)){
  1332. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1333. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  1334. if(ref[0] == 0)
  1335. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1336. if(ref[1] == 0)
  1337. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1338. }
  1339. }else
  1340. for(i4=0; i4<4; i4++){
  1341. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1342. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  1343. if(ref[0] == 0)
  1344. *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
  1345. if(ref[1] == 0)
  1346. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
  1347. }
  1348. }
  1349. }
  1350. }
  1351. }
  1352. }else{ /* direct temporal mv pred */
  1353. const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
  1354. const int *dist_scale_factor = h->dist_scale_factor;
  1355. if(FRAME_MBAFF){
  1356. if(IS_INTERLACED(*mb_type)){
  1357. map_col_to_list0[0] = h->map_col_to_list0_field[0];
  1358. map_col_to_list0[1] = h->map_col_to_list0_field[1];
  1359. dist_scale_factor = h->dist_scale_factor_field;
  1360. }
  1361. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col)){
  1362. /* FIXME assumes direct_8x8_inference == 1 */
  1363. const int pair_xy = s->mb_x + (s->mb_y&~1)*s->mb_stride;
  1364. int mb_types_col[2];
  1365. int y_shift;
  1366. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1
  1367. | (is_b8x8 ? 0 : MB_TYPE_DIRECT2)
  1368. | (*mb_type & MB_TYPE_INTERLACED);
  1369. sub_mb_type = MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_16x16;
  1370. if(IS_INTERLACED(*mb_type)){
  1371. /* frame to field scaling */
  1372. mb_types_col[0] = h->ref_list[1][0].mb_type[pair_xy];
  1373. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1374. if(s->mb_y&1){
  1375. l1ref0 -= 2*h->b8_stride;
  1376. l1ref1 -= 2*h->b8_stride;
  1377. l1mv0 -= 4*h->b_stride;
  1378. l1mv1 -= 4*h->b_stride;
  1379. }
  1380. y_shift = 0;
  1381. if( (mb_types_col[0] & MB_TYPE_16x16_OR_INTRA)
  1382. && (mb_types_col[1] & MB_TYPE_16x16_OR_INTRA)
  1383. && !is_b8x8)
  1384. *mb_type |= MB_TYPE_16x8;
  1385. else
  1386. *mb_type |= MB_TYPE_8x8;
  1387. }else{
  1388. /* field to frame scaling */
  1389. /* col_mb_y = (mb_y&~1) + (topAbsDiffPOC < bottomAbsDiffPOC ? 0 : 1)
  1390. * but in MBAFF, top and bottom POC are equal */
  1391. int dy = (s->mb_y&1) ? 1 : 2;
  1392. mb_types_col[0] =
  1393. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1394. l1ref0 += dy*h->b8_stride;
  1395. l1ref1 += dy*h->b8_stride;
  1396. l1mv0 += 2*dy*h->b_stride;
  1397. l1mv1 += 2*dy*h->b_stride;
  1398. y_shift = 2;
  1399. if((mb_types_col[0] & (MB_TYPE_16x16_OR_INTRA|MB_TYPE_16x8))
  1400. && !is_b8x8)
  1401. *mb_type |= MB_TYPE_16x16;
  1402. else
  1403. *mb_type |= MB_TYPE_8x8;
  1404. }
  1405. for(i8=0; i8<4; i8++){
  1406. const int x8 = i8&1;
  1407. const int y8 = i8>>1;
  1408. int ref0, scale;
  1409. const int16_t (*l1mv)[2]= l1mv0;
  1410. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1411. continue;
  1412. h->sub_mb_type[i8] = sub_mb_type;
  1413. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1414. if(IS_INTRA(mb_types_col[y8])){
  1415. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1416. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1417. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1418. continue;
  1419. }
  1420. ref0 = l1ref0[x8 + (y8*2>>y_shift)*h->b8_stride];
  1421. if(ref0 >= 0)
  1422. ref0 = map_col_to_list0[0][ref0*2>>y_shift];
  1423. else{
  1424. ref0 = map_col_to_list0[1][l1ref1[x8 + (y8*2>>y_shift)*h->b8_stride]*2>>y_shift];
  1425. l1mv= l1mv1;
  1426. }
  1427. scale = dist_scale_factor[ref0];
  1428. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1429. {
  1430. const int16_t *mv_col = l1mv[x8*3 + (y8*6>>y_shift)*h->b_stride];
  1431. int my_col = (mv_col[1]<<y_shift)/2;
  1432. int mx = (scale * mv_col[0] + 128) >> 8;
  1433. int my = (scale * my_col + 128) >> 8;
  1434. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1435. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
  1436. }
  1437. }
  1438. return;
  1439. }
  1440. }
  1441. /* one-to-one mv scaling */
  1442. if(IS_16X16(*mb_type)){
  1443. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  1444. if(IS_INTRA(mb_type_col)){
  1445. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  1446. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  1447. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  1448. }else{
  1449. const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0]]
  1450. : map_col_to_list0[1][l1ref1[0]];
  1451. const int scale = dist_scale_factor[ref0];
  1452. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  1453. int mv_l0[2];
  1454. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1455. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1456. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref0, 1);
  1457. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0],mv_l0[1]), 4);
  1458. 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);
  1459. }
  1460. }else{
  1461. for(i8=0; i8<4; i8++){
  1462. const int x8 = i8&1;
  1463. const int y8 = i8>>1;
  1464. int ref0, scale;
  1465. const int16_t (*l1mv)[2]= l1mv0;
  1466. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1467. continue;
  1468. h->sub_mb_type[i8] = sub_mb_type;
  1469. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1470. if(IS_INTRA(mb_type_col)){
  1471. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1472. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1473. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1474. continue;
  1475. }
  1476. ref0 = l1ref0[x8 + y8*h->b8_stride];
  1477. if(ref0 >= 0)
  1478. ref0 = map_col_to_list0[0][ref0];
  1479. else{
  1480. ref0 = map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]];
  1481. l1mv= l1mv1;
  1482. }
  1483. scale = dist_scale_factor[ref0];
  1484. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1485. if(IS_SUB_8X8(sub_mb_type)){
  1486. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1487. int mx = (scale * mv_col[0] + 128) >> 8;
  1488. int my = (scale * mv_col[1] + 128) >> 8;
  1489. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1490. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
  1491. }else
  1492. for(i4=0; i4<4; i4++){
  1493. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1494. int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
  1495. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1496. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1497. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
  1498. pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  1499. }
  1500. }
  1501. }
  1502. }
  1503. }
  1504. static inline void write_back_motion(H264Context *h, int mb_type){
  1505. MpegEncContext * const s = &h->s;
  1506. const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1507. const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1508. int list;
  1509. if(!USES_LIST(mb_type, 0))
  1510. fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, h->b8_stride, (uint8_t)LIST_NOT_USED, 1);
  1511. for(list=0; list<2; list++){
  1512. int y;
  1513. if(!USES_LIST(mb_type, list))
  1514. continue;
  1515. for(y=0; y<4; y++){
  1516. *(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];
  1517. *(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];
  1518. }
  1519. if( h->pps.cabac ) {
  1520. if(IS_SKIP(mb_type))
  1521. fill_rectangle(h->mvd_table[list][b_xy], 4, 4, h->b_stride, 0, 4);
  1522. else
  1523. for(y=0; y<4; y++){
  1524. *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
  1525. *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
  1526. }
  1527. }
  1528. {
  1529. int8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
  1530. ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]];
  1531. ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]];
  1532. ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]];
  1533. ref_index[1+1*h->b8_stride]= h->ref_cache[list][scan8[12]];
  1534. }
  1535. }
  1536. if(h->slice_type == B_TYPE && h->pps.cabac){
  1537. if(IS_8X8(mb_type)){
  1538. uint8_t *direct_table = &h->direct_table[b8_xy];
  1539. direct_table[1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
  1540. direct_table[0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
  1541. direct_table[1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
  1542. }
  1543. }
  1544. }
  1545. /**
  1546. * Decodes a network abstraction layer unit.
  1547. * @param consumed is the number of bytes used as input
  1548. * @param length is the length of the array
  1549. * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp tailing?
  1550. * @returns decoded bytes, might be src+1 if no escapes
  1551. */
  1552. static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){
  1553. int i, si, di;
  1554. uint8_t *dst;
  1555. // src[0]&0x80; //forbidden bit
  1556. h->nal_ref_idc= src[0]>>5;
  1557. h->nal_unit_type= src[0]&0x1F;
  1558. src++; length--;
  1559. #if 0
  1560. for(i=0; i<length; i++)
  1561. printf("%2X ", src[i]);
  1562. #endif
  1563. for(i=0; i+1<length; i+=2){
  1564. if(src[i]) continue;
  1565. if(i>0 && src[i-1]==0) i--;
  1566. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  1567. if(src[i+2]!=3){
  1568. /* startcode, so we must be past the end */
  1569. length=i;
  1570. }
  1571. break;
  1572. }
  1573. }
  1574. if(i>=length-1){ //no escaped 0
  1575. *dst_length= length;
  1576. *consumed= length+1; //+1 for the header
  1577. return src;
  1578. }
  1579. h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length);
  1580. dst= h->rbsp_buffer;
  1581. //printf("decoding esc\n");
  1582. si=di=0;
  1583. while(si<length){
  1584. //remove escapes (very rare 1:2^22)
  1585. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  1586. if(src[si+2]==3){ //escape
  1587. dst[di++]= 0;
  1588. dst[di++]= 0;
  1589. si+=3;
  1590. continue;
  1591. }else //next start code
  1592. break;
  1593. }
  1594. dst[di++]= src[si++];
  1595. }
  1596. *dst_length= di;
  1597. *consumed= si + 1;//+1 for the header
  1598. //FIXME store exact number of bits in the getbitcontext (its needed for decoding)
  1599. return dst;
  1600. }
  1601. #if 0
  1602. /**
  1603. * @param src the data which should be escaped
  1604. * @param dst the target buffer, dst+1 == src is allowed as a special case
  1605. * @param length the length of the src data
  1606. * @param dst_length the length of the dst array
  1607. * @returns length of escaped data in bytes or -1 if an error occured
  1608. */
  1609. static int encode_nal(H264Context *h, uint8_t *dst, uint8_t *src, int length, int dst_length){
  1610. int i, escape_count, si, di;
  1611. uint8_t *temp;
  1612. assert(length>=0);
  1613. assert(dst_length>0);
  1614. dst[0]= (h->nal_ref_idc<<5) + h->nal_unit_type;
  1615. if(length==0) return 1;
  1616. escape_count= 0;
  1617. for(i=0; i<length; i+=2){
  1618. if(src[i]) continue;
  1619. if(i>0 && src[i-1]==0)
  1620. i--;
  1621. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  1622. escape_count++;
  1623. i+=2;
  1624. }
  1625. }
  1626. if(escape_count==0){
  1627. if(dst+1 != src)
  1628. memcpy(dst+1, src, length);
  1629. return length + 1;
  1630. }
  1631. if(length + escape_count + 1> dst_length)
  1632. return -1;
  1633. //this should be damn rare (hopefully)
  1634. h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length + escape_count);
  1635. temp= h->rbsp_buffer;
  1636. //printf("encoding esc\n");
  1637. si= 0;
  1638. di= 0;
  1639. while(si < length){
  1640. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  1641. temp[di++]= 0; si++;
  1642. temp[di++]= 0; si++;
  1643. temp[di++]= 3;
  1644. temp[di++]= src[si++];
  1645. }
  1646. else
  1647. temp[di++]= src[si++];
  1648. }
  1649. memcpy(dst+1, temp, length+escape_count);
  1650. assert(di == length+escape_count);
  1651. return di + 1;
  1652. }
  1653. /**
  1654. * write 1,10,100,1000,... for alignment, yes its exactly inverse to mpeg4
  1655. */
  1656. static void encode_rbsp_trailing(PutBitContext *pb){
  1657. int length;
  1658. put_bits(pb, 1, 1);
  1659. length= (-put_bits_count(pb))&7;
  1660. if(length) put_bits(pb, length, 0);
  1661. }
  1662. #endif
  1663. /**
  1664. * identifies the exact end of the bitstream
  1665. * @return the length of the trailing, or 0 if damaged
  1666. */
  1667. static int decode_rbsp_trailing(uint8_t *src){
  1668. int v= *src;
  1669. int r;
  1670. tprintf("rbsp trailing %X\n", v);
  1671. for(r=1; r<9; r++){
  1672. if(v&1) return r;
  1673. v>>=1;
  1674. }
  1675. return 0;
  1676. }
  1677. /**
  1678. * idct tranforms the 16 dc values and dequantize them.
  1679. * @param qp quantization parameter
  1680. */
  1681. static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1682. #define stride 16
  1683. int i;
  1684. int temp[16]; //FIXME check if this is a good idea
  1685. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1686. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1687. //memset(block, 64, 2*256);
  1688. //return;
  1689. for(i=0; i<4; i++){
  1690. const int offset= y_offset[i];
  1691. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1692. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1693. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1694. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1695. temp[4*i+0]= z0+z3;
  1696. temp[4*i+1]= z1+z2;
  1697. temp[4*i+2]= z1-z2;
  1698. temp[4*i+3]= z0-z3;
  1699. }
  1700. for(i=0; i<4; i++){
  1701. const int offset= x_offset[i];
  1702. const int z0= temp[4*0+i] + temp[4*2+i];
  1703. const int z1= temp[4*0+i] - temp[4*2+i];
  1704. const int z2= temp[4*1+i] - temp[4*3+i];
  1705. const int z3= temp[4*1+i] + temp[4*3+i];
  1706. block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_resdual
  1707. block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
  1708. block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
  1709. block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
  1710. }
  1711. }
  1712. #if 0
  1713. /**
  1714. * dct tranforms the 16 dc values.
  1715. * @param qp quantization parameter ??? FIXME
  1716. */
  1717. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  1718. // const int qmul= dequant_coeff[qp][0];
  1719. int i;
  1720. int temp[16]; //FIXME check if this is a good idea
  1721. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1722. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1723. for(i=0; i<4; i++){
  1724. const int offset= y_offset[i];
  1725. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1726. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1727. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1728. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1729. temp[4*i+0]= z0+z3;
  1730. temp[4*i+1]= z1+z2;
  1731. temp[4*i+2]= z1-z2;
  1732. temp[4*i+3]= z0-z3;
  1733. }
  1734. for(i=0; i<4; i++){
  1735. const int offset= x_offset[i];
  1736. const int z0= temp[4*0+i] + temp[4*2+i];
  1737. const int z1= temp[4*0+i] - temp[4*2+i];
  1738. const int z2= temp[4*1+i] - temp[4*3+i];
  1739. const int z3= temp[4*1+i] + temp[4*3+i];
  1740. block[stride*0 +offset]= (z0 + z3)>>1;
  1741. block[stride*2 +offset]= (z1 + z2)>>1;
  1742. block[stride*8 +offset]= (z1 - z2)>>1;
  1743. block[stride*10+offset]= (z0 - z3)>>1;
  1744. }
  1745. }
  1746. #endif
  1747. #undef xStride
  1748. #undef stride
  1749. static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1750. const int stride= 16*2;
  1751. const int xStride= 16;
  1752. int a,b,c,d,e;
  1753. a= block[stride*0 + xStride*0];
  1754. b= block[stride*0 + xStride*1];
  1755. c= block[stride*1 + xStride*0];
  1756. d= block[stride*1 + xStride*1];
  1757. e= a-b;
  1758. a= a+b;
  1759. b= c-d;
  1760. c= c+d;
  1761. block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
  1762. block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
  1763. block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
  1764. block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
  1765. }
  1766. #if 0
  1767. static void chroma_dc_dct_c(DCTELEM *block){
  1768. const int stride= 16*2;
  1769. const int xStride= 16;
  1770. int a,b,c,d,e;
  1771. a= block[stride*0 + xStride*0];
  1772. b= block[stride*0 + xStride*1];
  1773. c= block[stride*1 + xStride*0];
  1774. d= block[stride*1 + xStride*1];
  1775. e= a-b;
  1776. a= a+b;
  1777. b= c-d;
  1778. c= c+d;
  1779. block[stride*0 + xStride*0]= (a+c);
  1780. block[stride*0 + xStride*1]= (e+b);
  1781. block[stride*1 + xStride*0]= (a-c);
  1782. block[stride*1 + xStride*1]= (e-b);
  1783. }
  1784. #endif
  1785. /**
  1786. * gets the chroma qp.
  1787. */
  1788. static inline int get_chroma_qp(int chroma_qp_index_offset, int qscale){
  1789. return chroma_qp[clip(qscale + chroma_qp_index_offset, 0, 51)];
  1790. }
  1791. #if 0
  1792. static void h264_diff_dct_c(DCTELEM *block, uint8_t *src1, uint8_t *src2, int stride){
  1793. int i;
  1794. //FIXME try int temp instead of block
  1795. for(i=0; i<4; i++){
  1796. const int d0= src1[0 + i*stride] - src2[0 + i*stride];
  1797. const int d1= src1[1 + i*stride] - src2[1 + i*stride];
  1798. const int d2= src1[2 + i*stride] - src2[2 + i*stride];
  1799. const int d3= src1[3 + i*stride] - src2[3 + i*stride];
  1800. const int z0= d0 + d3;
  1801. const int z3= d0 - d3;
  1802. const int z1= d1 + d2;
  1803. const int z2= d1 - d2;
  1804. block[0 + 4*i]= z0 + z1;
  1805. block[1 + 4*i]= 2*z3 + z2;
  1806. block[2 + 4*i]= z0 - z1;
  1807. block[3 + 4*i]= z3 - 2*z2;
  1808. }
  1809. for(i=0; i<4; i++){
  1810. const int z0= block[0*4 + i] + block[3*4 + i];
  1811. const int z3= block[0*4 + i] - block[3*4 + i];
  1812. const int z1= block[1*4 + i] + block[2*4 + i];
  1813. const int z2= block[1*4 + i] - block[2*4 + i];
  1814. block[0*4 + i]= z0 + z1;
  1815. block[1*4 + i]= 2*z3 + z2;
  1816. block[2*4 + i]= z0 - z1;
  1817. block[3*4 + i]= z3 - 2*z2;
  1818. }
  1819. }
  1820. #endif
  1821. //FIXME need to check that this doesnt overflow signed 32 bit for low qp, i am not sure, it's very close
  1822. //FIXME check that gcc inlines this (and optimizes intra & seperate_dc stuff away)
  1823. static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int seperate_dc){
  1824. int i;
  1825. const int * const quant_table= quant_coeff[qscale];
  1826. const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
  1827. const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
  1828. const unsigned int threshold2= (threshold1<<1);
  1829. int last_non_zero;
  1830. if(seperate_dc){
  1831. if(qscale<=18){
  1832. //avoid overflows
  1833. const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
  1834. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
  1835. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1836. int level= block[0]*quant_coeff[qscale+18][0];
  1837. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1838. if(level>0){
  1839. level= (dc_bias + level)>>(QUANT_SHIFT-2);
  1840. block[0]= level;
  1841. }else{
  1842. level= (dc_bias - level)>>(QUANT_SHIFT-2);
  1843. block[0]= -level;
  1844. }
  1845. // last_non_zero = i;
  1846. }else{
  1847. block[0]=0;
  1848. }
  1849. }else{
  1850. const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
  1851. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
  1852. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1853. int level= block[0]*quant_table[0];
  1854. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1855. if(level>0){
  1856. level= (dc_bias + level)>>(QUANT_SHIFT+1);
  1857. block[0]= level;
  1858. }else{
  1859. level= (dc_bias - level)>>(QUANT_SHIFT+1);
  1860. block[0]= -level;
  1861. }
  1862. // last_non_zero = i;
  1863. }else{
  1864. block[0]=0;
  1865. }
  1866. }
  1867. last_non_zero= 0;
  1868. i=1;
  1869. }else{
  1870. last_non_zero= -1;
  1871. i=0;
  1872. }
  1873. for(; i<16; i++){
  1874. const int j= scantable[i];
  1875. int level= block[j]*quant_table[j];
  1876. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  1877. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  1878. if(((unsigned)(level+threshold1))>threshold2){
  1879. if(level>0){
  1880. level= (bias + level)>>QUANT_SHIFT;
  1881. block[j]= level;
  1882. }else{
  1883. level= (bias - level)>>QUANT_SHIFT;
  1884. block[j]= -level;
  1885. }
  1886. last_non_zero = i;
  1887. }else{
  1888. block[j]=0;
  1889. }
  1890. }
  1891. return last_non_zero;
  1892. }
  1893. static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){
  1894. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1895. ((uint32_t*)(src+0*stride))[0]= a;
  1896. ((uint32_t*)(src+1*stride))[0]= a;
  1897. ((uint32_t*)(src+2*stride))[0]= a;
  1898. ((uint32_t*)(src+3*stride))[0]= a;
  1899. }
  1900. static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){
  1901. ((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101;
  1902. ((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101;
  1903. ((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101;
  1904. ((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101;
  1905. }
  1906. static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1907. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
  1908. + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
  1909. ((uint32_t*)(src+0*stride))[0]=
  1910. ((uint32_t*)(src+1*stride))[0]=
  1911. ((uint32_t*)(src+2*stride))[0]=
  1912. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1913. }
  1914. static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1915. const int dc= ( src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
  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_top_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1922. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[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_128_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1929. ((uint32_t*)(src+0*stride))[0]=
  1930. ((uint32_t*)(src+1*stride))[0]=
  1931. ((uint32_t*)(src+2*stride))[0]=
  1932. ((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U;
  1933. }
  1934. #define LOAD_TOP_RIGHT_EDGE\
  1935. const int t4= topright[0];\
  1936. const int t5= topright[1];\
  1937. const int t6= topright[2];\
  1938. const int t7= topright[3];\
  1939. #define LOAD_LEFT_EDGE\
  1940. const int l0= src[-1+0*stride];\
  1941. const int l1= src[-1+1*stride];\
  1942. const int l2= src[-1+2*stride];\
  1943. const int l3= src[-1+3*stride];\
  1944. #define LOAD_TOP_EDGE\
  1945. const int t0= src[ 0-1*stride];\
  1946. const int t1= src[ 1-1*stride];\
  1947. const int t2= src[ 2-1*stride];\
  1948. const int t3= src[ 3-1*stride];\
  1949. static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){
  1950. const int lt= src[-1-1*stride];
  1951. LOAD_TOP_EDGE
  1952. LOAD_LEFT_EDGE
  1953. src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
  1954. src[0+2*stride]=
  1955. src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
  1956. src[0+1*stride]=
  1957. src[1+2*stride]=
  1958. src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
  1959. src[0+0*stride]=
  1960. src[1+1*stride]=
  1961. src[2+2*stride]=
  1962. src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1963. src[1+0*stride]=
  1964. src[2+1*stride]=
  1965. src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1966. src[2+0*stride]=
  1967. src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1968. src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1969. }
  1970. static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){
  1971. LOAD_TOP_EDGE
  1972. LOAD_TOP_RIGHT_EDGE
  1973. // LOAD_LEFT_EDGE
  1974. src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
  1975. src[1+0*stride]=
  1976. src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
  1977. src[2+0*stride]=
  1978. src[1+1*stride]=
  1979. src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
  1980. src[3+0*stride]=
  1981. src[2+1*stride]=
  1982. src[1+2*stride]=
  1983. src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
  1984. src[3+1*stride]=
  1985. src[2+2*stride]=
  1986. src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
  1987. src[3+2*stride]=
  1988. src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
  1989. src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
  1990. }
  1991. static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
  1992. const int lt= src[-1-1*stride];
  1993. LOAD_TOP_EDGE
  1994. LOAD_LEFT_EDGE
  1995. const __attribute__((unused)) int unu= l3;
  1996. src[0+0*stride]=
  1997. src[1+2*stride]=(lt + t0 + 1)>>1;
  1998. src[1+0*stride]=
  1999. src[2+2*stride]=(t0 + t1 + 1)>>1;
  2000. src[2+0*stride]=
  2001. src[3+2*stride]=(t1 + t2 + 1)>>1;
  2002. src[3+0*stride]=(t2 + t3 + 1)>>1;
  2003. src[0+1*stride]=
  2004. src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  2005. src[1+1*stride]=
  2006. src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
  2007. src[2+1*stride]=
  2008. src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  2009. src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  2010. src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  2011. src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  2012. }
  2013. static void pred4x4_vertical_left_c(uint8_t *src, uint8_t *topright, int stride){
  2014. LOAD_TOP_EDGE
  2015. LOAD_TOP_RIGHT_EDGE
  2016. const __attribute__((unused)) int unu= t7;
  2017. src[0+0*stride]=(t0 + t1 + 1)>>1;
  2018. src[1+0*stride]=
  2019. src[0+2*stride]=(t1 + t2 + 1)>>1;
  2020. src[2+0*stride]=
  2021. src[1+2*stride]=(t2 + t3 + 1)>>1;
  2022. src[3+0*stride]=
  2023. src[2+2*stride]=(t3 + t4+ 1)>>1;
  2024. src[3+2*stride]=(t4 + t5+ 1)>>1;
  2025. src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  2026. src[1+1*stride]=
  2027. src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  2028. src[2+1*stride]=
  2029. src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
  2030. src[3+1*stride]=
  2031. src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
  2032. src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
  2033. }
  2034. static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){
  2035. LOAD_LEFT_EDGE
  2036. src[0+0*stride]=(l0 + l1 + 1)>>1;
  2037. src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  2038. src[2+0*stride]=
  2039. src[0+1*stride]=(l1 + l2 + 1)>>1;
  2040. src[3+0*stride]=
  2041. src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  2042. src[2+1*stride]=
  2043. src[0+2*stride]=(l2 + l3 + 1)>>1;
  2044. src[3+1*stride]=
  2045. src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
  2046. src[3+2*stride]=
  2047. src[1+3*stride]=
  2048. src[0+3*stride]=
  2049. src[2+2*stride]=
  2050. src[2+3*stride]=
  2051. src[3+3*stride]=l3;
  2052. }
  2053. static void pred4x4_horizontal_down_c(uint8_t *src, uint8_t *topright, int stride){
  2054. const int lt= src[-1-1*stride];
  2055. LOAD_TOP_EDGE
  2056. LOAD_LEFT_EDGE
  2057. const __attribute__((unused)) int unu= t3;
  2058. src[0+0*stride]=
  2059. src[2+1*stride]=(lt + l0 + 1)>>1;
  2060. src[1+0*stride]=
  2061. src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
  2062. src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
  2063. src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  2064. src[0+1*stride]=
  2065. src[2+2*stride]=(l0 + l1 + 1)>>1;
  2066. src[1+1*stride]=
  2067. src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  2068. src[0+2*stride]=
  2069. src[2+3*stride]=(l1 + l2+ 1)>>1;
  2070. src[1+2*stride]=
  2071. src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  2072. src[0+3*stride]=(l2 + l3 + 1)>>1;
  2073. src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  2074. }
  2075. static void pred16x16_vertical_c(uint8_t *src, int stride){
  2076. int i;
  2077. const uint32_t a= ((uint32_t*)(src-stride))[0];
  2078. const uint32_t b= ((uint32_t*)(src-stride))[1];
  2079. const uint32_t c= ((uint32_t*)(src-stride))[2];
  2080. const uint32_t d= ((uint32_t*)(src-stride))[3];
  2081. for(i=0; i<16; i++){
  2082. ((uint32_t*)(src+i*stride))[0]= a;
  2083. ((uint32_t*)(src+i*stride))[1]= b;
  2084. ((uint32_t*)(src+i*stride))[2]= c;
  2085. ((uint32_t*)(src+i*stride))[3]= d;
  2086. }
  2087. }
  2088. static void pred16x16_horizontal_c(uint8_t *src, int stride){
  2089. int i;
  2090. for(i=0; i<16; i++){
  2091. ((uint32_t*)(src+i*stride))[0]=
  2092. ((uint32_t*)(src+i*stride))[1]=
  2093. ((uint32_t*)(src+i*stride))[2]=
  2094. ((uint32_t*)(src+i*stride))[3]= src[-1+i*stride]*0x01010101;
  2095. }
  2096. }
  2097. static void pred16x16_dc_c(uint8_t *src, int stride){
  2098. int i, dc=0;
  2099. for(i=0;i<16; i++){
  2100. dc+= src[-1+i*stride];
  2101. }
  2102. for(i=0;i<16; i++){
  2103. dc+= src[i-stride];
  2104. }
  2105. dc= 0x01010101*((dc + 16)>>5);
  2106. for(i=0; i<16; i++){
  2107. ((uint32_t*)(src+i*stride))[0]=
  2108. ((uint32_t*)(src+i*stride))[1]=
  2109. ((uint32_t*)(src+i*stride))[2]=
  2110. ((uint32_t*)(src+i*stride))[3]= dc;
  2111. }
  2112. }
  2113. static void pred16x16_left_dc_c(uint8_t *src, int stride){
  2114. int i, dc=0;
  2115. for(i=0;i<16; i++){
  2116. dc+= src[-1+i*stride];
  2117. }
  2118. dc= 0x01010101*((dc + 8)>>4);
  2119. for(i=0; i<16; i++){
  2120. ((uint32_t*)(src+i*stride))[0]=
  2121. ((uint32_t*)(src+i*stride))[1]=
  2122. ((uint32_t*)(src+i*stride))[2]=
  2123. ((uint32_t*)(src+i*stride))[3]= dc;
  2124. }
  2125. }
  2126. static void pred16x16_top_dc_c(uint8_t *src, int stride){
  2127. int i, dc=0;
  2128. for(i=0;i<16; i++){
  2129. dc+= src[i-stride];
  2130. }
  2131. dc= 0x01010101*((dc + 8)>>4);
  2132. for(i=0; i<16; i++){
  2133. ((uint32_t*)(src+i*stride))[0]=
  2134. ((uint32_t*)(src+i*stride))[1]=
  2135. ((uint32_t*)(src+i*stride))[2]=
  2136. ((uint32_t*)(src+i*stride))[3]= dc;
  2137. }
  2138. }
  2139. static void pred16x16_128_dc_c(uint8_t *src, int stride){
  2140. int i;
  2141. for(i=0; i<16; i++){
  2142. ((uint32_t*)(src+i*stride))[0]=
  2143. ((uint32_t*)(src+i*stride))[1]=
  2144. ((uint32_t*)(src+i*stride))[2]=
  2145. ((uint32_t*)(src+i*stride))[3]= 0x01010101U*128U;
  2146. }
  2147. }
  2148. static inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){
  2149. int i, j, k;
  2150. int a;
  2151. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  2152. const uint8_t * const src0 = src+7-stride;
  2153. const uint8_t *src1 = src+8*stride-1;
  2154. const uint8_t *src2 = src1-2*stride; // == src+6*stride-1;
  2155. int H = src0[1] - src0[-1];
  2156. int V = src1[0] - src2[ 0];
  2157. for(k=2; k<=8; ++k) {
  2158. src1 += stride; src2 -= stride;
  2159. H += k*(src0[k] - src0[-k]);
  2160. V += k*(src1[0] - src2[ 0]);
  2161. }
  2162. if(svq3){
  2163. H = ( 5*(H/4) ) / 16;
  2164. V = ( 5*(V/4) ) / 16;
  2165. /* required for 100% accuracy */
  2166. i = H; H = V; V = i;
  2167. }else{
  2168. H = ( 5*H+32 ) >> 6;
  2169. V = ( 5*V+32 ) >> 6;
  2170. }
  2171. a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
  2172. for(j=16; j>0; --j) {
  2173. int b = a;
  2174. a += V;
  2175. for(i=-16; i<0; i+=4) {
  2176. src[16+i] = cm[ (b ) >> 5 ];
  2177. src[17+i] = cm[ (b+ H) >> 5 ];
  2178. src[18+i] = cm[ (b+2*H) >> 5 ];
  2179. src[19+i] = cm[ (b+3*H) >> 5 ];
  2180. b += 4*H;
  2181. }
  2182. src += stride;
  2183. }
  2184. }
  2185. static void pred16x16_plane_c(uint8_t *src, int stride){
  2186. pred16x16_plane_compat_c(src, stride, 0);
  2187. }
  2188. static void pred8x8_vertical_c(uint8_t *src, int stride){
  2189. int i;
  2190. const uint32_t a= ((uint32_t*)(src-stride))[0];
  2191. const uint32_t b= ((uint32_t*)(src-stride))[1];
  2192. for(i=0; i<8; i++){
  2193. ((uint32_t*)(src+i*stride))[0]= a;
  2194. ((uint32_t*)(src+i*stride))[1]= b;
  2195. }
  2196. }
  2197. static void pred8x8_horizontal_c(uint8_t *src, int stride){
  2198. int i;
  2199. for(i=0; i<8; i++){
  2200. ((uint32_t*)(src+i*stride))[0]=
  2201. ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101;
  2202. }
  2203. }
  2204. static void pred8x8_128_dc_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]= 0x01010101U*128U;
  2209. }
  2210. }
  2211. static void pred8x8_left_dc_c(uint8_t *src, int stride){
  2212. int i;
  2213. int dc0, dc2;
  2214. dc0=dc2=0;
  2215. for(i=0;i<4; i++){
  2216. dc0+= src[-1+i*stride];
  2217. dc2+= src[-1+(i+4)*stride];
  2218. }
  2219. dc0= 0x01010101*((dc0 + 2)>>2);
  2220. dc2= 0x01010101*((dc2 + 2)>>2);
  2221. for(i=0; i<4; i++){
  2222. ((uint32_t*)(src+i*stride))[0]=
  2223. ((uint32_t*)(src+i*stride))[1]= dc0;
  2224. }
  2225. for(i=4; i<8; i++){
  2226. ((uint32_t*)(src+i*stride))[0]=
  2227. ((uint32_t*)(src+i*stride))[1]= dc2;
  2228. }
  2229. }
  2230. static void pred8x8_top_dc_c(uint8_t *src, int stride){
  2231. int i;
  2232. int dc0, dc1;
  2233. dc0=dc1=0;
  2234. for(i=0;i<4; i++){
  2235. dc0+= src[i-stride];
  2236. dc1+= src[4+i-stride];
  2237. }
  2238. dc0= 0x01010101*((dc0 + 2)>>2);
  2239. dc1= 0x01010101*((dc1 + 2)>>2);
  2240. for(i=0; i<4; i++){
  2241. ((uint32_t*)(src+i*stride))[0]= dc0;
  2242. ((uint32_t*)(src+i*stride))[1]= dc1;
  2243. }
  2244. for(i=4; i<8; i++){
  2245. ((uint32_t*)(src+i*stride))[0]= dc0;
  2246. ((uint32_t*)(src+i*stride))[1]= dc1;
  2247. }
  2248. }
  2249. static void pred8x8_dc_c(uint8_t *src, int stride){
  2250. int i;
  2251. int dc0, dc1, dc2, dc3;
  2252. dc0=dc1=dc2=0;
  2253. for(i=0;i<4; i++){
  2254. dc0+= src[-1+i*stride] + src[i-stride];
  2255. dc1+= src[4+i-stride];
  2256. dc2+= src[-1+(i+4)*stride];
  2257. }
  2258. dc3= 0x01010101*((dc1 + dc2 + 4)>>3);
  2259. dc0= 0x01010101*((dc0 + 4)>>3);
  2260. dc1= 0x01010101*((dc1 + 2)>>2);
  2261. dc2= 0x01010101*((dc2 + 2)>>2);
  2262. for(i=0; i<4; i++){
  2263. ((uint32_t*)(src+i*stride))[0]= dc0;
  2264. ((uint32_t*)(src+i*stride))[1]= dc1;
  2265. }
  2266. for(i=4; i<8; i++){
  2267. ((uint32_t*)(src+i*stride))[0]= dc2;
  2268. ((uint32_t*)(src+i*stride))[1]= dc3;
  2269. }
  2270. }
  2271. static void pred8x8_plane_c(uint8_t *src, int stride){
  2272. int j, k;
  2273. int a;
  2274. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  2275. const uint8_t * const src0 = src+3-stride;
  2276. const uint8_t *src1 = src+4*stride-1;
  2277. const uint8_t *src2 = src1-2*stride; // == src+2*stride-1;
  2278. int H = src0[1] - src0[-1];
  2279. int V = src1[0] - src2[ 0];
  2280. for(k=2; k<=4; ++k) {
  2281. src1 += stride; src2 -= stride;
  2282. H += k*(src0[k] - src0[-k]);
  2283. V += k*(src1[0] - src2[ 0]);
  2284. }
  2285. H = ( 17*H+16 ) >> 5;
  2286. V = ( 17*V+16 ) >> 5;
  2287. a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
  2288. for(j=8; j>0; --j) {
  2289. int b = a;
  2290. a += V;
  2291. src[0] = cm[ (b ) >> 5 ];
  2292. src[1] = cm[ (b+ H) >> 5 ];
  2293. src[2] = cm[ (b+2*H) >> 5 ];
  2294. src[3] = cm[ (b+3*H) >> 5 ];
  2295. src[4] = cm[ (b+4*H) >> 5 ];
  2296. src[5] = cm[ (b+5*H) >> 5 ];
  2297. src[6] = cm[ (b+6*H) >> 5 ];
  2298. src[7] = cm[ (b+7*H) >> 5 ];
  2299. src += stride;
  2300. }
  2301. }
  2302. #define SRC(x,y) src[(x)+(y)*stride]
  2303. #define PL(y) \
  2304. const int l##y = (SRC(-1,y-1) + 2*SRC(-1,y) + SRC(-1,y+1) + 2) >> 2;
  2305. #define PREDICT_8x8_LOAD_LEFT \
  2306. const int l0 = ((has_topleft ? SRC(-1,-1) : SRC(-1,0)) \
  2307. + 2*SRC(-1,0) + SRC(-1,1) + 2) >> 2; \
  2308. PL(1) PL(2) PL(3) PL(4) PL(5) PL(6) \
  2309. const int l7 attribute_unused = (SRC(-1,6) + 3*SRC(-1,7) + 2) >> 2
  2310. #define PT(x) \
  2311. const int t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  2312. #define PREDICT_8x8_LOAD_TOP \
  2313. const int t0 = ((has_topleft ? SRC(-1,-1) : SRC(0,-1)) \
  2314. + 2*SRC(0,-1) + SRC(1,-1) + 2) >> 2; \
  2315. PT(1) PT(2) PT(3) PT(4) PT(5) PT(6) \
  2316. const int t7 attribute_unused = ((has_topright ? SRC(8,-1) : SRC(7,-1)) \
  2317. + 2*SRC(7,-1) + SRC(6,-1) + 2) >> 2
  2318. #define PTR(x) \
  2319. t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  2320. #define PREDICT_8x8_LOAD_TOPRIGHT \
  2321. int t8, t9, t10, t11, t12, t13, t14, t15; \
  2322. if(has_topright) { \
  2323. PTR(8) PTR(9) PTR(10) PTR(11) PTR(12) PTR(13) PTR(14) \
  2324. t15 = (SRC(14,-1) + 3*SRC(15,-1) + 2) >> 2; \
  2325. } else t8=t9=t10=t11=t12=t13=t14=t15= SRC(7,-1);
  2326. #define PREDICT_8x8_LOAD_TOPLEFT \
  2327. const int lt = (SRC(-1,0) + 2*SRC(-1,-1) + SRC(0,-1) + 2) >> 2
  2328. #define PREDICT_8x8_DC(v) \
  2329. int y; \
  2330. for( y = 0; y < 8; y++ ) { \
  2331. ((uint32_t*)src)[0] = \
  2332. ((uint32_t*)src)[1] = v; \
  2333. src += stride; \
  2334. }
  2335. static void pred8x8l_128_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2336. {
  2337. PREDICT_8x8_DC(0x80808080);
  2338. }
  2339. static void pred8x8l_left_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2340. {
  2341. PREDICT_8x8_LOAD_LEFT;
  2342. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7+4) >> 3) * 0x01010101;
  2343. PREDICT_8x8_DC(dc);
  2344. }
  2345. static void pred8x8l_top_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2346. {
  2347. PREDICT_8x8_LOAD_TOP;
  2348. const uint32_t dc = ((t0+t1+t2+t3+t4+t5+t6+t7+4) >> 3) * 0x01010101;
  2349. PREDICT_8x8_DC(dc);
  2350. }
  2351. static void pred8x8l_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2352. {
  2353. PREDICT_8x8_LOAD_LEFT;
  2354. PREDICT_8x8_LOAD_TOP;
  2355. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7
  2356. +t0+t1+t2+t3+t4+t5+t6+t7+8) >> 4) * 0x01010101;
  2357. PREDICT_8x8_DC(dc);
  2358. }
  2359. static void pred8x8l_horizontal_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2360. {
  2361. PREDICT_8x8_LOAD_LEFT;
  2362. #define ROW(y) ((uint32_t*)(src+y*stride))[0] =\
  2363. ((uint32_t*)(src+y*stride))[1] = 0x01010101 * l##y
  2364. ROW(0); ROW(1); ROW(2); ROW(3); ROW(4); ROW(5); ROW(6); ROW(7);
  2365. #undef ROW
  2366. }
  2367. static void pred8x8l_vertical_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2368. {
  2369. int y;
  2370. PREDICT_8x8_LOAD_TOP;
  2371. src[0] = t0;
  2372. src[1] = t1;
  2373. src[2] = t2;
  2374. src[3] = t3;
  2375. src[4] = t4;
  2376. src[5] = t5;
  2377. src[6] = t6;
  2378. src[7] = t7;
  2379. for( y = 1; y < 8; y++ )
  2380. *(uint64_t*)(src+y*stride) = *(uint64_t*)src;
  2381. }
  2382. static void pred8x8l_down_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2383. {
  2384. PREDICT_8x8_LOAD_TOP;
  2385. PREDICT_8x8_LOAD_TOPRIGHT;
  2386. SRC(0,0)= (t0 + 2*t1 + t2 + 2) >> 2;
  2387. SRC(0,1)=SRC(1,0)= (t1 + 2*t2 + t3 + 2) >> 2;
  2388. SRC(0,2)=SRC(1,1)=SRC(2,0)= (t2 + 2*t3 + t4 + 2) >> 2;
  2389. SRC(0,3)=SRC(1,2)=SRC(2,1)=SRC(3,0)= (t3 + 2*t4 + t5 + 2) >> 2;
  2390. SRC(0,4)=SRC(1,3)=SRC(2,2)=SRC(3,1)=SRC(4,0)= (t4 + 2*t5 + t6 + 2) >> 2;
  2391. SRC(0,5)=SRC(1,4)=SRC(2,3)=SRC(3,2)=SRC(4,1)=SRC(5,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2392. 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;
  2393. 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;
  2394. 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;
  2395. SRC(2,7)=SRC(3,6)=SRC(4,5)=SRC(5,4)=SRC(6,3)=SRC(7,2)= (t9 + 2*t10 + t11 + 2) >> 2;
  2396. SRC(3,7)=SRC(4,6)=SRC(5,5)=SRC(6,4)=SRC(7,3)= (t10 + 2*t11 + t12 + 2) >> 2;
  2397. SRC(4,7)=SRC(5,6)=SRC(6,5)=SRC(7,4)= (t11 + 2*t12 + t13 + 2) >> 2;
  2398. SRC(5,7)=SRC(6,6)=SRC(7,5)= (t12 + 2*t13 + t14 + 2) >> 2;
  2399. SRC(6,7)=SRC(7,6)= (t13 + 2*t14 + t15 + 2) >> 2;
  2400. SRC(7,7)= (t14 + 3*t15 + 2) >> 2;
  2401. }
  2402. static void pred8x8l_down_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2403. {
  2404. PREDICT_8x8_LOAD_TOP;
  2405. PREDICT_8x8_LOAD_LEFT;
  2406. PREDICT_8x8_LOAD_TOPLEFT;
  2407. SRC(0,7)= (l7 + 2*l6 + l5 + 2) >> 2;
  2408. SRC(0,6)=SRC(1,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2409. SRC(0,5)=SRC(1,6)=SRC(2,7)= (l5 + 2*l4 + l3 + 2) >> 2;
  2410. SRC(0,4)=SRC(1,5)=SRC(2,6)=SRC(3,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2411. SRC(0,3)=SRC(1,4)=SRC(2,5)=SRC(3,6)=SRC(4,7)= (l3 + 2*l2 + l1 + 2) >> 2;
  2412. SRC(0,2)=SRC(1,3)=SRC(2,4)=SRC(3,5)=SRC(4,6)=SRC(5,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2413. 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;
  2414. 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;
  2415. 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;
  2416. SRC(2,0)=SRC(3,1)=SRC(4,2)=SRC(5,3)=SRC(6,4)=SRC(7,5)= (t0 + 2*t1 + t2 + 2) >> 2;
  2417. SRC(3,0)=SRC(4,1)=SRC(5,2)=SRC(6,3)=SRC(7,4)= (t1 + 2*t2 + t3 + 2) >> 2;
  2418. SRC(4,0)=SRC(5,1)=SRC(6,2)=SRC(7,3)= (t2 + 2*t3 + t4 + 2) >> 2;
  2419. SRC(5,0)=SRC(6,1)=SRC(7,2)= (t3 + 2*t4 + t5 + 2) >> 2;
  2420. SRC(6,0)=SRC(7,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2421. SRC(7,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2422. }
  2423. static void pred8x8l_vertical_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2424. {
  2425. PREDICT_8x8_LOAD_TOP;
  2426. PREDICT_8x8_LOAD_LEFT;
  2427. PREDICT_8x8_LOAD_TOPLEFT;
  2428. SRC(0,6)= (l5 + 2*l4 + l3 + 2) >> 2;
  2429. SRC(0,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2430. SRC(0,4)=SRC(1,6)= (l3 + 2*l2 + l1 + 2) >> 2;
  2431. SRC(0,5)=SRC(1,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2432. SRC(0,2)=SRC(1,4)=SRC(2,6)= (l1 + 2*l0 + lt + 2) >> 2;
  2433. SRC(0,3)=SRC(1,5)=SRC(2,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2434. SRC(0,1)=SRC(1,3)=SRC(2,5)=SRC(3,7)= (l0 + 2*lt + t0 + 2) >> 2;
  2435. SRC(0,0)=SRC(1,2)=SRC(2,4)=SRC(3,6)= (lt + t0 + 1) >> 1;
  2436. SRC(1,1)=SRC(2,3)=SRC(3,5)=SRC(4,7)= (lt + 2*t0 + t1 + 2) >> 2;
  2437. SRC(1,0)=SRC(2,2)=SRC(3,4)=SRC(4,6)= (t0 + t1 + 1) >> 1;
  2438. SRC(2,1)=SRC(3,3)=SRC(4,5)=SRC(5,7)= (t0 + 2*t1 + t2 + 2) >> 2;
  2439. SRC(2,0)=SRC(3,2)=SRC(4,4)=SRC(5,6)= (t1 + t2 + 1) >> 1;
  2440. SRC(3,1)=SRC(4,3)=SRC(5,5)=SRC(6,7)= (t1 + 2*t2 + t3 + 2) >> 2;
  2441. SRC(3,0)=SRC(4,2)=SRC(5,4)=SRC(6,6)= (t2 + t3 + 1) >> 1;
  2442. SRC(4,1)=SRC(5,3)=SRC(6,5)=SRC(7,7)= (t2 + 2*t3 + t4 + 2) >> 2;
  2443. SRC(4,0)=SRC(5,2)=SRC(6,4)=SRC(7,6)= (t3 + t4 + 1) >> 1;
  2444. SRC(5,1)=SRC(6,3)=SRC(7,5)= (t3 + 2*t4 + t5 + 2) >> 2;
  2445. SRC(5,0)=SRC(6,2)=SRC(7,4)= (t4 + t5 + 1) >> 1;
  2446. SRC(6,1)=SRC(7,3)= (t4 + 2*t5 + t6 + 2) >> 2;
  2447. SRC(6,0)=SRC(7,2)= (t5 + t6 + 1) >> 1;
  2448. SRC(7,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2449. SRC(7,0)= (t6 + t7 + 1) >> 1;
  2450. }
  2451. static void pred8x8l_horizontal_down_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2452. {
  2453. PREDICT_8x8_LOAD_TOP;
  2454. PREDICT_8x8_LOAD_LEFT;
  2455. PREDICT_8x8_LOAD_TOPLEFT;
  2456. SRC(0,7)= (l6 + l7 + 1) >> 1;
  2457. SRC(1,7)= (l5 + 2*l6 + l7 + 2) >> 2;
  2458. SRC(0,6)=SRC(2,7)= (l5 + l6 + 1) >> 1;
  2459. SRC(1,6)=SRC(3,7)= (l4 + 2*l5 + l6 + 2) >> 2;
  2460. SRC(0,5)=SRC(2,6)=SRC(4,7)= (l4 + l5 + 1) >> 1;
  2461. SRC(1,5)=SRC(3,6)=SRC(5,7)= (l3 + 2*l4 + l5 + 2) >> 2;
  2462. SRC(0,4)=SRC(2,5)=SRC(4,6)=SRC(6,7)= (l3 + l4 + 1) >> 1;
  2463. SRC(1,4)=SRC(3,5)=SRC(5,6)=SRC(7,7)= (l2 + 2*l3 + l4 + 2) >> 2;
  2464. SRC(0,3)=SRC(2,4)=SRC(4,5)=SRC(6,6)= (l2 + l3 + 1) >> 1;
  2465. SRC(1,3)=SRC(3,4)=SRC(5,5)=SRC(7,6)= (l1 + 2*l2 + l3 + 2) >> 2;
  2466. SRC(0,2)=SRC(2,3)=SRC(4,4)=SRC(6,5)= (l1 + l2 + 1) >> 1;
  2467. SRC(1,2)=SRC(3,3)=SRC(5,4)=SRC(7,5)= (l0 + 2*l1 + l2 + 2) >> 2;
  2468. SRC(0,1)=SRC(2,2)=SRC(4,3)=SRC(6,4)= (l0 + l1 + 1) >> 1;
  2469. SRC(1,1)=SRC(3,2)=SRC(5,3)=SRC(7,4)= (lt + 2*l0 + l1 + 2) >> 2;
  2470. SRC(0,0)=SRC(2,1)=SRC(4,2)=SRC(6,3)= (lt + l0 + 1) >> 1;
  2471. SRC(1,0)=SRC(3,1)=SRC(5,2)=SRC(7,3)= (l0 + 2*lt + t0 + 2) >> 2;
  2472. SRC(2,0)=SRC(4,1)=SRC(6,2)= (t1 + 2*t0 + lt + 2) >> 2;
  2473. SRC(3,0)=SRC(5,1)=SRC(7,2)= (t2 + 2*t1 + t0 + 2) >> 2;
  2474. SRC(4,0)=SRC(6,1)= (t3 + 2*t2 + t1 + 2) >> 2;
  2475. SRC(5,0)=SRC(7,1)= (t4 + 2*t3 + t2 + 2) >> 2;
  2476. SRC(6,0)= (t5 + 2*t4 + t3 + 2) >> 2;
  2477. SRC(7,0)= (t6 + 2*t5 + t4 + 2) >> 2;
  2478. }
  2479. static void pred8x8l_vertical_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2480. {
  2481. PREDICT_8x8_LOAD_TOP;
  2482. PREDICT_8x8_LOAD_TOPRIGHT;
  2483. SRC(0,0)= (t0 + t1 + 1) >> 1;
  2484. SRC(0,1)= (t0 + 2*t1 + t2 + 2) >> 2;
  2485. SRC(0,2)=SRC(1,0)= (t1 + t2 + 1) >> 1;
  2486. SRC(0,3)=SRC(1,1)= (t1 + 2*t2 + t3 + 2) >> 2;
  2487. SRC(0,4)=SRC(1,2)=SRC(2,0)= (t2 + t3 + 1) >> 1;
  2488. SRC(0,5)=SRC(1,3)=SRC(2,1)= (t2 + 2*t3 + t4 + 2) >> 2;
  2489. SRC(0,6)=SRC(1,4)=SRC(2,2)=SRC(3,0)= (t3 + t4 + 1) >> 1;
  2490. SRC(0,7)=SRC(1,5)=SRC(2,3)=SRC(3,1)= (t3 + 2*t4 + t5 + 2) >> 2;
  2491. SRC(1,6)=SRC(2,4)=SRC(3,2)=SRC(4,0)= (t4 + t5 + 1) >> 1;
  2492. SRC(1,7)=SRC(2,5)=SRC(3,3)=SRC(4,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2493. SRC(2,6)=SRC(3,4)=SRC(4,2)=SRC(5,0)= (t5 + t6 + 1) >> 1;
  2494. SRC(2,7)=SRC(3,5)=SRC(4,3)=SRC(5,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2495. SRC(3,6)=SRC(4,4)=SRC(5,2)=SRC(6,0)= (t6 + t7 + 1) >> 1;
  2496. SRC(3,7)=SRC(4,5)=SRC(5,3)=SRC(6,1)= (t6 + 2*t7 + t8 + 2) >> 2;
  2497. SRC(4,6)=SRC(5,4)=SRC(6,2)=SRC(7,0)= (t7 + t8 + 1) >> 1;
  2498. SRC(4,7)=SRC(5,5)=SRC(6,3)=SRC(7,1)= (t7 + 2*t8 + t9 + 2) >> 2;
  2499. SRC(5,6)=SRC(6,4)=SRC(7,2)= (t8 + t9 + 1) >> 1;
  2500. SRC(5,7)=SRC(6,5)=SRC(7,3)= (t8 + 2*t9 + t10 + 2) >> 2;
  2501. SRC(6,6)=SRC(7,4)= (t9 + t10 + 1) >> 1;
  2502. SRC(6,7)=SRC(7,5)= (t9 + 2*t10 + t11 + 2) >> 2;
  2503. SRC(7,6)= (t10 + t11 + 1) >> 1;
  2504. SRC(7,7)= (t10 + 2*t11 + t12 + 2) >> 2;
  2505. }
  2506. static void pred8x8l_horizontal_up_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2507. {
  2508. PREDICT_8x8_LOAD_LEFT;
  2509. SRC(0,0)= (l0 + l1 + 1) >> 1;
  2510. SRC(1,0)= (l0 + 2*l1 + l2 + 2) >> 2;
  2511. SRC(0,1)=SRC(2,0)= (l1 + l2 + 1) >> 1;
  2512. SRC(1,1)=SRC(3,0)= (l1 + 2*l2 + l3 + 2) >> 2;
  2513. SRC(0,2)=SRC(2,1)=SRC(4,0)= (l2 + l3 + 1) >> 1;
  2514. SRC(1,2)=SRC(3,1)=SRC(5,0)= (l2 + 2*l3 + l4 + 2) >> 2;
  2515. SRC(0,3)=SRC(2,2)=SRC(4,1)=SRC(6,0)= (l3 + l4 + 1) >> 1;
  2516. SRC(1,3)=SRC(3,2)=SRC(5,1)=SRC(7,0)= (l3 + 2*l4 + l5 + 2) >> 2;
  2517. SRC(0,4)=SRC(2,3)=SRC(4,2)=SRC(6,1)= (l4 + l5 + 1) >> 1;
  2518. SRC(1,4)=SRC(3,3)=SRC(5,2)=SRC(7,1)= (l4 + 2*l5 + l6 + 2) >> 2;
  2519. SRC(0,5)=SRC(2,4)=SRC(4,3)=SRC(6,2)= (l5 + l6 + 1) >> 1;
  2520. SRC(1,5)=SRC(3,4)=SRC(5,3)=SRC(7,2)= (l5 + 2*l6 + l7 + 2) >> 2;
  2521. SRC(0,6)=SRC(2,5)=SRC(4,4)=SRC(6,3)= (l6 + l7 + 1) >> 1;
  2522. SRC(1,6)=SRC(3,5)=SRC(5,4)=SRC(7,3)= (l6 + 3*l7 + 2) >> 2;
  2523. SRC(0,7)=SRC(1,7)=SRC(2,6)=SRC(2,7)=SRC(3,6)=
  2524. SRC(3,7)=SRC(4,5)=SRC(4,6)=SRC(4,7)=SRC(5,5)=
  2525. SRC(5,6)=SRC(5,7)=SRC(6,4)=SRC(6,5)=SRC(6,6)=
  2526. SRC(6,7)=SRC(7,4)=SRC(7,5)=SRC(7,6)=SRC(7,7)= l7;
  2527. }
  2528. #undef PREDICT_8x8_LOAD_LEFT
  2529. #undef PREDICT_8x8_LOAD_TOP
  2530. #undef PREDICT_8x8_LOAD_TOPLEFT
  2531. #undef PREDICT_8x8_LOAD_TOPRIGHT
  2532. #undef PREDICT_8x8_DC
  2533. #undef PTR
  2534. #undef PT
  2535. #undef PL
  2536. #undef SRC
  2537. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  2538. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2539. int src_x_offset, int src_y_offset,
  2540. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  2541. MpegEncContext * const s = &h->s;
  2542. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  2543. int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  2544. const int luma_xy= (mx&3) + ((my&3)<<2);
  2545. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
  2546. uint8_t * src_cb, * src_cr;
  2547. int extra_width= h->emu_edge_width;
  2548. int extra_height= h->emu_edge_height;
  2549. int emu=0;
  2550. const int full_mx= mx>>2;
  2551. const int full_my= my>>2;
  2552. const int pic_width = 16*s->mb_width;
  2553. const int pic_height = 16*s->mb_height >> MB_MBAFF;
  2554. if(!pic->data[0])
  2555. return;
  2556. if(mx&7) extra_width -= 3;
  2557. if(my&7) extra_height -= 3;
  2558. if( full_mx < 0-extra_width
  2559. || full_my < 0-extra_height
  2560. || full_mx + 16/*FIXME*/ > pic_width + extra_width
  2561. || full_my + 16/*FIXME*/ > pic_height + extra_height){
  2562. 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);
  2563. src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
  2564. emu=1;
  2565. }
  2566. qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
  2567. if(!square){
  2568. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
  2569. }
  2570. if(s->flags&CODEC_FLAG_GRAY) return;
  2571. if(MB_MBAFF){
  2572. // chroma offset when predicting from a field of opposite parity
  2573. my += 2 * ((s->mb_y & 1) - (h->ref_cache[list][scan8[n]] & 1));
  2574. emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
  2575. }
  2576. src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2577. src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2578. if(emu){
  2579. 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);
  2580. src_cb= s->edge_emu_buffer;
  2581. }
  2582. chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2583. if(emu){
  2584. 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);
  2585. src_cr= s->edge_emu_buffer;
  2586. }
  2587. chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2588. }
  2589. static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
  2590. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2591. int x_offset, int y_offset,
  2592. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2593. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2594. int list0, int list1){
  2595. MpegEncContext * const s = &h->s;
  2596. qpel_mc_func *qpix_op= qpix_put;
  2597. h264_chroma_mc_func chroma_op= chroma_put;
  2598. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2599. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2600. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2601. x_offset += 8*s->mb_x;
  2602. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2603. if(list0){
  2604. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  2605. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  2606. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2607. qpix_op, chroma_op);
  2608. qpix_op= qpix_avg;
  2609. chroma_op= chroma_avg;
  2610. }
  2611. if(list1){
  2612. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  2613. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  2614. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2615. qpix_op, chroma_op);
  2616. }
  2617. }
  2618. static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
  2619. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2620. int x_offset, int y_offset,
  2621. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2622. h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
  2623. h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
  2624. int list0, int list1){
  2625. MpegEncContext * const s = &h->s;
  2626. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2627. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2628. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2629. x_offset += 8*s->mb_x;
  2630. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2631. if(list0 && list1){
  2632. /* don't optimize for luma-only case, since B-frames usually
  2633. * use implicit weights => chroma too. */
  2634. uint8_t *tmp_cb = s->obmc_scratchpad;
  2635. uint8_t *tmp_cr = s->obmc_scratchpad + 8;
  2636. uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
  2637. int refn0 = h->ref_cache[0][ scan8[n] ];
  2638. int refn1 = h->ref_cache[1][ scan8[n] ];
  2639. mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
  2640. dest_y, dest_cb, dest_cr,
  2641. x_offset, y_offset, qpix_put, chroma_put);
  2642. mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
  2643. tmp_y, tmp_cb, tmp_cr,
  2644. x_offset, y_offset, qpix_put, chroma_put);
  2645. if(h->use_weight == 2){
  2646. int weight0 = h->implicit_weight[refn0][refn1];
  2647. int weight1 = 64 - weight0;
  2648. luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
  2649. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2650. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2651. }else{
  2652. luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
  2653. h->luma_weight[0][refn0], h->luma_weight[1][refn1],
  2654. h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
  2655. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2656. h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
  2657. h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
  2658. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2659. h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
  2660. h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
  2661. }
  2662. }else{
  2663. int list = list1 ? 1 : 0;
  2664. int refn = h->ref_cache[list][ scan8[n] ];
  2665. Picture *ref= &h->ref_list[list][refn];
  2666. mc_dir_part(h, ref, n, square, chroma_height, delta, list,
  2667. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2668. qpix_put, chroma_put);
  2669. luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
  2670. h->luma_weight[list][refn], h->luma_offset[list][refn]);
  2671. if(h->use_weight_chroma){
  2672. chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2673. h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
  2674. chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2675. h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
  2676. }
  2677. }
  2678. }
  2679. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  2680. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2681. int x_offset, int y_offset,
  2682. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2683. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2684. h264_weight_func *weight_op, h264_biweight_func *weight_avg,
  2685. int list0, int list1){
  2686. if((h->use_weight==2 && list0 && list1
  2687. && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
  2688. || h->use_weight==1)
  2689. mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2690. x_offset, y_offset, qpix_put, chroma_put,
  2691. weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
  2692. else
  2693. mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2694. x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
  2695. }
  2696. static inline void prefetch_motion(H264Context *h, int list){
  2697. /* fetch pixels for estimated mv 4 macroblocks ahead
  2698. * optimized for 64byte cache lines */
  2699. MpegEncContext * const s = &h->s;
  2700. const int refn = h->ref_cache[list][scan8[0]];
  2701. if(refn >= 0){
  2702. const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
  2703. const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
  2704. uint8_t **src= h->ref_list[list][refn].data;
  2705. int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
  2706. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  2707. off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
  2708. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  2709. }
  2710. }
  2711. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2712. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  2713. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
  2714. h264_weight_func *weight_op, h264_biweight_func *weight_avg){
  2715. MpegEncContext * const s = &h->s;
  2716. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2717. const int mb_type= s->current_picture.mb_type[mb_xy];
  2718. assert(IS_INTER(mb_type));
  2719. prefetch_motion(h, 0);
  2720. if(IS_16X16(mb_type)){
  2721. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  2722. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  2723. &weight_op[0], &weight_avg[0],
  2724. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2725. }else if(IS_16X8(mb_type)){
  2726. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  2727. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2728. &weight_op[1], &weight_avg[1],
  2729. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2730. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  2731. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2732. &weight_op[1], &weight_avg[1],
  2733. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2734. }else if(IS_8X16(mb_type)){
  2735. mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
  2736. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2737. &weight_op[2], &weight_avg[2],
  2738. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2739. mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
  2740. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2741. &weight_op[2], &weight_avg[2],
  2742. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2743. }else{
  2744. int i;
  2745. assert(IS_8X8(mb_type));
  2746. for(i=0; i<4; i++){
  2747. const int sub_mb_type= h->sub_mb_type[i];
  2748. const int n= 4*i;
  2749. int x_offset= (i&1)<<2;
  2750. int y_offset= (i&2)<<1;
  2751. if(IS_SUB_8X8(sub_mb_type)){
  2752. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2753. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2754. &weight_op[3], &weight_avg[3],
  2755. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2756. }else if(IS_SUB_8X4(sub_mb_type)){
  2757. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2758. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2759. &weight_op[4], &weight_avg[4],
  2760. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2761. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  2762. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2763. &weight_op[4], &weight_avg[4],
  2764. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2765. }else if(IS_SUB_4X8(sub_mb_type)){
  2766. mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2767. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2768. &weight_op[5], &weight_avg[5],
  2769. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2770. mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
  2771. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2772. &weight_op[5], &weight_avg[5],
  2773. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2774. }else{
  2775. int j;
  2776. assert(IS_SUB_4X4(sub_mb_type));
  2777. for(j=0; j<4; j++){
  2778. int sub_x_offset= x_offset + 2*(j&1);
  2779. int sub_y_offset= y_offset + (j&2);
  2780. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  2781. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2782. &weight_op[6], &weight_avg[6],
  2783. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2784. }
  2785. }
  2786. }
  2787. }
  2788. prefetch_motion(h, 1);
  2789. }
  2790. static void decode_init_vlc(){
  2791. static int done = 0;
  2792. if (!done) {
  2793. int i;
  2794. done = 1;
  2795. init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
  2796. &chroma_dc_coeff_token_len [0], 1, 1,
  2797. &chroma_dc_coeff_token_bits[0], 1, 1, 1);
  2798. for(i=0; i<4; i++){
  2799. init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
  2800. &coeff_token_len [i][0], 1, 1,
  2801. &coeff_token_bits[i][0], 1, 1, 1);
  2802. }
  2803. for(i=0; i<3; i++){
  2804. init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
  2805. &chroma_dc_total_zeros_len [i][0], 1, 1,
  2806. &chroma_dc_total_zeros_bits[i][0], 1, 1, 1);
  2807. }
  2808. for(i=0; i<15; i++){
  2809. init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
  2810. &total_zeros_len [i][0], 1, 1,
  2811. &total_zeros_bits[i][0], 1, 1, 1);
  2812. }
  2813. for(i=0; i<6; i++){
  2814. init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
  2815. &run_len [i][0], 1, 1,
  2816. &run_bits[i][0], 1, 1, 1);
  2817. }
  2818. init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
  2819. &run_len [6][0], 1, 1,
  2820. &run_bits[6][0], 1, 1, 1);
  2821. }
  2822. }
  2823. /**
  2824. * Sets the intra prediction function pointers.
  2825. */
  2826. static void init_pred_ptrs(H264Context *h){
  2827. // MpegEncContext * const s = &h->s;
  2828. h->pred4x4[VERT_PRED ]= pred4x4_vertical_c;
  2829. h->pred4x4[HOR_PRED ]= pred4x4_horizontal_c;
  2830. h->pred4x4[DC_PRED ]= pred4x4_dc_c;
  2831. h->pred4x4[DIAG_DOWN_LEFT_PRED ]= pred4x4_down_left_c;
  2832. h->pred4x4[DIAG_DOWN_RIGHT_PRED]= pred4x4_down_right_c;
  2833. h->pred4x4[VERT_RIGHT_PRED ]= pred4x4_vertical_right_c;
  2834. h->pred4x4[HOR_DOWN_PRED ]= pred4x4_horizontal_down_c;
  2835. h->pred4x4[VERT_LEFT_PRED ]= pred4x4_vertical_left_c;
  2836. h->pred4x4[HOR_UP_PRED ]= pred4x4_horizontal_up_c;
  2837. h->pred4x4[LEFT_DC_PRED ]= pred4x4_left_dc_c;
  2838. h->pred4x4[TOP_DC_PRED ]= pred4x4_top_dc_c;
  2839. h->pred4x4[DC_128_PRED ]= pred4x4_128_dc_c;
  2840. h->pred8x8l[VERT_PRED ]= pred8x8l_vertical_c;
  2841. h->pred8x8l[HOR_PRED ]= pred8x8l_horizontal_c;
  2842. h->pred8x8l[DC_PRED ]= pred8x8l_dc_c;
  2843. h->pred8x8l[DIAG_DOWN_LEFT_PRED ]= pred8x8l_down_left_c;
  2844. h->pred8x8l[DIAG_DOWN_RIGHT_PRED]= pred8x8l_down_right_c;
  2845. h->pred8x8l[VERT_RIGHT_PRED ]= pred8x8l_vertical_right_c;
  2846. h->pred8x8l[HOR_DOWN_PRED ]= pred8x8l_horizontal_down_c;
  2847. h->pred8x8l[VERT_LEFT_PRED ]= pred8x8l_vertical_left_c;
  2848. h->pred8x8l[HOR_UP_PRED ]= pred8x8l_horizontal_up_c;
  2849. h->pred8x8l[LEFT_DC_PRED ]= pred8x8l_left_dc_c;
  2850. h->pred8x8l[TOP_DC_PRED ]= pred8x8l_top_dc_c;
  2851. h->pred8x8l[DC_128_PRED ]= pred8x8l_128_dc_c;
  2852. h->pred8x8[DC_PRED8x8 ]= pred8x8_dc_c;
  2853. h->pred8x8[VERT_PRED8x8 ]= pred8x8_vertical_c;
  2854. h->pred8x8[HOR_PRED8x8 ]= pred8x8_horizontal_c;
  2855. h->pred8x8[PLANE_PRED8x8 ]= pred8x8_plane_c;
  2856. h->pred8x8[LEFT_DC_PRED8x8]= pred8x8_left_dc_c;
  2857. h->pred8x8[TOP_DC_PRED8x8 ]= pred8x8_top_dc_c;
  2858. h->pred8x8[DC_128_PRED8x8 ]= pred8x8_128_dc_c;
  2859. h->pred16x16[DC_PRED8x8 ]= pred16x16_dc_c;
  2860. h->pred16x16[VERT_PRED8x8 ]= pred16x16_vertical_c;
  2861. h->pred16x16[HOR_PRED8x8 ]= pred16x16_horizontal_c;
  2862. h->pred16x16[PLANE_PRED8x8 ]= pred16x16_plane_c;
  2863. h->pred16x16[LEFT_DC_PRED8x8]= pred16x16_left_dc_c;
  2864. h->pred16x16[TOP_DC_PRED8x8 ]= pred16x16_top_dc_c;
  2865. h->pred16x16[DC_128_PRED8x8 ]= pred16x16_128_dc_c;
  2866. }
  2867. static void free_tables(H264Context *h){
  2868. av_freep(&h->intra4x4_pred_mode);
  2869. av_freep(&h->chroma_pred_mode_table);
  2870. av_freep(&h->cbp_table);
  2871. av_freep(&h->mvd_table[0]);
  2872. av_freep(&h->mvd_table[1]);
  2873. av_freep(&h->direct_table);
  2874. av_freep(&h->non_zero_count);
  2875. av_freep(&h->slice_table_base);
  2876. av_freep(&h->top_borders[1]);
  2877. av_freep(&h->top_borders[0]);
  2878. h->slice_table= NULL;
  2879. av_freep(&h->mb2b_xy);
  2880. av_freep(&h->mb2b8_xy);
  2881. av_freep(&h->s.obmc_scratchpad);
  2882. }
  2883. static void init_dequant8_coeff_table(H264Context *h){
  2884. int i,q,x;
  2885. const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
  2886. h->dequant8_coeff[0] = h->dequant8_buffer[0];
  2887. h->dequant8_coeff[1] = h->dequant8_buffer[1];
  2888. for(i=0; i<2; i++ ){
  2889. if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
  2890. h->dequant8_coeff[1] = h->dequant8_buffer[0];
  2891. break;
  2892. }
  2893. for(q=0; q<52; q++){
  2894. int shift = div6[q];
  2895. int idx = rem6[q];
  2896. for(x=0; x<64; x++)
  2897. h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
  2898. ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
  2899. h->pps.scaling_matrix8[i][x]) << shift;
  2900. }
  2901. }
  2902. }
  2903. static void init_dequant4_coeff_table(H264Context *h){
  2904. int i,j,q,x;
  2905. const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
  2906. for(i=0; i<6; i++ ){
  2907. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  2908. for(j=0; j<i; j++){
  2909. if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
  2910. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  2911. break;
  2912. }
  2913. }
  2914. if(j<i)
  2915. continue;
  2916. for(q=0; q<52; q++){
  2917. int shift = div6[q] + 2;
  2918. int idx = rem6[q];
  2919. for(x=0; x<16; x++)
  2920. h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
  2921. ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
  2922. h->pps.scaling_matrix4[i][x]) << shift;
  2923. }
  2924. }
  2925. }
  2926. static void init_dequant_tables(H264Context *h){
  2927. int i,x;
  2928. init_dequant4_coeff_table(h);
  2929. if(h->pps.transform_8x8_mode)
  2930. init_dequant8_coeff_table(h);
  2931. if(h->sps.transform_bypass){
  2932. for(i=0; i<6; i++)
  2933. for(x=0; x<16; x++)
  2934. h->dequant4_coeff[i][0][x] = 1<<6;
  2935. if(h->pps.transform_8x8_mode)
  2936. for(i=0; i<2; i++)
  2937. for(x=0; x<64; x++)
  2938. h->dequant8_coeff[i][0][x] = 1<<6;
  2939. }
  2940. }
  2941. /**
  2942. * allocates tables.
  2943. * needs width/height
  2944. */
  2945. static int alloc_tables(H264Context *h){
  2946. MpegEncContext * const s = &h->s;
  2947. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  2948. int x,y;
  2949. CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
  2950. CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
  2951. CHECKED_ALLOCZ(h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(uint8_t))
  2952. CHECKED_ALLOCZ(h->top_borders[0] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2953. CHECKED_ALLOCZ(h->top_borders[1] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2954. CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
  2955. if( h->pps.cabac ) {
  2956. CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
  2957. CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
  2958. CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t));
  2959. CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t));
  2960. }
  2961. memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(uint8_t));
  2962. h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
  2963. CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint32_t));
  2964. CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint32_t));
  2965. for(y=0; y<s->mb_height; y++){
  2966. for(x=0; x<s->mb_width; x++){
  2967. const int mb_xy= x + y*s->mb_stride;
  2968. const int b_xy = 4*x + 4*y*h->b_stride;
  2969. const int b8_xy= 2*x + 2*y*h->b8_stride;
  2970. h->mb2b_xy [mb_xy]= b_xy;
  2971. h->mb2b8_xy[mb_xy]= b8_xy;
  2972. }
  2973. }
  2974. s->obmc_scratchpad = NULL;
  2975. if(!h->dequant4_coeff[0])
  2976. init_dequant_tables(h);
  2977. return 0;
  2978. fail:
  2979. free_tables(h);
  2980. return -1;
  2981. }
  2982. static void common_init(H264Context *h){
  2983. MpegEncContext * const s = &h->s;
  2984. s->width = s->avctx->width;
  2985. s->height = s->avctx->height;
  2986. s->codec_id= s->avctx->codec->id;
  2987. init_pred_ptrs(h);
  2988. h->dequant_coeff_pps= -1;
  2989. s->unrestricted_mv=1;
  2990. s->decode=1; //FIXME
  2991. memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  2992. memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  2993. }
  2994. static int decode_init(AVCodecContext *avctx){
  2995. H264Context *h= avctx->priv_data;
  2996. MpegEncContext * const s = &h->s;
  2997. MPV_decode_defaults(s);
  2998. s->avctx = avctx;
  2999. common_init(h);
  3000. s->out_format = FMT_H264;
  3001. s->workaround_bugs= avctx->workaround_bugs;
  3002. // set defaults
  3003. // s->decode_mb= ff_h263_decode_mb;
  3004. s->low_delay= 1;
  3005. avctx->pix_fmt= PIX_FMT_YUV420P;
  3006. decode_init_vlc();
  3007. if(avctx->extradata_size > 0 && avctx->extradata &&
  3008. *(char *)avctx->extradata == 1){
  3009. h->is_avc = 1;
  3010. h->got_avcC = 0;
  3011. } else {
  3012. h->is_avc = 0;
  3013. }
  3014. return 0;
  3015. }
  3016. static int frame_start(H264Context *h){
  3017. MpegEncContext * const s = &h->s;
  3018. int i;
  3019. if(MPV_frame_start(s, s->avctx) < 0)
  3020. return -1;
  3021. ff_er_frame_start(s);
  3022. assert(s->linesize && s->uvlinesize);
  3023. for(i=0; i<16; i++){
  3024. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  3025. h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  3026. }
  3027. for(i=0; i<4; i++){
  3028. h->block_offset[16+i]=
  3029. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  3030. h->block_offset[24+16+i]=
  3031. h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  3032. }
  3033. /* can't be in alloc_tables because linesize isn't known there.
  3034. * FIXME: redo bipred weight to not require extra buffer? */
  3035. if(!s->obmc_scratchpad)
  3036. s->obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  3037. /* some macroblocks will be accessed before they're available */
  3038. if(FRAME_MBAFF)
  3039. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(uint8_t));
  3040. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  3041. return 0;
  3042. }
  3043. static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  3044. MpegEncContext * const s = &h->s;
  3045. int i;
  3046. src_y -= linesize;
  3047. src_cb -= uvlinesize;
  3048. src_cr -= uvlinesize;
  3049. // There are two lines saved, the line above the the top macroblock of a pair,
  3050. // and the line above the bottom macroblock
  3051. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  3052. for(i=1; i<17; i++){
  3053. h->left_border[i]= src_y[15+i* linesize];
  3054. }
  3055. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
  3056. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
  3057. if(!(s->flags&CODEC_FLAG_GRAY)){
  3058. h->left_border[17 ]= h->top_borders[0][s->mb_x][16+7];
  3059. h->left_border[17+9]= h->top_borders[0][s->mb_x][24+7];
  3060. for(i=1; i<9; i++){
  3061. h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
  3062. h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
  3063. }
  3064. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
  3065. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
  3066. }
  3067. }
  3068. 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){
  3069. MpegEncContext * const s = &h->s;
  3070. int temp8, i;
  3071. uint64_t temp64;
  3072. int deblock_left = (s->mb_x > 0);
  3073. int deblock_top = (s->mb_y > 0);
  3074. src_y -= linesize + 1;
  3075. src_cb -= uvlinesize + 1;
  3076. src_cr -= uvlinesize + 1;
  3077. #define XCHG(a,b,t,xchg)\
  3078. t= a;\
  3079. if(xchg)\
  3080. a= b;\
  3081. b= t;
  3082. if(deblock_left){
  3083. for(i = !deblock_top; i<17; i++){
  3084. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  3085. }
  3086. }
  3087. if(deblock_top){
  3088. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  3089. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  3090. if(s->mb_x+1 < s->mb_width){
  3091. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  3092. }
  3093. }
  3094. if(!(s->flags&CODEC_FLAG_GRAY)){
  3095. if(deblock_left){
  3096. for(i = !deblock_top; i<9; i++){
  3097. XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
  3098. XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
  3099. }
  3100. }
  3101. if(deblock_top){
  3102. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  3103. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  3104. }
  3105. }
  3106. }
  3107. static inline void backup_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  3108. MpegEncContext * const s = &h->s;
  3109. int i;
  3110. src_y -= 2 * linesize;
  3111. src_cb -= 2 * uvlinesize;
  3112. src_cr -= 2 * uvlinesize;
  3113. // There are two lines saved, the line above the the top macroblock of a pair,
  3114. // and the line above the bottom macroblock
  3115. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  3116. h->left_border[1]= h->top_borders[1][s->mb_x][15];
  3117. for(i=2; i<34; i++){
  3118. h->left_border[i]= src_y[15+i* linesize];
  3119. }
  3120. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 32*linesize);
  3121. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+32*linesize);
  3122. *(uint64_t*)(h->top_borders[1][s->mb_x]+0)= *(uint64_t*)(src_y + 33*linesize);
  3123. *(uint64_t*)(h->top_borders[1][s->mb_x]+8)= *(uint64_t*)(src_y +8+33*linesize);
  3124. if(!(s->flags&CODEC_FLAG_GRAY)){
  3125. h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7];
  3126. h->left_border[34+ 1]= h->top_borders[1][s->mb_x][16+7];
  3127. h->left_border[34+18 ]= h->top_borders[0][s->mb_x][24+7];
  3128. h->left_border[34+18+1]= h->top_borders[1][s->mb_x][24+7];
  3129. for(i=2; i<18; i++){
  3130. h->left_border[i+34 ]= src_cb[7+i*uvlinesize];
  3131. h->left_border[i+34+18]= src_cr[7+i*uvlinesize];
  3132. }
  3133. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+16*uvlinesize);
  3134. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+16*uvlinesize);
  3135. *(uint64_t*)(h->top_borders[1][s->mb_x]+16)= *(uint64_t*)(src_cb+17*uvlinesize);
  3136. *(uint64_t*)(h->top_borders[1][s->mb_x]+24)= *(uint64_t*)(src_cr+17*uvlinesize);
  3137. }
  3138. }
  3139. 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){
  3140. MpegEncContext * const s = &h->s;
  3141. int temp8, i;
  3142. uint64_t temp64;
  3143. int deblock_left = (s->mb_x > 0);
  3144. int deblock_top = (s->mb_y > 1);
  3145. 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);
  3146. src_y -= 2 * linesize + 1;
  3147. src_cb -= 2 * uvlinesize + 1;
  3148. src_cr -= 2 * uvlinesize + 1;
  3149. #define XCHG(a,b,t,xchg)\
  3150. t= a;\
  3151. if(xchg)\
  3152. a= b;\
  3153. b= t;
  3154. if(deblock_left){
  3155. for(i = (!deblock_top)<<1; i<34; i++){
  3156. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  3157. }
  3158. }
  3159. if(deblock_top){
  3160. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  3161. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  3162. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+0), *(uint64_t*)(src_y +1 +linesize), temp64, xchg);
  3163. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+8), *(uint64_t*)(src_y +9 +linesize), temp64, 1);
  3164. if(s->mb_x+1 < s->mb_width){
  3165. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  3166. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x+1]), *(uint64_t*)(src_y +17 +linesize), temp64, 1);
  3167. }
  3168. }
  3169. if(!(s->flags&CODEC_FLAG_GRAY)){
  3170. if(deblock_left){
  3171. for(i = (!deblock_top) << 1; i<18; i++){
  3172. XCHG(h->left_border[i+34 ], src_cb[i*uvlinesize], temp8, xchg);
  3173. XCHG(h->left_border[i+34+18], src_cr[i*uvlinesize], temp8, xchg);
  3174. }
  3175. }
  3176. if(deblock_top){
  3177. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  3178. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  3179. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+16), *(uint64_t*)(src_cb+1 +uvlinesize), temp64, 1);
  3180. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+24), *(uint64_t*)(src_cr+1 +uvlinesize), temp64, 1);
  3181. }
  3182. }
  3183. }
  3184. static void hl_decode_mb(H264Context *h){
  3185. MpegEncContext * const s = &h->s;
  3186. const int mb_x= s->mb_x;
  3187. const int mb_y= s->mb_y;
  3188. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3189. const int mb_type= s->current_picture.mb_type[mb_xy];
  3190. uint8_t *dest_y, *dest_cb, *dest_cr;
  3191. int linesize, uvlinesize /*dct_offset*/;
  3192. int i;
  3193. int *block_offset = &h->block_offset[0];
  3194. const unsigned int bottom = mb_y & 1;
  3195. const int transform_bypass = (s->qscale == 0 && h->sps.transform_bypass);
  3196. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  3197. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  3198. if(!s->decode)
  3199. return;
  3200. dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3201. dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3202. dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3203. s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
  3204. s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
  3205. if (MB_FIELD) {
  3206. linesize = h->mb_linesize = s->linesize * 2;
  3207. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  3208. block_offset = &h->block_offset[24];
  3209. if(mb_y&1){ //FIXME move out of this func?
  3210. dest_y -= s->linesize*15;
  3211. dest_cb-= s->uvlinesize*7;
  3212. dest_cr-= s->uvlinesize*7;
  3213. }
  3214. if(FRAME_MBAFF) {
  3215. int list;
  3216. for(list=0; list<2; list++){
  3217. if(!USES_LIST(mb_type, list))
  3218. continue;
  3219. if(IS_16X16(mb_type)){
  3220. int8_t *ref = &h->ref_cache[list][scan8[0]];
  3221. fill_rectangle(ref, 4, 4, 8, 16+*ref^(s->mb_y&1), 1);
  3222. }else{
  3223. for(i=0; i<16; i+=4){
  3224. //FIXME can refs be smaller than 8x8 when !direct_8x8_inference ?
  3225. int ref = h->ref_cache[list][scan8[i]];
  3226. if(ref >= 0)
  3227. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, 16+ref^(s->mb_y&1), 1);
  3228. }
  3229. }
  3230. }
  3231. }
  3232. } else {
  3233. linesize = h->mb_linesize = s->linesize;
  3234. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  3235. // dct_offset = s->linesize * 16;
  3236. }
  3237. if(transform_bypass){
  3238. idct_dc_add =
  3239. idct_add = IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  3240. }else if(IS_8x8DCT(mb_type)){
  3241. idct_dc_add = s->dsp.h264_idct8_dc_add;
  3242. idct_add = s->dsp.h264_idct8_add;
  3243. }else{
  3244. idct_dc_add = s->dsp.h264_idct_dc_add;
  3245. idct_add = s->dsp.h264_idct_add;
  3246. }
  3247. if(FRAME_MBAFF && h->deblocking_filter && IS_INTRA(mb_type)
  3248. && (!bottom || !IS_INTRA(s->current_picture.mb_type[mb_xy-s->mb_stride]))){
  3249. int mbt_y = mb_y&~1;
  3250. uint8_t *top_y = s->current_picture.data[0] + (mbt_y * 16* s->linesize ) + mb_x * 16;
  3251. uint8_t *top_cb = s->current_picture.data[1] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  3252. uint8_t *top_cr = s->current_picture.data[2] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  3253. xchg_pair_border(h, top_y, top_cb, top_cr, s->linesize, s->uvlinesize, 1);
  3254. }
  3255. if (IS_INTRA_PCM(mb_type)) {
  3256. unsigned int x, y;
  3257. // The pixels are stored in h->mb array in the same order as levels,
  3258. // copy them in output in the correct order.
  3259. for(i=0; i<16; i++) {
  3260. for (y=0; y<4; y++) {
  3261. for (x=0; x<4; x++) {
  3262. *(dest_y + block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x];
  3263. }
  3264. }
  3265. }
  3266. for(i=16; i<16+4; i++) {
  3267. for (y=0; y<4; y++) {
  3268. for (x=0; x<4; x++) {
  3269. *(dest_cb + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  3270. }
  3271. }
  3272. }
  3273. for(i=20; i<20+4; i++) {
  3274. for (y=0; y<4; y++) {
  3275. for (x=0; x<4; x++) {
  3276. *(dest_cr + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  3277. }
  3278. }
  3279. }
  3280. } else {
  3281. if(IS_INTRA(mb_type)){
  3282. if(h->deblocking_filter && !FRAME_MBAFF)
  3283. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1);
  3284. if(!(s->flags&CODEC_FLAG_GRAY)){
  3285. h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  3286. h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  3287. }
  3288. if(IS_INTRA4x4(mb_type)){
  3289. if(!s->encoding){
  3290. if(IS_8x8DCT(mb_type)){
  3291. for(i=0; i<16; i+=4){
  3292. uint8_t * const ptr= dest_y + block_offset[i];
  3293. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  3294. const int nnz = h->non_zero_count_cache[ scan8[i] ];
  3295. h->pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  3296. (h->topright_samples_available<<(i+1))&0x8000, linesize);
  3297. if(nnz){
  3298. if(nnz == 1 && h->mb[i*16])
  3299. idct_dc_add(ptr, h->mb + i*16, linesize);
  3300. else
  3301. idct_add(ptr, h->mb + i*16, linesize);
  3302. }
  3303. }
  3304. }else
  3305. for(i=0; i<16; i++){
  3306. uint8_t * const ptr= dest_y + block_offset[i];
  3307. uint8_t *topright;
  3308. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  3309. int nnz, tr;
  3310. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  3311. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  3312. assert(mb_y || linesize <= block_offset[i]);
  3313. if(!topright_avail){
  3314. tr= ptr[3 - linesize]*0x01010101;
  3315. topright= (uint8_t*) &tr;
  3316. }else
  3317. topright= ptr + 4 - linesize;
  3318. }else
  3319. topright= NULL;
  3320. h->pred4x4[ dir ](ptr, topright, linesize);
  3321. nnz = h->non_zero_count_cache[ scan8[i] ];
  3322. if(nnz){
  3323. if(s->codec_id == CODEC_ID_H264){
  3324. if(nnz == 1 && h->mb[i*16])
  3325. idct_dc_add(ptr, h->mb + i*16, linesize);
  3326. else
  3327. idct_add(ptr, h->mb + i*16, linesize);
  3328. }else
  3329. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  3330. }
  3331. }
  3332. }
  3333. }else{
  3334. h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  3335. if(s->codec_id == CODEC_ID_H264){
  3336. if(!transform_bypass)
  3337. h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[IS_INTRA(mb_type) ? 0:3][s->qscale][0]);
  3338. }else
  3339. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  3340. }
  3341. if(h->deblocking_filter && !FRAME_MBAFF)
  3342. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
  3343. }else if(s->codec_id == CODEC_ID_H264){
  3344. hl_motion(h, dest_y, dest_cb, dest_cr,
  3345. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  3346. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  3347. s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
  3348. }
  3349. if(!IS_INTRA4x4(mb_type)){
  3350. if(s->codec_id == CODEC_ID_H264){
  3351. if(IS_INTRA16x16(mb_type)){
  3352. for(i=0; i<16; i++){
  3353. if(h->non_zero_count_cache[ scan8[i] ])
  3354. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3355. else if(h->mb[i*16])
  3356. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3357. }
  3358. }else{
  3359. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  3360. for(i=0; i<16; i+=di){
  3361. int nnz = h->non_zero_count_cache[ scan8[i] ];
  3362. if(nnz){
  3363. if(nnz==1 && h->mb[i*16])
  3364. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3365. else
  3366. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3367. }
  3368. }
  3369. }
  3370. }else{
  3371. for(i=0; i<16; i++){
  3372. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  3373. uint8_t * const ptr= dest_y + block_offset[i];
  3374. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  3375. }
  3376. }
  3377. }
  3378. }
  3379. if(!(s->flags&CODEC_FLAG_GRAY)){
  3380. uint8_t *dest[2] = {dest_cb, dest_cr};
  3381. if(transform_bypass){
  3382. idct_add = idct_dc_add = s->dsp.add_pixels4;
  3383. }else{
  3384. idct_add = s->dsp.h264_idct_add;
  3385. idct_dc_add = s->dsp.h264_idct_dc_add;
  3386. 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]);
  3387. 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]);
  3388. }
  3389. if(s->codec_id == CODEC_ID_H264){
  3390. for(i=16; i<16+8; i++){
  3391. if(h->non_zero_count_cache[ scan8[i] ])
  3392. idct_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3393. else if(h->mb[i*16])
  3394. idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3395. }
  3396. }else{
  3397. for(i=16; i<16+8; i++){
  3398. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  3399. uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
  3400. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  3401. }
  3402. }
  3403. }
  3404. }
  3405. }
  3406. if(h->deblocking_filter) {
  3407. if (FRAME_MBAFF) {
  3408. //FIXME try deblocking one mb at a time?
  3409. // the reduction in load/storing mvs and such might outweigh the extra backup/xchg_border
  3410. const int mb_y = s->mb_y - 1;
  3411. uint8_t *pair_dest_y, *pair_dest_cb, *pair_dest_cr;
  3412. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3413. const int mb_type_top = s->current_picture.mb_type[mb_xy];
  3414. const int mb_type_bottom= s->current_picture.mb_type[mb_xy+s->mb_stride];
  3415. if (!bottom) return;
  3416. pair_dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3417. pair_dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3418. pair_dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3419. if(IS_INTRA(mb_type_top | mb_type_bottom))
  3420. xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0);
  3421. backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize);
  3422. // deblock a pair
  3423. // top
  3424. s->mb_y--;
  3425. 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);
  3426. fill_caches(h, mb_type_top, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3427. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy]);
  3428. filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize);
  3429. // bottom
  3430. s->mb_y++;
  3431. tprintf("call mbaff filter_mb\n");
  3432. fill_caches(h, mb_type_bottom, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3433. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy+s->mb_stride]);
  3434. filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3435. } else {
  3436. tprintf("call filter_mb\n");
  3437. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3438. fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3439. filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3440. }
  3441. }
  3442. }
  3443. /**
  3444. * fills the default_ref_list.
  3445. */
  3446. static int fill_default_ref_list(H264Context *h){
  3447. MpegEncContext * const s = &h->s;
  3448. int i;
  3449. int smallest_poc_greater_than_current = -1;
  3450. Picture sorted_short_ref[32];
  3451. if(h->slice_type==B_TYPE){
  3452. int out_i;
  3453. int limit= INT_MIN;
  3454. /* sort frame according to poc in B slice */
  3455. for(out_i=0; out_i<h->short_ref_count; out_i++){
  3456. int best_i=INT_MIN;
  3457. int best_poc=INT_MAX;
  3458. for(i=0; i<h->short_ref_count; i++){
  3459. const int poc= h->short_ref[i]->poc;
  3460. if(poc > limit && poc < best_poc){
  3461. best_poc= poc;
  3462. best_i= i;
  3463. }
  3464. }
  3465. assert(best_i != INT_MIN);
  3466. limit= best_poc;
  3467. sorted_short_ref[out_i]= *h->short_ref[best_i];
  3468. 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);
  3469. if (-1 == smallest_poc_greater_than_current) {
  3470. if (h->short_ref[best_i]->poc >= s->current_picture_ptr->poc) {
  3471. smallest_poc_greater_than_current = out_i;
  3472. }
  3473. }
  3474. }
  3475. }
  3476. if(s->picture_structure == PICT_FRAME){
  3477. if(h->slice_type==B_TYPE){
  3478. int list;
  3479. tprintf("current poc: %d, smallest_poc_greater_than_current: %d\n", s->current_picture_ptr->poc, smallest_poc_greater_than_current);
  3480. // find the largest poc
  3481. for(list=0; list<2; list++){
  3482. int index = 0;
  3483. int j= -99;
  3484. int step= list ? -1 : 1;
  3485. for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++, j+=step) {
  3486. while(j<0 || j>= h->short_ref_count){
  3487. if(j != -99 && step == (list ? -1 : 1))
  3488. return -1;
  3489. step = -step;
  3490. j= smallest_poc_greater_than_current + (step>>1);
  3491. }
  3492. if(sorted_short_ref[j].reference != 3) continue;
  3493. h->default_ref_list[list][index ]= sorted_short_ref[j];
  3494. h->default_ref_list[list][index++].pic_id= sorted_short_ref[j].frame_num;
  3495. }
  3496. for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){
  3497. if(h->long_ref[i] == NULL) continue;
  3498. if(h->long_ref[i]->reference != 3) continue;
  3499. h->default_ref_list[ list ][index ]= *h->long_ref[i];
  3500. h->default_ref_list[ list ][index++].pic_id= i;;
  3501. }
  3502. if(list && (smallest_poc_greater_than_current<=0 || smallest_poc_greater_than_current>=h->short_ref_count) && (1 < index)){
  3503. // swap the two first elements of L1 when
  3504. // L0 and L1 are identical
  3505. Picture temp= h->default_ref_list[1][0];
  3506. h->default_ref_list[1][0] = h->default_ref_list[1][1];
  3507. h->default_ref_list[1][1] = temp;
  3508. }
  3509. if(index < h->ref_count[ list ])
  3510. memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
  3511. }
  3512. }else{
  3513. int index=0;
  3514. for(i=0; i<h->short_ref_count; i++){
  3515. if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
  3516. h->default_ref_list[0][index ]= *h->short_ref[i];
  3517. h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
  3518. }
  3519. for(i = 0; i < 16; i++){
  3520. if(h->long_ref[i] == NULL) continue;
  3521. if(h->long_ref[i]->reference != 3) continue;
  3522. h->default_ref_list[0][index ]= *h->long_ref[i];
  3523. h->default_ref_list[0][index++].pic_id= i;;
  3524. }
  3525. if(index < h->ref_count[0])
  3526. memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
  3527. }
  3528. }else{ //FIELD
  3529. if(h->slice_type==B_TYPE){
  3530. }else{
  3531. //FIXME second field balh
  3532. }
  3533. }
  3534. #ifdef TRACE
  3535. for (i=0; i<h->ref_count[0]; i++) {
  3536. 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]);
  3537. }
  3538. if(h->slice_type==B_TYPE){
  3539. for (i=0; i<h->ref_count[1]; i++) {
  3540. 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]);
  3541. }
  3542. }
  3543. #endif
  3544. return 0;
  3545. }
  3546. static void print_short_term(H264Context *h);
  3547. static void print_long_term(H264Context *h);
  3548. static int decode_ref_pic_list_reordering(H264Context *h){
  3549. MpegEncContext * const s = &h->s;
  3550. int list, index;
  3551. print_short_term(h);
  3552. print_long_term(h);
  3553. if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move before func
  3554. for(list=0; list<2; list++){
  3555. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  3556. if(get_bits1(&s->gb)){
  3557. int pred= h->curr_pic_num;
  3558. for(index=0; ; index++){
  3559. int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
  3560. int pic_id;
  3561. int i;
  3562. Picture *ref = NULL;
  3563. if(reordering_of_pic_nums_idc==3)
  3564. break;
  3565. if(index >= h->ref_count[list]){
  3566. av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
  3567. return -1;
  3568. }
  3569. if(reordering_of_pic_nums_idc<3){
  3570. if(reordering_of_pic_nums_idc<2){
  3571. const int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  3572. if(abs_diff_pic_num >= h->max_pic_num){
  3573. av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  3574. return -1;
  3575. }
  3576. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  3577. else pred+= abs_diff_pic_num;
  3578. pred &= h->max_pic_num - 1;
  3579. for(i= h->short_ref_count-1; i>=0; i--){
  3580. ref = h->short_ref[i];
  3581. assert(ref->reference == 3);
  3582. assert(!ref->long_ref);
  3583. if(ref->data[0] != NULL && ref->frame_num == pred && ref->long_ref == 0) // ignore non existing pictures by testing data[0] pointer
  3584. break;
  3585. }
  3586. if(i>=0)
  3587. ref->pic_id= ref->frame_num;
  3588. }else{
  3589. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  3590. ref = h->long_ref[pic_id];
  3591. ref->pic_id= pic_id;
  3592. assert(ref->reference == 3);
  3593. assert(ref->long_ref);
  3594. i=0;
  3595. }
  3596. if (i < 0) {
  3597. av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  3598. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  3599. } else {
  3600. for(i=index; i+1<h->ref_count[list]; i++){
  3601. if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
  3602. break;
  3603. }
  3604. for(; i > index; i--){
  3605. h->ref_list[list][i]= h->ref_list[list][i-1];
  3606. }
  3607. h->ref_list[list][index]= *ref;
  3608. }
  3609. }else{
  3610. av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  3611. return -1;
  3612. }
  3613. }
  3614. }
  3615. if(h->slice_type!=B_TYPE) break;
  3616. }
  3617. for(list=0; list<2; list++){
  3618. for(index= 0; index < h->ref_count[list]; index++){
  3619. if(!h->ref_list[list][index].data[0])
  3620. h->ref_list[list][index]= s->current_picture;
  3621. }
  3622. if(h->slice_type!=B_TYPE) break;
  3623. }
  3624. if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)
  3625. direct_dist_scale_factor(h);
  3626. direct_ref_list_init(h);
  3627. return 0;
  3628. }
  3629. static void fill_mbaff_ref_list(H264Context *h){
  3630. int list, i, j;
  3631. for(list=0; list<2; list++){
  3632. for(i=0; i<h->ref_count[list]; i++){
  3633. Picture *frame = &h->ref_list[list][i];
  3634. Picture *field = &h->ref_list[list][16+2*i];
  3635. field[0] = *frame;
  3636. for(j=0; j<3; j++)
  3637. field[0].linesize[j] <<= 1;
  3638. field[1] = field[0];
  3639. for(j=0; j<3; j++)
  3640. field[1].data[j] += frame->linesize[j];
  3641. h->luma_weight[list][16+2*i] = h->luma_weight[list][16+2*i+1] = h->luma_weight[list][i];
  3642. h->luma_offset[list][16+2*i] = h->luma_offset[list][16+2*i+1] = h->luma_offset[list][i];
  3643. for(j=0; j<2; j++){
  3644. h->chroma_weight[list][16+2*i][j] = h->chroma_weight[list][16+2*i+1][j] = h->chroma_weight[list][i][j];
  3645. h->chroma_offset[list][16+2*i][j] = h->chroma_offset[list][16+2*i+1][j] = h->chroma_offset[list][i][j];
  3646. }
  3647. }
  3648. }
  3649. for(j=0; j<h->ref_count[1]; j++){
  3650. for(i=0; i<h->ref_count[0]; i++)
  3651. h->implicit_weight[j][16+2*i] = h->implicit_weight[j][16+2*i+1] = h->implicit_weight[j][i];
  3652. memcpy(h->implicit_weight[16+2*j], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3653. memcpy(h->implicit_weight[16+2*j+1], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3654. }
  3655. }
  3656. static int pred_weight_table(H264Context *h){
  3657. MpegEncContext * const s = &h->s;
  3658. int list, i;
  3659. int luma_def, chroma_def;
  3660. h->use_weight= 0;
  3661. h->use_weight_chroma= 0;
  3662. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  3663. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  3664. luma_def = 1<<h->luma_log2_weight_denom;
  3665. chroma_def = 1<<h->chroma_log2_weight_denom;
  3666. for(list=0; list<2; list++){
  3667. for(i=0; i<h->ref_count[list]; i++){
  3668. int luma_weight_flag, chroma_weight_flag;
  3669. luma_weight_flag= get_bits1(&s->gb);
  3670. if(luma_weight_flag){
  3671. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  3672. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  3673. if( h->luma_weight[list][i] != luma_def
  3674. || h->luma_offset[list][i] != 0)
  3675. h->use_weight= 1;
  3676. }else{
  3677. h->luma_weight[list][i]= luma_def;
  3678. h->luma_offset[list][i]= 0;
  3679. }
  3680. chroma_weight_flag= get_bits1(&s->gb);
  3681. if(chroma_weight_flag){
  3682. int j;
  3683. for(j=0; j<2; j++){
  3684. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  3685. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  3686. if( h->chroma_weight[list][i][j] != chroma_def
  3687. || h->chroma_offset[list][i][j] != 0)
  3688. h->use_weight_chroma= 1;
  3689. }
  3690. }else{
  3691. int j;
  3692. for(j=0; j<2; j++){
  3693. h->chroma_weight[list][i][j]= chroma_def;
  3694. h->chroma_offset[list][i][j]= 0;
  3695. }
  3696. }
  3697. }
  3698. if(h->slice_type != B_TYPE) break;
  3699. }
  3700. h->use_weight= h->use_weight || h->use_weight_chroma;
  3701. return 0;
  3702. }
  3703. static void implicit_weight_table(H264Context *h){
  3704. MpegEncContext * const s = &h->s;
  3705. int ref0, ref1;
  3706. int cur_poc = s->current_picture_ptr->poc;
  3707. if( h->ref_count[0] == 1 && h->ref_count[1] == 1
  3708. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  3709. h->use_weight= 0;
  3710. h->use_weight_chroma= 0;
  3711. return;
  3712. }
  3713. h->use_weight= 2;
  3714. h->use_weight_chroma= 2;
  3715. h->luma_log2_weight_denom= 5;
  3716. h->chroma_log2_weight_denom= 5;
  3717. for(ref0=0; ref0 < h->ref_count[0]; ref0++){
  3718. int poc0 = h->ref_list[0][ref0].poc;
  3719. for(ref1=0; ref1 < h->ref_count[1]; ref1++){
  3720. int poc1 = h->ref_list[1][ref1].poc;
  3721. int td = clip(poc1 - poc0, -128, 127);
  3722. if(td){
  3723. int tb = clip(cur_poc - poc0, -128, 127);
  3724. int tx = (16384 + (FFABS(td) >> 1)) / td;
  3725. int dist_scale_factor = clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
  3726. if(dist_scale_factor < -64 || dist_scale_factor > 128)
  3727. h->implicit_weight[ref0][ref1] = 32;
  3728. else
  3729. h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
  3730. }else
  3731. h->implicit_weight[ref0][ref1] = 32;
  3732. }
  3733. }
  3734. }
  3735. static inline void unreference_pic(H264Context *h, Picture *pic){
  3736. int i;
  3737. pic->reference=0;
  3738. if(pic == h->delayed_output_pic)
  3739. pic->reference=1;
  3740. else{
  3741. for(i = 0; h->delayed_pic[i]; i++)
  3742. if(pic == h->delayed_pic[i]){
  3743. pic->reference=1;
  3744. break;
  3745. }
  3746. }
  3747. }
  3748. /**
  3749. * instantaneous decoder refresh.
  3750. */
  3751. static void idr(H264Context *h){
  3752. int i;
  3753. for(i=0; i<16; i++){
  3754. if (h->long_ref[i] != NULL) {
  3755. unreference_pic(h, h->long_ref[i]);
  3756. h->long_ref[i]= NULL;
  3757. }
  3758. }
  3759. h->long_ref_count=0;
  3760. for(i=0; i<h->short_ref_count; i++){
  3761. unreference_pic(h, h->short_ref[i]);
  3762. h->short_ref[i]= NULL;
  3763. }
  3764. h->short_ref_count=0;
  3765. }
  3766. /* forget old pics after a seek */
  3767. static void flush_dpb(AVCodecContext *avctx){
  3768. H264Context *h= avctx->priv_data;
  3769. int i;
  3770. for(i=0; i<16; i++) {
  3771. if(h->delayed_pic[i])
  3772. h->delayed_pic[i]->reference= 0;
  3773. h->delayed_pic[i]= NULL;
  3774. }
  3775. if(h->delayed_output_pic)
  3776. h->delayed_output_pic->reference= 0;
  3777. h->delayed_output_pic= NULL;
  3778. idr(h);
  3779. if(h->s.current_picture_ptr)
  3780. h->s.current_picture_ptr->reference= 0;
  3781. }
  3782. /**
  3783. *
  3784. * @return the removed picture or NULL if an error occurs
  3785. */
  3786. static Picture * remove_short(H264Context *h, int frame_num){
  3787. MpegEncContext * const s = &h->s;
  3788. int i;
  3789. if(s->avctx->debug&FF_DEBUG_MMCO)
  3790. av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  3791. for(i=0; i<h->short_ref_count; i++){
  3792. Picture *pic= h->short_ref[i];
  3793. if(s->avctx->debug&FF_DEBUG_MMCO)
  3794. av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  3795. if(pic->frame_num == frame_num){
  3796. h->short_ref[i]= NULL;
  3797. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
  3798. h->short_ref_count--;
  3799. return pic;
  3800. }
  3801. }
  3802. return NULL;
  3803. }
  3804. /**
  3805. *
  3806. * @return the removed picture or NULL if an error occurs
  3807. */
  3808. static Picture * remove_long(H264Context *h, int i){
  3809. Picture *pic;
  3810. pic= h->long_ref[i];
  3811. h->long_ref[i]= NULL;
  3812. if(pic) h->long_ref_count--;
  3813. return pic;
  3814. }
  3815. /**
  3816. * print short term list
  3817. */
  3818. static void print_short_term(H264Context *h) {
  3819. uint32_t i;
  3820. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3821. av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
  3822. for(i=0; i<h->short_ref_count; i++){
  3823. Picture *pic= h->short_ref[i];
  3824. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3825. }
  3826. }
  3827. }
  3828. /**
  3829. * print long term list
  3830. */
  3831. static void print_long_term(H264Context *h) {
  3832. uint32_t i;
  3833. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3834. av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
  3835. for(i = 0; i < 16; i++){
  3836. Picture *pic= h->long_ref[i];
  3837. if (pic) {
  3838. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3839. }
  3840. }
  3841. }
  3842. }
  3843. /**
  3844. * Executes the reference picture marking (memory management control operations).
  3845. */
  3846. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  3847. MpegEncContext * const s = &h->s;
  3848. int i, j;
  3849. int current_is_long=0;
  3850. Picture *pic;
  3851. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  3852. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  3853. for(i=0; i<mmco_count; i++){
  3854. if(s->avctx->debug&FF_DEBUG_MMCO)
  3855. 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);
  3856. switch(mmco[i].opcode){
  3857. case MMCO_SHORT2UNUSED:
  3858. pic= remove_short(h, mmco[i].short_frame_num);
  3859. if(pic)
  3860. unreference_pic(h, pic);
  3861. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3862. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_short() failure\n");
  3863. break;
  3864. case MMCO_SHORT2LONG:
  3865. pic= remove_long(h, mmco[i].long_index);
  3866. if(pic) unreference_pic(h, pic);
  3867. h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
  3868. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3869. h->long_ref_count++;
  3870. break;
  3871. case MMCO_LONG2UNUSED:
  3872. pic= remove_long(h, mmco[i].long_index);
  3873. if(pic)
  3874. unreference_pic(h, pic);
  3875. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3876. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_long() failure\n");
  3877. break;
  3878. case MMCO_LONG:
  3879. pic= remove_long(h, mmco[i].long_index);
  3880. if(pic) unreference_pic(h, pic);
  3881. h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
  3882. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3883. h->long_ref_count++;
  3884. current_is_long=1;
  3885. break;
  3886. case MMCO_SET_MAX_LONG:
  3887. assert(mmco[i].long_index <= 16);
  3888. // just remove the long term which index is greater than new max
  3889. for(j = mmco[i].long_index; j<16; j++){
  3890. pic = remove_long(h, j);
  3891. if (pic) unreference_pic(h, pic);
  3892. }
  3893. break;
  3894. case MMCO_RESET:
  3895. while(h->short_ref_count){
  3896. pic= remove_short(h, h->short_ref[0]->frame_num);
  3897. unreference_pic(h, pic);
  3898. }
  3899. for(j = 0; j < 16; j++) {
  3900. pic= remove_long(h, j);
  3901. if(pic) unreference_pic(h, pic);
  3902. }
  3903. break;
  3904. default: assert(0);
  3905. }
  3906. }
  3907. if(!current_is_long){
  3908. pic= remove_short(h, s->current_picture_ptr->frame_num);
  3909. if(pic){
  3910. unreference_pic(h, pic);
  3911. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  3912. }
  3913. if(h->short_ref_count)
  3914. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  3915. h->short_ref[0]= s->current_picture_ptr;
  3916. h->short_ref[0]->long_ref=0;
  3917. h->short_ref_count++;
  3918. }
  3919. print_short_term(h);
  3920. print_long_term(h);
  3921. return 0;
  3922. }
  3923. static int decode_ref_pic_marking(H264Context *h){
  3924. MpegEncContext * const s = &h->s;
  3925. int i;
  3926. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  3927. s->broken_link= get_bits1(&s->gb) -1;
  3928. h->mmco[0].long_index= get_bits1(&s->gb) - 1; // current_long_term_idx
  3929. if(h->mmco[0].long_index == -1)
  3930. h->mmco_index= 0;
  3931. else{
  3932. h->mmco[0].opcode= MMCO_LONG;
  3933. h->mmco_index= 1;
  3934. }
  3935. }else{
  3936. if(get_bits1(&s->gb)){ // adaptive_ref_pic_marking_mode_flag
  3937. for(i= 0; i<MAX_MMCO_COUNT; i++) {
  3938. MMCOOpcode opcode= get_ue_golomb(&s->gb);;
  3939. h->mmco[i].opcode= opcode;
  3940. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  3941. 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
  3942. /* if(h->mmco[i].short_frame_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_frame_num ] == NULL){
  3943. av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
  3944. return -1;
  3945. }*/
  3946. }
  3947. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  3948. h->mmco[i].long_index= get_ue_golomb(&s->gb);
  3949. 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){
  3950. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  3951. return -1;
  3952. }
  3953. }
  3954. if(opcode > MMCO_LONG){
  3955. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  3956. return -1;
  3957. }
  3958. if(opcode == MMCO_END)
  3959. break;
  3960. }
  3961. h->mmco_index= i;
  3962. }else{
  3963. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  3964. if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){ //FIXME fields
  3965. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  3966. h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  3967. h->mmco_index= 1;
  3968. }else
  3969. h->mmco_index= 0;
  3970. }
  3971. }
  3972. return 0;
  3973. }
  3974. static int init_poc(H264Context *h){
  3975. MpegEncContext * const s = &h->s;
  3976. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  3977. int field_poc[2];
  3978. if(h->nal_unit_type == NAL_IDR_SLICE){
  3979. h->frame_num_offset= 0;
  3980. }else{
  3981. if(h->frame_num < h->prev_frame_num)
  3982. h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
  3983. else
  3984. h->frame_num_offset= h->prev_frame_num_offset;
  3985. }
  3986. if(h->sps.poc_type==0){
  3987. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  3988. if(h->nal_unit_type == NAL_IDR_SLICE){
  3989. h->prev_poc_msb=
  3990. h->prev_poc_lsb= 0;
  3991. }
  3992. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  3993. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  3994. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  3995. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  3996. else
  3997. h->poc_msb = h->prev_poc_msb;
  3998. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  3999. field_poc[0] =
  4000. field_poc[1] = h->poc_msb + h->poc_lsb;
  4001. if(s->picture_structure == PICT_FRAME)
  4002. field_poc[1] += h->delta_poc_bottom;
  4003. }else if(h->sps.poc_type==1){
  4004. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  4005. int i;
  4006. if(h->sps.poc_cycle_length != 0)
  4007. abs_frame_num = h->frame_num_offset + h->frame_num;
  4008. else
  4009. abs_frame_num = 0;
  4010. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  4011. abs_frame_num--;
  4012. expected_delta_per_poc_cycle = 0;
  4013. for(i=0; i < h->sps.poc_cycle_length; i++)
  4014. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  4015. if(abs_frame_num > 0){
  4016. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  4017. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  4018. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  4019. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  4020. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  4021. } else
  4022. expectedpoc = 0;
  4023. if(h->nal_ref_idc == 0)
  4024. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  4025. field_poc[0] = expectedpoc + h->delta_poc[0];
  4026. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  4027. if(s->picture_structure == PICT_FRAME)
  4028. field_poc[1] += h->delta_poc[1];
  4029. }else{
  4030. int poc;
  4031. if(h->nal_unit_type == NAL_IDR_SLICE){
  4032. poc= 0;
  4033. }else{
  4034. if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
  4035. else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
  4036. }
  4037. field_poc[0]= poc;
  4038. field_poc[1]= poc;
  4039. }
  4040. if(s->picture_structure != PICT_BOTTOM_FIELD)
  4041. s->current_picture_ptr->field_poc[0]= field_poc[0];
  4042. if(s->picture_structure != PICT_TOP_FIELD)
  4043. s->current_picture_ptr->field_poc[1]= field_poc[1];
  4044. if(s->picture_structure == PICT_FRAME) // FIXME field pix?
  4045. s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]);
  4046. return 0;
  4047. }
  4048. /**
  4049. * decodes a slice header.
  4050. * this will allso call MPV_common_init() and frame_start() as needed
  4051. */
  4052. static int decode_slice_header(H264Context *h){
  4053. MpegEncContext * const s = &h->s;
  4054. int first_mb_in_slice, pps_id;
  4055. int num_ref_idx_active_override_flag;
  4056. static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
  4057. int slice_type;
  4058. int default_ref_list_done = 0;
  4059. s->current_picture.reference= h->nal_ref_idc != 0;
  4060. s->dropable= h->nal_ref_idc == 0;
  4061. first_mb_in_slice= get_ue_golomb(&s->gb);
  4062. slice_type= get_ue_golomb(&s->gb);
  4063. if(slice_type > 9){
  4064. 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);
  4065. return -1;
  4066. }
  4067. if(slice_type > 4){
  4068. slice_type -= 5;
  4069. h->slice_type_fixed=1;
  4070. }else
  4071. h->slice_type_fixed=0;
  4072. slice_type= slice_type_map[ slice_type ];
  4073. if (slice_type == I_TYPE
  4074. || (h->slice_num != 0 && slice_type == h->slice_type) ) {
  4075. default_ref_list_done = 1;
  4076. }
  4077. h->slice_type= slice_type;
  4078. s->pict_type= h->slice_type; // to make a few old func happy, it's wrong though
  4079. pps_id= get_ue_golomb(&s->gb);
  4080. if(pps_id>255){
  4081. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  4082. return -1;
  4083. }
  4084. h->pps= h->pps_buffer[pps_id];
  4085. if(h->pps.slice_group_count == 0){
  4086. av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referenced\n");
  4087. return -1;
  4088. }
  4089. h->sps= h->sps_buffer[ h->pps.sps_id ];
  4090. if(h->sps.log2_max_frame_num == 0){
  4091. av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referenced\n");
  4092. return -1;
  4093. }
  4094. if(h->dequant_coeff_pps != pps_id){
  4095. h->dequant_coeff_pps = pps_id;
  4096. init_dequant_tables(h);
  4097. }
  4098. s->mb_width= h->sps.mb_width;
  4099. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  4100. h->b_stride= s->mb_width*4;
  4101. h->b8_stride= s->mb_width*2;
  4102. s->width = 16*s->mb_width - 2*(h->sps.crop_left + h->sps.crop_right );
  4103. if(h->sps.frame_mbs_only_flag)
  4104. s->height= 16*s->mb_height - 2*(h->sps.crop_top + h->sps.crop_bottom);
  4105. else
  4106. s->height= 16*s->mb_height - 4*(h->sps.crop_top + h->sps.crop_bottom); //FIXME recheck
  4107. if (s->context_initialized
  4108. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  4109. free_tables(h);
  4110. MPV_common_end(s);
  4111. }
  4112. if (!s->context_initialized) {
  4113. if (MPV_common_init(s) < 0)
  4114. return -1;
  4115. if(s->dsp.h264_idct_add == ff_h264_idct_add_c){ //FIXME little ugly
  4116. memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
  4117. memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
  4118. }else{
  4119. int i;
  4120. for(i=0; i<16; i++){
  4121. #define T(x) (x>>2) | ((x<<2) & 0xF)
  4122. h->zigzag_scan[i] = T(zigzag_scan[i]);
  4123. h-> field_scan[i] = T( field_scan[i]);
  4124. #undef T
  4125. }
  4126. }
  4127. if(s->dsp.h264_idct8_add == ff_h264_idct8_add_c){
  4128. memcpy(h->zigzag_scan8x8, zigzag_scan8x8, 64*sizeof(uint8_t));
  4129. memcpy(h->zigzag_scan8x8_cavlc, zigzag_scan8x8_cavlc, 64*sizeof(uint8_t));
  4130. memcpy(h->field_scan8x8, field_scan8x8, 64*sizeof(uint8_t));
  4131. memcpy(h->field_scan8x8_cavlc, field_scan8x8_cavlc, 64*sizeof(uint8_t));
  4132. }else{
  4133. int i;
  4134. for(i=0; i<64; i++){
  4135. #define T(x) (x>>3) | ((x&7)<<3)
  4136. h->zigzag_scan8x8[i] = T(zigzag_scan8x8[i]);
  4137. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  4138. h->field_scan8x8[i] = T(field_scan8x8[i]);
  4139. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  4140. #undef T
  4141. }
  4142. }
  4143. if(h->sps.transform_bypass){ //FIXME same ugly
  4144. h->zigzag_scan_q0 = zigzag_scan;
  4145. h->zigzag_scan8x8_q0 = zigzag_scan8x8;
  4146. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  4147. h->field_scan_q0 = field_scan;
  4148. h->field_scan8x8_q0 = field_scan8x8;
  4149. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  4150. }else{
  4151. h->zigzag_scan_q0 = h->zigzag_scan;
  4152. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  4153. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  4154. h->field_scan_q0 = h->field_scan;
  4155. h->field_scan8x8_q0 = h->field_scan8x8;
  4156. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  4157. }
  4158. alloc_tables(h);
  4159. s->avctx->width = s->width;
  4160. s->avctx->height = s->height;
  4161. s->avctx->sample_aspect_ratio= h->sps.sar;
  4162. if(!s->avctx->sample_aspect_ratio.den)
  4163. s->avctx->sample_aspect_ratio.den = 1;
  4164. if(h->sps.timing_info_present_flag){
  4165. s->avctx->time_base= (AVRational){h->sps.num_units_in_tick * 2, h->sps.time_scale};
  4166. if(h->x264_build > 0 && h->x264_build < 44)
  4167. s->avctx->time_base.den *= 2;
  4168. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  4169. s->avctx->time_base.num, s->avctx->time_base.den, 1<<30);
  4170. }
  4171. }
  4172. if(h->slice_num == 0){
  4173. if(frame_start(h) < 0)
  4174. return -1;
  4175. }
  4176. s->current_picture_ptr->frame_num= //FIXME frame_num cleanup
  4177. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  4178. h->mb_mbaff = 0;
  4179. h->mb_aff_frame = 0;
  4180. if(h->sps.frame_mbs_only_flag){
  4181. s->picture_structure= PICT_FRAME;
  4182. }else{
  4183. if(get_bits1(&s->gb)) { //field_pic_flag
  4184. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  4185. av_log(h->s.avctx, AV_LOG_ERROR, "PAFF interlacing is not implemented\n");
  4186. } else {
  4187. s->picture_structure= PICT_FRAME;
  4188. h->mb_aff_frame = h->sps.mb_aff;
  4189. }
  4190. }
  4191. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  4192. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << h->mb_aff_frame;
  4193. if(s->mb_y >= s->mb_height){
  4194. return -1;
  4195. }
  4196. if(s->picture_structure==PICT_FRAME){
  4197. h->curr_pic_num= h->frame_num;
  4198. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  4199. }else{
  4200. h->curr_pic_num= 2*h->frame_num;
  4201. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  4202. }
  4203. if(h->nal_unit_type == NAL_IDR_SLICE){
  4204. get_ue_golomb(&s->gb); /* idr_pic_id */
  4205. }
  4206. if(h->sps.poc_type==0){
  4207. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  4208. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  4209. h->delta_poc_bottom= get_se_golomb(&s->gb);
  4210. }
  4211. }
  4212. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  4213. h->delta_poc[0]= get_se_golomb(&s->gb);
  4214. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  4215. h->delta_poc[1]= get_se_golomb(&s->gb);
  4216. }
  4217. init_poc(h);
  4218. if(h->pps.redundant_pic_cnt_present){
  4219. h->redundant_pic_count= get_ue_golomb(&s->gb);
  4220. }
  4221. //set defaults, might be overriden a few line later
  4222. h->ref_count[0]= h->pps.ref_count[0];
  4223. h->ref_count[1]= h->pps.ref_count[1];
  4224. if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){
  4225. if(h->slice_type == B_TYPE){
  4226. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  4227. if(h->sps.mb_aff && h->direct_spatial_mv_pred)
  4228. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + spatial direct mode is not implemented\n");
  4229. }
  4230. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  4231. if(num_ref_idx_active_override_flag){
  4232. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  4233. if(h->slice_type==B_TYPE)
  4234. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  4235. if(h->ref_count[0] > 32 || h->ref_count[1] > 32){
  4236. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  4237. return -1;
  4238. }
  4239. }
  4240. }
  4241. if(!default_ref_list_done){
  4242. fill_default_ref_list(h);
  4243. }
  4244. if(decode_ref_pic_list_reordering(h) < 0)
  4245. return -1;
  4246. if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
  4247. || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
  4248. pred_weight_table(h);
  4249. else if(h->pps.weighted_bipred_idc==2 && h->slice_type==B_TYPE)
  4250. implicit_weight_table(h);
  4251. else
  4252. h->use_weight = 0;
  4253. if(s->current_picture.reference)
  4254. decode_ref_pic_marking(h);
  4255. if(FRAME_MBAFF)
  4256. fill_mbaff_ref_list(h);
  4257. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac )
  4258. h->cabac_init_idc = get_ue_golomb(&s->gb);
  4259. h->last_qscale_diff = 0;
  4260. s->qscale = h->pps.init_qp + get_se_golomb(&s->gb);
  4261. if(s->qscale<0 || s->qscale>51){
  4262. av_log(s->avctx, AV_LOG_ERROR, "QP %d out of range\n", s->qscale);
  4263. return -1;
  4264. }
  4265. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4266. //FIXME qscale / qp ... stuff
  4267. if(h->slice_type == SP_TYPE){
  4268. get_bits1(&s->gb); /* sp_for_switch_flag */
  4269. }
  4270. if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
  4271. get_se_golomb(&s->gb); /* slice_qs_delta */
  4272. }
  4273. h->deblocking_filter = 1;
  4274. h->slice_alpha_c0_offset = 0;
  4275. h->slice_beta_offset = 0;
  4276. if( h->pps.deblocking_filter_parameters_present ) {
  4277. h->deblocking_filter= get_ue_golomb(&s->gb);
  4278. if(h->deblocking_filter < 2)
  4279. h->deblocking_filter^= 1; // 1<->0
  4280. if( h->deblocking_filter ) {
  4281. h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
  4282. h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
  4283. }
  4284. }
  4285. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  4286. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type != I_TYPE)
  4287. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type == B_TYPE)
  4288. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  4289. h->deblocking_filter= 0;
  4290. #if 0 //FMO
  4291. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  4292. slice_group_change_cycle= get_bits(&s->gb, ?);
  4293. #endif
  4294. h->slice_num++;
  4295. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
  4296. h->emu_edge_height= FRAME_MBAFF ? 0 : h->emu_edge_width;
  4297. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  4298. 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",
  4299. h->slice_num,
  4300. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  4301. first_mb_in_slice,
  4302. av_get_pict_type_char(h->slice_type),
  4303. pps_id, h->frame_num,
  4304. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  4305. h->ref_count[0], h->ref_count[1],
  4306. s->qscale,
  4307. h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2,
  4308. h->use_weight,
  4309. h->use_weight==1 && h->use_weight_chroma ? "c" : ""
  4310. );
  4311. }
  4312. if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !s->current_picture.reference){
  4313. s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
  4314. s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
  4315. }else{
  4316. s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
  4317. s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
  4318. }
  4319. return 0;
  4320. }
  4321. /**
  4322. *
  4323. */
  4324. static inline int get_level_prefix(GetBitContext *gb){
  4325. unsigned int buf;
  4326. int log;
  4327. OPEN_READER(re, gb);
  4328. UPDATE_CACHE(re, gb);
  4329. buf=GET_CACHE(re, gb);
  4330. log= 32 - av_log2(buf);
  4331. #ifdef TRACE
  4332. print_bin(buf>>(32-log), log);
  4333. 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__);
  4334. #endif
  4335. LAST_SKIP_BITS(re, gb, log);
  4336. CLOSE_READER(re, gb);
  4337. return log-1;
  4338. }
  4339. static inline int get_dct8x8_allowed(H264Context *h){
  4340. int i;
  4341. for(i=0; i<4; i++){
  4342. if(!IS_SUB_8X8(h->sub_mb_type[i])
  4343. || (!h->sps.direct_8x8_inference_flag && IS_DIRECT(h->sub_mb_type[i])))
  4344. return 0;
  4345. }
  4346. return 1;
  4347. }
  4348. /**
  4349. * decodes a residual block.
  4350. * @param n block index
  4351. * @param scantable scantable
  4352. * @param max_coeff number of coefficients in the block
  4353. * @return <0 if an error occured
  4354. */
  4355. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
  4356. MpegEncContext * const s = &h->s;
  4357. 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};
  4358. int level[16];
  4359. int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
  4360. //FIXME put trailing_onex into the context
  4361. if(n == CHROMA_DC_BLOCK_INDEX){
  4362. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  4363. total_coeff= coeff_token>>2;
  4364. }else{
  4365. if(n == LUMA_DC_BLOCK_INDEX){
  4366. total_coeff= pred_non_zero_count(h, 0);
  4367. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4368. total_coeff= coeff_token>>2;
  4369. }else{
  4370. total_coeff= pred_non_zero_count(h, n);
  4371. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4372. total_coeff= coeff_token>>2;
  4373. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  4374. }
  4375. }
  4376. //FIXME set last_non_zero?
  4377. if(total_coeff==0)
  4378. return 0;
  4379. trailing_ones= coeff_token&3;
  4380. tprintf("trailing:%d, total:%d\n", trailing_ones, total_coeff);
  4381. assert(total_coeff<=16);
  4382. for(i=0; i<trailing_ones; i++){
  4383. level[i]= 1 - 2*get_bits1(gb);
  4384. }
  4385. if(i<total_coeff) {
  4386. int level_code, mask;
  4387. int suffix_length = total_coeff > 10 && trailing_ones < 3;
  4388. int prefix= get_level_prefix(gb);
  4389. //first coefficient has suffix_length equal to 0 or 1
  4390. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  4391. if(suffix_length)
  4392. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4393. else
  4394. level_code= (prefix<<suffix_length); //part
  4395. }else if(prefix==14){
  4396. if(suffix_length)
  4397. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4398. else
  4399. level_code= prefix + get_bits(gb, 4); //part
  4400. }else if(prefix==15){
  4401. level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part
  4402. if(suffix_length==0) level_code+=15; //FIXME doesn't make (much)sense
  4403. }else{
  4404. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4405. return -1;
  4406. }
  4407. if(trailing_ones < 3) level_code += 2;
  4408. suffix_length = 1;
  4409. if(level_code > 5)
  4410. suffix_length++;
  4411. mask= -(level_code&1);
  4412. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4413. i++;
  4414. //remaining coefficients have suffix_length > 0
  4415. for(;i<total_coeff;i++) {
  4416. static const int suffix_limit[7] = {0,5,11,23,47,95,INT_MAX };
  4417. prefix = get_level_prefix(gb);
  4418. if(prefix<15){
  4419. level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
  4420. }else if(prefix==15){
  4421. level_code = (prefix<<suffix_length) + get_bits(gb, 12);
  4422. }else{
  4423. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4424. return -1;
  4425. }
  4426. mask= -(level_code&1);
  4427. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4428. if(level_code > suffix_limit[suffix_length])
  4429. suffix_length++;
  4430. }
  4431. }
  4432. if(total_coeff == max_coeff)
  4433. zeros_left=0;
  4434. else{
  4435. if(n == CHROMA_DC_BLOCK_INDEX)
  4436. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  4437. else
  4438. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  4439. }
  4440. coeff_num = zeros_left + total_coeff - 1;
  4441. j = scantable[coeff_num];
  4442. if(n > 24){
  4443. block[j] = level[0];
  4444. for(i=1;i<total_coeff;i++) {
  4445. if(zeros_left <= 0)
  4446. run_before = 0;
  4447. else if(zeros_left < 7){
  4448. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4449. }else{
  4450. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4451. }
  4452. zeros_left -= run_before;
  4453. coeff_num -= 1 + run_before;
  4454. j= scantable[ coeff_num ];
  4455. block[j]= level[i];
  4456. }
  4457. }else{
  4458. block[j] = (level[0] * qmul[j] + 32)>>6;
  4459. for(i=1;i<total_coeff;i++) {
  4460. if(zeros_left <= 0)
  4461. run_before = 0;
  4462. else if(zeros_left < 7){
  4463. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4464. }else{
  4465. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4466. }
  4467. zeros_left -= run_before;
  4468. coeff_num -= 1 + run_before;
  4469. j= scantable[ coeff_num ];
  4470. block[j]= (level[i] * qmul[j] + 32)>>6;
  4471. }
  4472. }
  4473. if(zeros_left<0){
  4474. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  4475. return -1;
  4476. }
  4477. return 0;
  4478. }
  4479. static void predict_field_decoding_flag(H264Context *h){
  4480. MpegEncContext * const s = &h->s;
  4481. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4482. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  4483. ? s->current_picture.mb_type[mb_xy-1]
  4484. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  4485. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  4486. : 0;
  4487. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  4488. }
  4489. /**
  4490. * decodes a P_SKIP or B_SKIP macroblock
  4491. */
  4492. static void decode_mb_skip(H264Context *h){
  4493. MpegEncContext * const s = &h->s;
  4494. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4495. int mb_type=0;
  4496. memset(h->non_zero_count[mb_xy], 0, 16);
  4497. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  4498. if(MB_FIELD)
  4499. mb_type|= MB_TYPE_INTERLACED;
  4500. if( h->slice_type == B_TYPE )
  4501. {
  4502. // just for fill_caches. pred_direct_motion will set the real mb_type
  4503. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
  4504. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4505. pred_direct_motion(h, &mb_type);
  4506. mb_type|= MB_TYPE_SKIP;
  4507. }
  4508. else
  4509. {
  4510. int mx, my;
  4511. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
  4512. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4513. pred_pskip_motion(h, &mx, &my);
  4514. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  4515. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  4516. }
  4517. write_back_motion(h, mb_type);
  4518. s->current_picture.mb_type[mb_xy]= mb_type;
  4519. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4520. h->slice_table[ mb_xy ]= h->slice_num;
  4521. h->prev_mb_skipped= 1;
  4522. }
  4523. /**
  4524. * decodes a macroblock
  4525. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  4526. */
  4527. static int decode_mb_cavlc(H264Context *h){
  4528. MpegEncContext * const s = &h->s;
  4529. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4530. int mb_type, partition_count, cbp;
  4531. int dct8x8_allowed= h->pps.transform_8x8_mode;
  4532. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?
  4533. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  4534. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  4535. down the code */
  4536. if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){
  4537. if(s->mb_skip_run==-1)
  4538. s->mb_skip_run= get_ue_golomb(&s->gb);
  4539. if (s->mb_skip_run--) {
  4540. if(FRAME_MBAFF && (s->mb_y&1) == 0){
  4541. if(s->mb_skip_run==0)
  4542. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4543. else
  4544. predict_field_decoding_flag(h);
  4545. }
  4546. decode_mb_skip(h);
  4547. return 0;
  4548. }
  4549. }
  4550. if(FRAME_MBAFF){
  4551. if( (s->mb_y&1) == 0 )
  4552. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4553. }else
  4554. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  4555. h->prev_mb_skipped= 0;
  4556. mb_type= get_ue_golomb(&s->gb);
  4557. if(h->slice_type == B_TYPE){
  4558. if(mb_type < 23){
  4559. partition_count= b_mb_type_info[mb_type].partition_count;
  4560. mb_type= b_mb_type_info[mb_type].type;
  4561. }else{
  4562. mb_type -= 23;
  4563. goto decode_intra_mb;
  4564. }
  4565. }else if(h->slice_type == P_TYPE /*|| h->slice_type == SP_TYPE */){
  4566. if(mb_type < 5){
  4567. partition_count= p_mb_type_info[mb_type].partition_count;
  4568. mb_type= p_mb_type_info[mb_type].type;
  4569. }else{
  4570. mb_type -= 5;
  4571. goto decode_intra_mb;
  4572. }
  4573. }else{
  4574. assert(h->slice_type == I_TYPE);
  4575. decode_intra_mb:
  4576. if(mb_type > 25){
  4577. av_log(h->s.avctx, AV_LOG_ERROR, "mb_type %d in %c slice too large at %d %d\n", mb_type, av_get_pict_type_char(h->slice_type), s->mb_x, s->mb_y);
  4578. return -1;
  4579. }
  4580. partition_count=0;
  4581. cbp= i_mb_type_info[mb_type].cbp;
  4582. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  4583. mb_type= i_mb_type_info[mb_type].type;
  4584. }
  4585. if(MB_FIELD)
  4586. mb_type |= MB_TYPE_INTERLACED;
  4587. h->slice_table[ mb_xy ]= h->slice_num;
  4588. if(IS_INTRA_PCM(mb_type)){
  4589. unsigned int x, y;
  4590. // we assume these blocks are very rare so we dont optimize it
  4591. align_get_bits(&s->gb);
  4592. // The pixels are stored in the same order as levels in h->mb array.
  4593. for(y=0; y<16; y++){
  4594. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  4595. for(x=0; x<16; x++){
  4596. tprintf("LUMA ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4597. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= get_bits(&s->gb, 8);
  4598. }
  4599. }
  4600. for(y=0; y<8; y++){
  4601. const int index= 256 + 4*(y&3) + 32*(y>>2);
  4602. for(x=0; x<8; x++){
  4603. tprintf("CHROMA U ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4604. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4605. }
  4606. }
  4607. for(y=0; y<8; y++){
  4608. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  4609. for(x=0; x<8; x++){
  4610. tprintf("CHROMA V ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4611. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4612. }
  4613. }
  4614. // In deblocking, the quantizer is 0
  4615. s->current_picture.qscale_table[mb_xy]= 0;
  4616. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  4617. // All coeffs are present
  4618. memset(h->non_zero_count[mb_xy], 16, 16);
  4619. s->current_picture.mb_type[mb_xy]= mb_type;
  4620. return 0;
  4621. }
  4622. if(MB_MBAFF){
  4623. h->ref_count[0] <<= 1;
  4624. h->ref_count[1] <<= 1;
  4625. }
  4626. fill_caches(h, mb_type, 0);
  4627. //mb_pred
  4628. if(IS_INTRA(mb_type)){
  4629. // init_top_left_availability(h);
  4630. if(IS_INTRA4x4(mb_type)){
  4631. int i;
  4632. int di = 1;
  4633. if(dct8x8_allowed && get_bits1(&s->gb)){
  4634. mb_type |= MB_TYPE_8x8DCT;
  4635. di = 4;
  4636. }
  4637. // fill_intra4x4_pred_table(h);
  4638. for(i=0; i<16; i+=di){
  4639. int mode= pred_intra_mode(h, i);
  4640. if(!get_bits1(&s->gb)){
  4641. const int rem_mode= get_bits(&s->gb, 3);
  4642. mode = rem_mode + (rem_mode >= mode);
  4643. }
  4644. if(di==4)
  4645. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  4646. else
  4647. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  4648. }
  4649. write_back_intra_pred_mode(h);
  4650. if( check_intra4x4_pred_mode(h) < 0)
  4651. return -1;
  4652. }else{
  4653. h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
  4654. if(h->intra16x16_pred_mode < 0)
  4655. return -1;
  4656. }
  4657. h->chroma_pred_mode= get_ue_golomb(&s->gb);
  4658. h->chroma_pred_mode= check_intra_pred_mode(h, h->chroma_pred_mode);
  4659. if(h->chroma_pred_mode < 0)
  4660. return -1;
  4661. }else if(partition_count==4){
  4662. int i, j, sub_partition_count[4], list, ref[2][4];
  4663. if(h->slice_type == B_TYPE){
  4664. for(i=0; i<4; i++){
  4665. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4666. if(h->sub_mb_type[i] >=13){
  4667. 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);
  4668. return -1;
  4669. }
  4670. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4671. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4672. }
  4673. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  4674. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
  4675. pred_direct_motion(h, &mb_type);
  4676. h->ref_cache[0][scan8[4]] =
  4677. h->ref_cache[1][scan8[4]] =
  4678. h->ref_cache[0][scan8[12]] =
  4679. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  4680. }
  4681. }else{
  4682. assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE); //FIXME SP correct ?
  4683. for(i=0; i<4; i++){
  4684. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4685. if(h->sub_mb_type[i] >=4){
  4686. 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);
  4687. return -1;
  4688. }
  4689. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4690. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4691. }
  4692. }
  4693. for(list=0; list<2; list++){
  4694. int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4695. if(ref_count == 0) continue;
  4696. for(i=0; i<4; i++){
  4697. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  4698. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4699. ref[list][i] = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
  4700. }else{
  4701. //FIXME
  4702. ref[list][i] = -1;
  4703. }
  4704. }
  4705. }
  4706. if(dct8x8_allowed)
  4707. dct8x8_allowed = get_dct8x8_allowed(h);
  4708. for(list=0; list<2; list++){
  4709. const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4710. if(ref_count == 0) continue;
  4711. for(i=0; i<4; i++){
  4712. if(IS_DIRECT(h->sub_mb_type[i])) {
  4713. h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];
  4714. continue;
  4715. }
  4716. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  4717. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  4718. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4719. const int sub_mb_type= h->sub_mb_type[i];
  4720. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  4721. for(j=0; j<sub_partition_count[i]; j++){
  4722. int mx, my;
  4723. const int index= 4*i + block_width*j;
  4724. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  4725. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  4726. mx += get_se_golomb(&s->gb);
  4727. my += get_se_golomb(&s->gb);
  4728. tprintf("final mv:%d %d\n", mx, my);
  4729. if(IS_SUB_8X8(sub_mb_type)){
  4730. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  4731. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  4732. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  4733. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  4734. }else if(IS_SUB_8X4(sub_mb_type)){
  4735. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  4736. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  4737. }else if(IS_SUB_4X8(sub_mb_type)){
  4738. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  4739. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  4740. }else{
  4741. assert(IS_SUB_4X4(sub_mb_type));
  4742. mv_cache[ 0 ][0]= mx;
  4743. mv_cache[ 0 ][1]= my;
  4744. }
  4745. }
  4746. }else{
  4747. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  4748. p[0] = p[1]=
  4749. p[8] = p[9]= 0;
  4750. }
  4751. }
  4752. }
  4753. }else if(IS_DIRECT(mb_type)){
  4754. pred_direct_motion(h, &mb_type);
  4755. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  4756. }else{
  4757. int list, mx, my, i;
  4758. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  4759. if(IS_16X16(mb_type)){
  4760. for(list=0; list<2; list++){
  4761. if(h->ref_count[list]>0){
  4762. if(IS_DIR(mb_type, 0, list)){
  4763. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4764. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  4765. }else
  4766. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4767. }
  4768. }
  4769. for(list=0; list<2; list++){
  4770. if(IS_DIR(mb_type, 0, list)){
  4771. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  4772. mx += get_se_golomb(&s->gb);
  4773. my += get_se_golomb(&s->gb);
  4774. tprintf("final mv:%d %d\n", mx, my);
  4775. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  4776. }else
  4777. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  4778. }
  4779. }
  4780. else if(IS_16X8(mb_type)){
  4781. for(list=0; list<2; list++){
  4782. if(h->ref_count[list]>0){
  4783. for(i=0; i<2; i++){
  4784. if(IS_DIR(mb_type, i, list)){
  4785. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4786. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  4787. }else
  4788. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  4789. }
  4790. }
  4791. }
  4792. for(list=0; list<2; list++){
  4793. for(i=0; i<2; i++){
  4794. if(IS_DIR(mb_type, i, list)){
  4795. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  4796. mx += get_se_golomb(&s->gb);
  4797. my += get_se_golomb(&s->gb);
  4798. tprintf("final mv:%d %d\n", mx, my);
  4799. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  4800. }else
  4801. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  4802. }
  4803. }
  4804. }else{
  4805. assert(IS_8X16(mb_type));
  4806. for(list=0; list<2; list++){
  4807. if(h->ref_count[list]>0){
  4808. for(i=0; i<2; i++){
  4809. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  4810. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4811. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  4812. }else
  4813. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4814. }
  4815. }
  4816. }
  4817. for(list=0; list<2; list++){
  4818. for(i=0; i<2; i++){
  4819. if(IS_DIR(mb_type, i, list)){
  4820. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  4821. mx += get_se_golomb(&s->gb);
  4822. my += get_se_golomb(&s->gb);
  4823. tprintf("final mv:%d %d\n", mx, my);
  4824. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  4825. }else
  4826. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  4827. }
  4828. }
  4829. }
  4830. }
  4831. if(IS_INTER(mb_type))
  4832. write_back_motion(h, mb_type);
  4833. if(!IS_INTRA16x16(mb_type)){
  4834. cbp= get_ue_golomb(&s->gb);
  4835. if(cbp > 47){
  4836. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%d) at %d %d\n", cbp, s->mb_x, s->mb_y);
  4837. return -1;
  4838. }
  4839. if(IS_INTRA4x4(mb_type))
  4840. cbp= golomb_to_intra4x4_cbp[cbp];
  4841. else
  4842. cbp= golomb_to_inter_cbp[cbp];
  4843. }
  4844. h->cbp = cbp;
  4845. if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
  4846. if(get_bits1(&s->gb))
  4847. mb_type |= MB_TYPE_8x8DCT;
  4848. }
  4849. s->current_picture.mb_type[mb_xy]= mb_type;
  4850. if(cbp || IS_INTRA16x16(mb_type)){
  4851. int i8x8, i4x4, chroma_idx;
  4852. int chroma_qp, dquant;
  4853. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  4854. const uint8_t *scan, *scan8x8, *dc_scan;
  4855. // fill_non_zero_count_cache(h);
  4856. if(IS_INTERLACED(mb_type)){
  4857. scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
  4858. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  4859. dc_scan= luma_dc_field_scan;
  4860. }else{
  4861. scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
  4862. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  4863. dc_scan= luma_dc_zigzag_scan;
  4864. }
  4865. dquant= get_se_golomb(&s->gb);
  4866. if( dquant > 25 || dquant < -26 ){
  4867. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  4868. return -1;
  4869. }
  4870. s->qscale += dquant;
  4871. if(((unsigned)s->qscale) > 51){
  4872. if(s->qscale<0) s->qscale+= 52;
  4873. else s->qscale-= 52;
  4874. }
  4875. h->chroma_qp= chroma_qp= get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4876. if(IS_INTRA16x16(mb_type)){
  4877. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){
  4878. return -1; //FIXME continue if partitioned and other return -1 too
  4879. }
  4880. assert((cbp&15) == 0 || (cbp&15) == 15);
  4881. if(cbp&15){
  4882. for(i8x8=0; i8x8<4; i8x8++){
  4883. for(i4x4=0; i4x4<4; i4x4++){
  4884. const int index= i4x4 + 4*i8x8;
  4885. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){
  4886. return -1;
  4887. }
  4888. }
  4889. }
  4890. }else{
  4891. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  4892. }
  4893. }else{
  4894. for(i8x8=0; i8x8<4; i8x8++){
  4895. if(cbp & (1<<i8x8)){
  4896. if(IS_8x8DCT(mb_type)){
  4897. DCTELEM *buf = &h->mb[64*i8x8];
  4898. uint8_t *nnz;
  4899. for(i4x4=0; i4x4<4; i4x4++){
  4900. if( decode_residual(h, gb, buf, i4x4+4*i8x8, scan8x8+16*i4x4,
  4901. h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )
  4902. return -1;
  4903. }
  4904. nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4905. nnz[0] += nnz[1] + nnz[8] + nnz[9];
  4906. }else{
  4907. for(i4x4=0; i4x4<4; i4x4++){
  4908. const int index= i4x4 + 4*i8x8;
  4909. if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){
  4910. return -1;
  4911. }
  4912. }
  4913. }
  4914. }else{
  4915. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4916. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  4917. }
  4918. }
  4919. }
  4920. if(cbp&0x30){
  4921. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  4922. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){
  4923. return -1;
  4924. }
  4925. }
  4926. if(cbp&0x20){
  4927. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  4928. for(i4x4=0; i4x4<4; i4x4++){
  4929. const int index= 16 + 4*chroma_idx + i4x4;
  4930. 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){
  4931. return -1;
  4932. }
  4933. }
  4934. }
  4935. }else{
  4936. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4937. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4938. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4939. }
  4940. }else{
  4941. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4942. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  4943. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4944. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4945. }
  4946. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4947. write_back_non_zero_count(h);
  4948. if(MB_MBAFF){
  4949. h->ref_count[0] >>= 1;
  4950. h->ref_count[1] >>= 1;
  4951. }
  4952. return 0;
  4953. }
  4954. static int decode_cabac_field_decoding_flag(H264Context *h) {
  4955. MpegEncContext * const s = &h->s;
  4956. const int mb_x = s->mb_x;
  4957. const int mb_y = s->mb_y & ~1;
  4958. const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
  4959. const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
  4960. unsigned int ctx = 0;
  4961. if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
  4962. ctx += 1;
  4963. }
  4964. if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
  4965. ctx += 1;
  4966. }
  4967. return get_cabac_noinline( &h->cabac, &h->cabac_state[70 + ctx] );
  4968. }
  4969. static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
  4970. uint8_t *state= &h->cabac_state[ctx_base];
  4971. int mb_type;
  4972. if(intra_slice){
  4973. MpegEncContext * const s = &h->s;
  4974. const int mba_xy = h->left_mb_xy[0];
  4975. const int mbb_xy = h->top_mb_xy;
  4976. int ctx=0;
  4977. if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
  4978. ctx++;
  4979. if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
  4980. ctx++;
  4981. if( get_cabac_noinline( &h->cabac, &state[ctx] ) == 0 )
  4982. return 0; /* I4x4 */
  4983. state += 2;
  4984. }else{
  4985. if( get_cabac_noinline( &h->cabac, &state[0] ) == 0 )
  4986. return 0; /* I4x4 */
  4987. }
  4988. if( get_cabac_terminate( &h->cabac ) )
  4989. return 25; /* PCM */
  4990. mb_type = 1; /* I16x16 */
  4991. mb_type += 12 * get_cabac_noinline( &h->cabac, &state[1] ); /* cbp_luma != 0 */
  4992. if( get_cabac_noinline( &h->cabac, &state[2] ) ) /* cbp_chroma */
  4993. mb_type += 4 + 4 * get_cabac_noinline( &h->cabac, &state[2+intra_slice] );
  4994. mb_type += 2 * get_cabac_noinline( &h->cabac, &state[3+intra_slice] );
  4995. mb_type += 1 * get_cabac_noinline( &h->cabac, &state[3+2*intra_slice] );
  4996. return mb_type;
  4997. }
  4998. static int decode_cabac_mb_type( H264Context *h ) {
  4999. MpegEncContext * const s = &h->s;
  5000. if( h->slice_type == I_TYPE ) {
  5001. return decode_cabac_intra_mb_type(h, 3, 1);
  5002. } else if( h->slice_type == P_TYPE ) {
  5003. if( get_cabac_noinline( &h->cabac, &h->cabac_state[14] ) == 0 ) {
  5004. /* P-type */
  5005. if( get_cabac_noinline( &h->cabac, &h->cabac_state[15] ) == 0 ) {
  5006. /* P_L0_D16x16, P_8x8 */
  5007. return 3 * get_cabac_noinline( &h->cabac, &h->cabac_state[16] );
  5008. } else {
  5009. /* P_L0_D8x16, P_L0_D16x8 */
  5010. return 2 - get_cabac_noinline( &h->cabac, &h->cabac_state[17] );
  5011. }
  5012. } else {
  5013. return decode_cabac_intra_mb_type(h, 17, 0) + 5;
  5014. }
  5015. } else if( h->slice_type == B_TYPE ) {
  5016. const int mba_xy = h->left_mb_xy[0];
  5017. const int mbb_xy = h->top_mb_xy;
  5018. int ctx = 0;
  5019. int bits;
  5020. if( h->slice_table[mba_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
  5021. ctx++;
  5022. if( h->slice_table[mbb_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
  5023. ctx++;
  5024. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+ctx] ) )
  5025. return 0; /* B_Direct_16x16 */
  5026. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+3] ) ) {
  5027. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
  5028. }
  5029. bits = get_cabac_noinline( &h->cabac, &h->cabac_state[27+4] ) << 3;
  5030. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 2;
  5031. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 1;
  5032. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  5033. if( bits < 8 )
  5034. return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
  5035. else if( bits == 13 ) {
  5036. return decode_cabac_intra_mb_type(h, 32, 0) + 23;
  5037. } else if( bits == 14 )
  5038. return 11; /* B_L1_L0_8x16 */
  5039. else if( bits == 15 )
  5040. return 22; /* B_8x8 */
  5041. bits= ( bits<<1 ) | get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  5042. return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
  5043. } else {
  5044. /* TODO SI/SP frames? */
  5045. return -1;
  5046. }
  5047. }
  5048. static int decode_cabac_mb_skip( H264Context *h, int mb_x, int mb_y ) {
  5049. MpegEncContext * const s = &h->s;
  5050. int mba_xy, mbb_xy;
  5051. int ctx = 0;
  5052. if(FRAME_MBAFF){ //FIXME merge with the stuff in fill_caches?
  5053. int mb_xy = mb_x + (mb_y&~1)*s->mb_stride;
  5054. mba_xy = mb_xy - 1;
  5055. if( (mb_y&1)
  5056. && h->slice_table[mba_xy] == h->slice_num
  5057. && MB_FIELD == !!IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) )
  5058. mba_xy += s->mb_stride;
  5059. if( MB_FIELD ){
  5060. mbb_xy = mb_xy - s->mb_stride;
  5061. if( !(mb_y&1)
  5062. && h->slice_table[mbb_xy] == h->slice_num
  5063. && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) )
  5064. mbb_xy -= s->mb_stride;
  5065. }else
  5066. mbb_xy = mb_x + (mb_y-1)*s->mb_stride;
  5067. }else{
  5068. int mb_xy = mb_x + mb_y*s->mb_stride;
  5069. mba_xy = mb_xy - 1;
  5070. mbb_xy = mb_xy - s->mb_stride;
  5071. }
  5072. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
  5073. ctx++;
  5074. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
  5075. ctx++;
  5076. if( h->slice_type == B_TYPE )
  5077. ctx += 13;
  5078. return get_cabac_noinline( &h->cabac, &h->cabac_state[11+ctx] );
  5079. }
  5080. static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
  5081. int mode = 0;
  5082. if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
  5083. return pred_mode;
  5084. mode += 1 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5085. mode += 2 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5086. mode += 4 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5087. if( mode >= pred_mode )
  5088. return mode + 1;
  5089. else
  5090. return mode;
  5091. }
  5092. static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
  5093. const int mba_xy = h->left_mb_xy[0];
  5094. const int mbb_xy = h->top_mb_xy;
  5095. int ctx = 0;
  5096. /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
  5097. if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
  5098. ctx++;
  5099. if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
  5100. ctx++;
  5101. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
  5102. return 0;
  5103. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  5104. return 1;
  5105. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  5106. return 2;
  5107. else
  5108. return 3;
  5109. }
  5110. static const uint8_t block_idx_x[16] = {
  5111. 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
  5112. };
  5113. static const uint8_t block_idx_y[16] = {
  5114. 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
  5115. };
  5116. static const uint8_t block_idx_xy[4][4] = {
  5117. { 0, 2, 8, 10},
  5118. { 1, 3, 9, 11},
  5119. { 4, 6, 12, 14},
  5120. { 5, 7, 13, 15}
  5121. };
  5122. static int decode_cabac_mb_cbp_luma( H264Context *h) {
  5123. int cbp = 0;
  5124. int cbp_b = -1;
  5125. int i8x8;
  5126. if( h->slice_table[h->top_mb_xy] == h->slice_num ) {
  5127. cbp_b = h->top_cbp;
  5128. tprintf("cbp_b = top_cbp = %x\n", cbp_b);
  5129. }
  5130. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5131. int cbp_a = -1;
  5132. int x, y;
  5133. int ctx = 0;
  5134. x = block_idx_x[4*i8x8];
  5135. y = block_idx_y[4*i8x8];
  5136. if( x > 0 )
  5137. cbp_a = cbp;
  5138. else if( h->slice_table[h->left_mb_xy[0]] == h->slice_num ) {
  5139. cbp_a = h->left_cbp;
  5140. tprintf("cbp_a = left_cbp = %x\n", cbp_a);
  5141. }
  5142. if( y > 0 )
  5143. cbp_b = cbp;
  5144. /* No need to test for skip as we put 0 for skip block */
  5145. /* No need to test for IPCM as we put 1 for IPCM block */
  5146. if( cbp_a >= 0 ) {
  5147. int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
  5148. if( ((cbp_a >> i8x8a)&0x01) == 0 )
  5149. ctx++;
  5150. }
  5151. if( cbp_b >= 0 ) {
  5152. int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
  5153. if( ((cbp_b >> i8x8b)&0x01) == 0 )
  5154. ctx += 2;
  5155. }
  5156. if( get_cabac( &h->cabac, &h->cabac_state[73 + ctx] ) ) {
  5157. cbp |= 1 << i8x8;
  5158. }
  5159. }
  5160. return cbp;
  5161. }
  5162. static int decode_cabac_mb_cbp_chroma( H264Context *h) {
  5163. int ctx;
  5164. int cbp_a, cbp_b;
  5165. cbp_a = (h->left_cbp>>4)&0x03;
  5166. cbp_b = (h-> top_cbp>>4)&0x03;
  5167. ctx = 0;
  5168. if( cbp_a > 0 ) ctx++;
  5169. if( cbp_b > 0 ) ctx += 2;
  5170. if( get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
  5171. return 0;
  5172. ctx = 4;
  5173. if( cbp_a == 2 ) ctx++;
  5174. if( cbp_b == 2 ) ctx += 2;
  5175. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] );
  5176. }
  5177. static int decode_cabac_mb_dqp( H264Context *h) {
  5178. MpegEncContext * const s = &h->s;
  5179. int mbn_xy;
  5180. int ctx = 0;
  5181. int val = 0;
  5182. if( s->mb_x > 0 )
  5183. mbn_xy = s->mb_x + s->mb_y*s->mb_stride - 1;
  5184. else
  5185. mbn_xy = s->mb_width - 1 + (s->mb_y-1)*s->mb_stride;
  5186. if( h->last_qscale_diff != 0 )
  5187. ctx++;
  5188. while( get_cabac_noinline( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
  5189. if( ctx < 2 )
  5190. ctx = 2;
  5191. else
  5192. ctx = 3;
  5193. val++;
  5194. if(val > 102) //prevent infinite loop
  5195. return INT_MIN;
  5196. }
  5197. if( val&0x01 )
  5198. return (val + 1)/2;
  5199. else
  5200. return -(val + 1)/2;
  5201. }
  5202. static int decode_cabac_p_mb_sub_type( H264Context *h ) {
  5203. if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
  5204. return 0; /* 8x8 */
  5205. if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
  5206. return 1; /* 8x4 */
  5207. if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
  5208. return 2; /* 4x8 */
  5209. return 3; /* 4x4 */
  5210. }
  5211. static int decode_cabac_b_mb_sub_type( H264Context *h ) {
  5212. int type;
  5213. if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
  5214. return 0; /* B_Direct_8x8 */
  5215. if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
  5216. return 1 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */
  5217. type = 3;
  5218. if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
  5219. if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
  5220. return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
  5221. type += 4;
  5222. }
  5223. type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
  5224. type += get_cabac( &h->cabac, &h->cabac_state[39] );
  5225. return type;
  5226. }
  5227. static inline int decode_cabac_mb_transform_size( H264Context *h ) {
  5228. return get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
  5229. }
  5230. static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
  5231. int refa = h->ref_cache[list][scan8[n] - 1];
  5232. int refb = h->ref_cache[list][scan8[n] - 8];
  5233. int ref = 0;
  5234. int ctx = 0;
  5235. if( h->slice_type == B_TYPE) {
  5236. if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
  5237. ctx++;
  5238. if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
  5239. ctx += 2;
  5240. } else {
  5241. if( refa > 0 )
  5242. ctx++;
  5243. if( refb > 0 )
  5244. ctx += 2;
  5245. }
  5246. while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
  5247. ref++;
  5248. if( ctx < 4 )
  5249. ctx = 4;
  5250. else
  5251. ctx = 5;
  5252. }
  5253. return ref;
  5254. }
  5255. static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
  5256. int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
  5257. abs( h->mvd_cache[list][scan8[n] - 8][l] );
  5258. int ctxbase = (l == 0) ? 40 : 47;
  5259. int ctx, mvd;
  5260. if( amvd < 3 )
  5261. ctx = 0;
  5262. else if( amvd > 32 )
  5263. ctx = 2;
  5264. else
  5265. ctx = 1;
  5266. if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
  5267. return 0;
  5268. mvd= 1;
  5269. ctx= 3;
  5270. while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
  5271. mvd++;
  5272. if( ctx < 6 )
  5273. ctx++;
  5274. }
  5275. if( mvd >= 9 ) {
  5276. int k = 3;
  5277. while( get_cabac_bypass( &h->cabac ) ) {
  5278. mvd += 1 << k;
  5279. k++;
  5280. }
  5281. while( k-- ) {
  5282. if( get_cabac_bypass( &h->cabac ) )
  5283. mvd += 1 << k;
  5284. }
  5285. }
  5286. return get_cabac_bypass_sign( &h->cabac, -mvd );
  5287. }
  5288. static int inline get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) {
  5289. int nza, nzb;
  5290. int ctx = 0;
  5291. if( cat == 0 ) {
  5292. nza = h->left_cbp&0x100;
  5293. nzb = h-> top_cbp&0x100;
  5294. } else if( cat == 1 || cat == 2 ) {
  5295. nza = h->non_zero_count_cache[scan8[idx] - 1];
  5296. nzb = h->non_zero_count_cache[scan8[idx] - 8];
  5297. } else if( cat == 3 ) {
  5298. nza = (h->left_cbp>>(6+idx))&0x01;
  5299. nzb = (h-> top_cbp>>(6+idx))&0x01;
  5300. } else {
  5301. assert(cat == 4);
  5302. nza = h->non_zero_count_cache[scan8[16+idx] - 1];
  5303. nzb = h->non_zero_count_cache[scan8[16+idx] - 8];
  5304. }
  5305. if( nza > 0 )
  5306. ctx++;
  5307. if( nzb > 0 )
  5308. ctx += 2;
  5309. return ctx + 4 * cat;
  5310. }
  5311. static const __attribute((used)) uint8_t last_coeff_flag_offset_8x8[63] = {
  5312. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5313. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  5314. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  5315. 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
  5316. };
  5317. static int decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff) {
  5318. const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
  5319. static const int significant_coeff_flag_offset[2][6] = {
  5320. { 105+0, 105+15, 105+29, 105+44, 105+47, 402 },
  5321. { 277+0, 277+15, 277+29, 277+44, 277+47, 436 }
  5322. };
  5323. static const int last_coeff_flag_offset[2][6] = {
  5324. { 166+0, 166+15, 166+29, 166+44, 166+47, 417 },
  5325. { 338+0, 338+15, 338+29, 338+44, 338+47, 451 }
  5326. };
  5327. static const int coeff_abs_level_m1_offset[6] = {
  5328. 227+0, 227+10, 227+20, 227+30, 227+39, 426
  5329. };
  5330. static const uint8_t significant_coeff_flag_offset_8x8[2][63] = {
  5331. { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
  5332. 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
  5333. 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
  5334. 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12 },
  5335. { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
  5336. 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
  5337. 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
  5338. 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }
  5339. };
  5340. int index[64];
  5341. int last;
  5342. int coeff_count = 0;
  5343. int abslevel1 = 1;
  5344. int abslevelgt1 = 0;
  5345. uint8_t *significant_coeff_ctx_base;
  5346. uint8_t *last_coeff_ctx_base;
  5347. uint8_t *abs_level_m1_ctx_base;
  5348. #ifndef ARCH_X86
  5349. #define CABAC_ON_STACK
  5350. #endif
  5351. #ifdef CABAC_ON_STACK
  5352. #define CC &cc
  5353. CABACContext cc;
  5354. cc.range = h->cabac.range;
  5355. cc.low = h->cabac.low;
  5356. cc.bytestream= h->cabac.bytestream;
  5357. #else
  5358. #define CC &h->cabac
  5359. #endif
  5360. /* cat: 0-> DC 16x16 n = 0
  5361. * 1-> AC 16x16 n = luma4x4idx
  5362. * 2-> Luma4x4 n = luma4x4idx
  5363. * 3-> DC Chroma n = iCbCr
  5364. * 4-> AC Chroma n = 4 * iCbCr + chroma4x4idx
  5365. * 5-> Luma8x8 n = 4 * luma8x8idx
  5366. */
  5367. /* read coded block flag */
  5368. if( cat != 5 ) {
  5369. if( get_cabac( CC, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n ) ] ) == 0 ) {
  5370. if( cat == 1 || cat == 2 )
  5371. h->non_zero_count_cache[scan8[n]] = 0;
  5372. else if( cat == 4 )
  5373. h->non_zero_count_cache[scan8[16+n]] = 0;
  5374. #ifdef CABAC_ON_STACK
  5375. h->cabac.range = cc.range ;
  5376. h->cabac.low = cc.low ;
  5377. h->cabac.bytestream= cc.bytestream;
  5378. #endif
  5379. return 0;
  5380. }
  5381. }
  5382. significant_coeff_ctx_base = h->cabac_state
  5383. + significant_coeff_flag_offset[MB_FIELD][cat];
  5384. last_coeff_ctx_base = h->cabac_state
  5385. + last_coeff_flag_offset[MB_FIELD][cat];
  5386. abs_level_m1_ctx_base = h->cabac_state
  5387. + coeff_abs_level_m1_offset[cat];
  5388. if( cat == 5 ) {
  5389. #define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \
  5390. for(last= 0; last < coefs; last++) { \
  5391. uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \
  5392. if( get_cabac( CC, sig_ctx )) { \
  5393. uint8_t *last_ctx = last_coeff_ctx_base + last_off; \
  5394. index[coeff_count++] = last; \
  5395. if( get_cabac( CC, last_ctx ) ) { \
  5396. last= max_coeff; \
  5397. break; \
  5398. } \
  5399. } \
  5400. }\
  5401. if( last == max_coeff -1 ) {\
  5402. index[coeff_count++] = last;\
  5403. }
  5404. const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
  5405. #if defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__))
  5406. coeff_count= decode_significance_8x8_x86(CC, significant_coeff_ctx_base, index, sig_off);
  5407. } else {
  5408. coeff_count= decode_significance_x86(CC, max_coeff, significant_coeff_ctx_base, index);
  5409. #else
  5410. DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );
  5411. } else {
  5412. DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
  5413. #endif
  5414. }
  5415. assert(coeff_count > 0);
  5416. if( cat == 0 )
  5417. h->cbp_table[mb_xy] |= 0x100;
  5418. else if( cat == 1 || cat == 2 )
  5419. h->non_zero_count_cache[scan8[n]] = coeff_count;
  5420. else if( cat == 3 )
  5421. h->cbp_table[mb_xy] |= 0x40 << n;
  5422. else if( cat == 4 )
  5423. h->non_zero_count_cache[scan8[16+n]] = coeff_count;
  5424. else {
  5425. assert( cat == 5 );
  5426. fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);
  5427. }
  5428. for( coeff_count--; coeff_count >= 0; coeff_count-- ) {
  5429. uint8_t *ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 )) + abs_level_m1_ctx_base;
  5430. int j= scantable[index[coeff_count]];
  5431. if( get_cabac( CC, ctx ) == 0 ) {
  5432. if( !qmul ) {
  5433. block[j] = get_cabac_bypass_sign( CC, -1);
  5434. }else{
  5435. block[j] = (get_cabac_bypass_sign( CC, -qmul[j]) + 32) >> 6;;
  5436. }
  5437. abslevel1++;
  5438. } else {
  5439. int coeff_abs = 2;
  5440. ctx = 5 + FFMIN( 4, abslevelgt1 ) + abs_level_m1_ctx_base;
  5441. while( coeff_abs < 15 && get_cabac( CC, ctx ) ) {
  5442. coeff_abs++;
  5443. }
  5444. if( coeff_abs >= 15 ) {
  5445. int j = 0;
  5446. while( get_cabac_bypass( CC ) ) {
  5447. j++;
  5448. }
  5449. coeff_abs=1;
  5450. while( j-- ) {
  5451. coeff_abs += coeff_abs + get_cabac_bypass( CC );
  5452. }
  5453. coeff_abs+= 14;
  5454. }
  5455. if( !qmul ) {
  5456. if( get_cabac_bypass( CC ) ) block[j] = -coeff_abs;
  5457. else block[j] = coeff_abs;
  5458. }else{
  5459. if( get_cabac_bypass( CC ) ) block[j] = (-coeff_abs * qmul[j] + 32) >> 6;
  5460. else block[j] = ( coeff_abs * qmul[j] + 32) >> 6;
  5461. }
  5462. abslevelgt1++;
  5463. }
  5464. }
  5465. #ifdef CABAC_ON_STACK
  5466. h->cabac.range = cc.range ;
  5467. h->cabac.low = cc.low ;
  5468. h->cabac.bytestream= cc.bytestream;
  5469. #endif
  5470. return 0;
  5471. }
  5472. static void inline compute_mb_neighbors(H264Context *h)
  5473. {
  5474. MpegEncContext * const s = &h->s;
  5475. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  5476. h->top_mb_xy = mb_xy - s->mb_stride;
  5477. h->left_mb_xy[0] = mb_xy - 1;
  5478. if(FRAME_MBAFF){
  5479. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  5480. const int top_pair_xy = pair_xy - s->mb_stride;
  5481. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  5482. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  5483. const int curr_mb_frame_flag = !MB_FIELD;
  5484. const int bottom = (s->mb_y & 1);
  5485. if (bottom
  5486. ? !curr_mb_frame_flag // bottom macroblock
  5487. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  5488. ) {
  5489. h->top_mb_xy -= s->mb_stride;
  5490. }
  5491. if (left_mb_frame_flag != curr_mb_frame_flag) {
  5492. h->left_mb_xy[0] = pair_xy - 1;
  5493. }
  5494. }
  5495. return;
  5496. }
  5497. /**
  5498. * decodes a macroblock
  5499. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  5500. */
  5501. static int decode_mb_cabac(H264Context *h) {
  5502. MpegEncContext * const s = &h->s;
  5503. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  5504. int mb_type, partition_count, cbp = 0;
  5505. int dct8x8_allowed= h->pps.transform_8x8_mode;
  5506. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?)
  5507. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  5508. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) {
  5509. int skip;
  5510. /* a skipped mb needs the aff flag from the following mb */
  5511. if( FRAME_MBAFF && s->mb_x==0 && (s->mb_y&1)==0 )
  5512. predict_field_decoding_flag(h);
  5513. if( FRAME_MBAFF && (s->mb_y&1)==1 && h->prev_mb_skipped )
  5514. skip = h->next_mb_skipped;
  5515. else
  5516. skip = decode_cabac_mb_skip( h, s->mb_x, s->mb_y );
  5517. /* read skip flags */
  5518. if( skip ) {
  5519. if( FRAME_MBAFF && (s->mb_y&1)==0 ){
  5520. s->current_picture.mb_type[mb_xy] = MB_TYPE_SKIP;
  5521. h->next_mb_skipped = decode_cabac_mb_skip( h, s->mb_x, s->mb_y+1 );
  5522. if(h->next_mb_skipped)
  5523. predict_field_decoding_flag(h);
  5524. else
  5525. h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5526. }
  5527. decode_mb_skip(h);
  5528. h->cbp_table[mb_xy] = 0;
  5529. h->chroma_pred_mode_table[mb_xy] = 0;
  5530. h->last_qscale_diff = 0;
  5531. return 0;
  5532. }
  5533. }
  5534. if(FRAME_MBAFF){
  5535. if( (s->mb_y&1) == 0 )
  5536. h->mb_mbaff =
  5537. h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5538. }else
  5539. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  5540. h->prev_mb_skipped = 0;
  5541. compute_mb_neighbors(h);
  5542. if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
  5543. av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
  5544. return -1;
  5545. }
  5546. if( h->slice_type == B_TYPE ) {
  5547. if( mb_type < 23 ){
  5548. partition_count= b_mb_type_info[mb_type].partition_count;
  5549. mb_type= b_mb_type_info[mb_type].type;
  5550. }else{
  5551. mb_type -= 23;
  5552. goto decode_intra_mb;
  5553. }
  5554. } else if( h->slice_type == P_TYPE ) {
  5555. if( mb_type < 5) {
  5556. partition_count= p_mb_type_info[mb_type].partition_count;
  5557. mb_type= p_mb_type_info[mb_type].type;
  5558. } else {
  5559. mb_type -= 5;
  5560. goto decode_intra_mb;
  5561. }
  5562. } else {
  5563. assert(h->slice_type == I_TYPE);
  5564. decode_intra_mb:
  5565. partition_count = 0;
  5566. cbp= i_mb_type_info[mb_type].cbp;
  5567. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  5568. mb_type= i_mb_type_info[mb_type].type;
  5569. }
  5570. if(MB_FIELD)
  5571. mb_type |= MB_TYPE_INTERLACED;
  5572. h->slice_table[ mb_xy ]= h->slice_num;
  5573. if(IS_INTRA_PCM(mb_type)) {
  5574. const uint8_t *ptr;
  5575. unsigned int x, y;
  5576. // We assume these blocks are very rare so we dont optimize it.
  5577. // FIXME The two following lines get the bitstream position in the cabac
  5578. // decode, I think it should be done by a function in cabac.h (or cabac.c).
  5579. ptr= h->cabac.bytestream;
  5580. if (h->cabac.low&0x1) ptr-=CABAC_BITS/8;
  5581. // The pixels are stored in the same order as levels in h->mb array.
  5582. for(y=0; y<16; y++){
  5583. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  5584. for(x=0; x<16; x++){
  5585. tprintf("LUMA ICPM LEVEL (%3d)\n", *ptr);
  5586. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= *ptr++;
  5587. }
  5588. }
  5589. for(y=0; y<8; y++){
  5590. const int index= 256 + 4*(y&3) + 32*(y>>2);
  5591. for(x=0; x<8; x++){
  5592. tprintf("CHROMA U ICPM LEVEL (%3d)\n", *ptr);
  5593. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5594. }
  5595. }
  5596. for(y=0; y<8; y++){
  5597. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  5598. for(x=0; x<8; x++){
  5599. tprintf("CHROMA V ICPM LEVEL (%3d)\n", *ptr);
  5600. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5601. }
  5602. }
  5603. ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);
  5604. // All blocks are present
  5605. h->cbp_table[mb_xy] = 0x1ef;
  5606. h->chroma_pred_mode_table[mb_xy] = 0;
  5607. // In deblocking, the quantizer is 0
  5608. s->current_picture.qscale_table[mb_xy]= 0;
  5609. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  5610. // All coeffs are present
  5611. memset(h->non_zero_count[mb_xy], 16, 16);
  5612. s->current_picture.mb_type[mb_xy]= mb_type;
  5613. return 0;
  5614. }
  5615. if(MB_MBAFF){
  5616. h->ref_count[0] <<= 1;
  5617. h->ref_count[1] <<= 1;
  5618. }
  5619. fill_caches(h, mb_type, 0);
  5620. if( IS_INTRA( mb_type ) ) {
  5621. int i;
  5622. if( IS_INTRA4x4( mb_type ) ) {
  5623. if( dct8x8_allowed && decode_cabac_mb_transform_size( h ) ) {
  5624. mb_type |= MB_TYPE_8x8DCT;
  5625. for( i = 0; i < 16; i+=4 ) {
  5626. int pred = pred_intra_mode( h, i );
  5627. int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5628. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  5629. }
  5630. } else {
  5631. for( i = 0; i < 16; i++ ) {
  5632. int pred = pred_intra_mode( h, i );
  5633. h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5634. //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
  5635. }
  5636. }
  5637. write_back_intra_pred_mode(h);
  5638. if( check_intra4x4_pred_mode(h) < 0 ) return -1;
  5639. } else {
  5640. h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );
  5641. if( h->intra16x16_pred_mode < 0 ) return -1;
  5642. }
  5643. h->chroma_pred_mode_table[mb_xy] =
  5644. h->chroma_pred_mode = decode_cabac_mb_chroma_pre_mode( h );
  5645. h->chroma_pred_mode= check_intra_pred_mode( h, h->chroma_pred_mode );
  5646. if( h->chroma_pred_mode < 0 ) return -1;
  5647. } else if( partition_count == 4 ) {
  5648. int i, j, sub_partition_count[4], list, ref[2][4];
  5649. if( h->slice_type == B_TYPE ) {
  5650. for( i = 0; i < 4; i++ ) {
  5651. h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
  5652. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5653. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5654. }
  5655. if( IS_DIRECT(h->sub_mb_type[0] | h->sub_mb_type[1] |
  5656. h->sub_mb_type[2] | h->sub_mb_type[3]) ) {
  5657. pred_direct_motion(h, &mb_type);
  5658. if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
  5659. for( i = 0; i < 4; i++ )
  5660. if( IS_DIRECT(h->sub_mb_type[i]) )
  5661. fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );
  5662. }
  5663. }
  5664. } else {
  5665. for( i = 0; i < 4; i++ ) {
  5666. h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
  5667. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5668. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5669. }
  5670. }
  5671. for( list = 0; list < 2; list++ ) {
  5672. if( h->ref_count[list] > 0 ) {
  5673. for( i = 0; i < 4; i++ ) {
  5674. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  5675. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  5676. if( h->ref_count[list] > 1 )
  5677. ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );
  5678. else
  5679. ref[list][i] = 0;
  5680. } else {
  5681. ref[list][i] = -1;
  5682. }
  5683. h->ref_cache[list][ scan8[4*i]+1 ]=
  5684. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  5685. }
  5686. }
  5687. }
  5688. if(dct8x8_allowed)
  5689. dct8x8_allowed = get_dct8x8_allowed(h);
  5690. for(list=0; list<2; list++){
  5691. for(i=0; i<4; i++){
  5692. if(IS_DIRECT(h->sub_mb_type[i])){
  5693. fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);
  5694. continue;
  5695. }
  5696. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];
  5697. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  5698. const int sub_mb_type= h->sub_mb_type[i];
  5699. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  5700. for(j=0; j<sub_partition_count[i]; j++){
  5701. int mpx, mpy;
  5702. int mx, my;
  5703. const int index= 4*i + block_width*j;
  5704. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  5705. int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];
  5706. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);
  5707. mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );
  5708. my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );
  5709. tprintf("final mv:%d %d\n", mx, my);
  5710. if(IS_SUB_8X8(sub_mb_type)){
  5711. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  5712. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  5713. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  5714. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  5715. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]=
  5716. mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;
  5717. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]=
  5718. mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;
  5719. }else if(IS_SUB_8X4(sub_mb_type)){
  5720. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  5721. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  5722. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]= mx- mpx;
  5723. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]= my - mpy;
  5724. }else if(IS_SUB_4X8(sub_mb_type)){
  5725. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  5726. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  5727. mvd_cache[ 0 ][0]= mvd_cache[ 8 ][0]= mx - mpx;
  5728. mvd_cache[ 0 ][1]= mvd_cache[ 8 ][1]= my - mpy;
  5729. }else{
  5730. assert(IS_SUB_4X4(sub_mb_type));
  5731. mv_cache[ 0 ][0]= mx;
  5732. mv_cache[ 0 ][1]= my;
  5733. mvd_cache[ 0 ][0]= mx - mpx;
  5734. mvd_cache[ 0 ][1]= my - mpy;
  5735. }
  5736. }
  5737. }else{
  5738. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  5739. uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];
  5740. p[0] = p[1] = p[8] = p[9] = 0;
  5741. pd[0]= pd[1]= pd[8]= pd[9]= 0;
  5742. }
  5743. }
  5744. }
  5745. } else if( IS_DIRECT(mb_type) ) {
  5746. pred_direct_motion(h, &mb_type);
  5747. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  5748. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  5749. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  5750. } else {
  5751. int list, mx, my, i, mpx, mpy;
  5752. if(IS_16X16(mb_type)){
  5753. for(list=0; list<2; list++){
  5754. if(IS_DIR(mb_type, 0, list)){
  5755. if(h->ref_count[list] > 0 ){
  5756. const int ref = h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 0 ) : 0;
  5757. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
  5758. }
  5759. }else
  5760. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  5761. }
  5762. for(list=0; list<2; list++){
  5763. if(IS_DIR(mb_type, 0, list)){
  5764. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);
  5765. mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );
  5766. my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );
  5767. tprintf("final mv:%d %d\n", mx, my);
  5768. fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5769. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  5770. }else
  5771. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  5772. }
  5773. }
  5774. else if(IS_16X8(mb_type)){
  5775. for(list=0; list<2; list++){
  5776. if(h->ref_count[list]>0){
  5777. for(i=0; i<2; i++){
  5778. if(IS_DIR(mb_type, i, list)){
  5779. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 8*i ) : 0;
  5780. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
  5781. }else
  5782. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  5783. }
  5784. }
  5785. }
  5786. for(list=0; list<2; list++){
  5787. for(i=0; i<2; i++){
  5788. if(IS_DIR(mb_type, i, list)){
  5789. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);
  5790. mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );
  5791. my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );
  5792. tprintf("final mv:%d %d\n", mx, my);
  5793. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);
  5794. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  5795. }else{
  5796. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5797. fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5798. }
  5799. }
  5800. }
  5801. }else{
  5802. assert(IS_8X16(mb_type));
  5803. for(list=0; list<2; list++){
  5804. if(h->ref_count[list]>0){
  5805. for(i=0; i<2; i++){
  5806. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  5807. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 4*i ) : 0;
  5808. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
  5809. }else
  5810. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  5811. }
  5812. }
  5813. }
  5814. for(list=0; list<2; list++){
  5815. for(i=0; i<2; i++){
  5816. if(IS_DIR(mb_type, i, list)){
  5817. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);
  5818. mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );
  5819. my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );
  5820. tprintf("final mv:%d %d\n", mx, my);
  5821. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5822. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  5823. }else{
  5824. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5825. fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5826. }
  5827. }
  5828. }
  5829. }
  5830. }
  5831. if( IS_INTER( mb_type ) ) {
  5832. h->chroma_pred_mode_table[mb_xy] = 0;
  5833. write_back_motion( h, mb_type );
  5834. }
  5835. if( !IS_INTRA16x16( mb_type ) ) {
  5836. cbp = decode_cabac_mb_cbp_luma( h );
  5837. cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
  5838. }
  5839. h->cbp_table[mb_xy] = h->cbp = cbp;
  5840. if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
  5841. if( decode_cabac_mb_transform_size( h ) )
  5842. mb_type |= MB_TYPE_8x8DCT;
  5843. }
  5844. s->current_picture.mb_type[mb_xy]= mb_type;
  5845. if( cbp || IS_INTRA16x16( mb_type ) ) {
  5846. const uint8_t *scan, *scan8x8, *dc_scan;
  5847. int dqp;
  5848. if(IS_INTERLACED(mb_type)){
  5849. scan8x8= s->qscale ? h->field_scan8x8 : h->field_scan8x8_q0;
  5850. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  5851. dc_scan= luma_dc_field_scan;
  5852. }else{
  5853. scan8x8= s->qscale ? h->zigzag_scan8x8 : h->zigzag_scan8x8_q0;
  5854. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  5855. dc_scan= luma_dc_zigzag_scan;
  5856. }
  5857. h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
  5858. if( dqp == INT_MIN ){
  5859. av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", s->mb_x, s->mb_y);
  5860. return -1;
  5861. }
  5862. s->qscale += dqp;
  5863. if(((unsigned)s->qscale) > 51){
  5864. if(s->qscale<0) s->qscale+= 52;
  5865. else s->qscale-= 52;
  5866. }
  5867. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  5868. if( IS_INTRA16x16( mb_type ) ) {
  5869. int i;
  5870. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
  5871. if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16) < 0)
  5872. return -1;
  5873. if( cbp&15 ) {
  5874. for( i = 0; i < 16; i++ ) {
  5875. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
  5876. if( decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 )
  5877. return -1;
  5878. }
  5879. } else {
  5880. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  5881. }
  5882. } else {
  5883. int i8x8, i4x4;
  5884. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5885. if( cbp & (1<<i8x8) ) {
  5886. if( IS_8x8DCT(mb_type) ) {
  5887. if( decode_cabac_residual(h, h->mb + 64*i8x8, 5, 4*i8x8,
  5888. scan8x8, h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 64) < 0 )
  5889. return -1;
  5890. } else
  5891. for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
  5892. const int index = 4*i8x8 + i4x4;
  5893. //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
  5894. //START_TIMER
  5895. 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 )
  5896. return -1;
  5897. //STOP_TIMER("decode_residual")
  5898. }
  5899. } else {
  5900. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  5901. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  5902. }
  5903. }
  5904. }
  5905. if( cbp&0x30 ){
  5906. int c;
  5907. for( c = 0; c < 2; c++ ) {
  5908. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
  5909. if( decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, NULL, 4) < 0)
  5910. return -1;
  5911. }
  5912. }
  5913. if( cbp&0x20 ) {
  5914. int c, i;
  5915. for( c = 0; c < 2; c++ ) {
  5916. for( i = 0; i < 4; i++ ) {
  5917. const int index = 16 + 4 * c + i;
  5918. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
  5919. 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)
  5920. return -1;
  5921. }
  5922. }
  5923. } else {
  5924. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5925. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5926. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5927. }
  5928. } else {
  5929. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5930. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  5931. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5932. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5933. h->last_qscale_diff = 0;
  5934. }
  5935. s->current_picture.qscale_table[mb_xy]= s->qscale;
  5936. write_back_non_zero_count(h);
  5937. if(MB_MBAFF){
  5938. h->ref_count[0] >>= 1;
  5939. h->ref_count[1] >>= 1;
  5940. }
  5941. return 0;
  5942. }
  5943. static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5944. int i, d;
  5945. const int index_a = qp + h->slice_alpha_c0_offset;
  5946. const int alpha = (alpha_table+52)[index_a];
  5947. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5948. if( bS[0] < 4 ) {
  5949. int8_t tc[4];
  5950. for(i=0; i<4; i++)
  5951. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  5952. h->s.dsp.h264_h_loop_filter_luma(pix, stride, alpha, beta, tc);
  5953. } else {
  5954. /* 16px edge length, because bS=4 is triggered by being at
  5955. * the edge of an intra MB, so all 4 bS are the same */
  5956. for( d = 0; d < 16; d++ ) {
  5957. const int p0 = pix[-1];
  5958. const int p1 = pix[-2];
  5959. const int p2 = pix[-3];
  5960. const int q0 = pix[0];
  5961. const int q1 = pix[1];
  5962. const int q2 = pix[2];
  5963. if( FFABS( p0 - q0 ) < alpha &&
  5964. FFABS( p1 - p0 ) < beta &&
  5965. FFABS( q1 - q0 ) < beta ) {
  5966. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5967. if( FFABS( p2 - p0 ) < beta)
  5968. {
  5969. const int p3 = pix[-4];
  5970. /* p0', p1', p2' */
  5971. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5972. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5973. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5974. } else {
  5975. /* p0' */
  5976. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5977. }
  5978. if( FFABS( q2 - q0 ) < beta)
  5979. {
  5980. const int q3 = pix[3];
  5981. /* q0', q1', q2' */
  5982. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5983. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5984. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5985. } else {
  5986. /* q0' */
  5987. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5988. }
  5989. }else{
  5990. /* p0', q0' */
  5991. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5992. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5993. }
  5994. 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]);
  5995. }
  5996. pix += stride;
  5997. }
  5998. }
  5999. }
  6000. static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  6001. int i;
  6002. const int index_a = qp + h->slice_alpha_c0_offset;
  6003. const int alpha = (alpha_table+52)[index_a];
  6004. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  6005. if( bS[0] < 4 ) {
  6006. int8_t tc[4];
  6007. for(i=0; i<4; i++)
  6008. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  6009. h->s.dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
  6010. } else {
  6011. h->s.dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
  6012. }
  6013. }
  6014. static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  6015. int i;
  6016. for( i = 0; i < 16; i++, pix += stride) {
  6017. int index_a;
  6018. int alpha;
  6019. int beta;
  6020. int qp_index;
  6021. int bS_index = (i >> 1);
  6022. if (!MB_FIELD) {
  6023. bS_index &= ~1;
  6024. bS_index |= (i & 1);
  6025. }
  6026. if( bS[bS_index] == 0 ) {
  6027. continue;
  6028. }
  6029. qp_index = MB_FIELD ? (i >> 3) : (i & 1);
  6030. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  6031. alpha = (alpha_table+52)[index_a];
  6032. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  6033. if( bS[bS_index] < 4 ) {
  6034. const int tc0 = (tc0_table+52)[index_a][bS[bS_index] - 1];
  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( FFABS( p0 - q0 ) < alpha &&
  6042. FFABS( p1 - p0 ) < beta &&
  6043. FFABS( q1 - q0 ) < beta ) {
  6044. int tc = tc0;
  6045. int i_delta;
  6046. if( FFABS( p2 - p0 ) < beta ) {
  6047. pix[-2] = p1 + clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  6048. tc++;
  6049. }
  6050. if( FFABS( q2 - q0 ) < beta ) {
  6051. pix[1] = q1 + clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  6052. tc++;
  6053. }
  6054. i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  6055. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  6056. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  6057. 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);
  6058. }
  6059. }else{
  6060. const int p0 = pix[-1];
  6061. const int p1 = pix[-2];
  6062. const int p2 = pix[-3];
  6063. const int q0 = pix[0];
  6064. const int q1 = pix[1];
  6065. const int q2 = pix[2];
  6066. if( FFABS( p0 - q0 ) < alpha &&
  6067. FFABS( p1 - p0 ) < beta &&
  6068. FFABS( q1 - q0 ) < beta ) {
  6069. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  6070. if( FFABS( p2 - p0 ) < beta)
  6071. {
  6072. const int p3 = pix[-4];
  6073. /* p0', p1', p2' */
  6074. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  6075. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  6076. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  6077. } else {
  6078. /* p0' */
  6079. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6080. }
  6081. if( FFABS( q2 - q0 ) < beta)
  6082. {
  6083. const int q3 = pix[3];
  6084. /* q0', q1', q2' */
  6085. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  6086. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  6087. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  6088. } else {
  6089. /* q0' */
  6090. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6091. }
  6092. }else{
  6093. /* p0', q0' */
  6094. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6095. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6096. }
  6097. 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]);
  6098. }
  6099. }
  6100. }
  6101. }
  6102. static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  6103. int i;
  6104. for( i = 0; i < 8; i++, pix += stride) {
  6105. int index_a;
  6106. int alpha;
  6107. int beta;
  6108. int qp_index;
  6109. int bS_index = i;
  6110. if( bS[bS_index] == 0 ) {
  6111. continue;
  6112. }
  6113. qp_index = MB_FIELD ? (i >> 2) : (i & 1);
  6114. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  6115. alpha = (alpha_table+52)[index_a];
  6116. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  6117. if( bS[bS_index] < 4 ) {
  6118. const int tc = (tc0_table+52)[index_a][bS[bS_index] - 1] + 1;
  6119. const int p0 = pix[-1];
  6120. const int p1 = pix[-2];
  6121. const int q0 = pix[0];
  6122. const int q1 = pix[1];
  6123. if( FFABS( p0 - q0 ) < alpha &&
  6124. FFABS( p1 - p0 ) < beta &&
  6125. FFABS( q1 - q0 ) < beta ) {
  6126. const int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  6127. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  6128. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  6129. 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);
  6130. }
  6131. }else{
  6132. const int p0 = pix[-1];
  6133. const int p1 = pix[-2];
  6134. const int q0 = pix[0];
  6135. const int q1 = pix[1];
  6136. if( FFABS( p0 - q0 ) < alpha &&
  6137. FFABS( p1 - p0 ) < beta &&
  6138. FFABS( q1 - q0 ) < beta ) {
  6139. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  6140. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  6141. 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]);
  6142. }
  6143. }
  6144. }
  6145. }
  6146. static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  6147. int i, d;
  6148. const int index_a = qp + h->slice_alpha_c0_offset;
  6149. const int alpha = (alpha_table+52)[index_a];
  6150. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  6151. const int pix_next = stride;
  6152. if( bS[0] < 4 ) {
  6153. int8_t tc[4];
  6154. for(i=0; i<4; i++)
  6155. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  6156. h->s.dsp.h264_v_loop_filter_luma(pix, stride, alpha, beta, tc);
  6157. } else {
  6158. /* 16px edge length, see filter_mb_edgev */
  6159. for( d = 0; d < 16; d++ ) {
  6160. const int p0 = pix[-1*pix_next];
  6161. const int p1 = pix[-2*pix_next];
  6162. const int p2 = pix[-3*pix_next];
  6163. const int q0 = pix[0];
  6164. const int q1 = pix[1*pix_next];
  6165. const int q2 = pix[2*pix_next];
  6166. if( FFABS( p0 - q0 ) < alpha &&
  6167. FFABS( p1 - p0 ) < beta &&
  6168. FFABS( q1 - q0 ) < beta ) {
  6169. const int p3 = pix[-4*pix_next];
  6170. const int q3 = pix[ 3*pix_next];
  6171. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  6172. if( FFABS( p2 - p0 ) < beta) {
  6173. /* p0', p1', p2' */
  6174. pix[-1*pix_next] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  6175. pix[-2*pix_next] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  6176. pix[-3*pix_next] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  6177. } else {
  6178. /* p0' */
  6179. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6180. }
  6181. if( FFABS( q2 - q0 ) < beta) {
  6182. /* q0', q1', q2' */
  6183. pix[0*pix_next] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  6184. pix[1*pix_next] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  6185. pix[2*pix_next] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  6186. } else {
  6187. /* q0' */
  6188. pix[0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6189. }
  6190. }else{
  6191. /* p0', q0' */
  6192. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6193. pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6194. }
  6195. 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]);
  6196. }
  6197. pix++;
  6198. }
  6199. }
  6200. }
  6201. static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  6202. int i;
  6203. const int index_a = qp + h->slice_alpha_c0_offset;
  6204. const int alpha = (alpha_table+52)[index_a];
  6205. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  6206. if( bS[0] < 4 ) {
  6207. int8_t tc[4];
  6208. for(i=0; i<4; i++)
  6209. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  6210. h->s.dsp.h264_v_loop_filter_chroma(pix, stride, alpha, beta, tc);
  6211. } else {
  6212. h->s.dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);
  6213. }
  6214. }
  6215. static void filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {
  6216. MpegEncContext * const s = &h->s;
  6217. int mb_xy, mb_type;
  6218. int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh;
  6219. if(mb_x==0 || mb_y==0 || !s->dsp.h264_loop_filter_strength) {
  6220. filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);
  6221. return;
  6222. }
  6223. assert(!FRAME_MBAFF);
  6224. mb_xy = mb_x + mb_y*s->mb_stride;
  6225. mb_type = s->current_picture.mb_type[mb_xy];
  6226. qp = s->current_picture.qscale_table[mb_xy];
  6227. qp0 = s->current_picture.qscale_table[mb_xy-1];
  6228. qp1 = s->current_picture.qscale_table[h->top_mb_xy];
  6229. qpc = get_chroma_qp( h->pps.chroma_qp_index_offset, qp );
  6230. qpc0 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp0 );
  6231. qpc1 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp1 );
  6232. qp0 = (qp + qp0 + 1) >> 1;
  6233. qp1 = (qp + qp1 + 1) >> 1;
  6234. qpc0 = (qpc + qpc0 + 1) >> 1;
  6235. qpc1 = (qpc + qpc1 + 1) >> 1;
  6236. qp_thresh = 15 - h->slice_alpha_c0_offset;
  6237. if(qp <= qp_thresh && qp0 <= qp_thresh && qp1 <= qp_thresh &&
  6238. qpc <= qp_thresh && qpc0 <= qp_thresh && qpc1 <= qp_thresh)
  6239. return;
  6240. if( IS_INTRA(mb_type) ) {
  6241. int16_t bS4[4] = {4,4,4,4};
  6242. int16_t bS3[4] = {3,3,3,3};
  6243. if( IS_8x8DCT(mb_type) ) {
  6244. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  6245. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  6246. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  6247. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  6248. } else {
  6249. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  6250. filter_mb_edgev( h, &img_y[4*1], linesize, bS3, qp );
  6251. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  6252. filter_mb_edgev( h, &img_y[4*3], linesize, bS3, qp );
  6253. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  6254. filter_mb_edgeh( h, &img_y[4*1*linesize], linesize, bS3, qp );
  6255. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  6256. filter_mb_edgeh( h, &img_y[4*3*linesize], linesize, bS3, qp );
  6257. }
  6258. filter_mb_edgecv( h, &img_cb[2*0], uvlinesize, bS4, qpc0 );
  6259. filter_mb_edgecv( h, &img_cb[2*2], uvlinesize, bS3, qpc );
  6260. filter_mb_edgecv( h, &img_cr[2*0], uvlinesize, bS4, qpc0 );
  6261. filter_mb_edgecv( h, &img_cr[2*2], uvlinesize, bS3, qpc );
  6262. filter_mb_edgech( h, &img_cb[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  6263. filter_mb_edgech( h, &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc );
  6264. filter_mb_edgech( h, &img_cr[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  6265. filter_mb_edgech( h, &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc );
  6266. return;
  6267. } else {
  6268. DECLARE_ALIGNED_8(int16_t, bS[2][4][4]);
  6269. uint64_t (*bSv)[4] = (uint64_t(*)[4])bS;
  6270. int edges;
  6271. if( IS_8x8DCT(mb_type) && (h->cbp&7) == 7 ) {
  6272. edges = 4;
  6273. bSv[0][0] = bSv[0][2] = bSv[1][0] = bSv[1][2] = 0x0002000200020002ULL;
  6274. } else {
  6275. int mask_edge1 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 :
  6276. (mb_type & MB_TYPE_16x8) ? 1 : 0;
  6277. int mask_edge0 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16))
  6278. && (s->current_picture.mb_type[mb_xy-1] & (MB_TYPE_16x16 | MB_TYPE_8x16))
  6279. ? 3 : 0;
  6280. int step = IS_8x8DCT(mb_type) ? 2 : 1;
  6281. edges = (mb_type & MB_TYPE_16x16) && !(h->cbp & 15) ? 1 : 4;
  6282. s->dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,
  6283. (h->slice_type == B_TYPE), edges, step, mask_edge0, mask_edge1 );
  6284. }
  6285. if( IS_INTRA(s->current_picture.mb_type[mb_xy-1]) )
  6286. bSv[0][0] = 0x0004000400040004ULL;
  6287. if( IS_INTRA(s->current_picture.mb_type[h->top_mb_xy]) )
  6288. bSv[1][0] = 0x0004000400040004ULL;
  6289. #define FILTER(hv,dir,edge)\
  6290. if(bSv[dir][edge]) {\
  6291. filter_mb_edge##hv( h, &img_y[4*edge*(dir?linesize:1)], linesize, bS[dir][edge], edge ? qp : qp##dir );\
  6292. if(!(edge&1)) {\
  6293. filter_mb_edgec##hv( h, &img_cb[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  6294. filter_mb_edgec##hv( h, &img_cr[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  6295. }\
  6296. }
  6297. if( edges == 1 ) {
  6298. FILTER(v,0,0);
  6299. FILTER(h,1,0);
  6300. } else if( IS_8x8DCT(mb_type) ) {
  6301. FILTER(v,0,0);
  6302. FILTER(v,0,2);
  6303. FILTER(h,1,0);
  6304. FILTER(h,1,2);
  6305. } else {
  6306. FILTER(v,0,0);
  6307. FILTER(v,0,1);
  6308. FILTER(v,0,2);
  6309. FILTER(v,0,3);
  6310. FILTER(h,1,0);
  6311. FILTER(h,1,1);
  6312. FILTER(h,1,2);
  6313. FILTER(h,1,3);
  6314. }
  6315. #undef FILTER
  6316. }
  6317. }
  6318. 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) {
  6319. MpegEncContext * const s = &h->s;
  6320. const int mb_xy= mb_x + mb_y*s->mb_stride;
  6321. const int mb_type = s->current_picture.mb_type[mb_xy];
  6322. const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4;
  6323. int first_vertical_edge_done = 0;
  6324. int dir;
  6325. /* FIXME: A given frame may occupy more than one position in
  6326. * the reference list. So ref2frm should be populated with
  6327. * frame numbers, not indices. */
  6328. static const int ref2frm[34] = {-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  6329. 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
  6330. //for sufficiently low qp, filtering wouldn't do anything
  6331. //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
  6332. if(!FRAME_MBAFF){
  6333. int qp_thresh = 15 - h->slice_alpha_c0_offset - FFMAX(0, h->pps.chroma_qp_index_offset);
  6334. int qp = s->current_picture.qscale_table[mb_xy];
  6335. if(qp <= qp_thresh
  6336. && (mb_x == 0 || ((qp + s->current_picture.qscale_table[mb_xy-1] + 1)>>1) <= qp_thresh)
  6337. && (mb_y == 0 || ((qp + s->current_picture.qscale_table[h->top_mb_xy] + 1)>>1) <= qp_thresh)){
  6338. return;
  6339. }
  6340. }
  6341. if (FRAME_MBAFF
  6342. // left mb is in picture
  6343. && h->slice_table[mb_xy-1] != 255
  6344. // and current and left pair do not have the same interlaced type
  6345. && (IS_INTERLACED(mb_type) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
  6346. // and left mb is in the same slice if deblocking_filter == 2
  6347. && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
  6348. /* First vertical edge is different in MBAFF frames
  6349. * There are 8 different bS to compute and 2 different Qp
  6350. */
  6351. const int pair_xy = mb_x + (mb_y&~1)*s->mb_stride;
  6352. const int left_mb_xy[2] = { pair_xy-1, pair_xy-1+s->mb_stride };
  6353. int16_t bS[8];
  6354. int qp[2];
  6355. int chroma_qp[2];
  6356. int mb_qp, mbn0_qp, mbn1_qp;
  6357. int i;
  6358. first_vertical_edge_done = 1;
  6359. if( IS_INTRA(mb_type) )
  6360. bS[0] = bS[1] = bS[2] = bS[3] = bS[4] = bS[5] = bS[6] = bS[7] = 4;
  6361. else {
  6362. for( i = 0; i < 8; i++ ) {
  6363. int mbn_xy = MB_FIELD ? left_mb_xy[i>>2] : left_mb_xy[i&1];
  6364. if( IS_INTRA( s->current_picture.mb_type[mbn_xy] ) )
  6365. bS[i] = 4;
  6366. else if( h->non_zero_count_cache[12+8*(i>>1)] != 0 ||
  6367. /* FIXME: with 8x8dct + cavlc, should check cbp instead of nnz */
  6368. h->non_zero_count[mbn_xy][MB_FIELD ? i&3 : (i>>2)+(mb_y&1)*2] )
  6369. bS[i] = 2;
  6370. else
  6371. bS[i] = 1;
  6372. }
  6373. }
  6374. mb_qp = s->current_picture.qscale_table[mb_xy];
  6375. mbn0_qp = s->current_picture.qscale_table[left_mb_xy[0]];
  6376. mbn1_qp = s->current_picture.qscale_table[left_mb_xy[1]];
  6377. qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1;
  6378. chroma_qp[0] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6379. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn0_qp ) + 1 ) >> 1;
  6380. qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1;
  6381. chroma_qp[1] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6382. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn1_qp ) + 1 ) >> 1;
  6383. /* Filter edge */
  6384. 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);
  6385. { int i; for (i = 0; i < 8; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6386. filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp );
  6387. filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, chroma_qp );
  6388. filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, chroma_qp );
  6389. }
  6390. /* dir : 0 -> vertical edge, 1 -> horizontal edge */
  6391. for( dir = 0; dir < 2; dir++ )
  6392. {
  6393. int edge;
  6394. const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
  6395. const int mbm_type = s->current_picture.mb_type[mbm_xy];
  6396. int start = h->slice_table[mbm_xy] == 255 ? 1 : 0;
  6397. const int edges = (mb_type & (MB_TYPE_16x16|MB_TYPE_SKIP))
  6398. == (MB_TYPE_16x16|MB_TYPE_SKIP) ? 1 : 4;
  6399. // how often to recheck mv-based bS when iterating between edges
  6400. const int mask_edge = (mb_type & (MB_TYPE_16x16 | (MB_TYPE_16x8 << dir))) ? 3 :
  6401. (mb_type & (MB_TYPE_8x16 >> dir)) ? 1 : 0;
  6402. // how often to recheck mv-based bS when iterating along each edge
  6403. const int mask_par0 = mb_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir));
  6404. if (first_vertical_edge_done) {
  6405. start = 1;
  6406. first_vertical_edge_done = 0;
  6407. }
  6408. if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
  6409. start = 1;
  6410. if (FRAME_MBAFF && (dir == 1) && ((mb_y&1) == 0) && start == 0
  6411. && !IS_INTERLACED(mb_type)
  6412. && IS_INTERLACED(mbm_type)
  6413. ) {
  6414. // This is a special case in the norm where the filtering must
  6415. // be done twice (one each of the field) even if we are in a
  6416. // frame macroblock.
  6417. //
  6418. static const int nnz_idx[4] = {4,5,6,3};
  6419. unsigned int tmp_linesize = 2 * linesize;
  6420. unsigned int tmp_uvlinesize = 2 * uvlinesize;
  6421. int mbn_xy = mb_xy - 2 * s->mb_stride;
  6422. int qp, chroma_qp;
  6423. int i, j;
  6424. int16_t bS[4];
  6425. for(j=0; j<2; j++, mbn_xy += s->mb_stride){
  6426. if( IS_INTRA(mb_type) ||
  6427. IS_INTRA(s->current_picture.mb_type[mbn_xy]) ) {
  6428. bS[0] = bS[1] = bS[2] = bS[3] = 3;
  6429. } else {
  6430. const uint8_t *mbn_nnz = h->non_zero_count[mbn_xy];
  6431. for( i = 0; i < 4; i++ ) {
  6432. if( h->non_zero_count_cache[scan8[0]+i] != 0 ||
  6433. mbn_nnz[nnz_idx[i]] != 0 )
  6434. bS[i] = 2;
  6435. else
  6436. bS[i] = 1;
  6437. }
  6438. }
  6439. // Do not use s->qscale as luma quantizer because it has not the same
  6440. // value in IPCM macroblocks.
  6441. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6442. 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);
  6443. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6444. filter_mb_edgeh( h, &img_y[j*linesize], tmp_linesize, bS, qp );
  6445. chroma_qp = ( h->chroma_qp +
  6446. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6447. filter_mb_edgech( h, &img_cb[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6448. filter_mb_edgech( h, &img_cr[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6449. }
  6450. start = 1;
  6451. }
  6452. /* Calculate bS */
  6453. for( edge = start; edge < edges; edge++ ) {
  6454. /* mbn_xy: neighbor macroblock */
  6455. const int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
  6456. const int mbn_type = s->current_picture.mb_type[mbn_xy];
  6457. int16_t bS[4];
  6458. int qp;
  6459. if( (edge&1) && IS_8x8DCT(mb_type) )
  6460. continue;
  6461. if( IS_INTRA(mb_type) ||
  6462. IS_INTRA(mbn_type) ) {
  6463. int value;
  6464. if (edge == 0) {
  6465. if ( (!IS_INTERLACED(mb_type) && !IS_INTERLACED(mbm_type))
  6466. || ((FRAME_MBAFF || (s->picture_structure != PICT_FRAME)) && (dir == 0))
  6467. ) {
  6468. value = 4;
  6469. } else {
  6470. value = 3;
  6471. }
  6472. } else {
  6473. value = 3;
  6474. }
  6475. bS[0] = bS[1] = bS[2] = bS[3] = value;
  6476. } else {
  6477. int i, l;
  6478. int mv_done;
  6479. if( edge & mask_edge ) {
  6480. bS[0] = bS[1] = bS[2] = bS[3] = 0;
  6481. mv_done = 1;
  6482. }
  6483. else if( FRAME_MBAFF && IS_INTERLACED(mb_type ^ mbn_type)) {
  6484. bS[0] = bS[1] = bS[2] = bS[3] = 1;
  6485. mv_done = 1;
  6486. }
  6487. else if( mask_par0 && (edge || (mbn_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir)))) ) {
  6488. int b_idx= 8 + 4 + edge * (dir ? 8:1);
  6489. int bn_idx= b_idx - (dir ? 8:1);
  6490. int v = 0;
  6491. for( l = 0; !v && l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6492. v |= ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6493. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6494. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit;
  6495. }
  6496. bS[0] = bS[1] = bS[2] = bS[3] = v;
  6497. mv_done = 1;
  6498. }
  6499. else
  6500. mv_done = 0;
  6501. for( i = 0; i < 4; i++ ) {
  6502. int x = dir == 0 ? edge : i;
  6503. int y = dir == 0 ? i : edge;
  6504. int b_idx= 8 + 4 + x + 8*y;
  6505. int bn_idx= b_idx - (dir ? 8:1);
  6506. if( h->non_zero_count_cache[b_idx] != 0 ||
  6507. h->non_zero_count_cache[bn_idx] != 0 ) {
  6508. bS[i] = 2;
  6509. }
  6510. else if(!mv_done)
  6511. {
  6512. bS[i] = 0;
  6513. for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6514. if( ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6515. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6516. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit ) {
  6517. bS[i] = 1;
  6518. break;
  6519. }
  6520. }
  6521. }
  6522. }
  6523. if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
  6524. continue;
  6525. }
  6526. /* Filter edge */
  6527. // Do not use s->qscale as luma quantizer because it has not the same
  6528. // value in IPCM macroblocks.
  6529. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6530. //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]);
  6531. tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
  6532. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6533. if( dir == 0 ) {
  6534. filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
  6535. if( (edge&1) == 0 ) {
  6536. int chroma_qp = ( h->chroma_qp +
  6537. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6538. filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp );
  6539. filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp );
  6540. }
  6541. } else {
  6542. filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
  6543. if( (edge&1) == 0 ) {
  6544. int chroma_qp = ( h->chroma_qp +
  6545. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6546. filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6547. filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6548. }
  6549. }
  6550. }
  6551. }
  6552. }
  6553. static int decode_slice(H264Context *h){
  6554. MpegEncContext * const s = &h->s;
  6555. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  6556. s->mb_skip_run= -1;
  6557. if( h->pps.cabac ) {
  6558. int i;
  6559. /* realign */
  6560. align_get_bits( &s->gb );
  6561. /* init cabac */
  6562. ff_init_cabac_states( &h->cabac);
  6563. ff_init_cabac_decoder( &h->cabac,
  6564. s->gb.buffer + get_bits_count(&s->gb)/8,
  6565. ( s->gb.size_in_bits - get_bits_count(&s->gb) + 7)/8);
  6566. /* calculate pre-state */
  6567. for( i= 0; i < 460; i++ ) {
  6568. int pre;
  6569. if( h->slice_type == I_TYPE )
  6570. pre = clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
  6571. else
  6572. 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 );
  6573. if( pre <= 63 )
  6574. h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
  6575. else
  6576. h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
  6577. }
  6578. for(;;){
  6579. //START_TIMER
  6580. int ret = decode_mb_cabac(h);
  6581. int eos;
  6582. //STOP_TIMER("decode_mb_cabac")
  6583. if(ret>=0) hl_decode_mb(h);
  6584. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  6585. s->mb_y++;
  6586. if(ret>=0) ret = decode_mb_cabac(h);
  6587. if(ret>=0) hl_decode_mb(h);
  6588. s->mb_y--;
  6589. }
  6590. eos = get_cabac_terminate( &h->cabac );
  6591. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  6592. 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);
  6593. 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);
  6594. return -1;
  6595. }
  6596. if( ++s->mb_x >= s->mb_width ) {
  6597. s->mb_x = 0;
  6598. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6599. ++s->mb_y;
  6600. if(FRAME_MBAFF) {
  6601. ++s->mb_y;
  6602. }
  6603. }
  6604. if( eos || s->mb_y >= s->mb_height ) {
  6605. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6606. 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);
  6607. return 0;
  6608. }
  6609. }
  6610. } else {
  6611. for(;;){
  6612. int ret = decode_mb_cavlc(h);
  6613. if(ret>=0) hl_decode_mb(h);
  6614. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  6615. s->mb_y++;
  6616. ret = decode_mb_cavlc(h);
  6617. if(ret>=0) hl_decode_mb(h);
  6618. s->mb_y--;
  6619. }
  6620. if(ret<0){
  6621. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6622. 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);
  6623. return -1;
  6624. }
  6625. if(++s->mb_x >= s->mb_width){
  6626. s->mb_x=0;
  6627. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6628. ++s->mb_y;
  6629. if(FRAME_MBAFF) {
  6630. ++s->mb_y;
  6631. }
  6632. if(s->mb_y >= s->mb_height){
  6633. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6634. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  6635. 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);
  6636. return 0;
  6637. }else{
  6638. 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);
  6639. return -1;
  6640. }
  6641. }
  6642. }
  6643. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  6644. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6645. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  6646. 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);
  6647. return 0;
  6648. }else{
  6649. 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);
  6650. return -1;
  6651. }
  6652. }
  6653. }
  6654. }
  6655. #if 0
  6656. for(;s->mb_y < s->mb_height; s->mb_y++){
  6657. for(;s->mb_x < s->mb_width; s->mb_x++){
  6658. int ret= decode_mb(h);
  6659. hl_decode_mb(h);
  6660. if(ret<0){
  6661. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6662. 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);
  6663. return -1;
  6664. }
  6665. if(++s->mb_x >= s->mb_width){
  6666. s->mb_x=0;
  6667. if(++s->mb_y >= s->mb_height){
  6668. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6669. 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);
  6670. return 0;
  6671. }else{
  6672. 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);
  6673. return -1;
  6674. }
  6675. }
  6676. }
  6677. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  6678. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6679. 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);
  6680. return 0;
  6681. }else{
  6682. 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);
  6683. return -1;
  6684. }
  6685. }
  6686. }
  6687. s->mb_x=0;
  6688. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6689. }
  6690. #endif
  6691. return -1; //not reached
  6692. }
  6693. static int decode_unregistered_user_data(H264Context *h, int size){
  6694. MpegEncContext * const s = &h->s;
  6695. uint8_t user_data[16+256];
  6696. int e, build, i;
  6697. if(size<16)
  6698. return -1;
  6699. for(i=0; i<sizeof(user_data)-1 && i<size; i++){
  6700. user_data[i]= get_bits(&s->gb, 8);
  6701. }
  6702. user_data[i]= 0;
  6703. e= sscanf(user_data+16, "x264 - core %d"/*%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html*/, &build);
  6704. if(e==1 && build>=0)
  6705. h->x264_build= build;
  6706. if(s->avctx->debug & FF_DEBUG_BUGS)
  6707. av_log(s->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data+16);
  6708. for(; i<size; i++)
  6709. skip_bits(&s->gb, 8);
  6710. return 0;
  6711. }
  6712. static int decode_sei(H264Context *h){
  6713. MpegEncContext * const s = &h->s;
  6714. while(get_bits_count(&s->gb) + 16 < s->gb.size_in_bits){
  6715. int size, type;
  6716. type=0;
  6717. do{
  6718. type+= show_bits(&s->gb, 8);
  6719. }while(get_bits(&s->gb, 8) == 255);
  6720. size=0;
  6721. do{
  6722. size+= show_bits(&s->gb, 8);
  6723. }while(get_bits(&s->gb, 8) == 255);
  6724. switch(type){
  6725. case 5:
  6726. if(decode_unregistered_user_data(h, size) < 0)
  6727. return -1;
  6728. break;
  6729. default:
  6730. skip_bits(&s->gb, 8*size);
  6731. }
  6732. //FIXME check bits here
  6733. align_get_bits(&s->gb);
  6734. }
  6735. return 0;
  6736. }
  6737. static inline void decode_hrd_parameters(H264Context *h, SPS *sps){
  6738. MpegEncContext * const s = &h->s;
  6739. int cpb_count, i;
  6740. cpb_count = get_ue_golomb(&s->gb) + 1;
  6741. get_bits(&s->gb, 4); /* bit_rate_scale */
  6742. get_bits(&s->gb, 4); /* cpb_size_scale */
  6743. for(i=0; i<cpb_count; i++){
  6744. get_ue_golomb(&s->gb); /* bit_rate_value_minus1 */
  6745. get_ue_golomb(&s->gb); /* cpb_size_value_minus1 */
  6746. get_bits1(&s->gb); /* cbr_flag */
  6747. }
  6748. get_bits(&s->gb, 5); /* initial_cpb_removal_delay_length_minus1 */
  6749. get_bits(&s->gb, 5); /* cpb_removal_delay_length_minus1 */
  6750. get_bits(&s->gb, 5); /* dpb_output_delay_length_minus1 */
  6751. get_bits(&s->gb, 5); /* time_offset_length */
  6752. }
  6753. static inline int decode_vui_parameters(H264Context *h, SPS *sps){
  6754. MpegEncContext * const s = &h->s;
  6755. int aspect_ratio_info_present_flag, aspect_ratio_idc;
  6756. int nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag;
  6757. aspect_ratio_info_present_flag= get_bits1(&s->gb);
  6758. if( aspect_ratio_info_present_flag ) {
  6759. aspect_ratio_idc= get_bits(&s->gb, 8);
  6760. if( aspect_ratio_idc == EXTENDED_SAR ) {
  6761. sps->sar.num= get_bits(&s->gb, 16);
  6762. sps->sar.den= get_bits(&s->gb, 16);
  6763. }else if(aspect_ratio_idc < 14){
  6764. sps->sar= pixel_aspect[aspect_ratio_idc];
  6765. }else{
  6766. av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
  6767. return -1;
  6768. }
  6769. }else{
  6770. sps->sar.num=
  6771. sps->sar.den= 0;
  6772. }
  6773. // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
  6774. if(get_bits1(&s->gb)){ /* overscan_info_present_flag */
  6775. get_bits1(&s->gb); /* overscan_appropriate_flag */
  6776. }
  6777. if(get_bits1(&s->gb)){ /* video_signal_type_present_flag */
  6778. get_bits(&s->gb, 3); /* video_format */
  6779. get_bits1(&s->gb); /* video_full_range_flag */
  6780. if(get_bits1(&s->gb)){ /* colour_description_present_flag */
  6781. get_bits(&s->gb, 8); /* colour_primaries */
  6782. get_bits(&s->gb, 8); /* transfer_characteristics */
  6783. get_bits(&s->gb, 8); /* matrix_coefficients */
  6784. }
  6785. }
  6786. if(get_bits1(&s->gb)){ /* chroma_location_info_present_flag */
  6787. get_ue_golomb(&s->gb); /* chroma_sample_location_type_top_field */
  6788. get_ue_golomb(&s->gb); /* chroma_sample_location_type_bottom_field */
  6789. }
  6790. sps->timing_info_present_flag = get_bits1(&s->gb);
  6791. if(sps->timing_info_present_flag){
  6792. sps->num_units_in_tick = get_bits_long(&s->gb, 32);
  6793. sps->time_scale = get_bits_long(&s->gb, 32);
  6794. sps->fixed_frame_rate_flag = get_bits1(&s->gb);
  6795. }
  6796. nal_hrd_parameters_present_flag = get_bits1(&s->gb);
  6797. if(nal_hrd_parameters_present_flag)
  6798. decode_hrd_parameters(h, sps);
  6799. vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
  6800. if(vcl_hrd_parameters_present_flag)
  6801. decode_hrd_parameters(h, sps);
  6802. if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag)
  6803. get_bits1(&s->gb); /* low_delay_hrd_flag */
  6804. get_bits1(&s->gb); /* pic_struct_present_flag */
  6805. sps->bitstream_restriction_flag = get_bits1(&s->gb);
  6806. if(sps->bitstream_restriction_flag){
  6807. get_bits1(&s->gb); /* motion_vectors_over_pic_boundaries_flag */
  6808. get_ue_golomb(&s->gb); /* max_bytes_per_pic_denom */
  6809. get_ue_golomb(&s->gb); /* max_bits_per_mb_denom */
  6810. get_ue_golomb(&s->gb); /* log2_max_mv_length_horizontal */
  6811. get_ue_golomb(&s->gb); /* log2_max_mv_length_vertical */
  6812. sps->num_reorder_frames = get_ue_golomb(&s->gb);
  6813. get_ue_golomb(&s->gb); /* max_dec_frame_buffering */
  6814. }
  6815. return 0;
  6816. }
  6817. static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,
  6818. const uint8_t *jvt_list, const uint8_t *fallback_list){
  6819. MpegEncContext * const s = &h->s;
  6820. int i, last = 8, next = 8;
  6821. const uint8_t *scan = size == 16 ? zigzag_scan : zigzag_scan8x8;
  6822. if(!get_bits1(&s->gb)) /* matrix not written, we use the predicted one */
  6823. memcpy(factors, fallback_list, size*sizeof(uint8_t));
  6824. else
  6825. for(i=0;i<size;i++){
  6826. if(next)
  6827. next = (last + get_se_golomb(&s->gb)) & 0xff;
  6828. if(!i && !next){ /* matrix not written, we use the preset one */
  6829. memcpy(factors, jvt_list, size*sizeof(uint8_t));
  6830. break;
  6831. }
  6832. last = factors[scan[i]] = next ? next : last;
  6833. }
  6834. }
  6835. static void decode_scaling_matrices(H264Context *h, SPS *sps, PPS *pps, int is_sps,
  6836. uint8_t (*scaling_matrix4)[16], uint8_t (*scaling_matrix8)[64]){
  6837. MpegEncContext * const s = &h->s;
  6838. int fallback_sps = !is_sps && sps->scaling_matrix_present;
  6839. const uint8_t *fallback[4] = {
  6840. fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
  6841. fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
  6842. fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
  6843. fallback_sps ? sps->scaling_matrix8[1] : default_scaling8[1]
  6844. };
  6845. if(get_bits1(&s->gb)){
  6846. sps->scaling_matrix_present |= is_sps;
  6847. decode_scaling_list(h,scaling_matrix4[0],16,default_scaling4[0],fallback[0]); // Intra, Y
  6848. decode_scaling_list(h,scaling_matrix4[1],16,default_scaling4[0],scaling_matrix4[0]); // Intra, Cr
  6849. decode_scaling_list(h,scaling_matrix4[2],16,default_scaling4[0],scaling_matrix4[1]); // Intra, Cb
  6850. decode_scaling_list(h,scaling_matrix4[3],16,default_scaling4[1],fallback[1]); // Inter, Y
  6851. decode_scaling_list(h,scaling_matrix4[4],16,default_scaling4[1],scaling_matrix4[3]); // Inter, Cr
  6852. decode_scaling_list(h,scaling_matrix4[5],16,default_scaling4[1],scaling_matrix4[4]); // Inter, Cb
  6853. if(is_sps || pps->transform_8x8_mode){
  6854. decode_scaling_list(h,scaling_matrix8[0],64,default_scaling8[0],fallback[2]); // Intra, Y
  6855. decode_scaling_list(h,scaling_matrix8[1],64,default_scaling8[1],fallback[3]); // Inter, Y
  6856. }
  6857. } else if(fallback_sps) {
  6858. memcpy(scaling_matrix4, sps->scaling_matrix4, 6*16*sizeof(uint8_t));
  6859. memcpy(scaling_matrix8, sps->scaling_matrix8, 2*64*sizeof(uint8_t));
  6860. }
  6861. }
  6862. static inline int decode_seq_parameter_set(H264Context *h){
  6863. MpegEncContext * const s = &h->s;
  6864. int profile_idc, level_idc;
  6865. int sps_id, i;
  6866. SPS *sps;
  6867. profile_idc= get_bits(&s->gb, 8);
  6868. get_bits1(&s->gb); //constraint_set0_flag
  6869. get_bits1(&s->gb); //constraint_set1_flag
  6870. get_bits1(&s->gb); //constraint_set2_flag
  6871. get_bits1(&s->gb); //constraint_set3_flag
  6872. get_bits(&s->gb, 4); // reserved
  6873. level_idc= get_bits(&s->gb, 8);
  6874. sps_id= get_ue_golomb(&s->gb);
  6875. sps= &h->sps_buffer[ sps_id ];
  6876. sps->profile_idc= profile_idc;
  6877. sps->level_idc= level_idc;
  6878. if(sps->profile_idc >= 100){ //high profile
  6879. if(get_ue_golomb(&s->gb) == 3) //chroma_format_idc
  6880. get_bits1(&s->gb); //residual_color_transform_flag
  6881. get_ue_golomb(&s->gb); //bit_depth_luma_minus8
  6882. get_ue_golomb(&s->gb); //bit_depth_chroma_minus8
  6883. sps->transform_bypass = get_bits1(&s->gb);
  6884. decode_scaling_matrices(h, sps, NULL, 1, sps->scaling_matrix4, sps->scaling_matrix8);
  6885. }else
  6886. sps->scaling_matrix_present = 0;
  6887. sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
  6888. sps->poc_type= get_ue_golomb(&s->gb);
  6889. if(sps->poc_type == 0){ //FIXME #define
  6890. sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
  6891. } else if(sps->poc_type == 1){//FIXME #define
  6892. sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
  6893. sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
  6894. sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
  6895. sps->poc_cycle_length= get_ue_golomb(&s->gb);
  6896. for(i=0; i<sps->poc_cycle_length; i++)
  6897. sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
  6898. }
  6899. if(sps->poc_type > 2){
  6900. av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
  6901. return -1;
  6902. }
  6903. sps->ref_frame_count= get_ue_golomb(&s->gb);
  6904. if(sps->ref_frame_count > MAX_PICTURE_COUNT-2){
  6905. av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");
  6906. }
  6907. sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
  6908. sps->mb_width= get_ue_golomb(&s->gb) + 1;
  6909. sps->mb_height= get_ue_golomb(&s->gb) + 1;
  6910. if((unsigned)sps->mb_width >= INT_MAX/16 || (unsigned)sps->mb_height >= INT_MAX/16 ||
  6911. avcodec_check_dimensions(NULL, 16*sps->mb_width, 16*sps->mb_height))
  6912. return -1;
  6913. sps->frame_mbs_only_flag= get_bits1(&s->gb);
  6914. if(!sps->frame_mbs_only_flag)
  6915. sps->mb_aff= get_bits1(&s->gb);
  6916. else
  6917. sps->mb_aff= 0;
  6918. sps->direct_8x8_inference_flag= get_bits1(&s->gb);
  6919. #ifndef ALLOW_INTERLACE
  6920. if(sps->mb_aff)
  6921. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF support not included; enable it at compile-time.\n");
  6922. #endif
  6923. if(!sps->direct_8x8_inference_flag && sps->mb_aff)
  6924. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + !direct_8x8_inference is not implemented\n");
  6925. sps->crop= get_bits1(&s->gb);
  6926. if(sps->crop){
  6927. sps->crop_left = get_ue_golomb(&s->gb);
  6928. sps->crop_right = get_ue_golomb(&s->gb);
  6929. sps->crop_top = get_ue_golomb(&s->gb);
  6930. sps->crop_bottom= get_ue_golomb(&s->gb);
  6931. if(sps->crop_left || sps->crop_top){
  6932. av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ...\n");
  6933. }
  6934. }else{
  6935. sps->crop_left =
  6936. sps->crop_right =
  6937. sps->crop_top =
  6938. sps->crop_bottom= 0;
  6939. }
  6940. sps->vui_parameters_present_flag= get_bits1(&s->gb);
  6941. if( sps->vui_parameters_present_flag )
  6942. decode_vui_parameters(h, sps);
  6943. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6944. 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",
  6945. sps_id, sps->profile_idc, sps->level_idc,
  6946. sps->poc_type,
  6947. sps->ref_frame_count,
  6948. sps->mb_width, sps->mb_height,
  6949. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  6950. sps->direct_8x8_inference_flag ? "8B8" : "",
  6951. sps->crop_left, sps->crop_right,
  6952. sps->crop_top, sps->crop_bottom,
  6953. sps->vui_parameters_present_flag ? "VUI" : ""
  6954. );
  6955. }
  6956. return 0;
  6957. }
  6958. static inline int decode_picture_parameter_set(H264Context *h, int bit_length){
  6959. MpegEncContext * const s = &h->s;
  6960. int pps_id= get_ue_golomb(&s->gb);
  6961. PPS *pps= &h->pps_buffer[pps_id];
  6962. pps->sps_id= get_ue_golomb(&s->gb);
  6963. pps->cabac= get_bits1(&s->gb);
  6964. pps->pic_order_present= get_bits1(&s->gb);
  6965. pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
  6966. if(pps->slice_group_count > 1 ){
  6967. pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
  6968. av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
  6969. switch(pps->mb_slice_group_map_type){
  6970. case 0:
  6971. #if 0
  6972. | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
  6973. | run_length[ i ] |1 |ue(v) |
  6974. #endif
  6975. break;
  6976. case 2:
  6977. #if 0
  6978. | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
  6979. |{ | | |
  6980. | top_left_mb[ i ] |1 |ue(v) |
  6981. | bottom_right_mb[ i ] |1 |ue(v) |
  6982. | } | | |
  6983. #endif
  6984. break;
  6985. case 3:
  6986. case 4:
  6987. case 5:
  6988. #if 0
  6989. | slice_group_change_direction_flag |1 |u(1) |
  6990. | slice_group_change_rate_minus1 |1 |ue(v) |
  6991. #endif
  6992. break;
  6993. case 6:
  6994. #if 0
  6995. | slice_group_id_cnt_minus1 |1 |ue(v) |
  6996. | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
  6997. |) | | |
  6998. | slice_group_id[ i ] |1 |u(v) |
  6999. #endif
  7000. break;
  7001. }
  7002. }
  7003. pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  7004. pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  7005. if(pps->ref_count[0] > 32 || pps->ref_count[1] > 32){
  7006. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
  7007. return -1;
  7008. }
  7009. pps->weighted_pred= get_bits1(&s->gb);
  7010. pps->weighted_bipred_idc= get_bits(&s->gb, 2);
  7011. pps->init_qp= get_se_golomb(&s->gb) + 26;
  7012. pps->init_qs= get_se_golomb(&s->gb) + 26;
  7013. pps->chroma_qp_index_offset= get_se_golomb(&s->gb);
  7014. pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
  7015. pps->constrained_intra_pred= get_bits1(&s->gb);
  7016. pps->redundant_pic_cnt_present = get_bits1(&s->gb);
  7017. pps->transform_8x8_mode= 0;
  7018. h->dequant_coeff_pps= -1; //contents of sps/pps can change even if id doesn't, so reinit
  7019. memset(pps->scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  7020. memset(pps->scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  7021. if(get_bits_count(&s->gb) < bit_length){
  7022. pps->transform_8x8_mode= get_bits1(&s->gb);
  7023. decode_scaling_matrices(h, &h->sps_buffer[pps->sps_id], pps, 0, pps->scaling_matrix4, pps->scaling_matrix8);
  7024. get_se_golomb(&s->gb); //second_chroma_qp_index_offset
  7025. }
  7026. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  7027. 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",
  7028. pps_id, pps->sps_id,
  7029. pps->cabac ? "CABAC" : "CAVLC",
  7030. pps->slice_group_count,
  7031. pps->ref_count[0], pps->ref_count[1],
  7032. pps->weighted_pred ? "weighted" : "",
  7033. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset,
  7034. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  7035. pps->constrained_intra_pred ? "CONSTR" : "",
  7036. pps->redundant_pic_cnt_present ? "REDU" : "",
  7037. pps->transform_8x8_mode ? "8x8DCT" : ""
  7038. );
  7039. }
  7040. return 0;
  7041. }
  7042. /**
  7043. * finds the end of the current frame in the bitstream.
  7044. * @return the position of the first byte of the next frame, or -1
  7045. */
  7046. static int find_frame_end(H264Context *h, const uint8_t *buf, int buf_size){
  7047. int i;
  7048. uint32_t state;
  7049. ParseContext *pc = &(h->s.parse_context);
  7050. //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
  7051. // mb_addr= pc->mb_addr - 1;
  7052. state= pc->state;
  7053. for(i=0; i<=buf_size; i++){
  7054. if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  7055. tprintf("find_frame_end new startcode = %08x, frame_start_found = %d, pos = %d\n", state, pc->frame_start_found, i);
  7056. if(pc->frame_start_found){
  7057. // If there isn't one more byte in the buffer
  7058. // the test on first_mb_in_slice cannot be done yet
  7059. // do it at next call.
  7060. if (i >= buf_size) break;
  7061. if (buf[i] & 0x80) {
  7062. // first_mb_in_slice is 0, probably the first nal of a new
  7063. // slice
  7064. tprintf("find_frame_end frame_end_found, state = %08x, pos = %d\n", state, i);
  7065. pc->state=-1;
  7066. pc->frame_start_found= 0;
  7067. return i-4;
  7068. }
  7069. }
  7070. pc->frame_start_found = 1;
  7071. }
  7072. if((state&0xFFFFFF1F) == 0x107 || (state&0xFFFFFF1F) == 0x108 || (state&0xFFFFFF1F) == 0x109){
  7073. if(pc->frame_start_found){
  7074. pc->state=-1;
  7075. pc->frame_start_found= 0;
  7076. return i-4;
  7077. }
  7078. }
  7079. if (i<buf_size)
  7080. state= (state<<8) | buf[i];
  7081. }
  7082. pc->state= state;
  7083. return END_NOT_FOUND;
  7084. }
  7085. #ifdef CONFIG_H264_PARSER
  7086. static int h264_parse(AVCodecParserContext *s,
  7087. AVCodecContext *avctx,
  7088. uint8_t **poutbuf, int *poutbuf_size,
  7089. const uint8_t *buf, int buf_size)
  7090. {
  7091. H264Context *h = s->priv_data;
  7092. ParseContext *pc = &h->s.parse_context;
  7093. int next;
  7094. next= find_frame_end(h, buf, buf_size);
  7095. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  7096. *poutbuf = NULL;
  7097. *poutbuf_size = 0;
  7098. return buf_size;
  7099. }
  7100. *poutbuf = (uint8_t *)buf;
  7101. *poutbuf_size = buf_size;
  7102. return next;
  7103. }
  7104. static int h264_split(AVCodecContext *avctx,
  7105. const uint8_t *buf, int buf_size)
  7106. {
  7107. int i;
  7108. uint32_t state = -1;
  7109. int has_sps= 0;
  7110. for(i=0; i<=buf_size; i++){
  7111. if((state&0xFFFFFF1F) == 0x107)
  7112. has_sps=1;
  7113. /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  7114. }*/
  7115. if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
  7116. if(has_sps){
  7117. while(i>4 && buf[i-5]==0) i--;
  7118. return i-4;
  7119. }
  7120. }
  7121. if (i<buf_size)
  7122. state= (state<<8) | buf[i];
  7123. }
  7124. return 0;
  7125. }
  7126. #endif /* CONFIG_H264_PARSER */
  7127. static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){
  7128. MpegEncContext * const s = &h->s;
  7129. AVCodecContext * const avctx= s->avctx;
  7130. int buf_index=0;
  7131. #if 0
  7132. int i;
  7133. for(i=0; i<50; i++){
  7134. av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
  7135. }
  7136. #endif
  7137. h->slice_num = 0;
  7138. s->current_picture_ptr= NULL;
  7139. for(;;){
  7140. int consumed;
  7141. int dst_length;
  7142. int bit_length;
  7143. uint8_t *ptr;
  7144. int i, nalsize = 0;
  7145. if(h->is_avc) {
  7146. if(buf_index >= buf_size) break;
  7147. nalsize = 0;
  7148. for(i = 0; i < h->nal_length_size; i++)
  7149. nalsize = (nalsize << 8) | buf[buf_index++];
  7150. if(nalsize <= 1){
  7151. if(nalsize == 1){
  7152. buf_index++;
  7153. continue;
  7154. }else{
  7155. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  7156. break;
  7157. }
  7158. }
  7159. } else {
  7160. // start code prefix search
  7161. for(; buf_index + 3 < buf_size; buf_index++){
  7162. // this should allways succeed in the first iteration
  7163. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  7164. break;
  7165. }
  7166. if(buf_index+3 >= buf_size) break;
  7167. buf_index+=3;
  7168. }
  7169. ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, h->is_avc ? nalsize : buf_size - buf_index);
  7170. while(ptr[dst_length - 1] == 0 && dst_length > 1)
  7171. dst_length--;
  7172. bit_length= 8*dst_length - decode_rbsp_trailing(ptr + dst_length - 1);
  7173. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  7174. 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);
  7175. }
  7176. if (h->is_avc && (nalsize != consumed))
  7177. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  7178. buf_index += consumed;
  7179. if( (s->hurry_up == 1 && h->nal_ref_idc == 0) //FIXME dont discard SEI id
  7180. ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  7181. continue;
  7182. switch(h->nal_unit_type){
  7183. case NAL_IDR_SLICE:
  7184. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  7185. case NAL_SLICE:
  7186. init_get_bits(&s->gb, ptr, bit_length);
  7187. h->intra_gb_ptr=
  7188. h->inter_gb_ptr= &s->gb;
  7189. s->data_partitioning = 0;
  7190. if(decode_slice_header(h) < 0){
  7191. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  7192. break;
  7193. }
  7194. s->current_picture_ptr->key_frame= (h->nal_unit_type == NAL_IDR_SLICE);
  7195. if(h->redundant_pic_count==0 && s->hurry_up < 5
  7196. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  7197. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  7198. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  7199. && avctx->skip_frame < AVDISCARD_ALL)
  7200. decode_slice(h);
  7201. break;
  7202. case NAL_DPA:
  7203. init_get_bits(&s->gb, ptr, bit_length);
  7204. h->intra_gb_ptr=
  7205. h->inter_gb_ptr= NULL;
  7206. s->data_partitioning = 1;
  7207. if(decode_slice_header(h) < 0){
  7208. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  7209. }
  7210. break;
  7211. case NAL_DPB:
  7212. init_get_bits(&h->intra_gb, ptr, bit_length);
  7213. h->intra_gb_ptr= &h->intra_gb;
  7214. break;
  7215. case NAL_DPC:
  7216. init_get_bits(&h->inter_gb, ptr, bit_length);
  7217. h->inter_gb_ptr= &h->inter_gb;
  7218. if(h->redundant_pic_count==0 && h->intra_gb_ptr && s->data_partitioning
  7219. && s->hurry_up < 5
  7220. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  7221. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  7222. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  7223. && avctx->skip_frame < AVDISCARD_ALL)
  7224. decode_slice(h);
  7225. break;
  7226. case NAL_SEI:
  7227. init_get_bits(&s->gb, ptr, bit_length);
  7228. decode_sei(h);
  7229. break;
  7230. case NAL_SPS:
  7231. init_get_bits(&s->gb, ptr, bit_length);
  7232. decode_seq_parameter_set(h);
  7233. if(s->flags& CODEC_FLAG_LOW_DELAY)
  7234. s->low_delay=1;
  7235. if(avctx->has_b_frames < 2)
  7236. avctx->has_b_frames= !s->low_delay;
  7237. break;
  7238. case NAL_PPS:
  7239. init_get_bits(&s->gb, ptr, bit_length);
  7240. decode_picture_parameter_set(h, bit_length);
  7241. break;
  7242. case NAL_AUD:
  7243. case NAL_END_SEQUENCE:
  7244. case NAL_END_STREAM:
  7245. case NAL_FILLER_DATA:
  7246. case NAL_SPS_EXT:
  7247. case NAL_AUXILIARY_SLICE:
  7248. break;
  7249. default:
  7250. av_log(avctx, AV_LOG_ERROR, "Unknown NAL code: %d\n", h->nal_unit_type);
  7251. }
  7252. }
  7253. if(!s->current_picture_ptr) return buf_index; //no frame
  7254. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  7255. s->current_picture_ptr->pict_type= s->pict_type;
  7256. h->prev_frame_num_offset= h->frame_num_offset;
  7257. h->prev_frame_num= h->frame_num;
  7258. if(s->current_picture_ptr->reference){
  7259. h->prev_poc_msb= h->poc_msb;
  7260. h->prev_poc_lsb= h->poc_lsb;
  7261. }
  7262. if(s->current_picture_ptr->reference)
  7263. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  7264. ff_er_frame_end(s);
  7265. MPV_frame_end(s);
  7266. return buf_index;
  7267. }
  7268. /**
  7269. * returns the number of bytes consumed for building the current frame
  7270. */
  7271. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  7272. if(s->flags&CODEC_FLAG_TRUNCATED){
  7273. pos -= s->parse_context.last_index;
  7274. if(pos<0) pos=0; // FIXME remove (unneeded?)
  7275. return pos;
  7276. }else{
  7277. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  7278. if(pos+10>buf_size) pos=buf_size; // oops ;)
  7279. return pos;
  7280. }
  7281. }
  7282. static int decode_frame(AVCodecContext *avctx,
  7283. void *data, int *data_size,
  7284. uint8_t *buf, int buf_size)
  7285. {
  7286. H264Context *h = avctx->priv_data;
  7287. MpegEncContext *s = &h->s;
  7288. AVFrame *pict = data;
  7289. int buf_index;
  7290. s->flags= avctx->flags;
  7291. s->flags2= avctx->flags2;
  7292. /* no supplementary picture */
  7293. if (buf_size == 0) {
  7294. return 0;
  7295. }
  7296. if(s->flags&CODEC_FLAG_TRUNCATED){
  7297. int next= find_frame_end(h, buf, buf_size);
  7298. if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
  7299. return buf_size;
  7300. //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);
  7301. }
  7302. if(h->is_avc && !h->got_avcC) {
  7303. int i, cnt, nalsize;
  7304. unsigned char *p = avctx->extradata;
  7305. if(avctx->extradata_size < 7) {
  7306. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  7307. return -1;
  7308. }
  7309. if(*p != 1) {
  7310. av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
  7311. return -1;
  7312. }
  7313. /* sps and pps in the avcC always have length coded with 2 bytes,
  7314. so put a fake nal_length_size = 2 while parsing them */
  7315. h->nal_length_size = 2;
  7316. // Decode sps from avcC
  7317. cnt = *(p+5) & 0x1f; // Number of sps
  7318. p += 6;
  7319. for (i = 0; i < cnt; i++) {
  7320. nalsize = BE_16(p) + 2;
  7321. if(decode_nal_units(h, p, nalsize) < 0) {
  7322. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  7323. return -1;
  7324. }
  7325. p += nalsize;
  7326. }
  7327. // Decode pps from avcC
  7328. cnt = *(p++); // Number of pps
  7329. for (i = 0; i < cnt; i++) {
  7330. nalsize = BE_16(p) + 2;
  7331. if(decode_nal_units(h, p, nalsize) != nalsize) {
  7332. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  7333. return -1;
  7334. }
  7335. p += nalsize;
  7336. }
  7337. // Now store right nal length size, that will be use to parse all other nals
  7338. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  7339. // Do not reparse avcC
  7340. h->got_avcC = 1;
  7341. }
  7342. if(!h->is_avc && s->avctx->extradata_size && s->picture_number==0){
  7343. if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
  7344. return -1;
  7345. }
  7346. buf_index=decode_nal_units(h, buf, buf_size);
  7347. if(buf_index < 0)
  7348. return -1;
  7349. //FIXME do something with unavailable reference frames
  7350. // if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_index, buf_size);
  7351. if(!s->current_picture_ptr){
  7352. av_log(h->s.avctx, AV_LOG_DEBUG, "error, NO frame\n");
  7353. return -1;
  7354. }
  7355. {
  7356. Picture *out = s->current_picture_ptr;
  7357. #if 0 //decode order
  7358. *data_size = sizeof(AVFrame);
  7359. #else
  7360. /* Sort B-frames into display order */
  7361. Picture *cur = s->current_picture_ptr;
  7362. Picture *prev = h->delayed_output_pic;
  7363. int i, pics, cross_idr, out_of_order, out_idx;
  7364. if(h->sps.bitstream_restriction_flag
  7365. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  7366. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  7367. s->low_delay = 0;
  7368. }
  7369. pics = 0;
  7370. while(h->delayed_pic[pics]) pics++;
  7371. h->delayed_pic[pics++] = cur;
  7372. if(cur->reference == 0)
  7373. cur->reference = 1;
  7374. cross_idr = 0;
  7375. for(i=0; h->delayed_pic[i]; i++)
  7376. if(h->delayed_pic[i]->key_frame || h->delayed_pic[i]->poc==0)
  7377. cross_idr = 1;
  7378. out = h->delayed_pic[0];
  7379. out_idx = 0;
  7380. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
  7381. if(h->delayed_pic[i]->poc < out->poc){
  7382. out = h->delayed_pic[i];
  7383. out_idx = i;
  7384. }
  7385. out_of_order = !cross_idr && prev && out->poc < prev->poc;
  7386. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  7387. { }
  7388. else if(prev && pics <= s->avctx->has_b_frames)
  7389. out = prev;
  7390. else if((out_of_order && pics-1 == s->avctx->has_b_frames && pics < 15)
  7391. || (s->low_delay &&
  7392. ((!cross_idr && prev && out->poc > prev->poc + 2)
  7393. || cur->pict_type == B_TYPE)))
  7394. {
  7395. s->low_delay = 0;
  7396. s->avctx->has_b_frames++;
  7397. out = prev;
  7398. }
  7399. else if(out_of_order)
  7400. out = prev;
  7401. if(out_of_order || pics > s->avctx->has_b_frames){
  7402. for(i=out_idx; h->delayed_pic[i]; i++)
  7403. h->delayed_pic[i] = h->delayed_pic[i+1];
  7404. }
  7405. if(prev == out)
  7406. *data_size = 0;
  7407. else
  7408. *data_size = sizeof(AVFrame);
  7409. if(prev && prev != out && prev->reference == 1)
  7410. prev->reference = 0;
  7411. h->delayed_output_pic = out;
  7412. #endif
  7413. if(out)
  7414. *pict= *(AVFrame*)out;
  7415. else
  7416. av_log(avctx, AV_LOG_DEBUG, "no picture\n");
  7417. }
  7418. assert(pict->data[0] || !*data_size);
  7419. ff_print_debug_info(s, pict);
  7420. //printf("out %d\n", (int)pict->data[0]);
  7421. #if 0 //?
  7422. /* Return the Picture timestamp as the frame number */
  7423. /* we substract 1 because it is added on utils.c */
  7424. avctx->frame_number = s->picture_number - 1;
  7425. #endif
  7426. return get_consumed_bytes(s, buf_index, buf_size);
  7427. }
  7428. #if 0
  7429. static inline void fill_mb_avail(H264Context *h){
  7430. MpegEncContext * const s = &h->s;
  7431. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  7432. if(s->mb_y){
  7433. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  7434. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  7435. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  7436. }else{
  7437. h->mb_avail[0]=
  7438. h->mb_avail[1]=
  7439. h->mb_avail[2]= 0;
  7440. }
  7441. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  7442. h->mb_avail[4]= 1; //FIXME move out
  7443. h->mb_avail[5]= 0; //FIXME move out
  7444. }
  7445. #endif
  7446. #if 0 //selftest
  7447. #define COUNT 8000
  7448. #define SIZE (COUNT*40)
  7449. int main(){
  7450. int i;
  7451. uint8_t temp[SIZE];
  7452. PutBitContext pb;
  7453. GetBitContext gb;
  7454. // int int_temp[10000];
  7455. DSPContext dsp;
  7456. AVCodecContext avctx;
  7457. dsputil_init(&dsp, &avctx);
  7458. init_put_bits(&pb, temp, SIZE);
  7459. printf("testing unsigned exp golomb\n");
  7460. for(i=0; i<COUNT; i++){
  7461. START_TIMER
  7462. set_ue_golomb(&pb, i);
  7463. STOP_TIMER("set_ue_golomb");
  7464. }
  7465. flush_put_bits(&pb);
  7466. init_get_bits(&gb, temp, 8*SIZE);
  7467. for(i=0; i<COUNT; i++){
  7468. int j, s;
  7469. s= show_bits(&gb, 24);
  7470. START_TIMER
  7471. j= get_ue_golomb(&gb);
  7472. if(j != i){
  7473. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7474. // return -1;
  7475. }
  7476. STOP_TIMER("get_ue_golomb");
  7477. }
  7478. init_put_bits(&pb, temp, SIZE);
  7479. printf("testing signed exp golomb\n");
  7480. for(i=0; i<COUNT; i++){
  7481. START_TIMER
  7482. set_se_golomb(&pb, i - COUNT/2);
  7483. STOP_TIMER("set_se_golomb");
  7484. }
  7485. flush_put_bits(&pb);
  7486. init_get_bits(&gb, temp, 8*SIZE);
  7487. for(i=0; i<COUNT; i++){
  7488. int j, s;
  7489. s= show_bits(&gb, 24);
  7490. START_TIMER
  7491. j= get_se_golomb(&gb);
  7492. if(j != i - COUNT/2){
  7493. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7494. // return -1;
  7495. }
  7496. STOP_TIMER("get_se_golomb");
  7497. }
  7498. printf("testing 4x4 (I)DCT\n");
  7499. DCTELEM block[16];
  7500. uint8_t src[16], ref[16];
  7501. uint64_t error= 0, max_error=0;
  7502. for(i=0; i<COUNT; i++){
  7503. int j;
  7504. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  7505. for(j=0; j<16; j++){
  7506. ref[j]= random()%255;
  7507. src[j]= random()%255;
  7508. }
  7509. h264_diff_dct_c(block, src, ref, 4);
  7510. //normalize
  7511. for(j=0; j<16; j++){
  7512. // printf("%d ", block[j]);
  7513. block[j]= block[j]*4;
  7514. if(j&1) block[j]= (block[j]*4 + 2)/5;
  7515. if(j&4) block[j]= (block[j]*4 + 2)/5;
  7516. }
  7517. // printf("\n");
  7518. s->dsp.h264_idct_add(ref, block, 4);
  7519. /* for(j=0; j<16; j++){
  7520. printf("%d ", ref[j]);
  7521. }
  7522. printf("\n");*/
  7523. for(j=0; j<16; j++){
  7524. int diff= FFABS(src[j] - ref[j]);
  7525. error+= diff*diff;
  7526. max_error= FFMAX(max_error, diff);
  7527. }
  7528. }
  7529. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  7530. #if 0
  7531. printf("testing quantizer\n");
  7532. for(qp=0; qp<52; qp++){
  7533. for(i=0; i<16; i++)
  7534. src1_block[i]= src2_block[i]= random()%255;
  7535. }
  7536. #endif
  7537. printf("Testing NAL layer\n");
  7538. uint8_t bitstream[COUNT];
  7539. uint8_t nal[COUNT*2];
  7540. H264Context h;
  7541. memset(&h, 0, sizeof(H264Context));
  7542. for(i=0; i<COUNT; i++){
  7543. int zeros= i;
  7544. int nal_length;
  7545. int consumed;
  7546. int out_length;
  7547. uint8_t *out;
  7548. int j;
  7549. for(j=0; j<COUNT; j++){
  7550. bitstream[j]= (random() % 255) + 1;
  7551. }
  7552. for(j=0; j<zeros; j++){
  7553. int pos= random() % COUNT;
  7554. while(bitstream[pos] == 0){
  7555. pos++;
  7556. pos %= COUNT;
  7557. }
  7558. bitstream[pos]=0;
  7559. }
  7560. START_TIMER
  7561. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  7562. if(nal_length<0){
  7563. printf("encoding failed\n");
  7564. return -1;
  7565. }
  7566. out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
  7567. STOP_TIMER("NAL")
  7568. if(out_length != COUNT){
  7569. printf("incorrect length %d %d\n", out_length, COUNT);
  7570. return -1;
  7571. }
  7572. if(consumed != nal_length){
  7573. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  7574. return -1;
  7575. }
  7576. if(memcmp(bitstream, out, COUNT)){
  7577. printf("missmatch\n");
  7578. return -1;
  7579. }
  7580. }
  7581. printf("Testing RBSP\n");
  7582. return 0;
  7583. }
  7584. #endif
  7585. static int decode_end(AVCodecContext *avctx)
  7586. {
  7587. H264Context *h = avctx->priv_data;
  7588. MpegEncContext *s = &h->s;
  7589. av_freep(&h->rbsp_buffer);
  7590. free_tables(h); //FIXME cleanup init stuff perhaps
  7591. MPV_common_end(s);
  7592. // memset(h, 0, sizeof(H264Context));
  7593. return 0;
  7594. }
  7595. AVCodec h264_decoder = {
  7596. "h264",
  7597. CODEC_TYPE_VIDEO,
  7598. CODEC_ID_H264,
  7599. sizeof(H264Context),
  7600. decode_init,
  7601. NULL,
  7602. decode_end,
  7603. decode_frame,
  7604. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  7605. .flush= flush_dpb,
  7606. };
  7607. #ifdef CONFIG_H264_PARSER
  7608. AVCodecParser h264_parser = {
  7609. { CODEC_ID_H264 },
  7610. sizeof(H264Context),
  7611. NULL,
  7612. h264_parse,
  7613. ff_parse_close,
  7614. h264_split,
  7615. };
  7616. #endif
  7617. #include "svq3.c"