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.

6097 lines
210KB

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