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.

8575 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. unsigned int sps_id;
  107. int cabac; ///< entropy_coding_mode_flag
  108. int pic_order_present; ///< pic_order_present_flag
  109. int slice_group_count; ///< num_slice_groups_minus1 + 1
  110. int mb_slice_group_map_type;
  111. 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. if (dst == NULL){
  1588. return NULL;
  1589. }
  1590. //printf("decoding esc\n");
  1591. si=di=0;
  1592. while(si<length){
  1593. //remove escapes (very rare 1:2^22)
  1594. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  1595. if(src[si+2]==3){ //escape
  1596. dst[di++]= 0;
  1597. dst[di++]= 0;
  1598. si+=3;
  1599. continue;
  1600. }else //next start code
  1601. break;
  1602. }
  1603. dst[di++]= src[si++];
  1604. }
  1605. *dst_length= di;
  1606. *consumed= si + 1;//+1 for the header
  1607. //FIXME store exact number of bits in the getbitcontext (its needed for decoding)
  1608. return dst;
  1609. }
  1610. /**
  1611. * identifies the exact end of the bitstream
  1612. * @return the length of the trailing, or 0 if damaged
  1613. */
  1614. static int decode_rbsp_trailing(uint8_t *src){
  1615. int v= *src;
  1616. int r;
  1617. tprintf("rbsp trailing %X\n", v);
  1618. for(r=1; r<9; r++){
  1619. if(v&1) return r;
  1620. v>>=1;
  1621. }
  1622. return 0;
  1623. }
  1624. /**
  1625. * idct tranforms the 16 dc values and dequantize them.
  1626. * @param qp quantization parameter
  1627. */
  1628. static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1629. #define stride 16
  1630. int i;
  1631. int temp[16]; //FIXME check if this is a good idea
  1632. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1633. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1634. //memset(block, 64, 2*256);
  1635. //return;
  1636. for(i=0; i<4; i++){
  1637. const int offset= y_offset[i];
  1638. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1639. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1640. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1641. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1642. temp[4*i+0]= z0+z3;
  1643. temp[4*i+1]= z1+z2;
  1644. temp[4*i+2]= z1-z2;
  1645. temp[4*i+3]= z0-z3;
  1646. }
  1647. for(i=0; i<4; i++){
  1648. const int offset= x_offset[i];
  1649. const int z0= temp[4*0+i] + temp[4*2+i];
  1650. const int z1= temp[4*0+i] - temp[4*2+i];
  1651. const int z2= temp[4*1+i] - temp[4*3+i];
  1652. const int z3= temp[4*1+i] + temp[4*3+i];
  1653. block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_resdual
  1654. block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
  1655. block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
  1656. block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
  1657. }
  1658. }
  1659. #if 0
  1660. /**
  1661. * dct tranforms the 16 dc values.
  1662. * @param qp quantization parameter ??? FIXME
  1663. */
  1664. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  1665. // const int qmul= dequant_coeff[qp][0];
  1666. int i;
  1667. int temp[16]; //FIXME check if this is a good idea
  1668. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1669. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1670. for(i=0; i<4; i++){
  1671. const int offset= y_offset[i];
  1672. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1673. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1674. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1675. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1676. temp[4*i+0]= z0+z3;
  1677. temp[4*i+1]= z1+z2;
  1678. temp[4*i+2]= z1-z2;
  1679. temp[4*i+3]= z0-z3;
  1680. }
  1681. for(i=0; i<4; i++){
  1682. const int offset= x_offset[i];
  1683. const int z0= temp[4*0+i] + temp[4*2+i];
  1684. const int z1= temp[4*0+i] - temp[4*2+i];
  1685. const int z2= temp[4*1+i] - temp[4*3+i];
  1686. const int z3= temp[4*1+i] + temp[4*3+i];
  1687. block[stride*0 +offset]= (z0 + z3)>>1;
  1688. block[stride*2 +offset]= (z1 + z2)>>1;
  1689. block[stride*8 +offset]= (z1 - z2)>>1;
  1690. block[stride*10+offset]= (z0 - z3)>>1;
  1691. }
  1692. }
  1693. #endif
  1694. #undef xStride
  1695. #undef stride
  1696. static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1697. const int stride= 16*2;
  1698. const int xStride= 16;
  1699. int a,b,c,d,e;
  1700. a= block[stride*0 + xStride*0];
  1701. b= block[stride*0 + xStride*1];
  1702. c= block[stride*1 + xStride*0];
  1703. d= block[stride*1 + xStride*1];
  1704. e= a-b;
  1705. a= a+b;
  1706. b= c-d;
  1707. c= c+d;
  1708. block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
  1709. block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
  1710. block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
  1711. block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
  1712. }
  1713. #if 0
  1714. static void chroma_dc_dct_c(DCTELEM *block){
  1715. const int stride= 16*2;
  1716. const int xStride= 16;
  1717. int a,b,c,d,e;
  1718. a= block[stride*0 + xStride*0];
  1719. b= block[stride*0 + xStride*1];
  1720. c= block[stride*1 + xStride*0];
  1721. d= block[stride*1 + xStride*1];
  1722. e= a-b;
  1723. a= a+b;
  1724. b= c-d;
  1725. c= c+d;
  1726. block[stride*0 + xStride*0]= (a+c);
  1727. block[stride*0 + xStride*1]= (e+b);
  1728. block[stride*1 + xStride*0]= (a-c);
  1729. block[stride*1 + xStride*1]= (e-b);
  1730. }
  1731. #endif
  1732. /**
  1733. * gets the chroma qp.
  1734. */
  1735. static inline int get_chroma_qp(int chroma_qp_index_offset, int qscale){
  1736. return chroma_qp[clip(qscale + chroma_qp_index_offset, 0, 51)];
  1737. }
  1738. //FIXME need to check that this doesnt overflow signed 32 bit for low qp, i am not sure, it's very close
  1739. //FIXME check that gcc inlines this (and optimizes intra & seperate_dc stuff away)
  1740. static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int seperate_dc){
  1741. int i;
  1742. const int * const quant_table= quant_coeff[qscale];
  1743. const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
  1744. const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
  1745. const unsigned int threshold2= (threshold1<<1);
  1746. int last_non_zero;
  1747. if(seperate_dc){
  1748. if(qscale<=18){
  1749. //avoid overflows
  1750. const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
  1751. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
  1752. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1753. int level= block[0]*quant_coeff[qscale+18][0];
  1754. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1755. if(level>0){
  1756. level= (dc_bias + level)>>(QUANT_SHIFT-2);
  1757. block[0]= level;
  1758. }else{
  1759. level= (dc_bias - level)>>(QUANT_SHIFT-2);
  1760. block[0]= -level;
  1761. }
  1762. // last_non_zero = i;
  1763. }else{
  1764. block[0]=0;
  1765. }
  1766. }else{
  1767. const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
  1768. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
  1769. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1770. int level= block[0]*quant_table[0];
  1771. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1772. if(level>0){
  1773. level= (dc_bias + level)>>(QUANT_SHIFT+1);
  1774. block[0]= level;
  1775. }else{
  1776. level= (dc_bias - level)>>(QUANT_SHIFT+1);
  1777. block[0]= -level;
  1778. }
  1779. // last_non_zero = i;
  1780. }else{
  1781. block[0]=0;
  1782. }
  1783. }
  1784. last_non_zero= 0;
  1785. i=1;
  1786. }else{
  1787. last_non_zero= -1;
  1788. i=0;
  1789. }
  1790. for(; i<16; i++){
  1791. const int j= scantable[i];
  1792. int level= block[j]*quant_table[j];
  1793. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  1794. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  1795. if(((unsigned)(level+threshold1))>threshold2){
  1796. if(level>0){
  1797. level= (bias + level)>>QUANT_SHIFT;
  1798. block[j]= level;
  1799. }else{
  1800. level= (bias - level)>>QUANT_SHIFT;
  1801. block[j]= -level;
  1802. }
  1803. last_non_zero = i;
  1804. }else{
  1805. block[j]=0;
  1806. }
  1807. }
  1808. return last_non_zero;
  1809. }
  1810. static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){
  1811. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1812. ((uint32_t*)(src+0*stride))[0]= a;
  1813. ((uint32_t*)(src+1*stride))[0]= a;
  1814. ((uint32_t*)(src+2*stride))[0]= a;
  1815. ((uint32_t*)(src+3*stride))[0]= a;
  1816. }
  1817. static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){
  1818. ((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101;
  1819. ((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101;
  1820. ((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101;
  1821. ((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101;
  1822. }
  1823. static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1824. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
  1825. + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
  1826. ((uint32_t*)(src+0*stride))[0]=
  1827. ((uint32_t*)(src+1*stride))[0]=
  1828. ((uint32_t*)(src+2*stride))[0]=
  1829. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1830. }
  1831. static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1832. const int dc= ( src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
  1833. ((uint32_t*)(src+0*stride))[0]=
  1834. ((uint32_t*)(src+1*stride))[0]=
  1835. ((uint32_t*)(src+2*stride))[0]=
  1836. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1837. }
  1838. static void pred4x4_top_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1839. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2;
  1840. ((uint32_t*)(src+0*stride))[0]=
  1841. ((uint32_t*)(src+1*stride))[0]=
  1842. ((uint32_t*)(src+2*stride))[0]=
  1843. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1844. }
  1845. static void pred4x4_128_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1846. ((uint32_t*)(src+0*stride))[0]=
  1847. ((uint32_t*)(src+1*stride))[0]=
  1848. ((uint32_t*)(src+2*stride))[0]=
  1849. ((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U;
  1850. }
  1851. #define LOAD_TOP_RIGHT_EDGE\
  1852. const int t4= topright[0];\
  1853. const int t5= topright[1];\
  1854. const int t6= topright[2];\
  1855. const int t7= topright[3];\
  1856. #define LOAD_LEFT_EDGE\
  1857. const int l0= src[-1+0*stride];\
  1858. const int l1= src[-1+1*stride];\
  1859. const int l2= src[-1+2*stride];\
  1860. const int l3= src[-1+3*stride];\
  1861. #define LOAD_TOP_EDGE\
  1862. const int t0= src[ 0-1*stride];\
  1863. const int t1= src[ 1-1*stride];\
  1864. const int t2= src[ 2-1*stride];\
  1865. const int t3= src[ 3-1*stride];\
  1866. static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){
  1867. const int lt= src[-1-1*stride];
  1868. LOAD_TOP_EDGE
  1869. LOAD_LEFT_EDGE
  1870. src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
  1871. src[0+2*stride]=
  1872. src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
  1873. src[0+1*stride]=
  1874. src[1+2*stride]=
  1875. src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
  1876. src[0+0*stride]=
  1877. src[1+1*stride]=
  1878. src[2+2*stride]=
  1879. src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1880. src[1+0*stride]=
  1881. src[2+1*stride]=
  1882. src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1883. src[2+0*stride]=
  1884. src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1885. src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1886. }
  1887. static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){
  1888. LOAD_TOP_EDGE
  1889. LOAD_TOP_RIGHT_EDGE
  1890. // LOAD_LEFT_EDGE
  1891. src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
  1892. src[1+0*stride]=
  1893. src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
  1894. src[2+0*stride]=
  1895. src[1+1*stride]=
  1896. src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
  1897. src[3+0*stride]=
  1898. src[2+1*stride]=
  1899. src[1+2*stride]=
  1900. src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
  1901. src[3+1*stride]=
  1902. src[2+2*stride]=
  1903. src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
  1904. src[3+2*stride]=
  1905. src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
  1906. src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
  1907. }
  1908. static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
  1909. const int lt= src[-1-1*stride];
  1910. LOAD_TOP_EDGE
  1911. LOAD_LEFT_EDGE
  1912. const __attribute__((unused)) int unu= l3;
  1913. src[0+0*stride]=
  1914. src[1+2*stride]=(lt + t0 + 1)>>1;
  1915. src[1+0*stride]=
  1916. src[2+2*stride]=(t0 + t1 + 1)>>1;
  1917. src[2+0*stride]=
  1918. src[3+2*stride]=(t1 + t2 + 1)>>1;
  1919. src[3+0*stride]=(t2 + t3 + 1)>>1;
  1920. src[0+1*stride]=
  1921. src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1922. src[1+1*stride]=
  1923. src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1924. src[2+1*stride]=
  1925. src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1926. src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1927. src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1928. src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1929. }
  1930. static void pred4x4_vertical_left_c(uint8_t *src, uint8_t *topright, int stride){
  1931. LOAD_TOP_EDGE
  1932. LOAD_TOP_RIGHT_EDGE
  1933. const __attribute__((unused)) int unu= t7;
  1934. src[0+0*stride]=(t0 + t1 + 1)>>1;
  1935. src[1+0*stride]=
  1936. src[0+2*stride]=(t1 + t2 + 1)>>1;
  1937. src[2+0*stride]=
  1938. src[1+2*stride]=(t2 + t3 + 1)>>1;
  1939. src[3+0*stride]=
  1940. src[2+2*stride]=(t3 + t4+ 1)>>1;
  1941. src[3+2*stride]=(t4 + t5+ 1)>>1;
  1942. src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1943. src[1+1*stride]=
  1944. src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1945. src[2+1*stride]=
  1946. src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
  1947. src[3+1*stride]=
  1948. src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
  1949. src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
  1950. }
  1951. static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){
  1952. LOAD_LEFT_EDGE
  1953. src[0+0*stride]=(l0 + l1 + 1)>>1;
  1954. src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1955. src[2+0*stride]=
  1956. src[0+1*stride]=(l1 + l2 + 1)>>1;
  1957. src[3+0*stride]=
  1958. src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1959. src[2+1*stride]=
  1960. src[0+2*stride]=(l2 + l3 + 1)>>1;
  1961. src[3+1*stride]=
  1962. src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
  1963. src[3+2*stride]=
  1964. src[1+3*stride]=
  1965. src[0+3*stride]=
  1966. src[2+2*stride]=
  1967. src[2+3*stride]=
  1968. src[3+3*stride]=l3;
  1969. }
  1970. static void pred4x4_horizontal_down_c(uint8_t *src, uint8_t *topright, int stride){
  1971. const int lt= src[-1-1*stride];
  1972. LOAD_TOP_EDGE
  1973. LOAD_LEFT_EDGE
  1974. const __attribute__((unused)) int unu= t3;
  1975. src[0+0*stride]=
  1976. src[2+1*stride]=(lt + l0 + 1)>>1;
  1977. src[1+0*stride]=
  1978. src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1979. src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1980. src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1981. src[0+1*stride]=
  1982. src[2+2*stride]=(l0 + l1 + 1)>>1;
  1983. src[1+1*stride]=
  1984. src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1985. src[0+2*stride]=
  1986. src[2+3*stride]=(l1 + l2+ 1)>>1;
  1987. src[1+2*stride]=
  1988. src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1989. src[0+3*stride]=(l2 + l3 + 1)>>1;
  1990. src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1991. }
  1992. void ff_pred16x16_vertical_c(uint8_t *src, int stride){
  1993. int i;
  1994. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1995. const uint32_t b= ((uint32_t*)(src-stride))[1];
  1996. const uint32_t c= ((uint32_t*)(src-stride))[2];
  1997. const uint32_t d= ((uint32_t*)(src-stride))[3];
  1998. for(i=0; i<16; i++){
  1999. ((uint32_t*)(src+i*stride))[0]= a;
  2000. ((uint32_t*)(src+i*stride))[1]= b;
  2001. ((uint32_t*)(src+i*stride))[2]= c;
  2002. ((uint32_t*)(src+i*stride))[3]= d;
  2003. }
  2004. }
  2005. void ff_pred16x16_horizontal_c(uint8_t *src, int stride){
  2006. int i;
  2007. for(i=0; i<16; i++){
  2008. ((uint32_t*)(src+i*stride))[0]=
  2009. ((uint32_t*)(src+i*stride))[1]=
  2010. ((uint32_t*)(src+i*stride))[2]=
  2011. ((uint32_t*)(src+i*stride))[3]= src[-1+i*stride]*0x01010101;
  2012. }
  2013. }
  2014. void ff_pred16x16_dc_c(uint8_t *src, int stride){
  2015. int i, dc=0;
  2016. for(i=0;i<16; i++){
  2017. dc+= src[-1+i*stride];
  2018. }
  2019. for(i=0;i<16; i++){
  2020. dc+= src[i-stride];
  2021. }
  2022. dc= 0x01010101*((dc + 16)>>5);
  2023. for(i=0; i<16; i++){
  2024. ((uint32_t*)(src+i*stride))[0]=
  2025. ((uint32_t*)(src+i*stride))[1]=
  2026. ((uint32_t*)(src+i*stride))[2]=
  2027. ((uint32_t*)(src+i*stride))[3]= dc;
  2028. }
  2029. }
  2030. static void pred16x16_left_dc_c(uint8_t *src, int stride){
  2031. int i, dc=0;
  2032. for(i=0;i<16; i++){
  2033. dc+= src[-1+i*stride];
  2034. }
  2035. dc= 0x01010101*((dc + 8)>>4);
  2036. for(i=0; i<16; i++){
  2037. ((uint32_t*)(src+i*stride))[0]=
  2038. ((uint32_t*)(src+i*stride))[1]=
  2039. ((uint32_t*)(src+i*stride))[2]=
  2040. ((uint32_t*)(src+i*stride))[3]= dc;
  2041. }
  2042. }
  2043. static void pred16x16_top_dc_c(uint8_t *src, int stride){
  2044. int i, dc=0;
  2045. for(i=0;i<16; i++){
  2046. dc+= src[i-stride];
  2047. }
  2048. dc= 0x01010101*((dc + 8)>>4);
  2049. for(i=0; i<16; i++){
  2050. ((uint32_t*)(src+i*stride))[0]=
  2051. ((uint32_t*)(src+i*stride))[1]=
  2052. ((uint32_t*)(src+i*stride))[2]=
  2053. ((uint32_t*)(src+i*stride))[3]= dc;
  2054. }
  2055. }
  2056. void ff_pred16x16_128_dc_c(uint8_t *src, int stride){
  2057. int i;
  2058. for(i=0; i<16; i++){
  2059. ((uint32_t*)(src+i*stride))[0]=
  2060. ((uint32_t*)(src+i*stride))[1]=
  2061. ((uint32_t*)(src+i*stride))[2]=
  2062. ((uint32_t*)(src+i*stride))[3]= 0x01010101U*128U;
  2063. }
  2064. }
  2065. static inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){
  2066. int i, j, k;
  2067. int a;
  2068. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  2069. const uint8_t * const src0 = src+7-stride;
  2070. const uint8_t *src1 = src+8*stride-1;
  2071. const uint8_t *src2 = src1-2*stride; // == src+6*stride-1;
  2072. int H = src0[1] - src0[-1];
  2073. int V = src1[0] - src2[ 0];
  2074. for(k=2; k<=8; ++k) {
  2075. src1 += stride; src2 -= stride;
  2076. H += k*(src0[k] - src0[-k]);
  2077. V += k*(src1[0] - src2[ 0]);
  2078. }
  2079. if(svq3){
  2080. H = ( 5*(H/4) ) / 16;
  2081. V = ( 5*(V/4) ) / 16;
  2082. /* required for 100% accuracy */
  2083. i = H; H = V; V = i;
  2084. }else{
  2085. H = ( 5*H+32 ) >> 6;
  2086. V = ( 5*V+32 ) >> 6;
  2087. }
  2088. a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
  2089. for(j=16; j>0; --j) {
  2090. int b = a;
  2091. a += V;
  2092. for(i=-16; i<0; i+=4) {
  2093. src[16+i] = cm[ (b ) >> 5 ];
  2094. src[17+i] = cm[ (b+ H) >> 5 ];
  2095. src[18+i] = cm[ (b+2*H) >> 5 ];
  2096. src[19+i] = cm[ (b+3*H) >> 5 ];
  2097. b += 4*H;
  2098. }
  2099. src += stride;
  2100. }
  2101. }
  2102. void ff_pred16x16_plane_c(uint8_t *src, int stride){
  2103. pred16x16_plane_compat_c(src, stride, 0);
  2104. }
  2105. void ff_pred8x8_vertical_c(uint8_t *src, int stride){
  2106. int i;
  2107. const uint32_t a= ((uint32_t*)(src-stride))[0];
  2108. const uint32_t b= ((uint32_t*)(src-stride))[1];
  2109. for(i=0; i<8; i++){
  2110. ((uint32_t*)(src+i*stride))[0]= a;
  2111. ((uint32_t*)(src+i*stride))[1]= b;
  2112. }
  2113. }
  2114. void ff_pred8x8_horizontal_c(uint8_t *src, int stride){
  2115. int i;
  2116. for(i=0; i<8; i++){
  2117. ((uint32_t*)(src+i*stride))[0]=
  2118. ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101;
  2119. }
  2120. }
  2121. void ff_pred8x8_128_dc_c(uint8_t *src, int stride){
  2122. int i;
  2123. for(i=0; i<8; i++){
  2124. ((uint32_t*)(src+i*stride))[0]=
  2125. ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
  2126. }
  2127. }
  2128. static void pred8x8_left_dc_c(uint8_t *src, int stride){
  2129. int i;
  2130. int dc0, dc2;
  2131. dc0=dc2=0;
  2132. for(i=0;i<4; i++){
  2133. dc0+= src[-1+i*stride];
  2134. dc2+= src[-1+(i+4)*stride];
  2135. }
  2136. dc0= 0x01010101*((dc0 + 2)>>2);
  2137. dc2= 0x01010101*((dc2 + 2)>>2);
  2138. for(i=0; i<4; i++){
  2139. ((uint32_t*)(src+i*stride))[0]=
  2140. ((uint32_t*)(src+i*stride))[1]= dc0;
  2141. }
  2142. for(i=4; i<8; i++){
  2143. ((uint32_t*)(src+i*stride))[0]=
  2144. ((uint32_t*)(src+i*stride))[1]= dc2;
  2145. }
  2146. }
  2147. static void pred8x8_top_dc_c(uint8_t *src, int stride){
  2148. int i;
  2149. int dc0, dc1;
  2150. dc0=dc1=0;
  2151. for(i=0;i<4; i++){
  2152. dc0+= src[i-stride];
  2153. dc1+= src[4+i-stride];
  2154. }
  2155. dc0= 0x01010101*((dc0 + 2)>>2);
  2156. dc1= 0x01010101*((dc1 + 2)>>2);
  2157. for(i=0; i<4; i++){
  2158. ((uint32_t*)(src+i*stride))[0]= dc0;
  2159. ((uint32_t*)(src+i*stride))[1]= dc1;
  2160. }
  2161. for(i=4; i<8; i++){
  2162. ((uint32_t*)(src+i*stride))[0]= dc0;
  2163. ((uint32_t*)(src+i*stride))[1]= dc1;
  2164. }
  2165. }
  2166. void ff_pred8x8_dc_c(uint8_t *src, int stride){
  2167. int i;
  2168. int dc0, dc1, dc2, dc3;
  2169. dc0=dc1=dc2=0;
  2170. for(i=0;i<4; i++){
  2171. dc0+= src[-1+i*stride] + src[i-stride];
  2172. dc1+= src[4+i-stride];
  2173. dc2+= src[-1+(i+4)*stride];
  2174. }
  2175. dc3= 0x01010101*((dc1 + dc2 + 4)>>3);
  2176. dc0= 0x01010101*((dc0 + 4)>>3);
  2177. dc1= 0x01010101*((dc1 + 2)>>2);
  2178. dc2= 0x01010101*((dc2 + 2)>>2);
  2179. for(i=0; i<4; i++){
  2180. ((uint32_t*)(src+i*stride))[0]= dc0;
  2181. ((uint32_t*)(src+i*stride))[1]= dc1;
  2182. }
  2183. for(i=4; i<8; i++){
  2184. ((uint32_t*)(src+i*stride))[0]= dc2;
  2185. ((uint32_t*)(src+i*stride))[1]= dc3;
  2186. }
  2187. }
  2188. void ff_pred8x8_plane_c(uint8_t *src, int stride){
  2189. int j, k;
  2190. int a;
  2191. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  2192. const uint8_t * const src0 = src+3-stride;
  2193. const uint8_t *src1 = src+4*stride-1;
  2194. const uint8_t *src2 = src1-2*stride; // == src+2*stride-1;
  2195. int H = src0[1] - src0[-1];
  2196. int V = src1[0] - src2[ 0];
  2197. for(k=2; k<=4; ++k) {
  2198. src1 += stride; src2 -= stride;
  2199. H += k*(src0[k] - src0[-k]);
  2200. V += k*(src1[0] - src2[ 0]);
  2201. }
  2202. H = ( 17*H+16 ) >> 5;
  2203. V = ( 17*V+16 ) >> 5;
  2204. a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
  2205. for(j=8; j>0; --j) {
  2206. int b = a;
  2207. a += V;
  2208. src[0] = cm[ (b ) >> 5 ];
  2209. src[1] = cm[ (b+ H) >> 5 ];
  2210. src[2] = cm[ (b+2*H) >> 5 ];
  2211. src[3] = cm[ (b+3*H) >> 5 ];
  2212. src[4] = cm[ (b+4*H) >> 5 ];
  2213. src[5] = cm[ (b+5*H) >> 5 ];
  2214. src[6] = cm[ (b+6*H) >> 5 ];
  2215. src[7] = cm[ (b+7*H) >> 5 ];
  2216. src += stride;
  2217. }
  2218. }
  2219. #define SRC(x,y) src[(x)+(y)*stride]
  2220. #define PL(y) \
  2221. const int l##y = (SRC(-1,y-1) + 2*SRC(-1,y) + SRC(-1,y+1) + 2) >> 2;
  2222. #define PREDICT_8x8_LOAD_LEFT \
  2223. const int l0 = ((has_topleft ? SRC(-1,-1) : SRC(-1,0)) \
  2224. + 2*SRC(-1,0) + SRC(-1,1) + 2) >> 2; \
  2225. PL(1) PL(2) PL(3) PL(4) PL(5) PL(6) \
  2226. const int l7 attribute_unused = (SRC(-1,6) + 3*SRC(-1,7) + 2) >> 2
  2227. #define PT(x) \
  2228. const int t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  2229. #define PREDICT_8x8_LOAD_TOP \
  2230. const int t0 = ((has_topleft ? SRC(-1,-1) : SRC(0,-1)) \
  2231. + 2*SRC(0,-1) + SRC(1,-1) + 2) >> 2; \
  2232. PT(1) PT(2) PT(3) PT(4) PT(5) PT(6) \
  2233. const int t7 attribute_unused = ((has_topright ? SRC(8,-1) : SRC(7,-1)) \
  2234. + 2*SRC(7,-1) + SRC(6,-1) + 2) >> 2
  2235. #define PTR(x) \
  2236. t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  2237. #define PREDICT_8x8_LOAD_TOPRIGHT \
  2238. int t8, t9, t10, t11, t12, t13, t14, t15; \
  2239. if(has_topright) { \
  2240. PTR(8) PTR(9) PTR(10) PTR(11) PTR(12) PTR(13) PTR(14) \
  2241. t15 = (SRC(14,-1) + 3*SRC(15,-1) + 2) >> 2; \
  2242. } else t8=t9=t10=t11=t12=t13=t14=t15= SRC(7,-1);
  2243. #define PREDICT_8x8_LOAD_TOPLEFT \
  2244. const int lt = (SRC(-1,0) + 2*SRC(-1,-1) + SRC(0,-1) + 2) >> 2
  2245. #define PREDICT_8x8_DC(v) \
  2246. int y; \
  2247. for( y = 0; y < 8; y++ ) { \
  2248. ((uint32_t*)src)[0] = \
  2249. ((uint32_t*)src)[1] = v; \
  2250. src += stride; \
  2251. }
  2252. static void pred8x8l_128_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2253. {
  2254. PREDICT_8x8_DC(0x80808080);
  2255. }
  2256. static void pred8x8l_left_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2257. {
  2258. PREDICT_8x8_LOAD_LEFT;
  2259. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7+4) >> 3) * 0x01010101;
  2260. PREDICT_8x8_DC(dc);
  2261. }
  2262. static void pred8x8l_top_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2263. {
  2264. PREDICT_8x8_LOAD_TOP;
  2265. const uint32_t dc = ((t0+t1+t2+t3+t4+t5+t6+t7+4) >> 3) * 0x01010101;
  2266. PREDICT_8x8_DC(dc);
  2267. }
  2268. static void pred8x8l_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2269. {
  2270. PREDICT_8x8_LOAD_LEFT;
  2271. PREDICT_8x8_LOAD_TOP;
  2272. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7
  2273. +t0+t1+t2+t3+t4+t5+t6+t7+8) >> 4) * 0x01010101;
  2274. PREDICT_8x8_DC(dc);
  2275. }
  2276. static void pred8x8l_horizontal_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2277. {
  2278. PREDICT_8x8_LOAD_LEFT;
  2279. #define ROW(y) ((uint32_t*)(src+y*stride))[0] =\
  2280. ((uint32_t*)(src+y*stride))[1] = 0x01010101 * l##y
  2281. ROW(0); ROW(1); ROW(2); ROW(3); ROW(4); ROW(5); ROW(6); ROW(7);
  2282. #undef ROW
  2283. }
  2284. static void pred8x8l_vertical_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2285. {
  2286. int y;
  2287. PREDICT_8x8_LOAD_TOP;
  2288. src[0] = t0;
  2289. src[1] = t1;
  2290. src[2] = t2;
  2291. src[3] = t3;
  2292. src[4] = t4;
  2293. src[5] = t5;
  2294. src[6] = t6;
  2295. src[7] = t7;
  2296. for( y = 1; y < 8; y++ )
  2297. *(uint64_t*)(src+y*stride) = *(uint64_t*)src;
  2298. }
  2299. static void pred8x8l_down_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2300. {
  2301. PREDICT_8x8_LOAD_TOP;
  2302. PREDICT_8x8_LOAD_TOPRIGHT;
  2303. SRC(0,0)= (t0 + 2*t1 + t2 + 2) >> 2;
  2304. SRC(0,1)=SRC(1,0)= (t1 + 2*t2 + t3 + 2) >> 2;
  2305. SRC(0,2)=SRC(1,1)=SRC(2,0)= (t2 + 2*t3 + t4 + 2) >> 2;
  2306. SRC(0,3)=SRC(1,2)=SRC(2,1)=SRC(3,0)= (t3 + 2*t4 + t5 + 2) >> 2;
  2307. SRC(0,4)=SRC(1,3)=SRC(2,2)=SRC(3,1)=SRC(4,0)= (t4 + 2*t5 + t6 + 2) >> 2;
  2308. SRC(0,5)=SRC(1,4)=SRC(2,3)=SRC(3,2)=SRC(4,1)=SRC(5,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2309. 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;
  2310. 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;
  2311. 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;
  2312. SRC(2,7)=SRC(3,6)=SRC(4,5)=SRC(5,4)=SRC(6,3)=SRC(7,2)= (t9 + 2*t10 + t11 + 2) >> 2;
  2313. SRC(3,7)=SRC(4,6)=SRC(5,5)=SRC(6,4)=SRC(7,3)= (t10 + 2*t11 + t12 + 2) >> 2;
  2314. SRC(4,7)=SRC(5,6)=SRC(6,5)=SRC(7,4)= (t11 + 2*t12 + t13 + 2) >> 2;
  2315. SRC(5,7)=SRC(6,6)=SRC(7,5)= (t12 + 2*t13 + t14 + 2) >> 2;
  2316. SRC(6,7)=SRC(7,6)= (t13 + 2*t14 + t15 + 2) >> 2;
  2317. SRC(7,7)= (t14 + 3*t15 + 2) >> 2;
  2318. }
  2319. static void pred8x8l_down_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2320. {
  2321. PREDICT_8x8_LOAD_TOP;
  2322. PREDICT_8x8_LOAD_LEFT;
  2323. PREDICT_8x8_LOAD_TOPLEFT;
  2324. SRC(0,7)= (l7 + 2*l6 + l5 + 2) >> 2;
  2325. SRC(0,6)=SRC(1,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2326. SRC(0,5)=SRC(1,6)=SRC(2,7)= (l5 + 2*l4 + l3 + 2) >> 2;
  2327. SRC(0,4)=SRC(1,5)=SRC(2,6)=SRC(3,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2328. SRC(0,3)=SRC(1,4)=SRC(2,5)=SRC(3,6)=SRC(4,7)= (l3 + 2*l2 + l1 + 2) >> 2;
  2329. SRC(0,2)=SRC(1,3)=SRC(2,4)=SRC(3,5)=SRC(4,6)=SRC(5,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2330. 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;
  2331. 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;
  2332. 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;
  2333. SRC(2,0)=SRC(3,1)=SRC(4,2)=SRC(5,3)=SRC(6,4)=SRC(7,5)= (t0 + 2*t1 + t2 + 2) >> 2;
  2334. SRC(3,0)=SRC(4,1)=SRC(5,2)=SRC(6,3)=SRC(7,4)= (t1 + 2*t2 + t3 + 2) >> 2;
  2335. SRC(4,0)=SRC(5,1)=SRC(6,2)=SRC(7,3)= (t2 + 2*t3 + t4 + 2) >> 2;
  2336. SRC(5,0)=SRC(6,1)=SRC(7,2)= (t3 + 2*t4 + t5 + 2) >> 2;
  2337. SRC(6,0)=SRC(7,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2338. SRC(7,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2339. }
  2340. static void pred8x8l_vertical_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2341. {
  2342. PREDICT_8x8_LOAD_TOP;
  2343. PREDICT_8x8_LOAD_LEFT;
  2344. PREDICT_8x8_LOAD_TOPLEFT;
  2345. SRC(0,6)= (l5 + 2*l4 + l3 + 2) >> 2;
  2346. SRC(0,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2347. SRC(0,4)=SRC(1,6)= (l3 + 2*l2 + l1 + 2) >> 2;
  2348. SRC(0,5)=SRC(1,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2349. SRC(0,2)=SRC(1,4)=SRC(2,6)= (l1 + 2*l0 + lt + 2) >> 2;
  2350. SRC(0,3)=SRC(1,5)=SRC(2,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2351. SRC(0,1)=SRC(1,3)=SRC(2,5)=SRC(3,7)= (l0 + 2*lt + t0 + 2) >> 2;
  2352. SRC(0,0)=SRC(1,2)=SRC(2,4)=SRC(3,6)= (lt + t0 + 1) >> 1;
  2353. SRC(1,1)=SRC(2,3)=SRC(3,5)=SRC(4,7)= (lt + 2*t0 + t1 + 2) >> 2;
  2354. SRC(1,0)=SRC(2,2)=SRC(3,4)=SRC(4,6)= (t0 + t1 + 1) >> 1;
  2355. SRC(2,1)=SRC(3,3)=SRC(4,5)=SRC(5,7)= (t0 + 2*t1 + t2 + 2) >> 2;
  2356. SRC(2,0)=SRC(3,2)=SRC(4,4)=SRC(5,6)= (t1 + t2 + 1) >> 1;
  2357. SRC(3,1)=SRC(4,3)=SRC(5,5)=SRC(6,7)= (t1 + 2*t2 + t3 + 2) >> 2;
  2358. SRC(3,0)=SRC(4,2)=SRC(5,4)=SRC(6,6)= (t2 + t3 + 1) >> 1;
  2359. SRC(4,1)=SRC(5,3)=SRC(6,5)=SRC(7,7)= (t2 + 2*t3 + t4 + 2) >> 2;
  2360. SRC(4,0)=SRC(5,2)=SRC(6,4)=SRC(7,6)= (t3 + t4 + 1) >> 1;
  2361. SRC(5,1)=SRC(6,3)=SRC(7,5)= (t3 + 2*t4 + t5 + 2) >> 2;
  2362. SRC(5,0)=SRC(6,2)=SRC(7,4)= (t4 + t5 + 1) >> 1;
  2363. SRC(6,1)=SRC(7,3)= (t4 + 2*t5 + t6 + 2) >> 2;
  2364. SRC(6,0)=SRC(7,2)= (t5 + t6 + 1) >> 1;
  2365. SRC(7,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2366. SRC(7,0)= (t6 + t7 + 1) >> 1;
  2367. }
  2368. static void pred8x8l_horizontal_down_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2369. {
  2370. PREDICT_8x8_LOAD_TOP;
  2371. PREDICT_8x8_LOAD_LEFT;
  2372. PREDICT_8x8_LOAD_TOPLEFT;
  2373. SRC(0,7)= (l6 + l7 + 1) >> 1;
  2374. SRC(1,7)= (l5 + 2*l6 + l7 + 2) >> 2;
  2375. SRC(0,6)=SRC(2,7)= (l5 + l6 + 1) >> 1;
  2376. SRC(1,6)=SRC(3,7)= (l4 + 2*l5 + l6 + 2) >> 2;
  2377. SRC(0,5)=SRC(2,6)=SRC(4,7)= (l4 + l5 + 1) >> 1;
  2378. SRC(1,5)=SRC(3,6)=SRC(5,7)= (l3 + 2*l4 + l5 + 2) >> 2;
  2379. SRC(0,4)=SRC(2,5)=SRC(4,6)=SRC(6,7)= (l3 + l4 + 1) >> 1;
  2380. SRC(1,4)=SRC(3,5)=SRC(5,6)=SRC(7,7)= (l2 + 2*l3 + l4 + 2) >> 2;
  2381. SRC(0,3)=SRC(2,4)=SRC(4,5)=SRC(6,6)= (l2 + l3 + 1) >> 1;
  2382. SRC(1,3)=SRC(3,4)=SRC(5,5)=SRC(7,6)= (l1 + 2*l2 + l3 + 2) >> 2;
  2383. SRC(0,2)=SRC(2,3)=SRC(4,4)=SRC(6,5)= (l1 + l2 + 1) >> 1;
  2384. SRC(1,2)=SRC(3,3)=SRC(5,4)=SRC(7,5)= (l0 + 2*l1 + l2 + 2) >> 2;
  2385. SRC(0,1)=SRC(2,2)=SRC(4,3)=SRC(6,4)= (l0 + l1 + 1) >> 1;
  2386. SRC(1,1)=SRC(3,2)=SRC(5,3)=SRC(7,4)= (lt + 2*l0 + l1 + 2) >> 2;
  2387. SRC(0,0)=SRC(2,1)=SRC(4,2)=SRC(6,3)= (lt + l0 + 1) >> 1;
  2388. SRC(1,0)=SRC(3,1)=SRC(5,2)=SRC(7,3)= (l0 + 2*lt + t0 + 2) >> 2;
  2389. SRC(2,0)=SRC(4,1)=SRC(6,2)= (t1 + 2*t0 + lt + 2) >> 2;
  2390. SRC(3,0)=SRC(5,1)=SRC(7,2)= (t2 + 2*t1 + t0 + 2) >> 2;
  2391. SRC(4,0)=SRC(6,1)= (t3 + 2*t2 + t1 + 2) >> 2;
  2392. SRC(5,0)=SRC(7,1)= (t4 + 2*t3 + t2 + 2) >> 2;
  2393. SRC(6,0)= (t5 + 2*t4 + t3 + 2) >> 2;
  2394. SRC(7,0)= (t6 + 2*t5 + t4 + 2) >> 2;
  2395. }
  2396. static void pred8x8l_vertical_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2397. {
  2398. PREDICT_8x8_LOAD_TOP;
  2399. PREDICT_8x8_LOAD_TOPRIGHT;
  2400. SRC(0,0)= (t0 + t1 + 1) >> 1;
  2401. SRC(0,1)= (t0 + 2*t1 + t2 + 2) >> 2;
  2402. SRC(0,2)=SRC(1,0)= (t1 + t2 + 1) >> 1;
  2403. SRC(0,3)=SRC(1,1)= (t1 + 2*t2 + t3 + 2) >> 2;
  2404. SRC(0,4)=SRC(1,2)=SRC(2,0)= (t2 + t3 + 1) >> 1;
  2405. SRC(0,5)=SRC(1,3)=SRC(2,1)= (t2 + 2*t3 + t4 + 2) >> 2;
  2406. SRC(0,6)=SRC(1,4)=SRC(2,2)=SRC(3,0)= (t3 + t4 + 1) >> 1;
  2407. SRC(0,7)=SRC(1,5)=SRC(2,3)=SRC(3,1)= (t3 + 2*t4 + t5 + 2) >> 2;
  2408. SRC(1,6)=SRC(2,4)=SRC(3,2)=SRC(4,0)= (t4 + t5 + 1) >> 1;
  2409. SRC(1,7)=SRC(2,5)=SRC(3,3)=SRC(4,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2410. SRC(2,6)=SRC(3,4)=SRC(4,2)=SRC(5,0)= (t5 + t6 + 1) >> 1;
  2411. SRC(2,7)=SRC(3,5)=SRC(4,3)=SRC(5,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2412. SRC(3,6)=SRC(4,4)=SRC(5,2)=SRC(6,0)= (t6 + t7 + 1) >> 1;
  2413. SRC(3,7)=SRC(4,5)=SRC(5,3)=SRC(6,1)= (t6 + 2*t7 + t8 + 2) >> 2;
  2414. SRC(4,6)=SRC(5,4)=SRC(6,2)=SRC(7,0)= (t7 + t8 + 1) >> 1;
  2415. SRC(4,7)=SRC(5,5)=SRC(6,3)=SRC(7,1)= (t7 + 2*t8 + t9 + 2) >> 2;
  2416. SRC(5,6)=SRC(6,4)=SRC(7,2)= (t8 + t9 + 1) >> 1;
  2417. SRC(5,7)=SRC(6,5)=SRC(7,3)= (t8 + 2*t9 + t10 + 2) >> 2;
  2418. SRC(6,6)=SRC(7,4)= (t9 + t10 + 1) >> 1;
  2419. SRC(6,7)=SRC(7,5)= (t9 + 2*t10 + t11 + 2) >> 2;
  2420. SRC(7,6)= (t10 + t11 + 1) >> 1;
  2421. SRC(7,7)= (t10 + 2*t11 + t12 + 2) >> 2;
  2422. }
  2423. static void pred8x8l_horizontal_up_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2424. {
  2425. PREDICT_8x8_LOAD_LEFT;
  2426. SRC(0,0)= (l0 + l1 + 1) >> 1;
  2427. SRC(1,0)= (l0 + 2*l1 + l2 + 2) >> 2;
  2428. SRC(0,1)=SRC(2,0)= (l1 + l2 + 1) >> 1;
  2429. SRC(1,1)=SRC(3,0)= (l1 + 2*l2 + l3 + 2) >> 2;
  2430. SRC(0,2)=SRC(2,1)=SRC(4,0)= (l2 + l3 + 1) >> 1;
  2431. SRC(1,2)=SRC(3,1)=SRC(5,0)= (l2 + 2*l3 + l4 + 2) >> 2;
  2432. SRC(0,3)=SRC(2,2)=SRC(4,1)=SRC(6,0)= (l3 + l4 + 1) >> 1;
  2433. SRC(1,3)=SRC(3,2)=SRC(5,1)=SRC(7,0)= (l3 + 2*l4 + l5 + 2) >> 2;
  2434. SRC(0,4)=SRC(2,3)=SRC(4,2)=SRC(6,1)= (l4 + l5 + 1) >> 1;
  2435. SRC(1,4)=SRC(3,3)=SRC(5,2)=SRC(7,1)= (l4 + 2*l5 + l6 + 2) >> 2;
  2436. SRC(0,5)=SRC(2,4)=SRC(4,3)=SRC(6,2)= (l5 + l6 + 1) >> 1;
  2437. SRC(1,5)=SRC(3,4)=SRC(5,3)=SRC(7,2)= (l5 + 2*l6 + l7 + 2) >> 2;
  2438. SRC(0,6)=SRC(2,5)=SRC(4,4)=SRC(6,3)= (l6 + l7 + 1) >> 1;
  2439. SRC(1,6)=SRC(3,5)=SRC(5,4)=SRC(7,3)= (l6 + 3*l7 + 2) >> 2;
  2440. SRC(0,7)=SRC(1,7)=SRC(2,6)=SRC(2,7)=SRC(3,6)=
  2441. SRC(3,7)=SRC(4,5)=SRC(4,6)=SRC(4,7)=SRC(5,5)=
  2442. SRC(5,6)=SRC(5,7)=SRC(6,4)=SRC(6,5)=SRC(6,6)=
  2443. SRC(6,7)=SRC(7,4)=SRC(7,5)=SRC(7,6)=SRC(7,7)= l7;
  2444. }
  2445. #undef PREDICT_8x8_LOAD_LEFT
  2446. #undef PREDICT_8x8_LOAD_TOP
  2447. #undef PREDICT_8x8_LOAD_TOPLEFT
  2448. #undef PREDICT_8x8_LOAD_TOPRIGHT
  2449. #undef PREDICT_8x8_DC
  2450. #undef PTR
  2451. #undef PT
  2452. #undef PL
  2453. #undef SRC
  2454. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  2455. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2456. int src_x_offset, int src_y_offset,
  2457. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  2458. MpegEncContext * const s = &h->s;
  2459. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  2460. int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  2461. const int luma_xy= (mx&3) + ((my&3)<<2);
  2462. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
  2463. uint8_t * src_cb, * src_cr;
  2464. int extra_width= h->emu_edge_width;
  2465. int extra_height= h->emu_edge_height;
  2466. int emu=0;
  2467. const int full_mx= mx>>2;
  2468. const int full_my= my>>2;
  2469. const int pic_width = 16*s->mb_width;
  2470. const int pic_height = 16*s->mb_height >> MB_MBAFF;
  2471. if(!pic->data[0])
  2472. return;
  2473. if(mx&7) extra_width -= 3;
  2474. if(my&7) extra_height -= 3;
  2475. if( full_mx < 0-extra_width
  2476. || full_my < 0-extra_height
  2477. || full_mx + 16/*FIXME*/ > pic_width + extra_width
  2478. || full_my + 16/*FIXME*/ > pic_height + extra_height){
  2479. 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);
  2480. src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
  2481. emu=1;
  2482. }
  2483. qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
  2484. if(!square){
  2485. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
  2486. }
  2487. if(s->flags&CODEC_FLAG_GRAY) return;
  2488. if(MB_MBAFF){
  2489. // chroma offset when predicting from a field of opposite parity
  2490. my += 2 * ((s->mb_y & 1) - (h->ref_cache[list][scan8[n]] & 1));
  2491. emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
  2492. }
  2493. src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2494. src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2495. if(emu){
  2496. 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);
  2497. src_cb= s->edge_emu_buffer;
  2498. }
  2499. chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2500. if(emu){
  2501. 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);
  2502. src_cr= s->edge_emu_buffer;
  2503. }
  2504. chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2505. }
  2506. static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
  2507. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2508. int x_offset, int y_offset,
  2509. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2510. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2511. int list0, int list1){
  2512. MpegEncContext * const s = &h->s;
  2513. qpel_mc_func *qpix_op= qpix_put;
  2514. h264_chroma_mc_func chroma_op= chroma_put;
  2515. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2516. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2517. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2518. x_offset += 8*s->mb_x;
  2519. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2520. if(list0){
  2521. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  2522. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  2523. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2524. qpix_op, chroma_op);
  2525. qpix_op= qpix_avg;
  2526. chroma_op= chroma_avg;
  2527. }
  2528. if(list1){
  2529. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  2530. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  2531. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2532. qpix_op, chroma_op);
  2533. }
  2534. }
  2535. static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
  2536. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2537. int x_offset, int y_offset,
  2538. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2539. h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
  2540. h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
  2541. int list0, int list1){
  2542. MpegEncContext * const s = &h->s;
  2543. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2544. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2545. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2546. x_offset += 8*s->mb_x;
  2547. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2548. if(list0 && list1){
  2549. /* don't optimize for luma-only case, since B-frames usually
  2550. * use implicit weights => chroma too. */
  2551. uint8_t *tmp_cb = s->obmc_scratchpad;
  2552. uint8_t *tmp_cr = s->obmc_scratchpad + 8;
  2553. uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
  2554. int refn0 = h->ref_cache[0][ scan8[n] ];
  2555. int refn1 = h->ref_cache[1][ scan8[n] ];
  2556. mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
  2557. dest_y, dest_cb, dest_cr,
  2558. x_offset, y_offset, qpix_put, chroma_put);
  2559. mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
  2560. tmp_y, tmp_cb, tmp_cr,
  2561. x_offset, y_offset, qpix_put, chroma_put);
  2562. if(h->use_weight == 2){
  2563. int weight0 = h->implicit_weight[refn0][refn1];
  2564. int weight1 = 64 - weight0;
  2565. luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
  2566. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2567. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2568. }else{
  2569. luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
  2570. h->luma_weight[0][refn0], h->luma_weight[1][refn1],
  2571. h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
  2572. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2573. h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
  2574. h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
  2575. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2576. h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
  2577. h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
  2578. }
  2579. }else{
  2580. int list = list1 ? 1 : 0;
  2581. int refn = h->ref_cache[list][ scan8[n] ];
  2582. Picture *ref= &h->ref_list[list][refn];
  2583. mc_dir_part(h, ref, n, square, chroma_height, delta, list,
  2584. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2585. qpix_put, chroma_put);
  2586. luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
  2587. h->luma_weight[list][refn], h->luma_offset[list][refn]);
  2588. if(h->use_weight_chroma){
  2589. chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2590. h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
  2591. chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2592. h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
  2593. }
  2594. }
  2595. }
  2596. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  2597. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2598. int x_offset, int y_offset,
  2599. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2600. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2601. h264_weight_func *weight_op, h264_biweight_func *weight_avg,
  2602. int list0, int list1){
  2603. if((h->use_weight==2 && list0 && list1
  2604. && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
  2605. || h->use_weight==1)
  2606. mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2607. x_offset, y_offset, qpix_put, chroma_put,
  2608. weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
  2609. else
  2610. mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2611. x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
  2612. }
  2613. static inline void prefetch_motion(H264Context *h, int list){
  2614. /* fetch pixels for estimated mv 4 macroblocks ahead
  2615. * optimized for 64byte cache lines */
  2616. MpegEncContext * const s = &h->s;
  2617. const int refn = h->ref_cache[list][scan8[0]];
  2618. if(refn >= 0){
  2619. const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
  2620. const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
  2621. uint8_t **src= h->ref_list[list][refn].data;
  2622. int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
  2623. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  2624. off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
  2625. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  2626. }
  2627. }
  2628. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2629. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  2630. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
  2631. h264_weight_func *weight_op, h264_biweight_func *weight_avg){
  2632. MpegEncContext * const s = &h->s;
  2633. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2634. const int mb_type= s->current_picture.mb_type[mb_xy];
  2635. assert(IS_INTER(mb_type));
  2636. prefetch_motion(h, 0);
  2637. if(IS_16X16(mb_type)){
  2638. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  2639. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  2640. &weight_op[0], &weight_avg[0],
  2641. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2642. }else if(IS_16X8(mb_type)){
  2643. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  2644. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2645. &weight_op[1], &weight_avg[1],
  2646. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2647. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  2648. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2649. &weight_op[1], &weight_avg[1],
  2650. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2651. }else if(IS_8X16(mb_type)){
  2652. mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
  2653. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2654. &weight_op[2], &weight_avg[2],
  2655. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2656. mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
  2657. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2658. &weight_op[2], &weight_avg[2],
  2659. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2660. }else{
  2661. int i;
  2662. assert(IS_8X8(mb_type));
  2663. for(i=0; i<4; i++){
  2664. const int sub_mb_type= h->sub_mb_type[i];
  2665. const int n= 4*i;
  2666. int x_offset= (i&1)<<2;
  2667. int y_offset= (i&2)<<1;
  2668. if(IS_SUB_8X8(sub_mb_type)){
  2669. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2670. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2671. &weight_op[3], &weight_avg[3],
  2672. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2673. }else if(IS_SUB_8X4(sub_mb_type)){
  2674. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2675. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2676. &weight_op[4], &weight_avg[4],
  2677. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2678. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  2679. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2680. &weight_op[4], &weight_avg[4],
  2681. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2682. }else if(IS_SUB_4X8(sub_mb_type)){
  2683. mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2684. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2685. &weight_op[5], &weight_avg[5],
  2686. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2687. mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
  2688. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2689. &weight_op[5], &weight_avg[5],
  2690. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2691. }else{
  2692. int j;
  2693. assert(IS_SUB_4X4(sub_mb_type));
  2694. for(j=0; j<4; j++){
  2695. int sub_x_offset= x_offset + 2*(j&1);
  2696. int sub_y_offset= y_offset + (j&2);
  2697. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  2698. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2699. &weight_op[6], &weight_avg[6],
  2700. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2701. }
  2702. }
  2703. }
  2704. }
  2705. prefetch_motion(h, 1);
  2706. }
  2707. static void decode_init_vlc(){
  2708. static int done = 0;
  2709. if (!done) {
  2710. int i;
  2711. done = 1;
  2712. init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
  2713. &chroma_dc_coeff_token_len [0], 1, 1,
  2714. &chroma_dc_coeff_token_bits[0], 1, 1, 1);
  2715. for(i=0; i<4; i++){
  2716. init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
  2717. &coeff_token_len [i][0], 1, 1,
  2718. &coeff_token_bits[i][0], 1, 1, 1);
  2719. }
  2720. for(i=0; i<3; i++){
  2721. init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
  2722. &chroma_dc_total_zeros_len [i][0], 1, 1,
  2723. &chroma_dc_total_zeros_bits[i][0], 1, 1, 1);
  2724. }
  2725. for(i=0; i<15; i++){
  2726. init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
  2727. &total_zeros_len [i][0], 1, 1,
  2728. &total_zeros_bits[i][0], 1, 1, 1);
  2729. }
  2730. for(i=0; i<6; i++){
  2731. init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
  2732. &run_len [i][0], 1, 1,
  2733. &run_bits[i][0], 1, 1, 1);
  2734. }
  2735. init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
  2736. &run_len [6][0], 1, 1,
  2737. &run_bits[6][0], 1, 1, 1);
  2738. }
  2739. }
  2740. /**
  2741. * Sets the intra prediction function pointers.
  2742. */
  2743. static void init_pred_ptrs(H264Context *h){
  2744. // MpegEncContext * const s = &h->s;
  2745. h->pred4x4[VERT_PRED ]= pred4x4_vertical_c;
  2746. h->pred4x4[HOR_PRED ]= pred4x4_horizontal_c;
  2747. h->pred4x4[DC_PRED ]= pred4x4_dc_c;
  2748. h->pred4x4[DIAG_DOWN_LEFT_PRED ]= pred4x4_down_left_c;
  2749. h->pred4x4[DIAG_DOWN_RIGHT_PRED]= pred4x4_down_right_c;
  2750. h->pred4x4[VERT_RIGHT_PRED ]= pred4x4_vertical_right_c;
  2751. h->pred4x4[HOR_DOWN_PRED ]= pred4x4_horizontal_down_c;
  2752. h->pred4x4[VERT_LEFT_PRED ]= pred4x4_vertical_left_c;
  2753. h->pred4x4[HOR_UP_PRED ]= pred4x4_horizontal_up_c;
  2754. h->pred4x4[LEFT_DC_PRED ]= pred4x4_left_dc_c;
  2755. h->pred4x4[TOP_DC_PRED ]= pred4x4_top_dc_c;
  2756. h->pred4x4[DC_128_PRED ]= pred4x4_128_dc_c;
  2757. h->pred8x8l[VERT_PRED ]= pred8x8l_vertical_c;
  2758. h->pred8x8l[HOR_PRED ]= pred8x8l_horizontal_c;
  2759. h->pred8x8l[DC_PRED ]= pred8x8l_dc_c;
  2760. h->pred8x8l[DIAG_DOWN_LEFT_PRED ]= pred8x8l_down_left_c;
  2761. h->pred8x8l[DIAG_DOWN_RIGHT_PRED]= pred8x8l_down_right_c;
  2762. h->pred8x8l[VERT_RIGHT_PRED ]= pred8x8l_vertical_right_c;
  2763. h->pred8x8l[HOR_DOWN_PRED ]= pred8x8l_horizontal_down_c;
  2764. h->pred8x8l[VERT_LEFT_PRED ]= pred8x8l_vertical_left_c;
  2765. h->pred8x8l[HOR_UP_PRED ]= pred8x8l_horizontal_up_c;
  2766. h->pred8x8l[LEFT_DC_PRED ]= pred8x8l_left_dc_c;
  2767. h->pred8x8l[TOP_DC_PRED ]= pred8x8l_top_dc_c;
  2768. h->pred8x8l[DC_128_PRED ]= pred8x8l_128_dc_c;
  2769. h->pred8x8[DC_PRED8x8 ]= ff_pred8x8_dc_c;
  2770. h->pred8x8[VERT_PRED8x8 ]= ff_pred8x8_vertical_c;
  2771. h->pred8x8[HOR_PRED8x8 ]= ff_pred8x8_horizontal_c;
  2772. h->pred8x8[PLANE_PRED8x8 ]= ff_pred8x8_plane_c;
  2773. h->pred8x8[LEFT_DC_PRED8x8]= pred8x8_left_dc_c;
  2774. h->pred8x8[TOP_DC_PRED8x8 ]= pred8x8_top_dc_c;
  2775. h->pred8x8[DC_128_PRED8x8 ]= ff_pred8x8_128_dc_c;
  2776. h->pred16x16[DC_PRED8x8 ]= ff_pred16x16_dc_c;
  2777. h->pred16x16[VERT_PRED8x8 ]= ff_pred16x16_vertical_c;
  2778. h->pred16x16[HOR_PRED8x8 ]= ff_pred16x16_horizontal_c;
  2779. h->pred16x16[PLANE_PRED8x8 ]= ff_pred16x16_plane_c;
  2780. h->pred16x16[LEFT_DC_PRED8x8]= pred16x16_left_dc_c;
  2781. h->pred16x16[TOP_DC_PRED8x8 ]= pred16x16_top_dc_c;
  2782. h->pred16x16[DC_128_PRED8x8 ]= ff_pred16x16_128_dc_c;
  2783. }
  2784. static void free_tables(H264Context *h){
  2785. av_freep(&h->intra4x4_pred_mode);
  2786. av_freep(&h->chroma_pred_mode_table);
  2787. av_freep(&h->cbp_table);
  2788. av_freep(&h->mvd_table[0]);
  2789. av_freep(&h->mvd_table[1]);
  2790. av_freep(&h->direct_table);
  2791. av_freep(&h->non_zero_count);
  2792. av_freep(&h->slice_table_base);
  2793. av_freep(&h->top_borders[1]);
  2794. av_freep(&h->top_borders[0]);
  2795. h->slice_table= NULL;
  2796. av_freep(&h->mb2b_xy);
  2797. av_freep(&h->mb2b8_xy);
  2798. av_freep(&h->s.obmc_scratchpad);
  2799. }
  2800. static void init_dequant8_coeff_table(H264Context *h){
  2801. int i,q,x;
  2802. const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
  2803. h->dequant8_coeff[0] = h->dequant8_buffer[0];
  2804. h->dequant8_coeff[1] = h->dequant8_buffer[1];
  2805. for(i=0; i<2; i++ ){
  2806. if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
  2807. h->dequant8_coeff[1] = h->dequant8_buffer[0];
  2808. break;
  2809. }
  2810. for(q=0; q<52; q++){
  2811. int shift = ff_div6[q];
  2812. int idx = ff_rem6[q];
  2813. for(x=0; x<64; x++)
  2814. h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
  2815. ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
  2816. h->pps.scaling_matrix8[i][x]) << shift;
  2817. }
  2818. }
  2819. }
  2820. static void init_dequant4_coeff_table(H264Context *h){
  2821. int i,j,q,x;
  2822. const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
  2823. for(i=0; i<6; i++ ){
  2824. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  2825. for(j=0; j<i; j++){
  2826. if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
  2827. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  2828. break;
  2829. }
  2830. }
  2831. if(j<i)
  2832. continue;
  2833. for(q=0; q<52; q++){
  2834. int shift = ff_div6[q] + 2;
  2835. int idx = ff_rem6[q];
  2836. for(x=0; x<16; x++)
  2837. h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
  2838. ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
  2839. h->pps.scaling_matrix4[i][x]) << shift;
  2840. }
  2841. }
  2842. }
  2843. static void init_dequant_tables(H264Context *h){
  2844. int i,x;
  2845. init_dequant4_coeff_table(h);
  2846. if(h->pps.transform_8x8_mode)
  2847. init_dequant8_coeff_table(h);
  2848. if(h->sps.transform_bypass){
  2849. for(i=0; i<6; i++)
  2850. for(x=0; x<16; x++)
  2851. h->dequant4_coeff[i][0][x] = 1<<6;
  2852. if(h->pps.transform_8x8_mode)
  2853. for(i=0; i<2; i++)
  2854. for(x=0; x<64; x++)
  2855. h->dequant8_coeff[i][0][x] = 1<<6;
  2856. }
  2857. }
  2858. /**
  2859. * allocates tables.
  2860. * needs width/height
  2861. */
  2862. static int alloc_tables(H264Context *h){
  2863. MpegEncContext * const s = &h->s;
  2864. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  2865. int x,y;
  2866. CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
  2867. CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
  2868. CHECKED_ALLOCZ(h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(uint8_t))
  2869. CHECKED_ALLOCZ(h->top_borders[0] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2870. CHECKED_ALLOCZ(h->top_borders[1] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2871. CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
  2872. if( h->pps.cabac ) {
  2873. CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
  2874. CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
  2875. CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t));
  2876. CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t));
  2877. }
  2878. memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(uint8_t));
  2879. h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
  2880. CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint32_t));
  2881. CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint32_t));
  2882. for(y=0; y<s->mb_height; y++){
  2883. for(x=0; x<s->mb_width; x++){
  2884. const int mb_xy= x + y*s->mb_stride;
  2885. const int b_xy = 4*x + 4*y*h->b_stride;
  2886. const int b8_xy= 2*x + 2*y*h->b8_stride;
  2887. h->mb2b_xy [mb_xy]= b_xy;
  2888. h->mb2b8_xy[mb_xy]= b8_xy;
  2889. }
  2890. }
  2891. s->obmc_scratchpad = NULL;
  2892. if(!h->dequant4_coeff[0])
  2893. init_dequant_tables(h);
  2894. return 0;
  2895. fail:
  2896. free_tables(h);
  2897. return -1;
  2898. }
  2899. static void common_init(H264Context *h){
  2900. MpegEncContext * const s = &h->s;
  2901. s->width = s->avctx->width;
  2902. s->height = s->avctx->height;
  2903. s->codec_id= s->avctx->codec->id;
  2904. init_pred_ptrs(h);
  2905. h->dequant_coeff_pps= -1;
  2906. s->unrestricted_mv=1;
  2907. s->decode=1; //FIXME
  2908. memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  2909. memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  2910. }
  2911. static int decode_init(AVCodecContext *avctx){
  2912. H264Context *h= avctx->priv_data;
  2913. MpegEncContext * const s = &h->s;
  2914. MPV_decode_defaults(s);
  2915. s->avctx = avctx;
  2916. common_init(h);
  2917. s->out_format = FMT_H264;
  2918. s->workaround_bugs= avctx->workaround_bugs;
  2919. // set defaults
  2920. // s->decode_mb= ff_h263_decode_mb;
  2921. s->low_delay= 1;
  2922. avctx->pix_fmt= PIX_FMT_YUV420P;
  2923. decode_init_vlc();
  2924. if(avctx->extradata_size > 0 && avctx->extradata &&
  2925. *(char *)avctx->extradata == 1){
  2926. h->is_avc = 1;
  2927. h->got_avcC = 0;
  2928. } else {
  2929. h->is_avc = 0;
  2930. }
  2931. return 0;
  2932. }
  2933. static int frame_start(H264Context *h){
  2934. MpegEncContext * const s = &h->s;
  2935. int i;
  2936. if(MPV_frame_start(s, s->avctx) < 0)
  2937. return -1;
  2938. ff_er_frame_start(s);
  2939. assert(s->linesize && s->uvlinesize);
  2940. for(i=0; i<16; i++){
  2941. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  2942. h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  2943. }
  2944. for(i=0; i<4; i++){
  2945. h->block_offset[16+i]=
  2946. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2947. h->block_offset[24+16+i]=
  2948. h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2949. }
  2950. /* can't be in alloc_tables because linesize isn't known there.
  2951. * FIXME: redo bipred weight to not require extra buffer? */
  2952. if(!s->obmc_scratchpad)
  2953. s->obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  2954. /* some macroblocks will be accessed before they're available */
  2955. if(FRAME_MBAFF)
  2956. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(uint8_t));
  2957. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  2958. return 0;
  2959. }
  2960. static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  2961. MpegEncContext * const s = &h->s;
  2962. int i;
  2963. src_y -= linesize;
  2964. src_cb -= uvlinesize;
  2965. src_cr -= uvlinesize;
  2966. // There are two lines saved, the line above the the top macroblock of a pair,
  2967. // and the line above the bottom macroblock
  2968. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  2969. for(i=1; i<17; i++){
  2970. h->left_border[i]= src_y[15+i* linesize];
  2971. }
  2972. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
  2973. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
  2974. if(!(s->flags&CODEC_FLAG_GRAY)){
  2975. h->left_border[17 ]= h->top_borders[0][s->mb_x][16+7];
  2976. h->left_border[17+9]= h->top_borders[0][s->mb_x][24+7];
  2977. for(i=1; i<9; i++){
  2978. h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
  2979. h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
  2980. }
  2981. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
  2982. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
  2983. }
  2984. }
  2985. 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){
  2986. MpegEncContext * const s = &h->s;
  2987. int temp8, i;
  2988. uint64_t temp64;
  2989. int deblock_left = (s->mb_x > 0);
  2990. int deblock_top = (s->mb_y > 0);
  2991. src_y -= linesize + 1;
  2992. src_cb -= uvlinesize + 1;
  2993. src_cr -= uvlinesize + 1;
  2994. #define XCHG(a,b,t,xchg)\
  2995. t= a;\
  2996. if(xchg)\
  2997. a= b;\
  2998. b= t;
  2999. if(deblock_left){
  3000. for(i = !deblock_top; i<17; i++){
  3001. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  3002. }
  3003. }
  3004. if(deblock_top){
  3005. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  3006. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  3007. if(s->mb_x+1 < s->mb_width){
  3008. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  3009. }
  3010. }
  3011. if(!(s->flags&CODEC_FLAG_GRAY)){
  3012. if(deblock_left){
  3013. for(i = !deblock_top; i<9; i++){
  3014. XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
  3015. XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
  3016. }
  3017. }
  3018. if(deblock_top){
  3019. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  3020. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  3021. }
  3022. }
  3023. }
  3024. static inline void backup_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  3025. MpegEncContext * const s = &h->s;
  3026. int i;
  3027. src_y -= 2 * linesize;
  3028. src_cb -= 2 * uvlinesize;
  3029. src_cr -= 2 * uvlinesize;
  3030. // There are two lines saved, the line above the the top macroblock of a pair,
  3031. // and the line above the bottom macroblock
  3032. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  3033. h->left_border[1]= h->top_borders[1][s->mb_x][15];
  3034. for(i=2; i<34; i++){
  3035. h->left_border[i]= src_y[15+i* linesize];
  3036. }
  3037. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 32*linesize);
  3038. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+32*linesize);
  3039. *(uint64_t*)(h->top_borders[1][s->mb_x]+0)= *(uint64_t*)(src_y + 33*linesize);
  3040. *(uint64_t*)(h->top_borders[1][s->mb_x]+8)= *(uint64_t*)(src_y +8+33*linesize);
  3041. if(!(s->flags&CODEC_FLAG_GRAY)){
  3042. h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7];
  3043. h->left_border[34+ 1]= h->top_borders[1][s->mb_x][16+7];
  3044. h->left_border[34+18 ]= h->top_borders[0][s->mb_x][24+7];
  3045. h->left_border[34+18+1]= h->top_borders[1][s->mb_x][24+7];
  3046. for(i=2; i<18; i++){
  3047. h->left_border[i+34 ]= src_cb[7+i*uvlinesize];
  3048. h->left_border[i+34+18]= src_cr[7+i*uvlinesize];
  3049. }
  3050. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+16*uvlinesize);
  3051. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+16*uvlinesize);
  3052. *(uint64_t*)(h->top_borders[1][s->mb_x]+16)= *(uint64_t*)(src_cb+17*uvlinesize);
  3053. *(uint64_t*)(h->top_borders[1][s->mb_x]+24)= *(uint64_t*)(src_cr+17*uvlinesize);
  3054. }
  3055. }
  3056. 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){
  3057. MpegEncContext * const s = &h->s;
  3058. int temp8, i;
  3059. uint64_t temp64;
  3060. int deblock_left = (s->mb_x > 0);
  3061. int deblock_top = (s->mb_y > 1);
  3062. 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);
  3063. src_y -= 2 * linesize + 1;
  3064. src_cb -= 2 * uvlinesize + 1;
  3065. src_cr -= 2 * uvlinesize + 1;
  3066. #define XCHG(a,b,t,xchg)\
  3067. t= a;\
  3068. if(xchg)\
  3069. a= b;\
  3070. b= t;
  3071. if(deblock_left){
  3072. for(i = (!deblock_top)<<1; i<34; i++){
  3073. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  3074. }
  3075. }
  3076. if(deblock_top){
  3077. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  3078. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  3079. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+0), *(uint64_t*)(src_y +1 +linesize), temp64, xchg);
  3080. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+8), *(uint64_t*)(src_y +9 +linesize), temp64, 1);
  3081. if(s->mb_x+1 < s->mb_width){
  3082. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  3083. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x+1]), *(uint64_t*)(src_y +17 +linesize), temp64, 1);
  3084. }
  3085. }
  3086. if(!(s->flags&CODEC_FLAG_GRAY)){
  3087. if(deblock_left){
  3088. for(i = (!deblock_top) << 1; i<18; i++){
  3089. XCHG(h->left_border[i+34 ], src_cb[i*uvlinesize], temp8, xchg);
  3090. XCHG(h->left_border[i+34+18], src_cr[i*uvlinesize], temp8, xchg);
  3091. }
  3092. }
  3093. if(deblock_top){
  3094. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  3095. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  3096. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+16), *(uint64_t*)(src_cb+1 +uvlinesize), temp64, 1);
  3097. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+24), *(uint64_t*)(src_cr+1 +uvlinesize), temp64, 1);
  3098. }
  3099. }
  3100. }
  3101. static void hl_decode_mb(H264Context *h){
  3102. MpegEncContext * const s = &h->s;
  3103. const int mb_x= s->mb_x;
  3104. const int mb_y= s->mb_y;
  3105. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3106. const int mb_type= s->current_picture.mb_type[mb_xy];
  3107. uint8_t *dest_y, *dest_cb, *dest_cr;
  3108. int linesize, uvlinesize /*dct_offset*/;
  3109. int i;
  3110. int *block_offset = &h->block_offset[0];
  3111. const unsigned int bottom = mb_y & 1;
  3112. const int transform_bypass = (s->qscale == 0 && h->sps.transform_bypass);
  3113. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  3114. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  3115. if(!s->decode)
  3116. return;
  3117. dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3118. dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3119. dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3120. s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
  3121. s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
  3122. if (MB_FIELD) {
  3123. linesize = h->mb_linesize = s->linesize * 2;
  3124. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  3125. block_offset = &h->block_offset[24];
  3126. if(mb_y&1){ //FIXME move out of this func?
  3127. dest_y -= s->linesize*15;
  3128. dest_cb-= s->uvlinesize*7;
  3129. dest_cr-= s->uvlinesize*7;
  3130. }
  3131. if(FRAME_MBAFF) {
  3132. int list;
  3133. for(list=0; list<2; list++){
  3134. if(!USES_LIST(mb_type, list))
  3135. continue;
  3136. if(IS_16X16(mb_type)){
  3137. int8_t *ref = &h->ref_cache[list][scan8[0]];
  3138. fill_rectangle(ref, 4, 4, 8, 16+*ref^(s->mb_y&1), 1);
  3139. }else{
  3140. for(i=0; i<16; i+=4){
  3141. //FIXME can refs be smaller than 8x8 when !direct_8x8_inference ?
  3142. int ref = h->ref_cache[list][scan8[i]];
  3143. if(ref >= 0)
  3144. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, 16+ref^(s->mb_y&1), 1);
  3145. }
  3146. }
  3147. }
  3148. }
  3149. } else {
  3150. linesize = h->mb_linesize = s->linesize;
  3151. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  3152. // dct_offset = s->linesize * 16;
  3153. }
  3154. if(transform_bypass){
  3155. idct_dc_add =
  3156. idct_add = IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  3157. }else if(IS_8x8DCT(mb_type)){
  3158. idct_dc_add = s->dsp.h264_idct8_dc_add;
  3159. idct_add = s->dsp.h264_idct8_add;
  3160. }else{
  3161. idct_dc_add = s->dsp.h264_idct_dc_add;
  3162. idct_add = s->dsp.h264_idct_add;
  3163. }
  3164. if(FRAME_MBAFF && h->deblocking_filter && IS_INTRA(mb_type)
  3165. && (!bottom || !IS_INTRA(s->current_picture.mb_type[mb_xy-s->mb_stride]))){
  3166. int mbt_y = mb_y&~1;
  3167. uint8_t *top_y = s->current_picture.data[0] + (mbt_y * 16* s->linesize ) + mb_x * 16;
  3168. uint8_t *top_cb = s->current_picture.data[1] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  3169. uint8_t *top_cr = s->current_picture.data[2] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  3170. xchg_pair_border(h, top_y, top_cb, top_cr, s->linesize, s->uvlinesize, 1);
  3171. }
  3172. if (IS_INTRA_PCM(mb_type)) {
  3173. unsigned int x, y;
  3174. // The pixels are stored in h->mb array in the same order as levels,
  3175. // copy them in output in the correct order.
  3176. for(i=0; i<16; i++) {
  3177. for (y=0; y<4; y++) {
  3178. for (x=0; x<4; x++) {
  3179. *(dest_y + block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x];
  3180. }
  3181. }
  3182. }
  3183. for(i=16; i<16+4; i++) {
  3184. for (y=0; y<4; y++) {
  3185. for (x=0; x<4; x++) {
  3186. *(dest_cb + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  3187. }
  3188. }
  3189. }
  3190. for(i=20; i<20+4; i++) {
  3191. for (y=0; y<4; y++) {
  3192. for (x=0; x<4; x++) {
  3193. *(dest_cr + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  3194. }
  3195. }
  3196. }
  3197. } else {
  3198. if(IS_INTRA(mb_type)){
  3199. if(h->deblocking_filter && !FRAME_MBAFF)
  3200. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1);
  3201. if(!(s->flags&CODEC_FLAG_GRAY)){
  3202. h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  3203. h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  3204. }
  3205. if(IS_INTRA4x4(mb_type)){
  3206. if(!s->encoding){
  3207. if(IS_8x8DCT(mb_type)){
  3208. for(i=0; i<16; i+=4){
  3209. uint8_t * const ptr= dest_y + block_offset[i];
  3210. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  3211. const int nnz = h->non_zero_count_cache[ scan8[i] ];
  3212. h->pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  3213. (h->topright_samples_available<<(i+1))&0x8000, linesize);
  3214. if(nnz){
  3215. if(nnz == 1 && h->mb[i*16])
  3216. idct_dc_add(ptr, h->mb + i*16, linesize);
  3217. else
  3218. idct_add(ptr, h->mb + i*16, linesize);
  3219. }
  3220. }
  3221. }else
  3222. for(i=0; i<16; i++){
  3223. uint8_t * const ptr= dest_y + block_offset[i];
  3224. uint8_t *topright;
  3225. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  3226. int nnz, tr;
  3227. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  3228. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  3229. assert(mb_y || linesize <= block_offset[i]);
  3230. if(!topright_avail){
  3231. tr= ptr[3 - linesize]*0x01010101;
  3232. topright= (uint8_t*) &tr;
  3233. }else
  3234. topright= ptr + 4 - linesize;
  3235. }else
  3236. topright= NULL;
  3237. h->pred4x4[ dir ](ptr, topright, linesize);
  3238. nnz = h->non_zero_count_cache[ scan8[i] ];
  3239. if(nnz){
  3240. if(s->codec_id == CODEC_ID_H264){
  3241. if(nnz == 1 && h->mb[i*16])
  3242. idct_dc_add(ptr, h->mb + i*16, linesize);
  3243. else
  3244. idct_add(ptr, h->mb + i*16, linesize);
  3245. }else
  3246. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  3247. }
  3248. }
  3249. }
  3250. }else{
  3251. h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  3252. if(s->codec_id == CODEC_ID_H264){
  3253. if(!transform_bypass)
  3254. h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[IS_INTRA(mb_type) ? 0:3][s->qscale][0]);
  3255. }else
  3256. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  3257. }
  3258. if(h->deblocking_filter && !FRAME_MBAFF)
  3259. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
  3260. }else if(s->codec_id == CODEC_ID_H264){
  3261. hl_motion(h, dest_y, dest_cb, dest_cr,
  3262. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  3263. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  3264. s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
  3265. }
  3266. if(!IS_INTRA4x4(mb_type)){
  3267. if(s->codec_id == CODEC_ID_H264){
  3268. if(IS_INTRA16x16(mb_type)){
  3269. for(i=0; i<16; i++){
  3270. if(h->non_zero_count_cache[ scan8[i] ])
  3271. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3272. else if(h->mb[i*16])
  3273. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3274. }
  3275. }else{
  3276. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  3277. for(i=0; i<16; i+=di){
  3278. int nnz = h->non_zero_count_cache[ scan8[i] ];
  3279. if(nnz){
  3280. if(nnz==1 && h->mb[i*16])
  3281. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3282. else
  3283. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3284. }
  3285. }
  3286. }
  3287. }else{
  3288. for(i=0; i<16; i++){
  3289. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  3290. uint8_t * const ptr= dest_y + block_offset[i];
  3291. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  3292. }
  3293. }
  3294. }
  3295. }
  3296. if(!(s->flags&CODEC_FLAG_GRAY)){
  3297. uint8_t *dest[2] = {dest_cb, dest_cr};
  3298. if(transform_bypass){
  3299. idct_add = idct_dc_add = s->dsp.add_pixels4;
  3300. }else{
  3301. idct_add = s->dsp.h264_idct_add;
  3302. idct_dc_add = s->dsp.h264_idct_dc_add;
  3303. 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]);
  3304. 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]);
  3305. }
  3306. if(s->codec_id == CODEC_ID_H264){
  3307. for(i=16; i<16+8; i++){
  3308. if(h->non_zero_count_cache[ scan8[i] ])
  3309. idct_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3310. else if(h->mb[i*16])
  3311. idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3312. }
  3313. }else{
  3314. for(i=16; i<16+8; i++){
  3315. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  3316. uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
  3317. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  3318. }
  3319. }
  3320. }
  3321. }
  3322. }
  3323. if(h->deblocking_filter) {
  3324. if (FRAME_MBAFF) {
  3325. //FIXME try deblocking one mb at a time?
  3326. // the reduction in load/storing mvs and such might outweigh the extra backup/xchg_border
  3327. const int mb_y = s->mb_y - 1;
  3328. uint8_t *pair_dest_y, *pair_dest_cb, *pair_dest_cr;
  3329. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3330. const int mb_type_top = s->current_picture.mb_type[mb_xy];
  3331. const int mb_type_bottom= s->current_picture.mb_type[mb_xy+s->mb_stride];
  3332. if (!bottom) return;
  3333. pair_dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3334. pair_dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3335. pair_dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3336. if(IS_INTRA(mb_type_top | mb_type_bottom))
  3337. xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0);
  3338. backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize);
  3339. // deblock a pair
  3340. // top
  3341. s->mb_y--;
  3342. 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);
  3343. fill_caches(h, mb_type_top, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3344. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy]);
  3345. filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize);
  3346. // bottom
  3347. s->mb_y++;
  3348. tprintf("call mbaff filter_mb\n");
  3349. fill_caches(h, mb_type_bottom, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3350. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy+s->mb_stride]);
  3351. filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3352. } else {
  3353. tprintf("call filter_mb\n");
  3354. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3355. fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3356. filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3357. }
  3358. }
  3359. }
  3360. /**
  3361. * fills the default_ref_list.
  3362. */
  3363. static int fill_default_ref_list(H264Context *h){
  3364. MpegEncContext * const s = &h->s;
  3365. int i;
  3366. int smallest_poc_greater_than_current = -1;
  3367. Picture sorted_short_ref[32];
  3368. if(h->slice_type==B_TYPE){
  3369. int out_i;
  3370. int limit= INT_MIN;
  3371. /* sort frame according to poc in B slice */
  3372. for(out_i=0; out_i<h->short_ref_count; out_i++){
  3373. int best_i=INT_MIN;
  3374. int best_poc=INT_MAX;
  3375. for(i=0; i<h->short_ref_count; i++){
  3376. const int poc= h->short_ref[i]->poc;
  3377. if(poc > limit && poc < best_poc){
  3378. best_poc= poc;
  3379. best_i= i;
  3380. }
  3381. }
  3382. assert(best_i != INT_MIN);
  3383. limit= best_poc;
  3384. sorted_short_ref[out_i]= *h->short_ref[best_i];
  3385. 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);
  3386. if (-1 == smallest_poc_greater_than_current) {
  3387. if (h->short_ref[best_i]->poc >= s->current_picture_ptr->poc) {
  3388. smallest_poc_greater_than_current = out_i;
  3389. }
  3390. }
  3391. }
  3392. }
  3393. if(s->picture_structure == PICT_FRAME){
  3394. if(h->slice_type==B_TYPE){
  3395. int list;
  3396. tprintf("current poc: %d, smallest_poc_greater_than_current: %d\n", s->current_picture_ptr->poc, smallest_poc_greater_than_current);
  3397. // find the largest poc
  3398. for(list=0; list<2; list++){
  3399. int index = 0;
  3400. int j= -99;
  3401. int step= list ? -1 : 1;
  3402. for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++, j+=step) {
  3403. while(j<0 || j>= h->short_ref_count){
  3404. if(j != -99 && step == (list ? -1 : 1))
  3405. return -1;
  3406. step = -step;
  3407. j= smallest_poc_greater_than_current + (step>>1);
  3408. }
  3409. if(sorted_short_ref[j].reference != 3) continue;
  3410. h->default_ref_list[list][index ]= sorted_short_ref[j];
  3411. h->default_ref_list[list][index++].pic_id= sorted_short_ref[j].frame_num;
  3412. }
  3413. for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){
  3414. if(h->long_ref[i] == NULL) continue;
  3415. if(h->long_ref[i]->reference != 3) continue;
  3416. h->default_ref_list[ list ][index ]= *h->long_ref[i];
  3417. h->default_ref_list[ list ][index++].pic_id= i;;
  3418. }
  3419. if(list && (smallest_poc_greater_than_current<=0 || smallest_poc_greater_than_current>=h->short_ref_count) && (1 < index)){
  3420. // swap the two first elements of L1 when
  3421. // L0 and L1 are identical
  3422. Picture temp= h->default_ref_list[1][0];
  3423. h->default_ref_list[1][0] = h->default_ref_list[1][1];
  3424. h->default_ref_list[1][1] = temp;
  3425. }
  3426. if(index < h->ref_count[ list ])
  3427. memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
  3428. }
  3429. }else{
  3430. int index=0;
  3431. for(i=0; i<h->short_ref_count; i++){
  3432. if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
  3433. h->default_ref_list[0][index ]= *h->short_ref[i];
  3434. h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
  3435. }
  3436. for(i = 0; i < 16; i++){
  3437. if(h->long_ref[i] == NULL) continue;
  3438. if(h->long_ref[i]->reference != 3) continue;
  3439. h->default_ref_list[0][index ]= *h->long_ref[i];
  3440. h->default_ref_list[0][index++].pic_id= i;;
  3441. }
  3442. if(index < h->ref_count[0])
  3443. memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
  3444. }
  3445. }else{ //FIELD
  3446. if(h->slice_type==B_TYPE){
  3447. }else{
  3448. //FIXME second field balh
  3449. }
  3450. }
  3451. #ifdef TRACE
  3452. for (i=0; i<h->ref_count[0]; i++) {
  3453. 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]);
  3454. }
  3455. if(h->slice_type==B_TYPE){
  3456. for (i=0; i<h->ref_count[1]; i++) {
  3457. 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]);
  3458. }
  3459. }
  3460. #endif
  3461. return 0;
  3462. }
  3463. static void print_short_term(H264Context *h);
  3464. static void print_long_term(H264Context *h);
  3465. static int decode_ref_pic_list_reordering(H264Context *h){
  3466. MpegEncContext * const s = &h->s;
  3467. int list, index;
  3468. print_short_term(h);
  3469. print_long_term(h);
  3470. if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move before func
  3471. for(list=0; list<2; list++){
  3472. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  3473. if(get_bits1(&s->gb)){
  3474. int pred= h->curr_pic_num;
  3475. for(index=0; ; index++){
  3476. int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
  3477. int pic_id;
  3478. int i;
  3479. Picture *ref = NULL;
  3480. if(reordering_of_pic_nums_idc==3)
  3481. break;
  3482. if(index >= h->ref_count[list]){
  3483. av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
  3484. return -1;
  3485. }
  3486. if(reordering_of_pic_nums_idc<3){
  3487. if(reordering_of_pic_nums_idc<2){
  3488. const int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  3489. if(abs_diff_pic_num >= h->max_pic_num){
  3490. av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  3491. return -1;
  3492. }
  3493. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  3494. else pred+= abs_diff_pic_num;
  3495. pred &= h->max_pic_num - 1;
  3496. for(i= h->short_ref_count-1; i>=0; i--){
  3497. ref = h->short_ref[i];
  3498. assert(ref->reference == 3);
  3499. assert(!ref->long_ref);
  3500. if(ref->data[0] != NULL && ref->frame_num == pred && ref->long_ref == 0) // ignore non existing pictures by testing data[0] pointer
  3501. break;
  3502. }
  3503. if(i>=0)
  3504. ref->pic_id= ref->frame_num;
  3505. }else{
  3506. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  3507. ref = h->long_ref[pic_id];
  3508. if(ref){
  3509. ref->pic_id= pic_id;
  3510. assert(ref->reference == 3);
  3511. assert(ref->long_ref);
  3512. i=0;
  3513. }else{
  3514. i=-1;
  3515. }
  3516. }
  3517. if (i < 0) {
  3518. av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  3519. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  3520. } else {
  3521. for(i=index; i+1<h->ref_count[list]; i++){
  3522. if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
  3523. break;
  3524. }
  3525. for(; i > index; i--){
  3526. h->ref_list[list][i]= h->ref_list[list][i-1];
  3527. }
  3528. h->ref_list[list][index]= *ref;
  3529. }
  3530. }else{
  3531. av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  3532. return -1;
  3533. }
  3534. }
  3535. }
  3536. if(h->slice_type!=B_TYPE) break;
  3537. }
  3538. for(list=0; list<2; list++){
  3539. for(index= 0; index < h->ref_count[list]; index++){
  3540. if(!h->ref_list[list][index].data[0])
  3541. h->ref_list[list][index]= s->current_picture;
  3542. }
  3543. if(h->slice_type!=B_TYPE) break;
  3544. }
  3545. if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)
  3546. direct_dist_scale_factor(h);
  3547. direct_ref_list_init(h);
  3548. return 0;
  3549. }
  3550. static void fill_mbaff_ref_list(H264Context *h){
  3551. int list, i, j;
  3552. for(list=0; list<2; list++){
  3553. for(i=0; i<h->ref_count[list]; i++){
  3554. Picture *frame = &h->ref_list[list][i];
  3555. Picture *field = &h->ref_list[list][16+2*i];
  3556. field[0] = *frame;
  3557. for(j=0; j<3; j++)
  3558. field[0].linesize[j] <<= 1;
  3559. field[1] = field[0];
  3560. for(j=0; j<3; j++)
  3561. field[1].data[j] += frame->linesize[j];
  3562. h->luma_weight[list][16+2*i] = h->luma_weight[list][16+2*i+1] = h->luma_weight[list][i];
  3563. h->luma_offset[list][16+2*i] = h->luma_offset[list][16+2*i+1] = h->luma_offset[list][i];
  3564. for(j=0; j<2; j++){
  3565. h->chroma_weight[list][16+2*i][j] = h->chroma_weight[list][16+2*i+1][j] = h->chroma_weight[list][i][j];
  3566. h->chroma_offset[list][16+2*i][j] = h->chroma_offset[list][16+2*i+1][j] = h->chroma_offset[list][i][j];
  3567. }
  3568. }
  3569. }
  3570. for(j=0; j<h->ref_count[1]; j++){
  3571. for(i=0; i<h->ref_count[0]; i++)
  3572. h->implicit_weight[j][16+2*i] = h->implicit_weight[j][16+2*i+1] = h->implicit_weight[j][i];
  3573. memcpy(h->implicit_weight[16+2*j], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3574. memcpy(h->implicit_weight[16+2*j+1], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3575. }
  3576. }
  3577. static int pred_weight_table(H264Context *h){
  3578. MpegEncContext * const s = &h->s;
  3579. int list, i;
  3580. int luma_def, chroma_def;
  3581. h->use_weight= 0;
  3582. h->use_weight_chroma= 0;
  3583. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  3584. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  3585. luma_def = 1<<h->luma_log2_weight_denom;
  3586. chroma_def = 1<<h->chroma_log2_weight_denom;
  3587. for(list=0; list<2; list++){
  3588. for(i=0; i<h->ref_count[list]; i++){
  3589. int luma_weight_flag, chroma_weight_flag;
  3590. luma_weight_flag= get_bits1(&s->gb);
  3591. if(luma_weight_flag){
  3592. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  3593. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  3594. if( h->luma_weight[list][i] != luma_def
  3595. || h->luma_offset[list][i] != 0)
  3596. h->use_weight= 1;
  3597. }else{
  3598. h->luma_weight[list][i]= luma_def;
  3599. h->luma_offset[list][i]= 0;
  3600. }
  3601. chroma_weight_flag= get_bits1(&s->gb);
  3602. if(chroma_weight_flag){
  3603. int j;
  3604. for(j=0; j<2; j++){
  3605. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  3606. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  3607. if( h->chroma_weight[list][i][j] != chroma_def
  3608. || h->chroma_offset[list][i][j] != 0)
  3609. h->use_weight_chroma= 1;
  3610. }
  3611. }else{
  3612. int j;
  3613. for(j=0; j<2; j++){
  3614. h->chroma_weight[list][i][j]= chroma_def;
  3615. h->chroma_offset[list][i][j]= 0;
  3616. }
  3617. }
  3618. }
  3619. if(h->slice_type != B_TYPE) break;
  3620. }
  3621. h->use_weight= h->use_weight || h->use_weight_chroma;
  3622. return 0;
  3623. }
  3624. static void implicit_weight_table(H264Context *h){
  3625. MpegEncContext * const s = &h->s;
  3626. int ref0, ref1;
  3627. int cur_poc = s->current_picture_ptr->poc;
  3628. if( h->ref_count[0] == 1 && h->ref_count[1] == 1
  3629. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  3630. h->use_weight= 0;
  3631. h->use_weight_chroma= 0;
  3632. return;
  3633. }
  3634. h->use_weight= 2;
  3635. h->use_weight_chroma= 2;
  3636. h->luma_log2_weight_denom= 5;
  3637. h->chroma_log2_weight_denom= 5;
  3638. for(ref0=0; ref0 < h->ref_count[0]; ref0++){
  3639. int poc0 = h->ref_list[0][ref0].poc;
  3640. for(ref1=0; ref1 < h->ref_count[1]; ref1++){
  3641. int poc1 = h->ref_list[1][ref1].poc;
  3642. int td = clip(poc1 - poc0, -128, 127);
  3643. if(td){
  3644. int tb = clip(cur_poc - poc0, -128, 127);
  3645. int tx = (16384 + (FFABS(td) >> 1)) / td;
  3646. int dist_scale_factor = clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
  3647. if(dist_scale_factor < -64 || dist_scale_factor > 128)
  3648. h->implicit_weight[ref0][ref1] = 32;
  3649. else
  3650. h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
  3651. }else
  3652. h->implicit_weight[ref0][ref1] = 32;
  3653. }
  3654. }
  3655. }
  3656. static inline void unreference_pic(H264Context *h, Picture *pic){
  3657. int i;
  3658. pic->reference=0;
  3659. if(pic == h->delayed_output_pic)
  3660. pic->reference=1;
  3661. else{
  3662. for(i = 0; h->delayed_pic[i]; i++)
  3663. if(pic == h->delayed_pic[i]){
  3664. pic->reference=1;
  3665. break;
  3666. }
  3667. }
  3668. }
  3669. /**
  3670. * instantaneous decoder refresh.
  3671. */
  3672. static void idr(H264Context *h){
  3673. int i;
  3674. for(i=0; i<16; i++){
  3675. if (h->long_ref[i] != NULL) {
  3676. unreference_pic(h, h->long_ref[i]);
  3677. h->long_ref[i]= NULL;
  3678. }
  3679. }
  3680. h->long_ref_count=0;
  3681. for(i=0; i<h->short_ref_count; i++){
  3682. unreference_pic(h, h->short_ref[i]);
  3683. h->short_ref[i]= NULL;
  3684. }
  3685. h->short_ref_count=0;
  3686. }
  3687. /* forget old pics after a seek */
  3688. static void flush_dpb(AVCodecContext *avctx){
  3689. H264Context *h= avctx->priv_data;
  3690. int i;
  3691. for(i=0; i<16; i++) {
  3692. if(h->delayed_pic[i])
  3693. h->delayed_pic[i]->reference= 0;
  3694. h->delayed_pic[i]= NULL;
  3695. }
  3696. if(h->delayed_output_pic)
  3697. h->delayed_output_pic->reference= 0;
  3698. h->delayed_output_pic= NULL;
  3699. idr(h);
  3700. if(h->s.current_picture_ptr)
  3701. h->s.current_picture_ptr->reference= 0;
  3702. }
  3703. /**
  3704. *
  3705. * @return the removed picture or NULL if an error occurs
  3706. */
  3707. static Picture * remove_short(H264Context *h, int frame_num){
  3708. MpegEncContext * const s = &h->s;
  3709. int i;
  3710. if(s->avctx->debug&FF_DEBUG_MMCO)
  3711. av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  3712. for(i=0; i<h->short_ref_count; i++){
  3713. Picture *pic= h->short_ref[i];
  3714. if(s->avctx->debug&FF_DEBUG_MMCO)
  3715. av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  3716. if(pic->frame_num == frame_num){
  3717. h->short_ref[i]= NULL;
  3718. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
  3719. h->short_ref_count--;
  3720. return pic;
  3721. }
  3722. }
  3723. return NULL;
  3724. }
  3725. /**
  3726. *
  3727. * @return the removed picture or NULL if an error occurs
  3728. */
  3729. static Picture * remove_long(H264Context *h, int i){
  3730. Picture *pic;
  3731. pic= h->long_ref[i];
  3732. h->long_ref[i]= NULL;
  3733. if(pic) h->long_ref_count--;
  3734. return pic;
  3735. }
  3736. /**
  3737. * print short term list
  3738. */
  3739. static void print_short_term(H264Context *h) {
  3740. uint32_t i;
  3741. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3742. av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
  3743. for(i=0; i<h->short_ref_count; i++){
  3744. Picture *pic= h->short_ref[i];
  3745. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3746. }
  3747. }
  3748. }
  3749. /**
  3750. * print long term list
  3751. */
  3752. static void print_long_term(H264Context *h) {
  3753. uint32_t i;
  3754. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3755. av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
  3756. for(i = 0; i < 16; i++){
  3757. Picture *pic= h->long_ref[i];
  3758. if (pic) {
  3759. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3760. }
  3761. }
  3762. }
  3763. }
  3764. /**
  3765. * Executes the reference picture marking (memory management control operations).
  3766. */
  3767. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  3768. MpegEncContext * const s = &h->s;
  3769. int i, j;
  3770. int current_is_long=0;
  3771. Picture *pic;
  3772. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  3773. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  3774. for(i=0; i<mmco_count; i++){
  3775. if(s->avctx->debug&FF_DEBUG_MMCO)
  3776. 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);
  3777. switch(mmco[i].opcode){
  3778. case MMCO_SHORT2UNUSED:
  3779. pic= remove_short(h, mmco[i].short_frame_num);
  3780. if(pic)
  3781. unreference_pic(h, pic);
  3782. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3783. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_short() failure\n");
  3784. break;
  3785. case MMCO_SHORT2LONG:
  3786. pic= remove_long(h, mmco[i].long_index);
  3787. if(pic) unreference_pic(h, pic);
  3788. h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
  3789. if (h->long_ref[ mmco[i].long_index ]){
  3790. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3791. h->long_ref_count++;
  3792. }
  3793. break;
  3794. case MMCO_LONG2UNUSED:
  3795. pic= remove_long(h, mmco[i].long_index);
  3796. if(pic)
  3797. unreference_pic(h, pic);
  3798. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3799. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_long() failure\n");
  3800. break;
  3801. case MMCO_LONG:
  3802. pic= remove_long(h, mmco[i].long_index);
  3803. if(pic) unreference_pic(h, pic);
  3804. h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
  3805. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3806. h->long_ref_count++;
  3807. current_is_long=1;
  3808. break;
  3809. case MMCO_SET_MAX_LONG:
  3810. assert(mmco[i].long_index <= 16);
  3811. // just remove the long term which index is greater than new max
  3812. for(j = mmco[i].long_index; j<16; j++){
  3813. pic = remove_long(h, j);
  3814. if (pic) unreference_pic(h, pic);
  3815. }
  3816. break;
  3817. case MMCO_RESET:
  3818. while(h->short_ref_count){
  3819. pic= remove_short(h, h->short_ref[0]->frame_num);
  3820. if(pic) unreference_pic(h, pic);
  3821. }
  3822. for(j = 0; j < 16; j++) {
  3823. pic= remove_long(h, j);
  3824. if(pic) unreference_pic(h, pic);
  3825. }
  3826. break;
  3827. default: assert(0);
  3828. }
  3829. }
  3830. if(!current_is_long){
  3831. pic= remove_short(h, s->current_picture_ptr->frame_num);
  3832. if(pic){
  3833. unreference_pic(h, pic);
  3834. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  3835. }
  3836. if(h->short_ref_count)
  3837. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  3838. h->short_ref[0]= s->current_picture_ptr;
  3839. h->short_ref[0]->long_ref=0;
  3840. h->short_ref_count++;
  3841. }
  3842. print_short_term(h);
  3843. print_long_term(h);
  3844. return 0;
  3845. }
  3846. static int decode_ref_pic_marking(H264Context *h){
  3847. MpegEncContext * const s = &h->s;
  3848. int i;
  3849. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  3850. s->broken_link= get_bits1(&s->gb) -1;
  3851. h->mmco[0].long_index= get_bits1(&s->gb) - 1; // current_long_term_idx
  3852. if(h->mmco[0].long_index == -1)
  3853. h->mmco_index= 0;
  3854. else{
  3855. h->mmco[0].opcode= MMCO_LONG;
  3856. h->mmco_index= 1;
  3857. }
  3858. }else{
  3859. if(get_bits1(&s->gb)){ // adaptive_ref_pic_marking_mode_flag
  3860. for(i= 0; i<MAX_MMCO_COUNT; i++) {
  3861. MMCOOpcode opcode= get_ue_golomb(&s->gb);;
  3862. h->mmco[i].opcode= opcode;
  3863. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  3864. 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
  3865. /* if(h->mmco[i].short_frame_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_frame_num ] == NULL){
  3866. av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
  3867. return -1;
  3868. }*/
  3869. }
  3870. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  3871. h->mmco[i].long_index= get_ue_golomb(&s->gb);
  3872. 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){
  3873. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  3874. return -1;
  3875. }
  3876. }
  3877. if(opcode > MMCO_LONG){
  3878. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  3879. return -1;
  3880. }
  3881. if(opcode == MMCO_END)
  3882. break;
  3883. }
  3884. h->mmco_index= i;
  3885. }else{
  3886. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  3887. if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){ //FIXME fields
  3888. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  3889. h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  3890. h->mmco_index= 1;
  3891. }else
  3892. h->mmco_index= 0;
  3893. }
  3894. }
  3895. return 0;
  3896. }
  3897. static int init_poc(H264Context *h){
  3898. MpegEncContext * const s = &h->s;
  3899. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  3900. int field_poc[2];
  3901. if(h->nal_unit_type == NAL_IDR_SLICE){
  3902. h->frame_num_offset= 0;
  3903. }else{
  3904. if(h->frame_num < h->prev_frame_num)
  3905. h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
  3906. else
  3907. h->frame_num_offset= h->prev_frame_num_offset;
  3908. }
  3909. if(h->sps.poc_type==0){
  3910. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  3911. if(h->nal_unit_type == NAL_IDR_SLICE){
  3912. h->prev_poc_msb=
  3913. h->prev_poc_lsb= 0;
  3914. }
  3915. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  3916. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  3917. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  3918. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  3919. else
  3920. h->poc_msb = h->prev_poc_msb;
  3921. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  3922. field_poc[0] =
  3923. field_poc[1] = h->poc_msb + h->poc_lsb;
  3924. if(s->picture_structure == PICT_FRAME)
  3925. field_poc[1] += h->delta_poc_bottom;
  3926. }else if(h->sps.poc_type==1){
  3927. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  3928. int i;
  3929. if(h->sps.poc_cycle_length != 0)
  3930. abs_frame_num = h->frame_num_offset + h->frame_num;
  3931. else
  3932. abs_frame_num = 0;
  3933. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  3934. abs_frame_num--;
  3935. expected_delta_per_poc_cycle = 0;
  3936. for(i=0; i < h->sps.poc_cycle_length; i++)
  3937. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  3938. if(abs_frame_num > 0){
  3939. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  3940. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  3941. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  3942. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  3943. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  3944. } else
  3945. expectedpoc = 0;
  3946. if(h->nal_ref_idc == 0)
  3947. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  3948. field_poc[0] = expectedpoc + h->delta_poc[0];
  3949. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  3950. if(s->picture_structure == PICT_FRAME)
  3951. field_poc[1] += h->delta_poc[1];
  3952. }else{
  3953. int poc;
  3954. if(h->nal_unit_type == NAL_IDR_SLICE){
  3955. poc= 0;
  3956. }else{
  3957. if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
  3958. else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
  3959. }
  3960. field_poc[0]= poc;
  3961. field_poc[1]= poc;
  3962. }
  3963. if(s->picture_structure != PICT_BOTTOM_FIELD)
  3964. s->current_picture_ptr->field_poc[0]= field_poc[0];
  3965. if(s->picture_structure != PICT_TOP_FIELD)
  3966. s->current_picture_ptr->field_poc[1]= field_poc[1];
  3967. if(s->picture_structure == PICT_FRAME) // FIXME field pix?
  3968. s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]);
  3969. return 0;
  3970. }
  3971. /**
  3972. * decodes a slice header.
  3973. * this will allso call MPV_common_init() and frame_start() as needed
  3974. */
  3975. static int decode_slice_header(H264Context *h){
  3976. MpegEncContext * const s = &h->s;
  3977. int first_mb_in_slice;
  3978. unsigned int pps_id;
  3979. int num_ref_idx_active_override_flag;
  3980. static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
  3981. int slice_type;
  3982. int default_ref_list_done = 0;
  3983. s->current_picture.reference= h->nal_ref_idc != 0;
  3984. s->dropable= h->nal_ref_idc == 0;
  3985. first_mb_in_slice= get_ue_golomb(&s->gb);
  3986. slice_type= get_ue_golomb(&s->gb);
  3987. if(slice_type > 9){
  3988. 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);
  3989. return -1;
  3990. }
  3991. if(slice_type > 4){
  3992. slice_type -= 5;
  3993. h->slice_type_fixed=1;
  3994. }else
  3995. h->slice_type_fixed=0;
  3996. slice_type= slice_type_map[ slice_type ];
  3997. if (slice_type == I_TYPE
  3998. || (h->slice_num != 0 && slice_type == h->slice_type) ) {
  3999. default_ref_list_done = 1;
  4000. }
  4001. h->slice_type= slice_type;
  4002. s->pict_type= h->slice_type; // to make a few old func happy, it's wrong though
  4003. pps_id= get_ue_golomb(&s->gb);
  4004. if(pps_id>=MAX_PPS_COUNT){
  4005. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  4006. return -1;
  4007. }
  4008. h->pps= h->pps_buffer[pps_id];
  4009. if(h->pps.slice_group_count == 0){
  4010. av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referenced\n");
  4011. return -1;
  4012. }
  4013. h->sps= h->sps_buffer[ h->pps.sps_id ];
  4014. if(h->sps.log2_max_frame_num == 0){
  4015. av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referenced\n");
  4016. return -1;
  4017. }
  4018. if(h->dequant_coeff_pps != (int)pps_id){
  4019. h->dequant_coeff_pps = (int)pps_id;
  4020. init_dequant_tables(h);
  4021. }
  4022. s->mb_width= h->sps.mb_width;
  4023. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  4024. h->b_stride= s->mb_width*4;
  4025. h->b8_stride= s->mb_width*2;
  4026. s->width = 16*s->mb_width - 2*(h->sps.crop_left + h->sps.crop_right );
  4027. if(h->sps.frame_mbs_only_flag)
  4028. s->height= 16*s->mb_height - 2*(h->sps.crop_top + h->sps.crop_bottom);
  4029. else
  4030. s->height= 16*s->mb_height - 4*(h->sps.crop_top + h->sps.crop_bottom); //FIXME recheck
  4031. if (s->context_initialized
  4032. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  4033. free_tables(h);
  4034. MPV_common_end(s);
  4035. }
  4036. if (!s->context_initialized) {
  4037. if (MPV_common_init(s) < 0)
  4038. return -1;
  4039. if(s->dsp.h264_idct_add == ff_h264_idct_add_c){ //FIXME little ugly
  4040. memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
  4041. memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
  4042. }else{
  4043. int i;
  4044. for(i=0; i<16; i++){
  4045. #define T(x) (x>>2) | ((x<<2) & 0xF)
  4046. h->zigzag_scan[i] = T(zigzag_scan[i]);
  4047. h-> field_scan[i] = T( field_scan[i]);
  4048. #undef T
  4049. }
  4050. }
  4051. if(s->dsp.h264_idct8_add == ff_h264_idct8_add_c){
  4052. memcpy(h->zigzag_scan8x8, zigzag_scan8x8, 64*sizeof(uint8_t));
  4053. memcpy(h->zigzag_scan8x8_cavlc, zigzag_scan8x8_cavlc, 64*sizeof(uint8_t));
  4054. memcpy(h->field_scan8x8, field_scan8x8, 64*sizeof(uint8_t));
  4055. memcpy(h->field_scan8x8_cavlc, field_scan8x8_cavlc, 64*sizeof(uint8_t));
  4056. }else{
  4057. int i;
  4058. for(i=0; i<64; i++){
  4059. #define T(x) (x>>3) | ((x&7)<<3)
  4060. h->zigzag_scan8x8[i] = T(zigzag_scan8x8[i]);
  4061. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  4062. h->field_scan8x8[i] = T(field_scan8x8[i]);
  4063. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  4064. #undef T
  4065. }
  4066. }
  4067. if(h->sps.transform_bypass){ //FIXME same ugly
  4068. h->zigzag_scan_q0 = zigzag_scan;
  4069. h->zigzag_scan8x8_q0 = zigzag_scan8x8;
  4070. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  4071. h->field_scan_q0 = field_scan;
  4072. h->field_scan8x8_q0 = field_scan8x8;
  4073. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  4074. }else{
  4075. h->zigzag_scan_q0 = h->zigzag_scan;
  4076. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  4077. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  4078. h->field_scan_q0 = h->field_scan;
  4079. h->field_scan8x8_q0 = h->field_scan8x8;
  4080. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  4081. }
  4082. alloc_tables(h);
  4083. s->avctx->width = s->width;
  4084. s->avctx->height = s->height;
  4085. s->avctx->sample_aspect_ratio= h->sps.sar;
  4086. if(!s->avctx->sample_aspect_ratio.den)
  4087. s->avctx->sample_aspect_ratio.den = 1;
  4088. if(h->sps.timing_info_present_flag){
  4089. s->avctx->time_base= (AVRational){h->sps.num_units_in_tick * 2, h->sps.time_scale};
  4090. if(h->x264_build > 0 && h->x264_build < 44)
  4091. s->avctx->time_base.den *= 2;
  4092. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  4093. s->avctx->time_base.num, s->avctx->time_base.den, 1<<30);
  4094. }
  4095. }
  4096. if(h->slice_num == 0){
  4097. if(frame_start(h) < 0)
  4098. return -1;
  4099. }
  4100. s->current_picture_ptr->frame_num= //FIXME frame_num cleanup
  4101. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  4102. h->mb_mbaff = 0;
  4103. h->mb_aff_frame = 0;
  4104. if(h->sps.frame_mbs_only_flag){
  4105. s->picture_structure= PICT_FRAME;
  4106. }else{
  4107. if(get_bits1(&s->gb)) { //field_pic_flag
  4108. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  4109. av_log(h->s.avctx, AV_LOG_ERROR, "PAFF interlacing is not implemented\n");
  4110. } else {
  4111. s->picture_structure= PICT_FRAME;
  4112. h->mb_aff_frame = h->sps.mb_aff;
  4113. }
  4114. }
  4115. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  4116. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << h->mb_aff_frame;
  4117. if(s->mb_y >= s->mb_height){
  4118. return -1;
  4119. }
  4120. if(s->picture_structure==PICT_FRAME){
  4121. h->curr_pic_num= h->frame_num;
  4122. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  4123. }else{
  4124. h->curr_pic_num= 2*h->frame_num;
  4125. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  4126. }
  4127. if(h->nal_unit_type == NAL_IDR_SLICE){
  4128. get_ue_golomb(&s->gb); /* idr_pic_id */
  4129. }
  4130. if(h->sps.poc_type==0){
  4131. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  4132. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  4133. h->delta_poc_bottom= get_se_golomb(&s->gb);
  4134. }
  4135. }
  4136. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  4137. h->delta_poc[0]= get_se_golomb(&s->gb);
  4138. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  4139. h->delta_poc[1]= get_se_golomb(&s->gb);
  4140. }
  4141. init_poc(h);
  4142. if(h->pps.redundant_pic_cnt_present){
  4143. h->redundant_pic_count= get_ue_golomb(&s->gb);
  4144. }
  4145. //set defaults, might be overriden a few line later
  4146. h->ref_count[0]= h->pps.ref_count[0];
  4147. h->ref_count[1]= h->pps.ref_count[1];
  4148. if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){
  4149. if(h->slice_type == B_TYPE){
  4150. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  4151. if(h->sps.mb_aff && h->direct_spatial_mv_pred)
  4152. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + spatial direct mode is not implemented\n");
  4153. }
  4154. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  4155. if(num_ref_idx_active_override_flag){
  4156. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  4157. if(h->slice_type==B_TYPE)
  4158. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  4159. if(h->ref_count[0] > 32 || h->ref_count[1] > 32){
  4160. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  4161. return -1;
  4162. }
  4163. }
  4164. }
  4165. if(!default_ref_list_done){
  4166. fill_default_ref_list(h);
  4167. }
  4168. if(decode_ref_pic_list_reordering(h) < 0)
  4169. return -1;
  4170. if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
  4171. || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
  4172. pred_weight_table(h);
  4173. else if(h->pps.weighted_bipred_idc==2 && h->slice_type==B_TYPE)
  4174. implicit_weight_table(h);
  4175. else
  4176. h->use_weight = 0;
  4177. if(s->current_picture.reference)
  4178. decode_ref_pic_marking(h);
  4179. if(FRAME_MBAFF)
  4180. fill_mbaff_ref_list(h);
  4181. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac )
  4182. h->cabac_init_idc = get_ue_golomb(&s->gb);
  4183. h->last_qscale_diff = 0;
  4184. s->qscale = h->pps.init_qp + get_se_golomb(&s->gb);
  4185. if(s->qscale<0 || s->qscale>51){
  4186. av_log(s->avctx, AV_LOG_ERROR, "QP %d out of range\n", s->qscale);
  4187. return -1;
  4188. }
  4189. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4190. //FIXME qscale / qp ... stuff
  4191. if(h->slice_type == SP_TYPE){
  4192. get_bits1(&s->gb); /* sp_for_switch_flag */
  4193. }
  4194. if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
  4195. get_se_golomb(&s->gb); /* slice_qs_delta */
  4196. }
  4197. h->deblocking_filter = 1;
  4198. h->slice_alpha_c0_offset = 0;
  4199. h->slice_beta_offset = 0;
  4200. if( h->pps.deblocking_filter_parameters_present ) {
  4201. h->deblocking_filter= get_ue_golomb(&s->gb);
  4202. if(h->deblocking_filter < 2)
  4203. h->deblocking_filter^= 1; // 1<->0
  4204. if( h->deblocking_filter ) {
  4205. h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
  4206. h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
  4207. }
  4208. }
  4209. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  4210. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type != I_TYPE)
  4211. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type == B_TYPE)
  4212. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  4213. h->deblocking_filter= 0;
  4214. #if 0 //FMO
  4215. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  4216. slice_group_change_cycle= get_bits(&s->gb, ?);
  4217. #endif
  4218. h->slice_num++;
  4219. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
  4220. h->emu_edge_height= FRAME_MBAFF ? 0 : h->emu_edge_width;
  4221. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  4222. av_log(h->s.avctx, AV_LOG_DEBUG, "slice:%d %s mb:%d %c pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s\n",
  4223. h->slice_num,
  4224. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  4225. first_mb_in_slice,
  4226. av_get_pict_type_char(h->slice_type),
  4227. pps_id, h->frame_num,
  4228. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  4229. h->ref_count[0], h->ref_count[1],
  4230. s->qscale,
  4231. h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2,
  4232. h->use_weight,
  4233. h->use_weight==1 && h->use_weight_chroma ? "c" : ""
  4234. );
  4235. }
  4236. if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !s->current_picture.reference){
  4237. s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
  4238. s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
  4239. }else{
  4240. s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
  4241. s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
  4242. }
  4243. return 0;
  4244. }
  4245. /**
  4246. *
  4247. */
  4248. static inline int get_level_prefix(GetBitContext *gb){
  4249. unsigned int buf;
  4250. int log;
  4251. OPEN_READER(re, gb);
  4252. UPDATE_CACHE(re, gb);
  4253. buf=GET_CACHE(re, gb);
  4254. log= 32 - av_log2(buf);
  4255. #ifdef TRACE
  4256. print_bin(buf>>(32-log), log);
  4257. 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__);
  4258. #endif
  4259. LAST_SKIP_BITS(re, gb, log);
  4260. CLOSE_READER(re, gb);
  4261. return log-1;
  4262. }
  4263. static inline int get_dct8x8_allowed(H264Context *h){
  4264. int i;
  4265. for(i=0; i<4; i++){
  4266. if(!IS_SUB_8X8(h->sub_mb_type[i])
  4267. || (!h->sps.direct_8x8_inference_flag && IS_DIRECT(h->sub_mb_type[i])))
  4268. return 0;
  4269. }
  4270. return 1;
  4271. }
  4272. /**
  4273. * decodes a residual block.
  4274. * @param n block index
  4275. * @param scantable scantable
  4276. * @param max_coeff number of coefficients in the block
  4277. * @return <0 if an error occured
  4278. */
  4279. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
  4280. MpegEncContext * const s = &h->s;
  4281. 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};
  4282. int level[16];
  4283. int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
  4284. //FIXME put trailing_onex into the context
  4285. if(n == CHROMA_DC_BLOCK_INDEX){
  4286. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  4287. total_coeff= coeff_token>>2;
  4288. }else{
  4289. if(n == LUMA_DC_BLOCK_INDEX){
  4290. total_coeff= pred_non_zero_count(h, 0);
  4291. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4292. total_coeff= coeff_token>>2;
  4293. }else{
  4294. total_coeff= pred_non_zero_count(h, n);
  4295. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4296. total_coeff= coeff_token>>2;
  4297. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  4298. }
  4299. }
  4300. //FIXME set last_non_zero?
  4301. if(total_coeff==0)
  4302. return 0;
  4303. if(total_coeff<0) {
  4304. av_log(h->s.avctx, AV_LOG_ERROR, "corrupted macroblock %d %d (total_coeff<0)\n", s->mb_x, s->mb_y);
  4305. return -1;
  4306. }
  4307. trailing_ones= coeff_token&3;
  4308. tprintf("trailing:%d, total:%d\n", trailing_ones, total_coeff);
  4309. assert(total_coeff<=16);
  4310. for(i=0; i<trailing_ones; i++){
  4311. level[i]= 1 - 2*get_bits1(gb);
  4312. }
  4313. if(i<total_coeff) {
  4314. int level_code, mask;
  4315. int suffix_length = total_coeff > 10 && trailing_ones < 3;
  4316. int prefix= get_level_prefix(gb);
  4317. //first coefficient has suffix_length equal to 0 or 1
  4318. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  4319. if(suffix_length)
  4320. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4321. else
  4322. level_code= (prefix<<suffix_length); //part
  4323. }else if(prefix==14){
  4324. if(suffix_length)
  4325. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4326. else
  4327. level_code= prefix + get_bits(gb, 4); //part
  4328. }else if(prefix==15){
  4329. level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part
  4330. if(suffix_length==0) level_code+=15; //FIXME doesn't make (much)sense
  4331. }else{
  4332. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4333. return -1;
  4334. }
  4335. if(trailing_ones < 3) level_code += 2;
  4336. suffix_length = 1;
  4337. if(level_code > 5)
  4338. suffix_length++;
  4339. mask= -(level_code&1);
  4340. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4341. i++;
  4342. //remaining coefficients have suffix_length > 0
  4343. for(;i<total_coeff;i++) {
  4344. static const int suffix_limit[7] = {0,5,11,23,47,95,INT_MAX };
  4345. prefix = get_level_prefix(gb);
  4346. if(prefix<15){
  4347. level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
  4348. }else if(prefix==15){
  4349. level_code = (prefix<<suffix_length) + get_bits(gb, 12);
  4350. }else{
  4351. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4352. return -1;
  4353. }
  4354. mask= -(level_code&1);
  4355. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4356. if(level_code > suffix_limit[suffix_length])
  4357. suffix_length++;
  4358. }
  4359. }
  4360. if(total_coeff == max_coeff)
  4361. zeros_left=0;
  4362. else{
  4363. if(n == CHROMA_DC_BLOCK_INDEX)
  4364. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  4365. else
  4366. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  4367. }
  4368. coeff_num = zeros_left + total_coeff - 1;
  4369. j = scantable[coeff_num];
  4370. if(n > 24){
  4371. block[j] = level[0];
  4372. for(i=1;i<total_coeff;i++) {
  4373. if(zeros_left <= 0)
  4374. run_before = 0;
  4375. else if(zeros_left < 7){
  4376. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4377. }else{
  4378. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4379. }
  4380. zeros_left -= run_before;
  4381. coeff_num -= 1 + run_before;
  4382. j= scantable[ coeff_num ];
  4383. block[j]= level[i];
  4384. }
  4385. }else{
  4386. block[j] = (level[0] * qmul[j] + 32)>>6;
  4387. for(i=1;i<total_coeff;i++) {
  4388. if(zeros_left <= 0)
  4389. run_before = 0;
  4390. else if(zeros_left < 7){
  4391. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4392. }else{
  4393. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4394. }
  4395. zeros_left -= run_before;
  4396. coeff_num -= 1 + run_before;
  4397. j= scantable[ coeff_num ];
  4398. block[j]= (level[i] * qmul[j] + 32)>>6;
  4399. }
  4400. }
  4401. if(zeros_left<0){
  4402. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  4403. return -1;
  4404. }
  4405. return 0;
  4406. }
  4407. static void predict_field_decoding_flag(H264Context *h){
  4408. MpegEncContext * const s = &h->s;
  4409. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4410. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  4411. ? s->current_picture.mb_type[mb_xy-1]
  4412. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  4413. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  4414. : 0;
  4415. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  4416. }
  4417. /**
  4418. * decodes a P_SKIP or B_SKIP macroblock
  4419. */
  4420. static void decode_mb_skip(H264Context *h){
  4421. MpegEncContext * const s = &h->s;
  4422. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4423. int mb_type=0;
  4424. memset(h->non_zero_count[mb_xy], 0, 16);
  4425. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  4426. if(MB_FIELD)
  4427. mb_type|= MB_TYPE_INTERLACED;
  4428. if( h->slice_type == B_TYPE )
  4429. {
  4430. // just for fill_caches. pred_direct_motion will set the real mb_type
  4431. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
  4432. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4433. pred_direct_motion(h, &mb_type);
  4434. mb_type|= MB_TYPE_SKIP;
  4435. }
  4436. else
  4437. {
  4438. int mx, my;
  4439. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
  4440. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4441. pred_pskip_motion(h, &mx, &my);
  4442. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  4443. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  4444. }
  4445. write_back_motion(h, mb_type);
  4446. s->current_picture.mb_type[mb_xy]= mb_type;
  4447. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4448. h->slice_table[ mb_xy ]= h->slice_num;
  4449. h->prev_mb_skipped= 1;
  4450. }
  4451. /**
  4452. * decodes a macroblock
  4453. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  4454. */
  4455. static int decode_mb_cavlc(H264Context *h){
  4456. MpegEncContext * const s = &h->s;
  4457. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4458. int mb_type, partition_count, cbp;
  4459. int dct8x8_allowed= h->pps.transform_8x8_mode;
  4460. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?
  4461. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  4462. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  4463. down the code */
  4464. if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){
  4465. if(s->mb_skip_run==-1)
  4466. s->mb_skip_run= get_ue_golomb(&s->gb);
  4467. if (s->mb_skip_run--) {
  4468. if(FRAME_MBAFF && (s->mb_y&1) == 0){
  4469. if(s->mb_skip_run==0)
  4470. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4471. else
  4472. predict_field_decoding_flag(h);
  4473. }
  4474. decode_mb_skip(h);
  4475. return 0;
  4476. }
  4477. }
  4478. if(FRAME_MBAFF){
  4479. if( (s->mb_y&1) == 0 )
  4480. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4481. }else
  4482. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  4483. h->prev_mb_skipped= 0;
  4484. mb_type= get_ue_golomb(&s->gb);
  4485. if(h->slice_type == B_TYPE){
  4486. if(mb_type < 23){
  4487. partition_count= b_mb_type_info[mb_type].partition_count;
  4488. mb_type= b_mb_type_info[mb_type].type;
  4489. }else{
  4490. mb_type -= 23;
  4491. goto decode_intra_mb;
  4492. }
  4493. }else if(h->slice_type == P_TYPE /*|| h->slice_type == SP_TYPE */){
  4494. if(mb_type < 5){
  4495. partition_count= p_mb_type_info[mb_type].partition_count;
  4496. mb_type= p_mb_type_info[mb_type].type;
  4497. }else{
  4498. mb_type -= 5;
  4499. goto decode_intra_mb;
  4500. }
  4501. }else{
  4502. assert(h->slice_type == I_TYPE);
  4503. decode_intra_mb:
  4504. if(mb_type > 25){
  4505. 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);
  4506. return -1;
  4507. }
  4508. partition_count=0;
  4509. cbp= i_mb_type_info[mb_type].cbp;
  4510. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  4511. mb_type= i_mb_type_info[mb_type].type;
  4512. }
  4513. if(MB_FIELD)
  4514. mb_type |= MB_TYPE_INTERLACED;
  4515. h->slice_table[ mb_xy ]= h->slice_num;
  4516. if(IS_INTRA_PCM(mb_type)){
  4517. unsigned int x, y;
  4518. // we assume these blocks are very rare so we dont optimize it
  4519. align_get_bits(&s->gb);
  4520. // The pixels are stored in the same order as levels in h->mb array.
  4521. for(y=0; y<16; y++){
  4522. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  4523. for(x=0; x<16; x++){
  4524. tprintf("LUMA ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4525. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= get_bits(&s->gb, 8);
  4526. }
  4527. }
  4528. for(y=0; y<8; y++){
  4529. const int index= 256 + 4*(y&3) + 32*(y>>2);
  4530. for(x=0; x<8; x++){
  4531. tprintf("CHROMA U ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4532. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4533. }
  4534. }
  4535. for(y=0; y<8; y++){
  4536. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  4537. for(x=0; x<8; x++){
  4538. tprintf("CHROMA V ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4539. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4540. }
  4541. }
  4542. // In deblocking, the quantizer is 0
  4543. s->current_picture.qscale_table[mb_xy]= 0;
  4544. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  4545. // All coeffs are present
  4546. memset(h->non_zero_count[mb_xy], 16, 16);
  4547. s->current_picture.mb_type[mb_xy]= mb_type;
  4548. return 0;
  4549. }
  4550. if(MB_MBAFF){
  4551. h->ref_count[0] <<= 1;
  4552. h->ref_count[1] <<= 1;
  4553. }
  4554. fill_caches(h, mb_type, 0);
  4555. //mb_pred
  4556. if(IS_INTRA(mb_type)){
  4557. // init_top_left_availability(h);
  4558. if(IS_INTRA4x4(mb_type)){
  4559. int i;
  4560. int di = 1;
  4561. if(dct8x8_allowed && get_bits1(&s->gb)){
  4562. mb_type |= MB_TYPE_8x8DCT;
  4563. di = 4;
  4564. }
  4565. // fill_intra4x4_pred_table(h);
  4566. for(i=0; i<16; i+=di){
  4567. int mode= pred_intra_mode(h, i);
  4568. if(!get_bits1(&s->gb)){
  4569. const int rem_mode= get_bits(&s->gb, 3);
  4570. mode = rem_mode + (rem_mode >= mode);
  4571. }
  4572. if(di==4)
  4573. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  4574. else
  4575. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  4576. }
  4577. write_back_intra_pred_mode(h);
  4578. if( check_intra4x4_pred_mode(h) < 0)
  4579. return -1;
  4580. }else{
  4581. h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
  4582. if(h->intra16x16_pred_mode < 0)
  4583. return -1;
  4584. }
  4585. h->chroma_pred_mode= get_ue_golomb(&s->gb);
  4586. h->chroma_pred_mode= check_intra_pred_mode(h, h->chroma_pred_mode);
  4587. if(h->chroma_pred_mode < 0)
  4588. return -1;
  4589. }else if(partition_count==4){
  4590. int i, j, sub_partition_count[4], list, ref[2][4];
  4591. if(h->slice_type == B_TYPE){
  4592. for(i=0; i<4; i++){
  4593. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4594. if(h->sub_mb_type[i] >=13){
  4595. 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);
  4596. return -1;
  4597. }
  4598. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4599. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4600. }
  4601. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  4602. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
  4603. pred_direct_motion(h, &mb_type);
  4604. h->ref_cache[0][scan8[4]] =
  4605. h->ref_cache[1][scan8[4]] =
  4606. h->ref_cache[0][scan8[12]] =
  4607. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  4608. }
  4609. }else{
  4610. assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE); //FIXME SP correct ?
  4611. for(i=0; i<4; i++){
  4612. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4613. if(h->sub_mb_type[i] >=4){
  4614. 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);
  4615. return -1;
  4616. }
  4617. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4618. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4619. }
  4620. }
  4621. for(list=0; list<2; list++){
  4622. int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4623. if(ref_count == 0) continue;
  4624. for(i=0; i<4; i++){
  4625. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  4626. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4627. ref[list][i] = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
  4628. }else{
  4629. //FIXME
  4630. ref[list][i] = -1;
  4631. }
  4632. }
  4633. }
  4634. if(dct8x8_allowed)
  4635. dct8x8_allowed = get_dct8x8_allowed(h);
  4636. for(list=0; list<2; list++){
  4637. const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4638. if(ref_count == 0) continue;
  4639. for(i=0; i<4; i++){
  4640. if(IS_DIRECT(h->sub_mb_type[i])) {
  4641. h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];
  4642. continue;
  4643. }
  4644. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  4645. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  4646. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4647. const int sub_mb_type= h->sub_mb_type[i];
  4648. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  4649. for(j=0; j<sub_partition_count[i]; j++){
  4650. int mx, my;
  4651. const int index= 4*i + block_width*j;
  4652. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  4653. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  4654. mx += get_se_golomb(&s->gb);
  4655. my += get_se_golomb(&s->gb);
  4656. tprintf("final mv:%d %d\n", mx, my);
  4657. if(IS_SUB_8X8(sub_mb_type)){
  4658. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  4659. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  4660. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  4661. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  4662. }else if(IS_SUB_8X4(sub_mb_type)){
  4663. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  4664. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  4665. }else if(IS_SUB_4X8(sub_mb_type)){
  4666. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  4667. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  4668. }else{
  4669. assert(IS_SUB_4X4(sub_mb_type));
  4670. mv_cache[ 0 ][0]= mx;
  4671. mv_cache[ 0 ][1]= my;
  4672. }
  4673. }
  4674. }else{
  4675. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  4676. p[0] = p[1]=
  4677. p[8] = p[9]= 0;
  4678. }
  4679. }
  4680. }
  4681. }else if(IS_DIRECT(mb_type)){
  4682. pred_direct_motion(h, &mb_type);
  4683. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  4684. }else{
  4685. int list, mx, my, i;
  4686. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  4687. if(IS_16X16(mb_type)){
  4688. for(list=0; list<2; list++){
  4689. if(h->ref_count[list]>0){
  4690. if(IS_DIR(mb_type, 0, list)){
  4691. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4692. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  4693. }else
  4694. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4695. }
  4696. }
  4697. for(list=0; list<2; list++){
  4698. if(IS_DIR(mb_type, 0, list)){
  4699. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  4700. mx += get_se_golomb(&s->gb);
  4701. my += get_se_golomb(&s->gb);
  4702. tprintf("final mv:%d %d\n", mx, my);
  4703. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  4704. }else
  4705. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  4706. }
  4707. }
  4708. else if(IS_16X8(mb_type)){
  4709. for(list=0; list<2; list++){
  4710. if(h->ref_count[list]>0){
  4711. for(i=0; i<2; i++){
  4712. if(IS_DIR(mb_type, i, list)){
  4713. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4714. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  4715. }else
  4716. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  4717. }
  4718. }
  4719. }
  4720. for(list=0; list<2; list++){
  4721. for(i=0; i<2; i++){
  4722. if(IS_DIR(mb_type, i, list)){
  4723. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  4724. mx += get_se_golomb(&s->gb);
  4725. my += get_se_golomb(&s->gb);
  4726. tprintf("final mv:%d %d\n", mx, my);
  4727. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  4728. }else
  4729. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  4730. }
  4731. }
  4732. }else{
  4733. assert(IS_8X16(mb_type));
  4734. for(list=0; list<2; list++){
  4735. if(h->ref_count[list]>0){
  4736. for(i=0; i<2; i++){
  4737. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  4738. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4739. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  4740. }else
  4741. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4742. }
  4743. }
  4744. }
  4745. for(list=0; list<2; list++){
  4746. for(i=0; i<2; i++){
  4747. if(IS_DIR(mb_type, i, list)){
  4748. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  4749. mx += get_se_golomb(&s->gb);
  4750. my += get_se_golomb(&s->gb);
  4751. tprintf("final mv:%d %d\n", mx, my);
  4752. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  4753. }else
  4754. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  4755. }
  4756. }
  4757. }
  4758. }
  4759. if(IS_INTER(mb_type))
  4760. write_back_motion(h, mb_type);
  4761. if(!IS_INTRA16x16(mb_type)){
  4762. cbp= get_ue_golomb(&s->gb);
  4763. if(cbp > 47){
  4764. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%d) at %d %d\n", cbp, s->mb_x, s->mb_y);
  4765. return -1;
  4766. }
  4767. if(IS_INTRA4x4(mb_type))
  4768. cbp= golomb_to_intra4x4_cbp[cbp];
  4769. else
  4770. cbp= golomb_to_inter_cbp[cbp];
  4771. }
  4772. h->cbp = cbp;
  4773. if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
  4774. if(get_bits1(&s->gb))
  4775. mb_type |= MB_TYPE_8x8DCT;
  4776. }
  4777. s->current_picture.mb_type[mb_xy]= mb_type;
  4778. if(cbp || IS_INTRA16x16(mb_type)){
  4779. int i8x8, i4x4, chroma_idx;
  4780. int chroma_qp, dquant;
  4781. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  4782. const uint8_t *scan, *scan8x8, *dc_scan;
  4783. // fill_non_zero_count_cache(h);
  4784. if(IS_INTERLACED(mb_type)){
  4785. scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
  4786. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  4787. dc_scan= luma_dc_field_scan;
  4788. }else{
  4789. scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
  4790. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  4791. dc_scan= luma_dc_zigzag_scan;
  4792. }
  4793. dquant= get_se_golomb(&s->gb);
  4794. if( dquant > 25 || dquant < -26 ){
  4795. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  4796. return -1;
  4797. }
  4798. s->qscale += dquant;
  4799. if(((unsigned)s->qscale) > 51){
  4800. if(s->qscale<0) s->qscale+= 52;
  4801. else s->qscale-= 52;
  4802. }
  4803. h->chroma_qp= chroma_qp= get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4804. if(IS_INTRA16x16(mb_type)){
  4805. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){
  4806. return -1; //FIXME continue if partitioned and other return -1 too
  4807. }
  4808. assert((cbp&15) == 0 || (cbp&15) == 15);
  4809. if(cbp&15){
  4810. for(i8x8=0; i8x8<4; i8x8++){
  4811. for(i4x4=0; i4x4<4; i4x4++){
  4812. const int index= i4x4 + 4*i8x8;
  4813. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){
  4814. return -1;
  4815. }
  4816. }
  4817. }
  4818. }else{
  4819. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  4820. }
  4821. }else{
  4822. for(i8x8=0; i8x8<4; i8x8++){
  4823. if(cbp & (1<<i8x8)){
  4824. if(IS_8x8DCT(mb_type)){
  4825. DCTELEM *buf = &h->mb[64*i8x8];
  4826. uint8_t *nnz;
  4827. for(i4x4=0; i4x4<4; i4x4++){
  4828. if( decode_residual(h, gb, buf, i4x4+4*i8x8, scan8x8+16*i4x4,
  4829. h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )
  4830. return -1;
  4831. }
  4832. nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4833. nnz[0] += nnz[1] + nnz[8] + nnz[9];
  4834. }else{
  4835. for(i4x4=0; i4x4<4; i4x4++){
  4836. const int index= i4x4 + 4*i8x8;
  4837. if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){
  4838. return -1;
  4839. }
  4840. }
  4841. }
  4842. }else{
  4843. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4844. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  4845. }
  4846. }
  4847. }
  4848. if(cbp&0x30){
  4849. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  4850. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){
  4851. return -1;
  4852. }
  4853. }
  4854. if(cbp&0x20){
  4855. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  4856. for(i4x4=0; i4x4<4; i4x4++){
  4857. const int index= 16 + 4*chroma_idx + i4x4;
  4858. 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){
  4859. return -1;
  4860. }
  4861. }
  4862. }
  4863. }else{
  4864. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4865. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4866. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4867. }
  4868. }else{
  4869. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4870. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  4871. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4872. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4873. }
  4874. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4875. write_back_non_zero_count(h);
  4876. if(MB_MBAFF){
  4877. h->ref_count[0] >>= 1;
  4878. h->ref_count[1] >>= 1;
  4879. }
  4880. return 0;
  4881. }
  4882. static int decode_cabac_field_decoding_flag(H264Context *h) {
  4883. MpegEncContext * const s = &h->s;
  4884. const int mb_x = s->mb_x;
  4885. const int mb_y = s->mb_y & ~1;
  4886. const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
  4887. const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
  4888. unsigned int ctx = 0;
  4889. if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
  4890. ctx += 1;
  4891. }
  4892. if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
  4893. ctx += 1;
  4894. }
  4895. return get_cabac_noinline( &h->cabac, &h->cabac_state[70 + ctx] );
  4896. }
  4897. static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
  4898. uint8_t *state= &h->cabac_state[ctx_base];
  4899. int mb_type;
  4900. if(intra_slice){
  4901. MpegEncContext * const s = &h->s;
  4902. const int mba_xy = h->left_mb_xy[0];
  4903. const int mbb_xy = h->top_mb_xy;
  4904. int ctx=0;
  4905. if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
  4906. ctx++;
  4907. if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
  4908. ctx++;
  4909. if( get_cabac_noinline( &h->cabac, &state[ctx] ) == 0 )
  4910. return 0; /* I4x4 */
  4911. state += 2;
  4912. }else{
  4913. if( get_cabac_noinline( &h->cabac, &state[0] ) == 0 )
  4914. return 0; /* I4x4 */
  4915. }
  4916. if( get_cabac_terminate( &h->cabac ) )
  4917. return 25; /* PCM */
  4918. mb_type = 1; /* I16x16 */
  4919. mb_type += 12 * get_cabac_noinline( &h->cabac, &state[1] ); /* cbp_luma != 0 */
  4920. if( get_cabac_noinline( &h->cabac, &state[2] ) ) /* cbp_chroma */
  4921. mb_type += 4 + 4 * get_cabac_noinline( &h->cabac, &state[2+intra_slice] );
  4922. mb_type += 2 * get_cabac_noinline( &h->cabac, &state[3+intra_slice] );
  4923. mb_type += 1 * get_cabac_noinline( &h->cabac, &state[3+2*intra_slice] );
  4924. return mb_type;
  4925. }
  4926. static int decode_cabac_mb_type( H264Context *h ) {
  4927. MpegEncContext * const s = &h->s;
  4928. if( h->slice_type == I_TYPE ) {
  4929. return decode_cabac_intra_mb_type(h, 3, 1);
  4930. } else if( h->slice_type == P_TYPE ) {
  4931. if( get_cabac_noinline( &h->cabac, &h->cabac_state[14] ) == 0 ) {
  4932. /* P-type */
  4933. if( get_cabac_noinline( &h->cabac, &h->cabac_state[15] ) == 0 ) {
  4934. /* P_L0_D16x16, P_8x8 */
  4935. return 3 * get_cabac_noinline( &h->cabac, &h->cabac_state[16] );
  4936. } else {
  4937. /* P_L0_D8x16, P_L0_D16x8 */
  4938. return 2 - get_cabac_noinline( &h->cabac, &h->cabac_state[17] );
  4939. }
  4940. } else {
  4941. return decode_cabac_intra_mb_type(h, 17, 0) + 5;
  4942. }
  4943. } else if( h->slice_type == B_TYPE ) {
  4944. const int mba_xy = h->left_mb_xy[0];
  4945. const int mbb_xy = h->top_mb_xy;
  4946. int ctx = 0;
  4947. int bits;
  4948. if( h->slice_table[mba_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
  4949. ctx++;
  4950. if( h->slice_table[mbb_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
  4951. ctx++;
  4952. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+ctx] ) )
  4953. return 0; /* B_Direct_16x16 */
  4954. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+3] ) ) {
  4955. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
  4956. }
  4957. bits = get_cabac_noinline( &h->cabac, &h->cabac_state[27+4] ) << 3;
  4958. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 2;
  4959. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 1;
  4960. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  4961. if( bits < 8 )
  4962. return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
  4963. else if( bits == 13 ) {
  4964. return decode_cabac_intra_mb_type(h, 32, 0) + 23;
  4965. } else if( bits == 14 )
  4966. return 11; /* B_L1_L0_8x16 */
  4967. else if( bits == 15 )
  4968. return 22; /* B_8x8 */
  4969. bits= ( bits<<1 ) | get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  4970. return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
  4971. } else {
  4972. /* TODO SI/SP frames? */
  4973. return -1;
  4974. }
  4975. }
  4976. static int decode_cabac_mb_skip( H264Context *h, int mb_x, int mb_y ) {
  4977. MpegEncContext * const s = &h->s;
  4978. int mba_xy, mbb_xy;
  4979. int ctx = 0;
  4980. if(FRAME_MBAFF){ //FIXME merge with the stuff in fill_caches?
  4981. int mb_xy = mb_x + (mb_y&~1)*s->mb_stride;
  4982. mba_xy = mb_xy - 1;
  4983. if( (mb_y&1)
  4984. && h->slice_table[mba_xy] == h->slice_num
  4985. && MB_FIELD == !!IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) )
  4986. mba_xy += s->mb_stride;
  4987. if( MB_FIELD ){
  4988. mbb_xy = mb_xy - s->mb_stride;
  4989. if( !(mb_y&1)
  4990. && h->slice_table[mbb_xy] == h->slice_num
  4991. && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) )
  4992. mbb_xy -= s->mb_stride;
  4993. }else
  4994. mbb_xy = mb_x + (mb_y-1)*s->mb_stride;
  4995. }else{
  4996. int mb_xy = mb_x + mb_y*s->mb_stride;
  4997. mba_xy = mb_xy - 1;
  4998. mbb_xy = mb_xy - s->mb_stride;
  4999. }
  5000. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
  5001. ctx++;
  5002. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
  5003. ctx++;
  5004. if( h->slice_type == B_TYPE )
  5005. ctx += 13;
  5006. return get_cabac_noinline( &h->cabac, &h->cabac_state[11+ctx] );
  5007. }
  5008. static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
  5009. int mode = 0;
  5010. if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
  5011. return pred_mode;
  5012. mode += 1 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5013. mode += 2 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5014. mode += 4 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5015. if( mode >= pred_mode )
  5016. return mode + 1;
  5017. else
  5018. return mode;
  5019. }
  5020. static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
  5021. const int mba_xy = h->left_mb_xy[0];
  5022. const int mbb_xy = h->top_mb_xy;
  5023. int ctx = 0;
  5024. /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
  5025. if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
  5026. ctx++;
  5027. if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
  5028. ctx++;
  5029. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
  5030. return 0;
  5031. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  5032. return 1;
  5033. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  5034. return 2;
  5035. else
  5036. return 3;
  5037. }
  5038. static const uint8_t block_idx_x[16] = {
  5039. 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
  5040. };
  5041. static const uint8_t block_idx_y[16] = {
  5042. 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
  5043. };
  5044. static const uint8_t block_idx_xy[4][4] = {
  5045. { 0, 2, 8, 10},
  5046. { 1, 3, 9, 11},
  5047. { 4, 6, 12, 14},
  5048. { 5, 7, 13, 15}
  5049. };
  5050. static int decode_cabac_mb_cbp_luma( H264Context *h) {
  5051. int cbp = 0;
  5052. int cbp_b = -1;
  5053. int i8x8;
  5054. if( h->slice_table[h->top_mb_xy] == h->slice_num ) {
  5055. cbp_b = h->top_cbp;
  5056. tprintf("cbp_b = top_cbp = %x\n", cbp_b);
  5057. }
  5058. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5059. int cbp_a = -1;
  5060. int x, y;
  5061. int ctx = 0;
  5062. x = block_idx_x[4*i8x8];
  5063. y = block_idx_y[4*i8x8];
  5064. if( x > 0 )
  5065. cbp_a = cbp;
  5066. else if( h->slice_table[h->left_mb_xy[0]] == h->slice_num ) {
  5067. cbp_a = h->left_cbp;
  5068. tprintf("cbp_a = left_cbp = %x\n", cbp_a);
  5069. }
  5070. if( y > 0 )
  5071. cbp_b = cbp;
  5072. /* No need to test for skip as we put 0 for skip block */
  5073. /* No need to test for IPCM as we put 1 for IPCM block */
  5074. if( cbp_a >= 0 ) {
  5075. int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
  5076. if( ((cbp_a >> i8x8a)&0x01) == 0 )
  5077. ctx++;
  5078. }
  5079. if( cbp_b >= 0 ) {
  5080. int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
  5081. if( ((cbp_b >> i8x8b)&0x01) == 0 )
  5082. ctx += 2;
  5083. }
  5084. if( get_cabac( &h->cabac, &h->cabac_state[73 + ctx] ) ) {
  5085. cbp |= 1 << i8x8;
  5086. }
  5087. }
  5088. return cbp;
  5089. }
  5090. static int decode_cabac_mb_cbp_chroma( H264Context *h) {
  5091. int ctx;
  5092. int cbp_a, cbp_b;
  5093. cbp_a = (h->left_cbp>>4)&0x03;
  5094. cbp_b = (h-> top_cbp>>4)&0x03;
  5095. ctx = 0;
  5096. if( cbp_a > 0 ) ctx++;
  5097. if( cbp_b > 0 ) ctx += 2;
  5098. if( get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
  5099. return 0;
  5100. ctx = 4;
  5101. if( cbp_a == 2 ) ctx++;
  5102. if( cbp_b == 2 ) ctx += 2;
  5103. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] );
  5104. }
  5105. static int decode_cabac_mb_dqp( H264Context *h) {
  5106. MpegEncContext * const s = &h->s;
  5107. int mbn_xy;
  5108. int ctx = 0;
  5109. int val = 0;
  5110. if( s->mb_x > 0 )
  5111. mbn_xy = s->mb_x + s->mb_y*s->mb_stride - 1;
  5112. else
  5113. mbn_xy = s->mb_width - 1 + (s->mb_y-1)*s->mb_stride;
  5114. if( h->last_qscale_diff != 0 )
  5115. ctx++;
  5116. while( get_cabac_noinline( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
  5117. if( ctx < 2 )
  5118. ctx = 2;
  5119. else
  5120. ctx = 3;
  5121. val++;
  5122. if(val > 102) //prevent infinite loop
  5123. return INT_MIN;
  5124. }
  5125. if( val&0x01 )
  5126. return (val + 1)/2;
  5127. else
  5128. return -(val + 1)/2;
  5129. }
  5130. static int decode_cabac_p_mb_sub_type( H264Context *h ) {
  5131. if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
  5132. return 0; /* 8x8 */
  5133. if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
  5134. return 1; /* 8x4 */
  5135. if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
  5136. return 2; /* 4x8 */
  5137. return 3; /* 4x4 */
  5138. }
  5139. static int decode_cabac_b_mb_sub_type( H264Context *h ) {
  5140. int type;
  5141. if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
  5142. return 0; /* B_Direct_8x8 */
  5143. if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
  5144. return 1 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */
  5145. type = 3;
  5146. if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
  5147. if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
  5148. return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
  5149. type += 4;
  5150. }
  5151. type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
  5152. type += get_cabac( &h->cabac, &h->cabac_state[39] );
  5153. return type;
  5154. }
  5155. static inline int decode_cabac_mb_transform_size( H264Context *h ) {
  5156. return get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
  5157. }
  5158. static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
  5159. int refa = h->ref_cache[list][scan8[n] - 1];
  5160. int refb = h->ref_cache[list][scan8[n] - 8];
  5161. int ref = 0;
  5162. int ctx = 0;
  5163. if( h->slice_type == B_TYPE) {
  5164. if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
  5165. ctx++;
  5166. if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
  5167. ctx += 2;
  5168. } else {
  5169. if( refa > 0 )
  5170. ctx++;
  5171. if( refb > 0 )
  5172. ctx += 2;
  5173. }
  5174. while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
  5175. ref++;
  5176. if( ctx < 4 )
  5177. ctx = 4;
  5178. else
  5179. ctx = 5;
  5180. }
  5181. return ref;
  5182. }
  5183. static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
  5184. int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
  5185. abs( h->mvd_cache[list][scan8[n] - 8][l] );
  5186. int ctxbase = (l == 0) ? 40 : 47;
  5187. int ctx, mvd;
  5188. if( amvd < 3 )
  5189. ctx = 0;
  5190. else if( amvd > 32 )
  5191. ctx = 2;
  5192. else
  5193. ctx = 1;
  5194. if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
  5195. return 0;
  5196. mvd= 1;
  5197. ctx= 3;
  5198. while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
  5199. mvd++;
  5200. if( ctx < 6 )
  5201. ctx++;
  5202. }
  5203. if( mvd >= 9 ) {
  5204. int k = 3;
  5205. while( get_cabac_bypass( &h->cabac ) ) {
  5206. mvd += 1 << k;
  5207. k++;
  5208. }
  5209. while( k-- ) {
  5210. if( get_cabac_bypass( &h->cabac ) )
  5211. mvd += 1 << k;
  5212. }
  5213. }
  5214. return get_cabac_bypass_sign( &h->cabac, -mvd );
  5215. }
  5216. static int inline get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) {
  5217. int nza, nzb;
  5218. int ctx = 0;
  5219. if( cat == 0 ) {
  5220. nza = h->left_cbp&0x100;
  5221. nzb = h-> top_cbp&0x100;
  5222. } else if( cat == 1 || cat == 2 ) {
  5223. nza = h->non_zero_count_cache[scan8[idx] - 1];
  5224. nzb = h->non_zero_count_cache[scan8[idx] - 8];
  5225. } else if( cat == 3 ) {
  5226. nza = (h->left_cbp>>(6+idx))&0x01;
  5227. nzb = (h-> top_cbp>>(6+idx))&0x01;
  5228. } else {
  5229. assert(cat == 4);
  5230. nza = h->non_zero_count_cache[scan8[16+idx] - 1];
  5231. nzb = h->non_zero_count_cache[scan8[16+idx] - 8];
  5232. }
  5233. if( nza > 0 )
  5234. ctx++;
  5235. if( nzb > 0 )
  5236. ctx += 2;
  5237. return ctx + 4 * cat;
  5238. }
  5239. static const __attribute((used)) uint8_t last_coeff_flag_offset_8x8[63] = {
  5240. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5241. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  5242. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  5243. 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
  5244. };
  5245. static int decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff) {
  5246. const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
  5247. static const int significant_coeff_flag_offset[2][6] = {
  5248. { 105+0, 105+15, 105+29, 105+44, 105+47, 402 },
  5249. { 277+0, 277+15, 277+29, 277+44, 277+47, 436 }
  5250. };
  5251. static const int last_coeff_flag_offset[2][6] = {
  5252. { 166+0, 166+15, 166+29, 166+44, 166+47, 417 },
  5253. { 338+0, 338+15, 338+29, 338+44, 338+47, 451 }
  5254. };
  5255. static const int coeff_abs_level_m1_offset[6] = {
  5256. 227+0, 227+10, 227+20, 227+30, 227+39, 426
  5257. };
  5258. static const uint8_t significant_coeff_flag_offset_8x8[2][63] = {
  5259. { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
  5260. 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
  5261. 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
  5262. 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12 },
  5263. { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
  5264. 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
  5265. 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
  5266. 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }
  5267. };
  5268. int index[64];
  5269. int last;
  5270. int coeff_count = 0;
  5271. int abslevel1 = 1;
  5272. int abslevelgt1 = 0;
  5273. uint8_t *significant_coeff_ctx_base;
  5274. uint8_t *last_coeff_ctx_base;
  5275. uint8_t *abs_level_m1_ctx_base;
  5276. #ifndef ARCH_X86
  5277. #define CABAC_ON_STACK
  5278. #endif
  5279. #ifdef CABAC_ON_STACK
  5280. #define CC &cc
  5281. CABACContext cc;
  5282. cc.range = h->cabac.range;
  5283. cc.low = h->cabac.low;
  5284. cc.bytestream= h->cabac.bytestream;
  5285. #else
  5286. #define CC &h->cabac
  5287. #endif
  5288. /* cat: 0-> DC 16x16 n = 0
  5289. * 1-> AC 16x16 n = luma4x4idx
  5290. * 2-> Luma4x4 n = luma4x4idx
  5291. * 3-> DC Chroma n = iCbCr
  5292. * 4-> AC Chroma n = 4 * iCbCr + chroma4x4idx
  5293. * 5-> Luma8x8 n = 4 * luma8x8idx
  5294. */
  5295. /* read coded block flag */
  5296. if( cat != 5 ) {
  5297. if( get_cabac( CC, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n ) ] ) == 0 ) {
  5298. if( cat == 1 || cat == 2 )
  5299. h->non_zero_count_cache[scan8[n]] = 0;
  5300. else if( cat == 4 )
  5301. h->non_zero_count_cache[scan8[16+n]] = 0;
  5302. #ifdef CABAC_ON_STACK
  5303. h->cabac.range = cc.range ;
  5304. h->cabac.low = cc.low ;
  5305. h->cabac.bytestream= cc.bytestream;
  5306. #endif
  5307. return 0;
  5308. }
  5309. }
  5310. significant_coeff_ctx_base = h->cabac_state
  5311. + significant_coeff_flag_offset[MB_FIELD][cat];
  5312. last_coeff_ctx_base = h->cabac_state
  5313. + last_coeff_flag_offset[MB_FIELD][cat];
  5314. abs_level_m1_ctx_base = h->cabac_state
  5315. + coeff_abs_level_m1_offset[cat];
  5316. if( cat == 5 ) {
  5317. #define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \
  5318. for(last= 0; last < coefs; last++) { \
  5319. uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \
  5320. if( get_cabac( CC, sig_ctx )) { \
  5321. uint8_t *last_ctx = last_coeff_ctx_base + last_off; \
  5322. index[coeff_count++] = last; \
  5323. if( get_cabac( CC, last_ctx ) ) { \
  5324. last= max_coeff; \
  5325. break; \
  5326. } \
  5327. } \
  5328. }\
  5329. if( last == max_coeff -1 ) {\
  5330. index[coeff_count++] = last;\
  5331. }
  5332. const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
  5333. #if defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__))
  5334. coeff_count= decode_significance_8x8_x86(CC, significant_coeff_ctx_base, index, sig_off);
  5335. } else {
  5336. coeff_count= decode_significance_x86(CC, max_coeff, significant_coeff_ctx_base, index);
  5337. #else
  5338. DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );
  5339. } else {
  5340. DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
  5341. #endif
  5342. }
  5343. assert(coeff_count > 0);
  5344. if( cat == 0 )
  5345. h->cbp_table[mb_xy] |= 0x100;
  5346. else if( cat == 1 || cat == 2 )
  5347. h->non_zero_count_cache[scan8[n]] = coeff_count;
  5348. else if( cat == 3 )
  5349. h->cbp_table[mb_xy] |= 0x40 << n;
  5350. else if( cat == 4 )
  5351. h->non_zero_count_cache[scan8[16+n]] = coeff_count;
  5352. else {
  5353. assert( cat == 5 );
  5354. fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);
  5355. }
  5356. for( coeff_count--; coeff_count >= 0; coeff_count-- ) {
  5357. uint8_t *ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 )) + abs_level_m1_ctx_base;
  5358. int j= scantable[index[coeff_count]];
  5359. if( get_cabac( CC, ctx ) == 0 ) {
  5360. if( !qmul ) {
  5361. block[j] = get_cabac_bypass_sign( CC, -1);
  5362. }else{
  5363. block[j] = (get_cabac_bypass_sign( CC, -qmul[j]) + 32) >> 6;;
  5364. }
  5365. abslevel1++;
  5366. } else {
  5367. int coeff_abs = 2;
  5368. ctx = 5 + FFMIN( 4, abslevelgt1 ) + abs_level_m1_ctx_base;
  5369. while( coeff_abs < 15 && get_cabac( CC, ctx ) ) {
  5370. coeff_abs++;
  5371. }
  5372. if( coeff_abs >= 15 ) {
  5373. int j = 0;
  5374. while( get_cabac_bypass( CC ) ) {
  5375. j++;
  5376. }
  5377. coeff_abs=1;
  5378. while( j-- ) {
  5379. coeff_abs += coeff_abs + get_cabac_bypass( CC );
  5380. }
  5381. coeff_abs+= 14;
  5382. }
  5383. if( !qmul ) {
  5384. if( get_cabac_bypass( CC ) ) block[j] = -coeff_abs;
  5385. else block[j] = coeff_abs;
  5386. }else{
  5387. if( get_cabac_bypass( CC ) ) block[j] = (-coeff_abs * qmul[j] + 32) >> 6;
  5388. else block[j] = ( coeff_abs * qmul[j] + 32) >> 6;
  5389. }
  5390. abslevelgt1++;
  5391. }
  5392. }
  5393. #ifdef CABAC_ON_STACK
  5394. h->cabac.range = cc.range ;
  5395. h->cabac.low = cc.low ;
  5396. h->cabac.bytestream= cc.bytestream;
  5397. #endif
  5398. return 0;
  5399. }
  5400. static void inline compute_mb_neighbors(H264Context *h)
  5401. {
  5402. MpegEncContext * const s = &h->s;
  5403. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  5404. h->top_mb_xy = mb_xy - s->mb_stride;
  5405. h->left_mb_xy[0] = mb_xy - 1;
  5406. if(FRAME_MBAFF){
  5407. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  5408. const int top_pair_xy = pair_xy - s->mb_stride;
  5409. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  5410. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  5411. const int curr_mb_frame_flag = !MB_FIELD;
  5412. const int bottom = (s->mb_y & 1);
  5413. if (bottom
  5414. ? !curr_mb_frame_flag // bottom macroblock
  5415. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  5416. ) {
  5417. h->top_mb_xy -= s->mb_stride;
  5418. }
  5419. if (left_mb_frame_flag != curr_mb_frame_flag) {
  5420. h->left_mb_xy[0] = pair_xy - 1;
  5421. }
  5422. }
  5423. return;
  5424. }
  5425. /**
  5426. * decodes a macroblock
  5427. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  5428. */
  5429. static int decode_mb_cabac(H264Context *h) {
  5430. MpegEncContext * const s = &h->s;
  5431. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  5432. int mb_type, partition_count, cbp = 0;
  5433. int dct8x8_allowed= h->pps.transform_8x8_mode;
  5434. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?)
  5435. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  5436. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) {
  5437. int skip;
  5438. /* a skipped mb needs the aff flag from the following mb */
  5439. if( FRAME_MBAFF && s->mb_x==0 && (s->mb_y&1)==0 )
  5440. predict_field_decoding_flag(h);
  5441. if( FRAME_MBAFF && (s->mb_y&1)==1 && h->prev_mb_skipped )
  5442. skip = h->next_mb_skipped;
  5443. else
  5444. skip = decode_cabac_mb_skip( h, s->mb_x, s->mb_y );
  5445. /* read skip flags */
  5446. if( skip ) {
  5447. if( FRAME_MBAFF && (s->mb_y&1)==0 ){
  5448. s->current_picture.mb_type[mb_xy] = MB_TYPE_SKIP;
  5449. h->next_mb_skipped = decode_cabac_mb_skip( h, s->mb_x, s->mb_y+1 );
  5450. if(h->next_mb_skipped)
  5451. predict_field_decoding_flag(h);
  5452. else
  5453. h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5454. }
  5455. decode_mb_skip(h);
  5456. h->cbp_table[mb_xy] = 0;
  5457. h->chroma_pred_mode_table[mb_xy] = 0;
  5458. h->last_qscale_diff = 0;
  5459. return 0;
  5460. }
  5461. }
  5462. if(FRAME_MBAFF){
  5463. if( (s->mb_y&1) == 0 )
  5464. h->mb_mbaff =
  5465. h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5466. }else
  5467. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  5468. h->prev_mb_skipped = 0;
  5469. compute_mb_neighbors(h);
  5470. if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
  5471. av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
  5472. return -1;
  5473. }
  5474. if( h->slice_type == B_TYPE ) {
  5475. if( mb_type < 23 ){
  5476. partition_count= b_mb_type_info[mb_type].partition_count;
  5477. mb_type= b_mb_type_info[mb_type].type;
  5478. }else{
  5479. mb_type -= 23;
  5480. goto decode_intra_mb;
  5481. }
  5482. } else if( h->slice_type == P_TYPE ) {
  5483. if( mb_type < 5) {
  5484. partition_count= p_mb_type_info[mb_type].partition_count;
  5485. mb_type= p_mb_type_info[mb_type].type;
  5486. } else {
  5487. mb_type -= 5;
  5488. goto decode_intra_mb;
  5489. }
  5490. } else {
  5491. assert(h->slice_type == I_TYPE);
  5492. decode_intra_mb:
  5493. partition_count = 0;
  5494. cbp= i_mb_type_info[mb_type].cbp;
  5495. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  5496. mb_type= i_mb_type_info[mb_type].type;
  5497. }
  5498. if(MB_FIELD)
  5499. mb_type |= MB_TYPE_INTERLACED;
  5500. h->slice_table[ mb_xy ]= h->slice_num;
  5501. if(IS_INTRA_PCM(mb_type)) {
  5502. const uint8_t *ptr;
  5503. unsigned int x, y;
  5504. // We assume these blocks are very rare so we dont optimize it.
  5505. // FIXME The two following lines get the bitstream position in the cabac
  5506. // decode, I think it should be done by a function in cabac.h (or cabac.c).
  5507. ptr= h->cabac.bytestream;
  5508. if(h->cabac.low&0x1) ptr--;
  5509. if(CABAC_BITS==16){
  5510. if(h->cabac.low&0x1FF) ptr--;
  5511. }
  5512. // The pixels are stored in the same order as levels in h->mb array.
  5513. for(y=0; y<16; y++){
  5514. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  5515. for(x=0; x<16; x++){
  5516. tprintf("LUMA ICPM LEVEL (%3d)\n", *ptr);
  5517. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= *ptr++;
  5518. }
  5519. }
  5520. for(y=0; y<8; y++){
  5521. const int index= 256 + 4*(y&3) + 32*(y>>2);
  5522. for(x=0; x<8; x++){
  5523. tprintf("CHROMA U ICPM LEVEL (%3d)\n", *ptr);
  5524. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5525. }
  5526. }
  5527. for(y=0; y<8; y++){
  5528. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  5529. for(x=0; x<8; x++){
  5530. tprintf("CHROMA V ICPM LEVEL (%3d)\n", *ptr);
  5531. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5532. }
  5533. }
  5534. ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);
  5535. // All blocks are present
  5536. h->cbp_table[mb_xy] = 0x1ef;
  5537. h->chroma_pred_mode_table[mb_xy] = 0;
  5538. // In deblocking, the quantizer is 0
  5539. s->current_picture.qscale_table[mb_xy]= 0;
  5540. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  5541. // All coeffs are present
  5542. memset(h->non_zero_count[mb_xy], 16, 16);
  5543. s->current_picture.mb_type[mb_xy]= mb_type;
  5544. return 0;
  5545. }
  5546. if(MB_MBAFF){
  5547. h->ref_count[0] <<= 1;
  5548. h->ref_count[1] <<= 1;
  5549. }
  5550. fill_caches(h, mb_type, 0);
  5551. if( IS_INTRA( mb_type ) ) {
  5552. int i;
  5553. if( IS_INTRA4x4( mb_type ) ) {
  5554. if( dct8x8_allowed && decode_cabac_mb_transform_size( h ) ) {
  5555. mb_type |= MB_TYPE_8x8DCT;
  5556. for( i = 0; i < 16; i+=4 ) {
  5557. int pred = pred_intra_mode( h, i );
  5558. int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5559. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  5560. }
  5561. } else {
  5562. for( i = 0; i < 16; i++ ) {
  5563. int pred = pred_intra_mode( h, i );
  5564. h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5565. //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
  5566. }
  5567. }
  5568. write_back_intra_pred_mode(h);
  5569. if( check_intra4x4_pred_mode(h) < 0 ) return -1;
  5570. } else {
  5571. h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );
  5572. if( h->intra16x16_pred_mode < 0 ) return -1;
  5573. }
  5574. h->chroma_pred_mode_table[mb_xy] =
  5575. h->chroma_pred_mode = decode_cabac_mb_chroma_pre_mode( h );
  5576. h->chroma_pred_mode= check_intra_pred_mode( h, h->chroma_pred_mode );
  5577. if( h->chroma_pred_mode < 0 ) return -1;
  5578. } else if( partition_count == 4 ) {
  5579. int i, j, sub_partition_count[4], list, ref[2][4];
  5580. if( h->slice_type == B_TYPE ) {
  5581. for( i = 0; i < 4; i++ ) {
  5582. h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
  5583. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5584. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5585. }
  5586. if( IS_DIRECT(h->sub_mb_type[0] | h->sub_mb_type[1] |
  5587. h->sub_mb_type[2] | h->sub_mb_type[3]) ) {
  5588. pred_direct_motion(h, &mb_type);
  5589. if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
  5590. for( i = 0; i < 4; i++ )
  5591. if( IS_DIRECT(h->sub_mb_type[i]) )
  5592. fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );
  5593. }
  5594. }
  5595. } else {
  5596. for( i = 0; i < 4; i++ ) {
  5597. h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
  5598. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5599. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5600. }
  5601. }
  5602. for( list = 0; list < 2; list++ ) {
  5603. if( h->ref_count[list] > 0 ) {
  5604. for( i = 0; i < 4; i++ ) {
  5605. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  5606. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  5607. if( h->ref_count[list] > 1 )
  5608. ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );
  5609. else
  5610. ref[list][i] = 0;
  5611. } else {
  5612. ref[list][i] = -1;
  5613. }
  5614. h->ref_cache[list][ scan8[4*i]+1 ]=
  5615. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  5616. }
  5617. }
  5618. }
  5619. if(dct8x8_allowed)
  5620. dct8x8_allowed = get_dct8x8_allowed(h);
  5621. for(list=0; list<2; list++){
  5622. for(i=0; i<4; i++){
  5623. if(IS_DIRECT(h->sub_mb_type[i])){
  5624. fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);
  5625. continue;
  5626. }
  5627. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];
  5628. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  5629. const int sub_mb_type= h->sub_mb_type[i];
  5630. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  5631. for(j=0; j<sub_partition_count[i]; j++){
  5632. int mpx, mpy;
  5633. int mx, my;
  5634. const int index= 4*i + block_width*j;
  5635. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  5636. int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];
  5637. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);
  5638. mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );
  5639. my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );
  5640. tprintf("final mv:%d %d\n", mx, my);
  5641. if(IS_SUB_8X8(sub_mb_type)){
  5642. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  5643. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  5644. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  5645. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  5646. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]=
  5647. mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;
  5648. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]=
  5649. mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;
  5650. }else if(IS_SUB_8X4(sub_mb_type)){
  5651. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  5652. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  5653. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]= mx- mpx;
  5654. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]= my - mpy;
  5655. }else if(IS_SUB_4X8(sub_mb_type)){
  5656. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  5657. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  5658. mvd_cache[ 0 ][0]= mvd_cache[ 8 ][0]= mx - mpx;
  5659. mvd_cache[ 0 ][1]= mvd_cache[ 8 ][1]= my - mpy;
  5660. }else{
  5661. assert(IS_SUB_4X4(sub_mb_type));
  5662. mv_cache[ 0 ][0]= mx;
  5663. mv_cache[ 0 ][1]= my;
  5664. mvd_cache[ 0 ][0]= mx - mpx;
  5665. mvd_cache[ 0 ][1]= my - mpy;
  5666. }
  5667. }
  5668. }else{
  5669. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  5670. uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];
  5671. p[0] = p[1] = p[8] = p[9] = 0;
  5672. pd[0]= pd[1]= pd[8]= pd[9]= 0;
  5673. }
  5674. }
  5675. }
  5676. } else if( IS_DIRECT(mb_type) ) {
  5677. pred_direct_motion(h, &mb_type);
  5678. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  5679. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  5680. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  5681. } else {
  5682. int list, mx, my, i, mpx, mpy;
  5683. if(IS_16X16(mb_type)){
  5684. for(list=0; list<2; list++){
  5685. if(IS_DIR(mb_type, 0, list)){
  5686. if(h->ref_count[list] > 0 ){
  5687. const int ref = h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 0 ) : 0;
  5688. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
  5689. }
  5690. }else
  5691. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  5692. }
  5693. for(list=0; list<2; list++){
  5694. if(IS_DIR(mb_type, 0, list)){
  5695. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);
  5696. mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );
  5697. my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );
  5698. tprintf("final mv:%d %d\n", mx, my);
  5699. fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5700. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  5701. }else
  5702. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  5703. }
  5704. }
  5705. else if(IS_16X8(mb_type)){
  5706. for(list=0; list<2; list++){
  5707. if(h->ref_count[list]>0){
  5708. for(i=0; i<2; i++){
  5709. if(IS_DIR(mb_type, i, list)){
  5710. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 8*i ) : 0;
  5711. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
  5712. }else
  5713. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  5714. }
  5715. }
  5716. }
  5717. for(list=0; list<2; list++){
  5718. for(i=0; i<2; i++){
  5719. if(IS_DIR(mb_type, i, list)){
  5720. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);
  5721. mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );
  5722. my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );
  5723. tprintf("final mv:%d %d\n", mx, my);
  5724. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);
  5725. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  5726. }else{
  5727. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5728. fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5729. }
  5730. }
  5731. }
  5732. }else{
  5733. assert(IS_8X16(mb_type));
  5734. for(list=0; list<2; list++){
  5735. if(h->ref_count[list]>0){
  5736. for(i=0; i<2; i++){
  5737. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  5738. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 4*i ) : 0;
  5739. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
  5740. }else
  5741. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  5742. }
  5743. }
  5744. }
  5745. for(list=0; list<2; list++){
  5746. for(i=0; i<2; i++){
  5747. if(IS_DIR(mb_type, i, list)){
  5748. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);
  5749. mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );
  5750. my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );
  5751. tprintf("final mv:%d %d\n", mx, my);
  5752. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5753. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  5754. }else{
  5755. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5756. fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5757. }
  5758. }
  5759. }
  5760. }
  5761. }
  5762. if( IS_INTER( mb_type ) ) {
  5763. h->chroma_pred_mode_table[mb_xy] = 0;
  5764. write_back_motion( h, mb_type );
  5765. }
  5766. if( !IS_INTRA16x16( mb_type ) ) {
  5767. cbp = decode_cabac_mb_cbp_luma( h );
  5768. cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
  5769. }
  5770. h->cbp_table[mb_xy] = h->cbp = cbp;
  5771. if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
  5772. if( decode_cabac_mb_transform_size( h ) )
  5773. mb_type |= MB_TYPE_8x8DCT;
  5774. }
  5775. s->current_picture.mb_type[mb_xy]= mb_type;
  5776. if( cbp || IS_INTRA16x16( mb_type ) ) {
  5777. const uint8_t *scan, *scan8x8, *dc_scan;
  5778. int dqp;
  5779. if(IS_INTERLACED(mb_type)){
  5780. scan8x8= s->qscale ? h->field_scan8x8 : h->field_scan8x8_q0;
  5781. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  5782. dc_scan= luma_dc_field_scan;
  5783. }else{
  5784. scan8x8= s->qscale ? h->zigzag_scan8x8 : h->zigzag_scan8x8_q0;
  5785. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  5786. dc_scan= luma_dc_zigzag_scan;
  5787. }
  5788. h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
  5789. if( dqp == INT_MIN ){
  5790. av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", s->mb_x, s->mb_y);
  5791. return -1;
  5792. }
  5793. s->qscale += dqp;
  5794. if(((unsigned)s->qscale) > 51){
  5795. if(s->qscale<0) s->qscale+= 52;
  5796. else s->qscale-= 52;
  5797. }
  5798. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  5799. if( IS_INTRA16x16( mb_type ) ) {
  5800. int i;
  5801. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
  5802. if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16) < 0)
  5803. return -1;
  5804. if( cbp&15 ) {
  5805. for( i = 0; i < 16; i++ ) {
  5806. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
  5807. if( decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 )
  5808. return -1;
  5809. }
  5810. } else {
  5811. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  5812. }
  5813. } else {
  5814. int i8x8, i4x4;
  5815. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5816. if( cbp & (1<<i8x8) ) {
  5817. if( IS_8x8DCT(mb_type) ) {
  5818. if( decode_cabac_residual(h, h->mb + 64*i8x8, 5, 4*i8x8,
  5819. scan8x8, h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 64) < 0 )
  5820. return -1;
  5821. } else
  5822. for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
  5823. const int index = 4*i8x8 + i4x4;
  5824. //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
  5825. //START_TIMER
  5826. 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 )
  5827. return -1;
  5828. //STOP_TIMER("decode_residual")
  5829. }
  5830. } else {
  5831. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  5832. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  5833. }
  5834. }
  5835. }
  5836. if( cbp&0x30 ){
  5837. int c;
  5838. for( c = 0; c < 2; c++ ) {
  5839. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
  5840. if( decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, NULL, 4) < 0)
  5841. return -1;
  5842. }
  5843. }
  5844. if( cbp&0x20 ) {
  5845. int c, i;
  5846. for( c = 0; c < 2; c++ ) {
  5847. for( i = 0; i < 4; i++ ) {
  5848. const int index = 16 + 4 * c + i;
  5849. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
  5850. 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)
  5851. return -1;
  5852. }
  5853. }
  5854. } else {
  5855. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5856. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5857. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5858. }
  5859. } else {
  5860. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5861. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  5862. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5863. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5864. h->last_qscale_diff = 0;
  5865. }
  5866. s->current_picture.qscale_table[mb_xy]= s->qscale;
  5867. write_back_non_zero_count(h);
  5868. if(MB_MBAFF){
  5869. h->ref_count[0] >>= 1;
  5870. h->ref_count[1] >>= 1;
  5871. }
  5872. return 0;
  5873. }
  5874. static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5875. int i, d;
  5876. const int index_a = qp + h->slice_alpha_c0_offset;
  5877. const int alpha = (alpha_table+52)[index_a];
  5878. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5879. if( bS[0] < 4 ) {
  5880. int8_t tc[4];
  5881. for(i=0; i<4; i++)
  5882. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  5883. h->s.dsp.h264_h_loop_filter_luma(pix, stride, alpha, beta, tc);
  5884. } else {
  5885. /* 16px edge length, because bS=4 is triggered by being at
  5886. * the edge of an intra MB, so all 4 bS are the same */
  5887. for( d = 0; d < 16; d++ ) {
  5888. const int p0 = pix[-1];
  5889. const int p1 = pix[-2];
  5890. const int p2 = pix[-3];
  5891. const int q0 = pix[0];
  5892. const int q1 = pix[1];
  5893. const int q2 = pix[2];
  5894. if( FFABS( p0 - q0 ) < alpha &&
  5895. FFABS( p1 - p0 ) < beta &&
  5896. FFABS( q1 - q0 ) < beta ) {
  5897. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5898. if( FFABS( p2 - p0 ) < beta)
  5899. {
  5900. const int p3 = pix[-4];
  5901. /* p0', p1', p2' */
  5902. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5903. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5904. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5905. } else {
  5906. /* p0' */
  5907. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5908. }
  5909. if( FFABS( q2 - q0 ) < beta)
  5910. {
  5911. const int q3 = pix[3];
  5912. /* q0', q1', q2' */
  5913. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5914. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5915. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5916. } else {
  5917. /* q0' */
  5918. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5919. }
  5920. }else{
  5921. /* p0', q0' */
  5922. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5923. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5924. }
  5925. 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]);
  5926. }
  5927. pix += stride;
  5928. }
  5929. }
  5930. }
  5931. static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5932. int i;
  5933. const int index_a = qp + h->slice_alpha_c0_offset;
  5934. const int alpha = (alpha_table+52)[index_a];
  5935. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5936. if( bS[0] < 4 ) {
  5937. int8_t tc[4];
  5938. for(i=0; i<4; i++)
  5939. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  5940. h->s.dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
  5941. } else {
  5942. h->s.dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
  5943. }
  5944. }
  5945. static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  5946. int i;
  5947. for( i = 0; i < 16; i++, pix += stride) {
  5948. int index_a;
  5949. int alpha;
  5950. int beta;
  5951. int qp_index;
  5952. int bS_index = (i >> 1);
  5953. if (!MB_FIELD) {
  5954. bS_index &= ~1;
  5955. bS_index |= (i & 1);
  5956. }
  5957. if( bS[bS_index] == 0 ) {
  5958. continue;
  5959. }
  5960. qp_index = MB_FIELD ? (i >> 3) : (i & 1);
  5961. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  5962. alpha = (alpha_table+52)[index_a];
  5963. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  5964. if( bS[bS_index] < 4 ) {
  5965. const int tc0 = (tc0_table+52)[index_a][bS[bS_index] - 1];
  5966. const int p0 = pix[-1];
  5967. const int p1 = pix[-2];
  5968. const int p2 = pix[-3];
  5969. const int q0 = pix[0];
  5970. const int q1 = pix[1];
  5971. const int q2 = pix[2];
  5972. if( FFABS( p0 - q0 ) < alpha &&
  5973. FFABS( p1 - p0 ) < beta &&
  5974. FFABS( q1 - q0 ) < beta ) {
  5975. int tc = tc0;
  5976. int i_delta;
  5977. if( FFABS( p2 - p0 ) < beta ) {
  5978. pix[-2] = p1 + clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  5979. tc++;
  5980. }
  5981. if( FFABS( q2 - q0 ) < beta ) {
  5982. pix[1] = q1 + clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  5983. tc++;
  5984. }
  5985. i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  5986. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  5987. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  5988. 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);
  5989. }
  5990. }else{
  5991. const int p0 = pix[-1];
  5992. const int p1 = pix[-2];
  5993. const int p2 = pix[-3];
  5994. const int q0 = pix[0];
  5995. const int q1 = pix[1];
  5996. const int q2 = pix[2];
  5997. if( FFABS( p0 - q0 ) < alpha &&
  5998. FFABS( p1 - p0 ) < beta &&
  5999. FFABS( q1 - q0 ) < beta ) {
  6000. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  6001. if( FFABS( p2 - p0 ) < beta)
  6002. {
  6003. const int p3 = pix[-4];
  6004. /* p0', p1', p2' */
  6005. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  6006. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  6007. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  6008. } else {
  6009. /* p0' */
  6010. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6011. }
  6012. if( FFABS( q2 - q0 ) < beta)
  6013. {
  6014. const int q3 = pix[3];
  6015. /* q0', q1', q2' */
  6016. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  6017. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  6018. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  6019. } else {
  6020. /* q0' */
  6021. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6022. }
  6023. }else{
  6024. /* p0', q0' */
  6025. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6026. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6027. }
  6028. 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]);
  6029. }
  6030. }
  6031. }
  6032. }
  6033. static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  6034. int i;
  6035. for( i = 0; i < 8; i++, pix += stride) {
  6036. int index_a;
  6037. int alpha;
  6038. int beta;
  6039. int qp_index;
  6040. int bS_index = i;
  6041. if( bS[bS_index] == 0 ) {
  6042. continue;
  6043. }
  6044. qp_index = MB_FIELD ? (i >> 2) : (i & 1);
  6045. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  6046. alpha = (alpha_table+52)[index_a];
  6047. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  6048. if( bS[bS_index] < 4 ) {
  6049. const int tc = (tc0_table+52)[index_a][bS[bS_index] - 1] + 1;
  6050. const int p0 = pix[-1];
  6051. const int p1 = pix[-2];
  6052. const int q0 = pix[0];
  6053. const int q1 = pix[1];
  6054. if( FFABS( p0 - q0 ) < alpha &&
  6055. FFABS( p1 - p0 ) < beta &&
  6056. FFABS( q1 - q0 ) < beta ) {
  6057. const int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  6058. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  6059. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  6060. 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);
  6061. }
  6062. }else{
  6063. const int p0 = pix[-1];
  6064. const int p1 = pix[-2];
  6065. const int q0 = pix[0];
  6066. const int q1 = pix[1];
  6067. if( FFABS( p0 - q0 ) < alpha &&
  6068. FFABS( p1 - p0 ) < beta &&
  6069. FFABS( q1 - q0 ) < beta ) {
  6070. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  6071. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  6072. 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]);
  6073. }
  6074. }
  6075. }
  6076. }
  6077. static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  6078. int i, d;
  6079. const int index_a = qp + h->slice_alpha_c0_offset;
  6080. const int alpha = (alpha_table+52)[index_a];
  6081. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  6082. const int pix_next = stride;
  6083. if( bS[0] < 4 ) {
  6084. int8_t tc[4];
  6085. for(i=0; i<4; i++)
  6086. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  6087. h->s.dsp.h264_v_loop_filter_luma(pix, stride, alpha, beta, tc);
  6088. } else {
  6089. /* 16px edge length, see filter_mb_edgev */
  6090. for( d = 0; d < 16; d++ ) {
  6091. const int p0 = pix[-1*pix_next];
  6092. const int p1 = pix[-2*pix_next];
  6093. const int p2 = pix[-3*pix_next];
  6094. const int q0 = pix[0];
  6095. const int q1 = pix[1*pix_next];
  6096. const int q2 = pix[2*pix_next];
  6097. if( FFABS( p0 - q0 ) < alpha &&
  6098. FFABS( p1 - p0 ) < beta &&
  6099. FFABS( q1 - q0 ) < beta ) {
  6100. const int p3 = pix[-4*pix_next];
  6101. const int q3 = pix[ 3*pix_next];
  6102. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  6103. if( FFABS( p2 - p0 ) < beta) {
  6104. /* p0', p1', p2' */
  6105. pix[-1*pix_next] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  6106. pix[-2*pix_next] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  6107. pix[-3*pix_next] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  6108. } else {
  6109. /* p0' */
  6110. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6111. }
  6112. if( FFABS( q2 - q0 ) < beta) {
  6113. /* q0', q1', q2' */
  6114. pix[0*pix_next] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  6115. pix[1*pix_next] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  6116. pix[2*pix_next] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  6117. } else {
  6118. /* q0' */
  6119. pix[0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6120. }
  6121. }else{
  6122. /* p0', q0' */
  6123. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6124. pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6125. }
  6126. 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]);
  6127. }
  6128. pix++;
  6129. }
  6130. }
  6131. }
  6132. static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  6133. int i;
  6134. const int index_a = qp + h->slice_alpha_c0_offset;
  6135. const int alpha = (alpha_table+52)[index_a];
  6136. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  6137. if( bS[0] < 4 ) {
  6138. int8_t tc[4];
  6139. for(i=0; i<4; i++)
  6140. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  6141. h->s.dsp.h264_v_loop_filter_chroma(pix, stride, alpha, beta, tc);
  6142. } else {
  6143. h->s.dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);
  6144. }
  6145. }
  6146. 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) {
  6147. MpegEncContext * const s = &h->s;
  6148. int mb_xy, mb_type;
  6149. int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh;
  6150. if(mb_x==0 || mb_y==0 || !s->dsp.h264_loop_filter_strength) {
  6151. filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);
  6152. return;
  6153. }
  6154. assert(!FRAME_MBAFF);
  6155. mb_xy = mb_x + mb_y*s->mb_stride;
  6156. mb_type = s->current_picture.mb_type[mb_xy];
  6157. qp = s->current_picture.qscale_table[mb_xy];
  6158. qp0 = s->current_picture.qscale_table[mb_xy-1];
  6159. qp1 = s->current_picture.qscale_table[h->top_mb_xy];
  6160. qpc = get_chroma_qp( h->pps.chroma_qp_index_offset, qp );
  6161. qpc0 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp0 );
  6162. qpc1 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp1 );
  6163. qp0 = (qp + qp0 + 1) >> 1;
  6164. qp1 = (qp + qp1 + 1) >> 1;
  6165. qpc0 = (qpc + qpc0 + 1) >> 1;
  6166. qpc1 = (qpc + qpc1 + 1) >> 1;
  6167. qp_thresh = 15 - h->slice_alpha_c0_offset;
  6168. if(qp <= qp_thresh && qp0 <= qp_thresh && qp1 <= qp_thresh &&
  6169. qpc <= qp_thresh && qpc0 <= qp_thresh && qpc1 <= qp_thresh)
  6170. return;
  6171. if( IS_INTRA(mb_type) ) {
  6172. int16_t bS4[4] = {4,4,4,4};
  6173. int16_t bS3[4] = {3,3,3,3};
  6174. if( IS_8x8DCT(mb_type) ) {
  6175. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  6176. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  6177. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  6178. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  6179. } else {
  6180. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  6181. filter_mb_edgev( h, &img_y[4*1], linesize, bS3, qp );
  6182. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  6183. filter_mb_edgev( h, &img_y[4*3], linesize, bS3, qp );
  6184. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  6185. filter_mb_edgeh( h, &img_y[4*1*linesize], linesize, bS3, qp );
  6186. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  6187. filter_mb_edgeh( h, &img_y[4*3*linesize], linesize, bS3, qp );
  6188. }
  6189. filter_mb_edgecv( h, &img_cb[2*0], uvlinesize, bS4, qpc0 );
  6190. filter_mb_edgecv( h, &img_cb[2*2], uvlinesize, bS3, qpc );
  6191. filter_mb_edgecv( h, &img_cr[2*0], uvlinesize, bS4, qpc0 );
  6192. filter_mb_edgecv( h, &img_cr[2*2], uvlinesize, bS3, qpc );
  6193. filter_mb_edgech( h, &img_cb[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  6194. filter_mb_edgech( h, &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc );
  6195. filter_mb_edgech( h, &img_cr[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  6196. filter_mb_edgech( h, &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc );
  6197. return;
  6198. } else {
  6199. DECLARE_ALIGNED_8(int16_t, bS[2][4][4]);
  6200. uint64_t (*bSv)[4] = (uint64_t(*)[4])bS;
  6201. int edges;
  6202. if( IS_8x8DCT(mb_type) && (h->cbp&7) == 7 ) {
  6203. edges = 4;
  6204. bSv[0][0] = bSv[0][2] = bSv[1][0] = bSv[1][2] = 0x0002000200020002ULL;
  6205. } else {
  6206. int mask_edge1 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 :
  6207. (mb_type & MB_TYPE_16x8) ? 1 : 0;
  6208. int mask_edge0 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16))
  6209. && (s->current_picture.mb_type[mb_xy-1] & (MB_TYPE_16x16 | MB_TYPE_8x16))
  6210. ? 3 : 0;
  6211. int step = IS_8x8DCT(mb_type) ? 2 : 1;
  6212. edges = (mb_type & MB_TYPE_16x16) && !(h->cbp & 15) ? 1 : 4;
  6213. s->dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,
  6214. (h->slice_type == B_TYPE), edges, step, mask_edge0, mask_edge1 );
  6215. }
  6216. if( IS_INTRA(s->current_picture.mb_type[mb_xy-1]) )
  6217. bSv[0][0] = 0x0004000400040004ULL;
  6218. if( IS_INTRA(s->current_picture.mb_type[h->top_mb_xy]) )
  6219. bSv[1][0] = 0x0004000400040004ULL;
  6220. #define FILTER(hv,dir,edge)\
  6221. if(bSv[dir][edge]) {\
  6222. filter_mb_edge##hv( h, &img_y[4*edge*(dir?linesize:1)], linesize, bS[dir][edge], edge ? qp : qp##dir );\
  6223. if(!(edge&1)) {\
  6224. filter_mb_edgec##hv( h, &img_cb[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  6225. filter_mb_edgec##hv( h, &img_cr[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  6226. }\
  6227. }
  6228. if( edges == 1 ) {
  6229. FILTER(v,0,0);
  6230. FILTER(h,1,0);
  6231. } else if( IS_8x8DCT(mb_type) ) {
  6232. FILTER(v,0,0);
  6233. FILTER(v,0,2);
  6234. FILTER(h,1,0);
  6235. FILTER(h,1,2);
  6236. } else {
  6237. FILTER(v,0,0);
  6238. FILTER(v,0,1);
  6239. FILTER(v,0,2);
  6240. FILTER(v,0,3);
  6241. FILTER(h,1,0);
  6242. FILTER(h,1,1);
  6243. FILTER(h,1,2);
  6244. FILTER(h,1,3);
  6245. }
  6246. #undef FILTER
  6247. }
  6248. }
  6249. 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) {
  6250. MpegEncContext * const s = &h->s;
  6251. const int mb_xy= mb_x + mb_y*s->mb_stride;
  6252. const int mb_type = s->current_picture.mb_type[mb_xy];
  6253. const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4;
  6254. int first_vertical_edge_done = 0;
  6255. int dir;
  6256. /* FIXME: A given frame may occupy more than one position in
  6257. * the reference list. So ref2frm should be populated with
  6258. * frame numbers, not indices. */
  6259. static const int ref2frm[34] = {-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  6260. 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
  6261. //for sufficiently low qp, filtering wouldn't do anything
  6262. //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
  6263. if(!FRAME_MBAFF){
  6264. int qp_thresh = 15 - h->slice_alpha_c0_offset - FFMAX(0, h->pps.chroma_qp_index_offset);
  6265. int qp = s->current_picture.qscale_table[mb_xy];
  6266. if(qp <= qp_thresh
  6267. && (mb_x == 0 || ((qp + s->current_picture.qscale_table[mb_xy-1] + 1)>>1) <= qp_thresh)
  6268. && (mb_y == 0 || ((qp + s->current_picture.qscale_table[h->top_mb_xy] + 1)>>1) <= qp_thresh)){
  6269. return;
  6270. }
  6271. }
  6272. if (FRAME_MBAFF
  6273. // left mb is in picture
  6274. && h->slice_table[mb_xy-1] != 255
  6275. // and current and left pair do not have the same interlaced type
  6276. && (IS_INTERLACED(mb_type) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
  6277. // and left mb is in the same slice if deblocking_filter == 2
  6278. && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
  6279. /* First vertical edge is different in MBAFF frames
  6280. * There are 8 different bS to compute and 2 different Qp
  6281. */
  6282. const int pair_xy = mb_x + (mb_y&~1)*s->mb_stride;
  6283. const int left_mb_xy[2] = { pair_xy-1, pair_xy-1+s->mb_stride };
  6284. int16_t bS[8];
  6285. int qp[2];
  6286. int chroma_qp[2];
  6287. int mb_qp, mbn0_qp, mbn1_qp;
  6288. int i;
  6289. first_vertical_edge_done = 1;
  6290. if( IS_INTRA(mb_type) )
  6291. bS[0] = bS[1] = bS[2] = bS[3] = bS[4] = bS[5] = bS[6] = bS[7] = 4;
  6292. else {
  6293. for( i = 0; i < 8; i++ ) {
  6294. int mbn_xy = MB_FIELD ? left_mb_xy[i>>2] : left_mb_xy[i&1];
  6295. if( IS_INTRA( s->current_picture.mb_type[mbn_xy] ) )
  6296. bS[i] = 4;
  6297. else if( h->non_zero_count_cache[12+8*(i>>1)] != 0 ||
  6298. /* FIXME: with 8x8dct + cavlc, should check cbp instead of nnz */
  6299. h->non_zero_count[mbn_xy][MB_FIELD ? i&3 : (i>>2)+(mb_y&1)*2] )
  6300. bS[i] = 2;
  6301. else
  6302. bS[i] = 1;
  6303. }
  6304. }
  6305. mb_qp = s->current_picture.qscale_table[mb_xy];
  6306. mbn0_qp = s->current_picture.qscale_table[left_mb_xy[0]];
  6307. mbn1_qp = s->current_picture.qscale_table[left_mb_xy[1]];
  6308. qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1;
  6309. chroma_qp[0] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6310. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn0_qp ) + 1 ) >> 1;
  6311. qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1;
  6312. chroma_qp[1] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6313. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn1_qp ) + 1 ) >> 1;
  6314. /* Filter edge */
  6315. 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);
  6316. { int i; for (i = 0; i < 8; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6317. filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp );
  6318. filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, chroma_qp );
  6319. filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, chroma_qp );
  6320. }
  6321. /* dir : 0 -> vertical edge, 1 -> horizontal edge */
  6322. for( dir = 0; dir < 2; dir++ )
  6323. {
  6324. int edge;
  6325. const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
  6326. const int mbm_type = s->current_picture.mb_type[mbm_xy];
  6327. int start = h->slice_table[mbm_xy] == 255 ? 1 : 0;
  6328. const int edges = (mb_type & (MB_TYPE_16x16|MB_TYPE_SKIP))
  6329. == (MB_TYPE_16x16|MB_TYPE_SKIP) ? 1 : 4;
  6330. // how often to recheck mv-based bS when iterating between edges
  6331. const int mask_edge = (mb_type & (MB_TYPE_16x16 | (MB_TYPE_16x8 << dir))) ? 3 :
  6332. (mb_type & (MB_TYPE_8x16 >> dir)) ? 1 : 0;
  6333. // how often to recheck mv-based bS when iterating along each edge
  6334. const int mask_par0 = mb_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir));
  6335. if (first_vertical_edge_done) {
  6336. start = 1;
  6337. first_vertical_edge_done = 0;
  6338. }
  6339. if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
  6340. start = 1;
  6341. if (FRAME_MBAFF && (dir == 1) && ((mb_y&1) == 0) && start == 0
  6342. && !IS_INTERLACED(mb_type)
  6343. && IS_INTERLACED(mbm_type)
  6344. ) {
  6345. // This is a special case in the norm where the filtering must
  6346. // be done twice (one each of the field) even if we are in a
  6347. // frame macroblock.
  6348. //
  6349. static const int nnz_idx[4] = {4,5,6,3};
  6350. unsigned int tmp_linesize = 2 * linesize;
  6351. unsigned int tmp_uvlinesize = 2 * uvlinesize;
  6352. int mbn_xy = mb_xy - 2 * s->mb_stride;
  6353. int qp, chroma_qp;
  6354. int i, j;
  6355. int16_t bS[4];
  6356. for(j=0; j<2; j++, mbn_xy += s->mb_stride){
  6357. if( IS_INTRA(mb_type) ||
  6358. IS_INTRA(s->current_picture.mb_type[mbn_xy]) ) {
  6359. bS[0] = bS[1] = bS[2] = bS[3] = 3;
  6360. } else {
  6361. const uint8_t *mbn_nnz = h->non_zero_count[mbn_xy];
  6362. for( i = 0; i < 4; i++ ) {
  6363. if( h->non_zero_count_cache[scan8[0]+i] != 0 ||
  6364. mbn_nnz[nnz_idx[i]] != 0 )
  6365. bS[i] = 2;
  6366. else
  6367. bS[i] = 1;
  6368. }
  6369. }
  6370. // Do not use s->qscale as luma quantizer because it has not the same
  6371. // value in IPCM macroblocks.
  6372. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6373. 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);
  6374. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6375. filter_mb_edgeh( h, &img_y[j*linesize], tmp_linesize, bS, qp );
  6376. chroma_qp = ( h->chroma_qp +
  6377. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6378. filter_mb_edgech( h, &img_cb[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6379. filter_mb_edgech( h, &img_cr[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6380. }
  6381. start = 1;
  6382. }
  6383. /* Calculate bS */
  6384. for( edge = start; edge < edges; edge++ ) {
  6385. /* mbn_xy: neighbor macroblock */
  6386. const int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
  6387. const int mbn_type = s->current_picture.mb_type[mbn_xy];
  6388. int16_t bS[4];
  6389. int qp;
  6390. if( (edge&1) && IS_8x8DCT(mb_type) )
  6391. continue;
  6392. if( IS_INTRA(mb_type) ||
  6393. IS_INTRA(mbn_type) ) {
  6394. int value;
  6395. if (edge == 0) {
  6396. if ( (!IS_INTERLACED(mb_type) && !IS_INTERLACED(mbm_type))
  6397. || ((FRAME_MBAFF || (s->picture_structure != PICT_FRAME)) && (dir == 0))
  6398. ) {
  6399. value = 4;
  6400. } else {
  6401. value = 3;
  6402. }
  6403. } else {
  6404. value = 3;
  6405. }
  6406. bS[0] = bS[1] = bS[2] = bS[3] = value;
  6407. } else {
  6408. int i, l;
  6409. int mv_done;
  6410. if( edge & mask_edge ) {
  6411. bS[0] = bS[1] = bS[2] = bS[3] = 0;
  6412. mv_done = 1;
  6413. }
  6414. else if( FRAME_MBAFF && IS_INTERLACED(mb_type ^ mbn_type)) {
  6415. bS[0] = bS[1] = bS[2] = bS[3] = 1;
  6416. mv_done = 1;
  6417. }
  6418. else if( mask_par0 && (edge || (mbn_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir)))) ) {
  6419. int b_idx= 8 + 4 + edge * (dir ? 8:1);
  6420. int bn_idx= b_idx - (dir ? 8:1);
  6421. int v = 0;
  6422. for( l = 0; !v && l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6423. v |= ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6424. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6425. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit;
  6426. }
  6427. bS[0] = bS[1] = bS[2] = bS[3] = v;
  6428. mv_done = 1;
  6429. }
  6430. else
  6431. mv_done = 0;
  6432. for( i = 0; i < 4; i++ ) {
  6433. int x = dir == 0 ? edge : i;
  6434. int y = dir == 0 ? i : edge;
  6435. int b_idx= 8 + 4 + x + 8*y;
  6436. int bn_idx= b_idx - (dir ? 8:1);
  6437. if( h->non_zero_count_cache[b_idx] != 0 ||
  6438. h->non_zero_count_cache[bn_idx] != 0 ) {
  6439. bS[i] = 2;
  6440. }
  6441. else if(!mv_done)
  6442. {
  6443. bS[i] = 0;
  6444. for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6445. if( ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6446. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6447. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit ) {
  6448. bS[i] = 1;
  6449. break;
  6450. }
  6451. }
  6452. }
  6453. }
  6454. if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
  6455. continue;
  6456. }
  6457. /* Filter edge */
  6458. // Do not use s->qscale as luma quantizer because it has not the same
  6459. // value in IPCM macroblocks.
  6460. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6461. //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]);
  6462. tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
  6463. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6464. if( dir == 0 ) {
  6465. filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
  6466. if( (edge&1) == 0 ) {
  6467. int chroma_qp = ( h->chroma_qp +
  6468. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6469. filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp );
  6470. filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp );
  6471. }
  6472. } else {
  6473. filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
  6474. if( (edge&1) == 0 ) {
  6475. int chroma_qp = ( h->chroma_qp +
  6476. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6477. filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6478. filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6479. }
  6480. }
  6481. }
  6482. }
  6483. }
  6484. static int decode_slice(H264Context *h){
  6485. MpegEncContext * const s = &h->s;
  6486. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  6487. s->mb_skip_run= -1;
  6488. if( h->pps.cabac ) {
  6489. int i;
  6490. /* realign */
  6491. align_get_bits( &s->gb );
  6492. /* init cabac */
  6493. ff_init_cabac_states( &h->cabac);
  6494. ff_init_cabac_decoder( &h->cabac,
  6495. s->gb.buffer + get_bits_count(&s->gb)/8,
  6496. ( s->gb.size_in_bits - get_bits_count(&s->gb) + 7)/8);
  6497. /* calculate pre-state */
  6498. for( i= 0; i < 460; i++ ) {
  6499. int pre;
  6500. if( h->slice_type == I_TYPE )
  6501. pre = clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
  6502. else
  6503. 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 );
  6504. if( pre <= 63 )
  6505. h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
  6506. else
  6507. h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
  6508. }
  6509. for(;;){
  6510. //START_TIMER
  6511. int ret = decode_mb_cabac(h);
  6512. int eos;
  6513. //STOP_TIMER("decode_mb_cabac")
  6514. if(ret>=0) hl_decode_mb(h);
  6515. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  6516. s->mb_y++;
  6517. if(ret>=0) ret = decode_mb_cabac(h);
  6518. if(ret>=0) hl_decode_mb(h);
  6519. s->mb_y--;
  6520. }
  6521. eos = get_cabac_terminate( &h->cabac );
  6522. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  6523. 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);
  6524. 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);
  6525. return -1;
  6526. }
  6527. if( ++s->mb_x >= s->mb_width ) {
  6528. s->mb_x = 0;
  6529. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6530. ++s->mb_y;
  6531. if(FRAME_MBAFF) {
  6532. ++s->mb_y;
  6533. }
  6534. }
  6535. if( eos || s->mb_y >= s->mb_height ) {
  6536. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6537. 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);
  6538. return 0;
  6539. }
  6540. }
  6541. } else {
  6542. for(;;){
  6543. int ret = decode_mb_cavlc(h);
  6544. if(ret>=0) hl_decode_mb(h);
  6545. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  6546. s->mb_y++;
  6547. ret = decode_mb_cavlc(h);
  6548. if(ret>=0) hl_decode_mb(h);
  6549. s->mb_y--;
  6550. }
  6551. if(ret<0){
  6552. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6553. 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);
  6554. return -1;
  6555. }
  6556. if(++s->mb_x >= s->mb_width){
  6557. s->mb_x=0;
  6558. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6559. ++s->mb_y;
  6560. if(FRAME_MBAFF) {
  6561. ++s->mb_y;
  6562. }
  6563. if(s->mb_y >= s->mb_height){
  6564. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6565. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  6566. 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);
  6567. return 0;
  6568. }else{
  6569. 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);
  6570. return -1;
  6571. }
  6572. }
  6573. }
  6574. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  6575. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6576. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  6577. 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);
  6578. return 0;
  6579. }else{
  6580. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
  6581. return -1;
  6582. }
  6583. }
  6584. }
  6585. }
  6586. #if 0
  6587. for(;s->mb_y < s->mb_height; s->mb_y++){
  6588. for(;s->mb_x < s->mb_width; s->mb_x++){
  6589. int ret= decode_mb(h);
  6590. hl_decode_mb(h);
  6591. if(ret<0){
  6592. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6593. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
  6594. return -1;
  6595. }
  6596. if(++s->mb_x >= s->mb_width){
  6597. s->mb_x=0;
  6598. if(++s->mb_y >= s->mb_height){
  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_END|DC_END|MV_END)&part_mask);
  6604. return -1;
  6605. }
  6606. }
  6607. }
  6608. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  6609. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6610. 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);
  6611. return 0;
  6612. }else{
  6613. 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);
  6614. return -1;
  6615. }
  6616. }
  6617. }
  6618. s->mb_x=0;
  6619. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6620. }
  6621. #endif
  6622. return -1; //not reached
  6623. }
  6624. static int decode_unregistered_user_data(H264Context *h, int size){
  6625. MpegEncContext * const s = &h->s;
  6626. uint8_t user_data[16+256];
  6627. int e, build, i;
  6628. if(size<16)
  6629. return -1;
  6630. for(i=0; i<sizeof(user_data)-1 && i<size; i++){
  6631. user_data[i]= get_bits(&s->gb, 8);
  6632. }
  6633. user_data[i]= 0;
  6634. e= sscanf(user_data+16, "x264 - core %d"/*%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html*/, &build);
  6635. if(e==1 && build>=0)
  6636. h->x264_build= build;
  6637. if(s->avctx->debug & FF_DEBUG_BUGS)
  6638. av_log(s->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data+16);
  6639. for(; i<size; i++)
  6640. skip_bits(&s->gb, 8);
  6641. return 0;
  6642. }
  6643. static int decode_sei(H264Context *h){
  6644. MpegEncContext * const s = &h->s;
  6645. while(get_bits_count(&s->gb) + 16 < s->gb.size_in_bits){
  6646. int size, type;
  6647. type=0;
  6648. do{
  6649. type+= show_bits(&s->gb, 8);
  6650. }while(get_bits(&s->gb, 8) == 255);
  6651. size=0;
  6652. do{
  6653. size+= show_bits(&s->gb, 8);
  6654. }while(get_bits(&s->gb, 8) == 255);
  6655. switch(type){
  6656. case 5:
  6657. if(decode_unregistered_user_data(h, size) < 0)
  6658. return -1;
  6659. break;
  6660. default:
  6661. skip_bits(&s->gb, 8*size);
  6662. }
  6663. //FIXME check bits here
  6664. align_get_bits(&s->gb);
  6665. }
  6666. return 0;
  6667. }
  6668. static inline void decode_hrd_parameters(H264Context *h, SPS *sps){
  6669. MpegEncContext * const s = &h->s;
  6670. int cpb_count, i;
  6671. cpb_count = get_ue_golomb(&s->gb) + 1;
  6672. get_bits(&s->gb, 4); /* bit_rate_scale */
  6673. get_bits(&s->gb, 4); /* cpb_size_scale */
  6674. for(i=0; i<cpb_count; i++){
  6675. get_ue_golomb(&s->gb); /* bit_rate_value_minus1 */
  6676. get_ue_golomb(&s->gb); /* cpb_size_value_minus1 */
  6677. get_bits1(&s->gb); /* cbr_flag */
  6678. }
  6679. get_bits(&s->gb, 5); /* initial_cpb_removal_delay_length_minus1 */
  6680. get_bits(&s->gb, 5); /* cpb_removal_delay_length_minus1 */
  6681. get_bits(&s->gb, 5); /* dpb_output_delay_length_minus1 */
  6682. get_bits(&s->gb, 5); /* time_offset_length */
  6683. }
  6684. static inline int decode_vui_parameters(H264Context *h, SPS *sps){
  6685. MpegEncContext * const s = &h->s;
  6686. int aspect_ratio_info_present_flag, aspect_ratio_idc;
  6687. int nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag;
  6688. aspect_ratio_info_present_flag= get_bits1(&s->gb);
  6689. if( aspect_ratio_info_present_flag ) {
  6690. aspect_ratio_idc= get_bits(&s->gb, 8);
  6691. if( aspect_ratio_idc == EXTENDED_SAR ) {
  6692. sps->sar.num= get_bits(&s->gb, 16);
  6693. sps->sar.den= get_bits(&s->gb, 16);
  6694. }else if(aspect_ratio_idc < 14){
  6695. sps->sar= pixel_aspect[aspect_ratio_idc];
  6696. }else{
  6697. av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
  6698. return -1;
  6699. }
  6700. }else{
  6701. sps->sar.num=
  6702. sps->sar.den= 0;
  6703. }
  6704. // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
  6705. if(get_bits1(&s->gb)){ /* overscan_info_present_flag */
  6706. get_bits1(&s->gb); /* overscan_appropriate_flag */
  6707. }
  6708. if(get_bits1(&s->gb)){ /* video_signal_type_present_flag */
  6709. get_bits(&s->gb, 3); /* video_format */
  6710. get_bits1(&s->gb); /* video_full_range_flag */
  6711. if(get_bits1(&s->gb)){ /* colour_description_present_flag */
  6712. get_bits(&s->gb, 8); /* colour_primaries */
  6713. get_bits(&s->gb, 8); /* transfer_characteristics */
  6714. get_bits(&s->gb, 8); /* matrix_coefficients */
  6715. }
  6716. }
  6717. if(get_bits1(&s->gb)){ /* chroma_location_info_present_flag */
  6718. get_ue_golomb(&s->gb); /* chroma_sample_location_type_top_field */
  6719. get_ue_golomb(&s->gb); /* chroma_sample_location_type_bottom_field */
  6720. }
  6721. sps->timing_info_present_flag = get_bits1(&s->gb);
  6722. if(sps->timing_info_present_flag){
  6723. sps->num_units_in_tick = get_bits_long(&s->gb, 32);
  6724. sps->time_scale = get_bits_long(&s->gb, 32);
  6725. sps->fixed_frame_rate_flag = get_bits1(&s->gb);
  6726. }
  6727. nal_hrd_parameters_present_flag = get_bits1(&s->gb);
  6728. if(nal_hrd_parameters_present_flag)
  6729. decode_hrd_parameters(h, sps);
  6730. vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
  6731. if(vcl_hrd_parameters_present_flag)
  6732. decode_hrd_parameters(h, sps);
  6733. if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag)
  6734. get_bits1(&s->gb); /* low_delay_hrd_flag */
  6735. get_bits1(&s->gb); /* pic_struct_present_flag */
  6736. sps->bitstream_restriction_flag = get_bits1(&s->gb);
  6737. if(sps->bitstream_restriction_flag){
  6738. get_bits1(&s->gb); /* motion_vectors_over_pic_boundaries_flag */
  6739. get_ue_golomb(&s->gb); /* max_bytes_per_pic_denom */
  6740. get_ue_golomb(&s->gb); /* max_bits_per_mb_denom */
  6741. get_ue_golomb(&s->gb); /* log2_max_mv_length_horizontal */
  6742. get_ue_golomb(&s->gb); /* log2_max_mv_length_vertical */
  6743. sps->num_reorder_frames = get_ue_golomb(&s->gb);
  6744. get_ue_golomb(&s->gb); /* max_dec_frame_buffering */
  6745. }
  6746. return 0;
  6747. }
  6748. static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,
  6749. const uint8_t *jvt_list, const uint8_t *fallback_list){
  6750. MpegEncContext * const s = &h->s;
  6751. int i, last = 8, next = 8;
  6752. const uint8_t *scan = size == 16 ? zigzag_scan : zigzag_scan8x8;
  6753. if(!get_bits1(&s->gb)) /* matrix not written, we use the predicted one */
  6754. memcpy(factors, fallback_list, size*sizeof(uint8_t));
  6755. else
  6756. for(i=0;i<size;i++){
  6757. if(next)
  6758. next = (last + get_se_golomb(&s->gb)) & 0xff;
  6759. if(!i && !next){ /* matrix not written, we use the preset one */
  6760. memcpy(factors, jvt_list, size*sizeof(uint8_t));
  6761. break;
  6762. }
  6763. last = factors[scan[i]] = next ? next : last;
  6764. }
  6765. }
  6766. static void decode_scaling_matrices(H264Context *h, SPS *sps, PPS *pps, int is_sps,
  6767. uint8_t (*scaling_matrix4)[16], uint8_t (*scaling_matrix8)[64]){
  6768. MpegEncContext * const s = &h->s;
  6769. int fallback_sps = !is_sps && sps->scaling_matrix_present;
  6770. const uint8_t *fallback[4] = {
  6771. fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
  6772. fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
  6773. fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
  6774. fallback_sps ? sps->scaling_matrix8[1] : default_scaling8[1]
  6775. };
  6776. if(get_bits1(&s->gb)){
  6777. sps->scaling_matrix_present |= is_sps;
  6778. decode_scaling_list(h,scaling_matrix4[0],16,default_scaling4[0],fallback[0]); // Intra, Y
  6779. decode_scaling_list(h,scaling_matrix4[1],16,default_scaling4[0],scaling_matrix4[0]); // Intra, Cr
  6780. decode_scaling_list(h,scaling_matrix4[2],16,default_scaling4[0],scaling_matrix4[1]); // Intra, Cb
  6781. decode_scaling_list(h,scaling_matrix4[3],16,default_scaling4[1],fallback[1]); // Inter, Y
  6782. decode_scaling_list(h,scaling_matrix4[4],16,default_scaling4[1],scaling_matrix4[3]); // Inter, Cr
  6783. decode_scaling_list(h,scaling_matrix4[5],16,default_scaling4[1],scaling_matrix4[4]); // Inter, Cb
  6784. if(is_sps || pps->transform_8x8_mode){
  6785. decode_scaling_list(h,scaling_matrix8[0],64,default_scaling8[0],fallback[2]); // Intra, Y
  6786. decode_scaling_list(h,scaling_matrix8[1],64,default_scaling8[1],fallback[3]); // Inter, Y
  6787. }
  6788. } else if(fallback_sps) {
  6789. memcpy(scaling_matrix4, sps->scaling_matrix4, 6*16*sizeof(uint8_t));
  6790. memcpy(scaling_matrix8, sps->scaling_matrix8, 2*64*sizeof(uint8_t));
  6791. }
  6792. }
  6793. static inline int decode_seq_parameter_set(H264Context *h){
  6794. MpegEncContext * const s = &h->s;
  6795. int profile_idc, level_idc;
  6796. unsigned int sps_id;
  6797. int i;
  6798. SPS *sps;
  6799. profile_idc= get_bits(&s->gb, 8);
  6800. get_bits1(&s->gb); //constraint_set0_flag
  6801. get_bits1(&s->gb); //constraint_set1_flag
  6802. get_bits1(&s->gb); //constraint_set2_flag
  6803. get_bits1(&s->gb); //constraint_set3_flag
  6804. get_bits(&s->gb, 4); // reserved
  6805. level_idc= get_bits(&s->gb, 8);
  6806. sps_id= get_ue_golomb(&s->gb);
  6807. if (sps_id >= MAX_SPS_COUNT){
  6808. // ok it has gone out of hand, someone is sending us bad stuff.
  6809. av_log(h->s.avctx, AV_LOG_ERROR, "illegal sps_id (%d)\n", sps_id);
  6810. return -1;
  6811. }
  6812. sps= &h->sps_buffer[ sps_id ];
  6813. sps->profile_idc= profile_idc;
  6814. sps->level_idc= level_idc;
  6815. if(sps->profile_idc >= 100){ //high profile
  6816. if(get_ue_golomb(&s->gb) == 3) //chroma_format_idc
  6817. get_bits1(&s->gb); //residual_color_transform_flag
  6818. get_ue_golomb(&s->gb); //bit_depth_luma_minus8
  6819. get_ue_golomb(&s->gb); //bit_depth_chroma_minus8
  6820. sps->transform_bypass = get_bits1(&s->gb);
  6821. decode_scaling_matrices(h, sps, NULL, 1, sps->scaling_matrix4, sps->scaling_matrix8);
  6822. }else
  6823. sps->scaling_matrix_present = 0;
  6824. sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
  6825. sps->poc_type= get_ue_golomb(&s->gb);
  6826. if(sps->poc_type == 0){ //FIXME #define
  6827. sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
  6828. } else if(sps->poc_type == 1){//FIXME #define
  6829. sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
  6830. sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
  6831. sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
  6832. sps->poc_cycle_length= get_ue_golomb(&s->gb);
  6833. for(i=0; i<sps->poc_cycle_length; i++)
  6834. sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
  6835. }
  6836. if(sps->poc_type > 2){
  6837. av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
  6838. return -1;
  6839. }
  6840. sps->ref_frame_count= get_ue_golomb(&s->gb);
  6841. if(sps->ref_frame_count > MAX_PICTURE_COUNT-2){
  6842. av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");
  6843. }
  6844. sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
  6845. sps->mb_width= get_ue_golomb(&s->gb) + 1;
  6846. sps->mb_height= get_ue_golomb(&s->gb) + 1;
  6847. if((unsigned)sps->mb_width >= INT_MAX/16 || (unsigned)sps->mb_height >= INT_MAX/16 ||
  6848. avcodec_check_dimensions(NULL, 16*sps->mb_width, 16*sps->mb_height))
  6849. return -1;
  6850. sps->frame_mbs_only_flag= get_bits1(&s->gb);
  6851. if(!sps->frame_mbs_only_flag)
  6852. sps->mb_aff= get_bits1(&s->gb);
  6853. else
  6854. sps->mb_aff= 0;
  6855. sps->direct_8x8_inference_flag= get_bits1(&s->gb);
  6856. #ifndef ALLOW_INTERLACE
  6857. if(sps->mb_aff)
  6858. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF support not included; enable it at compile-time.\n");
  6859. #endif
  6860. if(!sps->direct_8x8_inference_flag && sps->mb_aff)
  6861. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + !direct_8x8_inference is not implemented\n");
  6862. sps->crop= get_bits1(&s->gb);
  6863. if(sps->crop){
  6864. sps->crop_left = get_ue_golomb(&s->gb);
  6865. sps->crop_right = get_ue_golomb(&s->gb);
  6866. sps->crop_top = get_ue_golomb(&s->gb);
  6867. sps->crop_bottom= get_ue_golomb(&s->gb);
  6868. if(sps->crop_left || sps->crop_top){
  6869. av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ...\n");
  6870. }
  6871. }else{
  6872. sps->crop_left =
  6873. sps->crop_right =
  6874. sps->crop_top =
  6875. sps->crop_bottom= 0;
  6876. }
  6877. sps->vui_parameters_present_flag= get_bits1(&s->gb);
  6878. if( sps->vui_parameters_present_flag )
  6879. decode_vui_parameters(h, sps);
  6880. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6881. av_log(h->s.avctx, AV_LOG_DEBUG, "sps:%u profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%d/%d/%d/%d %s\n",
  6882. sps_id, sps->profile_idc, sps->level_idc,
  6883. sps->poc_type,
  6884. sps->ref_frame_count,
  6885. sps->mb_width, sps->mb_height,
  6886. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  6887. sps->direct_8x8_inference_flag ? "8B8" : "",
  6888. sps->crop_left, sps->crop_right,
  6889. sps->crop_top, sps->crop_bottom,
  6890. sps->vui_parameters_present_flag ? "VUI" : ""
  6891. );
  6892. }
  6893. return 0;
  6894. }
  6895. static inline int decode_picture_parameter_set(H264Context *h, int bit_length){
  6896. MpegEncContext * const s = &h->s;
  6897. unsigned int pps_id= get_ue_golomb(&s->gb);
  6898. PPS *pps;
  6899. if(pps_id>=MAX_PPS_COUNT){
  6900. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  6901. return -1;
  6902. }
  6903. pps = &h->pps_buffer[pps_id];
  6904. pps->sps_id= get_ue_golomb(&s->gb);
  6905. pps->cabac= get_bits1(&s->gb);
  6906. pps->pic_order_present= get_bits1(&s->gb);
  6907. pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
  6908. if(pps->slice_group_count > 1 ){
  6909. pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
  6910. av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
  6911. switch(pps->mb_slice_group_map_type){
  6912. case 0:
  6913. #if 0
  6914. | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
  6915. | run_length[ i ] |1 |ue(v) |
  6916. #endif
  6917. break;
  6918. case 2:
  6919. #if 0
  6920. | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
  6921. |{ | | |
  6922. | top_left_mb[ i ] |1 |ue(v) |
  6923. | bottom_right_mb[ i ] |1 |ue(v) |
  6924. | } | | |
  6925. #endif
  6926. break;
  6927. case 3:
  6928. case 4:
  6929. case 5:
  6930. #if 0
  6931. | slice_group_change_direction_flag |1 |u(1) |
  6932. | slice_group_change_rate_minus1 |1 |ue(v) |
  6933. #endif
  6934. break;
  6935. case 6:
  6936. #if 0
  6937. | slice_group_id_cnt_minus1 |1 |ue(v) |
  6938. | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
  6939. |) | | |
  6940. | slice_group_id[ i ] |1 |u(v) |
  6941. #endif
  6942. break;
  6943. }
  6944. }
  6945. pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  6946. pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  6947. if(pps->ref_count[0] > 32 || pps->ref_count[1] > 32){
  6948. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
  6949. return -1;
  6950. }
  6951. pps->weighted_pred= get_bits1(&s->gb);
  6952. pps->weighted_bipred_idc= get_bits(&s->gb, 2);
  6953. pps->init_qp= get_se_golomb(&s->gb) + 26;
  6954. pps->init_qs= get_se_golomb(&s->gb) + 26;
  6955. pps->chroma_qp_index_offset= get_se_golomb(&s->gb);
  6956. pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
  6957. pps->constrained_intra_pred= get_bits1(&s->gb);
  6958. pps->redundant_pic_cnt_present = get_bits1(&s->gb);
  6959. pps->transform_8x8_mode= 0;
  6960. h->dequant_coeff_pps= -1; //contents of sps/pps can change even if id doesn't, so reinit
  6961. memset(pps->scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  6962. memset(pps->scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  6963. if(get_bits_count(&s->gb) < bit_length){
  6964. pps->transform_8x8_mode= get_bits1(&s->gb);
  6965. decode_scaling_matrices(h, &h->sps_buffer[pps->sps_id], pps, 0, pps->scaling_matrix4, pps->scaling_matrix8);
  6966. get_se_golomb(&s->gb); //second_chroma_qp_index_offset
  6967. }
  6968. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6969. av_log(h->s.avctx, AV_LOG_DEBUG, "pps:%u sps:%u %s slice_groups:%d ref:%d/%d %s qp:%d/%d/%d %s %s %s %s\n",
  6970. pps_id, pps->sps_id,
  6971. pps->cabac ? "CABAC" : "CAVLC",
  6972. pps->slice_group_count,
  6973. pps->ref_count[0], pps->ref_count[1],
  6974. pps->weighted_pred ? "weighted" : "",
  6975. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset,
  6976. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  6977. pps->constrained_intra_pred ? "CONSTR" : "",
  6978. pps->redundant_pic_cnt_present ? "REDU" : "",
  6979. pps->transform_8x8_mode ? "8x8DCT" : ""
  6980. );
  6981. }
  6982. return 0;
  6983. }
  6984. /**
  6985. * finds the end of the current frame in the bitstream.
  6986. * @return the position of the first byte of the next frame, or -1
  6987. */
  6988. static int find_frame_end(H264Context *h, const uint8_t *buf, int buf_size){
  6989. int i;
  6990. uint32_t state;
  6991. ParseContext *pc = &(h->s.parse_context);
  6992. //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
  6993. // mb_addr= pc->mb_addr - 1;
  6994. state= pc->state;
  6995. for(i=0; i<=buf_size; i++){
  6996. if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  6997. tprintf("find_frame_end new startcode = %08x, frame_start_found = %d, pos = %d\n", state, pc->frame_start_found, i);
  6998. if(pc->frame_start_found){
  6999. // If there isn't one more byte in the buffer
  7000. // the test on first_mb_in_slice cannot be done yet
  7001. // do it at next call.
  7002. if (i >= buf_size) break;
  7003. if (buf[i] & 0x80) {
  7004. // first_mb_in_slice is 0, probably the first nal of a new
  7005. // slice
  7006. tprintf("find_frame_end frame_end_found, state = %08x, pos = %d\n", state, i);
  7007. pc->state=-1;
  7008. pc->frame_start_found= 0;
  7009. return i-4;
  7010. }
  7011. }
  7012. pc->frame_start_found = 1;
  7013. }
  7014. if((state&0xFFFFFF1F) == 0x107 || (state&0xFFFFFF1F) == 0x108 || (state&0xFFFFFF1F) == 0x109){
  7015. if(pc->frame_start_found){
  7016. pc->state=-1;
  7017. pc->frame_start_found= 0;
  7018. return i-4;
  7019. }
  7020. }
  7021. if (i<buf_size)
  7022. state= (state<<8) | buf[i];
  7023. }
  7024. pc->state= state;
  7025. return END_NOT_FOUND;
  7026. }
  7027. #ifdef CONFIG_H264_PARSER
  7028. static int h264_parse(AVCodecParserContext *s,
  7029. AVCodecContext *avctx,
  7030. uint8_t **poutbuf, int *poutbuf_size,
  7031. const uint8_t *buf, int buf_size)
  7032. {
  7033. H264Context *h = s->priv_data;
  7034. ParseContext *pc = &h->s.parse_context;
  7035. int next;
  7036. next= find_frame_end(h, buf, buf_size);
  7037. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  7038. *poutbuf = NULL;
  7039. *poutbuf_size = 0;
  7040. return buf_size;
  7041. }
  7042. *poutbuf = (uint8_t *)buf;
  7043. *poutbuf_size = buf_size;
  7044. return next;
  7045. }
  7046. static int h264_split(AVCodecContext *avctx,
  7047. const uint8_t *buf, int buf_size)
  7048. {
  7049. int i;
  7050. uint32_t state = -1;
  7051. int has_sps= 0;
  7052. for(i=0; i<=buf_size; i++){
  7053. if((state&0xFFFFFF1F) == 0x107)
  7054. has_sps=1;
  7055. /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  7056. }*/
  7057. if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
  7058. if(has_sps){
  7059. while(i>4 && buf[i-5]==0) i--;
  7060. return i-4;
  7061. }
  7062. }
  7063. if (i<buf_size)
  7064. state= (state<<8) | buf[i];
  7065. }
  7066. return 0;
  7067. }
  7068. #endif /* CONFIG_H264_PARSER */
  7069. static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){
  7070. MpegEncContext * const s = &h->s;
  7071. AVCodecContext * const avctx= s->avctx;
  7072. int buf_index=0;
  7073. #if 0
  7074. int i;
  7075. for(i=0; i<50; i++){
  7076. av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
  7077. }
  7078. #endif
  7079. h->slice_num = 0;
  7080. s->current_picture_ptr= NULL;
  7081. for(;;){
  7082. int consumed;
  7083. int dst_length;
  7084. int bit_length;
  7085. uint8_t *ptr;
  7086. int i, nalsize = 0;
  7087. if(h->is_avc) {
  7088. if(buf_index >= buf_size) break;
  7089. nalsize = 0;
  7090. for(i = 0; i < h->nal_length_size; i++)
  7091. nalsize = (nalsize << 8) | buf[buf_index++];
  7092. if(nalsize <= 1 || nalsize > buf_size){
  7093. if(nalsize == 1){
  7094. buf_index++;
  7095. continue;
  7096. }else{
  7097. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  7098. break;
  7099. }
  7100. }
  7101. } else {
  7102. // start code prefix search
  7103. for(; buf_index + 3 < buf_size; buf_index++){
  7104. // this should allways succeed in the first iteration
  7105. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  7106. break;
  7107. }
  7108. if(buf_index+3 >= buf_size) break;
  7109. buf_index+=3;
  7110. }
  7111. ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, h->is_avc ? nalsize : buf_size - buf_index);
  7112. if (ptr==NULL || dst_length <= 0){
  7113. return -1;
  7114. }
  7115. while(ptr[dst_length - 1] == 0 && dst_length > 1)
  7116. dst_length--;
  7117. bit_length= 8*dst_length - decode_rbsp_trailing(ptr + dst_length - 1);
  7118. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  7119. 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);
  7120. }
  7121. if (h->is_avc && (nalsize != consumed))
  7122. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  7123. buf_index += consumed;
  7124. if( (s->hurry_up == 1 && h->nal_ref_idc == 0) //FIXME dont discard SEI id
  7125. ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  7126. continue;
  7127. switch(h->nal_unit_type){
  7128. case NAL_IDR_SLICE:
  7129. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  7130. case NAL_SLICE:
  7131. init_get_bits(&s->gb, ptr, bit_length);
  7132. h->intra_gb_ptr=
  7133. h->inter_gb_ptr= &s->gb;
  7134. s->data_partitioning = 0;
  7135. if(decode_slice_header(h) < 0){
  7136. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  7137. break;
  7138. }
  7139. s->current_picture_ptr->key_frame= (h->nal_unit_type == NAL_IDR_SLICE);
  7140. if(h->redundant_pic_count==0 && 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_DPA:
  7148. init_get_bits(&s->gb, ptr, bit_length);
  7149. h->intra_gb_ptr=
  7150. h->inter_gb_ptr= NULL;
  7151. s->data_partitioning = 1;
  7152. if(decode_slice_header(h) < 0){
  7153. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  7154. }
  7155. break;
  7156. case NAL_DPB:
  7157. init_get_bits(&h->intra_gb, ptr, bit_length);
  7158. h->intra_gb_ptr= &h->intra_gb;
  7159. break;
  7160. case NAL_DPC:
  7161. init_get_bits(&h->inter_gb, ptr, bit_length);
  7162. h->inter_gb_ptr= &h->inter_gb;
  7163. if(h->redundant_pic_count==0 && h->intra_gb_ptr && s->data_partitioning
  7164. && s->context_initialized
  7165. && s->hurry_up < 5
  7166. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  7167. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  7168. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  7169. && avctx->skip_frame < AVDISCARD_ALL)
  7170. decode_slice(h);
  7171. break;
  7172. case NAL_SEI:
  7173. init_get_bits(&s->gb, ptr, bit_length);
  7174. decode_sei(h);
  7175. break;
  7176. case NAL_SPS:
  7177. init_get_bits(&s->gb, ptr, bit_length);
  7178. decode_seq_parameter_set(h);
  7179. if(s->flags& CODEC_FLAG_LOW_DELAY)
  7180. s->low_delay=1;
  7181. if(avctx->has_b_frames < 2)
  7182. avctx->has_b_frames= !s->low_delay;
  7183. break;
  7184. case NAL_PPS:
  7185. init_get_bits(&s->gb, ptr, bit_length);
  7186. decode_picture_parameter_set(h, bit_length);
  7187. break;
  7188. case NAL_AUD:
  7189. case NAL_END_SEQUENCE:
  7190. case NAL_END_STREAM:
  7191. case NAL_FILLER_DATA:
  7192. case NAL_SPS_EXT:
  7193. case NAL_AUXILIARY_SLICE:
  7194. break;
  7195. default:
  7196. av_log(avctx, AV_LOG_ERROR, "Unknown NAL code: %d\n", h->nal_unit_type);
  7197. }
  7198. }
  7199. if(!s->current_picture_ptr) return buf_index; //no frame
  7200. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  7201. s->current_picture_ptr->pict_type= s->pict_type;
  7202. h->prev_frame_num_offset= h->frame_num_offset;
  7203. h->prev_frame_num= h->frame_num;
  7204. if(s->current_picture_ptr->reference){
  7205. h->prev_poc_msb= h->poc_msb;
  7206. h->prev_poc_lsb= h->poc_lsb;
  7207. }
  7208. if(s->current_picture_ptr->reference)
  7209. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  7210. ff_er_frame_end(s);
  7211. MPV_frame_end(s);
  7212. return buf_index;
  7213. }
  7214. /**
  7215. * returns the number of bytes consumed for building the current frame
  7216. */
  7217. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  7218. if(s->flags&CODEC_FLAG_TRUNCATED){
  7219. pos -= s->parse_context.last_index;
  7220. if(pos<0) pos=0; // FIXME remove (unneeded?)
  7221. return pos;
  7222. }else{
  7223. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  7224. if(pos+10>buf_size) pos=buf_size; // oops ;)
  7225. return pos;
  7226. }
  7227. }
  7228. static int decode_frame(AVCodecContext *avctx,
  7229. void *data, int *data_size,
  7230. uint8_t *buf, int buf_size)
  7231. {
  7232. H264Context *h = avctx->priv_data;
  7233. MpegEncContext *s = &h->s;
  7234. AVFrame *pict = data;
  7235. int buf_index;
  7236. s->flags= avctx->flags;
  7237. s->flags2= avctx->flags2;
  7238. /* no supplementary picture */
  7239. if (buf_size == 0) {
  7240. return 0;
  7241. }
  7242. if(s->flags&CODEC_FLAG_TRUNCATED){
  7243. int next= find_frame_end(h, buf, buf_size);
  7244. if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
  7245. return buf_size;
  7246. //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);
  7247. }
  7248. if(h->is_avc && !h->got_avcC) {
  7249. int i, cnt, nalsize;
  7250. unsigned char *p = avctx->extradata;
  7251. if(avctx->extradata_size < 7) {
  7252. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  7253. return -1;
  7254. }
  7255. if(*p != 1) {
  7256. av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
  7257. return -1;
  7258. }
  7259. /* sps and pps in the avcC always have length coded with 2 bytes,
  7260. so put a fake nal_length_size = 2 while parsing them */
  7261. h->nal_length_size = 2;
  7262. // Decode sps from avcC
  7263. cnt = *(p+5) & 0x1f; // Number of sps
  7264. p += 6;
  7265. for (i = 0; i < cnt; i++) {
  7266. nalsize = BE_16(p) + 2;
  7267. if(decode_nal_units(h, p, nalsize) < 0) {
  7268. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  7269. return -1;
  7270. }
  7271. p += nalsize;
  7272. }
  7273. // Decode pps from avcC
  7274. cnt = *(p++); // Number of pps
  7275. for (i = 0; i < cnt; i++) {
  7276. nalsize = BE_16(p) + 2;
  7277. if(decode_nal_units(h, p, nalsize) != nalsize) {
  7278. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  7279. return -1;
  7280. }
  7281. p += nalsize;
  7282. }
  7283. // Now store right nal length size, that will be use to parse all other nals
  7284. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  7285. // Do not reparse avcC
  7286. h->got_avcC = 1;
  7287. }
  7288. if(!h->is_avc && s->avctx->extradata_size && s->picture_number==0){
  7289. if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
  7290. return -1;
  7291. }
  7292. buf_index=decode_nal_units(h, buf, buf_size);
  7293. if(buf_index < 0)
  7294. return -1;
  7295. //FIXME do something with unavailable reference frames
  7296. // if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_index, buf_size);
  7297. if(!s->current_picture_ptr){
  7298. av_log(h->s.avctx, AV_LOG_DEBUG, "error, NO frame\n");
  7299. return -1;
  7300. }
  7301. {
  7302. Picture *out = s->current_picture_ptr;
  7303. #if 0 //decode order
  7304. *data_size = sizeof(AVFrame);
  7305. #else
  7306. /* Sort B-frames into display order */
  7307. Picture *cur = s->current_picture_ptr;
  7308. Picture *prev = h->delayed_output_pic;
  7309. int i, pics, cross_idr, out_of_order, out_idx;
  7310. if(h->sps.bitstream_restriction_flag
  7311. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  7312. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  7313. s->low_delay = 0;
  7314. }
  7315. pics = 0;
  7316. while(h->delayed_pic[pics]) pics++;
  7317. h->delayed_pic[pics++] = cur;
  7318. if(cur->reference == 0)
  7319. cur->reference = 1;
  7320. cross_idr = 0;
  7321. for(i=0; h->delayed_pic[i]; i++)
  7322. if(h->delayed_pic[i]->key_frame || h->delayed_pic[i]->poc==0)
  7323. cross_idr = 1;
  7324. out = h->delayed_pic[0];
  7325. out_idx = 0;
  7326. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
  7327. if(h->delayed_pic[i]->poc < out->poc){
  7328. out = h->delayed_pic[i];
  7329. out_idx = i;
  7330. }
  7331. out_of_order = !cross_idr && prev && out->poc < prev->poc;
  7332. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  7333. { }
  7334. else if(prev && pics <= s->avctx->has_b_frames)
  7335. out = prev;
  7336. else if((out_of_order && pics-1 == s->avctx->has_b_frames && pics < 15)
  7337. || (s->low_delay &&
  7338. ((!cross_idr && prev && out->poc > prev->poc + 2)
  7339. || cur->pict_type == B_TYPE)))
  7340. {
  7341. s->low_delay = 0;
  7342. s->avctx->has_b_frames++;
  7343. out = prev;
  7344. }
  7345. else if(out_of_order)
  7346. out = prev;
  7347. if(out_of_order || pics > s->avctx->has_b_frames){
  7348. for(i=out_idx; h->delayed_pic[i]; i++)
  7349. h->delayed_pic[i] = h->delayed_pic[i+1];
  7350. }
  7351. if(prev == out)
  7352. *data_size = 0;
  7353. else
  7354. *data_size = sizeof(AVFrame);
  7355. if(prev && prev != out && prev->reference == 1)
  7356. prev->reference = 0;
  7357. h->delayed_output_pic = out;
  7358. #endif
  7359. if(out)
  7360. *pict= *(AVFrame*)out;
  7361. else
  7362. av_log(avctx, AV_LOG_DEBUG, "no picture\n");
  7363. }
  7364. assert(pict->data[0] || !*data_size);
  7365. ff_print_debug_info(s, pict);
  7366. //printf("out %d\n", (int)pict->data[0]);
  7367. #if 0 //?
  7368. /* Return the Picture timestamp as the frame number */
  7369. /* we substract 1 because it is added on utils.c */
  7370. avctx->frame_number = s->picture_number - 1;
  7371. #endif
  7372. return get_consumed_bytes(s, buf_index, buf_size);
  7373. }
  7374. #if 0
  7375. static inline void fill_mb_avail(H264Context *h){
  7376. MpegEncContext * const s = &h->s;
  7377. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  7378. if(s->mb_y){
  7379. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  7380. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  7381. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  7382. }else{
  7383. h->mb_avail[0]=
  7384. h->mb_avail[1]=
  7385. h->mb_avail[2]= 0;
  7386. }
  7387. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  7388. h->mb_avail[4]= 1; //FIXME move out
  7389. h->mb_avail[5]= 0; //FIXME move out
  7390. }
  7391. #endif
  7392. #if 0 //selftest
  7393. #define COUNT 8000
  7394. #define SIZE (COUNT*40)
  7395. int main(){
  7396. int i;
  7397. uint8_t temp[SIZE];
  7398. PutBitContext pb;
  7399. GetBitContext gb;
  7400. // int int_temp[10000];
  7401. DSPContext dsp;
  7402. AVCodecContext avctx;
  7403. dsputil_init(&dsp, &avctx);
  7404. init_put_bits(&pb, temp, SIZE);
  7405. printf("testing unsigned exp golomb\n");
  7406. for(i=0; i<COUNT; i++){
  7407. START_TIMER
  7408. set_ue_golomb(&pb, i);
  7409. STOP_TIMER("set_ue_golomb");
  7410. }
  7411. flush_put_bits(&pb);
  7412. init_get_bits(&gb, temp, 8*SIZE);
  7413. for(i=0; i<COUNT; i++){
  7414. int j, s;
  7415. s= show_bits(&gb, 24);
  7416. START_TIMER
  7417. j= get_ue_golomb(&gb);
  7418. if(j != i){
  7419. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7420. // return -1;
  7421. }
  7422. STOP_TIMER("get_ue_golomb");
  7423. }
  7424. init_put_bits(&pb, temp, SIZE);
  7425. printf("testing signed exp golomb\n");
  7426. for(i=0; i<COUNT; i++){
  7427. START_TIMER
  7428. set_se_golomb(&pb, i - COUNT/2);
  7429. STOP_TIMER("set_se_golomb");
  7430. }
  7431. flush_put_bits(&pb);
  7432. init_get_bits(&gb, temp, 8*SIZE);
  7433. for(i=0; i<COUNT; i++){
  7434. int j, s;
  7435. s= show_bits(&gb, 24);
  7436. START_TIMER
  7437. j= get_se_golomb(&gb);
  7438. if(j != i - COUNT/2){
  7439. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7440. // return -1;
  7441. }
  7442. STOP_TIMER("get_se_golomb");
  7443. }
  7444. printf("testing 4x4 (I)DCT\n");
  7445. DCTELEM block[16];
  7446. uint8_t src[16], ref[16];
  7447. uint64_t error= 0, max_error=0;
  7448. for(i=0; i<COUNT; i++){
  7449. int j;
  7450. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  7451. for(j=0; j<16; j++){
  7452. ref[j]= random()%255;
  7453. src[j]= random()%255;
  7454. }
  7455. h264_diff_dct_c(block, src, ref, 4);
  7456. //normalize
  7457. for(j=0; j<16; j++){
  7458. // printf("%d ", block[j]);
  7459. block[j]= block[j]*4;
  7460. if(j&1) block[j]= (block[j]*4 + 2)/5;
  7461. if(j&4) block[j]= (block[j]*4 + 2)/5;
  7462. }
  7463. // printf("\n");
  7464. s->dsp.h264_idct_add(ref, block, 4);
  7465. /* for(j=0; j<16; j++){
  7466. printf("%d ", ref[j]);
  7467. }
  7468. printf("\n");*/
  7469. for(j=0; j<16; j++){
  7470. int diff= FFABS(src[j] - ref[j]);
  7471. error+= diff*diff;
  7472. max_error= FFMAX(max_error, diff);
  7473. }
  7474. }
  7475. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  7476. #if 0
  7477. printf("testing quantizer\n");
  7478. for(qp=0; qp<52; qp++){
  7479. for(i=0; i<16; i++)
  7480. src1_block[i]= src2_block[i]= random()%255;
  7481. }
  7482. #endif
  7483. printf("Testing NAL layer\n");
  7484. uint8_t bitstream[COUNT];
  7485. uint8_t nal[COUNT*2];
  7486. H264Context h;
  7487. memset(&h, 0, sizeof(H264Context));
  7488. for(i=0; i<COUNT; i++){
  7489. int zeros= i;
  7490. int nal_length;
  7491. int consumed;
  7492. int out_length;
  7493. uint8_t *out;
  7494. int j;
  7495. for(j=0; j<COUNT; j++){
  7496. bitstream[j]= (random() % 255) + 1;
  7497. }
  7498. for(j=0; j<zeros; j++){
  7499. int pos= random() % COUNT;
  7500. while(bitstream[pos] == 0){
  7501. pos++;
  7502. pos %= COUNT;
  7503. }
  7504. bitstream[pos]=0;
  7505. }
  7506. START_TIMER
  7507. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  7508. if(nal_length<0){
  7509. printf("encoding failed\n");
  7510. return -1;
  7511. }
  7512. out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
  7513. STOP_TIMER("NAL")
  7514. if(out_length != COUNT){
  7515. printf("incorrect length %d %d\n", out_length, COUNT);
  7516. return -1;
  7517. }
  7518. if(consumed != nal_length){
  7519. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  7520. return -1;
  7521. }
  7522. if(memcmp(bitstream, out, COUNT)){
  7523. printf("missmatch\n");
  7524. return -1;
  7525. }
  7526. }
  7527. printf("Testing RBSP\n");
  7528. return 0;
  7529. }
  7530. #endif
  7531. static int decode_end(AVCodecContext *avctx)
  7532. {
  7533. H264Context *h = avctx->priv_data;
  7534. MpegEncContext *s = &h->s;
  7535. av_freep(&h->rbsp_buffer);
  7536. free_tables(h); //FIXME cleanup init stuff perhaps
  7537. MPV_common_end(s);
  7538. // memset(h, 0, sizeof(H264Context));
  7539. return 0;
  7540. }
  7541. AVCodec h264_decoder = {
  7542. "h264",
  7543. CODEC_TYPE_VIDEO,
  7544. CODEC_ID_H264,
  7545. sizeof(H264Context),
  7546. decode_init,
  7547. NULL,
  7548. decode_end,
  7549. decode_frame,
  7550. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  7551. .flush= flush_dpb,
  7552. };
  7553. #ifdef CONFIG_H264_PARSER
  7554. AVCodecParser h264_parser = {
  7555. { CODEC_ID_H264 },
  7556. sizeof(H264Context),
  7557. NULL,
  7558. h264_parse,
  7559. ff_parse_close,
  7560. h264_split,
  7561. };
  7562. #endif
  7563. #include "svq3.c"