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.

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