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.

7232 lines
266KB

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