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.

7334 lines
270KB

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