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.

6600 lines
236KB

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