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.

4401 lines
149KB

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