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.

6098 lines
211KB

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