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.

6044 lines
210KB

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