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.

4893 lines
169KB

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