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.

7673 lines
285KB

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