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.

7748 lines
288KB

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