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.

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