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.

8571 lines
321KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. /**
  23. * @file h264.c
  24. * H.264 / AVC / MPEG4 part10 codec.
  25. * @author Michael Niedermayer <michaelni@gmx.at>
  26. */
  27. #include "common.h"
  28. #include "dsputil.h"
  29. #include "avcodec.h"
  30. #include "mpegvideo.h"
  31. #include "h264data.h"
  32. #include "golomb.h"
  33. #include "cabac.h"
  34. //#undef NDEBUG
  35. #include <assert.h>
  36. #define interlaced_dct interlaced_dct_is_a_bad_name
  37. #define mb_intra mb_intra_isnt_initalized_see_mb_type
  38. #define LUMA_DC_BLOCK_INDEX 25
  39. #define CHROMA_DC_BLOCK_INDEX 26
  40. #define CHROMA_DC_COEFF_TOKEN_VLC_BITS 8
  41. #define COEFF_TOKEN_VLC_BITS 8
  42. #define TOTAL_ZEROS_VLC_BITS 9
  43. #define CHROMA_DC_TOTAL_ZEROS_VLC_BITS 3
  44. #define RUN_VLC_BITS 3
  45. #define RUN7_VLC_BITS 6
  46. #define MAX_SPS_COUNT 32
  47. #define MAX_PPS_COUNT 256
  48. #define MAX_MMCO_COUNT 66
  49. /* Compiling in interlaced support reduces the speed
  50. * of progressive decoding by about 2%. */
  51. #define ALLOW_INTERLACE
  52. #ifdef ALLOW_INTERLACE
  53. #define MB_MBAFF h->mb_mbaff
  54. #define MB_FIELD h->mb_field_decoding_flag
  55. #define FRAME_MBAFF h->mb_aff_frame
  56. #else
  57. #define MB_MBAFF 0
  58. #define MB_FIELD 0
  59. #define FRAME_MBAFF 0
  60. #undef IS_INTERLACED
  61. #define IS_INTERLACED(mb_type) 0
  62. #endif
  63. /**
  64. * Sequence parameter set
  65. */
  66. typedef struct SPS{
  67. int profile_idc;
  68. int level_idc;
  69. int transform_bypass; ///< qpprime_y_zero_transform_bypass_flag
  70. int log2_max_frame_num; ///< log2_max_frame_num_minus4 + 4
  71. int poc_type; ///< pic_order_cnt_type
  72. int log2_max_poc_lsb; ///< log2_max_pic_order_cnt_lsb_minus4
  73. int delta_pic_order_always_zero_flag;
  74. int offset_for_non_ref_pic;
  75. int offset_for_top_to_bottom_field;
  76. int poc_cycle_length; ///< num_ref_frames_in_pic_order_cnt_cycle
  77. int ref_frame_count; ///< num_ref_frames
  78. int gaps_in_frame_num_allowed_flag;
  79. int mb_width; ///< frame_width_in_mbs_minus1 + 1
  80. int mb_height; ///< frame_height_in_mbs_minus1 + 1
  81. int frame_mbs_only_flag;
  82. int mb_aff; ///<mb_adaptive_frame_field_flag
  83. int direct_8x8_inference_flag;
  84. int crop; ///< frame_cropping_flag
  85. int crop_left; ///< frame_cropping_rect_left_offset
  86. int crop_right; ///< frame_cropping_rect_right_offset
  87. int crop_top; ///< frame_cropping_rect_top_offset
  88. int crop_bottom; ///< frame_cropping_rect_bottom_offset
  89. int vui_parameters_present_flag;
  90. AVRational sar;
  91. int timing_info_present_flag;
  92. uint32_t num_units_in_tick;
  93. uint32_t time_scale;
  94. int fixed_frame_rate_flag;
  95. short offset_for_ref_frame[256]; //FIXME dyn aloc?
  96. int bitstream_restriction_flag;
  97. int num_reorder_frames;
  98. int scaling_matrix_present;
  99. uint8_t scaling_matrix4[6][16];
  100. uint8_t scaling_matrix8[2][64];
  101. }SPS;
  102. /**
  103. * Picture parameter set
  104. */
  105. typedef struct PPS{
  106. int sps_id;
  107. int cabac; ///< entropy_coding_mode_flag
  108. int pic_order_present; ///< pic_order_present_flag
  109. int slice_group_count; ///< num_slice_groups_minus1 + 1
  110. int mb_slice_group_map_type;
  111. int ref_count[2]; ///< num_ref_idx_l0/1_active_minus1 + 1
  112. int weighted_pred; ///< weighted_pred_flag
  113. int weighted_bipred_idc;
  114. int init_qp; ///< pic_init_qp_minus26 + 26
  115. int init_qs; ///< pic_init_qs_minus26 + 26
  116. int chroma_qp_index_offset;
  117. int deblocking_filter_parameters_present; ///< deblocking_filter_parameters_present_flag
  118. int constrained_intra_pred; ///< constrained_intra_pred_flag
  119. int redundant_pic_cnt_present; ///< redundant_pic_cnt_present_flag
  120. int transform_8x8_mode; ///< transform_8x8_mode_flag
  121. uint8_t scaling_matrix4[6][16];
  122. uint8_t scaling_matrix8[2][64];
  123. }PPS;
  124. /**
  125. * Memory management control operation opcode.
  126. */
  127. typedef enum MMCOOpcode{
  128. MMCO_END=0,
  129. MMCO_SHORT2UNUSED,
  130. MMCO_LONG2UNUSED,
  131. MMCO_SHORT2LONG,
  132. MMCO_SET_MAX_LONG,
  133. MMCO_RESET,
  134. MMCO_LONG,
  135. } MMCOOpcode;
  136. /**
  137. * Memory management control operation.
  138. */
  139. typedef struct MMCO{
  140. MMCOOpcode opcode;
  141. int short_frame_num;
  142. int long_index;
  143. } MMCO;
  144. /**
  145. * H264Context
  146. */
  147. typedef struct H264Context{
  148. MpegEncContext s;
  149. int nal_ref_idc;
  150. int nal_unit_type;
  151. uint8_t *rbsp_buffer;
  152. unsigned int rbsp_buffer_size;
  153. /**
  154. * Used to parse AVC variant of h264
  155. */
  156. int is_avc; ///< this flag is != 0 if codec is avc1
  157. int got_avcC; ///< flag used to parse avcC data only once
  158. int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
  159. int chroma_qp; //QPc
  160. int prev_mb_skipped;
  161. int next_mb_skipped;
  162. //prediction stuff
  163. int chroma_pred_mode;
  164. int intra16x16_pred_mode;
  165. int top_mb_xy;
  166. int left_mb_xy[2];
  167. int8_t intra4x4_pred_mode_cache[5*8];
  168. int8_t (*intra4x4_pred_mode)[8];
  169. void (*pred4x4 [9+3])(uint8_t *src, uint8_t *topright, int stride);//FIXME move to dsp?
  170. void (*pred8x8l [9+3])(uint8_t *src, int topleft, int topright, int stride);
  171. void (*pred8x8 [4+3])(uint8_t *src, int stride);
  172. void (*pred16x16[4+3])(uint8_t *src, int stride);
  173. unsigned int topleft_samples_available;
  174. unsigned int top_samples_available;
  175. unsigned int topright_samples_available;
  176. unsigned int left_samples_available;
  177. uint8_t (*top_borders[2])[16+2*8];
  178. uint8_t left_border[2*(17+2*9)];
  179. /**
  180. * non zero coeff count cache.
  181. * is 64 if not available.
  182. */
  183. DECLARE_ALIGNED_8(uint8_t, non_zero_count_cache[6*8]);
  184. uint8_t (*non_zero_count)[16];
  185. /**
  186. * Motion vector cache.
  187. */
  188. DECLARE_ALIGNED_8(int16_t, mv_cache[2][5*8][2]);
  189. DECLARE_ALIGNED_8(int8_t, ref_cache[2][5*8]);
  190. #define LIST_NOT_USED -1 //FIXME rename?
  191. #define PART_NOT_AVAILABLE -2
  192. /**
  193. * is 1 if the specific list MV&references are set to 0,0,-2.
  194. */
  195. int mv_cache_clean[2];
  196. /**
  197. * number of neighbors (top and/or left) that used 8x8 dct
  198. */
  199. int neighbor_transform_size;
  200. /**
  201. * block_offset[ 0..23] for frame macroblocks
  202. * block_offset[24..47] for field macroblocks
  203. */
  204. int block_offset[2*(16+8)];
  205. uint32_t *mb2b_xy; //FIXME are these 4 a good idea?
  206. uint32_t *mb2b8_xy;
  207. int b_stride; //FIXME use s->b4_stride
  208. int b8_stride;
  209. int mb_linesize; ///< may be equal to s->linesize or s->linesize*2, for mbaff
  210. int mb_uvlinesize;
  211. int emu_edge_width;
  212. int emu_edge_height;
  213. int halfpel_flag;
  214. int thirdpel_flag;
  215. int unknown_svq3_flag;
  216. int next_slice_index;
  217. SPS sps_buffer[MAX_SPS_COUNT];
  218. SPS sps; ///< current sps
  219. PPS pps_buffer[MAX_PPS_COUNT];
  220. /**
  221. * current pps
  222. */
  223. PPS pps; //FIXME move to Picture perhaps? (->no) do we need that?
  224. uint32_t dequant4_buffer[6][52][16];
  225. uint32_t dequant8_buffer[2][52][64];
  226. uint32_t (*dequant4_coeff[6])[16];
  227. uint32_t (*dequant8_coeff[2])[64];
  228. int dequant_coeff_pps; ///< reinit tables when pps changes
  229. int slice_num;
  230. uint8_t *slice_table_base;
  231. uint8_t *slice_table; ///< slice_table_base + 2*mb_stride + 1
  232. int slice_type;
  233. int slice_type_fixed;
  234. //interlacing specific flags
  235. int mb_aff_frame;
  236. int mb_field_decoding_flag;
  237. int mb_mbaff; ///< mb_aff_frame && mb_field_decoding_flag
  238. int sub_mb_type[4];
  239. //POC stuff
  240. int poc_lsb;
  241. int poc_msb;
  242. int delta_poc_bottom;
  243. int delta_poc[2];
  244. int frame_num;
  245. int prev_poc_msb; ///< poc_msb of the last reference pic for POC type 0
  246. int prev_poc_lsb; ///< poc_lsb of the last reference pic for POC type 0
  247. int frame_num_offset; ///< for POC type 2
  248. int prev_frame_num_offset; ///< for POC type 2
  249. int prev_frame_num; ///< frame_num of the last pic for POC type 1/2
  250. /**
  251. * frame_num for frames or 2*frame_num for field pics.
  252. */
  253. int curr_pic_num;
  254. /**
  255. * max_frame_num or 2*max_frame_num for field pics.
  256. */
  257. int max_pic_num;
  258. //Weighted pred stuff
  259. int use_weight;
  260. int use_weight_chroma;
  261. int luma_log2_weight_denom;
  262. int chroma_log2_weight_denom;
  263. int luma_weight[2][48];
  264. int luma_offset[2][48];
  265. int chroma_weight[2][48][2];
  266. int chroma_offset[2][48][2];
  267. int implicit_weight[48][48];
  268. //deblock
  269. int deblocking_filter; ///< disable_deblocking_filter_idc with 1<->0
  270. int slice_alpha_c0_offset;
  271. int slice_beta_offset;
  272. int redundant_pic_count;
  273. int direct_spatial_mv_pred;
  274. int dist_scale_factor[16];
  275. int dist_scale_factor_field[32];
  276. int map_col_to_list0[2][16];
  277. int map_col_to_list0_field[2][32];
  278. /**
  279. * num_ref_idx_l0/1_active_minus1 + 1
  280. */
  281. int ref_count[2]; ///< counts frames or fields, depending on current mb mode
  282. Picture *short_ref[32];
  283. Picture *long_ref[32];
  284. Picture default_ref_list[2][32];
  285. Picture ref_list[2][48]; ///< 0..15: frame refs, 16..47: mbaff field refs
  286. Picture *delayed_pic[16]; //FIXME size?
  287. Picture *delayed_output_pic;
  288. /**
  289. * memory management control operations buffer.
  290. */
  291. MMCO mmco[MAX_MMCO_COUNT];
  292. int mmco_index;
  293. int long_ref_count; ///< number of actual long term references
  294. int short_ref_count; ///< number of actual short term references
  295. //data partitioning
  296. GetBitContext intra_gb;
  297. GetBitContext inter_gb;
  298. GetBitContext *intra_gb_ptr;
  299. GetBitContext *inter_gb_ptr;
  300. DECLARE_ALIGNED_8(DCTELEM, mb[16*24]);
  301. /**
  302. * Cabac
  303. */
  304. CABACContext cabac;
  305. uint8_t cabac_state[460];
  306. int cabac_init_idc;
  307. /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
  308. uint16_t *cbp_table;
  309. int cbp;
  310. int top_cbp;
  311. int left_cbp;
  312. /* chroma_pred_mode for i4x4 or i16x16, else 0 */
  313. uint8_t *chroma_pred_mode_table;
  314. int last_qscale_diff;
  315. int16_t (*mvd_table[2])[2];
  316. DECLARE_ALIGNED_8(int16_t, mvd_cache[2][5*8][2]);
  317. uint8_t *direct_table;
  318. uint8_t direct_cache[5*8];
  319. uint8_t zigzag_scan[16];
  320. uint8_t zigzag_scan8x8[64];
  321. uint8_t zigzag_scan8x8_cavlc[64];
  322. uint8_t field_scan[16];
  323. uint8_t field_scan8x8[64];
  324. uint8_t field_scan8x8_cavlc[64];
  325. const uint8_t *zigzag_scan_q0;
  326. const uint8_t *zigzag_scan8x8_q0;
  327. const uint8_t *zigzag_scan8x8_cavlc_q0;
  328. const uint8_t *field_scan_q0;
  329. const uint8_t *field_scan8x8_q0;
  330. const uint8_t *field_scan8x8_cavlc_q0;
  331. int x264_build;
  332. }H264Context;
  333. static VLC coeff_token_vlc[4];
  334. static VLC chroma_dc_coeff_token_vlc;
  335. static VLC total_zeros_vlc[15];
  336. static VLC chroma_dc_total_zeros_vlc[3];
  337. static VLC run_vlc[6];
  338. static VLC run7_vlc;
  339. static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
  340. static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
  341. static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
  342. static void filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
  343. static 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. /**
  351. * fill a rectangle.
  352. * @param h height of the rectangle, should be a constant
  353. * @param w width of the rectangle, should be a constant
  354. * @param size the size of val (1 or 4), should be a constant
  355. */
  356. static always_inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){
  357. uint8_t *p= (uint8_t*)vp;
  358. assert(size==1 || size==4);
  359. assert(w<=4);
  360. w *= size;
  361. stride *= size;
  362. assert((((long)vp)&(FFMIN(w, STRIDE_ALIGN)-1)) == 0);
  363. assert((stride&(w-1))==0);
  364. if(w==2){
  365. const uint16_t v= size==4 ? val : val*0x0101;
  366. *(uint16_t*)(p + 0*stride)= v;
  367. if(h==1) return;
  368. *(uint16_t*)(p + 1*stride)= v;
  369. if(h==2) return;
  370. *(uint16_t*)(p + 2*stride)=
  371. *(uint16_t*)(p + 3*stride)= v;
  372. }else if(w==4){
  373. const uint32_t v= size==4 ? val : val*0x01010101;
  374. *(uint32_t*)(p + 0*stride)= v;
  375. if(h==1) return;
  376. *(uint32_t*)(p + 1*stride)= v;
  377. if(h==2) return;
  378. *(uint32_t*)(p + 2*stride)=
  379. *(uint32_t*)(p + 3*stride)= v;
  380. }else if(w==8){
  381. //gcc can't optimize 64bit math on x86_32
  382. #if defined(ARCH_X86_64) || (defined(MP_WORDSIZE) && MP_WORDSIZE >= 64)
  383. const uint64_t v= val*0x0100000001ULL;
  384. *(uint64_t*)(p + 0*stride)= v;
  385. if(h==1) return;
  386. *(uint64_t*)(p + 1*stride)= v;
  387. if(h==2) return;
  388. *(uint64_t*)(p + 2*stride)=
  389. *(uint64_t*)(p + 3*stride)= v;
  390. }else if(w==16){
  391. const uint64_t v= val*0x0100000001ULL;
  392. *(uint64_t*)(p + 0+0*stride)=
  393. *(uint64_t*)(p + 8+0*stride)=
  394. *(uint64_t*)(p + 0+1*stride)=
  395. *(uint64_t*)(p + 8+1*stride)= v;
  396. if(h==2) return;
  397. *(uint64_t*)(p + 0+2*stride)=
  398. *(uint64_t*)(p + 8+2*stride)=
  399. *(uint64_t*)(p + 0+3*stride)=
  400. *(uint64_t*)(p + 8+3*stride)= v;
  401. #else
  402. *(uint32_t*)(p + 0+0*stride)=
  403. *(uint32_t*)(p + 4+0*stride)= val;
  404. if(h==1) return;
  405. *(uint32_t*)(p + 0+1*stride)=
  406. *(uint32_t*)(p + 4+1*stride)= val;
  407. if(h==2) return;
  408. *(uint32_t*)(p + 0+2*stride)=
  409. *(uint32_t*)(p + 4+2*stride)=
  410. *(uint32_t*)(p + 0+3*stride)=
  411. *(uint32_t*)(p + 4+3*stride)= val;
  412. }else if(w==16){
  413. *(uint32_t*)(p + 0+0*stride)=
  414. *(uint32_t*)(p + 4+0*stride)=
  415. *(uint32_t*)(p + 8+0*stride)=
  416. *(uint32_t*)(p +12+0*stride)=
  417. *(uint32_t*)(p + 0+1*stride)=
  418. *(uint32_t*)(p + 4+1*stride)=
  419. *(uint32_t*)(p + 8+1*stride)=
  420. *(uint32_t*)(p +12+1*stride)= val;
  421. if(h==2) return;
  422. *(uint32_t*)(p + 0+2*stride)=
  423. *(uint32_t*)(p + 4+2*stride)=
  424. *(uint32_t*)(p + 8+2*stride)=
  425. *(uint32_t*)(p +12+2*stride)=
  426. *(uint32_t*)(p + 0+3*stride)=
  427. *(uint32_t*)(p + 4+3*stride)=
  428. *(uint32_t*)(p + 8+3*stride)=
  429. *(uint32_t*)(p +12+3*stride)= val;
  430. #endif
  431. }else
  432. assert(0);
  433. assert(h==4);
  434. }
  435. static void fill_caches(H264Context *h, int mb_type, int for_deblock){
  436. MpegEncContext * const s = &h->s;
  437. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  438. int topleft_xy, top_xy, topright_xy, left_xy[2];
  439. int topleft_type, top_type, topright_type, left_type[2];
  440. int left_block[8];
  441. int i;
  442. //FIXME deblocking could skip the intra and nnz parts.
  443. if(for_deblock && (h->slice_num == 1 || h->slice_table[mb_xy] == h->slice_table[mb_xy-s->mb_stride]) && !FRAME_MBAFF)
  444. return;
  445. //wow what a mess, why didn't they simplify the interlacing&intra stuff, i can't imagine that these complex rules are worth it
  446. top_xy = mb_xy - s->mb_stride;
  447. topleft_xy = top_xy - 1;
  448. topright_xy= top_xy + 1;
  449. left_xy[1] = left_xy[0] = mb_xy-1;
  450. left_block[0]= 0;
  451. left_block[1]= 1;
  452. left_block[2]= 2;
  453. left_block[3]= 3;
  454. left_block[4]= 7;
  455. left_block[5]= 10;
  456. left_block[6]= 8;
  457. left_block[7]= 11;
  458. if(FRAME_MBAFF){
  459. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  460. const int top_pair_xy = pair_xy - s->mb_stride;
  461. const int topleft_pair_xy = top_pair_xy - 1;
  462. const int topright_pair_xy = top_pair_xy + 1;
  463. const int topleft_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
  464. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  465. const int topright_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
  466. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  467. const int curr_mb_frame_flag = !IS_INTERLACED(mb_type);
  468. const int bottom = (s->mb_y & 1);
  469. 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);
  470. if (bottom
  471. ? !curr_mb_frame_flag // bottom macroblock
  472. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  473. ) {
  474. top_xy -= s->mb_stride;
  475. }
  476. if (bottom
  477. ? !curr_mb_frame_flag // bottom macroblock
  478. : (!curr_mb_frame_flag && !topleft_mb_frame_flag) // top macroblock
  479. ) {
  480. topleft_xy -= s->mb_stride;
  481. }
  482. if (bottom
  483. ? !curr_mb_frame_flag // bottom macroblock
  484. : (!curr_mb_frame_flag && !topright_mb_frame_flag) // top macroblock
  485. ) {
  486. topright_xy -= s->mb_stride;
  487. }
  488. if (left_mb_frame_flag != curr_mb_frame_flag) {
  489. left_xy[1] = left_xy[0] = pair_xy - 1;
  490. if (curr_mb_frame_flag) {
  491. if (bottom) {
  492. left_block[0]= 2;
  493. left_block[1]= 2;
  494. left_block[2]= 3;
  495. left_block[3]= 3;
  496. left_block[4]= 8;
  497. left_block[5]= 11;
  498. left_block[6]= 8;
  499. left_block[7]= 11;
  500. } else {
  501. left_block[0]= 0;
  502. left_block[1]= 0;
  503. left_block[2]= 1;
  504. left_block[3]= 1;
  505. left_block[4]= 7;
  506. left_block[5]= 10;
  507. left_block[6]= 7;
  508. left_block[7]= 10;
  509. }
  510. } else {
  511. left_xy[1] += s->mb_stride;
  512. //left_block[0]= 0;
  513. left_block[1]= 2;
  514. left_block[2]= 0;
  515. left_block[3]= 2;
  516. //left_block[4]= 7;
  517. left_block[5]= 10;
  518. left_block[6]= 7;
  519. left_block[7]= 10;
  520. }
  521. }
  522. }
  523. h->top_mb_xy = top_xy;
  524. h->left_mb_xy[0] = left_xy[0];
  525. h->left_mb_xy[1] = left_xy[1];
  526. if(for_deblock){
  527. topleft_type = 0;
  528. topright_type = 0;
  529. top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0;
  530. left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
  531. left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
  532. if(FRAME_MBAFF && !IS_INTRA(mb_type)){
  533. int list;
  534. int v = *(uint16_t*)&h->non_zero_count[mb_xy][14];
  535. for(i=0; i<16; i++)
  536. h->non_zero_count_cache[scan8[i]] = (v>>i)&1;
  537. for(list=0; list<1+(h->slice_type==B_TYPE); list++){
  538. if(USES_LIST(mb_type,list)){
  539. uint32_t *src = (uint32_t*)s->current_picture.motion_val[list][h->mb2b_xy[mb_xy]];
  540. uint32_t *dst = (uint32_t*)h->mv_cache[list][scan8[0]];
  541. int8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
  542. for(i=0; i<4; i++, dst+=8, src+=h->b_stride){
  543. dst[0] = src[0];
  544. dst[1] = src[1];
  545. dst[2] = src[2];
  546. dst[3] = src[3];
  547. }
  548. *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =
  549. *(uint32_t*)&h->ref_cache[list][scan8[ 2]] = pack16to32(ref[0],ref[1])*0x0101;
  550. ref += h->b8_stride;
  551. *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =
  552. *(uint32_t*)&h->ref_cache[list][scan8[10]] = pack16to32(ref[0],ref[1])*0x0101;
  553. }else{
  554. fill_rectangle(&h-> mv_cache[list][scan8[ 0]], 4, 4, 8, 0, 4);
  555. fill_rectangle(&h->ref_cache[list][scan8[ 0]], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  556. }
  557. }
  558. }
  559. }else{
  560. topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
  561. top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
  562. topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
  563. left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
  564. left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
  565. }
  566. if(IS_INTRA(mb_type)){
  567. h->topleft_samples_available=
  568. h->top_samples_available=
  569. h->left_samples_available= 0xFFFF;
  570. h->topright_samples_available= 0xEEEA;
  571. if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
  572. h->topleft_samples_available= 0xB3FF;
  573. h->top_samples_available= 0x33FF;
  574. h->topright_samples_available= 0x26EA;
  575. }
  576. for(i=0; i<2; i++){
  577. if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
  578. h->topleft_samples_available&= 0xDF5F;
  579. h->left_samples_available&= 0x5F5F;
  580. }
  581. }
  582. if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
  583. h->topleft_samples_available&= 0x7FFF;
  584. if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
  585. h->topright_samples_available&= 0xFBFF;
  586. if(IS_INTRA4x4(mb_type)){
  587. if(IS_INTRA4x4(top_type)){
  588. h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
  589. h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
  590. h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
  591. h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
  592. }else{
  593. int pred;
  594. if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred))
  595. pred= -1;
  596. else{
  597. pred= 2;
  598. }
  599. h->intra4x4_pred_mode_cache[4+8*0]=
  600. h->intra4x4_pred_mode_cache[5+8*0]=
  601. h->intra4x4_pred_mode_cache[6+8*0]=
  602. h->intra4x4_pred_mode_cache[7+8*0]= pred;
  603. }
  604. for(i=0; i<2; i++){
  605. if(IS_INTRA4x4(left_type[i])){
  606. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
  607. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
  608. }else{
  609. int pred;
  610. if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred))
  611. pred= -1;
  612. else{
  613. pred= 2;
  614. }
  615. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
  616. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
  617. }
  618. }
  619. }
  620. }
  621. /*
  622. 0 . T T. T T T T
  623. 1 L . .L . . . .
  624. 2 L . .L . . . .
  625. 3 . T TL . . . .
  626. 4 L . .L . . . .
  627. 5 L . .. . . . .
  628. */
  629. //FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec)
  630. if(top_type){
  631. h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
  632. h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
  633. h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
  634. h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
  635. h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
  636. h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
  637. h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
  638. h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
  639. }else{
  640. h->non_zero_count_cache[4+8*0]=
  641. h->non_zero_count_cache[5+8*0]=
  642. h->non_zero_count_cache[6+8*0]=
  643. h->non_zero_count_cache[7+8*0]=
  644. h->non_zero_count_cache[1+8*0]=
  645. h->non_zero_count_cache[2+8*0]=
  646. h->non_zero_count_cache[1+8*3]=
  647. h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  648. }
  649. for (i=0; i<2; i++) {
  650. if(left_type[i]){
  651. h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
  652. h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
  653. h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
  654. h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
  655. }else{
  656. h->non_zero_count_cache[3+8*1 + 2*8*i]=
  657. h->non_zero_count_cache[3+8*2 + 2*8*i]=
  658. h->non_zero_count_cache[0+8*1 + 8*i]=
  659. h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  660. }
  661. }
  662. if( h->pps.cabac ) {
  663. // top_cbp
  664. if(top_type) {
  665. h->top_cbp = h->cbp_table[top_xy];
  666. } else if(IS_INTRA(mb_type)) {
  667. h->top_cbp = 0x1C0;
  668. } else {
  669. h->top_cbp = 0;
  670. }
  671. // left_cbp
  672. if (left_type[0]) {
  673. h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
  674. } else if(IS_INTRA(mb_type)) {
  675. h->left_cbp = 0x1C0;
  676. } else {
  677. h->left_cbp = 0;
  678. }
  679. if (left_type[0]) {
  680. h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
  681. }
  682. if (left_type[1]) {
  683. h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
  684. }
  685. }
  686. #if 1
  687. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  688. int list;
  689. for(list=0; list<1+(h->slice_type==B_TYPE); list++){
  690. if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
  691. /*if(!h->mv_cache_clean[list]){
  692. memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
  693. memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
  694. h->mv_cache_clean[list]= 1;
  695. }*/
  696. continue;
  697. }
  698. h->mv_cache_clean[list]= 0;
  699. if(USES_LIST(top_type, list)){
  700. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  701. const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
  702. *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
  703. *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
  704. *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
  705. *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
  706. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  707. h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
  708. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  709. h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
  710. }else{
  711. *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
  712. *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
  713. *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
  714. *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
  715. *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
  716. }
  717. //FIXME unify cleanup or sth
  718. if(USES_LIST(left_type[0], list)){
  719. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  720. const int b8_xy= h->mb2b8_xy[left_xy[0]] + 1;
  721. *(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]];
  722. *(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]];
  723. h->ref_cache[list][scan8[0] - 1 + 0*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0]>>1)];
  724. h->ref_cache[list][scan8[0] - 1 + 1*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1]>>1)];
  725. }else{
  726. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 0*8]=
  727. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 1*8]= 0;
  728. h->ref_cache[list][scan8[0] - 1 + 0*8]=
  729. h->ref_cache[list][scan8[0] - 1 + 1*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  730. }
  731. if(USES_LIST(left_type[1], list)){
  732. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  733. const int b8_xy= h->mb2b8_xy[left_xy[1]] + 1;
  734. *(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]];
  735. *(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]];
  736. h->ref_cache[list][scan8[0] - 1 + 2*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[2]>>1)];
  737. h->ref_cache[list][scan8[0] - 1 + 3*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[3]>>1)];
  738. }else{
  739. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 2*8]=
  740. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 3*8]= 0;
  741. h->ref_cache[list][scan8[0] - 1 + 2*8]=
  742. h->ref_cache[list][scan8[0] - 1 + 3*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  743. assert((!left_type[0]) == (!left_type[1]));
  744. }
  745. if((for_deblock || (IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred)) && !FRAME_MBAFF)
  746. continue;
  747. if(USES_LIST(topleft_type, list)){
  748. const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
  749. const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + h->b8_stride;
  750. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  751. h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  752. }else{
  753. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
  754. h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  755. }
  756. if(USES_LIST(topright_type, list)){
  757. const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
  758. const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
  759. *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  760. h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  761. }else{
  762. *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
  763. h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  764. }
  765. if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)
  766. continue;
  767. h->ref_cache[list][scan8[5 ]+1] =
  768. h->ref_cache[list][scan8[7 ]+1] =
  769. h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewhere else)
  770. h->ref_cache[list][scan8[4 ]] =
  771. h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
  772. *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
  773. *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
  774. *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  775. *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
  776. *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
  777. if( h->pps.cabac ) {
  778. /* XXX beurk, Load mvd */
  779. if(USES_LIST(top_type, list)){
  780. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  781. *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
  782. *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
  783. *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
  784. *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
  785. }else{
  786. *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
  787. *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
  788. *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
  789. *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
  790. }
  791. if(USES_LIST(left_type[0], list)){
  792. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  793. *(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]];
  794. *(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]];
  795. }else{
  796. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
  797. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
  798. }
  799. if(USES_LIST(left_type[1], list)){
  800. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  801. *(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]];
  802. *(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]];
  803. }else{
  804. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
  805. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
  806. }
  807. *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
  808. *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
  809. *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  810. *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
  811. *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
  812. if(h->slice_type == B_TYPE){
  813. fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
  814. if(IS_DIRECT(top_type)){
  815. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
  816. }else if(IS_8X8(top_type)){
  817. int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
  818. h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
  819. h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
  820. }else{
  821. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
  822. }
  823. if(IS_DIRECT(left_type[0]))
  824. h->direct_cache[scan8[0] - 1 + 0*8]= 1;
  825. else if(IS_8X8(left_type[0]))
  826. 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)];
  827. else
  828. h->direct_cache[scan8[0] - 1 + 0*8]= 0;
  829. if(IS_DIRECT(left_type[1]))
  830. h->direct_cache[scan8[0] - 1 + 2*8]= 1;
  831. else if(IS_8X8(left_type[1]))
  832. 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)];
  833. else
  834. h->direct_cache[scan8[0] - 1 + 2*8]= 0;
  835. }
  836. }
  837. if(FRAME_MBAFF){
  838. #define MAP_MVS\
  839. MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
  840. MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
  841. MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
  842. MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
  843. MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
  844. MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
  845. MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\
  846. MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\
  847. MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\
  848. MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
  849. if(MB_FIELD){
  850. #define MAP_F2F(idx, mb_type)\
  851. if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  852. h->ref_cache[list][idx] <<= 1;\
  853. h->mv_cache[list][idx][1] /= 2;\
  854. h->mvd_cache[list][idx][1] /= 2;\
  855. }
  856. MAP_MVS
  857. #undef MAP_F2F
  858. }else{
  859. #define MAP_F2F(idx, mb_type)\
  860. if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  861. h->ref_cache[list][idx] >>= 1;\
  862. h->mv_cache[list][idx][1] <<= 1;\
  863. h->mvd_cache[list][idx][1] <<= 1;\
  864. }
  865. MAP_MVS
  866. #undef MAP_F2F
  867. }
  868. }
  869. }
  870. }
  871. #endif
  872. h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
  873. }
  874. static inline void write_back_intra_pred_mode(H264Context *h){
  875. MpegEncContext * const s = &h->s;
  876. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  877. h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
  878. h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
  879. h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
  880. h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
  881. h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
  882. h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
  883. h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
  884. }
  885. /**
  886. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  887. */
  888. static inline int check_intra4x4_pred_mode(H264Context *h){
  889. MpegEncContext * const s = &h->s;
  890. static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
  891. static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
  892. int i;
  893. if(!(h->top_samples_available&0x8000)){
  894. for(i=0; i<4; i++){
  895. int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
  896. if(status<0){
  897. 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);
  898. return -1;
  899. } else if(status){
  900. h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
  901. }
  902. }
  903. }
  904. if(!(h->left_samples_available&0x8000)){
  905. for(i=0; i<4; i++){
  906. int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
  907. if(status<0){
  908. 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);
  909. return -1;
  910. } else if(status){
  911. h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
  912. }
  913. }
  914. }
  915. return 0;
  916. } //FIXME cleanup like next
  917. /**
  918. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  919. */
  920. static inline int check_intra_pred_mode(H264Context *h, int mode){
  921. MpegEncContext * const s = &h->s;
  922. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  923. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  924. if(mode < 0 || mode > 6) {
  925. 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);
  926. return -1;
  927. }
  928. if(!(h->top_samples_available&0x8000)){
  929. mode= top[ mode ];
  930. if(mode<0){
  931. 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);
  932. return -1;
  933. }
  934. }
  935. if(!(h->left_samples_available&0x8000)){
  936. mode= left[ mode ];
  937. if(mode<0){
  938. 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);
  939. return -1;
  940. }
  941. }
  942. return mode;
  943. }
  944. /**
  945. * gets the predicted intra4x4 prediction mode.
  946. */
  947. static inline int pred_intra_mode(H264Context *h, int n){
  948. const int index8= scan8[n];
  949. const int left= h->intra4x4_pred_mode_cache[index8 - 1];
  950. const int top = h->intra4x4_pred_mode_cache[index8 - 8];
  951. const int min= FFMIN(left, top);
  952. tprintf("mode:%d %d min:%d\n", left ,top, min);
  953. if(min<0) return DC_PRED;
  954. else return min;
  955. }
  956. static inline void write_back_non_zero_count(H264Context *h){
  957. MpegEncContext * const s = &h->s;
  958. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  959. h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
  960. h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
  961. h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
  962. h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
  963. h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
  964. h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
  965. h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
  966. h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
  967. h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
  968. h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
  969. h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
  970. h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
  971. h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
  972. if(FRAME_MBAFF){
  973. // store all luma nnzs, for deblocking
  974. int v = 0, i;
  975. for(i=0; i<16; i++)
  976. v += (!!h->non_zero_count_cache[scan8[i]]) << i;
  977. *(uint16_t*)&h->non_zero_count[mb_xy][14] = v;
  978. }
  979. }
  980. /**
  981. * gets the predicted number of non zero coefficients.
  982. * @param n block index
  983. */
  984. static inline int pred_non_zero_count(H264Context *h, int n){
  985. const int index8= scan8[n];
  986. const int left= h->non_zero_count_cache[index8 - 1];
  987. const int top = h->non_zero_count_cache[index8 - 8];
  988. int i= left + top;
  989. if(i<64) i= (i+1)>>1;
  990. tprintf("pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
  991. return i&31;
  992. }
  993. static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
  994. const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
  995. /* there is no consistent mapping of mvs to neighboring locations that will
  996. * make mbaff happy, so we can't move all this logic to fill_caches */
  997. if(FRAME_MBAFF){
  998. MpegEncContext *s = &h->s;
  999. const uint32_t *mb_types = s->current_picture_ptr->mb_type;
  1000. const int16_t *mv;
  1001. *(uint32_t*)h->mv_cache[list][scan8[0]-2] = 0;
  1002. *C = h->mv_cache[list][scan8[0]-2];
  1003. if(!MB_FIELD
  1004. && (s->mb_y&1) && i < scan8[0]+8 && topright_ref != PART_NOT_AVAILABLE){
  1005. int topright_xy = s->mb_x + (s->mb_y-1)*s->mb_stride + (i == scan8[0]+3);
  1006. if(IS_INTERLACED(mb_types[topright_xy])){
  1007. #define SET_DIAG_MV(MV_OP, REF_OP, X4, Y4)\
  1008. const int x4 = X4, y4 = Y4;\
  1009. const int mb_type = mb_types[(x4>>2)+(y4>>2)*s->mb_stride];\
  1010. if(!USES_LIST(mb_type,list) && !IS_8X8(mb_type))\
  1011. return LIST_NOT_USED;\
  1012. mv = s->current_picture_ptr->motion_val[list][x4 + y4*h->b_stride];\
  1013. h->mv_cache[list][scan8[0]-2][0] = mv[0];\
  1014. h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
  1015. return s->current_picture_ptr->ref_index[list][(x4>>1) + (y4>>1)*h->b8_stride] REF_OP;
  1016. SET_DIAG_MV(*2, >>1, s->mb_x*4+(i&7)-4+part_width, s->mb_y*4-1);
  1017. }
  1018. }
  1019. if(topright_ref == PART_NOT_AVAILABLE
  1020. && ((s->mb_y&1) || i >= scan8[0]+8) && (i&7)==4
  1021. && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
  1022. if(!MB_FIELD
  1023. && IS_INTERLACED(mb_types[h->left_mb_xy[0]])){
  1024. SET_DIAG_MV(*2, >>1, s->mb_x*4-1, (s->mb_y|1)*4+(s->mb_y&1)*2+(i>>4)-1);
  1025. }
  1026. if(MB_FIELD
  1027. && !IS_INTERLACED(mb_types[h->left_mb_xy[0]])
  1028. && i >= scan8[0]+8){
  1029. // leftshift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's ok.
  1030. SET_DIAG_MV(>>1, <<1, s->mb_x*4-1, (s->mb_y&~1)*4 - 1 + ((i-scan8[0])>>3)*2);
  1031. }
  1032. }
  1033. #undef SET_DIAG_MV
  1034. }
  1035. if(topright_ref != PART_NOT_AVAILABLE){
  1036. *C= h->mv_cache[list][ i - 8 + part_width ];
  1037. return topright_ref;
  1038. }else{
  1039. tprintf("topright MV not available\n");
  1040. *C= h->mv_cache[list][ i - 8 - 1 ];
  1041. return h->ref_cache[list][ i - 8 - 1 ];
  1042. }
  1043. }
  1044. /**
  1045. * gets the predicted MV.
  1046. * @param n the block index
  1047. * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
  1048. * @param mx the x component of the predicted motion vector
  1049. * @param my the y component of the predicted motion vector
  1050. */
  1051. static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
  1052. const int index8= scan8[n];
  1053. const int top_ref= h->ref_cache[list][ index8 - 8 ];
  1054. const int left_ref= h->ref_cache[list][ index8 - 1 ];
  1055. const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
  1056. const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
  1057. const int16_t * C;
  1058. int diagonal_ref, match_count;
  1059. assert(part_width==1 || part_width==2 || part_width==4);
  1060. /* mv_cache
  1061. B . . A T T T T
  1062. U . . L . . , .
  1063. U . . L . . . .
  1064. U . . L . . , .
  1065. . . . L . . . .
  1066. */
  1067. diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
  1068. match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
  1069. tprintf("pred_motion match_count=%d\n", match_count);
  1070. if(match_count > 1){ //most common
  1071. *mx= mid_pred(A[0], B[0], C[0]);
  1072. *my= mid_pred(A[1], B[1], C[1]);
  1073. }else if(match_count==1){
  1074. if(left_ref==ref){
  1075. *mx= A[0];
  1076. *my= A[1];
  1077. }else if(top_ref==ref){
  1078. *mx= B[0];
  1079. *my= B[1];
  1080. }else{
  1081. *mx= C[0];
  1082. *my= C[1];
  1083. }
  1084. }else{
  1085. if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
  1086. *mx= A[0];
  1087. *my= A[1];
  1088. }else{
  1089. *mx= mid_pred(A[0], B[0], C[0]);
  1090. *my= mid_pred(A[1], B[1], C[1]);
  1091. }
  1092. }
  1093. 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);
  1094. }
  1095. /**
  1096. * gets the directionally predicted 16x8 MV.
  1097. * @param n the block index
  1098. * @param mx the x component of the predicted motion vector
  1099. * @param my the y component of the predicted motion vector
  1100. */
  1101. static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  1102. if(n==0){
  1103. const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
  1104. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  1105. 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);
  1106. if(top_ref == ref){
  1107. *mx= B[0];
  1108. *my= B[1];
  1109. return;
  1110. }
  1111. }else{
  1112. const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
  1113. const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
  1114. 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);
  1115. if(left_ref == ref){
  1116. *mx= A[0];
  1117. *my= A[1];
  1118. return;
  1119. }
  1120. }
  1121. //RARE
  1122. pred_motion(h, n, 4, list, ref, mx, my);
  1123. }
  1124. /**
  1125. * gets the directionally predicted 8x16 MV.
  1126. * @param n the block index
  1127. * @param mx the x component of the predicted motion vector
  1128. * @param my the y component of the predicted motion vector
  1129. */
  1130. static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  1131. if(n==0){
  1132. const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
  1133. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  1134. 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);
  1135. if(left_ref == ref){
  1136. *mx= A[0];
  1137. *my= A[1];
  1138. return;
  1139. }
  1140. }else{
  1141. const int16_t * C;
  1142. int diagonal_ref;
  1143. diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
  1144. 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);
  1145. if(diagonal_ref == ref){
  1146. *mx= C[0];
  1147. *my= C[1];
  1148. return;
  1149. }
  1150. }
  1151. //RARE
  1152. pred_motion(h, n, 2, list, ref, mx, my);
  1153. }
  1154. static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
  1155. const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
  1156. const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
  1157. tprintf("pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
  1158. if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
  1159. || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
  1160. || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
  1161. *mx = *my = 0;
  1162. return;
  1163. }
  1164. pred_motion(h, 0, 4, 0, 0, mx, my);
  1165. return;
  1166. }
  1167. static inline void direct_dist_scale_factor(H264Context * const h){
  1168. const int poc = h->s.current_picture_ptr->poc;
  1169. const int poc1 = h->ref_list[1][0].poc;
  1170. int i;
  1171. for(i=0; i<h->ref_count[0]; i++){
  1172. int poc0 = h->ref_list[0][i].poc;
  1173. int td = clip(poc1 - poc0, -128, 127);
  1174. if(td == 0 /* FIXME || pic0 is a long-term ref */){
  1175. h->dist_scale_factor[i] = 256;
  1176. }else{
  1177. int tb = clip(poc - poc0, -128, 127);
  1178. int tx = (16384 + (FFABS(td) >> 1)) / td;
  1179. h->dist_scale_factor[i] = clip((tb*tx + 32) >> 6, -1024, 1023);
  1180. }
  1181. }
  1182. if(FRAME_MBAFF){
  1183. for(i=0; i<h->ref_count[0]; i++){
  1184. h->dist_scale_factor_field[2*i] =
  1185. h->dist_scale_factor_field[2*i+1] = h->dist_scale_factor[i];
  1186. }
  1187. }
  1188. }
  1189. static inline void direct_ref_list_init(H264Context * const h){
  1190. MpegEncContext * const s = &h->s;
  1191. Picture * const ref1 = &h->ref_list[1][0];
  1192. Picture * const cur = s->current_picture_ptr;
  1193. int list, i, j;
  1194. if(cur->pict_type == I_TYPE)
  1195. cur->ref_count[0] = 0;
  1196. if(cur->pict_type != B_TYPE)
  1197. cur->ref_count[1] = 0;
  1198. for(list=0; list<2; list++){
  1199. cur->ref_count[list] = h->ref_count[list];
  1200. for(j=0; j<h->ref_count[list]; j++)
  1201. cur->ref_poc[list][j] = h->ref_list[list][j].poc;
  1202. }
  1203. if(cur->pict_type != B_TYPE || h->direct_spatial_mv_pred)
  1204. return;
  1205. for(list=0; list<2; list++){
  1206. for(i=0; i<ref1->ref_count[list]; i++){
  1207. const int poc = ref1->ref_poc[list][i];
  1208. h->map_col_to_list0[list][i] = 0; /* bogus; fills in for missing frames */
  1209. for(j=0; j<h->ref_count[list]; j++)
  1210. if(h->ref_list[list][j].poc == poc){
  1211. h->map_col_to_list0[list][i] = j;
  1212. break;
  1213. }
  1214. }
  1215. }
  1216. if(FRAME_MBAFF){
  1217. for(list=0; list<2; list++){
  1218. for(i=0; i<ref1->ref_count[list]; i++){
  1219. j = h->map_col_to_list0[list][i];
  1220. h->map_col_to_list0_field[list][2*i] = 2*j;
  1221. h->map_col_to_list0_field[list][2*i+1] = 2*j+1;
  1222. }
  1223. }
  1224. }
  1225. }
  1226. static inline void pred_direct_motion(H264Context * const h, int *mb_type){
  1227. MpegEncContext * const s = &h->s;
  1228. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  1229. const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1230. const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1231. const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
  1232. const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
  1233. const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[1][b4_xy];
  1234. const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
  1235. const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy];
  1236. const int is_b8x8 = IS_8X8(*mb_type);
  1237. int sub_mb_type;
  1238. int i8, i4;
  1239. #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
  1240. if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
  1241. /* FIXME save sub mb types from previous frames (or derive from MVs)
  1242. * so we know exactly what block size to use */
  1243. sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  1244. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  1245. }else if(!is_b8x8 && (mb_type_col & MB_TYPE_16x16_OR_INTRA)){
  1246. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  1247. *mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
  1248. }else{
  1249. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  1250. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  1251. }
  1252. if(!is_b8x8)
  1253. *mb_type |= MB_TYPE_DIRECT2;
  1254. if(MB_FIELD)
  1255. *mb_type |= MB_TYPE_INTERLACED;
  1256. 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);
  1257. if(h->direct_spatial_mv_pred){
  1258. int ref[2];
  1259. int mv[2][2];
  1260. int list;
  1261. /* FIXME interlacing + spatial direct uses wrong colocated block positions */
  1262. /* ref = min(neighbors) */
  1263. for(list=0; list<2; list++){
  1264. int refa = h->ref_cache[list][scan8[0] - 1];
  1265. int refb = h->ref_cache[list][scan8[0] - 8];
  1266. int refc = h->ref_cache[list][scan8[0] - 8 + 4];
  1267. if(refc == -2)
  1268. refc = h->ref_cache[list][scan8[0] - 8 - 1];
  1269. ref[list] = refa;
  1270. if(ref[list] < 0 || (refb < ref[list] && refb >= 0))
  1271. ref[list] = refb;
  1272. if(ref[list] < 0 || (refc < ref[list] && refc >= 0))
  1273. ref[list] = refc;
  1274. if(ref[list] < 0)
  1275. ref[list] = -1;
  1276. }
  1277. if(ref[0] < 0 && ref[1] < 0){
  1278. ref[0] = ref[1] = 0;
  1279. mv[0][0] = mv[0][1] =
  1280. mv[1][0] = mv[1][1] = 0;
  1281. }else{
  1282. for(list=0; list<2; list++){
  1283. if(ref[list] >= 0)
  1284. pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
  1285. else
  1286. mv[list][0] = mv[list][1] = 0;
  1287. }
  1288. }
  1289. if(ref[1] < 0){
  1290. *mb_type &= ~MB_TYPE_P0L1;
  1291. sub_mb_type &= ~MB_TYPE_P0L1;
  1292. }else if(ref[0] < 0){
  1293. *mb_type &= ~MB_TYPE_P0L0;
  1294. sub_mb_type &= ~MB_TYPE_P0L0;
  1295. }
  1296. if(IS_16X16(*mb_type)){
  1297. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  1298. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  1299. if(!IS_INTRA(mb_type_col)
  1300. && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
  1301. || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
  1302. && (h->x264_build>33 || !h->x264_build)))){
  1303. if(ref[0] > 0)
  1304. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1305. else
  1306. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  1307. if(ref[1] > 0)
  1308. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1309. else
  1310. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  1311. }else{
  1312. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1313. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1314. }
  1315. }else{
  1316. for(i8=0; i8<4; i8++){
  1317. const int x8 = i8&1;
  1318. const int y8 = i8>>1;
  1319. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1320. continue;
  1321. h->sub_mb_type[i8] = sub_mb_type;
  1322. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1323. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1324. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  1325. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  1326. /* col_zero_flag */
  1327. if(!IS_INTRA(mb_type_col) && ( l1ref0[x8 + y8*h->b8_stride] == 0
  1328. || (l1ref0[x8 + y8*h->b8_stride] < 0 && l1ref1[x8 + y8*h->b8_stride] == 0
  1329. && (h->x264_build>33 || !h->x264_build)))){
  1330. const int16_t (*l1mv)[2]= l1ref0[x8 + y8*h->b8_stride] == 0 ? l1mv0 : l1mv1;
  1331. if(IS_SUB_8X8(sub_mb_type)){
  1332. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1333. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  1334. if(ref[0] == 0)
  1335. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1336. if(ref[1] == 0)
  1337. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1338. }
  1339. }else
  1340. for(i4=0; i4<4; i4++){
  1341. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1342. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  1343. if(ref[0] == 0)
  1344. *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
  1345. if(ref[1] == 0)
  1346. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
  1347. }
  1348. }
  1349. }
  1350. }
  1351. }
  1352. }else{ /* direct temporal mv pred */
  1353. const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
  1354. const int *dist_scale_factor = h->dist_scale_factor;
  1355. if(FRAME_MBAFF){
  1356. if(IS_INTERLACED(*mb_type)){
  1357. map_col_to_list0[0] = h->map_col_to_list0_field[0];
  1358. map_col_to_list0[1] = h->map_col_to_list0_field[1];
  1359. dist_scale_factor = h->dist_scale_factor_field;
  1360. }
  1361. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col)){
  1362. /* FIXME assumes direct_8x8_inference == 1 */
  1363. const int pair_xy = s->mb_x + (s->mb_y&~1)*s->mb_stride;
  1364. int mb_types_col[2];
  1365. int y_shift;
  1366. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1
  1367. | (is_b8x8 ? 0 : MB_TYPE_DIRECT2)
  1368. | (*mb_type & MB_TYPE_INTERLACED);
  1369. sub_mb_type = MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_16x16;
  1370. if(IS_INTERLACED(*mb_type)){
  1371. /* frame to field scaling */
  1372. mb_types_col[0] = h->ref_list[1][0].mb_type[pair_xy];
  1373. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1374. if(s->mb_y&1){
  1375. l1ref0 -= 2*h->b8_stride;
  1376. l1ref1 -= 2*h->b8_stride;
  1377. l1mv0 -= 4*h->b_stride;
  1378. l1mv1 -= 4*h->b_stride;
  1379. }
  1380. y_shift = 0;
  1381. if( (mb_types_col[0] & MB_TYPE_16x16_OR_INTRA)
  1382. && (mb_types_col[1] & MB_TYPE_16x16_OR_INTRA)
  1383. && !is_b8x8)
  1384. *mb_type |= MB_TYPE_16x8;
  1385. else
  1386. *mb_type |= MB_TYPE_8x8;
  1387. }else{
  1388. /* field to frame scaling */
  1389. /* col_mb_y = (mb_y&~1) + (topAbsDiffPOC < bottomAbsDiffPOC ? 0 : 1)
  1390. * but in MBAFF, top and bottom POC are equal */
  1391. int dy = (s->mb_y&1) ? 1 : 2;
  1392. mb_types_col[0] =
  1393. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1394. l1ref0 += dy*h->b8_stride;
  1395. l1ref1 += dy*h->b8_stride;
  1396. l1mv0 += 2*dy*h->b_stride;
  1397. l1mv1 += 2*dy*h->b_stride;
  1398. y_shift = 2;
  1399. if((mb_types_col[0] & (MB_TYPE_16x16_OR_INTRA|MB_TYPE_16x8))
  1400. && !is_b8x8)
  1401. *mb_type |= MB_TYPE_16x16;
  1402. else
  1403. *mb_type |= MB_TYPE_8x8;
  1404. }
  1405. for(i8=0; i8<4; i8++){
  1406. const int x8 = i8&1;
  1407. const int y8 = i8>>1;
  1408. int ref0, scale;
  1409. const int16_t (*l1mv)[2]= l1mv0;
  1410. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1411. continue;
  1412. h->sub_mb_type[i8] = sub_mb_type;
  1413. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1414. if(IS_INTRA(mb_types_col[y8])){
  1415. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1416. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1417. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1418. continue;
  1419. }
  1420. ref0 = l1ref0[x8 + (y8*2>>y_shift)*h->b8_stride];
  1421. if(ref0 >= 0)
  1422. ref0 = map_col_to_list0[0][ref0*2>>y_shift];
  1423. else{
  1424. ref0 = map_col_to_list0[1][l1ref1[x8 + (y8*2>>y_shift)*h->b8_stride]*2>>y_shift];
  1425. l1mv= l1mv1;
  1426. }
  1427. scale = dist_scale_factor[ref0];
  1428. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1429. {
  1430. const int16_t *mv_col = l1mv[x8*3 + (y8*6>>y_shift)*h->b_stride];
  1431. int my_col = (mv_col[1]<<y_shift)/2;
  1432. int mx = (scale * mv_col[0] + 128) >> 8;
  1433. int my = (scale * my_col + 128) >> 8;
  1434. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1435. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
  1436. }
  1437. }
  1438. return;
  1439. }
  1440. }
  1441. /* one-to-one mv scaling */
  1442. if(IS_16X16(*mb_type)){
  1443. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  1444. if(IS_INTRA(mb_type_col)){
  1445. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  1446. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  1447. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  1448. }else{
  1449. const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0]]
  1450. : map_col_to_list0[1][l1ref1[0]];
  1451. const int scale = dist_scale_factor[ref0];
  1452. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  1453. int mv_l0[2];
  1454. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1455. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1456. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref0, 1);
  1457. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0],mv_l0[1]), 4);
  1458. 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);
  1459. }
  1460. }else{
  1461. for(i8=0; i8<4; i8++){
  1462. const int x8 = i8&1;
  1463. const int y8 = i8>>1;
  1464. int ref0, scale;
  1465. const int16_t (*l1mv)[2]= l1mv0;
  1466. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1467. continue;
  1468. h->sub_mb_type[i8] = sub_mb_type;
  1469. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1470. if(IS_INTRA(mb_type_col)){
  1471. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1472. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1473. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1474. continue;
  1475. }
  1476. ref0 = l1ref0[x8 + y8*h->b8_stride];
  1477. if(ref0 >= 0)
  1478. ref0 = map_col_to_list0[0][ref0];
  1479. else{
  1480. ref0 = map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]];
  1481. l1mv= l1mv1;
  1482. }
  1483. scale = dist_scale_factor[ref0];
  1484. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1485. if(IS_SUB_8X8(sub_mb_type)){
  1486. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1487. int mx = (scale * mv_col[0] + 128) >> 8;
  1488. int my = (scale * mv_col[1] + 128) >> 8;
  1489. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1490. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
  1491. }else
  1492. for(i4=0; i4<4; i4++){
  1493. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1494. int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
  1495. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1496. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1497. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
  1498. pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  1499. }
  1500. }
  1501. }
  1502. }
  1503. }
  1504. static inline void write_back_motion(H264Context *h, int mb_type){
  1505. MpegEncContext * const s = &h->s;
  1506. const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1507. const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1508. int list;
  1509. if(!USES_LIST(mb_type, 0))
  1510. fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, h->b8_stride, (uint8_t)LIST_NOT_USED, 1);
  1511. for(list=0; list<2; list++){
  1512. int y;
  1513. if(!USES_LIST(mb_type, list))
  1514. continue;
  1515. for(y=0; y<4; y++){
  1516. *(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];
  1517. *(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];
  1518. }
  1519. if( h->pps.cabac ) {
  1520. if(IS_SKIP(mb_type))
  1521. fill_rectangle(h->mvd_table[list][b_xy], 4, 4, h->b_stride, 0, 4);
  1522. else
  1523. for(y=0; y<4; y++){
  1524. *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
  1525. *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
  1526. }
  1527. }
  1528. {
  1529. int8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
  1530. ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]];
  1531. ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]];
  1532. ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]];
  1533. ref_index[1+1*h->b8_stride]= h->ref_cache[list][scan8[12]];
  1534. }
  1535. }
  1536. if(h->slice_type == B_TYPE && h->pps.cabac){
  1537. if(IS_8X8(mb_type)){
  1538. uint8_t *direct_table = &h->direct_table[b8_xy];
  1539. direct_table[1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
  1540. direct_table[0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
  1541. direct_table[1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
  1542. }
  1543. }
  1544. }
  1545. /**
  1546. * Decodes a network abstraction layer unit.
  1547. * @param consumed is the number of bytes used as input
  1548. * @param length is the length of the array
  1549. * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp tailing?
  1550. * @returns decoded bytes, might be src+1 if no escapes
  1551. */
  1552. static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){
  1553. int i, si, di;
  1554. uint8_t *dst;
  1555. // src[0]&0x80; //forbidden bit
  1556. h->nal_ref_idc= src[0]>>5;
  1557. h->nal_unit_type= src[0]&0x1F;
  1558. src++; length--;
  1559. #if 0
  1560. for(i=0; i<length; i++)
  1561. printf("%2X ", src[i]);
  1562. #endif
  1563. for(i=0; i+1<length; i+=2){
  1564. if(src[i]) continue;
  1565. if(i>0 && src[i-1]==0) i--;
  1566. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  1567. if(src[i+2]!=3){
  1568. /* startcode, so we must be past the end */
  1569. length=i;
  1570. }
  1571. break;
  1572. }
  1573. }
  1574. if(i>=length-1){ //no escaped 0
  1575. *dst_length= length;
  1576. *consumed= length+1; //+1 for the header
  1577. return src;
  1578. }
  1579. h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length);
  1580. dst= h->rbsp_buffer;
  1581. //printf("decoding esc\n");
  1582. si=di=0;
  1583. while(si<length){
  1584. //remove escapes (very rare 1:2^22)
  1585. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  1586. if(src[si+2]==3){ //escape
  1587. dst[di++]= 0;
  1588. dst[di++]= 0;
  1589. si+=3;
  1590. continue;
  1591. }else //next start code
  1592. break;
  1593. }
  1594. dst[di++]= src[si++];
  1595. }
  1596. *dst_length= di;
  1597. *consumed= si + 1;//+1 for the header
  1598. //FIXME store exact number of bits in the getbitcontext (its needed for decoding)
  1599. return dst;
  1600. }
  1601. /**
  1602. * identifies the exact end of the bitstream
  1603. * @return the length of the trailing, or 0 if damaged
  1604. */
  1605. static int decode_rbsp_trailing(uint8_t *src){
  1606. int v= *src;
  1607. int r;
  1608. tprintf("rbsp trailing %X\n", v);
  1609. for(r=1; r<9; r++){
  1610. if(v&1) return r;
  1611. v>>=1;
  1612. }
  1613. return 0;
  1614. }
  1615. /**
  1616. * idct tranforms the 16 dc values and dequantize them.
  1617. * @param qp quantization parameter
  1618. */
  1619. static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1620. #define stride 16
  1621. int i;
  1622. int temp[16]; //FIXME check if this is a good idea
  1623. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1624. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1625. //memset(block, 64, 2*256);
  1626. //return;
  1627. for(i=0; i<4; i++){
  1628. const int offset= y_offset[i];
  1629. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1630. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1631. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1632. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1633. temp[4*i+0]= z0+z3;
  1634. temp[4*i+1]= z1+z2;
  1635. temp[4*i+2]= z1-z2;
  1636. temp[4*i+3]= z0-z3;
  1637. }
  1638. for(i=0; i<4; i++){
  1639. const int offset= x_offset[i];
  1640. const int z0= temp[4*0+i] + temp[4*2+i];
  1641. const int z1= temp[4*0+i] - temp[4*2+i];
  1642. const int z2= temp[4*1+i] - temp[4*3+i];
  1643. const int z3= temp[4*1+i] + temp[4*3+i];
  1644. block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_resdual
  1645. block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
  1646. block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
  1647. block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
  1648. }
  1649. }
  1650. #if 0
  1651. /**
  1652. * dct tranforms the 16 dc values.
  1653. * @param qp quantization parameter ??? FIXME
  1654. */
  1655. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  1656. // const int qmul= dequant_coeff[qp][0];
  1657. int i;
  1658. int temp[16]; //FIXME check if this is a good idea
  1659. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1660. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1661. for(i=0; i<4; i++){
  1662. const int offset= y_offset[i];
  1663. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1664. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1665. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1666. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1667. temp[4*i+0]= z0+z3;
  1668. temp[4*i+1]= z1+z2;
  1669. temp[4*i+2]= z1-z2;
  1670. temp[4*i+3]= z0-z3;
  1671. }
  1672. for(i=0; i<4; i++){
  1673. const int offset= x_offset[i];
  1674. const int z0= temp[4*0+i] + temp[4*2+i];
  1675. const int z1= temp[4*0+i] - temp[4*2+i];
  1676. const int z2= temp[4*1+i] - temp[4*3+i];
  1677. const int z3= temp[4*1+i] + temp[4*3+i];
  1678. block[stride*0 +offset]= (z0 + z3)>>1;
  1679. block[stride*2 +offset]= (z1 + z2)>>1;
  1680. block[stride*8 +offset]= (z1 - z2)>>1;
  1681. block[stride*10+offset]= (z0 - z3)>>1;
  1682. }
  1683. }
  1684. #endif
  1685. #undef xStride
  1686. #undef stride
  1687. static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1688. const int stride= 16*2;
  1689. const int xStride= 16;
  1690. int a,b,c,d,e;
  1691. a= block[stride*0 + xStride*0];
  1692. b= block[stride*0 + xStride*1];
  1693. c= block[stride*1 + xStride*0];
  1694. d= block[stride*1 + xStride*1];
  1695. e= a-b;
  1696. a= a+b;
  1697. b= c-d;
  1698. c= c+d;
  1699. block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
  1700. block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
  1701. block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
  1702. block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
  1703. }
  1704. #if 0
  1705. static void chroma_dc_dct_c(DCTELEM *block){
  1706. const int stride= 16*2;
  1707. const int xStride= 16;
  1708. int a,b,c,d,e;
  1709. a= block[stride*0 + xStride*0];
  1710. b= block[stride*0 + xStride*1];
  1711. c= block[stride*1 + xStride*0];
  1712. d= block[stride*1 + xStride*1];
  1713. e= a-b;
  1714. a= a+b;
  1715. b= c-d;
  1716. c= c+d;
  1717. block[stride*0 + xStride*0]= (a+c);
  1718. block[stride*0 + xStride*1]= (e+b);
  1719. block[stride*1 + xStride*0]= (a-c);
  1720. block[stride*1 + xStride*1]= (e-b);
  1721. }
  1722. #endif
  1723. /**
  1724. * gets the chroma qp.
  1725. */
  1726. static inline int get_chroma_qp(int chroma_qp_index_offset, int qscale){
  1727. return chroma_qp[clip(qscale + chroma_qp_index_offset, 0, 51)];
  1728. }
  1729. #if 0
  1730. static void h264_diff_dct_c(DCTELEM *block, uint8_t *src1, uint8_t *src2, int stride){
  1731. int i;
  1732. //FIXME try int temp instead of block
  1733. for(i=0; i<4; i++){
  1734. const int d0= src1[0 + i*stride] - src2[0 + i*stride];
  1735. const int d1= src1[1 + i*stride] - src2[1 + i*stride];
  1736. const int d2= src1[2 + i*stride] - src2[2 + i*stride];
  1737. const int d3= src1[3 + i*stride] - src2[3 + i*stride];
  1738. const int z0= d0 + d3;
  1739. const int z3= d0 - d3;
  1740. const int z1= d1 + d2;
  1741. const int z2= d1 - d2;
  1742. block[0 + 4*i]= z0 + z1;
  1743. block[1 + 4*i]= 2*z3 + z2;
  1744. block[2 + 4*i]= z0 - z1;
  1745. block[3 + 4*i]= z3 - 2*z2;
  1746. }
  1747. for(i=0; i<4; i++){
  1748. const int z0= block[0*4 + i] + block[3*4 + i];
  1749. const int z3= block[0*4 + i] - block[3*4 + i];
  1750. const int z1= block[1*4 + i] + block[2*4 + i];
  1751. const int z2= block[1*4 + i] - block[2*4 + i];
  1752. block[0*4 + i]= z0 + z1;
  1753. block[1*4 + i]= 2*z3 + z2;
  1754. block[2*4 + i]= z0 - z1;
  1755. block[3*4 + i]= z3 - 2*z2;
  1756. }
  1757. }
  1758. #endif
  1759. //FIXME need to check that this doesnt overflow signed 32 bit for low qp, i am not sure, it's very close
  1760. //FIXME check that gcc inlines this (and optimizes intra & seperate_dc stuff away)
  1761. static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int seperate_dc){
  1762. int i;
  1763. const int * const quant_table= quant_coeff[qscale];
  1764. const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
  1765. const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
  1766. const unsigned int threshold2= (threshold1<<1);
  1767. int last_non_zero;
  1768. if(seperate_dc){
  1769. if(qscale<=18){
  1770. //avoid overflows
  1771. const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
  1772. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
  1773. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1774. int level= block[0]*quant_coeff[qscale+18][0];
  1775. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1776. if(level>0){
  1777. level= (dc_bias + level)>>(QUANT_SHIFT-2);
  1778. block[0]= level;
  1779. }else{
  1780. level= (dc_bias - level)>>(QUANT_SHIFT-2);
  1781. block[0]= -level;
  1782. }
  1783. // last_non_zero = i;
  1784. }else{
  1785. block[0]=0;
  1786. }
  1787. }else{
  1788. const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
  1789. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
  1790. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1791. int level= block[0]*quant_table[0];
  1792. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1793. if(level>0){
  1794. level= (dc_bias + level)>>(QUANT_SHIFT+1);
  1795. block[0]= level;
  1796. }else{
  1797. level= (dc_bias - level)>>(QUANT_SHIFT+1);
  1798. block[0]= -level;
  1799. }
  1800. // last_non_zero = i;
  1801. }else{
  1802. block[0]=0;
  1803. }
  1804. }
  1805. last_non_zero= 0;
  1806. i=1;
  1807. }else{
  1808. last_non_zero= -1;
  1809. i=0;
  1810. }
  1811. for(; i<16; i++){
  1812. const int j= scantable[i];
  1813. int level= block[j]*quant_table[j];
  1814. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  1815. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  1816. if(((unsigned)(level+threshold1))>threshold2){
  1817. if(level>0){
  1818. level= (bias + level)>>QUANT_SHIFT;
  1819. block[j]= level;
  1820. }else{
  1821. level= (bias - level)>>QUANT_SHIFT;
  1822. block[j]= -level;
  1823. }
  1824. last_non_zero = i;
  1825. }else{
  1826. block[j]=0;
  1827. }
  1828. }
  1829. return last_non_zero;
  1830. }
  1831. static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){
  1832. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1833. ((uint32_t*)(src+0*stride))[0]= a;
  1834. ((uint32_t*)(src+1*stride))[0]= a;
  1835. ((uint32_t*)(src+2*stride))[0]= a;
  1836. ((uint32_t*)(src+3*stride))[0]= a;
  1837. }
  1838. static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){
  1839. ((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101;
  1840. ((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101;
  1841. ((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101;
  1842. ((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101;
  1843. }
  1844. static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1845. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
  1846. + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
  1847. ((uint32_t*)(src+0*stride))[0]=
  1848. ((uint32_t*)(src+1*stride))[0]=
  1849. ((uint32_t*)(src+2*stride))[0]=
  1850. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1851. }
  1852. static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1853. const int dc= ( src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
  1854. ((uint32_t*)(src+0*stride))[0]=
  1855. ((uint32_t*)(src+1*stride))[0]=
  1856. ((uint32_t*)(src+2*stride))[0]=
  1857. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1858. }
  1859. static void pred4x4_top_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1860. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2;
  1861. ((uint32_t*)(src+0*stride))[0]=
  1862. ((uint32_t*)(src+1*stride))[0]=
  1863. ((uint32_t*)(src+2*stride))[0]=
  1864. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1865. }
  1866. static void pred4x4_128_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1867. ((uint32_t*)(src+0*stride))[0]=
  1868. ((uint32_t*)(src+1*stride))[0]=
  1869. ((uint32_t*)(src+2*stride))[0]=
  1870. ((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U;
  1871. }
  1872. #define LOAD_TOP_RIGHT_EDGE\
  1873. const int t4= topright[0];\
  1874. const int t5= topright[1];\
  1875. const int t6= topright[2];\
  1876. const int t7= topright[3];\
  1877. #define LOAD_LEFT_EDGE\
  1878. const int l0= src[-1+0*stride];\
  1879. const int l1= src[-1+1*stride];\
  1880. const int l2= src[-1+2*stride];\
  1881. const int l3= src[-1+3*stride];\
  1882. #define LOAD_TOP_EDGE\
  1883. const int t0= src[ 0-1*stride];\
  1884. const int t1= src[ 1-1*stride];\
  1885. const int t2= src[ 2-1*stride];\
  1886. const int t3= src[ 3-1*stride];\
  1887. static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){
  1888. const int lt= src[-1-1*stride];
  1889. LOAD_TOP_EDGE
  1890. LOAD_LEFT_EDGE
  1891. src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
  1892. src[0+2*stride]=
  1893. src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
  1894. src[0+1*stride]=
  1895. src[1+2*stride]=
  1896. src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
  1897. src[0+0*stride]=
  1898. src[1+1*stride]=
  1899. src[2+2*stride]=
  1900. src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1901. src[1+0*stride]=
  1902. src[2+1*stride]=
  1903. src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1904. src[2+0*stride]=
  1905. src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1906. src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1907. }
  1908. static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){
  1909. LOAD_TOP_EDGE
  1910. LOAD_TOP_RIGHT_EDGE
  1911. // LOAD_LEFT_EDGE
  1912. src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
  1913. src[1+0*stride]=
  1914. src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
  1915. src[2+0*stride]=
  1916. src[1+1*stride]=
  1917. src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
  1918. src[3+0*stride]=
  1919. src[2+1*stride]=
  1920. src[1+2*stride]=
  1921. src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
  1922. src[3+1*stride]=
  1923. src[2+2*stride]=
  1924. src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
  1925. src[3+2*stride]=
  1926. src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
  1927. src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
  1928. }
  1929. static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
  1930. const int lt= src[-1-1*stride];
  1931. LOAD_TOP_EDGE
  1932. LOAD_LEFT_EDGE
  1933. const __attribute__((unused)) int unu= l3;
  1934. src[0+0*stride]=
  1935. src[1+2*stride]=(lt + t0 + 1)>>1;
  1936. src[1+0*stride]=
  1937. src[2+2*stride]=(t0 + t1 + 1)>>1;
  1938. src[2+0*stride]=
  1939. src[3+2*stride]=(t1 + t2 + 1)>>1;
  1940. src[3+0*stride]=(t2 + t3 + 1)>>1;
  1941. src[0+1*stride]=
  1942. src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1943. src[1+1*stride]=
  1944. src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1945. src[2+1*stride]=
  1946. src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1947. src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1948. src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1949. src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1950. }
  1951. static void pred4x4_vertical_left_c(uint8_t *src, uint8_t *topright, int stride){
  1952. LOAD_TOP_EDGE
  1953. LOAD_TOP_RIGHT_EDGE
  1954. const __attribute__((unused)) int unu= t7;
  1955. src[0+0*stride]=(t0 + t1 + 1)>>1;
  1956. src[1+0*stride]=
  1957. src[0+2*stride]=(t1 + t2 + 1)>>1;
  1958. src[2+0*stride]=
  1959. src[1+2*stride]=(t2 + t3 + 1)>>1;
  1960. src[3+0*stride]=
  1961. src[2+2*stride]=(t3 + t4+ 1)>>1;
  1962. src[3+2*stride]=(t4 + t5+ 1)>>1;
  1963. src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1964. src[1+1*stride]=
  1965. src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1966. src[2+1*stride]=
  1967. src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
  1968. src[3+1*stride]=
  1969. src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
  1970. src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
  1971. }
  1972. static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){
  1973. LOAD_LEFT_EDGE
  1974. src[0+0*stride]=(l0 + l1 + 1)>>1;
  1975. src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1976. src[2+0*stride]=
  1977. src[0+1*stride]=(l1 + l2 + 1)>>1;
  1978. src[3+0*stride]=
  1979. src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1980. src[2+1*stride]=
  1981. src[0+2*stride]=(l2 + l3 + 1)>>1;
  1982. src[3+1*stride]=
  1983. src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
  1984. src[3+2*stride]=
  1985. src[1+3*stride]=
  1986. src[0+3*stride]=
  1987. src[2+2*stride]=
  1988. src[2+3*stride]=
  1989. src[3+3*stride]=l3;
  1990. }
  1991. static void pred4x4_horizontal_down_c(uint8_t *src, uint8_t *topright, int stride){
  1992. const int lt= src[-1-1*stride];
  1993. LOAD_TOP_EDGE
  1994. LOAD_LEFT_EDGE
  1995. const __attribute__((unused)) int unu= t3;
  1996. src[0+0*stride]=
  1997. src[2+1*stride]=(lt + l0 + 1)>>1;
  1998. src[1+0*stride]=
  1999. src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
  2000. src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
  2001. src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  2002. src[0+1*stride]=
  2003. src[2+2*stride]=(l0 + l1 + 1)>>1;
  2004. src[1+1*stride]=
  2005. src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  2006. src[0+2*stride]=
  2007. src[2+3*stride]=(l1 + l2+ 1)>>1;
  2008. src[1+2*stride]=
  2009. src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  2010. src[0+3*stride]=(l2 + l3 + 1)>>1;
  2011. src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  2012. }
  2013. void ff_pred16x16_vertical_c(uint8_t *src, int stride){
  2014. int i;
  2015. const uint32_t a= ((uint32_t*)(src-stride))[0];
  2016. const uint32_t b= ((uint32_t*)(src-stride))[1];
  2017. const uint32_t c= ((uint32_t*)(src-stride))[2];
  2018. const uint32_t d= ((uint32_t*)(src-stride))[3];
  2019. for(i=0; i<16; i++){
  2020. ((uint32_t*)(src+i*stride))[0]= a;
  2021. ((uint32_t*)(src+i*stride))[1]= b;
  2022. ((uint32_t*)(src+i*stride))[2]= c;
  2023. ((uint32_t*)(src+i*stride))[3]= d;
  2024. }
  2025. }
  2026. void ff_pred16x16_horizontal_c(uint8_t *src, int stride){
  2027. int i;
  2028. for(i=0; i<16; i++){
  2029. ((uint32_t*)(src+i*stride))[0]=
  2030. ((uint32_t*)(src+i*stride))[1]=
  2031. ((uint32_t*)(src+i*stride))[2]=
  2032. ((uint32_t*)(src+i*stride))[3]= src[-1+i*stride]*0x01010101;
  2033. }
  2034. }
  2035. void ff_pred16x16_dc_c(uint8_t *src, int stride){
  2036. int i, dc=0;
  2037. for(i=0;i<16; i++){
  2038. dc+= src[-1+i*stride];
  2039. }
  2040. for(i=0;i<16; i++){
  2041. dc+= src[i-stride];
  2042. }
  2043. dc= 0x01010101*((dc + 16)>>5);
  2044. for(i=0; i<16; i++){
  2045. ((uint32_t*)(src+i*stride))[0]=
  2046. ((uint32_t*)(src+i*stride))[1]=
  2047. ((uint32_t*)(src+i*stride))[2]=
  2048. ((uint32_t*)(src+i*stride))[3]= dc;
  2049. }
  2050. }
  2051. static void pred16x16_left_dc_c(uint8_t *src, int stride){
  2052. int i, dc=0;
  2053. for(i=0;i<16; i++){
  2054. dc+= src[-1+i*stride];
  2055. }
  2056. dc= 0x01010101*((dc + 8)>>4);
  2057. for(i=0; i<16; i++){
  2058. ((uint32_t*)(src+i*stride))[0]=
  2059. ((uint32_t*)(src+i*stride))[1]=
  2060. ((uint32_t*)(src+i*stride))[2]=
  2061. ((uint32_t*)(src+i*stride))[3]= dc;
  2062. }
  2063. }
  2064. static void pred16x16_top_dc_c(uint8_t *src, int stride){
  2065. int i, dc=0;
  2066. for(i=0;i<16; i++){
  2067. dc+= src[i-stride];
  2068. }
  2069. dc= 0x01010101*((dc + 8)>>4);
  2070. for(i=0; i<16; i++){
  2071. ((uint32_t*)(src+i*stride))[0]=
  2072. ((uint32_t*)(src+i*stride))[1]=
  2073. ((uint32_t*)(src+i*stride))[2]=
  2074. ((uint32_t*)(src+i*stride))[3]= dc;
  2075. }
  2076. }
  2077. void ff_pred16x16_128_dc_c(uint8_t *src, int stride){
  2078. int i;
  2079. for(i=0; i<16; i++){
  2080. ((uint32_t*)(src+i*stride))[0]=
  2081. ((uint32_t*)(src+i*stride))[1]=
  2082. ((uint32_t*)(src+i*stride))[2]=
  2083. ((uint32_t*)(src+i*stride))[3]= 0x01010101U*128U;
  2084. }
  2085. }
  2086. static inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){
  2087. int i, j, k;
  2088. int a;
  2089. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  2090. const uint8_t * const src0 = src+7-stride;
  2091. const uint8_t *src1 = src+8*stride-1;
  2092. const uint8_t *src2 = src1-2*stride; // == src+6*stride-1;
  2093. int H = src0[1] - src0[-1];
  2094. int V = src1[0] - src2[ 0];
  2095. for(k=2; k<=8; ++k) {
  2096. src1 += stride; src2 -= stride;
  2097. H += k*(src0[k] - src0[-k]);
  2098. V += k*(src1[0] - src2[ 0]);
  2099. }
  2100. if(svq3){
  2101. H = ( 5*(H/4) ) / 16;
  2102. V = ( 5*(V/4) ) / 16;
  2103. /* required for 100% accuracy */
  2104. i = H; H = V; V = i;
  2105. }else{
  2106. H = ( 5*H+32 ) >> 6;
  2107. V = ( 5*V+32 ) >> 6;
  2108. }
  2109. a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
  2110. for(j=16; j>0; --j) {
  2111. int b = a;
  2112. a += V;
  2113. for(i=-16; i<0; i+=4) {
  2114. src[16+i] = cm[ (b ) >> 5 ];
  2115. src[17+i] = cm[ (b+ H) >> 5 ];
  2116. src[18+i] = cm[ (b+2*H) >> 5 ];
  2117. src[19+i] = cm[ (b+3*H) >> 5 ];
  2118. b += 4*H;
  2119. }
  2120. src += stride;
  2121. }
  2122. }
  2123. void ff_pred16x16_plane_c(uint8_t *src, int stride){
  2124. pred16x16_plane_compat_c(src, stride, 0);
  2125. }
  2126. void ff_pred8x8_vertical_c(uint8_t *src, int stride){
  2127. int i;
  2128. const uint32_t a= ((uint32_t*)(src-stride))[0];
  2129. const uint32_t b= ((uint32_t*)(src-stride))[1];
  2130. for(i=0; i<8; i++){
  2131. ((uint32_t*)(src+i*stride))[0]= a;
  2132. ((uint32_t*)(src+i*stride))[1]= b;
  2133. }
  2134. }
  2135. void ff_pred8x8_horizontal_c(uint8_t *src, int stride){
  2136. int i;
  2137. for(i=0; i<8; i++){
  2138. ((uint32_t*)(src+i*stride))[0]=
  2139. ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101;
  2140. }
  2141. }
  2142. void ff_pred8x8_128_dc_c(uint8_t *src, int stride){
  2143. int i;
  2144. for(i=0; i<8; i++){
  2145. ((uint32_t*)(src+i*stride))[0]=
  2146. ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
  2147. }
  2148. }
  2149. static void pred8x8_left_dc_c(uint8_t *src, int stride){
  2150. int i;
  2151. int dc0, dc2;
  2152. dc0=dc2=0;
  2153. for(i=0;i<4; i++){
  2154. dc0+= src[-1+i*stride];
  2155. dc2+= src[-1+(i+4)*stride];
  2156. }
  2157. dc0= 0x01010101*((dc0 + 2)>>2);
  2158. dc2= 0x01010101*((dc2 + 2)>>2);
  2159. for(i=0; i<4; i++){
  2160. ((uint32_t*)(src+i*stride))[0]=
  2161. ((uint32_t*)(src+i*stride))[1]= dc0;
  2162. }
  2163. for(i=4; i<8; i++){
  2164. ((uint32_t*)(src+i*stride))[0]=
  2165. ((uint32_t*)(src+i*stride))[1]= dc2;
  2166. }
  2167. }
  2168. static void pred8x8_top_dc_c(uint8_t *src, int stride){
  2169. int i;
  2170. int dc0, dc1;
  2171. dc0=dc1=0;
  2172. for(i=0;i<4; i++){
  2173. dc0+= src[i-stride];
  2174. dc1+= src[4+i-stride];
  2175. }
  2176. dc0= 0x01010101*((dc0 + 2)>>2);
  2177. dc1= 0x01010101*((dc1 + 2)>>2);
  2178. for(i=0; i<4; i++){
  2179. ((uint32_t*)(src+i*stride))[0]= dc0;
  2180. ((uint32_t*)(src+i*stride))[1]= dc1;
  2181. }
  2182. for(i=4; i<8; i++){
  2183. ((uint32_t*)(src+i*stride))[0]= dc0;
  2184. ((uint32_t*)(src+i*stride))[1]= dc1;
  2185. }
  2186. }
  2187. void ff_pred8x8_dc_c(uint8_t *src, int stride){
  2188. int i;
  2189. int dc0, dc1, dc2, dc3;
  2190. dc0=dc1=dc2=0;
  2191. for(i=0;i<4; i++){
  2192. dc0+= src[-1+i*stride] + src[i-stride];
  2193. dc1+= src[4+i-stride];
  2194. dc2+= src[-1+(i+4)*stride];
  2195. }
  2196. dc3= 0x01010101*((dc1 + dc2 + 4)>>3);
  2197. dc0= 0x01010101*((dc0 + 4)>>3);
  2198. dc1= 0x01010101*((dc1 + 2)>>2);
  2199. dc2= 0x01010101*((dc2 + 2)>>2);
  2200. for(i=0; i<4; i++){
  2201. ((uint32_t*)(src+i*stride))[0]= dc0;
  2202. ((uint32_t*)(src+i*stride))[1]= dc1;
  2203. }
  2204. for(i=4; i<8; i++){
  2205. ((uint32_t*)(src+i*stride))[0]= dc2;
  2206. ((uint32_t*)(src+i*stride))[1]= dc3;
  2207. }
  2208. }
  2209. void ff_pred8x8_plane_c(uint8_t *src, int stride){
  2210. int j, k;
  2211. int a;
  2212. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  2213. const uint8_t * const src0 = src+3-stride;
  2214. const uint8_t *src1 = src+4*stride-1;
  2215. const uint8_t *src2 = src1-2*stride; // == src+2*stride-1;
  2216. int H = src0[1] - src0[-1];
  2217. int V = src1[0] - src2[ 0];
  2218. for(k=2; k<=4; ++k) {
  2219. src1 += stride; src2 -= stride;
  2220. H += k*(src0[k] - src0[-k]);
  2221. V += k*(src1[0] - src2[ 0]);
  2222. }
  2223. H = ( 17*H+16 ) >> 5;
  2224. V = ( 17*V+16 ) >> 5;
  2225. a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
  2226. for(j=8; j>0; --j) {
  2227. int b = a;
  2228. a += V;
  2229. src[0] = cm[ (b ) >> 5 ];
  2230. src[1] = cm[ (b+ H) >> 5 ];
  2231. src[2] = cm[ (b+2*H) >> 5 ];
  2232. src[3] = cm[ (b+3*H) >> 5 ];
  2233. src[4] = cm[ (b+4*H) >> 5 ];
  2234. src[5] = cm[ (b+5*H) >> 5 ];
  2235. src[6] = cm[ (b+6*H) >> 5 ];
  2236. src[7] = cm[ (b+7*H) >> 5 ];
  2237. src += stride;
  2238. }
  2239. }
  2240. #define SRC(x,y) src[(x)+(y)*stride]
  2241. #define PL(y) \
  2242. const int l##y = (SRC(-1,y-1) + 2*SRC(-1,y) + SRC(-1,y+1) + 2) >> 2;
  2243. #define PREDICT_8x8_LOAD_LEFT \
  2244. const int l0 = ((has_topleft ? SRC(-1,-1) : SRC(-1,0)) \
  2245. + 2*SRC(-1,0) + SRC(-1,1) + 2) >> 2; \
  2246. PL(1) PL(2) PL(3) PL(4) PL(5) PL(6) \
  2247. const int l7 attribute_unused = (SRC(-1,6) + 3*SRC(-1,7) + 2) >> 2
  2248. #define PT(x) \
  2249. const int t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  2250. #define PREDICT_8x8_LOAD_TOP \
  2251. const int t0 = ((has_topleft ? SRC(-1,-1) : SRC(0,-1)) \
  2252. + 2*SRC(0,-1) + SRC(1,-1) + 2) >> 2; \
  2253. PT(1) PT(2) PT(3) PT(4) PT(5) PT(6) \
  2254. const int t7 attribute_unused = ((has_topright ? SRC(8,-1) : SRC(7,-1)) \
  2255. + 2*SRC(7,-1) + SRC(6,-1) + 2) >> 2
  2256. #define PTR(x) \
  2257. t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  2258. #define PREDICT_8x8_LOAD_TOPRIGHT \
  2259. int t8, t9, t10, t11, t12, t13, t14, t15; \
  2260. if(has_topright) { \
  2261. PTR(8) PTR(9) PTR(10) PTR(11) PTR(12) PTR(13) PTR(14) \
  2262. t15 = (SRC(14,-1) + 3*SRC(15,-1) + 2) >> 2; \
  2263. } else t8=t9=t10=t11=t12=t13=t14=t15= SRC(7,-1);
  2264. #define PREDICT_8x8_LOAD_TOPLEFT \
  2265. const int lt = (SRC(-1,0) + 2*SRC(-1,-1) + SRC(0,-1) + 2) >> 2
  2266. #define PREDICT_8x8_DC(v) \
  2267. int y; \
  2268. for( y = 0; y < 8; y++ ) { \
  2269. ((uint32_t*)src)[0] = \
  2270. ((uint32_t*)src)[1] = v; \
  2271. src += stride; \
  2272. }
  2273. static void pred8x8l_128_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2274. {
  2275. PREDICT_8x8_DC(0x80808080);
  2276. }
  2277. static void pred8x8l_left_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2278. {
  2279. PREDICT_8x8_LOAD_LEFT;
  2280. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7+4) >> 3) * 0x01010101;
  2281. PREDICT_8x8_DC(dc);
  2282. }
  2283. static void pred8x8l_top_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2284. {
  2285. PREDICT_8x8_LOAD_TOP;
  2286. const uint32_t dc = ((t0+t1+t2+t3+t4+t5+t6+t7+4) >> 3) * 0x01010101;
  2287. PREDICT_8x8_DC(dc);
  2288. }
  2289. static void pred8x8l_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2290. {
  2291. PREDICT_8x8_LOAD_LEFT;
  2292. PREDICT_8x8_LOAD_TOP;
  2293. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7
  2294. +t0+t1+t2+t3+t4+t5+t6+t7+8) >> 4) * 0x01010101;
  2295. PREDICT_8x8_DC(dc);
  2296. }
  2297. static void pred8x8l_horizontal_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2298. {
  2299. PREDICT_8x8_LOAD_LEFT;
  2300. #define ROW(y) ((uint32_t*)(src+y*stride))[0] =\
  2301. ((uint32_t*)(src+y*stride))[1] = 0x01010101 * l##y
  2302. ROW(0); ROW(1); ROW(2); ROW(3); ROW(4); ROW(5); ROW(6); ROW(7);
  2303. #undef ROW
  2304. }
  2305. static void pred8x8l_vertical_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2306. {
  2307. int y;
  2308. PREDICT_8x8_LOAD_TOP;
  2309. src[0] = t0;
  2310. src[1] = t1;
  2311. src[2] = t2;
  2312. src[3] = t3;
  2313. src[4] = t4;
  2314. src[5] = t5;
  2315. src[6] = t6;
  2316. src[7] = t7;
  2317. for( y = 1; y < 8; y++ )
  2318. *(uint64_t*)(src+y*stride) = *(uint64_t*)src;
  2319. }
  2320. static void pred8x8l_down_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2321. {
  2322. PREDICT_8x8_LOAD_TOP;
  2323. PREDICT_8x8_LOAD_TOPRIGHT;
  2324. SRC(0,0)= (t0 + 2*t1 + t2 + 2) >> 2;
  2325. SRC(0,1)=SRC(1,0)= (t1 + 2*t2 + t3 + 2) >> 2;
  2326. SRC(0,2)=SRC(1,1)=SRC(2,0)= (t2 + 2*t3 + t4 + 2) >> 2;
  2327. SRC(0,3)=SRC(1,2)=SRC(2,1)=SRC(3,0)= (t3 + 2*t4 + t5 + 2) >> 2;
  2328. SRC(0,4)=SRC(1,3)=SRC(2,2)=SRC(3,1)=SRC(4,0)= (t4 + 2*t5 + t6 + 2) >> 2;
  2329. SRC(0,5)=SRC(1,4)=SRC(2,3)=SRC(3,2)=SRC(4,1)=SRC(5,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2330. 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;
  2331. 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;
  2332. 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;
  2333. SRC(2,7)=SRC(3,6)=SRC(4,5)=SRC(5,4)=SRC(6,3)=SRC(7,2)= (t9 + 2*t10 + t11 + 2) >> 2;
  2334. SRC(3,7)=SRC(4,6)=SRC(5,5)=SRC(6,4)=SRC(7,3)= (t10 + 2*t11 + t12 + 2) >> 2;
  2335. SRC(4,7)=SRC(5,6)=SRC(6,5)=SRC(7,4)= (t11 + 2*t12 + t13 + 2) >> 2;
  2336. SRC(5,7)=SRC(6,6)=SRC(7,5)= (t12 + 2*t13 + t14 + 2) >> 2;
  2337. SRC(6,7)=SRC(7,6)= (t13 + 2*t14 + t15 + 2) >> 2;
  2338. SRC(7,7)= (t14 + 3*t15 + 2) >> 2;
  2339. }
  2340. static void pred8x8l_down_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,7)= (l7 + 2*l6 + l5 + 2) >> 2;
  2346. SRC(0,6)=SRC(1,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2347. SRC(0,5)=SRC(1,6)=SRC(2,7)= (l5 + 2*l4 + l3 + 2) >> 2;
  2348. SRC(0,4)=SRC(1,5)=SRC(2,6)=SRC(3,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2349. SRC(0,3)=SRC(1,4)=SRC(2,5)=SRC(3,6)=SRC(4,7)= (l3 + 2*l2 + l1 + 2) >> 2;
  2350. SRC(0,2)=SRC(1,3)=SRC(2,4)=SRC(3,5)=SRC(4,6)=SRC(5,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2351. 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;
  2352. 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;
  2353. 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;
  2354. SRC(2,0)=SRC(3,1)=SRC(4,2)=SRC(5,3)=SRC(6,4)=SRC(7,5)= (t0 + 2*t1 + t2 + 2) >> 2;
  2355. SRC(3,0)=SRC(4,1)=SRC(5,2)=SRC(6,3)=SRC(7,4)= (t1 + 2*t2 + t3 + 2) >> 2;
  2356. SRC(4,0)=SRC(5,1)=SRC(6,2)=SRC(7,3)= (t2 + 2*t3 + t4 + 2) >> 2;
  2357. SRC(5,0)=SRC(6,1)=SRC(7,2)= (t3 + 2*t4 + t5 + 2) >> 2;
  2358. SRC(6,0)=SRC(7,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2359. SRC(7,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2360. }
  2361. static void pred8x8l_vertical_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2362. {
  2363. PREDICT_8x8_LOAD_TOP;
  2364. PREDICT_8x8_LOAD_LEFT;
  2365. PREDICT_8x8_LOAD_TOPLEFT;
  2366. SRC(0,6)= (l5 + 2*l4 + l3 + 2) >> 2;
  2367. SRC(0,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2368. SRC(0,4)=SRC(1,6)= (l3 + 2*l2 + l1 + 2) >> 2;
  2369. SRC(0,5)=SRC(1,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2370. SRC(0,2)=SRC(1,4)=SRC(2,6)= (l1 + 2*l0 + lt + 2) >> 2;
  2371. SRC(0,3)=SRC(1,5)=SRC(2,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2372. SRC(0,1)=SRC(1,3)=SRC(2,5)=SRC(3,7)= (l0 + 2*lt + t0 + 2) >> 2;
  2373. SRC(0,0)=SRC(1,2)=SRC(2,4)=SRC(3,6)= (lt + t0 + 1) >> 1;
  2374. SRC(1,1)=SRC(2,3)=SRC(3,5)=SRC(4,7)= (lt + 2*t0 + t1 + 2) >> 2;
  2375. SRC(1,0)=SRC(2,2)=SRC(3,4)=SRC(4,6)= (t0 + t1 + 1) >> 1;
  2376. SRC(2,1)=SRC(3,3)=SRC(4,5)=SRC(5,7)= (t0 + 2*t1 + t2 + 2) >> 2;
  2377. SRC(2,0)=SRC(3,2)=SRC(4,4)=SRC(5,6)= (t1 + t2 + 1) >> 1;
  2378. SRC(3,1)=SRC(4,3)=SRC(5,5)=SRC(6,7)= (t1 + 2*t2 + t3 + 2) >> 2;
  2379. SRC(3,0)=SRC(4,2)=SRC(5,4)=SRC(6,6)= (t2 + t3 + 1) >> 1;
  2380. SRC(4,1)=SRC(5,3)=SRC(6,5)=SRC(7,7)= (t2 + 2*t3 + t4 + 2) >> 2;
  2381. SRC(4,0)=SRC(5,2)=SRC(6,4)=SRC(7,6)= (t3 + t4 + 1) >> 1;
  2382. SRC(5,1)=SRC(6,3)=SRC(7,5)= (t3 + 2*t4 + t5 + 2) >> 2;
  2383. SRC(5,0)=SRC(6,2)=SRC(7,4)= (t4 + t5 + 1) >> 1;
  2384. SRC(6,1)=SRC(7,3)= (t4 + 2*t5 + t6 + 2) >> 2;
  2385. SRC(6,0)=SRC(7,2)= (t5 + t6 + 1) >> 1;
  2386. SRC(7,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2387. SRC(7,0)= (t6 + t7 + 1) >> 1;
  2388. }
  2389. static void pred8x8l_horizontal_down_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2390. {
  2391. PREDICT_8x8_LOAD_TOP;
  2392. PREDICT_8x8_LOAD_LEFT;
  2393. PREDICT_8x8_LOAD_TOPLEFT;
  2394. SRC(0,7)= (l6 + l7 + 1) >> 1;
  2395. SRC(1,7)= (l5 + 2*l6 + l7 + 2) >> 2;
  2396. SRC(0,6)=SRC(2,7)= (l5 + l6 + 1) >> 1;
  2397. SRC(1,6)=SRC(3,7)= (l4 + 2*l5 + l6 + 2) >> 2;
  2398. SRC(0,5)=SRC(2,6)=SRC(4,7)= (l4 + l5 + 1) >> 1;
  2399. SRC(1,5)=SRC(3,6)=SRC(5,7)= (l3 + 2*l4 + l5 + 2) >> 2;
  2400. SRC(0,4)=SRC(2,5)=SRC(4,6)=SRC(6,7)= (l3 + l4 + 1) >> 1;
  2401. SRC(1,4)=SRC(3,5)=SRC(5,6)=SRC(7,7)= (l2 + 2*l3 + l4 + 2) >> 2;
  2402. SRC(0,3)=SRC(2,4)=SRC(4,5)=SRC(6,6)= (l2 + l3 + 1) >> 1;
  2403. SRC(1,3)=SRC(3,4)=SRC(5,5)=SRC(7,6)= (l1 + 2*l2 + l3 + 2) >> 2;
  2404. SRC(0,2)=SRC(2,3)=SRC(4,4)=SRC(6,5)= (l1 + l2 + 1) >> 1;
  2405. SRC(1,2)=SRC(3,3)=SRC(5,4)=SRC(7,5)= (l0 + 2*l1 + l2 + 2) >> 2;
  2406. SRC(0,1)=SRC(2,2)=SRC(4,3)=SRC(6,4)= (l0 + l1 + 1) >> 1;
  2407. SRC(1,1)=SRC(3,2)=SRC(5,3)=SRC(7,4)= (lt + 2*l0 + l1 + 2) >> 2;
  2408. SRC(0,0)=SRC(2,1)=SRC(4,2)=SRC(6,3)= (lt + l0 + 1) >> 1;
  2409. SRC(1,0)=SRC(3,1)=SRC(5,2)=SRC(7,3)= (l0 + 2*lt + t0 + 2) >> 2;
  2410. SRC(2,0)=SRC(4,1)=SRC(6,2)= (t1 + 2*t0 + lt + 2) >> 2;
  2411. SRC(3,0)=SRC(5,1)=SRC(7,2)= (t2 + 2*t1 + t0 + 2) >> 2;
  2412. SRC(4,0)=SRC(6,1)= (t3 + 2*t2 + t1 + 2) >> 2;
  2413. SRC(5,0)=SRC(7,1)= (t4 + 2*t3 + t2 + 2) >> 2;
  2414. SRC(6,0)= (t5 + 2*t4 + t3 + 2) >> 2;
  2415. SRC(7,0)= (t6 + 2*t5 + t4 + 2) >> 2;
  2416. }
  2417. static void pred8x8l_vertical_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2418. {
  2419. PREDICT_8x8_LOAD_TOP;
  2420. PREDICT_8x8_LOAD_TOPRIGHT;
  2421. SRC(0,0)= (t0 + t1 + 1) >> 1;
  2422. SRC(0,1)= (t0 + 2*t1 + t2 + 2) >> 2;
  2423. SRC(0,2)=SRC(1,0)= (t1 + t2 + 1) >> 1;
  2424. SRC(0,3)=SRC(1,1)= (t1 + 2*t2 + t3 + 2) >> 2;
  2425. SRC(0,4)=SRC(1,2)=SRC(2,0)= (t2 + t3 + 1) >> 1;
  2426. SRC(0,5)=SRC(1,3)=SRC(2,1)= (t2 + 2*t3 + t4 + 2) >> 2;
  2427. SRC(0,6)=SRC(1,4)=SRC(2,2)=SRC(3,0)= (t3 + t4 + 1) >> 1;
  2428. SRC(0,7)=SRC(1,5)=SRC(2,3)=SRC(3,1)= (t3 + 2*t4 + t5 + 2) >> 2;
  2429. SRC(1,6)=SRC(2,4)=SRC(3,2)=SRC(4,0)= (t4 + t5 + 1) >> 1;
  2430. SRC(1,7)=SRC(2,5)=SRC(3,3)=SRC(4,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2431. SRC(2,6)=SRC(3,4)=SRC(4,2)=SRC(5,0)= (t5 + t6 + 1) >> 1;
  2432. SRC(2,7)=SRC(3,5)=SRC(4,3)=SRC(5,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2433. SRC(3,6)=SRC(4,4)=SRC(5,2)=SRC(6,0)= (t6 + t7 + 1) >> 1;
  2434. SRC(3,7)=SRC(4,5)=SRC(5,3)=SRC(6,1)= (t6 + 2*t7 + t8 + 2) >> 2;
  2435. SRC(4,6)=SRC(5,4)=SRC(6,2)=SRC(7,0)= (t7 + t8 + 1) >> 1;
  2436. SRC(4,7)=SRC(5,5)=SRC(6,3)=SRC(7,1)= (t7 + 2*t8 + t9 + 2) >> 2;
  2437. SRC(5,6)=SRC(6,4)=SRC(7,2)= (t8 + t9 + 1) >> 1;
  2438. SRC(5,7)=SRC(6,5)=SRC(7,3)= (t8 + 2*t9 + t10 + 2) >> 2;
  2439. SRC(6,6)=SRC(7,4)= (t9 + t10 + 1) >> 1;
  2440. SRC(6,7)=SRC(7,5)= (t9 + 2*t10 + t11 + 2) >> 2;
  2441. SRC(7,6)= (t10 + t11 + 1) >> 1;
  2442. SRC(7,7)= (t10 + 2*t11 + t12 + 2) >> 2;
  2443. }
  2444. static void pred8x8l_horizontal_up_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2445. {
  2446. PREDICT_8x8_LOAD_LEFT;
  2447. SRC(0,0)= (l0 + l1 + 1) >> 1;
  2448. SRC(1,0)= (l0 + 2*l1 + l2 + 2) >> 2;
  2449. SRC(0,1)=SRC(2,0)= (l1 + l2 + 1) >> 1;
  2450. SRC(1,1)=SRC(3,0)= (l1 + 2*l2 + l3 + 2) >> 2;
  2451. SRC(0,2)=SRC(2,1)=SRC(4,0)= (l2 + l3 + 1) >> 1;
  2452. SRC(1,2)=SRC(3,1)=SRC(5,0)= (l2 + 2*l3 + l4 + 2) >> 2;
  2453. SRC(0,3)=SRC(2,2)=SRC(4,1)=SRC(6,0)= (l3 + l4 + 1) >> 1;
  2454. SRC(1,3)=SRC(3,2)=SRC(5,1)=SRC(7,0)= (l3 + 2*l4 + l5 + 2) >> 2;
  2455. SRC(0,4)=SRC(2,3)=SRC(4,2)=SRC(6,1)= (l4 + l5 + 1) >> 1;
  2456. SRC(1,4)=SRC(3,3)=SRC(5,2)=SRC(7,1)= (l4 + 2*l5 + l6 + 2) >> 2;
  2457. SRC(0,5)=SRC(2,4)=SRC(4,3)=SRC(6,2)= (l5 + l6 + 1) >> 1;
  2458. SRC(1,5)=SRC(3,4)=SRC(5,3)=SRC(7,2)= (l5 + 2*l6 + l7 + 2) >> 2;
  2459. SRC(0,6)=SRC(2,5)=SRC(4,4)=SRC(6,3)= (l6 + l7 + 1) >> 1;
  2460. SRC(1,6)=SRC(3,5)=SRC(5,4)=SRC(7,3)= (l6 + 3*l7 + 2) >> 2;
  2461. SRC(0,7)=SRC(1,7)=SRC(2,6)=SRC(2,7)=SRC(3,6)=
  2462. SRC(3,7)=SRC(4,5)=SRC(4,6)=SRC(4,7)=SRC(5,5)=
  2463. SRC(5,6)=SRC(5,7)=SRC(6,4)=SRC(6,5)=SRC(6,6)=
  2464. SRC(6,7)=SRC(7,4)=SRC(7,5)=SRC(7,6)=SRC(7,7)= l7;
  2465. }
  2466. #undef PREDICT_8x8_LOAD_LEFT
  2467. #undef PREDICT_8x8_LOAD_TOP
  2468. #undef PREDICT_8x8_LOAD_TOPLEFT
  2469. #undef PREDICT_8x8_LOAD_TOPRIGHT
  2470. #undef PREDICT_8x8_DC
  2471. #undef PTR
  2472. #undef PT
  2473. #undef PL
  2474. #undef SRC
  2475. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  2476. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2477. int src_x_offset, int src_y_offset,
  2478. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  2479. MpegEncContext * const s = &h->s;
  2480. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  2481. int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  2482. const int luma_xy= (mx&3) + ((my&3)<<2);
  2483. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
  2484. uint8_t * src_cb, * src_cr;
  2485. int extra_width= h->emu_edge_width;
  2486. int extra_height= h->emu_edge_height;
  2487. int emu=0;
  2488. const int full_mx= mx>>2;
  2489. const int full_my= my>>2;
  2490. const int pic_width = 16*s->mb_width;
  2491. const int pic_height = 16*s->mb_height >> MB_MBAFF;
  2492. if(!pic->data[0])
  2493. return;
  2494. if(mx&7) extra_width -= 3;
  2495. if(my&7) extra_height -= 3;
  2496. if( full_mx < 0-extra_width
  2497. || full_my < 0-extra_height
  2498. || full_mx + 16/*FIXME*/ > pic_width + extra_width
  2499. || full_my + 16/*FIXME*/ > pic_height + extra_height){
  2500. 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);
  2501. src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
  2502. emu=1;
  2503. }
  2504. qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
  2505. if(!square){
  2506. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
  2507. }
  2508. if(s->flags&CODEC_FLAG_GRAY) return;
  2509. if(MB_MBAFF){
  2510. // chroma offset when predicting from a field of opposite parity
  2511. my += 2 * ((s->mb_y & 1) - (h->ref_cache[list][scan8[n]] & 1));
  2512. emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
  2513. }
  2514. src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2515. src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2516. if(emu){
  2517. 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);
  2518. src_cb= s->edge_emu_buffer;
  2519. }
  2520. chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2521. if(emu){
  2522. 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);
  2523. src_cr= s->edge_emu_buffer;
  2524. }
  2525. chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2526. }
  2527. static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
  2528. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2529. int x_offset, int y_offset,
  2530. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2531. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2532. int list0, int list1){
  2533. MpegEncContext * const s = &h->s;
  2534. qpel_mc_func *qpix_op= qpix_put;
  2535. h264_chroma_mc_func chroma_op= chroma_put;
  2536. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2537. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2538. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2539. x_offset += 8*s->mb_x;
  2540. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2541. if(list0){
  2542. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  2543. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  2544. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2545. qpix_op, chroma_op);
  2546. qpix_op= qpix_avg;
  2547. chroma_op= chroma_avg;
  2548. }
  2549. if(list1){
  2550. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  2551. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  2552. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2553. qpix_op, chroma_op);
  2554. }
  2555. }
  2556. static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
  2557. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2558. int x_offset, int y_offset,
  2559. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2560. h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
  2561. h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
  2562. int list0, int list1){
  2563. MpegEncContext * const s = &h->s;
  2564. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2565. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2566. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2567. x_offset += 8*s->mb_x;
  2568. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2569. if(list0 && list1){
  2570. /* don't optimize for luma-only case, since B-frames usually
  2571. * use implicit weights => chroma too. */
  2572. uint8_t *tmp_cb = s->obmc_scratchpad;
  2573. uint8_t *tmp_cr = s->obmc_scratchpad + 8;
  2574. uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
  2575. int refn0 = h->ref_cache[0][ scan8[n] ];
  2576. int refn1 = h->ref_cache[1][ scan8[n] ];
  2577. mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
  2578. dest_y, dest_cb, dest_cr,
  2579. x_offset, y_offset, qpix_put, chroma_put);
  2580. mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
  2581. tmp_y, tmp_cb, tmp_cr,
  2582. x_offset, y_offset, qpix_put, chroma_put);
  2583. if(h->use_weight == 2){
  2584. int weight0 = h->implicit_weight[refn0][refn1];
  2585. int weight1 = 64 - weight0;
  2586. luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
  2587. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2588. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2589. }else{
  2590. luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
  2591. h->luma_weight[0][refn0], h->luma_weight[1][refn1],
  2592. h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
  2593. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2594. h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
  2595. h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
  2596. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2597. h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
  2598. h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
  2599. }
  2600. }else{
  2601. int list = list1 ? 1 : 0;
  2602. int refn = h->ref_cache[list][ scan8[n] ];
  2603. Picture *ref= &h->ref_list[list][refn];
  2604. mc_dir_part(h, ref, n, square, chroma_height, delta, list,
  2605. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2606. qpix_put, chroma_put);
  2607. luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
  2608. h->luma_weight[list][refn], h->luma_offset[list][refn]);
  2609. if(h->use_weight_chroma){
  2610. chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2611. h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
  2612. chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2613. h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
  2614. }
  2615. }
  2616. }
  2617. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  2618. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2619. int x_offset, int y_offset,
  2620. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2621. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2622. h264_weight_func *weight_op, h264_biweight_func *weight_avg,
  2623. int list0, int list1){
  2624. if((h->use_weight==2 && list0 && list1
  2625. && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
  2626. || h->use_weight==1)
  2627. mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2628. x_offset, y_offset, qpix_put, chroma_put,
  2629. weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
  2630. else
  2631. mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2632. x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
  2633. }
  2634. static inline void prefetch_motion(H264Context *h, int list){
  2635. /* fetch pixels for estimated mv 4 macroblocks ahead
  2636. * optimized for 64byte cache lines */
  2637. MpegEncContext * const s = &h->s;
  2638. const int refn = h->ref_cache[list][scan8[0]];
  2639. if(refn >= 0){
  2640. const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
  2641. const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
  2642. uint8_t **src= h->ref_list[list][refn].data;
  2643. int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
  2644. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  2645. off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
  2646. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  2647. }
  2648. }
  2649. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2650. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  2651. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
  2652. h264_weight_func *weight_op, h264_biweight_func *weight_avg){
  2653. MpegEncContext * const s = &h->s;
  2654. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2655. const int mb_type= s->current_picture.mb_type[mb_xy];
  2656. assert(IS_INTER(mb_type));
  2657. prefetch_motion(h, 0);
  2658. if(IS_16X16(mb_type)){
  2659. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  2660. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  2661. &weight_op[0], &weight_avg[0],
  2662. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2663. }else if(IS_16X8(mb_type)){
  2664. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  2665. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2666. &weight_op[1], &weight_avg[1],
  2667. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2668. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  2669. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2670. &weight_op[1], &weight_avg[1],
  2671. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2672. }else if(IS_8X16(mb_type)){
  2673. mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
  2674. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2675. &weight_op[2], &weight_avg[2],
  2676. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2677. mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
  2678. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2679. &weight_op[2], &weight_avg[2],
  2680. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2681. }else{
  2682. int i;
  2683. assert(IS_8X8(mb_type));
  2684. for(i=0; i<4; i++){
  2685. const int sub_mb_type= h->sub_mb_type[i];
  2686. const int n= 4*i;
  2687. int x_offset= (i&1)<<2;
  2688. int y_offset= (i&2)<<1;
  2689. if(IS_SUB_8X8(sub_mb_type)){
  2690. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2691. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2692. &weight_op[3], &weight_avg[3],
  2693. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2694. }else if(IS_SUB_8X4(sub_mb_type)){
  2695. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2696. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2697. &weight_op[4], &weight_avg[4],
  2698. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2699. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  2700. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2701. &weight_op[4], &weight_avg[4],
  2702. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2703. }else if(IS_SUB_4X8(sub_mb_type)){
  2704. mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2705. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2706. &weight_op[5], &weight_avg[5],
  2707. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2708. mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
  2709. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2710. &weight_op[5], &weight_avg[5],
  2711. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2712. }else{
  2713. int j;
  2714. assert(IS_SUB_4X4(sub_mb_type));
  2715. for(j=0; j<4; j++){
  2716. int sub_x_offset= x_offset + 2*(j&1);
  2717. int sub_y_offset= y_offset + (j&2);
  2718. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  2719. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2720. &weight_op[6], &weight_avg[6],
  2721. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2722. }
  2723. }
  2724. }
  2725. }
  2726. prefetch_motion(h, 1);
  2727. }
  2728. static void decode_init_vlc(){
  2729. static int done = 0;
  2730. if (!done) {
  2731. int i;
  2732. done = 1;
  2733. init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
  2734. &chroma_dc_coeff_token_len [0], 1, 1,
  2735. &chroma_dc_coeff_token_bits[0], 1, 1, 1);
  2736. for(i=0; i<4; i++){
  2737. init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
  2738. &coeff_token_len [i][0], 1, 1,
  2739. &coeff_token_bits[i][0], 1, 1, 1);
  2740. }
  2741. for(i=0; i<3; i++){
  2742. init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
  2743. &chroma_dc_total_zeros_len [i][0], 1, 1,
  2744. &chroma_dc_total_zeros_bits[i][0], 1, 1, 1);
  2745. }
  2746. for(i=0; i<15; i++){
  2747. init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
  2748. &total_zeros_len [i][0], 1, 1,
  2749. &total_zeros_bits[i][0], 1, 1, 1);
  2750. }
  2751. for(i=0; i<6; i++){
  2752. init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
  2753. &run_len [i][0], 1, 1,
  2754. &run_bits[i][0], 1, 1, 1);
  2755. }
  2756. init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
  2757. &run_len [6][0], 1, 1,
  2758. &run_bits[6][0], 1, 1, 1);
  2759. }
  2760. }
  2761. /**
  2762. * Sets the intra prediction function pointers.
  2763. */
  2764. static void init_pred_ptrs(H264Context *h){
  2765. // MpegEncContext * const s = &h->s;
  2766. h->pred4x4[VERT_PRED ]= pred4x4_vertical_c;
  2767. h->pred4x4[HOR_PRED ]= pred4x4_horizontal_c;
  2768. h->pred4x4[DC_PRED ]= pred4x4_dc_c;
  2769. h->pred4x4[DIAG_DOWN_LEFT_PRED ]= pred4x4_down_left_c;
  2770. h->pred4x4[DIAG_DOWN_RIGHT_PRED]= pred4x4_down_right_c;
  2771. h->pred4x4[VERT_RIGHT_PRED ]= pred4x4_vertical_right_c;
  2772. h->pred4x4[HOR_DOWN_PRED ]= pred4x4_horizontal_down_c;
  2773. h->pred4x4[VERT_LEFT_PRED ]= pred4x4_vertical_left_c;
  2774. h->pred4x4[HOR_UP_PRED ]= pred4x4_horizontal_up_c;
  2775. h->pred4x4[LEFT_DC_PRED ]= pred4x4_left_dc_c;
  2776. h->pred4x4[TOP_DC_PRED ]= pred4x4_top_dc_c;
  2777. h->pred4x4[DC_128_PRED ]= pred4x4_128_dc_c;
  2778. h->pred8x8l[VERT_PRED ]= pred8x8l_vertical_c;
  2779. h->pred8x8l[HOR_PRED ]= pred8x8l_horizontal_c;
  2780. h->pred8x8l[DC_PRED ]= pred8x8l_dc_c;
  2781. h->pred8x8l[DIAG_DOWN_LEFT_PRED ]= pred8x8l_down_left_c;
  2782. h->pred8x8l[DIAG_DOWN_RIGHT_PRED]= pred8x8l_down_right_c;
  2783. h->pred8x8l[VERT_RIGHT_PRED ]= pred8x8l_vertical_right_c;
  2784. h->pred8x8l[HOR_DOWN_PRED ]= pred8x8l_horizontal_down_c;
  2785. h->pred8x8l[VERT_LEFT_PRED ]= pred8x8l_vertical_left_c;
  2786. h->pred8x8l[HOR_UP_PRED ]= pred8x8l_horizontal_up_c;
  2787. h->pred8x8l[LEFT_DC_PRED ]= pred8x8l_left_dc_c;
  2788. h->pred8x8l[TOP_DC_PRED ]= pred8x8l_top_dc_c;
  2789. h->pred8x8l[DC_128_PRED ]= pred8x8l_128_dc_c;
  2790. h->pred8x8[DC_PRED8x8 ]= ff_pred8x8_dc_c;
  2791. h->pred8x8[VERT_PRED8x8 ]= ff_pred8x8_vertical_c;
  2792. h->pred8x8[HOR_PRED8x8 ]= ff_pred8x8_horizontal_c;
  2793. h->pred8x8[PLANE_PRED8x8 ]= ff_pred8x8_plane_c;
  2794. h->pred8x8[LEFT_DC_PRED8x8]= pred8x8_left_dc_c;
  2795. h->pred8x8[TOP_DC_PRED8x8 ]= pred8x8_top_dc_c;
  2796. h->pred8x8[DC_128_PRED8x8 ]= ff_pred8x8_128_dc_c;
  2797. h->pred16x16[DC_PRED8x8 ]= ff_pred16x16_dc_c;
  2798. h->pred16x16[VERT_PRED8x8 ]= ff_pred16x16_vertical_c;
  2799. h->pred16x16[HOR_PRED8x8 ]= ff_pred16x16_horizontal_c;
  2800. h->pred16x16[PLANE_PRED8x8 ]= ff_pred16x16_plane_c;
  2801. h->pred16x16[LEFT_DC_PRED8x8]= pred16x16_left_dc_c;
  2802. h->pred16x16[TOP_DC_PRED8x8 ]= pred16x16_top_dc_c;
  2803. h->pred16x16[DC_128_PRED8x8 ]= ff_pred16x16_128_dc_c;
  2804. }
  2805. static void free_tables(H264Context *h){
  2806. av_freep(&h->intra4x4_pred_mode);
  2807. av_freep(&h->chroma_pred_mode_table);
  2808. av_freep(&h->cbp_table);
  2809. av_freep(&h->mvd_table[0]);
  2810. av_freep(&h->mvd_table[1]);
  2811. av_freep(&h->direct_table);
  2812. av_freep(&h->non_zero_count);
  2813. av_freep(&h->slice_table_base);
  2814. av_freep(&h->top_borders[1]);
  2815. av_freep(&h->top_borders[0]);
  2816. h->slice_table= NULL;
  2817. av_freep(&h->mb2b_xy);
  2818. av_freep(&h->mb2b8_xy);
  2819. av_freep(&h->s.obmc_scratchpad);
  2820. }
  2821. static void init_dequant8_coeff_table(H264Context *h){
  2822. int i,q,x;
  2823. const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
  2824. h->dequant8_coeff[0] = h->dequant8_buffer[0];
  2825. h->dequant8_coeff[1] = h->dequant8_buffer[1];
  2826. for(i=0; i<2; i++ ){
  2827. if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
  2828. h->dequant8_coeff[1] = h->dequant8_buffer[0];
  2829. break;
  2830. }
  2831. for(q=0; q<52; q++){
  2832. int shift = div6[q];
  2833. int idx = rem6[q];
  2834. for(x=0; x<64; x++)
  2835. h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
  2836. ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
  2837. h->pps.scaling_matrix8[i][x]) << shift;
  2838. }
  2839. }
  2840. }
  2841. static void init_dequant4_coeff_table(H264Context *h){
  2842. int i,j,q,x;
  2843. const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
  2844. for(i=0; i<6; i++ ){
  2845. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  2846. for(j=0; j<i; j++){
  2847. if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
  2848. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  2849. break;
  2850. }
  2851. }
  2852. if(j<i)
  2853. continue;
  2854. for(q=0; q<52; q++){
  2855. int shift = div6[q] + 2;
  2856. int idx = rem6[q];
  2857. for(x=0; x<16; x++)
  2858. h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
  2859. ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
  2860. h->pps.scaling_matrix4[i][x]) << shift;
  2861. }
  2862. }
  2863. }
  2864. static void init_dequant_tables(H264Context *h){
  2865. int i,x;
  2866. init_dequant4_coeff_table(h);
  2867. if(h->pps.transform_8x8_mode)
  2868. init_dequant8_coeff_table(h);
  2869. if(h->sps.transform_bypass){
  2870. for(i=0; i<6; i++)
  2871. for(x=0; x<16; x++)
  2872. h->dequant4_coeff[i][0][x] = 1<<6;
  2873. if(h->pps.transform_8x8_mode)
  2874. for(i=0; i<2; i++)
  2875. for(x=0; x<64; x++)
  2876. h->dequant8_coeff[i][0][x] = 1<<6;
  2877. }
  2878. }
  2879. /**
  2880. * allocates tables.
  2881. * needs width/height
  2882. */
  2883. static int alloc_tables(H264Context *h){
  2884. MpegEncContext * const s = &h->s;
  2885. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  2886. int x,y;
  2887. CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
  2888. CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
  2889. CHECKED_ALLOCZ(h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(uint8_t))
  2890. CHECKED_ALLOCZ(h->top_borders[0] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2891. CHECKED_ALLOCZ(h->top_borders[1] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2892. CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
  2893. if( h->pps.cabac ) {
  2894. CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
  2895. CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
  2896. CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t));
  2897. CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t));
  2898. }
  2899. memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(uint8_t));
  2900. h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
  2901. CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint32_t));
  2902. CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint32_t));
  2903. for(y=0; y<s->mb_height; y++){
  2904. for(x=0; x<s->mb_width; x++){
  2905. const int mb_xy= x + y*s->mb_stride;
  2906. const int b_xy = 4*x + 4*y*h->b_stride;
  2907. const int b8_xy= 2*x + 2*y*h->b8_stride;
  2908. h->mb2b_xy [mb_xy]= b_xy;
  2909. h->mb2b8_xy[mb_xy]= b8_xy;
  2910. }
  2911. }
  2912. s->obmc_scratchpad = NULL;
  2913. if(!h->dequant4_coeff[0])
  2914. init_dequant_tables(h);
  2915. return 0;
  2916. fail:
  2917. free_tables(h);
  2918. return -1;
  2919. }
  2920. static void common_init(H264Context *h){
  2921. MpegEncContext * const s = &h->s;
  2922. s->width = s->avctx->width;
  2923. s->height = s->avctx->height;
  2924. s->codec_id= s->avctx->codec->id;
  2925. init_pred_ptrs(h);
  2926. h->dequant_coeff_pps= -1;
  2927. s->unrestricted_mv=1;
  2928. s->decode=1; //FIXME
  2929. memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  2930. memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  2931. }
  2932. static int decode_init(AVCodecContext *avctx){
  2933. H264Context *h= avctx->priv_data;
  2934. MpegEncContext * const s = &h->s;
  2935. MPV_decode_defaults(s);
  2936. s->avctx = avctx;
  2937. common_init(h);
  2938. s->out_format = FMT_H264;
  2939. s->workaround_bugs= avctx->workaround_bugs;
  2940. // set defaults
  2941. // s->decode_mb= ff_h263_decode_mb;
  2942. s->low_delay= 1;
  2943. avctx->pix_fmt= PIX_FMT_YUV420P;
  2944. decode_init_vlc();
  2945. if(avctx->extradata_size > 0 && avctx->extradata &&
  2946. *(char *)avctx->extradata == 1){
  2947. h->is_avc = 1;
  2948. h->got_avcC = 0;
  2949. } else {
  2950. h->is_avc = 0;
  2951. }
  2952. return 0;
  2953. }
  2954. static int frame_start(H264Context *h){
  2955. MpegEncContext * const s = &h->s;
  2956. int i;
  2957. if(MPV_frame_start(s, s->avctx) < 0)
  2958. return -1;
  2959. ff_er_frame_start(s);
  2960. assert(s->linesize && s->uvlinesize);
  2961. for(i=0; i<16; i++){
  2962. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  2963. h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  2964. }
  2965. for(i=0; i<4; i++){
  2966. h->block_offset[16+i]=
  2967. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2968. h->block_offset[24+16+i]=
  2969. h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2970. }
  2971. /* can't be in alloc_tables because linesize isn't known there.
  2972. * FIXME: redo bipred weight to not require extra buffer? */
  2973. if(!s->obmc_scratchpad)
  2974. s->obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  2975. /* some macroblocks will be accessed before they're available */
  2976. if(FRAME_MBAFF)
  2977. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(uint8_t));
  2978. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  2979. return 0;
  2980. }
  2981. static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  2982. MpegEncContext * const s = &h->s;
  2983. int i;
  2984. src_y -= linesize;
  2985. src_cb -= uvlinesize;
  2986. src_cr -= uvlinesize;
  2987. // There are two lines saved, the line above the the top macroblock of a pair,
  2988. // and the line above the bottom macroblock
  2989. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  2990. for(i=1; i<17; i++){
  2991. h->left_border[i]= src_y[15+i* linesize];
  2992. }
  2993. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
  2994. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
  2995. if(!(s->flags&CODEC_FLAG_GRAY)){
  2996. h->left_border[17 ]= h->top_borders[0][s->mb_x][16+7];
  2997. h->left_border[17+9]= h->top_borders[0][s->mb_x][24+7];
  2998. for(i=1; i<9; i++){
  2999. h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
  3000. h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
  3001. }
  3002. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
  3003. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
  3004. }
  3005. }
  3006. 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){
  3007. MpegEncContext * const s = &h->s;
  3008. int temp8, i;
  3009. uint64_t temp64;
  3010. int deblock_left = (s->mb_x > 0);
  3011. int deblock_top = (s->mb_y > 0);
  3012. src_y -= linesize + 1;
  3013. src_cb -= uvlinesize + 1;
  3014. src_cr -= uvlinesize + 1;
  3015. #define XCHG(a,b,t,xchg)\
  3016. t= a;\
  3017. if(xchg)\
  3018. a= b;\
  3019. b= t;
  3020. if(deblock_left){
  3021. for(i = !deblock_top; i<17; i++){
  3022. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  3023. }
  3024. }
  3025. if(deblock_top){
  3026. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  3027. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  3028. if(s->mb_x+1 < s->mb_width){
  3029. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  3030. }
  3031. }
  3032. if(!(s->flags&CODEC_FLAG_GRAY)){
  3033. if(deblock_left){
  3034. for(i = !deblock_top; i<9; i++){
  3035. XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
  3036. XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
  3037. }
  3038. }
  3039. if(deblock_top){
  3040. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  3041. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  3042. }
  3043. }
  3044. }
  3045. static inline void backup_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  3046. MpegEncContext * const s = &h->s;
  3047. int i;
  3048. src_y -= 2 * linesize;
  3049. src_cb -= 2 * uvlinesize;
  3050. src_cr -= 2 * uvlinesize;
  3051. // There are two lines saved, the line above the the top macroblock of a pair,
  3052. // and the line above the bottom macroblock
  3053. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  3054. h->left_border[1]= h->top_borders[1][s->mb_x][15];
  3055. for(i=2; i<34; i++){
  3056. h->left_border[i]= src_y[15+i* linesize];
  3057. }
  3058. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 32*linesize);
  3059. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+32*linesize);
  3060. *(uint64_t*)(h->top_borders[1][s->mb_x]+0)= *(uint64_t*)(src_y + 33*linesize);
  3061. *(uint64_t*)(h->top_borders[1][s->mb_x]+8)= *(uint64_t*)(src_y +8+33*linesize);
  3062. if(!(s->flags&CODEC_FLAG_GRAY)){
  3063. h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7];
  3064. h->left_border[34+ 1]= h->top_borders[1][s->mb_x][16+7];
  3065. h->left_border[34+18 ]= h->top_borders[0][s->mb_x][24+7];
  3066. h->left_border[34+18+1]= h->top_borders[1][s->mb_x][24+7];
  3067. for(i=2; i<18; i++){
  3068. h->left_border[i+34 ]= src_cb[7+i*uvlinesize];
  3069. h->left_border[i+34+18]= src_cr[7+i*uvlinesize];
  3070. }
  3071. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+16*uvlinesize);
  3072. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+16*uvlinesize);
  3073. *(uint64_t*)(h->top_borders[1][s->mb_x]+16)= *(uint64_t*)(src_cb+17*uvlinesize);
  3074. *(uint64_t*)(h->top_borders[1][s->mb_x]+24)= *(uint64_t*)(src_cr+17*uvlinesize);
  3075. }
  3076. }
  3077. 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){
  3078. MpegEncContext * const s = &h->s;
  3079. int temp8, i;
  3080. uint64_t temp64;
  3081. int deblock_left = (s->mb_x > 0);
  3082. int deblock_top = (s->mb_y > 1);
  3083. 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);
  3084. src_y -= 2 * linesize + 1;
  3085. src_cb -= 2 * uvlinesize + 1;
  3086. src_cr -= 2 * uvlinesize + 1;
  3087. #define XCHG(a,b,t,xchg)\
  3088. t= a;\
  3089. if(xchg)\
  3090. a= b;\
  3091. b= t;
  3092. if(deblock_left){
  3093. for(i = (!deblock_top)<<1; i<34; i++){
  3094. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  3095. }
  3096. }
  3097. if(deblock_top){
  3098. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  3099. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  3100. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+0), *(uint64_t*)(src_y +1 +linesize), temp64, xchg);
  3101. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+8), *(uint64_t*)(src_y +9 +linesize), temp64, 1);
  3102. if(s->mb_x+1 < s->mb_width){
  3103. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  3104. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x+1]), *(uint64_t*)(src_y +17 +linesize), temp64, 1);
  3105. }
  3106. }
  3107. if(!(s->flags&CODEC_FLAG_GRAY)){
  3108. if(deblock_left){
  3109. for(i = (!deblock_top) << 1; i<18; i++){
  3110. XCHG(h->left_border[i+34 ], src_cb[i*uvlinesize], temp8, xchg);
  3111. XCHG(h->left_border[i+34+18], src_cr[i*uvlinesize], temp8, xchg);
  3112. }
  3113. }
  3114. if(deblock_top){
  3115. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  3116. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  3117. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+16), *(uint64_t*)(src_cb+1 +uvlinesize), temp64, 1);
  3118. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+24), *(uint64_t*)(src_cr+1 +uvlinesize), temp64, 1);
  3119. }
  3120. }
  3121. }
  3122. static void hl_decode_mb(H264Context *h){
  3123. MpegEncContext * const s = &h->s;
  3124. const int mb_x= s->mb_x;
  3125. const int mb_y= s->mb_y;
  3126. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3127. const int mb_type= s->current_picture.mb_type[mb_xy];
  3128. uint8_t *dest_y, *dest_cb, *dest_cr;
  3129. int linesize, uvlinesize /*dct_offset*/;
  3130. int i;
  3131. int *block_offset = &h->block_offset[0];
  3132. const unsigned int bottom = mb_y & 1;
  3133. const int transform_bypass = (s->qscale == 0 && h->sps.transform_bypass);
  3134. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  3135. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  3136. if(!s->decode)
  3137. return;
  3138. dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3139. dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3140. dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3141. s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
  3142. s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
  3143. if (MB_FIELD) {
  3144. linesize = h->mb_linesize = s->linesize * 2;
  3145. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  3146. block_offset = &h->block_offset[24];
  3147. if(mb_y&1){ //FIXME move out of this func?
  3148. dest_y -= s->linesize*15;
  3149. dest_cb-= s->uvlinesize*7;
  3150. dest_cr-= s->uvlinesize*7;
  3151. }
  3152. if(FRAME_MBAFF) {
  3153. int list;
  3154. for(list=0; list<2; list++){
  3155. if(!USES_LIST(mb_type, list))
  3156. continue;
  3157. if(IS_16X16(mb_type)){
  3158. int8_t *ref = &h->ref_cache[list][scan8[0]];
  3159. fill_rectangle(ref, 4, 4, 8, 16+*ref^(s->mb_y&1), 1);
  3160. }else{
  3161. for(i=0; i<16; i+=4){
  3162. //FIXME can refs be smaller than 8x8 when !direct_8x8_inference ?
  3163. int ref = h->ref_cache[list][scan8[i]];
  3164. if(ref >= 0)
  3165. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, 16+ref^(s->mb_y&1), 1);
  3166. }
  3167. }
  3168. }
  3169. }
  3170. } else {
  3171. linesize = h->mb_linesize = s->linesize;
  3172. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  3173. // dct_offset = s->linesize * 16;
  3174. }
  3175. if(transform_bypass){
  3176. idct_dc_add =
  3177. idct_add = IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  3178. }else if(IS_8x8DCT(mb_type)){
  3179. idct_dc_add = s->dsp.h264_idct8_dc_add;
  3180. idct_add = s->dsp.h264_idct8_add;
  3181. }else{
  3182. idct_dc_add = s->dsp.h264_idct_dc_add;
  3183. idct_add = s->dsp.h264_idct_add;
  3184. }
  3185. if(FRAME_MBAFF && h->deblocking_filter && IS_INTRA(mb_type)
  3186. && (!bottom || !IS_INTRA(s->current_picture.mb_type[mb_xy-s->mb_stride]))){
  3187. int mbt_y = mb_y&~1;
  3188. uint8_t *top_y = s->current_picture.data[0] + (mbt_y * 16* s->linesize ) + mb_x * 16;
  3189. uint8_t *top_cb = s->current_picture.data[1] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  3190. uint8_t *top_cr = s->current_picture.data[2] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  3191. xchg_pair_border(h, top_y, top_cb, top_cr, s->linesize, s->uvlinesize, 1);
  3192. }
  3193. if (IS_INTRA_PCM(mb_type)) {
  3194. unsigned int x, y;
  3195. // The pixels are stored in h->mb array in the same order as levels,
  3196. // copy them in output in the correct order.
  3197. for(i=0; i<16; i++) {
  3198. for (y=0; y<4; y++) {
  3199. for (x=0; x<4; x++) {
  3200. *(dest_y + block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x];
  3201. }
  3202. }
  3203. }
  3204. for(i=16; i<16+4; i++) {
  3205. for (y=0; y<4; y++) {
  3206. for (x=0; x<4; x++) {
  3207. *(dest_cb + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  3208. }
  3209. }
  3210. }
  3211. for(i=20; i<20+4; i++) {
  3212. for (y=0; y<4; y++) {
  3213. for (x=0; x<4; x++) {
  3214. *(dest_cr + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  3215. }
  3216. }
  3217. }
  3218. } else {
  3219. if(IS_INTRA(mb_type)){
  3220. if(h->deblocking_filter && !FRAME_MBAFF)
  3221. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1);
  3222. if(!(s->flags&CODEC_FLAG_GRAY)){
  3223. h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  3224. h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  3225. }
  3226. if(IS_INTRA4x4(mb_type)){
  3227. if(!s->encoding){
  3228. if(IS_8x8DCT(mb_type)){
  3229. for(i=0; i<16; i+=4){
  3230. uint8_t * const ptr= dest_y + block_offset[i];
  3231. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  3232. const int nnz = h->non_zero_count_cache[ scan8[i] ];
  3233. h->pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  3234. (h->topright_samples_available<<(i+1))&0x8000, linesize);
  3235. if(nnz){
  3236. if(nnz == 1 && h->mb[i*16])
  3237. idct_dc_add(ptr, h->mb + i*16, linesize);
  3238. else
  3239. idct_add(ptr, h->mb + i*16, linesize);
  3240. }
  3241. }
  3242. }else
  3243. for(i=0; i<16; i++){
  3244. uint8_t * const ptr= dest_y + block_offset[i];
  3245. uint8_t *topright;
  3246. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  3247. int nnz, tr;
  3248. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  3249. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  3250. assert(mb_y || linesize <= block_offset[i]);
  3251. if(!topright_avail){
  3252. tr= ptr[3 - linesize]*0x01010101;
  3253. topright= (uint8_t*) &tr;
  3254. }else
  3255. topright= ptr + 4 - linesize;
  3256. }else
  3257. topright= NULL;
  3258. h->pred4x4[ dir ](ptr, topright, linesize);
  3259. nnz = h->non_zero_count_cache[ scan8[i] ];
  3260. if(nnz){
  3261. if(s->codec_id == CODEC_ID_H264){
  3262. if(nnz == 1 && h->mb[i*16])
  3263. idct_dc_add(ptr, h->mb + i*16, linesize);
  3264. else
  3265. idct_add(ptr, h->mb + i*16, linesize);
  3266. }else
  3267. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  3268. }
  3269. }
  3270. }
  3271. }else{
  3272. h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  3273. if(s->codec_id == CODEC_ID_H264){
  3274. if(!transform_bypass)
  3275. h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[IS_INTRA(mb_type) ? 0:3][s->qscale][0]);
  3276. }else
  3277. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  3278. }
  3279. if(h->deblocking_filter && !FRAME_MBAFF)
  3280. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
  3281. }else if(s->codec_id == CODEC_ID_H264){
  3282. hl_motion(h, dest_y, dest_cb, dest_cr,
  3283. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  3284. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  3285. s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
  3286. }
  3287. if(!IS_INTRA4x4(mb_type)){
  3288. if(s->codec_id == CODEC_ID_H264){
  3289. if(IS_INTRA16x16(mb_type)){
  3290. for(i=0; i<16; i++){
  3291. if(h->non_zero_count_cache[ scan8[i] ])
  3292. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3293. else if(h->mb[i*16])
  3294. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3295. }
  3296. }else{
  3297. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  3298. for(i=0; i<16; i+=di){
  3299. int nnz = h->non_zero_count_cache[ scan8[i] ];
  3300. if(nnz){
  3301. if(nnz==1 && h->mb[i*16])
  3302. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3303. else
  3304. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3305. }
  3306. }
  3307. }
  3308. }else{
  3309. for(i=0; i<16; i++){
  3310. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  3311. uint8_t * const ptr= dest_y + block_offset[i];
  3312. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  3313. }
  3314. }
  3315. }
  3316. }
  3317. if(!(s->flags&CODEC_FLAG_GRAY)){
  3318. uint8_t *dest[2] = {dest_cb, dest_cr};
  3319. if(transform_bypass){
  3320. idct_add = idct_dc_add = s->dsp.add_pixels4;
  3321. }else{
  3322. idct_add = s->dsp.h264_idct_add;
  3323. idct_dc_add = s->dsp.h264_idct_dc_add;
  3324. 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]);
  3325. 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]);
  3326. }
  3327. if(s->codec_id == CODEC_ID_H264){
  3328. for(i=16; i<16+8; i++){
  3329. if(h->non_zero_count_cache[ scan8[i] ])
  3330. idct_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3331. else if(h->mb[i*16])
  3332. idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3333. }
  3334. }else{
  3335. for(i=16; i<16+8; i++){
  3336. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  3337. uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
  3338. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  3339. }
  3340. }
  3341. }
  3342. }
  3343. }
  3344. if(h->deblocking_filter) {
  3345. if (FRAME_MBAFF) {
  3346. //FIXME try deblocking one mb at a time?
  3347. // the reduction in load/storing mvs and such might outweigh the extra backup/xchg_border
  3348. const int mb_y = s->mb_y - 1;
  3349. uint8_t *pair_dest_y, *pair_dest_cb, *pair_dest_cr;
  3350. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3351. const int mb_type_top = s->current_picture.mb_type[mb_xy];
  3352. const int mb_type_bottom= s->current_picture.mb_type[mb_xy+s->mb_stride];
  3353. if (!bottom) return;
  3354. pair_dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3355. pair_dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3356. pair_dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3357. if(IS_INTRA(mb_type_top | mb_type_bottom))
  3358. xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0);
  3359. backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize);
  3360. // deblock a pair
  3361. // top
  3362. s->mb_y--;
  3363. 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);
  3364. fill_caches(h, mb_type_top, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3365. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy]);
  3366. filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize);
  3367. // bottom
  3368. s->mb_y++;
  3369. tprintf("call mbaff filter_mb\n");
  3370. fill_caches(h, mb_type_bottom, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3371. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy+s->mb_stride]);
  3372. filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3373. } else {
  3374. tprintf("call filter_mb\n");
  3375. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3376. fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3377. filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3378. }
  3379. }
  3380. }
  3381. /**
  3382. * fills the default_ref_list.
  3383. */
  3384. static int fill_default_ref_list(H264Context *h){
  3385. MpegEncContext * const s = &h->s;
  3386. int i;
  3387. int smallest_poc_greater_than_current = -1;
  3388. Picture sorted_short_ref[32];
  3389. if(h->slice_type==B_TYPE){
  3390. int out_i;
  3391. int limit= INT_MIN;
  3392. /* sort frame according to poc in B slice */
  3393. for(out_i=0; out_i<h->short_ref_count; out_i++){
  3394. int best_i=INT_MIN;
  3395. int best_poc=INT_MAX;
  3396. for(i=0; i<h->short_ref_count; i++){
  3397. const int poc= h->short_ref[i]->poc;
  3398. if(poc > limit && poc < best_poc){
  3399. best_poc= poc;
  3400. best_i= i;
  3401. }
  3402. }
  3403. assert(best_i != INT_MIN);
  3404. limit= best_poc;
  3405. sorted_short_ref[out_i]= *h->short_ref[best_i];
  3406. 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);
  3407. if (-1 == smallest_poc_greater_than_current) {
  3408. if (h->short_ref[best_i]->poc >= s->current_picture_ptr->poc) {
  3409. smallest_poc_greater_than_current = out_i;
  3410. }
  3411. }
  3412. }
  3413. }
  3414. if(s->picture_structure == PICT_FRAME){
  3415. if(h->slice_type==B_TYPE){
  3416. int list;
  3417. tprintf("current poc: %d, smallest_poc_greater_than_current: %d\n", s->current_picture_ptr->poc, smallest_poc_greater_than_current);
  3418. // find the largest poc
  3419. for(list=0; list<2; list++){
  3420. int index = 0;
  3421. int j= -99;
  3422. int step= list ? -1 : 1;
  3423. for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++, j+=step) {
  3424. while(j<0 || j>= h->short_ref_count){
  3425. if(j != -99 && step == (list ? -1 : 1))
  3426. return -1;
  3427. step = -step;
  3428. j= smallest_poc_greater_than_current + (step>>1);
  3429. }
  3430. if(sorted_short_ref[j].reference != 3) continue;
  3431. h->default_ref_list[list][index ]= sorted_short_ref[j];
  3432. h->default_ref_list[list][index++].pic_id= sorted_short_ref[j].frame_num;
  3433. }
  3434. for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){
  3435. if(h->long_ref[i] == NULL) continue;
  3436. if(h->long_ref[i]->reference != 3) continue;
  3437. h->default_ref_list[ list ][index ]= *h->long_ref[i];
  3438. h->default_ref_list[ list ][index++].pic_id= i;;
  3439. }
  3440. if(list && (smallest_poc_greater_than_current<=0 || smallest_poc_greater_than_current>=h->short_ref_count) && (1 < index)){
  3441. // swap the two first elements of L1 when
  3442. // L0 and L1 are identical
  3443. Picture temp= h->default_ref_list[1][0];
  3444. h->default_ref_list[1][0] = h->default_ref_list[1][1];
  3445. h->default_ref_list[1][1] = temp;
  3446. }
  3447. if(index < h->ref_count[ list ])
  3448. memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
  3449. }
  3450. }else{
  3451. int index=0;
  3452. for(i=0; i<h->short_ref_count; i++){
  3453. if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
  3454. h->default_ref_list[0][index ]= *h->short_ref[i];
  3455. h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
  3456. }
  3457. for(i = 0; i < 16; i++){
  3458. if(h->long_ref[i] == NULL) continue;
  3459. if(h->long_ref[i]->reference != 3) continue;
  3460. h->default_ref_list[0][index ]= *h->long_ref[i];
  3461. h->default_ref_list[0][index++].pic_id= i;;
  3462. }
  3463. if(index < h->ref_count[0])
  3464. memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
  3465. }
  3466. }else{ //FIELD
  3467. if(h->slice_type==B_TYPE){
  3468. }else{
  3469. //FIXME second field balh
  3470. }
  3471. }
  3472. #ifdef TRACE
  3473. for (i=0; i<h->ref_count[0]; i++) {
  3474. 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]);
  3475. }
  3476. if(h->slice_type==B_TYPE){
  3477. for (i=0; i<h->ref_count[1]; i++) {
  3478. 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]);
  3479. }
  3480. }
  3481. #endif
  3482. return 0;
  3483. }
  3484. static void print_short_term(H264Context *h);
  3485. static void print_long_term(H264Context *h);
  3486. static int decode_ref_pic_list_reordering(H264Context *h){
  3487. MpegEncContext * const s = &h->s;
  3488. int list, index;
  3489. print_short_term(h);
  3490. print_long_term(h);
  3491. if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move before func
  3492. for(list=0; list<2; list++){
  3493. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  3494. if(get_bits1(&s->gb)){
  3495. int pred= h->curr_pic_num;
  3496. for(index=0; ; index++){
  3497. int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
  3498. int pic_id;
  3499. int i;
  3500. Picture *ref = NULL;
  3501. if(reordering_of_pic_nums_idc==3)
  3502. break;
  3503. if(index >= h->ref_count[list]){
  3504. av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
  3505. return -1;
  3506. }
  3507. if(reordering_of_pic_nums_idc<3){
  3508. if(reordering_of_pic_nums_idc<2){
  3509. const int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  3510. if(abs_diff_pic_num >= h->max_pic_num){
  3511. av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  3512. return -1;
  3513. }
  3514. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  3515. else pred+= abs_diff_pic_num;
  3516. pred &= h->max_pic_num - 1;
  3517. for(i= h->short_ref_count-1; i>=0; i--){
  3518. ref = h->short_ref[i];
  3519. assert(ref->reference == 3);
  3520. assert(!ref->long_ref);
  3521. if(ref->data[0] != NULL && ref->frame_num == pred && ref->long_ref == 0) // ignore non existing pictures by testing data[0] pointer
  3522. break;
  3523. }
  3524. if(i>=0)
  3525. ref->pic_id= ref->frame_num;
  3526. }else{
  3527. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  3528. ref = h->long_ref[pic_id];
  3529. ref->pic_id= pic_id;
  3530. assert(ref->reference == 3);
  3531. assert(ref->long_ref);
  3532. i=0;
  3533. }
  3534. if (i < 0) {
  3535. av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  3536. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  3537. } else {
  3538. for(i=index; i+1<h->ref_count[list]; i++){
  3539. if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
  3540. break;
  3541. }
  3542. for(; i > index; i--){
  3543. h->ref_list[list][i]= h->ref_list[list][i-1];
  3544. }
  3545. h->ref_list[list][index]= *ref;
  3546. }
  3547. }else{
  3548. av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  3549. return -1;
  3550. }
  3551. }
  3552. }
  3553. if(h->slice_type!=B_TYPE) break;
  3554. }
  3555. for(list=0; list<2; list++){
  3556. for(index= 0; index < h->ref_count[list]; index++){
  3557. if(!h->ref_list[list][index].data[0])
  3558. h->ref_list[list][index]= s->current_picture;
  3559. }
  3560. if(h->slice_type!=B_TYPE) break;
  3561. }
  3562. if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)
  3563. direct_dist_scale_factor(h);
  3564. direct_ref_list_init(h);
  3565. return 0;
  3566. }
  3567. static void fill_mbaff_ref_list(H264Context *h){
  3568. int list, i, j;
  3569. for(list=0; list<2; list++){
  3570. for(i=0; i<h->ref_count[list]; i++){
  3571. Picture *frame = &h->ref_list[list][i];
  3572. Picture *field = &h->ref_list[list][16+2*i];
  3573. field[0] = *frame;
  3574. for(j=0; j<3; j++)
  3575. field[0].linesize[j] <<= 1;
  3576. field[1] = field[0];
  3577. for(j=0; j<3; j++)
  3578. field[1].data[j] += frame->linesize[j];
  3579. h->luma_weight[list][16+2*i] = h->luma_weight[list][16+2*i+1] = h->luma_weight[list][i];
  3580. h->luma_offset[list][16+2*i] = h->luma_offset[list][16+2*i+1] = h->luma_offset[list][i];
  3581. for(j=0; j<2; j++){
  3582. h->chroma_weight[list][16+2*i][j] = h->chroma_weight[list][16+2*i+1][j] = h->chroma_weight[list][i][j];
  3583. h->chroma_offset[list][16+2*i][j] = h->chroma_offset[list][16+2*i+1][j] = h->chroma_offset[list][i][j];
  3584. }
  3585. }
  3586. }
  3587. for(j=0; j<h->ref_count[1]; j++){
  3588. for(i=0; i<h->ref_count[0]; i++)
  3589. h->implicit_weight[j][16+2*i] = h->implicit_weight[j][16+2*i+1] = h->implicit_weight[j][i];
  3590. memcpy(h->implicit_weight[16+2*j], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3591. memcpy(h->implicit_weight[16+2*j+1], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3592. }
  3593. }
  3594. static int pred_weight_table(H264Context *h){
  3595. MpegEncContext * const s = &h->s;
  3596. int list, i;
  3597. int luma_def, chroma_def;
  3598. h->use_weight= 0;
  3599. h->use_weight_chroma= 0;
  3600. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  3601. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  3602. luma_def = 1<<h->luma_log2_weight_denom;
  3603. chroma_def = 1<<h->chroma_log2_weight_denom;
  3604. for(list=0; list<2; list++){
  3605. for(i=0; i<h->ref_count[list]; i++){
  3606. int luma_weight_flag, chroma_weight_flag;
  3607. luma_weight_flag= get_bits1(&s->gb);
  3608. if(luma_weight_flag){
  3609. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  3610. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  3611. if( h->luma_weight[list][i] != luma_def
  3612. || h->luma_offset[list][i] != 0)
  3613. h->use_weight= 1;
  3614. }else{
  3615. h->luma_weight[list][i]= luma_def;
  3616. h->luma_offset[list][i]= 0;
  3617. }
  3618. chroma_weight_flag= get_bits1(&s->gb);
  3619. if(chroma_weight_flag){
  3620. int j;
  3621. for(j=0; j<2; j++){
  3622. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  3623. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  3624. if( h->chroma_weight[list][i][j] != chroma_def
  3625. || h->chroma_offset[list][i][j] != 0)
  3626. h->use_weight_chroma= 1;
  3627. }
  3628. }else{
  3629. int j;
  3630. for(j=0; j<2; j++){
  3631. h->chroma_weight[list][i][j]= chroma_def;
  3632. h->chroma_offset[list][i][j]= 0;
  3633. }
  3634. }
  3635. }
  3636. if(h->slice_type != B_TYPE) break;
  3637. }
  3638. h->use_weight= h->use_weight || h->use_weight_chroma;
  3639. return 0;
  3640. }
  3641. static void implicit_weight_table(H264Context *h){
  3642. MpegEncContext * const s = &h->s;
  3643. int ref0, ref1;
  3644. int cur_poc = s->current_picture_ptr->poc;
  3645. if( h->ref_count[0] == 1 && h->ref_count[1] == 1
  3646. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  3647. h->use_weight= 0;
  3648. h->use_weight_chroma= 0;
  3649. return;
  3650. }
  3651. h->use_weight= 2;
  3652. h->use_weight_chroma= 2;
  3653. h->luma_log2_weight_denom= 5;
  3654. h->chroma_log2_weight_denom= 5;
  3655. for(ref0=0; ref0 < h->ref_count[0]; ref0++){
  3656. int poc0 = h->ref_list[0][ref0].poc;
  3657. for(ref1=0; ref1 < h->ref_count[1]; ref1++){
  3658. int poc1 = h->ref_list[1][ref1].poc;
  3659. int td = clip(poc1 - poc0, -128, 127);
  3660. if(td){
  3661. int tb = clip(cur_poc - poc0, -128, 127);
  3662. int tx = (16384 + (FFABS(td) >> 1)) / td;
  3663. int dist_scale_factor = clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
  3664. if(dist_scale_factor < -64 || dist_scale_factor > 128)
  3665. h->implicit_weight[ref0][ref1] = 32;
  3666. else
  3667. h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
  3668. }else
  3669. h->implicit_weight[ref0][ref1] = 32;
  3670. }
  3671. }
  3672. }
  3673. static inline void unreference_pic(H264Context *h, Picture *pic){
  3674. int i;
  3675. pic->reference=0;
  3676. if(pic == h->delayed_output_pic)
  3677. pic->reference=1;
  3678. else{
  3679. for(i = 0; h->delayed_pic[i]; i++)
  3680. if(pic == h->delayed_pic[i]){
  3681. pic->reference=1;
  3682. break;
  3683. }
  3684. }
  3685. }
  3686. /**
  3687. * instantaneous decoder refresh.
  3688. */
  3689. static void idr(H264Context *h){
  3690. int i;
  3691. for(i=0; i<16; i++){
  3692. if (h->long_ref[i] != NULL) {
  3693. unreference_pic(h, h->long_ref[i]);
  3694. h->long_ref[i]= NULL;
  3695. }
  3696. }
  3697. h->long_ref_count=0;
  3698. for(i=0; i<h->short_ref_count; i++){
  3699. unreference_pic(h, h->short_ref[i]);
  3700. h->short_ref[i]= NULL;
  3701. }
  3702. h->short_ref_count=0;
  3703. }
  3704. /* forget old pics after a seek */
  3705. static void flush_dpb(AVCodecContext *avctx){
  3706. H264Context *h= avctx->priv_data;
  3707. int i;
  3708. for(i=0; i<16; i++) {
  3709. if(h->delayed_pic[i])
  3710. h->delayed_pic[i]->reference= 0;
  3711. h->delayed_pic[i]= NULL;
  3712. }
  3713. if(h->delayed_output_pic)
  3714. h->delayed_output_pic->reference= 0;
  3715. h->delayed_output_pic= NULL;
  3716. idr(h);
  3717. if(h->s.current_picture_ptr)
  3718. h->s.current_picture_ptr->reference= 0;
  3719. }
  3720. /**
  3721. *
  3722. * @return the removed picture or NULL if an error occurs
  3723. */
  3724. static Picture * remove_short(H264Context *h, int frame_num){
  3725. MpegEncContext * const s = &h->s;
  3726. int i;
  3727. if(s->avctx->debug&FF_DEBUG_MMCO)
  3728. av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  3729. for(i=0; i<h->short_ref_count; i++){
  3730. Picture *pic= h->short_ref[i];
  3731. if(s->avctx->debug&FF_DEBUG_MMCO)
  3732. av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  3733. if(pic->frame_num == frame_num){
  3734. h->short_ref[i]= NULL;
  3735. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
  3736. h->short_ref_count--;
  3737. return pic;
  3738. }
  3739. }
  3740. return NULL;
  3741. }
  3742. /**
  3743. *
  3744. * @return the removed picture or NULL if an error occurs
  3745. */
  3746. static Picture * remove_long(H264Context *h, int i){
  3747. Picture *pic;
  3748. pic= h->long_ref[i];
  3749. h->long_ref[i]= NULL;
  3750. if(pic) h->long_ref_count--;
  3751. return pic;
  3752. }
  3753. /**
  3754. * print short term list
  3755. */
  3756. static void print_short_term(H264Context *h) {
  3757. uint32_t i;
  3758. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3759. av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
  3760. for(i=0; i<h->short_ref_count; i++){
  3761. Picture *pic= h->short_ref[i];
  3762. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3763. }
  3764. }
  3765. }
  3766. /**
  3767. * print long term list
  3768. */
  3769. static void print_long_term(H264Context *h) {
  3770. uint32_t i;
  3771. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3772. av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
  3773. for(i = 0; i < 16; i++){
  3774. Picture *pic= h->long_ref[i];
  3775. if (pic) {
  3776. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3777. }
  3778. }
  3779. }
  3780. }
  3781. /**
  3782. * Executes the reference picture marking (memory management control operations).
  3783. */
  3784. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  3785. MpegEncContext * const s = &h->s;
  3786. int i, j;
  3787. int current_is_long=0;
  3788. Picture *pic;
  3789. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  3790. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  3791. for(i=0; i<mmco_count; i++){
  3792. if(s->avctx->debug&FF_DEBUG_MMCO)
  3793. 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);
  3794. switch(mmco[i].opcode){
  3795. case MMCO_SHORT2UNUSED:
  3796. pic= remove_short(h, mmco[i].short_frame_num);
  3797. if(pic)
  3798. unreference_pic(h, pic);
  3799. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3800. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_short() failure\n");
  3801. break;
  3802. case MMCO_SHORT2LONG:
  3803. pic= remove_long(h, mmco[i].long_index);
  3804. if(pic) unreference_pic(h, pic);
  3805. h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
  3806. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3807. h->long_ref_count++;
  3808. break;
  3809. case MMCO_LONG2UNUSED:
  3810. pic= remove_long(h, mmco[i].long_index);
  3811. if(pic)
  3812. unreference_pic(h, pic);
  3813. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3814. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_long() failure\n");
  3815. break;
  3816. case MMCO_LONG:
  3817. pic= remove_long(h, mmco[i].long_index);
  3818. if(pic) unreference_pic(h, pic);
  3819. h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
  3820. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3821. h->long_ref_count++;
  3822. current_is_long=1;
  3823. break;
  3824. case MMCO_SET_MAX_LONG:
  3825. assert(mmco[i].long_index <= 16);
  3826. // just remove the long term which index is greater than new max
  3827. for(j = mmco[i].long_index; j<16; j++){
  3828. pic = remove_long(h, j);
  3829. if (pic) unreference_pic(h, pic);
  3830. }
  3831. break;
  3832. case MMCO_RESET:
  3833. while(h->short_ref_count){
  3834. pic= remove_short(h, h->short_ref[0]->frame_num);
  3835. unreference_pic(h, pic);
  3836. }
  3837. for(j = 0; j < 16; j++) {
  3838. pic= remove_long(h, j);
  3839. if(pic) unreference_pic(h, pic);
  3840. }
  3841. break;
  3842. default: assert(0);
  3843. }
  3844. }
  3845. if(!current_is_long){
  3846. pic= remove_short(h, s->current_picture_ptr->frame_num);
  3847. if(pic){
  3848. unreference_pic(h, pic);
  3849. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  3850. }
  3851. if(h->short_ref_count)
  3852. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  3853. h->short_ref[0]= s->current_picture_ptr;
  3854. h->short_ref[0]->long_ref=0;
  3855. h->short_ref_count++;
  3856. }
  3857. print_short_term(h);
  3858. print_long_term(h);
  3859. return 0;
  3860. }
  3861. static int decode_ref_pic_marking(H264Context *h){
  3862. MpegEncContext * const s = &h->s;
  3863. int i;
  3864. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  3865. s->broken_link= get_bits1(&s->gb) -1;
  3866. h->mmco[0].long_index= get_bits1(&s->gb) - 1; // current_long_term_idx
  3867. if(h->mmco[0].long_index == -1)
  3868. h->mmco_index= 0;
  3869. else{
  3870. h->mmco[0].opcode= MMCO_LONG;
  3871. h->mmco_index= 1;
  3872. }
  3873. }else{
  3874. if(get_bits1(&s->gb)){ // adaptive_ref_pic_marking_mode_flag
  3875. for(i= 0; i<MAX_MMCO_COUNT; i++) {
  3876. MMCOOpcode opcode= get_ue_golomb(&s->gb);;
  3877. h->mmco[i].opcode= opcode;
  3878. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  3879. 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
  3880. /* if(h->mmco[i].short_frame_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_frame_num ] == NULL){
  3881. av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
  3882. return -1;
  3883. }*/
  3884. }
  3885. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  3886. h->mmco[i].long_index= get_ue_golomb(&s->gb);
  3887. 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){
  3888. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  3889. return -1;
  3890. }
  3891. }
  3892. if(opcode > MMCO_LONG){
  3893. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  3894. return -1;
  3895. }
  3896. if(opcode == MMCO_END)
  3897. break;
  3898. }
  3899. h->mmco_index= i;
  3900. }else{
  3901. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  3902. if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){ //FIXME fields
  3903. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  3904. h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  3905. h->mmco_index= 1;
  3906. }else
  3907. h->mmco_index= 0;
  3908. }
  3909. }
  3910. return 0;
  3911. }
  3912. static int init_poc(H264Context *h){
  3913. MpegEncContext * const s = &h->s;
  3914. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  3915. int field_poc[2];
  3916. if(h->nal_unit_type == NAL_IDR_SLICE){
  3917. h->frame_num_offset= 0;
  3918. }else{
  3919. if(h->frame_num < h->prev_frame_num)
  3920. h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
  3921. else
  3922. h->frame_num_offset= h->prev_frame_num_offset;
  3923. }
  3924. if(h->sps.poc_type==0){
  3925. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  3926. if(h->nal_unit_type == NAL_IDR_SLICE){
  3927. h->prev_poc_msb=
  3928. h->prev_poc_lsb= 0;
  3929. }
  3930. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  3931. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  3932. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  3933. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  3934. else
  3935. h->poc_msb = h->prev_poc_msb;
  3936. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  3937. field_poc[0] =
  3938. field_poc[1] = h->poc_msb + h->poc_lsb;
  3939. if(s->picture_structure == PICT_FRAME)
  3940. field_poc[1] += h->delta_poc_bottom;
  3941. }else if(h->sps.poc_type==1){
  3942. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  3943. int i;
  3944. if(h->sps.poc_cycle_length != 0)
  3945. abs_frame_num = h->frame_num_offset + h->frame_num;
  3946. else
  3947. abs_frame_num = 0;
  3948. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  3949. abs_frame_num--;
  3950. expected_delta_per_poc_cycle = 0;
  3951. for(i=0; i < h->sps.poc_cycle_length; i++)
  3952. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  3953. if(abs_frame_num > 0){
  3954. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  3955. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  3956. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  3957. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  3958. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  3959. } else
  3960. expectedpoc = 0;
  3961. if(h->nal_ref_idc == 0)
  3962. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  3963. field_poc[0] = expectedpoc + h->delta_poc[0];
  3964. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  3965. if(s->picture_structure == PICT_FRAME)
  3966. field_poc[1] += h->delta_poc[1];
  3967. }else{
  3968. int poc;
  3969. if(h->nal_unit_type == NAL_IDR_SLICE){
  3970. poc= 0;
  3971. }else{
  3972. if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
  3973. else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
  3974. }
  3975. field_poc[0]= poc;
  3976. field_poc[1]= poc;
  3977. }
  3978. if(s->picture_structure != PICT_BOTTOM_FIELD)
  3979. s->current_picture_ptr->field_poc[0]= field_poc[0];
  3980. if(s->picture_structure != PICT_TOP_FIELD)
  3981. s->current_picture_ptr->field_poc[1]= field_poc[1];
  3982. if(s->picture_structure == PICT_FRAME) // FIXME field pix?
  3983. s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]);
  3984. return 0;
  3985. }
  3986. /**
  3987. * decodes a slice header.
  3988. * this will allso call MPV_common_init() and frame_start() as needed
  3989. */
  3990. static int decode_slice_header(H264Context *h){
  3991. MpegEncContext * const s = &h->s;
  3992. int first_mb_in_slice, pps_id;
  3993. int num_ref_idx_active_override_flag;
  3994. static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
  3995. int slice_type;
  3996. int default_ref_list_done = 0;
  3997. s->current_picture.reference= h->nal_ref_idc != 0;
  3998. s->dropable= h->nal_ref_idc == 0;
  3999. first_mb_in_slice= get_ue_golomb(&s->gb);
  4000. slice_type= get_ue_golomb(&s->gb);
  4001. if(slice_type > 9){
  4002. 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);
  4003. return -1;
  4004. }
  4005. if(slice_type > 4){
  4006. slice_type -= 5;
  4007. h->slice_type_fixed=1;
  4008. }else
  4009. h->slice_type_fixed=0;
  4010. slice_type= slice_type_map[ slice_type ];
  4011. if (slice_type == I_TYPE
  4012. || (h->slice_num != 0 && slice_type == h->slice_type) ) {
  4013. default_ref_list_done = 1;
  4014. }
  4015. h->slice_type= slice_type;
  4016. s->pict_type= h->slice_type; // to make a few old func happy, it's wrong though
  4017. pps_id= get_ue_golomb(&s->gb);
  4018. if(pps_id>255){
  4019. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  4020. return -1;
  4021. }
  4022. h->pps= h->pps_buffer[pps_id];
  4023. if(h->pps.slice_group_count == 0){
  4024. av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referenced\n");
  4025. return -1;
  4026. }
  4027. h->sps= h->sps_buffer[ h->pps.sps_id ];
  4028. if(h->sps.log2_max_frame_num == 0){
  4029. av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referenced\n");
  4030. return -1;
  4031. }
  4032. if(h->dequant_coeff_pps != pps_id){
  4033. h->dequant_coeff_pps = pps_id;
  4034. init_dequant_tables(h);
  4035. }
  4036. s->mb_width= h->sps.mb_width;
  4037. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  4038. h->b_stride= s->mb_width*4;
  4039. h->b8_stride= s->mb_width*2;
  4040. s->width = 16*s->mb_width - 2*(h->sps.crop_left + h->sps.crop_right );
  4041. if(h->sps.frame_mbs_only_flag)
  4042. s->height= 16*s->mb_height - 2*(h->sps.crop_top + h->sps.crop_bottom);
  4043. else
  4044. s->height= 16*s->mb_height - 4*(h->sps.crop_top + h->sps.crop_bottom); //FIXME recheck
  4045. if (s->context_initialized
  4046. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  4047. free_tables(h);
  4048. MPV_common_end(s);
  4049. }
  4050. if (!s->context_initialized) {
  4051. if (MPV_common_init(s) < 0)
  4052. return -1;
  4053. if(s->dsp.h264_idct_add == ff_h264_idct_add_c){ //FIXME little ugly
  4054. memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
  4055. memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
  4056. }else{
  4057. int i;
  4058. for(i=0; i<16; i++){
  4059. #define T(x) (x>>2) | ((x<<2) & 0xF)
  4060. h->zigzag_scan[i] = T(zigzag_scan[i]);
  4061. h-> field_scan[i] = T( field_scan[i]);
  4062. #undef T
  4063. }
  4064. }
  4065. if(s->dsp.h264_idct8_add == ff_h264_idct8_add_c){
  4066. memcpy(h->zigzag_scan8x8, zigzag_scan8x8, 64*sizeof(uint8_t));
  4067. memcpy(h->zigzag_scan8x8_cavlc, zigzag_scan8x8_cavlc, 64*sizeof(uint8_t));
  4068. memcpy(h->field_scan8x8, field_scan8x8, 64*sizeof(uint8_t));
  4069. memcpy(h->field_scan8x8_cavlc, field_scan8x8_cavlc, 64*sizeof(uint8_t));
  4070. }else{
  4071. int i;
  4072. for(i=0; i<64; i++){
  4073. #define T(x) (x>>3) | ((x&7)<<3)
  4074. h->zigzag_scan8x8[i] = T(zigzag_scan8x8[i]);
  4075. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  4076. h->field_scan8x8[i] = T(field_scan8x8[i]);
  4077. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  4078. #undef T
  4079. }
  4080. }
  4081. if(h->sps.transform_bypass){ //FIXME same ugly
  4082. h->zigzag_scan_q0 = zigzag_scan;
  4083. h->zigzag_scan8x8_q0 = zigzag_scan8x8;
  4084. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  4085. h->field_scan_q0 = field_scan;
  4086. h->field_scan8x8_q0 = field_scan8x8;
  4087. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  4088. }else{
  4089. h->zigzag_scan_q0 = h->zigzag_scan;
  4090. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  4091. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  4092. h->field_scan_q0 = h->field_scan;
  4093. h->field_scan8x8_q0 = h->field_scan8x8;
  4094. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  4095. }
  4096. alloc_tables(h);
  4097. s->avctx->width = s->width;
  4098. s->avctx->height = s->height;
  4099. s->avctx->sample_aspect_ratio= h->sps.sar;
  4100. if(!s->avctx->sample_aspect_ratio.den)
  4101. s->avctx->sample_aspect_ratio.den = 1;
  4102. if(h->sps.timing_info_present_flag){
  4103. s->avctx->time_base= (AVRational){h->sps.num_units_in_tick * 2, h->sps.time_scale};
  4104. if(h->x264_build > 0 && h->x264_build < 44)
  4105. s->avctx->time_base.den *= 2;
  4106. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  4107. s->avctx->time_base.num, s->avctx->time_base.den, 1<<30);
  4108. }
  4109. }
  4110. if(h->slice_num == 0){
  4111. if(frame_start(h) < 0)
  4112. return -1;
  4113. }
  4114. s->current_picture_ptr->frame_num= //FIXME frame_num cleanup
  4115. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  4116. h->mb_mbaff = 0;
  4117. h->mb_aff_frame = 0;
  4118. if(h->sps.frame_mbs_only_flag){
  4119. s->picture_structure= PICT_FRAME;
  4120. }else{
  4121. if(get_bits1(&s->gb)) { //field_pic_flag
  4122. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  4123. av_log(h->s.avctx, AV_LOG_ERROR, "PAFF interlacing is not implemented\n");
  4124. } else {
  4125. s->picture_structure= PICT_FRAME;
  4126. h->mb_aff_frame = h->sps.mb_aff;
  4127. }
  4128. }
  4129. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  4130. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << h->mb_aff_frame;
  4131. if(s->mb_y >= s->mb_height){
  4132. return -1;
  4133. }
  4134. if(s->picture_structure==PICT_FRAME){
  4135. h->curr_pic_num= h->frame_num;
  4136. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  4137. }else{
  4138. h->curr_pic_num= 2*h->frame_num;
  4139. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  4140. }
  4141. if(h->nal_unit_type == NAL_IDR_SLICE){
  4142. get_ue_golomb(&s->gb); /* idr_pic_id */
  4143. }
  4144. if(h->sps.poc_type==0){
  4145. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  4146. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  4147. h->delta_poc_bottom= get_se_golomb(&s->gb);
  4148. }
  4149. }
  4150. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  4151. h->delta_poc[0]= get_se_golomb(&s->gb);
  4152. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  4153. h->delta_poc[1]= get_se_golomb(&s->gb);
  4154. }
  4155. init_poc(h);
  4156. if(h->pps.redundant_pic_cnt_present){
  4157. h->redundant_pic_count= get_ue_golomb(&s->gb);
  4158. }
  4159. //set defaults, might be overriden a few line later
  4160. h->ref_count[0]= h->pps.ref_count[0];
  4161. h->ref_count[1]= h->pps.ref_count[1];
  4162. if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){
  4163. if(h->slice_type == B_TYPE){
  4164. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  4165. if(h->sps.mb_aff && h->direct_spatial_mv_pred)
  4166. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + spatial direct mode is not implemented\n");
  4167. }
  4168. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  4169. if(num_ref_idx_active_override_flag){
  4170. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  4171. if(h->slice_type==B_TYPE)
  4172. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  4173. if(h->ref_count[0] > 32 || h->ref_count[1] > 32){
  4174. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  4175. return -1;
  4176. }
  4177. }
  4178. }
  4179. if(!default_ref_list_done){
  4180. fill_default_ref_list(h);
  4181. }
  4182. if(decode_ref_pic_list_reordering(h) < 0)
  4183. return -1;
  4184. if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
  4185. || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
  4186. pred_weight_table(h);
  4187. else if(h->pps.weighted_bipred_idc==2 && h->slice_type==B_TYPE)
  4188. implicit_weight_table(h);
  4189. else
  4190. h->use_weight = 0;
  4191. if(s->current_picture.reference)
  4192. decode_ref_pic_marking(h);
  4193. if(FRAME_MBAFF)
  4194. fill_mbaff_ref_list(h);
  4195. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac )
  4196. h->cabac_init_idc = get_ue_golomb(&s->gb);
  4197. h->last_qscale_diff = 0;
  4198. s->qscale = h->pps.init_qp + get_se_golomb(&s->gb);
  4199. if(s->qscale<0 || s->qscale>51){
  4200. av_log(s->avctx, AV_LOG_ERROR, "QP %d out of range\n", s->qscale);
  4201. return -1;
  4202. }
  4203. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4204. //FIXME qscale / qp ... stuff
  4205. if(h->slice_type == SP_TYPE){
  4206. get_bits1(&s->gb); /* sp_for_switch_flag */
  4207. }
  4208. if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
  4209. get_se_golomb(&s->gb); /* slice_qs_delta */
  4210. }
  4211. h->deblocking_filter = 1;
  4212. h->slice_alpha_c0_offset = 0;
  4213. h->slice_beta_offset = 0;
  4214. if( h->pps.deblocking_filter_parameters_present ) {
  4215. h->deblocking_filter= get_ue_golomb(&s->gb);
  4216. if(h->deblocking_filter < 2)
  4217. h->deblocking_filter^= 1; // 1<->0
  4218. if( h->deblocking_filter ) {
  4219. h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
  4220. h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
  4221. }
  4222. }
  4223. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  4224. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type != I_TYPE)
  4225. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type == B_TYPE)
  4226. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  4227. h->deblocking_filter= 0;
  4228. #if 0 //FMO
  4229. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  4230. slice_group_change_cycle= get_bits(&s->gb, ?);
  4231. #endif
  4232. h->slice_num++;
  4233. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
  4234. h->emu_edge_height= FRAME_MBAFF ? 0 : h->emu_edge_width;
  4235. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  4236. av_log(h->s.avctx, AV_LOG_DEBUG, "slice:%d %s mb:%d %c pps:%d frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s\n",
  4237. h->slice_num,
  4238. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  4239. first_mb_in_slice,
  4240. av_get_pict_type_char(h->slice_type),
  4241. pps_id, h->frame_num,
  4242. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  4243. h->ref_count[0], h->ref_count[1],
  4244. s->qscale,
  4245. h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2,
  4246. h->use_weight,
  4247. h->use_weight==1 && h->use_weight_chroma ? "c" : ""
  4248. );
  4249. }
  4250. if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !s->current_picture.reference){
  4251. s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
  4252. s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
  4253. }else{
  4254. s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
  4255. s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
  4256. }
  4257. return 0;
  4258. }
  4259. /**
  4260. *
  4261. */
  4262. static inline int get_level_prefix(GetBitContext *gb){
  4263. unsigned int buf;
  4264. int log;
  4265. OPEN_READER(re, gb);
  4266. UPDATE_CACHE(re, gb);
  4267. buf=GET_CACHE(re, gb);
  4268. log= 32 - av_log2(buf);
  4269. #ifdef TRACE
  4270. print_bin(buf>>(32-log), log);
  4271. 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__);
  4272. #endif
  4273. LAST_SKIP_BITS(re, gb, log);
  4274. CLOSE_READER(re, gb);
  4275. return log-1;
  4276. }
  4277. static inline int get_dct8x8_allowed(H264Context *h){
  4278. int i;
  4279. for(i=0; i<4; i++){
  4280. if(!IS_SUB_8X8(h->sub_mb_type[i])
  4281. || (!h->sps.direct_8x8_inference_flag && IS_DIRECT(h->sub_mb_type[i])))
  4282. return 0;
  4283. }
  4284. return 1;
  4285. }
  4286. /**
  4287. * decodes a residual block.
  4288. * @param n block index
  4289. * @param scantable scantable
  4290. * @param max_coeff number of coefficients in the block
  4291. * @return <0 if an error occured
  4292. */
  4293. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
  4294. MpegEncContext * const s = &h->s;
  4295. 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};
  4296. int level[16];
  4297. int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
  4298. //FIXME put trailing_onex into the context
  4299. if(n == CHROMA_DC_BLOCK_INDEX){
  4300. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  4301. total_coeff= coeff_token>>2;
  4302. }else{
  4303. if(n == LUMA_DC_BLOCK_INDEX){
  4304. total_coeff= pred_non_zero_count(h, 0);
  4305. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4306. total_coeff= coeff_token>>2;
  4307. }else{
  4308. total_coeff= pred_non_zero_count(h, n);
  4309. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4310. total_coeff= coeff_token>>2;
  4311. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  4312. }
  4313. }
  4314. //FIXME set last_non_zero?
  4315. if(total_coeff==0)
  4316. return 0;
  4317. if(total_coeff<0) {
  4318. av_log(h->s.avctx, AV_LOG_ERROR, "corrupted macroblock %d %d (total_coeff<0)\n", s->mb_x, s->mb_y);
  4319. return -1;
  4320. }
  4321. trailing_ones= coeff_token&3;
  4322. tprintf("trailing:%d, total:%d\n", trailing_ones, total_coeff);
  4323. assert(total_coeff<=16);
  4324. for(i=0; i<trailing_ones; i++){
  4325. level[i]= 1 - 2*get_bits1(gb);
  4326. }
  4327. if(i<total_coeff) {
  4328. int level_code, mask;
  4329. int suffix_length = total_coeff > 10 && trailing_ones < 3;
  4330. int prefix= get_level_prefix(gb);
  4331. //first coefficient has suffix_length equal to 0 or 1
  4332. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  4333. if(suffix_length)
  4334. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4335. else
  4336. level_code= (prefix<<suffix_length); //part
  4337. }else if(prefix==14){
  4338. if(suffix_length)
  4339. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4340. else
  4341. level_code= prefix + get_bits(gb, 4); //part
  4342. }else if(prefix==15){
  4343. level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part
  4344. if(suffix_length==0) level_code+=15; //FIXME doesn't make (much)sense
  4345. }else{
  4346. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4347. return -1;
  4348. }
  4349. if(trailing_ones < 3) level_code += 2;
  4350. suffix_length = 1;
  4351. if(level_code > 5)
  4352. suffix_length++;
  4353. mask= -(level_code&1);
  4354. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4355. i++;
  4356. //remaining coefficients have suffix_length > 0
  4357. for(;i<total_coeff;i++) {
  4358. static const int suffix_limit[7] = {0,5,11,23,47,95,INT_MAX };
  4359. prefix = get_level_prefix(gb);
  4360. if(prefix<15){
  4361. level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
  4362. }else if(prefix==15){
  4363. level_code = (prefix<<suffix_length) + get_bits(gb, 12);
  4364. }else{
  4365. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4366. return -1;
  4367. }
  4368. mask= -(level_code&1);
  4369. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4370. if(level_code > suffix_limit[suffix_length])
  4371. suffix_length++;
  4372. }
  4373. }
  4374. if(total_coeff == max_coeff)
  4375. zeros_left=0;
  4376. else{
  4377. if(n == CHROMA_DC_BLOCK_INDEX)
  4378. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  4379. else
  4380. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  4381. }
  4382. coeff_num = zeros_left + total_coeff - 1;
  4383. j = scantable[coeff_num];
  4384. if(n > 24){
  4385. block[j] = level[0];
  4386. for(i=1;i<total_coeff;i++) {
  4387. if(zeros_left <= 0)
  4388. run_before = 0;
  4389. else if(zeros_left < 7){
  4390. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4391. }else{
  4392. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4393. }
  4394. zeros_left -= run_before;
  4395. coeff_num -= 1 + run_before;
  4396. j= scantable[ coeff_num ];
  4397. block[j]= level[i];
  4398. }
  4399. }else{
  4400. block[j] = (level[0] * qmul[j] + 32)>>6;
  4401. for(i=1;i<total_coeff;i++) {
  4402. if(zeros_left <= 0)
  4403. run_before = 0;
  4404. else if(zeros_left < 7){
  4405. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4406. }else{
  4407. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4408. }
  4409. zeros_left -= run_before;
  4410. coeff_num -= 1 + run_before;
  4411. j= scantable[ coeff_num ];
  4412. block[j]= (level[i] * qmul[j] + 32)>>6;
  4413. }
  4414. }
  4415. if(zeros_left<0){
  4416. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  4417. return -1;
  4418. }
  4419. return 0;
  4420. }
  4421. static void predict_field_decoding_flag(H264Context *h){
  4422. MpegEncContext * const s = &h->s;
  4423. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4424. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  4425. ? s->current_picture.mb_type[mb_xy-1]
  4426. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  4427. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  4428. : 0;
  4429. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  4430. }
  4431. /**
  4432. * decodes a P_SKIP or B_SKIP macroblock
  4433. */
  4434. static void decode_mb_skip(H264Context *h){
  4435. MpegEncContext * const s = &h->s;
  4436. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4437. int mb_type=0;
  4438. memset(h->non_zero_count[mb_xy], 0, 16);
  4439. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  4440. if(MB_FIELD)
  4441. mb_type|= MB_TYPE_INTERLACED;
  4442. if( h->slice_type == B_TYPE )
  4443. {
  4444. // just for fill_caches. pred_direct_motion will set the real mb_type
  4445. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
  4446. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4447. pred_direct_motion(h, &mb_type);
  4448. mb_type|= MB_TYPE_SKIP;
  4449. }
  4450. else
  4451. {
  4452. int mx, my;
  4453. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
  4454. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4455. pred_pskip_motion(h, &mx, &my);
  4456. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  4457. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  4458. }
  4459. write_back_motion(h, mb_type);
  4460. s->current_picture.mb_type[mb_xy]= mb_type;
  4461. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4462. h->slice_table[ mb_xy ]= h->slice_num;
  4463. h->prev_mb_skipped= 1;
  4464. }
  4465. /**
  4466. * decodes a macroblock
  4467. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  4468. */
  4469. static int decode_mb_cavlc(H264Context *h){
  4470. MpegEncContext * const s = &h->s;
  4471. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4472. int mb_type, partition_count, cbp;
  4473. int dct8x8_allowed= h->pps.transform_8x8_mode;
  4474. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?
  4475. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  4476. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  4477. down the code */
  4478. if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){
  4479. if(s->mb_skip_run==-1)
  4480. s->mb_skip_run= get_ue_golomb(&s->gb);
  4481. if (s->mb_skip_run--) {
  4482. if(FRAME_MBAFF && (s->mb_y&1) == 0){
  4483. if(s->mb_skip_run==0)
  4484. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4485. else
  4486. predict_field_decoding_flag(h);
  4487. }
  4488. decode_mb_skip(h);
  4489. return 0;
  4490. }
  4491. }
  4492. if(FRAME_MBAFF){
  4493. if( (s->mb_y&1) == 0 )
  4494. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4495. }else
  4496. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  4497. h->prev_mb_skipped= 0;
  4498. mb_type= get_ue_golomb(&s->gb);
  4499. if(h->slice_type == B_TYPE){
  4500. if(mb_type < 23){
  4501. partition_count= b_mb_type_info[mb_type].partition_count;
  4502. mb_type= b_mb_type_info[mb_type].type;
  4503. }else{
  4504. mb_type -= 23;
  4505. goto decode_intra_mb;
  4506. }
  4507. }else if(h->slice_type == P_TYPE /*|| h->slice_type == SP_TYPE */){
  4508. if(mb_type < 5){
  4509. partition_count= p_mb_type_info[mb_type].partition_count;
  4510. mb_type= p_mb_type_info[mb_type].type;
  4511. }else{
  4512. mb_type -= 5;
  4513. goto decode_intra_mb;
  4514. }
  4515. }else{
  4516. assert(h->slice_type == I_TYPE);
  4517. decode_intra_mb:
  4518. if(mb_type > 25){
  4519. 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);
  4520. return -1;
  4521. }
  4522. partition_count=0;
  4523. cbp= i_mb_type_info[mb_type].cbp;
  4524. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  4525. mb_type= i_mb_type_info[mb_type].type;
  4526. }
  4527. if(MB_FIELD)
  4528. mb_type |= MB_TYPE_INTERLACED;
  4529. h->slice_table[ mb_xy ]= h->slice_num;
  4530. if(IS_INTRA_PCM(mb_type)){
  4531. unsigned int x, y;
  4532. // we assume these blocks are very rare so we dont optimize it
  4533. align_get_bits(&s->gb);
  4534. // The pixels are stored in the same order as levels in h->mb array.
  4535. for(y=0; y<16; y++){
  4536. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  4537. for(x=0; x<16; x++){
  4538. tprintf("LUMA ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4539. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= get_bits(&s->gb, 8);
  4540. }
  4541. }
  4542. for(y=0; y<8; y++){
  4543. const int index= 256 + 4*(y&3) + 32*(y>>2);
  4544. for(x=0; x<8; x++){
  4545. tprintf("CHROMA U ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4546. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4547. }
  4548. }
  4549. for(y=0; y<8; y++){
  4550. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  4551. for(x=0; x<8; x++){
  4552. tprintf("CHROMA V ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4553. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4554. }
  4555. }
  4556. // In deblocking, the quantizer is 0
  4557. s->current_picture.qscale_table[mb_xy]= 0;
  4558. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  4559. // All coeffs are present
  4560. memset(h->non_zero_count[mb_xy], 16, 16);
  4561. s->current_picture.mb_type[mb_xy]= mb_type;
  4562. return 0;
  4563. }
  4564. if(MB_MBAFF){
  4565. h->ref_count[0] <<= 1;
  4566. h->ref_count[1] <<= 1;
  4567. }
  4568. fill_caches(h, mb_type, 0);
  4569. //mb_pred
  4570. if(IS_INTRA(mb_type)){
  4571. // init_top_left_availability(h);
  4572. if(IS_INTRA4x4(mb_type)){
  4573. int i;
  4574. int di = 1;
  4575. if(dct8x8_allowed && get_bits1(&s->gb)){
  4576. mb_type |= MB_TYPE_8x8DCT;
  4577. di = 4;
  4578. }
  4579. // fill_intra4x4_pred_table(h);
  4580. for(i=0; i<16; i+=di){
  4581. int mode= pred_intra_mode(h, i);
  4582. if(!get_bits1(&s->gb)){
  4583. const int rem_mode= get_bits(&s->gb, 3);
  4584. mode = rem_mode + (rem_mode >= mode);
  4585. }
  4586. if(di==4)
  4587. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  4588. else
  4589. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  4590. }
  4591. write_back_intra_pred_mode(h);
  4592. if( check_intra4x4_pred_mode(h) < 0)
  4593. return -1;
  4594. }else{
  4595. h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
  4596. if(h->intra16x16_pred_mode < 0)
  4597. return -1;
  4598. }
  4599. h->chroma_pred_mode= get_ue_golomb(&s->gb);
  4600. h->chroma_pred_mode= check_intra_pred_mode(h, h->chroma_pred_mode);
  4601. if(h->chroma_pred_mode < 0)
  4602. return -1;
  4603. }else if(partition_count==4){
  4604. int i, j, sub_partition_count[4], list, ref[2][4];
  4605. if(h->slice_type == B_TYPE){
  4606. for(i=0; i<4; i++){
  4607. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4608. if(h->sub_mb_type[i] >=13){
  4609. 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);
  4610. return -1;
  4611. }
  4612. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4613. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4614. }
  4615. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  4616. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
  4617. pred_direct_motion(h, &mb_type);
  4618. h->ref_cache[0][scan8[4]] =
  4619. h->ref_cache[1][scan8[4]] =
  4620. h->ref_cache[0][scan8[12]] =
  4621. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  4622. }
  4623. }else{
  4624. assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE); //FIXME SP correct ?
  4625. for(i=0; i<4; i++){
  4626. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4627. if(h->sub_mb_type[i] >=4){
  4628. 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);
  4629. return -1;
  4630. }
  4631. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4632. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4633. }
  4634. }
  4635. for(list=0; list<2; list++){
  4636. int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4637. if(ref_count == 0) continue;
  4638. for(i=0; i<4; i++){
  4639. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  4640. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4641. ref[list][i] = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
  4642. }else{
  4643. //FIXME
  4644. ref[list][i] = -1;
  4645. }
  4646. }
  4647. }
  4648. if(dct8x8_allowed)
  4649. dct8x8_allowed = get_dct8x8_allowed(h);
  4650. for(list=0; list<2; list++){
  4651. const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4652. if(ref_count == 0) continue;
  4653. for(i=0; i<4; i++){
  4654. if(IS_DIRECT(h->sub_mb_type[i])) {
  4655. h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];
  4656. continue;
  4657. }
  4658. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  4659. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  4660. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4661. const int sub_mb_type= h->sub_mb_type[i];
  4662. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  4663. for(j=0; j<sub_partition_count[i]; j++){
  4664. int mx, my;
  4665. const int index= 4*i + block_width*j;
  4666. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  4667. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  4668. mx += get_se_golomb(&s->gb);
  4669. my += get_se_golomb(&s->gb);
  4670. tprintf("final mv:%d %d\n", mx, my);
  4671. if(IS_SUB_8X8(sub_mb_type)){
  4672. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  4673. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  4674. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  4675. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  4676. }else if(IS_SUB_8X4(sub_mb_type)){
  4677. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  4678. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  4679. }else if(IS_SUB_4X8(sub_mb_type)){
  4680. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  4681. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  4682. }else{
  4683. assert(IS_SUB_4X4(sub_mb_type));
  4684. mv_cache[ 0 ][0]= mx;
  4685. mv_cache[ 0 ][1]= my;
  4686. }
  4687. }
  4688. }else{
  4689. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  4690. p[0] = p[1]=
  4691. p[8] = p[9]= 0;
  4692. }
  4693. }
  4694. }
  4695. }else if(IS_DIRECT(mb_type)){
  4696. pred_direct_motion(h, &mb_type);
  4697. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  4698. }else{
  4699. int list, mx, my, i;
  4700. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  4701. if(IS_16X16(mb_type)){
  4702. for(list=0; list<2; list++){
  4703. if(h->ref_count[list]>0){
  4704. if(IS_DIR(mb_type, 0, list)){
  4705. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4706. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  4707. }else
  4708. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4709. }
  4710. }
  4711. for(list=0; list<2; list++){
  4712. if(IS_DIR(mb_type, 0, list)){
  4713. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  4714. mx += get_se_golomb(&s->gb);
  4715. my += get_se_golomb(&s->gb);
  4716. tprintf("final mv:%d %d\n", mx, my);
  4717. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  4718. }else
  4719. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  4720. }
  4721. }
  4722. else if(IS_16X8(mb_type)){
  4723. for(list=0; list<2; list++){
  4724. if(h->ref_count[list]>0){
  4725. for(i=0; i<2; i++){
  4726. if(IS_DIR(mb_type, i, list)){
  4727. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4728. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  4729. }else
  4730. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  4731. }
  4732. }
  4733. }
  4734. for(list=0; list<2; list++){
  4735. for(i=0; i<2; i++){
  4736. if(IS_DIR(mb_type, i, list)){
  4737. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  4738. mx += get_se_golomb(&s->gb);
  4739. my += get_se_golomb(&s->gb);
  4740. tprintf("final mv:%d %d\n", mx, my);
  4741. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  4742. }else
  4743. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  4744. }
  4745. }
  4746. }else{
  4747. assert(IS_8X16(mb_type));
  4748. for(list=0; list<2; list++){
  4749. if(h->ref_count[list]>0){
  4750. for(i=0; i<2; i++){
  4751. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  4752. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4753. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  4754. }else
  4755. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4756. }
  4757. }
  4758. }
  4759. for(list=0; list<2; list++){
  4760. for(i=0; i<2; i++){
  4761. if(IS_DIR(mb_type, i, list)){
  4762. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  4763. mx += get_se_golomb(&s->gb);
  4764. my += get_se_golomb(&s->gb);
  4765. tprintf("final mv:%d %d\n", mx, my);
  4766. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  4767. }else
  4768. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  4769. }
  4770. }
  4771. }
  4772. }
  4773. if(IS_INTER(mb_type))
  4774. write_back_motion(h, mb_type);
  4775. if(!IS_INTRA16x16(mb_type)){
  4776. cbp= get_ue_golomb(&s->gb);
  4777. if(cbp > 47){
  4778. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%d) at %d %d\n", cbp, s->mb_x, s->mb_y);
  4779. return -1;
  4780. }
  4781. if(IS_INTRA4x4(mb_type))
  4782. cbp= golomb_to_intra4x4_cbp[cbp];
  4783. else
  4784. cbp= golomb_to_inter_cbp[cbp];
  4785. }
  4786. h->cbp = cbp;
  4787. if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
  4788. if(get_bits1(&s->gb))
  4789. mb_type |= MB_TYPE_8x8DCT;
  4790. }
  4791. s->current_picture.mb_type[mb_xy]= mb_type;
  4792. if(cbp || IS_INTRA16x16(mb_type)){
  4793. int i8x8, i4x4, chroma_idx;
  4794. int chroma_qp, dquant;
  4795. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  4796. const uint8_t *scan, *scan8x8, *dc_scan;
  4797. // fill_non_zero_count_cache(h);
  4798. if(IS_INTERLACED(mb_type)){
  4799. scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
  4800. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  4801. dc_scan= luma_dc_field_scan;
  4802. }else{
  4803. scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
  4804. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  4805. dc_scan= luma_dc_zigzag_scan;
  4806. }
  4807. dquant= get_se_golomb(&s->gb);
  4808. if( dquant > 25 || dquant < -26 ){
  4809. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  4810. return -1;
  4811. }
  4812. s->qscale += dquant;
  4813. if(((unsigned)s->qscale) > 51){
  4814. if(s->qscale<0) s->qscale+= 52;
  4815. else s->qscale-= 52;
  4816. }
  4817. h->chroma_qp= chroma_qp= get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4818. if(IS_INTRA16x16(mb_type)){
  4819. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){
  4820. return -1; //FIXME continue if partitioned and other return -1 too
  4821. }
  4822. assert((cbp&15) == 0 || (cbp&15) == 15);
  4823. if(cbp&15){
  4824. for(i8x8=0; i8x8<4; i8x8++){
  4825. for(i4x4=0; i4x4<4; i4x4++){
  4826. const int index= i4x4 + 4*i8x8;
  4827. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){
  4828. return -1;
  4829. }
  4830. }
  4831. }
  4832. }else{
  4833. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  4834. }
  4835. }else{
  4836. for(i8x8=0; i8x8<4; i8x8++){
  4837. if(cbp & (1<<i8x8)){
  4838. if(IS_8x8DCT(mb_type)){
  4839. DCTELEM *buf = &h->mb[64*i8x8];
  4840. uint8_t *nnz;
  4841. for(i4x4=0; i4x4<4; i4x4++){
  4842. if( decode_residual(h, gb, buf, i4x4+4*i8x8, scan8x8+16*i4x4,
  4843. h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )
  4844. return -1;
  4845. }
  4846. nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4847. nnz[0] += nnz[1] + nnz[8] + nnz[9];
  4848. }else{
  4849. for(i4x4=0; i4x4<4; i4x4++){
  4850. const int index= i4x4 + 4*i8x8;
  4851. if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){
  4852. return -1;
  4853. }
  4854. }
  4855. }
  4856. }else{
  4857. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4858. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  4859. }
  4860. }
  4861. }
  4862. if(cbp&0x30){
  4863. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  4864. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){
  4865. return -1;
  4866. }
  4867. }
  4868. if(cbp&0x20){
  4869. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  4870. for(i4x4=0; i4x4<4; i4x4++){
  4871. const int index= 16 + 4*chroma_idx + i4x4;
  4872. 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){
  4873. return -1;
  4874. }
  4875. }
  4876. }
  4877. }else{
  4878. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4879. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4880. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4881. }
  4882. }else{
  4883. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4884. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  4885. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4886. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4887. }
  4888. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4889. write_back_non_zero_count(h);
  4890. if(MB_MBAFF){
  4891. h->ref_count[0] >>= 1;
  4892. h->ref_count[1] >>= 1;
  4893. }
  4894. return 0;
  4895. }
  4896. static int decode_cabac_field_decoding_flag(H264Context *h) {
  4897. MpegEncContext * const s = &h->s;
  4898. const int mb_x = s->mb_x;
  4899. const int mb_y = s->mb_y & ~1;
  4900. const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
  4901. const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
  4902. unsigned int ctx = 0;
  4903. if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
  4904. ctx += 1;
  4905. }
  4906. if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
  4907. ctx += 1;
  4908. }
  4909. return get_cabac_noinline( &h->cabac, &h->cabac_state[70 + ctx] );
  4910. }
  4911. static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
  4912. uint8_t *state= &h->cabac_state[ctx_base];
  4913. int mb_type;
  4914. if(intra_slice){
  4915. MpegEncContext * const s = &h->s;
  4916. const int mba_xy = h->left_mb_xy[0];
  4917. const int mbb_xy = h->top_mb_xy;
  4918. int ctx=0;
  4919. if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
  4920. ctx++;
  4921. if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
  4922. ctx++;
  4923. if( get_cabac_noinline( &h->cabac, &state[ctx] ) == 0 )
  4924. return 0; /* I4x4 */
  4925. state += 2;
  4926. }else{
  4927. if( get_cabac_noinline( &h->cabac, &state[0] ) == 0 )
  4928. return 0; /* I4x4 */
  4929. }
  4930. if( get_cabac_terminate( &h->cabac ) )
  4931. return 25; /* PCM */
  4932. mb_type = 1; /* I16x16 */
  4933. mb_type += 12 * get_cabac_noinline( &h->cabac, &state[1] ); /* cbp_luma != 0 */
  4934. if( get_cabac_noinline( &h->cabac, &state[2] ) ) /* cbp_chroma */
  4935. mb_type += 4 + 4 * get_cabac_noinline( &h->cabac, &state[2+intra_slice] );
  4936. mb_type += 2 * get_cabac_noinline( &h->cabac, &state[3+intra_slice] );
  4937. mb_type += 1 * get_cabac_noinline( &h->cabac, &state[3+2*intra_slice] );
  4938. return mb_type;
  4939. }
  4940. static int decode_cabac_mb_type( H264Context *h ) {
  4941. MpegEncContext * const s = &h->s;
  4942. if( h->slice_type == I_TYPE ) {
  4943. return decode_cabac_intra_mb_type(h, 3, 1);
  4944. } else if( h->slice_type == P_TYPE ) {
  4945. if( get_cabac_noinline( &h->cabac, &h->cabac_state[14] ) == 0 ) {
  4946. /* P-type */
  4947. if( get_cabac_noinline( &h->cabac, &h->cabac_state[15] ) == 0 ) {
  4948. /* P_L0_D16x16, P_8x8 */
  4949. return 3 * get_cabac_noinline( &h->cabac, &h->cabac_state[16] );
  4950. } else {
  4951. /* P_L0_D8x16, P_L0_D16x8 */
  4952. return 2 - get_cabac_noinline( &h->cabac, &h->cabac_state[17] );
  4953. }
  4954. } else {
  4955. return decode_cabac_intra_mb_type(h, 17, 0) + 5;
  4956. }
  4957. } else if( h->slice_type == B_TYPE ) {
  4958. const int mba_xy = h->left_mb_xy[0];
  4959. const int mbb_xy = h->top_mb_xy;
  4960. int ctx = 0;
  4961. int bits;
  4962. if( h->slice_table[mba_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
  4963. ctx++;
  4964. if( h->slice_table[mbb_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
  4965. ctx++;
  4966. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+ctx] ) )
  4967. return 0; /* B_Direct_16x16 */
  4968. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+3] ) ) {
  4969. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
  4970. }
  4971. bits = get_cabac_noinline( &h->cabac, &h->cabac_state[27+4] ) << 3;
  4972. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 2;
  4973. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 1;
  4974. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  4975. if( bits < 8 )
  4976. return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
  4977. else if( bits == 13 ) {
  4978. return decode_cabac_intra_mb_type(h, 32, 0) + 23;
  4979. } else if( bits == 14 )
  4980. return 11; /* B_L1_L0_8x16 */
  4981. else if( bits == 15 )
  4982. return 22; /* B_8x8 */
  4983. bits= ( bits<<1 ) | get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  4984. return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
  4985. } else {
  4986. /* TODO SI/SP frames? */
  4987. return -1;
  4988. }
  4989. }
  4990. static int decode_cabac_mb_skip( H264Context *h, int mb_x, int mb_y ) {
  4991. MpegEncContext * const s = &h->s;
  4992. int mba_xy, mbb_xy;
  4993. int ctx = 0;
  4994. if(FRAME_MBAFF){ //FIXME merge with the stuff in fill_caches?
  4995. int mb_xy = mb_x + (mb_y&~1)*s->mb_stride;
  4996. mba_xy = mb_xy - 1;
  4997. if( (mb_y&1)
  4998. && h->slice_table[mba_xy] == h->slice_num
  4999. && MB_FIELD == !!IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) )
  5000. mba_xy += s->mb_stride;
  5001. if( MB_FIELD ){
  5002. mbb_xy = mb_xy - s->mb_stride;
  5003. if( !(mb_y&1)
  5004. && h->slice_table[mbb_xy] == h->slice_num
  5005. && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) )
  5006. mbb_xy -= s->mb_stride;
  5007. }else
  5008. mbb_xy = mb_x + (mb_y-1)*s->mb_stride;
  5009. }else{
  5010. int mb_xy = mb_x + mb_y*s->mb_stride;
  5011. mba_xy = mb_xy - 1;
  5012. mbb_xy = mb_xy - s->mb_stride;
  5013. }
  5014. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
  5015. ctx++;
  5016. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
  5017. ctx++;
  5018. if( h->slice_type == B_TYPE )
  5019. ctx += 13;
  5020. return get_cabac_noinline( &h->cabac, &h->cabac_state[11+ctx] );
  5021. }
  5022. static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
  5023. int mode = 0;
  5024. if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
  5025. return pred_mode;
  5026. mode += 1 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5027. mode += 2 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5028. mode += 4 * get_cabac( &h->cabac, &h->cabac_state[69] );
  5029. if( mode >= pred_mode )
  5030. return mode + 1;
  5031. else
  5032. return mode;
  5033. }
  5034. static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
  5035. const int mba_xy = h->left_mb_xy[0];
  5036. const int mbb_xy = h->top_mb_xy;
  5037. int ctx = 0;
  5038. /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
  5039. if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
  5040. ctx++;
  5041. if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
  5042. ctx++;
  5043. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
  5044. return 0;
  5045. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  5046. return 1;
  5047. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  5048. return 2;
  5049. else
  5050. return 3;
  5051. }
  5052. static const uint8_t block_idx_x[16] = {
  5053. 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
  5054. };
  5055. static const uint8_t block_idx_y[16] = {
  5056. 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
  5057. };
  5058. static const uint8_t block_idx_xy[4][4] = {
  5059. { 0, 2, 8, 10},
  5060. { 1, 3, 9, 11},
  5061. { 4, 6, 12, 14},
  5062. { 5, 7, 13, 15}
  5063. };
  5064. static int decode_cabac_mb_cbp_luma( H264Context *h) {
  5065. int cbp = 0;
  5066. int cbp_b = -1;
  5067. int i8x8;
  5068. if( h->slice_table[h->top_mb_xy] == h->slice_num ) {
  5069. cbp_b = h->top_cbp;
  5070. tprintf("cbp_b = top_cbp = %x\n", cbp_b);
  5071. }
  5072. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5073. int cbp_a = -1;
  5074. int x, y;
  5075. int ctx = 0;
  5076. x = block_idx_x[4*i8x8];
  5077. y = block_idx_y[4*i8x8];
  5078. if( x > 0 )
  5079. cbp_a = cbp;
  5080. else if( h->slice_table[h->left_mb_xy[0]] == h->slice_num ) {
  5081. cbp_a = h->left_cbp;
  5082. tprintf("cbp_a = left_cbp = %x\n", cbp_a);
  5083. }
  5084. if( y > 0 )
  5085. cbp_b = cbp;
  5086. /* No need to test for skip as we put 0 for skip block */
  5087. /* No need to test for IPCM as we put 1 for IPCM block */
  5088. if( cbp_a >= 0 ) {
  5089. int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
  5090. if( ((cbp_a >> i8x8a)&0x01) == 0 )
  5091. ctx++;
  5092. }
  5093. if( cbp_b >= 0 ) {
  5094. int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
  5095. if( ((cbp_b >> i8x8b)&0x01) == 0 )
  5096. ctx += 2;
  5097. }
  5098. if( get_cabac( &h->cabac, &h->cabac_state[73 + ctx] ) ) {
  5099. cbp |= 1 << i8x8;
  5100. }
  5101. }
  5102. return cbp;
  5103. }
  5104. static int decode_cabac_mb_cbp_chroma( H264Context *h) {
  5105. int ctx;
  5106. int cbp_a, cbp_b;
  5107. cbp_a = (h->left_cbp>>4)&0x03;
  5108. cbp_b = (h-> top_cbp>>4)&0x03;
  5109. ctx = 0;
  5110. if( cbp_a > 0 ) ctx++;
  5111. if( cbp_b > 0 ) ctx += 2;
  5112. if( get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
  5113. return 0;
  5114. ctx = 4;
  5115. if( cbp_a == 2 ) ctx++;
  5116. if( cbp_b == 2 ) ctx += 2;
  5117. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] );
  5118. }
  5119. static int decode_cabac_mb_dqp( H264Context *h) {
  5120. MpegEncContext * const s = &h->s;
  5121. int mbn_xy;
  5122. int ctx = 0;
  5123. int val = 0;
  5124. if( s->mb_x > 0 )
  5125. mbn_xy = s->mb_x + s->mb_y*s->mb_stride - 1;
  5126. else
  5127. mbn_xy = s->mb_width - 1 + (s->mb_y-1)*s->mb_stride;
  5128. if( h->last_qscale_diff != 0 )
  5129. ctx++;
  5130. while( get_cabac_noinline( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
  5131. if( ctx < 2 )
  5132. ctx = 2;
  5133. else
  5134. ctx = 3;
  5135. val++;
  5136. if(val > 102) //prevent infinite loop
  5137. return INT_MIN;
  5138. }
  5139. if( val&0x01 )
  5140. return (val + 1)/2;
  5141. else
  5142. return -(val + 1)/2;
  5143. }
  5144. static int decode_cabac_p_mb_sub_type( H264Context *h ) {
  5145. if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
  5146. return 0; /* 8x8 */
  5147. if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
  5148. return 1; /* 8x4 */
  5149. if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
  5150. return 2; /* 4x8 */
  5151. return 3; /* 4x4 */
  5152. }
  5153. static int decode_cabac_b_mb_sub_type( H264Context *h ) {
  5154. int type;
  5155. if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
  5156. return 0; /* B_Direct_8x8 */
  5157. if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
  5158. return 1 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */
  5159. type = 3;
  5160. if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
  5161. if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
  5162. return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
  5163. type += 4;
  5164. }
  5165. type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
  5166. type += get_cabac( &h->cabac, &h->cabac_state[39] );
  5167. return type;
  5168. }
  5169. static inline int decode_cabac_mb_transform_size( H264Context *h ) {
  5170. return get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
  5171. }
  5172. static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
  5173. int refa = h->ref_cache[list][scan8[n] - 1];
  5174. int refb = h->ref_cache[list][scan8[n] - 8];
  5175. int ref = 0;
  5176. int ctx = 0;
  5177. if( h->slice_type == B_TYPE) {
  5178. if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
  5179. ctx++;
  5180. if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
  5181. ctx += 2;
  5182. } else {
  5183. if( refa > 0 )
  5184. ctx++;
  5185. if( refb > 0 )
  5186. ctx += 2;
  5187. }
  5188. while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
  5189. ref++;
  5190. if( ctx < 4 )
  5191. ctx = 4;
  5192. else
  5193. ctx = 5;
  5194. }
  5195. return ref;
  5196. }
  5197. static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
  5198. int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
  5199. abs( h->mvd_cache[list][scan8[n] - 8][l] );
  5200. int ctxbase = (l == 0) ? 40 : 47;
  5201. int ctx, mvd;
  5202. if( amvd < 3 )
  5203. ctx = 0;
  5204. else if( amvd > 32 )
  5205. ctx = 2;
  5206. else
  5207. ctx = 1;
  5208. if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
  5209. return 0;
  5210. mvd= 1;
  5211. ctx= 3;
  5212. while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
  5213. mvd++;
  5214. if( ctx < 6 )
  5215. ctx++;
  5216. }
  5217. if( mvd >= 9 ) {
  5218. int k = 3;
  5219. while( get_cabac_bypass( &h->cabac ) ) {
  5220. mvd += 1 << k;
  5221. k++;
  5222. }
  5223. while( k-- ) {
  5224. if( get_cabac_bypass( &h->cabac ) )
  5225. mvd += 1 << k;
  5226. }
  5227. }
  5228. return get_cabac_bypass_sign( &h->cabac, -mvd );
  5229. }
  5230. static int inline get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) {
  5231. int nza, nzb;
  5232. int ctx = 0;
  5233. if( cat == 0 ) {
  5234. nza = h->left_cbp&0x100;
  5235. nzb = h-> top_cbp&0x100;
  5236. } else if( cat == 1 || cat == 2 ) {
  5237. nza = h->non_zero_count_cache[scan8[idx] - 1];
  5238. nzb = h->non_zero_count_cache[scan8[idx] - 8];
  5239. } else if( cat == 3 ) {
  5240. nza = (h->left_cbp>>(6+idx))&0x01;
  5241. nzb = (h-> top_cbp>>(6+idx))&0x01;
  5242. } else {
  5243. assert(cat == 4);
  5244. nza = h->non_zero_count_cache[scan8[16+idx] - 1];
  5245. nzb = h->non_zero_count_cache[scan8[16+idx] - 8];
  5246. }
  5247. if( nza > 0 )
  5248. ctx++;
  5249. if( nzb > 0 )
  5250. ctx += 2;
  5251. return ctx + 4 * cat;
  5252. }
  5253. static const __attribute((used)) uint8_t last_coeff_flag_offset_8x8[63] = {
  5254. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5255. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  5256. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  5257. 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
  5258. };
  5259. static int decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff) {
  5260. const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
  5261. static const int significant_coeff_flag_offset[2][6] = {
  5262. { 105+0, 105+15, 105+29, 105+44, 105+47, 402 },
  5263. { 277+0, 277+15, 277+29, 277+44, 277+47, 436 }
  5264. };
  5265. static const int last_coeff_flag_offset[2][6] = {
  5266. { 166+0, 166+15, 166+29, 166+44, 166+47, 417 },
  5267. { 338+0, 338+15, 338+29, 338+44, 338+47, 451 }
  5268. };
  5269. static const int coeff_abs_level_m1_offset[6] = {
  5270. 227+0, 227+10, 227+20, 227+30, 227+39, 426
  5271. };
  5272. static const uint8_t significant_coeff_flag_offset_8x8[2][63] = {
  5273. { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
  5274. 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
  5275. 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
  5276. 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12 },
  5277. { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
  5278. 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
  5279. 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
  5280. 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }
  5281. };
  5282. int index[64];
  5283. int last;
  5284. int coeff_count = 0;
  5285. int abslevel1 = 1;
  5286. int abslevelgt1 = 0;
  5287. uint8_t *significant_coeff_ctx_base;
  5288. uint8_t *last_coeff_ctx_base;
  5289. uint8_t *abs_level_m1_ctx_base;
  5290. #ifndef ARCH_X86
  5291. #define CABAC_ON_STACK
  5292. #endif
  5293. #ifdef CABAC_ON_STACK
  5294. #define CC &cc
  5295. CABACContext cc;
  5296. cc.range = h->cabac.range;
  5297. cc.low = h->cabac.low;
  5298. cc.bytestream= h->cabac.bytestream;
  5299. #else
  5300. #define CC &h->cabac
  5301. #endif
  5302. /* cat: 0-> DC 16x16 n = 0
  5303. * 1-> AC 16x16 n = luma4x4idx
  5304. * 2-> Luma4x4 n = luma4x4idx
  5305. * 3-> DC Chroma n = iCbCr
  5306. * 4-> AC Chroma n = 4 * iCbCr + chroma4x4idx
  5307. * 5-> Luma8x8 n = 4 * luma8x8idx
  5308. */
  5309. /* read coded block flag */
  5310. if( cat != 5 ) {
  5311. if( get_cabac( CC, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n ) ] ) == 0 ) {
  5312. if( cat == 1 || cat == 2 )
  5313. h->non_zero_count_cache[scan8[n]] = 0;
  5314. else if( cat == 4 )
  5315. h->non_zero_count_cache[scan8[16+n]] = 0;
  5316. #ifdef CABAC_ON_STACK
  5317. h->cabac.range = cc.range ;
  5318. h->cabac.low = cc.low ;
  5319. h->cabac.bytestream= cc.bytestream;
  5320. #endif
  5321. return 0;
  5322. }
  5323. }
  5324. significant_coeff_ctx_base = h->cabac_state
  5325. + significant_coeff_flag_offset[MB_FIELD][cat];
  5326. last_coeff_ctx_base = h->cabac_state
  5327. + last_coeff_flag_offset[MB_FIELD][cat];
  5328. abs_level_m1_ctx_base = h->cabac_state
  5329. + coeff_abs_level_m1_offset[cat];
  5330. if( cat == 5 ) {
  5331. #define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \
  5332. for(last= 0; last < coefs; last++) { \
  5333. uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \
  5334. if( get_cabac( CC, sig_ctx )) { \
  5335. uint8_t *last_ctx = last_coeff_ctx_base + last_off; \
  5336. index[coeff_count++] = last; \
  5337. if( get_cabac( CC, last_ctx ) ) { \
  5338. last= max_coeff; \
  5339. break; \
  5340. } \
  5341. } \
  5342. }\
  5343. if( last == max_coeff -1 ) {\
  5344. index[coeff_count++] = last;\
  5345. }
  5346. const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
  5347. #if defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__))
  5348. coeff_count= decode_significance_8x8_x86(CC, significant_coeff_ctx_base, index, sig_off);
  5349. } else {
  5350. coeff_count= decode_significance_x86(CC, max_coeff, significant_coeff_ctx_base, index);
  5351. #else
  5352. DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );
  5353. } else {
  5354. DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
  5355. #endif
  5356. }
  5357. assert(coeff_count > 0);
  5358. if( cat == 0 )
  5359. h->cbp_table[mb_xy] |= 0x100;
  5360. else if( cat == 1 || cat == 2 )
  5361. h->non_zero_count_cache[scan8[n]] = coeff_count;
  5362. else if( cat == 3 )
  5363. h->cbp_table[mb_xy] |= 0x40 << n;
  5364. else if( cat == 4 )
  5365. h->non_zero_count_cache[scan8[16+n]] = coeff_count;
  5366. else {
  5367. assert( cat == 5 );
  5368. fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);
  5369. }
  5370. for( coeff_count--; coeff_count >= 0; coeff_count-- ) {
  5371. uint8_t *ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 )) + abs_level_m1_ctx_base;
  5372. int j= scantable[index[coeff_count]];
  5373. if( get_cabac( CC, ctx ) == 0 ) {
  5374. if( !qmul ) {
  5375. block[j] = get_cabac_bypass_sign( CC, -1);
  5376. }else{
  5377. block[j] = (get_cabac_bypass_sign( CC, -qmul[j]) + 32) >> 6;;
  5378. }
  5379. abslevel1++;
  5380. } else {
  5381. int coeff_abs = 2;
  5382. ctx = 5 + FFMIN( 4, abslevelgt1 ) + abs_level_m1_ctx_base;
  5383. while( coeff_abs < 15 && get_cabac( CC, ctx ) ) {
  5384. coeff_abs++;
  5385. }
  5386. if( coeff_abs >= 15 ) {
  5387. int j = 0;
  5388. while( get_cabac_bypass( CC ) ) {
  5389. j++;
  5390. }
  5391. coeff_abs=1;
  5392. while( j-- ) {
  5393. coeff_abs += coeff_abs + get_cabac_bypass( CC );
  5394. }
  5395. coeff_abs+= 14;
  5396. }
  5397. if( !qmul ) {
  5398. if( get_cabac_bypass( CC ) ) block[j] = -coeff_abs;
  5399. else block[j] = coeff_abs;
  5400. }else{
  5401. if( get_cabac_bypass( CC ) ) block[j] = (-coeff_abs * qmul[j] + 32) >> 6;
  5402. else block[j] = ( coeff_abs * qmul[j] + 32) >> 6;
  5403. }
  5404. abslevelgt1++;
  5405. }
  5406. }
  5407. #ifdef CABAC_ON_STACK
  5408. h->cabac.range = cc.range ;
  5409. h->cabac.low = cc.low ;
  5410. h->cabac.bytestream= cc.bytestream;
  5411. #endif
  5412. return 0;
  5413. }
  5414. static void inline compute_mb_neighbors(H264Context *h)
  5415. {
  5416. MpegEncContext * const s = &h->s;
  5417. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  5418. h->top_mb_xy = mb_xy - s->mb_stride;
  5419. h->left_mb_xy[0] = mb_xy - 1;
  5420. if(FRAME_MBAFF){
  5421. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  5422. const int top_pair_xy = pair_xy - s->mb_stride;
  5423. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  5424. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  5425. const int curr_mb_frame_flag = !MB_FIELD;
  5426. const int bottom = (s->mb_y & 1);
  5427. if (bottom
  5428. ? !curr_mb_frame_flag // bottom macroblock
  5429. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  5430. ) {
  5431. h->top_mb_xy -= s->mb_stride;
  5432. }
  5433. if (left_mb_frame_flag != curr_mb_frame_flag) {
  5434. h->left_mb_xy[0] = pair_xy - 1;
  5435. }
  5436. }
  5437. return;
  5438. }
  5439. /**
  5440. * decodes a macroblock
  5441. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  5442. */
  5443. static int decode_mb_cabac(H264Context *h) {
  5444. MpegEncContext * const s = &h->s;
  5445. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  5446. int mb_type, partition_count, cbp = 0;
  5447. int dct8x8_allowed= h->pps.transform_8x8_mode;
  5448. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?)
  5449. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  5450. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) {
  5451. int skip;
  5452. /* a skipped mb needs the aff flag from the following mb */
  5453. if( FRAME_MBAFF && s->mb_x==0 && (s->mb_y&1)==0 )
  5454. predict_field_decoding_flag(h);
  5455. if( FRAME_MBAFF && (s->mb_y&1)==1 && h->prev_mb_skipped )
  5456. skip = h->next_mb_skipped;
  5457. else
  5458. skip = decode_cabac_mb_skip( h, s->mb_x, s->mb_y );
  5459. /* read skip flags */
  5460. if( skip ) {
  5461. if( FRAME_MBAFF && (s->mb_y&1)==0 ){
  5462. s->current_picture.mb_type[mb_xy] = MB_TYPE_SKIP;
  5463. h->next_mb_skipped = decode_cabac_mb_skip( h, s->mb_x, s->mb_y+1 );
  5464. if(h->next_mb_skipped)
  5465. predict_field_decoding_flag(h);
  5466. else
  5467. h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5468. }
  5469. decode_mb_skip(h);
  5470. h->cbp_table[mb_xy] = 0;
  5471. h->chroma_pred_mode_table[mb_xy] = 0;
  5472. h->last_qscale_diff = 0;
  5473. return 0;
  5474. }
  5475. }
  5476. if(FRAME_MBAFF){
  5477. if( (s->mb_y&1) == 0 )
  5478. h->mb_mbaff =
  5479. h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5480. }else
  5481. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  5482. h->prev_mb_skipped = 0;
  5483. compute_mb_neighbors(h);
  5484. if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
  5485. av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
  5486. return -1;
  5487. }
  5488. if( h->slice_type == B_TYPE ) {
  5489. if( mb_type < 23 ){
  5490. partition_count= b_mb_type_info[mb_type].partition_count;
  5491. mb_type= b_mb_type_info[mb_type].type;
  5492. }else{
  5493. mb_type -= 23;
  5494. goto decode_intra_mb;
  5495. }
  5496. } else if( h->slice_type == P_TYPE ) {
  5497. if( mb_type < 5) {
  5498. partition_count= p_mb_type_info[mb_type].partition_count;
  5499. mb_type= p_mb_type_info[mb_type].type;
  5500. } else {
  5501. mb_type -= 5;
  5502. goto decode_intra_mb;
  5503. }
  5504. } else {
  5505. assert(h->slice_type == I_TYPE);
  5506. decode_intra_mb:
  5507. partition_count = 0;
  5508. cbp= i_mb_type_info[mb_type].cbp;
  5509. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  5510. mb_type= i_mb_type_info[mb_type].type;
  5511. }
  5512. if(MB_FIELD)
  5513. mb_type |= MB_TYPE_INTERLACED;
  5514. h->slice_table[ mb_xy ]= h->slice_num;
  5515. if(IS_INTRA_PCM(mb_type)) {
  5516. const uint8_t *ptr;
  5517. unsigned int x, y;
  5518. // We assume these blocks are very rare so we dont optimize it.
  5519. // FIXME The two following lines get the bitstream position in the cabac
  5520. // decode, I think it should be done by a function in cabac.h (or cabac.c).
  5521. ptr= h->cabac.bytestream;
  5522. if (h->cabac.low&0x1) ptr-=CABAC_BITS/8;
  5523. // The pixels are stored in the same order as levels in h->mb array.
  5524. for(y=0; y<16; y++){
  5525. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  5526. for(x=0; x<16; x++){
  5527. tprintf("LUMA ICPM LEVEL (%3d)\n", *ptr);
  5528. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= *ptr++;
  5529. }
  5530. }
  5531. for(y=0; y<8; y++){
  5532. const int index= 256 + 4*(y&3) + 32*(y>>2);
  5533. for(x=0; x<8; x++){
  5534. tprintf("CHROMA U ICPM LEVEL (%3d)\n", *ptr);
  5535. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5536. }
  5537. }
  5538. for(y=0; y<8; y++){
  5539. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  5540. for(x=0; x<8; x++){
  5541. tprintf("CHROMA V ICPM LEVEL (%3d)\n", *ptr);
  5542. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5543. }
  5544. }
  5545. ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);
  5546. // All blocks are present
  5547. h->cbp_table[mb_xy] = 0x1ef;
  5548. h->chroma_pred_mode_table[mb_xy] = 0;
  5549. // In deblocking, the quantizer is 0
  5550. s->current_picture.qscale_table[mb_xy]= 0;
  5551. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  5552. // All coeffs are present
  5553. memset(h->non_zero_count[mb_xy], 16, 16);
  5554. s->current_picture.mb_type[mb_xy]= mb_type;
  5555. return 0;
  5556. }
  5557. if(MB_MBAFF){
  5558. h->ref_count[0] <<= 1;
  5559. h->ref_count[1] <<= 1;
  5560. }
  5561. fill_caches(h, mb_type, 0);
  5562. if( IS_INTRA( mb_type ) ) {
  5563. int i;
  5564. if( IS_INTRA4x4( mb_type ) ) {
  5565. if( dct8x8_allowed && decode_cabac_mb_transform_size( h ) ) {
  5566. mb_type |= MB_TYPE_8x8DCT;
  5567. for( i = 0; i < 16; i+=4 ) {
  5568. int pred = pred_intra_mode( h, i );
  5569. int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5570. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  5571. }
  5572. } else {
  5573. for( i = 0; i < 16; i++ ) {
  5574. int pred = pred_intra_mode( h, i );
  5575. h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5576. //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
  5577. }
  5578. }
  5579. write_back_intra_pred_mode(h);
  5580. if( check_intra4x4_pred_mode(h) < 0 ) return -1;
  5581. } else {
  5582. h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );
  5583. if( h->intra16x16_pred_mode < 0 ) return -1;
  5584. }
  5585. h->chroma_pred_mode_table[mb_xy] =
  5586. h->chroma_pred_mode = decode_cabac_mb_chroma_pre_mode( h );
  5587. h->chroma_pred_mode= check_intra_pred_mode( h, h->chroma_pred_mode );
  5588. if( h->chroma_pred_mode < 0 ) return -1;
  5589. } else if( partition_count == 4 ) {
  5590. int i, j, sub_partition_count[4], list, ref[2][4];
  5591. if( h->slice_type == B_TYPE ) {
  5592. for( i = 0; i < 4; i++ ) {
  5593. h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
  5594. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5595. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5596. }
  5597. if( IS_DIRECT(h->sub_mb_type[0] | h->sub_mb_type[1] |
  5598. h->sub_mb_type[2] | h->sub_mb_type[3]) ) {
  5599. pred_direct_motion(h, &mb_type);
  5600. if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
  5601. for( i = 0; i < 4; i++ )
  5602. if( IS_DIRECT(h->sub_mb_type[i]) )
  5603. fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );
  5604. }
  5605. }
  5606. } else {
  5607. for( i = 0; i < 4; i++ ) {
  5608. h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
  5609. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5610. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5611. }
  5612. }
  5613. for( list = 0; list < 2; list++ ) {
  5614. if( h->ref_count[list] > 0 ) {
  5615. for( i = 0; i < 4; i++ ) {
  5616. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  5617. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  5618. if( h->ref_count[list] > 1 )
  5619. ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );
  5620. else
  5621. ref[list][i] = 0;
  5622. } else {
  5623. ref[list][i] = -1;
  5624. }
  5625. h->ref_cache[list][ scan8[4*i]+1 ]=
  5626. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  5627. }
  5628. }
  5629. }
  5630. if(dct8x8_allowed)
  5631. dct8x8_allowed = get_dct8x8_allowed(h);
  5632. for(list=0; list<2; list++){
  5633. for(i=0; i<4; i++){
  5634. if(IS_DIRECT(h->sub_mb_type[i])){
  5635. fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);
  5636. continue;
  5637. }
  5638. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];
  5639. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  5640. const int sub_mb_type= h->sub_mb_type[i];
  5641. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  5642. for(j=0; j<sub_partition_count[i]; j++){
  5643. int mpx, mpy;
  5644. int mx, my;
  5645. const int index= 4*i + block_width*j;
  5646. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  5647. int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];
  5648. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);
  5649. mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );
  5650. my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );
  5651. tprintf("final mv:%d %d\n", mx, my);
  5652. if(IS_SUB_8X8(sub_mb_type)){
  5653. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  5654. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  5655. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  5656. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  5657. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]=
  5658. mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;
  5659. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]=
  5660. mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;
  5661. }else if(IS_SUB_8X4(sub_mb_type)){
  5662. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  5663. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  5664. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]= mx- mpx;
  5665. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]= my - mpy;
  5666. }else if(IS_SUB_4X8(sub_mb_type)){
  5667. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  5668. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  5669. mvd_cache[ 0 ][0]= mvd_cache[ 8 ][0]= mx - mpx;
  5670. mvd_cache[ 0 ][1]= mvd_cache[ 8 ][1]= my - mpy;
  5671. }else{
  5672. assert(IS_SUB_4X4(sub_mb_type));
  5673. mv_cache[ 0 ][0]= mx;
  5674. mv_cache[ 0 ][1]= my;
  5675. mvd_cache[ 0 ][0]= mx - mpx;
  5676. mvd_cache[ 0 ][1]= my - mpy;
  5677. }
  5678. }
  5679. }else{
  5680. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  5681. uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];
  5682. p[0] = p[1] = p[8] = p[9] = 0;
  5683. pd[0]= pd[1]= pd[8]= pd[9]= 0;
  5684. }
  5685. }
  5686. }
  5687. } else if( IS_DIRECT(mb_type) ) {
  5688. pred_direct_motion(h, &mb_type);
  5689. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  5690. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  5691. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  5692. } else {
  5693. int list, mx, my, i, mpx, mpy;
  5694. if(IS_16X16(mb_type)){
  5695. for(list=0; list<2; list++){
  5696. if(IS_DIR(mb_type, 0, list)){
  5697. if(h->ref_count[list] > 0 ){
  5698. const int ref = h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 0 ) : 0;
  5699. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
  5700. }
  5701. }else
  5702. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  5703. }
  5704. for(list=0; list<2; list++){
  5705. if(IS_DIR(mb_type, 0, list)){
  5706. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);
  5707. mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );
  5708. my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );
  5709. tprintf("final mv:%d %d\n", mx, my);
  5710. fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5711. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  5712. }else
  5713. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  5714. }
  5715. }
  5716. else if(IS_16X8(mb_type)){
  5717. for(list=0; list<2; list++){
  5718. if(h->ref_count[list]>0){
  5719. for(i=0; i<2; i++){
  5720. if(IS_DIR(mb_type, i, list)){
  5721. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 8*i ) : 0;
  5722. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
  5723. }else
  5724. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  5725. }
  5726. }
  5727. }
  5728. for(list=0; list<2; list++){
  5729. for(i=0; i<2; i++){
  5730. if(IS_DIR(mb_type, i, list)){
  5731. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);
  5732. mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );
  5733. my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );
  5734. tprintf("final mv:%d %d\n", mx, my);
  5735. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);
  5736. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  5737. }else{
  5738. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5739. fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5740. }
  5741. }
  5742. }
  5743. }else{
  5744. assert(IS_8X16(mb_type));
  5745. for(list=0; list<2; list++){
  5746. if(h->ref_count[list]>0){
  5747. for(i=0; i<2; i++){
  5748. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  5749. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 4*i ) : 0;
  5750. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
  5751. }else
  5752. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  5753. }
  5754. }
  5755. }
  5756. for(list=0; list<2; list++){
  5757. for(i=0; i<2; i++){
  5758. if(IS_DIR(mb_type, i, list)){
  5759. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);
  5760. mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );
  5761. my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );
  5762. tprintf("final mv:%d %d\n", mx, my);
  5763. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5764. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  5765. }else{
  5766. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5767. fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5768. }
  5769. }
  5770. }
  5771. }
  5772. }
  5773. if( IS_INTER( mb_type ) ) {
  5774. h->chroma_pred_mode_table[mb_xy] = 0;
  5775. write_back_motion( h, mb_type );
  5776. }
  5777. if( !IS_INTRA16x16( mb_type ) ) {
  5778. cbp = decode_cabac_mb_cbp_luma( h );
  5779. cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
  5780. }
  5781. h->cbp_table[mb_xy] = h->cbp = cbp;
  5782. if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
  5783. if( decode_cabac_mb_transform_size( h ) )
  5784. mb_type |= MB_TYPE_8x8DCT;
  5785. }
  5786. s->current_picture.mb_type[mb_xy]= mb_type;
  5787. if( cbp || IS_INTRA16x16( mb_type ) ) {
  5788. const uint8_t *scan, *scan8x8, *dc_scan;
  5789. int dqp;
  5790. if(IS_INTERLACED(mb_type)){
  5791. scan8x8= s->qscale ? h->field_scan8x8 : h->field_scan8x8_q0;
  5792. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  5793. dc_scan= luma_dc_field_scan;
  5794. }else{
  5795. scan8x8= s->qscale ? h->zigzag_scan8x8 : h->zigzag_scan8x8_q0;
  5796. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  5797. dc_scan= luma_dc_zigzag_scan;
  5798. }
  5799. h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
  5800. if( dqp == INT_MIN ){
  5801. av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", s->mb_x, s->mb_y);
  5802. return -1;
  5803. }
  5804. s->qscale += dqp;
  5805. if(((unsigned)s->qscale) > 51){
  5806. if(s->qscale<0) s->qscale+= 52;
  5807. else s->qscale-= 52;
  5808. }
  5809. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  5810. if( IS_INTRA16x16( mb_type ) ) {
  5811. int i;
  5812. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
  5813. if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16) < 0)
  5814. return -1;
  5815. if( cbp&15 ) {
  5816. for( i = 0; i < 16; i++ ) {
  5817. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
  5818. if( decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 )
  5819. return -1;
  5820. }
  5821. } else {
  5822. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  5823. }
  5824. } else {
  5825. int i8x8, i4x4;
  5826. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5827. if( cbp & (1<<i8x8) ) {
  5828. if( IS_8x8DCT(mb_type) ) {
  5829. if( decode_cabac_residual(h, h->mb + 64*i8x8, 5, 4*i8x8,
  5830. scan8x8, h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 64) < 0 )
  5831. return -1;
  5832. } else
  5833. for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
  5834. const int index = 4*i8x8 + i4x4;
  5835. //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
  5836. //START_TIMER
  5837. 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 )
  5838. return -1;
  5839. //STOP_TIMER("decode_residual")
  5840. }
  5841. } else {
  5842. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  5843. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  5844. }
  5845. }
  5846. }
  5847. if( cbp&0x30 ){
  5848. int c;
  5849. for( c = 0; c < 2; c++ ) {
  5850. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
  5851. if( decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, NULL, 4) < 0)
  5852. return -1;
  5853. }
  5854. }
  5855. if( cbp&0x20 ) {
  5856. int c, i;
  5857. for( c = 0; c < 2; c++ ) {
  5858. for( i = 0; i < 4; i++ ) {
  5859. const int index = 16 + 4 * c + i;
  5860. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
  5861. 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)
  5862. return -1;
  5863. }
  5864. }
  5865. } else {
  5866. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5867. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5868. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5869. }
  5870. } else {
  5871. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5872. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  5873. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5874. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5875. h->last_qscale_diff = 0;
  5876. }
  5877. s->current_picture.qscale_table[mb_xy]= s->qscale;
  5878. write_back_non_zero_count(h);
  5879. if(MB_MBAFF){
  5880. h->ref_count[0] >>= 1;
  5881. h->ref_count[1] >>= 1;
  5882. }
  5883. return 0;
  5884. }
  5885. static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5886. int i, d;
  5887. const int index_a = qp + h->slice_alpha_c0_offset;
  5888. const int alpha = (alpha_table+52)[index_a];
  5889. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5890. if( bS[0] < 4 ) {
  5891. int8_t tc[4];
  5892. for(i=0; i<4; i++)
  5893. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  5894. h->s.dsp.h264_h_loop_filter_luma(pix, stride, alpha, beta, tc);
  5895. } else {
  5896. /* 16px edge length, because bS=4 is triggered by being at
  5897. * the edge of an intra MB, so all 4 bS are the same */
  5898. for( d = 0; d < 16; d++ ) {
  5899. const int p0 = pix[-1];
  5900. const int p1 = pix[-2];
  5901. const int p2 = pix[-3];
  5902. const int q0 = pix[0];
  5903. const int q1 = pix[1];
  5904. const int q2 = pix[2];
  5905. if( FFABS( p0 - q0 ) < alpha &&
  5906. FFABS( p1 - p0 ) < beta &&
  5907. FFABS( q1 - q0 ) < beta ) {
  5908. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5909. if( FFABS( p2 - p0 ) < beta)
  5910. {
  5911. const int p3 = pix[-4];
  5912. /* p0', p1', p2' */
  5913. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5914. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5915. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5916. } else {
  5917. /* p0' */
  5918. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5919. }
  5920. if( FFABS( q2 - q0 ) < beta)
  5921. {
  5922. const int q3 = pix[3];
  5923. /* q0', q1', q2' */
  5924. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5925. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5926. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5927. } else {
  5928. /* q0' */
  5929. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5930. }
  5931. }else{
  5932. /* p0', q0' */
  5933. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5934. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5935. }
  5936. 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]);
  5937. }
  5938. pix += stride;
  5939. }
  5940. }
  5941. }
  5942. static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5943. int i;
  5944. const int index_a = qp + h->slice_alpha_c0_offset;
  5945. const int alpha = (alpha_table+52)[index_a];
  5946. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5947. if( bS[0] < 4 ) {
  5948. int8_t tc[4];
  5949. for(i=0; i<4; i++)
  5950. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  5951. h->s.dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
  5952. } else {
  5953. h->s.dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
  5954. }
  5955. }
  5956. static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  5957. int i;
  5958. for( i = 0; i < 16; i++, pix += stride) {
  5959. int index_a;
  5960. int alpha;
  5961. int beta;
  5962. int qp_index;
  5963. int bS_index = (i >> 1);
  5964. if (!MB_FIELD) {
  5965. bS_index &= ~1;
  5966. bS_index |= (i & 1);
  5967. }
  5968. if( bS[bS_index] == 0 ) {
  5969. continue;
  5970. }
  5971. qp_index = MB_FIELD ? (i >> 3) : (i & 1);
  5972. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  5973. alpha = (alpha_table+52)[index_a];
  5974. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  5975. if( bS[bS_index] < 4 ) {
  5976. const int tc0 = (tc0_table+52)[index_a][bS[bS_index] - 1];
  5977. const int p0 = pix[-1];
  5978. const int p1 = pix[-2];
  5979. const int p2 = pix[-3];
  5980. const int q0 = pix[0];
  5981. const int q1 = pix[1];
  5982. const int q2 = pix[2];
  5983. if( FFABS( p0 - q0 ) < alpha &&
  5984. FFABS( p1 - p0 ) < beta &&
  5985. FFABS( q1 - q0 ) < beta ) {
  5986. int tc = tc0;
  5987. int i_delta;
  5988. if( FFABS( p2 - p0 ) < beta ) {
  5989. pix[-2] = p1 + clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  5990. tc++;
  5991. }
  5992. if( FFABS( q2 - q0 ) < beta ) {
  5993. pix[1] = q1 + clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  5994. tc++;
  5995. }
  5996. i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  5997. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  5998. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  5999. 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);
  6000. }
  6001. }else{
  6002. const int p0 = pix[-1];
  6003. const int p1 = pix[-2];
  6004. const int p2 = pix[-3];
  6005. const int q0 = pix[0];
  6006. const int q1 = pix[1];
  6007. const int q2 = pix[2];
  6008. if( FFABS( p0 - q0 ) < alpha &&
  6009. FFABS( p1 - p0 ) < beta &&
  6010. FFABS( q1 - q0 ) < beta ) {
  6011. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  6012. if( FFABS( p2 - p0 ) < beta)
  6013. {
  6014. const int p3 = pix[-4];
  6015. /* p0', p1', p2' */
  6016. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  6017. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  6018. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  6019. } else {
  6020. /* p0' */
  6021. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6022. }
  6023. if( FFABS( q2 - q0 ) < beta)
  6024. {
  6025. const int q3 = pix[3];
  6026. /* q0', q1', q2' */
  6027. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  6028. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  6029. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  6030. } else {
  6031. /* q0' */
  6032. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6033. }
  6034. }else{
  6035. /* p0', q0' */
  6036. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6037. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6038. }
  6039. 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]);
  6040. }
  6041. }
  6042. }
  6043. }
  6044. static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  6045. int i;
  6046. for( i = 0; i < 8; i++, pix += stride) {
  6047. int index_a;
  6048. int alpha;
  6049. int beta;
  6050. int qp_index;
  6051. int bS_index = i;
  6052. if( bS[bS_index] == 0 ) {
  6053. continue;
  6054. }
  6055. qp_index = MB_FIELD ? (i >> 2) : (i & 1);
  6056. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  6057. alpha = (alpha_table+52)[index_a];
  6058. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  6059. if( bS[bS_index] < 4 ) {
  6060. const int tc = (tc0_table+52)[index_a][bS[bS_index] - 1] + 1;
  6061. const int p0 = pix[-1];
  6062. const int p1 = pix[-2];
  6063. const int q0 = pix[0];
  6064. const int q1 = pix[1];
  6065. if( FFABS( p0 - q0 ) < alpha &&
  6066. FFABS( p1 - p0 ) < beta &&
  6067. FFABS( q1 - q0 ) < beta ) {
  6068. const int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  6069. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  6070. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  6071. 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);
  6072. }
  6073. }else{
  6074. const int p0 = pix[-1];
  6075. const int p1 = pix[-2];
  6076. const int q0 = pix[0];
  6077. const int q1 = pix[1];
  6078. if( FFABS( p0 - q0 ) < alpha &&
  6079. FFABS( p1 - p0 ) < beta &&
  6080. FFABS( q1 - q0 ) < beta ) {
  6081. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  6082. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  6083. 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]);
  6084. }
  6085. }
  6086. }
  6087. }
  6088. static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  6089. int i, d;
  6090. const int index_a = qp + h->slice_alpha_c0_offset;
  6091. const int alpha = (alpha_table+52)[index_a];
  6092. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  6093. const int pix_next = stride;
  6094. if( bS[0] < 4 ) {
  6095. int8_t tc[4];
  6096. for(i=0; i<4; i++)
  6097. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  6098. h->s.dsp.h264_v_loop_filter_luma(pix, stride, alpha, beta, tc);
  6099. } else {
  6100. /* 16px edge length, see filter_mb_edgev */
  6101. for( d = 0; d < 16; d++ ) {
  6102. const int p0 = pix[-1*pix_next];
  6103. const int p1 = pix[-2*pix_next];
  6104. const int p2 = pix[-3*pix_next];
  6105. const int q0 = pix[0];
  6106. const int q1 = pix[1*pix_next];
  6107. const int q2 = pix[2*pix_next];
  6108. if( FFABS( p0 - q0 ) < alpha &&
  6109. FFABS( p1 - p0 ) < beta &&
  6110. FFABS( q1 - q0 ) < beta ) {
  6111. const int p3 = pix[-4*pix_next];
  6112. const int q3 = pix[ 3*pix_next];
  6113. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  6114. if( FFABS( p2 - p0 ) < beta) {
  6115. /* p0', p1', p2' */
  6116. pix[-1*pix_next] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  6117. pix[-2*pix_next] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  6118. pix[-3*pix_next] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  6119. } else {
  6120. /* p0' */
  6121. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6122. }
  6123. if( FFABS( q2 - q0 ) < beta) {
  6124. /* q0', q1', q2' */
  6125. pix[0*pix_next] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  6126. pix[1*pix_next] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  6127. pix[2*pix_next] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  6128. } else {
  6129. /* q0' */
  6130. pix[0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6131. }
  6132. }else{
  6133. /* p0', q0' */
  6134. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  6135. pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  6136. }
  6137. 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]);
  6138. }
  6139. pix++;
  6140. }
  6141. }
  6142. }
  6143. static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  6144. int i;
  6145. const int index_a = qp + h->slice_alpha_c0_offset;
  6146. const int alpha = (alpha_table+52)[index_a];
  6147. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  6148. if( bS[0] < 4 ) {
  6149. int8_t tc[4];
  6150. for(i=0; i<4; i++)
  6151. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  6152. h->s.dsp.h264_v_loop_filter_chroma(pix, stride, alpha, beta, tc);
  6153. } else {
  6154. h->s.dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);
  6155. }
  6156. }
  6157. 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) {
  6158. MpegEncContext * const s = &h->s;
  6159. int mb_xy, mb_type;
  6160. int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh;
  6161. if(mb_x==0 || mb_y==0 || !s->dsp.h264_loop_filter_strength) {
  6162. filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);
  6163. return;
  6164. }
  6165. assert(!FRAME_MBAFF);
  6166. mb_xy = mb_x + mb_y*s->mb_stride;
  6167. mb_type = s->current_picture.mb_type[mb_xy];
  6168. qp = s->current_picture.qscale_table[mb_xy];
  6169. qp0 = s->current_picture.qscale_table[mb_xy-1];
  6170. qp1 = s->current_picture.qscale_table[h->top_mb_xy];
  6171. qpc = get_chroma_qp( h->pps.chroma_qp_index_offset, qp );
  6172. qpc0 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp0 );
  6173. qpc1 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp1 );
  6174. qp0 = (qp + qp0 + 1) >> 1;
  6175. qp1 = (qp + qp1 + 1) >> 1;
  6176. qpc0 = (qpc + qpc0 + 1) >> 1;
  6177. qpc1 = (qpc + qpc1 + 1) >> 1;
  6178. qp_thresh = 15 - h->slice_alpha_c0_offset;
  6179. if(qp <= qp_thresh && qp0 <= qp_thresh && qp1 <= qp_thresh &&
  6180. qpc <= qp_thresh && qpc0 <= qp_thresh && qpc1 <= qp_thresh)
  6181. return;
  6182. if( IS_INTRA(mb_type) ) {
  6183. int16_t bS4[4] = {4,4,4,4};
  6184. int16_t bS3[4] = {3,3,3,3};
  6185. if( IS_8x8DCT(mb_type) ) {
  6186. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  6187. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  6188. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  6189. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  6190. } else {
  6191. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  6192. filter_mb_edgev( h, &img_y[4*1], linesize, bS3, qp );
  6193. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  6194. filter_mb_edgev( h, &img_y[4*3], linesize, bS3, qp );
  6195. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  6196. filter_mb_edgeh( h, &img_y[4*1*linesize], linesize, bS3, qp );
  6197. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  6198. filter_mb_edgeh( h, &img_y[4*3*linesize], linesize, bS3, qp );
  6199. }
  6200. filter_mb_edgecv( h, &img_cb[2*0], uvlinesize, bS4, qpc0 );
  6201. filter_mb_edgecv( h, &img_cb[2*2], uvlinesize, bS3, qpc );
  6202. filter_mb_edgecv( h, &img_cr[2*0], uvlinesize, bS4, qpc0 );
  6203. filter_mb_edgecv( h, &img_cr[2*2], uvlinesize, bS3, qpc );
  6204. filter_mb_edgech( h, &img_cb[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  6205. filter_mb_edgech( h, &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc );
  6206. filter_mb_edgech( h, &img_cr[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  6207. filter_mb_edgech( h, &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc );
  6208. return;
  6209. } else {
  6210. DECLARE_ALIGNED_8(int16_t, bS[2][4][4]);
  6211. uint64_t (*bSv)[4] = (uint64_t(*)[4])bS;
  6212. int edges;
  6213. if( IS_8x8DCT(mb_type) && (h->cbp&7) == 7 ) {
  6214. edges = 4;
  6215. bSv[0][0] = bSv[0][2] = bSv[1][0] = bSv[1][2] = 0x0002000200020002ULL;
  6216. } else {
  6217. int mask_edge1 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 :
  6218. (mb_type & MB_TYPE_16x8) ? 1 : 0;
  6219. int mask_edge0 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16))
  6220. && (s->current_picture.mb_type[mb_xy-1] & (MB_TYPE_16x16 | MB_TYPE_8x16))
  6221. ? 3 : 0;
  6222. int step = IS_8x8DCT(mb_type) ? 2 : 1;
  6223. edges = (mb_type & MB_TYPE_16x16) && !(h->cbp & 15) ? 1 : 4;
  6224. s->dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,
  6225. (h->slice_type == B_TYPE), edges, step, mask_edge0, mask_edge1 );
  6226. }
  6227. if( IS_INTRA(s->current_picture.mb_type[mb_xy-1]) )
  6228. bSv[0][0] = 0x0004000400040004ULL;
  6229. if( IS_INTRA(s->current_picture.mb_type[h->top_mb_xy]) )
  6230. bSv[1][0] = 0x0004000400040004ULL;
  6231. #define FILTER(hv,dir,edge)\
  6232. if(bSv[dir][edge]) {\
  6233. filter_mb_edge##hv( h, &img_y[4*edge*(dir?linesize:1)], linesize, bS[dir][edge], edge ? qp : qp##dir );\
  6234. if(!(edge&1)) {\
  6235. filter_mb_edgec##hv( h, &img_cb[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  6236. filter_mb_edgec##hv( h, &img_cr[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  6237. }\
  6238. }
  6239. if( edges == 1 ) {
  6240. FILTER(v,0,0);
  6241. FILTER(h,1,0);
  6242. } else if( IS_8x8DCT(mb_type) ) {
  6243. FILTER(v,0,0);
  6244. FILTER(v,0,2);
  6245. FILTER(h,1,0);
  6246. FILTER(h,1,2);
  6247. } else {
  6248. FILTER(v,0,0);
  6249. FILTER(v,0,1);
  6250. FILTER(v,0,2);
  6251. FILTER(v,0,3);
  6252. FILTER(h,1,0);
  6253. FILTER(h,1,1);
  6254. FILTER(h,1,2);
  6255. FILTER(h,1,3);
  6256. }
  6257. #undef FILTER
  6258. }
  6259. }
  6260. 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) {
  6261. MpegEncContext * const s = &h->s;
  6262. const int mb_xy= mb_x + mb_y*s->mb_stride;
  6263. const int mb_type = s->current_picture.mb_type[mb_xy];
  6264. const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4;
  6265. int first_vertical_edge_done = 0;
  6266. int dir;
  6267. /* FIXME: A given frame may occupy more than one position in
  6268. * the reference list. So ref2frm should be populated with
  6269. * frame numbers, not indices. */
  6270. static const int ref2frm[34] = {-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  6271. 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
  6272. //for sufficiently low qp, filtering wouldn't do anything
  6273. //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
  6274. if(!FRAME_MBAFF){
  6275. int qp_thresh = 15 - h->slice_alpha_c0_offset - FFMAX(0, h->pps.chroma_qp_index_offset);
  6276. int qp = s->current_picture.qscale_table[mb_xy];
  6277. if(qp <= qp_thresh
  6278. && (mb_x == 0 || ((qp + s->current_picture.qscale_table[mb_xy-1] + 1)>>1) <= qp_thresh)
  6279. && (mb_y == 0 || ((qp + s->current_picture.qscale_table[h->top_mb_xy] + 1)>>1) <= qp_thresh)){
  6280. return;
  6281. }
  6282. }
  6283. if (FRAME_MBAFF
  6284. // left mb is in picture
  6285. && h->slice_table[mb_xy-1] != 255
  6286. // and current and left pair do not have the same interlaced type
  6287. && (IS_INTERLACED(mb_type) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
  6288. // and left mb is in the same slice if deblocking_filter == 2
  6289. && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
  6290. /* First vertical edge is different in MBAFF frames
  6291. * There are 8 different bS to compute and 2 different Qp
  6292. */
  6293. const int pair_xy = mb_x + (mb_y&~1)*s->mb_stride;
  6294. const int left_mb_xy[2] = { pair_xy-1, pair_xy-1+s->mb_stride };
  6295. int16_t bS[8];
  6296. int qp[2];
  6297. int chroma_qp[2];
  6298. int mb_qp, mbn0_qp, mbn1_qp;
  6299. int i;
  6300. first_vertical_edge_done = 1;
  6301. if( IS_INTRA(mb_type) )
  6302. bS[0] = bS[1] = bS[2] = bS[3] = bS[4] = bS[5] = bS[6] = bS[7] = 4;
  6303. else {
  6304. for( i = 0; i < 8; i++ ) {
  6305. int mbn_xy = MB_FIELD ? left_mb_xy[i>>2] : left_mb_xy[i&1];
  6306. if( IS_INTRA( s->current_picture.mb_type[mbn_xy] ) )
  6307. bS[i] = 4;
  6308. else if( h->non_zero_count_cache[12+8*(i>>1)] != 0 ||
  6309. /* FIXME: with 8x8dct + cavlc, should check cbp instead of nnz */
  6310. h->non_zero_count[mbn_xy][MB_FIELD ? i&3 : (i>>2)+(mb_y&1)*2] )
  6311. bS[i] = 2;
  6312. else
  6313. bS[i] = 1;
  6314. }
  6315. }
  6316. mb_qp = s->current_picture.qscale_table[mb_xy];
  6317. mbn0_qp = s->current_picture.qscale_table[left_mb_xy[0]];
  6318. mbn1_qp = s->current_picture.qscale_table[left_mb_xy[1]];
  6319. qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1;
  6320. chroma_qp[0] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6321. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn0_qp ) + 1 ) >> 1;
  6322. qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1;
  6323. chroma_qp[1] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6324. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn1_qp ) + 1 ) >> 1;
  6325. /* Filter edge */
  6326. 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);
  6327. { int i; for (i = 0; i < 8; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6328. filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp );
  6329. filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, chroma_qp );
  6330. filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, chroma_qp );
  6331. }
  6332. /* dir : 0 -> vertical edge, 1 -> horizontal edge */
  6333. for( dir = 0; dir < 2; dir++ )
  6334. {
  6335. int edge;
  6336. const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
  6337. const int mbm_type = s->current_picture.mb_type[mbm_xy];
  6338. int start = h->slice_table[mbm_xy] == 255 ? 1 : 0;
  6339. const int edges = (mb_type & (MB_TYPE_16x16|MB_TYPE_SKIP))
  6340. == (MB_TYPE_16x16|MB_TYPE_SKIP) ? 1 : 4;
  6341. // how often to recheck mv-based bS when iterating between edges
  6342. const int mask_edge = (mb_type & (MB_TYPE_16x16 | (MB_TYPE_16x8 << dir))) ? 3 :
  6343. (mb_type & (MB_TYPE_8x16 >> dir)) ? 1 : 0;
  6344. // how often to recheck mv-based bS when iterating along each edge
  6345. const int mask_par0 = mb_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir));
  6346. if (first_vertical_edge_done) {
  6347. start = 1;
  6348. first_vertical_edge_done = 0;
  6349. }
  6350. if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
  6351. start = 1;
  6352. if (FRAME_MBAFF && (dir == 1) && ((mb_y&1) == 0) && start == 0
  6353. && !IS_INTERLACED(mb_type)
  6354. && IS_INTERLACED(mbm_type)
  6355. ) {
  6356. // This is a special case in the norm where the filtering must
  6357. // be done twice (one each of the field) even if we are in a
  6358. // frame macroblock.
  6359. //
  6360. static const int nnz_idx[4] = {4,5,6,3};
  6361. unsigned int tmp_linesize = 2 * linesize;
  6362. unsigned int tmp_uvlinesize = 2 * uvlinesize;
  6363. int mbn_xy = mb_xy - 2 * s->mb_stride;
  6364. int qp, chroma_qp;
  6365. int i, j;
  6366. int16_t bS[4];
  6367. for(j=0; j<2; j++, mbn_xy += s->mb_stride){
  6368. if( IS_INTRA(mb_type) ||
  6369. IS_INTRA(s->current_picture.mb_type[mbn_xy]) ) {
  6370. bS[0] = bS[1] = bS[2] = bS[3] = 3;
  6371. } else {
  6372. const uint8_t *mbn_nnz = h->non_zero_count[mbn_xy];
  6373. for( i = 0; i < 4; i++ ) {
  6374. if( h->non_zero_count_cache[scan8[0]+i] != 0 ||
  6375. mbn_nnz[nnz_idx[i]] != 0 )
  6376. bS[i] = 2;
  6377. else
  6378. bS[i] = 1;
  6379. }
  6380. }
  6381. // Do not use s->qscale as luma quantizer because it has not the same
  6382. // value in IPCM macroblocks.
  6383. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6384. 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);
  6385. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6386. filter_mb_edgeh( h, &img_y[j*linesize], tmp_linesize, bS, qp );
  6387. chroma_qp = ( h->chroma_qp +
  6388. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6389. filter_mb_edgech( h, &img_cb[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6390. filter_mb_edgech( h, &img_cr[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6391. }
  6392. start = 1;
  6393. }
  6394. /* Calculate bS */
  6395. for( edge = start; edge < edges; edge++ ) {
  6396. /* mbn_xy: neighbor macroblock */
  6397. const int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
  6398. const int mbn_type = s->current_picture.mb_type[mbn_xy];
  6399. int16_t bS[4];
  6400. int qp;
  6401. if( (edge&1) && IS_8x8DCT(mb_type) )
  6402. continue;
  6403. if( IS_INTRA(mb_type) ||
  6404. IS_INTRA(mbn_type) ) {
  6405. int value;
  6406. if (edge == 0) {
  6407. if ( (!IS_INTERLACED(mb_type) && !IS_INTERLACED(mbm_type))
  6408. || ((FRAME_MBAFF || (s->picture_structure != PICT_FRAME)) && (dir == 0))
  6409. ) {
  6410. value = 4;
  6411. } else {
  6412. value = 3;
  6413. }
  6414. } else {
  6415. value = 3;
  6416. }
  6417. bS[0] = bS[1] = bS[2] = bS[3] = value;
  6418. } else {
  6419. int i, l;
  6420. int mv_done;
  6421. if( edge & mask_edge ) {
  6422. bS[0] = bS[1] = bS[2] = bS[3] = 0;
  6423. mv_done = 1;
  6424. }
  6425. else if( FRAME_MBAFF && IS_INTERLACED(mb_type ^ mbn_type)) {
  6426. bS[0] = bS[1] = bS[2] = bS[3] = 1;
  6427. mv_done = 1;
  6428. }
  6429. else if( mask_par0 && (edge || (mbn_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir)))) ) {
  6430. int b_idx= 8 + 4 + edge * (dir ? 8:1);
  6431. int bn_idx= b_idx - (dir ? 8:1);
  6432. int v = 0;
  6433. for( l = 0; !v && l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6434. v |= ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6435. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6436. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit;
  6437. }
  6438. bS[0] = bS[1] = bS[2] = bS[3] = v;
  6439. mv_done = 1;
  6440. }
  6441. else
  6442. mv_done = 0;
  6443. for( i = 0; i < 4; i++ ) {
  6444. int x = dir == 0 ? edge : i;
  6445. int y = dir == 0 ? i : edge;
  6446. int b_idx= 8 + 4 + x + 8*y;
  6447. int bn_idx= b_idx - (dir ? 8:1);
  6448. if( h->non_zero_count_cache[b_idx] != 0 ||
  6449. h->non_zero_count_cache[bn_idx] != 0 ) {
  6450. bS[i] = 2;
  6451. }
  6452. else if(!mv_done)
  6453. {
  6454. bS[i] = 0;
  6455. for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6456. if( ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6457. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6458. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit ) {
  6459. bS[i] = 1;
  6460. break;
  6461. }
  6462. }
  6463. }
  6464. }
  6465. if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
  6466. continue;
  6467. }
  6468. /* Filter edge */
  6469. // Do not use s->qscale as luma quantizer because it has not the same
  6470. // value in IPCM macroblocks.
  6471. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6472. //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]);
  6473. tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
  6474. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6475. if( dir == 0 ) {
  6476. filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
  6477. if( (edge&1) == 0 ) {
  6478. int chroma_qp = ( h->chroma_qp +
  6479. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6480. filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp );
  6481. filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp );
  6482. }
  6483. } else {
  6484. filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
  6485. if( (edge&1) == 0 ) {
  6486. int chroma_qp = ( h->chroma_qp +
  6487. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6488. filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6489. filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6490. }
  6491. }
  6492. }
  6493. }
  6494. }
  6495. static int decode_slice(H264Context *h){
  6496. MpegEncContext * const s = &h->s;
  6497. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  6498. s->mb_skip_run= -1;
  6499. if( h->pps.cabac ) {
  6500. int i;
  6501. /* realign */
  6502. align_get_bits( &s->gb );
  6503. /* init cabac */
  6504. ff_init_cabac_states( &h->cabac);
  6505. ff_init_cabac_decoder( &h->cabac,
  6506. s->gb.buffer + get_bits_count(&s->gb)/8,
  6507. ( s->gb.size_in_bits - get_bits_count(&s->gb) + 7)/8);
  6508. /* calculate pre-state */
  6509. for( i= 0; i < 460; i++ ) {
  6510. int pre;
  6511. if( h->slice_type == I_TYPE )
  6512. pre = clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
  6513. else
  6514. 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 );
  6515. if( pre <= 63 )
  6516. h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
  6517. else
  6518. h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
  6519. }
  6520. for(;;){
  6521. //START_TIMER
  6522. int ret = decode_mb_cabac(h);
  6523. int eos;
  6524. //STOP_TIMER("decode_mb_cabac")
  6525. if(ret>=0) hl_decode_mb(h);
  6526. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  6527. s->mb_y++;
  6528. if(ret>=0) ret = decode_mb_cabac(h);
  6529. if(ret>=0) hl_decode_mb(h);
  6530. s->mb_y--;
  6531. }
  6532. eos = get_cabac_terminate( &h->cabac );
  6533. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  6534. 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);
  6535. 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);
  6536. return -1;
  6537. }
  6538. if( ++s->mb_x >= s->mb_width ) {
  6539. s->mb_x = 0;
  6540. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6541. ++s->mb_y;
  6542. if(FRAME_MBAFF) {
  6543. ++s->mb_y;
  6544. }
  6545. }
  6546. if( eos || s->mb_y >= s->mb_height ) {
  6547. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6548. 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);
  6549. return 0;
  6550. }
  6551. }
  6552. } else {
  6553. for(;;){
  6554. int ret = decode_mb_cavlc(h);
  6555. if(ret>=0) hl_decode_mb(h);
  6556. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  6557. s->mb_y++;
  6558. ret = decode_mb_cavlc(h);
  6559. if(ret>=0) hl_decode_mb(h);
  6560. s->mb_y--;
  6561. }
  6562. if(ret<0){
  6563. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6564. 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);
  6565. return -1;
  6566. }
  6567. if(++s->mb_x >= s->mb_width){
  6568. s->mb_x=0;
  6569. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6570. ++s->mb_y;
  6571. if(FRAME_MBAFF) {
  6572. ++s->mb_y;
  6573. }
  6574. if(s->mb_y >= s->mb_height){
  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_END|DC_END|MV_END)&part_mask);
  6581. return -1;
  6582. }
  6583. }
  6584. }
  6585. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  6586. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6587. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  6588. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  6589. return 0;
  6590. }else{
  6591. 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);
  6592. return -1;
  6593. }
  6594. }
  6595. }
  6596. }
  6597. #if 0
  6598. for(;s->mb_y < s->mb_height; s->mb_y++){
  6599. for(;s->mb_x < s->mb_width; s->mb_x++){
  6600. int ret= decode_mb(h);
  6601. hl_decode_mb(h);
  6602. if(ret<0){
  6603. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6604. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
  6605. return -1;
  6606. }
  6607. if(++s->mb_x >= s->mb_width){
  6608. s->mb_x=0;
  6609. if(++s->mb_y >= s->mb_height){
  6610. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6611. 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);
  6612. return 0;
  6613. }else{
  6614. 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);
  6615. return -1;
  6616. }
  6617. }
  6618. }
  6619. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  6620. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6621. 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);
  6622. return 0;
  6623. }else{
  6624. 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);
  6625. return -1;
  6626. }
  6627. }
  6628. }
  6629. s->mb_x=0;
  6630. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6631. }
  6632. #endif
  6633. return -1; //not reached
  6634. }
  6635. static int decode_unregistered_user_data(H264Context *h, int size){
  6636. MpegEncContext * const s = &h->s;
  6637. uint8_t user_data[16+256];
  6638. int e, build, i;
  6639. if(size<16)
  6640. return -1;
  6641. for(i=0; i<sizeof(user_data)-1 && i<size; i++){
  6642. user_data[i]= get_bits(&s->gb, 8);
  6643. }
  6644. user_data[i]= 0;
  6645. e= sscanf(user_data+16, "x264 - core %d"/*%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html*/, &build);
  6646. if(e==1 && build>=0)
  6647. h->x264_build= build;
  6648. if(s->avctx->debug & FF_DEBUG_BUGS)
  6649. av_log(s->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data+16);
  6650. for(; i<size; i++)
  6651. skip_bits(&s->gb, 8);
  6652. return 0;
  6653. }
  6654. static int decode_sei(H264Context *h){
  6655. MpegEncContext * const s = &h->s;
  6656. while(get_bits_count(&s->gb) + 16 < s->gb.size_in_bits){
  6657. int size, type;
  6658. type=0;
  6659. do{
  6660. type+= show_bits(&s->gb, 8);
  6661. }while(get_bits(&s->gb, 8) == 255);
  6662. size=0;
  6663. do{
  6664. size+= show_bits(&s->gb, 8);
  6665. }while(get_bits(&s->gb, 8) == 255);
  6666. switch(type){
  6667. case 5:
  6668. if(decode_unregistered_user_data(h, size) < 0)
  6669. return -1;
  6670. break;
  6671. default:
  6672. skip_bits(&s->gb, 8*size);
  6673. }
  6674. //FIXME check bits here
  6675. align_get_bits(&s->gb);
  6676. }
  6677. return 0;
  6678. }
  6679. static inline void decode_hrd_parameters(H264Context *h, SPS *sps){
  6680. MpegEncContext * const s = &h->s;
  6681. int cpb_count, i;
  6682. cpb_count = get_ue_golomb(&s->gb) + 1;
  6683. get_bits(&s->gb, 4); /* bit_rate_scale */
  6684. get_bits(&s->gb, 4); /* cpb_size_scale */
  6685. for(i=0; i<cpb_count; i++){
  6686. get_ue_golomb(&s->gb); /* bit_rate_value_minus1 */
  6687. get_ue_golomb(&s->gb); /* cpb_size_value_minus1 */
  6688. get_bits1(&s->gb); /* cbr_flag */
  6689. }
  6690. get_bits(&s->gb, 5); /* initial_cpb_removal_delay_length_minus1 */
  6691. get_bits(&s->gb, 5); /* cpb_removal_delay_length_minus1 */
  6692. get_bits(&s->gb, 5); /* dpb_output_delay_length_minus1 */
  6693. get_bits(&s->gb, 5); /* time_offset_length */
  6694. }
  6695. static inline int decode_vui_parameters(H264Context *h, SPS *sps){
  6696. MpegEncContext * const s = &h->s;
  6697. int aspect_ratio_info_present_flag, aspect_ratio_idc;
  6698. int nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag;
  6699. aspect_ratio_info_present_flag= get_bits1(&s->gb);
  6700. if( aspect_ratio_info_present_flag ) {
  6701. aspect_ratio_idc= get_bits(&s->gb, 8);
  6702. if( aspect_ratio_idc == EXTENDED_SAR ) {
  6703. sps->sar.num= get_bits(&s->gb, 16);
  6704. sps->sar.den= get_bits(&s->gb, 16);
  6705. }else if(aspect_ratio_idc < 14){
  6706. sps->sar= pixel_aspect[aspect_ratio_idc];
  6707. }else{
  6708. av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
  6709. return -1;
  6710. }
  6711. }else{
  6712. sps->sar.num=
  6713. sps->sar.den= 0;
  6714. }
  6715. // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
  6716. if(get_bits1(&s->gb)){ /* overscan_info_present_flag */
  6717. get_bits1(&s->gb); /* overscan_appropriate_flag */
  6718. }
  6719. if(get_bits1(&s->gb)){ /* video_signal_type_present_flag */
  6720. get_bits(&s->gb, 3); /* video_format */
  6721. get_bits1(&s->gb); /* video_full_range_flag */
  6722. if(get_bits1(&s->gb)){ /* colour_description_present_flag */
  6723. get_bits(&s->gb, 8); /* colour_primaries */
  6724. get_bits(&s->gb, 8); /* transfer_characteristics */
  6725. get_bits(&s->gb, 8); /* matrix_coefficients */
  6726. }
  6727. }
  6728. if(get_bits1(&s->gb)){ /* chroma_location_info_present_flag */
  6729. get_ue_golomb(&s->gb); /* chroma_sample_location_type_top_field */
  6730. get_ue_golomb(&s->gb); /* chroma_sample_location_type_bottom_field */
  6731. }
  6732. sps->timing_info_present_flag = get_bits1(&s->gb);
  6733. if(sps->timing_info_present_flag){
  6734. sps->num_units_in_tick = get_bits_long(&s->gb, 32);
  6735. sps->time_scale = get_bits_long(&s->gb, 32);
  6736. sps->fixed_frame_rate_flag = get_bits1(&s->gb);
  6737. }
  6738. nal_hrd_parameters_present_flag = get_bits1(&s->gb);
  6739. if(nal_hrd_parameters_present_flag)
  6740. decode_hrd_parameters(h, sps);
  6741. vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
  6742. if(vcl_hrd_parameters_present_flag)
  6743. decode_hrd_parameters(h, sps);
  6744. if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag)
  6745. get_bits1(&s->gb); /* low_delay_hrd_flag */
  6746. get_bits1(&s->gb); /* pic_struct_present_flag */
  6747. sps->bitstream_restriction_flag = get_bits1(&s->gb);
  6748. if(sps->bitstream_restriction_flag){
  6749. get_bits1(&s->gb); /* motion_vectors_over_pic_boundaries_flag */
  6750. get_ue_golomb(&s->gb); /* max_bytes_per_pic_denom */
  6751. get_ue_golomb(&s->gb); /* max_bits_per_mb_denom */
  6752. get_ue_golomb(&s->gb); /* log2_max_mv_length_horizontal */
  6753. get_ue_golomb(&s->gb); /* log2_max_mv_length_vertical */
  6754. sps->num_reorder_frames = get_ue_golomb(&s->gb);
  6755. get_ue_golomb(&s->gb); /* max_dec_frame_buffering */
  6756. }
  6757. return 0;
  6758. }
  6759. static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,
  6760. const uint8_t *jvt_list, const uint8_t *fallback_list){
  6761. MpegEncContext * const s = &h->s;
  6762. int i, last = 8, next = 8;
  6763. const uint8_t *scan = size == 16 ? zigzag_scan : zigzag_scan8x8;
  6764. if(!get_bits1(&s->gb)) /* matrix not written, we use the predicted one */
  6765. memcpy(factors, fallback_list, size*sizeof(uint8_t));
  6766. else
  6767. for(i=0;i<size;i++){
  6768. if(next)
  6769. next = (last + get_se_golomb(&s->gb)) & 0xff;
  6770. if(!i && !next){ /* matrix not written, we use the preset one */
  6771. memcpy(factors, jvt_list, size*sizeof(uint8_t));
  6772. break;
  6773. }
  6774. last = factors[scan[i]] = next ? next : last;
  6775. }
  6776. }
  6777. static void decode_scaling_matrices(H264Context *h, SPS *sps, PPS *pps, int is_sps,
  6778. uint8_t (*scaling_matrix4)[16], uint8_t (*scaling_matrix8)[64]){
  6779. MpegEncContext * const s = &h->s;
  6780. int fallback_sps = !is_sps && sps->scaling_matrix_present;
  6781. const uint8_t *fallback[4] = {
  6782. fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
  6783. fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
  6784. fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
  6785. fallback_sps ? sps->scaling_matrix8[1] : default_scaling8[1]
  6786. };
  6787. if(get_bits1(&s->gb)){
  6788. sps->scaling_matrix_present |= is_sps;
  6789. decode_scaling_list(h,scaling_matrix4[0],16,default_scaling4[0],fallback[0]); // Intra, Y
  6790. decode_scaling_list(h,scaling_matrix4[1],16,default_scaling4[0],scaling_matrix4[0]); // Intra, Cr
  6791. decode_scaling_list(h,scaling_matrix4[2],16,default_scaling4[0],scaling_matrix4[1]); // Intra, Cb
  6792. decode_scaling_list(h,scaling_matrix4[3],16,default_scaling4[1],fallback[1]); // Inter, Y
  6793. decode_scaling_list(h,scaling_matrix4[4],16,default_scaling4[1],scaling_matrix4[3]); // Inter, Cr
  6794. decode_scaling_list(h,scaling_matrix4[5],16,default_scaling4[1],scaling_matrix4[4]); // Inter, Cb
  6795. if(is_sps || pps->transform_8x8_mode){
  6796. decode_scaling_list(h,scaling_matrix8[0],64,default_scaling8[0],fallback[2]); // Intra, Y
  6797. decode_scaling_list(h,scaling_matrix8[1],64,default_scaling8[1],fallback[3]); // Inter, Y
  6798. }
  6799. } else if(fallback_sps) {
  6800. memcpy(scaling_matrix4, sps->scaling_matrix4, 6*16*sizeof(uint8_t));
  6801. memcpy(scaling_matrix8, sps->scaling_matrix8, 2*64*sizeof(uint8_t));
  6802. }
  6803. }
  6804. static inline int decode_seq_parameter_set(H264Context *h){
  6805. MpegEncContext * const s = &h->s;
  6806. int profile_idc, level_idc;
  6807. int sps_id, i;
  6808. SPS *sps;
  6809. profile_idc= get_bits(&s->gb, 8);
  6810. get_bits1(&s->gb); //constraint_set0_flag
  6811. get_bits1(&s->gb); //constraint_set1_flag
  6812. get_bits1(&s->gb); //constraint_set2_flag
  6813. get_bits1(&s->gb); //constraint_set3_flag
  6814. get_bits(&s->gb, 4); // reserved
  6815. level_idc= get_bits(&s->gb, 8);
  6816. sps_id= get_ue_golomb(&s->gb);
  6817. sps= &h->sps_buffer[ sps_id ];
  6818. sps->profile_idc= profile_idc;
  6819. sps->level_idc= level_idc;
  6820. if(sps->profile_idc >= 100){ //high profile
  6821. if(get_ue_golomb(&s->gb) == 3) //chroma_format_idc
  6822. get_bits1(&s->gb); //residual_color_transform_flag
  6823. get_ue_golomb(&s->gb); //bit_depth_luma_minus8
  6824. get_ue_golomb(&s->gb); //bit_depth_chroma_minus8
  6825. sps->transform_bypass = get_bits1(&s->gb);
  6826. decode_scaling_matrices(h, sps, NULL, 1, sps->scaling_matrix4, sps->scaling_matrix8);
  6827. }else
  6828. sps->scaling_matrix_present = 0;
  6829. sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
  6830. sps->poc_type= get_ue_golomb(&s->gb);
  6831. if(sps->poc_type == 0){ //FIXME #define
  6832. sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
  6833. } else if(sps->poc_type == 1){//FIXME #define
  6834. sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
  6835. sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
  6836. sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
  6837. sps->poc_cycle_length= get_ue_golomb(&s->gb);
  6838. for(i=0; i<sps->poc_cycle_length; i++)
  6839. sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
  6840. }
  6841. if(sps->poc_type > 2){
  6842. av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
  6843. return -1;
  6844. }
  6845. sps->ref_frame_count= get_ue_golomb(&s->gb);
  6846. if(sps->ref_frame_count > MAX_PICTURE_COUNT-2){
  6847. av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");
  6848. }
  6849. sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
  6850. sps->mb_width= get_ue_golomb(&s->gb) + 1;
  6851. sps->mb_height= get_ue_golomb(&s->gb) + 1;
  6852. if((unsigned)sps->mb_width >= INT_MAX/16 || (unsigned)sps->mb_height >= INT_MAX/16 ||
  6853. avcodec_check_dimensions(NULL, 16*sps->mb_width, 16*sps->mb_height))
  6854. return -1;
  6855. sps->frame_mbs_only_flag= get_bits1(&s->gb);
  6856. if(!sps->frame_mbs_only_flag)
  6857. sps->mb_aff= get_bits1(&s->gb);
  6858. else
  6859. sps->mb_aff= 0;
  6860. sps->direct_8x8_inference_flag= get_bits1(&s->gb);
  6861. #ifndef ALLOW_INTERLACE
  6862. if(sps->mb_aff)
  6863. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF support not included; enable it at compile-time.\n");
  6864. #endif
  6865. if(!sps->direct_8x8_inference_flag && sps->mb_aff)
  6866. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + !direct_8x8_inference is not implemented\n");
  6867. sps->crop= get_bits1(&s->gb);
  6868. if(sps->crop){
  6869. sps->crop_left = get_ue_golomb(&s->gb);
  6870. sps->crop_right = get_ue_golomb(&s->gb);
  6871. sps->crop_top = get_ue_golomb(&s->gb);
  6872. sps->crop_bottom= get_ue_golomb(&s->gb);
  6873. if(sps->crop_left || sps->crop_top){
  6874. av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ...\n");
  6875. }
  6876. }else{
  6877. sps->crop_left =
  6878. sps->crop_right =
  6879. sps->crop_top =
  6880. sps->crop_bottom= 0;
  6881. }
  6882. sps->vui_parameters_present_flag= get_bits1(&s->gb);
  6883. if( sps->vui_parameters_present_flag )
  6884. decode_vui_parameters(h, sps);
  6885. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6886. av_log(h->s.avctx, AV_LOG_DEBUG, "sps:%d profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%d/%d/%d/%d %s\n",
  6887. sps_id, sps->profile_idc, sps->level_idc,
  6888. sps->poc_type,
  6889. sps->ref_frame_count,
  6890. sps->mb_width, sps->mb_height,
  6891. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  6892. sps->direct_8x8_inference_flag ? "8B8" : "",
  6893. sps->crop_left, sps->crop_right,
  6894. sps->crop_top, sps->crop_bottom,
  6895. sps->vui_parameters_present_flag ? "VUI" : ""
  6896. );
  6897. }
  6898. return 0;
  6899. }
  6900. static inline int decode_picture_parameter_set(H264Context *h, int bit_length){
  6901. MpegEncContext * const s = &h->s;
  6902. int pps_id= get_ue_golomb(&s->gb);
  6903. PPS *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:%d sps:%d %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){
  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. while(ptr[dst_length - 1] == 0 && dst_length > 1)
  7113. dst_length--;
  7114. bit_length= 8*dst_length - decode_rbsp_trailing(ptr + dst_length - 1);
  7115. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  7116. 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);
  7117. }
  7118. if (h->is_avc && (nalsize != consumed))
  7119. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  7120. buf_index += consumed;
  7121. if( (s->hurry_up == 1 && h->nal_ref_idc == 0) //FIXME dont discard SEI id
  7122. ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  7123. continue;
  7124. switch(h->nal_unit_type){
  7125. case NAL_IDR_SLICE:
  7126. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  7127. case NAL_SLICE:
  7128. init_get_bits(&s->gb, ptr, bit_length);
  7129. h->intra_gb_ptr=
  7130. h->inter_gb_ptr= &s->gb;
  7131. s->data_partitioning = 0;
  7132. if(decode_slice_header(h) < 0){
  7133. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  7134. break;
  7135. }
  7136. s->current_picture_ptr->key_frame= (h->nal_unit_type == NAL_IDR_SLICE);
  7137. if(h->redundant_pic_count==0 && s->hurry_up < 5
  7138. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  7139. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  7140. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  7141. && avctx->skip_frame < AVDISCARD_ALL)
  7142. decode_slice(h);
  7143. break;
  7144. case NAL_DPA:
  7145. init_get_bits(&s->gb, ptr, bit_length);
  7146. h->intra_gb_ptr=
  7147. h->inter_gb_ptr= NULL;
  7148. s->data_partitioning = 1;
  7149. if(decode_slice_header(h) < 0){
  7150. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  7151. }
  7152. break;
  7153. case NAL_DPB:
  7154. init_get_bits(&h->intra_gb, ptr, bit_length);
  7155. h->intra_gb_ptr= &h->intra_gb;
  7156. break;
  7157. case NAL_DPC:
  7158. init_get_bits(&h->inter_gb, ptr, bit_length);
  7159. h->inter_gb_ptr= &h->inter_gb;
  7160. if(h->redundant_pic_count==0 && h->intra_gb_ptr && s->data_partitioning
  7161. && s->hurry_up < 5
  7162. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  7163. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  7164. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  7165. && avctx->skip_frame < AVDISCARD_ALL)
  7166. decode_slice(h);
  7167. break;
  7168. case NAL_SEI:
  7169. init_get_bits(&s->gb, ptr, bit_length);
  7170. decode_sei(h);
  7171. break;
  7172. case NAL_SPS:
  7173. init_get_bits(&s->gb, ptr, bit_length);
  7174. decode_seq_parameter_set(h);
  7175. if(s->flags& CODEC_FLAG_LOW_DELAY)
  7176. s->low_delay=1;
  7177. if(avctx->has_b_frames < 2)
  7178. avctx->has_b_frames= !s->low_delay;
  7179. break;
  7180. case NAL_PPS:
  7181. init_get_bits(&s->gb, ptr, bit_length);
  7182. decode_picture_parameter_set(h, bit_length);
  7183. break;
  7184. case NAL_AUD:
  7185. case NAL_END_SEQUENCE:
  7186. case NAL_END_STREAM:
  7187. case NAL_FILLER_DATA:
  7188. case NAL_SPS_EXT:
  7189. case NAL_AUXILIARY_SLICE:
  7190. break;
  7191. default:
  7192. av_log(avctx, AV_LOG_ERROR, "Unknown NAL code: %d\n", h->nal_unit_type);
  7193. }
  7194. }
  7195. if(!s->current_picture_ptr) return buf_index; //no frame
  7196. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  7197. s->current_picture_ptr->pict_type= s->pict_type;
  7198. h->prev_frame_num_offset= h->frame_num_offset;
  7199. h->prev_frame_num= h->frame_num;
  7200. if(s->current_picture_ptr->reference){
  7201. h->prev_poc_msb= h->poc_msb;
  7202. h->prev_poc_lsb= h->poc_lsb;
  7203. }
  7204. if(s->current_picture_ptr->reference)
  7205. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  7206. ff_er_frame_end(s);
  7207. MPV_frame_end(s);
  7208. return buf_index;
  7209. }
  7210. /**
  7211. * returns the number of bytes consumed for building the current frame
  7212. */
  7213. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  7214. if(s->flags&CODEC_FLAG_TRUNCATED){
  7215. pos -= s->parse_context.last_index;
  7216. if(pos<0) pos=0; // FIXME remove (unneeded?)
  7217. return pos;
  7218. }else{
  7219. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  7220. if(pos+10>buf_size) pos=buf_size; // oops ;)
  7221. return pos;
  7222. }
  7223. }
  7224. static int decode_frame(AVCodecContext *avctx,
  7225. void *data, int *data_size,
  7226. uint8_t *buf, int buf_size)
  7227. {
  7228. H264Context *h = avctx->priv_data;
  7229. MpegEncContext *s = &h->s;
  7230. AVFrame *pict = data;
  7231. int buf_index;
  7232. s->flags= avctx->flags;
  7233. s->flags2= avctx->flags2;
  7234. /* no supplementary picture */
  7235. if (buf_size == 0) {
  7236. return 0;
  7237. }
  7238. if(s->flags&CODEC_FLAG_TRUNCATED){
  7239. int next= find_frame_end(h, buf, buf_size);
  7240. if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
  7241. return buf_size;
  7242. //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);
  7243. }
  7244. if(h->is_avc && !h->got_avcC) {
  7245. int i, cnt, nalsize;
  7246. unsigned char *p = avctx->extradata;
  7247. if(avctx->extradata_size < 7) {
  7248. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  7249. return -1;
  7250. }
  7251. if(*p != 1) {
  7252. av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
  7253. return -1;
  7254. }
  7255. /* sps and pps in the avcC always have length coded with 2 bytes,
  7256. so put a fake nal_length_size = 2 while parsing them */
  7257. h->nal_length_size = 2;
  7258. // Decode sps from avcC
  7259. cnt = *(p+5) & 0x1f; // Number of sps
  7260. p += 6;
  7261. for (i = 0; i < cnt; i++) {
  7262. nalsize = BE_16(p) + 2;
  7263. if(decode_nal_units(h, p, nalsize) < 0) {
  7264. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  7265. return -1;
  7266. }
  7267. p += nalsize;
  7268. }
  7269. // Decode pps from avcC
  7270. cnt = *(p++); // Number of pps
  7271. for (i = 0; i < cnt; i++) {
  7272. nalsize = BE_16(p) + 2;
  7273. if(decode_nal_units(h, p, nalsize) != nalsize) {
  7274. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  7275. return -1;
  7276. }
  7277. p += nalsize;
  7278. }
  7279. // Now store right nal length size, that will be use to parse all other nals
  7280. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  7281. // Do not reparse avcC
  7282. h->got_avcC = 1;
  7283. }
  7284. if(!h->is_avc && s->avctx->extradata_size && s->picture_number==0){
  7285. if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
  7286. return -1;
  7287. }
  7288. buf_index=decode_nal_units(h, buf, buf_size);
  7289. if(buf_index < 0)
  7290. return -1;
  7291. //FIXME do something with unavailable reference frames
  7292. // if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_index, buf_size);
  7293. if(!s->current_picture_ptr){
  7294. av_log(h->s.avctx, AV_LOG_DEBUG, "error, NO frame\n");
  7295. return -1;
  7296. }
  7297. {
  7298. Picture *out = s->current_picture_ptr;
  7299. #if 0 //decode order
  7300. *data_size = sizeof(AVFrame);
  7301. #else
  7302. /* Sort B-frames into display order */
  7303. Picture *cur = s->current_picture_ptr;
  7304. Picture *prev = h->delayed_output_pic;
  7305. int i, pics, cross_idr, out_of_order, out_idx;
  7306. if(h->sps.bitstream_restriction_flag
  7307. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  7308. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  7309. s->low_delay = 0;
  7310. }
  7311. pics = 0;
  7312. while(h->delayed_pic[pics]) pics++;
  7313. h->delayed_pic[pics++] = cur;
  7314. if(cur->reference == 0)
  7315. cur->reference = 1;
  7316. cross_idr = 0;
  7317. for(i=0; h->delayed_pic[i]; i++)
  7318. if(h->delayed_pic[i]->key_frame || h->delayed_pic[i]->poc==0)
  7319. cross_idr = 1;
  7320. out = h->delayed_pic[0];
  7321. out_idx = 0;
  7322. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
  7323. if(h->delayed_pic[i]->poc < out->poc){
  7324. out = h->delayed_pic[i];
  7325. out_idx = i;
  7326. }
  7327. out_of_order = !cross_idr && prev && out->poc < prev->poc;
  7328. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  7329. { }
  7330. else if(prev && pics <= s->avctx->has_b_frames)
  7331. out = prev;
  7332. else if((out_of_order && pics-1 == s->avctx->has_b_frames && pics < 15)
  7333. || (s->low_delay &&
  7334. ((!cross_idr && prev && out->poc > prev->poc + 2)
  7335. || cur->pict_type == B_TYPE)))
  7336. {
  7337. s->low_delay = 0;
  7338. s->avctx->has_b_frames++;
  7339. out = prev;
  7340. }
  7341. else if(out_of_order)
  7342. out = prev;
  7343. if(out_of_order || pics > s->avctx->has_b_frames){
  7344. for(i=out_idx; h->delayed_pic[i]; i++)
  7345. h->delayed_pic[i] = h->delayed_pic[i+1];
  7346. }
  7347. if(prev == out)
  7348. *data_size = 0;
  7349. else
  7350. *data_size = sizeof(AVFrame);
  7351. if(prev && prev != out && prev->reference == 1)
  7352. prev->reference = 0;
  7353. h->delayed_output_pic = out;
  7354. #endif
  7355. if(out)
  7356. *pict= *(AVFrame*)out;
  7357. else
  7358. av_log(avctx, AV_LOG_DEBUG, "no picture\n");
  7359. }
  7360. assert(pict->data[0] || !*data_size);
  7361. ff_print_debug_info(s, pict);
  7362. //printf("out %d\n", (int)pict->data[0]);
  7363. #if 0 //?
  7364. /* Return the Picture timestamp as the frame number */
  7365. /* we substract 1 because it is added on utils.c */
  7366. avctx->frame_number = s->picture_number - 1;
  7367. #endif
  7368. return get_consumed_bytes(s, buf_index, buf_size);
  7369. }
  7370. #if 0
  7371. static inline void fill_mb_avail(H264Context *h){
  7372. MpegEncContext * const s = &h->s;
  7373. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  7374. if(s->mb_y){
  7375. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  7376. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  7377. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  7378. }else{
  7379. h->mb_avail[0]=
  7380. h->mb_avail[1]=
  7381. h->mb_avail[2]= 0;
  7382. }
  7383. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  7384. h->mb_avail[4]= 1; //FIXME move out
  7385. h->mb_avail[5]= 0; //FIXME move out
  7386. }
  7387. #endif
  7388. #if 0 //selftest
  7389. #define COUNT 8000
  7390. #define SIZE (COUNT*40)
  7391. int main(){
  7392. int i;
  7393. uint8_t temp[SIZE];
  7394. PutBitContext pb;
  7395. GetBitContext gb;
  7396. // int int_temp[10000];
  7397. DSPContext dsp;
  7398. AVCodecContext avctx;
  7399. dsputil_init(&dsp, &avctx);
  7400. init_put_bits(&pb, temp, SIZE);
  7401. printf("testing unsigned exp golomb\n");
  7402. for(i=0; i<COUNT; i++){
  7403. START_TIMER
  7404. set_ue_golomb(&pb, i);
  7405. STOP_TIMER("set_ue_golomb");
  7406. }
  7407. flush_put_bits(&pb);
  7408. init_get_bits(&gb, temp, 8*SIZE);
  7409. for(i=0; i<COUNT; i++){
  7410. int j, s;
  7411. s= show_bits(&gb, 24);
  7412. START_TIMER
  7413. j= get_ue_golomb(&gb);
  7414. if(j != i){
  7415. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7416. // return -1;
  7417. }
  7418. STOP_TIMER("get_ue_golomb");
  7419. }
  7420. init_put_bits(&pb, temp, SIZE);
  7421. printf("testing signed exp golomb\n");
  7422. for(i=0; i<COUNT; i++){
  7423. START_TIMER
  7424. set_se_golomb(&pb, i - COUNT/2);
  7425. STOP_TIMER("set_se_golomb");
  7426. }
  7427. flush_put_bits(&pb);
  7428. init_get_bits(&gb, temp, 8*SIZE);
  7429. for(i=0; i<COUNT; i++){
  7430. int j, s;
  7431. s= show_bits(&gb, 24);
  7432. START_TIMER
  7433. j= get_se_golomb(&gb);
  7434. if(j != i - COUNT/2){
  7435. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7436. // return -1;
  7437. }
  7438. STOP_TIMER("get_se_golomb");
  7439. }
  7440. printf("testing 4x4 (I)DCT\n");
  7441. DCTELEM block[16];
  7442. uint8_t src[16], ref[16];
  7443. uint64_t error= 0, max_error=0;
  7444. for(i=0; i<COUNT; i++){
  7445. int j;
  7446. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  7447. for(j=0; j<16; j++){
  7448. ref[j]= random()%255;
  7449. src[j]= random()%255;
  7450. }
  7451. h264_diff_dct_c(block, src, ref, 4);
  7452. //normalize
  7453. for(j=0; j<16; j++){
  7454. // printf("%d ", block[j]);
  7455. block[j]= block[j]*4;
  7456. if(j&1) block[j]= (block[j]*4 + 2)/5;
  7457. if(j&4) block[j]= (block[j]*4 + 2)/5;
  7458. }
  7459. // printf("\n");
  7460. s->dsp.h264_idct_add(ref, block, 4);
  7461. /* for(j=0; j<16; j++){
  7462. printf("%d ", ref[j]);
  7463. }
  7464. printf("\n");*/
  7465. for(j=0; j<16; j++){
  7466. int diff= FFABS(src[j] - ref[j]);
  7467. error+= diff*diff;
  7468. max_error= FFMAX(max_error, diff);
  7469. }
  7470. }
  7471. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  7472. #if 0
  7473. printf("testing quantizer\n");
  7474. for(qp=0; qp<52; qp++){
  7475. for(i=0; i<16; i++)
  7476. src1_block[i]= src2_block[i]= random()%255;
  7477. }
  7478. #endif
  7479. printf("Testing NAL layer\n");
  7480. uint8_t bitstream[COUNT];
  7481. uint8_t nal[COUNT*2];
  7482. H264Context h;
  7483. memset(&h, 0, sizeof(H264Context));
  7484. for(i=0; i<COUNT; i++){
  7485. int zeros= i;
  7486. int nal_length;
  7487. int consumed;
  7488. int out_length;
  7489. uint8_t *out;
  7490. int j;
  7491. for(j=0; j<COUNT; j++){
  7492. bitstream[j]= (random() % 255) + 1;
  7493. }
  7494. for(j=0; j<zeros; j++){
  7495. int pos= random() % COUNT;
  7496. while(bitstream[pos] == 0){
  7497. pos++;
  7498. pos %= COUNT;
  7499. }
  7500. bitstream[pos]=0;
  7501. }
  7502. START_TIMER
  7503. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  7504. if(nal_length<0){
  7505. printf("encoding failed\n");
  7506. return -1;
  7507. }
  7508. out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
  7509. STOP_TIMER("NAL")
  7510. if(out_length != COUNT){
  7511. printf("incorrect length %d %d\n", out_length, COUNT);
  7512. return -1;
  7513. }
  7514. if(consumed != nal_length){
  7515. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  7516. return -1;
  7517. }
  7518. if(memcmp(bitstream, out, COUNT)){
  7519. printf("missmatch\n");
  7520. return -1;
  7521. }
  7522. }
  7523. printf("Testing RBSP\n");
  7524. return 0;
  7525. }
  7526. #endif
  7527. static int decode_end(AVCodecContext *avctx)
  7528. {
  7529. H264Context *h = avctx->priv_data;
  7530. MpegEncContext *s = &h->s;
  7531. av_freep(&h->rbsp_buffer);
  7532. free_tables(h); //FIXME cleanup init stuff perhaps
  7533. MPV_common_end(s);
  7534. // memset(h, 0, sizeof(H264Context));
  7535. return 0;
  7536. }
  7537. AVCodec h264_decoder = {
  7538. "h264",
  7539. CODEC_TYPE_VIDEO,
  7540. CODEC_ID_H264,
  7541. sizeof(H264Context),
  7542. decode_init,
  7543. NULL,
  7544. decode_end,
  7545. decode_frame,
  7546. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  7547. .flush= flush_dpb,
  7548. };
  7549. #ifdef CONFIG_H264_PARSER
  7550. AVCodecParser h264_parser = {
  7551. { CODEC_ID_H264 },
  7552. sizeof(H264Context),
  7553. NULL,
  7554. h264_parse,
  7555. ff_parse_close,
  7556. h264_split,
  7557. };
  7558. #endif
  7559. #include "svq3.c"