You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

8656 lines
325KB

  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. unsigned 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. unsigned 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. unsigned 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. unsigned int ref_count[2]; ///< counts frames or fields, depending on current mb mode
  282. unsigned int list_count;
  283. Picture *short_ref[32];
  284. Picture *long_ref[32];
  285. Picture default_ref_list[2][32];
  286. Picture ref_list[2][48]; ///< 0..15: frame refs, 16..47: mbaff field refs
  287. Picture *delayed_pic[18]; //FIXME size?
  288. Picture *delayed_output_pic;
  289. /**
  290. * memory management control operations buffer.
  291. */
  292. MMCO mmco[MAX_MMCO_COUNT];
  293. int mmco_index;
  294. int long_ref_count; ///< number of actual long term references
  295. int short_ref_count; ///< number of actual short term references
  296. //data partitioning
  297. GetBitContext intra_gb;
  298. GetBitContext inter_gb;
  299. GetBitContext *intra_gb_ptr;
  300. GetBitContext *inter_gb_ptr;
  301. DECLARE_ALIGNED_8(DCTELEM, mb[16*24]);
  302. DCTELEM mb_padding[256]; ///< as mb is addressed by scantable[i] and scantable is uint8_t we can either check that i is not to large or ensure that there is some unused stuff after mb
  303. /**
  304. * Cabac
  305. */
  306. CABACContext cabac;
  307. uint8_t cabac_state[460];
  308. int cabac_init_idc;
  309. /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
  310. uint16_t *cbp_table;
  311. int cbp;
  312. int top_cbp;
  313. int left_cbp;
  314. /* chroma_pred_mode for i4x4 or i16x16, else 0 */
  315. uint8_t *chroma_pred_mode_table;
  316. int last_qscale_diff;
  317. int16_t (*mvd_table[2])[2];
  318. DECLARE_ALIGNED_8(int16_t, mvd_cache[2][5*8][2]);
  319. uint8_t *direct_table;
  320. uint8_t direct_cache[5*8];
  321. uint8_t zigzag_scan[16];
  322. uint8_t zigzag_scan8x8[64];
  323. uint8_t zigzag_scan8x8_cavlc[64];
  324. uint8_t field_scan[16];
  325. uint8_t field_scan8x8[64];
  326. uint8_t field_scan8x8_cavlc[64];
  327. const uint8_t *zigzag_scan_q0;
  328. const uint8_t *zigzag_scan8x8_q0;
  329. const uint8_t *zigzag_scan8x8_cavlc_q0;
  330. const uint8_t *field_scan_q0;
  331. const uint8_t *field_scan8x8_q0;
  332. const uint8_t *field_scan8x8_cavlc_q0;
  333. int x264_build;
  334. }H264Context;
  335. static VLC coeff_token_vlc[4];
  336. static VLC chroma_dc_coeff_token_vlc;
  337. static VLC total_zeros_vlc[15];
  338. static VLC chroma_dc_total_zeros_vlc[3];
  339. static VLC run_vlc[6];
  340. static VLC run7_vlc;
  341. static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
  342. static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
  343. 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);
  344. 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);
  345. static av_always_inline uint32_t pack16to32(int a, int b){
  346. #ifdef WORDS_BIGENDIAN
  347. return (b&0xFFFF) + (a<<16);
  348. #else
  349. return (a&0xFFFF) + (b<<16);
  350. #endif
  351. }
  352. const uint8_t ff_rem6[52]={
  353. 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
  354. };
  355. const uint8_t ff_div6[52]={
  356. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
  357. };
  358. /**
  359. * fill a rectangle.
  360. * @param h height of the rectangle, should be a constant
  361. * @param w width of the rectangle, should be a constant
  362. * @param size the size of val (1 or 4), should be a constant
  363. */
  364. static av_always_inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){
  365. uint8_t *p= (uint8_t*)vp;
  366. assert(size==1 || size==4);
  367. assert(w<=4);
  368. w *= size;
  369. stride *= size;
  370. assert((((long)vp)&(FFMIN(w, STRIDE_ALIGN)-1)) == 0);
  371. assert((stride&(w-1))==0);
  372. if(w==2){
  373. const uint16_t v= size==4 ? val : val*0x0101;
  374. *(uint16_t*)(p + 0*stride)= v;
  375. if(h==1) return;
  376. *(uint16_t*)(p + 1*stride)= v;
  377. if(h==2) return;
  378. *(uint16_t*)(p + 2*stride)=
  379. *(uint16_t*)(p + 3*stride)= v;
  380. }else if(w==4){
  381. const uint32_t v= size==4 ? val : val*0x01010101;
  382. *(uint32_t*)(p + 0*stride)= v;
  383. if(h==1) return;
  384. *(uint32_t*)(p + 1*stride)= v;
  385. if(h==2) return;
  386. *(uint32_t*)(p + 2*stride)=
  387. *(uint32_t*)(p + 3*stride)= v;
  388. }else if(w==8){
  389. //gcc can't optimize 64bit math on x86_32
  390. #if defined(ARCH_X86_64) || (defined(MP_WORDSIZE) && MP_WORDSIZE >= 64)
  391. const uint64_t v= val*0x0100000001ULL;
  392. *(uint64_t*)(p + 0*stride)= v;
  393. if(h==1) return;
  394. *(uint64_t*)(p + 1*stride)= v;
  395. if(h==2) return;
  396. *(uint64_t*)(p + 2*stride)=
  397. *(uint64_t*)(p + 3*stride)= v;
  398. }else if(w==16){
  399. const uint64_t v= val*0x0100000001ULL;
  400. *(uint64_t*)(p + 0+0*stride)=
  401. *(uint64_t*)(p + 8+0*stride)=
  402. *(uint64_t*)(p + 0+1*stride)=
  403. *(uint64_t*)(p + 8+1*stride)= v;
  404. if(h==2) return;
  405. *(uint64_t*)(p + 0+2*stride)=
  406. *(uint64_t*)(p + 8+2*stride)=
  407. *(uint64_t*)(p + 0+3*stride)=
  408. *(uint64_t*)(p + 8+3*stride)= v;
  409. #else
  410. *(uint32_t*)(p + 0+0*stride)=
  411. *(uint32_t*)(p + 4+0*stride)= val;
  412. if(h==1) return;
  413. *(uint32_t*)(p + 0+1*stride)=
  414. *(uint32_t*)(p + 4+1*stride)= val;
  415. if(h==2) return;
  416. *(uint32_t*)(p + 0+2*stride)=
  417. *(uint32_t*)(p + 4+2*stride)=
  418. *(uint32_t*)(p + 0+3*stride)=
  419. *(uint32_t*)(p + 4+3*stride)= val;
  420. }else if(w==16){
  421. *(uint32_t*)(p + 0+0*stride)=
  422. *(uint32_t*)(p + 4+0*stride)=
  423. *(uint32_t*)(p + 8+0*stride)=
  424. *(uint32_t*)(p +12+0*stride)=
  425. *(uint32_t*)(p + 0+1*stride)=
  426. *(uint32_t*)(p + 4+1*stride)=
  427. *(uint32_t*)(p + 8+1*stride)=
  428. *(uint32_t*)(p +12+1*stride)= val;
  429. if(h==2) return;
  430. *(uint32_t*)(p + 0+2*stride)=
  431. *(uint32_t*)(p + 4+2*stride)=
  432. *(uint32_t*)(p + 8+2*stride)=
  433. *(uint32_t*)(p +12+2*stride)=
  434. *(uint32_t*)(p + 0+3*stride)=
  435. *(uint32_t*)(p + 4+3*stride)=
  436. *(uint32_t*)(p + 8+3*stride)=
  437. *(uint32_t*)(p +12+3*stride)= val;
  438. #endif
  439. }else
  440. assert(0);
  441. assert(h==4);
  442. }
  443. static void fill_caches(H264Context *h, int mb_type, int for_deblock){
  444. MpegEncContext * const s = &h->s;
  445. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  446. int topleft_xy, top_xy, topright_xy, left_xy[2];
  447. int topleft_type, top_type, topright_type, left_type[2];
  448. int left_block[8];
  449. int i;
  450. //FIXME deblocking could skip the intra and nnz parts.
  451. if(for_deblock && (h->slice_num == 1 || h->slice_table[mb_xy] == h->slice_table[mb_xy-s->mb_stride]) && !FRAME_MBAFF)
  452. return;
  453. //wow what a mess, why didn't they simplify the interlacing&intra stuff, i can't imagine that these complex rules are worth it
  454. top_xy = mb_xy - s->mb_stride;
  455. topleft_xy = top_xy - 1;
  456. topright_xy= top_xy + 1;
  457. left_xy[1] = left_xy[0] = mb_xy-1;
  458. left_block[0]= 0;
  459. left_block[1]= 1;
  460. left_block[2]= 2;
  461. left_block[3]= 3;
  462. left_block[4]= 7;
  463. left_block[5]= 10;
  464. left_block[6]= 8;
  465. left_block[7]= 11;
  466. if(FRAME_MBAFF){
  467. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  468. const int top_pair_xy = pair_xy - s->mb_stride;
  469. const int topleft_pair_xy = top_pair_xy - 1;
  470. const int topright_pair_xy = top_pair_xy + 1;
  471. const int topleft_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
  472. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  473. const int topright_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
  474. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  475. const int curr_mb_frame_flag = !IS_INTERLACED(mb_type);
  476. const int bottom = (s->mb_y & 1);
  477. 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);
  478. if (bottom
  479. ? !curr_mb_frame_flag // bottom macroblock
  480. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  481. ) {
  482. top_xy -= s->mb_stride;
  483. }
  484. if (bottom
  485. ? !curr_mb_frame_flag // bottom macroblock
  486. : (!curr_mb_frame_flag && !topleft_mb_frame_flag) // top macroblock
  487. ) {
  488. topleft_xy -= s->mb_stride;
  489. }
  490. if (bottom
  491. ? !curr_mb_frame_flag // bottom macroblock
  492. : (!curr_mb_frame_flag && !topright_mb_frame_flag) // top macroblock
  493. ) {
  494. topright_xy -= s->mb_stride;
  495. }
  496. if (left_mb_frame_flag != curr_mb_frame_flag) {
  497. left_xy[1] = left_xy[0] = pair_xy - 1;
  498. if (curr_mb_frame_flag) {
  499. if (bottom) {
  500. left_block[0]= 2;
  501. left_block[1]= 2;
  502. left_block[2]= 3;
  503. left_block[3]= 3;
  504. left_block[4]= 8;
  505. left_block[5]= 11;
  506. left_block[6]= 8;
  507. left_block[7]= 11;
  508. } else {
  509. left_block[0]= 0;
  510. left_block[1]= 0;
  511. left_block[2]= 1;
  512. left_block[3]= 1;
  513. left_block[4]= 7;
  514. left_block[5]= 10;
  515. left_block[6]= 7;
  516. left_block[7]= 10;
  517. }
  518. } else {
  519. left_xy[1] += s->mb_stride;
  520. //left_block[0]= 0;
  521. left_block[1]= 2;
  522. left_block[2]= 0;
  523. left_block[3]= 2;
  524. //left_block[4]= 7;
  525. left_block[5]= 10;
  526. left_block[6]= 7;
  527. left_block[7]= 10;
  528. }
  529. }
  530. }
  531. h->top_mb_xy = top_xy;
  532. h->left_mb_xy[0] = left_xy[0];
  533. h->left_mb_xy[1] = left_xy[1];
  534. if(for_deblock){
  535. topleft_type = 0;
  536. topright_type = 0;
  537. top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0;
  538. left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
  539. left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
  540. if(FRAME_MBAFF && !IS_INTRA(mb_type)){
  541. int list;
  542. int v = *(uint16_t*)&h->non_zero_count[mb_xy][14];
  543. for(i=0; i<16; i++)
  544. h->non_zero_count_cache[scan8[i]] = (v>>i)&1;
  545. for(list=0; list<1+(h->slice_type==B_TYPE); list++){
  546. if(USES_LIST(mb_type,list)){
  547. uint32_t *src = (uint32_t*)s->current_picture.motion_val[list][h->mb2b_xy[mb_xy]];
  548. uint32_t *dst = (uint32_t*)h->mv_cache[list][scan8[0]];
  549. int8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
  550. for(i=0; i<4; i++, dst+=8, src+=h->b_stride){
  551. dst[0] = src[0];
  552. dst[1] = src[1];
  553. dst[2] = src[2];
  554. dst[3] = src[3];
  555. }
  556. *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =
  557. *(uint32_t*)&h->ref_cache[list][scan8[ 2]] = pack16to32(ref[0],ref[1])*0x0101;
  558. ref += h->b8_stride;
  559. *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =
  560. *(uint32_t*)&h->ref_cache[list][scan8[10]] = pack16to32(ref[0],ref[1])*0x0101;
  561. }else{
  562. fill_rectangle(&h-> mv_cache[list][scan8[ 0]], 4, 4, 8, 0, 4);
  563. fill_rectangle(&h->ref_cache[list][scan8[ 0]], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  564. }
  565. }
  566. }
  567. }else{
  568. topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
  569. top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
  570. topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
  571. left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
  572. left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
  573. }
  574. if(IS_INTRA(mb_type)){
  575. h->topleft_samples_available=
  576. h->top_samples_available=
  577. h->left_samples_available= 0xFFFF;
  578. h->topright_samples_available= 0xEEEA;
  579. if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
  580. h->topleft_samples_available= 0xB3FF;
  581. h->top_samples_available= 0x33FF;
  582. h->topright_samples_available= 0x26EA;
  583. }
  584. for(i=0; i<2; i++){
  585. if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
  586. h->topleft_samples_available&= 0xDF5F;
  587. h->left_samples_available&= 0x5F5F;
  588. }
  589. }
  590. if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
  591. h->topleft_samples_available&= 0x7FFF;
  592. if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
  593. h->topright_samples_available&= 0xFBFF;
  594. if(IS_INTRA4x4(mb_type)){
  595. if(IS_INTRA4x4(top_type)){
  596. h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
  597. h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
  598. h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
  599. h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
  600. }else{
  601. int pred;
  602. if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred))
  603. pred= -1;
  604. else{
  605. pred= 2;
  606. }
  607. h->intra4x4_pred_mode_cache[4+8*0]=
  608. h->intra4x4_pred_mode_cache[5+8*0]=
  609. h->intra4x4_pred_mode_cache[6+8*0]=
  610. h->intra4x4_pred_mode_cache[7+8*0]= pred;
  611. }
  612. for(i=0; i<2; i++){
  613. if(IS_INTRA4x4(left_type[i])){
  614. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
  615. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
  616. }else{
  617. int pred;
  618. if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred))
  619. pred= -1;
  620. else{
  621. pred= 2;
  622. }
  623. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
  624. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
  625. }
  626. }
  627. }
  628. }
  629. /*
  630. 0 . T T. T T T T
  631. 1 L . .L . . . .
  632. 2 L . .L . . . .
  633. 3 . T TL . . . .
  634. 4 L . .L . . . .
  635. 5 L . .. . . . .
  636. */
  637. //FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec)
  638. if(top_type){
  639. h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
  640. h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
  641. h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
  642. h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
  643. h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
  644. h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
  645. h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
  646. h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
  647. }else{
  648. h->non_zero_count_cache[4+8*0]=
  649. h->non_zero_count_cache[5+8*0]=
  650. h->non_zero_count_cache[6+8*0]=
  651. h->non_zero_count_cache[7+8*0]=
  652. h->non_zero_count_cache[1+8*0]=
  653. h->non_zero_count_cache[2+8*0]=
  654. h->non_zero_count_cache[1+8*3]=
  655. h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  656. }
  657. for (i=0; i<2; i++) {
  658. if(left_type[i]){
  659. h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
  660. h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
  661. h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
  662. h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
  663. }else{
  664. h->non_zero_count_cache[3+8*1 + 2*8*i]=
  665. h->non_zero_count_cache[3+8*2 + 2*8*i]=
  666. h->non_zero_count_cache[0+8*1 + 8*i]=
  667. h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  668. }
  669. }
  670. if( h->pps.cabac ) {
  671. // top_cbp
  672. if(top_type) {
  673. h->top_cbp = h->cbp_table[top_xy];
  674. } else if(IS_INTRA(mb_type)) {
  675. h->top_cbp = 0x1C0;
  676. } else {
  677. h->top_cbp = 0;
  678. }
  679. // left_cbp
  680. if (left_type[0]) {
  681. h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
  682. } else if(IS_INTRA(mb_type)) {
  683. h->left_cbp = 0x1C0;
  684. } else {
  685. h->left_cbp = 0;
  686. }
  687. if (left_type[0]) {
  688. h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
  689. }
  690. if (left_type[1]) {
  691. h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
  692. }
  693. }
  694. #if 1
  695. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  696. int list;
  697. for(list=0; list<1+(h->slice_type==B_TYPE); list++){
  698. if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
  699. /*if(!h->mv_cache_clean[list]){
  700. memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
  701. memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
  702. h->mv_cache_clean[list]= 1;
  703. }*/
  704. continue;
  705. }
  706. h->mv_cache_clean[list]= 0;
  707. if(USES_LIST(top_type, list)){
  708. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  709. const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
  710. *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
  711. *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
  712. *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
  713. *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
  714. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  715. h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
  716. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  717. h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
  718. }else{
  719. *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
  720. *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
  721. *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
  722. *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
  723. *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
  724. }
  725. //FIXME unify cleanup or sth
  726. if(USES_LIST(left_type[0], list)){
  727. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  728. const int b8_xy= h->mb2b8_xy[left_xy[0]] + 1;
  729. *(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]];
  730. *(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]];
  731. h->ref_cache[list][scan8[0] - 1 + 0*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0]>>1)];
  732. h->ref_cache[list][scan8[0] - 1 + 1*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1]>>1)];
  733. }else{
  734. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 0*8]=
  735. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 1*8]= 0;
  736. h->ref_cache[list][scan8[0] - 1 + 0*8]=
  737. h->ref_cache[list][scan8[0] - 1 + 1*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  738. }
  739. if(USES_LIST(left_type[1], list)){
  740. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  741. const int b8_xy= h->mb2b8_xy[left_xy[1]] + 1;
  742. *(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]];
  743. *(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]];
  744. h->ref_cache[list][scan8[0] - 1 + 2*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[2]>>1)];
  745. h->ref_cache[list][scan8[0] - 1 + 3*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[3]>>1)];
  746. }else{
  747. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 2*8]=
  748. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 3*8]= 0;
  749. h->ref_cache[list][scan8[0] - 1 + 2*8]=
  750. h->ref_cache[list][scan8[0] - 1 + 3*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  751. assert((!left_type[0]) == (!left_type[1]));
  752. }
  753. if((for_deblock || (IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred)) && !FRAME_MBAFF)
  754. continue;
  755. if(USES_LIST(topleft_type, list)){
  756. const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
  757. const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + h->b8_stride;
  758. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  759. h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  760. }else{
  761. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
  762. h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  763. }
  764. if(USES_LIST(topright_type, list)){
  765. const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
  766. const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
  767. *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  768. h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  769. }else{
  770. *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
  771. h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  772. }
  773. if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)
  774. continue;
  775. h->ref_cache[list][scan8[5 ]+1] =
  776. h->ref_cache[list][scan8[7 ]+1] =
  777. h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewhere else)
  778. h->ref_cache[list][scan8[4 ]] =
  779. h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
  780. *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
  781. *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
  782. *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  783. *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
  784. *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
  785. if( h->pps.cabac ) {
  786. /* XXX beurk, Load mvd */
  787. if(USES_LIST(top_type, list)){
  788. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  789. *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
  790. *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
  791. *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
  792. *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
  793. }else{
  794. *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
  795. *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
  796. *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
  797. *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
  798. }
  799. if(USES_LIST(left_type[0], list)){
  800. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  801. *(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]];
  802. *(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]];
  803. }else{
  804. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
  805. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
  806. }
  807. if(USES_LIST(left_type[1], list)){
  808. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  809. *(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]];
  810. *(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]];
  811. }else{
  812. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
  813. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
  814. }
  815. *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
  816. *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
  817. *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  818. *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
  819. *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
  820. if(h->slice_type == B_TYPE){
  821. fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
  822. if(IS_DIRECT(top_type)){
  823. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
  824. }else if(IS_8X8(top_type)){
  825. int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
  826. h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
  827. h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
  828. }else{
  829. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
  830. }
  831. if(IS_DIRECT(left_type[0]))
  832. h->direct_cache[scan8[0] - 1 + 0*8]= 1;
  833. else if(IS_8X8(left_type[0]))
  834. 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)];
  835. else
  836. h->direct_cache[scan8[0] - 1 + 0*8]= 0;
  837. if(IS_DIRECT(left_type[1]))
  838. h->direct_cache[scan8[0] - 1 + 2*8]= 1;
  839. else if(IS_8X8(left_type[1]))
  840. 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)];
  841. else
  842. h->direct_cache[scan8[0] - 1 + 2*8]= 0;
  843. }
  844. }
  845. if(FRAME_MBAFF){
  846. #define MAP_MVS\
  847. MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
  848. MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
  849. MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
  850. MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
  851. MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
  852. MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
  853. MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\
  854. MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\
  855. MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\
  856. MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
  857. if(MB_FIELD){
  858. #define MAP_F2F(idx, mb_type)\
  859. if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  860. h->ref_cache[list][idx] <<= 1;\
  861. h->mv_cache[list][idx][1] /= 2;\
  862. h->mvd_cache[list][idx][1] /= 2;\
  863. }
  864. MAP_MVS
  865. #undef MAP_F2F
  866. }else{
  867. #define MAP_F2F(idx, mb_type)\
  868. if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  869. h->ref_cache[list][idx] >>= 1;\
  870. h->mv_cache[list][idx][1] <<= 1;\
  871. h->mvd_cache[list][idx][1] <<= 1;\
  872. }
  873. MAP_MVS
  874. #undef MAP_F2F
  875. }
  876. }
  877. }
  878. }
  879. #endif
  880. h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
  881. }
  882. static inline void write_back_intra_pred_mode(H264Context *h){
  883. MpegEncContext * const s = &h->s;
  884. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  885. h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
  886. h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
  887. h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
  888. h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
  889. h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
  890. h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
  891. h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
  892. }
  893. /**
  894. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  895. */
  896. static inline int check_intra4x4_pred_mode(H264Context *h){
  897. MpegEncContext * const s = &h->s;
  898. static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
  899. static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
  900. int i;
  901. if(!(h->top_samples_available&0x8000)){
  902. for(i=0; i<4; i++){
  903. int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
  904. if(status<0){
  905. 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);
  906. return -1;
  907. } else if(status){
  908. h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
  909. }
  910. }
  911. }
  912. if(!(h->left_samples_available&0x8000)){
  913. for(i=0; i<4; i++){
  914. int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
  915. if(status<0){
  916. 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);
  917. return -1;
  918. } else if(status){
  919. h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
  920. }
  921. }
  922. }
  923. return 0;
  924. } //FIXME cleanup like next
  925. /**
  926. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  927. */
  928. static inline int check_intra_pred_mode(H264Context *h, int mode){
  929. MpegEncContext * const s = &h->s;
  930. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  931. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  932. if(mode > 6U) {
  933. 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);
  934. return -1;
  935. }
  936. if(!(h->top_samples_available&0x8000)){
  937. mode= top[ mode ];
  938. if(mode<0){
  939. 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);
  940. return -1;
  941. }
  942. }
  943. if(!(h->left_samples_available&0x8000)){
  944. mode= left[ mode ];
  945. if(mode<0){
  946. 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);
  947. return -1;
  948. }
  949. }
  950. return mode;
  951. }
  952. /**
  953. * gets the predicted intra4x4 prediction mode.
  954. */
  955. static inline int pred_intra_mode(H264Context *h, int n){
  956. const int index8= scan8[n];
  957. const int left= h->intra4x4_pred_mode_cache[index8 - 1];
  958. const int top = h->intra4x4_pred_mode_cache[index8 - 8];
  959. const int min= FFMIN(left, top);
  960. tprintf("mode:%d %d min:%d\n", left ,top, min);
  961. if(min<0) return DC_PRED;
  962. else return min;
  963. }
  964. static inline void write_back_non_zero_count(H264Context *h){
  965. MpegEncContext * const s = &h->s;
  966. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  967. h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
  968. h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
  969. h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
  970. h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
  971. h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
  972. h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
  973. h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
  974. h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
  975. h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
  976. h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
  977. h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
  978. h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
  979. h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
  980. if(FRAME_MBAFF){
  981. // store all luma nnzs, for deblocking
  982. int v = 0, i;
  983. for(i=0; i<16; i++)
  984. v += (!!h->non_zero_count_cache[scan8[i]]) << i;
  985. *(uint16_t*)&h->non_zero_count[mb_xy][14] = v;
  986. }
  987. }
  988. /**
  989. * gets the predicted number of non zero coefficients.
  990. * @param n block index
  991. */
  992. static inline int pred_non_zero_count(H264Context *h, int n){
  993. const int index8= scan8[n];
  994. const int left= h->non_zero_count_cache[index8 - 1];
  995. const int top = h->non_zero_count_cache[index8 - 8];
  996. int i= left + top;
  997. if(i<64) i= (i+1)>>1;
  998. tprintf("pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
  999. return i&31;
  1000. }
  1001. static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
  1002. const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
  1003. /* there is no consistent mapping of mvs to neighboring locations that will
  1004. * make mbaff happy, so we can't move all this logic to fill_caches */
  1005. if(FRAME_MBAFF){
  1006. MpegEncContext *s = &h->s;
  1007. const uint32_t *mb_types = s->current_picture_ptr->mb_type;
  1008. const int16_t *mv;
  1009. *(uint32_t*)h->mv_cache[list][scan8[0]-2] = 0;
  1010. *C = h->mv_cache[list][scan8[0]-2];
  1011. if(!MB_FIELD
  1012. && (s->mb_y&1) && i < scan8[0]+8 && topright_ref != PART_NOT_AVAILABLE){
  1013. int topright_xy = s->mb_x + (s->mb_y-1)*s->mb_stride + (i == scan8[0]+3);
  1014. if(IS_INTERLACED(mb_types[topright_xy])){
  1015. #define SET_DIAG_MV(MV_OP, REF_OP, X4, Y4)\
  1016. const int x4 = X4, y4 = Y4;\
  1017. const int mb_type = mb_types[(x4>>2)+(y4>>2)*s->mb_stride];\
  1018. if(!USES_LIST(mb_type,list) && !IS_8X8(mb_type))\
  1019. return LIST_NOT_USED;\
  1020. mv = s->current_picture_ptr->motion_val[list][x4 + y4*h->b_stride];\
  1021. h->mv_cache[list][scan8[0]-2][0] = mv[0];\
  1022. h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
  1023. return s->current_picture_ptr->ref_index[list][(x4>>1) + (y4>>1)*h->b8_stride] REF_OP;
  1024. SET_DIAG_MV(*2, >>1, s->mb_x*4+(i&7)-4+part_width, s->mb_y*4-1);
  1025. }
  1026. }
  1027. if(topright_ref == PART_NOT_AVAILABLE
  1028. && ((s->mb_y&1) || i >= scan8[0]+8) && (i&7)==4
  1029. && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
  1030. if(!MB_FIELD
  1031. && IS_INTERLACED(mb_types[h->left_mb_xy[0]])){
  1032. SET_DIAG_MV(*2, >>1, s->mb_x*4-1, (s->mb_y|1)*4+(s->mb_y&1)*2+(i>>4)-1);
  1033. }
  1034. if(MB_FIELD
  1035. && !IS_INTERLACED(mb_types[h->left_mb_xy[0]])
  1036. && i >= scan8[0]+8){
  1037. // leftshift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's ok.
  1038. SET_DIAG_MV(>>1, <<1, s->mb_x*4-1, (s->mb_y&~1)*4 - 1 + ((i-scan8[0])>>3)*2);
  1039. }
  1040. }
  1041. #undef SET_DIAG_MV
  1042. }
  1043. if(topright_ref != PART_NOT_AVAILABLE){
  1044. *C= h->mv_cache[list][ i - 8 + part_width ];
  1045. return topright_ref;
  1046. }else{
  1047. tprintf("topright MV not available\n");
  1048. *C= h->mv_cache[list][ i - 8 - 1 ];
  1049. return h->ref_cache[list][ i - 8 - 1 ];
  1050. }
  1051. }
  1052. /**
  1053. * gets the predicted MV.
  1054. * @param n the block index
  1055. * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
  1056. * @param mx the x component of the predicted motion vector
  1057. * @param my the y component of the predicted motion vector
  1058. */
  1059. static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
  1060. const int index8= scan8[n];
  1061. const int top_ref= h->ref_cache[list][ index8 - 8 ];
  1062. const int left_ref= h->ref_cache[list][ index8 - 1 ];
  1063. const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
  1064. const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
  1065. const int16_t * C;
  1066. int diagonal_ref, match_count;
  1067. assert(part_width==1 || part_width==2 || part_width==4);
  1068. /* mv_cache
  1069. B . . A T T T T
  1070. U . . L . . , .
  1071. U . . L . . . .
  1072. U . . L . . , .
  1073. . . . L . . . .
  1074. */
  1075. diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
  1076. match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
  1077. tprintf("pred_motion match_count=%d\n", match_count);
  1078. if(match_count > 1){ //most common
  1079. *mx= mid_pred(A[0], B[0], C[0]);
  1080. *my= mid_pred(A[1], B[1], C[1]);
  1081. }else if(match_count==1){
  1082. if(left_ref==ref){
  1083. *mx= A[0];
  1084. *my= A[1];
  1085. }else if(top_ref==ref){
  1086. *mx= B[0];
  1087. *my= B[1];
  1088. }else{
  1089. *mx= C[0];
  1090. *my= C[1];
  1091. }
  1092. }else{
  1093. if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
  1094. *mx= A[0];
  1095. *my= A[1];
  1096. }else{
  1097. *mx= mid_pred(A[0], B[0], C[0]);
  1098. *my= mid_pred(A[1], B[1], C[1]);
  1099. }
  1100. }
  1101. 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);
  1102. }
  1103. /**
  1104. * gets the directionally predicted 16x8 MV.
  1105. * @param n the block index
  1106. * @param mx the x component of the predicted motion vector
  1107. * @param my the y component of the predicted motion vector
  1108. */
  1109. static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  1110. if(n==0){
  1111. const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
  1112. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  1113. 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);
  1114. if(top_ref == ref){
  1115. *mx= B[0];
  1116. *my= B[1];
  1117. return;
  1118. }
  1119. }else{
  1120. const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
  1121. const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
  1122. 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);
  1123. if(left_ref == ref){
  1124. *mx= A[0];
  1125. *my= A[1];
  1126. return;
  1127. }
  1128. }
  1129. //RARE
  1130. pred_motion(h, n, 4, list, ref, mx, my);
  1131. }
  1132. /**
  1133. * gets the directionally predicted 8x16 MV.
  1134. * @param n the block index
  1135. * @param mx the x component of the predicted motion vector
  1136. * @param my the y component of the predicted motion vector
  1137. */
  1138. static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  1139. if(n==0){
  1140. const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
  1141. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  1142. 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);
  1143. if(left_ref == ref){
  1144. *mx= A[0];
  1145. *my= A[1];
  1146. return;
  1147. }
  1148. }else{
  1149. const int16_t * C;
  1150. int diagonal_ref;
  1151. diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
  1152. 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);
  1153. if(diagonal_ref == ref){
  1154. *mx= C[0];
  1155. *my= C[1];
  1156. return;
  1157. }
  1158. }
  1159. //RARE
  1160. pred_motion(h, n, 2, list, ref, mx, my);
  1161. }
  1162. static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
  1163. const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
  1164. const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
  1165. tprintf("pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
  1166. if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
  1167. || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
  1168. || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
  1169. *mx = *my = 0;
  1170. return;
  1171. }
  1172. pred_motion(h, 0, 4, 0, 0, mx, my);
  1173. return;
  1174. }
  1175. static inline void direct_dist_scale_factor(H264Context * const h){
  1176. const int poc = h->s.current_picture_ptr->poc;
  1177. const int poc1 = h->ref_list[1][0].poc;
  1178. int i;
  1179. for(i=0; i<h->ref_count[0]; i++){
  1180. int poc0 = h->ref_list[0][i].poc;
  1181. int td = clip(poc1 - poc0, -128, 127);
  1182. if(td == 0 /* FIXME || pic0 is a long-term ref */){
  1183. h->dist_scale_factor[i] = 256;
  1184. }else{
  1185. int tb = clip(poc - poc0, -128, 127);
  1186. int tx = (16384 + (FFABS(td) >> 1)) / td;
  1187. h->dist_scale_factor[i] = clip((tb*tx + 32) >> 6, -1024, 1023);
  1188. }
  1189. }
  1190. if(FRAME_MBAFF){
  1191. for(i=0; i<h->ref_count[0]; i++){
  1192. h->dist_scale_factor_field[2*i] =
  1193. h->dist_scale_factor_field[2*i+1] = h->dist_scale_factor[i];
  1194. }
  1195. }
  1196. }
  1197. static inline void direct_ref_list_init(H264Context * const h){
  1198. MpegEncContext * const s = &h->s;
  1199. Picture * const ref1 = &h->ref_list[1][0];
  1200. Picture * const cur = s->current_picture_ptr;
  1201. int list, i, j;
  1202. if(cur->pict_type == I_TYPE)
  1203. cur->ref_count[0] = 0;
  1204. if(cur->pict_type != B_TYPE)
  1205. cur->ref_count[1] = 0;
  1206. for(list=0; list<2; list++){
  1207. cur->ref_count[list] = h->ref_count[list];
  1208. for(j=0; j<h->ref_count[list]; j++)
  1209. cur->ref_poc[list][j] = h->ref_list[list][j].poc;
  1210. }
  1211. if(cur->pict_type != B_TYPE || h->direct_spatial_mv_pred)
  1212. return;
  1213. for(list=0; list<2; list++){
  1214. for(i=0; i<ref1->ref_count[list]; i++){
  1215. const int poc = ref1->ref_poc[list][i];
  1216. h->map_col_to_list0[list][i] = 0; /* bogus; fills in for missing frames */
  1217. for(j=0; j<h->ref_count[list]; j++)
  1218. if(h->ref_list[list][j].poc == poc){
  1219. h->map_col_to_list0[list][i] = j;
  1220. break;
  1221. }
  1222. }
  1223. }
  1224. if(FRAME_MBAFF){
  1225. for(list=0; list<2; list++){
  1226. for(i=0; i<ref1->ref_count[list]; i++){
  1227. j = h->map_col_to_list0[list][i];
  1228. h->map_col_to_list0_field[list][2*i] = 2*j;
  1229. h->map_col_to_list0_field[list][2*i+1] = 2*j+1;
  1230. }
  1231. }
  1232. }
  1233. }
  1234. static inline void pred_direct_motion(H264Context * const h, int *mb_type){
  1235. MpegEncContext * const s = &h->s;
  1236. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  1237. const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1238. const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1239. const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
  1240. const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
  1241. const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[1][b4_xy];
  1242. const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
  1243. const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy];
  1244. const int is_b8x8 = IS_8X8(*mb_type);
  1245. unsigned int sub_mb_type;
  1246. int i8, i4;
  1247. #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
  1248. if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
  1249. /* FIXME save sub mb types from previous frames (or derive from MVs)
  1250. * so we know exactly what block size to use */
  1251. sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  1252. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  1253. }else if(!is_b8x8 && (mb_type_col & MB_TYPE_16x16_OR_INTRA)){
  1254. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  1255. *mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
  1256. }else{
  1257. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  1258. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  1259. }
  1260. if(!is_b8x8)
  1261. *mb_type |= MB_TYPE_DIRECT2;
  1262. if(MB_FIELD)
  1263. *mb_type |= MB_TYPE_INTERLACED;
  1264. 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);
  1265. if(h->direct_spatial_mv_pred){
  1266. int ref[2];
  1267. int mv[2][2];
  1268. int list;
  1269. /* FIXME interlacing + spatial direct uses wrong colocated block positions */
  1270. /* ref = min(neighbors) */
  1271. for(list=0; list<2; list++){
  1272. int refa = h->ref_cache[list][scan8[0] - 1];
  1273. int refb = h->ref_cache[list][scan8[0] - 8];
  1274. int refc = h->ref_cache[list][scan8[0] - 8 + 4];
  1275. if(refc == -2)
  1276. refc = h->ref_cache[list][scan8[0] - 8 - 1];
  1277. ref[list] = refa;
  1278. if(ref[list] < 0 || (refb < ref[list] && refb >= 0))
  1279. ref[list] = refb;
  1280. if(ref[list] < 0 || (refc < ref[list] && refc >= 0))
  1281. ref[list] = refc;
  1282. if(ref[list] < 0)
  1283. ref[list] = -1;
  1284. }
  1285. if(ref[0] < 0 && ref[1] < 0){
  1286. ref[0] = ref[1] = 0;
  1287. mv[0][0] = mv[0][1] =
  1288. mv[1][0] = mv[1][1] = 0;
  1289. }else{
  1290. for(list=0; list<2; list++){
  1291. if(ref[list] >= 0)
  1292. pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
  1293. else
  1294. mv[list][0] = mv[list][1] = 0;
  1295. }
  1296. }
  1297. if(ref[1] < 0){
  1298. *mb_type &= ~MB_TYPE_P0L1;
  1299. sub_mb_type &= ~MB_TYPE_P0L1;
  1300. }else if(ref[0] < 0){
  1301. *mb_type &= ~MB_TYPE_P0L0;
  1302. sub_mb_type &= ~MB_TYPE_P0L0;
  1303. }
  1304. if(IS_16X16(*mb_type)){
  1305. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  1306. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  1307. if(!IS_INTRA(mb_type_col)
  1308. && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
  1309. || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
  1310. && (h->x264_build>33 || !h->x264_build)))){
  1311. if(ref[0] > 0)
  1312. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1313. else
  1314. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  1315. if(ref[1] > 0)
  1316. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1317. else
  1318. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  1319. }else{
  1320. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1321. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1322. }
  1323. }else{
  1324. for(i8=0; i8<4; i8++){
  1325. const int x8 = i8&1;
  1326. const int y8 = i8>>1;
  1327. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1328. continue;
  1329. h->sub_mb_type[i8] = sub_mb_type;
  1330. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1331. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1332. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  1333. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  1334. /* col_zero_flag */
  1335. if(!IS_INTRA(mb_type_col) && ( l1ref0[x8 + y8*h->b8_stride] == 0
  1336. || (l1ref0[x8 + y8*h->b8_stride] < 0 && l1ref1[x8 + y8*h->b8_stride] == 0
  1337. && (h->x264_build>33 || !h->x264_build)))){
  1338. const int16_t (*l1mv)[2]= l1ref0[x8 + y8*h->b8_stride] == 0 ? l1mv0 : l1mv1;
  1339. if(IS_SUB_8X8(sub_mb_type)){
  1340. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1341. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  1342. if(ref[0] == 0)
  1343. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1344. if(ref[1] == 0)
  1345. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1346. }
  1347. }else
  1348. for(i4=0; i4<4; i4++){
  1349. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1350. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  1351. if(ref[0] == 0)
  1352. *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
  1353. if(ref[1] == 0)
  1354. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
  1355. }
  1356. }
  1357. }
  1358. }
  1359. }
  1360. }else{ /* direct temporal mv pred */
  1361. const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
  1362. const int *dist_scale_factor = h->dist_scale_factor;
  1363. if(FRAME_MBAFF){
  1364. if(IS_INTERLACED(*mb_type)){
  1365. map_col_to_list0[0] = h->map_col_to_list0_field[0];
  1366. map_col_to_list0[1] = h->map_col_to_list0_field[1];
  1367. dist_scale_factor = h->dist_scale_factor_field;
  1368. }
  1369. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col)){
  1370. /* FIXME assumes direct_8x8_inference == 1 */
  1371. const int pair_xy = s->mb_x + (s->mb_y&~1)*s->mb_stride;
  1372. int mb_types_col[2];
  1373. int y_shift;
  1374. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1
  1375. | (is_b8x8 ? 0 : MB_TYPE_DIRECT2)
  1376. | (*mb_type & MB_TYPE_INTERLACED);
  1377. sub_mb_type = MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_16x16;
  1378. if(IS_INTERLACED(*mb_type)){
  1379. /* frame to field scaling */
  1380. mb_types_col[0] = h->ref_list[1][0].mb_type[pair_xy];
  1381. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1382. if(s->mb_y&1){
  1383. l1ref0 -= 2*h->b8_stride;
  1384. l1ref1 -= 2*h->b8_stride;
  1385. l1mv0 -= 4*h->b_stride;
  1386. l1mv1 -= 4*h->b_stride;
  1387. }
  1388. y_shift = 0;
  1389. if( (mb_types_col[0] & MB_TYPE_16x16_OR_INTRA)
  1390. && (mb_types_col[1] & MB_TYPE_16x16_OR_INTRA)
  1391. && !is_b8x8)
  1392. *mb_type |= MB_TYPE_16x8;
  1393. else
  1394. *mb_type |= MB_TYPE_8x8;
  1395. }else{
  1396. /* field to frame scaling */
  1397. /* col_mb_y = (mb_y&~1) + (topAbsDiffPOC < bottomAbsDiffPOC ? 0 : 1)
  1398. * but in MBAFF, top and bottom POC are equal */
  1399. int dy = (s->mb_y&1) ? 1 : 2;
  1400. mb_types_col[0] =
  1401. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1402. l1ref0 += dy*h->b8_stride;
  1403. l1ref1 += dy*h->b8_stride;
  1404. l1mv0 += 2*dy*h->b_stride;
  1405. l1mv1 += 2*dy*h->b_stride;
  1406. y_shift = 2;
  1407. if((mb_types_col[0] & (MB_TYPE_16x16_OR_INTRA|MB_TYPE_16x8))
  1408. && !is_b8x8)
  1409. *mb_type |= MB_TYPE_16x16;
  1410. else
  1411. *mb_type |= MB_TYPE_8x8;
  1412. }
  1413. for(i8=0; i8<4; i8++){
  1414. const int x8 = i8&1;
  1415. const int y8 = i8>>1;
  1416. int ref0, scale;
  1417. const int16_t (*l1mv)[2]= l1mv0;
  1418. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1419. continue;
  1420. h->sub_mb_type[i8] = sub_mb_type;
  1421. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1422. if(IS_INTRA(mb_types_col[y8])){
  1423. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1424. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1425. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1426. continue;
  1427. }
  1428. ref0 = l1ref0[x8 + (y8*2>>y_shift)*h->b8_stride];
  1429. if(ref0 >= 0)
  1430. ref0 = map_col_to_list0[0][ref0*2>>y_shift];
  1431. else{
  1432. ref0 = map_col_to_list0[1][l1ref1[x8 + (y8*2>>y_shift)*h->b8_stride]*2>>y_shift];
  1433. l1mv= l1mv1;
  1434. }
  1435. scale = dist_scale_factor[ref0];
  1436. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1437. {
  1438. const int16_t *mv_col = l1mv[x8*3 + (y8*6>>y_shift)*h->b_stride];
  1439. int my_col = (mv_col[1]<<y_shift)/2;
  1440. int mx = (scale * mv_col[0] + 128) >> 8;
  1441. int my = (scale * my_col + 128) >> 8;
  1442. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1443. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
  1444. }
  1445. }
  1446. return;
  1447. }
  1448. }
  1449. /* one-to-one mv scaling */
  1450. if(IS_16X16(*mb_type)){
  1451. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  1452. if(IS_INTRA(mb_type_col)){
  1453. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  1454. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  1455. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  1456. }else{
  1457. const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0]]
  1458. : map_col_to_list0[1][l1ref1[0]];
  1459. const int scale = dist_scale_factor[ref0];
  1460. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  1461. int mv_l0[2];
  1462. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1463. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1464. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref0, 1);
  1465. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0],mv_l0[1]), 4);
  1466. 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);
  1467. }
  1468. }else{
  1469. for(i8=0; i8<4; i8++){
  1470. const int x8 = i8&1;
  1471. const int y8 = i8>>1;
  1472. int ref0, scale;
  1473. const int16_t (*l1mv)[2]= l1mv0;
  1474. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1475. continue;
  1476. h->sub_mb_type[i8] = sub_mb_type;
  1477. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1478. if(IS_INTRA(mb_type_col)){
  1479. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1480. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1481. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1482. continue;
  1483. }
  1484. ref0 = l1ref0[x8 + y8*h->b8_stride];
  1485. if(ref0 >= 0)
  1486. ref0 = map_col_to_list0[0][ref0];
  1487. else{
  1488. ref0 = map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]];
  1489. l1mv= l1mv1;
  1490. }
  1491. scale = dist_scale_factor[ref0];
  1492. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1493. if(IS_SUB_8X8(sub_mb_type)){
  1494. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1495. int mx = (scale * mv_col[0] + 128) >> 8;
  1496. int my = (scale * mv_col[1] + 128) >> 8;
  1497. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1498. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
  1499. }else
  1500. for(i4=0; i4<4; i4++){
  1501. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1502. int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
  1503. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1504. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1505. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
  1506. pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  1507. }
  1508. }
  1509. }
  1510. }
  1511. }
  1512. static inline void write_back_motion(H264Context *h, int mb_type){
  1513. MpegEncContext * const s = &h->s;
  1514. const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1515. const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1516. int list;
  1517. if(!USES_LIST(mb_type, 0))
  1518. fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, h->b8_stride, (uint8_t)LIST_NOT_USED, 1);
  1519. for(list=0; list<2; list++){
  1520. int y;
  1521. if(!USES_LIST(mb_type, list))
  1522. continue;
  1523. for(y=0; y<4; y++){
  1524. *(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];
  1525. *(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];
  1526. }
  1527. if( h->pps.cabac ) {
  1528. if(IS_SKIP(mb_type))
  1529. fill_rectangle(h->mvd_table[list][b_xy], 4, 4, h->b_stride, 0, 4);
  1530. else
  1531. for(y=0; y<4; y++){
  1532. *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
  1533. *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
  1534. }
  1535. }
  1536. {
  1537. int8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
  1538. ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]];
  1539. ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]];
  1540. ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]];
  1541. ref_index[1+1*h->b8_stride]= h->ref_cache[list][scan8[12]];
  1542. }
  1543. }
  1544. if(h->slice_type == B_TYPE && h->pps.cabac){
  1545. if(IS_8X8(mb_type)){
  1546. uint8_t *direct_table = &h->direct_table[b8_xy];
  1547. direct_table[1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
  1548. direct_table[0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
  1549. direct_table[1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
  1550. }
  1551. }
  1552. }
  1553. /**
  1554. * Decodes a network abstraction layer unit.
  1555. * @param consumed is the number of bytes used as input
  1556. * @param length is the length of the array
  1557. * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp tailing?
  1558. * @returns decoded bytes, might be src+1 if no escapes
  1559. */
  1560. static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){
  1561. int i, si, di;
  1562. uint8_t *dst;
  1563. // src[0]&0x80; //forbidden bit
  1564. h->nal_ref_idc= src[0]>>5;
  1565. h->nal_unit_type= src[0]&0x1F;
  1566. src++; length--;
  1567. #if 0
  1568. for(i=0; i<length; i++)
  1569. printf("%2X ", src[i]);
  1570. #endif
  1571. for(i=0; i+1<length; i+=2){
  1572. if(src[i]) continue;
  1573. if(i>0 && src[i-1]==0) i--;
  1574. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  1575. if(src[i+2]!=3){
  1576. /* startcode, so we must be past the end */
  1577. length=i;
  1578. }
  1579. break;
  1580. }
  1581. }
  1582. if(i>=length-1){ //no escaped 0
  1583. *dst_length= length;
  1584. *consumed= length+1; //+1 for the header
  1585. return src;
  1586. }
  1587. h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length);
  1588. dst= h->rbsp_buffer;
  1589. if (dst == NULL){
  1590. return NULL;
  1591. }
  1592. //printf("decoding esc\n");
  1593. si=di=0;
  1594. while(si<length){
  1595. //remove escapes (very rare 1:2^22)
  1596. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  1597. if(src[si+2]==3){ //escape
  1598. dst[di++]= 0;
  1599. dst[di++]= 0;
  1600. si+=3;
  1601. continue;
  1602. }else //next start code
  1603. break;
  1604. }
  1605. dst[di++]= src[si++];
  1606. }
  1607. *dst_length= di;
  1608. *consumed= si + 1;//+1 for the header
  1609. //FIXME store exact number of bits in the getbitcontext (its needed for decoding)
  1610. return dst;
  1611. }
  1612. /**
  1613. * identifies the exact end of the bitstream
  1614. * @return the length of the trailing, or 0 if damaged
  1615. */
  1616. static int decode_rbsp_trailing(uint8_t *src){
  1617. int v= *src;
  1618. int r;
  1619. tprintf("rbsp trailing %X\n", v);
  1620. for(r=1; r<9; r++){
  1621. if(v&1) return r;
  1622. v>>=1;
  1623. }
  1624. return 0;
  1625. }
  1626. /**
  1627. * idct tranforms the 16 dc values and dequantize them.
  1628. * @param qp quantization parameter
  1629. */
  1630. static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1631. #define stride 16
  1632. int i;
  1633. int temp[16]; //FIXME check if this is a good idea
  1634. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1635. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1636. //memset(block, 64, 2*256);
  1637. //return;
  1638. for(i=0; i<4; i++){
  1639. const int offset= y_offset[i];
  1640. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1641. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1642. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1643. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1644. temp[4*i+0]= z0+z3;
  1645. temp[4*i+1]= z1+z2;
  1646. temp[4*i+2]= z1-z2;
  1647. temp[4*i+3]= z0-z3;
  1648. }
  1649. for(i=0; i<4; i++){
  1650. const int offset= x_offset[i];
  1651. const int z0= temp[4*0+i] + temp[4*2+i];
  1652. const int z1= temp[4*0+i] - temp[4*2+i];
  1653. const int z2= temp[4*1+i] - temp[4*3+i];
  1654. const int z3= temp[4*1+i] + temp[4*3+i];
  1655. block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_resdual
  1656. block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
  1657. block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
  1658. block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
  1659. }
  1660. }
  1661. #if 0
  1662. /**
  1663. * dct tranforms the 16 dc values.
  1664. * @param qp quantization parameter ??? FIXME
  1665. */
  1666. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  1667. // const int qmul= dequant_coeff[qp][0];
  1668. int i;
  1669. int temp[16]; //FIXME check if this is a good idea
  1670. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1671. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1672. for(i=0; i<4; i++){
  1673. const int offset= y_offset[i];
  1674. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1675. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1676. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1677. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1678. temp[4*i+0]= z0+z3;
  1679. temp[4*i+1]= z1+z2;
  1680. temp[4*i+2]= z1-z2;
  1681. temp[4*i+3]= z0-z3;
  1682. }
  1683. for(i=0; i<4; i++){
  1684. const int offset= x_offset[i];
  1685. const int z0= temp[4*0+i] + temp[4*2+i];
  1686. const int z1= temp[4*0+i] - temp[4*2+i];
  1687. const int z2= temp[4*1+i] - temp[4*3+i];
  1688. const int z3= temp[4*1+i] + temp[4*3+i];
  1689. block[stride*0 +offset]= (z0 + z3)>>1;
  1690. block[stride*2 +offset]= (z1 + z2)>>1;
  1691. block[stride*8 +offset]= (z1 - z2)>>1;
  1692. block[stride*10+offset]= (z0 - z3)>>1;
  1693. }
  1694. }
  1695. #endif
  1696. #undef xStride
  1697. #undef stride
  1698. static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1699. const int stride= 16*2;
  1700. const int xStride= 16;
  1701. int a,b,c,d,e;
  1702. a= block[stride*0 + xStride*0];
  1703. b= block[stride*0 + xStride*1];
  1704. c= block[stride*1 + xStride*0];
  1705. d= block[stride*1 + xStride*1];
  1706. e= a-b;
  1707. a= a+b;
  1708. b= c-d;
  1709. c= c+d;
  1710. block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
  1711. block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
  1712. block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
  1713. block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
  1714. }
  1715. #if 0
  1716. static void chroma_dc_dct_c(DCTELEM *block){
  1717. const int stride= 16*2;
  1718. const int xStride= 16;
  1719. int a,b,c,d,e;
  1720. a= block[stride*0 + xStride*0];
  1721. b= block[stride*0 + xStride*1];
  1722. c= block[stride*1 + xStride*0];
  1723. d= block[stride*1 + xStride*1];
  1724. e= a-b;
  1725. a= a+b;
  1726. b= c-d;
  1727. c= c+d;
  1728. block[stride*0 + xStride*0]= (a+c);
  1729. block[stride*0 + xStride*1]= (e+b);
  1730. block[stride*1 + xStride*0]= (a-c);
  1731. block[stride*1 + xStride*1]= (e-b);
  1732. }
  1733. #endif
  1734. /**
  1735. * gets the chroma qp.
  1736. */
  1737. static inline int get_chroma_qp(int chroma_qp_index_offset, int qscale){
  1738. return chroma_qp[clip(qscale + chroma_qp_index_offset, 0, 51)];
  1739. }
  1740. //FIXME need to check that this doesnt overflow signed 32 bit for low qp, i am not sure, it's very close
  1741. //FIXME check that gcc inlines this (and optimizes intra & seperate_dc stuff away)
  1742. static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int seperate_dc){
  1743. int i;
  1744. const int * const quant_table= quant_coeff[qscale];
  1745. const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
  1746. const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
  1747. const unsigned int threshold2= (threshold1<<1);
  1748. int last_non_zero;
  1749. if(seperate_dc){
  1750. if(qscale<=18){
  1751. //avoid overflows
  1752. const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
  1753. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
  1754. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1755. int level= block[0]*quant_coeff[qscale+18][0];
  1756. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1757. if(level>0){
  1758. level= (dc_bias + level)>>(QUANT_SHIFT-2);
  1759. block[0]= level;
  1760. }else{
  1761. level= (dc_bias - level)>>(QUANT_SHIFT-2);
  1762. block[0]= -level;
  1763. }
  1764. // last_non_zero = i;
  1765. }else{
  1766. block[0]=0;
  1767. }
  1768. }else{
  1769. const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
  1770. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
  1771. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1772. int level= block[0]*quant_table[0];
  1773. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1774. if(level>0){
  1775. level= (dc_bias + level)>>(QUANT_SHIFT+1);
  1776. block[0]= level;
  1777. }else{
  1778. level= (dc_bias - level)>>(QUANT_SHIFT+1);
  1779. block[0]= -level;
  1780. }
  1781. // last_non_zero = i;
  1782. }else{
  1783. block[0]=0;
  1784. }
  1785. }
  1786. last_non_zero= 0;
  1787. i=1;
  1788. }else{
  1789. last_non_zero= -1;
  1790. i=0;
  1791. }
  1792. for(; i<16; i++){
  1793. const int j= scantable[i];
  1794. int level= block[j]*quant_table[j];
  1795. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  1796. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  1797. if(((unsigned)(level+threshold1))>threshold2){
  1798. if(level>0){
  1799. level= (bias + level)>>QUANT_SHIFT;
  1800. block[j]= level;
  1801. }else{
  1802. level= (bias - level)>>QUANT_SHIFT;
  1803. block[j]= -level;
  1804. }
  1805. last_non_zero = i;
  1806. }else{
  1807. block[j]=0;
  1808. }
  1809. }
  1810. return last_non_zero;
  1811. }
  1812. static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){
  1813. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1814. ((uint32_t*)(src+0*stride))[0]= a;
  1815. ((uint32_t*)(src+1*stride))[0]= a;
  1816. ((uint32_t*)(src+2*stride))[0]= a;
  1817. ((uint32_t*)(src+3*stride))[0]= a;
  1818. }
  1819. static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){
  1820. ((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101;
  1821. ((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101;
  1822. ((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101;
  1823. ((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101;
  1824. }
  1825. static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1826. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
  1827. + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
  1828. ((uint32_t*)(src+0*stride))[0]=
  1829. ((uint32_t*)(src+1*stride))[0]=
  1830. ((uint32_t*)(src+2*stride))[0]=
  1831. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1832. }
  1833. static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1834. const int dc= ( src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
  1835. ((uint32_t*)(src+0*stride))[0]=
  1836. ((uint32_t*)(src+1*stride))[0]=
  1837. ((uint32_t*)(src+2*stride))[0]=
  1838. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1839. }
  1840. static void pred4x4_top_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1841. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2;
  1842. ((uint32_t*)(src+0*stride))[0]=
  1843. ((uint32_t*)(src+1*stride))[0]=
  1844. ((uint32_t*)(src+2*stride))[0]=
  1845. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1846. }
  1847. static void pred4x4_128_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1848. ((uint32_t*)(src+0*stride))[0]=
  1849. ((uint32_t*)(src+1*stride))[0]=
  1850. ((uint32_t*)(src+2*stride))[0]=
  1851. ((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U;
  1852. }
  1853. #define LOAD_TOP_RIGHT_EDGE\
  1854. const int t4= topright[0];\
  1855. const int t5= topright[1];\
  1856. const int t6= topright[2];\
  1857. const int t7= topright[3];\
  1858. #define LOAD_LEFT_EDGE\
  1859. const int l0= src[-1+0*stride];\
  1860. const int l1= src[-1+1*stride];\
  1861. const int l2= src[-1+2*stride];\
  1862. const int l3= src[-1+3*stride];\
  1863. #define LOAD_TOP_EDGE\
  1864. const int t0= src[ 0-1*stride];\
  1865. const int t1= src[ 1-1*stride];\
  1866. const int t2= src[ 2-1*stride];\
  1867. const int t3= src[ 3-1*stride];\
  1868. static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){
  1869. const int lt= src[-1-1*stride];
  1870. LOAD_TOP_EDGE
  1871. LOAD_LEFT_EDGE
  1872. src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
  1873. src[0+2*stride]=
  1874. src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
  1875. src[0+1*stride]=
  1876. src[1+2*stride]=
  1877. src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
  1878. src[0+0*stride]=
  1879. src[1+1*stride]=
  1880. src[2+2*stride]=
  1881. src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1882. src[1+0*stride]=
  1883. src[2+1*stride]=
  1884. src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1885. src[2+0*stride]=
  1886. src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1887. src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1888. }
  1889. static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){
  1890. LOAD_TOP_EDGE
  1891. LOAD_TOP_RIGHT_EDGE
  1892. // LOAD_LEFT_EDGE
  1893. src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
  1894. src[1+0*stride]=
  1895. src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
  1896. src[2+0*stride]=
  1897. src[1+1*stride]=
  1898. src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
  1899. src[3+0*stride]=
  1900. src[2+1*stride]=
  1901. src[1+2*stride]=
  1902. src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
  1903. src[3+1*stride]=
  1904. src[2+2*stride]=
  1905. src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
  1906. src[3+2*stride]=
  1907. src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
  1908. src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
  1909. }
  1910. static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
  1911. const int lt= src[-1-1*stride];
  1912. LOAD_TOP_EDGE
  1913. LOAD_LEFT_EDGE
  1914. const __attribute__((unused)) int unu= l3;
  1915. src[0+0*stride]=
  1916. src[1+2*stride]=(lt + t0 + 1)>>1;
  1917. src[1+0*stride]=
  1918. src[2+2*stride]=(t0 + t1 + 1)>>1;
  1919. src[2+0*stride]=
  1920. src[3+2*stride]=(t1 + t2 + 1)>>1;
  1921. src[3+0*stride]=(t2 + t3 + 1)>>1;
  1922. src[0+1*stride]=
  1923. src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1924. src[1+1*stride]=
  1925. src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1926. src[2+1*stride]=
  1927. src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1928. src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1929. src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1930. src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1931. }
  1932. static void pred4x4_vertical_left_c(uint8_t *src, uint8_t *topright, int stride){
  1933. LOAD_TOP_EDGE
  1934. LOAD_TOP_RIGHT_EDGE
  1935. const __attribute__((unused)) int unu= t7;
  1936. src[0+0*stride]=(t0 + t1 + 1)>>1;
  1937. src[1+0*stride]=
  1938. src[0+2*stride]=(t1 + t2 + 1)>>1;
  1939. src[2+0*stride]=
  1940. src[1+2*stride]=(t2 + t3 + 1)>>1;
  1941. src[3+0*stride]=
  1942. src[2+2*stride]=(t3 + t4+ 1)>>1;
  1943. src[3+2*stride]=(t4 + t5+ 1)>>1;
  1944. src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1945. src[1+1*stride]=
  1946. src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1947. src[2+1*stride]=
  1948. src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
  1949. src[3+1*stride]=
  1950. src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
  1951. src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
  1952. }
  1953. static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){
  1954. LOAD_LEFT_EDGE
  1955. src[0+0*stride]=(l0 + l1 + 1)>>1;
  1956. src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1957. src[2+0*stride]=
  1958. src[0+1*stride]=(l1 + l2 + 1)>>1;
  1959. src[3+0*stride]=
  1960. src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1961. src[2+1*stride]=
  1962. src[0+2*stride]=(l2 + l3 + 1)>>1;
  1963. src[3+1*stride]=
  1964. src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
  1965. src[3+2*stride]=
  1966. src[1+3*stride]=
  1967. src[0+3*stride]=
  1968. src[2+2*stride]=
  1969. src[2+3*stride]=
  1970. src[3+3*stride]=l3;
  1971. }
  1972. static void pred4x4_horizontal_down_c(uint8_t *src, uint8_t *topright, int stride){
  1973. const int lt= src[-1-1*stride];
  1974. LOAD_TOP_EDGE
  1975. LOAD_LEFT_EDGE
  1976. const __attribute__((unused)) int unu= t3;
  1977. src[0+0*stride]=
  1978. src[2+1*stride]=(lt + l0 + 1)>>1;
  1979. src[1+0*stride]=
  1980. src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1981. src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1982. src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1983. src[0+1*stride]=
  1984. src[2+2*stride]=(l0 + l1 + 1)>>1;
  1985. src[1+1*stride]=
  1986. src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1987. src[0+2*stride]=
  1988. src[2+3*stride]=(l1 + l2+ 1)>>1;
  1989. src[1+2*stride]=
  1990. src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1991. src[0+3*stride]=(l2 + l3 + 1)>>1;
  1992. src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1993. }
  1994. void ff_pred16x16_vertical_c(uint8_t *src, int stride){
  1995. int i;
  1996. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1997. const uint32_t b= ((uint32_t*)(src-stride))[1];
  1998. const uint32_t c= ((uint32_t*)(src-stride))[2];
  1999. const uint32_t d= ((uint32_t*)(src-stride))[3];
  2000. for(i=0; i<16; i++){
  2001. ((uint32_t*)(src+i*stride))[0]= a;
  2002. ((uint32_t*)(src+i*stride))[1]= b;
  2003. ((uint32_t*)(src+i*stride))[2]= c;
  2004. ((uint32_t*)(src+i*stride))[3]= d;
  2005. }
  2006. }
  2007. void ff_pred16x16_horizontal_c(uint8_t *src, int stride){
  2008. int i;
  2009. for(i=0; i<16; i++){
  2010. ((uint32_t*)(src+i*stride))[0]=
  2011. ((uint32_t*)(src+i*stride))[1]=
  2012. ((uint32_t*)(src+i*stride))[2]=
  2013. ((uint32_t*)(src+i*stride))[3]= src[-1+i*stride]*0x01010101;
  2014. }
  2015. }
  2016. void ff_pred16x16_dc_c(uint8_t *src, int stride){
  2017. int i, dc=0;
  2018. for(i=0;i<16; i++){
  2019. dc+= src[-1+i*stride];
  2020. }
  2021. for(i=0;i<16; i++){
  2022. dc+= src[i-stride];
  2023. }
  2024. dc= 0x01010101*((dc + 16)>>5);
  2025. for(i=0; i<16; i++){
  2026. ((uint32_t*)(src+i*stride))[0]=
  2027. ((uint32_t*)(src+i*stride))[1]=
  2028. ((uint32_t*)(src+i*stride))[2]=
  2029. ((uint32_t*)(src+i*stride))[3]= dc;
  2030. }
  2031. }
  2032. static void pred16x16_left_dc_c(uint8_t *src, int stride){
  2033. int i, dc=0;
  2034. for(i=0;i<16; i++){
  2035. dc+= src[-1+i*stride];
  2036. }
  2037. dc= 0x01010101*((dc + 8)>>4);
  2038. for(i=0; i<16; i++){
  2039. ((uint32_t*)(src+i*stride))[0]=
  2040. ((uint32_t*)(src+i*stride))[1]=
  2041. ((uint32_t*)(src+i*stride))[2]=
  2042. ((uint32_t*)(src+i*stride))[3]= dc;
  2043. }
  2044. }
  2045. static void pred16x16_top_dc_c(uint8_t *src, int stride){
  2046. int i, dc=0;
  2047. for(i=0;i<16; i++){
  2048. dc+= src[i-stride];
  2049. }
  2050. dc= 0x01010101*((dc + 8)>>4);
  2051. for(i=0; i<16; i++){
  2052. ((uint32_t*)(src+i*stride))[0]=
  2053. ((uint32_t*)(src+i*stride))[1]=
  2054. ((uint32_t*)(src+i*stride))[2]=
  2055. ((uint32_t*)(src+i*stride))[3]= dc;
  2056. }
  2057. }
  2058. void ff_pred16x16_128_dc_c(uint8_t *src, int stride){
  2059. int i;
  2060. for(i=0; i<16; i++){
  2061. ((uint32_t*)(src+i*stride))[0]=
  2062. ((uint32_t*)(src+i*stride))[1]=
  2063. ((uint32_t*)(src+i*stride))[2]=
  2064. ((uint32_t*)(src+i*stride))[3]= 0x01010101U*128U;
  2065. }
  2066. }
  2067. static inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){
  2068. int i, j, k;
  2069. int a;
  2070. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  2071. const uint8_t * const src0 = src+7-stride;
  2072. const uint8_t *src1 = src+8*stride-1;
  2073. const uint8_t *src2 = src1-2*stride; // == src+6*stride-1;
  2074. int H = src0[1] - src0[-1];
  2075. int V = src1[0] - src2[ 0];
  2076. for(k=2; k<=8; ++k) {
  2077. src1 += stride; src2 -= stride;
  2078. H += k*(src0[k] - src0[-k]);
  2079. V += k*(src1[0] - src2[ 0]);
  2080. }
  2081. if(svq3){
  2082. H = ( 5*(H/4) ) / 16;
  2083. V = ( 5*(V/4) ) / 16;
  2084. /* required for 100% accuracy */
  2085. i = H; H = V; V = i;
  2086. }else{
  2087. H = ( 5*H+32 ) >> 6;
  2088. V = ( 5*V+32 ) >> 6;
  2089. }
  2090. a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
  2091. for(j=16; j>0; --j) {
  2092. int b = a;
  2093. a += V;
  2094. for(i=-16; i<0; i+=4) {
  2095. src[16+i] = cm[ (b ) >> 5 ];
  2096. src[17+i] = cm[ (b+ H) >> 5 ];
  2097. src[18+i] = cm[ (b+2*H) >> 5 ];
  2098. src[19+i] = cm[ (b+3*H) >> 5 ];
  2099. b += 4*H;
  2100. }
  2101. src += stride;
  2102. }
  2103. }
  2104. void ff_pred16x16_plane_c(uint8_t *src, int stride){
  2105. pred16x16_plane_compat_c(src, stride, 0);
  2106. }
  2107. void ff_pred8x8_vertical_c(uint8_t *src, int stride){
  2108. int i;
  2109. const uint32_t a= ((uint32_t*)(src-stride))[0];
  2110. const uint32_t b= ((uint32_t*)(src-stride))[1];
  2111. for(i=0; i<8; i++){
  2112. ((uint32_t*)(src+i*stride))[0]= a;
  2113. ((uint32_t*)(src+i*stride))[1]= b;
  2114. }
  2115. }
  2116. void ff_pred8x8_horizontal_c(uint8_t *src, int stride){
  2117. int i;
  2118. for(i=0; i<8; i++){
  2119. ((uint32_t*)(src+i*stride))[0]=
  2120. ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101;
  2121. }
  2122. }
  2123. void ff_pred8x8_128_dc_c(uint8_t *src, int stride){
  2124. int i;
  2125. for(i=0; i<8; i++){
  2126. ((uint32_t*)(src+i*stride))[0]=
  2127. ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
  2128. }
  2129. }
  2130. static void pred8x8_left_dc_c(uint8_t *src, int stride){
  2131. int i;
  2132. int dc0, dc2;
  2133. dc0=dc2=0;
  2134. for(i=0;i<4; i++){
  2135. dc0+= src[-1+i*stride];
  2136. dc2+= src[-1+(i+4)*stride];
  2137. }
  2138. dc0= 0x01010101*((dc0 + 2)>>2);
  2139. dc2= 0x01010101*((dc2 + 2)>>2);
  2140. for(i=0; i<4; i++){
  2141. ((uint32_t*)(src+i*stride))[0]=
  2142. ((uint32_t*)(src+i*stride))[1]= dc0;
  2143. }
  2144. for(i=4; i<8; i++){
  2145. ((uint32_t*)(src+i*stride))[0]=
  2146. ((uint32_t*)(src+i*stride))[1]= dc2;
  2147. }
  2148. }
  2149. static void pred8x8_top_dc_c(uint8_t *src, int stride){
  2150. int i;
  2151. int dc0, dc1;
  2152. dc0=dc1=0;
  2153. for(i=0;i<4; i++){
  2154. dc0+= src[i-stride];
  2155. dc1+= src[4+i-stride];
  2156. }
  2157. dc0= 0x01010101*((dc0 + 2)>>2);
  2158. dc1= 0x01010101*((dc1 + 2)>>2);
  2159. for(i=0; i<4; i++){
  2160. ((uint32_t*)(src+i*stride))[0]= dc0;
  2161. ((uint32_t*)(src+i*stride))[1]= dc1;
  2162. }
  2163. for(i=4; i<8; i++){
  2164. ((uint32_t*)(src+i*stride))[0]= dc0;
  2165. ((uint32_t*)(src+i*stride))[1]= dc1;
  2166. }
  2167. }
  2168. void ff_pred8x8_dc_c(uint8_t *src, int stride){
  2169. int i;
  2170. int dc0, dc1, dc2, dc3;
  2171. dc0=dc1=dc2=0;
  2172. for(i=0;i<4; i++){
  2173. dc0+= src[-1+i*stride] + src[i-stride];
  2174. dc1+= src[4+i-stride];
  2175. dc2+= src[-1+(i+4)*stride];
  2176. }
  2177. dc3= 0x01010101*((dc1 + dc2 + 4)>>3);
  2178. dc0= 0x01010101*((dc0 + 4)>>3);
  2179. dc1= 0x01010101*((dc1 + 2)>>2);
  2180. dc2= 0x01010101*((dc2 + 2)>>2);
  2181. for(i=0; i<4; i++){
  2182. ((uint32_t*)(src+i*stride))[0]= dc0;
  2183. ((uint32_t*)(src+i*stride))[1]= dc1;
  2184. }
  2185. for(i=4; i<8; i++){
  2186. ((uint32_t*)(src+i*stride))[0]= dc2;
  2187. ((uint32_t*)(src+i*stride))[1]= dc3;
  2188. }
  2189. }
  2190. void ff_pred8x8_plane_c(uint8_t *src, int stride){
  2191. int j, k;
  2192. int a;
  2193. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  2194. const uint8_t * const src0 = src+3-stride;
  2195. const uint8_t *src1 = src+4*stride-1;
  2196. const uint8_t *src2 = src1-2*stride; // == src+2*stride-1;
  2197. int H = src0[1] - src0[-1];
  2198. int V = src1[0] - src2[ 0];
  2199. for(k=2; k<=4; ++k) {
  2200. src1 += stride; src2 -= stride;
  2201. H += k*(src0[k] - src0[-k]);
  2202. V += k*(src1[0] - src2[ 0]);
  2203. }
  2204. H = ( 17*H+16 ) >> 5;
  2205. V = ( 17*V+16 ) >> 5;
  2206. a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
  2207. for(j=8; j>0; --j) {
  2208. int b = a;
  2209. a += V;
  2210. src[0] = cm[ (b ) >> 5 ];
  2211. src[1] = cm[ (b+ H) >> 5 ];
  2212. src[2] = cm[ (b+2*H) >> 5 ];
  2213. src[3] = cm[ (b+3*H) >> 5 ];
  2214. src[4] = cm[ (b+4*H) >> 5 ];
  2215. src[5] = cm[ (b+5*H) >> 5 ];
  2216. src[6] = cm[ (b+6*H) >> 5 ];
  2217. src[7] = cm[ (b+7*H) >> 5 ];
  2218. src += stride;
  2219. }
  2220. }
  2221. #define SRC(x,y) src[(x)+(y)*stride]
  2222. #define PL(y) \
  2223. const int l##y = (SRC(-1,y-1) + 2*SRC(-1,y) + SRC(-1,y+1) + 2) >> 2;
  2224. #define PREDICT_8x8_LOAD_LEFT \
  2225. const int l0 = ((has_topleft ? SRC(-1,-1) : SRC(-1,0)) \
  2226. + 2*SRC(-1,0) + SRC(-1,1) + 2) >> 2; \
  2227. PL(1) PL(2) PL(3) PL(4) PL(5) PL(6) \
  2228. const int l7 attribute_unused = (SRC(-1,6) + 3*SRC(-1,7) + 2) >> 2
  2229. #define PT(x) \
  2230. const int t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  2231. #define PREDICT_8x8_LOAD_TOP \
  2232. const int t0 = ((has_topleft ? SRC(-1,-1) : SRC(0,-1)) \
  2233. + 2*SRC(0,-1) + SRC(1,-1) + 2) >> 2; \
  2234. PT(1) PT(2) PT(3) PT(4) PT(5) PT(6) \
  2235. const int t7 attribute_unused = ((has_topright ? SRC(8,-1) : SRC(7,-1)) \
  2236. + 2*SRC(7,-1) + SRC(6,-1) + 2) >> 2
  2237. #define PTR(x) \
  2238. t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  2239. #define PREDICT_8x8_LOAD_TOPRIGHT \
  2240. int t8, t9, t10, t11, t12, t13, t14, t15; \
  2241. if(has_topright) { \
  2242. PTR(8) PTR(9) PTR(10) PTR(11) PTR(12) PTR(13) PTR(14) \
  2243. t15 = (SRC(14,-1) + 3*SRC(15,-1) + 2) >> 2; \
  2244. } else t8=t9=t10=t11=t12=t13=t14=t15= SRC(7,-1);
  2245. #define PREDICT_8x8_LOAD_TOPLEFT \
  2246. const int lt = (SRC(-1,0) + 2*SRC(-1,-1) + SRC(0,-1) + 2) >> 2
  2247. #define PREDICT_8x8_DC(v) \
  2248. int y; \
  2249. for( y = 0; y < 8; y++ ) { \
  2250. ((uint32_t*)src)[0] = \
  2251. ((uint32_t*)src)[1] = v; \
  2252. src += stride; \
  2253. }
  2254. static void pred8x8l_128_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2255. {
  2256. PREDICT_8x8_DC(0x80808080);
  2257. }
  2258. static void pred8x8l_left_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2259. {
  2260. PREDICT_8x8_LOAD_LEFT;
  2261. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7+4) >> 3) * 0x01010101;
  2262. PREDICT_8x8_DC(dc);
  2263. }
  2264. static void pred8x8l_top_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2265. {
  2266. PREDICT_8x8_LOAD_TOP;
  2267. const uint32_t dc = ((t0+t1+t2+t3+t4+t5+t6+t7+4) >> 3) * 0x01010101;
  2268. PREDICT_8x8_DC(dc);
  2269. }
  2270. static void pred8x8l_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2271. {
  2272. PREDICT_8x8_LOAD_LEFT;
  2273. PREDICT_8x8_LOAD_TOP;
  2274. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7
  2275. +t0+t1+t2+t3+t4+t5+t6+t7+8) >> 4) * 0x01010101;
  2276. PREDICT_8x8_DC(dc);
  2277. }
  2278. static void pred8x8l_horizontal_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2279. {
  2280. PREDICT_8x8_LOAD_LEFT;
  2281. #define ROW(y) ((uint32_t*)(src+y*stride))[0] =\
  2282. ((uint32_t*)(src+y*stride))[1] = 0x01010101 * l##y
  2283. ROW(0); ROW(1); ROW(2); ROW(3); ROW(4); ROW(5); ROW(6); ROW(7);
  2284. #undef ROW
  2285. }
  2286. static void pred8x8l_vertical_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2287. {
  2288. int y;
  2289. PREDICT_8x8_LOAD_TOP;
  2290. src[0] = t0;
  2291. src[1] = t1;
  2292. src[2] = t2;
  2293. src[3] = t3;
  2294. src[4] = t4;
  2295. src[5] = t5;
  2296. src[6] = t6;
  2297. src[7] = t7;
  2298. for( y = 1; y < 8; y++ )
  2299. *(uint64_t*)(src+y*stride) = *(uint64_t*)src;
  2300. }
  2301. static void pred8x8l_down_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2302. {
  2303. PREDICT_8x8_LOAD_TOP;
  2304. PREDICT_8x8_LOAD_TOPRIGHT;
  2305. SRC(0,0)= (t0 + 2*t1 + t2 + 2) >> 2;
  2306. SRC(0,1)=SRC(1,0)= (t1 + 2*t2 + t3 + 2) >> 2;
  2307. SRC(0,2)=SRC(1,1)=SRC(2,0)= (t2 + 2*t3 + t4 + 2) >> 2;
  2308. SRC(0,3)=SRC(1,2)=SRC(2,1)=SRC(3,0)= (t3 + 2*t4 + t5 + 2) >> 2;
  2309. SRC(0,4)=SRC(1,3)=SRC(2,2)=SRC(3,1)=SRC(4,0)= (t4 + 2*t5 + t6 + 2) >> 2;
  2310. SRC(0,5)=SRC(1,4)=SRC(2,3)=SRC(3,2)=SRC(4,1)=SRC(5,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2311. 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;
  2312. 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;
  2313. 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;
  2314. SRC(2,7)=SRC(3,6)=SRC(4,5)=SRC(5,4)=SRC(6,3)=SRC(7,2)= (t9 + 2*t10 + t11 + 2) >> 2;
  2315. SRC(3,7)=SRC(4,6)=SRC(5,5)=SRC(6,4)=SRC(7,3)= (t10 + 2*t11 + t12 + 2) >> 2;
  2316. SRC(4,7)=SRC(5,6)=SRC(6,5)=SRC(7,4)= (t11 + 2*t12 + t13 + 2) >> 2;
  2317. SRC(5,7)=SRC(6,6)=SRC(7,5)= (t12 + 2*t13 + t14 + 2) >> 2;
  2318. SRC(6,7)=SRC(7,6)= (t13 + 2*t14 + t15 + 2) >> 2;
  2319. SRC(7,7)= (t14 + 3*t15 + 2) >> 2;
  2320. }
  2321. static void pred8x8l_down_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2322. {
  2323. PREDICT_8x8_LOAD_TOP;
  2324. PREDICT_8x8_LOAD_LEFT;
  2325. PREDICT_8x8_LOAD_TOPLEFT;
  2326. SRC(0,7)= (l7 + 2*l6 + l5 + 2) >> 2;
  2327. SRC(0,6)=SRC(1,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2328. SRC(0,5)=SRC(1,6)=SRC(2,7)= (l5 + 2*l4 + l3 + 2) >> 2;
  2329. SRC(0,4)=SRC(1,5)=SRC(2,6)=SRC(3,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2330. SRC(0,3)=SRC(1,4)=SRC(2,5)=SRC(3,6)=SRC(4,7)= (l3 + 2*l2 + l1 + 2) >> 2;
  2331. SRC(0,2)=SRC(1,3)=SRC(2,4)=SRC(3,5)=SRC(4,6)=SRC(5,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2332. 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;
  2333. 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;
  2334. 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;
  2335. SRC(2,0)=SRC(3,1)=SRC(4,2)=SRC(5,3)=SRC(6,4)=SRC(7,5)= (t0 + 2*t1 + t2 + 2) >> 2;
  2336. SRC(3,0)=SRC(4,1)=SRC(5,2)=SRC(6,3)=SRC(7,4)= (t1 + 2*t2 + t3 + 2) >> 2;
  2337. SRC(4,0)=SRC(5,1)=SRC(6,2)=SRC(7,3)= (t2 + 2*t3 + t4 + 2) >> 2;
  2338. SRC(5,0)=SRC(6,1)=SRC(7,2)= (t3 + 2*t4 + t5 + 2) >> 2;
  2339. SRC(6,0)=SRC(7,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2340. SRC(7,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2341. }
  2342. static void pred8x8l_vertical_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2343. {
  2344. PREDICT_8x8_LOAD_TOP;
  2345. PREDICT_8x8_LOAD_LEFT;
  2346. PREDICT_8x8_LOAD_TOPLEFT;
  2347. SRC(0,6)= (l5 + 2*l4 + l3 + 2) >> 2;
  2348. SRC(0,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2349. SRC(0,4)=SRC(1,6)= (l3 + 2*l2 + l1 + 2) >> 2;
  2350. SRC(0,5)=SRC(1,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2351. SRC(0,2)=SRC(1,4)=SRC(2,6)= (l1 + 2*l0 + lt + 2) >> 2;
  2352. SRC(0,3)=SRC(1,5)=SRC(2,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2353. SRC(0,1)=SRC(1,3)=SRC(2,5)=SRC(3,7)= (l0 + 2*lt + t0 + 2) >> 2;
  2354. SRC(0,0)=SRC(1,2)=SRC(2,4)=SRC(3,6)= (lt + t0 + 1) >> 1;
  2355. SRC(1,1)=SRC(2,3)=SRC(3,5)=SRC(4,7)= (lt + 2*t0 + t1 + 2) >> 2;
  2356. SRC(1,0)=SRC(2,2)=SRC(3,4)=SRC(4,6)= (t0 + t1 + 1) >> 1;
  2357. SRC(2,1)=SRC(3,3)=SRC(4,5)=SRC(5,7)= (t0 + 2*t1 + t2 + 2) >> 2;
  2358. SRC(2,0)=SRC(3,2)=SRC(4,4)=SRC(5,6)= (t1 + t2 + 1) >> 1;
  2359. SRC(3,1)=SRC(4,3)=SRC(5,5)=SRC(6,7)= (t1 + 2*t2 + t3 + 2) >> 2;
  2360. SRC(3,0)=SRC(4,2)=SRC(5,4)=SRC(6,6)= (t2 + t3 + 1) >> 1;
  2361. SRC(4,1)=SRC(5,3)=SRC(6,5)=SRC(7,7)= (t2 + 2*t3 + t4 + 2) >> 2;
  2362. SRC(4,0)=SRC(5,2)=SRC(6,4)=SRC(7,6)= (t3 + t4 + 1) >> 1;
  2363. SRC(5,1)=SRC(6,3)=SRC(7,5)= (t3 + 2*t4 + t5 + 2) >> 2;
  2364. SRC(5,0)=SRC(6,2)=SRC(7,4)= (t4 + t5 + 1) >> 1;
  2365. SRC(6,1)=SRC(7,3)= (t4 + 2*t5 + t6 + 2) >> 2;
  2366. SRC(6,0)=SRC(7,2)= (t5 + t6 + 1) >> 1;
  2367. SRC(7,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2368. SRC(7,0)= (t6 + t7 + 1) >> 1;
  2369. }
  2370. static void pred8x8l_horizontal_down_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2371. {
  2372. PREDICT_8x8_LOAD_TOP;
  2373. PREDICT_8x8_LOAD_LEFT;
  2374. PREDICT_8x8_LOAD_TOPLEFT;
  2375. SRC(0,7)= (l6 + l7 + 1) >> 1;
  2376. SRC(1,7)= (l5 + 2*l6 + l7 + 2) >> 2;
  2377. SRC(0,6)=SRC(2,7)= (l5 + l6 + 1) >> 1;
  2378. SRC(1,6)=SRC(3,7)= (l4 + 2*l5 + l6 + 2) >> 2;
  2379. SRC(0,5)=SRC(2,6)=SRC(4,7)= (l4 + l5 + 1) >> 1;
  2380. SRC(1,5)=SRC(3,6)=SRC(5,7)= (l3 + 2*l4 + l5 + 2) >> 2;
  2381. SRC(0,4)=SRC(2,5)=SRC(4,6)=SRC(6,7)= (l3 + l4 + 1) >> 1;
  2382. SRC(1,4)=SRC(3,5)=SRC(5,6)=SRC(7,7)= (l2 + 2*l3 + l4 + 2) >> 2;
  2383. SRC(0,3)=SRC(2,4)=SRC(4,5)=SRC(6,6)= (l2 + l3 + 1) >> 1;
  2384. SRC(1,3)=SRC(3,4)=SRC(5,5)=SRC(7,6)= (l1 + 2*l2 + l3 + 2) >> 2;
  2385. SRC(0,2)=SRC(2,3)=SRC(4,4)=SRC(6,5)= (l1 + l2 + 1) >> 1;
  2386. SRC(1,2)=SRC(3,3)=SRC(5,4)=SRC(7,5)= (l0 + 2*l1 + l2 + 2) >> 2;
  2387. SRC(0,1)=SRC(2,2)=SRC(4,3)=SRC(6,4)= (l0 + l1 + 1) >> 1;
  2388. SRC(1,1)=SRC(3,2)=SRC(5,3)=SRC(7,4)= (lt + 2*l0 + l1 + 2) >> 2;
  2389. SRC(0,0)=SRC(2,1)=SRC(4,2)=SRC(6,3)= (lt + l0 + 1) >> 1;
  2390. SRC(1,0)=SRC(3,1)=SRC(5,2)=SRC(7,3)= (l0 + 2*lt + t0 + 2) >> 2;
  2391. SRC(2,0)=SRC(4,1)=SRC(6,2)= (t1 + 2*t0 + lt + 2) >> 2;
  2392. SRC(3,0)=SRC(5,1)=SRC(7,2)= (t2 + 2*t1 + t0 + 2) >> 2;
  2393. SRC(4,0)=SRC(6,1)= (t3 + 2*t2 + t1 + 2) >> 2;
  2394. SRC(5,0)=SRC(7,1)= (t4 + 2*t3 + t2 + 2) >> 2;
  2395. SRC(6,0)= (t5 + 2*t4 + t3 + 2) >> 2;
  2396. SRC(7,0)= (t6 + 2*t5 + t4 + 2) >> 2;
  2397. }
  2398. static void pred8x8l_vertical_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2399. {
  2400. PREDICT_8x8_LOAD_TOP;
  2401. PREDICT_8x8_LOAD_TOPRIGHT;
  2402. SRC(0,0)= (t0 + t1 + 1) >> 1;
  2403. SRC(0,1)= (t0 + 2*t1 + t2 + 2) >> 2;
  2404. SRC(0,2)=SRC(1,0)= (t1 + t2 + 1) >> 1;
  2405. SRC(0,3)=SRC(1,1)= (t1 + 2*t2 + t3 + 2) >> 2;
  2406. SRC(0,4)=SRC(1,2)=SRC(2,0)= (t2 + t3 + 1) >> 1;
  2407. SRC(0,5)=SRC(1,3)=SRC(2,1)= (t2 + 2*t3 + t4 + 2) >> 2;
  2408. SRC(0,6)=SRC(1,4)=SRC(2,2)=SRC(3,0)= (t3 + t4 + 1) >> 1;
  2409. SRC(0,7)=SRC(1,5)=SRC(2,3)=SRC(3,1)= (t3 + 2*t4 + t5 + 2) >> 2;
  2410. SRC(1,6)=SRC(2,4)=SRC(3,2)=SRC(4,0)= (t4 + t5 + 1) >> 1;
  2411. SRC(1,7)=SRC(2,5)=SRC(3,3)=SRC(4,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2412. SRC(2,6)=SRC(3,4)=SRC(4,2)=SRC(5,0)= (t5 + t6 + 1) >> 1;
  2413. SRC(2,7)=SRC(3,5)=SRC(4,3)=SRC(5,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2414. SRC(3,6)=SRC(4,4)=SRC(5,2)=SRC(6,0)= (t6 + t7 + 1) >> 1;
  2415. SRC(3,7)=SRC(4,5)=SRC(5,3)=SRC(6,1)= (t6 + 2*t7 + t8 + 2) >> 2;
  2416. SRC(4,6)=SRC(5,4)=SRC(6,2)=SRC(7,0)= (t7 + t8 + 1) >> 1;
  2417. SRC(4,7)=SRC(5,5)=SRC(6,3)=SRC(7,1)= (t7 + 2*t8 + t9 + 2) >> 2;
  2418. SRC(5,6)=SRC(6,4)=SRC(7,2)= (t8 + t9 + 1) >> 1;
  2419. SRC(5,7)=SRC(6,5)=SRC(7,3)= (t8 + 2*t9 + t10 + 2) >> 2;
  2420. SRC(6,6)=SRC(7,4)= (t9 + t10 + 1) >> 1;
  2421. SRC(6,7)=SRC(7,5)= (t9 + 2*t10 + t11 + 2) >> 2;
  2422. SRC(7,6)= (t10 + t11 + 1) >> 1;
  2423. SRC(7,7)= (t10 + 2*t11 + t12 + 2) >> 2;
  2424. }
  2425. static void pred8x8l_horizontal_up_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2426. {
  2427. PREDICT_8x8_LOAD_LEFT;
  2428. SRC(0,0)= (l0 + l1 + 1) >> 1;
  2429. SRC(1,0)= (l0 + 2*l1 + l2 + 2) >> 2;
  2430. SRC(0,1)=SRC(2,0)= (l1 + l2 + 1) >> 1;
  2431. SRC(1,1)=SRC(3,0)= (l1 + 2*l2 + l3 + 2) >> 2;
  2432. SRC(0,2)=SRC(2,1)=SRC(4,0)= (l2 + l3 + 1) >> 1;
  2433. SRC(1,2)=SRC(3,1)=SRC(5,0)= (l2 + 2*l3 + l4 + 2) >> 2;
  2434. SRC(0,3)=SRC(2,2)=SRC(4,1)=SRC(6,0)= (l3 + l4 + 1) >> 1;
  2435. SRC(1,3)=SRC(3,2)=SRC(5,1)=SRC(7,0)= (l3 + 2*l4 + l5 + 2) >> 2;
  2436. SRC(0,4)=SRC(2,3)=SRC(4,2)=SRC(6,1)= (l4 + l5 + 1) >> 1;
  2437. SRC(1,4)=SRC(3,3)=SRC(5,2)=SRC(7,1)= (l4 + 2*l5 + l6 + 2) >> 2;
  2438. SRC(0,5)=SRC(2,4)=SRC(4,3)=SRC(6,2)= (l5 + l6 + 1) >> 1;
  2439. SRC(1,5)=SRC(3,4)=SRC(5,3)=SRC(7,2)= (l5 + 2*l6 + l7 + 2) >> 2;
  2440. SRC(0,6)=SRC(2,5)=SRC(4,4)=SRC(6,3)= (l6 + l7 + 1) >> 1;
  2441. SRC(1,6)=SRC(3,5)=SRC(5,4)=SRC(7,3)= (l6 + 3*l7 + 2) >> 2;
  2442. SRC(0,7)=SRC(1,7)=SRC(2,6)=SRC(2,7)=SRC(3,6)=
  2443. SRC(3,7)=SRC(4,5)=SRC(4,6)=SRC(4,7)=SRC(5,5)=
  2444. SRC(5,6)=SRC(5,7)=SRC(6,4)=SRC(6,5)=SRC(6,6)=
  2445. SRC(6,7)=SRC(7,4)=SRC(7,5)=SRC(7,6)=SRC(7,7)= l7;
  2446. }
  2447. #undef PREDICT_8x8_LOAD_LEFT
  2448. #undef PREDICT_8x8_LOAD_TOP
  2449. #undef PREDICT_8x8_LOAD_TOPLEFT
  2450. #undef PREDICT_8x8_LOAD_TOPRIGHT
  2451. #undef PREDICT_8x8_DC
  2452. #undef PTR
  2453. #undef PT
  2454. #undef PL
  2455. #undef SRC
  2456. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  2457. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2458. int src_x_offset, int src_y_offset,
  2459. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  2460. MpegEncContext * const s = &h->s;
  2461. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  2462. int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  2463. const int luma_xy= (mx&3) + ((my&3)<<2);
  2464. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
  2465. uint8_t * src_cb, * src_cr;
  2466. int extra_width= h->emu_edge_width;
  2467. int extra_height= h->emu_edge_height;
  2468. int emu=0;
  2469. const int full_mx= mx>>2;
  2470. const int full_my= my>>2;
  2471. const int pic_width = 16*s->mb_width;
  2472. const int pic_height = 16*s->mb_height >> MB_MBAFF;
  2473. if(!pic->data[0]) //FIXME this is unacceptable, some senseable error concealment must be done for missing reference frames
  2474. return;
  2475. if(mx&7) extra_width -= 3;
  2476. if(my&7) extra_height -= 3;
  2477. if( full_mx < 0-extra_width
  2478. || full_my < 0-extra_height
  2479. || full_mx + 16/*FIXME*/ > pic_width + extra_width
  2480. || full_my + 16/*FIXME*/ > pic_height + extra_height){
  2481. 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);
  2482. src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
  2483. emu=1;
  2484. }
  2485. qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
  2486. if(!square){
  2487. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
  2488. }
  2489. if(s->flags&CODEC_FLAG_GRAY) return;
  2490. if(MB_MBAFF){
  2491. // chroma offset when predicting from a field of opposite parity
  2492. my += 2 * ((s->mb_y & 1) - (h->ref_cache[list][scan8[n]] & 1));
  2493. emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
  2494. }
  2495. src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2496. src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2497. if(emu){
  2498. 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);
  2499. src_cb= s->edge_emu_buffer;
  2500. }
  2501. chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2502. if(emu){
  2503. 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);
  2504. src_cr= s->edge_emu_buffer;
  2505. }
  2506. chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2507. }
  2508. static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
  2509. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2510. int x_offset, int y_offset,
  2511. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2512. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2513. int list0, int list1){
  2514. MpegEncContext * const s = &h->s;
  2515. qpel_mc_func *qpix_op= qpix_put;
  2516. h264_chroma_mc_func chroma_op= chroma_put;
  2517. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2518. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2519. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2520. x_offset += 8*s->mb_x;
  2521. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2522. if(list0){
  2523. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  2524. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  2525. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2526. qpix_op, chroma_op);
  2527. qpix_op= qpix_avg;
  2528. chroma_op= chroma_avg;
  2529. }
  2530. if(list1){
  2531. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  2532. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  2533. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2534. qpix_op, chroma_op);
  2535. }
  2536. }
  2537. static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
  2538. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2539. int x_offset, int y_offset,
  2540. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2541. h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
  2542. h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
  2543. int list0, int list1){
  2544. MpegEncContext * const s = &h->s;
  2545. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2546. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2547. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2548. x_offset += 8*s->mb_x;
  2549. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2550. if(list0 && list1){
  2551. /* don't optimize for luma-only case, since B-frames usually
  2552. * use implicit weights => chroma too. */
  2553. uint8_t *tmp_cb = s->obmc_scratchpad;
  2554. uint8_t *tmp_cr = s->obmc_scratchpad + 8;
  2555. uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
  2556. int refn0 = h->ref_cache[0][ scan8[n] ];
  2557. int refn1 = h->ref_cache[1][ scan8[n] ];
  2558. mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
  2559. dest_y, dest_cb, dest_cr,
  2560. x_offset, y_offset, qpix_put, chroma_put);
  2561. mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
  2562. tmp_y, tmp_cb, tmp_cr,
  2563. x_offset, y_offset, qpix_put, chroma_put);
  2564. if(h->use_weight == 2){
  2565. int weight0 = h->implicit_weight[refn0][refn1];
  2566. int weight1 = 64 - weight0;
  2567. luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
  2568. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2569. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2570. }else{
  2571. luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
  2572. h->luma_weight[0][refn0], h->luma_weight[1][refn1],
  2573. h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
  2574. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2575. h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
  2576. h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
  2577. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2578. h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
  2579. h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
  2580. }
  2581. }else{
  2582. int list = list1 ? 1 : 0;
  2583. int refn = h->ref_cache[list][ scan8[n] ];
  2584. Picture *ref= &h->ref_list[list][refn];
  2585. mc_dir_part(h, ref, n, square, chroma_height, delta, list,
  2586. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2587. qpix_put, chroma_put);
  2588. luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
  2589. h->luma_weight[list][refn], h->luma_offset[list][refn]);
  2590. if(h->use_weight_chroma){
  2591. chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2592. h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
  2593. chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2594. h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
  2595. }
  2596. }
  2597. }
  2598. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  2599. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2600. int x_offset, int y_offset,
  2601. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2602. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2603. h264_weight_func *weight_op, h264_biweight_func *weight_avg,
  2604. int list0, int list1){
  2605. if((h->use_weight==2 && list0 && list1
  2606. && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
  2607. || h->use_weight==1)
  2608. mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2609. x_offset, y_offset, qpix_put, chroma_put,
  2610. weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
  2611. else
  2612. mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2613. x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
  2614. }
  2615. static inline void prefetch_motion(H264Context *h, int list){
  2616. /* fetch pixels for estimated mv 4 macroblocks ahead
  2617. * optimized for 64byte cache lines */
  2618. MpegEncContext * const s = &h->s;
  2619. const int refn = h->ref_cache[list][scan8[0]];
  2620. if(refn >= 0){
  2621. const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
  2622. const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
  2623. uint8_t **src= h->ref_list[list][refn].data;
  2624. int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
  2625. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  2626. off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
  2627. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  2628. }
  2629. }
  2630. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2631. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  2632. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
  2633. h264_weight_func *weight_op, h264_biweight_func *weight_avg){
  2634. MpegEncContext * const s = &h->s;
  2635. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2636. const int mb_type= s->current_picture.mb_type[mb_xy];
  2637. assert(IS_INTER(mb_type));
  2638. prefetch_motion(h, 0);
  2639. if(IS_16X16(mb_type)){
  2640. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  2641. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  2642. &weight_op[0], &weight_avg[0],
  2643. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2644. }else if(IS_16X8(mb_type)){
  2645. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  2646. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2647. &weight_op[1], &weight_avg[1],
  2648. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2649. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  2650. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2651. &weight_op[1], &weight_avg[1],
  2652. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2653. }else if(IS_8X16(mb_type)){
  2654. mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
  2655. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2656. &weight_op[2], &weight_avg[2],
  2657. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2658. mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
  2659. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2660. &weight_op[2], &weight_avg[2],
  2661. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2662. }else{
  2663. int i;
  2664. assert(IS_8X8(mb_type));
  2665. for(i=0; i<4; i++){
  2666. const int sub_mb_type= h->sub_mb_type[i];
  2667. const int n= 4*i;
  2668. int x_offset= (i&1)<<2;
  2669. int y_offset= (i&2)<<1;
  2670. if(IS_SUB_8X8(sub_mb_type)){
  2671. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2672. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2673. &weight_op[3], &weight_avg[3],
  2674. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2675. }else if(IS_SUB_8X4(sub_mb_type)){
  2676. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2677. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2678. &weight_op[4], &weight_avg[4],
  2679. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2680. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  2681. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2682. &weight_op[4], &weight_avg[4],
  2683. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2684. }else if(IS_SUB_4X8(sub_mb_type)){
  2685. mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2686. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2687. &weight_op[5], &weight_avg[5],
  2688. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2689. mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
  2690. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2691. &weight_op[5], &weight_avg[5],
  2692. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2693. }else{
  2694. int j;
  2695. assert(IS_SUB_4X4(sub_mb_type));
  2696. for(j=0; j<4; j++){
  2697. int sub_x_offset= x_offset + 2*(j&1);
  2698. int sub_y_offset= y_offset + (j&2);
  2699. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  2700. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2701. &weight_op[6], &weight_avg[6],
  2702. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2703. }
  2704. }
  2705. }
  2706. }
  2707. prefetch_motion(h, 1);
  2708. }
  2709. static void decode_init_vlc(){
  2710. static int done = 0;
  2711. if (!done) {
  2712. int i;
  2713. done = 1;
  2714. init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
  2715. &chroma_dc_coeff_token_len [0], 1, 1,
  2716. &chroma_dc_coeff_token_bits[0], 1, 1, 1);
  2717. for(i=0; i<4; i++){
  2718. init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
  2719. &coeff_token_len [i][0], 1, 1,
  2720. &coeff_token_bits[i][0], 1, 1, 1);
  2721. }
  2722. for(i=0; i<3; i++){
  2723. init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
  2724. &chroma_dc_total_zeros_len [i][0], 1, 1,
  2725. &chroma_dc_total_zeros_bits[i][0], 1, 1, 1);
  2726. }
  2727. for(i=0; i<15; i++){
  2728. init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
  2729. &total_zeros_len [i][0], 1, 1,
  2730. &total_zeros_bits[i][0], 1, 1, 1);
  2731. }
  2732. for(i=0; i<6; i++){
  2733. init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
  2734. &run_len [i][0], 1, 1,
  2735. &run_bits[i][0], 1, 1, 1);
  2736. }
  2737. init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
  2738. &run_len [6][0], 1, 1,
  2739. &run_bits[6][0], 1, 1, 1);
  2740. }
  2741. }
  2742. /**
  2743. * Sets the intra prediction function pointers.
  2744. */
  2745. static void init_pred_ptrs(H264Context *h){
  2746. // MpegEncContext * const s = &h->s;
  2747. h->pred4x4[VERT_PRED ]= pred4x4_vertical_c;
  2748. h->pred4x4[HOR_PRED ]= pred4x4_horizontal_c;
  2749. h->pred4x4[DC_PRED ]= pred4x4_dc_c;
  2750. h->pred4x4[DIAG_DOWN_LEFT_PRED ]= pred4x4_down_left_c;
  2751. h->pred4x4[DIAG_DOWN_RIGHT_PRED]= pred4x4_down_right_c;
  2752. h->pred4x4[VERT_RIGHT_PRED ]= pred4x4_vertical_right_c;
  2753. h->pred4x4[HOR_DOWN_PRED ]= pred4x4_horizontal_down_c;
  2754. h->pred4x4[VERT_LEFT_PRED ]= pred4x4_vertical_left_c;
  2755. h->pred4x4[HOR_UP_PRED ]= pred4x4_horizontal_up_c;
  2756. h->pred4x4[LEFT_DC_PRED ]= pred4x4_left_dc_c;
  2757. h->pred4x4[TOP_DC_PRED ]= pred4x4_top_dc_c;
  2758. h->pred4x4[DC_128_PRED ]= pred4x4_128_dc_c;
  2759. h->pred8x8l[VERT_PRED ]= pred8x8l_vertical_c;
  2760. h->pred8x8l[HOR_PRED ]= pred8x8l_horizontal_c;
  2761. h->pred8x8l[DC_PRED ]= pred8x8l_dc_c;
  2762. h->pred8x8l[DIAG_DOWN_LEFT_PRED ]= pred8x8l_down_left_c;
  2763. h->pred8x8l[DIAG_DOWN_RIGHT_PRED]= pred8x8l_down_right_c;
  2764. h->pred8x8l[VERT_RIGHT_PRED ]= pred8x8l_vertical_right_c;
  2765. h->pred8x8l[HOR_DOWN_PRED ]= pred8x8l_horizontal_down_c;
  2766. h->pred8x8l[VERT_LEFT_PRED ]= pred8x8l_vertical_left_c;
  2767. h->pred8x8l[HOR_UP_PRED ]= pred8x8l_horizontal_up_c;
  2768. h->pred8x8l[LEFT_DC_PRED ]= pred8x8l_left_dc_c;
  2769. h->pred8x8l[TOP_DC_PRED ]= pred8x8l_top_dc_c;
  2770. h->pred8x8l[DC_128_PRED ]= pred8x8l_128_dc_c;
  2771. h->pred8x8[DC_PRED8x8 ]= ff_pred8x8_dc_c;
  2772. h->pred8x8[VERT_PRED8x8 ]= ff_pred8x8_vertical_c;
  2773. h->pred8x8[HOR_PRED8x8 ]= ff_pred8x8_horizontal_c;
  2774. h->pred8x8[PLANE_PRED8x8 ]= ff_pred8x8_plane_c;
  2775. h->pred8x8[LEFT_DC_PRED8x8]= pred8x8_left_dc_c;
  2776. h->pred8x8[TOP_DC_PRED8x8 ]= pred8x8_top_dc_c;
  2777. h->pred8x8[DC_128_PRED8x8 ]= ff_pred8x8_128_dc_c;
  2778. h->pred16x16[DC_PRED8x8 ]= ff_pred16x16_dc_c;
  2779. h->pred16x16[VERT_PRED8x8 ]= ff_pred16x16_vertical_c;
  2780. h->pred16x16[HOR_PRED8x8 ]= ff_pred16x16_horizontal_c;
  2781. h->pred16x16[PLANE_PRED8x8 ]= ff_pred16x16_plane_c;
  2782. h->pred16x16[LEFT_DC_PRED8x8]= pred16x16_left_dc_c;
  2783. h->pred16x16[TOP_DC_PRED8x8 ]= pred16x16_top_dc_c;
  2784. h->pred16x16[DC_128_PRED8x8 ]= ff_pred16x16_128_dc_c;
  2785. }
  2786. static void free_tables(H264Context *h){
  2787. av_freep(&h->intra4x4_pred_mode);
  2788. av_freep(&h->chroma_pred_mode_table);
  2789. av_freep(&h->cbp_table);
  2790. av_freep(&h->mvd_table[0]);
  2791. av_freep(&h->mvd_table[1]);
  2792. av_freep(&h->direct_table);
  2793. av_freep(&h->non_zero_count);
  2794. av_freep(&h->slice_table_base);
  2795. av_freep(&h->top_borders[1]);
  2796. av_freep(&h->top_borders[0]);
  2797. h->slice_table= NULL;
  2798. av_freep(&h->mb2b_xy);
  2799. av_freep(&h->mb2b8_xy);
  2800. av_freep(&h->s.obmc_scratchpad);
  2801. }
  2802. static void init_dequant8_coeff_table(H264Context *h){
  2803. int i,q,x;
  2804. const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
  2805. h->dequant8_coeff[0] = h->dequant8_buffer[0];
  2806. h->dequant8_coeff[1] = h->dequant8_buffer[1];
  2807. for(i=0; i<2; i++ ){
  2808. if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
  2809. h->dequant8_coeff[1] = h->dequant8_buffer[0];
  2810. break;
  2811. }
  2812. for(q=0; q<52; q++){
  2813. int shift = ff_div6[q];
  2814. int idx = ff_rem6[q];
  2815. for(x=0; x<64; x++)
  2816. h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
  2817. ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
  2818. h->pps.scaling_matrix8[i][x]) << shift;
  2819. }
  2820. }
  2821. }
  2822. static void init_dequant4_coeff_table(H264Context *h){
  2823. int i,j,q,x;
  2824. const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
  2825. for(i=0; i<6; i++ ){
  2826. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  2827. for(j=0; j<i; j++){
  2828. if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
  2829. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  2830. break;
  2831. }
  2832. }
  2833. if(j<i)
  2834. continue;
  2835. for(q=0; q<52; q++){
  2836. int shift = ff_div6[q] + 2;
  2837. int idx = ff_rem6[q];
  2838. for(x=0; x<16; x++)
  2839. h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
  2840. ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
  2841. h->pps.scaling_matrix4[i][x]) << shift;
  2842. }
  2843. }
  2844. }
  2845. static void init_dequant_tables(H264Context *h){
  2846. int i,x;
  2847. init_dequant4_coeff_table(h);
  2848. if(h->pps.transform_8x8_mode)
  2849. init_dequant8_coeff_table(h);
  2850. if(h->sps.transform_bypass){
  2851. for(i=0; i<6; i++)
  2852. for(x=0; x<16; x++)
  2853. h->dequant4_coeff[i][0][x] = 1<<6;
  2854. if(h->pps.transform_8x8_mode)
  2855. for(i=0; i<2; i++)
  2856. for(x=0; x<64; x++)
  2857. h->dequant8_coeff[i][0][x] = 1<<6;
  2858. }
  2859. }
  2860. /**
  2861. * allocates tables.
  2862. * needs width/height
  2863. */
  2864. static int alloc_tables(H264Context *h){
  2865. MpegEncContext * const s = &h->s;
  2866. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  2867. int x,y;
  2868. CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
  2869. CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
  2870. CHECKED_ALLOCZ(h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(uint8_t))
  2871. CHECKED_ALLOCZ(h->top_borders[0] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2872. CHECKED_ALLOCZ(h->top_borders[1] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2873. CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
  2874. if( h->pps.cabac ) {
  2875. CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
  2876. CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
  2877. CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t));
  2878. CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t));
  2879. }
  2880. memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(uint8_t));
  2881. h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
  2882. CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint32_t));
  2883. CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint32_t));
  2884. for(y=0; y<s->mb_height; y++){
  2885. for(x=0; x<s->mb_width; x++){
  2886. const int mb_xy= x + y*s->mb_stride;
  2887. const int b_xy = 4*x + 4*y*h->b_stride;
  2888. const int b8_xy= 2*x + 2*y*h->b8_stride;
  2889. h->mb2b_xy [mb_xy]= b_xy;
  2890. h->mb2b8_xy[mb_xy]= b8_xy;
  2891. }
  2892. }
  2893. s->obmc_scratchpad = NULL;
  2894. if(!h->dequant4_coeff[0])
  2895. init_dequant_tables(h);
  2896. return 0;
  2897. fail:
  2898. free_tables(h);
  2899. return -1;
  2900. }
  2901. static void common_init(H264Context *h){
  2902. MpegEncContext * const s = &h->s;
  2903. s->width = s->avctx->width;
  2904. s->height = s->avctx->height;
  2905. s->codec_id= s->avctx->codec->id;
  2906. init_pred_ptrs(h);
  2907. h->dequant_coeff_pps= -1;
  2908. s->unrestricted_mv=1;
  2909. s->decode=1; //FIXME
  2910. memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  2911. memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  2912. }
  2913. static int decode_init(AVCodecContext *avctx){
  2914. H264Context *h= avctx->priv_data;
  2915. MpegEncContext * const s = &h->s;
  2916. MPV_decode_defaults(s);
  2917. s->avctx = avctx;
  2918. common_init(h);
  2919. s->out_format = FMT_H264;
  2920. s->workaround_bugs= avctx->workaround_bugs;
  2921. // set defaults
  2922. // s->decode_mb= ff_h263_decode_mb;
  2923. s->low_delay= 1;
  2924. avctx->pix_fmt= PIX_FMT_YUV420P;
  2925. decode_init_vlc();
  2926. if(avctx->extradata_size > 0 && avctx->extradata &&
  2927. *(char *)avctx->extradata == 1){
  2928. h->is_avc = 1;
  2929. h->got_avcC = 0;
  2930. } else {
  2931. h->is_avc = 0;
  2932. }
  2933. return 0;
  2934. }
  2935. static int frame_start(H264Context *h){
  2936. MpegEncContext * const s = &h->s;
  2937. int i;
  2938. if(MPV_frame_start(s, s->avctx) < 0)
  2939. return -1;
  2940. ff_er_frame_start(s);
  2941. assert(s->linesize && s->uvlinesize);
  2942. for(i=0; i<16; i++){
  2943. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  2944. h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  2945. }
  2946. for(i=0; i<4; i++){
  2947. h->block_offset[16+i]=
  2948. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2949. h->block_offset[24+16+i]=
  2950. h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2951. }
  2952. /* can't be in alloc_tables because linesize isn't known there.
  2953. * FIXME: redo bipred weight to not require extra buffer? */
  2954. if(!s->obmc_scratchpad)
  2955. s->obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  2956. /* some macroblocks will be accessed before they're available */
  2957. if(FRAME_MBAFF)
  2958. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(uint8_t));
  2959. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  2960. return 0;
  2961. }
  2962. static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  2963. MpegEncContext * const s = &h->s;
  2964. int i;
  2965. src_y -= linesize;
  2966. src_cb -= uvlinesize;
  2967. src_cr -= uvlinesize;
  2968. // There are two lines saved, the line above the the top macroblock of a pair,
  2969. // and the line above the bottom macroblock
  2970. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  2971. for(i=1; i<17; i++){
  2972. h->left_border[i]= src_y[15+i* linesize];
  2973. }
  2974. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
  2975. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
  2976. if(!(s->flags&CODEC_FLAG_GRAY)){
  2977. h->left_border[17 ]= h->top_borders[0][s->mb_x][16+7];
  2978. h->left_border[17+9]= h->top_borders[0][s->mb_x][24+7];
  2979. for(i=1; i<9; i++){
  2980. h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
  2981. h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
  2982. }
  2983. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
  2984. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
  2985. }
  2986. }
  2987. 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){
  2988. MpegEncContext * const s = &h->s;
  2989. int temp8, i;
  2990. uint64_t temp64;
  2991. int deblock_left = (s->mb_x > 0);
  2992. int deblock_top = (s->mb_y > 0);
  2993. src_y -= linesize + 1;
  2994. src_cb -= uvlinesize + 1;
  2995. src_cr -= uvlinesize + 1;
  2996. #define XCHG(a,b,t,xchg)\
  2997. t= a;\
  2998. if(xchg)\
  2999. a= b;\
  3000. b= t;
  3001. if(deblock_left){
  3002. for(i = !deblock_top; i<17; i++){
  3003. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  3004. }
  3005. }
  3006. if(deblock_top){
  3007. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  3008. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  3009. if(s->mb_x+1 < s->mb_width){
  3010. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  3011. }
  3012. }
  3013. if(!(s->flags&CODEC_FLAG_GRAY)){
  3014. if(deblock_left){
  3015. for(i = !deblock_top; i<9; i++){
  3016. XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
  3017. XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
  3018. }
  3019. }
  3020. if(deblock_top){
  3021. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  3022. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  3023. }
  3024. }
  3025. }
  3026. static inline void backup_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  3027. MpegEncContext * const s = &h->s;
  3028. int i;
  3029. src_y -= 2 * linesize;
  3030. src_cb -= 2 * uvlinesize;
  3031. src_cr -= 2 * uvlinesize;
  3032. // There are two lines saved, the line above the the top macroblock of a pair,
  3033. // and the line above the bottom macroblock
  3034. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  3035. h->left_border[1]= h->top_borders[1][s->mb_x][15];
  3036. for(i=2; i<34; i++){
  3037. h->left_border[i]= src_y[15+i* linesize];
  3038. }
  3039. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 32*linesize);
  3040. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+32*linesize);
  3041. *(uint64_t*)(h->top_borders[1][s->mb_x]+0)= *(uint64_t*)(src_y + 33*linesize);
  3042. *(uint64_t*)(h->top_borders[1][s->mb_x]+8)= *(uint64_t*)(src_y +8+33*linesize);
  3043. if(!(s->flags&CODEC_FLAG_GRAY)){
  3044. h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7];
  3045. h->left_border[34+ 1]= h->top_borders[1][s->mb_x][16+7];
  3046. h->left_border[34+18 ]= h->top_borders[0][s->mb_x][24+7];
  3047. h->left_border[34+18+1]= h->top_borders[1][s->mb_x][24+7];
  3048. for(i=2; i<18; i++){
  3049. h->left_border[i+34 ]= src_cb[7+i*uvlinesize];
  3050. h->left_border[i+34+18]= src_cr[7+i*uvlinesize];
  3051. }
  3052. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+16*uvlinesize);
  3053. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+16*uvlinesize);
  3054. *(uint64_t*)(h->top_borders[1][s->mb_x]+16)= *(uint64_t*)(src_cb+17*uvlinesize);
  3055. *(uint64_t*)(h->top_borders[1][s->mb_x]+24)= *(uint64_t*)(src_cr+17*uvlinesize);
  3056. }
  3057. }
  3058. 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){
  3059. MpegEncContext * const s = &h->s;
  3060. int temp8, i;
  3061. uint64_t temp64;
  3062. int deblock_left = (s->mb_x > 0);
  3063. int deblock_top = (s->mb_y > 1);
  3064. 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);
  3065. src_y -= 2 * linesize + 1;
  3066. src_cb -= 2 * uvlinesize + 1;
  3067. src_cr -= 2 * uvlinesize + 1;
  3068. #define XCHG(a,b,t,xchg)\
  3069. t= a;\
  3070. if(xchg)\
  3071. a= b;\
  3072. b= t;
  3073. if(deblock_left){
  3074. for(i = (!deblock_top)<<1; i<34; i++){
  3075. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  3076. }
  3077. }
  3078. if(deblock_top){
  3079. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  3080. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  3081. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+0), *(uint64_t*)(src_y +1 +linesize), temp64, xchg);
  3082. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+8), *(uint64_t*)(src_y +9 +linesize), temp64, 1);
  3083. if(s->mb_x+1 < s->mb_width){
  3084. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  3085. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x+1]), *(uint64_t*)(src_y +17 +linesize), temp64, 1);
  3086. }
  3087. }
  3088. if(!(s->flags&CODEC_FLAG_GRAY)){
  3089. if(deblock_left){
  3090. for(i = (!deblock_top) << 1; i<18; i++){
  3091. XCHG(h->left_border[i+34 ], src_cb[i*uvlinesize], temp8, xchg);
  3092. XCHG(h->left_border[i+34+18], src_cr[i*uvlinesize], temp8, xchg);
  3093. }
  3094. }
  3095. if(deblock_top){
  3096. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  3097. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  3098. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+16), *(uint64_t*)(src_cb+1 +uvlinesize), temp64, 1);
  3099. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+24), *(uint64_t*)(src_cr+1 +uvlinesize), temp64, 1);
  3100. }
  3101. }
  3102. }
  3103. static void hl_decode_mb(H264Context *h){
  3104. MpegEncContext * const s = &h->s;
  3105. const int mb_x= s->mb_x;
  3106. const int mb_y= s->mb_y;
  3107. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3108. const int mb_type= s->current_picture.mb_type[mb_xy];
  3109. uint8_t *dest_y, *dest_cb, *dest_cr;
  3110. int linesize, uvlinesize /*dct_offset*/;
  3111. int i;
  3112. int *block_offset = &h->block_offset[0];
  3113. const unsigned int bottom = mb_y & 1;
  3114. const int transform_bypass = (s->qscale == 0 && h->sps.transform_bypass);
  3115. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  3116. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  3117. if(!s->decode)
  3118. return;
  3119. dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3120. dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3121. dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3122. s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
  3123. s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
  3124. if (MB_FIELD) {
  3125. linesize = h->mb_linesize = s->linesize * 2;
  3126. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  3127. block_offset = &h->block_offset[24];
  3128. if(mb_y&1){ //FIXME move out of this func?
  3129. dest_y -= s->linesize*15;
  3130. dest_cb-= s->uvlinesize*7;
  3131. dest_cr-= s->uvlinesize*7;
  3132. }
  3133. if(FRAME_MBAFF) {
  3134. int list;
  3135. for(list=0; list<2; list++){
  3136. if(!USES_LIST(mb_type, list))
  3137. continue;
  3138. if(IS_16X16(mb_type)){
  3139. int8_t *ref = &h->ref_cache[list][scan8[0]];
  3140. fill_rectangle(ref, 4, 4, 8, 16+*ref^(s->mb_y&1), 1);
  3141. }else{
  3142. for(i=0; i<16; i+=4){
  3143. //FIXME can refs be smaller than 8x8 when !direct_8x8_inference ?
  3144. int ref = h->ref_cache[list][scan8[i]];
  3145. if(ref >= 0)
  3146. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, 16+ref^(s->mb_y&1), 1);
  3147. }
  3148. }
  3149. }
  3150. }
  3151. } else {
  3152. linesize = h->mb_linesize = s->linesize;
  3153. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  3154. // dct_offset = s->linesize * 16;
  3155. }
  3156. if(transform_bypass){
  3157. idct_dc_add =
  3158. idct_add = IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  3159. }else if(IS_8x8DCT(mb_type)){
  3160. idct_dc_add = s->dsp.h264_idct8_dc_add;
  3161. idct_add = s->dsp.h264_idct8_add;
  3162. }else{
  3163. idct_dc_add = s->dsp.h264_idct_dc_add;
  3164. idct_add = s->dsp.h264_idct_add;
  3165. }
  3166. if(FRAME_MBAFF && h->deblocking_filter && IS_INTRA(mb_type)
  3167. && (!bottom || !IS_INTRA(s->current_picture.mb_type[mb_xy-s->mb_stride]))){
  3168. int mbt_y = mb_y&~1;
  3169. uint8_t *top_y = s->current_picture.data[0] + (mbt_y * 16* s->linesize ) + mb_x * 16;
  3170. uint8_t *top_cb = s->current_picture.data[1] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  3171. uint8_t *top_cr = s->current_picture.data[2] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  3172. xchg_pair_border(h, top_y, top_cb, top_cr, s->linesize, s->uvlinesize, 1);
  3173. }
  3174. if (IS_INTRA_PCM(mb_type)) {
  3175. unsigned int x, y;
  3176. // The pixels are stored in h->mb array in the same order as levels,
  3177. // copy them in output in the correct order.
  3178. for(i=0; i<16; i++) {
  3179. for (y=0; y<4; y++) {
  3180. for (x=0; x<4; x++) {
  3181. *(dest_y + block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x];
  3182. }
  3183. }
  3184. }
  3185. for(i=16; i<16+4; i++) {
  3186. for (y=0; y<4; y++) {
  3187. for (x=0; x<4; x++) {
  3188. *(dest_cb + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  3189. }
  3190. }
  3191. }
  3192. for(i=20; i<20+4; i++) {
  3193. for (y=0; y<4; y++) {
  3194. for (x=0; x<4; x++) {
  3195. *(dest_cr + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  3196. }
  3197. }
  3198. }
  3199. } else {
  3200. if(IS_INTRA(mb_type)){
  3201. if(h->deblocking_filter && !FRAME_MBAFF)
  3202. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1);
  3203. if(!(s->flags&CODEC_FLAG_GRAY)){
  3204. h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  3205. h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  3206. }
  3207. if(IS_INTRA4x4(mb_type)){
  3208. if(!s->encoding){
  3209. if(IS_8x8DCT(mb_type)){
  3210. for(i=0; i<16; i+=4){
  3211. uint8_t * const ptr= dest_y + block_offset[i];
  3212. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  3213. const int nnz = h->non_zero_count_cache[ scan8[i] ];
  3214. h->pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  3215. (h->topright_samples_available<<(i+1))&0x8000, linesize);
  3216. if(nnz){
  3217. if(nnz == 1 && h->mb[i*16])
  3218. idct_dc_add(ptr, h->mb + i*16, linesize);
  3219. else
  3220. idct_add(ptr, h->mb + i*16, linesize);
  3221. }
  3222. }
  3223. }else
  3224. for(i=0; i<16; i++){
  3225. uint8_t * const ptr= dest_y + block_offset[i];
  3226. uint8_t *topright;
  3227. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  3228. int nnz, tr;
  3229. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  3230. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  3231. assert(mb_y || linesize <= block_offset[i]);
  3232. if(!topright_avail){
  3233. tr= ptr[3 - linesize]*0x01010101;
  3234. topright= (uint8_t*) &tr;
  3235. }else
  3236. topright= ptr + 4 - linesize;
  3237. }else
  3238. topright= NULL;
  3239. h->pred4x4[ dir ](ptr, topright, linesize);
  3240. nnz = h->non_zero_count_cache[ scan8[i] ];
  3241. if(nnz){
  3242. if(s->codec_id == CODEC_ID_H264){
  3243. if(nnz == 1 && h->mb[i*16])
  3244. idct_dc_add(ptr, h->mb + i*16, linesize);
  3245. else
  3246. idct_add(ptr, h->mb + i*16, linesize);
  3247. }else
  3248. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  3249. }
  3250. }
  3251. }
  3252. }else{
  3253. h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  3254. if(s->codec_id == CODEC_ID_H264){
  3255. if(!transform_bypass)
  3256. h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[IS_INTRA(mb_type) ? 0:3][s->qscale][0]);
  3257. }else
  3258. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  3259. }
  3260. if(h->deblocking_filter && !FRAME_MBAFF)
  3261. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
  3262. }else if(s->codec_id == CODEC_ID_H264){
  3263. hl_motion(h, dest_y, dest_cb, dest_cr,
  3264. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  3265. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  3266. s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
  3267. }
  3268. if(!IS_INTRA4x4(mb_type)){
  3269. if(s->codec_id == CODEC_ID_H264){
  3270. if(IS_INTRA16x16(mb_type)){
  3271. for(i=0; i<16; i++){
  3272. if(h->non_zero_count_cache[ scan8[i] ])
  3273. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3274. else if(h->mb[i*16])
  3275. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3276. }
  3277. }else{
  3278. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  3279. for(i=0; i<16; i+=di){
  3280. int nnz = h->non_zero_count_cache[ scan8[i] ];
  3281. if(nnz){
  3282. if(nnz==1 && h->mb[i*16])
  3283. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3284. else
  3285. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3286. }
  3287. }
  3288. }
  3289. }else{
  3290. for(i=0; i<16; i++){
  3291. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  3292. uint8_t * const ptr= dest_y + block_offset[i];
  3293. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  3294. }
  3295. }
  3296. }
  3297. }
  3298. if(!(s->flags&CODEC_FLAG_GRAY)){
  3299. uint8_t *dest[2] = {dest_cb, dest_cr};
  3300. if(transform_bypass){
  3301. idct_add = idct_dc_add = s->dsp.add_pixels4;
  3302. }else{
  3303. idct_add = s->dsp.h264_idct_add;
  3304. idct_dc_add = s->dsp.h264_idct_dc_add;
  3305. 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]);
  3306. 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]);
  3307. }
  3308. if(s->codec_id == CODEC_ID_H264){
  3309. for(i=16; i<16+8; i++){
  3310. if(h->non_zero_count_cache[ scan8[i] ])
  3311. idct_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3312. else if(h->mb[i*16])
  3313. idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3314. }
  3315. }else{
  3316. for(i=16; i<16+8; i++){
  3317. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  3318. uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
  3319. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  3320. }
  3321. }
  3322. }
  3323. }
  3324. }
  3325. if(h->deblocking_filter) {
  3326. if (FRAME_MBAFF) {
  3327. //FIXME try deblocking one mb at a time?
  3328. // the reduction in load/storing mvs and such might outweigh the extra backup/xchg_border
  3329. const int mb_y = s->mb_y - 1;
  3330. uint8_t *pair_dest_y, *pair_dest_cb, *pair_dest_cr;
  3331. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3332. const int mb_type_top = s->current_picture.mb_type[mb_xy];
  3333. const int mb_type_bottom= s->current_picture.mb_type[mb_xy+s->mb_stride];
  3334. if (!bottom) return;
  3335. pair_dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3336. pair_dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3337. pair_dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3338. if(IS_INTRA(mb_type_top | mb_type_bottom))
  3339. xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0);
  3340. backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize);
  3341. // deblock a pair
  3342. // top
  3343. s->mb_y--;
  3344. 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);
  3345. fill_caches(h, mb_type_top, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3346. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy]);
  3347. filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize);
  3348. // bottom
  3349. s->mb_y++;
  3350. tprintf("call mbaff filter_mb\n");
  3351. fill_caches(h, mb_type_bottom, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3352. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy+s->mb_stride]);
  3353. filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3354. } else {
  3355. tprintf("call filter_mb\n");
  3356. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3357. fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3358. filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3359. }
  3360. }
  3361. }
  3362. /**
  3363. * fills the default_ref_list.
  3364. */
  3365. static int fill_default_ref_list(H264Context *h){
  3366. MpegEncContext * const s = &h->s;
  3367. int i;
  3368. int smallest_poc_greater_than_current = -1;
  3369. Picture sorted_short_ref[32];
  3370. if(h->slice_type==B_TYPE){
  3371. int out_i;
  3372. int limit= INT_MIN;
  3373. /* sort frame according to poc in B slice */
  3374. for(out_i=0; out_i<h->short_ref_count; out_i++){
  3375. int best_i=INT_MIN;
  3376. int best_poc=INT_MAX;
  3377. for(i=0; i<h->short_ref_count; i++){
  3378. const int poc= h->short_ref[i]->poc;
  3379. if(poc > limit && poc < best_poc){
  3380. best_poc= poc;
  3381. best_i= i;
  3382. }
  3383. }
  3384. assert(best_i != INT_MIN);
  3385. limit= best_poc;
  3386. sorted_short_ref[out_i]= *h->short_ref[best_i];
  3387. 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);
  3388. if (-1 == smallest_poc_greater_than_current) {
  3389. if (h->short_ref[best_i]->poc >= s->current_picture_ptr->poc) {
  3390. smallest_poc_greater_than_current = out_i;
  3391. }
  3392. }
  3393. }
  3394. }
  3395. if(s->picture_structure == PICT_FRAME){
  3396. if(h->slice_type==B_TYPE){
  3397. int list;
  3398. tprintf("current poc: %d, smallest_poc_greater_than_current: %d\n", s->current_picture_ptr->poc, smallest_poc_greater_than_current);
  3399. // find the largest poc
  3400. for(list=0; list<2; list++){
  3401. int index = 0;
  3402. int j= -99;
  3403. int step= list ? -1 : 1;
  3404. for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++, j+=step) {
  3405. while(j<0 || j>= h->short_ref_count){
  3406. if(j != -99 && step == (list ? -1 : 1))
  3407. return -1;
  3408. step = -step;
  3409. j= smallest_poc_greater_than_current + (step>>1);
  3410. }
  3411. if(sorted_short_ref[j].reference != 3) continue;
  3412. h->default_ref_list[list][index ]= sorted_short_ref[j];
  3413. h->default_ref_list[list][index++].pic_id= sorted_short_ref[j].frame_num;
  3414. }
  3415. for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){
  3416. if(h->long_ref[i] == NULL) continue;
  3417. if(h->long_ref[i]->reference != 3) continue;
  3418. h->default_ref_list[ list ][index ]= *h->long_ref[i];
  3419. h->default_ref_list[ list ][index++].pic_id= i;;
  3420. }
  3421. if(list && (smallest_poc_greater_than_current<=0 || smallest_poc_greater_than_current>=h->short_ref_count) && (1 < index)){
  3422. // swap the two first elements of L1 when
  3423. // L0 and L1 are identical
  3424. Picture temp= h->default_ref_list[1][0];
  3425. h->default_ref_list[1][0] = h->default_ref_list[1][1];
  3426. h->default_ref_list[1][1] = temp;
  3427. }
  3428. if(index < h->ref_count[ list ])
  3429. memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
  3430. }
  3431. }else{
  3432. int index=0;
  3433. for(i=0; i<h->short_ref_count; i++){
  3434. if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
  3435. h->default_ref_list[0][index ]= *h->short_ref[i];
  3436. h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
  3437. }
  3438. for(i = 0; i < 16; i++){
  3439. if(h->long_ref[i] == NULL) continue;
  3440. if(h->long_ref[i]->reference != 3) continue;
  3441. h->default_ref_list[0][index ]= *h->long_ref[i];
  3442. h->default_ref_list[0][index++].pic_id= i;;
  3443. }
  3444. if(index < h->ref_count[0])
  3445. memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
  3446. }
  3447. }else{ //FIELD
  3448. if(h->slice_type==B_TYPE){
  3449. }else{
  3450. //FIXME second field balh
  3451. }
  3452. }
  3453. #ifdef TRACE
  3454. for (i=0; i<h->ref_count[0]; i++) {
  3455. 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]);
  3456. }
  3457. if(h->slice_type==B_TYPE){
  3458. for (i=0; i<h->ref_count[1]; i++) {
  3459. 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]);
  3460. }
  3461. }
  3462. #endif
  3463. return 0;
  3464. }
  3465. static void print_short_term(H264Context *h);
  3466. static void print_long_term(H264Context *h);
  3467. static int decode_ref_pic_list_reordering(H264Context *h){
  3468. MpegEncContext * const s = &h->s;
  3469. int list, index;
  3470. print_short_term(h);
  3471. print_long_term(h);
  3472. if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move before func
  3473. for(list=0; list<2; list++){
  3474. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  3475. if(get_bits1(&s->gb)){
  3476. int pred= h->curr_pic_num;
  3477. for(index=0; ; index++){
  3478. unsigned int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
  3479. unsigned int pic_id;
  3480. int i;
  3481. Picture *ref = NULL;
  3482. if(reordering_of_pic_nums_idc==3)
  3483. break;
  3484. if(index >= h->ref_count[list]){
  3485. av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
  3486. return -1;
  3487. }
  3488. if(reordering_of_pic_nums_idc<3){
  3489. if(reordering_of_pic_nums_idc<2){
  3490. const unsigned int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  3491. if(abs_diff_pic_num >= h->max_pic_num){
  3492. av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  3493. return -1;
  3494. }
  3495. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  3496. else pred+= abs_diff_pic_num;
  3497. pred &= h->max_pic_num - 1;
  3498. for(i= h->short_ref_count-1; i>=0; i--){
  3499. ref = h->short_ref[i];
  3500. assert(ref->reference == 3);
  3501. assert(!ref->long_ref);
  3502. if(ref->data[0] != NULL && ref->frame_num == pred && ref->long_ref == 0) // ignore non existing pictures by testing data[0] pointer
  3503. break;
  3504. }
  3505. if(i>=0)
  3506. ref->pic_id= ref->frame_num;
  3507. }else{
  3508. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  3509. if(pic_id>31){
  3510. av_log(h->s.avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
  3511. return -1;
  3512. }
  3513. ref = h->long_ref[pic_id];
  3514. if(ref){
  3515. ref->pic_id= pic_id;
  3516. assert(ref->reference == 3);
  3517. assert(ref->long_ref);
  3518. i=0;
  3519. }else{
  3520. i=-1;
  3521. }
  3522. }
  3523. if (i < 0) {
  3524. av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  3525. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  3526. } else {
  3527. for(i=index; i+1<h->ref_count[list]; i++){
  3528. if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
  3529. break;
  3530. }
  3531. for(; i > index; i--){
  3532. h->ref_list[list][i]= h->ref_list[list][i-1];
  3533. }
  3534. h->ref_list[list][index]= *ref;
  3535. }
  3536. }else{
  3537. av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  3538. return -1;
  3539. }
  3540. }
  3541. }
  3542. if(h->slice_type!=B_TYPE) break;
  3543. }
  3544. for(list=0; list<2; list++){
  3545. for(index= 0; index < h->ref_count[list]; index++){
  3546. if(!h->ref_list[list][index].data[0])
  3547. h->ref_list[list][index]= s->current_picture;
  3548. }
  3549. if(h->slice_type!=B_TYPE) break;
  3550. }
  3551. if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)
  3552. direct_dist_scale_factor(h);
  3553. direct_ref_list_init(h);
  3554. return 0;
  3555. }
  3556. static void fill_mbaff_ref_list(H264Context *h){
  3557. int list, i, j;
  3558. for(list=0; list<2; list++){
  3559. for(i=0; i<h->ref_count[list]; i++){
  3560. Picture *frame = &h->ref_list[list][i];
  3561. Picture *field = &h->ref_list[list][16+2*i];
  3562. field[0] = *frame;
  3563. for(j=0; j<3; j++)
  3564. field[0].linesize[j] <<= 1;
  3565. field[1] = field[0];
  3566. for(j=0; j<3; j++)
  3567. field[1].data[j] += frame->linesize[j];
  3568. h->luma_weight[list][16+2*i] = h->luma_weight[list][16+2*i+1] = h->luma_weight[list][i];
  3569. h->luma_offset[list][16+2*i] = h->luma_offset[list][16+2*i+1] = h->luma_offset[list][i];
  3570. for(j=0; j<2; j++){
  3571. h->chroma_weight[list][16+2*i][j] = h->chroma_weight[list][16+2*i+1][j] = h->chroma_weight[list][i][j];
  3572. h->chroma_offset[list][16+2*i][j] = h->chroma_offset[list][16+2*i+1][j] = h->chroma_offset[list][i][j];
  3573. }
  3574. }
  3575. }
  3576. for(j=0; j<h->ref_count[1]; j++){
  3577. for(i=0; i<h->ref_count[0]; i++)
  3578. h->implicit_weight[j][16+2*i] = h->implicit_weight[j][16+2*i+1] = h->implicit_weight[j][i];
  3579. memcpy(h->implicit_weight[16+2*j], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3580. memcpy(h->implicit_weight[16+2*j+1], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3581. }
  3582. }
  3583. static int pred_weight_table(H264Context *h){
  3584. MpegEncContext * const s = &h->s;
  3585. int list, i;
  3586. int luma_def, chroma_def;
  3587. h->use_weight= 0;
  3588. h->use_weight_chroma= 0;
  3589. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  3590. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  3591. luma_def = 1<<h->luma_log2_weight_denom;
  3592. chroma_def = 1<<h->chroma_log2_weight_denom;
  3593. for(list=0; list<2; list++){
  3594. for(i=0; i<h->ref_count[list]; i++){
  3595. int luma_weight_flag, chroma_weight_flag;
  3596. luma_weight_flag= get_bits1(&s->gb);
  3597. if(luma_weight_flag){
  3598. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  3599. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  3600. if( h->luma_weight[list][i] != luma_def
  3601. || h->luma_offset[list][i] != 0)
  3602. h->use_weight= 1;
  3603. }else{
  3604. h->luma_weight[list][i]= luma_def;
  3605. h->luma_offset[list][i]= 0;
  3606. }
  3607. chroma_weight_flag= get_bits1(&s->gb);
  3608. if(chroma_weight_flag){
  3609. int j;
  3610. for(j=0; j<2; j++){
  3611. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  3612. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  3613. if( h->chroma_weight[list][i][j] != chroma_def
  3614. || h->chroma_offset[list][i][j] != 0)
  3615. h->use_weight_chroma= 1;
  3616. }
  3617. }else{
  3618. int j;
  3619. for(j=0; j<2; j++){
  3620. h->chroma_weight[list][i][j]= chroma_def;
  3621. h->chroma_offset[list][i][j]= 0;
  3622. }
  3623. }
  3624. }
  3625. if(h->slice_type != B_TYPE) break;
  3626. }
  3627. h->use_weight= h->use_weight || h->use_weight_chroma;
  3628. return 0;
  3629. }
  3630. static void implicit_weight_table(H264Context *h){
  3631. MpegEncContext * const s = &h->s;
  3632. int ref0, ref1;
  3633. int cur_poc = s->current_picture_ptr->poc;
  3634. if( h->ref_count[0] == 1 && h->ref_count[1] == 1
  3635. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  3636. h->use_weight= 0;
  3637. h->use_weight_chroma= 0;
  3638. return;
  3639. }
  3640. h->use_weight= 2;
  3641. h->use_weight_chroma= 2;
  3642. h->luma_log2_weight_denom= 5;
  3643. h->chroma_log2_weight_denom= 5;
  3644. for(ref0=0; ref0 < h->ref_count[0]; ref0++){
  3645. int poc0 = h->ref_list[0][ref0].poc;
  3646. for(ref1=0; ref1 < h->ref_count[1]; ref1++){
  3647. int poc1 = h->ref_list[1][ref1].poc;
  3648. int td = clip(poc1 - poc0, -128, 127);
  3649. if(td){
  3650. int tb = clip(cur_poc - poc0, -128, 127);
  3651. int tx = (16384 + (FFABS(td) >> 1)) / td;
  3652. int dist_scale_factor = clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
  3653. if(dist_scale_factor < -64 || dist_scale_factor > 128)
  3654. h->implicit_weight[ref0][ref1] = 32;
  3655. else
  3656. h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
  3657. }else
  3658. h->implicit_weight[ref0][ref1] = 32;
  3659. }
  3660. }
  3661. }
  3662. static inline void unreference_pic(H264Context *h, Picture *pic){
  3663. int i;
  3664. pic->reference=0;
  3665. if(pic == h->delayed_output_pic)
  3666. pic->reference=1;
  3667. else{
  3668. for(i = 0; h->delayed_pic[i]; i++)
  3669. if(pic == h->delayed_pic[i]){
  3670. pic->reference=1;
  3671. break;
  3672. }
  3673. }
  3674. }
  3675. /**
  3676. * instantaneous decoder refresh.
  3677. */
  3678. static void idr(H264Context *h){
  3679. int i;
  3680. for(i=0; i<16; i++){
  3681. if (h->long_ref[i] != NULL) {
  3682. unreference_pic(h, h->long_ref[i]);
  3683. h->long_ref[i]= NULL;
  3684. }
  3685. }
  3686. h->long_ref_count=0;
  3687. for(i=0; i<h->short_ref_count; i++){
  3688. unreference_pic(h, h->short_ref[i]);
  3689. h->short_ref[i]= NULL;
  3690. }
  3691. h->short_ref_count=0;
  3692. }
  3693. /* forget old pics after a seek */
  3694. static void flush_dpb(AVCodecContext *avctx){
  3695. H264Context *h= avctx->priv_data;
  3696. int i;
  3697. for(i=0; i<16; i++) {
  3698. if(h->delayed_pic[i])
  3699. h->delayed_pic[i]->reference= 0;
  3700. h->delayed_pic[i]= NULL;
  3701. }
  3702. if(h->delayed_output_pic)
  3703. h->delayed_output_pic->reference= 0;
  3704. h->delayed_output_pic= NULL;
  3705. idr(h);
  3706. if(h->s.current_picture_ptr)
  3707. h->s.current_picture_ptr->reference= 0;
  3708. }
  3709. /**
  3710. *
  3711. * @return the removed picture or NULL if an error occurs
  3712. */
  3713. static Picture * remove_short(H264Context *h, int frame_num){
  3714. MpegEncContext * const s = &h->s;
  3715. int i;
  3716. if(s->avctx->debug&FF_DEBUG_MMCO)
  3717. av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  3718. for(i=0; i<h->short_ref_count; i++){
  3719. Picture *pic= h->short_ref[i];
  3720. if(s->avctx->debug&FF_DEBUG_MMCO)
  3721. av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  3722. if(pic->frame_num == frame_num){
  3723. h->short_ref[i]= NULL;
  3724. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
  3725. h->short_ref_count--;
  3726. return pic;
  3727. }
  3728. }
  3729. return NULL;
  3730. }
  3731. /**
  3732. *
  3733. * @return the removed picture or NULL if an error occurs
  3734. */
  3735. static Picture * remove_long(H264Context *h, int i){
  3736. Picture *pic;
  3737. pic= h->long_ref[i];
  3738. h->long_ref[i]= NULL;
  3739. if(pic) h->long_ref_count--;
  3740. return pic;
  3741. }
  3742. /**
  3743. * print short term list
  3744. */
  3745. static void print_short_term(H264Context *h) {
  3746. uint32_t i;
  3747. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3748. av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
  3749. for(i=0; i<h->short_ref_count; i++){
  3750. Picture *pic= h->short_ref[i];
  3751. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3752. }
  3753. }
  3754. }
  3755. /**
  3756. * print long term list
  3757. */
  3758. static void print_long_term(H264Context *h) {
  3759. uint32_t i;
  3760. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3761. av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
  3762. for(i = 0; i < 16; i++){
  3763. Picture *pic= h->long_ref[i];
  3764. if (pic) {
  3765. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3766. }
  3767. }
  3768. }
  3769. }
  3770. /**
  3771. * Executes the reference picture marking (memory management control operations).
  3772. */
  3773. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  3774. MpegEncContext * const s = &h->s;
  3775. int i, j;
  3776. int current_is_long=0;
  3777. Picture *pic;
  3778. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  3779. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  3780. for(i=0; i<mmco_count; i++){
  3781. if(s->avctx->debug&FF_DEBUG_MMCO)
  3782. 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);
  3783. switch(mmco[i].opcode){
  3784. case MMCO_SHORT2UNUSED:
  3785. pic= remove_short(h, mmco[i].short_frame_num);
  3786. if(pic)
  3787. unreference_pic(h, pic);
  3788. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3789. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_short() failure\n");
  3790. break;
  3791. case MMCO_SHORT2LONG:
  3792. pic= remove_long(h, mmco[i].long_index);
  3793. if(pic) unreference_pic(h, pic);
  3794. h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
  3795. if (h->long_ref[ mmco[i].long_index ]){
  3796. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3797. h->long_ref_count++;
  3798. }
  3799. break;
  3800. case MMCO_LONG2UNUSED:
  3801. pic= remove_long(h, mmco[i].long_index);
  3802. if(pic)
  3803. unreference_pic(h, pic);
  3804. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3805. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_long() failure\n");
  3806. break;
  3807. case MMCO_LONG:
  3808. pic= remove_long(h, mmco[i].long_index);
  3809. if(pic) unreference_pic(h, pic);
  3810. h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
  3811. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3812. h->long_ref_count++;
  3813. current_is_long=1;
  3814. break;
  3815. case MMCO_SET_MAX_LONG:
  3816. assert(mmco[i].long_index <= 16);
  3817. // just remove the long term which index is greater than new max
  3818. for(j = mmco[i].long_index; j<16; j++){
  3819. pic = remove_long(h, j);
  3820. if (pic) unreference_pic(h, pic);
  3821. }
  3822. break;
  3823. case MMCO_RESET:
  3824. while(h->short_ref_count){
  3825. pic= remove_short(h, h->short_ref[0]->frame_num);
  3826. if(pic) unreference_pic(h, pic);
  3827. }
  3828. for(j = 0; j < 16; j++) {
  3829. pic= remove_long(h, j);
  3830. if(pic) unreference_pic(h, pic);
  3831. }
  3832. break;
  3833. default: assert(0);
  3834. }
  3835. }
  3836. if(!current_is_long){
  3837. pic= remove_short(h, s->current_picture_ptr->frame_num);
  3838. if(pic){
  3839. unreference_pic(h, pic);
  3840. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  3841. }
  3842. if(h->short_ref_count)
  3843. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  3844. h->short_ref[0]= s->current_picture_ptr;
  3845. h->short_ref[0]->long_ref=0;
  3846. h->short_ref_count++;
  3847. }
  3848. print_short_term(h);
  3849. print_long_term(h);
  3850. return 0;
  3851. }
  3852. static int decode_ref_pic_marking(H264Context *h){
  3853. MpegEncContext * const s = &h->s;
  3854. int i;
  3855. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  3856. s->broken_link= get_bits1(&s->gb) -1;
  3857. h->mmco[0].long_index= get_bits1(&s->gb) - 1; // current_long_term_idx
  3858. if(h->mmco[0].long_index == -1)
  3859. h->mmco_index= 0;
  3860. else{
  3861. h->mmco[0].opcode= MMCO_LONG;
  3862. h->mmco_index= 1;
  3863. }
  3864. }else{
  3865. if(get_bits1(&s->gb)){ // adaptive_ref_pic_marking_mode_flag
  3866. for(i= 0; i<MAX_MMCO_COUNT; i++) {
  3867. MMCOOpcode opcode= get_ue_golomb(&s->gb);;
  3868. h->mmco[i].opcode= opcode;
  3869. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  3870. 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
  3871. /* if(h->mmco[i].short_frame_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_frame_num ] == NULL){
  3872. av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
  3873. return -1;
  3874. }*/
  3875. }
  3876. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  3877. unsigned int long_index= get_ue_golomb(&s->gb);
  3878. if(/*h->mmco[i].long_index >= h->long_ref_count || h->long_ref[ h->mmco[i].long_index ] == NULL*/ long_index >= 16){
  3879. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  3880. return -1;
  3881. }
  3882. h->mmco[i].long_index= long_index;
  3883. }
  3884. if(opcode > (unsigned)MMCO_LONG){
  3885. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  3886. return -1;
  3887. }
  3888. if(opcode == MMCO_END)
  3889. break;
  3890. }
  3891. h->mmco_index= i;
  3892. }else{
  3893. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  3894. if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){ //FIXME fields
  3895. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  3896. h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  3897. h->mmco_index= 1;
  3898. }else
  3899. h->mmco_index= 0;
  3900. }
  3901. }
  3902. return 0;
  3903. }
  3904. static int init_poc(H264Context *h){
  3905. MpegEncContext * const s = &h->s;
  3906. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  3907. int field_poc[2];
  3908. if(h->nal_unit_type == NAL_IDR_SLICE){
  3909. h->frame_num_offset= 0;
  3910. }else{
  3911. if(h->frame_num < h->prev_frame_num)
  3912. h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
  3913. else
  3914. h->frame_num_offset= h->prev_frame_num_offset;
  3915. }
  3916. if(h->sps.poc_type==0){
  3917. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  3918. if(h->nal_unit_type == NAL_IDR_SLICE){
  3919. h->prev_poc_msb=
  3920. h->prev_poc_lsb= 0;
  3921. }
  3922. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  3923. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  3924. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  3925. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  3926. else
  3927. h->poc_msb = h->prev_poc_msb;
  3928. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  3929. field_poc[0] =
  3930. field_poc[1] = h->poc_msb + h->poc_lsb;
  3931. if(s->picture_structure == PICT_FRAME)
  3932. field_poc[1] += h->delta_poc_bottom;
  3933. }else if(h->sps.poc_type==1){
  3934. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  3935. int i;
  3936. if(h->sps.poc_cycle_length != 0)
  3937. abs_frame_num = h->frame_num_offset + h->frame_num;
  3938. else
  3939. abs_frame_num = 0;
  3940. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  3941. abs_frame_num--;
  3942. expected_delta_per_poc_cycle = 0;
  3943. for(i=0; i < h->sps.poc_cycle_length; i++)
  3944. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  3945. if(abs_frame_num > 0){
  3946. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  3947. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  3948. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  3949. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  3950. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  3951. } else
  3952. expectedpoc = 0;
  3953. if(h->nal_ref_idc == 0)
  3954. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  3955. field_poc[0] = expectedpoc + h->delta_poc[0];
  3956. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  3957. if(s->picture_structure == PICT_FRAME)
  3958. field_poc[1] += h->delta_poc[1];
  3959. }else{
  3960. int poc;
  3961. if(h->nal_unit_type == NAL_IDR_SLICE){
  3962. poc= 0;
  3963. }else{
  3964. if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
  3965. else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
  3966. }
  3967. field_poc[0]= poc;
  3968. field_poc[1]= poc;
  3969. }
  3970. if(s->picture_structure != PICT_BOTTOM_FIELD)
  3971. s->current_picture_ptr->field_poc[0]= field_poc[0];
  3972. if(s->picture_structure != PICT_TOP_FIELD)
  3973. s->current_picture_ptr->field_poc[1]= field_poc[1];
  3974. if(s->picture_structure == PICT_FRAME) // FIXME field pix?
  3975. s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]);
  3976. return 0;
  3977. }
  3978. /**
  3979. * decodes a slice header.
  3980. * this will allso call MPV_common_init() and frame_start() as needed
  3981. */
  3982. static int decode_slice_header(H264Context *h){
  3983. MpegEncContext * const s = &h->s;
  3984. unsigned int first_mb_in_slice;
  3985. unsigned int pps_id;
  3986. int num_ref_idx_active_override_flag;
  3987. static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
  3988. unsigned int slice_type, tmp;
  3989. int default_ref_list_done = 0;
  3990. s->current_picture.reference= h->nal_ref_idc != 0;
  3991. s->dropable= h->nal_ref_idc == 0;
  3992. first_mb_in_slice= get_ue_golomb(&s->gb);
  3993. slice_type= get_ue_golomb(&s->gb);
  3994. if(slice_type > 9){
  3995. 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);
  3996. return -1;
  3997. }
  3998. if(slice_type > 4){
  3999. slice_type -= 5;
  4000. h->slice_type_fixed=1;
  4001. }else
  4002. h->slice_type_fixed=0;
  4003. slice_type= slice_type_map[ slice_type ];
  4004. if (slice_type == I_TYPE
  4005. || (h->slice_num != 0 && slice_type == h->slice_type) ) {
  4006. default_ref_list_done = 1;
  4007. }
  4008. h->slice_type= slice_type;
  4009. s->pict_type= h->slice_type; // to make a few old func happy, it's wrong though
  4010. pps_id= get_ue_golomb(&s->gb);
  4011. if(pps_id>=MAX_PPS_COUNT){
  4012. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  4013. return -1;
  4014. }
  4015. h->pps= h->pps_buffer[pps_id];
  4016. if(h->pps.slice_group_count == 0){
  4017. av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referenced\n");
  4018. return -1;
  4019. }
  4020. h->sps= h->sps_buffer[ h->pps.sps_id ];
  4021. if(h->sps.log2_max_frame_num == 0){
  4022. av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referenced\n");
  4023. return -1;
  4024. }
  4025. if(h->dequant_coeff_pps != pps_id){
  4026. h->dequant_coeff_pps = pps_id;
  4027. init_dequant_tables(h);
  4028. }
  4029. s->mb_width= h->sps.mb_width;
  4030. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  4031. h->b_stride= s->mb_width*4;
  4032. h->b8_stride= s->mb_width*2;
  4033. s->width = 16*s->mb_width - 2*(h->sps.crop_left + h->sps.crop_right );
  4034. if(h->sps.frame_mbs_only_flag)
  4035. s->height= 16*s->mb_height - 2*(h->sps.crop_top + h->sps.crop_bottom);
  4036. else
  4037. s->height= 16*s->mb_height - 4*(h->sps.crop_top + h->sps.crop_bottom); //FIXME recheck
  4038. if (s->context_initialized
  4039. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  4040. free_tables(h);
  4041. MPV_common_end(s);
  4042. }
  4043. if (!s->context_initialized) {
  4044. if (MPV_common_init(s) < 0)
  4045. return -1;
  4046. if(s->dsp.h264_idct_add == ff_h264_idct_add_c){ //FIXME little ugly
  4047. memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
  4048. memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
  4049. }else{
  4050. int i;
  4051. for(i=0; i<16; i++){
  4052. #define T(x) (x>>2) | ((x<<2) & 0xF)
  4053. h->zigzag_scan[i] = T(zigzag_scan[i]);
  4054. h-> field_scan[i] = T( field_scan[i]);
  4055. #undef T
  4056. }
  4057. }
  4058. if(s->dsp.h264_idct8_add == ff_h264_idct8_add_c){
  4059. memcpy(h->zigzag_scan8x8, zigzag_scan8x8, 64*sizeof(uint8_t));
  4060. memcpy(h->zigzag_scan8x8_cavlc, zigzag_scan8x8_cavlc, 64*sizeof(uint8_t));
  4061. memcpy(h->field_scan8x8, field_scan8x8, 64*sizeof(uint8_t));
  4062. memcpy(h->field_scan8x8_cavlc, field_scan8x8_cavlc, 64*sizeof(uint8_t));
  4063. }else{
  4064. int i;
  4065. for(i=0; i<64; i++){
  4066. #define T(x) (x>>3) | ((x&7)<<3)
  4067. h->zigzag_scan8x8[i] = T(zigzag_scan8x8[i]);
  4068. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  4069. h->field_scan8x8[i] = T(field_scan8x8[i]);
  4070. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  4071. #undef T
  4072. }
  4073. }
  4074. if(h->sps.transform_bypass){ //FIXME same ugly
  4075. h->zigzag_scan_q0 = zigzag_scan;
  4076. h->zigzag_scan8x8_q0 = zigzag_scan8x8;
  4077. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  4078. h->field_scan_q0 = field_scan;
  4079. h->field_scan8x8_q0 = field_scan8x8;
  4080. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  4081. }else{
  4082. h->zigzag_scan_q0 = h->zigzag_scan;
  4083. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  4084. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  4085. h->field_scan_q0 = h->field_scan;
  4086. h->field_scan8x8_q0 = h->field_scan8x8;
  4087. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  4088. }
  4089. alloc_tables(h);
  4090. s->avctx->width = s->width;
  4091. s->avctx->height = s->height;
  4092. s->avctx->sample_aspect_ratio= h->sps.sar;
  4093. if(!s->avctx->sample_aspect_ratio.den)
  4094. s->avctx->sample_aspect_ratio.den = 1;
  4095. if(h->sps.timing_info_present_flag){
  4096. s->avctx->time_base= (AVRational){h->sps.num_units_in_tick * 2, h->sps.time_scale};
  4097. if(h->x264_build > 0 && h->x264_build < 44)
  4098. s->avctx->time_base.den *= 2;
  4099. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  4100. s->avctx->time_base.num, s->avctx->time_base.den, 1<<30);
  4101. }
  4102. }
  4103. if(h->slice_num == 0){
  4104. if(frame_start(h) < 0)
  4105. return -1;
  4106. }
  4107. s->current_picture_ptr->frame_num= //FIXME frame_num cleanup
  4108. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  4109. h->mb_mbaff = 0;
  4110. h->mb_aff_frame = 0;
  4111. if(h->sps.frame_mbs_only_flag){
  4112. s->picture_structure= PICT_FRAME;
  4113. }else{
  4114. if(get_bits1(&s->gb)) { //field_pic_flag
  4115. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  4116. av_log(h->s.avctx, AV_LOG_ERROR, "PAFF interlacing is not implemented\n");
  4117. } else {
  4118. s->picture_structure= PICT_FRAME;
  4119. h->mb_aff_frame = h->sps.mb_aff;
  4120. }
  4121. }
  4122. assert(s->mb_num == s->mb_width * s->mb_height);
  4123. if(first_mb_in_slice << h->mb_aff_frame >= s->mb_num ||
  4124. first_mb_in_slice >= s->mb_num){
  4125. av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  4126. return -1;
  4127. }
  4128. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  4129. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << h->mb_aff_frame;
  4130. assert(s->mb_y < s->mb_height);
  4131. if(s->picture_structure==PICT_FRAME){
  4132. h->curr_pic_num= h->frame_num;
  4133. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  4134. }else{
  4135. h->curr_pic_num= 2*h->frame_num;
  4136. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  4137. }
  4138. if(h->nal_unit_type == NAL_IDR_SLICE){
  4139. get_ue_golomb(&s->gb); /* idr_pic_id */
  4140. }
  4141. if(h->sps.poc_type==0){
  4142. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  4143. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  4144. h->delta_poc_bottom= get_se_golomb(&s->gb);
  4145. }
  4146. }
  4147. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  4148. h->delta_poc[0]= get_se_golomb(&s->gb);
  4149. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  4150. h->delta_poc[1]= get_se_golomb(&s->gb);
  4151. }
  4152. init_poc(h);
  4153. if(h->pps.redundant_pic_cnt_present){
  4154. h->redundant_pic_count= get_ue_golomb(&s->gb);
  4155. }
  4156. //set defaults, might be overriden a few line later
  4157. h->ref_count[0]= h->pps.ref_count[0];
  4158. h->ref_count[1]= h->pps.ref_count[1];
  4159. if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){
  4160. if(h->slice_type == B_TYPE){
  4161. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  4162. if(h->sps.mb_aff && h->direct_spatial_mv_pred)
  4163. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + spatial direct mode is not implemented\n");
  4164. }
  4165. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  4166. if(num_ref_idx_active_override_flag){
  4167. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  4168. if(h->slice_type==B_TYPE)
  4169. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  4170. if(h->ref_count[0]-1 > 32-1 || h->ref_count[1]-1 > 32-1){
  4171. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  4172. h->ref_count[0]= h->ref_count[1]= 1;
  4173. return -1;
  4174. }
  4175. }
  4176. if(h->slice_type == B_TYPE)
  4177. h->list_count= 2;
  4178. else
  4179. h->list_count= 1;
  4180. }else
  4181. h->list_count= 0;
  4182. if(!default_ref_list_done){
  4183. fill_default_ref_list(h);
  4184. }
  4185. if(decode_ref_pic_list_reordering(h) < 0)
  4186. return -1;
  4187. if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
  4188. || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
  4189. pred_weight_table(h);
  4190. else if(h->pps.weighted_bipred_idc==2 && h->slice_type==B_TYPE)
  4191. implicit_weight_table(h);
  4192. else
  4193. h->use_weight = 0;
  4194. if(s->current_picture.reference)
  4195. decode_ref_pic_marking(h);
  4196. if(FRAME_MBAFF)
  4197. fill_mbaff_ref_list(h);
  4198. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac ){
  4199. tmp = get_ue_golomb(&s->gb);
  4200. if(tmp > 2){
  4201. av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
  4202. return -1;
  4203. }
  4204. h->cabac_init_idc= tmp;
  4205. }
  4206. h->last_qscale_diff = 0;
  4207. tmp = h->pps.init_qp + get_se_golomb(&s->gb);
  4208. if(tmp>51){
  4209. av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  4210. return -1;
  4211. }
  4212. s->qscale= tmp;
  4213. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4214. //FIXME qscale / qp ... stuff
  4215. if(h->slice_type == SP_TYPE){
  4216. get_bits1(&s->gb); /* sp_for_switch_flag */
  4217. }
  4218. if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
  4219. get_se_golomb(&s->gb); /* slice_qs_delta */
  4220. }
  4221. h->deblocking_filter = 1;
  4222. h->slice_alpha_c0_offset = 0;
  4223. h->slice_beta_offset = 0;
  4224. if( h->pps.deblocking_filter_parameters_present ) {
  4225. tmp= get_ue_golomb(&s->gb);
  4226. if(tmp > 2){
  4227. av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
  4228. return -1;
  4229. }
  4230. h->deblocking_filter= tmp;
  4231. if(h->deblocking_filter < 2)
  4232. h->deblocking_filter^= 1; // 1<->0
  4233. if( h->deblocking_filter ) {
  4234. h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
  4235. h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
  4236. }
  4237. }
  4238. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  4239. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type != I_TYPE)
  4240. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type == B_TYPE)
  4241. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  4242. h->deblocking_filter= 0;
  4243. #if 0 //FMO
  4244. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  4245. slice_group_change_cycle= get_bits(&s->gb, ?);
  4246. #endif
  4247. h->slice_num++;
  4248. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
  4249. h->emu_edge_height= FRAME_MBAFF ? 0 : h->emu_edge_width;
  4250. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  4251. av_log(h->s.avctx, AV_LOG_DEBUG, "slice:%d %s mb:%d %c pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s\n",
  4252. h->slice_num,
  4253. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  4254. first_mb_in_slice,
  4255. av_get_pict_type_char(h->slice_type),
  4256. pps_id, h->frame_num,
  4257. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  4258. h->ref_count[0], h->ref_count[1],
  4259. s->qscale,
  4260. h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2,
  4261. h->use_weight,
  4262. h->use_weight==1 && h->use_weight_chroma ? "c" : ""
  4263. );
  4264. }
  4265. if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !s->current_picture.reference){
  4266. s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
  4267. s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
  4268. }else{
  4269. s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
  4270. s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
  4271. }
  4272. return 0;
  4273. }
  4274. /**
  4275. *
  4276. */
  4277. static inline int get_level_prefix(GetBitContext *gb){
  4278. unsigned int buf;
  4279. int log;
  4280. OPEN_READER(re, gb);
  4281. UPDATE_CACHE(re, gb);
  4282. buf=GET_CACHE(re, gb);
  4283. log= 32 - av_log2(buf);
  4284. #ifdef TRACE
  4285. print_bin(buf>>(32-log), log);
  4286. 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__);
  4287. #endif
  4288. LAST_SKIP_BITS(re, gb, log);
  4289. CLOSE_READER(re, gb);
  4290. return log-1;
  4291. }
  4292. static inline int get_dct8x8_allowed(H264Context *h){
  4293. int i;
  4294. for(i=0; i<4; i++){
  4295. if(!IS_SUB_8X8(h->sub_mb_type[i])
  4296. || (!h->sps.direct_8x8_inference_flag && IS_DIRECT(h->sub_mb_type[i])))
  4297. return 0;
  4298. }
  4299. return 1;
  4300. }
  4301. /**
  4302. * decodes a residual block.
  4303. * @param n block index
  4304. * @param scantable scantable
  4305. * @param max_coeff number of coefficients in the block
  4306. * @return <0 if an error occured
  4307. */
  4308. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
  4309. MpegEncContext * const s = &h->s;
  4310. 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};
  4311. int level[16];
  4312. int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
  4313. //FIXME put trailing_onex into the context
  4314. if(n == CHROMA_DC_BLOCK_INDEX){
  4315. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  4316. total_coeff= coeff_token>>2;
  4317. }else{
  4318. if(n == LUMA_DC_BLOCK_INDEX){
  4319. total_coeff= pred_non_zero_count(h, 0);
  4320. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4321. total_coeff= coeff_token>>2;
  4322. }else{
  4323. total_coeff= pred_non_zero_count(h, n);
  4324. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4325. total_coeff= coeff_token>>2;
  4326. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  4327. }
  4328. }
  4329. //FIXME set last_non_zero?
  4330. if(total_coeff==0)
  4331. return 0;
  4332. if(total_coeff > (unsigned)max_coeff) {
  4333. av_log(h->s.avctx, AV_LOG_ERROR, "corrupted macroblock %d %d (total_coeff=%d)\n", s->mb_x, s->mb_y, total_coeff);
  4334. return -1;
  4335. }
  4336. trailing_ones= coeff_token&3;
  4337. tprintf("trailing:%d, total:%d\n", trailing_ones, total_coeff);
  4338. assert(total_coeff<=16);
  4339. for(i=0; i<trailing_ones; i++){
  4340. level[i]= 1 - 2*get_bits1(gb);
  4341. }
  4342. if(i<total_coeff) {
  4343. int level_code, mask;
  4344. int suffix_length = total_coeff > 10 && trailing_ones < 3;
  4345. int prefix= get_level_prefix(gb);
  4346. //first coefficient has suffix_length equal to 0 or 1
  4347. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  4348. if(suffix_length)
  4349. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4350. else
  4351. level_code= (prefix<<suffix_length); //part
  4352. }else if(prefix==14){
  4353. if(suffix_length)
  4354. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4355. else
  4356. level_code= prefix + get_bits(gb, 4); //part
  4357. }else if(prefix==15){
  4358. level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part
  4359. if(suffix_length==0) level_code+=15; //FIXME doesn't make (much)sense
  4360. }else{
  4361. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4362. return -1;
  4363. }
  4364. if(trailing_ones < 3) level_code += 2;
  4365. suffix_length = 1;
  4366. if(level_code > 5)
  4367. suffix_length++;
  4368. mask= -(level_code&1);
  4369. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4370. i++;
  4371. //remaining coefficients have suffix_length > 0
  4372. for(;i<total_coeff;i++) {
  4373. static const int suffix_limit[7] = {0,5,11,23,47,95,INT_MAX };
  4374. prefix = get_level_prefix(gb);
  4375. if(prefix<15){
  4376. level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
  4377. }else if(prefix==15){
  4378. level_code = (prefix<<suffix_length) + get_bits(gb, 12);
  4379. }else{
  4380. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4381. return -1;
  4382. }
  4383. mask= -(level_code&1);
  4384. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4385. if(level_code > suffix_limit[suffix_length])
  4386. suffix_length++;
  4387. }
  4388. }
  4389. if(total_coeff == max_coeff)
  4390. zeros_left=0;
  4391. else{
  4392. if(n == CHROMA_DC_BLOCK_INDEX)
  4393. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  4394. else
  4395. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  4396. }
  4397. coeff_num = zeros_left + total_coeff - 1;
  4398. j = scantable[coeff_num];
  4399. if(n > 24){
  4400. block[j] = level[0];
  4401. for(i=1;i<total_coeff;i++) {
  4402. if(zeros_left <= 0)
  4403. run_before = 0;
  4404. else if(zeros_left < 7){
  4405. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4406. }else{
  4407. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4408. }
  4409. zeros_left -= run_before;
  4410. coeff_num -= 1 + run_before;
  4411. j= scantable[ coeff_num ];
  4412. block[j]= level[i];
  4413. }
  4414. }else{
  4415. block[j] = (level[0] * qmul[j] + 32)>>6;
  4416. for(i=1;i<total_coeff;i++) {
  4417. if(zeros_left <= 0)
  4418. run_before = 0;
  4419. else if(zeros_left < 7){
  4420. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4421. }else{
  4422. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4423. }
  4424. zeros_left -= run_before;
  4425. coeff_num -= 1 + run_before;
  4426. j= scantable[ coeff_num ];
  4427. block[j]= (level[i] * qmul[j] + 32)>>6;
  4428. }
  4429. }
  4430. if(zeros_left<0){
  4431. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  4432. return -1;
  4433. }
  4434. return 0;
  4435. }
  4436. static void predict_field_decoding_flag(H264Context *h){
  4437. MpegEncContext * const s = &h->s;
  4438. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4439. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  4440. ? s->current_picture.mb_type[mb_xy-1]
  4441. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  4442. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  4443. : 0;
  4444. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  4445. }
  4446. /**
  4447. * decodes a P_SKIP or B_SKIP macroblock
  4448. */
  4449. static void decode_mb_skip(H264Context *h){
  4450. MpegEncContext * const s = &h->s;
  4451. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4452. int mb_type=0;
  4453. memset(h->non_zero_count[mb_xy], 0, 16);
  4454. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  4455. if(MB_FIELD)
  4456. mb_type|= MB_TYPE_INTERLACED;
  4457. if( h->slice_type == B_TYPE )
  4458. {
  4459. // just for fill_caches. pred_direct_motion will set the real mb_type
  4460. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
  4461. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4462. pred_direct_motion(h, &mb_type);
  4463. mb_type|= MB_TYPE_SKIP;
  4464. }
  4465. else
  4466. {
  4467. int mx, my;
  4468. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
  4469. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4470. pred_pskip_motion(h, &mx, &my);
  4471. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  4472. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  4473. }
  4474. write_back_motion(h, mb_type);
  4475. s->current_picture.mb_type[mb_xy]= mb_type;
  4476. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4477. h->slice_table[ mb_xy ]= h->slice_num;
  4478. h->prev_mb_skipped= 1;
  4479. }
  4480. /**
  4481. * decodes a macroblock
  4482. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  4483. */
  4484. static int decode_mb_cavlc(H264Context *h){
  4485. MpegEncContext * const s = &h->s;
  4486. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4487. int partition_count;
  4488. unsigned int mb_type, cbp;
  4489. int dct8x8_allowed= h->pps.transform_8x8_mode;
  4490. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?
  4491. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  4492. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  4493. down the code */
  4494. if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){
  4495. if(s->mb_skip_run==-1)
  4496. s->mb_skip_run= get_ue_golomb(&s->gb);
  4497. if (s->mb_skip_run--) {
  4498. if(FRAME_MBAFF && (s->mb_y&1) == 0){
  4499. if(s->mb_skip_run==0)
  4500. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4501. else
  4502. predict_field_decoding_flag(h);
  4503. }
  4504. decode_mb_skip(h);
  4505. return 0;
  4506. }
  4507. }
  4508. if(FRAME_MBAFF){
  4509. if( (s->mb_y&1) == 0 )
  4510. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4511. }else
  4512. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  4513. h->prev_mb_skipped= 0;
  4514. mb_type= get_ue_golomb(&s->gb);
  4515. if(h->slice_type == B_TYPE){
  4516. if(mb_type < 23){
  4517. partition_count= b_mb_type_info[mb_type].partition_count;
  4518. mb_type= b_mb_type_info[mb_type].type;
  4519. }else{
  4520. mb_type -= 23;
  4521. goto decode_intra_mb;
  4522. }
  4523. }else if(h->slice_type == P_TYPE /*|| h->slice_type == SP_TYPE */){
  4524. if(mb_type < 5){
  4525. partition_count= p_mb_type_info[mb_type].partition_count;
  4526. mb_type= p_mb_type_info[mb_type].type;
  4527. }else{
  4528. mb_type -= 5;
  4529. goto decode_intra_mb;
  4530. }
  4531. }else{
  4532. assert(h->slice_type == I_TYPE);
  4533. decode_intra_mb:
  4534. if(mb_type > 25){
  4535. 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);
  4536. return -1;
  4537. }
  4538. partition_count=0;
  4539. cbp= i_mb_type_info[mb_type].cbp;
  4540. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  4541. mb_type= i_mb_type_info[mb_type].type;
  4542. }
  4543. if(MB_FIELD)
  4544. mb_type |= MB_TYPE_INTERLACED;
  4545. h->slice_table[ mb_xy ]= h->slice_num;
  4546. if(IS_INTRA_PCM(mb_type)){
  4547. unsigned int x, y;
  4548. // we assume these blocks are very rare so we dont optimize it
  4549. align_get_bits(&s->gb);
  4550. // The pixels are stored in the same order as levels in h->mb array.
  4551. for(y=0; y<16; y++){
  4552. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  4553. for(x=0; x<16; x++){
  4554. tprintf("LUMA ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4555. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= get_bits(&s->gb, 8);
  4556. }
  4557. }
  4558. for(y=0; y<8; y++){
  4559. const int index= 256 + 4*(y&3) + 32*(y>>2);
  4560. for(x=0; x<8; x++){
  4561. tprintf("CHROMA U ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4562. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4563. }
  4564. }
  4565. for(y=0; y<8; y++){
  4566. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  4567. for(x=0; x<8; x++){
  4568. tprintf("CHROMA V ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4569. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4570. }
  4571. }
  4572. // In deblocking, the quantizer is 0
  4573. s->current_picture.qscale_table[mb_xy]= 0;
  4574. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  4575. // All coeffs are present
  4576. memset(h->non_zero_count[mb_xy], 16, 16);
  4577. s->current_picture.mb_type[mb_xy]= mb_type;
  4578. return 0;
  4579. }
  4580. if(MB_MBAFF){
  4581. h->ref_count[0] <<= 1;
  4582. h->ref_count[1] <<= 1;
  4583. }
  4584. fill_caches(h, mb_type, 0);
  4585. //mb_pred
  4586. if(IS_INTRA(mb_type)){
  4587. int pred_mode;
  4588. // init_top_left_availability(h);
  4589. if(IS_INTRA4x4(mb_type)){
  4590. int i;
  4591. int di = 1;
  4592. if(dct8x8_allowed && get_bits1(&s->gb)){
  4593. mb_type |= MB_TYPE_8x8DCT;
  4594. di = 4;
  4595. }
  4596. // fill_intra4x4_pred_table(h);
  4597. for(i=0; i<16; i+=di){
  4598. int mode= pred_intra_mode(h, i);
  4599. if(!get_bits1(&s->gb)){
  4600. const int rem_mode= get_bits(&s->gb, 3);
  4601. mode = rem_mode + (rem_mode >= mode);
  4602. }
  4603. if(di==4)
  4604. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  4605. else
  4606. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  4607. }
  4608. write_back_intra_pred_mode(h);
  4609. if( check_intra4x4_pred_mode(h) < 0)
  4610. return -1;
  4611. }else{
  4612. h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
  4613. if(h->intra16x16_pred_mode < 0)
  4614. return -1;
  4615. }
  4616. pred_mode= check_intra_pred_mode(h, get_ue_golomb(&s->gb));
  4617. if(pred_mode < 0)
  4618. return -1;
  4619. h->chroma_pred_mode= pred_mode;
  4620. }else if(partition_count==4){
  4621. int i, j, sub_partition_count[4], list, ref[2][4];
  4622. if(h->slice_type == B_TYPE){
  4623. for(i=0; i<4; i++){
  4624. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4625. if(h->sub_mb_type[i] >=13){
  4626. av_log(h->s.avctx, AV_LOG_ERROR, "B sub_mb_type %u out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
  4627. return -1;
  4628. }
  4629. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4630. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4631. }
  4632. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  4633. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
  4634. pred_direct_motion(h, &mb_type);
  4635. h->ref_cache[0][scan8[4]] =
  4636. h->ref_cache[1][scan8[4]] =
  4637. h->ref_cache[0][scan8[12]] =
  4638. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  4639. }
  4640. }else{
  4641. assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE); //FIXME SP correct ?
  4642. for(i=0; i<4; i++){
  4643. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4644. if(h->sub_mb_type[i] >=4){
  4645. av_log(h->s.avctx, AV_LOG_ERROR, "P sub_mb_type %u out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
  4646. return -1;
  4647. }
  4648. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4649. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4650. }
  4651. }
  4652. for(list=0; list<2; list++){
  4653. int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4654. if(ref_count == 0) continue;
  4655. for(i=0; i<4; i++){
  4656. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  4657. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4658. unsigned int tmp = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
  4659. if(tmp>=ref_count){
  4660. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", tmp);
  4661. return -1;
  4662. }
  4663. ref[list][i]= tmp;
  4664. }else{
  4665. //FIXME
  4666. ref[list][i] = -1;
  4667. }
  4668. }
  4669. }
  4670. if(dct8x8_allowed)
  4671. dct8x8_allowed = get_dct8x8_allowed(h);
  4672. for(list=0; list<2; list++){
  4673. const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4674. if(ref_count == 0) continue;
  4675. for(i=0; i<4; i++){
  4676. if(IS_DIRECT(h->sub_mb_type[i])) {
  4677. h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];
  4678. continue;
  4679. }
  4680. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  4681. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  4682. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4683. const int sub_mb_type= h->sub_mb_type[i];
  4684. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  4685. for(j=0; j<sub_partition_count[i]; j++){
  4686. int mx, my;
  4687. const int index= 4*i + block_width*j;
  4688. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  4689. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  4690. mx += get_se_golomb(&s->gb);
  4691. my += get_se_golomb(&s->gb);
  4692. tprintf("final mv:%d %d\n", mx, my);
  4693. if(IS_SUB_8X8(sub_mb_type)){
  4694. mv_cache[ 1 ][0]=
  4695. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  4696. mv_cache[ 1 ][1]=
  4697. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  4698. }else if(IS_SUB_8X4(sub_mb_type)){
  4699. mv_cache[ 1 ][0]= mx;
  4700. mv_cache[ 1 ][1]= my;
  4701. }else if(IS_SUB_4X8(sub_mb_type)){
  4702. mv_cache[ 8 ][0]= mx;
  4703. mv_cache[ 8 ][1]= my;
  4704. }
  4705. mv_cache[ 0 ][0]= mx;
  4706. mv_cache[ 0 ][1]= my;
  4707. }
  4708. }else{
  4709. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  4710. p[0] = p[1]=
  4711. p[8] = p[9]= 0;
  4712. }
  4713. }
  4714. }
  4715. }else if(IS_DIRECT(mb_type)){
  4716. pred_direct_motion(h, &mb_type);
  4717. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  4718. }else{
  4719. int list, mx, my, i;
  4720. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  4721. if(IS_16X16(mb_type)){
  4722. for(list=0; list<2; list++){
  4723. if(h->ref_count[list]>0){
  4724. if(IS_DIR(mb_type, 0, list)){
  4725. unsigned int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4726. if(val >= h->ref_count[list]){
  4727. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4728. return -1;
  4729. }
  4730. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  4731. }else
  4732. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4733. }
  4734. }
  4735. for(list=0; list<2; list++){
  4736. if(IS_DIR(mb_type, 0, list)){
  4737. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  4738. mx += get_se_golomb(&s->gb);
  4739. my += get_se_golomb(&s->gb);
  4740. tprintf("final mv:%d %d\n", mx, my);
  4741. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  4742. }else
  4743. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  4744. }
  4745. }
  4746. else if(IS_16X8(mb_type)){
  4747. for(list=0; list<2; list++){
  4748. if(h->ref_count[list]>0){
  4749. for(i=0; i<2; i++){
  4750. if(IS_DIR(mb_type, i, list)){
  4751. unsigned int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4752. if(val >= h->ref_count[list]){
  4753. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4754. return -1;
  4755. }
  4756. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  4757. }else
  4758. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  4759. }
  4760. }
  4761. }
  4762. for(list=0; list<2; list++){
  4763. for(i=0; i<2; i++){
  4764. if(IS_DIR(mb_type, i, list)){
  4765. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  4766. mx += get_se_golomb(&s->gb);
  4767. my += get_se_golomb(&s->gb);
  4768. tprintf("final mv:%d %d\n", mx, my);
  4769. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  4770. }else
  4771. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  4772. }
  4773. }
  4774. }else{
  4775. assert(IS_8X16(mb_type));
  4776. for(list=0; list<2; list++){
  4777. if(h->ref_count[list]>0){
  4778. for(i=0; i<2; i++){
  4779. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  4780. unsigned int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4781. if(val >= h->ref_count[list]){
  4782. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4783. return -1;
  4784. }
  4785. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  4786. }else
  4787. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4788. }
  4789. }
  4790. }
  4791. for(list=0; list<2; list++){
  4792. for(i=0; i<2; i++){
  4793. if(IS_DIR(mb_type, i, list)){
  4794. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  4795. mx += get_se_golomb(&s->gb);
  4796. my += get_se_golomb(&s->gb);
  4797. tprintf("final mv:%d %d\n", mx, my);
  4798. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  4799. }else
  4800. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  4801. }
  4802. }
  4803. }
  4804. }
  4805. if(IS_INTER(mb_type))
  4806. write_back_motion(h, mb_type);
  4807. if(!IS_INTRA16x16(mb_type)){
  4808. cbp= get_ue_golomb(&s->gb);
  4809. if(cbp > 47){
  4810. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%u) at %d %d\n", cbp, s->mb_x, s->mb_y);
  4811. return -1;
  4812. }
  4813. if(IS_INTRA4x4(mb_type))
  4814. cbp= golomb_to_intra4x4_cbp[cbp];
  4815. else
  4816. cbp= golomb_to_inter_cbp[cbp];
  4817. }
  4818. h->cbp = cbp;
  4819. if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
  4820. if(get_bits1(&s->gb))
  4821. mb_type |= MB_TYPE_8x8DCT;
  4822. }
  4823. s->current_picture.mb_type[mb_xy]= mb_type;
  4824. if(cbp || IS_INTRA16x16(mb_type)){
  4825. int i8x8, i4x4, chroma_idx;
  4826. int chroma_qp, dquant;
  4827. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  4828. const uint8_t *scan, *scan8x8, *dc_scan;
  4829. // fill_non_zero_count_cache(h);
  4830. if(IS_INTERLACED(mb_type)){
  4831. scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
  4832. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  4833. dc_scan= luma_dc_field_scan;
  4834. }else{
  4835. scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
  4836. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  4837. dc_scan= luma_dc_zigzag_scan;
  4838. }
  4839. dquant= get_se_golomb(&s->gb);
  4840. if( dquant > 25 || dquant < -26 ){
  4841. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  4842. return -1;
  4843. }
  4844. s->qscale += dquant;
  4845. if(((unsigned)s->qscale) > 51){
  4846. if(s->qscale<0) s->qscale+= 52;
  4847. else s->qscale-= 52;
  4848. }
  4849. h->chroma_qp= chroma_qp= get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4850. if(IS_INTRA16x16(mb_type)){
  4851. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){
  4852. return -1; //FIXME continue if partitioned and other return -1 too
  4853. }
  4854. assert((cbp&15) == 0 || (cbp&15) == 15);
  4855. if(cbp&15){
  4856. for(i8x8=0; i8x8<4; i8x8++){
  4857. for(i4x4=0; i4x4<4; i4x4++){
  4858. const int index= i4x4 + 4*i8x8;
  4859. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){
  4860. return -1;
  4861. }
  4862. }
  4863. }
  4864. }else{
  4865. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  4866. }
  4867. }else{
  4868. for(i8x8=0; i8x8<4; i8x8++){
  4869. if(cbp & (1<<i8x8)){
  4870. if(IS_8x8DCT(mb_type)){
  4871. DCTELEM *buf = &h->mb[64*i8x8];
  4872. uint8_t *nnz;
  4873. for(i4x4=0; i4x4<4; i4x4++){
  4874. if( decode_residual(h, gb, buf, i4x4+4*i8x8, scan8x8+16*i4x4,
  4875. h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )
  4876. return -1;
  4877. }
  4878. nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4879. nnz[0] += nnz[1] + nnz[8] + nnz[9];
  4880. }else{
  4881. for(i4x4=0; i4x4<4; i4x4++){
  4882. const int index= i4x4 + 4*i8x8;
  4883. if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){
  4884. return -1;
  4885. }
  4886. }
  4887. }
  4888. }else{
  4889. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4890. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  4891. }
  4892. }
  4893. }
  4894. if(cbp&0x30){
  4895. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  4896. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){
  4897. return -1;
  4898. }
  4899. }
  4900. if(cbp&0x20){
  4901. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  4902. for(i4x4=0; i4x4<4; i4x4++){
  4903. const int index= 16 + 4*chroma_idx + i4x4;
  4904. 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){
  4905. return -1;
  4906. }
  4907. }
  4908. }
  4909. }else{
  4910. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4911. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4912. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4913. }
  4914. }else{
  4915. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4916. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  4917. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4918. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4919. }
  4920. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4921. write_back_non_zero_count(h);
  4922. if(MB_MBAFF){
  4923. h->ref_count[0] >>= 1;
  4924. h->ref_count[1] >>= 1;
  4925. }
  4926. return 0;
  4927. }
  4928. static int decode_cabac_field_decoding_flag(H264Context *h) {
  4929. MpegEncContext * const s = &h->s;
  4930. const int mb_x = s->mb_x;
  4931. const int mb_y = s->mb_y & ~1;
  4932. const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
  4933. const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
  4934. unsigned int ctx = 0;
  4935. if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
  4936. ctx += 1;
  4937. }
  4938. if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
  4939. ctx += 1;
  4940. }
  4941. return get_cabac_noinline( &h->cabac, &h->cabac_state[70 + ctx] );
  4942. }
  4943. static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
  4944. uint8_t *state= &h->cabac_state[ctx_base];
  4945. int mb_type;
  4946. if(intra_slice){
  4947. MpegEncContext * const s = &h->s;
  4948. const int mba_xy = h->left_mb_xy[0];
  4949. const int mbb_xy = h->top_mb_xy;
  4950. int ctx=0;
  4951. if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
  4952. ctx++;
  4953. if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
  4954. ctx++;
  4955. if( get_cabac_noinline( &h->cabac, &state[ctx] ) == 0 )
  4956. return 0; /* I4x4 */
  4957. state += 2;
  4958. }else{
  4959. if( get_cabac_noinline( &h->cabac, &state[0] ) == 0 )
  4960. return 0; /* I4x4 */
  4961. }
  4962. if( get_cabac_terminate( &h->cabac ) )
  4963. return 25; /* PCM */
  4964. mb_type = 1; /* I16x16 */
  4965. mb_type += 12 * get_cabac_noinline( &h->cabac, &state[1] ); /* cbp_luma != 0 */
  4966. if( get_cabac_noinline( &h->cabac, &state[2] ) ) /* cbp_chroma */
  4967. mb_type += 4 + 4 * get_cabac_noinline( &h->cabac, &state[2+intra_slice] );
  4968. mb_type += 2 * get_cabac_noinline( &h->cabac, &state[3+intra_slice] );
  4969. mb_type += 1 * get_cabac_noinline( &h->cabac, &state[3+2*intra_slice] );
  4970. return mb_type;
  4971. }
  4972. static int decode_cabac_mb_type( H264Context *h ) {
  4973. MpegEncContext * const s = &h->s;
  4974. if( h->slice_type == I_TYPE ) {
  4975. return decode_cabac_intra_mb_type(h, 3, 1);
  4976. } else if( h->slice_type == P_TYPE ) {
  4977. if( get_cabac_noinline( &h->cabac, &h->cabac_state[14] ) == 0 ) {
  4978. /* P-type */
  4979. if( get_cabac_noinline( &h->cabac, &h->cabac_state[15] ) == 0 ) {
  4980. /* P_L0_D16x16, P_8x8 */
  4981. return 3 * get_cabac_noinline( &h->cabac, &h->cabac_state[16] );
  4982. } else {
  4983. /* P_L0_D8x16, P_L0_D16x8 */
  4984. return 2 - get_cabac_noinline( &h->cabac, &h->cabac_state[17] );
  4985. }
  4986. } else {
  4987. return decode_cabac_intra_mb_type(h, 17, 0) + 5;
  4988. }
  4989. } else if( h->slice_type == B_TYPE ) {
  4990. const int mba_xy = h->left_mb_xy[0];
  4991. const int mbb_xy = h->top_mb_xy;
  4992. int ctx = 0;
  4993. int bits;
  4994. if( h->slice_table[mba_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
  4995. ctx++;
  4996. if( h->slice_table[mbb_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
  4997. ctx++;
  4998. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+ctx] ) )
  4999. return 0; /* B_Direct_16x16 */
  5000. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+3] ) ) {
  5001. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
  5002. }
  5003. bits = get_cabac_noinline( &h->cabac, &h->cabac_state[27+4] ) << 3;
  5004. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 2;
  5005. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 1;
  5006. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  5007. if( bits < 8 )
  5008. return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
  5009. else if( bits == 13 ) {
  5010. return decode_cabac_intra_mb_type(h, 32, 0) + 23;
  5011. } else if( bits == 14 )
  5012. return 11; /* B_L1_L0_8x16 */
  5013. else if( bits == 15 )
  5014. return 22; /* B_8x8 */
  5015. bits= ( bits<<1 ) | get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  5016. return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
  5017. } else {
  5018. /* TODO SI/SP frames? */
  5019. return -1;
  5020. }
  5021. }
  5022. static int decode_cabac_mb_skip( H264Context *h, int mb_x, int mb_y ) {
  5023. MpegEncContext * const s = &h->s;
  5024. int mba_xy, mbb_xy;
  5025. int ctx = 0;
  5026. if(FRAME_MBAFF){ //FIXME merge with the stuff in fill_caches?
  5027. int mb_xy = mb_x + (mb_y&~1)*s->mb_stride;
  5028. mba_xy = mb_xy - 1;
  5029. if( (mb_y&1)
  5030. && h->slice_table[mba_xy] == h->slice_num
  5031. && MB_FIELD == !!IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) )
  5032. mba_xy += s->mb_stride;
  5033. if( MB_FIELD ){
  5034. mbb_xy = mb_xy - s->mb_stride;
  5035. if( !(mb_y&1)
  5036. && h->slice_table[mbb_xy] == h->slice_num
  5037. && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) )
  5038. mbb_xy -= s->mb_stride;
  5039. }else
  5040. mbb_xy = mb_x + (mb_y-1)*s->mb_stride;
  5041. }else{
  5042. int mb_xy = mb_x + mb_y*s->mb_stride;
  5043. mba_xy = mb_xy - 1;
  5044. mbb_xy = mb_xy - s->mb_stride;
  5045. }
  5046. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
  5047. ctx++;
  5048. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
  5049. ctx++;
  5050. if( h->slice_type == B_TYPE )
  5051. ctx += 13;
  5052. return get_cabac_noinline( &h->cabac, &h->cabac_state[11+ctx] );
  5053. }
  5054. static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
  5055. int mode = 0;
  5056. if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
  5057. return pred_mode;
  5058. mode += 1 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5059. mode += 2 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5060. mode += 4 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5061. if( mode >= pred_mode )
  5062. return mode + 1;
  5063. else
  5064. return mode;
  5065. }
  5066. static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
  5067. const int mba_xy = h->left_mb_xy[0];
  5068. const int mbb_xy = h->top_mb_xy;
  5069. int ctx = 0;
  5070. /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
  5071. if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
  5072. ctx++;
  5073. if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
  5074. ctx++;
  5075. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
  5076. return 0;
  5077. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  5078. return 1;
  5079. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  5080. return 2;
  5081. else
  5082. return 3;
  5083. }
  5084. static const uint8_t block_idx_x[16] = {
  5085. 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
  5086. };
  5087. static const uint8_t block_idx_y[16] = {
  5088. 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
  5089. };
  5090. static const uint8_t block_idx_xy[4][4] = {
  5091. { 0, 2, 8, 10},
  5092. { 1, 3, 9, 11},
  5093. { 4, 6, 12, 14},
  5094. { 5, 7, 13, 15}
  5095. };
  5096. static int decode_cabac_mb_cbp_luma( H264Context *h) {
  5097. int cbp = 0;
  5098. int cbp_b = -1;
  5099. int i8x8;
  5100. if( h->slice_table[h->top_mb_xy] == h->slice_num ) {
  5101. cbp_b = h->top_cbp;
  5102. tprintf("cbp_b = top_cbp = %x\n", cbp_b);
  5103. }
  5104. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5105. int cbp_a = -1;
  5106. int x, y;
  5107. int ctx = 0;
  5108. x = block_idx_x[4*i8x8];
  5109. y = block_idx_y[4*i8x8];
  5110. if( x > 0 )
  5111. cbp_a = cbp;
  5112. else if( h->slice_table[h->left_mb_xy[0]] == h->slice_num ) {
  5113. cbp_a = h->left_cbp;
  5114. tprintf("cbp_a = left_cbp = %x\n", cbp_a);
  5115. }
  5116. if( y > 0 )
  5117. cbp_b = cbp;
  5118. /* No need to test for skip as we put 0 for skip block */
  5119. /* No need to test for IPCM as we put 1 for IPCM block */
  5120. if( cbp_a >= 0 ) {
  5121. int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
  5122. if( ((cbp_a >> i8x8a)&0x01) == 0 )
  5123. ctx++;
  5124. }
  5125. if( cbp_b >= 0 ) {
  5126. int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
  5127. if( ((cbp_b >> i8x8b)&0x01) == 0 )
  5128. ctx += 2;
  5129. }
  5130. if( get_cabac( &h->cabac, &h->cabac_state[73 + ctx] ) ) {
  5131. cbp |= 1 << i8x8;
  5132. }
  5133. }
  5134. return cbp;
  5135. }
  5136. static int decode_cabac_mb_cbp_chroma( H264Context *h) {
  5137. int ctx;
  5138. int cbp_a, cbp_b;
  5139. cbp_a = (h->left_cbp>>4)&0x03;
  5140. cbp_b = (h-> top_cbp>>4)&0x03;
  5141. ctx = 0;
  5142. if( cbp_a > 0 ) ctx++;
  5143. if( cbp_b > 0 ) ctx += 2;
  5144. if( get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
  5145. return 0;
  5146. ctx = 4;
  5147. if( cbp_a == 2 ) ctx++;
  5148. if( cbp_b == 2 ) ctx += 2;
  5149. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] );
  5150. }
  5151. static int decode_cabac_mb_dqp( H264Context *h) {
  5152. MpegEncContext * const s = &h->s;
  5153. int mbn_xy;
  5154. int ctx = 0;
  5155. int val = 0;
  5156. if( s->mb_x > 0 )
  5157. mbn_xy = s->mb_x + s->mb_y*s->mb_stride - 1;
  5158. else
  5159. mbn_xy = s->mb_width - 1 + (s->mb_y-1)*s->mb_stride;
  5160. if( h->last_qscale_diff != 0 )
  5161. ctx++;
  5162. while( get_cabac_noinline( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
  5163. if( ctx < 2 )
  5164. ctx = 2;
  5165. else
  5166. ctx = 3;
  5167. val++;
  5168. if(val > 102) //prevent infinite loop
  5169. return INT_MIN;
  5170. }
  5171. if( val&0x01 )
  5172. return (val + 1)/2;
  5173. else
  5174. return -(val + 1)/2;
  5175. }
  5176. static int decode_cabac_p_mb_sub_type( H264Context *h ) {
  5177. if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
  5178. return 0; /* 8x8 */
  5179. if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
  5180. return 1; /* 8x4 */
  5181. if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
  5182. return 2; /* 4x8 */
  5183. return 3; /* 4x4 */
  5184. }
  5185. static int decode_cabac_b_mb_sub_type( H264Context *h ) {
  5186. int type;
  5187. if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
  5188. return 0; /* B_Direct_8x8 */
  5189. if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
  5190. return 1 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */
  5191. type = 3;
  5192. if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
  5193. if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
  5194. return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
  5195. type += 4;
  5196. }
  5197. type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
  5198. type += get_cabac( &h->cabac, &h->cabac_state[39] );
  5199. return type;
  5200. }
  5201. static inline int decode_cabac_mb_transform_size( H264Context *h ) {
  5202. return get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
  5203. }
  5204. static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
  5205. int refa = h->ref_cache[list][scan8[n] - 1];
  5206. int refb = h->ref_cache[list][scan8[n] - 8];
  5207. int ref = 0;
  5208. int ctx = 0;
  5209. if( h->slice_type == B_TYPE) {
  5210. if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
  5211. ctx++;
  5212. if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
  5213. ctx += 2;
  5214. } else {
  5215. if( refa > 0 )
  5216. ctx++;
  5217. if( refb > 0 )
  5218. ctx += 2;
  5219. }
  5220. while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
  5221. ref++;
  5222. if( ctx < 4 )
  5223. ctx = 4;
  5224. else
  5225. ctx = 5;
  5226. if(ref >= 32 /*h->ref_list[list]*/){
  5227. av_log(h->s.avctx, AV_LOG_ERROR, "overflow in decode_cabac_mb_ref\n");
  5228. return 0; //FIXME we should return -1 and check the return everywhere
  5229. }
  5230. }
  5231. return ref;
  5232. }
  5233. static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
  5234. int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
  5235. abs( h->mvd_cache[list][scan8[n] - 8][l] );
  5236. int ctxbase = (l == 0) ? 40 : 47;
  5237. int ctx, mvd;
  5238. if( amvd < 3 )
  5239. ctx = 0;
  5240. else if( amvd > 32 )
  5241. ctx = 2;
  5242. else
  5243. ctx = 1;
  5244. if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
  5245. return 0;
  5246. mvd= 1;
  5247. ctx= 3;
  5248. while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
  5249. mvd++;
  5250. if( ctx < 6 )
  5251. ctx++;
  5252. }
  5253. if( mvd >= 9 ) {
  5254. int k = 3;
  5255. while( get_cabac_bypass( &h->cabac ) ) {
  5256. mvd += 1 << k;
  5257. k++;
  5258. if(k>24){
  5259. av_log(h->s.avctx, AV_LOG_ERROR, "overflow in decode_cabac_mb_mvd\n");
  5260. return INT_MIN;
  5261. }
  5262. }
  5263. while( k-- ) {
  5264. if( get_cabac_bypass( &h->cabac ) )
  5265. mvd += 1 << k;
  5266. }
  5267. }
  5268. return get_cabac_bypass_sign( &h->cabac, -mvd );
  5269. }
  5270. static int inline get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) {
  5271. int nza, nzb;
  5272. int ctx = 0;
  5273. if( cat == 0 ) {
  5274. nza = h->left_cbp&0x100;
  5275. nzb = h-> top_cbp&0x100;
  5276. } else if( cat == 1 || cat == 2 ) {
  5277. nza = h->non_zero_count_cache[scan8[idx] - 1];
  5278. nzb = h->non_zero_count_cache[scan8[idx] - 8];
  5279. } else if( cat == 3 ) {
  5280. nza = (h->left_cbp>>(6+idx))&0x01;
  5281. nzb = (h-> top_cbp>>(6+idx))&0x01;
  5282. } else {
  5283. assert(cat == 4);
  5284. nza = h->non_zero_count_cache[scan8[16+idx] - 1];
  5285. nzb = h->non_zero_count_cache[scan8[16+idx] - 8];
  5286. }
  5287. if( nza > 0 )
  5288. ctx++;
  5289. if( nzb > 0 )
  5290. ctx += 2;
  5291. return ctx + 4 * cat;
  5292. }
  5293. static const __attribute((used)) uint8_t last_coeff_flag_offset_8x8[63] = {
  5294. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5295. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  5296. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  5297. 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
  5298. };
  5299. static int decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff) {
  5300. const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
  5301. static const int significant_coeff_flag_offset[2][6] = {
  5302. { 105+0, 105+15, 105+29, 105+44, 105+47, 402 },
  5303. { 277+0, 277+15, 277+29, 277+44, 277+47, 436 }
  5304. };
  5305. static const int last_coeff_flag_offset[2][6] = {
  5306. { 166+0, 166+15, 166+29, 166+44, 166+47, 417 },
  5307. { 338+0, 338+15, 338+29, 338+44, 338+47, 451 }
  5308. };
  5309. static const int coeff_abs_level_m1_offset[6] = {
  5310. 227+0, 227+10, 227+20, 227+30, 227+39, 426
  5311. };
  5312. static const uint8_t significant_coeff_flag_offset_8x8[2][63] = {
  5313. { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
  5314. 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
  5315. 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
  5316. 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12 },
  5317. { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
  5318. 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
  5319. 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
  5320. 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }
  5321. };
  5322. int index[64];
  5323. int last;
  5324. int coeff_count = 0;
  5325. int abslevel1 = 1;
  5326. int abslevelgt1 = 0;
  5327. uint8_t *significant_coeff_ctx_base;
  5328. uint8_t *last_coeff_ctx_base;
  5329. uint8_t *abs_level_m1_ctx_base;
  5330. #ifndef ARCH_X86
  5331. #define CABAC_ON_STACK
  5332. #endif
  5333. #ifdef CABAC_ON_STACK
  5334. #define CC &cc
  5335. CABACContext cc;
  5336. cc.range = h->cabac.range;
  5337. cc.low = h->cabac.low;
  5338. cc.bytestream= h->cabac.bytestream;
  5339. #else
  5340. #define CC &h->cabac
  5341. #endif
  5342. /* cat: 0-> DC 16x16 n = 0
  5343. * 1-> AC 16x16 n = luma4x4idx
  5344. * 2-> Luma4x4 n = luma4x4idx
  5345. * 3-> DC Chroma n = iCbCr
  5346. * 4-> AC Chroma n = 4 * iCbCr + chroma4x4idx
  5347. * 5-> Luma8x8 n = 4 * luma8x8idx
  5348. */
  5349. /* read coded block flag */
  5350. if( cat != 5 ) {
  5351. if( get_cabac( CC, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n ) ] ) == 0 ) {
  5352. if( cat == 1 || cat == 2 )
  5353. h->non_zero_count_cache[scan8[n]] = 0;
  5354. else if( cat == 4 )
  5355. h->non_zero_count_cache[scan8[16+n]] = 0;
  5356. #ifdef CABAC_ON_STACK
  5357. h->cabac.range = cc.range ;
  5358. h->cabac.low = cc.low ;
  5359. h->cabac.bytestream= cc.bytestream;
  5360. #endif
  5361. return 0;
  5362. }
  5363. }
  5364. significant_coeff_ctx_base = h->cabac_state
  5365. + significant_coeff_flag_offset[MB_FIELD][cat];
  5366. last_coeff_ctx_base = h->cabac_state
  5367. + last_coeff_flag_offset[MB_FIELD][cat];
  5368. abs_level_m1_ctx_base = h->cabac_state
  5369. + coeff_abs_level_m1_offset[cat];
  5370. if( cat == 5 ) {
  5371. #define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \
  5372. for(last= 0; last < coefs; last++) { \
  5373. uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \
  5374. if( get_cabac( CC, sig_ctx )) { \
  5375. uint8_t *last_ctx = last_coeff_ctx_base + last_off; \
  5376. index[coeff_count++] = last; \
  5377. if( get_cabac( CC, last_ctx ) ) { \
  5378. last= max_coeff; \
  5379. break; \
  5380. } \
  5381. } \
  5382. }\
  5383. if( last == max_coeff -1 ) {\
  5384. index[coeff_count++] = last;\
  5385. }
  5386. const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
  5387. #if defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__))
  5388. coeff_count= decode_significance_8x8_x86(CC, significant_coeff_ctx_base, index, sig_off);
  5389. } else {
  5390. coeff_count= decode_significance_x86(CC, max_coeff, significant_coeff_ctx_base, index);
  5391. #else
  5392. DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );
  5393. } else {
  5394. DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
  5395. #endif
  5396. }
  5397. assert(coeff_count > 0);
  5398. if( cat == 0 )
  5399. h->cbp_table[mb_xy] |= 0x100;
  5400. else if( cat == 1 || cat == 2 )
  5401. h->non_zero_count_cache[scan8[n]] = coeff_count;
  5402. else if( cat == 3 )
  5403. h->cbp_table[mb_xy] |= 0x40 << n;
  5404. else if( cat == 4 )
  5405. h->non_zero_count_cache[scan8[16+n]] = coeff_count;
  5406. else {
  5407. assert( cat == 5 );
  5408. fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);
  5409. }
  5410. for( coeff_count--; coeff_count >= 0; coeff_count-- ) {
  5411. uint8_t *ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 )) + abs_level_m1_ctx_base;
  5412. int j= scantable[index[coeff_count]];
  5413. if( get_cabac( CC, ctx ) == 0 ) {
  5414. if( !qmul ) {
  5415. block[j] = get_cabac_bypass_sign( CC, -1);
  5416. }else{
  5417. block[j] = (get_cabac_bypass_sign( CC, -qmul[j]) + 32) >> 6;;
  5418. }
  5419. abslevel1++;
  5420. } else {
  5421. int coeff_abs = 2;
  5422. ctx = 5 + FFMIN( 4, abslevelgt1 ) + abs_level_m1_ctx_base;
  5423. while( coeff_abs < 15 && get_cabac( CC, ctx ) ) {
  5424. coeff_abs++;
  5425. }
  5426. if( coeff_abs >= 15 ) {
  5427. int j = 0;
  5428. while( get_cabac_bypass( CC ) ) {
  5429. j++;
  5430. }
  5431. coeff_abs=1;
  5432. while( j-- ) {
  5433. coeff_abs += coeff_abs + get_cabac_bypass( CC );
  5434. }
  5435. coeff_abs+= 14;
  5436. }
  5437. if( !qmul ) {
  5438. if( get_cabac_bypass( CC ) ) block[j] = -coeff_abs;
  5439. else block[j] = coeff_abs;
  5440. }else{
  5441. if( get_cabac_bypass( CC ) ) block[j] = (-coeff_abs * qmul[j] + 32) >> 6;
  5442. else block[j] = ( coeff_abs * qmul[j] + 32) >> 6;
  5443. }
  5444. abslevelgt1++;
  5445. }
  5446. }
  5447. #ifdef CABAC_ON_STACK
  5448. h->cabac.range = cc.range ;
  5449. h->cabac.low = cc.low ;
  5450. h->cabac.bytestream= cc.bytestream;
  5451. #endif
  5452. return 0;
  5453. }
  5454. static void inline compute_mb_neighbors(H264Context *h)
  5455. {
  5456. MpegEncContext * const s = &h->s;
  5457. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  5458. h->top_mb_xy = mb_xy - s->mb_stride;
  5459. h->left_mb_xy[0] = mb_xy - 1;
  5460. if(FRAME_MBAFF){
  5461. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  5462. const int top_pair_xy = pair_xy - s->mb_stride;
  5463. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  5464. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  5465. const int curr_mb_frame_flag = !MB_FIELD;
  5466. const int bottom = (s->mb_y & 1);
  5467. if (bottom
  5468. ? !curr_mb_frame_flag // bottom macroblock
  5469. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  5470. ) {
  5471. h->top_mb_xy -= s->mb_stride;
  5472. }
  5473. if (left_mb_frame_flag != curr_mb_frame_flag) {
  5474. h->left_mb_xy[0] = pair_xy - 1;
  5475. }
  5476. }
  5477. return;
  5478. }
  5479. /**
  5480. * decodes a macroblock
  5481. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  5482. */
  5483. static int decode_mb_cabac(H264Context *h) {
  5484. MpegEncContext * const s = &h->s;
  5485. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  5486. int mb_type, partition_count, cbp = 0;
  5487. int dct8x8_allowed= h->pps.transform_8x8_mode;
  5488. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?)
  5489. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  5490. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) {
  5491. int skip;
  5492. /* a skipped mb needs the aff flag from the following mb */
  5493. if( FRAME_MBAFF && s->mb_x==0 && (s->mb_y&1)==0 )
  5494. predict_field_decoding_flag(h);
  5495. if( FRAME_MBAFF && (s->mb_y&1)==1 && h->prev_mb_skipped )
  5496. skip = h->next_mb_skipped;
  5497. else
  5498. skip = decode_cabac_mb_skip( h, s->mb_x, s->mb_y );
  5499. /* read skip flags */
  5500. if( skip ) {
  5501. if( FRAME_MBAFF && (s->mb_y&1)==0 ){
  5502. s->current_picture.mb_type[mb_xy] = MB_TYPE_SKIP;
  5503. h->next_mb_skipped = decode_cabac_mb_skip( h, s->mb_x, s->mb_y+1 );
  5504. if(h->next_mb_skipped)
  5505. predict_field_decoding_flag(h);
  5506. else
  5507. h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5508. }
  5509. decode_mb_skip(h);
  5510. h->cbp_table[mb_xy] = 0;
  5511. h->chroma_pred_mode_table[mb_xy] = 0;
  5512. h->last_qscale_diff = 0;
  5513. return 0;
  5514. }
  5515. }
  5516. if(FRAME_MBAFF){
  5517. if( (s->mb_y&1) == 0 )
  5518. h->mb_mbaff =
  5519. h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5520. }else
  5521. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  5522. h->prev_mb_skipped = 0;
  5523. compute_mb_neighbors(h);
  5524. if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
  5525. av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
  5526. return -1;
  5527. }
  5528. if( h->slice_type == B_TYPE ) {
  5529. if( mb_type < 23 ){
  5530. partition_count= b_mb_type_info[mb_type].partition_count;
  5531. mb_type= b_mb_type_info[mb_type].type;
  5532. }else{
  5533. mb_type -= 23;
  5534. goto decode_intra_mb;
  5535. }
  5536. } else if( h->slice_type == P_TYPE ) {
  5537. if( mb_type < 5) {
  5538. partition_count= p_mb_type_info[mb_type].partition_count;
  5539. mb_type= p_mb_type_info[mb_type].type;
  5540. } else {
  5541. mb_type -= 5;
  5542. goto decode_intra_mb;
  5543. }
  5544. } else {
  5545. assert(h->slice_type == I_TYPE);
  5546. decode_intra_mb:
  5547. partition_count = 0;
  5548. cbp= i_mb_type_info[mb_type].cbp;
  5549. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  5550. mb_type= i_mb_type_info[mb_type].type;
  5551. }
  5552. if(MB_FIELD)
  5553. mb_type |= MB_TYPE_INTERLACED;
  5554. h->slice_table[ mb_xy ]= h->slice_num;
  5555. if(IS_INTRA_PCM(mb_type)) {
  5556. const uint8_t *ptr;
  5557. unsigned int x, y;
  5558. // We assume these blocks are very rare so we dont optimize it.
  5559. // FIXME The two following lines get the bitstream position in the cabac
  5560. // decode, I think it should be done by a function in cabac.h (or cabac.c).
  5561. ptr= h->cabac.bytestream;
  5562. if(h->cabac.low&0x1) ptr--;
  5563. if(CABAC_BITS==16){
  5564. if(h->cabac.low&0x1FF) ptr--;
  5565. }
  5566. // The pixels are stored in the same order as levels in h->mb array.
  5567. for(y=0; y<16; y++){
  5568. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  5569. for(x=0; x<16; x++){
  5570. tprintf("LUMA ICPM LEVEL (%3d)\n", *ptr);
  5571. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= *ptr++;
  5572. }
  5573. }
  5574. for(y=0; y<8; y++){
  5575. const int index= 256 + 4*(y&3) + 32*(y>>2);
  5576. for(x=0; x<8; x++){
  5577. tprintf("CHROMA U ICPM LEVEL (%3d)\n", *ptr);
  5578. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5579. }
  5580. }
  5581. for(y=0; y<8; y++){
  5582. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  5583. for(x=0; x<8; x++){
  5584. tprintf("CHROMA V ICPM LEVEL (%3d)\n", *ptr);
  5585. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5586. }
  5587. }
  5588. ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);
  5589. // All blocks are present
  5590. h->cbp_table[mb_xy] = 0x1ef;
  5591. h->chroma_pred_mode_table[mb_xy] = 0;
  5592. // In deblocking, the quantizer is 0
  5593. s->current_picture.qscale_table[mb_xy]= 0;
  5594. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  5595. // All coeffs are present
  5596. memset(h->non_zero_count[mb_xy], 16, 16);
  5597. s->current_picture.mb_type[mb_xy]= mb_type;
  5598. return 0;
  5599. }
  5600. if(MB_MBAFF){
  5601. h->ref_count[0] <<= 1;
  5602. h->ref_count[1] <<= 1;
  5603. }
  5604. fill_caches(h, mb_type, 0);
  5605. if( IS_INTRA( mb_type ) ) {
  5606. int i, pred_mode;
  5607. if( IS_INTRA4x4( mb_type ) ) {
  5608. if( dct8x8_allowed && decode_cabac_mb_transform_size( h ) ) {
  5609. mb_type |= MB_TYPE_8x8DCT;
  5610. for( i = 0; i < 16; i+=4 ) {
  5611. int pred = pred_intra_mode( h, i );
  5612. int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5613. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  5614. }
  5615. } else {
  5616. for( i = 0; i < 16; i++ ) {
  5617. int pred = pred_intra_mode( h, i );
  5618. h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5619. //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
  5620. }
  5621. }
  5622. write_back_intra_pred_mode(h);
  5623. if( check_intra4x4_pred_mode(h) < 0 ) return -1;
  5624. } else {
  5625. h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );
  5626. if( h->intra16x16_pred_mode < 0 ) return -1;
  5627. }
  5628. h->chroma_pred_mode_table[mb_xy] =
  5629. pred_mode = decode_cabac_mb_chroma_pre_mode( h );
  5630. pred_mode= check_intra_pred_mode( h, pred_mode );
  5631. if( pred_mode < 0 ) return -1;
  5632. h->chroma_pred_mode= pred_mode;
  5633. } else if( partition_count == 4 ) {
  5634. int i, j, sub_partition_count[4], list, ref[2][4];
  5635. if( h->slice_type == B_TYPE ) {
  5636. for( i = 0; i < 4; i++ ) {
  5637. h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
  5638. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5639. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5640. }
  5641. if( IS_DIRECT(h->sub_mb_type[0] | h->sub_mb_type[1] |
  5642. h->sub_mb_type[2] | h->sub_mb_type[3]) ) {
  5643. pred_direct_motion(h, &mb_type);
  5644. if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
  5645. for( i = 0; i < 4; i++ )
  5646. if( IS_DIRECT(h->sub_mb_type[i]) )
  5647. fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );
  5648. }
  5649. }
  5650. } else {
  5651. for( i = 0; i < 4; i++ ) {
  5652. h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
  5653. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5654. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5655. }
  5656. }
  5657. for( list = 0; list < 2; list++ ) {
  5658. if( h->ref_count[list] > 0 ) {
  5659. for( i = 0; i < 4; i++ ) {
  5660. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  5661. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  5662. if( h->ref_count[list] > 1 )
  5663. ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );
  5664. else
  5665. ref[list][i] = 0;
  5666. } else {
  5667. ref[list][i] = -1;
  5668. }
  5669. h->ref_cache[list][ scan8[4*i]+1 ]=
  5670. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  5671. }
  5672. }
  5673. }
  5674. if(dct8x8_allowed)
  5675. dct8x8_allowed = get_dct8x8_allowed(h);
  5676. for(list=0; list<2; list++){
  5677. for(i=0; i<4; i++){
  5678. if(IS_DIRECT(h->sub_mb_type[i])){
  5679. fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);
  5680. continue;
  5681. }
  5682. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];
  5683. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  5684. const int sub_mb_type= h->sub_mb_type[i];
  5685. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  5686. for(j=0; j<sub_partition_count[i]; j++){
  5687. int mpx, mpy;
  5688. int mx, my;
  5689. const int index= 4*i + block_width*j;
  5690. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  5691. int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];
  5692. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);
  5693. mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );
  5694. my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );
  5695. tprintf("final mv:%d %d\n", mx, my);
  5696. if(IS_SUB_8X8(sub_mb_type)){
  5697. mv_cache[ 1 ][0]=
  5698. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  5699. mv_cache[ 1 ][1]=
  5700. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  5701. mvd_cache[ 1 ][0]=
  5702. mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;
  5703. mvd_cache[ 1 ][1]=
  5704. mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;
  5705. }else if(IS_SUB_8X4(sub_mb_type)){
  5706. mv_cache[ 1 ][0]= mx;
  5707. mv_cache[ 1 ][1]= my;
  5708. mvd_cache[ 1 ][0]= mx - mpx;
  5709. mvd_cache[ 1 ][1]= my - mpy;
  5710. }else if(IS_SUB_4X8(sub_mb_type)){
  5711. mv_cache[ 8 ][0]= mx;
  5712. mv_cache[ 8 ][1]= my;
  5713. mvd_cache[ 8 ][0]= mx - mpx;
  5714. mvd_cache[ 8 ][1]= my - mpy;
  5715. }
  5716. mv_cache[ 0 ][0]= mx;
  5717. mv_cache[ 0 ][1]= my;
  5718. mvd_cache[ 0 ][0]= mx - mpx;
  5719. mvd_cache[ 0 ][1]= my - mpy;
  5720. }
  5721. }else{
  5722. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  5723. uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];
  5724. p[0] = p[1] = p[8] = p[9] = 0;
  5725. pd[0]= pd[1]= pd[8]= pd[9]= 0;
  5726. }
  5727. }
  5728. }
  5729. } else if( IS_DIRECT(mb_type) ) {
  5730. pred_direct_motion(h, &mb_type);
  5731. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  5732. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  5733. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  5734. } else {
  5735. int list, mx, my, i, mpx, mpy;
  5736. if(IS_16X16(mb_type)){
  5737. for(list=0; list<2; list++){
  5738. if(IS_DIR(mb_type, 0, list)){
  5739. if(h->ref_count[list] > 0 ){
  5740. const int ref = h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 0 ) : 0;
  5741. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
  5742. }
  5743. }else
  5744. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  5745. }
  5746. for(list=0; list<2; list++){
  5747. if(IS_DIR(mb_type, 0, list)){
  5748. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);
  5749. mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );
  5750. my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );
  5751. tprintf("final mv:%d %d\n", mx, my);
  5752. fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5753. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  5754. }else
  5755. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  5756. }
  5757. }
  5758. else if(IS_16X8(mb_type)){
  5759. for(list=0; list<2; list++){
  5760. if(h->ref_count[list]>0){
  5761. for(i=0; i<2; i++){
  5762. if(IS_DIR(mb_type, i, list)){
  5763. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 8*i ) : 0;
  5764. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
  5765. }else
  5766. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  5767. }
  5768. }
  5769. }
  5770. for(list=0; list<2; list++){
  5771. for(i=0; i<2; i++){
  5772. if(IS_DIR(mb_type, i, list)){
  5773. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);
  5774. mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );
  5775. my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );
  5776. tprintf("final mv:%d %d\n", mx, my);
  5777. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);
  5778. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  5779. }else{
  5780. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5781. fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5782. }
  5783. }
  5784. }
  5785. }else{
  5786. assert(IS_8X16(mb_type));
  5787. for(list=0; list<2; list++){
  5788. if(h->ref_count[list]>0){
  5789. for(i=0; i<2; i++){
  5790. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  5791. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 4*i ) : 0;
  5792. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
  5793. }else
  5794. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  5795. }
  5796. }
  5797. }
  5798. for(list=0; list<2; list++){
  5799. for(i=0; i<2; i++){
  5800. if(IS_DIR(mb_type, i, list)){
  5801. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);
  5802. mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );
  5803. my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );
  5804. tprintf("final mv:%d %d\n", mx, my);
  5805. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5806. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  5807. }else{
  5808. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5809. fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5810. }
  5811. }
  5812. }
  5813. }
  5814. }
  5815. if( IS_INTER( mb_type ) ) {
  5816. h->chroma_pred_mode_table[mb_xy] = 0;
  5817. write_back_motion( h, mb_type );
  5818. }
  5819. if( !IS_INTRA16x16( mb_type ) ) {
  5820. cbp = decode_cabac_mb_cbp_luma( h );
  5821. cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
  5822. }
  5823. h->cbp_table[mb_xy] = h->cbp = cbp;
  5824. if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
  5825. if( decode_cabac_mb_transform_size( h ) )
  5826. mb_type |= MB_TYPE_8x8DCT;
  5827. }
  5828. s->current_picture.mb_type[mb_xy]= mb_type;
  5829. if( cbp || IS_INTRA16x16( mb_type ) ) {
  5830. const uint8_t *scan, *scan8x8, *dc_scan;
  5831. int dqp;
  5832. if(IS_INTERLACED(mb_type)){
  5833. scan8x8= s->qscale ? h->field_scan8x8 : h->field_scan8x8_q0;
  5834. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  5835. dc_scan= luma_dc_field_scan;
  5836. }else{
  5837. scan8x8= s->qscale ? h->zigzag_scan8x8 : h->zigzag_scan8x8_q0;
  5838. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  5839. dc_scan= luma_dc_zigzag_scan;
  5840. }
  5841. h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
  5842. if( dqp == INT_MIN ){
  5843. av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", s->mb_x, s->mb_y);
  5844. return -1;
  5845. }
  5846. s->qscale += dqp;
  5847. if(((unsigned)s->qscale) > 51){
  5848. if(s->qscale<0) s->qscale+= 52;
  5849. else s->qscale-= 52;
  5850. }
  5851. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  5852. if( IS_INTRA16x16( mb_type ) ) {
  5853. int i;
  5854. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
  5855. if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16) < 0)
  5856. return -1;
  5857. if( cbp&15 ) {
  5858. for( i = 0; i < 16; i++ ) {
  5859. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
  5860. if( decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 )
  5861. return -1;
  5862. }
  5863. } else {
  5864. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  5865. }
  5866. } else {
  5867. int i8x8, i4x4;
  5868. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5869. if( cbp & (1<<i8x8) ) {
  5870. if( IS_8x8DCT(mb_type) ) {
  5871. if( decode_cabac_residual(h, h->mb + 64*i8x8, 5, 4*i8x8,
  5872. scan8x8, h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 64) < 0 )
  5873. return -1;
  5874. } else
  5875. for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
  5876. const int index = 4*i8x8 + i4x4;
  5877. //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
  5878. //START_TIMER
  5879. if( decode_cabac_residual(h, h->mb + 16*index, 2, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) < 0 )
  5880. return -1;
  5881. //STOP_TIMER("decode_residual")
  5882. }
  5883. } else {
  5884. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  5885. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  5886. }
  5887. }
  5888. }
  5889. if( cbp&0x30 ){
  5890. int c;
  5891. for( c = 0; c < 2; c++ ) {
  5892. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
  5893. if( decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, NULL, 4) < 0)
  5894. return -1;
  5895. }
  5896. }
  5897. if( cbp&0x20 ) {
  5898. int c, i;
  5899. for( c = 0; c < 2; c++ ) {
  5900. for( i = 0; i < 4; i++ ) {
  5901. const int index = 16 + 4 * c + i;
  5902. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
  5903. 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)
  5904. return -1;
  5905. }
  5906. }
  5907. } else {
  5908. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5909. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5910. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5911. }
  5912. } else {
  5913. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5914. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  5915. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5916. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5917. h->last_qscale_diff = 0;
  5918. }
  5919. s->current_picture.qscale_table[mb_xy]= s->qscale;
  5920. write_back_non_zero_count(h);
  5921. if(MB_MBAFF){
  5922. h->ref_count[0] >>= 1;
  5923. h->ref_count[1] >>= 1;
  5924. }
  5925. return 0;
  5926. }
  5927. static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5928. int i, d;
  5929. const int index_a = qp + h->slice_alpha_c0_offset;
  5930. const int alpha = (alpha_table+52)[index_a];
  5931. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5932. if( bS[0] < 4 ) {
  5933. int8_t tc[4];
  5934. for(i=0; i<4; i++)
  5935. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  5936. h->s.dsp.h264_h_loop_filter_luma(pix, stride, alpha, beta, tc);
  5937. } else {
  5938. /* 16px edge length, because bS=4 is triggered by being at
  5939. * the edge of an intra MB, so all 4 bS are the same */
  5940. for( d = 0; d < 16; d++ ) {
  5941. const int p0 = pix[-1];
  5942. const int p1 = pix[-2];
  5943. const int p2 = pix[-3];
  5944. const int q0 = pix[0];
  5945. const int q1 = pix[1];
  5946. const int q2 = pix[2];
  5947. if( FFABS( p0 - q0 ) < alpha &&
  5948. FFABS( p1 - p0 ) < beta &&
  5949. FFABS( q1 - q0 ) < beta ) {
  5950. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5951. if( FFABS( p2 - p0 ) < beta)
  5952. {
  5953. const int p3 = pix[-4];
  5954. /* p0', p1', p2' */
  5955. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5956. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5957. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5958. } else {
  5959. /* p0' */
  5960. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5961. }
  5962. if( FFABS( q2 - q0 ) < beta)
  5963. {
  5964. const int q3 = pix[3];
  5965. /* q0', q1', q2' */
  5966. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5967. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5968. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5969. } else {
  5970. /* q0' */
  5971. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5972. }
  5973. }else{
  5974. /* p0', q0' */
  5975. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5976. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5977. }
  5978. 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]);
  5979. }
  5980. pix += stride;
  5981. }
  5982. }
  5983. }
  5984. static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5985. int i;
  5986. const int index_a = qp + h->slice_alpha_c0_offset;
  5987. const int alpha = (alpha_table+52)[index_a];
  5988. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5989. if( bS[0] < 4 ) {
  5990. int8_t tc[4];
  5991. for(i=0; i<4; i++)
  5992. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  5993. h->s.dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
  5994. } else {
  5995. h->s.dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
  5996. }
  5997. }
  5998. static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  5999. int i;
  6000. for( i = 0; i < 16; i++, pix += stride) {
  6001. int index_a;
  6002. int alpha;
  6003. int beta;
  6004. int qp_index;
  6005. int bS_index = (i >> 1);
  6006. if (!MB_FIELD) {
  6007. bS_index &= ~1;
  6008. bS_index |= (i & 1);
  6009. }
  6010. if( bS[bS_index] == 0 ) {
  6011. continue;
  6012. }
  6013. qp_index = MB_FIELD ? (i >> 3) : (i & 1);
  6014. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  6015. alpha = (alpha_table+52)[index_a];
  6016. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  6017. if( bS[bS_index] < 4 ) {
  6018. const int tc0 = (tc0_table+52)[index_a][bS[bS_index] - 1];
  6019. const int p0 = pix[-1];
  6020. const int p1 = pix[-2];
  6021. const int p2 = pix[-3];
  6022. const int q0 = pix[0];
  6023. const int q1 = pix[1];
  6024. const int q2 = pix[2];
  6025. if( FFABS( p0 - q0 ) < alpha &&
  6026. FFABS( p1 - p0 ) < beta &&
  6027. FFABS( q1 - q0 ) < beta ) {
  6028. int tc = tc0;
  6029. int i_delta;
  6030. if( FFABS( p2 - p0 ) < beta ) {
  6031. pix[-2] = p1 + clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  6032. tc++;
  6033. }
  6034. if( FFABS( q2 - q0 ) < beta ) {
  6035. pix[1] = q1 + clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  6036. tc++;
  6037. }
  6038. i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  6039. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  6040. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  6041. 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);
  6042. }
  6043. }else{
  6044. const int p0 = pix[-1];
  6045. const int p1 = pix[-2];
  6046. const int p2 = pix[-3];
  6047. const int q0 = pix[0];
  6048. const int q1 = pix[1];
  6049. const int q2 = pix[2];
  6050. if( FFABS( p0 - q0 ) < alpha &&
  6051. FFABS( p1 - p0 ) < beta &&
  6052. FFABS( q1 - q0 ) < beta ) {
  6053. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  6054. if( FFABS( p2 - p0 ) < beta)
  6055. {
  6056. const int p3 = pix[-4];
  6057. /* p0', p1', p2' */
  6058. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  6059. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  6060. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  6061. } else {
  6062. /* p0' */
  6063. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6064. }
  6065. if( FFABS( q2 - q0 ) < beta)
  6066. {
  6067. const int q3 = pix[3];
  6068. /* q0', q1', q2' */
  6069. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  6070. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  6071. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  6072. } else {
  6073. /* q0' */
  6074. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6075. }
  6076. }else{
  6077. /* p0', q0' */
  6078. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6079. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6080. }
  6081. 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]);
  6082. }
  6083. }
  6084. }
  6085. }
  6086. static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  6087. int i;
  6088. for( i = 0; i < 8; i++, pix += stride) {
  6089. int index_a;
  6090. int alpha;
  6091. int beta;
  6092. int qp_index;
  6093. int bS_index = i;
  6094. if( bS[bS_index] == 0 ) {
  6095. continue;
  6096. }
  6097. qp_index = MB_FIELD ? (i >> 2) : (i & 1);
  6098. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  6099. alpha = (alpha_table+52)[index_a];
  6100. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  6101. if( bS[bS_index] < 4 ) {
  6102. const int tc = (tc0_table+52)[index_a][bS[bS_index] - 1] + 1;
  6103. const int p0 = pix[-1];
  6104. const int p1 = pix[-2];
  6105. const int q0 = pix[0];
  6106. const int q1 = pix[1];
  6107. if( FFABS( p0 - q0 ) < alpha &&
  6108. FFABS( p1 - p0 ) < beta &&
  6109. FFABS( q1 - q0 ) < beta ) {
  6110. const int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  6111. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  6112. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  6113. 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);
  6114. }
  6115. }else{
  6116. const int p0 = pix[-1];
  6117. const int p1 = pix[-2];
  6118. const int q0 = pix[0];
  6119. const int q1 = pix[1];
  6120. if( FFABS( p0 - q0 ) < alpha &&
  6121. FFABS( p1 - p0 ) < beta &&
  6122. FFABS( q1 - q0 ) < beta ) {
  6123. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  6124. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  6125. 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]);
  6126. }
  6127. }
  6128. }
  6129. }
  6130. static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  6131. int i, d;
  6132. const int index_a = qp + h->slice_alpha_c0_offset;
  6133. const int alpha = (alpha_table+52)[index_a];
  6134. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  6135. const int pix_next = stride;
  6136. if( bS[0] < 4 ) {
  6137. int8_t tc[4];
  6138. for(i=0; i<4; i++)
  6139. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  6140. h->s.dsp.h264_v_loop_filter_luma(pix, stride, alpha, beta, tc);
  6141. } else {
  6142. /* 16px edge length, see filter_mb_edgev */
  6143. for( d = 0; d < 16; d++ ) {
  6144. const int p0 = pix[-1*pix_next];
  6145. const int p1 = pix[-2*pix_next];
  6146. const int p2 = pix[-3*pix_next];
  6147. const int q0 = pix[0];
  6148. const int q1 = pix[1*pix_next];
  6149. const int q2 = pix[2*pix_next];
  6150. if( FFABS( p0 - q0 ) < alpha &&
  6151. FFABS( p1 - p0 ) < beta &&
  6152. FFABS( q1 - q0 ) < beta ) {
  6153. const int p3 = pix[-4*pix_next];
  6154. const int q3 = pix[ 3*pix_next];
  6155. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  6156. if( FFABS( p2 - p0 ) < beta) {
  6157. /* p0', p1', p2' */
  6158. pix[-1*pix_next] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  6159. pix[-2*pix_next] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  6160. pix[-3*pix_next] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  6161. } else {
  6162. /* p0' */
  6163. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6164. }
  6165. if( FFABS( q2 - q0 ) < beta) {
  6166. /* q0', q1', q2' */
  6167. pix[0*pix_next] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  6168. pix[1*pix_next] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  6169. pix[2*pix_next] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  6170. } else {
  6171. /* q0' */
  6172. pix[0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6173. }
  6174. }else{
  6175. /* p0', q0' */
  6176. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6177. pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6178. }
  6179. 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]);
  6180. }
  6181. pix++;
  6182. }
  6183. }
  6184. }
  6185. static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  6186. int i;
  6187. const int index_a = qp + h->slice_alpha_c0_offset;
  6188. const int alpha = (alpha_table+52)[index_a];
  6189. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  6190. if( bS[0] < 4 ) {
  6191. int8_t tc[4];
  6192. for(i=0; i<4; i++)
  6193. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  6194. h->s.dsp.h264_v_loop_filter_chroma(pix, stride, alpha, beta, tc);
  6195. } else {
  6196. h->s.dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);
  6197. }
  6198. }
  6199. 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) {
  6200. MpegEncContext * const s = &h->s;
  6201. int mb_xy, mb_type;
  6202. int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh;
  6203. if(mb_x==0 || mb_y==0 || !s->dsp.h264_loop_filter_strength) {
  6204. filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);
  6205. return;
  6206. }
  6207. assert(!FRAME_MBAFF);
  6208. mb_xy = mb_x + mb_y*s->mb_stride;
  6209. mb_type = s->current_picture.mb_type[mb_xy];
  6210. qp = s->current_picture.qscale_table[mb_xy];
  6211. qp0 = s->current_picture.qscale_table[mb_xy-1];
  6212. qp1 = s->current_picture.qscale_table[h->top_mb_xy];
  6213. qpc = get_chroma_qp( h->pps.chroma_qp_index_offset, qp );
  6214. qpc0 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp0 );
  6215. qpc1 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp1 );
  6216. qp0 = (qp + qp0 + 1) >> 1;
  6217. qp1 = (qp + qp1 + 1) >> 1;
  6218. qpc0 = (qpc + qpc0 + 1) >> 1;
  6219. qpc1 = (qpc + qpc1 + 1) >> 1;
  6220. qp_thresh = 15 - h->slice_alpha_c0_offset;
  6221. if(qp <= qp_thresh && qp0 <= qp_thresh && qp1 <= qp_thresh &&
  6222. qpc <= qp_thresh && qpc0 <= qp_thresh && qpc1 <= qp_thresh)
  6223. return;
  6224. if( IS_INTRA(mb_type) ) {
  6225. int16_t bS4[4] = {4,4,4,4};
  6226. int16_t bS3[4] = {3,3,3,3};
  6227. if( IS_8x8DCT(mb_type) ) {
  6228. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  6229. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  6230. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  6231. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  6232. } else {
  6233. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  6234. filter_mb_edgev( h, &img_y[4*1], linesize, bS3, qp );
  6235. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  6236. filter_mb_edgev( h, &img_y[4*3], linesize, bS3, qp );
  6237. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  6238. filter_mb_edgeh( h, &img_y[4*1*linesize], linesize, bS3, qp );
  6239. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  6240. filter_mb_edgeh( h, &img_y[4*3*linesize], linesize, bS3, qp );
  6241. }
  6242. filter_mb_edgecv( h, &img_cb[2*0], uvlinesize, bS4, qpc0 );
  6243. filter_mb_edgecv( h, &img_cb[2*2], uvlinesize, bS3, qpc );
  6244. filter_mb_edgecv( h, &img_cr[2*0], uvlinesize, bS4, qpc0 );
  6245. filter_mb_edgecv( h, &img_cr[2*2], uvlinesize, bS3, qpc );
  6246. filter_mb_edgech( h, &img_cb[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  6247. filter_mb_edgech( h, &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc );
  6248. filter_mb_edgech( h, &img_cr[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  6249. filter_mb_edgech( h, &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc );
  6250. return;
  6251. } else {
  6252. DECLARE_ALIGNED_8(int16_t, bS[2][4][4]);
  6253. uint64_t (*bSv)[4] = (uint64_t(*)[4])bS;
  6254. int edges;
  6255. if( IS_8x8DCT(mb_type) && (h->cbp&7) == 7 ) {
  6256. edges = 4;
  6257. bSv[0][0] = bSv[0][2] = bSv[1][0] = bSv[1][2] = 0x0002000200020002ULL;
  6258. } else {
  6259. int mask_edge1 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 :
  6260. (mb_type & MB_TYPE_16x8) ? 1 : 0;
  6261. int mask_edge0 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16))
  6262. && (s->current_picture.mb_type[mb_xy-1] & (MB_TYPE_16x16 | MB_TYPE_8x16))
  6263. ? 3 : 0;
  6264. int step = IS_8x8DCT(mb_type) ? 2 : 1;
  6265. edges = (mb_type & MB_TYPE_16x16) && !(h->cbp & 15) ? 1 : 4;
  6266. s->dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,
  6267. (h->slice_type == B_TYPE), edges, step, mask_edge0, mask_edge1 );
  6268. }
  6269. if( IS_INTRA(s->current_picture.mb_type[mb_xy-1]) )
  6270. bSv[0][0] = 0x0004000400040004ULL;
  6271. if( IS_INTRA(s->current_picture.mb_type[h->top_mb_xy]) )
  6272. bSv[1][0] = 0x0004000400040004ULL;
  6273. #define FILTER(hv,dir,edge)\
  6274. if(bSv[dir][edge]) {\
  6275. filter_mb_edge##hv( h, &img_y[4*edge*(dir?linesize:1)], linesize, bS[dir][edge], edge ? qp : qp##dir );\
  6276. if(!(edge&1)) {\
  6277. filter_mb_edgec##hv( h, &img_cb[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  6278. filter_mb_edgec##hv( h, &img_cr[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  6279. }\
  6280. }
  6281. if( edges == 1 ) {
  6282. FILTER(v,0,0);
  6283. FILTER(h,1,0);
  6284. } else if( IS_8x8DCT(mb_type) ) {
  6285. FILTER(v,0,0);
  6286. FILTER(v,0,2);
  6287. FILTER(h,1,0);
  6288. FILTER(h,1,2);
  6289. } else {
  6290. FILTER(v,0,0);
  6291. FILTER(v,0,1);
  6292. FILTER(v,0,2);
  6293. FILTER(v,0,3);
  6294. FILTER(h,1,0);
  6295. FILTER(h,1,1);
  6296. FILTER(h,1,2);
  6297. FILTER(h,1,3);
  6298. }
  6299. #undef FILTER
  6300. }
  6301. }
  6302. 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) {
  6303. MpegEncContext * const s = &h->s;
  6304. const int mb_xy= mb_x + mb_y*s->mb_stride;
  6305. const int mb_type = s->current_picture.mb_type[mb_xy];
  6306. const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4;
  6307. int first_vertical_edge_done = 0;
  6308. int dir;
  6309. /* FIXME: A given frame may occupy more than one position in
  6310. * the reference list. So ref2frm should be populated with
  6311. * frame numbers, not indices. */
  6312. static const int ref2frm[34] = {-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  6313. 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
  6314. //for sufficiently low qp, filtering wouldn't do anything
  6315. //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
  6316. if(!FRAME_MBAFF){
  6317. int qp_thresh = 15 - h->slice_alpha_c0_offset - FFMAX(0, h->pps.chroma_qp_index_offset);
  6318. int qp = s->current_picture.qscale_table[mb_xy];
  6319. if(qp <= qp_thresh
  6320. && (mb_x == 0 || ((qp + s->current_picture.qscale_table[mb_xy-1] + 1)>>1) <= qp_thresh)
  6321. && (mb_y == 0 || ((qp + s->current_picture.qscale_table[h->top_mb_xy] + 1)>>1) <= qp_thresh)){
  6322. return;
  6323. }
  6324. }
  6325. if (FRAME_MBAFF
  6326. // left mb is in picture
  6327. && h->slice_table[mb_xy-1] != 255
  6328. // and current and left pair do not have the same interlaced type
  6329. && (IS_INTERLACED(mb_type) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
  6330. // and left mb is in the same slice if deblocking_filter == 2
  6331. && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
  6332. /* First vertical edge is different in MBAFF frames
  6333. * There are 8 different bS to compute and 2 different Qp
  6334. */
  6335. const int pair_xy = mb_x + (mb_y&~1)*s->mb_stride;
  6336. const int left_mb_xy[2] = { pair_xy-1, pair_xy-1+s->mb_stride };
  6337. int16_t bS[8];
  6338. int qp[2];
  6339. int chroma_qp[2];
  6340. int mb_qp, mbn0_qp, mbn1_qp;
  6341. int i;
  6342. first_vertical_edge_done = 1;
  6343. if( IS_INTRA(mb_type) )
  6344. bS[0] = bS[1] = bS[2] = bS[3] = bS[4] = bS[5] = bS[6] = bS[7] = 4;
  6345. else {
  6346. for( i = 0; i < 8; i++ ) {
  6347. int mbn_xy = MB_FIELD ? left_mb_xy[i>>2] : left_mb_xy[i&1];
  6348. if( IS_INTRA( s->current_picture.mb_type[mbn_xy] ) )
  6349. bS[i] = 4;
  6350. else if( h->non_zero_count_cache[12+8*(i>>1)] != 0 ||
  6351. /* FIXME: with 8x8dct + cavlc, should check cbp instead of nnz */
  6352. h->non_zero_count[mbn_xy][MB_FIELD ? i&3 : (i>>2)+(mb_y&1)*2] )
  6353. bS[i] = 2;
  6354. else
  6355. bS[i] = 1;
  6356. }
  6357. }
  6358. mb_qp = s->current_picture.qscale_table[mb_xy];
  6359. mbn0_qp = s->current_picture.qscale_table[left_mb_xy[0]];
  6360. mbn1_qp = s->current_picture.qscale_table[left_mb_xy[1]];
  6361. qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1;
  6362. chroma_qp[0] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6363. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn0_qp ) + 1 ) >> 1;
  6364. qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1;
  6365. chroma_qp[1] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6366. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn1_qp ) + 1 ) >> 1;
  6367. /* Filter edge */
  6368. 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);
  6369. { int i; for (i = 0; i < 8; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6370. filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp );
  6371. filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, chroma_qp );
  6372. filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, chroma_qp );
  6373. }
  6374. /* dir : 0 -> vertical edge, 1 -> horizontal edge */
  6375. for( dir = 0; dir < 2; dir++ )
  6376. {
  6377. int edge;
  6378. const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
  6379. const int mbm_type = s->current_picture.mb_type[mbm_xy];
  6380. int start = h->slice_table[mbm_xy] == 255 ? 1 : 0;
  6381. const int edges = (mb_type & (MB_TYPE_16x16|MB_TYPE_SKIP))
  6382. == (MB_TYPE_16x16|MB_TYPE_SKIP) ? 1 : 4;
  6383. // how often to recheck mv-based bS when iterating between edges
  6384. const int mask_edge = (mb_type & (MB_TYPE_16x16 | (MB_TYPE_16x8 << dir))) ? 3 :
  6385. (mb_type & (MB_TYPE_8x16 >> dir)) ? 1 : 0;
  6386. // how often to recheck mv-based bS when iterating along each edge
  6387. const int mask_par0 = mb_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir));
  6388. if (first_vertical_edge_done) {
  6389. start = 1;
  6390. first_vertical_edge_done = 0;
  6391. }
  6392. if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
  6393. start = 1;
  6394. if (FRAME_MBAFF && (dir == 1) && ((mb_y&1) == 0) && start == 0
  6395. && !IS_INTERLACED(mb_type)
  6396. && IS_INTERLACED(mbm_type)
  6397. ) {
  6398. // This is a special case in the norm where the filtering must
  6399. // be done twice (one each of the field) even if we are in a
  6400. // frame macroblock.
  6401. //
  6402. static const int nnz_idx[4] = {4,5,6,3};
  6403. unsigned int tmp_linesize = 2 * linesize;
  6404. unsigned int tmp_uvlinesize = 2 * uvlinesize;
  6405. int mbn_xy = mb_xy - 2 * s->mb_stride;
  6406. int qp, chroma_qp;
  6407. int i, j;
  6408. int16_t bS[4];
  6409. for(j=0; j<2; j++, mbn_xy += s->mb_stride){
  6410. if( IS_INTRA(mb_type) ||
  6411. IS_INTRA(s->current_picture.mb_type[mbn_xy]) ) {
  6412. bS[0] = bS[1] = bS[2] = bS[3] = 3;
  6413. } else {
  6414. const uint8_t *mbn_nnz = h->non_zero_count[mbn_xy];
  6415. for( i = 0; i < 4; i++ ) {
  6416. if( h->non_zero_count_cache[scan8[0]+i] != 0 ||
  6417. mbn_nnz[nnz_idx[i]] != 0 )
  6418. bS[i] = 2;
  6419. else
  6420. bS[i] = 1;
  6421. }
  6422. }
  6423. // Do not use s->qscale as luma quantizer because it has not the same
  6424. // value in IPCM macroblocks.
  6425. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6426. 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);
  6427. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6428. filter_mb_edgeh( h, &img_y[j*linesize], tmp_linesize, bS, qp );
  6429. chroma_qp = ( h->chroma_qp +
  6430. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6431. filter_mb_edgech( h, &img_cb[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6432. filter_mb_edgech( h, &img_cr[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6433. }
  6434. start = 1;
  6435. }
  6436. /* Calculate bS */
  6437. for( edge = start; edge < edges; edge++ ) {
  6438. /* mbn_xy: neighbor macroblock */
  6439. const int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
  6440. const int mbn_type = s->current_picture.mb_type[mbn_xy];
  6441. int16_t bS[4];
  6442. int qp;
  6443. if( (edge&1) && IS_8x8DCT(mb_type) )
  6444. continue;
  6445. if( IS_INTRA(mb_type) ||
  6446. IS_INTRA(mbn_type) ) {
  6447. int value;
  6448. if (edge == 0) {
  6449. if ( (!IS_INTERLACED(mb_type) && !IS_INTERLACED(mbm_type))
  6450. || ((FRAME_MBAFF || (s->picture_structure != PICT_FRAME)) && (dir == 0))
  6451. ) {
  6452. value = 4;
  6453. } else {
  6454. value = 3;
  6455. }
  6456. } else {
  6457. value = 3;
  6458. }
  6459. bS[0] = bS[1] = bS[2] = bS[3] = value;
  6460. } else {
  6461. int i, l;
  6462. int mv_done;
  6463. if( edge & mask_edge ) {
  6464. bS[0] = bS[1] = bS[2] = bS[3] = 0;
  6465. mv_done = 1;
  6466. }
  6467. else if( FRAME_MBAFF && IS_INTERLACED(mb_type ^ mbn_type)) {
  6468. bS[0] = bS[1] = bS[2] = bS[3] = 1;
  6469. mv_done = 1;
  6470. }
  6471. else if( mask_par0 && (edge || (mbn_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir)))) ) {
  6472. int b_idx= 8 + 4 + edge * (dir ? 8:1);
  6473. int bn_idx= b_idx - (dir ? 8:1);
  6474. int v = 0;
  6475. for( l = 0; !v && l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6476. v |= ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6477. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6478. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit;
  6479. }
  6480. bS[0] = bS[1] = bS[2] = bS[3] = v;
  6481. mv_done = 1;
  6482. }
  6483. else
  6484. mv_done = 0;
  6485. for( i = 0; i < 4; i++ ) {
  6486. int x = dir == 0 ? edge : i;
  6487. int y = dir == 0 ? i : edge;
  6488. int b_idx= 8 + 4 + x + 8*y;
  6489. int bn_idx= b_idx - (dir ? 8:1);
  6490. if( h->non_zero_count_cache[b_idx] != 0 ||
  6491. h->non_zero_count_cache[bn_idx] != 0 ) {
  6492. bS[i] = 2;
  6493. }
  6494. else if(!mv_done)
  6495. {
  6496. bS[i] = 0;
  6497. for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6498. if( ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6499. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6500. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit ) {
  6501. bS[i] = 1;
  6502. break;
  6503. }
  6504. }
  6505. }
  6506. }
  6507. if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
  6508. continue;
  6509. }
  6510. /* Filter edge */
  6511. // Do not use s->qscale as luma quantizer because it has not the same
  6512. // value in IPCM macroblocks.
  6513. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6514. //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]);
  6515. tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
  6516. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6517. if( dir == 0 ) {
  6518. filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
  6519. if( (edge&1) == 0 ) {
  6520. int chroma_qp = ( h->chroma_qp +
  6521. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6522. filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp );
  6523. filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp );
  6524. }
  6525. } else {
  6526. filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
  6527. if( (edge&1) == 0 ) {
  6528. int chroma_qp = ( h->chroma_qp +
  6529. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6530. filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6531. filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6532. }
  6533. }
  6534. }
  6535. }
  6536. }
  6537. static int decode_slice(H264Context *h){
  6538. MpegEncContext * const s = &h->s;
  6539. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  6540. s->mb_skip_run= -1;
  6541. if( h->pps.cabac ) {
  6542. int i;
  6543. /* realign */
  6544. align_get_bits( &s->gb );
  6545. /* init cabac */
  6546. ff_init_cabac_states( &h->cabac);
  6547. ff_init_cabac_decoder( &h->cabac,
  6548. s->gb.buffer + get_bits_count(&s->gb)/8,
  6549. ( s->gb.size_in_bits - get_bits_count(&s->gb) + 7)/8);
  6550. /* calculate pre-state */
  6551. for( i= 0; i < 460; i++ ) {
  6552. int pre;
  6553. if( h->slice_type == I_TYPE )
  6554. pre = clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
  6555. else
  6556. 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 );
  6557. if( pre <= 63 )
  6558. h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
  6559. else
  6560. h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
  6561. }
  6562. for(;;){
  6563. //START_TIMER
  6564. int ret = decode_mb_cabac(h);
  6565. int eos;
  6566. //STOP_TIMER("decode_mb_cabac")
  6567. if(ret>=0) hl_decode_mb(h);
  6568. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  6569. s->mb_y++;
  6570. if(ret>=0) ret = decode_mb_cabac(h);
  6571. if(ret>=0) hl_decode_mb(h);
  6572. s->mb_y--;
  6573. }
  6574. eos = get_cabac_terminate( &h->cabac );
  6575. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  6576. 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);
  6577. 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);
  6578. return -1;
  6579. }
  6580. if( ++s->mb_x >= s->mb_width ) {
  6581. s->mb_x = 0;
  6582. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6583. ++s->mb_y;
  6584. if(FRAME_MBAFF) {
  6585. ++s->mb_y;
  6586. }
  6587. }
  6588. if( eos || s->mb_y >= s->mb_height ) {
  6589. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6590. 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);
  6591. return 0;
  6592. }
  6593. }
  6594. } else {
  6595. for(;;){
  6596. int ret = decode_mb_cavlc(h);
  6597. if(ret>=0) hl_decode_mb(h);
  6598. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  6599. s->mb_y++;
  6600. ret = decode_mb_cavlc(h);
  6601. if(ret>=0) hl_decode_mb(h);
  6602. s->mb_y--;
  6603. }
  6604. if(ret<0){
  6605. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6606. 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);
  6607. return -1;
  6608. }
  6609. if(++s->mb_x >= s->mb_width){
  6610. s->mb_x=0;
  6611. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6612. ++s->mb_y;
  6613. if(FRAME_MBAFF) {
  6614. ++s->mb_y;
  6615. }
  6616. if(s->mb_y >= s->mb_height){
  6617. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6618. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  6619. 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);
  6620. return 0;
  6621. }else{
  6622. 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);
  6623. return -1;
  6624. }
  6625. }
  6626. }
  6627. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  6628. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6629. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  6630. 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);
  6631. return 0;
  6632. }else{
  6633. 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);
  6634. return -1;
  6635. }
  6636. }
  6637. }
  6638. }
  6639. #if 0
  6640. for(;s->mb_y < s->mb_height; s->mb_y++){
  6641. for(;s->mb_x < s->mb_width; s->mb_x++){
  6642. int ret= decode_mb(h);
  6643. hl_decode_mb(h);
  6644. if(ret<0){
  6645. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6646. 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);
  6647. return -1;
  6648. }
  6649. if(++s->mb_x >= s->mb_width){
  6650. s->mb_x=0;
  6651. if(++s->mb_y >= s->mb_height){
  6652. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6653. 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);
  6654. return 0;
  6655. }else{
  6656. 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);
  6657. return -1;
  6658. }
  6659. }
  6660. }
  6661. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  6662. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6663. 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);
  6664. return 0;
  6665. }else{
  6666. 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);
  6667. return -1;
  6668. }
  6669. }
  6670. }
  6671. s->mb_x=0;
  6672. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6673. }
  6674. #endif
  6675. return -1; //not reached
  6676. }
  6677. static int decode_unregistered_user_data(H264Context *h, int size){
  6678. MpegEncContext * const s = &h->s;
  6679. uint8_t user_data[16+256];
  6680. int e, build, i;
  6681. if(size<16)
  6682. return -1;
  6683. for(i=0; i<sizeof(user_data)-1 && i<size; i++){
  6684. user_data[i]= get_bits(&s->gb, 8);
  6685. }
  6686. user_data[i]= 0;
  6687. e= sscanf(user_data+16, "x264 - core %d"/*%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html*/, &build);
  6688. if(e==1 && build>=0)
  6689. h->x264_build= build;
  6690. if(s->avctx->debug & FF_DEBUG_BUGS)
  6691. av_log(s->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data+16);
  6692. for(; i<size; i++)
  6693. skip_bits(&s->gb, 8);
  6694. return 0;
  6695. }
  6696. static int decode_sei(H264Context *h){
  6697. MpegEncContext * const s = &h->s;
  6698. while(get_bits_count(&s->gb) + 16 < s->gb.size_in_bits){
  6699. int size, type;
  6700. type=0;
  6701. do{
  6702. type+= show_bits(&s->gb, 8);
  6703. }while(get_bits(&s->gb, 8) == 255);
  6704. size=0;
  6705. do{
  6706. size+= show_bits(&s->gb, 8);
  6707. }while(get_bits(&s->gb, 8) == 255);
  6708. switch(type){
  6709. case 5:
  6710. if(decode_unregistered_user_data(h, size) < 0)
  6711. return -1;
  6712. break;
  6713. default:
  6714. skip_bits(&s->gb, 8*size);
  6715. }
  6716. //FIXME check bits here
  6717. align_get_bits(&s->gb);
  6718. }
  6719. return 0;
  6720. }
  6721. static inline void decode_hrd_parameters(H264Context *h, SPS *sps){
  6722. MpegEncContext * const s = &h->s;
  6723. int cpb_count, i;
  6724. cpb_count = get_ue_golomb(&s->gb) + 1;
  6725. get_bits(&s->gb, 4); /* bit_rate_scale */
  6726. get_bits(&s->gb, 4); /* cpb_size_scale */
  6727. for(i=0; i<cpb_count; i++){
  6728. get_ue_golomb(&s->gb); /* bit_rate_value_minus1 */
  6729. get_ue_golomb(&s->gb); /* cpb_size_value_minus1 */
  6730. get_bits1(&s->gb); /* cbr_flag */
  6731. }
  6732. get_bits(&s->gb, 5); /* initial_cpb_removal_delay_length_minus1 */
  6733. get_bits(&s->gb, 5); /* cpb_removal_delay_length_minus1 */
  6734. get_bits(&s->gb, 5); /* dpb_output_delay_length_minus1 */
  6735. get_bits(&s->gb, 5); /* time_offset_length */
  6736. }
  6737. static inline int decode_vui_parameters(H264Context *h, SPS *sps){
  6738. MpegEncContext * const s = &h->s;
  6739. int aspect_ratio_info_present_flag;
  6740. unsigned int aspect_ratio_idc;
  6741. int nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag;
  6742. aspect_ratio_info_present_flag= get_bits1(&s->gb);
  6743. if( aspect_ratio_info_present_flag ) {
  6744. aspect_ratio_idc= get_bits(&s->gb, 8);
  6745. if( aspect_ratio_idc == EXTENDED_SAR ) {
  6746. sps->sar.num= get_bits(&s->gb, 16);
  6747. sps->sar.den= get_bits(&s->gb, 16);
  6748. }else if(aspect_ratio_idc < 14){
  6749. sps->sar= pixel_aspect[aspect_ratio_idc];
  6750. }else{
  6751. av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
  6752. return -1;
  6753. }
  6754. }else{
  6755. sps->sar.num=
  6756. sps->sar.den= 0;
  6757. }
  6758. // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
  6759. if(get_bits1(&s->gb)){ /* overscan_info_present_flag */
  6760. get_bits1(&s->gb); /* overscan_appropriate_flag */
  6761. }
  6762. if(get_bits1(&s->gb)){ /* video_signal_type_present_flag */
  6763. get_bits(&s->gb, 3); /* video_format */
  6764. get_bits1(&s->gb); /* video_full_range_flag */
  6765. if(get_bits1(&s->gb)){ /* colour_description_present_flag */
  6766. get_bits(&s->gb, 8); /* colour_primaries */
  6767. get_bits(&s->gb, 8); /* transfer_characteristics */
  6768. get_bits(&s->gb, 8); /* matrix_coefficients */
  6769. }
  6770. }
  6771. if(get_bits1(&s->gb)){ /* chroma_location_info_present_flag */
  6772. get_ue_golomb(&s->gb); /* chroma_sample_location_type_top_field */
  6773. get_ue_golomb(&s->gb); /* chroma_sample_location_type_bottom_field */
  6774. }
  6775. sps->timing_info_present_flag = get_bits1(&s->gb);
  6776. if(sps->timing_info_present_flag){
  6777. sps->num_units_in_tick = get_bits_long(&s->gb, 32);
  6778. sps->time_scale = get_bits_long(&s->gb, 32);
  6779. sps->fixed_frame_rate_flag = get_bits1(&s->gb);
  6780. }
  6781. nal_hrd_parameters_present_flag = get_bits1(&s->gb);
  6782. if(nal_hrd_parameters_present_flag)
  6783. decode_hrd_parameters(h, sps);
  6784. vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
  6785. if(vcl_hrd_parameters_present_flag)
  6786. decode_hrd_parameters(h, sps);
  6787. if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag)
  6788. get_bits1(&s->gb); /* low_delay_hrd_flag */
  6789. get_bits1(&s->gb); /* pic_struct_present_flag */
  6790. sps->bitstream_restriction_flag = get_bits1(&s->gb);
  6791. if(sps->bitstream_restriction_flag){
  6792. unsigned int num_reorder_frames;
  6793. get_bits1(&s->gb); /* motion_vectors_over_pic_boundaries_flag */
  6794. get_ue_golomb(&s->gb); /* max_bytes_per_pic_denom */
  6795. get_ue_golomb(&s->gb); /* max_bits_per_mb_denom */
  6796. get_ue_golomb(&s->gb); /* log2_max_mv_length_horizontal */
  6797. get_ue_golomb(&s->gb); /* log2_max_mv_length_vertical */
  6798. num_reorder_frames= get_ue_golomb(&s->gb);
  6799. get_ue_golomb(&s->gb); /*max_dec_frame_buffering*/
  6800. if(num_reorder_frames > 16 /*max_dec_frame_buffering || max_dec_frame_buffering > 16*/){
  6801. av_log(h->s.avctx, AV_LOG_ERROR, "illegal num_reorder_frames %d\n", num_reorder_frames);
  6802. return -1;
  6803. }
  6804. sps->num_reorder_frames= num_reorder_frames;
  6805. }
  6806. return 0;
  6807. }
  6808. static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,
  6809. const uint8_t *jvt_list, const uint8_t *fallback_list){
  6810. MpegEncContext * const s = &h->s;
  6811. int i, last = 8, next = 8;
  6812. const uint8_t *scan = size == 16 ? zigzag_scan : zigzag_scan8x8;
  6813. if(!get_bits1(&s->gb)) /* matrix not written, we use the predicted one */
  6814. memcpy(factors, fallback_list, size*sizeof(uint8_t));
  6815. else
  6816. for(i=0;i<size;i++){
  6817. if(next)
  6818. next = (last + get_se_golomb(&s->gb)) & 0xff;
  6819. if(!i && !next){ /* matrix not written, we use the preset one */
  6820. memcpy(factors, jvt_list, size*sizeof(uint8_t));
  6821. break;
  6822. }
  6823. last = factors[scan[i]] = next ? next : last;
  6824. }
  6825. }
  6826. static void decode_scaling_matrices(H264Context *h, SPS *sps, PPS *pps, int is_sps,
  6827. uint8_t (*scaling_matrix4)[16], uint8_t (*scaling_matrix8)[64]){
  6828. MpegEncContext * const s = &h->s;
  6829. int fallback_sps = !is_sps && sps->scaling_matrix_present;
  6830. const uint8_t *fallback[4] = {
  6831. fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
  6832. fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
  6833. fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
  6834. fallback_sps ? sps->scaling_matrix8[1] : default_scaling8[1]
  6835. };
  6836. if(get_bits1(&s->gb)){
  6837. sps->scaling_matrix_present |= is_sps;
  6838. decode_scaling_list(h,scaling_matrix4[0],16,default_scaling4[0],fallback[0]); // Intra, Y
  6839. decode_scaling_list(h,scaling_matrix4[1],16,default_scaling4[0],scaling_matrix4[0]); // Intra, Cr
  6840. decode_scaling_list(h,scaling_matrix4[2],16,default_scaling4[0],scaling_matrix4[1]); // Intra, Cb
  6841. decode_scaling_list(h,scaling_matrix4[3],16,default_scaling4[1],fallback[1]); // Inter, Y
  6842. decode_scaling_list(h,scaling_matrix4[4],16,default_scaling4[1],scaling_matrix4[3]); // Inter, Cr
  6843. decode_scaling_list(h,scaling_matrix4[5],16,default_scaling4[1],scaling_matrix4[4]); // Inter, Cb
  6844. if(is_sps || pps->transform_8x8_mode){
  6845. decode_scaling_list(h,scaling_matrix8[0],64,default_scaling8[0],fallback[2]); // Intra, Y
  6846. decode_scaling_list(h,scaling_matrix8[1],64,default_scaling8[1],fallback[3]); // Inter, Y
  6847. }
  6848. } else if(fallback_sps) {
  6849. memcpy(scaling_matrix4, sps->scaling_matrix4, 6*16*sizeof(uint8_t));
  6850. memcpy(scaling_matrix8, sps->scaling_matrix8, 2*64*sizeof(uint8_t));
  6851. }
  6852. }
  6853. static inline int decode_seq_parameter_set(H264Context *h){
  6854. MpegEncContext * const s = &h->s;
  6855. int profile_idc, level_idc;
  6856. unsigned int sps_id, tmp, mb_width, mb_height;
  6857. int i;
  6858. SPS *sps;
  6859. profile_idc= get_bits(&s->gb, 8);
  6860. get_bits1(&s->gb); //constraint_set0_flag
  6861. get_bits1(&s->gb); //constraint_set1_flag
  6862. get_bits1(&s->gb); //constraint_set2_flag
  6863. get_bits1(&s->gb); //constraint_set3_flag
  6864. get_bits(&s->gb, 4); // reserved
  6865. level_idc= get_bits(&s->gb, 8);
  6866. sps_id= get_ue_golomb(&s->gb);
  6867. if (sps_id >= MAX_SPS_COUNT){
  6868. // ok it has gone out of hand, someone is sending us bad stuff.
  6869. av_log(h->s.avctx, AV_LOG_ERROR, "illegal sps_id (%d)\n", sps_id);
  6870. return -1;
  6871. }
  6872. sps= &h->sps_buffer[ sps_id ];
  6873. sps->profile_idc= profile_idc;
  6874. sps->level_idc= level_idc;
  6875. if(sps->profile_idc >= 100){ //high profile
  6876. if(get_ue_golomb(&s->gb) == 3) //chroma_format_idc
  6877. get_bits1(&s->gb); //residual_color_transform_flag
  6878. get_ue_golomb(&s->gb); //bit_depth_luma_minus8
  6879. get_ue_golomb(&s->gb); //bit_depth_chroma_minus8
  6880. sps->transform_bypass = get_bits1(&s->gb);
  6881. decode_scaling_matrices(h, sps, NULL, 1, sps->scaling_matrix4, sps->scaling_matrix8);
  6882. }else
  6883. sps->scaling_matrix_present = 0;
  6884. sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
  6885. sps->poc_type= get_ue_golomb(&s->gb);
  6886. if(sps->poc_type == 0){ //FIXME #define
  6887. sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
  6888. } else if(sps->poc_type == 1){//FIXME #define
  6889. sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
  6890. sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
  6891. sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
  6892. tmp= get_ue_golomb(&s->gb);
  6893. if(tmp >= sizeof(sps->offset_for_ref_frame) / sizeof(sps->offset_for_ref_frame[0])){
  6894. av_log(h->s.avctx, AV_LOG_ERROR, "poc_cycle_length overflow %u\n", tmp);
  6895. return -1;
  6896. }
  6897. sps->poc_cycle_length= tmp;
  6898. for(i=0; i<sps->poc_cycle_length; i++)
  6899. sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
  6900. }else if(sps->poc_type != 2){
  6901. av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
  6902. return -1;
  6903. }
  6904. tmp= get_ue_golomb(&s->gb);
  6905. if(tmp > MAX_PICTURE_COUNT-2){
  6906. av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");
  6907. }
  6908. sps->ref_frame_count= tmp;
  6909. sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
  6910. mb_width= get_ue_golomb(&s->gb) + 1;
  6911. mb_height= get_ue_golomb(&s->gb) + 1;
  6912. if(mb_width >= INT_MAX/16 || mb_height >= INT_MAX/16 ||
  6913. avcodec_check_dimensions(NULL, 16*mb_width, 16*mb_height)){
  6914. av_log(h->s.avctx, AV_LOG_ERROR, "mb_width/height overflow\n");
  6915. return -1;
  6916. }
  6917. sps->mb_width = mb_width;
  6918. sps->mb_height= mb_height;
  6919. sps->frame_mbs_only_flag= get_bits1(&s->gb);
  6920. if(!sps->frame_mbs_only_flag)
  6921. sps->mb_aff= get_bits1(&s->gb);
  6922. else
  6923. sps->mb_aff= 0;
  6924. sps->direct_8x8_inference_flag= get_bits1(&s->gb);
  6925. #ifndef ALLOW_INTERLACE
  6926. if(sps->mb_aff)
  6927. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF support not included; enable it at compile-time.\n");
  6928. #endif
  6929. if(!sps->direct_8x8_inference_flag && sps->mb_aff)
  6930. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + !direct_8x8_inference is not implemented\n");
  6931. sps->crop= get_bits1(&s->gb);
  6932. if(sps->crop){
  6933. sps->crop_left = get_ue_golomb(&s->gb);
  6934. sps->crop_right = get_ue_golomb(&s->gb);
  6935. sps->crop_top = get_ue_golomb(&s->gb);
  6936. sps->crop_bottom= get_ue_golomb(&s->gb);
  6937. if(sps->crop_left || sps->crop_top){
  6938. av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ...\n");
  6939. }
  6940. }else{
  6941. sps->crop_left =
  6942. sps->crop_right =
  6943. sps->crop_top =
  6944. sps->crop_bottom= 0;
  6945. }
  6946. sps->vui_parameters_present_flag= get_bits1(&s->gb);
  6947. if( sps->vui_parameters_present_flag )
  6948. decode_vui_parameters(h, sps);
  6949. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6950. av_log(h->s.avctx, AV_LOG_DEBUG, "sps:%u profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%d/%d/%d/%d %s\n",
  6951. sps_id, sps->profile_idc, sps->level_idc,
  6952. sps->poc_type,
  6953. sps->ref_frame_count,
  6954. sps->mb_width, sps->mb_height,
  6955. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  6956. sps->direct_8x8_inference_flag ? "8B8" : "",
  6957. sps->crop_left, sps->crop_right,
  6958. sps->crop_top, sps->crop_bottom,
  6959. sps->vui_parameters_present_flag ? "VUI" : ""
  6960. );
  6961. }
  6962. return 0;
  6963. }
  6964. static inline int decode_picture_parameter_set(H264Context *h, int bit_length){
  6965. MpegEncContext * const s = &h->s;
  6966. unsigned int tmp, pps_id= get_ue_golomb(&s->gb);
  6967. PPS *pps;
  6968. if(pps_id>=MAX_PPS_COUNT){
  6969. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  6970. return -1;
  6971. }
  6972. pps = &h->pps_buffer[pps_id];
  6973. tmp= get_ue_golomb(&s->gb);
  6974. if(tmp>=MAX_SPS_COUNT){
  6975. av_log(h->s.avctx, AV_LOG_ERROR, "sps_id out of range\n");
  6976. return -1;
  6977. }
  6978. pps->sps_id= tmp;
  6979. pps->cabac= get_bits1(&s->gb);
  6980. pps->pic_order_present= get_bits1(&s->gb);
  6981. pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
  6982. if(pps->slice_group_count > 1 ){
  6983. pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
  6984. av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
  6985. switch(pps->mb_slice_group_map_type){
  6986. case 0:
  6987. #if 0
  6988. | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
  6989. | run_length[ i ] |1 |ue(v) |
  6990. #endif
  6991. break;
  6992. case 2:
  6993. #if 0
  6994. | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
  6995. |{ | | |
  6996. | top_left_mb[ i ] |1 |ue(v) |
  6997. | bottom_right_mb[ i ] |1 |ue(v) |
  6998. | } | | |
  6999. #endif
  7000. break;
  7001. case 3:
  7002. case 4:
  7003. case 5:
  7004. #if 0
  7005. | slice_group_change_direction_flag |1 |u(1) |
  7006. | slice_group_change_rate_minus1 |1 |ue(v) |
  7007. #endif
  7008. break;
  7009. case 6:
  7010. #if 0
  7011. | slice_group_id_cnt_minus1 |1 |ue(v) |
  7012. | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
  7013. |) | | |
  7014. | slice_group_id[ i ] |1 |u(v) |
  7015. #endif
  7016. break;
  7017. }
  7018. }
  7019. pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  7020. pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  7021. if(pps->ref_count[0]-1 > 32-1 || pps->ref_count[1]-1 > 32-1){
  7022. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
  7023. pps->ref_count[0]= pps->ref_count[1]= 1;
  7024. return -1;
  7025. }
  7026. pps->weighted_pred= get_bits1(&s->gb);
  7027. pps->weighted_bipred_idc= get_bits(&s->gb, 2);
  7028. pps->init_qp= get_se_golomb(&s->gb) + 26;
  7029. pps->init_qs= get_se_golomb(&s->gb) + 26;
  7030. pps->chroma_qp_index_offset= get_se_golomb(&s->gb);
  7031. pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
  7032. pps->constrained_intra_pred= get_bits1(&s->gb);
  7033. pps->redundant_pic_cnt_present = get_bits1(&s->gb);
  7034. pps->transform_8x8_mode= 0;
  7035. h->dequant_coeff_pps= -1; //contents of sps/pps can change even if id doesn't, so reinit
  7036. memset(pps->scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  7037. memset(pps->scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  7038. if(get_bits_count(&s->gb) < bit_length){
  7039. pps->transform_8x8_mode= get_bits1(&s->gb);
  7040. decode_scaling_matrices(h, &h->sps_buffer[pps->sps_id], pps, 0, pps->scaling_matrix4, pps->scaling_matrix8);
  7041. get_se_golomb(&s->gb); //second_chroma_qp_index_offset
  7042. }
  7043. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  7044. av_log(h->s.avctx, AV_LOG_DEBUG, "pps:%u sps:%u %s slice_groups:%d ref:%d/%d %s qp:%d/%d/%d %s %s %s %s\n",
  7045. pps_id, pps->sps_id,
  7046. pps->cabac ? "CABAC" : "CAVLC",
  7047. pps->slice_group_count,
  7048. pps->ref_count[0], pps->ref_count[1],
  7049. pps->weighted_pred ? "weighted" : "",
  7050. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset,
  7051. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  7052. pps->constrained_intra_pred ? "CONSTR" : "",
  7053. pps->redundant_pic_cnt_present ? "REDU" : "",
  7054. pps->transform_8x8_mode ? "8x8DCT" : ""
  7055. );
  7056. }
  7057. return 0;
  7058. }
  7059. /**
  7060. * finds the end of the current frame in the bitstream.
  7061. * @return the position of the first byte of the next frame, or -1
  7062. */
  7063. static int find_frame_end(H264Context *h, const uint8_t *buf, int buf_size){
  7064. int i;
  7065. uint32_t state;
  7066. ParseContext *pc = &(h->s.parse_context);
  7067. //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
  7068. // mb_addr= pc->mb_addr - 1;
  7069. state= pc->state;
  7070. for(i=0; i<=buf_size; i++){
  7071. if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  7072. tprintf("find_frame_end new startcode = %08x, frame_start_found = %d, pos = %d\n", state, pc->frame_start_found, i);
  7073. if(pc->frame_start_found){
  7074. // If there isn't one more byte in the buffer
  7075. // the test on first_mb_in_slice cannot be done yet
  7076. // do it at next call.
  7077. if (i >= buf_size) break;
  7078. if (buf[i] & 0x80) {
  7079. // first_mb_in_slice is 0, probably the first nal of a new
  7080. // slice
  7081. tprintf("find_frame_end frame_end_found, state = %08x, pos = %d\n", state, i);
  7082. pc->state=-1;
  7083. pc->frame_start_found= 0;
  7084. return i-4;
  7085. }
  7086. }
  7087. pc->frame_start_found = 1;
  7088. }
  7089. if((state&0xFFFFFF1F) == 0x107 || (state&0xFFFFFF1F) == 0x108 || (state&0xFFFFFF1F) == 0x109){
  7090. if(pc->frame_start_found){
  7091. pc->state=-1;
  7092. pc->frame_start_found= 0;
  7093. return i-4;
  7094. }
  7095. }
  7096. if (i<buf_size)
  7097. state= (state<<8) | buf[i];
  7098. }
  7099. pc->state= state;
  7100. return END_NOT_FOUND;
  7101. }
  7102. #ifdef CONFIG_H264_PARSER
  7103. static int h264_parse(AVCodecParserContext *s,
  7104. AVCodecContext *avctx,
  7105. uint8_t **poutbuf, int *poutbuf_size,
  7106. const uint8_t *buf, int buf_size)
  7107. {
  7108. H264Context *h = s->priv_data;
  7109. ParseContext *pc = &h->s.parse_context;
  7110. int next;
  7111. next= find_frame_end(h, buf, buf_size);
  7112. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  7113. *poutbuf = NULL;
  7114. *poutbuf_size = 0;
  7115. return buf_size;
  7116. }
  7117. *poutbuf = (uint8_t *)buf;
  7118. *poutbuf_size = buf_size;
  7119. return next;
  7120. }
  7121. static int h264_split(AVCodecContext *avctx,
  7122. const uint8_t *buf, int buf_size)
  7123. {
  7124. int i;
  7125. uint32_t state = -1;
  7126. int has_sps= 0;
  7127. for(i=0; i<=buf_size; i++){
  7128. if((state&0xFFFFFF1F) == 0x107)
  7129. has_sps=1;
  7130. /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  7131. }*/
  7132. if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
  7133. if(has_sps){
  7134. while(i>4 && buf[i-5]==0) i--;
  7135. return i-4;
  7136. }
  7137. }
  7138. if (i<buf_size)
  7139. state= (state<<8) | buf[i];
  7140. }
  7141. return 0;
  7142. }
  7143. #endif /* CONFIG_H264_PARSER */
  7144. static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){
  7145. MpegEncContext * const s = &h->s;
  7146. AVCodecContext * const avctx= s->avctx;
  7147. int buf_index=0;
  7148. #if 0
  7149. int i;
  7150. for(i=0; i<50; i++){
  7151. av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
  7152. }
  7153. #endif
  7154. h->slice_num = 0;
  7155. s->current_picture_ptr= NULL;
  7156. for(;;){
  7157. int consumed;
  7158. int dst_length;
  7159. int bit_length;
  7160. uint8_t *ptr;
  7161. int i, nalsize = 0;
  7162. if(h->is_avc) {
  7163. if(buf_index >= buf_size) break;
  7164. nalsize = 0;
  7165. for(i = 0; i < h->nal_length_size; i++)
  7166. nalsize = (nalsize << 8) | buf[buf_index++];
  7167. if(nalsize <= 1 || nalsize > buf_size){
  7168. if(nalsize == 1){
  7169. buf_index++;
  7170. continue;
  7171. }else{
  7172. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  7173. break;
  7174. }
  7175. }
  7176. } else {
  7177. // start code prefix search
  7178. for(; buf_index + 3 < buf_size; buf_index++){
  7179. // this should allways succeed in the first iteration
  7180. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  7181. break;
  7182. }
  7183. if(buf_index+3 >= buf_size) break;
  7184. buf_index+=3;
  7185. }
  7186. ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, h->is_avc ? nalsize : buf_size - buf_index);
  7187. if (ptr==NULL || dst_length <= 0){
  7188. return -1;
  7189. }
  7190. while(ptr[dst_length - 1] == 0 && dst_length > 1)
  7191. dst_length--;
  7192. bit_length= 8*dst_length - decode_rbsp_trailing(ptr + dst_length - 1);
  7193. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  7194. 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);
  7195. }
  7196. if (h->is_avc && (nalsize != consumed))
  7197. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  7198. buf_index += consumed;
  7199. if( (s->hurry_up == 1 && h->nal_ref_idc == 0) //FIXME dont discard SEI id
  7200. ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  7201. continue;
  7202. switch(h->nal_unit_type){
  7203. case NAL_IDR_SLICE:
  7204. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  7205. case NAL_SLICE:
  7206. init_get_bits(&s->gb, ptr, bit_length);
  7207. h->intra_gb_ptr=
  7208. h->inter_gb_ptr= &s->gb;
  7209. s->data_partitioning = 0;
  7210. if(decode_slice_header(h) < 0){
  7211. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  7212. break;
  7213. }
  7214. s->current_picture_ptr->key_frame= (h->nal_unit_type == NAL_IDR_SLICE);
  7215. if(h->redundant_pic_count==0 && s->hurry_up < 5
  7216. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  7217. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  7218. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  7219. && avctx->skip_frame < AVDISCARD_ALL)
  7220. decode_slice(h);
  7221. break;
  7222. case NAL_DPA:
  7223. init_get_bits(&s->gb, ptr, bit_length);
  7224. h->intra_gb_ptr=
  7225. h->inter_gb_ptr= NULL;
  7226. s->data_partitioning = 1;
  7227. if(decode_slice_header(h) < 0){
  7228. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  7229. }
  7230. break;
  7231. case NAL_DPB:
  7232. init_get_bits(&h->intra_gb, ptr, bit_length);
  7233. h->intra_gb_ptr= &h->intra_gb;
  7234. break;
  7235. case NAL_DPC:
  7236. init_get_bits(&h->inter_gb, ptr, bit_length);
  7237. h->inter_gb_ptr= &h->inter_gb;
  7238. if(h->redundant_pic_count==0 && h->intra_gb_ptr && s->data_partitioning
  7239. && s->context_initialized
  7240. && s->hurry_up < 5
  7241. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  7242. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  7243. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  7244. && avctx->skip_frame < AVDISCARD_ALL)
  7245. decode_slice(h);
  7246. break;
  7247. case NAL_SEI:
  7248. init_get_bits(&s->gb, ptr, bit_length);
  7249. decode_sei(h);
  7250. break;
  7251. case NAL_SPS:
  7252. init_get_bits(&s->gb, ptr, bit_length);
  7253. decode_seq_parameter_set(h);
  7254. if(s->flags& CODEC_FLAG_LOW_DELAY)
  7255. s->low_delay=1;
  7256. if(avctx->has_b_frames < 2)
  7257. avctx->has_b_frames= !s->low_delay;
  7258. break;
  7259. case NAL_PPS:
  7260. init_get_bits(&s->gb, ptr, bit_length);
  7261. decode_picture_parameter_set(h, bit_length);
  7262. break;
  7263. case NAL_AUD:
  7264. case NAL_END_SEQUENCE:
  7265. case NAL_END_STREAM:
  7266. case NAL_FILLER_DATA:
  7267. case NAL_SPS_EXT:
  7268. case NAL_AUXILIARY_SLICE:
  7269. break;
  7270. default:
  7271. av_log(avctx, AV_LOG_ERROR, "Unknown NAL code: %d\n", h->nal_unit_type);
  7272. }
  7273. }
  7274. if(!s->current_picture_ptr) return buf_index; //no frame
  7275. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  7276. s->current_picture_ptr->pict_type= s->pict_type;
  7277. h->prev_frame_num_offset= h->frame_num_offset;
  7278. h->prev_frame_num= h->frame_num;
  7279. if(s->current_picture_ptr->reference){
  7280. h->prev_poc_msb= h->poc_msb;
  7281. h->prev_poc_lsb= h->poc_lsb;
  7282. }
  7283. if(s->current_picture_ptr->reference)
  7284. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  7285. ff_er_frame_end(s);
  7286. MPV_frame_end(s);
  7287. return buf_index;
  7288. }
  7289. /**
  7290. * returns the number of bytes consumed for building the current frame
  7291. */
  7292. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  7293. if(s->flags&CODEC_FLAG_TRUNCATED){
  7294. pos -= s->parse_context.last_index;
  7295. if(pos<0) pos=0; // FIXME remove (unneeded?)
  7296. return pos;
  7297. }else{
  7298. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  7299. if(pos+10>buf_size) pos=buf_size; // oops ;)
  7300. return pos;
  7301. }
  7302. }
  7303. static int decode_frame(AVCodecContext *avctx,
  7304. void *data, int *data_size,
  7305. uint8_t *buf, int buf_size)
  7306. {
  7307. H264Context *h = avctx->priv_data;
  7308. MpegEncContext *s = &h->s;
  7309. AVFrame *pict = data;
  7310. int buf_index;
  7311. s->flags= avctx->flags;
  7312. s->flags2= avctx->flags2;
  7313. /* no supplementary picture */
  7314. if (buf_size == 0) {
  7315. return 0;
  7316. }
  7317. if(s->flags&CODEC_FLAG_TRUNCATED){
  7318. int next= find_frame_end(h, buf, buf_size);
  7319. if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
  7320. return buf_size;
  7321. //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);
  7322. }
  7323. if(h->is_avc && !h->got_avcC) {
  7324. int i, cnt, nalsize;
  7325. unsigned char *p = avctx->extradata;
  7326. if(avctx->extradata_size < 7) {
  7327. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  7328. return -1;
  7329. }
  7330. if(*p != 1) {
  7331. av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
  7332. return -1;
  7333. }
  7334. /* sps and pps in the avcC always have length coded with 2 bytes,
  7335. so put a fake nal_length_size = 2 while parsing them */
  7336. h->nal_length_size = 2;
  7337. // Decode sps from avcC
  7338. cnt = *(p+5) & 0x1f; // Number of sps
  7339. p += 6;
  7340. for (i = 0; i < cnt; i++) {
  7341. nalsize = AV_RB16(p) + 2;
  7342. if(decode_nal_units(h, p, nalsize) < 0) {
  7343. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  7344. return -1;
  7345. }
  7346. p += nalsize;
  7347. }
  7348. // Decode pps from avcC
  7349. cnt = *(p++); // Number of pps
  7350. for (i = 0; i < cnt; i++) {
  7351. nalsize = AV_RB16(p) + 2;
  7352. if(decode_nal_units(h, p, nalsize) != nalsize) {
  7353. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  7354. return -1;
  7355. }
  7356. p += nalsize;
  7357. }
  7358. // Now store right nal length size, that will be use to parse all other nals
  7359. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  7360. // Do not reparse avcC
  7361. h->got_avcC = 1;
  7362. }
  7363. if(avctx->frame_number==0 && !h->is_avc && s->avctx->extradata_size){
  7364. if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
  7365. return -1;
  7366. }
  7367. buf_index=decode_nal_units(h, buf, buf_size);
  7368. if(buf_index < 0)
  7369. return -1;
  7370. //FIXME do something with unavailable reference frames
  7371. // if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_index, buf_size);
  7372. if(!s->current_picture_ptr){
  7373. av_log(h->s.avctx, AV_LOG_DEBUG, "error, NO frame\n");
  7374. return -1;
  7375. }
  7376. {
  7377. Picture *out = s->current_picture_ptr;
  7378. #if 0 //decode order
  7379. *data_size = sizeof(AVFrame);
  7380. #else
  7381. /* Sort B-frames into display order */
  7382. Picture *cur = s->current_picture_ptr;
  7383. Picture *prev = h->delayed_output_pic;
  7384. int i, pics, cross_idr, out_of_order, out_idx;
  7385. if(h->sps.bitstream_restriction_flag
  7386. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  7387. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  7388. s->low_delay = 0;
  7389. }
  7390. pics = 0;
  7391. while(h->delayed_pic[pics]) pics++;
  7392. assert(pics+1 < sizeof(h->delayed_pic) / sizeof(h->delayed_pic[0]));
  7393. h->delayed_pic[pics++] = cur;
  7394. if(cur->reference == 0)
  7395. cur->reference = 1;
  7396. cross_idr = 0;
  7397. for(i=0; h->delayed_pic[i]; i++)
  7398. if(h->delayed_pic[i]->key_frame || h->delayed_pic[i]->poc==0)
  7399. cross_idr = 1;
  7400. out = h->delayed_pic[0];
  7401. out_idx = 0;
  7402. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
  7403. if(h->delayed_pic[i]->poc < out->poc){
  7404. out = h->delayed_pic[i];
  7405. out_idx = i;
  7406. }
  7407. out_of_order = !cross_idr && prev && out->poc < prev->poc;
  7408. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  7409. { }
  7410. else if(prev && pics <= s->avctx->has_b_frames)
  7411. out = prev;
  7412. else if((out_of_order && pics-1 == s->avctx->has_b_frames && pics < 15)
  7413. || (s->low_delay &&
  7414. ((!cross_idr && prev && out->poc > prev->poc + 2)
  7415. || cur->pict_type == B_TYPE)))
  7416. {
  7417. s->low_delay = 0;
  7418. s->avctx->has_b_frames++;
  7419. out = prev;
  7420. }
  7421. else if(out_of_order)
  7422. out = prev;
  7423. if(out_of_order || pics > s->avctx->has_b_frames){
  7424. for(i=out_idx; h->delayed_pic[i]; i++)
  7425. h->delayed_pic[i] = h->delayed_pic[i+1];
  7426. }
  7427. if(prev == out)
  7428. *data_size = 0;
  7429. else
  7430. *data_size = sizeof(AVFrame);
  7431. if(prev && prev != out && prev->reference == 1)
  7432. prev->reference = 0;
  7433. h->delayed_output_pic = out;
  7434. #endif
  7435. if(out)
  7436. *pict= *(AVFrame*)out;
  7437. else
  7438. av_log(avctx, AV_LOG_DEBUG, "no picture\n");
  7439. }
  7440. assert(pict->data[0] || !*data_size);
  7441. ff_print_debug_info(s, pict);
  7442. //printf("out %d\n", (int)pict->data[0]);
  7443. #if 0 //?
  7444. /* Return the Picture timestamp as the frame number */
  7445. /* we substract 1 because it is added on utils.c */
  7446. avctx->frame_number = s->picture_number - 1;
  7447. #endif
  7448. return get_consumed_bytes(s, buf_index, buf_size);
  7449. }
  7450. #if 0
  7451. static inline void fill_mb_avail(H264Context *h){
  7452. MpegEncContext * const s = &h->s;
  7453. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  7454. if(s->mb_y){
  7455. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  7456. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  7457. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  7458. }else{
  7459. h->mb_avail[0]=
  7460. h->mb_avail[1]=
  7461. h->mb_avail[2]= 0;
  7462. }
  7463. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  7464. h->mb_avail[4]= 1; //FIXME move out
  7465. h->mb_avail[5]= 0; //FIXME move out
  7466. }
  7467. #endif
  7468. #if 0 //selftest
  7469. #define COUNT 8000
  7470. #define SIZE (COUNT*40)
  7471. int main(){
  7472. int i;
  7473. uint8_t temp[SIZE];
  7474. PutBitContext pb;
  7475. GetBitContext gb;
  7476. // int int_temp[10000];
  7477. DSPContext dsp;
  7478. AVCodecContext avctx;
  7479. dsputil_init(&dsp, &avctx);
  7480. init_put_bits(&pb, temp, SIZE);
  7481. printf("testing unsigned exp golomb\n");
  7482. for(i=0; i<COUNT; i++){
  7483. START_TIMER
  7484. set_ue_golomb(&pb, i);
  7485. STOP_TIMER("set_ue_golomb");
  7486. }
  7487. flush_put_bits(&pb);
  7488. init_get_bits(&gb, temp, 8*SIZE);
  7489. for(i=0; i<COUNT; i++){
  7490. int j, s;
  7491. s= show_bits(&gb, 24);
  7492. START_TIMER
  7493. j= get_ue_golomb(&gb);
  7494. if(j != i){
  7495. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7496. // return -1;
  7497. }
  7498. STOP_TIMER("get_ue_golomb");
  7499. }
  7500. init_put_bits(&pb, temp, SIZE);
  7501. printf("testing signed exp golomb\n");
  7502. for(i=0; i<COUNT; i++){
  7503. START_TIMER
  7504. set_se_golomb(&pb, i - COUNT/2);
  7505. STOP_TIMER("set_se_golomb");
  7506. }
  7507. flush_put_bits(&pb);
  7508. init_get_bits(&gb, temp, 8*SIZE);
  7509. for(i=0; i<COUNT; i++){
  7510. int j, s;
  7511. s= show_bits(&gb, 24);
  7512. START_TIMER
  7513. j= get_se_golomb(&gb);
  7514. if(j != i - COUNT/2){
  7515. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7516. // return -1;
  7517. }
  7518. STOP_TIMER("get_se_golomb");
  7519. }
  7520. printf("testing 4x4 (I)DCT\n");
  7521. DCTELEM block[16];
  7522. uint8_t src[16], ref[16];
  7523. uint64_t error= 0, max_error=0;
  7524. for(i=0; i<COUNT; i++){
  7525. int j;
  7526. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  7527. for(j=0; j<16; j++){
  7528. ref[j]= random()%255;
  7529. src[j]= random()%255;
  7530. }
  7531. h264_diff_dct_c(block, src, ref, 4);
  7532. //normalize
  7533. for(j=0; j<16; j++){
  7534. // printf("%d ", block[j]);
  7535. block[j]= block[j]*4;
  7536. if(j&1) block[j]= (block[j]*4 + 2)/5;
  7537. if(j&4) block[j]= (block[j]*4 + 2)/5;
  7538. }
  7539. // printf("\n");
  7540. s->dsp.h264_idct_add(ref, block, 4);
  7541. /* for(j=0; j<16; j++){
  7542. printf("%d ", ref[j]);
  7543. }
  7544. printf("\n");*/
  7545. for(j=0; j<16; j++){
  7546. int diff= FFABS(src[j] - ref[j]);
  7547. error+= diff*diff;
  7548. max_error= FFMAX(max_error, diff);
  7549. }
  7550. }
  7551. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  7552. #if 0
  7553. printf("testing quantizer\n");
  7554. for(qp=0; qp<52; qp++){
  7555. for(i=0; i<16; i++)
  7556. src1_block[i]= src2_block[i]= random()%255;
  7557. }
  7558. #endif
  7559. printf("Testing NAL layer\n");
  7560. uint8_t bitstream[COUNT];
  7561. uint8_t nal[COUNT*2];
  7562. H264Context h;
  7563. memset(&h, 0, sizeof(H264Context));
  7564. for(i=0; i<COUNT; i++){
  7565. int zeros= i;
  7566. int nal_length;
  7567. int consumed;
  7568. int out_length;
  7569. uint8_t *out;
  7570. int j;
  7571. for(j=0; j<COUNT; j++){
  7572. bitstream[j]= (random() % 255) + 1;
  7573. }
  7574. for(j=0; j<zeros; j++){
  7575. int pos= random() % COUNT;
  7576. while(bitstream[pos] == 0){
  7577. pos++;
  7578. pos %= COUNT;
  7579. }
  7580. bitstream[pos]=0;
  7581. }
  7582. START_TIMER
  7583. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  7584. if(nal_length<0){
  7585. printf("encoding failed\n");
  7586. return -1;
  7587. }
  7588. out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
  7589. STOP_TIMER("NAL")
  7590. if(out_length != COUNT){
  7591. printf("incorrect length %d %d\n", out_length, COUNT);
  7592. return -1;
  7593. }
  7594. if(consumed != nal_length){
  7595. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  7596. return -1;
  7597. }
  7598. if(memcmp(bitstream, out, COUNT)){
  7599. printf("missmatch\n");
  7600. return -1;
  7601. }
  7602. }
  7603. printf("Testing RBSP\n");
  7604. return 0;
  7605. }
  7606. #endif
  7607. static int decode_end(AVCodecContext *avctx)
  7608. {
  7609. H264Context *h = avctx->priv_data;
  7610. MpegEncContext *s = &h->s;
  7611. av_freep(&h->rbsp_buffer);
  7612. free_tables(h); //FIXME cleanup init stuff perhaps
  7613. MPV_common_end(s);
  7614. // memset(h, 0, sizeof(H264Context));
  7615. return 0;
  7616. }
  7617. AVCodec h264_decoder = {
  7618. "h264",
  7619. CODEC_TYPE_VIDEO,
  7620. CODEC_ID_H264,
  7621. sizeof(H264Context),
  7622. decode_init,
  7623. NULL,
  7624. decode_end,
  7625. decode_frame,
  7626. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  7627. .flush= flush_dpb,
  7628. };
  7629. #ifdef CONFIG_H264_PARSER
  7630. AVCodecParser h264_parser = {
  7631. { CODEC_ID_H264 },
  7632. sizeof(H264Context),
  7633. NULL,
  7634. h264_parse,
  7635. ff_parse_close,
  7636. h264_split,
  7637. };
  7638. #endif
  7639. #include "svq3.c"