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.

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