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.

6003 lines
207KB

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