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.

7991 lines
296KB

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