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.

7251 lines
267KB

  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= -1;
  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=-1;
  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 != -1);
  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. step = -step;
  2808. j= smallest_poc_greater_than_current + (step>>1);
  2809. }
  2810. if(sorted_short_ref[j].reference != 3) continue;
  2811. h->default_ref_list[list][index ]= sorted_short_ref[j];
  2812. h->default_ref_list[list][index++].pic_id= sorted_short_ref[j].frame_num;
  2813. }
  2814. for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){
  2815. if(h->long_ref[i] == NULL) continue;
  2816. if(h->long_ref[i]->reference != 3) continue;
  2817. h->default_ref_list[ list ][index ]= *h->long_ref[i];
  2818. h->default_ref_list[ list ][index++].pic_id= i;;
  2819. }
  2820. if(list && (smallest_poc_greater_than_current<=0 || smallest_poc_greater_than_current>=h->short_ref_count) && (1 < index)){
  2821. // swap the two first elements of L1 when
  2822. // L0 and L1 are identical
  2823. Picture temp= h->default_ref_list[1][0];
  2824. h->default_ref_list[1][0] = h->default_ref_list[1][1];
  2825. h->default_ref_list[1][1] = temp;
  2826. }
  2827. if(index < h->ref_count[ list ])
  2828. memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
  2829. }
  2830. }else{
  2831. int index=0;
  2832. for(i=0; i<h->short_ref_count; i++){
  2833. if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
  2834. h->default_ref_list[0][index ]= *h->short_ref[i];
  2835. h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
  2836. }
  2837. for(i = 0; i < 16; i++){
  2838. if(h->long_ref[i] == NULL) continue;
  2839. if(h->long_ref[i]->reference != 3) continue;
  2840. h->default_ref_list[0][index ]= *h->long_ref[i];
  2841. h->default_ref_list[0][index++].pic_id= i;;
  2842. }
  2843. if(index < h->ref_count[0])
  2844. memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
  2845. }
  2846. }else{ //FIELD
  2847. if(h->slice_type==B_TYPE){
  2848. }else{
  2849. //FIXME second field balh
  2850. }
  2851. }
  2852. #ifdef TRACE
  2853. for (i=0; i<h->ref_count[0]; i++) {
  2854. 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]);
  2855. }
  2856. if(h->slice_type==B_TYPE){
  2857. for (i=0; i<h->ref_count[1]; i++) {
  2858. 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]);
  2859. }
  2860. }
  2861. #endif
  2862. return 0;
  2863. }
  2864. static void print_short_term(H264Context *h);
  2865. static void print_long_term(H264Context *h);
  2866. static int decode_ref_pic_list_reordering(H264Context *h){
  2867. MpegEncContext * const s = &h->s;
  2868. int list;
  2869. print_short_term(h);
  2870. print_long_term(h);
  2871. if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move before func
  2872. for(list=0; list<2; list++){
  2873. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  2874. if(get_bits1(&s->gb)){
  2875. int pred= h->curr_pic_num;
  2876. int index;
  2877. for(index=0; ; index++){
  2878. int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
  2879. int pic_id;
  2880. int i;
  2881. Picture *ref = NULL;
  2882. if(reordering_of_pic_nums_idc==3)
  2883. break;
  2884. if(index >= h->ref_count[list]){
  2885. av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
  2886. return -1;
  2887. }
  2888. if(reordering_of_pic_nums_idc<3){
  2889. if(reordering_of_pic_nums_idc<2){
  2890. const int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  2891. if(abs_diff_pic_num >= h->max_pic_num){
  2892. av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  2893. return -1;
  2894. }
  2895. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  2896. else pred+= abs_diff_pic_num;
  2897. pred &= h->max_pic_num - 1;
  2898. for(i= h->ref_count[list]-1; i>=0; i--){
  2899. if(h->ref_list[list][i].data[0] != NULL && h->ref_list[list][i].pic_id == pred && h->ref_list[list][i].long_ref==0) // ignore non existing pictures by testing data[0] pointer
  2900. break;
  2901. }
  2902. }else{
  2903. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  2904. for(i= h->ref_count[list]-1; i>=0; i--){
  2905. if(h->ref_list[list][i].pic_id == pic_id && h->ref_list[list][i].long_ref==1) // no need to ignore non existing pictures as non existing pictures have long_ref==0
  2906. break;
  2907. }
  2908. }
  2909. if (i < 0) {
  2910. av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  2911. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  2912. } else if (i != index) /* this test is not necessary, it is only an optimisation to skip double copy of Picture structure in this case */ {
  2913. Picture tmp= h->ref_list[list][i];
  2914. if (i < index) {
  2915. i = h->ref_count[list];
  2916. }
  2917. for(; i > index; i--){
  2918. h->ref_list[list][i]= h->ref_list[list][i-1];
  2919. }
  2920. h->ref_list[list][index]= tmp;
  2921. }
  2922. }else{
  2923. av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  2924. return -1;
  2925. }
  2926. }
  2927. }
  2928. if(h->slice_type!=B_TYPE) break;
  2929. }
  2930. if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)
  2931. direct_dist_scale_factor(h);
  2932. direct_ref_list_init(h);
  2933. return 0;
  2934. }
  2935. static int pred_weight_table(H264Context *h){
  2936. MpegEncContext * const s = &h->s;
  2937. int list, i;
  2938. int luma_def, chroma_def;
  2939. h->use_weight= 0;
  2940. h->use_weight_chroma= 0;
  2941. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  2942. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  2943. luma_def = 1<<h->luma_log2_weight_denom;
  2944. chroma_def = 1<<h->chroma_log2_weight_denom;
  2945. for(list=0; list<2; list++){
  2946. for(i=0; i<h->ref_count[list]; i++){
  2947. int luma_weight_flag, chroma_weight_flag;
  2948. luma_weight_flag= get_bits1(&s->gb);
  2949. if(luma_weight_flag){
  2950. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  2951. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  2952. if( h->luma_weight[list][i] != luma_def
  2953. || h->luma_offset[list][i] != 0)
  2954. h->use_weight= 1;
  2955. }else{
  2956. h->luma_weight[list][i]= luma_def;
  2957. h->luma_offset[list][i]= 0;
  2958. }
  2959. chroma_weight_flag= get_bits1(&s->gb);
  2960. if(chroma_weight_flag){
  2961. int j;
  2962. for(j=0; j<2; j++){
  2963. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  2964. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  2965. if( h->chroma_weight[list][i][j] != chroma_def
  2966. || h->chroma_offset[list][i][j] != 0)
  2967. h->use_weight_chroma= 1;
  2968. }
  2969. }else{
  2970. int j;
  2971. for(j=0; j<2; j++){
  2972. h->chroma_weight[list][i][j]= chroma_def;
  2973. h->chroma_offset[list][i][j]= 0;
  2974. }
  2975. }
  2976. }
  2977. if(h->slice_type != B_TYPE) break;
  2978. }
  2979. h->use_weight= h->use_weight || h->use_weight_chroma;
  2980. return 0;
  2981. }
  2982. static void implicit_weight_table(H264Context *h){
  2983. MpegEncContext * const s = &h->s;
  2984. int ref0, ref1;
  2985. int cur_poc = s->current_picture_ptr->poc;
  2986. if( h->ref_count[0] == 1 && h->ref_count[1] == 1
  2987. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  2988. h->use_weight= 0;
  2989. h->use_weight_chroma= 0;
  2990. return;
  2991. }
  2992. h->use_weight= 2;
  2993. h->use_weight_chroma= 2;
  2994. h->luma_log2_weight_denom= 5;
  2995. h->chroma_log2_weight_denom= 5;
  2996. /* FIXME: MBAFF */
  2997. for(ref0=0; ref0 < h->ref_count[0]; ref0++){
  2998. int poc0 = h->ref_list[0][ref0].poc;
  2999. for(ref1=0; ref1 < h->ref_count[1]; ref1++){
  3000. int poc1 = h->ref_list[1][ref1].poc;
  3001. int td = clip(poc1 - poc0, -128, 127);
  3002. if(td){
  3003. int tb = clip(cur_poc - poc0, -128, 127);
  3004. int tx = (16384 + (ABS(td) >> 1)) / td;
  3005. int dist_scale_factor = clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
  3006. if(dist_scale_factor < -64 || dist_scale_factor > 128)
  3007. h->implicit_weight[ref0][ref1] = 32;
  3008. else
  3009. h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
  3010. }else
  3011. h->implicit_weight[ref0][ref1] = 32;
  3012. }
  3013. }
  3014. }
  3015. static inline void unreference_pic(H264Context *h, Picture *pic){
  3016. int i;
  3017. pic->reference=0;
  3018. if(pic == h->delayed_output_pic)
  3019. pic->reference=1;
  3020. else{
  3021. for(i = 0; h->delayed_pic[i]; i++)
  3022. if(pic == h->delayed_pic[i]){
  3023. pic->reference=1;
  3024. break;
  3025. }
  3026. }
  3027. }
  3028. /**
  3029. * instantaneous decoder refresh.
  3030. */
  3031. static void idr(H264Context *h){
  3032. int i;
  3033. for(i=0; i<16; i++){
  3034. if (h->long_ref[i] != NULL) {
  3035. unreference_pic(h, h->long_ref[i]);
  3036. h->long_ref[i]= NULL;
  3037. }
  3038. }
  3039. h->long_ref_count=0;
  3040. for(i=0; i<h->short_ref_count; i++){
  3041. unreference_pic(h, h->short_ref[i]);
  3042. h->short_ref[i]= NULL;
  3043. }
  3044. h->short_ref_count=0;
  3045. }
  3046. /* forget old pics after a seek */
  3047. static void flush_dpb(AVCodecContext *avctx){
  3048. H264Context *h= avctx->priv_data;
  3049. int i;
  3050. for(i=0; i<16; i++)
  3051. h->delayed_pic[i]= NULL;
  3052. h->delayed_output_pic= NULL;
  3053. idr(h);
  3054. }
  3055. /**
  3056. *
  3057. * @return the removed picture or NULL if an error occurs
  3058. */
  3059. static Picture * remove_short(H264Context *h, int frame_num){
  3060. MpegEncContext * const s = &h->s;
  3061. int i;
  3062. if(s->avctx->debug&FF_DEBUG_MMCO)
  3063. av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  3064. for(i=0; i<h->short_ref_count; i++){
  3065. Picture *pic= h->short_ref[i];
  3066. if(s->avctx->debug&FF_DEBUG_MMCO)
  3067. av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  3068. if(pic->frame_num == frame_num){
  3069. h->short_ref[i]= NULL;
  3070. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
  3071. h->short_ref_count--;
  3072. return pic;
  3073. }
  3074. }
  3075. return NULL;
  3076. }
  3077. /**
  3078. *
  3079. * @return the removed picture or NULL if an error occurs
  3080. */
  3081. static Picture * remove_long(H264Context *h, int i){
  3082. Picture *pic;
  3083. pic= h->long_ref[i];
  3084. h->long_ref[i]= NULL;
  3085. if(pic) h->long_ref_count--;
  3086. return pic;
  3087. }
  3088. /**
  3089. * print short term list
  3090. */
  3091. static void print_short_term(H264Context *h) {
  3092. uint32_t i;
  3093. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3094. av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
  3095. for(i=0; i<h->short_ref_count; i++){
  3096. Picture *pic= h->short_ref[i];
  3097. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3098. }
  3099. }
  3100. }
  3101. /**
  3102. * print long term list
  3103. */
  3104. static void print_long_term(H264Context *h) {
  3105. uint32_t i;
  3106. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3107. av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
  3108. for(i = 0; i < 16; i++){
  3109. Picture *pic= h->long_ref[i];
  3110. if (pic) {
  3111. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3112. }
  3113. }
  3114. }
  3115. }
  3116. /**
  3117. * Executes the reference picture marking (memory management control operations).
  3118. */
  3119. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  3120. MpegEncContext * const s = &h->s;
  3121. int i, j;
  3122. int current_is_long=0;
  3123. Picture *pic;
  3124. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  3125. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  3126. for(i=0; i<mmco_count; i++){
  3127. if(s->avctx->debug&FF_DEBUG_MMCO)
  3128. 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);
  3129. switch(mmco[i].opcode){
  3130. case MMCO_SHORT2UNUSED:
  3131. pic= remove_short(h, mmco[i].short_frame_num);
  3132. if(pic==NULL) return -1;
  3133. unreference_pic(h, pic);
  3134. break;
  3135. case MMCO_SHORT2LONG:
  3136. pic= remove_long(h, mmco[i].long_index);
  3137. if(pic) unreference_pic(h, pic);
  3138. h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
  3139. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3140. h->long_ref_count++;
  3141. break;
  3142. case MMCO_LONG2UNUSED:
  3143. pic= remove_long(h, mmco[i].long_index);
  3144. if(pic==NULL) return -1;
  3145. unreference_pic(h, pic);
  3146. break;
  3147. case MMCO_LONG:
  3148. pic= remove_long(h, mmco[i].long_index);
  3149. if(pic) unreference_pic(h, pic);
  3150. h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
  3151. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3152. h->long_ref_count++;
  3153. current_is_long=1;
  3154. break;
  3155. case MMCO_SET_MAX_LONG:
  3156. assert(mmco[i].long_index <= 16);
  3157. // just remove the long term which index is greater than new max
  3158. for(j = mmco[i].long_index; j<16; j++){
  3159. pic = remove_long(h, j);
  3160. if (pic) unreference_pic(h, pic);
  3161. }
  3162. break;
  3163. case MMCO_RESET:
  3164. while(h->short_ref_count){
  3165. pic= remove_short(h, h->short_ref[0]->frame_num);
  3166. unreference_pic(h, pic);
  3167. }
  3168. for(j = 0; j < 16; j++) {
  3169. pic= remove_long(h, j);
  3170. if(pic) unreference_pic(h, pic);
  3171. }
  3172. break;
  3173. default: assert(0);
  3174. }
  3175. }
  3176. if(!current_is_long){
  3177. pic= remove_short(h, s->current_picture_ptr->frame_num);
  3178. if(pic){
  3179. unreference_pic(h, pic);
  3180. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  3181. }
  3182. if(h->short_ref_count)
  3183. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  3184. h->short_ref[0]= s->current_picture_ptr;
  3185. h->short_ref[0]->long_ref=0;
  3186. h->short_ref_count++;
  3187. }
  3188. print_short_term(h);
  3189. print_long_term(h);
  3190. return 0;
  3191. }
  3192. static int decode_ref_pic_marking(H264Context *h){
  3193. MpegEncContext * const s = &h->s;
  3194. int i;
  3195. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  3196. s->broken_link= get_bits1(&s->gb) -1;
  3197. h->mmco[0].long_index= get_bits1(&s->gb) - 1; // current_long_term_idx
  3198. if(h->mmco[0].long_index == -1)
  3199. h->mmco_index= 0;
  3200. else{
  3201. h->mmco[0].opcode= MMCO_LONG;
  3202. h->mmco_index= 1;
  3203. }
  3204. }else{
  3205. if(get_bits1(&s->gb)){ // adaptive_ref_pic_marking_mode_flag
  3206. for(i= 0; i<MAX_MMCO_COUNT; i++) {
  3207. MMCOOpcode opcode= get_ue_golomb(&s->gb);;
  3208. h->mmco[i].opcode= opcode;
  3209. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  3210. 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
  3211. /* if(h->mmco[i].short_frame_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_frame_num ] == NULL){
  3212. fprintf(stderr, "illegal short ref in memory management control operation %d\n", mmco);
  3213. return -1;
  3214. }*/
  3215. }
  3216. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  3217. h->mmco[i].long_index= get_ue_golomb(&s->gb);
  3218. 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){
  3219. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  3220. return -1;
  3221. }
  3222. }
  3223. if(opcode > MMCO_LONG){
  3224. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  3225. return -1;
  3226. }
  3227. if(opcode == MMCO_END)
  3228. break;
  3229. }
  3230. h->mmco_index= i;
  3231. }else{
  3232. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  3233. if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){ //FIXME fields
  3234. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  3235. h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  3236. h->mmco_index= 1;
  3237. }else
  3238. h->mmco_index= 0;
  3239. }
  3240. }
  3241. return 0;
  3242. }
  3243. static int init_poc(H264Context *h){
  3244. MpegEncContext * const s = &h->s;
  3245. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  3246. int field_poc[2];
  3247. if(h->nal_unit_type == NAL_IDR_SLICE){
  3248. h->frame_num_offset= 0;
  3249. }else{
  3250. if(h->frame_num < h->prev_frame_num)
  3251. h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
  3252. else
  3253. h->frame_num_offset= h->prev_frame_num_offset;
  3254. }
  3255. if(h->sps.poc_type==0){
  3256. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  3257. if(h->nal_unit_type == NAL_IDR_SLICE){
  3258. h->prev_poc_msb=
  3259. h->prev_poc_lsb= 0;
  3260. }
  3261. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  3262. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  3263. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  3264. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  3265. else
  3266. h->poc_msb = h->prev_poc_msb;
  3267. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  3268. field_poc[0] =
  3269. field_poc[1] = h->poc_msb + h->poc_lsb;
  3270. if(s->picture_structure == PICT_FRAME)
  3271. field_poc[1] += h->delta_poc_bottom;
  3272. }else if(h->sps.poc_type==1){
  3273. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  3274. int i;
  3275. if(h->sps.poc_cycle_length != 0)
  3276. abs_frame_num = h->frame_num_offset + h->frame_num;
  3277. else
  3278. abs_frame_num = 0;
  3279. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  3280. abs_frame_num--;
  3281. expected_delta_per_poc_cycle = 0;
  3282. for(i=0; i < h->sps.poc_cycle_length; i++)
  3283. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  3284. if(abs_frame_num > 0){
  3285. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  3286. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  3287. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  3288. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  3289. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  3290. } else
  3291. expectedpoc = 0;
  3292. if(h->nal_ref_idc == 0)
  3293. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  3294. field_poc[0] = expectedpoc + h->delta_poc[0];
  3295. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  3296. if(s->picture_structure == PICT_FRAME)
  3297. field_poc[1] += h->delta_poc[1];
  3298. }else{
  3299. int poc;
  3300. if(h->nal_unit_type == NAL_IDR_SLICE){
  3301. poc= 0;
  3302. }else{
  3303. if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
  3304. else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
  3305. }
  3306. field_poc[0]= poc;
  3307. field_poc[1]= poc;
  3308. }
  3309. if(s->picture_structure != PICT_BOTTOM_FIELD)
  3310. s->current_picture_ptr->field_poc[0]= field_poc[0];
  3311. if(s->picture_structure != PICT_TOP_FIELD)
  3312. s->current_picture_ptr->field_poc[1]= field_poc[1];
  3313. if(s->picture_structure == PICT_FRAME) // FIXME field pix?
  3314. s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]);
  3315. return 0;
  3316. }
  3317. /**
  3318. * decodes a slice header.
  3319. * this will allso call MPV_common_init() and frame_start() as needed
  3320. */
  3321. static int decode_slice_header(H264Context *h){
  3322. MpegEncContext * const s = &h->s;
  3323. int first_mb_in_slice, pps_id;
  3324. int num_ref_idx_active_override_flag;
  3325. static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
  3326. int slice_type;
  3327. int default_ref_list_done = 0;
  3328. s->current_picture.reference= h->nal_ref_idc != 0;
  3329. s->dropable= h->nal_ref_idc == 0;
  3330. first_mb_in_slice= get_ue_golomb(&s->gb);
  3331. slice_type= get_ue_golomb(&s->gb);
  3332. if(slice_type > 9){
  3333. 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);
  3334. return -1;
  3335. }
  3336. if(slice_type > 4){
  3337. slice_type -= 5;
  3338. h->slice_type_fixed=1;
  3339. }else
  3340. h->slice_type_fixed=0;
  3341. slice_type= slice_type_map[ slice_type ];
  3342. if (slice_type == I_TYPE
  3343. || (h->slice_num != 0 && slice_type == h->slice_type) ) {
  3344. default_ref_list_done = 1;
  3345. }
  3346. h->slice_type= slice_type;
  3347. s->pict_type= h->slice_type; // to make a few old func happy, it's wrong though
  3348. pps_id= get_ue_golomb(&s->gb);
  3349. if(pps_id>255){
  3350. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  3351. return -1;
  3352. }
  3353. h->pps= h->pps_buffer[pps_id];
  3354. if(h->pps.slice_group_count == 0){
  3355. av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referenced\n");
  3356. return -1;
  3357. }
  3358. h->sps= h->sps_buffer[ h->pps.sps_id ];
  3359. if(h->sps.log2_max_frame_num == 0){
  3360. av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referenced\n");
  3361. return -1;
  3362. }
  3363. s->mb_width= h->sps.mb_width;
  3364. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  3365. h->b_stride= s->mb_width*4 + 1;
  3366. h->b8_stride= s->mb_width*2 + 1;
  3367. s->width = 16*s->mb_width - 2*(h->sps.crop_left + h->sps.crop_right );
  3368. if(h->sps.frame_mbs_only_flag)
  3369. s->height= 16*s->mb_height - 2*(h->sps.crop_top + h->sps.crop_bottom);
  3370. else
  3371. s->height= 16*s->mb_height - 4*(h->sps.crop_top + h->sps.crop_bottom); //FIXME recheck
  3372. if (s->context_initialized
  3373. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  3374. free_tables(h);
  3375. MPV_common_end(s);
  3376. }
  3377. if (!s->context_initialized) {
  3378. if (MPV_common_init(s) < 0)
  3379. return -1;
  3380. alloc_tables(h);
  3381. s->avctx->width = s->width;
  3382. s->avctx->height = s->height;
  3383. s->avctx->sample_aspect_ratio= h->sps.sar;
  3384. if(!s->avctx->sample_aspect_ratio.den)
  3385. s->avctx->sample_aspect_ratio.den = 1;
  3386. if(h->sps.timing_info_present_flag){
  3387. s->avctx->time_base= (AVRational){h->sps.num_units_in_tick, h->sps.time_scale};
  3388. }
  3389. }
  3390. if(h->slice_num == 0){
  3391. frame_start(h);
  3392. }
  3393. s->current_picture_ptr->frame_num= //FIXME frame_num cleanup
  3394. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  3395. h->mb_aff_frame = 0;
  3396. if(h->sps.frame_mbs_only_flag){
  3397. s->picture_structure= PICT_FRAME;
  3398. }else{
  3399. if(get_bits1(&s->gb)) { //field_pic_flag
  3400. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  3401. } else {
  3402. s->picture_structure= PICT_FRAME;
  3403. first_mb_in_slice <<= 1;
  3404. h->mb_aff_frame = h->sps.mb_aff;
  3405. }
  3406. }
  3407. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  3408. s->resync_mb_y = s->mb_y = first_mb_in_slice / s->mb_width;
  3409. if(s->picture_structure==PICT_FRAME){
  3410. h->curr_pic_num= h->frame_num;
  3411. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  3412. }else{
  3413. h->curr_pic_num= 2*h->frame_num;
  3414. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  3415. }
  3416. if(h->nal_unit_type == NAL_IDR_SLICE){
  3417. get_ue_golomb(&s->gb); /* idr_pic_id */
  3418. }
  3419. if(h->sps.poc_type==0){
  3420. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  3421. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  3422. h->delta_poc_bottom= get_se_golomb(&s->gb);
  3423. }
  3424. }
  3425. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  3426. h->delta_poc[0]= get_se_golomb(&s->gb);
  3427. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  3428. h->delta_poc[1]= get_se_golomb(&s->gb);
  3429. }
  3430. init_poc(h);
  3431. if(h->pps.redundant_pic_cnt_present){
  3432. h->redundant_pic_count= get_ue_golomb(&s->gb);
  3433. }
  3434. //set defaults, might be overriden a few line later
  3435. h->ref_count[0]= h->pps.ref_count[0];
  3436. h->ref_count[1]= h->pps.ref_count[1];
  3437. if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){
  3438. if(h->slice_type == B_TYPE){
  3439. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  3440. }
  3441. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  3442. if(num_ref_idx_active_override_flag){
  3443. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  3444. if(h->slice_type==B_TYPE)
  3445. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  3446. if(h->ref_count[0] > 32 || h->ref_count[1] > 32){
  3447. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  3448. return -1;
  3449. }
  3450. }
  3451. }
  3452. if(!default_ref_list_done){
  3453. fill_default_ref_list(h);
  3454. }
  3455. decode_ref_pic_list_reordering(h);
  3456. if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
  3457. || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
  3458. pred_weight_table(h);
  3459. else if(h->pps.weighted_bipred_idc==2 && h->slice_type==B_TYPE)
  3460. implicit_weight_table(h);
  3461. else
  3462. h->use_weight = 0;
  3463. if(s->current_picture.reference)
  3464. decode_ref_pic_marking(h);
  3465. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac )
  3466. h->cabac_init_idc = get_ue_golomb(&s->gb);
  3467. h->last_qscale_diff = 0;
  3468. s->qscale = h->pps.init_qp + get_se_golomb(&s->gb);
  3469. if(s->qscale<0 || s->qscale>51){
  3470. av_log(s->avctx, AV_LOG_ERROR, "QP %d out of range\n", s->qscale);
  3471. return -1;
  3472. }
  3473. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  3474. //FIXME qscale / qp ... stuff
  3475. if(h->slice_type == SP_TYPE){
  3476. get_bits1(&s->gb); /* sp_for_switch_flag */
  3477. }
  3478. if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
  3479. get_se_golomb(&s->gb); /* slice_qs_delta */
  3480. }
  3481. h->deblocking_filter = 1;
  3482. h->slice_alpha_c0_offset = 0;
  3483. h->slice_beta_offset = 0;
  3484. if( h->pps.deblocking_filter_parameters_present ) {
  3485. h->deblocking_filter= get_ue_golomb(&s->gb);
  3486. if(h->deblocking_filter < 2)
  3487. h->deblocking_filter^= 1; // 1<->0
  3488. if( h->deblocking_filter ) {
  3489. h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
  3490. h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
  3491. }
  3492. }
  3493. #if 0 //FMO
  3494. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  3495. slice_group_change_cycle= get_bits(&s->gb, ?);
  3496. #endif
  3497. h->slice_num++;
  3498. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  3499. 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",
  3500. h->slice_num,
  3501. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  3502. first_mb_in_slice,
  3503. av_get_pict_type_char(h->slice_type),
  3504. pps_id, h->frame_num,
  3505. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  3506. h->ref_count[0], h->ref_count[1],
  3507. s->qscale,
  3508. h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2,
  3509. h->use_weight,
  3510. h->use_weight==1 && h->use_weight_chroma ? "c" : ""
  3511. );
  3512. }
  3513. return 0;
  3514. }
  3515. /**
  3516. *
  3517. */
  3518. static inline int get_level_prefix(GetBitContext *gb){
  3519. unsigned int buf;
  3520. int log;
  3521. OPEN_READER(re, gb);
  3522. UPDATE_CACHE(re, gb);
  3523. buf=GET_CACHE(re, gb);
  3524. log= 32 - av_log2(buf);
  3525. #ifdef TRACE
  3526. print_bin(buf>>(32-log), log);
  3527. 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__);
  3528. #endif
  3529. LAST_SKIP_BITS(re, gb, log);
  3530. CLOSE_READER(re, gb);
  3531. return log-1;
  3532. }
  3533. /**
  3534. * decodes a residual block.
  3535. * @param n block index
  3536. * @param scantable scantable
  3537. * @param max_coeff number of coefficients in the block
  3538. * @return <0 if an error occured
  3539. */
  3540. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, int qp, int max_coeff){
  3541. MpegEncContext * const s = &h->s;
  3542. const uint16_t *qmul= dequant_coeff[qp];
  3543. 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};
  3544. int level[16], run[16];
  3545. int suffix_length, zeros_left, coeff_num, coeff_token, total_coeff, i, trailing_ones;
  3546. //FIXME put trailing_onex into the context
  3547. if(n == CHROMA_DC_BLOCK_INDEX){
  3548. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  3549. total_coeff= coeff_token>>2;
  3550. }else{
  3551. if(n == LUMA_DC_BLOCK_INDEX){
  3552. total_coeff= pred_non_zero_count(h, 0);
  3553. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  3554. total_coeff= coeff_token>>2;
  3555. }else{
  3556. total_coeff= pred_non_zero_count(h, n);
  3557. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  3558. total_coeff= coeff_token>>2;
  3559. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  3560. }
  3561. }
  3562. //FIXME set last_non_zero?
  3563. if(total_coeff==0)
  3564. return 0;
  3565. trailing_ones= coeff_token&3;
  3566. tprintf("trailing:%d, total:%d\n", trailing_ones, total_coeff);
  3567. assert(total_coeff<=16);
  3568. for(i=0; i<trailing_ones; i++){
  3569. level[i]= 1 - 2*get_bits1(gb);
  3570. }
  3571. suffix_length= total_coeff > 10 && trailing_ones < 3;
  3572. for(; i<total_coeff; i++){
  3573. const int prefix= get_level_prefix(gb);
  3574. int level_code, mask;
  3575. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  3576. if(suffix_length)
  3577. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  3578. else
  3579. level_code= (prefix<<suffix_length); //part
  3580. }else if(prefix==14){
  3581. if(suffix_length)
  3582. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  3583. else
  3584. level_code= prefix + get_bits(gb, 4); //part
  3585. }else if(prefix==15){
  3586. level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part
  3587. if(suffix_length==0) level_code+=15; //FIXME doesn't make (much)sense
  3588. }else{
  3589. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  3590. return -1;
  3591. }
  3592. if(i==trailing_ones && i<3) level_code+= 2; //FIXME split first iteration
  3593. mask= -(level_code&1);
  3594. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  3595. if(suffix_length==0) suffix_length=1; //FIXME split first iteration
  3596. #if 1
  3597. if(ABS(level[i]) > (3<<(suffix_length-1)) && suffix_length<6) suffix_length++;
  3598. #else
  3599. if((2+level_code)>>1) > (3<<(suffix_length-1)) && suffix_length<6) suffix_length++;
  3600. /* ? == prefix > 2 or sth */
  3601. #endif
  3602. tprintf("level: %d suffix_length:%d\n", level[i], suffix_length);
  3603. }
  3604. if(total_coeff == max_coeff)
  3605. zeros_left=0;
  3606. else{
  3607. if(n == CHROMA_DC_BLOCK_INDEX)
  3608. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  3609. else
  3610. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  3611. }
  3612. for(i=0; i<total_coeff-1; i++){
  3613. if(zeros_left <=0)
  3614. break;
  3615. else if(zeros_left < 7){
  3616. run[i]= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  3617. }else{
  3618. run[i]= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  3619. }
  3620. zeros_left -= run[i];
  3621. }
  3622. if(zeros_left<0){
  3623. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  3624. return -1;
  3625. }
  3626. for(; i<total_coeff-1; i++){
  3627. run[i]= 0;
  3628. }
  3629. run[i]= zeros_left;
  3630. coeff_num=-1;
  3631. if(n > 24){
  3632. for(i=total_coeff-1; i>=0; i--){ //FIXME merge into rundecode?
  3633. int j;
  3634. coeff_num += run[i] + 1; //FIXME add 1 earlier ?
  3635. j= scantable[ coeff_num ];
  3636. block[j]= level[i];
  3637. }
  3638. }else{
  3639. for(i=total_coeff-1; i>=0; i--){ //FIXME merge into rundecode?
  3640. int j;
  3641. coeff_num += run[i] + 1; //FIXME add 1 earlier ?
  3642. j= scantable[ coeff_num ];
  3643. block[j]= level[i] * qmul[j];
  3644. // printf("%d %d ", block[j], qmul[j]);
  3645. }
  3646. }
  3647. return 0;
  3648. }
  3649. /**
  3650. * decodes a P_SKIP or B_SKIP macroblock
  3651. */
  3652. static void decode_mb_skip(H264Context *h){
  3653. MpegEncContext * const s = &h->s;
  3654. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  3655. int mb_type=0;
  3656. memset(h->non_zero_count[mb_xy], 0, 16);
  3657. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  3658. if(h->mb_aff_frame && s->mb_skip_run==0 && (s->mb_y&1)==0){
  3659. h->mb_field_decoding_flag= get_bits1(&s->gb);
  3660. }
  3661. if(h->mb_field_decoding_flag)
  3662. mb_type|= MB_TYPE_INTERLACED;
  3663. if( h->slice_type == B_TYPE )
  3664. {
  3665. // just for fill_caches. pred_direct_motion will set the real mb_type
  3666. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
  3667. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  3668. pred_direct_motion(h, &mb_type);
  3669. if(h->pps.cabac){
  3670. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  3671. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  3672. }
  3673. }
  3674. else
  3675. {
  3676. int mx, my;
  3677. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
  3678. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  3679. pred_pskip_motion(h, &mx, &my);
  3680. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  3681. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  3682. if(h->pps.cabac)
  3683. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  3684. }
  3685. write_back_motion(h, mb_type);
  3686. s->current_picture.mb_type[mb_xy]= mb_type|MB_TYPE_SKIP;
  3687. s->current_picture.qscale_table[mb_xy]= s->qscale;
  3688. h->slice_table[ mb_xy ]= h->slice_num;
  3689. h->prev_mb_skipped= 1;
  3690. }
  3691. /**
  3692. * decodes a macroblock
  3693. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  3694. */
  3695. static int decode_mb_cavlc(H264Context *h){
  3696. MpegEncContext * const s = &h->s;
  3697. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  3698. int mb_type, partition_count, cbp;
  3699. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?
  3700. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  3701. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  3702. down the code */
  3703. if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){
  3704. if(s->mb_skip_run==-1)
  3705. s->mb_skip_run= get_ue_golomb(&s->gb);
  3706. if (s->mb_skip_run--) {
  3707. decode_mb_skip(h);
  3708. return 0;
  3709. }
  3710. }
  3711. if(h->mb_aff_frame){
  3712. if ( ((s->mb_y&1) == 0) || h->prev_mb_skipped)
  3713. h->mb_field_decoding_flag = get_bits1(&s->gb);
  3714. }else
  3715. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  3716. h->prev_mb_skipped= 0;
  3717. mb_type= get_ue_golomb(&s->gb);
  3718. if(h->slice_type == B_TYPE){
  3719. if(mb_type < 23){
  3720. partition_count= b_mb_type_info[mb_type].partition_count;
  3721. mb_type= b_mb_type_info[mb_type].type;
  3722. }else{
  3723. mb_type -= 23;
  3724. goto decode_intra_mb;
  3725. }
  3726. }else if(h->slice_type == P_TYPE /*|| h->slice_type == SP_TYPE */){
  3727. if(mb_type < 5){
  3728. partition_count= p_mb_type_info[mb_type].partition_count;
  3729. mb_type= p_mb_type_info[mb_type].type;
  3730. }else{
  3731. mb_type -= 5;
  3732. goto decode_intra_mb;
  3733. }
  3734. }else{
  3735. assert(h->slice_type == I_TYPE);
  3736. decode_intra_mb:
  3737. if(mb_type > 25){
  3738. 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);
  3739. return -1;
  3740. }
  3741. partition_count=0;
  3742. cbp= i_mb_type_info[mb_type].cbp;
  3743. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  3744. mb_type= i_mb_type_info[mb_type].type;
  3745. }
  3746. if(h->mb_field_decoding_flag)
  3747. mb_type |= MB_TYPE_INTERLACED;
  3748. s->current_picture.mb_type[mb_xy]= mb_type;
  3749. h->slice_table[ mb_xy ]= h->slice_num;
  3750. if(IS_INTRA_PCM(mb_type)){
  3751. unsigned int x, y;
  3752. // we assume these blocks are very rare so we dont optimize it
  3753. align_get_bits(&s->gb);
  3754. // The pixels are stored in the same order as levels in h->mb array.
  3755. for(y=0; y<16; y++){
  3756. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  3757. for(x=0; x<16; x++){
  3758. tprintf("LUMA ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  3759. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= get_bits(&s->gb, 8);
  3760. }
  3761. }
  3762. for(y=0; y<8; y++){
  3763. const int index= 256 + 4*(y&3) + 32*(y>>2);
  3764. for(x=0; x<8; x++){
  3765. tprintf("CHROMA U ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  3766. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  3767. }
  3768. }
  3769. for(y=0; y<8; y++){
  3770. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  3771. for(x=0; x<8; x++){
  3772. tprintf("CHROMA V ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  3773. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  3774. }
  3775. }
  3776. // In deblocking, the quantizer is 0
  3777. s->current_picture.qscale_table[mb_xy]= 0;
  3778. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  3779. // All coeffs are present
  3780. memset(h->non_zero_count[mb_xy], 16, 16);
  3781. return 0;
  3782. }
  3783. fill_caches(h, mb_type, 0);
  3784. //mb_pred
  3785. if(IS_INTRA(mb_type)){
  3786. // init_top_left_availability(h);
  3787. if(IS_INTRA4x4(mb_type)){
  3788. int i;
  3789. // fill_intra4x4_pred_table(h);
  3790. for(i=0; i<16; i++){
  3791. const int mode_coded= !get_bits1(&s->gb);
  3792. const int predicted_mode= pred_intra_mode(h, i);
  3793. int mode;
  3794. if(mode_coded){
  3795. const int rem_mode= get_bits(&s->gb, 3);
  3796. if(rem_mode<predicted_mode)
  3797. mode= rem_mode;
  3798. else
  3799. mode= rem_mode + 1;
  3800. }else{
  3801. mode= predicted_mode;
  3802. }
  3803. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  3804. }
  3805. write_back_intra_pred_mode(h);
  3806. if( check_intra4x4_pred_mode(h) < 0)
  3807. return -1;
  3808. }else{
  3809. h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
  3810. if(h->intra16x16_pred_mode < 0)
  3811. return -1;
  3812. }
  3813. h->chroma_pred_mode= get_ue_golomb(&s->gb);
  3814. h->chroma_pred_mode= check_intra_pred_mode(h, h->chroma_pred_mode);
  3815. if(h->chroma_pred_mode < 0)
  3816. return -1;
  3817. }else if(partition_count==4){
  3818. int i, j, sub_partition_count[4], list, ref[2][4];
  3819. if(h->slice_type == B_TYPE){
  3820. for(i=0; i<4; i++){
  3821. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  3822. if(h->sub_mb_type[i] >=13){
  3823. 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);
  3824. return -1;
  3825. }
  3826. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  3827. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  3828. }
  3829. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  3830. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3]))
  3831. pred_direct_motion(h, &mb_type);
  3832. }else{
  3833. assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE); //FIXME SP correct ?
  3834. for(i=0; i<4; i++){
  3835. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  3836. if(h->sub_mb_type[i] >=4){
  3837. 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);
  3838. return -1;
  3839. }
  3840. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  3841. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  3842. }
  3843. }
  3844. for(list=0; list<2; list++){
  3845. int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  3846. if(ref_count == 0) continue;
  3847. if (h->mb_aff_frame && h->mb_field_decoding_flag) {
  3848. ref_count <<= 1;
  3849. }
  3850. for(i=0; i<4; i++){
  3851. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  3852. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  3853. ref[list][i] = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
  3854. }else{
  3855. //FIXME
  3856. ref[list][i] = -1;
  3857. }
  3858. }
  3859. }
  3860. for(list=0; list<2; list++){
  3861. const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  3862. if(ref_count == 0) continue;
  3863. for(i=0; i<4; i++){
  3864. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  3865. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  3866. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  3867. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  3868. const int sub_mb_type= h->sub_mb_type[i];
  3869. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  3870. for(j=0; j<sub_partition_count[i]; j++){
  3871. int mx, my;
  3872. const int index= 4*i + block_width*j;
  3873. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  3874. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  3875. mx += get_se_golomb(&s->gb);
  3876. my += get_se_golomb(&s->gb);
  3877. tprintf("final mv:%d %d\n", mx, my);
  3878. if(IS_SUB_8X8(sub_mb_type)){
  3879. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  3880. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  3881. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  3882. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  3883. }else if(IS_SUB_8X4(sub_mb_type)){
  3884. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  3885. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  3886. }else if(IS_SUB_4X8(sub_mb_type)){
  3887. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  3888. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  3889. }else{
  3890. assert(IS_SUB_4X4(sub_mb_type));
  3891. mv_cache[ 0 ][0]= mx;
  3892. mv_cache[ 0 ][1]= my;
  3893. }
  3894. }
  3895. }else{
  3896. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  3897. p[0] = p[1]=
  3898. p[8] = p[9]= 0;
  3899. }
  3900. }
  3901. }
  3902. }else if(IS_DIRECT(mb_type)){
  3903. pred_direct_motion(h, &mb_type);
  3904. s->current_picture.mb_type[mb_xy]= mb_type;
  3905. }else{
  3906. int list, mx, my, i;
  3907. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  3908. if(IS_16X16(mb_type)){
  3909. for(list=0; list<2; list++){
  3910. if(h->ref_count[list]>0){
  3911. if(IS_DIR(mb_type, 0, list)){
  3912. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  3913. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  3914. }else
  3915. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (LIST_NOT_USED&0xFF), 1);
  3916. }
  3917. }
  3918. for(list=0; list<2; list++){
  3919. if(IS_DIR(mb_type, 0, list)){
  3920. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  3921. mx += get_se_golomb(&s->gb);
  3922. my += get_se_golomb(&s->gb);
  3923. tprintf("final mv:%d %d\n", mx, my);
  3924. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  3925. }else
  3926. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  3927. }
  3928. }
  3929. else if(IS_16X8(mb_type)){
  3930. for(list=0; list<2; list++){
  3931. if(h->ref_count[list]>0){
  3932. for(i=0; i<2; i++){
  3933. if(IS_DIR(mb_type, i, list)){
  3934. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  3935. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  3936. }else
  3937. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  3938. }
  3939. }
  3940. }
  3941. for(list=0; list<2; list++){
  3942. for(i=0; i<2; i++){
  3943. if(IS_DIR(mb_type, i, list)){
  3944. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  3945. mx += get_se_golomb(&s->gb);
  3946. my += get_se_golomb(&s->gb);
  3947. tprintf("final mv:%d %d\n", mx, my);
  3948. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  3949. }else
  3950. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  3951. }
  3952. }
  3953. }else{
  3954. assert(IS_8X16(mb_type));
  3955. for(list=0; list<2; list++){
  3956. if(h->ref_count[list]>0){
  3957. for(i=0; i<2; i++){
  3958. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  3959. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  3960. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  3961. }else
  3962. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  3963. }
  3964. }
  3965. }
  3966. for(list=0; list<2; list++){
  3967. for(i=0; i<2; i++){
  3968. if(IS_DIR(mb_type, i, list)){
  3969. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  3970. mx += get_se_golomb(&s->gb);
  3971. my += get_se_golomb(&s->gb);
  3972. tprintf("final mv:%d %d\n", mx, my);
  3973. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  3974. }else
  3975. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  3976. }
  3977. }
  3978. }
  3979. }
  3980. if(IS_INTER(mb_type))
  3981. write_back_motion(h, mb_type);
  3982. if(!IS_INTRA16x16(mb_type)){
  3983. cbp= get_ue_golomb(&s->gb);
  3984. if(cbp > 47){
  3985. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%d) at %d %d\n", cbp, s->mb_x, s->mb_y);
  3986. return -1;
  3987. }
  3988. if(IS_INTRA4x4(mb_type))
  3989. cbp= golomb_to_intra4x4_cbp[cbp];
  3990. else
  3991. cbp= golomb_to_inter_cbp[cbp];
  3992. }
  3993. if(cbp || IS_INTRA16x16(mb_type)){
  3994. int i8x8, i4x4, chroma_idx;
  3995. int chroma_qp, dquant;
  3996. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  3997. const uint8_t *scan, *dc_scan;
  3998. // fill_non_zero_count_cache(h);
  3999. if(IS_INTERLACED(mb_type)){
  4000. scan= field_scan;
  4001. dc_scan= luma_dc_field_scan;
  4002. }else{
  4003. scan= zigzag_scan;
  4004. dc_scan= luma_dc_zigzag_scan;
  4005. }
  4006. dquant= get_se_golomb(&s->gb);
  4007. if( dquant > 25 || dquant < -26 ){
  4008. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  4009. return -1;
  4010. }
  4011. s->qscale += dquant;
  4012. if(((unsigned)s->qscale) > 51){
  4013. if(s->qscale<0) s->qscale+= 52;
  4014. else s->qscale-= 52;
  4015. }
  4016. h->chroma_qp= chroma_qp= get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4017. if(IS_INTRA16x16(mb_type)){
  4018. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, s->qscale, 16) < 0){
  4019. return -1; //FIXME continue if partitioned and other return -1 too
  4020. }
  4021. assert((cbp&15) == 0 || (cbp&15) == 15);
  4022. if(cbp&15){
  4023. for(i8x8=0; i8x8<4; i8x8++){
  4024. for(i4x4=0; i4x4<4; i4x4++){
  4025. const int index= i4x4 + 4*i8x8;
  4026. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, s->qscale, 15) < 0 ){
  4027. return -1;
  4028. }
  4029. }
  4030. }
  4031. }else{
  4032. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  4033. }
  4034. }else{
  4035. for(i8x8=0; i8x8<4; i8x8++){
  4036. if(cbp & (1<<i8x8)){
  4037. for(i4x4=0; i4x4<4; i4x4++){
  4038. const int index= i4x4 + 4*i8x8;
  4039. if( decode_residual(h, gb, h->mb + 16*index, index, scan, s->qscale, 16) <0 ){
  4040. return -1;
  4041. }
  4042. }
  4043. }else{
  4044. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4045. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  4046. }
  4047. }
  4048. }
  4049. if(cbp&0x30){
  4050. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  4051. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, chroma_qp, 4) < 0){
  4052. return -1;
  4053. }
  4054. }
  4055. if(cbp&0x20){
  4056. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  4057. for(i4x4=0; i4x4<4; i4x4++){
  4058. const int index= 16 + 4*chroma_idx + i4x4;
  4059. if( decode_residual(h, gb, h->mb + 16*index, index, scan + 1, chroma_qp, 15) < 0){
  4060. return -1;
  4061. }
  4062. }
  4063. }
  4064. }else{
  4065. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4066. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4067. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4068. }
  4069. }else{
  4070. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4071. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  4072. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4073. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4074. }
  4075. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4076. write_back_non_zero_count(h);
  4077. return 0;
  4078. }
  4079. static int decode_cabac_field_decoding_flag(H264Context *h) {
  4080. MpegEncContext * const s = &h->s;
  4081. const int mb_x = s->mb_x;
  4082. const int mb_y = s->mb_y & ~1;
  4083. const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
  4084. const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
  4085. unsigned int ctx = 0;
  4086. if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
  4087. ctx += 1;
  4088. }
  4089. if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
  4090. ctx += 1;
  4091. }
  4092. return get_cabac( &h->cabac, &h->cabac_state[70 + ctx] );
  4093. }
  4094. static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
  4095. uint8_t *state= &h->cabac_state[ctx_base];
  4096. int mb_type;
  4097. if(intra_slice){
  4098. MpegEncContext * const s = &h->s;
  4099. const int mba_xy = h->left_mb_xy[0];
  4100. const int mbb_xy = h->top_mb_xy;
  4101. int ctx=0;
  4102. if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
  4103. ctx++;
  4104. if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
  4105. ctx++;
  4106. if( get_cabac( &h->cabac, &state[ctx] ) == 0 )
  4107. return 0; /* I4x4 */
  4108. state += 2;
  4109. }else{
  4110. if( get_cabac( &h->cabac, &state[0] ) == 0 )
  4111. return 0; /* I4x4 */
  4112. }
  4113. if( get_cabac_terminate( &h->cabac ) )
  4114. return 25; /* PCM */
  4115. mb_type = 1; /* I16x16 */
  4116. if( get_cabac( &h->cabac, &state[1] ) )
  4117. mb_type += 12; /* cbp_luma != 0 */
  4118. if( get_cabac( &h->cabac, &state[2] ) ) {
  4119. if( get_cabac( &h->cabac, &state[2+intra_slice] ) )
  4120. mb_type += 4 * 2; /* cbp_chroma == 2 */
  4121. else
  4122. mb_type += 4 * 1; /* cbp_chroma == 1 */
  4123. }
  4124. if( get_cabac( &h->cabac, &state[3+intra_slice] ) )
  4125. mb_type += 2;
  4126. if( get_cabac( &h->cabac, &state[3+2*intra_slice] ) )
  4127. mb_type += 1;
  4128. return mb_type;
  4129. }
  4130. static int decode_cabac_mb_type( H264Context *h ) {
  4131. MpegEncContext * const s = &h->s;
  4132. if( h->slice_type == I_TYPE ) {
  4133. return decode_cabac_intra_mb_type(h, 3, 1);
  4134. } else if( h->slice_type == P_TYPE ) {
  4135. if( get_cabac( &h->cabac, &h->cabac_state[14] ) == 0 ) {
  4136. /* P-type */
  4137. if( get_cabac( &h->cabac, &h->cabac_state[15] ) == 0 ) {
  4138. if( get_cabac( &h->cabac, &h->cabac_state[16] ) == 0 )
  4139. return 0; /* P_L0_D16x16; */
  4140. else
  4141. return 3; /* P_8x8; */
  4142. } else {
  4143. if( get_cabac( &h->cabac, &h->cabac_state[17] ) == 0 )
  4144. return 2; /* P_L0_D8x16; */
  4145. else
  4146. return 1; /* P_L0_D16x8; */
  4147. }
  4148. } else {
  4149. return decode_cabac_intra_mb_type(h, 17, 0) + 5;
  4150. }
  4151. } else if( h->slice_type == B_TYPE ) {
  4152. const int mba_xy = h->left_mb_xy[0];
  4153. const int mbb_xy = h->top_mb_xy;
  4154. int ctx = 0;
  4155. int bits;
  4156. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] )
  4157. && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
  4158. ctx++;
  4159. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] )
  4160. && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
  4161. ctx++;
  4162. if( !get_cabac( &h->cabac, &h->cabac_state[27+ctx] ) )
  4163. return 0; /* B_Direct_16x16 */
  4164. if( !get_cabac( &h->cabac, &h->cabac_state[27+3] ) ) {
  4165. return 1 + get_cabac( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
  4166. }
  4167. bits = get_cabac( &h->cabac, &h->cabac_state[27+4] ) << 3;
  4168. bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] ) << 2;
  4169. bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] ) << 1;
  4170. bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] );
  4171. if( bits < 8 )
  4172. return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
  4173. else if( bits == 13 ) {
  4174. return decode_cabac_intra_mb_type(h, 32, 0) + 23;
  4175. } else if( bits == 14 )
  4176. return 11; /* B_L1_L0_8x16 */
  4177. else if( bits == 15 )
  4178. return 22; /* B_8x8 */
  4179. bits= ( bits<<1 ) | get_cabac( &h->cabac, &h->cabac_state[27+5] );
  4180. return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
  4181. } else {
  4182. /* TODO SI/SP frames? */
  4183. return -1;
  4184. }
  4185. }
  4186. static int decode_cabac_mb_skip( H264Context *h) {
  4187. MpegEncContext * const s = &h->s;
  4188. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  4189. const int mba_xy = mb_xy - 1;
  4190. const int mbb_xy = mb_xy - s->mb_stride;
  4191. int ctx = 0;
  4192. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
  4193. ctx++;
  4194. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
  4195. ctx++;
  4196. if( h->slice_type == P_TYPE || h->slice_type == SP_TYPE)
  4197. return get_cabac( &h->cabac, &h->cabac_state[11+ctx] );
  4198. else /* B-frame */
  4199. return get_cabac( &h->cabac, &h->cabac_state[24+ctx] );
  4200. }
  4201. static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
  4202. int mode = 0;
  4203. if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
  4204. return pred_mode;
  4205. if( get_cabac( &h->cabac, &h->cabac_state[69] ) )
  4206. mode += 1;
  4207. if( get_cabac( &h->cabac, &h->cabac_state[69] ) )
  4208. mode += 2;
  4209. if( get_cabac( &h->cabac, &h->cabac_state[69] ) )
  4210. mode += 4;
  4211. if( mode >= pred_mode )
  4212. return mode + 1;
  4213. else
  4214. return mode;
  4215. }
  4216. static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
  4217. const int mba_xy = h->left_mb_xy[0];
  4218. const int mbb_xy = h->top_mb_xy;
  4219. int ctx = 0;
  4220. /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
  4221. if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
  4222. ctx++;
  4223. if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
  4224. ctx++;
  4225. if( get_cabac( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
  4226. return 0;
  4227. if( get_cabac( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  4228. return 1;
  4229. if( get_cabac( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  4230. return 2;
  4231. else
  4232. return 3;
  4233. }
  4234. static const uint8_t block_idx_x[16] = {
  4235. 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
  4236. };
  4237. static const uint8_t block_idx_y[16] = {
  4238. 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
  4239. };
  4240. static const uint8_t block_idx_xy[4][4] = {
  4241. { 0, 2, 8, 10},
  4242. { 1, 3, 9, 11},
  4243. { 4, 6, 12, 14},
  4244. { 5, 7, 13, 15}
  4245. };
  4246. static int decode_cabac_mb_cbp_luma( H264Context *h) {
  4247. MpegEncContext * const s = &h->s;
  4248. int cbp = 0;
  4249. int i8x8;
  4250. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  4251. int cbp_a = -1;
  4252. int cbp_b = -1;
  4253. int x, y;
  4254. int ctx = 0;
  4255. x = block_idx_x[4*i8x8];
  4256. y = block_idx_y[4*i8x8];
  4257. if( x > 0 )
  4258. cbp_a = cbp;
  4259. else if( s->mb_x > 0 && (h->slice_table[h->left_mb_xy[0]] == h->slice_num)) {
  4260. cbp_a = h->left_cbp;
  4261. tprintf("cbp_a = left_cbp = %x\n", cbp_a);
  4262. }
  4263. if( y > 0 )
  4264. cbp_b = cbp;
  4265. else if( s->mb_y > 0 && (h->slice_table[h->top_mb_xy] == h->slice_num)) {
  4266. cbp_b = h->top_cbp;
  4267. tprintf("cbp_b = top_cbp = %x\n", cbp_b);
  4268. }
  4269. /* No need to test for skip as we put 0 for skip block */
  4270. /* No need to test for IPCM as we put 1 for IPCM block */
  4271. if( cbp_a >= 0 ) {
  4272. int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
  4273. if( ((cbp_a >> i8x8a)&0x01) == 0 )
  4274. ctx++;
  4275. }
  4276. if( cbp_b >= 0 ) {
  4277. int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
  4278. if( ((cbp_b >> i8x8b)&0x01) == 0 )
  4279. ctx += 2;
  4280. }
  4281. if( get_cabac( &h->cabac, &h->cabac_state[73 + ctx] ) ) {
  4282. cbp |= 1 << i8x8;
  4283. }
  4284. }
  4285. return cbp;
  4286. }
  4287. static int decode_cabac_mb_cbp_chroma( H264Context *h) {
  4288. int ctx;
  4289. int cbp_a, cbp_b;
  4290. cbp_a = (h->left_cbp>>4)&0x03;
  4291. cbp_b = (h-> top_cbp>>4)&0x03;
  4292. ctx = 0;
  4293. if( cbp_a > 0 ) ctx++;
  4294. if( cbp_b > 0 ) ctx += 2;
  4295. if( get_cabac( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
  4296. return 0;
  4297. ctx = 4;
  4298. if( cbp_a == 2 ) ctx++;
  4299. if( cbp_b == 2 ) ctx += 2;
  4300. return 1 + get_cabac( &h->cabac, &h->cabac_state[77 + ctx] );
  4301. }
  4302. static int decode_cabac_mb_dqp( H264Context *h) {
  4303. MpegEncContext * const s = &h->s;
  4304. int mbn_xy;
  4305. int ctx = 0;
  4306. int val = 0;
  4307. if( s->mb_x > 0 )
  4308. mbn_xy = s->mb_x + s->mb_y*s->mb_stride - 1;
  4309. else
  4310. mbn_xy = s->mb_width - 1 + (s->mb_y-1)*s->mb_stride;
  4311. if( h->last_qscale_diff != 0 && ( IS_INTRA16x16(s->current_picture.mb_type[mbn_xy] ) || (h->cbp_table[mbn_xy]&0x3f) ) )
  4312. ctx++;
  4313. while( get_cabac( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
  4314. if( ctx < 2 )
  4315. ctx = 2;
  4316. else
  4317. ctx = 3;
  4318. val++;
  4319. }
  4320. if( val&0x01 )
  4321. return (val + 1)/2;
  4322. else
  4323. return -(val + 1)/2;
  4324. }
  4325. static int decode_cabac_p_mb_sub_type( H264Context *h ) {
  4326. if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
  4327. return 0; /* 8x8 */
  4328. if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
  4329. return 1; /* 8x4 */
  4330. if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
  4331. return 2; /* 4x8 */
  4332. return 3; /* 4x4 */
  4333. }
  4334. static int decode_cabac_b_mb_sub_type( H264Context *h ) {
  4335. int type;
  4336. if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
  4337. return 0; /* B_Direct_8x8 */
  4338. if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
  4339. return 1 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */
  4340. type = 3;
  4341. if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
  4342. if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
  4343. return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
  4344. type += 4;
  4345. }
  4346. type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
  4347. type += get_cabac( &h->cabac, &h->cabac_state[39] );
  4348. return type;
  4349. }
  4350. static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
  4351. int refa = h->ref_cache[list][scan8[n] - 1];
  4352. int refb = h->ref_cache[list][scan8[n] - 8];
  4353. int ref = 0;
  4354. int ctx = 0;
  4355. if( h->slice_type == B_TYPE) {
  4356. if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
  4357. ctx++;
  4358. if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
  4359. ctx += 2;
  4360. } else {
  4361. if( refa > 0 )
  4362. ctx++;
  4363. if( refb > 0 )
  4364. ctx += 2;
  4365. }
  4366. while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
  4367. ref++;
  4368. if( ctx < 4 )
  4369. ctx = 4;
  4370. else
  4371. ctx = 5;
  4372. }
  4373. return ref;
  4374. }
  4375. static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
  4376. int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
  4377. abs( h->mvd_cache[list][scan8[n] - 8][l] );
  4378. int ctxbase = (l == 0) ? 40 : 47;
  4379. int ctx, mvd;
  4380. if( amvd < 3 )
  4381. ctx = 0;
  4382. else if( amvd > 32 )
  4383. ctx = 2;
  4384. else
  4385. ctx = 1;
  4386. if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
  4387. return 0;
  4388. mvd= 1;
  4389. ctx= 3;
  4390. while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
  4391. mvd++;
  4392. if( ctx < 6 )
  4393. ctx++;
  4394. }
  4395. if( mvd >= 9 ) {
  4396. int k = 3;
  4397. while( get_cabac_bypass( &h->cabac ) ) {
  4398. mvd += 1 << k;
  4399. k++;
  4400. }
  4401. while( k-- ) {
  4402. if( get_cabac_bypass( &h->cabac ) )
  4403. mvd += 1 << k;
  4404. }
  4405. }
  4406. if( get_cabac_bypass( &h->cabac ) ) return -mvd;
  4407. else return mvd;
  4408. }
  4409. static int inline get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) {
  4410. int nza, nzb;
  4411. int ctx = 0;
  4412. if( cat == 0 ) {
  4413. nza = h->left_cbp&0x100;
  4414. nzb = h-> top_cbp&0x100;
  4415. } else if( cat == 1 || cat == 2 ) {
  4416. nza = h->non_zero_count_cache[scan8[idx] - 1];
  4417. nzb = h->non_zero_count_cache[scan8[idx] - 8];
  4418. } else if( cat == 3 ) {
  4419. nza = (h->left_cbp>>(6+idx))&0x01;
  4420. nzb = (h-> top_cbp>>(6+idx))&0x01;
  4421. } else {
  4422. assert(cat == 4);
  4423. nza = h->non_zero_count_cache[scan8[16+idx] - 1];
  4424. nzb = h->non_zero_count_cache[scan8[16+idx] - 8];
  4425. }
  4426. if( nza > 0 )
  4427. ctx++;
  4428. if( nzb > 0 )
  4429. ctx += 2;
  4430. return ctx + 4 * cat;
  4431. }
  4432. static int inline decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, int qp, int max_coeff) {
  4433. const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
  4434. const uint16_t *qmul= dequant_coeff[qp];
  4435. static const int significant_coeff_flag_field_offset[2] = { 105, 277 };
  4436. static const int last_significant_coeff_flag_field_offset[2] = { 166, 338 };
  4437. static const int significant_coeff_flag_offset[5] = { 0, 15, 29, 44, 47 };
  4438. static const int coeff_abs_level_m1_offset[5] = {227+ 0, 227+10, 227+20, 227+30, 227+39 };
  4439. int index[16];
  4440. int i, last;
  4441. int coeff_count = 0;
  4442. int abslevel1 = 1;
  4443. int abslevelgt1 = 0;
  4444. /* cat: 0-> DC 16x16 n = 0
  4445. * 1-> AC 16x16 n = luma4x4idx
  4446. * 2-> Luma4x4 n = luma4x4idx
  4447. * 3-> DC Chroma n = iCbCr
  4448. * 4-> AC Chroma n = 4 * iCbCr + chroma4x4idx
  4449. */
  4450. /* read coded block flag */
  4451. if( get_cabac( &h->cabac, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n ) ] ) == 0 ) {
  4452. if( cat == 1 || cat == 2 )
  4453. h->non_zero_count_cache[scan8[n]] = 0;
  4454. else if( cat == 4 )
  4455. h->non_zero_count_cache[scan8[16+n]] = 0;
  4456. return 0;
  4457. }
  4458. for(last= 0; last < max_coeff - 1; last++) {
  4459. if( get_cabac( &h->cabac, &h->cabac_state[significant_coeff_flag_field_offset[h->mb_field_decoding_flag]+significant_coeff_flag_offset[cat]+last] )) {
  4460. index[coeff_count++] = last;
  4461. 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] ) ) {
  4462. last= max_coeff;
  4463. break;
  4464. }
  4465. }
  4466. }
  4467. if( last == max_coeff -1 ) {
  4468. index[coeff_count++] = last;
  4469. }
  4470. assert(coeff_count > 0);
  4471. if( cat == 0 )
  4472. h->cbp_table[mb_xy] |= 0x100;
  4473. else if( cat == 1 || cat == 2 )
  4474. h->non_zero_count_cache[scan8[n]] = coeff_count;
  4475. else if( cat == 3 )
  4476. h->cbp_table[mb_xy] |= 0x40 << n;
  4477. else {
  4478. assert( cat == 4 );
  4479. h->non_zero_count_cache[scan8[16+n]] = coeff_count;
  4480. }
  4481. for( i = coeff_count - 1; i >= 0; i-- ) {
  4482. int ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 )) + coeff_abs_level_m1_offset[cat];
  4483. int j= scantable[index[i]];
  4484. if( get_cabac( &h->cabac, &h->cabac_state[ctx] ) == 0 ) {
  4485. if( cat == 0 || cat == 3 ) {
  4486. if( get_cabac_bypass( &h->cabac ) ) block[j] = -1;
  4487. else block[j] = 1;
  4488. }else{
  4489. if( get_cabac_bypass( &h->cabac ) ) block[j] = -qmul[j];
  4490. else block[j] = qmul[j];
  4491. }
  4492. abslevel1++;
  4493. } else {
  4494. int coeff_abs = 2;
  4495. ctx = 5 + FFMIN( 4, abslevelgt1 ) + coeff_abs_level_m1_offset[cat];
  4496. while( coeff_abs < 15 && get_cabac( &h->cabac, &h->cabac_state[ctx] ) ) {
  4497. coeff_abs++;
  4498. }
  4499. if( coeff_abs >= 15 ) {
  4500. int j = 0;
  4501. while( get_cabac_bypass( &h->cabac ) ) {
  4502. coeff_abs += 1 << j;
  4503. j++;
  4504. }
  4505. while( j-- ) {
  4506. if( get_cabac_bypass( &h->cabac ) )
  4507. coeff_abs += 1 << j ;
  4508. }
  4509. }
  4510. if( cat == 0 || cat == 3 ) {
  4511. if( get_cabac_bypass( &h->cabac ) ) block[j] = -coeff_abs;
  4512. else block[j] = coeff_abs;
  4513. }else{
  4514. if( get_cabac_bypass( &h->cabac ) ) block[j] = -coeff_abs * qmul[j];
  4515. else block[j] = coeff_abs * qmul[j];
  4516. }
  4517. abslevelgt1++;
  4518. }
  4519. }
  4520. return 0;
  4521. }
  4522. void inline compute_mb_neighboors(H264Context *h)
  4523. {
  4524. MpegEncContext * const s = &h->s;
  4525. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  4526. h->top_mb_xy = mb_xy - s->mb_stride;
  4527. h->left_mb_xy[0] = mb_xy - 1;
  4528. if(h->mb_aff_frame){
  4529. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  4530. const int top_pair_xy = pair_xy - s->mb_stride;
  4531. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  4532. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  4533. const int curr_mb_frame_flag = !h->mb_field_decoding_flag;
  4534. const int bottom = (s->mb_y & 1);
  4535. if (bottom
  4536. ? !curr_mb_frame_flag // bottom macroblock
  4537. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  4538. ) {
  4539. h->top_mb_xy -= s->mb_stride;
  4540. }
  4541. if (left_mb_frame_flag != curr_mb_frame_flag) {
  4542. h->left_mb_xy[0] = pair_xy - 1;
  4543. }
  4544. }
  4545. return;
  4546. }
  4547. /**
  4548. * decodes a macroblock
  4549. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  4550. */
  4551. static int decode_mb_cabac(H264Context *h) {
  4552. MpegEncContext * const s = &h->s;
  4553. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4554. int mb_type, partition_count, cbp = 0;
  4555. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?)
  4556. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  4557. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) {
  4558. /* read skip flags */
  4559. if( decode_cabac_mb_skip( h ) ) {
  4560. decode_mb_skip(h);
  4561. h->cbp_table[mb_xy] = 0;
  4562. h->chroma_pred_mode_table[mb_xy] = 0;
  4563. h->last_qscale_diff = 0;
  4564. return 0;
  4565. }
  4566. }
  4567. if(h->mb_aff_frame){
  4568. if ( ((s->mb_y&1) == 0) || h->prev_mb_skipped)
  4569. h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  4570. }else
  4571. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  4572. h->prev_mb_skipped = 0;
  4573. compute_mb_neighboors(h);
  4574. if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
  4575. av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
  4576. return -1;
  4577. }
  4578. if( h->slice_type == B_TYPE ) {
  4579. if( mb_type < 23 ){
  4580. partition_count= b_mb_type_info[mb_type].partition_count;
  4581. mb_type= b_mb_type_info[mb_type].type;
  4582. }else{
  4583. mb_type -= 23;
  4584. goto decode_intra_mb;
  4585. }
  4586. } else if( h->slice_type == P_TYPE ) {
  4587. if( mb_type < 5) {
  4588. partition_count= p_mb_type_info[mb_type].partition_count;
  4589. mb_type= p_mb_type_info[mb_type].type;
  4590. } else {
  4591. mb_type -= 5;
  4592. goto decode_intra_mb;
  4593. }
  4594. } else {
  4595. assert(h->slice_type == I_TYPE);
  4596. decode_intra_mb:
  4597. partition_count = 0;
  4598. cbp= i_mb_type_info[mb_type].cbp;
  4599. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  4600. mb_type= i_mb_type_info[mb_type].type;
  4601. }
  4602. if(h->mb_field_decoding_flag)
  4603. mb_type |= MB_TYPE_INTERLACED;
  4604. s->current_picture.mb_type[mb_xy]= mb_type;
  4605. h->slice_table[ mb_xy ]= h->slice_num;
  4606. if(IS_INTRA_PCM(mb_type)) {
  4607. const uint8_t *ptr;
  4608. unsigned int x, y;
  4609. // We assume these blocks are very rare so we dont optimize it.
  4610. // FIXME The two following lines get the bitstream position in the cabac
  4611. // decode, I think it should be done by a function in cabac.h (or cabac.c).
  4612. ptr= h->cabac.bytestream;
  4613. if (h->cabac.low&0x1) ptr-=CABAC_BITS/8;
  4614. // The pixels are stored in the same order as levels in h->mb array.
  4615. for(y=0; y<16; y++){
  4616. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  4617. for(x=0; x<16; x++){
  4618. tprintf("LUMA ICPM LEVEL (%3d)\n", *ptr);
  4619. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= *ptr++;
  4620. }
  4621. }
  4622. for(y=0; y<8; y++){
  4623. const int index= 256 + 4*(y&3) + 32*(y>>2);
  4624. for(x=0; x<8; x++){
  4625. tprintf("CHROMA U ICPM LEVEL (%3d)\n", *ptr);
  4626. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  4627. }
  4628. }
  4629. for(y=0; y<8; y++){
  4630. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  4631. for(x=0; x<8; x++){
  4632. tprintf("CHROMA V ICPM LEVEL (%3d)\n", *ptr);
  4633. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  4634. }
  4635. }
  4636. ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);
  4637. // All blocks are present
  4638. h->cbp_table[mb_xy] = 0x1ef;
  4639. h->chroma_pred_mode_table[mb_xy] = 0;
  4640. // In deblocking, the quantizer is 0
  4641. s->current_picture.qscale_table[mb_xy]= 0;
  4642. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  4643. // All coeffs are present
  4644. memset(h->non_zero_count[mb_xy], 16, 16);
  4645. return 0;
  4646. }
  4647. fill_caches(h, mb_type, 0);
  4648. if( IS_INTRA( mb_type ) ) {
  4649. if( IS_INTRA4x4( mb_type ) ) {
  4650. int i;
  4651. for( i = 0; i < 16; i++ ) {
  4652. int pred = pred_intra_mode( h, i );
  4653. h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  4654. //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
  4655. }
  4656. write_back_intra_pred_mode(h);
  4657. if( check_intra4x4_pred_mode(h) < 0 ) return -1;
  4658. } else {
  4659. h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );
  4660. if( h->intra16x16_pred_mode < 0 ) return -1;
  4661. }
  4662. h->chroma_pred_mode_table[mb_xy] =
  4663. h->chroma_pred_mode = decode_cabac_mb_chroma_pre_mode( h );
  4664. h->chroma_pred_mode= check_intra_pred_mode( h, h->chroma_pred_mode );
  4665. if( h->chroma_pred_mode < 0 ) return -1;
  4666. } else if( partition_count == 4 ) {
  4667. int i, j, sub_partition_count[4], list, ref[2][4];
  4668. if( h->slice_type == B_TYPE ) {
  4669. for( i = 0; i < 4; i++ ) {
  4670. h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
  4671. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4672. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4673. }
  4674. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  4675. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
  4676. pred_direct_motion(h, &mb_type);
  4677. if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
  4678. for( i = 0; i < 4; i++ )
  4679. if( IS_DIRECT(h->sub_mb_type[i]) )
  4680. fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );
  4681. }
  4682. }
  4683. } else {
  4684. for( i = 0; i < 4; i++ ) {
  4685. h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
  4686. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4687. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4688. }
  4689. }
  4690. for( list = 0; list < 2; list++ ) {
  4691. if( h->ref_count[list] > 0 ) {
  4692. for( i = 0; i < 4; i++ ) {
  4693. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  4694. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4695. if( h->ref_count[list] > 1 )
  4696. ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );
  4697. else
  4698. ref[list][i] = 0;
  4699. } else {
  4700. ref[list][i] = -1;
  4701. }
  4702. h->ref_cache[list][ scan8[4*i]+1 ]=
  4703. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  4704. }
  4705. }
  4706. }
  4707. for(list=0; list<2; list++){
  4708. for(i=0; i<4; i++){
  4709. if(IS_DIRECT(h->sub_mb_type[i])){
  4710. fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);
  4711. continue;
  4712. }
  4713. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];
  4714. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  4715. const int sub_mb_type= h->sub_mb_type[i];
  4716. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  4717. for(j=0; j<sub_partition_count[i]; j++){
  4718. int mpx, mpy;
  4719. int mx, my;
  4720. const int index= 4*i + block_width*j;
  4721. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  4722. int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];
  4723. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);
  4724. mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );
  4725. my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );
  4726. tprintf("final mv:%d %d\n", mx, my);
  4727. if(IS_SUB_8X8(sub_mb_type)){
  4728. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  4729. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  4730. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  4731. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  4732. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]=
  4733. mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;
  4734. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]=
  4735. mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;
  4736. }else if(IS_SUB_8X4(sub_mb_type)){
  4737. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  4738. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  4739. mvd_cache[ 0 ][0]= mvd_cache[ 1 ][0]= mx- mpx;
  4740. mvd_cache[ 0 ][1]= mvd_cache[ 1 ][1]= my - mpy;
  4741. }else if(IS_SUB_4X8(sub_mb_type)){
  4742. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  4743. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  4744. mvd_cache[ 0 ][0]= mvd_cache[ 8 ][0]= mx - mpx;
  4745. mvd_cache[ 0 ][1]= mvd_cache[ 8 ][1]= my - mpy;
  4746. }else{
  4747. assert(IS_SUB_4X4(sub_mb_type));
  4748. mv_cache[ 0 ][0]= mx;
  4749. mv_cache[ 0 ][1]= my;
  4750. mvd_cache[ 0 ][0]= mx - mpx;
  4751. mvd_cache[ 0 ][1]= my - mpy;
  4752. }
  4753. }
  4754. }else{
  4755. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  4756. uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];
  4757. p[0] = p[1] = p[8] = p[9] = 0;
  4758. pd[0]= pd[1]= pd[8]= pd[9]= 0;
  4759. }
  4760. }
  4761. }
  4762. } else if( IS_DIRECT(mb_type) ) {
  4763. pred_direct_motion(h, &mb_type);
  4764. s->current_picture.mb_type[mb_xy]= mb_type;
  4765. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  4766. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  4767. } else {
  4768. int list, mx, my, i, mpx, mpy;
  4769. if(IS_16X16(mb_type)){
  4770. for(list=0; list<2; list++){
  4771. if(IS_DIR(mb_type, 0, list)){
  4772. if(h->ref_count[list] > 0 ){
  4773. const int ref = h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 0 ) : 0;
  4774. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
  4775. }
  4776. }else
  4777. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  4778. }
  4779. for(list=0; list<2; list++){
  4780. if(IS_DIR(mb_type, 0, list)){
  4781. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);
  4782. mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );
  4783. my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );
  4784. tprintf("final mv:%d %d\n", mx, my);
  4785. fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  4786. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  4787. }else
  4788. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  4789. }
  4790. }
  4791. else if(IS_16X8(mb_type)){
  4792. for(list=0; list<2; list++){
  4793. if(h->ref_count[list]>0){
  4794. for(i=0; i<2; i++){
  4795. if(IS_DIR(mb_type, i, list)){
  4796. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 8*i ) : 0;
  4797. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
  4798. }else
  4799. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  4800. }
  4801. }
  4802. }
  4803. for(list=0; list<2; list++){
  4804. for(i=0; i<2; i++){
  4805. if(IS_DIR(mb_type, i, list)){
  4806. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);
  4807. mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );
  4808. my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );
  4809. tprintf("final mv:%d %d\n", mx, my);
  4810. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);
  4811. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  4812. }else{
  4813. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  4814. fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  4815. }
  4816. }
  4817. }
  4818. }else{
  4819. assert(IS_8X16(mb_type));
  4820. for(list=0; list<2; list++){
  4821. if(h->ref_count[list]>0){
  4822. for(i=0; i<2; i++){
  4823. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  4824. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 4*i ) : 0;
  4825. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
  4826. }else
  4827. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4828. }
  4829. }
  4830. }
  4831. for(list=0; list<2; list++){
  4832. for(i=0; i<2; i++){
  4833. if(IS_DIR(mb_type, i, list)){
  4834. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);
  4835. mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );
  4836. my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );
  4837. tprintf("final mv:%d %d\n", mx, my);
  4838. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  4839. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  4840. }else{
  4841. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  4842. fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  4843. }
  4844. }
  4845. }
  4846. }
  4847. }
  4848. if( IS_INTER( mb_type ) ) {
  4849. h->chroma_pred_mode_table[mb_xy] = 0;
  4850. write_back_motion( h, mb_type );
  4851. }
  4852. if( !IS_INTRA16x16( mb_type ) ) {
  4853. cbp = decode_cabac_mb_cbp_luma( h );
  4854. cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
  4855. }
  4856. h->cbp_table[mb_xy] = cbp;
  4857. if( cbp || IS_INTRA16x16( mb_type ) ) {
  4858. const uint8_t *scan, *dc_scan;
  4859. int dqp;
  4860. if(IS_INTERLACED(mb_type)){
  4861. scan= field_scan;
  4862. dc_scan= luma_dc_field_scan;
  4863. }else{
  4864. scan= zigzag_scan;
  4865. dc_scan= luma_dc_zigzag_scan;
  4866. }
  4867. h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
  4868. s->qscale += dqp;
  4869. if(((unsigned)s->qscale) > 51){
  4870. if(s->qscale<0) s->qscale+= 52;
  4871. else s->qscale-= 52;
  4872. }
  4873. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4874. if( IS_INTRA16x16( mb_type ) ) {
  4875. int i;
  4876. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
  4877. if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, s->qscale, 16) < 0)
  4878. return -1;
  4879. if( cbp&15 ) {
  4880. for( i = 0; i < 16; i++ ) {
  4881. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
  4882. if( decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, s->qscale, 15) < 0 )
  4883. return -1;
  4884. }
  4885. } else {
  4886. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  4887. }
  4888. } else {
  4889. int i8x8, i4x4;
  4890. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  4891. if( cbp & (1<<i8x8) ) {
  4892. for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
  4893. const int index = 4*i8x8 + i4x4;
  4894. //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
  4895. if( decode_cabac_residual(h, h->mb + 16*index, 2, index, scan, s->qscale, 16) < 0 )
  4896. return -1;
  4897. }
  4898. } else {
  4899. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4900. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  4901. }
  4902. }
  4903. }
  4904. if( cbp&0x30 ){
  4905. int c;
  4906. for( c = 0; c < 2; c++ ) {
  4907. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
  4908. if( decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, h->chroma_qp, 4) < 0)
  4909. return -1;
  4910. }
  4911. }
  4912. if( cbp&0x20 ) {
  4913. int c, i;
  4914. for( c = 0; c < 2; c++ ) {
  4915. for( i = 0; i < 4; i++ ) {
  4916. const int index = 16 + 4 * c + i;
  4917. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
  4918. if( decode_cabac_residual(h, h->mb + 16*index, 4, index - 16, scan + 1, h->chroma_qp, 15) < 0)
  4919. return -1;
  4920. }
  4921. }
  4922. } else {
  4923. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4924. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4925. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4926. }
  4927. } else {
  4928. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4929. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  4930. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4931. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4932. }
  4933. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4934. write_back_non_zero_count(h);
  4935. return 0;
  4936. }
  4937. static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  4938. int i, d;
  4939. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  4940. const int alpha = alpha_table[index_a];
  4941. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  4942. if( bS[0] < 4 ) {
  4943. int tc[4];
  4944. for(i=0; i<4; i++)
  4945. tc[i] = bS[i] ? tc0_table[index_a][bS[i] - 1] : -1;
  4946. h->s.dsp.h264_h_loop_filter_luma(pix, stride, alpha, beta, tc);
  4947. } else {
  4948. /* 16px edge length, because bS=4 is triggered by being at
  4949. * the edge of an intra MB, so all 4 bS are the same */
  4950. for( d = 0; d < 16; d++ ) {
  4951. const int p0 = pix[-1];
  4952. const int p1 = pix[-2];
  4953. const int p2 = pix[-3];
  4954. const int q0 = pix[0];
  4955. const int q1 = pix[1];
  4956. const int q2 = pix[2];
  4957. if( ABS( p0 - q0 ) < alpha &&
  4958. ABS( p1 - p0 ) < beta &&
  4959. ABS( q1 - q0 ) < beta ) {
  4960. if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  4961. if( ABS( p2 - p0 ) < beta)
  4962. {
  4963. const int p3 = pix[-4];
  4964. /* p0', p1', p2' */
  4965. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  4966. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  4967. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  4968. } else {
  4969. /* p0' */
  4970. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  4971. }
  4972. if( ABS( q2 - q0 ) < beta)
  4973. {
  4974. const int q3 = pix[3];
  4975. /* q0', q1', q2' */
  4976. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  4977. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  4978. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  4979. } else {
  4980. /* q0' */
  4981. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  4982. }
  4983. }else{
  4984. /* p0', q0' */
  4985. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  4986. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  4987. }
  4988. 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]);
  4989. }
  4990. pix += stride;
  4991. }
  4992. }
  4993. }
  4994. static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  4995. int i, d;
  4996. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  4997. const int alpha = alpha_table[index_a];
  4998. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  4999. if( bS[0] < 4 ) {
  5000. int tc[4];
  5001. for(i=0; i<4; i++)
  5002. tc[i] = bS[i] ? tc0_table[index_a][bS[i] - 1] + 1 : 0;
  5003. h->s.dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
  5004. } else {
  5005. /* 8px edge length, see filter_mb_edgev */
  5006. for( d = 0; d < 8; d++ ){
  5007. const int p0 = pix[-1];
  5008. const int p1 = pix[-2];
  5009. const int q0 = pix[0];
  5010. const int q1 = pix[1];
  5011. if( ABS( p0 - q0 ) < alpha &&
  5012. ABS( p1 - p0 ) < beta &&
  5013. ABS( q1 - q0 ) < beta ) {
  5014. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  5015. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  5016. tprintf("filter_mb_edgecv i:%d d:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);
  5017. }
  5018. pix += stride;
  5019. }
  5020. }
  5021. }
  5022. static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int bS[8], int qp[2] ) {
  5023. int i;
  5024. for( i = 0; i < 16; i++, pix += stride) {
  5025. int index_a;
  5026. int alpha;
  5027. int beta;
  5028. int qp_index;
  5029. int bS_index = (i >> 1);
  5030. if (h->mb_field_decoding_flag) {
  5031. bS_index &= ~1;
  5032. bS_index |= (i & 1);
  5033. }
  5034. if( bS[bS_index] == 0 ) {
  5035. continue;
  5036. }
  5037. qp_index = h->mb_field_decoding_flag ? (i & 1) : (i >> 3);
  5038. index_a = clip( qp[qp_index] + h->slice_alpha_c0_offset, 0, 51 );
  5039. alpha = alpha_table[index_a];
  5040. beta = beta_table[clip( qp[qp_index] + h->slice_beta_offset, 0, 51 )];
  5041. if( bS[bS_index] < 4 ) {
  5042. const int tc0 = tc0_table[index_a][bS[bS_index] - 1];
  5043. /* 4px edge length */
  5044. const int p0 = pix[-1];
  5045. const int p1 = pix[-2];
  5046. const int p2 = pix[-3];
  5047. const int q0 = pix[0];
  5048. const int q1 = pix[1];
  5049. const int q2 = pix[2];
  5050. if( ABS( p0 - q0 ) < alpha &&
  5051. ABS( p1 - p0 ) < beta &&
  5052. ABS( q1 - q0 ) < beta ) {
  5053. int tc = tc0;
  5054. int i_delta;
  5055. if( ABS( p2 - p0 ) < beta ) {
  5056. pix[-2] = p1 + clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  5057. tc++;
  5058. }
  5059. if( ABS( q2 - q0 ) < beta ) {
  5060. pix[1] = q1 + clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  5061. tc++;
  5062. }
  5063. i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  5064. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  5065. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  5066. 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);
  5067. }
  5068. }else{
  5069. /* 4px edge length */
  5070. const int p0 = pix[-1];
  5071. const int p1 = pix[-2];
  5072. const int p2 = pix[-3];
  5073. const int q0 = pix[0];
  5074. const int q1 = pix[1];
  5075. const int q2 = pix[2];
  5076. if( ABS( p0 - q0 ) < alpha &&
  5077. ABS( p1 - p0 ) < beta &&
  5078. ABS( q1 - q0 ) < beta ) {
  5079. if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5080. if( ABS( p2 - p0 ) < beta)
  5081. {
  5082. const int p3 = pix[-4];
  5083. /* p0', p1', p2' */
  5084. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5085. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5086. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5087. } else {
  5088. /* p0' */
  5089. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5090. }
  5091. if( ABS( q2 - q0 ) < beta)
  5092. {
  5093. const int q3 = pix[3];
  5094. /* q0', q1', q2' */
  5095. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5096. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5097. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5098. } else {
  5099. /* q0' */
  5100. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5101. }
  5102. }else{
  5103. /* p0', q0' */
  5104. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5105. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5106. }
  5107. 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]);
  5108. }
  5109. }
  5110. }
  5111. }
  5112. static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp[2] ) {
  5113. int i;
  5114. for( i = 0; i < 8; i++, pix += stride) {
  5115. int index_a;
  5116. int alpha;
  5117. int beta;
  5118. int qp_index;
  5119. int bS_index = i;
  5120. if( bS[bS_index] == 0 ) {
  5121. continue;
  5122. }
  5123. qp_index = h->mb_field_decoding_flag ? (i & 1) : (i >> 3);
  5124. index_a = clip( qp[qp_index] + h->slice_alpha_c0_offset, 0, 51 );
  5125. alpha = alpha_table[index_a];
  5126. beta = beta_table[clip( qp[qp_index] + h->slice_beta_offset, 0, 51 )];
  5127. if( bS[bS_index] < 4 ) {
  5128. const int tc = tc0_table[index_a][bS[bS_index] - 1] + 1;
  5129. /* 2px edge length (because we use same bS than the one for luma) */
  5130. const int p0 = pix[-1];
  5131. const int p1 = pix[-2];
  5132. const int q0 = pix[0];
  5133. const int q1 = pix[1];
  5134. if( ABS( p0 - q0 ) < alpha &&
  5135. ABS( p1 - p0 ) < beta &&
  5136. ABS( q1 - q0 ) < beta ) {
  5137. const int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  5138. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  5139. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  5140. 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);
  5141. }
  5142. }else{
  5143. const int p0 = pix[-1];
  5144. const int p1 = pix[-2];
  5145. const int q0 = pix[0];
  5146. const int q1 = pix[1];
  5147. if( ABS( p0 - q0 ) < alpha &&
  5148. ABS( p1 - p0 ) < beta &&
  5149. ABS( q1 - q0 ) < beta ) {
  5150. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  5151. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  5152. 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]);
  5153. }
  5154. }
  5155. }
  5156. }
  5157. static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  5158. int i, d;
  5159. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  5160. const int alpha = alpha_table[index_a];
  5161. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  5162. const int pix_next = stride;
  5163. if( bS[0] < 4 ) {
  5164. int tc[4];
  5165. for(i=0; i<4; i++)
  5166. tc[i] = bS[i] ? tc0_table[index_a][bS[i] - 1] : -1;
  5167. h->s.dsp.h264_v_loop_filter_luma(pix, stride, alpha, beta, tc);
  5168. } else {
  5169. /* 16px edge length, see filter_mb_edgev */
  5170. for( d = 0; d < 16; d++ ) {
  5171. const int p0 = pix[-1*pix_next];
  5172. const int p1 = pix[-2*pix_next];
  5173. const int p2 = pix[-3*pix_next];
  5174. const int q0 = pix[0];
  5175. const int q1 = pix[1*pix_next];
  5176. const int q2 = pix[2*pix_next];
  5177. if( ABS( p0 - q0 ) < alpha &&
  5178. ABS( p1 - p0 ) < beta &&
  5179. ABS( q1 - q0 ) < beta ) {
  5180. const int p3 = pix[-4*pix_next];
  5181. const int q3 = pix[ 3*pix_next];
  5182. if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5183. if( ABS( p2 - p0 ) < beta) {
  5184. /* p0', p1', p2' */
  5185. pix[-1*pix_next] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5186. pix[-2*pix_next] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5187. pix[-3*pix_next] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5188. } else {
  5189. /* p0' */
  5190. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5191. }
  5192. if( ABS( q2 - q0 ) < beta) {
  5193. /* q0', q1', q2' */
  5194. pix[0*pix_next] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5195. pix[1*pix_next] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5196. pix[2*pix_next] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5197. } else {
  5198. /* q0' */
  5199. pix[0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5200. }
  5201. }else{
  5202. /* p0', q0' */
  5203. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5204. pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5205. }
  5206. 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]);
  5207. }
  5208. pix++;
  5209. }
  5210. }
  5211. }
  5212. static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  5213. int i, d;
  5214. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  5215. const int alpha = alpha_table[index_a];
  5216. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  5217. const int pix_next = stride;
  5218. if( bS[0] < 4 ) {
  5219. int 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. /* 8px edge length, see filter_mb_edgev */
  5225. for( d = 0; d < 8; d++ ) {
  5226. const int p0 = pix[-1*pix_next];
  5227. const int p1 = pix[-2*pix_next];
  5228. const int q0 = pix[0];
  5229. const int q1 = pix[1*pix_next];
  5230. if( ABS( p0 - q0 ) < alpha &&
  5231. ABS( p1 - p0 ) < beta &&
  5232. ABS( q1 - q0 ) < beta ) {
  5233. pix[-pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  5234. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  5235. tprintf("filter_mb_edgech 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], pix[-3*pix_next], p1, p0, q0, q1, pix[2*pix_next], pix[-2*pix_next], pix[-pix_next], pix[0], pix[pix_next]);
  5236. }
  5237. pix++;
  5238. }
  5239. }
  5240. }
  5241. 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) {
  5242. MpegEncContext * const s = &h->s;
  5243. const int mb_xy= mb_x + mb_y*s->mb_stride;
  5244. int first_vertical_edge_done = 0;
  5245. int dir;
  5246. /* FIXME: A given frame may occupy more than one position in
  5247. * the reference list. So ref2frm should be populated with
  5248. * frame numbers, not indices. */
  5249. static const int ref2frm[18] = {-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  5250. if (h->mb_aff_frame
  5251. // left mb is in picture
  5252. && h->slice_table[mb_xy-1] != 255
  5253. // and current and left pair do not have the same interlaced type
  5254. && (IS_INTERLACED(s->current_picture.mb_type[mb_xy]) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
  5255. // and left mb is in the same slice if deblocking_filter == 2
  5256. && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
  5257. /* First vertical edge is different in MBAFF frames
  5258. * There are 8 different bS to compute and 2 different Qp
  5259. */
  5260. int bS[8];
  5261. int qp[2];
  5262. int chroma_qp[2];
  5263. int i;
  5264. first_vertical_edge_done = 1;
  5265. for( i = 0; i < 8; i++ ) {
  5266. int y = i>>1;
  5267. int b_idx= 8 + 4 + 8*y;
  5268. int bn_idx= b_idx - 1;
  5269. int mbn_xy = h->mb_field_decoding_flag ? h->left_mb_xy[i>>2] : h->left_mb_xy[i&1];
  5270. if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) ||
  5271. IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) {
  5272. bS[i] = 4;
  5273. } else if( h->non_zero_count_cache[b_idx] != 0 ||
  5274. h->non_zero_count_cache[bn_idx] != 0 ) {
  5275. bS[i] = 2;
  5276. } else {
  5277. int l;
  5278. bS[i] = 0;
  5279. for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
  5280. if( ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  5281. ABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  5282. ABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= 4 ) {
  5283. bS[i] = 1;
  5284. break;
  5285. }
  5286. }
  5287. }
  5288. }
  5289. if(bS[0]+bS[1]+bS[2]+bS[3] != 0) {
  5290. // Do not use s->qscale as luma quantizer because it has not the same
  5291. // value in IPCM macroblocks.
  5292. qp[0] = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[h->left_mb_xy[0]] + 1 ) >> 1;
  5293. chroma_qp[0] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy] ) +
  5294. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[h->left_mb_xy[0]] ) + 1 ) >> 1;
  5295. qp[1] = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[h->left_mb_xy[1]] + 1 ) >> 1;
  5296. chroma_qp[1] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy] ) +
  5297. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[h->left_mb_xy[1]] ) + 1 ) >> 1;
  5298. /* Filter edge */
  5299. 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);
  5300. { int i; for (i = 0; i < 8; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  5301. filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp );
  5302. filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, chroma_qp );
  5303. filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, chroma_qp );
  5304. }
  5305. }
  5306. /* dir : 0 -> vertical edge, 1 -> horizontal edge */
  5307. for( dir = 0; dir < 2; dir++ )
  5308. {
  5309. int edge;
  5310. const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
  5311. int start = h->slice_table[mbm_xy] == 255 ? 1 : 0;
  5312. if (first_vertical_edge_done) {
  5313. start = 1;
  5314. first_vertical_edge_done = 0;
  5315. }
  5316. if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
  5317. start = 1;
  5318. /* Calculate bS */
  5319. for( edge = start; edge < 4; edge++ ) {
  5320. /* mbn_xy: neighbor macroblock */
  5321. int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
  5322. int bS[4];
  5323. int qp;
  5324. if (h->mb_aff_frame && (dir == 1) && (edge == 0) && ((mb_y & 1) == 0)
  5325. && !IS_INTERLACED(s->current_picture.mb_type[mb_xy])
  5326. && IS_INTERLACED(s->current_picture.mb_type[mbn_xy])
  5327. ) {
  5328. // This is a special case in the norm where the filtering must
  5329. // be done twice (one each of the field) even if we are in a
  5330. // frame macroblock.
  5331. //
  5332. unsigned int tmp_linesize = 2 * linesize;
  5333. unsigned int tmp_uvlinesize = 2 * uvlinesize;
  5334. int mbn_xy = mb_xy - 2 * s->mb_stride;
  5335. int qp, chroma_qp;
  5336. // first filtering
  5337. if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) ||
  5338. IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) {
  5339. bS[0] = bS[1] = bS[2] = bS[3] = 3;
  5340. } else {
  5341. // TODO
  5342. assert(0);
  5343. }
  5344. /* Filter edge */
  5345. // Do not use s->qscale as luma quantizer because it has not the same
  5346. // value in IPCM macroblocks.
  5347. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  5348. 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);
  5349. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  5350. filter_mb_edgeh( h, &img_y[0], tmp_linesize, bS, qp );
  5351. chroma_qp = ( h->chroma_qp +
  5352. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  5353. filter_mb_edgech( h, &img_cb[0], tmp_uvlinesize, bS, chroma_qp );
  5354. filter_mb_edgech( h, &img_cr[0], tmp_uvlinesize, bS, chroma_qp );
  5355. // second filtering
  5356. mbn_xy += s->mb_stride;
  5357. if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) ||
  5358. IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) {
  5359. bS[0] = bS[1] = bS[2] = bS[3] = 3;
  5360. } else {
  5361. // TODO
  5362. assert(0);
  5363. }
  5364. /* Filter edge */
  5365. // Do not use s->qscale as luma quantizer because it has not the same
  5366. // value in IPCM macroblocks.
  5367. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  5368. 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);
  5369. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  5370. filter_mb_edgeh( h, &img_y[linesize], tmp_linesize, bS, qp );
  5371. chroma_qp = ( h->chroma_qp +
  5372. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  5373. filter_mb_edgech( h, &img_cb[uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  5374. filter_mb_edgech( h, &img_cr[uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  5375. continue;
  5376. }
  5377. if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) ||
  5378. IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) {
  5379. int value;
  5380. if (edge == 0) {
  5381. if ( (!IS_INTERLACED(s->current_picture.mb_type[mb_xy]) && !IS_INTERLACED(s->current_picture.mb_type[mbm_xy]))
  5382. || ((h->mb_aff_frame || (s->picture_structure != PICT_FRAME)) && (dir == 0))
  5383. ) {
  5384. value = 4;
  5385. } else {
  5386. value = 3;
  5387. }
  5388. } else {
  5389. value = 3;
  5390. }
  5391. bS[0] = bS[1] = bS[2] = bS[3] = value;
  5392. } else {
  5393. int i;
  5394. for( i = 0; i < 4; i++ ) {
  5395. int x = dir == 0 ? edge : i;
  5396. int y = dir == 0 ? i : edge;
  5397. int b_idx= 8 + 4 + x + 8*y;
  5398. int bn_idx= b_idx - (dir ? 8:1);
  5399. if( h->non_zero_count_cache[b_idx] != 0 ||
  5400. h->non_zero_count_cache[bn_idx] != 0 ) {
  5401. bS[i] = 2;
  5402. }
  5403. else
  5404. {
  5405. int l;
  5406. bS[i] = 0;
  5407. for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
  5408. if( ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  5409. ABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  5410. ABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= 4 ) {
  5411. bS[i] = 1;
  5412. break;
  5413. }
  5414. }
  5415. }
  5416. }
  5417. if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
  5418. continue;
  5419. }
  5420. /* Filter edge */
  5421. // Do not use s->qscale as luma quantizer because it has not the same
  5422. // value in IPCM macroblocks.
  5423. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  5424. //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]);
  5425. tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
  5426. { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
  5427. if( dir == 0 ) {
  5428. filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
  5429. if( (edge&1) == 0 ) {
  5430. int chroma_qp = ( h->chroma_qp +
  5431. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  5432. filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp );
  5433. filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp );
  5434. }
  5435. } else {
  5436. filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
  5437. if( (edge&1) == 0 ) {
  5438. int chroma_qp = ( h->chroma_qp +
  5439. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  5440. filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  5441. filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  5442. }
  5443. }
  5444. }
  5445. }
  5446. }
  5447. static int decode_slice(H264Context *h){
  5448. MpegEncContext * const s = &h->s;
  5449. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  5450. s->mb_skip_run= -1;
  5451. if( h->pps.cabac ) {
  5452. int i;
  5453. /* realign */
  5454. align_get_bits( &s->gb );
  5455. /* init cabac */
  5456. ff_init_cabac_states( &h->cabac, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64 );
  5457. ff_init_cabac_decoder( &h->cabac,
  5458. s->gb.buffer + get_bits_count(&s->gb)/8,
  5459. ( s->gb.size_in_bits - get_bits_count(&s->gb) + 7)/8);
  5460. /* calculate pre-state */
  5461. for( i= 0; i < 399; i++ ) {
  5462. int pre;
  5463. if( h->slice_type == I_TYPE )
  5464. pre = clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
  5465. else
  5466. 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 );
  5467. if( pre <= 63 )
  5468. h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
  5469. else
  5470. h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
  5471. }
  5472. for(;;){
  5473. int ret = decode_mb_cabac(h);
  5474. int eos;
  5475. if(ret>=0) hl_decode_mb(h);
  5476. /* XXX: useless as decode_mb_cabac it doesn't support that ... */
  5477. if( ret >= 0 && h->mb_aff_frame ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  5478. s->mb_y++;
  5479. if(ret>=0) ret = decode_mb_cabac(h);
  5480. hl_decode_mb(h);
  5481. s->mb_y--;
  5482. }
  5483. eos = get_cabac_terminate( &h->cabac );
  5484. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 1) {
  5485. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  5486. 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);
  5487. return -1;
  5488. }
  5489. if( ++s->mb_x >= s->mb_width ) {
  5490. s->mb_x = 0;
  5491. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  5492. ++s->mb_y;
  5493. if(h->mb_aff_frame) {
  5494. ++s->mb_y;
  5495. }
  5496. }
  5497. if( eos || s->mb_y >= s->mb_height ) {
  5498. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  5499. 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);
  5500. return 0;
  5501. }
  5502. }
  5503. } else {
  5504. for(;;){
  5505. int ret = decode_mb_cavlc(h);
  5506. if(ret>=0) hl_decode_mb(h);
  5507. if(ret>=0 && h->mb_aff_frame){ //FIXME optimal? or let mb_decode decode 16x32 ?
  5508. s->mb_y++;
  5509. ret = decode_mb_cavlc(h);
  5510. if(ret>=0) hl_decode_mb(h);
  5511. s->mb_y--;
  5512. }
  5513. if(ret<0){
  5514. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  5515. 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);
  5516. return -1;
  5517. }
  5518. if(++s->mb_x >= s->mb_width){
  5519. s->mb_x=0;
  5520. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  5521. ++s->mb_y;
  5522. if(h->mb_aff_frame) {
  5523. ++s->mb_y;
  5524. }
  5525. if(s->mb_y >= s->mb_height){
  5526. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  5527. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  5528. 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);
  5529. return 0;
  5530. }else{
  5531. 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);
  5532. return -1;
  5533. }
  5534. }
  5535. }
  5536. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  5537. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  5538. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  5539. 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);
  5540. return 0;
  5541. }else{
  5542. 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);
  5543. return -1;
  5544. }
  5545. }
  5546. }
  5547. }
  5548. #if 0
  5549. for(;s->mb_y < s->mb_height; s->mb_y++){
  5550. for(;s->mb_x < s->mb_width; s->mb_x++){
  5551. int ret= decode_mb(h);
  5552. hl_decode_mb(h);
  5553. if(ret<0){
  5554. fprintf(stderr, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  5555. 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);
  5556. return -1;
  5557. }
  5558. if(++s->mb_x >= s->mb_width){
  5559. s->mb_x=0;
  5560. if(++s->mb_y >= s->mb_height){
  5561. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  5562. 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);
  5563. return 0;
  5564. }else{
  5565. 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);
  5566. return -1;
  5567. }
  5568. }
  5569. }
  5570. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  5571. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  5572. 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);
  5573. return 0;
  5574. }else{
  5575. 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);
  5576. return -1;
  5577. }
  5578. }
  5579. }
  5580. s->mb_x=0;
  5581. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  5582. }
  5583. #endif
  5584. return -1; //not reached
  5585. }
  5586. static inline void decode_hrd_parameters(H264Context *h, SPS *sps){
  5587. MpegEncContext * const s = &h->s;
  5588. int cpb_count, i;
  5589. cpb_count = get_ue_golomb(&s->gb) + 1;
  5590. get_bits(&s->gb, 4); /* bit_rate_scale */
  5591. get_bits(&s->gb, 4); /* cpb_size_scale */
  5592. for(i=0; i<cpb_count; i++){
  5593. get_ue_golomb(&s->gb); /* bit_rate_value_minus1 */
  5594. get_ue_golomb(&s->gb); /* cpb_size_value_minus1 */
  5595. get_bits1(&s->gb); /* cbr_flag */
  5596. }
  5597. get_bits(&s->gb, 5); /* initial_cpb_removal_delay_length_minus1 */
  5598. get_bits(&s->gb, 5); /* cpb_removal_delay_length_minus1 */
  5599. get_bits(&s->gb, 5); /* dpb_output_delay_length_minus1 */
  5600. get_bits(&s->gb, 5); /* time_offset_length */
  5601. }
  5602. static inline int decode_vui_parameters(H264Context *h, SPS *sps){
  5603. MpegEncContext * const s = &h->s;
  5604. int aspect_ratio_info_present_flag, aspect_ratio_idc;
  5605. int nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag;
  5606. aspect_ratio_info_present_flag= get_bits1(&s->gb);
  5607. if( aspect_ratio_info_present_flag ) {
  5608. aspect_ratio_idc= get_bits(&s->gb, 8);
  5609. if( aspect_ratio_idc == EXTENDED_SAR ) {
  5610. sps->sar.num= get_bits(&s->gb, 16);
  5611. sps->sar.den= get_bits(&s->gb, 16);
  5612. }else if(aspect_ratio_idc < 16){
  5613. sps->sar= pixel_aspect[aspect_ratio_idc];
  5614. }else{
  5615. av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
  5616. return -1;
  5617. }
  5618. }else{
  5619. sps->sar.num=
  5620. sps->sar.den= 0;
  5621. }
  5622. // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
  5623. if(get_bits1(&s->gb)){ /* overscan_info_present_flag */
  5624. get_bits1(&s->gb); /* overscan_appropriate_flag */
  5625. }
  5626. if(get_bits1(&s->gb)){ /* video_signal_type_present_flag */
  5627. get_bits(&s->gb, 3); /* video_format */
  5628. get_bits1(&s->gb); /* video_full_range_flag */
  5629. if(get_bits1(&s->gb)){ /* colour_description_present_flag */
  5630. get_bits(&s->gb, 8); /* colour_primaries */
  5631. get_bits(&s->gb, 8); /* transfer_characteristics */
  5632. get_bits(&s->gb, 8); /* matrix_coefficients */
  5633. }
  5634. }
  5635. if(get_bits1(&s->gb)){ /* chroma_location_info_present_flag */
  5636. get_ue_golomb(&s->gb); /* chroma_sample_location_type_top_field */
  5637. get_ue_golomb(&s->gb); /* chroma_sample_location_type_bottom_field */
  5638. }
  5639. sps->timing_info_present_flag = get_bits1(&s->gb);
  5640. if(sps->timing_info_present_flag){
  5641. sps->num_units_in_tick = get_bits_long(&s->gb, 32);
  5642. sps->time_scale = get_bits_long(&s->gb, 32);
  5643. sps->fixed_frame_rate_flag = get_bits1(&s->gb);
  5644. }
  5645. nal_hrd_parameters_present_flag = get_bits1(&s->gb);
  5646. if(nal_hrd_parameters_present_flag)
  5647. decode_hrd_parameters(h, sps);
  5648. vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
  5649. if(vcl_hrd_parameters_present_flag)
  5650. decode_hrd_parameters(h, sps);
  5651. if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag)
  5652. get_bits1(&s->gb); /* low_delay_hrd_flag */
  5653. get_bits1(&s->gb); /* pic_struct_present_flag */
  5654. sps->bitstream_restriction_flag = get_bits1(&s->gb);
  5655. if(sps->bitstream_restriction_flag){
  5656. get_bits1(&s->gb); /* motion_vectors_over_pic_boundaries_flag */
  5657. get_ue_golomb(&s->gb); /* max_bytes_per_pic_denom */
  5658. get_ue_golomb(&s->gb); /* max_bits_per_mb_denom */
  5659. get_ue_golomb(&s->gb); /* log2_max_mv_length_horizontal */
  5660. get_ue_golomb(&s->gb); /* log2_max_mv_length_vertical */
  5661. sps->num_reorder_frames = get_ue_golomb(&s->gb);
  5662. get_ue_golomb(&s->gb); /* max_dec_frame_buffering */
  5663. }
  5664. return 0;
  5665. }
  5666. static inline int decode_seq_parameter_set(H264Context *h){
  5667. MpegEncContext * const s = &h->s;
  5668. int profile_idc, level_idc;
  5669. int sps_id, i;
  5670. SPS *sps;
  5671. profile_idc= get_bits(&s->gb, 8);
  5672. get_bits1(&s->gb); //constraint_set0_flag
  5673. get_bits1(&s->gb); //constraint_set1_flag
  5674. get_bits1(&s->gb); //constraint_set2_flag
  5675. get_bits1(&s->gb); //constraint_set3_flag
  5676. get_bits(&s->gb, 4); // reserved
  5677. level_idc= get_bits(&s->gb, 8);
  5678. sps_id= get_ue_golomb(&s->gb);
  5679. sps= &h->sps_buffer[ sps_id ];
  5680. sps->profile_idc= profile_idc;
  5681. sps->level_idc= level_idc;
  5682. sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
  5683. sps->poc_type= get_ue_golomb(&s->gb);
  5684. if(sps->poc_type == 0){ //FIXME #define
  5685. sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
  5686. } else if(sps->poc_type == 1){//FIXME #define
  5687. sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
  5688. sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
  5689. sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
  5690. sps->poc_cycle_length= get_ue_golomb(&s->gb);
  5691. for(i=0; i<sps->poc_cycle_length; i++)
  5692. sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
  5693. }
  5694. if(sps->poc_type > 2){
  5695. av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
  5696. return -1;
  5697. }
  5698. sps->ref_frame_count= get_ue_golomb(&s->gb);
  5699. if(sps->ref_frame_count > MAX_PICTURE_COUNT-2){
  5700. av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");
  5701. }
  5702. sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
  5703. sps->mb_width= get_ue_golomb(&s->gb) + 1;
  5704. sps->mb_height= get_ue_golomb(&s->gb) + 1;
  5705. if((unsigned)sps->mb_width >= INT_MAX/16 || (unsigned)sps->mb_height >= INT_MAX/16 ||
  5706. avcodec_check_dimensions(NULL, 16*sps->mb_width, 16*sps->mb_height))
  5707. return -1;
  5708. sps->frame_mbs_only_flag= get_bits1(&s->gb);
  5709. if(!sps->frame_mbs_only_flag)
  5710. sps->mb_aff= get_bits1(&s->gb);
  5711. else
  5712. sps->mb_aff= 0;
  5713. sps->direct_8x8_inference_flag= get_bits1(&s->gb);
  5714. sps->crop= get_bits1(&s->gb);
  5715. if(sps->crop){
  5716. sps->crop_left = get_ue_golomb(&s->gb);
  5717. sps->crop_right = get_ue_golomb(&s->gb);
  5718. sps->crop_top = get_ue_golomb(&s->gb);
  5719. sps->crop_bottom= get_ue_golomb(&s->gb);
  5720. if(sps->crop_left || sps->crop_top){
  5721. av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ...\n");
  5722. }
  5723. }else{
  5724. sps->crop_left =
  5725. sps->crop_right =
  5726. sps->crop_top =
  5727. sps->crop_bottom= 0;
  5728. }
  5729. sps->vui_parameters_present_flag= get_bits1(&s->gb);
  5730. if( sps->vui_parameters_present_flag )
  5731. decode_vui_parameters(h, sps);
  5732. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  5733. 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",
  5734. sps_id, sps->profile_idc, sps->level_idc,
  5735. sps->poc_type,
  5736. sps->ref_frame_count,
  5737. sps->mb_width, sps->mb_height,
  5738. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  5739. sps->direct_8x8_inference_flag ? "8B8" : "",
  5740. sps->crop_left, sps->crop_right,
  5741. sps->crop_top, sps->crop_bottom,
  5742. sps->vui_parameters_present_flag ? "VUI" : ""
  5743. );
  5744. }
  5745. return 0;
  5746. }
  5747. static inline int decode_picture_parameter_set(H264Context *h){
  5748. MpegEncContext * const s = &h->s;
  5749. int pps_id= get_ue_golomb(&s->gb);
  5750. PPS *pps= &h->pps_buffer[pps_id];
  5751. pps->sps_id= get_ue_golomb(&s->gb);
  5752. pps->cabac= get_bits1(&s->gb);
  5753. pps->pic_order_present= get_bits1(&s->gb);
  5754. pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
  5755. if(pps->slice_group_count > 1 ){
  5756. pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
  5757. av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
  5758. switch(pps->mb_slice_group_map_type){
  5759. case 0:
  5760. #if 0
  5761. | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
  5762. | run_length[ i ] |1 |ue(v) |
  5763. #endif
  5764. break;
  5765. case 2:
  5766. #if 0
  5767. | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
  5768. |{ | | |
  5769. | top_left_mb[ i ] |1 |ue(v) |
  5770. | bottom_right_mb[ i ] |1 |ue(v) |
  5771. | } | | |
  5772. #endif
  5773. break;
  5774. case 3:
  5775. case 4:
  5776. case 5:
  5777. #if 0
  5778. | slice_group_change_direction_flag |1 |u(1) |
  5779. | slice_group_change_rate_minus1 |1 |ue(v) |
  5780. #endif
  5781. break;
  5782. case 6:
  5783. #if 0
  5784. | slice_group_id_cnt_minus1 |1 |ue(v) |
  5785. | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
  5786. |) | | |
  5787. | slice_group_id[ i ] |1 |u(v) |
  5788. #endif
  5789. break;
  5790. }
  5791. }
  5792. pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  5793. pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  5794. if(pps->ref_count[0] > 32 || pps->ref_count[1] > 32){
  5795. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
  5796. return -1;
  5797. }
  5798. pps->weighted_pred= get_bits1(&s->gb);
  5799. pps->weighted_bipred_idc= get_bits(&s->gb, 2);
  5800. pps->init_qp= get_se_golomb(&s->gb) + 26;
  5801. pps->init_qs= get_se_golomb(&s->gb) + 26;
  5802. pps->chroma_qp_index_offset= get_se_golomb(&s->gb);
  5803. pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
  5804. pps->constrained_intra_pred= get_bits1(&s->gb);
  5805. pps->redundant_pic_cnt_present = get_bits1(&s->gb);
  5806. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  5807. 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",
  5808. pps_id, pps->sps_id,
  5809. pps->cabac ? "CABAC" : "CAVLC",
  5810. pps->slice_group_count,
  5811. pps->ref_count[0], pps->ref_count[1],
  5812. pps->weighted_pred ? "weighted" : "",
  5813. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset,
  5814. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  5815. pps->constrained_intra_pred ? "CONSTR" : "",
  5816. pps->redundant_pic_cnt_present ? "REDU" : ""
  5817. );
  5818. }
  5819. return 0;
  5820. }
  5821. /**
  5822. * finds the end of the current frame in the bitstream.
  5823. * @return the position of the first byte of the next frame, or -1
  5824. */
  5825. static int find_frame_end(H264Context *h, const uint8_t *buf, int buf_size){
  5826. int i;
  5827. uint32_t state;
  5828. ParseContext *pc = &(h->s.parse_context);
  5829. //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
  5830. // mb_addr= pc->mb_addr - 1;
  5831. state= pc->state;
  5832. for(i=0; i<=buf_size; i++){
  5833. if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  5834. tprintf("find_frame_end new startcode = %08x, frame_start_found = %d, pos = %d\n", state, pc->frame_start_found, i);
  5835. if(pc->frame_start_found){
  5836. // If there isn't one more byte in the buffer
  5837. // the test on first_mb_in_slice cannot be done yet
  5838. // do it at next call.
  5839. if (i >= buf_size) break;
  5840. if (buf[i] & 0x80) {
  5841. // first_mb_in_slice is 0, probably the first nal of a new
  5842. // slice
  5843. tprintf("find_frame_end frame_end_found, state = %08x, pos = %d\n", state, i);
  5844. pc->state=-1;
  5845. pc->frame_start_found= 0;
  5846. return i-4;
  5847. }
  5848. }
  5849. pc->frame_start_found = 1;
  5850. }
  5851. if (i<buf_size)
  5852. state= (state<<8) | buf[i];
  5853. }
  5854. pc->state= state;
  5855. return END_NOT_FOUND;
  5856. }
  5857. static int h264_parse(AVCodecParserContext *s,
  5858. AVCodecContext *avctx,
  5859. uint8_t **poutbuf, int *poutbuf_size,
  5860. const uint8_t *buf, int buf_size)
  5861. {
  5862. H264Context *h = s->priv_data;
  5863. ParseContext *pc = &h->s.parse_context;
  5864. int next;
  5865. next= find_frame_end(h, buf, buf_size);
  5866. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  5867. *poutbuf = NULL;
  5868. *poutbuf_size = 0;
  5869. return buf_size;
  5870. }
  5871. *poutbuf = (uint8_t *)buf;
  5872. *poutbuf_size = buf_size;
  5873. return next;
  5874. }
  5875. static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){
  5876. MpegEncContext * const s = &h->s;
  5877. AVCodecContext * const avctx= s->avctx;
  5878. int buf_index=0;
  5879. #if 0
  5880. int i;
  5881. for(i=0; i<32; i++){
  5882. printf("%X ", buf[i]);
  5883. }
  5884. #endif
  5885. h->slice_num = 0;
  5886. for(;;){
  5887. int consumed;
  5888. int dst_length;
  5889. int bit_length;
  5890. uint8_t *ptr;
  5891. int i, nalsize = 0;
  5892. if(h->is_avc) {
  5893. if(buf_index >= buf_size) break;
  5894. nalsize = 0;
  5895. for(i = 0; i < h->nal_length_size; i++)
  5896. nalsize = (nalsize << 8) | buf[buf_index++];
  5897. } else {
  5898. // start code prefix search
  5899. for(; buf_index + 3 < buf_size; buf_index++){
  5900. // this should allways succeed in the first iteration
  5901. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  5902. break;
  5903. }
  5904. if(buf_index+3 >= buf_size) break;
  5905. buf_index+=3;
  5906. }
  5907. ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, h->is_avc ? nalsize : buf_size - buf_index);
  5908. if(ptr[dst_length - 1] == 0) dst_length--;
  5909. bit_length= 8*dst_length - decode_rbsp_trailing(ptr + dst_length - 1);
  5910. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  5911. 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);
  5912. }
  5913. if (h->is_avc && (nalsize != consumed))
  5914. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  5915. buf_index += consumed;
  5916. if( s->hurry_up == 1 && h->nal_ref_idc == 0 )
  5917. continue;
  5918. switch(h->nal_unit_type){
  5919. case NAL_IDR_SLICE:
  5920. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  5921. case NAL_SLICE:
  5922. init_get_bits(&s->gb, ptr, bit_length);
  5923. h->intra_gb_ptr=
  5924. h->inter_gb_ptr= &s->gb;
  5925. s->data_partitioning = 0;
  5926. if(decode_slice_header(h) < 0) return -1;
  5927. if(h->redundant_pic_count==0 && s->hurry_up < 5 )
  5928. decode_slice(h);
  5929. break;
  5930. case NAL_DPA:
  5931. init_get_bits(&s->gb, ptr, bit_length);
  5932. h->intra_gb_ptr=
  5933. h->inter_gb_ptr= NULL;
  5934. s->data_partitioning = 1;
  5935. if(decode_slice_header(h) < 0) return -1;
  5936. break;
  5937. case NAL_DPB:
  5938. init_get_bits(&h->intra_gb, ptr, bit_length);
  5939. h->intra_gb_ptr= &h->intra_gb;
  5940. break;
  5941. case NAL_DPC:
  5942. init_get_bits(&h->inter_gb, ptr, bit_length);
  5943. h->inter_gb_ptr= &h->inter_gb;
  5944. if(h->redundant_pic_count==0 && h->intra_gb_ptr && s->data_partitioning && s->hurry_up < 5 )
  5945. decode_slice(h);
  5946. break;
  5947. case NAL_SEI:
  5948. break;
  5949. case NAL_SPS:
  5950. init_get_bits(&s->gb, ptr, bit_length);
  5951. decode_seq_parameter_set(h);
  5952. if(s->flags& CODEC_FLAG_LOW_DELAY)
  5953. s->low_delay=1;
  5954. if(avctx->has_b_frames < 2)
  5955. avctx->has_b_frames= !s->low_delay;
  5956. break;
  5957. case NAL_PPS:
  5958. init_get_bits(&s->gb, ptr, bit_length);
  5959. decode_picture_parameter_set(h);
  5960. break;
  5961. case NAL_PICTURE_DELIMITER:
  5962. break;
  5963. case NAL_FILTER_DATA:
  5964. break;
  5965. default:
  5966. av_log(avctx, AV_LOG_ERROR, "Unknown NAL code: %d\n", h->nal_unit_type);
  5967. }
  5968. }
  5969. if(!s->current_picture_ptr) return buf_index; //no frame
  5970. s->current_picture_ptr->pict_type= s->pict_type;
  5971. s->current_picture_ptr->key_frame= s->pict_type == I_TYPE && h->nal_unit_type == NAL_IDR_SLICE;
  5972. h->prev_frame_num_offset= h->frame_num_offset;
  5973. h->prev_frame_num= h->frame_num;
  5974. if(s->current_picture_ptr->reference){
  5975. h->prev_poc_msb= h->poc_msb;
  5976. h->prev_poc_lsb= h->poc_lsb;
  5977. }
  5978. if(s->current_picture_ptr->reference)
  5979. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  5980. ff_er_frame_end(s);
  5981. MPV_frame_end(s);
  5982. return buf_index;
  5983. }
  5984. /**
  5985. * returns the number of bytes consumed for building the current frame
  5986. */
  5987. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  5988. if(s->flags&CODEC_FLAG_TRUNCATED){
  5989. pos -= s->parse_context.last_index;
  5990. if(pos<0) pos=0; // FIXME remove (unneeded?)
  5991. return pos;
  5992. }else{
  5993. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  5994. if(pos+10>buf_size) pos=buf_size; // oops ;)
  5995. return pos;
  5996. }
  5997. }
  5998. static int decode_frame(AVCodecContext *avctx,
  5999. void *data, int *data_size,
  6000. uint8_t *buf, int buf_size)
  6001. {
  6002. H264Context *h = avctx->priv_data;
  6003. MpegEncContext *s = &h->s;
  6004. AVFrame *pict = data;
  6005. int buf_index;
  6006. s->flags= avctx->flags;
  6007. s->flags2= avctx->flags2;
  6008. /* no supplementary picture */
  6009. if (buf_size == 0) {
  6010. return 0;
  6011. }
  6012. if(s->flags&CODEC_FLAG_TRUNCATED){
  6013. int next= find_frame_end(h, buf, buf_size);
  6014. if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
  6015. return buf_size;
  6016. //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);
  6017. }
  6018. if(h->is_avc && !h->got_avcC) {
  6019. int i, cnt, nalsize;
  6020. unsigned char *p = avctx->extradata;
  6021. if(avctx->extradata_size < 7) {
  6022. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  6023. return -1;
  6024. }
  6025. if(*p != 1) {
  6026. av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
  6027. return -1;
  6028. }
  6029. /* sps and pps in the avcC always have length coded with 2 bytes,
  6030. so put a fake nal_length_size = 2 while parsing them */
  6031. h->nal_length_size = 2;
  6032. // Decode sps from avcC
  6033. cnt = *(p+5) & 0x1f; // Number of sps
  6034. p += 6;
  6035. for (i = 0; i < cnt; i++) {
  6036. nalsize = BE_16(p) + 2;
  6037. if(decode_nal_units(h, p, nalsize) != nalsize) {
  6038. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  6039. return -1;
  6040. }
  6041. p += nalsize;
  6042. }
  6043. // Decode pps from avcC
  6044. cnt = *(p++); // Number of pps
  6045. for (i = 0; i < cnt; i++) {
  6046. nalsize = BE_16(p) + 2;
  6047. if(decode_nal_units(h, p, nalsize) != nalsize) {
  6048. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  6049. return -1;
  6050. }
  6051. p += nalsize;
  6052. }
  6053. // Now store right nal length size, that will be use to parse all other nals
  6054. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  6055. // Do not reparse avcC
  6056. h->got_avcC = 1;
  6057. }
  6058. if(!h->is_avc && s->avctx->extradata_size && s->picture_number==0){
  6059. if(0 < decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) )
  6060. return -1;
  6061. }
  6062. buf_index=decode_nal_units(h, buf, buf_size);
  6063. if(buf_index < 0)
  6064. return -1;
  6065. //FIXME do something with unavailable reference frames
  6066. // if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_index, buf_size);
  6067. if(!s->current_picture_ptr){
  6068. av_log(h->s.avctx, AV_LOG_DEBUG, "error, NO frame\n");
  6069. return -1;
  6070. }
  6071. {
  6072. Picture *out = s->current_picture_ptr;
  6073. #if 0 //decode order
  6074. *data_size = sizeof(AVFrame);
  6075. #else
  6076. /* Sort B-frames into display order */
  6077. Picture *cur = s->current_picture_ptr;
  6078. Picture *prev = h->delayed_output_pic;
  6079. int out_idx = 0;
  6080. int pics = 0;
  6081. int out_of_order;
  6082. int cross_idr = 0;
  6083. int dropped_frame = 0;
  6084. int i;
  6085. if(h->sps.bitstream_restriction_flag
  6086. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  6087. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  6088. s->low_delay = 0;
  6089. }
  6090. while(h->delayed_pic[pics]) pics++;
  6091. h->delayed_pic[pics++] = cur;
  6092. if(cur->reference == 0)
  6093. cur->reference = 1;
  6094. for(i=0; h->delayed_pic[i]; i++)
  6095. if(h->delayed_pic[i]->key_frame || h->delayed_pic[i]->poc==0)
  6096. cross_idr = 1;
  6097. out = h->delayed_pic[0];
  6098. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
  6099. if(h->delayed_pic[i]->poc < out->poc){
  6100. out = h->delayed_pic[i];
  6101. out_idx = i;
  6102. }
  6103. out_of_order = !cross_idr && prev && out->poc < prev->poc;
  6104. if(prev && pics <= s->avctx->has_b_frames)
  6105. out = prev;
  6106. else if((out_of_order && pics-1 == s->avctx->has_b_frames)
  6107. || (s->low_delay &&
  6108. ((!cross_idr && prev && out->poc > prev->poc + 2)
  6109. || cur->pict_type == B_TYPE)))
  6110. {
  6111. s->low_delay = 0;
  6112. s->avctx->has_b_frames++;
  6113. out = prev;
  6114. }
  6115. else if(out_of_order)
  6116. out = prev;
  6117. if(out_of_order || pics > s->avctx->has_b_frames){
  6118. dropped_frame = (out != h->delayed_pic[out_idx]);
  6119. for(i=out_idx; h->delayed_pic[i]; i++)
  6120. h->delayed_pic[i] = h->delayed_pic[i+1];
  6121. }
  6122. if(prev == out && !dropped_frame)
  6123. *data_size = 0;
  6124. else
  6125. *data_size = sizeof(AVFrame);
  6126. if(prev && prev != out && prev->reference == 1)
  6127. prev->reference = 0;
  6128. h->delayed_output_pic = out;
  6129. #endif
  6130. *pict= *(AVFrame*)out;
  6131. }
  6132. assert(pict->data[0]);
  6133. ff_print_debug_info(s, pict);
  6134. //printf("out %d\n", (int)pict->data[0]);
  6135. #if 0 //?
  6136. /* Return the Picture timestamp as the frame number */
  6137. /* we substract 1 because it is added on utils.c */
  6138. avctx->frame_number = s->picture_number - 1;
  6139. #endif
  6140. return get_consumed_bytes(s, buf_index, buf_size);
  6141. }
  6142. #if 0
  6143. static inline void fill_mb_avail(H264Context *h){
  6144. MpegEncContext * const s = &h->s;
  6145. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  6146. if(s->mb_y){
  6147. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  6148. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  6149. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  6150. }else{
  6151. h->mb_avail[0]=
  6152. h->mb_avail[1]=
  6153. h->mb_avail[2]= 0;
  6154. }
  6155. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  6156. h->mb_avail[4]= 1; //FIXME move out
  6157. h->mb_avail[5]= 0; //FIXME move out
  6158. }
  6159. #endif
  6160. #if 0 //selftest
  6161. #define COUNT 8000
  6162. #define SIZE (COUNT*40)
  6163. int main(){
  6164. int i;
  6165. uint8_t temp[SIZE];
  6166. PutBitContext pb;
  6167. GetBitContext gb;
  6168. // int int_temp[10000];
  6169. DSPContext dsp;
  6170. AVCodecContext avctx;
  6171. dsputil_init(&dsp, &avctx);
  6172. init_put_bits(&pb, temp, SIZE);
  6173. printf("testing unsigned exp golomb\n");
  6174. for(i=0; i<COUNT; i++){
  6175. START_TIMER
  6176. set_ue_golomb(&pb, i);
  6177. STOP_TIMER("set_ue_golomb");
  6178. }
  6179. flush_put_bits(&pb);
  6180. init_get_bits(&gb, temp, 8*SIZE);
  6181. for(i=0; i<COUNT; i++){
  6182. int j, s;
  6183. s= show_bits(&gb, 24);
  6184. START_TIMER
  6185. j= get_ue_golomb(&gb);
  6186. if(j != i){
  6187. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  6188. // return -1;
  6189. }
  6190. STOP_TIMER("get_ue_golomb");
  6191. }
  6192. init_put_bits(&pb, temp, SIZE);
  6193. printf("testing signed exp golomb\n");
  6194. for(i=0; i<COUNT; i++){
  6195. START_TIMER
  6196. set_se_golomb(&pb, i - COUNT/2);
  6197. STOP_TIMER("set_se_golomb");
  6198. }
  6199. flush_put_bits(&pb);
  6200. init_get_bits(&gb, temp, 8*SIZE);
  6201. for(i=0; i<COUNT; i++){
  6202. int j, s;
  6203. s= show_bits(&gb, 24);
  6204. START_TIMER
  6205. j= get_se_golomb(&gb);
  6206. if(j != i - COUNT/2){
  6207. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  6208. // return -1;
  6209. }
  6210. STOP_TIMER("get_se_golomb");
  6211. }
  6212. printf("testing 4x4 (I)DCT\n");
  6213. DCTELEM block[16];
  6214. uint8_t src[16], ref[16];
  6215. uint64_t error= 0, max_error=0;
  6216. for(i=0; i<COUNT; i++){
  6217. int j;
  6218. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  6219. for(j=0; j<16; j++){
  6220. ref[j]= random()%255;
  6221. src[j]= random()%255;
  6222. }
  6223. h264_diff_dct_c(block, src, ref, 4);
  6224. //normalize
  6225. for(j=0; j<16; j++){
  6226. // printf("%d ", block[j]);
  6227. block[j]= block[j]*4;
  6228. if(j&1) block[j]= (block[j]*4 + 2)/5;
  6229. if(j&4) block[j]= (block[j]*4 + 2)/5;
  6230. }
  6231. // printf("\n");
  6232. s->dsp.h264_idct_add(ref, block, 4);
  6233. /* for(j=0; j<16; j++){
  6234. printf("%d ", ref[j]);
  6235. }
  6236. printf("\n");*/
  6237. for(j=0; j<16; j++){
  6238. int diff= ABS(src[j] - ref[j]);
  6239. error+= diff*diff;
  6240. max_error= FFMAX(max_error, diff);
  6241. }
  6242. }
  6243. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  6244. #if 0
  6245. printf("testing quantizer\n");
  6246. for(qp=0; qp<52; qp++){
  6247. for(i=0; i<16; i++)
  6248. src1_block[i]= src2_block[i]= random()%255;
  6249. }
  6250. #endif
  6251. printf("Testing NAL layer\n");
  6252. uint8_t bitstream[COUNT];
  6253. uint8_t nal[COUNT*2];
  6254. H264Context h;
  6255. memset(&h, 0, sizeof(H264Context));
  6256. for(i=0; i<COUNT; i++){
  6257. int zeros= i;
  6258. int nal_length;
  6259. int consumed;
  6260. int out_length;
  6261. uint8_t *out;
  6262. int j;
  6263. for(j=0; j<COUNT; j++){
  6264. bitstream[j]= (random() % 255) + 1;
  6265. }
  6266. for(j=0; j<zeros; j++){
  6267. int pos= random() % COUNT;
  6268. while(bitstream[pos] == 0){
  6269. pos++;
  6270. pos %= COUNT;
  6271. }
  6272. bitstream[pos]=0;
  6273. }
  6274. START_TIMER
  6275. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  6276. if(nal_length<0){
  6277. printf("encoding failed\n");
  6278. return -1;
  6279. }
  6280. out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
  6281. STOP_TIMER("NAL")
  6282. if(out_length != COUNT){
  6283. printf("incorrect length %d %d\n", out_length, COUNT);
  6284. return -1;
  6285. }
  6286. if(consumed != nal_length){
  6287. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  6288. return -1;
  6289. }
  6290. if(memcmp(bitstream, out, COUNT)){
  6291. printf("missmatch\n");
  6292. return -1;
  6293. }
  6294. }
  6295. printf("Testing RBSP\n");
  6296. return 0;
  6297. }
  6298. #endif
  6299. static int decode_end(AVCodecContext *avctx)
  6300. {
  6301. H264Context *h = avctx->priv_data;
  6302. MpegEncContext *s = &h->s;
  6303. free_tables(h); //FIXME cleanup init stuff perhaps
  6304. MPV_common_end(s);
  6305. // memset(h, 0, sizeof(H264Context));
  6306. return 0;
  6307. }
  6308. AVCodec h264_decoder = {
  6309. "h264",
  6310. CODEC_TYPE_VIDEO,
  6311. CODEC_ID_H264,
  6312. sizeof(H264Context),
  6313. decode_init,
  6314. NULL,
  6315. decode_end,
  6316. decode_frame,
  6317. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  6318. .flush= flush_dpb,
  6319. };
  6320. AVCodecParser h264_parser = {
  6321. { CODEC_ID_H264 },
  6322. sizeof(H264Context),
  6323. NULL,
  6324. h264_parse,
  6325. ff_parse_close,
  6326. };
  6327. #include "svq3.c"