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.

7280 lines
268KB

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