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.

4421 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 log2_max_frame_num; ///< log2_max_frame_num_minus4 + 4
  53. int poc_type; ///< pic_order_cnt_type
  54. int log2_max_poc_lsb; ///< log2_max_pic_order_cnt_lsb_minus4
  55. int delta_pic_order_always_zero_flag;
  56. int offset_for_non_ref_pic;
  57. int offset_for_top_to_bottom_field;
  58. int poc_cycle_length; ///< num_ref_frames_in_pic_order_cnt_cycle
  59. int ref_frame_count; ///< num_ref_frames
  60. int gaps_in_frame_num_allowed_flag;
  61. int mb_width; ///< frame_width_in_mbs_minus1 + 1
  62. int mb_height; ///< frame_height_in_mbs_minus1 + 1
  63. int frame_mbs_only_flag;
  64. int mb_aff; ///<mb_adaptive_frame_field_flag
  65. int direct_8x8_inference_flag;
  66. int crop; ///< frame_cropping_flag
  67. int crop_left; ///< frame_cropping_rect_left_offset
  68. int crop_right; ///< frame_cropping_rect_right_offset
  69. int crop_top; ///< frame_cropping_rect_top_offset
  70. int crop_bottom; ///< frame_cropping_rect_bottom_offset
  71. int vui_parameters_present_flag;
  72. int sar_width;
  73. int sar_height;
  74. short offset_for_ref_frame[256]; //FIXME dyn aloc?
  75. }SPS;
  76. /**
  77. * Picture parameter set
  78. */
  79. typedef struct PPS{
  80. int sps_id;
  81. int cabac; ///< entropy_coding_mode_flag
  82. int pic_order_present; ///< pic_order_present_flag
  83. int slice_group_count; ///< num_slice_groups_minus1 + 1
  84. int mb_slice_group_map_type;
  85. int ref_count[2]; ///< num_ref_idx_l0/1_active_minus1 + 1
  86. int weighted_pred; ///< weighted_pred_flag
  87. int weighted_bipred_idc;
  88. int init_qp; ///< pic_init_qp_minus26 + 26
  89. int init_qs; ///< pic_init_qs_minus26 + 26
  90. int chroma_qp_index_offset;
  91. int deblocking_filter_parameters_present; ///< deblocking_filter_parameters_present_flag
  92. int constrained_intra_pred; ///< constrained_intra_pred_flag
  93. int redundant_pic_cnt_present; ///< redundant_pic_cnt_present_flag
  94. }PPS;
  95. /**
  96. * Memory management control operation opcode.
  97. */
  98. typedef enum MMCOOpcode{
  99. MMCO_END=0,
  100. MMCO_SHORT2UNUSED,
  101. MMCO_LONG2UNUSED,
  102. MMCO_SHORT2LONG,
  103. MMCO_SET_MAX_LONG,
  104. MMCO_RESET,
  105. MMCO_LONG,
  106. } MMCOOpcode;
  107. /**
  108. * Memory management control operation.
  109. */
  110. typedef struct MMCO{
  111. MMCOOpcode opcode;
  112. int short_frame_num;
  113. int long_index;
  114. } MMCO;
  115. /**
  116. * H264Context
  117. */
  118. typedef struct H264Context{
  119. MpegEncContext s;
  120. int nal_ref_idc;
  121. int nal_unit_type;
  122. #define NAL_SLICE 1
  123. #define NAL_DPA 2
  124. #define NAL_DPB 3
  125. #define NAL_DPC 4
  126. #define NAL_IDR_SLICE 5
  127. #define NAL_SEI 6
  128. #define NAL_SPS 7
  129. #define NAL_PPS 8
  130. #define NAL_PICTURE_DELIMITER 9
  131. #define NAL_FILTER_DATA 10
  132. uint8_t *rbsp_buffer;
  133. int rbsp_buffer_size;
  134. int chroma_qp; //QPc
  135. int prev_mb_skiped; //FIXME remove (IMHO not used)
  136. //prediction stuff
  137. int chroma_pred_mode;
  138. int intra16x16_pred_mode;
  139. int8_t intra4x4_pred_mode_cache[5*8];
  140. int8_t (*intra4x4_pred_mode)[8];
  141. void (*pred4x4 [9+3])(uint8_t *src, uint8_t *topright, int stride);//FIXME move to dsp?
  142. void (*pred8x8 [4+3])(uint8_t *src, int stride);
  143. void (*pred16x16[4+3])(uint8_t *src, int stride);
  144. unsigned int topleft_samples_available;
  145. unsigned int top_samples_available;
  146. unsigned int topright_samples_available;
  147. unsigned int left_samples_available;
  148. /**
  149. * non zero coeff count cache.
  150. * is 64 if not available.
  151. */
  152. uint8_t non_zero_count_cache[6*8];
  153. uint8_t (*non_zero_count)[16];
  154. /**
  155. * Motion vector cache.
  156. */
  157. int16_t mv_cache[2][5*8][2];
  158. int8_t ref_cache[2][5*8];
  159. #define LIST_NOT_USED -1 //FIXME rename?
  160. #define PART_NOT_AVAILABLE -2
  161. /**
  162. * is 1 if the specific list MV&references are set to 0,0,-2.
  163. */
  164. int mv_cache_clean[2];
  165. int block_offset[16+8];
  166. int chroma_subblock_offset[16]; //FIXME remove
  167. uint16_t *mb2b_xy; //FIXME are these 4 a good idea?
  168. uint16_t *mb2b8_xy;
  169. int b_stride;
  170. int b8_stride;
  171. int halfpel_flag;
  172. int thirdpel_flag;
  173. int unknown_svq3_flag;
  174. int next_slice_index;
  175. SPS sps_buffer[MAX_SPS_COUNT];
  176. SPS sps; ///< current sps
  177. PPS pps_buffer[MAX_PPS_COUNT];
  178. /**
  179. * current pps
  180. */
  181. PPS pps; //FIXME move tp Picture perhaps? (->no) do we need that?
  182. int slice_num;
  183. uint8_t *slice_table_base;
  184. uint8_t *slice_table; ///< slice_table_base + mb_stride + 1
  185. int slice_type;
  186. int slice_type_fixed;
  187. //interlacing specific flags
  188. int mb_field_decoding_flag;
  189. int sub_mb_type[4];
  190. //POC stuff
  191. int poc_lsb;
  192. int poc_msb;
  193. int delta_poc_bottom;
  194. int delta_poc[2];
  195. int frame_num;
  196. int prev_poc_msb; ///< poc_msb of the last reference pic for POC type 0
  197. int prev_poc_lsb; ///< poc_lsb of the last reference pic for POC type 0
  198. int frame_num_offset; ///< for POC type 2
  199. int prev_frame_num_offset; ///< for POC type 2
  200. int prev_frame_num; ///< frame_num of the last pic for POC type 1/2
  201. /**
  202. * frame_num for frames or 2*frame_num for field pics.
  203. */
  204. int curr_pic_num;
  205. /**
  206. * max_frame_num or 2*max_frame_num for field pics.
  207. */
  208. int max_pic_num;
  209. //Weighted pred stuff
  210. int luma_log2_weight_denom;
  211. int chroma_log2_weight_denom;
  212. int luma_weight[2][16];
  213. int luma_offset[2][16];
  214. int chroma_weight[2][16][2];
  215. int chroma_offset[2][16][2];
  216. //deblock
  217. int disable_deblocking_filter_idc;
  218. int slice_alpha_c0_offset_div2;
  219. int slice_beta_offset_div2;
  220. int redundant_pic_count;
  221. int direct_spatial_mv_pred;
  222. /**
  223. * num_ref_idx_l0/1_active_minus1 + 1
  224. */
  225. int ref_count[2];// FIXME split for AFF
  226. Picture *short_ref[16];
  227. Picture *long_ref[16];
  228. Picture default_ref_list[2][32];
  229. Picture ref_list[2][32]; //FIXME size?
  230. Picture field_ref_list[2][32]; //FIXME size?
  231. /**
  232. * memory management control operations buffer.
  233. */
  234. MMCO mmco[MAX_MMCO_COUNT];
  235. int mmco_index;
  236. int long_ref_count; ///< number of actual long term references
  237. int short_ref_count; ///< number of actual short term references
  238. //data partitioning
  239. GetBitContext intra_gb;
  240. GetBitContext inter_gb;
  241. GetBitContext *intra_gb_ptr;
  242. GetBitContext *inter_gb_ptr;
  243. DCTELEM mb[16*24] __align8;
  244. }H264Context;
  245. static VLC coeff_token_vlc[4];
  246. static VLC chroma_dc_coeff_token_vlc;
  247. static VLC total_zeros_vlc[15];
  248. static VLC chroma_dc_total_zeros_vlc[3];
  249. static VLC run_vlc[6];
  250. static VLC run7_vlc;
  251. static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
  252. static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
  253. static inline uint32_t pack16to32(int a, int b){
  254. #ifdef WORDS_BIGENDIAN
  255. return (b&0xFFFF) + (a<<16);
  256. #else
  257. return (a&0xFFFF) + (b<<16);
  258. #endif
  259. }
  260. /**
  261. * fill a rectangle.
  262. * @param h height of the recatangle, should be a constant
  263. * @param w width of the recatangle, should be a constant
  264. * @param size the size of val (1 or 4), should be a constant
  265. */
  266. static inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){ //FIXME ensure this IS inlined
  267. uint8_t *p= (uint8_t*)vp;
  268. assert(size==1 || size==4);
  269. w *= size;
  270. stride *= size;
  271. //FIXME check what gcc generates for 64 bit on x86 and possible write a 32 bit ver of it
  272. if(w==2 && h==2){
  273. *(uint16_t*)(p + 0)=
  274. *(uint16_t*)(p + stride)= size==4 ? val : val*0x0101;
  275. }else if(w==2 && h==4){
  276. *(uint16_t*)(p + 0*stride)=
  277. *(uint16_t*)(p + 1*stride)=
  278. *(uint16_t*)(p + 2*stride)=
  279. *(uint16_t*)(p + 3*stride)= size==4 ? val : val*0x0101;
  280. }else if(w==4 && h==1){
  281. *(uint32_t*)(p + 0*stride)= size==4 ? val : val*0x01010101;
  282. }else if(w==4 && h==2){
  283. *(uint32_t*)(p + 0*stride)=
  284. *(uint32_t*)(p + 1*stride)= size==4 ? val : val*0x01010101;
  285. }else if(w==4 && h==4){
  286. *(uint32_t*)(p + 0*stride)=
  287. *(uint32_t*)(p + 1*stride)=
  288. *(uint32_t*)(p + 2*stride)=
  289. *(uint32_t*)(p + 3*stride)= size==4 ? val : val*0x01010101;
  290. }else if(w==8 && h==1){
  291. *(uint32_t*)(p + 0)=
  292. *(uint32_t*)(p + 4)= size==4 ? val : val*0x01010101;
  293. }else if(w==8 && h==2){
  294. *(uint32_t*)(p + 0 + 0*stride)=
  295. *(uint32_t*)(p + 4 + 0*stride)=
  296. *(uint32_t*)(p + 0 + 1*stride)=
  297. *(uint32_t*)(p + 4 + 1*stride)= size==4 ? val : val*0x01010101;
  298. }else if(w==8 && h==4){
  299. *(uint64_t*)(p + 0*stride)=
  300. *(uint64_t*)(p + 1*stride)=
  301. *(uint64_t*)(p + 2*stride)=
  302. *(uint64_t*)(p + 3*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
  303. }else if(w==16 && h==2){
  304. *(uint64_t*)(p + 0+0*stride)=
  305. *(uint64_t*)(p + 8+0*stride)=
  306. *(uint64_t*)(p + 0+1*stride)=
  307. *(uint64_t*)(p + 8+1*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
  308. }else if(w==16 && h==4){
  309. *(uint64_t*)(p + 0+0*stride)=
  310. *(uint64_t*)(p + 8+0*stride)=
  311. *(uint64_t*)(p + 0+1*stride)=
  312. *(uint64_t*)(p + 8+1*stride)=
  313. *(uint64_t*)(p + 0+2*stride)=
  314. *(uint64_t*)(p + 8+2*stride)=
  315. *(uint64_t*)(p + 0+3*stride)=
  316. *(uint64_t*)(p + 8+3*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
  317. }else
  318. assert(0);
  319. }
  320. static inline void fill_caches(H264Context *h, int mb_type){
  321. MpegEncContext * const s = &h->s;
  322. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  323. int topleft_xy, top_xy, topright_xy, left_xy[2];
  324. int topleft_type, top_type, topright_type, left_type[2];
  325. int left_block[4];
  326. int i;
  327. //wow what a mess, why didnt they simplify the interlacing&intra stuff, i cant imagine that these complex rules are worth it
  328. if(h->sps.mb_aff){
  329. //FIXME
  330. topleft_xy = 0; /* avoid warning */
  331. top_xy = 0; /* avoid warning */
  332. topright_xy = 0; /* avoid warning */
  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. static void free_tables(H264Context *h){
  1823. av_freep(&h->intra4x4_pred_mode);
  1824. av_freep(&h->non_zero_count);
  1825. av_freep(&h->slice_table_base);
  1826. h->slice_table= NULL;
  1827. av_freep(&h->mb2b_xy);
  1828. av_freep(&h->mb2b8_xy);
  1829. }
  1830. /**
  1831. * allocates tables.
  1832. * needs widzh/height
  1833. */
  1834. static int alloc_tables(H264Context *h){
  1835. MpegEncContext * const s = &h->s;
  1836. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  1837. int x,y;
  1838. CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
  1839. CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
  1840. CHECKED_ALLOCZ(h->slice_table_base , big_mb_num * sizeof(uint8_t))
  1841. memset(h->slice_table_base, -1, big_mb_num * sizeof(uint8_t));
  1842. h->slice_table= h->slice_table_base + s->mb_stride + 1;
  1843. CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint16_t));
  1844. CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint16_t));
  1845. for(y=0; y<s->mb_height; y++){
  1846. for(x=0; x<s->mb_width; x++){
  1847. const int mb_xy= x + y*s->mb_stride;
  1848. const int b_xy = 4*x + 4*y*h->b_stride;
  1849. const int b8_xy= 2*x + 2*y*h->b8_stride;
  1850. h->mb2b_xy [mb_xy]= b_xy;
  1851. h->mb2b8_xy[mb_xy]= b8_xy;
  1852. }
  1853. }
  1854. return 0;
  1855. fail:
  1856. free_tables(h);
  1857. return -1;
  1858. }
  1859. static void common_init(H264Context *h){
  1860. MpegEncContext * const s = &h->s;
  1861. s->width = s->avctx->width;
  1862. s->height = s->avctx->height;
  1863. s->codec_id= s->avctx->codec->id;
  1864. init_pred_ptrs(h);
  1865. s->decode=1; //FIXME
  1866. }
  1867. static int decode_init(AVCodecContext *avctx){
  1868. H264Context *h= avctx->priv_data;
  1869. MpegEncContext * const s = &h->s;
  1870. s->avctx = avctx;
  1871. common_init(h);
  1872. s->out_format = FMT_H264;
  1873. s->workaround_bugs= avctx->workaround_bugs;
  1874. // set defaults
  1875. s->progressive_sequence=1;
  1876. // s->decode_mb= ff_h263_decode_mb;
  1877. s->low_delay= 1;
  1878. avctx->pix_fmt= PIX_FMT_YUV420P;
  1879. decode_init_vlc(h);
  1880. return 0;
  1881. }
  1882. static void frame_start(H264Context *h){
  1883. MpegEncContext * const s = &h->s;
  1884. int i;
  1885. MPV_frame_start(s, s->avctx);
  1886. ff_er_frame_start(s);
  1887. h->mmco_index=0;
  1888. assert(s->linesize && s->uvlinesize);
  1889. for(i=0; i<16; i++){
  1890. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  1891. h->chroma_subblock_offset[i]= 2*((scan8[i] - scan8[0])&7) + 2*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  1892. }
  1893. for(i=0; i<4; i++){
  1894. h->block_offset[16+i]=
  1895. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  1896. }
  1897. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  1898. }
  1899. static void hl_decode_mb(H264Context *h){
  1900. MpegEncContext * const s = &h->s;
  1901. const int mb_x= s->mb_x;
  1902. const int mb_y= s->mb_y;
  1903. const int mb_xy= mb_x + mb_y*s->mb_stride;
  1904. const int mb_type= s->current_picture.mb_type[mb_xy];
  1905. uint8_t *dest_y, *dest_cb, *dest_cr;
  1906. int linesize, uvlinesize /*dct_offset*/;
  1907. int i;
  1908. if(!s->decode)
  1909. return;
  1910. if(s->mb_skiped){
  1911. }
  1912. dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  1913. dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  1914. dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  1915. if (h->mb_field_decoding_flag) {
  1916. linesize = s->linesize * 2;
  1917. uvlinesize = s->uvlinesize * 2;
  1918. if(mb_y&1){ //FIXME move out of this func?
  1919. dest_y -= s->linesize*15;
  1920. dest_cb-= s->linesize*7;
  1921. dest_cr-= s->linesize*7;
  1922. }
  1923. } else {
  1924. linesize = s->linesize;
  1925. uvlinesize = s->uvlinesize;
  1926. // dct_offset = s->linesize * 16;
  1927. }
  1928. if(IS_INTRA(mb_type)){
  1929. if(!(s->flags&CODEC_FLAG_GRAY)){
  1930. h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  1931. h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  1932. }
  1933. if(IS_INTRA4x4(mb_type)){
  1934. if(!s->encoding){
  1935. for(i=0; i<16; i++){
  1936. uint8_t * const ptr= dest_y + h->block_offset[i];
  1937. uint8_t *topright= ptr + 4 - linesize;
  1938. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  1939. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  1940. int tr;
  1941. if(!topright_avail){
  1942. tr= ptr[3 - linesize]*0x01010101;
  1943. topright= (uint8_t*) &tr;
  1944. }
  1945. h->pred4x4[ dir ](ptr, topright, linesize);
  1946. if(h->non_zero_count_cache[ scan8[i] ]){
  1947. if(s->codec_id == CODEC_ID_H264)
  1948. h264_add_idct_c(ptr, h->mb + i*16, linesize);
  1949. else
  1950. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  1951. }
  1952. }
  1953. }
  1954. }else{
  1955. h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  1956. if(s->codec_id == CODEC_ID_H264)
  1957. h264_luma_dc_dequant_idct_c(h->mb, s->qscale);
  1958. else
  1959. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  1960. }
  1961. }else if(s->codec_id == CODEC_ID_H264){
  1962. hl_motion(h, dest_y, dest_cb, dest_cr,
  1963. s->dsp.put_h264_qpel_pixels_tab, s->dsp.put_h264_chroma_pixels_tab,
  1964. s->dsp.avg_h264_qpel_pixels_tab, s->dsp.avg_h264_chroma_pixels_tab);
  1965. }
  1966. if(!IS_INTRA4x4(mb_type)){
  1967. if(s->codec_id == CODEC_ID_H264){
  1968. for(i=0; i<16; i++){
  1969. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  1970. uint8_t * const ptr= dest_y + h->block_offset[i];
  1971. h264_add_idct_c(ptr, h->mb + i*16, linesize);
  1972. }
  1973. }
  1974. }else{
  1975. for(i=0; i<16; i++){
  1976. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  1977. uint8_t * const ptr= dest_y + h->block_offset[i];
  1978. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  1979. }
  1980. }
  1981. }
  1982. }
  1983. if(!(s->flags&CODEC_FLAG_GRAY)){
  1984. chroma_dc_dequant_idct_c(h->mb + 16*16, h->chroma_qp);
  1985. chroma_dc_dequant_idct_c(h->mb + 16*16+4*16, h->chroma_qp);
  1986. if(s->codec_id == CODEC_ID_H264){
  1987. for(i=16; i<16+4; i++){
  1988. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  1989. uint8_t * const ptr= dest_cb + h->block_offset[i];
  1990. h264_add_idct_c(ptr, h->mb + i*16, uvlinesize);
  1991. }
  1992. }
  1993. for(i=20; i<20+4; i++){
  1994. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  1995. uint8_t * const ptr= dest_cr + h->block_offset[i];
  1996. h264_add_idct_c(ptr, h->mb + i*16, uvlinesize);
  1997. }
  1998. }
  1999. }else{
  2000. for(i=16; i<16+4; i++){
  2001. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  2002. uint8_t * const ptr= dest_cb + h->block_offset[i];
  2003. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  2004. }
  2005. }
  2006. for(i=20; i<20+4; i++){
  2007. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  2008. uint8_t * const ptr= dest_cr + h->block_offset[i];
  2009. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  2010. }
  2011. }
  2012. }
  2013. }
  2014. }
  2015. static void decode_mb_cabac(H264Context *h){
  2016. // MpegEncContext * const s = &h->s;
  2017. }
  2018. /**
  2019. * fills the default_ref_list.
  2020. */
  2021. static int fill_default_ref_list(H264Context *h){
  2022. MpegEncContext * const s = &h->s;
  2023. int i;
  2024. Picture sorted_short_ref[16];
  2025. if(h->slice_type==B_TYPE){
  2026. int out_i;
  2027. int limit= -1;
  2028. for(out_i=0; out_i<h->short_ref_count; out_i++){
  2029. int best_i=-1;
  2030. int best_poc=-1;
  2031. for(i=0; i<h->short_ref_count; i++){
  2032. const int poc= h->short_ref[i]->poc;
  2033. if(poc > limit && poc < best_poc){
  2034. best_poc= poc;
  2035. best_i= i;
  2036. }
  2037. }
  2038. assert(best_i != -1);
  2039. limit= best_poc;
  2040. sorted_short_ref[out_i]= *h->short_ref[best_i];
  2041. }
  2042. }
  2043. if(s->picture_structure == PICT_FRAME){
  2044. if(h->slice_type==B_TYPE){
  2045. const int current_poc= s->current_picture_ptr->poc;
  2046. int list;
  2047. for(list=0; list<2; list++){
  2048. int index=0;
  2049. for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++){
  2050. const int i2= list ? h->short_ref_count - i - 1 : i;
  2051. const int poc= sorted_short_ref[i2].poc;
  2052. if(sorted_short_ref[i2].reference != 3) continue; //FIXME refernce field shit
  2053. if((list==1 && poc > current_poc) || (list==0 && poc < current_poc)){
  2054. h->default_ref_list[list][index ]= sorted_short_ref[i2];
  2055. h->default_ref_list[list][index++].pic_id= sorted_short_ref[i2].frame_num;
  2056. }
  2057. }
  2058. for(i=0; i<h->long_ref_count && index < h->ref_count[ list ]; i++){
  2059. if(h->long_ref[i]->reference != 3) continue;
  2060. h->default_ref_list[ list ][index ]= *h->long_ref[i];
  2061. h->default_ref_list[ list ][index++].pic_id= i;;
  2062. }
  2063. if(h->long_ref_count > 1 && h->short_ref_count==0){
  2064. Picture temp= h->default_ref_list[1][0];
  2065. h->default_ref_list[1][0] = h->default_ref_list[1][1];
  2066. h->default_ref_list[1][0] = temp;
  2067. }
  2068. if(index < h->ref_count[ list ])
  2069. memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
  2070. }
  2071. }else{
  2072. int index=0;
  2073. for(i=0; i<h->short_ref_count && index < h->ref_count[0]; i++){
  2074. if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
  2075. h->default_ref_list[0][index ]= *h->short_ref[i];
  2076. h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
  2077. }
  2078. for(i=0; i<h->long_ref_count && index < h->ref_count[0]; i++){
  2079. if(h->long_ref[i]->reference != 3) continue;
  2080. h->default_ref_list[0][index ]= *h->long_ref[i];
  2081. h->default_ref_list[0][index++].pic_id= i;;
  2082. }
  2083. if(index < h->ref_count[0])
  2084. memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
  2085. }
  2086. }else{ //FIELD
  2087. if(h->slice_type==B_TYPE){
  2088. }else{
  2089. //FIXME second field balh
  2090. }
  2091. }
  2092. return 0;
  2093. }
  2094. static int decode_ref_pic_list_reordering(H264Context *h){
  2095. MpegEncContext * const s = &h->s;
  2096. int list;
  2097. if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move beofre func
  2098. for(list=0; list<2; list++){
  2099. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  2100. if(get_bits1(&s->gb)){
  2101. int pred= h->curr_pic_num;
  2102. int index;
  2103. for(index=0; ; index++){
  2104. int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
  2105. int pic_id;
  2106. int i;
  2107. if(index >= h->ref_count[list]){
  2108. fprintf(stderr, "reference count overflow\n");
  2109. return -1;
  2110. }
  2111. if(reordering_of_pic_nums_idc<3){
  2112. if(reordering_of_pic_nums_idc<2){
  2113. const int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  2114. if(abs_diff_pic_num >= h->max_pic_num){
  2115. fprintf(stderr, "abs_diff_pic_num overflow\n");
  2116. return -1;
  2117. }
  2118. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  2119. else pred+= abs_diff_pic_num;
  2120. pred &= h->max_pic_num - 1;
  2121. for(i= h->ref_count[list]-1; i>=index; i--){
  2122. if(h->ref_list[list][i].pic_id == pred && h->ref_list[list][i].long_ref==0)
  2123. break;
  2124. }
  2125. }else{
  2126. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  2127. for(i= h->ref_count[list]-1; i>=index; i--){
  2128. if(h->ref_list[list][i].pic_id == pic_id && h->ref_list[list][i].long_ref==1)
  2129. break;
  2130. }
  2131. }
  2132. if(i < index){
  2133. fprintf(stderr, "reference picture missing during reorder\n");
  2134. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  2135. }else if(i > index){
  2136. Picture tmp= h->ref_list[list][i];
  2137. for(; i>index; i--){
  2138. h->ref_list[list][i]= h->ref_list[list][i-1];
  2139. }
  2140. h->ref_list[list][index]= tmp;
  2141. }
  2142. }else if(reordering_of_pic_nums_idc==3)
  2143. break;
  2144. else{
  2145. fprintf(stderr, "illegal reordering_of_pic_nums_idc\n");
  2146. return -1;
  2147. }
  2148. }
  2149. }
  2150. if(h->slice_type!=B_TYPE) break;
  2151. }
  2152. return 0;
  2153. }
  2154. static int pred_weight_table(H264Context *h){
  2155. MpegEncContext * const s = &h->s;
  2156. int list, i;
  2157. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  2158. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  2159. for(list=0; list<2; list++){
  2160. for(i=0; i<h->ref_count[list]; i++){
  2161. int luma_weight_flag, chroma_weight_flag;
  2162. luma_weight_flag= get_bits1(&s->gb);
  2163. if(luma_weight_flag){
  2164. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  2165. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  2166. }
  2167. chroma_weight_flag= get_bits1(&s->gb);
  2168. if(chroma_weight_flag){
  2169. int j;
  2170. for(j=0; j<2; j++){
  2171. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  2172. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  2173. }
  2174. }
  2175. }
  2176. if(h->slice_type != B_TYPE) break;
  2177. }
  2178. return 0;
  2179. }
  2180. /**
  2181. * instantaneos decoder refresh.
  2182. */
  2183. static void idr(H264Context *h){
  2184. int i;
  2185. for(i=0; i<h->long_ref_count; i++){
  2186. h->long_ref[i]->reference=0;
  2187. h->long_ref[i]= NULL;
  2188. }
  2189. h->long_ref_count=0;
  2190. for(i=0; i<h->short_ref_count; i++){
  2191. h->short_ref[i]->reference=0;
  2192. h->short_ref[i]= NULL;
  2193. }
  2194. h->short_ref_count=0;
  2195. }
  2196. /**
  2197. *
  2198. * @return the removed picture or NULL if an error occures
  2199. */
  2200. static Picture * remove_short(H264Context *h, int frame_num){
  2201. MpegEncContext * const s = &h->s;
  2202. int i;
  2203. if(s->avctx->debug&FF_DEBUG_MMCO)
  2204. printf("remove short %d count %d\n", frame_num, h->short_ref_count);
  2205. for(i=0; i<h->short_ref_count; i++){
  2206. Picture *pic= h->short_ref[i];
  2207. if(s->avctx->debug&FF_DEBUG_MMCO)
  2208. printf("%d %d %p\n", i, pic->frame_num, pic);
  2209. if(pic->frame_num == frame_num){
  2210. h->short_ref[i]= NULL;
  2211. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
  2212. h->short_ref_count--;
  2213. return pic;
  2214. }
  2215. }
  2216. return NULL;
  2217. }
  2218. /**
  2219. *
  2220. * @return the removed picture or NULL if an error occures
  2221. */
  2222. static Picture * remove_long(H264Context *h, int i){
  2223. Picture *pic;
  2224. if(i >= h->long_ref_count) return NULL;
  2225. pic= h->long_ref[i];
  2226. if(pic==NULL) return NULL;
  2227. h->long_ref[i]= NULL;
  2228. memmove(&h->long_ref[i], &h->long_ref[i+1], (h->long_ref_count - i - 1)*sizeof(Picture*));
  2229. h->long_ref_count--;
  2230. return pic;
  2231. }
  2232. /**
  2233. * Executes the reference picture marking (memory management control operations).
  2234. */
  2235. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  2236. MpegEncContext * const s = &h->s;
  2237. int i;
  2238. int current_is_long=0;
  2239. Picture *pic;
  2240. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  2241. printf("no mmco here\n");
  2242. for(i=0; i<mmco_count; i++){
  2243. if(s->avctx->debug&FF_DEBUG_MMCO)
  2244. printf("mmco:%d %d %d\n", h->mmco[i].opcode, h->mmco[i].short_frame_num, h->mmco[i].long_index);
  2245. switch(mmco[i].opcode){
  2246. case MMCO_SHORT2UNUSED:
  2247. pic= remove_short(h, mmco[i].short_frame_num);
  2248. if(pic==NULL) return -1;
  2249. pic->reference= 0;
  2250. break;
  2251. case MMCO_SHORT2LONG:
  2252. pic= remove_long(h, mmco[i].long_index);
  2253. if(pic) pic->reference=0;
  2254. h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
  2255. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  2256. break;
  2257. case MMCO_LONG2UNUSED:
  2258. pic= remove_long(h, mmco[i].long_index);
  2259. if(pic==NULL) return -1;
  2260. pic->reference= 0;
  2261. break;
  2262. case MMCO_LONG:
  2263. pic= remove_long(h, mmco[i].long_index);
  2264. if(pic) pic->reference=0;
  2265. h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
  2266. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  2267. h->long_ref_count++;
  2268. current_is_long=1;
  2269. break;
  2270. case MMCO_SET_MAX_LONG:
  2271. assert(mmco[i].long_index <= 16);
  2272. while(mmco[i].long_index < h->long_ref_count){
  2273. pic= remove_long(h, mmco[i].long_index);
  2274. pic->reference=0;
  2275. }
  2276. while(mmco[i].long_index > h->long_ref_count){
  2277. h->long_ref[ h->long_ref_count++ ]= NULL;
  2278. }
  2279. break;
  2280. case MMCO_RESET:
  2281. while(h->short_ref_count){
  2282. pic= remove_short(h, h->short_ref[0]->frame_num);
  2283. pic->reference=0;
  2284. }
  2285. while(h->long_ref_count){
  2286. pic= remove_long(h, h->long_ref_count-1);
  2287. pic->reference=0;
  2288. }
  2289. break;
  2290. default: assert(0);
  2291. }
  2292. }
  2293. if(!current_is_long){
  2294. pic= remove_short(h, s->current_picture_ptr->frame_num);
  2295. if(pic){
  2296. pic->reference=0;
  2297. fprintf(stderr, "illegal short term buffer state detected\n");
  2298. }
  2299. if(h->short_ref_count)
  2300. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  2301. h->short_ref[0]= s->current_picture_ptr;
  2302. h->short_ref[0]->long_ref=0;
  2303. h->short_ref_count++;
  2304. }
  2305. return 0;
  2306. }
  2307. static int decode_ref_pic_marking(H264Context *h){
  2308. MpegEncContext * const s = &h->s;
  2309. int i;
  2310. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  2311. s->broken_link= get_bits1(&s->gb) -1;
  2312. h->mmco[0].long_index= get_bits1(&s->gb) - 1; // current_long_term_idx
  2313. if(h->mmco[0].long_index == -1)
  2314. h->mmco_index= 0;
  2315. else{
  2316. h->mmco[0].opcode= MMCO_LONG;
  2317. h->mmco_index= 1;
  2318. }
  2319. }else{
  2320. if(get_bits1(&s->gb)){ // adaptive_ref_pic_marking_mode_flag
  2321. for(i= h->mmco_index; i<MAX_MMCO_COUNT; i++) {
  2322. MMCOOpcode opcode= get_ue_golomb(&s->gb);;
  2323. h->mmco[i].opcode= opcode;
  2324. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  2325. 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
  2326. /* if(h->mmco[i].short_frame_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_frame_num ] == NULL){
  2327. fprintf(stderr, "illegal short ref in memory management control operation %d\n", mmco);
  2328. return -1;
  2329. }*/
  2330. }
  2331. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  2332. h->mmco[i].long_index= get_ue_golomb(&s->gb);
  2333. 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){
  2334. fprintf(stderr, "illegal long ref in memory management control operation %d\n", opcode);
  2335. return -1;
  2336. }
  2337. }
  2338. if(opcode > MMCO_LONG){
  2339. fprintf(stderr, "illegal memory management control operation %d\n", opcode);
  2340. return -1;
  2341. }
  2342. }
  2343. h->mmco_index= i;
  2344. }else{
  2345. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  2346. if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){ //FIXME fields
  2347. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  2348. h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  2349. h->mmco_index= 1;
  2350. }else
  2351. h->mmco_index= 0;
  2352. }
  2353. }
  2354. return 0;
  2355. }
  2356. static int init_poc(H264Context *h){
  2357. MpegEncContext * const s = &h->s;
  2358. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  2359. int field_poc[2];
  2360. if(h->nal_unit_type == NAL_IDR_SLICE){
  2361. h->frame_num_offset= 0;
  2362. }else{
  2363. if(h->frame_num < h->prev_frame_num)
  2364. h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
  2365. else
  2366. h->frame_num_offset= h->prev_frame_num_offset;
  2367. }
  2368. if(h->sps.poc_type==0){
  2369. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  2370. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  2371. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  2372. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  2373. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  2374. else
  2375. h->poc_msb = h->prev_poc_msb;
  2376. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  2377. field_poc[0] =
  2378. field_poc[1] = h->poc_msb + h->poc_lsb;
  2379. if(s->picture_structure == PICT_FRAME)
  2380. field_poc[1] += h->delta_poc_bottom;
  2381. }else if(h->sps.poc_type==1){
  2382. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  2383. int i;
  2384. if(h->sps.poc_cycle_length != 0)
  2385. abs_frame_num = h->frame_num_offset + h->frame_num;
  2386. else
  2387. abs_frame_num = 0;
  2388. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  2389. abs_frame_num--;
  2390. expected_delta_per_poc_cycle = 0;
  2391. for(i=0; i < h->sps.poc_cycle_length; i++)
  2392. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  2393. if(abs_frame_num > 0){
  2394. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  2395. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  2396. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  2397. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  2398. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  2399. } else
  2400. expectedpoc = 0;
  2401. if(h->nal_ref_idc == 0)
  2402. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  2403. field_poc[0] = expectedpoc + h->delta_poc[0];
  2404. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  2405. if(s->picture_structure == PICT_FRAME)
  2406. field_poc[1] += h->delta_poc[1];
  2407. }else{
  2408. int poc;
  2409. if(h->nal_unit_type == NAL_IDR_SLICE){
  2410. poc= 0;
  2411. }else{
  2412. if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
  2413. else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
  2414. }
  2415. field_poc[0]= poc;
  2416. field_poc[1]= poc;
  2417. }
  2418. if(s->picture_structure != PICT_BOTTOM_FIELD)
  2419. s->current_picture_ptr->field_poc[0]= field_poc[0];
  2420. if(s->picture_structure != PICT_TOP_FIELD)
  2421. s->current_picture_ptr->field_poc[1]= field_poc[1];
  2422. if(s->picture_structure == PICT_FRAME) // FIXME field pix?
  2423. s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]);
  2424. return 0;
  2425. }
  2426. /**
  2427. * decodes a slice header.
  2428. * this will allso call MPV_common_init() and frame_start() as needed
  2429. */
  2430. static int decode_slice_header(H264Context *h){
  2431. MpegEncContext * const s = &h->s;
  2432. int first_mb_in_slice, pps_id;
  2433. int num_ref_idx_active_override_flag;
  2434. static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
  2435. float new_aspect;
  2436. s->current_picture.reference= h->nal_ref_idc != 0;
  2437. first_mb_in_slice= get_ue_golomb(&s->gb);
  2438. h->slice_type= get_ue_golomb(&s->gb);
  2439. if(h->slice_type > 9){
  2440. fprintf(stderr, "slice type too large (%d) at %d %d\n", h->slice_type, s->mb_x, s->mb_y);
  2441. }
  2442. if(h->slice_type > 4){
  2443. h->slice_type -= 5;
  2444. h->slice_type_fixed=1;
  2445. }else
  2446. h->slice_type_fixed=0;
  2447. h->slice_type= slice_type_map[ h->slice_type ];
  2448. s->pict_type= h->slice_type; // to make a few old func happy, its wrong though
  2449. pps_id= get_ue_golomb(&s->gb);
  2450. if(pps_id>255){
  2451. fprintf(stderr, "pps_id out of range\n");
  2452. return -1;
  2453. }
  2454. h->pps= h->pps_buffer[pps_id];
  2455. if(h->pps.slice_group_count == 0){
  2456. fprintf(stderr, "non existing PPS referenced\n");
  2457. return -1;
  2458. }
  2459. h->sps= h->sps_buffer[ h->pps.sps_id ];
  2460. if(h->sps.log2_max_frame_num == 0){
  2461. fprintf(stderr, "non existing SPS referenced\n");
  2462. return -1;
  2463. }
  2464. s->mb_width= h->sps.mb_width;
  2465. s->mb_height= h->sps.mb_height;
  2466. h->b_stride= s->mb_width*4;
  2467. h->b8_stride= s->mb_width*2;
  2468. s->mb_x = first_mb_in_slice % s->mb_width;
  2469. s->mb_y = first_mb_in_slice / s->mb_width; //FIXME AFFW
  2470. s->width = 16*s->mb_width - 2*(h->sps.crop_left + h->sps.crop_right );
  2471. if(h->sps.frame_mbs_only_flag)
  2472. s->height= 16*s->mb_height - 2*(h->sps.crop_top + h->sps.crop_bottom);
  2473. else
  2474. s->height= 16*s->mb_height - 4*(h->sps.crop_top + h->sps.crop_bottom); //FIXME recheck
  2475. if(s->aspected_height) //FIXME emms at end of slice ?
  2476. new_aspect= h->sps.sar_width*s->width / (float)(s->height*h->sps.sar_height);
  2477. else
  2478. new_aspect=0;
  2479. if (s->context_initialized
  2480. && ( s->width != s->avctx->width || s->height != s->avctx->height
  2481. || ABS(new_aspect - s->avctx->aspect_ratio) > 0.001)) {
  2482. free_tables(h);
  2483. MPV_common_end(s);
  2484. }
  2485. if (!s->context_initialized) {
  2486. if (MPV_common_init(s) < 0)
  2487. return -1;
  2488. alloc_tables(h);
  2489. s->avctx->width = s->width;
  2490. s->avctx->height = s->height;
  2491. s->avctx->aspect_ratio= new_aspect;
  2492. }
  2493. if(first_mb_in_slice == 0){
  2494. frame_start(h);
  2495. }
  2496. s->current_picture_ptr->frame_num= //FIXME frame_num cleanup
  2497. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  2498. if(h->sps.frame_mbs_only_flag){
  2499. s->picture_structure= PICT_FRAME;
  2500. }else{
  2501. if(get_bits1(&s->gb)) //field_pic_flag
  2502. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  2503. else
  2504. s->picture_structure= PICT_FRAME;
  2505. }
  2506. if(s->picture_structure==PICT_FRAME){
  2507. h->curr_pic_num= h->frame_num;
  2508. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  2509. }else{
  2510. h->curr_pic_num= 2*h->frame_num;
  2511. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  2512. }
  2513. if(h->nal_unit_type == NAL_IDR_SLICE){
  2514. get_ue_golomb(&s->gb); /* idr_pic_id */
  2515. }
  2516. if(h->sps.poc_type==0){
  2517. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  2518. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  2519. h->delta_poc_bottom= get_se_golomb(&s->gb);
  2520. }
  2521. }
  2522. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  2523. h->delta_poc[0]= get_se_golomb(&s->gb);
  2524. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  2525. h->delta_poc[1]= get_se_golomb(&s->gb);
  2526. }
  2527. init_poc(h);
  2528. if(h->pps.redundant_pic_cnt_present){
  2529. h->redundant_pic_count= get_ue_golomb(&s->gb);
  2530. }
  2531. //set defaults, might be overriden a few line later
  2532. h->ref_count[0]= h->pps.ref_count[0];
  2533. h->ref_count[1]= h->pps.ref_count[1];
  2534. if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){
  2535. if(h->slice_type == B_TYPE){
  2536. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  2537. }
  2538. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  2539. if(num_ref_idx_active_override_flag){
  2540. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  2541. if(h->slice_type==B_TYPE)
  2542. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  2543. if(h->ref_count[0] > 32 || h->ref_count[1] > 32){
  2544. fprintf(stderr, "reference overflow\n");
  2545. return -1;
  2546. }
  2547. }
  2548. }
  2549. if(first_mb_in_slice == 0){
  2550. fill_default_ref_list(h);
  2551. }
  2552. decode_ref_pic_list_reordering(h);
  2553. if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
  2554. || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
  2555. pred_weight_table(h);
  2556. if(s->current_picture.reference)
  2557. decode_ref_pic_marking(h);
  2558. //FIXME CABAC stuff
  2559. s->qscale = h->pps.init_qp + get_se_golomb(&s->gb); //slice_qp_delta
  2560. //FIXME qscale / qp ... stuff
  2561. if(h->slice_type == SP_TYPE){
  2562. get_bits1(&s->gb); /* sp_for_switch_flag */
  2563. }
  2564. if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
  2565. get_se_golomb(&s->gb); /* slice_qs_delta */
  2566. }
  2567. if( h->pps.deblocking_filter_parameters_present ) {
  2568. h->disable_deblocking_filter_idc= get_ue_golomb(&s->gb);
  2569. if( h->disable_deblocking_filter_idc != 1 ) {
  2570. h->slice_alpha_c0_offset_div2= get_se_golomb(&s->gb);
  2571. h->slice_beta_offset_div2= get_se_golomb(&s->gb);
  2572. }
  2573. }else
  2574. h->disable_deblocking_filter_idc= 0;
  2575. #if 0 //FMO
  2576. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  2577. slice_group_change_cycle= get_bits(&s->gb, ?);
  2578. #endif
  2579. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  2580. printf("mb:%d %c pps:%d frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d\n",
  2581. first_mb_in_slice,
  2582. av_get_pict_type_char(h->slice_type),
  2583. pps_id, h->frame_num,
  2584. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  2585. h->ref_count[0], h->ref_count[1],
  2586. s->qscale,
  2587. h->disable_deblocking_filter_idc
  2588. );
  2589. }
  2590. return 0;
  2591. }
  2592. /**
  2593. *
  2594. */
  2595. static inline int get_level_prefix(GetBitContext *gb){
  2596. unsigned int buf;
  2597. int log;
  2598. OPEN_READER(re, gb);
  2599. UPDATE_CACHE(re, gb);
  2600. buf=GET_CACHE(re, gb);
  2601. log= 32 - av_log2(buf);
  2602. #ifdef TRACE
  2603. print_bin(buf>>(32-log), log);
  2604. printf("%5d %2d %3d lpr @%5d in %s get_level_prefix\n", buf>>(32-log), log, log-1, get_bits_count(gb), __FILE__);
  2605. #endif
  2606. LAST_SKIP_BITS(re, gb, log);
  2607. CLOSE_READER(re, gb);
  2608. return log-1;
  2609. }
  2610. /**
  2611. * decodes a residual block.
  2612. * @param n block index
  2613. * @param scantable scantable
  2614. * @param max_coeff number of coefficients in the block
  2615. * @return <0 if an error occured
  2616. */
  2617. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, int qp, int max_coeff){
  2618. MpegEncContext * const s = &h->s;
  2619. const uint16_t *qmul= dequant_coeff[qp];
  2620. 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};
  2621. int level[16], run[16];
  2622. int suffix_length, zeros_left, coeff_num, coeff_token, total_coeff, i, trailing_ones;
  2623. //FIXME put trailing_onex into the context
  2624. if(n == CHROMA_DC_BLOCK_INDEX){
  2625. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  2626. total_coeff= coeff_token>>2;
  2627. }else{
  2628. if(n == LUMA_DC_BLOCK_INDEX){
  2629. total_coeff= pred_non_zero_count(h, 0);
  2630. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  2631. total_coeff= coeff_token>>2;
  2632. }else{
  2633. total_coeff= pred_non_zero_count(h, n);
  2634. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  2635. total_coeff= coeff_token>>2;
  2636. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  2637. }
  2638. }
  2639. //FIXME set last_non_zero?
  2640. if(total_coeff==0)
  2641. return 0;
  2642. trailing_ones= coeff_token&3;
  2643. tprintf("trailing:%d, total:%d\n", trailing_ones, total_coeff);
  2644. assert(total_coeff<=16);
  2645. for(i=0; i<trailing_ones; i++){
  2646. level[i]= 1 - 2*get_bits1(gb);
  2647. }
  2648. suffix_length= total_coeff > 10 && trailing_ones < 3;
  2649. for(; i<total_coeff; i++){
  2650. const int prefix= get_level_prefix(gb);
  2651. int level_code, mask;
  2652. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  2653. if(suffix_length)
  2654. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  2655. else
  2656. level_code= (prefix<<suffix_length); //part
  2657. }else if(prefix==14){
  2658. if(suffix_length)
  2659. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  2660. else
  2661. level_code= prefix + get_bits(gb, 4); //part
  2662. }else if(prefix==15){
  2663. level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part
  2664. if(suffix_length==0) level_code+=15; //FIXME doesnt make (much)sense
  2665. }else{
  2666. fprintf(stderr, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  2667. return -1;
  2668. }
  2669. if(i==trailing_ones && i<3) level_code+= 2; //FIXME split first iteration
  2670. mask= -(level_code&1);
  2671. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  2672. if(suffix_length==0) suffix_length=1; //FIXME split first iteration
  2673. #if 1
  2674. if(ABS(level[i]) > (3<<(suffix_length-1)) && suffix_length<6) suffix_length++;
  2675. #else
  2676. if((2+level_code)>>1) > (3<<(suffix_length-1)) && suffix_length<6) suffix_length++;
  2677. ? == prefix > 2 or sth
  2678. #endif
  2679. tprintf("level: %d suffix_length:%d\n", level[i], suffix_length);
  2680. }
  2681. if(total_coeff == max_coeff)
  2682. zeros_left=0;
  2683. else{
  2684. if(n == CHROMA_DC_BLOCK_INDEX)
  2685. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  2686. else
  2687. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  2688. }
  2689. for(i=0; i<total_coeff-1; i++){
  2690. if(zeros_left <=0)
  2691. break;
  2692. else if(zeros_left < 7){
  2693. run[i]= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  2694. }else{
  2695. run[i]= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  2696. }
  2697. zeros_left -= run[i];
  2698. }
  2699. if(zeros_left<0){
  2700. fprintf(stderr, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  2701. return -1;
  2702. }
  2703. for(; i<total_coeff-1; i++){
  2704. run[i]= 0;
  2705. }
  2706. run[i]= zeros_left;
  2707. coeff_num=-1;
  2708. if(n > 24){
  2709. for(i=total_coeff-1; i>=0; i--){ //FIXME merge into rundecode?
  2710. int j;
  2711. coeff_num += run[i] + 1; //FIXME add 1 earlier ?
  2712. j= scantable[ coeff_num ];
  2713. block[j]= level[i];
  2714. }
  2715. }else{
  2716. for(i=total_coeff-1; i>=0; i--){ //FIXME merge into rundecode?
  2717. int j;
  2718. coeff_num += run[i] + 1; //FIXME add 1 earlier ?
  2719. j= scantable[ coeff_num ];
  2720. block[j]= level[i] * qmul[j];
  2721. // printf("%d %d ", block[j], qmul[j]);
  2722. }
  2723. }
  2724. return 0;
  2725. }
  2726. /**
  2727. * decodes a macroblock
  2728. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  2729. */
  2730. static int decode_mb(H264Context *h){
  2731. MpegEncContext * const s = &h->s;
  2732. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2733. int mb_type, partition_count, cbp;
  2734. s->dsp.clear_blocks(h->mb); //FIXME avoid if allready clear (move after skip handlong?
  2735. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  2736. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  2737. down the code */
  2738. if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){
  2739. if(s->mb_skip_run==-1)
  2740. s->mb_skip_run= get_ue_golomb(&s->gb);
  2741. if (s->mb_skip_run--) {
  2742. int mx, my;
  2743. /* skip mb */
  2744. //FIXME b frame
  2745. mb_type= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0;
  2746. memset(h->non_zero_count[mb_xy], 0, 16);
  2747. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  2748. if(h->sps.mb_aff && s->mb_skip_run==0 && (s->mb_y&1)==0){
  2749. h->mb_field_decoding_flag= get_bits1(&s->gb);
  2750. }
  2751. if(h->mb_field_decoding_flag)
  2752. mb_type|= MB_TYPE_INTERLACED;
  2753. fill_caches(h, mb_type); //FIXME check what is needed and what not ...
  2754. pred_pskip_motion(h, &mx, &my);
  2755. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  2756. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  2757. write_back_motion(h, mb_type);
  2758. s->current_picture.mb_type[mb_xy]= mb_type; //FIXME SKIP type
  2759. h->slice_table[ mb_xy ]= h->slice_num;
  2760. h->prev_mb_skiped= 1;
  2761. return 0;
  2762. }
  2763. }
  2764. if(h->sps.mb_aff /* && !field pic FIXME needed? */){
  2765. if((s->mb_y&1)==0)
  2766. h->mb_field_decoding_flag = get_bits1(&s->gb);
  2767. }else
  2768. h->mb_field_decoding_flag=0; //FIXME som ed note ?!
  2769. h->prev_mb_skiped= 0;
  2770. mb_type= get_ue_golomb(&s->gb);
  2771. if(h->slice_type == B_TYPE){
  2772. if(mb_type < 23){
  2773. partition_count= b_mb_type_info[mb_type].partition_count;
  2774. mb_type= b_mb_type_info[mb_type].type;
  2775. }else{
  2776. mb_type -= 23;
  2777. goto decode_intra_mb;
  2778. }
  2779. }else if(h->slice_type == P_TYPE /*|| h->slice_type == SP_TYPE */){
  2780. if(mb_type < 5){
  2781. partition_count= p_mb_type_info[mb_type].partition_count;
  2782. mb_type= p_mb_type_info[mb_type].type;
  2783. }else{
  2784. mb_type -= 5;
  2785. goto decode_intra_mb;
  2786. }
  2787. }else{
  2788. assert(h->slice_type == I_TYPE);
  2789. decode_intra_mb:
  2790. if(mb_type > 25){
  2791. 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);
  2792. return -1;
  2793. }
  2794. partition_count=0;
  2795. cbp= i_mb_type_info[mb_type].cbp;
  2796. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  2797. mb_type= i_mb_type_info[mb_type].type;
  2798. }
  2799. if(h->mb_field_decoding_flag)
  2800. mb_type |= MB_TYPE_INTERLACED;
  2801. s->current_picture.mb_type[mb_xy]= mb_type;
  2802. h->slice_table[ mb_xy ]= h->slice_num;
  2803. if(IS_INTRA_PCM(mb_type)){
  2804. const uint8_t *ptr;
  2805. int x, y;
  2806. // we assume these blocks are very rare so we dont optimize it
  2807. align_get_bits(&s->gb);
  2808. ptr= s->gb.buffer + get_bits_count(&s->gb);
  2809. for(y=0; y<16; y++){
  2810. const int index= 4*(y&3) + 64*(y>>2);
  2811. for(x=0; x<16; x++){
  2812. h->mb[index + (x&3) + 16*(x>>2)]= *(ptr++);
  2813. }
  2814. }
  2815. for(y=0; y<8; y++){
  2816. const int index= 256 + 4*(y&3) + 32*(y>>2);
  2817. for(x=0; x<8; x++){
  2818. h->mb[index + (x&3) + 16*(x>>2)]= *(ptr++);
  2819. }
  2820. }
  2821. for(y=0; y<8; y++){
  2822. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  2823. for(x=0; x<8; x++){
  2824. h->mb[index + (x&3) + 16*(x>>2)]= *(ptr++);
  2825. }
  2826. }
  2827. skip_bits(&s->gb, 384); //FIXME check /fix the bitstream readers
  2828. memset(h->non_zero_count[mb_xy], 16, 16);
  2829. return 0;
  2830. }
  2831. fill_caches(h, mb_type);
  2832. //mb_pred
  2833. if(IS_INTRA(mb_type)){
  2834. // init_top_left_availability(h);
  2835. if(IS_INTRA4x4(mb_type)){
  2836. int i;
  2837. // fill_intra4x4_pred_table(h);
  2838. for(i=0; i<16; i++){
  2839. const int mode_coded= !get_bits1(&s->gb);
  2840. const int predicted_mode= pred_intra_mode(h, i);
  2841. int mode;
  2842. if(mode_coded){
  2843. const int rem_mode= get_bits(&s->gb, 3);
  2844. if(rem_mode<predicted_mode)
  2845. mode= rem_mode;
  2846. else
  2847. mode= rem_mode + 1;
  2848. }else{
  2849. mode= predicted_mode;
  2850. }
  2851. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  2852. }
  2853. write_back_intra_pred_mode(h);
  2854. if( check_intra4x4_pred_mode(h) < 0)
  2855. return -1;
  2856. }else{
  2857. h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
  2858. if(h->intra16x16_pred_mode < 0)
  2859. return -1;
  2860. }
  2861. h->chroma_pred_mode= get_ue_golomb(&s->gb);
  2862. h->chroma_pred_mode= check_intra_pred_mode(h, h->chroma_pred_mode);
  2863. if(h->chroma_pred_mode < 0)
  2864. return -1;
  2865. }else if(partition_count==4){
  2866. int i, j, sub_partition_count[4], list, ref[2][4];
  2867. if(h->slice_type == B_TYPE){
  2868. for(i=0; i<4; i++){
  2869. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  2870. if(h->sub_mb_type[i] >=13){
  2871. 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);
  2872. return -1;
  2873. }
  2874. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  2875. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  2876. }
  2877. }else{
  2878. assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE); //FIXME SP correct ?
  2879. for(i=0; i<4; i++){
  2880. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  2881. if(h->sub_mb_type[i] >=4){
  2882. 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);
  2883. return -1;
  2884. }
  2885. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  2886. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  2887. }
  2888. }
  2889. for(list=0; list<2; list++){
  2890. const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  2891. if(ref_count == 0) continue;
  2892. for(i=0; i<4; i++){
  2893. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  2894. ref[list][i] = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
  2895. }else{
  2896. //FIXME
  2897. ref[list][i] = -1;
  2898. }
  2899. }
  2900. }
  2901. for(list=0; list<2; list++){
  2902. const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  2903. if(ref_count == 0) continue;
  2904. for(i=0; i<4; i++){
  2905. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  2906. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  2907. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  2908. const int sub_mb_type= h->sub_mb_type[i];
  2909. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  2910. for(j=0; j<sub_partition_count[i]; j++){
  2911. int mx, my;
  2912. const int index= 4*i + block_width*j;
  2913. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  2914. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  2915. mx += get_se_golomb(&s->gb);
  2916. my += get_se_golomb(&s->gb);
  2917. tprintf("final mv:%d %d\n", mx, my);
  2918. if(IS_SUB_8X8(sub_mb_type)){
  2919. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  2920. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  2921. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  2922. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  2923. }else if(IS_SUB_8X4(sub_mb_type)){
  2924. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  2925. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  2926. }else if(IS_SUB_4X8(sub_mb_type)){
  2927. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  2928. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  2929. }else{
  2930. assert(IS_SUB_4X4(sub_mb_type));
  2931. mv_cache[ 0 ][0]= mx;
  2932. mv_cache[ 0 ][1]= my;
  2933. }
  2934. }
  2935. }else{
  2936. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  2937. p[0] = p[1]=
  2938. p[8] = p[9]= 0;
  2939. }
  2940. }
  2941. }
  2942. }else if(!IS_DIRECT(mb_type)){
  2943. int list, mx, my, i;
  2944. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  2945. if(IS_16X16(mb_type)){
  2946. for(list=0; list<2; list++){
  2947. if(h->ref_count[0]>0){
  2948. if(IS_DIR(mb_type, 0, list)){
  2949. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  2950. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  2951. }
  2952. }
  2953. }
  2954. for(list=0; list<2; list++){
  2955. if(IS_DIR(mb_type, 0, list)){
  2956. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  2957. mx += get_se_golomb(&s->gb);
  2958. my += get_se_golomb(&s->gb);
  2959. tprintf("final mv:%d %d\n", mx, my);
  2960. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  2961. }
  2962. }
  2963. }
  2964. else if(IS_16X8(mb_type)){
  2965. for(list=0; list<2; list++){
  2966. if(h->ref_count[list]>0){
  2967. for(i=0; i<2; i++){
  2968. if(IS_DIR(mb_type, i, list)){
  2969. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  2970. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  2971. }
  2972. }
  2973. }
  2974. }
  2975. for(list=0; list<2; list++){
  2976. for(i=0; i<2; i++){
  2977. if(IS_DIR(mb_type, i, list)){
  2978. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  2979. mx += get_se_golomb(&s->gb);
  2980. my += get_se_golomb(&s->gb);
  2981. tprintf("final mv:%d %d\n", mx, my);
  2982. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  2983. }
  2984. }
  2985. }
  2986. }else{
  2987. assert(IS_8X16(mb_type));
  2988. for(list=0; list<2; list++){
  2989. if(h->ref_count[list]>0){
  2990. for(i=0; i<2; i++){
  2991. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  2992. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  2993. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  2994. }
  2995. }
  2996. }
  2997. }
  2998. for(list=0; list<2; list++){
  2999. for(i=0; i<2; i++){
  3000. if(IS_DIR(mb_type, i, list)){
  3001. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  3002. mx += get_se_golomb(&s->gb);
  3003. my += get_se_golomb(&s->gb);
  3004. tprintf("final mv:%d %d\n", mx, my);
  3005. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  3006. }
  3007. }
  3008. }
  3009. }
  3010. }
  3011. if(IS_INTER(mb_type))
  3012. write_back_motion(h, mb_type);
  3013. if(!IS_INTRA16x16(mb_type)){
  3014. cbp= get_ue_golomb(&s->gb);
  3015. if(cbp > 47){
  3016. fprintf(stderr, "cbp too large (%d) at %d %d\n", cbp, s->mb_x, s->mb_y);
  3017. return -1;
  3018. }
  3019. if(IS_INTRA4x4(mb_type))
  3020. cbp= golomb_to_intra4x4_cbp[cbp];
  3021. else
  3022. cbp= golomb_to_inter_cbp[cbp];
  3023. }
  3024. if(cbp || IS_INTRA16x16(mb_type)){
  3025. int i8x8, i4x4, chroma_idx;
  3026. int chroma_qp, dquant;
  3027. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  3028. const uint8_t *scan, *dc_scan;
  3029. // fill_non_zero_count_cache(h);
  3030. if(IS_INTERLACED(mb_type)){
  3031. scan= field_scan;
  3032. dc_scan= luma_dc_field_scan;
  3033. }else{
  3034. scan= zigzag_scan;
  3035. dc_scan= luma_dc_zigzag_scan;
  3036. }
  3037. dquant= get_se_golomb(&s->gb);
  3038. if( dquant > 25 || dquant < -26 ){
  3039. fprintf(stderr, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  3040. return -1;
  3041. }
  3042. s->qscale += dquant;
  3043. if(((unsigned)s->qscale) > 51){
  3044. if(s->qscale<0) s->qscale+= 52;
  3045. else s->qscale-= 52;
  3046. }
  3047. h->chroma_qp= chroma_qp= get_chroma_qp(h, s->qscale);
  3048. if(IS_INTRA16x16(mb_type)){
  3049. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, s->qscale, 16) < 0){
  3050. return -1; //FIXME continue if partotioned and other retirn -1 too
  3051. }
  3052. assert((cbp&15) == 0 || (cbp&15) == 15);
  3053. if(cbp&15){
  3054. for(i8x8=0; i8x8<4; i8x8++){
  3055. for(i4x4=0; i4x4<4; i4x4++){
  3056. const int index= i4x4 + 4*i8x8;
  3057. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, s->qscale, 15) < 0 ){
  3058. return -1;
  3059. }
  3060. }
  3061. }
  3062. }else{
  3063. memset(&h->non_zero_count_cache[8], 0, 8*4); //FIXME stupid & slow
  3064. }
  3065. }else{
  3066. for(i8x8=0; i8x8<4; i8x8++){
  3067. if(cbp & (1<<i8x8)){
  3068. for(i4x4=0; i4x4<4; i4x4++){
  3069. const int index= i4x4 + 4*i8x8;
  3070. if( decode_residual(h, gb, h->mb + 16*index, index, scan, s->qscale, 16) <0 ){
  3071. return -1;
  3072. }
  3073. }
  3074. }else{
  3075. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  3076. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  3077. }
  3078. }
  3079. }
  3080. if(cbp&0x30){
  3081. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  3082. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, chroma_qp, 4) < 0){
  3083. return -1;
  3084. }
  3085. }
  3086. if(cbp&0x20){
  3087. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  3088. for(i4x4=0; i4x4<4; i4x4++){
  3089. const int index= 16 + 4*chroma_idx + i4x4;
  3090. if( decode_residual(h, gb, h->mb + 16*index, index, scan + 1, chroma_qp, 15) < 0){
  3091. return -1;
  3092. }
  3093. }
  3094. }
  3095. }else{
  3096. uint8_t * const nnz= &h->non_zero_count_cache[0];
  3097. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  3098. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  3099. }
  3100. }else{
  3101. memset(&h->non_zero_count_cache[8], 0, 8*5);
  3102. }
  3103. write_back_non_zero_count(h);
  3104. return 0;
  3105. }
  3106. static int decode_slice(H264Context *h){
  3107. MpegEncContext * const s = &h->s;
  3108. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  3109. s->mb_skip_run= -1;
  3110. #if 1
  3111. for(;;){
  3112. int ret= decode_mb(h);
  3113. hl_decode_mb(h);
  3114. if(ret>=0 && h->sps.mb_aff){ //FIXME optimal? or let mb_decode decode 16x32 ?
  3115. s->mb_y++;
  3116. ret= decode_mb(h);
  3117. hl_decode_mb(h);
  3118. s->mb_y--;
  3119. }
  3120. if(ret<0){
  3121. fprintf(stderr, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  3122. 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);
  3123. return -1;
  3124. }
  3125. if(++s->mb_x >= s->mb_width){
  3126. s->mb_x=0;
  3127. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  3128. if(++s->mb_y >= s->mb_height){
  3129. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  3130. if(get_bits_count(&s->gb) == s->gb.size_in_bits){
  3131. 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);
  3132. return 0;
  3133. }else{
  3134. 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);
  3135. return -1;
  3136. }
  3137. }
  3138. }
  3139. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  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_ERROR|DC_ERROR|MV_ERROR)&part_mask);
  3145. return -1;
  3146. }
  3147. }
  3148. }
  3149. #endif
  3150. #if 0
  3151. for(;s->mb_y < s->mb_height; s->mb_y++){
  3152. for(;s->mb_x < s->mb_width; s->mb_x++){
  3153. int ret= decode_mb(h);
  3154. hl_decode_mb(h);
  3155. if(ret<0){
  3156. fprintf(stderr, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  3157. 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);
  3158. return -1;
  3159. }
  3160. if(++s->mb_x >= s->mb_width){
  3161. s->mb_x=0;
  3162. if(++s->mb_y >= s->mb_height){
  3163. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  3164. 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);
  3165. return 0;
  3166. }else{
  3167. 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);
  3168. return -1;
  3169. }
  3170. }
  3171. }
  3172. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  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_ERROR|DC_ERROR|MV_ERROR)&part_mask);
  3178. return -1;
  3179. }
  3180. }
  3181. }
  3182. s->mb_x=0;
  3183. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  3184. }
  3185. #endif
  3186. return -1; //not reached
  3187. }
  3188. static inline int decode_vui_parameters(H264Context *h, SPS *sps){
  3189. MpegEncContext * const s = &h->s;
  3190. int aspect_ratio_info_present_flag, aspect_ratio_idc;
  3191. aspect_ratio_info_present_flag= get_bits1(&s->gb);
  3192. if( aspect_ratio_info_present_flag ) {
  3193. aspect_ratio_idc= get_bits(&s->gb, 8);
  3194. if( aspect_ratio_idc == EXTENDED_SAR ) {
  3195. sps->sar_width= get_bits(&s->gb, 16);
  3196. sps->sar_height= get_bits(&s->gb, 16);
  3197. }else if(aspect_ratio_idc < 16){
  3198. sps->sar_width= pixel_aspect[aspect_ratio_idc][0];
  3199. sps->sar_height= pixel_aspect[aspect_ratio_idc][1];
  3200. }else{
  3201. fprintf(stderr, "illegal aspect ratio\n");
  3202. return -1;
  3203. }
  3204. }else{
  3205. sps->sar_width=
  3206. sps->sar_height= 0;
  3207. }
  3208. // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
  3209. #if 0
  3210. | overscan_info_present_flag |0 |u(1) |
  3211. | if( overscan_info_present_flag ) | | |
  3212. | overscan_appropriate_flag |0 |u(1) |
  3213. | video_signal_type_present_flag |0 |u(1) |
  3214. | if( video_signal_type_present_flag ) { | | |
  3215. | video_format |0 |u(3) |
  3216. | video_full_range_flag |0 |u(1) |
  3217. | colour_description_present_flag |0 |u(1) |
  3218. | if( colour_description_present_flag ) { | | |
  3219. | colour_primaries |0 |u(8) |
  3220. | transfer_characteristics |0 |u(8) |
  3221. | matrix_coefficients |0 |u(8) |
  3222. | } | | |
  3223. | } | | |
  3224. | chroma_location_info_present_flag |0 |u(1) |
  3225. | if ( chroma_location_info_present_flag ) { | | |
  3226. | chroma_sample_location_type_top_field |0 |ue(v) |
  3227. | chroma_sample_location_type_bottom_field |0 |ue(v) |
  3228. | } | | |
  3229. | timing_info_present_flag |0 |u(1) |
  3230. | if( timing_info_present_flag ) { | | |
  3231. | num_units_in_tick |0 |u(32) |
  3232. | time_scale |0 |u(32) |
  3233. | fixed_frame_rate_flag |0 |u(1) |
  3234. | } | | |
  3235. | nal_hrd_parameters_present_flag |0 |u(1) |
  3236. | if( nal_hrd_parameters_present_flag = = 1) | | |
  3237. | hrd_parameters( ) | | |
  3238. | vcl_hrd_parameters_present_flag |0 |u(1) |
  3239. | if( vcl_hrd_parameters_present_flag = = 1) | | |
  3240. | hrd_parameters( ) | | |
  3241. | if( ( nal_hrd_parameters_present_flag = = 1 | || | |
  3242. | | | |
  3243. |( vcl_hrd_parameters_present_flag = = 1 ) ) | | |
  3244. | low_delay_hrd_flag |0 |u(1) |
  3245. | bitstream_restriction_flag |0 |u(1) |
  3246. | if( bitstream_restriction_flag ) { |0 |u(1) |
  3247. | motion_vectors_over_pic_boundaries_flag |0 |u(1) |
  3248. | max_bytes_per_pic_denom |0 |ue(v) |
  3249. | max_bits_per_mb_denom |0 |ue(v) |
  3250. | log2_max_mv_length_horizontal |0 |ue(v) |
  3251. | log2_max_mv_length_vertical |0 |ue(v) |
  3252. | num_reorder_frames |0 |ue(v) |
  3253. | max_dec_frame_buffering |0 |ue(v) |
  3254. | } | | |
  3255. |} | | |
  3256. #endif
  3257. return 0;
  3258. }
  3259. static inline int decode_seq_parameter_set(H264Context *h){
  3260. MpegEncContext * const s = &h->s;
  3261. int profile_idc, level_idc;
  3262. int sps_id, i;
  3263. SPS *sps;
  3264. profile_idc= get_bits(&s->gb, 8);
  3265. get_bits1(&s->gb); //constraint_set0_flag
  3266. get_bits1(&s->gb); //constraint_set1_flag
  3267. get_bits1(&s->gb); //constraint_set2_flag
  3268. get_bits(&s->gb, 5); // reserved
  3269. level_idc= get_bits(&s->gb, 8);
  3270. sps_id= get_ue_golomb(&s->gb);
  3271. sps= &h->sps_buffer[ sps_id ];
  3272. sps->profile_idc= profile_idc;
  3273. sps->level_idc= level_idc;
  3274. sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
  3275. sps->poc_type= get_ue_golomb(&s->gb);
  3276. if(sps->poc_type == 0){ //FIXME #define
  3277. sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
  3278. } else if(sps->poc_type == 1){//FIXME #define
  3279. sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
  3280. sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
  3281. sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
  3282. sps->poc_cycle_length= get_ue_golomb(&s->gb);
  3283. for(i=0; i<sps->poc_cycle_length; i++)
  3284. sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
  3285. }
  3286. if(sps->poc_type > 2){
  3287. fprintf(stderr, "illegal POC type %d\n", sps->poc_type);
  3288. return -1;
  3289. }
  3290. sps->ref_frame_count= get_ue_golomb(&s->gb);
  3291. sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
  3292. sps->mb_width= get_ue_golomb(&s->gb) + 1;
  3293. sps->mb_height= get_ue_golomb(&s->gb) + 1;
  3294. sps->frame_mbs_only_flag= get_bits1(&s->gb);
  3295. if(!sps->frame_mbs_only_flag)
  3296. sps->mb_aff= get_bits1(&s->gb);
  3297. else
  3298. sps->mb_aff= 0;
  3299. sps->direct_8x8_inference_flag= get_bits1(&s->gb);
  3300. sps->crop= get_bits1(&s->gb);
  3301. if(sps->crop){
  3302. sps->crop_left = get_ue_golomb(&s->gb);
  3303. sps->crop_right = get_ue_golomb(&s->gb);
  3304. sps->crop_top = get_ue_golomb(&s->gb);
  3305. sps->crop_bottom= get_ue_golomb(&s->gb);
  3306. if(sps->crop_left || sps->crop_top){
  3307. fprintf(stderr, "insane croping not completly supported, this could look slightly wrong ...\n");
  3308. }
  3309. }else{
  3310. sps->crop_left =
  3311. sps->crop_right =
  3312. sps->crop_top =
  3313. sps->crop_bottom= 0;
  3314. }
  3315. sps->vui_parameters_present_flag= get_bits1(&s->gb);
  3316. if( sps->vui_parameters_present_flag )
  3317. decode_vui_parameters(h, sps);
  3318. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  3319. printf("sps:%d profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%d/%d/%d/%d %s\n",
  3320. sps_id, sps->profile_idc, sps->level_idc,
  3321. sps->poc_type,
  3322. sps->ref_frame_count,
  3323. sps->mb_width, sps->mb_height,
  3324. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  3325. sps->direct_8x8_inference_flag ? "8B8" : "",
  3326. sps->crop_left, sps->crop_right,
  3327. sps->crop_top, sps->crop_bottom,
  3328. sps->vui_parameters_present_flag ? "VUI" : ""
  3329. );
  3330. }
  3331. return 0;
  3332. }
  3333. static inline int decode_picture_parameter_set(H264Context *h){
  3334. MpegEncContext * const s = &h->s;
  3335. int pps_id= get_ue_golomb(&s->gb);
  3336. PPS *pps= &h->pps_buffer[pps_id];
  3337. pps->sps_id= get_ue_golomb(&s->gb);
  3338. pps->cabac= get_bits1(&s->gb);
  3339. pps->pic_order_present= get_bits1(&s->gb);
  3340. pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
  3341. if(pps->slice_group_count > 1 ){
  3342. pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
  3343. fprintf(stderr, "FMO not supported\n");
  3344. switch(pps->mb_slice_group_map_type){
  3345. case 0:
  3346. #if 0
  3347. | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
  3348. | run_length[ i ] |1 |ue(v) |
  3349. #endif
  3350. break;
  3351. case 2:
  3352. #if 0
  3353. | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
  3354. |{ | | |
  3355. | top_left_mb[ i ] |1 |ue(v) |
  3356. | bottom_right_mb[ i ] |1 |ue(v) |
  3357. | } | | |
  3358. #endif
  3359. break;
  3360. case 3:
  3361. case 4:
  3362. case 5:
  3363. #if 0
  3364. | slice_group_change_direction_flag |1 |u(1) |
  3365. | slice_group_change_rate_minus1 |1 |ue(v) |
  3366. #endif
  3367. break;
  3368. case 6:
  3369. #if 0
  3370. | slice_group_id_cnt_minus1 |1 |ue(v) |
  3371. | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
  3372. |) | | |
  3373. | slice_group_id[ i ] |1 |u(v) |
  3374. #endif
  3375. break;
  3376. }
  3377. }
  3378. pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  3379. pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  3380. if(pps->ref_count[0] > 32 || pps->ref_count[1] > 32){
  3381. fprintf(stderr, "reference overflow (pps)\n");
  3382. return -1;
  3383. }
  3384. pps->weighted_pred= get_bits1(&s->gb);
  3385. pps->weighted_bipred_idc= get_bits(&s->gb, 2);
  3386. pps->init_qp= get_se_golomb(&s->gb) + 26;
  3387. pps->init_qs= get_se_golomb(&s->gb) + 26;
  3388. pps->chroma_qp_index_offset= get_se_golomb(&s->gb);
  3389. pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
  3390. pps->constrained_intra_pred= get_bits1(&s->gb);
  3391. pps->redundant_pic_cnt_present = get_bits1(&s->gb);
  3392. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  3393. printf("pps:%d sps:%d %s slice_groups:%d ref:%d/%d %s qp:%d/%d/%d %s %s %s\n",
  3394. pps_id, pps->sps_id,
  3395. pps->cabac ? "CABAC" : "CAVLC",
  3396. pps->slice_group_count,
  3397. pps->ref_count[0], pps->ref_count[1],
  3398. pps->weighted_pred ? "weighted" : "",
  3399. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset,
  3400. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  3401. pps->constrained_intra_pred ? "CONSTR" : "",
  3402. pps->redundant_pic_cnt_present ? "REDU" : ""
  3403. );
  3404. }
  3405. return 0;
  3406. }
  3407. /**
  3408. * finds the end of the current frame in the bitstream.
  3409. * @return the position of the first byte of the next frame, or -1
  3410. */
  3411. static int find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
  3412. ParseContext *pc= &s->parse_context;
  3413. int i;
  3414. uint32_t state;
  3415. //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
  3416. // mb_addr= pc->mb_addr - 1;
  3417. state= pc->state;
  3418. //FIXME this will fail with slices
  3419. for(i=0; i<buf_size; i++){
  3420. state= (state<<8) | buf[i];
  3421. if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  3422. if(pc->frame_start_found){
  3423. pc->state=-1;
  3424. pc->frame_start_found= 0;
  3425. return i-3;
  3426. }
  3427. pc->frame_start_found= 1;
  3428. }
  3429. }
  3430. pc->state= state;
  3431. return END_NOT_FOUND;
  3432. }
  3433. static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){
  3434. MpegEncContext * const s = &h->s;
  3435. AVCodecContext * const avctx= s->avctx;
  3436. int buf_index=0;
  3437. #if 0
  3438. int i;
  3439. for(i=0; i<32; i++){
  3440. printf("%X ", buf[i]);
  3441. }
  3442. #endif
  3443. for(;;){
  3444. int consumed;
  3445. int dst_length;
  3446. int bit_length;
  3447. uint8_t *ptr;
  3448. // start code prefix search
  3449. for(; buf_index + 3 < buf_size; buf_index++){
  3450. // this should allways succeed in the first iteration
  3451. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  3452. break;
  3453. }
  3454. if(buf_index+3 >= buf_size) break;
  3455. buf_index+=3;
  3456. ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, buf_size - buf_index);
  3457. if(ptr[dst_length - 1] == 0) dst_length--;
  3458. bit_length= 8*dst_length - decode_rbsp_trailing(ptr + dst_length - 1);
  3459. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  3460. printf("NAL %d at %d length %d\n", h->nal_unit_type, buf_index, dst_length);
  3461. }
  3462. buf_index += consumed;
  3463. if(h->nal_ref_idc < s->hurry_up)
  3464. continue;
  3465. switch(h->nal_unit_type){
  3466. case NAL_IDR_SLICE:
  3467. idr(h); //FIXME ensure we dont loose some frames if there is reordering
  3468. case NAL_SLICE:
  3469. init_get_bits(&s->gb, ptr, bit_length);
  3470. h->intra_gb_ptr=
  3471. h->inter_gb_ptr= &s->gb;
  3472. s->data_partitioning = 0;
  3473. if(decode_slice_header(h) < 0) return -1;
  3474. if(h->redundant_pic_count==0)
  3475. decode_slice(h);
  3476. break;
  3477. case NAL_DPA:
  3478. init_get_bits(&s->gb, ptr, bit_length);
  3479. h->intra_gb_ptr=
  3480. h->inter_gb_ptr= NULL;
  3481. s->data_partitioning = 1;
  3482. if(decode_slice_header(h) < 0) return -1;
  3483. break;
  3484. case NAL_DPB:
  3485. init_get_bits(&h->intra_gb, ptr, bit_length);
  3486. h->intra_gb_ptr= &h->intra_gb;
  3487. break;
  3488. case NAL_DPC:
  3489. init_get_bits(&h->inter_gb, ptr, bit_length);
  3490. h->inter_gb_ptr= &h->inter_gb;
  3491. if(h->redundant_pic_count==0 && h->intra_gb_ptr && s->data_partitioning)
  3492. decode_slice(h);
  3493. break;
  3494. case NAL_SEI:
  3495. break;
  3496. case NAL_SPS:
  3497. init_get_bits(&s->gb, ptr, bit_length);
  3498. decode_seq_parameter_set(h);
  3499. if(s->flags& CODEC_FLAG_LOW_DELAY)
  3500. s->low_delay=1;
  3501. avctx->has_b_frames= !s->low_delay;
  3502. break;
  3503. case NAL_PPS:
  3504. init_get_bits(&s->gb, ptr, bit_length);
  3505. decode_picture_parameter_set(h);
  3506. break;
  3507. case NAL_PICTURE_DELIMITER:
  3508. break;
  3509. case NAL_FILTER_DATA:
  3510. break;
  3511. }
  3512. //FIXME move after where irt is set
  3513. s->current_picture.pict_type= s->pict_type;
  3514. s->current_picture.key_frame= s->pict_type == I_TYPE;
  3515. }
  3516. if(!s->current_picture_ptr) return buf_index; //no frame
  3517. h->prev_frame_num_offset= h->frame_num_offset;
  3518. h->prev_frame_num= h->frame_num;
  3519. if(s->current_picture_ptr->reference){
  3520. h->prev_poc_msb= h->poc_msb;
  3521. h->prev_poc_lsb= h->poc_lsb;
  3522. }
  3523. if(s->current_picture_ptr->reference)
  3524. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  3525. else
  3526. assert(h->mmco_index==0);
  3527. ff_er_frame_end(s);
  3528. MPV_frame_end(s);
  3529. return buf_index;
  3530. }
  3531. /**
  3532. * retunrs the number of bytes consumed for building the current frame
  3533. */
  3534. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  3535. if(s->flags&CODEC_FLAG_TRUNCATED){
  3536. pos -= s->parse_context.last_index;
  3537. if(pos<0) pos=0; // FIXME remove (uneeded?)
  3538. return pos;
  3539. }else{
  3540. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  3541. if(pos+10>buf_size) pos=buf_size; // oops ;)
  3542. return pos;
  3543. }
  3544. }
  3545. static int decode_frame(AVCodecContext *avctx,
  3546. void *data, int *data_size,
  3547. uint8_t *buf, int buf_size)
  3548. {
  3549. H264Context *h = avctx->priv_data;
  3550. MpegEncContext *s = &h->s;
  3551. AVFrame *pict = data;
  3552. int buf_index;
  3553. s->flags= avctx->flags;
  3554. *data_size = 0;
  3555. /* no supplementary picture */
  3556. if (buf_size == 0) {
  3557. return 0;
  3558. }
  3559. if(s->flags&CODEC_FLAG_TRUNCATED){
  3560. int next= find_frame_end(s, buf, buf_size);
  3561. if( ff_combine_frame(s, next, &buf, &buf_size) < 0 )
  3562. return buf_size;
  3563. //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);
  3564. }
  3565. if(s->avctx->extradata_size && s->picture_number==0){
  3566. if(0 < decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) )
  3567. return -1;
  3568. }
  3569. buf_index=decode_nal_units(h, buf, buf_size);
  3570. if(buf_index < 0)
  3571. return -1;
  3572. //FIXME do something with unavailable reference frames
  3573. // if(ret==FRAME_SKIPED) return get_consumed_bytes(s, buf_index, buf_size);
  3574. #if 0
  3575. if(s->pict_type==B_TYPE || s->low_delay){
  3576. *pict= *(AVFrame*)&s->current_picture;
  3577. } else {
  3578. *pict= *(AVFrame*)&s->last_picture;
  3579. }
  3580. #endif
  3581. if(!s->current_picture_ptr){
  3582. fprintf(stderr, "error, NO frame\n");
  3583. return -1;
  3584. }
  3585. *pict= *(AVFrame*)&s->current_picture; //FIXME
  3586. ff_print_debug_info(s, s->current_picture_ptr);
  3587. assert(pict->data[0]);
  3588. //printf("out %d\n", (int)pict->data[0]);
  3589. #if 0 //?
  3590. /* Return the Picture timestamp as the frame number */
  3591. /* we substract 1 because it is added on utils.c */
  3592. avctx->frame_number = s->picture_number - 1;
  3593. #endif
  3594. #if 0
  3595. /* dont output the last pic after seeking */
  3596. if(s->last_picture_ptr || s->low_delay)
  3597. //Note this isnt a issue as a IDR pic should flush teh buffers
  3598. #endif
  3599. *data_size = sizeof(AVFrame);
  3600. return get_consumed_bytes(s, buf_index, buf_size);
  3601. }
  3602. #if 0
  3603. static inline void fill_mb_avail(H264Context *h){
  3604. MpegEncContext * const s = &h->s;
  3605. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  3606. if(s->mb_y){
  3607. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  3608. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  3609. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  3610. }else{
  3611. h->mb_avail[0]=
  3612. h->mb_avail[1]=
  3613. h->mb_avail[2]= 0;
  3614. }
  3615. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  3616. h->mb_avail[4]= 1; //FIXME move out
  3617. h->mb_avail[5]= 0; //FIXME move out
  3618. }
  3619. #endif
  3620. #if 0 //selftest
  3621. #define COUNT 8000
  3622. #define SIZE (COUNT*40)
  3623. int main(){
  3624. int i;
  3625. uint8_t temp[SIZE];
  3626. PutBitContext pb;
  3627. GetBitContext gb;
  3628. // int int_temp[10000];
  3629. DSPContext dsp;
  3630. AVCodecContext avctx;
  3631. dsputil_init(&dsp, &avctx);
  3632. init_put_bits(&pb, temp, SIZE, NULL, NULL);
  3633. printf("testing unsigned exp golomb\n");
  3634. for(i=0; i<COUNT; i++){
  3635. START_TIMER
  3636. set_ue_golomb(&pb, i);
  3637. STOP_TIMER("set_ue_golomb");
  3638. }
  3639. flush_put_bits(&pb);
  3640. init_get_bits(&gb, temp, 8*SIZE);
  3641. for(i=0; i<COUNT; i++){
  3642. int j, s;
  3643. s= show_bits(&gb, 24);
  3644. START_TIMER
  3645. j= get_ue_golomb(&gb);
  3646. if(j != i){
  3647. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  3648. // return -1;
  3649. }
  3650. STOP_TIMER("get_ue_golomb");
  3651. }
  3652. init_put_bits(&pb, temp, SIZE, NULL, NULL);
  3653. printf("testing signed exp golomb\n");
  3654. for(i=0; i<COUNT; i++){
  3655. START_TIMER
  3656. set_se_golomb(&pb, i - COUNT/2);
  3657. STOP_TIMER("set_se_golomb");
  3658. }
  3659. flush_put_bits(&pb);
  3660. init_get_bits(&gb, temp, 8*SIZE);
  3661. for(i=0; i<COUNT; i++){
  3662. int j, s;
  3663. s= show_bits(&gb, 24);
  3664. START_TIMER
  3665. j= get_se_golomb(&gb);
  3666. if(j != i - COUNT/2){
  3667. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  3668. // return -1;
  3669. }
  3670. STOP_TIMER("get_se_golomb");
  3671. }
  3672. printf("testing 4x4 (I)DCT\n");
  3673. DCTELEM block[16];
  3674. uint8_t src[16], ref[16];
  3675. uint64_t error= 0, max_error=0;
  3676. for(i=0; i<COUNT; i++){
  3677. int j;
  3678. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  3679. for(j=0; j<16; j++){
  3680. ref[j]= random()%255;
  3681. src[j]= random()%255;
  3682. }
  3683. h264_diff_dct_c(block, src, ref, 4);
  3684. //normalize
  3685. for(j=0; j<16; j++){
  3686. // printf("%d ", block[j]);
  3687. block[j]= block[j]*4;
  3688. if(j&1) block[j]= (block[j]*4 + 2)/5;
  3689. if(j&4) block[j]= (block[j]*4 + 2)/5;
  3690. }
  3691. // printf("\n");
  3692. h264_add_idct_c(ref, block, 4);
  3693. /* for(j=0; j<16; j++){
  3694. printf("%d ", ref[j]);
  3695. }
  3696. printf("\n");*/
  3697. for(j=0; j<16; j++){
  3698. int diff= ABS(src[j] - ref[j]);
  3699. error+= diff*diff;
  3700. max_error= FFMAX(max_error, diff);
  3701. }
  3702. }
  3703. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  3704. #if 0
  3705. printf("testing quantizer\n");
  3706. for(qp=0; qp<52; qp++){
  3707. for(i=0; i<16; i++)
  3708. src1_block[i]= src2_block[i]= random()%255;
  3709. }
  3710. #endif
  3711. printf("Testing NAL layer\n");
  3712. uint8_t bitstream[COUNT];
  3713. uint8_t nal[COUNT*2];
  3714. H264Context h;
  3715. memset(&h, 0, sizeof(H264Context));
  3716. for(i=0; i<COUNT; i++){
  3717. int zeros= i;
  3718. int nal_length;
  3719. int consumed;
  3720. int out_length;
  3721. uint8_t *out;
  3722. int j;
  3723. for(j=0; j<COUNT; j++){
  3724. bitstream[j]= (random() % 255) + 1;
  3725. }
  3726. for(j=0; j<zeros; j++){
  3727. int pos= random() % COUNT;
  3728. while(bitstream[pos] == 0){
  3729. pos++;
  3730. pos %= COUNT;
  3731. }
  3732. bitstream[pos]=0;
  3733. }
  3734. START_TIMER
  3735. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  3736. if(nal_length<0){
  3737. printf("encoding failed\n");
  3738. return -1;
  3739. }
  3740. out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
  3741. STOP_TIMER("NAL")
  3742. if(out_length != COUNT){
  3743. printf("incorrect length %d %d\n", out_length, COUNT);
  3744. return -1;
  3745. }
  3746. if(consumed != nal_length){
  3747. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  3748. return -1;
  3749. }
  3750. if(memcmp(bitstream, out, COUNT)){
  3751. printf("missmatch\n");
  3752. return -1;
  3753. }
  3754. }
  3755. printf("Testing RBSP\n");
  3756. return 0;
  3757. }
  3758. #endif
  3759. static int decode_end(AVCodecContext *avctx)
  3760. {
  3761. H264Context *h = avctx->priv_data;
  3762. MpegEncContext *s = &h->s;
  3763. free_tables(h); //FIXME cleanup init stuff perhaps
  3764. MPV_common_end(s);
  3765. // memset(h, 0, sizeof(H264Context));
  3766. return 0;
  3767. }
  3768. AVCodec h264_decoder = {
  3769. "h264",
  3770. CODEC_TYPE_VIDEO,
  3771. CODEC_ID_H264,
  3772. sizeof(H264Context),
  3773. decode_init,
  3774. NULL,
  3775. decode_end,
  3776. decode_frame,
  3777. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  3778. };
  3779. #include "svq3.c"