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.

8696 lines
325KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. /**
  23. * @file h264.c
  24. * H.264 / AVC / MPEG4 part10 codec.
  25. * @author Michael Niedermayer <michaelni@gmx.at>
  26. */
  27. #include "common.h"
  28. #include "dsputil.h"
  29. #include "avcodec.h"
  30. #include "mpegvideo.h"
  31. #include "h264data.h"
  32. #include "golomb.h"
  33. #include "cabac.h"
  34. //#undef NDEBUG
  35. #include <assert.h>
  36. #define interlaced_dct interlaced_dct_is_a_bad_name
  37. #define mb_intra mb_intra_isnt_initalized_see_mb_type
  38. #define LUMA_DC_BLOCK_INDEX 25
  39. #define CHROMA_DC_BLOCK_INDEX 26
  40. #define CHROMA_DC_COEFF_TOKEN_VLC_BITS 8
  41. #define COEFF_TOKEN_VLC_BITS 8
  42. #define TOTAL_ZEROS_VLC_BITS 9
  43. #define CHROMA_DC_TOTAL_ZEROS_VLC_BITS 3
  44. #define RUN_VLC_BITS 3
  45. #define RUN7_VLC_BITS 6
  46. #define MAX_SPS_COUNT 32
  47. #define MAX_PPS_COUNT 256
  48. #define MAX_MMCO_COUNT 66
  49. /* Compiling in interlaced support reduces the speed
  50. * of progressive decoding by about 2%. */
  51. #define ALLOW_INTERLACE
  52. #ifdef ALLOW_INTERLACE
  53. #define MB_MBAFF h->mb_mbaff
  54. #define MB_FIELD h->mb_field_decoding_flag
  55. #define FRAME_MBAFF h->mb_aff_frame
  56. #else
  57. #define MB_MBAFF 0
  58. #define MB_FIELD 0
  59. #define FRAME_MBAFF 0
  60. #undef IS_INTERLACED
  61. #define IS_INTERLACED(mb_type) 0
  62. #endif
  63. /**
  64. * Sequence parameter set
  65. */
  66. typedef struct SPS{
  67. int profile_idc;
  68. int level_idc;
  69. int transform_bypass; ///< qpprime_y_zero_transform_bypass_flag
  70. int log2_max_frame_num; ///< log2_max_frame_num_minus4 + 4
  71. int poc_type; ///< pic_order_cnt_type
  72. int log2_max_poc_lsb; ///< log2_max_pic_order_cnt_lsb_minus4
  73. int delta_pic_order_always_zero_flag;
  74. int offset_for_non_ref_pic;
  75. int offset_for_top_to_bottom_field;
  76. int poc_cycle_length; ///< num_ref_frames_in_pic_order_cnt_cycle
  77. int ref_frame_count; ///< num_ref_frames
  78. int gaps_in_frame_num_allowed_flag;
  79. int mb_width; ///< frame_width_in_mbs_minus1 + 1
  80. int mb_height; ///< frame_height_in_mbs_minus1 + 1
  81. int frame_mbs_only_flag;
  82. int mb_aff; ///<mb_adaptive_frame_field_flag
  83. int direct_8x8_inference_flag;
  84. int crop; ///< frame_cropping_flag
  85. int crop_left; ///< frame_cropping_rect_left_offset
  86. int crop_right; ///< frame_cropping_rect_right_offset
  87. int crop_top; ///< frame_cropping_rect_top_offset
  88. int crop_bottom; ///< frame_cropping_rect_bottom_offset
  89. int vui_parameters_present_flag;
  90. AVRational sar;
  91. int timing_info_present_flag;
  92. uint32_t num_units_in_tick;
  93. uint32_t time_scale;
  94. int fixed_frame_rate_flag;
  95. short offset_for_ref_frame[256]; //FIXME dyn aloc?
  96. int bitstream_restriction_flag;
  97. int num_reorder_frames;
  98. int scaling_matrix_present;
  99. uint8_t scaling_matrix4[6][16];
  100. uint8_t scaling_matrix8[2][64];
  101. }SPS;
  102. /**
  103. * Picture parameter set
  104. */
  105. typedef struct PPS{
  106. unsigned int sps_id;
  107. int cabac; ///< entropy_coding_mode_flag
  108. int pic_order_present; ///< pic_order_present_flag
  109. int slice_group_count; ///< num_slice_groups_minus1 + 1
  110. int mb_slice_group_map_type;
  111. unsigned int ref_count[2]; ///< num_ref_idx_l0/1_active_minus1 + 1
  112. int weighted_pred; ///< weighted_pred_flag
  113. int weighted_bipred_idc;
  114. int init_qp; ///< pic_init_qp_minus26 + 26
  115. int init_qs; ///< pic_init_qs_minus26 + 26
  116. int chroma_qp_index_offset;
  117. int deblocking_filter_parameters_present; ///< deblocking_filter_parameters_present_flag
  118. int constrained_intra_pred; ///< constrained_intra_pred_flag
  119. int redundant_pic_cnt_present; ///< redundant_pic_cnt_present_flag
  120. int transform_8x8_mode; ///< transform_8x8_mode_flag
  121. uint8_t scaling_matrix4[6][16];
  122. uint8_t scaling_matrix8[2][64];
  123. }PPS;
  124. /**
  125. * Memory management control operation opcode.
  126. */
  127. typedef enum MMCOOpcode{
  128. MMCO_END=0,
  129. MMCO_SHORT2UNUSED,
  130. MMCO_LONG2UNUSED,
  131. MMCO_SHORT2LONG,
  132. MMCO_SET_MAX_LONG,
  133. MMCO_RESET,
  134. MMCO_LONG,
  135. } MMCOOpcode;
  136. /**
  137. * Memory management control operation.
  138. */
  139. typedef struct MMCO{
  140. MMCOOpcode opcode;
  141. int short_frame_num;
  142. int long_index;
  143. } MMCO;
  144. /**
  145. * H264Context
  146. */
  147. typedef struct H264Context{
  148. MpegEncContext s;
  149. int nal_ref_idc;
  150. int nal_unit_type;
  151. uint8_t *rbsp_buffer;
  152. unsigned int rbsp_buffer_size;
  153. /**
  154. * Used to parse AVC variant of h264
  155. */
  156. int is_avc; ///< this flag is != 0 if codec is avc1
  157. int got_avcC; ///< flag used to parse avcC data only once
  158. int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
  159. int chroma_qp; //QPc
  160. int prev_mb_skipped;
  161. int next_mb_skipped;
  162. //prediction stuff
  163. int chroma_pred_mode;
  164. int intra16x16_pred_mode;
  165. int top_mb_xy;
  166. int left_mb_xy[2];
  167. int8_t intra4x4_pred_mode_cache[5*8];
  168. int8_t (*intra4x4_pred_mode)[8];
  169. void (*pred4x4 [9+3])(uint8_t *src, uint8_t *topright, int stride);//FIXME move to dsp?
  170. void (*pred8x8l [9+3])(uint8_t *src, int topleft, int topright, int stride);
  171. void (*pred8x8 [4+3])(uint8_t *src, int stride);
  172. void (*pred16x16[4+3])(uint8_t *src, int stride);
  173. unsigned int topleft_samples_available;
  174. unsigned int top_samples_available;
  175. unsigned int topright_samples_available;
  176. unsigned int left_samples_available;
  177. uint8_t (*top_borders[2])[16+2*8];
  178. uint8_t left_border[2*(17+2*9)];
  179. /**
  180. * non zero coeff count cache.
  181. * is 64 if not available.
  182. */
  183. DECLARE_ALIGNED_8(uint8_t, non_zero_count_cache[6*8]);
  184. uint8_t (*non_zero_count)[16];
  185. /**
  186. * Motion vector cache.
  187. */
  188. DECLARE_ALIGNED_8(int16_t, mv_cache[2][5*8][2]);
  189. DECLARE_ALIGNED_8(int8_t, ref_cache[2][5*8]);
  190. #define LIST_NOT_USED -1 //FIXME rename?
  191. #define PART_NOT_AVAILABLE -2
  192. /**
  193. * is 1 if the specific list MV&references are set to 0,0,-2.
  194. */
  195. int mv_cache_clean[2];
  196. /**
  197. * number of neighbors (top and/or left) that used 8x8 dct
  198. */
  199. int neighbor_transform_size;
  200. /**
  201. * block_offset[ 0..23] for frame macroblocks
  202. * block_offset[24..47] for field macroblocks
  203. */
  204. int block_offset[2*(16+8)];
  205. uint32_t *mb2b_xy; //FIXME are these 4 a good idea?
  206. uint32_t *mb2b8_xy;
  207. int b_stride; //FIXME use s->b4_stride
  208. int b8_stride;
  209. int mb_linesize; ///< may be equal to s->linesize or s->linesize*2, for mbaff
  210. int mb_uvlinesize;
  211. int emu_edge_width;
  212. int emu_edge_height;
  213. int halfpel_flag;
  214. int thirdpel_flag;
  215. int unknown_svq3_flag;
  216. int next_slice_index;
  217. SPS sps_buffer[MAX_SPS_COUNT];
  218. SPS sps; ///< current sps
  219. PPS pps_buffer[MAX_PPS_COUNT];
  220. /**
  221. * current pps
  222. */
  223. PPS pps; //FIXME move to Picture perhaps? (->no) do we need that?
  224. uint32_t dequant4_buffer[6][52][16];
  225. uint32_t dequant8_buffer[2][52][64];
  226. uint32_t (*dequant4_coeff[6])[16];
  227. uint32_t (*dequant8_coeff[2])[64];
  228. int dequant_coeff_pps; ///< reinit tables when pps changes
  229. int slice_num;
  230. uint8_t *slice_table_base;
  231. uint8_t *slice_table; ///< slice_table_base + 2*mb_stride + 1
  232. int slice_type;
  233. int slice_type_fixed;
  234. //interlacing specific flags
  235. int mb_aff_frame;
  236. int mb_field_decoding_flag;
  237. int mb_mbaff; ///< mb_aff_frame && mb_field_decoding_flag
  238. unsigned int sub_mb_type[4];
  239. //POC stuff
  240. int poc_lsb;
  241. int poc_msb;
  242. int delta_poc_bottom;
  243. int delta_poc[2];
  244. int frame_num;
  245. int prev_poc_msb; ///< poc_msb of the last reference pic for POC type 0
  246. int prev_poc_lsb; ///< poc_lsb of the last reference pic for POC type 0
  247. int frame_num_offset; ///< for POC type 2
  248. int prev_frame_num_offset; ///< for POC type 2
  249. int prev_frame_num; ///< frame_num of the last pic for POC type 1/2
  250. /**
  251. * frame_num for frames or 2*frame_num for field pics.
  252. */
  253. int curr_pic_num;
  254. /**
  255. * max_frame_num or 2*max_frame_num for field pics.
  256. */
  257. int max_pic_num;
  258. //Weighted pred stuff
  259. int use_weight;
  260. int use_weight_chroma;
  261. int luma_log2_weight_denom;
  262. int chroma_log2_weight_denom;
  263. int luma_weight[2][48];
  264. int luma_offset[2][48];
  265. int chroma_weight[2][48][2];
  266. int chroma_offset[2][48][2];
  267. int implicit_weight[48][48];
  268. //deblock
  269. int deblocking_filter; ///< disable_deblocking_filter_idc with 1<->0
  270. int slice_alpha_c0_offset;
  271. int slice_beta_offset;
  272. int redundant_pic_count;
  273. int direct_spatial_mv_pred;
  274. int dist_scale_factor[16];
  275. int dist_scale_factor_field[32];
  276. int map_col_to_list0[2][16];
  277. int map_col_to_list0_field[2][32];
  278. /**
  279. * num_ref_idx_l0/1_active_minus1 + 1
  280. */
  281. unsigned int ref_count[2]; ///< counts frames or fields, depending on current mb mode
  282. unsigned int list_count;
  283. Picture *short_ref[32];
  284. Picture *long_ref[32];
  285. Picture default_ref_list[2][32];
  286. Picture ref_list[2][48]; ///< 0..15: frame refs, 16..47: mbaff field refs
  287. Picture *delayed_pic[18]; //FIXME size?
  288. Picture *delayed_output_pic;
  289. /**
  290. * memory management control operations buffer.
  291. */
  292. MMCO mmco[MAX_MMCO_COUNT];
  293. int mmco_index;
  294. int long_ref_count; ///< number of actual long term references
  295. int short_ref_count; ///< number of actual short term references
  296. //data partitioning
  297. GetBitContext intra_gb;
  298. GetBitContext inter_gb;
  299. GetBitContext *intra_gb_ptr;
  300. GetBitContext *inter_gb_ptr;
  301. DECLARE_ALIGNED_8(DCTELEM, mb[16*24]);
  302. DCTELEM mb_padding[256]; ///< as mb is addressed by scantable[i] and scantable is uint8_t we can either check that i is not to large or ensure that there is some unused stuff after mb
  303. /**
  304. * Cabac
  305. */
  306. CABACContext cabac;
  307. uint8_t cabac_state[460];
  308. int cabac_init_idc;
  309. /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
  310. uint16_t *cbp_table;
  311. int cbp;
  312. int top_cbp;
  313. int left_cbp;
  314. /* chroma_pred_mode for i4x4 or i16x16, else 0 */
  315. uint8_t *chroma_pred_mode_table;
  316. int last_qscale_diff;
  317. int16_t (*mvd_table[2])[2];
  318. DECLARE_ALIGNED_8(int16_t, mvd_cache[2][5*8][2]);
  319. uint8_t *direct_table;
  320. uint8_t direct_cache[5*8];
  321. uint8_t zigzag_scan[16];
  322. uint8_t zigzag_scan8x8[64];
  323. uint8_t zigzag_scan8x8_cavlc[64];
  324. uint8_t field_scan[16];
  325. uint8_t field_scan8x8[64];
  326. uint8_t field_scan8x8_cavlc[64];
  327. const uint8_t *zigzag_scan_q0;
  328. const uint8_t *zigzag_scan8x8_q0;
  329. const uint8_t *zigzag_scan8x8_cavlc_q0;
  330. const uint8_t *field_scan_q0;
  331. const uint8_t *field_scan8x8_q0;
  332. const uint8_t *field_scan8x8_cavlc_q0;
  333. int x264_build;
  334. }H264Context;
  335. static VLC coeff_token_vlc[4];
  336. static VLC chroma_dc_coeff_token_vlc;
  337. static VLC total_zeros_vlc[15];
  338. static VLC chroma_dc_total_zeros_vlc[3];
  339. static VLC run_vlc[6];
  340. static VLC run7_vlc;
  341. static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
  342. static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
  343. static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
  344. static void filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
  345. static av_always_inline uint32_t pack16to32(int a, int b){
  346. #ifdef WORDS_BIGENDIAN
  347. return (b&0xFFFF) + (a<<16);
  348. #else
  349. return (a&0xFFFF) + (b<<16);
  350. #endif
  351. }
  352. const uint8_t ff_rem6[52]={
  353. 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
  354. };
  355. const uint8_t ff_div6[52]={
  356. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
  357. };
  358. /**
  359. * fill a rectangle.
  360. * @param h height of the rectangle, should be a constant
  361. * @param w width of the rectangle, should be a constant
  362. * @param size the size of val (1 or 4), should be a constant
  363. */
  364. static av_always_inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){
  365. uint8_t *p= (uint8_t*)vp;
  366. assert(size==1 || size==4);
  367. assert(w<=4);
  368. w *= size;
  369. stride *= size;
  370. assert((((long)vp)&(FFMIN(w, STRIDE_ALIGN)-1)) == 0);
  371. assert((stride&(w-1))==0);
  372. if(w==2){
  373. const uint16_t v= size==4 ? val : val*0x0101;
  374. *(uint16_t*)(p + 0*stride)= v;
  375. if(h==1) return;
  376. *(uint16_t*)(p + 1*stride)= v;
  377. if(h==2) return;
  378. *(uint16_t*)(p + 2*stride)=
  379. *(uint16_t*)(p + 3*stride)= v;
  380. }else if(w==4){
  381. const uint32_t v= size==4 ? val : val*0x01010101;
  382. *(uint32_t*)(p + 0*stride)= v;
  383. if(h==1) return;
  384. *(uint32_t*)(p + 1*stride)= v;
  385. if(h==2) return;
  386. *(uint32_t*)(p + 2*stride)=
  387. *(uint32_t*)(p + 3*stride)= v;
  388. }else if(w==8){
  389. //gcc can't optimize 64bit math on x86_32
  390. #if defined(ARCH_X86_64) || (defined(MP_WORDSIZE) && MP_WORDSIZE >= 64)
  391. const uint64_t v= val*0x0100000001ULL;
  392. *(uint64_t*)(p + 0*stride)= v;
  393. if(h==1) return;
  394. *(uint64_t*)(p + 1*stride)= v;
  395. if(h==2) return;
  396. *(uint64_t*)(p + 2*stride)=
  397. *(uint64_t*)(p + 3*stride)= v;
  398. }else if(w==16){
  399. const uint64_t v= val*0x0100000001ULL;
  400. *(uint64_t*)(p + 0+0*stride)=
  401. *(uint64_t*)(p + 8+0*stride)=
  402. *(uint64_t*)(p + 0+1*stride)=
  403. *(uint64_t*)(p + 8+1*stride)= v;
  404. if(h==2) return;
  405. *(uint64_t*)(p + 0+2*stride)=
  406. *(uint64_t*)(p + 8+2*stride)=
  407. *(uint64_t*)(p + 0+3*stride)=
  408. *(uint64_t*)(p + 8+3*stride)= v;
  409. #else
  410. *(uint32_t*)(p + 0+0*stride)=
  411. *(uint32_t*)(p + 4+0*stride)= val;
  412. if(h==1) return;
  413. *(uint32_t*)(p + 0+1*stride)=
  414. *(uint32_t*)(p + 4+1*stride)= val;
  415. if(h==2) return;
  416. *(uint32_t*)(p + 0+2*stride)=
  417. *(uint32_t*)(p + 4+2*stride)=
  418. *(uint32_t*)(p + 0+3*stride)=
  419. *(uint32_t*)(p + 4+3*stride)= val;
  420. }else if(w==16){
  421. *(uint32_t*)(p + 0+0*stride)=
  422. *(uint32_t*)(p + 4+0*stride)=
  423. *(uint32_t*)(p + 8+0*stride)=
  424. *(uint32_t*)(p +12+0*stride)=
  425. *(uint32_t*)(p + 0+1*stride)=
  426. *(uint32_t*)(p + 4+1*stride)=
  427. *(uint32_t*)(p + 8+1*stride)=
  428. *(uint32_t*)(p +12+1*stride)= val;
  429. if(h==2) return;
  430. *(uint32_t*)(p + 0+2*stride)=
  431. *(uint32_t*)(p + 4+2*stride)=
  432. *(uint32_t*)(p + 8+2*stride)=
  433. *(uint32_t*)(p +12+2*stride)=
  434. *(uint32_t*)(p + 0+3*stride)=
  435. *(uint32_t*)(p + 4+3*stride)=
  436. *(uint32_t*)(p + 8+3*stride)=
  437. *(uint32_t*)(p +12+3*stride)= val;
  438. #endif
  439. }else
  440. assert(0);
  441. assert(h==4);
  442. }
  443. static void fill_caches(H264Context *h, int mb_type, int for_deblock){
  444. MpegEncContext * const s = &h->s;
  445. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  446. int topleft_xy, top_xy, topright_xy, left_xy[2];
  447. int topleft_type, top_type, topright_type, left_type[2];
  448. int left_block[8];
  449. int i;
  450. //FIXME deblocking could skip the intra and nnz parts.
  451. if(for_deblock && (h->slice_num == 1 || h->slice_table[mb_xy] == h->slice_table[mb_xy-s->mb_stride]) && !FRAME_MBAFF)
  452. return;
  453. //wow what a mess, why didn't they simplify the interlacing&intra stuff, i can't imagine that these complex rules are worth it
  454. top_xy = mb_xy - s->mb_stride;
  455. topleft_xy = top_xy - 1;
  456. topright_xy= top_xy + 1;
  457. left_xy[1] = left_xy[0] = mb_xy-1;
  458. left_block[0]= 0;
  459. left_block[1]= 1;
  460. left_block[2]= 2;
  461. left_block[3]= 3;
  462. left_block[4]= 7;
  463. left_block[5]= 10;
  464. left_block[6]= 8;
  465. left_block[7]= 11;
  466. if(FRAME_MBAFF){
  467. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  468. const int top_pair_xy = pair_xy - s->mb_stride;
  469. const int topleft_pair_xy = top_pair_xy - 1;
  470. const int topright_pair_xy = top_pair_xy + 1;
  471. const int topleft_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
  472. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  473. const int topright_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
  474. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  475. const int curr_mb_frame_flag = !IS_INTERLACED(mb_type);
  476. const int bottom = (s->mb_y & 1);
  477. tprintf(s->avctx, "fill_caches: curr_mb_frame_flag:%d, left_mb_frame_flag:%d, topleft_mb_frame_flag:%d, top_mb_frame_flag:%d, topright_mb_frame_flag:%d\n", curr_mb_frame_flag, left_mb_frame_flag, topleft_mb_frame_flag, top_mb_frame_flag, topright_mb_frame_flag);
  478. if (bottom
  479. ? !curr_mb_frame_flag // bottom macroblock
  480. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  481. ) {
  482. top_xy -= s->mb_stride;
  483. }
  484. if (bottom
  485. ? !curr_mb_frame_flag // bottom macroblock
  486. : (!curr_mb_frame_flag && !topleft_mb_frame_flag) // top macroblock
  487. ) {
  488. topleft_xy -= s->mb_stride;
  489. }
  490. if (bottom
  491. ? !curr_mb_frame_flag // bottom macroblock
  492. : (!curr_mb_frame_flag && !topright_mb_frame_flag) // top macroblock
  493. ) {
  494. topright_xy -= s->mb_stride;
  495. }
  496. if (left_mb_frame_flag != curr_mb_frame_flag) {
  497. left_xy[1] = left_xy[0] = pair_xy - 1;
  498. if (curr_mb_frame_flag) {
  499. if (bottom) {
  500. left_block[0]= 2;
  501. left_block[1]= 2;
  502. left_block[2]= 3;
  503. left_block[3]= 3;
  504. left_block[4]= 8;
  505. left_block[5]= 11;
  506. left_block[6]= 8;
  507. left_block[7]= 11;
  508. } else {
  509. left_block[0]= 0;
  510. left_block[1]= 0;
  511. left_block[2]= 1;
  512. left_block[3]= 1;
  513. left_block[4]= 7;
  514. left_block[5]= 10;
  515. left_block[6]= 7;
  516. left_block[7]= 10;
  517. }
  518. } else {
  519. left_xy[1] += s->mb_stride;
  520. //left_block[0]= 0;
  521. left_block[1]= 2;
  522. left_block[2]= 0;
  523. left_block[3]= 2;
  524. //left_block[4]= 7;
  525. left_block[5]= 10;
  526. left_block[6]= 7;
  527. left_block[7]= 10;
  528. }
  529. }
  530. }
  531. h->top_mb_xy = top_xy;
  532. h->left_mb_xy[0] = left_xy[0];
  533. h->left_mb_xy[1] = left_xy[1];
  534. if(for_deblock){
  535. topleft_type = 0;
  536. topright_type = 0;
  537. top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0;
  538. left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
  539. left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
  540. if(FRAME_MBAFF && !IS_INTRA(mb_type)){
  541. int list;
  542. int v = *(uint16_t*)&h->non_zero_count[mb_xy][14];
  543. for(i=0; i<16; i++)
  544. h->non_zero_count_cache[scan8[i]] = (v>>i)&1;
  545. for(list=0; list<h->list_count; list++){
  546. if(USES_LIST(mb_type,list)){
  547. uint32_t *src = (uint32_t*)s->current_picture.motion_val[list][h->mb2b_xy[mb_xy]];
  548. uint32_t *dst = (uint32_t*)h->mv_cache[list][scan8[0]];
  549. int8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
  550. for(i=0; i<4; i++, dst+=8, src+=h->b_stride){
  551. dst[0] = src[0];
  552. dst[1] = src[1];
  553. dst[2] = src[2];
  554. dst[3] = src[3];
  555. }
  556. *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =
  557. *(uint32_t*)&h->ref_cache[list][scan8[ 2]] = pack16to32(ref[0],ref[1])*0x0101;
  558. ref += h->b8_stride;
  559. *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =
  560. *(uint32_t*)&h->ref_cache[list][scan8[10]] = pack16to32(ref[0],ref[1])*0x0101;
  561. }else{
  562. fill_rectangle(&h-> mv_cache[list][scan8[ 0]], 4, 4, 8, 0, 4);
  563. fill_rectangle(&h->ref_cache[list][scan8[ 0]], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  564. }
  565. }
  566. }
  567. }else{
  568. topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
  569. top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
  570. topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
  571. left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
  572. left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
  573. }
  574. if(IS_INTRA(mb_type)){
  575. h->topleft_samples_available=
  576. h->top_samples_available=
  577. h->left_samples_available= 0xFFFF;
  578. h->topright_samples_available= 0xEEEA;
  579. if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
  580. h->topleft_samples_available= 0xB3FF;
  581. h->top_samples_available= 0x33FF;
  582. h->topright_samples_available= 0x26EA;
  583. }
  584. for(i=0; i<2; i++){
  585. if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
  586. h->topleft_samples_available&= 0xDF5F;
  587. h->left_samples_available&= 0x5F5F;
  588. }
  589. }
  590. if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
  591. h->topleft_samples_available&= 0x7FFF;
  592. if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
  593. h->topright_samples_available&= 0xFBFF;
  594. if(IS_INTRA4x4(mb_type)){
  595. if(IS_INTRA4x4(top_type)){
  596. h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
  597. h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
  598. h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
  599. h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
  600. }else{
  601. int pred;
  602. if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred))
  603. pred= -1;
  604. else{
  605. pred= 2;
  606. }
  607. h->intra4x4_pred_mode_cache[4+8*0]=
  608. h->intra4x4_pred_mode_cache[5+8*0]=
  609. h->intra4x4_pred_mode_cache[6+8*0]=
  610. h->intra4x4_pred_mode_cache[7+8*0]= pred;
  611. }
  612. for(i=0; i<2; i++){
  613. if(IS_INTRA4x4(left_type[i])){
  614. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
  615. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
  616. }else{
  617. int pred;
  618. if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred))
  619. pred= -1;
  620. else{
  621. pred= 2;
  622. }
  623. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
  624. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
  625. }
  626. }
  627. }
  628. }
  629. /*
  630. 0 . T T. T T T T
  631. 1 L . .L . . . .
  632. 2 L . .L . . . .
  633. 3 . T TL . . . .
  634. 4 L . .L . . . .
  635. 5 L . .. . . . .
  636. */
  637. //FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec)
  638. if(top_type){
  639. h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
  640. h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
  641. h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
  642. h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
  643. h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
  644. h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
  645. h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
  646. h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
  647. }else{
  648. h->non_zero_count_cache[4+8*0]=
  649. h->non_zero_count_cache[5+8*0]=
  650. h->non_zero_count_cache[6+8*0]=
  651. h->non_zero_count_cache[7+8*0]=
  652. h->non_zero_count_cache[1+8*0]=
  653. h->non_zero_count_cache[2+8*0]=
  654. h->non_zero_count_cache[1+8*3]=
  655. h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  656. }
  657. for (i=0; i<2; i++) {
  658. if(left_type[i]){
  659. h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
  660. h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
  661. h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
  662. h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
  663. }else{
  664. h->non_zero_count_cache[3+8*1 + 2*8*i]=
  665. h->non_zero_count_cache[3+8*2 + 2*8*i]=
  666. h->non_zero_count_cache[0+8*1 + 8*i]=
  667. h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  668. }
  669. }
  670. if( h->pps.cabac ) {
  671. // top_cbp
  672. if(top_type) {
  673. h->top_cbp = h->cbp_table[top_xy];
  674. } else if(IS_INTRA(mb_type)) {
  675. h->top_cbp = 0x1C0;
  676. } else {
  677. h->top_cbp = 0;
  678. }
  679. // left_cbp
  680. if (left_type[0]) {
  681. h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
  682. } else if(IS_INTRA(mb_type)) {
  683. h->left_cbp = 0x1C0;
  684. } else {
  685. h->left_cbp = 0;
  686. }
  687. if (left_type[0]) {
  688. h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
  689. }
  690. if (left_type[1]) {
  691. h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
  692. }
  693. }
  694. #if 1
  695. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  696. int list;
  697. for(list=0; list<h->list_count; list++){
  698. if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
  699. /*if(!h->mv_cache_clean[list]){
  700. memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
  701. memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
  702. h->mv_cache_clean[list]= 1;
  703. }*/
  704. continue;
  705. }
  706. h->mv_cache_clean[list]= 0;
  707. if(USES_LIST(top_type, list)){
  708. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  709. const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
  710. *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
  711. *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
  712. *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
  713. *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
  714. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  715. h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
  716. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  717. h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
  718. }else{
  719. *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
  720. *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
  721. *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
  722. *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
  723. *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
  724. }
  725. for(i=0; i<2; i++){
  726. int cache_idx = scan8[0] - 1 + i*2*8;
  727. if(USES_LIST(left_type[i], list)){
  728. const int b_xy= h->mb2b_xy[left_xy[i]] + 3;
  729. const int b8_xy= h->mb2b8_xy[left_xy[i]] + 1;
  730. *(uint32_t*)h->mv_cache[list][cache_idx ]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[0+i*2]];
  731. *(uint32_t*)h->mv_cache[list][cache_idx+8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[1+i*2]];
  732. h->ref_cache[list][cache_idx ]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0+i*2]>>1)];
  733. h->ref_cache[list][cache_idx+8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1+i*2]>>1)];
  734. }else{
  735. *(uint32_t*)h->mv_cache [list][cache_idx ]=
  736. *(uint32_t*)h->mv_cache [list][cache_idx+8]= 0;
  737. h->ref_cache[list][cache_idx ]=
  738. h->ref_cache[list][cache_idx+8]= left_type[i] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  739. }
  740. }
  741. if((for_deblock || (IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred)) && !FRAME_MBAFF)
  742. continue;
  743. if(USES_LIST(topleft_type, list)){
  744. const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
  745. const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + h->b8_stride;
  746. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  747. h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  748. }else{
  749. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
  750. h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  751. }
  752. if(USES_LIST(topright_type, list)){
  753. const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
  754. const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
  755. *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  756. h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  757. }else{
  758. *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
  759. h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  760. }
  761. if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)
  762. continue;
  763. h->ref_cache[list][scan8[5 ]+1] =
  764. h->ref_cache[list][scan8[7 ]+1] =
  765. h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewhere else)
  766. h->ref_cache[list][scan8[4 ]] =
  767. h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
  768. *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
  769. *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
  770. *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  771. *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
  772. *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
  773. if( h->pps.cabac ) {
  774. /* XXX beurk, Load mvd */
  775. if(USES_LIST(top_type, list)){
  776. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  777. *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
  778. *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
  779. *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
  780. *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
  781. }else{
  782. *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
  783. *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
  784. *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
  785. *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
  786. }
  787. if(USES_LIST(left_type[0], list)){
  788. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  789. *(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]];
  790. *(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]];
  791. }else{
  792. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
  793. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
  794. }
  795. if(USES_LIST(left_type[1], list)){
  796. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  797. *(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]];
  798. *(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]];
  799. }else{
  800. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
  801. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
  802. }
  803. *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
  804. *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
  805. *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  806. *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
  807. *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
  808. if(h->slice_type == B_TYPE){
  809. fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
  810. if(IS_DIRECT(top_type)){
  811. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
  812. }else if(IS_8X8(top_type)){
  813. int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
  814. h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
  815. h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
  816. }else{
  817. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
  818. }
  819. if(IS_DIRECT(left_type[0]))
  820. h->direct_cache[scan8[0] - 1 + 0*8]= 1;
  821. else if(IS_8X8(left_type[0]))
  822. 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)];
  823. else
  824. h->direct_cache[scan8[0] - 1 + 0*8]= 0;
  825. if(IS_DIRECT(left_type[1]))
  826. h->direct_cache[scan8[0] - 1 + 2*8]= 1;
  827. else if(IS_8X8(left_type[1]))
  828. 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)];
  829. else
  830. h->direct_cache[scan8[0] - 1 + 2*8]= 0;
  831. }
  832. }
  833. if(FRAME_MBAFF){
  834. #define MAP_MVS\
  835. MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
  836. MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
  837. MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
  838. MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
  839. MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
  840. MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
  841. MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\
  842. MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\
  843. MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\
  844. MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
  845. if(MB_FIELD){
  846. #define MAP_F2F(idx, mb_type)\
  847. if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  848. h->ref_cache[list][idx] <<= 1;\
  849. h->mv_cache[list][idx][1] /= 2;\
  850. h->mvd_cache[list][idx][1] /= 2;\
  851. }
  852. MAP_MVS
  853. #undef MAP_F2F
  854. }else{
  855. #define MAP_F2F(idx, mb_type)\
  856. if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  857. h->ref_cache[list][idx] >>= 1;\
  858. h->mv_cache[list][idx][1] <<= 1;\
  859. h->mvd_cache[list][idx][1] <<= 1;\
  860. }
  861. MAP_MVS
  862. #undef MAP_F2F
  863. }
  864. }
  865. }
  866. }
  867. #endif
  868. h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
  869. }
  870. static inline void write_back_intra_pred_mode(H264Context *h){
  871. MpegEncContext * const s = &h->s;
  872. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  873. h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
  874. h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
  875. h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
  876. h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
  877. h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
  878. h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
  879. h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
  880. }
  881. /**
  882. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  883. */
  884. static inline int check_intra4x4_pred_mode(H264Context *h){
  885. MpegEncContext * const s = &h->s;
  886. static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
  887. static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
  888. int i;
  889. if(!(h->top_samples_available&0x8000)){
  890. for(i=0; i<4; i++){
  891. int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
  892. if(status<0){
  893. 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);
  894. return -1;
  895. } else if(status){
  896. h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
  897. }
  898. }
  899. }
  900. if(!(h->left_samples_available&0x8000)){
  901. for(i=0; i<4; i++){
  902. int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
  903. if(status<0){
  904. 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);
  905. return -1;
  906. } else if(status){
  907. h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
  908. }
  909. }
  910. }
  911. return 0;
  912. } //FIXME cleanup like next
  913. /**
  914. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  915. */
  916. static inline int check_intra_pred_mode(H264Context *h, int mode){
  917. MpegEncContext * const s = &h->s;
  918. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  919. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  920. if(mode > 6U) {
  921. 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);
  922. return -1;
  923. }
  924. if(!(h->top_samples_available&0x8000)){
  925. mode= top[ mode ];
  926. if(mode<0){
  927. 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);
  928. return -1;
  929. }
  930. }
  931. if(!(h->left_samples_available&0x8000)){
  932. mode= left[ mode ];
  933. if(mode<0){
  934. 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);
  935. return -1;
  936. }
  937. }
  938. return mode;
  939. }
  940. /**
  941. * gets the predicted intra4x4 prediction mode.
  942. */
  943. static inline int pred_intra_mode(H264Context *h, int n){
  944. const int index8= scan8[n];
  945. const int left= h->intra4x4_pred_mode_cache[index8 - 1];
  946. const int top = h->intra4x4_pred_mode_cache[index8 - 8];
  947. const int min= FFMIN(left, top);
  948. tprintf(h->s.avctx, "mode:%d %d min:%d\n", left ,top, min);
  949. if(min<0) return DC_PRED;
  950. else return min;
  951. }
  952. static inline void write_back_non_zero_count(H264Context *h){
  953. MpegEncContext * const s = &h->s;
  954. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  955. h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
  956. h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
  957. h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
  958. h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
  959. h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
  960. h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
  961. h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
  962. h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
  963. h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
  964. h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
  965. h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
  966. h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
  967. h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
  968. if(FRAME_MBAFF){
  969. // store all luma nnzs, for deblocking
  970. int v = 0, i;
  971. for(i=0; i<16; i++)
  972. v += (!!h->non_zero_count_cache[scan8[i]]) << i;
  973. *(uint16_t*)&h->non_zero_count[mb_xy][14] = v;
  974. }
  975. }
  976. /**
  977. * gets the predicted number of non zero coefficients.
  978. * @param n block index
  979. */
  980. static inline int pred_non_zero_count(H264Context *h, int n){
  981. const int index8= scan8[n];
  982. const int left= h->non_zero_count_cache[index8 - 1];
  983. const int top = h->non_zero_count_cache[index8 - 8];
  984. int i= left + top;
  985. if(i<64) i= (i+1)>>1;
  986. tprintf(h->s.avctx, "pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
  987. return i&31;
  988. }
  989. static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
  990. const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
  991. MpegEncContext *s = &h->s;
  992. /* there is no consistent mapping of mvs to neighboring locations that will
  993. * make mbaff happy, so we can't move all this logic to fill_caches */
  994. if(FRAME_MBAFF){
  995. const uint32_t *mb_types = s->current_picture_ptr->mb_type;
  996. const int16_t *mv;
  997. *(uint32_t*)h->mv_cache[list][scan8[0]-2] = 0;
  998. *C = h->mv_cache[list][scan8[0]-2];
  999. if(!MB_FIELD
  1000. && (s->mb_y&1) && i < scan8[0]+8 && topright_ref != PART_NOT_AVAILABLE){
  1001. int topright_xy = s->mb_x + (s->mb_y-1)*s->mb_stride + (i == scan8[0]+3);
  1002. if(IS_INTERLACED(mb_types[topright_xy])){
  1003. #define SET_DIAG_MV(MV_OP, REF_OP, X4, Y4)\
  1004. const int x4 = X4, y4 = Y4;\
  1005. const int mb_type = mb_types[(x4>>2)+(y4>>2)*s->mb_stride];\
  1006. if(!USES_LIST(mb_type,list) && !IS_8X8(mb_type))\
  1007. return LIST_NOT_USED;\
  1008. mv = s->current_picture_ptr->motion_val[list][x4 + y4*h->b_stride];\
  1009. h->mv_cache[list][scan8[0]-2][0] = mv[0];\
  1010. h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
  1011. return s->current_picture_ptr->ref_index[list][(x4>>1) + (y4>>1)*h->b8_stride] REF_OP;
  1012. SET_DIAG_MV(*2, >>1, s->mb_x*4+(i&7)-4+part_width, s->mb_y*4-1);
  1013. }
  1014. }
  1015. if(topright_ref == PART_NOT_AVAILABLE
  1016. && ((s->mb_y&1) || i >= scan8[0]+8) && (i&7)==4
  1017. && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
  1018. if(!MB_FIELD
  1019. && IS_INTERLACED(mb_types[h->left_mb_xy[0]])){
  1020. SET_DIAG_MV(*2, >>1, s->mb_x*4-1, (s->mb_y|1)*4+(s->mb_y&1)*2+(i>>4)-1);
  1021. }
  1022. if(MB_FIELD
  1023. && !IS_INTERLACED(mb_types[h->left_mb_xy[0]])
  1024. && i >= scan8[0]+8){
  1025. // leftshift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's ok.
  1026. SET_DIAG_MV(>>1, <<1, s->mb_x*4-1, (s->mb_y&~1)*4 - 1 + ((i-scan8[0])>>3)*2);
  1027. }
  1028. }
  1029. #undef SET_DIAG_MV
  1030. }
  1031. if(topright_ref != PART_NOT_AVAILABLE){
  1032. *C= h->mv_cache[list][ i - 8 + part_width ];
  1033. return topright_ref;
  1034. }else{
  1035. tprintf(s->avctx, "topright MV not available\n");
  1036. *C= h->mv_cache[list][ i - 8 - 1 ];
  1037. return h->ref_cache[list][ i - 8 - 1 ];
  1038. }
  1039. }
  1040. /**
  1041. * gets the predicted MV.
  1042. * @param n the block index
  1043. * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
  1044. * @param mx the x component of the predicted motion vector
  1045. * @param my the y component of the predicted motion vector
  1046. */
  1047. static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
  1048. const int index8= scan8[n];
  1049. const int top_ref= h->ref_cache[list][ index8 - 8 ];
  1050. const int left_ref= h->ref_cache[list][ index8 - 1 ];
  1051. const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
  1052. const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
  1053. const int16_t * C;
  1054. int diagonal_ref, match_count;
  1055. assert(part_width==1 || part_width==2 || part_width==4);
  1056. /* mv_cache
  1057. B . . A T T T T
  1058. U . . L . . , .
  1059. U . . L . . . .
  1060. U . . L . . , .
  1061. . . . L . . . .
  1062. */
  1063. diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
  1064. match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
  1065. tprintf(h->s.avctx, "pred_motion match_count=%d\n", match_count);
  1066. if(match_count > 1){ //most common
  1067. *mx= mid_pred(A[0], B[0], C[0]);
  1068. *my= mid_pred(A[1], B[1], C[1]);
  1069. }else if(match_count==1){
  1070. if(left_ref==ref){
  1071. *mx= A[0];
  1072. *my= A[1];
  1073. }else if(top_ref==ref){
  1074. *mx= B[0];
  1075. *my= B[1];
  1076. }else{
  1077. *mx= C[0];
  1078. *my= C[1];
  1079. }
  1080. }else{
  1081. if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
  1082. *mx= A[0];
  1083. *my= A[1];
  1084. }else{
  1085. *mx= mid_pred(A[0], B[0], C[0]);
  1086. *my= mid_pred(A[1], B[1], C[1]);
  1087. }
  1088. }
  1089. tprintf(h->s.avctx, "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);
  1090. }
  1091. /**
  1092. * gets the directionally predicted 16x8 MV.
  1093. * @param n the block index
  1094. * @param mx the x component of the predicted motion vector
  1095. * @param my the y component of the predicted motion vector
  1096. */
  1097. static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  1098. if(n==0){
  1099. const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
  1100. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  1101. tprintf(h->s.avctx, "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);
  1102. if(top_ref == ref){
  1103. *mx= B[0];
  1104. *my= B[1];
  1105. return;
  1106. }
  1107. }else{
  1108. const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
  1109. const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
  1110. tprintf(h->s.avctx, "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);
  1111. if(left_ref == ref){
  1112. *mx= A[0];
  1113. *my= A[1];
  1114. return;
  1115. }
  1116. }
  1117. //RARE
  1118. pred_motion(h, n, 4, list, ref, mx, my);
  1119. }
  1120. /**
  1121. * gets the directionally predicted 8x16 MV.
  1122. * @param n the block index
  1123. * @param mx the x component of the predicted motion vector
  1124. * @param my the y component of the predicted motion vector
  1125. */
  1126. static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  1127. if(n==0){
  1128. const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
  1129. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  1130. tprintf(h->s.avctx, "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);
  1131. if(left_ref == ref){
  1132. *mx= A[0];
  1133. *my= A[1];
  1134. return;
  1135. }
  1136. }else{
  1137. const int16_t * C;
  1138. int diagonal_ref;
  1139. diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
  1140. tprintf(h->s.avctx, "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);
  1141. if(diagonal_ref == ref){
  1142. *mx= C[0];
  1143. *my= C[1];
  1144. return;
  1145. }
  1146. }
  1147. //RARE
  1148. pred_motion(h, n, 2, list, ref, mx, my);
  1149. }
  1150. static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
  1151. const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
  1152. const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
  1153. tprintf(h->s.avctx, "pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
  1154. if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
  1155. || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
  1156. || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
  1157. *mx = *my = 0;
  1158. return;
  1159. }
  1160. pred_motion(h, 0, 4, 0, 0, mx, my);
  1161. return;
  1162. }
  1163. static inline void direct_dist_scale_factor(H264Context * const h){
  1164. const int poc = h->s.current_picture_ptr->poc;
  1165. const int poc1 = h->ref_list[1][0].poc;
  1166. int i;
  1167. for(i=0; i<h->ref_count[0]; i++){
  1168. int poc0 = h->ref_list[0][i].poc;
  1169. int td = av_clip(poc1 - poc0, -128, 127);
  1170. if(td == 0 /* FIXME || pic0 is a long-term ref */){
  1171. h->dist_scale_factor[i] = 256;
  1172. }else{
  1173. int tb = av_clip(poc - poc0, -128, 127);
  1174. int tx = (16384 + (FFABS(td) >> 1)) / td;
  1175. h->dist_scale_factor[i] = av_clip((tb*tx + 32) >> 6, -1024, 1023);
  1176. }
  1177. }
  1178. if(FRAME_MBAFF){
  1179. for(i=0; i<h->ref_count[0]; i++){
  1180. h->dist_scale_factor_field[2*i] =
  1181. h->dist_scale_factor_field[2*i+1] = h->dist_scale_factor[i];
  1182. }
  1183. }
  1184. }
  1185. static inline void direct_ref_list_init(H264Context * const h){
  1186. MpegEncContext * const s = &h->s;
  1187. Picture * const ref1 = &h->ref_list[1][0];
  1188. Picture * const cur = s->current_picture_ptr;
  1189. int list, i, j;
  1190. if(cur->pict_type == I_TYPE)
  1191. cur->ref_count[0] = 0;
  1192. if(cur->pict_type != B_TYPE)
  1193. cur->ref_count[1] = 0;
  1194. for(list=0; list<2; list++){
  1195. cur->ref_count[list] = h->ref_count[list];
  1196. for(j=0; j<h->ref_count[list]; j++)
  1197. cur->ref_poc[list][j] = h->ref_list[list][j].poc;
  1198. }
  1199. if(cur->pict_type != B_TYPE || h->direct_spatial_mv_pred)
  1200. return;
  1201. for(list=0; list<2; list++){
  1202. for(i=0; i<ref1->ref_count[list]; i++){
  1203. const int poc = ref1->ref_poc[list][i];
  1204. h->map_col_to_list0[list][i] = 0; /* bogus; fills in for missing frames */
  1205. for(j=0; j<h->ref_count[list]; j++)
  1206. if(h->ref_list[list][j].poc == poc){
  1207. h->map_col_to_list0[list][i] = j;
  1208. break;
  1209. }
  1210. }
  1211. }
  1212. if(FRAME_MBAFF){
  1213. for(list=0; list<2; list++){
  1214. for(i=0; i<ref1->ref_count[list]; i++){
  1215. j = h->map_col_to_list0[list][i];
  1216. h->map_col_to_list0_field[list][2*i] = 2*j;
  1217. h->map_col_to_list0_field[list][2*i+1] = 2*j+1;
  1218. }
  1219. }
  1220. }
  1221. }
  1222. static inline void pred_direct_motion(H264Context * const h, int *mb_type){
  1223. MpegEncContext * const s = &h->s;
  1224. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  1225. const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1226. const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1227. const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
  1228. const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
  1229. const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[1][b4_xy];
  1230. const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
  1231. const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy];
  1232. const int is_b8x8 = IS_8X8(*mb_type);
  1233. unsigned int sub_mb_type;
  1234. int i8, i4;
  1235. #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
  1236. if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
  1237. /* FIXME save sub mb types from previous frames (or derive from MVs)
  1238. * so we know exactly what block size to use */
  1239. sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  1240. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  1241. }else if(!is_b8x8 && (mb_type_col & MB_TYPE_16x16_OR_INTRA)){
  1242. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  1243. *mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
  1244. }else{
  1245. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  1246. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  1247. }
  1248. if(!is_b8x8)
  1249. *mb_type |= MB_TYPE_DIRECT2;
  1250. if(MB_FIELD)
  1251. *mb_type |= MB_TYPE_INTERLACED;
  1252. tprintf(s->avctx, "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);
  1253. if(h->direct_spatial_mv_pred){
  1254. int ref[2];
  1255. int mv[2][2];
  1256. int list;
  1257. /* FIXME interlacing + spatial direct uses wrong colocated block positions */
  1258. /* ref = min(neighbors) */
  1259. for(list=0; list<2; list++){
  1260. int refa = h->ref_cache[list][scan8[0] - 1];
  1261. int refb = h->ref_cache[list][scan8[0] - 8];
  1262. int refc = h->ref_cache[list][scan8[0] - 8 + 4];
  1263. if(refc == -2)
  1264. refc = h->ref_cache[list][scan8[0] - 8 - 1];
  1265. ref[list] = refa;
  1266. if(ref[list] < 0 || (refb < ref[list] && refb >= 0))
  1267. ref[list] = refb;
  1268. if(ref[list] < 0 || (refc < ref[list] && refc >= 0))
  1269. ref[list] = refc;
  1270. if(ref[list] < 0)
  1271. ref[list] = -1;
  1272. }
  1273. if(ref[0] < 0 && ref[1] < 0){
  1274. ref[0] = ref[1] = 0;
  1275. mv[0][0] = mv[0][1] =
  1276. mv[1][0] = mv[1][1] = 0;
  1277. }else{
  1278. for(list=0; list<2; list++){
  1279. if(ref[list] >= 0)
  1280. pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
  1281. else
  1282. mv[list][0] = mv[list][1] = 0;
  1283. }
  1284. }
  1285. if(ref[1] < 0){
  1286. *mb_type &= ~MB_TYPE_P0L1;
  1287. sub_mb_type &= ~MB_TYPE_P0L1;
  1288. }else if(ref[0] < 0){
  1289. *mb_type &= ~MB_TYPE_P0L0;
  1290. sub_mb_type &= ~MB_TYPE_P0L0;
  1291. }
  1292. if(IS_16X16(*mb_type)){
  1293. int a=0, b=0;
  1294. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  1295. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  1296. if(!IS_INTRA(mb_type_col)
  1297. && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
  1298. || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
  1299. && (h->x264_build>33 || !h->x264_build)))){
  1300. if(ref[0] > 0)
  1301. a= pack16to32(mv[0][0],mv[0][1]);
  1302. if(ref[1] > 0)
  1303. b= pack16to32(mv[1][0],mv[1][1]);
  1304. }else{
  1305. a= pack16to32(mv[0][0],mv[0][1]);
  1306. b= pack16to32(mv[1][0],mv[1][1]);
  1307. }
  1308. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
  1309. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
  1310. }else{
  1311. for(i8=0; i8<4; i8++){
  1312. const int x8 = i8&1;
  1313. const int y8 = i8>>1;
  1314. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1315. continue;
  1316. h->sub_mb_type[i8] = sub_mb_type;
  1317. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1318. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1319. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  1320. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  1321. /* col_zero_flag */
  1322. if(!IS_INTRA(mb_type_col) && ( l1ref0[x8 + y8*h->b8_stride] == 0
  1323. || (l1ref0[x8 + y8*h->b8_stride] < 0 && l1ref1[x8 + y8*h->b8_stride] == 0
  1324. && (h->x264_build>33 || !h->x264_build)))){
  1325. const int16_t (*l1mv)[2]= l1ref0[x8 + y8*h->b8_stride] == 0 ? l1mv0 : l1mv1;
  1326. if(IS_SUB_8X8(sub_mb_type)){
  1327. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1328. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  1329. if(ref[0] == 0)
  1330. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1331. if(ref[1] == 0)
  1332. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1333. }
  1334. }else
  1335. for(i4=0; i4<4; i4++){
  1336. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1337. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  1338. if(ref[0] == 0)
  1339. *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
  1340. if(ref[1] == 0)
  1341. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
  1342. }
  1343. }
  1344. }
  1345. }
  1346. }
  1347. }else{ /* direct temporal mv pred */
  1348. const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
  1349. const int *dist_scale_factor = h->dist_scale_factor;
  1350. if(FRAME_MBAFF){
  1351. if(IS_INTERLACED(*mb_type)){
  1352. map_col_to_list0[0] = h->map_col_to_list0_field[0];
  1353. map_col_to_list0[1] = h->map_col_to_list0_field[1];
  1354. dist_scale_factor = h->dist_scale_factor_field;
  1355. }
  1356. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col)){
  1357. /* FIXME assumes direct_8x8_inference == 1 */
  1358. const int pair_xy = s->mb_x + (s->mb_y&~1)*s->mb_stride;
  1359. int mb_types_col[2];
  1360. int y_shift;
  1361. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1
  1362. | (is_b8x8 ? 0 : MB_TYPE_DIRECT2)
  1363. | (*mb_type & MB_TYPE_INTERLACED);
  1364. sub_mb_type = MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_16x16;
  1365. if(IS_INTERLACED(*mb_type)){
  1366. /* frame to field scaling */
  1367. mb_types_col[0] = h->ref_list[1][0].mb_type[pair_xy];
  1368. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1369. if(s->mb_y&1){
  1370. l1ref0 -= 2*h->b8_stride;
  1371. l1ref1 -= 2*h->b8_stride;
  1372. l1mv0 -= 4*h->b_stride;
  1373. l1mv1 -= 4*h->b_stride;
  1374. }
  1375. y_shift = 0;
  1376. if( (mb_types_col[0] & MB_TYPE_16x16_OR_INTRA)
  1377. && (mb_types_col[1] & MB_TYPE_16x16_OR_INTRA)
  1378. && !is_b8x8)
  1379. *mb_type |= MB_TYPE_16x8;
  1380. else
  1381. *mb_type |= MB_TYPE_8x8;
  1382. }else{
  1383. /* field to frame scaling */
  1384. /* col_mb_y = (mb_y&~1) + (topAbsDiffPOC < bottomAbsDiffPOC ? 0 : 1)
  1385. * but in MBAFF, top and bottom POC are equal */
  1386. int dy = (s->mb_y&1) ? 1 : 2;
  1387. mb_types_col[0] =
  1388. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1389. l1ref0 += dy*h->b8_stride;
  1390. l1ref1 += dy*h->b8_stride;
  1391. l1mv0 += 2*dy*h->b_stride;
  1392. l1mv1 += 2*dy*h->b_stride;
  1393. y_shift = 2;
  1394. if((mb_types_col[0] & (MB_TYPE_16x16_OR_INTRA|MB_TYPE_16x8))
  1395. && !is_b8x8)
  1396. *mb_type |= MB_TYPE_16x16;
  1397. else
  1398. *mb_type |= MB_TYPE_8x8;
  1399. }
  1400. for(i8=0; i8<4; i8++){
  1401. const int x8 = i8&1;
  1402. const int y8 = i8>>1;
  1403. int ref0, scale;
  1404. const int16_t (*l1mv)[2]= l1mv0;
  1405. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1406. continue;
  1407. h->sub_mb_type[i8] = sub_mb_type;
  1408. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1409. if(IS_INTRA(mb_types_col[y8])){
  1410. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1411. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1412. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1413. continue;
  1414. }
  1415. ref0 = l1ref0[x8 + (y8*2>>y_shift)*h->b8_stride];
  1416. if(ref0 >= 0)
  1417. ref0 = map_col_to_list0[0][ref0*2>>y_shift];
  1418. else{
  1419. ref0 = map_col_to_list0[1][l1ref1[x8 + (y8*2>>y_shift)*h->b8_stride]*2>>y_shift];
  1420. l1mv= l1mv1;
  1421. }
  1422. scale = dist_scale_factor[ref0];
  1423. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1424. {
  1425. const int16_t *mv_col = l1mv[x8*3 + (y8*6>>y_shift)*h->b_stride];
  1426. int my_col = (mv_col[1]<<y_shift)/2;
  1427. int mx = (scale * mv_col[0] + 128) >> 8;
  1428. int my = (scale * my_col + 128) >> 8;
  1429. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1430. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
  1431. }
  1432. }
  1433. return;
  1434. }
  1435. }
  1436. /* one-to-one mv scaling */
  1437. if(IS_16X16(*mb_type)){
  1438. int ref, mv0, mv1;
  1439. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  1440. if(IS_INTRA(mb_type_col)){
  1441. ref=mv0=mv1=0;
  1442. }else{
  1443. const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0]]
  1444. : map_col_to_list0[1][l1ref1[0]];
  1445. const int scale = dist_scale_factor[ref0];
  1446. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  1447. int mv_l0[2];
  1448. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1449. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1450. ref= ref0;
  1451. mv0= pack16to32(mv_l0[0],mv_l0[1]);
  1452. mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  1453. }
  1454. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
  1455. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
  1456. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
  1457. }else{
  1458. for(i8=0; i8<4; i8++){
  1459. const int x8 = i8&1;
  1460. const int y8 = i8>>1;
  1461. int ref0, scale;
  1462. const int16_t (*l1mv)[2]= l1mv0;
  1463. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1464. continue;
  1465. h->sub_mb_type[i8] = sub_mb_type;
  1466. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1467. if(IS_INTRA(mb_type_col)){
  1468. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1469. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1470. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1471. continue;
  1472. }
  1473. ref0 = l1ref0[x8 + y8*h->b8_stride];
  1474. if(ref0 >= 0)
  1475. ref0 = map_col_to_list0[0][ref0];
  1476. else{
  1477. ref0 = map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]];
  1478. l1mv= l1mv1;
  1479. }
  1480. scale = dist_scale_factor[ref0];
  1481. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1482. if(IS_SUB_8X8(sub_mb_type)){
  1483. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1484. int mx = (scale * mv_col[0] + 128) >> 8;
  1485. int my = (scale * mv_col[1] + 128) >> 8;
  1486. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1487. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
  1488. }else
  1489. for(i4=0; i4<4; i4++){
  1490. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1491. int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
  1492. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1493. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1494. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
  1495. pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  1496. }
  1497. }
  1498. }
  1499. }
  1500. }
  1501. static inline void write_back_motion(H264Context *h, int mb_type){
  1502. MpegEncContext * const s = &h->s;
  1503. const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1504. const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1505. int list;
  1506. if(!USES_LIST(mb_type, 0))
  1507. fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, h->b8_stride, (uint8_t)LIST_NOT_USED, 1);
  1508. for(list=0; list<h->list_count; list++){
  1509. int y;
  1510. if(!USES_LIST(mb_type, list))
  1511. continue;
  1512. for(y=0; y<4; y++){
  1513. *(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];
  1514. *(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];
  1515. }
  1516. if( h->pps.cabac ) {
  1517. if(IS_SKIP(mb_type))
  1518. fill_rectangle(h->mvd_table[list][b_xy], 4, 4, h->b_stride, 0, 4);
  1519. else
  1520. for(y=0; y<4; y++){
  1521. *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
  1522. *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
  1523. }
  1524. }
  1525. {
  1526. int8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
  1527. ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]];
  1528. ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]];
  1529. ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]];
  1530. ref_index[1+1*h->b8_stride]= h->ref_cache[list][scan8[12]];
  1531. }
  1532. }
  1533. if(h->slice_type == B_TYPE && h->pps.cabac){
  1534. if(IS_8X8(mb_type)){
  1535. uint8_t *direct_table = &h->direct_table[b8_xy];
  1536. direct_table[1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
  1537. direct_table[0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
  1538. direct_table[1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
  1539. }
  1540. }
  1541. }
  1542. /**
  1543. * Decodes a network abstraction layer unit.
  1544. * @param consumed is the number of bytes used as input
  1545. * @param length is the length of the array
  1546. * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp tailing?
  1547. * @returns decoded bytes, might be src+1 if no escapes
  1548. */
  1549. static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){
  1550. int i, si, di;
  1551. uint8_t *dst;
  1552. // src[0]&0x80; //forbidden bit
  1553. h->nal_ref_idc= src[0]>>5;
  1554. h->nal_unit_type= src[0]&0x1F;
  1555. src++; length--;
  1556. #if 0
  1557. for(i=0; i<length; i++)
  1558. printf("%2X ", src[i]);
  1559. #endif
  1560. for(i=0; i+1<length; i+=2){
  1561. if(src[i]) continue;
  1562. if(i>0 && src[i-1]==0) i--;
  1563. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  1564. if(src[i+2]!=3){
  1565. /* startcode, so we must be past the end */
  1566. length=i;
  1567. }
  1568. break;
  1569. }
  1570. }
  1571. if(i>=length-1){ //no escaped 0
  1572. *dst_length= length;
  1573. *consumed= length+1; //+1 for the header
  1574. return src;
  1575. }
  1576. h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length);
  1577. dst= h->rbsp_buffer;
  1578. if (dst == NULL){
  1579. return NULL;
  1580. }
  1581. //printf("decoding esc\n");
  1582. si=di=0;
  1583. while(si<length){
  1584. //remove escapes (very rare 1:2^22)
  1585. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  1586. if(src[si+2]==3){ //escape
  1587. dst[di++]= 0;
  1588. dst[di++]= 0;
  1589. si+=3;
  1590. continue;
  1591. }else //next start code
  1592. break;
  1593. }
  1594. dst[di++]= src[si++];
  1595. }
  1596. *dst_length= di;
  1597. *consumed= si + 1;//+1 for the header
  1598. //FIXME store exact number of bits in the getbitcontext (its needed for decoding)
  1599. return dst;
  1600. }
  1601. /**
  1602. * identifies the exact end of the bitstream
  1603. * @return the length of the trailing, or 0 if damaged
  1604. */
  1605. static int decode_rbsp_trailing(H264Context *h, uint8_t *src){
  1606. int v= *src;
  1607. int r;
  1608. tprintf(h->s.avctx, "rbsp trailing %X\n", v);
  1609. for(r=1; r<9; r++){
  1610. if(v&1) return r;
  1611. v>>=1;
  1612. }
  1613. return 0;
  1614. }
  1615. /**
  1616. * idct tranforms the 16 dc values and dequantize them.
  1617. * @param qp quantization parameter
  1618. */
  1619. static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1620. #define stride 16
  1621. int i;
  1622. int temp[16]; //FIXME check if this is a good idea
  1623. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1624. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1625. //memset(block, 64, 2*256);
  1626. //return;
  1627. for(i=0; i<4; i++){
  1628. const int offset= y_offset[i];
  1629. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1630. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1631. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1632. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1633. temp[4*i+0]= z0+z3;
  1634. temp[4*i+1]= z1+z2;
  1635. temp[4*i+2]= z1-z2;
  1636. temp[4*i+3]= z0-z3;
  1637. }
  1638. for(i=0; i<4; i++){
  1639. const int offset= x_offset[i];
  1640. const int z0= temp[4*0+i] + temp[4*2+i];
  1641. const int z1= temp[4*0+i] - temp[4*2+i];
  1642. const int z2= temp[4*1+i] - temp[4*3+i];
  1643. const int z3= temp[4*1+i] + temp[4*3+i];
  1644. block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_resdual
  1645. block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
  1646. block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
  1647. block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
  1648. }
  1649. }
  1650. #if 0
  1651. /**
  1652. * dct tranforms the 16 dc values.
  1653. * @param qp quantization parameter ??? FIXME
  1654. */
  1655. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  1656. // const int qmul= dequant_coeff[qp][0];
  1657. int i;
  1658. int temp[16]; //FIXME check if this is a good idea
  1659. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1660. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1661. for(i=0; i<4; i++){
  1662. const int offset= y_offset[i];
  1663. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1664. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1665. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1666. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1667. temp[4*i+0]= z0+z3;
  1668. temp[4*i+1]= z1+z2;
  1669. temp[4*i+2]= z1-z2;
  1670. temp[4*i+3]= z0-z3;
  1671. }
  1672. for(i=0; i<4; i++){
  1673. const int offset= x_offset[i];
  1674. const int z0= temp[4*0+i] + temp[4*2+i];
  1675. const int z1= temp[4*0+i] - temp[4*2+i];
  1676. const int z2= temp[4*1+i] - temp[4*3+i];
  1677. const int z3= temp[4*1+i] + temp[4*3+i];
  1678. block[stride*0 +offset]= (z0 + z3)>>1;
  1679. block[stride*2 +offset]= (z1 + z2)>>1;
  1680. block[stride*8 +offset]= (z1 - z2)>>1;
  1681. block[stride*10+offset]= (z0 - z3)>>1;
  1682. }
  1683. }
  1684. #endif
  1685. #undef xStride
  1686. #undef stride
  1687. static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1688. const int stride= 16*2;
  1689. const int xStride= 16;
  1690. int a,b,c,d,e;
  1691. a= block[stride*0 + xStride*0];
  1692. b= block[stride*0 + xStride*1];
  1693. c= block[stride*1 + xStride*0];
  1694. d= block[stride*1 + xStride*1];
  1695. e= a-b;
  1696. a= a+b;
  1697. b= c-d;
  1698. c= c+d;
  1699. block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
  1700. block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
  1701. block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
  1702. block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
  1703. }
  1704. #if 0
  1705. static void chroma_dc_dct_c(DCTELEM *block){
  1706. const int stride= 16*2;
  1707. const int xStride= 16;
  1708. int a,b,c,d,e;
  1709. a= block[stride*0 + xStride*0];
  1710. b= block[stride*0 + xStride*1];
  1711. c= block[stride*1 + xStride*0];
  1712. d= block[stride*1 + xStride*1];
  1713. e= a-b;
  1714. a= a+b;
  1715. b= c-d;
  1716. c= c+d;
  1717. block[stride*0 + xStride*0]= (a+c);
  1718. block[stride*0 + xStride*1]= (e+b);
  1719. block[stride*1 + xStride*0]= (a-c);
  1720. block[stride*1 + xStride*1]= (e-b);
  1721. }
  1722. #endif
  1723. /**
  1724. * gets the chroma qp.
  1725. */
  1726. static inline int get_chroma_qp(int chroma_qp_index_offset, int qscale){
  1727. return chroma_qp[av_clip(qscale + chroma_qp_index_offset, 0, 51)];
  1728. }
  1729. //FIXME need to check that this doesnt overflow signed 32 bit for low qp, i am not sure, it's very close
  1730. //FIXME check that gcc inlines this (and optimizes intra & separate_dc stuff away)
  1731. static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int separate_dc){
  1732. int i;
  1733. const int * const quant_table= quant_coeff[qscale];
  1734. const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
  1735. const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
  1736. const unsigned int threshold2= (threshold1<<1);
  1737. int last_non_zero;
  1738. if(separate_dc){
  1739. if(qscale<=18){
  1740. //avoid overflows
  1741. const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
  1742. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
  1743. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1744. int level= block[0]*quant_coeff[qscale+18][0];
  1745. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1746. if(level>0){
  1747. level= (dc_bias + level)>>(QUANT_SHIFT-2);
  1748. block[0]= level;
  1749. }else{
  1750. level= (dc_bias - level)>>(QUANT_SHIFT-2);
  1751. block[0]= -level;
  1752. }
  1753. // last_non_zero = i;
  1754. }else{
  1755. block[0]=0;
  1756. }
  1757. }else{
  1758. const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
  1759. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
  1760. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1761. int level= block[0]*quant_table[0];
  1762. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1763. if(level>0){
  1764. level= (dc_bias + level)>>(QUANT_SHIFT+1);
  1765. block[0]= level;
  1766. }else{
  1767. level= (dc_bias - level)>>(QUANT_SHIFT+1);
  1768. block[0]= -level;
  1769. }
  1770. // last_non_zero = i;
  1771. }else{
  1772. block[0]=0;
  1773. }
  1774. }
  1775. last_non_zero= 0;
  1776. i=1;
  1777. }else{
  1778. last_non_zero= -1;
  1779. i=0;
  1780. }
  1781. for(; i<16; i++){
  1782. const int j= scantable[i];
  1783. int level= block[j]*quant_table[j];
  1784. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  1785. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  1786. if(((unsigned)(level+threshold1))>threshold2){
  1787. if(level>0){
  1788. level= (bias + level)>>QUANT_SHIFT;
  1789. block[j]= level;
  1790. }else{
  1791. level= (bias - level)>>QUANT_SHIFT;
  1792. block[j]= -level;
  1793. }
  1794. last_non_zero = i;
  1795. }else{
  1796. block[j]=0;
  1797. }
  1798. }
  1799. return last_non_zero;
  1800. }
  1801. static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){
  1802. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1803. ((uint32_t*)(src+0*stride))[0]= a;
  1804. ((uint32_t*)(src+1*stride))[0]= a;
  1805. ((uint32_t*)(src+2*stride))[0]= a;
  1806. ((uint32_t*)(src+3*stride))[0]= a;
  1807. }
  1808. static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){
  1809. ((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101;
  1810. ((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101;
  1811. ((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101;
  1812. ((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101;
  1813. }
  1814. static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1815. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
  1816. + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
  1817. ((uint32_t*)(src+0*stride))[0]=
  1818. ((uint32_t*)(src+1*stride))[0]=
  1819. ((uint32_t*)(src+2*stride))[0]=
  1820. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1821. }
  1822. static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1823. const int dc= ( src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
  1824. ((uint32_t*)(src+0*stride))[0]=
  1825. ((uint32_t*)(src+1*stride))[0]=
  1826. ((uint32_t*)(src+2*stride))[0]=
  1827. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1828. }
  1829. static void pred4x4_top_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1830. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2;
  1831. ((uint32_t*)(src+0*stride))[0]=
  1832. ((uint32_t*)(src+1*stride))[0]=
  1833. ((uint32_t*)(src+2*stride))[0]=
  1834. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1835. }
  1836. static void pred4x4_128_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1837. ((uint32_t*)(src+0*stride))[0]=
  1838. ((uint32_t*)(src+1*stride))[0]=
  1839. ((uint32_t*)(src+2*stride))[0]=
  1840. ((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U;
  1841. }
  1842. #define LOAD_TOP_RIGHT_EDGE\
  1843. const int t4= topright[0];\
  1844. const int t5= topright[1];\
  1845. const int t6= topright[2];\
  1846. const int t7= topright[3];\
  1847. #define LOAD_LEFT_EDGE\
  1848. const int l0= src[-1+0*stride];\
  1849. const int l1= src[-1+1*stride];\
  1850. const int l2= src[-1+2*stride];\
  1851. const int l3= src[-1+3*stride];\
  1852. #define LOAD_TOP_EDGE\
  1853. const int t0= src[ 0-1*stride];\
  1854. const int t1= src[ 1-1*stride];\
  1855. const int t2= src[ 2-1*stride];\
  1856. const int t3= src[ 3-1*stride];\
  1857. static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){
  1858. const int lt= src[-1-1*stride];
  1859. LOAD_TOP_EDGE
  1860. LOAD_LEFT_EDGE
  1861. src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
  1862. src[0+2*stride]=
  1863. src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
  1864. src[0+1*stride]=
  1865. src[1+2*stride]=
  1866. src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
  1867. src[0+0*stride]=
  1868. src[1+1*stride]=
  1869. src[2+2*stride]=
  1870. src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1871. src[1+0*stride]=
  1872. src[2+1*stride]=
  1873. src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1874. src[2+0*stride]=
  1875. src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1876. src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1877. }
  1878. static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){
  1879. LOAD_TOP_EDGE
  1880. LOAD_TOP_RIGHT_EDGE
  1881. // LOAD_LEFT_EDGE
  1882. src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
  1883. src[1+0*stride]=
  1884. src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
  1885. src[2+0*stride]=
  1886. src[1+1*stride]=
  1887. src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
  1888. src[3+0*stride]=
  1889. src[2+1*stride]=
  1890. src[1+2*stride]=
  1891. src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
  1892. src[3+1*stride]=
  1893. src[2+2*stride]=
  1894. src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
  1895. src[3+2*stride]=
  1896. src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
  1897. src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
  1898. }
  1899. static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
  1900. const int lt= src[-1-1*stride];
  1901. LOAD_TOP_EDGE
  1902. LOAD_LEFT_EDGE
  1903. const __attribute__((unused)) int unu= l3;
  1904. src[0+0*stride]=
  1905. src[1+2*stride]=(lt + t0 + 1)>>1;
  1906. src[1+0*stride]=
  1907. src[2+2*stride]=(t0 + t1 + 1)>>1;
  1908. src[2+0*stride]=
  1909. src[3+2*stride]=(t1 + t2 + 1)>>1;
  1910. src[3+0*stride]=(t2 + t3 + 1)>>1;
  1911. src[0+1*stride]=
  1912. src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1913. src[1+1*stride]=
  1914. src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1915. src[2+1*stride]=
  1916. src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1917. src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1918. src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1919. src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1920. }
  1921. static void pred4x4_vertical_left_c(uint8_t *src, uint8_t *topright, int stride){
  1922. LOAD_TOP_EDGE
  1923. LOAD_TOP_RIGHT_EDGE
  1924. const __attribute__((unused)) int unu= t7;
  1925. src[0+0*stride]=(t0 + t1 + 1)>>1;
  1926. src[1+0*stride]=
  1927. src[0+2*stride]=(t1 + t2 + 1)>>1;
  1928. src[2+0*stride]=
  1929. src[1+2*stride]=(t2 + t3 + 1)>>1;
  1930. src[3+0*stride]=
  1931. src[2+2*stride]=(t3 + t4+ 1)>>1;
  1932. src[3+2*stride]=(t4 + t5+ 1)>>1;
  1933. src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1934. src[1+1*stride]=
  1935. src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1936. src[2+1*stride]=
  1937. src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
  1938. src[3+1*stride]=
  1939. src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
  1940. src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
  1941. }
  1942. static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){
  1943. LOAD_LEFT_EDGE
  1944. src[0+0*stride]=(l0 + l1 + 1)>>1;
  1945. src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1946. src[2+0*stride]=
  1947. src[0+1*stride]=(l1 + l2 + 1)>>1;
  1948. src[3+0*stride]=
  1949. src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1950. src[2+1*stride]=
  1951. src[0+2*stride]=(l2 + l3 + 1)>>1;
  1952. src[3+1*stride]=
  1953. src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
  1954. src[3+2*stride]=
  1955. src[1+3*stride]=
  1956. src[0+3*stride]=
  1957. src[2+2*stride]=
  1958. src[2+3*stride]=
  1959. src[3+3*stride]=l3;
  1960. }
  1961. static void pred4x4_horizontal_down_c(uint8_t *src, uint8_t *topright, int stride){
  1962. const int lt= src[-1-1*stride];
  1963. LOAD_TOP_EDGE
  1964. LOAD_LEFT_EDGE
  1965. const __attribute__((unused)) int unu= t3;
  1966. src[0+0*stride]=
  1967. src[2+1*stride]=(lt + l0 + 1)>>1;
  1968. src[1+0*stride]=
  1969. src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1970. src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1971. src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1972. src[0+1*stride]=
  1973. src[2+2*stride]=(l0 + l1 + 1)>>1;
  1974. src[1+1*stride]=
  1975. src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1976. src[0+2*stride]=
  1977. src[2+3*stride]=(l1 + l2+ 1)>>1;
  1978. src[1+2*stride]=
  1979. src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1980. src[0+3*stride]=(l2 + l3 + 1)>>1;
  1981. src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1982. }
  1983. void ff_pred16x16_vertical_c(uint8_t *src, int stride){
  1984. int i;
  1985. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1986. const uint32_t b= ((uint32_t*)(src-stride))[1];
  1987. const uint32_t c= ((uint32_t*)(src-stride))[2];
  1988. const uint32_t d= ((uint32_t*)(src-stride))[3];
  1989. for(i=0; i<16; i++){
  1990. ((uint32_t*)(src+i*stride))[0]= a;
  1991. ((uint32_t*)(src+i*stride))[1]= b;
  1992. ((uint32_t*)(src+i*stride))[2]= c;
  1993. ((uint32_t*)(src+i*stride))[3]= d;
  1994. }
  1995. }
  1996. void ff_pred16x16_horizontal_c(uint8_t *src, int stride){
  1997. int i;
  1998. for(i=0; i<16; i++){
  1999. ((uint32_t*)(src+i*stride))[0]=
  2000. ((uint32_t*)(src+i*stride))[1]=
  2001. ((uint32_t*)(src+i*stride))[2]=
  2002. ((uint32_t*)(src+i*stride))[3]= src[-1+i*stride]*0x01010101;
  2003. }
  2004. }
  2005. void ff_pred16x16_dc_c(uint8_t *src, int stride){
  2006. int i, dc=0;
  2007. for(i=0;i<16; i++){
  2008. dc+= src[-1+i*stride];
  2009. }
  2010. for(i=0;i<16; i++){
  2011. dc+= src[i-stride];
  2012. }
  2013. dc= 0x01010101*((dc + 16)>>5);
  2014. for(i=0; i<16; i++){
  2015. ((uint32_t*)(src+i*stride))[0]=
  2016. ((uint32_t*)(src+i*stride))[1]=
  2017. ((uint32_t*)(src+i*stride))[2]=
  2018. ((uint32_t*)(src+i*stride))[3]= dc;
  2019. }
  2020. }
  2021. static void pred16x16_left_dc_c(uint8_t *src, int stride){
  2022. int i, dc=0;
  2023. for(i=0;i<16; i++){
  2024. dc+= src[-1+i*stride];
  2025. }
  2026. dc= 0x01010101*((dc + 8)>>4);
  2027. for(i=0; i<16; i++){
  2028. ((uint32_t*)(src+i*stride))[0]=
  2029. ((uint32_t*)(src+i*stride))[1]=
  2030. ((uint32_t*)(src+i*stride))[2]=
  2031. ((uint32_t*)(src+i*stride))[3]= dc;
  2032. }
  2033. }
  2034. static void pred16x16_top_dc_c(uint8_t *src, int stride){
  2035. int i, dc=0;
  2036. for(i=0;i<16; i++){
  2037. dc+= src[i-stride];
  2038. }
  2039. dc= 0x01010101*((dc + 8)>>4);
  2040. for(i=0; i<16; i++){
  2041. ((uint32_t*)(src+i*stride))[0]=
  2042. ((uint32_t*)(src+i*stride))[1]=
  2043. ((uint32_t*)(src+i*stride))[2]=
  2044. ((uint32_t*)(src+i*stride))[3]= dc;
  2045. }
  2046. }
  2047. void ff_pred16x16_128_dc_c(uint8_t *src, int stride){
  2048. int i;
  2049. for(i=0; i<16; i++){
  2050. ((uint32_t*)(src+i*stride))[0]=
  2051. ((uint32_t*)(src+i*stride))[1]=
  2052. ((uint32_t*)(src+i*stride))[2]=
  2053. ((uint32_t*)(src+i*stride))[3]= 0x01010101U*128U;
  2054. }
  2055. }
  2056. static inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){
  2057. int i, j, k;
  2058. int a;
  2059. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  2060. const uint8_t * const src0 = src+7-stride;
  2061. const uint8_t *src1 = src+8*stride-1;
  2062. const uint8_t *src2 = src1-2*stride; // == src+6*stride-1;
  2063. int H = src0[1] - src0[-1];
  2064. int V = src1[0] - src2[ 0];
  2065. for(k=2; k<=8; ++k) {
  2066. src1 += stride; src2 -= stride;
  2067. H += k*(src0[k] - src0[-k]);
  2068. V += k*(src1[0] - src2[ 0]);
  2069. }
  2070. if(svq3){
  2071. H = ( 5*(H/4) ) / 16;
  2072. V = ( 5*(V/4) ) / 16;
  2073. /* required for 100% accuracy */
  2074. i = H; H = V; V = i;
  2075. }else{
  2076. H = ( 5*H+32 ) >> 6;
  2077. V = ( 5*V+32 ) >> 6;
  2078. }
  2079. a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
  2080. for(j=16; j>0; --j) {
  2081. int b = a;
  2082. a += V;
  2083. for(i=-16; i<0; i+=4) {
  2084. src[16+i] = cm[ (b ) >> 5 ];
  2085. src[17+i] = cm[ (b+ H) >> 5 ];
  2086. src[18+i] = cm[ (b+2*H) >> 5 ];
  2087. src[19+i] = cm[ (b+3*H) >> 5 ];
  2088. b += 4*H;
  2089. }
  2090. src += stride;
  2091. }
  2092. }
  2093. void ff_pred16x16_plane_c(uint8_t *src, int stride){
  2094. pred16x16_plane_compat_c(src, stride, 0);
  2095. }
  2096. void ff_pred8x8_vertical_c(uint8_t *src, int stride){
  2097. int i;
  2098. const uint32_t a= ((uint32_t*)(src-stride))[0];
  2099. const uint32_t b= ((uint32_t*)(src-stride))[1];
  2100. for(i=0; i<8; i++){
  2101. ((uint32_t*)(src+i*stride))[0]= a;
  2102. ((uint32_t*)(src+i*stride))[1]= b;
  2103. }
  2104. }
  2105. void ff_pred8x8_horizontal_c(uint8_t *src, int stride){
  2106. int i;
  2107. for(i=0; i<8; i++){
  2108. ((uint32_t*)(src+i*stride))[0]=
  2109. ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101;
  2110. }
  2111. }
  2112. void ff_pred8x8_128_dc_c(uint8_t *src, int stride){
  2113. int i;
  2114. for(i=0; i<8; i++){
  2115. ((uint32_t*)(src+i*stride))[0]=
  2116. ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
  2117. }
  2118. }
  2119. static void pred8x8_left_dc_c(uint8_t *src, int stride){
  2120. int i;
  2121. int dc0, dc2;
  2122. dc0=dc2=0;
  2123. for(i=0;i<4; i++){
  2124. dc0+= src[-1+i*stride];
  2125. dc2+= src[-1+(i+4)*stride];
  2126. }
  2127. dc0= 0x01010101*((dc0 + 2)>>2);
  2128. dc2= 0x01010101*((dc2 + 2)>>2);
  2129. for(i=0; i<4; i++){
  2130. ((uint32_t*)(src+i*stride))[0]=
  2131. ((uint32_t*)(src+i*stride))[1]= dc0;
  2132. }
  2133. for(i=4; i<8; i++){
  2134. ((uint32_t*)(src+i*stride))[0]=
  2135. ((uint32_t*)(src+i*stride))[1]= dc2;
  2136. }
  2137. }
  2138. static void pred8x8_top_dc_c(uint8_t *src, int stride){
  2139. int i;
  2140. int dc0, dc1;
  2141. dc0=dc1=0;
  2142. for(i=0;i<4; i++){
  2143. dc0+= src[i-stride];
  2144. dc1+= src[4+i-stride];
  2145. }
  2146. dc0= 0x01010101*((dc0 + 2)>>2);
  2147. dc1= 0x01010101*((dc1 + 2)>>2);
  2148. for(i=0; i<4; i++){
  2149. ((uint32_t*)(src+i*stride))[0]= dc0;
  2150. ((uint32_t*)(src+i*stride))[1]= dc1;
  2151. }
  2152. for(i=4; i<8; i++){
  2153. ((uint32_t*)(src+i*stride))[0]= dc0;
  2154. ((uint32_t*)(src+i*stride))[1]= dc1;
  2155. }
  2156. }
  2157. void ff_pred8x8_dc_c(uint8_t *src, int stride){
  2158. int i;
  2159. int dc0, dc1, dc2, dc3;
  2160. dc0=dc1=dc2=0;
  2161. for(i=0;i<4; i++){
  2162. dc0+= src[-1+i*stride] + src[i-stride];
  2163. dc1+= src[4+i-stride];
  2164. dc2+= src[-1+(i+4)*stride];
  2165. }
  2166. dc3= 0x01010101*((dc1 + dc2 + 4)>>3);
  2167. dc0= 0x01010101*((dc0 + 4)>>3);
  2168. dc1= 0x01010101*((dc1 + 2)>>2);
  2169. dc2= 0x01010101*((dc2 + 2)>>2);
  2170. for(i=0; i<4; i++){
  2171. ((uint32_t*)(src+i*stride))[0]= dc0;
  2172. ((uint32_t*)(src+i*stride))[1]= dc1;
  2173. }
  2174. for(i=4; i<8; i++){
  2175. ((uint32_t*)(src+i*stride))[0]= dc2;
  2176. ((uint32_t*)(src+i*stride))[1]= dc3;
  2177. }
  2178. }
  2179. void ff_pred8x8_plane_c(uint8_t *src, int stride){
  2180. int j, k;
  2181. int a;
  2182. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  2183. const uint8_t * const src0 = src+3-stride;
  2184. const uint8_t *src1 = src+4*stride-1;
  2185. const uint8_t *src2 = src1-2*stride; // == src+2*stride-1;
  2186. int H = src0[1] - src0[-1];
  2187. int V = src1[0] - src2[ 0];
  2188. for(k=2; k<=4; ++k) {
  2189. src1 += stride; src2 -= stride;
  2190. H += k*(src0[k] - src0[-k]);
  2191. V += k*(src1[0] - src2[ 0]);
  2192. }
  2193. H = ( 17*H+16 ) >> 5;
  2194. V = ( 17*V+16 ) >> 5;
  2195. a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
  2196. for(j=8; j>0; --j) {
  2197. int b = a;
  2198. a += V;
  2199. src[0] = cm[ (b ) >> 5 ];
  2200. src[1] = cm[ (b+ H) >> 5 ];
  2201. src[2] = cm[ (b+2*H) >> 5 ];
  2202. src[3] = cm[ (b+3*H) >> 5 ];
  2203. src[4] = cm[ (b+4*H) >> 5 ];
  2204. src[5] = cm[ (b+5*H) >> 5 ];
  2205. src[6] = cm[ (b+6*H) >> 5 ];
  2206. src[7] = cm[ (b+7*H) >> 5 ];
  2207. src += stride;
  2208. }
  2209. }
  2210. #define SRC(x,y) src[(x)+(y)*stride]
  2211. #define PL(y) \
  2212. const int l##y = (SRC(-1,y-1) + 2*SRC(-1,y) + SRC(-1,y+1) + 2) >> 2;
  2213. #define PREDICT_8x8_LOAD_LEFT \
  2214. const int l0 = ((has_topleft ? SRC(-1,-1) : SRC(-1,0)) \
  2215. + 2*SRC(-1,0) + SRC(-1,1) + 2) >> 2; \
  2216. PL(1) PL(2) PL(3) PL(4) PL(5) PL(6) \
  2217. const int l7 attribute_unused = (SRC(-1,6) + 3*SRC(-1,7) + 2) >> 2
  2218. #define PT(x) \
  2219. const int t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  2220. #define PREDICT_8x8_LOAD_TOP \
  2221. const int t0 = ((has_topleft ? SRC(-1,-1) : SRC(0,-1)) \
  2222. + 2*SRC(0,-1) + SRC(1,-1) + 2) >> 2; \
  2223. PT(1) PT(2) PT(3) PT(4) PT(5) PT(6) \
  2224. const int t7 attribute_unused = ((has_topright ? SRC(8,-1) : SRC(7,-1)) \
  2225. + 2*SRC(7,-1) + SRC(6,-1) + 2) >> 2
  2226. #define PTR(x) \
  2227. t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  2228. #define PREDICT_8x8_LOAD_TOPRIGHT \
  2229. int t8, t9, t10, t11, t12, t13, t14, t15; \
  2230. if(has_topright) { \
  2231. PTR(8) PTR(9) PTR(10) PTR(11) PTR(12) PTR(13) PTR(14) \
  2232. t15 = (SRC(14,-1) + 3*SRC(15,-1) + 2) >> 2; \
  2233. } else t8=t9=t10=t11=t12=t13=t14=t15= SRC(7,-1);
  2234. #define PREDICT_8x8_LOAD_TOPLEFT \
  2235. const int lt = (SRC(-1,0) + 2*SRC(-1,-1) + SRC(0,-1) + 2) >> 2
  2236. #define PREDICT_8x8_DC(v) \
  2237. int y; \
  2238. for( y = 0; y < 8; y++ ) { \
  2239. ((uint32_t*)src)[0] = \
  2240. ((uint32_t*)src)[1] = v; \
  2241. src += stride; \
  2242. }
  2243. static void pred8x8l_128_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2244. {
  2245. PREDICT_8x8_DC(0x80808080);
  2246. }
  2247. static void pred8x8l_left_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2248. {
  2249. PREDICT_8x8_LOAD_LEFT;
  2250. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7+4) >> 3) * 0x01010101;
  2251. PREDICT_8x8_DC(dc);
  2252. }
  2253. static void pred8x8l_top_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2254. {
  2255. PREDICT_8x8_LOAD_TOP;
  2256. const uint32_t dc = ((t0+t1+t2+t3+t4+t5+t6+t7+4) >> 3) * 0x01010101;
  2257. PREDICT_8x8_DC(dc);
  2258. }
  2259. static void pred8x8l_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2260. {
  2261. PREDICT_8x8_LOAD_LEFT;
  2262. PREDICT_8x8_LOAD_TOP;
  2263. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7
  2264. +t0+t1+t2+t3+t4+t5+t6+t7+8) >> 4) * 0x01010101;
  2265. PREDICT_8x8_DC(dc);
  2266. }
  2267. static void pred8x8l_horizontal_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2268. {
  2269. PREDICT_8x8_LOAD_LEFT;
  2270. #define ROW(y) ((uint32_t*)(src+y*stride))[0] =\
  2271. ((uint32_t*)(src+y*stride))[1] = 0x01010101 * l##y
  2272. ROW(0); ROW(1); ROW(2); ROW(3); ROW(4); ROW(5); ROW(6); ROW(7);
  2273. #undef ROW
  2274. }
  2275. static void pred8x8l_vertical_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2276. {
  2277. int y;
  2278. PREDICT_8x8_LOAD_TOP;
  2279. src[0] = t0;
  2280. src[1] = t1;
  2281. src[2] = t2;
  2282. src[3] = t3;
  2283. src[4] = t4;
  2284. src[5] = t5;
  2285. src[6] = t6;
  2286. src[7] = t7;
  2287. for( y = 1; y < 8; y++ )
  2288. *(uint64_t*)(src+y*stride) = *(uint64_t*)src;
  2289. }
  2290. static void pred8x8l_down_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2291. {
  2292. PREDICT_8x8_LOAD_TOP;
  2293. PREDICT_8x8_LOAD_TOPRIGHT;
  2294. SRC(0,0)= (t0 + 2*t1 + t2 + 2) >> 2;
  2295. SRC(0,1)=SRC(1,0)= (t1 + 2*t2 + t3 + 2) >> 2;
  2296. SRC(0,2)=SRC(1,1)=SRC(2,0)= (t2 + 2*t3 + t4 + 2) >> 2;
  2297. SRC(0,3)=SRC(1,2)=SRC(2,1)=SRC(3,0)= (t3 + 2*t4 + t5 + 2) >> 2;
  2298. SRC(0,4)=SRC(1,3)=SRC(2,2)=SRC(3,1)=SRC(4,0)= (t4 + 2*t5 + t6 + 2) >> 2;
  2299. SRC(0,5)=SRC(1,4)=SRC(2,3)=SRC(3,2)=SRC(4,1)=SRC(5,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2300. 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;
  2301. 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;
  2302. 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;
  2303. SRC(2,7)=SRC(3,6)=SRC(4,5)=SRC(5,4)=SRC(6,3)=SRC(7,2)= (t9 + 2*t10 + t11 + 2) >> 2;
  2304. SRC(3,7)=SRC(4,6)=SRC(5,5)=SRC(6,4)=SRC(7,3)= (t10 + 2*t11 + t12 + 2) >> 2;
  2305. SRC(4,7)=SRC(5,6)=SRC(6,5)=SRC(7,4)= (t11 + 2*t12 + t13 + 2) >> 2;
  2306. SRC(5,7)=SRC(6,6)=SRC(7,5)= (t12 + 2*t13 + t14 + 2) >> 2;
  2307. SRC(6,7)=SRC(7,6)= (t13 + 2*t14 + t15 + 2) >> 2;
  2308. SRC(7,7)= (t14 + 3*t15 + 2) >> 2;
  2309. }
  2310. static void pred8x8l_down_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2311. {
  2312. PREDICT_8x8_LOAD_TOP;
  2313. PREDICT_8x8_LOAD_LEFT;
  2314. PREDICT_8x8_LOAD_TOPLEFT;
  2315. SRC(0,7)= (l7 + 2*l6 + l5 + 2) >> 2;
  2316. SRC(0,6)=SRC(1,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2317. SRC(0,5)=SRC(1,6)=SRC(2,7)= (l5 + 2*l4 + l3 + 2) >> 2;
  2318. SRC(0,4)=SRC(1,5)=SRC(2,6)=SRC(3,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2319. SRC(0,3)=SRC(1,4)=SRC(2,5)=SRC(3,6)=SRC(4,7)= (l3 + 2*l2 + l1 + 2) >> 2;
  2320. SRC(0,2)=SRC(1,3)=SRC(2,4)=SRC(3,5)=SRC(4,6)=SRC(5,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2321. 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;
  2322. 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;
  2323. 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;
  2324. SRC(2,0)=SRC(3,1)=SRC(4,2)=SRC(5,3)=SRC(6,4)=SRC(7,5)= (t0 + 2*t1 + t2 + 2) >> 2;
  2325. SRC(3,0)=SRC(4,1)=SRC(5,2)=SRC(6,3)=SRC(7,4)= (t1 + 2*t2 + t3 + 2) >> 2;
  2326. SRC(4,0)=SRC(5,1)=SRC(6,2)=SRC(7,3)= (t2 + 2*t3 + t4 + 2) >> 2;
  2327. SRC(5,0)=SRC(6,1)=SRC(7,2)= (t3 + 2*t4 + t5 + 2) >> 2;
  2328. SRC(6,0)=SRC(7,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2329. SRC(7,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2330. }
  2331. static void pred8x8l_vertical_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2332. {
  2333. PREDICT_8x8_LOAD_TOP;
  2334. PREDICT_8x8_LOAD_LEFT;
  2335. PREDICT_8x8_LOAD_TOPLEFT;
  2336. SRC(0,6)= (l5 + 2*l4 + l3 + 2) >> 2;
  2337. SRC(0,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2338. SRC(0,4)=SRC(1,6)= (l3 + 2*l2 + l1 + 2) >> 2;
  2339. SRC(0,5)=SRC(1,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2340. SRC(0,2)=SRC(1,4)=SRC(2,6)= (l1 + 2*l0 + lt + 2) >> 2;
  2341. SRC(0,3)=SRC(1,5)=SRC(2,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2342. SRC(0,1)=SRC(1,3)=SRC(2,5)=SRC(3,7)= (l0 + 2*lt + t0 + 2) >> 2;
  2343. SRC(0,0)=SRC(1,2)=SRC(2,4)=SRC(3,6)= (lt + t0 + 1) >> 1;
  2344. SRC(1,1)=SRC(2,3)=SRC(3,5)=SRC(4,7)= (lt + 2*t0 + t1 + 2) >> 2;
  2345. SRC(1,0)=SRC(2,2)=SRC(3,4)=SRC(4,6)= (t0 + t1 + 1) >> 1;
  2346. SRC(2,1)=SRC(3,3)=SRC(4,5)=SRC(5,7)= (t0 + 2*t1 + t2 + 2) >> 2;
  2347. SRC(2,0)=SRC(3,2)=SRC(4,4)=SRC(5,6)= (t1 + t2 + 1) >> 1;
  2348. SRC(3,1)=SRC(4,3)=SRC(5,5)=SRC(6,7)= (t1 + 2*t2 + t3 + 2) >> 2;
  2349. SRC(3,0)=SRC(4,2)=SRC(5,4)=SRC(6,6)= (t2 + t3 + 1) >> 1;
  2350. SRC(4,1)=SRC(5,3)=SRC(6,5)=SRC(7,7)= (t2 + 2*t3 + t4 + 2) >> 2;
  2351. SRC(4,0)=SRC(5,2)=SRC(6,4)=SRC(7,6)= (t3 + t4 + 1) >> 1;
  2352. SRC(5,1)=SRC(6,3)=SRC(7,5)= (t3 + 2*t4 + t5 + 2) >> 2;
  2353. SRC(5,0)=SRC(6,2)=SRC(7,4)= (t4 + t5 + 1) >> 1;
  2354. SRC(6,1)=SRC(7,3)= (t4 + 2*t5 + t6 + 2) >> 2;
  2355. SRC(6,0)=SRC(7,2)= (t5 + t6 + 1) >> 1;
  2356. SRC(7,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2357. SRC(7,0)= (t6 + t7 + 1) >> 1;
  2358. }
  2359. static void pred8x8l_horizontal_down_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2360. {
  2361. PREDICT_8x8_LOAD_TOP;
  2362. PREDICT_8x8_LOAD_LEFT;
  2363. PREDICT_8x8_LOAD_TOPLEFT;
  2364. SRC(0,7)= (l6 + l7 + 1) >> 1;
  2365. SRC(1,7)= (l5 + 2*l6 + l7 + 2) >> 2;
  2366. SRC(0,6)=SRC(2,7)= (l5 + l6 + 1) >> 1;
  2367. SRC(1,6)=SRC(3,7)= (l4 + 2*l5 + l6 + 2) >> 2;
  2368. SRC(0,5)=SRC(2,6)=SRC(4,7)= (l4 + l5 + 1) >> 1;
  2369. SRC(1,5)=SRC(3,6)=SRC(5,7)= (l3 + 2*l4 + l5 + 2) >> 2;
  2370. SRC(0,4)=SRC(2,5)=SRC(4,6)=SRC(6,7)= (l3 + l4 + 1) >> 1;
  2371. SRC(1,4)=SRC(3,5)=SRC(5,6)=SRC(7,7)= (l2 + 2*l3 + l4 + 2) >> 2;
  2372. SRC(0,3)=SRC(2,4)=SRC(4,5)=SRC(6,6)= (l2 + l3 + 1) >> 1;
  2373. SRC(1,3)=SRC(3,4)=SRC(5,5)=SRC(7,6)= (l1 + 2*l2 + l3 + 2) >> 2;
  2374. SRC(0,2)=SRC(2,3)=SRC(4,4)=SRC(6,5)= (l1 + l2 + 1) >> 1;
  2375. SRC(1,2)=SRC(3,3)=SRC(5,4)=SRC(7,5)= (l0 + 2*l1 + l2 + 2) >> 2;
  2376. SRC(0,1)=SRC(2,2)=SRC(4,3)=SRC(6,4)= (l0 + l1 + 1) >> 1;
  2377. SRC(1,1)=SRC(3,2)=SRC(5,3)=SRC(7,4)= (lt + 2*l0 + l1 + 2) >> 2;
  2378. SRC(0,0)=SRC(2,1)=SRC(4,2)=SRC(6,3)= (lt + l0 + 1) >> 1;
  2379. SRC(1,0)=SRC(3,1)=SRC(5,2)=SRC(7,3)= (l0 + 2*lt + t0 + 2) >> 2;
  2380. SRC(2,0)=SRC(4,1)=SRC(6,2)= (t1 + 2*t0 + lt + 2) >> 2;
  2381. SRC(3,0)=SRC(5,1)=SRC(7,2)= (t2 + 2*t1 + t0 + 2) >> 2;
  2382. SRC(4,0)=SRC(6,1)= (t3 + 2*t2 + t1 + 2) >> 2;
  2383. SRC(5,0)=SRC(7,1)= (t4 + 2*t3 + t2 + 2) >> 2;
  2384. SRC(6,0)= (t5 + 2*t4 + t3 + 2) >> 2;
  2385. SRC(7,0)= (t6 + 2*t5 + t4 + 2) >> 2;
  2386. }
  2387. static void pred8x8l_vertical_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2388. {
  2389. PREDICT_8x8_LOAD_TOP;
  2390. PREDICT_8x8_LOAD_TOPRIGHT;
  2391. SRC(0,0)= (t0 + t1 + 1) >> 1;
  2392. SRC(0,1)= (t0 + 2*t1 + t2 + 2) >> 2;
  2393. SRC(0,2)=SRC(1,0)= (t1 + t2 + 1) >> 1;
  2394. SRC(0,3)=SRC(1,1)= (t1 + 2*t2 + t3 + 2) >> 2;
  2395. SRC(0,4)=SRC(1,2)=SRC(2,0)= (t2 + t3 + 1) >> 1;
  2396. SRC(0,5)=SRC(1,3)=SRC(2,1)= (t2 + 2*t3 + t4 + 2) >> 2;
  2397. SRC(0,6)=SRC(1,4)=SRC(2,2)=SRC(3,0)= (t3 + t4 + 1) >> 1;
  2398. SRC(0,7)=SRC(1,5)=SRC(2,3)=SRC(3,1)= (t3 + 2*t4 + t5 + 2) >> 2;
  2399. SRC(1,6)=SRC(2,4)=SRC(3,2)=SRC(4,0)= (t4 + t5 + 1) >> 1;
  2400. SRC(1,7)=SRC(2,5)=SRC(3,3)=SRC(4,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2401. SRC(2,6)=SRC(3,4)=SRC(4,2)=SRC(5,0)= (t5 + t6 + 1) >> 1;
  2402. SRC(2,7)=SRC(3,5)=SRC(4,3)=SRC(5,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2403. SRC(3,6)=SRC(4,4)=SRC(5,2)=SRC(6,0)= (t6 + t7 + 1) >> 1;
  2404. SRC(3,7)=SRC(4,5)=SRC(5,3)=SRC(6,1)= (t6 + 2*t7 + t8 + 2) >> 2;
  2405. SRC(4,6)=SRC(5,4)=SRC(6,2)=SRC(7,0)= (t7 + t8 + 1) >> 1;
  2406. SRC(4,7)=SRC(5,5)=SRC(6,3)=SRC(7,1)= (t7 + 2*t8 + t9 + 2) >> 2;
  2407. SRC(5,6)=SRC(6,4)=SRC(7,2)= (t8 + t9 + 1) >> 1;
  2408. SRC(5,7)=SRC(6,5)=SRC(7,3)= (t8 + 2*t9 + t10 + 2) >> 2;
  2409. SRC(6,6)=SRC(7,4)= (t9 + t10 + 1) >> 1;
  2410. SRC(6,7)=SRC(7,5)= (t9 + 2*t10 + t11 + 2) >> 2;
  2411. SRC(7,6)= (t10 + t11 + 1) >> 1;
  2412. SRC(7,7)= (t10 + 2*t11 + t12 + 2) >> 2;
  2413. }
  2414. static void pred8x8l_horizontal_up_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2415. {
  2416. PREDICT_8x8_LOAD_LEFT;
  2417. SRC(0,0)= (l0 + l1 + 1) >> 1;
  2418. SRC(1,0)= (l0 + 2*l1 + l2 + 2) >> 2;
  2419. SRC(0,1)=SRC(2,0)= (l1 + l2 + 1) >> 1;
  2420. SRC(1,1)=SRC(3,0)= (l1 + 2*l2 + l3 + 2) >> 2;
  2421. SRC(0,2)=SRC(2,1)=SRC(4,0)= (l2 + l3 + 1) >> 1;
  2422. SRC(1,2)=SRC(3,1)=SRC(5,0)= (l2 + 2*l3 + l4 + 2) >> 2;
  2423. SRC(0,3)=SRC(2,2)=SRC(4,1)=SRC(6,0)= (l3 + l4 + 1) >> 1;
  2424. SRC(1,3)=SRC(3,2)=SRC(5,1)=SRC(7,0)= (l3 + 2*l4 + l5 + 2) >> 2;
  2425. SRC(0,4)=SRC(2,3)=SRC(4,2)=SRC(6,1)= (l4 + l5 + 1) >> 1;
  2426. SRC(1,4)=SRC(3,3)=SRC(5,2)=SRC(7,1)= (l4 + 2*l5 + l6 + 2) >> 2;
  2427. SRC(0,5)=SRC(2,4)=SRC(4,3)=SRC(6,2)= (l5 + l6 + 1) >> 1;
  2428. SRC(1,5)=SRC(3,4)=SRC(5,3)=SRC(7,2)= (l5 + 2*l6 + l7 + 2) >> 2;
  2429. SRC(0,6)=SRC(2,5)=SRC(4,4)=SRC(6,3)= (l6 + l7 + 1) >> 1;
  2430. SRC(1,6)=SRC(3,5)=SRC(5,4)=SRC(7,3)= (l6 + 3*l7 + 2) >> 2;
  2431. SRC(0,7)=SRC(1,7)=SRC(2,6)=SRC(2,7)=SRC(3,6)=
  2432. SRC(3,7)=SRC(4,5)=SRC(4,6)=SRC(4,7)=SRC(5,5)=
  2433. SRC(5,6)=SRC(5,7)=SRC(6,4)=SRC(6,5)=SRC(6,6)=
  2434. SRC(6,7)=SRC(7,4)=SRC(7,5)=SRC(7,6)=SRC(7,7)= l7;
  2435. }
  2436. #undef PREDICT_8x8_LOAD_LEFT
  2437. #undef PREDICT_8x8_LOAD_TOP
  2438. #undef PREDICT_8x8_LOAD_TOPLEFT
  2439. #undef PREDICT_8x8_LOAD_TOPRIGHT
  2440. #undef PREDICT_8x8_DC
  2441. #undef PTR
  2442. #undef PT
  2443. #undef PL
  2444. #undef SRC
  2445. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  2446. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2447. int src_x_offset, int src_y_offset,
  2448. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  2449. MpegEncContext * const s = &h->s;
  2450. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  2451. int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  2452. const int luma_xy= (mx&3) + ((my&3)<<2);
  2453. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
  2454. uint8_t * src_cb, * src_cr;
  2455. int extra_width= h->emu_edge_width;
  2456. int extra_height= h->emu_edge_height;
  2457. int emu=0;
  2458. const int full_mx= mx>>2;
  2459. const int full_my= my>>2;
  2460. const int pic_width = 16*s->mb_width;
  2461. const int pic_height = 16*s->mb_height >> MB_MBAFF;
  2462. if(!pic->data[0]) //FIXME this is unacceptable, some senseable error concealment must be done for missing reference frames
  2463. return;
  2464. if(mx&7) extra_width -= 3;
  2465. if(my&7) extra_height -= 3;
  2466. if( full_mx < 0-extra_width
  2467. || full_my < 0-extra_height
  2468. || full_mx + 16/*FIXME*/ > pic_width + extra_width
  2469. || full_my + 16/*FIXME*/ > pic_height + extra_height){
  2470. 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);
  2471. src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
  2472. emu=1;
  2473. }
  2474. qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
  2475. if(!square){
  2476. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
  2477. }
  2478. if(s->flags&CODEC_FLAG_GRAY) return;
  2479. if(MB_MBAFF){
  2480. // chroma offset when predicting from a field of opposite parity
  2481. my += 2 * ((s->mb_y & 1) - (h->ref_cache[list][scan8[n]] & 1));
  2482. emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
  2483. }
  2484. src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2485. src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2486. if(emu){
  2487. 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);
  2488. src_cb= s->edge_emu_buffer;
  2489. }
  2490. chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2491. if(emu){
  2492. 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);
  2493. src_cr= s->edge_emu_buffer;
  2494. }
  2495. chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2496. }
  2497. static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
  2498. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2499. int x_offset, int y_offset,
  2500. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2501. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2502. int list0, int list1){
  2503. MpegEncContext * const s = &h->s;
  2504. qpel_mc_func *qpix_op= qpix_put;
  2505. h264_chroma_mc_func chroma_op= chroma_put;
  2506. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2507. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2508. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2509. x_offset += 8*s->mb_x;
  2510. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2511. if(list0){
  2512. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  2513. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  2514. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2515. qpix_op, chroma_op);
  2516. qpix_op= qpix_avg;
  2517. chroma_op= chroma_avg;
  2518. }
  2519. if(list1){
  2520. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  2521. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  2522. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2523. qpix_op, chroma_op);
  2524. }
  2525. }
  2526. static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
  2527. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2528. int x_offset, int y_offset,
  2529. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2530. h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
  2531. h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
  2532. int list0, int list1){
  2533. MpegEncContext * const s = &h->s;
  2534. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2535. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2536. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2537. x_offset += 8*s->mb_x;
  2538. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2539. if(list0 && list1){
  2540. /* don't optimize for luma-only case, since B-frames usually
  2541. * use implicit weights => chroma too. */
  2542. uint8_t *tmp_cb = s->obmc_scratchpad;
  2543. uint8_t *tmp_cr = s->obmc_scratchpad + 8;
  2544. uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
  2545. int refn0 = h->ref_cache[0][ scan8[n] ];
  2546. int refn1 = h->ref_cache[1][ scan8[n] ];
  2547. mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
  2548. dest_y, dest_cb, dest_cr,
  2549. x_offset, y_offset, qpix_put, chroma_put);
  2550. mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
  2551. tmp_y, tmp_cb, tmp_cr,
  2552. x_offset, y_offset, qpix_put, chroma_put);
  2553. if(h->use_weight == 2){
  2554. int weight0 = h->implicit_weight[refn0][refn1];
  2555. int weight1 = 64 - weight0;
  2556. luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
  2557. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2558. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2559. }else{
  2560. luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
  2561. h->luma_weight[0][refn0], h->luma_weight[1][refn1],
  2562. h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
  2563. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2564. h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
  2565. h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
  2566. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2567. h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
  2568. h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
  2569. }
  2570. }else{
  2571. int list = list1 ? 1 : 0;
  2572. int refn = h->ref_cache[list][ scan8[n] ];
  2573. Picture *ref= &h->ref_list[list][refn];
  2574. mc_dir_part(h, ref, n, square, chroma_height, delta, list,
  2575. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2576. qpix_put, chroma_put);
  2577. luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
  2578. h->luma_weight[list][refn], h->luma_offset[list][refn]);
  2579. if(h->use_weight_chroma){
  2580. chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2581. h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
  2582. chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2583. h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
  2584. }
  2585. }
  2586. }
  2587. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  2588. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2589. int x_offset, int y_offset,
  2590. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2591. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2592. h264_weight_func *weight_op, h264_biweight_func *weight_avg,
  2593. int list0, int list1){
  2594. if((h->use_weight==2 && list0 && list1
  2595. && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
  2596. || h->use_weight==1)
  2597. mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2598. x_offset, y_offset, qpix_put, chroma_put,
  2599. weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
  2600. else
  2601. mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2602. x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
  2603. }
  2604. static inline void prefetch_motion(H264Context *h, int list){
  2605. /* fetch pixels for estimated mv 4 macroblocks ahead
  2606. * optimized for 64byte cache lines */
  2607. MpegEncContext * const s = &h->s;
  2608. const int refn = h->ref_cache[list][scan8[0]];
  2609. if(refn >= 0){
  2610. const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
  2611. const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
  2612. uint8_t **src= h->ref_list[list][refn].data;
  2613. int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
  2614. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  2615. off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
  2616. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  2617. }
  2618. }
  2619. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2620. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  2621. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
  2622. h264_weight_func *weight_op, h264_biweight_func *weight_avg){
  2623. MpegEncContext * const s = &h->s;
  2624. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2625. const int mb_type= s->current_picture.mb_type[mb_xy];
  2626. assert(IS_INTER(mb_type));
  2627. prefetch_motion(h, 0);
  2628. if(IS_16X16(mb_type)){
  2629. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  2630. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  2631. &weight_op[0], &weight_avg[0],
  2632. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2633. }else if(IS_16X8(mb_type)){
  2634. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  2635. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2636. &weight_op[1], &weight_avg[1],
  2637. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2638. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  2639. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2640. &weight_op[1], &weight_avg[1],
  2641. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2642. }else if(IS_8X16(mb_type)){
  2643. mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
  2644. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2645. &weight_op[2], &weight_avg[2],
  2646. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2647. mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
  2648. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2649. &weight_op[2], &weight_avg[2],
  2650. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2651. }else{
  2652. int i;
  2653. assert(IS_8X8(mb_type));
  2654. for(i=0; i<4; i++){
  2655. const int sub_mb_type= h->sub_mb_type[i];
  2656. const int n= 4*i;
  2657. int x_offset= (i&1)<<2;
  2658. int y_offset= (i&2)<<1;
  2659. if(IS_SUB_8X8(sub_mb_type)){
  2660. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2661. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2662. &weight_op[3], &weight_avg[3],
  2663. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2664. }else if(IS_SUB_8X4(sub_mb_type)){
  2665. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2666. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2667. &weight_op[4], &weight_avg[4],
  2668. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2669. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  2670. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2671. &weight_op[4], &weight_avg[4],
  2672. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2673. }else if(IS_SUB_4X8(sub_mb_type)){
  2674. mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2675. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2676. &weight_op[5], &weight_avg[5],
  2677. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2678. mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
  2679. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2680. &weight_op[5], &weight_avg[5],
  2681. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2682. }else{
  2683. int j;
  2684. assert(IS_SUB_4X4(sub_mb_type));
  2685. for(j=0; j<4; j++){
  2686. int sub_x_offset= x_offset + 2*(j&1);
  2687. int sub_y_offset= y_offset + (j&2);
  2688. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  2689. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2690. &weight_op[6], &weight_avg[6],
  2691. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2692. }
  2693. }
  2694. }
  2695. }
  2696. prefetch_motion(h, 1);
  2697. }
  2698. static void decode_init_vlc(){
  2699. static int done = 0;
  2700. if (!done) {
  2701. int i;
  2702. done = 1;
  2703. init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
  2704. &chroma_dc_coeff_token_len [0], 1, 1,
  2705. &chroma_dc_coeff_token_bits[0], 1, 1, 1);
  2706. for(i=0; i<4; i++){
  2707. init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
  2708. &coeff_token_len [i][0], 1, 1,
  2709. &coeff_token_bits[i][0], 1, 1, 1);
  2710. }
  2711. for(i=0; i<3; i++){
  2712. init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
  2713. &chroma_dc_total_zeros_len [i][0], 1, 1,
  2714. &chroma_dc_total_zeros_bits[i][0], 1, 1, 1);
  2715. }
  2716. for(i=0; i<15; i++){
  2717. init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
  2718. &total_zeros_len [i][0], 1, 1,
  2719. &total_zeros_bits[i][0], 1, 1, 1);
  2720. }
  2721. for(i=0; i<6; i++){
  2722. init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
  2723. &run_len [i][0], 1, 1,
  2724. &run_bits[i][0], 1, 1, 1);
  2725. }
  2726. init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
  2727. &run_len [6][0], 1, 1,
  2728. &run_bits[6][0], 1, 1, 1);
  2729. }
  2730. }
  2731. /**
  2732. * Sets the intra prediction function pointers.
  2733. */
  2734. static void init_pred_ptrs(H264Context *h){
  2735. // MpegEncContext * const s = &h->s;
  2736. h->pred4x4[VERT_PRED ]= pred4x4_vertical_c;
  2737. h->pred4x4[HOR_PRED ]= pred4x4_horizontal_c;
  2738. h->pred4x4[DC_PRED ]= pred4x4_dc_c;
  2739. h->pred4x4[DIAG_DOWN_LEFT_PRED ]= pred4x4_down_left_c;
  2740. h->pred4x4[DIAG_DOWN_RIGHT_PRED]= pred4x4_down_right_c;
  2741. h->pred4x4[VERT_RIGHT_PRED ]= pred4x4_vertical_right_c;
  2742. h->pred4x4[HOR_DOWN_PRED ]= pred4x4_horizontal_down_c;
  2743. h->pred4x4[VERT_LEFT_PRED ]= pred4x4_vertical_left_c;
  2744. h->pred4x4[HOR_UP_PRED ]= pred4x4_horizontal_up_c;
  2745. h->pred4x4[LEFT_DC_PRED ]= pred4x4_left_dc_c;
  2746. h->pred4x4[TOP_DC_PRED ]= pred4x4_top_dc_c;
  2747. h->pred4x4[DC_128_PRED ]= pred4x4_128_dc_c;
  2748. h->pred8x8l[VERT_PRED ]= pred8x8l_vertical_c;
  2749. h->pred8x8l[HOR_PRED ]= pred8x8l_horizontal_c;
  2750. h->pred8x8l[DC_PRED ]= pred8x8l_dc_c;
  2751. h->pred8x8l[DIAG_DOWN_LEFT_PRED ]= pred8x8l_down_left_c;
  2752. h->pred8x8l[DIAG_DOWN_RIGHT_PRED]= pred8x8l_down_right_c;
  2753. h->pred8x8l[VERT_RIGHT_PRED ]= pred8x8l_vertical_right_c;
  2754. h->pred8x8l[HOR_DOWN_PRED ]= pred8x8l_horizontal_down_c;
  2755. h->pred8x8l[VERT_LEFT_PRED ]= pred8x8l_vertical_left_c;
  2756. h->pred8x8l[HOR_UP_PRED ]= pred8x8l_horizontal_up_c;
  2757. h->pred8x8l[LEFT_DC_PRED ]= pred8x8l_left_dc_c;
  2758. h->pred8x8l[TOP_DC_PRED ]= pred8x8l_top_dc_c;
  2759. h->pred8x8l[DC_128_PRED ]= pred8x8l_128_dc_c;
  2760. h->pred8x8[DC_PRED8x8 ]= ff_pred8x8_dc_c;
  2761. h->pred8x8[VERT_PRED8x8 ]= ff_pred8x8_vertical_c;
  2762. h->pred8x8[HOR_PRED8x8 ]= ff_pred8x8_horizontal_c;
  2763. h->pred8x8[PLANE_PRED8x8 ]= ff_pred8x8_plane_c;
  2764. h->pred8x8[LEFT_DC_PRED8x8]= pred8x8_left_dc_c;
  2765. h->pred8x8[TOP_DC_PRED8x8 ]= pred8x8_top_dc_c;
  2766. h->pred8x8[DC_128_PRED8x8 ]= ff_pred8x8_128_dc_c;
  2767. h->pred16x16[DC_PRED8x8 ]= ff_pred16x16_dc_c;
  2768. h->pred16x16[VERT_PRED8x8 ]= ff_pred16x16_vertical_c;
  2769. h->pred16x16[HOR_PRED8x8 ]= ff_pred16x16_horizontal_c;
  2770. h->pred16x16[PLANE_PRED8x8 ]= ff_pred16x16_plane_c;
  2771. h->pred16x16[LEFT_DC_PRED8x8]= pred16x16_left_dc_c;
  2772. h->pred16x16[TOP_DC_PRED8x8 ]= pred16x16_top_dc_c;
  2773. h->pred16x16[DC_128_PRED8x8 ]= ff_pred16x16_128_dc_c;
  2774. }
  2775. static void free_tables(H264Context *h){
  2776. av_freep(&h->intra4x4_pred_mode);
  2777. av_freep(&h->chroma_pred_mode_table);
  2778. av_freep(&h->cbp_table);
  2779. av_freep(&h->mvd_table[0]);
  2780. av_freep(&h->mvd_table[1]);
  2781. av_freep(&h->direct_table);
  2782. av_freep(&h->non_zero_count);
  2783. av_freep(&h->slice_table_base);
  2784. av_freep(&h->top_borders[1]);
  2785. av_freep(&h->top_borders[0]);
  2786. h->slice_table= NULL;
  2787. av_freep(&h->mb2b_xy);
  2788. av_freep(&h->mb2b8_xy);
  2789. av_freep(&h->s.obmc_scratchpad);
  2790. }
  2791. static void init_dequant8_coeff_table(H264Context *h){
  2792. int i,q,x;
  2793. const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
  2794. h->dequant8_coeff[0] = h->dequant8_buffer[0];
  2795. h->dequant8_coeff[1] = h->dequant8_buffer[1];
  2796. for(i=0; i<2; i++ ){
  2797. if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
  2798. h->dequant8_coeff[1] = h->dequant8_buffer[0];
  2799. break;
  2800. }
  2801. for(q=0; q<52; q++){
  2802. int shift = ff_div6[q];
  2803. int idx = ff_rem6[q];
  2804. for(x=0; x<64; x++)
  2805. h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
  2806. ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
  2807. h->pps.scaling_matrix8[i][x]) << shift;
  2808. }
  2809. }
  2810. }
  2811. static void init_dequant4_coeff_table(H264Context *h){
  2812. int i,j,q,x;
  2813. const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
  2814. for(i=0; i<6; i++ ){
  2815. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  2816. for(j=0; j<i; j++){
  2817. if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
  2818. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  2819. break;
  2820. }
  2821. }
  2822. if(j<i)
  2823. continue;
  2824. for(q=0; q<52; q++){
  2825. int shift = ff_div6[q] + 2;
  2826. int idx = ff_rem6[q];
  2827. for(x=0; x<16; x++)
  2828. h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
  2829. ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
  2830. h->pps.scaling_matrix4[i][x]) << shift;
  2831. }
  2832. }
  2833. }
  2834. static void init_dequant_tables(H264Context *h){
  2835. int i,x;
  2836. init_dequant4_coeff_table(h);
  2837. if(h->pps.transform_8x8_mode)
  2838. init_dequant8_coeff_table(h);
  2839. if(h->sps.transform_bypass){
  2840. for(i=0; i<6; i++)
  2841. for(x=0; x<16; x++)
  2842. h->dequant4_coeff[i][0][x] = 1<<6;
  2843. if(h->pps.transform_8x8_mode)
  2844. for(i=0; i<2; i++)
  2845. for(x=0; x<64; x++)
  2846. h->dequant8_coeff[i][0][x] = 1<<6;
  2847. }
  2848. }
  2849. /**
  2850. * allocates tables.
  2851. * needs width/height
  2852. */
  2853. static int alloc_tables(H264Context *h){
  2854. MpegEncContext * const s = &h->s;
  2855. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  2856. int x,y;
  2857. CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
  2858. CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
  2859. CHECKED_ALLOCZ(h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(uint8_t))
  2860. CHECKED_ALLOCZ(h->top_borders[0] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2861. CHECKED_ALLOCZ(h->top_borders[1] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2862. CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
  2863. if( h->pps.cabac ) {
  2864. CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
  2865. CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
  2866. CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t));
  2867. CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t));
  2868. }
  2869. memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(uint8_t));
  2870. h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
  2871. CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint32_t));
  2872. CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint32_t));
  2873. for(y=0; y<s->mb_height; y++){
  2874. for(x=0; x<s->mb_width; x++){
  2875. const int mb_xy= x + y*s->mb_stride;
  2876. const int b_xy = 4*x + 4*y*h->b_stride;
  2877. const int b8_xy= 2*x + 2*y*h->b8_stride;
  2878. h->mb2b_xy [mb_xy]= b_xy;
  2879. h->mb2b8_xy[mb_xy]= b8_xy;
  2880. }
  2881. }
  2882. s->obmc_scratchpad = NULL;
  2883. if(!h->dequant4_coeff[0])
  2884. init_dequant_tables(h);
  2885. return 0;
  2886. fail:
  2887. free_tables(h);
  2888. return -1;
  2889. }
  2890. static void common_init(H264Context *h){
  2891. MpegEncContext * const s = &h->s;
  2892. s->width = s->avctx->width;
  2893. s->height = s->avctx->height;
  2894. s->codec_id= s->avctx->codec->id;
  2895. init_pred_ptrs(h);
  2896. h->dequant_coeff_pps= -1;
  2897. s->unrestricted_mv=1;
  2898. s->decode=1; //FIXME
  2899. memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  2900. memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  2901. }
  2902. static int decode_init(AVCodecContext *avctx){
  2903. H264Context *h= avctx->priv_data;
  2904. MpegEncContext * const s = &h->s;
  2905. MPV_decode_defaults(s);
  2906. s->avctx = avctx;
  2907. common_init(h);
  2908. s->out_format = FMT_H264;
  2909. s->workaround_bugs= avctx->workaround_bugs;
  2910. // set defaults
  2911. // s->decode_mb= ff_h263_decode_mb;
  2912. s->low_delay= 1;
  2913. avctx->pix_fmt= PIX_FMT_YUV420P;
  2914. decode_init_vlc();
  2915. if(avctx->extradata_size > 0 && avctx->extradata &&
  2916. *(char *)avctx->extradata == 1){
  2917. h->is_avc = 1;
  2918. h->got_avcC = 0;
  2919. } else {
  2920. h->is_avc = 0;
  2921. }
  2922. return 0;
  2923. }
  2924. static int frame_start(H264Context *h){
  2925. MpegEncContext * const s = &h->s;
  2926. int i;
  2927. if(MPV_frame_start(s, s->avctx) < 0)
  2928. return -1;
  2929. ff_er_frame_start(s);
  2930. assert(s->linesize && s->uvlinesize);
  2931. for(i=0; i<16; i++){
  2932. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  2933. h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  2934. }
  2935. for(i=0; i<4; i++){
  2936. h->block_offset[16+i]=
  2937. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2938. h->block_offset[24+16+i]=
  2939. h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2940. }
  2941. /* can't be in alloc_tables because linesize isn't known there.
  2942. * FIXME: redo bipred weight to not require extra buffer? */
  2943. if(!s->obmc_scratchpad)
  2944. s->obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  2945. /* some macroblocks will be accessed before they're available */
  2946. if(FRAME_MBAFF)
  2947. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(uint8_t));
  2948. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  2949. return 0;
  2950. }
  2951. static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  2952. MpegEncContext * const s = &h->s;
  2953. int i;
  2954. src_y -= linesize;
  2955. src_cb -= uvlinesize;
  2956. src_cr -= uvlinesize;
  2957. // There are two lines saved, the line above the the top macroblock of a pair,
  2958. // and the line above the bottom macroblock
  2959. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  2960. for(i=1; i<17; i++){
  2961. h->left_border[i]= src_y[15+i* linesize];
  2962. }
  2963. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
  2964. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
  2965. if(!(s->flags&CODEC_FLAG_GRAY)){
  2966. h->left_border[17 ]= h->top_borders[0][s->mb_x][16+7];
  2967. h->left_border[17+9]= h->top_borders[0][s->mb_x][24+7];
  2968. for(i=1; i<9; i++){
  2969. h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
  2970. h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
  2971. }
  2972. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
  2973. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
  2974. }
  2975. }
  2976. 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){
  2977. MpegEncContext * const s = &h->s;
  2978. int temp8, i;
  2979. uint64_t temp64;
  2980. int deblock_left = (s->mb_x > 0);
  2981. int deblock_top = (s->mb_y > 0);
  2982. src_y -= linesize + 1;
  2983. src_cb -= uvlinesize + 1;
  2984. src_cr -= uvlinesize + 1;
  2985. #define XCHG(a,b,t,xchg)\
  2986. t= a;\
  2987. if(xchg)\
  2988. a= b;\
  2989. b= t;
  2990. if(deblock_left){
  2991. for(i = !deblock_top; i<17; i++){
  2992. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  2993. }
  2994. }
  2995. if(deblock_top){
  2996. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  2997. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  2998. if(s->mb_x+1 < s->mb_width){
  2999. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  3000. }
  3001. }
  3002. if(!(s->flags&CODEC_FLAG_GRAY)){
  3003. if(deblock_left){
  3004. for(i = !deblock_top; i<9; i++){
  3005. XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
  3006. XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
  3007. }
  3008. }
  3009. if(deblock_top){
  3010. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  3011. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  3012. }
  3013. }
  3014. }
  3015. static inline void backup_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  3016. MpegEncContext * const s = &h->s;
  3017. int i;
  3018. src_y -= 2 * linesize;
  3019. src_cb -= 2 * uvlinesize;
  3020. src_cr -= 2 * uvlinesize;
  3021. // There are two lines saved, the line above the the top macroblock of a pair,
  3022. // and the line above the bottom macroblock
  3023. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  3024. h->left_border[1]= h->top_borders[1][s->mb_x][15];
  3025. for(i=2; i<34; i++){
  3026. h->left_border[i]= src_y[15+i* linesize];
  3027. }
  3028. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 32*linesize);
  3029. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+32*linesize);
  3030. *(uint64_t*)(h->top_borders[1][s->mb_x]+0)= *(uint64_t*)(src_y + 33*linesize);
  3031. *(uint64_t*)(h->top_borders[1][s->mb_x]+8)= *(uint64_t*)(src_y +8+33*linesize);
  3032. if(!(s->flags&CODEC_FLAG_GRAY)){
  3033. h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7];
  3034. h->left_border[34+ 1]= h->top_borders[1][s->mb_x][16+7];
  3035. h->left_border[34+18 ]= h->top_borders[0][s->mb_x][24+7];
  3036. h->left_border[34+18+1]= h->top_borders[1][s->mb_x][24+7];
  3037. for(i=2; i<18; i++){
  3038. h->left_border[i+34 ]= src_cb[7+i*uvlinesize];
  3039. h->left_border[i+34+18]= src_cr[7+i*uvlinesize];
  3040. }
  3041. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+16*uvlinesize);
  3042. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+16*uvlinesize);
  3043. *(uint64_t*)(h->top_borders[1][s->mb_x]+16)= *(uint64_t*)(src_cb+17*uvlinesize);
  3044. *(uint64_t*)(h->top_borders[1][s->mb_x]+24)= *(uint64_t*)(src_cr+17*uvlinesize);
  3045. }
  3046. }
  3047. 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){
  3048. MpegEncContext * const s = &h->s;
  3049. int temp8, i;
  3050. uint64_t temp64;
  3051. int deblock_left = (s->mb_x > 0);
  3052. int deblock_top = (s->mb_y > 1);
  3053. tprintf(s->avctx, "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);
  3054. src_y -= 2 * linesize + 1;
  3055. src_cb -= 2 * uvlinesize + 1;
  3056. src_cr -= 2 * uvlinesize + 1;
  3057. #define XCHG(a,b,t,xchg)\
  3058. t= a;\
  3059. if(xchg)\
  3060. a= b;\
  3061. b= t;
  3062. if(deblock_left){
  3063. for(i = (!deblock_top)<<1; i<34; i++){
  3064. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  3065. }
  3066. }
  3067. if(deblock_top){
  3068. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  3069. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  3070. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+0), *(uint64_t*)(src_y +1 +linesize), temp64, xchg);
  3071. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+8), *(uint64_t*)(src_y +9 +linesize), temp64, 1);
  3072. if(s->mb_x+1 < s->mb_width){
  3073. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  3074. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x+1]), *(uint64_t*)(src_y +17 +linesize), temp64, 1);
  3075. }
  3076. }
  3077. if(!(s->flags&CODEC_FLAG_GRAY)){
  3078. if(deblock_left){
  3079. for(i = (!deblock_top) << 1; i<18; i++){
  3080. XCHG(h->left_border[i+34 ], src_cb[i*uvlinesize], temp8, xchg);
  3081. XCHG(h->left_border[i+34+18], src_cr[i*uvlinesize], temp8, xchg);
  3082. }
  3083. }
  3084. if(deblock_top){
  3085. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  3086. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  3087. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+16), *(uint64_t*)(src_cb+1 +uvlinesize), temp64, 1);
  3088. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+24), *(uint64_t*)(src_cr+1 +uvlinesize), temp64, 1);
  3089. }
  3090. }
  3091. }
  3092. static void av_always_inline hl_decode_mb_internal(H264Context *h, int simple){
  3093. MpegEncContext * const s = &h->s;
  3094. const int mb_x= s->mb_x;
  3095. const int mb_y= s->mb_y;
  3096. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3097. const int mb_type= s->current_picture.mb_type[mb_xy];
  3098. uint8_t *dest_y, *dest_cb, *dest_cr;
  3099. int linesize, uvlinesize /*dct_offset*/;
  3100. int i;
  3101. int *block_offset = &h->block_offset[0];
  3102. const unsigned int bottom = mb_y & 1;
  3103. const int transform_bypass = (s->qscale == 0 && h->sps.transform_bypass), is_h264 = (simple || s->codec_id == CODEC_ID_H264);
  3104. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  3105. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  3106. dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3107. dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3108. dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3109. s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
  3110. s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
  3111. if (!simple && MB_FIELD) {
  3112. linesize = h->mb_linesize = s->linesize * 2;
  3113. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  3114. block_offset = &h->block_offset[24];
  3115. if(mb_y&1){ //FIXME move out of this func?
  3116. dest_y -= s->linesize*15;
  3117. dest_cb-= s->uvlinesize*7;
  3118. dest_cr-= s->uvlinesize*7;
  3119. }
  3120. if(FRAME_MBAFF) {
  3121. int list;
  3122. for(list=0; list<h->list_count; list++){
  3123. if(!USES_LIST(mb_type, list))
  3124. continue;
  3125. if(IS_16X16(mb_type)){
  3126. int8_t *ref = &h->ref_cache[list][scan8[0]];
  3127. fill_rectangle(ref, 4, 4, 8, 16+*ref^(s->mb_y&1), 1);
  3128. }else{
  3129. for(i=0; i<16; i+=4){
  3130. //FIXME can refs be smaller than 8x8 when !direct_8x8_inference ?
  3131. int ref = h->ref_cache[list][scan8[i]];
  3132. if(ref >= 0)
  3133. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, 16+ref^(s->mb_y&1), 1);
  3134. }
  3135. }
  3136. }
  3137. }
  3138. } else {
  3139. linesize = h->mb_linesize = s->linesize;
  3140. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  3141. // dct_offset = s->linesize * 16;
  3142. }
  3143. if(transform_bypass){
  3144. idct_dc_add =
  3145. idct_add = IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  3146. }else if(IS_8x8DCT(mb_type)){
  3147. idct_dc_add = s->dsp.h264_idct8_dc_add;
  3148. idct_add = s->dsp.h264_idct8_add;
  3149. }else{
  3150. idct_dc_add = s->dsp.h264_idct_dc_add;
  3151. idct_add = s->dsp.h264_idct_add;
  3152. }
  3153. if(!simple && FRAME_MBAFF && h->deblocking_filter && IS_INTRA(mb_type)
  3154. && (!bottom || !IS_INTRA(s->current_picture.mb_type[mb_xy-s->mb_stride]))){
  3155. int mbt_y = mb_y&~1;
  3156. uint8_t *top_y = s->current_picture.data[0] + (mbt_y * 16* s->linesize ) + mb_x * 16;
  3157. uint8_t *top_cb = s->current_picture.data[1] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  3158. uint8_t *top_cr = s->current_picture.data[2] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  3159. xchg_pair_border(h, top_y, top_cb, top_cr, s->linesize, s->uvlinesize, 1);
  3160. }
  3161. if (!simple && IS_INTRA_PCM(mb_type)) {
  3162. unsigned int x, y;
  3163. // The pixels are stored in h->mb array in the same order as levels,
  3164. // copy them in output in the correct order.
  3165. for(i=0; i<16; i++) {
  3166. for (y=0; y<4; y++) {
  3167. for (x=0; x<4; x++) {
  3168. *(dest_y + block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x];
  3169. }
  3170. }
  3171. }
  3172. for(i=16; i<16+4; i++) {
  3173. for (y=0; y<4; y++) {
  3174. for (x=0; x<4; x++) {
  3175. *(dest_cb + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  3176. }
  3177. }
  3178. }
  3179. for(i=20; i<20+4; i++) {
  3180. for (y=0; y<4; y++) {
  3181. for (x=0; x<4; x++) {
  3182. *(dest_cr + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  3183. }
  3184. }
  3185. }
  3186. } else {
  3187. if(IS_INTRA(mb_type)){
  3188. if(h->deblocking_filter && (simple || !FRAME_MBAFF))
  3189. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1);
  3190. if(simple || !(s->flags&CODEC_FLAG_GRAY)){
  3191. h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  3192. h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  3193. }
  3194. if(IS_INTRA4x4(mb_type)){
  3195. if(simple || !s->encoding){
  3196. if(IS_8x8DCT(mb_type)){
  3197. for(i=0; i<16; i+=4){
  3198. uint8_t * const ptr= dest_y + block_offset[i];
  3199. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  3200. const int nnz = h->non_zero_count_cache[ scan8[i] ];
  3201. h->pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  3202. (h->topright_samples_available<<i)&0x4000, linesize);
  3203. if(nnz){
  3204. if(nnz == 1 && h->mb[i*16])
  3205. idct_dc_add(ptr, h->mb + i*16, linesize);
  3206. else
  3207. idct_add(ptr, h->mb + i*16, linesize);
  3208. }
  3209. }
  3210. }else
  3211. for(i=0; i<16; i++){
  3212. uint8_t * const ptr= dest_y + block_offset[i];
  3213. uint8_t *topright;
  3214. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  3215. int nnz, tr;
  3216. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  3217. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  3218. assert(mb_y || linesize <= block_offset[i]);
  3219. if(!topright_avail){
  3220. tr= ptr[3 - linesize]*0x01010101;
  3221. topright= (uint8_t*) &tr;
  3222. }else
  3223. topright= ptr + 4 - linesize;
  3224. }else
  3225. topright= NULL;
  3226. h->pred4x4[ dir ](ptr, topright, linesize);
  3227. nnz = h->non_zero_count_cache[ scan8[i] ];
  3228. if(nnz){
  3229. if(is_h264){
  3230. if(nnz == 1 && h->mb[i*16])
  3231. idct_dc_add(ptr, h->mb + i*16, linesize);
  3232. else
  3233. idct_add(ptr, h->mb + i*16, linesize);
  3234. }else
  3235. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  3236. }
  3237. }
  3238. }
  3239. }else{
  3240. h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  3241. if(is_h264){
  3242. if(!transform_bypass)
  3243. h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[IS_INTRA(mb_type) ? 0:3][s->qscale][0]);
  3244. }else
  3245. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  3246. }
  3247. if(h->deblocking_filter && (simple || !FRAME_MBAFF))
  3248. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
  3249. }else if(is_h264){
  3250. hl_motion(h, dest_y, dest_cb, dest_cr,
  3251. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  3252. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  3253. s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
  3254. }
  3255. if(!IS_INTRA4x4(mb_type)){
  3256. if(is_h264){
  3257. if(IS_INTRA16x16(mb_type)){
  3258. for(i=0; i<16; i++){
  3259. if(h->non_zero_count_cache[ scan8[i] ])
  3260. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3261. else if(h->mb[i*16])
  3262. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3263. }
  3264. }else{
  3265. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  3266. for(i=0; i<16; i+=di){
  3267. int nnz = h->non_zero_count_cache[ scan8[i] ];
  3268. if(nnz){
  3269. if(nnz==1 && h->mb[i*16])
  3270. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3271. else
  3272. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3273. }
  3274. }
  3275. }
  3276. }else{
  3277. for(i=0; i<16; i++){
  3278. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  3279. uint8_t * const ptr= dest_y + block_offset[i];
  3280. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  3281. }
  3282. }
  3283. }
  3284. }
  3285. if(simple || !(s->flags&CODEC_FLAG_GRAY)){
  3286. uint8_t *dest[2] = {dest_cb, dest_cr};
  3287. if(transform_bypass){
  3288. idct_add = idct_dc_add = s->dsp.add_pixels4;
  3289. }else{
  3290. idct_add = s->dsp.h264_idct_add;
  3291. idct_dc_add = s->dsp.h264_idct_dc_add;
  3292. 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]);
  3293. 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]);
  3294. }
  3295. if(is_h264){
  3296. for(i=16; i<16+8; i++){
  3297. if(h->non_zero_count_cache[ scan8[i] ])
  3298. idct_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3299. else if(h->mb[i*16])
  3300. idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3301. }
  3302. }else{
  3303. for(i=16; i<16+8; i++){
  3304. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  3305. uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
  3306. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  3307. }
  3308. }
  3309. }
  3310. }
  3311. }
  3312. if(h->deblocking_filter) {
  3313. if (!simple && FRAME_MBAFF) {
  3314. //FIXME try deblocking one mb at a time?
  3315. // the reduction in load/storing mvs and such might outweigh the extra backup/xchg_border
  3316. const int mb_y = s->mb_y - 1;
  3317. uint8_t *pair_dest_y, *pair_dest_cb, *pair_dest_cr;
  3318. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3319. const int mb_type_top = s->current_picture.mb_type[mb_xy];
  3320. const int mb_type_bottom= s->current_picture.mb_type[mb_xy+s->mb_stride];
  3321. if (!bottom) return;
  3322. pair_dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3323. pair_dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3324. pair_dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3325. if(IS_INTRA(mb_type_top | mb_type_bottom))
  3326. xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0);
  3327. backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize);
  3328. // deblock a pair
  3329. // top
  3330. s->mb_y--;
  3331. tprintf(h->s.avctx, "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);
  3332. fill_caches(h, mb_type_top, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3333. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy]);
  3334. filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize);
  3335. // bottom
  3336. s->mb_y++;
  3337. tprintf(h->s.avctx, "call mbaff filter_mb\n");
  3338. fill_caches(h, mb_type_bottom, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3339. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy+s->mb_stride]);
  3340. filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3341. } else {
  3342. tprintf(h->s.avctx, "call filter_mb\n");
  3343. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3344. fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3345. filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3346. }
  3347. }
  3348. }
  3349. /**
  3350. * Process a macroblock; this case avoids checks for expensive uncommon cases.
  3351. */
  3352. static void hl_decode_mb_simple(H264Context *h){
  3353. hl_decode_mb_internal(h, 1);
  3354. }
  3355. /**
  3356. * Process a macroblock; this handles edge cases, such as interlacing.
  3357. */
  3358. static void av_noinline hl_decode_mb_complex(H264Context *h){
  3359. hl_decode_mb_internal(h, 0);
  3360. }
  3361. static void hl_decode_mb(H264Context *h){
  3362. MpegEncContext * const s = &h->s;
  3363. const int mb_x= s->mb_x;
  3364. const int mb_y= s->mb_y;
  3365. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3366. const int mb_type= s->current_picture.mb_type[mb_xy];
  3367. int is_complex = FRAME_MBAFF || MB_FIELD || IS_INTRA_PCM(mb_type) || s->codec_id != CODEC_ID_H264 || (s->flags&CODEC_FLAG_GRAY) || s->encoding;
  3368. if(!s->decode)
  3369. return;
  3370. if (is_complex)
  3371. hl_decode_mb_complex(h);
  3372. else hl_decode_mb_simple(h);
  3373. }
  3374. /**
  3375. * fills the default_ref_list.
  3376. */
  3377. static int fill_default_ref_list(H264Context *h){
  3378. MpegEncContext * const s = &h->s;
  3379. int i;
  3380. int smallest_poc_greater_than_current = -1;
  3381. Picture sorted_short_ref[32];
  3382. if(h->slice_type==B_TYPE){
  3383. int out_i;
  3384. int limit= INT_MIN;
  3385. /* sort frame according to poc in B slice */
  3386. for(out_i=0; out_i<h->short_ref_count; out_i++){
  3387. int best_i=INT_MIN;
  3388. int best_poc=INT_MAX;
  3389. for(i=0; i<h->short_ref_count; i++){
  3390. const int poc= h->short_ref[i]->poc;
  3391. if(poc > limit && poc < best_poc){
  3392. best_poc= poc;
  3393. best_i= i;
  3394. }
  3395. }
  3396. assert(best_i != INT_MIN);
  3397. limit= best_poc;
  3398. sorted_short_ref[out_i]= *h->short_ref[best_i];
  3399. tprintf(h->s.avctx, "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);
  3400. if (-1 == smallest_poc_greater_than_current) {
  3401. if (h->short_ref[best_i]->poc >= s->current_picture_ptr->poc) {
  3402. smallest_poc_greater_than_current = out_i;
  3403. }
  3404. }
  3405. }
  3406. }
  3407. if(s->picture_structure == PICT_FRAME){
  3408. if(h->slice_type==B_TYPE){
  3409. int list;
  3410. tprintf(h->s.avctx, "current poc: %d, smallest_poc_greater_than_current: %d\n", s->current_picture_ptr->poc, smallest_poc_greater_than_current);
  3411. // find the largest poc
  3412. for(list=0; list<2; list++){
  3413. int index = 0;
  3414. int j= -99;
  3415. int step= list ? -1 : 1;
  3416. for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++, j+=step) {
  3417. while(j<0 || j>= h->short_ref_count){
  3418. if(j != -99 && step == (list ? -1 : 1))
  3419. return -1;
  3420. step = -step;
  3421. j= smallest_poc_greater_than_current + (step>>1);
  3422. }
  3423. if(sorted_short_ref[j].reference != 3) continue;
  3424. h->default_ref_list[list][index ]= sorted_short_ref[j];
  3425. h->default_ref_list[list][index++].pic_id= sorted_short_ref[j].frame_num;
  3426. }
  3427. for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){
  3428. if(h->long_ref[i] == NULL) continue;
  3429. if(h->long_ref[i]->reference != 3) continue;
  3430. h->default_ref_list[ list ][index ]= *h->long_ref[i];
  3431. h->default_ref_list[ list ][index++].pic_id= i;;
  3432. }
  3433. if(list && (smallest_poc_greater_than_current<=0 || smallest_poc_greater_than_current>=h->short_ref_count) && (1 < index)){
  3434. // swap the two first elements of L1 when
  3435. // L0 and L1 are identical
  3436. Picture temp= h->default_ref_list[1][0];
  3437. h->default_ref_list[1][0] = h->default_ref_list[1][1];
  3438. h->default_ref_list[1][1] = temp;
  3439. }
  3440. if(index < h->ref_count[ list ])
  3441. memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
  3442. }
  3443. }else{
  3444. int index=0;
  3445. for(i=0; i<h->short_ref_count; i++){
  3446. if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
  3447. h->default_ref_list[0][index ]= *h->short_ref[i];
  3448. h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
  3449. }
  3450. for(i = 0; i < 16; i++){
  3451. if(h->long_ref[i] == NULL) continue;
  3452. if(h->long_ref[i]->reference != 3) continue;
  3453. h->default_ref_list[0][index ]= *h->long_ref[i];
  3454. h->default_ref_list[0][index++].pic_id= i;;
  3455. }
  3456. if(index < h->ref_count[0])
  3457. memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
  3458. }
  3459. }else{ //FIELD
  3460. if(h->slice_type==B_TYPE){
  3461. }else{
  3462. //FIXME second field balh
  3463. }
  3464. }
  3465. #ifdef TRACE
  3466. for (i=0; i<h->ref_count[0]; i++) {
  3467. tprintf(h->s.avctx, "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]);
  3468. }
  3469. if(h->slice_type==B_TYPE){
  3470. for (i=0; i<h->ref_count[1]; i++) {
  3471. tprintf(h->s.avctx, "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]);
  3472. }
  3473. }
  3474. #endif
  3475. return 0;
  3476. }
  3477. static void print_short_term(H264Context *h);
  3478. static void print_long_term(H264Context *h);
  3479. static int decode_ref_pic_list_reordering(H264Context *h){
  3480. MpegEncContext * const s = &h->s;
  3481. int list, index;
  3482. print_short_term(h);
  3483. print_long_term(h);
  3484. if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move before func
  3485. for(list=0; list<h->list_count; list++){
  3486. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  3487. if(get_bits1(&s->gb)){
  3488. int pred= h->curr_pic_num;
  3489. for(index=0; ; index++){
  3490. unsigned int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
  3491. unsigned int pic_id;
  3492. int i;
  3493. Picture *ref = NULL;
  3494. if(reordering_of_pic_nums_idc==3)
  3495. break;
  3496. if(index >= h->ref_count[list]){
  3497. av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
  3498. return -1;
  3499. }
  3500. if(reordering_of_pic_nums_idc<3){
  3501. if(reordering_of_pic_nums_idc<2){
  3502. const unsigned int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  3503. if(abs_diff_pic_num >= h->max_pic_num){
  3504. av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  3505. return -1;
  3506. }
  3507. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  3508. else pred+= abs_diff_pic_num;
  3509. pred &= h->max_pic_num - 1;
  3510. for(i= h->short_ref_count-1; i>=0; i--){
  3511. ref = h->short_ref[i];
  3512. assert(ref->reference == 3);
  3513. assert(!ref->long_ref);
  3514. if(ref->data[0] != NULL && ref->frame_num == pred && ref->long_ref == 0) // ignore non existing pictures by testing data[0] pointer
  3515. break;
  3516. }
  3517. if(i>=0)
  3518. ref->pic_id= ref->frame_num;
  3519. }else{
  3520. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  3521. if(pic_id>31){
  3522. av_log(h->s.avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
  3523. return -1;
  3524. }
  3525. ref = h->long_ref[pic_id];
  3526. if(ref){
  3527. ref->pic_id= pic_id;
  3528. assert(ref->reference == 3);
  3529. assert(ref->long_ref);
  3530. i=0;
  3531. }else{
  3532. i=-1;
  3533. }
  3534. }
  3535. if (i < 0) {
  3536. av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  3537. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  3538. } else {
  3539. for(i=index; i+1<h->ref_count[list]; i++){
  3540. if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
  3541. break;
  3542. }
  3543. for(; i > index; i--){
  3544. h->ref_list[list][i]= h->ref_list[list][i-1];
  3545. }
  3546. h->ref_list[list][index]= *ref;
  3547. }
  3548. }else{
  3549. av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  3550. return -1;
  3551. }
  3552. }
  3553. }
  3554. }
  3555. for(list=0; list<h->list_count; list++){
  3556. for(index= 0; index < h->ref_count[list]; index++){
  3557. if(!h->ref_list[list][index].data[0])
  3558. h->ref_list[list][index]= s->current_picture;
  3559. }
  3560. }
  3561. if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)
  3562. direct_dist_scale_factor(h);
  3563. direct_ref_list_init(h);
  3564. return 0;
  3565. }
  3566. static void fill_mbaff_ref_list(H264Context *h){
  3567. int list, i, j;
  3568. for(list=0; list<2; list++){ //FIXME try list_count
  3569. for(i=0; i<h->ref_count[list]; i++){
  3570. Picture *frame = &h->ref_list[list][i];
  3571. Picture *field = &h->ref_list[list][16+2*i];
  3572. field[0] = *frame;
  3573. for(j=0; j<3; j++)
  3574. field[0].linesize[j] <<= 1;
  3575. field[1] = field[0];
  3576. for(j=0; j<3; j++)
  3577. field[1].data[j] += frame->linesize[j];
  3578. h->luma_weight[list][16+2*i] = h->luma_weight[list][16+2*i+1] = h->luma_weight[list][i];
  3579. h->luma_offset[list][16+2*i] = h->luma_offset[list][16+2*i+1] = h->luma_offset[list][i];
  3580. for(j=0; j<2; j++){
  3581. h->chroma_weight[list][16+2*i][j] = h->chroma_weight[list][16+2*i+1][j] = h->chroma_weight[list][i][j];
  3582. h->chroma_offset[list][16+2*i][j] = h->chroma_offset[list][16+2*i+1][j] = h->chroma_offset[list][i][j];
  3583. }
  3584. }
  3585. }
  3586. for(j=0; j<h->ref_count[1]; j++){
  3587. for(i=0; i<h->ref_count[0]; i++)
  3588. h->implicit_weight[j][16+2*i] = h->implicit_weight[j][16+2*i+1] = h->implicit_weight[j][i];
  3589. memcpy(h->implicit_weight[16+2*j], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3590. memcpy(h->implicit_weight[16+2*j+1], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3591. }
  3592. }
  3593. static int pred_weight_table(H264Context *h){
  3594. MpegEncContext * const s = &h->s;
  3595. int list, i;
  3596. int luma_def, chroma_def;
  3597. h->use_weight= 0;
  3598. h->use_weight_chroma= 0;
  3599. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  3600. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  3601. luma_def = 1<<h->luma_log2_weight_denom;
  3602. chroma_def = 1<<h->chroma_log2_weight_denom;
  3603. for(list=0; list<2; list++){
  3604. for(i=0; i<h->ref_count[list]; i++){
  3605. int luma_weight_flag, chroma_weight_flag;
  3606. luma_weight_flag= get_bits1(&s->gb);
  3607. if(luma_weight_flag){
  3608. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  3609. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  3610. if( h->luma_weight[list][i] != luma_def
  3611. || h->luma_offset[list][i] != 0)
  3612. h->use_weight= 1;
  3613. }else{
  3614. h->luma_weight[list][i]= luma_def;
  3615. h->luma_offset[list][i]= 0;
  3616. }
  3617. chroma_weight_flag= get_bits1(&s->gb);
  3618. if(chroma_weight_flag){
  3619. int j;
  3620. for(j=0; j<2; j++){
  3621. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  3622. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  3623. if( h->chroma_weight[list][i][j] != chroma_def
  3624. || h->chroma_offset[list][i][j] != 0)
  3625. h->use_weight_chroma= 1;
  3626. }
  3627. }else{
  3628. int j;
  3629. for(j=0; j<2; j++){
  3630. h->chroma_weight[list][i][j]= chroma_def;
  3631. h->chroma_offset[list][i][j]= 0;
  3632. }
  3633. }
  3634. }
  3635. if(h->slice_type != B_TYPE) break;
  3636. }
  3637. h->use_weight= h->use_weight || h->use_weight_chroma;
  3638. return 0;
  3639. }
  3640. static void implicit_weight_table(H264Context *h){
  3641. MpegEncContext * const s = &h->s;
  3642. int ref0, ref1;
  3643. int cur_poc = s->current_picture_ptr->poc;
  3644. if( h->ref_count[0] == 1 && h->ref_count[1] == 1
  3645. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  3646. h->use_weight= 0;
  3647. h->use_weight_chroma= 0;
  3648. return;
  3649. }
  3650. h->use_weight= 2;
  3651. h->use_weight_chroma= 2;
  3652. h->luma_log2_weight_denom= 5;
  3653. h->chroma_log2_weight_denom= 5;
  3654. for(ref0=0; ref0 < h->ref_count[0]; ref0++){
  3655. int poc0 = h->ref_list[0][ref0].poc;
  3656. for(ref1=0; ref1 < h->ref_count[1]; ref1++){
  3657. int poc1 = h->ref_list[1][ref1].poc;
  3658. int td = av_clip(poc1 - poc0, -128, 127);
  3659. if(td){
  3660. int tb = av_clip(cur_poc - poc0, -128, 127);
  3661. int tx = (16384 + (FFABS(td) >> 1)) / td;
  3662. int dist_scale_factor = av_clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
  3663. if(dist_scale_factor < -64 || dist_scale_factor > 128)
  3664. h->implicit_weight[ref0][ref1] = 32;
  3665. else
  3666. h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
  3667. }else
  3668. h->implicit_weight[ref0][ref1] = 32;
  3669. }
  3670. }
  3671. }
  3672. static inline void unreference_pic(H264Context *h, Picture *pic){
  3673. int i;
  3674. pic->reference=0;
  3675. if(pic == h->delayed_output_pic)
  3676. pic->reference=1;
  3677. else{
  3678. for(i = 0; h->delayed_pic[i]; i++)
  3679. if(pic == h->delayed_pic[i]){
  3680. pic->reference=1;
  3681. break;
  3682. }
  3683. }
  3684. }
  3685. /**
  3686. * instantaneous decoder refresh.
  3687. */
  3688. static void idr(H264Context *h){
  3689. int i;
  3690. for(i=0; i<16; i++){
  3691. if (h->long_ref[i] != NULL) {
  3692. unreference_pic(h, h->long_ref[i]);
  3693. h->long_ref[i]= NULL;
  3694. }
  3695. }
  3696. h->long_ref_count=0;
  3697. for(i=0; i<h->short_ref_count; i++){
  3698. unreference_pic(h, h->short_ref[i]);
  3699. h->short_ref[i]= NULL;
  3700. }
  3701. h->short_ref_count=0;
  3702. }
  3703. /* forget old pics after a seek */
  3704. static void flush_dpb(AVCodecContext *avctx){
  3705. H264Context *h= avctx->priv_data;
  3706. int i;
  3707. for(i=0; i<16; i++) {
  3708. if(h->delayed_pic[i])
  3709. h->delayed_pic[i]->reference= 0;
  3710. h->delayed_pic[i]= NULL;
  3711. }
  3712. if(h->delayed_output_pic)
  3713. h->delayed_output_pic->reference= 0;
  3714. h->delayed_output_pic= NULL;
  3715. idr(h);
  3716. if(h->s.current_picture_ptr)
  3717. h->s.current_picture_ptr->reference= 0;
  3718. }
  3719. /**
  3720. *
  3721. * @return the removed picture or NULL if an error occurs
  3722. */
  3723. static Picture * remove_short(H264Context *h, int frame_num){
  3724. MpegEncContext * const s = &h->s;
  3725. int i;
  3726. if(s->avctx->debug&FF_DEBUG_MMCO)
  3727. av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  3728. for(i=0; i<h->short_ref_count; i++){
  3729. Picture *pic= h->short_ref[i];
  3730. if(s->avctx->debug&FF_DEBUG_MMCO)
  3731. av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  3732. if(pic->frame_num == frame_num){
  3733. h->short_ref[i]= NULL;
  3734. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
  3735. h->short_ref_count--;
  3736. return pic;
  3737. }
  3738. }
  3739. return NULL;
  3740. }
  3741. /**
  3742. *
  3743. * @return the removed picture or NULL if an error occurs
  3744. */
  3745. static Picture * remove_long(H264Context *h, int i){
  3746. Picture *pic;
  3747. pic= h->long_ref[i];
  3748. h->long_ref[i]= NULL;
  3749. if(pic) h->long_ref_count--;
  3750. return pic;
  3751. }
  3752. /**
  3753. * print short term list
  3754. */
  3755. static void print_short_term(H264Context *h) {
  3756. uint32_t i;
  3757. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3758. av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
  3759. for(i=0; i<h->short_ref_count; i++){
  3760. Picture *pic= h->short_ref[i];
  3761. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3762. }
  3763. }
  3764. }
  3765. /**
  3766. * print long term list
  3767. */
  3768. static void print_long_term(H264Context *h) {
  3769. uint32_t i;
  3770. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3771. av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
  3772. for(i = 0; i < 16; i++){
  3773. Picture *pic= h->long_ref[i];
  3774. if (pic) {
  3775. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3776. }
  3777. }
  3778. }
  3779. }
  3780. /**
  3781. * Executes the reference picture marking (memory management control operations).
  3782. */
  3783. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  3784. MpegEncContext * const s = &h->s;
  3785. int i, j;
  3786. int current_is_long=0;
  3787. Picture *pic;
  3788. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  3789. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  3790. for(i=0; i<mmco_count; i++){
  3791. if(s->avctx->debug&FF_DEBUG_MMCO)
  3792. 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);
  3793. switch(mmco[i].opcode){
  3794. case MMCO_SHORT2UNUSED:
  3795. pic= remove_short(h, mmco[i].short_frame_num);
  3796. if(pic)
  3797. unreference_pic(h, pic);
  3798. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3799. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_short() failure\n");
  3800. break;
  3801. case MMCO_SHORT2LONG:
  3802. pic= remove_long(h, mmco[i].long_index);
  3803. if(pic) unreference_pic(h, pic);
  3804. h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
  3805. if (h->long_ref[ mmco[i].long_index ]){
  3806. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3807. h->long_ref_count++;
  3808. }
  3809. break;
  3810. case MMCO_LONG2UNUSED:
  3811. pic= remove_long(h, mmco[i].long_index);
  3812. if(pic)
  3813. unreference_pic(h, pic);
  3814. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3815. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_long() failure\n");
  3816. break;
  3817. case MMCO_LONG:
  3818. pic= remove_long(h, mmco[i].long_index);
  3819. if(pic) unreference_pic(h, pic);
  3820. h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
  3821. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3822. h->long_ref_count++;
  3823. current_is_long=1;
  3824. break;
  3825. case MMCO_SET_MAX_LONG:
  3826. assert(mmco[i].long_index <= 16);
  3827. // just remove the long term which index is greater than new max
  3828. for(j = mmco[i].long_index; j<16; j++){
  3829. pic = remove_long(h, j);
  3830. if (pic) unreference_pic(h, pic);
  3831. }
  3832. break;
  3833. case MMCO_RESET:
  3834. while(h->short_ref_count){
  3835. pic= remove_short(h, h->short_ref[0]->frame_num);
  3836. if(pic) unreference_pic(h, pic);
  3837. }
  3838. for(j = 0; j < 16; j++) {
  3839. pic= remove_long(h, j);
  3840. if(pic) unreference_pic(h, pic);
  3841. }
  3842. break;
  3843. default: assert(0);
  3844. }
  3845. }
  3846. if(!current_is_long){
  3847. pic= remove_short(h, s->current_picture_ptr->frame_num);
  3848. if(pic){
  3849. unreference_pic(h, pic);
  3850. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  3851. }
  3852. if(h->short_ref_count)
  3853. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  3854. h->short_ref[0]= s->current_picture_ptr;
  3855. h->short_ref[0]->long_ref=0;
  3856. h->short_ref_count++;
  3857. }
  3858. print_short_term(h);
  3859. print_long_term(h);
  3860. return 0;
  3861. }
  3862. static int decode_ref_pic_marking(H264Context *h){
  3863. MpegEncContext * const s = &h->s;
  3864. int i;
  3865. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  3866. s->broken_link= get_bits1(&s->gb) -1;
  3867. h->mmco[0].long_index= get_bits1(&s->gb) - 1; // current_long_term_idx
  3868. if(h->mmco[0].long_index == -1)
  3869. h->mmco_index= 0;
  3870. else{
  3871. h->mmco[0].opcode= MMCO_LONG;
  3872. h->mmco_index= 1;
  3873. }
  3874. }else{
  3875. if(get_bits1(&s->gb)){ // adaptive_ref_pic_marking_mode_flag
  3876. for(i= 0; i<MAX_MMCO_COUNT; i++) {
  3877. MMCOOpcode opcode= get_ue_golomb(&s->gb);;
  3878. h->mmco[i].opcode= opcode;
  3879. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  3880. 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
  3881. /* if(h->mmco[i].short_frame_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_frame_num ] == NULL){
  3882. av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
  3883. return -1;
  3884. }*/
  3885. }
  3886. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  3887. unsigned int long_index= get_ue_golomb(&s->gb);
  3888. if(/*h->mmco[i].long_index >= h->long_ref_count || h->long_ref[ h->mmco[i].long_index ] == NULL*/ long_index >= 16){
  3889. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  3890. return -1;
  3891. }
  3892. h->mmco[i].long_index= long_index;
  3893. }
  3894. if(opcode > (unsigned)MMCO_LONG){
  3895. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  3896. return -1;
  3897. }
  3898. if(opcode == MMCO_END)
  3899. break;
  3900. }
  3901. h->mmco_index= i;
  3902. }else{
  3903. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  3904. if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){ //FIXME fields
  3905. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  3906. h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  3907. h->mmco_index= 1;
  3908. }else
  3909. h->mmco_index= 0;
  3910. }
  3911. }
  3912. return 0;
  3913. }
  3914. static int init_poc(H264Context *h){
  3915. MpegEncContext * const s = &h->s;
  3916. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  3917. int field_poc[2];
  3918. if(h->nal_unit_type == NAL_IDR_SLICE){
  3919. h->frame_num_offset= 0;
  3920. }else{
  3921. if(h->frame_num < h->prev_frame_num)
  3922. h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
  3923. else
  3924. h->frame_num_offset= h->prev_frame_num_offset;
  3925. }
  3926. if(h->sps.poc_type==0){
  3927. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  3928. if(h->nal_unit_type == NAL_IDR_SLICE){
  3929. h->prev_poc_msb=
  3930. h->prev_poc_lsb= 0;
  3931. }
  3932. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  3933. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  3934. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  3935. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  3936. else
  3937. h->poc_msb = h->prev_poc_msb;
  3938. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  3939. field_poc[0] =
  3940. field_poc[1] = h->poc_msb + h->poc_lsb;
  3941. if(s->picture_structure == PICT_FRAME)
  3942. field_poc[1] += h->delta_poc_bottom;
  3943. }else if(h->sps.poc_type==1){
  3944. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  3945. int i;
  3946. if(h->sps.poc_cycle_length != 0)
  3947. abs_frame_num = h->frame_num_offset + h->frame_num;
  3948. else
  3949. abs_frame_num = 0;
  3950. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  3951. abs_frame_num--;
  3952. expected_delta_per_poc_cycle = 0;
  3953. for(i=0; i < h->sps.poc_cycle_length; i++)
  3954. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  3955. if(abs_frame_num > 0){
  3956. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  3957. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  3958. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  3959. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  3960. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  3961. } else
  3962. expectedpoc = 0;
  3963. if(h->nal_ref_idc == 0)
  3964. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  3965. field_poc[0] = expectedpoc + h->delta_poc[0];
  3966. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  3967. if(s->picture_structure == PICT_FRAME)
  3968. field_poc[1] += h->delta_poc[1];
  3969. }else{
  3970. int poc;
  3971. if(h->nal_unit_type == NAL_IDR_SLICE){
  3972. poc= 0;
  3973. }else{
  3974. if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
  3975. else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
  3976. }
  3977. field_poc[0]= poc;
  3978. field_poc[1]= poc;
  3979. }
  3980. if(s->picture_structure != PICT_BOTTOM_FIELD)
  3981. s->current_picture_ptr->field_poc[0]= field_poc[0];
  3982. if(s->picture_structure != PICT_TOP_FIELD)
  3983. s->current_picture_ptr->field_poc[1]= field_poc[1];
  3984. if(s->picture_structure == PICT_FRAME) // FIXME field pix?
  3985. s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]);
  3986. return 0;
  3987. }
  3988. /**
  3989. * decodes a slice header.
  3990. * this will allso call MPV_common_init() and frame_start() as needed
  3991. */
  3992. static int decode_slice_header(H264Context *h){
  3993. MpegEncContext * const s = &h->s;
  3994. unsigned int first_mb_in_slice;
  3995. unsigned int pps_id;
  3996. int num_ref_idx_active_override_flag;
  3997. static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
  3998. unsigned int slice_type, tmp;
  3999. int default_ref_list_done = 0;
  4000. s->current_picture.reference= h->nal_ref_idc != 0;
  4001. s->dropable= h->nal_ref_idc == 0;
  4002. first_mb_in_slice= get_ue_golomb(&s->gb);
  4003. slice_type= get_ue_golomb(&s->gb);
  4004. if(slice_type > 9){
  4005. 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);
  4006. return -1;
  4007. }
  4008. if(slice_type > 4){
  4009. slice_type -= 5;
  4010. h->slice_type_fixed=1;
  4011. }else
  4012. h->slice_type_fixed=0;
  4013. slice_type= slice_type_map[ slice_type ];
  4014. if (slice_type == I_TYPE
  4015. || (h->slice_num != 0 && slice_type == h->slice_type) ) {
  4016. default_ref_list_done = 1;
  4017. }
  4018. h->slice_type= slice_type;
  4019. s->pict_type= h->slice_type; // to make a few old func happy, it's wrong though
  4020. pps_id= get_ue_golomb(&s->gb);
  4021. if(pps_id>=MAX_PPS_COUNT){
  4022. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  4023. return -1;
  4024. }
  4025. h->pps= h->pps_buffer[pps_id];
  4026. if(h->pps.slice_group_count == 0){
  4027. av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referenced\n");
  4028. return -1;
  4029. }
  4030. h->sps= h->sps_buffer[ h->pps.sps_id ];
  4031. if(h->sps.log2_max_frame_num == 0){
  4032. av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referenced\n");
  4033. return -1;
  4034. }
  4035. if(h->dequant_coeff_pps != pps_id){
  4036. h->dequant_coeff_pps = pps_id;
  4037. init_dequant_tables(h);
  4038. }
  4039. s->mb_width= h->sps.mb_width;
  4040. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  4041. h->b_stride= s->mb_width*4;
  4042. h->b8_stride= s->mb_width*2;
  4043. s->width = 16*s->mb_width - 2*(h->sps.crop_left + h->sps.crop_right );
  4044. if(h->sps.frame_mbs_only_flag)
  4045. s->height= 16*s->mb_height - 2*(h->sps.crop_top + h->sps.crop_bottom);
  4046. else
  4047. s->height= 16*s->mb_height - 4*(h->sps.crop_top + h->sps.crop_bottom); //FIXME recheck
  4048. if (s->context_initialized
  4049. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  4050. free_tables(h);
  4051. MPV_common_end(s);
  4052. }
  4053. if (!s->context_initialized) {
  4054. if (MPV_common_init(s) < 0)
  4055. return -1;
  4056. if(s->dsp.h264_idct_add == ff_h264_idct_add_c){ //FIXME little ugly
  4057. memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
  4058. memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
  4059. }else{
  4060. int i;
  4061. for(i=0; i<16; i++){
  4062. #define T(x) (x>>2) | ((x<<2) & 0xF)
  4063. h->zigzag_scan[i] = T(zigzag_scan[i]);
  4064. h-> field_scan[i] = T( field_scan[i]);
  4065. #undef T
  4066. }
  4067. }
  4068. if(s->dsp.h264_idct8_add == ff_h264_idct8_add_c){
  4069. memcpy(h->zigzag_scan8x8, zigzag_scan8x8, 64*sizeof(uint8_t));
  4070. memcpy(h->zigzag_scan8x8_cavlc, zigzag_scan8x8_cavlc, 64*sizeof(uint8_t));
  4071. memcpy(h->field_scan8x8, field_scan8x8, 64*sizeof(uint8_t));
  4072. memcpy(h->field_scan8x8_cavlc, field_scan8x8_cavlc, 64*sizeof(uint8_t));
  4073. }else{
  4074. int i;
  4075. for(i=0; i<64; i++){
  4076. #define T(x) (x>>3) | ((x&7)<<3)
  4077. h->zigzag_scan8x8[i] = T(zigzag_scan8x8[i]);
  4078. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  4079. h->field_scan8x8[i] = T(field_scan8x8[i]);
  4080. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  4081. #undef T
  4082. }
  4083. }
  4084. if(h->sps.transform_bypass){ //FIXME same ugly
  4085. h->zigzag_scan_q0 = zigzag_scan;
  4086. h->zigzag_scan8x8_q0 = zigzag_scan8x8;
  4087. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  4088. h->field_scan_q0 = field_scan;
  4089. h->field_scan8x8_q0 = field_scan8x8;
  4090. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  4091. }else{
  4092. h->zigzag_scan_q0 = h->zigzag_scan;
  4093. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  4094. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  4095. h->field_scan_q0 = h->field_scan;
  4096. h->field_scan8x8_q0 = h->field_scan8x8;
  4097. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  4098. }
  4099. alloc_tables(h);
  4100. s->avctx->width = s->width;
  4101. s->avctx->height = s->height;
  4102. s->avctx->sample_aspect_ratio= h->sps.sar;
  4103. if(!s->avctx->sample_aspect_ratio.den)
  4104. s->avctx->sample_aspect_ratio.den = 1;
  4105. if(h->sps.timing_info_present_flag){
  4106. s->avctx->time_base= (AVRational){h->sps.num_units_in_tick * 2, h->sps.time_scale};
  4107. if(h->x264_build > 0 && h->x264_build < 44)
  4108. s->avctx->time_base.den *= 2;
  4109. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  4110. s->avctx->time_base.num, s->avctx->time_base.den, 1<<30);
  4111. }
  4112. }
  4113. if(h->slice_num == 0){
  4114. if(frame_start(h) < 0)
  4115. return -1;
  4116. }
  4117. s->current_picture_ptr->frame_num= //FIXME frame_num cleanup
  4118. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  4119. h->mb_mbaff = 0;
  4120. h->mb_aff_frame = 0;
  4121. if(h->sps.frame_mbs_only_flag){
  4122. s->picture_structure= PICT_FRAME;
  4123. }else{
  4124. if(get_bits1(&s->gb)) { //field_pic_flag
  4125. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  4126. av_log(h->s.avctx, AV_LOG_ERROR, "PAFF interlacing is not implemented\n");
  4127. } else {
  4128. s->picture_structure= PICT_FRAME;
  4129. h->mb_aff_frame = h->sps.mb_aff;
  4130. }
  4131. }
  4132. assert(s->mb_num == s->mb_width * s->mb_height);
  4133. if(first_mb_in_slice << h->mb_aff_frame >= s->mb_num ||
  4134. first_mb_in_slice >= s->mb_num){
  4135. av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  4136. return -1;
  4137. }
  4138. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  4139. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << h->mb_aff_frame;
  4140. assert(s->mb_y < s->mb_height);
  4141. if(s->picture_structure==PICT_FRAME){
  4142. h->curr_pic_num= h->frame_num;
  4143. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  4144. }else{
  4145. h->curr_pic_num= 2*h->frame_num;
  4146. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  4147. }
  4148. if(h->nal_unit_type == NAL_IDR_SLICE){
  4149. get_ue_golomb(&s->gb); /* idr_pic_id */
  4150. }
  4151. if(h->sps.poc_type==0){
  4152. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  4153. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  4154. h->delta_poc_bottom= get_se_golomb(&s->gb);
  4155. }
  4156. }
  4157. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  4158. h->delta_poc[0]= get_se_golomb(&s->gb);
  4159. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  4160. h->delta_poc[1]= get_se_golomb(&s->gb);
  4161. }
  4162. init_poc(h);
  4163. if(h->pps.redundant_pic_cnt_present){
  4164. h->redundant_pic_count= get_ue_golomb(&s->gb);
  4165. }
  4166. //set defaults, might be overriden a few line later
  4167. h->ref_count[0]= h->pps.ref_count[0];
  4168. h->ref_count[1]= h->pps.ref_count[1];
  4169. if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){
  4170. if(h->slice_type == B_TYPE){
  4171. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  4172. if(h->sps.mb_aff && h->direct_spatial_mv_pred)
  4173. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + spatial direct mode is not implemented\n");
  4174. }
  4175. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  4176. if(num_ref_idx_active_override_flag){
  4177. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  4178. if(h->slice_type==B_TYPE)
  4179. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  4180. if(h->ref_count[0]-1 > 32-1 || h->ref_count[1]-1 > 32-1){
  4181. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  4182. h->ref_count[0]= h->ref_count[1]= 1;
  4183. return -1;
  4184. }
  4185. }
  4186. if(h->slice_type == B_TYPE)
  4187. h->list_count= 2;
  4188. else
  4189. h->list_count= 1;
  4190. }else
  4191. h->list_count= 0;
  4192. if(!default_ref_list_done){
  4193. fill_default_ref_list(h);
  4194. }
  4195. if(decode_ref_pic_list_reordering(h) < 0)
  4196. return -1;
  4197. if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
  4198. || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
  4199. pred_weight_table(h);
  4200. else if(h->pps.weighted_bipred_idc==2 && h->slice_type==B_TYPE)
  4201. implicit_weight_table(h);
  4202. else
  4203. h->use_weight = 0;
  4204. if(s->current_picture.reference)
  4205. decode_ref_pic_marking(h);
  4206. if(FRAME_MBAFF)
  4207. fill_mbaff_ref_list(h);
  4208. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac ){
  4209. tmp = get_ue_golomb(&s->gb);
  4210. if(tmp > 2){
  4211. av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
  4212. return -1;
  4213. }
  4214. h->cabac_init_idc= tmp;
  4215. }
  4216. h->last_qscale_diff = 0;
  4217. tmp = h->pps.init_qp + get_se_golomb(&s->gb);
  4218. if(tmp>51){
  4219. av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  4220. return -1;
  4221. }
  4222. s->qscale= tmp;
  4223. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4224. //FIXME qscale / qp ... stuff
  4225. if(h->slice_type == SP_TYPE){
  4226. get_bits1(&s->gb); /* sp_for_switch_flag */
  4227. }
  4228. if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
  4229. get_se_golomb(&s->gb); /* slice_qs_delta */
  4230. }
  4231. h->deblocking_filter = 1;
  4232. h->slice_alpha_c0_offset = 0;
  4233. h->slice_beta_offset = 0;
  4234. if( h->pps.deblocking_filter_parameters_present ) {
  4235. tmp= get_ue_golomb(&s->gb);
  4236. if(tmp > 2){
  4237. av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
  4238. return -1;
  4239. }
  4240. h->deblocking_filter= tmp;
  4241. if(h->deblocking_filter < 2)
  4242. h->deblocking_filter^= 1; // 1<->0
  4243. if( h->deblocking_filter ) {
  4244. h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
  4245. h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
  4246. }
  4247. }
  4248. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  4249. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type != I_TYPE)
  4250. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type == B_TYPE)
  4251. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  4252. h->deblocking_filter= 0;
  4253. #if 0 //FMO
  4254. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  4255. slice_group_change_cycle= get_bits(&s->gb, ?);
  4256. #endif
  4257. h->slice_num++;
  4258. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
  4259. h->emu_edge_height= FRAME_MBAFF ? 0 : h->emu_edge_width;
  4260. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  4261. 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",
  4262. h->slice_num,
  4263. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  4264. first_mb_in_slice,
  4265. av_get_pict_type_char(h->slice_type),
  4266. pps_id, h->frame_num,
  4267. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  4268. h->ref_count[0], h->ref_count[1],
  4269. s->qscale,
  4270. h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2,
  4271. h->use_weight,
  4272. h->use_weight==1 && h->use_weight_chroma ? "c" : ""
  4273. );
  4274. }
  4275. if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !s->current_picture.reference){
  4276. s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
  4277. s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
  4278. }else{
  4279. s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
  4280. s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
  4281. }
  4282. return 0;
  4283. }
  4284. /**
  4285. *
  4286. */
  4287. static inline int get_level_prefix(GetBitContext *gb){
  4288. unsigned int buf;
  4289. int log;
  4290. OPEN_READER(re, gb);
  4291. UPDATE_CACHE(re, gb);
  4292. buf=GET_CACHE(re, gb);
  4293. log= 32 - av_log2(buf);
  4294. #ifdef TRACE
  4295. print_bin(buf>>(32-log), log);
  4296. 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__);
  4297. #endif
  4298. LAST_SKIP_BITS(re, gb, log);
  4299. CLOSE_READER(re, gb);
  4300. return log-1;
  4301. }
  4302. static inline int get_dct8x8_allowed(H264Context *h){
  4303. int i;
  4304. for(i=0; i<4; i++){
  4305. if(!IS_SUB_8X8(h->sub_mb_type[i])
  4306. || (!h->sps.direct_8x8_inference_flag && IS_DIRECT(h->sub_mb_type[i])))
  4307. return 0;
  4308. }
  4309. return 1;
  4310. }
  4311. /**
  4312. * decodes a residual block.
  4313. * @param n block index
  4314. * @param scantable scantable
  4315. * @param max_coeff number of coefficients in the block
  4316. * @return <0 if an error occured
  4317. */
  4318. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
  4319. MpegEncContext * const s = &h->s;
  4320. 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};
  4321. int level[16];
  4322. int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
  4323. //FIXME put trailing_onex into the context
  4324. if(n == CHROMA_DC_BLOCK_INDEX){
  4325. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  4326. total_coeff= coeff_token>>2;
  4327. }else{
  4328. if(n == LUMA_DC_BLOCK_INDEX){
  4329. total_coeff= pred_non_zero_count(h, 0);
  4330. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4331. total_coeff= coeff_token>>2;
  4332. }else{
  4333. total_coeff= pred_non_zero_count(h, n);
  4334. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4335. total_coeff= coeff_token>>2;
  4336. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  4337. }
  4338. }
  4339. //FIXME set last_non_zero?
  4340. if(total_coeff==0)
  4341. return 0;
  4342. if(total_coeff > (unsigned)max_coeff) {
  4343. av_log(h->s.avctx, AV_LOG_ERROR, "corrupted macroblock %d %d (total_coeff=%d)\n", s->mb_x, s->mb_y, total_coeff);
  4344. return -1;
  4345. }
  4346. trailing_ones= coeff_token&3;
  4347. tprintf(h->s.avctx, "trailing:%d, total:%d\n", trailing_ones, total_coeff);
  4348. assert(total_coeff<=16);
  4349. for(i=0; i<trailing_ones; i++){
  4350. level[i]= 1 - 2*get_bits1(gb);
  4351. }
  4352. if(i<total_coeff) {
  4353. int level_code, mask;
  4354. int suffix_length = total_coeff > 10 && trailing_ones < 3;
  4355. int prefix= get_level_prefix(gb);
  4356. //first coefficient has suffix_length equal to 0 or 1
  4357. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  4358. if(suffix_length)
  4359. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4360. else
  4361. level_code= (prefix<<suffix_length); //part
  4362. }else if(prefix==14){
  4363. if(suffix_length)
  4364. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4365. else
  4366. level_code= prefix + get_bits(gb, 4); //part
  4367. }else if(prefix==15){
  4368. level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part
  4369. if(suffix_length==0) level_code+=15; //FIXME doesn't make (much)sense
  4370. }else{
  4371. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4372. return -1;
  4373. }
  4374. if(trailing_ones < 3) level_code += 2;
  4375. suffix_length = 1;
  4376. if(level_code > 5)
  4377. suffix_length++;
  4378. mask= -(level_code&1);
  4379. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4380. i++;
  4381. //remaining coefficients have suffix_length > 0
  4382. for(;i<total_coeff;i++) {
  4383. static const int suffix_limit[7] = {0,5,11,23,47,95,INT_MAX };
  4384. prefix = get_level_prefix(gb);
  4385. if(prefix<15){
  4386. level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
  4387. }else if(prefix==15){
  4388. level_code = (prefix<<suffix_length) + get_bits(gb, 12);
  4389. }else{
  4390. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4391. return -1;
  4392. }
  4393. mask= -(level_code&1);
  4394. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4395. if(level_code > suffix_limit[suffix_length])
  4396. suffix_length++;
  4397. }
  4398. }
  4399. if(total_coeff == max_coeff)
  4400. zeros_left=0;
  4401. else{
  4402. if(n == CHROMA_DC_BLOCK_INDEX)
  4403. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  4404. else
  4405. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  4406. }
  4407. coeff_num = zeros_left + total_coeff - 1;
  4408. j = scantable[coeff_num];
  4409. if(n > 24){
  4410. block[j] = level[0];
  4411. for(i=1;i<total_coeff;i++) {
  4412. if(zeros_left <= 0)
  4413. run_before = 0;
  4414. else if(zeros_left < 7){
  4415. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4416. }else{
  4417. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4418. }
  4419. zeros_left -= run_before;
  4420. coeff_num -= 1 + run_before;
  4421. j= scantable[ coeff_num ];
  4422. block[j]= level[i];
  4423. }
  4424. }else{
  4425. block[j] = (level[0] * qmul[j] + 32)>>6;
  4426. for(i=1;i<total_coeff;i++) {
  4427. if(zeros_left <= 0)
  4428. run_before = 0;
  4429. else if(zeros_left < 7){
  4430. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4431. }else{
  4432. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4433. }
  4434. zeros_left -= run_before;
  4435. coeff_num -= 1 + run_before;
  4436. j= scantable[ coeff_num ];
  4437. block[j]= (level[i] * qmul[j] + 32)>>6;
  4438. }
  4439. }
  4440. if(zeros_left<0){
  4441. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  4442. return -1;
  4443. }
  4444. return 0;
  4445. }
  4446. static void predict_field_decoding_flag(H264Context *h){
  4447. MpegEncContext * const s = &h->s;
  4448. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4449. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  4450. ? s->current_picture.mb_type[mb_xy-1]
  4451. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  4452. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  4453. : 0;
  4454. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  4455. }
  4456. /**
  4457. * decodes a P_SKIP or B_SKIP macroblock
  4458. */
  4459. static void decode_mb_skip(H264Context *h){
  4460. MpegEncContext * const s = &h->s;
  4461. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4462. int mb_type=0;
  4463. memset(h->non_zero_count[mb_xy], 0, 16);
  4464. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  4465. if(MB_FIELD)
  4466. mb_type|= MB_TYPE_INTERLACED;
  4467. if( h->slice_type == B_TYPE )
  4468. {
  4469. // just for fill_caches. pred_direct_motion will set the real mb_type
  4470. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
  4471. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4472. pred_direct_motion(h, &mb_type);
  4473. mb_type|= MB_TYPE_SKIP;
  4474. }
  4475. else
  4476. {
  4477. int mx, my;
  4478. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
  4479. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4480. pred_pskip_motion(h, &mx, &my);
  4481. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  4482. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  4483. }
  4484. write_back_motion(h, mb_type);
  4485. s->current_picture.mb_type[mb_xy]= mb_type;
  4486. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4487. h->slice_table[ mb_xy ]= h->slice_num;
  4488. h->prev_mb_skipped= 1;
  4489. }
  4490. /**
  4491. * decodes a macroblock
  4492. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  4493. */
  4494. static int decode_mb_cavlc(H264Context *h){
  4495. MpegEncContext * const s = &h->s;
  4496. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4497. int partition_count;
  4498. unsigned int mb_type, cbp;
  4499. int dct8x8_allowed= h->pps.transform_8x8_mode;
  4500. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?
  4501. tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  4502. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  4503. down the code */
  4504. if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){
  4505. if(s->mb_skip_run==-1)
  4506. s->mb_skip_run= get_ue_golomb(&s->gb);
  4507. if (s->mb_skip_run--) {
  4508. if(FRAME_MBAFF && (s->mb_y&1) == 0){
  4509. if(s->mb_skip_run==0)
  4510. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4511. else
  4512. predict_field_decoding_flag(h);
  4513. }
  4514. decode_mb_skip(h);
  4515. return 0;
  4516. }
  4517. }
  4518. if(FRAME_MBAFF){
  4519. if( (s->mb_y&1) == 0 )
  4520. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4521. }else
  4522. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  4523. h->prev_mb_skipped= 0;
  4524. mb_type= get_ue_golomb(&s->gb);
  4525. if(h->slice_type == B_TYPE){
  4526. if(mb_type < 23){
  4527. partition_count= b_mb_type_info[mb_type].partition_count;
  4528. mb_type= b_mb_type_info[mb_type].type;
  4529. }else{
  4530. mb_type -= 23;
  4531. goto decode_intra_mb;
  4532. }
  4533. }else if(h->slice_type == P_TYPE /*|| h->slice_type == SP_TYPE */){
  4534. if(mb_type < 5){
  4535. partition_count= p_mb_type_info[mb_type].partition_count;
  4536. mb_type= p_mb_type_info[mb_type].type;
  4537. }else{
  4538. mb_type -= 5;
  4539. goto decode_intra_mb;
  4540. }
  4541. }else{
  4542. assert(h->slice_type == I_TYPE);
  4543. decode_intra_mb:
  4544. if(mb_type > 25){
  4545. 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);
  4546. return -1;
  4547. }
  4548. partition_count=0;
  4549. cbp= i_mb_type_info[mb_type].cbp;
  4550. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  4551. mb_type= i_mb_type_info[mb_type].type;
  4552. }
  4553. if(MB_FIELD)
  4554. mb_type |= MB_TYPE_INTERLACED;
  4555. h->slice_table[ mb_xy ]= h->slice_num;
  4556. if(IS_INTRA_PCM(mb_type)){
  4557. unsigned int x, y;
  4558. // we assume these blocks are very rare so we dont optimize it
  4559. align_get_bits(&s->gb);
  4560. // The pixels are stored in the same order as levels in h->mb array.
  4561. for(y=0; y<16; y++){
  4562. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  4563. for(x=0; x<16; x++){
  4564. tprintf(s->avctx, "LUMA ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4565. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= get_bits(&s->gb, 8);
  4566. }
  4567. }
  4568. for(y=0; y<8; y++){
  4569. const int index= 256 + 4*(y&3) + 32*(y>>2);
  4570. for(x=0; x<8; x++){
  4571. tprintf(s->avctx, "CHROMA U ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4572. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4573. }
  4574. }
  4575. for(y=0; y<8; y++){
  4576. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  4577. for(x=0; x<8; x++){
  4578. tprintf(s->avctx, "CHROMA V ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4579. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4580. }
  4581. }
  4582. // In deblocking, the quantizer is 0
  4583. s->current_picture.qscale_table[mb_xy]= 0;
  4584. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  4585. // All coeffs are present
  4586. memset(h->non_zero_count[mb_xy], 16, 16);
  4587. s->current_picture.mb_type[mb_xy]= mb_type;
  4588. return 0;
  4589. }
  4590. if(MB_MBAFF){
  4591. h->ref_count[0] <<= 1;
  4592. h->ref_count[1] <<= 1;
  4593. }
  4594. fill_caches(h, mb_type, 0);
  4595. //mb_pred
  4596. if(IS_INTRA(mb_type)){
  4597. int pred_mode;
  4598. // init_top_left_availability(h);
  4599. if(IS_INTRA4x4(mb_type)){
  4600. int i;
  4601. int di = 1;
  4602. if(dct8x8_allowed && get_bits1(&s->gb)){
  4603. mb_type |= MB_TYPE_8x8DCT;
  4604. di = 4;
  4605. }
  4606. // fill_intra4x4_pred_table(h);
  4607. for(i=0; i<16; i+=di){
  4608. int mode= pred_intra_mode(h, i);
  4609. if(!get_bits1(&s->gb)){
  4610. const int rem_mode= get_bits(&s->gb, 3);
  4611. mode = rem_mode + (rem_mode >= mode);
  4612. }
  4613. if(di==4)
  4614. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  4615. else
  4616. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  4617. }
  4618. write_back_intra_pred_mode(h);
  4619. if( check_intra4x4_pred_mode(h) < 0)
  4620. return -1;
  4621. }else{
  4622. h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
  4623. if(h->intra16x16_pred_mode < 0)
  4624. return -1;
  4625. }
  4626. pred_mode= check_intra_pred_mode(h, get_ue_golomb(&s->gb));
  4627. if(pred_mode < 0)
  4628. return -1;
  4629. h->chroma_pred_mode= pred_mode;
  4630. }else if(partition_count==4){
  4631. int i, j, sub_partition_count[4], list, ref[2][4];
  4632. if(h->slice_type == B_TYPE){
  4633. for(i=0; i<4; i++){
  4634. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4635. if(h->sub_mb_type[i] >=13){
  4636. 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);
  4637. return -1;
  4638. }
  4639. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4640. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4641. }
  4642. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  4643. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
  4644. pred_direct_motion(h, &mb_type);
  4645. h->ref_cache[0][scan8[4]] =
  4646. h->ref_cache[1][scan8[4]] =
  4647. h->ref_cache[0][scan8[12]] =
  4648. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  4649. }
  4650. }else{
  4651. assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE); //FIXME SP correct ?
  4652. for(i=0; i<4; i++){
  4653. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4654. if(h->sub_mb_type[i] >=4){
  4655. 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);
  4656. return -1;
  4657. }
  4658. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4659. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4660. }
  4661. }
  4662. for(list=0; list<h->list_count; list++){
  4663. int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4664. for(i=0; i<4; i++){
  4665. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  4666. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4667. unsigned int tmp = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
  4668. if(tmp>=ref_count){
  4669. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", tmp);
  4670. return -1;
  4671. }
  4672. ref[list][i]= tmp;
  4673. }else{
  4674. //FIXME
  4675. ref[list][i] = -1;
  4676. }
  4677. }
  4678. }
  4679. if(dct8x8_allowed)
  4680. dct8x8_allowed = get_dct8x8_allowed(h);
  4681. for(list=0; list<h->list_count; list++){
  4682. const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4683. for(i=0; i<4; i++){
  4684. if(IS_DIRECT(h->sub_mb_type[i])) {
  4685. h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];
  4686. continue;
  4687. }
  4688. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  4689. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  4690. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4691. const int sub_mb_type= h->sub_mb_type[i];
  4692. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  4693. for(j=0; j<sub_partition_count[i]; j++){
  4694. int mx, my;
  4695. const int index= 4*i + block_width*j;
  4696. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  4697. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  4698. mx += get_se_golomb(&s->gb);
  4699. my += get_se_golomb(&s->gb);
  4700. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4701. if(IS_SUB_8X8(sub_mb_type)){
  4702. mv_cache[ 1 ][0]=
  4703. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  4704. mv_cache[ 1 ][1]=
  4705. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  4706. }else if(IS_SUB_8X4(sub_mb_type)){
  4707. mv_cache[ 1 ][0]= mx;
  4708. mv_cache[ 1 ][1]= my;
  4709. }else if(IS_SUB_4X8(sub_mb_type)){
  4710. mv_cache[ 8 ][0]= mx;
  4711. mv_cache[ 8 ][1]= my;
  4712. }
  4713. mv_cache[ 0 ][0]= mx;
  4714. mv_cache[ 0 ][1]= my;
  4715. }
  4716. }else{
  4717. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  4718. p[0] = p[1]=
  4719. p[8] = p[9]= 0;
  4720. }
  4721. }
  4722. }
  4723. }else if(IS_DIRECT(mb_type)){
  4724. pred_direct_motion(h, &mb_type);
  4725. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  4726. }else{
  4727. int list, mx, my, i;
  4728. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  4729. if(IS_16X16(mb_type)){
  4730. for(list=0; list<h->list_count; list++){
  4731. unsigned int val;
  4732. if(IS_DIR(mb_type, 0, list)){
  4733. val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4734. if(val >= h->ref_count[list]){
  4735. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4736. return -1;
  4737. }
  4738. }else
  4739. val= LIST_NOT_USED&0xFF;
  4740. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  4741. }
  4742. for(list=0; list<h->list_count; list++){
  4743. unsigned int val;
  4744. if(IS_DIR(mb_type, 0, list)){
  4745. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  4746. mx += get_se_golomb(&s->gb);
  4747. my += get_se_golomb(&s->gb);
  4748. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4749. val= pack16to32(mx,my);
  4750. }else
  4751. val=0;
  4752. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, val, 4);
  4753. }
  4754. }
  4755. else if(IS_16X8(mb_type)){
  4756. for(list=0; list<h->list_count; list++){
  4757. for(i=0; i<2; i++){
  4758. unsigned int val;
  4759. if(IS_DIR(mb_type, i, list)){
  4760. val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4761. if(val >= h->ref_count[list]){
  4762. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4763. return -1;
  4764. }
  4765. }else
  4766. val= LIST_NOT_USED&0xFF;
  4767. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  4768. }
  4769. }
  4770. for(list=0; list<h->list_count; list++){
  4771. for(i=0; i<2; i++){
  4772. unsigned int val;
  4773. if(IS_DIR(mb_type, i, list)){
  4774. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  4775. mx += get_se_golomb(&s->gb);
  4776. my += get_se_golomb(&s->gb);
  4777. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4778. val= pack16to32(mx,my);
  4779. }else
  4780. val=0;
  4781. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 4);
  4782. }
  4783. }
  4784. }else{
  4785. assert(IS_8X16(mb_type));
  4786. for(list=0; list<h->list_count; list++){
  4787. for(i=0; i<2; i++){
  4788. unsigned int val;
  4789. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  4790. val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4791. if(val >= h->ref_count[list]){
  4792. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4793. return -1;
  4794. }
  4795. }else
  4796. val= LIST_NOT_USED&0xFF;
  4797. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  4798. }
  4799. }
  4800. for(list=0; list<h->list_count; list++){
  4801. for(i=0; i<2; i++){
  4802. unsigned int val;
  4803. if(IS_DIR(mb_type, i, list)){
  4804. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  4805. mx += get_se_golomb(&s->gb);
  4806. my += get_se_golomb(&s->gb);
  4807. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4808. val= pack16to32(mx,my);
  4809. }else
  4810. val=0;
  4811. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 4);
  4812. }
  4813. }
  4814. }
  4815. }
  4816. if(IS_INTER(mb_type))
  4817. write_back_motion(h, mb_type);
  4818. if(!IS_INTRA16x16(mb_type)){
  4819. cbp= get_ue_golomb(&s->gb);
  4820. if(cbp > 47){
  4821. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%u) at %d %d\n", cbp, s->mb_x, s->mb_y);
  4822. return -1;
  4823. }
  4824. if(IS_INTRA4x4(mb_type))
  4825. cbp= golomb_to_intra4x4_cbp[cbp];
  4826. else
  4827. cbp= golomb_to_inter_cbp[cbp];
  4828. }
  4829. h->cbp = cbp;
  4830. if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
  4831. if(get_bits1(&s->gb))
  4832. mb_type |= MB_TYPE_8x8DCT;
  4833. }
  4834. s->current_picture.mb_type[mb_xy]= mb_type;
  4835. if(cbp || IS_INTRA16x16(mb_type)){
  4836. int i8x8, i4x4, chroma_idx;
  4837. int chroma_qp, dquant;
  4838. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  4839. const uint8_t *scan, *scan8x8, *dc_scan;
  4840. // fill_non_zero_count_cache(h);
  4841. if(IS_INTERLACED(mb_type)){
  4842. scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
  4843. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  4844. dc_scan= luma_dc_field_scan;
  4845. }else{
  4846. scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
  4847. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  4848. dc_scan= luma_dc_zigzag_scan;
  4849. }
  4850. dquant= get_se_golomb(&s->gb);
  4851. if( dquant > 25 || dquant < -26 ){
  4852. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  4853. return -1;
  4854. }
  4855. s->qscale += dquant;
  4856. if(((unsigned)s->qscale) > 51){
  4857. if(s->qscale<0) s->qscale+= 52;
  4858. else s->qscale-= 52;
  4859. }
  4860. h->chroma_qp= chroma_qp= get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4861. if(IS_INTRA16x16(mb_type)){
  4862. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){
  4863. return -1; //FIXME continue if partitioned and other return -1 too
  4864. }
  4865. assert((cbp&15) == 0 || (cbp&15) == 15);
  4866. if(cbp&15){
  4867. for(i8x8=0; i8x8<4; i8x8++){
  4868. for(i4x4=0; i4x4<4; i4x4++){
  4869. const int index= i4x4 + 4*i8x8;
  4870. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){
  4871. return -1;
  4872. }
  4873. }
  4874. }
  4875. }else{
  4876. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  4877. }
  4878. }else{
  4879. for(i8x8=0; i8x8<4; i8x8++){
  4880. if(cbp & (1<<i8x8)){
  4881. if(IS_8x8DCT(mb_type)){
  4882. DCTELEM *buf = &h->mb[64*i8x8];
  4883. uint8_t *nnz;
  4884. for(i4x4=0; i4x4<4; i4x4++){
  4885. if( decode_residual(h, gb, buf, i4x4+4*i8x8, scan8x8+16*i4x4,
  4886. h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )
  4887. return -1;
  4888. }
  4889. nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4890. nnz[0] += nnz[1] + nnz[8] + nnz[9];
  4891. }else{
  4892. for(i4x4=0; i4x4<4; i4x4++){
  4893. const int index= i4x4 + 4*i8x8;
  4894. if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){
  4895. return -1;
  4896. }
  4897. }
  4898. }
  4899. }else{
  4900. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4901. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  4902. }
  4903. }
  4904. }
  4905. if(cbp&0x30){
  4906. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  4907. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){
  4908. return -1;
  4909. }
  4910. }
  4911. if(cbp&0x20){
  4912. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  4913. for(i4x4=0; i4x4<4; i4x4++){
  4914. const int index= 16 + 4*chroma_idx + i4x4;
  4915. 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){
  4916. return -1;
  4917. }
  4918. }
  4919. }
  4920. }else{
  4921. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4922. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4923. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4924. }
  4925. }else{
  4926. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4927. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  4928. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4929. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4930. }
  4931. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4932. write_back_non_zero_count(h);
  4933. if(MB_MBAFF){
  4934. h->ref_count[0] >>= 1;
  4935. h->ref_count[1] >>= 1;
  4936. }
  4937. return 0;
  4938. }
  4939. static int decode_cabac_field_decoding_flag(H264Context *h) {
  4940. MpegEncContext * const s = &h->s;
  4941. const int mb_x = s->mb_x;
  4942. const int mb_y = s->mb_y & ~1;
  4943. const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
  4944. const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
  4945. unsigned int ctx = 0;
  4946. if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
  4947. ctx += 1;
  4948. }
  4949. if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
  4950. ctx += 1;
  4951. }
  4952. return get_cabac_noinline( &h->cabac, &h->cabac_state[70 + ctx] );
  4953. }
  4954. static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
  4955. uint8_t *state= &h->cabac_state[ctx_base];
  4956. int mb_type;
  4957. if(intra_slice){
  4958. MpegEncContext * const s = &h->s;
  4959. const int mba_xy = h->left_mb_xy[0];
  4960. const int mbb_xy = h->top_mb_xy;
  4961. int ctx=0;
  4962. if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
  4963. ctx++;
  4964. if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
  4965. ctx++;
  4966. if( get_cabac_noinline( &h->cabac, &state[ctx] ) == 0 )
  4967. return 0; /* I4x4 */
  4968. state += 2;
  4969. }else{
  4970. if( get_cabac_noinline( &h->cabac, &state[0] ) == 0 )
  4971. return 0; /* I4x4 */
  4972. }
  4973. if( get_cabac_terminate( &h->cabac ) )
  4974. return 25; /* PCM */
  4975. mb_type = 1; /* I16x16 */
  4976. mb_type += 12 * get_cabac_noinline( &h->cabac, &state[1] ); /* cbp_luma != 0 */
  4977. if( get_cabac_noinline( &h->cabac, &state[2] ) ) /* cbp_chroma */
  4978. mb_type += 4 + 4 * get_cabac_noinline( &h->cabac, &state[2+intra_slice] );
  4979. mb_type += 2 * get_cabac_noinline( &h->cabac, &state[3+intra_slice] );
  4980. mb_type += 1 * get_cabac_noinline( &h->cabac, &state[3+2*intra_slice] );
  4981. return mb_type;
  4982. }
  4983. static int decode_cabac_mb_type( H264Context *h ) {
  4984. MpegEncContext * const s = &h->s;
  4985. if( h->slice_type == I_TYPE ) {
  4986. return decode_cabac_intra_mb_type(h, 3, 1);
  4987. } else if( h->slice_type == P_TYPE ) {
  4988. if( get_cabac_noinline( &h->cabac, &h->cabac_state[14] ) == 0 ) {
  4989. /* P-type */
  4990. if( get_cabac_noinline( &h->cabac, &h->cabac_state[15] ) == 0 ) {
  4991. /* P_L0_D16x16, P_8x8 */
  4992. return 3 * get_cabac_noinline( &h->cabac, &h->cabac_state[16] );
  4993. } else {
  4994. /* P_L0_D8x16, P_L0_D16x8 */
  4995. return 2 - get_cabac_noinline( &h->cabac, &h->cabac_state[17] );
  4996. }
  4997. } else {
  4998. return decode_cabac_intra_mb_type(h, 17, 0) + 5;
  4999. }
  5000. } else if( h->slice_type == B_TYPE ) {
  5001. const int mba_xy = h->left_mb_xy[0];
  5002. const int mbb_xy = h->top_mb_xy;
  5003. int ctx = 0;
  5004. int bits;
  5005. if( h->slice_table[mba_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
  5006. ctx++;
  5007. if( h->slice_table[mbb_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
  5008. ctx++;
  5009. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+ctx] ) )
  5010. return 0; /* B_Direct_16x16 */
  5011. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+3] ) ) {
  5012. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
  5013. }
  5014. bits = get_cabac_noinline( &h->cabac, &h->cabac_state[27+4] ) << 3;
  5015. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 2;
  5016. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 1;
  5017. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  5018. if( bits < 8 )
  5019. return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
  5020. else if( bits == 13 ) {
  5021. return decode_cabac_intra_mb_type(h, 32, 0) + 23;
  5022. } else if( bits == 14 )
  5023. return 11; /* B_L1_L0_8x16 */
  5024. else if( bits == 15 )
  5025. return 22; /* B_8x8 */
  5026. bits= ( bits<<1 ) | get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  5027. return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
  5028. } else {
  5029. /* TODO SI/SP frames? */
  5030. return -1;
  5031. }
  5032. }
  5033. static int decode_cabac_mb_skip( H264Context *h, int mb_x, int mb_y ) {
  5034. MpegEncContext * const s = &h->s;
  5035. int mba_xy, mbb_xy;
  5036. int ctx = 0;
  5037. if(FRAME_MBAFF){ //FIXME merge with the stuff in fill_caches?
  5038. int mb_xy = mb_x + (mb_y&~1)*s->mb_stride;
  5039. mba_xy = mb_xy - 1;
  5040. if( (mb_y&1)
  5041. && h->slice_table[mba_xy] == h->slice_num
  5042. && MB_FIELD == !!IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) )
  5043. mba_xy += s->mb_stride;
  5044. if( MB_FIELD ){
  5045. mbb_xy = mb_xy - s->mb_stride;
  5046. if( !(mb_y&1)
  5047. && h->slice_table[mbb_xy] == h->slice_num
  5048. && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) )
  5049. mbb_xy -= s->mb_stride;
  5050. }else
  5051. mbb_xy = mb_x + (mb_y-1)*s->mb_stride;
  5052. }else{
  5053. int mb_xy = mb_x + mb_y*s->mb_stride;
  5054. mba_xy = mb_xy - 1;
  5055. mbb_xy = mb_xy - s->mb_stride;
  5056. }
  5057. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
  5058. ctx++;
  5059. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
  5060. ctx++;
  5061. if( h->slice_type == B_TYPE )
  5062. ctx += 13;
  5063. return get_cabac_noinline( &h->cabac, &h->cabac_state[11+ctx] );
  5064. }
  5065. static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
  5066. int mode = 0;
  5067. if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
  5068. return pred_mode;
  5069. mode += 1 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5070. mode += 2 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5071. mode += 4 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5072. if( mode >= pred_mode )
  5073. return mode + 1;
  5074. else
  5075. return mode;
  5076. }
  5077. static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
  5078. const int mba_xy = h->left_mb_xy[0];
  5079. const int mbb_xy = h->top_mb_xy;
  5080. int ctx = 0;
  5081. /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
  5082. if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
  5083. ctx++;
  5084. if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
  5085. ctx++;
  5086. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
  5087. return 0;
  5088. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  5089. return 1;
  5090. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  5091. return 2;
  5092. else
  5093. return 3;
  5094. }
  5095. static const uint8_t block_idx_x[16] = {
  5096. 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
  5097. };
  5098. static const uint8_t block_idx_y[16] = {
  5099. 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
  5100. };
  5101. static const uint8_t block_idx_xy[4][4] = {
  5102. { 0, 2, 8, 10},
  5103. { 1, 3, 9, 11},
  5104. { 4, 6, 12, 14},
  5105. { 5, 7, 13, 15}
  5106. };
  5107. static int decode_cabac_mb_cbp_luma( H264Context *h) {
  5108. int cbp = 0;
  5109. int cbp_b = -1;
  5110. int i8x8;
  5111. if( h->slice_table[h->top_mb_xy] == h->slice_num ) {
  5112. cbp_b = h->top_cbp;
  5113. tprintf(h->s.avctx, "cbp_b = top_cbp = %x\n", cbp_b);
  5114. }
  5115. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5116. int cbp_a = -1;
  5117. int x, y;
  5118. int ctx = 0;
  5119. x = block_idx_x[4*i8x8];
  5120. y = block_idx_y[4*i8x8];
  5121. if( x > 0 )
  5122. cbp_a = cbp;
  5123. else if( h->slice_table[h->left_mb_xy[0]] == h->slice_num ) {
  5124. cbp_a = h->left_cbp;
  5125. tprintf(h->s.avctx, "cbp_a = left_cbp = %x\n", cbp_a);
  5126. }
  5127. if( y > 0 )
  5128. cbp_b = cbp;
  5129. /* No need to test for skip as we put 0 for skip block */
  5130. /* No need to test for IPCM as we put 1 for IPCM block */
  5131. if( cbp_a >= 0 ) {
  5132. int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
  5133. if( ((cbp_a >> i8x8a)&0x01) == 0 )
  5134. ctx++;
  5135. }
  5136. if( cbp_b >= 0 ) {
  5137. int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
  5138. if( ((cbp_b >> i8x8b)&0x01) == 0 )
  5139. ctx += 2;
  5140. }
  5141. if( get_cabac( &h->cabac, &h->cabac_state[73 + ctx] ) ) {
  5142. cbp |= 1 << i8x8;
  5143. }
  5144. }
  5145. return cbp;
  5146. }
  5147. static int decode_cabac_mb_cbp_chroma( H264Context *h) {
  5148. int ctx;
  5149. int cbp_a, cbp_b;
  5150. cbp_a = (h->left_cbp>>4)&0x03;
  5151. cbp_b = (h-> top_cbp>>4)&0x03;
  5152. ctx = 0;
  5153. if( cbp_a > 0 ) ctx++;
  5154. if( cbp_b > 0 ) ctx += 2;
  5155. if( get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
  5156. return 0;
  5157. ctx = 4;
  5158. if( cbp_a == 2 ) ctx++;
  5159. if( cbp_b == 2 ) ctx += 2;
  5160. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] );
  5161. }
  5162. static int decode_cabac_mb_dqp( H264Context *h) {
  5163. MpegEncContext * const s = &h->s;
  5164. int mbn_xy;
  5165. int ctx = 0;
  5166. int val = 0;
  5167. if( s->mb_x > 0 )
  5168. mbn_xy = s->mb_x + s->mb_y*s->mb_stride - 1;
  5169. else
  5170. mbn_xy = s->mb_width - 1 + (s->mb_y-1)*s->mb_stride;
  5171. if( h->last_qscale_diff != 0 )
  5172. ctx++;
  5173. while( get_cabac_noinline( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
  5174. if( ctx < 2 )
  5175. ctx = 2;
  5176. else
  5177. ctx = 3;
  5178. val++;
  5179. if(val > 102) //prevent infinite loop
  5180. return INT_MIN;
  5181. }
  5182. if( val&0x01 )
  5183. return (val + 1)/2;
  5184. else
  5185. return -(val + 1)/2;
  5186. }
  5187. static int decode_cabac_p_mb_sub_type( H264Context *h ) {
  5188. if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
  5189. return 0; /* 8x8 */
  5190. if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
  5191. return 1; /* 8x4 */
  5192. if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
  5193. return 2; /* 4x8 */
  5194. return 3; /* 4x4 */
  5195. }
  5196. static int decode_cabac_b_mb_sub_type( H264Context *h ) {
  5197. int type;
  5198. if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
  5199. return 0; /* B_Direct_8x8 */
  5200. if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
  5201. return 1 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */
  5202. type = 3;
  5203. if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
  5204. if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
  5205. return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
  5206. type += 4;
  5207. }
  5208. type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
  5209. type += get_cabac( &h->cabac, &h->cabac_state[39] );
  5210. return type;
  5211. }
  5212. static inline int decode_cabac_mb_transform_size( H264Context *h ) {
  5213. return get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
  5214. }
  5215. static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
  5216. int refa = h->ref_cache[list][scan8[n] - 1];
  5217. int refb = h->ref_cache[list][scan8[n] - 8];
  5218. int ref = 0;
  5219. int ctx = 0;
  5220. if( h->slice_type == B_TYPE) {
  5221. if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
  5222. ctx++;
  5223. if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
  5224. ctx += 2;
  5225. } else {
  5226. if( refa > 0 )
  5227. ctx++;
  5228. if( refb > 0 )
  5229. ctx += 2;
  5230. }
  5231. while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
  5232. ref++;
  5233. if( ctx < 4 )
  5234. ctx = 4;
  5235. else
  5236. ctx = 5;
  5237. if(ref >= 32 /*h->ref_list[list]*/){
  5238. av_log(h->s.avctx, AV_LOG_ERROR, "overflow in decode_cabac_mb_ref\n");
  5239. return 0; //FIXME we should return -1 and check the return everywhere
  5240. }
  5241. }
  5242. return ref;
  5243. }
  5244. static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
  5245. int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
  5246. abs( h->mvd_cache[list][scan8[n] - 8][l] );
  5247. int ctxbase = (l == 0) ? 40 : 47;
  5248. int ctx, mvd;
  5249. if( amvd < 3 )
  5250. ctx = 0;
  5251. else if( amvd > 32 )
  5252. ctx = 2;
  5253. else
  5254. ctx = 1;
  5255. if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
  5256. return 0;
  5257. mvd= 1;
  5258. ctx= 3;
  5259. while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
  5260. mvd++;
  5261. if( ctx < 6 )
  5262. ctx++;
  5263. }
  5264. if( mvd >= 9 ) {
  5265. int k = 3;
  5266. while( get_cabac_bypass( &h->cabac ) ) {
  5267. mvd += 1 << k;
  5268. k++;
  5269. if(k>24){
  5270. av_log(h->s.avctx, AV_LOG_ERROR, "overflow in decode_cabac_mb_mvd\n");
  5271. return INT_MIN;
  5272. }
  5273. }
  5274. while( k-- ) {
  5275. if( get_cabac_bypass( &h->cabac ) )
  5276. mvd += 1 << k;
  5277. }
  5278. }
  5279. return get_cabac_bypass_sign( &h->cabac, -mvd );
  5280. }
  5281. static int inline get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) {
  5282. int nza, nzb;
  5283. int ctx = 0;
  5284. if( cat == 0 ) {
  5285. nza = h->left_cbp&0x100;
  5286. nzb = h-> top_cbp&0x100;
  5287. } else if( cat == 1 || cat == 2 ) {
  5288. nza = h->non_zero_count_cache[scan8[idx] - 1];
  5289. nzb = h->non_zero_count_cache[scan8[idx] - 8];
  5290. } else if( cat == 3 ) {
  5291. nza = (h->left_cbp>>(6+idx))&0x01;
  5292. nzb = (h-> top_cbp>>(6+idx))&0x01;
  5293. } else {
  5294. assert(cat == 4);
  5295. nza = h->non_zero_count_cache[scan8[16+idx] - 1];
  5296. nzb = h->non_zero_count_cache[scan8[16+idx] - 8];
  5297. }
  5298. if( nza > 0 )
  5299. ctx++;
  5300. if( nzb > 0 )
  5301. ctx += 2;
  5302. return ctx + 4 * cat;
  5303. }
  5304. static const __attribute((used)) uint8_t last_coeff_flag_offset_8x8[63] = {
  5305. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5306. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  5307. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  5308. 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
  5309. };
  5310. static int decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff) {
  5311. const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
  5312. static const int significant_coeff_flag_offset[2][6] = {
  5313. { 105+0, 105+15, 105+29, 105+44, 105+47, 402 },
  5314. { 277+0, 277+15, 277+29, 277+44, 277+47, 436 }
  5315. };
  5316. static const int last_coeff_flag_offset[2][6] = {
  5317. { 166+0, 166+15, 166+29, 166+44, 166+47, 417 },
  5318. { 338+0, 338+15, 338+29, 338+44, 338+47, 451 }
  5319. };
  5320. static const int coeff_abs_level_m1_offset[6] = {
  5321. 227+0, 227+10, 227+20, 227+30, 227+39, 426
  5322. };
  5323. static const uint8_t significant_coeff_flag_offset_8x8[2][63] = {
  5324. { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
  5325. 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
  5326. 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
  5327. 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12 },
  5328. { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
  5329. 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
  5330. 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
  5331. 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }
  5332. };
  5333. int index[64];
  5334. int last;
  5335. int coeff_count = 0;
  5336. int abslevel1 = 1;
  5337. int abslevelgt1 = 0;
  5338. uint8_t *significant_coeff_ctx_base;
  5339. uint8_t *last_coeff_ctx_base;
  5340. uint8_t *abs_level_m1_ctx_base;
  5341. #ifndef ARCH_X86
  5342. #define CABAC_ON_STACK
  5343. #endif
  5344. #ifdef CABAC_ON_STACK
  5345. #define CC &cc
  5346. CABACContext cc;
  5347. cc.range = h->cabac.range;
  5348. cc.low = h->cabac.low;
  5349. cc.bytestream= h->cabac.bytestream;
  5350. #else
  5351. #define CC &h->cabac
  5352. #endif
  5353. /* cat: 0-> DC 16x16 n = 0
  5354. * 1-> AC 16x16 n = luma4x4idx
  5355. * 2-> Luma4x4 n = luma4x4idx
  5356. * 3-> DC Chroma n = iCbCr
  5357. * 4-> AC Chroma n = 4 * iCbCr + chroma4x4idx
  5358. * 5-> Luma8x8 n = 4 * luma8x8idx
  5359. */
  5360. /* read coded block flag */
  5361. if( cat != 5 ) {
  5362. if( get_cabac( CC, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n ) ] ) == 0 ) {
  5363. if( cat == 1 || cat == 2 )
  5364. h->non_zero_count_cache[scan8[n]] = 0;
  5365. else if( cat == 4 )
  5366. h->non_zero_count_cache[scan8[16+n]] = 0;
  5367. #ifdef CABAC_ON_STACK
  5368. h->cabac.range = cc.range ;
  5369. h->cabac.low = cc.low ;
  5370. h->cabac.bytestream= cc.bytestream;
  5371. #endif
  5372. return 0;
  5373. }
  5374. }
  5375. significant_coeff_ctx_base = h->cabac_state
  5376. + significant_coeff_flag_offset[MB_FIELD][cat];
  5377. last_coeff_ctx_base = h->cabac_state
  5378. + last_coeff_flag_offset[MB_FIELD][cat];
  5379. abs_level_m1_ctx_base = h->cabac_state
  5380. + coeff_abs_level_m1_offset[cat];
  5381. if( cat == 5 ) {
  5382. #define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \
  5383. for(last= 0; last < coefs; last++) { \
  5384. uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \
  5385. if( get_cabac( CC, sig_ctx )) { \
  5386. uint8_t *last_ctx = last_coeff_ctx_base + last_off; \
  5387. index[coeff_count++] = last; \
  5388. if( get_cabac( CC, last_ctx ) ) { \
  5389. last= max_coeff; \
  5390. break; \
  5391. } \
  5392. } \
  5393. }\
  5394. if( last == max_coeff -1 ) {\
  5395. index[coeff_count++] = last;\
  5396. }
  5397. const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
  5398. #if defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__))
  5399. coeff_count= decode_significance_8x8_x86(CC, significant_coeff_ctx_base, index, sig_off);
  5400. } else {
  5401. coeff_count= decode_significance_x86(CC, max_coeff, significant_coeff_ctx_base, index);
  5402. #else
  5403. DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );
  5404. } else {
  5405. DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
  5406. #endif
  5407. }
  5408. assert(coeff_count > 0);
  5409. if( cat == 0 )
  5410. h->cbp_table[mb_xy] |= 0x100;
  5411. else if( cat == 1 || cat == 2 )
  5412. h->non_zero_count_cache[scan8[n]] = coeff_count;
  5413. else if( cat == 3 )
  5414. h->cbp_table[mb_xy] |= 0x40 << n;
  5415. else if( cat == 4 )
  5416. h->non_zero_count_cache[scan8[16+n]] = coeff_count;
  5417. else {
  5418. assert( cat == 5 );
  5419. fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);
  5420. }
  5421. for( coeff_count--; coeff_count >= 0; coeff_count-- ) {
  5422. uint8_t *ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 )) + abs_level_m1_ctx_base;
  5423. int j= scantable[index[coeff_count]];
  5424. if( get_cabac( CC, ctx ) == 0 ) {
  5425. if( !qmul ) {
  5426. block[j] = get_cabac_bypass_sign( CC, -1);
  5427. }else{
  5428. block[j] = (get_cabac_bypass_sign( CC, -qmul[j]) + 32) >> 6;;
  5429. }
  5430. abslevel1++;
  5431. } else {
  5432. int coeff_abs = 2;
  5433. ctx = 5 + FFMIN( 4, abslevelgt1 ) + abs_level_m1_ctx_base;
  5434. while( coeff_abs < 15 && get_cabac( CC, ctx ) ) {
  5435. coeff_abs++;
  5436. }
  5437. if( coeff_abs >= 15 ) {
  5438. int j = 0;
  5439. while( get_cabac_bypass( CC ) ) {
  5440. j++;
  5441. }
  5442. coeff_abs=1;
  5443. while( j-- ) {
  5444. coeff_abs += coeff_abs + get_cabac_bypass( CC );
  5445. }
  5446. coeff_abs+= 14;
  5447. }
  5448. if( !qmul ) {
  5449. if( get_cabac_bypass( CC ) ) block[j] = -coeff_abs;
  5450. else block[j] = coeff_abs;
  5451. }else{
  5452. if( get_cabac_bypass( CC ) ) block[j] = (-coeff_abs * qmul[j] + 32) >> 6;
  5453. else block[j] = ( coeff_abs * qmul[j] + 32) >> 6;
  5454. }
  5455. abslevelgt1++;
  5456. }
  5457. }
  5458. #ifdef CABAC_ON_STACK
  5459. h->cabac.range = cc.range ;
  5460. h->cabac.low = cc.low ;
  5461. h->cabac.bytestream= cc.bytestream;
  5462. #endif
  5463. return 0;
  5464. }
  5465. static void inline compute_mb_neighbors(H264Context *h)
  5466. {
  5467. MpegEncContext * const s = &h->s;
  5468. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  5469. h->top_mb_xy = mb_xy - s->mb_stride;
  5470. h->left_mb_xy[0] = mb_xy - 1;
  5471. if(FRAME_MBAFF){
  5472. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  5473. const int top_pair_xy = pair_xy - s->mb_stride;
  5474. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  5475. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  5476. const int curr_mb_frame_flag = !MB_FIELD;
  5477. const int bottom = (s->mb_y & 1);
  5478. if (bottom
  5479. ? !curr_mb_frame_flag // bottom macroblock
  5480. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  5481. ) {
  5482. h->top_mb_xy -= s->mb_stride;
  5483. }
  5484. if (left_mb_frame_flag != curr_mb_frame_flag) {
  5485. h->left_mb_xy[0] = pair_xy - 1;
  5486. }
  5487. }
  5488. return;
  5489. }
  5490. /**
  5491. * decodes a macroblock
  5492. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  5493. */
  5494. static int decode_mb_cabac(H264Context *h) {
  5495. MpegEncContext * const s = &h->s;
  5496. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  5497. int mb_type, partition_count, cbp = 0;
  5498. int dct8x8_allowed= h->pps.transform_8x8_mode;
  5499. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?)
  5500. tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  5501. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) {
  5502. int skip;
  5503. /* a skipped mb needs the aff flag from the following mb */
  5504. if( FRAME_MBAFF && s->mb_x==0 && (s->mb_y&1)==0 )
  5505. predict_field_decoding_flag(h);
  5506. if( FRAME_MBAFF && (s->mb_y&1)==1 && h->prev_mb_skipped )
  5507. skip = h->next_mb_skipped;
  5508. else
  5509. skip = decode_cabac_mb_skip( h, s->mb_x, s->mb_y );
  5510. /* read skip flags */
  5511. if( skip ) {
  5512. if( FRAME_MBAFF && (s->mb_y&1)==0 ){
  5513. s->current_picture.mb_type[mb_xy] = MB_TYPE_SKIP;
  5514. h->next_mb_skipped = decode_cabac_mb_skip( h, s->mb_x, s->mb_y+1 );
  5515. if(h->next_mb_skipped)
  5516. predict_field_decoding_flag(h);
  5517. else
  5518. h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5519. }
  5520. decode_mb_skip(h);
  5521. h->cbp_table[mb_xy] = 0;
  5522. h->chroma_pred_mode_table[mb_xy] = 0;
  5523. h->last_qscale_diff = 0;
  5524. return 0;
  5525. }
  5526. }
  5527. if(FRAME_MBAFF){
  5528. if( (s->mb_y&1) == 0 )
  5529. h->mb_mbaff =
  5530. h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5531. }else
  5532. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  5533. h->prev_mb_skipped = 0;
  5534. compute_mb_neighbors(h);
  5535. if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
  5536. av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
  5537. return -1;
  5538. }
  5539. if( h->slice_type == B_TYPE ) {
  5540. if( mb_type < 23 ){
  5541. partition_count= b_mb_type_info[mb_type].partition_count;
  5542. mb_type= b_mb_type_info[mb_type].type;
  5543. }else{
  5544. mb_type -= 23;
  5545. goto decode_intra_mb;
  5546. }
  5547. } else if( h->slice_type == P_TYPE ) {
  5548. if( mb_type < 5) {
  5549. partition_count= p_mb_type_info[mb_type].partition_count;
  5550. mb_type= p_mb_type_info[mb_type].type;
  5551. } else {
  5552. mb_type -= 5;
  5553. goto decode_intra_mb;
  5554. }
  5555. } else {
  5556. assert(h->slice_type == I_TYPE);
  5557. decode_intra_mb:
  5558. partition_count = 0;
  5559. cbp= i_mb_type_info[mb_type].cbp;
  5560. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  5561. mb_type= i_mb_type_info[mb_type].type;
  5562. }
  5563. if(MB_FIELD)
  5564. mb_type |= MB_TYPE_INTERLACED;
  5565. h->slice_table[ mb_xy ]= h->slice_num;
  5566. if(IS_INTRA_PCM(mb_type)) {
  5567. const uint8_t *ptr;
  5568. unsigned int x, y;
  5569. // We assume these blocks are very rare so we dont optimize it.
  5570. // FIXME The two following lines get the bitstream position in the cabac
  5571. // decode, I think it should be done by a function in cabac.h (or cabac.c).
  5572. ptr= h->cabac.bytestream;
  5573. if(h->cabac.low&0x1) ptr--;
  5574. if(CABAC_BITS==16){
  5575. if(h->cabac.low&0x1FF) ptr--;
  5576. }
  5577. // The pixels are stored in the same order as levels in h->mb array.
  5578. for(y=0; y<16; y++){
  5579. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  5580. for(x=0; x<16; x++){
  5581. tprintf(s->avctx, "LUMA ICPM LEVEL (%3d)\n", *ptr);
  5582. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= *ptr++;
  5583. }
  5584. }
  5585. for(y=0; y<8; y++){
  5586. const int index= 256 + 4*(y&3) + 32*(y>>2);
  5587. for(x=0; x<8; x++){
  5588. tprintf(s->avctx, "CHROMA U ICPM LEVEL (%3d)\n", *ptr);
  5589. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5590. }
  5591. }
  5592. for(y=0; y<8; y++){
  5593. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  5594. for(x=0; x<8; x++){
  5595. tprintf(s->avctx, "CHROMA V ICPM LEVEL (%3d)\n", *ptr);
  5596. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5597. }
  5598. }
  5599. ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);
  5600. // All blocks are present
  5601. h->cbp_table[mb_xy] = 0x1ef;
  5602. h->chroma_pred_mode_table[mb_xy] = 0;
  5603. // In deblocking, the quantizer is 0
  5604. s->current_picture.qscale_table[mb_xy]= 0;
  5605. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  5606. // All coeffs are present
  5607. memset(h->non_zero_count[mb_xy], 16, 16);
  5608. s->current_picture.mb_type[mb_xy]= mb_type;
  5609. return 0;
  5610. }
  5611. if(MB_MBAFF){
  5612. h->ref_count[0] <<= 1;
  5613. h->ref_count[1] <<= 1;
  5614. }
  5615. fill_caches(h, mb_type, 0);
  5616. if( IS_INTRA( mb_type ) ) {
  5617. int i, pred_mode;
  5618. if( IS_INTRA4x4( mb_type ) ) {
  5619. if( dct8x8_allowed && decode_cabac_mb_transform_size( h ) ) {
  5620. mb_type |= MB_TYPE_8x8DCT;
  5621. for( i = 0; i < 16; i+=4 ) {
  5622. int pred = pred_intra_mode( h, i );
  5623. int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5624. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  5625. }
  5626. } else {
  5627. for( i = 0; i < 16; i++ ) {
  5628. int pred = pred_intra_mode( h, i );
  5629. h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5630. //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
  5631. }
  5632. }
  5633. write_back_intra_pred_mode(h);
  5634. if( check_intra4x4_pred_mode(h) < 0 ) return -1;
  5635. } else {
  5636. h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );
  5637. if( h->intra16x16_pred_mode < 0 ) return -1;
  5638. }
  5639. h->chroma_pred_mode_table[mb_xy] =
  5640. pred_mode = decode_cabac_mb_chroma_pre_mode( h );
  5641. pred_mode= check_intra_pred_mode( h, pred_mode );
  5642. if( pred_mode < 0 ) return -1;
  5643. h->chroma_pred_mode= pred_mode;
  5644. } else if( partition_count == 4 ) {
  5645. int i, j, sub_partition_count[4], list, ref[2][4];
  5646. if( h->slice_type == B_TYPE ) {
  5647. for( i = 0; i < 4; i++ ) {
  5648. h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
  5649. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5650. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5651. }
  5652. if( IS_DIRECT(h->sub_mb_type[0] | h->sub_mb_type[1] |
  5653. h->sub_mb_type[2] | h->sub_mb_type[3]) ) {
  5654. pred_direct_motion(h, &mb_type);
  5655. if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
  5656. for( i = 0; i < 4; i++ )
  5657. if( IS_DIRECT(h->sub_mb_type[i]) )
  5658. fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );
  5659. }
  5660. }
  5661. } else {
  5662. for( i = 0; i < 4; i++ ) {
  5663. h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
  5664. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5665. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5666. }
  5667. }
  5668. for( list = 0; list < h->list_count; list++ ) {
  5669. for( i = 0; i < 4; i++ ) {
  5670. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  5671. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  5672. if( h->ref_count[list] > 1 )
  5673. ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );
  5674. else
  5675. ref[list][i] = 0;
  5676. } else {
  5677. ref[list][i] = -1;
  5678. }
  5679. h->ref_cache[list][ scan8[4*i]+1 ]=
  5680. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  5681. }
  5682. }
  5683. if(dct8x8_allowed)
  5684. dct8x8_allowed = get_dct8x8_allowed(h);
  5685. for(list=0; list<h->list_count; list++){
  5686. for(i=0; i<4; i++){
  5687. if(IS_DIRECT(h->sub_mb_type[i])){
  5688. fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);
  5689. continue;
  5690. }
  5691. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];
  5692. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  5693. const int sub_mb_type= h->sub_mb_type[i];
  5694. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  5695. for(j=0; j<sub_partition_count[i]; j++){
  5696. int mpx, mpy;
  5697. int mx, my;
  5698. const int index= 4*i + block_width*j;
  5699. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  5700. int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];
  5701. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);
  5702. mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );
  5703. my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );
  5704. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5705. if(IS_SUB_8X8(sub_mb_type)){
  5706. mv_cache[ 1 ][0]=
  5707. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  5708. mv_cache[ 1 ][1]=
  5709. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  5710. mvd_cache[ 1 ][0]=
  5711. mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;
  5712. mvd_cache[ 1 ][1]=
  5713. mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;
  5714. }else if(IS_SUB_8X4(sub_mb_type)){
  5715. mv_cache[ 1 ][0]= mx;
  5716. mv_cache[ 1 ][1]= my;
  5717. mvd_cache[ 1 ][0]= mx - mpx;
  5718. mvd_cache[ 1 ][1]= my - mpy;
  5719. }else if(IS_SUB_4X8(sub_mb_type)){
  5720. mv_cache[ 8 ][0]= mx;
  5721. mv_cache[ 8 ][1]= my;
  5722. mvd_cache[ 8 ][0]= mx - mpx;
  5723. mvd_cache[ 8 ][1]= my - mpy;
  5724. }
  5725. mv_cache[ 0 ][0]= mx;
  5726. mv_cache[ 0 ][1]= my;
  5727. mvd_cache[ 0 ][0]= mx - mpx;
  5728. mvd_cache[ 0 ][1]= my - mpy;
  5729. }
  5730. }else{
  5731. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  5732. uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];
  5733. p[0] = p[1] = p[8] = p[9] = 0;
  5734. pd[0]= pd[1]= pd[8]= pd[9]= 0;
  5735. }
  5736. }
  5737. }
  5738. } else if( IS_DIRECT(mb_type) ) {
  5739. pred_direct_motion(h, &mb_type);
  5740. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  5741. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  5742. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  5743. } else {
  5744. int list, mx, my, i, mpx, mpy;
  5745. if(IS_16X16(mb_type)){
  5746. for(list=0; list<h->list_count; list++){
  5747. if(IS_DIR(mb_type, 0, list)){
  5748. const int ref = h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 0 ) : 0;
  5749. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
  5750. }else
  5751. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1); //FIXME factorize and the other fill_rect below too
  5752. }
  5753. for(list=0; list<h->list_count; list++){
  5754. if(IS_DIR(mb_type, 0, list)){
  5755. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);
  5756. mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );
  5757. my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );
  5758. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5759. fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5760. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  5761. }else
  5762. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  5763. }
  5764. }
  5765. else if(IS_16X8(mb_type)){
  5766. for(list=0; list<h->list_count; list++){
  5767. for(i=0; i<2; i++){
  5768. if(IS_DIR(mb_type, i, list)){
  5769. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 8*i ) : 0;
  5770. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
  5771. }else
  5772. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  5773. }
  5774. }
  5775. for(list=0; list<h->list_count; list++){
  5776. for(i=0; i<2; i++){
  5777. if(IS_DIR(mb_type, i, list)){
  5778. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);
  5779. mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );
  5780. my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );
  5781. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5782. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);
  5783. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  5784. }else{
  5785. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5786. fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5787. }
  5788. }
  5789. }
  5790. }else{
  5791. assert(IS_8X16(mb_type));
  5792. for(list=0; list<h->list_count; list++){
  5793. for(i=0; i<2; i++){
  5794. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  5795. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 4*i ) : 0;
  5796. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
  5797. }else
  5798. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  5799. }
  5800. }
  5801. for(list=0; list<h->list_count; list++){
  5802. for(i=0; i<2; i++){
  5803. if(IS_DIR(mb_type, i, list)){
  5804. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);
  5805. mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );
  5806. my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );
  5807. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5808. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5809. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  5810. }else{
  5811. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5812. fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5813. }
  5814. }
  5815. }
  5816. }
  5817. }
  5818. if( IS_INTER( mb_type ) ) {
  5819. h->chroma_pred_mode_table[mb_xy] = 0;
  5820. write_back_motion( h, mb_type );
  5821. }
  5822. if( !IS_INTRA16x16( mb_type ) ) {
  5823. cbp = decode_cabac_mb_cbp_luma( h );
  5824. cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
  5825. }
  5826. h->cbp_table[mb_xy] = h->cbp = cbp;
  5827. if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
  5828. if( decode_cabac_mb_transform_size( h ) )
  5829. mb_type |= MB_TYPE_8x8DCT;
  5830. }
  5831. s->current_picture.mb_type[mb_xy]= mb_type;
  5832. if( cbp || IS_INTRA16x16( mb_type ) ) {
  5833. const uint8_t *scan, *scan8x8, *dc_scan;
  5834. int dqp;
  5835. if(IS_INTERLACED(mb_type)){
  5836. scan8x8= s->qscale ? h->field_scan8x8 : h->field_scan8x8_q0;
  5837. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  5838. dc_scan= luma_dc_field_scan;
  5839. }else{
  5840. scan8x8= s->qscale ? h->zigzag_scan8x8 : h->zigzag_scan8x8_q0;
  5841. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  5842. dc_scan= luma_dc_zigzag_scan;
  5843. }
  5844. h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
  5845. if( dqp == INT_MIN ){
  5846. av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", s->mb_x, s->mb_y);
  5847. return -1;
  5848. }
  5849. s->qscale += dqp;
  5850. if(((unsigned)s->qscale) > 51){
  5851. if(s->qscale<0) s->qscale+= 52;
  5852. else s->qscale-= 52;
  5853. }
  5854. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  5855. if( IS_INTRA16x16( mb_type ) ) {
  5856. int i;
  5857. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
  5858. if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16) < 0)
  5859. return -1;
  5860. if( cbp&15 ) {
  5861. for( i = 0; i < 16; i++ ) {
  5862. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
  5863. if( decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 )
  5864. return -1;
  5865. }
  5866. } else {
  5867. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  5868. }
  5869. } else {
  5870. int i8x8, i4x4;
  5871. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5872. if( cbp & (1<<i8x8) ) {
  5873. if( IS_8x8DCT(mb_type) ) {
  5874. if( decode_cabac_residual(h, h->mb + 64*i8x8, 5, 4*i8x8,
  5875. scan8x8, h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 64) < 0 )
  5876. return -1;
  5877. } else
  5878. for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
  5879. const int index = 4*i8x8 + i4x4;
  5880. //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
  5881. //START_TIMER
  5882. 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 )
  5883. return -1;
  5884. //STOP_TIMER("decode_residual")
  5885. }
  5886. } else {
  5887. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  5888. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  5889. }
  5890. }
  5891. }
  5892. if( cbp&0x30 ){
  5893. int c;
  5894. for( c = 0; c < 2; c++ ) {
  5895. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
  5896. if( decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, NULL, 4) < 0)
  5897. return -1;
  5898. }
  5899. }
  5900. if( cbp&0x20 ) {
  5901. int c, i;
  5902. for( c = 0; c < 2; c++ ) {
  5903. for( i = 0; i < 4; i++ ) {
  5904. const int index = 16 + 4 * c + i;
  5905. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
  5906. 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)
  5907. return -1;
  5908. }
  5909. }
  5910. } else {
  5911. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5912. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5913. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5914. }
  5915. } else {
  5916. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5917. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  5918. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5919. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5920. h->last_qscale_diff = 0;
  5921. }
  5922. s->current_picture.qscale_table[mb_xy]= s->qscale;
  5923. write_back_non_zero_count(h);
  5924. if(MB_MBAFF){
  5925. h->ref_count[0] >>= 1;
  5926. h->ref_count[1] >>= 1;
  5927. }
  5928. return 0;
  5929. }
  5930. static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5931. int i, d;
  5932. const int index_a = qp + h->slice_alpha_c0_offset;
  5933. const int alpha = (alpha_table+52)[index_a];
  5934. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5935. if( bS[0] < 4 ) {
  5936. int8_t tc[4];
  5937. for(i=0; i<4; i++)
  5938. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  5939. h->s.dsp.h264_h_loop_filter_luma(pix, stride, alpha, beta, tc);
  5940. } else {
  5941. /* 16px edge length, because bS=4 is triggered by being at
  5942. * the edge of an intra MB, so all 4 bS are the same */
  5943. for( d = 0; d < 16; d++ ) {
  5944. const int p0 = pix[-1];
  5945. const int p1 = pix[-2];
  5946. const int p2 = pix[-3];
  5947. const int q0 = pix[0];
  5948. const int q1 = pix[1];
  5949. const int q2 = pix[2];
  5950. if( FFABS( p0 - q0 ) < alpha &&
  5951. FFABS( p1 - p0 ) < beta &&
  5952. FFABS( q1 - q0 ) < beta ) {
  5953. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5954. if( FFABS( p2 - p0 ) < beta)
  5955. {
  5956. const int p3 = pix[-4];
  5957. /* p0', p1', p2' */
  5958. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5959. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5960. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5961. } else {
  5962. /* p0' */
  5963. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5964. }
  5965. if( FFABS( q2 - q0 ) < beta)
  5966. {
  5967. const int q3 = pix[3];
  5968. /* q0', q1', q2' */
  5969. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5970. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5971. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5972. } else {
  5973. /* q0' */
  5974. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5975. }
  5976. }else{
  5977. /* p0', q0' */
  5978. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5979. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5980. }
  5981. tprintf(h->s.avctx, "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]);
  5982. }
  5983. pix += stride;
  5984. }
  5985. }
  5986. }
  5987. static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5988. int i;
  5989. const int index_a = qp + h->slice_alpha_c0_offset;
  5990. const int alpha = (alpha_table+52)[index_a];
  5991. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5992. if( bS[0] < 4 ) {
  5993. int8_t tc[4];
  5994. for(i=0; i<4; i++)
  5995. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  5996. h->s.dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
  5997. } else {
  5998. h->s.dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
  5999. }
  6000. }
  6001. static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  6002. int i;
  6003. for( i = 0; i < 16; i++, pix += stride) {
  6004. int index_a;
  6005. int alpha;
  6006. int beta;
  6007. int qp_index;
  6008. int bS_index = (i >> 1);
  6009. if (!MB_FIELD) {
  6010. bS_index &= ~1;
  6011. bS_index |= (i & 1);
  6012. }
  6013. if( bS[bS_index] == 0 ) {
  6014. continue;
  6015. }
  6016. qp_index = MB_FIELD ? (i >> 3) : (i & 1);
  6017. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  6018. alpha = (alpha_table+52)[index_a];
  6019. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  6020. if( bS[bS_index] < 4 ) {
  6021. const int tc0 = (tc0_table+52)[index_a][bS[bS_index] - 1];
  6022. const int p0 = pix[-1];
  6023. const int p1 = pix[-2];
  6024. const int p2 = pix[-3];
  6025. const int q0 = pix[0];
  6026. const int q1 = pix[1];
  6027. const int q2 = pix[2];
  6028. if( FFABS( p0 - q0 ) < alpha &&
  6029. FFABS( p1 - p0 ) < beta &&
  6030. FFABS( q1 - q0 ) < beta ) {
  6031. int tc = tc0;
  6032. int i_delta;
  6033. if( FFABS( p2 - p0 ) < beta ) {
  6034. pix[-2] = p1 + av_clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  6035. tc++;
  6036. }
  6037. if( FFABS( q2 - q0 ) < beta ) {
  6038. pix[1] = q1 + av_clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  6039. tc++;
  6040. }
  6041. i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  6042. pix[-1] = av_clip_uint8( p0 + i_delta ); /* p0' */
  6043. pix[0] = av_clip_uint8( q0 - i_delta ); /* q0' */
  6044. tprintf(h->s.avctx, "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);
  6045. }
  6046. }else{
  6047. const int p0 = pix[-1];
  6048. const int p1 = pix[-2];
  6049. const int p2 = pix[-3];
  6050. const int q0 = pix[0];
  6051. const int q1 = pix[1];
  6052. const int q2 = pix[2];
  6053. if( FFABS( p0 - q0 ) < alpha &&
  6054. FFABS( p1 - p0 ) < beta &&
  6055. FFABS( q1 - q0 ) < beta ) {
  6056. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  6057. if( FFABS( p2 - p0 ) < beta)
  6058. {
  6059. const int p3 = pix[-4];
  6060. /* p0', p1', p2' */
  6061. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  6062. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  6063. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  6064. } else {
  6065. /* p0' */
  6066. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6067. }
  6068. if( FFABS( q2 - q0 ) < beta)
  6069. {
  6070. const int q3 = pix[3];
  6071. /* q0', q1', q2' */
  6072. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  6073. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  6074. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  6075. } else {
  6076. /* q0' */
  6077. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6078. }
  6079. }else{
  6080. /* p0', q0' */
  6081. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6082. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6083. }
  6084. tprintf(h->s.avctx, "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]);
  6085. }
  6086. }
  6087. }
  6088. }
  6089. static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  6090. int i;
  6091. for( i = 0; i < 8; i++, pix += stride) {
  6092. int index_a;
  6093. int alpha;
  6094. int beta;
  6095. int qp_index;
  6096. int bS_index = i;
  6097. if( bS[bS_index] == 0 ) {
  6098. continue;
  6099. }
  6100. qp_index = MB_FIELD ? (i >> 2) : (i & 1);
  6101. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  6102. alpha = (alpha_table+52)[index_a];
  6103. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  6104. if( bS[bS_index] < 4 ) {
  6105. const int tc = (tc0_table+52)[index_a][bS[bS_index] - 1] + 1;
  6106. const int p0 = pix[-1];
  6107. const int p1 = pix[-2];
  6108. const int q0 = pix[0];
  6109. const int q1 = pix[1];
  6110. if( FFABS( p0 - q0 ) < alpha &&
  6111. FFABS( p1 - p0 ) < beta &&
  6112. FFABS( q1 - q0 ) < beta ) {
  6113. const int i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  6114. pix[-1] = av_clip_uint8( p0 + i_delta ); /* p0' */
  6115. pix[0] = av_clip_uint8( q0 - i_delta ); /* q0' */
  6116. tprintf(h->s.avctx, "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);
  6117. }
  6118. }else{
  6119. const int p0 = pix[-1];
  6120. const int p1 = pix[-2];
  6121. const int q0 = pix[0];
  6122. const int q1 = pix[1];
  6123. if( FFABS( p0 - q0 ) < alpha &&
  6124. FFABS( p1 - p0 ) < beta &&
  6125. FFABS( q1 - q0 ) < beta ) {
  6126. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  6127. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  6128. tprintf(h->s.avctx, "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]);
  6129. }
  6130. }
  6131. }
  6132. }
  6133. static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  6134. int i, d;
  6135. const int index_a = qp + h->slice_alpha_c0_offset;
  6136. const int alpha = (alpha_table+52)[index_a];
  6137. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  6138. const int pix_next = stride;
  6139. if( bS[0] < 4 ) {
  6140. int8_t tc[4];
  6141. for(i=0; i<4; i++)
  6142. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  6143. h->s.dsp.h264_v_loop_filter_luma(pix, stride, alpha, beta, tc);
  6144. } else {
  6145. /* 16px edge length, see filter_mb_edgev */
  6146. for( d = 0; d < 16; d++ ) {
  6147. const int p0 = pix[-1*pix_next];
  6148. const int p1 = pix[-2*pix_next];
  6149. const int p2 = pix[-3*pix_next];
  6150. const int q0 = pix[0];
  6151. const int q1 = pix[1*pix_next];
  6152. const int q2 = pix[2*pix_next];
  6153. if( FFABS( p0 - q0 ) < alpha &&
  6154. FFABS( p1 - p0 ) < beta &&
  6155. FFABS( q1 - q0 ) < beta ) {
  6156. const int p3 = pix[-4*pix_next];
  6157. const int q3 = pix[ 3*pix_next];
  6158. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  6159. if( FFABS( p2 - p0 ) < beta) {
  6160. /* p0', p1', p2' */
  6161. pix[-1*pix_next] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  6162. pix[-2*pix_next] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  6163. pix[-3*pix_next] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  6164. } else {
  6165. /* p0' */
  6166. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6167. }
  6168. if( FFABS( q2 - q0 ) < beta) {
  6169. /* q0', q1', q2' */
  6170. pix[0*pix_next] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  6171. pix[1*pix_next] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  6172. pix[2*pix_next] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  6173. } else {
  6174. /* q0' */
  6175. pix[0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6176. }
  6177. }else{
  6178. /* p0', q0' */
  6179. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6180. pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6181. }
  6182. tprintf(h->s.avctx, "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]);
  6183. }
  6184. pix++;
  6185. }
  6186. }
  6187. }
  6188. static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  6189. int i;
  6190. const int index_a = qp + h->slice_alpha_c0_offset;
  6191. const int alpha = (alpha_table+52)[index_a];
  6192. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  6193. if( bS[0] < 4 ) {
  6194. int8_t tc[4];
  6195. for(i=0; i<4; i++)
  6196. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  6197. h->s.dsp.h264_v_loop_filter_chroma(pix, stride, alpha, beta, tc);
  6198. } else {
  6199. h->s.dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);
  6200. }
  6201. }
  6202. 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) {
  6203. MpegEncContext * const s = &h->s;
  6204. int mb_xy, mb_type;
  6205. int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh;
  6206. if(mb_x==0 || mb_y==0 || !s->dsp.h264_loop_filter_strength) {
  6207. filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);
  6208. return;
  6209. }
  6210. assert(!FRAME_MBAFF);
  6211. mb_xy = mb_x + mb_y*s->mb_stride;
  6212. mb_type = s->current_picture.mb_type[mb_xy];
  6213. qp = s->current_picture.qscale_table[mb_xy];
  6214. qp0 = s->current_picture.qscale_table[mb_xy-1];
  6215. qp1 = s->current_picture.qscale_table[h->top_mb_xy];
  6216. qpc = get_chroma_qp( h->pps.chroma_qp_index_offset, qp );
  6217. qpc0 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp0 );
  6218. qpc1 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp1 );
  6219. qp0 = (qp + qp0 + 1) >> 1;
  6220. qp1 = (qp + qp1 + 1) >> 1;
  6221. qpc0 = (qpc + qpc0 + 1) >> 1;
  6222. qpc1 = (qpc + qpc1 + 1) >> 1;
  6223. qp_thresh = 15 - h->slice_alpha_c0_offset;
  6224. if(qp <= qp_thresh && qp0 <= qp_thresh && qp1 <= qp_thresh &&
  6225. qpc <= qp_thresh && qpc0 <= qp_thresh && qpc1 <= qp_thresh)
  6226. return;
  6227. if( IS_INTRA(mb_type) ) {
  6228. int16_t bS4[4] = {4,4,4,4};
  6229. int16_t bS3[4] = {3,3,3,3};
  6230. if( IS_8x8DCT(mb_type) ) {
  6231. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  6232. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  6233. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  6234. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  6235. } else {
  6236. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  6237. filter_mb_edgev( h, &img_y[4*1], linesize, bS3, qp );
  6238. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  6239. filter_mb_edgev( h, &img_y[4*3], linesize, bS3, qp );
  6240. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  6241. filter_mb_edgeh( h, &img_y[4*1*linesize], linesize, bS3, qp );
  6242. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  6243. filter_mb_edgeh( h, &img_y[4*3*linesize], linesize, bS3, qp );
  6244. }
  6245. filter_mb_edgecv( h, &img_cb[2*0], uvlinesize, bS4, qpc0 );
  6246. filter_mb_edgecv( h, &img_cb[2*2], uvlinesize, bS3, qpc );
  6247. filter_mb_edgecv( h, &img_cr[2*0], uvlinesize, bS4, qpc0 );
  6248. filter_mb_edgecv( h, &img_cr[2*2], uvlinesize, bS3, qpc );
  6249. filter_mb_edgech( h, &img_cb[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  6250. filter_mb_edgech( h, &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc );
  6251. filter_mb_edgech( h, &img_cr[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  6252. filter_mb_edgech( h, &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc );
  6253. return;
  6254. } else {
  6255. DECLARE_ALIGNED_8(int16_t, bS[2][4][4]);
  6256. uint64_t (*bSv)[4] = (uint64_t(*)[4])bS;
  6257. int edges;
  6258. if( IS_8x8DCT(mb_type) && (h->cbp&7) == 7 ) {
  6259. edges = 4;
  6260. bSv[0][0] = bSv[0][2] = bSv[1][0] = bSv[1][2] = 0x0002000200020002ULL;
  6261. } else {
  6262. int mask_edge1 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 :
  6263. (mb_type & MB_TYPE_16x8) ? 1 : 0;
  6264. int mask_edge0 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16))
  6265. && (s->current_picture.mb_type[mb_xy-1] & (MB_TYPE_16x16 | MB_TYPE_8x16))
  6266. ? 3 : 0;
  6267. int step = IS_8x8DCT(mb_type) ? 2 : 1;
  6268. edges = (mb_type & MB_TYPE_16x16) && !(h->cbp & 15) ? 1 : 4;
  6269. s->dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,
  6270. (h->slice_type == B_TYPE), edges, step, mask_edge0, mask_edge1 );
  6271. }
  6272. if( IS_INTRA(s->current_picture.mb_type[mb_xy-1]) )
  6273. bSv[0][0] = 0x0004000400040004ULL;
  6274. if( IS_INTRA(s->current_picture.mb_type[h->top_mb_xy]) )
  6275. bSv[1][0] = 0x0004000400040004ULL;
  6276. #define FILTER(hv,dir,edge)\
  6277. if(bSv[dir][edge]) {\
  6278. filter_mb_edge##hv( h, &img_y[4*edge*(dir?linesize:1)], linesize, bS[dir][edge], edge ? qp : qp##dir );\
  6279. if(!(edge&1)) {\
  6280. filter_mb_edgec##hv( h, &img_cb[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  6281. filter_mb_edgec##hv( h, &img_cr[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  6282. }\
  6283. }
  6284. if( edges == 1 ) {
  6285. FILTER(v,0,0);
  6286. FILTER(h,1,0);
  6287. } else if( IS_8x8DCT(mb_type) ) {
  6288. FILTER(v,0,0);
  6289. FILTER(v,0,2);
  6290. FILTER(h,1,0);
  6291. FILTER(h,1,2);
  6292. } else {
  6293. FILTER(v,0,0);
  6294. FILTER(v,0,1);
  6295. FILTER(v,0,2);
  6296. FILTER(v,0,3);
  6297. FILTER(h,1,0);
  6298. FILTER(h,1,1);
  6299. FILTER(h,1,2);
  6300. FILTER(h,1,3);
  6301. }
  6302. #undef FILTER
  6303. }
  6304. }
  6305. 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) {
  6306. MpegEncContext * const s = &h->s;
  6307. const int mb_xy= mb_x + mb_y*s->mb_stride;
  6308. const int mb_type = s->current_picture.mb_type[mb_xy];
  6309. const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4;
  6310. int first_vertical_edge_done = 0;
  6311. int dir;
  6312. /* FIXME: A given frame may occupy more than one position in
  6313. * the reference list. So ref2frm should be populated with
  6314. * frame numbers, not indices. */
  6315. static const int ref2frm[34] = {-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  6316. 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
  6317. //for sufficiently low qp, filtering wouldn't do anything
  6318. //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
  6319. if(!FRAME_MBAFF){
  6320. int qp_thresh = 15 - h->slice_alpha_c0_offset - FFMAX(0, h->pps.chroma_qp_index_offset);
  6321. int qp = s->current_picture.qscale_table[mb_xy];
  6322. if(qp <= qp_thresh
  6323. && (mb_x == 0 || ((qp + s->current_picture.qscale_table[mb_xy-1] + 1)>>1) <= qp_thresh)
  6324. && (mb_y == 0 || ((qp + s->current_picture.qscale_table[h->top_mb_xy] + 1)>>1) <= qp_thresh)){
  6325. return;
  6326. }
  6327. }
  6328. if (FRAME_MBAFF
  6329. // left mb is in picture
  6330. && h->slice_table[mb_xy-1] != 255
  6331. // and current and left pair do not have the same interlaced type
  6332. && (IS_INTERLACED(mb_type) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
  6333. // and left mb is in the same slice if deblocking_filter == 2
  6334. && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
  6335. /* First vertical edge is different in MBAFF frames
  6336. * There are 8 different bS to compute and 2 different Qp
  6337. */
  6338. const int pair_xy = mb_x + (mb_y&~1)*s->mb_stride;
  6339. const int left_mb_xy[2] = { pair_xy-1, pair_xy-1+s->mb_stride };
  6340. int16_t bS[8];
  6341. int qp[2];
  6342. int chroma_qp[2];
  6343. int mb_qp, mbn0_qp, mbn1_qp;
  6344. int i;
  6345. first_vertical_edge_done = 1;
  6346. if( IS_INTRA(mb_type) )
  6347. bS[0] = bS[1] = bS[2] = bS[3] = bS[4] = bS[5] = bS[6] = bS[7] = 4;
  6348. else {
  6349. for( i = 0; i < 8; i++ ) {
  6350. int mbn_xy = MB_FIELD ? left_mb_xy[i>>2] : left_mb_xy[i&1];
  6351. if( IS_INTRA( s->current_picture.mb_type[mbn_xy] ) )
  6352. bS[i] = 4;
  6353. else if( h->non_zero_count_cache[12+8*(i>>1)] != 0 ||
  6354. /* FIXME: with 8x8dct + cavlc, should check cbp instead of nnz */
  6355. h->non_zero_count[mbn_xy][MB_FIELD ? i&3 : (i>>2)+(mb_y&1)*2] )
  6356. bS[i] = 2;
  6357. else
  6358. bS[i] = 1;
  6359. }
  6360. }
  6361. mb_qp = s->current_picture.qscale_table[mb_xy];
  6362. mbn0_qp = s->current_picture.qscale_table[left_mb_xy[0]];
  6363. mbn1_qp = s->current_picture.qscale_table[left_mb_xy[1]];
  6364. qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1;
  6365. chroma_qp[0] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6366. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn0_qp ) + 1 ) >> 1;
  6367. qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1;
  6368. chroma_qp[1] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6369. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn1_qp ) + 1 ) >> 1;
  6370. /* Filter edge */
  6371. tprintf(s->avctx, "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);
  6372. { int i; for (i = 0; i < 8; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
  6373. filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp );
  6374. filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, chroma_qp );
  6375. filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, chroma_qp );
  6376. }
  6377. /* dir : 0 -> vertical edge, 1 -> horizontal edge */
  6378. for( dir = 0; dir < 2; dir++ )
  6379. {
  6380. int edge;
  6381. const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
  6382. const int mbm_type = s->current_picture.mb_type[mbm_xy];
  6383. int start = h->slice_table[mbm_xy] == 255 ? 1 : 0;
  6384. const int edges = (mb_type & (MB_TYPE_16x16|MB_TYPE_SKIP))
  6385. == (MB_TYPE_16x16|MB_TYPE_SKIP) ? 1 : 4;
  6386. // how often to recheck mv-based bS when iterating between edges
  6387. const int mask_edge = (mb_type & (MB_TYPE_16x16 | (MB_TYPE_16x8 << dir))) ? 3 :
  6388. (mb_type & (MB_TYPE_8x16 >> dir)) ? 1 : 0;
  6389. // how often to recheck mv-based bS when iterating along each edge
  6390. const int mask_par0 = mb_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir));
  6391. if (first_vertical_edge_done) {
  6392. start = 1;
  6393. first_vertical_edge_done = 0;
  6394. }
  6395. if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
  6396. start = 1;
  6397. if (FRAME_MBAFF && (dir == 1) && ((mb_y&1) == 0) && start == 0
  6398. && !IS_INTERLACED(mb_type)
  6399. && IS_INTERLACED(mbm_type)
  6400. ) {
  6401. // This is a special case in the norm where the filtering must
  6402. // be done twice (one each of the field) even if we are in a
  6403. // frame macroblock.
  6404. //
  6405. static const int nnz_idx[4] = {4,5,6,3};
  6406. unsigned int tmp_linesize = 2 * linesize;
  6407. unsigned int tmp_uvlinesize = 2 * uvlinesize;
  6408. int mbn_xy = mb_xy - 2 * s->mb_stride;
  6409. int qp, chroma_qp;
  6410. int i, j;
  6411. int16_t bS[4];
  6412. for(j=0; j<2; j++, mbn_xy += s->mb_stride){
  6413. if( IS_INTRA(mb_type) ||
  6414. IS_INTRA(s->current_picture.mb_type[mbn_xy]) ) {
  6415. bS[0] = bS[1] = bS[2] = bS[3] = 3;
  6416. } else {
  6417. const uint8_t *mbn_nnz = h->non_zero_count[mbn_xy];
  6418. for( i = 0; i < 4; i++ ) {
  6419. if( h->non_zero_count_cache[scan8[0]+i] != 0 ||
  6420. mbn_nnz[nnz_idx[i]] != 0 )
  6421. bS[i] = 2;
  6422. else
  6423. bS[i] = 1;
  6424. }
  6425. }
  6426. // Do not use s->qscale as luma quantizer because it has not the same
  6427. // value in IPCM macroblocks.
  6428. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6429. tprintf(s->avctx, "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);
  6430. { int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
  6431. filter_mb_edgeh( h, &img_y[j*linesize], tmp_linesize, bS, qp );
  6432. chroma_qp = ( h->chroma_qp +
  6433. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6434. filter_mb_edgech( h, &img_cb[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6435. filter_mb_edgech( h, &img_cr[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6436. }
  6437. start = 1;
  6438. }
  6439. /* Calculate bS */
  6440. for( edge = start; edge < edges; edge++ ) {
  6441. /* mbn_xy: neighbor macroblock */
  6442. const int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
  6443. const int mbn_type = s->current_picture.mb_type[mbn_xy];
  6444. int16_t bS[4];
  6445. int qp;
  6446. if( (edge&1) && IS_8x8DCT(mb_type) )
  6447. continue;
  6448. if( IS_INTRA(mb_type) ||
  6449. IS_INTRA(mbn_type) ) {
  6450. int value;
  6451. if (edge == 0) {
  6452. if ( (!IS_INTERLACED(mb_type) && !IS_INTERLACED(mbm_type))
  6453. || ((FRAME_MBAFF || (s->picture_structure != PICT_FRAME)) && (dir == 0))
  6454. ) {
  6455. value = 4;
  6456. } else {
  6457. value = 3;
  6458. }
  6459. } else {
  6460. value = 3;
  6461. }
  6462. bS[0] = bS[1] = bS[2] = bS[3] = value;
  6463. } else {
  6464. int i, l;
  6465. int mv_done;
  6466. if( edge & mask_edge ) {
  6467. bS[0] = bS[1] = bS[2] = bS[3] = 0;
  6468. mv_done = 1;
  6469. }
  6470. else if( FRAME_MBAFF && IS_INTERLACED(mb_type ^ mbn_type)) {
  6471. bS[0] = bS[1] = bS[2] = bS[3] = 1;
  6472. mv_done = 1;
  6473. }
  6474. else if( mask_par0 && (edge || (mbn_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir)))) ) {
  6475. int b_idx= 8 + 4 + edge * (dir ? 8:1);
  6476. int bn_idx= b_idx - (dir ? 8:1);
  6477. int v = 0;
  6478. for( l = 0; !v && l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6479. v |= ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6480. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6481. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit;
  6482. }
  6483. bS[0] = bS[1] = bS[2] = bS[3] = v;
  6484. mv_done = 1;
  6485. }
  6486. else
  6487. mv_done = 0;
  6488. for( i = 0; i < 4; i++ ) {
  6489. int x = dir == 0 ? edge : i;
  6490. int y = dir == 0 ? i : edge;
  6491. int b_idx= 8 + 4 + x + 8*y;
  6492. int bn_idx= b_idx - (dir ? 8:1);
  6493. if( h->non_zero_count_cache[b_idx] != 0 ||
  6494. h->non_zero_count_cache[bn_idx] != 0 ) {
  6495. bS[i] = 2;
  6496. }
  6497. else if(!mv_done)
  6498. {
  6499. bS[i] = 0;
  6500. for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6501. if( ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6502. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6503. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit ) {
  6504. bS[i] = 1;
  6505. break;
  6506. }
  6507. }
  6508. }
  6509. }
  6510. if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
  6511. continue;
  6512. }
  6513. /* Filter edge */
  6514. // Do not use s->qscale as luma quantizer because it has not the same
  6515. // value in IPCM macroblocks.
  6516. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6517. //tprintf(s->avctx, "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]);
  6518. tprintf(s->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
  6519. { int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
  6520. if( dir == 0 ) {
  6521. filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
  6522. if( (edge&1) == 0 ) {
  6523. int chroma_qp = ( h->chroma_qp +
  6524. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6525. filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp );
  6526. filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp );
  6527. }
  6528. } else {
  6529. filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
  6530. if( (edge&1) == 0 ) {
  6531. int chroma_qp = ( h->chroma_qp +
  6532. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6533. filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6534. filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6535. }
  6536. }
  6537. }
  6538. }
  6539. }
  6540. static int decode_slice(H264Context *h){
  6541. MpegEncContext * const s = &h->s;
  6542. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  6543. s->mb_skip_run= -1;
  6544. if( h->pps.cabac ) {
  6545. int i;
  6546. /* realign */
  6547. align_get_bits( &s->gb );
  6548. /* init cabac */
  6549. ff_init_cabac_states( &h->cabac);
  6550. ff_init_cabac_decoder( &h->cabac,
  6551. s->gb.buffer + get_bits_count(&s->gb)/8,
  6552. ( s->gb.size_in_bits - get_bits_count(&s->gb) + 7)/8);
  6553. /* calculate pre-state */
  6554. for( i= 0; i < 460; i++ ) {
  6555. int pre;
  6556. if( h->slice_type == I_TYPE )
  6557. pre = av_clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
  6558. else
  6559. pre = av_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 );
  6560. if( pre <= 63 )
  6561. h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
  6562. else
  6563. h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
  6564. }
  6565. for(;;){
  6566. //START_TIMER
  6567. int ret = decode_mb_cabac(h);
  6568. int eos;
  6569. //STOP_TIMER("decode_mb_cabac")
  6570. if(ret>=0) hl_decode_mb(h);
  6571. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  6572. s->mb_y++;
  6573. if(ret>=0) ret = decode_mb_cabac(h);
  6574. if(ret>=0) hl_decode_mb(h);
  6575. s->mb_y--;
  6576. }
  6577. eos = get_cabac_terminate( &h->cabac );
  6578. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  6579. 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);
  6580. 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);
  6581. return -1;
  6582. }
  6583. if( ++s->mb_x >= s->mb_width ) {
  6584. s->mb_x = 0;
  6585. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6586. ++s->mb_y;
  6587. if(FRAME_MBAFF) {
  6588. ++s->mb_y;
  6589. }
  6590. }
  6591. if( eos || s->mb_y >= s->mb_height ) {
  6592. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6593. 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);
  6594. return 0;
  6595. }
  6596. }
  6597. } else {
  6598. for(;;){
  6599. int ret = decode_mb_cavlc(h);
  6600. if(ret>=0) hl_decode_mb(h);
  6601. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  6602. s->mb_y++;
  6603. ret = decode_mb_cavlc(h);
  6604. if(ret>=0) hl_decode_mb(h);
  6605. s->mb_y--;
  6606. }
  6607. if(ret<0){
  6608. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6609. 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);
  6610. return -1;
  6611. }
  6612. if(++s->mb_x >= s->mb_width){
  6613. s->mb_x=0;
  6614. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6615. ++s->mb_y;
  6616. if(FRAME_MBAFF) {
  6617. ++s->mb_y;
  6618. }
  6619. if(s->mb_y >= s->mb_height){
  6620. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6621. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  6622. 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);
  6623. return 0;
  6624. }else{
  6625. 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);
  6626. return -1;
  6627. }
  6628. }
  6629. }
  6630. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  6631. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6632. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  6633. 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);
  6634. return 0;
  6635. }else{
  6636. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
  6637. return -1;
  6638. }
  6639. }
  6640. }
  6641. }
  6642. #if 0
  6643. for(;s->mb_y < s->mb_height; s->mb_y++){
  6644. for(;s->mb_x < s->mb_width; s->mb_x++){
  6645. int ret= decode_mb(h);
  6646. hl_decode_mb(h);
  6647. if(ret<0){
  6648. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6649. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
  6650. return -1;
  6651. }
  6652. if(++s->mb_x >= s->mb_width){
  6653. s->mb_x=0;
  6654. if(++s->mb_y >= s->mb_height){
  6655. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6656. 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);
  6657. return 0;
  6658. }else{
  6659. 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);
  6660. return -1;
  6661. }
  6662. }
  6663. }
  6664. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  6665. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6666. 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);
  6667. return 0;
  6668. }else{
  6669. 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);
  6670. return -1;
  6671. }
  6672. }
  6673. }
  6674. s->mb_x=0;
  6675. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6676. }
  6677. #endif
  6678. return -1; //not reached
  6679. }
  6680. static int decode_unregistered_user_data(H264Context *h, int size){
  6681. MpegEncContext * const s = &h->s;
  6682. uint8_t user_data[16+256];
  6683. int e, build, i;
  6684. if(size<16)
  6685. return -1;
  6686. for(i=0; i<sizeof(user_data)-1 && i<size; i++){
  6687. user_data[i]= get_bits(&s->gb, 8);
  6688. }
  6689. user_data[i]= 0;
  6690. e= sscanf(user_data+16, "x264 - core %d"/*%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html*/, &build);
  6691. if(e==1 && build>=0)
  6692. h->x264_build= build;
  6693. if(s->avctx->debug & FF_DEBUG_BUGS)
  6694. av_log(s->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data+16);
  6695. for(; i<size; i++)
  6696. skip_bits(&s->gb, 8);
  6697. return 0;
  6698. }
  6699. static int decode_sei(H264Context *h){
  6700. MpegEncContext * const s = &h->s;
  6701. while(get_bits_count(&s->gb) + 16 < s->gb.size_in_bits){
  6702. int size, type;
  6703. type=0;
  6704. do{
  6705. type+= show_bits(&s->gb, 8);
  6706. }while(get_bits(&s->gb, 8) == 255);
  6707. size=0;
  6708. do{
  6709. size+= show_bits(&s->gb, 8);
  6710. }while(get_bits(&s->gb, 8) == 255);
  6711. switch(type){
  6712. case 5:
  6713. if(decode_unregistered_user_data(h, size) < 0)
  6714. return -1;
  6715. break;
  6716. default:
  6717. skip_bits(&s->gb, 8*size);
  6718. }
  6719. //FIXME check bits here
  6720. align_get_bits(&s->gb);
  6721. }
  6722. return 0;
  6723. }
  6724. static inline void decode_hrd_parameters(H264Context *h, SPS *sps){
  6725. MpegEncContext * const s = &h->s;
  6726. int cpb_count, i;
  6727. cpb_count = get_ue_golomb(&s->gb) + 1;
  6728. get_bits(&s->gb, 4); /* bit_rate_scale */
  6729. get_bits(&s->gb, 4); /* cpb_size_scale */
  6730. for(i=0; i<cpb_count; i++){
  6731. get_ue_golomb(&s->gb); /* bit_rate_value_minus1 */
  6732. get_ue_golomb(&s->gb); /* cpb_size_value_minus1 */
  6733. get_bits1(&s->gb); /* cbr_flag */
  6734. }
  6735. get_bits(&s->gb, 5); /* initial_cpb_removal_delay_length_minus1 */
  6736. get_bits(&s->gb, 5); /* cpb_removal_delay_length_minus1 */
  6737. get_bits(&s->gb, 5); /* dpb_output_delay_length_minus1 */
  6738. get_bits(&s->gb, 5); /* time_offset_length */
  6739. }
  6740. static inline int decode_vui_parameters(H264Context *h, SPS *sps){
  6741. MpegEncContext * const s = &h->s;
  6742. int aspect_ratio_info_present_flag;
  6743. unsigned int aspect_ratio_idc;
  6744. int nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag;
  6745. aspect_ratio_info_present_flag= get_bits1(&s->gb);
  6746. if( aspect_ratio_info_present_flag ) {
  6747. aspect_ratio_idc= get_bits(&s->gb, 8);
  6748. if( aspect_ratio_idc == EXTENDED_SAR ) {
  6749. sps->sar.num= get_bits(&s->gb, 16);
  6750. sps->sar.den= get_bits(&s->gb, 16);
  6751. }else if(aspect_ratio_idc < 14){
  6752. sps->sar= pixel_aspect[aspect_ratio_idc];
  6753. }else{
  6754. av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
  6755. return -1;
  6756. }
  6757. }else{
  6758. sps->sar.num=
  6759. sps->sar.den= 0;
  6760. }
  6761. // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
  6762. if(get_bits1(&s->gb)){ /* overscan_info_present_flag */
  6763. get_bits1(&s->gb); /* overscan_appropriate_flag */
  6764. }
  6765. if(get_bits1(&s->gb)){ /* video_signal_type_present_flag */
  6766. get_bits(&s->gb, 3); /* video_format */
  6767. get_bits1(&s->gb); /* video_full_range_flag */
  6768. if(get_bits1(&s->gb)){ /* colour_description_present_flag */
  6769. get_bits(&s->gb, 8); /* colour_primaries */
  6770. get_bits(&s->gb, 8); /* transfer_characteristics */
  6771. get_bits(&s->gb, 8); /* matrix_coefficients */
  6772. }
  6773. }
  6774. if(get_bits1(&s->gb)){ /* chroma_location_info_present_flag */
  6775. get_ue_golomb(&s->gb); /* chroma_sample_location_type_top_field */
  6776. get_ue_golomb(&s->gb); /* chroma_sample_location_type_bottom_field */
  6777. }
  6778. sps->timing_info_present_flag = get_bits1(&s->gb);
  6779. if(sps->timing_info_present_flag){
  6780. sps->num_units_in_tick = get_bits_long(&s->gb, 32);
  6781. sps->time_scale = get_bits_long(&s->gb, 32);
  6782. sps->fixed_frame_rate_flag = get_bits1(&s->gb);
  6783. }
  6784. nal_hrd_parameters_present_flag = get_bits1(&s->gb);
  6785. if(nal_hrd_parameters_present_flag)
  6786. decode_hrd_parameters(h, sps);
  6787. vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
  6788. if(vcl_hrd_parameters_present_flag)
  6789. decode_hrd_parameters(h, sps);
  6790. if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag)
  6791. get_bits1(&s->gb); /* low_delay_hrd_flag */
  6792. get_bits1(&s->gb); /* pic_struct_present_flag */
  6793. sps->bitstream_restriction_flag = get_bits1(&s->gb);
  6794. if(sps->bitstream_restriction_flag){
  6795. unsigned int num_reorder_frames;
  6796. get_bits1(&s->gb); /* motion_vectors_over_pic_boundaries_flag */
  6797. get_ue_golomb(&s->gb); /* max_bytes_per_pic_denom */
  6798. get_ue_golomb(&s->gb); /* max_bits_per_mb_denom */
  6799. get_ue_golomb(&s->gb); /* log2_max_mv_length_horizontal */
  6800. get_ue_golomb(&s->gb); /* log2_max_mv_length_vertical */
  6801. num_reorder_frames= get_ue_golomb(&s->gb);
  6802. get_ue_golomb(&s->gb); /*max_dec_frame_buffering*/
  6803. if(num_reorder_frames > 16 /*max_dec_frame_buffering || max_dec_frame_buffering > 16*/){
  6804. av_log(h->s.avctx, AV_LOG_ERROR, "illegal num_reorder_frames %d\n", num_reorder_frames);
  6805. return -1;
  6806. }
  6807. sps->num_reorder_frames= num_reorder_frames;
  6808. }
  6809. return 0;
  6810. }
  6811. static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,
  6812. const uint8_t *jvt_list, const uint8_t *fallback_list){
  6813. MpegEncContext * const s = &h->s;
  6814. int i, last = 8, next = 8;
  6815. const uint8_t *scan = size == 16 ? zigzag_scan : zigzag_scan8x8;
  6816. if(!get_bits1(&s->gb)) /* matrix not written, we use the predicted one */
  6817. memcpy(factors, fallback_list, size*sizeof(uint8_t));
  6818. else
  6819. for(i=0;i<size;i++){
  6820. if(next)
  6821. next = (last + get_se_golomb(&s->gb)) & 0xff;
  6822. if(!i && !next){ /* matrix not written, we use the preset one */
  6823. memcpy(factors, jvt_list, size*sizeof(uint8_t));
  6824. break;
  6825. }
  6826. last = factors[scan[i]] = next ? next : last;
  6827. }
  6828. }
  6829. static void decode_scaling_matrices(H264Context *h, SPS *sps, PPS *pps, int is_sps,
  6830. uint8_t (*scaling_matrix4)[16], uint8_t (*scaling_matrix8)[64]){
  6831. MpegEncContext * const s = &h->s;
  6832. int fallback_sps = !is_sps && sps->scaling_matrix_present;
  6833. const uint8_t *fallback[4] = {
  6834. fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
  6835. fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
  6836. fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
  6837. fallback_sps ? sps->scaling_matrix8[1] : default_scaling8[1]
  6838. };
  6839. if(get_bits1(&s->gb)){
  6840. sps->scaling_matrix_present |= is_sps;
  6841. decode_scaling_list(h,scaling_matrix4[0],16,default_scaling4[0],fallback[0]); // Intra, Y
  6842. decode_scaling_list(h,scaling_matrix4[1],16,default_scaling4[0],scaling_matrix4[0]); // Intra, Cr
  6843. decode_scaling_list(h,scaling_matrix4[2],16,default_scaling4[0],scaling_matrix4[1]); // Intra, Cb
  6844. decode_scaling_list(h,scaling_matrix4[3],16,default_scaling4[1],fallback[1]); // Inter, Y
  6845. decode_scaling_list(h,scaling_matrix4[4],16,default_scaling4[1],scaling_matrix4[3]); // Inter, Cr
  6846. decode_scaling_list(h,scaling_matrix4[5],16,default_scaling4[1],scaling_matrix4[4]); // Inter, Cb
  6847. if(is_sps || pps->transform_8x8_mode){
  6848. decode_scaling_list(h,scaling_matrix8[0],64,default_scaling8[0],fallback[2]); // Intra, Y
  6849. decode_scaling_list(h,scaling_matrix8[1],64,default_scaling8[1],fallback[3]); // Inter, Y
  6850. }
  6851. } else if(fallback_sps) {
  6852. memcpy(scaling_matrix4, sps->scaling_matrix4, 6*16*sizeof(uint8_t));
  6853. memcpy(scaling_matrix8, sps->scaling_matrix8, 2*64*sizeof(uint8_t));
  6854. }
  6855. }
  6856. static inline int decode_seq_parameter_set(H264Context *h){
  6857. MpegEncContext * const s = &h->s;
  6858. int profile_idc, level_idc;
  6859. unsigned int sps_id, tmp, mb_width, mb_height;
  6860. int i;
  6861. SPS *sps;
  6862. profile_idc= get_bits(&s->gb, 8);
  6863. get_bits1(&s->gb); //constraint_set0_flag
  6864. get_bits1(&s->gb); //constraint_set1_flag
  6865. get_bits1(&s->gb); //constraint_set2_flag
  6866. get_bits1(&s->gb); //constraint_set3_flag
  6867. get_bits(&s->gb, 4); // reserved
  6868. level_idc= get_bits(&s->gb, 8);
  6869. sps_id= get_ue_golomb(&s->gb);
  6870. if (sps_id >= MAX_SPS_COUNT){
  6871. // ok it has gone out of hand, someone is sending us bad stuff.
  6872. av_log(h->s.avctx, AV_LOG_ERROR, "illegal sps_id (%d)\n", sps_id);
  6873. return -1;
  6874. }
  6875. sps= &h->sps_buffer[ sps_id ];
  6876. sps->profile_idc= profile_idc;
  6877. sps->level_idc= level_idc;
  6878. if(sps->profile_idc >= 100){ //high profile
  6879. if(get_ue_golomb(&s->gb) == 3) //chroma_format_idc
  6880. get_bits1(&s->gb); //residual_color_transform_flag
  6881. get_ue_golomb(&s->gb); //bit_depth_luma_minus8
  6882. get_ue_golomb(&s->gb); //bit_depth_chroma_minus8
  6883. sps->transform_bypass = get_bits1(&s->gb);
  6884. decode_scaling_matrices(h, sps, NULL, 1, sps->scaling_matrix4, sps->scaling_matrix8);
  6885. }else
  6886. sps->scaling_matrix_present = 0;
  6887. sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
  6888. sps->poc_type= get_ue_golomb(&s->gb);
  6889. if(sps->poc_type == 0){ //FIXME #define
  6890. sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
  6891. } else if(sps->poc_type == 1){//FIXME #define
  6892. sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
  6893. sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
  6894. sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
  6895. tmp= get_ue_golomb(&s->gb);
  6896. if(tmp >= sizeof(sps->offset_for_ref_frame) / sizeof(sps->offset_for_ref_frame[0])){
  6897. av_log(h->s.avctx, AV_LOG_ERROR, "poc_cycle_length overflow %u\n", tmp);
  6898. return -1;
  6899. }
  6900. sps->poc_cycle_length= tmp;
  6901. for(i=0; i<sps->poc_cycle_length; i++)
  6902. sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
  6903. }else if(sps->poc_type != 2){
  6904. av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
  6905. return -1;
  6906. }
  6907. tmp= get_ue_golomb(&s->gb);
  6908. if(tmp > MAX_PICTURE_COUNT-2){
  6909. av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");
  6910. }
  6911. sps->ref_frame_count= tmp;
  6912. sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
  6913. mb_width= get_ue_golomb(&s->gb) + 1;
  6914. mb_height= get_ue_golomb(&s->gb) + 1;
  6915. if(mb_width >= INT_MAX/16 || mb_height >= INT_MAX/16 ||
  6916. avcodec_check_dimensions(NULL, 16*mb_width, 16*mb_height)){
  6917. av_log(h->s.avctx, AV_LOG_ERROR, "mb_width/height overflow\n");
  6918. return -1;
  6919. }
  6920. sps->mb_width = mb_width;
  6921. sps->mb_height= mb_height;
  6922. sps->frame_mbs_only_flag= get_bits1(&s->gb);
  6923. if(!sps->frame_mbs_only_flag)
  6924. sps->mb_aff= get_bits1(&s->gb);
  6925. else
  6926. sps->mb_aff= 0;
  6927. sps->direct_8x8_inference_flag= get_bits1(&s->gb);
  6928. #ifndef ALLOW_INTERLACE
  6929. if(sps->mb_aff)
  6930. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF support not included; enable it at compile-time.\n");
  6931. #endif
  6932. if(!sps->direct_8x8_inference_flag && sps->mb_aff)
  6933. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + !direct_8x8_inference is not implemented\n");
  6934. sps->crop= get_bits1(&s->gb);
  6935. if(sps->crop){
  6936. sps->crop_left = get_ue_golomb(&s->gb);
  6937. sps->crop_right = get_ue_golomb(&s->gb);
  6938. sps->crop_top = get_ue_golomb(&s->gb);
  6939. sps->crop_bottom= get_ue_golomb(&s->gb);
  6940. if(sps->crop_left || sps->crop_top){
  6941. av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ...\n");
  6942. }
  6943. }else{
  6944. sps->crop_left =
  6945. sps->crop_right =
  6946. sps->crop_top =
  6947. sps->crop_bottom= 0;
  6948. }
  6949. sps->vui_parameters_present_flag= get_bits1(&s->gb);
  6950. if( sps->vui_parameters_present_flag )
  6951. decode_vui_parameters(h, sps);
  6952. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6953. 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",
  6954. sps_id, sps->profile_idc, sps->level_idc,
  6955. sps->poc_type,
  6956. sps->ref_frame_count,
  6957. sps->mb_width, sps->mb_height,
  6958. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  6959. sps->direct_8x8_inference_flag ? "8B8" : "",
  6960. sps->crop_left, sps->crop_right,
  6961. sps->crop_top, sps->crop_bottom,
  6962. sps->vui_parameters_present_flag ? "VUI" : ""
  6963. );
  6964. }
  6965. return 0;
  6966. }
  6967. static inline int decode_picture_parameter_set(H264Context *h, int bit_length){
  6968. MpegEncContext * const s = &h->s;
  6969. unsigned int tmp, pps_id= get_ue_golomb(&s->gb);
  6970. PPS *pps;
  6971. if(pps_id>=MAX_PPS_COUNT){
  6972. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  6973. return -1;
  6974. }
  6975. pps = &h->pps_buffer[pps_id];
  6976. tmp= get_ue_golomb(&s->gb);
  6977. if(tmp>=MAX_SPS_COUNT){
  6978. av_log(h->s.avctx, AV_LOG_ERROR, "sps_id out of range\n");
  6979. return -1;
  6980. }
  6981. pps->sps_id= tmp;
  6982. pps->cabac= get_bits1(&s->gb);
  6983. pps->pic_order_present= get_bits1(&s->gb);
  6984. pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
  6985. if(pps->slice_group_count > 1 ){
  6986. pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
  6987. av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
  6988. switch(pps->mb_slice_group_map_type){
  6989. case 0:
  6990. #if 0
  6991. | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
  6992. | run_length[ i ] |1 |ue(v) |
  6993. #endif
  6994. break;
  6995. case 2:
  6996. #if 0
  6997. | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
  6998. |{ | | |
  6999. | top_left_mb[ i ] |1 |ue(v) |
  7000. | bottom_right_mb[ i ] |1 |ue(v) |
  7001. | } | | |
  7002. #endif
  7003. break;
  7004. case 3:
  7005. case 4:
  7006. case 5:
  7007. #if 0
  7008. | slice_group_change_direction_flag |1 |u(1) |
  7009. | slice_group_change_rate_minus1 |1 |ue(v) |
  7010. #endif
  7011. break;
  7012. case 6:
  7013. #if 0
  7014. | slice_group_id_cnt_minus1 |1 |ue(v) |
  7015. | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
  7016. |) | | |
  7017. | slice_group_id[ i ] |1 |u(v) |
  7018. #endif
  7019. break;
  7020. }
  7021. }
  7022. pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  7023. pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  7024. if(pps->ref_count[0]-1 > 32-1 || pps->ref_count[1]-1 > 32-1){
  7025. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
  7026. pps->ref_count[0]= pps->ref_count[1]= 1;
  7027. return -1;
  7028. }
  7029. pps->weighted_pred= get_bits1(&s->gb);
  7030. pps->weighted_bipred_idc= get_bits(&s->gb, 2);
  7031. pps->init_qp= get_se_golomb(&s->gb) + 26;
  7032. pps->init_qs= get_se_golomb(&s->gb) + 26;
  7033. pps->chroma_qp_index_offset= get_se_golomb(&s->gb);
  7034. pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
  7035. pps->constrained_intra_pred= get_bits1(&s->gb);
  7036. pps->redundant_pic_cnt_present = get_bits1(&s->gb);
  7037. pps->transform_8x8_mode= 0;
  7038. h->dequant_coeff_pps= -1; //contents of sps/pps can change even if id doesn't, so reinit
  7039. memset(pps->scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  7040. memset(pps->scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  7041. if(get_bits_count(&s->gb) < bit_length){
  7042. pps->transform_8x8_mode= get_bits1(&s->gb);
  7043. decode_scaling_matrices(h, &h->sps_buffer[pps->sps_id], pps, 0, pps->scaling_matrix4, pps->scaling_matrix8);
  7044. get_se_golomb(&s->gb); //second_chroma_qp_index_offset
  7045. }
  7046. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  7047. 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",
  7048. pps_id, pps->sps_id,
  7049. pps->cabac ? "CABAC" : "CAVLC",
  7050. pps->slice_group_count,
  7051. pps->ref_count[0], pps->ref_count[1],
  7052. pps->weighted_pred ? "weighted" : "",
  7053. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset,
  7054. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  7055. pps->constrained_intra_pred ? "CONSTR" : "",
  7056. pps->redundant_pic_cnt_present ? "REDU" : "",
  7057. pps->transform_8x8_mode ? "8x8DCT" : ""
  7058. );
  7059. }
  7060. return 0;
  7061. }
  7062. /**
  7063. * finds the end of the current frame in the bitstream.
  7064. * @return the position of the first byte of the next frame, or -1
  7065. */
  7066. static int find_frame_end(H264Context *h, const uint8_t *buf, int buf_size){
  7067. int i;
  7068. uint32_t state;
  7069. ParseContext *pc = &(h->s.parse_context);
  7070. //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
  7071. // mb_addr= pc->mb_addr - 1;
  7072. state= pc->state;
  7073. if(state>13)
  7074. state= 7;
  7075. for(i=0; i<buf_size; i++){
  7076. if(state==7){
  7077. for(; i<buf_size; i++){
  7078. if(!buf[i]){
  7079. state=2;
  7080. break;
  7081. }
  7082. }
  7083. }else if(state<=2){
  7084. if(buf[i]==1) state^= 5; //2->7, 1->4, 0->5
  7085. else if(buf[i]) state = 7;
  7086. else state>>=1; //2->1, 1->0, 0->0
  7087. }else if(state<=5){
  7088. int v= buf[i] & 0x1F;
  7089. if(v==7 || v==8 || v==9){
  7090. if(pc->frame_start_found){
  7091. i++;
  7092. found:
  7093. pc->state=7;
  7094. pc->frame_start_found= 0;
  7095. return i-(state&5);
  7096. }
  7097. }else if(v==1 || v==2 || v==5){
  7098. if(pc->frame_start_found){
  7099. state+=8;
  7100. continue;
  7101. }else
  7102. pc->frame_start_found = 1;
  7103. }
  7104. state= 7;
  7105. }else{
  7106. if(buf[i] & 0x80)
  7107. goto found;
  7108. state= 7;
  7109. }
  7110. }
  7111. pc->state= state;
  7112. return END_NOT_FOUND;
  7113. }
  7114. #ifdef CONFIG_H264_PARSER
  7115. static int h264_parse(AVCodecParserContext *s,
  7116. AVCodecContext *avctx,
  7117. uint8_t **poutbuf, int *poutbuf_size,
  7118. const uint8_t *buf, int buf_size)
  7119. {
  7120. H264Context *h = s->priv_data;
  7121. ParseContext *pc = &h->s.parse_context;
  7122. int next;
  7123. next= find_frame_end(h, buf, buf_size);
  7124. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  7125. *poutbuf = NULL;
  7126. *poutbuf_size = 0;
  7127. return buf_size;
  7128. }
  7129. if(next<0){
  7130. find_frame_end(h, &pc->buffer[pc->last_index + next], -next); //update state
  7131. }
  7132. *poutbuf = (uint8_t *)buf;
  7133. *poutbuf_size = buf_size;
  7134. return next;
  7135. }
  7136. static int h264_split(AVCodecContext *avctx,
  7137. const uint8_t *buf, int buf_size)
  7138. {
  7139. int i;
  7140. uint32_t state = -1;
  7141. int has_sps= 0;
  7142. for(i=0; i<=buf_size; i++){
  7143. if((state&0xFFFFFF1F) == 0x107)
  7144. has_sps=1;
  7145. /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  7146. }*/
  7147. if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
  7148. if(has_sps){
  7149. while(i>4 && buf[i-5]==0) i--;
  7150. return i-4;
  7151. }
  7152. }
  7153. if (i<buf_size)
  7154. state= (state<<8) | buf[i];
  7155. }
  7156. return 0;
  7157. }
  7158. #endif /* CONFIG_H264_PARSER */
  7159. static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){
  7160. MpegEncContext * const s = &h->s;
  7161. AVCodecContext * const avctx= s->avctx;
  7162. int buf_index=0;
  7163. #if 0
  7164. int i;
  7165. for(i=0; i<50; i++){
  7166. av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
  7167. }
  7168. #endif
  7169. h->slice_num = 0;
  7170. s->current_picture_ptr= NULL;
  7171. for(;;){
  7172. int consumed;
  7173. int dst_length;
  7174. int bit_length;
  7175. uint8_t *ptr;
  7176. int i, nalsize = 0;
  7177. if(h->is_avc) {
  7178. if(buf_index >= buf_size) break;
  7179. nalsize = 0;
  7180. for(i = 0; i < h->nal_length_size; i++)
  7181. nalsize = (nalsize << 8) | buf[buf_index++];
  7182. if(nalsize <= 1 || nalsize > buf_size){
  7183. if(nalsize == 1){
  7184. buf_index++;
  7185. continue;
  7186. }else{
  7187. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  7188. break;
  7189. }
  7190. }
  7191. } else {
  7192. // start code prefix search
  7193. for(; buf_index + 3 < buf_size; buf_index++){
  7194. // this should allways succeed in the first iteration
  7195. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  7196. break;
  7197. }
  7198. if(buf_index+3 >= buf_size) break;
  7199. buf_index+=3;
  7200. }
  7201. ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, h->is_avc ? nalsize : buf_size - buf_index);
  7202. if (ptr==NULL || dst_length <= 0){
  7203. return -1;
  7204. }
  7205. while(ptr[dst_length - 1] == 0 && dst_length > 1)
  7206. dst_length--;
  7207. bit_length= 8*dst_length - decode_rbsp_trailing(h, ptr + dst_length - 1);
  7208. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  7209. 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);
  7210. }
  7211. if (h->is_avc && (nalsize != consumed))
  7212. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  7213. buf_index += consumed;
  7214. if( (s->hurry_up == 1 && h->nal_ref_idc == 0) //FIXME dont discard SEI id
  7215. ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  7216. continue;
  7217. switch(h->nal_unit_type){
  7218. case NAL_IDR_SLICE:
  7219. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  7220. case NAL_SLICE:
  7221. init_get_bits(&s->gb, ptr, bit_length);
  7222. h->intra_gb_ptr=
  7223. h->inter_gb_ptr= &s->gb;
  7224. s->data_partitioning = 0;
  7225. if(decode_slice_header(h) < 0){
  7226. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  7227. break;
  7228. }
  7229. s->current_picture_ptr->key_frame= (h->nal_unit_type == NAL_IDR_SLICE);
  7230. if(h->redundant_pic_count==0 && s->hurry_up < 5
  7231. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  7232. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  7233. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  7234. && avctx->skip_frame < AVDISCARD_ALL)
  7235. decode_slice(h);
  7236. break;
  7237. case NAL_DPA:
  7238. init_get_bits(&s->gb, ptr, bit_length);
  7239. h->intra_gb_ptr=
  7240. h->inter_gb_ptr= NULL;
  7241. s->data_partitioning = 1;
  7242. if(decode_slice_header(h) < 0){
  7243. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  7244. }
  7245. break;
  7246. case NAL_DPB:
  7247. init_get_bits(&h->intra_gb, ptr, bit_length);
  7248. h->intra_gb_ptr= &h->intra_gb;
  7249. break;
  7250. case NAL_DPC:
  7251. init_get_bits(&h->inter_gb, ptr, bit_length);
  7252. h->inter_gb_ptr= &h->inter_gb;
  7253. if(h->redundant_pic_count==0 && h->intra_gb_ptr && s->data_partitioning
  7254. && s->context_initialized
  7255. && s->hurry_up < 5
  7256. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  7257. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  7258. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  7259. && avctx->skip_frame < AVDISCARD_ALL)
  7260. decode_slice(h);
  7261. break;
  7262. case NAL_SEI:
  7263. init_get_bits(&s->gb, ptr, bit_length);
  7264. decode_sei(h);
  7265. break;
  7266. case NAL_SPS:
  7267. init_get_bits(&s->gb, ptr, bit_length);
  7268. decode_seq_parameter_set(h);
  7269. if(s->flags& CODEC_FLAG_LOW_DELAY)
  7270. s->low_delay=1;
  7271. if(avctx->has_b_frames < 2)
  7272. avctx->has_b_frames= !s->low_delay;
  7273. break;
  7274. case NAL_PPS:
  7275. init_get_bits(&s->gb, ptr, bit_length);
  7276. decode_picture_parameter_set(h, bit_length);
  7277. break;
  7278. case NAL_AUD:
  7279. case NAL_END_SEQUENCE:
  7280. case NAL_END_STREAM:
  7281. case NAL_FILLER_DATA:
  7282. case NAL_SPS_EXT:
  7283. case NAL_AUXILIARY_SLICE:
  7284. break;
  7285. default:
  7286. av_log(avctx, AV_LOG_ERROR, "Unknown NAL code: %d\n", h->nal_unit_type);
  7287. }
  7288. }
  7289. if(!s->current_picture_ptr) return buf_index; //no frame
  7290. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  7291. s->current_picture_ptr->pict_type= s->pict_type;
  7292. h->prev_frame_num_offset= h->frame_num_offset;
  7293. h->prev_frame_num= h->frame_num;
  7294. if(s->current_picture_ptr->reference){
  7295. h->prev_poc_msb= h->poc_msb;
  7296. h->prev_poc_lsb= h->poc_lsb;
  7297. }
  7298. if(s->current_picture_ptr->reference)
  7299. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  7300. ff_er_frame_end(s);
  7301. MPV_frame_end(s);
  7302. return buf_index;
  7303. }
  7304. /**
  7305. * returns the number of bytes consumed for building the current frame
  7306. */
  7307. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  7308. if(s->flags&CODEC_FLAG_TRUNCATED){
  7309. pos -= s->parse_context.last_index;
  7310. if(pos<0) pos=0; // FIXME remove (unneeded?)
  7311. return pos;
  7312. }else{
  7313. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  7314. if(pos+10>buf_size) pos=buf_size; // oops ;)
  7315. return pos;
  7316. }
  7317. }
  7318. static int decode_frame(AVCodecContext *avctx,
  7319. void *data, int *data_size,
  7320. uint8_t *buf, int buf_size)
  7321. {
  7322. H264Context *h = avctx->priv_data;
  7323. MpegEncContext *s = &h->s;
  7324. AVFrame *pict = data;
  7325. int buf_index;
  7326. s->flags= avctx->flags;
  7327. s->flags2= avctx->flags2;
  7328. /* no supplementary picture */
  7329. if (buf_size == 0) {
  7330. Picture *out;
  7331. int i, out_idx;
  7332. //FIXME factorize this with the output code below
  7333. out = h->delayed_pic[0];
  7334. out_idx = 0;
  7335. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
  7336. if(h->delayed_pic[i]->poc < out->poc){
  7337. out = h->delayed_pic[i];
  7338. out_idx = i;
  7339. }
  7340. for(i=out_idx; h->delayed_pic[i]; i++)
  7341. h->delayed_pic[i] = h->delayed_pic[i+1];
  7342. if(out){
  7343. *data_size = sizeof(AVFrame);
  7344. *pict= *(AVFrame*)out;
  7345. }
  7346. return 0;
  7347. }
  7348. if(s->flags&CODEC_FLAG_TRUNCATED){
  7349. int next= find_frame_end(h, buf, buf_size);
  7350. if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
  7351. return buf_size;
  7352. //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);
  7353. }
  7354. if(h->is_avc && !h->got_avcC) {
  7355. int i, cnt, nalsize;
  7356. unsigned char *p = avctx->extradata;
  7357. if(avctx->extradata_size < 7) {
  7358. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  7359. return -1;
  7360. }
  7361. if(*p != 1) {
  7362. av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
  7363. return -1;
  7364. }
  7365. /* sps and pps in the avcC always have length coded with 2 bytes,
  7366. so put a fake nal_length_size = 2 while parsing them */
  7367. h->nal_length_size = 2;
  7368. // Decode sps from avcC
  7369. cnt = *(p+5) & 0x1f; // Number of sps
  7370. p += 6;
  7371. for (i = 0; i < cnt; i++) {
  7372. nalsize = AV_RB16(p) + 2;
  7373. if(decode_nal_units(h, p, nalsize) < 0) {
  7374. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  7375. return -1;
  7376. }
  7377. p += nalsize;
  7378. }
  7379. // Decode pps from avcC
  7380. cnt = *(p++); // Number of pps
  7381. for (i = 0; i < cnt; i++) {
  7382. nalsize = AV_RB16(p) + 2;
  7383. if(decode_nal_units(h, p, nalsize) != nalsize) {
  7384. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  7385. return -1;
  7386. }
  7387. p += nalsize;
  7388. }
  7389. // Now store right nal length size, that will be use to parse all other nals
  7390. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  7391. // Do not reparse avcC
  7392. h->got_avcC = 1;
  7393. }
  7394. if(avctx->frame_number==0 && !h->is_avc && s->avctx->extradata_size){
  7395. if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
  7396. return -1;
  7397. }
  7398. buf_index=decode_nal_units(h, buf, buf_size);
  7399. if(buf_index < 0)
  7400. return -1;
  7401. //FIXME do something with unavailable reference frames
  7402. // if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_index, buf_size);
  7403. if(!s->current_picture_ptr){
  7404. av_log(h->s.avctx, AV_LOG_DEBUG, "error, NO frame\n");
  7405. return -1;
  7406. }
  7407. {
  7408. Picture *out = s->current_picture_ptr;
  7409. #if 0 //decode order
  7410. *data_size = sizeof(AVFrame);
  7411. #else
  7412. /* Sort B-frames into display order */
  7413. Picture *cur = s->current_picture_ptr;
  7414. Picture *prev = h->delayed_output_pic;
  7415. int i, pics, cross_idr, out_of_order, out_idx;
  7416. if(h->sps.bitstream_restriction_flag
  7417. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  7418. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  7419. s->low_delay = 0;
  7420. }
  7421. pics = 0;
  7422. while(h->delayed_pic[pics]) pics++;
  7423. assert(pics+1 < sizeof(h->delayed_pic) / sizeof(h->delayed_pic[0]));
  7424. h->delayed_pic[pics++] = cur;
  7425. if(cur->reference == 0)
  7426. cur->reference = 1;
  7427. cross_idr = 0;
  7428. for(i=0; h->delayed_pic[i]; i++)
  7429. if(h->delayed_pic[i]->key_frame || h->delayed_pic[i]->poc==0)
  7430. cross_idr = 1;
  7431. out = h->delayed_pic[0];
  7432. out_idx = 0;
  7433. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
  7434. if(h->delayed_pic[i]->poc < out->poc){
  7435. out = h->delayed_pic[i];
  7436. out_idx = i;
  7437. }
  7438. out_of_order = !cross_idr && prev && out->poc < prev->poc;
  7439. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  7440. { }
  7441. else if(prev && pics <= s->avctx->has_b_frames)
  7442. out = prev;
  7443. else if((out_of_order && pics-1 == s->avctx->has_b_frames && pics < 15)
  7444. || (s->low_delay &&
  7445. ((!cross_idr && prev && out->poc > prev->poc + 2)
  7446. || cur->pict_type == B_TYPE)))
  7447. {
  7448. s->low_delay = 0;
  7449. s->avctx->has_b_frames++;
  7450. out = prev;
  7451. }
  7452. else if(out_of_order)
  7453. out = prev;
  7454. if(out_of_order || pics > s->avctx->has_b_frames){
  7455. for(i=out_idx; h->delayed_pic[i]; i++)
  7456. h->delayed_pic[i] = h->delayed_pic[i+1];
  7457. }
  7458. if(prev == out)
  7459. *data_size = 0;
  7460. else
  7461. *data_size = sizeof(AVFrame);
  7462. if(prev && prev != out && prev->reference == 1)
  7463. prev->reference = 0;
  7464. h->delayed_output_pic = out;
  7465. #endif
  7466. if(out)
  7467. *pict= *(AVFrame*)out;
  7468. else
  7469. av_log(avctx, AV_LOG_DEBUG, "no picture\n");
  7470. }
  7471. assert(pict->data[0] || !*data_size);
  7472. ff_print_debug_info(s, pict);
  7473. //printf("out %d\n", (int)pict->data[0]);
  7474. #if 0 //?
  7475. /* Return the Picture timestamp as the frame number */
  7476. /* we substract 1 because it is added on utils.c */
  7477. avctx->frame_number = s->picture_number - 1;
  7478. #endif
  7479. return get_consumed_bytes(s, buf_index, buf_size);
  7480. }
  7481. #if 0
  7482. static inline void fill_mb_avail(H264Context *h){
  7483. MpegEncContext * const s = &h->s;
  7484. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  7485. if(s->mb_y){
  7486. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  7487. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  7488. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  7489. }else{
  7490. h->mb_avail[0]=
  7491. h->mb_avail[1]=
  7492. h->mb_avail[2]= 0;
  7493. }
  7494. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  7495. h->mb_avail[4]= 1; //FIXME move out
  7496. h->mb_avail[5]= 0; //FIXME move out
  7497. }
  7498. #endif
  7499. #if 0 //selftest
  7500. #define COUNT 8000
  7501. #define SIZE (COUNT*40)
  7502. int main(){
  7503. int i;
  7504. uint8_t temp[SIZE];
  7505. PutBitContext pb;
  7506. GetBitContext gb;
  7507. // int int_temp[10000];
  7508. DSPContext dsp;
  7509. AVCodecContext avctx;
  7510. dsputil_init(&dsp, &avctx);
  7511. init_put_bits(&pb, temp, SIZE);
  7512. printf("testing unsigned exp golomb\n");
  7513. for(i=0; i<COUNT; i++){
  7514. START_TIMER
  7515. set_ue_golomb(&pb, i);
  7516. STOP_TIMER("set_ue_golomb");
  7517. }
  7518. flush_put_bits(&pb);
  7519. init_get_bits(&gb, temp, 8*SIZE);
  7520. for(i=0; i<COUNT; i++){
  7521. int j, s;
  7522. s= show_bits(&gb, 24);
  7523. START_TIMER
  7524. j= get_ue_golomb(&gb);
  7525. if(j != i){
  7526. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7527. // return -1;
  7528. }
  7529. STOP_TIMER("get_ue_golomb");
  7530. }
  7531. init_put_bits(&pb, temp, SIZE);
  7532. printf("testing signed exp golomb\n");
  7533. for(i=0; i<COUNT; i++){
  7534. START_TIMER
  7535. set_se_golomb(&pb, i - COUNT/2);
  7536. STOP_TIMER("set_se_golomb");
  7537. }
  7538. flush_put_bits(&pb);
  7539. init_get_bits(&gb, temp, 8*SIZE);
  7540. for(i=0; i<COUNT; i++){
  7541. int j, s;
  7542. s= show_bits(&gb, 24);
  7543. START_TIMER
  7544. j= get_se_golomb(&gb);
  7545. if(j != i - COUNT/2){
  7546. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7547. // return -1;
  7548. }
  7549. STOP_TIMER("get_se_golomb");
  7550. }
  7551. printf("testing 4x4 (I)DCT\n");
  7552. DCTELEM block[16];
  7553. uint8_t src[16], ref[16];
  7554. uint64_t error= 0, max_error=0;
  7555. for(i=0; i<COUNT; i++){
  7556. int j;
  7557. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  7558. for(j=0; j<16; j++){
  7559. ref[j]= random()%255;
  7560. src[j]= random()%255;
  7561. }
  7562. h264_diff_dct_c(block, src, ref, 4);
  7563. //normalize
  7564. for(j=0; j<16; j++){
  7565. // printf("%d ", block[j]);
  7566. block[j]= block[j]*4;
  7567. if(j&1) block[j]= (block[j]*4 + 2)/5;
  7568. if(j&4) block[j]= (block[j]*4 + 2)/5;
  7569. }
  7570. // printf("\n");
  7571. s->dsp.h264_idct_add(ref, block, 4);
  7572. /* for(j=0; j<16; j++){
  7573. printf("%d ", ref[j]);
  7574. }
  7575. printf("\n");*/
  7576. for(j=0; j<16; j++){
  7577. int diff= FFABS(src[j] - ref[j]);
  7578. error+= diff*diff;
  7579. max_error= FFMAX(max_error, diff);
  7580. }
  7581. }
  7582. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  7583. #if 0
  7584. printf("testing quantizer\n");
  7585. for(qp=0; qp<52; qp++){
  7586. for(i=0; i<16; i++)
  7587. src1_block[i]= src2_block[i]= random()%255;
  7588. }
  7589. #endif
  7590. printf("Testing NAL layer\n");
  7591. uint8_t bitstream[COUNT];
  7592. uint8_t nal[COUNT*2];
  7593. H264Context h;
  7594. memset(&h, 0, sizeof(H264Context));
  7595. for(i=0; i<COUNT; i++){
  7596. int zeros= i;
  7597. int nal_length;
  7598. int consumed;
  7599. int out_length;
  7600. uint8_t *out;
  7601. int j;
  7602. for(j=0; j<COUNT; j++){
  7603. bitstream[j]= (random() % 255) + 1;
  7604. }
  7605. for(j=0; j<zeros; j++){
  7606. int pos= random() % COUNT;
  7607. while(bitstream[pos] == 0){
  7608. pos++;
  7609. pos %= COUNT;
  7610. }
  7611. bitstream[pos]=0;
  7612. }
  7613. START_TIMER
  7614. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  7615. if(nal_length<0){
  7616. printf("encoding failed\n");
  7617. return -1;
  7618. }
  7619. out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
  7620. STOP_TIMER("NAL")
  7621. if(out_length != COUNT){
  7622. printf("incorrect length %d %d\n", out_length, COUNT);
  7623. return -1;
  7624. }
  7625. if(consumed != nal_length){
  7626. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  7627. return -1;
  7628. }
  7629. if(memcmp(bitstream, out, COUNT)){
  7630. printf("missmatch\n");
  7631. return -1;
  7632. }
  7633. }
  7634. printf("Testing RBSP\n");
  7635. return 0;
  7636. }
  7637. #endif
  7638. static int decode_end(AVCodecContext *avctx)
  7639. {
  7640. H264Context *h = avctx->priv_data;
  7641. MpegEncContext *s = &h->s;
  7642. av_freep(&h->rbsp_buffer);
  7643. free_tables(h); //FIXME cleanup init stuff perhaps
  7644. MPV_common_end(s);
  7645. // memset(h, 0, sizeof(H264Context));
  7646. return 0;
  7647. }
  7648. AVCodec h264_decoder = {
  7649. "h264",
  7650. CODEC_TYPE_VIDEO,
  7651. CODEC_ID_H264,
  7652. sizeof(H264Context),
  7653. decode_init,
  7654. NULL,
  7655. decode_end,
  7656. decode_frame,
  7657. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  7658. .flush= flush_dpb,
  7659. };
  7660. #ifdef CONFIG_H264_PARSER
  7661. AVCodecParser h264_parser = {
  7662. { CODEC_ID_H264 },
  7663. sizeof(H264Context),
  7664. NULL,
  7665. h264_parse,
  7666. ff_parse_close,
  7667. h264_split,
  7668. };
  7669. #endif
  7670. #include "svq3.c"