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.

5692 lines
194KB

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