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.

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