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.

4399 lines
148KB

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