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.

4774 lines
163KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file h264.c
  22. * H.264 / AVC / MPEG4 part10 codec.
  23. * @author Michael Niedermayer <michaelni@gmx.at>
  24. */
  25. #include "common.h"
  26. #include "dsputil.h"
  27. #include "avcodec.h"
  28. #include "mpegvideo.h"
  29. #include "h264data.h"
  30. #include "golomb.h"
  31. #undef NDEBUG
  32. #include <assert.h>
  33. #define interlaced_dct interlaced_dct_is_a_bad_name
  34. #define mb_intra mb_intra_isnt_initalized_see_mb_type
  35. #define LUMA_DC_BLOCK_INDEX 25
  36. #define CHROMA_DC_BLOCK_INDEX 26
  37. #define CHROMA_DC_COEFF_TOKEN_VLC_BITS 8
  38. #define COEFF_TOKEN_VLC_BITS 8
  39. #define TOTAL_ZEROS_VLC_BITS 9
  40. #define CHROMA_DC_TOTAL_ZEROS_VLC_BITS 3
  41. #define RUN_VLC_BITS 3
  42. #define RUN7_VLC_BITS 6
  43. #define MAX_SPS_COUNT 32
  44. #define MAX_PPS_COUNT 256
  45. #define MAX_MMCO_COUNT 66
  46. /**
  47. * Sequence parameter set
  48. */
  49. typedef struct SPS{
  50. int profile_idc;
  51. int level_idc;
  52. int log2_max_frame_num; ///< log2_max_frame_num_minus4 + 4
  53. int poc_type; ///< pic_order_cnt_type
  54. int log2_max_poc_lsb; ///< log2_max_pic_order_cnt_lsb_minus4
  55. int delta_pic_order_always_zero_flag;
  56. int offset_for_non_ref_pic;
  57. int offset_for_top_to_bottom_field;
  58. int poc_cycle_length; ///< num_ref_frames_in_pic_order_cnt_cycle
  59. int ref_frame_count; ///< num_ref_frames
  60. int gaps_in_frame_num_allowed_flag;
  61. int mb_width; ///< frame_width_in_mbs_minus1 + 1
  62. int mb_height; ///< frame_height_in_mbs_minus1 + 1
  63. int frame_mbs_only_flag;
  64. int mb_aff; ///<mb_adaptive_frame_field_flag
  65. int direct_8x8_inference_flag;
  66. int crop; ///< frame_cropping_flag
  67. int crop_left; ///< frame_cropping_rect_left_offset
  68. int crop_right; ///< frame_cropping_rect_right_offset
  69. int crop_top; ///< frame_cropping_rect_top_offset
  70. int crop_bottom; ///< frame_cropping_rect_bottom_offset
  71. int vui_parameters_present_flag;
  72. AVRational sar;
  73. short offset_for_ref_frame[256]; //FIXME dyn aloc?
  74. }SPS;
  75. /**
  76. * Picture parameter set
  77. */
  78. typedef struct PPS{
  79. int sps_id;
  80. int cabac; ///< entropy_coding_mode_flag
  81. int pic_order_present; ///< pic_order_present_flag
  82. int slice_group_count; ///< num_slice_groups_minus1 + 1
  83. int mb_slice_group_map_type;
  84. int ref_count[2]; ///< num_ref_idx_l0/1_active_minus1 + 1
  85. int weighted_pred; ///< weighted_pred_flag
  86. int weighted_bipred_idc;
  87. int init_qp; ///< pic_init_qp_minus26 + 26
  88. int init_qs; ///< pic_init_qs_minus26 + 26
  89. int chroma_qp_index_offset;
  90. int deblocking_filter_parameters_present; ///< deblocking_filter_parameters_present_flag
  91. int constrained_intra_pred; ///< constrained_intra_pred_flag
  92. int redundant_pic_cnt_present; ///< redundant_pic_cnt_present_flag
  93. }PPS;
  94. /**
  95. * Memory management control operation opcode.
  96. */
  97. typedef enum MMCOOpcode{
  98. MMCO_END=0,
  99. MMCO_SHORT2UNUSED,
  100. MMCO_LONG2UNUSED,
  101. MMCO_SHORT2LONG,
  102. MMCO_SET_MAX_LONG,
  103. MMCO_RESET,
  104. MMCO_LONG,
  105. } MMCOOpcode;
  106. /**
  107. * Memory management control operation.
  108. */
  109. typedef struct MMCO{
  110. MMCOOpcode opcode;
  111. int short_frame_num;
  112. int long_index;
  113. } MMCO;
  114. /**
  115. * H264Context
  116. */
  117. typedef struct H264Context{
  118. MpegEncContext s;
  119. int nal_ref_idc;
  120. int nal_unit_type;
  121. #define NAL_SLICE 1
  122. #define NAL_DPA 2
  123. #define NAL_DPB 3
  124. #define NAL_DPC 4
  125. #define NAL_IDR_SLICE 5
  126. #define NAL_SEI 6
  127. #define NAL_SPS 7
  128. #define NAL_PPS 8
  129. #define NAL_PICTURE_DELIMITER 9
  130. #define NAL_FILTER_DATA 10
  131. uint8_t *rbsp_buffer;
  132. int rbsp_buffer_size;
  133. int chroma_qp; //QPc
  134. int prev_mb_skiped; //FIXME remove (IMHO not used)
  135. //prediction stuff
  136. int chroma_pred_mode;
  137. int intra16x16_pred_mode;
  138. int8_t intra4x4_pred_mode_cache[5*8];
  139. int8_t (*intra4x4_pred_mode)[8];
  140. void (*pred4x4 [9+3])(uint8_t *src, uint8_t *topright, int stride);//FIXME move to dsp?
  141. void (*pred8x8 [4+3])(uint8_t *src, int stride);
  142. void (*pred16x16[4+3])(uint8_t *src, int stride);
  143. unsigned int topleft_samples_available;
  144. unsigned int top_samples_available;
  145. unsigned int topright_samples_available;
  146. unsigned int left_samples_available;
  147. /**
  148. * non zero coeff count cache.
  149. * is 64 if not available.
  150. */
  151. uint8_t non_zero_count_cache[6*8];
  152. uint8_t (*non_zero_count)[16+4+4]; /* store all values for deblocking filter */
  153. /**
  154. * Motion vector cache.
  155. */
  156. int16_t mv_cache[2][5*8][2];
  157. int8_t ref_cache[2][5*8];
  158. #define LIST_NOT_USED -1 //FIXME rename?
  159. #define PART_NOT_AVAILABLE -2
  160. /**
  161. * is 1 if the specific list MV&references are set to 0,0,-2.
  162. */
  163. int mv_cache_clean[2];
  164. int block_offset[16+8];
  165. int chroma_subblock_offset[16]; //FIXME remove
  166. uint16_t *mb2b_xy; //FIXME are these 4 a good idea?
  167. uint16_t *mb2b8_xy;
  168. int b_stride;
  169. int b8_stride;
  170. int halfpel_flag;
  171. int thirdpel_flag;
  172. int unknown_svq3_flag;
  173. int next_slice_index;
  174. SPS sps_buffer[MAX_SPS_COUNT];
  175. SPS sps; ///< current sps
  176. PPS pps_buffer[MAX_PPS_COUNT];
  177. /**
  178. * current pps
  179. */
  180. PPS pps; //FIXME move tp Picture perhaps? (->no) do we need that?
  181. int slice_num;
  182. uint8_t *slice_table_base;
  183. uint8_t *slice_table; ///< slice_table_base + mb_stride + 1
  184. int slice_type;
  185. int slice_type_fixed;
  186. //interlacing specific flags
  187. int mb_field_decoding_flag;
  188. int sub_mb_type[4];
  189. //POC stuff
  190. int poc_lsb;
  191. int poc_msb;
  192. int delta_poc_bottom;
  193. int delta_poc[2];
  194. int frame_num;
  195. int prev_poc_msb; ///< poc_msb of the last reference pic for POC type 0
  196. int prev_poc_lsb; ///< poc_lsb of the last reference pic for POC type 0
  197. int frame_num_offset; ///< for POC type 2
  198. int prev_frame_num_offset; ///< for POC type 2
  199. int prev_frame_num; ///< frame_num of the last pic for POC type 1/2
  200. /**
  201. * frame_num for frames or 2*frame_num for field pics.
  202. */
  203. int curr_pic_num;
  204. /**
  205. * max_frame_num or 2*max_frame_num for field pics.
  206. */
  207. int max_pic_num;
  208. //Weighted pred stuff
  209. int luma_log2_weight_denom;
  210. int chroma_log2_weight_denom;
  211. int luma_weight[2][16];
  212. int luma_offset[2][16];
  213. int chroma_weight[2][16][2];
  214. int chroma_offset[2][16][2];
  215. //deblock
  216. int disable_deblocking_filter_idc;
  217. int slice_alpha_c0_offset;
  218. int slice_beta_offset;
  219. int redundant_pic_count;
  220. int direct_spatial_mv_pred;
  221. /**
  222. * num_ref_idx_l0/1_active_minus1 + 1
  223. */
  224. int ref_count[2];// FIXME split for AFF
  225. Picture *short_ref[16];
  226. Picture *long_ref[16];
  227. Picture default_ref_list[2][32];
  228. Picture ref_list[2][32]; //FIXME size?
  229. Picture field_ref_list[2][32]; //FIXME size?
  230. /**
  231. * memory management control operations buffer.
  232. */
  233. MMCO mmco[MAX_MMCO_COUNT];
  234. int mmco_index;
  235. int long_ref_count; ///< number of actual long term references
  236. int short_ref_count; ///< number of actual short term references
  237. //data partitioning
  238. GetBitContext intra_gb;
  239. GetBitContext inter_gb;
  240. GetBitContext *intra_gb_ptr;
  241. GetBitContext *inter_gb_ptr;
  242. DCTELEM mb[16*24] __align8;
  243. }H264Context;
  244. static VLC coeff_token_vlc[4];
  245. static VLC chroma_dc_coeff_token_vlc;
  246. static VLC total_zeros_vlc[15];
  247. static VLC chroma_dc_total_zeros_vlc[3];
  248. static VLC run_vlc[6];
  249. static VLC run7_vlc;
  250. static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
  251. static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
  252. static inline uint32_t pack16to32(int a, int b){
  253. #ifdef WORDS_BIGENDIAN
  254. return (b&0xFFFF) + (a<<16);
  255. #else
  256. return (a&0xFFFF) + (b<<16);
  257. #endif
  258. }
  259. /**
  260. * fill a rectangle.
  261. * @param h height of the recatangle, should be a constant
  262. * @param w width of the recatangle, should be a constant
  263. * @param size the size of val (1 or 4), should be a constant
  264. */
  265. static inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){ //FIXME ensure this IS inlined
  266. uint8_t *p= (uint8_t*)vp;
  267. assert(size==1 || size==4);
  268. w *= size;
  269. stride *= size;
  270. //FIXME check what gcc generates for 64 bit on x86 and possible write a 32 bit ver of it
  271. if(w==2 && h==2){
  272. *(uint16_t*)(p + 0)=
  273. *(uint16_t*)(p + stride)= size==4 ? val : val*0x0101;
  274. }else if(w==2 && h==4){
  275. *(uint16_t*)(p + 0*stride)=
  276. *(uint16_t*)(p + 1*stride)=
  277. *(uint16_t*)(p + 2*stride)=
  278. *(uint16_t*)(p + 3*stride)= size==4 ? val : val*0x0101;
  279. }else if(w==4 && h==1){
  280. *(uint32_t*)(p + 0*stride)= size==4 ? val : val*0x01010101;
  281. }else if(w==4 && h==2){
  282. *(uint32_t*)(p + 0*stride)=
  283. *(uint32_t*)(p + 1*stride)= size==4 ? val : val*0x01010101;
  284. }else if(w==4 && h==4){
  285. *(uint32_t*)(p + 0*stride)=
  286. *(uint32_t*)(p + 1*stride)=
  287. *(uint32_t*)(p + 2*stride)=
  288. *(uint32_t*)(p + 3*stride)= size==4 ? val : val*0x01010101;
  289. }else if(w==8 && h==1){
  290. *(uint32_t*)(p + 0)=
  291. *(uint32_t*)(p + 4)= size==4 ? val : val*0x01010101;
  292. }else if(w==8 && h==2){
  293. *(uint32_t*)(p + 0 + 0*stride)=
  294. *(uint32_t*)(p + 4 + 0*stride)=
  295. *(uint32_t*)(p + 0 + 1*stride)=
  296. *(uint32_t*)(p + 4 + 1*stride)= size==4 ? val : val*0x01010101;
  297. }else if(w==8 && h==4){
  298. *(uint64_t*)(p + 0*stride)=
  299. *(uint64_t*)(p + 1*stride)=
  300. *(uint64_t*)(p + 2*stride)=
  301. *(uint64_t*)(p + 3*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
  302. }else if(w==16 && h==2){
  303. *(uint64_t*)(p + 0+0*stride)=
  304. *(uint64_t*)(p + 8+0*stride)=
  305. *(uint64_t*)(p + 0+1*stride)=
  306. *(uint64_t*)(p + 8+1*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
  307. }else if(w==16 && h==4){
  308. *(uint64_t*)(p + 0+0*stride)=
  309. *(uint64_t*)(p + 8+0*stride)=
  310. *(uint64_t*)(p + 0+1*stride)=
  311. *(uint64_t*)(p + 8+1*stride)=
  312. *(uint64_t*)(p + 0+2*stride)=
  313. *(uint64_t*)(p + 8+2*stride)=
  314. *(uint64_t*)(p + 0+3*stride)=
  315. *(uint64_t*)(p + 8+3*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
  316. }else
  317. assert(0);
  318. }
  319. static inline void fill_caches(H264Context *h, int mb_type){
  320. MpegEncContext * const s = &h->s;
  321. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  322. int topleft_xy, top_xy, topright_xy, left_xy[2];
  323. int topleft_type, top_type, topright_type, left_type[2];
  324. int left_block[4];
  325. int i;
  326. //wow what a mess, why didnt they simplify the interlacing&intra stuff, i cant imagine that these complex rules are worth it
  327. if(h->sps.mb_aff){
  328. //FIXME
  329. topleft_xy = 0; /* avoid warning */
  330. top_xy = 0; /* avoid warning */
  331. topright_xy = 0; /* avoid warning */
  332. }else{
  333. topleft_xy = mb_xy-1 - s->mb_stride;
  334. top_xy = mb_xy - s->mb_stride;
  335. topright_xy= mb_xy+1 - s->mb_stride;
  336. left_xy[0] = mb_xy-1;
  337. left_xy[1] = mb_xy-1;
  338. left_block[0]= 0;
  339. left_block[1]= 1;
  340. left_block[2]= 2;
  341. left_block[3]= 3;
  342. }
  343. topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
  344. top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
  345. topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
  346. left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
  347. left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
  348. if(IS_INTRA(mb_type)){
  349. h->topleft_samples_available=
  350. h->top_samples_available=
  351. h->left_samples_available= 0xFFFF;
  352. h->topright_samples_available= 0xEEEA;
  353. if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
  354. h->topleft_samples_available= 0xB3FF;
  355. h->top_samples_available= 0x33FF;
  356. h->topright_samples_available= 0x26EA;
  357. }
  358. for(i=0; i<2; i++){
  359. if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
  360. h->topleft_samples_available&= 0xDF5F;
  361. h->left_samples_available&= 0x5F5F;
  362. }
  363. }
  364. if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
  365. h->topleft_samples_available&= 0x7FFF;
  366. if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
  367. h->topright_samples_available&= 0xFBFF;
  368. if(IS_INTRA4x4(mb_type)){
  369. if(IS_INTRA4x4(top_type)){
  370. h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
  371. h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
  372. h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
  373. h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
  374. }else{
  375. int pred;
  376. if(IS_INTRA16x16(top_type) || (IS_INTER(top_type) && !h->pps.constrained_intra_pred))
  377. pred= 2;
  378. else{
  379. pred= -1;
  380. }
  381. h->intra4x4_pred_mode_cache[4+8*0]=
  382. h->intra4x4_pred_mode_cache[5+8*0]=
  383. h->intra4x4_pred_mode_cache[6+8*0]=
  384. h->intra4x4_pred_mode_cache[7+8*0]= pred;
  385. }
  386. for(i=0; i<2; i++){
  387. if(IS_INTRA4x4(left_type[i])){
  388. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
  389. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
  390. }else{
  391. int pred;
  392. if(IS_INTRA16x16(left_type[i]) || (IS_INTER(left_type[i]) && !h->pps.constrained_intra_pred))
  393. pred= 2;
  394. else{
  395. pred= -1;
  396. }
  397. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
  398. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
  399. }
  400. }
  401. }
  402. }
  403. /*
  404. 0 . T T. T T T T
  405. 1 L . .L . . . .
  406. 2 L . .L . . . .
  407. 3 . T TL . . . .
  408. 4 L . .L . . . .
  409. 5 L . .. . . . .
  410. */
  411. //FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec)
  412. if(top_type){
  413. h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][10];
  414. h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][11];
  415. h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][14];
  416. h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][15];
  417. h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][18];
  418. h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][19];
  419. h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][22];
  420. h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][23];
  421. }else{
  422. h->non_zero_count_cache[4+8*0]=
  423. h->non_zero_count_cache[5+8*0]=
  424. h->non_zero_count_cache[6+8*0]=
  425. h->non_zero_count_cache[7+8*0]=
  426. h->non_zero_count_cache[1+8*0]=
  427. h->non_zero_count_cache[2+8*0]=
  428. h->non_zero_count_cache[1+8*3]=
  429. h->non_zero_count_cache[2+8*3]= 64;
  430. }
  431. if(left_type[0]){
  432. h->non_zero_count_cache[3+8*1]= h->non_zero_count[left_xy[0]][5];
  433. h->non_zero_count_cache[3+8*2]= h->non_zero_count[left_xy[0]][7];
  434. h->non_zero_count_cache[0+8*1]= h->non_zero_count[left_xy[0]][17]; //FIXME left_block
  435. h->non_zero_count_cache[0+8*4]= h->non_zero_count[left_xy[0]][21];
  436. }else{
  437. h->non_zero_count_cache[3+8*1]=
  438. h->non_zero_count_cache[3+8*2]=
  439. h->non_zero_count_cache[0+8*1]=
  440. h->non_zero_count_cache[0+8*4]= 64;
  441. }
  442. if(left_type[1]){
  443. h->non_zero_count_cache[3+8*3]= h->non_zero_count[left_xy[1]][13];
  444. h->non_zero_count_cache[3+8*4]= h->non_zero_count[left_xy[1]][15];
  445. h->non_zero_count_cache[0+8*2]= h->non_zero_count[left_xy[1]][19];
  446. h->non_zero_count_cache[0+8*5]= h->non_zero_count[left_xy[1]][23];
  447. }else{
  448. h->non_zero_count_cache[3+8*3]=
  449. h->non_zero_count_cache[3+8*4]=
  450. h->non_zero_count_cache[0+8*2]=
  451. h->non_zero_count_cache[0+8*5]= 64;
  452. }
  453. #if 1
  454. if(IS_INTER(mb_type)){
  455. int list;
  456. for(list=0; list<2; list++){
  457. if((!IS_8X8(mb_type)) && !USES_LIST(mb_type, list)){
  458. /*if(!h->mv_cache_clean[list]){
  459. memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
  460. memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
  461. h->mv_cache_clean[list]= 1;
  462. }*/
  463. continue; //FIXME direct mode ...
  464. }
  465. h->mv_cache_clean[list]= 0;
  466. if(IS_INTER(topleft_type)){
  467. const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
  468. const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + h->b8_stride;
  469. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  470. h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  471. }else{
  472. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
  473. h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  474. }
  475. if(IS_INTER(top_type)){
  476. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  477. const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
  478. *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
  479. *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
  480. *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
  481. *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
  482. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  483. h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
  484. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  485. h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
  486. }else{
  487. *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
  488. *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
  489. *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
  490. *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
  491. *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
  492. }
  493. if(IS_INTER(topright_type)){
  494. const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
  495. const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
  496. *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  497. h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  498. }else{
  499. *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
  500. h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  501. }
  502. //FIXME unify cleanup or sth
  503. if(IS_INTER(left_type[0])){
  504. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  505. const int b8_xy= h->mb2b8_xy[left_xy[0]] + 1;
  506. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 + 0*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[0]];
  507. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 + 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[1]];
  508. h->ref_cache[list][scan8[0] - 1 + 0*8]=
  509. h->ref_cache[list][scan8[0] - 1 + 1*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0]>>1)];
  510. }else{
  511. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 0*8]=
  512. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 1*8]= 0;
  513. h->ref_cache[list][scan8[0] - 1 + 0*8]=
  514. h->ref_cache[list][scan8[0] - 1 + 1*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  515. }
  516. if(IS_INTER(left_type[1])){
  517. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  518. const int b8_xy= h->mb2b8_xy[left_xy[1]] + 1;
  519. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 + 2*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[2]];
  520. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 + 3*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[3]];
  521. h->ref_cache[list][scan8[0] - 1 + 2*8]=
  522. h->ref_cache[list][scan8[0] - 1 + 3*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[2]>>1)];
  523. }else{
  524. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 2*8]=
  525. *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 3*8]= 0;
  526. h->ref_cache[list][scan8[0] - 1 + 2*8]=
  527. h->ref_cache[list][scan8[0] - 1 + 3*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  528. }
  529. h->ref_cache[list][scan8[5 ]+1] =
  530. h->ref_cache[list][scan8[7 ]+1] =
  531. h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewher else)
  532. h->ref_cache[list][scan8[4 ]] =
  533. h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
  534. *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
  535. *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
  536. *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewher else)
  537. *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
  538. *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
  539. }
  540. //FIXME
  541. }
  542. #endif
  543. }
  544. static inline void write_back_intra_pred_mode(H264Context *h){
  545. MpegEncContext * const s = &h->s;
  546. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  547. h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
  548. h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
  549. h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
  550. h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
  551. h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
  552. h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
  553. h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
  554. }
  555. /**
  556. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  557. */
  558. static inline int check_intra4x4_pred_mode(H264Context *h){
  559. MpegEncContext * const s = &h->s;
  560. static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
  561. static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
  562. int i;
  563. if(!(h->top_samples_available&0x8000)){
  564. for(i=0; i<4; i++){
  565. int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
  566. if(status<0){
  567. av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
  568. return -1;
  569. } else if(status){
  570. h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
  571. }
  572. }
  573. }
  574. if(!(h->left_samples_available&0x8000)){
  575. for(i=0; i<4; i++){
  576. int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
  577. if(status<0){
  578. av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
  579. return -1;
  580. } else if(status){
  581. h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
  582. }
  583. }
  584. }
  585. return 0;
  586. } //FIXME cleanup like next
  587. /**
  588. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  589. */
  590. static inline int check_intra_pred_mode(H264Context *h, int mode){
  591. MpegEncContext * const s = &h->s;
  592. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  593. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  594. if(!(h->top_samples_available&0x8000)){
  595. mode= top[ mode ];
  596. if(mode<0){
  597. av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
  598. return -1;
  599. }
  600. }
  601. if(!(h->left_samples_available&0x8000)){
  602. mode= left[ mode ];
  603. if(mode<0){
  604. av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
  605. return -1;
  606. }
  607. }
  608. return mode;
  609. }
  610. /**
  611. * gets the predicted intra4x4 prediction mode.
  612. */
  613. static inline int pred_intra_mode(H264Context *h, int n){
  614. const int index8= scan8[n];
  615. const int left= h->intra4x4_pred_mode_cache[index8 - 1];
  616. const int top = h->intra4x4_pred_mode_cache[index8 - 8];
  617. const int min= FFMIN(left, top);
  618. tprintf("mode:%d %d min:%d\n", left ,top, min);
  619. if(min<0) return DC_PRED;
  620. else return min;
  621. }
  622. static inline void write_back_non_zero_count(H264Context *h){
  623. MpegEncContext * const s = &h->s;
  624. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  625. int n;
  626. for( n = 0; n < 16+4+4; n++ )
  627. h->non_zero_count[mb_xy][n] = h->non_zero_count_cache[scan8[n]];
  628. }
  629. /**
  630. * gets the predicted number of non zero coefficients.
  631. * @param n block index
  632. */
  633. static inline int pred_non_zero_count(H264Context *h, int n){
  634. const int index8= scan8[n];
  635. const int left= h->non_zero_count_cache[index8 - 1];
  636. const int top = h->non_zero_count_cache[index8 - 8];
  637. int i= left + top;
  638. if(i<64) i= (i+1)>>1;
  639. tprintf("pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
  640. return i&31;
  641. }
  642. static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
  643. const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
  644. if(topright_ref != PART_NOT_AVAILABLE){
  645. *C= h->mv_cache[list][ i - 8 + part_width ];
  646. return topright_ref;
  647. }else{
  648. tprintf("topright MV not available\n");
  649. *C= h->mv_cache[list][ i - 8 - 1 ];
  650. return h->ref_cache[list][ i - 8 - 1 ];
  651. }
  652. }
  653. /**
  654. * gets the predicted MV.
  655. * @param n the block index
  656. * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
  657. * @param mx the x component of the predicted motion vector
  658. * @param my the y component of the predicted motion vector
  659. */
  660. static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
  661. const int index8= scan8[n];
  662. const int top_ref= h->ref_cache[list][ index8 - 8 ];
  663. const int left_ref= h->ref_cache[list][ index8 - 1 ];
  664. const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
  665. const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
  666. const int16_t * C;
  667. int diagonal_ref, match_count;
  668. assert(part_width==1 || part_width==2 || part_width==4);
  669. /* mv_cache
  670. B . . A T T T T
  671. U . . L . . , .
  672. U . . L . . . .
  673. U . . L . . , .
  674. . . . L . . . .
  675. */
  676. diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
  677. match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
  678. if(match_count > 1){ //most common
  679. *mx= mid_pred(A[0], B[0], C[0]);
  680. *my= mid_pred(A[1], B[1], C[1]);
  681. }else if(match_count==1){
  682. if(left_ref==ref){
  683. *mx= A[0];
  684. *my= A[1];
  685. }else if(top_ref==ref){
  686. *mx= B[0];
  687. *my= B[1];
  688. }else{
  689. *mx= C[0];
  690. *my= C[1];
  691. }
  692. }else{
  693. if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
  694. *mx= A[0];
  695. *my= A[1];
  696. }else{
  697. *mx= mid_pred(A[0], B[0], C[0]);
  698. *my= mid_pred(A[1], B[1], C[1]);
  699. }
  700. }
  701. tprintf("pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], diagonal_ref, C[0], C[1], left_ref, A[0], A[1], ref, *mx, *my, h->s.mb_x, h->s.mb_y, n, list);
  702. }
  703. /**
  704. * gets the directionally predicted 16x8 MV.
  705. * @param n the block index
  706. * @param mx the x component of the predicted motion vector
  707. * @param my the y component of the predicted motion vector
  708. */
  709. static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  710. if(n==0){
  711. const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
  712. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  713. tprintf("pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d", top_ref, B[0], B[1], h->s.mb_x, h->s.mb_y, n, list);
  714. if(top_ref == ref){
  715. *mx= B[0];
  716. *my= B[1];
  717. return;
  718. }
  719. }else{
  720. const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
  721. const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
  722. tprintf("pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
  723. if(left_ref == ref){
  724. *mx= A[0];
  725. *my= A[1];
  726. return;
  727. }
  728. }
  729. //RARE
  730. pred_motion(h, n, 4, list, ref, mx, my);
  731. }
  732. /**
  733. * gets the directionally predicted 8x16 MV.
  734. * @param n the block index
  735. * @param mx the x component of the predicted motion vector
  736. * @param my the y component of the predicted motion vector
  737. */
  738. static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  739. if(n==0){
  740. const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
  741. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  742. tprintf("pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
  743. if(left_ref == ref){
  744. *mx= A[0];
  745. *my= A[1];
  746. return;
  747. }
  748. }else{
  749. const int16_t * C;
  750. int diagonal_ref;
  751. diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
  752. tprintf("pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d", diagonal_ref, C[0], C[1], h->s.mb_x, h->s.mb_y, n, list);
  753. if(diagonal_ref == ref){
  754. *mx= C[0];
  755. *my= C[1];
  756. return;
  757. }
  758. }
  759. //RARE
  760. pred_motion(h, n, 2, list, ref, mx, my);
  761. }
  762. static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
  763. const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
  764. const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
  765. tprintf("pred_pskip: (%d) (%d) at %2d %2d", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
  766. if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
  767. || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
  768. || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
  769. *mx = *my = 0;
  770. return;
  771. }
  772. pred_motion(h, 0, 4, 0, 0, mx, my);
  773. return;
  774. }
  775. static inline void write_back_motion(H264Context *h, int mb_type){
  776. MpegEncContext * const s = &h->s;
  777. const int 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= (-put_bits_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 inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){
  1452. int i, j, k;
  1453. int a;
  1454. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  1455. const uint8_t * const src0 = src+7-stride;
  1456. const uint8_t *src1 = src+8*stride-1;
  1457. const uint8_t *src2 = src1-2*stride; // == src+6*stride-1;
  1458. int H = src0[1] - src0[-1];
  1459. int V = src1[0] - src2[ 0];
  1460. for(k=2; k<=8; ++k) {
  1461. src1 += stride; src2 -= stride;
  1462. H += k*(src0[k] - src0[-k]);
  1463. V += k*(src1[0] - src2[ 0]);
  1464. }
  1465. if(svq3){
  1466. H = ( 5*(H/4) ) / 16;
  1467. V = ( 5*(V/4) ) / 16;
  1468. /* required for 100% accuracy */
  1469. i = H; H = V; V = i;
  1470. }else{
  1471. H = ( 5*H+32 ) >> 6;
  1472. V = ( 5*V+32 ) >> 6;
  1473. }
  1474. a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
  1475. for(j=16; j>0; --j) {
  1476. int b = a;
  1477. a += V;
  1478. for(i=-16; i<0; i+=4) {
  1479. src[16+i] = cm[ (b ) >> 5 ];
  1480. src[17+i] = cm[ (b+ H) >> 5 ];
  1481. src[18+i] = cm[ (b+2*H) >> 5 ];
  1482. src[19+i] = cm[ (b+3*H) >> 5 ];
  1483. b += 4*H;
  1484. }
  1485. src += stride;
  1486. }
  1487. }
  1488. static void pred16x16_plane_c(uint8_t *src, int stride){
  1489. pred16x16_plane_compat_c(src, stride, 0);
  1490. }
  1491. static void pred8x8_vertical_c(uint8_t *src, int stride){
  1492. int i;
  1493. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1494. const uint32_t b= ((uint32_t*)(src-stride))[1];
  1495. for(i=0; i<8; i++){
  1496. ((uint32_t*)(src+i*stride))[0]= a;
  1497. ((uint32_t*)(src+i*stride))[1]= b;
  1498. }
  1499. }
  1500. static void pred8x8_horizontal_c(uint8_t *src, int stride){
  1501. int i;
  1502. for(i=0; i<8; i++){
  1503. ((uint32_t*)(src+i*stride))[0]=
  1504. ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101;
  1505. }
  1506. }
  1507. static void pred8x8_128_dc_c(uint8_t *src, int stride){
  1508. int i;
  1509. for(i=0; i<4; i++){
  1510. ((uint32_t*)(src+i*stride))[0]=
  1511. ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
  1512. }
  1513. for(i=4; i<8; i++){
  1514. ((uint32_t*)(src+i*stride))[0]=
  1515. ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
  1516. }
  1517. }
  1518. static void pred8x8_left_dc_c(uint8_t *src, int stride){
  1519. int i;
  1520. int dc0, dc2;
  1521. dc0=dc2=0;
  1522. for(i=0;i<4; i++){
  1523. dc0+= src[-1+i*stride];
  1524. dc2+= src[-1+(i+4)*stride];
  1525. }
  1526. dc0= 0x01010101*((dc0 + 2)>>2);
  1527. dc2= 0x01010101*((dc2 + 2)>>2);
  1528. for(i=0; i<4; i++){
  1529. ((uint32_t*)(src+i*stride))[0]=
  1530. ((uint32_t*)(src+i*stride))[1]= dc0;
  1531. }
  1532. for(i=4; i<8; i++){
  1533. ((uint32_t*)(src+i*stride))[0]=
  1534. ((uint32_t*)(src+i*stride))[1]= dc2;
  1535. }
  1536. }
  1537. static void pred8x8_top_dc_c(uint8_t *src, int stride){
  1538. int i;
  1539. int dc0, dc1;
  1540. dc0=dc1=0;
  1541. for(i=0;i<4; i++){
  1542. dc0+= src[i-stride];
  1543. dc1+= src[4+i-stride];
  1544. }
  1545. dc0= 0x01010101*((dc0 + 2)>>2);
  1546. dc1= 0x01010101*((dc1 + 2)>>2);
  1547. for(i=0; i<4; i++){
  1548. ((uint32_t*)(src+i*stride))[0]= dc0;
  1549. ((uint32_t*)(src+i*stride))[1]= dc1;
  1550. }
  1551. for(i=4; i<8; i++){
  1552. ((uint32_t*)(src+i*stride))[0]= dc0;
  1553. ((uint32_t*)(src+i*stride))[1]= dc1;
  1554. }
  1555. }
  1556. static void pred8x8_dc_c(uint8_t *src, int stride){
  1557. int i;
  1558. int dc0, dc1, dc2, dc3;
  1559. dc0=dc1=dc2=0;
  1560. for(i=0;i<4; i++){
  1561. dc0+= src[-1+i*stride] + src[i-stride];
  1562. dc1+= src[4+i-stride];
  1563. dc2+= src[-1+(i+4)*stride];
  1564. }
  1565. dc3= 0x01010101*((dc1 + dc2 + 4)>>3);
  1566. dc0= 0x01010101*((dc0 + 4)>>3);
  1567. dc1= 0x01010101*((dc1 + 2)>>2);
  1568. dc2= 0x01010101*((dc2 + 2)>>2);
  1569. for(i=0; i<4; i++){
  1570. ((uint32_t*)(src+i*stride))[0]= dc0;
  1571. ((uint32_t*)(src+i*stride))[1]= dc1;
  1572. }
  1573. for(i=4; i<8; i++){
  1574. ((uint32_t*)(src+i*stride))[0]= dc2;
  1575. ((uint32_t*)(src+i*stride))[1]= dc3;
  1576. }
  1577. }
  1578. static void pred8x8_plane_c(uint8_t *src, int stride){
  1579. int j, k;
  1580. int a;
  1581. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  1582. const uint8_t * const src0 = src+3-stride;
  1583. const uint8_t *src1 = src+4*stride-1;
  1584. const uint8_t *src2 = src1-2*stride; // == src+2*stride-1;
  1585. int H = src0[1] - src0[-1];
  1586. int V = src1[0] - src2[ 0];
  1587. for(k=2; k<=4; ++k) {
  1588. src1 += stride; src2 -= stride;
  1589. H += k*(src0[k] - src0[-k]);
  1590. V += k*(src1[0] - src2[ 0]);
  1591. }
  1592. H = ( 17*H+16 ) >> 5;
  1593. V = ( 17*V+16 ) >> 5;
  1594. a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
  1595. for(j=8; j>0; --j) {
  1596. int b = a;
  1597. a += V;
  1598. src[0] = cm[ (b ) >> 5 ];
  1599. src[1] = cm[ (b+ H) >> 5 ];
  1600. src[2] = cm[ (b+2*H) >> 5 ];
  1601. src[3] = cm[ (b+3*H) >> 5 ];
  1602. src[4] = cm[ (b+4*H) >> 5 ];
  1603. src[5] = cm[ (b+5*H) >> 5 ];
  1604. src[6] = cm[ (b+6*H) >> 5 ];
  1605. src[7] = cm[ (b+7*H) >> 5 ];
  1606. src += stride;
  1607. }
  1608. }
  1609. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  1610. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1611. int src_x_offset, int src_y_offset,
  1612. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  1613. MpegEncContext * const s = &h->s;
  1614. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  1615. const int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  1616. const int luma_xy= (mx&3) + ((my&3)<<2);
  1617. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*s->linesize;
  1618. uint8_t * src_cb= pic->data[1] + (mx>>3) + (my>>3)*s->uvlinesize;
  1619. uint8_t * src_cr= pic->data[2] + (mx>>3) + (my>>3)*s->uvlinesize;
  1620. int extra_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16; //FIXME increase edge?, IMHO not worth it
  1621. int extra_height= extra_width;
  1622. int emu=0;
  1623. const int full_mx= mx>>2;
  1624. const int full_my= my>>2;
  1625. assert(pic->data[0]);
  1626. if(mx&7) extra_width -= 3;
  1627. if(my&7) extra_height -= 3;
  1628. if( full_mx < 0-extra_width
  1629. || full_my < 0-extra_height
  1630. || full_mx + 16/*FIXME*/ > s->width + extra_width
  1631. || full_my + 16/*FIXME*/ > s->height + extra_height){
  1632. ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*s->linesize, s->linesize, 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, s->width, s->height);
  1633. src_y= s->edge_emu_buffer + 2 + 2*s->linesize;
  1634. emu=1;
  1635. }
  1636. qpix_op[luma_xy](dest_y, src_y, s->linesize); //FIXME try variable height perhaps?
  1637. if(!square){
  1638. qpix_op[luma_xy](dest_y + delta, src_y + delta, s->linesize);
  1639. }
  1640. if(s->flags&CODEC_FLAG_GRAY) return;
  1641. if(emu){
  1642. ff_emulated_edge_mc(s->edge_emu_buffer, src_cb, s->uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), s->width>>1, s->height>>1);
  1643. src_cb= s->edge_emu_buffer;
  1644. }
  1645. chroma_op(dest_cb, src_cb, s->uvlinesize, chroma_height, mx&7, my&7);
  1646. if(emu){
  1647. ff_emulated_edge_mc(s->edge_emu_buffer, src_cr, s->uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), s->width>>1, s->height>>1);
  1648. src_cr= s->edge_emu_buffer;
  1649. }
  1650. chroma_op(dest_cr, src_cr, s->uvlinesize, chroma_height, mx&7, my&7);
  1651. }
  1652. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  1653. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1654. int x_offset, int y_offset,
  1655. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  1656. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  1657. int list0, int list1){
  1658. MpegEncContext * const s = &h->s;
  1659. qpel_mc_func *qpix_op= qpix_put;
  1660. h264_chroma_mc_func chroma_op= chroma_put;
  1661. dest_y += 2*x_offset + 2*y_offset*s-> linesize;
  1662. dest_cb += x_offset + y_offset*s->uvlinesize;
  1663. dest_cr += x_offset + y_offset*s->uvlinesize;
  1664. x_offset += 8*s->mb_x;
  1665. y_offset += 8*s->mb_y;
  1666. if(list0){
  1667. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  1668. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  1669. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1670. qpix_op, chroma_op);
  1671. qpix_op= qpix_avg;
  1672. chroma_op= chroma_avg;
  1673. }
  1674. if(list1){
  1675. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  1676. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  1677. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1678. qpix_op, chroma_op);
  1679. }
  1680. }
  1681. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1682. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  1683. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg)){
  1684. MpegEncContext * const s = &h->s;
  1685. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  1686. const int mb_type= s->current_picture.mb_type[mb_xy];
  1687. assert(IS_INTER(mb_type));
  1688. if(IS_16X16(mb_type)){
  1689. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  1690. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  1691. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  1692. }else if(IS_16X8(mb_type)){
  1693. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  1694. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  1695. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  1696. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  1697. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  1698. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  1699. }else if(IS_8X16(mb_type)){
  1700. mc_part(h, 0, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 0, 0,
  1701. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  1702. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  1703. mc_part(h, 4, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 4, 0,
  1704. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  1705. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  1706. }else{
  1707. int i;
  1708. assert(IS_8X8(mb_type));
  1709. for(i=0; i<4; i++){
  1710. const int sub_mb_type= h->sub_mb_type[i];
  1711. const int n= 4*i;
  1712. int x_offset= (i&1)<<2;
  1713. int y_offset= (i&2)<<1;
  1714. if(IS_SUB_8X8(sub_mb_type)){
  1715. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1716. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  1717. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1718. }else if(IS_SUB_8X4(sub_mb_type)){
  1719. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1720. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  1721. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1722. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  1723. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  1724. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1725. }else if(IS_SUB_4X8(sub_mb_type)){
  1726. mc_part(h, n , 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1727. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  1728. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1729. mc_part(h, n+1, 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset+2, 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. }else{
  1733. int j;
  1734. assert(IS_SUB_4X4(sub_mb_type));
  1735. for(j=0; j<4; j++){
  1736. int sub_x_offset= x_offset + 2*(j&1);
  1737. int sub_y_offset= y_offset + (j&2);
  1738. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  1739. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  1740. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1741. }
  1742. }
  1743. }
  1744. }
  1745. }
  1746. static void decode_init_vlc(H264Context *h){
  1747. static int done = 0;
  1748. if (!done) {
  1749. int i;
  1750. done = 1;
  1751. init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
  1752. &chroma_dc_coeff_token_len [0], 1, 1,
  1753. &chroma_dc_coeff_token_bits[0], 1, 1);
  1754. for(i=0; i<4; i++){
  1755. init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
  1756. &coeff_token_len [i][0], 1, 1,
  1757. &coeff_token_bits[i][0], 1, 1);
  1758. }
  1759. for(i=0; i<3; i++){
  1760. init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
  1761. &chroma_dc_total_zeros_len [i][0], 1, 1,
  1762. &chroma_dc_total_zeros_bits[i][0], 1, 1);
  1763. }
  1764. for(i=0; i<15; i++){
  1765. init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
  1766. &total_zeros_len [i][0], 1, 1,
  1767. &total_zeros_bits[i][0], 1, 1);
  1768. }
  1769. for(i=0; i<6; i++){
  1770. init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
  1771. &run_len [i][0], 1, 1,
  1772. &run_bits[i][0], 1, 1);
  1773. }
  1774. init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
  1775. &run_len [6][0], 1, 1,
  1776. &run_bits[6][0], 1, 1);
  1777. }
  1778. }
  1779. /**
  1780. * Sets the intra prediction function pointers.
  1781. */
  1782. static void init_pred_ptrs(H264Context *h){
  1783. // MpegEncContext * const s = &h->s;
  1784. h->pred4x4[VERT_PRED ]= pred4x4_vertical_c;
  1785. h->pred4x4[HOR_PRED ]= pred4x4_horizontal_c;
  1786. h->pred4x4[DC_PRED ]= pred4x4_dc_c;
  1787. h->pred4x4[DIAG_DOWN_LEFT_PRED ]= pred4x4_down_left_c;
  1788. h->pred4x4[DIAG_DOWN_RIGHT_PRED]= pred4x4_down_right_c;
  1789. h->pred4x4[VERT_RIGHT_PRED ]= pred4x4_vertical_right_c;
  1790. h->pred4x4[HOR_DOWN_PRED ]= pred4x4_horizontal_down_c;
  1791. h->pred4x4[VERT_LEFT_PRED ]= pred4x4_vertical_left_c;
  1792. h->pred4x4[HOR_UP_PRED ]= pred4x4_horizontal_up_c;
  1793. h->pred4x4[LEFT_DC_PRED ]= pred4x4_left_dc_c;
  1794. h->pred4x4[TOP_DC_PRED ]= pred4x4_top_dc_c;
  1795. h->pred4x4[DC_128_PRED ]= pred4x4_128_dc_c;
  1796. h->pred8x8[DC_PRED8x8 ]= pred8x8_dc_c;
  1797. h->pred8x8[VERT_PRED8x8 ]= pred8x8_vertical_c;
  1798. h->pred8x8[HOR_PRED8x8 ]= pred8x8_horizontal_c;
  1799. h->pred8x8[PLANE_PRED8x8 ]= pred8x8_plane_c;
  1800. h->pred8x8[LEFT_DC_PRED8x8]= pred8x8_left_dc_c;
  1801. h->pred8x8[TOP_DC_PRED8x8 ]= pred8x8_top_dc_c;
  1802. h->pred8x8[DC_128_PRED8x8 ]= pred8x8_128_dc_c;
  1803. h->pred16x16[DC_PRED8x8 ]= pred16x16_dc_c;
  1804. h->pred16x16[VERT_PRED8x8 ]= pred16x16_vertical_c;
  1805. h->pred16x16[HOR_PRED8x8 ]= pred16x16_horizontal_c;
  1806. h->pred16x16[PLANE_PRED8x8 ]= pred16x16_plane_c;
  1807. h->pred16x16[LEFT_DC_PRED8x8]= pred16x16_left_dc_c;
  1808. h->pred16x16[TOP_DC_PRED8x8 ]= pred16x16_top_dc_c;
  1809. h->pred16x16[DC_128_PRED8x8 ]= pred16x16_128_dc_c;
  1810. }
  1811. static void free_tables(H264Context *h){
  1812. av_freep(&h->intra4x4_pred_mode);
  1813. av_freep(&h->non_zero_count);
  1814. av_freep(&h->slice_table_base);
  1815. h->slice_table= NULL;
  1816. av_freep(&h->mb2b_xy);
  1817. av_freep(&h->mb2b8_xy);
  1818. }
  1819. /**
  1820. * allocates tables.
  1821. * needs widzh/height
  1822. */
  1823. static int alloc_tables(H264Context *h){
  1824. MpegEncContext * const s = &h->s;
  1825. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  1826. int x,y;
  1827. CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
  1828. CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * (16+4+4) * sizeof(uint8_t))
  1829. CHECKED_ALLOCZ(h->slice_table_base , big_mb_num * sizeof(uint8_t))
  1830. memset(h->slice_table_base, -1, big_mb_num * sizeof(uint8_t));
  1831. h->slice_table= h->slice_table_base + s->mb_stride + 1;
  1832. CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint16_t));
  1833. CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint16_t));
  1834. for(y=0; y<s->mb_height; y++){
  1835. for(x=0; x<s->mb_width; x++){
  1836. const int mb_xy= x + y*s->mb_stride;
  1837. const int b_xy = 4*x + 4*y*h->b_stride;
  1838. const int b8_xy= 2*x + 2*y*h->b8_stride;
  1839. h->mb2b_xy [mb_xy]= b_xy;
  1840. h->mb2b8_xy[mb_xy]= b8_xy;
  1841. }
  1842. }
  1843. return 0;
  1844. fail:
  1845. free_tables(h);
  1846. return -1;
  1847. }
  1848. static void common_init(H264Context *h){
  1849. MpegEncContext * const s = &h->s;
  1850. s->width = s->avctx->width;
  1851. s->height = s->avctx->height;
  1852. s->codec_id= s->avctx->codec->id;
  1853. init_pred_ptrs(h);
  1854. s->unrestricted_mv=1;
  1855. s->decode=1; //FIXME
  1856. }
  1857. static int decode_init(AVCodecContext *avctx){
  1858. H264Context *h= avctx->priv_data;
  1859. MpegEncContext * const s = &h->s;
  1860. MPV_decode_defaults(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->decode_mb= ff_h263_decode_mb;
  1867. s->low_delay= 1;
  1868. avctx->pix_fmt= PIX_FMT_YUV420P;
  1869. decode_init_vlc(h);
  1870. return 0;
  1871. }
  1872. static void frame_start(H264Context *h){
  1873. MpegEncContext * const s = &h->s;
  1874. int i;
  1875. MPV_frame_start(s, s->avctx);
  1876. ff_er_frame_start(s);
  1877. h->mmco_index=0;
  1878. assert(s->linesize && s->uvlinesize);
  1879. for(i=0; i<16; i++){
  1880. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  1881. h->chroma_subblock_offset[i]= 2*((scan8[i] - scan8[0])&7) + 2*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  1882. }
  1883. for(i=0; i<4; i++){
  1884. h->block_offset[16+i]=
  1885. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  1886. }
  1887. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  1888. }
  1889. static void hl_decode_mb(H264Context *h){
  1890. MpegEncContext * const s = &h->s;
  1891. const int mb_x= s->mb_x;
  1892. const int mb_y= s->mb_y;
  1893. const int mb_xy= mb_x + mb_y*s->mb_stride;
  1894. const int mb_type= s->current_picture.mb_type[mb_xy];
  1895. uint8_t *dest_y, *dest_cb, *dest_cr;
  1896. int linesize, uvlinesize /*dct_offset*/;
  1897. int i;
  1898. if(!s->decode)
  1899. return;
  1900. if(s->mb_skiped){
  1901. }
  1902. dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  1903. dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  1904. dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  1905. if (h->mb_field_decoding_flag) {
  1906. linesize = s->linesize * 2;
  1907. uvlinesize = s->uvlinesize * 2;
  1908. if(mb_y&1){ //FIXME move out of this func?
  1909. dest_y -= s->linesize*15;
  1910. dest_cb-= s->linesize*7;
  1911. dest_cr-= s->linesize*7;
  1912. }
  1913. } else {
  1914. linesize = s->linesize;
  1915. uvlinesize = s->uvlinesize;
  1916. // dct_offset = s->linesize * 16;
  1917. }
  1918. if(IS_INTRA(mb_type)){
  1919. if(!(s->flags&CODEC_FLAG_GRAY)){
  1920. h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  1921. h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  1922. }
  1923. if(IS_INTRA4x4(mb_type)){
  1924. if(!s->encoding){
  1925. for(i=0; i<16; i++){
  1926. uint8_t * const ptr= dest_y + h->block_offset[i];
  1927. uint8_t *topright= ptr + 4 - linesize;
  1928. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  1929. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  1930. int tr;
  1931. if(!topright_avail){
  1932. tr= ptr[3 - linesize]*0x01010101;
  1933. topright= (uint8_t*) &tr;
  1934. }
  1935. h->pred4x4[ dir ](ptr, topright, linesize);
  1936. if(h->non_zero_count_cache[ scan8[i] ]){
  1937. if(s->codec_id == CODEC_ID_H264)
  1938. h264_add_idct_c(ptr, h->mb + i*16, linesize);
  1939. else
  1940. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  1941. }
  1942. }
  1943. }
  1944. }else{
  1945. h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  1946. if(s->codec_id == CODEC_ID_H264)
  1947. h264_luma_dc_dequant_idct_c(h->mb, s->qscale);
  1948. else
  1949. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  1950. }
  1951. }else if(s->codec_id == CODEC_ID_H264){
  1952. hl_motion(h, dest_y, dest_cb, dest_cr,
  1953. s->dsp.put_h264_qpel_pixels_tab, s->dsp.put_h264_chroma_pixels_tab,
  1954. s->dsp.avg_h264_qpel_pixels_tab, s->dsp.avg_h264_chroma_pixels_tab);
  1955. }
  1956. if(!IS_INTRA4x4(mb_type)){
  1957. if(s->codec_id == CODEC_ID_H264){
  1958. for(i=0; i<16; i++){
  1959. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  1960. uint8_t * const ptr= dest_y + h->block_offset[i];
  1961. h264_add_idct_c(ptr, h->mb + i*16, linesize);
  1962. }
  1963. }
  1964. }else{
  1965. for(i=0; i<16; i++){
  1966. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  1967. uint8_t * const ptr= dest_y + h->block_offset[i];
  1968. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  1969. }
  1970. }
  1971. }
  1972. }
  1973. if(!(s->flags&CODEC_FLAG_GRAY)){
  1974. chroma_dc_dequant_idct_c(h->mb + 16*16, h->chroma_qp);
  1975. chroma_dc_dequant_idct_c(h->mb + 16*16+4*16, h->chroma_qp);
  1976. if(s->codec_id == CODEC_ID_H264){
  1977. for(i=16; i<16+4; i++){
  1978. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  1979. uint8_t * const ptr= dest_cb + h->block_offset[i];
  1980. h264_add_idct_c(ptr, h->mb + i*16, uvlinesize);
  1981. }
  1982. }
  1983. for(i=20; i<20+4; i++){
  1984. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  1985. uint8_t * const ptr= dest_cr + h->block_offset[i];
  1986. h264_add_idct_c(ptr, h->mb + i*16, uvlinesize);
  1987. }
  1988. }
  1989. }else{
  1990. for(i=16; i<16+4; i++){
  1991. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  1992. uint8_t * const ptr= dest_cb + h->block_offset[i];
  1993. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  1994. }
  1995. }
  1996. for(i=20; i<20+4; i++){
  1997. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  1998. uint8_t * const ptr= dest_cr + h->block_offset[i];
  1999. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  2000. }
  2001. }
  2002. }
  2003. }
  2004. }
  2005. static void decode_mb_cabac(H264Context *h){
  2006. // MpegEncContext * const s = &h->s;
  2007. }
  2008. /**
  2009. * fills the default_ref_list.
  2010. */
  2011. static int fill_default_ref_list(H264Context *h){
  2012. MpegEncContext * const s = &h->s;
  2013. int i;
  2014. Picture sorted_short_ref[16];
  2015. if(h->slice_type==B_TYPE){
  2016. int out_i;
  2017. int limit= -1;
  2018. for(out_i=0; out_i<h->short_ref_count; out_i++){
  2019. int best_i=-1;
  2020. int best_poc=-1;
  2021. for(i=0; i<h->short_ref_count; i++){
  2022. const int poc= h->short_ref[i]->poc;
  2023. if(poc > limit && poc < best_poc){
  2024. best_poc= poc;
  2025. best_i= i;
  2026. }
  2027. }
  2028. assert(best_i != -1);
  2029. limit= best_poc;
  2030. sorted_short_ref[out_i]= *h->short_ref[best_i];
  2031. }
  2032. }
  2033. if(s->picture_structure == PICT_FRAME){
  2034. if(h->slice_type==B_TYPE){
  2035. const int current_poc= s->current_picture_ptr->poc;
  2036. int list;
  2037. for(list=0; list<2; list++){
  2038. int index=0;
  2039. for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++){
  2040. const int i2= list ? h->short_ref_count - i - 1 : i;
  2041. const int poc= sorted_short_ref[i2].poc;
  2042. if(sorted_short_ref[i2].reference != 3) continue; //FIXME refernce field shit
  2043. if((list==1 && poc > current_poc) || (list==0 && poc < current_poc)){
  2044. h->default_ref_list[list][index ]= sorted_short_ref[i2];
  2045. h->default_ref_list[list][index++].pic_id= sorted_short_ref[i2].frame_num;
  2046. }
  2047. }
  2048. for(i=0; i<h->long_ref_count && index < h->ref_count[ list ]; i++){
  2049. if(h->long_ref[i]->reference != 3) continue;
  2050. h->default_ref_list[ list ][index ]= *h->long_ref[i];
  2051. h->default_ref_list[ list ][index++].pic_id= i;;
  2052. }
  2053. if(h->long_ref_count > 1 && h->short_ref_count==0){
  2054. Picture temp= h->default_ref_list[1][0];
  2055. h->default_ref_list[1][0] = h->default_ref_list[1][1];
  2056. h->default_ref_list[1][0] = temp;
  2057. }
  2058. if(index < h->ref_count[ list ])
  2059. memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
  2060. }
  2061. }else{
  2062. int index=0;
  2063. for(i=0; i<h->short_ref_count && index < h->ref_count[0]; i++){
  2064. if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
  2065. h->default_ref_list[0][index ]= *h->short_ref[i];
  2066. h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
  2067. }
  2068. for(i=0; i<h->long_ref_count && index < h->ref_count[0]; i++){
  2069. if(h->long_ref[i]->reference != 3) continue;
  2070. h->default_ref_list[0][index ]= *h->long_ref[i];
  2071. h->default_ref_list[0][index++].pic_id= i;;
  2072. }
  2073. if(index < h->ref_count[0])
  2074. memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
  2075. }
  2076. }else{ //FIELD
  2077. if(h->slice_type==B_TYPE){
  2078. }else{
  2079. //FIXME second field balh
  2080. }
  2081. }
  2082. return 0;
  2083. }
  2084. static int decode_ref_pic_list_reordering(H264Context *h){
  2085. MpegEncContext * const s = &h->s;
  2086. int list;
  2087. if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move beofre func
  2088. for(list=0; list<2; list++){
  2089. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  2090. if(get_bits1(&s->gb)){
  2091. int pred= h->curr_pic_num;
  2092. int index;
  2093. for(index=0; ; index++){
  2094. int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
  2095. int pic_id;
  2096. int i;
  2097. if(index >= h->ref_count[list]){
  2098. av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
  2099. return -1;
  2100. }
  2101. if(reordering_of_pic_nums_idc<3){
  2102. if(reordering_of_pic_nums_idc<2){
  2103. const int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  2104. if(abs_diff_pic_num >= h->max_pic_num){
  2105. av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  2106. return -1;
  2107. }
  2108. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  2109. else pred+= abs_diff_pic_num;
  2110. pred &= h->max_pic_num - 1;
  2111. for(i= h->ref_count[list]-1; i>=index; i--){
  2112. if(h->ref_list[list][i].pic_id == pred && h->ref_list[list][i].long_ref==0)
  2113. break;
  2114. }
  2115. }else{
  2116. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  2117. for(i= h->ref_count[list]-1; i>=index; i--){
  2118. if(h->ref_list[list][i].pic_id == pic_id && h->ref_list[list][i].long_ref==1)
  2119. break;
  2120. }
  2121. }
  2122. if(i < index){
  2123. av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  2124. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  2125. }else if(i > index){
  2126. Picture tmp= h->ref_list[list][i];
  2127. for(; i>index; i--){
  2128. h->ref_list[list][i]= h->ref_list[list][i-1];
  2129. }
  2130. h->ref_list[list][index]= tmp;
  2131. }
  2132. }else if(reordering_of_pic_nums_idc==3)
  2133. break;
  2134. else{
  2135. av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  2136. return -1;
  2137. }
  2138. }
  2139. }
  2140. if(h->slice_type!=B_TYPE) break;
  2141. }
  2142. return 0;
  2143. }
  2144. static int pred_weight_table(H264Context *h){
  2145. MpegEncContext * const s = &h->s;
  2146. int list, i;
  2147. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  2148. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  2149. for(list=0; list<2; list++){
  2150. for(i=0; i<h->ref_count[list]; i++){
  2151. int luma_weight_flag, chroma_weight_flag;
  2152. luma_weight_flag= get_bits1(&s->gb);
  2153. if(luma_weight_flag){
  2154. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  2155. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  2156. }
  2157. chroma_weight_flag= get_bits1(&s->gb);
  2158. if(chroma_weight_flag){
  2159. int j;
  2160. for(j=0; j<2; j++){
  2161. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  2162. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  2163. }
  2164. }
  2165. }
  2166. if(h->slice_type != B_TYPE) break;
  2167. }
  2168. return 0;
  2169. }
  2170. /**
  2171. * instantaneos decoder refresh.
  2172. */
  2173. static void idr(H264Context *h){
  2174. int i;
  2175. for(i=0; i<h->long_ref_count; i++){
  2176. h->long_ref[i]->reference=0;
  2177. h->long_ref[i]= NULL;
  2178. }
  2179. h->long_ref_count=0;
  2180. for(i=0; i<h->short_ref_count; i++){
  2181. h->short_ref[i]->reference=0;
  2182. h->short_ref[i]= NULL;
  2183. }
  2184. h->short_ref_count=0;
  2185. }
  2186. /**
  2187. *
  2188. * @return the removed picture or NULL if an error occures
  2189. */
  2190. static Picture * remove_short(H264Context *h, int frame_num){
  2191. MpegEncContext * const s = &h->s;
  2192. int i;
  2193. if(s->avctx->debug&FF_DEBUG_MMCO)
  2194. av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  2195. for(i=0; i<h->short_ref_count; i++){
  2196. Picture *pic= h->short_ref[i];
  2197. if(s->avctx->debug&FF_DEBUG_MMCO)
  2198. av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  2199. if(pic->frame_num == frame_num){
  2200. h->short_ref[i]= NULL;
  2201. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
  2202. h->short_ref_count--;
  2203. return pic;
  2204. }
  2205. }
  2206. return NULL;
  2207. }
  2208. /**
  2209. *
  2210. * @return the removed picture or NULL if an error occures
  2211. */
  2212. static Picture * remove_long(H264Context *h, int i){
  2213. Picture *pic;
  2214. if(i >= h->long_ref_count) return NULL;
  2215. pic= h->long_ref[i];
  2216. if(pic==NULL) return NULL;
  2217. h->long_ref[i]= NULL;
  2218. memmove(&h->long_ref[i], &h->long_ref[i+1], (h->long_ref_count - i - 1)*sizeof(Picture*));
  2219. h->long_ref_count--;
  2220. return pic;
  2221. }
  2222. /**
  2223. * Executes the reference picture marking (memory management control operations).
  2224. */
  2225. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  2226. MpegEncContext * const s = &h->s;
  2227. int i;
  2228. int current_is_long=0;
  2229. Picture *pic;
  2230. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  2231. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  2232. for(i=0; i<mmco_count; i++){
  2233. if(s->avctx->debug&FF_DEBUG_MMCO)
  2234. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco:%d %d %d\n", h->mmco[i].opcode, h->mmco[i].short_frame_num, h->mmco[i].long_index);
  2235. switch(mmco[i].opcode){
  2236. case MMCO_SHORT2UNUSED:
  2237. pic= remove_short(h, mmco[i].short_frame_num);
  2238. if(pic==NULL) return -1;
  2239. pic->reference= 0;
  2240. break;
  2241. case MMCO_SHORT2LONG:
  2242. pic= remove_long(h, mmco[i].long_index);
  2243. if(pic) pic->reference=0;
  2244. h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
  2245. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  2246. break;
  2247. case MMCO_LONG2UNUSED:
  2248. pic= remove_long(h, mmco[i].long_index);
  2249. if(pic==NULL) return -1;
  2250. pic->reference= 0;
  2251. break;
  2252. case MMCO_LONG:
  2253. pic= remove_long(h, mmco[i].long_index);
  2254. if(pic) pic->reference=0;
  2255. h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
  2256. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  2257. h->long_ref_count++;
  2258. current_is_long=1;
  2259. break;
  2260. case MMCO_SET_MAX_LONG:
  2261. assert(mmco[i].long_index <= 16);
  2262. while(mmco[i].long_index < h->long_ref_count){
  2263. pic= remove_long(h, mmco[i].long_index);
  2264. pic->reference=0;
  2265. }
  2266. while(mmco[i].long_index > h->long_ref_count){
  2267. h->long_ref[ h->long_ref_count++ ]= NULL;
  2268. }
  2269. break;
  2270. case MMCO_RESET:
  2271. while(h->short_ref_count){
  2272. pic= remove_short(h, h->short_ref[0]->frame_num);
  2273. pic->reference=0;
  2274. }
  2275. while(h->long_ref_count){
  2276. pic= remove_long(h, h->long_ref_count-1);
  2277. pic->reference=0;
  2278. }
  2279. break;
  2280. default: assert(0);
  2281. }
  2282. }
  2283. if(!current_is_long){
  2284. pic= remove_short(h, s->current_picture_ptr->frame_num);
  2285. if(pic){
  2286. pic->reference=0;
  2287. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  2288. }
  2289. if(h->short_ref_count)
  2290. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  2291. h->short_ref[0]= s->current_picture_ptr;
  2292. h->short_ref[0]->long_ref=0;
  2293. h->short_ref_count++;
  2294. }
  2295. return 0;
  2296. }
  2297. static int decode_ref_pic_marking(H264Context *h){
  2298. MpegEncContext * const s = &h->s;
  2299. int i;
  2300. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  2301. s->broken_link= get_bits1(&s->gb) -1;
  2302. h->mmco[0].long_index= get_bits1(&s->gb) - 1; // current_long_term_idx
  2303. if(h->mmco[0].long_index == -1)
  2304. h->mmco_index= 0;
  2305. else{
  2306. h->mmco[0].opcode= MMCO_LONG;
  2307. h->mmco_index= 1;
  2308. }
  2309. }else{
  2310. if(get_bits1(&s->gb)){ // adaptive_ref_pic_marking_mode_flag
  2311. for(i= h->mmco_index; i<MAX_MMCO_COUNT; i++) {
  2312. MMCOOpcode opcode= get_ue_golomb(&s->gb);;
  2313. h->mmco[i].opcode= opcode;
  2314. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  2315. 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
  2316. /* if(h->mmco[i].short_frame_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_frame_num ] == NULL){
  2317. fprintf(stderr, "illegal short ref in memory management control operation %d\n", mmco);
  2318. return -1;
  2319. }*/
  2320. }
  2321. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  2322. h->mmco[i].long_index= get_ue_golomb(&s->gb);
  2323. 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){
  2324. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  2325. return -1;
  2326. }
  2327. }
  2328. if(opcode > MMCO_LONG){
  2329. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  2330. return -1;
  2331. }
  2332. }
  2333. h->mmco_index= i;
  2334. }else{
  2335. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  2336. if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){ //FIXME fields
  2337. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  2338. h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  2339. h->mmco_index= 1;
  2340. }else
  2341. h->mmco_index= 0;
  2342. }
  2343. }
  2344. return 0;
  2345. }
  2346. static int init_poc(H264Context *h){
  2347. MpegEncContext * const s = &h->s;
  2348. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  2349. int field_poc[2];
  2350. if(h->nal_unit_type == NAL_IDR_SLICE){
  2351. h->frame_num_offset= 0;
  2352. }else{
  2353. if(h->frame_num < h->prev_frame_num)
  2354. h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
  2355. else
  2356. h->frame_num_offset= h->prev_frame_num_offset;
  2357. }
  2358. if(h->sps.poc_type==0){
  2359. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  2360. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  2361. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  2362. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  2363. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  2364. else
  2365. h->poc_msb = h->prev_poc_msb;
  2366. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  2367. field_poc[0] =
  2368. field_poc[1] = h->poc_msb + h->poc_lsb;
  2369. if(s->picture_structure == PICT_FRAME)
  2370. field_poc[1] += h->delta_poc_bottom;
  2371. }else if(h->sps.poc_type==1){
  2372. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  2373. int i;
  2374. if(h->sps.poc_cycle_length != 0)
  2375. abs_frame_num = h->frame_num_offset + h->frame_num;
  2376. else
  2377. abs_frame_num = 0;
  2378. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  2379. abs_frame_num--;
  2380. expected_delta_per_poc_cycle = 0;
  2381. for(i=0; i < h->sps.poc_cycle_length; i++)
  2382. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  2383. if(abs_frame_num > 0){
  2384. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  2385. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  2386. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  2387. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  2388. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  2389. } else
  2390. expectedpoc = 0;
  2391. if(h->nal_ref_idc == 0)
  2392. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  2393. field_poc[0] = expectedpoc + h->delta_poc[0];
  2394. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  2395. if(s->picture_structure == PICT_FRAME)
  2396. field_poc[1] += h->delta_poc[1];
  2397. }else{
  2398. int poc;
  2399. if(h->nal_unit_type == NAL_IDR_SLICE){
  2400. poc= 0;
  2401. }else{
  2402. if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
  2403. else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
  2404. }
  2405. field_poc[0]= poc;
  2406. field_poc[1]= poc;
  2407. }
  2408. if(s->picture_structure != PICT_BOTTOM_FIELD)
  2409. s->current_picture_ptr->field_poc[0]= field_poc[0];
  2410. if(s->picture_structure != PICT_TOP_FIELD)
  2411. s->current_picture_ptr->field_poc[1]= field_poc[1];
  2412. if(s->picture_structure == PICT_FRAME) // FIXME field pix?
  2413. s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]);
  2414. return 0;
  2415. }
  2416. /**
  2417. * decodes a slice header.
  2418. * this will allso call MPV_common_init() and frame_start() as needed
  2419. */
  2420. static int decode_slice_header(H264Context *h){
  2421. MpegEncContext * const s = &h->s;
  2422. int first_mb_in_slice, pps_id;
  2423. int num_ref_idx_active_override_flag;
  2424. static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
  2425. s->current_picture.reference= h->nal_ref_idc != 0;
  2426. first_mb_in_slice= get_ue_golomb(&s->gb);
  2427. h->slice_type= get_ue_golomb(&s->gb);
  2428. if(h->slice_type > 9){
  2429. av_log(h->s.avctx, AV_LOG_ERROR, "slice type too large (%d) at %d %d\n", h->slice_type, s->mb_x, s->mb_y);
  2430. }
  2431. if(h->slice_type > 4){
  2432. h->slice_type -= 5;
  2433. h->slice_type_fixed=1;
  2434. }else
  2435. h->slice_type_fixed=0;
  2436. h->slice_type= slice_type_map[ h->slice_type ];
  2437. s->pict_type= h->slice_type; // to make a few old func happy, its wrong though
  2438. pps_id= get_ue_golomb(&s->gb);
  2439. if(pps_id>255){
  2440. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  2441. return -1;
  2442. }
  2443. h->pps= h->pps_buffer[pps_id];
  2444. if(h->pps.slice_group_count == 0){
  2445. av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referenced\n");
  2446. return -1;
  2447. }
  2448. h->sps= h->sps_buffer[ h->pps.sps_id ];
  2449. if(h->sps.log2_max_frame_num == 0){
  2450. av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referenced\n");
  2451. return -1;
  2452. }
  2453. s->mb_width= h->sps.mb_width;
  2454. s->mb_height= h->sps.mb_height;
  2455. h->b_stride= s->mb_width*4;
  2456. h->b8_stride= s->mb_width*2;
  2457. s->mb_x = first_mb_in_slice % s->mb_width;
  2458. s->mb_y = first_mb_in_slice / s->mb_width; //FIXME AFFW
  2459. s->width = 16*s->mb_width - 2*(h->sps.crop_left + h->sps.crop_right );
  2460. if(h->sps.frame_mbs_only_flag)
  2461. s->height= 16*s->mb_height - 2*(h->sps.crop_top + h->sps.crop_bottom);
  2462. else
  2463. s->height= 16*s->mb_height - 4*(h->sps.crop_top + h->sps.crop_bottom); //FIXME recheck
  2464. if (s->context_initialized
  2465. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  2466. free_tables(h);
  2467. MPV_common_end(s);
  2468. }
  2469. if (!s->context_initialized) {
  2470. if (MPV_common_init(s) < 0)
  2471. return -1;
  2472. alloc_tables(h);
  2473. s->avctx->width = s->width;
  2474. s->avctx->height = s->height;
  2475. s->avctx->sample_aspect_ratio= h->sps.sar;
  2476. }
  2477. if(first_mb_in_slice == 0){
  2478. frame_start(h);
  2479. }
  2480. s->current_picture_ptr->frame_num= //FIXME frame_num cleanup
  2481. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  2482. if(h->sps.frame_mbs_only_flag){
  2483. s->picture_structure= PICT_FRAME;
  2484. }else{
  2485. if(get_bits1(&s->gb)) //field_pic_flag
  2486. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  2487. else
  2488. s->picture_structure= PICT_FRAME;
  2489. }
  2490. if(s->picture_structure==PICT_FRAME){
  2491. h->curr_pic_num= h->frame_num;
  2492. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  2493. }else{
  2494. h->curr_pic_num= 2*h->frame_num;
  2495. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  2496. }
  2497. if(h->nal_unit_type == NAL_IDR_SLICE){
  2498. get_ue_golomb(&s->gb); /* idr_pic_id */
  2499. }
  2500. if(h->sps.poc_type==0){
  2501. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  2502. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  2503. h->delta_poc_bottom= get_se_golomb(&s->gb);
  2504. }
  2505. }
  2506. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  2507. h->delta_poc[0]= get_se_golomb(&s->gb);
  2508. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  2509. h->delta_poc[1]= get_se_golomb(&s->gb);
  2510. }
  2511. init_poc(h);
  2512. if(h->pps.redundant_pic_cnt_present){
  2513. h->redundant_pic_count= get_ue_golomb(&s->gb);
  2514. }
  2515. //set defaults, might be overriden a few line later
  2516. h->ref_count[0]= h->pps.ref_count[0];
  2517. h->ref_count[1]= h->pps.ref_count[1];
  2518. if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){
  2519. if(h->slice_type == B_TYPE){
  2520. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  2521. }
  2522. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  2523. if(num_ref_idx_active_override_flag){
  2524. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  2525. if(h->slice_type==B_TYPE)
  2526. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  2527. if(h->ref_count[0] > 32 || h->ref_count[1] > 32){
  2528. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  2529. return -1;
  2530. }
  2531. }
  2532. }
  2533. if(first_mb_in_slice == 0){
  2534. fill_default_ref_list(h);
  2535. }
  2536. decode_ref_pic_list_reordering(h);
  2537. if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
  2538. || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
  2539. pred_weight_table(h);
  2540. if(s->current_picture.reference)
  2541. decode_ref_pic_marking(h);
  2542. //FIXME CABAC stuff
  2543. s->qscale = h->pps.init_qp + get_se_golomb(&s->gb); //slice_qp_delta
  2544. //FIXME qscale / qp ... stuff
  2545. if(h->slice_type == SP_TYPE){
  2546. get_bits1(&s->gb); /* sp_for_switch_flag */
  2547. }
  2548. if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
  2549. get_se_golomb(&s->gb); /* slice_qs_delta */
  2550. }
  2551. if( h->pps.deblocking_filter_parameters_present ) {
  2552. h->disable_deblocking_filter_idc= get_ue_golomb(&s->gb);
  2553. if( h->disable_deblocking_filter_idc != 1 ) {
  2554. h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
  2555. h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
  2556. } else {
  2557. h->slice_alpha_c0_offset = 0;
  2558. h->slice_beta_offset = 0;
  2559. }
  2560. } else {
  2561. h->disable_deblocking_filter_idc = 0;
  2562. h->slice_alpha_c0_offset = 0;
  2563. h->slice_beta_offset = 0;
  2564. }
  2565. #if 0 //FMO
  2566. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  2567. slice_group_change_cycle= get_bits(&s->gb, ?);
  2568. #endif
  2569. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  2570. av_log(h->s.avctx, AV_LOG_DEBUG, "mb:%d %c pps:%d frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d\n",
  2571. first_mb_in_slice,
  2572. av_get_pict_type_char(h->slice_type),
  2573. pps_id, h->frame_num,
  2574. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  2575. h->ref_count[0], h->ref_count[1],
  2576. s->qscale,
  2577. h->disable_deblocking_filter_idc
  2578. );
  2579. }
  2580. return 0;
  2581. }
  2582. /**
  2583. *
  2584. */
  2585. static inline int get_level_prefix(GetBitContext *gb){
  2586. unsigned int buf;
  2587. int log;
  2588. OPEN_READER(re, gb);
  2589. UPDATE_CACHE(re, gb);
  2590. buf=GET_CACHE(re, gb);
  2591. log= 32 - av_log2(buf);
  2592. #ifdef TRACE
  2593. print_bin(buf>>(32-log), log);
  2594. printf("%5d %2d %3d lpr @%5d in %s get_level_prefix\n", buf>>(32-log), log, log-1, get_bits_count(gb), __FILE__);
  2595. #endif
  2596. LAST_SKIP_BITS(re, gb, log);
  2597. CLOSE_READER(re, gb);
  2598. return log-1;
  2599. }
  2600. /**
  2601. * decodes a residual block.
  2602. * @param n block index
  2603. * @param scantable scantable
  2604. * @param max_coeff number of coefficients in the block
  2605. * @return <0 if an error occured
  2606. */
  2607. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, int qp, int max_coeff){
  2608. MpegEncContext * const s = &h->s;
  2609. const uint16_t *qmul= dequant_coeff[qp];
  2610. 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};
  2611. int level[16], run[16];
  2612. int suffix_length, zeros_left, coeff_num, coeff_token, total_coeff, i, trailing_ones;
  2613. //FIXME put trailing_onex into the context
  2614. if(n == CHROMA_DC_BLOCK_INDEX){
  2615. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  2616. total_coeff= coeff_token>>2;
  2617. }else{
  2618. if(n == LUMA_DC_BLOCK_INDEX){
  2619. total_coeff= pred_non_zero_count(h, 0);
  2620. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  2621. total_coeff= coeff_token>>2;
  2622. }else{
  2623. total_coeff= pred_non_zero_count(h, n);
  2624. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  2625. total_coeff= coeff_token>>2;
  2626. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  2627. }
  2628. }
  2629. //FIXME set last_non_zero?
  2630. if(total_coeff==0)
  2631. return 0;
  2632. trailing_ones= coeff_token&3;
  2633. tprintf("trailing:%d, total:%d\n", trailing_ones, total_coeff);
  2634. assert(total_coeff<=16);
  2635. for(i=0; i<trailing_ones; i++){
  2636. level[i]= 1 - 2*get_bits1(gb);
  2637. }
  2638. suffix_length= total_coeff > 10 && trailing_ones < 3;
  2639. for(; i<total_coeff; i++){
  2640. const int prefix= get_level_prefix(gb);
  2641. int level_code, mask;
  2642. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  2643. if(suffix_length)
  2644. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  2645. else
  2646. level_code= (prefix<<suffix_length); //part
  2647. }else if(prefix==14){
  2648. if(suffix_length)
  2649. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  2650. else
  2651. level_code= prefix + get_bits(gb, 4); //part
  2652. }else if(prefix==15){
  2653. level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part
  2654. if(suffix_length==0) level_code+=15; //FIXME doesnt make (much)sense
  2655. }else{
  2656. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  2657. return -1;
  2658. }
  2659. if(i==trailing_ones && i<3) level_code+= 2; //FIXME split first iteration
  2660. mask= -(level_code&1);
  2661. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  2662. if(suffix_length==0) suffix_length=1; //FIXME split first iteration
  2663. #if 1
  2664. if(ABS(level[i]) > (3<<(suffix_length-1)) && suffix_length<6) suffix_length++;
  2665. #else
  2666. if((2+level_code)>>1) > (3<<(suffix_length-1)) && suffix_length<6) suffix_length++;
  2667. /* ? == prefix > 2 or sth */
  2668. #endif
  2669. tprintf("level: %d suffix_length:%d\n", level[i], suffix_length);
  2670. }
  2671. if(total_coeff == max_coeff)
  2672. zeros_left=0;
  2673. else{
  2674. if(n == CHROMA_DC_BLOCK_INDEX)
  2675. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  2676. else
  2677. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  2678. }
  2679. for(i=0; i<total_coeff-1; i++){
  2680. if(zeros_left <=0)
  2681. break;
  2682. else if(zeros_left < 7){
  2683. run[i]= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  2684. }else{
  2685. run[i]= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  2686. }
  2687. zeros_left -= run[i];
  2688. }
  2689. if(zeros_left<0){
  2690. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  2691. return -1;
  2692. }
  2693. for(; i<total_coeff-1; i++){
  2694. run[i]= 0;
  2695. }
  2696. run[i]= zeros_left;
  2697. coeff_num=-1;
  2698. if(n > 24){
  2699. for(i=total_coeff-1; i>=0; i--){ //FIXME merge into rundecode?
  2700. int j;
  2701. coeff_num += run[i] + 1; //FIXME add 1 earlier ?
  2702. j= scantable[ coeff_num ];
  2703. block[j]= level[i];
  2704. }
  2705. }else{
  2706. for(i=total_coeff-1; i>=0; i--){ //FIXME merge into rundecode?
  2707. int j;
  2708. coeff_num += run[i] + 1; //FIXME add 1 earlier ?
  2709. j= scantable[ coeff_num ];
  2710. block[j]= level[i] * qmul[j];
  2711. // printf("%d %d ", block[j], qmul[j]);
  2712. }
  2713. }
  2714. return 0;
  2715. }
  2716. /**
  2717. * decodes a macroblock
  2718. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  2719. */
  2720. static int decode_mb(H264Context *h){
  2721. MpegEncContext * const s = &h->s;
  2722. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2723. int mb_type, partition_count, cbp;
  2724. s->dsp.clear_blocks(h->mb); //FIXME avoid if allready clear (move after skip handlong?
  2725. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  2726. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  2727. down the code */
  2728. if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){
  2729. if(s->mb_skip_run==-1)
  2730. s->mb_skip_run= get_ue_golomb(&s->gb);
  2731. if (s->mb_skip_run--) {
  2732. int mx, my;
  2733. /* skip mb */
  2734. //FIXME b frame
  2735. mb_type= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0;
  2736. memset(h->non_zero_count[mb_xy], 0, 16+4+4);
  2737. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  2738. if(h->sps.mb_aff && s->mb_skip_run==0 && (s->mb_y&1)==0){
  2739. h->mb_field_decoding_flag= get_bits1(&s->gb);
  2740. }
  2741. if(h->mb_field_decoding_flag)
  2742. mb_type|= MB_TYPE_INTERLACED;
  2743. fill_caches(h, mb_type); //FIXME check what is needed and what not ...
  2744. pred_pskip_motion(h, &mx, &my);
  2745. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  2746. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  2747. write_back_motion(h, mb_type);
  2748. s->current_picture.mb_type[mb_xy]= mb_type; //FIXME SKIP type
  2749. s->current_picture.qscale_table[mb_xy]= s->qscale;
  2750. h->slice_table[ mb_xy ]= h->slice_num;
  2751. h->prev_mb_skiped= 1;
  2752. return 0;
  2753. }
  2754. }
  2755. if(h->sps.mb_aff /* && !field pic FIXME needed? */){
  2756. if((s->mb_y&1)==0)
  2757. h->mb_field_decoding_flag = get_bits1(&s->gb);
  2758. }else
  2759. h->mb_field_decoding_flag=0; //FIXME som ed note ?!
  2760. h->prev_mb_skiped= 0;
  2761. mb_type= get_ue_golomb(&s->gb);
  2762. if(h->slice_type == B_TYPE){
  2763. if(mb_type < 23){
  2764. partition_count= b_mb_type_info[mb_type].partition_count;
  2765. mb_type= b_mb_type_info[mb_type].type;
  2766. }else{
  2767. mb_type -= 23;
  2768. goto decode_intra_mb;
  2769. }
  2770. }else if(h->slice_type == P_TYPE /*|| h->slice_type == SP_TYPE */){
  2771. if(mb_type < 5){
  2772. partition_count= p_mb_type_info[mb_type].partition_count;
  2773. mb_type= p_mb_type_info[mb_type].type;
  2774. }else{
  2775. mb_type -= 5;
  2776. goto decode_intra_mb;
  2777. }
  2778. }else{
  2779. assert(h->slice_type == I_TYPE);
  2780. decode_intra_mb:
  2781. if(mb_type > 25){
  2782. av_log(h->s.avctx, AV_LOG_ERROR, "mb_type %d in %c slice to large at %d %d\n", mb_type, av_get_pict_type_char(h->slice_type), s->mb_x, s->mb_y);
  2783. return -1;
  2784. }
  2785. partition_count=0;
  2786. cbp= i_mb_type_info[mb_type].cbp;
  2787. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  2788. mb_type= i_mb_type_info[mb_type].type;
  2789. }
  2790. if(h->mb_field_decoding_flag)
  2791. mb_type |= MB_TYPE_INTERLACED;
  2792. s->current_picture.mb_type[mb_xy]= mb_type;
  2793. h->slice_table[ mb_xy ]= h->slice_num;
  2794. if(IS_INTRA_PCM(mb_type)){
  2795. const uint8_t *ptr;
  2796. int x, y;
  2797. // we assume these blocks are very rare so we dont optimize it
  2798. align_get_bits(&s->gb);
  2799. ptr= s->gb.buffer + get_bits_count(&s->gb);
  2800. for(y=0; y<16; y++){
  2801. const int index= 4*(y&3) + 64*(y>>2);
  2802. for(x=0; x<16; x++){
  2803. h->mb[index + (x&3) + 16*(x>>2)]= *(ptr++);
  2804. }
  2805. }
  2806. for(y=0; y<8; y++){
  2807. const int index= 256 + 4*(y&3) + 32*(y>>2);
  2808. for(x=0; x<8; x++){
  2809. h->mb[index + (x&3) + 16*(x>>2)]= *(ptr++);
  2810. }
  2811. }
  2812. for(y=0; y<8; y++){
  2813. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  2814. for(x=0; x<8; x++){
  2815. h->mb[index + (x&3) + 16*(x>>2)]= *(ptr++);
  2816. }
  2817. }
  2818. skip_bits(&s->gb, 384); //FIXME check /fix the bitstream readers
  2819. memset(h->non_zero_count[mb_xy], 16, 16+4+4);
  2820. s->current_picture.qscale_table[mb_xy]= s->qscale;
  2821. return 0;
  2822. }
  2823. fill_caches(h, mb_type);
  2824. //mb_pred
  2825. if(IS_INTRA(mb_type)){
  2826. // init_top_left_availability(h);
  2827. if(IS_INTRA4x4(mb_type)){
  2828. int i;
  2829. // fill_intra4x4_pred_table(h);
  2830. for(i=0; i<16; i++){
  2831. const int mode_coded= !get_bits1(&s->gb);
  2832. const int predicted_mode= pred_intra_mode(h, i);
  2833. int mode;
  2834. if(mode_coded){
  2835. const int rem_mode= get_bits(&s->gb, 3);
  2836. if(rem_mode<predicted_mode)
  2837. mode= rem_mode;
  2838. else
  2839. mode= rem_mode + 1;
  2840. }else{
  2841. mode= predicted_mode;
  2842. }
  2843. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  2844. }
  2845. write_back_intra_pred_mode(h);
  2846. if( check_intra4x4_pred_mode(h) < 0)
  2847. return -1;
  2848. }else{
  2849. h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
  2850. if(h->intra16x16_pred_mode < 0)
  2851. return -1;
  2852. }
  2853. h->chroma_pred_mode= get_ue_golomb(&s->gb);
  2854. h->chroma_pred_mode= check_intra_pred_mode(h, h->chroma_pred_mode);
  2855. if(h->chroma_pred_mode < 0)
  2856. return -1;
  2857. }else if(partition_count==4){
  2858. int i, j, sub_partition_count[4], list, ref[2][4];
  2859. if(h->slice_type == B_TYPE){
  2860. for(i=0; i<4; i++){
  2861. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  2862. if(h->sub_mb_type[i] >=13){
  2863. av_log(h->s.avctx, AV_LOG_ERROR, "B sub_mb_type %d out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
  2864. return -1;
  2865. }
  2866. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  2867. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  2868. }
  2869. }else{
  2870. assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE); //FIXME SP correct ?
  2871. for(i=0; i<4; i++){
  2872. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  2873. if(h->sub_mb_type[i] >=4){
  2874. av_log(h->s.avctx, AV_LOG_ERROR, "P sub_mb_type %d out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
  2875. return -1;
  2876. }
  2877. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  2878. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  2879. }
  2880. }
  2881. for(list=0; list<2; list++){
  2882. const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  2883. if(ref_count == 0) continue;
  2884. for(i=0; i<4; i++){
  2885. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  2886. ref[list][i] = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
  2887. }else{
  2888. //FIXME
  2889. ref[list][i] = -1;
  2890. }
  2891. }
  2892. }
  2893. for(list=0; list<2; list++){
  2894. const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  2895. if(ref_count == 0) continue;
  2896. for(i=0; i<4; i++){
  2897. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  2898. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  2899. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  2900. const int sub_mb_type= h->sub_mb_type[i];
  2901. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  2902. for(j=0; j<sub_partition_count[i]; j++){
  2903. int mx, my;
  2904. const int index= 4*i + block_width*j;
  2905. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  2906. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  2907. mx += get_se_golomb(&s->gb);
  2908. my += get_se_golomb(&s->gb);
  2909. tprintf("final mv:%d %d\n", mx, my);
  2910. if(IS_SUB_8X8(sub_mb_type)){
  2911. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  2912. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  2913. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  2914. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  2915. }else if(IS_SUB_8X4(sub_mb_type)){
  2916. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  2917. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  2918. }else if(IS_SUB_4X8(sub_mb_type)){
  2919. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  2920. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  2921. }else{
  2922. assert(IS_SUB_4X4(sub_mb_type));
  2923. mv_cache[ 0 ][0]= mx;
  2924. mv_cache[ 0 ][1]= my;
  2925. }
  2926. }
  2927. }else{
  2928. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  2929. p[0] = p[1]=
  2930. p[8] = p[9]= 0;
  2931. }
  2932. }
  2933. }
  2934. }else if(!IS_DIRECT(mb_type)){
  2935. int list, mx, my, i;
  2936. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  2937. if(IS_16X16(mb_type)){
  2938. for(list=0; list<2; list++){
  2939. if(h->ref_count[0]>0){
  2940. if(IS_DIR(mb_type, 0, list)){
  2941. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  2942. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  2943. }
  2944. }
  2945. }
  2946. for(list=0; list<2; list++){
  2947. if(IS_DIR(mb_type, 0, list)){
  2948. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  2949. mx += get_se_golomb(&s->gb);
  2950. my += get_se_golomb(&s->gb);
  2951. tprintf("final mv:%d %d\n", mx, my);
  2952. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  2953. }
  2954. }
  2955. }
  2956. else if(IS_16X8(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)){
  2961. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  2962. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 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_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*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] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  2975. }
  2976. }
  2977. }
  2978. }else{
  2979. assert(IS_8X16(mb_type));
  2980. for(list=0; list<2; list++){
  2981. if(h->ref_count[list]>0){
  2982. for(i=0; i<2; i++){
  2983. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  2984. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  2985. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  2986. }
  2987. }
  2988. }
  2989. }
  2990. for(list=0; list<2; list++){
  2991. for(i=0; i<2; i++){
  2992. if(IS_DIR(mb_type, i, list)){
  2993. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  2994. mx += get_se_golomb(&s->gb);
  2995. my += get_se_golomb(&s->gb);
  2996. tprintf("final mv:%d %d\n", mx, my);
  2997. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  2998. }
  2999. }
  3000. }
  3001. }
  3002. }
  3003. if(IS_INTER(mb_type))
  3004. write_back_motion(h, mb_type);
  3005. if(!IS_INTRA16x16(mb_type)){
  3006. cbp= get_ue_golomb(&s->gb);
  3007. if(cbp > 47){
  3008. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%d) at %d %d\n", cbp, s->mb_x, s->mb_y);
  3009. return -1;
  3010. }
  3011. if(IS_INTRA4x4(mb_type))
  3012. cbp= golomb_to_intra4x4_cbp[cbp];
  3013. else
  3014. cbp= golomb_to_inter_cbp[cbp];
  3015. }
  3016. if(cbp || IS_INTRA16x16(mb_type)){
  3017. int i8x8, i4x4, chroma_idx;
  3018. int chroma_qp, dquant;
  3019. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  3020. const uint8_t *scan, *dc_scan;
  3021. // fill_non_zero_count_cache(h);
  3022. if(IS_INTERLACED(mb_type)){
  3023. scan= field_scan;
  3024. dc_scan= luma_dc_field_scan;
  3025. }else{
  3026. scan= zigzag_scan;
  3027. dc_scan= luma_dc_zigzag_scan;
  3028. }
  3029. dquant= get_se_golomb(&s->gb);
  3030. if( dquant > 25 || dquant < -26 ){
  3031. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  3032. return -1;
  3033. }
  3034. s->qscale += dquant;
  3035. if(((unsigned)s->qscale) > 51){
  3036. if(s->qscale<0) s->qscale+= 52;
  3037. else s->qscale-= 52;
  3038. }
  3039. h->chroma_qp= chroma_qp= get_chroma_qp(h, s->qscale);
  3040. if(IS_INTRA16x16(mb_type)){
  3041. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, s->qscale, 16) < 0){
  3042. return -1; //FIXME continue if partotioned and other retirn -1 too
  3043. }
  3044. assert((cbp&15) == 0 || (cbp&15) == 15);
  3045. if(cbp&15){
  3046. for(i8x8=0; i8x8<4; i8x8++){
  3047. for(i4x4=0; i4x4<4; i4x4++){
  3048. const int index= i4x4 + 4*i8x8;
  3049. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, s->qscale, 15) < 0 ){
  3050. return -1;
  3051. }
  3052. }
  3053. }
  3054. }else{
  3055. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  3056. }
  3057. }else{
  3058. for(i8x8=0; i8x8<4; i8x8++){
  3059. if(cbp & (1<<i8x8)){
  3060. for(i4x4=0; i4x4<4; i4x4++){
  3061. const int index= i4x4 + 4*i8x8;
  3062. if( decode_residual(h, gb, h->mb + 16*index, index, scan, s->qscale, 16) <0 ){
  3063. return -1;
  3064. }
  3065. }
  3066. }else{
  3067. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  3068. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  3069. }
  3070. }
  3071. }
  3072. if(cbp&0x30){
  3073. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  3074. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, chroma_qp, 4) < 0){
  3075. return -1;
  3076. }
  3077. }
  3078. if(cbp&0x20){
  3079. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  3080. for(i4x4=0; i4x4<4; i4x4++){
  3081. const int index= 16 + 4*chroma_idx + i4x4;
  3082. if( decode_residual(h, gb, h->mb + 16*index, index, scan + 1, chroma_qp, 15) < 0){
  3083. return -1;
  3084. }
  3085. }
  3086. }
  3087. }else{
  3088. uint8_t * const nnz= &h->non_zero_count_cache[0];
  3089. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  3090. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  3091. }
  3092. }else{
  3093. memset(&h->non_zero_count_cache[8], 0, 8*5);
  3094. }
  3095. s->current_picture.qscale_table[mb_xy]= s->qscale;
  3096. write_back_non_zero_count(h);
  3097. return 0;
  3098. }
  3099. static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  3100. int i, d;
  3101. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  3102. const int alpha = alpha_table[index_a];
  3103. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  3104. for( i = 0; i < 4; i++ ) {
  3105. if( bS[i] == 0 ) {
  3106. pix += 4 * stride;
  3107. continue;
  3108. }
  3109. /* 4px edge length */
  3110. for( d = 0; d < 4; d++ ) {
  3111. const uint8_t p0 = pix[-1];
  3112. const uint8_t p1 = pix[-2];
  3113. const uint8_t p2 = pix[-3];
  3114. const uint8_t q0 = pix[0];
  3115. const uint8_t q1 = pix[1];
  3116. const uint8_t q2 = pix[2];
  3117. if( abs( p0 - q0 ) >= alpha ||
  3118. abs( p1 - p0 ) >= beta ||
  3119. abs( q1 - q0 ) >= beta ) {
  3120. pix += stride;
  3121. continue;
  3122. }
  3123. if( bS[i] < 4 ) {
  3124. const int tc0 = tc0_table[index_a][bS[i] - 1];
  3125. int tc = tc0;
  3126. int i_delta;
  3127. if( abs( p2 - p0 ) < beta ) {
  3128. pix[-2] = p1 + clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  3129. tc++;
  3130. }
  3131. if( abs( q2 - q0 ) < beta ) {
  3132. pix[1] = q1 + clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  3133. tc++;
  3134. }
  3135. i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  3136. pix[-1] = clip( p0 + i_delta, 0, 255 ); /* p0' */
  3137. pix[0] = clip( q0 - i_delta, 0, 255 ); /* q0' */
  3138. }
  3139. else
  3140. {
  3141. const int c = abs( p0 - q0 ) < (( alpha >> 2 ) + 2 );
  3142. if( abs( p2 - p0 ) < beta && c )
  3143. {
  3144. const uint8_t p3 = pix[-4];
  3145. /* p0', p1', p2' */
  3146. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  3147. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  3148. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  3149. } else {
  3150. /* p0' */
  3151. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  3152. }
  3153. if( abs( q2 - q0 ) < beta && c )
  3154. {
  3155. const uint8_t q3 = pix[3];
  3156. /* q0', q1', q2' */
  3157. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  3158. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  3159. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  3160. } else {
  3161. /* q0' */
  3162. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  3163. }
  3164. }
  3165. pix += stride;
  3166. }
  3167. }
  3168. }
  3169. static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  3170. int i, d;
  3171. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  3172. const int alpha = alpha_table[index_a];
  3173. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  3174. for( i = 0; i < 4; i++ ) {
  3175. if( bS[i] == 0 ) {
  3176. pix += 2 * stride;
  3177. continue;
  3178. }
  3179. /* 2px edge length (because we use same bS than the one for luma) */
  3180. for( d = 0; d < 2; d++ )
  3181. {
  3182. const uint8_t p0 = pix[-1];
  3183. const uint8_t p1 = pix[-2];
  3184. const uint8_t q0 = pix[0];
  3185. const uint8_t q1 = pix[1];
  3186. if( abs( p0 - q0 ) >= alpha ||
  3187. abs( p1 - p0 ) >= beta ||
  3188. abs( q1 - q0 ) >= beta ) {
  3189. pix += stride;
  3190. continue;
  3191. }
  3192. if( bS[i] < 4 ) {
  3193. const int tc = tc0_table[index_a][bS[i] - 1] + 1;
  3194. const int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  3195. pix[-1] = clip( p0 + i_delta, 0, 255 ); /* p0' */
  3196. pix[0] = clip( q0 - i_delta, 0, 255 ); /* q0' */
  3197. } else {
  3198. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  3199. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  3200. }
  3201. pix += stride;
  3202. }
  3203. }
  3204. }
  3205. static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  3206. int i, d;
  3207. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  3208. const int alpha = alpha_table[index_a];
  3209. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  3210. const int pix_next = stride;
  3211. for( i = 0; i < 4; i++ ) {
  3212. if( bS[i] == 0 ) {
  3213. pix += 4;
  3214. continue;
  3215. }
  3216. /* 4px edge length */
  3217. for( d = 0; d < 4; d++ ) {
  3218. const uint8_t p0 = pix[-1*pix_next];
  3219. const uint8_t p1 = pix[-2*pix_next];
  3220. const uint8_t p2 = pix[-3*pix_next];
  3221. const uint8_t q0 = pix[0];
  3222. const uint8_t q1 = pix[1*pix_next];
  3223. const uint8_t q2 = pix[2*pix_next];
  3224. if( abs( p0 - q0 ) >= alpha ||
  3225. abs( p1 - p0 ) >= beta ||
  3226. abs( q1 - q0 ) >= beta ) {
  3227. pix++;
  3228. continue;
  3229. }
  3230. if( bS[i] < 4 ) {
  3231. const int tc0 = tc0_table[index_a][bS[i] - 1];
  3232. int tc = tc0;
  3233. int i_delta;
  3234. if( abs( p2 - p0 ) < beta ) {
  3235. pix[-2*pix_next] = p1 + clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  3236. tc++;
  3237. }
  3238. if( abs( q2 - q0 ) < beta ) {
  3239. pix[pix_next] = q1 + clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  3240. tc++;
  3241. }
  3242. i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  3243. pix[-pix_next] = clip( p0 + i_delta, 0, 255 ); /* p0' */
  3244. pix[0] = clip( q0 - i_delta, 0, 255 ); /* q0' */
  3245. }
  3246. else
  3247. {
  3248. const uint8_t p3 = pix[-4*pix_next];
  3249. const uint8_t q3 = pix[ 3*pix_next];
  3250. const int c = abs( p0 - q0 ) < (( alpha >> 2 ) + 2 );
  3251. if( abs( p2 - p0 ) < beta && c ) {
  3252. /* p0', p1', p2' */
  3253. pix[-1*pix_next] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  3254. pix[-2*pix_next] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  3255. pix[-3*pix_next] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  3256. } else {
  3257. /* p0' */
  3258. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  3259. }
  3260. if( abs( q2 - q0 ) < beta && c ) {
  3261. /* q0', q1', q2' */
  3262. pix[0*pix_next] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  3263. pix[1*pix_next] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  3264. pix[2*pix_next] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  3265. } else {
  3266. /* q0' */
  3267. pix[0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  3268. }
  3269. }
  3270. pix++;
  3271. }
  3272. }
  3273. }
  3274. static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  3275. int i, d;
  3276. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  3277. const int alpha = alpha_table[index_a];
  3278. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  3279. const int pix_next = stride;
  3280. for( i = 0; i < 4; i++ )
  3281. {
  3282. if( bS[i] == 0 ) {
  3283. pix += 2;
  3284. continue;
  3285. }
  3286. /* 2px edge length (see deblocking_filter_edgecv) */
  3287. for( d = 0; d < 2; d++ ) {
  3288. const uint8_t p0 = pix[-1*pix_next];
  3289. const uint8_t p1 = pix[-2*pix_next];
  3290. const uint8_t q0 = pix[0];
  3291. const uint8_t q1 = pix[1*pix_next];
  3292. if( abs( p0 - q0 ) >= alpha ||
  3293. abs( p1 - p0 ) >= beta ||
  3294. abs( q1 - q0 ) >= beta ) {
  3295. pix++;
  3296. continue;
  3297. }
  3298. if( bS[i] < 4 ) {
  3299. int tc = tc0_table[index_a][bS[i] - 1] + 1;
  3300. int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  3301. pix[-pix_next] = clip( p0 + i_delta, 0, 255 ); /* p0' */
  3302. pix[0] = clip( q0 - i_delta, 0, 255 ); /* q0' */
  3303. }
  3304. else
  3305. {
  3306. pix[-pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  3307. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  3308. }
  3309. pix++;
  3310. }
  3311. }
  3312. }
  3313. static void filter_mb( H264Context *h, int mb_x, int mb_y ) {
  3314. MpegEncContext * const s = &h->s;
  3315. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3316. uint8_t *img_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3317. uint8_t *img_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3318. uint8_t *img_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3319. int linesize, uvlinesize;
  3320. int dir;
  3321. #if 0
  3322. /* FIXME what's that ? */
  3323. if( !s->decode )
  3324. return;
  3325. #endif
  3326. /* FIXME Implement deblocking filter for field MB */
  3327. if( h->sps.mb_aff ) {
  3328. return;
  3329. }
  3330. linesize = s->linesize;
  3331. uvlinesize = s->uvlinesize;
  3332. /* dir : 0 -> vertical edge, 1 -> horizontal edge */
  3333. for( dir = 0; dir < 2; dir++ )
  3334. {
  3335. int start = 0;
  3336. int edge;
  3337. /* test picture boundary */
  3338. if( ( dir == 0 && mb_x == 0 ) || ( dir == 1 && mb_y == 0 ) ) {
  3339. start = 1;
  3340. }
  3341. /* FIXME test slice boundary */
  3342. if( h->disable_deblocking_filter_idc == 2 ) {
  3343. }
  3344. /* Calculate bS */
  3345. for( edge = start; edge < 4; edge++ ) {
  3346. /* mbn_xy: neighbour macroblock (how that works for field ?) */
  3347. int mbn_xy = edge > 0 ? mb_xy : ( dir == 0 ? mb_xy -1 : mb_xy - s->mb_stride );
  3348. int bS[4];
  3349. int qp;
  3350. if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) ||
  3351. IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) {
  3352. bS[0] = bS[1] = bS[2] = bS[3] = ( edge == 0 ? 4 : 3 );
  3353. } else {
  3354. int i;
  3355. for( i = 0; i < 4; i++ ) {
  3356. static const uint8_t block_idx_xy[4][4] = {
  3357. { 0, 2, 8, 10}, { 1, 3, 9, 11},
  3358. { 4, 6, 12, 14}, { 5, 7, 13, 15}
  3359. };
  3360. int x = dir == 0 ? edge : i;
  3361. int y = dir == 0 ? i : edge;
  3362. int xn = (x - (dir == 0 ? 1 : 0 ))&0x03;
  3363. int yn = (y - (dir == 0 ? 0 : 1 ))&0x03;
  3364. if( h->non_zero_count[mb_xy][block_idx_xy[x][y]] != 0 ||
  3365. h->non_zero_count[mbn_xy][block_idx_xy[xn][yn]] != 0 ) {
  3366. bS[i] = 2;
  3367. }
  3368. else if( h->slice_type == P_TYPE ) {
  3369. const int b8_xy = h->mb2b8_xy[mb_xy]+(y/2)*h->b8_stride+(x/2);
  3370. const int b8n_xy= h->mb2b8_xy[mbn_xy]+(yn/2)*h->b8_stride+(xn/2);
  3371. const int b_xy = h->mb2b_xy[mb_xy]+y*h->b_stride+x;
  3372. const int bn_xy = h->mb2b_xy[mbn_xy]+yn*h->b_stride+xn;
  3373. if( s->current_picture.ref_index[0][b8_xy] != s->current_picture.ref_index[0][b8n_xy] ||
  3374. abs( s->current_picture.motion_val[0][b_xy][0] - s->current_picture.motion_val[0][bn_xy][0] ) >= 4 ||
  3375. abs( s->current_picture.motion_val[0][b_xy][1] - s->current_picture.motion_val[0][bn_xy][1] ) >= 4 )
  3376. bS[i] = 1;
  3377. else
  3378. bS[i] = 0;
  3379. }
  3380. else {
  3381. /* FIXME Add support for B frame */
  3382. return;
  3383. }
  3384. }
  3385. }
  3386. /* Filter edge */
  3387. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  3388. if( dir == 0 ) {
  3389. filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
  3390. if( edge%2 == 0 ) {
  3391. int chroma_qp = ( get_chroma_qp( h, s->current_picture.qscale_table[mb_xy] ) +
  3392. get_chroma_qp( h, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  3393. filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp );
  3394. filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp );
  3395. }
  3396. } else {
  3397. filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
  3398. if( edge%2 == 0 ) {
  3399. int chroma_qp = ( get_chroma_qp( h, s->current_picture.qscale_table[mb_xy] ) +
  3400. get_chroma_qp( h, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  3401. filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  3402. filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  3403. }
  3404. }
  3405. }
  3406. }
  3407. }
  3408. static int decode_slice(H264Context *h){
  3409. MpegEncContext * const s = &h->s;
  3410. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  3411. s->mb_skip_run= -1;
  3412. #if 1
  3413. for(;;){
  3414. int ret= decode_mb(h);
  3415. hl_decode_mb(h);
  3416. if(ret>=0 && h->sps.mb_aff){ //FIXME optimal? or let mb_decode decode 16x32 ?
  3417. s->mb_y++;
  3418. ret= decode_mb(h);
  3419. hl_decode_mb(h);
  3420. s->mb_y--;
  3421. }
  3422. if(ret<0){
  3423. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  3424. 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);
  3425. return -1;
  3426. }
  3427. if(++s->mb_x >= s->mb_width){
  3428. s->mb_x=0;
  3429. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  3430. if(++s->mb_y >= s->mb_height){
  3431. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  3432. if(get_bits_count(&s->gb) == s->gb.size_in_bits){
  3433. 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);
  3434. return 0;
  3435. }else{
  3436. 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);
  3437. return -1;
  3438. }
  3439. }
  3440. }
  3441. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  3442. if(get_bits_count(&s->gb) == s->gb.size_in_bits){
  3443. 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);
  3444. return 0;
  3445. }else{
  3446. 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);
  3447. return -1;
  3448. }
  3449. }
  3450. }
  3451. #endif
  3452. #if 0
  3453. for(;s->mb_y < s->mb_height; s->mb_y++){
  3454. for(;s->mb_x < s->mb_width; s->mb_x++){
  3455. int ret= decode_mb(h);
  3456. hl_decode_mb(h);
  3457. if(ret<0){
  3458. fprintf(stderr, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  3459. 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);
  3460. return -1;
  3461. }
  3462. if(++s->mb_x >= s->mb_width){
  3463. s->mb_x=0;
  3464. if(++s->mb_y >= s->mb_height){
  3465. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  3466. 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);
  3467. return 0;
  3468. }else{
  3469. 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);
  3470. return -1;
  3471. }
  3472. }
  3473. }
  3474. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  3475. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  3476. 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);
  3477. return 0;
  3478. }else{
  3479. 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);
  3480. return -1;
  3481. }
  3482. }
  3483. }
  3484. s->mb_x=0;
  3485. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  3486. }
  3487. #endif
  3488. return -1; //not reached
  3489. }
  3490. static void filter_frame(H264Context *h) {
  3491. int mb_x = 0;
  3492. int mb_y = 0;
  3493. for( mb_y = 0; mb_y < h->s.mb_height; mb_y++ ) {
  3494. for( mb_x = 0; mb_x < h->s.mb_width; mb_x++ ) {
  3495. filter_mb( h, mb_x, mb_y );
  3496. }
  3497. }
  3498. }
  3499. static inline int decode_vui_parameters(H264Context *h, SPS *sps){
  3500. MpegEncContext * const s = &h->s;
  3501. int aspect_ratio_info_present_flag, aspect_ratio_idc;
  3502. aspect_ratio_info_present_flag= get_bits1(&s->gb);
  3503. if( aspect_ratio_info_present_flag ) {
  3504. aspect_ratio_idc= get_bits(&s->gb, 8);
  3505. if( aspect_ratio_idc == EXTENDED_SAR ) {
  3506. sps->sar.num= get_bits(&s->gb, 16);
  3507. sps->sar.den= get_bits(&s->gb, 16);
  3508. }else if(aspect_ratio_idc < 16){
  3509. sps->sar= pixel_aspect[aspect_ratio_idc];
  3510. }else{
  3511. av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
  3512. return -1;
  3513. }
  3514. }else{
  3515. sps->sar.num=
  3516. sps->sar.den= 0;
  3517. }
  3518. // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
  3519. #if 0
  3520. | overscan_info_present_flag |0 |u(1) |
  3521. | if( overscan_info_present_flag ) | | |
  3522. | overscan_appropriate_flag |0 |u(1) |
  3523. | video_signal_type_present_flag |0 |u(1) |
  3524. | if( video_signal_type_present_flag ) { | | |
  3525. | video_format |0 |u(3) |
  3526. | video_full_range_flag |0 |u(1) |
  3527. | colour_description_present_flag |0 |u(1) |
  3528. | if( colour_description_present_flag ) { | | |
  3529. | colour_primaries |0 |u(8) |
  3530. | transfer_characteristics |0 |u(8) |
  3531. | matrix_coefficients |0 |u(8) |
  3532. | } | | |
  3533. | } | | |
  3534. | chroma_location_info_present_flag |0 |u(1) |
  3535. | if ( chroma_location_info_present_flag ) { | | |
  3536. | chroma_sample_location_type_top_field |0 |ue(v) |
  3537. | chroma_sample_location_type_bottom_field |0 |ue(v) |
  3538. | } | | |
  3539. | timing_info_present_flag |0 |u(1) |
  3540. | if( timing_info_present_flag ) { | | |
  3541. | num_units_in_tick |0 |u(32) |
  3542. | time_scale |0 |u(32) |
  3543. | fixed_frame_rate_flag |0 |u(1) |
  3544. | } | | |
  3545. | nal_hrd_parameters_present_flag |0 |u(1) |
  3546. | if( nal_hrd_parameters_present_flag = = 1) | | |
  3547. | hrd_parameters( ) | | |
  3548. | vcl_hrd_parameters_present_flag |0 |u(1) |
  3549. | if( vcl_hrd_parameters_present_flag = = 1) | | |
  3550. | hrd_parameters( ) | | |
  3551. | if( ( nal_hrd_parameters_present_flag = = 1 | || | |
  3552. | | | |
  3553. |( vcl_hrd_parameters_present_flag = = 1 ) ) | | |
  3554. | low_delay_hrd_flag |0 |u(1) |
  3555. | bitstream_restriction_flag |0 |u(1) |
  3556. | if( bitstream_restriction_flag ) { |0 |u(1) |
  3557. | motion_vectors_over_pic_boundaries_flag |0 |u(1) |
  3558. | max_bytes_per_pic_denom |0 |ue(v) |
  3559. | max_bits_per_mb_denom |0 |ue(v) |
  3560. | log2_max_mv_length_horizontal |0 |ue(v) |
  3561. | log2_max_mv_length_vertical |0 |ue(v) |
  3562. | num_reorder_frames |0 |ue(v) |
  3563. | max_dec_frame_buffering |0 |ue(v) |
  3564. | } | | |
  3565. |} | | |
  3566. #endif
  3567. return 0;
  3568. }
  3569. static inline int decode_seq_parameter_set(H264Context *h){
  3570. MpegEncContext * const s = &h->s;
  3571. int profile_idc, level_idc;
  3572. int sps_id, i;
  3573. SPS *sps;
  3574. profile_idc= get_bits(&s->gb, 8);
  3575. get_bits1(&s->gb); //constraint_set0_flag
  3576. get_bits1(&s->gb); //constraint_set1_flag
  3577. get_bits1(&s->gb); //constraint_set2_flag
  3578. get_bits(&s->gb, 5); // reserved
  3579. level_idc= get_bits(&s->gb, 8);
  3580. sps_id= get_ue_golomb(&s->gb);
  3581. sps= &h->sps_buffer[ sps_id ];
  3582. sps->profile_idc= profile_idc;
  3583. sps->level_idc= level_idc;
  3584. sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
  3585. sps->poc_type= get_ue_golomb(&s->gb);
  3586. if(sps->poc_type == 0){ //FIXME #define
  3587. sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
  3588. } else if(sps->poc_type == 1){//FIXME #define
  3589. sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
  3590. sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
  3591. sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
  3592. sps->poc_cycle_length= get_ue_golomb(&s->gb);
  3593. for(i=0; i<sps->poc_cycle_length; i++)
  3594. sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
  3595. }
  3596. if(sps->poc_type > 2){
  3597. av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
  3598. return -1;
  3599. }
  3600. sps->ref_frame_count= get_ue_golomb(&s->gb);
  3601. sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
  3602. sps->mb_width= get_ue_golomb(&s->gb) + 1;
  3603. sps->mb_height= get_ue_golomb(&s->gb) + 1;
  3604. sps->frame_mbs_only_flag= get_bits1(&s->gb);
  3605. if(!sps->frame_mbs_only_flag)
  3606. sps->mb_aff= get_bits1(&s->gb);
  3607. else
  3608. sps->mb_aff= 0;
  3609. sps->direct_8x8_inference_flag= get_bits1(&s->gb);
  3610. sps->crop= get_bits1(&s->gb);
  3611. if(sps->crop){
  3612. sps->crop_left = get_ue_golomb(&s->gb);
  3613. sps->crop_right = get_ue_golomb(&s->gb);
  3614. sps->crop_top = get_ue_golomb(&s->gb);
  3615. sps->crop_bottom= get_ue_golomb(&s->gb);
  3616. if(sps->crop_left || sps->crop_top){
  3617. av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completly supported, this could look slightly wrong ...\n");
  3618. }
  3619. }else{
  3620. sps->crop_left =
  3621. sps->crop_right =
  3622. sps->crop_top =
  3623. sps->crop_bottom= 0;
  3624. }
  3625. sps->vui_parameters_present_flag= get_bits1(&s->gb);
  3626. if( sps->vui_parameters_present_flag )
  3627. decode_vui_parameters(h, sps);
  3628. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  3629. av_log(h->s.avctx, AV_LOG_DEBUG, "sps:%d profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%d/%d/%d/%d %s\n",
  3630. sps_id, sps->profile_idc, sps->level_idc,
  3631. sps->poc_type,
  3632. sps->ref_frame_count,
  3633. sps->mb_width, sps->mb_height,
  3634. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  3635. sps->direct_8x8_inference_flag ? "8B8" : "",
  3636. sps->crop_left, sps->crop_right,
  3637. sps->crop_top, sps->crop_bottom,
  3638. sps->vui_parameters_present_flag ? "VUI" : ""
  3639. );
  3640. }
  3641. return 0;
  3642. }
  3643. static inline int decode_picture_parameter_set(H264Context *h){
  3644. MpegEncContext * const s = &h->s;
  3645. int pps_id= get_ue_golomb(&s->gb);
  3646. PPS *pps= &h->pps_buffer[pps_id];
  3647. pps->sps_id= get_ue_golomb(&s->gb);
  3648. pps->cabac= get_bits1(&s->gb);
  3649. pps->pic_order_present= get_bits1(&s->gb);
  3650. pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
  3651. if(pps->slice_group_count > 1 ){
  3652. pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
  3653. av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
  3654. switch(pps->mb_slice_group_map_type){
  3655. case 0:
  3656. #if 0
  3657. | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
  3658. | run_length[ i ] |1 |ue(v) |
  3659. #endif
  3660. break;
  3661. case 2:
  3662. #if 0
  3663. | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
  3664. |{ | | |
  3665. | top_left_mb[ i ] |1 |ue(v) |
  3666. | bottom_right_mb[ i ] |1 |ue(v) |
  3667. | } | | |
  3668. #endif
  3669. break;
  3670. case 3:
  3671. case 4:
  3672. case 5:
  3673. #if 0
  3674. | slice_group_change_direction_flag |1 |u(1) |
  3675. | slice_group_change_rate_minus1 |1 |ue(v) |
  3676. #endif
  3677. break;
  3678. case 6:
  3679. #if 0
  3680. | slice_group_id_cnt_minus1 |1 |ue(v) |
  3681. | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
  3682. |) | | |
  3683. | slice_group_id[ i ] |1 |u(v) |
  3684. #endif
  3685. break;
  3686. }
  3687. }
  3688. pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  3689. pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  3690. if(pps->ref_count[0] > 32 || pps->ref_count[1] > 32){
  3691. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
  3692. return -1;
  3693. }
  3694. pps->weighted_pred= get_bits1(&s->gb);
  3695. pps->weighted_bipred_idc= get_bits(&s->gb, 2);
  3696. pps->init_qp= get_se_golomb(&s->gb) + 26;
  3697. pps->init_qs= get_se_golomb(&s->gb) + 26;
  3698. pps->chroma_qp_index_offset= get_se_golomb(&s->gb);
  3699. pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
  3700. pps->constrained_intra_pred= get_bits1(&s->gb);
  3701. pps->redundant_pic_cnt_present = get_bits1(&s->gb);
  3702. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  3703. av_log(h->s.avctx, AV_LOG_DEBUG, "pps:%d sps:%d %s slice_groups:%d ref:%d/%d %s qp:%d/%d/%d %s %s %s\n",
  3704. pps_id, pps->sps_id,
  3705. pps->cabac ? "CABAC" : "CAVLC",
  3706. pps->slice_group_count,
  3707. pps->ref_count[0], pps->ref_count[1],
  3708. pps->weighted_pred ? "weighted" : "",
  3709. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset,
  3710. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  3711. pps->constrained_intra_pred ? "CONSTR" : "",
  3712. pps->redundant_pic_cnt_present ? "REDU" : ""
  3713. );
  3714. }
  3715. return 0;
  3716. }
  3717. /**
  3718. * finds the end of the current frame in the bitstream.
  3719. * @return the position of the first byte of the next frame, or -1
  3720. */
  3721. static int find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
  3722. ParseContext *pc= &s->parse_context;
  3723. int i;
  3724. uint32_t state;
  3725. //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
  3726. // mb_addr= pc->mb_addr - 1;
  3727. state= pc->state;
  3728. //FIXME this will fail with slices
  3729. for(i=0; i<buf_size; i++){
  3730. state= (state<<8) | buf[i];
  3731. if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  3732. if(pc->frame_start_found){
  3733. pc->state=-1;
  3734. pc->frame_start_found= 0;
  3735. return i-3;
  3736. }
  3737. pc->frame_start_found= 1;
  3738. }
  3739. }
  3740. pc->state= state;
  3741. return END_NOT_FOUND;
  3742. }
  3743. static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){
  3744. MpegEncContext * const s = &h->s;
  3745. AVCodecContext * const avctx= s->avctx;
  3746. int buf_index=0;
  3747. #if 0
  3748. int i;
  3749. for(i=0; i<32; i++){
  3750. printf("%X ", buf[i]);
  3751. }
  3752. #endif
  3753. for(;;){
  3754. int consumed;
  3755. int dst_length;
  3756. int bit_length;
  3757. uint8_t *ptr;
  3758. // start code prefix search
  3759. for(; buf_index + 3 < buf_size; buf_index++){
  3760. // this should allways succeed in the first iteration
  3761. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  3762. break;
  3763. }
  3764. if(buf_index+3 >= buf_size) break;
  3765. buf_index+=3;
  3766. ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, buf_size - buf_index);
  3767. if(ptr[dst_length - 1] == 0) dst_length--;
  3768. bit_length= 8*dst_length - decode_rbsp_trailing(ptr + dst_length - 1);
  3769. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  3770. av_log(h->s.avctx, AV_LOG_DEBUG, "NAL %d at %d length %d\n", h->nal_unit_type, buf_index, dst_length);
  3771. }
  3772. buf_index += consumed;
  3773. if(h->nal_ref_idc < s->hurry_up)
  3774. continue;
  3775. switch(h->nal_unit_type){
  3776. case NAL_IDR_SLICE:
  3777. idr(h); //FIXME ensure we dont loose some frames if there is reordering
  3778. case NAL_SLICE:
  3779. init_get_bits(&s->gb, ptr, bit_length);
  3780. h->intra_gb_ptr=
  3781. h->inter_gb_ptr= &s->gb;
  3782. s->data_partitioning = 0;
  3783. if(decode_slice_header(h) < 0) return -1;
  3784. if(h->redundant_pic_count==0)
  3785. decode_slice(h);
  3786. break;
  3787. case NAL_DPA:
  3788. init_get_bits(&s->gb, ptr, bit_length);
  3789. h->intra_gb_ptr=
  3790. h->inter_gb_ptr= NULL;
  3791. s->data_partitioning = 1;
  3792. if(decode_slice_header(h) < 0) return -1;
  3793. break;
  3794. case NAL_DPB:
  3795. init_get_bits(&h->intra_gb, ptr, bit_length);
  3796. h->intra_gb_ptr= &h->intra_gb;
  3797. break;
  3798. case NAL_DPC:
  3799. init_get_bits(&h->inter_gb, ptr, bit_length);
  3800. h->inter_gb_ptr= &h->inter_gb;
  3801. if(h->redundant_pic_count==0 && h->intra_gb_ptr && s->data_partitioning)
  3802. decode_slice(h);
  3803. break;
  3804. case NAL_SEI:
  3805. break;
  3806. case NAL_SPS:
  3807. init_get_bits(&s->gb, ptr, bit_length);
  3808. decode_seq_parameter_set(h);
  3809. if(s->flags& CODEC_FLAG_LOW_DELAY)
  3810. s->low_delay=1;
  3811. avctx->has_b_frames= !s->low_delay;
  3812. break;
  3813. case NAL_PPS:
  3814. init_get_bits(&s->gb, ptr, bit_length);
  3815. decode_picture_parameter_set(h);
  3816. break;
  3817. case NAL_PICTURE_DELIMITER:
  3818. break;
  3819. case NAL_FILTER_DATA:
  3820. break;
  3821. }
  3822. //FIXME move after where irt is set
  3823. s->current_picture.pict_type= s->pict_type;
  3824. s->current_picture.key_frame= s->pict_type == I_TYPE;
  3825. }
  3826. if(!s->current_picture_ptr) return buf_index; //no frame
  3827. h->prev_frame_num_offset= h->frame_num_offset;
  3828. h->prev_frame_num= h->frame_num;
  3829. if(s->current_picture_ptr->reference){
  3830. h->prev_poc_msb= h->poc_msb;
  3831. h->prev_poc_lsb= h->poc_lsb;
  3832. }
  3833. if(s->current_picture_ptr->reference)
  3834. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  3835. else
  3836. assert(h->mmco_index==0);
  3837. ff_er_frame_end(s);
  3838. if( h->disable_deblocking_filter_idc != 1 ) {
  3839. filter_frame( h );
  3840. }
  3841. MPV_frame_end(s);
  3842. return buf_index;
  3843. }
  3844. /**
  3845. * retunrs the number of bytes consumed for building the current frame
  3846. */
  3847. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  3848. if(s->flags&CODEC_FLAG_TRUNCATED){
  3849. pos -= s->parse_context.last_index;
  3850. if(pos<0) pos=0; // FIXME remove (uneeded?)
  3851. return pos;
  3852. }else{
  3853. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  3854. if(pos+10>buf_size) pos=buf_size; // oops ;)
  3855. return pos;
  3856. }
  3857. }
  3858. static int decode_frame(AVCodecContext *avctx,
  3859. void *data, int *data_size,
  3860. uint8_t *buf, int buf_size)
  3861. {
  3862. H264Context *h = avctx->priv_data;
  3863. MpegEncContext *s = &h->s;
  3864. AVFrame *pict = data;
  3865. int buf_index;
  3866. s->flags= avctx->flags;
  3867. s->flags2= avctx->flags2;
  3868. *data_size = 0;
  3869. /* no supplementary picture */
  3870. if (buf_size == 0) {
  3871. return 0;
  3872. }
  3873. if(s->flags&CODEC_FLAG_TRUNCATED){
  3874. int next= find_frame_end(s, buf, buf_size);
  3875. if( ff_combine_frame(s, next, &buf, &buf_size) < 0 )
  3876. return buf_size;
  3877. //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);
  3878. }
  3879. if(s->avctx->extradata_size && s->picture_number==0){
  3880. if(0 < decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) )
  3881. return -1;
  3882. }
  3883. buf_index=decode_nal_units(h, buf, buf_size);
  3884. if(buf_index < 0)
  3885. return -1;
  3886. //FIXME do something with unavailable reference frames
  3887. // if(ret==FRAME_SKIPED) return get_consumed_bytes(s, buf_index, buf_size);
  3888. #if 0
  3889. if(s->pict_type==B_TYPE || s->low_delay){
  3890. *pict= *(AVFrame*)&s->current_picture;
  3891. } else {
  3892. *pict= *(AVFrame*)&s->last_picture;
  3893. }
  3894. #endif
  3895. if(!s->current_picture_ptr){
  3896. av_log(h->s.avctx, AV_LOG_DEBUG, "error, NO frame\n");
  3897. return -1;
  3898. }
  3899. *pict= *(AVFrame*)&s->current_picture; //FIXME
  3900. ff_print_debug_info(s, pict);
  3901. assert(pict->data[0]);
  3902. //printf("out %d\n", (int)pict->data[0]);
  3903. #if 0 //?
  3904. /* Return the Picture timestamp as the frame number */
  3905. /* we substract 1 because it is added on utils.c */
  3906. avctx->frame_number = s->picture_number - 1;
  3907. #endif
  3908. #if 0
  3909. /* dont output the last pic after seeking */
  3910. if(s->last_picture_ptr || s->low_delay)
  3911. //Note this isnt a issue as a IDR pic should flush teh buffers
  3912. #endif
  3913. *data_size = sizeof(AVFrame);
  3914. return get_consumed_bytes(s, buf_index, buf_size);
  3915. }
  3916. #if 0
  3917. static inline void fill_mb_avail(H264Context *h){
  3918. MpegEncContext * const s = &h->s;
  3919. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  3920. if(s->mb_y){
  3921. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  3922. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  3923. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  3924. }else{
  3925. h->mb_avail[0]=
  3926. h->mb_avail[1]=
  3927. h->mb_avail[2]= 0;
  3928. }
  3929. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  3930. h->mb_avail[4]= 1; //FIXME move out
  3931. h->mb_avail[5]= 0; //FIXME move out
  3932. }
  3933. #endif
  3934. #if 0 //selftest
  3935. #define COUNT 8000
  3936. #define SIZE (COUNT*40)
  3937. int main(){
  3938. int i;
  3939. uint8_t temp[SIZE];
  3940. PutBitContext pb;
  3941. GetBitContext gb;
  3942. // int int_temp[10000];
  3943. DSPContext dsp;
  3944. AVCodecContext avctx;
  3945. dsputil_init(&dsp, &avctx);
  3946. init_put_bits(&pb, temp, SIZE);
  3947. printf("testing unsigned exp golomb\n");
  3948. for(i=0; i<COUNT; i++){
  3949. START_TIMER
  3950. set_ue_golomb(&pb, i);
  3951. STOP_TIMER("set_ue_golomb");
  3952. }
  3953. flush_put_bits(&pb);
  3954. init_get_bits(&gb, temp, 8*SIZE);
  3955. for(i=0; i<COUNT; i++){
  3956. int j, s;
  3957. s= show_bits(&gb, 24);
  3958. START_TIMER
  3959. j= get_ue_golomb(&gb);
  3960. if(j != i){
  3961. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  3962. // return -1;
  3963. }
  3964. STOP_TIMER("get_ue_golomb");
  3965. }
  3966. init_put_bits(&pb, temp, SIZE);
  3967. printf("testing signed exp golomb\n");
  3968. for(i=0; i<COUNT; i++){
  3969. START_TIMER
  3970. set_se_golomb(&pb, i - COUNT/2);
  3971. STOP_TIMER("set_se_golomb");
  3972. }
  3973. flush_put_bits(&pb);
  3974. init_get_bits(&gb, temp, 8*SIZE);
  3975. for(i=0; i<COUNT; i++){
  3976. int j, s;
  3977. s= show_bits(&gb, 24);
  3978. START_TIMER
  3979. j= get_se_golomb(&gb);
  3980. if(j != i - COUNT/2){
  3981. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  3982. // return -1;
  3983. }
  3984. STOP_TIMER("get_se_golomb");
  3985. }
  3986. printf("testing 4x4 (I)DCT\n");
  3987. DCTELEM block[16];
  3988. uint8_t src[16], ref[16];
  3989. uint64_t error= 0, max_error=0;
  3990. for(i=0; i<COUNT; i++){
  3991. int j;
  3992. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  3993. for(j=0; j<16; j++){
  3994. ref[j]= random()%255;
  3995. src[j]= random()%255;
  3996. }
  3997. h264_diff_dct_c(block, src, ref, 4);
  3998. //normalize
  3999. for(j=0; j<16; j++){
  4000. // printf("%d ", block[j]);
  4001. block[j]= block[j]*4;
  4002. if(j&1) block[j]= (block[j]*4 + 2)/5;
  4003. if(j&4) block[j]= (block[j]*4 + 2)/5;
  4004. }
  4005. // printf("\n");
  4006. h264_add_idct_c(ref, block, 4);
  4007. /* for(j=0; j<16; j++){
  4008. printf("%d ", ref[j]);
  4009. }
  4010. printf("\n");*/
  4011. for(j=0; j<16; j++){
  4012. int diff= ABS(src[j] - ref[j]);
  4013. error+= diff*diff;
  4014. max_error= FFMAX(max_error, diff);
  4015. }
  4016. }
  4017. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  4018. #if 0
  4019. printf("testing quantizer\n");
  4020. for(qp=0; qp<52; qp++){
  4021. for(i=0; i<16; i++)
  4022. src1_block[i]= src2_block[i]= random()%255;
  4023. }
  4024. #endif
  4025. printf("Testing NAL layer\n");
  4026. uint8_t bitstream[COUNT];
  4027. uint8_t nal[COUNT*2];
  4028. H264Context h;
  4029. memset(&h, 0, sizeof(H264Context));
  4030. for(i=0; i<COUNT; i++){
  4031. int zeros= i;
  4032. int nal_length;
  4033. int consumed;
  4034. int out_length;
  4035. uint8_t *out;
  4036. int j;
  4037. for(j=0; j<COUNT; j++){
  4038. bitstream[j]= (random() % 255) + 1;
  4039. }
  4040. for(j=0; j<zeros; j++){
  4041. int pos= random() % COUNT;
  4042. while(bitstream[pos] == 0){
  4043. pos++;
  4044. pos %= COUNT;
  4045. }
  4046. bitstream[pos]=0;
  4047. }
  4048. START_TIMER
  4049. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  4050. if(nal_length<0){
  4051. printf("encoding failed\n");
  4052. return -1;
  4053. }
  4054. out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
  4055. STOP_TIMER("NAL")
  4056. if(out_length != COUNT){
  4057. printf("incorrect length %d %d\n", out_length, COUNT);
  4058. return -1;
  4059. }
  4060. if(consumed != nal_length){
  4061. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  4062. return -1;
  4063. }
  4064. if(memcmp(bitstream, out, COUNT)){
  4065. printf("missmatch\n");
  4066. return -1;
  4067. }
  4068. }
  4069. printf("Testing RBSP\n");
  4070. return 0;
  4071. }
  4072. #endif
  4073. static int decode_end(AVCodecContext *avctx)
  4074. {
  4075. H264Context *h = avctx->priv_data;
  4076. MpegEncContext *s = &h->s;
  4077. free_tables(h); //FIXME cleanup init stuff perhaps
  4078. MPV_common_end(s);
  4079. // memset(h, 0, sizeof(H264Context));
  4080. return 0;
  4081. }
  4082. AVCodec h264_decoder = {
  4083. "h264",
  4084. CODEC_TYPE_VIDEO,
  4085. CODEC_ID_H264,
  4086. sizeof(H264Context),
  4087. decode_init,
  4088. NULL,
  4089. decode_end,
  4090. decode_frame,
  4091. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  4092. };
  4093. #include "svq3.c"