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.

6563 lines
234KB

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