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.

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