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.

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