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.

6024 lines
207KB

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