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.

8654 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. Picture *short_ref[32];
  283. Picture *long_ref[32];
  284. Picture default_ref_list[2][32];
  285. Picture ref_list[2][48]; ///< 0..15: frame refs, 16..47: mbaff field refs
  286. Picture *delayed_pic[18]; //FIXME size?
  287. Picture *delayed_output_pic;
  288. /**
  289. * memory management control operations buffer.
  290. */
  291. MMCO mmco[MAX_MMCO_COUNT];
  292. int mmco_index;
  293. int long_ref_count; ///< number of actual long term references
  294. int short_ref_count; ///< number of actual short term references
  295. //data partitioning
  296. GetBitContext intra_gb;
  297. GetBitContext inter_gb;
  298. GetBitContext *intra_gb_ptr;
  299. GetBitContext *inter_gb_ptr;
  300. DECLARE_ALIGNED_8(DCTELEM, mb[16*24]);
  301. 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
  302. /**
  303. * Cabac
  304. */
  305. CABACContext cabac;
  306. uint8_t cabac_state[460];
  307. int cabac_init_idc;
  308. /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
  309. uint16_t *cbp_table;
  310. int cbp;
  311. int top_cbp;
  312. int left_cbp;
  313. /* chroma_pred_mode for i4x4 or i16x16, else 0 */
  314. uint8_t *chroma_pred_mode_table;
  315. int last_qscale_diff;
  316. int16_t (*mvd_table[2])[2];
  317. DECLARE_ALIGNED_8(int16_t, mvd_cache[2][5*8][2]);
  318. uint8_t *direct_table;
  319. uint8_t direct_cache[5*8];
  320. uint8_t zigzag_scan[16];
  321. uint8_t zigzag_scan8x8[64];
  322. uint8_t zigzag_scan8x8_cavlc[64];
  323. uint8_t field_scan[16];
  324. uint8_t field_scan8x8[64];
  325. uint8_t field_scan8x8_cavlc[64];
  326. const uint8_t *zigzag_scan_q0;
  327. const uint8_t *zigzag_scan8x8_q0;
  328. const uint8_t *zigzag_scan8x8_cavlc_q0;
  329. const uint8_t *field_scan_q0;
  330. const uint8_t *field_scan8x8_q0;
  331. const uint8_t *field_scan8x8_cavlc_q0;
  332. int x264_build;
  333. }H264Context;
  334. static VLC coeff_token_vlc[4];
  335. static VLC chroma_dc_coeff_token_vlc;
  336. static VLC total_zeros_vlc[15];
  337. static VLC chroma_dc_total_zeros_vlc[3];
  338. static VLC run_vlc[6];
  339. static VLC run7_vlc;
  340. static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
  341. static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
  342. 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);
  343. 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);
  344. static av_always_inline uint32_t pack16to32(int a, int b){
  345. #ifdef WORDS_BIGENDIAN
  346. return (b&0xFFFF) + (a<<16);
  347. #else
  348. return (a&0xFFFF) + (b<<16);
  349. #endif
  350. }
  351. const uint8_t ff_rem6[52]={
  352. 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,
  353. };
  354. const uint8_t ff_div6[52]={
  355. 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,
  356. };
  357. /**
  358. * fill a rectangle.
  359. * @param h height of the rectangle, should be a constant
  360. * @param w width of the rectangle, should be a constant
  361. * @param size the size of val (1 or 4), should be a constant
  362. */
  363. static av_always_inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){
  364. uint8_t *p= (uint8_t*)vp;
  365. assert(size==1 || size==4);
  366. assert(w<=4);
  367. w *= size;
  368. stride *= size;
  369. assert((((long)vp)&(FFMIN(w, STRIDE_ALIGN)-1)) == 0);
  370. assert((stride&(w-1))==0);
  371. if(w==2){
  372. const uint16_t v= size==4 ? val : val*0x0101;
  373. *(uint16_t*)(p + 0*stride)= v;
  374. if(h==1) return;
  375. *(uint16_t*)(p + 1*stride)= v;
  376. if(h==2) return;
  377. *(uint16_t*)(p + 2*stride)=
  378. *(uint16_t*)(p + 3*stride)= v;
  379. }else if(w==4){
  380. const uint32_t v= size==4 ? val : val*0x01010101;
  381. *(uint32_t*)(p + 0*stride)= v;
  382. if(h==1) return;
  383. *(uint32_t*)(p + 1*stride)= v;
  384. if(h==2) return;
  385. *(uint32_t*)(p + 2*stride)=
  386. *(uint32_t*)(p + 3*stride)= v;
  387. }else if(w==8){
  388. //gcc can't optimize 64bit math on x86_32
  389. #if defined(ARCH_X86_64) || (defined(MP_WORDSIZE) && MP_WORDSIZE >= 64)
  390. const uint64_t v= val*0x0100000001ULL;
  391. *(uint64_t*)(p + 0*stride)= v;
  392. if(h==1) return;
  393. *(uint64_t*)(p + 1*stride)= v;
  394. if(h==2) return;
  395. *(uint64_t*)(p + 2*stride)=
  396. *(uint64_t*)(p + 3*stride)= v;
  397. }else if(w==16){
  398. const uint64_t v= val*0x0100000001ULL;
  399. *(uint64_t*)(p + 0+0*stride)=
  400. *(uint64_t*)(p + 8+0*stride)=
  401. *(uint64_t*)(p + 0+1*stride)=
  402. *(uint64_t*)(p + 8+1*stride)= v;
  403. if(h==2) return;
  404. *(uint64_t*)(p + 0+2*stride)=
  405. *(uint64_t*)(p + 8+2*stride)=
  406. *(uint64_t*)(p + 0+3*stride)=
  407. *(uint64_t*)(p + 8+3*stride)= v;
  408. #else
  409. *(uint32_t*)(p + 0+0*stride)=
  410. *(uint32_t*)(p + 4+0*stride)= val;
  411. if(h==1) return;
  412. *(uint32_t*)(p + 0+1*stride)=
  413. *(uint32_t*)(p + 4+1*stride)= val;
  414. if(h==2) return;
  415. *(uint32_t*)(p + 0+2*stride)=
  416. *(uint32_t*)(p + 4+2*stride)=
  417. *(uint32_t*)(p + 0+3*stride)=
  418. *(uint32_t*)(p + 4+3*stride)= val;
  419. }else if(w==16){
  420. *(uint32_t*)(p + 0+0*stride)=
  421. *(uint32_t*)(p + 4+0*stride)=
  422. *(uint32_t*)(p + 8+0*stride)=
  423. *(uint32_t*)(p +12+0*stride)=
  424. *(uint32_t*)(p + 0+1*stride)=
  425. *(uint32_t*)(p + 4+1*stride)=
  426. *(uint32_t*)(p + 8+1*stride)=
  427. *(uint32_t*)(p +12+1*stride)= val;
  428. if(h==2) return;
  429. *(uint32_t*)(p + 0+2*stride)=
  430. *(uint32_t*)(p + 4+2*stride)=
  431. *(uint32_t*)(p + 8+2*stride)=
  432. *(uint32_t*)(p +12+2*stride)=
  433. *(uint32_t*)(p + 0+3*stride)=
  434. *(uint32_t*)(p + 4+3*stride)=
  435. *(uint32_t*)(p + 8+3*stride)=
  436. *(uint32_t*)(p +12+3*stride)= val;
  437. #endif
  438. }else
  439. assert(0);
  440. assert(h==4);
  441. }
  442. static void fill_caches(H264Context *h, int mb_type, int for_deblock){
  443. MpegEncContext * const s = &h->s;
  444. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  445. int topleft_xy, top_xy, topright_xy, left_xy[2];
  446. int topleft_type, top_type, topright_type, left_type[2];
  447. int left_block[8];
  448. int i;
  449. //FIXME deblocking could skip the intra and nnz parts.
  450. if(for_deblock && (h->slice_num == 1 || h->slice_table[mb_xy] == h->slice_table[mb_xy-s->mb_stride]) && !FRAME_MBAFF)
  451. return;
  452. //wow what a mess, why didn't they simplify the interlacing&intra stuff, i can't imagine that these complex rules are worth it
  453. top_xy = mb_xy - s->mb_stride;
  454. topleft_xy = top_xy - 1;
  455. topright_xy= top_xy + 1;
  456. left_xy[1] = left_xy[0] = mb_xy-1;
  457. left_block[0]= 0;
  458. left_block[1]= 1;
  459. left_block[2]= 2;
  460. left_block[3]= 3;
  461. left_block[4]= 7;
  462. left_block[5]= 10;
  463. left_block[6]= 8;
  464. left_block[7]= 11;
  465. if(FRAME_MBAFF){
  466. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  467. const int top_pair_xy = pair_xy - s->mb_stride;
  468. const int topleft_pair_xy = top_pair_xy - 1;
  469. const int topright_pair_xy = top_pair_xy + 1;
  470. const int topleft_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
  471. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  472. const int topright_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
  473. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  474. const int curr_mb_frame_flag = !IS_INTERLACED(mb_type);
  475. const int bottom = (s->mb_y & 1);
  476. 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);
  477. if (bottom
  478. ? !curr_mb_frame_flag // bottom macroblock
  479. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  480. ) {
  481. top_xy -= s->mb_stride;
  482. }
  483. if (bottom
  484. ? !curr_mb_frame_flag // bottom macroblock
  485. : (!curr_mb_frame_flag && !topleft_mb_frame_flag) // top macroblock
  486. ) {
  487. topleft_xy -= s->mb_stride;
  488. }
  489. if (bottom
  490. ? !curr_mb_frame_flag // bottom macroblock
  491. : (!curr_mb_frame_flag && !topright_mb_frame_flag) // top macroblock
  492. ) {
  493. topright_xy -= s->mb_stride;
  494. }
  495. if (left_mb_frame_flag != curr_mb_frame_flag) {
  496. left_xy[1] = left_xy[0] = pair_xy - 1;
  497. if (curr_mb_frame_flag) {
  498. if (bottom) {
  499. left_block[0]= 2;
  500. left_block[1]= 2;
  501. left_block[2]= 3;
  502. left_block[3]= 3;
  503. left_block[4]= 8;
  504. left_block[5]= 11;
  505. left_block[6]= 8;
  506. left_block[7]= 11;
  507. } else {
  508. left_block[0]= 0;
  509. left_block[1]= 0;
  510. left_block[2]= 1;
  511. left_block[3]= 1;
  512. left_block[4]= 7;
  513. left_block[5]= 10;
  514. left_block[6]= 7;
  515. left_block[7]= 10;
  516. }
  517. } else {
  518. left_xy[1] += s->mb_stride;
  519. //left_block[0]= 0;
  520. left_block[1]= 2;
  521. left_block[2]= 0;
  522. left_block[3]= 2;
  523. //left_block[4]= 7;
  524. left_block[5]= 10;
  525. left_block[6]= 7;
  526. left_block[7]= 10;
  527. }
  528. }
  529. }
  530. h->top_mb_xy = top_xy;
  531. h->left_mb_xy[0] = left_xy[0];
  532. h->left_mb_xy[1] = left_xy[1];
  533. if(for_deblock){
  534. topleft_type = 0;
  535. topright_type = 0;
  536. top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0;
  537. left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
  538. left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
  539. if(FRAME_MBAFF && !IS_INTRA(mb_type)){
  540. int list;
  541. int v = *(uint16_t*)&h->non_zero_count[mb_xy][14];
  542. for(i=0; i<16; i++)
  543. h->non_zero_count_cache[scan8[i]] = (v>>i)&1;
  544. for(list=0; list<1+(h->slice_type==B_TYPE); list++){
  545. if(USES_LIST(mb_type,list)){
  546. uint32_t *src = (uint32_t*)s->current_picture.motion_val[list][h->mb2b_xy[mb_xy]];
  547. uint32_t *dst = (uint32_t*)h->mv_cache[list][scan8[0]];
  548. int8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
  549. for(i=0; i<4; i++, dst+=8, src+=h->b_stride){
  550. dst[0] = src[0];
  551. dst[1] = src[1];
  552. dst[2] = src[2];
  553. dst[3] = src[3];
  554. }
  555. *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =
  556. *(uint32_t*)&h->ref_cache[list][scan8[ 2]] = pack16to32(ref[0],ref[1])*0x0101;
  557. ref += h->b8_stride;
  558. *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =
  559. *(uint32_t*)&h->ref_cache[list][scan8[10]] = pack16to32(ref[0],ref[1])*0x0101;
  560. }else{
  561. fill_rectangle(&h-> mv_cache[list][scan8[ 0]], 4, 4, 8, 0, 4);
  562. fill_rectangle(&h->ref_cache[list][scan8[ 0]], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  563. }
  564. }
  565. }
  566. }else{
  567. topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
  568. top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
  569. topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
  570. left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
  571. left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
  572. }
  573. if(IS_INTRA(mb_type)){
  574. h->topleft_samples_available=
  575. h->top_samples_available=
  576. h->left_samples_available= 0xFFFF;
  577. h->topright_samples_available= 0xEEEA;
  578. if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
  579. h->topleft_samples_available= 0xB3FF;
  580. h->top_samples_available= 0x33FF;
  581. h->topright_samples_available= 0x26EA;
  582. }
  583. for(i=0; i<2; i++){
  584. if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
  585. h->topleft_samples_available&= 0xDF5F;
  586. h->left_samples_available&= 0x5F5F;
  587. }
  588. }
  589. if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
  590. h->topleft_samples_available&= 0x7FFF;
  591. if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
  592. h->topright_samples_available&= 0xFBFF;
  593. if(IS_INTRA4x4(mb_type)){
  594. if(IS_INTRA4x4(top_type)){
  595. h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
  596. h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
  597. h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
  598. h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
  599. }else{
  600. int pred;
  601. if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred))
  602. pred= -1;
  603. else{
  604. pred= 2;
  605. }
  606. h->intra4x4_pred_mode_cache[4+8*0]=
  607. h->intra4x4_pred_mode_cache[5+8*0]=
  608. h->intra4x4_pred_mode_cache[6+8*0]=
  609. h->intra4x4_pred_mode_cache[7+8*0]= pred;
  610. }
  611. for(i=0; i<2; i++){
  612. if(IS_INTRA4x4(left_type[i])){
  613. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
  614. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
  615. }else{
  616. int pred;
  617. if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred))
  618. pred= -1;
  619. else{
  620. pred= 2;
  621. }
  622. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
  623. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
  624. }
  625. }
  626. }
  627. }
  628. /*
  629. 0 . T T. T T T T
  630. 1 L . .L . . . .
  631. 2 L . .L . . . .
  632. 3 . T TL . . . .
  633. 4 L . .L . . . .
  634. 5 L . .. . . . .
  635. */
  636. //FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec)
  637. if(top_type){
  638. h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
  639. h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
  640. h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
  641. h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
  642. h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
  643. h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
  644. h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
  645. h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
  646. }else{
  647. h->non_zero_count_cache[4+8*0]=
  648. h->non_zero_count_cache[5+8*0]=
  649. h->non_zero_count_cache[6+8*0]=
  650. h->non_zero_count_cache[7+8*0]=
  651. h->non_zero_count_cache[1+8*0]=
  652. h->non_zero_count_cache[2+8*0]=
  653. h->non_zero_count_cache[1+8*3]=
  654. h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  655. }
  656. for (i=0; i<2; i++) {
  657. if(left_type[i]){
  658. h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
  659. h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
  660. h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
  661. h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
  662. }else{
  663. h->non_zero_count_cache[3+8*1 + 2*8*i]=
  664. h->non_zero_count_cache[3+8*2 + 2*8*i]=
  665. h->non_zero_count_cache[0+8*1 + 8*i]=
  666. h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  667. }
  668. }
  669. if( h->pps.cabac ) {
  670. // top_cbp
  671. if(top_type) {
  672. h->top_cbp = h->cbp_table[top_xy];
  673. } else if(IS_INTRA(mb_type)) {
  674. h->top_cbp = 0x1C0;
  675. } else {
  676. h->top_cbp = 0;
  677. }
  678. // left_cbp
  679. if (left_type[0]) {
  680. h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
  681. } else if(IS_INTRA(mb_type)) {
  682. h->left_cbp = 0x1C0;
  683. } else {
  684. h->left_cbp = 0;
  685. }
  686. if (left_type[0]) {
  687. h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
  688. }
  689. if (left_type[1]) {
  690. h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
  691. }
  692. }
  693. #if 1
  694. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  695. int list;
  696. for(list=0; list<1+(h->slice_type==B_TYPE); list++){
  697. if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
  698. /*if(!h->mv_cache_clean[list]){
  699. memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
  700. memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
  701. h->mv_cache_clean[list]= 1;
  702. }*/
  703. continue;
  704. }
  705. h->mv_cache_clean[list]= 0;
  706. if(USES_LIST(top_type, list)){
  707. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  708. const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
  709. *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
  710. *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
  711. *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
  712. *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
  713. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  714. h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
  715. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  716. h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
  717. }else{
  718. *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
  719. *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
  720. *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
  721. *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
  722. *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
  723. }
  724. //FIXME unify cleanup or sth
  725. if(USES_LIST(left_type[0], list)){
  726. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  727. const int b8_xy= h->mb2b8_xy[left_xy[0]] + 1;
  728. *(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]];
  729. *(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]];
  730. h->ref_cache[list][scan8[0] - 1 + 0*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0]>>1)];
  731. h->ref_cache[list][scan8[0] - 1 + 1*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1]>>1)];
  732. }else{
  733. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 0*8]=
  734. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 1*8]= 0;
  735. h->ref_cache[list][scan8[0] - 1 + 0*8]=
  736. h->ref_cache[list][scan8[0] - 1 + 1*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  737. }
  738. if(USES_LIST(left_type[1], list)){
  739. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  740. const int b8_xy= h->mb2b8_xy[left_xy[1]] + 1;
  741. *(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]];
  742. *(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]];
  743. h->ref_cache[list][scan8[0] - 1 + 2*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[2]>>1)];
  744. h->ref_cache[list][scan8[0] - 1 + 3*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[3]>>1)];
  745. }else{
  746. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 2*8]=
  747. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 3*8]= 0;
  748. h->ref_cache[list][scan8[0] - 1 + 2*8]=
  749. h->ref_cache[list][scan8[0] - 1 + 3*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  750. assert((!left_type[0]) == (!left_type[1]));
  751. }
  752. if((for_deblock || (IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred)) && !FRAME_MBAFF)
  753. continue;
  754. if(USES_LIST(topleft_type, list)){
  755. const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
  756. const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + h->b8_stride;
  757. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  758. h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  759. }else{
  760. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
  761. h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  762. }
  763. if(USES_LIST(topright_type, list)){
  764. const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
  765. const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
  766. *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  767. h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  768. }else{
  769. *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
  770. h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  771. }
  772. if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)
  773. continue;
  774. h->ref_cache[list][scan8[5 ]+1] =
  775. h->ref_cache[list][scan8[7 ]+1] =
  776. h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewhere else)
  777. h->ref_cache[list][scan8[4 ]] =
  778. h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
  779. *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
  780. *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
  781. *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  782. *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
  783. *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
  784. if( h->pps.cabac ) {
  785. /* XXX beurk, Load mvd */
  786. if(USES_LIST(top_type, list)){
  787. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  788. *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
  789. *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
  790. *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
  791. *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
  792. }else{
  793. *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
  794. *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
  795. *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
  796. *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
  797. }
  798. if(USES_LIST(left_type[0], list)){
  799. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  800. *(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]];
  801. *(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]];
  802. }else{
  803. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
  804. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
  805. }
  806. if(USES_LIST(left_type[1], list)){
  807. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  808. *(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]];
  809. *(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]];
  810. }else{
  811. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
  812. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
  813. }
  814. *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
  815. *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
  816. *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  817. *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
  818. *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
  819. if(h->slice_type == B_TYPE){
  820. fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
  821. if(IS_DIRECT(top_type)){
  822. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
  823. }else if(IS_8X8(top_type)){
  824. int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
  825. h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
  826. h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
  827. }else{
  828. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
  829. }
  830. if(IS_DIRECT(left_type[0]))
  831. h->direct_cache[scan8[0] - 1 + 0*8]= 1;
  832. else if(IS_8X8(left_type[0]))
  833. 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)];
  834. else
  835. h->direct_cache[scan8[0] - 1 + 0*8]= 0;
  836. if(IS_DIRECT(left_type[1]))
  837. h->direct_cache[scan8[0] - 1 + 2*8]= 1;
  838. else if(IS_8X8(left_type[1]))
  839. 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)];
  840. else
  841. h->direct_cache[scan8[0] - 1 + 2*8]= 0;
  842. }
  843. }
  844. if(FRAME_MBAFF){
  845. #define MAP_MVS\
  846. MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
  847. MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
  848. MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
  849. MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
  850. MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
  851. MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
  852. MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\
  853. MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\
  854. MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\
  855. MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
  856. if(MB_FIELD){
  857. #define MAP_F2F(idx, mb_type)\
  858. if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  859. h->ref_cache[list][idx] <<= 1;\
  860. h->mv_cache[list][idx][1] /= 2;\
  861. h->mvd_cache[list][idx][1] /= 2;\
  862. }
  863. MAP_MVS
  864. #undef MAP_F2F
  865. }else{
  866. #define MAP_F2F(idx, mb_type)\
  867. if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  868. h->ref_cache[list][idx] >>= 1;\
  869. h->mv_cache[list][idx][1] <<= 1;\
  870. h->mvd_cache[list][idx][1] <<= 1;\
  871. }
  872. MAP_MVS
  873. #undef MAP_F2F
  874. }
  875. }
  876. }
  877. }
  878. #endif
  879. h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
  880. }
  881. static inline void write_back_intra_pred_mode(H264Context *h){
  882. MpegEncContext * const s = &h->s;
  883. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  884. h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
  885. h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
  886. h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
  887. h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
  888. h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
  889. h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
  890. h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
  891. }
  892. /**
  893. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  894. */
  895. static inline int check_intra4x4_pred_mode(H264Context *h){
  896. MpegEncContext * const s = &h->s;
  897. static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
  898. static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
  899. int i;
  900. if(!(h->top_samples_available&0x8000)){
  901. for(i=0; i<4; i++){
  902. int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
  903. if(status<0){
  904. 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);
  905. return -1;
  906. } else if(status){
  907. h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
  908. }
  909. }
  910. }
  911. if(!(h->left_samples_available&0x8000)){
  912. for(i=0; i<4; i++){
  913. int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
  914. if(status<0){
  915. 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);
  916. return -1;
  917. } else if(status){
  918. h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
  919. }
  920. }
  921. }
  922. return 0;
  923. } //FIXME cleanup like next
  924. /**
  925. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  926. */
  927. static inline int check_intra_pred_mode(H264Context *h, int mode){
  928. MpegEncContext * const s = &h->s;
  929. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  930. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  931. if(mode < 0 || mode > 6) {
  932. 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);
  933. return -1;
  934. }
  935. if(!(h->top_samples_available&0x8000)){
  936. mode= top[ mode ];
  937. if(mode<0){
  938. 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);
  939. return -1;
  940. }
  941. }
  942. if(!(h->left_samples_available&0x8000)){
  943. mode= left[ mode ];
  944. if(mode<0){
  945. 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);
  946. return -1;
  947. }
  948. }
  949. return mode;
  950. }
  951. /**
  952. * gets the predicted intra4x4 prediction mode.
  953. */
  954. static inline int pred_intra_mode(H264Context *h, int n){
  955. const int index8= scan8[n];
  956. const int left= h->intra4x4_pred_mode_cache[index8 - 1];
  957. const int top = h->intra4x4_pred_mode_cache[index8 - 8];
  958. const int min= FFMIN(left, top);
  959. tprintf("mode:%d %d min:%d\n", left ,top, min);
  960. if(min<0) return DC_PRED;
  961. else return min;
  962. }
  963. static inline void write_back_non_zero_count(H264Context *h){
  964. MpegEncContext * const s = &h->s;
  965. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  966. h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
  967. h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
  968. h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
  969. h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
  970. h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
  971. h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
  972. h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
  973. h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
  974. h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
  975. h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
  976. h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
  977. h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
  978. h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
  979. if(FRAME_MBAFF){
  980. // store all luma nnzs, for deblocking
  981. int v = 0, i;
  982. for(i=0; i<16; i++)
  983. v += (!!h->non_zero_count_cache[scan8[i]]) << i;
  984. *(uint16_t*)&h->non_zero_count[mb_xy][14] = v;
  985. }
  986. }
  987. /**
  988. * gets the predicted number of non zero coefficients.
  989. * @param n block index
  990. */
  991. static inline int pred_non_zero_count(H264Context *h, int n){
  992. const int index8= scan8[n];
  993. const int left= h->non_zero_count_cache[index8 - 1];
  994. const int top = h->non_zero_count_cache[index8 - 8];
  995. int i= left + top;
  996. if(i<64) i= (i+1)>>1;
  997. tprintf("pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
  998. return i&31;
  999. }
  1000. static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
  1001. const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
  1002. /* there is no consistent mapping of mvs to neighboring locations that will
  1003. * make mbaff happy, so we can't move all this logic to fill_caches */
  1004. if(FRAME_MBAFF){
  1005. MpegEncContext *s = &h->s;
  1006. const uint32_t *mb_types = s->current_picture_ptr->mb_type;
  1007. const int16_t *mv;
  1008. *(uint32_t*)h->mv_cache[list][scan8[0]-2] = 0;
  1009. *C = h->mv_cache[list][scan8[0]-2];
  1010. if(!MB_FIELD
  1011. && (s->mb_y&1) && i < scan8[0]+8 && topright_ref != PART_NOT_AVAILABLE){
  1012. int topright_xy = s->mb_x + (s->mb_y-1)*s->mb_stride + (i == scan8[0]+3);
  1013. if(IS_INTERLACED(mb_types[topright_xy])){
  1014. #define SET_DIAG_MV(MV_OP, REF_OP, X4, Y4)\
  1015. const int x4 = X4, y4 = Y4;\
  1016. const int mb_type = mb_types[(x4>>2)+(y4>>2)*s->mb_stride];\
  1017. if(!USES_LIST(mb_type,list) && !IS_8X8(mb_type))\
  1018. return LIST_NOT_USED;\
  1019. mv = s->current_picture_ptr->motion_val[list][x4 + y4*h->b_stride];\
  1020. h->mv_cache[list][scan8[0]-2][0] = mv[0];\
  1021. h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
  1022. return s->current_picture_ptr->ref_index[list][(x4>>1) + (y4>>1)*h->b8_stride] REF_OP;
  1023. SET_DIAG_MV(*2, >>1, s->mb_x*4+(i&7)-4+part_width, s->mb_y*4-1);
  1024. }
  1025. }
  1026. if(topright_ref == PART_NOT_AVAILABLE
  1027. && ((s->mb_y&1) || i >= scan8[0]+8) && (i&7)==4
  1028. && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
  1029. if(!MB_FIELD
  1030. && IS_INTERLACED(mb_types[h->left_mb_xy[0]])){
  1031. SET_DIAG_MV(*2, >>1, s->mb_x*4-1, (s->mb_y|1)*4+(s->mb_y&1)*2+(i>>4)-1);
  1032. }
  1033. if(MB_FIELD
  1034. && !IS_INTERLACED(mb_types[h->left_mb_xy[0]])
  1035. && i >= scan8[0]+8){
  1036. // leftshift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's ok.
  1037. SET_DIAG_MV(>>1, <<1, s->mb_x*4-1, (s->mb_y&~1)*4 - 1 + ((i-scan8[0])>>3)*2);
  1038. }
  1039. }
  1040. #undef SET_DIAG_MV
  1041. }
  1042. if(topright_ref != PART_NOT_AVAILABLE){
  1043. *C= h->mv_cache[list][ i - 8 + part_width ];
  1044. return topright_ref;
  1045. }else{
  1046. tprintf("topright MV not available\n");
  1047. *C= h->mv_cache[list][ i - 8 - 1 ];
  1048. return h->ref_cache[list][ i - 8 - 1 ];
  1049. }
  1050. }
  1051. /**
  1052. * gets the predicted MV.
  1053. * @param n the block index
  1054. * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
  1055. * @param mx the x component of the predicted motion vector
  1056. * @param my the y component of the predicted motion vector
  1057. */
  1058. static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
  1059. const int index8= scan8[n];
  1060. const int top_ref= h->ref_cache[list][ index8 - 8 ];
  1061. const int left_ref= h->ref_cache[list][ index8 - 1 ];
  1062. const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
  1063. const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
  1064. const int16_t * C;
  1065. int diagonal_ref, match_count;
  1066. assert(part_width==1 || part_width==2 || part_width==4);
  1067. /* mv_cache
  1068. B . . A T T T T
  1069. U . . L . . , .
  1070. U . . L . . . .
  1071. U . . L . . , .
  1072. . . . L . . . .
  1073. */
  1074. diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
  1075. match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
  1076. tprintf("pred_motion match_count=%d\n", match_count);
  1077. if(match_count > 1){ //most common
  1078. *mx= mid_pred(A[0], B[0], C[0]);
  1079. *my= mid_pred(A[1], B[1], C[1]);
  1080. }else if(match_count==1){
  1081. if(left_ref==ref){
  1082. *mx= A[0];
  1083. *my= A[1];
  1084. }else if(top_ref==ref){
  1085. *mx= B[0];
  1086. *my= B[1];
  1087. }else{
  1088. *mx= C[0];
  1089. *my= C[1];
  1090. }
  1091. }else{
  1092. if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
  1093. *mx= A[0];
  1094. *my= A[1];
  1095. }else{
  1096. *mx= mid_pred(A[0], B[0], C[0]);
  1097. *my= mid_pred(A[1], B[1], C[1]);
  1098. }
  1099. }
  1100. 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);
  1101. }
  1102. /**
  1103. * gets the directionally predicted 16x8 MV.
  1104. * @param n the block index
  1105. * @param mx the x component of the predicted motion vector
  1106. * @param my the y component of the predicted motion vector
  1107. */
  1108. static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  1109. if(n==0){
  1110. const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
  1111. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  1112. 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);
  1113. if(top_ref == ref){
  1114. *mx= B[0];
  1115. *my= B[1];
  1116. return;
  1117. }
  1118. }else{
  1119. const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
  1120. const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
  1121. 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);
  1122. if(left_ref == ref){
  1123. *mx= A[0];
  1124. *my= A[1];
  1125. return;
  1126. }
  1127. }
  1128. //RARE
  1129. pred_motion(h, n, 4, list, ref, mx, my);
  1130. }
  1131. /**
  1132. * gets the directionally predicted 8x16 MV.
  1133. * @param n the block index
  1134. * @param mx the x component of the predicted motion vector
  1135. * @param my the y component of the predicted motion vector
  1136. */
  1137. static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  1138. if(n==0){
  1139. const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
  1140. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  1141. 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);
  1142. if(left_ref == ref){
  1143. *mx= A[0];
  1144. *my= A[1];
  1145. return;
  1146. }
  1147. }else{
  1148. const int16_t * C;
  1149. int diagonal_ref;
  1150. diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
  1151. 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);
  1152. if(diagonal_ref == ref){
  1153. *mx= C[0];
  1154. *my= C[1];
  1155. return;
  1156. }
  1157. }
  1158. //RARE
  1159. pred_motion(h, n, 2, list, ref, mx, my);
  1160. }
  1161. static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
  1162. const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
  1163. const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
  1164. tprintf("pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
  1165. if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
  1166. || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
  1167. || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
  1168. *mx = *my = 0;
  1169. return;
  1170. }
  1171. pred_motion(h, 0, 4, 0, 0, mx, my);
  1172. return;
  1173. }
  1174. static inline void direct_dist_scale_factor(H264Context * const h){
  1175. const int poc = h->s.current_picture_ptr->poc;
  1176. const int poc1 = h->ref_list[1][0].poc;
  1177. int i;
  1178. for(i=0; i<h->ref_count[0]; i++){
  1179. int poc0 = h->ref_list[0][i].poc;
  1180. int td = clip(poc1 - poc0, -128, 127);
  1181. if(td == 0 /* FIXME || pic0 is a long-term ref */){
  1182. h->dist_scale_factor[i] = 256;
  1183. }else{
  1184. int tb = clip(poc - poc0, -128, 127);
  1185. int tx = (16384 + (FFABS(td) >> 1)) / td;
  1186. h->dist_scale_factor[i] = clip((tb*tx + 32) >> 6, -1024, 1023);
  1187. }
  1188. }
  1189. if(FRAME_MBAFF){
  1190. for(i=0; i<h->ref_count[0]; i++){
  1191. h->dist_scale_factor_field[2*i] =
  1192. h->dist_scale_factor_field[2*i+1] = h->dist_scale_factor[i];
  1193. }
  1194. }
  1195. }
  1196. static inline void direct_ref_list_init(H264Context * const h){
  1197. MpegEncContext * const s = &h->s;
  1198. Picture * const ref1 = &h->ref_list[1][0];
  1199. Picture * const cur = s->current_picture_ptr;
  1200. int list, i, j;
  1201. if(cur->pict_type == I_TYPE)
  1202. cur->ref_count[0] = 0;
  1203. if(cur->pict_type != B_TYPE)
  1204. cur->ref_count[1] = 0;
  1205. for(list=0; list<2; list++){
  1206. cur->ref_count[list] = h->ref_count[list];
  1207. for(j=0; j<h->ref_count[list]; j++)
  1208. cur->ref_poc[list][j] = h->ref_list[list][j].poc;
  1209. }
  1210. if(cur->pict_type != B_TYPE || h->direct_spatial_mv_pred)
  1211. return;
  1212. for(list=0; list<2; list++){
  1213. for(i=0; i<ref1->ref_count[list]; i++){
  1214. const int poc = ref1->ref_poc[list][i];
  1215. h->map_col_to_list0[list][i] = 0; /* bogus; fills in for missing frames */
  1216. for(j=0; j<h->ref_count[list]; j++)
  1217. if(h->ref_list[list][j].poc == poc){
  1218. h->map_col_to_list0[list][i] = j;
  1219. break;
  1220. }
  1221. }
  1222. }
  1223. if(FRAME_MBAFF){
  1224. for(list=0; list<2; list++){
  1225. for(i=0; i<ref1->ref_count[list]; i++){
  1226. j = h->map_col_to_list0[list][i];
  1227. h->map_col_to_list0_field[list][2*i] = 2*j;
  1228. h->map_col_to_list0_field[list][2*i+1] = 2*j+1;
  1229. }
  1230. }
  1231. }
  1232. }
  1233. static inline void pred_direct_motion(H264Context * const h, int *mb_type){
  1234. MpegEncContext * const s = &h->s;
  1235. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  1236. const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1237. const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1238. const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
  1239. const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
  1240. const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[1][b4_xy];
  1241. const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
  1242. const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy];
  1243. const int is_b8x8 = IS_8X8(*mb_type);
  1244. unsigned int sub_mb_type;
  1245. int i8, i4;
  1246. #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
  1247. if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
  1248. /* FIXME save sub mb types from previous frames (or derive from MVs)
  1249. * so we know exactly what block size to use */
  1250. sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  1251. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  1252. }else if(!is_b8x8 && (mb_type_col & MB_TYPE_16x16_OR_INTRA)){
  1253. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  1254. *mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
  1255. }else{
  1256. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  1257. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  1258. }
  1259. if(!is_b8x8)
  1260. *mb_type |= MB_TYPE_DIRECT2;
  1261. if(MB_FIELD)
  1262. *mb_type |= MB_TYPE_INTERLACED;
  1263. 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);
  1264. if(h->direct_spatial_mv_pred){
  1265. int ref[2];
  1266. int mv[2][2];
  1267. int list;
  1268. /* FIXME interlacing + spatial direct uses wrong colocated block positions */
  1269. /* ref = min(neighbors) */
  1270. for(list=0; list<2; list++){
  1271. int refa = h->ref_cache[list][scan8[0] - 1];
  1272. int refb = h->ref_cache[list][scan8[0] - 8];
  1273. int refc = h->ref_cache[list][scan8[0] - 8 + 4];
  1274. if(refc == -2)
  1275. refc = h->ref_cache[list][scan8[0] - 8 - 1];
  1276. ref[list] = refa;
  1277. if(ref[list] < 0 || (refb < ref[list] && refb >= 0))
  1278. ref[list] = refb;
  1279. if(ref[list] < 0 || (refc < ref[list] && refc >= 0))
  1280. ref[list] = refc;
  1281. if(ref[list] < 0)
  1282. ref[list] = -1;
  1283. }
  1284. if(ref[0] < 0 && ref[1] < 0){
  1285. ref[0] = ref[1] = 0;
  1286. mv[0][0] = mv[0][1] =
  1287. mv[1][0] = mv[1][1] = 0;
  1288. }else{
  1289. for(list=0; list<2; list++){
  1290. if(ref[list] >= 0)
  1291. pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
  1292. else
  1293. mv[list][0] = mv[list][1] = 0;
  1294. }
  1295. }
  1296. if(ref[1] < 0){
  1297. *mb_type &= ~MB_TYPE_P0L1;
  1298. sub_mb_type &= ~MB_TYPE_P0L1;
  1299. }else if(ref[0] < 0){
  1300. *mb_type &= ~MB_TYPE_P0L0;
  1301. sub_mb_type &= ~MB_TYPE_P0L0;
  1302. }
  1303. if(IS_16X16(*mb_type)){
  1304. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  1305. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  1306. if(!IS_INTRA(mb_type_col)
  1307. && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
  1308. || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
  1309. && (h->x264_build>33 || !h->x264_build)))){
  1310. if(ref[0] > 0)
  1311. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1312. else
  1313. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  1314. if(ref[1] > 0)
  1315. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1316. else
  1317. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  1318. }else{
  1319. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1320. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1321. }
  1322. }else{
  1323. for(i8=0; i8<4; i8++){
  1324. const int x8 = i8&1;
  1325. const int y8 = i8>>1;
  1326. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1327. continue;
  1328. h->sub_mb_type[i8] = sub_mb_type;
  1329. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1330. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1331. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  1332. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  1333. /* col_zero_flag */
  1334. if(!IS_INTRA(mb_type_col) && ( l1ref0[x8 + y8*h->b8_stride] == 0
  1335. || (l1ref0[x8 + y8*h->b8_stride] < 0 && l1ref1[x8 + y8*h->b8_stride] == 0
  1336. && (h->x264_build>33 || !h->x264_build)))){
  1337. const int16_t (*l1mv)[2]= l1ref0[x8 + y8*h->b8_stride] == 0 ? l1mv0 : l1mv1;
  1338. if(IS_SUB_8X8(sub_mb_type)){
  1339. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1340. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  1341. if(ref[0] == 0)
  1342. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1343. if(ref[1] == 0)
  1344. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1345. }
  1346. }else
  1347. for(i4=0; i4<4; i4++){
  1348. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1349. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  1350. if(ref[0] == 0)
  1351. *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
  1352. if(ref[1] == 0)
  1353. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
  1354. }
  1355. }
  1356. }
  1357. }
  1358. }
  1359. }else{ /* direct temporal mv pred */
  1360. const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
  1361. const int *dist_scale_factor = h->dist_scale_factor;
  1362. if(FRAME_MBAFF){
  1363. if(IS_INTERLACED(*mb_type)){
  1364. map_col_to_list0[0] = h->map_col_to_list0_field[0];
  1365. map_col_to_list0[1] = h->map_col_to_list0_field[1];
  1366. dist_scale_factor = h->dist_scale_factor_field;
  1367. }
  1368. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col)){
  1369. /* FIXME assumes direct_8x8_inference == 1 */
  1370. const int pair_xy = s->mb_x + (s->mb_y&~1)*s->mb_stride;
  1371. int mb_types_col[2];
  1372. int y_shift;
  1373. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1
  1374. | (is_b8x8 ? 0 : MB_TYPE_DIRECT2)
  1375. | (*mb_type & MB_TYPE_INTERLACED);
  1376. sub_mb_type = MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_16x16;
  1377. if(IS_INTERLACED(*mb_type)){
  1378. /* frame to field scaling */
  1379. mb_types_col[0] = h->ref_list[1][0].mb_type[pair_xy];
  1380. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1381. if(s->mb_y&1){
  1382. l1ref0 -= 2*h->b8_stride;
  1383. l1ref1 -= 2*h->b8_stride;
  1384. l1mv0 -= 4*h->b_stride;
  1385. l1mv1 -= 4*h->b_stride;
  1386. }
  1387. y_shift = 0;
  1388. if( (mb_types_col[0] & MB_TYPE_16x16_OR_INTRA)
  1389. && (mb_types_col[1] & MB_TYPE_16x16_OR_INTRA)
  1390. && !is_b8x8)
  1391. *mb_type |= MB_TYPE_16x8;
  1392. else
  1393. *mb_type |= MB_TYPE_8x8;
  1394. }else{
  1395. /* field to frame scaling */
  1396. /* col_mb_y = (mb_y&~1) + (topAbsDiffPOC < bottomAbsDiffPOC ? 0 : 1)
  1397. * but in MBAFF, top and bottom POC are equal */
  1398. int dy = (s->mb_y&1) ? 1 : 2;
  1399. mb_types_col[0] =
  1400. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1401. l1ref0 += dy*h->b8_stride;
  1402. l1ref1 += dy*h->b8_stride;
  1403. l1mv0 += 2*dy*h->b_stride;
  1404. l1mv1 += 2*dy*h->b_stride;
  1405. y_shift = 2;
  1406. if((mb_types_col[0] & (MB_TYPE_16x16_OR_INTRA|MB_TYPE_16x8))
  1407. && !is_b8x8)
  1408. *mb_type |= MB_TYPE_16x16;
  1409. else
  1410. *mb_type |= MB_TYPE_8x8;
  1411. }
  1412. for(i8=0; i8<4; i8++){
  1413. const int x8 = i8&1;
  1414. const int y8 = i8>>1;
  1415. int ref0, scale;
  1416. const int16_t (*l1mv)[2]= l1mv0;
  1417. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1418. continue;
  1419. h->sub_mb_type[i8] = sub_mb_type;
  1420. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1421. if(IS_INTRA(mb_types_col[y8])){
  1422. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1423. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1424. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1425. continue;
  1426. }
  1427. ref0 = l1ref0[x8 + (y8*2>>y_shift)*h->b8_stride];
  1428. if(ref0 >= 0)
  1429. ref0 = map_col_to_list0[0][ref0*2>>y_shift];
  1430. else{
  1431. ref0 = map_col_to_list0[1][l1ref1[x8 + (y8*2>>y_shift)*h->b8_stride]*2>>y_shift];
  1432. l1mv= l1mv1;
  1433. }
  1434. scale = dist_scale_factor[ref0];
  1435. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1436. {
  1437. const int16_t *mv_col = l1mv[x8*3 + (y8*6>>y_shift)*h->b_stride];
  1438. int my_col = (mv_col[1]<<y_shift)/2;
  1439. int mx = (scale * mv_col[0] + 128) >> 8;
  1440. int my = (scale * my_col + 128) >> 8;
  1441. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1442. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
  1443. }
  1444. }
  1445. return;
  1446. }
  1447. }
  1448. /* one-to-one mv scaling */
  1449. if(IS_16X16(*mb_type)){
  1450. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  1451. if(IS_INTRA(mb_type_col)){
  1452. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  1453. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  1454. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  1455. }else{
  1456. const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0]]
  1457. : map_col_to_list0[1][l1ref1[0]];
  1458. const int scale = dist_scale_factor[ref0];
  1459. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  1460. int mv_l0[2];
  1461. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1462. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1463. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref0, 1);
  1464. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0],mv_l0[1]), 4);
  1465. 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);
  1466. }
  1467. }else{
  1468. for(i8=0; i8<4; i8++){
  1469. const int x8 = i8&1;
  1470. const int y8 = i8>>1;
  1471. int ref0, scale;
  1472. const int16_t (*l1mv)[2]= l1mv0;
  1473. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1474. continue;
  1475. h->sub_mb_type[i8] = sub_mb_type;
  1476. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1477. if(IS_INTRA(mb_type_col)){
  1478. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1479. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1480. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1481. continue;
  1482. }
  1483. ref0 = l1ref0[x8 + y8*h->b8_stride];
  1484. if(ref0 >= 0)
  1485. ref0 = map_col_to_list0[0][ref0];
  1486. else{
  1487. ref0 = map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]];
  1488. l1mv= l1mv1;
  1489. }
  1490. scale = dist_scale_factor[ref0];
  1491. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1492. if(IS_SUB_8X8(sub_mb_type)){
  1493. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1494. int mx = (scale * mv_col[0] + 128) >> 8;
  1495. int my = (scale * mv_col[1] + 128) >> 8;
  1496. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1497. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
  1498. }else
  1499. for(i4=0; i4<4; i4++){
  1500. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1501. int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
  1502. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1503. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1504. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
  1505. pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  1506. }
  1507. }
  1508. }
  1509. }
  1510. }
  1511. static inline void write_back_motion(H264Context *h, int mb_type){
  1512. MpegEncContext * const s = &h->s;
  1513. const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1514. const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1515. int list;
  1516. if(!USES_LIST(mb_type, 0))
  1517. fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, h->b8_stride, (uint8_t)LIST_NOT_USED, 1);
  1518. for(list=0; list<2; list++){
  1519. int y;
  1520. if(!USES_LIST(mb_type, list))
  1521. continue;
  1522. for(y=0; y<4; y++){
  1523. *(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];
  1524. *(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];
  1525. }
  1526. if( h->pps.cabac ) {
  1527. if(IS_SKIP(mb_type))
  1528. fill_rectangle(h->mvd_table[list][b_xy], 4, 4, h->b_stride, 0, 4);
  1529. else
  1530. for(y=0; y<4; y++){
  1531. *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
  1532. *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
  1533. }
  1534. }
  1535. {
  1536. int8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
  1537. ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]];
  1538. ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]];
  1539. ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]];
  1540. ref_index[1+1*h->b8_stride]= h->ref_cache[list][scan8[12]];
  1541. }
  1542. }
  1543. if(h->slice_type == B_TYPE && h->pps.cabac){
  1544. if(IS_8X8(mb_type)){
  1545. uint8_t *direct_table = &h->direct_table[b8_xy];
  1546. direct_table[1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
  1547. direct_table[0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
  1548. direct_table[1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
  1549. }
  1550. }
  1551. }
  1552. /**
  1553. * Decodes a network abstraction layer unit.
  1554. * @param consumed is the number of bytes used as input
  1555. * @param length is the length of the array
  1556. * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp tailing?
  1557. * @returns decoded bytes, might be src+1 if no escapes
  1558. */
  1559. static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){
  1560. int i, si, di;
  1561. uint8_t *dst;
  1562. // src[0]&0x80; //forbidden bit
  1563. h->nal_ref_idc= src[0]>>5;
  1564. h->nal_unit_type= src[0]&0x1F;
  1565. src++; length--;
  1566. #if 0
  1567. for(i=0; i<length; i++)
  1568. printf("%2X ", src[i]);
  1569. #endif
  1570. for(i=0; i+1<length; i+=2){
  1571. if(src[i]) continue;
  1572. if(i>0 && src[i-1]==0) i--;
  1573. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  1574. if(src[i+2]!=3){
  1575. /* startcode, so we must be past the end */
  1576. length=i;
  1577. }
  1578. break;
  1579. }
  1580. }
  1581. if(i>=length-1){ //no escaped 0
  1582. *dst_length= length;
  1583. *consumed= length+1; //+1 for the header
  1584. return src;
  1585. }
  1586. h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length);
  1587. dst= h->rbsp_buffer;
  1588. if (dst == NULL){
  1589. return NULL;
  1590. }
  1591. //printf("decoding esc\n");
  1592. si=di=0;
  1593. while(si<length){
  1594. //remove escapes (very rare 1:2^22)
  1595. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  1596. if(src[si+2]==3){ //escape
  1597. dst[di++]= 0;
  1598. dst[di++]= 0;
  1599. si+=3;
  1600. continue;
  1601. }else //next start code
  1602. break;
  1603. }
  1604. dst[di++]= src[si++];
  1605. }
  1606. *dst_length= di;
  1607. *consumed= si + 1;//+1 for the header
  1608. //FIXME store exact number of bits in the getbitcontext (its needed for decoding)
  1609. return dst;
  1610. }
  1611. /**
  1612. * identifies the exact end of the bitstream
  1613. * @return the length of the trailing, or 0 if damaged
  1614. */
  1615. static int decode_rbsp_trailing(uint8_t *src){
  1616. int v= *src;
  1617. int r;
  1618. tprintf("rbsp trailing %X\n", v);
  1619. for(r=1; r<9; r++){
  1620. if(v&1) return r;
  1621. v>>=1;
  1622. }
  1623. return 0;
  1624. }
  1625. /**
  1626. * idct tranforms the 16 dc values and dequantize them.
  1627. * @param qp quantization parameter
  1628. */
  1629. static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1630. #define stride 16
  1631. int i;
  1632. int temp[16]; //FIXME check if this is a good idea
  1633. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1634. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1635. //memset(block, 64, 2*256);
  1636. //return;
  1637. for(i=0; i<4; i++){
  1638. const int offset= y_offset[i];
  1639. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1640. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1641. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1642. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1643. temp[4*i+0]= z0+z3;
  1644. temp[4*i+1]= z1+z2;
  1645. temp[4*i+2]= z1-z2;
  1646. temp[4*i+3]= z0-z3;
  1647. }
  1648. for(i=0; i<4; i++){
  1649. const int offset= x_offset[i];
  1650. const int z0= temp[4*0+i] + temp[4*2+i];
  1651. const int z1= temp[4*0+i] - temp[4*2+i];
  1652. const int z2= temp[4*1+i] - temp[4*3+i];
  1653. const int z3= temp[4*1+i] + temp[4*3+i];
  1654. block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_resdual
  1655. block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
  1656. block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
  1657. block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
  1658. }
  1659. }
  1660. #if 0
  1661. /**
  1662. * dct tranforms the 16 dc values.
  1663. * @param qp quantization parameter ??? FIXME
  1664. */
  1665. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  1666. // const int qmul= dequant_coeff[qp][0];
  1667. int i;
  1668. int temp[16]; //FIXME check if this is a good idea
  1669. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1670. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1671. for(i=0; i<4; i++){
  1672. const int offset= y_offset[i];
  1673. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1674. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1675. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1676. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1677. temp[4*i+0]= z0+z3;
  1678. temp[4*i+1]= z1+z2;
  1679. temp[4*i+2]= z1-z2;
  1680. temp[4*i+3]= z0-z3;
  1681. }
  1682. for(i=0; i<4; i++){
  1683. const int offset= x_offset[i];
  1684. const int z0= temp[4*0+i] + temp[4*2+i];
  1685. const int z1= temp[4*0+i] - temp[4*2+i];
  1686. const int z2= temp[4*1+i] - temp[4*3+i];
  1687. const int z3= temp[4*1+i] + temp[4*3+i];
  1688. block[stride*0 +offset]= (z0 + z3)>>1;
  1689. block[stride*2 +offset]= (z1 + z2)>>1;
  1690. block[stride*8 +offset]= (z1 - z2)>>1;
  1691. block[stride*10+offset]= (z0 - z3)>>1;
  1692. }
  1693. }
  1694. #endif
  1695. #undef xStride
  1696. #undef stride
  1697. static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1698. const int stride= 16*2;
  1699. const int xStride= 16;
  1700. int a,b,c,d,e;
  1701. a= block[stride*0 + xStride*0];
  1702. b= block[stride*0 + xStride*1];
  1703. c= block[stride*1 + xStride*0];
  1704. d= block[stride*1 + xStride*1];
  1705. e= a-b;
  1706. a= a+b;
  1707. b= c-d;
  1708. c= c+d;
  1709. block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
  1710. block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
  1711. block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
  1712. block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
  1713. }
  1714. #if 0
  1715. static void chroma_dc_dct_c(DCTELEM *block){
  1716. const int stride= 16*2;
  1717. const int xStride= 16;
  1718. int a,b,c,d,e;
  1719. a= block[stride*0 + xStride*0];
  1720. b= block[stride*0 + xStride*1];
  1721. c= block[stride*1 + xStride*0];
  1722. d= block[stride*1 + xStride*1];
  1723. e= a-b;
  1724. a= a+b;
  1725. b= c-d;
  1726. c= c+d;
  1727. block[stride*0 + xStride*0]= (a+c);
  1728. block[stride*0 + xStride*1]= (e+b);
  1729. block[stride*1 + xStride*0]= (a-c);
  1730. block[stride*1 + xStride*1]= (e-b);
  1731. }
  1732. #endif
  1733. /**
  1734. * gets the chroma qp.
  1735. */
  1736. static inline int get_chroma_qp(int chroma_qp_index_offset, int qscale){
  1737. return chroma_qp[clip(qscale + chroma_qp_index_offset, 0, 51)];
  1738. }
  1739. //FIXME need to check that this doesnt overflow signed 32 bit for low qp, i am not sure, it's very close
  1740. //FIXME check that gcc inlines this (and optimizes intra & seperate_dc stuff away)
  1741. static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int seperate_dc){
  1742. int i;
  1743. const int * const quant_table= quant_coeff[qscale];
  1744. const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
  1745. const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
  1746. const unsigned int threshold2= (threshold1<<1);
  1747. int last_non_zero;
  1748. if(seperate_dc){
  1749. if(qscale<=18){
  1750. //avoid overflows
  1751. const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
  1752. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
  1753. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1754. int level= block[0]*quant_coeff[qscale+18][0];
  1755. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1756. if(level>0){
  1757. level= (dc_bias + level)>>(QUANT_SHIFT-2);
  1758. block[0]= level;
  1759. }else{
  1760. level= (dc_bias - level)>>(QUANT_SHIFT-2);
  1761. block[0]= -level;
  1762. }
  1763. // last_non_zero = i;
  1764. }else{
  1765. block[0]=0;
  1766. }
  1767. }else{
  1768. const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
  1769. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
  1770. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1771. int level= block[0]*quant_table[0];
  1772. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1773. if(level>0){
  1774. level= (dc_bias + level)>>(QUANT_SHIFT+1);
  1775. block[0]= level;
  1776. }else{
  1777. level= (dc_bias - level)>>(QUANT_SHIFT+1);
  1778. block[0]= -level;
  1779. }
  1780. // last_non_zero = i;
  1781. }else{
  1782. block[0]=0;
  1783. }
  1784. }
  1785. last_non_zero= 0;
  1786. i=1;
  1787. }else{
  1788. last_non_zero= -1;
  1789. i=0;
  1790. }
  1791. for(; i<16; i++){
  1792. const int j= scantable[i];
  1793. int level= block[j]*quant_table[j];
  1794. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  1795. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  1796. if(((unsigned)(level+threshold1))>threshold2){
  1797. if(level>0){
  1798. level= (bias + level)>>QUANT_SHIFT;
  1799. block[j]= level;
  1800. }else{
  1801. level= (bias - level)>>QUANT_SHIFT;
  1802. block[j]= -level;
  1803. }
  1804. last_non_zero = i;
  1805. }else{
  1806. block[j]=0;
  1807. }
  1808. }
  1809. return last_non_zero;
  1810. }
  1811. static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){
  1812. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1813. ((uint32_t*)(src+0*stride))[0]= a;
  1814. ((uint32_t*)(src+1*stride))[0]= a;
  1815. ((uint32_t*)(src+2*stride))[0]= a;
  1816. ((uint32_t*)(src+3*stride))[0]= a;
  1817. }
  1818. static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){
  1819. ((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101;
  1820. ((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101;
  1821. ((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101;
  1822. ((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101;
  1823. }
  1824. static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1825. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
  1826. + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
  1827. ((uint32_t*)(src+0*stride))[0]=
  1828. ((uint32_t*)(src+1*stride))[0]=
  1829. ((uint32_t*)(src+2*stride))[0]=
  1830. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1831. }
  1832. static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1833. const int dc= ( src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
  1834. ((uint32_t*)(src+0*stride))[0]=
  1835. ((uint32_t*)(src+1*stride))[0]=
  1836. ((uint32_t*)(src+2*stride))[0]=
  1837. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1838. }
  1839. static void pred4x4_top_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1840. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2;
  1841. ((uint32_t*)(src+0*stride))[0]=
  1842. ((uint32_t*)(src+1*stride))[0]=
  1843. ((uint32_t*)(src+2*stride))[0]=
  1844. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1845. }
  1846. static void pred4x4_128_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1847. ((uint32_t*)(src+0*stride))[0]=
  1848. ((uint32_t*)(src+1*stride))[0]=
  1849. ((uint32_t*)(src+2*stride))[0]=
  1850. ((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U;
  1851. }
  1852. #define LOAD_TOP_RIGHT_EDGE\
  1853. const int t4= topright[0];\
  1854. const int t5= topright[1];\
  1855. const int t6= topright[2];\
  1856. const int t7= topright[3];\
  1857. #define LOAD_LEFT_EDGE\
  1858. const int l0= src[-1+0*stride];\
  1859. const int l1= src[-1+1*stride];\
  1860. const int l2= src[-1+2*stride];\
  1861. const int l3= src[-1+3*stride];\
  1862. #define LOAD_TOP_EDGE\
  1863. const int t0= src[ 0-1*stride];\
  1864. const int t1= src[ 1-1*stride];\
  1865. const int t2= src[ 2-1*stride];\
  1866. const int t3= src[ 3-1*stride];\
  1867. static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){
  1868. const int lt= src[-1-1*stride];
  1869. LOAD_TOP_EDGE
  1870. LOAD_LEFT_EDGE
  1871. src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
  1872. src[0+2*stride]=
  1873. src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
  1874. src[0+1*stride]=
  1875. src[1+2*stride]=
  1876. src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
  1877. src[0+0*stride]=
  1878. src[1+1*stride]=
  1879. src[2+2*stride]=
  1880. src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1881. src[1+0*stride]=
  1882. src[2+1*stride]=
  1883. src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1884. src[2+0*stride]=
  1885. src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1886. src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1887. }
  1888. static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){
  1889. LOAD_TOP_EDGE
  1890. LOAD_TOP_RIGHT_EDGE
  1891. // LOAD_LEFT_EDGE
  1892. src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
  1893. src[1+0*stride]=
  1894. src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
  1895. src[2+0*stride]=
  1896. src[1+1*stride]=
  1897. src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
  1898. src[3+0*stride]=
  1899. src[2+1*stride]=
  1900. src[1+2*stride]=
  1901. src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
  1902. src[3+1*stride]=
  1903. src[2+2*stride]=
  1904. src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
  1905. src[3+2*stride]=
  1906. src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
  1907. src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
  1908. }
  1909. static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
  1910. const int lt= src[-1-1*stride];
  1911. LOAD_TOP_EDGE
  1912. LOAD_LEFT_EDGE
  1913. const __attribute__((unused)) int unu= l3;
  1914. src[0+0*stride]=
  1915. src[1+2*stride]=(lt + t0 + 1)>>1;
  1916. src[1+0*stride]=
  1917. src[2+2*stride]=(t0 + t1 + 1)>>1;
  1918. src[2+0*stride]=
  1919. src[3+2*stride]=(t1 + t2 + 1)>>1;
  1920. src[3+0*stride]=(t2 + t3 + 1)>>1;
  1921. src[0+1*stride]=
  1922. src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1923. src[1+1*stride]=
  1924. src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1925. src[2+1*stride]=
  1926. src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1927. src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1928. src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1929. src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1930. }
  1931. static void pred4x4_vertical_left_c(uint8_t *src, uint8_t *topright, int stride){
  1932. LOAD_TOP_EDGE
  1933. LOAD_TOP_RIGHT_EDGE
  1934. const __attribute__((unused)) int unu= t7;
  1935. src[0+0*stride]=(t0 + t1 + 1)>>1;
  1936. src[1+0*stride]=
  1937. src[0+2*stride]=(t1 + t2 + 1)>>1;
  1938. src[2+0*stride]=
  1939. src[1+2*stride]=(t2 + t3 + 1)>>1;
  1940. src[3+0*stride]=
  1941. src[2+2*stride]=(t3 + t4+ 1)>>1;
  1942. src[3+2*stride]=(t4 + t5+ 1)>>1;
  1943. src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1944. src[1+1*stride]=
  1945. src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1946. src[2+1*stride]=
  1947. src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
  1948. src[3+1*stride]=
  1949. src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
  1950. src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
  1951. }
  1952. static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){
  1953. LOAD_LEFT_EDGE
  1954. src[0+0*stride]=(l0 + l1 + 1)>>1;
  1955. src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1956. src[2+0*stride]=
  1957. src[0+1*stride]=(l1 + l2 + 1)>>1;
  1958. src[3+0*stride]=
  1959. src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1960. src[2+1*stride]=
  1961. src[0+2*stride]=(l2 + l3 + 1)>>1;
  1962. src[3+1*stride]=
  1963. src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
  1964. src[3+2*stride]=
  1965. src[1+3*stride]=
  1966. src[0+3*stride]=
  1967. src[2+2*stride]=
  1968. src[2+3*stride]=
  1969. src[3+3*stride]=l3;
  1970. }
  1971. static void pred4x4_horizontal_down_c(uint8_t *src, uint8_t *topright, int stride){
  1972. const int lt= src[-1-1*stride];
  1973. LOAD_TOP_EDGE
  1974. LOAD_LEFT_EDGE
  1975. const __attribute__((unused)) int unu= t3;
  1976. src[0+0*stride]=
  1977. src[2+1*stride]=(lt + l0 + 1)>>1;
  1978. src[1+0*stride]=
  1979. src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1980. src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1981. src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1982. src[0+1*stride]=
  1983. src[2+2*stride]=(l0 + l1 + 1)>>1;
  1984. src[1+1*stride]=
  1985. src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1986. src[0+2*stride]=
  1987. src[2+3*stride]=(l1 + l2+ 1)>>1;
  1988. src[1+2*stride]=
  1989. src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1990. src[0+3*stride]=(l2 + l3 + 1)>>1;
  1991. src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1992. }
  1993. void ff_pred16x16_vertical_c(uint8_t *src, int stride){
  1994. int i;
  1995. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1996. const uint32_t b= ((uint32_t*)(src-stride))[1];
  1997. const uint32_t c= ((uint32_t*)(src-stride))[2];
  1998. const uint32_t d= ((uint32_t*)(src-stride))[3];
  1999. for(i=0; i<16; i++){
  2000. ((uint32_t*)(src+i*stride))[0]= a;
  2001. ((uint32_t*)(src+i*stride))[1]= b;
  2002. ((uint32_t*)(src+i*stride))[2]= c;
  2003. ((uint32_t*)(src+i*stride))[3]= d;
  2004. }
  2005. }
  2006. void ff_pred16x16_horizontal_c(uint8_t *src, int stride){
  2007. int i;
  2008. for(i=0; i<16; i++){
  2009. ((uint32_t*)(src+i*stride))[0]=
  2010. ((uint32_t*)(src+i*stride))[1]=
  2011. ((uint32_t*)(src+i*stride))[2]=
  2012. ((uint32_t*)(src+i*stride))[3]= src[-1+i*stride]*0x01010101;
  2013. }
  2014. }
  2015. void ff_pred16x16_dc_c(uint8_t *src, int stride){
  2016. int i, dc=0;
  2017. for(i=0;i<16; i++){
  2018. dc+= src[-1+i*stride];
  2019. }
  2020. for(i=0;i<16; i++){
  2021. dc+= src[i-stride];
  2022. }
  2023. dc= 0x01010101*((dc + 16)>>5);
  2024. for(i=0; i<16; i++){
  2025. ((uint32_t*)(src+i*stride))[0]=
  2026. ((uint32_t*)(src+i*stride))[1]=
  2027. ((uint32_t*)(src+i*stride))[2]=
  2028. ((uint32_t*)(src+i*stride))[3]= dc;
  2029. }
  2030. }
  2031. static void pred16x16_left_dc_c(uint8_t *src, int stride){
  2032. int i, dc=0;
  2033. for(i=0;i<16; i++){
  2034. dc+= src[-1+i*stride];
  2035. }
  2036. dc= 0x01010101*((dc + 8)>>4);
  2037. for(i=0; i<16; i++){
  2038. ((uint32_t*)(src+i*stride))[0]=
  2039. ((uint32_t*)(src+i*stride))[1]=
  2040. ((uint32_t*)(src+i*stride))[2]=
  2041. ((uint32_t*)(src+i*stride))[3]= dc;
  2042. }
  2043. }
  2044. static void pred16x16_top_dc_c(uint8_t *src, int stride){
  2045. int i, dc=0;
  2046. for(i=0;i<16; i++){
  2047. dc+= src[i-stride];
  2048. }
  2049. dc= 0x01010101*((dc + 8)>>4);
  2050. for(i=0; i<16; i++){
  2051. ((uint32_t*)(src+i*stride))[0]=
  2052. ((uint32_t*)(src+i*stride))[1]=
  2053. ((uint32_t*)(src+i*stride))[2]=
  2054. ((uint32_t*)(src+i*stride))[3]= dc;
  2055. }
  2056. }
  2057. void ff_pred16x16_128_dc_c(uint8_t *src, int stride){
  2058. int i;
  2059. for(i=0; i<16; i++){
  2060. ((uint32_t*)(src+i*stride))[0]=
  2061. ((uint32_t*)(src+i*stride))[1]=
  2062. ((uint32_t*)(src+i*stride))[2]=
  2063. ((uint32_t*)(src+i*stride))[3]= 0x01010101U*128U;
  2064. }
  2065. }
  2066. static inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){
  2067. int i, j, k;
  2068. int a;
  2069. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  2070. const uint8_t * const src0 = src+7-stride;
  2071. const uint8_t *src1 = src+8*stride-1;
  2072. const uint8_t *src2 = src1-2*stride; // == src+6*stride-1;
  2073. int H = src0[1] - src0[-1];
  2074. int V = src1[0] - src2[ 0];
  2075. for(k=2; k<=8; ++k) {
  2076. src1 += stride; src2 -= stride;
  2077. H += k*(src0[k] - src0[-k]);
  2078. V += k*(src1[0] - src2[ 0]);
  2079. }
  2080. if(svq3){
  2081. H = ( 5*(H/4) ) / 16;
  2082. V = ( 5*(V/4) ) / 16;
  2083. /* required for 100% accuracy */
  2084. i = H; H = V; V = i;
  2085. }else{
  2086. H = ( 5*H+32 ) >> 6;
  2087. V = ( 5*V+32 ) >> 6;
  2088. }
  2089. a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
  2090. for(j=16; j>0; --j) {
  2091. int b = a;
  2092. a += V;
  2093. for(i=-16; i<0; i+=4) {
  2094. src[16+i] = cm[ (b ) >> 5 ];
  2095. src[17+i] = cm[ (b+ H) >> 5 ];
  2096. src[18+i] = cm[ (b+2*H) >> 5 ];
  2097. src[19+i] = cm[ (b+3*H) >> 5 ];
  2098. b += 4*H;
  2099. }
  2100. src += stride;
  2101. }
  2102. }
  2103. void ff_pred16x16_plane_c(uint8_t *src, int stride){
  2104. pred16x16_plane_compat_c(src, stride, 0);
  2105. }
  2106. void ff_pred8x8_vertical_c(uint8_t *src, int stride){
  2107. int i;
  2108. const uint32_t a= ((uint32_t*)(src-stride))[0];
  2109. const uint32_t b= ((uint32_t*)(src-stride))[1];
  2110. for(i=0; i<8; i++){
  2111. ((uint32_t*)(src+i*stride))[0]= a;
  2112. ((uint32_t*)(src+i*stride))[1]= b;
  2113. }
  2114. }
  2115. void ff_pred8x8_horizontal_c(uint8_t *src, int stride){
  2116. int i;
  2117. for(i=0; i<8; i++){
  2118. ((uint32_t*)(src+i*stride))[0]=
  2119. ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101;
  2120. }
  2121. }
  2122. void ff_pred8x8_128_dc_c(uint8_t *src, int stride){
  2123. int i;
  2124. for(i=0; i<8; i++){
  2125. ((uint32_t*)(src+i*stride))[0]=
  2126. ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
  2127. }
  2128. }
  2129. static void pred8x8_left_dc_c(uint8_t *src, int stride){
  2130. int i;
  2131. int dc0, dc2;
  2132. dc0=dc2=0;
  2133. for(i=0;i<4; i++){
  2134. dc0+= src[-1+i*stride];
  2135. dc2+= src[-1+(i+4)*stride];
  2136. }
  2137. dc0= 0x01010101*((dc0 + 2)>>2);
  2138. dc2= 0x01010101*((dc2 + 2)>>2);
  2139. for(i=0; i<4; i++){
  2140. ((uint32_t*)(src+i*stride))[0]=
  2141. ((uint32_t*)(src+i*stride))[1]= dc0;
  2142. }
  2143. for(i=4; i<8; i++){
  2144. ((uint32_t*)(src+i*stride))[0]=
  2145. ((uint32_t*)(src+i*stride))[1]= dc2;
  2146. }
  2147. }
  2148. static void pred8x8_top_dc_c(uint8_t *src, int stride){
  2149. int i;
  2150. int dc0, dc1;
  2151. dc0=dc1=0;
  2152. for(i=0;i<4; i++){
  2153. dc0+= src[i-stride];
  2154. dc1+= src[4+i-stride];
  2155. }
  2156. dc0= 0x01010101*((dc0 + 2)>>2);
  2157. dc1= 0x01010101*((dc1 + 2)>>2);
  2158. for(i=0; i<4; i++){
  2159. ((uint32_t*)(src+i*stride))[0]= dc0;
  2160. ((uint32_t*)(src+i*stride))[1]= dc1;
  2161. }
  2162. for(i=4; i<8; i++){
  2163. ((uint32_t*)(src+i*stride))[0]= dc0;
  2164. ((uint32_t*)(src+i*stride))[1]= dc1;
  2165. }
  2166. }
  2167. void ff_pred8x8_dc_c(uint8_t *src, int stride){
  2168. int i;
  2169. int dc0, dc1, dc2, dc3;
  2170. dc0=dc1=dc2=0;
  2171. for(i=0;i<4; i++){
  2172. dc0+= src[-1+i*stride] + src[i-stride];
  2173. dc1+= src[4+i-stride];
  2174. dc2+= src[-1+(i+4)*stride];
  2175. }
  2176. dc3= 0x01010101*((dc1 + dc2 + 4)>>3);
  2177. dc0= 0x01010101*((dc0 + 4)>>3);
  2178. dc1= 0x01010101*((dc1 + 2)>>2);
  2179. dc2= 0x01010101*((dc2 + 2)>>2);
  2180. for(i=0; i<4; i++){
  2181. ((uint32_t*)(src+i*stride))[0]= dc0;
  2182. ((uint32_t*)(src+i*stride))[1]= dc1;
  2183. }
  2184. for(i=4; i<8; i++){
  2185. ((uint32_t*)(src+i*stride))[0]= dc2;
  2186. ((uint32_t*)(src+i*stride))[1]= dc3;
  2187. }
  2188. }
  2189. void ff_pred8x8_plane_c(uint8_t *src, int stride){
  2190. int j, k;
  2191. int a;
  2192. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  2193. const uint8_t * const src0 = src+3-stride;
  2194. const uint8_t *src1 = src+4*stride-1;
  2195. const uint8_t *src2 = src1-2*stride; // == src+2*stride-1;
  2196. int H = src0[1] - src0[-1];
  2197. int V = src1[0] - src2[ 0];
  2198. for(k=2; k<=4; ++k) {
  2199. src1 += stride; src2 -= stride;
  2200. H += k*(src0[k] - src0[-k]);
  2201. V += k*(src1[0] - src2[ 0]);
  2202. }
  2203. H = ( 17*H+16 ) >> 5;
  2204. V = ( 17*V+16 ) >> 5;
  2205. a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
  2206. for(j=8; j>0; --j) {
  2207. int b = a;
  2208. a += V;
  2209. src[0] = cm[ (b ) >> 5 ];
  2210. src[1] = cm[ (b+ H) >> 5 ];
  2211. src[2] = cm[ (b+2*H) >> 5 ];
  2212. src[3] = cm[ (b+3*H) >> 5 ];
  2213. src[4] = cm[ (b+4*H) >> 5 ];
  2214. src[5] = cm[ (b+5*H) >> 5 ];
  2215. src[6] = cm[ (b+6*H) >> 5 ];
  2216. src[7] = cm[ (b+7*H) >> 5 ];
  2217. src += stride;
  2218. }
  2219. }
  2220. #define SRC(x,y) src[(x)+(y)*stride]
  2221. #define PL(y) \
  2222. const int l##y = (SRC(-1,y-1) + 2*SRC(-1,y) + SRC(-1,y+1) + 2) >> 2;
  2223. #define PREDICT_8x8_LOAD_LEFT \
  2224. const int l0 = ((has_topleft ? SRC(-1,-1) : SRC(-1,0)) \
  2225. + 2*SRC(-1,0) + SRC(-1,1) + 2) >> 2; \
  2226. PL(1) PL(2) PL(3) PL(4) PL(5) PL(6) \
  2227. const int l7 attribute_unused = (SRC(-1,6) + 3*SRC(-1,7) + 2) >> 2
  2228. #define PT(x) \
  2229. const int t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  2230. #define PREDICT_8x8_LOAD_TOP \
  2231. const int t0 = ((has_topleft ? SRC(-1,-1) : SRC(0,-1)) \
  2232. + 2*SRC(0,-1) + SRC(1,-1) + 2) >> 2; \
  2233. PT(1) PT(2) PT(3) PT(4) PT(5) PT(6) \
  2234. const int t7 attribute_unused = ((has_topright ? SRC(8,-1) : SRC(7,-1)) \
  2235. + 2*SRC(7,-1) + SRC(6,-1) + 2) >> 2
  2236. #define PTR(x) \
  2237. t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  2238. #define PREDICT_8x8_LOAD_TOPRIGHT \
  2239. int t8, t9, t10, t11, t12, t13, t14, t15; \
  2240. if(has_topright) { \
  2241. PTR(8) PTR(9) PTR(10) PTR(11) PTR(12) PTR(13) PTR(14) \
  2242. t15 = (SRC(14,-1) + 3*SRC(15,-1) + 2) >> 2; \
  2243. } else t8=t9=t10=t11=t12=t13=t14=t15= SRC(7,-1);
  2244. #define PREDICT_8x8_LOAD_TOPLEFT \
  2245. const int lt = (SRC(-1,0) + 2*SRC(-1,-1) + SRC(0,-1) + 2) >> 2
  2246. #define PREDICT_8x8_DC(v) \
  2247. int y; \
  2248. for( y = 0; y < 8; y++ ) { \
  2249. ((uint32_t*)src)[0] = \
  2250. ((uint32_t*)src)[1] = v; \
  2251. src += stride; \
  2252. }
  2253. static void pred8x8l_128_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2254. {
  2255. PREDICT_8x8_DC(0x80808080);
  2256. }
  2257. static void pred8x8l_left_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2258. {
  2259. PREDICT_8x8_LOAD_LEFT;
  2260. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7+4) >> 3) * 0x01010101;
  2261. PREDICT_8x8_DC(dc);
  2262. }
  2263. static void pred8x8l_top_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2264. {
  2265. PREDICT_8x8_LOAD_TOP;
  2266. const uint32_t dc = ((t0+t1+t2+t3+t4+t5+t6+t7+4) >> 3) * 0x01010101;
  2267. PREDICT_8x8_DC(dc);
  2268. }
  2269. static void pred8x8l_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2270. {
  2271. PREDICT_8x8_LOAD_LEFT;
  2272. PREDICT_8x8_LOAD_TOP;
  2273. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7
  2274. +t0+t1+t2+t3+t4+t5+t6+t7+8) >> 4) * 0x01010101;
  2275. PREDICT_8x8_DC(dc);
  2276. }
  2277. static void pred8x8l_horizontal_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2278. {
  2279. PREDICT_8x8_LOAD_LEFT;
  2280. #define ROW(y) ((uint32_t*)(src+y*stride))[0] =\
  2281. ((uint32_t*)(src+y*stride))[1] = 0x01010101 * l##y
  2282. ROW(0); ROW(1); ROW(2); ROW(3); ROW(4); ROW(5); ROW(6); ROW(7);
  2283. #undef ROW
  2284. }
  2285. static void pred8x8l_vertical_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2286. {
  2287. int y;
  2288. PREDICT_8x8_LOAD_TOP;
  2289. src[0] = t0;
  2290. src[1] = t1;
  2291. src[2] = t2;
  2292. src[3] = t3;
  2293. src[4] = t4;
  2294. src[5] = t5;
  2295. src[6] = t6;
  2296. src[7] = t7;
  2297. for( y = 1; y < 8; y++ )
  2298. *(uint64_t*)(src+y*stride) = *(uint64_t*)src;
  2299. }
  2300. static void pred8x8l_down_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2301. {
  2302. PREDICT_8x8_LOAD_TOP;
  2303. PREDICT_8x8_LOAD_TOPRIGHT;
  2304. SRC(0,0)= (t0 + 2*t1 + t2 + 2) >> 2;
  2305. SRC(0,1)=SRC(1,0)= (t1 + 2*t2 + t3 + 2) >> 2;
  2306. SRC(0,2)=SRC(1,1)=SRC(2,0)= (t2 + 2*t3 + t4 + 2) >> 2;
  2307. SRC(0,3)=SRC(1,2)=SRC(2,1)=SRC(3,0)= (t3 + 2*t4 + t5 + 2) >> 2;
  2308. SRC(0,4)=SRC(1,3)=SRC(2,2)=SRC(3,1)=SRC(4,0)= (t4 + 2*t5 + t6 + 2) >> 2;
  2309. SRC(0,5)=SRC(1,4)=SRC(2,3)=SRC(3,2)=SRC(4,1)=SRC(5,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2310. 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;
  2311. 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;
  2312. 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;
  2313. SRC(2,7)=SRC(3,6)=SRC(4,5)=SRC(5,4)=SRC(6,3)=SRC(7,2)= (t9 + 2*t10 + t11 + 2) >> 2;
  2314. SRC(3,7)=SRC(4,6)=SRC(5,5)=SRC(6,4)=SRC(7,3)= (t10 + 2*t11 + t12 + 2) >> 2;
  2315. SRC(4,7)=SRC(5,6)=SRC(6,5)=SRC(7,4)= (t11 + 2*t12 + t13 + 2) >> 2;
  2316. SRC(5,7)=SRC(6,6)=SRC(7,5)= (t12 + 2*t13 + t14 + 2) >> 2;
  2317. SRC(6,7)=SRC(7,6)= (t13 + 2*t14 + t15 + 2) >> 2;
  2318. SRC(7,7)= (t14 + 3*t15 + 2) >> 2;
  2319. }
  2320. static void pred8x8l_down_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2321. {
  2322. PREDICT_8x8_LOAD_TOP;
  2323. PREDICT_8x8_LOAD_LEFT;
  2324. PREDICT_8x8_LOAD_TOPLEFT;
  2325. SRC(0,7)= (l7 + 2*l6 + l5 + 2) >> 2;
  2326. SRC(0,6)=SRC(1,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2327. SRC(0,5)=SRC(1,6)=SRC(2,7)= (l5 + 2*l4 + l3 + 2) >> 2;
  2328. SRC(0,4)=SRC(1,5)=SRC(2,6)=SRC(3,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2329. SRC(0,3)=SRC(1,4)=SRC(2,5)=SRC(3,6)=SRC(4,7)= (l3 + 2*l2 + l1 + 2) >> 2;
  2330. SRC(0,2)=SRC(1,3)=SRC(2,4)=SRC(3,5)=SRC(4,6)=SRC(5,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2331. 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;
  2332. 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;
  2333. 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;
  2334. SRC(2,0)=SRC(3,1)=SRC(4,2)=SRC(5,3)=SRC(6,4)=SRC(7,5)= (t0 + 2*t1 + t2 + 2) >> 2;
  2335. SRC(3,0)=SRC(4,1)=SRC(5,2)=SRC(6,3)=SRC(7,4)= (t1 + 2*t2 + t3 + 2) >> 2;
  2336. SRC(4,0)=SRC(5,1)=SRC(6,2)=SRC(7,3)= (t2 + 2*t3 + t4 + 2) >> 2;
  2337. SRC(5,0)=SRC(6,1)=SRC(7,2)= (t3 + 2*t4 + t5 + 2) >> 2;
  2338. SRC(6,0)=SRC(7,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2339. SRC(7,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2340. }
  2341. static void pred8x8l_vertical_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2342. {
  2343. PREDICT_8x8_LOAD_TOP;
  2344. PREDICT_8x8_LOAD_LEFT;
  2345. PREDICT_8x8_LOAD_TOPLEFT;
  2346. SRC(0,6)= (l5 + 2*l4 + l3 + 2) >> 2;
  2347. SRC(0,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2348. SRC(0,4)=SRC(1,6)= (l3 + 2*l2 + l1 + 2) >> 2;
  2349. SRC(0,5)=SRC(1,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2350. SRC(0,2)=SRC(1,4)=SRC(2,6)= (l1 + 2*l0 + lt + 2) >> 2;
  2351. SRC(0,3)=SRC(1,5)=SRC(2,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2352. SRC(0,1)=SRC(1,3)=SRC(2,5)=SRC(3,7)= (l0 + 2*lt + t0 + 2) >> 2;
  2353. SRC(0,0)=SRC(1,2)=SRC(2,4)=SRC(3,6)= (lt + t0 + 1) >> 1;
  2354. SRC(1,1)=SRC(2,3)=SRC(3,5)=SRC(4,7)= (lt + 2*t0 + t1 + 2) >> 2;
  2355. SRC(1,0)=SRC(2,2)=SRC(3,4)=SRC(4,6)= (t0 + t1 + 1) >> 1;
  2356. SRC(2,1)=SRC(3,3)=SRC(4,5)=SRC(5,7)= (t0 + 2*t1 + t2 + 2) >> 2;
  2357. SRC(2,0)=SRC(3,2)=SRC(4,4)=SRC(5,6)= (t1 + t2 + 1) >> 1;
  2358. SRC(3,1)=SRC(4,3)=SRC(5,5)=SRC(6,7)= (t1 + 2*t2 + t3 + 2) >> 2;
  2359. SRC(3,0)=SRC(4,2)=SRC(5,4)=SRC(6,6)= (t2 + t3 + 1) >> 1;
  2360. SRC(4,1)=SRC(5,3)=SRC(6,5)=SRC(7,7)= (t2 + 2*t3 + t4 + 2) >> 2;
  2361. SRC(4,0)=SRC(5,2)=SRC(6,4)=SRC(7,6)= (t3 + t4 + 1) >> 1;
  2362. SRC(5,1)=SRC(6,3)=SRC(7,5)= (t3 + 2*t4 + t5 + 2) >> 2;
  2363. SRC(5,0)=SRC(6,2)=SRC(7,4)= (t4 + t5 + 1) >> 1;
  2364. SRC(6,1)=SRC(7,3)= (t4 + 2*t5 + t6 + 2) >> 2;
  2365. SRC(6,0)=SRC(7,2)= (t5 + t6 + 1) >> 1;
  2366. SRC(7,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2367. SRC(7,0)= (t6 + t7 + 1) >> 1;
  2368. }
  2369. static void pred8x8l_horizontal_down_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2370. {
  2371. PREDICT_8x8_LOAD_TOP;
  2372. PREDICT_8x8_LOAD_LEFT;
  2373. PREDICT_8x8_LOAD_TOPLEFT;
  2374. SRC(0,7)= (l6 + l7 + 1) >> 1;
  2375. SRC(1,7)= (l5 + 2*l6 + l7 + 2) >> 2;
  2376. SRC(0,6)=SRC(2,7)= (l5 + l6 + 1) >> 1;
  2377. SRC(1,6)=SRC(3,7)= (l4 + 2*l5 + l6 + 2) >> 2;
  2378. SRC(0,5)=SRC(2,6)=SRC(4,7)= (l4 + l5 + 1) >> 1;
  2379. SRC(1,5)=SRC(3,6)=SRC(5,7)= (l3 + 2*l4 + l5 + 2) >> 2;
  2380. SRC(0,4)=SRC(2,5)=SRC(4,6)=SRC(6,7)= (l3 + l4 + 1) >> 1;
  2381. SRC(1,4)=SRC(3,5)=SRC(5,6)=SRC(7,7)= (l2 + 2*l3 + l4 + 2) >> 2;
  2382. SRC(0,3)=SRC(2,4)=SRC(4,5)=SRC(6,6)= (l2 + l3 + 1) >> 1;
  2383. SRC(1,3)=SRC(3,4)=SRC(5,5)=SRC(7,6)= (l1 + 2*l2 + l3 + 2) >> 2;
  2384. SRC(0,2)=SRC(2,3)=SRC(4,4)=SRC(6,5)= (l1 + l2 + 1) >> 1;
  2385. SRC(1,2)=SRC(3,3)=SRC(5,4)=SRC(7,5)= (l0 + 2*l1 + l2 + 2) >> 2;
  2386. SRC(0,1)=SRC(2,2)=SRC(4,3)=SRC(6,4)= (l0 + l1 + 1) >> 1;
  2387. SRC(1,1)=SRC(3,2)=SRC(5,3)=SRC(7,4)= (lt + 2*l0 + l1 + 2) >> 2;
  2388. SRC(0,0)=SRC(2,1)=SRC(4,2)=SRC(6,3)= (lt + l0 + 1) >> 1;
  2389. SRC(1,0)=SRC(3,1)=SRC(5,2)=SRC(7,3)= (l0 + 2*lt + t0 + 2) >> 2;
  2390. SRC(2,0)=SRC(4,1)=SRC(6,2)= (t1 + 2*t0 + lt + 2) >> 2;
  2391. SRC(3,0)=SRC(5,1)=SRC(7,2)= (t2 + 2*t1 + t0 + 2) >> 2;
  2392. SRC(4,0)=SRC(6,1)= (t3 + 2*t2 + t1 + 2) >> 2;
  2393. SRC(5,0)=SRC(7,1)= (t4 + 2*t3 + t2 + 2) >> 2;
  2394. SRC(6,0)= (t5 + 2*t4 + t3 + 2) >> 2;
  2395. SRC(7,0)= (t6 + 2*t5 + t4 + 2) >> 2;
  2396. }
  2397. static void pred8x8l_vertical_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2398. {
  2399. PREDICT_8x8_LOAD_TOP;
  2400. PREDICT_8x8_LOAD_TOPRIGHT;
  2401. SRC(0,0)= (t0 + t1 + 1) >> 1;
  2402. SRC(0,1)= (t0 + 2*t1 + t2 + 2) >> 2;
  2403. SRC(0,2)=SRC(1,0)= (t1 + t2 + 1) >> 1;
  2404. SRC(0,3)=SRC(1,1)= (t1 + 2*t2 + t3 + 2) >> 2;
  2405. SRC(0,4)=SRC(1,2)=SRC(2,0)= (t2 + t3 + 1) >> 1;
  2406. SRC(0,5)=SRC(1,3)=SRC(2,1)= (t2 + 2*t3 + t4 + 2) >> 2;
  2407. SRC(0,6)=SRC(1,4)=SRC(2,2)=SRC(3,0)= (t3 + t4 + 1) >> 1;
  2408. SRC(0,7)=SRC(1,5)=SRC(2,3)=SRC(3,1)= (t3 + 2*t4 + t5 + 2) >> 2;
  2409. SRC(1,6)=SRC(2,4)=SRC(3,2)=SRC(4,0)= (t4 + t5 + 1) >> 1;
  2410. SRC(1,7)=SRC(2,5)=SRC(3,3)=SRC(4,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2411. SRC(2,6)=SRC(3,4)=SRC(4,2)=SRC(5,0)= (t5 + t6 + 1) >> 1;
  2412. SRC(2,7)=SRC(3,5)=SRC(4,3)=SRC(5,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2413. SRC(3,6)=SRC(4,4)=SRC(5,2)=SRC(6,0)= (t6 + t7 + 1) >> 1;
  2414. SRC(3,7)=SRC(4,5)=SRC(5,3)=SRC(6,1)= (t6 + 2*t7 + t8 + 2) >> 2;
  2415. SRC(4,6)=SRC(5,4)=SRC(6,2)=SRC(7,0)= (t7 + t8 + 1) >> 1;
  2416. SRC(4,7)=SRC(5,5)=SRC(6,3)=SRC(7,1)= (t7 + 2*t8 + t9 + 2) >> 2;
  2417. SRC(5,6)=SRC(6,4)=SRC(7,2)= (t8 + t9 + 1) >> 1;
  2418. SRC(5,7)=SRC(6,5)=SRC(7,3)= (t8 + 2*t9 + t10 + 2) >> 2;
  2419. SRC(6,6)=SRC(7,4)= (t9 + t10 + 1) >> 1;
  2420. SRC(6,7)=SRC(7,5)= (t9 + 2*t10 + t11 + 2) >> 2;
  2421. SRC(7,6)= (t10 + t11 + 1) >> 1;
  2422. SRC(7,7)= (t10 + 2*t11 + t12 + 2) >> 2;
  2423. }
  2424. static void pred8x8l_horizontal_up_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2425. {
  2426. PREDICT_8x8_LOAD_LEFT;
  2427. SRC(0,0)= (l0 + l1 + 1) >> 1;
  2428. SRC(1,0)= (l0 + 2*l1 + l2 + 2) >> 2;
  2429. SRC(0,1)=SRC(2,0)= (l1 + l2 + 1) >> 1;
  2430. SRC(1,1)=SRC(3,0)= (l1 + 2*l2 + l3 + 2) >> 2;
  2431. SRC(0,2)=SRC(2,1)=SRC(4,0)= (l2 + l3 + 1) >> 1;
  2432. SRC(1,2)=SRC(3,1)=SRC(5,0)= (l2 + 2*l3 + l4 + 2) >> 2;
  2433. SRC(0,3)=SRC(2,2)=SRC(4,1)=SRC(6,0)= (l3 + l4 + 1) >> 1;
  2434. SRC(1,3)=SRC(3,2)=SRC(5,1)=SRC(7,0)= (l3 + 2*l4 + l5 + 2) >> 2;
  2435. SRC(0,4)=SRC(2,3)=SRC(4,2)=SRC(6,1)= (l4 + l5 + 1) >> 1;
  2436. SRC(1,4)=SRC(3,3)=SRC(5,2)=SRC(7,1)= (l4 + 2*l5 + l6 + 2) >> 2;
  2437. SRC(0,5)=SRC(2,4)=SRC(4,3)=SRC(6,2)= (l5 + l6 + 1) >> 1;
  2438. SRC(1,5)=SRC(3,4)=SRC(5,3)=SRC(7,2)= (l5 + 2*l6 + l7 + 2) >> 2;
  2439. SRC(0,6)=SRC(2,5)=SRC(4,4)=SRC(6,3)= (l6 + l7 + 1) >> 1;
  2440. SRC(1,6)=SRC(3,5)=SRC(5,4)=SRC(7,3)= (l6 + 3*l7 + 2) >> 2;
  2441. SRC(0,7)=SRC(1,7)=SRC(2,6)=SRC(2,7)=SRC(3,6)=
  2442. SRC(3,7)=SRC(4,5)=SRC(4,6)=SRC(4,7)=SRC(5,5)=
  2443. SRC(5,6)=SRC(5,7)=SRC(6,4)=SRC(6,5)=SRC(6,6)=
  2444. SRC(6,7)=SRC(7,4)=SRC(7,5)=SRC(7,6)=SRC(7,7)= l7;
  2445. }
  2446. #undef PREDICT_8x8_LOAD_LEFT
  2447. #undef PREDICT_8x8_LOAD_TOP
  2448. #undef PREDICT_8x8_LOAD_TOPLEFT
  2449. #undef PREDICT_8x8_LOAD_TOPRIGHT
  2450. #undef PREDICT_8x8_DC
  2451. #undef PTR
  2452. #undef PT
  2453. #undef PL
  2454. #undef SRC
  2455. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  2456. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2457. int src_x_offset, int src_y_offset,
  2458. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  2459. MpegEncContext * const s = &h->s;
  2460. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  2461. int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  2462. const int luma_xy= (mx&3) + ((my&3)<<2);
  2463. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
  2464. uint8_t * src_cb, * src_cr;
  2465. int extra_width= h->emu_edge_width;
  2466. int extra_height= h->emu_edge_height;
  2467. int emu=0;
  2468. const int full_mx= mx>>2;
  2469. const int full_my= my>>2;
  2470. const int pic_width = 16*s->mb_width;
  2471. const int pic_height = 16*s->mb_height >> MB_MBAFF;
  2472. if(!pic->data[0]) //FIXME this is unacceptable, some senseable error concealment must be done for missing reference frames
  2473. return;
  2474. if(mx&7) extra_width -= 3;
  2475. if(my&7) extra_height -= 3;
  2476. if( full_mx < 0-extra_width
  2477. || full_my < 0-extra_height
  2478. || full_mx + 16/*FIXME*/ > pic_width + extra_width
  2479. || full_my + 16/*FIXME*/ > pic_height + extra_height){
  2480. 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);
  2481. src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
  2482. emu=1;
  2483. }
  2484. qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
  2485. if(!square){
  2486. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
  2487. }
  2488. if(s->flags&CODEC_FLAG_GRAY) return;
  2489. if(MB_MBAFF){
  2490. // chroma offset when predicting from a field of opposite parity
  2491. my += 2 * ((s->mb_y & 1) - (h->ref_cache[list][scan8[n]] & 1));
  2492. emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
  2493. }
  2494. src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2495. src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2496. if(emu){
  2497. 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);
  2498. src_cb= s->edge_emu_buffer;
  2499. }
  2500. chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2501. if(emu){
  2502. 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);
  2503. src_cr= s->edge_emu_buffer;
  2504. }
  2505. chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2506. }
  2507. static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
  2508. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2509. int x_offset, int y_offset,
  2510. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2511. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2512. int list0, int list1){
  2513. MpegEncContext * const s = &h->s;
  2514. qpel_mc_func *qpix_op= qpix_put;
  2515. h264_chroma_mc_func chroma_op= chroma_put;
  2516. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2517. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2518. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2519. x_offset += 8*s->mb_x;
  2520. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2521. if(list0){
  2522. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  2523. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  2524. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2525. qpix_op, chroma_op);
  2526. qpix_op= qpix_avg;
  2527. chroma_op= chroma_avg;
  2528. }
  2529. if(list1){
  2530. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  2531. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  2532. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2533. qpix_op, chroma_op);
  2534. }
  2535. }
  2536. static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
  2537. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2538. int x_offset, int y_offset,
  2539. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2540. h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
  2541. h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
  2542. int list0, int list1){
  2543. MpegEncContext * const s = &h->s;
  2544. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2545. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2546. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2547. x_offset += 8*s->mb_x;
  2548. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2549. if(list0 && list1){
  2550. /* don't optimize for luma-only case, since B-frames usually
  2551. * use implicit weights => chroma too. */
  2552. uint8_t *tmp_cb = s->obmc_scratchpad;
  2553. uint8_t *tmp_cr = s->obmc_scratchpad + 8;
  2554. uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
  2555. int refn0 = h->ref_cache[0][ scan8[n] ];
  2556. int refn1 = h->ref_cache[1][ scan8[n] ];
  2557. mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
  2558. dest_y, dest_cb, dest_cr,
  2559. x_offset, y_offset, qpix_put, chroma_put);
  2560. mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
  2561. tmp_y, tmp_cb, tmp_cr,
  2562. x_offset, y_offset, qpix_put, chroma_put);
  2563. if(h->use_weight == 2){
  2564. int weight0 = h->implicit_weight[refn0][refn1];
  2565. int weight1 = 64 - weight0;
  2566. luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
  2567. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2568. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2569. }else{
  2570. luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
  2571. h->luma_weight[0][refn0], h->luma_weight[1][refn1],
  2572. h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
  2573. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2574. h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
  2575. h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
  2576. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2577. h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
  2578. h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
  2579. }
  2580. }else{
  2581. int list = list1 ? 1 : 0;
  2582. int refn = h->ref_cache[list][ scan8[n] ];
  2583. Picture *ref= &h->ref_list[list][refn];
  2584. mc_dir_part(h, ref, n, square, chroma_height, delta, list,
  2585. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2586. qpix_put, chroma_put);
  2587. luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
  2588. h->luma_weight[list][refn], h->luma_offset[list][refn]);
  2589. if(h->use_weight_chroma){
  2590. chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2591. h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
  2592. chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2593. h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
  2594. }
  2595. }
  2596. }
  2597. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  2598. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2599. int x_offset, int y_offset,
  2600. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2601. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2602. h264_weight_func *weight_op, h264_biweight_func *weight_avg,
  2603. int list0, int list1){
  2604. if((h->use_weight==2 && list0 && list1
  2605. && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
  2606. || h->use_weight==1)
  2607. mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2608. x_offset, y_offset, qpix_put, chroma_put,
  2609. weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
  2610. else
  2611. mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2612. x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
  2613. }
  2614. static inline void prefetch_motion(H264Context *h, int list){
  2615. /* fetch pixels for estimated mv 4 macroblocks ahead
  2616. * optimized for 64byte cache lines */
  2617. MpegEncContext * const s = &h->s;
  2618. const int refn = h->ref_cache[list][scan8[0]];
  2619. if(refn >= 0){
  2620. const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
  2621. const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
  2622. uint8_t **src= h->ref_list[list][refn].data;
  2623. int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
  2624. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  2625. off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
  2626. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  2627. }
  2628. }
  2629. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2630. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  2631. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
  2632. h264_weight_func *weight_op, h264_biweight_func *weight_avg){
  2633. MpegEncContext * const s = &h->s;
  2634. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2635. const int mb_type= s->current_picture.mb_type[mb_xy];
  2636. assert(IS_INTER(mb_type));
  2637. prefetch_motion(h, 0);
  2638. if(IS_16X16(mb_type)){
  2639. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  2640. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  2641. &weight_op[0], &weight_avg[0],
  2642. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2643. }else if(IS_16X8(mb_type)){
  2644. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  2645. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2646. &weight_op[1], &weight_avg[1],
  2647. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2648. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  2649. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2650. &weight_op[1], &weight_avg[1],
  2651. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2652. }else if(IS_8X16(mb_type)){
  2653. mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
  2654. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2655. &weight_op[2], &weight_avg[2],
  2656. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2657. mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
  2658. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2659. &weight_op[2], &weight_avg[2],
  2660. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2661. }else{
  2662. int i;
  2663. assert(IS_8X8(mb_type));
  2664. for(i=0; i<4; i++){
  2665. const int sub_mb_type= h->sub_mb_type[i];
  2666. const int n= 4*i;
  2667. int x_offset= (i&1)<<2;
  2668. int y_offset= (i&2)<<1;
  2669. if(IS_SUB_8X8(sub_mb_type)){
  2670. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2671. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2672. &weight_op[3], &weight_avg[3],
  2673. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2674. }else if(IS_SUB_8X4(sub_mb_type)){
  2675. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2676. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2677. &weight_op[4], &weight_avg[4],
  2678. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2679. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  2680. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2681. &weight_op[4], &weight_avg[4],
  2682. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2683. }else if(IS_SUB_4X8(sub_mb_type)){
  2684. mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2685. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2686. &weight_op[5], &weight_avg[5],
  2687. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2688. mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
  2689. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2690. &weight_op[5], &weight_avg[5],
  2691. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2692. }else{
  2693. int j;
  2694. assert(IS_SUB_4X4(sub_mb_type));
  2695. for(j=0; j<4; j++){
  2696. int sub_x_offset= x_offset + 2*(j&1);
  2697. int sub_y_offset= y_offset + (j&2);
  2698. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  2699. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2700. &weight_op[6], &weight_avg[6],
  2701. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2702. }
  2703. }
  2704. }
  2705. }
  2706. prefetch_motion(h, 1);
  2707. }
  2708. static void decode_init_vlc(){
  2709. static int done = 0;
  2710. if (!done) {
  2711. int i;
  2712. done = 1;
  2713. init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
  2714. &chroma_dc_coeff_token_len [0], 1, 1,
  2715. &chroma_dc_coeff_token_bits[0], 1, 1, 1);
  2716. for(i=0; i<4; i++){
  2717. init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
  2718. &coeff_token_len [i][0], 1, 1,
  2719. &coeff_token_bits[i][0], 1, 1, 1);
  2720. }
  2721. for(i=0; i<3; i++){
  2722. init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
  2723. &chroma_dc_total_zeros_len [i][0], 1, 1,
  2724. &chroma_dc_total_zeros_bits[i][0], 1, 1, 1);
  2725. }
  2726. for(i=0; i<15; i++){
  2727. init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
  2728. &total_zeros_len [i][0], 1, 1,
  2729. &total_zeros_bits[i][0], 1, 1, 1);
  2730. }
  2731. for(i=0; i<6; i++){
  2732. init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
  2733. &run_len [i][0], 1, 1,
  2734. &run_bits[i][0], 1, 1, 1);
  2735. }
  2736. init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
  2737. &run_len [6][0], 1, 1,
  2738. &run_bits[6][0], 1, 1, 1);
  2739. }
  2740. }
  2741. /**
  2742. * Sets the intra prediction function pointers.
  2743. */
  2744. static void init_pred_ptrs(H264Context *h){
  2745. // MpegEncContext * const s = &h->s;
  2746. h->pred4x4[VERT_PRED ]= pred4x4_vertical_c;
  2747. h->pred4x4[HOR_PRED ]= pred4x4_horizontal_c;
  2748. h->pred4x4[DC_PRED ]= pred4x4_dc_c;
  2749. h->pred4x4[DIAG_DOWN_LEFT_PRED ]= pred4x4_down_left_c;
  2750. h->pred4x4[DIAG_DOWN_RIGHT_PRED]= pred4x4_down_right_c;
  2751. h->pred4x4[VERT_RIGHT_PRED ]= pred4x4_vertical_right_c;
  2752. h->pred4x4[HOR_DOWN_PRED ]= pred4x4_horizontal_down_c;
  2753. h->pred4x4[VERT_LEFT_PRED ]= pred4x4_vertical_left_c;
  2754. h->pred4x4[HOR_UP_PRED ]= pred4x4_horizontal_up_c;
  2755. h->pred4x4[LEFT_DC_PRED ]= pred4x4_left_dc_c;
  2756. h->pred4x4[TOP_DC_PRED ]= pred4x4_top_dc_c;
  2757. h->pred4x4[DC_128_PRED ]= pred4x4_128_dc_c;
  2758. h->pred8x8l[VERT_PRED ]= pred8x8l_vertical_c;
  2759. h->pred8x8l[HOR_PRED ]= pred8x8l_horizontal_c;
  2760. h->pred8x8l[DC_PRED ]= pred8x8l_dc_c;
  2761. h->pred8x8l[DIAG_DOWN_LEFT_PRED ]= pred8x8l_down_left_c;
  2762. h->pred8x8l[DIAG_DOWN_RIGHT_PRED]= pred8x8l_down_right_c;
  2763. h->pred8x8l[VERT_RIGHT_PRED ]= pred8x8l_vertical_right_c;
  2764. h->pred8x8l[HOR_DOWN_PRED ]= pred8x8l_horizontal_down_c;
  2765. h->pred8x8l[VERT_LEFT_PRED ]= pred8x8l_vertical_left_c;
  2766. h->pred8x8l[HOR_UP_PRED ]= pred8x8l_horizontal_up_c;
  2767. h->pred8x8l[LEFT_DC_PRED ]= pred8x8l_left_dc_c;
  2768. h->pred8x8l[TOP_DC_PRED ]= pred8x8l_top_dc_c;
  2769. h->pred8x8l[DC_128_PRED ]= pred8x8l_128_dc_c;
  2770. h->pred8x8[DC_PRED8x8 ]= ff_pred8x8_dc_c;
  2771. h->pred8x8[VERT_PRED8x8 ]= ff_pred8x8_vertical_c;
  2772. h->pred8x8[HOR_PRED8x8 ]= ff_pred8x8_horizontal_c;
  2773. h->pred8x8[PLANE_PRED8x8 ]= ff_pred8x8_plane_c;
  2774. h->pred8x8[LEFT_DC_PRED8x8]= pred8x8_left_dc_c;
  2775. h->pred8x8[TOP_DC_PRED8x8 ]= pred8x8_top_dc_c;
  2776. h->pred8x8[DC_128_PRED8x8 ]= ff_pred8x8_128_dc_c;
  2777. h->pred16x16[DC_PRED8x8 ]= ff_pred16x16_dc_c;
  2778. h->pred16x16[VERT_PRED8x8 ]= ff_pred16x16_vertical_c;
  2779. h->pred16x16[HOR_PRED8x8 ]= ff_pred16x16_horizontal_c;
  2780. h->pred16x16[PLANE_PRED8x8 ]= ff_pred16x16_plane_c;
  2781. h->pred16x16[LEFT_DC_PRED8x8]= pred16x16_left_dc_c;
  2782. h->pred16x16[TOP_DC_PRED8x8 ]= pred16x16_top_dc_c;
  2783. h->pred16x16[DC_128_PRED8x8 ]= ff_pred16x16_128_dc_c;
  2784. }
  2785. static void free_tables(H264Context *h){
  2786. av_freep(&h->intra4x4_pred_mode);
  2787. av_freep(&h->chroma_pred_mode_table);
  2788. av_freep(&h->cbp_table);
  2789. av_freep(&h->mvd_table[0]);
  2790. av_freep(&h->mvd_table[1]);
  2791. av_freep(&h->direct_table);
  2792. av_freep(&h->non_zero_count);
  2793. av_freep(&h->slice_table_base);
  2794. av_freep(&h->top_borders[1]);
  2795. av_freep(&h->top_borders[0]);
  2796. h->slice_table= NULL;
  2797. av_freep(&h->mb2b_xy);
  2798. av_freep(&h->mb2b8_xy);
  2799. av_freep(&h->s.obmc_scratchpad);
  2800. }
  2801. static void init_dequant8_coeff_table(H264Context *h){
  2802. int i,q,x;
  2803. const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
  2804. h->dequant8_coeff[0] = h->dequant8_buffer[0];
  2805. h->dequant8_coeff[1] = h->dequant8_buffer[1];
  2806. for(i=0; i<2; i++ ){
  2807. if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
  2808. h->dequant8_coeff[1] = h->dequant8_buffer[0];
  2809. break;
  2810. }
  2811. for(q=0; q<52; q++){
  2812. int shift = ff_div6[q];
  2813. int idx = ff_rem6[q];
  2814. for(x=0; x<64; x++)
  2815. h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
  2816. ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
  2817. h->pps.scaling_matrix8[i][x]) << shift;
  2818. }
  2819. }
  2820. }
  2821. static void init_dequant4_coeff_table(H264Context *h){
  2822. int i,j,q,x;
  2823. const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
  2824. for(i=0; i<6; i++ ){
  2825. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  2826. for(j=0; j<i; j++){
  2827. if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
  2828. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  2829. break;
  2830. }
  2831. }
  2832. if(j<i)
  2833. continue;
  2834. for(q=0; q<52; q++){
  2835. int shift = ff_div6[q] + 2;
  2836. int idx = ff_rem6[q];
  2837. for(x=0; x<16; x++)
  2838. h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
  2839. ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
  2840. h->pps.scaling_matrix4[i][x]) << shift;
  2841. }
  2842. }
  2843. }
  2844. static void init_dequant_tables(H264Context *h){
  2845. int i,x;
  2846. init_dequant4_coeff_table(h);
  2847. if(h->pps.transform_8x8_mode)
  2848. init_dequant8_coeff_table(h);
  2849. if(h->sps.transform_bypass){
  2850. for(i=0; i<6; i++)
  2851. for(x=0; x<16; x++)
  2852. h->dequant4_coeff[i][0][x] = 1<<6;
  2853. if(h->pps.transform_8x8_mode)
  2854. for(i=0; i<2; i++)
  2855. for(x=0; x<64; x++)
  2856. h->dequant8_coeff[i][0][x] = 1<<6;
  2857. }
  2858. }
  2859. /**
  2860. * allocates tables.
  2861. * needs width/height
  2862. */
  2863. static int alloc_tables(H264Context *h){
  2864. MpegEncContext * const s = &h->s;
  2865. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  2866. int x,y;
  2867. CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
  2868. CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
  2869. CHECKED_ALLOCZ(h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(uint8_t))
  2870. CHECKED_ALLOCZ(h->top_borders[0] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2871. CHECKED_ALLOCZ(h->top_borders[1] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2872. CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
  2873. if( h->pps.cabac ) {
  2874. CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
  2875. CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
  2876. CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t));
  2877. CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t));
  2878. }
  2879. memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(uint8_t));
  2880. h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
  2881. CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint32_t));
  2882. CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint32_t));
  2883. for(y=0; y<s->mb_height; y++){
  2884. for(x=0; x<s->mb_width; x++){
  2885. const int mb_xy= x + y*s->mb_stride;
  2886. const int b_xy = 4*x + 4*y*h->b_stride;
  2887. const int b8_xy= 2*x + 2*y*h->b8_stride;
  2888. h->mb2b_xy [mb_xy]= b_xy;
  2889. h->mb2b8_xy[mb_xy]= b8_xy;
  2890. }
  2891. }
  2892. s->obmc_scratchpad = NULL;
  2893. if(!h->dequant4_coeff[0])
  2894. init_dequant_tables(h);
  2895. return 0;
  2896. fail:
  2897. free_tables(h);
  2898. return -1;
  2899. }
  2900. static void common_init(H264Context *h){
  2901. MpegEncContext * const s = &h->s;
  2902. s->width = s->avctx->width;
  2903. s->height = s->avctx->height;
  2904. s->codec_id= s->avctx->codec->id;
  2905. init_pred_ptrs(h);
  2906. h->dequant_coeff_pps= -1;
  2907. s->unrestricted_mv=1;
  2908. s->decode=1; //FIXME
  2909. memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  2910. memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  2911. }
  2912. static int decode_init(AVCodecContext *avctx){
  2913. H264Context *h= avctx->priv_data;
  2914. MpegEncContext * const s = &h->s;
  2915. MPV_decode_defaults(s);
  2916. s->avctx = avctx;
  2917. common_init(h);
  2918. s->out_format = FMT_H264;
  2919. s->workaround_bugs= avctx->workaround_bugs;
  2920. // set defaults
  2921. // s->decode_mb= ff_h263_decode_mb;
  2922. s->low_delay= 1;
  2923. avctx->pix_fmt= PIX_FMT_YUV420P;
  2924. decode_init_vlc();
  2925. if(avctx->extradata_size > 0 && avctx->extradata &&
  2926. *(char *)avctx->extradata == 1){
  2927. h->is_avc = 1;
  2928. h->got_avcC = 0;
  2929. } else {
  2930. h->is_avc = 0;
  2931. }
  2932. return 0;
  2933. }
  2934. static int frame_start(H264Context *h){
  2935. MpegEncContext * const s = &h->s;
  2936. int i;
  2937. if(MPV_frame_start(s, s->avctx) < 0)
  2938. return -1;
  2939. ff_er_frame_start(s);
  2940. assert(s->linesize && s->uvlinesize);
  2941. for(i=0; i<16; i++){
  2942. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  2943. h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  2944. }
  2945. for(i=0; i<4; i++){
  2946. h->block_offset[16+i]=
  2947. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2948. h->block_offset[24+16+i]=
  2949. h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2950. }
  2951. /* can't be in alloc_tables because linesize isn't known there.
  2952. * FIXME: redo bipred weight to not require extra buffer? */
  2953. if(!s->obmc_scratchpad)
  2954. s->obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  2955. /* some macroblocks will be accessed before they're available */
  2956. if(FRAME_MBAFF)
  2957. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(uint8_t));
  2958. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  2959. return 0;
  2960. }
  2961. static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  2962. MpegEncContext * const s = &h->s;
  2963. int i;
  2964. src_y -= linesize;
  2965. src_cb -= uvlinesize;
  2966. src_cr -= uvlinesize;
  2967. // There are two lines saved, the line above the the top macroblock of a pair,
  2968. // and the line above the bottom macroblock
  2969. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  2970. for(i=1; i<17; i++){
  2971. h->left_border[i]= src_y[15+i* linesize];
  2972. }
  2973. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
  2974. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
  2975. if(!(s->flags&CODEC_FLAG_GRAY)){
  2976. h->left_border[17 ]= h->top_borders[0][s->mb_x][16+7];
  2977. h->left_border[17+9]= h->top_borders[0][s->mb_x][24+7];
  2978. for(i=1; i<9; i++){
  2979. h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
  2980. h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
  2981. }
  2982. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
  2983. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
  2984. }
  2985. }
  2986. 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){
  2987. MpegEncContext * const s = &h->s;
  2988. int temp8, i;
  2989. uint64_t temp64;
  2990. int deblock_left = (s->mb_x > 0);
  2991. int deblock_top = (s->mb_y > 0);
  2992. src_y -= linesize + 1;
  2993. src_cb -= uvlinesize + 1;
  2994. src_cr -= uvlinesize + 1;
  2995. #define XCHG(a,b,t,xchg)\
  2996. t= a;\
  2997. if(xchg)\
  2998. a= b;\
  2999. b= t;
  3000. if(deblock_left){
  3001. for(i = !deblock_top; i<17; i++){
  3002. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  3003. }
  3004. }
  3005. if(deblock_top){
  3006. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  3007. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  3008. if(s->mb_x+1 < s->mb_width){
  3009. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  3010. }
  3011. }
  3012. if(!(s->flags&CODEC_FLAG_GRAY)){
  3013. if(deblock_left){
  3014. for(i = !deblock_top; i<9; i++){
  3015. XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
  3016. XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
  3017. }
  3018. }
  3019. if(deblock_top){
  3020. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  3021. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  3022. }
  3023. }
  3024. }
  3025. static inline void backup_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  3026. MpegEncContext * const s = &h->s;
  3027. int i;
  3028. src_y -= 2 * linesize;
  3029. src_cb -= 2 * uvlinesize;
  3030. src_cr -= 2 * uvlinesize;
  3031. // There are two lines saved, the line above the the top macroblock of a pair,
  3032. // and the line above the bottom macroblock
  3033. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  3034. h->left_border[1]= h->top_borders[1][s->mb_x][15];
  3035. for(i=2; i<34; i++){
  3036. h->left_border[i]= src_y[15+i* linesize];
  3037. }
  3038. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 32*linesize);
  3039. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+32*linesize);
  3040. *(uint64_t*)(h->top_borders[1][s->mb_x]+0)= *(uint64_t*)(src_y + 33*linesize);
  3041. *(uint64_t*)(h->top_borders[1][s->mb_x]+8)= *(uint64_t*)(src_y +8+33*linesize);
  3042. if(!(s->flags&CODEC_FLAG_GRAY)){
  3043. h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7];
  3044. h->left_border[34+ 1]= h->top_borders[1][s->mb_x][16+7];
  3045. h->left_border[34+18 ]= h->top_borders[0][s->mb_x][24+7];
  3046. h->left_border[34+18+1]= h->top_borders[1][s->mb_x][24+7];
  3047. for(i=2; i<18; i++){
  3048. h->left_border[i+34 ]= src_cb[7+i*uvlinesize];
  3049. h->left_border[i+34+18]= src_cr[7+i*uvlinesize];
  3050. }
  3051. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+16*uvlinesize);
  3052. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+16*uvlinesize);
  3053. *(uint64_t*)(h->top_borders[1][s->mb_x]+16)= *(uint64_t*)(src_cb+17*uvlinesize);
  3054. *(uint64_t*)(h->top_borders[1][s->mb_x]+24)= *(uint64_t*)(src_cr+17*uvlinesize);
  3055. }
  3056. }
  3057. 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){
  3058. MpegEncContext * const s = &h->s;
  3059. int temp8, i;
  3060. uint64_t temp64;
  3061. int deblock_left = (s->mb_x > 0);
  3062. int deblock_top = (s->mb_y > 1);
  3063. 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);
  3064. src_y -= 2 * linesize + 1;
  3065. src_cb -= 2 * uvlinesize + 1;
  3066. src_cr -= 2 * uvlinesize + 1;
  3067. #define XCHG(a,b,t,xchg)\
  3068. t= a;\
  3069. if(xchg)\
  3070. a= b;\
  3071. b= t;
  3072. if(deblock_left){
  3073. for(i = (!deblock_top)<<1; i<34; i++){
  3074. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  3075. }
  3076. }
  3077. if(deblock_top){
  3078. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  3079. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  3080. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+0), *(uint64_t*)(src_y +1 +linesize), temp64, xchg);
  3081. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+8), *(uint64_t*)(src_y +9 +linesize), temp64, 1);
  3082. if(s->mb_x+1 < s->mb_width){
  3083. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  3084. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x+1]), *(uint64_t*)(src_y +17 +linesize), temp64, 1);
  3085. }
  3086. }
  3087. if(!(s->flags&CODEC_FLAG_GRAY)){
  3088. if(deblock_left){
  3089. for(i = (!deblock_top) << 1; i<18; i++){
  3090. XCHG(h->left_border[i+34 ], src_cb[i*uvlinesize], temp8, xchg);
  3091. XCHG(h->left_border[i+34+18], src_cr[i*uvlinesize], temp8, xchg);
  3092. }
  3093. }
  3094. if(deblock_top){
  3095. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  3096. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  3097. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+16), *(uint64_t*)(src_cb+1 +uvlinesize), temp64, 1);
  3098. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+24), *(uint64_t*)(src_cr+1 +uvlinesize), temp64, 1);
  3099. }
  3100. }
  3101. }
  3102. static void hl_decode_mb(H264Context *h){
  3103. MpegEncContext * const s = &h->s;
  3104. const int mb_x= s->mb_x;
  3105. const int mb_y= s->mb_y;
  3106. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3107. const int mb_type= s->current_picture.mb_type[mb_xy];
  3108. uint8_t *dest_y, *dest_cb, *dest_cr;
  3109. int linesize, uvlinesize /*dct_offset*/;
  3110. int i;
  3111. int *block_offset = &h->block_offset[0];
  3112. const unsigned int bottom = mb_y & 1;
  3113. const int transform_bypass = (s->qscale == 0 && h->sps.transform_bypass);
  3114. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  3115. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  3116. if(!s->decode)
  3117. return;
  3118. dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3119. dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3120. dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3121. s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
  3122. s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
  3123. if (MB_FIELD) {
  3124. linesize = h->mb_linesize = s->linesize * 2;
  3125. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  3126. block_offset = &h->block_offset[24];
  3127. if(mb_y&1){ //FIXME move out of this func?
  3128. dest_y -= s->linesize*15;
  3129. dest_cb-= s->uvlinesize*7;
  3130. dest_cr-= s->uvlinesize*7;
  3131. }
  3132. if(FRAME_MBAFF) {
  3133. int list;
  3134. for(list=0; list<2; list++){
  3135. if(!USES_LIST(mb_type, list))
  3136. continue;
  3137. if(IS_16X16(mb_type)){
  3138. int8_t *ref = &h->ref_cache[list][scan8[0]];
  3139. fill_rectangle(ref, 4, 4, 8, 16+*ref^(s->mb_y&1), 1);
  3140. }else{
  3141. for(i=0; i<16; i+=4){
  3142. //FIXME can refs be smaller than 8x8 when !direct_8x8_inference ?
  3143. int ref = h->ref_cache[list][scan8[i]];
  3144. if(ref >= 0)
  3145. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, 16+ref^(s->mb_y&1), 1);
  3146. }
  3147. }
  3148. }
  3149. }
  3150. } else {
  3151. linesize = h->mb_linesize = s->linesize;
  3152. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  3153. // dct_offset = s->linesize * 16;
  3154. }
  3155. if(transform_bypass){
  3156. idct_dc_add =
  3157. idct_add = IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  3158. }else if(IS_8x8DCT(mb_type)){
  3159. idct_dc_add = s->dsp.h264_idct8_dc_add;
  3160. idct_add = s->dsp.h264_idct8_add;
  3161. }else{
  3162. idct_dc_add = s->dsp.h264_idct_dc_add;
  3163. idct_add = s->dsp.h264_idct_add;
  3164. }
  3165. if(FRAME_MBAFF && h->deblocking_filter && IS_INTRA(mb_type)
  3166. && (!bottom || !IS_INTRA(s->current_picture.mb_type[mb_xy-s->mb_stride]))){
  3167. int mbt_y = mb_y&~1;
  3168. uint8_t *top_y = s->current_picture.data[0] + (mbt_y * 16* s->linesize ) + mb_x * 16;
  3169. uint8_t *top_cb = s->current_picture.data[1] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  3170. uint8_t *top_cr = s->current_picture.data[2] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  3171. xchg_pair_border(h, top_y, top_cb, top_cr, s->linesize, s->uvlinesize, 1);
  3172. }
  3173. if (IS_INTRA_PCM(mb_type)) {
  3174. unsigned int x, y;
  3175. // The pixels are stored in h->mb array in the same order as levels,
  3176. // copy them in output in the correct order.
  3177. for(i=0; i<16; i++) {
  3178. for (y=0; y<4; y++) {
  3179. for (x=0; x<4; x++) {
  3180. *(dest_y + block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x];
  3181. }
  3182. }
  3183. }
  3184. for(i=16; i<16+4; i++) {
  3185. for (y=0; y<4; y++) {
  3186. for (x=0; x<4; x++) {
  3187. *(dest_cb + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  3188. }
  3189. }
  3190. }
  3191. for(i=20; i<20+4; i++) {
  3192. for (y=0; y<4; y++) {
  3193. for (x=0; x<4; x++) {
  3194. *(dest_cr + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  3195. }
  3196. }
  3197. }
  3198. } else {
  3199. if(IS_INTRA(mb_type)){
  3200. if(h->deblocking_filter && !FRAME_MBAFF)
  3201. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1);
  3202. if(!(s->flags&CODEC_FLAG_GRAY)){
  3203. h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  3204. h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  3205. }
  3206. if(IS_INTRA4x4(mb_type)){
  3207. if(!s->encoding){
  3208. if(IS_8x8DCT(mb_type)){
  3209. for(i=0; i<16; i+=4){
  3210. uint8_t * const ptr= dest_y + block_offset[i];
  3211. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  3212. const int nnz = h->non_zero_count_cache[ scan8[i] ];
  3213. h->pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  3214. (h->topright_samples_available<<(i+1))&0x8000, linesize);
  3215. if(nnz){
  3216. if(nnz == 1 && h->mb[i*16])
  3217. idct_dc_add(ptr, h->mb + i*16, linesize);
  3218. else
  3219. idct_add(ptr, h->mb + i*16, linesize);
  3220. }
  3221. }
  3222. }else
  3223. for(i=0; i<16; i++){
  3224. uint8_t * const ptr= dest_y + block_offset[i];
  3225. uint8_t *topright;
  3226. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  3227. int nnz, tr;
  3228. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  3229. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  3230. assert(mb_y || linesize <= block_offset[i]);
  3231. if(!topright_avail){
  3232. tr= ptr[3 - linesize]*0x01010101;
  3233. topright= (uint8_t*) &tr;
  3234. }else
  3235. topright= ptr + 4 - linesize;
  3236. }else
  3237. topright= NULL;
  3238. h->pred4x4[ dir ](ptr, topright, linesize);
  3239. nnz = h->non_zero_count_cache[ scan8[i] ];
  3240. if(nnz){
  3241. if(s->codec_id == CODEC_ID_H264){
  3242. if(nnz == 1 && h->mb[i*16])
  3243. idct_dc_add(ptr, h->mb + i*16, linesize);
  3244. else
  3245. idct_add(ptr, h->mb + i*16, linesize);
  3246. }else
  3247. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  3248. }
  3249. }
  3250. }
  3251. }else{
  3252. h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  3253. if(s->codec_id == CODEC_ID_H264){
  3254. if(!transform_bypass)
  3255. h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[IS_INTRA(mb_type) ? 0:3][s->qscale][0]);
  3256. }else
  3257. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  3258. }
  3259. if(h->deblocking_filter && !FRAME_MBAFF)
  3260. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
  3261. }else if(s->codec_id == CODEC_ID_H264){
  3262. hl_motion(h, dest_y, dest_cb, dest_cr,
  3263. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  3264. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  3265. s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
  3266. }
  3267. if(!IS_INTRA4x4(mb_type)){
  3268. if(s->codec_id == CODEC_ID_H264){
  3269. if(IS_INTRA16x16(mb_type)){
  3270. for(i=0; i<16; i++){
  3271. if(h->non_zero_count_cache[ scan8[i] ])
  3272. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3273. else if(h->mb[i*16])
  3274. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3275. }
  3276. }else{
  3277. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  3278. for(i=0; i<16; i+=di){
  3279. int nnz = h->non_zero_count_cache[ scan8[i] ];
  3280. if(nnz){
  3281. if(nnz==1 && h->mb[i*16])
  3282. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3283. else
  3284. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3285. }
  3286. }
  3287. }
  3288. }else{
  3289. for(i=0; i<16; i++){
  3290. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  3291. uint8_t * const ptr= dest_y + block_offset[i];
  3292. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  3293. }
  3294. }
  3295. }
  3296. }
  3297. if(!(s->flags&CODEC_FLAG_GRAY)){
  3298. uint8_t *dest[2] = {dest_cb, dest_cr};
  3299. if(transform_bypass){
  3300. idct_add = idct_dc_add = s->dsp.add_pixels4;
  3301. }else{
  3302. idct_add = s->dsp.h264_idct_add;
  3303. idct_dc_add = s->dsp.h264_idct_dc_add;
  3304. 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]);
  3305. 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]);
  3306. }
  3307. if(s->codec_id == CODEC_ID_H264){
  3308. for(i=16; i<16+8; i++){
  3309. if(h->non_zero_count_cache[ scan8[i] ])
  3310. idct_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3311. else if(h->mb[i*16])
  3312. idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3313. }
  3314. }else{
  3315. for(i=16; i<16+8; i++){
  3316. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  3317. uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
  3318. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  3319. }
  3320. }
  3321. }
  3322. }
  3323. }
  3324. if(h->deblocking_filter) {
  3325. if (FRAME_MBAFF) {
  3326. //FIXME try deblocking one mb at a time?
  3327. // the reduction in load/storing mvs and such might outweigh the extra backup/xchg_border
  3328. const int mb_y = s->mb_y - 1;
  3329. uint8_t *pair_dest_y, *pair_dest_cb, *pair_dest_cr;
  3330. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3331. const int mb_type_top = s->current_picture.mb_type[mb_xy];
  3332. const int mb_type_bottom= s->current_picture.mb_type[mb_xy+s->mb_stride];
  3333. if (!bottom) return;
  3334. pair_dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3335. pair_dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3336. pair_dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3337. if(IS_INTRA(mb_type_top | mb_type_bottom))
  3338. xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0);
  3339. backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize);
  3340. // deblock a pair
  3341. // top
  3342. s->mb_y--;
  3343. 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);
  3344. fill_caches(h, mb_type_top, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3345. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy]);
  3346. filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize);
  3347. // bottom
  3348. s->mb_y++;
  3349. tprintf("call mbaff filter_mb\n");
  3350. fill_caches(h, mb_type_bottom, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3351. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy+s->mb_stride]);
  3352. filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3353. } else {
  3354. tprintf("call filter_mb\n");
  3355. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3356. fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3357. filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3358. }
  3359. }
  3360. }
  3361. /**
  3362. * fills the default_ref_list.
  3363. */
  3364. static int fill_default_ref_list(H264Context *h){
  3365. MpegEncContext * const s = &h->s;
  3366. int i;
  3367. int smallest_poc_greater_than_current = -1;
  3368. Picture sorted_short_ref[32];
  3369. if(h->slice_type==B_TYPE){
  3370. int out_i;
  3371. int limit= INT_MIN;
  3372. /* sort frame according to poc in B slice */
  3373. for(out_i=0; out_i<h->short_ref_count; out_i++){
  3374. int best_i=INT_MIN;
  3375. int best_poc=INT_MAX;
  3376. for(i=0; i<h->short_ref_count; i++){
  3377. const int poc= h->short_ref[i]->poc;
  3378. if(poc > limit && poc < best_poc){
  3379. best_poc= poc;
  3380. best_i= i;
  3381. }
  3382. }
  3383. assert(best_i != INT_MIN);
  3384. limit= best_poc;
  3385. sorted_short_ref[out_i]= *h->short_ref[best_i];
  3386. 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);
  3387. if (-1 == smallest_poc_greater_than_current) {
  3388. if (h->short_ref[best_i]->poc >= s->current_picture_ptr->poc) {
  3389. smallest_poc_greater_than_current = out_i;
  3390. }
  3391. }
  3392. }
  3393. }
  3394. if(s->picture_structure == PICT_FRAME){
  3395. if(h->slice_type==B_TYPE){
  3396. int list;
  3397. tprintf("current poc: %d, smallest_poc_greater_than_current: %d\n", s->current_picture_ptr->poc, smallest_poc_greater_than_current);
  3398. // find the largest poc
  3399. for(list=0; list<2; list++){
  3400. int index = 0;
  3401. int j= -99;
  3402. int step= list ? -1 : 1;
  3403. for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++, j+=step) {
  3404. while(j<0 || j>= h->short_ref_count){
  3405. if(j != -99 && step == (list ? -1 : 1))
  3406. return -1;
  3407. step = -step;
  3408. j= smallest_poc_greater_than_current + (step>>1);
  3409. }
  3410. if(sorted_short_ref[j].reference != 3) continue;
  3411. h->default_ref_list[list][index ]= sorted_short_ref[j];
  3412. h->default_ref_list[list][index++].pic_id= sorted_short_ref[j].frame_num;
  3413. }
  3414. for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){
  3415. if(h->long_ref[i] == NULL) continue;
  3416. if(h->long_ref[i]->reference != 3) continue;
  3417. h->default_ref_list[ list ][index ]= *h->long_ref[i];
  3418. h->default_ref_list[ list ][index++].pic_id= i;;
  3419. }
  3420. if(list && (smallest_poc_greater_than_current<=0 || smallest_poc_greater_than_current>=h->short_ref_count) && (1 < index)){
  3421. // swap the two first elements of L1 when
  3422. // L0 and L1 are identical
  3423. Picture temp= h->default_ref_list[1][0];
  3424. h->default_ref_list[1][0] = h->default_ref_list[1][1];
  3425. h->default_ref_list[1][1] = temp;
  3426. }
  3427. if(index < h->ref_count[ list ])
  3428. memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
  3429. }
  3430. }else{
  3431. int index=0;
  3432. for(i=0; i<h->short_ref_count; i++){
  3433. if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
  3434. h->default_ref_list[0][index ]= *h->short_ref[i];
  3435. h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
  3436. }
  3437. for(i = 0; i < 16; i++){
  3438. if(h->long_ref[i] == NULL) continue;
  3439. if(h->long_ref[i]->reference != 3) continue;
  3440. h->default_ref_list[0][index ]= *h->long_ref[i];
  3441. h->default_ref_list[0][index++].pic_id= i;;
  3442. }
  3443. if(index < h->ref_count[0])
  3444. memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
  3445. }
  3446. }else{ //FIELD
  3447. if(h->slice_type==B_TYPE){
  3448. }else{
  3449. //FIXME second field balh
  3450. }
  3451. }
  3452. #ifdef TRACE
  3453. for (i=0; i<h->ref_count[0]; i++) {
  3454. 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]);
  3455. }
  3456. if(h->slice_type==B_TYPE){
  3457. for (i=0; i<h->ref_count[1]; i++) {
  3458. 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]);
  3459. }
  3460. }
  3461. #endif
  3462. return 0;
  3463. }
  3464. static void print_short_term(H264Context *h);
  3465. static void print_long_term(H264Context *h);
  3466. static int decode_ref_pic_list_reordering(H264Context *h){
  3467. MpegEncContext * const s = &h->s;
  3468. int list, index;
  3469. print_short_term(h);
  3470. print_long_term(h);
  3471. if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move before func
  3472. for(list=0; list<2; list++){
  3473. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  3474. if(get_bits1(&s->gb)){
  3475. int pred= h->curr_pic_num;
  3476. for(index=0; ; index++){
  3477. unsigned int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
  3478. unsigned int pic_id;
  3479. int i;
  3480. Picture *ref = NULL;
  3481. if(reordering_of_pic_nums_idc==3)
  3482. break;
  3483. if(index >= h->ref_count[list]){
  3484. av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
  3485. return -1;
  3486. }
  3487. if(reordering_of_pic_nums_idc<3){
  3488. if(reordering_of_pic_nums_idc<2){
  3489. const unsigned int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  3490. if(abs_diff_pic_num >= h->max_pic_num){
  3491. av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  3492. return -1;
  3493. }
  3494. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  3495. else pred+= abs_diff_pic_num;
  3496. pred &= h->max_pic_num - 1;
  3497. for(i= h->short_ref_count-1; i>=0; i--){
  3498. ref = h->short_ref[i];
  3499. assert(ref->reference == 3);
  3500. assert(!ref->long_ref);
  3501. if(ref->data[0] != NULL && ref->frame_num == pred && ref->long_ref == 0) // ignore non existing pictures by testing data[0] pointer
  3502. break;
  3503. }
  3504. if(i>=0)
  3505. ref->pic_id= ref->frame_num;
  3506. }else{
  3507. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  3508. if(pic_id>31){
  3509. av_log(h->s.avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
  3510. return -1;
  3511. }
  3512. ref = h->long_ref[pic_id];
  3513. if(ref){
  3514. ref->pic_id= pic_id;
  3515. assert(ref->reference == 3);
  3516. assert(ref->long_ref);
  3517. i=0;
  3518. }else{
  3519. i=-1;
  3520. }
  3521. }
  3522. if (i < 0) {
  3523. av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  3524. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  3525. } else {
  3526. for(i=index; i+1<h->ref_count[list]; i++){
  3527. if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
  3528. break;
  3529. }
  3530. for(; i > index; i--){
  3531. h->ref_list[list][i]= h->ref_list[list][i-1];
  3532. }
  3533. h->ref_list[list][index]= *ref;
  3534. }
  3535. }else{
  3536. av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  3537. return -1;
  3538. }
  3539. }
  3540. }
  3541. if(h->slice_type!=B_TYPE) break;
  3542. }
  3543. for(list=0; list<2; list++){
  3544. for(index= 0; index < h->ref_count[list]; index++){
  3545. if(!h->ref_list[list][index].data[0])
  3546. h->ref_list[list][index]= s->current_picture;
  3547. }
  3548. if(h->slice_type!=B_TYPE) break;
  3549. }
  3550. if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)
  3551. direct_dist_scale_factor(h);
  3552. direct_ref_list_init(h);
  3553. return 0;
  3554. }
  3555. static void fill_mbaff_ref_list(H264Context *h){
  3556. int list, i, j;
  3557. for(list=0; list<2; list++){
  3558. for(i=0; i<h->ref_count[list]; i++){
  3559. Picture *frame = &h->ref_list[list][i];
  3560. Picture *field = &h->ref_list[list][16+2*i];
  3561. field[0] = *frame;
  3562. for(j=0; j<3; j++)
  3563. field[0].linesize[j] <<= 1;
  3564. field[1] = field[0];
  3565. for(j=0; j<3; j++)
  3566. field[1].data[j] += frame->linesize[j];
  3567. h->luma_weight[list][16+2*i] = h->luma_weight[list][16+2*i+1] = h->luma_weight[list][i];
  3568. h->luma_offset[list][16+2*i] = h->luma_offset[list][16+2*i+1] = h->luma_offset[list][i];
  3569. for(j=0; j<2; j++){
  3570. h->chroma_weight[list][16+2*i][j] = h->chroma_weight[list][16+2*i+1][j] = h->chroma_weight[list][i][j];
  3571. h->chroma_offset[list][16+2*i][j] = h->chroma_offset[list][16+2*i+1][j] = h->chroma_offset[list][i][j];
  3572. }
  3573. }
  3574. }
  3575. for(j=0; j<h->ref_count[1]; j++){
  3576. for(i=0; i<h->ref_count[0]; i++)
  3577. h->implicit_weight[j][16+2*i] = h->implicit_weight[j][16+2*i+1] = h->implicit_weight[j][i];
  3578. memcpy(h->implicit_weight[16+2*j], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3579. memcpy(h->implicit_weight[16+2*j+1], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3580. }
  3581. }
  3582. static int pred_weight_table(H264Context *h){
  3583. MpegEncContext * const s = &h->s;
  3584. int list, i;
  3585. int luma_def, chroma_def;
  3586. h->use_weight= 0;
  3587. h->use_weight_chroma= 0;
  3588. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  3589. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  3590. luma_def = 1<<h->luma_log2_weight_denom;
  3591. chroma_def = 1<<h->chroma_log2_weight_denom;
  3592. for(list=0; list<2; list++){
  3593. for(i=0; i<h->ref_count[list]; i++){
  3594. int luma_weight_flag, chroma_weight_flag;
  3595. luma_weight_flag= get_bits1(&s->gb);
  3596. if(luma_weight_flag){
  3597. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  3598. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  3599. if( h->luma_weight[list][i] != luma_def
  3600. || h->luma_offset[list][i] != 0)
  3601. h->use_weight= 1;
  3602. }else{
  3603. h->luma_weight[list][i]= luma_def;
  3604. h->luma_offset[list][i]= 0;
  3605. }
  3606. chroma_weight_flag= get_bits1(&s->gb);
  3607. if(chroma_weight_flag){
  3608. int j;
  3609. for(j=0; j<2; j++){
  3610. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  3611. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  3612. if( h->chroma_weight[list][i][j] != chroma_def
  3613. || h->chroma_offset[list][i][j] != 0)
  3614. h->use_weight_chroma= 1;
  3615. }
  3616. }else{
  3617. int j;
  3618. for(j=0; j<2; j++){
  3619. h->chroma_weight[list][i][j]= chroma_def;
  3620. h->chroma_offset[list][i][j]= 0;
  3621. }
  3622. }
  3623. }
  3624. if(h->slice_type != B_TYPE) break;
  3625. }
  3626. h->use_weight= h->use_weight || h->use_weight_chroma;
  3627. return 0;
  3628. }
  3629. static void implicit_weight_table(H264Context *h){
  3630. MpegEncContext * const s = &h->s;
  3631. int ref0, ref1;
  3632. int cur_poc = s->current_picture_ptr->poc;
  3633. if( h->ref_count[0] == 1 && h->ref_count[1] == 1
  3634. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  3635. h->use_weight= 0;
  3636. h->use_weight_chroma= 0;
  3637. return;
  3638. }
  3639. h->use_weight= 2;
  3640. h->use_weight_chroma= 2;
  3641. h->luma_log2_weight_denom= 5;
  3642. h->chroma_log2_weight_denom= 5;
  3643. for(ref0=0; ref0 < h->ref_count[0]; ref0++){
  3644. int poc0 = h->ref_list[0][ref0].poc;
  3645. for(ref1=0; ref1 < h->ref_count[1]; ref1++){
  3646. int poc1 = h->ref_list[1][ref1].poc;
  3647. int td = clip(poc1 - poc0, -128, 127);
  3648. if(td){
  3649. int tb = clip(cur_poc - poc0, -128, 127);
  3650. int tx = (16384 + (FFABS(td) >> 1)) / td;
  3651. int dist_scale_factor = clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
  3652. if(dist_scale_factor < -64 || dist_scale_factor > 128)
  3653. h->implicit_weight[ref0][ref1] = 32;
  3654. else
  3655. h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
  3656. }else
  3657. h->implicit_weight[ref0][ref1] = 32;
  3658. }
  3659. }
  3660. }
  3661. static inline void unreference_pic(H264Context *h, Picture *pic){
  3662. int i;
  3663. pic->reference=0;
  3664. if(pic == h->delayed_output_pic)
  3665. pic->reference=1;
  3666. else{
  3667. for(i = 0; h->delayed_pic[i]; i++)
  3668. if(pic == h->delayed_pic[i]){
  3669. pic->reference=1;
  3670. break;
  3671. }
  3672. }
  3673. }
  3674. /**
  3675. * instantaneous decoder refresh.
  3676. */
  3677. static void idr(H264Context *h){
  3678. int i;
  3679. for(i=0; i<16; i++){
  3680. if (h->long_ref[i] != NULL) {
  3681. unreference_pic(h, h->long_ref[i]);
  3682. h->long_ref[i]= NULL;
  3683. }
  3684. }
  3685. h->long_ref_count=0;
  3686. for(i=0; i<h->short_ref_count; i++){
  3687. unreference_pic(h, h->short_ref[i]);
  3688. h->short_ref[i]= NULL;
  3689. }
  3690. h->short_ref_count=0;
  3691. }
  3692. /* forget old pics after a seek */
  3693. static void flush_dpb(AVCodecContext *avctx){
  3694. H264Context *h= avctx->priv_data;
  3695. int i;
  3696. for(i=0; i<16; i++) {
  3697. if(h->delayed_pic[i])
  3698. h->delayed_pic[i]->reference= 0;
  3699. h->delayed_pic[i]= NULL;
  3700. }
  3701. if(h->delayed_output_pic)
  3702. h->delayed_output_pic->reference= 0;
  3703. h->delayed_output_pic= NULL;
  3704. idr(h);
  3705. if(h->s.current_picture_ptr)
  3706. h->s.current_picture_ptr->reference= 0;
  3707. }
  3708. /**
  3709. *
  3710. * @return the removed picture or NULL if an error occurs
  3711. */
  3712. static Picture * remove_short(H264Context *h, int frame_num){
  3713. MpegEncContext * const s = &h->s;
  3714. int i;
  3715. if(s->avctx->debug&FF_DEBUG_MMCO)
  3716. av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  3717. for(i=0; i<h->short_ref_count; i++){
  3718. Picture *pic= h->short_ref[i];
  3719. if(s->avctx->debug&FF_DEBUG_MMCO)
  3720. av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  3721. if(pic->frame_num == frame_num){
  3722. h->short_ref[i]= NULL;
  3723. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
  3724. h->short_ref_count--;
  3725. return pic;
  3726. }
  3727. }
  3728. return NULL;
  3729. }
  3730. /**
  3731. *
  3732. * @return the removed picture or NULL if an error occurs
  3733. */
  3734. static Picture * remove_long(H264Context *h, int i){
  3735. Picture *pic;
  3736. pic= h->long_ref[i];
  3737. h->long_ref[i]= NULL;
  3738. if(pic) h->long_ref_count--;
  3739. return pic;
  3740. }
  3741. /**
  3742. * print short term list
  3743. */
  3744. static void print_short_term(H264Context *h) {
  3745. uint32_t i;
  3746. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3747. av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
  3748. for(i=0; i<h->short_ref_count; i++){
  3749. Picture *pic= h->short_ref[i];
  3750. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3751. }
  3752. }
  3753. }
  3754. /**
  3755. * print long term list
  3756. */
  3757. static void print_long_term(H264Context *h) {
  3758. uint32_t i;
  3759. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3760. av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
  3761. for(i = 0; i < 16; i++){
  3762. Picture *pic= h->long_ref[i];
  3763. if (pic) {
  3764. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3765. }
  3766. }
  3767. }
  3768. }
  3769. /**
  3770. * Executes the reference picture marking (memory management control operations).
  3771. */
  3772. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  3773. MpegEncContext * const s = &h->s;
  3774. int i, j;
  3775. int current_is_long=0;
  3776. Picture *pic;
  3777. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  3778. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  3779. for(i=0; i<mmco_count; i++){
  3780. if(s->avctx->debug&FF_DEBUG_MMCO)
  3781. 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);
  3782. switch(mmco[i].opcode){
  3783. case MMCO_SHORT2UNUSED:
  3784. pic= remove_short(h, mmco[i].short_frame_num);
  3785. if(pic)
  3786. unreference_pic(h, pic);
  3787. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3788. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_short() failure\n");
  3789. break;
  3790. case MMCO_SHORT2LONG:
  3791. pic= remove_long(h, mmco[i].long_index);
  3792. if(pic) unreference_pic(h, pic);
  3793. h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
  3794. if (h->long_ref[ mmco[i].long_index ]){
  3795. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3796. h->long_ref_count++;
  3797. }
  3798. break;
  3799. case MMCO_LONG2UNUSED:
  3800. pic= remove_long(h, mmco[i].long_index);
  3801. if(pic)
  3802. unreference_pic(h, pic);
  3803. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3804. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_long() failure\n");
  3805. break;
  3806. case MMCO_LONG:
  3807. pic= remove_long(h, mmco[i].long_index);
  3808. if(pic) unreference_pic(h, pic);
  3809. h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
  3810. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3811. h->long_ref_count++;
  3812. current_is_long=1;
  3813. break;
  3814. case MMCO_SET_MAX_LONG:
  3815. assert(mmco[i].long_index <= 16);
  3816. // just remove the long term which index is greater than new max
  3817. for(j = mmco[i].long_index; j<16; j++){
  3818. pic = remove_long(h, j);
  3819. if (pic) unreference_pic(h, pic);
  3820. }
  3821. break;
  3822. case MMCO_RESET:
  3823. while(h->short_ref_count){
  3824. pic= remove_short(h, h->short_ref[0]->frame_num);
  3825. if(pic) unreference_pic(h, pic);
  3826. }
  3827. for(j = 0; j < 16; j++) {
  3828. pic= remove_long(h, j);
  3829. if(pic) unreference_pic(h, pic);
  3830. }
  3831. break;
  3832. default: assert(0);
  3833. }
  3834. }
  3835. if(!current_is_long){
  3836. pic= remove_short(h, s->current_picture_ptr->frame_num);
  3837. if(pic){
  3838. unreference_pic(h, pic);
  3839. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  3840. }
  3841. if(h->short_ref_count)
  3842. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  3843. h->short_ref[0]= s->current_picture_ptr;
  3844. h->short_ref[0]->long_ref=0;
  3845. h->short_ref_count++;
  3846. }
  3847. print_short_term(h);
  3848. print_long_term(h);
  3849. return 0;
  3850. }
  3851. static int decode_ref_pic_marking(H264Context *h){
  3852. MpegEncContext * const s = &h->s;
  3853. int i;
  3854. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  3855. s->broken_link= get_bits1(&s->gb) -1;
  3856. h->mmco[0].long_index= get_bits1(&s->gb) - 1; // current_long_term_idx
  3857. if(h->mmco[0].long_index == -1)
  3858. h->mmco_index= 0;
  3859. else{
  3860. h->mmco[0].opcode= MMCO_LONG;
  3861. h->mmco_index= 1;
  3862. }
  3863. }else{
  3864. if(get_bits1(&s->gb)){ // adaptive_ref_pic_marking_mode_flag
  3865. for(i= 0; i<MAX_MMCO_COUNT; i++) {
  3866. MMCOOpcode opcode= get_ue_golomb(&s->gb);;
  3867. h->mmco[i].opcode= opcode;
  3868. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  3869. 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
  3870. /* if(h->mmco[i].short_frame_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_frame_num ] == NULL){
  3871. av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
  3872. return -1;
  3873. }*/
  3874. }
  3875. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  3876. unsigned int long_index= get_ue_golomb(&s->gb);
  3877. if(/*h->mmco[i].long_index >= h->long_ref_count || h->long_ref[ h->mmco[i].long_index ] == NULL*/ long_index >= 16){
  3878. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  3879. return -1;
  3880. }
  3881. h->mmco[i].long_index= long_index;
  3882. }
  3883. if(opcode > (unsigned)MMCO_LONG){
  3884. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  3885. return -1;
  3886. }
  3887. if(opcode == MMCO_END)
  3888. break;
  3889. }
  3890. h->mmco_index= i;
  3891. }else{
  3892. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  3893. if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){ //FIXME fields
  3894. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  3895. h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  3896. h->mmco_index= 1;
  3897. }else
  3898. h->mmco_index= 0;
  3899. }
  3900. }
  3901. return 0;
  3902. }
  3903. static int init_poc(H264Context *h){
  3904. MpegEncContext * const s = &h->s;
  3905. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  3906. int field_poc[2];
  3907. if(h->nal_unit_type == NAL_IDR_SLICE){
  3908. h->frame_num_offset= 0;
  3909. }else{
  3910. if(h->frame_num < h->prev_frame_num)
  3911. h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
  3912. else
  3913. h->frame_num_offset= h->prev_frame_num_offset;
  3914. }
  3915. if(h->sps.poc_type==0){
  3916. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  3917. if(h->nal_unit_type == NAL_IDR_SLICE){
  3918. h->prev_poc_msb=
  3919. h->prev_poc_lsb= 0;
  3920. }
  3921. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  3922. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  3923. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  3924. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  3925. else
  3926. h->poc_msb = h->prev_poc_msb;
  3927. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  3928. field_poc[0] =
  3929. field_poc[1] = h->poc_msb + h->poc_lsb;
  3930. if(s->picture_structure == PICT_FRAME)
  3931. field_poc[1] += h->delta_poc_bottom;
  3932. }else if(h->sps.poc_type==1){
  3933. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  3934. int i;
  3935. if(h->sps.poc_cycle_length != 0)
  3936. abs_frame_num = h->frame_num_offset + h->frame_num;
  3937. else
  3938. abs_frame_num = 0;
  3939. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  3940. abs_frame_num--;
  3941. expected_delta_per_poc_cycle = 0;
  3942. for(i=0; i < h->sps.poc_cycle_length; i++)
  3943. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  3944. if(abs_frame_num > 0){
  3945. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  3946. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  3947. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  3948. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  3949. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  3950. } else
  3951. expectedpoc = 0;
  3952. if(h->nal_ref_idc == 0)
  3953. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  3954. field_poc[0] = expectedpoc + h->delta_poc[0];
  3955. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  3956. if(s->picture_structure == PICT_FRAME)
  3957. field_poc[1] += h->delta_poc[1];
  3958. }else{
  3959. int poc;
  3960. if(h->nal_unit_type == NAL_IDR_SLICE){
  3961. poc= 0;
  3962. }else{
  3963. if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
  3964. else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
  3965. }
  3966. field_poc[0]= poc;
  3967. field_poc[1]= poc;
  3968. }
  3969. if(s->picture_structure != PICT_BOTTOM_FIELD)
  3970. s->current_picture_ptr->field_poc[0]= field_poc[0];
  3971. if(s->picture_structure != PICT_TOP_FIELD)
  3972. s->current_picture_ptr->field_poc[1]= field_poc[1];
  3973. if(s->picture_structure == PICT_FRAME) // FIXME field pix?
  3974. s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]);
  3975. return 0;
  3976. }
  3977. /**
  3978. * decodes a slice header.
  3979. * this will allso call MPV_common_init() and frame_start() as needed
  3980. */
  3981. static int decode_slice_header(H264Context *h){
  3982. MpegEncContext * const s = &h->s;
  3983. unsigned int first_mb_in_slice;
  3984. unsigned int pps_id;
  3985. int num_ref_idx_active_override_flag;
  3986. static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
  3987. unsigned int slice_type, tmp;
  3988. int default_ref_list_done = 0;
  3989. s->current_picture.reference= h->nal_ref_idc != 0;
  3990. s->dropable= h->nal_ref_idc == 0;
  3991. first_mb_in_slice= get_ue_golomb(&s->gb);
  3992. slice_type= get_ue_golomb(&s->gb);
  3993. if(slice_type > 9){
  3994. 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);
  3995. return -1;
  3996. }
  3997. if(slice_type > 4){
  3998. slice_type -= 5;
  3999. h->slice_type_fixed=1;
  4000. }else
  4001. h->slice_type_fixed=0;
  4002. slice_type= slice_type_map[ slice_type ];
  4003. if (slice_type == I_TYPE
  4004. || (h->slice_num != 0 && slice_type == h->slice_type) ) {
  4005. default_ref_list_done = 1;
  4006. }
  4007. h->slice_type= slice_type;
  4008. s->pict_type= h->slice_type; // to make a few old func happy, it's wrong though
  4009. pps_id= get_ue_golomb(&s->gb);
  4010. if(pps_id>=MAX_PPS_COUNT){
  4011. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  4012. return -1;
  4013. }
  4014. h->pps= h->pps_buffer[pps_id];
  4015. if(h->pps.slice_group_count == 0){
  4016. av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referenced\n");
  4017. return -1;
  4018. }
  4019. h->sps= h->sps_buffer[ h->pps.sps_id ];
  4020. if(h->sps.log2_max_frame_num == 0){
  4021. av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referenced\n");
  4022. return -1;
  4023. }
  4024. if(h->dequant_coeff_pps != pps_id){
  4025. h->dequant_coeff_pps = pps_id;
  4026. init_dequant_tables(h);
  4027. }
  4028. s->mb_width= h->sps.mb_width;
  4029. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  4030. h->b_stride= s->mb_width*4;
  4031. h->b8_stride= s->mb_width*2;
  4032. s->width = 16*s->mb_width - 2*(h->sps.crop_left + h->sps.crop_right );
  4033. if(h->sps.frame_mbs_only_flag)
  4034. s->height= 16*s->mb_height - 2*(h->sps.crop_top + h->sps.crop_bottom);
  4035. else
  4036. s->height= 16*s->mb_height - 4*(h->sps.crop_top + h->sps.crop_bottom); //FIXME recheck
  4037. if (s->context_initialized
  4038. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  4039. free_tables(h);
  4040. MPV_common_end(s);
  4041. }
  4042. if (!s->context_initialized) {
  4043. if (MPV_common_init(s) < 0)
  4044. return -1;
  4045. if(s->dsp.h264_idct_add == ff_h264_idct_add_c){ //FIXME little ugly
  4046. memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
  4047. memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
  4048. }else{
  4049. int i;
  4050. for(i=0; i<16; i++){
  4051. #define T(x) (x>>2) | ((x<<2) & 0xF)
  4052. h->zigzag_scan[i] = T(zigzag_scan[i]);
  4053. h-> field_scan[i] = T( field_scan[i]);
  4054. #undef T
  4055. }
  4056. }
  4057. if(s->dsp.h264_idct8_add == ff_h264_idct8_add_c){
  4058. memcpy(h->zigzag_scan8x8, zigzag_scan8x8, 64*sizeof(uint8_t));
  4059. memcpy(h->zigzag_scan8x8_cavlc, zigzag_scan8x8_cavlc, 64*sizeof(uint8_t));
  4060. memcpy(h->field_scan8x8, field_scan8x8, 64*sizeof(uint8_t));
  4061. memcpy(h->field_scan8x8_cavlc, field_scan8x8_cavlc, 64*sizeof(uint8_t));
  4062. }else{
  4063. int i;
  4064. for(i=0; i<64; i++){
  4065. #define T(x) (x>>3) | ((x&7)<<3)
  4066. h->zigzag_scan8x8[i] = T(zigzag_scan8x8[i]);
  4067. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  4068. h->field_scan8x8[i] = T(field_scan8x8[i]);
  4069. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  4070. #undef T
  4071. }
  4072. }
  4073. if(h->sps.transform_bypass){ //FIXME same ugly
  4074. h->zigzag_scan_q0 = zigzag_scan;
  4075. h->zigzag_scan8x8_q0 = zigzag_scan8x8;
  4076. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  4077. h->field_scan_q0 = field_scan;
  4078. h->field_scan8x8_q0 = field_scan8x8;
  4079. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  4080. }else{
  4081. h->zigzag_scan_q0 = h->zigzag_scan;
  4082. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  4083. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  4084. h->field_scan_q0 = h->field_scan;
  4085. h->field_scan8x8_q0 = h->field_scan8x8;
  4086. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  4087. }
  4088. alloc_tables(h);
  4089. s->avctx->width = s->width;
  4090. s->avctx->height = s->height;
  4091. s->avctx->sample_aspect_ratio= h->sps.sar;
  4092. if(!s->avctx->sample_aspect_ratio.den)
  4093. s->avctx->sample_aspect_ratio.den = 1;
  4094. if(h->sps.timing_info_present_flag){
  4095. s->avctx->time_base= (AVRational){h->sps.num_units_in_tick * 2, h->sps.time_scale};
  4096. if(h->x264_build > 0 && h->x264_build < 44)
  4097. s->avctx->time_base.den *= 2;
  4098. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  4099. s->avctx->time_base.num, s->avctx->time_base.den, 1<<30);
  4100. }
  4101. }
  4102. if(h->slice_num == 0){
  4103. if(frame_start(h) < 0)
  4104. return -1;
  4105. }
  4106. s->current_picture_ptr->frame_num= //FIXME frame_num cleanup
  4107. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  4108. h->mb_mbaff = 0;
  4109. h->mb_aff_frame = 0;
  4110. if(h->sps.frame_mbs_only_flag){
  4111. s->picture_structure= PICT_FRAME;
  4112. }else{
  4113. if(get_bits1(&s->gb)) { //field_pic_flag
  4114. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  4115. av_log(h->s.avctx, AV_LOG_ERROR, "PAFF interlacing is not implemented\n");
  4116. } else {
  4117. s->picture_structure= PICT_FRAME;
  4118. h->mb_aff_frame = h->sps.mb_aff;
  4119. }
  4120. }
  4121. assert(s->mb_num == s->mb_width * s->mb_height);
  4122. if(first_mb_in_slice << h->mb_aff_frame >= s->mb_num ||
  4123. first_mb_in_slice >= s->mb_num){
  4124. av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  4125. return -1;
  4126. }
  4127. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  4128. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << h->mb_aff_frame;
  4129. assert(s->mb_y < s->mb_height);
  4130. if(s->picture_structure==PICT_FRAME){
  4131. h->curr_pic_num= h->frame_num;
  4132. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  4133. }else{
  4134. h->curr_pic_num= 2*h->frame_num;
  4135. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  4136. }
  4137. if(h->nal_unit_type == NAL_IDR_SLICE){
  4138. get_ue_golomb(&s->gb); /* idr_pic_id */
  4139. }
  4140. if(h->sps.poc_type==0){
  4141. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  4142. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  4143. h->delta_poc_bottom= get_se_golomb(&s->gb);
  4144. }
  4145. }
  4146. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  4147. h->delta_poc[0]= get_se_golomb(&s->gb);
  4148. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  4149. h->delta_poc[1]= get_se_golomb(&s->gb);
  4150. }
  4151. init_poc(h);
  4152. if(h->pps.redundant_pic_cnt_present){
  4153. h->redundant_pic_count= get_ue_golomb(&s->gb);
  4154. }
  4155. //set defaults, might be overriden a few line later
  4156. h->ref_count[0]= h->pps.ref_count[0];
  4157. h->ref_count[1]= h->pps.ref_count[1];
  4158. if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){
  4159. if(h->slice_type == B_TYPE){
  4160. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  4161. if(h->sps.mb_aff && h->direct_spatial_mv_pred)
  4162. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + spatial direct mode is not implemented\n");
  4163. }
  4164. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  4165. if(num_ref_idx_active_override_flag){
  4166. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  4167. if(h->slice_type==B_TYPE)
  4168. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  4169. if(h->ref_count[0] > 32 || h->ref_count[1] > 32){
  4170. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  4171. h->ref_count[0]= h->ref_count[1]= 1;
  4172. return -1;
  4173. }
  4174. }
  4175. }
  4176. if(!default_ref_list_done){
  4177. fill_default_ref_list(h);
  4178. }
  4179. if(decode_ref_pic_list_reordering(h) < 0)
  4180. return -1;
  4181. if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
  4182. || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
  4183. pred_weight_table(h);
  4184. else if(h->pps.weighted_bipred_idc==2 && h->slice_type==B_TYPE)
  4185. implicit_weight_table(h);
  4186. else
  4187. h->use_weight = 0;
  4188. if(s->current_picture.reference)
  4189. decode_ref_pic_marking(h);
  4190. if(FRAME_MBAFF)
  4191. fill_mbaff_ref_list(h);
  4192. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac ){
  4193. tmp = get_ue_golomb(&s->gb);
  4194. if(tmp > 2){
  4195. av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
  4196. return -1;
  4197. }
  4198. h->cabac_init_idc= tmp;
  4199. }
  4200. h->last_qscale_diff = 0;
  4201. tmp = h->pps.init_qp + get_se_golomb(&s->gb);
  4202. if(tmp>51){
  4203. av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  4204. return -1;
  4205. }
  4206. s->qscale= tmp;
  4207. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4208. //FIXME qscale / qp ... stuff
  4209. if(h->slice_type == SP_TYPE){
  4210. get_bits1(&s->gb); /* sp_for_switch_flag */
  4211. }
  4212. if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
  4213. get_se_golomb(&s->gb); /* slice_qs_delta */
  4214. }
  4215. h->deblocking_filter = 1;
  4216. h->slice_alpha_c0_offset = 0;
  4217. h->slice_beta_offset = 0;
  4218. if( h->pps.deblocking_filter_parameters_present ) {
  4219. tmp= get_ue_golomb(&s->gb);
  4220. if(tmp > 2){
  4221. av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
  4222. return -1;
  4223. }
  4224. h->deblocking_filter= tmp;
  4225. if(h->deblocking_filter < 2)
  4226. h->deblocking_filter^= 1; // 1<->0
  4227. if( h->deblocking_filter ) {
  4228. h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
  4229. h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
  4230. }
  4231. }
  4232. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  4233. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type != I_TYPE)
  4234. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type == B_TYPE)
  4235. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  4236. h->deblocking_filter= 0;
  4237. #if 0 //FMO
  4238. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  4239. slice_group_change_cycle= get_bits(&s->gb, ?);
  4240. #endif
  4241. h->slice_num++;
  4242. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
  4243. h->emu_edge_height= FRAME_MBAFF ? 0 : h->emu_edge_width;
  4244. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  4245. 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",
  4246. h->slice_num,
  4247. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  4248. first_mb_in_slice,
  4249. av_get_pict_type_char(h->slice_type),
  4250. pps_id, h->frame_num,
  4251. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  4252. h->ref_count[0], h->ref_count[1],
  4253. s->qscale,
  4254. h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2,
  4255. h->use_weight,
  4256. h->use_weight==1 && h->use_weight_chroma ? "c" : ""
  4257. );
  4258. }
  4259. if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !s->current_picture.reference){
  4260. s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
  4261. s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
  4262. }else{
  4263. s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
  4264. s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
  4265. }
  4266. return 0;
  4267. }
  4268. /**
  4269. *
  4270. */
  4271. static inline int get_level_prefix(GetBitContext *gb){
  4272. unsigned int buf;
  4273. int log;
  4274. OPEN_READER(re, gb);
  4275. UPDATE_CACHE(re, gb);
  4276. buf=GET_CACHE(re, gb);
  4277. log= 32 - av_log2(buf);
  4278. #ifdef TRACE
  4279. print_bin(buf>>(32-log), log);
  4280. 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__);
  4281. #endif
  4282. LAST_SKIP_BITS(re, gb, log);
  4283. CLOSE_READER(re, gb);
  4284. return log-1;
  4285. }
  4286. static inline int get_dct8x8_allowed(H264Context *h){
  4287. int i;
  4288. for(i=0; i<4; i++){
  4289. if(!IS_SUB_8X8(h->sub_mb_type[i])
  4290. || (!h->sps.direct_8x8_inference_flag && IS_DIRECT(h->sub_mb_type[i])))
  4291. return 0;
  4292. }
  4293. return 1;
  4294. }
  4295. /**
  4296. * decodes a residual block.
  4297. * @param n block index
  4298. * @param scantable scantable
  4299. * @param max_coeff number of coefficients in the block
  4300. * @return <0 if an error occured
  4301. */
  4302. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
  4303. MpegEncContext * const s = &h->s;
  4304. 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};
  4305. int level[16];
  4306. int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
  4307. //FIXME put trailing_onex into the context
  4308. if(n == CHROMA_DC_BLOCK_INDEX){
  4309. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  4310. total_coeff= coeff_token>>2;
  4311. }else{
  4312. if(n == LUMA_DC_BLOCK_INDEX){
  4313. total_coeff= pred_non_zero_count(h, 0);
  4314. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4315. total_coeff= coeff_token>>2;
  4316. }else{
  4317. total_coeff= pred_non_zero_count(h, n);
  4318. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4319. total_coeff= coeff_token>>2;
  4320. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  4321. }
  4322. }
  4323. //FIXME set last_non_zero?
  4324. if(total_coeff==0)
  4325. return 0;
  4326. if(total_coeff > (unsigned)max_coeff) {
  4327. av_log(h->s.avctx, AV_LOG_ERROR, "corrupted macroblock %d %d (total_coeff=%d)\n", s->mb_x, s->mb_y, total_coeff);
  4328. return -1;
  4329. }
  4330. trailing_ones= coeff_token&3;
  4331. tprintf("trailing:%d, total:%d\n", trailing_ones, total_coeff);
  4332. assert(total_coeff<=16);
  4333. for(i=0; i<trailing_ones; i++){
  4334. level[i]= 1 - 2*get_bits1(gb);
  4335. }
  4336. if(i<total_coeff) {
  4337. int level_code, mask;
  4338. int suffix_length = total_coeff > 10 && trailing_ones < 3;
  4339. int prefix= get_level_prefix(gb);
  4340. //first coefficient has suffix_length equal to 0 or 1
  4341. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  4342. if(suffix_length)
  4343. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4344. else
  4345. level_code= (prefix<<suffix_length); //part
  4346. }else if(prefix==14){
  4347. if(suffix_length)
  4348. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4349. else
  4350. level_code= prefix + get_bits(gb, 4); //part
  4351. }else if(prefix==15){
  4352. level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part
  4353. if(suffix_length==0) level_code+=15; //FIXME doesn't make (much)sense
  4354. }else{
  4355. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4356. return -1;
  4357. }
  4358. if(trailing_ones < 3) level_code += 2;
  4359. suffix_length = 1;
  4360. if(level_code > 5)
  4361. suffix_length++;
  4362. mask= -(level_code&1);
  4363. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4364. i++;
  4365. //remaining coefficients have suffix_length > 0
  4366. for(;i<total_coeff;i++) {
  4367. static const int suffix_limit[7] = {0,5,11,23,47,95,INT_MAX };
  4368. prefix = get_level_prefix(gb);
  4369. if(prefix<15){
  4370. level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
  4371. }else if(prefix==15){
  4372. level_code = (prefix<<suffix_length) + get_bits(gb, 12);
  4373. }else{
  4374. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4375. return -1;
  4376. }
  4377. mask= -(level_code&1);
  4378. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4379. if(level_code > suffix_limit[suffix_length])
  4380. suffix_length++;
  4381. }
  4382. }
  4383. if(total_coeff == max_coeff)
  4384. zeros_left=0;
  4385. else{
  4386. if(n == CHROMA_DC_BLOCK_INDEX)
  4387. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  4388. else
  4389. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  4390. }
  4391. coeff_num = zeros_left + total_coeff - 1;
  4392. j = scantable[coeff_num];
  4393. if(n > 24){
  4394. block[j] = level[0];
  4395. for(i=1;i<total_coeff;i++) {
  4396. if(zeros_left <= 0)
  4397. run_before = 0;
  4398. else if(zeros_left < 7){
  4399. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4400. }else{
  4401. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4402. }
  4403. zeros_left -= run_before;
  4404. coeff_num -= 1 + run_before;
  4405. j= scantable[ coeff_num ];
  4406. block[j]= level[i];
  4407. }
  4408. }else{
  4409. block[j] = (level[0] * qmul[j] + 32)>>6;
  4410. for(i=1;i<total_coeff;i++) {
  4411. if(zeros_left <= 0)
  4412. run_before = 0;
  4413. else if(zeros_left < 7){
  4414. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4415. }else{
  4416. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4417. }
  4418. zeros_left -= run_before;
  4419. coeff_num -= 1 + run_before;
  4420. j= scantable[ coeff_num ];
  4421. block[j]= (level[i] * qmul[j] + 32)>>6;
  4422. }
  4423. }
  4424. if(zeros_left<0){
  4425. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  4426. return -1;
  4427. }
  4428. return 0;
  4429. }
  4430. static void predict_field_decoding_flag(H264Context *h){
  4431. MpegEncContext * const s = &h->s;
  4432. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4433. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  4434. ? s->current_picture.mb_type[mb_xy-1]
  4435. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  4436. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  4437. : 0;
  4438. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  4439. }
  4440. /**
  4441. * decodes a P_SKIP or B_SKIP macroblock
  4442. */
  4443. static void decode_mb_skip(H264Context *h){
  4444. MpegEncContext * const s = &h->s;
  4445. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4446. int mb_type=0;
  4447. memset(h->non_zero_count[mb_xy], 0, 16);
  4448. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  4449. if(MB_FIELD)
  4450. mb_type|= MB_TYPE_INTERLACED;
  4451. if( h->slice_type == B_TYPE )
  4452. {
  4453. // just for fill_caches. pred_direct_motion will set the real mb_type
  4454. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
  4455. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4456. pred_direct_motion(h, &mb_type);
  4457. mb_type|= MB_TYPE_SKIP;
  4458. }
  4459. else
  4460. {
  4461. int mx, my;
  4462. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
  4463. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4464. pred_pskip_motion(h, &mx, &my);
  4465. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  4466. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  4467. }
  4468. write_back_motion(h, mb_type);
  4469. s->current_picture.mb_type[mb_xy]= mb_type;
  4470. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4471. h->slice_table[ mb_xy ]= h->slice_num;
  4472. h->prev_mb_skipped= 1;
  4473. }
  4474. /**
  4475. * decodes a macroblock
  4476. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  4477. */
  4478. static int decode_mb_cavlc(H264Context *h){
  4479. MpegEncContext * const s = &h->s;
  4480. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4481. int partition_count;
  4482. unsigned int mb_type, cbp;
  4483. int dct8x8_allowed= h->pps.transform_8x8_mode;
  4484. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?
  4485. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  4486. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  4487. down the code */
  4488. if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){
  4489. if(s->mb_skip_run==-1)
  4490. s->mb_skip_run= get_ue_golomb(&s->gb);
  4491. if (s->mb_skip_run--) {
  4492. if(FRAME_MBAFF && (s->mb_y&1) == 0){
  4493. if(s->mb_skip_run==0)
  4494. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4495. else
  4496. predict_field_decoding_flag(h);
  4497. }
  4498. decode_mb_skip(h);
  4499. return 0;
  4500. }
  4501. }
  4502. if(FRAME_MBAFF){
  4503. if( (s->mb_y&1) == 0 )
  4504. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4505. }else
  4506. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  4507. h->prev_mb_skipped= 0;
  4508. mb_type= get_ue_golomb(&s->gb);
  4509. if(h->slice_type == B_TYPE){
  4510. if(mb_type < 23){
  4511. partition_count= b_mb_type_info[mb_type].partition_count;
  4512. mb_type= b_mb_type_info[mb_type].type;
  4513. }else{
  4514. mb_type -= 23;
  4515. goto decode_intra_mb;
  4516. }
  4517. }else if(h->slice_type == P_TYPE /*|| h->slice_type == SP_TYPE */){
  4518. if(mb_type < 5){
  4519. partition_count= p_mb_type_info[mb_type].partition_count;
  4520. mb_type= p_mb_type_info[mb_type].type;
  4521. }else{
  4522. mb_type -= 5;
  4523. goto decode_intra_mb;
  4524. }
  4525. }else{
  4526. assert(h->slice_type == I_TYPE);
  4527. decode_intra_mb:
  4528. if(mb_type > 25){
  4529. 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);
  4530. return -1;
  4531. }
  4532. partition_count=0;
  4533. cbp= i_mb_type_info[mb_type].cbp;
  4534. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  4535. mb_type= i_mb_type_info[mb_type].type;
  4536. }
  4537. if(MB_FIELD)
  4538. mb_type |= MB_TYPE_INTERLACED;
  4539. h->slice_table[ mb_xy ]= h->slice_num;
  4540. if(IS_INTRA_PCM(mb_type)){
  4541. unsigned int x, y;
  4542. // we assume these blocks are very rare so we dont optimize it
  4543. align_get_bits(&s->gb);
  4544. // The pixels are stored in the same order as levels in h->mb array.
  4545. for(y=0; y<16; y++){
  4546. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  4547. for(x=0; x<16; x++){
  4548. tprintf("LUMA ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4549. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= get_bits(&s->gb, 8);
  4550. }
  4551. }
  4552. for(y=0; y<8; y++){
  4553. const int index= 256 + 4*(y&3) + 32*(y>>2);
  4554. for(x=0; x<8; x++){
  4555. tprintf("CHROMA U ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4556. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4557. }
  4558. }
  4559. for(y=0; y<8; y++){
  4560. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  4561. for(x=0; x<8; x++){
  4562. tprintf("CHROMA V ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4563. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4564. }
  4565. }
  4566. // In deblocking, the quantizer is 0
  4567. s->current_picture.qscale_table[mb_xy]= 0;
  4568. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  4569. // All coeffs are present
  4570. memset(h->non_zero_count[mb_xy], 16, 16);
  4571. s->current_picture.mb_type[mb_xy]= mb_type;
  4572. return 0;
  4573. }
  4574. if(MB_MBAFF){
  4575. h->ref_count[0] <<= 1;
  4576. h->ref_count[1] <<= 1;
  4577. }
  4578. fill_caches(h, mb_type, 0);
  4579. //mb_pred
  4580. if(IS_INTRA(mb_type)){
  4581. int pred_mode;
  4582. // init_top_left_availability(h);
  4583. if(IS_INTRA4x4(mb_type)){
  4584. int i;
  4585. int di = 1;
  4586. if(dct8x8_allowed && get_bits1(&s->gb)){
  4587. mb_type |= MB_TYPE_8x8DCT;
  4588. di = 4;
  4589. }
  4590. // fill_intra4x4_pred_table(h);
  4591. for(i=0; i<16; i+=di){
  4592. int mode= pred_intra_mode(h, i);
  4593. if(!get_bits1(&s->gb)){
  4594. const int rem_mode= get_bits(&s->gb, 3);
  4595. mode = rem_mode + (rem_mode >= mode);
  4596. }
  4597. if(di==4)
  4598. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  4599. else
  4600. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  4601. }
  4602. write_back_intra_pred_mode(h);
  4603. if( check_intra4x4_pred_mode(h) < 0)
  4604. return -1;
  4605. }else{
  4606. h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
  4607. if(h->intra16x16_pred_mode < 0)
  4608. return -1;
  4609. }
  4610. pred_mode= check_intra_pred_mode(h, get_ue_golomb(&s->gb));
  4611. if(pred_mode < 0)
  4612. return -1;
  4613. h->chroma_pred_mode= pred_mode;
  4614. }else if(partition_count==4){
  4615. int i, j, sub_partition_count[4], list, ref[2][4];
  4616. if(h->slice_type == B_TYPE){
  4617. for(i=0; i<4; i++){
  4618. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4619. if(h->sub_mb_type[i] >=13){
  4620. 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);
  4621. return -1;
  4622. }
  4623. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4624. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4625. }
  4626. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  4627. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
  4628. pred_direct_motion(h, &mb_type);
  4629. h->ref_cache[0][scan8[4]] =
  4630. h->ref_cache[1][scan8[4]] =
  4631. h->ref_cache[0][scan8[12]] =
  4632. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  4633. }
  4634. }else{
  4635. assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE); //FIXME SP correct ?
  4636. for(i=0; i<4; i++){
  4637. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4638. if(h->sub_mb_type[i] >=4){
  4639. 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);
  4640. return -1;
  4641. }
  4642. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4643. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4644. }
  4645. }
  4646. for(list=0; list<2; list++){
  4647. int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4648. if(ref_count == 0) continue;
  4649. for(i=0; i<4; i++){
  4650. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  4651. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4652. unsigned int tmp = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
  4653. if(tmp>=ref_count){
  4654. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", tmp);
  4655. return -1;
  4656. }
  4657. ref[list][i]= tmp;
  4658. }else{
  4659. //FIXME
  4660. ref[list][i] = -1;
  4661. }
  4662. }
  4663. }
  4664. if(dct8x8_allowed)
  4665. dct8x8_allowed = get_dct8x8_allowed(h);
  4666. for(list=0; list<2; list++){
  4667. const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4668. if(ref_count == 0) continue;
  4669. for(i=0; i<4; i++){
  4670. if(IS_DIRECT(h->sub_mb_type[i])) {
  4671. h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];
  4672. continue;
  4673. }
  4674. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  4675. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  4676. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4677. const int sub_mb_type= h->sub_mb_type[i];
  4678. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  4679. for(j=0; j<sub_partition_count[i]; j++){
  4680. int mx, my;
  4681. const int index= 4*i + block_width*j;
  4682. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  4683. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  4684. mx += get_se_golomb(&s->gb);
  4685. my += get_se_golomb(&s->gb);
  4686. tprintf("final mv:%d %d\n", mx, my);
  4687. if(IS_SUB_8X8(sub_mb_type)){
  4688. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  4689. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  4690. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  4691. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  4692. }else if(IS_SUB_8X4(sub_mb_type)){
  4693. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  4694. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  4695. }else if(IS_SUB_4X8(sub_mb_type)){
  4696. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  4697. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  4698. }else{
  4699. assert(IS_SUB_4X4(sub_mb_type));
  4700. mv_cache[ 0 ][0]= mx;
  4701. mv_cache[ 0 ][1]= my;
  4702. }
  4703. }
  4704. }else{
  4705. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  4706. p[0] = p[1]=
  4707. p[8] = p[9]= 0;
  4708. }
  4709. }
  4710. }
  4711. }else if(IS_DIRECT(mb_type)){
  4712. pred_direct_motion(h, &mb_type);
  4713. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  4714. }else{
  4715. int list, mx, my, i;
  4716. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  4717. if(IS_16X16(mb_type)){
  4718. for(list=0; list<2; list++){
  4719. if(h->ref_count[list]>0){
  4720. if(IS_DIR(mb_type, 0, list)){
  4721. unsigned int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4722. if(val >= h->ref_count[list]){
  4723. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4724. return -1;
  4725. }
  4726. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  4727. }else
  4728. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4729. }
  4730. }
  4731. for(list=0; list<2; list++){
  4732. if(IS_DIR(mb_type, 0, list)){
  4733. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  4734. mx += get_se_golomb(&s->gb);
  4735. my += get_se_golomb(&s->gb);
  4736. tprintf("final mv:%d %d\n", mx, my);
  4737. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  4738. }else
  4739. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  4740. }
  4741. }
  4742. else if(IS_16X8(mb_type)){
  4743. for(list=0; list<2; list++){
  4744. if(h->ref_count[list]>0){
  4745. for(i=0; i<2; i++){
  4746. if(IS_DIR(mb_type, i, list)){
  4747. unsigned int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4748. if(val >= h->ref_count[list]){
  4749. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4750. return -1;
  4751. }
  4752. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  4753. }else
  4754. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  4755. }
  4756. }
  4757. }
  4758. for(list=0; list<2; list++){
  4759. for(i=0; i<2; i++){
  4760. if(IS_DIR(mb_type, i, list)){
  4761. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  4762. mx += get_se_golomb(&s->gb);
  4763. my += get_se_golomb(&s->gb);
  4764. tprintf("final mv:%d %d\n", mx, my);
  4765. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  4766. }else
  4767. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  4768. }
  4769. }
  4770. }else{
  4771. assert(IS_8X16(mb_type));
  4772. for(list=0; list<2; list++){
  4773. if(h->ref_count[list]>0){
  4774. for(i=0; i<2; i++){
  4775. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  4776. unsigned int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4777. if(val >= h->ref_count[list]){
  4778. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4779. return -1;
  4780. }
  4781. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  4782. }else
  4783. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4784. }
  4785. }
  4786. }
  4787. for(list=0; list<2; list++){
  4788. for(i=0; i<2; i++){
  4789. if(IS_DIR(mb_type, i, list)){
  4790. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  4791. mx += get_se_golomb(&s->gb);
  4792. my += get_se_golomb(&s->gb);
  4793. tprintf("final mv:%d %d\n", mx, my);
  4794. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  4795. }else
  4796. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  4797. }
  4798. }
  4799. }
  4800. }
  4801. if(IS_INTER(mb_type))
  4802. write_back_motion(h, mb_type);
  4803. if(!IS_INTRA16x16(mb_type)){
  4804. cbp= get_ue_golomb(&s->gb);
  4805. if(cbp > 47){
  4806. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%u) at %d %d\n", cbp, s->mb_x, s->mb_y);
  4807. return -1;
  4808. }
  4809. if(IS_INTRA4x4(mb_type))
  4810. cbp= golomb_to_intra4x4_cbp[cbp];
  4811. else
  4812. cbp= golomb_to_inter_cbp[cbp];
  4813. }
  4814. h->cbp = cbp;
  4815. if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
  4816. if(get_bits1(&s->gb))
  4817. mb_type |= MB_TYPE_8x8DCT;
  4818. }
  4819. s->current_picture.mb_type[mb_xy]= mb_type;
  4820. if(cbp || IS_INTRA16x16(mb_type)){
  4821. int i8x8, i4x4, chroma_idx;
  4822. int chroma_qp, dquant;
  4823. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  4824. const uint8_t *scan, *scan8x8, *dc_scan;
  4825. // fill_non_zero_count_cache(h);
  4826. if(IS_INTERLACED(mb_type)){
  4827. scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
  4828. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  4829. dc_scan= luma_dc_field_scan;
  4830. }else{
  4831. scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
  4832. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  4833. dc_scan= luma_dc_zigzag_scan;
  4834. }
  4835. dquant= get_se_golomb(&s->gb);
  4836. if( dquant > 25 || dquant < -26 ){
  4837. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  4838. return -1;
  4839. }
  4840. s->qscale += dquant;
  4841. if(((unsigned)s->qscale) > 51){
  4842. if(s->qscale<0) s->qscale+= 52;
  4843. else s->qscale-= 52;
  4844. }
  4845. h->chroma_qp= chroma_qp= get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4846. if(IS_INTRA16x16(mb_type)){
  4847. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){
  4848. return -1; //FIXME continue if partitioned and other return -1 too
  4849. }
  4850. assert((cbp&15) == 0 || (cbp&15) == 15);
  4851. if(cbp&15){
  4852. for(i8x8=0; i8x8<4; i8x8++){
  4853. for(i4x4=0; i4x4<4; i4x4++){
  4854. const int index= i4x4 + 4*i8x8;
  4855. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){
  4856. return -1;
  4857. }
  4858. }
  4859. }
  4860. }else{
  4861. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  4862. }
  4863. }else{
  4864. for(i8x8=0; i8x8<4; i8x8++){
  4865. if(cbp & (1<<i8x8)){
  4866. if(IS_8x8DCT(mb_type)){
  4867. DCTELEM *buf = &h->mb[64*i8x8];
  4868. uint8_t *nnz;
  4869. for(i4x4=0; i4x4<4; i4x4++){
  4870. if( decode_residual(h, gb, buf, i4x4+4*i8x8, scan8x8+16*i4x4,
  4871. h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )
  4872. return -1;
  4873. }
  4874. nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4875. nnz[0] += nnz[1] + nnz[8] + nnz[9];
  4876. }else{
  4877. for(i4x4=0; i4x4<4; i4x4++){
  4878. const int index= i4x4 + 4*i8x8;
  4879. if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){
  4880. return -1;
  4881. }
  4882. }
  4883. }
  4884. }else{
  4885. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4886. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  4887. }
  4888. }
  4889. }
  4890. if(cbp&0x30){
  4891. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  4892. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){
  4893. return -1;
  4894. }
  4895. }
  4896. if(cbp&0x20){
  4897. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  4898. for(i4x4=0; i4x4<4; i4x4++){
  4899. const int index= 16 + 4*chroma_idx + i4x4;
  4900. 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){
  4901. return -1;
  4902. }
  4903. }
  4904. }
  4905. }else{
  4906. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4907. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4908. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4909. }
  4910. }else{
  4911. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4912. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  4913. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4914. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4915. }
  4916. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4917. write_back_non_zero_count(h);
  4918. if(MB_MBAFF){
  4919. h->ref_count[0] >>= 1;
  4920. h->ref_count[1] >>= 1;
  4921. }
  4922. return 0;
  4923. }
  4924. static int decode_cabac_field_decoding_flag(H264Context *h) {
  4925. MpegEncContext * const s = &h->s;
  4926. const int mb_x = s->mb_x;
  4927. const int mb_y = s->mb_y & ~1;
  4928. const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
  4929. const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
  4930. unsigned int ctx = 0;
  4931. if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
  4932. ctx += 1;
  4933. }
  4934. if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
  4935. ctx += 1;
  4936. }
  4937. return get_cabac_noinline( &h->cabac, &h->cabac_state[70 + ctx] );
  4938. }
  4939. static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
  4940. uint8_t *state= &h->cabac_state[ctx_base];
  4941. int mb_type;
  4942. if(intra_slice){
  4943. MpegEncContext * const s = &h->s;
  4944. const int mba_xy = h->left_mb_xy[0];
  4945. const int mbb_xy = h->top_mb_xy;
  4946. int ctx=0;
  4947. if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
  4948. ctx++;
  4949. if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
  4950. ctx++;
  4951. if( get_cabac_noinline( &h->cabac, &state[ctx] ) == 0 )
  4952. return 0; /* I4x4 */
  4953. state += 2;
  4954. }else{
  4955. if( get_cabac_noinline( &h->cabac, &state[0] ) == 0 )
  4956. return 0; /* I4x4 */
  4957. }
  4958. if( get_cabac_terminate( &h->cabac ) )
  4959. return 25; /* PCM */
  4960. mb_type = 1; /* I16x16 */
  4961. mb_type += 12 * get_cabac_noinline( &h->cabac, &state[1] ); /* cbp_luma != 0 */
  4962. if( get_cabac_noinline( &h->cabac, &state[2] ) ) /* cbp_chroma */
  4963. mb_type += 4 + 4 * get_cabac_noinline( &h->cabac, &state[2+intra_slice] );
  4964. mb_type += 2 * get_cabac_noinline( &h->cabac, &state[3+intra_slice] );
  4965. mb_type += 1 * get_cabac_noinline( &h->cabac, &state[3+2*intra_slice] );
  4966. return mb_type;
  4967. }
  4968. static int decode_cabac_mb_type( H264Context *h ) {
  4969. MpegEncContext * const s = &h->s;
  4970. if( h->slice_type == I_TYPE ) {
  4971. return decode_cabac_intra_mb_type(h, 3, 1);
  4972. } else if( h->slice_type == P_TYPE ) {
  4973. if( get_cabac_noinline( &h->cabac, &h->cabac_state[14] ) == 0 ) {
  4974. /* P-type */
  4975. if( get_cabac_noinline( &h->cabac, &h->cabac_state[15] ) == 0 ) {
  4976. /* P_L0_D16x16, P_8x8 */
  4977. return 3 * get_cabac_noinline( &h->cabac, &h->cabac_state[16] );
  4978. } else {
  4979. /* P_L0_D8x16, P_L0_D16x8 */
  4980. return 2 - get_cabac_noinline( &h->cabac, &h->cabac_state[17] );
  4981. }
  4982. } else {
  4983. return decode_cabac_intra_mb_type(h, 17, 0) + 5;
  4984. }
  4985. } else if( h->slice_type == B_TYPE ) {
  4986. const int mba_xy = h->left_mb_xy[0];
  4987. const int mbb_xy = h->top_mb_xy;
  4988. int ctx = 0;
  4989. int bits;
  4990. if( h->slice_table[mba_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
  4991. ctx++;
  4992. if( h->slice_table[mbb_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
  4993. ctx++;
  4994. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+ctx] ) )
  4995. return 0; /* B_Direct_16x16 */
  4996. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+3] ) ) {
  4997. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
  4998. }
  4999. bits = get_cabac_noinline( &h->cabac, &h->cabac_state[27+4] ) << 3;
  5000. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 2;
  5001. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 1;
  5002. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  5003. if( bits < 8 )
  5004. return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
  5005. else if( bits == 13 ) {
  5006. return decode_cabac_intra_mb_type(h, 32, 0) + 23;
  5007. } else if( bits == 14 )
  5008. return 11; /* B_L1_L0_8x16 */
  5009. else if( bits == 15 )
  5010. return 22; /* B_8x8 */
  5011. bits= ( bits<<1 ) | get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  5012. return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
  5013. } else {
  5014. /* TODO SI/SP frames? */
  5015. return -1;
  5016. }
  5017. }
  5018. static int decode_cabac_mb_skip( H264Context *h, int mb_x, int mb_y ) {
  5019. MpegEncContext * const s = &h->s;
  5020. int mba_xy, mbb_xy;
  5021. int ctx = 0;
  5022. if(FRAME_MBAFF){ //FIXME merge with the stuff in fill_caches?
  5023. int mb_xy = mb_x + (mb_y&~1)*s->mb_stride;
  5024. mba_xy = mb_xy - 1;
  5025. if( (mb_y&1)
  5026. && h->slice_table[mba_xy] == h->slice_num
  5027. && MB_FIELD == !!IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) )
  5028. mba_xy += s->mb_stride;
  5029. if( MB_FIELD ){
  5030. mbb_xy = mb_xy - s->mb_stride;
  5031. if( !(mb_y&1)
  5032. && h->slice_table[mbb_xy] == h->slice_num
  5033. && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) )
  5034. mbb_xy -= s->mb_stride;
  5035. }else
  5036. mbb_xy = mb_x + (mb_y-1)*s->mb_stride;
  5037. }else{
  5038. int mb_xy = mb_x + mb_y*s->mb_stride;
  5039. mba_xy = mb_xy - 1;
  5040. mbb_xy = mb_xy - s->mb_stride;
  5041. }
  5042. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
  5043. ctx++;
  5044. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
  5045. ctx++;
  5046. if( h->slice_type == B_TYPE )
  5047. ctx += 13;
  5048. return get_cabac_noinline( &h->cabac, &h->cabac_state[11+ctx] );
  5049. }
  5050. static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
  5051. int mode = 0;
  5052. if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
  5053. return pred_mode;
  5054. mode += 1 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5055. mode += 2 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5056. mode += 4 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5057. if( mode >= pred_mode )
  5058. return mode + 1;
  5059. else
  5060. return mode;
  5061. }
  5062. static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
  5063. const int mba_xy = h->left_mb_xy[0];
  5064. const int mbb_xy = h->top_mb_xy;
  5065. int ctx = 0;
  5066. /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
  5067. if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
  5068. ctx++;
  5069. if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
  5070. ctx++;
  5071. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
  5072. return 0;
  5073. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  5074. return 1;
  5075. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  5076. return 2;
  5077. else
  5078. return 3;
  5079. }
  5080. static const uint8_t block_idx_x[16] = {
  5081. 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
  5082. };
  5083. static const uint8_t block_idx_y[16] = {
  5084. 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
  5085. };
  5086. static const uint8_t block_idx_xy[4][4] = {
  5087. { 0, 2, 8, 10},
  5088. { 1, 3, 9, 11},
  5089. { 4, 6, 12, 14},
  5090. { 5, 7, 13, 15}
  5091. };
  5092. static int decode_cabac_mb_cbp_luma( H264Context *h) {
  5093. int cbp = 0;
  5094. int cbp_b = -1;
  5095. int i8x8;
  5096. if( h->slice_table[h->top_mb_xy] == h->slice_num ) {
  5097. cbp_b = h->top_cbp;
  5098. tprintf("cbp_b = top_cbp = %x\n", cbp_b);
  5099. }
  5100. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5101. int cbp_a = -1;
  5102. int x, y;
  5103. int ctx = 0;
  5104. x = block_idx_x[4*i8x8];
  5105. y = block_idx_y[4*i8x8];
  5106. if( x > 0 )
  5107. cbp_a = cbp;
  5108. else if( h->slice_table[h->left_mb_xy[0]] == h->slice_num ) {
  5109. cbp_a = h->left_cbp;
  5110. tprintf("cbp_a = left_cbp = %x\n", cbp_a);
  5111. }
  5112. if( y > 0 )
  5113. cbp_b = cbp;
  5114. /* No need to test for skip as we put 0 for skip block */
  5115. /* No need to test for IPCM as we put 1 for IPCM block */
  5116. if( cbp_a >= 0 ) {
  5117. int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
  5118. if( ((cbp_a >> i8x8a)&0x01) == 0 )
  5119. ctx++;
  5120. }
  5121. if( cbp_b >= 0 ) {
  5122. int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
  5123. if( ((cbp_b >> i8x8b)&0x01) == 0 )
  5124. ctx += 2;
  5125. }
  5126. if( get_cabac( &h->cabac, &h->cabac_state[73 + ctx] ) ) {
  5127. cbp |= 1 << i8x8;
  5128. }
  5129. }
  5130. return cbp;
  5131. }
  5132. static int decode_cabac_mb_cbp_chroma( H264Context *h) {
  5133. int ctx;
  5134. int cbp_a, cbp_b;
  5135. cbp_a = (h->left_cbp>>4)&0x03;
  5136. cbp_b = (h-> top_cbp>>4)&0x03;
  5137. ctx = 0;
  5138. if( cbp_a > 0 ) ctx++;
  5139. if( cbp_b > 0 ) ctx += 2;
  5140. if( get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
  5141. return 0;
  5142. ctx = 4;
  5143. if( cbp_a == 2 ) ctx++;
  5144. if( cbp_b == 2 ) ctx += 2;
  5145. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] );
  5146. }
  5147. static int decode_cabac_mb_dqp( H264Context *h) {
  5148. MpegEncContext * const s = &h->s;
  5149. int mbn_xy;
  5150. int ctx = 0;
  5151. int val = 0;
  5152. if( s->mb_x > 0 )
  5153. mbn_xy = s->mb_x + s->mb_y*s->mb_stride - 1;
  5154. else
  5155. mbn_xy = s->mb_width - 1 + (s->mb_y-1)*s->mb_stride;
  5156. if( h->last_qscale_diff != 0 )
  5157. ctx++;
  5158. while( get_cabac_noinline( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
  5159. if( ctx < 2 )
  5160. ctx = 2;
  5161. else
  5162. ctx = 3;
  5163. val++;
  5164. if(val > 102) //prevent infinite loop
  5165. return INT_MIN;
  5166. }
  5167. if( val&0x01 )
  5168. return (val + 1)/2;
  5169. else
  5170. return -(val + 1)/2;
  5171. }
  5172. static int decode_cabac_p_mb_sub_type( H264Context *h ) {
  5173. if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
  5174. return 0; /* 8x8 */
  5175. if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
  5176. return 1; /* 8x4 */
  5177. if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
  5178. return 2; /* 4x8 */
  5179. return 3; /* 4x4 */
  5180. }
  5181. static int decode_cabac_b_mb_sub_type( H264Context *h ) {
  5182. int type;
  5183. if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
  5184. return 0; /* B_Direct_8x8 */
  5185. if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
  5186. return 1 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */
  5187. type = 3;
  5188. if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
  5189. if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
  5190. return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
  5191. type += 4;
  5192. }
  5193. type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
  5194. type += get_cabac( &h->cabac, &h->cabac_state[39] );
  5195. return type;
  5196. }
  5197. static inline int decode_cabac_mb_transform_size( H264Context *h ) {
  5198. return get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
  5199. }
  5200. static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
  5201. int refa = h->ref_cache[list][scan8[n] - 1];
  5202. int refb = h->ref_cache[list][scan8[n] - 8];
  5203. int ref = 0;
  5204. int ctx = 0;
  5205. if( h->slice_type == B_TYPE) {
  5206. if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
  5207. ctx++;
  5208. if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
  5209. ctx += 2;
  5210. } else {
  5211. if( refa > 0 )
  5212. ctx++;
  5213. if( refb > 0 )
  5214. ctx += 2;
  5215. }
  5216. while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
  5217. ref++;
  5218. if( ctx < 4 )
  5219. ctx = 4;
  5220. else
  5221. ctx = 5;
  5222. if(ref >= 32 /*h->ref_list[list]*/){
  5223. av_log(h->s.avctx, AV_LOG_ERROR, "overflow in decode_cabac_mb_ref\n");
  5224. return 0; //FIXME we should return -1 and check the return everywhere
  5225. }
  5226. }
  5227. return ref;
  5228. }
  5229. static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
  5230. int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
  5231. abs( h->mvd_cache[list][scan8[n] - 8][l] );
  5232. int ctxbase = (l == 0) ? 40 : 47;
  5233. int ctx, mvd;
  5234. if( amvd < 3 )
  5235. ctx = 0;
  5236. else if( amvd > 32 )
  5237. ctx = 2;
  5238. else
  5239. ctx = 1;
  5240. if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
  5241. return 0;
  5242. mvd= 1;
  5243. ctx= 3;
  5244. while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
  5245. mvd++;
  5246. if( ctx < 6 )
  5247. ctx++;
  5248. }
  5249. if( mvd >= 9 ) {
  5250. int k = 3;
  5251. while( get_cabac_bypass( &h->cabac ) ) {
  5252. mvd += 1 << k;
  5253. k++;
  5254. if(k>24){
  5255. av_log(h->s.avctx, AV_LOG_ERROR, "overflow in decode_cabac_mb_mvd\n");
  5256. return INT_MIN;
  5257. }
  5258. }
  5259. while( k-- ) {
  5260. if( get_cabac_bypass( &h->cabac ) )
  5261. mvd += 1 << k;
  5262. }
  5263. }
  5264. return get_cabac_bypass_sign( &h->cabac, -mvd );
  5265. }
  5266. static int inline get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) {
  5267. int nza, nzb;
  5268. int ctx = 0;
  5269. if( cat == 0 ) {
  5270. nza = h->left_cbp&0x100;
  5271. nzb = h-> top_cbp&0x100;
  5272. } else if( cat == 1 || cat == 2 ) {
  5273. nza = h->non_zero_count_cache[scan8[idx] - 1];
  5274. nzb = h->non_zero_count_cache[scan8[idx] - 8];
  5275. } else if( cat == 3 ) {
  5276. nza = (h->left_cbp>>(6+idx))&0x01;
  5277. nzb = (h-> top_cbp>>(6+idx))&0x01;
  5278. } else {
  5279. assert(cat == 4);
  5280. nza = h->non_zero_count_cache[scan8[16+idx] - 1];
  5281. nzb = h->non_zero_count_cache[scan8[16+idx] - 8];
  5282. }
  5283. if( nza > 0 )
  5284. ctx++;
  5285. if( nzb > 0 )
  5286. ctx += 2;
  5287. return ctx + 4 * cat;
  5288. }
  5289. static const __attribute((used)) uint8_t last_coeff_flag_offset_8x8[63] = {
  5290. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5291. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  5292. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  5293. 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
  5294. };
  5295. static int decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff) {
  5296. const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
  5297. static const int significant_coeff_flag_offset[2][6] = {
  5298. { 105+0, 105+15, 105+29, 105+44, 105+47, 402 },
  5299. { 277+0, 277+15, 277+29, 277+44, 277+47, 436 }
  5300. };
  5301. static const int last_coeff_flag_offset[2][6] = {
  5302. { 166+0, 166+15, 166+29, 166+44, 166+47, 417 },
  5303. { 338+0, 338+15, 338+29, 338+44, 338+47, 451 }
  5304. };
  5305. static const int coeff_abs_level_m1_offset[6] = {
  5306. 227+0, 227+10, 227+20, 227+30, 227+39, 426
  5307. };
  5308. static const uint8_t significant_coeff_flag_offset_8x8[2][63] = {
  5309. { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
  5310. 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
  5311. 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
  5312. 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12 },
  5313. { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
  5314. 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
  5315. 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
  5316. 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }
  5317. };
  5318. int index[64];
  5319. int last;
  5320. int coeff_count = 0;
  5321. int abslevel1 = 1;
  5322. int abslevelgt1 = 0;
  5323. uint8_t *significant_coeff_ctx_base;
  5324. uint8_t *last_coeff_ctx_base;
  5325. uint8_t *abs_level_m1_ctx_base;
  5326. #ifndef ARCH_X86
  5327. #define CABAC_ON_STACK
  5328. #endif
  5329. #ifdef CABAC_ON_STACK
  5330. #define CC &cc
  5331. CABACContext cc;
  5332. cc.range = h->cabac.range;
  5333. cc.low = h->cabac.low;
  5334. cc.bytestream= h->cabac.bytestream;
  5335. #else
  5336. #define CC &h->cabac
  5337. #endif
  5338. /* cat: 0-> DC 16x16 n = 0
  5339. * 1-> AC 16x16 n = luma4x4idx
  5340. * 2-> Luma4x4 n = luma4x4idx
  5341. * 3-> DC Chroma n = iCbCr
  5342. * 4-> AC Chroma n = 4 * iCbCr + chroma4x4idx
  5343. * 5-> Luma8x8 n = 4 * luma8x8idx
  5344. */
  5345. /* read coded block flag */
  5346. if( cat != 5 ) {
  5347. if( get_cabac( CC, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n ) ] ) == 0 ) {
  5348. if( cat == 1 || cat == 2 )
  5349. h->non_zero_count_cache[scan8[n]] = 0;
  5350. else if( cat == 4 )
  5351. h->non_zero_count_cache[scan8[16+n]] = 0;
  5352. #ifdef CABAC_ON_STACK
  5353. h->cabac.range = cc.range ;
  5354. h->cabac.low = cc.low ;
  5355. h->cabac.bytestream= cc.bytestream;
  5356. #endif
  5357. return 0;
  5358. }
  5359. }
  5360. significant_coeff_ctx_base = h->cabac_state
  5361. + significant_coeff_flag_offset[MB_FIELD][cat];
  5362. last_coeff_ctx_base = h->cabac_state
  5363. + last_coeff_flag_offset[MB_FIELD][cat];
  5364. abs_level_m1_ctx_base = h->cabac_state
  5365. + coeff_abs_level_m1_offset[cat];
  5366. if( cat == 5 ) {
  5367. #define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \
  5368. for(last= 0; last < coefs; last++) { \
  5369. uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \
  5370. if( get_cabac( CC, sig_ctx )) { \
  5371. uint8_t *last_ctx = last_coeff_ctx_base + last_off; \
  5372. index[coeff_count++] = last; \
  5373. if( get_cabac( CC, last_ctx ) ) { \
  5374. last= max_coeff; \
  5375. break; \
  5376. } \
  5377. } \
  5378. }\
  5379. if( last == max_coeff -1 ) {\
  5380. index[coeff_count++] = last;\
  5381. }
  5382. const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
  5383. #if defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__))
  5384. coeff_count= decode_significance_8x8_x86(CC, significant_coeff_ctx_base, index, sig_off);
  5385. } else {
  5386. coeff_count= decode_significance_x86(CC, max_coeff, significant_coeff_ctx_base, index);
  5387. #else
  5388. DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );
  5389. } else {
  5390. DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
  5391. #endif
  5392. }
  5393. assert(coeff_count > 0);
  5394. if( cat == 0 )
  5395. h->cbp_table[mb_xy] |= 0x100;
  5396. else if( cat == 1 || cat == 2 )
  5397. h->non_zero_count_cache[scan8[n]] = coeff_count;
  5398. else if( cat == 3 )
  5399. h->cbp_table[mb_xy] |= 0x40 << n;
  5400. else if( cat == 4 )
  5401. h->non_zero_count_cache[scan8[16+n]] = coeff_count;
  5402. else {
  5403. assert( cat == 5 );
  5404. fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);
  5405. }
  5406. for( coeff_count--; coeff_count >= 0; coeff_count-- ) {
  5407. uint8_t *ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 )) + abs_level_m1_ctx_base;
  5408. int j= scantable[index[coeff_count]];
  5409. if( get_cabac( CC, ctx ) == 0 ) {
  5410. if( !qmul ) {
  5411. block[j] = get_cabac_bypass_sign( CC, -1);
  5412. }else{
  5413. block[j] = (get_cabac_bypass_sign( CC, -qmul[j]) + 32) >> 6;;
  5414. }
  5415. abslevel1++;
  5416. } else {
  5417. int coeff_abs = 2;
  5418. ctx = 5 + FFMIN( 4, abslevelgt1 ) + abs_level_m1_ctx_base;
  5419. while( coeff_abs < 15 && get_cabac( CC, ctx ) ) {
  5420. coeff_abs++;
  5421. }
  5422. if( coeff_abs >= 15 ) {
  5423. int j = 0;
  5424. while( get_cabac_bypass( CC ) ) {
  5425. j++;
  5426. }
  5427. coeff_abs=1;
  5428. while( j-- ) {
  5429. coeff_abs += coeff_abs + get_cabac_bypass( CC );
  5430. }
  5431. coeff_abs+= 14;
  5432. }
  5433. if( !qmul ) {
  5434. if( get_cabac_bypass( CC ) ) block[j] = -coeff_abs;
  5435. else block[j] = coeff_abs;
  5436. }else{
  5437. if( get_cabac_bypass( CC ) ) block[j] = (-coeff_abs * qmul[j] + 32) >> 6;
  5438. else block[j] = ( coeff_abs * qmul[j] + 32) >> 6;
  5439. }
  5440. abslevelgt1++;
  5441. }
  5442. }
  5443. #ifdef CABAC_ON_STACK
  5444. h->cabac.range = cc.range ;
  5445. h->cabac.low = cc.low ;
  5446. h->cabac.bytestream= cc.bytestream;
  5447. #endif
  5448. return 0;
  5449. }
  5450. static void inline compute_mb_neighbors(H264Context *h)
  5451. {
  5452. MpegEncContext * const s = &h->s;
  5453. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  5454. h->top_mb_xy = mb_xy - s->mb_stride;
  5455. h->left_mb_xy[0] = mb_xy - 1;
  5456. if(FRAME_MBAFF){
  5457. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  5458. const int top_pair_xy = pair_xy - s->mb_stride;
  5459. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  5460. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  5461. const int curr_mb_frame_flag = !MB_FIELD;
  5462. const int bottom = (s->mb_y & 1);
  5463. if (bottom
  5464. ? !curr_mb_frame_flag // bottom macroblock
  5465. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  5466. ) {
  5467. h->top_mb_xy -= s->mb_stride;
  5468. }
  5469. if (left_mb_frame_flag != curr_mb_frame_flag) {
  5470. h->left_mb_xy[0] = pair_xy - 1;
  5471. }
  5472. }
  5473. return;
  5474. }
  5475. /**
  5476. * decodes a macroblock
  5477. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  5478. */
  5479. static int decode_mb_cabac(H264Context *h) {
  5480. MpegEncContext * const s = &h->s;
  5481. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  5482. int mb_type, partition_count, cbp = 0;
  5483. int dct8x8_allowed= h->pps.transform_8x8_mode;
  5484. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?)
  5485. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  5486. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) {
  5487. int skip;
  5488. /* a skipped mb needs the aff flag from the following mb */
  5489. if( FRAME_MBAFF && s->mb_x==0 && (s->mb_y&1)==0 )
  5490. predict_field_decoding_flag(h);
  5491. if( FRAME_MBAFF && (s->mb_y&1)==1 && h->prev_mb_skipped )
  5492. skip = h->next_mb_skipped;
  5493. else
  5494. skip = decode_cabac_mb_skip( h, s->mb_x, s->mb_y );
  5495. /* read skip flags */
  5496. if( skip ) {
  5497. if( FRAME_MBAFF && (s->mb_y&1)==0 ){
  5498. s->current_picture.mb_type[mb_xy] = MB_TYPE_SKIP;
  5499. h->next_mb_skipped = decode_cabac_mb_skip( h, s->mb_x, s->mb_y+1 );
  5500. if(h->next_mb_skipped)
  5501. predict_field_decoding_flag(h);
  5502. else
  5503. h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5504. }
  5505. decode_mb_skip(h);
  5506. h->cbp_table[mb_xy] = 0;
  5507. h->chroma_pred_mode_table[mb_xy] = 0;
  5508. h->last_qscale_diff = 0;
  5509. return 0;
  5510. }
  5511. }
  5512. if(FRAME_MBAFF){
  5513. if( (s->mb_y&1) == 0 )
  5514. h->mb_mbaff =
  5515. h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5516. }else
  5517. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  5518. h->prev_mb_skipped = 0;
  5519. compute_mb_neighbors(h);
  5520. if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
  5521. av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
  5522. return -1;
  5523. }
  5524. if( h->slice_type == B_TYPE ) {
  5525. if( mb_type < 23 ){
  5526. partition_count= b_mb_type_info[mb_type].partition_count;
  5527. mb_type= b_mb_type_info[mb_type].type;
  5528. }else{
  5529. mb_type -= 23;
  5530. goto decode_intra_mb;
  5531. }
  5532. } else if( h->slice_type == P_TYPE ) {
  5533. if( mb_type < 5) {
  5534. partition_count= p_mb_type_info[mb_type].partition_count;
  5535. mb_type= p_mb_type_info[mb_type].type;
  5536. } else {
  5537. mb_type -= 5;
  5538. goto decode_intra_mb;
  5539. }
  5540. } else {
  5541. assert(h->slice_type == I_TYPE);
  5542. decode_intra_mb:
  5543. partition_count = 0;
  5544. cbp= i_mb_type_info[mb_type].cbp;
  5545. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  5546. mb_type= i_mb_type_info[mb_type].type;
  5547. }
  5548. if(MB_FIELD)
  5549. mb_type |= MB_TYPE_INTERLACED;
  5550. h->slice_table[ mb_xy ]= h->slice_num;
  5551. if(IS_INTRA_PCM(mb_type)) {
  5552. const uint8_t *ptr;
  5553. unsigned int x, y;
  5554. // We assume these blocks are very rare so we dont optimize it.
  5555. // FIXME The two following lines get the bitstream position in the cabac
  5556. // decode, I think it should be done by a function in cabac.h (or cabac.c).
  5557. ptr= h->cabac.bytestream;
  5558. if(h->cabac.low&0x1) ptr--;
  5559. if(CABAC_BITS==16){
  5560. if(h->cabac.low&0x1FF) ptr--;
  5561. }
  5562. // The pixels are stored in the same order as levels in h->mb array.
  5563. for(y=0; y<16; y++){
  5564. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  5565. for(x=0; x<16; x++){
  5566. tprintf("LUMA ICPM LEVEL (%3d)\n", *ptr);
  5567. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= *ptr++;
  5568. }
  5569. }
  5570. for(y=0; y<8; y++){
  5571. const int index= 256 + 4*(y&3) + 32*(y>>2);
  5572. for(x=0; x<8; x++){
  5573. tprintf("CHROMA U ICPM LEVEL (%3d)\n", *ptr);
  5574. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5575. }
  5576. }
  5577. for(y=0; y<8; y++){
  5578. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  5579. for(x=0; x<8; x++){
  5580. tprintf("CHROMA V ICPM LEVEL (%3d)\n", *ptr);
  5581. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5582. }
  5583. }
  5584. ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);
  5585. // All blocks are present
  5586. h->cbp_table[mb_xy] = 0x1ef;
  5587. h->chroma_pred_mode_table[mb_xy] = 0;
  5588. // In deblocking, the quantizer is 0
  5589. s->current_picture.qscale_table[mb_xy]= 0;
  5590. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  5591. // All coeffs are present
  5592. memset(h->non_zero_count[mb_xy], 16, 16);
  5593. s->current_picture.mb_type[mb_xy]= mb_type;
  5594. return 0;
  5595. }
  5596. if(MB_MBAFF){
  5597. h->ref_count[0] <<= 1;
  5598. h->ref_count[1] <<= 1;
  5599. }
  5600. fill_caches(h, mb_type, 0);
  5601. if( IS_INTRA( mb_type ) ) {
  5602. int i, pred_mode;
  5603. if( IS_INTRA4x4( mb_type ) ) {
  5604. if( dct8x8_allowed && decode_cabac_mb_transform_size( h ) ) {
  5605. mb_type |= MB_TYPE_8x8DCT;
  5606. for( i = 0; i < 16; i+=4 ) {
  5607. int pred = pred_intra_mode( h, i );
  5608. int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5609. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  5610. }
  5611. } else {
  5612. for( i = 0; i < 16; i++ ) {
  5613. int pred = pred_intra_mode( h, i );
  5614. h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5615. //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
  5616. }
  5617. }
  5618. write_back_intra_pred_mode(h);
  5619. if( check_intra4x4_pred_mode(h) < 0 ) return -1;
  5620. } else {
  5621. h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );
  5622. if( h->intra16x16_pred_mode < 0 ) return -1;
  5623. }
  5624. h->chroma_pred_mode_table[mb_xy] =
  5625. pred_mode = decode_cabac_mb_chroma_pre_mode( h );
  5626. pred_mode= check_intra_pred_mode( h, pred_mode );
  5627. if( pred_mode < 0 ) return -1;
  5628. h->chroma_pred_mode= pred_mode;
  5629. } else if( partition_count == 4 ) {
  5630. int i, j, sub_partition_count[4], list, ref[2][4];
  5631. if( h->slice_type == B_TYPE ) {
  5632. for( i = 0; i < 4; i++ ) {
  5633. h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
  5634. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5635. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5636. }
  5637. if( IS_DIRECT(h->sub_mb_type[0] | h->sub_mb_type[1] |
  5638. h->sub_mb_type[2] | h->sub_mb_type[3]) ) {
  5639. pred_direct_motion(h, &mb_type);
  5640. if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
  5641. for( i = 0; i < 4; i++ )
  5642. if( IS_DIRECT(h->sub_mb_type[i]) )
  5643. fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );
  5644. }
  5645. }
  5646. } else {
  5647. for( i = 0; i < 4; i++ ) {
  5648. h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
  5649. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5650. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5651. }
  5652. }
  5653. for( list = 0; list < 2; list++ ) {
  5654. if( h->ref_count[list] > 0 ) {
  5655. for( i = 0; i < 4; i++ ) {
  5656. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  5657. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  5658. if( h->ref_count[list] > 1 )
  5659. ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );
  5660. else
  5661. ref[list][i] = 0;
  5662. } else {
  5663. ref[list][i] = -1;
  5664. }
  5665. h->ref_cache[list][ scan8[4*i]+1 ]=
  5666. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  5667. }
  5668. }
  5669. }
  5670. if(dct8x8_allowed)
  5671. dct8x8_allowed = get_dct8x8_allowed(h);
  5672. for(list=0; list<2; list++){
  5673. for(i=0; i<4; i++){
  5674. if(IS_DIRECT(h->sub_mb_type[i])){
  5675. fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);
  5676. continue;
  5677. }
  5678. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];
  5679. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  5680. const int sub_mb_type= h->sub_mb_type[i];
  5681. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  5682. for(j=0; j<sub_partition_count[i]; j++){
  5683. int mpx, mpy;
  5684. int mx, my;
  5685. const int index= 4*i + block_width*j;
  5686. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  5687. int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];
  5688. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);
  5689. mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );
  5690. my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );
  5691. tprintf("final mv:%d %d\n", mx, my);
  5692. if(IS_SUB_8X8(sub_mb_type)){
  5693. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  5694. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  5695. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  5696. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  5697. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]=
  5698. mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;
  5699. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]=
  5700. mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;
  5701. }else if(IS_SUB_8X4(sub_mb_type)){
  5702. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  5703. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  5704. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]= mx- mpx;
  5705. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]= my - mpy;
  5706. }else if(IS_SUB_4X8(sub_mb_type)){
  5707. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  5708. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  5709. mvd_cache[ 0 ][0]= mvd_cache[ 8 ][0]= mx - mpx;
  5710. mvd_cache[ 0 ][1]= mvd_cache[ 8 ][1]= my - mpy;
  5711. }else{
  5712. assert(IS_SUB_4X4(sub_mb_type));
  5713. mv_cache[ 0 ][0]= mx;
  5714. mv_cache[ 0 ][1]= my;
  5715. mvd_cache[ 0 ][0]= mx - mpx;
  5716. mvd_cache[ 0 ][1]= my - mpy;
  5717. }
  5718. }
  5719. }else{
  5720. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  5721. uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];
  5722. p[0] = p[1] = p[8] = p[9] = 0;
  5723. pd[0]= pd[1]= pd[8]= pd[9]= 0;
  5724. }
  5725. }
  5726. }
  5727. } else if( IS_DIRECT(mb_type) ) {
  5728. pred_direct_motion(h, &mb_type);
  5729. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  5730. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  5731. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  5732. } else {
  5733. int list, mx, my, i, mpx, mpy;
  5734. if(IS_16X16(mb_type)){
  5735. for(list=0; list<2; list++){
  5736. if(IS_DIR(mb_type, 0, list)){
  5737. if(h->ref_count[list] > 0 ){
  5738. const int ref = h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 0 ) : 0;
  5739. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
  5740. }
  5741. }else
  5742. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  5743. }
  5744. for(list=0; list<2; list++){
  5745. if(IS_DIR(mb_type, 0, list)){
  5746. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);
  5747. mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );
  5748. my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );
  5749. tprintf("final mv:%d %d\n", mx, my);
  5750. fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5751. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  5752. }else
  5753. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  5754. }
  5755. }
  5756. else if(IS_16X8(mb_type)){
  5757. for(list=0; list<2; list++){
  5758. if(h->ref_count[list]>0){
  5759. for(i=0; i<2; i++){
  5760. if(IS_DIR(mb_type, i, list)){
  5761. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 8*i ) : 0;
  5762. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
  5763. }else
  5764. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  5765. }
  5766. }
  5767. }
  5768. for(list=0; list<2; list++){
  5769. for(i=0; i<2; i++){
  5770. if(IS_DIR(mb_type, i, list)){
  5771. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);
  5772. mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );
  5773. my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );
  5774. tprintf("final mv:%d %d\n", mx, my);
  5775. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);
  5776. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  5777. }else{
  5778. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5779. fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5780. }
  5781. }
  5782. }
  5783. }else{
  5784. assert(IS_8X16(mb_type));
  5785. for(list=0; list<2; list++){
  5786. if(h->ref_count[list]>0){
  5787. for(i=0; i<2; i++){
  5788. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  5789. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 4*i ) : 0;
  5790. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
  5791. }else
  5792. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  5793. }
  5794. }
  5795. }
  5796. for(list=0; list<2; list++){
  5797. for(i=0; i<2; i++){
  5798. if(IS_DIR(mb_type, i, list)){
  5799. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);
  5800. mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );
  5801. my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );
  5802. tprintf("final mv:%d %d\n", mx, my);
  5803. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5804. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  5805. }else{
  5806. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5807. fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5808. }
  5809. }
  5810. }
  5811. }
  5812. }
  5813. if( IS_INTER( mb_type ) ) {
  5814. h->chroma_pred_mode_table[mb_xy] = 0;
  5815. write_back_motion( h, mb_type );
  5816. }
  5817. if( !IS_INTRA16x16( mb_type ) ) {
  5818. cbp = decode_cabac_mb_cbp_luma( h );
  5819. cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
  5820. }
  5821. h->cbp_table[mb_xy] = h->cbp = cbp;
  5822. if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
  5823. if( decode_cabac_mb_transform_size( h ) )
  5824. mb_type |= MB_TYPE_8x8DCT;
  5825. }
  5826. s->current_picture.mb_type[mb_xy]= mb_type;
  5827. if( cbp || IS_INTRA16x16( mb_type ) ) {
  5828. const uint8_t *scan, *scan8x8, *dc_scan;
  5829. int dqp;
  5830. if(IS_INTERLACED(mb_type)){
  5831. scan8x8= s->qscale ? h->field_scan8x8 : h->field_scan8x8_q0;
  5832. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  5833. dc_scan= luma_dc_field_scan;
  5834. }else{
  5835. scan8x8= s->qscale ? h->zigzag_scan8x8 : h->zigzag_scan8x8_q0;
  5836. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  5837. dc_scan= luma_dc_zigzag_scan;
  5838. }
  5839. h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
  5840. if( dqp == INT_MIN ){
  5841. av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", s->mb_x, s->mb_y);
  5842. return -1;
  5843. }
  5844. s->qscale += dqp;
  5845. if(((unsigned)s->qscale) > 51){
  5846. if(s->qscale<0) s->qscale+= 52;
  5847. else s->qscale-= 52;
  5848. }
  5849. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  5850. if( IS_INTRA16x16( mb_type ) ) {
  5851. int i;
  5852. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
  5853. if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16) < 0)
  5854. return -1;
  5855. if( cbp&15 ) {
  5856. for( i = 0; i < 16; i++ ) {
  5857. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
  5858. if( decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 )
  5859. return -1;
  5860. }
  5861. } else {
  5862. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  5863. }
  5864. } else {
  5865. int i8x8, i4x4;
  5866. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5867. if( cbp & (1<<i8x8) ) {
  5868. if( IS_8x8DCT(mb_type) ) {
  5869. if( decode_cabac_residual(h, h->mb + 64*i8x8, 5, 4*i8x8,
  5870. scan8x8, h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 64) < 0 )
  5871. return -1;
  5872. } else
  5873. for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
  5874. const int index = 4*i8x8 + i4x4;
  5875. //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
  5876. //START_TIMER
  5877. 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 )
  5878. return -1;
  5879. //STOP_TIMER("decode_residual")
  5880. }
  5881. } else {
  5882. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  5883. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  5884. }
  5885. }
  5886. }
  5887. if( cbp&0x30 ){
  5888. int c;
  5889. for( c = 0; c < 2; c++ ) {
  5890. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
  5891. if( decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, NULL, 4) < 0)
  5892. return -1;
  5893. }
  5894. }
  5895. if( cbp&0x20 ) {
  5896. int c, i;
  5897. for( c = 0; c < 2; c++ ) {
  5898. for( i = 0; i < 4; i++ ) {
  5899. const int index = 16 + 4 * c + i;
  5900. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
  5901. 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)
  5902. return -1;
  5903. }
  5904. }
  5905. } else {
  5906. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5907. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5908. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5909. }
  5910. } else {
  5911. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5912. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  5913. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5914. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5915. h->last_qscale_diff = 0;
  5916. }
  5917. s->current_picture.qscale_table[mb_xy]= s->qscale;
  5918. write_back_non_zero_count(h);
  5919. if(MB_MBAFF){
  5920. h->ref_count[0] >>= 1;
  5921. h->ref_count[1] >>= 1;
  5922. }
  5923. return 0;
  5924. }
  5925. static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5926. int i, d;
  5927. const int index_a = qp + h->slice_alpha_c0_offset;
  5928. const int alpha = (alpha_table+52)[index_a];
  5929. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5930. if( bS[0] < 4 ) {
  5931. int8_t tc[4];
  5932. for(i=0; i<4; i++)
  5933. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  5934. h->s.dsp.h264_h_loop_filter_luma(pix, stride, alpha, beta, tc);
  5935. } else {
  5936. /* 16px edge length, because bS=4 is triggered by being at
  5937. * the edge of an intra MB, so all 4 bS are the same */
  5938. for( d = 0; d < 16; d++ ) {
  5939. const int p0 = pix[-1];
  5940. const int p1 = pix[-2];
  5941. const int p2 = pix[-3];
  5942. const int q0 = pix[0];
  5943. const int q1 = pix[1];
  5944. const int q2 = pix[2];
  5945. if( FFABS( p0 - q0 ) < alpha &&
  5946. FFABS( p1 - p0 ) < beta &&
  5947. FFABS( q1 - q0 ) < beta ) {
  5948. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5949. if( FFABS( p2 - p0 ) < beta)
  5950. {
  5951. const int p3 = pix[-4];
  5952. /* p0', p1', p2' */
  5953. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5954. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5955. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5956. } else {
  5957. /* p0' */
  5958. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5959. }
  5960. if( FFABS( q2 - q0 ) < beta)
  5961. {
  5962. const int q3 = pix[3];
  5963. /* q0', q1', q2' */
  5964. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5965. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5966. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5967. } else {
  5968. /* q0' */
  5969. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5970. }
  5971. }else{
  5972. /* p0', q0' */
  5973. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5974. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5975. }
  5976. 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]);
  5977. }
  5978. pix += stride;
  5979. }
  5980. }
  5981. }
  5982. static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5983. int i;
  5984. const int index_a = qp + h->slice_alpha_c0_offset;
  5985. const int alpha = (alpha_table+52)[index_a];
  5986. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5987. if( bS[0] < 4 ) {
  5988. int8_t tc[4];
  5989. for(i=0; i<4; i++)
  5990. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  5991. h->s.dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
  5992. } else {
  5993. h->s.dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
  5994. }
  5995. }
  5996. static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  5997. int i;
  5998. for( i = 0; i < 16; i++, pix += stride) {
  5999. int index_a;
  6000. int alpha;
  6001. int beta;
  6002. int qp_index;
  6003. int bS_index = (i >> 1);
  6004. if (!MB_FIELD) {
  6005. bS_index &= ~1;
  6006. bS_index |= (i & 1);
  6007. }
  6008. if( bS[bS_index] == 0 ) {
  6009. continue;
  6010. }
  6011. qp_index = MB_FIELD ? (i >> 3) : (i & 1);
  6012. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  6013. alpha = (alpha_table+52)[index_a];
  6014. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  6015. if( bS[bS_index] < 4 ) {
  6016. const int tc0 = (tc0_table+52)[index_a][bS[bS_index] - 1];
  6017. const int p0 = pix[-1];
  6018. const int p1 = pix[-2];
  6019. const int p2 = pix[-3];
  6020. const int q0 = pix[0];
  6021. const int q1 = pix[1];
  6022. const int q2 = pix[2];
  6023. if( FFABS( p0 - q0 ) < alpha &&
  6024. FFABS( p1 - p0 ) < beta &&
  6025. FFABS( q1 - q0 ) < beta ) {
  6026. int tc = tc0;
  6027. int i_delta;
  6028. if( FFABS( p2 - p0 ) < beta ) {
  6029. pix[-2] = p1 + clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  6030. tc++;
  6031. }
  6032. if( FFABS( q2 - q0 ) < beta ) {
  6033. pix[1] = q1 + clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  6034. tc++;
  6035. }
  6036. i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  6037. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  6038. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  6039. 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);
  6040. }
  6041. }else{
  6042. const int p0 = pix[-1];
  6043. const int p1 = pix[-2];
  6044. const int p2 = pix[-3];
  6045. const int q0 = pix[0];
  6046. const int q1 = pix[1];
  6047. const int q2 = pix[2];
  6048. if( FFABS( p0 - q0 ) < alpha &&
  6049. FFABS( p1 - p0 ) < beta &&
  6050. FFABS( q1 - q0 ) < beta ) {
  6051. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  6052. if( FFABS( p2 - p0 ) < beta)
  6053. {
  6054. const int p3 = pix[-4];
  6055. /* p0', p1', p2' */
  6056. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  6057. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  6058. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  6059. } else {
  6060. /* p0' */
  6061. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6062. }
  6063. if( FFABS( q2 - q0 ) < beta)
  6064. {
  6065. const int q3 = pix[3];
  6066. /* q0', q1', q2' */
  6067. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  6068. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  6069. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  6070. } else {
  6071. /* q0' */
  6072. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6073. }
  6074. }else{
  6075. /* p0', q0' */
  6076. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6077. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6078. }
  6079. 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]);
  6080. }
  6081. }
  6082. }
  6083. }
  6084. static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  6085. int i;
  6086. for( i = 0; i < 8; i++, pix += stride) {
  6087. int index_a;
  6088. int alpha;
  6089. int beta;
  6090. int qp_index;
  6091. int bS_index = i;
  6092. if( bS[bS_index] == 0 ) {
  6093. continue;
  6094. }
  6095. qp_index = MB_FIELD ? (i >> 2) : (i & 1);
  6096. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  6097. alpha = (alpha_table+52)[index_a];
  6098. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  6099. if( bS[bS_index] < 4 ) {
  6100. const int tc = (tc0_table+52)[index_a][bS[bS_index] - 1] + 1;
  6101. const int p0 = pix[-1];
  6102. const int p1 = pix[-2];
  6103. const int q0 = pix[0];
  6104. const int q1 = pix[1];
  6105. if( FFABS( p0 - q0 ) < alpha &&
  6106. FFABS( p1 - p0 ) < beta &&
  6107. FFABS( q1 - q0 ) < beta ) {
  6108. const int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  6109. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  6110. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  6111. 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);
  6112. }
  6113. }else{
  6114. const int p0 = pix[-1];
  6115. const int p1 = pix[-2];
  6116. const int q0 = pix[0];
  6117. const int q1 = pix[1];
  6118. if( FFABS( p0 - q0 ) < alpha &&
  6119. FFABS( p1 - p0 ) < beta &&
  6120. FFABS( q1 - q0 ) < beta ) {
  6121. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  6122. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  6123. 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]);
  6124. }
  6125. }
  6126. }
  6127. }
  6128. static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  6129. int i, d;
  6130. const int index_a = qp + h->slice_alpha_c0_offset;
  6131. const int alpha = (alpha_table+52)[index_a];
  6132. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  6133. const int pix_next = stride;
  6134. if( bS[0] < 4 ) {
  6135. int8_t tc[4];
  6136. for(i=0; i<4; i++)
  6137. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  6138. h->s.dsp.h264_v_loop_filter_luma(pix, stride, alpha, beta, tc);
  6139. } else {
  6140. /* 16px edge length, see filter_mb_edgev */
  6141. for( d = 0; d < 16; d++ ) {
  6142. const int p0 = pix[-1*pix_next];
  6143. const int p1 = pix[-2*pix_next];
  6144. const int p2 = pix[-3*pix_next];
  6145. const int q0 = pix[0];
  6146. const int q1 = pix[1*pix_next];
  6147. const int q2 = pix[2*pix_next];
  6148. if( FFABS( p0 - q0 ) < alpha &&
  6149. FFABS( p1 - p0 ) < beta &&
  6150. FFABS( q1 - q0 ) < beta ) {
  6151. const int p3 = pix[-4*pix_next];
  6152. const int q3 = pix[ 3*pix_next];
  6153. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  6154. if( FFABS( p2 - p0 ) < beta) {
  6155. /* p0', p1', p2' */
  6156. pix[-1*pix_next] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  6157. pix[-2*pix_next] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  6158. pix[-3*pix_next] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  6159. } else {
  6160. /* p0' */
  6161. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6162. }
  6163. if( FFABS( q2 - q0 ) < beta) {
  6164. /* q0', q1', q2' */
  6165. pix[0*pix_next] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  6166. pix[1*pix_next] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  6167. pix[2*pix_next] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  6168. } else {
  6169. /* q0' */
  6170. pix[0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6171. }
  6172. }else{
  6173. /* p0', q0' */
  6174. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6175. pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6176. }
  6177. 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]);
  6178. }
  6179. pix++;
  6180. }
  6181. }
  6182. }
  6183. static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  6184. int i;
  6185. const int index_a = qp + h->slice_alpha_c0_offset;
  6186. const int alpha = (alpha_table+52)[index_a];
  6187. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  6188. if( bS[0] < 4 ) {
  6189. int8_t tc[4];
  6190. for(i=0; i<4; i++)
  6191. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  6192. h->s.dsp.h264_v_loop_filter_chroma(pix, stride, alpha, beta, tc);
  6193. } else {
  6194. h->s.dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);
  6195. }
  6196. }
  6197. 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) {
  6198. MpegEncContext * const s = &h->s;
  6199. int mb_xy, mb_type;
  6200. int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh;
  6201. if(mb_x==0 || mb_y==0 || !s->dsp.h264_loop_filter_strength) {
  6202. filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);
  6203. return;
  6204. }
  6205. assert(!FRAME_MBAFF);
  6206. mb_xy = mb_x + mb_y*s->mb_stride;
  6207. mb_type = s->current_picture.mb_type[mb_xy];
  6208. qp = s->current_picture.qscale_table[mb_xy];
  6209. qp0 = s->current_picture.qscale_table[mb_xy-1];
  6210. qp1 = s->current_picture.qscale_table[h->top_mb_xy];
  6211. qpc = get_chroma_qp( h->pps.chroma_qp_index_offset, qp );
  6212. qpc0 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp0 );
  6213. qpc1 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp1 );
  6214. qp0 = (qp + qp0 + 1) >> 1;
  6215. qp1 = (qp + qp1 + 1) >> 1;
  6216. qpc0 = (qpc + qpc0 + 1) >> 1;
  6217. qpc1 = (qpc + qpc1 + 1) >> 1;
  6218. qp_thresh = 15 - h->slice_alpha_c0_offset;
  6219. if(qp <= qp_thresh && qp0 <= qp_thresh && qp1 <= qp_thresh &&
  6220. qpc <= qp_thresh && qpc0 <= qp_thresh && qpc1 <= qp_thresh)
  6221. return;
  6222. if( IS_INTRA(mb_type) ) {
  6223. int16_t bS4[4] = {4,4,4,4};
  6224. int16_t bS3[4] = {3,3,3,3};
  6225. if( IS_8x8DCT(mb_type) ) {
  6226. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  6227. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  6228. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  6229. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  6230. } else {
  6231. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  6232. filter_mb_edgev( h, &img_y[4*1], linesize, bS3, qp );
  6233. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  6234. filter_mb_edgev( h, &img_y[4*3], linesize, bS3, qp );
  6235. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  6236. filter_mb_edgeh( h, &img_y[4*1*linesize], linesize, bS3, qp );
  6237. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  6238. filter_mb_edgeh( h, &img_y[4*3*linesize], linesize, bS3, qp );
  6239. }
  6240. filter_mb_edgecv( h, &img_cb[2*0], uvlinesize, bS4, qpc0 );
  6241. filter_mb_edgecv( h, &img_cb[2*2], uvlinesize, bS3, qpc );
  6242. filter_mb_edgecv( h, &img_cr[2*0], uvlinesize, bS4, qpc0 );
  6243. filter_mb_edgecv( h, &img_cr[2*2], uvlinesize, bS3, qpc );
  6244. filter_mb_edgech( h, &img_cb[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  6245. filter_mb_edgech( h, &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc );
  6246. filter_mb_edgech( h, &img_cr[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  6247. filter_mb_edgech( h, &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc );
  6248. return;
  6249. } else {
  6250. DECLARE_ALIGNED_8(int16_t, bS[2][4][4]);
  6251. uint64_t (*bSv)[4] = (uint64_t(*)[4])bS;
  6252. int edges;
  6253. if( IS_8x8DCT(mb_type) && (h->cbp&7) == 7 ) {
  6254. edges = 4;
  6255. bSv[0][0] = bSv[0][2] = bSv[1][0] = bSv[1][2] = 0x0002000200020002ULL;
  6256. } else {
  6257. int mask_edge1 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 :
  6258. (mb_type & MB_TYPE_16x8) ? 1 : 0;
  6259. int mask_edge0 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16))
  6260. && (s->current_picture.mb_type[mb_xy-1] & (MB_TYPE_16x16 | MB_TYPE_8x16))
  6261. ? 3 : 0;
  6262. int step = IS_8x8DCT(mb_type) ? 2 : 1;
  6263. edges = (mb_type & MB_TYPE_16x16) && !(h->cbp & 15) ? 1 : 4;
  6264. s->dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,
  6265. (h->slice_type == B_TYPE), edges, step, mask_edge0, mask_edge1 );
  6266. }
  6267. if( IS_INTRA(s->current_picture.mb_type[mb_xy-1]) )
  6268. bSv[0][0] = 0x0004000400040004ULL;
  6269. if( IS_INTRA(s->current_picture.mb_type[h->top_mb_xy]) )
  6270. bSv[1][0] = 0x0004000400040004ULL;
  6271. #define FILTER(hv,dir,edge)\
  6272. if(bSv[dir][edge]) {\
  6273. filter_mb_edge##hv( h, &img_y[4*edge*(dir?linesize:1)], linesize, bS[dir][edge], edge ? qp : qp##dir );\
  6274. if(!(edge&1)) {\
  6275. filter_mb_edgec##hv( h, &img_cb[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  6276. filter_mb_edgec##hv( h, &img_cr[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  6277. }\
  6278. }
  6279. if( edges == 1 ) {
  6280. FILTER(v,0,0);
  6281. FILTER(h,1,0);
  6282. } else if( IS_8x8DCT(mb_type) ) {
  6283. FILTER(v,0,0);
  6284. FILTER(v,0,2);
  6285. FILTER(h,1,0);
  6286. FILTER(h,1,2);
  6287. } else {
  6288. FILTER(v,0,0);
  6289. FILTER(v,0,1);
  6290. FILTER(v,0,2);
  6291. FILTER(v,0,3);
  6292. FILTER(h,1,0);
  6293. FILTER(h,1,1);
  6294. FILTER(h,1,2);
  6295. FILTER(h,1,3);
  6296. }
  6297. #undef FILTER
  6298. }
  6299. }
  6300. 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) {
  6301. MpegEncContext * const s = &h->s;
  6302. const int mb_xy= mb_x + mb_y*s->mb_stride;
  6303. const int mb_type = s->current_picture.mb_type[mb_xy];
  6304. const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4;
  6305. int first_vertical_edge_done = 0;
  6306. int dir;
  6307. /* FIXME: A given frame may occupy more than one position in
  6308. * the reference list. So ref2frm should be populated with
  6309. * frame numbers, not indices. */
  6310. static const int ref2frm[34] = {-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  6311. 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
  6312. //for sufficiently low qp, filtering wouldn't do anything
  6313. //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
  6314. if(!FRAME_MBAFF){
  6315. int qp_thresh = 15 - h->slice_alpha_c0_offset - FFMAX(0, h->pps.chroma_qp_index_offset);
  6316. int qp = s->current_picture.qscale_table[mb_xy];
  6317. if(qp <= qp_thresh
  6318. && (mb_x == 0 || ((qp + s->current_picture.qscale_table[mb_xy-1] + 1)>>1) <= qp_thresh)
  6319. && (mb_y == 0 || ((qp + s->current_picture.qscale_table[h->top_mb_xy] + 1)>>1) <= qp_thresh)){
  6320. return;
  6321. }
  6322. }
  6323. if (FRAME_MBAFF
  6324. // left mb is in picture
  6325. && h->slice_table[mb_xy-1] != 255
  6326. // and current and left pair do not have the same interlaced type
  6327. && (IS_INTERLACED(mb_type) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
  6328. // and left mb is in the same slice if deblocking_filter == 2
  6329. && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
  6330. /* First vertical edge is different in MBAFF frames
  6331. * There are 8 different bS to compute and 2 different Qp
  6332. */
  6333. const int pair_xy = mb_x + (mb_y&~1)*s->mb_stride;
  6334. const int left_mb_xy[2] = { pair_xy-1, pair_xy-1+s->mb_stride };
  6335. int16_t bS[8];
  6336. int qp[2];
  6337. int chroma_qp[2];
  6338. int mb_qp, mbn0_qp, mbn1_qp;
  6339. int i;
  6340. first_vertical_edge_done = 1;
  6341. if( IS_INTRA(mb_type) )
  6342. bS[0] = bS[1] = bS[2] = bS[3] = bS[4] = bS[5] = bS[6] = bS[7] = 4;
  6343. else {
  6344. for( i = 0; i < 8; i++ ) {
  6345. int mbn_xy = MB_FIELD ? left_mb_xy[i>>2] : left_mb_xy[i&1];
  6346. if( IS_INTRA( s->current_picture.mb_type[mbn_xy] ) )
  6347. bS[i] = 4;
  6348. else if( h->non_zero_count_cache[12+8*(i>>1)] != 0 ||
  6349. /* FIXME: with 8x8dct + cavlc, should check cbp instead of nnz */
  6350. h->non_zero_count[mbn_xy][MB_FIELD ? i&3 : (i>>2)+(mb_y&1)*2] )
  6351. bS[i] = 2;
  6352. else
  6353. bS[i] = 1;
  6354. }
  6355. }
  6356. mb_qp = s->current_picture.qscale_table[mb_xy];
  6357. mbn0_qp = s->current_picture.qscale_table[left_mb_xy[0]];
  6358. mbn1_qp = s->current_picture.qscale_table[left_mb_xy[1]];
  6359. qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1;
  6360. chroma_qp[0] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6361. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn0_qp ) + 1 ) >> 1;
  6362. qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1;
  6363. chroma_qp[1] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6364. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn1_qp ) + 1 ) >> 1;
  6365. /* Filter edge */
  6366. 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);
  6367. { int i; for (i = 0; i < 8; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6368. filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp );
  6369. filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, chroma_qp );
  6370. filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, chroma_qp );
  6371. }
  6372. /* dir : 0 -> vertical edge, 1 -> horizontal edge */
  6373. for( dir = 0; dir < 2; dir++ )
  6374. {
  6375. int edge;
  6376. const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
  6377. const int mbm_type = s->current_picture.mb_type[mbm_xy];
  6378. int start = h->slice_table[mbm_xy] == 255 ? 1 : 0;
  6379. const int edges = (mb_type & (MB_TYPE_16x16|MB_TYPE_SKIP))
  6380. == (MB_TYPE_16x16|MB_TYPE_SKIP) ? 1 : 4;
  6381. // how often to recheck mv-based bS when iterating between edges
  6382. const int mask_edge = (mb_type & (MB_TYPE_16x16 | (MB_TYPE_16x8 << dir))) ? 3 :
  6383. (mb_type & (MB_TYPE_8x16 >> dir)) ? 1 : 0;
  6384. // how often to recheck mv-based bS when iterating along each edge
  6385. const int mask_par0 = mb_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir));
  6386. if (first_vertical_edge_done) {
  6387. start = 1;
  6388. first_vertical_edge_done = 0;
  6389. }
  6390. if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
  6391. start = 1;
  6392. if (FRAME_MBAFF && (dir == 1) && ((mb_y&1) == 0) && start == 0
  6393. && !IS_INTERLACED(mb_type)
  6394. && IS_INTERLACED(mbm_type)
  6395. ) {
  6396. // This is a special case in the norm where the filtering must
  6397. // be done twice (one each of the field) even if we are in a
  6398. // frame macroblock.
  6399. //
  6400. static const int nnz_idx[4] = {4,5,6,3};
  6401. unsigned int tmp_linesize = 2 * linesize;
  6402. unsigned int tmp_uvlinesize = 2 * uvlinesize;
  6403. int mbn_xy = mb_xy - 2 * s->mb_stride;
  6404. int qp, chroma_qp;
  6405. int i, j;
  6406. int16_t bS[4];
  6407. for(j=0; j<2; j++, mbn_xy += s->mb_stride){
  6408. if( IS_INTRA(mb_type) ||
  6409. IS_INTRA(s->current_picture.mb_type[mbn_xy]) ) {
  6410. bS[0] = bS[1] = bS[2] = bS[3] = 3;
  6411. } else {
  6412. const uint8_t *mbn_nnz = h->non_zero_count[mbn_xy];
  6413. for( i = 0; i < 4; i++ ) {
  6414. if( h->non_zero_count_cache[scan8[0]+i] != 0 ||
  6415. mbn_nnz[nnz_idx[i]] != 0 )
  6416. bS[i] = 2;
  6417. else
  6418. bS[i] = 1;
  6419. }
  6420. }
  6421. // Do not use s->qscale as luma quantizer because it has not the same
  6422. // value in IPCM macroblocks.
  6423. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6424. 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);
  6425. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6426. filter_mb_edgeh( h, &img_y[j*linesize], tmp_linesize, bS, qp );
  6427. chroma_qp = ( h->chroma_qp +
  6428. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6429. filter_mb_edgech( h, &img_cb[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6430. filter_mb_edgech( h, &img_cr[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6431. }
  6432. start = 1;
  6433. }
  6434. /* Calculate bS */
  6435. for( edge = start; edge < edges; edge++ ) {
  6436. /* mbn_xy: neighbor macroblock */
  6437. const int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
  6438. const int mbn_type = s->current_picture.mb_type[mbn_xy];
  6439. int16_t bS[4];
  6440. int qp;
  6441. if( (edge&1) && IS_8x8DCT(mb_type) )
  6442. continue;
  6443. if( IS_INTRA(mb_type) ||
  6444. IS_INTRA(mbn_type) ) {
  6445. int value;
  6446. if (edge == 0) {
  6447. if ( (!IS_INTERLACED(mb_type) && !IS_INTERLACED(mbm_type))
  6448. || ((FRAME_MBAFF || (s->picture_structure != PICT_FRAME)) && (dir == 0))
  6449. ) {
  6450. value = 4;
  6451. } else {
  6452. value = 3;
  6453. }
  6454. } else {
  6455. value = 3;
  6456. }
  6457. bS[0] = bS[1] = bS[2] = bS[3] = value;
  6458. } else {
  6459. int i, l;
  6460. int mv_done;
  6461. if( edge & mask_edge ) {
  6462. bS[0] = bS[1] = bS[2] = bS[3] = 0;
  6463. mv_done = 1;
  6464. }
  6465. else if( FRAME_MBAFF && IS_INTERLACED(mb_type ^ mbn_type)) {
  6466. bS[0] = bS[1] = bS[2] = bS[3] = 1;
  6467. mv_done = 1;
  6468. }
  6469. else if( mask_par0 && (edge || (mbn_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir)))) ) {
  6470. int b_idx= 8 + 4 + edge * (dir ? 8:1);
  6471. int bn_idx= b_idx - (dir ? 8:1);
  6472. int v = 0;
  6473. for( l = 0; !v && l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6474. v |= ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6475. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6476. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit;
  6477. }
  6478. bS[0] = bS[1] = bS[2] = bS[3] = v;
  6479. mv_done = 1;
  6480. }
  6481. else
  6482. mv_done = 0;
  6483. for( i = 0; i < 4; i++ ) {
  6484. int x = dir == 0 ? edge : i;
  6485. int y = dir == 0 ? i : edge;
  6486. int b_idx= 8 + 4 + x + 8*y;
  6487. int bn_idx= b_idx - (dir ? 8:1);
  6488. if( h->non_zero_count_cache[b_idx] != 0 ||
  6489. h->non_zero_count_cache[bn_idx] != 0 ) {
  6490. bS[i] = 2;
  6491. }
  6492. else if(!mv_done)
  6493. {
  6494. bS[i] = 0;
  6495. for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6496. if( ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6497. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6498. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit ) {
  6499. bS[i] = 1;
  6500. break;
  6501. }
  6502. }
  6503. }
  6504. }
  6505. if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
  6506. continue;
  6507. }
  6508. /* Filter edge */
  6509. // Do not use s->qscale as luma quantizer because it has not the same
  6510. // value in IPCM macroblocks.
  6511. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6512. //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]);
  6513. tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
  6514. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6515. if( dir == 0 ) {
  6516. filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
  6517. if( (edge&1) == 0 ) {
  6518. int chroma_qp = ( h->chroma_qp +
  6519. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6520. filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp );
  6521. filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp );
  6522. }
  6523. } else {
  6524. filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
  6525. if( (edge&1) == 0 ) {
  6526. int chroma_qp = ( h->chroma_qp +
  6527. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6528. filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6529. filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6530. }
  6531. }
  6532. }
  6533. }
  6534. }
  6535. static int decode_slice(H264Context *h){
  6536. MpegEncContext * const s = &h->s;
  6537. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  6538. s->mb_skip_run= -1;
  6539. if( h->pps.cabac ) {
  6540. int i;
  6541. /* realign */
  6542. align_get_bits( &s->gb );
  6543. /* init cabac */
  6544. ff_init_cabac_states( &h->cabac);
  6545. ff_init_cabac_decoder( &h->cabac,
  6546. s->gb.buffer + get_bits_count(&s->gb)/8,
  6547. ( s->gb.size_in_bits - get_bits_count(&s->gb) + 7)/8);
  6548. /* calculate pre-state */
  6549. for( i= 0; i < 460; i++ ) {
  6550. int pre;
  6551. if( h->slice_type == I_TYPE )
  6552. pre = clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
  6553. else
  6554. 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 );
  6555. if( pre <= 63 )
  6556. h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
  6557. else
  6558. h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
  6559. }
  6560. for(;;){
  6561. //START_TIMER
  6562. int ret = decode_mb_cabac(h);
  6563. int eos;
  6564. //STOP_TIMER("decode_mb_cabac")
  6565. if(ret>=0) hl_decode_mb(h);
  6566. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  6567. s->mb_y++;
  6568. if(ret>=0) ret = decode_mb_cabac(h);
  6569. if(ret>=0) hl_decode_mb(h);
  6570. s->mb_y--;
  6571. }
  6572. eos = get_cabac_terminate( &h->cabac );
  6573. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  6574. 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);
  6575. 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);
  6576. return -1;
  6577. }
  6578. if( ++s->mb_x >= s->mb_width ) {
  6579. s->mb_x = 0;
  6580. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6581. ++s->mb_y;
  6582. if(FRAME_MBAFF) {
  6583. ++s->mb_y;
  6584. }
  6585. }
  6586. if( eos || s->mb_y >= s->mb_height ) {
  6587. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6588. 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);
  6589. return 0;
  6590. }
  6591. }
  6592. } else {
  6593. for(;;){
  6594. int ret = decode_mb_cavlc(h);
  6595. if(ret>=0) hl_decode_mb(h);
  6596. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  6597. s->mb_y++;
  6598. ret = decode_mb_cavlc(h);
  6599. if(ret>=0) hl_decode_mb(h);
  6600. s->mb_y--;
  6601. }
  6602. if(ret<0){
  6603. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6604. 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);
  6605. return -1;
  6606. }
  6607. if(++s->mb_x >= s->mb_width){
  6608. s->mb_x=0;
  6609. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6610. ++s->mb_y;
  6611. if(FRAME_MBAFF) {
  6612. ++s->mb_y;
  6613. }
  6614. if(s->mb_y >= s->mb_height){
  6615. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6616. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  6617. 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);
  6618. return 0;
  6619. }else{
  6620. 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);
  6621. return -1;
  6622. }
  6623. }
  6624. }
  6625. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  6626. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6627. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  6628. 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);
  6629. return 0;
  6630. }else{
  6631. 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);
  6632. return -1;
  6633. }
  6634. }
  6635. }
  6636. }
  6637. #if 0
  6638. for(;s->mb_y < s->mb_height; s->mb_y++){
  6639. for(;s->mb_x < s->mb_width; s->mb_x++){
  6640. int ret= decode_mb(h);
  6641. hl_decode_mb(h);
  6642. if(ret<0){
  6643. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6644. 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);
  6645. return -1;
  6646. }
  6647. if(++s->mb_x >= s->mb_width){
  6648. s->mb_x=0;
  6649. if(++s->mb_y >= s->mb_height){
  6650. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6651. 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);
  6652. return 0;
  6653. }else{
  6654. 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);
  6655. return -1;
  6656. }
  6657. }
  6658. }
  6659. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  6660. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6661. 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);
  6662. return 0;
  6663. }else{
  6664. 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);
  6665. return -1;
  6666. }
  6667. }
  6668. }
  6669. s->mb_x=0;
  6670. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6671. }
  6672. #endif
  6673. return -1; //not reached
  6674. }
  6675. static int decode_unregistered_user_data(H264Context *h, int size){
  6676. MpegEncContext * const s = &h->s;
  6677. uint8_t user_data[16+256];
  6678. int e, build, i;
  6679. if(size<16)
  6680. return -1;
  6681. for(i=0; i<sizeof(user_data)-1 && i<size; i++){
  6682. user_data[i]= get_bits(&s->gb, 8);
  6683. }
  6684. user_data[i]= 0;
  6685. e= sscanf(user_data+16, "x264 - core %d"/*%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html*/, &build);
  6686. if(e==1 && build>=0)
  6687. h->x264_build= build;
  6688. if(s->avctx->debug & FF_DEBUG_BUGS)
  6689. av_log(s->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data+16);
  6690. for(; i<size; i++)
  6691. skip_bits(&s->gb, 8);
  6692. return 0;
  6693. }
  6694. static int decode_sei(H264Context *h){
  6695. MpegEncContext * const s = &h->s;
  6696. while(get_bits_count(&s->gb) + 16 < s->gb.size_in_bits){
  6697. int size, type;
  6698. type=0;
  6699. do{
  6700. type+= show_bits(&s->gb, 8);
  6701. }while(get_bits(&s->gb, 8) == 255);
  6702. size=0;
  6703. do{
  6704. size+= show_bits(&s->gb, 8);
  6705. }while(get_bits(&s->gb, 8) == 255);
  6706. switch(type){
  6707. case 5:
  6708. if(decode_unregistered_user_data(h, size) < 0)
  6709. return -1;
  6710. break;
  6711. default:
  6712. skip_bits(&s->gb, 8*size);
  6713. }
  6714. //FIXME check bits here
  6715. align_get_bits(&s->gb);
  6716. }
  6717. return 0;
  6718. }
  6719. static inline void decode_hrd_parameters(H264Context *h, SPS *sps){
  6720. MpegEncContext * const s = &h->s;
  6721. int cpb_count, i;
  6722. cpb_count = get_ue_golomb(&s->gb) + 1;
  6723. get_bits(&s->gb, 4); /* bit_rate_scale */
  6724. get_bits(&s->gb, 4); /* cpb_size_scale */
  6725. for(i=0; i<cpb_count; i++){
  6726. get_ue_golomb(&s->gb); /* bit_rate_value_minus1 */
  6727. get_ue_golomb(&s->gb); /* cpb_size_value_minus1 */
  6728. get_bits1(&s->gb); /* cbr_flag */
  6729. }
  6730. get_bits(&s->gb, 5); /* initial_cpb_removal_delay_length_minus1 */
  6731. get_bits(&s->gb, 5); /* cpb_removal_delay_length_minus1 */
  6732. get_bits(&s->gb, 5); /* dpb_output_delay_length_minus1 */
  6733. get_bits(&s->gb, 5); /* time_offset_length */
  6734. }
  6735. static inline int decode_vui_parameters(H264Context *h, SPS *sps){
  6736. MpegEncContext * const s = &h->s;
  6737. int aspect_ratio_info_present_flag;
  6738. unsigned int aspect_ratio_idc;
  6739. int nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag;
  6740. aspect_ratio_info_present_flag= get_bits1(&s->gb);
  6741. if( aspect_ratio_info_present_flag ) {
  6742. aspect_ratio_idc= get_bits(&s->gb, 8);
  6743. if( aspect_ratio_idc == EXTENDED_SAR ) {
  6744. sps->sar.num= get_bits(&s->gb, 16);
  6745. sps->sar.den= get_bits(&s->gb, 16);
  6746. }else if(aspect_ratio_idc < 14){
  6747. sps->sar= pixel_aspect[aspect_ratio_idc];
  6748. }else{
  6749. av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
  6750. return -1;
  6751. }
  6752. }else{
  6753. sps->sar.num=
  6754. sps->sar.den= 0;
  6755. }
  6756. // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
  6757. if(get_bits1(&s->gb)){ /* overscan_info_present_flag */
  6758. get_bits1(&s->gb); /* overscan_appropriate_flag */
  6759. }
  6760. if(get_bits1(&s->gb)){ /* video_signal_type_present_flag */
  6761. get_bits(&s->gb, 3); /* video_format */
  6762. get_bits1(&s->gb); /* video_full_range_flag */
  6763. if(get_bits1(&s->gb)){ /* colour_description_present_flag */
  6764. get_bits(&s->gb, 8); /* colour_primaries */
  6765. get_bits(&s->gb, 8); /* transfer_characteristics */
  6766. get_bits(&s->gb, 8); /* matrix_coefficients */
  6767. }
  6768. }
  6769. if(get_bits1(&s->gb)){ /* chroma_location_info_present_flag */
  6770. get_ue_golomb(&s->gb); /* chroma_sample_location_type_top_field */
  6771. get_ue_golomb(&s->gb); /* chroma_sample_location_type_bottom_field */
  6772. }
  6773. sps->timing_info_present_flag = get_bits1(&s->gb);
  6774. if(sps->timing_info_present_flag){
  6775. sps->num_units_in_tick = get_bits_long(&s->gb, 32);
  6776. sps->time_scale = get_bits_long(&s->gb, 32);
  6777. sps->fixed_frame_rate_flag = get_bits1(&s->gb);
  6778. }
  6779. nal_hrd_parameters_present_flag = get_bits1(&s->gb);
  6780. if(nal_hrd_parameters_present_flag)
  6781. decode_hrd_parameters(h, sps);
  6782. vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
  6783. if(vcl_hrd_parameters_present_flag)
  6784. decode_hrd_parameters(h, sps);
  6785. if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag)
  6786. get_bits1(&s->gb); /* low_delay_hrd_flag */
  6787. get_bits1(&s->gb); /* pic_struct_present_flag */
  6788. sps->bitstream_restriction_flag = get_bits1(&s->gb);
  6789. if(sps->bitstream_restriction_flag){
  6790. unsigned int num_reorder_frames;
  6791. get_bits1(&s->gb); /* motion_vectors_over_pic_boundaries_flag */
  6792. get_ue_golomb(&s->gb); /* max_bytes_per_pic_denom */
  6793. get_ue_golomb(&s->gb); /* max_bits_per_mb_denom */
  6794. get_ue_golomb(&s->gb); /* log2_max_mv_length_horizontal */
  6795. get_ue_golomb(&s->gb); /* log2_max_mv_length_vertical */
  6796. num_reorder_frames= get_ue_golomb(&s->gb);
  6797. get_ue_golomb(&s->gb); /*max_dec_frame_buffering*/
  6798. if(num_reorder_frames > 16 /*max_dec_frame_buffering || max_dec_frame_buffering > 16*/){
  6799. av_log(h->s.avctx, AV_LOG_ERROR, "illegal num_reorder_frames %d\n", num_reorder_frames);
  6800. return -1;
  6801. }
  6802. sps->num_reorder_frames= num_reorder_frames;
  6803. }
  6804. return 0;
  6805. }
  6806. static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,
  6807. const uint8_t *jvt_list, const uint8_t *fallback_list){
  6808. MpegEncContext * const s = &h->s;
  6809. int i, last = 8, next = 8;
  6810. const uint8_t *scan = size == 16 ? zigzag_scan : zigzag_scan8x8;
  6811. if(!get_bits1(&s->gb)) /* matrix not written, we use the predicted one */
  6812. memcpy(factors, fallback_list, size*sizeof(uint8_t));
  6813. else
  6814. for(i=0;i<size;i++){
  6815. if(next)
  6816. next = (last + get_se_golomb(&s->gb)) & 0xff;
  6817. if(!i && !next){ /* matrix not written, we use the preset one */
  6818. memcpy(factors, jvt_list, size*sizeof(uint8_t));
  6819. break;
  6820. }
  6821. last = factors[scan[i]] = next ? next : last;
  6822. }
  6823. }
  6824. static void decode_scaling_matrices(H264Context *h, SPS *sps, PPS *pps, int is_sps,
  6825. uint8_t (*scaling_matrix4)[16], uint8_t (*scaling_matrix8)[64]){
  6826. MpegEncContext * const s = &h->s;
  6827. int fallback_sps = !is_sps && sps->scaling_matrix_present;
  6828. const uint8_t *fallback[4] = {
  6829. fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
  6830. fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
  6831. fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
  6832. fallback_sps ? sps->scaling_matrix8[1] : default_scaling8[1]
  6833. };
  6834. if(get_bits1(&s->gb)){
  6835. sps->scaling_matrix_present |= is_sps;
  6836. decode_scaling_list(h,scaling_matrix4[0],16,default_scaling4[0],fallback[0]); // Intra, Y
  6837. decode_scaling_list(h,scaling_matrix4[1],16,default_scaling4[0],scaling_matrix4[0]); // Intra, Cr
  6838. decode_scaling_list(h,scaling_matrix4[2],16,default_scaling4[0],scaling_matrix4[1]); // Intra, Cb
  6839. decode_scaling_list(h,scaling_matrix4[3],16,default_scaling4[1],fallback[1]); // Inter, Y
  6840. decode_scaling_list(h,scaling_matrix4[4],16,default_scaling4[1],scaling_matrix4[3]); // Inter, Cr
  6841. decode_scaling_list(h,scaling_matrix4[5],16,default_scaling4[1],scaling_matrix4[4]); // Inter, Cb
  6842. if(is_sps || pps->transform_8x8_mode){
  6843. decode_scaling_list(h,scaling_matrix8[0],64,default_scaling8[0],fallback[2]); // Intra, Y
  6844. decode_scaling_list(h,scaling_matrix8[1],64,default_scaling8[1],fallback[3]); // Inter, Y
  6845. }
  6846. } else if(fallback_sps) {
  6847. memcpy(scaling_matrix4, sps->scaling_matrix4, 6*16*sizeof(uint8_t));
  6848. memcpy(scaling_matrix8, sps->scaling_matrix8, 2*64*sizeof(uint8_t));
  6849. }
  6850. }
  6851. static inline int decode_seq_parameter_set(H264Context *h){
  6852. MpegEncContext * const s = &h->s;
  6853. int profile_idc, level_idc;
  6854. unsigned int sps_id, tmp, mb_width, mb_height;
  6855. int i;
  6856. SPS *sps;
  6857. profile_idc= get_bits(&s->gb, 8);
  6858. get_bits1(&s->gb); //constraint_set0_flag
  6859. get_bits1(&s->gb); //constraint_set1_flag
  6860. get_bits1(&s->gb); //constraint_set2_flag
  6861. get_bits1(&s->gb); //constraint_set3_flag
  6862. get_bits(&s->gb, 4); // reserved
  6863. level_idc= get_bits(&s->gb, 8);
  6864. sps_id= get_ue_golomb(&s->gb);
  6865. if (sps_id >= MAX_SPS_COUNT){
  6866. // ok it has gone out of hand, someone is sending us bad stuff.
  6867. av_log(h->s.avctx, AV_LOG_ERROR, "illegal sps_id (%d)\n", sps_id);
  6868. return -1;
  6869. }
  6870. sps= &h->sps_buffer[ sps_id ];
  6871. sps->profile_idc= profile_idc;
  6872. sps->level_idc= level_idc;
  6873. if(sps->profile_idc >= 100){ //high profile
  6874. if(get_ue_golomb(&s->gb) == 3) //chroma_format_idc
  6875. get_bits1(&s->gb); //residual_color_transform_flag
  6876. get_ue_golomb(&s->gb); //bit_depth_luma_minus8
  6877. get_ue_golomb(&s->gb); //bit_depth_chroma_minus8
  6878. sps->transform_bypass = get_bits1(&s->gb);
  6879. decode_scaling_matrices(h, sps, NULL, 1, sps->scaling_matrix4, sps->scaling_matrix8);
  6880. }else
  6881. sps->scaling_matrix_present = 0;
  6882. sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
  6883. sps->poc_type= get_ue_golomb(&s->gb);
  6884. if(sps->poc_type == 0){ //FIXME #define
  6885. sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
  6886. } else if(sps->poc_type == 1){//FIXME #define
  6887. sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
  6888. sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
  6889. sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
  6890. tmp= get_ue_golomb(&s->gb);
  6891. if(tmp >= sizeof(sps->offset_for_ref_frame) / sizeof(sps->offset_for_ref_frame[0])){
  6892. av_log(h->s.avctx, AV_LOG_ERROR, "poc_cycle_length overflow %u\n", tmp);
  6893. return -1;
  6894. }
  6895. sps->poc_cycle_length= tmp;
  6896. for(i=0; i<sps->poc_cycle_length; i++)
  6897. sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
  6898. }else if(sps->poc_type != 2){
  6899. av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
  6900. return -1;
  6901. }
  6902. tmp= get_ue_golomb(&s->gb);
  6903. if(tmp > MAX_PICTURE_COUNT-2){
  6904. av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");
  6905. }
  6906. sps->ref_frame_count= tmp;
  6907. sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
  6908. mb_width= get_ue_golomb(&s->gb) + 1;
  6909. mb_height= get_ue_golomb(&s->gb) + 1;
  6910. if(mb_width >= INT_MAX/16 || mb_height >= INT_MAX/16 ||
  6911. avcodec_check_dimensions(NULL, 16*mb_width, 16*mb_height)){
  6912. av_log(h->s.avctx, AV_LOG_ERROR, "mb_width/height overflow\n");
  6913. return -1;
  6914. }
  6915. sps->mb_width = mb_width;
  6916. sps->mb_height= mb_height;
  6917. sps->frame_mbs_only_flag= get_bits1(&s->gb);
  6918. if(!sps->frame_mbs_only_flag)
  6919. sps->mb_aff= get_bits1(&s->gb);
  6920. else
  6921. sps->mb_aff= 0;
  6922. sps->direct_8x8_inference_flag= get_bits1(&s->gb);
  6923. #ifndef ALLOW_INTERLACE
  6924. if(sps->mb_aff)
  6925. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF support not included; enable it at compile-time.\n");
  6926. #endif
  6927. if(!sps->direct_8x8_inference_flag && sps->mb_aff)
  6928. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + !direct_8x8_inference is not implemented\n");
  6929. sps->crop= get_bits1(&s->gb);
  6930. if(sps->crop){
  6931. sps->crop_left = get_ue_golomb(&s->gb);
  6932. sps->crop_right = get_ue_golomb(&s->gb);
  6933. sps->crop_top = get_ue_golomb(&s->gb);
  6934. sps->crop_bottom= get_ue_golomb(&s->gb);
  6935. if(sps->crop_left || sps->crop_top){
  6936. av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ...\n");
  6937. }
  6938. }else{
  6939. sps->crop_left =
  6940. sps->crop_right =
  6941. sps->crop_top =
  6942. sps->crop_bottom= 0;
  6943. }
  6944. sps->vui_parameters_present_flag= get_bits1(&s->gb);
  6945. if( sps->vui_parameters_present_flag )
  6946. decode_vui_parameters(h, sps);
  6947. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6948. 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",
  6949. sps_id, sps->profile_idc, sps->level_idc,
  6950. sps->poc_type,
  6951. sps->ref_frame_count,
  6952. sps->mb_width, sps->mb_height,
  6953. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  6954. sps->direct_8x8_inference_flag ? "8B8" : "",
  6955. sps->crop_left, sps->crop_right,
  6956. sps->crop_top, sps->crop_bottom,
  6957. sps->vui_parameters_present_flag ? "VUI" : ""
  6958. );
  6959. }
  6960. return 0;
  6961. }
  6962. static inline int decode_picture_parameter_set(H264Context *h, int bit_length){
  6963. MpegEncContext * const s = &h->s;
  6964. unsigned int tmp, pps_id= get_ue_golomb(&s->gb);
  6965. PPS *pps;
  6966. if(pps_id>=MAX_PPS_COUNT){
  6967. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  6968. return -1;
  6969. }
  6970. pps = &h->pps_buffer[pps_id];
  6971. tmp= get_ue_golomb(&s->gb);
  6972. if(tmp>=MAX_SPS_COUNT){
  6973. av_log(h->s.avctx, AV_LOG_ERROR, "sps_id out of range\n");
  6974. return -1;
  6975. }
  6976. pps->sps_id= tmp;
  6977. pps->cabac= get_bits1(&s->gb);
  6978. pps->pic_order_present= get_bits1(&s->gb);
  6979. pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
  6980. if(pps->slice_group_count > 1 ){
  6981. pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
  6982. av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
  6983. switch(pps->mb_slice_group_map_type){
  6984. case 0:
  6985. #if 0
  6986. | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
  6987. | run_length[ i ] |1 |ue(v) |
  6988. #endif
  6989. break;
  6990. case 2:
  6991. #if 0
  6992. | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
  6993. |{ | | |
  6994. | top_left_mb[ i ] |1 |ue(v) |
  6995. | bottom_right_mb[ i ] |1 |ue(v) |
  6996. | } | | |
  6997. #endif
  6998. break;
  6999. case 3:
  7000. case 4:
  7001. case 5:
  7002. #if 0
  7003. | slice_group_change_direction_flag |1 |u(1) |
  7004. | slice_group_change_rate_minus1 |1 |ue(v) |
  7005. #endif
  7006. break;
  7007. case 6:
  7008. #if 0
  7009. | slice_group_id_cnt_minus1 |1 |ue(v) |
  7010. | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
  7011. |) | | |
  7012. | slice_group_id[ i ] |1 |u(v) |
  7013. #endif
  7014. break;
  7015. }
  7016. }
  7017. pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  7018. pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  7019. if(pps->ref_count[0] > 32 || pps->ref_count[1] > 32){
  7020. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
  7021. pps->ref_count[0]= pps->ref_count[1]= 1;
  7022. return -1;
  7023. }
  7024. pps->weighted_pred= get_bits1(&s->gb);
  7025. pps->weighted_bipred_idc= get_bits(&s->gb, 2);
  7026. pps->init_qp= get_se_golomb(&s->gb) + 26;
  7027. pps->init_qs= get_se_golomb(&s->gb) + 26;
  7028. pps->chroma_qp_index_offset= get_se_golomb(&s->gb);
  7029. pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
  7030. pps->constrained_intra_pred= get_bits1(&s->gb);
  7031. pps->redundant_pic_cnt_present = get_bits1(&s->gb);
  7032. pps->transform_8x8_mode= 0;
  7033. h->dequant_coeff_pps= -1; //contents of sps/pps can change even if id doesn't, so reinit
  7034. memset(pps->scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  7035. memset(pps->scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  7036. if(get_bits_count(&s->gb) < bit_length){
  7037. pps->transform_8x8_mode= get_bits1(&s->gb);
  7038. decode_scaling_matrices(h, &h->sps_buffer[pps->sps_id], pps, 0, pps->scaling_matrix4, pps->scaling_matrix8);
  7039. get_se_golomb(&s->gb); //second_chroma_qp_index_offset
  7040. }
  7041. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  7042. 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",
  7043. pps_id, pps->sps_id,
  7044. pps->cabac ? "CABAC" : "CAVLC",
  7045. pps->slice_group_count,
  7046. pps->ref_count[0], pps->ref_count[1],
  7047. pps->weighted_pred ? "weighted" : "",
  7048. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset,
  7049. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  7050. pps->constrained_intra_pred ? "CONSTR" : "",
  7051. pps->redundant_pic_cnt_present ? "REDU" : "",
  7052. pps->transform_8x8_mode ? "8x8DCT" : ""
  7053. );
  7054. }
  7055. return 0;
  7056. }
  7057. /**
  7058. * finds the end of the current frame in the bitstream.
  7059. * @return the position of the first byte of the next frame, or -1
  7060. */
  7061. static int find_frame_end(H264Context *h, const uint8_t *buf, int buf_size){
  7062. int i;
  7063. uint32_t state;
  7064. ParseContext *pc = &(h->s.parse_context);
  7065. //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
  7066. // mb_addr= pc->mb_addr - 1;
  7067. state= pc->state;
  7068. for(i=0; i<=buf_size; i++){
  7069. if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  7070. tprintf("find_frame_end new startcode = %08x, frame_start_found = %d, pos = %d\n", state, pc->frame_start_found, i);
  7071. if(pc->frame_start_found){
  7072. // If there isn't one more byte in the buffer
  7073. // the test on first_mb_in_slice cannot be done yet
  7074. // do it at next call.
  7075. if (i >= buf_size) break;
  7076. if (buf[i] & 0x80) {
  7077. // first_mb_in_slice is 0, probably the first nal of a new
  7078. // slice
  7079. tprintf("find_frame_end frame_end_found, state = %08x, pos = %d\n", state, i);
  7080. pc->state=-1;
  7081. pc->frame_start_found= 0;
  7082. return i-4;
  7083. }
  7084. }
  7085. pc->frame_start_found = 1;
  7086. }
  7087. if((state&0xFFFFFF1F) == 0x107 || (state&0xFFFFFF1F) == 0x108 || (state&0xFFFFFF1F) == 0x109){
  7088. if(pc->frame_start_found){
  7089. pc->state=-1;
  7090. pc->frame_start_found= 0;
  7091. return i-4;
  7092. }
  7093. }
  7094. if (i<buf_size)
  7095. state= (state<<8) | buf[i];
  7096. }
  7097. pc->state= state;
  7098. return END_NOT_FOUND;
  7099. }
  7100. #ifdef CONFIG_H264_PARSER
  7101. static int h264_parse(AVCodecParserContext *s,
  7102. AVCodecContext *avctx,
  7103. uint8_t **poutbuf, int *poutbuf_size,
  7104. const uint8_t *buf, int buf_size)
  7105. {
  7106. H264Context *h = s->priv_data;
  7107. ParseContext *pc = &h->s.parse_context;
  7108. int next;
  7109. next= find_frame_end(h, buf, buf_size);
  7110. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  7111. *poutbuf = NULL;
  7112. *poutbuf_size = 0;
  7113. return buf_size;
  7114. }
  7115. *poutbuf = (uint8_t *)buf;
  7116. *poutbuf_size = buf_size;
  7117. return next;
  7118. }
  7119. static int h264_split(AVCodecContext *avctx,
  7120. const uint8_t *buf, int buf_size)
  7121. {
  7122. int i;
  7123. uint32_t state = -1;
  7124. int has_sps= 0;
  7125. for(i=0; i<=buf_size; i++){
  7126. if((state&0xFFFFFF1F) == 0x107)
  7127. has_sps=1;
  7128. /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  7129. }*/
  7130. if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
  7131. if(has_sps){
  7132. while(i>4 && buf[i-5]==0) i--;
  7133. return i-4;
  7134. }
  7135. }
  7136. if (i<buf_size)
  7137. state= (state<<8) | buf[i];
  7138. }
  7139. return 0;
  7140. }
  7141. #endif /* CONFIG_H264_PARSER */
  7142. static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){
  7143. MpegEncContext * const s = &h->s;
  7144. AVCodecContext * const avctx= s->avctx;
  7145. int buf_index=0;
  7146. #if 0
  7147. int i;
  7148. for(i=0; i<50; i++){
  7149. av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
  7150. }
  7151. #endif
  7152. h->slice_num = 0;
  7153. s->current_picture_ptr= NULL;
  7154. for(;;){
  7155. int consumed;
  7156. int dst_length;
  7157. int bit_length;
  7158. uint8_t *ptr;
  7159. int i, nalsize = 0;
  7160. if(h->is_avc) {
  7161. if(buf_index >= buf_size) break;
  7162. nalsize = 0;
  7163. for(i = 0; i < h->nal_length_size; i++)
  7164. nalsize = (nalsize << 8) | buf[buf_index++];
  7165. if(nalsize <= 1 || nalsize > buf_size){
  7166. if(nalsize == 1){
  7167. buf_index++;
  7168. continue;
  7169. }else{
  7170. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  7171. break;
  7172. }
  7173. }
  7174. } else {
  7175. // start code prefix search
  7176. for(; buf_index + 3 < buf_size; buf_index++){
  7177. // this should allways succeed in the first iteration
  7178. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  7179. break;
  7180. }
  7181. if(buf_index+3 >= buf_size) break;
  7182. buf_index+=3;
  7183. }
  7184. ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, h->is_avc ? nalsize : buf_size - buf_index);
  7185. if (ptr==NULL || dst_length <= 0){
  7186. return -1;
  7187. }
  7188. while(ptr[dst_length - 1] == 0 && dst_length > 1)
  7189. dst_length--;
  7190. bit_length= 8*dst_length - decode_rbsp_trailing(ptr + dst_length - 1);
  7191. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  7192. 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);
  7193. }
  7194. if (h->is_avc && (nalsize != consumed))
  7195. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  7196. buf_index += consumed;
  7197. if( (s->hurry_up == 1 && h->nal_ref_idc == 0) //FIXME dont discard SEI id
  7198. ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  7199. continue;
  7200. switch(h->nal_unit_type){
  7201. case NAL_IDR_SLICE:
  7202. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  7203. case NAL_SLICE:
  7204. init_get_bits(&s->gb, ptr, bit_length);
  7205. h->intra_gb_ptr=
  7206. h->inter_gb_ptr= &s->gb;
  7207. s->data_partitioning = 0;
  7208. if(decode_slice_header(h) < 0){
  7209. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  7210. break;
  7211. }
  7212. s->current_picture_ptr->key_frame= (h->nal_unit_type == NAL_IDR_SLICE);
  7213. if(h->redundant_pic_count==0 && s->hurry_up < 5
  7214. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  7215. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  7216. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  7217. && avctx->skip_frame < AVDISCARD_ALL)
  7218. decode_slice(h);
  7219. break;
  7220. case NAL_DPA:
  7221. init_get_bits(&s->gb, ptr, bit_length);
  7222. h->intra_gb_ptr=
  7223. h->inter_gb_ptr= NULL;
  7224. s->data_partitioning = 1;
  7225. if(decode_slice_header(h) < 0){
  7226. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  7227. }
  7228. break;
  7229. case NAL_DPB:
  7230. init_get_bits(&h->intra_gb, ptr, bit_length);
  7231. h->intra_gb_ptr= &h->intra_gb;
  7232. break;
  7233. case NAL_DPC:
  7234. init_get_bits(&h->inter_gb, ptr, bit_length);
  7235. h->inter_gb_ptr= &h->inter_gb;
  7236. if(h->redundant_pic_count==0 && h->intra_gb_ptr && s->data_partitioning
  7237. && s->context_initialized
  7238. && s->hurry_up < 5
  7239. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  7240. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  7241. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  7242. && avctx->skip_frame < AVDISCARD_ALL)
  7243. decode_slice(h);
  7244. break;
  7245. case NAL_SEI:
  7246. init_get_bits(&s->gb, ptr, bit_length);
  7247. decode_sei(h);
  7248. break;
  7249. case NAL_SPS:
  7250. init_get_bits(&s->gb, ptr, bit_length);
  7251. decode_seq_parameter_set(h);
  7252. if(s->flags& CODEC_FLAG_LOW_DELAY)
  7253. s->low_delay=1;
  7254. if(avctx->has_b_frames < 2)
  7255. avctx->has_b_frames= !s->low_delay;
  7256. break;
  7257. case NAL_PPS:
  7258. init_get_bits(&s->gb, ptr, bit_length);
  7259. decode_picture_parameter_set(h, bit_length);
  7260. break;
  7261. case NAL_AUD:
  7262. case NAL_END_SEQUENCE:
  7263. case NAL_END_STREAM:
  7264. case NAL_FILLER_DATA:
  7265. case NAL_SPS_EXT:
  7266. case NAL_AUXILIARY_SLICE:
  7267. break;
  7268. default:
  7269. av_log(avctx, AV_LOG_ERROR, "Unknown NAL code: %d\n", h->nal_unit_type);
  7270. }
  7271. }
  7272. if(!s->current_picture_ptr) return buf_index; //no frame
  7273. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  7274. s->current_picture_ptr->pict_type= s->pict_type;
  7275. h->prev_frame_num_offset= h->frame_num_offset;
  7276. h->prev_frame_num= h->frame_num;
  7277. if(s->current_picture_ptr->reference){
  7278. h->prev_poc_msb= h->poc_msb;
  7279. h->prev_poc_lsb= h->poc_lsb;
  7280. }
  7281. if(s->current_picture_ptr->reference)
  7282. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  7283. ff_er_frame_end(s);
  7284. MPV_frame_end(s);
  7285. return buf_index;
  7286. }
  7287. /**
  7288. * returns the number of bytes consumed for building the current frame
  7289. */
  7290. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  7291. if(s->flags&CODEC_FLAG_TRUNCATED){
  7292. pos -= s->parse_context.last_index;
  7293. if(pos<0) pos=0; // FIXME remove (unneeded?)
  7294. return pos;
  7295. }else{
  7296. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  7297. if(pos+10>buf_size) pos=buf_size; // oops ;)
  7298. return pos;
  7299. }
  7300. }
  7301. static int decode_frame(AVCodecContext *avctx,
  7302. void *data, int *data_size,
  7303. uint8_t *buf, int buf_size)
  7304. {
  7305. H264Context *h = avctx->priv_data;
  7306. MpegEncContext *s = &h->s;
  7307. AVFrame *pict = data;
  7308. int buf_index;
  7309. s->flags= avctx->flags;
  7310. s->flags2= avctx->flags2;
  7311. /* no supplementary picture */
  7312. if (buf_size == 0) {
  7313. return 0;
  7314. }
  7315. if(s->flags&CODEC_FLAG_TRUNCATED){
  7316. int next= find_frame_end(h, buf, buf_size);
  7317. if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
  7318. return buf_size;
  7319. //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);
  7320. }
  7321. if(h->is_avc && !h->got_avcC) {
  7322. int i, cnt, nalsize;
  7323. unsigned char *p = avctx->extradata;
  7324. if(avctx->extradata_size < 7) {
  7325. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  7326. return -1;
  7327. }
  7328. if(*p != 1) {
  7329. av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
  7330. return -1;
  7331. }
  7332. /* sps and pps in the avcC always have length coded with 2 bytes,
  7333. so put a fake nal_length_size = 2 while parsing them */
  7334. h->nal_length_size = 2;
  7335. // Decode sps from avcC
  7336. cnt = *(p+5) & 0x1f; // Number of sps
  7337. p += 6;
  7338. for (i = 0; i < cnt; i++) {
  7339. nalsize = AV_RB16(p) + 2;
  7340. if(decode_nal_units(h, p, nalsize) < 0) {
  7341. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  7342. return -1;
  7343. }
  7344. p += nalsize;
  7345. }
  7346. // Decode pps from avcC
  7347. cnt = *(p++); // Number of pps
  7348. for (i = 0; i < cnt; i++) {
  7349. nalsize = AV_RB16(p) + 2;
  7350. if(decode_nal_units(h, p, nalsize) != nalsize) {
  7351. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  7352. return -1;
  7353. }
  7354. p += nalsize;
  7355. }
  7356. // Now store right nal length size, that will be use to parse all other nals
  7357. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  7358. // Do not reparse avcC
  7359. h->got_avcC = 1;
  7360. }
  7361. if(avctx->frame_number==0 && !h->is_avc && s->avctx->extradata_size){
  7362. if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
  7363. return -1;
  7364. }
  7365. buf_index=decode_nal_units(h, buf, buf_size);
  7366. if(buf_index < 0)
  7367. return -1;
  7368. //FIXME do something with unavailable reference frames
  7369. // if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_index, buf_size);
  7370. if(!s->current_picture_ptr){
  7371. av_log(h->s.avctx, AV_LOG_DEBUG, "error, NO frame\n");
  7372. return -1;
  7373. }
  7374. {
  7375. Picture *out = s->current_picture_ptr;
  7376. #if 0 //decode order
  7377. *data_size = sizeof(AVFrame);
  7378. #else
  7379. /* Sort B-frames into display order */
  7380. Picture *cur = s->current_picture_ptr;
  7381. Picture *prev = h->delayed_output_pic;
  7382. int i, pics, cross_idr, out_of_order, out_idx;
  7383. if(h->sps.bitstream_restriction_flag
  7384. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  7385. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  7386. s->low_delay = 0;
  7387. }
  7388. pics = 0;
  7389. while(h->delayed_pic[pics]) pics++;
  7390. assert(pics+1 < sizeof(h->delayed_pic) / sizeof(h->delayed_pic[0]));
  7391. h->delayed_pic[pics++] = cur;
  7392. if(cur->reference == 0)
  7393. cur->reference = 1;
  7394. cross_idr = 0;
  7395. for(i=0; h->delayed_pic[i]; i++)
  7396. if(h->delayed_pic[i]->key_frame || h->delayed_pic[i]->poc==0)
  7397. cross_idr = 1;
  7398. out = h->delayed_pic[0];
  7399. out_idx = 0;
  7400. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
  7401. if(h->delayed_pic[i]->poc < out->poc){
  7402. out = h->delayed_pic[i];
  7403. out_idx = i;
  7404. }
  7405. out_of_order = !cross_idr && prev && out->poc < prev->poc;
  7406. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  7407. { }
  7408. else if(prev && pics <= s->avctx->has_b_frames)
  7409. out = prev;
  7410. else if((out_of_order && pics-1 == s->avctx->has_b_frames && pics < 15)
  7411. || (s->low_delay &&
  7412. ((!cross_idr && prev && out->poc > prev->poc + 2)
  7413. || cur->pict_type == B_TYPE)))
  7414. {
  7415. s->low_delay = 0;
  7416. s->avctx->has_b_frames++;
  7417. out = prev;
  7418. }
  7419. else if(out_of_order)
  7420. out = prev;
  7421. if(out_of_order || pics > s->avctx->has_b_frames){
  7422. for(i=out_idx; h->delayed_pic[i]; i++)
  7423. h->delayed_pic[i] = h->delayed_pic[i+1];
  7424. }
  7425. if(prev == out)
  7426. *data_size = 0;
  7427. else
  7428. *data_size = sizeof(AVFrame);
  7429. if(prev && prev != out && prev->reference == 1)
  7430. prev->reference = 0;
  7431. h->delayed_output_pic = out;
  7432. #endif
  7433. if(out)
  7434. *pict= *(AVFrame*)out;
  7435. else
  7436. av_log(avctx, AV_LOG_DEBUG, "no picture\n");
  7437. }
  7438. assert(pict->data[0] || !*data_size);
  7439. ff_print_debug_info(s, pict);
  7440. //printf("out %d\n", (int)pict->data[0]);
  7441. #if 0 //?
  7442. /* Return the Picture timestamp as the frame number */
  7443. /* we substract 1 because it is added on utils.c */
  7444. avctx->frame_number = s->picture_number - 1;
  7445. #endif
  7446. return get_consumed_bytes(s, buf_index, buf_size);
  7447. }
  7448. #if 0
  7449. static inline void fill_mb_avail(H264Context *h){
  7450. MpegEncContext * const s = &h->s;
  7451. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  7452. if(s->mb_y){
  7453. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  7454. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  7455. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  7456. }else{
  7457. h->mb_avail[0]=
  7458. h->mb_avail[1]=
  7459. h->mb_avail[2]= 0;
  7460. }
  7461. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  7462. h->mb_avail[4]= 1; //FIXME move out
  7463. h->mb_avail[5]= 0; //FIXME move out
  7464. }
  7465. #endif
  7466. #if 0 //selftest
  7467. #define COUNT 8000
  7468. #define SIZE (COUNT*40)
  7469. int main(){
  7470. int i;
  7471. uint8_t temp[SIZE];
  7472. PutBitContext pb;
  7473. GetBitContext gb;
  7474. // int int_temp[10000];
  7475. DSPContext dsp;
  7476. AVCodecContext avctx;
  7477. dsputil_init(&dsp, &avctx);
  7478. init_put_bits(&pb, temp, SIZE);
  7479. printf("testing unsigned exp golomb\n");
  7480. for(i=0; i<COUNT; i++){
  7481. START_TIMER
  7482. set_ue_golomb(&pb, i);
  7483. STOP_TIMER("set_ue_golomb");
  7484. }
  7485. flush_put_bits(&pb);
  7486. init_get_bits(&gb, temp, 8*SIZE);
  7487. for(i=0; i<COUNT; i++){
  7488. int j, s;
  7489. s= show_bits(&gb, 24);
  7490. START_TIMER
  7491. j= get_ue_golomb(&gb);
  7492. if(j != i){
  7493. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7494. // return -1;
  7495. }
  7496. STOP_TIMER("get_ue_golomb");
  7497. }
  7498. init_put_bits(&pb, temp, SIZE);
  7499. printf("testing signed exp golomb\n");
  7500. for(i=0; i<COUNT; i++){
  7501. START_TIMER
  7502. set_se_golomb(&pb, i - COUNT/2);
  7503. STOP_TIMER("set_se_golomb");
  7504. }
  7505. flush_put_bits(&pb);
  7506. init_get_bits(&gb, temp, 8*SIZE);
  7507. for(i=0; i<COUNT; i++){
  7508. int j, s;
  7509. s= show_bits(&gb, 24);
  7510. START_TIMER
  7511. j= get_se_golomb(&gb);
  7512. if(j != i - COUNT/2){
  7513. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7514. // return -1;
  7515. }
  7516. STOP_TIMER("get_se_golomb");
  7517. }
  7518. printf("testing 4x4 (I)DCT\n");
  7519. DCTELEM block[16];
  7520. uint8_t src[16], ref[16];
  7521. uint64_t error= 0, max_error=0;
  7522. for(i=0; i<COUNT; i++){
  7523. int j;
  7524. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  7525. for(j=0; j<16; j++){
  7526. ref[j]= random()%255;
  7527. src[j]= random()%255;
  7528. }
  7529. h264_diff_dct_c(block, src, ref, 4);
  7530. //normalize
  7531. for(j=0; j<16; j++){
  7532. // printf("%d ", block[j]);
  7533. block[j]= block[j]*4;
  7534. if(j&1) block[j]= (block[j]*4 + 2)/5;
  7535. if(j&4) block[j]= (block[j]*4 + 2)/5;
  7536. }
  7537. // printf("\n");
  7538. s->dsp.h264_idct_add(ref, block, 4);
  7539. /* for(j=0; j<16; j++){
  7540. printf("%d ", ref[j]);
  7541. }
  7542. printf("\n");*/
  7543. for(j=0; j<16; j++){
  7544. int diff= FFABS(src[j] - ref[j]);
  7545. error+= diff*diff;
  7546. max_error= FFMAX(max_error, diff);
  7547. }
  7548. }
  7549. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  7550. #if 0
  7551. printf("testing quantizer\n");
  7552. for(qp=0; qp<52; qp++){
  7553. for(i=0; i<16; i++)
  7554. src1_block[i]= src2_block[i]= random()%255;
  7555. }
  7556. #endif
  7557. printf("Testing NAL layer\n");
  7558. uint8_t bitstream[COUNT];
  7559. uint8_t nal[COUNT*2];
  7560. H264Context h;
  7561. memset(&h, 0, sizeof(H264Context));
  7562. for(i=0; i<COUNT; i++){
  7563. int zeros= i;
  7564. int nal_length;
  7565. int consumed;
  7566. int out_length;
  7567. uint8_t *out;
  7568. int j;
  7569. for(j=0; j<COUNT; j++){
  7570. bitstream[j]= (random() % 255) + 1;
  7571. }
  7572. for(j=0; j<zeros; j++){
  7573. int pos= random() % COUNT;
  7574. while(bitstream[pos] == 0){
  7575. pos++;
  7576. pos %= COUNT;
  7577. }
  7578. bitstream[pos]=0;
  7579. }
  7580. START_TIMER
  7581. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  7582. if(nal_length<0){
  7583. printf("encoding failed\n");
  7584. return -1;
  7585. }
  7586. out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
  7587. STOP_TIMER("NAL")
  7588. if(out_length != COUNT){
  7589. printf("incorrect length %d %d\n", out_length, COUNT);
  7590. return -1;
  7591. }
  7592. if(consumed != nal_length){
  7593. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  7594. return -1;
  7595. }
  7596. if(memcmp(bitstream, out, COUNT)){
  7597. printf("missmatch\n");
  7598. return -1;
  7599. }
  7600. }
  7601. printf("Testing RBSP\n");
  7602. return 0;
  7603. }
  7604. #endif
  7605. static int decode_end(AVCodecContext *avctx)
  7606. {
  7607. H264Context *h = avctx->priv_data;
  7608. MpegEncContext *s = &h->s;
  7609. av_freep(&h->rbsp_buffer);
  7610. free_tables(h); //FIXME cleanup init stuff perhaps
  7611. MPV_common_end(s);
  7612. // memset(h, 0, sizeof(H264Context));
  7613. return 0;
  7614. }
  7615. AVCodec h264_decoder = {
  7616. "h264",
  7617. CODEC_TYPE_VIDEO,
  7618. CODEC_ID_H264,
  7619. sizeof(H264Context),
  7620. decode_init,
  7621. NULL,
  7622. decode_end,
  7623. decode_frame,
  7624. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  7625. .flush= flush_dpb,
  7626. };
  7627. #ifdef CONFIG_H264_PARSER
  7628. AVCodecParser h264_parser = {
  7629. { CODEC_ID_H264 },
  7630. sizeof(H264Context),
  7631. NULL,
  7632. h264_parse,
  7633. ff_parse_close,
  7634. h264_split,
  7635. };
  7636. #endif
  7637. #include "svq3.c"