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.

6021 lines
209KB

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