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.

6848 lines
246KB

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