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.

4420 lines
150KB

  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. #undef NDEBUG
  32. #include <assert.h>
  33. #define interlaced_dct interlaced_dct_is_a_bad_name
  34. #define mb_intra mb_intra_isnt_initalized_see_mb_type
  35. #define LUMA_DC_BLOCK_INDEX 25
  36. #define CHROMA_DC_BLOCK_INDEX 26
  37. #define CHROMA_DC_COEFF_TOKEN_VLC_BITS 8
  38. #define COEFF_TOKEN_VLC_BITS 8
  39. #define TOTAL_ZEROS_VLC_BITS 9
  40. #define CHROMA_DC_TOTAL_ZEROS_VLC_BITS 3
  41. #define RUN_VLC_BITS 3
  42. #define RUN7_VLC_BITS 6
  43. #define MAX_SPS_COUNT 32
  44. #define MAX_PPS_COUNT 256
  45. #define MAX_MMCO_COUNT 66
  46. /**
  47. * Sequence parameter set
  48. */
  49. typedef struct SPS{
  50. int profile_idc;
  51. int level_idc;
  52. int multiple_slice_groups; ///< more_than_one_slice_group_allowed_flag
  53. int arbitrary_slice_order; ///< arbitrary_slice_order_allowed_flag
  54. int redundant_slices; ///< redundant_slices_allowed_flag
  55. int log2_max_frame_num; ///< log2_max_frame_num_minus4 + 4
  56. int poc_type; ///< pic_order_cnt_type
  57. int log2_max_poc_lsb; ///< log2_max_pic_order_cnt_lsb_minus4
  58. int delta_pic_order_always_zero_flag;
  59. int offset_for_non_ref_pic;
  60. int offset_for_top_to_bottom_field;
  61. int poc_cycle_length; ///< num_ref_frames_in_pic_order_cnt_cycle
  62. int ref_frame_count; ///< num_ref_frames
  63. int required_frame_num_update_behaviour_flag;
  64. int mb_width; ///< frame_width_in_mbs_minus1 + 1
  65. int mb_height; ///< frame_height_in_mbs_minus1 + 1
  66. int frame_mbs_only_flag;
  67. int mb_aff; ///<mb_adaptive_frame_field_flag
  68. int direct_8x8_inference_flag;
  69. int vui_parameters_present_flag;
  70. int sar_width;
  71. int sar_height;
  72. short offset_for_ref_frame[256]; //FIXME dyn aloc?
  73. }SPS;
  74. /**
  75. * Picture parameter set
  76. */
  77. typedef struct PPS{
  78. int sps_id;
  79. int cabac; ///< entropy_coding_mode_flag
  80. int pic_order_present; ///< pic_order_present_flag
  81. int slice_group_count; ///< num_slice_groups_minus1 + 1
  82. int mb_slice_group_map_type;
  83. int ref_count[2]; ///< num_ref_idx_l0/1_active_minus1 + 1
  84. int weighted_pred; ///< weighted_pred_flag
  85. int weighted_bipred_idc;
  86. int init_qp; ///< pic_init_qp_minus26 + 26
  87. int init_qs; ///< pic_init_qs_minus26 + 26
  88. int chroma_qp_index_offset;
  89. int deblocking_filter_parameters_present; ///< deblocking_filter_parameters_present_flag
  90. int constrained_intra_pred; ///< constrained_intra_pred_flag
  91. int redundant_pic_cnt_present; ///< redundant_pic_cnt_present_flag
  92. int crop; ///< frame_cropping_flag
  93. int crop_left; ///< frame_cropping_rect_left_offset
  94. int crop_right; ///< frame_cropping_rect_right_offset
  95. int crop_top; ///< frame_cropping_rect_top_offset
  96. int crop_bottom; ///< frame_cropping_rect_bottom_offset
  97. }PPS;
  98. /**
  99. * Memory management control operation opcode.
  100. */
  101. typedef enum MMCOOpcode{
  102. MMCO_END=0,
  103. MMCO_SHORT2UNUSED,
  104. MMCO_LONG2UNUSED,
  105. MMCO_SHORT2LONG,
  106. MMCO_SET_MAX_LONG,
  107. MMCO_RESET,
  108. MMCO_LONG,
  109. } MMCOOpcode;
  110. /**
  111. * Memory management control operation.
  112. */
  113. typedef struct MMCO{
  114. MMCOOpcode opcode;
  115. int short_frame_num;
  116. int long_index;
  117. } MMCO;
  118. /**
  119. * H264Context
  120. */
  121. typedef struct H264Context{
  122. MpegEncContext s;
  123. int nal_ref_idc;
  124. int nal_unit_type;
  125. #define NAL_SLICE 1
  126. #define NAL_DPA 2
  127. #define NAL_DPB 3
  128. #define NAL_DPC 4
  129. #define NAL_IDR_SLICE 5
  130. #define NAL_SEI 6
  131. #define NAL_SPS 7
  132. #define NAL_PPS 8
  133. #define NAL_PICTURE_DELIMITER 9
  134. #define NAL_FILTER_DATA 10
  135. uint8_t *rbsp_buffer;
  136. int rbsp_buffer_size;
  137. int chroma_qp; //QPc
  138. int prev_mb_skiped; //FIXME remove (IMHO not used)
  139. //prediction stuff
  140. int chroma_pred_mode;
  141. int intra16x16_pred_mode;
  142. int8_t intra4x4_pred_mode_cache[5*8];
  143. int8_t (*intra4x4_pred_mode)[8];
  144. void (*pred4x4 [9+3])(uint8_t *src, uint8_t *topright, int stride);//FIXME move to dsp?
  145. void (*pred8x8 [4+3])(uint8_t *src, int stride);
  146. void (*pred16x16[4+3])(uint8_t *src, int stride);
  147. unsigned int topleft_samples_available;
  148. unsigned int top_samples_available;
  149. unsigned int topright_samples_available;
  150. unsigned int left_samples_available;
  151. /**
  152. * non zero coeff count cache.
  153. * is 64 if not available.
  154. */
  155. uint8_t non_zero_count_cache[6*8];
  156. uint8_t (*non_zero_count)[16];
  157. /**
  158. * Motion vector cache.
  159. */
  160. int16_t mv_cache[2][5*8][2];
  161. int8_t ref_cache[2][5*8];
  162. #define LIST_NOT_USED -1 //FIXME rename?
  163. #define PART_NOT_AVAILABLE -2
  164. /**
  165. * is 1 if the specific list MV&references are set to 0,0,-2.
  166. */
  167. int mv_cache_clean[2];
  168. int block_offset[16+8];
  169. int chroma_subblock_offset[16]; //FIXME remove
  170. uint16_t *mb2b_xy; //FIXME are these 4 a good idea?
  171. uint16_t *mb2b8_xy;
  172. int b_stride;
  173. int b8_stride;
  174. int halfpel_flag;
  175. int thirdpel_flag;
  176. SPS sps_buffer[MAX_SPS_COUNT];
  177. SPS sps; ///< current sps
  178. PPS pps_buffer[MAX_PPS_COUNT];
  179. /**
  180. * current pps
  181. */
  182. PPS pps; //FIXME move tp Picture perhaps? (->no) do we need that?
  183. int slice_num;
  184. uint8_t *slice_table_base;
  185. uint8_t *slice_table; ///< slice_table_base + mb_stride + 1
  186. int slice_type;
  187. int slice_type_fixed;
  188. //interlacing specific flags
  189. int mb_field_decoding_flag;
  190. int sub_mb_type[4];
  191. //POC stuff
  192. int poc_lsb;
  193. int poc_msb;
  194. int delta_poc_bottom;
  195. int delta_poc[2];
  196. int frame_num;
  197. int prev_poc_msb; ///< poc_msb of the last reference pic for POC type 0
  198. int prev_poc_lsb; ///< poc_lsb of the last reference pic for POC type 0
  199. int frame_num_offset; ///< for POC type 2
  200. int prev_frame_num_offset; ///< for POC type 2
  201. int prev_frame_num; ///< frame_num of the last pic for POC type 1/2
  202. /**
  203. * frame_num for frames or 2*frame_num for field pics.
  204. */
  205. int curr_pic_num;
  206. /**
  207. * max_frame_num or 2*max_frame_num for field pics.
  208. */
  209. int max_pic_num;
  210. //Weighted pred stuff
  211. int luma_log2_weight_denom;
  212. int chroma_log2_weight_denom;
  213. int luma_weight[2][16];
  214. int luma_offset[2][16];
  215. int chroma_weight[2][16][2];
  216. int chroma_offset[2][16][2];
  217. //deblock
  218. int disable_deblocking_filter_idc;
  219. int slice_alpha_c0_offset_div2;
  220. int slice_beta_offset_div2;
  221. int redundant_pic_count;
  222. int direct_spatial_mv_pred;
  223. /**
  224. * num_ref_idx_l0/1_active_minus1 + 1
  225. */
  226. int ref_count[2];// FIXME split for AFF
  227. Picture *short_ref[16];
  228. Picture *long_ref[16];
  229. Picture default_ref_list[2][32];
  230. Picture ref_list[2][32]; //FIXME size?
  231. Picture field_ref_list[2][32]; //FIXME size?
  232. /**
  233. * memory management control operations buffer.
  234. */
  235. MMCO mmco[MAX_MMCO_COUNT];
  236. int mmco_index;
  237. int long_ref_count; ///< number of actual long term references
  238. int short_ref_count; ///< number of actual short term references
  239. //data partitioning
  240. GetBitContext intra_gb;
  241. GetBitContext inter_gb;
  242. GetBitContext *intra_gb_ptr;
  243. GetBitContext *inter_gb_ptr;
  244. DCTELEM mb[16*24] __align8;
  245. }H264Context;
  246. static VLC coeff_token_vlc[4];
  247. static VLC chroma_dc_coeff_token_vlc;
  248. static VLC total_zeros_vlc[15];
  249. static VLC chroma_dc_total_zeros_vlc[3];
  250. static VLC run_vlc[6];
  251. static VLC run7_vlc;
  252. static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
  253. static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
  254. /**
  255. * fill a rectangle.
  256. * @param h height of the recatangle, should be a constant
  257. * @param w width of the recatangle, should be a constant
  258. * @param size the size of val (1 or 4), should be a constant
  259. */
  260. static inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){ //FIXME ensure this IS inlined
  261. uint8_t *p= (uint8_t*)vp;
  262. assert(size==1 || size==4);
  263. w *= size;
  264. stride *= size;
  265. //FIXME check what gcc generates for 64 bit on x86 and possible write a 32 bit ver of it
  266. if(w==2 && h==2){
  267. *(uint16_t*)(p + 0)=
  268. *(uint16_t*)(p + stride)= size==4 ? val : val*0x0101;
  269. }else if(w==2 && h==4){
  270. *(uint16_t*)(p + 0*stride)=
  271. *(uint16_t*)(p + 1*stride)=
  272. *(uint16_t*)(p + 2*stride)=
  273. *(uint16_t*)(p + 3*stride)= size==4 ? val : val*0x0101;
  274. }else if(w==4 && h==2){
  275. *(uint32_t*)(p + 0*stride)=
  276. *(uint32_t*)(p + 1*stride)= size==4 ? val : val*0x01010101;
  277. }else if(w==4 && h==4){
  278. *(uint32_t*)(p + 0*stride)=
  279. *(uint32_t*)(p + 1*stride)=
  280. *(uint32_t*)(p + 2*stride)=
  281. *(uint32_t*)(p + 3*stride)= size==4 ? val : val*0x01010101;
  282. }else if(w==8 && h==1){
  283. *(uint32_t*)(p + 0)=
  284. *(uint32_t*)(p + 4)= size==4 ? val : val*0x01010101;
  285. }else if(w==8 && h==2){
  286. *(uint32_t*)(p + 0 + 0*stride)=
  287. *(uint32_t*)(p + 4 + 0*stride)=
  288. *(uint32_t*)(p + 0 + 1*stride)=
  289. *(uint32_t*)(p + 4 + 1*stride)= size==4 ? val : val*0x01010101;
  290. }else if(w==8 && h==4){
  291. *(uint64_t*)(p + 0*stride)=
  292. *(uint64_t*)(p + 1*stride)=
  293. *(uint64_t*)(p + 2*stride)=
  294. *(uint64_t*)(p + 3*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
  295. }else if(w==16 && h==2){
  296. *(uint64_t*)(p + 0+0*stride)=
  297. *(uint64_t*)(p + 8+0*stride)=
  298. *(uint64_t*)(p + 0+1*stride)=
  299. *(uint64_t*)(p + 8+1*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
  300. }else if(w==16 && h==4){
  301. *(uint64_t*)(p + 0+0*stride)=
  302. *(uint64_t*)(p + 8+0*stride)=
  303. *(uint64_t*)(p + 0+1*stride)=
  304. *(uint64_t*)(p + 8+1*stride)=
  305. *(uint64_t*)(p + 0+2*stride)=
  306. *(uint64_t*)(p + 8+2*stride)=
  307. *(uint64_t*)(p + 0+3*stride)=
  308. *(uint64_t*)(p + 8+3*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
  309. }else
  310. assert(0);
  311. }
  312. static inline void fill_caches(H264Context *h, int mb_type){
  313. MpegEncContext * const s = &h->s;
  314. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  315. int topleft_xy, top_xy, topright_xy, left_xy[2];
  316. int topleft_type, top_type, topright_type, left_type[2];
  317. int left_block[4];
  318. int i;
  319. //wow what a mess, why didnt they simplify the interlacing&intra stuff, i cant imagine that these complex rules are worth it
  320. if(h->sps.mb_aff){
  321. //FIXME
  322. }else{
  323. topleft_xy = mb_xy-1 - s->mb_stride;
  324. top_xy = mb_xy - s->mb_stride;
  325. topright_xy= mb_xy+1 - s->mb_stride;
  326. left_xy[0] = mb_xy-1;
  327. left_xy[1] = mb_xy-1;
  328. left_block[0]= 0;
  329. left_block[1]= 1;
  330. left_block[2]= 2;
  331. left_block[3]= 3;
  332. }
  333. topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
  334. top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
  335. topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
  336. left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
  337. left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
  338. if(IS_INTRA(mb_type)){
  339. h->topleft_samples_available=
  340. h->top_samples_available=
  341. h->left_samples_available= 0xFFFF;
  342. h->topright_samples_available= 0xEEEA;
  343. if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
  344. h->topleft_samples_available= 0xB3FF;
  345. h->top_samples_available= 0x33FF;
  346. h->topright_samples_available= 0x26EA;
  347. }
  348. for(i=0; i<2; i++){
  349. if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
  350. h->topleft_samples_available&= 0xDF5F;
  351. h->left_samples_available&= 0x5F5F;
  352. }
  353. }
  354. if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
  355. h->topleft_samples_available&= 0x7FFF;
  356. if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
  357. h->topright_samples_available&= 0xFBFF;
  358. if(IS_INTRA4x4(mb_type)){
  359. if(IS_INTRA4x4(top_type)){
  360. h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
  361. h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
  362. h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
  363. h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
  364. }else{
  365. int pred;
  366. if(IS_INTRA16x16(top_type) || (IS_INTER(top_type) && !h->pps.constrained_intra_pred))
  367. pred= 2;
  368. else{
  369. pred= -1;
  370. }
  371. h->intra4x4_pred_mode_cache[4+8*0]=
  372. h->intra4x4_pred_mode_cache[5+8*0]=
  373. h->intra4x4_pred_mode_cache[6+8*0]=
  374. h->intra4x4_pred_mode_cache[7+8*0]= pred;
  375. }
  376. for(i=0; i<2; i++){
  377. if(IS_INTRA4x4(left_type[i])){
  378. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
  379. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
  380. }else{
  381. int pred;
  382. if(IS_INTRA16x16(left_type[i]) || (IS_INTER(left_type[i]) && !h->pps.constrained_intra_pred))
  383. pred= 2;
  384. else{
  385. pred= -1;
  386. }
  387. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
  388. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
  389. }
  390. }
  391. }
  392. }
  393. /*
  394. 0 . T T. T T T T
  395. 1 L . .L . . . .
  396. 2 L . .L . . . .
  397. 3 . T TL . . . .
  398. 4 L . .L . . . .
  399. 5 L . .. . . . .
  400. */
  401. //FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec)
  402. if(top_type){
  403. h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][0];
  404. h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][1];
  405. h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][2];
  406. h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
  407. h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][7];
  408. h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
  409. h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][10];
  410. h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
  411. }else{
  412. h->non_zero_count_cache[4+8*0]=
  413. h->non_zero_count_cache[5+8*0]=
  414. h->non_zero_count_cache[6+8*0]=
  415. h->non_zero_count_cache[7+8*0]=
  416. h->non_zero_count_cache[1+8*0]=
  417. h->non_zero_count_cache[2+8*0]=
  418. h->non_zero_count_cache[1+8*3]=
  419. h->non_zero_count_cache[2+8*3]= 64;
  420. }
  421. if(left_type[0]){
  422. h->non_zero_count_cache[3+8*1]= h->non_zero_count[left_xy[0]][6];
  423. h->non_zero_count_cache[3+8*2]= h->non_zero_count[left_xy[0]][5];
  424. h->non_zero_count_cache[0+8*1]= h->non_zero_count[left_xy[0]][9]; //FIXME left_block
  425. h->non_zero_count_cache[0+8*4]= h->non_zero_count[left_xy[0]][12];
  426. }else{
  427. h->non_zero_count_cache[3+8*1]=
  428. h->non_zero_count_cache[3+8*2]=
  429. h->non_zero_count_cache[0+8*1]=
  430. h->non_zero_count_cache[0+8*4]= 64;
  431. }
  432. if(left_type[1]){
  433. h->non_zero_count_cache[3+8*3]= h->non_zero_count[left_xy[1]][4];
  434. h->non_zero_count_cache[3+8*4]= h->non_zero_count[left_xy[1]][3];
  435. h->non_zero_count_cache[0+8*2]= h->non_zero_count[left_xy[1]][8];
  436. h->non_zero_count_cache[0+8*5]= h->non_zero_count[left_xy[1]][11];
  437. }else{
  438. h->non_zero_count_cache[3+8*3]=
  439. h->non_zero_count_cache[3+8*4]=
  440. h->non_zero_count_cache[0+8*2]=
  441. h->non_zero_count_cache[0+8*5]= 64;
  442. }
  443. #if 1
  444. if(IS_INTER(mb_type)){
  445. int list;
  446. for(list=0; list<2; list++){
  447. if((!IS_8X8(mb_type)) && !USES_LIST(mb_type, list)){
  448. /*if(!h->mv_cache_clean[list]){
  449. memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
  450. memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
  451. h->mv_cache_clean[list]= 1;
  452. }*/
  453. continue; //FIXME direct mode ...
  454. }
  455. h->mv_cache_clean[list]= 0;
  456. if(IS_INTER(topleft_type)){
  457. const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
  458. const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + h->b8_stride;
  459. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  460. h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  461. }else{
  462. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
  463. h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  464. }
  465. if(IS_INTER(top_type)){
  466. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  467. const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
  468. *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
  469. *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
  470. *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
  471. *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
  472. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  473. h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
  474. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  475. h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
  476. }else{
  477. *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
  478. *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
  479. *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
  480. *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
  481. *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
  482. }
  483. if(IS_INTER(topright_type)){
  484. const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
  485. const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
  486. *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  487. h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  488. }else{
  489. *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
  490. h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  491. }
  492. //FIXME unify cleanup or sth
  493. if(IS_INTER(left_type[0])){
  494. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  495. const int b8_xy= h->mb2b8_xy[left_xy[0]] + 1;
  496. *(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]];
  497. *(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]];
  498. h->ref_cache[list][scan8[0] - 1 + 0*8]=
  499. h->ref_cache[list][scan8[0] - 1 + 1*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0]>>1)];
  500. }else{
  501. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 0*8]=
  502. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 1*8]= 0;
  503. h->ref_cache[list][scan8[0] - 1 + 0*8]=
  504. h->ref_cache[list][scan8[0] - 1 + 1*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  505. }
  506. if(IS_INTER(left_type[1])){
  507. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  508. const int b8_xy= h->mb2b8_xy[left_xy[1]] + 1;
  509. *(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]];
  510. *(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]];
  511. h->ref_cache[list][scan8[0] - 1 + 2*8]=
  512. h->ref_cache[list][scan8[0] - 1 + 3*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[2]>>1)];
  513. }else{
  514. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 2*8]=
  515. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 3*8]= 0;
  516. h->ref_cache[list][scan8[0] - 1 + 2*8]=
  517. h->ref_cache[list][scan8[0] - 1 + 3*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  518. }
  519. h->ref_cache[list][scan8[5 ]+1] =
  520. h->ref_cache[list][scan8[7 ]+1] =
  521. h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewher else)
  522. h->ref_cache[list][scan8[4 ]] =
  523. h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
  524. *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
  525. *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
  526. *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewher else)
  527. *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
  528. *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
  529. }
  530. //FIXME
  531. }
  532. #endif
  533. }
  534. static inline void write_back_intra_pred_mode(H264Context *h){
  535. MpegEncContext * const s = &h->s;
  536. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  537. h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
  538. h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
  539. h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
  540. h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
  541. h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
  542. h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
  543. h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
  544. }
  545. /**
  546. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  547. */
  548. static inline int check_intra4x4_pred_mode(H264Context *h){
  549. MpegEncContext * const s = &h->s;
  550. static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
  551. static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
  552. int i;
  553. if(!(h->top_samples_available&0x8000)){
  554. for(i=0; i<4; i++){
  555. int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
  556. if(status<0){
  557. fprintf(stderr, "top block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
  558. return -1;
  559. } else if(status){
  560. h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
  561. }
  562. }
  563. }
  564. if(!(h->left_samples_available&0x8000)){
  565. for(i=0; i<4; i++){
  566. int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
  567. if(status<0){
  568. fprintf(stderr, "left block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
  569. return -1;
  570. } else if(status){
  571. h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
  572. }
  573. }
  574. }
  575. return 0;
  576. } //FIXME cleanup like next
  577. /**
  578. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  579. */
  580. static inline int check_intra_pred_mode(H264Context *h, int mode){
  581. MpegEncContext * const s = &h->s;
  582. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  583. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  584. if(!(h->top_samples_available&0x8000)){
  585. mode= top[ mode ];
  586. if(mode<0){
  587. fprintf(stderr, "top block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
  588. return -1;
  589. }
  590. }
  591. if(!(h->left_samples_available&0x8000)){
  592. mode= left[ mode ];
  593. if(mode<0){
  594. fprintf(stderr, "left block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
  595. return -1;
  596. }
  597. }
  598. return mode;
  599. }
  600. /**
  601. * gets the predicted intra4x4 prediction mode.
  602. */
  603. static inline int pred_intra_mode(H264Context *h, int n){
  604. const int index8= scan8[n];
  605. const int left= h->intra4x4_pred_mode_cache[index8 - 1];
  606. const int top = h->intra4x4_pred_mode_cache[index8 - 8];
  607. const int min= FFMIN(left, top);
  608. tprintf("mode:%d %d min:%d\n", left ,top, min);
  609. if(min<0) return DC_PRED;
  610. else return min;
  611. }
  612. static inline void write_back_non_zero_count(H264Context *h){
  613. MpegEncContext * const s = &h->s;
  614. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  615. h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[4+8*4];
  616. h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[5+8*4];
  617. h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[6+8*4];
  618. h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
  619. h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[7+8*3];
  620. h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[7+8*2];
  621. h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[7+8*1];
  622. h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[1+8*2];
  623. h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
  624. h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[2+8*1];
  625. h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[1+8*5];
  626. h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
  627. h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[2+8*4];
  628. }
  629. /**
  630. * gets the predicted number of non zero coefficients.
  631. * @param n block index
  632. */
  633. static inline int pred_non_zero_count(H264Context *h, int n){
  634. const int index8= scan8[n];
  635. const int left= h->non_zero_count_cache[index8 - 1];
  636. const int top = h->non_zero_count_cache[index8 - 8];
  637. int i= left + top;
  638. if(i<64) i= (i+1)>>1;
  639. tprintf("pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
  640. return i&31;
  641. }
  642. static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
  643. const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
  644. if(topright_ref != PART_NOT_AVAILABLE){
  645. *C= h->mv_cache[list][ i - 8 + part_width ];
  646. return topright_ref;
  647. }else{
  648. tprintf("topright MV not available\n");
  649. *C= h->mv_cache[list][ i - 8 - 1 ];
  650. return h->ref_cache[list][ i - 8 - 1 ];
  651. }
  652. }
  653. /**
  654. * gets the predicted MV.
  655. * @param n the block index
  656. * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
  657. * @param mx the x component of the predicted motion vector
  658. * @param my the y component of the predicted motion vector
  659. */
  660. static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
  661. const int index8= scan8[n];
  662. const int top_ref= h->ref_cache[list][ index8 - 8 ];
  663. const int left_ref= h->ref_cache[list][ index8 - 1 ];
  664. const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
  665. const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
  666. const int16_t * C;
  667. int diagonal_ref, match_count;
  668. assert(part_width==1 || part_width==2 || part_width==4);
  669. /* mv_cache
  670. B . . A T T T T
  671. U . . L . . , .
  672. U . . L . . . .
  673. U . . L . . , .
  674. . . . L . . . .
  675. */
  676. diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
  677. match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
  678. if(match_count > 1){ //most common
  679. *mx= mid_pred(A[0], B[0], C[0]);
  680. *my= mid_pred(A[1], B[1], C[1]);
  681. }else if(match_count==1){
  682. if(left_ref==ref){
  683. *mx= A[0];
  684. *my= A[1];
  685. }else if(top_ref==ref){
  686. *mx= B[0];
  687. *my= B[1];
  688. }else{
  689. *mx= C[0];
  690. *my= C[1];
  691. }
  692. }else{
  693. if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
  694. *mx= A[0];
  695. *my= A[1];
  696. }else{
  697. *mx= mid_pred(A[0], B[0], C[0]);
  698. *my= mid_pred(A[1], B[1], C[1]);
  699. }
  700. }
  701. 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);
  702. }
  703. /**
  704. * gets the directionally predicted 16x8 MV.
  705. * @param n the block index
  706. * @param mx the x component of the predicted motion vector
  707. * @param my the y component of the predicted motion vector
  708. */
  709. static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  710. if(n==0){
  711. const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
  712. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  713. 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);
  714. if(top_ref == ref){
  715. *mx= B[0];
  716. *my= B[1];
  717. return;
  718. }
  719. }else{
  720. const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
  721. const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
  722. 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);
  723. if(left_ref == ref){
  724. *mx= A[0];
  725. *my= A[1];
  726. return;
  727. }
  728. }
  729. //RARE
  730. pred_motion(h, n, 4, list, ref, mx, my);
  731. }
  732. /**
  733. * gets the directionally predicted 8x16 MV.
  734. * @param n the block index
  735. * @param mx the x component of the predicted motion vector
  736. * @param my the y component of the predicted motion vector
  737. */
  738. static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  739. if(n==0){
  740. const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
  741. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  742. 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);
  743. if(left_ref == ref){
  744. *mx= A[0];
  745. *my= A[1];
  746. return;
  747. }
  748. }else{
  749. const int16_t * C;
  750. int diagonal_ref;
  751. diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
  752. 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);
  753. if(diagonal_ref == ref){
  754. *mx= C[0];
  755. *my= C[1];
  756. return;
  757. }
  758. }
  759. //RARE
  760. pred_motion(h, n, 2, list, ref, mx, my);
  761. }
  762. static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
  763. const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
  764. const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
  765. tprintf("pred_pskip: (%d) (%d) at %2d %2d", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
  766. if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
  767. || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
  768. || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
  769. *mx = *my = 0;
  770. return;
  771. }
  772. pred_motion(h, 0, 4, 0, 0, mx, my);
  773. return;
  774. }
  775. static inline void write_back_motion(H264Context *h, int mb_type){
  776. MpegEncContext * const s = &h->s;
  777. const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  778. const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  779. int list;
  780. for(list=0; list<2; list++){
  781. int y;
  782. if((!IS_8X8(mb_type)) && !USES_LIST(mb_type, list)){
  783. if(1){ //FIXME skip or never read if mb_type doesnt use it
  784. for(y=0; y<4; y++){
  785. *(uint64_t*)s->current_picture.motion_val[list][b_xy + 0 + y*h->b_stride]=
  786. *(uint64_t*)s->current_picture.motion_val[list][b_xy + 2 + y*h->b_stride]= 0;
  787. }
  788. for(y=0; y<2; y++){
  789. *(uint16_t*)s->current_picture.motion_val[list][b8_xy + y*h->b8_stride]= (LIST_NOT_USED&0xFF)*0x0101;
  790. }
  791. }
  792. continue; //FIXME direct mode ...
  793. }
  794. for(y=0; y<4; y++){
  795. *(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];
  796. *(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];
  797. }
  798. for(y=0; y<2; y++){
  799. s->current_picture.ref_index[list][b8_xy + 0 + y*h->b8_stride]= h->ref_cache[list][scan8[0]+0 + 16*y];
  800. s->current_picture.ref_index[list][b8_xy + 1 + y*h->b8_stride]= h->ref_cache[list][scan8[0]+2 + 16*y];
  801. }
  802. }
  803. }
  804. /**
  805. * Decodes a network abstraction layer unit.
  806. * @param consumed is the number of bytes used as input
  807. * @param length is the length of the array
  808. * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp ttailing?
  809. * @returns decoded bytes, might be src+1 if no escapes
  810. */
  811. static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){
  812. int i, si, di;
  813. uint8_t *dst;
  814. // src[0]&0x80; //forbidden bit
  815. h->nal_ref_idc= src[0]>>5;
  816. h->nal_unit_type= src[0]&0x1F;
  817. src++; length--;
  818. #if 0
  819. for(i=0; i<length; i++)
  820. printf("%2X ", src[i]);
  821. #endif
  822. for(i=0; i+1<length; i+=2){
  823. if(src[i]) continue;
  824. if(i>0 && src[i-1]==0) i--;
  825. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  826. if(src[i+2]!=3){
  827. /* startcode, so we must be past the end */
  828. length=i;
  829. }
  830. break;
  831. }
  832. }
  833. if(i>=length-1){ //no escaped 0
  834. *dst_length= length;
  835. *consumed= length+1; //+1 for the header
  836. return src;
  837. }
  838. h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length);
  839. dst= h->rbsp_buffer;
  840. //printf("deoding esc\n");
  841. si=di=0;
  842. while(si<length){
  843. //remove escapes (very rare 1:2^22)
  844. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  845. if(src[si+2]==3){ //escape
  846. dst[di++]= 0;
  847. dst[di++]= 0;
  848. si+=3;
  849. }else //next start code
  850. break;
  851. }
  852. dst[di++]= src[si++];
  853. }
  854. *dst_length= di;
  855. *consumed= si + 1;//+1 for the header
  856. //FIXME store exact number of bits in the getbitcontext (its needed for decoding)
  857. return dst;
  858. }
  859. /**
  860. * @param src the data which should be escaped
  861. * @param dst the target buffer, dst+1 == src is allowed as a special case
  862. * @param length the length of the src data
  863. * @param dst_length the length of the dst array
  864. * @returns length of escaped data in bytes or -1 if an error occured
  865. */
  866. static int encode_nal(H264Context *h, uint8_t *dst, uint8_t *src, int length, int dst_length){
  867. int i, escape_count, si, di;
  868. uint8_t *temp;
  869. assert(length>=0);
  870. assert(dst_length>0);
  871. dst[0]= (h->nal_ref_idc<<5) + h->nal_unit_type;
  872. if(length==0) return 1;
  873. escape_count= 0;
  874. for(i=0; i<length; i+=2){
  875. if(src[i]) continue;
  876. if(i>0 && src[i-1]==0)
  877. i--;
  878. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  879. escape_count++;
  880. i+=2;
  881. }
  882. }
  883. if(escape_count==0){
  884. if(dst+1 != src)
  885. memcpy(dst+1, src, length);
  886. return length + 1;
  887. }
  888. if(length + escape_count + 1> dst_length)
  889. return -1;
  890. //this should be damn rare (hopefully)
  891. h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length + escape_count);
  892. temp= h->rbsp_buffer;
  893. //printf("encoding esc\n");
  894. si= 0;
  895. di= 0;
  896. while(si < length){
  897. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  898. temp[di++]= 0; si++;
  899. temp[di++]= 0; si++;
  900. temp[di++]= 3;
  901. temp[di++]= src[si++];
  902. }
  903. else
  904. temp[di++]= src[si++];
  905. }
  906. memcpy(dst+1, temp, length+escape_count);
  907. assert(di == length+escape_count);
  908. return di + 1;
  909. }
  910. /**
  911. * write 1,10,100,1000,... for alignment, yes its exactly inverse to mpeg4
  912. */
  913. static void encode_rbsp_trailing(PutBitContext *pb){
  914. int length;
  915. put_bits(pb, 1, 1);
  916. length= (-get_bit_count(pb))&7;
  917. if(length) put_bits(pb, length, 0);
  918. }
  919. /**
  920. * identifies the exact end of the bitstream
  921. * @return the length of the trailing, or 0 if damaged
  922. */
  923. static int decode_rbsp_trailing(uint8_t *src){
  924. int v= *src;
  925. int r;
  926. tprintf("rbsp trailing %X\n", v);
  927. for(r=1; r<9; r++){
  928. if(v&1) return r;
  929. v>>=1;
  930. }
  931. return 0;
  932. }
  933. /**
  934. * idct tranforms the 16 dc values and dequantize them.
  935. * @param qp quantization parameter
  936. */
  937. static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp){
  938. const int qmul= dequant_coeff[qp][0];
  939. #define stride 16
  940. int i;
  941. int temp[16]; //FIXME check if this is a good idea
  942. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  943. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  944. //memset(block, 64, 2*256);
  945. //return;
  946. for(i=0; i<4; i++){
  947. const int offset= y_offset[i];
  948. const int z0= block[offset+stride*0] + block[offset+stride*4];
  949. const int z1= block[offset+stride*0] - block[offset+stride*4];
  950. const int z2= block[offset+stride*1] - block[offset+stride*5];
  951. const int z3= block[offset+stride*1] + block[offset+stride*5];
  952. temp[4*i+0]= z0+z3;
  953. temp[4*i+1]= z1+z2;
  954. temp[4*i+2]= z1-z2;
  955. temp[4*i+3]= z0-z3;
  956. }
  957. for(i=0; i<4; i++){
  958. const int offset= x_offset[i];
  959. const int z0= temp[4*0+i] + temp[4*2+i];
  960. const int z1= temp[4*0+i] - temp[4*2+i];
  961. const int z2= temp[4*1+i] - temp[4*3+i];
  962. const int z3= temp[4*1+i] + temp[4*3+i];
  963. block[stride*0 +offset]= ((z0 + z3)*qmul + 2)>>2; //FIXME think about merging this into decode_resdual
  964. block[stride*2 +offset]= ((z1 + z2)*qmul + 2)>>2;
  965. block[stride*8 +offset]= ((z1 - z2)*qmul + 2)>>2;
  966. block[stride*10+offset]= ((z0 - z3)*qmul + 2)>>2;
  967. }
  968. }
  969. /**
  970. * dct tranforms the 16 dc values.
  971. * @param qp quantization parameter ??? FIXME
  972. */
  973. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  974. // const int qmul= dequant_coeff[qp][0];
  975. int i;
  976. int temp[16]; //FIXME check if this is a good idea
  977. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  978. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  979. for(i=0; i<4; i++){
  980. const int offset= y_offset[i];
  981. const int z0= block[offset+stride*0] + block[offset+stride*4];
  982. const int z1= block[offset+stride*0] - block[offset+stride*4];
  983. const int z2= block[offset+stride*1] - block[offset+stride*5];
  984. const int z3= block[offset+stride*1] + block[offset+stride*5];
  985. temp[4*i+0]= z0+z3;
  986. temp[4*i+1]= z1+z2;
  987. temp[4*i+2]= z1-z2;
  988. temp[4*i+3]= z0-z3;
  989. }
  990. for(i=0; i<4; i++){
  991. const int offset= x_offset[i];
  992. const int z0= temp[4*0+i] + temp[4*2+i];
  993. const int z1= temp[4*0+i] - temp[4*2+i];
  994. const int z2= temp[4*1+i] - temp[4*3+i];
  995. const int z3= temp[4*1+i] + temp[4*3+i];
  996. block[stride*0 +offset]= (z0 + z3)>>1;
  997. block[stride*2 +offset]= (z1 + z2)>>1;
  998. block[stride*8 +offset]= (z1 - z2)>>1;
  999. block[stride*10+offset]= (z0 - z3)>>1;
  1000. }
  1001. }
  1002. #undef xStride
  1003. #undef stride
  1004. static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp){
  1005. const int qmul= dequant_coeff[qp][0];
  1006. const int stride= 16*2;
  1007. const int xStride= 16;
  1008. int a,b,c,d,e;
  1009. a= block[stride*0 + xStride*0];
  1010. b= block[stride*0 + xStride*1];
  1011. c= block[stride*1 + xStride*0];
  1012. d= block[stride*1 + xStride*1];
  1013. e= a-b;
  1014. a= a+b;
  1015. b= c-d;
  1016. c= c+d;
  1017. block[stride*0 + xStride*0]= ((a+c)*qmul + 0)>>1;
  1018. block[stride*0 + xStride*1]= ((e+b)*qmul + 0)>>1;
  1019. block[stride*1 + xStride*0]= ((a-c)*qmul + 0)>>1;
  1020. block[stride*1 + xStride*1]= ((e-b)*qmul + 0)>>1;
  1021. }
  1022. static void chroma_dc_dct_c(DCTELEM *block){
  1023. const int stride= 16*2;
  1024. const int xStride= 16;
  1025. int a,b,c,d,e;
  1026. a= block[stride*0 + xStride*0];
  1027. b= block[stride*0 + xStride*1];
  1028. c= block[stride*1 + xStride*0];
  1029. d= block[stride*1 + xStride*1];
  1030. e= a-b;
  1031. a= a+b;
  1032. b= c-d;
  1033. c= c+d;
  1034. block[stride*0 + xStride*0]= (a+c);
  1035. block[stride*0 + xStride*1]= (e+b);
  1036. block[stride*1 + xStride*0]= (a-c);
  1037. block[stride*1 + xStride*1]= (e-b);
  1038. }
  1039. /**
  1040. * gets the chroma qp.
  1041. */
  1042. static inline int get_chroma_qp(H264Context *h, int qscale){
  1043. return chroma_qp[clip(qscale + h->pps.chroma_qp_index_offset, 0, 51)];
  1044. }
  1045. /**
  1046. *
  1047. */
  1048. static void h264_add_idct_c(uint8_t *dst, DCTELEM *block, int stride){
  1049. int i;
  1050. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  1051. block[0] += 32;
  1052. #if 1
  1053. for(i=0; i<4; i++){
  1054. const int z0= block[i + 4*0] + block[i + 4*2];
  1055. const int z1= block[i + 4*0] - block[i + 4*2];
  1056. const int z2= (block[i + 4*1]>>1) - block[i + 4*3];
  1057. const int z3= block[i + 4*1] + (block[i + 4*3]>>1);
  1058. block[i + 4*0]= z0 + z3;
  1059. block[i + 4*1]= z1 + z2;
  1060. block[i + 4*2]= z1 - z2;
  1061. block[i + 4*3]= z0 - z3;
  1062. }
  1063. for(i=0; i<4; i++){
  1064. const int z0= block[0 + 4*i] + block[2 + 4*i];
  1065. const int z1= block[0 + 4*i] - block[2 + 4*i];
  1066. const int z2= (block[1 + 4*i]>>1) - block[3 + 4*i];
  1067. const int z3= block[1 + 4*i] + (block[3 + 4*i]>>1);
  1068. dst[0 + i*stride]= cm[ dst[0 + i*stride] + ((z0 + z3) >> 6) ];
  1069. dst[1 + i*stride]= cm[ dst[1 + i*stride] + ((z1 + z2) >> 6) ];
  1070. dst[2 + i*stride]= cm[ dst[2 + i*stride] + ((z1 - z2) >> 6) ];
  1071. dst[3 + i*stride]= cm[ dst[3 + i*stride] + ((z0 - z3) >> 6) ];
  1072. }
  1073. #else
  1074. for(i=0; i<4; i++){
  1075. const int z0= block[0 + 4*i] + block[2 + 4*i];
  1076. const int z1= block[0 + 4*i] - block[2 + 4*i];
  1077. const int z2= (block[1 + 4*i]>>1) - block[3 + 4*i];
  1078. const int z3= block[1 + 4*i] + (block[3 + 4*i]>>1);
  1079. block[0 + 4*i]= z0 + z3;
  1080. block[1 + 4*i]= z1 + z2;
  1081. block[2 + 4*i]= z1 - z2;
  1082. block[3 + 4*i]= z0 - z3;
  1083. }
  1084. for(i=0; i<4; i++){
  1085. const int z0= block[i + 4*0] + block[i + 4*2];
  1086. const int z1= block[i + 4*0] - block[i + 4*2];
  1087. const int z2= (block[i + 4*1]>>1) - block[i + 4*3];
  1088. const int z3= block[i + 4*1] + (block[i + 4*3]>>1);
  1089. dst[i + 0*stride]= cm[ dst[i + 0*stride] + ((z0 + z3) >> 6) ];
  1090. dst[i + 1*stride]= cm[ dst[i + 1*stride] + ((z1 + z2) >> 6) ];
  1091. dst[i + 2*stride]= cm[ dst[i + 2*stride] + ((z1 - z2) >> 6) ];
  1092. dst[i + 3*stride]= cm[ dst[i + 3*stride] + ((z0 - z3) >> 6) ];
  1093. }
  1094. #endif
  1095. }
  1096. static void h264_diff_dct_c(DCTELEM *block, uint8_t *src1, uint8_t *src2, int stride){
  1097. int i;
  1098. //FIXME try int temp instead of block
  1099. for(i=0; i<4; i++){
  1100. const int d0= src1[0 + i*stride] - src2[0 + i*stride];
  1101. const int d1= src1[1 + i*stride] - src2[1 + i*stride];
  1102. const int d2= src1[2 + i*stride] - src2[2 + i*stride];
  1103. const int d3= src1[3 + i*stride] - src2[3 + i*stride];
  1104. const int z0= d0 + d3;
  1105. const int z3= d0 - d3;
  1106. const int z1= d1 + d2;
  1107. const int z2= d1 - d2;
  1108. block[0 + 4*i]= z0 + z1;
  1109. block[1 + 4*i]= 2*z3 + z2;
  1110. block[2 + 4*i]= z0 - z1;
  1111. block[3 + 4*i]= z3 - 2*z2;
  1112. }
  1113. for(i=0; i<4; i++){
  1114. const int z0= block[0*4 + i] + block[3*4 + i];
  1115. const int z3= block[0*4 + i] - block[3*4 + i];
  1116. const int z1= block[1*4 + i] + block[2*4 + i];
  1117. const int z2= block[1*4 + i] - block[2*4 + i];
  1118. block[0*4 + i]= z0 + z1;
  1119. block[1*4 + i]= 2*z3 + z2;
  1120. block[2*4 + i]= z0 - z1;
  1121. block[3*4 + i]= z3 - 2*z2;
  1122. }
  1123. }
  1124. //FIXME need to check that this doesnt overflow signed 32 bit for low qp, iam not sure, its very close
  1125. //FIXME check that gcc inlines this (and optimizes intra & seperate_dc stuff away)
  1126. static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int seperate_dc){
  1127. int i;
  1128. const int * const quant_table= quant_coeff[qscale];
  1129. const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
  1130. const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
  1131. const unsigned int threshold2= (threshold1<<1);
  1132. int last_non_zero;
  1133. if(seperate_dc){
  1134. if(qscale<=18){
  1135. //avoid overflows
  1136. const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
  1137. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
  1138. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1139. int level= block[0]*quant_coeff[qscale+18][0];
  1140. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1141. if(level>0){
  1142. level= (dc_bias + level)>>(QUANT_SHIFT-2);
  1143. block[0]= level;
  1144. }else{
  1145. level= (dc_bias - level)>>(QUANT_SHIFT-2);
  1146. block[0]= -level;
  1147. }
  1148. // last_non_zero = i;
  1149. }else{
  1150. block[0]=0;
  1151. }
  1152. }else{
  1153. const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
  1154. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
  1155. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1156. int level= block[0]*quant_table[0];
  1157. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1158. if(level>0){
  1159. level= (dc_bias + level)>>(QUANT_SHIFT+1);
  1160. block[0]= level;
  1161. }else{
  1162. level= (dc_bias - level)>>(QUANT_SHIFT+1);
  1163. block[0]= -level;
  1164. }
  1165. // last_non_zero = i;
  1166. }else{
  1167. block[0]=0;
  1168. }
  1169. }
  1170. last_non_zero= 0;
  1171. i=1;
  1172. }else{
  1173. last_non_zero= -1;
  1174. i=0;
  1175. }
  1176. for(; i<16; i++){
  1177. const int j= scantable[i];
  1178. int level= block[j]*quant_table[j];
  1179. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  1180. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  1181. if(((unsigned)(level+threshold1))>threshold2){
  1182. if(level>0){
  1183. level= (bias + level)>>QUANT_SHIFT;
  1184. block[j]= level;
  1185. }else{
  1186. level= (bias - level)>>QUANT_SHIFT;
  1187. block[j]= -level;
  1188. }
  1189. last_non_zero = i;
  1190. }else{
  1191. block[j]=0;
  1192. }
  1193. }
  1194. return last_non_zero;
  1195. }
  1196. static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){
  1197. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1198. ((uint32_t*)(src+0*stride))[0]= a;
  1199. ((uint32_t*)(src+1*stride))[0]= a;
  1200. ((uint32_t*)(src+2*stride))[0]= a;
  1201. ((uint32_t*)(src+3*stride))[0]= a;
  1202. }
  1203. static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){
  1204. ((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101;
  1205. ((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101;
  1206. ((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101;
  1207. ((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101;
  1208. }
  1209. static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1210. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
  1211. + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
  1212. ((uint32_t*)(src+0*stride))[0]=
  1213. ((uint32_t*)(src+1*stride))[0]=
  1214. ((uint32_t*)(src+2*stride))[0]=
  1215. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1216. }
  1217. static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1218. const int dc= ( src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
  1219. ((uint32_t*)(src+0*stride))[0]=
  1220. ((uint32_t*)(src+1*stride))[0]=
  1221. ((uint32_t*)(src+2*stride))[0]=
  1222. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1223. }
  1224. static void pred4x4_top_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1225. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2;
  1226. ((uint32_t*)(src+0*stride))[0]=
  1227. ((uint32_t*)(src+1*stride))[0]=
  1228. ((uint32_t*)(src+2*stride))[0]=
  1229. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1230. }
  1231. static void pred4x4_128_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1232. ((uint32_t*)(src+0*stride))[0]=
  1233. ((uint32_t*)(src+1*stride))[0]=
  1234. ((uint32_t*)(src+2*stride))[0]=
  1235. ((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U;
  1236. }
  1237. #define LOAD_TOP_RIGHT_EDGE\
  1238. const int t4= topright[0];\
  1239. const int t5= topright[1];\
  1240. const int t6= topright[2];\
  1241. const int t7= topright[3];\
  1242. #define LOAD_LEFT_EDGE\
  1243. const int l0= src[-1+0*stride];\
  1244. const int l1= src[-1+1*stride];\
  1245. const int l2= src[-1+2*stride];\
  1246. const int l3= src[-1+3*stride];\
  1247. #define LOAD_TOP_EDGE\
  1248. const int t0= src[ 0-1*stride];\
  1249. const int t1= src[ 1-1*stride];\
  1250. const int t2= src[ 2-1*stride];\
  1251. const int t3= src[ 3-1*stride];\
  1252. static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){
  1253. const int lt= src[-1-1*stride];
  1254. LOAD_TOP_EDGE
  1255. LOAD_LEFT_EDGE
  1256. src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
  1257. src[0+2*stride]=
  1258. src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
  1259. src[0+1*stride]=
  1260. src[1+2*stride]=
  1261. src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
  1262. src[0+0*stride]=
  1263. src[1+1*stride]=
  1264. src[2+2*stride]=
  1265. src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1266. src[1+0*stride]=
  1267. src[2+1*stride]=
  1268. src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1269. src[2+0*stride]=
  1270. src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1271. src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1272. };
  1273. static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){
  1274. LOAD_TOP_EDGE
  1275. LOAD_TOP_RIGHT_EDGE
  1276. // LOAD_LEFT_EDGE
  1277. src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
  1278. src[1+0*stride]=
  1279. src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
  1280. src[2+0*stride]=
  1281. src[1+1*stride]=
  1282. src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
  1283. src[3+0*stride]=
  1284. src[2+1*stride]=
  1285. src[1+2*stride]=
  1286. src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
  1287. src[3+1*stride]=
  1288. src[2+2*stride]=
  1289. src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
  1290. src[3+2*stride]=
  1291. src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
  1292. src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
  1293. };
  1294. static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
  1295. const int lt= src[-1-1*stride];
  1296. LOAD_TOP_EDGE
  1297. LOAD_LEFT_EDGE
  1298. const __attribute__((unused)) int unu= l3;
  1299. src[0+0*stride]=
  1300. src[1+2*stride]=(lt + t0 + 1)>>1;
  1301. src[1+0*stride]=
  1302. src[2+2*stride]=(t0 + t1 + 1)>>1;
  1303. src[2+0*stride]=
  1304. src[3+2*stride]=(t1 + t2 + 1)>>1;
  1305. src[3+0*stride]=(t2 + t3 + 1)>>1;
  1306. src[0+1*stride]=
  1307. src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1308. src[1+1*stride]=
  1309. src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1310. src[2+1*stride]=
  1311. src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1312. src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1313. src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1314. src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1315. };
  1316. static void pred4x4_vertical_left_c(uint8_t *src, uint8_t *topright, int stride){
  1317. LOAD_TOP_EDGE
  1318. LOAD_TOP_RIGHT_EDGE
  1319. const __attribute__((unused)) int unu= t7;
  1320. src[0+0*stride]=(t0 + t1 + 1)>>1;
  1321. src[1+0*stride]=
  1322. src[0+2*stride]=(t1 + t2 + 1)>>1;
  1323. src[2+0*stride]=
  1324. src[1+2*stride]=(t2 + t3 + 1)>>1;
  1325. src[3+0*stride]=
  1326. src[2+2*stride]=(t3 + t4+ 1)>>1;
  1327. src[3+2*stride]=(t4 + t5+ 1)>>1;
  1328. src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1329. src[1+1*stride]=
  1330. src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1331. src[2+1*stride]=
  1332. src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
  1333. src[3+1*stride]=
  1334. src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
  1335. src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
  1336. };
  1337. static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){
  1338. LOAD_LEFT_EDGE
  1339. src[0+0*stride]=(l0 + l1 + 1)>>1;
  1340. src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1341. src[2+0*stride]=
  1342. src[0+1*stride]=(l1 + l2 + 1)>>1;
  1343. src[3+0*stride]=
  1344. src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1345. src[2+1*stride]=
  1346. src[0+2*stride]=(l2 + l3 + 1)>>1;
  1347. src[3+1*stride]=
  1348. src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
  1349. src[3+2*stride]=
  1350. src[1+3*stride]=
  1351. src[0+3*stride]=
  1352. src[2+2*stride]=
  1353. src[2+3*stride]=
  1354. src[3+3*stride]=l3;
  1355. };
  1356. static void pred4x4_horizontal_down_c(uint8_t *src, uint8_t *topright, int stride){
  1357. const int lt= src[-1-1*stride];
  1358. LOAD_TOP_EDGE
  1359. LOAD_LEFT_EDGE
  1360. const __attribute__((unused)) int unu= t3;
  1361. src[0+0*stride]=
  1362. src[2+1*stride]=(lt + l0 + 1)>>1;
  1363. src[1+0*stride]=
  1364. src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1365. src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1366. src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1367. src[0+1*stride]=
  1368. src[2+2*stride]=(l0 + l1 + 1)>>1;
  1369. src[1+1*stride]=
  1370. src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1371. src[0+2*stride]=
  1372. src[2+3*stride]=(l1 + l2+ 1)>>1;
  1373. src[1+2*stride]=
  1374. src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1375. src[0+3*stride]=(l2 + l3 + 1)>>1;
  1376. src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1377. };
  1378. static void pred16x16_vertical_c(uint8_t *src, int stride){
  1379. int i;
  1380. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1381. const uint32_t b= ((uint32_t*)(src-stride))[1];
  1382. const uint32_t c= ((uint32_t*)(src-stride))[2];
  1383. const uint32_t d= ((uint32_t*)(src-stride))[3];
  1384. for(i=0; i<16; i++){
  1385. ((uint32_t*)(src+i*stride))[0]= a;
  1386. ((uint32_t*)(src+i*stride))[1]= b;
  1387. ((uint32_t*)(src+i*stride))[2]= c;
  1388. ((uint32_t*)(src+i*stride))[3]= d;
  1389. }
  1390. }
  1391. static void pred16x16_horizontal_c(uint8_t *src, int stride){
  1392. int i;
  1393. for(i=0; i<16; i++){
  1394. ((uint32_t*)(src+i*stride))[0]=
  1395. ((uint32_t*)(src+i*stride))[1]=
  1396. ((uint32_t*)(src+i*stride))[2]=
  1397. ((uint32_t*)(src+i*stride))[3]= src[-1+i*stride]*0x01010101;
  1398. }
  1399. }
  1400. static void pred16x16_dc_c(uint8_t *src, int stride){
  1401. int i, dc=0;
  1402. for(i=0;i<16; i++){
  1403. dc+= src[-1+i*stride];
  1404. }
  1405. for(i=0;i<16; i++){
  1406. dc+= src[i-stride];
  1407. }
  1408. dc= 0x01010101*((dc + 16)>>5);
  1409. for(i=0; i<16; i++){
  1410. ((uint32_t*)(src+i*stride))[0]=
  1411. ((uint32_t*)(src+i*stride))[1]=
  1412. ((uint32_t*)(src+i*stride))[2]=
  1413. ((uint32_t*)(src+i*stride))[3]= dc;
  1414. }
  1415. }
  1416. static void pred16x16_left_dc_c(uint8_t *src, int stride){
  1417. int i, dc=0;
  1418. for(i=0;i<16; i++){
  1419. dc+= src[-1+i*stride];
  1420. }
  1421. dc= 0x01010101*((dc + 8)>>4);
  1422. for(i=0; i<16; i++){
  1423. ((uint32_t*)(src+i*stride))[0]=
  1424. ((uint32_t*)(src+i*stride))[1]=
  1425. ((uint32_t*)(src+i*stride))[2]=
  1426. ((uint32_t*)(src+i*stride))[3]= dc;
  1427. }
  1428. }
  1429. static void pred16x16_top_dc_c(uint8_t *src, int stride){
  1430. int i, dc=0;
  1431. for(i=0;i<16; i++){
  1432. dc+= src[i-stride];
  1433. }
  1434. dc= 0x01010101*((dc + 8)>>4);
  1435. for(i=0; i<16; i++){
  1436. ((uint32_t*)(src+i*stride))[0]=
  1437. ((uint32_t*)(src+i*stride))[1]=
  1438. ((uint32_t*)(src+i*stride))[2]=
  1439. ((uint32_t*)(src+i*stride))[3]= dc;
  1440. }
  1441. }
  1442. static void pred16x16_128_dc_c(uint8_t *src, int stride){
  1443. int i;
  1444. for(i=0; i<16; i++){
  1445. ((uint32_t*)(src+i*stride))[0]=
  1446. ((uint32_t*)(src+i*stride))[1]=
  1447. ((uint32_t*)(src+i*stride))[2]=
  1448. ((uint32_t*)(src+i*stride))[3]= 0x01010101U*128U;
  1449. }
  1450. }
  1451. static inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){
  1452. int i, j, k;
  1453. int a;
  1454. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  1455. const uint8_t * const src0 = src+7-stride;
  1456. const uint8_t *src1 = src+8*stride-1;
  1457. const uint8_t *src2 = src1-2*stride; // == src+6*stride-1;
  1458. int H = src0[1] - src0[-1];
  1459. int V = src1[0] - src2[ 0];
  1460. for(k=2; k<=8; ++k) {
  1461. src1 += stride; src2 -= stride;
  1462. H += k*(src0[k] - src0[-k]);
  1463. V += k*(src1[0] - src2[ 0]);
  1464. }
  1465. if(svq3){
  1466. H = ( 5*(H/4) ) / 16;
  1467. V = ( 5*(V/4) ) / 16;
  1468. }else{
  1469. H = ( 5*H+32 ) >> 6;
  1470. V = ( 5*V+32 ) >> 6;
  1471. }
  1472. a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
  1473. for(j=16; j>0; --j) {
  1474. int b = a;
  1475. a += V;
  1476. for(i=-16; i<0; i+=4) {
  1477. src[16+i] = cm[ (b ) >> 5 ];
  1478. src[17+i] = cm[ (b+ H) >> 5 ];
  1479. src[18+i] = cm[ (b+2*H) >> 5 ];
  1480. src[19+i] = cm[ (b+3*H) >> 5 ];
  1481. b += 4*H;
  1482. }
  1483. src += stride;
  1484. }
  1485. }
  1486. static void pred16x16_plane_c(uint8_t *src, int stride){
  1487. pred16x16_plane_compat_c(src, stride, 0);
  1488. }
  1489. static void pred8x8_vertical_c(uint8_t *src, int stride){
  1490. int i;
  1491. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1492. const uint32_t b= ((uint32_t*)(src-stride))[1];
  1493. for(i=0; i<8; i++){
  1494. ((uint32_t*)(src+i*stride))[0]= a;
  1495. ((uint32_t*)(src+i*stride))[1]= b;
  1496. }
  1497. }
  1498. static void pred8x8_horizontal_c(uint8_t *src, int stride){
  1499. int i;
  1500. for(i=0; i<8; i++){
  1501. ((uint32_t*)(src+i*stride))[0]=
  1502. ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101;
  1503. }
  1504. }
  1505. static void pred8x8_128_dc_c(uint8_t *src, int stride){
  1506. int i;
  1507. for(i=0; i<4; i++){
  1508. ((uint32_t*)(src+i*stride))[0]=
  1509. ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
  1510. }
  1511. for(i=4; i<8; i++){
  1512. ((uint32_t*)(src+i*stride))[0]=
  1513. ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
  1514. }
  1515. }
  1516. static void pred8x8_left_dc_c(uint8_t *src, int stride){
  1517. int i;
  1518. int dc0, dc2;
  1519. dc0=dc2=0;
  1520. for(i=0;i<4; i++){
  1521. dc0+= src[-1+i*stride];
  1522. dc2+= src[-1+(i+4)*stride];
  1523. }
  1524. dc0= 0x01010101*((dc0 + 2)>>2);
  1525. dc2= 0x01010101*((dc2 + 2)>>2);
  1526. for(i=0; i<4; i++){
  1527. ((uint32_t*)(src+i*stride))[0]=
  1528. ((uint32_t*)(src+i*stride))[1]= dc0;
  1529. }
  1530. for(i=4; i<8; i++){
  1531. ((uint32_t*)(src+i*stride))[0]=
  1532. ((uint32_t*)(src+i*stride))[1]= dc2;
  1533. }
  1534. }
  1535. static void pred8x8_top_dc_c(uint8_t *src, int stride){
  1536. int i;
  1537. int dc0, dc1;
  1538. dc0=dc1=0;
  1539. for(i=0;i<4; i++){
  1540. dc0+= src[i-stride];
  1541. dc1+= src[4+i-stride];
  1542. }
  1543. dc0= 0x01010101*((dc0 + 2)>>2);
  1544. dc1= 0x01010101*((dc1 + 2)>>2);
  1545. for(i=0; i<4; i++){
  1546. ((uint32_t*)(src+i*stride))[0]= dc0;
  1547. ((uint32_t*)(src+i*stride))[1]= dc1;
  1548. }
  1549. for(i=4; i<8; i++){
  1550. ((uint32_t*)(src+i*stride))[0]= dc0;
  1551. ((uint32_t*)(src+i*stride))[1]= dc1;
  1552. }
  1553. }
  1554. static void pred8x8_dc_c(uint8_t *src, int stride){
  1555. int i;
  1556. int dc0, dc1, dc2, dc3;
  1557. dc0=dc1=dc2=0;
  1558. for(i=0;i<4; i++){
  1559. dc0+= src[-1+i*stride] + src[i-stride];
  1560. dc1+= src[4+i-stride];
  1561. dc2+= src[-1+(i+4)*stride];
  1562. }
  1563. dc3= 0x01010101*((dc1 + dc2 + 4)>>3);
  1564. dc0= 0x01010101*((dc0 + 4)>>3);
  1565. dc1= 0x01010101*((dc1 + 2)>>2);
  1566. dc2= 0x01010101*((dc2 + 2)>>2);
  1567. for(i=0; i<4; i++){
  1568. ((uint32_t*)(src+i*stride))[0]= dc0;
  1569. ((uint32_t*)(src+i*stride))[1]= dc1;
  1570. }
  1571. for(i=4; i<8; i++){
  1572. ((uint32_t*)(src+i*stride))[0]= dc2;
  1573. ((uint32_t*)(src+i*stride))[1]= dc3;
  1574. }
  1575. }
  1576. static void pred8x8_plane_c(uint8_t *src, int stride){
  1577. int j, k;
  1578. int a;
  1579. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  1580. const uint8_t * const src0 = src+3-stride;
  1581. const uint8_t *src1 = src+4*stride-1;
  1582. const uint8_t *src2 = src1-2*stride; // == src+2*stride-1;
  1583. int H = src0[1] - src0[-1];
  1584. int V = src1[0] - src2[ 0];
  1585. for(k=2; k<=4; ++k) {
  1586. src1 += stride; src2 -= stride;
  1587. H += k*(src0[k] - src0[-k]);
  1588. V += k*(src1[0] - src2[ 0]);
  1589. }
  1590. H = ( 17*H+16 ) >> 5;
  1591. V = ( 17*V+16 ) >> 5;
  1592. a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
  1593. for(j=8; j>0; --j) {
  1594. int b = a;
  1595. a += V;
  1596. src[0] = cm[ (b ) >> 5 ];
  1597. src[1] = cm[ (b+ H) >> 5 ];
  1598. src[2] = cm[ (b+2*H) >> 5 ];
  1599. src[3] = cm[ (b+3*H) >> 5 ];
  1600. src[4] = cm[ (b+4*H) >> 5 ];
  1601. src[5] = cm[ (b+5*H) >> 5 ];
  1602. src[6] = cm[ (b+6*H) >> 5 ];
  1603. src[7] = cm[ (b+7*H) >> 5 ];
  1604. src += stride;
  1605. }
  1606. }
  1607. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  1608. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1609. int src_x_offset, int src_y_offset,
  1610. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  1611. MpegEncContext * const s = &h->s;
  1612. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  1613. const int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  1614. const int luma_xy= (mx&3) + ((my&3)<<2);
  1615. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*s->linesize;
  1616. uint8_t * src_cb= pic->data[1] + (mx>>3) + (my>>3)*s->uvlinesize;
  1617. uint8_t * src_cr= pic->data[2] + (mx>>3) + (my>>3)*s->uvlinesize;
  1618. int extra_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16; //FIXME increase edge?, IMHO not worth it
  1619. int extra_height= extra_width;
  1620. int emu=0;
  1621. const int full_mx= mx>>2;
  1622. const int full_my= my>>2;
  1623. assert(pic->data[0]);
  1624. if(mx&7) extra_width -= 3;
  1625. if(my&7) extra_height -= 3;
  1626. if( full_mx < 0-extra_width
  1627. || full_my < 0-extra_height
  1628. || full_mx + 16/*FIXME*/ > s->width + extra_width
  1629. || full_my + 16/*FIXME*/ > s->height + extra_height){
  1630. ff_emulated_edge_mc(s, src_y - 2 - 2*s->linesize, s->linesize, 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, s->width, s->height);
  1631. src_y= s->edge_emu_buffer + 2 + 2*s->linesize;
  1632. emu=1;
  1633. }
  1634. qpix_op[luma_xy](dest_y, src_y, s->linesize); //FIXME try variable height perhaps?
  1635. if(!square){
  1636. qpix_op[luma_xy](dest_y + delta, src_y + delta, s->linesize);
  1637. }
  1638. if(s->flags&CODEC_FLAG_GRAY) return;
  1639. if(emu){
  1640. ff_emulated_edge_mc(s, src_cb, s->uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), s->width>>1, s->height>>1);
  1641. src_cb= s->edge_emu_buffer;
  1642. }
  1643. chroma_op(dest_cb, src_cb, s->uvlinesize, chroma_height, mx&7, my&7);
  1644. if(emu){
  1645. ff_emulated_edge_mc(s, src_cr, s->uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), s->width>>1, s->height>>1);
  1646. src_cr= s->edge_emu_buffer;
  1647. }
  1648. chroma_op(dest_cr, src_cr, s->uvlinesize, chroma_height, mx&7, my&7);
  1649. }
  1650. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  1651. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1652. int x_offset, int y_offset,
  1653. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  1654. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  1655. int list0, int list1){
  1656. MpegEncContext * const s = &h->s;
  1657. qpel_mc_func *qpix_op= qpix_put;
  1658. h264_chroma_mc_func chroma_op= chroma_put;
  1659. dest_y += 2*x_offset + 2*y_offset*s-> linesize;
  1660. dest_cb += x_offset + y_offset*s->uvlinesize;
  1661. dest_cr += x_offset + y_offset*s->uvlinesize;
  1662. x_offset += 8*s->mb_x;
  1663. y_offset += 8*s->mb_y;
  1664. if(list0){
  1665. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  1666. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  1667. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1668. qpix_op, chroma_op);
  1669. qpix_op= qpix_avg;
  1670. chroma_op= chroma_avg;
  1671. }
  1672. if(list1){
  1673. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  1674. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  1675. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1676. qpix_op, chroma_op);
  1677. }
  1678. }
  1679. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1680. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  1681. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg)){
  1682. MpegEncContext * const s = &h->s;
  1683. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  1684. const int mb_type= s->current_picture.mb_type[mb_xy];
  1685. assert(IS_INTER(mb_type));
  1686. if(IS_16X16(mb_type)){
  1687. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  1688. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  1689. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  1690. }else if(IS_16X8(mb_type)){
  1691. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  1692. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  1693. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  1694. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  1695. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  1696. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  1697. }else if(IS_8X16(mb_type)){
  1698. mc_part(h, 0, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 0, 0,
  1699. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  1700. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  1701. mc_part(h, 4, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 4, 0,
  1702. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  1703. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  1704. }else{
  1705. int i;
  1706. assert(IS_8X8(mb_type));
  1707. for(i=0; i<4; i++){
  1708. const int sub_mb_type= h->sub_mb_type[i];
  1709. const int n= 4*i;
  1710. int x_offset= (i&1)<<2;
  1711. int y_offset= (i&2)<<1;
  1712. if(IS_SUB_8X8(sub_mb_type)){
  1713. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1714. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  1715. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1716. }else if(IS_SUB_8X4(sub_mb_type)){
  1717. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1718. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  1719. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1720. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  1721. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  1722. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1723. }else if(IS_SUB_4X8(sub_mb_type)){
  1724. mc_part(h, n , 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1725. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  1726. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1727. mc_part(h, n+1, 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
  1728. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  1729. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1730. }else{
  1731. int j;
  1732. assert(IS_SUB_4X4(sub_mb_type));
  1733. for(j=0; j<4; j++){
  1734. int sub_x_offset= x_offset + 2*(j&1);
  1735. int sub_y_offset= y_offset + (j&2);
  1736. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  1737. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  1738. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1739. }
  1740. }
  1741. }
  1742. }
  1743. }
  1744. static void decode_init_vlc(H264Context *h){
  1745. static int done = 0;
  1746. if (!done) {
  1747. int i;
  1748. done = 1;
  1749. init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
  1750. &chroma_dc_coeff_token_len [0], 1, 1,
  1751. &chroma_dc_coeff_token_bits[0], 1, 1);
  1752. for(i=0; i<4; i++){
  1753. init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
  1754. &coeff_token_len [i][0], 1, 1,
  1755. &coeff_token_bits[i][0], 1, 1);
  1756. }
  1757. for(i=0; i<3; i++){
  1758. init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
  1759. &chroma_dc_total_zeros_len [i][0], 1, 1,
  1760. &chroma_dc_total_zeros_bits[i][0], 1, 1);
  1761. }
  1762. for(i=0; i<15; i++){
  1763. init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
  1764. &total_zeros_len [i][0], 1, 1,
  1765. &total_zeros_bits[i][0], 1, 1);
  1766. }
  1767. for(i=0; i<6; i++){
  1768. init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
  1769. &run_len [i][0], 1, 1,
  1770. &run_bits[i][0], 1, 1);
  1771. }
  1772. init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
  1773. &run_len [6][0], 1, 1,
  1774. &run_bits[6][0], 1, 1);
  1775. }
  1776. }
  1777. /**
  1778. * Sets the intra prediction function pointers.
  1779. */
  1780. static void init_pred_ptrs(H264Context *h){
  1781. // MpegEncContext * const s = &h->s;
  1782. h->pred4x4[VERT_PRED ]= pred4x4_vertical_c;
  1783. h->pred4x4[HOR_PRED ]= pred4x4_horizontal_c;
  1784. h->pred4x4[DC_PRED ]= pred4x4_dc_c;
  1785. h->pred4x4[DIAG_DOWN_LEFT_PRED ]= pred4x4_down_left_c;
  1786. h->pred4x4[DIAG_DOWN_RIGHT_PRED]= pred4x4_down_right_c;
  1787. h->pred4x4[VERT_RIGHT_PRED ]= pred4x4_vertical_right_c;
  1788. h->pred4x4[HOR_DOWN_PRED ]= pred4x4_horizontal_down_c;
  1789. h->pred4x4[VERT_LEFT_PRED ]= pred4x4_vertical_left_c;
  1790. h->pred4x4[HOR_UP_PRED ]= pred4x4_horizontal_up_c;
  1791. h->pred4x4[LEFT_DC_PRED ]= pred4x4_left_dc_c;
  1792. h->pred4x4[TOP_DC_PRED ]= pred4x4_top_dc_c;
  1793. h->pred4x4[DC_128_PRED ]= pred4x4_128_dc_c;
  1794. h->pred8x8[DC_PRED8x8 ]= pred8x8_dc_c;
  1795. h->pred8x8[VERT_PRED8x8 ]= pred8x8_vertical_c;
  1796. h->pred8x8[HOR_PRED8x8 ]= pred8x8_horizontal_c;
  1797. h->pred8x8[PLANE_PRED8x8 ]= pred8x8_plane_c;
  1798. h->pred8x8[LEFT_DC_PRED8x8]= pred8x8_left_dc_c;
  1799. h->pred8x8[TOP_DC_PRED8x8 ]= pred8x8_top_dc_c;
  1800. h->pred8x8[DC_128_PRED8x8 ]= pred8x8_128_dc_c;
  1801. h->pred16x16[DC_PRED8x8 ]= pred16x16_dc_c;
  1802. h->pred16x16[VERT_PRED8x8 ]= pred16x16_vertical_c;
  1803. h->pred16x16[HOR_PRED8x8 ]= pred16x16_horizontal_c;
  1804. h->pred16x16[PLANE_PRED8x8 ]= pred16x16_plane_c;
  1805. h->pred16x16[LEFT_DC_PRED8x8]= pred16x16_left_dc_c;
  1806. h->pred16x16[TOP_DC_PRED8x8 ]= pred16x16_top_dc_c;
  1807. h->pred16x16[DC_128_PRED8x8 ]= pred16x16_128_dc_c;
  1808. }
  1809. //FIXME factorize
  1810. #define CHECKED_ALLOCZ(p, size)\
  1811. {\
  1812. p= av_mallocz(size);\
  1813. if(p==NULL){\
  1814. perror("malloc");\
  1815. goto fail;\
  1816. }\
  1817. }
  1818. static void free_tables(H264Context *h){
  1819. av_freep(&h->intra4x4_pred_mode);
  1820. av_freep(&h->non_zero_count);
  1821. av_freep(&h->slice_table_base);
  1822. h->slice_table= NULL;
  1823. av_freep(&h->mb2b_xy);
  1824. av_freep(&h->mb2b8_xy);
  1825. }
  1826. /**
  1827. * allocates tables.
  1828. * needs widzh/height
  1829. */
  1830. static int alloc_tables(H264Context *h){
  1831. MpegEncContext * const s = &h->s;
  1832. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  1833. int x,y;
  1834. CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
  1835. CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
  1836. CHECKED_ALLOCZ(h->slice_table_base , big_mb_num * sizeof(uint8_t))
  1837. memset(h->slice_table_base, -1, big_mb_num * sizeof(uint8_t));
  1838. h->slice_table= h->slice_table_base + s->mb_stride + 1;
  1839. CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint16_t));
  1840. CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint16_t));
  1841. for(y=0; y<s->mb_height; y++){
  1842. for(x=0; x<s->mb_width; x++){
  1843. const int mb_xy= x + y*s->mb_stride;
  1844. const int b_xy = 4*x + 4*y*h->b_stride;
  1845. const int b8_xy= 2*x + 2*y*h->b8_stride;
  1846. h->mb2b_xy [mb_xy]= b_xy;
  1847. h->mb2b8_xy[mb_xy]= b8_xy;
  1848. }
  1849. }
  1850. return 0;
  1851. fail:
  1852. free_tables(h);
  1853. return -1;
  1854. }
  1855. static void common_init(H264Context *h){
  1856. MpegEncContext * const s = &h->s;
  1857. s->width = s->avctx->width;
  1858. s->height = s->avctx->height;
  1859. s->codec_id= s->avctx->codec->id;
  1860. init_pred_ptrs(h);
  1861. s->decode=1; //FIXME
  1862. }
  1863. static int decode_init(AVCodecContext *avctx){
  1864. H264Context *h= avctx->priv_data;
  1865. MpegEncContext * const s = &h->s;
  1866. s->avctx = avctx;
  1867. common_init(h);
  1868. s->out_format = FMT_H264;
  1869. s->workaround_bugs= avctx->workaround_bugs;
  1870. // set defaults
  1871. s->progressive_sequence=1;
  1872. // s->decode_mb= ff_h263_decode_mb;
  1873. s->low_delay= 1;
  1874. avctx->pix_fmt= PIX_FMT_YUV420P;
  1875. decode_init_vlc(h);
  1876. return 0;
  1877. }
  1878. static void frame_start(H264Context *h){
  1879. MpegEncContext * const s = &h->s;
  1880. int i;
  1881. MPV_frame_start(s, s->avctx);
  1882. ff_er_frame_start(s);
  1883. h->mmco_index=0;
  1884. assert(s->linesize && s->uvlinesize);
  1885. for(i=0; i<16; i++){
  1886. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  1887. h->chroma_subblock_offset[i]= 2*((scan8[i] - scan8[0])&7) + 2*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  1888. }
  1889. for(i=0; i<4; i++){
  1890. h->block_offset[16+i]=
  1891. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  1892. }
  1893. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  1894. }
  1895. static void hl_decode_mb(H264Context *h){
  1896. MpegEncContext * const s = &h->s;
  1897. const int mb_x= s->mb_x;
  1898. const int mb_y= s->mb_y;
  1899. const int mb_xy= mb_x + mb_y*s->mb_stride;
  1900. const int mb_type= s->current_picture.mb_type[mb_xy];
  1901. uint8_t *dest_y, *dest_cb, *dest_cr;
  1902. int linesize, uvlinesize /*dct_offset*/;
  1903. int i;
  1904. if(!s->decode)
  1905. return;
  1906. if(s->mb_skiped){
  1907. }
  1908. dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  1909. dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  1910. dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  1911. if (h->mb_field_decoding_flag) {
  1912. linesize = s->linesize * 2;
  1913. uvlinesize = s->uvlinesize * 2;
  1914. if(mb_y&1){ //FIXME move out of this func?
  1915. dest_y -= s->linesize*15;
  1916. dest_cb-= s->linesize*7;
  1917. dest_cr-= s->linesize*7;
  1918. }
  1919. } else {
  1920. linesize = s->linesize;
  1921. uvlinesize = s->uvlinesize;
  1922. // dct_offset = s->linesize * 16;
  1923. }
  1924. if(IS_INTRA(mb_type)){
  1925. if(!(s->flags&CODEC_FLAG_GRAY)){
  1926. h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  1927. h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  1928. }
  1929. if(IS_INTRA4x4(mb_type)){
  1930. if(!s->encoding){
  1931. for(i=0; i<16; i++){
  1932. uint8_t * const ptr= dest_y + h->block_offset[i];
  1933. uint8_t *topright= ptr + 4 - linesize;
  1934. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  1935. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  1936. int tr;
  1937. if(!topright_avail){
  1938. tr= ptr[3 - linesize]*0x01010101;
  1939. topright= (uint8_t*) &tr;
  1940. }
  1941. h->pred4x4[ dir ](ptr, topright, linesize);
  1942. if(h->non_zero_count_cache[ scan8[i] ]){
  1943. if(s->codec_id == CODEC_ID_H264)
  1944. h264_add_idct_c(ptr, h->mb + i*16, linesize);
  1945. else
  1946. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  1947. }
  1948. }
  1949. }
  1950. }else{
  1951. h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  1952. if(s->codec_id == CODEC_ID_H264)
  1953. h264_luma_dc_dequant_idct_c(h->mb, s->qscale);
  1954. else
  1955. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  1956. }
  1957. }else if(s->codec_id == CODEC_ID_H264){
  1958. hl_motion(h, dest_y, dest_cb, dest_cr,
  1959. s->dsp.put_h264_qpel_pixels_tab, s->dsp.put_h264_chroma_pixels_tab,
  1960. s->dsp.avg_h264_qpel_pixels_tab, s->dsp.avg_h264_chroma_pixels_tab);
  1961. }
  1962. if(!IS_INTRA4x4(mb_type)){
  1963. if(s->codec_id == CODEC_ID_H264){
  1964. for(i=0; i<16; i++){
  1965. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  1966. uint8_t * const ptr= dest_y + h->block_offset[i];
  1967. h264_add_idct_c(ptr, h->mb + i*16, linesize);
  1968. }
  1969. }
  1970. }else{
  1971. for(i=0; i<16; i++){
  1972. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  1973. uint8_t * const ptr= dest_y + h->block_offset[i];
  1974. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  1975. }
  1976. }
  1977. }
  1978. }
  1979. if(!(s->flags&CODEC_FLAG_GRAY)){
  1980. chroma_dc_dequant_idct_c(h->mb + 16*16, h->chroma_qp);
  1981. chroma_dc_dequant_idct_c(h->mb + 16*16+4*16, h->chroma_qp);
  1982. if(s->codec_id == CODEC_ID_H264){
  1983. for(i=16; i<16+4; i++){
  1984. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  1985. uint8_t * const ptr= dest_cb + h->block_offset[i];
  1986. h264_add_idct_c(ptr, h->mb + i*16, uvlinesize);
  1987. }
  1988. }
  1989. for(i=20; i<20+4; i++){
  1990. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  1991. uint8_t * const ptr= dest_cr + h->block_offset[i];
  1992. h264_add_idct_c(ptr, h->mb + i*16, uvlinesize);
  1993. }
  1994. }
  1995. }else{
  1996. for(i=16; i<16+4; i++){
  1997. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  1998. uint8_t * const ptr= dest_cb + h->block_offset[i];
  1999. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  2000. }
  2001. }
  2002. for(i=20; i<20+4; i++){
  2003. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  2004. uint8_t * const ptr= dest_cr + h->block_offset[i];
  2005. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  2006. }
  2007. }
  2008. }
  2009. }
  2010. }
  2011. static void decode_mb_cabac(H264Context *h){
  2012. // MpegEncContext * const s = &h->s;
  2013. }
  2014. /**
  2015. * fills the default_ref_list.
  2016. */
  2017. static int fill_default_ref_list(H264Context *h){
  2018. MpegEncContext * const s = &h->s;
  2019. int i;
  2020. Picture sorted_short_ref[16];
  2021. if(h->slice_type==B_TYPE){
  2022. int out_i;
  2023. int limit= -1;
  2024. for(out_i=0; out_i<h->short_ref_count; out_i++){
  2025. int best_i=-1;
  2026. int best_poc=-1;
  2027. for(i=0; i<h->short_ref_count; i++){
  2028. const int poc= h->short_ref[i]->poc;
  2029. if(poc > limit && poc < best_poc){
  2030. best_poc= poc;
  2031. best_i= i;
  2032. }
  2033. }
  2034. assert(best_i != -1);
  2035. limit= best_poc;
  2036. sorted_short_ref[out_i]= *h->short_ref[best_i];
  2037. }
  2038. }
  2039. if(s->picture_structure == PICT_FRAME){
  2040. if(h->slice_type==B_TYPE){
  2041. const int current_poc= s->current_picture_ptr->poc;
  2042. int list;
  2043. for(list=0; list<2; list++){
  2044. int index=0;
  2045. for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++){
  2046. const int i2= list ? h->short_ref_count - i - 1 : i;
  2047. const int poc= sorted_short_ref[i2].poc;
  2048. if(sorted_short_ref[i2].reference != 3) continue; //FIXME refernce field shit
  2049. if((list==1 && poc > current_poc) || (list==0 && poc < current_poc)){
  2050. h->default_ref_list[list][index ]= sorted_short_ref[i2];
  2051. h->default_ref_list[list][index++].pic_id= sorted_short_ref[i2].frame_num;
  2052. }
  2053. }
  2054. for(i=0; i<h->long_ref_count && index < h->ref_count[ list ]; i++){
  2055. if(h->long_ref[i]->reference != 3) continue;
  2056. h->default_ref_list[ list ][index ]= *h->long_ref[i];
  2057. h->default_ref_list[ list ][index++].pic_id= i;;
  2058. }
  2059. if(h->long_ref_count > 1 && h->short_ref_count==0){
  2060. Picture temp= h->default_ref_list[1][0];
  2061. h->default_ref_list[1][0] = h->default_ref_list[1][1];
  2062. h->default_ref_list[1][0] = temp;
  2063. }
  2064. if(index < h->ref_count[ list ])
  2065. memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
  2066. }
  2067. }else{
  2068. int index=0;
  2069. for(i=0; i<h->short_ref_count && index < h->ref_count[0]; i++){
  2070. if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
  2071. h->default_ref_list[0][index ]= *h->short_ref[i];
  2072. h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
  2073. }
  2074. for(i=0; i<h->long_ref_count && index < h->ref_count[0]; i++){
  2075. if(h->long_ref[i]->reference != 3) continue;
  2076. h->default_ref_list[0][index ]= *h->long_ref[i];
  2077. h->default_ref_list[0][index++].pic_id= i;;
  2078. }
  2079. if(index < h->ref_count[0])
  2080. memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
  2081. }
  2082. }else{ //FIELD
  2083. if(h->slice_type==B_TYPE){
  2084. }else{
  2085. //FIXME second field balh
  2086. }
  2087. }
  2088. return 0;
  2089. }
  2090. static int decode_ref_pic_list_reordering(H264Context *h){
  2091. MpegEncContext * const s = &h->s;
  2092. int list;
  2093. if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move beofre func
  2094. for(list=0; list<2; list++){
  2095. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  2096. if(get_bits1(&s->gb)){
  2097. int pred= h->curr_pic_num;
  2098. int index;
  2099. for(index=0; ; index++){
  2100. int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
  2101. int pic_id;
  2102. int i;
  2103. if(index >= h->ref_count[list]){
  2104. fprintf(stderr, "reference count overflow\n");
  2105. return -1;
  2106. }
  2107. if(reordering_of_pic_nums_idc<3){
  2108. if(reordering_of_pic_nums_idc<2){
  2109. const int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  2110. if(abs_diff_pic_num >= h->max_pic_num){
  2111. fprintf(stderr, "abs_diff_pic_num overflow\n");
  2112. return -1;
  2113. }
  2114. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  2115. else pred+= abs_diff_pic_num;
  2116. pred &= h->max_pic_num - 1;
  2117. for(i= h->ref_count[list]-1; i>=index; i--){
  2118. if(h->ref_list[list][i].pic_id == pred && h->ref_list[list][i].long_ref==0)
  2119. break;
  2120. }
  2121. }else{
  2122. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  2123. for(i= h->ref_count[list]-1; i>=index; i--){
  2124. if(h->ref_list[list][i].pic_id == pic_id && h->ref_list[list][i].long_ref==1)
  2125. break;
  2126. }
  2127. }
  2128. if(i < index){
  2129. fprintf(stderr, "reference picture missing during reorder\n");
  2130. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  2131. }else if(i > index){
  2132. Picture tmp= h->ref_list[list][i];
  2133. for(; i>index; i--){
  2134. h->ref_list[list][i]= h->ref_list[list][i-1];
  2135. }
  2136. h->ref_list[list][index]= tmp;
  2137. }
  2138. }else if(reordering_of_pic_nums_idc==3)
  2139. break;
  2140. else{
  2141. fprintf(stderr, "illegal reordering_of_pic_nums_idc\n");
  2142. return -1;
  2143. }
  2144. }
  2145. }
  2146. if(h->slice_type!=B_TYPE) break;
  2147. }
  2148. return 0;
  2149. }
  2150. static int pred_weight_table(H264Context *h){
  2151. MpegEncContext * const s = &h->s;
  2152. int list, i;
  2153. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  2154. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  2155. for(list=0; list<2; list++){
  2156. for(i=0; i<h->ref_count[list]; i++){
  2157. int luma_weight_flag, chroma_weight_flag;
  2158. luma_weight_flag= get_bits1(&s->gb);
  2159. if(luma_weight_flag){
  2160. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  2161. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  2162. }
  2163. chroma_weight_flag= get_bits1(&s->gb);
  2164. if(chroma_weight_flag){
  2165. int j;
  2166. for(j=0; j<2; j++){
  2167. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  2168. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  2169. }
  2170. }
  2171. }
  2172. if(h->slice_type != B_TYPE) break;
  2173. }
  2174. return 0;
  2175. }
  2176. /**
  2177. * instantaneos decoder refresh.
  2178. */
  2179. static void idr(H264Context *h){
  2180. int i;
  2181. for(i=0; i<h->long_ref_count; i++){
  2182. h->long_ref[i]->reference=0;
  2183. h->long_ref[i]= NULL;
  2184. }
  2185. h->long_ref_count=0;
  2186. for(i=0; i<h->short_ref_count; i++){
  2187. h->short_ref[i]->reference=0;
  2188. h->short_ref[i]= NULL;
  2189. }
  2190. h->short_ref_count=0;
  2191. }
  2192. /**
  2193. *
  2194. * @return the removed picture or NULL if an error occures
  2195. */
  2196. static Picture * remove_short(H264Context *h, int frame_num){
  2197. MpegEncContext * const s = &h->s;
  2198. int i;
  2199. if(s->avctx->debug&FF_DEBUG_MMCO)
  2200. printf("remove short %d count %d\n", frame_num, h->short_ref_count);
  2201. for(i=0; i<h->short_ref_count; i++){
  2202. Picture *pic= h->short_ref[i];
  2203. if(s->avctx->debug&FF_DEBUG_MMCO)
  2204. printf("%d %d %X\n", i, pic->frame_num, (int)pic);
  2205. if(pic->frame_num == frame_num){
  2206. h->short_ref[i]= NULL;
  2207. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
  2208. h->short_ref_count--;
  2209. return pic;
  2210. }
  2211. }
  2212. return NULL;
  2213. }
  2214. /**
  2215. *
  2216. * @return the removed picture or NULL if an error occures
  2217. */
  2218. static Picture * remove_long(H264Context *h, int i){
  2219. Picture *pic;
  2220. if(i >= h->long_ref_count) return NULL;
  2221. pic= h->long_ref[i];
  2222. if(pic==NULL) return NULL;
  2223. h->long_ref[i]= NULL;
  2224. memmove(&h->long_ref[i], &h->long_ref[i+1], (h->long_ref_count - i - 1)*sizeof(Picture*));
  2225. h->long_ref_count--;
  2226. return pic;
  2227. }
  2228. /**
  2229. * Executes the reference picture marking (memory management control operations).
  2230. */
  2231. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  2232. MpegEncContext * const s = &h->s;
  2233. int i;
  2234. int current_is_long=0;
  2235. Picture *pic;
  2236. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  2237. printf("no mmco here\n");
  2238. for(i=0; i<mmco_count; i++){
  2239. if(s->avctx->debug&FF_DEBUG_MMCO)
  2240. printf("mmco:%d %d %d\n", h->mmco[i].opcode, h->mmco[i].short_frame_num, h->mmco[i].long_index);
  2241. switch(mmco[i].opcode){
  2242. case MMCO_SHORT2UNUSED:
  2243. pic= remove_short(h, mmco[i].short_frame_num);
  2244. if(pic==NULL) return -1;
  2245. pic->reference= 0;
  2246. break;
  2247. case MMCO_SHORT2LONG:
  2248. pic= remove_long(h, mmco[i].long_index);
  2249. if(pic) pic->reference=0;
  2250. h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
  2251. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  2252. break;
  2253. case MMCO_LONG2UNUSED:
  2254. pic= remove_long(h, mmco[i].long_index);
  2255. if(pic==NULL) return -1;
  2256. pic->reference= 0;
  2257. break;
  2258. case MMCO_LONG:
  2259. pic= remove_long(h, mmco[i].long_index);
  2260. if(pic) pic->reference=0;
  2261. h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
  2262. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  2263. h->long_ref_count++;
  2264. current_is_long=1;
  2265. break;
  2266. case MMCO_SET_MAX_LONG:
  2267. assert(mmco[i].long_index <= 16);
  2268. while(mmco[i].long_index < h->long_ref_count){
  2269. pic= remove_long(h, mmco[i].long_index);
  2270. pic->reference=0;
  2271. }
  2272. while(mmco[i].long_index > h->long_ref_count){
  2273. h->long_ref[ h->long_ref_count++ ]= NULL;
  2274. }
  2275. break;
  2276. case MMCO_RESET:
  2277. while(h->short_ref_count){
  2278. pic= remove_short(h, h->short_ref[0]->frame_num);
  2279. pic->reference=0;
  2280. }
  2281. while(h->long_ref_count){
  2282. pic= remove_long(h, h->long_ref_count-1);
  2283. pic->reference=0;
  2284. }
  2285. break;
  2286. default: assert(0);
  2287. }
  2288. }
  2289. if(!current_is_long){
  2290. pic= remove_short(h, s->current_picture_ptr->frame_num);
  2291. if(pic){
  2292. pic->reference=0;
  2293. fprintf(stderr, "illegal short term buffer state detected\n");
  2294. }
  2295. if(h->short_ref_count)
  2296. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  2297. h->short_ref[0]= s->current_picture_ptr;
  2298. h->short_ref[0]->long_ref=0;
  2299. h->short_ref_count++;
  2300. }
  2301. return 0;
  2302. }
  2303. static int decode_ref_pic_marking(H264Context *h){
  2304. MpegEncContext * const s = &h->s;
  2305. int i;
  2306. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  2307. s->broken_link= get_bits1(&s->gb) -1;
  2308. h->mmco[0].long_index= get_bits1(&s->gb) - 1; // current_long_term_idx
  2309. if(h->mmco[0].long_index == -1)
  2310. h->mmco_index= 0;
  2311. else{
  2312. h->mmco[0].opcode= MMCO_LONG;
  2313. h->mmco_index= 1;
  2314. }
  2315. }else{
  2316. if(get_bits1(&s->gb)){ // adaptive_ref_pic_marking_mode_flag
  2317. for(i= h->mmco_index; i<MAX_MMCO_COUNT; i++) {
  2318. MMCOOpcode opcode= get_ue_golomb(&s->gb);;
  2319. h->mmco[i].opcode= opcode;
  2320. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  2321. 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
  2322. /* if(h->mmco[i].short_frame_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_frame_num ] == NULL){
  2323. fprintf(stderr, "illegal short ref in memory management control operation %d\n", mmco);
  2324. return -1;
  2325. }*/
  2326. }
  2327. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  2328. h->mmco[i].long_index= get_ue_golomb(&s->gb);
  2329. 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){
  2330. fprintf(stderr, "illegal long ref in memory management control operation %d\n", opcode);
  2331. return -1;
  2332. }
  2333. }
  2334. if(opcode > MMCO_LONG){
  2335. fprintf(stderr, "illegal memory management control operation %d\n", opcode);
  2336. return -1;
  2337. }
  2338. }
  2339. h->mmco_index= i;
  2340. }else{
  2341. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  2342. if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){ //FIXME fields
  2343. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  2344. h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  2345. h->mmco_index= 1;
  2346. }else
  2347. h->mmco_index= 0;
  2348. }
  2349. }
  2350. return 0;
  2351. }
  2352. static int init_poc(H264Context *h){
  2353. MpegEncContext * const s = &h->s;
  2354. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  2355. int field_poc[2];
  2356. if(h->nal_unit_type == NAL_IDR_SLICE){
  2357. h->frame_num_offset= 0;
  2358. }else{
  2359. if(h->frame_num < h->prev_frame_num)
  2360. h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
  2361. else
  2362. h->frame_num_offset= h->prev_frame_num_offset;
  2363. }
  2364. if(h->sps.poc_type==0){
  2365. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  2366. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  2367. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  2368. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  2369. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  2370. else
  2371. h->poc_msb = h->prev_poc_msb;
  2372. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  2373. field_poc[0] =
  2374. field_poc[1] = h->poc_msb + h->poc_lsb;
  2375. if(s->picture_structure == PICT_FRAME)
  2376. field_poc[1] += h->delta_poc_bottom;
  2377. }else if(h->sps.poc_type==1){
  2378. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  2379. int i;
  2380. if(h->sps.poc_cycle_length != 0)
  2381. abs_frame_num = h->frame_num_offset + h->frame_num;
  2382. else
  2383. abs_frame_num = 0;
  2384. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  2385. abs_frame_num--;
  2386. expected_delta_per_poc_cycle = 0;
  2387. for(i=0; i < h->sps.poc_cycle_length; i++)
  2388. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  2389. if(abs_frame_num > 0){
  2390. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  2391. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  2392. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  2393. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  2394. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  2395. } else
  2396. expectedpoc = 0;
  2397. if(h->nal_ref_idc == 0)
  2398. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  2399. field_poc[0] = expectedpoc + h->delta_poc[0];
  2400. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  2401. if(s->picture_structure == PICT_FRAME)
  2402. field_poc[1] += h->delta_poc[1];
  2403. }else{
  2404. int poc;
  2405. if(h->nal_unit_type == NAL_IDR_SLICE){
  2406. poc= 0;
  2407. }else{
  2408. if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
  2409. else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
  2410. }
  2411. field_poc[0]= poc;
  2412. field_poc[1]= poc;
  2413. }
  2414. if(s->picture_structure != PICT_BOTTOM_FIELD)
  2415. s->current_picture_ptr->field_poc[0]= field_poc[0];
  2416. if(s->picture_structure != PICT_TOP_FIELD)
  2417. s->current_picture_ptr->field_poc[1]= field_poc[1];
  2418. if(s->picture_structure == PICT_FRAME) // FIXME field pix?
  2419. s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]);
  2420. return 0;
  2421. }
  2422. /**
  2423. * decodes a slice header.
  2424. * this will allso call MPV_common_init() and frame_start() as needed
  2425. */
  2426. static int decode_slice_header(H264Context *h){
  2427. MpegEncContext * const s = &h->s;
  2428. int first_mb_in_slice, pps_id;
  2429. int num_ref_idx_active_override_flag;
  2430. static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
  2431. float new_aspect;
  2432. s->current_picture.reference= h->nal_ref_idc != 0;
  2433. first_mb_in_slice= get_ue_golomb(&s->gb);
  2434. h->slice_type= get_ue_golomb(&s->gb);
  2435. if(h->slice_type > 9){
  2436. fprintf(stderr, "slice type too large (%d) at %d %d\n", h->slice_type, s->mb_x, s->mb_y);
  2437. }
  2438. if(h->slice_type > 4){
  2439. h->slice_type -= 5;
  2440. h->slice_type_fixed=1;
  2441. }else
  2442. h->slice_type_fixed=0;
  2443. h->slice_type= slice_type_map[ h->slice_type ];
  2444. s->pict_type= h->slice_type; // to make a few old func happy, its wrong though
  2445. pps_id= get_ue_golomb(&s->gb);
  2446. if(pps_id>255){
  2447. fprintf(stderr, "pps_id out of range\n");
  2448. return -1;
  2449. }
  2450. h->pps= h->pps_buffer[pps_id];
  2451. if(h->pps.slice_group_count == 0){
  2452. fprintf(stderr, "non existing PPS referenced\n");
  2453. return -1;
  2454. }
  2455. h->sps= h->sps_buffer[ h->pps.sps_id ];
  2456. if(h->sps.log2_max_frame_num == 0){
  2457. fprintf(stderr, "non existing SPS referenced\n");
  2458. return -1;
  2459. }
  2460. s->mb_width= h->sps.mb_width;
  2461. s->mb_height= h->sps.mb_height;
  2462. h->b_stride= s->mb_width*4;
  2463. h->b8_stride= s->mb_width*2;
  2464. s->mb_x = first_mb_in_slice % s->mb_width;
  2465. s->mb_y = first_mb_in_slice / s->mb_width; //FIXME AFFW
  2466. s->width = 16*s->mb_width - 2*(h->pps.crop_left + h->pps.crop_right );
  2467. if(h->sps.frame_mbs_only_flag)
  2468. s->height= 16*s->mb_height - 2*(h->pps.crop_top + h->pps.crop_bottom);
  2469. else
  2470. s->height= 16*s->mb_height - 4*(h->pps.crop_top + h->pps.crop_bottom); //FIXME recheck
  2471. if(h->pps.crop_left || h->pps.crop_top){
  2472. fprintf(stderr, "insane croping not completly supported, this could look slightly wrong ...\n");
  2473. }
  2474. if(s->aspected_height) //FIXME emms at end of slice ?
  2475. new_aspect= h->sps.sar_width*s->width / (float)(s->height*h->sps.sar_height);
  2476. else
  2477. new_aspect=0;
  2478. if (s->context_initialized
  2479. && ( s->width != s->avctx->width || s->height != s->avctx->height
  2480. || ABS(new_aspect - s->avctx->aspect_ratio) > 0.001)) {
  2481. free_tables(h);
  2482. MPV_common_end(s);
  2483. }
  2484. if (!s->context_initialized) {
  2485. if (MPV_common_init(s) < 0)
  2486. return -1;
  2487. alloc_tables(h);
  2488. s->avctx->width = s->width;
  2489. s->avctx->height = s->height;
  2490. s->avctx->aspect_ratio= new_aspect;
  2491. }
  2492. if(first_mb_in_slice == 0){
  2493. frame_start(h);
  2494. }
  2495. s->current_picture_ptr->frame_num= //FIXME frame_num cleanup
  2496. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  2497. if(h->sps.frame_mbs_only_flag){
  2498. s->picture_structure= PICT_FRAME;
  2499. }else{
  2500. if(get_bits1(&s->gb)) //field_pic_flag
  2501. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  2502. else
  2503. s->picture_structure= PICT_FRAME;
  2504. }
  2505. if(s->picture_structure==PICT_FRAME){
  2506. h->curr_pic_num= h->frame_num;
  2507. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  2508. }else{
  2509. h->curr_pic_num= 2*h->frame_num;
  2510. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  2511. }
  2512. if(h->nal_unit_type == NAL_IDR_SLICE){
  2513. int idr_pic_id= get_ue_golomb(&s->gb);
  2514. }
  2515. if(h->sps.poc_type==0){
  2516. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  2517. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  2518. h->delta_poc_bottom= get_se_golomb(&s->gb);
  2519. }
  2520. }
  2521. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  2522. h->delta_poc[0]= get_se_golomb(&s->gb);
  2523. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  2524. h->delta_poc[1]= get_se_golomb(&s->gb);
  2525. }
  2526. init_poc(h);
  2527. if(h->pps.redundant_pic_cnt_present){
  2528. h->redundant_pic_count= get_ue_golomb(&s->gb);
  2529. }
  2530. //set defaults, might be overriden a few line later
  2531. h->ref_count[0]= h->pps.ref_count[0];
  2532. h->ref_count[1]= h->pps.ref_count[1];
  2533. if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){
  2534. if(h->slice_type == B_TYPE){
  2535. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  2536. }
  2537. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  2538. if(num_ref_idx_active_override_flag){
  2539. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  2540. if(h->slice_type==B_TYPE)
  2541. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  2542. if(h->ref_count[0] > 32 || h->ref_count[1] > 32){
  2543. fprintf(stderr, "reference overflow\n");
  2544. return -1;
  2545. }
  2546. }
  2547. }
  2548. if(first_mb_in_slice == 0){
  2549. fill_default_ref_list(h);
  2550. }
  2551. decode_ref_pic_list_reordering(h);
  2552. if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
  2553. || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
  2554. pred_weight_table(h);
  2555. if(s->current_picture.reference)
  2556. decode_ref_pic_marking(h);
  2557. //FIXME CABAC stuff
  2558. s->qscale = h->pps.init_qp + get_se_golomb(&s->gb); //slice_qp_delta
  2559. //FIXME qscale / qp ... stuff
  2560. if(h->slice_type == SP_TYPE){
  2561. int sp_for_switch_flag= get_bits1(&s->gb);
  2562. }
  2563. if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
  2564. int slice_qs_delta= get_se_golomb(&s->gb);
  2565. }
  2566. if( h->pps.deblocking_filter_parameters_present ) {
  2567. h->disable_deblocking_filter_idc= get_ue_golomb(&s->gb);
  2568. if( h->disable_deblocking_filter_idc != 1 ) {
  2569. h->slice_alpha_c0_offset_div2= get_se_golomb(&s->gb);
  2570. h->slice_beta_offset_div2= get_se_golomb(&s->gb);
  2571. }
  2572. }else
  2573. h->disable_deblocking_filter_idc= 0;
  2574. #if 0 //FMO
  2575. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  2576. slice_group_change_cycle= get_bits(&s->gb, ?);
  2577. #endif
  2578. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  2579. printf("mb:%d %c pps:%d frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d\n",
  2580. first_mb_in_slice,
  2581. ff_get_pict_type_char(h->slice_type),
  2582. pps_id, h->frame_num,
  2583. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  2584. h->ref_count[0], h->ref_count[1],
  2585. s->qscale,
  2586. h->disable_deblocking_filter_idc
  2587. );
  2588. }
  2589. return 0;
  2590. }
  2591. /**
  2592. *
  2593. */
  2594. static inline int get_level_prefix(GetBitContext *gb){
  2595. unsigned int buf;
  2596. int log;
  2597. OPEN_READER(re, gb);
  2598. UPDATE_CACHE(re, gb);
  2599. buf=GET_CACHE(re, gb);
  2600. log= 32 - av_log2(buf);
  2601. #ifdef TRACE
  2602. print_bin(buf>>(32-log), log);
  2603. printf("%5d %2d %3d lpr @%5d in %s get_level_prefix\n", buf>>(32-log), log, log-1, get_bits_count(gb), __FILE__);
  2604. #endif
  2605. LAST_SKIP_BITS(re, gb, log);
  2606. CLOSE_READER(re, gb);
  2607. return log-1;
  2608. }
  2609. /**
  2610. * decodes a residual block.
  2611. * @param n block index
  2612. * @param scantable scantable
  2613. * @param max_coeff number of coefficients in the block
  2614. * @return <0 if an error occured
  2615. */
  2616. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, int qp, int max_coeff){
  2617. MpegEncContext * const s = &h->s;
  2618. const uint16_t *qmul= dequant_coeff[qp];
  2619. 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};
  2620. int level[16], run[16];
  2621. int suffix_length, zeros_left, coeff_num, coeff_token, total_coeff, i, trailing_ones;
  2622. //FIXME put trailing_onex into the context
  2623. if(n == CHROMA_DC_BLOCK_INDEX){
  2624. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  2625. total_coeff= coeff_token>>2;
  2626. }else{
  2627. if(n == LUMA_DC_BLOCK_INDEX){
  2628. total_coeff= pred_non_zero_count(h, 0);
  2629. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  2630. total_coeff= coeff_token>>2;
  2631. }else{
  2632. total_coeff= pred_non_zero_count(h, n);
  2633. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  2634. total_coeff= coeff_token>>2;
  2635. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  2636. }
  2637. }
  2638. //FIXME set last_non_zero?
  2639. if(total_coeff==0)
  2640. return 0;
  2641. trailing_ones= coeff_token&3;
  2642. tprintf("trailing:%d, total:%d\n", trailing_ones, total_coeff);
  2643. assert(total_coeff<=16);
  2644. for(i=0; i<trailing_ones; i++){
  2645. level[i]= 1 - 2*get_bits1(gb);
  2646. }
  2647. suffix_length= total_coeff > 10 && trailing_ones < 3;
  2648. for(; i<total_coeff; i++){
  2649. const int prefix= get_level_prefix(gb);
  2650. int level_code, mask;
  2651. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  2652. if(suffix_length)
  2653. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  2654. else
  2655. level_code= (prefix<<suffix_length); //part
  2656. }else if(prefix==14){
  2657. if(suffix_length)
  2658. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  2659. else
  2660. level_code= prefix + get_bits(gb, 4); //part
  2661. }else if(prefix==15){
  2662. level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part
  2663. if(suffix_length==0) level_code+=15; //FIXME doesnt make (much)sense
  2664. }else{
  2665. fprintf(stderr, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  2666. return -1;
  2667. }
  2668. if(i==trailing_ones && i<3) level_code+= 2; //FIXME split first iteration
  2669. mask= -(level_code&1);
  2670. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  2671. if(suffix_length==0) suffix_length=1; //FIXME split first iteration
  2672. #if 1
  2673. if(ABS(level[i]) > (3<<(suffix_length-1)) && suffix_length<6) suffix_length++;
  2674. #else
  2675. if((2+level_code)>>1) > (3<<(suffix_length-1)) && suffix_length<6) suffix_length++;
  2676. ? == prefix > 2 or sth
  2677. #endif
  2678. tprintf("level: %d suffix_length:%d\n", level[i], suffix_length);
  2679. }
  2680. if(total_coeff == max_coeff)
  2681. zeros_left=0;
  2682. else{
  2683. if(n == CHROMA_DC_BLOCK_INDEX)
  2684. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  2685. else
  2686. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  2687. }
  2688. for(i=0; i<total_coeff-1; i++){
  2689. if(zeros_left <=0)
  2690. break;
  2691. else if(zeros_left < 7){
  2692. run[i]= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  2693. }else{
  2694. run[i]= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  2695. }
  2696. zeros_left -= run[i];
  2697. }
  2698. if(zeros_left<0){
  2699. fprintf(stderr, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  2700. return -1;
  2701. }
  2702. for(; i<total_coeff-1; i++){
  2703. run[i]= 0;
  2704. }
  2705. run[i]= zeros_left;
  2706. coeff_num=-1;
  2707. if(n > 24){
  2708. for(i=total_coeff-1; i>=0; i--){ //FIXME merge into rundecode?
  2709. int j;
  2710. coeff_num += run[i] + 1; //FIXME add 1 earlier ?
  2711. j= scantable[ coeff_num ];
  2712. block[j]= level[i];
  2713. }
  2714. }else{
  2715. for(i=total_coeff-1; i>=0; i--){ //FIXME merge into rundecode?
  2716. int j;
  2717. coeff_num += run[i] + 1; //FIXME add 1 earlier ?
  2718. j= scantable[ coeff_num ];
  2719. block[j]= level[i] * qmul[j];
  2720. // printf("%d %d ", block[j], qmul[j]);
  2721. }
  2722. }
  2723. return 0;
  2724. }
  2725. /**
  2726. * decodes a macroblock
  2727. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  2728. */
  2729. static int decode_mb(H264Context *h){
  2730. MpegEncContext * const s = &h->s;
  2731. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2732. int mb_type, partition_count, cbp;
  2733. memset(h->mb, 0, sizeof(int16_t)*24*16); //FIXME avoid if allready clear (move after skip handlong?
  2734. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  2735. if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){
  2736. if(s->mb_skip_run==-1)
  2737. s->mb_skip_run= get_ue_golomb(&s->gb);
  2738. if (s->mb_skip_run--) {
  2739. int mx, my;
  2740. /* skip mb */
  2741. //FIXME b frame
  2742. mb_type= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0;
  2743. memset(h->non_zero_count[mb_xy], 0, 16);
  2744. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  2745. if(h->sps.mb_aff && s->mb_skip_run==0 && (s->mb_y&1)==0){
  2746. h->mb_field_decoding_flag= get_bits1(&s->gb);
  2747. }
  2748. if(h->mb_field_decoding_flag)
  2749. mb_type|= MB_TYPE_INTERLACED;
  2750. fill_caches(h, mb_type); //FIXME check what is needed and what not ...
  2751. pred_pskip_motion(h, &mx, &my);
  2752. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  2753. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, (mx&0xFFFF)+(my<<16), 4);
  2754. write_back_motion(h, mb_type);
  2755. s->current_picture.mb_type[mb_xy]= mb_type; //FIXME SKIP type
  2756. h->slice_table[ mb_xy ]= h->slice_num;
  2757. h->prev_mb_skiped= 1;
  2758. return 0;
  2759. }
  2760. }
  2761. if(h->sps.mb_aff /* && !field pic FIXME needed? */){
  2762. if((s->mb_y&1)==0)
  2763. h->mb_field_decoding_flag = get_bits1(&s->gb);
  2764. }else
  2765. h->mb_field_decoding_flag=0; //FIXME som ed note ?!
  2766. h->prev_mb_skiped= 0;
  2767. mb_type= get_ue_golomb(&s->gb);
  2768. if(h->slice_type == B_TYPE){
  2769. if(mb_type < 23){
  2770. partition_count= b_mb_type_info[mb_type].partition_count;
  2771. mb_type= b_mb_type_info[mb_type].type;
  2772. }else{
  2773. mb_type -= 23;
  2774. goto decode_intra_mb;
  2775. }
  2776. }else if(h->slice_type == P_TYPE /*|| h->slice_type == SP_TYPE */){
  2777. if(mb_type < 5){
  2778. partition_count= p_mb_type_info[mb_type].partition_count;
  2779. mb_type= p_mb_type_info[mb_type].type;
  2780. }else{
  2781. mb_type -= 5;
  2782. goto decode_intra_mb;
  2783. }
  2784. }else{
  2785. assert(h->slice_type == I_TYPE);
  2786. decode_intra_mb:
  2787. if(mb_type > 25){
  2788. fprintf(stderr, "mb_type %d in %c slice to large at %d %d\n", mb_type, ff_get_pict_type_char(h->slice_type), s->mb_x, s->mb_y);
  2789. return -1;
  2790. }
  2791. partition_count=0;
  2792. cbp= i_mb_type_info[mb_type].cbp;
  2793. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  2794. mb_type= i_mb_type_info[mb_type].type;
  2795. }
  2796. if(h->mb_field_decoding_flag)
  2797. mb_type |= MB_TYPE_INTERLACED;
  2798. s->current_picture.mb_type[mb_xy]= mb_type;
  2799. h->slice_table[ mb_xy ]= h->slice_num;
  2800. if(IS_INTRA_PCM(mb_type)){
  2801. const uint8_t *ptr;
  2802. int x, y;
  2803. // we assume these blocks are very rare so we dont optimize it
  2804. align_get_bits(&s->gb);
  2805. ptr= s->gb.buffer + get_bits_count(&s->gb);
  2806. for(y=0; y<16; y++){
  2807. const int index= 4*(y&3) + 64*(y>>2);
  2808. for(x=0; x<16; x++){
  2809. h->mb[index + (x&3) + 16*(x>>2)]= *(ptr++);
  2810. }
  2811. }
  2812. for(y=0; y<8; y++){
  2813. const int index= 256 + 4*(y&3) + 32*(y>>2);
  2814. for(x=0; x<8; x++){
  2815. h->mb[index + (x&3) + 16*(x>>2)]= *(ptr++);
  2816. }
  2817. }
  2818. for(y=0; y<8; y++){
  2819. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  2820. for(x=0; x<8; x++){
  2821. h->mb[index + (x&3) + 16*(x>>2)]= *(ptr++);
  2822. }
  2823. }
  2824. skip_bits(&s->gb, 384); //FIXME check /fix the bitstream readers
  2825. memset(h->non_zero_count[mb_xy], 16, 16);
  2826. return 0;
  2827. }
  2828. fill_caches(h, mb_type);
  2829. //mb_pred
  2830. if(IS_INTRA(mb_type)){
  2831. // init_top_left_availability(h);
  2832. if(IS_INTRA4x4(mb_type)){
  2833. int i;
  2834. // fill_intra4x4_pred_table(h);
  2835. for(i=0; i<16; i++){
  2836. const int mode_coded= !get_bits1(&s->gb);
  2837. const int predicted_mode= pred_intra_mode(h, i);
  2838. int mode;
  2839. if(mode_coded){
  2840. const int rem_mode= get_bits(&s->gb, 3);
  2841. if(rem_mode<predicted_mode)
  2842. mode= rem_mode;
  2843. else
  2844. mode= rem_mode + 1;
  2845. }else{
  2846. mode= predicted_mode;
  2847. }
  2848. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  2849. }
  2850. write_back_intra_pred_mode(h);
  2851. if( check_intra4x4_pred_mode(h) < 0)
  2852. return -1;
  2853. }else{
  2854. h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
  2855. if(h->intra16x16_pred_mode < 0)
  2856. return -1;
  2857. }
  2858. h->chroma_pred_mode= get_ue_golomb(&s->gb);
  2859. h->chroma_pred_mode= check_intra_pred_mode(h, h->chroma_pred_mode);
  2860. if(h->chroma_pred_mode < 0)
  2861. return -1;
  2862. }else if(partition_count==4){
  2863. int i, j, sub_partition_count[4], list, ref[2][4];
  2864. if(h->slice_type == B_TYPE){
  2865. for(i=0; i<4; i++){
  2866. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  2867. if(h->sub_mb_type[i] >=13){
  2868. fprintf(stderr, "B sub_mb_type %d out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
  2869. return -1;
  2870. }
  2871. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  2872. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  2873. }
  2874. }else{
  2875. assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE); //FIXME SP correct ?
  2876. for(i=0; i<4; i++){
  2877. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  2878. if(h->sub_mb_type[i] >=4){
  2879. fprintf(stderr, "P sub_mb_type %d out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
  2880. return -1;
  2881. }
  2882. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  2883. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  2884. }
  2885. }
  2886. for(list=0; list<2; list++){
  2887. const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  2888. if(ref_count == 0) continue;
  2889. for(i=0; i<4; i++){
  2890. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  2891. ref[list][i] = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
  2892. }else{
  2893. //FIXME
  2894. ref[list][i] = -1;
  2895. }
  2896. }
  2897. }
  2898. for(list=0; list<2; list++){
  2899. const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  2900. if(ref_count == 0) continue;
  2901. for(i=0; i<4; i++){
  2902. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  2903. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  2904. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  2905. const int sub_mb_type= h->sub_mb_type[i];
  2906. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  2907. for(j=0; j<sub_partition_count[i]; j++){
  2908. int mx, my;
  2909. const int index= 4*i + block_width*j;
  2910. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  2911. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  2912. mx += get_se_golomb(&s->gb);
  2913. my += get_se_golomb(&s->gb);
  2914. tprintf("final mv:%d %d\n", mx, my);
  2915. if(IS_SUB_8X8(sub_mb_type)){
  2916. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  2917. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  2918. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  2919. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  2920. }else if(IS_SUB_8X4(sub_mb_type)){
  2921. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  2922. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  2923. }else if(IS_SUB_4X8(sub_mb_type)){
  2924. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  2925. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  2926. }else{
  2927. assert(IS_SUB_4X4(sub_mb_type));
  2928. mv_cache[ 0 ][0]= mx;
  2929. mv_cache[ 0 ][1]= my;
  2930. }
  2931. }
  2932. }else{
  2933. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  2934. p[0] = p[1]=
  2935. p[8] = p[9]= 0;
  2936. }
  2937. }
  2938. }
  2939. }else if(!IS_DIRECT(mb_type)){
  2940. int list, mx, my, i;
  2941. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  2942. if(IS_16X16(mb_type)){
  2943. for(list=0; list<2; list++){
  2944. if(h->ref_count[0]>0){
  2945. if(IS_DIR(mb_type, 0, list)){
  2946. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  2947. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  2948. }
  2949. }
  2950. }
  2951. for(list=0; list<2; list++){
  2952. if(IS_DIR(mb_type, 0, list)){
  2953. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  2954. mx += get_se_golomb(&s->gb);
  2955. my += get_se_golomb(&s->gb);
  2956. tprintf("final mv:%d %d\n", mx, my);
  2957. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, (mx&0xFFFF) + (my<<16), 4);
  2958. }
  2959. }
  2960. }
  2961. else if(IS_16X8(mb_type)){
  2962. for(list=0; list<2; list++){
  2963. if(h->ref_count[list]>0){
  2964. for(i=0; i<2; i++){
  2965. if(IS_DIR(mb_type, i, list)){
  2966. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  2967. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  2968. }
  2969. }
  2970. }
  2971. }
  2972. for(list=0; list<2; list++){
  2973. for(i=0; i<2; i++){
  2974. if(IS_DIR(mb_type, i, list)){
  2975. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  2976. mx += get_se_golomb(&s->gb);
  2977. my += get_se_golomb(&s->gb);
  2978. tprintf("final mv:%d %d\n", mx, my);
  2979. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (mx&0xFFFF) + (my<<16), 4);
  2980. }
  2981. }
  2982. }
  2983. }else{
  2984. assert(IS_8X16(mb_type));
  2985. for(list=0; list<2; list++){
  2986. if(h->ref_count[list]>0){
  2987. for(i=0; i<2; i++){
  2988. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  2989. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  2990. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  2991. }
  2992. }
  2993. }
  2994. }
  2995. for(list=0; list<2; list++){
  2996. for(i=0; i<2; i++){
  2997. if(IS_DIR(mb_type, i, list)){
  2998. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  2999. mx += get_se_golomb(&s->gb);
  3000. my += get_se_golomb(&s->gb);
  3001. tprintf("final mv:%d %d\n", mx, my);
  3002. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (mx&0xFFFF) + (my<<16), 4);
  3003. }
  3004. }
  3005. }
  3006. }
  3007. }
  3008. if(IS_INTER(mb_type))
  3009. write_back_motion(h, mb_type);
  3010. if(!IS_INTRA16x16(mb_type)){
  3011. cbp= get_ue_golomb(&s->gb);
  3012. if(cbp > 47){
  3013. fprintf(stderr, "cbp too large (%d) at %d %d\n", cbp, s->mb_x, s->mb_y);
  3014. return -1;
  3015. }
  3016. if(IS_INTRA4x4(mb_type))
  3017. cbp= golomb_to_intra4x4_cbp[cbp];
  3018. else
  3019. cbp= golomb_to_inter_cbp[cbp];
  3020. }
  3021. if(cbp || IS_INTRA16x16(mb_type)){
  3022. int i8x8, i4x4, chroma_idx;
  3023. int chroma_qp, dquant;
  3024. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  3025. const uint8_t *scan, *dc_scan;
  3026. // fill_non_zero_count_cache(h);
  3027. if(IS_INTERLACED(mb_type)){
  3028. scan= field_scan;
  3029. dc_scan= luma_dc_field_scan;
  3030. }else{
  3031. scan= zigzag_scan;
  3032. dc_scan= luma_dc_zigzag_scan;
  3033. }
  3034. dquant= get_se_golomb(&s->gb);
  3035. if( dquant > 25 || dquant < -26 ){
  3036. fprintf(stderr, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  3037. return -1;
  3038. }
  3039. s->qscale += dquant;
  3040. if(((unsigned)s->qscale) > 51){
  3041. if(s->qscale<0) s->qscale+= 52;
  3042. else s->qscale-= 52;
  3043. }
  3044. h->chroma_qp= chroma_qp= get_chroma_qp(h, s->qscale);
  3045. if(IS_INTRA16x16(mb_type)){
  3046. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, s->qscale, 16) < 0){
  3047. return -1; //FIXME continue if partotioned and other retirn -1 too
  3048. }
  3049. assert((cbp&15) == 0 || (cbp&15) == 15);
  3050. if(cbp&15){
  3051. for(i8x8=0; i8x8<4; i8x8++){
  3052. for(i4x4=0; i4x4<4; i4x4++){
  3053. const int index= i4x4 + 4*i8x8;
  3054. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, s->qscale, 15) < 0 ){
  3055. return -1;
  3056. }
  3057. }
  3058. }
  3059. }else{
  3060. memset(&h->non_zero_count_cache[8], 0, 8*4); //FIXME stupid & slow
  3061. }
  3062. }else{
  3063. for(i8x8=0; i8x8<4; i8x8++){
  3064. if(cbp & (1<<i8x8)){
  3065. for(i4x4=0; i4x4<4; i4x4++){
  3066. const int index= i4x4 + 4*i8x8;
  3067. if( decode_residual(h, gb, h->mb + 16*index, index, scan, s->qscale, 16) <0 ){
  3068. return -1;
  3069. }
  3070. }
  3071. }else{
  3072. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  3073. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  3074. }
  3075. }
  3076. }
  3077. if(cbp&0x30){
  3078. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  3079. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, chroma_qp, 4) < 0){
  3080. return -1;
  3081. }
  3082. }
  3083. if(cbp&0x20){
  3084. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  3085. for(i4x4=0; i4x4<4; i4x4++){
  3086. const int index= 16 + 4*chroma_idx + i4x4;
  3087. if( decode_residual(h, gb, h->mb + 16*index, index, scan + 1, chroma_qp, 15) < 0){
  3088. return -1;
  3089. }
  3090. }
  3091. }
  3092. }else{
  3093. uint8_t * const nnz= &h->non_zero_count_cache[0];
  3094. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  3095. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  3096. }
  3097. }else{
  3098. memset(&h->non_zero_count_cache[8], 0, 8*5);
  3099. }
  3100. write_back_non_zero_count(h);
  3101. return 0;
  3102. }
  3103. static int decode_slice(H264Context *h){
  3104. MpegEncContext * const s = &h->s;
  3105. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  3106. s->mb_skip_run= -1;
  3107. #if 1
  3108. for(;;){
  3109. int ret= decode_mb(h);
  3110. hl_decode_mb(h);
  3111. if(ret>=0 && h->sps.mb_aff){ //FIXME optimal? or let mb_decode decode 16x32 ?
  3112. s->mb_y++;
  3113. ret= decode_mb(h);
  3114. hl_decode_mb(h);
  3115. s->mb_y--;
  3116. }
  3117. if(ret<0){
  3118. fprintf(stderr, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  3119. 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);
  3120. return -1;
  3121. }
  3122. if(++s->mb_x >= s->mb_width){
  3123. s->mb_x=0;
  3124. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  3125. if(++s->mb_y >= s->mb_height){
  3126. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  3127. if(get_bits_count(&s->gb) == s->gb.size_in_bits){
  3128. 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);
  3129. return 0;
  3130. }else{
  3131. 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);
  3132. return -1;
  3133. }
  3134. }
  3135. }
  3136. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  3137. if(get_bits_count(&s->gb) == s->gb.size_in_bits){
  3138. 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);
  3139. return 0;
  3140. }else{
  3141. 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);
  3142. return -1;
  3143. }
  3144. }
  3145. }
  3146. #endif
  3147. #if 0
  3148. for(;s->mb_y < s->mb_height; s->mb_y++){
  3149. for(;s->mb_x < s->mb_width; s->mb_x++){
  3150. int ret= decode_mb(h);
  3151. hl_decode_mb(h);
  3152. if(ret<0){
  3153. fprintf(stderr, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  3154. 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);
  3155. return -1;
  3156. }
  3157. if(++s->mb_x >= s->mb_width){
  3158. s->mb_x=0;
  3159. if(++s->mb_y >= s->mb_height){
  3160. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  3161. 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);
  3162. return 0;
  3163. }else{
  3164. 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);
  3165. return -1;
  3166. }
  3167. }
  3168. }
  3169. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  3170. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  3171. 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);
  3172. return 0;
  3173. }else{
  3174. 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);
  3175. return -1;
  3176. }
  3177. }
  3178. }
  3179. s->mb_x=0;
  3180. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  3181. }
  3182. #endif
  3183. return -1; //not reached
  3184. }
  3185. static inline int decode_vui_parameters(H264Context *h, SPS *sps){
  3186. MpegEncContext * const s = &h->s;
  3187. int aspect_ratio_info_present_flag, aspect_ratio_idc;
  3188. aspect_ratio_info_present_flag= get_bits1(&s->gb);
  3189. if( aspect_ratio_info_present_flag ) {
  3190. aspect_ratio_idc= get_bits(&s->gb, 8);
  3191. if( aspect_ratio_idc == EXTENDED_SAR ) {
  3192. sps->sar_width= get_bits(&s->gb, 16);
  3193. sps->sar_height= get_bits(&s->gb, 16);
  3194. }else if(aspect_ratio_idc < 16){
  3195. sps->sar_width= pixel_aspect[aspect_ratio_idc][0];
  3196. sps->sar_height= pixel_aspect[aspect_ratio_idc][1];
  3197. }else{
  3198. fprintf(stderr, "illegal aspect ratio\n");
  3199. return -1;
  3200. }
  3201. }else{
  3202. sps->sar_width=
  3203. sps->sar_height= 0;
  3204. }
  3205. // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
  3206. #if 0
  3207. | overscan_info_present_flag |0 |u(1) |
  3208. | if( overscan_info_present_flag ) | | |
  3209. | overscan_appropriate_flag |0 |u(1) |
  3210. | video_signal_type_present_flag |0 |u(1) |
  3211. | if( video_signal_type_present_flag ) { | | |
  3212. | video_format |0 |u(3) |
  3213. | video_full_range_flag |0 |u(1) |
  3214. | colour_description_present_flag |0 |u(1) |
  3215. | if( colour_description_present_flag ) { | | |
  3216. | colour_primaries |0 |u(8) |
  3217. | transfer_characteristics |0 |u(8) |
  3218. | matrix_coefficients |0 |u(8) |
  3219. | } | | |
  3220. | } | | |
  3221. | chroma_location_info_present_flag |0 |u(1) |
  3222. | if ( chroma_location_info_present_flag ) { | | |
  3223. | chroma_sample_location_type_top_field |0 |ue(v) |
  3224. | chroma_sample_location_type_bottom_field |0 |ue(v) |
  3225. | } | | |
  3226. | timing_info_present_flag |0 |u(1) |
  3227. | if( timing_info_present_flag ) { | | |
  3228. | num_units_in_tick |0 |u(32) |
  3229. | time_scale |0 |u(32) |
  3230. | fixed_frame_rate_flag |0 |u(1) |
  3231. | } | | |
  3232. | nal_hrd_parameters_present_flag |0 |u(1) |
  3233. | if( nal_hrd_parameters_present_flag = = 1) | | |
  3234. | hrd_parameters( ) | | |
  3235. | vcl_hrd_parameters_present_flag |0 |u(1) |
  3236. | if( vcl_hrd_parameters_present_flag = = 1) | | |
  3237. | hrd_parameters( ) | | |
  3238. | if( ( nal_hrd_parameters_present_flag = = 1 | || | |
  3239. | | | |
  3240. |( vcl_hrd_parameters_present_flag = = 1 ) ) | | |
  3241. | low_delay_hrd_flag |0 |u(1) |
  3242. | bitstream_restriction_flag |0 |u(1) |
  3243. | if( bitstream_restriction_flag ) { |0 |u(1) |
  3244. | motion_vectors_over_pic_boundaries_flag |0 |u(1) |
  3245. | max_bytes_per_pic_denom |0 |ue(v) |
  3246. | max_bits_per_mb_denom |0 |ue(v) |
  3247. | log2_max_mv_length_horizontal |0 |ue(v) |
  3248. | log2_max_mv_length_vertical |0 |ue(v) |
  3249. | num_reorder_frames |0 |ue(v) |
  3250. | max_dec_frame_buffering |0 |ue(v) |
  3251. | } | | |
  3252. |} | | |
  3253. #endif
  3254. return 0;
  3255. }
  3256. static inline int decode_seq_parameter_set(H264Context *h){
  3257. MpegEncContext * const s = &h->s;
  3258. int profile_idc, level_idc, multiple_slice_groups, arbitrary_slice_order, redundant_slices;
  3259. int sps_id, i;
  3260. SPS *sps;
  3261. profile_idc= get_bits(&s->gb, 8);
  3262. level_idc= get_bits(&s->gb, 8);
  3263. multiple_slice_groups= get_bits1(&s->gb);
  3264. arbitrary_slice_order= get_bits1(&s->gb);
  3265. redundant_slices= get_bits1(&s->gb);
  3266. sps_id= get_ue_golomb(&s->gb);
  3267. sps= &h->sps_buffer[ sps_id ];
  3268. sps->profile_idc= profile_idc;
  3269. sps->level_idc= level_idc;
  3270. sps->multiple_slice_groups= multiple_slice_groups;
  3271. sps->arbitrary_slice_order= arbitrary_slice_order;
  3272. sps->redundant_slices= redundant_slices;
  3273. sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
  3274. sps->poc_type= get_ue_golomb(&s->gb);
  3275. if(sps->poc_type == 0){ //FIXME #define
  3276. sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
  3277. } else if(sps->poc_type == 1){//FIXME #define
  3278. sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
  3279. sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
  3280. sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
  3281. sps->poc_cycle_length= get_ue_golomb(&s->gb);
  3282. for(i=0; i<sps->poc_cycle_length; i++)
  3283. sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
  3284. }
  3285. if(sps->poc_type > 2){
  3286. fprintf(stderr, "illegal POC type %d\n", sps->poc_type);
  3287. return -1;
  3288. }
  3289. sps->ref_frame_count= get_ue_golomb(&s->gb);
  3290. sps->required_frame_num_update_behaviour_flag= get_bits1(&s->gb);
  3291. sps->mb_width= get_ue_golomb(&s->gb) + 1;
  3292. sps->mb_height= get_ue_golomb(&s->gb) + 1;
  3293. sps->frame_mbs_only_flag= get_bits1(&s->gb);
  3294. if(!sps->frame_mbs_only_flag)
  3295. sps->mb_aff= get_bits1(&s->gb);
  3296. else
  3297. sps->mb_aff= 0;
  3298. sps->direct_8x8_inference_flag= get_bits1(&s->gb);
  3299. sps->vui_parameters_present_flag= get_bits1(&s->gb);
  3300. if( sps->vui_parameters_present_flag )
  3301. decode_vui_parameters(h, sps);
  3302. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  3303. printf("sps:%d profile:%d/%d poc:%d ref:%d %dx%d %s %s %s\n",
  3304. sps_id, sps->profile_idc, sps->level_idc,
  3305. sps->poc_type,
  3306. sps->ref_frame_count,
  3307. sps->mb_width, sps->mb_height,
  3308. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  3309. sps->direct_8x8_inference_flag ? "8B8" : "",
  3310. sps->vui_parameters_present_flag ? "VUI" : ""
  3311. );
  3312. }
  3313. return 0;
  3314. }
  3315. static inline int decode_picture_parameter_set(H264Context *h){
  3316. MpegEncContext * const s = &h->s;
  3317. int pps_id= get_ue_golomb(&s->gb);
  3318. PPS *pps= &h->pps_buffer[pps_id];
  3319. pps->sps_id= get_ue_golomb(&s->gb);
  3320. pps->cabac= get_bits1(&s->gb);
  3321. pps->pic_order_present= get_bits1(&s->gb);
  3322. pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
  3323. if(pps->slice_group_count > 1 ){
  3324. pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
  3325. fprintf(stderr, "FMO not supported\n");
  3326. switch(pps->mb_slice_group_map_type){
  3327. case 0:
  3328. #if 0
  3329. | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
  3330. | run_length[ i ] |1 |ue(v) |
  3331. #endif
  3332. break;
  3333. case 2:
  3334. #if 0
  3335. | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
  3336. |{ | | |
  3337. | top_left_mb[ i ] |1 |ue(v) |
  3338. | bottom_right_mb[ i ] |1 |ue(v) |
  3339. | } | | |
  3340. #endif
  3341. break;
  3342. case 3:
  3343. case 4:
  3344. case 5:
  3345. #if 0
  3346. | slice_group_change_direction_flag |1 |u(1) |
  3347. | slice_group_change_rate_minus1 |1 |ue(v) |
  3348. #endif
  3349. break;
  3350. case 6:
  3351. #if 0
  3352. | slice_group_id_cnt_minus1 |1 |ue(v) |
  3353. | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
  3354. |) | | |
  3355. | slice_group_id[ i ] |1 |u(v) |
  3356. #endif
  3357. break;
  3358. }
  3359. }
  3360. pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  3361. pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  3362. if(pps->ref_count[0] > 32 || pps->ref_count[1] > 32){
  3363. fprintf(stderr, "reference overflow (pps)\n");
  3364. return -1;
  3365. }
  3366. pps->weighted_pred= get_bits1(&s->gb);
  3367. pps->weighted_bipred_idc= get_bits(&s->gb, 2);
  3368. pps->init_qp= get_se_golomb(&s->gb) + 26;
  3369. pps->init_qs= get_se_golomb(&s->gb) + 26;
  3370. pps->chroma_qp_index_offset= get_se_golomb(&s->gb);
  3371. pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
  3372. pps->constrained_intra_pred= get_bits1(&s->gb);
  3373. pps->redundant_pic_cnt_present = get_bits1(&s->gb);
  3374. pps->crop= get_bits1(&s->gb);
  3375. if(pps->crop){
  3376. pps->crop_left = get_ue_golomb(&s->gb);
  3377. pps->crop_right = get_ue_golomb(&s->gb);
  3378. pps->crop_top = get_ue_golomb(&s->gb);
  3379. pps->crop_bottom= get_ue_golomb(&s->gb);
  3380. }else{
  3381. pps->crop_left =
  3382. pps->crop_right =
  3383. pps->crop_top =
  3384. pps->crop_bottom= 0;
  3385. }
  3386. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  3387. printf("pps:%d sps:%d %s slice_groups:%d ref:%d/%d %s qp:%d/%d/%d %s %s %s crop:%d/%d/%d/%d\n",
  3388. pps_id, pps->sps_id,
  3389. pps->cabac ? "CABAC" : "CAVLC",
  3390. pps->slice_group_count,
  3391. pps->ref_count[0], pps->ref_count[1],
  3392. pps->weighted_pred ? "weighted" : "",
  3393. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset,
  3394. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  3395. pps->constrained_intra_pred ? "CONSTR" : "",
  3396. pps->redundant_pic_cnt_present ? "REDU" : "",
  3397. pps->crop_left, pps->crop_right,
  3398. pps->crop_top, pps->crop_bottom
  3399. );
  3400. }
  3401. return 0;
  3402. }
  3403. /**
  3404. * finds the end of the current frame in the bitstream.
  3405. * @return the position of the first byte of the next frame, or -1
  3406. */
  3407. static int find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
  3408. ParseContext *pc= &s->parse_context;
  3409. int i;
  3410. uint32_t state;
  3411. //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
  3412. // mb_addr= pc->mb_addr - 1;
  3413. state= pc->state;
  3414. //FIXME this will fail with slices
  3415. for(i=0; i<buf_size; i++){
  3416. state= (state<<8) | buf[i];
  3417. if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  3418. if(pc->frame_start_found){
  3419. pc->state=-1;
  3420. pc->frame_start_found= 0;
  3421. return i-3;
  3422. }
  3423. pc->frame_start_found= 1;
  3424. }
  3425. }
  3426. pc->state= state;
  3427. return END_NOT_FOUND;
  3428. }
  3429. static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){
  3430. MpegEncContext * const s = &h->s;
  3431. AVCodecContext * const avctx= s->avctx;
  3432. int buf_index=0;
  3433. int i;
  3434. #if 0
  3435. for(i=0; i<32; i++){
  3436. printf("%X ", buf[i]);
  3437. }
  3438. #endif
  3439. for(;;){
  3440. int consumed;
  3441. int dst_length;
  3442. int bit_length;
  3443. uint8_t *ptr;
  3444. // start code prefix search
  3445. for(; buf_index + 3 < buf_size; buf_index++){
  3446. // this should allways succeed in the first iteration
  3447. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  3448. break;
  3449. }
  3450. if(buf_index+3 >= buf_size) break;
  3451. buf_index+=3;
  3452. ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, buf_size - buf_index);
  3453. if(ptr[dst_length - 1] == 0) dst_length--;
  3454. bit_length= 8*dst_length - decode_rbsp_trailing(ptr + dst_length - 1);
  3455. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  3456. printf("NAL %d at %d length %d\n", h->nal_unit_type, buf_index, dst_length);
  3457. }
  3458. buf_index += consumed;
  3459. if(h->nal_ref_idc < s->hurry_up)
  3460. continue;
  3461. switch(h->nal_unit_type){
  3462. case NAL_IDR_SLICE:
  3463. idr(h); //FIXME ensure we dont loose some frames if there is reordering
  3464. case NAL_SLICE:
  3465. init_get_bits(&s->gb, ptr, bit_length);
  3466. h->intra_gb_ptr=
  3467. h->inter_gb_ptr= &s->gb;
  3468. s->data_partitioning = 0;
  3469. if(decode_slice_header(h) < 0) return -1;
  3470. if(h->redundant_pic_count==0)
  3471. decode_slice(h);
  3472. break;
  3473. case NAL_DPA:
  3474. init_get_bits(&s->gb, ptr, bit_length);
  3475. h->intra_gb_ptr=
  3476. h->inter_gb_ptr= NULL;
  3477. s->data_partitioning = 1;
  3478. if(decode_slice_header(h) < 0) return -1;
  3479. break;
  3480. case NAL_DPB:
  3481. init_get_bits(&h->intra_gb, ptr, bit_length);
  3482. h->intra_gb_ptr= &h->intra_gb;
  3483. break;
  3484. case NAL_DPC:
  3485. init_get_bits(&h->inter_gb, ptr, bit_length);
  3486. h->inter_gb_ptr= &h->inter_gb;
  3487. if(h->redundant_pic_count==0 && h->intra_gb_ptr && s->data_partitioning)
  3488. decode_slice(h);
  3489. break;
  3490. case NAL_SEI:
  3491. break;
  3492. case NAL_SPS:
  3493. init_get_bits(&s->gb, ptr, bit_length);
  3494. decode_seq_parameter_set(h);
  3495. if(s->flags& CODEC_FLAG_LOW_DELAY)
  3496. s->low_delay=1;
  3497. avctx->has_b_frames= !s->low_delay;
  3498. break;
  3499. case NAL_PPS:
  3500. init_get_bits(&s->gb, ptr, bit_length);
  3501. decode_picture_parameter_set(h);
  3502. break;
  3503. case NAL_PICTURE_DELIMITER:
  3504. break;
  3505. case NAL_FILTER_DATA:
  3506. break;
  3507. }
  3508. //FIXME move after where irt is set
  3509. s->current_picture.pict_type= s->pict_type;
  3510. s->current_picture.key_frame= s->pict_type == I_TYPE;
  3511. }
  3512. if(!s->current_picture_ptr) return buf_index; //no frame
  3513. h->prev_frame_num_offset= h->frame_num_offset;
  3514. h->prev_frame_num= h->frame_num;
  3515. if(s->current_picture_ptr->reference){
  3516. h->prev_poc_msb= h->poc_msb;
  3517. h->prev_poc_lsb= h->poc_lsb;
  3518. }
  3519. if(s->current_picture_ptr->reference)
  3520. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  3521. else
  3522. assert(h->mmco_index==0);
  3523. ff_er_frame_end(s);
  3524. MPV_frame_end(s);
  3525. return buf_index;
  3526. }
  3527. /**
  3528. * retunrs the number of bytes consumed for building the current frame
  3529. */
  3530. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  3531. if(s->flags&CODEC_FLAG_TRUNCATED){
  3532. pos -= s->parse_context.last_index;
  3533. if(pos<0) pos=0; // FIXME remove (uneeded?)
  3534. return pos;
  3535. }else{
  3536. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  3537. if(pos+10>buf_size) pos=buf_size; // oops ;)
  3538. return pos;
  3539. }
  3540. }
  3541. static int decode_frame(AVCodecContext *avctx,
  3542. void *data, int *data_size,
  3543. uint8_t *buf, int buf_size)
  3544. {
  3545. H264Context *h = avctx->priv_data;
  3546. MpegEncContext *s = &h->s;
  3547. AVFrame *pict = data;
  3548. int buf_index;
  3549. s->flags= avctx->flags;
  3550. *data_size = 0;
  3551. /* no supplementary picture */
  3552. if (buf_size == 0) {
  3553. return 0;
  3554. }
  3555. if(s->flags&CODEC_FLAG_TRUNCATED){
  3556. int next= find_frame_end(s, buf, buf_size);
  3557. if( ff_combine_frame(s, next, &buf, &buf_size) < 0 )
  3558. return buf_size;
  3559. //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);
  3560. }
  3561. if(s->avctx->extradata_size && s->picture_number==0){
  3562. if(0 < decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) )
  3563. return -1;
  3564. }
  3565. buf_index=decode_nal_units(h, buf, buf_size);
  3566. if(buf_index < 0)
  3567. return -1;
  3568. //FIXME do something with unavailable reference frames
  3569. // if(ret==FRAME_SKIPED) return get_consumed_bytes(s, buf_index, buf_size);
  3570. #if 0
  3571. if(s->pict_type==B_TYPE || s->low_delay){
  3572. *pict= *(AVFrame*)&s->current_picture;
  3573. } else {
  3574. *pict= *(AVFrame*)&s->last_picture;
  3575. }
  3576. #endif
  3577. if(!s->current_picture_ptr){
  3578. fprintf(stderr, "error, NO frame\n");
  3579. return -1;
  3580. }
  3581. *pict= *(AVFrame*)&s->current_picture; //FIXME
  3582. ff_print_debug_info(s, s->current_picture_ptr);
  3583. assert(pict->data[0]);
  3584. //printf("out %d\n", (int)pict->data[0]);
  3585. #if 0 //?
  3586. /* Return the Picture timestamp as the frame number */
  3587. /* we substract 1 because it is added on utils.c */
  3588. avctx->frame_number = s->picture_number - 1;
  3589. #endif
  3590. #if 0
  3591. /* dont output the last pic after seeking */
  3592. if(s->last_picture_ptr || s->low_delay)
  3593. //Note this isnt a issue as a IDR pic should flush teh buffers
  3594. #endif
  3595. *data_size = sizeof(AVFrame);
  3596. return get_consumed_bytes(s, buf_index, buf_size);
  3597. }
  3598. #if 0
  3599. static inline void fill_mb_avail(H264Context *h){
  3600. MpegEncContext * const s = &h->s;
  3601. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  3602. if(s->mb_y){
  3603. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  3604. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  3605. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  3606. }else{
  3607. h->mb_avail[0]=
  3608. h->mb_avail[1]=
  3609. h->mb_avail[2]= 0;
  3610. }
  3611. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  3612. h->mb_avail[4]= 1; //FIXME move out
  3613. h->mb_avail[5]= 0; //FIXME move out
  3614. }
  3615. #endif
  3616. #if 0 //selftest
  3617. #define COUNT 8000
  3618. #define SIZE (COUNT*40)
  3619. int main(){
  3620. int i;
  3621. uint8_t temp[SIZE];
  3622. PutBitContext pb;
  3623. GetBitContext gb;
  3624. // int int_temp[10000];
  3625. DSPContext dsp;
  3626. AVCodecContext avctx;
  3627. dsputil_init(&dsp, &avctx);
  3628. init_put_bits(&pb, temp, SIZE, NULL, NULL);
  3629. printf("testing unsigned exp golomb\n");
  3630. for(i=0; i<COUNT; i++){
  3631. START_TIMER
  3632. set_ue_golomb(&pb, i);
  3633. STOP_TIMER("set_ue_golomb");
  3634. }
  3635. flush_put_bits(&pb);
  3636. init_get_bits(&gb, temp, 8*SIZE);
  3637. for(i=0; i<COUNT; i++){
  3638. int j, s;
  3639. s= show_bits(&gb, 24);
  3640. START_TIMER
  3641. j= get_ue_golomb(&gb);
  3642. if(j != i){
  3643. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  3644. // return -1;
  3645. }
  3646. STOP_TIMER("get_ue_golomb");
  3647. }
  3648. init_put_bits(&pb, temp, SIZE, NULL, NULL);
  3649. printf("testing signed exp golomb\n");
  3650. for(i=0; i<COUNT; i++){
  3651. START_TIMER
  3652. set_se_golomb(&pb, i - COUNT/2);
  3653. STOP_TIMER("set_se_golomb");
  3654. }
  3655. flush_put_bits(&pb);
  3656. init_get_bits(&gb, temp, 8*SIZE);
  3657. for(i=0; i<COUNT; i++){
  3658. int j, s;
  3659. s= show_bits(&gb, 24);
  3660. START_TIMER
  3661. j= get_se_golomb(&gb);
  3662. if(j != i - COUNT/2){
  3663. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  3664. // return -1;
  3665. }
  3666. STOP_TIMER("get_se_golomb");
  3667. }
  3668. printf("testing 4x4 (I)DCT\n");
  3669. DCTELEM block[16];
  3670. uint8_t src[16], ref[16];
  3671. uint64_t error= 0, max_error=0;
  3672. for(i=0; i<COUNT; i++){
  3673. int j;
  3674. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  3675. for(j=0; j<16; j++){
  3676. ref[j]= random()%255;
  3677. src[j]= random()%255;
  3678. }
  3679. h264_diff_dct_c(block, src, ref, 4);
  3680. //normalize
  3681. for(j=0; j<16; j++){
  3682. // printf("%d ", block[j]);
  3683. block[j]= block[j]*4;
  3684. if(j&1) block[j]= (block[j]*4 + 2)/5;
  3685. if(j&4) block[j]= (block[j]*4 + 2)/5;
  3686. }
  3687. // printf("\n");
  3688. h264_add_idct_c(ref, block, 4);
  3689. /* for(j=0; j<16; j++){
  3690. printf("%d ", ref[j]);
  3691. }
  3692. printf("\n");*/
  3693. for(j=0; j<16; j++){
  3694. int diff= ABS(src[j] - ref[j]);
  3695. error+= diff*diff;
  3696. max_error= FFMAX(max_error, diff);
  3697. }
  3698. }
  3699. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  3700. #if 0
  3701. printf("testing quantizer\n");
  3702. for(qp=0; qp<52; qp++){
  3703. for(i=0; i<16; i++)
  3704. src1_block[i]= src2_block[i]= random()%255;
  3705. }
  3706. #endif
  3707. printf("Testing NAL layer\n");
  3708. uint8_t bitstream[COUNT];
  3709. uint8_t nal[COUNT*2];
  3710. H264Context h;
  3711. memset(&h, 0, sizeof(H264Context));
  3712. for(i=0; i<COUNT; i++){
  3713. int zeros= i;
  3714. int nal_length;
  3715. int consumed;
  3716. int out_length;
  3717. uint8_t *out;
  3718. int j;
  3719. for(j=0; j<COUNT; j++){
  3720. bitstream[j]= (random() % 255) + 1;
  3721. }
  3722. for(j=0; j<zeros; j++){
  3723. int pos= random() % COUNT;
  3724. while(bitstream[pos] == 0){
  3725. pos++;
  3726. pos %= COUNT;
  3727. }
  3728. bitstream[pos]=0;
  3729. }
  3730. START_TIMER
  3731. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  3732. if(nal_length<0){
  3733. printf("encoding failed\n");
  3734. return -1;
  3735. }
  3736. out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
  3737. STOP_TIMER("NAL")
  3738. if(out_length != COUNT){
  3739. printf("incorrect length %d %d\n", out_length, COUNT);
  3740. return -1;
  3741. }
  3742. if(consumed != nal_length){
  3743. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  3744. return -1;
  3745. }
  3746. if(memcmp(bitstream, out, COUNT)){
  3747. printf("missmatch\n");
  3748. return -1;
  3749. }
  3750. }
  3751. printf("Testing RBSP\n");
  3752. return 0;
  3753. }
  3754. #endif
  3755. static int decode_end(AVCodecContext *avctx)
  3756. {
  3757. H264Context *h = avctx->priv_data;
  3758. MpegEncContext *s = &h->s;
  3759. free_tables(h); //FIXME cleanup init stuff perhaps
  3760. MPV_common_end(s);
  3761. // memset(h, 0, sizeof(H264Context));
  3762. return 0;
  3763. }
  3764. AVCodec h264_decoder = {
  3765. "h264",
  3766. CODEC_TYPE_VIDEO,
  3767. CODEC_ID_H264,
  3768. sizeof(H264Context),
  3769. decode_init,
  3770. NULL,
  3771. decode_end,
  3772. decode_frame,
  3773. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  3774. };
  3775. #include "svq3.c"