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.

6384 lines
226KB

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