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.

8115 lines
302KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. */
  20. /**
  21. * @file h264.c
  22. * H.264 / AVC / MPEG4 part10 codec.
  23. * @author Michael Niedermayer <michaelni@gmx.at>
  24. */
  25. #include "common.h"
  26. #include "dsputil.h"
  27. #include "avcodec.h"
  28. #include "mpegvideo.h"
  29. #include "h264data.h"
  30. #include "golomb.h"
  31. #include "cabac.h"
  32. //#undef NDEBUG
  33. #include <assert.h>
  34. #define interlaced_dct interlaced_dct_is_a_bad_name
  35. #define mb_intra mb_intra_isnt_initalized_see_mb_type
  36. #define LUMA_DC_BLOCK_INDEX 25
  37. #define CHROMA_DC_BLOCK_INDEX 26
  38. #define CHROMA_DC_COEFF_TOKEN_VLC_BITS 8
  39. #define COEFF_TOKEN_VLC_BITS 8
  40. #define TOTAL_ZEROS_VLC_BITS 9
  41. #define CHROMA_DC_TOTAL_ZEROS_VLC_BITS 3
  42. #define RUN_VLC_BITS 3
  43. #define RUN7_VLC_BITS 6
  44. #define MAX_SPS_COUNT 32
  45. #define MAX_PPS_COUNT 256
  46. #define MAX_MMCO_COUNT 66
  47. /**
  48. * Sequence parameter set
  49. */
  50. typedef struct SPS{
  51. int profile_idc;
  52. int level_idc;
  53. int transform_bypass; ///< qpprime_y_zero_transform_bypass_flag
  54. int log2_max_frame_num; ///< log2_max_frame_num_minus4 + 4
  55. int poc_type; ///< pic_order_cnt_type
  56. int log2_max_poc_lsb; ///< log2_max_pic_order_cnt_lsb_minus4
  57. int delta_pic_order_always_zero_flag;
  58. int offset_for_non_ref_pic;
  59. int offset_for_top_to_bottom_field;
  60. int poc_cycle_length; ///< num_ref_frames_in_pic_order_cnt_cycle
  61. int ref_frame_count; ///< num_ref_frames
  62. int gaps_in_frame_num_allowed_flag;
  63. int mb_width; ///< frame_width_in_mbs_minus1 + 1
  64. int mb_height; ///< frame_height_in_mbs_minus1 + 1
  65. int frame_mbs_only_flag;
  66. int mb_aff; ///<mb_adaptive_frame_field_flag
  67. int direct_8x8_inference_flag;
  68. int crop; ///< frame_cropping_flag
  69. int crop_left; ///< frame_cropping_rect_left_offset
  70. int crop_right; ///< frame_cropping_rect_right_offset
  71. int crop_top; ///< frame_cropping_rect_top_offset
  72. int crop_bottom; ///< frame_cropping_rect_bottom_offset
  73. int vui_parameters_present_flag;
  74. AVRational sar;
  75. int timing_info_present_flag;
  76. uint32_t num_units_in_tick;
  77. uint32_t time_scale;
  78. int fixed_frame_rate_flag;
  79. short offset_for_ref_frame[256]; //FIXME dyn aloc?
  80. int bitstream_restriction_flag;
  81. int num_reorder_frames;
  82. int scaling_matrix_present;
  83. uint8_t scaling_matrix4[6][16];
  84. uint8_t scaling_matrix8[2][64];
  85. }SPS;
  86. /**
  87. * Picture parameter set
  88. */
  89. typedef struct PPS{
  90. int sps_id;
  91. int cabac; ///< entropy_coding_mode_flag
  92. int pic_order_present; ///< pic_order_present_flag
  93. int slice_group_count; ///< num_slice_groups_minus1 + 1
  94. int mb_slice_group_map_type;
  95. int ref_count[2]; ///< num_ref_idx_l0/1_active_minus1 + 1
  96. int weighted_pred; ///< weighted_pred_flag
  97. int weighted_bipred_idc;
  98. int init_qp; ///< pic_init_qp_minus26 + 26
  99. int init_qs; ///< pic_init_qs_minus26 + 26
  100. int chroma_qp_index_offset;
  101. int deblocking_filter_parameters_present; ///< deblocking_filter_parameters_present_flag
  102. int constrained_intra_pred; ///< constrained_intra_pred_flag
  103. int redundant_pic_cnt_present; ///< redundant_pic_cnt_present_flag
  104. int transform_8x8_mode; ///< transform_8x8_mode_flag
  105. uint8_t scaling_matrix4[6][16];
  106. uint8_t scaling_matrix8[2][64];
  107. }PPS;
  108. /**
  109. * Memory management control operation opcode.
  110. */
  111. typedef enum MMCOOpcode{
  112. MMCO_END=0,
  113. MMCO_SHORT2UNUSED,
  114. MMCO_LONG2UNUSED,
  115. MMCO_SHORT2LONG,
  116. MMCO_SET_MAX_LONG,
  117. MMCO_RESET,
  118. MMCO_LONG,
  119. } MMCOOpcode;
  120. /**
  121. * Memory management control operation.
  122. */
  123. typedef struct MMCO{
  124. MMCOOpcode opcode;
  125. int short_frame_num;
  126. int long_index;
  127. } MMCO;
  128. /**
  129. * H264Context
  130. */
  131. typedef struct H264Context{
  132. MpegEncContext s;
  133. int nal_ref_idc;
  134. int nal_unit_type;
  135. #define NAL_SLICE 1
  136. #define NAL_DPA 2
  137. #define NAL_DPB 3
  138. #define NAL_DPC 4
  139. #define NAL_IDR_SLICE 5
  140. #define NAL_SEI 6
  141. #define NAL_SPS 7
  142. #define NAL_PPS 8
  143. #define NAL_AUD 9
  144. #define NAL_END_SEQUENCE 10
  145. #define NAL_END_STREAM 11
  146. #define NAL_FILLER_DATA 12
  147. #define NAL_SPS_EXT 13
  148. #define NAL_AUXILIARY_SLICE 19
  149. uint8_t *rbsp_buffer;
  150. unsigned int rbsp_buffer_size;
  151. /**
  152. * Used to parse AVC variant of h264
  153. */
  154. int is_avc; ///< this flag is != 0 if codec is avc1
  155. int got_avcC; ///< flag used to parse avcC data only once
  156. int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
  157. int chroma_qp; //QPc
  158. int prev_mb_skipped; //FIXME remove (IMHO not used)
  159. //prediction stuff
  160. int chroma_pred_mode;
  161. int intra16x16_pred_mode;
  162. int top_mb_xy;
  163. int left_mb_xy[2];
  164. int8_t intra4x4_pred_mode_cache[5*8];
  165. int8_t (*intra4x4_pred_mode)[8];
  166. void (*pred4x4 [9+3])(uint8_t *src, uint8_t *topright, int stride);//FIXME move to dsp?
  167. void (*pred8x8l [9+3])(uint8_t *src, int topleft, int topright, int stride);
  168. void (*pred8x8 [4+3])(uint8_t *src, int stride);
  169. void (*pred16x16[4+3])(uint8_t *src, int stride);
  170. unsigned int topleft_samples_available;
  171. unsigned int top_samples_available;
  172. unsigned int topright_samples_available;
  173. unsigned int left_samples_available;
  174. uint8_t (*top_borders[2])[16+2*8];
  175. uint8_t left_border[2*(17+2*9)];
  176. /**
  177. * non zero coeff count cache.
  178. * is 64 if not available.
  179. */
  180. DECLARE_ALIGNED_8(uint8_t, non_zero_count_cache[6*8]);
  181. uint8_t (*non_zero_count)[16];
  182. /**
  183. * Motion vector cache.
  184. */
  185. DECLARE_ALIGNED_8(int16_t, mv_cache[2][5*8][2]);
  186. DECLARE_ALIGNED_8(int8_t, ref_cache[2][5*8]);
  187. #define LIST_NOT_USED -1 //FIXME rename?
  188. #define PART_NOT_AVAILABLE -2
  189. /**
  190. * is 1 if the specific list MV&references are set to 0,0,-2.
  191. */
  192. int mv_cache_clean[2];
  193. /**
  194. * number of neighbors (top and/or left) that used 8x8 dct
  195. */
  196. int neighbor_transform_size;
  197. /**
  198. * block_offset[ 0..23] for frame macroblocks
  199. * block_offset[24..47] for field macroblocks
  200. */
  201. int block_offset[2*(16+8)];
  202. uint32_t *mb2b_xy; //FIXME are these 4 a good idea?
  203. uint32_t *mb2b8_xy;
  204. int b_stride; //FIXME use s->b4_stride
  205. int b8_stride;
  206. int halfpel_flag;
  207. int thirdpel_flag;
  208. int unknown_svq3_flag;
  209. int next_slice_index;
  210. SPS sps_buffer[MAX_SPS_COUNT];
  211. SPS sps; ///< current sps
  212. PPS pps_buffer[MAX_PPS_COUNT];
  213. /**
  214. * current pps
  215. */
  216. PPS pps; //FIXME move to Picture perhaps? (->no) do we need that?
  217. uint32_t dequant4_buffer[6][52][16];
  218. uint32_t dequant8_buffer[2][52][64];
  219. uint32_t (*dequant4_coeff[6])[16];
  220. uint32_t (*dequant8_coeff[2])[64];
  221. int dequant_coeff_pps; ///< reinit tables when pps changes
  222. int slice_num;
  223. uint8_t *slice_table_base;
  224. uint8_t *slice_table; ///< slice_table_base + mb_stride + 1
  225. int slice_type;
  226. int slice_type_fixed;
  227. //interlacing specific flags
  228. int mb_aff_frame;
  229. int mb_field_decoding_flag;
  230. int sub_mb_type[4];
  231. //POC stuff
  232. int poc_lsb;
  233. int poc_msb;
  234. int delta_poc_bottom;
  235. int delta_poc[2];
  236. int frame_num;
  237. int prev_poc_msb; ///< poc_msb of the last reference pic for POC type 0
  238. int prev_poc_lsb; ///< poc_lsb of the last reference pic for POC type 0
  239. int frame_num_offset; ///< for POC type 2
  240. int prev_frame_num_offset; ///< for POC type 2
  241. int prev_frame_num; ///< frame_num of the last pic for POC type 1/2
  242. /**
  243. * frame_num for frames or 2*frame_num for field pics.
  244. */
  245. int curr_pic_num;
  246. /**
  247. * max_frame_num or 2*max_frame_num for field pics.
  248. */
  249. int max_pic_num;
  250. //Weighted pred stuff
  251. int use_weight;
  252. int use_weight_chroma;
  253. int luma_log2_weight_denom;
  254. int chroma_log2_weight_denom;
  255. int luma_weight[2][16];
  256. int luma_offset[2][16];
  257. int chroma_weight[2][16][2];
  258. int chroma_offset[2][16][2];
  259. int implicit_weight[16][16];
  260. //deblock
  261. int deblocking_filter; ///< disable_deblocking_filter_idc with 1<->0
  262. int slice_alpha_c0_offset;
  263. int slice_beta_offset;
  264. int redundant_pic_count;
  265. int direct_spatial_mv_pred;
  266. int dist_scale_factor[16];
  267. int map_col_to_list0[2][16];
  268. /**
  269. * num_ref_idx_l0/1_active_minus1 + 1
  270. */
  271. int ref_count[2];// FIXME split for AFF
  272. Picture *short_ref[32];
  273. Picture *long_ref[32];
  274. Picture default_ref_list[2][32];
  275. Picture ref_list[2][32]; //FIXME size?
  276. Picture field_ref_list[2][32]; //FIXME size?
  277. Picture *delayed_pic[16]; //FIXME size?
  278. Picture *delayed_output_pic;
  279. /**
  280. * memory management control operations buffer.
  281. */
  282. MMCO mmco[MAX_MMCO_COUNT];
  283. int mmco_index;
  284. int long_ref_count; ///< number of actual long term references
  285. int short_ref_count; ///< number of actual short term references
  286. //data partitioning
  287. GetBitContext intra_gb;
  288. GetBitContext inter_gb;
  289. GetBitContext *intra_gb_ptr;
  290. GetBitContext *inter_gb_ptr;
  291. DECLARE_ALIGNED_8(DCTELEM, mb[16*24]);
  292. /**
  293. * Cabac
  294. */
  295. CABACContext cabac;
  296. uint8_t cabac_state[460];
  297. int cabac_init_idc;
  298. /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
  299. uint16_t *cbp_table;
  300. int top_cbp;
  301. int left_cbp;
  302. /* chroma_pred_mode for i4x4 or i16x16, else 0 */
  303. uint8_t *chroma_pred_mode_table;
  304. int last_qscale_diff;
  305. int16_t (*mvd_table[2])[2];
  306. DECLARE_ALIGNED_8(int16_t, mvd_cache[2][5*8][2]);
  307. uint8_t *direct_table;
  308. uint8_t direct_cache[5*8];
  309. uint8_t zigzag_scan[16];
  310. uint8_t field_scan[16];
  311. uint8_t zigzag_scan8x8[64];
  312. uint8_t zigzag_scan8x8_cavlc[64];
  313. const uint8_t *zigzag_scan_q0;
  314. const uint8_t *field_scan_q0;
  315. const uint8_t *zigzag_scan8x8_q0;
  316. const uint8_t *zigzag_scan8x8_cavlc_q0;
  317. int x264_build;
  318. }H264Context;
  319. static VLC coeff_token_vlc[4];
  320. static VLC chroma_dc_coeff_token_vlc;
  321. static VLC total_zeros_vlc[15];
  322. static VLC chroma_dc_total_zeros_vlc[3];
  323. static VLC run_vlc[6];
  324. static VLC run7_vlc;
  325. static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
  326. static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
  327. 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);
  328. static always_inline uint32_t pack16to32(int a, int b){
  329. #ifdef WORDS_BIGENDIAN
  330. return (b&0xFFFF) + (a<<16);
  331. #else
  332. return (a&0xFFFF) + (b<<16);
  333. #endif
  334. }
  335. /**
  336. * fill a rectangle.
  337. * @param h height of the rectangle, should be a constant
  338. * @param w width of the rectangle, should be a constant
  339. * @param size the size of val (1 or 4), should be a constant
  340. */
  341. static always_inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){
  342. uint8_t *p= (uint8_t*)vp;
  343. assert(size==1 || size==4);
  344. assert(w<=4);
  345. w *= size;
  346. stride *= size;
  347. assert((((long)vp)&(FFMIN(w, STRIDE_ALIGN)-1)) == 0);
  348. assert((stride&(w-1))==0);
  349. if(w==2){
  350. const uint16_t v= size==4 ? val : val*0x0101;
  351. *(uint16_t*)(p + 0*stride)= v;
  352. if(h==1) return;
  353. *(uint16_t*)(p + 1*stride)= v;
  354. if(h==2) return;
  355. *(uint16_t*)(p + 2*stride)=
  356. *(uint16_t*)(p + 3*stride)= v;
  357. }else if(w==4){
  358. const uint32_t v= size==4 ? val : val*0x01010101;
  359. *(uint32_t*)(p + 0*stride)= v;
  360. if(h==1) return;
  361. *(uint32_t*)(p + 1*stride)= v;
  362. if(h==2) return;
  363. *(uint32_t*)(p + 2*stride)=
  364. *(uint32_t*)(p + 3*stride)= v;
  365. }else if(w==8){
  366. //gcc can't optimize 64bit math on x86_32
  367. #if defined(ARCH_X86_64) || (defined(MP_WORDSIZE) && MP_WORDSIZE >= 64)
  368. const uint64_t v= val*0x0100000001ULL;
  369. *(uint64_t*)(p + 0*stride)= v;
  370. if(h==1) return;
  371. *(uint64_t*)(p + 1*stride)= v;
  372. if(h==2) return;
  373. *(uint64_t*)(p + 2*stride)=
  374. *(uint64_t*)(p + 3*stride)= v;
  375. }else if(w==16){
  376. const uint64_t v= val*0x0100000001ULL;
  377. *(uint64_t*)(p + 0+0*stride)=
  378. *(uint64_t*)(p + 8+0*stride)=
  379. *(uint64_t*)(p + 0+1*stride)=
  380. *(uint64_t*)(p + 8+1*stride)= v;
  381. if(h==2) return;
  382. *(uint64_t*)(p + 0+2*stride)=
  383. *(uint64_t*)(p + 8+2*stride)=
  384. *(uint64_t*)(p + 0+3*stride)=
  385. *(uint64_t*)(p + 8+3*stride)= v;
  386. #else
  387. *(uint32_t*)(p + 0+0*stride)=
  388. *(uint32_t*)(p + 4+0*stride)= val;
  389. if(h==1) return;
  390. *(uint32_t*)(p + 0+1*stride)=
  391. *(uint32_t*)(p + 4+1*stride)= val;
  392. if(h==2) return;
  393. *(uint32_t*)(p + 0+2*stride)=
  394. *(uint32_t*)(p + 4+2*stride)=
  395. *(uint32_t*)(p + 0+3*stride)=
  396. *(uint32_t*)(p + 4+3*stride)= val;
  397. }else if(w==16){
  398. *(uint32_t*)(p + 0+0*stride)=
  399. *(uint32_t*)(p + 4+0*stride)=
  400. *(uint32_t*)(p + 8+0*stride)=
  401. *(uint32_t*)(p +12+0*stride)=
  402. *(uint32_t*)(p + 0+1*stride)=
  403. *(uint32_t*)(p + 4+1*stride)=
  404. *(uint32_t*)(p + 8+1*stride)=
  405. *(uint32_t*)(p +12+1*stride)= val;
  406. if(h==2) return;
  407. *(uint32_t*)(p + 0+2*stride)=
  408. *(uint32_t*)(p + 4+2*stride)=
  409. *(uint32_t*)(p + 8+2*stride)=
  410. *(uint32_t*)(p +12+2*stride)=
  411. *(uint32_t*)(p + 0+3*stride)=
  412. *(uint32_t*)(p + 4+3*stride)=
  413. *(uint32_t*)(p + 8+3*stride)=
  414. *(uint32_t*)(p +12+3*stride)= val;
  415. #endif
  416. }else
  417. assert(0);
  418. assert(h==4);
  419. }
  420. static void fill_caches(H264Context *h, int mb_type, int for_deblock){
  421. MpegEncContext * const s = &h->s;
  422. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  423. int topleft_xy, top_xy, topright_xy, left_xy[2];
  424. int topleft_type, top_type, topright_type, left_type[2];
  425. int left_block[8];
  426. int i;
  427. //FIXME deblocking can skip fill_caches much of the time with multiple slices too.
  428. // the actual condition is whether we're on the edge of a slice,
  429. // and even then the intra and nnz parts are unnecessary.
  430. if(for_deblock && h->slice_num == 1)
  431. return;
  432. //wow what a mess, why didn't they simplify the interlacing&intra stuff, i can't imagine that these complex rules are worth it
  433. top_xy = mb_xy - s->mb_stride;
  434. topleft_xy = top_xy - 1;
  435. topright_xy= top_xy + 1;
  436. left_xy[1] = left_xy[0] = mb_xy-1;
  437. left_block[0]= 0;
  438. left_block[1]= 1;
  439. left_block[2]= 2;
  440. left_block[3]= 3;
  441. left_block[4]= 7;
  442. left_block[5]= 10;
  443. left_block[6]= 8;
  444. left_block[7]= 11;
  445. if(h->mb_aff_frame){
  446. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  447. const int top_pair_xy = pair_xy - s->mb_stride;
  448. const int topleft_pair_xy = top_pair_xy - 1;
  449. const int topright_pair_xy = top_pair_xy + 1;
  450. const int topleft_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
  451. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  452. const int topright_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
  453. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  454. const int curr_mb_frame_flag = !IS_INTERLACED(mb_type);
  455. const int bottom = (s->mb_y & 1);
  456. 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);
  457. if (bottom
  458. ? !curr_mb_frame_flag // bottom macroblock
  459. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  460. ) {
  461. top_xy -= s->mb_stride;
  462. }
  463. if (bottom
  464. ? !curr_mb_frame_flag // bottom macroblock
  465. : (!curr_mb_frame_flag && !topleft_mb_frame_flag) // top macroblock
  466. ) {
  467. topleft_xy -= s->mb_stride;
  468. }
  469. if (bottom
  470. ? !curr_mb_frame_flag // bottom macroblock
  471. : (!curr_mb_frame_flag && !topright_mb_frame_flag) // top macroblock
  472. ) {
  473. topright_xy -= s->mb_stride;
  474. }
  475. if (left_mb_frame_flag != curr_mb_frame_flag) {
  476. left_xy[1] = left_xy[0] = pair_xy - 1;
  477. if (curr_mb_frame_flag) {
  478. if (bottom) {
  479. left_block[0]= 2;
  480. left_block[1]= 2;
  481. left_block[2]= 3;
  482. left_block[3]= 3;
  483. left_block[4]= 8;
  484. left_block[5]= 11;
  485. left_block[6]= 8;
  486. left_block[7]= 11;
  487. } else {
  488. left_block[0]= 0;
  489. left_block[1]= 0;
  490. left_block[2]= 1;
  491. left_block[3]= 1;
  492. left_block[4]= 7;
  493. left_block[5]= 10;
  494. left_block[6]= 7;
  495. left_block[7]= 10;
  496. }
  497. } else {
  498. left_xy[1] += s->mb_stride;
  499. //left_block[0]= 0;
  500. left_block[1]= 2;
  501. left_block[2]= 0;
  502. left_block[3]= 2;
  503. //left_block[4]= 7;
  504. left_block[5]= 10;
  505. left_block[6]= 7;
  506. left_block[7]= 10;
  507. }
  508. }
  509. }
  510. h->top_mb_xy = top_xy;
  511. h->left_mb_xy[0] = left_xy[0];
  512. h->left_mb_xy[1] = left_xy[1];
  513. if(for_deblock){
  514. topleft_type = h->slice_table[topleft_xy ] < 255 ? s->current_picture.mb_type[topleft_xy] : 0;
  515. top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0;
  516. topright_type= h->slice_table[topright_xy] < 255 ? s->current_picture.mb_type[topright_xy]: 0;
  517. left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
  518. left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
  519. }else{
  520. topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
  521. top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
  522. topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
  523. left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
  524. left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
  525. }
  526. if(IS_INTRA(mb_type)){
  527. h->topleft_samples_available=
  528. h->top_samples_available=
  529. h->left_samples_available= 0xFFFF;
  530. h->topright_samples_available= 0xEEEA;
  531. if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
  532. h->topleft_samples_available= 0xB3FF;
  533. h->top_samples_available= 0x33FF;
  534. h->topright_samples_available= 0x26EA;
  535. }
  536. for(i=0; i<2; i++){
  537. if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
  538. h->topleft_samples_available&= 0xDF5F;
  539. h->left_samples_available&= 0x5F5F;
  540. }
  541. }
  542. if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
  543. h->topleft_samples_available&= 0x7FFF;
  544. if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
  545. h->topright_samples_available&= 0xFBFF;
  546. if(IS_INTRA4x4(mb_type)){
  547. if(IS_INTRA4x4(top_type)){
  548. h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
  549. h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
  550. h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
  551. h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
  552. }else{
  553. int pred;
  554. if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred))
  555. pred= -1;
  556. else{
  557. pred= 2;
  558. }
  559. h->intra4x4_pred_mode_cache[4+8*0]=
  560. h->intra4x4_pred_mode_cache[5+8*0]=
  561. h->intra4x4_pred_mode_cache[6+8*0]=
  562. h->intra4x4_pred_mode_cache[7+8*0]= pred;
  563. }
  564. for(i=0; i<2; i++){
  565. if(IS_INTRA4x4(left_type[i])){
  566. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
  567. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
  568. }else{
  569. int pred;
  570. if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred))
  571. pred= -1;
  572. else{
  573. pred= 2;
  574. }
  575. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
  576. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
  577. }
  578. }
  579. }
  580. }
  581. /*
  582. 0 . T T. T T T T
  583. 1 L . .L . . . .
  584. 2 L . .L . . . .
  585. 3 . T TL . . . .
  586. 4 L . .L . . . .
  587. 5 L . .. . . . .
  588. */
  589. //FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec)
  590. if(top_type){
  591. h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
  592. h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
  593. h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
  594. h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
  595. h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
  596. h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
  597. h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
  598. h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
  599. }else{
  600. h->non_zero_count_cache[4+8*0]=
  601. h->non_zero_count_cache[5+8*0]=
  602. h->non_zero_count_cache[6+8*0]=
  603. h->non_zero_count_cache[7+8*0]=
  604. h->non_zero_count_cache[1+8*0]=
  605. h->non_zero_count_cache[2+8*0]=
  606. h->non_zero_count_cache[1+8*3]=
  607. h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  608. }
  609. for (i=0; i<2; i++) {
  610. if(left_type[i]){
  611. h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
  612. h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
  613. h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
  614. h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
  615. }else{
  616. h->non_zero_count_cache[3+8*1 + 2*8*i]=
  617. h->non_zero_count_cache[3+8*2 + 2*8*i]=
  618. h->non_zero_count_cache[0+8*1 + 8*i]=
  619. h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  620. }
  621. }
  622. if( h->pps.cabac ) {
  623. // top_cbp
  624. if(top_type) {
  625. h->top_cbp = h->cbp_table[top_xy];
  626. } else if(IS_INTRA(mb_type)) {
  627. h->top_cbp = 0x1C0;
  628. } else {
  629. h->top_cbp = 0;
  630. }
  631. // left_cbp
  632. if (left_type[0]) {
  633. h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
  634. } else if(IS_INTRA(mb_type)) {
  635. h->left_cbp = 0x1C0;
  636. } else {
  637. h->left_cbp = 0;
  638. }
  639. if (left_type[0]) {
  640. h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
  641. }
  642. if (left_type[1]) {
  643. h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
  644. }
  645. }
  646. #if 1
  647. //FIXME direct mb can skip much of this
  648. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  649. int list;
  650. for(list=0; list<1+(h->slice_type==B_TYPE); list++){
  651. if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
  652. /*if(!h->mv_cache_clean[list]){
  653. memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
  654. memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
  655. h->mv_cache_clean[list]= 1;
  656. }*/
  657. continue;
  658. }
  659. h->mv_cache_clean[list]= 0;
  660. if(USES_LIST(top_type, list)){
  661. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  662. const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
  663. *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
  664. *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
  665. *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
  666. *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
  667. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  668. h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
  669. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  670. h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
  671. }else{
  672. *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
  673. *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
  674. *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
  675. *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
  676. *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
  677. }
  678. //FIXME unify cleanup or sth
  679. if(USES_LIST(left_type[0], list)){
  680. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  681. const int b8_xy= h->mb2b8_xy[left_xy[0]] + 1;
  682. *(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]];
  683. *(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]];
  684. h->ref_cache[list][scan8[0] - 1 + 0*8]=
  685. h->ref_cache[list][scan8[0] - 1 + 1*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0]>>1)];
  686. }else{
  687. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 0*8]=
  688. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 1*8]= 0;
  689. h->ref_cache[list][scan8[0] - 1 + 0*8]=
  690. h->ref_cache[list][scan8[0] - 1 + 1*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  691. }
  692. if(USES_LIST(left_type[1], list)){
  693. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  694. const int b8_xy= h->mb2b8_xy[left_xy[1]] + 1;
  695. *(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]];
  696. *(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]];
  697. h->ref_cache[list][scan8[0] - 1 + 2*8]=
  698. h->ref_cache[list][scan8[0] - 1 + 3*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[2]>>1)];
  699. }else{
  700. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 2*8]=
  701. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 3*8]= 0;
  702. h->ref_cache[list][scan8[0] - 1 + 2*8]=
  703. h->ref_cache[list][scan8[0] - 1 + 3*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  704. assert((!left_type[0]) == (!left_type[1]));
  705. }
  706. if(for_deblock || (IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred))
  707. continue;
  708. if(USES_LIST(topleft_type, list)){
  709. const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
  710. const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + h->b8_stride;
  711. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  712. h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  713. }else{
  714. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
  715. h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  716. }
  717. if(USES_LIST(topright_type, list)){
  718. const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
  719. const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
  720. *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  721. h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  722. }else{
  723. *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
  724. h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  725. }
  726. h->ref_cache[list][scan8[5 ]+1] =
  727. h->ref_cache[list][scan8[7 ]+1] =
  728. h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewhere else)
  729. h->ref_cache[list][scan8[4 ]] =
  730. h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
  731. *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
  732. *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
  733. *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  734. *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
  735. *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
  736. if( h->pps.cabac ) {
  737. /* XXX beurk, Load mvd */
  738. if(USES_LIST(topleft_type, list)){
  739. const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
  740. *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy];
  741. }else{
  742. *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 - 1*8]= 0;
  743. }
  744. if(USES_LIST(top_type, list)){
  745. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  746. *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
  747. *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
  748. *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
  749. *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
  750. }else{
  751. *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
  752. *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
  753. *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
  754. *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
  755. }
  756. if(USES_LIST(left_type[0], list)){
  757. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  758. *(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]];
  759. *(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]];
  760. }else{
  761. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
  762. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
  763. }
  764. if(USES_LIST(left_type[1], list)){
  765. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  766. *(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]];
  767. *(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]];
  768. }else{
  769. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
  770. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
  771. }
  772. *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
  773. *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
  774. *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  775. *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
  776. *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
  777. if(h->slice_type == B_TYPE){
  778. fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
  779. if(IS_DIRECT(top_type)){
  780. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
  781. }else if(IS_8X8(top_type)){
  782. int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
  783. h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
  784. h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
  785. }else{
  786. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
  787. }
  788. //FIXME interlacing
  789. if(IS_DIRECT(left_type[0])){
  790. h->direct_cache[scan8[0] - 1 + 0*8]=
  791. h->direct_cache[scan8[0] - 1 + 2*8]= 1;
  792. }else if(IS_8X8(left_type[0])){
  793. int b8_xy = h->mb2b8_xy[left_xy[0]] + 1;
  794. h->direct_cache[scan8[0] - 1 + 0*8]= h->direct_table[b8_xy];
  795. h->direct_cache[scan8[0] - 1 + 2*8]= h->direct_table[b8_xy + h->b8_stride];
  796. }else{
  797. h->direct_cache[scan8[0] - 1 + 0*8]=
  798. h->direct_cache[scan8[0] - 1 + 2*8]= 0;
  799. }
  800. }
  801. }
  802. }
  803. }
  804. #endif
  805. h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
  806. }
  807. static inline void write_back_intra_pred_mode(H264Context *h){
  808. MpegEncContext * const s = &h->s;
  809. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  810. h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
  811. h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
  812. h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
  813. h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
  814. h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
  815. h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
  816. h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
  817. }
  818. /**
  819. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  820. */
  821. static inline int check_intra4x4_pred_mode(H264Context *h){
  822. MpegEncContext * const s = &h->s;
  823. static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
  824. static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
  825. int i;
  826. if(!(h->top_samples_available&0x8000)){
  827. for(i=0; i<4; i++){
  828. int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
  829. if(status<0){
  830. 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);
  831. return -1;
  832. } else if(status){
  833. h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
  834. }
  835. }
  836. }
  837. if(!(h->left_samples_available&0x8000)){
  838. for(i=0; i<4; i++){
  839. int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
  840. if(status<0){
  841. 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);
  842. return -1;
  843. } else if(status){
  844. h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
  845. }
  846. }
  847. }
  848. return 0;
  849. } //FIXME cleanup like next
  850. /**
  851. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  852. */
  853. static inline int check_intra_pred_mode(H264Context *h, int mode){
  854. MpegEncContext * const s = &h->s;
  855. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  856. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  857. if(mode < 0 || mode > 6) {
  858. 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);
  859. return -1;
  860. }
  861. if(!(h->top_samples_available&0x8000)){
  862. mode= top[ mode ];
  863. if(mode<0){
  864. 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);
  865. return -1;
  866. }
  867. }
  868. if(!(h->left_samples_available&0x8000)){
  869. mode= left[ mode ];
  870. if(mode<0){
  871. 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);
  872. return -1;
  873. }
  874. }
  875. return mode;
  876. }
  877. /**
  878. * gets the predicted intra4x4 prediction mode.
  879. */
  880. static inline int pred_intra_mode(H264Context *h, int n){
  881. const int index8= scan8[n];
  882. const int left= h->intra4x4_pred_mode_cache[index8 - 1];
  883. const int top = h->intra4x4_pred_mode_cache[index8 - 8];
  884. const int min= FFMIN(left, top);
  885. tprintf("mode:%d %d min:%d\n", left ,top, min);
  886. if(min<0) return DC_PRED;
  887. else return min;
  888. }
  889. static inline void write_back_non_zero_count(H264Context *h){
  890. MpegEncContext * const s = &h->s;
  891. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  892. h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
  893. h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
  894. h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
  895. h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
  896. h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
  897. h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
  898. h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
  899. h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
  900. h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
  901. h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
  902. h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
  903. h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
  904. h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
  905. }
  906. /**
  907. * gets the predicted number of non zero coefficients.
  908. * @param n block index
  909. */
  910. static inline int pred_non_zero_count(H264Context *h, int n){
  911. const int index8= scan8[n];
  912. const int left= h->non_zero_count_cache[index8 - 1];
  913. const int top = h->non_zero_count_cache[index8 - 8];
  914. int i= left + top;
  915. if(i<64) i= (i+1)>>1;
  916. tprintf("pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
  917. return i&31;
  918. }
  919. static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
  920. const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
  921. if(topright_ref != PART_NOT_AVAILABLE){
  922. *C= h->mv_cache[list][ i - 8 + part_width ];
  923. return topright_ref;
  924. }else{
  925. tprintf("topright MV not available\n");
  926. *C= h->mv_cache[list][ i - 8 - 1 ];
  927. return h->ref_cache[list][ i - 8 - 1 ];
  928. }
  929. }
  930. /**
  931. * gets the predicted MV.
  932. * @param n the block index
  933. * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
  934. * @param mx the x component of the predicted motion vector
  935. * @param my the y component of the predicted motion vector
  936. */
  937. static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
  938. const int index8= scan8[n];
  939. const int top_ref= h->ref_cache[list][ index8 - 8 ];
  940. const int left_ref= h->ref_cache[list][ index8 - 1 ];
  941. const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
  942. const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
  943. const int16_t * C;
  944. int diagonal_ref, match_count;
  945. assert(part_width==1 || part_width==2 || part_width==4);
  946. /* mv_cache
  947. B . . A T T T T
  948. U . . L . . , .
  949. U . . L . . . .
  950. U . . L . . , .
  951. . . . L . . . .
  952. */
  953. diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
  954. match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
  955. tprintf("pred_motion match_count=%d\n", match_count);
  956. if(match_count > 1){ //most common
  957. *mx= mid_pred(A[0], B[0], C[0]);
  958. *my= mid_pred(A[1], B[1], C[1]);
  959. }else if(match_count==1){
  960. if(left_ref==ref){
  961. *mx= A[0];
  962. *my= A[1];
  963. }else if(top_ref==ref){
  964. *mx= B[0];
  965. *my= B[1];
  966. }else{
  967. *mx= C[0];
  968. *my= C[1];
  969. }
  970. }else{
  971. if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
  972. *mx= A[0];
  973. *my= A[1];
  974. }else{
  975. *mx= mid_pred(A[0], B[0], C[0]);
  976. *my= mid_pred(A[1], B[1], C[1]);
  977. }
  978. }
  979. 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);
  980. }
  981. /**
  982. * gets the directionally predicted 16x8 MV.
  983. * @param n the block index
  984. * @param mx the x component of the predicted motion vector
  985. * @param my the y component of the predicted motion vector
  986. */
  987. static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  988. if(n==0){
  989. const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
  990. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  991. 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);
  992. if(top_ref == ref){
  993. *mx= B[0];
  994. *my= B[1];
  995. return;
  996. }
  997. }else{
  998. const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
  999. const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
  1000. 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);
  1001. if(left_ref == ref){
  1002. *mx= A[0];
  1003. *my= A[1];
  1004. return;
  1005. }
  1006. }
  1007. //RARE
  1008. pred_motion(h, n, 4, list, ref, mx, my);
  1009. }
  1010. /**
  1011. * gets the directionally predicted 8x16 MV.
  1012. * @param n the block index
  1013. * @param mx the x component of the predicted motion vector
  1014. * @param my the y component of the predicted motion vector
  1015. */
  1016. static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  1017. if(n==0){
  1018. const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
  1019. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  1020. 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);
  1021. if(left_ref == ref){
  1022. *mx= A[0];
  1023. *my= A[1];
  1024. return;
  1025. }
  1026. }else{
  1027. const int16_t * C;
  1028. int diagonal_ref;
  1029. diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
  1030. 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);
  1031. if(diagonal_ref == ref){
  1032. *mx= C[0];
  1033. *my= C[1];
  1034. return;
  1035. }
  1036. }
  1037. //RARE
  1038. pred_motion(h, n, 2, list, ref, mx, my);
  1039. }
  1040. static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
  1041. const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
  1042. const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
  1043. tprintf("pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
  1044. if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
  1045. || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
  1046. || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
  1047. *mx = *my = 0;
  1048. return;
  1049. }
  1050. pred_motion(h, 0, 4, 0, 0, mx, my);
  1051. return;
  1052. }
  1053. static inline void direct_dist_scale_factor(H264Context * const h){
  1054. const int poc = h->s.current_picture_ptr->poc;
  1055. const int poc1 = h->ref_list[1][0].poc;
  1056. int i;
  1057. for(i=0; i<h->ref_count[0]; i++){
  1058. int poc0 = h->ref_list[0][i].poc;
  1059. int td = clip(poc1 - poc0, -128, 127);
  1060. if(td == 0 /* FIXME || pic0 is a long-term ref */){
  1061. h->dist_scale_factor[i] = 256;
  1062. }else{
  1063. int tb = clip(poc - poc0, -128, 127);
  1064. int tx = (16384 + (ABS(td) >> 1)) / td;
  1065. h->dist_scale_factor[i] = clip((tb*tx + 32) >> 6, -1024, 1023);
  1066. }
  1067. }
  1068. }
  1069. static inline void direct_ref_list_init(H264Context * const h){
  1070. MpegEncContext * const s = &h->s;
  1071. Picture * const ref1 = &h->ref_list[1][0];
  1072. Picture * const cur = s->current_picture_ptr;
  1073. int list, i, j;
  1074. if(cur->pict_type == I_TYPE)
  1075. cur->ref_count[0] = 0;
  1076. if(cur->pict_type != B_TYPE)
  1077. cur->ref_count[1] = 0;
  1078. for(list=0; list<2; list++){
  1079. cur->ref_count[list] = h->ref_count[list];
  1080. for(j=0; j<h->ref_count[list]; j++)
  1081. cur->ref_poc[list][j] = h->ref_list[list][j].poc;
  1082. }
  1083. if(cur->pict_type != B_TYPE || h->direct_spatial_mv_pred)
  1084. return;
  1085. for(list=0; list<2; list++){
  1086. for(i=0; i<ref1->ref_count[list]; i++){
  1087. const int poc = ref1->ref_poc[list][i];
  1088. h->map_col_to_list0[list][i] = 0; /* bogus; fills in for missing frames */
  1089. for(j=0; j<h->ref_count[list]; j++)
  1090. if(h->ref_list[list][j].poc == poc){
  1091. h->map_col_to_list0[list][i] = j;
  1092. break;
  1093. }
  1094. }
  1095. }
  1096. }
  1097. static inline void pred_direct_motion(H264Context * const h, int *mb_type){
  1098. MpegEncContext * const s = &h->s;
  1099. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  1100. const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1101. const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1102. const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
  1103. const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
  1104. const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[1][b4_xy];
  1105. const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
  1106. const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy];
  1107. const int is_b8x8 = IS_8X8(*mb_type);
  1108. int sub_mb_type;
  1109. int i8, i4;
  1110. if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
  1111. /* FIXME save sub mb types from previous frames (or derive from MVs)
  1112. * so we know exactly what block size to use */
  1113. sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  1114. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  1115. }else if(!is_b8x8 && (IS_16X16(mb_type_col) || IS_INTRA(mb_type_col))){
  1116. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  1117. *mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
  1118. }else{
  1119. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  1120. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  1121. }
  1122. if(!is_b8x8)
  1123. *mb_type |= MB_TYPE_DIRECT2;
  1124. 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);
  1125. if(h->direct_spatial_mv_pred){
  1126. int ref[2];
  1127. int mv[2][2];
  1128. int list;
  1129. /* ref = min(neighbors) */
  1130. for(list=0; list<2; list++){
  1131. int refa = h->ref_cache[list][scan8[0] - 1];
  1132. int refb = h->ref_cache[list][scan8[0] - 8];
  1133. int refc = h->ref_cache[list][scan8[0] - 8 + 4];
  1134. if(refc == -2)
  1135. refc = h->ref_cache[list][scan8[0] - 8 - 1];
  1136. ref[list] = refa;
  1137. if(ref[list] < 0 || (refb < ref[list] && refb >= 0))
  1138. ref[list] = refb;
  1139. if(ref[list] < 0 || (refc < ref[list] && refc >= 0))
  1140. ref[list] = refc;
  1141. if(ref[list] < 0)
  1142. ref[list] = -1;
  1143. }
  1144. if(ref[0] < 0 && ref[1] < 0){
  1145. ref[0] = ref[1] = 0;
  1146. mv[0][0] = mv[0][1] =
  1147. mv[1][0] = mv[1][1] = 0;
  1148. }else{
  1149. for(list=0; list<2; list++){
  1150. if(ref[list] >= 0)
  1151. pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
  1152. else
  1153. mv[list][0] = mv[list][1] = 0;
  1154. }
  1155. }
  1156. if(ref[1] < 0){
  1157. *mb_type &= ~MB_TYPE_P0L1;
  1158. sub_mb_type &= ~MB_TYPE_P0L1;
  1159. }else if(ref[0] < 0){
  1160. *mb_type &= ~MB_TYPE_P0L0;
  1161. sub_mb_type &= ~MB_TYPE_P0L0;
  1162. }
  1163. if(IS_16X16(*mb_type)){
  1164. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  1165. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  1166. if(!IS_INTRA(mb_type_col)
  1167. && ( (l1ref0[0] == 0 && ABS(l1mv0[0][0]) <= 1 && ABS(l1mv0[0][1]) <= 1)
  1168. || (l1ref0[0] < 0 && l1ref1[0] == 0 && ABS(l1mv1[0][0]) <= 1 && ABS(l1mv1[0][1]) <= 1
  1169. && (h->x264_build>33 || !h->x264_build)))){
  1170. if(ref[0] > 0)
  1171. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1172. else
  1173. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  1174. if(ref[1] > 0)
  1175. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1176. else
  1177. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  1178. }else{
  1179. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1180. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1181. }
  1182. }else{
  1183. for(i8=0; i8<4; i8++){
  1184. const int x8 = i8&1;
  1185. const int y8 = i8>>1;
  1186. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1187. continue;
  1188. h->sub_mb_type[i8] = sub_mb_type;
  1189. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1190. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1191. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  1192. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  1193. /* col_zero_flag */
  1194. if(!IS_INTRA(mb_type_col) && ( l1ref0[x8 + y8*h->b8_stride] == 0
  1195. || (l1ref0[x8 + y8*h->b8_stride] < 0 && l1ref1[x8 + y8*h->b8_stride] == 0
  1196. && (h->x264_build>33 || !h->x264_build)))){
  1197. const int16_t (*l1mv)[2]= l1ref0[x8 + y8*h->b8_stride] == 0 ? l1mv0 : l1mv1;
  1198. if(IS_SUB_8X8(sub_mb_type)){
  1199. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1200. if(ABS(mv_col[0]) <= 1 && ABS(mv_col[1]) <= 1){
  1201. if(ref[0] == 0)
  1202. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1203. if(ref[1] == 0)
  1204. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1205. }
  1206. }else
  1207. for(i4=0; i4<4; i4++){
  1208. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1209. if(ABS(mv_col[0]) <= 1 && ABS(mv_col[1]) <= 1){
  1210. if(ref[0] == 0)
  1211. *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
  1212. if(ref[1] == 0)
  1213. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
  1214. }
  1215. }
  1216. }
  1217. }
  1218. }
  1219. }else{ /* direct temporal mv pred */
  1220. if(IS_16X16(*mb_type)){
  1221. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  1222. if(IS_INTRA(mb_type_col)){
  1223. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  1224. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  1225. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  1226. }else{
  1227. const int ref0 = l1ref0[0] >= 0 ? h->map_col_to_list0[0][l1ref0[0]]
  1228. : h->map_col_to_list0[1][l1ref1[0]];
  1229. const int dist_scale_factor = h->dist_scale_factor[ref0];
  1230. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  1231. int mv_l0[2];
  1232. mv_l0[0] = (dist_scale_factor * mv_col[0] + 128) >> 8;
  1233. mv_l0[1] = (dist_scale_factor * mv_col[1] + 128) >> 8;
  1234. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref0, 1);
  1235. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0],mv_l0[1]), 4);
  1236. 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);
  1237. }
  1238. }else{
  1239. for(i8=0; i8<4; i8++){
  1240. const int x8 = i8&1;
  1241. const int y8 = i8>>1;
  1242. int ref0, dist_scale_factor;
  1243. const int16_t (*l1mv)[2]= l1mv0;
  1244. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1245. continue;
  1246. h->sub_mb_type[i8] = sub_mb_type;
  1247. if(IS_INTRA(mb_type_col)){
  1248. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1249. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1250. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1251. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1252. continue;
  1253. }
  1254. ref0 = l1ref0[x8 + y8*h->b8_stride];
  1255. if(ref0 >= 0)
  1256. ref0 = h->map_col_to_list0[0][ref0];
  1257. else{
  1258. ref0 = h->map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]];
  1259. l1mv= l1mv1;
  1260. }
  1261. dist_scale_factor = h->dist_scale_factor[ref0];
  1262. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1263. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1264. if(IS_SUB_8X8(sub_mb_type)){
  1265. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1266. int mx = (dist_scale_factor * mv_col[0] + 128) >> 8;
  1267. int my = (dist_scale_factor * mv_col[1] + 128) >> 8;
  1268. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1269. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
  1270. }else
  1271. for(i4=0; i4<4; i4++){
  1272. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1273. int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
  1274. mv_l0[0] = (dist_scale_factor * mv_col[0] + 128) >> 8;
  1275. mv_l0[1] = (dist_scale_factor * mv_col[1] + 128) >> 8;
  1276. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
  1277. pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  1278. }
  1279. }
  1280. }
  1281. }
  1282. }
  1283. static inline void write_back_motion(H264Context *h, int mb_type){
  1284. MpegEncContext * const s = &h->s;
  1285. const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1286. const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1287. int list;
  1288. if(!USES_LIST(mb_type, 0))
  1289. fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, h->b8_stride, (uint8_t)LIST_NOT_USED, 1);
  1290. for(list=0; list<2; list++){
  1291. int y;
  1292. if(!USES_LIST(mb_type, list))
  1293. continue;
  1294. for(y=0; y<4; y++){
  1295. *(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];
  1296. *(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];
  1297. }
  1298. if( h->pps.cabac ) {
  1299. for(y=0; y<4; y++){
  1300. *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
  1301. *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
  1302. }
  1303. }
  1304. {
  1305. uint8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
  1306. ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]];
  1307. ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]];
  1308. ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]];
  1309. ref_index[1+1*h->b8_stride]= h->ref_cache[list][scan8[12]];
  1310. }
  1311. }
  1312. if(h->slice_type == B_TYPE && h->pps.cabac){
  1313. if(IS_8X8(mb_type)){
  1314. uint8_t *direct_table = &h->direct_table[b8_xy];
  1315. direct_table[1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
  1316. direct_table[0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
  1317. direct_table[1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
  1318. }
  1319. }
  1320. }
  1321. /**
  1322. * Decodes a network abstraction layer unit.
  1323. * @param consumed is the number of bytes used as input
  1324. * @param length is the length of the array
  1325. * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp tailing?
  1326. * @returns decoded bytes, might be src+1 if no escapes
  1327. */
  1328. static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){
  1329. int i, si, di;
  1330. uint8_t *dst;
  1331. // src[0]&0x80; //forbidden bit
  1332. h->nal_ref_idc= src[0]>>5;
  1333. h->nal_unit_type= src[0]&0x1F;
  1334. src++; length--;
  1335. #if 0
  1336. for(i=0; i<length; i++)
  1337. printf("%2X ", src[i]);
  1338. #endif
  1339. for(i=0; i+1<length; i+=2){
  1340. if(src[i]) continue;
  1341. if(i>0 && src[i-1]==0) i--;
  1342. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  1343. if(src[i+2]!=3){
  1344. /* startcode, so we must be past the end */
  1345. length=i;
  1346. }
  1347. break;
  1348. }
  1349. }
  1350. if(i>=length-1){ //no escaped 0
  1351. *dst_length= length;
  1352. *consumed= length+1; //+1 for the header
  1353. return src;
  1354. }
  1355. h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length);
  1356. dst= h->rbsp_buffer;
  1357. //printf("decoding esc\n");
  1358. si=di=0;
  1359. while(si<length){
  1360. //remove escapes (very rare 1:2^22)
  1361. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  1362. if(src[si+2]==3){ //escape
  1363. dst[di++]= 0;
  1364. dst[di++]= 0;
  1365. si+=3;
  1366. continue;
  1367. }else //next start code
  1368. break;
  1369. }
  1370. dst[di++]= src[si++];
  1371. }
  1372. *dst_length= di;
  1373. *consumed= si + 1;//+1 for the header
  1374. //FIXME store exact number of bits in the getbitcontext (its needed for decoding)
  1375. return dst;
  1376. }
  1377. #if 0
  1378. /**
  1379. * @param src the data which should be escaped
  1380. * @param dst the target buffer, dst+1 == src is allowed as a special case
  1381. * @param length the length of the src data
  1382. * @param dst_length the length of the dst array
  1383. * @returns length of escaped data in bytes or -1 if an error occured
  1384. */
  1385. static int encode_nal(H264Context *h, uint8_t *dst, uint8_t *src, int length, int dst_length){
  1386. int i, escape_count, si, di;
  1387. uint8_t *temp;
  1388. assert(length>=0);
  1389. assert(dst_length>0);
  1390. dst[0]= (h->nal_ref_idc<<5) + h->nal_unit_type;
  1391. if(length==0) return 1;
  1392. escape_count= 0;
  1393. for(i=0; i<length; i+=2){
  1394. if(src[i]) continue;
  1395. if(i>0 && src[i-1]==0)
  1396. i--;
  1397. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  1398. escape_count++;
  1399. i+=2;
  1400. }
  1401. }
  1402. if(escape_count==0){
  1403. if(dst+1 != src)
  1404. memcpy(dst+1, src, length);
  1405. return length + 1;
  1406. }
  1407. if(length + escape_count + 1> dst_length)
  1408. return -1;
  1409. //this should be damn rare (hopefully)
  1410. h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length + escape_count);
  1411. temp= h->rbsp_buffer;
  1412. //printf("encoding esc\n");
  1413. si= 0;
  1414. di= 0;
  1415. while(si < length){
  1416. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  1417. temp[di++]= 0; si++;
  1418. temp[di++]= 0; si++;
  1419. temp[di++]= 3;
  1420. temp[di++]= src[si++];
  1421. }
  1422. else
  1423. temp[di++]= src[si++];
  1424. }
  1425. memcpy(dst+1, temp, length+escape_count);
  1426. assert(di == length+escape_count);
  1427. return di + 1;
  1428. }
  1429. /**
  1430. * write 1,10,100,1000,... for alignment, yes its exactly inverse to mpeg4
  1431. */
  1432. static void encode_rbsp_trailing(PutBitContext *pb){
  1433. int length;
  1434. put_bits(pb, 1, 1);
  1435. length= (-put_bits_count(pb))&7;
  1436. if(length) put_bits(pb, length, 0);
  1437. }
  1438. #endif
  1439. /**
  1440. * identifies the exact end of the bitstream
  1441. * @return the length of the trailing, or 0 if damaged
  1442. */
  1443. static int decode_rbsp_trailing(uint8_t *src){
  1444. int v= *src;
  1445. int r;
  1446. tprintf("rbsp trailing %X\n", v);
  1447. for(r=1; r<9; r++){
  1448. if(v&1) return r;
  1449. v>>=1;
  1450. }
  1451. return 0;
  1452. }
  1453. /**
  1454. * idct tranforms the 16 dc values and dequantize them.
  1455. * @param qp quantization parameter
  1456. */
  1457. static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1458. #define stride 16
  1459. int i;
  1460. int temp[16]; //FIXME check if this is a good idea
  1461. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1462. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1463. //memset(block, 64, 2*256);
  1464. //return;
  1465. for(i=0; i<4; i++){
  1466. const int offset= y_offset[i];
  1467. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1468. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1469. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1470. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1471. temp[4*i+0]= z0+z3;
  1472. temp[4*i+1]= z1+z2;
  1473. temp[4*i+2]= z1-z2;
  1474. temp[4*i+3]= z0-z3;
  1475. }
  1476. for(i=0; i<4; i++){
  1477. const int offset= x_offset[i];
  1478. const int z0= temp[4*0+i] + temp[4*2+i];
  1479. const int z1= temp[4*0+i] - temp[4*2+i];
  1480. const int z2= temp[4*1+i] - temp[4*3+i];
  1481. const int z3= temp[4*1+i] + temp[4*3+i];
  1482. block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_resdual
  1483. block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
  1484. block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
  1485. block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
  1486. }
  1487. }
  1488. #if 0
  1489. /**
  1490. * dct tranforms the 16 dc values.
  1491. * @param qp quantization parameter ??? FIXME
  1492. */
  1493. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  1494. // const int qmul= dequant_coeff[qp][0];
  1495. int i;
  1496. int temp[16]; //FIXME check if this is a good idea
  1497. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1498. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1499. for(i=0; i<4; i++){
  1500. const int offset= y_offset[i];
  1501. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1502. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1503. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1504. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1505. temp[4*i+0]= z0+z3;
  1506. temp[4*i+1]= z1+z2;
  1507. temp[4*i+2]= z1-z2;
  1508. temp[4*i+3]= z0-z3;
  1509. }
  1510. for(i=0; i<4; i++){
  1511. const int offset= x_offset[i];
  1512. const int z0= temp[4*0+i] + temp[4*2+i];
  1513. const int z1= temp[4*0+i] - temp[4*2+i];
  1514. const int z2= temp[4*1+i] - temp[4*3+i];
  1515. const int z3= temp[4*1+i] + temp[4*3+i];
  1516. block[stride*0 +offset]= (z0 + z3)>>1;
  1517. block[stride*2 +offset]= (z1 + z2)>>1;
  1518. block[stride*8 +offset]= (z1 - z2)>>1;
  1519. block[stride*10+offset]= (z0 - z3)>>1;
  1520. }
  1521. }
  1522. #endif
  1523. #undef xStride
  1524. #undef stride
  1525. static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1526. const int stride= 16*2;
  1527. const int xStride= 16;
  1528. int a,b,c,d,e;
  1529. a= block[stride*0 + xStride*0];
  1530. b= block[stride*0 + xStride*1];
  1531. c= block[stride*1 + xStride*0];
  1532. d= block[stride*1 + xStride*1];
  1533. e= a-b;
  1534. a= a+b;
  1535. b= c-d;
  1536. c= c+d;
  1537. block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
  1538. block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
  1539. block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
  1540. block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
  1541. }
  1542. #if 0
  1543. static void chroma_dc_dct_c(DCTELEM *block){
  1544. const int stride= 16*2;
  1545. const int xStride= 16;
  1546. int a,b,c,d,e;
  1547. a= block[stride*0 + xStride*0];
  1548. b= block[stride*0 + xStride*1];
  1549. c= block[stride*1 + xStride*0];
  1550. d= block[stride*1 + xStride*1];
  1551. e= a-b;
  1552. a= a+b;
  1553. b= c-d;
  1554. c= c+d;
  1555. block[stride*0 + xStride*0]= (a+c);
  1556. block[stride*0 + xStride*1]= (e+b);
  1557. block[stride*1 + xStride*0]= (a-c);
  1558. block[stride*1 + xStride*1]= (e-b);
  1559. }
  1560. #endif
  1561. /**
  1562. * gets the chroma qp.
  1563. */
  1564. static inline int get_chroma_qp(int chroma_qp_index_offset, int qscale){
  1565. return chroma_qp[clip(qscale + chroma_qp_index_offset, 0, 51)];
  1566. }
  1567. #if 0
  1568. static void h264_diff_dct_c(DCTELEM *block, uint8_t *src1, uint8_t *src2, int stride){
  1569. int i;
  1570. //FIXME try int temp instead of block
  1571. for(i=0; i<4; i++){
  1572. const int d0= src1[0 + i*stride] - src2[0 + i*stride];
  1573. const int d1= src1[1 + i*stride] - src2[1 + i*stride];
  1574. const int d2= src1[2 + i*stride] - src2[2 + i*stride];
  1575. const int d3= src1[3 + i*stride] - src2[3 + i*stride];
  1576. const int z0= d0 + d3;
  1577. const int z3= d0 - d3;
  1578. const int z1= d1 + d2;
  1579. const int z2= d1 - d2;
  1580. block[0 + 4*i]= z0 + z1;
  1581. block[1 + 4*i]= 2*z3 + z2;
  1582. block[2 + 4*i]= z0 - z1;
  1583. block[3 + 4*i]= z3 - 2*z2;
  1584. }
  1585. for(i=0; i<4; i++){
  1586. const int z0= block[0*4 + i] + block[3*4 + i];
  1587. const int z3= block[0*4 + i] - block[3*4 + i];
  1588. const int z1= block[1*4 + i] + block[2*4 + i];
  1589. const int z2= block[1*4 + i] - block[2*4 + i];
  1590. block[0*4 + i]= z0 + z1;
  1591. block[1*4 + i]= 2*z3 + z2;
  1592. block[2*4 + i]= z0 - z1;
  1593. block[3*4 + i]= z3 - 2*z2;
  1594. }
  1595. }
  1596. #endif
  1597. //FIXME need to check that this doesnt overflow signed 32 bit for low qp, i am not sure, it's very close
  1598. //FIXME check that gcc inlines this (and optimizes intra & seperate_dc stuff away)
  1599. static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int seperate_dc){
  1600. int i;
  1601. const int * const quant_table= quant_coeff[qscale];
  1602. const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
  1603. const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
  1604. const unsigned int threshold2= (threshold1<<1);
  1605. int last_non_zero;
  1606. if(seperate_dc){
  1607. if(qscale<=18){
  1608. //avoid overflows
  1609. const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
  1610. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
  1611. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1612. int level= block[0]*quant_coeff[qscale+18][0];
  1613. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1614. if(level>0){
  1615. level= (dc_bias + level)>>(QUANT_SHIFT-2);
  1616. block[0]= level;
  1617. }else{
  1618. level= (dc_bias - level)>>(QUANT_SHIFT-2);
  1619. block[0]= -level;
  1620. }
  1621. // last_non_zero = i;
  1622. }else{
  1623. block[0]=0;
  1624. }
  1625. }else{
  1626. const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
  1627. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
  1628. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1629. int level= block[0]*quant_table[0];
  1630. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1631. if(level>0){
  1632. level= (dc_bias + level)>>(QUANT_SHIFT+1);
  1633. block[0]= level;
  1634. }else{
  1635. level= (dc_bias - level)>>(QUANT_SHIFT+1);
  1636. block[0]= -level;
  1637. }
  1638. // last_non_zero = i;
  1639. }else{
  1640. block[0]=0;
  1641. }
  1642. }
  1643. last_non_zero= 0;
  1644. i=1;
  1645. }else{
  1646. last_non_zero= -1;
  1647. i=0;
  1648. }
  1649. for(; i<16; i++){
  1650. const int j= scantable[i];
  1651. int level= block[j]*quant_table[j];
  1652. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  1653. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  1654. if(((unsigned)(level+threshold1))>threshold2){
  1655. if(level>0){
  1656. level= (bias + level)>>QUANT_SHIFT;
  1657. block[j]= level;
  1658. }else{
  1659. level= (bias - level)>>QUANT_SHIFT;
  1660. block[j]= -level;
  1661. }
  1662. last_non_zero = i;
  1663. }else{
  1664. block[j]=0;
  1665. }
  1666. }
  1667. return last_non_zero;
  1668. }
  1669. static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){
  1670. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1671. ((uint32_t*)(src+0*stride))[0]= a;
  1672. ((uint32_t*)(src+1*stride))[0]= a;
  1673. ((uint32_t*)(src+2*stride))[0]= a;
  1674. ((uint32_t*)(src+3*stride))[0]= a;
  1675. }
  1676. static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){
  1677. ((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101;
  1678. ((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101;
  1679. ((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101;
  1680. ((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101;
  1681. }
  1682. static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1683. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
  1684. + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
  1685. ((uint32_t*)(src+0*stride))[0]=
  1686. ((uint32_t*)(src+1*stride))[0]=
  1687. ((uint32_t*)(src+2*stride))[0]=
  1688. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1689. }
  1690. static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1691. const int dc= ( src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
  1692. ((uint32_t*)(src+0*stride))[0]=
  1693. ((uint32_t*)(src+1*stride))[0]=
  1694. ((uint32_t*)(src+2*stride))[0]=
  1695. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1696. }
  1697. static void pred4x4_top_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1698. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2;
  1699. ((uint32_t*)(src+0*stride))[0]=
  1700. ((uint32_t*)(src+1*stride))[0]=
  1701. ((uint32_t*)(src+2*stride))[0]=
  1702. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1703. }
  1704. static void pred4x4_128_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1705. ((uint32_t*)(src+0*stride))[0]=
  1706. ((uint32_t*)(src+1*stride))[0]=
  1707. ((uint32_t*)(src+2*stride))[0]=
  1708. ((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U;
  1709. }
  1710. #define LOAD_TOP_RIGHT_EDGE\
  1711. const int t4= topright[0];\
  1712. const int t5= topright[1];\
  1713. const int t6= topright[2];\
  1714. const int t7= topright[3];\
  1715. #define LOAD_LEFT_EDGE\
  1716. const int l0= src[-1+0*stride];\
  1717. const int l1= src[-1+1*stride];\
  1718. const int l2= src[-1+2*stride];\
  1719. const int l3= src[-1+3*stride];\
  1720. #define LOAD_TOP_EDGE\
  1721. const int t0= src[ 0-1*stride];\
  1722. const int t1= src[ 1-1*stride];\
  1723. const int t2= src[ 2-1*stride];\
  1724. const int t3= src[ 3-1*stride];\
  1725. static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){
  1726. const int lt= src[-1-1*stride];
  1727. LOAD_TOP_EDGE
  1728. LOAD_LEFT_EDGE
  1729. src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
  1730. src[0+2*stride]=
  1731. src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
  1732. src[0+1*stride]=
  1733. src[1+2*stride]=
  1734. src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
  1735. src[0+0*stride]=
  1736. src[1+1*stride]=
  1737. src[2+2*stride]=
  1738. src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1739. src[1+0*stride]=
  1740. src[2+1*stride]=
  1741. src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1742. src[2+0*stride]=
  1743. src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1744. src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1745. }
  1746. static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){
  1747. LOAD_TOP_EDGE
  1748. LOAD_TOP_RIGHT_EDGE
  1749. // LOAD_LEFT_EDGE
  1750. src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
  1751. src[1+0*stride]=
  1752. src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
  1753. src[2+0*stride]=
  1754. src[1+1*stride]=
  1755. src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
  1756. src[3+0*stride]=
  1757. src[2+1*stride]=
  1758. src[1+2*stride]=
  1759. src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
  1760. src[3+1*stride]=
  1761. src[2+2*stride]=
  1762. src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
  1763. src[3+2*stride]=
  1764. src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
  1765. src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
  1766. }
  1767. static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
  1768. const int lt= src[-1-1*stride];
  1769. LOAD_TOP_EDGE
  1770. LOAD_LEFT_EDGE
  1771. const __attribute__((unused)) int unu= l3;
  1772. src[0+0*stride]=
  1773. src[1+2*stride]=(lt + t0 + 1)>>1;
  1774. src[1+0*stride]=
  1775. src[2+2*stride]=(t0 + t1 + 1)>>1;
  1776. src[2+0*stride]=
  1777. src[3+2*stride]=(t1 + t2 + 1)>>1;
  1778. src[3+0*stride]=(t2 + t3 + 1)>>1;
  1779. src[0+1*stride]=
  1780. src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1781. src[1+1*stride]=
  1782. src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1783. src[2+1*stride]=
  1784. src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1785. src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1786. src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1787. src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1788. }
  1789. static void pred4x4_vertical_left_c(uint8_t *src, uint8_t *topright, int stride){
  1790. LOAD_TOP_EDGE
  1791. LOAD_TOP_RIGHT_EDGE
  1792. const __attribute__((unused)) int unu= t7;
  1793. src[0+0*stride]=(t0 + t1 + 1)>>1;
  1794. src[1+0*stride]=
  1795. src[0+2*stride]=(t1 + t2 + 1)>>1;
  1796. src[2+0*stride]=
  1797. src[1+2*stride]=(t2 + t3 + 1)>>1;
  1798. src[3+0*stride]=
  1799. src[2+2*stride]=(t3 + t4+ 1)>>1;
  1800. src[3+2*stride]=(t4 + t5+ 1)>>1;
  1801. src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1802. src[1+1*stride]=
  1803. src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1804. src[2+1*stride]=
  1805. src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
  1806. src[3+1*stride]=
  1807. src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
  1808. src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
  1809. }
  1810. static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){
  1811. LOAD_LEFT_EDGE
  1812. src[0+0*stride]=(l0 + l1 + 1)>>1;
  1813. src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1814. src[2+0*stride]=
  1815. src[0+1*stride]=(l1 + l2 + 1)>>1;
  1816. src[3+0*stride]=
  1817. src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1818. src[2+1*stride]=
  1819. src[0+2*stride]=(l2 + l3 + 1)>>1;
  1820. src[3+1*stride]=
  1821. src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
  1822. src[3+2*stride]=
  1823. src[1+3*stride]=
  1824. src[0+3*stride]=
  1825. src[2+2*stride]=
  1826. src[2+3*stride]=
  1827. src[3+3*stride]=l3;
  1828. }
  1829. static void pred4x4_horizontal_down_c(uint8_t *src, uint8_t *topright, int stride){
  1830. const int lt= src[-1-1*stride];
  1831. LOAD_TOP_EDGE
  1832. LOAD_LEFT_EDGE
  1833. const __attribute__((unused)) int unu= t3;
  1834. src[0+0*stride]=
  1835. src[2+1*stride]=(lt + l0 + 1)>>1;
  1836. src[1+0*stride]=
  1837. src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1838. src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1839. src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1840. src[0+1*stride]=
  1841. src[2+2*stride]=(l0 + l1 + 1)>>1;
  1842. src[1+1*stride]=
  1843. src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1844. src[0+2*stride]=
  1845. src[2+3*stride]=(l1 + l2+ 1)>>1;
  1846. src[1+2*stride]=
  1847. src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1848. src[0+3*stride]=(l2 + l3 + 1)>>1;
  1849. src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1850. }
  1851. static void pred16x16_vertical_c(uint8_t *src, int stride){
  1852. int i;
  1853. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1854. const uint32_t b= ((uint32_t*)(src-stride))[1];
  1855. const uint32_t c= ((uint32_t*)(src-stride))[2];
  1856. const uint32_t d= ((uint32_t*)(src-stride))[3];
  1857. for(i=0; i<16; i++){
  1858. ((uint32_t*)(src+i*stride))[0]= a;
  1859. ((uint32_t*)(src+i*stride))[1]= b;
  1860. ((uint32_t*)(src+i*stride))[2]= c;
  1861. ((uint32_t*)(src+i*stride))[3]= d;
  1862. }
  1863. }
  1864. static void pred16x16_horizontal_c(uint8_t *src, int stride){
  1865. int i;
  1866. for(i=0; i<16; i++){
  1867. ((uint32_t*)(src+i*stride))[0]=
  1868. ((uint32_t*)(src+i*stride))[1]=
  1869. ((uint32_t*)(src+i*stride))[2]=
  1870. ((uint32_t*)(src+i*stride))[3]= src[-1+i*stride]*0x01010101;
  1871. }
  1872. }
  1873. static void pred16x16_dc_c(uint8_t *src, int stride){
  1874. int i, dc=0;
  1875. for(i=0;i<16; i++){
  1876. dc+= src[-1+i*stride];
  1877. }
  1878. for(i=0;i<16; i++){
  1879. dc+= src[i-stride];
  1880. }
  1881. dc= 0x01010101*((dc + 16)>>5);
  1882. for(i=0; i<16; i++){
  1883. ((uint32_t*)(src+i*stride))[0]=
  1884. ((uint32_t*)(src+i*stride))[1]=
  1885. ((uint32_t*)(src+i*stride))[2]=
  1886. ((uint32_t*)(src+i*stride))[3]= dc;
  1887. }
  1888. }
  1889. static void pred16x16_left_dc_c(uint8_t *src, int stride){
  1890. int i, dc=0;
  1891. for(i=0;i<16; i++){
  1892. dc+= src[-1+i*stride];
  1893. }
  1894. dc= 0x01010101*((dc + 8)>>4);
  1895. for(i=0; i<16; i++){
  1896. ((uint32_t*)(src+i*stride))[0]=
  1897. ((uint32_t*)(src+i*stride))[1]=
  1898. ((uint32_t*)(src+i*stride))[2]=
  1899. ((uint32_t*)(src+i*stride))[3]= dc;
  1900. }
  1901. }
  1902. static void pred16x16_top_dc_c(uint8_t *src, int stride){
  1903. int i, dc=0;
  1904. for(i=0;i<16; i++){
  1905. dc+= src[i-stride];
  1906. }
  1907. dc= 0x01010101*((dc + 8)>>4);
  1908. for(i=0; i<16; i++){
  1909. ((uint32_t*)(src+i*stride))[0]=
  1910. ((uint32_t*)(src+i*stride))[1]=
  1911. ((uint32_t*)(src+i*stride))[2]=
  1912. ((uint32_t*)(src+i*stride))[3]= dc;
  1913. }
  1914. }
  1915. static void pred16x16_128_dc_c(uint8_t *src, int stride){
  1916. int i;
  1917. for(i=0; i<16; i++){
  1918. ((uint32_t*)(src+i*stride))[0]=
  1919. ((uint32_t*)(src+i*stride))[1]=
  1920. ((uint32_t*)(src+i*stride))[2]=
  1921. ((uint32_t*)(src+i*stride))[3]= 0x01010101U*128U;
  1922. }
  1923. }
  1924. static inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){
  1925. int i, j, k;
  1926. int a;
  1927. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  1928. const uint8_t * const src0 = src+7-stride;
  1929. const uint8_t *src1 = src+8*stride-1;
  1930. const uint8_t *src2 = src1-2*stride; // == src+6*stride-1;
  1931. int H = src0[1] - src0[-1];
  1932. int V = src1[0] - src2[ 0];
  1933. for(k=2; k<=8; ++k) {
  1934. src1 += stride; src2 -= stride;
  1935. H += k*(src0[k] - src0[-k]);
  1936. V += k*(src1[0] - src2[ 0]);
  1937. }
  1938. if(svq3){
  1939. H = ( 5*(H/4) ) / 16;
  1940. V = ( 5*(V/4) ) / 16;
  1941. /* required for 100% accuracy */
  1942. i = H; H = V; V = i;
  1943. }else{
  1944. H = ( 5*H+32 ) >> 6;
  1945. V = ( 5*V+32 ) >> 6;
  1946. }
  1947. a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
  1948. for(j=16; j>0; --j) {
  1949. int b = a;
  1950. a += V;
  1951. for(i=-16; i<0; i+=4) {
  1952. src[16+i] = cm[ (b ) >> 5 ];
  1953. src[17+i] = cm[ (b+ H) >> 5 ];
  1954. src[18+i] = cm[ (b+2*H) >> 5 ];
  1955. src[19+i] = cm[ (b+3*H) >> 5 ];
  1956. b += 4*H;
  1957. }
  1958. src += stride;
  1959. }
  1960. }
  1961. static void pred16x16_plane_c(uint8_t *src, int stride){
  1962. pred16x16_plane_compat_c(src, stride, 0);
  1963. }
  1964. static void pred8x8_vertical_c(uint8_t *src, int stride){
  1965. int i;
  1966. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1967. const uint32_t b= ((uint32_t*)(src-stride))[1];
  1968. for(i=0; i<8; i++){
  1969. ((uint32_t*)(src+i*stride))[0]= a;
  1970. ((uint32_t*)(src+i*stride))[1]= b;
  1971. }
  1972. }
  1973. static void pred8x8_horizontal_c(uint8_t *src, int stride){
  1974. int i;
  1975. for(i=0; i<8; i++){
  1976. ((uint32_t*)(src+i*stride))[0]=
  1977. ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101;
  1978. }
  1979. }
  1980. static void pred8x8_128_dc_c(uint8_t *src, int stride){
  1981. int i;
  1982. for(i=0; i<8; i++){
  1983. ((uint32_t*)(src+i*stride))[0]=
  1984. ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
  1985. }
  1986. }
  1987. static void pred8x8_left_dc_c(uint8_t *src, int stride){
  1988. int i;
  1989. int dc0, dc2;
  1990. dc0=dc2=0;
  1991. for(i=0;i<4; i++){
  1992. dc0+= src[-1+i*stride];
  1993. dc2+= src[-1+(i+4)*stride];
  1994. }
  1995. dc0= 0x01010101*((dc0 + 2)>>2);
  1996. dc2= 0x01010101*((dc2 + 2)>>2);
  1997. for(i=0; i<4; i++){
  1998. ((uint32_t*)(src+i*stride))[0]=
  1999. ((uint32_t*)(src+i*stride))[1]= dc0;
  2000. }
  2001. for(i=4; i<8; i++){
  2002. ((uint32_t*)(src+i*stride))[0]=
  2003. ((uint32_t*)(src+i*stride))[1]= dc2;
  2004. }
  2005. }
  2006. static void pred8x8_top_dc_c(uint8_t *src, int stride){
  2007. int i;
  2008. int dc0, dc1;
  2009. dc0=dc1=0;
  2010. for(i=0;i<4; i++){
  2011. dc0+= src[i-stride];
  2012. dc1+= src[4+i-stride];
  2013. }
  2014. dc0= 0x01010101*((dc0 + 2)>>2);
  2015. dc1= 0x01010101*((dc1 + 2)>>2);
  2016. for(i=0; i<4; i++){
  2017. ((uint32_t*)(src+i*stride))[0]= dc0;
  2018. ((uint32_t*)(src+i*stride))[1]= dc1;
  2019. }
  2020. for(i=4; i<8; i++){
  2021. ((uint32_t*)(src+i*stride))[0]= dc0;
  2022. ((uint32_t*)(src+i*stride))[1]= dc1;
  2023. }
  2024. }
  2025. static void pred8x8_dc_c(uint8_t *src, int stride){
  2026. int i;
  2027. int dc0, dc1, dc2, dc3;
  2028. dc0=dc1=dc2=0;
  2029. for(i=0;i<4; i++){
  2030. dc0+= src[-1+i*stride] + src[i-stride];
  2031. dc1+= src[4+i-stride];
  2032. dc2+= src[-1+(i+4)*stride];
  2033. }
  2034. dc3= 0x01010101*((dc1 + dc2 + 4)>>3);
  2035. dc0= 0x01010101*((dc0 + 4)>>3);
  2036. dc1= 0x01010101*((dc1 + 2)>>2);
  2037. dc2= 0x01010101*((dc2 + 2)>>2);
  2038. for(i=0; i<4; i++){
  2039. ((uint32_t*)(src+i*stride))[0]= dc0;
  2040. ((uint32_t*)(src+i*stride))[1]= dc1;
  2041. }
  2042. for(i=4; i<8; i++){
  2043. ((uint32_t*)(src+i*stride))[0]= dc2;
  2044. ((uint32_t*)(src+i*stride))[1]= dc3;
  2045. }
  2046. }
  2047. static void pred8x8_plane_c(uint8_t *src, int stride){
  2048. int j, k;
  2049. int a;
  2050. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  2051. const uint8_t * const src0 = src+3-stride;
  2052. const uint8_t *src1 = src+4*stride-1;
  2053. const uint8_t *src2 = src1-2*stride; // == src+2*stride-1;
  2054. int H = src0[1] - src0[-1];
  2055. int V = src1[0] - src2[ 0];
  2056. for(k=2; k<=4; ++k) {
  2057. src1 += stride; src2 -= stride;
  2058. H += k*(src0[k] - src0[-k]);
  2059. V += k*(src1[0] - src2[ 0]);
  2060. }
  2061. H = ( 17*H+16 ) >> 5;
  2062. V = ( 17*V+16 ) >> 5;
  2063. a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
  2064. for(j=8; j>0; --j) {
  2065. int b = a;
  2066. a += V;
  2067. src[0] = cm[ (b ) >> 5 ];
  2068. src[1] = cm[ (b+ H) >> 5 ];
  2069. src[2] = cm[ (b+2*H) >> 5 ];
  2070. src[3] = cm[ (b+3*H) >> 5 ];
  2071. src[4] = cm[ (b+4*H) >> 5 ];
  2072. src[5] = cm[ (b+5*H) >> 5 ];
  2073. src[6] = cm[ (b+6*H) >> 5 ];
  2074. src[7] = cm[ (b+7*H) >> 5 ];
  2075. src += stride;
  2076. }
  2077. }
  2078. #define SRC(x,y) src[(x)+(y)*stride]
  2079. #define PL(y) \
  2080. const int l##y = (SRC(-1,y-1) + 2*SRC(-1,y) + SRC(-1,y+1) + 2) >> 2;
  2081. #define PREDICT_8x8_LOAD_LEFT \
  2082. const int l0 = ((has_topleft ? SRC(-1,-1) : SRC(-1,0)) \
  2083. + 2*SRC(-1,0) + SRC(-1,1) + 2) >> 2; \
  2084. PL(1) PL(2) PL(3) PL(4) PL(5) PL(6) \
  2085. const int l7 attribute_unused = (SRC(-1,6) + 3*SRC(-1,7) + 2) >> 2
  2086. #define PT(x) \
  2087. const int t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  2088. #define PREDICT_8x8_LOAD_TOP \
  2089. const int t0 = ((has_topleft ? SRC(-1,-1) : SRC(0,-1)) \
  2090. + 2*SRC(0,-1) + SRC(1,-1) + 2) >> 2; \
  2091. PT(1) PT(2) PT(3) PT(4) PT(5) PT(6) \
  2092. const int t7 attribute_unused = ((has_topright ? SRC(8,-1) : SRC(7,-1)) \
  2093. + 2*SRC(7,-1) + SRC(6,-1) + 2) >> 2
  2094. #define PTR(x) \
  2095. t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  2096. #define PREDICT_8x8_LOAD_TOPRIGHT \
  2097. int t8, t9, t10, t11, t12, t13, t14, t15; \
  2098. if(has_topright) { \
  2099. PTR(8) PTR(9) PTR(10) PTR(11) PTR(12) PTR(13) PTR(14) \
  2100. t15 = (SRC(14,-1) + 3*SRC(15,-1) + 2) >> 2; \
  2101. } else t8=t9=t10=t11=t12=t13=t14=t15= SRC(7,-1);
  2102. #define PREDICT_8x8_LOAD_TOPLEFT \
  2103. const int lt = (SRC(-1,0) + 2*SRC(-1,-1) + SRC(0,-1) + 2) >> 2
  2104. #define PREDICT_8x8_DC(v) \
  2105. int y; \
  2106. for( y = 0; y < 8; y++ ) { \
  2107. ((uint32_t*)src)[0] = \
  2108. ((uint32_t*)src)[1] = v; \
  2109. src += stride; \
  2110. }
  2111. static void pred8x8l_128_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2112. {
  2113. PREDICT_8x8_DC(0x80808080);
  2114. }
  2115. static void pred8x8l_left_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2116. {
  2117. PREDICT_8x8_LOAD_LEFT;
  2118. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7+4) >> 3) * 0x01010101;
  2119. PREDICT_8x8_DC(dc);
  2120. }
  2121. static void pred8x8l_top_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2122. {
  2123. PREDICT_8x8_LOAD_TOP;
  2124. const uint32_t dc = ((t0+t1+t2+t3+t4+t5+t6+t7+4) >> 3) * 0x01010101;
  2125. PREDICT_8x8_DC(dc);
  2126. }
  2127. static void pred8x8l_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2128. {
  2129. PREDICT_8x8_LOAD_LEFT;
  2130. PREDICT_8x8_LOAD_TOP;
  2131. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7
  2132. +t0+t1+t2+t3+t4+t5+t6+t7+8) >> 4) * 0x01010101;
  2133. PREDICT_8x8_DC(dc);
  2134. }
  2135. static void pred8x8l_horizontal_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2136. {
  2137. PREDICT_8x8_LOAD_LEFT;
  2138. #define ROW(y) ((uint32_t*)(src+y*stride))[0] =\
  2139. ((uint32_t*)(src+y*stride))[1] = 0x01010101 * l##y
  2140. ROW(0); ROW(1); ROW(2); ROW(3); ROW(4); ROW(5); ROW(6); ROW(7);
  2141. #undef ROW
  2142. }
  2143. static void pred8x8l_vertical_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2144. {
  2145. int y;
  2146. PREDICT_8x8_LOAD_TOP;
  2147. src[0] = t0;
  2148. src[1] = t1;
  2149. src[2] = t2;
  2150. src[3] = t3;
  2151. src[4] = t4;
  2152. src[5] = t5;
  2153. src[6] = t6;
  2154. src[7] = t7;
  2155. for( y = 1; y < 8; y++ )
  2156. *(uint64_t*)(src+y*stride) = *(uint64_t*)src;
  2157. }
  2158. static void pred8x8l_down_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2159. {
  2160. PREDICT_8x8_LOAD_TOP;
  2161. PREDICT_8x8_LOAD_TOPRIGHT;
  2162. SRC(0,0)= (t0 + 2*t1 + t2 + 2) >> 2;
  2163. SRC(0,1)=SRC(1,0)= (t1 + 2*t2 + t3 + 2) >> 2;
  2164. SRC(0,2)=SRC(1,1)=SRC(2,0)= (t2 + 2*t3 + t4 + 2) >> 2;
  2165. SRC(0,3)=SRC(1,2)=SRC(2,1)=SRC(3,0)= (t3 + 2*t4 + t5 + 2) >> 2;
  2166. SRC(0,4)=SRC(1,3)=SRC(2,2)=SRC(3,1)=SRC(4,0)= (t4 + 2*t5 + t6 + 2) >> 2;
  2167. SRC(0,5)=SRC(1,4)=SRC(2,3)=SRC(3,2)=SRC(4,1)=SRC(5,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2168. 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;
  2169. 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;
  2170. 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;
  2171. SRC(2,7)=SRC(3,6)=SRC(4,5)=SRC(5,4)=SRC(6,3)=SRC(7,2)= (t9 + 2*t10 + t11 + 2) >> 2;
  2172. SRC(3,7)=SRC(4,6)=SRC(5,5)=SRC(6,4)=SRC(7,3)= (t10 + 2*t11 + t12 + 2) >> 2;
  2173. SRC(4,7)=SRC(5,6)=SRC(6,5)=SRC(7,4)= (t11 + 2*t12 + t13 + 2) >> 2;
  2174. SRC(5,7)=SRC(6,6)=SRC(7,5)= (t12 + 2*t13 + t14 + 2) >> 2;
  2175. SRC(6,7)=SRC(7,6)= (t13 + 2*t14 + t15 + 2) >> 2;
  2176. SRC(7,7)= (t14 + 3*t15 + 2) >> 2;
  2177. }
  2178. static void pred8x8l_down_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2179. {
  2180. PREDICT_8x8_LOAD_TOP;
  2181. PREDICT_8x8_LOAD_LEFT;
  2182. PREDICT_8x8_LOAD_TOPLEFT;
  2183. SRC(0,7)= (l7 + 2*l6 + l5 + 2) >> 2;
  2184. SRC(0,6)=SRC(1,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2185. SRC(0,5)=SRC(1,6)=SRC(2,7)= (l5 + 2*l4 + l3 + 2) >> 2;
  2186. SRC(0,4)=SRC(1,5)=SRC(2,6)=SRC(3,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2187. SRC(0,3)=SRC(1,4)=SRC(2,5)=SRC(3,6)=SRC(4,7)= (l3 + 2*l2 + l1 + 2) >> 2;
  2188. SRC(0,2)=SRC(1,3)=SRC(2,4)=SRC(3,5)=SRC(4,6)=SRC(5,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2189. 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;
  2190. 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;
  2191. 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;
  2192. SRC(2,0)=SRC(3,1)=SRC(4,2)=SRC(5,3)=SRC(6,4)=SRC(7,5)= (t0 + 2*t1 + t2 + 2) >> 2;
  2193. SRC(3,0)=SRC(4,1)=SRC(5,2)=SRC(6,3)=SRC(7,4)= (t1 + 2*t2 + t3 + 2) >> 2;
  2194. SRC(4,0)=SRC(5,1)=SRC(6,2)=SRC(7,3)= (t2 + 2*t3 + t4 + 2) >> 2;
  2195. SRC(5,0)=SRC(6,1)=SRC(7,2)= (t3 + 2*t4 + t5 + 2) >> 2;
  2196. SRC(6,0)=SRC(7,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2197. SRC(7,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2198. }
  2199. static void pred8x8l_vertical_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2200. {
  2201. PREDICT_8x8_LOAD_TOP;
  2202. PREDICT_8x8_LOAD_LEFT;
  2203. PREDICT_8x8_LOAD_TOPLEFT;
  2204. SRC(0,6)= (l5 + 2*l4 + l3 + 2) >> 2;
  2205. SRC(0,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2206. SRC(0,4)=SRC(1,6)= (l3 + 2*l2 + l1 + 2) >> 2;
  2207. SRC(0,5)=SRC(1,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2208. SRC(0,2)=SRC(1,4)=SRC(2,6)= (l1 + 2*l0 + lt + 2) >> 2;
  2209. SRC(0,3)=SRC(1,5)=SRC(2,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2210. SRC(0,1)=SRC(1,3)=SRC(2,5)=SRC(3,7)= (l0 + 2*lt + t0 + 2) >> 2;
  2211. SRC(0,0)=SRC(1,2)=SRC(2,4)=SRC(3,6)= (lt + t0 + 1) >> 1;
  2212. SRC(1,1)=SRC(2,3)=SRC(3,5)=SRC(4,7)= (lt + 2*t0 + t1 + 2) >> 2;
  2213. SRC(1,0)=SRC(2,2)=SRC(3,4)=SRC(4,6)= (t0 + t1 + 1) >> 1;
  2214. SRC(2,1)=SRC(3,3)=SRC(4,5)=SRC(5,7)= (t0 + 2*t1 + t2 + 2) >> 2;
  2215. SRC(2,0)=SRC(3,2)=SRC(4,4)=SRC(5,6)= (t1 + t2 + 1) >> 1;
  2216. SRC(3,1)=SRC(4,3)=SRC(5,5)=SRC(6,7)= (t1 + 2*t2 + t3 + 2) >> 2;
  2217. SRC(3,0)=SRC(4,2)=SRC(5,4)=SRC(6,6)= (t2 + t3 + 1) >> 1;
  2218. SRC(4,1)=SRC(5,3)=SRC(6,5)=SRC(7,7)= (t2 + 2*t3 + t4 + 2) >> 2;
  2219. SRC(4,0)=SRC(5,2)=SRC(6,4)=SRC(7,6)= (t3 + t4 + 1) >> 1;
  2220. SRC(5,1)=SRC(6,3)=SRC(7,5)= (t3 + 2*t4 + t5 + 2) >> 2;
  2221. SRC(5,0)=SRC(6,2)=SRC(7,4)= (t4 + t5 + 1) >> 1;
  2222. SRC(6,1)=SRC(7,3)= (t4 + 2*t5 + t6 + 2) >> 2;
  2223. SRC(6,0)=SRC(7,2)= (t5 + t6 + 1) >> 1;
  2224. SRC(7,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2225. SRC(7,0)= (t6 + t7 + 1) >> 1;
  2226. }
  2227. static void pred8x8l_horizontal_down_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2228. {
  2229. PREDICT_8x8_LOAD_TOP;
  2230. PREDICT_8x8_LOAD_LEFT;
  2231. PREDICT_8x8_LOAD_TOPLEFT;
  2232. SRC(0,7)= (l6 + l7 + 1) >> 1;
  2233. SRC(1,7)= (l5 + 2*l6 + l7 + 2) >> 2;
  2234. SRC(0,6)=SRC(2,7)= (l5 + l6 + 1) >> 1;
  2235. SRC(1,6)=SRC(3,7)= (l4 + 2*l5 + l6 + 2) >> 2;
  2236. SRC(0,5)=SRC(2,6)=SRC(4,7)= (l4 + l5 + 1) >> 1;
  2237. SRC(1,5)=SRC(3,6)=SRC(5,7)= (l3 + 2*l4 + l5 + 2) >> 2;
  2238. SRC(0,4)=SRC(2,5)=SRC(4,6)=SRC(6,7)= (l3 + l4 + 1) >> 1;
  2239. SRC(1,4)=SRC(3,5)=SRC(5,6)=SRC(7,7)= (l2 + 2*l3 + l4 + 2) >> 2;
  2240. SRC(0,3)=SRC(2,4)=SRC(4,5)=SRC(6,6)= (l2 + l3 + 1) >> 1;
  2241. SRC(1,3)=SRC(3,4)=SRC(5,5)=SRC(7,6)= (l1 + 2*l2 + l3 + 2) >> 2;
  2242. SRC(0,2)=SRC(2,3)=SRC(4,4)=SRC(6,5)= (l1 + l2 + 1) >> 1;
  2243. SRC(1,2)=SRC(3,3)=SRC(5,4)=SRC(7,5)= (l0 + 2*l1 + l2 + 2) >> 2;
  2244. SRC(0,1)=SRC(2,2)=SRC(4,3)=SRC(6,4)= (l0 + l1 + 1) >> 1;
  2245. SRC(1,1)=SRC(3,2)=SRC(5,3)=SRC(7,4)= (lt + 2*l0 + l1 + 2) >> 2;
  2246. SRC(0,0)=SRC(2,1)=SRC(4,2)=SRC(6,3)= (lt + l0 + 1) >> 1;
  2247. SRC(1,0)=SRC(3,1)=SRC(5,2)=SRC(7,3)= (l0 + 2*lt + t0 + 2) >> 2;
  2248. SRC(2,0)=SRC(4,1)=SRC(6,2)= (t1 + 2*t0 + lt + 2) >> 2;
  2249. SRC(3,0)=SRC(5,1)=SRC(7,2)= (t2 + 2*t1 + t0 + 2) >> 2;
  2250. SRC(4,0)=SRC(6,1)= (t3 + 2*t2 + t1 + 2) >> 2;
  2251. SRC(5,0)=SRC(7,1)= (t4 + 2*t3 + t2 + 2) >> 2;
  2252. SRC(6,0)= (t5 + 2*t4 + t3 + 2) >> 2;
  2253. SRC(7,0)= (t6 + 2*t5 + t4 + 2) >> 2;
  2254. }
  2255. static void pred8x8l_vertical_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2256. {
  2257. PREDICT_8x8_LOAD_TOP;
  2258. PREDICT_8x8_LOAD_TOPRIGHT;
  2259. SRC(0,0)= (t0 + t1 + 1) >> 1;
  2260. SRC(0,1)= (t0 + 2*t1 + t2 + 2) >> 2;
  2261. SRC(0,2)=SRC(1,0)= (t1 + t2 + 1) >> 1;
  2262. SRC(0,3)=SRC(1,1)= (t1 + 2*t2 + t3 + 2) >> 2;
  2263. SRC(0,4)=SRC(1,2)=SRC(2,0)= (t2 + t3 + 1) >> 1;
  2264. SRC(0,5)=SRC(1,3)=SRC(2,1)= (t2 + 2*t3 + t4 + 2) >> 2;
  2265. SRC(0,6)=SRC(1,4)=SRC(2,2)=SRC(3,0)= (t3 + t4 + 1) >> 1;
  2266. SRC(0,7)=SRC(1,5)=SRC(2,3)=SRC(3,1)= (t3 + 2*t4 + t5 + 2) >> 2;
  2267. SRC(1,6)=SRC(2,4)=SRC(3,2)=SRC(4,0)= (t4 + t5 + 1) >> 1;
  2268. SRC(1,7)=SRC(2,5)=SRC(3,3)=SRC(4,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2269. SRC(2,6)=SRC(3,4)=SRC(4,2)=SRC(5,0)= (t5 + t6 + 1) >> 1;
  2270. SRC(2,7)=SRC(3,5)=SRC(4,3)=SRC(5,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2271. SRC(3,6)=SRC(4,4)=SRC(5,2)=SRC(6,0)= (t6 + t7 + 1) >> 1;
  2272. SRC(3,7)=SRC(4,5)=SRC(5,3)=SRC(6,1)= (t6 + 2*t7 + t8 + 2) >> 2;
  2273. SRC(4,6)=SRC(5,4)=SRC(6,2)=SRC(7,0)= (t7 + t8 + 1) >> 1;
  2274. SRC(4,7)=SRC(5,5)=SRC(6,3)=SRC(7,1)= (t7 + 2*t8 + t9 + 2) >> 2;
  2275. SRC(5,6)=SRC(6,4)=SRC(7,2)= (t8 + t9 + 1) >> 1;
  2276. SRC(5,7)=SRC(6,5)=SRC(7,3)= (t8 + 2*t9 + t10 + 2) >> 2;
  2277. SRC(6,6)=SRC(7,4)= (t9 + t10 + 1) >> 1;
  2278. SRC(6,7)=SRC(7,5)= (t9 + 2*t10 + t11 + 2) >> 2;
  2279. SRC(7,6)= (t10 + t11 + 1) >> 1;
  2280. SRC(7,7)= (t10 + 2*t11 + t12 + 2) >> 2;
  2281. }
  2282. static void pred8x8l_horizontal_up_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2283. {
  2284. PREDICT_8x8_LOAD_LEFT;
  2285. SRC(0,0)= (l0 + l1 + 1) >> 1;
  2286. SRC(1,0)= (l0 + 2*l1 + l2 + 2) >> 2;
  2287. SRC(0,1)=SRC(2,0)= (l1 + l2 + 1) >> 1;
  2288. SRC(1,1)=SRC(3,0)= (l1 + 2*l2 + l3 + 2) >> 2;
  2289. SRC(0,2)=SRC(2,1)=SRC(4,0)= (l2 + l3 + 1) >> 1;
  2290. SRC(1,2)=SRC(3,1)=SRC(5,0)= (l2 + 2*l3 + l4 + 2) >> 2;
  2291. SRC(0,3)=SRC(2,2)=SRC(4,1)=SRC(6,0)= (l3 + l4 + 1) >> 1;
  2292. SRC(1,3)=SRC(3,2)=SRC(5,1)=SRC(7,0)= (l3 + 2*l4 + l5 + 2) >> 2;
  2293. SRC(0,4)=SRC(2,3)=SRC(4,2)=SRC(6,1)= (l4 + l5 + 1) >> 1;
  2294. SRC(1,4)=SRC(3,3)=SRC(5,2)=SRC(7,1)= (l4 + 2*l5 + l6 + 2) >> 2;
  2295. SRC(0,5)=SRC(2,4)=SRC(4,3)=SRC(6,2)= (l5 + l6 + 1) >> 1;
  2296. SRC(1,5)=SRC(3,4)=SRC(5,3)=SRC(7,2)= (l5 + 2*l6 + l7 + 2) >> 2;
  2297. SRC(0,6)=SRC(2,5)=SRC(4,4)=SRC(6,3)= (l6 + l7 + 1) >> 1;
  2298. SRC(1,6)=SRC(3,5)=SRC(5,4)=SRC(7,3)= (l6 + 3*l7 + 2) >> 2;
  2299. SRC(0,7)=SRC(1,7)=SRC(2,6)=SRC(2,7)=SRC(3,6)=
  2300. SRC(3,7)=SRC(4,5)=SRC(4,6)=SRC(4,7)=SRC(5,5)=
  2301. SRC(5,6)=SRC(5,7)=SRC(6,4)=SRC(6,5)=SRC(6,6)=
  2302. SRC(6,7)=SRC(7,4)=SRC(7,5)=SRC(7,6)=SRC(7,7)= l7;
  2303. }
  2304. #undef PREDICT_8x8_LOAD_LEFT
  2305. #undef PREDICT_8x8_LOAD_TOP
  2306. #undef PREDICT_8x8_LOAD_TOPLEFT
  2307. #undef PREDICT_8x8_LOAD_TOPRIGHT
  2308. #undef PREDICT_8x8_DC
  2309. #undef PTR
  2310. #undef PT
  2311. #undef PL
  2312. #undef SRC
  2313. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  2314. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2315. int src_x_offset, int src_y_offset,
  2316. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  2317. MpegEncContext * const s = &h->s;
  2318. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  2319. const int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  2320. const int luma_xy= (mx&3) + ((my&3)<<2);
  2321. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*s->linesize;
  2322. uint8_t * src_cb= pic->data[1] + (mx>>3) + (my>>3)*s->uvlinesize;
  2323. uint8_t * src_cr= pic->data[2] + (mx>>3) + (my>>3)*s->uvlinesize;
  2324. int extra_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16; //FIXME increase edge?, IMHO not worth it
  2325. int extra_height= extra_width;
  2326. int emu=0;
  2327. const int full_mx= mx>>2;
  2328. const int full_my= my>>2;
  2329. const int pic_width = 16*s->mb_width;
  2330. const int pic_height = 16*s->mb_height;
  2331. if(!pic->data[0])
  2332. return;
  2333. if(mx&7) extra_width -= 3;
  2334. if(my&7) extra_height -= 3;
  2335. if( full_mx < 0-extra_width
  2336. || full_my < 0-extra_height
  2337. || full_mx + 16/*FIXME*/ > pic_width + extra_width
  2338. || full_my + 16/*FIXME*/ > pic_height + extra_height){
  2339. ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*s->linesize, s->linesize, 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
  2340. src_y= s->edge_emu_buffer + 2 + 2*s->linesize;
  2341. emu=1;
  2342. }
  2343. qpix_op[luma_xy](dest_y, src_y, s->linesize); //FIXME try variable height perhaps?
  2344. if(!square){
  2345. qpix_op[luma_xy](dest_y + delta, src_y + delta, s->linesize);
  2346. }
  2347. if(s->flags&CODEC_FLAG_GRAY) return;
  2348. if(emu){
  2349. ff_emulated_edge_mc(s->edge_emu_buffer, src_cb, s->uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
  2350. src_cb= s->edge_emu_buffer;
  2351. }
  2352. chroma_op(dest_cb, src_cb, s->uvlinesize, chroma_height, mx&7, my&7);
  2353. if(emu){
  2354. ff_emulated_edge_mc(s->edge_emu_buffer, src_cr, s->uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
  2355. src_cr= s->edge_emu_buffer;
  2356. }
  2357. chroma_op(dest_cr, src_cr, s->uvlinesize, chroma_height, mx&7, my&7);
  2358. }
  2359. static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
  2360. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2361. int x_offset, int y_offset,
  2362. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2363. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2364. int list0, int list1){
  2365. MpegEncContext * const s = &h->s;
  2366. qpel_mc_func *qpix_op= qpix_put;
  2367. h264_chroma_mc_func chroma_op= chroma_put;
  2368. dest_y += 2*x_offset + 2*y_offset*s-> linesize;
  2369. dest_cb += x_offset + y_offset*s->uvlinesize;
  2370. dest_cr += x_offset + y_offset*s->uvlinesize;
  2371. x_offset += 8*s->mb_x;
  2372. y_offset += 8*s->mb_y;
  2373. if(list0){
  2374. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  2375. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  2376. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2377. qpix_op, chroma_op);
  2378. qpix_op= qpix_avg;
  2379. chroma_op= chroma_avg;
  2380. }
  2381. if(list1){
  2382. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  2383. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  2384. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2385. qpix_op, chroma_op);
  2386. }
  2387. }
  2388. static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
  2389. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2390. int x_offset, int y_offset,
  2391. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2392. h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
  2393. h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
  2394. int list0, int list1){
  2395. MpegEncContext * const s = &h->s;
  2396. dest_y += 2*x_offset + 2*y_offset*s-> linesize;
  2397. dest_cb += x_offset + y_offset*s->uvlinesize;
  2398. dest_cr += x_offset + y_offset*s->uvlinesize;
  2399. x_offset += 8*s->mb_x;
  2400. y_offset += 8*s->mb_y;
  2401. if(list0 && list1){
  2402. /* don't optimize for luma-only case, since B-frames usually
  2403. * use implicit weights => chroma too. */
  2404. uint8_t *tmp_cb = s->obmc_scratchpad;
  2405. uint8_t *tmp_cr = tmp_cb + 8*s->uvlinesize;
  2406. uint8_t *tmp_y = tmp_cr + 8*s->uvlinesize;
  2407. int refn0 = h->ref_cache[0][ scan8[n] ];
  2408. int refn1 = h->ref_cache[1][ scan8[n] ];
  2409. mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
  2410. dest_y, dest_cb, dest_cr,
  2411. x_offset, y_offset, qpix_put, chroma_put);
  2412. mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
  2413. tmp_y, tmp_cb, tmp_cr,
  2414. x_offset, y_offset, qpix_put, chroma_put);
  2415. if(h->use_weight == 2){
  2416. int weight0 = h->implicit_weight[refn0][refn1];
  2417. int weight1 = 64 - weight0;
  2418. luma_weight_avg( dest_y, tmp_y, s-> linesize, 5, weight0, weight1, 0);
  2419. chroma_weight_avg(dest_cb, tmp_cb, s->uvlinesize, 5, weight0, weight1, 0);
  2420. chroma_weight_avg(dest_cr, tmp_cr, s->uvlinesize, 5, weight0, weight1, 0);
  2421. }else{
  2422. luma_weight_avg(dest_y, tmp_y, s->linesize, h->luma_log2_weight_denom,
  2423. h->luma_weight[0][refn0], h->luma_weight[1][refn1],
  2424. h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
  2425. chroma_weight_avg(dest_cb, tmp_cb, s->uvlinesize, h->chroma_log2_weight_denom,
  2426. h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
  2427. h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
  2428. chroma_weight_avg(dest_cr, tmp_cr, s->uvlinesize, h->chroma_log2_weight_denom,
  2429. h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
  2430. h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
  2431. }
  2432. }else{
  2433. int list = list1 ? 1 : 0;
  2434. int refn = h->ref_cache[list][ scan8[n] ];
  2435. Picture *ref= &h->ref_list[list][refn];
  2436. mc_dir_part(h, ref, n, square, chroma_height, delta, list,
  2437. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2438. qpix_put, chroma_put);
  2439. luma_weight_op(dest_y, s->linesize, h->luma_log2_weight_denom,
  2440. h->luma_weight[list][refn], h->luma_offset[list][refn]);
  2441. if(h->use_weight_chroma){
  2442. chroma_weight_op(dest_cb, s->uvlinesize, h->chroma_log2_weight_denom,
  2443. h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
  2444. chroma_weight_op(dest_cr, s->uvlinesize, h->chroma_log2_weight_denom,
  2445. h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
  2446. }
  2447. }
  2448. }
  2449. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  2450. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2451. int x_offset, int y_offset,
  2452. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2453. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2454. h264_weight_func *weight_op, h264_biweight_func *weight_avg,
  2455. int list0, int list1){
  2456. if((h->use_weight==2 && list0 && list1
  2457. && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
  2458. || h->use_weight==1)
  2459. mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2460. x_offset, y_offset, qpix_put, chroma_put,
  2461. weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
  2462. else
  2463. mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2464. x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
  2465. }
  2466. static inline void prefetch_motion(H264Context *h, int list){
  2467. /* fetch pixels for estimated mv 4 macroblocks ahead
  2468. * optimized for 64byte cache lines */
  2469. MpegEncContext * const s = &h->s;
  2470. const int refn = h->ref_cache[list][scan8[0]];
  2471. if(refn >= 0){
  2472. const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
  2473. const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
  2474. uint8_t **src= h->ref_list[list][refn].data;
  2475. int off= mx + (my + (s->mb_x&3)*4)*s->linesize + 64;
  2476. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  2477. off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
  2478. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  2479. }
  2480. }
  2481. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2482. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  2483. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
  2484. h264_weight_func *weight_op, h264_biweight_func *weight_avg){
  2485. MpegEncContext * const s = &h->s;
  2486. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2487. const int mb_type= s->current_picture.mb_type[mb_xy];
  2488. assert(IS_INTER(mb_type));
  2489. prefetch_motion(h, 0);
  2490. if(IS_16X16(mb_type)){
  2491. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  2492. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  2493. &weight_op[0], &weight_avg[0],
  2494. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2495. }else if(IS_16X8(mb_type)){
  2496. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  2497. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2498. &weight_op[1], &weight_avg[1],
  2499. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2500. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  2501. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2502. &weight_op[1], &weight_avg[1],
  2503. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2504. }else if(IS_8X16(mb_type)){
  2505. mc_part(h, 0, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 0, 0,
  2506. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2507. &weight_op[2], &weight_avg[2],
  2508. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2509. mc_part(h, 4, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 4, 0,
  2510. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2511. &weight_op[2], &weight_avg[2],
  2512. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2513. }else{
  2514. int i;
  2515. assert(IS_8X8(mb_type));
  2516. for(i=0; i<4; i++){
  2517. const int sub_mb_type= h->sub_mb_type[i];
  2518. const int n= 4*i;
  2519. int x_offset= (i&1)<<2;
  2520. int y_offset= (i&2)<<1;
  2521. if(IS_SUB_8X8(sub_mb_type)){
  2522. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2523. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2524. &weight_op[3], &weight_avg[3],
  2525. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2526. }else if(IS_SUB_8X4(sub_mb_type)){
  2527. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2528. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2529. &weight_op[4], &weight_avg[4],
  2530. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2531. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  2532. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2533. &weight_op[4], &weight_avg[4],
  2534. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2535. }else if(IS_SUB_4X8(sub_mb_type)){
  2536. mc_part(h, n , 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2537. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2538. &weight_op[5], &weight_avg[5],
  2539. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2540. mc_part(h, n+1, 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
  2541. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2542. &weight_op[5], &weight_avg[5],
  2543. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2544. }else{
  2545. int j;
  2546. assert(IS_SUB_4X4(sub_mb_type));
  2547. for(j=0; j<4; j++){
  2548. int sub_x_offset= x_offset + 2*(j&1);
  2549. int sub_y_offset= y_offset + (j&2);
  2550. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  2551. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2552. &weight_op[6], &weight_avg[6],
  2553. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2554. }
  2555. }
  2556. }
  2557. }
  2558. prefetch_motion(h, 1);
  2559. }
  2560. static void decode_init_vlc(H264Context *h){
  2561. static int done = 0;
  2562. if (!done) {
  2563. int i;
  2564. done = 1;
  2565. init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
  2566. &chroma_dc_coeff_token_len [0], 1, 1,
  2567. &chroma_dc_coeff_token_bits[0], 1, 1, 1);
  2568. for(i=0; i<4; i++){
  2569. init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
  2570. &coeff_token_len [i][0], 1, 1,
  2571. &coeff_token_bits[i][0], 1, 1, 1);
  2572. }
  2573. for(i=0; i<3; i++){
  2574. init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
  2575. &chroma_dc_total_zeros_len [i][0], 1, 1,
  2576. &chroma_dc_total_zeros_bits[i][0], 1, 1, 1);
  2577. }
  2578. for(i=0; i<15; i++){
  2579. init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
  2580. &total_zeros_len [i][0], 1, 1,
  2581. &total_zeros_bits[i][0], 1, 1, 1);
  2582. }
  2583. for(i=0; i<6; i++){
  2584. init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
  2585. &run_len [i][0], 1, 1,
  2586. &run_bits[i][0], 1, 1, 1);
  2587. }
  2588. init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
  2589. &run_len [6][0], 1, 1,
  2590. &run_bits[6][0], 1, 1, 1);
  2591. }
  2592. }
  2593. /**
  2594. * Sets the intra prediction function pointers.
  2595. */
  2596. static void init_pred_ptrs(H264Context *h){
  2597. // MpegEncContext * const s = &h->s;
  2598. h->pred4x4[VERT_PRED ]= pred4x4_vertical_c;
  2599. h->pred4x4[HOR_PRED ]= pred4x4_horizontal_c;
  2600. h->pred4x4[DC_PRED ]= pred4x4_dc_c;
  2601. h->pred4x4[DIAG_DOWN_LEFT_PRED ]= pred4x4_down_left_c;
  2602. h->pred4x4[DIAG_DOWN_RIGHT_PRED]= pred4x4_down_right_c;
  2603. h->pred4x4[VERT_RIGHT_PRED ]= pred4x4_vertical_right_c;
  2604. h->pred4x4[HOR_DOWN_PRED ]= pred4x4_horizontal_down_c;
  2605. h->pred4x4[VERT_LEFT_PRED ]= pred4x4_vertical_left_c;
  2606. h->pred4x4[HOR_UP_PRED ]= pred4x4_horizontal_up_c;
  2607. h->pred4x4[LEFT_DC_PRED ]= pred4x4_left_dc_c;
  2608. h->pred4x4[TOP_DC_PRED ]= pred4x4_top_dc_c;
  2609. h->pred4x4[DC_128_PRED ]= pred4x4_128_dc_c;
  2610. h->pred8x8l[VERT_PRED ]= pred8x8l_vertical_c;
  2611. h->pred8x8l[HOR_PRED ]= pred8x8l_horizontal_c;
  2612. h->pred8x8l[DC_PRED ]= pred8x8l_dc_c;
  2613. h->pred8x8l[DIAG_DOWN_LEFT_PRED ]= pred8x8l_down_left_c;
  2614. h->pred8x8l[DIAG_DOWN_RIGHT_PRED]= pred8x8l_down_right_c;
  2615. h->pred8x8l[VERT_RIGHT_PRED ]= pred8x8l_vertical_right_c;
  2616. h->pred8x8l[HOR_DOWN_PRED ]= pred8x8l_horizontal_down_c;
  2617. h->pred8x8l[VERT_LEFT_PRED ]= pred8x8l_vertical_left_c;
  2618. h->pred8x8l[HOR_UP_PRED ]= pred8x8l_horizontal_up_c;
  2619. h->pred8x8l[LEFT_DC_PRED ]= pred8x8l_left_dc_c;
  2620. h->pred8x8l[TOP_DC_PRED ]= pred8x8l_top_dc_c;
  2621. h->pred8x8l[DC_128_PRED ]= pred8x8l_128_dc_c;
  2622. h->pred8x8[DC_PRED8x8 ]= pred8x8_dc_c;
  2623. h->pred8x8[VERT_PRED8x8 ]= pred8x8_vertical_c;
  2624. h->pred8x8[HOR_PRED8x8 ]= pred8x8_horizontal_c;
  2625. h->pred8x8[PLANE_PRED8x8 ]= pred8x8_plane_c;
  2626. h->pred8x8[LEFT_DC_PRED8x8]= pred8x8_left_dc_c;
  2627. h->pred8x8[TOP_DC_PRED8x8 ]= pred8x8_top_dc_c;
  2628. h->pred8x8[DC_128_PRED8x8 ]= pred8x8_128_dc_c;
  2629. h->pred16x16[DC_PRED8x8 ]= pred16x16_dc_c;
  2630. h->pred16x16[VERT_PRED8x8 ]= pred16x16_vertical_c;
  2631. h->pred16x16[HOR_PRED8x8 ]= pred16x16_horizontal_c;
  2632. h->pred16x16[PLANE_PRED8x8 ]= pred16x16_plane_c;
  2633. h->pred16x16[LEFT_DC_PRED8x8]= pred16x16_left_dc_c;
  2634. h->pred16x16[TOP_DC_PRED8x8 ]= pred16x16_top_dc_c;
  2635. h->pred16x16[DC_128_PRED8x8 ]= pred16x16_128_dc_c;
  2636. }
  2637. static void free_tables(H264Context *h){
  2638. av_freep(&h->intra4x4_pred_mode);
  2639. av_freep(&h->chroma_pred_mode_table);
  2640. av_freep(&h->cbp_table);
  2641. av_freep(&h->mvd_table[0]);
  2642. av_freep(&h->mvd_table[1]);
  2643. av_freep(&h->direct_table);
  2644. av_freep(&h->non_zero_count);
  2645. av_freep(&h->slice_table_base);
  2646. av_freep(&h->top_borders[1]);
  2647. av_freep(&h->top_borders[0]);
  2648. h->slice_table= NULL;
  2649. av_freep(&h->mb2b_xy);
  2650. av_freep(&h->mb2b8_xy);
  2651. av_freep(&h->s.obmc_scratchpad);
  2652. }
  2653. static void init_dequant8_coeff_table(H264Context *h){
  2654. int i,q,x;
  2655. const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
  2656. h->dequant8_coeff[0] = h->dequant8_buffer[0];
  2657. h->dequant8_coeff[1] = h->dequant8_buffer[1];
  2658. for(i=0; i<2; i++ ){
  2659. if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
  2660. h->dequant8_coeff[1] = h->dequant8_buffer[0];
  2661. break;
  2662. }
  2663. for(q=0; q<52; q++){
  2664. int shift = div6[q];
  2665. int idx = rem6[q];
  2666. for(x=0; x<64; x++)
  2667. h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
  2668. ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
  2669. h->pps.scaling_matrix8[i][x]) << shift;
  2670. }
  2671. }
  2672. }
  2673. static void init_dequant4_coeff_table(H264Context *h){
  2674. int i,j,q,x;
  2675. const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
  2676. for(i=0; i<6; i++ ){
  2677. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  2678. for(j=0; j<i; j++){
  2679. if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
  2680. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  2681. break;
  2682. }
  2683. }
  2684. if(j<i)
  2685. continue;
  2686. for(q=0; q<52; q++){
  2687. int shift = div6[q] + 2;
  2688. int idx = rem6[q];
  2689. for(x=0; x<16; x++)
  2690. h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
  2691. ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
  2692. h->pps.scaling_matrix4[i][x]) << shift;
  2693. }
  2694. }
  2695. }
  2696. static void init_dequant_tables(H264Context *h){
  2697. int i,x;
  2698. init_dequant4_coeff_table(h);
  2699. if(h->pps.transform_8x8_mode)
  2700. init_dequant8_coeff_table(h);
  2701. if(h->sps.transform_bypass){
  2702. for(i=0; i<6; i++)
  2703. for(x=0; x<16; x++)
  2704. h->dequant4_coeff[i][0][x] = 1<<6;
  2705. if(h->pps.transform_8x8_mode)
  2706. for(i=0; i<2; i++)
  2707. for(x=0; x<64; x++)
  2708. h->dequant8_coeff[i][0][x] = 1<<6;
  2709. }
  2710. }
  2711. /**
  2712. * allocates tables.
  2713. * needs width/height
  2714. */
  2715. static int alloc_tables(H264Context *h){
  2716. MpegEncContext * const s = &h->s;
  2717. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  2718. int x,y;
  2719. CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
  2720. CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
  2721. CHECKED_ALLOCZ(h->slice_table_base , big_mb_num * sizeof(uint8_t))
  2722. CHECKED_ALLOCZ(h->top_borders[0] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2723. CHECKED_ALLOCZ(h->top_borders[1] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2724. CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
  2725. if( h->pps.cabac ) {
  2726. CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
  2727. CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
  2728. CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t));
  2729. CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t));
  2730. }
  2731. memset(h->slice_table_base, -1, big_mb_num * sizeof(uint8_t));
  2732. h->slice_table= h->slice_table_base + s->mb_stride + 1;
  2733. CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint32_t));
  2734. CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint32_t));
  2735. for(y=0; y<s->mb_height; y++){
  2736. for(x=0; x<s->mb_width; x++){
  2737. const int mb_xy= x + y*s->mb_stride;
  2738. const int b_xy = 4*x + 4*y*h->b_stride;
  2739. const int b8_xy= 2*x + 2*y*h->b8_stride;
  2740. h->mb2b_xy [mb_xy]= b_xy;
  2741. h->mb2b8_xy[mb_xy]= b8_xy;
  2742. }
  2743. }
  2744. s->obmc_scratchpad = NULL;
  2745. if(!h->dequant4_coeff[0])
  2746. init_dequant_tables(h);
  2747. return 0;
  2748. fail:
  2749. free_tables(h);
  2750. return -1;
  2751. }
  2752. static void common_init(H264Context *h){
  2753. MpegEncContext * const s = &h->s;
  2754. s->width = s->avctx->width;
  2755. s->height = s->avctx->height;
  2756. s->codec_id= s->avctx->codec->id;
  2757. init_pred_ptrs(h);
  2758. h->dequant_coeff_pps= -1;
  2759. s->unrestricted_mv=1;
  2760. s->decode=1; //FIXME
  2761. memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  2762. memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  2763. }
  2764. static int decode_init(AVCodecContext *avctx){
  2765. H264Context *h= avctx->priv_data;
  2766. MpegEncContext * const s = &h->s;
  2767. MPV_decode_defaults(s);
  2768. s->avctx = avctx;
  2769. common_init(h);
  2770. s->out_format = FMT_H264;
  2771. s->workaround_bugs= avctx->workaround_bugs;
  2772. // set defaults
  2773. // s->decode_mb= ff_h263_decode_mb;
  2774. s->low_delay= 1;
  2775. avctx->pix_fmt= PIX_FMT_YUV420P;
  2776. decode_init_vlc(h);
  2777. if(avctx->extradata_size > 0 && avctx->extradata &&
  2778. *(char *)avctx->extradata == 1){
  2779. h->is_avc = 1;
  2780. h->got_avcC = 0;
  2781. } else {
  2782. h->is_avc = 0;
  2783. }
  2784. return 0;
  2785. }
  2786. static int frame_start(H264Context *h){
  2787. MpegEncContext * const s = &h->s;
  2788. int i;
  2789. if(MPV_frame_start(s, s->avctx) < 0)
  2790. return -1;
  2791. ff_er_frame_start(s);
  2792. assert(s->linesize && s->uvlinesize);
  2793. for(i=0; i<16; i++){
  2794. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  2795. h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  2796. }
  2797. for(i=0; i<4; i++){
  2798. h->block_offset[16+i]=
  2799. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2800. h->block_offset[24+16+i]=
  2801. h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2802. }
  2803. /* can't be in alloc_tables because linesize isn't known there.
  2804. * FIXME: redo bipred weight to not require extra buffer? */
  2805. if(!s->obmc_scratchpad)
  2806. s->obmc_scratchpad = av_malloc(16*s->linesize + 2*8*s->uvlinesize);
  2807. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  2808. return 0;
  2809. }
  2810. static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  2811. MpegEncContext * const s = &h->s;
  2812. int i;
  2813. src_y -= linesize;
  2814. src_cb -= uvlinesize;
  2815. src_cr -= uvlinesize;
  2816. // There are two lines saved, the line above the the top macroblock of a pair,
  2817. // and the line above the bottom macroblock
  2818. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  2819. for(i=1; i<17; i++){
  2820. h->left_border[i]= src_y[15+i* linesize];
  2821. }
  2822. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
  2823. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
  2824. if(!(s->flags&CODEC_FLAG_GRAY)){
  2825. h->left_border[17 ]= h->top_borders[0][s->mb_x][16+7];
  2826. h->left_border[17+9]= h->top_borders[0][s->mb_x][24+7];
  2827. for(i=1; i<9; i++){
  2828. h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
  2829. h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
  2830. }
  2831. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
  2832. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
  2833. }
  2834. }
  2835. 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){
  2836. MpegEncContext * const s = &h->s;
  2837. int temp8, i;
  2838. uint64_t temp64;
  2839. int deblock_left = (s->mb_x > 0);
  2840. int deblock_top = (s->mb_y > 0);
  2841. src_y -= linesize + 1;
  2842. src_cb -= uvlinesize + 1;
  2843. src_cr -= uvlinesize + 1;
  2844. #define XCHG(a,b,t,xchg)\
  2845. t= a;\
  2846. if(xchg)\
  2847. a= b;\
  2848. b= t;
  2849. if(deblock_left){
  2850. for(i = !deblock_top; i<17; i++){
  2851. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  2852. }
  2853. }
  2854. if(deblock_top){
  2855. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  2856. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  2857. if(s->mb_x+1 < s->mb_width){
  2858. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  2859. }
  2860. }
  2861. if(!(s->flags&CODEC_FLAG_GRAY)){
  2862. if(deblock_left){
  2863. for(i = !deblock_top; i<9; i++){
  2864. XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
  2865. XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
  2866. }
  2867. }
  2868. if(deblock_top){
  2869. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  2870. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  2871. }
  2872. }
  2873. }
  2874. static inline void backup_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  2875. MpegEncContext * const s = &h->s;
  2876. int i;
  2877. src_y -= 2 * linesize;
  2878. src_cb -= 2 * uvlinesize;
  2879. src_cr -= 2 * uvlinesize;
  2880. // There are two lines saved, the line above the the top macroblock of a pair,
  2881. // and the line above the bottom macroblock
  2882. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  2883. h->left_border[1]= h->top_borders[1][s->mb_x][15];
  2884. for(i=2; i<34; i++){
  2885. h->left_border[i]= src_y[15+i* linesize];
  2886. }
  2887. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 32*linesize);
  2888. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+32*linesize);
  2889. *(uint64_t*)(h->top_borders[1][s->mb_x]+0)= *(uint64_t*)(src_y + 33*linesize);
  2890. *(uint64_t*)(h->top_borders[1][s->mb_x]+8)= *(uint64_t*)(src_y +8+33*linesize);
  2891. if(!(s->flags&CODEC_FLAG_GRAY)){
  2892. h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7];
  2893. h->left_border[34+ 1]= h->top_borders[1][s->mb_x][16+7];
  2894. h->left_border[34+18 ]= h->top_borders[0][s->mb_x][24+7];
  2895. h->left_border[34+18+1]= h->top_borders[1][s->mb_x][24+7];
  2896. for(i=2; i<18; i++){
  2897. h->left_border[i+34 ]= src_cb[7+i*uvlinesize];
  2898. h->left_border[i+34+18]= src_cr[7+i*uvlinesize];
  2899. }
  2900. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+16*uvlinesize);
  2901. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+16*uvlinesize);
  2902. *(uint64_t*)(h->top_borders[1][s->mb_x]+16)= *(uint64_t*)(src_cb+17*uvlinesize);
  2903. *(uint64_t*)(h->top_borders[1][s->mb_x]+24)= *(uint64_t*)(src_cr+17*uvlinesize);
  2904. }
  2905. }
  2906. 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){
  2907. MpegEncContext * const s = &h->s;
  2908. int temp8, i;
  2909. uint64_t temp64;
  2910. int deblock_left = (s->mb_x > 0);
  2911. int deblock_top = (s->mb_y > 0);
  2912. 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);
  2913. src_y -= 2 * linesize + 1;
  2914. src_cb -= 2 * uvlinesize + 1;
  2915. src_cr -= 2 * uvlinesize + 1;
  2916. #define XCHG(a,b,t,xchg)\
  2917. t= a;\
  2918. if(xchg)\
  2919. a= b;\
  2920. b= t;
  2921. if(deblock_left){
  2922. for(i = (!deblock_top)<<1; i<34; i++){
  2923. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  2924. }
  2925. }
  2926. if(deblock_top){
  2927. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  2928. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  2929. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+0), *(uint64_t*)(src_y +1 +linesize), temp64, xchg);
  2930. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+8), *(uint64_t*)(src_y +9 +linesize), temp64, 1);
  2931. }
  2932. if(!(s->flags&CODEC_FLAG_GRAY)){
  2933. if(deblock_left){
  2934. for(i = (!deblock_top) << 1; i<18; i++){
  2935. XCHG(h->left_border[i+34 ], src_cb[i*uvlinesize], temp8, xchg);
  2936. XCHG(h->left_border[i+34+18], src_cr[i*uvlinesize], temp8, xchg);
  2937. }
  2938. }
  2939. if(deblock_top){
  2940. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  2941. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  2942. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+16), *(uint64_t*)(src_cb+1 +uvlinesize), temp64, 1);
  2943. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+24), *(uint64_t*)(src_cr+1 +uvlinesize), temp64, 1);
  2944. }
  2945. }
  2946. }
  2947. static void hl_decode_mb(H264Context *h){
  2948. MpegEncContext * const s = &h->s;
  2949. const int mb_x= s->mb_x;
  2950. const int mb_y= s->mb_y;
  2951. const int mb_xy= mb_x + mb_y*s->mb_stride;
  2952. const int mb_type= s->current_picture.mb_type[mb_xy];
  2953. uint8_t *dest_y, *dest_cb, *dest_cr;
  2954. int linesize, uvlinesize /*dct_offset*/;
  2955. int i;
  2956. int *block_offset = &h->block_offset[0];
  2957. const unsigned int bottom = mb_y & 1;
  2958. const int transform_bypass = (s->qscale == 0 && h->sps.transform_bypass);
  2959. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  2960. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  2961. if(!s->decode)
  2962. return;
  2963. dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  2964. dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  2965. dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  2966. if (h->mb_field_decoding_flag) {
  2967. linesize = s->linesize * 2;
  2968. uvlinesize = s->uvlinesize * 2;
  2969. block_offset = &h->block_offset[24];
  2970. if(mb_y&1){ //FIXME move out of this func?
  2971. dest_y -= s->linesize*15;
  2972. dest_cb-= s->uvlinesize*7;
  2973. dest_cr-= s->uvlinesize*7;
  2974. }
  2975. } else {
  2976. linesize = s->linesize;
  2977. uvlinesize = s->uvlinesize;
  2978. // dct_offset = s->linesize * 16;
  2979. }
  2980. if(transform_bypass){
  2981. idct_dc_add =
  2982. idct_add = IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  2983. }else if(IS_8x8DCT(mb_type)){
  2984. idct_dc_add = s->dsp.h264_idct8_dc_add;
  2985. idct_add = s->dsp.h264_idct8_add;
  2986. }else{
  2987. idct_dc_add = s->dsp.h264_idct_dc_add;
  2988. idct_add = s->dsp.h264_idct_add;
  2989. }
  2990. if (IS_INTRA_PCM(mb_type)) {
  2991. unsigned int x, y;
  2992. // The pixels are stored in h->mb array in the same order as levels,
  2993. // copy them in output in the correct order.
  2994. for(i=0; i<16; i++) {
  2995. for (y=0; y<4; y++) {
  2996. for (x=0; x<4; x++) {
  2997. *(dest_y + block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x];
  2998. }
  2999. }
  3000. }
  3001. for(i=16; i<16+4; i++) {
  3002. for (y=0; y<4; y++) {
  3003. for (x=0; x<4; x++) {
  3004. *(dest_cb + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  3005. }
  3006. }
  3007. }
  3008. for(i=20; i<20+4; i++) {
  3009. for (y=0; y<4; y++) {
  3010. for (x=0; x<4; x++) {
  3011. *(dest_cr + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  3012. }
  3013. }
  3014. }
  3015. } else {
  3016. if(IS_INTRA(mb_type)){
  3017. if(h->deblocking_filter) {
  3018. if (h->mb_aff_frame) {
  3019. if (!bottom)
  3020. xchg_pair_border(h, dest_y, dest_cb, dest_cr, s->linesize, s->uvlinesize, 1);
  3021. } else {
  3022. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1);
  3023. }
  3024. }
  3025. if(!(s->flags&CODEC_FLAG_GRAY)){
  3026. h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  3027. h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  3028. }
  3029. if(IS_INTRA4x4(mb_type)){
  3030. if(!s->encoding){
  3031. if(IS_8x8DCT(mb_type)){
  3032. for(i=0; i<16; i+=4){
  3033. uint8_t * const ptr= dest_y + block_offset[i];
  3034. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  3035. const int nnz = h->non_zero_count_cache[ scan8[i] ];
  3036. h->pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  3037. (h->topright_samples_available<<(i+1))&0x8000, linesize);
  3038. if(nnz){
  3039. if(nnz == 1 && h->mb[i*16])
  3040. idct_dc_add(ptr, h->mb + i*16, linesize);
  3041. else
  3042. idct_add(ptr, h->mb + i*16, linesize);
  3043. }
  3044. }
  3045. }else
  3046. for(i=0; i<16; i++){
  3047. uint8_t * const ptr= dest_y + block_offset[i];
  3048. uint8_t *topright;
  3049. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  3050. int nnz, tr;
  3051. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  3052. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  3053. assert(mb_y || linesize <= block_offset[i]);
  3054. if(!topright_avail){
  3055. tr= ptr[3 - linesize]*0x01010101;
  3056. topright= (uint8_t*) &tr;
  3057. }else
  3058. topright= ptr + 4 - linesize;
  3059. }else
  3060. topright= NULL;
  3061. h->pred4x4[ dir ](ptr, topright, linesize);
  3062. nnz = h->non_zero_count_cache[ scan8[i] ];
  3063. if(nnz){
  3064. if(s->codec_id == CODEC_ID_H264){
  3065. if(nnz == 1 && h->mb[i*16])
  3066. idct_dc_add(ptr, h->mb + i*16, linesize);
  3067. else
  3068. idct_add(ptr, h->mb + i*16, linesize);
  3069. }else
  3070. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  3071. }
  3072. }
  3073. }
  3074. }else{
  3075. h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  3076. if(s->codec_id == CODEC_ID_H264){
  3077. if(!transform_bypass)
  3078. h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[IS_INTRA(mb_type) ? 0:3][s->qscale][0]);
  3079. }else
  3080. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  3081. }
  3082. if(h->deblocking_filter) {
  3083. if (h->mb_aff_frame) {
  3084. if (bottom) {
  3085. uint8_t *pair_dest_y = s->current_picture.data[0] + ((mb_y-1) * 16* s->linesize ) + mb_x * 16;
  3086. uint8_t *pair_dest_cb = s->current_picture.data[1] + ((mb_y-1) * 8 * s->uvlinesize) + mb_x * 8;
  3087. uint8_t *pair_dest_cr = s->current_picture.data[2] + ((mb_y-1) * 8 * s->uvlinesize) + mb_x * 8;
  3088. s->mb_y--;
  3089. xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0);
  3090. s->mb_y++;
  3091. }
  3092. } else {
  3093. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
  3094. }
  3095. }
  3096. }else if(s->codec_id == CODEC_ID_H264){
  3097. hl_motion(h, dest_y, dest_cb, dest_cr,
  3098. s->dsp.put_h264_qpel_pixels_tab, s->dsp.put_h264_chroma_pixels_tab,
  3099. s->dsp.avg_h264_qpel_pixels_tab, s->dsp.avg_h264_chroma_pixels_tab,
  3100. s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
  3101. }
  3102. if(!IS_INTRA4x4(mb_type)){
  3103. if(s->codec_id == CODEC_ID_H264){
  3104. if(IS_INTRA16x16(mb_type)){
  3105. for(i=0; i<16; i++){
  3106. if(h->non_zero_count_cache[ scan8[i] ])
  3107. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3108. else if(h->mb[i*16])
  3109. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3110. }
  3111. }else{
  3112. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  3113. for(i=0; i<16; i+=di){
  3114. int nnz = h->non_zero_count_cache[ scan8[i] ];
  3115. if(nnz){
  3116. if(nnz==1 && h->mb[i*16])
  3117. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3118. else
  3119. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  3120. }
  3121. }
  3122. }
  3123. }else{
  3124. for(i=0; i<16; i++){
  3125. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  3126. uint8_t * const ptr= dest_y + block_offset[i];
  3127. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  3128. }
  3129. }
  3130. }
  3131. }
  3132. if(!(s->flags&CODEC_FLAG_GRAY)){
  3133. uint8_t *dest[2] = {dest_cb, dest_cr};
  3134. if(transform_bypass){
  3135. idct_add = idct_dc_add = s->dsp.add_pixels4;
  3136. }else{
  3137. idct_add = s->dsp.h264_idct_add;
  3138. idct_dc_add = s->dsp.h264_idct_dc_add;
  3139. 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]);
  3140. 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]);
  3141. }
  3142. if(s->codec_id == CODEC_ID_H264){
  3143. for(i=16; i<16+8; i++){
  3144. if(h->non_zero_count_cache[ scan8[i] ])
  3145. idct_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3146. else if(h->mb[i*16])
  3147. idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3148. }
  3149. }else{
  3150. for(i=16; i<16+8; i++){
  3151. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  3152. uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
  3153. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  3154. }
  3155. }
  3156. }
  3157. }
  3158. }
  3159. if(h->deblocking_filter) {
  3160. if (h->mb_aff_frame) {
  3161. const int mb_y = s->mb_y - 1;
  3162. uint8_t *pair_dest_y, *pair_dest_cb, *pair_dest_cr;
  3163. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3164. const int mb_type_top = s->current_picture.mb_type[mb_xy];
  3165. const int mb_type_bottom= s->current_picture.mb_type[mb_xy+s->mb_stride];
  3166. uint8_t tmp = s->current_picture.data[1][384];
  3167. if (!bottom) return;
  3168. pair_dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3169. pair_dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3170. pair_dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3171. backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize);
  3172. // TODO deblock a pair
  3173. // top
  3174. s->mb_y--;
  3175. 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);
  3176. fill_caches(h, mb_type_top, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3177. filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize);
  3178. if (tmp != s->current_picture.data[1][384]) {
  3179. tprintf("modified pixel 8,1 (1)\n");
  3180. }
  3181. // bottom
  3182. s->mb_y++;
  3183. tprintf("call mbaff filter_mb\n");
  3184. fill_caches(h, mb_type_bottom, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3185. filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3186. if (tmp != s->current_picture.data[1][384]) {
  3187. tprintf("modified pixel 8,1 (2)\n");
  3188. }
  3189. } else {
  3190. tprintf("call filter_mb\n");
  3191. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3192. fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3193. filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3194. }
  3195. }
  3196. }
  3197. /**
  3198. * fills the default_ref_list.
  3199. */
  3200. static int fill_default_ref_list(H264Context *h){
  3201. MpegEncContext * const s = &h->s;
  3202. int i;
  3203. int smallest_poc_greater_than_current = -1;
  3204. Picture sorted_short_ref[32];
  3205. if(h->slice_type==B_TYPE){
  3206. int out_i;
  3207. int limit= INT_MIN;
  3208. /* sort frame according to poc in B slice */
  3209. for(out_i=0; out_i<h->short_ref_count; out_i++){
  3210. int best_i=INT_MIN;
  3211. int best_poc=INT_MAX;
  3212. for(i=0; i<h->short_ref_count; i++){
  3213. const int poc= h->short_ref[i]->poc;
  3214. if(poc > limit && poc < best_poc){
  3215. best_poc= poc;
  3216. best_i= i;
  3217. }
  3218. }
  3219. assert(best_i != INT_MIN);
  3220. limit= best_poc;
  3221. sorted_short_ref[out_i]= *h->short_ref[best_i];
  3222. 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);
  3223. if (-1 == smallest_poc_greater_than_current) {
  3224. if (h->short_ref[best_i]->poc >= s->current_picture_ptr->poc) {
  3225. smallest_poc_greater_than_current = out_i;
  3226. }
  3227. }
  3228. }
  3229. }
  3230. if(s->picture_structure == PICT_FRAME){
  3231. if(h->slice_type==B_TYPE){
  3232. int list;
  3233. tprintf("current poc: %d, smallest_poc_greater_than_current: %d\n", s->current_picture_ptr->poc, smallest_poc_greater_than_current);
  3234. // find the largest poc
  3235. for(list=0; list<2; list++){
  3236. int index = 0;
  3237. int j= -99;
  3238. int step= list ? -1 : 1;
  3239. for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++, j+=step) {
  3240. while(j<0 || j>= h->short_ref_count){
  3241. if(j != -99 && step == (list ? -1 : 1))
  3242. return -1;
  3243. step = -step;
  3244. j= smallest_poc_greater_than_current + (step>>1);
  3245. }
  3246. if(sorted_short_ref[j].reference != 3) continue;
  3247. h->default_ref_list[list][index ]= sorted_short_ref[j];
  3248. h->default_ref_list[list][index++].pic_id= sorted_short_ref[j].frame_num;
  3249. }
  3250. for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){
  3251. if(h->long_ref[i] == NULL) continue;
  3252. if(h->long_ref[i]->reference != 3) continue;
  3253. h->default_ref_list[ list ][index ]= *h->long_ref[i];
  3254. h->default_ref_list[ list ][index++].pic_id= i;;
  3255. }
  3256. if(list && (smallest_poc_greater_than_current<=0 || smallest_poc_greater_than_current>=h->short_ref_count) && (1 < index)){
  3257. // swap the two first elements of L1 when
  3258. // L0 and L1 are identical
  3259. Picture temp= h->default_ref_list[1][0];
  3260. h->default_ref_list[1][0] = h->default_ref_list[1][1];
  3261. h->default_ref_list[1][1] = temp;
  3262. }
  3263. if(index < h->ref_count[ list ])
  3264. memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
  3265. }
  3266. }else{
  3267. int index=0;
  3268. for(i=0; i<h->short_ref_count; i++){
  3269. if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
  3270. h->default_ref_list[0][index ]= *h->short_ref[i];
  3271. h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
  3272. }
  3273. for(i = 0; i < 16; i++){
  3274. if(h->long_ref[i] == NULL) continue;
  3275. if(h->long_ref[i]->reference != 3) continue;
  3276. h->default_ref_list[0][index ]= *h->long_ref[i];
  3277. h->default_ref_list[0][index++].pic_id= i;;
  3278. }
  3279. if(index < h->ref_count[0])
  3280. memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
  3281. }
  3282. }else{ //FIELD
  3283. if(h->slice_type==B_TYPE){
  3284. }else{
  3285. //FIXME second field balh
  3286. }
  3287. }
  3288. #ifdef TRACE
  3289. for (i=0; i<h->ref_count[0]; i++) {
  3290. 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]);
  3291. }
  3292. if(h->slice_type==B_TYPE){
  3293. for (i=0; i<h->ref_count[1]; i++) {
  3294. 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]);
  3295. }
  3296. }
  3297. #endif
  3298. return 0;
  3299. }
  3300. static void print_short_term(H264Context *h);
  3301. static void print_long_term(H264Context *h);
  3302. static int decode_ref_pic_list_reordering(H264Context *h){
  3303. MpegEncContext * const s = &h->s;
  3304. int list, index;
  3305. print_short_term(h);
  3306. print_long_term(h);
  3307. if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move before func
  3308. for(list=0; list<2; list++){
  3309. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  3310. if(get_bits1(&s->gb)){
  3311. int pred= h->curr_pic_num;
  3312. for(index=0; ; index++){
  3313. int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
  3314. int pic_id;
  3315. int i;
  3316. Picture *ref = NULL;
  3317. if(reordering_of_pic_nums_idc==3)
  3318. break;
  3319. if(index >= h->ref_count[list]){
  3320. av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
  3321. return -1;
  3322. }
  3323. if(reordering_of_pic_nums_idc<3){
  3324. if(reordering_of_pic_nums_idc<2){
  3325. const int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  3326. if(abs_diff_pic_num >= h->max_pic_num){
  3327. av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  3328. return -1;
  3329. }
  3330. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  3331. else pred+= abs_diff_pic_num;
  3332. pred &= h->max_pic_num - 1;
  3333. for(i= h->short_ref_count-1; i>=0; i--){
  3334. ref = h->short_ref[i];
  3335. assert(ref->reference == 3);
  3336. assert(!ref->long_ref);
  3337. if(ref->data[0] != NULL && ref->frame_num == pred && ref->long_ref == 0) // ignore non existing pictures by testing data[0] pointer
  3338. break;
  3339. }
  3340. if(i>=0)
  3341. ref->pic_id= ref->frame_num;
  3342. }else{
  3343. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  3344. ref = h->long_ref[pic_id];
  3345. ref->pic_id= pic_id;
  3346. assert(ref->reference == 3);
  3347. assert(ref->long_ref);
  3348. i=0;
  3349. }
  3350. if (i < 0) {
  3351. av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  3352. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  3353. } else {
  3354. for(i=index; i+1<h->ref_count[list]; i++){
  3355. if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
  3356. break;
  3357. }
  3358. for(; i > index; i--){
  3359. h->ref_list[list][i]= h->ref_list[list][i-1];
  3360. }
  3361. h->ref_list[list][index]= *ref;
  3362. }
  3363. }else{
  3364. av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  3365. return -1;
  3366. }
  3367. }
  3368. }
  3369. if(h->slice_type!=B_TYPE) break;
  3370. }
  3371. for(list=0; list<2; list++){
  3372. for(index= 0; index < h->ref_count[list]; index++){
  3373. if(!h->ref_list[list][index].data[0])
  3374. h->ref_list[list][index]= s->current_picture;
  3375. }
  3376. if(h->slice_type!=B_TYPE) break;
  3377. }
  3378. if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)
  3379. direct_dist_scale_factor(h);
  3380. direct_ref_list_init(h);
  3381. return 0;
  3382. }
  3383. static int pred_weight_table(H264Context *h){
  3384. MpegEncContext * const s = &h->s;
  3385. int list, i;
  3386. int luma_def, chroma_def;
  3387. h->use_weight= 0;
  3388. h->use_weight_chroma= 0;
  3389. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  3390. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  3391. luma_def = 1<<h->luma_log2_weight_denom;
  3392. chroma_def = 1<<h->chroma_log2_weight_denom;
  3393. for(list=0; list<2; list++){
  3394. for(i=0; i<h->ref_count[list]; i++){
  3395. int luma_weight_flag, chroma_weight_flag;
  3396. luma_weight_flag= get_bits1(&s->gb);
  3397. if(luma_weight_flag){
  3398. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  3399. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  3400. if( h->luma_weight[list][i] != luma_def
  3401. || h->luma_offset[list][i] != 0)
  3402. h->use_weight= 1;
  3403. }else{
  3404. h->luma_weight[list][i]= luma_def;
  3405. h->luma_offset[list][i]= 0;
  3406. }
  3407. chroma_weight_flag= get_bits1(&s->gb);
  3408. if(chroma_weight_flag){
  3409. int j;
  3410. for(j=0; j<2; j++){
  3411. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  3412. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  3413. if( h->chroma_weight[list][i][j] != chroma_def
  3414. || h->chroma_offset[list][i][j] != 0)
  3415. h->use_weight_chroma= 1;
  3416. }
  3417. }else{
  3418. int j;
  3419. for(j=0; j<2; j++){
  3420. h->chroma_weight[list][i][j]= chroma_def;
  3421. h->chroma_offset[list][i][j]= 0;
  3422. }
  3423. }
  3424. }
  3425. if(h->slice_type != B_TYPE) break;
  3426. }
  3427. h->use_weight= h->use_weight || h->use_weight_chroma;
  3428. return 0;
  3429. }
  3430. static void implicit_weight_table(H264Context *h){
  3431. MpegEncContext * const s = &h->s;
  3432. int ref0, ref1;
  3433. int cur_poc = s->current_picture_ptr->poc;
  3434. if( h->ref_count[0] == 1 && h->ref_count[1] == 1
  3435. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  3436. h->use_weight= 0;
  3437. h->use_weight_chroma= 0;
  3438. return;
  3439. }
  3440. h->use_weight= 2;
  3441. h->use_weight_chroma= 2;
  3442. h->luma_log2_weight_denom= 5;
  3443. h->chroma_log2_weight_denom= 5;
  3444. /* FIXME: MBAFF */
  3445. for(ref0=0; ref0 < h->ref_count[0]; ref0++){
  3446. int poc0 = h->ref_list[0][ref0].poc;
  3447. for(ref1=0; ref1 < h->ref_count[1]; ref1++){
  3448. int poc1 = h->ref_list[1][ref1].poc;
  3449. int td = clip(poc1 - poc0, -128, 127);
  3450. if(td){
  3451. int tb = clip(cur_poc - poc0, -128, 127);
  3452. int tx = (16384 + (ABS(td) >> 1)) / td;
  3453. int dist_scale_factor = clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
  3454. if(dist_scale_factor < -64 || dist_scale_factor > 128)
  3455. h->implicit_weight[ref0][ref1] = 32;
  3456. else
  3457. h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
  3458. }else
  3459. h->implicit_weight[ref0][ref1] = 32;
  3460. }
  3461. }
  3462. }
  3463. static inline void unreference_pic(H264Context *h, Picture *pic){
  3464. int i;
  3465. pic->reference=0;
  3466. if(pic == h->delayed_output_pic)
  3467. pic->reference=1;
  3468. else{
  3469. for(i = 0; h->delayed_pic[i]; i++)
  3470. if(pic == h->delayed_pic[i]){
  3471. pic->reference=1;
  3472. break;
  3473. }
  3474. }
  3475. }
  3476. /**
  3477. * instantaneous decoder refresh.
  3478. */
  3479. static void idr(H264Context *h){
  3480. int i;
  3481. for(i=0; i<16; i++){
  3482. if (h->long_ref[i] != NULL) {
  3483. unreference_pic(h, h->long_ref[i]);
  3484. h->long_ref[i]= NULL;
  3485. }
  3486. }
  3487. h->long_ref_count=0;
  3488. for(i=0; i<h->short_ref_count; i++){
  3489. unreference_pic(h, h->short_ref[i]);
  3490. h->short_ref[i]= NULL;
  3491. }
  3492. h->short_ref_count=0;
  3493. }
  3494. /* forget old pics after a seek */
  3495. static void flush_dpb(AVCodecContext *avctx){
  3496. H264Context *h= avctx->priv_data;
  3497. int i;
  3498. for(i=0; i<16; i++) {
  3499. if(h->delayed_pic[i])
  3500. h->delayed_pic[i]->reference= 0;
  3501. h->delayed_pic[i]= NULL;
  3502. }
  3503. if(h->delayed_output_pic)
  3504. h->delayed_output_pic->reference= 0;
  3505. h->delayed_output_pic= NULL;
  3506. idr(h);
  3507. if(h->s.current_picture_ptr)
  3508. h->s.current_picture_ptr->reference= 0;
  3509. }
  3510. /**
  3511. *
  3512. * @return the removed picture or NULL if an error occurs
  3513. */
  3514. static Picture * remove_short(H264Context *h, int frame_num){
  3515. MpegEncContext * const s = &h->s;
  3516. int i;
  3517. if(s->avctx->debug&FF_DEBUG_MMCO)
  3518. av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  3519. for(i=0; i<h->short_ref_count; i++){
  3520. Picture *pic= h->short_ref[i];
  3521. if(s->avctx->debug&FF_DEBUG_MMCO)
  3522. av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  3523. if(pic->frame_num == frame_num){
  3524. h->short_ref[i]= NULL;
  3525. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
  3526. h->short_ref_count--;
  3527. return pic;
  3528. }
  3529. }
  3530. return NULL;
  3531. }
  3532. /**
  3533. *
  3534. * @return the removed picture or NULL if an error occurs
  3535. */
  3536. static Picture * remove_long(H264Context *h, int i){
  3537. Picture *pic;
  3538. pic= h->long_ref[i];
  3539. h->long_ref[i]= NULL;
  3540. if(pic) h->long_ref_count--;
  3541. return pic;
  3542. }
  3543. /**
  3544. * print short term list
  3545. */
  3546. static void print_short_term(H264Context *h) {
  3547. uint32_t i;
  3548. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3549. av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
  3550. for(i=0; i<h->short_ref_count; i++){
  3551. Picture *pic= h->short_ref[i];
  3552. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3553. }
  3554. }
  3555. }
  3556. /**
  3557. * print long term list
  3558. */
  3559. static void print_long_term(H264Context *h) {
  3560. uint32_t i;
  3561. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3562. av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
  3563. for(i = 0; i < 16; i++){
  3564. Picture *pic= h->long_ref[i];
  3565. if (pic) {
  3566. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3567. }
  3568. }
  3569. }
  3570. }
  3571. /**
  3572. * Executes the reference picture marking (memory management control operations).
  3573. */
  3574. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  3575. MpegEncContext * const s = &h->s;
  3576. int i, j;
  3577. int current_is_long=0;
  3578. Picture *pic;
  3579. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  3580. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  3581. for(i=0; i<mmco_count; i++){
  3582. if(s->avctx->debug&FF_DEBUG_MMCO)
  3583. 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);
  3584. switch(mmco[i].opcode){
  3585. case MMCO_SHORT2UNUSED:
  3586. pic= remove_short(h, mmco[i].short_frame_num);
  3587. if(pic)
  3588. unreference_pic(h, pic);
  3589. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3590. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_short() failure\n");
  3591. break;
  3592. case MMCO_SHORT2LONG:
  3593. pic= remove_long(h, mmco[i].long_index);
  3594. if(pic) unreference_pic(h, pic);
  3595. h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
  3596. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3597. h->long_ref_count++;
  3598. break;
  3599. case MMCO_LONG2UNUSED:
  3600. pic= remove_long(h, mmco[i].long_index);
  3601. if(pic)
  3602. unreference_pic(h, pic);
  3603. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3604. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_long() failure\n");
  3605. break;
  3606. case MMCO_LONG:
  3607. pic= remove_long(h, mmco[i].long_index);
  3608. if(pic) unreference_pic(h, pic);
  3609. h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
  3610. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3611. h->long_ref_count++;
  3612. current_is_long=1;
  3613. break;
  3614. case MMCO_SET_MAX_LONG:
  3615. assert(mmco[i].long_index <= 16);
  3616. // just remove the long term which index is greater than new max
  3617. for(j = mmco[i].long_index; j<16; j++){
  3618. pic = remove_long(h, j);
  3619. if (pic) unreference_pic(h, pic);
  3620. }
  3621. break;
  3622. case MMCO_RESET:
  3623. while(h->short_ref_count){
  3624. pic= remove_short(h, h->short_ref[0]->frame_num);
  3625. unreference_pic(h, pic);
  3626. }
  3627. for(j = 0; j < 16; j++) {
  3628. pic= remove_long(h, j);
  3629. if(pic) unreference_pic(h, pic);
  3630. }
  3631. break;
  3632. default: assert(0);
  3633. }
  3634. }
  3635. if(!current_is_long){
  3636. pic= remove_short(h, s->current_picture_ptr->frame_num);
  3637. if(pic){
  3638. unreference_pic(h, pic);
  3639. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  3640. }
  3641. if(h->short_ref_count)
  3642. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  3643. h->short_ref[0]= s->current_picture_ptr;
  3644. h->short_ref[0]->long_ref=0;
  3645. h->short_ref_count++;
  3646. }
  3647. print_short_term(h);
  3648. print_long_term(h);
  3649. return 0;
  3650. }
  3651. static int decode_ref_pic_marking(H264Context *h){
  3652. MpegEncContext * const s = &h->s;
  3653. int i;
  3654. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  3655. s->broken_link= get_bits1(&s->gb) -1;
  3656. h->mmco[0].long_index= get_bits1(&s->gb) - 1; // current_long_term_idx
  3657. if(h->mmco[0].long_index == -1)
  3658. h->mmco_index= 0;
  3659. else{
  3660. h->mmco[0].opcode= MMCO_LONG;
  3661. h->mmco_index= 1;
  3662. }
  3663. }else{
  3664. if(get_bits1(&s->gb)){ // adaptive_ref_pic_marking_mode_flag
  3665. for(i= 0; i<MAX_MMCO_COUNT; i++) {
  3666. MMCOOpcode opcode= get_ue_golomb(&s->gb);;
  3667. h->mmco[i].opcode= opcode;
  3668. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  3669. 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
  3670. /* if(h->mmco[i].short_frame_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_frame_num ] == NULL){
  3671. av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
  3672. return -1;
  3673. }*/
  3674. }
  3675. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  3676. h->mmco[i].long_index= get_ue_golomb(&s->gb);
  3677. 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){
  3678. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  3679. return -1;
  3680. }
  3681. }
  3682. if(opcode > MMCO_LONG){
  3683. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  3684. return -1;
  3685. }
  3686. if(opcode == MMCO_END)
  3687. break;
  3688. }
  3689. h->mmco_index= i;
  3690. }else{
  3691. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  3692. if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){ //FIXME fields
  3693. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  3694. h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  3695. h->mmco_index= 1;
  3696. }else
  3697. h->mmco_index= 0;
  3698. }
  3699. }
  3700. return 0;
  3701. }
  3702. static int init_poc(H264Context *h){
  3703. MpegEncContext * const s = &h->s;
  3704. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  3705. int field_poc[2];
  3706. if(h->nal_unit_type == NAL_IDR_SLICE){
  3707. h->frame_num_offset= 0;
  3708. }else{
  3709. if(h->frame_num < h->prev_frame_num)
  3710. h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
  3711. else
  3712. h->frame_num_offset= h->prev_frame_num_offset;
  3713. }
  3714. if(h->sps.poc_type==0){
  3715. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  3716. if(h->nal_unit_type == NAL_IDR_SLICE){
  3717. h->prev_poc_msb=
  3718. h->prev_poc_lsb= 0;
  3719. }
  3720. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  3721. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  3722. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  3723. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  3724. else
  3725. h->poc_msb = h->prev_poc_msb;
  3726. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  3727. field_poc[0] =
  3728. field_poc[1] = h->poc_msb + h->poc_lsb;
  3729. if(s->picture_structure == PICT_FRAME)
  3730. field_poc[1] += h->delta_poc_bottom;
  3731. }else if(h->sps.poc_type==1){
  3732. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  3733. int i;
  3734. if(h->sps.poc_cycle_length != 0)
  3735. abs_frame_num = h->frame_num_offset + h->frame_num;
  3736. else
  3737. abs_frame_num = 0;
  3738. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  3739. abs_frame_num--;
  3740. expected_delta_per_poc_cycle = 0;
  3741. for(i=0; i < h->sps.poc_cycle_length; i++)
  3742. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  3743. if(abs_frame_num > 0){
  3744. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  3745. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  3746. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  3747. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  3748. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  3749. } else
  3750. expectedpoc = 0;
  3751. if(h->nal_ref_idc == 0)
  3752. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  3753. field_poc[0] = expectedpoc + h->delta_poc[0];
  3754. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  3755. if(s->picture_structure == PICT_FRAME)
  3756. field_poc[1] += h->delta_poc[1];
  3757. }else{
  3758. int poc;
  3759. if(h->nal_unit_type == NAL_IDR_SLICE){
  3760. poc= 0;
  3761. }else{
  3762. if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
  3763. else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
  3764. }
  3765. field_poc[0]= poc;
  3766. field_poc[1]= poc;
  3767. }
  3768. if(s->picture_structure != PICT_BOTTOM_FIELD)
  3769. s->current_picture_ptr->field_poc[0]= field_poc[0];
  3770. if(s->picture_structure != PICT_TOP_FIELD)
  3771. s->current_picture_ptr->field_poc[1]= field_poc[1];
  3772. if(s->picture_structure == PICT_FRAME) // FIXME field pix?
  3773. s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]);
  3774. return 0;
  3775. }
  3776. /**
  3777. * decodes a slice header.
  3778. * this will allso call MPV_common_init() and frame_start() as needed
  3779. */
  3780. static int decode_slice_header(H264Context *h){
  3781. MpegEncContext * const s = &h->s;
  3782. int first_mb_in_slice, pps_id;
  3783. int num_ref_idx_active_override_flag;
  3784. static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
  3785. int slice_type;
  3786. int default_ref_list_done = 0;
  3787. s->current_picture.reference= h->nal_ref_idc != 0;
  3788. s->dropable= h->nal_ref_idc == 0;
  3789. first_mb_in_slice= get_ue_golomb(&s->gb);
  3790. slice_type= get_ue_golomb(&s->gb);
  3791. if(slice_type > 9){
  3792. 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);
  3793. return -1;
  3794. }
  3795. if(slice_type > 4){
  3796. slice_type -= 5;
  3797. h->slice_type_fixed=1;
  3798. }else
  3799. h->slice_type_fixed=0;
  3800. slice_type= slice_type_map[ slice_type ];
  3801. if (slice_type == I_TYPE
  3802. || (h->slice_num != 0 && slice_type == h->slice_type) ) {
  3803. default_ref_list_done = 1;
  3804. }
  3805. h->slice_type= slice_type;
  3806. s->pict_type= h->slice_type; // to make a few old func happy, it's wrong though
  3807. pps_id= get_ue_golomb(&s->gb);
  3808. if(pps_id>255){
  3809. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  3810. return -1;
  3811. }
  3812. h->pps= h->pps_buffer[pps_id];
  3813. if(h->pps.slice_group_count == 0){
  3814. av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referenced\n");
  3815. return -1;
  3816. }
  3817. h->sps= h->sps_buffer[ h->pps.sps_id ];
  3818. if(h->sps.log2_max_frame_num == 0){
  3819. av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referenced\n");
  3820. return -1;
  3821. }
  3822. if(h->dequant_coeff_pps != pps_id){
  3823. h->dequant_coeff_pps = pps_id;
  3824. init_dequant_tables(h);
  3825. }
  3826. s->mb_width= h->sps.mb_width;
  3827. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  3828. h->b_stride= s->mb_width*4;
  3829. h->b8_stride= s->mb_width*2;
  3830. s->width = 16*s->mb_width - 2*(h->sps.crop_left + h->sps.crop_right );
  3831. if(h->sps.frame_mbs_only_flag)
  3832. s->height= 16*s->mb_height - 2*(h->sps.crop_top + h->sps.crop_bottom);
  3833. else
  3834. s->height= 16*s->mb_height - 4*(h->sps.crop_top + h->sps.crop_bottom); //FIXME recheck
  3835. if (s->context_initialized
  3836. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  3837. free_tables(h);
  3838. MPV_common_end(s);
  3839. }
  3840. if (!s->context_initialized) {
  3841. if (MPV_common_init(s) < 0)
  3842. return -1;
  3843. if(s->dsp.h264_idct_add == ff_h264_idct_add_c){ //FIXME little ugly
  3844. memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
  3845. memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
  3846. }else{
  3847. int i;
  3848. for(i=0; i<16; i++){
  3849. #define T(x) (x>>2) | ((x<<2) & 0xF)
  3850. h->zigzag_scan[i] = T(zigzag_scan[i]);
  3851. h-> field_scan[i] = T( field_scan[i]);
  3852. #undef T
  3853. }
  3854. }
  3855. if(s->dsp.h264_idct8_add == ff_h264_idct8_add_c){
  3856. memcpy(h->zigzag_scan8x8, zigzag_scan8x8, 64*sizeof(uint8_t));
  3857. memcpy(h->zigzag_scan8x8_cavlc, zigzag_scan8x8_cavlc, 64*sizeof(uint8_t));
  3858. }else{
  3859. int i;
  3860. for(i=0; i<64; i++){
  3861. #define T(x) (x>>3) | ((x&7)<<3)
  3862. h->zigzag_scan8x8[i] = T(zigzag_scan8x8[i]);
  3863. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  3864. #undef T
  3865. }
  3866. }
  3867. if(h->sps.transform_bypass){ //FIXME same ugly
  3868. h->zigzag_scan_q0 = zigzag_scan;
  3869. h->field_scan_q0 = field_scan;
  3870. h->zigzag_scan8x8_q0 = zigzag_scan8x8;
  3871. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  3872. }else{
  3873. h->zigzag_scan_q0 = h->zigzag_scan;
  3874. h->field_scan_q0 = h->field_scan;
  3875. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  3876. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  3877. }
  3878. alloc_tables(h);
  3879. s->avctx->width = s->width;
  3880. s->avctx->height = s->height;
  3881. s->avctx->sample_aspect_ratio= h->sps.sar;
  3882. if(!s->avctx->sample_aspect_ratio.den)
  3883. s->avctx->sample_aspect_ratio.den = 1;
  3884. if(h->sps.timing_info_present_flag){
  3885. s->avctx->time_base= (AVRational){h->sps.num_units_in_tick * 2, h->sps.time_scale};
  3886. if(h->x264_build > 0 && h->x264_build < 44)
  3887. s->avctx->time_base.den *= 2;
  3888. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  3889. s->avctx->time_base.num, s->avctx->time_base.den, 1<<30);
  3890. }
  3891. }
  3892. if(h->slice_num == 0){
  3893. if(frame_start(h) < 0)
  3894. return -1;
  3895. }
  3896. s->current_picture_ptr->frame_num= //FIXME frame_num cleanup
  3897. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  3898. h->mb_aff_frame = 0;
  3899. if(h->sps.frame_mbs_only_flag){
  3900. s->picture_structure= PICT_FRAME;
  3901. }else{
  3902. if(get_bits1(&s->gb)) { //field_pic_flag
  3903. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  3904. } else {
  3905. s->picture_structure= PICT_FRAME;
  3906. first_mb_in_slice <<= h->sps.mb_aff;
  3907. h->mb_aff_frame = h->sps.mb_aff;
  3908. }
  3909. }
  3910. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  3911. s->resync_mb_y = s->mb_y = first_mb_in_slice / s->mb_width;
  3912. if(s->mb_y >= s->mb_height){
  3913. return -1;
  3914. }
  3915. if(s->picture_structure==PICT_FRAME){
  3916. h->curr_pic_num= h->frame_num;
  3917. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  3918. }else{
  3919. h->curr_pic_num= 2*h->frame_num;
  3920. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  3921. }
  3922. if(h->nal_unit_type == NAL_IDR_SLICE){
  3923. get_ue_golomb(&s->gb); /* idr_pic_id */
  3924. }
  3925. if(h->sps.poc_type==0){
  3926. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  3927. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  3928. h->delta_poc_bottom= get_se_golomb(&s->gb);
  3929. }
  3930. }
  3931. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  3932. h->delta_poc[0]= get_se_golomb(&s->gb);
  3933. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  3934. h->delta_poc[1]= get_se_golomb(&s->gb);
  3935. }
  3936. init_poc(h);
  3937. if(h->pps.redundant_pic_cnt_present){
  3938. h->redundant_pic_count= get_ue_golomb(&s->gb);
  3939. }
  3940. //set defaults, might be overriden a few line later
  3941. h->ref_count[0]= h->pps.ref_count[0];
  3942. h->ref_count[1]= h->pps.ref_count[1];
  3943. if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){
  3944. if(h->slice_type == B_TYPE){
  3945. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  3946. }
  3947. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  3948. if(num_ref_idx_active_override_flag){
  3949. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  3950. if(h->slice_type==B_TYPE)
  3951. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  3952. if(h->ref_count[0] > 32 || h->ref_count[1] > 32){
  3953. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  3954. return -1;
  3955. }
  3956. }
  3957. }
  3958. if(!default_ref_list_done){
  3959. fill_default_ref_list(h);
  3960. }
  3961. if(decode_ref_pic_list_reordering(h) < 0)
  3962. return -1;
  3963. if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
  3964. || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
  3965. pred_weight_table(h);
  3966. else if(h->pps.weighted_bipred_idc==2 && h->slice_type==B_TYPE)
  3967. implicit_weight_table(h);
  3968. else
  3969. h->use_weight = 0;
  3970. if(s->current_picture.reference)
  3971. decode_ref_pic_marking(h);
  3972. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac )
  3973. h->cabac_init_idc = get_ue_golomb(&s->gb);
  3974. h->last_qscale_diff = 0;
  3975. s->qscale = h->pps.init_qp + get_se_golomb(&s->gb);
  3976. if(s->qscale<0 || s->qscale>51){
  3977. av_log(s->avctx, AV_LOG_ERROR, "QP %d out of range\n", s->qscale);
  3978. return -1;
  3979. }
  3980. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  3981. //FIXME qscale / qp ... stuff
  3982. if(h->slice_type == SP_TYPE){
  3983. get_bits1(&s->gb); /* sp_for_switch_flag */
  3984. }
  3985. if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
  3986. get_se_golomb(&s->gb); /* slice_qs_delta */
  3987. }
  3988. h->deblocking_filter = 1;
  3989. h->slice_alpha_c0_offset = 0;
  3990. h->slice_beta_offset = 0;
  3991. if( h->pps.deblocking_filter_parameters_present ) {
  3992. h->deblocking_filter= get_ue_golomb(&s->gb);
  3993. if(h->deblocking_filter < 2)
  3994. h->deblocking_filter^= 1; // 1<->0
  3995. if( h->deblocking_filter ) {
  3996. h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
  3997. h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
  3998. }
  3999. }
  4000. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  4001. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type != I_TYPE)
  4002. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type == B_TYPE)
  4003. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  4004. h->deblocking_filter= 0;
  4005. #if 0 //FMO
  4006. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  4007. slice_group_change_cycle= get_bits(&s->gb, ?);
  4008. #endif
  4009. h->slice_num++;
  4010. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  4011. 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",
  4012. h->slice_num,
  4013. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  4014. first_mb_in_slice,
  4015. av_get_pict_type_char(h->slice_type),
  4016. pps_id, h->frame_num,
  4017. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  4018. h->ref_count[0], h->ref_count[1],
  4019. s->qscale,
  4020. h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2,
  4021. h->use_weight,
  4022. h->use_weight==1 && h->use_weight_chroma ? "c" : ""
  4023. );
  4024. }
  4025. return 0;
  4026. }
  4027. /**
  4028. *
  4029. */
  4030. static inline int get_level_prefix(GetBitContext *gb){
  4031. unsigned int buf;
  4032. int log;
  4033. OPEN_READER(re, gb);
  4034. UPDATE_CACHE(re, gb);
  4035. buf=GET_CACHE(re, gb);
  4036. log= 32 - av_log2(buf);
  4037. #ifdef TRACE
  4038. print_bin(buf>>(32-log), log);
  4039. 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__);
  4040. #endif
  4041. LAST_SKIP_BITS(re, gb, log);
  4042. CLOSE_READER(re, gb);
  4043. return log-1;
  4044. }
  4045. static inline int get_dct8x8_allowed(H264Context *h){
  4046. int i;
  4047. for(i=0; i<4; i++){
  4048. if(!IS_SUB_8X8(h->sub_mb_type[i])
  4049. || (!h->sps.direct_8x8_inference_flag && IS_DIRECT(h->sub_mb_type[i])))
  4050. return 0;
  4051. }
  4052. return 1;
  4053. }
  4054. /**
  4055. * decodes a residual block.
  4056. * @param n block index
  4057. * @param scantable scantable
  4058. * @param max_coeff number of coefficients in the block
  4059. * @return <0 if an error occured
  4060. */
  4061. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
  4062. MpegEncContext * const s = &h->s;
  4063. 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};
  4064. int level[16];
  4065. int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
  4066. //FIXME put trailing_onex into the context
  4067. if(n == CHROMA_DC_BLOCK_INDEX){
  4068. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  4069. total_coeff= coeff_token>>2;
  4070. }else{
  4071. if(n == LUMA_DC_BLOCK_INDEX){
  4072. total_coeff= pred_non_zero_count(h, 0);
  4073. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4074. total_coeff= coeff_token>>2;
  4075. }else{
  4076. total_coeff= pred_non_zero_count(h, n);
  4077. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4078. total_coeff= coeff_token>>2;
  4079. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  4080. }
  4081. }
  4082. //FIXME set last_non_zero?
  4083. if(total_coeff==0)
  4084. return 0;
  4085. trailing_ones= coeff_token&3;
  4086. tprintf("trailing:%d, total:%d\n", trailing_ones, total_coeff);
  4087. assert(total_coeff<=16);
  4088. for(i=0; i<trailing_ones; i++){
  4089. level[i]= 1 - 2*get_bits1(gb);
  4090. }
  4091. if(i<total_coeff) {
  4092. int level_code, mask;
  4093. int suffix_length = total_coeff > 10 && trailing_ones < 3;
  4094. int prefix= get_level_prefix(gb);
  4095. //first coefficient has suffix_length equal to 0 or 1
  4096. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  4097. if(suffix_length)
  4098. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4099. else
  4100. level_code= (prefix<<suffix_length); //part
  4101. }else if(prefix==14){
  4102. if(suffix_length)
  4103. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4104. else
  4105. level_code= prefix + get_bits(gb, 4); //part
  4106. }else if(prefix==15){
  4107. level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part
  4108. if(suffix_length==0) level_code+=15; //FIXME doesn't make (much)sense
  4109. }else{
  4110. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4111. return -1;
  4112. }
  4113. if(trailing_ones < 3) level_code += 2;
  4114. suffix_length = 1;
  4115. if(level_code > 5)
  4116. suffix_length++;
  4117. mask= -(level_code&1);
  4118. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4119. i++;
  4120. //remaining coefficients have suffix_length > 0
  4121. for(;i<total_coeff;i++) {
  4122. static const int suffix_limit[7] = {0,5,11,23,47,95,INT_MAX };
  4123. prefix = get_level_prefix(gb);
  4124. if(prefix<15){
  4125. level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
  4126. }else if(prefix==15){
  4127. level_code = (prefix<<suffix_length) + get_bits(gb, 12);
  4128. }else{
  4129. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4130. return -1;
  4131. }
  4132. mask= -(level_code&1);
  4133. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4134. if(level_code > suffix_limit[suffix_length])
  4135. suffix_length++;
  4136. }
  4137. }
  4138. if(total_coeff == max_coeff)
  4139. zeros_left=0;
  4140. else{
  4141. if(n == CHROMA_DC_BLOCK_INDEX)
  4142. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  4143. else
  4144. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  4145. }
  4146. coeff_num = zeros_left + total_coeff - 1;
  4147. j = scantable[coeff_num];
  4148. if(n > 24){
  4149. block[j] = level[0];
  4150. for(i=1;i<total_coeff;i++) {
  4151. if(zeros_left <= 0)
  4152. run_before = 0;
  4153. else if(zeros_left < 7){
  4154. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4155. }else{
  4156. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4157. }
  4158. zeros_left -= run_before;
  4159. coeff_num -= 1 + run_before;
  4160. j= scantable[ coeff_num ];
  4161. block[j]= level[i];
  4162. }
  4163. }else{
  4164. block[j] = (level[0] * qmul[j] + 32)>>6;
  4165. for(i=1;i<total_coeff;i++) {
  4166. if(zeros_left <= 0)
  4167. run_before = 0;
  4168. else if(zeros_left < 7){
  4169. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4170. }else{
  4171. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4172. }
  4173. zeros_left -= run_before;
  4174. coeff_num -= 1 + run_before;
  4175. j= scantable[ coeff_num ];
  4176. block[j]= (level[i] * qmul[j] + 32)>>6;
  4177. }
  4178. }
  4179. if(zeros_left<0){
  4180. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  4181. return -1;
  4182. }
  4183. return 0;
  4184. }
  4185. /**
  4186. * decodes a P_SKIP or B_SKIP macroblock
  4187. */
  4188. static void decode_mb_skip(H264Context *h){
  4189. MpegEncContext * const s = &h->s;
  4190. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4191. int mb_type=0;
  4192. memset(h->non_zero_count[mb_xy], 0, 16);
  4193. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  4194. if(h->mb_aff_frame && s->mb_skip_run==0 && (s->mb_y&1)==0){
  4195. h->mb_field_decoding_flag= get_bits1(&s->gb);
  4196. }
  4197. if(h->mb_field_decoding_flag)
  4198. mb_type|= MB_TYPE_INTERLACED;
  4199. if( h->slice_type == B_TYPE )
  4200. {
  4201. // just for fill_caches. pred_direct_motion will set the real mb_type
  4202. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
  4203. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4204. pred_direct_motion(h, &mb_type);
  4205. if(h->pps.cabac){
  4206. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  4207. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  4208. }
  4209. }
  4210. else
  4211. {
  4212. int mx, my;
  4213. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
  4214. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4215. pred_pskip_motion(h, &mx, &my);
  4216. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  4217. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  4218. if(h->pps.cabac)
  4219. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  4220. }
  4221. write_back_motion(h, mb_type);
  4222. s->current_picture.mb_type[mb_xy]= mb_type|MB_TYPE_SKIP;
  4223. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4224. h->slice_table[ mb_xy ]= h->slice_num;
  4225. h->prev_mb_skipped= 1;
  4226. }
  4227. /**
  4228. * decodes a macroblock
  4229. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  4230. */
  4231. static int decode_mb_cavlc(H264Context *h){
  4232. MpegEncContext * const s = &h->s;
  4233. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4234. int mb_type, partition_count, cbp;
  4235. int dct8x8_allowed= h->pps.transform_8x8_mode;
  4236. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?
  4237. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  4238. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  4239. down the code */
  4240. if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){
  4241. if(s->mb_skip_run==-1)
  4242. s->mb_skip_run= get_ue_golomb(&s->gb);
  4243. if (s->mb_skip_run--) {
  4244. decode_mb_skip(h);
  4245. return 0;
  4246. }
  4247. }
  4248. if(h->mb_aff_frame){
  4249. if ( ((s->mb_y&1) == 0) || h->prev_mb_skipped)
  4250. h->mb_field_decoding_flag = get_bits1(&s->gb);
  4251. }else
  4252. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  4253. h->prev_mb_skipped= 0;
  4254. mb_type= get_ue_golomb(&s->gb);
  4255. if(h->slice_type == B_TYPE){
  4256. if(mb_type < 23){
  4257. partition_count= b_mb_type_info[mb_type].partition_count;
  4258. mb_type= b_mb_type_info[mb_type].type;
  4259. }else{
  4260. mb_type -= 23;
  4261. goto decode_intra_mb;
  4262. }
  4263. }else if(h->slice_type == P_TYPE /*|| h->slice_type == SP_TYPE */){
  4264. if(mb_type < 5){
  4265. partition_count= p_mb_type_info[mb_type].partition_count;
  4266. mb_type= p_mb_type_info[mb_type].type;
  4267. }else{
  4268. mb_type -= 5;
  4269. goto decode_intra_mb;
  4270. }
  4271. }else{
  4272. assert(h->slice_type == I_TYPE);
  4273. decode_intra_mb:
  4274. if(mb_type > 25){
  4275. av_log(h->s.avctx, AV_LOG_ERROR, "mb_type %d in %c slice to large at %d %d\n", mb_type, av_get_pict_type_char(h->slice_type), s->mb_x, s->mb_y);
  4276. return -1;
  4277. }
  4278. partition_count=0;
  4279. cbp= i_mb_type_info[mb_type].cbp;
  4280. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  4281. mb_type= i_mb_type_info[mb_type].type;
  4282. }
  4283. if(h->mb_field_decoding_flag)
  4284. mb_type |= MB_TYPE_INTERLACED;
  4285. h->slice_table[ mb_xy ]= h->slice_num;
  4286. if(IS_INTRA_PCM(mb_type)){
  4287. unsigned int x, y;
  4288. // we assume these blocks are very rare so we dont optimize it
  4289. align_get_bits(&s->gb);
  4290. // The pixels are stored in the same order as levels in h->mb array.
  4291. for(y=0; y<16; y++){
  4292. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  4293. for(x=0; x<16; x++){
  4294. tprintf("LUMA ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4295. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= get_bits(&s->gb, 8);
  4296. }
  4297. }
  4298. for(y=0; y<8; y++){
  4299. const int index= 256 + 4*(y&3) + 32*(y>>2);
  4300. for(x=0; x<8; x++){
  4301. tprintf("CHROMA U ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4302. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4303. }
  4304. }
  4305. for(y=0; y<8; y++){
  4306. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  4307. for(x=0; x<8; x++){
  4308. tprintf("CHROMA V ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4309. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4310. }
  4311. }
  4312. // In deblocking, the quantizer is 0
  4313. s->current_picture.qscale_table[mb_xy]= 0;
  4314. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  4315. // All coeffs are present
  4316. memset(h->non_zero_count[mb_xy], 16, 16);
  4317. s->current_picture.mb_type[mb_xy]= mb_type;
  4318. return 0;
  4319. }
  4320. fill_caches(h, mb_type, 0);
  4321. //mb_pred
  4322. if(IS_INTRA(mb_type)){
  4323. // init_top_left_availability(h);
  4324. if(IS_INTRA4x4(mb_type)){
  4325. int i;
  4326. int di = 1;
  4327. if(dct8x8_allowed && get_bits1(&s->gb)){
  4328. mb_type |= MB_TYPE_8x8DCT;
  4329. di = 4;
  4330. }
  4331. // fill_intra4x4_pred_table(h);
  4332. for(i=0; i<16; i+=di){
  4333. const int mode_coded= !get_bits1(&s->gb);
  4334. const int predicted_mode= pred_intra_mode(h, i);
  4335. int mode;
  4336. if(mode_coded){
  4337. const int rem_mode= get_bits(&s->gb, 3);
  4338. if(rem_mode<predicted_mode)
  4339. mode= rem_mode;
  4340. else
  4341. mode= rem_mode + 1;
  4342. }else{
  4343. mode= predicted_mode;
  4344. }
  4345. if(di==4)
  4346. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  4347. else
  4348. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  4349. }
  4350. write_back_intra_pred_mode(h);
  4351. if( check_intra4x4_pred_mode(h) < 0)
  4352. return -1;
  4353. }else{
  4354. h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
  4355. if(h->intra16x16_pred_mode < 0)
  4356. return -1;
  4357. }
  4358. h->chroma_pred_mode= get_ue_golomb(&s->gb);
  4359. h->chroma_pred_mode= check_intra_pred_mode(h, h->chroma_pred_mode);
  4360. if(h->chroma_pred_mode < 0)
  4361. return -1;
  4362. }else if(partition_count==4){
  4363. int i, j, sub_partition_count[4], list, ref[2][4];
  4364. if(h->slice_type == B_TYPE){
  4365. for(i=0; i<4; i++){
  4366. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4367. if(h->sub_mb_type[i] >=13){
  4368. 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);
  4369. return -1;
  4370. }
  4371. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4372. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4373. }
  4374. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  4375. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
  4376. pred_direct_motion(h, &mb_type);
  4377. h->ref_cache[0][scan8[4]] =
  4378. h->ref_cache[1][scan8[4]] =
  4379. h->ref_cache[0][scan8[12]] =
  4380. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  4381. }
  4382. }else{
  4383. assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE); //FIXME SP correct ?
  4384. for(i=0; i<4; i++){
  4385. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4386. if(h->sub_mb_type[i] >=4){
  4387. 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);
  4388. return -1;
  4389. }
  4390. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4391. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4392. }
  4393. }
  4394. for(list=0; list<2; list++){
  4395. int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4396. if(ref_count == 0) continue;
  4397. if (h->mb_aff_frame && h->mb_field_decoding_flag) {
  4398. ref_count <<= 1;
  4399. }
  4400. for(i=0; i<4; i++){
  4401. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  4402. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4403. ref[list][i] = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
  4404. }else{
  4405. //FIXME
  4406. ref[list][i] = -1;
  4407. }
  4408. }
  4409. }
  4410. if(dct8x8_allowed)
  4411. dct8x8_allowed = get_dct8x8_allowed(h);
  4412. for(list=0; list<2; list++){
  4413. const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4414. if(ref_count == 0) continue;
  4415. for(i=0; i<4; i++){
  4416. if(IS_DIRECT(h->sub_mb_type[i])) {
  4417. h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];
  4418. continue;
  4419. }
  4420. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  4421. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  4422. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4423. const int sub_mb_type= h->sub_mb_type[i];
  4424. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  4425. for(j=0; j<sub_partition_count[i]; j++){
  4426. int mx, my;
  4427. const int index= 4*i + block_width*j;
  4428. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  4429. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  4430. mx += get_se_golomb(&s->gb);
  4431. my += get_se_golomb(&s->gb);
  4432. tprintf("final mv:%d %d\n", mx, my);
  4433. if(IS_SUB_8X8(sub_mb_type)){
  4434. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  4435. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  4436. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  4437. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  4438. }else if(IS_SUB_8X4(sub_mb_type)){
  4439. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  4440. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  4441. }else if(IS_SUB_4X8(sub_mb_type)){
  4442. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  4443. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  4444. }else{
  4445. assert(IS_SUB_4X4(sub_mb_type));
  4446. mv_cache[ 0 ][0]= mx;
  4447. mv_cache[ 0 ][1]= my;
  4448. }
  4449. }
  4450. }else{
  4451. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  4452. p[0] = p[1]=
  4453. p[8] = p[9]= 0;
  4454. }
  4455. }
  4456. }
  4457. }else if(IS_DIRECT(mb_type)){
  4458. pred_direct_motion(h, &mb_type);
  4459. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  4460. }else{
  4461. int list, mx, my, i;
  4462. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  4463. if(IS_16X16(mb_type)){
  4464. for(list=0; list<2; list++){
  4465. if(h->ref_count[list]>0){
  4466. if(IS_DIR(mb_type, 0, list)){
  4467. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4468. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  4469. }else
  4470. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4471. }
  4472. }
  4473. for(list=0; list<2; list++){
  4474. if(IS_DIR(mb_type, 0, list)){
  4475. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  4476. mx += get_se_golomb(&s->gb);
  4477. my += get_se_golomb(&s->gb);
  4478. tprintf("final mv:%d %d\n", mx, my);
  4479. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  4480. }else
  4481. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  4482. }
  4483. }
  4484. else if(IS_16X8(mb_type)){
  4485. for(list=0; list<2; list++){
  4486. if(h->ref_count[list]>0){
  4487. for(i=0; i<2; i++){
  4488. if(IS_DIR(mb_type, i, list)){
  4489. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4490. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  4491. }else
  4492. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  4493. }
  4494. }
  4495. }
  4496. for(list=0; list<2; list++){
  4497. for(i=0; i<2; i++){
  4498. if(IS_DIR(mb_type, i, list)){
  4499. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  4500. mx += get_se_golomb(&s->gb);
  4501. my += get_se_golomb(&s->gb);
  4502. tprintf("final mv:%d %d\n", mx, my);
  4503. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  4504. }else
  4505. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  4506. }
  4507. }
  4508. }else{
  4509. assert(IS_8X16(mb_type));
  4510. for(list=0; list<2; list++){
  4511. if(h->ref_count[list]>0){
  4512. for(i=0; i<2; i++){
  4513. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  4514. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4515. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  4516. }else
  4517. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4518. }
  4519. }
  4520. }
  4521. for(list=0; list<2; list++){
  4522. for(i=0; i<2; i++){
  4523. if(IS_DIR(mb_type, i, list)){
  4524. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  4525. mx += get_se_golomb(&s->gb);
  4526. my += get_se_golomb(&s->gb);
  4527. tprintf("final mv:%d %d\n", mx, my);
  4528. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  4529. }else
  4530. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  4531. }
  4532. }
  4533. }
  4534. }
  4535. if(IS_INTER(mb_type))
  4536. write_back_motion(h, mb_type);
  4537. if(!IS_INTRA16x16(mb_type)){
  4538. cbp= get_ue_golomb(&s->gb);
  4539. if(cbp > 47){
  4540. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%d) at %d %d\n", cbp, s->mb_x, s->mb_y);
  4541. return -1;
  4542. }
  4543. if(IS_INTRA4x4(mb_type))
  4544. cbp= golomb_to_intra4x4_cbp[cbp];
  4545. else
  4546. cbp= golomb_to_inter_cbp[cbp];
  4547. }
  4548. if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
  4549. if(get_bits1(&s->gb))
  4550. mb_type |= MB_TYPE_8x8DCT;
  4551. }
  4552. s->current_picture.mb_type[mb_xy]= mb_type;
  4553. if(cbp || IS_INTRA16x16(mb_type)){
  4554. int i8x8, i4x4, chroma_idx;
  4555. int chroma_qp, dquant;
  4556. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  4557. const uint8_t *scan, *scan8x8, *dc_scan;
  4558. // fill_non_zero_count_cache(h);
  4559. if(IS_INTERLACED(mb_type)){
  4560. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  4561. dc_scan= luma_dc_field_scan;
  4562. }else{
  4563. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  4564. dc_scan= luma_dc_zigzag_scan;
  4565. }
  4566. scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
  4567. dquant= get_se_golomb(&s->gb);
  4568. if( dquant > 25 || dquant < -26 ){
  4569. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  4570. return -1;
  4571. }
  4572. s->qscale += dquant;
  4573. if(((unsigned)s->qscale) > 51){
  4574. if(s->qscale<0) s->qscale+= 52;
  4575. else s->qscale-= 52;
  4576. }
  4577. h->chroma_qp= chroma_qp= get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4578. if(IS_INTRA16x16(mb_type)){
  4579. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){
  4580. return -1; //FIXME continue if partitioned and other return -1 too
  4581. }
  4582. assert((cbp&15) == 0 || (cbp&15) == 15);
  4583. if(cbp&15){
  4584. for(i8x8=0; i8x8<4; i8x8++){
  4585. for(i4x4=0; i4x4<4; i4x4++){
  4586. const int index= i4x4 + 4*i8x8;
  4587. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){
  4588. return -1;
  4589. }
  4590. }
  4591. }
  4592. }else{
  4593. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  4594. }
  4595. }else{
  4596. for(i8x8=0; i8x8<4; i8x8++){
  4597. if(cbp & (1<<i8x8)){
  4598. if(IS_8x8DCT(mb_type)){
  4599. DCTELEM *buf = &h->mb[64*i8x8];
  4600. uint8_t *nnz;
  4601. for(i4x4=0; i4x4<4; i4x4++){
  4602. if( decode_residual(h, gb, buf, i4x4+4*i8x8, scan8x8+16*i4x4,
  4603. h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )
  4604. return -1;
  4605. }
  4606. nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4607. nnz[0] += nnz[1] + nnz[8] + nnz[9];
  4608. }else{
  4609. for(i4x4=0; i4x4<4; i4x4++){
  4610. const int index= i4x4 + 4*i8x8;
  4611. if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){
  4612. return -1;
  4613. }
  4614. }
  4615. }
  4616. }else{
  4617. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4618. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  4619. }
  4620. }
  4621. }
  4622. if(cbp&0x30){
  4623. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  4624. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){
  4625. return -1;
  4626. }
  4627. }
  4628. if(cbp&0x20){
  4629. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  4630. for(i4x4=0; i4x4<4; i4x4++){
  4631. const int index= 16 + 4*chroma_idx + i4x4;
  4632. 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){
  4633. return -1;
  4634. }
  4635. }
  4636. }
  4637. }else{
  4638. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4639. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4640. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4641. }
  4642. }else{
  4643. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4644. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  4645. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4646. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4647. }
  4648. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4649. write_back_non_zero_count(h);
  4650. return 0;
  4651. }
  4652. static int decode_cabac_field_decoding_flag(H264Context *h) {
  4653. MpegEncContext * const s = &h->s;
  4654. const int mb_x = s->mb_x;
  4655. const int mb_y = s->mb_y & ~1;
  4656. const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
  4657. const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
  4658. unsigned int ctx = 0;
  4659. if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
  4660. ctx += 1;
  4661. }
  4662. if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
  4663. ctx += 1;
  4664. }
  4665. return get_cabac( &h->cabac, &h->cabac_state[70 + ctx] );
  4666. }
  4667. static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
  4668. uint8_t *state= &h->cabac_state[ctx_base];
  4669. int mb_type;
  4670. if(intra_slice){
  4671. MpegEncContext * const s = &h->s;
  4672. const int mba_xy = h->left_mb_xy[0];
  4673. const int mbb_xy = h->top_mb_xy;
  4674. int ctx=0;
  4675. if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
  4676. ctx++;
  4677. if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
  4678. ctx++;
  4679. if( get_cabac( &h->cabac, &state[ctx] ) == 0 )
  4680. return 0; /* I4x4 */
  4681. state += 2;
  4682. }else{
  4683. if( get_cabac( &h->cabac, &state[0] ) == 0 )
  4684. return 0; /* I4x4 */
  4685. }
  4686. if( get_cabac_terminate( &h->cabac ) )
  4687. return 25; /* PCM */
  4688. mb_type = 1; /* I16x16 */
  4689. mb_type += 12 * get_cabac( &h->cabac, &state[1] ); /* cbp_luma != 0 */
  4690. if( get_cabac( &h->cabac, &state[2] ) ) /* cbp_chroma */
  4691. mb_type += 4 + 4 * get_cabac( &h->cabac, &state[2+intra_slice] );
  4692. mb_type += 2 * get_cabac( &h->cabac, &state[3+intra_slice] );
  4693. mb_type += 1 * get_cabac( &h->cabac, &state[3+2*intra_slice] );
  4694. return mb_type;
  4695. }
  4696. static int decode_cabac_mb_type( H264Context *h ) {
  4697. MpegEncContext * const s = &h->s;
  4698. if( h->slice_type == I_TYPE ) {
  4699. return decode_cabac_intra_mb_type(h, 3, 1);
  4700. } else if( h->slice_type == P_TYPE ) {
  4701. if( get_cabac( &h->cabac, &h->cabac_state[14] ) == 0 ) {
  4702. /* P-type */
  4703. if( get_cabac( &h->cabac, &h->cabac_state[15] ) == 0 ) {
  4704. /* P_L0_D16x16, P_8x8 */
  4705. return 3 * get_cabac( &h->cabac, &h->cabac_state[16] );
  4706. } else {
  4707. /* P_L0_D8x16, P_L0_D16x8 */
  4708. return 2 - get_cabac( &h->cabac, &h->cabac_state[17] );
  4709. }
  4710. } else {
  4711. return decode_cabac_intra_mb_type(h, 17, 0) + 5;
  4712. }
  4713. } else if( h->slice_type == B_TYPE ) {
  4714. const int mba_xy = h->left_mb_xy[0];
  4715. const int mbb_xy = h->top_mb_xy;
  4716. int ctx = 0;
  4717. int bits;
  4718. if( h->slice_table[mba_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
  4719. ctx++;
  4720. if( h->slice_table[mbb_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
  4721. ctx++;
  4722. if( !get_cabac( &h->cabac, &h->cabac_state[27+ctx] ) )
  4723. return 0; /* B_Direct_16x16 */
  4724. if( !get_cabac( &h->cabac, &h->cabac_state[27+3] ) ) {
  4725. return 1 + get_cabac( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
  4726. }
  4727. bits = get_cabac( &h->cabac, &h->cabac_state[27+4] ) << 3;
  4728. bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] ) << 2;
  4729. bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] ) << 1;
  4730. bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] );
  4731. if( bits < 8 )
  4732. return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
  4733. else if( bits == 13 ) {
  4734. return decode_cabac_intra_mb_type(h, 32, 0) + 23;
  4735. } else if( bits == 14 )
  4736. return 11; /* B_L1_L0_8x16 */
  4737. else if( bits == 15 )
  4738. return 22; /* B_8x8 */
  4739. bits= ( bits<<1 ) | get_cabac( &h->cabac, &h->cabac_state[27+5] );
  4740. return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
  4741. } else {
  4742. /* TODO SI/SP frames? */
  4743. return -1;
  4744. }
  4745. }
  4746. static int decode_cabac_mb_skip( H264Context *h) {
  4747. MpegEncContext * const s = &h->s;
  4748. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  4749. const int mba_xy = mb_xy - 1;
  4750. const int mbb_xy = mb_xy - s->mb_stride;
  4751. int ctx = 0;
  4752. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
  4753. ctx++;
  4754. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
  4755. ctx++;
  4756. if( h->slice_type == B_TYPE )
  4757. ctx += 13;
  4758. return get_cabac( &h->cabac, &h->cabac_state[11+ctx] );
  4759. }
  4760. static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
  4761. int mode = 0;
  4762. if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
  4763. return pred_mode;
  4764. mode += 1 * get_cabac( &h->cabac, &h->cabac_state[69] );
  4765. mode += 2 * get_cabac( &h->cabac, &h->cabac_state[69] );
  4766. mode += 4 * get_cabac( &h->cabac, &h->cabac_state[69] );
  4767. if( mode >= pred_mode )
  4768. return mode + 1;
  4769. else
  4770. return mode;
  4771. }
  4772. static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
  4773. const int mba_xy = h->left_mb_xy[0];
  4774. const int mbb_xy = h->top_mb_xy;
  4775. int ctx = 0;
  4776. /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
  4777. if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
  4778. ctx++;
  4779. if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
  4780. ctx++;
  4781. if( get_cabac( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
  4782. return 0;
  4783. if( get_cabac( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  4784. return 1;
  4785. if( get_cabac( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  4786. return 2;
  4787. else
  4788. return 3;
  4789. }
  4790. static const uint8_t block_idx_x[16] = {
  4791. 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
  4792. };
  4793. static const uint8_t block_idx_y[16] = {
  4794. 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
  4795. };
  4796. static const uint8_t block_idx_xy[4][4] = {
  4797. { 0, 2, 8, 10},
  4798. { 1, 3, 9, 11},
  4799. { 4, 6, 12, 14},
  4800. { 5, 7, 13, 15}
  4801. };
  4802. static int decode_cabac_mb_cbp_luma( H264Context *h) {
  4803. int cbp = 0;
  4804. int cbp_b = -1;
  4805. int i8x8;
  4806. if( h->slice_table[h->top_mb_xy] == h->slice_num ) {
  4807. cbp_b = h->top_cbp;
  4808. tprintf("cbp_b = top_cbp = %x\n", cbp_b);
  4809. }
  4810. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  4811. int cbp_a = -1;
  4812. int x, y;
  4813. int ctx = 0;
  4814. x = block_idx_x[4*i8x8];
  4815. y = block_idx_y[4*i8x8];
  4816. if( x > 0 )
  4817. cbp_a = cbp;
  4818. else if( h->slice_table[h->left_mb_xy[0]] == h->slice_num ) {
  4819. cbp_a = h->left_cbp;
  4820. tprintf("cbp_a = left_cbp = %x\n", cbp_a);
  4821. }
  4822. if( y > 0 )
  4823. cbp_b = cbp;
  4824. /* No need to test for skip as we put 0 for skip block */
  4825. /* No need to test for IPCM as we put 1 for IPCM block */
  4826. if( cbp_a >= 0 ) {
  4827. int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
  4828. if( ((cbp_a >> i8x8a)&0x01) == 0 )
  4829. ctx++;
  4830. }
  4831. if( cbp_b >= 0 ) {
  4832. int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
  4833. if( ((cbp_b >> i8x8b)&0x01) == 0 )
  4834. ctx += 2;
  4835. }
  4836. if( get_cabac( &h->cabac, &h->cabac_state[73 + ctx] ) ) {
  4837. cbp |= 1 << i8x8;
  4838. }
  4839. }
  4840. return cbp;
  4841. }
  4842. static int decode_cabac_mb_cbp_chroma( H264Context *h) {
  4843. int ctx;
  4844. int cbp_a, cbp_b;
  4845. cbp_a = (h->left_cbp>>4)&0x03;
  4846. cbp_b = (h-> top_cbp>>4)&0x03;
  4847. ctx = 0;
  4848. if( cbp_a > 0 ) ctx++;
  4849. if( cbp_b > 0 ) ctx += 2;
  4850. if( get_cabac( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
  4851. return 0;
  4852. ctx = 4;
  4853. if( cbp_a == 2 ) ctx++;
  4854. if( cbp_b == 2 ) ctx += 2;
  4855. return 1 + get_cabac( &h->cabac, &h->cabac_state[77 + ctx] );
  4856. }
  4857. static int decode_cabac_mb_dqp( H264Context *h) {
  4858. MpegEncContext * const s = &h->s;
  4859. int mbn_xy;
  4860. int ctx = 0;
  4861. int val = 0;
  4862. if( s->mb_x > 0 )
  4863. mbn_xy = s->mb_x + s->mb_y*s->mb_stride - 1;
  4864. else
  4865. mbn_xy = s->mb_width - 1 + (s->mb_y-1)*s->mb_stride;
  4866. if( h->last_qscale_diff != 0 && ( IS_INTRA16x16(s->current_picture.mb_type[mbn_xy] ) || (h->cbp_table[mbn_xy]&0x3f) ) )
  4867. ctx++;
  4868. while( get_cabac( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
  4869. if( ctx < 2 )
  4870. ctx = 2;
  4871. else
  4872. ctx = 3;
  4873. val++;
  4874. if(val > 102) //prevent infinite loop
  4875. return INT_MIN;
  4876. }
  4877. if( val&0x01 )
  4878. return (val + 1)/2;
  4879. else
  4880. return -(val + 1)/2;
  4881. }
  4882. static int decode_cabac_p_mb_sub_type( H264Context *h ) {
  4883. if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
  4884. return 0; /* 8x8 */
  4885. if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
  4886. return 1; /* 8x4 */
  4887. if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
  4888. return 2; /* 4x8 */
  4889. return 3; /* 4x4 */
  4890. }
  4891. static int decode_cabac_b_mb_sub_type( H264Context *h ) {
  4892. int type;
  4893. if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
  4894. return 0; /* B_Direct_8x8 */
  4895. if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
  4896. return 1 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */
  4897. type = 3;
  4898. if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
  4899. if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
  4900. return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
  4901. type += 4;
  4902. }
  4903. type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
  4904. type += get_cabac( &h->cabac, &h->cabac_state[39] );
  4905. return type;
  4906. }
  4907. static inline int decode_cabac_mb_transform_size( H264Context *h ) {
  4908. return get_cabac( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
  4909. }
  4910. static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
  4911. int refa = h->ref_cache[list][scan8[n] - 1];
  4912. int refb = h->ref_cache[list][scan8[n] - 8];
  4913. int ref = 0;
  4914. int ctx = 0;
  4915. if( h->slice_type == B_TYPE) {
  4916. if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
  4917. ctx++;
  4918. if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
  4919. ctx += 2;
  4920. } else {
  4921. if( refa > 0 )
  4922. ctx++;
  4923. if( refb > 0 )
  4924. ctx += 2;
  4925. }
  4926. while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
  4927. ref++;
  4928. if( ctx < 4 )
  4929. ctx = 4;
  4930. else
  4931. ctx = 5;
  4932. }
  4933. return ref;
  4934. }
  4935. static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
  4936. int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
  4937. abs( h->mvd_cache[list][scan8[n] - 8][l] );
  4938. int ctxbase = (l == 0) ? 40 : 47;
  4939. int ctx, mvd;
  4940. if( amvd < 3 )
  4941. ctx = 0;
  4942. else if( amvd > 32 )
  4943. ctx = 2;
  4944. else
  4945. ctx = 1;
  4946. if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
  4947. return 0;
  4948. mvd= 1;
  4949. ctx= 3;
  4950. while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
  4951. mvd++;
  4952. if( ctx < 6 )
  4953. ctx++;
  4954. }
  4955. if( mvd >= 9 ) {
  4956. int k = 3;
  4957. while( get_cabac_bypass( &h->cabac ) ) {
  4958. mvd += 1 << k;
  4959. k++;
  4960. }
  4961. while( k-- ) {
  4962. if( get_cabac_bypass( &h->cabac ) )
  4963. mvd += 1 << k;
  4964. }
  4965. }
  4966. if( get_cabac_bypass( &h->cabac ) ) return -mvd;
  4967. else return mvd;
  4968. }
  4969. static int inline get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) {
  4970. int nza, nzb;
  4971. int ctx = 0;
  4972. if( cat == 0 ) {
  4973. nza = h->left_cbp&0x100;
  4974. nzb = h-> top_cbp&0x100;
  4975. } else if( cat == 1 || cat == 2 ) {
  4976. nza = h->non_zero_count_cache[scan8[idx] - 1];
  4977. nzb = h->non_zero_count_cache[scan8[idx] - 8];
  4978. } else if( cat == 3 ) {
  4979. nza = (h->left_cbp>>(6+idx))&0x01;
  4980. nzb = (h-> top_cbp>>(6+idx))&0x01;
  4981. } else {
  4982. assert(cat == 4);
  4983. nza = h->non_zero_count_cache[scan8[16+idx] - 1];
  4984. nzb = h->non_zero_count_cache[scan8[16+idx] - 8];
  4985. }
  4986. if( nza > 0 )
  4987. ctx++;
  4988. if( nzb > 0 )
  4989. ctx += 2;
  4990. return ctx + 4 * cat;
  4991. }
  4992. static int decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff) {
  4993. const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
  4994. static const int significant_coeff_flag_field_offset[2] = { 105, 277 };
  4995. static const int last_significant_coeff_flag_field_offset[2] = { 166, 338 };
  4996. static const int significant_coeff_flag_offset[6] = { 0, 15, 29, 44, 47, 297 };
  4997. static const int last_significant_coeff_flag_offset[6] = { 0, 15, 29, 44, 47, 251 };
  4998. static const int coeff_abs_level_m1_offset[6] = { 227+0, 227+10, 227+20, 227+30, 227+39, 426 };
  4999. static const int significant_coeff_flag_offset_8x8[63] = {
  5000. 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
  5001. 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
  5002. 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
  5003. 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12
  5004. };
  5005. static const int last_coeff_flag_offset_8x8[63] = {
  5006. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5007. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  5008. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  5009. 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
  5010. };
  5011. int index[64];
  5012. int i, last;
  5013. int coeff_count = 0;
  5014. int abslevel1 = 1;
  5015. int abslevelgt1 = 0;
  5016. uint8_t *significant_coeff_ctx_base;
  5017. uint8_t *last_coeff_ctx_base;
  5018. uint8_t *abs_level_m1_ctx_base;
  5019. /* cat: 0-> DC 16x16 n = 0
  5020. * 1-> AC 16x16 n = luma4x4idx
  5021. * 2-> Luma4x4 n = luma4x4idx
  5022. * 3-> DC Chroma n = iCbCr
  5023. * 4-> AC Chroma n = 4 * iCbCr + chroma4x4idx
  5024. * 5-> Luma8x8 n = 4 * luma8x8idx
  5025. */
  5026. /* read coded block flag */
  5027. if( cat != 5 ) {
  5028. if( get_cabac( &h->cabac, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n ) ] ) == 0 ) {
  5029. if( cat == 1 || cat == 2 )
  5030. h->non_zero_count_cache[scan8[n]] = 0;
  5031. else if( cat == 4 )
  5032. h->non_zero_count_cache[scan8[16+n]] = 0;
  5033. return 0;
  5034. }
  5035. }
  5036. significant_coeff_ctx_base = h->cabac_state
  5037. + significant_coeff_flag_offset[cat]
  5038. + significant_coeff_flag_field_offset[h->mb_field_decoding_flag];
  5039. last_coeff_ctx_base = h->cabac_state
  5040. + last_significant_coeff_flag_offset[cat]
  5041. + last_significant_coeff_flag_field_offset[h->mb_field_decoding_flag];
  5042. abs_level_m1_ctx_base = h->cabac_state
  5043. + coeff_abs_level_m1_offset[cat];
  5044. if( cat == 5 ) {
  5045. #define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \
  5046. for(last= 0; last < coefs; last++) { \
  5047. uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \
  5048. if( get_cabac( &h->cabac, sig_ctx )) { \
  5049. uint8_t *last_ctx = last_coeff_ctx_base + last_off; \
  5050. index[coeff_count++] = last; \
  5051. if( get_cabac( &h->cabac, last_ctx ) ) { \
  5052. last= max_coeff; \
  5053. break; \
  5054. } \
  5055. } \
  5056. }
  5057. DECODE_SIGNIFICANCE( 63, significant_coeff_flag_offset_8x8[last],
  5058. last_coeff_flag_offset_8x8[last] );
  5059. } else {
  5060. DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
  5061. }
  5062. if( last == max_coeff -1 ) {
  5063. index[coeff_count++] = last;
  5064. }
  5065. assert(coeff_count > 0);
  5066. if( cat == 0 )
  5067. h->cbp_table[mb_xy] |= 0x100;
  5068. else if( cat == 1 || cat == 2 )
  5069. h->non_zero_count_cache[scan8[n]] = coeff_count;
  5070. else if( cat == 3 )
  5071. h->cbp_table[mb_xy] |= 0x40 << n;
  5072. else if( cat == 4 )
  5073. h->non_zero_count_cache[scan8[16+n]] = coeff_count;
  5074. else {
  5075. assert( cat == 5 );
  5076. fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);
  5077. }
  5078. for( i = coeff_count - 1; i >= 0; i-- ) {
  5079. uint8_t *ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 )) + abs_level_m1_ctx_base;
  5080. int j= scantable[index[i]];
  5081. if( get_cabac( &h->cabac, ctx ) == 0 ) {
  5082. if( !qmul ) {
  5083. if( get_cabac_bypass( &h->cabac ) ) block[j] = -1;
  5084. else block[j] = 1;
  5085. }else{
  5086. if( get_cabac_bypass( &h->cabac ) ) block[j] = (-qmul[j] + 32) >> 6;
  5087. else block[j] = ( qmul[j] + 32) >> 6;
  5088. }
  5089. abslevel1++;
  5090. } else {
  5091. int coeff_abs = 2;
  5092. ctx = 5 + FFMIN( 4, abslevelgt1 ) + abs_level_m1_ctx_base;
  5093. while( coeff_abs < 15 && get_cabac( &h->cabac, ctx ) ) {
  5094. coeff_abs++;
  5095. }
  5096. if( coeff_abs >= 15 ) {
  5097. int j = 0;
  5098. while( get_cabac_bypass( &h->cabac ) ) {
  5099. coeff_abs += 1 << j;
  5100. j++;
  5101. }
  5102. while( j-- ) {
  5103. if( get_cabac_bypass( &h->cabac ) )
  5104. coeff_abs += 1 << j ;
  5105. }
  5106. }
  5107. if( !qmul ) {
  5108. if( get_cabac_bypass( &h->cabac ) ) block[j] = -coeff_abs;
  5109. else block[j] = coeff_abs;
  5110. }else{
  5111. if( get_cabac_bypass( &h->cabac ) ) block[j] = (-coeff_abs * qmul[j] + 32) >> 6;
  5112. else block[j] = ( coeff_abs * qmul[j] + 32) >> 6;
  5113. }
  5114. abslevelgt1++;
  5115. }
  5116. }
  5117. return 0;
  5118. }
  5119. static void inline compute_mb_neighbors(H264Context *h)
  5120. {
  5121. MpegEncContext * const s = &h->s;
  5122. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  5123. h->top_mb_xy = mb_xy - s->mb_stride;
  5124. h->left_mb_xy[0] = mb_xy - 1;
  5125. if(h->mb_aff_frame){
  5126. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  5127. const int top_pair_xy = pair_xy - s->mb_stride;
  5128. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  5129. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  5130. const int curr_mb_frame_flag = !h->mb_field_decoding_flag;
  5131. const int bottom = (s->mb_y & 1);
  5132. if (bottom
  5133. ? !curr_mb_frame_flag // bottom macroblock
  5134. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  5135. ) {
  5136. h->top_mb_xy -= s->mb_stride;
  5137. }
  5138. if (left_mb_frame_flag != curr_mb_frame_flag) {
  5139. h->left_mb_xy[0] = pair_xy - 1;
  5140. }
  5141. }
  5142. return;
  5143. }
  5144. /**
  5145. * decodes a macroblock
  5146. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  5147. */
  5148. static int decode_mb_cabac(H264Context *h) {
  5149. MpegEncContext * const s = &h->s;
  5150. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  5151. int mb_type, partition_count, cbp = 0;
  5152. int dct8x8_allowed= h->pps.transform_8x8_mode;
  5153. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?)
  5154. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  5155. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) {
  5156. /* read skip flags */
  5157. if( decode_cabac_mb_skip( h ) ) {
  5158. decode_mb_skip(h);
  5159. h->cbp_table[mb_xy] = 0;
  5160. h->chroma_pred_mode_table[mb_xy] = 0;
  5161. h->last_qscale_diff = 0;
  5162. return 0;
  5163. }
  5164. }
  5165. if(h->mb_aff_frame){
  5166. if ( ((s->mb_y&1) == 0) || h->prev_mb_skipped)
  5167. h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5168. }else
  5169. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  5170. h->prev_mb_skipped = 0;
  5171. compute_mb_neighbors(h);
  5172. if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
  5173. av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
  5174. return -1;
  5175. }
  5176. if( h->slice_type == B_TYPE ) {
  5177. if( mb_type < 23 ){
  5178. partition_count= b_mb_type_info[mb_type].partition_count;
  5179. mb_type= b_mb_type_info[mb_type].type;
  5180. }else{
  5181. mb_type -= 23;
  5182. goto decode_intra_mb;
  5183. }
  5184. } else if( h->slice_type == P_TYPE ) {
  5185. if( mb_type < 5) {
  5186. partition_count= p_mb_type_info[mb_type].partition_count;
  5187. mb_type= p_mb_type_info[mb_type].type;
  5188. } else {
  5189. mb_type -= 5;
  5190. goto decode_intra_mb;
  5191. }
  5192. } else {
  5193. assert(h->slice_type == I_TYPE);
  5194. decode_intra_mb:
  5195. partition_count = 0;
  5196. cbp= i_mb_type_info[mb_type].cbp;
  5197. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  5198. mb_type= i_mb_type_info[mb_type].type;
  5199. }
  5200. if(h->mb_field_decoding_flag)
  5201. mb_type |= MB_TYPE_INTERLACED;
  5202. h->slice_table[ mb_xy ]= h->slice_num;
  5203. if(IS_INTRA_PCM(mb_type)) {
  5204. const uint8_t *ptr;
  5205. unsigned int x, y;
  5206. // We assume these blocks are very rare so we dont optimize it.
  5207. // FIXME The two following lines get the bitstream position in the cabac
  5208. // decode, I think it should be done by a function in cabac.h (or cabac.c).
  5209. ptr= h->cabac.bytestream;
  5210. if (h->cabac.low&0x1) ptr-=CABAC_BITS/8;
  5211. // The pixels are stored in the same order as levels in h->mb array.
  5212. for(y=0; y<16; y++){
  5213. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  5214. for(x=0; x<16; x++){
  5215. tprintf("LUMA ICPM LEVEL (%3d)\n", *ptr);
  5216. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= *ptr++;
  5217. }
  5218. }
  5219. for(y=0; y<8; y++){
  5220. const int index= 256 + 4*(y&3) + 32*(y>>2);
  5221. for(x=0; x<8; x++){
  5222. tprintf("CHROMA U ICPM LEVEL (%3d)\n", *ptr);
  5223. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5224. }
  5225. }
  5226. for(y=0; y<8; y++){
  5227. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  5228. for(x=0; x<8; x++){
  5229. tprintf("CHROMA V ICPM LEVEL (%3d)\n", *ptr);
  5230. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5231. }
  5232. }
  5233. ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);
  5234. // All blocks are present
  5235. h->cbp_table[mb_xy] = 0x1ef;
  5236. h->chroma_pred_mode_table[mb_xy] = 0;
  5237. // In deblocking, the quantizer is 0
  5238. s->current_picture.qscale_table[mb_xy]= 0;
  5239. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  5240. // All coeffs are present
  5241. memset(h->non_zero_count[mb_xy], 16, 16);
  5242. s->current_picture.mb_type[mb_xy]= mb_type;
  5243. return 0;
  5244. }
  5245. fill_caches(h, mb_type, 0);
  5246. if( IS_INTRA( mb_type ) ) {
  5247. int i;
  5248. if( IS_INTRA4x4( mb_type ) ) {
  5249. if( dct8x8_allowed && decode_cabac_mb_transform_size( h ) ) {
  5250. mb_type |= MB_TYPE_8x8DCT;
  5251. for( i = 0; i < 16; i+=4 ) {
  5252. int pred = pred_intra_mode( h, i );
  5253. int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5254. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  5255. }
  5256. } else {
  5257. for( i = 0; i < 16; i++ ) {
  5258. int pred = pred_intra_mode( h, i );
  5259. h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5260. //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
  5261. }
  5262. }
  5263. write_back_intra_pred_mode(h);
  5264. if( check_intra4x4_pred_mode(h) < 0 ) return -1;
  5265. } else {
  5266. h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );
  5267. if( h->intra16x16_pred_mode < 0 ) return -1;
  5268. }
  5269. h->chroma_pred_mode_table[mb_xy] =
  5270. h->chroma_pred_mode = decode_cabac_mb_chroma_pre_mode( h );
  5271. h->chroma_pred_mode= check_intra_pred_mode( h, h->chroma_pred_mode );
  5272. if( h->chroma_pred_mode < 0 ) return -1;
  5273. } else if( partition_count == 4 ) {
  5274. int i, j, sub_partition_count[4], list, ref[2][4];
  5275. if( h->slice_type == B_TYPE ) {
  5276. for( i = 0; i < 4; i++ ) {
  5277. h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
  5278. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5279. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5280. }
  5281. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  5282. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
  5283. pred_direct_motion(h, &mb_type);
  5284. if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
  5285. for( i = 0; i < 4; i++ )
  5286. if( IS_DIRECT(h->sub_mb_type[i]) )
  5287. fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );
  5288. }
  5289. }
  5290. } else {
  5291. for( i = 0; i < 4; i++ ) {
  5292. h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
  5293. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5294. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5295. }
  5296. }
  5297. for( list = 0; list < 2; list++ ) {
  5298. if( h->ref_count[list] > 0 ) {
  5299. for( i = 0; i < 4; i++ ) {
  5300. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  5301. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  5302. if( h->ref_count[list] > 1 )
  5303. ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );
  5304. else
  5305. ref[list][i] = 0;
  5306. } else {
  5307. ref[list][i] = -1;
  5308. }
  5309. h->ref_cache[list][ scan8[4*i]+1 ]=
  5310. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  5311. }
  5312. }
  5313. }
  5314. if(dct8x8_allowed)
  5315. dct8x8_allowed = get_dct8x8_allowed(h);
  5316. for(list=0; list<2; list++){
  5317. for(i=0; i<4; i++){
  5318. if(IS_DIRECT(h->sub_mb_type[i])){
  5319. fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);
  5320. continue;
  5321. }
  5322. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];
  5323. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  5324. const int sub_mb_type= h->sub_mb_type[i];
  5325. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  5326. for(j=0; j<sub_partition_count[i]; j++){
  5327. int mpx, mpy;
  5328. int mx, my;
  5329. const int index= 4*i + block_width*j;
  5330. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  5331. int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];
  5332. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);
  5333. mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );
  5334. my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );
  5335. tprintf("final mv:%d %d\n", mx, my);
  5336. if(IS_SUB_8X8(sub_mb_type)){
  5337. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  5338. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  5339. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  5340. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  5341. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]=
  5342. mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;
  5343. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]=
  5344. mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;
  5345. }else if(IS_SUB_8X4(sub_mb_type)){
  5346. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  5347. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  5348. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]= mx- mpx;
  5349. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]= my - mpy;
  5350. }else if(IS_SUB_4X8(sub_mb_type)){
  5351. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  5352. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  5353. mvd_cache[ 0 ][0]= mvd_cache[ 8 ][0]= mx - mpx;
  5354. mvd_cache[ 0 ][1]= mvd_cache[ 8 ][1]= my - mpy;
  5355. }else{
  5356. assert(IS_SUB_4X4(sub_mb_type));
  5357. mv_cache[ 0 ][0]= mx;
  5358. mv_cache[ 0 ][1]= my;
  5359. mvd_cache[ 0 ][0]= mx - mpx;
  5360. mvd_cache[ 0 ][1]= my - mpy;
  5361. }
  5362. }
  5363. }else{
  5364. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  5365. uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];
  5366. p[0] = p[1] = p[8] = p[9] = 0;
  5367. pd[0]= pd[1]= pd[8]= pd[9]= 0;
  5368. }
  5369. }
  5370. }
  5371. } else if( IS_DIRECT(mb_type) ) {
  5372. pred_direct_motion(h, &mb_type);
  5373. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  5374. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  5375. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  5376. } else {
  5377. int list, mx, my, i, mpx, mpy;
  5378. if(IS_16X16(mb_type)){
  5379. for(list=0; list<2; list++){
  5380. if(IS_DIR(mb_type, 0, list)){
  5381. if(h->ref_count[list] > 0 ){
  5382. const int ref = h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 0 ) : 0;
  5383. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
  5384. }
  5385. }else
  5386. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  5387. }
  5388. for(list=0; list<2; list++){
  5389. if(IS_DIR(mb_type, 0, list)){
  5390. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);
  5391. mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );
  5392. my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );
  5393. tprintf("final mv:%d %d\n", mx, my);
  5394. fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5395. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  5396. }else
  5397. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  5398. }
  5399. }
  5400. else if(IS_16X8(mb_type)){
  5401. for(list=0; list<2; list++){
  5402. if(h->ref_count[list]>0){
  5403. for(i=0; i<2; i++){
  5404. if(IS_DIR(mb_type, i, list)){
  5405. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 8*i ) : 0;
  5406. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
  5407. }else
  5408. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  5409. }
  5410. }
  5411. }
  5412. for(list=0; list<2; list++){
  5413. for(i=0; i<2; i++){
  5414. if(IS_DIR(mb_type, i, list)){
  5415. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);
  5416. mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );
  5417. my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );
  5418. tprintf("final mv:%d %d\n", mx, my);
  5419. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);
  5420. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  5421. }else{
  5422. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5423. fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5424. }
  5425. }
  5426. }
  5427. }else{
  5428. assert(IS_8X16(mb_type));
  5429. for(list=0; list<2; list++){
  5430. if(h->ref_count[list]>0){
  5431. for(i=0; i<2; i++){
  5432. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  5433. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 4*i ) : 0;
  5434. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
  5435. }else
  5436. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  5437. }
  5438. }
  5439. }
  5440. for(list=0; list<2; list++){
  5441. for(i=0; i<2; i++){
  5442. if(IS_DIR(mb_type, i, list)){
  5443. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);
  5444. mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );
  5445. my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );
  5446. tprintf("final mv:%d %d\n", mx, my);
  5447. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5448. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  5449. }else{
  5450. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5451. fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5452. }
  5453. }
  5454. }
  5455. }
  5456. }
  5457. if( IS_INTER( mb_type ) ) {
  5458. h->chroma_pred_mode_table[mb_xy] = 0;
  5459. write_back_motion( h, mb_type );
  5460. }
  5461. if( !IS_INTRA16x16( mb_type ) ) {
  5462. cbp = decode_cabac_mb_cbp_luma( h );
  5463. cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
  5464. }
  5465. h->cbp_table[mb_xy] = cbp;
  5466. if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
  5467. if( decode_cabac_mb_transform_size( h ) )
  5468. mb_type |= MB_TYPE_8x8DCT;
  5469. }
  5470. s->current_picture.mb_type[mb_xy]= mb_type;
  5471. if( cbp || IS_INTRA16x16( mb_type ) ) {
  5472. const uint8_t *scan, *scan8x8, *dc_scan;
  5473. int dqp;
  5474. if(IS_INTERLACED(mb_type)){
  5475. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  5476. dc_scan= luma_dc_field_scan;
  5477. }else{
  5478. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  5479. dc_scan= luma_dc_zigzag_scan;
  5480. }
  5481. scan8x8= s->qscale ? h->zigzag_scan8x8 : h->zigzag_scan8x8_q0;
  5482. h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
  5483. if( dqp == INT_MIN ){
  5484. av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", s->mb_x, s->mb_y);
  5485. return -1;
  5486. }
  5487. s->qscale += dqp;
  5488. if(((unsigned)s->qscale) > 51){
  5489. if(s->qscale<0) s->qscale+= 52;
  5490. else s->qscale-= 52;
  5491. }
  5492. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  5493. if( IS_INTRA16x16( mb_type ) ) {
  5494. int i;
  5495. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
  5496. if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16) < 0)
  5497. return -1;
  5498. if( cbp&15 ) {
  5499. for( i = 0; i < 16; i++ ) {
  5500. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
  5501. if( decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 )
  5502. return -1;
  5503. }
  5504. } else {
  5505. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  5506. }
  5507. } else {
  5508. int i8x8, i4x4;
  5509. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5510. if( cbp & (1<<i8x8) ) {
  5511. if( IS_8x8DCT(mb_type) ) {
  5512. if( decode_cabac_residual(h, h->mb + 64*i8x8, 5, 4*i8x8,
  5513. scan8x8, h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 64) < 0 )
  5514. return -1;
  5515. } else
  5516. for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
  5517. const int index = 4*i8x8 + i4x4;
  5518. //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
  5519. 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 )
  5520. return -1;
  5521. }
  5522. } else {
  5523. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  5524. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  5525. }
  5526. }
  5527. }
  5528. if( cbp&0x30 ){
  5529. int c;
  5530. for( c = 0; c < 2; c++ ) {
  5531. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
  5532. if( decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, NULL, 4) < 0)
  5533. return -1;
  5534. }
  5535. }
  5536. if( cbp&0x20 ) {
  5537. int c, i;
  5538. for( c = 0; c < 2; c++ ) {
  5539. for( i = 0; i < 4; i++ ) {
  5540. const int index = 16 + 4 * c + i;
  5541. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
  5542. 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)
  5543. return -1;
  5544. }
  5545. }
  5546. } else {
  5547. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5548. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5549. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5550. }
  5551. } else {
  5552. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5553. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  5554. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5555. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5556. }
  5557. s->current_picture.qscale_table[mb_xy]= s->qscale;
  5558. write_back_non_zero_count(h);
  5559. return 0;
  5560. }
  5561. static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  5562. int i, d;
  5563. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  5564. const int alpha = alpha_table[index_a];
  5565. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  5566. if( bS[0] < 4 ) {
  5567. int8_t tc[4];
  5568. for(i=0; i<4; i++)
  5569. tc[i] = bS[i] ? tc0_table[index_a][bS[i] - 1] : -1;
  5570. h->s.dsp.h264_h_loop_filter_luma(pix, stride, alpha, beta, tc);
  5571. } else {
  5572. /* 16px edge length, because bS=4 is triggered by being at
  5573. * the edge of an intra MB, so all 4 bS are the same */
  5574. for( d = 0; d < 16; d++ ) {
  5575. const int p0 = pix[-1];
  5576. const int p1 = pix[-2];
  5577. const int p2 = pix[-3];
  5578. const int q0 = pix[0];
  5579. const int q1 = pix[1];
  5580. const int q2 = pix[2];
  5581. if( ABS( p0 - q0 ) < alpha &&
  5582. ABS( p1 - p0 ) < beta &&
  5583. ABS( q1 - q0 ) < beta ) {
  5584. if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5585. if( ABS( p2 - p0 ) < beta)
  5586. {
  5587. const int p3 = pix[-4];
  5588. /* p0', p1', p2' */
  5589. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5590. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5591. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5592. } else {
  5593. /* p0' */
  5594. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5595. }
  5596. if( ABS( q2 - q0 ) < beta)
  5597. {
  5598. const int q3 = pix[3];
  5599. /* q0', q1', q2' */
  5600. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5601. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5602. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5603. } else {
  5604. /* q0' */
  5605. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5606. }
  5607. }else{
  5608. /* p0', q0' */
  5609. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5610. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5611. }
  5612. 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]);
  5613. }
  5614. pix += stride;
  5615. }
  5616. }
  5617. }
  5618. static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  5619. int i;
  5620. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  5621. const int alpha = alpha_table[index_a];
  5622. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  5623. if( bS[0] < 4 ) {
  5624. int8_t tc[4];
  5625. for(i=0; i<4; i++)
  5626. tc[i] = bS[i] ? tc0_table[index_a][bS[i] - 1] + 1 : 0;
  5627. h->s.dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
  5628. } else {
  5629. h->s.dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
  5630. }
  5631. }
  5632. static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int bS[8], int qp[2] ) {
  5633. int i;
  5634. for( i = 0; i < 16; i++, pix += stride) {
  5635. int index_a;
  5636. int alpha;
  5637. int beta;
  5638. int qp_index;
  5639. int bS_index = (i >> 1);
  5640. if (h->mb_field_decoding_flag) {
  5641. bS_index &= ~1;
  5642. bS_index |= (i & 1);
  5643. }
  5644. if( bS[bS_index] == 0 ) {
  5645. continue;
  5646. }
  5647. qp_index = h->mb_field_decoding_flag ? (i & 1) : (i >> 3);
  5648. index_a = clip( qp[qp_index] + h->slice_alpha_c0_offset, 0, 51 );
  5649. alpha = alpha_table[index_a];
  5650. beta = beta_table[clip( qp[qp_index] + h->slice_beta_offset, 0, 51 )];
  5651. if( bS[bS_index] < 4 ) {
  5652. const int tc0 = tc0_table[index_a][bS[bS_index] - 1];
  5653. /* 4px edge length */
  5654. const int p0 = pix[-1];
  5655. const int p1 = pix[-2];
  5656. const int p2 = pix[-3];
  5657. const int q0 = pix[0];
  5658. const int q1 = pix[1];
  5659. const int q2 = pix[2];
  5660. if( ABS( p0 - q0 ) < alpha &&
  5661. ABS( p1 - p0 ) < beta &&
  5662. ABS( q1 - q0 ) < beta ) {
  5663. int tc = tc0;
  5664. int i_delta;
  5665. if( ABS( p2 - p0 ) < beta ) {
  5666. pix[-2] = p1 + clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  5667. tc++;
  5668. }
  5669. if( ABS( q2 - q0 ) < beta ) {
  5670. pix[1] = q1 + clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  5671. tc++;
  5672. }
  5673. i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  5674. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  5675. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  5676. 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);
  5677. }
  5678. }else{
  5679. /* 4px edge length */
  5680. const int p0 = pix[-1];
  5681. const int p1 = pix[-2];
  5682. const int p2 = pix[-3];
  5683. const int q0 = pix[0];
  5684. const int q1 = pix[1];
  5685. const int q2 = pix[2];
  5686. if( ABS( p0 - q0 ) < alpha &&
  5687. ABS( p1 - p0 ) < beta &&
  5688. ABS( q1 - q0 ) < beta ) {
  5689. if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5690. if( ABS( p2 - p0 ) < beta)
  5691. {
  5692. const int p3 = pix[-4];
  5693. /* p0', p1', p2' */
  5694. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5695. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5696. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5697. } else {
  5698. /* p0' */
  5699. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5700. }
  5701. if( ABS( q2 - q0 ) < beta)
  5702. {
  5703. const int q3 = pix[3];
  5704. /* q0', q1', q2' */
  5705. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5706. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5707. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5708. } else {
  5709. /* q0' */
  5710. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5711. }
  5712. }else{
  5713. /* p0', q0' */
  5714. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5715. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5716. }
  5717. 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]);
  5718. }
  5719. }
  5720. }
  5721. }
  5722. static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp[2] ) {
  5723. int i;
  5724. for( i = 0; i < 8; i++, pix += stride) {
  5725. int index_a;
  5726. int alpha;
  5727. int beta;
  5728. int qp_index;
  5729. int bS_index = i;
  5730. if( bS[bS_index] == 0 ) {
  5731. continue;
  5732. }
  5733. qp_index = h->mb_field_decoding_flag ? (i & 1) : (i >> 3);
  5734. index_a = clip( qp[qp_index] + h->slice_alpha_c0_offset, 0, 51 );
  5735. alpha = alpha_table[index_a];
  5736. beta = beta_table[clip( qp[qp_index] + h->slice_beta_offset, 0, 51 )];
  5737. if( bS[bS_index] < 4 ) {
  5738. const int tc = tc0_table[index_a][bS[bS_index] - 1] + 1;
  5739. /* 2px edge length (because we use same bS than the one for luma) */
  5740. const int p0 = pix[-1];
  5741. const int p1 = pix[-2];
  5742. const int q0 = pix[0];
  5743. const int q1 = pix[1];
  5744. if( ABS( p0 - q0 ) < alpha &&
  5745. ABS( p1 - p0 ) < beta &&
  5746. ABS( q1 - q0 ) < beta ) {
  5747. const int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  5748. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  5749. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  5750. 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);
  5751. }
  5752. }else{
  5753. const int p0 = pix[-1];
  5754. const int p1 = pix[-2];
  5755. const int q0 = pix[0];
  5756. const int q1 = pix[1];
  5757. if( ABS( p0 - q0 ) < alpha &&
  5758. ABS( p1 - p0 ) < beta &&
  5759. ABS( q1 - q0 ) < beta ) {
  5760. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  5761. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  5762. 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]);
  5763. }
  5764. }
  5765. }
  5766. }
  5767. static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  5768. int i, d;
  5769. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  5770. const int alpha = alpha_table[index_a];
  5771. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  5772. const int pix_next = stride;
  5773. if( bS[0] < 4 ) {
  5774. int8_t tc[4];
  5775. for(i=0; i<4; i++)
  5776. tc[i] = bS[i] ? tc0_table[index_a][bS[i] - 1] : -1;
  5777. h->s.dsp.h264_v_loop_filter_luma(pix, stride, alpha, beta, tc);
  5778. } else {
  5779. /* 16px edge length, see filter_mb_edgev */
  5780. for( d = 0; d < 16; d++ ) {
  5781. const int p0 = pix[-1*pix_next];
  5782. const int p1 = pix[-2*pix_next];
  5783. const int p2 = pix[-3*pix_next];
  5784. const int q0 = pix[0];
  5785. const int q1 = pix[1*pix_next];
  5786. const int q2 = pix[2*pix_next];
  5787. if( ABS( p0 - q0 ) < alpha &&
  5788. ABS( p1 - p0 ) < beta &&
  5789. ABS( q1 - q0 ) < beta ) {
  5790. const int p3 = pix[-4*pix_next];
  5791. const int q3 = pix[ 3*pix_next];
  5792. if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5793. if( ABS( p2 - p0 ) < beta) {
  5794. /* p0', p1', p2' */
  5795. pix[-1*pix_next] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5796. pix[-2*pix_next] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5797. pix[-3*pix_next] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5798. } else {
  5799. /* p0' */
  5800. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5801. }
  5802. if( ABS( q2 - q0 ) < beta) {
  5803. /* q0', q1', q2' */
  5804. pix[0*pix_next] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5805. pix[1*pix_next] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5806. pix[2*pix_next] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5807. } else {
  5808. /* q0' */
  5809. pix[0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5810. }
  5811. }else{
  5812. /* p0', q0' */
  5813. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5814. pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5815. }
  5816. 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]);
  5817. }
  5818. pix++;
  5819. }
  5820. }
  5821. }
  5822. static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  5823. int i;
  5824. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  5825. const int alpha = alpha_table[index_a];
  5826. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  5827. if( bS[0] < 4 ) {
  5828. int8_t tc[4];
  5829. for(i=0; i<4; i++)
  5830. tc[i] = bS[i] ? tc0_table[index_a][bS[i] - 1] + 1 : 0;
  5831. h->s.dsp.h264_v_loop_filter_chroma(pix, stride, alpha, beta, tc);
  5832. } else {
  5833. h->s.dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);
  5834. }
  5835. }
  5836. 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) {
  5837. MpegEncContext * const s = &h->s;
  5838. const int mb_xy= mb_x + mb_y*s->mb_stride;
  5839. int first_vertical_edge_done = 0;
  5840. int dir;
  5841. /* FIXME: A given frame may occupy more than one position in
  5842. * the reference list. So ref2frm should be populated with
  5843. * frame numbers, not indices. */
  5844. static const int ref2frm[18] = {-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  5845. //for sufficiently low qp, filtering wouldn't do anything
  5846. //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
  5847. if(!h->mb_aff_frame){
  5848. int qp_thresh = 15 - h->slice_alpha_c0_offset - FFMAX(0, h->pps.chroma_qp_index_offset);
  5849. int qp = s->current_picture.qscale_table[mb_xy];
  5850. if(qp <= qp_thresh
  5851. && (mb_x == 0 || ((qp + s->current_picture.qscale_table[mb_xy-1] + 1)>>1) <= qp_thresh)
  5852. && (mb_y == 0 || ((qp + s->current_picture.qscale_table[h->top_mb_xy] + 1)>>1) <= qp_thresh)){
  5853. return;
  5854. }
  5855. }
  5856. if (h->mb_aff_frame
  5857. // left mb is in picture
  5858. && h->slice_table[mb_xy-1] != 255
  5859. // and current and left pair do not have the same interlaced type
  5860. && (IS_INTERLACED(s->current_picture.mb_type[mb_xy]) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
  5861. // and left mb is in the same slice if deblocking_filter == 2
  5862. && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
  5863. /* First vertical edge is different in MBAFF frames
  5864. * There are 8 different bS to compute and 2 different Qp
  5865. */
  5866. int bS[8];
  5867. int qp[2];
  5868. int chroma_qp[2];
  5869. int i;
  5870. first_vertical_edge_done = 1;
  5871. for( i = 0; i < 8; i++ ) {
  5872. int y = i>>1;
  5873. int b_idx= 8 + 4 + 8*y;
  5874. int bn_idx= b_idx - 1;
  5875. int mbn_xy = h->mb_field_decoding_flag ? h->left_mb_xy[i>>2] : h->left_mb_xy[i&1];
  5876. if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) ||
  5877. IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) {
  5878. bS[i] = 4;
  5879. } else if( h->non_zero_count_cache[b_idx] != 0 ||
  5880. /* FIXME: with 8x8dct + cavlc, should check cbp instead of nnz */
  5881. h->non_zero_count_cache[bn_idx] != 0 ) {
  5882. bS[i] = 2;
  5883. } else {
  5884. int l;
  5885. bS[i] = 0;
  5886. for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
  5887. if( ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  5888. ABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  5889. ABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= 4 ) {
  5890. bS[i] = 1;
  5891. break;
  5892. }
  5893. }
  5894. }
  5895. }
  5896. if(bS[0]+bS[1]+bS[2]+bS[3] != 0) {
  5897. // Do not use s->qscale as luma quantizer because it has not the same
  5898. // value in IPCM macroblocks.
  5899. qp[0] = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[h->left_mb_xy[0]] + 1 ) >> 1;
  5900. chroma_qp[0] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy] ) +
  5901. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[h->left_mb_xy[0]] ) + 1 ) >> 1;
  5902. qp[1] = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[h->left_mb_xy[1]] + 1 ) >> 1;
  5903. chroma_qp[1] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy] ) +
  5904. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[h->left_mb_xy[1]] ) + 1 ) >> 1;
  5905. /* Filter edge */
  5906. 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);
  5907. { int i; for (i = 0; i < 8; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  5908. filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp );
  5909. filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, chroma_qp );
  5910. filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, chroma_qp );
  5911. }
  5912. }
  5913. /* dir : 0 -> vertical edge, 1 -> horizontal edge */
  5914. for( dir = 0; dir < 2; dir++ )
  5915. {
  5916. int edge;
  5917. const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
  5918. const int mb_type = s->current_picture.mb_type[mb_xy];
  5919. const int mbm_type = s->current_picture.mb_type[mbm_xy];
  5920. int start = h->slice_table[mbm_xy] == 255 ? 1 : 0;
  5921. const int edges = (mb_type & (MB_TYPE_16x16|MB_TYPE_SKIP))
  5922. == (MB_TYPE_16x16|MB_TYPE_SKIP) ? 1 : 4;
  5923. // how often to recheck mv-based bS when iterating between edges
  5924. const int mask_edge = (mb_type & (MB_TYPE_16x16 | (MB_TYPE_16x8 << dir))) ? 3 :
  5925. (mb_type & (MB_TYPE_8x16 >> dir)) ? 1 : 0;
  5926. // how often to recheck mv-based bS when iterating along each edge
  5927. const int mask_par0 = mb_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir));
  5928. if (first_vertical_edge_done) {
  5929. start = 1;
  5930. first_vertical_edge_done = 0;
  5931. }
  5932. if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
  5933. start = 1;
  5934. /* Calculate bS */
  5935. for( edge = start; edge < edges; edge++ ) {
  5936. /* mbn_xy: neighbor macroblock */
  5937. const int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
  5938. const int mbn_type = s->current_picture.mb_type[mbn_xy];
  5939. int bS[4];
  5940. int qp;
  5941. if( (edge&1) && IS_8x8DCT(mb_type) )
  5942. continue;
  5943. if (h->mb_aff_frame && (dir == 1) && (edge == 0) && ((mb_y & 1) == 0)
  5944. && !IS_INTERLACED(mb_type)
  5945. && IS_INTERLACED(mbn_type)
  5946. ) {
  5947. // This is a special case in the norm where the filtering must
  5948. // be done twice (one each of the field) even if we are in a
  5949. // frame macroblock.
  5950. //
  5951. unsigned int tmp_linesize = 2 * linesize;
  5952. unsigned int tmp_uvlinesize = 2 * uvlinesize;
  5953. int mbn_xy = mb_xy - 2 * s->mb_stride;
  5954. int qp, chroma_qp;
  5955. // first filtering
  5956. if( IS_INTRA(mb_type) ||
  5957. IS_INTRA(s->current_picture.mb_type[mbn_xy]) ) {
  5958. bS[0] = bS[1] = bS[2] = bS[3] = 3;
  5959. } else {
  5960. // TODO
  5961. av_log(h->s.avctx, AV_LOG_ERROR, "both non intra (TODO)\n");
  5962. }
  5963. /* Filter edge */
  5964. // Do not use s->qscale as luma quantizer because it has not the same
  5965. // value in IPCM macroblocks.
  5966. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  5967. 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);
  5968. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  5969. filter_mb_edgeh( h, &img_y[0], tmp_linesize, bS, qp );
  5970. chroma_qp = ( h->chroma_qp +
  5971. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  5972. filter_mb_edgech( h, &img_cb[0], tmp_uvlinesize, bS, chroma_qp );
  5973. filter_mb_edgech( h, &img_cr[0], tmp_uvlinesize, bS, chroma_qp );
  5974. // second filtering
  5975. mbn_xy += s->mb_stride;
  5976. if( IS_INTRA(mb_type) ||
  5977. IS_INTRA(mbn_type) ) {
  5978. bS[0] = bS[1] = bS[2] = bS[3] = 3;
  5979. } else {
  5980. // TODO
  5981. av_log(h->s.avctx, AV_LOG_ERROR, "both non intra (TODO)\n");
  5982. }
  5983. /* Filter edge */
  5984. // Do not use s->qscale as luma quantizer because it has not the same
  5985. // value in IPCM macroblocks.
  5986. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  5987. 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);
  5988. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  5989. filter_mb_edgeh( h, &img_y[linesize], tmp_linesize, bS, qp );
  5990. chroma_qp = ( h->chroma_qp +
  5991. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  5992. filter_mb_edgech( h, &img_cb[uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  5993. filter_mb_edgech( h, &img_cr[uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  5994. continue;
  5995. }
  5996. if( IS_INTRA(mb_type) ||
  5997. IS_INTRA(mbn_type) ) {
  5998. int value;
  5999. if (edge == 0) {
  6000. if ( (!IS_INTERLACED(mb_type) && !IS_INTERLACED(mbm_type))
  6001. || ((h->mb_aff_frame || (s->picture_structure != PICT_FRAME)) && (dir == 0))
  6002. ) {
  6003. value = 4;
  6004. } else {
  6005. value = 3;
  6006. }
  6007. } else {
  6008. value = 3;
  6009. }
  6010. bS[0] = bS[1] = bS[2] = bS[3] = value;
  6011. } else {
  6012. int i, l;
  6013. int mv_done;
  6014. if( edge & mask_edge ) {
  6015. bS[0] = bS[1] = bS[2] = bS[3] = 0;
  6016. mv_done = 1;
  6017. }
  6018. else if( mask_par0 && (edge || (mbn_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir)))) ) {
  6019. int b_idx= 8 + 4 + edge * (dir ? 8:1);
  6020. int bn_idx= b_idx - (dir ? 8:1);
  6021. int v = 0;
  6022. for( l = 0; !v && l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6023. v |= ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6024. ABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6025. ABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= 4;
  6026. }
  6027. bS[0] = bS[1] = bS[2] = bS[3] = v;
  6028. mv_done = 1;
  6029. }
  6030. else
  6031. mv_done = 0;
  6032. for( i = 0; i < 4; i++ ) {
  6033. int x = dir == 0 ? edge : i;
  6034. int y = dir == 0 ? i : edge;
  6035. int b_idx= 8 + 4 + x + 8*y;
  6036. int bn_idx= b_idx - (dir ? 8:1);
  6037. if( h->non_zero_count_cache[b_idx] != 0 ||
  6038. h->non_zero_count_cache[bn_idx] != 0 ) {
  6039. bS[i] = 2;
  6040. }
  6041. else if(!mv_done)
  6042. {
  6043. bS[i] = 0;
  6044. for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6045. if( ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6046. ABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6047. ABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= 4 ) {
  6048. bS[i] = 1;
  6049. break;
  6050. }
  6051. }
  6052. }
  6053. }
  6054. if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
  6055. continue;
  6056. }
  6057. /* Filter edge */
  6058. // Do not use s->qscale as luma quantizer because it has not the same
  6059. // value in IPCM macroblocks.
  6060. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6061. //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]);
  6062. tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
  6063. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  6064. if( dir == 0 ) {
  6065. filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
  6066. if( (edge&1) == 0 ) {
  6067. int chroma_qp = ( h->chroma_qp +
  6068. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6069. filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp );
  6070. filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp );
  6071. }
  6072. } else {
  6073. filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
  6074. if( (edge&1) == 0 ) {
  6075. int chroma_qp = ( h->chroma_qp +
  6076. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6077. filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6078. filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6079. }
  6080. }
  6081. }
  6082. }
  6083. }
  6084. static int decode_slice(H264Context *h){
  6085. MpegEncContext * const s = &h->s;
  6086. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  6087. s->mb_skip_run= -1;
  6088. if( h->pps.cabac ) {
  6089. int i;
  6090. /* realign */
  6091. align_get_bits( &s->gb );
  6092. /* init cabac */
  6093. ff_init_cabac_states( &h->cabac, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64 );
  6094. ff_init_cabac_decoder( &h->cabac,
  6095. s->gb.buffer + get_bits_count(&s->gb)/8,
  6096. ( s->gb.size_in_bits - get_bits_count(&s->gb) + 7)/8);
  6097. /* calculate pre-state */
  6098. for( i= 0; i < 460; i++ ) {
  6099. int pre;
  6100. if( h->slice_type == I_TYPE )
  6101. pre = clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
  6102. else
  6103. 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 );
  6104. if( pre <= 63 )
  6105. h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
  6106. else
  6107. h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
  6108. }
  6109. for(;;){
  6110. int ret = decode_mb_cabac(h);
  6111. int eos;
  6112. if(ret>=0) hl_decode_mb(h);
  6113. /* XXX: useless as decode_mb_cabac it doesn't support that ... */
  6114. if( ret >= 0 && h->mb_aff_frame ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  6115. s->mb_y++;
  6116. if(ret>=0) ret = decode_mb_cabac(h);
  6117. if(ret>=0) hl_decode_mb(h);
  6118. s->mb_y--;
  6119. }
  6120. eos = get_cabac_terminate( &h->cabac );
  6121. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 1) {
  6122. 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);
  6123. 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);
  6124. return -1;
  6125. }
  6126. if( ++s->mb_x >= s->mb_width ) {
  6127. s->mb_x = 0;
  6128. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6129. ++s->mb_y;
  6130. if(h->mb_aff_frame) {
  6131. ++s->mb_y;
  6132. }
  6133. }
  6134. if( eos || s->mb_y >= s->mb_height ) {
  6135. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6136. 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);
  6137. return 0;
  6138. }
  6139. }
  6140. } else {
  6141. for(;;){
  6142. int ret = decode_mb_cavlc(h);
  6143. if(ret>=0) hl_decode_mb(h);
  6144. if(ret>=0 && h->mb_aff_frame){ //FIXME optimal? or let mb_decode decode 16x32 ?
  6145. s->mb_y++;
  6146. ret = decode_mb_cavlc(h);
  6147. if(ret>=0) hl_decode_mb(h);
  6148. s->mb_y--;
  6149. }
  6150. if(ret<0){
  6151. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6152. 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);
  6153. return -1;
  6154. }
  6155. if(++s->mb_x >= s->mb_width){
  6156. s->mb_x=0;
  6157. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6158. ++s->mb_y;
  6159. if(h->mb_aff_frame) {
  6160. ++s->mb_y;
  6161. }
  6162. if(s->mb_y >= s->mb_height){
  6163. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6164. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  6165. 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);
  6166. return 0;
  6167. }else{
  6168. 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);
  6169. return -1;
  6170. }
  6171. }
  6172. }
  6173. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  6174. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6175. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  6176. 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);
  6177. return 0;
  6178. }else{
  6179. 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);
  6180. return -1;
  6181. }
  6182. }
  6183. }
  6184. }
  6185. #if 0
  6186. for(;s->mb_y < s->mb_height; s->mb_y++){
  6187. for(;s->mb_x < s->mb_width; s->mb_x++){
  6188. int ret= decode_mb(h);
  6189. hl_decode_mb(h);
  6190. if(ret<0){
  6191. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6192. 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);
  6193. return -1;
  6194. }
  6195. if(++s->mb_x >= s->mb_width){
  6196. s->mb_x=0;
  6197. if(++s->mb_y >= s->mb_height){
  6198. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6199. 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);
  6200. return 0;
  6201. }else{
  6202. 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);
  6203. return -1;
  6204. }
  6205. }
  6206. }
  6207. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  6208. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6209. 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);
  6210. return 0;
  6211. }else{
  6212. 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);
  6213. return -1;
  6214. }
  6215. }
  6216. }
  6217. s->mb_x=0;
  6218. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6219. }
  6220. #endif
  6221. return -1; //not reached
  6222. }
  6223. static int decode_unregistered_user_data(H264Context *h, int size){
  6224. MpegEncContext * const s = &h->s;
  6225. uint8_t user_data[16+256];
  6226. int e, build, i;
  6227. if(size<16)
  6228. return -1;
  6229. for(i=0; i<sizeof(user_data)-1 && i<size; i++){
  6230. user_data[i]= get_bits(&s->gb, 8);
  6231. }
  6232. user_data[i]= 0;
  6233. e= sscanf(user_data+16, "x264 - core %d"/*%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html*/, &build);
  6234. if(e==1 && build>=0)
  6235. h->x264_build= build;
  6236. if(s->avctx->debug & FF_DEBUG_BUGS)
  6237. av_log(s->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data+16);
  6238. for(; i<size; i++)
  6239. skip_bits(&s->gb, 8);
  6240. return 0;
  6241. }
  6242. static int decode_sei(H264Context *h){
  6243. MpegEncContext * const s = &h->s;
  6244. while(get_bits_count(&s->gb) + 16 < s->gb.size_in_bits){
  6245. int size, type;
  6246. type=0;
  6247. do{
  6248. type+= show_bits(&s->gb, 8);
  6249. }while(get_bits(&s->gb, 8) == 255);
  6250. size=0;
  6251. do{
  6252. size+= show_bits(&s->gb, 8);
  6253. }while(get_bits(&s->gb, 8) == 255);
  6254. switch(type){
  6255. case 5:
  6256. if(decode_unregistered_user_data(h, size) < 0);
  6257. return -1;
  6258. break;
  6259. default:
  6260. skip_bits(&s->gb, 8*size);
  6261. }
  6262. //FIXME check bits here
  6263. align_get_bits(&s->gb);
  6264. }
  6265. return 0;
  6266. }
  6267. static inline void decode_hrd_parameters(H264Context *h, SPS *sps){
  6268. MpegEncContext * const s = &h->s;
  6269. int cpb_count, i;
  6270. cpb_count = get_ue_golomb(&s->gb) + 1;
  6271. get_bits(&s->gb, 4); /* bit_rate_scale */
  6272. get_bits(&s->gb, 4); /* cpb_size_scale */
  6273. for(i=0; i<cpb_count; i++){
  6274. get_ue_golomb(&s->gb); /* bit_rate_value_minus1 */
  6275. get_ue_golomb(&s->gb); /* cpb_size_value_minus1 */
  6276. get_bits1(&s->gb); /* cbr_flag */
  6277. }
  6278. get_bits(&s->gb, 5); /* initial_cpb_removal_delay_length_minus1 */
  6279. get_bits(&s->gb, 5); /* cpb_removal_delay_length_minus1 */
  6280. get_bits(&s->gb, 5); /* dpb_output_delay_length_minus1 */
  6281. get_bits(&s->gb, 5); /* time_offset_length */
  6282. }
  6283. static inline int decode_vui_parameters(H264Context *h, SPS *sps){
  6284. MpegEncContext * const s = &h->s;
  6285. int aspect_ratio_info_present_flag, aspect_ratio_idc;
  6286. int nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag;
  6287. aspect_ratio_info_present_flag= get_bits1(&s->gb);
  6288. if( aspect_ratio_info_present_flag ) {
  6289. aspect_ratio_idc= get_bits(&s->gb, 8);
  6290. if( aspect_ratio_idc == EXTENDED_SAR ) {
  6291. sps->sar.num= get_bits(&s->gb, 16);
  6292. sps->sar.den= get_bits(&s->gb, 16);
  6293. }else if(aspect_ratio_idc < 14){
  6294. sps->sar= pixel_aspect[aspect_ratio_idc];
  6295. }else{
  6296. av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
  6297. return -1;
  6298. }
  6299. }else{
  6300. sps->sar.num=
  6301. sps->sar.den= 0;
  6302. }
  6303. // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
  6304. if(get_bits1(&s->gb)){ /* overscan_info_present_flag */
  6305. get_bits1(&s->gb); /* overscan_appropriate_flag */
  6306. }
  6307. if(get_bits1(&s->gb)){ /* video_signal_type_present_flag */
  6308. get_bits(&s->gb, 3); /* video_format */
  6309. get_bits1(&s->gb); /* video_full_range_flag */
  6310. if(get_bits1(&s->gb)){ /* colour_description_present_flag */
  6311. get_bits(&s->gb, 8); /* colour_primaries */
  6312. get_bits(&s->gb, 8); /* transfer_characteristics */
  6313. get_bits(&s->gb, 8); /* matrix_coefficients */
  6314. }
  6315. }
  6316. if(get_bits1(&s->gb)){ /* chroma_location_info_present_flag */
  6317. get_ue_golomb(&s->gb); /* chroma_sample_location_type_top_field */
  6318. get_ue_golomb(&s->gb); /* chroma_sample_location_type_bottom_field */
  6319. }
  6320. sps->timing_info_present_flag = get_bits1(&s->gb);
  6321. if(sps->timing_info_present_flag){
  6322. sps->num_units_in_tick = get_bits_long(&s->gb, 32);
  6323. sps->time_scale = get_bits_long(&s->gb, 32);
  6324. sps->fixed_frame_rate_flag = get_bits1(&s->gb);
  6325. }
  6326. nal_hrd_parameters_present_flag = get_bits1(&s->gb);
  6327. if(nal_hrd_parameters_present_flag)
  6328. decode_hrd_parameters(h, sps);
  6329. vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
  6330. if(vcl_hrd_parameters_present_flag)
  6331. decode_hrd_parameters(h, sps);
  6332. if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag)
  6333. get_bits1(&s->gb); /* low_delay_hrd_flag */
  6334. get_bits1(&s->gb); /* pic_struct_present_flag */
  6335. sps->bitstream_restriction_flag = get_bits1(&s->gb);
  6336. if(sps->bitstream_restriction_flag){
  6337. get_bits1(&s->gb); /* motion_vectors_over_pic_boundaries_flag */
  6338. get_ue_golomb(&s->gb); /* max_bytes_per_pic_denom */
  6339. get_ue_golomb(&s->gb); /* max_bits_per_mb_denom */
  6340. get_ue_golomb(&s->gb); /* log2_max_mv_length_horizontal */
  6341. get_ue_golomb(&s->gb); /* log2_max_mv_length_vertical */
  6342. sps->num_reorder_frames = get_ue_golomb(&s->gb);
  6343. get_ue_golomb(&s->gb); /* max_dec_frame_buffering */
  6344. }
  6345. return 0;
  6346. }
  6347. static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,
  6348. const uint8_t *jvt_list, const uint8_t *fallback_list){
  6349. MpegEncContext * const s = &h->s;
  6350. int i, last = 8, next = 8;
  6351. const uint8_t *scan = size == 16 ? zigzag_scan : zigzag_scan8x8;
  6352. if(!get_bits1(&s->gb)) /* matrix not written, we use the predicted one */
  6353. memcpy(factors, fallback_list, size*sizeof(uint8_t));
  6354. else
  6355. for(i=0;i<size;i++){
  6356. if(next)
  6357. next = (last + get_se_golomb(&s->gb)) & 0xff;
  6358. if(!i && !next){ /* matrix not written, we use the preset one */
  6359. memcpy(factors, jvt_list, size*sizeof(uint8_t));
  6360. break;
  6361. }
  6362. last = factors[scan[i]] = next ? next : last;
  6363. }
  6364. }
  6365. static void decode_scaling_matrices(H264Context *h, SPS *sps, PPS *pps, int is_sps,
  6366. uint8_t (*scaling_matrix4)[16], uint8_t (*scaling_matrix8)[64]){
  6367. MpegEncContext * const s = &h->s;
  6368. int fallback_sps = !is_sps && sps->scaling_matrix_present;
  6369. const uint8_t *fallback[4] = {
  6370. fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
  6371. fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
  6372. fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
  6373. fallback_sps ? sps->scaling_matrix8[1] : default_scaling8[1]
  6374. };
  6375. if(get_bits1(&s->gb)){
  6376. sps->scaling_matrix_present |= is_sps;
  6377. decode_scaling_list(h,scaling_matrix4[0],16,default_scaling4[0],fallback[0]); // Intra, Y
  6378. decode_scaling_list(h,scaling_matrix4[1],16,default_scaling4[0],scaling_matrix4[0]); // Intra, Cr
  6379. decode_scaling_list(h,scaling_matrix4[2],16,default_scaling4[0],scaling_matrix4[1]); // Intra, Cb
  6380. decode_scaling_list(h,scaling_matrix4[3],16,default_scaling4[1],fallback[1]); // Inter, Y
  6381. decode_scaling_list(h,scaling_matrix4[4],16,default_scaling4[1],scaling_matrix4[3]); // Inter, Cr
  6382. decode_scaling_list(h,scaling_matrix4[5],16,default_scaling4[1],scaling_matrix4[4]); // Inter, Cb
  6383. if(is_sps || pps->transform_8x8_mode){
  6384. decode_scaling_list(h,scaling_matrix8[0],64,default_scaling8[0],fallback[2]); // Intra, Y
  6385. decode_scaling_list(h,scaling_matrix8[1],64,default_scaling8[1],fallback[3]); // Inter, Y
  6386. }
  6387. } else if(fallback_sps) {
  6388. memcpy(scaling_matrix4, sps->scaling_matrix4, 6*16*sizeof(uint8_t));
  6389. memcpy(scaling_matrix8, sps->scaling_matrix8, 2*64*sizeof(uint8_t));
  6390. }
  6391. }
  6392. static inline int decode_seq_parameter_set(H264Context *h){
  6393. MpegEncContext * const s = &h->s;
  6394. int profile_idc, level_idc;
  6395. int sps_id, i;
  6396. SPS *sps;
  6397. profile_idc= get_bits(&s->gb, 8);
  6398. get_bits1(&s->gb); //constraint_set0_flag
  6399. get_bits1(&s->gb); //constraint_set1_flag
  6400. get_bits1(&s->gb); //constraint_set2_flag
  6401. get_bits1(&s->gb); //constraint_set3_flag
  6402. get_bits(&s->gb, 4); // reserved
  6403. level_idc= get_bits(&s->gb, 8);
  6404. sps_id= get_ue_golomb(&s->gb);
  6405. sps= &h->sps_buffer[ sps_id ];
  6406. sps->profile_idc= profile_idc;
  6407. sps->level_idc= level_idc;
  6408. if(sps->profile_idc >= 100){ //high profile
  6409. if(get_ue_golomb(&s->gb) == 3) //chroma_format_idc
  6410. get_bits1(&s->gb); //residual_color_transform_flag
  6411. get_ue_golomb(&s->gb); //bit_depth_luma_minus8
  6412. get_ue_golomb(&s->gb); //bit_depth_chroma_minus8
  6413. sps->transform_bypass = get_bits1(&s->gb);
  6414. decode_scaling_matrices(h, sps, NULL, 1, sps->scaling_matrix4, sps->scaling_matrix8);
  6415. }else
  6416. sps->scaling_matrix_present = 0;
  6417. sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
  6418. sps->poc_type= get_ue_golomb(&s->gb);
  6419. if(sps->poc_type == 0){ //FIXME #define
  6420. sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
  6421. } else if(sps->poc_type == 1){//FIXME #define
  6422. sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
  6423. sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
  6424. sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
  6425. sps->poc_cycle_length= get_ue_golomb(&s->gb);
  6426. for(i=0; i<sps->poc_cycle_length; i++)
  6427. sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
  6428. }
  6429. if(sps->poc_type > 2){
  6430. av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
  6431. return -1;
  6432. }
  6433. sps->ref_frame_count= get_ue_golomb(&s->gb);
  6434. if(sps->ref_frame_count > MAX_PICTURE_COUNT-2){
  6435. av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");
  6436. }
  6437. sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
  6438. sps->mb_width= get_ue_golomb(&s->gb) + 1;
  6439. sps->mb_height= get_ue_golomb(&s->gb) + 1;
  6440. if((unsigned)sps->mb_width >= INT_MAX/16 || (unsigned)sps->mb_height >= INT_MAX/16 ||
  6441. avcodec_check_dimensions(NULL, 16*sps->mb_width, 16*sps->mb_height))
  6442. return -1;
  6443. sps->frame_mbs_only_flag= get_bits1(&s->gb);
  6444. if(!sps->frame_mbs_only_flag)
  6445. av_log(h->s.avctx, AV_LOG_ERROR, "interlacing is not supported, picture will probably be garbage\n");
  6446. if(!sps->frame_mbs_only_flag)
  6447. sps->mb_aff= get_bits1(&s->gb);
  6448. else
  6449. sps->mb_aff= 0;
  6450. sps->direct_8x8_inference_flag= get_bits1(&s->gb);
  6451. sps->crop= get_bits1(&s->gb);
  6452. if(sps->crop){
  6453. sps->crop_left = get_ue_golomb(&s->gb);
  6454. sps->crop_right = get_ue_golomb(&s->gb);
  6455. sps->crop_top = get_ue_golomb(&s->gb);
  6456. sps->crop_bottom= get_ue_golomb(&s->gb);
  6457. if(sps->crop_left || sps->crop_top){
  6458. av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ...\n");
  6459. }
  6460. }else{
  6461. sps->crop_left =
  6462. sps->crop_right =
  6463. sps->crop_top =
  6464. sps->crop_bottom= 0;
  6465. }
  6466. sps->vui_parameters_present_flag= get_bits1(&s->gb);
  6467. if( sps->vui_parameters_present_flag )
  6468. decode_vui_parameters(h, sps);
  6469. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6470. 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",
  6471. sps_id, sps->profile_idc, sps->level_idc,
  6472. sps->poc_type,
  6473. sps->ref_frame_count,
  6474. sps->mb_width, sps->mb_height,
  6475. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  6476. sps->direct_8x8_inference_flag ? "8B8" : "",
  6477. sps->crop_left, sps->crop_right,
  6478. sps->crop_top, sps->crop_bottom,
  6479. sps->vui_parameters_present_flag ? "VUI" : ""
  6480. );
  6481. }
  6482. return 0;
  6483. }
  6484. static inline int decode_picture_parameter_set(H264Context *h, int bit_length){
  6485. MpegEncContext * const s = &h->s;
  6486. int pps_id= get_ue_golomb(&s->gb);
  6487. PPS *pps= &h->pps_buffer[pps_id];
  6488. pps->sps_id= get_ue_golomb(&s->gb);
  6489. pps->cabac= get_bits1(&s->gb);
  6490. pps->pic_order_present= get_bits1(&s->gb);
  6491. pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
  6492. if(pps->slice_group_count > 1 ){
  6493. pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
  6494. av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
  6495. switch(pps->mb_slice_group_map_type){
  6496. case 0:
  6497. #if 0
  6498. | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
  6499. | run_length[ i ] |1 |ue(v) |
  6500. #endif
  6501. break;
  6502. case 2:
  6503. #if 0
  6504. | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
  6505. |{ | | |
  6506. | top_left_mb[ i ] |1 |ue(v) |
  6507. | bottom_right_mb[ i ] |1 |ue(v) |
  6508. | } | | |
  6509. #endif
  6510. break;
  6511. case 3:
  6512. case 4:
  6513. case 5:
  6514. #if 0
  6515. | slice_group_change_direction_flag |1 |u(1) |
  6516. | slice_group_change_rate_minus1 |1 |ue(v) |
  6517. #endif
  6518. break;
  6519. case 6:
  6520. #if 0
  6521. | slice_group_id_cnt_minus1 |1 |ue(v) |
  6522. | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
  6523. |) | | |
  6524. | slice_group_id[ i ] |1 |u(v) |
  6525. #endif
  6526. break;
  6527. }
  6528. }
  6529. pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  6530. pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  6531. if(pps->ref_count[0] > 32 || pps->ref_count[1] > 32){
  6532. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
  6533. return -1;
  6534. }
  6535. pps->weighted_pred= get_bits1(&s->gb);
  6536. pps->weighted_bipred_idc= get_bits(&s->gb, 2);
  6537. pps->init_qp= get_se_golomb(&s->gb) + 26;
  6538. pps->init_qs= get_se_golomb(&s->gb) + 26;
  6539. pps->chroma_qp_index_offset= get_se_golomb(&s->gb);
  6540. pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
  6541. pps->constrained_intra_pred= get_bits1(&s->gb);
  6542. pps->redundant_pic_cnt_present = get_bits1(&s->gb);
  6543. pps->transform_8x8_mode= 0;
  6544. h->dequant_coeff_pps= -1; //contents of sps/pps can change even if id doesn't, so reinit
  6545. memset(pps->scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  6546. memset(pps->scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  6547. if(get_bits_count(&s->gb) < bit_length){
  6548. pps->transform_8x8_mode= get_bits1(&s->gb);
  6549. decode_scaling_matrices(h, &h->sps_buffer[pps->sps_id], pps, 0, pps->scaling_matrix4, pps->scaling_matrix8);
  6550. get_se_golomb(&s->gb); //second_chroma_qp_index_offset
  6551. }
  6552. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6553. 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",
  6554. pps_id, pps->sps_id,
  6555. pps->cabac ? "CABAC" : "CAVLC",
  6556. pps->slice_group_count,
  6557. pps->ref_count[0], pps->ref_count[1],
  6558. pps->weighted_pred ? "weighted" : "",
  6559. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset,
  6560. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  6561. pps->constrained_intra_pred ? "CONSTR" : "",
  6562. pps->redundant_pic_cnt_present ? "REDU" : "",
  6563. pps->transform_8x8_mode ? "8x8DCT" : ""
  6564. );
  6565. }
  6566. return 0;
  6567. }
  6568. /**
  6569. * finds the end of the current frame in the bitstream.
  6570. * @return the position of the first byte of the next frame, or -1
  6571. */
  6572. static int find_frame_end(H264Context *h, const uint8_t *buf, int buf_size){
  6573. int i;
  6574. uint32_t state;
  6575. ParseContext *pc = &(h->s.parse_context);
  6576. //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
  6577. // mb_addr= pc->mb_addr - 1;
  6578. state= pc->state;
  6579. for(i=0; i<=buf_size; i++){
  6580. if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  6581. tprintf("find_frame_end new startcode = %08x, frame_start_found = %d, pos = %d\n", state, pc->frame_start_found, i);
  6582. if(pc->frame_start_found){
  6583. // If there isn't one more byte in the buffer
  6584. // the test on first_mb_in_slice cannot be done yet
  6585. // do it at next call.
  6586. if (i >= buf_size) break;
  6587. if (buf[i] & 0x80) {
  6588. // first_mb_in_slice is 0, probably the first nal of a new
  6589. // slice
  6590. tprintf("find_frame_end frame_end_found, state = %08x, pos = %d\n", state, i);
  6591. pc->state=-1;
  6592. pc->frame_start_found= 0;
  6593. return i-4;
  6594. }
  6595. }
  6596. pc->frame_start_found = 1;
  6597. }
  6598. if((state&0xFFFFFF1F) == 0x107 || (state&0xFFFFFF1F) == 0x108 || (state&0xFFFFFF1F) == 0x109){
  6599. if(pc->frame_start_found){
  6600. pc->state=-1;
  6601. pc->frame_start_found= 0;
  6602. return i-4;
  6603. }
  6604. }
  6605. if (i<buf_size)
  6606. state= (state<<8) | buf[i];
  6607. }
  6608. pc->state= state;
  6609. return END_NOT_FOUND;
  6610. }
  6611. static int h264_parse(AVCodecParserContext *s,
  6612. AVCodecContext *avctx,
  6613. uint8_t **poutbuf, int *poutbuf_size,
  6614. const uint8_t *buf, int buf_size)
  6615. {
  6616. H264Context *h = s->priv_data;
  6617. ParseContext *pc = &h->s.parse_context;
  6618. int next;
  6619. next= find_frame_end(h, buf, buf_size);
  6620. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  6621. *poutbuf = NULL;
  6622. *poutbuf_size = 0;
  6623. return buf_size;
  6624. }
  6625. *poutbuf = (uint8_t *)buf;
  6626. *poutbuf_size = buf_size;
  6627. return next;
  6628. }
  6629. static int h264_split(AVCodecContext *avctx,
  6630. const uint8_t *buf, int buf_size)
  6631. {
  6632. int i;
  6633. uint32_t state = -1;
  6634. int has_sps= 0;
  6635. for(i=0; i<=buf_size; i++){
  6636. if((state&0xFFFFFF1F) == 0x107)
  6637. has_sps=1;
  6638. /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  6639. }*/
  6640. if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
  6641. if(has_sps){
  6642. while(i>4 && buf[i-5]==0) i--;
  6643. return i-4;
  6644. }
  6645. }
  6646. if (i<buf_size)
  6647. state= (state<<8) | buf[i];
  6648. }
  6649. return 0;
  6650. }
  6651. static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){
  6652. MpegEncContext * const s = &h->s;
  6653. AVCodecContext * const avctx= s->avctx;
  6654. int buf_index=0;
  6655. #if 0
  6656. int i;
  6657. for(i=0; i<50; i++){
  6658. av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
  6659. }
  6660. #endif
  6661. h->slice_num = 0;
  6662. s->current_picture_ptr= NULL;
  6663. for(;;){
  6664. int consumed;
  6665. int dst_length;
  6666. int bit_length;
  6667. uint8_t *ptr;
  6668. int i, nalsize = 0;
  6669. if(h->is_avc) {
  6670. if(buf_index >= buf_size) break;
  6671. nalsize = 0;
  6672. for(i = 0; i < h->nal_length_size; i++)
  6673. nalsize = (nalsize << 8) | buf[buf_index++];
  6674. if(nalsize <= 1){
  6675. if(nalsize == 1){
  6676. buf_index++;
  6677. continue;
  6678. }else{
  6679. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  6680. break;
  6681. }
  6682. }
  6683. } else {
  6684. // start code prefix search
  6685. for(; buf_index + 3 < buf_size; buf_index++){
  6686. // this should allways succeed in the first iteration
  6687. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  6688. break;
  6689. }
  6690. if(buf_index+3 >= buf_size) break;
  6691. buf_index+=3;
  6692. }
  6693. ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, h->is_avc ? nalsize : buf_size - buf_index);
  6694. if(ptr[dst_length - 1] == 0) dst_length--;
  6695. bit_length= 8*dst_length - decode_rbsp_trailing(ptr + dst_length - 1);
  6696. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  6697. 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);
  6698. }
  6699. if (h->is_avc && (nalsize != consumed))
  6700. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  6701. buf_index += consumed;
  6702. if( (s->hurry_up == 1 && h->nal_ref_idc == 0) //FIXME dont discard SEI id
  6703. ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  6704. continue;
  6705. switch(h->nal_unit_type){
  6706. case NAL_IDR_SLICE:
  6707. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  6708. case NAL_SLICE:
  6709. init_get_bits(&s->gb, ptr, bit_length);
  6710. h->intra_gb_ptr=
  6711. h->inter_gb_ptr= &s->gb;
  6712. s->data_partitioning = 0;
  6713. if(decode_slice_header(h) < 0){
  6714. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  6715. break;
  6716. }
  6717. s->current_picture_ptr->key_frame= (h->nal_unit_type == NAL_IDR_SLICE);
  6718. if(h->redundant_pic_count==0 && s->hurry_up < 5
  6719. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  6720. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  6721. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  6722. && avctx->skip_frame < AVDISCARD_ALL)
  6723. decode_slice(h);
  6724. break;
  6725. case NAL_DPA:
  6726. init_get_bits(&s->gb, ptr, bit_length);
  6727. h->intra_gb_ptr=
  6728. h->inter_gb_ptr= NULL;
  6729. s->data_partitioning = 1;
  6730. if(decode_slice_header(h) < 0){
  6731. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  6732. }
  6733. break;
  6734. case NAL_DPB:
  6735. init_get_bits(&h->intra_gb, ptr, bit_length);
  6736. h->intra_gb_ptr= &h->intra_gb;
  6737. break;
  6738. case NAL_DPC:
  6739. init_get_bits(&h->inter_gb, ptr, bit_length);
  6740. h->inter_gb_ptr= &h->inter_gb;
  6741. if(h->redundant_pic_count==0 && h->intra_gb_ptr && s->data_partitioning
  6742. && s->hurry_up < 5
  6743. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  6744. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  6745. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  6746. && avctx->skip_frame < AVDISCARD_ALL)
  6747. decode_slice(h);
  6748. break;
  6749. case NAL_SEI:
  6750. init_get_bits(&s->gb, ptr, bit_length);
  6751. decode_sei(h);
  6752. break;
  6753. case NAL_SPS:
  6754. init_get_bits(&s->gb, ptr, bit_length);
  6755. decode_seq_parameter_set(h);
  6756. if(s->flags& CODEC_FLAG_LOW_DELAY)
  6757. s->low_delay=1;
  6758. if(avctx->has_b_frames < 2)
  6759. avctx->has_b_frames= !s->low_delay;
  6760. break;
  6761. case NAL_PPS:
  6762. init_get_bits(&s->gb, ptr, bit_length);
  6763. decode_picture_parameter_set(h, bit_length);
  6764. break;
  6765. case NAL_AUD:
  6766. case NAL_END_SEQUENCE:
  6767. case NAL_END_STREAM:
  6768. case NAL_FILLER_DATA:
  6769. case NAL_SPS_EXT:
  6770. case NAL_AUXILIARY_SLICE:
  6771. break;
  6772. default:
  6773. av_log(avctx, AV_LOG_ERROR, "Unknown NAL code: %d\n", h->nal_unit_type);
  6774. }
  6775. }
  6776. if(!s->current_picture_ptr) return buf_index; //no frame
  6777. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  6778. s->current_picture_ptr->pict_type= s->pict_type;
  6779. h->prev_frame_num_offset= h->frame_num_offset;
  6780. h->prev_frame_num= h->frame_num;
  6781. if(s->current_picture_ptr->reference){
  6782. h->prev_poc_msb= h->poc_msb;
  6783. h->prev_poc_lsb= h->poc_lsb;
  6784. }
  6785. if(s->current_picture_ptr->reference)
  6786. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  6787. ff_er_frame_end(s);
  6788. MPV_frame_end(s);
  6789. return buf_index;
  6790. }
  6791. /**
  6792. * returns the number of bytes consumed for building the current frame
  6793. */
  6794. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  6795. if(s->flags&CODEC_FLAG_TRUNCATED){
  6796. pos -= s->parse_context.last_index;
  6797. if(pos<0) pos=0; // FIXME remove (unneeded?)
  6798. return pos;
  6799. }else{
  6800. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  6801. if(pos+10>buf_size) pos=buf_size; // oops ;)
  6802. return pos;
  6803. }
  6804. }
  6805. static int decode_frame(AVCodecContext *avctx,
  6806. void *data, int *data_size,
  6807. uint8_t *buf, int buf_size)
  6808. {
  6809. H264Context *h = avctx->priv_data;
  6810. MpegEncContext *s = &h->s;
  6811. AVFrame *pict = data;
  6812. int buf_index;
  6813. s->flags= avctx->flags;
  6814. s->flags2= avctx->flags2;
  6815. /* no supplementary picture */
  6816. if (buf_size == 0) {
  6817. return 0;
  6818. }
  6819. if(s->flags&CODEC_FLAG_TRUNCATED){
  6820. int next= find_frame_end(h, buf, buf_size);
  6821. if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
  6822. return buf_size;
  6823. //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);
  6824. }
  6825. if(h->is_avc && !h->got_avcC) {
  6826. int i, cnt, nalsize;
  6827. unsigned char *p = avctx->extradata;
  6828. if(avctx->extradata_size < 7) {
  6829. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  6830. return -1;
  6831. }
  6832. if(*p != 1) {
  6833. av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
  6834. return -1;
  6835. }
  6836. /* sps and pps in the avcC always have length coded with 2 bytes,
  6837. so put a fake nal_length_size = 2 while parsing them */
  6838. h->nal_length_size = 2;
  6839. // Decode sps from avcC
  6840. cnt = *(p+5) & 0x1f; // Number of sps
  6841. p += 6;
  6842. for (i = 0; i < cnt; i++) {
  6843. nalsize = BE_16(p) + 2;
  6844. if(decode_nal_units(h, p, nalsize) < 0) {
  6845. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  6846. return -1;
  6847. }
  6848. p += nalsize;
  6849. }
  6850. // Decode pps from avcC
  6851. cnt = *(p++); // Number of pps
  6852. for (i = 0; i < cnt; i++) {
  6853. nalsize = BE_16(p) + 2;
  6854. if(decode_nal_units(h, p, nalsize) != nalsize) {
  6855. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  6856. return -1;
  6857. }
  6858. p += nalsize;
  6859. }
  6860. // Now store right nal length size, that will be use to parse all other nals
  6861. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  6862. // Do not reparse avcC
  6863. h->got_avcC = 1;
  6864. }
  6865. if(!h->is_avc && s->avctx->extradata_size && s->picture_number==0){
  6866. if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
  6867. return -1;
  6868. }
  6869. buf_index=decode_nal_units(h, buf, buf_size);
  6870. if(buf_index < 0)
  6871. return -1;
  6872. //FIXME do something with unavailable reference frames
  6873. // if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_index, buf_size);
  6874. if(!s->current_picture_ptr){
  6875. av_log(h->s.avctx, AV_LOG_DEBUG, "error, NO frame\n");
  6876. return -1;
  6877. }
  6878. {
  6879. Picture *out = s->current_picture_ptr;
  6880. #if 0 //decode order
  6881. *data_size = sizeof(AVFrame);
  6882. #else
  6883. /* Sort B-frames into display order */
  6884. Picture *cur = s->current_picture_ptr;
  6885. Picture *prev = h->delayed_output_pic;
  6886. int i, pics, cross_idr, out_of_order, out_idx;
  6887. if(h->sps.bitstream_restriction_flag
  6888. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  6889. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  6890. s->low_delay = 0;
  6891. }
  6892. pics = 0;
  6893. while(h->delayed_pic[pics]) pics++;
  6894. h->delayed_pic[pics++] = cur;
  6895. if(cur->reference == 0)
  6896. cur->reference = 1;
  6897. cross_idr = 0;
  6898. for(i=0; h->delayed_pic[i]; i++)
  6899. if(h->delayed_pic[i]->key_frame || h->delayed_pic[i]->poc==0)
  6900. cross_idr = 1;
  6901. out = h->delayed_pic[0];
  6902. out_idx = 0;
  6903. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
  6904. if(h->delayed_pic[i]->poc < out->poc){
  6905. out = h->delayed_pic[i];
  6906. out_idx = i;
  6907. }
  6908. out_of_order = !cross_idr && prev && out->poc < prev->poc;
  6909. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  6910. { }
  6911. else if(prev && pics <= s->avctx->has_b_frames)
  6912. out = prev;
  6913. else if((out_of_order && pics-1 == s->avctx->has_b_frames && pics < 15)
  6914. || (s->low_delay &&
  6915. ((!cross_idr && prev && out->poc > prev->poc + 2)
  6916. || cur->pict_type == B_TYPE)))
  6917. {
  6918. s->low_delay = 0;
  6919. s->avctx->has_b_frames++;
  6920. out = prev;
  6921. }
  6922. else if(out_of_order)
  6923. out = prev;
  6924. if(out_of_order || pics > s->avctx->has_b_frames){
  6925. for(i=out_idx; h->delayed_pic[i]; i++)
  6926. h->delayed_pic[i] = h->delayed_pic[i+1];
  6927. }
  6928. if(prev == out)
  6929. *data_size = 0;
  6930. else
  6931. *data_size = sizeof(AVFrame);
  6932. if(prev && prev != out && prev->reference == 1)
  6933. prev->reference = 0;
  6934. h->delayed_output_pic = out;
  6935. #endif
  6936. if(out)
  6937. *pict= *(AVFrame*)out;
  6938. else
  6939. av_log(avctx, AV_LOG_DEBUG, "no picture\n");
  6940. }
  6941. assert(pict->data[0] || !*data_size);
  6942. ff_print_debug_info(s, pict);
  6943. //printf("out %d\n", (int)pict->data[0]);
  6944. #if 0 //?
  6945. /* Return the Picture timestamp as the frame number */
  6946. /* we substract 1 because it is added on utils.c */
  6947. avctx->frame_number = s->picture_number - 1;
  6948. #endif
  6949. return get_consumed_bytes(s, buf_index, buf_size);
  6950. }
  6951. #if 0
  6952. static inline void fill_mb_avail(H264Context *h){
  6953. MpegEncContext * const s = &h->s;
  6954. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  6955. if(s->mb_y){
  6956. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  6957. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  6958. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  6959. }else{
  6960. h->mb_avail[0]=
  6961. h->mb_avail[1]=
  6962. h->mb_avail[2]= 0;
  6963. }
  6964. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  6965. h->mb_avail[4]= 1; //FIXME move out
  6966. h->mb_avail[5]= 0; //FIXME move out
  6967. }
  6968. #endif
  6969. #if 0 //selftest
  6970. #define COUNT 8000
  6971. #define SIZE (COUNT*40)
  6972. int main(){
  6973. int i;
  6974. uint8_t temp[SIZE];
  6975. PutBitContext pb;
  6976. GetBitContext gb;
  6977. // int int_temp[10000];
  6978. DSPContext dsp;
  6979. AVCodecContext avctx;
  6980. dsputil_init(&dsp, &avctx);
  6981. init_put_bits(&pb, temp, SIZE);
  6982. printf("testing unsigned exp golomb\n");
  6983. for(i=0; i<COUNT; i++){
  6984. START_TIMER
  6985. set_ue_golomb(&pb, i);
  6986. STOP_TIMER("set_ue_golomb");
  6987. }
  6988. flush_put_bits(&pb);
  6989. init_get_bits(&gb, temp, 8*SIZE);
  6990. for(i=0; i<COUNT; i++){
  6991. int j, s;
  6992. s= show_bits(&gb, 24);
  6993. START_TIMER
  6994. j= get_ue_golomb(&gb);
  6995. if(j != i){
  6996. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  6997. // return -1;
  6998. }
  6999. STOP_TIMER("get_ue_golomb");
  7000. }
  7001. init_put_bits(&pb, temp, SIZE);
  7002. printf("testing signed exp golomb\n");
  7003. for(i=0; i<COUNT; i++){
  7004. START_TIMER
  7005. set_se_golomb(&pb, i - COUNT/2);
  7006. STOP_TIMER("set_se_golomb");
  7007. }
  7008. flush_put_bits(&pb);
  7009. init_get_bits(&gb, temp, 8*SIZE);
  7010. for(i=0; i<COUNT; i++){
  7011. int j, s;
  7012. s= show_bits(&gb, 24);
  7013. START_TIMER
  7014. j= get_se_golomb(&gb);
  7015. if(j != i - COUNT/2){
  7016. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7017. // return -1;
  7018. }
  7019. STOP_TIMER("get_se_golomb");
  7020. }
  7021. printf("testing 4x4 (I)DCT\n");
  7022. DCTELEM block[16];
  7023. uint8_t src[16], ref[16];
  7024. uint64_t error= 0, max_error=0;
  7025. for(i=0; i<COUNT; i++){
  7026. int j;
  7027. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  7028. for(j=0; j<16; j++){
  7029. ref[j]= random()%255;
  7030. src[j]= random()%255;
  7031. }
  7032. h264_diff_dct_c(block, src, ref, 4);
  7033. //normalize
  7034. for(j=0; j<16; j++){
  7035. // printf("%d ", block[j]);
  7036. block[j]= block[j]*4;
  7037. if(j&1) block[j]= (block[j]*4 + 2)/5;
  7038. if(j&4) block[j]= (block[j]*4 + 2)/5;
  7039. }
  7040. // printf("\n");
  7041. s->dsp.h264_idct_add(ref, block, 4);
  7042. /* for(j=0; j<16; j++){
  7043. printf("%d ", ref[j]);
  7044. }
  7045. printf("\n");*/
  7046. for(j=0; j<16; j++){
  7047. int diff= ABS(src[j] - ref[j]);
  7048. error+= diff*diff;
  7049. max_error= FFMAX(max_error, diff);
  7050. }
  7051. }
  7052. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  7053. #if 0
  7054. printf("testing quantizer\n");
  7055. for(qp=0; qp<52; qp++){
  7056. for(i=0; i<16; i++)
  7057. src1_block[i]= src2_block[i]= random()%255;
  7058. }
  7059. #endif
  7060. printf("Testing NAL layer\n");
  7061. uint8_t bitstream[COUNT];
  7062. uint8_t nal[COUNT*2];
  7063. H264Context h;
  7064. memset(&h, 0, sizeof(H264Context));
  7065. for(i=0; i<COUNT; i++){
  7066. int zeros= i;
  7067. int nal_length;
  7068. int consumed;
  7069. int out_length;
  7070. uint8_t *out;
  7071. int j;
  7072. for(j=0; j<COUNT; j++){
  7073. bitstream[j]= (random() % 255) + 1;
  7074. }
  7075. for(j=0; j<zeros; j++){
  7076. int pos= random() % COUNT;
  7077. while(bitstream[pos] == 0){
  7078. pos++;
  7079. pos %= COUNT;
  7080. }
  7081. bitstream[pos]=0;
  7082. }
  7083. START_TIMER
  7084. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  7085. if(nal_length<0){
  7086. printf("encoding failed\n");
  7087. return -1;
  7088. }
  7089. out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
  7090. STOP_TIMER("NAL")
  7091. if(out_length != COUNT){
  7092. printf("incorrect length %d %d\n", out_length, COUNT);
  7093. return -1;
  7094. }
  7095. if(consumed != nal_length){
  7096. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  7097. return -1;
  7098. }
  7099. if(memcmp(bitstream, out, COUNT)){
  7100. printf("missmatch\n");
  7101. return -1;
  7102. }
  7103. }
  7104. printf("Testing RBSP\n");
  7105. return 0;
  7106. }
  7107. #endif
  7108. static int decode_end(AVCodecContext *avctx)
  7109. {
  7110. H264Context *h = avctx->priv_data;
  7111. MpegEncContext *s = &h->s;
  7112. av_freep(&h->rbsp_buffer);
  7113. free_tables(h); //FIXME cleanup init stuff perhaps
  7114. MPV_common_end(s);
  7115. // memset(h, 0, sizeof(H264Context));
  7116. return 0;
  7117. }
  7118. AVCodec h264_decoder = {
  7119. "h264",
  7120. CODEC_TYPE_VIDEO,
  7121. CODEC_ID_H264,
  7122. sizeof(H264Context),
  7123. decode_init,
  7124. NULL,
  7125. decode_end,
  7126. decode_frame,
  7127. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  7128. .flush= flush_dpb,
  7129. };
  7130. AVCodecParser h264_parser = {
  7131. { CODEC_ID_H264 },
  7132. sizeof(H264Context),
  7133. NULL,
  7134. h264_parse,
  7135. ff_parse_close,
  7136. h264_split,
  7137. };
  7138. #include "svq3.c"