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.

8547 lines
321KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. /**
  23. * @file h264.c
  24. * H.264 / AVC / MPEG4 part10 codec.
  25. * @author Michael Niedermayer <michaelni@gmx.at>
  26. */
  27. #include "common.h"
  28. #include "dsputil.h"
  29. #include "avcodec.h"
  30. #include "mpegvideo.h"
  31. #include "h264data.h"
  32. #include "golomb.h"
  33. #include "cabac.h"
  34. //#undef NDEBUG
  35. #include <assert.h>
  36. #define interlaced_dct interlaced_dct_is_a_bad_name
  37. #define mb_intra mb_intra_isnt_initalized_see_mb_type
  38. #define LUMA_DC_BLOCK_INDEX 25
  39. #define CHROMA_DC_BLOCK_INDEX 26
  40. #define CHROMA_DC_COEFF_TOKEN_VLC_BITS 8
  41. #define COEFF_TOKEN_VLC_BITS 8
  42. #define TOTAL_ZEROS_VLC_BITS 9
  43. #define CHROMA_DC_TOTAL_ZEROS_VLC_BITS 3
  44. #define RUN_VLC_BITS 3
  45. #define RUN7_VLC_BITS 6
  46. #define MAX_SPS_COUNT 32
  47. #define MAX_PPS_COUNT 256
  48. #define MAX_MMCO_COUNT 66
  49. /* Compiling in interlaced support reduces the speed
  50. * of progressive decoding by about 2%. */
  51. #define ALLOW_INTERLACE
  52. #ifdef ALLOW_INTERLACE
  53. #define MB_MBAFF h->mb_mbaff
  54. #define MB_FIELD h->mb_field_decoding_flag
  55. #define FRAME_MBAFF h->mb_aff_frame
  56. #else
  57. #define MB_MBAFF 0
  58. #define MB_FIELD 0
  59. #define FRAME_MBAFF 0
  60. #undef IS_INTERLACED
  61. #define IS_INTERLACED(mb_type) 0
  62. #endif
  63. /**
  64. * Sequence parameter set
  65. */
  66. typedef struct SPS{
  67. int profile_idc;
  68. int level_idc;
  69. int transform_bypass; ///< qpprime_y_zero_transform_bypass_flag
  70. int log2_max_frame_num; ///< log2_max_frame_num_minus4 + 4
  71. int poc_type; ///< pic_order_cnt_type
  72. int log2_max_poc_lsb; ///< log2_max_pic_order_cnt_lsb_minus4
  73. int delta_pic_order_always_zero_flag;
  74. int offset_for_non_ref_pic;
  75. int offset_for_top_to_bottom_field;
  76. int poc_cycle_length; ///< num_ref_frames_in_pic_order_cnt_cycle
  77. int ref_frame_count; ///< num_ref_frames
  78. int gaps_in_frame_num_allowed_flag;
  79. int mb_width; ///< frame_width_in_mbs_minus1 + 1
  80. int mb_height; ///< frame_height_in_mbs_minus1 + 1
  81. int frame_mbs_only_flag;
  82. int mb_aff; ///<mb_adaptive_frame_field_flag
  83. int direct_8x8_inference_flag;
  84. int crop; ///< frame_cropping_flag
  85. int crop_left; ///< frame_cropping_rect_left_offset
  86. int crop_right; ///< frame_cropping_rect_right_offset
  87. int crop_top; ///< frame_cropping_rect_top_offset
  88. int crop_bottom; ///< frame_cropping_rect_bottom_offset
  89. int vui_parameters_present_flag;
  90. AVRational sar;
  91. int timing_info_present_flag;
  92. uint32_t num_units_in_tick;
  93. uint32_t time_scale;
  94. int fixed_frame_rate_flag;
  95. short offset_for_ref_frame[256]; //FIXME dyn aloc?
  96. int bitstream_restriction_flag;
  97. int num_reorder_frames;
  98. int scaling_matrix_present;
  99. uint8_t scaling_matrix4[6][16];
  100. uint8_t scaling_matrix8[2][64];
  101. }SPS;
  102. /**
  103. * Picture parameter set
  104. */
  105. typedef struct PPS{
  106. int sps_id;
  107. int cabac; ///< entropy_coding_mode_flag
  108. int pic_order_present; ///< pic_order_present_flag
  109. int slice_group_count; ///< num_slice_groups_minus1 + 1
  110. int mb_slice_group_map_type;
  111. int ref_count[2]; ///< num_ref_idx_l0/1_active_minus1 + 1
  112. int weighted_pred; ///< weighted_pred_flag
  113. int weighted_bipred_idc;
  114. int init_qp; ///< pic_init_qp_minus26 + 26
  115. int init_qs; ///< pic_init_qs_minus26 + 26
  116. int chroma_qp_index_offset;
  117. int deblocking_filter_parameters_present; ///< deblocking_filter_parameters_present_flag
  118. int constrained_intra_pred; ///< constrained_intra_pred_flag
  119. int redundant_pic_cnt_present; ///< redundant_pic_cnt_present_flag
  120. int transform_8x8_mode; ///< transform_8x8_mode_flag
  121. uint8_t scaling_matrix4[6][16];
  122. uint8_t scaling_matrix8[2][64];
  123. }PPS;
  124. /**
  125. * Memory management control operation opcode.
  126. */
  127. typedef enum MMCOOpcode{
  128. MMCO_END=0,
  129. MMCO_SHORT2UNUSED,
  130. MMCO_LONG2UNUSED,
  131. MMCO_SHORT2LONG,
  132. MMCO_SET_MAX_LONG,
  133. MMCO_RESET,
  134. MMCO_LONG,
  135. } MMCOOpcode;
  136. /**
  137. * Memory management control operation.
  138. */
  139. typedef struct MMCO{
  140. MMCOOpcode opcode;
  141. int short_frame_num;
  142. int long_index;
  143. } MMCO;
  144. /**
  145. * H264Context
  146. */
  147. typedef struct H264Context{
  148. MpegEncContext s;
  149. int nal_ref_idc;
  150. int nal_unit_type;
  151. uint8_t *rbsp_buffer;
  152. unsigned int rbsp_buffer_size;
  153. /**
  154. * Used to parse AVC variant of h264
  155. */
  156. int is_avc; ///< this flag is != 0 if codec is avc1
  157. int got_avcC; ///< flag used to parse avcC data only once
  158. int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
  159. int chroma_qp; //QPc
  160. int prev_mb_skipped;
  161. int next_mb_skipped;
  162. //prediction stuff
  163. int chroma_pred_mode;
  164. int intra16x16_pred_mode;
  165. int top_mb_xy;
  166. int left_mb_xy[2];
  167. int8_t intra4x4_pred_mode_cache[5*8];
  168. int8_t (*intra4x4_pred_mode)[8];
  169. void (*pred4x4 [9+3])(uint8_t *src, uint8_t *topright, int stride);//FIXME move to dsp?
  170. void (*pred8x8l [9+3])(uint8_t *src, int topleft, int topright, int stride);
  171. void (*pred8x8 [4+3])(uint8_t *src, int stride);
  172. void (*pred16x16[4+3])(uint8_t *src, int stride);
  173. unsigned int topleft_samples_available;
  174. unsigned int top_samples_available;
  175. unsigned int topright_samples_available;
  176. unsigned int left_samples_available;
  177. uint8_t (*top_borders[2])[16+2*8];
  178. uint8_t left_border[2*(17+2*9)];
  179. /**
  180. * non zero coeff count cache.
  181. * is 64 if not available.
  182. */
  183. DECLARE_ALIGNED_8(uint8_t, non_zero_count_cache[6*8]);
  184. uint8_t (*non_zero_count)[16];
  185. /**
  186. * Motion vector cache.
  187. */
  188. DECLARE_ALIGNED_8(int16_t, mv_cache[2][5*8][2]);
  189. DECLARE_ALIGNED_8(int8_t, ref_cache[2][5*8]);
  190. #define LIST_NOT_USED -1 //FIXME rename?
  191. #define PART_NOT_AVAILABLE -2
  192. /**
  193. * is 1 if the specific list MV&references are set to 0,0,-2.
  194. */
  195. int mv_cache_clean[2];
  196. /**
  197. * number of neighbors (top and/or left) that used 8x8 dct
  198. */
  199. int neighbor_transform_size;
  200. /**
  201. * block_offset[ 0..23] for frame macroblocks
  202. * block_offset[24..47] for field macroblocks
  203. */
  204. int block_offset[2*(16+8)];
  205. uint32_t *mb2b_xy; //FIXME are these 4 a good idea?
  206. uint32_t *mb2b8_xy;
  207. int b_stride; //FIXME use s->b4_stride
  208. int b8_stride;
  209. int mb_linesize; ///< may be equal to s->linesize or s->linesize*2, for mbaff
  210. int mb_uvlinesize;
  211. int emu_edge_width;
  212. int emu_edge_height;
  213. int halfpel_flag;
  214. int thirdpel_flag;
  215. int unknown_svq3_flag;
  216. int next_slice_index;
  217. SPS sps_buffer[MAX_SPS_COUNT];
  218. SPS sps; ///< current sps
  219. PPS pps_buffer[MAX_PPS_COUNT];
  220. /**
  221. * current pps
  222. */
  223. PPS pps; //FIXME move to Picture perhaps? (->no) do we need that?
  224. uint32_t dequant4_buffer[6][52][16];
  225. uint32_t dequant8_buffer[2][52][64];
  226. uint32_t (*dequant4_coeff[6])[16];
  227. uint32_t (*dequant8_coeff[2])[64];
  228. int dequant_coeff_pps; ///< reinit tables when pps changes
  229. int slice_num;
  230. uint8_t *slice_table_base;
  231. uint8_t *slice_table; ///< slice_table_base + 2*mb_stride + 1
  232. int slice_type;
  233. int slice_type_fixed;
  234. //interlacing specific flags
  235. int mb_aff_frame;
  236. int mb_field_decoding_flag;
  237. int mb_mbaff; ///< mb_aff_frame && mb_field_decoding_flag
  238. int sub_mb_type[4];
  239. //POC stuff
  240. int poc_lsb;
  241. int poc_msb;
  242. int delta_poc_bottom;
  243. int delta_poc[2];
  244. int frame_num;
  245. int prev_poc_msb; ///< poc_msb of the last reference pic for POC type 0
  246. int prev_poc_lsb; ///< poc_lsb of the last reference pic for POC type 0
  247. int frame_num_offset; ///< for POC type 2
  248. int prev_frame_num_offset; ///< for POC type 2
  249. int prev_frame_num; ///< frame_num of the last pic for POC type 1/2
  250. /**
  251. * frame_num for frames or 2*frame_num for field pics.
  252. */
  253. int curr_pic_num;
  254. /**
  255. * max_frame_num or 2*max_frame_num for field pics.
  256. */
  257. int max_pic_num;
  258. //Weighted pred stuff
  259. int use_weight;
  260. int use_weight_chroma;
  261. int luma_log2_weight_denom;
  262. int chroma_log2_weight_denom;
  263. int luma_weight[2][48];
  264. int luma_offset[2][48];
  265. int chroma_weight[2][48][2];
  266. int chroma_offset[2][48][2];
  267. int implicit_weight[48][48];
  268. //deblock
  269. int deblocking_filter; ///< disable_deblocking_filter_idc with 1<->0
  270. int slice_alpha_c0_offset;
  271. int slice_beta_offset;
  272. int redundant_pic_count;
  273. int direct_spatial_mv_pred;
  274. int dist_scale_factor[16];
  275. int dist_scale_factor_field[32];
  276. int map_col_to_list0[2][16];
  277. int map_col_to_list0_field[2][32];
  278. /**
  279. * num_ref_idx_l0/1_active_minus1 + 1
  280. */
  281. int ref_count[2]; ///< counts frames or fields, depending on current mb mode
  282. Picture *short_ref[32];
  283. Picture *long_ref[32];
  284. Picture default_ref_list[2][32];
  285. Picture ref_list[2][48]; ///< 0..15: frame refs, 16..47: mbaff field refs
  286. Picture *delayed_pic[16]; //FIXME size?
  287. Picture *delayed_output_pic;
  288. /**
  289. * memory management control operations buffer.
  290. */
  291. MMCO mmco[MAX_MMCO_COUNT];
  292. int mmco_index;
  293. int long_ref_count; ///< number of actual long term references
  294. int short_ref_count; ///< number of actual short term references
  295. //data partitioning
  296. GetBitContext intra_gb;
  297. GetBitContext inter_gb;
  298. GetBitContext *intra_gb_ptr;
  299. GetBitContext *inter_gb_ptr;
  300. DECLARE_ALIGNED_8(DCTELEM, mb[16*24]);
  301. /**
  302. * Cabac
  303. */
  304. CABACContext cabac;
  305. uint8_t cabac_state[460];
  306. int cabac_init_idc;
  307. /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
  308. uint16_t *cbp_table;
  309. int cbp;
  310. int top_cbp;
  311. int left_cbp;
  312. /* chroma_pred_mode for i4x4 or i16x16, else 0 */
  313. uint8_t *chroma_pred_mode_table;
  314. int last_qscale_diff;
  315. int16_t (*mvd_table[2])[2];
  316. DECLARE_ALIGNED_8(int16_t, mvd_cache[2][5*8][2]);
  317. uint8_t *direct_table;
  318. uint8_t direct_cache[5*8];
  319. uint8_t zigzag_scan[16];
  320. uint8_t zigzag_scan8x8[64];
  321. uint8_t zigzag_scan8x8_cavlc[64];
  322. uint8_t field_scan[16];
  323. uint8_t field_scan8x8[64];
  324. uint8_t field_scan8x8_cavlc[64];
  325. const uint8_t *zigzag_scan_q0;
  326. const uint8_t *zigzag_scan8x8_q0;
  327. const uint8_t *zigzag_scan8x8_cavlc_q0;
  328. const uint8_t *field_scan_q0;
  329. const uint8_t *field_scan8x8_q0;
  330. const uint8_t *field_scan8x8_cavlc_q0;
  331. int x264_build;
  332. }H264Context;
  333. static VLC coeff_token_vlc[4];
  334. static VLC chroma_dc_coeff_token_vlc;
  335. static VLC total_zeros_vlc[15];
  336. static VLC chroma_dc_total_zeros_vlc[3];
  337. static VLC run_vlc[6];
  338. static VLC run7_vlc;
  339. static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
  340. static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
  341. static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
  342. static void filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
  343. static av_always_inline uint32_t pack16to32(int a, int b){
  344. #ifdef WORDS_BIGENDIAN
  345. return (b&0xFFFF) + (a<<16);
  346. #else
  347. return (a&0xFFFF) + (b<<16);
  348. #endif
  349. }
  350. const uint8_t ff_rem6[52]={
  351. 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,
  352. };
  353. const uint8_t ff_div6[52]={
  354. 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,
  355. };
  356. /**
  357. * fill a rectangle.
  358. * @param h height of the rectangle, should be a constant
  359. * @param w width of the rectangle, should be a constant
  360. * @param size the size of val (1 or 4), should be a constant
  361. */
  362. static av_always_inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){
  363. uint8_t *p= (uint8_t*)vp;
  364. assert(size==1 || size==4);
  365. assert(w<=4);
  366. w *= size;
  367. stride *= size;
  368. assert((((long)vp)&(FFMIN(w, STRIDE_ALIGN)-1)) == 0);
  369. assert((stride&(w-1))==0);
  370. if(w==2){
  371. const uint16_t v= size==4 ? val : val*0x0101;
  372. *(uint16_t*)(p + 0*stride)= v;
  373. if(h==1) return;
  374. *(uint16_t*)(p + 1*stride)= v;
  375. if(h==2) return;
  376. *(uint16_t*)(p + 2*stride)=
  377. *(uint16_t*)(p + 3*stride)= v;
  378. }else if(w==4){
  379. const uint32_t v= size==4 ? val : val*0x01010101;
  380. *(uint32_t*)(p + 0*stride)= v;
  381. if(h==1) return;
  382. *(uint32_t*)(p + 1*stride)= v;
  383. if(h==2) return;
  384. *(uint32_t*)(p + 2*stride)=
  385. *(uint32_t*)(p + 3*stride)= v;
  386. }else if(w==8){
  387. //gcc can't optimize 64bit math on x86_32
  388. #if defined(ARCH_X86_64) || (defined(MP_WORDSIZE) && MP_WORDSIZE >= 64)
  389. const uint64_t v= val*0x0100000001ULL;
  390. *(uint64_t*)(p + 0*stride)= v;
  391. if(h==1) return;
  392. *(uint64_t*)(p + 1*stride)= v;
  393. if(h==2) return;
  394. *(uint64_t*)(p + 2*stride)=
  395. *(uint64_t*)(p + 3*stride)= v;
  396. }else if(w==16){
  397. const uint64_t v= val*0x0100000001ULL;
  398. *(uint64_t*)(p + 0+0*stride)=
  399. *(uint64_t*)(p + 8+0*stride)=
  400. *(uint64_t*)(p + 0+1*stride)=
  401. *(uint64_t*)(p + 8+1*stride)= v;
  402. if(h==2) return;
  403. *(uint64_t*)(p + 0+2*stride)=
  404. *(uint64_t*)(p + 8+2*stride)=
  405. *(uint64_t*)(p + 0+3*stride)=
  406. *(uint64_t*)(p + 8+3*stride)= v;
  407. #else
  408. *(uint32_t*)(p + 0+0*stride)=
  409. *(uint32_t*)(p + 4+0*stride)= val;
  410. if(h==1) return;
  411. *(uint32_t*)(p + 0+1*stride)=
  412. *(uint32_t*)(p + 4+1*stride)= val;
  413. if(h==2) return;
  414. *(uint32_t*)(p + 0+2*stride)=
  415. *(uint32_t*)(p + 4+2*stride)=
  416. *(uint32_t*)(p + 0+3*stride)=
  417. *(uint32_t*)(p + 4+3*stride)= val;
  418. }else if(w==16){
  419. *(uint32_t*)(p + 0+0*stride)=
  420. *(uint32_t*)(p + 4+0*stride)=
  421. *(uint32_t*)(p + 8+0*stride)=
  422. *(uint32_t*)(p +12+0*stride)=
  423. *(uint32_t*)(p + 0+1*stride)=
  424. *(uint32_t*)(p + 4+1*stride)=
  425. *(uint32_t*)(p + 8+1*stride)=
  426. *(uint32_t*)(p +12+1*stride)= val;
  427. if(h==2) return;
  428. *(uint32_t*)(p + 0+2*stride)=
  429. *(uint32_t*)(p + 4+2*stride)=
  430. *(uint32_t*)(p + 8+2*stride)=
  431. *(uint32_t*)(p +12+2*stride)=
  432. *(uint32_t*)(p + 0+3*stride)=
  433. *(uint32_t*)(p + 4+3*stride)=
  434. *(uint32_t*)(p + 8+3*stride)=
  435. *(uint32_t*)(p +12+3*stride)= val;
  436. #endif
  437. }else
  438. assert(0);
  439. assert(h==4);
  440. }
  441. static void fill_caches(H264Context *h, int mb_type, int for_deblock){
  442. MpegEncContext * const s = &h->s;
  443. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  444. int topleft_xy, top_xy, topright_xy, left_xy[2];
  445. int topleft_type, top_type, topright_type, left_type[2];
  446. int left_block[8];
  447. int i;
  448. //FIXME deblocking could skip the intra and nnz parts.
  449. if(for_deblock && (h->slice_num == 1 || h->slice_table[mb_xy] == h->slice_table[mb_xy-s->mb_stride]) && !FRAME_MBAFF)
  450. return;
  451. //wow what a mess, why didn't they simplify the interlacing&intra stuff, i can't imagine that these complex rules are worth it
  452. top_xy = mb_xy - s->mb_stride;
  453. topleft_xy = top_xy - 1;
  454. topright_xy= top_xy + 1;
  455. left_xy[1] = left_xy[0] = mb_xy-1;
  456. left_block[0]= 0;
  457. left_block[1]= 1;
  458. left_block[2]= 2;
  459. left_block[3]= 3;
  460. left_block[4]= 7;
  461. left_block[5]= 10;
  462. left_block[6]= 8;
  463. left_block[7]= 11;
  464. if(FRAME_MBAFF){
  465. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  466. const int top_pair_xy = pair_xy - s->mb_stride;
  467. const int topleft_pair_xy = top_pair_xy - 1;
  468. const int topright_pair_xy = top_pair_xy + 1;
  469. const int topleft_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
  470. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  471. const int topright_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
  472. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  473. const int curr_mb_frame_flag = !IS_INTERLACED(mb_type);
  474. const int bottom = (s->mb_y & 1);
  475. tprintf("fill_caches: curr_mb_frame_flag:%d, left_mb_frame_flag:%d, topleft_mb_frame_flag:%d, top_mb_frame_flag:%d, topright_mb_frame_flag:%d\n", curr_mb_frame_flag, left_mb_frame_flag, topleft_mb_frame_flag, top_mb_frame_flag, topright_mb_frame_flag);
  476. if (bottom
  477. ? !curr_mb_frame_flag // bottom macroblock
  478. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  479. ) {
  480. top_xy -= s->mb_stride;
  481. }
  482. if (bottom
  483. ? !curr_mb_frame_flag // bottom macroblock
  484. : (!curr_mb_frame_flag && !topleft_mb_frame_flag) // top macroblock
  485. ) {
  486. topleft_xy -= s->mb_stride;
  487. }
  488. if (bottom
  489. ? !curr_mb_frame_flag // bottom macroblock
  490. : (!curr_mb_frame_flag && !topright_mb_frame_flag) // top macroblock
  491. ) {
  492. topright_xy -= s->mb_stride;
  493. }
  494. if (left_mb_frame_flag != curr_mb_frame_flag) {
  495. left_xy[1] = left_xy[0] = pair_xy - 1;
  496. if (curr_mb_frame_flag) {
  497. if (bottom) {
  498. left_block[0]= 2;
  499. left_block[1]= 2;
  500. left_block[2]= 3;
  501. left_block[3]= 3;
  502. left_block[4]= 8;
  503. left_block[5]= 11;
  504. left_block[6]= 8;
  505. left_block[7]= 11;
  506. } else {
  507. left_block[0]= 0;
  508. left_block[1]= 0;
  509. left_block[2]= 1;
  510. left_block[3]= 1;
  511. left_block[4]= 7;
  512. left_block[5]= 10;
  513. left_block[6]= 7;
  514. left_block[7]= 10;
  515. }
  516. } else {
  517. left_xy[1] += s->mb_stride;
  518. //left_block[0]= 0;
  519. left_block[1]= 2;
  520. left_block[2]= 0;
  521. left_block[3]= 2;
  522. //left_block[4]= 7;
  523. left_block[5]= 10;
  524. left_block[6]= 7;
  525. left_block[7]= 10;
  526. }
  527. }
  528. }
  529. h->top_mb_xy = top_xy;
  530. h->left_mb_xy[0] = left_xy[0];
  531. h->left_mb_xy[1] = left_xy[1];
  532. if(for_deblock){
  533. topleft_type = 0;
  534. topright_type = 0;
  535. top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0;
  536. left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
  537. left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
  538. if(FRAME_MBAFF && !IS_INTRA(mb_type)){
  539. int list;
  540. int v = *(uint16_t*)&h->non_zero_count[mb_xy][14];
  541. for(i=0; i<16; i++)
  542. h->non_zero_count_cache[scan8[i]] = (v>>i)&1;
  543. for(list=0; list<1+(h->slice_type==B_TYPE); list++){
  544. if(USES_LIST(mb_type,list)){
  545. uint32_t *src = (uint32_t*)s->current_picture.motion_val[list][h->mb2b_xy[mb_xy]];
  546. uint32_t *dst = (uint32_t*)h->mv_cache[list][scan8[0]];
  547. int8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
  548. for(i=0; i<4; i++, dst+=8, src+=h->b_stride){
  549. dst[0] = src[0];
  550. dst[1] = src[1];
  551. dst[2] = src[2];
  552. dst[3] = src[3];
  553. }
  554. *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =
  555. *(uint32_t*)&h->ref_cache[list][scan8[ 2]] = pack16to32(ref[0],ref[1])*0x0101;
  556. ref += h->b8_stride;
  557. *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =
  558. *(uint32_t*)&h->ref_cache[list][scan8[10]] = pack16to32(ref[0],ref[1])*0x0101;
  559. }else{
  560. fill_rectangle(&h-> mv_cache[list][scan8[ 0]], 4, 4, 8, 0, 4);
  561. fill_rectangle(&h->ref_cache[list][scan8[ 0]], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  562. }
  563. }
  564. }
  565. }else{
  566. topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
  567. top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
  568. topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
  569. left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
  570. left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
  571. }
  572. if(IS_INTRA(mb_type)){
  573. h->topleft_samples_available=
  574. h->top_samples_available=
  575. h->left_samples_available= 0xFFFF;
  576. h->topright_samples_available= 0xEEEA;
  577. if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
  578. h->topleft_samples_available= 0xB3FF;
  579. h->top_samples_available= 0x33FF;
  580. h->topright_samples_available= 0x26EA;
  581. }
  582. for(i=0; i<2; i++){
  583. if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
  584. h->topleft_samples_available&= 0xDF5F;
  585. h->left_samples_available&= 0x5F5F;
  586. }
  587. }
  588. if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
  589. h->topleft_samples_available&= 0x7FFF;
  590. if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
  591. h->topright_samples_available&= 0xFBFF;
  592. if(IS_INTRA4x4(mb_type)){
  593. if(IS_INTRA4x4(top_type)){
  594. h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
  595. h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
  596. h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
  597. h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
  598. }else{
  599. int pred;
  600. if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred))
  601. pred= -1;
  602. else{
  603. pred= 2;
  604. }
  605. h->intra4x4_pred_mode_cache[4+8*0]=
  606. h->intra4x4_pred_mode_cache[5+8*0]=
  607. h->intra4x4_pred_mode_cache[6+8*0]=
  608. h->intra4x4_pred_mode_cache[7+8*0]= pred;
  609. }
  610. for(i=0; i<2; i++){
  611. if(IS_INTRA4x4(left_type[i])){
  612. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
  613. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
  614. }else{
  615. int pred;
  616. if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred))
  617. pred= -1;
  618. else{
  619. pred= 2;
  620. }
  621. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
  622. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
  623. }
  624. }
  625. }
  626. }
  627. /*
  628. 0 . T T. T T T T
  629. 1 L . .L . . . .
  630. 2 L . .L . . . .
  631. 3 . T TL . . . .
  632. 4 L . .L . . . .
  633. 5 L . .. . . . .
  634. */
  635. //FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec)
  636. if(top_type){
  637. h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
  638. h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
  639. h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
  640. h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
  641. h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
  642. h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
  643. h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
  644. h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
  645. }else{
  646. h->non_zero_count_cache[4+8*0]=
  647. h->non_zero_count_cache[5+8*0]=
  648. h->non_zero_count_cache[6+8*0]=
  649. h->non_zero_count_cache[7+8*0]=
  650. h->non_zero_count_cache[1+8*0]=
  651. h->non_zero_count_cache[2+8*0]=
  652. h->non_zero_count_cache[1+8*3]=
  653. h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  654. }
  655. for (i=0; i<2; i++) {
  656. if(left_type[i]){
  657. h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
  658. h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
  659. h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
  660. h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
  661. }else{
  662. h->non_zero_count_cache[3+8*1 + 2*8*i]=
  663. h->non_zero_count_cache[3+8*2 + 2*8*i]=
  664. h->non_zero_count_cache[0+8*1 + 8*i]=
  665. h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  666. }
  667. }
  668. if( h->pps.cabac ) {
  669. // top_cbp
  670. if(top_type) {
  671. h->top_cbp = h->cbp_table[top_xy];
  672. } else if(IS_INTRA(mb_type)) {
  673. h->top_cbp = 0x1C0;
  674. } else {
  675. h->top_cbp = 0;
  676. }
  677. // left_cbp
  678. if (left_type[0]) {
  679. h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
  680. } else if(IS_INTRA(mb_type)) {
  681. h->left_cbp = 0x1C0;
  682. } else {
  683. h->left_cbp = 0;
  684. }
  685. if (left_type[0]) {
  686. h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
  687. }
  688. if (left_type[1]) {
  689. h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
  690. }
  691. }
  692. #if 1
  693. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  694. int list;
  695. for(list=0; list<1+(h->slice_type==B_TYPE); list++){
  696. if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
  697. /*if(!h->mv_cache_clean[list]){
  698. memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
  699. memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
  700. h->mv_cache_clean[list]= 1;
  701. }*/
  702. continue;
  703. }
  704. h->mv_cache_clean[list]= 0;
  705. if(USES_LIST(top_type, list)){
  706. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  707. const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
  708. *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
  709. *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
  710. *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
  711. *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
  712. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  713. h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
  714. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  715. h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
  716. }else{
  717. *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
  718. *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
  719. *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
  720. *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
  721. *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
  722. }
  723. //FIXME unify cleanup or sth
  724. if(USES_LIST(left_type[0], list)){
  725. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  726. const int b8_xy= h->mb2b8_xy[left_xy[0]] + 1;
  727. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 + 0*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[0]];
  728. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 + 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[1]];
  729. h->ref_cache[list][scan8[0] - 1 + 0*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0]>>1)];
  730. h->ref_cache[list][scan8[0] - 1 + 1*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1]>>1)];
  731. }else{
  732. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 0*8]=
  733. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 1*8]= 0;
  734. h->ref_cache[list][scan8[0] - 1 + 0*8]=
  735. h->ref_cache[list][scan8[0] - 1 + 1*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  736. }
  737. if(USES_LIST(left_type[1], list)){
  738. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  739. const int b8_xy= h->mb2b8_xy[left_xy[1]] + 1;
  740. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 + 2*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[2]];
  741. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 + 3*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[3]];
  742. h->ref_cache[list][scan8[0] - 1 + 2*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[2]>>1)];
  743. h->ref_cache[list][scan8[0] - 1 + 3*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[3]>>1)];
  744. }else{
  745. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 2*8]=
  746. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 3*8]= 0;
  747. h->ref_cache[list][scan8[0] - 1 + 2*8]=
  748. h->ref_cache[list][scan8[0] - 1 + 3*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  749. assert((!left_type[0]) == (!left_type[1]));
  750. }
  751. if((for_deblock || (IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred)) && !FRAME_MBAFF)
  752. continue;
  753. if(USES_LIST(topleft_type, list)){
  754. const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
  755. const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + h->b8_stride;
  756. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  757. h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  758. }else{
  759. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
  760. h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  761. }
  762. if(USES_LIST(topright_type, list)){
  763. const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
  764. const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
  765. *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  766. h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  767. }else{
  768. *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
  769. h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  770. }
  771. if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)
  772. continue;
  773. h->ref_cache[list][scan8[5 ]+1] =
  774. h->ref_cache[list][scan8[7 ]+1] =
  775. h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewhere else)
  776. h->ref_cache[list][scan8[4 ]] =
  777. h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
  778. *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
  779. *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
  780. *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  781. *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
  782. *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
  783. if( h->pps.cabac ) {
  784. /* XXX beurk, Load mvd */
  785. if(USES_LIST(top_type, list)){
  786. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  787. *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
  788. *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
  789. *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
  790. *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
  791. }else{
  792. *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
  793. *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
  794. *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
  795. *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
  796. }
  797. if(USES_LIST(left_type[0], list)){
  798. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  799. *(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]];
  800. *(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]];
  801. }else{
  802. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
  803. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
  804. }
  805. if(USES_LIST(left_type[1], list)){
  806. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  807. *(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]];
  808. *(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]];
  809. }else{
  810. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
  811. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
  812. }
  813. *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
  814. *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
  815. *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  816. *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
  817. *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
  818. if(h->slice_type == B_TYPE){
  819. fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
  820. if(IS_DIRECT(top_type)){
  821. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
  822. }else if(IS_8X8(top_type)){
  823. int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
  824. h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
  825. h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
  826. }else{
  827. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
  828. }
  829. if(IS_DIRECT(left_type[0]))
  830. h->direct_cache[scan8[0] - 1 + 0*8]= 1;
  831. else if(IS_8X8(left_type[0]))
  832. 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)];
  833. else
  834. h->direct_cache[scan8[0] - 1 + 0*8]= 0;
  835. if(IS_DIRECT(left_type[1]))
  836. h->direct_cache[scan8[0] - 1 + 2*8]= 1;
  837. else if(IS_8X8(left_type[1]))
  838. 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)];
  839. else
  840. h->direct_cache[scan8[0] - 1 + 2*8]= 0;
  841. }
  842. }
  843. if(FRAME_MBAFF){
  844. #define MAP_MVS\
  845. MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
  846. MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
  847. MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
  848. MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
  849. MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
  850. MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
  851. MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\
  852. MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\
  853. MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\
  854. MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
  855. if(MB_FIELD){
  856. #define MAP_F2F(idx, mb_type)\
  857. if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  858. h->ref_cache[list][idx] <<= 1;\
  859. h->mv_cache[list][idx][1] /= 2;\
  860. h->mvd_cache[list][idx][1] /= 2;\
  861. }
  862. MAP_MVS
  863. #undef MAP_F2F
  864. }else{
  865. #define MAP_F2F(idx, mb_type)\
  866. if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  867. h->ref_cache[list][idx] >>= 1;\
  868. h->mv_cache[list][idx][1] <<= 1;\
  869. h->mvd_cache[list][idx][1] <<= 1;\
  870. }
  871. MAP_MVS
  872. #undef MAP_F2F
  873. }
  874. }
  875. }
  876. }
  877. #endif
  878. h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
  879. }
  880. static inline void write_back_intra_pred_mode(H264Context *h){
  881. MpegEncContext * const s = &h->s;
  882. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  883. h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
  884. h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
  885. h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
  886. h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
  887. h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
  888. h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
  889. h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
  890. }
  891. /**
  892. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  893. */
  894. static inline int check_intra4x4_pred_mode(H264Context *h){
  895. MpegEncContext * const s = &h->s;
  896. static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
  897. static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
  898. int i;
  899. if(!(h->top_samples_available&0x8000)){
  900. for(i=0; i<4; i++){
  901. int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
  902. if(status<0){
  903. 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);
  904. return -1;
  905. } else if(status){
  906. h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
  907. }
  908. }
  909. }
  910. if(!(h->left_samples_available&0x8000)){
  911. for(i=0; i<4; i++){
  912. int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
  913. if(status<0){
  914. 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);
  915. return -1;
  916. } else if(status){
  917. h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
  918. }
  919. }
  920. }
  921. return 0;
  922. } //FIXME cleanup like next
  923. /**
  924. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  925. */
  926. static inline int check_intra_pred_mode(H264Context *h, int mode){
  927. MpegEncContext * const s = &h->s;
  928. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  929. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  930. if(mode < 0 || mode > 6) {
  931. 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);
  932. return -1;
  933. }
  934. if(!(h->top_samples_available&0x8000)){
  935. mode= top[ mode ];
  936. if(mode<0){
  937. 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);
  938. return -1;
  939. }
  940. }
  941. if(!(h->left_samples_available&0x8000)){
  942. mode= left[ mode ];
  943. if(mode<0){
  944. 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);
  945. return -1;
  946. }
  947. }
  948. return mode;
  949. }
  950. /**
  951. * gets the predicted intra4x4 prediction mode.
  952. */
  953. static inline int pred_intra_mode(H264Context *h, int n){
  954. const int index8= scan8[n];
  955. const int left= h->intra4x4_pred_mode_cache[index8 - 1];
  956. const int top = h->intra4x4_pred_mode_cache[index8 - 8];
  957. const int min= FFMIN(left, top);
  958. tprintf("mode:%d %d min:%d\n", left ,top, min);
  959. if(min<0) return DC_PRED;
  960. else return min;
  961. }
  962. static inline void write_back_non_zero_count(H264Context *h){
  963. MpegEncContext * const s = &h->s;
  964. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  965. h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
  966. h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
  967. h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
  968. h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
  969. h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
  970. h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
  971. h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
  972. h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
  973. h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
  974. h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
  975. h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
  976. h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
  977. h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
  978. if(FRAME_MBAFF){
  979. // store all luma nnzs, for deblocking
  980. int v = 0, i;
  981. for(i=0; i<16; i++)
  982. v += (!!h->non_zero_count_cache[scan8[i]]) << i;
  983. *(uint16_t*)&h->non_zero_count[mb_xy][14] = v;
  984. }
  985. }
  986. /**
  987. * gets the predicted number of non zero coefficients.
  988. * @param n block index
  989. */
  990. static inline int pred_non_zero_count(H264Context *h, int n){
  991. const int index8= scan8[n];
  992. const int left= h->non_zero_count_cache[index8 - 1];
  993. const int top = h->non_zero_count_cache[index8 - 8];
  994. int i= left + top;
  995. if(i<64) i= (i+1)>>1;
  996. tprintf("pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
  997. return i&31;
  998. }
  999. static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
  1000. const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
  1001. /* there is no consistent mapping of mvs to neighboring locations that will
  1002. * make mbaff happy, so we can't move all this logic to fill_caches */
  1003. if(FRAME_MBAFF){
  1004. MpegEncContext *s = &h->s;
  1005. const uint32_t *mb_types = s->current_picture_ptr->mb_type;
  1006. const int16_t *mv;
  1007. *(uint32_t*)h->mv_cache[list][scan8[0]-2] = 0;
  1008. *C = h->mv_cache[list][scan8[0]-2];
  1009. if(!MB_FIELD
  1010. && (s->mb_y&1) && i < scan8[0]+8 && topright_ref != PART_NOT_AVAILABLE){
  1011. int topright_xy = s->mb_x + (s->mb_y-1)*s->mb_stride + (i == scan8[0]+3);
  1012. if(IS_INTERLACED(mb_types[topright_xy])){
  1013. #define SET_DIAG_MV(MV_OP, REF_OP, X4, Y4)\
  1014. const int x4 = X4, y4 = Y4;\
  1015. const int mb_type = mb_types[(x4>>2)+(y4>>2)*s->mb_stride];\
  1016. if(!USES_LIST(mb_type,list) && !IS_8X8(mb_type))\
  1017. return LIST_NOT_USED;\
  1018. mv = s->current_picture_ptr->motion_val[list][x4 + y4*h->b_stride];\
  1019. h->mv_cache[list][scan8[0]-2][0] = mv[0];\
  1020. h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
  1021. return s->current_picture_ptr->ref_index[list][(x4>>1) + (y4>>1)*h->b8_stride] REF_OP;
  1022. SET_DIAG_MV(*2, >>1, s->mb_x*4+(i&7)-4+part_width, s->mb_y*4-1);
  1023. }
  1024. }
  1025. if(topright_ref == PART_NOT_AVAILABLE
  1026. && ((s->mb_y&1) || i >= scan8[0]+8) && (i&7)==4
  1027. && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
  1028. if(!MB_FIELD
  1029. && IS_INTERLACED(mb_types[h->left_mb_xy[0]])){
  1030. SET_DIAG_MV(*2, >>1, s->mb_x*4-1, (s->mb_y|1)*4+(s->mb_y&1)*2+(i>>4)-1);
  1031. }
  1032. if(MB_FIELD
  1033. && !IS_INTERLACED(mb_types[h->left_mb_xy[0]])
  1034. && i >= scan8[0]+8){
  1035. // leftshift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's ok.
  1036. SET_DIAG_MV(>>1, <<1, s->mb_x*4-1, (s->mb_y&~1)*4 - 1 + ((i-scan8[0])>>3)*2);
  1037. }
  1038. }
  1039. #undef SET_DIAG_MV
  1040. }
  1041. if(topright_ref != PART_NOT_AVAILABLE){
  1042. *C= h->mv_cache[list][ i - 8 + part_width ];
  1043. return topright_ref;
  1044. }else{
  1045. tprintf("topright MV not available\n");
  1046. *C= h->mv_cache[list][ i - 8 - 1 ];
  1047. return h->ref_cache[list][ i - 8 - 1 ];
  1048. }
  1049. }
  1050. /**
  1051. * gets the predicted MV.
  1052. * @param n the block index
  1053. * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
  1054. * @param mx the x component of the predicted motion vector
  1055. * @param my the y component of the predicted motion vector
  1056. */
  1057. static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
  1058. const int index8= scan8[n];
  1059. const int top_ref= h->ref_cache[list][ index8 - 8 ];
  1060. const int left_ref= h->ref_cache[list][ index8 - 1 ];
  1061. const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
  1062. const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
  1063. const int16_t * C;
  1064. int diagonal_ref, match_count;
  1065. assert(part_width==1 || part_width==2 || part_width==4);
  1066. /* mv_cache
  1067. B . . A T T T T
  1068. U . . L . . , .
  1069. U . . L . . . .
  1070. U . . L . . , .
  1071. . . . L . . . .
  1072. */
  1073. diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
  1074. match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
  1075. tprintf("pred_motion match_count=%d\n", match_count);
  1076. if(match_count > 1){ //most common
  1077. *mx= mid_pred(A[0], B[0], C[0]);
  1078. *my= mid_pred(A[1], B[1], C[1]);
  1079. }else if(match_count==1){
  1080. if(left_ref==ref){
  1081. *mx= A[0];
  1082. *my= A[1];
  1083. }else if(top_ref==ref){
  1084. *mx= B[0];
  1085. *my= B[1];
  1086. }else{
  1087. *mx= C[0];
  1088. *my= C[1];
  1089. }
  1090. }else{
  1091. if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
  1092. *mx= A[0];
  1093. *my= A[1];
  1094. }else{
  1095. *mx= mid_pred(A[0], B[0], C[0]);
  1096. *my= mid_pred(A[1], B[1], C[1]);
  1097. }
  1098. }
  1099. tprintf("pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], diagonal_ref, C[0], C[1], left_ref, A[0], A[1], ref, *mx, *my, h->s.mb_x, h->s.mb_y, n, list);
  1100. }
  1101. /**
  1102. * gets the directionally predicted 16x8 MV.
  1103. * @param n the block index
  1104. * @param mx the x component of the predicted motion vector
  1105. * @param my the y component of the predicted motion vector
  1106. */
  1107. static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  1108. if(n==0){
  1109. const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
  1110. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  1111. tprintf("pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], h->s.mb_x, h->s.mb_y, n, list);
  1112. if(top_ref == ref){
  1113. *mx= B[0];
  1114. *my= B[1];
  1115. return;
  1116. }
  1117. }else{
  1118. const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
  1119. const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
  1120. tprintf("pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
  1121. if(left_ref == ref){
  1122. *mx= A[0];
  1123. *my= A[1];
  1124. return;
  1125. }
  1126. }
  1127. //RARE
  1128. pred_motion(h, n, 4, list, ref, mx, my);
  1129. }
  1130. /**
  1131. * gets the directionally predicted 8x16 MV.
  1132. * @param n the block index
  1133. * @param mx the x component of the predicted motion vector
  1134. * @param my the y component of the predicted motion vector
  1135. */
  1136. static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  1137. if(n==0){
  1138. const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
  1139. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  1140. tprintf("pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
  1141. if(left_ref == ref){
  1142. *mx= A[0];
  1143. *my= A[1];
  1144. return;
  1145. }
  1146. }else{
  1147. const int16_t * C;
  1148. int diagonal_ref;
  1149. diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
  1150. tprintf("pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", diagonal_ref, C[0], C[1], h->s.mb_x, h->s.mb_y, n, list);
  1151. if(diagonal_ref == ref){
  1152. *mx= C[0];
  1153. *my= C[1];
  1154. return;
  1155. }
  1156. }
  1157. //RARE
  1158. pred_motion(h, n, 2, list, ref, mx, my);
  1159. }
  1160. static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
  1161. const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
  1162. const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
  1163. tprintf("pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
  1164. if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
  1165. || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
  1166. || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
  1167. *mx = *my = 0;
  1168. return;
  1169. }
  1170. pred_motion(h, 0, 4, 0, 0, mx, my);
  1171. return;
  1172. }
  1173. static inline void direct_dist_scale_factor(H264Context * const h){
  1174. const int poc = h->s.current_picture_ptr->poc;
  1175. const int poc1 = h->ref_list[1][0].poc;
  1176. int i;
  1177. for(i=0; i<h->ref_count[0]; i++){
  1178. int poc0 = h->ref_list[0][i].poc;
  1179. int td = clip(poc1 - poc0, -128, 127);
  1180. if(td == 0 /* FIXME || pic0 is a long-term ref */){
  1181. h->dist_scale_factor[i] = 256;
  1182. }else{
  1183. int tb = clip(poc - poc0, -128, 127);
  1184. int tx = (16384 + (FFABS(td) >> 1)) / td;
  1185. h->dist_scale_factor[i] = clip((tb*tx + 32) >> 6, -1024, 1023);
  1186. }
  1187. }
  1188. if(FRAME_MBAFF){
  1189. for(i=0; i<h->ref_count[0]; i++){
  1190. h->dist_scale_factor_field[2*i] =
  1191. h->dist_scale_factor_field[2*i+1] = h->dist_scale_factor[i];
  1192. }
  1193. }
  1194. }
  1195. static inline void direct_ref_list_init(H264Context * const h){
  1196. MpegEncContext * const s = &h->s;
  1197. Picture * const ref1 = &h->ref_list[1][0];
  1198. Picture * const cur = s->current_picture_ptr;
  1199. int list, i, j;
  1200. if(cur->pict_type == I_TYPE)
  1201. cur->ref_count[0] = 0;
  1202. if(cur->pict_type != B_TYPE)
  1203. cur->ref_count[1] = 0;
  1204. for(list=0; list<2; list++){
  1205. cur->ref_count[list] = h->ref_count[list];
  1206. for(j=0; j<h->ref_count[list]; j++)
  1207. cur->ref_poc[list][j] = h->ref_list[list][j].poc;
  1208. }
  1209. if(cur->pict_type != B_TYPE || h->direct_spatial_mv_pred)
  1210. return;
  1211. for(list=0; list<2; list++){
  1212. for(i=0; i<ref1->ref_count[list]; i++){
  1213. const int poc = ref1->ref_poc[list][i];
  1214. h->map_col_to_list0[list][i] = 0; /* bogus; fills in for missing frames */
  1215. for(j=0; j<h->ref_count[list]; j++)
  1216. if(h->ref_list[list][j].poc == poc){
  1217. h->map_col_to_list0[list][i] = j;
  1218. break;
  1219. }
  1220. }
  1221. }
  1222. if(FRAME_MBAFF){
  1223. for(list=0; list<2; list++){
  1224. for(i=0; i<ref1->ref_count[list]; i++){
  1225. j = h->map_col_to_list0[list][i];
  1226. h->map_col_to_list0_field[list][2*i] = 2*j;
  1227. h->map_col_to_list0_field[list][2*i+1] = 2*j+1;
  1228. }
  1229. }
  1230. }
  1231. }
  1232. static inline void pred_direct_motion(H264Context * const h, int *mb_type){
  1233. MpegEncContext * const s = &h->s;
  1234. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  1235. const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1236. const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1237. const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
  1238. const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
  1239. const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[1][b4_xy];
  1240. const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
  1241. const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy];
  1242. const int is_b8x8 = IS_8X8(*mb_type);
  1243. int sub_mb_type;
  1244. int i8, i4;
  1245. #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
  1246. if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
  1247. /* FIXME save sub mb types from previous frames (or derive from MVs)
  1248. * so we know exactly what block size to use */
  1249. sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  1250. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  1251. }else if(!is_b8x8 && (mb_type_col & MB_TYPE_16x16_OR_INTRA)){
  1252. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  1253. *mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
  1254. }else{
  1255. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  1256. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  1257. }
  1258. if(!is_b8x8)
  1259. *mb_type |= MB_TYPE_DIRECT2;
  1260. if(MB_FIELD)
  1261. *mb_type |= MB_TYPE_INTERLACED;
  1262. tprintf("mb_type = %08x, sub_mb_type = %08x, is_b8x8 = %d, mb_type_col = %08x\n", *mb_type, sub_mb_type, is_b8x8, mb_type_col);
  1263. if(h->direct_spatial_mv_pred){
  1264. int ref[2];
  1265. int mv[2][2];
  1266. int list;
  1267. /* FIXME interlacing + spatial direct uses wrong colocated block positions */
  1268. /* ref = min(neighbors) */
  1269. for(list=0; list<2; list++){
  1270. int refa = h->ref_cache[list][scan8[0] - 1];
  1271. int refb = h->ref_cache[list][scan8[0] - 8];
  1272. int refc = h->ref_cache[list][scan8[0] - 8 + 4];
  1273. if(refc == -2)
  1274. refc = h->ref_cache[list][scan8[0] - 8 - 1];
  1275. ref[list] = refa;
  1276. if(ref[list] < 0 || (refb < ref[list] && refb >= 0))
  1277. ref[list] = refb;
  1278. if(ref[list] < 0 || (refc < ref[list] && refc >= 0))
  1279. ref[list] = refc;
  1280. if(ref[list] < 0)
  1281. ref[list] = -1;
  1282. }
  1283. if(ref[0] < 0 && ref[1] < 0){
  1284. ref[0] = ref[1] = 0;
  1285. mv[0][0] = mv[0][1] =
  1286. mv[1][0] = mv[1][1] = 0;
  1287. }else{
  1288. for(list=0; list<2; list++){
  1289. if(ref[list] >= 0)
  1290. pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
  1291. else
  1292. mv[list][0] = mv[list][1] = 0;
  1293. }
  1294. }
  1295. if(ref[1] < 0){
  1296. *mb_type &= ~MB_TYPE_P0L1;
  1297. sub_mb_type &= ~MB_TYPE_P0L1;
  1298. }else if(ref[0] < 0){
  1299. *mb_type &= ~MB_TYPE_P0L0;
  1300. sub_mb_type &= ~MB_TYPE_P0L0;
  1301. }
  1302. if(IS_16X16(*mb_type)){
  1303. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  1304. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  1305. if(!IS_INTRA(mb_type_col)
  1306. && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
  1307. || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
  1308. && (h->x264_build>33 || !h->x264_build)))){
  1309. if(ref[0] > 0)
  1310. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1311. else
  1312. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  1313. if(ref[1] > 0)
  1314. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1315. else
  1316. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  1317. }else{
  1318. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1319. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1320. }
  1321. }else{
  1322. for(i8=0; i8<4; i8++){
  1323. const int x8 = i8&1;
  1324. const int y8 = i8>>1;
  1325. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1326. continue;
  1327. h->sub_mb_type[i8] = sub_mb_type;
  1328. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1329. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1330. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  1331. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  1332. /* col_zero_flag */
  1333. if(!IS_INTRA(mb_type_col) && ( l1ref0[x8 + y8*h->b8_stride] == 0
  1334. || (l1ref0[x8 + y8*h->b8_stride] < 0 && l1ref1[x8 + y8*h->b8_stride] == 0
  1335. && (h->x264_build>33 || !h->x264_build)))){
  1336. const int16_t (*l1mv)[2]= l1ref0[x8 + y8*h->b8_stride] == 0 ? l1mv0 : l1mv1;
  1337. if(IS_SUB_8X8(sub_mb_type)){
  1338. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1339. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  1340. if(ref[0] == 0)
  1341. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1342. if(ref[1] == 0)
  1343. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1344. }
  1345. }else
  1346. for(i4=0; i4<4; i4++){
  1347. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1348. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  1349. if(ref[0] == 0)
  1350. *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
  1351. if(ref[1] == 0)
  1352. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
  1353. }
  1354. }
  1355. }
  1356. }
  1357. }
  1358. }else{ /* direct temporal mv pred */
  1359. const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
  1360. const int *dist_scale_factor = h->dist_scale_factor;
  1361. if(FRAME_MBAFF){
  1362. if(IS_INTERLACED(*mb_type)){
  1363. map_col_to_list0[0] = h->map_col_to_list0_field[0];
  1364. map_col_to_list0[1] = h->map_col_to_list0_field[1];
  1365. dist_scale_factor = h->dist_scale_factor_field;
  1366. }
  1367. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col)){
  1368. /* FIXME assumes direct_8x8_inference == 1 */
  1369. const int pair_xy = s->mb_x + (s->mb_y&~1)*s->mb_stride;
  1370. int mb_types_col[2];
  1371. int y_shift;
  1372. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1
  1373. | (is_b8x8 ? 0 : MB_TYPE_DIRECT2)
  1374. | (*mb_type & MB_TYPE_INTERLACED);
  1375. sub_mb_type = MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_16x16;
  1376. if(IS_INTERLACED(*mb_type)){
  1377. /* frame to field scaling */
  1378. mb_types_col[0] = h->ref_list[1][0].mb_type[pair_xy];
  1379. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1380. if(s->mb_y&1){
  1381. l1ref0 -= 2*h->b8_stride;
  1382. l1ref1 -= 2*h->b8_stride;
  1383. l1mv0 -= 4*h->b_stride;
  1384. l1mv1 -= 4*h->b_stride;
  1385. }
  1386. y_shift = 0;
  1387. if( (mb_types_col[0] & MB_TYPE_16x16_OR_INTRA)
  1388. && (mb_types_col[1] & MB_TYPE_16x16_OR_INTRA)
  1389. && !is_b8x8)
  1390. *mb_type |= MB_TYPE_16x8;
  1391. else
  1392. *mb_type |= MB_TYPE_8x8;
  1393. }else{
  1394. /* field to frame scaling */
  1395. /* col_mb_y = (mb_y&~1) + (topAbsDiffPOC < bottomAbsDiffPOC ? 0 : 1)
  1396. * but in MBAFF, top and bottom POC are equal */
  1397. int dy = (s->mb_y&1) ? 1 : 2;
  1398. mb_types_col[0] =
  1399. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1400. l1ref0 += dy*h->b8_stride;
  1401. l1ref1 += dy*h->b8_stride;
  1402. l1mv0 += 2*dy*h->b_stride;
  1403. l1mv1 += 2*dy*h->b_stride;
  1404. y_shift = 2;
  1405. if((mb_types_col[0] & (MB_TYPE_16x16_OR_INTRA|MB_TYPE_16x8))
  1406. && !is_b8x8)
  1407. *mb_type |= MB_TYPE_16x16;
  1408. else
  1409. *mb_type |= MB_TYPE_8x8;
  1410. }
  1411. for(i8=0; i8<4; i8++){
  1412. const int x8 = i8&1;
  1413. const int y8 = i8>>1;
  1414. int ref0, scale;
  1415. const int16_t (*l1mv)[2]= l1mv0;
  1416. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1417. continue;
  1418. h->sub_mb_type[i8] = sub_mb_type;
  1419. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1420. if(IS_INTRA(mb_types_col[y8])){
  1421. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1422. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1423. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1424. continue;
  1425. }
  1426. ref0 = l1ref0[x8 + (y8*2>>y_shift)*h->b8_stride];
  1427. if(ref0 >= 0)
  1428. ref0 = map_col_to_list0[0][ref0*2>>y_shift];
  1429. else{
  1430. ref0 = map_col_to_list0[1][l1ref1[x8 + (y8*2>>y_shift)*h->b8_stride]*2>>y_shift];
  1431. l1mv= l1mv1;
  1432. }
  1433. scale = dist_scale_factor[ref0];
  1434. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1435. {
  1436. const int16_t *mv_col = l1mv[x8*3 + (y8*6>>y_shift)*h->b_stride];
  1437. int my_col = (mv_col[1]<<y_shift)/2;
  1438. int mx = (scale * mv_col[0] + 128) >> 8;
  1439. int my = (scale * my_col + 128) >> 8;
  1440. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1441. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
  1442. }
  1443. }
  1444. return;
  1445. }
  1446. }
  1447. /* one-to-one mv scaling */
  1448. if(IS_16X16(*mb_type)){
  1449. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  1450. if(IS_INTRA(mb_type_col)){
  1451. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  1452. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  1453. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  1454. }else{
  1455. const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0]]
  1456. : map_col_to_list0[1][l1ref1[0]];
  1457. const int scale = dist_scale_factor[ref0];
  1458. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  1459. int mv_l0[2];
  1460. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1461. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1462. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref0, 1);
  1463. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0],mv_l0[1]), 4);
  1464. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]), 4);
  1465. }
  1466. }else{
  1467. for(i8=0; i8<4; i8++){
  1468. const int x8 = i8&1;
  1469. const int y8 = i8>>1;
  1470. int ref0, scale;
  1471. const int16_t (*l1mv)[2]= l1mv0;
  1472. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1473. continue;
  1474. h->sub_mb_type[i8] = sub_mb_type;
  1475. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1476. if(IS_INTRA(mb_type_col)){
  1477. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1478. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1479. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1480. continue;
  1481. }
  1482. ref0 = l1ref0[x8 + y8*h->b8_stride];
  1483. if(ref0 >= 0)
  1484. ref0 = map_col_to_list0[0][ref0];
  1485. else{
  1486. ref0 = map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]];
  1487. l1mv= l1mv1;
  1488. }
  1489. scale = dist_scale_factor[ref0];
  1490. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1491. if(IS_SUB_8X8(sub_mb_type)){
  1492. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1493. int mx = (scale * mv_col[0] + 128) >> 8;
  1494. int my = (scale * mv_col[1] + 128) >> 8;
  1495. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1496. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
  1497. }else
  1498. for(i4=0; i4<4; i4++){
  1499. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1500. int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
  1501. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1502. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1503. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
  1504. pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  1505. }
  1506. }
  1507. }
  1508. }
  1509. }
  1510. static inline void write_back_motion(H264Context *h, int mb_type){
  1511. MpegEncContext * const s = &h->s;
  1512. const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1513. const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1514. int list;
  1515. if(!USES_LIST(mb_type, 0))
  1516. fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, h->b8_stride, (uint8_t)LIST_NOT_USED, 1);
  1517. for(list=0; list<2; list++){
  1518. int y;
  1519. if(!USES_LIST(mb_type, list))
  1520. continue;
  1521. for(y=0; y<4; y++){
  1522. *(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];
  1523. *(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];
  1524. }
  1525. if( h->pps.cabac ) {
  1526. if(IS_SKIP(mb_type))
  1527. fill_rectangle(h->mvd_table[list][b_xy], 4, 4, h->b_stride, 0, 4);
  1528. else
  1529. for(y=0; y<4; y++){
  1530. *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
  1531. *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
  1532. }
  1533. }
  1534. {
  1535. int8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
  1536. ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]];
  1537. ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]];
  1538. ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]];
  1539. ref_index[1+1*h->b8_stride]= h->ref_cache[list][scan8[12]];
  1540. }
  1541. }
  1542. if(h->slice_type == B_TYPE && h->pps.cabac){
  1543. if(IS_8X8(mb_type)){
  1544. uint8_t *direct_table = &h->direct_table[b8_xy];
  1545. direct_table[1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
  1546. direct_table[0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
  1547. direct_table[1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
  1548. }
  1549. }
  1550. }
  1551. /**
  1552. * Decodes a network abstraction layer unit.
  1553. * @param consumed is the number of bytes used as input
  1554. * @param length is the length of the array
  1555. * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp tailing?
  1556. * @returns decoded bytes, might be src+1 if no escapes
  1557. */
  1558. static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){
  1559. int i, si, di;
  1560. uint8_t *dst;
  1561. // src[0]&0x80; //forbidden bit
  1562. h->nal_ref_idc= src[0]>>5;
  1563. h->nal_unit_type= src[0]&0x1F;
  1564. src++; length--;
  1565. #if 0
  1566. for(i=0; i<length; i++)
  1567. printf("%2X ", src[i]);
  1568. #endif
  1569. for(i=0; i+1<length; i+=2){
  1570. if(src[i]) continue;
  1571. if(i>0 && src[i-1]==0) i--;
  1572. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  1573. if(src[i+2]!=3){
  1574. /* startcode, so we must be past the end */
  1575. length=i;
  1576. }
  1577. break;
  1578. }
  1579. }
  1580. if(i>=length-1){ //no escaped 0
  1581. *dst_length= length;
  1582. *consumed= length+1; //+1 for the header
  1583. return src;
  1584. }
  1585. h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length);
  1586. dst= h->rbsp_buffer;
  1587. //printf("decoding esc\n");
  1588. si=di=0;
  1589. while(si<length){
  1590. //remove escapes (very rare 1:2^22)
  1591. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  1592. if(src[si+2]==3){ //escape
  1593. dst[di++]= 0;
  1594. dst[di++]= 0;
  1595. si+=3;
  1596. continue;
  1597. }else //next start code
  1598. break;
  1599. }
  1600. dst[di++]= src[si++];
  1601. }
  1602. *dst_length= di;
  1603. *consumed= si + 1;//+1 for the header
  1604. //FIXME store exact number of bits in the getbitcontext (its needed for decoding)
  1605. return dst;
  1606. }
  1607. /**
  1608. * identifies the exact end of the bitstream
  1609. * @return the length of the trailing, or 0 if damaged
  1610. */
  1611. static int decode_rbsp_trailing(uint8_t *src){
  1612. int v= *src;
  1613. int r;
  1614. tprintf("rbsp trailing %X\n", v);
  1615. for(r=1; r<9; r++){
  1616. if(v&1) return r;
  1617. v>>=1;
  1618. }
  1619. return 0;
  1620. }
  1621. /**
  1622. * idct tranforms the 16 dc values and dequantize them.
  1623. * @param qp quantization parameter
  1624. */
  1625. static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1626. #define stride 16
  1627. int i;
  1628. int temp[16]; //FIXME check if this is a good idea
  1629. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1630. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1631. //memset(block, 64, 2*256);
  1632. //return;
  1633. for(i=0; i<4; i++){
  1634. const int offset= y_offset[i];
  1635. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1636. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1637. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1638. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1639. temp[4*i+0]= z0+z3;
  1640. temp[4*i+1]= z1+z2;
  1641. temp[4*i+2]= z1-z2;
  1642. temp[4*i+3]= z0-z3;
  1643. }
  1644. for(i=0; i<4; i++){
  1645. const int offset= x_offset[i];
  1646. const int z0= temp[4*0+i] + temp[4*2+i];
  1647. const int z1= temp[4*0+i] - temp[4*2+i];
  1648. const int z2= temp[4*1+i] - temp[4*3+i];
  1649. const int z3= temp[4*1+i] + temp[4*3+i];
  1650. block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_resdual
  1651. block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
  1652. block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
  1653. block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
  1654. }
  1655. }
  1656. #if 0
  1657. /**
  1658. * dct tranforms the 16 dc values.
  1659. * @param qp quantization parameter ??? FIXME
  1660. */
  1661. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  1662. // const int qmul= dequant_coeff[qp][0];
  1663. int i;
  1664. int temp[16]; //FIXME check if this is a good idea
  1665. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1666. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1667. for(i=0; i<4; i++){
  1668. const int offset= y_offset[i];
  1669. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1670. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1671. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1672. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1673. temp[4*i+0]= z0+z3;
  1674. temp[4*i+1]= z1+z2;
  1675. temp[4*i+2]= z1-z2;
  1676. temp[4*i+3]= z0-z3;
  1677. }
  1678. for(i=0; i<4; i++){
  1679. const int offset= x_offset[i];
  1680. const int z0= temp[4*0+i] + temp[4*2+i];
  1681. const int z1= temp[4*0+i] - temp[4*2+i];
  1682. const int z2= temp[4*1+i] - temp[4*3+i];
  1683. const int z3= temp[4*1+i] + temp[4*3+i];
  1684. block[stride*0 +offset]= (z0 + z3)>>1;
  1685. block[stride*2 +offset]= (z1 + z2)>>1;
  1686. block[stride*8 +offset]= (z1 - z2)>>1;
  1687. block[stride*10+offset]= (z0 - z3)>>1;
  1688. }
  1689. }
  1690. #endif
  1691. #undef xStride
  1692. #undef stride
  1693. static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1694. const int stride= 16*2;
  1695. const int xStride= 16;
  1696. int a,b,c,d,e;
  1697. a= block[stride*0 + xStride*0];
  1698. b= block[stride*0 + xStride*1];
  1699. c= block[stride*1 + xStride*0];
  1700. d= block[stride*1 + xStride*1];
  1701. e= a-b;
  1702. a= a+b;
  1703. b= c-d;
  1704. c= c+d;
  1705. block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
  1706. block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
  1707. block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
  1708. block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
  1709. }
  1710. #if 0
  1711. static void chroma_dc_dct_c(DCTELEM *block){
  1712. const int stride= 16*2;
  1713. const int xStride= 16;
  1714. int a,b,c,d,e;
  1715. a= block[stride*0 + xStride*0];
  1716. b= block[stride*0 + xStride*1];
  1717. c= block[stride*1 + xStride*0];
  1718. d= block[stride*1 + xStride*1];
  1719. e= a-b;
  1720. a= a+b;
  1721. b= c-d;
  1722. c= c+d;
  1723. block[stride*0 + xStride*0]= (a+c);
  1724. block[stride*0 + xStride*1]= (e+b);
  1725. block[stride*1 + xStride*0]= (a-c);
  1726. block[stride*1 + xStride*1]= (e-b);
  1727. }
  1728. #endif
  1729. /**
  1730. * gets the chroma qp.
  1731. */
  1732. static inline int get_chroma_qp(int chroma_qp_index_offset, int qscale){
  1733. return chroma_qp[clip(qscale + chroma_qp_index_offset, 0, 51)];
  1734. }
  1735. //FIXME need to check that this doesnt overflow signed 32 bit for low qp, i am not sure, it's very close
  1736. //FIXME check that gcc inlines this (and optimizes intra & seperate_dc stuff away)
  1737. static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int seperate_dc){
  1738. int i;
  1739. const int * const quant_table= quant_coeff[qscale];
  1740. const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
  1741. const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
  1742. const unsigned int threshold2= (threshold1<<1);
  1743. int last_non_zero;
  1744. if(seperate_dc){
  1745. if(qscale<=18){
  1746. //avoid overflows
  1747. const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
  1748. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
  1749. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1750. int level= block[0]*quant_coeff[qscale+18][0];
  1751. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1752. if(level>0){
  1753. level= (dc_bias + level)>>(QUANT_SHIFT-2);
  1754. block[0]= level;
  1755. }else{
  1756. level= (dc_bias - level)>>(QUANT_SHIFT-2);
  1757. block[0]= -level;
  1758. }
  1759. // last_non_zero = i;
  1760. }else{
  1761. block[0]=0;
  1762. }
  1763. }else{
  1764. const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
  1765. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
  1766. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1767. int level= block[0]*quant_table[0];
  1768. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1769. if(level>0){
  1770. level= (dc_bias + level)>>(QUANT_SHIFT+1);
  1771. block[0]= level;
  1772. }else{
  1773. level= (dc_bias - level)>>(QUANT_SHIFT+1);
  1774. block[0]= -level;
  1775. }
  1776. // last_non_zero = i;
  1777. }else{
  1778. block[0]=0;
  1779. }
  1780. }
  1781. last_non_zero= 0;
  1782. i=1;
  1783. }else{
  1784. last_non_zero= -1;
  1785. i=0;
  1786. }
  1787. for(; i<16; i++){
  1788. const int j= scantable[i];
  1789. int level= block[j]*quant_table[j];
  1790. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  1791. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  1792. if(((unsigned)(level+threshold1))>threshold2){
  1793. if(level>0){
  1794. level= (bias + level)>>QUANT_SHIFT;
  1795. block[j]= level;
  1796. }else{
  1797. level= (bias - level)>>QUANT_SHIFT;
  1798. block[j]= -level;
  1799. }
  1800. last_non_zero = i;
  1801. }else{
  1802. block[j]=0;
  1803. }
  1804. }
  1805. return last_non_zero;
  1806. }
  1807. static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){
  1808. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1809. ((uint32_t*)(src+0*stride))[0]= a;
  1810. ((uint32_t*)(src+1*stride))[0]= a;
  1811. ((uint32_t*)(src+2*stride))[0]= a;
  1812. ((uint32_t*)(src+3*stride))[0]= a;
  1813. }
  1814. static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){
  1815. ((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101;
  1816. ((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101;
  1817. ((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101;
  1818. ((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101;
  1819. }
  1820. static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1821. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
  1822. + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
  1823. ((uint32_t*)(src+0*stride))[0]=
  1824. ((uint32_t*)(src+1*stride))[0]=
  1825. ((uint32_t*)(src+2*stride))[0]=
  1826. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1827. }
  1828. static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1829. const int dc= ( src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
  1830. ((uint32_t*)(src+0*stride))[0]=
  1831. ((uint32_t*)(src+1*stride))[0]=
  1832. ((uint32_t*)(src+2*stride))[0]=
  1833. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1834. }
  1835. static void pred4x4_top_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1836. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2;
  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]= dc* 0x01010101;
  1841. }
  1842. static void pred4x4_128_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1843. ((uint32_t*)(src+0*stride))[0]=
  1844. ((uint32_t*)(src+1*stride))[0]=
  1845. ((uint32_t*)(src+2*stride))[0]=
  1846. ((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U;
  1847. }
  1848. #define LOAD_TOP_RIGHT_EDGE\
  1849. const int t4= topright[0];\
  1850. const int t5= topright[1];\
  1851. const int t6= topright[2];\
  1852. const int t7= topright[3];\
  1853. #define LOAD_LEFT_EDGE\
  1854. const int l0= src[-1+0*stride];\
  1855. const int l1= src[-1+1*stride];\
  1856. const int l2= src[-1+2*stride];\
  1857. const int l3= src[-1+3*stride];\
  1858. #define LOAD_TOP_EDGE\
  1859. const int t0= src[ 0-1*stride];\
  1860. const int t1= src[ 1-1*stride];\
  1861. const int t2= src[ 2-1*stride];\
  1862. const int t3= src[ 3-1*stride];\
  1863. static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){
  1864. const int lt= src[-1-1*stride];
  1865. LOAD_TOP_EDGE
  1866. LOAD_LEFT_EDGE
  1867. src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
  1868. src[0+2*stride]=
  1869. src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
  1870. src[0+1*stride]=
  1871. src[1+2*stride]=
  1872. src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
  1873. src[0+0*stride]=
  1874. src[1+1*stride]=
  1875. src[2+2*stride]=
  1876. src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1877. src[1+0*stride]=
  1878. src[2+1*stride]=
  1879. src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1880. src[2+0*stride]=
  1881. src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1882. src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1883. }
  1884. static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){
  1885. LOAD_TOP_EDGE
  1886. LOAD_TOP_RIGHT_EDGE
  1887. // LOAD_LEFT_EDGE
  1888. src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
  1889. src[1+0*stride]=
  1890. src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
  1891. src[2+0*stride]=
  1892. src[1+1*stride]=
  1893. src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
  1894. src[3+0*stride]=
  1895. src[2+1*stride]=
  1896. src[1+2*stride]=
  1897. src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
  1898. src[3+1*stride]=
  1899. src[2+2*stride]=
  1900. src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
  1901. src[3+2*stride]=
  1902. src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
  1903. src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
  1904. }
  1905. static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
  1906. const int lt= src[-1-1*stride];
  1907. LOAD_TOP_EDGE
  1908. LOAD_LEFT_EDGE
  1909. const __attribute__((unused)) int unu= l3;
  1910. src[0+0*stride]=
  1911. src[1+2*stride]=(lt + t0 + 1)>>1;
  1912. src[1+0*stride]=
  1913. src[2+2*stride]=(t0 + t1 + 1)>>1;
  1914. src[2+0*stride]=
  1915. src[3+2*stride]=(t1 + t2 + 1)>>1;
  1916. src[3+0*stride]=(t2 + t3 + 1)>>1;
  1917. src[0+1*stride]=
  1918. src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1919. src[1+1*stride]=
  1920. src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1921. src[2+1*stride]=
  1922. src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1923. src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1924. src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1925. src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1926. }
  1927. static void pred4x4_vertical_left_c(uint8_t *src, uint8_t *topright, int stride){
  1928. LOAD_TOP_EDGE
  1929. LOAD_TOP_RIGHT_EDGE
  1930. const __attribute__((unused)) int unu= t7;
  1931. src[0+0*stride]=(t0 + t1 + 1)>>1;
  1932. src[1+0*stride]=
  1933. src[0+2*stride]=(t1 + t2 + 1)>>1;
  1934. src[2+0*stride]=
  1935. src[1+2*stride]=(t2 + t3 + 1)>>1;
  1936. src[3+0*stride]=
  1937. src[2+2*stride]=(t3 + t4+ 1)>>1;
  1938. src[3+2*stride]=(t4 + t5+ 1)>>1;
  1939. src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1940. src[1+1*stride]=
  1941. src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1942. src[2+1*stride]=
  1943. src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
  1944. src[3+1*stride]=
  1945. src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
  1946. src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
  1947. }
  1948. static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){
  1949. LOAD_LEFT_EDGE
  1950. src[0+0*stride]=(l0 + l1 + 1)>>1;
  1951. src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1952. src[2+0*stride]=
  1953. src[0+1*stride]=(l1 + l2 + 1)>>1;
  1954. src[3+0*stride]=
  1955. src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1956. src[2+1*stride]=
  1957. src[0+2*stride]=(l2 + l3 + 1)>>1;
  1958. src[3+1*stride]=
  1959. src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
  1960. src[3+2*stride]=
  1961. src[1+3*stride]=
  1962. src[0+3*stride]=
  1963. src[2+2*stride]=
  1964. src[2+3*stride]=
  1965. src[3+3*stride]=l3;
  1966. }
  1967. static void pred4x4_horizontal_down_c(uint8_t *src, uint8_t *topright, int stride){
  1968. const int lt= src[-1-1*stride];
  1969. LOAD_TOP_EDGE
  1970. LOAD_LEFT_EDGE
  1971. const __attribute__((unused)) int unu= t3;
  1972. src[0+0*stride]=
  1973. src[2+1*stride]=(lt + l0 + 1)>>1;
  1974. src[1+0*stride]=
  1975. src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1976. src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1977. src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1978. src[0+1*stride]=
  1979. src[2+2*stride]=(l0 + l1 + 1)>>1;
  1980. src[1+1*stride]=
  1981. src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1982. src[0+2*stride]=
  1983. src[2+3*stride]=(l1 + l2+ 1)>>1;
  1984. src[1+2*stride]=
  1985. src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1986. src[0+3*stride]=(l2 + l3 + 1)>>1;
  1987. src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1988. }
  1989. void ff_pred16x16_vertical_c(uint8_t *src, int stride){
  1990. int i;
  1991. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1992. const uint32_t b= ((uint32_t*)(src-stride))[1];
  1993. const uint32_t c= ((uint32_t*)(src-stride))[2];
  1994. const uint32_t d= ((uint32_t*)(src-stride))[3];
  1995. for(i=0; i<16; i++){
  1996. ((uint32_t*)(src+i*stride))[0]= a;
  1997. ((uint32_t*)(src+i*stride))[1]= b;
  1998. ((uint32_t*)(src+i*stride))[2]= c;
  1999. ((uint32_t*)(src+i*stride))[3]= d;
  2000. }
  2001. }
  2002. void ff_pred16x16_horizontal_c(uint8_t *src, int stride){
  2003. int i;
  2004. for(i=0; i<16; i++){
  2005. ((uint32_t*)(src+i*stride))[0]=
  2006. ((uint32_t*)(src+i*stride))[1]=
  2007. ((uint32_t*)(src+i*stride))[2]=
  2008. ((uint32_t*)(src+i*stride))[3]= src[-1+i*stride]*0x01010101;
  2009. }
  2010. }
  2011. void ff_pred16x16_dc_c(uint8_t *src, int stride){
  2012. int i, dc=0;
  2013. for(i=0;i<16; i++){
  2014. dc+= src[-1+i*stride];
  2015. }
  2016. for(i=0;i<16; i++){
  2017. dc+= src[i-stride];
  2018. }
  2019. dc= 0x01010101*((dc + 16)>>5);
  2020. for(i=0; i<16; i++){
  2021. ((uint32_t*)(src+i*stride))[0]=
  2022. ((uint32_t*)(src+i*stride))[1]=
  2023. ((uint32_t*)(src+i*stride))[2]=
  2024. ((uint32_t*)(src+i*stride))[3]= dc;
  2025. }
  2026. }
  2027. static void pred16x16_left_dc_c(uint8_t *src, int stride){
  2028. int i, dc=0;
  2029. for(i=0;i<16; i++){
  2030. dc+= src[-1+i*stride];
  2031. }
  2032. dc= 0x01010101*((dc + 8)>>4);
  2033. for(i=0; i<16; i++){
  2034. ((uint32_t*)(src+i*stride))[0]=
  2035. ((uint32_t*)(src+i*stride))[1]=
  2036. ((uint32_t*)(src+i*stride))[2]=
  2037. ((uint32_t*)(src+i*stride))[3]= dc;
  2038. }
  2039. }
  2040. static void pred16x16_top_dc_c(uint8_t *src, int stride){
  2041. int i, dc=0;
  2042. for(i=0;i<16; i++){
  2043. dc+= src[i-stride];
  2044. }
  2045. dc= 0x01010101*((dc + 8)>>4);
  2046. for(i=0; i<16; i++){
  2047. ((uint32_t*)(src+i*stride))[0]=
  2048. ((uint32_t*)(src+i*stride))[1]=
  2049. ((uint32_t*)(src+i*stride))[2]=
  2050. ((uint32_t*)(src+i*stride))[3]= dc;
  2051. }
  2052. }
  2053. void ff_pred16x16_128_dc_c(uint8_t *src, int stride){
  2054. int i;
  2055. for(i=0; i<16; i++){
  2056. ((uint32_t*)(src+i*stride))[0]=
  2057. ((uint32_t*)(src+i*stride))[1]=
  2058. ((uint32_t*)(src+i*stride))[2]=
  2059. ((uint32_t*)(src+i*stride))[3]= 0x01010101U*128U;
  2060. }
  2061. }
  2062. static inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){
  2063. int i, j, k;
  2064. int a;
  2065. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  2066. const uint8_t * const src0 = src+7-stride;
  2067. const uint8_t *src1 = src+8*stride-1;
  2068. const uint8_t *src2 = src1-2*stride; // == src+6*stride-1;
  2069. int H = src0[1] - src0[-1];
  2070. int V = src1[0] - src2[ 0];
  2071. for(k=2; k<=8; ++k) {
  2072. src1 += stride; src2 -= stride;
  2073. H += k*(src0[k] - src0[-k]);
  2074. V += k*(src1[0] - src2[ 0]);
  2075. }
  2076. if(svq3){
  2077. H = ( 5*(H/4) ) / 16;
  2078. V = ( 5*(V/4) ) / 16;
  2079. /* required for 100% accuracy */
  2080. i = H; H = V; V = i;
  2081. }else{
  2082. H = ( 5*H+32 ) >> 6;
  2083. V = ( 5*V+32 ) >> 6;
  2084. }
  2085. a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
  2086. for(j=16; j>0; --j) {
  2087. int b = a;
  2088. a += V;
  2089. for(i=-16; i<0; i+=4) {
  2090. src[16+i] = cm[ (b ) >> 5 ];
  2091. src[17+i] = cm[ (b+ H) >> 5 ];
  2092. src[18+i] = cm[ (b+2*H) >> 5 ];
  2093. src[19+i] = cm[ (b+3*H) >> 5 ];
  2094. b += 4*H;
  2095. }
  2096. src += stride;
  2097. }
  2098. }
  2099. void ff_pred16x16_plane_c(uint8_t *src, int stride){
  2100. pred16x16_plane_compat_c(src, stride, 0);
  2101. }
  2102. void ff_pred8x8_vertical_c(uint8_t *src, int stride){
  2103. int i;
  2104. const uint32_t a= ((uint32_t*)(src-stride))[0];
  2105. const uint32_t b= ((uint32_t*)(src-stride))[1];
  2106. for(i=0; i<8; i++){
  2107. ((uint32_t*)(src+i*stride))[0]= a;
  2108. ((uint32_t*)(src+i*stride))[1]= b;
  2109. }
  2110. }
  2111. void ff_pred8x8_horizontal_c(uint8_t *src, int stride){
  2112. int i;
  2113. for(i=0; i<8; i++){
  2114. ((uint32_t*)(src+i*stride))[0]=
  2115. ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101;
  2116. }
  2117. }
  2118. void ff_pred8x8_128_dc_c(uint8_t *src, int stride){
  2119. int i;
  2120. for(i=0; i<8; i++){
  2121. ((uint32_t*)(src+i*stride))[0]=
  2122. ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
  2123. }
  2124. }
  2125. static void pred8x8_left_dc_c(uint8_t *src, int stride){
  2126. int i;
  2127. int dc0, dc2;
  2128. dc0=dc2=0;
  2129. for(i=0;i<4; i++){
  2130. dc0+= src[-1+i*stride];
  2131. dc2+= src[-1+(i+4)*stride];
  2132. }
  2133. dc0= 0x01010101*((dc0 + 2)>>2);
  2134. dc2= 0x01010101*((dc2 + 2)>>2);
  2135. for(i=0; i<4; i++){
  2136. ((uint32_t*)(src+i*stride))[0]=
  2137. ((uint32_t*)(src+i*stride))[1]= dc0;
  2138. }
  2139. for(i=4; i<8; i++){
  2140. ((uint32_t*)(src+i*stride))[0]=
  2141. ((uint32_t*)(src+i*stride))[1]= dc2;
  2142. }
  2143. }
  2144. static void pred8x8_top_dc_c(uint8_t *src, int stride){
  2145. int i;
  2146. int dc0, dc1;
  2147. dc0=dc1=0;
  2148. for(i=0;i<4; i++){
  2149. dc0+= src[i-stride];
  2150. dc1+= src[4+i-stride];
  2151. }
  2152. dc0= 0x01010101*((dc0 + 2)>>2);
  2153. dc1= 0x01010101*((dc1 + 2)>>2);
  2154. for(i=0; i<4; i++){
  2155. ((uint32_t*)(src+i*stride))[0]= dc0;
  2156. ((uint32_t*)(src+i*stride))[1]= dc1;
  2157. }
  2158. for(i=4; i<8; i++){
  2159. ((uint32_t*)(src+i*stride))[0]= dc0;
  2160. ((uint32_t*)(src+i*stride))[1]= dc1;
  2161. }
  2162. }
  2163. void ff_pred8x8_dc_c(uint8_t *src, int stride){
  2164. int i;
  2165. int dc0, dc1, dc2, dc3;
  2166. dc0=dc1=dc2=0;
  2167. for(i=0;i<4; i++){
  2168. dc0+= src[-1+i*stride] + src[i-stride];
  2169. dc1+= src[4+i-stride];
  2170. dc2+= src[-1+(i+4)*stride];
  2171. }
  2172. dc3= 0x01010101*((dc1 + dc2 + 4)>>3);
  2173. dc0= 0x01010101*((dc0 + 4)>>3);
  2174. dc1= 0x01010101*((dc1 + 2)>>2);
  2175. dc2= 0x01010101*((dc2 + 2)>>2);
  2176. for(i=0; i<4; i++){
  2177. ((uint32_t*)(src+i*stride))[0]= dc0;
  2178. ((uint32_t*)(src+i*stride))[1]= dc1;
  2179. }
  2180. for(i=4; i<8; i++){
  2181. ((uint32_t*)(src+i*stride))[0]= dc2;
  2182. ((uint32_t*)(src+i*stride))[1]= dc3;
  2183. }
  2184. }
  2185. void ff_pred8x8_plane_c(uint8_t *src, int stride){
  2186. int j, k;
  2187. int a;
  2188. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  2189. const uint8_t * const src0 = src+3-stride;
  2190. const uint8_t *src1 = src+4*stride-1;
  2191. const uint8_t *src2 = src1-2*stride; // == src+2*stride-1;
  2192. int H = src0[1] - src0[-1];
  2193. int V = src1[0] - src2[ 0];
  2194. for(k=2; k<=4; ++k) {
  2195. src1 += stride; src2 -= stride;
  2196. H += k*(src0[k] - src0[-k]);
  2197. V += k*(src1[0] - src2[ 0]);
  2198. }
  2199. H = ( 17*H+16 ) >> 5;
  2200. V = ( 17*V+16 ) >> 5;
  2201. a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
  2202. for(j=8; j>0; --j) {
  2203. int b = a;
  2204. a += V;
  2205. src[0] = cm[ (b ) >> 5 ];
  2206. src[1] = cm[ (b+ H) >> 5 ];
  2207. src[2] = cm[ (b+2*H) >> 5 ];
  2208. src[3] = cm[ (b+3*H) >> 5 ];
  2209. src[4] = cm[ (b+4*H) >> 5 ];
  2210. src[5] = cm[ (b+5*H) >> 5 ];
  2211. src[6] = cm[ (b+6*H) >> 5 ];
  2212. src[7] = cm[ (b+7*H) >> 5 ];
  2213. src += stride;
  2214. }
  2215. }
  2216. #define SRC(x,y) src[(x)+(y)*stride]
  2217. #define PL(y) \
  2218. const int l##y = (SRC(-1,y-1) + 2*SRC(-1,y) + SRC(-1,y+1) + 2) >> 2;
  2219. #define PREDICT_8x8_LOAD_LEFT \
  2220. const int l0 = ((has_topleft ? SRC(-1,-1) : SRC(-1,0)) \
  2221. + 2*SRC(-1,0) + SRC(-1,1) + 2) >> 2; \
  2222. PL(1) PL(2) PL(3) PL(4) PL(5) PL(6) \
  2223. const int l7 attribute_unused = (SRC(-1,6) + 3*SRC(-1,7) + 2) >> 2
  2224. #define PT(x) \
  2225. const int t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  2226. #define PREDICT_8x8_LOAD_TOP \
  2227. const int t0 = ((has_topleft ? SRC(-1,-1) : SRC(0,-1)) \
  2228. + 2*SRC(0,-1) + SRC(1,-1) + 2) >> 2; \
  2229. PT(1) PT(2) PT(3) PT(4) PT(5) PT(6) \
  2230. const int t7 attribute_unused = ((has_topright ? SRC(8,-1) : SRC(7,-1)) \
  2231. + 2*SRC(7,-1) + SRC(6,-1) + 2) >> 2
  2232. #define PTR(x) \
  2233. t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  2234. #define PREDICT_8x8_LOAD_TOPRIGHT \
  2235. int t8, t9, t10, t11, t12, t13, t14, t15; \
  2236. if(has_topright) { \
  2237. PTR(8) PTR(9) PTR(10) PTR(11) PTR(12) PTR(13) PTR(14) \
  2238. t15 = (SRC(14,-1) + 3*SRC(15,-1) + 2) >> 2; \
  2239. } else t8=t9=t10=t11=t12=t13=t14=t15= SRC(7,-1);
  2240. #define PREDICT_8x8_LOAD_TOPLEFT \
  2241. const int lt = (SRC(-1,0) + 2*SRC(-1,-1) + SRC(0,-1) + 2) >> 2
  2242. #define PREDICT_8x8_DC(v) \
  2243. int y; \
  2244. for( y = 0; y < 8; y++ ) { \
  2245. ((uint32_t*)src)[0] = \
  2246. ((uint32_t*)src)[1] = v; \
  2247. src += stride; \
  2248. }
  2249. static void pred8x8l_128_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2250. {
  2251. PREDICT_8x8_DC(0x80808080);
  2252. }
  2253. static void pred8x8l_left_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2254. {
  2255. PREDICT_8x8_LOAD_LEFT;
  2256. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7+4) >> 3) * 0x01010101;
  2257. PREDICT_8x8_DC(dc);
  2258. }
  2259. static void pred8x8l_top_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2260. {
  2261. PREDICT_8x8_LOAD_TOP;
  2262. const uint32_t dc = ((t0+t1+t2+t3+t4+t5+t6+t7+4) >> 3) * 0x01010101;
  2263. PREDICT_8x8_DC(dc);
  2264. }
  2265. static void pred8x8l_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2266. {
  2267. PREDICT_8x8_LOAD_LEFT;
  2268. PREDICT_8x8_LOAD_TOP;
  2269. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7
  2270. +t0+t1+t2+t3+t4+t5+t6+t7+8) >> 4) * 0x01010101;
  2271. PREDICT_8x8_DC(dc);
  2272. }
  2273. static void pred8x8l_horizontal_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2274. {
  2275. PREDICT_8x8_LOAD_LEFT;
  2276. #define ROW(y) ((uint32_t*)(src+y*stride))[0] =\
  2277. ((uint32_t*)(src+y*stride))[1] = 0x01010101 * l##y
  2278. ROW(0); ROW(1); ROW(2); ROW(3); ROW(4); ROW(5); ROW(6); ROW(7);
  2279. #undef ROW
  2280. }
  2281. static void pred8x8l_vertical_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2282. {
  2283. int y;
  2284. PREDICT_8x8_LOAD_TOP;
  2285. src[0] = t0;
  2286. src[1] = t1;
  2287. src[2] = t2;
  2288. src[3] = t3;
  2289. src[4] = t4;
  2290. src[5] = t5;
  2291. src[6] = t6;
  2292. src[7] = t7;
  2293. for( y = 1; y < 8; y++ )
  2294. *(uint64_t*)(src+y*stride) = *(uint64_t*)src;
  2295. }
  2296. static void pred8x8l_down_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2297. {
  2298. PREDICT_8x8_LOAD_TOP;
  2299. PREDICT_8x8_LOAD_TOPRIGHT;
  2300. SRC(0,0)= (t0 + 2*t1 + t2 + 2) >> 2;
  2301. SRC(0,1)=SRC(1,0)= (t1 + 2*t2 + t3 + 2) >> 2;
  2302. SRC(0,2)=SRC(1,1)=SRC(2,0)= (t2 + 2*t3 + t4 + 2) >> 2;
  2303. SRC(0,3)=SRC(1,2)=SRC(2,1)=SRC(3,0)= (t3 + 2*t4 + t5 + 2) >> 2;
  2304. SRC(0,4)=SRC(1,3)=SRC(2,2)=SRC(3,1)=SRC(4,0)= (t4 + 2*t5 + t6 + 2) >> 2;
  2305. SRC(0,5)=SRC(1,4)=SRC(2,3)=SRC(3,2)=SRC(4,1)=SRC(5,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2306. 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;
  2307. 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;
  2308. 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;
  2309. SRC(2,7)=SRC(3,6)=SRC(4,5)=SRC(5,4)=SRC(6,3)=SRC(7,2)= (t9 + 2*t10 + t11 + 2) >> 2;
  2310. SRC(3,7)=SRC(4,6)=SRC(5,5)=SRC(6,4)=SRC(7,3)= (t10 + 2*t11 + t12 + 2) >> 2;
  2311. SRC(4,7)=SRC(5,6)=SRC(6,5)=SRC(7,4)= (t11 + 2*t12 + t13 + 2) >> 2;
  2312. SRC(5,7)=SRC(6,6)=SRC(7,5)= (t12 + 2*t13 + t14 + 2) >> 2;
  2313. SRC(6,7)=SRC(7,6)= (t13 + 2*t14 + t15 + 2) >> 2;
  2314. SRC(7,7)= (t14 + 3*t15 + 2) >> 2;
  2315. }
  2316. static void pred8x8l_down_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2317. {
  2318. PREDICT_8x8_LOAD_TOP;
  2319. PREDICT_8x8_LOAD_LEFT;
  2320. PREDICT_8x8_LOAD_TOPLEFT;
  2321. SRC(0,7)= (l7 + 2*l6 + l5 + 2) >> 2;
  2322. SRC(0,6)=SRC(1,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2323. SRC(0,5)=SRC(1,6)=SRC(2,7)= (l5 + 2*l4 + l3 + 2) >> 2;
  2324. SRC(0,4)=SRC(1,5)=SRC(2,6)=SRC(3,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2325. SRC(0,3)=SRC(1,4)=SRC(2,5)=SRC(3,6)=SRC(4,7)= (l3 + 2*l2 + l1 + 2) >> 2;
  2326. SRC(0,2)=SRC(1,3)=SRC(2,4)=SRC(3,5)=SRC(4,6)=SRC(5,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2327. 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;
  2328. 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;
  2329. 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;
  2330. SRC(2,0)=SRC(3,1)=SRC(4,2)=SRC(5,3)=SRC(6,4)=SRC(7,5)= (t0 + 2*t1 + t2 + 2) >> 2;
  2331. SRC(3,0)=SRC(4,1)=SRC(5,2)=SRC(6,3)=SRC(7,4)= (t1 + 2*t2 + t3 + 2) >> 2;
  2332. SRC(4,0)=SRC(5,1)=SRC(6,2)=SRC(7,3)= (t2 + 2*t3 + t4 + 2) >> 2;
  2333. SRC(5,0)=SRC(6,1)=SRC(7,2)= (t3 + 2*t4 + t5 + 2) >> 2;
  2334. SRC(6,0)=SRC(7,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2335. SRC(7,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2336. }
  2337. static void pred8x8l_vertical_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2338. {
  2339. PREDICT_8x8_LOAD_TOP;
  2340. PREDICT_8x8_LOAD_LEFT;
  2341. PREDICT_8x8_LOAD_TOPLEFT;
  2342. SRC(0,6)= (l5 + 2*l4 + l3 + 2) >> 2;
  2343. SRC(0,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2344. SRC(0,4)=SRC(1,6)= (l3 + 2*l2 + l1 + 2) >> 2;
  2345. SRC(0,5)=SRC(1,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2346. SRC(0,2)=SRC(1,4)=SRC(2,6)= (l1 + 2*l0 + lt + 2) >> 2;
  2347. SRC(0,3)=SRC(1,5)=SRC(2,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2348. SRC(0,1)=SRC(1,3)=SRC(2,5)=SRC(3,7)= (l0 + 2*lt + t0 + 2) >> 2;
  2349. SRC(0,0)=SRC(1,2)=SRC(2,4)=SRC(3,6)= (lt + t0 + 1) >> 1;
  2350. SRC(1,1)=SRC(2,3)=SRC(3,5)=SRC(4,7)= (lt + 2*t0 + t1 + 2) >> 2;
  2351. SRC(1,0)=SRC(2,2)=SRC(3,4)=SRC(4,6)= (t0 + t1 + 1) >> 1;
  2352. SRC(2,1)=SRC(3,3)=SRC(4,5)=SRC(5,7)= (t0 + 2*t1 + t2 + 2) >> 2;
  2353. SRC(2,0)=SRC(3,2)=SRC(4,4)=SRC(5,6)= (t1 + t2 + 1) >> 1;
  2354. SRC(3,1)=SRC(4,3)=SRC(5,5)=SRC(6,7)= (t1 + 2*t2 + t3 + 2) >> 2;
  2355. SRC(3,0)=SRC(4,2)=SRC(5,4)=SRC(6,6)= (t2 + t3 + 1) >> 1;
  2356. SRC(4,1)=SRC(5,3)=SRC(6,5)=SRC(7,7)= (t2 + 2*t3 + t4 + 2) >> 2;
  2357. SRC(4,0)=SRC(5,2)=SRC(6,4)=SRC(7,6)= (t3 + t4 + 1) >> 1;
  2358. SRC(5,1)=SRC(6,3)=SRC(7,5)= (t3 + 2*t4 + t5 + 2) >> 2;
  2359. SRC(5,0)=SRC(6,2)=SRC(7,4)= (t4 + t5 + 1) >> 1;
  2360. SRC(6,1)=SRC(7,3)= (t4 + 2*t5 + t6 + 2) >> 2;
  2361. SRC(6,0)=SRC(7,2)= (t5 + t6 + 1) >> 1;
  2362. SRC(7,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2363. SRC(7,0)= (t6 + t7 + 1) >> 1;
  2364. }
  2365. static void pred8x8l_horizontal_down_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2366. {
  2367. PREDICT_8x8_LOAD_TOP;
  2368. PREDICT_8x8_LOAD_LEFT;
  2369. PREDICT_8x8_LOAD_TOPLEFT;
  2370. SRC(0,7)= (l6 + l7 + 1) >> 1;
  2371. SRC(1,7)= (l5 + 2*l6 + l7 + 2) >> 2;
  2372. SRC(0,6)=SRC(2,7)= (l5 + l6 + 1) >> 1;
  2373. SRC(1,6)=SRC(3,7)= (l4 + 2*l5 + l6 + 2) >> 2;
  2374. SRC(0,5)=SRC(2,6)=SRC(4,7)= (l4 + l5 + 1) >> 1;
  2375. SRC(1,5)=SRC(3,6)=SRC(5,7)= (l3 + 2*l4 + l5 + 2) >> 2;
  2376. SRC(0,4)=SRC(2,5)=SRC(4,6)=SRC(6,7)= (l3 + l4 + 1) >> 1;
  2377. SRC(1,4)=SRC(3,5)=SRC(5,6)=SRC(7,7)= (l2 + 2*l3 + l4 + 2) >> 2;
  2378. SRC(0,3)=SRC(2,4)=SRC(4,5)=SRC(6,6)= (l2 + l3 + 1) >> 1;
  2379. SRC(1,3)=SRC(3,4)=SRC(5,5)=SRC(7,6)= (l1 + 2*l2 + l3 + 2) >> 2;
  2380. SRC(0,2)=SRC(2,3)=SRC(4,4)=SRC(6,5)= (l1 + l2 + 1) >> 1;
  2381. SRC(1,2)=SRC(3,3)=SRC(5,4)=SRC(7,5)= (l0 + 2*l1 + l2 + 2) >> 2;
  2382. SRC(0,1)=SRC(2,2)=SRC(4,3)=SRC(6,4)= (l0 + l1 + 1) >> 1;
  2383. SRC(1,1)=SRC(3,2)=SRC(5,3)=SRC(7,4)= (lt + 2*l0 + l1 + 2) >> 2;
  2384. SRC(0,0)=SRC(2,1)=SRC(4,2)=SRC(6,3)= (lt + l0 + 1) >> 1;
  2385. SRC(1,0)=SRC(3,1)=SRC(5,2)=SRC(7,3)= (l0 + 2*lt + t0 + 2) >> 2;
  2386. SRC(2,0)=SRC(4,1)=SRC(6,2)= (t1 + 2*t0 + lt + 2) >> 2;
  2387. SRC(3,0)=SRC(5,1)=SRC(7,2)= (t2 + 2*t1 + t0 + 2) >> 2;
  2388. SRC(4,0)=SRC(6,1)= (t3 + 2*t2 + t1 + 2) >> 2;
  2389. SRC(5,0)=SRC(7,1)= (t4 + 2*t3 + t2 + 2) >> 2;
  2390. SRC(6,0)= (t5 + 2*t4 + t3 + 2) >> 2;
  2391. SRC(7,0)= (t6 + 2*t5 + t4 + 2) >> 2;
  2392. }
  2393. static void pred8x8l_vertical_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2394. {
  2395. PREDICT_8x8_LOAD_TOP;
  2396. PREDICT_8x8_LOAD_TOPRIGHT;
  2397. SRC(0,0)= (t0 + t1 + 1) >> 1;
  2398. SRC(0,1)= (t0 + 2*t1 + t2 + 2) >> 2;
  2399. SRC(0,2)=SRC(1,0)= (t1 + t2 + 1) >> 1;
  2400. SRC(0,3)=SRC(1,1)= (t1 + 2*t2 + t3 + 2) >> 2;
  2401. SRC(0,4)=SRC(1,2)=SRC(2,0)= (t2 + t3 + 1) >> 1;
  2402. SRC(0,5)=SRC(1,3)=SRC(2,1)= (t2 + 2*t3 + t4 + 2) >> 2;
  2403. SRC(0,6)=SRC(1,4)=SRC(2,2)=SRC(3,0)= (t3 + t4 + 1) >> 1;
  2404. SRC(0,7)=SRC(1,5)=SRC(2,3)=SRC(3,1)= (t3 + 2*t4 + t5 + 2) >> 2;
  2405. SRC(1,6)=SRC(2,4)=SRC(3,2)=SRC(4,0)= (t4 + t5 + 1) >> 1;
  2406. SRC(1,7)=SRC(2,5)=SRC(3,3)=SRC(4,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2407. SRC(2,6)=SRC(3,4)=SRC(4,2)=SRC(5,0)= (t5 + t6 + 1) >> 1;
  2408. SRC(2,7)=SRC(3,5)=SRC(4,3)=SRC(5,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2409. SRC(3,6)=SRC(4,4)=SRC(5,2)=SRC(6,0)= (t6 + t7 + 1) >> 1;
  2410. SRC(3,7)=SRC(4,5)=SRC(5,3)=SRC(6,1)= (t6 + 2*t7 + t8 + 2) >> 2;
  2411. SRC(4,6)=SRC(5,4)=SRC(6,2)=SRC(7,0)= (t7 + t8 + 1) >> 1;
  2412. SRC(4,7)=SRC(5,5)=SRC(6,3)=SRC(7,1)= (t7 + 2*t8 + t9 + 2) >> 2;
  2413. SRC(5,6)=SRC(6,4)=SRC(7,2)= (t8 + t9 + 1) >> 1;
  2414. SRC(5,7)=SRC(6,5)=SRC(7,3)= (t8 + 2*t9 + t10 + 2) >> 2;
  2415. SRC(6,6)=SRC(7,4)= (t9 + t10 + 1) >> 1;
  2416. SRC(6,7)=SRC(7,5)= (t9 + 2*t10 + t11 + 2) >> 2;
  2417. SRC(7,6)= (t10 + t11 + 1) >> 1;
  2418. SRC(7,7)= (t10 + 2*t11 + t12 + 2) >> 2;
  2419. }
  2420. static void pred8x8l_horizontal_up_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2421. {
  2422. PREDICT_8x8_LOAD_LEFT;
  2423. SRC(0,0)= (l0 + l1 + 1) >> 1;
  2424. SRC(1,0)= (l0 + 2*l1 + l2 + 2) >> 2;
  2425. SRC(0,1)=SRC(2,0)= (l1 + l2 + 1) >> 1;
  2426. SRC(1,1)=SRC(3,0)= (l1 + 2*l2 + l3 + 2) >> 2;
  2427. SRC(0,2)=SRC(2,1)=SRC(4,0)= (l2 + l3 + 1) >> 1;
  2428. SRC(1,2)=SRC(3,1)=SRC(5,0)= (l2 + 2*l3 + l4 + 2) >> 2;
  2429. SRC(0,3)=SRC(2,2)=SRC(4,1)=SRC(6,0)= (l3 + l4 + 1) >> 1;
  2430. SRC(1,3)=SRC(3,2)=SRC(5,1)=SRC(7,0)= (l3 + 2*l4 + l5 + 2) >> 2;
  2431. SRC(0,4)=SRC(2,3)=SRC(4,2)=SRC(6,1)= (l4 + l5 + 1) >> 1;
  2432. SRC(1,4)=SRC(3,3)=SRC(5,2)=SRC(7,1)= (l4 + 2*l5 + l6 + 2) >> 2;
  2433. SRC(0,5)=SRC(2,4)=SRC(4,3)=SRC(6,2)= (l5 + l6 + 1) >> 1;
  2434. SRC(1,5)=SRC(3,4)=SRC(5,3)=SRC(7,2)= (l5 + 2*l6 + l7 + 2) >> 2;
  2435. SRC(0,6)=SRC(2,5)=SRC(4,4)=SRC(6,3)= (l6 + l7 + 1) >> 1;
  2436. SRC(1,6)=SRC(3,5)=SRC(5,4)=SRC(7,3)= (l6 + 3*l7 + 2) >> 2;
  2437. SRC(0,7)=SRC(1,7)=SRC(2,6)=SRC(2,7)=SRC(3,6)=
  2438. SRC(3,7)=SRC(4,5)=SRC(4,6)=SRC(4,7)=SRC(5,5)=
  2439. SRC(5,6)=SRC(5,7)=SRC(6,4)=SRC(6,5)=SRC(6,6)=
  2440. SRC(6,7)=SRC(7,4)=SRC(7,5)=SRC(7,6)=SRC(7,7)= l7;
  2441. }
  2442. #undef PREDICT_8x8_LOAD_LEFT
  2443. #undef PREDICT_8x8_LOAD_TOP
  2444. #undef PREDICT_8x8_LOAD_TOPLEFT
  2445. #undef PREDICT_8x8_LOAD_TOPRIGHT
  2446. #undef PREDICT_8x8_DC
  2447. #undef PTR
  2448. #undef PT
  2449. #undef PL
  2450. #undef SRC
  2451. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  2452. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2453. int src_x_offset, int src_y_offset,
  2454. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  2455. MpegEncContext * const s = &h->s;
  2456. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  2457. int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  2458. const int luma_xy= (mx&3) + ((my&3)<<2);
  2459. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
  2460. uint8_t * src_cb, * src_cr;
  2461. int extra_width= h->emu_edge_width;
  2462. int extra_height= h->emu_edge_height;
  2463. int emu=0;
  2464. const int full_mx= mx>>2;
  2465. const int full_my= my>>2;
  2466. const int pic_width = 16*s->mb_width;
  2467. const int pic_height = 16*s->mb_height >> MB_MBAFF;
  2468. if(!pic->data[0])
  2469. return;
  2470. if(mx&7) extra_width -= 3;
  2471. if(my&7) extra_height -= 3;
  2472. if( full_mx < 0-extra_width
  2473. || full_my < 0-extra_height
  2474. || full_mx + 16/*FIXME*/ > pic_width + extra_width
  2475. || full_my + 16/*FIXME*/ > pic_height + extra_height){
  2476. 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);
  2477. src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
  2478. emu=1;
  2479. }
  2480. qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
  2481. if(!square){
  2482. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
  2483. }
  2484. if(s->flags&CODEC_FLAG_GRAY) return;
  2485. if(MB_MBAFF){
  2486. // chroma offset when predicting from a field of opposite parity
  2487. my += 2 * ((s->mb_y & 1) - (h->ref_cache[list][scan8[n]] & 1));
  2488. emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
  2489. }
  2490. src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2491. src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2492. if(emu){
  2493. 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);
  2494. src_cb= s->edge_emu_buffer;
  2495. }
  2496. chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2497. if(emu){
  2498. 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);
  2499. src_cr= s->edge_emu_buffer;
  2500. }
  2501. chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2502. }
  2503. static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
  2504. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2505. int x_offset, int y_offset,
  2506. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2507. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2508. int list0, int list1){
  2509. MpegEncContext * const s = &h->s;
  2510. qpel_mc_func *qpix_op= qpix_put;
  2511. h264_chroma_mc_func chroma_op= chroma_put;
  2512. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2513. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2514. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2515. x_offset += 8*s->mb_x;
  2516. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2517. if(list0){
  2518. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  2519. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  2520. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2521. qpix_op, chroma_op);
  2522. qpix_op= qpix_avg;
  2523. chroma_op= chroma_avg;
  2524. }
  2525. if(list1){
  2526. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  2527. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  2528. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2529. qpix_op, chroma_op);
  2530. }
  2531. }
  2532. static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
  2533. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2534. int x_offset, int y_offset,
  2535. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2536. h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
  2537. h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
  2538. int list0, int list1){
  2539. MpegEncContext * const s = &h->s;
  2540. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2541. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2542. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2543. x_offset += 8*s->mb_x;
  2544. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2545. if(list0 && list1){
  2546. /* don't optimize for luma-only case, since B-frames usually
  2547. * use implicit weights => chroma too. */
  2548. uint8_t *tmp_cb = s->obmc_scratchpad;
  2549. uint8_t *tmp_cr = s->obmc_scratchpad + 8;
  2550. uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
  2551. int refn0 = h->ref_cache[0][ scan8[n] ];
  2552. int refn1 = h->ref_cache[1][ scan8[n] ];
  2553. mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
  2554. dest_y, dest_cb, dest_cr,
  2555. x_offset, y_offset, qpix_put, chroma_put);
  2556. mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
  2557. tmp_y, tmp_cb, tmp_cr,
  2558. x_offset, y_offset, qpix_put, chroma_put);
  2559. if(h->use_weight == 2){
  2560. int weight0 = h->implicit_weight[refn0][refn1];
  2561. int weight1 = 64 - weight0;
  2562. luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
  2563. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2564. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2565. }else{
  2566. luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
  2567. h->luma_weight[0][refn0], h->luma_weight[1][refn1],
  2568. h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
  2569. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2570. h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
  2571. h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
  2572. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2573. h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
  2574. h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
  2575. }
  2576. }else{
  2577. int list = list1 ? 1 : 0;
  2578. int refn = h->ref_cache[list][ scan8[n] ];
  2579. Picture *ref= &h->ref_list[list][refn];
  2580. mc_dir_part(h, ref, n, square, chroma_height, delta, list,
  2581. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2582. qpix_put, chroma_put);
  2583. luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
  2584. h->luma_weight[list][refn], h->luma_offset[list][refn]);
  2585. if(h->use_weight_chroma){
  2586. chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2587. h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
  2588. chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2589. h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
  2590. }
  2591. }
  2592. }
  2593. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  2594. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2595. int x_offset, int y_offset,
  2596. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2597. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2598. h264_weight_func *weight_op, h264_biweight_func *weight_avg,
  2599. int list0, int list1){
  2600. if((h->use_weight==2 && list0 && list1
  2601. && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
  2602. || h->use_weight==1)
  2603. mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2604. x_offset, y_offset, qpix_put, chroma_put,
  2605. weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
  2606. else
  2607. mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2608. x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
  2609. }
  2610. static inline void prefetch_motion(H264Context *h, int list){
  2611. /* fetch pixels for estimated mv 4 macroblocks ahead
  2612. * optimized for 64byte cache lines */
  2613. MpegEncContext * const s = &h->s;
  2614. const int refn = h->ref_cache[list][scan8[0]];
  2615. if(refn >= 0){
  2616. const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
  2617. const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
  2618. uint8_t **src= h->ref_list[list][refn].data;
  2619. int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
  2620. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  2621. off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
  2622. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  2623. }
  2624. }
  2625. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2626. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  2627. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
  2628. h264_weight_func *weight_op, h264_biweight_func *weight_avg){
  2629. MpegEncContext * const s = &h->s;
  2630. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2631. const int mb_type= s->current_picture.mb_type[mb_xy];
  2632. assert(IS_INTER(mb_type));
  2633. prefetch_motion(h, 0);
  2634. if(IS_16X16(mb_type)){
  2635. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  2636. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  2637. &weight_op[0], &weight_avg[0],
  2638. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2639. }else if(IS_16X8(mb_type)){
  2640. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  2641. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2642. &weight_op[1], &weight_avg[1],
  2643. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2644. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  2645. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2646. &weight_op[1], &weight_avg[1],
  2647. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2648. }else if(IS_8X16(mb_type)){
  2649. mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
  2650. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2651. &weight_op[2], &weight_avg[2],
  2652. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2653. mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
  2654. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2655. &weight_op[2], &weight_avg[2],
  2656. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2657. }else{
  2658. int i;
  2659. assert(IS_8X8(mb_type));
  2660. for(i=0; i<4; i++){
  2661. const int sub_mb_type= h->sub_mb_type[i];
  2662. const int n= 4*i;
  2663. int x_offset= (i&1)<<2;
  2664. int y_offset= (i&2)<<1;
  2665. if(IS_SUB_8X8(sub_mb_type)){
  2666. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2667. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2668. &weight_op[3], &weight_avg[3],
  2669. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2670. }else if(IS_SUB_8X4(sub_mb_type)){
  2671. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2672. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2673. &weight_op[4], &weight_avg[4],
  2674. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2675. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  2676. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2677. &weight_op[4], &weight_avg[4],
  2678. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2679. }else if(IS_SUB_4X8(sub_mb_type)){
  2680. mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2681. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2682. &weight_op[5], &weight_avg[5],
  2683. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2684. mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
  2685. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2686. &weight_op[5], &weight_avg[5],
  2687. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2688. }else{
  2689. int j;
  2690. assert(IS_SUB_4X4(sub_mb_type));
  2691. for(j=0; j<4; j++){
  2692. int sub_x_offset= x_offset + 2*(j&1);
  2693. int sub_y_offset= y_offset + (j&2);
  2694. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  2695. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2696. &weight_op[6], &weight_avg[6],
  2697. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2698. }
  2699. }
  2700. }
  2701. }
  2702. prefetch_motion(h, 1);
  2703. }
  2704. static void decode_init_vlc(){
  2705. static int done = 0;
  2706. if (!done) {
  2707. int i;
  2708. done = 1;
  2709. init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
  2710. &chroma_dc_coeff_token_len [0], 1, 1,
  2711. &chroma_dc_coeff_token_bits[0], 1, 1, 1);
  2712. for(i=0; i<4; i++){
  2713. init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
  2714. &coeff_token_len [i][0], 1, 1,
  2715. &coeff_token_bits[i][0], 1, 1, 1);
  2716. }
  2717. for(i=0; i<3; i++){
  2718. init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
  2719. &chroma_dc_total_zeros_len [i][0], 1, 1,
  2720. &chroma_dc_total_zeros_bits[i][0], 1, 1, 1);
  2721. }
  2722. for(i=0; i<15; i++){
  2723. init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
  2724. &total_zeros_len [i][0], 1, 1,
  2725. &total_zeros_bits[i][0], 1, 1, 1);
  2726. }
  2727. for(i=0; i<6; i++){
  2728. init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
  2729. &run_len [i][0], 1, 1,
  2730. &run_bits[i][0], 1, 1, 1);
  2731. }
  2732. init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
  2733. &run_len [6][0], 1, 1,
  2734. &run_bits[6][0], 1, 1, 1);
  2735. }
  2736. }
  2737. /**
  2738. * Sets the intra prediction function pointers.
  2739. */
  2740. static void init_pred_ptrs(H264Context *h){
  2741. // MpegEncContext * const s = &h->s;
  2742. h->pred4x4[VERT_PRED ]= pred4x4_vertical_c;
  2743. h->pred4x4[HOR_PRED ]= pred4x4_horizontal_c;
  2744. h->pred4x4[DC_PRED ]= pred4x4_dc_c;
  2745. h->pred4x4[DIAG_DOWN_LEFT_PRED ]= pred4x4_down_left_c;
  2746. h->pred4x4[DIAG_DOWN_RIGHT_PRED]= pred4x4_down_right_c;
  2747. h->pred4x4[VERT_RIGHT_PRED ]= pred4x4_vertical_right_c;
  2748. h->pred4x4[HOR_DOWN_PRED ]= pred4x4_horizontal_down_c;
  2749. h->pred4x4[VERT_LEFT_PRED ]= pred4x4_vertical_left_c;
  2750. h->pred4x4[HOR_UP_PRED ]= pred4x4_horizontal_up_c;
  2751. h->pred4x4[LEFT_DC_PRED ]= pred4x4_left_dc_c;
  2752. h->pred4x4[TOP_DC_PRED ]= pred4x4_top_dc_c;
  2753. h->pred4x4[DC_128_PRED ]= pred4x4_128_dc_c;
  2754. h->pred8x8l[VERT_PRED ]= pred8x8l_vertical_c;
  2755. h->pred8x8l[HOR_PRED ]= pred8x8l_horizontal_c;
  2756. h->pred8x8l[DC_PRED ]= pred8x8l_dc_c;
  2757. h->pred8x8l[DIAG_DOWN_LEFT_PRED ]= pred8x8l_down_left_c;
  2758. h->pred8x8l[DIAG_DOWN_RIGHT_PRED]= pred8x8l_down_right_c;
  2759. h->pred8x8l[VERT_RIGHT_PRED ]= pred8x8l_vertical_right_c;
  2760. h->pred8x8l[HOR_DOWN_PRED ]= pred8x8l_horizontal_down_c;
  2761. h->pred8x8l[VERT_LEFT_PRED ]= pred8x8l_vertical_left_c;
  2762. h->pred8x8l[HOR_UP_PRED ]= pred8x8l_horizontal_up_c;
  2763. h->pred8x8l[LEFT_DC_PRED ]= pred8x8l_left_dc_c;
  2764. h->pred8x8l[TOP_DC_PRED ]= pred8x8l_top_dc_c;
  2765. h->pred8x8l[DC_128_PRED ]= pred8x8l_128_dc_c;
  2766. h->pred8x8[DC_PRED8x8 ]= ff_pred8x8_dc_c;
  2767. h->pred8x8[VERT_PRED8x8 ]= ff_pred8x8_vertical_c;
  2768. h->pred8x8[HOR_PRED8x8 ]= ff_pred8x8_horizontal_c;
  2769. h->pred8x8[PLANE_PRED8x8 ]= ff_pred8x8_plane_c;
  2770. h->pred8x8[LEFT_DC_PRED8x8]= pred8x8_left_dc_c;
  2771. h->pred8x8[TOP_DC_PRED8x8 ]= pred8x8_top_dc_c;
  2772. h->pred8x8[DC_128_PRED8x8 ]= ff_pred8x8_128_dc_c;
  2773. h->pred16x16[DC_PRED8x8 ]= ff_pred16x16_dc_c;
  2774. h->pred16x16[VERT_PRED8x8 ]= ff_pred16x16_vertical_c;
  2775. h->pred16x16[HOR_PRED8x8 ]= ff_pred16x16_horizontal_c;
  2776. h->pred16x16[PLANE_PRED8x8 ]= ff_pred16x16_plane_c;
  2777. h->pred16x16[LEFT_DC_PRED8x8]= pred16x16_left_dc_c;
  2778. h->pred16x16[TOP_DC_PRED8x8 ]= pred16x16_top_dc_c;
  2779. h->pred16x16[DC_128_PRED8x8 ]= ff_pred16x16_128_dc_c;
  2780. }
  2781. static void free_tables(H264Context *h){
  2782. av_freep(&h->intra4x4_pred_mode);
  2783. av_freep(&h->chroma_pred_mode_table);
  2784. av_freep(&h->cbp_table);
  2785. av_freep(&h->mvd_table[0]);
  2786. av_freep(&h->mvd_table[1]);
  2787. av_freep(&h->direct_table);
  2788. av_freep(&h->non_zero_count);
  2789. av_freep(&h->slice_table_base);
  2790. av_freep(&h->top_borders[1]);
  2791. av_freep(&h->top_borders[0]);
  2792. h->slice_table= NULL;
  2793. av_freep(&h->mb2b_xy);
  2794. av_freep(&h->mb2b8_xy);
  2795. av_freep(&h->s.obmc_scratchpad);
  2796. }
  2797. static void init_dequant8_coeff_table(H264Context *h){
  2798. int i,q,x;
  2799. const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
  2800. h->dequant8_coeff[0] = h->dequant8_buffer[0];
  2801. h->dequant8_coeff[1] = h->dequant8_buffer[1];
  2802. for(i=0; i<2; i++ ){
  2803. if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
  2804. h->dequant8_coeff[1] = h->dequant8_buffer[0];
  2805. break;
  2806. }
  2807. for(q=0; q<52; q++){
  2808. int shift = ff_div6[q];
  2809. int idx = ff_rem6[q];
  2810. for(x=0; x<64; x++)
  2811. h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
  2812. ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
  2813. h->pps.scaling_matrix8[i][x]) << shift;
  2814. }
  2815. }
  2816. }
  2817. static void init_dequant4_coeff_table(H264Context *h){
  2818. int i,j,q,x;
  2819. const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
  2820. for(i=0; i<6; i++ ){
  2821. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  2822. for(j=0; j<i; j++){
  2823. if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
  2824. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  2825. break;
  2826. }
  2827. }
  2828. if(j<i)
  2829. continue;
  2830. for(q=0; q<52; q++){
  2831. int shift = ff_div6[q] + 2;
  2832. int idx = ff_rem6[q];
  2833. for(x=0; x<16; x++)
  2834. h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
  2835. ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
  2836. h->pps.scaling_matrix4[i][x]) << shift;
  2837. }
  2838. }
  2839. }
  2840. static void init_dequant_tables(H264Context *h){
  2841. int i,x;
  2842. init_dequant4_coeff_table(h);
  2843. if(h->pps.transform_8x8_mode)
  2844. init_dequant8_coeff_table(h);
  2845. if(h->sps.transform_bypass){
  2846. for(i=0; i<6; i++)
  2847. for(x=0; x<16; x++)
  2848. h->dequant4_coeff[i][0][x] = 1<<6;
  2849. if(h->pps.transform_8x8_mode)
  2850. for(i=0; i<2; i++)
  2851. for(x=0; x<64; x++)
  2852. h->dequant8_coeff[i][0][x] = 1<<6;
  2853. }
  2854. }
  2855. /**
  2856. * allocates tables.
  2857. * needs width/height
  2858. */
  2859. static int alloc_tables(H264Context *h){
  2860. MpegEncContext * const s = &h->s;
  2861. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  2862. int x,y;
  2863. CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
  2864. CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
  2865. CHECKED_ALLOCZ(h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(uint8_t))
  2866. CHECKED_ALLOCZ(h->top_borders[0] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2867. CHECKED_ALLOCZ(h->top_borders[1] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2868. CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
  2869. if( h->pps.cabac ) {
  2870. CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
  2871. CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
  2872. CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t));
  2873. CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t));
  2874. }
  2875. memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(uint8_t));
  2876. h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
  2877. CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint32_t));
  2878. CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint32_t));
  2879. for(y=0; y<s->mb_height; y++){
  2880. for(x=0; x<s->mb_width; x++){
  2881. const int mb_xy= x + y*s->mb_stride;
  2882. const int b_xy = 4*x + 4*y*h->b_stride;
  2883. const int b8_xy= 2*x + 2*y*h->b8_stride;
  2884. h->mb2b_xy [mb_xy]= b_xy;
  2885. h->mb2b8_xy[mb_xy]= b8_xy;
  2886. }
  2887. }
  2888. s->obmc_scratchpad = NULL;
  2889. if(!h->dequant4_coeff[0])
  2890. init_dequant_tables(h);
  2891. return 0;
  2892. fail:
  2893. free_tables(h);
  2894. return -1;
  2895. }
  2896. static void common_init(H264Context *h){
  2897. MpegEncContext * const s = &h->s;
  2898. s->width = s->avctx->width;
  2899. s->height = s->avctx->height;
  2900. s->codec_id= s->avctx->codec->id;
  2901. init_pred_ptrs(h);
  2902. h->dequant_coeff_pps= -1;
  2903. s->unrestricted_mv=1;
  2904. s->decode=1; //FIXME
  2905. memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  2906. memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  2907. }
  2908. static int decode_init(AVCodecContext *avctx){
  2909. H264Context *h= avctx->priv_data;
  2910. MpegEncContext * const s = &h->s;
  2911. MPV_decode_defaults(s);
  2912. s->avctx = avctx;
  2913. common_init(h);
  2914. s->out_format = FMT_H264;
  2915. s->workaround_bugs= avctx->workaround_bugs;
  2916. // set defaults
  2917. // s->decode_mb= ff_h263_decode_mb;
  2918. s->low_delay= 1;
  2919. avctx->pix_fmt= PIX_FMT_YUV420P;
  2920. decode_init_vlc();
  2921. if(avctx->extradata_size > 0 && avctx->extradata &&
  2922. *(char *)avctx->extradata == 1){
  2923. h->is_avc = 1;
  2924. h->got_avcC = 0;
  2925. } else {
  2926. h->is_avc = 0;
  2927. }
  2928. return 0;
  2929. }
  2930. static int frame_start(H264Context *h){
  2931. MpegEncContext * const s = &h->s;
  2932. int i;
  2933. if(MPV_frame_start(s, s->avctx) < 0)
  2934. return -1;
  2935. ff_er_frame_start(s);
  2936. assert(s->linesize && s->uvlinesize);
  2937. for(i=0; i<16; i++){
  2938. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  2939. h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  2940. }
  2941. for(i=0; i<4; i++){
  2942. h->block_offset[16+i]=
  2943. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2944. h->block_offset[24+16+i]=
  2945. h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2946. }
  2947. /* can't be in alloc_tables because linesize isn't known there.
  2948. * FIXME: redo bipred weight to not require extra buffer? */
  2949. if(!s->obmc_scratchpad)
  2950. s->obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  2951. /* some macroblocks will be accessed before they're available */
  2952. if(FRAME_MBAFF)
  2953. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(uint8_t));
  2954. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  2955. return 0;
  2956. }
  2957. static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  2958. MpegEncContext * const s = &h->s;
  2959. int i;
  2960. src_y -= linesize;
  2961. src_cb -= uvlinesize;
  2962. src_cr -= uvlinesize;
  2963. // There are two lines saved, the line above the the top macroblock of a pair,
  2964. // and the line above the bottom macroblock
  2965. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  2966. for(i=1; i<17; i++){
  2967. h->left_border[i]= src_y[15+i* linesize];
  2968. }
  2969. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
  2970. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
  2971. if(!(s->flags&CODEC_FLAG_GRAY)){
  2972. h->left_border[17 ]= h->top_borders[0][s->mb_x][16+7];
  2973. h->left_border[17+9]= h->top_borders[0][s->mb_x][24+7];
  2974. for(i=1; i<9; i++){
  2975. h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
  2976. h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
  2977. }
  2978. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
  2979. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
  2980. }
  2981. }
  2982. 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){
  2983. MpegEncContext * const s = &h->s;
  2984. int temp8, i;
  2985. uint64_t temp64;
  2986. int deblock_left = (s->mb_x > 0);
  2987. int deblock_top = (s->mb_y > 0);
  2988. src_y -= linesize + 1;
  2989. src_cb -= uvlinesize + 1;
  2990. src_cr -= uvlinesize + 1;
  2991. #define XCHG(a,b,t,xchg)\
  2992. t= a;\
  2993. if(xchg)\
  2994. a= b;\
  2995. b= t;
  2996. if(deblock_left){
  2997. for(i = !deblock_top; i<17; i++){
  2998. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  2999. }
  3000. }
  3001. if(deblock_top){
  3002. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  3003. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  3004. if(s->mb_x+1 < s->mb_width){
  3005. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  3006. }
  3007. }
  3008. if(!(s->flags&CODEC_FLAG_GRAY)){
  3009. if(deblock_left){
  3010. for(i = !deblock_top; i<9; i++){
  3011. XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
  3012. XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
  3013. }
  3014. }
  3015. if(deblock_top){
  3016. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  3017. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  3018. }
  3019. }
  3020. }
  3021. static inline void backup_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  3022. MpegEncContext * const s = &h->s;
  3023. int i;
  3024. src_y -= 2 * linesize;
  3025. src_cb -= 2 * uvlinesize;
  3026. src_cr -= 2 * uvlinesize;
  3027. // There are two lines saved, the line above the the top macroblock of a pair,
  3028. // and the line above the bottom macroblock
  3029. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  3030. h->left_border[1]= h->top_borders[1][s->mb_x][15];
  3031. for(i=2; i<34; i++){
  3032. h->left_border[i]= src_y[15+i* linesize];
  3033. }
  3034. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 32*linesize);
  3035. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+32*linesize);
  3036. *(uint64_t*)(h->top_borders[1][s->mb_x]+0)= *(uint64_t*)(src_y + 33*linesize);
  3037. *(uint64_t*)(h->top_borders[1][s->mb_x]+8)= *(uint64_t*)(src_y +8+33*linesize);
  3038. if(!(s->flags&CODEC_FLAG_GRAY)){
  3039. h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7];
  3040. h->left_border[34+ 1]= h->top_borders[1][s->mb_x][16+7];
  3041. h->left_border[34+18 ]= h->top_borders[0][s->mb_x][24+7];
  3042. h->left_border[34+18+1]= h->top_borders[1][s->mb_x][24+7];
  3043. for(i=2; i<18; i++){
  3044. h->left_border[i+34 ]= src_cb[7+i*uvlinesize];
  3045. h->left_border[i+34+18]= src_cr[7+i*uvlinesize];
  3046. }
  3047. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+16*uvlinesize);
  3048. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+16*uvlinesize);
  3049. *(uint64_t*)(h->top_borders[1][s->mb_x]+16)= *(uint64_t*)(src_cb+17*uvlinesize);
  3050. *(uint64_t*)(h->top_borders[1][s->mb_x]+24)= *(uint64_t*)(src_cr+17*uvlinesize);
  3051. }
  3052. }
  3053. 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){
  3054. MpegEncContext * const s = &h->s;
  3055. int temp8, i;
  3056. uint64_t temp64;
  3057. int deblock_left = (s->mb_x > 0);
  3058. int deblock_top = (s->mb_y > 1);
  3059. tprintf("xchg_pair_border: src_y:%p src_cb:%p src_cr:%p ls:%d uvls:%d\n", src_y, src_cb, src_cr, linesize, uvlinesize);
  3060. src_y -= 2 * linesize + 1;
  3061. src_cb -= 2 * uvlinesize + 1;
  3062. src_cr -= 2 * uvlinesize + 1;
  3063. #define XCHG(a,b,t,xchg)\
  3064. t= a;\
  3065. if(xchg)\
  3066. a= b;\
  3067. b= t;
  3068. if(deblock_left){
  3069. for(i = (!deblock_top)<<1; i<34; i++){
  3070. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  3071. }
  3072. }
  3073. if(deblock_top){
  3074. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  3075. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  3076. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+0), *(uint64_t*)(src_y +1 +linesize), temp64, xchg);
  3077. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+8), *(uint64_t*)(src_y +9 +linesize), temp64, 1);
  3078. if(s->mb_x+1 < s->mb_width){
  3079. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  3080. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x+1]), *(uint64_t*)(src_y +17 +linesize), temp64, 1);
  3081. }
  3082. }
  3083. if(!(s->flags&CODEC_FLAG_GRAY)){
  3084. if(deblock_left){
  3085. for(i = (!deblock_top) << 1; i<18; i++){
  3086. XCHG(h->left_border[i+34 ], src_cb[i*uvlinesize], temp8, xchg);
  3087. XCHG(h->left_border[i+34+18], src_cr[i*uvlinesize], temp8, xchg);
  3088. }
  3089. }
  3090. if(deblock_top){
  3091. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  3092. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  3093. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+16), *(uint64_t*)(src_cb+1 +uvlinesize), temp64, 1);
  3094. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+24), *(uint64_t*)(src_cr+1 +uvlinesize), temp64, 1);
  3095. }
  3096. }
  3097. }
  3098. static void hl_decode_mb(H264Context *h){
  3099. MpegEncContext * const s = &h->s;
  3100. const int mb_x= s->mb_x;
  3101. const int mb_y= s->mb_y;
  3102. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3103. const int mb_type= s->current_picture.mb_type[mb_xy];
  3104. uint8_t *dest_y, *dest_cb, *dest_cr;
  3105. int linesize, uvlinesize /*dct_offset*/;
  3106. int i;
  3107. int *block_offset = &h->block_offset[0];
  3108. const unsigned int bottom = mb_y & 1;
  3109. const int transform_bypass = (s->qscale == 0 && h->sps.transform_bypass);
  3110. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  3111. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  3112. if(!s->decode)
  3113. return;
  3114. dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3115. dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3116. dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3117. s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
  3118. s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
  3119. if (MB_FIELD) {
  3120. linesize = h->mb_linesize = s->linesize * 2;
  3121. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  3122. block_offset = &h->block_offset[24];
  3123. if(mb_y&1){ //FIXME move out of this func?
  3124. dest_y -= s->linesize*15;
  3125. dest_cb-= s->uvlinesize*7;
  3126. dest_cr-= s->uvlinesize*7;
  3127. }
  3128. if(FRAME_MBAFF) {
  3129. int list;
  3130. for(list=0; list<2; list++){
  3131. if(!USES_LIST(mb_type, list))
  3132. continue;
  3133. if(IS_16X16(mb_type)){
  3134. int8_t *ref = &h->ref_cache[list][scan8[0]];
  3135. fill_rectangle(ref, 4, 4, 8, 16+*ref^(s->mb_y&1), 1);
  3136. }else{
  3137. for(i=0; i<16; i+=4){
  3138. //FIXME can refs be smaller than 8x8 when !direct_8x8_inference ?
  3139. int ref = h->ref_cache[list][scan8[i]];
  3140. if(ref >= 0)
  3141. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, 16+ref^(s->mb_y&1), 1);
  3142. }
  3143. }
  3144. }
  3145. }
  3146. } else {
  3147. linesize = h->mb_linesize = s->linesize;
  3148. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  3149. // dct_offset = s->linesize * 16;
  3150. }
  3151. if(transform_bypass){
  3152. idct_dc_add =
  3153. idct_add = IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  3154. }else if(IS_8x8DCT(mb_type)){
  3155. idct_dc_add = s->dsp.h264_idct8_dc_add;
  3156. idct_add = s->dsp.h264_idct8_add;
  3157. }else{
  3158. idct_dc_add = s->dsp.h264_idct_dc_add;
  3159. idct_add = s->dsp.h264_idct_add;
  3160. }
  3161. if(FRAME_MBAFF && h->deblocking_filter && IS_INTRA(mb_type)
  3162. && (!bottom || !IS_INTRA(s->current_picture.mb_type[mb_xy-s->mb_stride]))){
  3163. int mbt_y = mb_y&~1;
  3164. uint8_t *top_y = s->current_picture.data[0] + (mbt_y * 16* s->linesize ) + mb_x * 16;
  3165. uint8_t *top_cb = s->current_picture.data[1] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  3166. uint8_t *top_cr = s->current_picture.data[2] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  3167. xchg_pair_border(h, top_y, top_cb, top_cr, s->linesize, s->uvlinesize, 1);
  3168. }
  3169. if (IS_INTRA_PCM(mb_type)) {
  3170. unsigned int x, y;
  3171. // The pixels are stored in h->mb array in the same order as levels,
  3172. // copy them in output in the correct order.
  3173. for(i=0; i<16; i++) {
  3174. for (y=0; y<4; y++) {
  3175. for (x=0; x<4; x++) {
  3176. *(dest_y + block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x];
  3177. }
  3178. }
  3179. }
  3180. for(i=16; i<16+4; i++) {
  3181. for (y=0; y<4; y++) {
  3182. for (x=0; x<4; x++) {
  3183. *(dest_cb + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  3184. }
  3185. }
  3186. }
  3187. for(i=20; i<20+4; i++) {
  3188. for (y=0; y<4; y++) {
  3189. for (x=0; x<4; x++) {
  3190. *(dest_cr + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  3191. }
  3192. }
  3193. }
  3194. } else {
  3195. if(IS_INTRA(mb_type)){
  3196. if(h->deblocking_filter && !FRAME_MBAFF)
  3197. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1);
  3198. if(!(s->flags&CODEC_FLAG_GRAY)){
  3199. h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  3200. h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  3201. }
  3202. if(IS_INTRA4x4(mb_type)){
  3203. if(!s->encoding){
  3204. if(IS_8x8DCT(mb_type)){
  3205. for(i=0; i<16; i+=4){
  3206. uint8_t * const ptr= dest_y + block_offset[i];
  3207. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  3208. const int nnz = h->non_zero_count_cache[ scan8[i] ];
  3209. h->pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  3210. (h->topright_samples_available<<(i+1))&0x8000, linesize);
  3211. if(nnz){
  3212. if(nnz == 1 && h->mb[i*16])
  3213. idct_dc_add(ptr, h->mb + i*16, linesize);
  3214. else
  3215. idct_add(ptr, h->mb + i*16, linesize);
  3216. }
  3217. }
  3218. }else
  3219. for(i=0; i<16; i++){
  3220. uint8_t * const ptr= dest_y + block_offset[i];
  3221. uint8_t *topright;
  3222. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  3223. int nnz, tr;
  3224. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  3225. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  3226. assert(mb_y || linesize <= block_offset[i]);
  3227. if(!topright_avail){
  3228. tr= ptr[3 - linesize]*0x01010101;
  3229. topright= (uint8_t*) &tr;
  3230. }else
  3231. topright= ptr + 4 - linesize;
  3232. }else
  3233. topright= NULL;
  3234. h->pred4x4[ dir ](ptr, topright, linesize);
  3235. nnz = h->non_zero_count_cache[ scan8[i] ];
  3236. if(nnz){
  3237. if(s->codec_id == CODEC_ID_H264){
  3238. if(nnz == 1 && h->mb[i*16])
  3239. idct_dc_add(ptr, h->mb + i*16, linesize);
  3240. else
  3241. idct_add(ptr, h->mb + i*16, linesize);
  3242. }else
  3243. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  3244. }
  3245. }
  3246. }
  3247. }else{
  3248. h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  3249. if(s->codec_id == CODEC_ID_H264){
  3250. if(!transform_bypass)
  3251. h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[IS_INTRA(mb_type) ? 0:3][s->qscale][0]);
  3252. }else
  3253. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  3254. }
  3255. if(h->deblocking_filter && !FRAME_MBAFF)
  3256. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
  3257. }else if(s->codec_id == CODEC_ID_H264){
  3258. hl_motion(h, dest_y, dest_cb, dest_cr,
  3259. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  3260. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  3261. s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
  3262. }
  3263. if(!IS_INTRA4x4(mb_type)){
  3264. if(s->codec_id == CODEC_ID_H264){
  3265. if(IS_INTRA16x16(mb_type)){
  3266. for(i=0; i<16; i++){
  3267. if(h->non_zero_count_cache[ scan8[i] ])
  3268. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3269. else if(h->mb[i*16])
  3270. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3271. }
  3272. }else{
  3273. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  3274. for(i=0; i<16; i+=di){
  3275. int nnz = h->non_zero_count_cache[ scan8[i] ];
  3276. if(nnz){
  3277. if(nnz==1 && h->mb[i*16])
  3278. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3279. else
  3280. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3281. }
  3282. }
  3283. }
  3284. }else{
  3285. for(i=0; i<16; i++){
  3286. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  3287. uint8_t * const ptr= dest_y + block_offset[i];
  3288. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  3289. }
  3290. }
  3291. }
  3292. }
  3293. if(!(s->flags&CODEC_FLAG_GRAY)){
  3294. uint8_t *dest[2] = {dest_cb, dest_cr};
  3295. if(transform_bypass){
  3296. idct_add = idct_dc_add = s->dsp.add_pixels4;
  3297. }else{
  3298. idct_add = s->dsp.h264_idct_add;
  3299. idct_dc_add = s->dsp.h264_idct_dc_add;
  3300. 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]);
  3301. 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]);
  3302. }
  3303. if(s->codec_id == CODEC_ID_H264){
  3304. for(i=16; i<16+8; i++){
  3305. if(h->non_zero_count_cache[ scan8[i] ])
  3306. idct_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3307. else if(h->mb[i*16])
  3308. idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3309. }
  3310. }else{
  3311. for(i=16; i<16+8; i++){
  3312. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  3313. uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
  3314. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  3315. }
  3316. }
  3317. }
  3318. }
  3319. }
  3320. if(h->deblocking_filter) {
  3321. if (FRAME_MBAFF) {
  3322. //FIXME try deblocking one mb at a time?
  3323. // the reduction in load/storing mvs and such might outweigh the extra backup/xchg_border
  3324. const int mb_y = s->mb_y - 1;
  3325. uint8_t *pair_dest_y, *pair_dest_cb, *pair_dest_cr;
  3326. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3327. const int mb_type_top = s->current_picture.mb_type[mb_xy];
  3328. const int mb_type_bottom= s->current_picture.mb_type[mb_xy+s->mb_stride];
  3329. if (!bottom) return;
  3330. pair_dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3331. pair_dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3332. pair_dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3333. if(IS_INTRA(mb_type_top | mb_type_bottom))
  3334. xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0);
  3335. backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize);
  3336. // deblock a pair
  3337. // top
  3338. s->mb_y--;
  3339. tprintf("call mbaff filter_mb mb_x:%d mb_y:%d pair_dest_y = %p, dest_y = %p\n", mb_x, mb_y, pair_dest_y, dest_y);
  3340. fill_caches(h, mb_type_top, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3341. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy]);
  3342. filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize);
  3343. // bottom
  3344. s->mb_y++;
  3345. tprintf("call mbaff filter_mb\n");
  3346. fill_caches(h, mb_type_bottom, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3347. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy+s->mb_stride]);
  3348. filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3349. } else {
  3350. tprintf("call filter_mb\n");
  3351. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3352. fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3353. filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3354. }
  3355. }
  3356. }
  3357. /**
  3358. * fills the default_ref_list.
  3359. */
  3360. static int fill_default_ref_list(H264Context *h){
  3361. MpegEncContext * const s = &h->s;
  3362. int i;
  3363. int smallest_poc_greater_than_current = -1;
  3364. Picture sorted_short_ref[32];
  3365. if(h->slice_type==B_TYPE){
  3366. int out_i;
  3367. int limit= INT_MIN;
  3368. /* sort frame according to poc in B slice */
  3369. for(out_i=0; out_i<h->short_ref_count; out_i++){
  3370. int best_i=INT_MIN;
  3371. int best_poc=INT_MAX;
  3372. for(i=0; i<h->short_ref_count; i++){
  3373. const int poc= h->short_ref[i]->poc;
  3374. if(poc > limit && poc < best_poc){
  3375. best_poc= poc;
  3376. best_i= i;
  3377. }
  3378. }
  3379. assert(best_i != INT_MIN);
  3380. limit= best_poc;
  3381. sorted_short_ref[out_i]= *h->short_ref[best_i];
  3382. tprintf("sorted poc: %d->%d poc:%d fn:%d\n", best_i, out_i, sorted_short_ref[out_i].poc, sorted_short_ref[out_i].frame_num);
  3383. if (-1 == smallest_poc_greater_than_current) {
  3384. if (h->short_ref[best_i]->poc >= s->current_picture_ptr->poc) {
  3385. smallest_poc_greater_than_current = out_i;
  3386. }
  3387. }
  3388. }
  3389. }
  3390. if(s->picture_structure == PICT_FRAME){
  3391. if(h->slice_type==B_TYPE){
  3392. int list;
  3393. tprintf("current poc: %d, smallest_poc_greater_than_current: %d\n", s->current_picture_ptr->poc, smallest_poc_greater_than_current);
  3394. // find the largest poc
  3395. for(list=0; list<2; list++){
  3396. int index = 0;
  3397. int j= -99;
  3398. int step= list ? -1 : 1;
  3399. for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++, j+=step) {
  3400. while(j<0 || j>= h->short_ref_count){
  3401. if(j != -99 && step == (list ? -1 : 1))
  3402. return -1;
  3403. step = -step;
  3404. j= smallest_poc_greater_than_current + (step>>1);
  3405. }
  3406. if(sorted_short_ref[j].reference != 3) continue;
  3407. h->default_ref_list[list][index ]= sorted_short_ref[j];
  3408. h->default_ref_list[list][index++].pic_id= sorted_short_ref[j].frame_num;
  3409. }
  3410. for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){
  3411. if(h->long_ref[i] == NULL) continue;
  3412. if(h->long_ref[i]->reference != 3) continue;
  3413. h->default_ref_list[ list ][index ]= *h->long_ref[i];
  3414. h->default_ref_list[ list ][index++].pic_id= i;;
  3415. }
  3416. if(list && (smallest_poc_greater_than_current<=0 || smallest_poc_greater_than_current>=h->short_ref_count) && (1 < index)){
  3417. // swap the two first elements of L1 when
  3418. // L0 and L1 are identical
  3419. Picture temp= h->default_ref_list[1][0];
  3420. h->default_ref_list[1][0] = h->default_ref_list[1][1];
  3421. h->default_ref_list[1][1] = temp;
  3422. }
  3423. if(index < h->ref_count[ list ])
  3424. memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
  3425. }
  3426. }else{
  3427. int index=0;
  3428. for(i=0; i<h->short_ref_count; i++){
  3429. if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
  3430. h->default_ref_list[0][index ]= *h->short_ref[i];
  3431. h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
  3432. }
  3433. for(i = 0; i < 16; i++){
  3434. if(h->long_ref[i] == NULL) continue;
  3435. if(h->long_ref[i]->reference != 3) continue;
  3436. h->default_ref_list[0][index ]= *h->long_ref[i];
  3437. h->default_ref_list[0][index++].pic_id= i;;
  3438. }
  3439. if(index < h->ref_count[0])
  3440. memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
  3441. }
  3442. }else{ //FIELD
  3443. if(h->slice_type==B_TYPE){
  3444. }else{
  3445. //FIXME second field balh
  3446. }
  3447. }
  3448. #ifdef TRACE
  3449. for (i=0; i<h->ref_count[0]; i++) {
  3450. tprintf("List0: %s fn:%d 0x%p\n", (h->default_ref_list[0][i].long_ref ? "LT" : "ST"), h->default_ref_list[0][i].pic_id, h->default_ref_list[0][i].data[0]);
  3451. }
  3452. if(h->slice_type==B_TYPE){
  3453. for (i=0; i<h->ref_count[1]; i++) {
  3454. tprintf("List1: %s fn:%d 0x%p\n", (h->default_ref_list[1][i].long_ref ? "LT" : "ST"), h->default_ref_list[1][i].pic_id, h->default_ref_list[0][i].data[0]);
  3455. }
  3456. }
  3457. #endif
  3458. return 0;
  3459. }
  3460. static void print_short_term(H264Context *h);
  3461. static void print_long_term(H264Context *h);
  3462. static int decode_ref_pic_list_reordering(H264Context *h){
  3463. MpegEncContext * const s = &h->s;
  3464. int list, index;
  3465. print_short_term(h);
  3466. print_long_term(h);
  3467. if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move before func
  3468. for(list=0; list<2; list++){
  3469. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  3470. if(get_bits1(&s->gb)){
  3471. int pred= h->curr_pic_num;
  3472. for(index=0; ; index++){
  3473. int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
  3474. int pic_id;
  3475. int i;
  3476. Picture *ref = NULL;
  3477. if(reordering_of_pic_nums_idc==3)
  3478. break;
  3479. if(index >= h->ref_count[list]){
  3480. av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
  3481. return -1;
  3482. }
  3483. if(reordering_of_pic_nums_idc<3){
  3484. if(reordering_of_pic_nums_idc<2){
  3485. const int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  3486. if(abs_diff_pic_num >= h->max_pic_num){
  3487. av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  3488. return -1;
  3489. }
  3490. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  3491. else pred+= abs_diff_pic_num;
  3492. pred &= h->max_pic_num - 1;
  3493. for(i= h->short_ref_count-1; i>=0; i--){
  3494. ref = h->short_ref[i];
  3495. assert(ref->reference == 3);
  3496. assert(!ref->long_ref);
  3497. if(ref->data[0] != NULL && ref->frame_num == pred && ref->long_ref == 0) // ignore non existing pictures by testing data[0] pointer
  3498. break;
  3499. }
  3500. if(i>=0)
  3501. ref->pic_id= ref->frame_num;
  3502. }else{
  3503. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  3504. ref = h->long_ref[pic_id];
  3505. ref->pic_id= pic_id;
  3506. assert(ref->reference == 3);
  3507. assert(ref->long_ref);
  3508. i=0;
  3509. }
  3510. if (i < 0) {
  3511. av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  3512. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  3513. } else {
  3514. for(i=index; i+1<h->ref_count[list]; i++){
  3515. if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
  3516. break;
  3517. }
  3518. for(; i > index; i--){
  3519. h->ref_list[list][i]= h->ref_list[list][i-1];
  3520. }
  3521. h->ref_list[list][index]= *ref;
  3522. }
  3523. }else{
  3524. av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  3525. return -1;
  3526. }
  3527. }
  3528. }
  3529. if(h->slice_type!=B_TYPE) break;
  3530. }
  3531. for(list=0; list<2; list++){
  3532. for(index= 0; index < h->ref_count[list]; index++){
  3533. if(!h->ref_list[list][index].data[0])
  3534. h->ref_list[list][index]= s->current_picture;
  3535. }
  3536. if(h->slice_type!=B_TYPE) break;
  3537. }
  3538. if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)
  3539. direct_dist_scale_factor(h);
  3540. direct_ref_list_init(h);
  3541. return 0;
  3542. }
  3543. static void fill_mbaff_ref_list(H264Context *h){
  3544. int list, i, j;
  3545. for(list=0; list<2; list++){
  3546. for(i=0; i<h->ref_count[list]; i++){
  3547. Picture *frame = &h->ref_list[list][i];
  3548. Picture *field = &h->ref_list[list][16+2*i];
  3549. field[0] = *frame;
  3550. for(j=0; j<3; j++)
  3551. field[0].linesize[j] <<= 1;
  3552. field[1] = field[0];
  3553. for(j=0; j<3; j++)
  3554. field[1].data[j] += frame->linesize[j];
  3555. h->luma_weight[list][16+2*i] = h->luma_weight[list][16+2*i+1] = h->luma_weight[list][i];
  3556. h->luma_offset[list][16+2*i] = h->luma_offset[list][16+2*i+1] = h->luma_offset[list][i];
  3557. for(j=0; j<2; j++){
  3558. h->chroma_weight[list][16+2*i][j] = h->chroma_weight[list][16+2*i+1][j] = h->chroma_weight[list][i][j];
  3559. h->chroma_offset[list][16+2*i][j] = h->chroma_offset[list][16+2*i+1][j] = h->chroma_offset[list][i][j];
  3560. }
  3561. }
  3562. }
  3563. for(j=0; j<h->ref_count[1]; j++){
  3564. for(i=0; i<h->ref_count[0]; i++)
  3565. h->implicit_weight[j][16+2*i] = h->implicit_weight[j][16+2*i+1] = h->implicit_weight[j][i];
  3566. memcpy(h->implicit_weight[16+2*j], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3567. memcpy(h->implicit_weight[16+2*j+1], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3568. }
  3569. }
  3570. static int pred_weight_table(H264Context *h){
  3571. MpegEncContext * const s = &h->s;
  3572. int list, i;
  3573. int luma_def, chroma_def;
  3574. h->use_weight= 0;
  3575. h->use_weight_chroma= 0;
  3576. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  3577. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  3578. luma_def = 1<<h->luma_log2_weight_denom;
  3579. chroma_def = 1<<h->chroma_log2_weight_denom;
  3580. for(list=0; list<2; list++){
  3581. for(i=0; i<h->ref_count[list]; i++){
  3582. int luma_weight_flag, chroma_weight_flag;
  3583. luma_weight_flag= get_bits1(&s->gb);
  3584. if(luma_weight_flag){
  3585. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  3586. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  3587. if( h->luma_weight[list][i] != luma_def
  3588. || h->luma_offset[list][i] != 0)
  3589. h->use_weight= 1;
  3590. }else{
  3591. h->luma_weight[list][i]= luma_def;
  3592. h->luma_offset[list][i]= 0;
  3593. }
  3594. chroma_weight_flag= get_bits1(&s->gb);
  3595. if(chroma_weight_flag){
  3596. int j;
  3597. for(j=0; j<2; j++){
  3598. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  3599. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  3600. if( h->chroma_weight[list][i][j] != chroma_def
  3601. || h->chroma_offset[list][i][j] != 0)
  3602. h->use_weight_chroma= 1;
  3603. }
  3604. }else{
  3605. int j;
  3606. for(j=0; j<2; j++){
  3607. h->chroma_weight[list][i][j]= chroma_def;
  3608. h->chroma_offset[list][i][j]= 0;
  3609. }
  3610. }
  3611. }
  3612. if(h->slice_type != B_TYPE) break;
  3613. }
  3614. h->use_weight= h->use_weight || h->use_weight_chroma;
  3615. return 0;
  3616. }
  3617. static void implicit_weight_table(H264Context *h){
  3618. MpegEncContext * const s = &h->s;
  3619. int ref0, ref1;
  3620. int cur_poc = s->current_picture_ptr->poc;
  3621. if( h->ref_count[0] == 1 && h->ref_count[1] == 1
  3622. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  3623. h->use_weight= 0;
  3624. h->use_weight_chroma= 0;
  3625. return;
  3626. }
  3627. h->use_weight= 2;
  3628. h->use_weight_chroma= 2;
  3629. h->luma_log2_weight_denom= 5;
  3630. h->chroma_log2_weight_denom= 5;
  3631. for(ref0=0; ref0 < h->ref_count[0]; ref0++){
  3632. int poc0 = h->ref_list[0][ref0].poc;
  3633. for(ref1=0; ref1 < h->ref_count[1]; ref1++){
  3634. int poc1 = h->ref_list[1][ref1].poc;
  3635. int td = clip(poc1 - poc0, -128, 127);
  3636. if(td){
  3637. int tb = clip(cur_poc - poc0, -128, 127);
  3638. int tx = (16384 + (FFABS(td) >> 1)) / td;
  3639. int dist_scale_factor = clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
  3640. if(dist_scale_factor < -64 || dist_scale_factor > 128)
  3641. h->implicit_weight[ref0][ref1] = 32;
  3642. else
  3643. h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
  3644. }else
  3645. h->implicit_weight[ref0][ref1] = 32;
  3646. }
  3647. }
  3648. }
  3649. static inline void unreference_pic(H264Context *h, Picture *pic){
  3650. int i;
  3651. pic->reference=0;
  3652. if(pic == h->delayed_output_pic)
  3653. pic->reference=1;
  3654. else{
  3655. for(i = 0; h->delayed_pic[i]; i++)
  3656. if(pic == h->delayed_pic[i]){
  3657. pic->reference=1;
  3658. break;
  3659. }
  3660. }
  3661. }
  3662. /**
  3663. * instantaneous decoder refresh.
  3664. */
  3665. static void idr(H264Context *h){
  3666. int i;
  3667. for(i=0; i<16; i++){
  3668. if (h->long_ref[i] != NULL) {
  3669. unreference_pic(h, h->long_ref[i]);
  3670. h->long_ref[i]= NULL;
  3671. }
  3672. }
  3673. h->long_ref_count=0;
  3674. for(i=0; i<h->short_ref_count; i++){
  3675. unreference_pic(h, h->short_ref[i]);
  3676. h->short_ref[i]= NULL;
  3677. }
  3678. h->short_ref_count=0;
  3679. }
  3680. /* forget old pics after a seek */
  3681. static void flush_dpb(AVCodecContext *avctx){
  3682. H264Context *h= avctx->priv_data;
  3683. int i;
  3684. for(i=0; i<16; i++) {
  3685. if(h->delayed_pic[i])
  3686. h->delayed_pic[i]->reference= 0;
  3687. h->delayed_pic[i]= NULL;
  3688. }
  3689. if(h->delayed_output_pic)
  3690. h->delayed_output_pic->reference= 0;
  3691. h->delayed_output_pic= NULL;
  3692. idr(h);
  3693. if(h->s.current_picture_ptr)
  3694. h->s.current_picture_ptr->reference= 0;
  3695. }
  3696. /**
  3697. *
  3698. * @return the removed picture or NULL if an error occurs
  3699. */
  3700. static Picture * remove_short(H264Context *h, int frame_num){
  3701. MpegEncContext * const s = &h->s;
  3702. int i;
  3703. if(s->avctx->debug&FF_DEBUG_MMCO)
  3704. av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  3705. for(i=0; i<h->short_ref_count; i++){
  3706. Picture *pic= h->short_ref[i];
  3707. if(s->avctx->debug&FF_DEBUG_MMCO)
  3708. av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  3709. if(pic->frame_num == frame_num){
  3710. h->short_ref[i]= NULL;
  3711. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
  3712. h->short_ref_count--;
  3713. return pic;
  3714. }
  3715. }
  3716. return NULL;
  3717. }
  3718. /**
  3719. *
  3720. * @return the removed picture or NULL if an error occurs
  3721. */
  3722. static Picture * remove_long(H264Context *h, int i){
  3723. Picture *pic;
  3724. pic= h->long_ref[i];
  3725. h->long_ref[i]= NULL;
  3726. if(pic) h->long_ref_count--;
  3727. return pic;
  3728. }
  3729. /**
  3730. * print short term list
  3731. */
  3732. static void print_short_term(H264Context *h) {
  3733. uint32_t i;
  3734. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3735. av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
  3736. for(i=0; i<h->short_ref_count; i++){
  3737. Picture *pic= h->short_ref[i];
  3738. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3739. }
  3740. }
  3741. }
  3742. /**
  3743. * print long term list
  3744. */
  3745. static void print_long_term(H264Context *h) {
  3746. uint32_t i;
  3747. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3748. av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
  3749. for(i = 0; i < 16; i++){
  3750. Picture *pic= h->long_ref[i];
  3751. if (pic) {
  3752. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3753. }
  3754. }
  3755. }
  3756. }
  3757. /**
  3758. * Executes the reference picture marking (memory management control operations).
  3759. */
  3760. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  3761. MpegEncContext * const s = &h->s;
  3762. int i, j;
  3763. int current_is_long=0;
  3764. Picture *pic;
  3765. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  3766. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  3767. for(i=0; i<mmco_count; i++){
  3768. if(s->avctx->debug&FF_DEBUG_MMCO)
  3769. 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);
  3770. switch(mmco[i].opcode){
  3771. case MMCO_SHORT2UNUSED:
  3772. pic= remove_short(h, mmco[i].short_frame_num);
  3773. if(pic)
  3774. unreference_pic(h, pic);
  3775. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3776. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_short() failure\n");
  3777. break;
  3778. case MMCO_SHORT2LONG:
  3779. pic= remove_long(h, mmco[i].long_index);
  3780. if(pic) unreference_pic(h, pic);
  3781. h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
  3782. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3783. h->long_ref_count++;
  3784. break;
  3785. case MMCO_LONG2UNUSED:
  3786. pic= remove_long(h, mmco[i].long_index);
  3787. if(pic)
  3788. unreference_pic(h, pic);
  3789. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3790. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_long() failure\n");
  3791. break;
  3792. case MMCO_LONG:
  3793. pic= remove_long(h, mmco[i].long_index);
  3794. if(pic) unreference_pic(h, pic);
  3795. h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
  3796. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3797. h->long_ref_count++;
  3798. current_is_long=1;
  3799. break;
  3800. case MMCO_SET_MAX_LONG:
  3801. assert(mmco[i].long_index <= 16);
  3802. // just remove the long term which index is greater than new max
  3803. for(j = mmco[i].long_index; j<16; j++){
  3804. pic = remove_long(h, j);
  3805. if (pic) unreference_pic(h, pic);
  3806. }
  3807. break;
  3808. case MMCO_RESET:
  3809. while(h->short_ref_count){
  3810. pic= remove_short(h, h->short_ref[0]->frame_num);
  3811. unreference_pic(h, pic);
  3812. }
  3813. for(j = 0; j < 16; j++) {
  3814. pic= remove_long(h, j);
  3815. if(pic) unreference_pic(h, pic);
  3816. }
  3817. break;
  3818. default: assert(0);
  3819. }
  3820. }
  3821. if(!current_is_long){
  3822. pic= remove_short(h, s->current_picture_ptr->frame_num);
  3823. if(pic){
  3824. unreference_pic(h, pic);
  3825. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  3826. }
  3827. if(h->short_ref_count)
  3828. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  3829. h->short_ref[0]= s->current_picture_ptr;
  3830. h->short_ref[0]->long_ref=0;
  3831. h->short_ref_count++;
  3832. }
  3833. print_short_term(h);
  3834. print_long_term(h);
  3835. return 0;
  3836. }
  3837. static int decode_ref_pic_marking(H264Context *h){
  3838. MpegEncContext * const s = &h->s;
  3839. int i;
  3840. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  3841. s->broken_link= get_bits1(&s->gb) -1;
  3842. h->mmco[0].long_index= get_bits1(&s->gb) - 1; // current_long_term_idx
  3843. if(h->mmco[0].long_index == -1)
  3844. h->mmco_index= 0;
  3845. else{
  3846. h->mmco[0].opcode= MMCO_LONG;
  3847. h->mmco_index= 1;
  3848. }
  3849. }else{
  3850. if(get_bits1(&s->gb)){ // adaptive_ref_pic_marking_mode_flag
  3851. for(i= 0; i<MAX_MMCO_COUNT; i++) {
  3852. MMCOOpcode opcode= get_ue_golomb(&s->gb);;
  3853. h->mmco[i].opcode= opcode;
  3854. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  3855. 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
  3856. /* if(h->mmco[i].short_frame_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_frame_num ] == NULL){
  3857. av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
  3858. return -1;
  3859. }*/
  3860. }
  3861. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  3862. h->mmco[i].long_index= get_ue_golomb(&s->gb);
  3863. if(/*h->mmco[i].long_index >= h->long_ref_count || h->long_ref[ h->mmco[i].long_index ] == NULL*/ h->mmco[i].long_index >= 16){
  3864. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  3865. return -1;
  3866. }
  3867. }
  3868. if(opcode > MMCO_LONG){
  3869. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  3870. return -1;
  3871. }
  3872. if(opcode == MMCO_END)
  3873. break;
  3874. }
  3875. h->mmco_index= i;
  3876. }else{
  3877. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  3878. if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){ //FIXME fields
  3879. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  3880. h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  3881. h->mmco_index= 1;
  3882. }else
  3883. h->mmco_index= 0;
  3884. }
  3885. }
  3886. return 0;
  3887. }
  3888. static int init_poc(H264Context *h){
  3889. MpegEncContext * const s = &h->s;
  3890. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  3891. int field_poc[2];
  3892. if(h->nal_unit_type == NAL_IDR_SLICE){
  3893. h->frame_num_offset= 0;
  3894. }else{
  3895. if(h->frame_num < h->prev_frame_num)
  3896. h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
  3897. else
  3898. h->frame_num_offset= h->prev_frame_num_offset;
  3899. }
  3900. if(h->sps.poc_type==0){
  3901. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  3902. if(h->nal_unit_type == NAL_IDR_SLICE){
  3903. h->prev_poc_msb=
  3904. h->prev_poc_lsb= 0;
  3905. }
  3906. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  3907. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  3908. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  3909. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  3910. else
  3911. h->poc_msb = h->prev_poc_msb;
  3912. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  3913. field_poc[0] =
  3914. field_poc[1] = h->poc_msb + h->poc_lsb;
  3915. if(s->picture_structure == PICT_FRAME)
  3916. field_poc[1] += h->delta_poc_bottom;
  3917. }else if(h->sps.poc_type==1){
  3918. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  3919. int i;
  3920. if(h->sps.poc_cycle_length != 0)
  3921. abs_frame_num = h->frame_num_offset + h->frame_num;
  3922. else
  3923. abs_frame_num = 0;
  3924. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  3925. abs_frame_num--;
  3926. expected_delta_per_poc_cycle = 0;
  3927. for(i=0; i < h->sps.poc_cycle_length; i++)
  3928. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  3929. if(abs_frame_num > 0){
  3930. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  3931. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  3932. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  3933. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  3934. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  3935. } else
  3936. expectedpoc = 0;
  3937. if(h->nal_ref_idc == 0)
  3938. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  3939. field_poc[0] = expectedpoc + h->delta_poc[0];
  3940. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  3941. if(s->picture_structure == PICT_FRAME)
  3942. field_poc[1] += h->delta_poc[1];
  3943. }else{
  3944. int poc;
  3945. if(h->nal_unit_type == NAL_IDR_SLICE){
  3946. poc= 0;
  3947. }else{
  3948. if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
  3949. else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
  3950. }
  3951. field_poc[0]= poc;
  3952. field_poc[1]= poc;
  3953. }
  3954. if(s->picture_structure != PICT_BOTTOM_FIELD)
  3955. s->current_picture_ptr->field_poc[0]= field_poc[0];
  3956. if(s->picture_structure != PICT_TOP_FIELD)
  3957. s->current_picture_ptr->field_poc[1]= field_poc[1];
  3958. if(s->picture_structure == PICT_FRAME) // FIXME field pix?
  3959. s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]);
  3960. return 0;
  3961. }
  3962. /**
  3963. * decodes a slice header.
  3964. * this will allso call MPV_common_init() and frame_start() as needed
  3965. */
  3966. static int decode_slice_header(H264Context *h){
  3967. MpegEncContext * const s = &h->s;
  3968. int first_mb_in_slice, pps_id;
  3969. int num_ref_idx_active_override_flag;
  3970. static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
  3971. int slice_type;
  3972. int default_ref_list_done = 0;
  3973. s->current_picture.reference= h->nal_ref_idc != 0;
  3974. s->dropable= h->nal_ref_idc == 0;
  3975. first_mb_in_slice= get_ue_golomb(&s->gb);
  3976. slice_type= get_ue_golomb(&s->gb);
  3977. if(slice_type > 9){
  3978. 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);
  3979. return -1;
  3980. }
  3981. if(slice_type > 4){
  3982. slice_type -= 5;
  3983. h->slice_type_fixed=1;
  3984. }else
  3985. h->slice_type_fixed=0;
  3986. slice_type= slice_type_map[ slice_type ];
  3987. if (slice_type == I_TYPE
  3988. || (h->slice_num != 0 && slice_type == h->slice_type) ) {
  3989. default_ref_list_done = 1;
  3990. }
  3991. h->slice_type= slice_type;
  3992. s->pict_type= h->slice_type; // to make a few old func happy, it's wrong though
  3993. pps_id= get_ue_golomb(&s->gb);
  3994. if(pps_id>255){
  3995. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  3996. return -1;
  3997. }
  3998. h->pps= h->pps_buffer[pps_id];
  3999. if(h->pps.slice_group_count == 0){
  4000. av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referenced\n");
  4001. return -1;
  4002. }
  4003. h->sps= h->sps_buffer[ h->pps.sps_id ];
  4004. if(h->sps.log2_max_frame_num == 0){
  4005. av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referenced\n");
  4006. return -1;
  4007. }
  4008. if(h->dequant_coeff_pps != pps_id){
  4009. h->dequant_coeff_pps = pps_id;
  4010. init_dequant_tables(h);
  4011. }
  4012. s->mb_width= h->sps.mb_width;
  4013. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  4014. h->b_stride= s->mb_width*4;
  4015. h->b8_stride= s->mb_width*2;
  4016. s->width = 16*s->mb_width - 2*(h->sps.crop_left + h->sps.crop_right );
  4017. if(h->sps.frame_mbs_only_flag)
  4018. s->height= 16*s->mb_height - 2*(h->sps.crop_top + h->sps.crop_bottom);
  4019. else
  4020. s->height= 16*s->mb_height - 4*(h->sps.crop_top + h->sps.crop_bottom); //FIXME recheck
  4021. if (s->context_initialized
  4022. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  4023. free_tables(h);
  4024. MPV_common_end(s);
  4025. }
  4026. if (!s->context_initialized) {
  4027. if (MPV_common_init(s) < 0)
  4028. return -1;
  4029. if(s->dsp.h264_idct_add == ff_h264_idct_add_c){ //FIXME little ugly
  4030. memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
  4031. memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
  4032. }else{
  4033. int i;
  4034. for(i=0; i<16; i++){
  4035. #define T(x) (x>>2) | ((x<<2) & 0xF)
  4036. h->zigzag_scan[i] = T(zigzag_scan[i]);
  4037. h-> field_scan[i] = T( field_scan[i]);
  4038. #undef T
  4039. }
  4040. }
  4041. if(s->dsp.h264_idct8_add == ff_h264_idct8_add_c){
  4042. memcpy(h->zigzag_scan8x8, zigzag_scan8x8, 64*sizeof(uint8_t));
  4043. memcpy(h->zigzag_scan8x8_cavlc, zigzag_scan8x8_cavlc, 64*sizeof(uint8_t));
  4044. memcpy(h->field_scan8x8, field_scan8x8, 64*sizeof(uint8_t));
  4045. memcpy(h->field_scan8x8_cavlc, field_scan8x8_cavlc, 64*sizeof(uint8_t));
  4046. }else{
  4047. int i;
  4048. for(i=0; i<64; i++){
  4049. #define T(x) (x>>3) | ((x&7)<<3)
  4050. h->zigzag_scan8x8[i] = T(zigzag_scan8x8[i]);
  4051. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  4052. h->field_scan8x8[i] = T(field_scan8x8[i]);
  4053. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  4054. #undef T
  4055. }
  4056. }
  4057. if(h->sps.transform_bypass){ //FIXME same ugly
  4058. h->zigzag_scan_q0 = zigzag_scan;
  4059. h->zigzag_scan8x8_q0 = zigzag_scan8x8;
  4060. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  4061. h->field_scan_q0 = field_scan;
  4062. h->field_scan8x8_q0 = field_scan8x8;
  4063. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  4064. }else{
  4065. h->zigzag_scan_q0 = h->zigzag_scan;
  4066. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  4067. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  4068. h->field_scan_q0 = h->field_scan;
  4069. h->field_scan8x8_q0 = h->field_scan8x8;
  4070. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  4071. }
  4072. alloc_tables(h);
  4073. s->avctx->width = s->width;
  4074. s->avctx->height = s->height;
  4075. s->avctx->sample_aspect_ratio= h->sps.sar;
  4076. if(!s->avctx->sample_aspect_ratio.den)
  4077. s->avctx->sample_aspect_ratio.den = 1;
  4078. if(h->sps.timing_info_present_flag){
  4079. s->avctx->time_base= (AVRational){h->sps.num_units_in_tick * 2, h->sps.time_scale};
  4080. if(h->x264_build > 0 && h->x264_build < 44)
  4081. s->avctx->time_base.den *= 2;
  4082. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  4083. s->avctx->time_base.num, s->avctx->time_base.den, 1<<30);
  4084. }
  4085. }
  4086. if(h->slice_num == 0){
  4087. if(frame_start(h) < 0)
  4088. return -1;
  4089. }
  4090. s->current_picture_ptr->frame_num= //FIXME frame_num cleanup
  4091. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  4092. h->mb_mbaff = 0;
  4093. h->mb_aff_frame = 0;
  4094. if(h->sps.frame_mbs_only_flag){
  4095. s->picture_structure= PICT_FRAME;
  4096. }else{
  4097. if(get_bits1(&s->gb)) { //field_pic_flag
  4098. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  4099. av_log(h->s.avctx, AV_LOG_ERROR, "PAFF interlacing is not implemented\n");
  4100. } else {
  4101. s->picture_structure= PICT_FRAME;
  4102. h->mb_aff_frame = h->sps.mb_aff;
  4103. }
  4104. }
  4105. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  4106. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << h->mb_aff_frame;
  4107. if(s->mb_y >= s->mb_height){
  4108. return -1;
  4109. }
  4110. if(s->picture_structure==PICT_FRAME){
  4111. h->curr_pic_num= h->frame_num;
  4112. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  4113. }else{
  4114. h->curr_pic_num= 2*h->frame_num;
  4115. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  4116. }
  4117. if(h->nal_unit_type == NAL_IDR_SLICE){
  4118. get_ue_golomb(&s->gb); /* idr_pic_id */
  4119. }
  4120. if(h->sps.poc_type==0){
  4121. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  4122. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  4123. h->delta_poc_bottom= get_se_golomb(&s->gb);
  4124. }
  4125. }
  4126. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  4127. h->delta_poc[0]= get_se_golomb(&s->gb);
  4128. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  4129. h->delta_poc[1]= get_se_golomb(&s->gb);
  4130. }
  4131. init_poc(h);
  4132. if(h->pps.redundant_pic_cnt_present){
  4133. h->redundant_pic_count= get_ue_golomb(&s->gb);
  4134. }
  4135. //set defaults, might be overriden a few line later
  4136. h->ref_count[0]= h->pps.ref_count[0];
  4137. h->ref_count[1]= h->pps.ref_count[1];
  4138. if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){
  4139. if(h->slice_type == B_TYPE){
  4140. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  4141. if(h->sps.mb_aff && h->direct_spatial_mv_pred)
  4142. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + spatial direct mode is not implemented\n");
  4143. }
  4144. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  4145. if(num_ref_idx_active_override_flag){
  4146. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  4147. if(h->slice_type==B_TYPE)
  4148. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  4149. if(h->ref_count[0] > 32 || h->ref_count[1] > 32){
  4150. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  4151. return -1;
  4152. }
  4153. }
  4154. }
  4155. if(!default_ref_list_done){
  4156. fill_default_ref_list(h);
  4157. }
  4158. if(decode_ref_pic_list_reordering(h) < 0)
  4159. return -1;
  4160. if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
  4161. || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
  4162. pred_weight_table(h);
  4163. else if(h->pps.weighted_bipred_idc==2 && h->slice_type==B_TYPE)
  4164. implicit_weight_table(h);
  4165. else
  4166. h->use_weight = 0;
  4167. if(s->current_picture.reference)
  4168. decode_ref_pic_marking(h);
  4169. if(FRAME_MBAFF)
  4170. fill_mbaff_ref_list(h);
  4171. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac )
  4172. h->cabac_init_idc = get_ue_golomb(&s->gb);
  4173. h->last_qscale_diff = 0;
  4174. s->qscale = h->pps.init_qp + get_se_golomb(&s->gb);
  4175. if(s->qscale<0 || s->qscale>51){
  4176. av_log(s->avctx, AV_LOG_ERROR, "QP %d out of range\n", s->qscale);
  4177. return -1;
  4178. }
  4179. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4180. //FIXME qscale / qp ... stuff
  4181. if(h->slice_type == SP_TYPE){
  4182. get_bits1(&s->gb); /* sp_for_switch_flag */
  4183. }
  4184. if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
  4185. get_se_golomb(&s->gb); /* slice_qs_delta */
  4186. }
  4187. h->deblocking_filter = 1;
  4188. h->slice_alpha_c0_offset = 0;
  4189. h->slice_beta_offset = 0;
  4190. if( h->pps.deblocking_filter_parameters_present ) {
  4191. h->deblocking_filter= get_ue_golomb(&s->gb);
  4192. if(h->deblocking_filter < 2)
  4193. h->deblocking_filter^= 1; // 1<->0
  4194. if( h->deblocking_filter ) {
  4195. h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
  4196. h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
  4197. }
  4198. }
  4199. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  4200. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type != I_TYPE)
  4201. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type == B_TYPE)
  4202. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  4203. h->deblocking_filter= 0;
  4204. #if 0 //FMO
  4205. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  4206. slice_group_change_cycle= get_bits(&s->gb, ?);
  4207. #endif
  4208. h->slice_num++;
  4209. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
  4210. h->emu_edge_height= FRAME_MBAFF ? 0 : h->emu_edge_width;
  4211. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  4212. av_log(h->s.avctx, AV_LOG_DEBUG, "slice:%d %s mb:%d %c pps:%d frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s\n",
  4213. h->slice_num,
  4214. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  4215. first_mb_in_slice,
  4216. av_get_pict_type_char(h->slice_type),
  4217. pps_id, h->frame_num,
  4218. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  4219. h->ref_count[0], h->ref_count[1],
  4220. s->qscale,
  4221. h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2,
  4222. h->use_weight,
  4223. h->use_weight==1 && h->use_weight_chroma ? "c" : ""
  4224. );
  4225. }
  4226. if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !s->current_picture.reference){
  4227. s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
  4228. s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
  4229. }else{
  4230. s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
  4231. s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
  4232. }
  4233. return 0;
  4234. }
  4235. /**
  4236. *
  4237. */
  4238. static inline int get_level_prefix(GetBitContext *gb){
  4239. unsigned int buf;
  4240. int log;
  4241. OPEN_READER(re, gb);
  4242. UPDATE_CACHE(re, gb);
  4243. buf=GET_CACHE(re, gb);
  4244. log= 32 - av_log2(buf);
  4245. #ifdef TRACE
  4246. print_bin(buf>>(32-log), log);
  4247. 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__);
  4248. #endif
  4249. LAST_SKIP_BITS(re, gb, log);
  4250. CLOSE_READER(re, gb);
  4251. return log-1;
  4252. }
  4253. static inline int get_dct8x8_allowed(H264Context *h){
  4254. int i;
  4255. for(i=0; i<4; i++){
  4256. if(!IS_SUB_8X8(h->sub_mb_type[i])
  4257. || (!h->sps.direct_8x8_inference_flag && IS_DIRECT(h->sub_mb_type[i])))
  4258. return 0;
  4259. }
  4260. return 1;
  4261. }
  4262. /**
  4263. * decodes a residual block.
  4264. * @param n block index
  4265. * @param scantable scantable
  4266. * @param max_coeff number of coefficients in the block
  4267. * @return <0 if an error occured
  4268. */
  4269. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
  4270. MpegEncContext * const s = &h->s;
  4271. 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};
  4272. int level[16];
  4273. int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
  4274. //FIXME put trailing_onex into the context
  4275. if(n == CHROMA_DC_BLOCK_INDEX){
  4276. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  4277. total_coeff= coeff_token>>2;
  4278. }else{
  4279. if(n == LUMA_DC_BLOCK_INDEX){
  4280. total_coeff= pred_non_zero_count(h, 0);
  4281. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4282. total_coeff= coeff_token>>2;
  4283. }else{
  4284. total_coeff= pred_non_zero_count(h, n);
  4285. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4286. total_coeff= coeff_token>>2;
  4287. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  4288. }
  4289. }
  4290. //FIXME set last_non_zero?
  4291. if(total_coeff==0)
  4292. return 0;
  4293. if(total_coeff<0) {
  4294. av_log(h->s.avctx, AV_LOG_ERROR, "corrupted macroblock %d %d (total_coeff<0)\n", s->mb_x, s->mb_y);
  4295. return -1;
  4296. }
  4297. trailing_ones= coeff_token&3;
  4298. tprintf("trailing:%d, total:%d\n", trailing_ones, total_coeff);
  4299. assert(total_coeff<=16);
  4300. for(i=0; i<trailing_ones; i++){
  4301. level[i]= 1 - 2*get_bits1(gb);
  4302. }
  4303. if(i<total_coeff) {
  4304. int level_code, mask;
  4305. int suffix_length = total_coeff > 10 && trailing_ones < 3;
  4306. int prefix= get_level_prefix(gb);
  4307. //first coefficient has suffix_length equal to 0 or 1
  4308. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  4309. if(suffix_length)
  4310. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4311. else
  4312. level_code= (prefix<<suffix_length); //part
  4313. }else if(prefix==14){
  4314. if(suffix_length)
  4315. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4316. else
  4317. level_code= prefix + get_bits(gb, 4); //part
  4318. }else if(prefix==15){
  4319. level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part
  4320. if(suffix_length==0) level_code+=15; //FIXME doesn't make (much)sense
  4321. }else{
  4322. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4323. return -1;
  4324. }
  4325. if(trailing_ones < 3) level_code += 2;
  4326. suffix_length = 1;
  4327. if(level_code > 5)
  4328. suffix_length++;
  4329. mask= -(level_code&1);
  4330. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4331. i++;
  4332. //remaining coefficients have suffix_length > 0
  4333. for(;i<total_coeff;i++) {
  4334. static const int suffix_limit[7] = {0,5,11,23,47,95,INT_MAX };
  4335. prefix = get_level_prefix(gb);
  4336. if(prefix<15){
  4337. level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
  4338. }else if(prefix==15){
  4339. level_code = (prefix<<suffix_length) + get_bits(gb, 12);
  4340. }else{
  4341. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4342. return -1;
  4343. }
  4344. mask= -(level_code&1);
  4345. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4346. if(level_code > suffix_limit[suffix_length])
  4347. suffix_length++;
  4348. }
  4349. }
  4350. if(total_coeff == max_coeff)
  4351. zeros_left=0;
  4352. else{
  4353. if(n == CHROMA_DC_BLOCK_INDEX)
  4354. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  4355. else
  4356. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  4357. }
  4358. coeff_num = zeros_left + total_coeff - 1;
  4359. j = scantable[coeff_num];
  4360. if(n > 24){
  4361. block[j] = level[0];
  4362. for(i=1;i<total_coeff;i++) {
  4363. if(zeros_left <= 0)
  4364. run_before = 0;
  4365. else if(zeros_left < 7){
  4366. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4367. }else{
  4368. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4369. }
  4370. zeros_left -= run_before;
  4371. coeff_num -= 1 + run_before;
  4372. j= scantable[ coeff_num ];
  4373. block[j]= level[i];
  4374. }
  4375. }else{
  4376. block[j] = (level[0] * qmul[j] + 32)>>6;
  4377. for(i=1;i<total_coeff;i++) {
  4378. if(zeros_left <= 0)
  4379. run_before = 0;
  4380. else if(zeros_left < 7){
  4381. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4382. }else{
  4383. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4384. }
  4385. zeros_left -= run_before;
  4386. coeff_num -= 1 + run_before;
  4387. j= scantable[ coeff_num ];
  4388. block[j]= (level[i] * qmul[j] + 32)>>6;
  4389. }
  4390. }
  4391. if(zeros_left<0){
  4392. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  4393. return -1;
  4394. }
  4395. return 0;
  4396. }
  4397. static void predict_field_decoding_flag(H264Context *h){
  4398. MpegEncContext * const s = &h->s;
  4399. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4400. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  4401. ? s->current_picture.mb_type[mb_xy-1]
  4402. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  4403. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  4404. : 0;
  4405. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  4406. }
  4407. /**
  4408. * decodes a P_SKIP or B_SKIP macroblock
  4409. */
  4410. static void decode_mb_skip(H264Context *h){
  4411. MpegEncContext * const s = &h->s;
  4412. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4413. int mb_type=0;
  4414. memset(h->non_zero_count[mb_xy], 0, 16);
  4415. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  4416. if(MB_FIELD)
  4417. mb_type|= MB_TYPE_INTERLACED;
  4418. if( h->slice_type == B_TYPE )
  4419. {
  4420. // just for fill_caches. pred_direct_motion will set the real mb_type
  4421. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
  4422. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4423. pred_direct_motion(h, &mb_type);
  4424. mb_type|= MB_TYPE_SKIP;
  4425. }
  4426. else
  4427. {
  4428. int mx, my;
  4429. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
  4430. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4431. pred_pskip_motion(h, &mx, &my);
  4432. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  4433. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  4434. }
  4435. write_back_motion(h, mb_type);
  4436. s->current_picture.mb_type[mb_xy]= mb_type;
  4437. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4438. h->slice_table[ mb_xy ]= h->slice_num;
  4439. h->prev_mb_skipped= 1;
  4440. }
  4441. /**
  4442. * decodes a macroblock
  4443. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  4444. */
  4445. static int decode_mb_cavlc(H264Context *h){
  4446. MpegEncContext * const s = &h->s;
  4447. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4448. int mb_type, partition_count, cbp;
  4449. int dct8x8_allowed= h->pps.transform_8x8_mode;
  4450. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?
  4451. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  4452. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  4453. down the code */
  4454. if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){
  4455. if(s->mb_skip_run==-1)
  4456. s->mb_skip_run= get_ue_golomb(&s->gb);
  4457. if (s->mb_skip_run--) {
  4458. if(FRAME_MBAFF && (s->mb_y&1) == 0){
  4459. if(s->mb_skip_run==0)
  4460. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4461. else
  4462. predict_field_decoding_flag(h);
  4463. }
  4464. decode_mb_skip(h);
  4465. return 0;
  4466. }
  4467. }
  4468. if(FRAME_MBAFF){
  4469. if( (s->mb_y&1) == 0 )
  4470. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4471. }else
  4472. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  4473. h->prev_mb_skipped= 0;
  4474. mb_type= get_ue_golomb(&s->gb);
  4475. if(h->slice_type == B_TYPE){
  4476. if(mb_type < 23){
  4477. partition_count= b_mb_type_info[mb_type].partition_count;
  4478. mb_type= b_mb_type_info[mb_type].type;
  4479. }else{
  4480. mb_type -= 23;
  4481. goto decode_intra_mb;
  4482. }
  4483. }else if(h->slice_type == P_TYPE /*|| h->slice_type == SP_TYPE */){
  4484. if(mb_type < 5){
  4485. partition_count= p_mb_type_info[mb_type].partition_count;
  4486. mb_type= p_mb_type_info[mb_type].type;
  4487. }else{
  4488. mb_type -= 5;
  4489. goto decode_intra_mb;
  4490. }
  4491. }else{
  4492. assert(h->slice_type == I_TYPE);
  4493. decode_intra_mb:
  4494. if(mb_type > 25){
  4495. 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);
  4496. return -1;
  4497. }
  4498. partition_count=0;
  4499. cbp= i_mb_type_info[mb_type].cbp;
  4500. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  4501. mb_type= i_mb_type_info[mb_type].type;
  4502. }
  4503. if(MB_FIELD)
  4504. mb_type |= MB_TYPE_INTERLACED;
  4505. h->slice_table[ mb_xy ]= h->slice_num;
  4506. if(IS_INTRA_PCM(mb_type)){
  4507. unsigned int x, y;
  4508. // we assume these blocks are very rare so we dont optimize it
  4509. align_get_bits(&s->gb);
  4510. // The pixels are stored in the same order as levels in h->mb array.
  4511. for(y=0; y<16; y++){
  4512. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  4513. for(x=0; x<16; x++){
  4514. tprintf("LUMA ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4515. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= get_bits(&s->gb, 8);
  4516. }
  4517. }
  4518. for(y=0; y<8; y++){
  4519. const int index= 256 + 4*(y&3) + 32*(y>>2);
  4520. for(x=0; x<8; x++){
  4521. tprintf("CHROMA U ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4522. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4523. }
  4524. }
  4525. for(y=0; y<8; y++){
  4526. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  4527. for(x=0; x<8; x++){
  4528. tprintf("CHROMA V ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4529. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4530. }
  4531. }
  4532. // In deblocking, the quantizer is 0
  4533. s->current_picture.qscale_table[mb_xy]= 0;
  4534. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  4535. // All coeffs are present
  4536. memset(h->non_zero_count[mb_xy], 16, 16);
  4537. s->current_picture.mb_type[mb_xy]= mb_type;
  4538. return 0;
  4539. }
  4540. if(MB_MBAFF){
  4541. h->ref_count[0] <<= 1;
  4542. h->ref_count[1] <<= 1;
  4543. }
  4544. fill_caches(h, mb_type, 0);
  4545. //mb_pred
  4546. if(IS_INTRA(mb_type)){
  4547. // init_top_left_availability(h);
  4548. if(IS_INTRA4x4(mb_type)){
  4549. int i;
  4550. int di = 1;
  4551. if(dct8x8_allowed && get_bits1(&s->gb)){
  4552. mb_type |= MB_TYPE_8x8DCT;
  4553. di = 4;
  4554. }
  4555. // fill_intra4x4_pred_table(h);
  4556. for(i=0; i<16; i+=di){
  4557. int mode= pred_intra_mode(h, i);
  4558. if(!get_bits1(&s->gb)){
  4559. const int rem_mode= get_bits(&s->gb, 3);
  4560. mode = rem_mode + (rem_mode >= mode);
  4561. }
  4562. if(di==4)
  4563. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  4564. else
  4565. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  4566. }
  4567. write_back_intra_pred_mode(h);
  4568. if( check_intra4x4_pred_mode(h) < 0)
  4569. return -1;
  4570. }else{
  4571. h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
  4572. if(h->intra16x16_pred_mode < 0)
  4573. return -1;
  4574. }
  4575. h->chroma_pred_mode= get_ue_golomb(&s->gb);
  4576. h->chroma_pred_mode= check_intra_pred_mode(h, h->chroma_pred_mode);
  4577. if(h->chroma_pred_mode < 0)
  4578. return -1;
  4579. }else if(partition_count==4){
  4580. int i, j, sub_partition_count[4], list, ref[2][4];
  4581. if(h->slice_type == B_TYPE){
  4582. for(i=0; i<4; i++){
  4583. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4584. if(h->sub_mb_type[i] >=13){
  4585. av_log(h->s.avctx, AV_LOG_ERROR, "B sub_mb_type %d out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
  4586. return -1;
  4587. }
  4588. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4589. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4590. }
  4591. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  4592. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
  4593. pred_direct_motion(h, &mb_type);
  4594. h->ref_cache[0][scan8[4]] =
  4595. h->ref_cache[1][scan8[4]] =
  4596. h->ref_cache[0][scan8[12]] =
  4597. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  4598. }
  4599. }else{
  4600. assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE); //FIXME SP correct ?
  4601. for(i=0; i<4; i++){
  4602. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4603. if(h->sub_mb_type[i] >=4){
  4604. av_log(h->s.avctx, AV_LOG_ERROR, "P sub_mb_type %d out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
  4605. return -1;
  4606. }
  4607. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4608. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4609. }
  4610. }
  4611. for(list=0; list<2; list++){
  4612. int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4613. if(ref_count == 0) continue;
  4614. for(i=0; i<4; i++){
  4615. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  4616. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4617. ref[list][i] = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
  4618. }else{
  4619. //FIXME
  4620. ref[list][i] = -1;
  4621. }
  4622. }
  4623. }
  4624. if(dct8x8_allowed)
  4625. dct8x8_allowed = get_dct8x8_allowed(h);
  4626. for(list=0; list<2; list++){
  4627. const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4628. if(ref_count == 0) continue;
  4629. for(i=0; i<4; i++){
  4630. if(IS_DIRECT(h->sub_mb_type[i])) {
  4631. h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];
  4632. continue;
  4633. }
  4634. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  4635. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  4636. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4637. const int sub_mb_type= h->sub_mb_type[i];
  4638. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  4639. for(j=0; j<sub_partition_count[i]; j++){
  4640. int mx, my;
  4641. const int index= 4*i + block_width*j;
  4642. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  4643. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  4644. mx += get_se_golomb(&s->gb);
  4645. my += get_se_golomb(&s->gb);
  4646. tprintf("final mv:%d %d\n", mx, my);
  4647. if(IS_SUB_8X8(sub_mb_type)){
  4648. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  4649. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  4650. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  4651. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  4652. }else if(IS_SUB_8X4(sub_mb_type)){
  4653. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  4654. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  4655. }else if(IS_SUB_4X8(sub_mb_type)){
  4656. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  4657. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  4658. }else{
  4659. assert(IS_SUB_4X4(sub_mb_type));
  4660. mv_cache[ 0 ][0]= mx;
  4661. mv_cache[ 0 ][1]= my;
  4662. }
  4663. }
  4664. }else{
  4665. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  4666. p[0] = p[1]=
  4667. p[8] = p[9]= 0;
  4668. }
  4669. }
  4670. }
  4671. }else if(IS_DIRECT(mb_type)){
  4672. pred_direct_motion(h, &mb_type);
  4673. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  4674. }else{
  4675. int list, mx, my, i;
  4676. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  4677. if(IS_16X16(mb_type)){
  4678. for(list=0; list<2; list++){
  4679. if(h->ref_count[list]>0){
  4680. if(IS_DIR(mb_type, 0, list)){
  4681. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4682. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  4683. }else
  4684. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4685. }
  4686. }
  4687. for(list=0; list<2; list++){
  4688. if(IS_DIR(mb_type, 0, list)){
  4689. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  4690. mx += get_se_golomb(&s->gb);
  4691. my += get_se_golomb(&s->gb);
  4692. tprintf("final mv:%d %d\n", mx, my);
  4693. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  4694. }else
  4695. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  4696. }
  4697. }
  4698. else if(IS_16X8(mb_type)){
  4699. for(list=0; list<2; list++){
  4700. if(h->ref_count[list]>0){
  4701. for(i=0; i<2; i++){
  4702. if(IS_DIR(mb_type, i, list)){
  4703. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4704. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  4705. }else
  4706. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  4707. }
  4708. }
  4709. }
  4710. for(list=0; list<2; list++){
  4711. for(i=0; i<2; i++){
  4712. if(IS_DIR(mb_type, i, list)){
  4713. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  4714. mx += get_se_golomb(&s->gb);
  4715. my += get_se_golomb(&s->gb);
  4716. tprintf("final mv:%d %d\n", mx, my);
  4717. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  4718. }else
  4719. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  4720. }
  4721. }
  4722. }else{
  4723. assert(IS_8X16(mb_type));
  4724. for(list=0; list<2; list++){
  4725. if(h->ref_count[list]>0){
  4726. for(i=0; i<2; i++){
  4727. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  4728. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4729. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  4730. }else
  4731. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4732. }
  4733. }
  4734. }
  4735. for(list=0; list<2; list++){
  4736. for(i=0; i<2; i++){
  4737. if(IS_DIR(mb_type, i, list)){
  4738. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  4739. mx += get_se_golomb(&s->gb);
  4740. my += get_se_golomb(&s->gb);
  4741. tprintf("final mv:%d %d\n", mx, my);
  4742. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  4743. }else
  4744. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  4745. }
  4746. }
  4747. }
  4748. }
  4749. if(IS_INTER(mb_type))
  4750. write_back_motion(h, mb_type);
  4751. if(!IS_INTRA16x16(mb_type)){
  4752. cbp= get_ue_golomb(&s->gb);
  4753. if(cbp > 47){
  4754. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%d) at %d %d\n", cbp, s->mb_x, s->mb_y);
  4755. return -1;
  4756. }
  4757. if(IS_INTRA4x4(mb_type))
  4758. cbp= golomb_to_intra4x4_cbp[cbp];
  4759. else
  4760. cbp= golomb_to_inter_cbp[cbp];
  4761. }
  4762. h->cbp = cbp;
  4763. if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
  4764. if(get_bits1(&s->gb))
  4765. mb_type |= MB_TYPE_8x8DCT;
  4766. }
  4767. s->current_picture.mb_type[mb_xy]= mb_type;
  4768. if(cbp || IS_INTRA16x16(mb_type)){
  4769. int i8x8, i4x4, chroma_idx;
  4770. int chroma_qp, dquant;
  4771. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  4772. const uint8_t *scan, *scan8x8, *dc_scan;
  4773. // fill_non_zero_count_cache(h);
  4774. if(IS_INTERLACED(mb_type)){
  4775. scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
  4776. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  4777. dc_scan= luma_dc_field_scan;
  4778. }else{
  4779. scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
  4780. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  4781. dc_scan= luma_dc_zigzag_scan;
  4782. }
  4783. dquant= get_se_golomb(&s->gb);
  4784. if( dquant > 25 || dquant < -26 ){
  4785. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  4786. return -1;
  4787. }
  4788. s->qscale += dquant;
  4789. if(((unsigned)s->qscale) > 51){
  4790. if(s->qscale<0) s->qscale+= 52;
  4791. else s->qscale-= 52;
  4792. }
  4793. h->chroma_qp= chroma_qp= get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4794. if(IS_INTRA16x16(mb_type)){
  4795. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){
  4796. return -1; //FIXME continue if partitioned and other return -1 too
  4797. }
  4798. assert((cbp&15) == 0 || (cbp&15) == 15);
  4799. if(cbp&15){
  4800. for(i8x8=0; i8x8<4; i8x8++){
  4801. for(i4x4=0; i4x4<4; i4x4++){
  4802. const int index= i4x4 + 4*i8x8;
  4803. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){
  4804. return -1;
  4805. }
  4806. }
  4807. }
  4808. }else{
  4809. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  4810. }
  4811. }else{
  4812. for(i8x8=0; i8x8<4; i8x8++){
  4813. if(cbp & (1<<i8x8)){
  4814. if(IS_8x8DCT(mb_type)){
  4815. DCTELEM *buf = &h->mb[64*i8x8];
  4816. uint8_t *nnz;
  4817. for(i4x4=0; i4x4<4; i4x4++){
  4818. if( decode_residual(h, gb, buf, i4x4+4*i8x8, scan8x8+16*i4x4,
  4819. h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )
  4820. return -1;
  4821. }
  4822. nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4823. nnz[0] += nnz[1] + nnz[8] + nnz[9];
  4824. }else{
  4825. for(i4x4=0; i4x4<4; i4x4++){
  4826. const int index= i4x4 + 4*i8x8;
  4827. if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){
  4828. return -1;
  4829. }
  4830. }
  4831. }
  4832. }else{
  4833. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4834. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  4835. }
  4836. }
  4837. }
  4838. if(cbp&0x30){
  4839. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  4840. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){
  4841. return -1;
  4842. }
  4843. }
  4844. if(cbp&0x20){
  4845. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  4846. for(i4x4=0; i4x4<4; i4x4++){
  4847. const int index= 16 + 4*chroma_idx + i4x4;
  4848. 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){
  4849. return -1;
  4850. }
  4851. }
  4852. }
  4853. }else{
  4854. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4855. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4856. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4857. }
  4858. }else{
  4859. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4860. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  4861. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4862. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4863. }
  4864. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4865. write_back_non_zero_count(h);
  4866. if(MB_MBAFF){
  4867. h->ref_count[0] >>= 1;
  4868. h->ref_count[1] >>= 1;
  4869. }
  4870. return 0;
  4871. }
  4872. static int decode_cabac_field_decoding_flag(H264Context *h) {
  4873. MpegEncContext * const s = &h->s;
  4874. const int mb_x = s->mb_x;
  4875. const int mb_y = s->mb_y & ~1;
  4876. const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
  4877. const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
  4878. unsigned int ctx = 0;
  4879. if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
  4880. ctx += 1;
  4881. }
  4882. if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
  4883. ctx += 1;
  4884. }
  4885. return get_cabac_noinline( &h->cabac, &h->cabac_state[70 + ctx] );
  4886. }
  4887. static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
  4888. uint8_t *state= &h->cabac_state[ctx_base];
  4889. int mb_type;
  4890. if(intra_slice){
  4891. MpegEncContext * const s = &h->s;
  4892. const int mba_xy = h->left_mb_xy[0];
  4893. const int mbb_xy = h->top_mb_xy;
  4894. int ctx=0;
  4895. if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
  4896. ctx++;
  4897. if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
  4898. ctx++;
  4899. if( get_cabac_noinline( &h->cabac, &state[ctx] ) == 0 )
  4900. return 0; /* I4x4 */
  4901. state += 2;
  4902. }else{
  4903. if( get_cabac_noinline( &h->cabac, &state[0] ) == 0 )
  4904. return 0; /* I4x4 */
  4905. }
  4906. if( get_cabac_terminate( &h->cabac ) )
  4907. return 25; /* PCM */
  4908. mb_type = 1; /* I16x16 */
  4909. mb_type += 12 * get_cabac_noinline( &h->cabac, &state[1] ); /* cbp_luma != 0 */
  4910. if( get_cabac_noinline( &h->cabac, &state[2] ) ) /* cbp_chroma */
  4911. mb_type += 4 + 4 * get_cabac_noinline( &h->cabac, &state[2+intra_slice] );
  4912. mb_type += 2 * get_cabac_noinline( &h->cabac, &state[3+intra_slice] );
  4913. mb_type += 1 * get_cabac_noinline( &h->cabac, &state[3+2*intra_slice] );
  4914. return mb_type;
  4915. }
  4916. static int decode_cabac_mb_type( H264Context *h ) {
  4917. MpegEncContext * const s = &h->s;
  4918. if( h->slice_type == I_TYPE ) {
  4919. return decode_cabac_intra_mb_type(h, 3, 1);
  4920. } else if( h->slice_type == P_TYPE ) {
  4921. if( get_cabac_noinline( &h->cabac, &h->cabac_state[14] ) == 0 ) {
  4922. /* P-type */
  4923. if( get_cabac_noinline( &h->cabac, &h->cabac_state[15] ) == 0 ) {
  4924. /* P_L0_D16x16, P_8x8 */
  4925. return 3 * get_cabac_noinline( &h->cabac, &h->cabac_state[16] );
  4926. } else {
  4927. /* P_L0_D8x16, P_L0_D16x8 */
  4928. return 2 - get_cabac_noinline( &h->cabac, &h->cabac_state[17] );
  4929. }
  4930. } else {
  4931. return decode_cabac_intra_mb_type(h, 17, 0) + 5;
  4932. }
  4933. } else if( h->slice_type == B_TYPE ) {
  4934. const int mba_xy = h->left_mb_xy[0];
  4935. const int mbb_xy = h->top_mb_xy;
  4936. int ctx = 0;
  4937. int bits;
  4938. if( h->slice_table[mba_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
  4939. ctx++;
  4940. if( h->slice_table[mbb_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
  4941. ctx++;
  4942. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+ctx] ) )
  4943. return 0; /* B_Direct_16x16 */
  4944. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+3] ) ) {
  4945. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
  4946. }
  4947. bits = get_cabac_noinline( &h->cabac, &h->cabac_state[27+4] ) << 3;
  4948. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 2;
  4949. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 1;
  4950. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  4951. if( bits < 8 )
  4952. return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
  4953. else if( bits == 13 ) {
  4954. return decode_cabac_intra_mb_type(h, 32, 0) + 23;
  4955. } else if( bits == 14 )
  4956. return 11; /* B_L1_L0_8x16 */
  4957. else if( bits == 15 )
  4958. return 22; /* B_8x8 */
  4959. bits= ( bits<<1 ) | get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  4960. return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
  4961. } else {
  4962. /* TODO SI/SP frames? */
  4963. return -1;
  4964. }
  4965. }
  4966. static int decode_cabac_mb_skip( H264Context *h, int mb_x, int mb_y ) {
  4967. MpegEncContext * const s = &h->s;
  4968. int mba_xy, mbb_xy;
  4969. int ctx = 0;
  4970. if(FRAME_MBAFF){ //FIXME merge with the stuff in fill_caches?
  4971. int mb_xy = mb_x + (mb_y&~1)*s->mb_stride;
  4972. mba_xy = mb_xy - 1;
  4973. if( (mb_y&1)
  4974. && h->slice_table[mba_xy] == h->slice_num
  4975. && MB_FIELD == !!IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) )
  4976. mba_xy += s->mb_stride;
  4977. if( MB_FIELD ){
  4978. mbb_xy = mb_xy - s->mb_stride;
  4979. if( !(mb_y&1)
  4980. && h->slice_table[mbb_xy] == h->slice_num
  4981. && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) )
  4982. mbb_xy -= s->mb_stride;
  4983. }else
  4984. mbb_xy = mb_x + (mb_y-1)*s->mb_stride;
  4985. }else{
  4986. int mb_xy = mb_x + mb_y*s->mb_stride;
  4987. mba_xy = mb_xy - 1;
  4988. mbb_xy = mb_xy - s->mb_stride;
  4989. }
  4990. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
  4991. ctx++;
  4992. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
  4993. ctx++;
  4994. if( h->slice_type == B_TYPE )
  4995. ctx += 13;
  4996. return get_cabac_noinline( &h->cabac, &h->cabac_state[11+ctx] );
  4997. }
  4998. static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
  4999. int mode = 0;
  5000. if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
  5001. return pred_mode;
  5002. mode += 1 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5003. mode += 2 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5004. mode += 4 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5005. if( mode >= pred_mode )
  5006. return mode + 1;
  5007. else
  5008. return mode;
  5009. }
  5010. static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
  5011. const int mba_xy = h->left_mb_xy[0];
  5012. const int mbb_xy = h->top_mb_xy;
  5013. int ctx = 0;
  5014. /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
  5015. if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
  5016. ctx++;
  5017. if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
  5018. ctx++;
  5019. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
  5020. return 0;
  5021. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  5022. return 1;
  5023. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  5024. return 2;
  5025. else
  5026. return 3;
  5027. }
  5028. static const uint8_t block_idx_x[16] = {
  5029. 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
  5030. };
  5031. static const uint8_t block_idx_y[16] = {
  5032. 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
  5033. };
  5034. static const uint8_t block_idx_xy[4][4] = {
  5035. { 0, 2, 8, 10},
  5036. { 1, 3, 9, 11},
  5037. { 4, 6, 12, 14},
  5038. { 5, 7, 13, 15}
  5039. };
  5040. static int decode_cabac_mb_cbp_luma( H264Context *h) {
  5041. int cbp = 0;
  5042. int cbp_b = -1;
  5043. int i8x8;
  5044. if( h->slice_table[h->top_mb_xy] == h->slice_num ) {
  5045. cbp_b = h->top_cbp;
  5046. tprintf("cbp_b = top_cbp = %x\n", cbp_b);
  5047. }
  5048. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5049. int cbp_a = -1;
  5050. int x, y;
  5051. int ctx = 0;
  5052. x = block_idx_x[4*i8x8];
  5053. y = block_idx_y[4*i8x8];
  5054. if( x > 0 )
  5055. cbp_a = cbp;
  5056. else if( h->slice_table[h->left_mb_xy[0]] == h->slice_num ) {
  5057. cbp_a = h->left_cbp;
  5058. tprintf("cbp_a = left_cbp = %x\n", cbp_a);
  5059. }
  5060. if( y > 0 )
  5061. cbp_b = cbp;
  5062. /* No need to test for skip as we put 0 for skip block */
  5063. /* No need to test for IPCM as we put 1 for IPCM block */
  5064. if( cbp_a >= 0 ) {
  5065. int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
  5066. if( ((cbp_a >> i8x8a)&0x01) == 0 )
  5067. ctx++;
  5068. }
  5069. if( cbp_b >= 0 ) {
  5070. int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
  5071. if( ((cbp_b >> i8x8b)&0x01) == 0 )
  5072. ctx += 2;
  5073. }
  5074. if( get_cabac( &h->cabac, &h->cabac_state[73 + ctx] ) ) {
  5075. cbp |= 1 << i8x8;
  5076. }
  5077. }
  5078. return cbp;
  5079. }
  5080. static int decode_cabac_mb_cbp_chroma( H264Context *h) {
  5081. int ctx;
  5082. int cbp_a, cbp_b;
  5083. cbp_a = (h->left_cbp>>4)&0x03;
  5084. cbp_b = (h-> top_cbp>>4)&0x03;
  5085. ctx = 0;
  5086. if( cbp_a > 0 ) ctx++;
  5087. if( cbp_b > 0 ) ctx += 2;
  5088. if( get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
  5089. return 0;
  5090. ctx = 4;
  5091. if( cbp_a == 2 ) ctx++;
  5092. if( cbp_b == 2 ) ctx += 2;
  5093. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] );
  5094. }
  5095. static int decode_cabac_mb_dqp( H264Context *h) {
  5096. MpegEncContext * const s = &h->s;
  5097. int mbn_xy;
  5098. int ctx = 0;
  5099. int val = 0;
  5100. if( s->mb_x > 0 )
  5101. mbn_xy = s->mb_x + s->mb_y*s->mb_stride - 1;
  5102. else
  5103. mbn_xy = s->mb_width - 1 + (s->mb_y-1)*s->mb_stride;
  5104. if( h->last_qscale_diff != 0 )
  5105. ctx++;
  5106. while( get_cabac_noinline( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
  5107. if( ctx < 2 )
  5108. ctx = 2;
  5109. else
  5110. ctx = 3;
  5111. val++;
  5112. if(val > 102) //prevent infinite loop
  5113. return INT_MIN;
  5114. }
  5115. if( val&0x01 )
  5116. return (val + 1)/2;
  5117. else
  5118. return -(val + 1)/2;
  5119. }
  5120. static int decode_cabac_p_mb_sub_type( H264Context *h ) {
  5121. if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
  5122. return 0; /* 8x8 */
  5123. if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
  5124. return 1; /* 8x4 */
  5125. if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
  5126. return 2; /* 4x8 */
  5127. return 3; /* 4x4 */
  5128. }
  5129. static int decode_cabac_b_mb_sub_type( H264Context *h ) {
  5130. int type;
  5131. if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
  5132. return 0; /* B_Direct_8x8 */
  5133. if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
  5134. return 1 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */
  5135. type = 3;
  5136. if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
  5137. if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
  5138. return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
  5139. type += 4;
  5140. }
  5141. type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
  5142. type += get_cabac( &h->cabac, &h->cabac_state[39] );
  5143. return type;
  5144. }
  5145. static inline int decode_cabac_mb_transform_size( H264Context *h ) {
  5146. return get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
  5147. }
  5148. static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
  5149. int refa = h->ref_cache[list][scan8[n] - 1];
  5150. int refb = h->ref_cache[list][scan8[n] - 8];
  5151. int ref = 0;
  5152. int ctx = 0;
  5153. if( h->slice_type == B_TYPE) {
  5154. if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
  5155. ctx++;
  5156. if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
  5157. ctx += 2;
  5158. } else {
  5159. if( refa > 0 )
  5160. ctx++;
  5161. if( refb > 0 )
  5162. ctx += 2;
  5163. }
  5164. while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
  5165. ref++;
  5166. if( ctx < 4 )
  5167. ctx = 4;
  5168. else
  5169. ctx = 5;
  5170. }
  5171. return ref;
  5172. }
  5173. static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
  5174. int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
  5175. abs( h->mvd_cache[list][scan8[n] - 8][l] );
  5176. int ctxbase = (l == 0) ? 40 : 47;
  5177. int ctx, mvd;
  5178. if( amvd < 3 )
  5179. ctx = 0;
  5180. else if( amvd > 32 )
  5181. ctx = 2;
  5182. else
  5183. ctx = 1;
  5184. if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
  5185. return 0;
  5186. mvd= 1;
  5187. ctx= 3;
  5188. while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
  5189. mvd++;
  5190. if( ctx < 6 )
  5191. ctx++;
  5192. }
  5193. if( mvd >= 9 ) {
  5194. int k = 3;
  5195. while( get_cabac_bypass( &h->cabac ) ) {
  5196. mvd += 1 << k;
  5197. k++;
  5198. }
  5199. while( k-- ) {
  5200. if( get_cabac_bypass( &h->cabac ) )
  5201. mvd += 1 << k;
  5202. }
  5203. }
  5204. return get_cabac_bypass_sign( &h->cabac, -mvd );
  5205. }
  5206. static int inline get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) {
  5207. int nza, nzb;
  5208. int ctx = 0;
  5209. if( cat == 0 ) {
  5210. nza = h->left_cbp&0x100;
  5211. nzb = h-> top_cbp&0x100;
  5212. } else if( cat == 1 || cat == 2 ) {
  5213. nza = h->non_zero_count_cache[scan8[idx] - 1];
  5214. nzb = h->non_zero_count_cache[scan8[idx] - 8];
  5215. } else if( cat == 3 ) {
  5216. nza = (h->left_cbp>>(6+idx))&0x01;
  5217. nzb = (h-> top_cbp>>(6+idx))&0x01;
  5218. } else {
  5219. assert(cat == 4);
  5220. nza = h->non_zero_count_cache[scan8[16+idx] - 1];
  5221. nzb = h->non_zero_count_cache[scan8[16+idx] - 8];
  5222. }
  5223. if( nza > 0 )
  5224. ctx++;
  5225. if( nzb > 0 )
  5226. ctx += 2;
  5227. return ctx + 4 * cat;
  5228. }
  5229. static const __attribute((used)) uint8_t last_coeff_flag_offset_8x8[63] = {
  5230. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5231. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  5232. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  5233. 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
  5234. };
  5235. static int decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff) {
  5236. const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
  5237. static const int significant_coeff_flag_offset[2][6] = {
  5238. { 105+0, 105+15, 105+29, 105+44, 105+47, 402 },
  5239. { 277+0, 277+15, 277+29, 277+44, 277+47, 436 }
  5240. };
  5241. static const int last_coeff_flag_offset[2][6] = {
  5242. { 166+0, 166+15, 166+29, 166+44, 166+47, 417 },
  5243. { 338+0, 338+15, 338+29, 338+44, 338+47, 451 }
  5244. };
  5245. static const int coeff_abs_level_m1_offset[6] = {
  5246. 227+0, 227+10, 227+20, 227+30, 227+39, 426
  5247. };
  5248. static const uint8_t significant_coeff_flag_offset_8x8[2][63] = {
  5249. { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
  5250. 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
  5251. 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
  5252. 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12 },
  5253. { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
  5254. 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
  5255. 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
  5256. 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }
  5257. };
  5258. int index[64];
  5259. int last;
  5260. int coeff_count = 0;
  5261. int abslevel1 = 1;
  5262. int abslevelgt1 = 0;
  5263. uint8_t *significant_coeff_ctx_base;
  5264. uint8_t *last_coeff_ctx_base;
  5265. uint8_t *abs_level_m1_ctx_base;
  5266. #ifndef ARCH_X86
  5267. #define CABAC_ON_STACK
  5268. #endif
  5269. #ifdef CABAC_ON_STACK
  5270. #define CC &cc
  5271. CABACContext cc;
  5272. cc.range = h->cabac.range;
  5273. cc.low = h->cabac.low;
  5274. cc.bytestream= h->cabac.bytestream;
  5275. #else
  5276. #define CC &h->cabac
  5277. #endif
  5278. /* cat: 0-> DC 16x16 n = 0
  5279. * 1-> AC 16x16 n = luma4x4idx
  5280. * 2-> Luma4x4 n = luma4x4idx
  5281. * 3-> DC Chroma n = iCbCr
  5282. * 4-> AC Chroma n = 4 * iCbCr + chroma4x4idx
  5283. * 5-> Luma8x8 n = 4 * luma8x8idx
  5284. */
  5285. /* read coded block flag */
  5286. if( cat != 5 ) {
  5287. if( get_cabac( CC, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n ) ] ) == 0 ) {
  5288. if( cat == 1 || cat == 2 )
  5289. h->non_zero_count_cache[scan8[n]] = 0;
  5290. else if( cat == 4 )
  5291. h->non_zero_count_cache[scan8[16+n]] = 0;
  5292. #ifdef CABAC_ON_STACK
  5293. h->cabac.range = cc.range ;
  5294. h->cabac.low = cc.low ;
  5295. h->cabac.bytestream= cc.bytestream;
  5296. #endif
  5297. return 0;
  5298. }
  5299. }
  5300. significant_coeff_ctx_base = h->cabac_state
  5301. + significant_coeff_flag_offset[MB_FIELD][cat];
  5302. last_coeff_ctx_base = h->cabac_state
  5303. + last_coeff_flag_offset[MB_FIELD][cat];
  5304. abs_level_m1_ctx_base = h->cabac_state
  5305. + coeff_abs_level_m1_offset[cat];
  5306. if( cat == 5 ) {
  5307. #define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \
  5308. for(last= 0; last < coefs; last++) { \
  5309. uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \
  5310. if( get_cabac( CC, sig_ctx )) { \
  5311. uint8_t *last_ctx = last_coeff_ctx_base + last_off; \
  5312. index[coeff_count++] = last; \
  5313. if( get_cabac( CC, last_ctx ) ) { \
  5314. last= max_coeff; \
  5315. break; \
  5316. } \
  5317. } \
  5318. }\
  5319. if( last == max_coeff -1 ) {\
  5320. index[coeff_count++] = last;\
  5321. }
  5322. const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
  5323. #if defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__))
  5324. coeff_count= decode_significance_8x8_x86(CC, significant_coeff_ctx_base, index, sig_off);
  5325. } else {
  5326. coeff_count= decode_significance_x86(CC, max_coeff, significant_coeff_ctx_base, index);
  5327. #else
  5328. DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );
  5329. } else {
  5330. DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
  5331. #endif
  5332. }
  5333. assert(coeff_count > 0);
  5334. if( cat == 0 )
  5335. h->cbp_table[mb_xy] |= 0x100;
  5336. else if( cat == 1 || cat == 2 )
  5337. h->non_zero_count_cache[scan8[n]] = coeff_count;
  5338. else if( cat == 3 )
  5339. h->cbp_table[mb_xy] |= 0x40 << n;
  5340. else if( cat == 4 )
  5341. h->non_zero_count_cache[scan8[16+n]] = coeff_count;
  5342. else {
  5343. assert( cat == 5 );
  5344. fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);
  5345. }
  5346. for( coeff_count--; coeff_count >= 0; coeff_count-- ) {
  5347. uint8_t *ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 )) + abs_level_m1_ctx_base;
  5348. int j= scantable[index[coeff_count]];
  5349. if( get_cabac( CC, ctx ) == 0 ) {
  5350. if( !qmul ) {
  5351. block[j] = get_cabac_bypass_sign( CC, -1);
  5352. }else{
  5353. block[j] = (get_cabac_bypass_sign( CC, -qmul[j]) + 32) >> 6;;
  5354. }
  5355. abslevel1++;
  5356. } else {
  5357. int coeff_abs = 2;
  5358. ctx = 5 + FFMIN( 4, abslevelgt1 ) + abs_level_m1_ctx_base;
  5359. while( coeff_abs < 15 && get_cabac( CC, ctx ) ) {
  5360. coeff_abs++;
  5361. }
  5362. if( coeff_abs >= 15 ) {
  5363. int j = 0;
  5364. while( get_cabac_bypass( CC ) ) {
  5365. j++;
  5366. }
  5367. coeff_abs=1;
  5368. while( j-- ) {
  5369. coeff_abs += coeff_abs + get_cabac_bypass( CC );
  5370. }
  5371. coeff_abs+= 14;
  5372. }
  5373. if( !qmul ) {
  5374. if( get_cabac_bypass( CC ) ) block[j] = -coeff_abs;
  5375. else block[j] = coeff_abs;
  5376. }else{
  5377. if( get_cabac_bypass( CC ) ) block[j] = (-coeff_abs * qmul[j] + 32) >> 6;
  5378. else block[j] = ( coeff_abs * qmul[j] + 32) >> 6;
  5379. }
  5380. abslevelgt1++;
  5381. }
  5382. }
  5383. #ifdef CABAC_ON_STACK
  5384. h->cabac.range = cc.range ;
  5385. h->cabac.low = cc.low ;
  5386. h->cabac.bytestream= cc.bytestream;
  5387. #endif
  5388. return 0;
  5389. }
  5390. static void inline compute_mb_neighbors(H264Context *h)
  5391. {
  5392. MpegEncContext * const s = &h->s;
  5393. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  5394. h->top_mb_xy = mb_xy - s->mb_stride;
  5395. h->left_mb_xy[0] = mb_xy - 1;
  5396. if(FRAME_MBAFF){
  5397. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  5398. const int top_pair_xy = pair_xy - s->mb_stride;
  5399. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  5400. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  5401. const int curr_mb_frame_flag = !MB_FIELD;
  5402. const int bottom = (s->mb_y & 1);
  5403. if (bottom
  5404. ? !curr_mb_frame_flag // bottom macroblock
  5405. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  5406. ) {
  5407. h->top_mb_xy -= s->mb_stride;
  5408. }
  5409. if (left_mb_frame_flag != curr_mb_frame_flag) {
  5410. h->left_mb_xy[0] = pair_xy - 1;
  5411. }
  5412. }
  5413. return;
  5414. }
  5415. /**
  5416. * decodes a macroblock
  5417. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  5418. */
  5419. static int decode_mb_cabac(H264Context *h) {
  5420. MpegEncContext * const s = &h->s;
  5421. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  5422. int mb_type, partition_count, cbp = 0;
  5423. int dct8x8_allowed= h->pps.transform_8x8_mode;
  5424. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?)
  5425. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  5426. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) {
  5427. int skip;
  5428. /* a skipped mb needs the aff flag from the following mb */
  5429. if( FRAME_MBAFF && s->mb_x==0 && (s->mb_y&1)==0 )
  5430. predict_field_decoding_flag(h);
  5431. if( FRAME_MBAFF && (s->mb_y&1)==1 && h->prev_mb_skipped )
  5432. skip = h->next_mb_skipped;
  5433. else
  5434. skip = decode_cabac_mb_skip( h, s->mb_x, s->mb_y );
  5435. /* read skip flags */
  5436. if( skip ) {
  5437. if( FRAME_MBAFF && (s->mb_y&1)==0 ){
  5438. s->current_picture.mb_type[mb_xy] = MB_TYPE_SKIP;
  5439. h->next_mb_skipped = decode_cabac_mb_skip( h, s->mb_x, s->mb_y+1 );
  5440. if(h->next_mb_skipped)
  5441. predict_field_decoding_flag(h);
  5442. else
  5443. h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5444. }
  5445. decode_mb_skip(h);
  5446. h->cbp_table[mb_xy] = 0;
  5447. h->chroma_pred_mode_table[mb_xy] = 0;
  5448. h->last_qscale_diff = 0;
  5449. return 0;
  5450. }
  5451. }
  5452. if(FRAME_MBAFF){
  5453. if( (s->mb_y&1) == 0 )
  5454. h->mb_mbaff =
  5455. h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5456. }else
  5457. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  5458. h->prev_mb_skipped = 0;
  5459. compute_mb_neighbors(h);
  5460. if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
  5461. av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
  5462. return -1;
  5463. }
  5464. if( h->slice_type == B_TYPE ) {
  5465. if( mb_type < 23 ){
  5466. partition_count= b_mb_type_info[mb_type].partition_count;
  5467. mb_type= b_mb_type_info[mb_type].type;
  5468. }else{
  5469. mb_type -= 23;
  5470. goto decode_intra_mb;
  5471. }
  5472. } else if( h->slice_type == P_TYPE ) {
  5473. if( mb_type < 5) {
  5474. partition_count= p_mb_type_info[mb_type].partition_count;
  5475. mb_type= p_mb_type_info[mb_type].type;
  5476. } else {
  5477. mb_type -= 5;
  5478. goto decode_intra_mb;
  5479. }
  5480. } else {
  5481. assert(h->slice_type == I_TYPE);
  5482. decode_intra_mb:
  5483. partition_count = 0;
  5484. cbp= i_mb_type_info[mb_type].cbp;
  5485. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  5486. mb_type= i_mb_type_info[mb_type].type;
  5487. }
  5488. if(MB_FIELD)
  5489. mb_type |= MB_TYPE_INTERLACED;
  5490. h->slice_table[ mb_xy ]= h->slice_num;
  5491. if(IS_INTRA_PCM(mb_type)) {
  5492. const uint8_t *ptr;
  5493. unsigned int x, y;
  5494. // We assume these blocks are very rare so we dont optimize it.
  5495. // FIXME The two following lines get the bitstream position in the cabac
  5496. // decode, I think it should be done by a function in cabac.h (or cabac.c).
  5497. ptr= h->cabac.bytestream;
  5498. if(h->cabac.low&0x1) ptr--;
  5499. if(CABAC_BITS==16){
  5500. if(h->cabac.low&0x1FF) ptr--;
  5501. }
  5502. // The pixels are stored in the same order as levels in h->mb array.
  5503. for(y=0; y<16; y++){
  5504. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  5505. for(x=0; x<16; x++){
  5506. tprintf("LUMA ICPM LEVEL (%3d)\n", *ptr);
  5507. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= *ptr++;
  5508. }
  5509. }
  5510. for(y=0; y<8; y++){
  5511. const int index= 256 + 4*(y&3) + 32*(y>>2);
  5512. for(x=0; x<8; x++){
  5513. tprintf("CHROMA U ICPM LEVEL (%3d)\n", *ptr);
  5514. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5515. }
  5516. }
  5517. for(y=0; y<8; y++){
  5518. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  5519. for(x=0; x<8; x++){
  5520. tprintf("CHROMA V ICPM LEVEL (%3d)\n", *ptr);
  5521. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5522. }
  5523. }
  5524. ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);
  5525. // All blocks are present
  5526. h->cbp_table[mb_xy] = 0x1ef;
  5527. h->chroma_pred_mode_table[mb_xy] = 0;
  5528. // In deblocking, the quantizer is 0
  5529. s->current_picture.qscale_table[mb_xy]= 0;
  5530. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  5531. // All coeffs are present
  5532. memset(h->non_zero_count[mb_xy], 16, 16);
  5533. s->current_picture.mb_type[mb_xy]= mb_type;
  5534. return 0;
  5535. }
  5536. if(MB_MBAFF){
  5537. h->ref_count[0] <<= 1;
  5538. h->ref_count[1] <<= 1;
  5539. }
  5540. fill_caches(h, mb_type, 0);
  5541. if( IS_INTRA( mb_type ) ) {
  5542. int i;
  5543. if( IS_INTRA4x4( mb_type ) ) {
  5544. if( dct8x8_allowed && decode_cabac_mb_transform_size( h ) ) {
  5545. mb_type |= MB_TYPE_8x8DCT;
  5546. for( i = 0; i < 16; i+=4 ) {
  5547. int pred = pred_intra_mode( h, i );
  5548. int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5549. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  5550. }
  5551. } else {
  5552. for( i = 0; i < 16; i++ ) {
  5553. int pred = pred_intra_mode( h, i );
  5554. h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5555. //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
  5556. }
  5557. }
  5558. write_back_intra_pred_mode(h);
  5559. if( check_intra4x4_pred_mode(h) < 0 ) return -1;
  5560. } else {
  5561. h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );
  5562. if( h->intra16x16_pred_mode < 0 ) return -1;
  5563. }
  5564. h->chroma_pred_mode_table[mb_xy] =
  5565. h->chroma_pred_mode = decode_cabac_mb_chroma_pre_mode( h );
  5566. h->chroma_pred_mode= check_intra_pred_mode( h, h->chroma_pred_mode );
  5567. if( h->chroma_pred_mode < 0 ) return -1;
  5568. } else if( partition_count == 4 ) {
  5569. int i, j, sub_partition_count[4], list, ref[2][4];
  5570. if( h->slice_type == B_TYPE ) {
  5571. for( i = 0; i < 4; i++ ) {
  5572. h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
  5573. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5574. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5575. }
  5576. if( IS_DIRECT(h->sub_mb_type[0] | h->sub_mb_type[1] |
  5577. h->sub_mb_type[2] | h->sub_mb_type[3]) ) {
  5578. pred_direct_motion(h, &mb_type);
  5579. if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
  5580. for( i = 0; i < 4; i++ )
  5581. if( IS_DIRECT(h->sub_mb_type[i]) )
  5582. fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );
  5583. }
  5584. }
  5585. } else {
  5586. for( i = 0; i < 4; i++ ) {
  5587. h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
  5588. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5589. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5590. }
  5591. }
  5592. for( list = 0; list < 2; list++ ) {
  5593. if( h->ref_count[list] > 0 ) {
  5594. for( i = 0; i < 4; i++ ) {
  5595. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  5596. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  5597. if( h->ref_count[list] > 1 )
  5598. ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );
  5599. else
  5600. ref[list][i] = 0;
  5601. } else {
  5602. ref[list][i] = -1;
  5603. }
  5604. h->ref_cache[list][ scan8[4*i]+1 ]=
  5605. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  5606. }
  5607. }
  5608. }
  5609. if(dct8x8_allowed)
  5610. dct8x8_allowed = get_dct8x8_allowed(h);
  5611. for(list=0; list<2; list++){
  5612. for(i=0; i<4; i++){
  5613. if(IS_DIRECT(h->sub_mb_type[i])){
  5614. fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);
  5615. continue;
  5616. }
  5617. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];
  5618. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  5619. const int sub_mb_type= h->sub_mb_type[i];
  5620. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  5621. for(j=0; j<sub_partition_count[i]; j++){
  5622. int mpx, mpy;
  5623. int mx, my;
  5624. const int index= 4*i + block_width*j;
  5625. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  5626. int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];
  5627. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);
  5628. mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );
  5629. my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );
  5630. tprintf("final mv:%d %d\n", mx, my);
  5631. if(IS_SUB_8X8(sub_mb_type)){
  5632. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  5633. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  5634. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  5635. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  5636. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]=
  5637. mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;
  5638. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]=
  5639. mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;
  5640. }else if(IS_SUB_8X4(sub_mb_type)){
  5641. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  5642. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  5643. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]= mx- mpx;
  5644. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]= my - mpy;
  5645. }else if(IS_SUB_4X8(sub_mb_type)){
  5646. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  5647. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  5648. mvd_cache[ 0 ][0]= mvd_cache[ 8 ][0]= mx - mpx;
  5649. mvd_cache[ 0 ][1]= mvd_cache[ 8 ][1]= my - mpy;
  5650. }else{
  5651. assert(IS_SUB_4X4(sub_mb_type));
  5652. mv_cache[ 0 ][0]= mx;
  5653. mv_cache[ 0 ][1]= my;
  5654. mvd_cache[ 0 ][0]= mx - mpx;
  5655. mvd_cache[ 0 ][1]= my - mpy;
  5656. }
  5657. }
  5658. }else{
  5659. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  5660. uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];
  5661. p[0] = p[1] = p[8] = p[9] = 0;
  5662. pd[0]= pd[1]= pd[8]= pd[9]= 0;
  5663. }
  5664. }
  5665. }
  5666. } else if( IS_DIRECT(mb_type) ) {
  5667. pred_direct_motion(h, &mb_type);
  5668. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  5669. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  5670. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  5671. } else {
  5672. int list, mx, my, i, mpx, mpy;
  5673. if(IS_16X16(mb_type)){
  5674. for(list=0; list<2; list++){
  5675. if(IS_DIR(mb_type, 0, list)){
  5676. if(h->ref_count[list] > 0 ){
  5677. const int ref = h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 0 ) : 0;
  5678. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
  5679. }
  5680. }else
  5681. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  5682. }
  5683. for(list=0; list<2; list++){
  5684. if(IS_DIR(mb_type, 0, list)){
  5685. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);
  5686. mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );
  5687. my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );
  5688. tprintf("final mv:%d %d\n", mx, my);
  5689. fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5690. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  5691. }else
  5692. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  5693. }
  5694. }
  5695. else if(IS_16X8(mb_type)){
  5696. for(list=0; list<2; list++){
  5697. if(h->ref_count[list]>0){
  5698. for(i=0; i<2; i++){
  5699. if(IS_DIR(mb_type, i, list)){
  5700. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 8*i ) : 0;
  5701. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
  5702. }else
  5703. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  5704. }
  5705. }
  5706. }
  5707. for(list=0; list<2; list++){
  5708. for(i=0; i<2; i++){
  5709. if(IS_DIR(mb_type, i, list)){
  5710. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);
  5711. mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );
  5712. my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );
  5713. tprintf("final mv:%d %d\n", mx, my);
  5714. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);
  5715. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  5716. }else{
  5717. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5718. fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5719. }
  5720. }
  5721. }
  5722. }else{
  5723. assert(IS_8X16(mb_type));
  5724. for(list=0; list<2; list++){
  5725. if(h->ref_count[list]>0){
  5726. for(i=0; i<2; i++){
  5727. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  5728. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 4*i ) : 0;
  5729. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
  5730. }else
  5731. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  5732. }
  5733. }
  5734. }
  5735. for(list=0; list<2; list++){
  5736. for(i=0; i<2; i++){
  5737. if(IS_DIR(mb_type, i, list)){
  5738. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);
  5739. mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );
  5740. my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );
  5741. tprintf("final mv:%d %d\n", mx, my);
  5742. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5743. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  5744. }else{
  5745. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5746. fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5747. }
  5748. }
  5749. }
  5750. }
  5751. }
  5752. if( IS_INTER( mb_type ) ) {
  5753. h->chroma_pred_mode_table[mb_xy] = 0;
  5754. write_back_motion( h, mb_type );
  5755. }
  5756. if( !IS_INTRA16x16( mb_type ) ) {
  5757. cbp = decode_cabac_mb_cbp_luma( h );
  5758. cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
  5759. }
  5760. h->cbp_table[mb_xy] = h->cbp = cbp;
  5761. if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
  5762. if( decode_cabac_mb_transform_size( h ) )
  5763. mb_type |= MB_TYPE_8x8DCT;
  5764. }
  5765. s->current_picture.mb_type[mb_xy]= mb_type;
  5766. if( cbp || IS_INTRA16x16( mb_type ) ) {
  5767. const uint8_t *scan, *scan8x8, *dc_scan;
  5768. int dqp;
  5769. if(IS_INTERLACED(mb_type)){
  5770. scan8x8= s->qscale ? h->field_scan8x8 : h->field_scan8x8_q0;
  5771. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  5772. dc_scan= luma_dc_field_scan;
  5773. }else{
  5774. scan8x8= s->qscale ? h->zigzag_scan8x8 : h->zigzag_scan8x8_q0;
  5775. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  5776. dc_scan= luma_dc_zigzag_scan;
  5777. }
  5778. h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
  5779. if( dqp == INT_MIN ){
  5780. av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", s->mb_x, s->mb_y);
  5781. return -1;
  5782. }
  5783. s->qscale += dqp;
  5784. if(((unsigned)s->qscale) > 51){
  5785. if(s->qscale<0) s->qscale+= 52;
  5786. else s->qscale-= 52;
  5787. }
  5788. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  5789. if( IS_INTRA16x16( mb_type ) ) {
  5790. int i;
  5791. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
  5792. if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16) < 0)
  5793. return -1;
  5794. if( cbp&15 ) {
  5795. for( i = 0; i < 16; i++ ) {
  5796. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
  5797. if( decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 )
  5798. return -1;
  5799. }
  5800. } else {
  5801. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  5802. }
  5803. } else {
  5804. int i8x8, i4x4;
  5805. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5806. if( cbp & (1<<i8x8) ) {
  5807. if( IS_8x8DCT(mb_type) ) {
  5808. if( decode_cabac_residual(h, h->mb + 64*i8x8, 5, 4*i8x8,
  5809. scan8x8, h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 64) < 0 )
  5810. return -1;
  5811. } else
  5812. for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
  5813. const int index = 4*i8x8 + i4x4;
  5814. //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
  5815. //START_TIMER
  5816. 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 )
  5817. return -1;
  5818. //STOP_TIMER("decode_residual")
  5819. }
  5820. } else {
  5821. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  5822. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  5823. }
  5824. }
  5825. }
  5826. if( cbp&0x30 ){
  5827. int c;
  5828. for( c = 0; c < 2; c++ ) {
  5829. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
  5830. if( decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, NULL, 4) < 0)
  5831. return -1;
  5832. }
  5833. }
  5834. if( cbp&0x20 ) {
  5835. int c, i;
  5836. for( c = 0; c < 2; c++ ) {
  5837. for( i = 0; i < 4; i++ ) {
  5838. const int index = 16 + 4 * c + i;
  5839. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
  5840. 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)
  5841. return -1;
  5842. }
  5843. }
  5844. } else {
  5845. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5846. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5847. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5848. }
  5849. } else {
  5850. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5851. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  5852. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5853. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5854. h->last_qscale_diff = 0;
  5855. }
  5856. s->current_picture.qscale_table[mb_xy]= s->qscale;
  5857. write_back_non_zero_count(h);
  5858. if(MB_MBAFF){
  5859. h->ref_count[0] >>= 1;
  5860. h->ref_count[1] >>= 1;
  5861. }
  5862. return 0;
  5863. }
  5864. static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5865. int i, d;
  5866. const int index_a = qp + h->slice_alpha_c0_offset;
  5867. const int alpha = (alpha_table+52)[index_a];
  5868. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5869. if( bS[0] < 4 ) {
  5870. int8_t tc[4];
  5871. for(i=0; i<4; i++)
  5872. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  5873. h->s.dsp.h264_h_loop_filter_luma(pix, stride, alpha, beta, tc);
  5874. } else {
  5875. /* 16px edge length, because bS=4 is triggered by being at
  5876. * the edge of an intra MB, so all 4 bS are the same */
  5877. for( d = 0; d < 16; d++ ) {
  5878. const int p0 = pix[-1];
  5879. const int p1 = pix[-2];
  5880. const int p2 = pix[-3];
  5881. const int q0 = pix[0];
  5882. const int q1 = pix[1];
  5883. const int q2 = pix[2];
  5884. if( FFABS( p0 - q0 ) < alpha &&
  5885. FFABS( p1 - p0 ) < beta &&
  5886. FFABS( q1 - q0 ) < beta ) {
  5887. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5888. if( FFABS( p2 - p0 ) < beta)
  5889. {
  5890. const int p3 = pix[-4];
  5891. /* p0', p1', p2' */
  5892. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5893. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5894. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5895. } else {
  5896. /* p0' */
  5897. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5898. }
  5899. if( FFABS( q2 - q0 ) < beta)
  5900. {
  5901. const int q3 = pix[3];
  5902. /* q0', q1', q2' */
  5903. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5904. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5905. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5906. } else {
  5907. /* q0' */
  5908. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5909. }
  5910. }else{
  5911. /* p0', q0' */
  5912. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5913. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5914. }
  5915. tprintf("filter_mb_edgev i:%d d:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, p2, p1, p0, q0, q1, q2, pix[-2], pix[-1], pix[0], pix[1]);
  5916. }
  5917. pix += stride;
  5918. }
  5919. }
  5920. }
  5921. static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5922. int i;
  5923. const int index_a = qp + h->slice_alpha_c0_offset;
  5924. const int alpha = (alpha_table+52)[index_a];
  5925. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5926. if( bS[0] < 4 ) {
  5927. int8_t tc[4];
  5928. for(i=0; i<4; i++)
  5929. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  5930. h->s.dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
  5931. } else {
  5932. h->s.dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
  5933. }
  5934. }
  5935. static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  5936. int i;
  5937. for( i = 0; i < 16; i++, pix += stride) {
  5938. int index_a;
  5939. int alpha;
  5940. int beta;
  5941. int qp_index;
  5942. int bS_index = (i >> 1);
  5943. if (!MB_FIELD) {
  5944. bS_index &= ~1;
  5945. bS_index |= (i & 1);
  5946. }
  5947. if( bS[bS_index] == 0 ) {
  5948. continue;
  5949. }
  5950. qp_index = MB_FIELD ? (i >> 3) : (i & 1);
  5951. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  5952. alpha = (alpha_table+52)[index_a];
  5953. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  5954. if( bS[bS_index] < 4 ) {
  5955. const int tc0 = (tc0_table+52)[index_a][bS[bS_index] - 1];
  5956. const int p0 = pix[-1];
  5957. const int p1 = pix[-2];
  5958. const int p2 = pix[-3];
  5959. const int q0 = pix[0];
  5960. const int q1 = pix[1];
  5961. const int q2 = pix[2];
  5962. if( FFABS( p0 - q0 ) < alpha &&
  5963. FFABS( p1 - p0 ) < beta &&
  5964. FFABS( q1 - q0 ) < beta ) {
  5965. int tc = tc0;
  5966. int i_delta;
  5967. if( FFABS( p2 - p0 ) < beta ) {
  5968. pix[-2] = p1 + clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  5969. tc++;
  5970. }
  5971. if( FFABS( q2 - q0 ) < beta ) {
  5972. pix[1] = q1 + clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  5973. tc++;
  5974. }
  5975. i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  5976. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  5977. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  5978. tprintf("filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, tc, bS[bS_index], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);
  5979. }
  5980. }else{
  5981. const int p0 = pix[-1];
  5982. const int p1 = pix[-2];
  5983. const int p2 = pix[-3];
  5984. const int q0 = pix[0];
  5985. const int q1 = pix[1];
  5986. const int q2 = pix[2];
  5987. if( FFABS( p0 - q0 ) < alpha &&
  5988. FFABS( p1 - p0 ) < beta &&
  5989. FFABS( q1 - q0 ) < beta ) {
  5990. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5991. if( FFABS( p2 - p0 ) < beta)
  5992. {
  5993. const int p3 = pix[-4];
  5994. /* p0', p1', p2' */
  5995. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5996. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5997. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5998. } else {
  5999. /* p0' */
  6000. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6001. }
  6002. if( FFABS( q2 - q0 ) < beta)
  6003. {
  6004. const int q3 = pix[3];
  6005. /* q0', q1', q2' */
  6006. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  6007. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  6008. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  6009. } else {
  6010. /* q0' */
  6011. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6012. }
  6013. }else{
  6014. /* p0', q0' */
  6015. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6016. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6017. }
  6018. tprintf("filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, p2, p1, p0, q0, q1, q2, pix[-3], pix[-2], pix[-1], pix[0], pix[1], pix[2]);
  6019. }
  6020. }
  6021. }
  6022. }
  6023. static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  6024. int i;
  6025. for( i = 0; i < 8; i++, pix += stride) {
  6026. int index_a;
  6027. int alpha;
  6028. int beta;
  6029. int qp_index;
  6030. int bS_index = i;
  6031. if( bS[bS_index] == 0 ) {
  6032. continue;
  6033. }
  6034. qp_index = MB_FIELD ? (i >> 2) : (i & 1);
  6035. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  6036. alpha = (alpha_table+52)[index_a];
  6037. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  6038. if( bS[bS_index] < 4 ) {
  6039. const int tc = (tc0_table+52)[index_a][bS[bS_index] - 1] + 1;
  6040. const int p0 = pix[-1];
  6041. const int p1 = pix[-2];
  6042. const int q0 = pix[0];
  6043. const int q1 = pix[1];
  6044. if( FFABS( p0 - q0 ) < alpha &&
  6045. FFABS( p1 - p0 ) < beta &&
  6046. FFABS( q1 - q0 ) < beta ) {
  6047. const int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  6048. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  6049. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  6050. tprintf("filter_mb_mbaff_edgecv i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, tc, bS[bS_index], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);
  6051. }
  6052. }else{
  6053. const int p0 = pix[-1];
  6054. const int p1 = pix[-2];
  6055. const int q0 = pix[0];
  6056. const int q1 = pix[1];
  6057. if( FFABS( p0 - q0 ) < alpha &&
  6058. FFABS( p1 - p0 ) < beta &&
  6059. FFABS( q1 - q0 ) < beta ) {
  6060. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  6061. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  6062. tprintf("filter_mb_mbaff_edgecv i:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x, %02x, %02x]\n", i, pix[-3], p1, p0, q0, q1, pix[2], pix[-3], pix[-2], pix[-1], pix[0], pix[1], pix[2]);
  6063. }
  6064. }
  6065. }
  6066. }
  6067. static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  6068. int i, d;
  6069. const int index_a = qp + h->slice_alpha_c0_offset;
  6070. const int alpha = (alpha_table+52)[index_a];
  6071. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  6072. const int pix_next = stride;
  6073. if( bS[0] < 4 ) {
  6074. int8_t tc[4];
  6075. for(i=0; i<4; i++)
  6076. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  6077. h->s.dsp.h264_v_loop_filter_luma(pix, stride, alpha, beta, tc);
  6078. } else {
  6079. /* 16px edge length, see filter_mb_edgev */
  6080. for( d = 0; d < 16; d++ ) {
  6081. const int p0 = pix[-1*pix_next];
  6082. const int p1 = pix[-2*pix_next];
  6083. const int p2 = pix[-3*pix_next];
  6084. const int q0 = pix[0];
  6085. const int q1 = pix[1*pix_next];
  6086. const int q2 = pix[2*pix_next];
  6087. if( FFABS( p0 - q0 ) < alpha &&
  6088. FFABS( p1 - p0 ) < beta &&
  6089. FFABS( q1 - q0 ) < beta ) {
  6090. const int p3 = pix[-4*pix_next];
  6091. const int q3 = pix[ 3*pix_next];
  6092. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  6093. if( FFABS( p2 - p0 ) < beta) {
  6094. /* p0', p1', p2' */
  6095. pix[-1*pix_next] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  6096. pix[-2*pix_next] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  6097. pix[-3*pix_next] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  6098. } else {
  6099. /* p0' */
  6100. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6101. }
  6102. if( FFABS( q2 - q0 ) < beta) {
  6103. /* q0', q1', q2' */
  6104. pix[0*pix_next] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  6105. pix[1*pix_next] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  6106. pix[2*pix_next] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  6107. } else {
  6108. /* q0' */
  6109. pix[0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6110. }
  6111. }else{
  6112. /* p0', q0' */
  6113. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6114. pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6115. }
  6116. tprintf("filter_mb_edgeh i:%d d:%d, qp:%d, indexA:%d, alpha:%d, beta:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, qp, index_a, alpha, beta, bS[i], p2, p1, p0, q0, q1, q2, pix[-2*pix_next], pix[-pix_next], pix[0], pix[pix_next]);
  6117. }
  6118. pix++;
  6119. }
  6120. }
  6121. }
  6122. static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  6123. int i;
  6124. const int index_a = qp + h->slice_alpha_c0_offset;
  6125. const int alpha = (alpha_table+52)[index_a];
  6126. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  6127. if( bS[0] < 4 ) {
  6128. int8_t tc[4];
  6129. for(i=0; i<4; i++)
  6130. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  6131. h->s.dsp.h264_v_loop_filter_chroma(pix, stride, alpha, beta, tc);
  6132. } else {
  6133. h->s.dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);
  6134. }
  6135. }
  6136. 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) {
  6137. MpegEncContext * const s = &h->s;
  6138. int mb_xy, mb_type;
  6139. int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh;
  6140. if(mb_x==0 || mb_y==0 || !s->dsp.h264_loop_filter_strength) {
  6141. filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);
  6142. return;
  6143. }
  6144. assert(!FRAME_MBAFF);
  6145. mb_xy = mb_x + mb_y*s->mb_stride;
  6146. mb_type = s->current_picture.mb_type[mb_xy];
  6147. qp = s->current_picture.qscale_table[mb_xy];
  6148. qp0 = s->current_picture.qscale_table[mb_xy-1];
  6149. qp1 = s->current_picture.qscale_table[h->top_mb_xy];
  6150. qpc = get_chroma_qp( h->pps.chroma_qp_index_offset, qp );
  6151. qpc0 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp0 );
  6152. qpc1 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp1 );
  6153. qp0 = (qp + qp0 + 1) >> 1;
  6154. qp1 = (qp + qp1 + 1) >> 1;
  6155. qpc0 = (qpc + qpc0 + 1) >> 1;
  6156. qpc1 = (qpc + qpc1 + 1) >> 1;
  6157. qp_thresh = 15 - h->slice_alpha_c0_offset;
  6158. if(qp <= qp_thresh && qp0 <= qp_thresh && qp1 <= qp_thresh &&
  6159. qpc <= qp_thresh && qpc0 <= qp_thresh && qpc1 <= qp_thresh)
  6160. return;
  6161. if( IS_INTRA(mb_type) ) {
  6162. int16_t bS4[4] = {4,4,4,4};
  6163. int16_t bS3[4] = {3,3,3,3};
  6164. if( IS_8x8DCT(mb_type) ) {
  6165. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  6166. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  6167. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  6168. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  6169. } else {
  6170. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  6171. filter_mb_edgev( h, &img_y[4*1], linesize, bS3, qp );
  6172. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  6173. filter_mb_edgev( h, &img_y[4*3], linesize, bS3, qp );
  6174. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  6175. filter_mb_edgeh( h, &img_y[4*1*linesize], linesize, bS3, qp );
  6176. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  6177. filter_mb_edgeh( h, &img_y[4*3*linesize], linesize, bS3, qp );
  6178. }
  6179. filter_mb_edgecv( h, &img_cb[2*0], uvlinesize, bS4, qpc0 );
  6180. filter_mb_edgecv( h, &img_cb[2*2], uvlinesize, bS3, qpc );
  6181. filter_mb_edgecv( h, &img_cr[2*0], uvlinesize, bS4, qpc0 );
  6182. filter_mb_edgecv( h, &img_cr[2*2], uvlinesize, bS3, qpc );
  6183. filter_mb_edgech( h, &img_cb[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  6184. filter_mb_edgech( h, &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc );
  6185. filter_mb_edgech( h, &img_cr[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  6186. filter_mb_edgech( h, &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc );
  6187. return;
  6188. } else {
  6189. DECLARE_ALIGNED_8(int16_t, bS[2][4][4]);
  6190. uint64_t (*bSv)[4] = (uint64_t(*)[4])bS;
  6191. int edges;
  6192. if( IS_8x8DCT(mb_type) && (h->cbp&7) == 7 ) {
  6193. edges = 4;
  6194. bSv[0][0] = bSv[0][2] = bSv[1][0] = bSv[1][2] = 0x0002000200020002ULL;
  6195. } else {
  6196. int mask_edge1 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 :
  6197. (mb_type & MB_TYPE_16x8) ? 1 : 0;
  6198. int mask_edge0 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16))
  6199. && (s->current_picture.mb_type[mb_xy-1] & (MB_TYPE_16x16 | MB_TYPE_8x16))
  6200. ? 3 : 0;
  6201. int step = IS_8x8DCT(mb_type) ? 2 : 1;
  6202. edges = (mb_type & MB_TYPE_16x16) && !(h->cbp & 15) ? 1 : 4;
  6203. s->dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,
  6204. (h->slice_type == B_TYPE), edges, step, mask_edge0, mask_edge1 );
  6205. }
  6206. if( IS_INTRA(s->current_picture.mb_type[mb_xy-1]) )
  6207. bSv[0][0] = 0x0004000400040004ULL;
  6208. if( IS_INTRA(s->current_picture.mb_type[h->top_mb_xy]) )
  6209. bSv[1][0] = 0x0004000400040004ULL;
  6210. #define FILTER(hv,dir,edge)\
  6211. if(bSv[dir][edge]) {\
  6212. filter_mb_edge##hv( h, &img_y[4*edge*(dir?linesize:1)], linesize, bS[dir][edge], edge ? qp : qp##dir );\
  6213. if(!(edge&1)) {\
  6214. filter_mb_edgec##hv( h, &img_cb[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  6215. filter_mb_edgec##hv( h, &img_cr[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  6216. }\
  6217. }
  6218. if( edges == 1 ) {
  6219. FILTER(v,0,0);
  6220. FILTER(h,1,0);
  6221. } else if( IS_8x8DCT(mb_type) ) {
  6222. FILTER(v,0,0);
  6223. FILTER(v,0,2);
  6224. FILTER(h,1,0);
  6225. FILTER(h,1,2);
  6226. } else {
  6227. FILTER(v,0,0);
  6228. FILTER(v,0,1);
  6229. FILTER(v,0,2);
  6230. FILTER(v,0,3);
  6231. FILTER(h,1,0);
  6232. FILTER(h,1,1);
  6233. FILTER(h,1,2);
  6234. FILTER(h,1,3);
  6235. }
  6236. #undef FILTER
  6237. }
  6238. }
  6239. 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) {
  6240. MpegEncContext * const s = &h->s;
  6241. const int mb_xy= mb_x + mb_y*s->mb_stride;
  6242. const int mb_type = s->current_picture.mb_type[mb_xy];
  6243. const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4;
  6244. int first_vertical_edge_done = 0;
  6245. int dir;
  6246. /* FIXME: A given frame may occupy more than one position in
  6247. * the reference list. So ref2frm should be populated with
  6248. * frame numbers, not indices. */
  6249. static const int ref2frm[34] = {-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  6250. 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
  6251. //for sufficiently low qp, filtering wouldn't do anything
  6252. //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
  6253. if(!FRAME_MBAFF){
  6254. int qp_thresh = 15 - h->slice_alpha_c0_offset - FFMAX(0, h->pps.chroma_qp_index_offset);
  6255. int qp = s->current_picture.qscale_table[mb_xy];
  6256. if(qp <= qp_thresh
  6257. && (mb_x == 0 || ((qp + s->current_picture.qscale_table[mb_xy-1] + 1)>>1) <= qp_thresh)
  6258. && (mb_y == 0 || ((qp + s->current_picture.qscale_table[h->top_mb_xy] + 1)>>1) <= qp_thresh)){
  6259. return;
  6260. }
  6261. }
  6262. if (FRAME_MBAFF
  6263. // left mb is in picture
  6264. && h->slice_table[mb_xy-1] != 255
  6265. // and current and left pair do not have the same interlaced type
  6266. && (IS_INTERLACED(mb_type) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
  6267. // and left mb is in the same slice if deblocking_filter == 2
  6268. && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
  6269. /* First vertical edge is different in MBAFF frames
  6270. * There are 8 different bS to compute and 2 different Qp
  6271. */
  6272. const int pair_xy = mb_x + (mb_y&~1)*s->mb_stride;
  6273. const int left_mb_xy[2] = { pair_xy-1, pair_xy-1+s->mb_stride };
  6274. int16_t bS[8];
  6275. int qp[2];
  6276. int chroma_qp[2];
  6277. int mb_qp, mbn0_qp, mbn1_qp;
  6278. int i;
  6279. first_vertical_edge_done = 1;
  6280. if( IS_INTRA(mb_type) )
  6281. bS[0] = bS[1] = bS[2] = bS[3] = bS[4] = bS[5] = bS[6] = bS[7] = 4;
  6282. else {
  6283. for( i = 0; i < 8; i++ ) {
  6284. int mbn_xy = MB_FIELD ? left_mb_xy[i>>2] : left_mb_xy[i&1];
  6285. if( IS_INTRA( s->current_picture.mb_type[mbn_xy] ) )
  6286. bS[i] = 4;
  6287. else if( h->non_zero_count_cache[12+8*(i>>1)] != 0 ||
  6288. /* FIXME: with 8x8dct + cavlc, should check cbp instead of nnz */
  6289. h->non_zero_count[mbn_xy][MB_FIELD ? i&3 : (i>>2)+(mb_y&1)*2] )
  6290. bS[i] = 2;
  6291. else
  6292. bS[i] = 1;
  6293. }
  6294. }
  6295. mb_qp = s->current_picture.qscale_table[mb_xy];
  6296. mbn0_qp = s->current_picture.qscale_table[left_mb_xy[0]];
  6297. mbn1_qp = s->current_picture.qscale_table[left_mb_xy[1]];
  6298. qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1;
  6299. chroma_qp[0] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6300. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn0_qp ) + 1 ) >> 1;
  6301. qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1;
  6302. chroma_qp[1] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6303. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn1_qp ) + 1 ) >> 1;
  6304. /* Filter edge */
  6305. tprintf("filter mb:%d/%d MBAFF, QPy:%d/%d, QPc:%d/%d ls:%d uvls:%d", mb_x, mb_y, qp[0], qp[1], chroma_qp[0], chroma_qp[1], linesize, uvlinesize);
  6306. { int i; for (i = 0; i < 8; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6307. filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp );
  6308. filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, chroma_qp );
  6309. filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, chroma_qp );
  6310. }
  6311. /* dir : 0 -> vertical edge, 1 -> horizontal edge */
  6312. for( dir = 0; dir < 2; dir++ )
  6313. {
  6314. int edge;
  6315. const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
  6316. const int mbm_type = s->current_picture.mb_type[mbm_xy];
  6317. int start = h->slice_table[mbm_xy] == 255 ? 1 : 0;
  6318. const int edges = (mb_type & (MB_TYPE_16x16|MB_TYPE_SKIP))
  6319. == (MB_TYPE_16x16|MB_TYPE_SKIP) ? 1 : 4;
  6320. // how often to recheck mv-based bS when iterating between edges
  6321. const int mask_edge = (mb_type & (MB_TYPE_16x16 | (MB_TYPE_16x8 << dir))) ? 3 :
  6322. (mb_type & (MB_TYPE_8x16 >> dir)) ? 1 : 0;
  6323. // how often to recheck mv-based bS when iterating along each edge
  6324. const int mask_par0 = mb_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir));
  6325. if (first_vertical_edge_done) {
  6326. start = 1;
  6327. first_vertical_edge_done = 0;
  6328. }
  6329. if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
  6330. start = 1;
  6331. if (FRAME_MBAFF && (dir == 1) && ((mb_y&1) == 0) && start == 0
  6332. && !IS_INTERLACED(mb_type)
  6333. && IS_INTERLACED(mbm_type)
  6334. ) {
  6335. // This is a special case in the norm where the filtering must
  6336. // be done twice (one each of the field) even if we are in a
  6337. // frame macroblock.
  6338. //
  6339. static const int nnz_idx[4] = {4,5,6,3};
  6340. unsigned int tmp_linesize = 2 * linesize;
  6341. unsigned int tmp_uvlinesize = 2 * uvlinesize;
  6342. int mbn_xy = mb_xy - 2 * s->mb_stride;
  6343. int qp, chroma_qp;
  6344. int i, j;
  6345. int16_t bS[4];
  6346. for(j=0; j<2; j++, mbn_xy += s->mb_stride){
  6347. if( IS_INTRA(mb_type) ||
  6348. IS_INTRA(s->current_picture.mb_type[mbn_xy]) ) {
  6349. bS[0] = bS[1] = bS[2] = bS[3] = 3;
  6350. } else {
  6351. const uint8_t *mbn_nnz = h->non_zero_count[mbn_xy];
  6352. for( i = 0; i < 4; i++ ) {
  6353. if( h->non_zero_count_cache[scan8[0]+i] != 0 ||
  6354. mbn_nnz[nnz_idx[i]] != 0 )
  6355. bS[i] = 2;
  6356. else
  6357. bS[i] = 1;
  6358. }
  6359. }
  6360. // Do not use s->qscale as luma quantizer because it has not the same
  6361. // value in IPCM macroblocks.
  6362. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6363. tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, tmp_linesize, tmp_uvlinesize);
  6364. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6365. filter_mb_edgeh( h, &img_y[j*linesize], tmp_linesize, bS, qp );
  6366. chroma_qp = ( h->chroma_qp +
  6367. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6368. filter_mb_edgech( h, &img_cb[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6369. filter_mb_edgech( h, &img_cr[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6370. }
  6371. start = 1;
  6372. }
  6373. /* Calculate bS */
  6374. for( edge = start; edge < edges; edge++ ) {
  6375. /* mbn_xy: neighbor macroblock */
  6376. const int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
  6377. const int mbn_type = s->current_picture.mb_type[mbn_xy];
  6378. int16_t bS[4];
  6379. int qp;
  6380. if( (edge&1) && IS_8x8DCT(mb_type) )
  6381. continue;
  6382. if( IS_INTRA(mb_type) ||
  6383. IS_INTRA(mbn_type) ) {
  6384. int value;
  6385. if (edge == 0) {
  6386. if ( (!IS_INTERLACED(mb_type) && !IS_INTERLACED(mbm_type))
  6387. || ((FRAME_MBAFF || (s->picture_structure != PICT_FRAME)) && (dir == 0))
  6388. ) {
  6389. value = 4;
  6390. } else {
  6391. value = 3;
  6392. }
  6393. } else {
  6394. value = 3;
  6395. }
  6396. bS[0] = bS[1] = bS[2] = bS[3] = value;
  6397. } else {
  6398. int i, l;
  6399. int mv_done;
  6400. if( edge & mask_edge ) {
  6401. bS[0] = bS[1] = bS[2] = bS[3] = 0;
  6402. mv_done = 1;
  6403. }
  6404. else if( FRAME_MBAFF && IS_INTERLACED(mb_type ^ mbn_type)) {
  6405. bS[0] = bS[1] = bS[2] = bS[3] = 1;
  6406. mv_done = 1;
  6407. }
  6408. else if( mask_par0 && (edge || (mbn_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir)))) ) {
  6409. int b_idx= 8 + 4 + edge * (dir ? 8:1);
  6410. int bn_idx= b_idx - (dir ? 8:1);
  6411. int v = 0;
  6412. for( l = 0; !v && l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6413. v |= ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6414. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6415. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit;
  6416. }
  6417. bS[0] = bS[1] = bS[2] = bS[3] = v;
  6418. mv_done = 1;
  6419. }
  6420. else
  6421. mv_done = 0;
  6422. for( i = 0; i < 4; i++ ) {
  6423. int x = dir == 0 ? edge : i;
  6424. int y = dir == 0 ? i : edge;
  6425. int b_idx= 8 + 4 + x + 8*y;
  6426. int bn_idx= b_idx - (dir ? 8:1);
  6427. if( h->non_zero_count_cache[b_idx] != 0 ||
  6428. h->non_zero_count_cache[bn_idx] != 0 ) {
  6429. bS[i] = 2;
  6430. }
  6431. else if(!mv_done)
  6432. {
  6433. bS[i] = 0;
  6434. for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6435. if( ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6436. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6437. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit ) {
  6438. bS[i] = 1;
  6439. break;
  6440. }
  6441. }
  6442. }
  6443. }
  6444. if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
  6445. continue;
  6446. }
  6447. /* Filter edge */
  6448. // Do not use s->qscale as luma quantizer because it has not the same
  6449. // value in IPCM macroblocks.
  6450. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6451. //tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d, QPc:%d, QPcn:%d\n", mb_x, mb_y, dir, edge, qp, h->chroma_qp, s->current_picture.qscale_table[mbn_xy]);
  6452. tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
  6453. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6454. if( dir == 0 ) {
  6455. filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
  6456. if( (edge&1) == 0 ) {
  6457. int chroma_qp = ( h->chroma_qp +
  6458. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6459. filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp );
  6460. filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp );
  6461. }
  6462. } else {
  6463. filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
  6464. if( (edge&1) == 0 ) {
  6465. int chroma_qp = ( h->chroma_qp +
  6466. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6467. filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6468. filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6469. }
  6470. }
  6471. }
  6472. }
  6473. }
  6474. static int decode_slice(H264Context *h){
  6475. MpegEncContext * const s = &h->s;
  6476. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  6477. s->mb_skip_run= -1;
  6478. if( h->pps.cabac ) {
  6479. int i;
  6480. /* realign */
  6481. align_get_bits( &s->gb );
  6482. /* init cabac */
  6483. ff_init_cabac_states( &h->cabac);
  6484. ff_init_cabac_decoder( &h->cabac,
  6485. s->gb.buffer + get_bits_count(&s->gb)/8,
  6486. ( s->gb.size_in_bits - get_bits_count(&s->gb) + 7)/8);
  6487. /* calculate pre-state */
  6488. for( i= 0; i < 460; i++ ) {
  6489. int pre;
  6490. if( h->slice_type == I_TYPE )
  6491. pre = clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
  6492. else
  6493. pre = clip( ((cabac_context_init_PB[h->cabac_init_idc][i][0] * s->qscale) >>4 ) + cabac_context_init_PB[h->cabac_init_idc][i][1], 1, 126 );
  6494. if( pre <= 63 )
  6495. h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
  6496. else
  6497. h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
  6498. }
  6499. for(;;){
  6500. //START_TIMER
  6501. int ret = decode_mb_cabac(h);
  6502. int eos;
  6503. //STOP_TIMER("decode_mb_cabac")
  6504. if(ret>=0) hl_decode_mb(h);
  6505. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  6506. s->mb_y++;
  6507. if(ret>=0) ret = decode_mb_cabac(h);
  6508. if(ret>=0) hl_decode_mb(h);
  6509. s->mb_y--;
  6510. }
  6511. eos = get_cabac_terminate( &h->cabac );
  6512. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  6513. 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);
  6514. 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);
  6515. return -1;
  6516. }
  6517. if( ++s->mb_x >= s->mb_width ) {
  6518. s->mb_x = 0;
  6519. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6520. ++s->mb_y;
  6521. if(FRAME_MBAFF) {
  6522. ++s->mb_y;
  6523. }
  6524. }
  6525. if( eos || s->mb_y >= s->mb_height ) {
  6526. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6527. 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);
  6528. return 0;
  6529. }
  6530. }
  6531. } else {
  6532. for(;;){
  6533. int ret = decode_mb_cavlc(h);
  6534. if(ret>=0) hl_decode_mb(h);
  6535. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  6536. s->mb_y++;
  6537. ret = decode_mb_cavlc(h);
  6538. if(ret>=0) hl_decode_mb(h);
  6539. s->mb_y--;
  6540. }
  6541. if(ret<0){
  6542. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6543. 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);
  6544. return -1;
  6545. }
  6546. if(++s->mb_x >= s->mb_width){
  6547. s->mb_x=0;
  6548. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6549. ++s->mb_y;
  6550. if(FRAME_MBAFF) {
  6551. ++s->mb_y;
  6552. }
  6553. if(s->mb_y >= s->mb_height){
  6554. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6555. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  6556. 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);
  6557. return 0;
  6558. }else{
  6559. 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);
  6560. return -1;
  6561. }
  6562. }
  6563. }
  6564. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  6565. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6566. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  6567. 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);
  6568. return 0;
  6569. }else{
  6570. 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);
  6571. return -1;
  6572. }
  6573. }
  6574. }
  6575. }
  6576. #if 0
  6577. for(;s->mb_y < s->mb_height; s->mb_y++){
  6578. for(;s->mb_x < s->mb_width; s->mb_x++){
  6579. int ret= decode_mb(h);
  6580. hl_decode_mb(h);
  6581. if(ret<0){
  6582. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6583. 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);
  6584. return -1;
  6585. }
  6586. if(++s->mb_x >= s->mb_width){
  6587. s->mb_x=0;
  6588. if(++s->mb_y >= s->mb_height){
  6589. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6590. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  6591. return 0;
  6592. }else{
  6593. 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);
  6594. return -1;
  6595. }
  6596. }
  6597. }
  6598. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  6599. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6600. 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);
  6601. return 0;
  6602. }else{
  6603. 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);
  6604. return -1;
  6605. }
  6606. }
  6607. }
  6608. s->mb_x=0;
  6609. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6610. }
  6611. #endif
  6612. return -1; //not reached
  6613. }
  6614. static int decode_unregistered_user_data(H264Context *h, int size){
  6615. MpegEncContext * const s = &h->s;
  6616. uint8_t user_data[16+256];
  6617. int e, build, i;
  6618. if(size<16)
  6619. return -1;
  6620. for(i=0; i<sizeof(user_data)-1 && i<size; i++){
  6621. user_data[i]= get_bits(&s->gb, 8);
  6622. }
  6623. user_data[i]= 0;
  6624. e= sscanf(user_data+16, "x264 - core %d"/*%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html*/, &build);
  6625. if(e==1 && build>=0)
  6626. h->x264_build= build;
  6627. if(s->avctx->debug & FF_DEBUG_BUGS)
  6628. av_log(s->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data+16);
  6629. for(; i<size; i++)
  6630. skip_bits(&s->gb, 8);
  6631. return 0;
  6632. }
  6633. static int decode_sei(H264Context *h){
  6634. MpegEncContext * const s = &h->s;
  6635. while(get_bits_count(&s->gb) + 16 < s->gb.size_in_bits){
  6636. int size, type;
  6637. type=0;
  6638. do{
  6639. type+= show_bits(&s->gb, 8);
  6640. }while(get_bits(&s->gb, 8) == 255);
  6641. size=0;
  6642. do{
  6643. size+= show_bits(&s->gb, 8);
  6644. }while(get_bits(&s->gb, 8) == 255);
  6645. switch(type){
  6646. case 5:
  6647. if(decode_unregistered_user_data(h, size) < 0)
  6648. return -1;
  6649. break;
  6650. default:
  6651. skip_bits(&s->gb, 8*size);
  6652. }
  6653. //FIXME check bits here
  6654. align_get_bits(&s->gb);
  6655. }
  6656. return 0;
  6657. }
  6658. static inline void decode_hrd_parameters(H264Context *h, SPS *sps){
  6659. MpegEncContext * const s = &h->s;
  6660. int cpb_count, i;
  6661. cpb_count = get_ue_golomb(&s->gb) + 1;
  6662. get_bits(&s->gb, 4); /* bit_rate_scale */
  6663. get_bits(&s->gb, 4); /* cpb_size_scale */
  6664. for(i=0; i<cpb_count; i++){
  6665. get_ue_golomb(&s->gb); /* bit_rate_value_minus1 */
  6666. get_ue_golomb(&s->gb); /* cpb_size_value_minus1 */
  6667. get_bits1(&s->gb); /* cbr_flag */
  6668. }
  6669. get_bits(&s->gb, 5); /* initial_cpb_removal_delay_length_minus1 */
  6670. get_bits(&s->gb, 5); /* cpb_removal_delay_length_minus1 */
  6671. get_bits(&s->gb, 5); /* dpb_output_delay_length_minus1 */
  6672. get_bits(&s->gb, 5); /* time_offset_length */
  6673. }
  6674. static inline int decode_vui_parameters(H264Context *h, SPS *sps){
  6675. MpegEncContext * const s = &h->s;
  6676. int aspect_ratio_info_present_flag, aspect_ratio_idc;
  6677. int nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag;
  6678. aspect_ratio_info_present_flag= get_bits1(&s->gb);
  6679. if( aspect_ratio_info_present_flag ) {
  6680. aspect_ratio_idc= get_bits(&s->gb, 8);
  6681. if( aspect_ratio_idc == EXTENDED_SAR ) {
  6682. sps->sar.num= get_bits(&s->gb, 16);
  6683. sps->sar.den= get_bits(&s->gb, 16);
  6684. }else if(aspect_ratio_idc < 14){
  6685. sps->sar= pixel_aspect[aspect_ratio_idc];
  6686. }else{
  6687. av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
  6688. return -1;
  6689. }
  6690. }else{
  6691. sps->sar.num=
  6692. sps->sar.den= 0;
  6693. }
  6694. // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
  6695. if(get_bits1(&s->gb)){ /* overscan_info_present_flag */
  6696. get_bits1(&s->gb); /* overscan_appropriate_flag */
  6697. }
  6698. if(get_bits1(&s->gb)){ /* video_signal_type_present_flag */
  6699. get_bits(&s->gb, 3); /* video_format */
  6700. get_bits1(&s->gb); /* video_full_range_flag */
  6701. if(get_bits1(&s->gb)){ /* colour_description_present_flag */
  6702. get_bits(&s->gb, 8); /* colour_primaries */
  6703. get_bits(&s->gb, 8); /* transfer_characteristics */
  6704. get_bits(&s->gb, 8); /* matrix_coefficients */
  6705. }
  6706. }
  6707. if(get_bits1(&s->gb)){ /* chroma_location_info_present_flag */
  6708. get_ue_golomb(&s->gb); /* chroma_sample_location_type_top_field */
  6709. get_ue_golomb(&s->gb); /* chroma_sample_location_type_bottom_field */
  6710. }
  6711. sps->timing_info_present_flag = get_bits1(&s->gb);
  6712. if(sps->timing_info_present_flag){
  6713. sps->num_units_in_tick = get_bits_long(&s->gb, 32);
  6714. sps->time_scale = get_bits_long(&s->gb, 32);
  6715. sps->fixed_frame_rate_flag = get_bits1(&s->gb);
  6716. }
  6717. nal_hrd_parameters_present_flag = get_bits1(&s->gb);
  6718. if(nal_hrd_parameters_present_flag)
  6719. decode_hrd_parameters(h, sps);
  6720. vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
  6721. if(vcl_hrd_parameters_present_flag)
  6722. decode_hrd_parameters(h, sps);
  6723. if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag)
  6724. get_bits1(&s->gb); /* low_delay_hrd_flag */
  6725. get_bits1(&s->gb); /* pic_struct_present_flag */
  6726. sps->bitstream_restriction_flag = get_bits1(&s->gb);
  6727. if(sps->bitstream_restriction_flag){
  6728. get_bits1(&s->gb); /* motion_vectors_over_pic_boundaries_flag */
  6729. get_ue_golomb(&s->gb); /* max_bytes_per_pic_denom */
  6730. get_ue_golomb(&s->gb); /* max_bits_per_mb_denom */
  6731. get_ue_golomb(&s->gb); /* log2_max_mv_length_horizontal */
  6732. get_ue_golomb(&s->gb); /* log2_max_mv_length_vertical */
  6733. sps->num_reorder_frames = get_ue_golomb(&s->gb);
  6734. get_ue_golomb(&s->gb); /* max_dec_frame_buffering */
  6735. }
  6736. return 0;
  6737. }
  6738. static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,
  6739. const uint8_t *jvt_list, const uint8_t *fallback_list){
  6740. MpegEncContext * const s = &h->s;
  6741. int i, last = 8, next = 8;
  6742. const uint8_t *scan = size == 16 ? zigzag_scan : zigzag_scan8x8;
  6743. if(!get_bits1(&s->gb)) /* matrix not written, we use the predicted one */
  6744. memcpy(factors, fallback_list, size*sizeof(uint8_t));
  6745. else
  6746. for(i=0;i<size;i++){
  6747. if(next)
  6748. next = (last + get_se_golomb(&s->gb)) & 0xff;
  6749. if(!i && !next){ /* matrix not written, we use the preset one */
  6750. memcpy(factors, jvt_list, size*sizeof(uint8_t));
  6751. break;
  6752. }
  6753. last = factors[scan[i]] = next ? next : last;
  6754. }
  6755. }
  6756. static void decode_scaling_matrices(H264Context *h, SPS *sps, PPS *pps, int is_sps,
  6757. uint8_t (*scaling_matrix4)[16], uint8_t (*scaling_matrix8)[64]){
  6758. MpegEncContext * const s = &h->s;
  6759. int fallback_sps = !is_sps && sps->scaling_matrix_present;
  6760. const uint8_t *fallback[4] = {
  6761. fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
  6762. fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
  6763. fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
  6764. fallback_sps ? sps->scaling_matrix8[1] : default_scaling8[1]
  6765. };
  6766. if(get_bits1(&s->gb)){
  6767. sps->scaling_matrix_present |= is_sps;
  6768. decode_scaling_list(h,scaling_matrix4[0],16,default_scaling4[0],fallback[0]); // Intra, Y
  6769. decode_scaling_list(h,scaling_matrix4[1],16,default_scaling4[0],scaling_matrix4[0]); // Intra, Cr
  6770. decode_scaling_list(h,scaling_matrix4[2],16,default_scaling4[0],scaling_matrix4[1]); // Intra, Cb
  6771. decode_scaling_list(h,scaling_matrix4[3],16,default_scaling4[1],fallback[1]); // Inter, Y
  6772. decode_scaling_list(h,scaling_matrix4[4],16,default_scaling4[1],scaling_matrix4[3]); // Inter, Cr
  6773. decode_scaling_list(h,scaling_matrix4[5],16,default_scaling4[1],scaling_matrix4[4]); // Inter, Cb
  6774. if(is_sps || pps->transform_8x8_mode){
  6775. decode_scaling_list(h,scaling_matrix8[0],64,default_scaling8[0],fallback[2]); // Intra, Y
  6776. decode_scaling_list(h,scaling_matrix8[1],64,default_scaling8[1],fallback[3]); // Inter, Y
  6777. }
  6778. } else if(fallback_sps) {
  6779. memcpy(scaling_matrix4, sps->scaling_matrix4, 6*16*sizeof(uint8_t));
  6780. memcpy(scaling_matrix8, sps->scaling_matrix8, 2*64*sizeof(uint8_t));
  6781. }
  6782. }
  6783. static inline int decode_seq_parameter_set(H264Context *h){
  6784. MpegEncContext * const s = &h->s;
  6785. int profile_idc, level_idc;
  6786. int sps_id, i;
  6787. SPS *sps;
  6788. profile_idc= get_bits(&s->gb, 8);
  6789. get_bits1(&s->gb); //constraint_set0_flag
  6790. get_bits1(&s->gb); //constraint_set1_flag
  6791. get_bits1(&s->gb); //constraint_set2_flag
  6792. get_bits1(&s->gb); //constraint_set3_flag
  6793. get_bits(&s->gb, 4); // reserved
  6794. level_idc= get_bits(&s->gb, 8);
  6795. sps_id= get_ue_golomb(&s->gb);
  6796. sps= &h->sps_buffer[ sps_id ];
  6797. sps->profile_idc= profile_idc;
  6798. sps->level_idc= level_idc;
  6799. if(sps->profile_idc >= 100){ //high profile
  6800. if(get_ue_golomb(&s->gb) == 3) //chroma_format_idc
  6801. get_bits1(&s->gb); //residual_color_transform_flag
  6802. get_ue_golomb(&s->gb); //bit_depth_luma_minus8
  6803. get_ue_golomb(&s->gb); //bit_depth_chroma_minus8
  6804. sps->transform_bypass = get_bits1(&s->gb);
  6805. decode_scaling_matrices(h, sps, NULL, 1, sps->scaling_matrix4, sps->scaling_matrix8);
  6806. }else
  6807. sps->scaling_matrix_present = 0;
  6808. sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
  6809. sps->poc_type= get_ue_golomb(&s->gb);
  6810. if(sps->poc_type == 0){ //FIXME #define
  6811. sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
  6812. } else if(sps->poc_type == 1){//FIXME #define
  6813. sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
  6814. sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
  6815. sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
  6816. sps->poc_cycle_length= get_ue_golomb(&s->gb);
  6817. for(i=0; i<sps->poc_cycle_length; i++)
  6818. sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
  6819. }
  6820. if(sps->poc_type > 2){
  6821. av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
  6822. return -1;
  6823. }
  6824. sps->ref_frame_count= get_ue_golomb(&s->gb);
  6825. if(sps->ref_frame_count > MAX_PICTURE_COUNT-2){
  6826. av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");
  6827. }
  6828. sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
  6829. sps->mb_width= get_ue_golomb(&s->gb) + 1;
  6830. sps->mb_height= get_ue_golomb(&s->gb) + 1;
  6831. if((unsigned)sps->mb_width >= INT_MAX/16 || (unsigned)sps->mb_height >= INT_MAX/16 ||
  6832. avcodec_check_dimensions(NULL, 16*sps->mb_width, 16*sps->mb_height))
  6833. return -1;
  6834. sps->frame_mbs_only_flag= get_bits1(&s->gb);
  6835. if(!sps->frame_mbs_only_flag)
  6836. sps->mb_aff= get_bits1(&s->gb);
  6837. else
  6838. sps->mb_aff= 0;
  6839. sps->direct_8x8_inference_flag= get_bits1(&s->gb);
  6840. #ifndef ALLOW_INTERLACE
  6841. if(sps->mb_aff)
  6842. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF support not included; enable it at compile-time.\n");
  6843. #endif
  6844. if(!sps->direct_8x8_inference_flag && sps->mb_aff)
  6845. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + !direct_8x8_inference is not implemented\n");
  6846. sps->crop= get_bits1(&s->gb);
  6847. if(sps->crop){
  6848. sps->crop_left = get_ue_golomb(&s->gb);
  6849. sps->crop_right = get_ue_golomb(&s->gb);
  6850. sps->crop_top = get_ue_golomb(&s->gb);
  6851. sps->crop_bottom= get_ue_golomb(&s->gb);
  6852. if(sps->crop_left || sps->crop_top){
  6853. av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ...\n");
  6854. }
  6855. }else{
  6856. sps->crop_left =
  6857. sps->crop_right =
  6858. sps->crop_top =
  6859. sps->crop_bottom= 0;
  6860. }
  6861. sps->vui_parameters_present_flag= get_bits1(&s->gb);
  6862. if( sps->vui_parameters_present_flag )
  6863. decode_vui_parameters(h, sps);
  6864. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6865. av_log(h->s.avctx, AV_LOG_DEBUG, "sps:%d profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%d/%d/%d/%d %s\n",
  6866. sps_id, sps->profile_idc, sps->level_idc,
  6867. sps->poc_type,
  6868. sps->ref_frame_count,
  6869. sps->mb_width, sps->mb_height,
  6870. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  6871. sps->direct_8x8_inference_flag ? "8B8" : "",
  6872. sps->crop_left, sps->crop_right,
  6873. sps->crop_top, sps->crop_bottom,
  6874. sps->vui_parameters_present_flag ? "VUI" : ""
  6875. );
  6876. }
  6877. return 0;
  6878. }
  6879. static inline int decode_picture_parameter_set(H264Context *h, int bit_length){
  6880. MpegEncContext * const s = &h->s;
  6881. int pps_id= get_ue_golomb(&s->gb);
  6882. PPS *pps= &h->pps_buffer[pps_id];
  6883. pps->sps_id= get_ue_golomb(&s->gb);
  6884. pps->cabac= get_bits1(&s->gb);
  6885. pps->pic_order_present= get_bits1(&s->gb);
  6886. pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
  6887. if(pps->slice_group_count > 1 ){
  6888. pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
  6889. av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
  6890. switch(pps->mb_slice_group_map_type){
  6891. case 0:
  6892. #if 0
  6893. | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
  6894. | run_length[ i ] |1 |ue(v) |
  6895. #endif
  6896. break;
  6897. case 2:
  6898. #if 0
  6899. | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
  6900. |{ | | |
  6901. | top_left_mb[ i ] |1 |ue(v) |
  6902. | bottom_right_mb[ i ] |1 |ue(v) |
  6903. | } | | |
  6904. #endif
  6905. break;
  6906. case 3:
  6907. case 4:
  6908. case 5:
  6909. #if 0
  6910. | slice_group_change_direction_flag |1 |u(1) |
  6911. | slice_group_change_rate_minus1 |1 |ue(v) |
  6912. #endif
  6913. break;
  6914. case 6:
  6915. #if 0
  6916. | slice_group_id_cnt_minus1 |1 |ue(v) |
  6917. | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
  6918. |) | | |
  6919. | slice_group_id[ i ] |1 |u(v) |
  6920. #endif
  6921. break;
  6922. }
  6923. }
  6924. pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  6925. pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  6926. if(pps->ref_count[0] > 32 || pps->ref_count[1] > 32){
  6927. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
  6928. return -1;
  6929. }
  6930. pps->weighted_pred= get_bits1(&s->gb);
  6931. pps->weighted_bipred_idc= get_bits(&s->gb, 2);
  6932. pps->init_qp= get_se_golomb(&s->gb) + 26;
  6933. pps->init_qs= get_se_golomb(&s->gb) + 26;
  6934. pps->chroma_qp_index_offset= get_se_golomb(&s->gb);
  6935. pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
  6936. pps->constrained_intra_pred= get_bits1(&s->gb);
  6937. pps->redundant_pic_cnt_present = get_bits1(&s->gb);
  6938. pps->transform_8x8_mode= 0;
  6939. h->dequant_coeff_pps= -1; //contents of sps/pps can change even if id doesn't, so reinit
  6940. memset(pps->scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  6941. memset(pps->scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  6942. if(get_bits_count(&s->gb) < bit_length){
  6943. pps->transform_8x8_mode= get_bits1(&s->gb);
  6944. decode_scaling_matrices(h, &h->sps_buffer[pps->sps_id], pps, 0, pps->scaling_matrix4, pps->scaling_matrix8);
  6945. get_se_golomb(&s->gb); //second_chroma_qp_index_offset
  6946. }
  6947. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6948. av_log(h->s.avctx, AV_LOG_DEBUG, "pps:%d sps:%d %s slice_groups:%d ref:%d/%d %s qp:%d/%d/%d %s %s %s %s\n",
  6949. pps_id, pps->sps_id,
  6950. pps->cabac ? "CABAC" : "CAVLC",
  6951. pps->slice_group_count,
  6952. pps->ref_count[0], pps->ref_count[1],
  6953. pps->weighted_pred ? "weighted" : "",
  6954. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset,
  6955. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  6956. pps->constrained_intra_pred ? "CONSTR" : "",
  6957. pps->redundant_pic_cnt_present ? "REDU" : "",
  6958. pps->transform_8x8_mode ? "8x8DCT" : ""
  6959. );
  6960. }
  6961. return 0;
  6962. }
  6963. /**
  6964. * finds the end of the current frame in the bitstream.
  6965. * @return the position of the first byte of the next frame, or -1
  6966. */
  6967. static int find_frame_end(H264Context *h, const uint8_t *buf, int buf_size){
  6968. int i;
  6969. uint32_t state;
  6970. ParseContext *pc = &(h->s.parse_context);
  6971. //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
  6972. // mb_addr= pc->mb_addr - 1;
  6973. state= pc->state;
  6974. for(i=0; i<=buf_size; i++){
  6975. if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  6976. tprintf("find_frame_end new startcode = %08x, frame_start_found = %d, pos = %d\n", state, pc->frame_start_found, i);
  6977. if(pc->frame_start_found){
  6978. // If there isn't one more byte in the buffer
  6979. // the test on first_mb_in_slice cannot be done yet
  6980. // do it at next call.
  6981. if (i >= buf_size) break;
  6982. if (buf[i] & 0x80) {
  6983. // first_mb_in_slice is 0, probably the first nal of a new
  6984. // slice
  6985. tprintf("find_frame_end frame_end_found, state = %08x, pos = %d\n", state, i);
  6986. pc->state=-1;
  6987. pc->frame_start_found= 0;
  6988. return i-4;
  6989. }
  6990. }
  6991. pc->frame_start_found = 1;
  6992. }
  6993. if((state&0xFFFFFF1F) == 0x107 || (state&0xFFFFFF1F) == 0x108 || (state&0xFFFFFF1F) == 0x109){
  6994. if(pc->frame_start_found){
  6995. pc->state=-1;
  6996. pc->frame_start_found= 0;
  6997. return i-4;
  6998. }
  6999. }
  7000. if (i<buf_size)
  7001. state= (state<<8) | buf[i];
  7002. }
  7003. pc->state= state;
  7004. return END_NOT_FOUND;
  7005. }
  7006. #ifdef CONFIG_H264_PARSER
  7007. static int h264_parse(AVCodecParserContext *s,
  7008. AVCodecContext *avctx,
  7009. uint8_t **poutbuf, int *poutbuf_size,
  7010. const uint8_t *buf, int buf_size)
  7011. {
  7012. H264Context *h = s->priv_data;
  7013. ParseContext *pc = &h->s.parse_context;
  7014. int next;
  7015. next= find_frame_end(h, buf, buf_size);
  7016. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  7017. *poutbuf = NULL;
  7018. *poutbuf_size = 0;
  7019. return buf_size;
  7020. }
  7021. *poutbuf = (uint8_t *)buf;
  7022. *poutbuf_size = buf_size;
  7023. return next;
  7024. }
  7025. static int h264_split(AVCodecContext *avctx,
  7026. const uint8_t *buf, int buf_size)
  7027. {
  7028. int i;
  7029. uint32_t state = -1;
  7030. int has_sps= 0;
  7031. for(i=0; i<=buf_size; i++){
  7032. if((state&0xFFFFFF1F) == 0x107)
  7033. has_sps=1;
  7034. /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  7035. }*/
  7036. if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
  7037. if(has_sps){
  7038. while(i>4 && buf[i-5]==0) i--;
  7039. return i-4;
  7040. }
  7041. }
  7042. if (i<buf_size)
  7043. state= (state<<8) | buf[i];
  7044. }
  7045. return 0;
  7046. }
  7047. #endif /* CONFIG_H264_PARSER */
  7048. static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){
  7049. MpegEncContext * const s = &h->s;
  7050. AVCodecContext * const avctx= s->avctx;
  7051. int buf_index=0;
  7052. #if 0
  7053. int i;
  7054. for(i=0; i<50; i++){
  7055. av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
  7056. }
  7057. #endif
  7058. h->slice_num = 0;
  7059. s->current_picture_ptr= NULL;
  7060. for(;;){
  7061. int consumed;
  7062. int dst_length;
  7063. int bit_length;
  7064. uint8_t *ptr;
  7065. int i, nalsize = 0;
  7066. if(h->is_avc) {
  7067. if(buf_index >= buf_size) break;
  7068. nalsize = 0;
  7069. for(i = 0; i < h->nal_length_size; i++)
  7070. nalsize = (nalsize << 8) | buf[buf_index++];
  7071. if(nalsize <= 1){
  7072. if(nalsize == 1){
  7073. buf_index++;
  7074. continue;
  7075. }else{
  7076. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  7077. break;
  7078. }
  7079. }
  7080. } else {
  7081. // start code prefix search
  7082. for(; buf_index + 3 < buf_size; buf_index++){
  7083. // this should allways succeed in the first iteration
  7084. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  7085. break;
  7086. }
  7087. if(buf_index+3 >= buf_size) break;
  7088. buf_index+=3;
  7089. }
  7090. ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, h->is_avc ? nalsize : buf_size - buf_index);
  7091. while(ptr[dst_length - 1] == 0 && dst_length > 1)
  7092. dst_length--;
  7093. bit_length= 8*dst_length - decode_rbsp_trailing(ptr + dst_length - 1);
  7094. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  7095. 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);
  7096. }
  7097. if (h->is_avc && (nalsize != consumed))
  7098. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  7099. buf_index += consumed;
  7100. if( (s->hurry_up == 1 && h->nal_ref_idc == 0) //FIXME dont discard SEI id
  7101. ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  7102. continue;
  7103. switch(h->nal_unit_type){
  7104. case NAL_IDR_SLICE:
  7105. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  7106. case NAL_SLICE:
  7107. init_get_bits(&s->gb, ptr, bit_length);
  7108. h->intra_gb_ptr=
  7109. h->inter_gb_ptr= &s->gb;
  7110. s->data_partitioning = 0;
  7111. if(decode_slice_header(h) < 0){
  7112. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  7113. break;
  7114. }
  7115. s->current_picture_ptr->key_frame= (h->nal_unit_type == NAL_IDR_SLICE);
  7116. if(h->redundant_pic_count==0 && s->hurry_up < 5
  7117. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  7118. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  7119. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  7120. && avctx->skip_frame < AVDISCARD_ALL)
  7121. decode_slice(h);
  7122. break;
  7123. case NAL_DPA:
  7124. init_get_bits(&s->gb, ptr, bit_length);
  7125. h->intra_gb_ptr=
  7126. h->inter_gb_ptr= NULL;
  7127. s->data_partitioning = 1;
  7128. if(decode_slice_header(h) < 0){
  7129. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  7130. }
  7131. break;
  7132. case NAL_DPB:
  7133. init_get_bits(&h->intra_gb, ptr, bit_length);
  7134. h->intra_gb_ptr= &h->intra_gb;
  7135. break;
  7136. case NAL_DPC:
  7137. init_get_bits(&h->inter_gb, ptr, bit_length);
  7138. h->inter_gb_ptr= &h->inter_gb;
  7139. if(h->redundant_pic_count==0 && h->intra_gb_ptr && s->data_partitioning
  7140. && s->hurry_up < 5
  7141. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  7142. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  7143. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  7144. && avctx->skip_frame < AVDISCARD_ALL)
  7145. decode_slice(h);
  7146. break;
  7147. case NAL_SEI:
  7148. init_get_bits(&s->gb, ptr, bit_length);
  7149. decode_sei(h);
  7150. break;
  7151. case NAL_SPS:
  7152. init_get_bits(&s->gb, ptr, bit_length);
  7153. decode_seq_parameter_set(h);
  7154. if(s->flags& CODEC_FLAG_LOW_DELAY)
  7155. s->low_delay=1;
  7156. if(avctx->has_b_frames < 2)
  7157. avctx->has_b_frames= !s->low_delay;
  7158. break;
  7159. case NAL_PPS:
  7160. init_get_bits(&s->gb, ptr, bit_length);
  7161. decode_picture_parameter_set(h, bit_length);
  7162. break;
  7163. case NAL_AUD:
  7164. case NAL_END_SEQUENCE:
  7165. case NAL_END_STREAM:
  7166. case NAL_FILLER_DATA:
  7167. case NAL_SPS_EXT:
  7168. case NAL_AUXILIARY_SLICE:
  7169. break;
  7170. default:
  7171. av_log(avctx, AV_LOG_ERROR, "Unknown NAL code: %d\n", h->nal_unit_type);
  7172. }
  7173. }
  7174. if(!s->current_picture_ptr) return buf_index; //no frame
  7175. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  7176. s->current_picture_ptr->pict_type= s->pict_type;
  7177. h->prev_frame_num_offset= h->frame_num_offset;
  7178. h->prev_frame_num= h->frame_num;
  7179. if(s->current_picture_ptr->reference){
  7180. h->prev_poc_msb= h->poc_msb;
  7181. h->prev_poc_lsb= h->poc_lsb;
  7182. }
  7183. if(s->current_picture_ptr->reference)
  7184. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  7185. ff_er_frame_end(s);
  7186. MPV_frame_end(s);
  7187. return buf_index;
  7188. }
  7189. /**
  7190. * returns the number of bytes consumed for building the current frame
  7191. */
  7192. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  7193. if(s->flags&CODEC_FLAG_TRUNCATED){
  7194. pos -= s->parse_context.last_index;
  7195. if(pos<0) pos=0; // FIXME remove (unneeded?)
  7196. return pos;
  7197. }else{
  7198. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  7199. if(pos+10>buf_size) pos=buf_size; // oops ;)
  7200. return pos;
  7201. }
  7202. }
  7203. static int decode_frame(AVCodecContext *avctx,
  7204. void *data, int *data_size,
  7205. uint8_t *buf, int buf_size)
  7206. {
  7207. H264Context *h = avctx->priv_data;
  7208. MpegEncContext *s = &h->s;
  7209. AVFrame *pict = data;
  7210. int buf_index;
  7211. s->flags= avctx->flags;
  7212. s->flags2= avctx->flags2;
  7213. /* no supplementary picture */
  7214. if (buf_size == 0) {
  7215. return 0;
  7216. }
  7217. if(s->flags&CODEC_FLAG_TRUNCATED){
  7218. int next= find_frame_end(h, buf, buf_size);
  7219. if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
  7220. return buf_size;
  7221. //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);
  7222. }
  7223. if(h->is_avc && !h->got_avcC) {
  7224. int i, cnt, nalsize;
  7225. unsigned char *p = avctx->extradata;
  7226. if(avctx->extradata_size < 7) {
  7227. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  7228. return -1;
  7229. }
  7230. if(*p != 1) {
  7231. av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
  7232. return -1;
  7233. }
  7234. /* sps and pps in the avcC always have length coded with 2 bytes,
  7235. so put a fake nal_length_size = 2 while parsing them */
  7236. h->nal_length_size = 2;
  7237. // Decode sps from avcC
  7238. cnt = *(p+5) & 0x1f; // Number of sps
  7239. p += 6;
  7240. for (i = 0; i < cnt; i++) {
  7241. nalsize = BE_16(p) + 2;
  7242. if(decode_nal_units(h, p, nalsize) < 0) {
  7243. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  7244. return -1;
  7245. }
  7246. p += nalsize;
  7247. }
  7248. // Decode pps from avcC
  7249. cnt = *(p++); // Number of pps
  7250. for (i = 0; i < cnt; i++) {
  7251. nalsize = BE_16(p) + 2;
  7252. if(decode_nal_units(h, p, nalsize) != nalsize) {
  7253. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  7254. return -1;
  7255. }
  7256. p += nalsize;
  7257. }
  7258. // Now store right nal length size, that will be use to parse all other nals
  7259. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  7260. // Do not reparse avcC
  7261. h->got_avcC = 1;
  7262. }
  7263. if(!h->is_avc && s->avctx->extradata_size && s->picture_number==0){
  7264. if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
  7265. return -1;
  7266. }
  7267. buf_index=decode_nal_units(h, buf, buf_size);
  7268. if(buf_index < 0)
  7269. return -1;
  7270. //FIXME do something with unavailable reference frames
  7271. // if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_index, buf_size);
  7272. if(!s->current_picture_ptr){
  7273. av_log(h->s.avctx, AV_LOG_DEBUG, "error, NO frame\n");
  7274. return -1;
  7275. }
  7276. {
  7277. Picture *out = s->current_picture_ptr;
  7278. #if 0 //decode order
  7279. *data_size = sizeof(AVFrame);
  7280. #else
  7281. /* Sort B-frames into display order */
  7282. Picture *cur = s->current_picture_ptr;
  7283. Picture *prev = h->delayed_output_pic;
  7284. int i, pics, cross_idr, out_of_order, out_idx;
  7285. if(h->sps.bitstream_restriction_flag
  7286. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  7287. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  7288. s->low_delay = 0;
  7289. }
  7290. pics = 0;
  7291. while(h->delayed_pic[pics]) pics++;
  7292. h->delayed_pic[pics++] = cur;
  7293. if(cur->reference == 0)
  7294. cur->reference = 1;
  7295. cross_idr = 0;
  7296. for(i=0; h->delayed_pic[i]; i++)
  7297. if(h->delayed_pic[i]->key_frame || h->delayed_pic[i]->poc==0)
  7298. cross_idr = 1;
  7299. out = h->delayed_pic[0];
  7300. out_idx = 0;
  7301. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
  7302. if(h->delayed_pic[i]->poc < out->poc){
  7303. out = h->delayed_pic[i];
  7304. out_idx = i;
  7305. }
  7306. out_of_order = !cross_idr && prev && out->poc < prev->poc;
  7307. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  7308. { }
  7309. else if(prev && pics <= s->avctx->has_b_frames)
  7310. out = prev;
  7311. else if((out_of_order && pics-1 == s->avctx->has_b_frames && pics < 15)
  7312. || (s->low_delay &&
  7313. ((!cross_idr && prev && out->poc > prev->poc + 2)
  7314. || cur->pict_type == B_TYPE)))
  7315. {
  7316. s->low_delay = 0;
  7317. s->avctx->has_b_frames++;
  7318. out = prev;
  7319. }
  7320. else if(out_of_order)
  7321. out = prev;
  7322. if(out_of_order || pics > s->avctx->has_b_frames){
  7323. for(i=out_idx; h->delayed_pic[i]; i++)
  7324. h->delayed_pic[i] = h->delayed_pic[i+1];
  7325. }
  7326. if(prev == out)
  7327. *data_size = 0;
  7328. else
  7329. *data_size = sizeof(AVFrame);
  7330. if(prev && prev != out && prev->reference == 1)
  7331. prev->reference = 0;
  7332. h->delayed_output_pic = out;
  7333. #endif
  7334. if(out)
  7335. *pict= *(AVFrame*)out;
  7336. else
  7337. av_log(avctx, AV_LOG_DEBUG, "no picture\n");
  7338. }
  7339. assert(pict->data[0] || !*data_size);
  7340. ff_print_debug_info(s, pict);
  7341. //printf("out %d\n", (int)pict->data[0]);
  7342. #if 0 //?
  7343. /* Return the Picture timestamp as the frame number */
  7344. /* we substract 1 because it is added on utils.c */
  7345. avctx->frame_number = s->picture_number - 1;
  7346. #endif
  7347. return get_consumed_bytes(s, buf_index, buf_size);
  7348. }
  7349. #if 0
  7350. static inline void fill_mb_avail(H264Context *h){
  7351. MpegEncContext * const s = &h->s;
  7352. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  7353. if(s->mb_y){
  7354. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  7355. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  7356. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  7357. }else{
  7358. h->mb_avail[0]=
  7359. h->mb_avail[1]=
  7360. h->mb_avail[2]= 0;
  7361. }
  7362. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  7363. h->mb_avail[4]= 1; //FIXME move out
  7364. h->mb_avail[5]= 0; //FIXME move out
  7365. }
  7366. #endif
  7367. #if 0 //selftest
  7368. #define COUNT 8000
  7369. #define SIZE (COUNT*40)
  7370. int main(){
  7371. int i;
  7372. uint8_t temp[SIZE];
  7373. PutBitContext pb;
  7374. GetBitContext gb;
  7375. // int int_temp[10000];
  7376. DSPContext dsp;
  7377. AVCodecContext avctx;
  7378. dsputil_init(&dsp, &avctx);
  7379. init_put_bits(&pb, temp, SIZE);
  7380. printf("testing unsigned exp golomb\n");
  7381. for(i=0; i<COUNT; i++){
  7382. START_TIMER
  7383. set_ue_golomb(&pb, i);
  7384. STOP_TIMER("set_ue_golomb");
  7385. }
  7386. flush_put_bits(&pb);
  7387. init_get_bits(&gb, temp, 8*SIZE);
  7388. for(i=0; i<COUNT; i++){
  7389. int j, s;
  7390. s= show_bits(&gb, 24);
  7391. START_TIMER
  7392. j= get_ue_golomb(&gb);
  7393. if(j != i){
  7394. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7395. // return -1;
  7396. }
  7397. STOP_TIMER("get_ue_golomb");
  7398. }
  7399. init_put_bits(&pb, temp, SIZE);
  7400. printf("testing signed exp golomb\n");
  7401. for(i=0; i<COUNT; i++){
  7402. START_TIMER
  7403. set_se_golomb(&pb, i - COUNT/2);
  7404. STOP_TIMER("set_se_golomb");
  7405. }
  7406. flush_put_bits(&pb);
  7407. init_get_bits(&gb, temp, 8*SIZE);
  7408. for(i=0; i<COUNT; i++){
  7409. int j, s;
  7410. s= show_bits(&gb, 24);
  7411. START_TIMER
  7412. j= get_se_golomb(&gb);
  7413. if(j != i - COUNT/2){
  7414. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7415. // return -1;
  7416. }
  7417. STOP_TIMER("get_se_golomb");
  7418. }
  7419. printf("testing 4x4 (I)DCT\n");
  7420. DCTELEM block[16];
  7421. uint8_t src[16], ref[16];
  7422. uint64_t error= 0, max_error=0;
  7423. for(i=0; i<COUNT; i++){
  7424. int j;
  7425. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  7426. for(j=0; j<16; j++){
  7427. ref[j]= random()%255;
  7428. src[j]= random()%255;
  7429. }
  7430. h264_diff_dct_c(block, src, ref, 4);
  7431. //normalize
  7432. for(j=0; j<16; j++){
  7433. // printf("%d ", block[j]);
  7434. block[j]= block[j]*4;
  7435. if(j&1) block[j]= (block[j]*4 + 2)/5;
  7436. if(j&4) block[j]= (block[j]*4 + 2)/5;
  7437. }
  7438. // printf("\n");
  7439. s->dsp.h264_idct_add(ref, block, 4);
  7440. /* for(j=0; j<16; j++){
  7441. printf("%d ", ref[j]);
  7442. }
  7443. printf("\n");*/
  7444. for(j=0; j<16; j++){
  7445. int diff= FFABS(src[j] - ref[j]);
  7446. error+= diff*diff;
  7447. max_error= FFMAX(max_error, diff);
  7448. }
  7449. }
  7450. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  7451. #if 0
  7452. printf("testing quantizer\n");
  7453. for(qp=0; qp<52; qp++){
  7454. for(i=0; i<16; i++)
  7455. src1_block[i]= src2_block[i]= random()%255;
  7456. }
  7457. #endif
  7458. printf("Testing NAL layer\n");
  7459. uint8_t bitstream[COUNT];
  7460. uint8_t nal[COUNT*2];
  7461. H264Context h;
  7462. memset(&h, 0, sizeof(H264Context));
  7463. for(i=0; i<COUNT; i++){
  7464. int zeros= i;
  7465. int nal_length;
  7466. int consumed;
  7467. int out_length;
  7468. uint8_t *out;
  7469. int j;
  7470. for(j=0; j<COUNT; j++){
  7471. bitstream[j]= (random() % 255) + 1;
  7472. }
  7473. for(j=0; j<zeros; j++){
  7474. int pos= random() % COUNT;
  7475. while(bitstream[pos] == 0){
  7476. pos++;
  7477. pos %= COUNT;
  7478. }
  7479. bitstream[pos]=0;
  7480. }
  7481. START_TIMER
  7482. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  7483. if(nal_length<0){
  7484. printf("encoding failed\n");
  7485. return -1;
  7486. }
  7487. out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
  7488. STOP_TIMER("NAL")
  7489. if(out_length != COUNT){
  7490. printf("incorrect length %d %d\n", out_length, COUNT);
  7491. return -1;
  7492. }
  7493. if(consumed != nal_length){
  7494. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  7495. return -1;
  7496. }
  7497. if(memcmp(bitstream, out, COUNT)){
  7498. printf("missmatch\n");
  7499. return -1;
  7500. }
  7501. }
  7502. printf("Testing RBSP\n");
  7503. return 0;
  7504. }
  7505. #endif
  7506. static int decode_end(AVCodecContext *avctx)
  7507. {
  7508. H264Context *h = avctx->priv_data;
  7509. MpegEncContext *s = &h->s;
  7510. av_freep(&h->rbsp_buffer);
  7511. free_tables(h); //FIXME cleanup init stuff perhaps
  7512. MPV_common_end(s);
  7513. // memset(h, 0, sizeof(H264Context));
  7514. return 0;
  7515. }
  7516. AVCodec h264_decoder = {
  7517. "h264",
  7518. CODEC_TYPE_VIDEO,
  7519. CODEC_ID_H264,
  7520. sizeof(H264Context),
  7521. decode_init,
  7522. NULL,
  7523. decode_end,
  7524. decode_frame,
  7525. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  7526. .flush= flush_dpb,
  7527. };
  7528. #ifdef CONFIG_H264_PARSER
  7529. AVCodecParser h264_parser = {
  7530. { CODEC_ID_H264 },
  7531. sizeof(H264Context),
  7532. NULL,
  7533. h264_parse,
  7534. ff_parse_close,
  7535. h264_split,
  7536. };
  7537. #endif
  7538. #include "svq3.c"