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.

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