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.

6808 lines
245KB

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