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.

7248 lines
267KB

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