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.

7233 lines
266KB

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