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.

5720 lines
195KB

  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. #if 1
  1083. for(i=0; i<4; i++){
  1084. const int z0= block[i + 4*0] + block[i + 4*2];
  1085. const int z1= block[i + 4*0] - block[i + 4*2];
  1086. const int z2= (block[i + 4*1]>>1) - block[i + 4*3];
  1087. const int z3= block[i + 4*1] + (block[i + 4*3]>>1);
  1088. block[i + 4*0]= z0 + z3;
  1089. block[i + 4*1]= z1 + z2;
  1090. block[i + 4*2]= z1 - z2;
  1091. block[i + 4*3]= z0 - z3;
  1092. }
  1093. for(i=0; i<4; i++){
  1094. const int z0= block[0 + 4*i] + block[2 + 4*i];
  1095. const int z1= block[0 + 4*i] - block[2 + 4*i];
  1096. const int z2= (block[1 + 4*i]>>1) - block[3 + 4*i];
  1097. const int z3= block[1 + 4*i] + (block[3 + 4*i]>>1);
  1098. dst[0 + i*stride]= cm[ dst[0 + i*stride] + ((z0 + z3) >> 6) ];
  1099. dst[1 + i*stride]= cm[ dst[1 + i*stride] + ((z1 + z2) >> 6) ];
  1100. dst[2 + i*stride]= cm[ dst[2 + i*stride] + ((z1 - z2) >> 6) ];
  1101. dst[3 + i*stride]= cm[ dst[3 + i*stride] + ((z0 - z3) >> 6) ];
  1102. }
  1103. #else
  1104. for(i=0; i<4; i++){
  1105. const int z0= block[0 + 4*i] + block[2 + 4*i];
  1106. const int z1= block[0 + 4*i] - block[2 + 4*i];
  1107. const int z2= (block[1 + 4*i]>>1) - block[3 + 4*i];
  1108. const int z3= block[1 + 4*i] + (block[3 + 4*i]>>1);
  1109. block[0 + 4*i]= z0 + z3;
  1110. block[1 + 4*i]= z1 + z2;
  1111. block[2 + 4*i]= z1 - z2;
  1112. block[3 + 4*i]= z0 - z3;
  1113. }
  1114. for(i=0; i<4; i++){
  1115. const int z0= block[i + 4*0] + block[i + 4*2];
  1116. const int z1= block[i + 4*0] - block[i + 4*2];
  1117. const int z2= (block[i + 4*1]>>1) - block[i + 4*3];
  1118. const int z3= block[i + 4*1] + (block[i + 4*3]>>1);
  1119. dst[i + 0*stride]= cm[ dst[i + 0*stride] + ((z0 + z3) >> 6) ];
  1120. dst[i + 1*stride]= cm[ dst[i + 1*stride] + ((z1 + z2) >> 6) ];
  1121. dst[i + 2*stride]= cm[ dst[i + 2*stride] + ((z1 - z2) >> 6) ];
  1122. dst[i + 3*stride]= cm[ dst[i + 3*stride] + ((z0 - z3) >> 6) ];
  1123. }
  1124. #endif
  1125. }
  1126. #if 0
  1127. static void h264_diff_dct_c(DCTELEM *block, uint8_t *src1, uint8_t *src2, int stride){
  1128. int i;
  1129. //FIXME try int temp instead of block
  1130. for(i=0; i<4; i++){
  1131. const int d0= src1[0 + i*stride] - src2[0 + i*stride];
  1132. const int d1= src1[1 + i*stride] - src2[1 + i*stride];
  1133. const int d2= src1[2 + i*stride] - src2[2 + i*stride];
  1134. const int d3= src1[3 + i*stride] - src2[3 + i*stride];
  1135. const int z0= d0 + d3;
  1136. const int z3= d0 - d3;
  1137. const int z1= d1 + d2;
  1138. const int z2= d1 - d2;
  1139. block[0 + 4*i]= z0 + z1;
  1140. block[1 + 4*i]= 2*z3 + z2;
  1141. block[2 + 4*i]= z0 - z1;
  1142. block[3 + 4*i]= z3 - 2*z2;
  1143. }
  1144. for(i=0; i<4; i++){
  1145. const int z0= block[0*4 + i] + block[3*4 + i];
  1146. const int z3= block[0*4 + i] - block[3*4 + i];
  1147. const int z1= block[1*4 + i] + block[2*4 + i];
  1148. const int z2= block[1*4 + i] - block[2*4 + i];
  1149. block[0*4 + i]= z0 + z1;
  1150. block[1*4 + i]= 2*z3 + z2;
  1151. block[2*4 + i]= z0 - z1;
  1152. block[3*4 + i]= z3 - 2*z2;
  1153. }
  1154. }
  1155. #endif
  1156. //FIXME need to check that this doesnt overflow signed 32 bit for low qp, iam not sure, its very close
  1157. //FIXME check that gcc inlines this (and optimizes intra & seperate_dc stuff away)
  1158. static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int seperate_dc){
  1159. int i;
  1160. const int * const quant_table= quant_coeff[qscale];
  1161. const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
  1162. const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
  1163. const unsigned int threshold2= (threshold1<<1);
  1164. int last_non_zero;
  1165. if(seperate_dc){
  1166. if(qscale<=18){
  1167. //avoid overflows
  1168. const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
  1169. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
  1170. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1171. int level= block[0]*quant_coeff[qscale+18][0];
  1172. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1173. if(level>0){
  1174. level= (dc_bias + level)>>(QUANT_SHIFT-2);
  1175. block[0]= level;
  1176. }else{
  1177. level= (dc_bias - level)>>(QUANT_SHIFT-2);
  1178. block[0]= -level;
  1179. }
  1180. // last_non_zero = i;
  1181. }else{
  1182. block[0]=0;
  1183. }
  1184. }else{
  1185. const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
  1186. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
  1187. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1188. int level= block[0]*quant_table[0];
  1189. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1190. if(level>0){
  1191. level= (dc_bias + level)>>(QUANT_SHIFT+1);
  1192. block[0]= level;
  1193. }else{
  1194. level= (dc_bias - level)>>(QUANT_SHIFT+1);
  1195. block[0]= -level;
  1196. }
  1197. // last_non_zero = i;
  1198. }else{
  1199. block[0]=0;
  1200. }
  1201. }
  1202. last_non_zero= 0;
  1203. i=1;
  1204. }else{
  1205. last_non_zero= -1;
  1206. i=0;
  1207. }
  1208. for(; i<16; i++){
  1209. const int j= scantable[i];
  1210. int level= block[j]*quant_table[j];
  1211. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  1212. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  1213. if(((unsigned)(level+threshold1))>threshold2){
  1214. if(level>0){
  1215. level= (bias + level)>>QUANT_SHIFT;
  1216. block[j]= level;
  1217. }else{
  1218. level= (bias - level)>>QUANT_SHIFT;
  1219. block[j]= -level;
  1220. }
  1221. last_non_zero = i;
  1222. }else{
  1223. block[j]=0;
  1224. }
  1225. }
  1226. return last_non_zero;
  1227. }
  1228. static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){
  1229. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1230. ((uint32_t*)(src+0*stride))[0]= a;
  1231. ((uint32_t*)(src+1*stride))[0]= a;
  1232. ((uint32_t*)(src+2*stride))[0]= a;
  1233. ((uint32_t*)(src+3*stride))[0]= a;
  1234. }
  1235. static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){
  1236. ((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101;
  1237. ((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101;
  1238. ((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101;
  1239. ((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101;
  1240. }
  1241. static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1242. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
  1243. + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
  1244. ((uint32_t*)(src+0*stride))[0]=
  1245. ((uint32_t*)(src+1*stride))[0]=
  1246. ((uint32_t*)(src+2*stride))[0]=
  1247. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1248. }
  1249. static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1250. const int dc= ( src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
  1251. ((uint32_t*)(src+0*stride))[0]=
  1252. ((uint32_t*)(src+1*stride))[0]=
  1253. ((uint32_t*)(src+2*stride))[0]=
  1254. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1255. }
  1256. static void pred4x4_top_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1257. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2;
  1258. ((uint32_t*)(src+0*stride))[0]=
  1259. ((uint32_t*)(src+1*stride))[0]=
  1260. ((uint32_t*)(src+2*stride))[0]=
  1261. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1262. }
  1263. static void pred4x4_128_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1264. ((uint32_t*)(src+0*stride))[0]=
  1265. ((uint32_t*)(src+1*stride))[0]=
  1266. ((uint32_t*)(src+2*stride))[0]=
  1267. ((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U;
  1268. }
  1269. #define LOAD_TOP_RIGHT_EDGE\
  1270. const int t4= topright[0];\
  1271. const int t5= topright[1];\
  1272. const int t6= topright[2];\
  1273. const int t7= topright[3];\
  1274. #define LOAD_LEFT_EDGE\
  1275. const int l0= src[-1+0*stride];\
  1276. const int l1= src[-1+1*stride];\
  1277. const int l2= src[-1+2*stride];\
  1278. const int l3= src[-1+3*stride];\
  1279. #define LOAD_TOP_EDGE\
  1280. const int t0= src[ 0-1*stride];\
  1281. const int t1= src[ 1-1*stride];\
  1282. const int t2= src[ 2-1*stride];\
  1283. const int t3= src[ 3-1*stride];\
  1284. static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){
  1285. const int lt= src[-1-1*stride];
  1286. LOAD_TOP_EDGE
  1287. LOAD_LEFT_EDGE
  1288. src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
  1289. src[0+2*stride]=
  1290. src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
  1291. src[0+1*stride]=
  1292. src[1+2*stride]=
  1293. src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
  1294. src[0+0*stride]=
  1295. src[1+1*stride]=
  1296. src[2+2*stride]=
  1297. src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1298. src[1+0*stride]=
  1299. src[2+1*stride]=
  1300. src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1301. src[2+0*stride]=
  1302. src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1303. src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1304. }
  1305. static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){
  1306. LOAD_TOP_EDGE
  1307. LOAD_TOP_RIGHT_EDGE
  1308. // LOAD_LEFT_EDGE
  1309. src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
  1310. src[1+0*stride]=
  1311. src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
  1312. src[2+0*stride]=
  1313. src[1+1*stride]=
  1314. src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
  1315. src[3+0*stride]=
  1316. src[2+1*stride]=
  1317. src[1+2*stride]=
  1318. src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
  1319. src[3+1*stride]=
  1320. src[2+2*stride]=
  1321. src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
  1322. src[3+2*stride]=
  1323. src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
  1324. src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
  1325. }
  1326. static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
  1327. const int lt= src[-1-1*stride];
  1328. LOAD_TOP_EDGE
  1329. LOAD_LEFT_EDGE
  1330. const __attribute__((unused)) int unu= l3;
  1331. src[0+0*stride]=
  1332. src[1+2*stride]=(lt + t0 + 1)>>1;
  1333. src[1+0*stride]=
  1334. src[2+2*stride]=(t0 + t1 + 1)>>1;
  1335. src[2+0*stride]=
  1336. src[3+2*stride]=(t1 + t2 + 1)>>1;
  1337. src[3+0*stride]=(t2 + t3 + 1)>>1;
  1338. src[0+1*stride]=
  1339. src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1340. src[1+1*stride]=
  1341. src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1342. src[2+1*stride]=
  1343. src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1344. src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1345. src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1346. src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1347. }
  1348. static void pred4x4_vertical_left_c(uint8_t *src, uint8_t *topright, int stride){
  1349. LOAD_TOP_EDGE
  1350. LOAD_TOP_RIGHT_EDGE
  1351. const __attribute__((unused)) int unu= t7;
  1352. src[0+0*stride]=(t0 + t1 + 1)>>1;
  1353. src[1+0*stride]=
  1354. src[0+2*stride]=(t1 + t2 + 1)>>1;
  1355. src[2+0*stride]=
  1356. src[1+2*stride]=(t2 + t3 + 1)>>1;
  1357. src[3+0*stride]=
  1358. src[2+2*stride]=(t3 + t4+ 1)>>1;
  1359. src[3+2*stride]=(t4 + t5+ 1)>>1;
  1360. src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1361. src[1+1*stride]=
  1362. src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1363. src[2+1*stride]=
  1364. src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
  1365. src[3+1*stride]=
  1366. src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
  1367. src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
  1368. }
  1369. static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){
  1370. LOAD_LEFT_EDGE
  1371. src[0+0*stride]=(l0 + l1 + 1)>>1;
  1372. src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1373. src[2+0*stride]=
  1374. src[0+1*stride]=(l1 + l2 + 1)>>1;
  1375. src[3+0*stride]=
  1376. src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1377. src[2+1*stride]=
  1378. src[0+2*stride]=(l2 + l3 + 1)>>1;
  1379. src[3+1*stride]=
  1380. src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
  1381. src[3+2*stride]=
  1382. src[1+3*stride]=
  1383. src[0+3*stride]=
  1384. src[2+2*stride]=
  1385. src[2+3*stride]=
  1386. src[3+3*stride]=l3;
  1387. }
  1388. static void pred4x4_horizontal_down_c(uint8_t *src, uint8_t *topright, int stride){
  1389. const int lt= src[-1-1*stride];
  1390. LOAD_TOP_EDGE
  1391. LOAD_LEFT_EDGE
  1392. const __attribute__((unused)) int unu= t3;
  1393. src[0+0*stride]=
  1394. src[2+1*stride]=(lt + l0 + 1)>>1;
  1395. src[1+0*stride]=
  1396. src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1397. src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1398. src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1399. src[0+1*stride]=
  1400. src[2+2*stride]=(l0 + l1 + 1)>>1;
  1401. src[1+1*stride]=
  1402. src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1403. src[0+2*stride]=
  1404. src[2+3*stride]=(l1 + l2+ 1)>>1;
  1405. src[1+2*stride]=
  1406. src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1407. src[0+3*stride]=(l2 + l3 + 1)>>1;
  1408. src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1409. }
  1410. static void pred16x16_vertical_c(uint8_t *src, int stride){
  1411. int i;
  1412. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1413. const uint32_t b= ((uint32_t*)(src-stride))[1];
  1414. const uint32_t c= ((uint32_t*)(src-stride))[2];
  1415. const uint32_t d= ((uint32_t*)(src-stride))[3];
  1416. for(i=0; i<16; i++){
  1417. ((uint32_t*)(src+i*stride))[0]= a;
  1418. ((uint32_t*)(src+i*stride))[1]= b;
  1419. ((uint32_t*)(src+i*stride))[2]= c;
  1420. ((uint32_t*)(src+i*stride))[3]= d;
  1421. }
  1422. }
  1423. static void pred16x16_horizontal_c(uint8_t *src, int stride){
  1424. int i;
  1425. for(i=0; i<16; i++){
  1426. ((uint32_t*)(src+i*stride))[0]=
  1427. ((uint32_t*)(src+i*stride))[1]=
  1428. ((uint32_t*)(src+i*stride))[2]=
  1429. ((uint32_t*)(src+i*stride))[3]= src[-1+i*stride]*0x01010101;
  1430. }
  1431. }
  1432. static void pred16x16_dc_c(uint8_t *src, int stride){
  1433. int i, dc=0;
  1434. for(i=0;i<16; i++){
  1435. dc+= src[-1+i*stride];
  1436. }
  1437. for(i=0;i<16; i++){
  1438. dc+= src[i-stride];
  1439. }
  1440. dc= 0x01010101*((dc + 16)>>5);
  1441. for(i=0; i<16; i++){
  1442. ((uint32_t*)(src+i*stride))[0]=
  1443. ((uint32_t*)(src+i*stride))[1]=
  1444. ((uint32_t*)(src+i*stride))[2]=
  1445. ((uint32_t*)(src+i*stride))[3]= dc;
  1446. }
  1447. }
  1448. static void pred16x16_left_dc_c(uint8_t *src, int stride){
  1449. int i, dc=0;
  1450. for(i=0;i<16; i++){
  1451. dc+= src[-1+i*stride];
  1452. }
  1453. dc= 0x01010101*((dc + 8)>>4);
  1454. for(i=0; i<16; i++){
  1455. ((uint32_t*)(src+i*stride))[0]=
  1456. ((uint32_t*)(src+i*stride))[1]=
  1457. ((uint32_t*)(src+i*stride))[2]=
  1458. ((uint32_t*)(src+i*stride))[3]= dc;
  1459. }
  1460. }
  1461. static void pred16x16_top_dc_c(uint8_t *src, int stride){
  1462. int i, dc=0;
  1463. for(i=0;i<16; i++){
  1464. dc+= src[i-stride];
  1465. }
  1466. dc= 0x01010101*((dc + 8)>>4);
  1467. for(i=0; i<16; i++){
  1468. ((uint32_t*)(src+i*stride))[0]=
  1469. ((uint32_t*)(src+i*stride))[1]=
  1470. ((uint32_t*)(src+i*stride))[2]=
  1471. ((uint32_t*)(src+i*stride))[3]= dc;
  1472. }
  1473. }
  1474. static void pred16x16_128_dc_c(uint8_t *src, int stride){
  1475. int i;
  1476. for(i=0; i<16; i++){
  1477. ((uint32_t*)(src+i*stride))[0]=
  1478. ((uint32_t*)(src+i*stride))[1]=
  1479. ((uint32_t*)(src+i*stride))[2]=
  1480. ((uint32_t*)(src+i*stride))[3]= 0x01010101U*128U;
  1481. }
  1482. }
  1483. static inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){
  1484. int i, j, k;
  1485. int a;
  1486. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  1487. const uint8_t * const src0 = src+7-stride;
  1488. const uint8_t *src1 = src+8*stride-1;
  1489. const uint8_t *src2 = src1-2*stride; // == src+6*stride-1;
  1490. int H = src0[1] - src0[-1];
  1491. int V = src1[0] - src2[ 0];
  1492. for(k=2; k<=8; ++k) {
  1493. src1 += stride; src2 -= stride;
  1494. H += k*(src0[k] - src0[-k]);
  1495. V += k*(src1[0] - src2[ 0]);
  1496. }
  1497. if(svq3){
  1498. H = ( 5*(H/4) ) / 16;
  1499. V = ( 5*(V/4) ) / 16;
  1500. /* required for 100% accuracy */
  1501. i = H; H = V; V = i;
  1502. }else{
  1503. H = ( 5*H+32 ) >> 6;
  1504. V = ( 5*V+32 ) >> 6;
  1505. }
  1506. a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
  1507. for(j=16; j>0; --j) {
  1508. int b = a;
  1509. a += V;
  1510. for(i=-16; i<0; i+=4) {
  1511. src[16+i] = cm[ (b ) >> 5 ];
  1512. src[17+i] = cm[ (b+ H) >> 5 ];
  1513. src[18+i] = cm[ (b+2*H) >> 5 ];
  1514. src[19+i] = cm[ (b+3*H) >> 5 ];
  1515. b += 4*H;
  1516. }
  1517. src += stride;
  1518. }
  1519. }
  1520. static void pred16x16_plane_c(uint8_t *src, int stride){
  1521. pred16x16_plane_compat_c(src, stride, 0);
  1522. }
  1523. static void pred8x8_vertical_c(uint8_t *src, int stride){
  1524. int i;
  1525. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1526. const uint32_t b= ((uint32_t*)(src-stride))[1];
  1527. for(i=0; i<8; i++){
  1528. ((uint32_t*)(src+i*stride))[0]= a;
  1529. ((uint32_t*)(src+i*stride))[1]= b;
  1530. }
  1531. }
  1532. static void pred8x8_horizontal_c(uint8_t *src, int stride){
  1533. int i;
  1534. for(i=0; i<8; i++){
  1535. ((uint32_t*)(src+i*stride))[0]=
  1536. ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101;
  1537. }
  1538. }
  1539. static void pred8x8_128_dc_c(uint8_t *src, int stride){
  1540. int i;
  1541. for(i=0; i<4; i++){
  1542. ((uint32_t*)(src+i*stride))[0]=
  1543. ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
  1544. }
  1545. for(i=4; i<8; i++){
  1546. ((uint32_t*)(src+i*stride))[0]=
  1547. ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
  1548. }
  1549. }
  1550. static void pred8x8_left_dc_c(uint8_t *src, int stride){
  1551. int i;
  1552. int dc0, dc2;
  1553. dc0=dc2=0;
  1554. for(i=0;i<4; i++){
  1555. dc0+= src[-1+i*stride];
  1556. dc2+= src[-1+(i+4)*stride];
  1557. }
  1558. dc0= 0x01010101*((dc0 + 2)>>2);
  1559. dc2= 0x01010101*((dc2 + 2)>>2);
  1560. for(i=0; i<4; i++){
  1561. ((uint32_t*)(src+i*stride))[0]=
  1562. ((uint32_t*)(src+i*stride))[1]= dc0;
  1563. }
  1564. for(i=4; i<8; i++){
  1565. ((uint32_t*)(src+i*stride))[0]=
  1566. ((uint32_t*)(src+i*stride))[1]= dc2;
  1567. }
  1568. }
  1569. static void pred8x8_top_dc_c(uint8_t *src, int stride){
  1570. int i;
  1571. int dc0, dc1;
  1572. dc0=dc1=0;
  1573. for(i=0;i<4; i++){
  1574. dc0+= src[i-stride];
  1575. dc1+= src[4+i-stride];
  1576. }
  1577. dc0= 0x01010101*((dc0 + 2)>>2);
  1578. dc1= 0x01010101*((dc1 + 2)>>2);
  1579. for(i=0; i<4; i++){
  1580. ((uint32_t*)(src+i*stride))[0]= dc0;
  1581. ((uint32_t*)(src+i*stride))[1]= dc1;
  1582. }
  1583. for(i=4; i<8; i++){
  1584. ((uint32_t*)(src+i*stride))[0]= dc0;
  1585. ((uint32_t*)(src+i*stride))[1]= dc1;
  1586. }
  1587. }
  1588. static void pred8x8_dc_c(uint8_t *src, int stride){
  1589. int i;
  1590. int dc0, dc1, dc2, dc3;
  1591. dc0=dc1=dc2=0;
  1592. for(i=0;i<4; i++){
  1593. dc0+= src[-1+i*stride] + src[i-stride];
  1594. dc1+= src[4+i-stride];
  1595. dc2+= src[-1+(i+4)*stride];
  1596. }
  1597. dc3= 0x01010101*((dc1 + dc2 + 4)>>3);
  1598. dc0= 0x01010101*((dc0 + 4)>>3);
  1599. dc1= 0x01010101*((dc1 + 2)>>2);
  1600. dc2= 0x01010101*((dc2 + 2)>>2);
  1601. for(i=0; i<4; i++){
  1602. ((uint32_t*)(src+i*stride))[0]= dc0;
  1603. ((uint32_t*)(src+i*stride))[1]= dc1;
  1604. }
  1605. for(i=4; i<8; i++){
  1606. ((uint32_t*)(src+i*stride))[0]= dc2;
  1607. ((uint32_t*)(src+i*stride))[1]= dc3;
  1608. }
  1609. }
  1610. static void pred8x8_plane_c(uint8_t *src, int stride){
  1611. int j, k;
  1612. int a;
  1613. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  1614. const uint8_t * const src0 = src+3-stride;
  1615. const uint8_t *src1 = src+4*stride-1;
  1616. const uint8_t *src2 = src1-2*stride; // == src+2*stride-1;
  1617. int H = src0[1] - src0[-1];
  1618. int V = src1[0] - src2[ 0];
  1619. for(k=2; k<=4; ++k) {
  1620. src1 += stride; src2 -= stride;
  1621. H += k*(src0[k] - src0[-k]);
  1622. V += k*(src1[0] - src2[ 0]);
  1623. }
  1624. H = ( 17*H+16 ) >> 5;
  1625. V = ( 17*V+16 ) >> 5;
  1626. a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
  1627. for(j=8; j>0; --j) {
  1628. int b = a;
  1629. a += V;
  1630. src[0] = cm[ (b ) >> 5 ];
  1631. src[1] = cm[ (b+ H) >> 5 ];
  1632. src[2] = cm[ (b+2*H) >> 5 ];
  1633. src[3] = cm[ (b+3*H) >> 5 ];
  1634. src[4] = cm[ (b+4*H) >> 5 ];
  1635. src[5] = cm[ (b+5*H) >> 5 ];
  1636. src[6] = cm[ (b+6*H) >> 5 ];
  1637. src[7] = cm[ (b+7*H) >> 5 ];
  1638. src += stride;
  1639. }
  1640. }
  1641. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  1642. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1643. int src_x_offset, int src_y_offset,
  1644. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  1645. MpegEncContext * const s = &h->s;
  1646. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  1647. const int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  1648. const int luma_xy= (mx&3) + ((my&3)<<2);
  1649. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*s->linesize;
  1650. uint8_t * src_cb= pic->data[1] + (mx>>3) + (my>>3)*s->uvlinesize;
  1651. uint8_t * src_cr= pic->data[2] + (mx>>3) + (my>>3)*s->uvlinesize;
  1652. int extra_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16; //FIXME increase edge?, IMHO not worth it
  1653. int extra_height= extra_width;
  1654. int emu=0;
  1655. const int full_mx= mx>>2;
  1656. const int full_my= my>>2;
  1657. assert(pic->data[0]);
  1658. if(mx&7) extra_width -= 3;
  1659. if(my&7) extra_height -= 3;
  1660. if( full_mx < 0-extra_width
  1661. || full_my < 0-extra_height
  1662. || full_mx + 16/*FIXME*/ > s->width + extra_width
  1663. || full_my + 16/*FIXME*/ > s->height + extra_height){
  1664. 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);
  1665. src_y= s->edge_emu_buffer + 2 + 2*s->linesize;
  1666. emu=1;
  1667. }
  1668. qpix_op[luma_xy](dest_y, src_y, s->linesize); //FIXME try variable height perhaps?
  1669. if(!square){
  1670. qpix_op[luma_xy](dest_y + delta, src_y + delta, s->linesize);
  1671. }
  1672. if(s->flags&CODEC_FLAG_GRAY) return;
  1673. if(emu){
  1674. 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);
  1675. src_cb= s->edge_emu_buffer;
  1676. }
  1677. chroma_op(dest_cb, src_cb, s->uvlinesize, chroma_height, mx&7, my&7);
  1678. if(emu){
  1679. 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);
  1680. src_cr= s->edge_emu_buffer;
  1681. }
  1682. chroma_op(dest_cr, src_cr, s->uvlinesize, chroma_height, mx&7, my&7);
  1683. }
  1684. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  1685. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1686. int x_offset, int y_offset,
  1687. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  1688. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  1689. int list0, int list1){
  1690. MpegEncContext * const s = &h->s;
  1691. qpel_mc_func *qpix_op= qpix_put;
  1692. h264_chroma_mc_func chroma_op= chroma_put;
  1693. dest_y += 2*x_offset + 2*y_offset*s-> linesize;
  1694. dest_cb += x_offset + y_offset*s->uvlinesize;
  1695. dest_cr += x_offset + y_offset*s->uvlinesize;
  1696. x_offset += 8*s->mb_x;
  1697. y_offset += 8*s->mb_y;
  1698. if(list0){
  1699. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  1700. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  1701. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1702. qpix_op, chroma_op);
  1703. qpix_op= qpix_avg;
  1704. chroma_op= chroma_avg;
  1705. }
  1706. if(list1){
  1707. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  1708. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  1709. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1710. qpix_op, chroma_op);
  1711. }
  1712. }
  1713. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1714. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  1715. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg)){
  1716. MpegEncContext * const s = &h->s;
  1717. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  1718. const int mb_type= s->current_picture.mb_type[mb_xy];
  1719. assert(IS_INTER(mb_type));
  1720. if(IS_16X16(mb_type)){
  1721. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  1722. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  1723. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  1724. }else if(IS_16X8(mb_type)){
  1725. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  1726. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  1727. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  1728. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  1729. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  1730. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  1731. }else if(IS_8X16(mb_type)){
  1732. mc_part(h, 0, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 0, 0,
  1733. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  1734. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  1735. mc_part(h, 4, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 4, 0,
  1736. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  1737. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  1738. }else{
  1739. int i;
  1740. assert(IS_8X8(mb_type));
  1741. for(i=0; i<4; i++){
  1742. const int sub_mb_type= h->sub_mb_type[i];
  1743. const int n= 4*i;
  1744. int x_offset= (i&1)<<2;
  1745. int y_offset= (i&2)<<1;
  1746. if(IS_SUB_8X8(sub_mb_type)){
  1747. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1748. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  1749. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1750. }else if(IS_SUB_8X4(sub_mb_type)){
  1751. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1752. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  1753. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1754. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  1755. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  1756. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1757. }else if(IS_SUB_4X8(sub_mb_type)){
  1758. mc_part(h, n , 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1759. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  1760. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1761. mc_part(h, n+1, 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
  1762. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  1763. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1764. }else{
  1765. int j;
  1766. assert(IS_SUB_4X4(sub_mb_type));
  1767. for(j=0; j<4; j++){
  1768. int sub_x_offset= x_offset + 2*(j&1);
  1769. int sub_y_offset= y_offset + (j&2);
  1770. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  1771. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  1772. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1773. }
  1774. }
  1775. }
  1776. }
  1777. }
  1778. static void decode_init_vlc(H264Context *h){
  1779. static int done = 0;
  1780. if (!done) {
  1781. int i;
  1782. done = 1;
  1783. init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
  1784. &chroma_dc_coeff_token_len [0], 1, 1,
  1785. &chroma_dc_coeff_token_bits[0], 1, 1);
  1786. for(i=0; i<4; i++){
  1787. init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
  1788. &coeff_token_len [i][0], 1, 1,
  1789. &coeff_token_bits[i][0], 1, 1);
  1790. }
  1791. for(i=0; i<3; i++){
  1792. init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
  1793. &chroma_dc_total_zeros_len [i][0], 1, 1,
  1794. &chroma_dc_total_zeros_bits[i][0], 1, 1);
  1795. }
  1796. for(i=0; i<15; i++){
  1797. init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
  1798. &total_zeros_len [i][0], 1, 1,
  1799. &total_zeros_bits[i][0], 1, 1);
  1800. }
  1801. for(i=0; i<6; i++){
  1802. init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
  1803. &run_len [i][0], 1, 1,
  1804. &run_bits[i][0], 1, 1);
  1805. }
  1806. init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
  1807. &run_len [6][0], 1, 1,
  1808. &run_bits[6][0], 1, 1);
  1809. }
  1810. }
  1811. /**
  1812. * Sets the intra prediction function pointers.
  1813. */
  1814. static void init_pred_ptrs(H264Context *h){
  1815. // MpegEncContext * const s = &h->s;
  1816. h->pred4x4[VERT_PRED ]= pred4x4_vertical_c;
  1817. h->pred4x4[HOR_PRED ]= pred4x4_horizontal_c;
  1818. h->pred4x4[DC_PRED ]= pred4x4_dc_c;
  1819. h->pred4x4[DIAG_DOWN_LEFT_PRED ]= pred4x4_down_left_c;
  1820. h->pred4x4[DIAG_DOWN_RIGHT_PRED]= pred4x4_down_right_c;
  1821. h->pred4x4[VERT_RIGHT_PRED ]= pred4x4_vertical_right_c;
  1822. h->pred4x4[HOR_DOWN_PRED ]= pred4x4_horizontal_down_c;
  1823. h->pred4x4[VERT_LEFT_PRED ]= pred4x4_vertical_left_c;
  1824. h->pred4x4[HOR_UP_PRED ]= pred4x4_horizontal_up_c;
  1825. h->pred4x4[LEFT_DC_PRED ]= pred4x4_left_dc_c;
  1826. h->pred4x4[TOP_DC_PRED ]= pred4x4_top_dc_c;
  1827. h->pred4x4[DC_128_PRED ]= pred4x4_128_dc_c;
  1828. h->pred8x8[DC_PRED8x8 ]= pred8x8_dc_c;
  1829. h->pred8x8[VERT_PRED8x8 ]= pred8x8_vertical_c;
  1830. h->pred8x8[HOR_PRED8x8 ]= pred8x8_horizontal_c;
  1831. h->pred8x8[PLANE_PRED8x8 ]= pred8x8_plane_c;
  1832. h->pred8x8[LEFT_DC_PRED8x8]= pred8x8_left_dc_c;
  1833. h->pred8x8[TOP_DC_PRED8x8 ]= pred8x8_top_dc_c;
  1834. h->pred8x8[DC_128_PRED8x8 ]= pred8x8_128_dc_c;
  1835. h->pred16x16[DC_PRED8x8 ]= pred16x16_dc_c;
  1836. h->pred16x16[VERT_PRED8x8 ]= pred16x16_vertical_c;
  1837. h->pred16x16[HOR_PRED8x8 ]= pred16x16_horizontal_c;
  1838. h->pred16x16[PLANE_PRED8x8 ]= pred16x16_plane_c;
  1839. h->pred16x16[LEFT_DC_PRED8x8]= pred16x16_left_dc_c;
  1840. h->pred16x16[TOP_DC_PRED8x8 ]= pred16x16_top_dc_c;
  1841. h->pred16x16[DC_128_PRED8x8 ]= pred16x16_128_dc_c;
  1842. }
  1843. static void free_tables(H264Context *h){
  1844. av_freep(&h->intra4x4_pred_mode);
  1845. av_freep(&h->chroma_pred_mode_table);
  1846. av_freep(&h->cbp_table);
  1847. av_freep(&h->non_zero_count);
  1848. av_freep(&h->slice_table_base);
  1849. av_freep(&h->top_border);
  1850. h->slice_table= NULL;
  1851. av_freep(&h->mb2b_xy);
  1852. av_freep(&h->mb2b8_xy);
  1853. }
  1854. /**
  1855. * allocates tables.
  1856. * needs widzh/height
  1857. */
  1858. static int alloc_tables(H264Context *h){
  1859. MpegEncContext * const s = &h->s;
  1860. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  1861. int x,y;
  1862. CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
  1863. CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
  1864. CHECKED_ALLOCZ(h->slice_table_base , big_mb_num * sizeof(uint8_t))
  1865. CHECKED_ALLOCZ(h->top_border , s->mb_width * (16+8+8) * sizeof(uint8_t))
  1866. if( h->pps.cabac ) {
  1867. CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
  1868. CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
  1869. } else {
  1870. h->chroma_pred_mode_table = NULL;
  1871. h->cbp_table = NULL;
  1872. }
  1873. memset(h->slice_table_base, -1, big_mb_num * sizeof(uint8_t));
  1874. h->slice_table= h->slice_table_base + s->mb_stride + 1;
  1875. CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint16_t));
  1876. CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint16_t));
  1877. for(y=0; y<s->mb_height; y++){
  1878. for(x=0; x<s->mb_width; x++){
  1879. const int mb_xy= x + y*s->mb_stride;
  1880. const int b_xy = 4*x + 4*y*h->b_stride;
  1881. const int b8_xy= 2*x + 2*y*h->b8_stride;
  1882. h->mb2b_xy [mb_xy]= b_xy;
  1883. h->mb2b8_xy[mb_xy]= b8_xy;
  1884. }
  1885. }
  1886. return 0;
  1887. fail:
  1888. free_tables(h);
  1889. return -1;
  1890. }
  1891. static void common_init(H264Context *h){
  1892. MpegEncContext * const s = &h->s;
  1893. s->width = s->avctx->width;
  1894. s->height = s->avctx->height;
  1895. s->codec_id= s->avctx->codec->id;
  1896. init_pred_ptrs(h);
  1897. s->unrestricted_mv=1;
  1898. s->decode=1; //FIXME
  1899. }
  1900. static int decode_init(AVCodecContext *avctx){
  1901. H264Context *h= avctx->priv_data;
  1902. MpegEncContext * const s = &h->s;
  1903. MPV_decode_defaults(s);
  1904. s->avctx = avctx;
  1905. common_init(h);
  1906. s->out_format = FMT_H264;
  1907. s->workaround_bugs= avctx->workaround_bugs;
  1908. // set defaults
  1909. // s->decode_mb= ff_h263_decode_mb;
  1910. s->low_delay= 1;
  1911. avctx->pix_fmt= PIX_FMT_YUV420P;
  1912. decode_init_vlc(h);
  1913. return 0;
  1914. }
  1915. static void frame_start(H264Context *h){
  1916. MpegEncContext * const s = &h->s;
  1917. int i;
  1918. MPV_frame_start(s, s->avctx);
  1919. ff_er_frame_start(s);
  1920. h->mmco_index=0;
  1921. assert(s->linesize && s->uvlinesize);
  1922. for(i=0; i<16; i++){
  1923. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  1924. h->chroma_subblock_offset[i]= 2*((scan8[i] - scan8[0])&7) + 2*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  1925. }
  1926. for(i=0; i<4; i++){
  1927. h->block_offset[16+i]=
  1928. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  1929. }
  1930. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  1931. }
  1932. static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  1933. MpegEncContext * const s = &h->s;
  1934. int i;
  1935. src_y -= linesize;
  1936. src_cb -= uvlinesize;
  1937. src_cr -= uvlinesize;
  1938. h->left_border[0]= h->top_border[s->mb_x][15];
  1939. for(i=1; i<17; i++){
  1940. h->left_border[i]= src_y[15+i* linesize];
  1941. }
  1942. *(uint64_t*)(h->top_border[s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
  1943. *(uint64_t*)(h->top_border[s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
  1944. if(!(s->flags&CODEC_FLAG_GRAY)){
  1945. h->left_border[17 ]= h->top_border[s->mb_x][16+7];
  1946. h->left_border[17+9]= h->top_border[s->mb_x][24+7];
  1947. for(i=1; i<9; i++){
  1948. h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
  1949. h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
  1950. }
  1951. *(uint64_t*)(h->top_border[s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
  1952. *(uint64_t*)(h->top_border[s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
  1953. }
  1954. }
  1955. 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){
  1956. MpegEncContext * const s = &h->s;
  1957. int temp8, i;
  1958. uint64_t temp64;
  1959. src_y -= linesize + 1;
  1960. src_cb -= uvlinesize + 1;
  1961. src_cr -= uvlinesize + 1;
  1962. #define XCHG(a,b,t,xchg)\
  1963. t= a;\
  1964. if(xchg)\
  1965. a= b;\
  1966. b= t;
  1967. for(i=0; i<17; i++){
  1968. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  1969. }
  1970. XCHG(*(uint64_t*)(h->top_border[s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  1971. XCHG(*(uint64_t*)(h->top_border[s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  1972. if(!(s->flags&CODEC_FLAG_GRAY)){
  1973. for(i=0; i<9; i++){
  1974. XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
  1975. XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
  1976. }
  1977. XCHG(*(uint64_t*)(h->top_border[s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  1978. XCHG(*(uint64_t*)(h->top_border[s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  1979. }
  1980. }
  1981. static void hl_decode_mb(H264Context *h){
  1982. MpegEncContext * const s = &h->s;
  1983. const int mb_x= s->mb_x;
  1984. const int mb_y= s->mb_y;
  1985. const int mb_xy= mb_x + mb_y*s->mb_stride;
  1986. const int mb_type= s->current_picture.mb_type[mb_xy];
  1987. uint8_t *dest_y, *dest_cb, *dest_cr;
  1988. int linesize, uvlinesize /*dct_offset*/;
  1989. int i;
  1990. if(!s->decode)
  1991. return;
  1992. if(s->mb_skiped){
  1993. }
  1994. dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  1995. dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  1996. dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  1997. if (h->mb_field_decoding_flag) {
  1998. linesize = s->linesize * 2;
  1999. uvlinesize = s->uvlinesize * 2;
  2000. if(mb_y&1){ //FIXME move out of this func?
  2001. dest_y -= s->linesize*15;
  2002. dest_cb-= s->linesize*7;
  2003. dest_cr-= s->linesize*7;
  2004. }
  2005. } else {
  2006. linesize = s->linesize;
  2007. uvlinesize = s->uvlinesize;
  2008. // dct_offset = s->linesize * 16;
  2009. }
  2010. if(IS_INTRA(mb_type)){
  2011. if(h->deblocking_filter)
  2012. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1);
  2013. if(!(s->flags&CODEC_FLAG_GRAY)){
  2014. h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  2015. h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  2016. }
  2017. if(IS_INTRA4x4(mb_type)){
  2018. if(!s->encoding){
  2019. for(i=0; i<16; i++){
  2020. uint8_t * const ptr= dest_y + h->block_offset[i];
  2021. uint8_t *topright= ptr + 4 - linesize;
  2022. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  2023. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  2024. int tr;
  2025. if(!topright_avail){
  2026. tr= ptr[3 - linesize]*0x01010101;
  2027. topright= (uint8_t*) &tr;
  2028. }else if(i==5 && h->deblocking_filter){
  2029. tr= *(uint32_t*)h->top_border[mb_x+1];
  2030. topright= (uint8_t*) &tr;
  2031. }
  2032. h->pred4x4[ dir ](ptr, topright, linesize);
  2033. if(h->non_zero_count_cache[ scan8[i] ]){
  2034. if(s->codec_id == CODEC_ID_H264)
  2035. h264_add_idct_c(ptr, h->mb + i*16, linesize);
  2036. else
  2037. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  2038. }
  2039. }
  2040. }
  2041. }else{
  2042. h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  2043. if(s->codec_id == CODEC_ID_H264)
  2044. h264_luma_dc_dequant_idct_c(h->mb, s->qscale);
  2045. else
  2046. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  2047. }
  2048. if(h->deblocking_filter)
  2049. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
  2050. }else if(s->codec_id == CODEC_ID_H264){
  2051. hl_motion(h, dest_y, dest_cb, dest_cr,
  2052. s->dsp.put_h264_qpel_pixels_tab, s->dsp.put_h264_chroma_pixels_tab,
  2053. s->dsp.avg_h264_qpel_pixels_tab, s->dsp.avg_h264_chroma_pixels_tab);
  2054. }
  2055. if(!IS_INTRA4x4(mb_type)){
  2056. if(s->codec_id == CODEC_ID_H264){
  2057. for(i=0; i<16; i++){
  2058. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  2059. uint8_t * const ptr= dest_y + h->block_offset[i];
  2060. h264_add_idct_c(ptr, h->mb + i*16, linesize);
  2061. }
  2062. }
  2063. }else{
  2064. for(i=0; i<16; i++){
  2065. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  2066. uint8_t * const ptr= dest_y + h->block_offset[i];
  2067. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  2068. }
  2069. }
  2070. }
  2071. }
  2072. if(!(s->flags&CODEC_FLAG_GRAY)){
  2073. chroma_dc_dequant_idct_c(h->mb + 16*16, h->chroma_qp);
  2074. chroma_dc_dequant_idct_c(h->mb + 16*16+4*16, h->chroma_qp);
  2075. if(s->codec_id == CODEC_ID_H264){
  2076. for(i=16; i<16+4; i++){
  2077. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  2078. uint8_t * const ptr= dest_cb + h->block_offset[i];
  2079. h264_add_idct_c(ptr, h->mb + i*16, uvlinesize);
  2080. }
  2081. }
  2082. for(i=20; i<20+4; i++){
  2083. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  2084. uint8_t * const ptr= dest_cr + h->block_offset[i];
  2085. h264_add_idct_c(ptr, h->mb + i*16, uvlinesize);
  2086. }
  2087. }
  2088. }else{
  2089. for(i=16; i<16+4; i++){
  2090. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  2091. uint8_t * const ptr= dest_cb + h->block_offset[i];
  2092. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  2093. }
  2094. }
  2095. for(i=20; i<20+4; i++){
  2096. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  2097. uint8_t * const ptr= dest_cr + h->block_offset[i];
  2098. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  2099. }
  2100. }
  2101. }
  2102. }
  2103. if(h->deblocking_filter) {
  2104. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  2105. filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr);
  2106. }
  2107. }
  2108. /**
  2109. * fills the default_ref_list.
  2110. */
  2111. static int fill_default_ref_list(H264Context *h){
  2112. MpegEncContext * const s = &h->s;
  2113. int i;
  2114. Picture sorted_short_ref[16];
  2115. if(h->slice_type==B_TYPE){
  2116. int out_i;
  2117. int limit= -1;
  2118. for(out_i=0; out_i<h->short_ref_count; out_i++){
  2119. int best_i=-1;
  2120. int best_poc=-1;
  2121. for(i=0; i<h->short_ref_count; i++){
  2122. const int poc= h->short_ref[i]->poc;
  2123. if(poc > limit && poc < best_poc){
  2124. best_poc= poc;
  2125. best_i= i;
  2126. }
  2127. }
  2128. assert(best_i != -1);
  2129. limit= best_poc;
  2130. sorted_short_ref[out_i]= *h->short_ref[best_i];
  2131. }
  2132. }
  2133. if(s->picture_structure == PICT_FRAME){
  2134. if(h->slice_type==B_TYPE){
  2135. const int current_poc= s->current_picture_ptr->poc;
  2136. int list;
  2137. for(list=0; list<2; list++){
  2138. int index=0;
  2139. for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++){
  2140. const int i2= list ? h->short_ref_count - i - 1 : i;
  2141. const int poc= sorted_short_ref[i2].poc;
  2142. if(sorted_short_ref[i2].reference != 3) continue; //FIXME refernce field shit
  2143. if((list==1 && poc > current_poc) || (list==0 && poc < current_poc)){
  2144. h->default_ref_list[list][index ]= sorted_short_ref[i2];
  2145. h->default_ref_list[list][index++].pic_id= sorted_short_ref[i2].frame_num;
  2146. }
  2147. }
  2148. for(i=0; i<h->long_ref_count && index < h->ref_count[ list ]; i++){
  2149. if(h->long_ref[i]->reference != 3) continue;
  2150. h->default_ref_list[ list ][index ]= *h->long_ref[i];
  2151. h->default_ref_list[ list ][index++].pic_id= i;;
  2152. }
  2153. if(h->long_ref_count > 1 && h->short_ref_count==0){
  2154. Picture temp= h->default_ref_list[1][0];
  2155. h->default_ref_list[1][0] = h->default_ref_list[1][1];
  2156. h->default_ref_list[1][0] = temp;
  2157. }
  2158. if(index < h->ref_count[ list ])
  2159. memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
  2160. }
  2161. }else{
  2162. int index=0;
  2163. for(i=0; i<h->short_ref_count && index < h->ref_count[0]; i++){
  2164. if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
  2165. h->default_ref_list[0][index ]= *h->short_ref[i];
  2166. h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
  2167. }
  2168. for(i=0; i<h->long_ref_count && index < h->ref_count[0]; i++){
  2169. if(h->long_ref[i]->reference != 3) continue;
  2170. h->default_ref_list[0][index ]= *h->long_ref[i];
  2171. h->default_ref_list[0][index++].pic_id= i;;
  2172. }
  2173. if(index < h->ref_count[0])
  2174. memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
  2175. }
  2176. }else{ //FIELD
  2177. if(h->slice_type==B_TYPE){
  2178. }else{
  2179. //FIXME second field balh
  2180. }
  2181. }
  2182. return 0;
  2183. }
  2184. static int decode_ref_pic_list_reordering(H264Context *h){
  2185. MpegEncContext * const s = &h->s;
  2186. int list;
  2187. if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move beofre func
  2188. for(list=0; list<2; list++){
  2189. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  2190. if(get_bits1(&s->gb)){
  2191. int pred= h->curr_pic_num;
  2192. int index;
  2193. for(index=0; ; index++){
  2194. int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
  2195. int pic_id;
  2196. int i;
  2197. if(index >= h->ref_count[list]){
  2198. av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
  2199. return -1;
  2200. }
  2201. if(reordering_of_pic_nums_idc<3){
  2202. if(reordering_of_pic_nums_idc<2){
  2203. const int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  2204. if(abs_diff_pic_num >= h->max_pic_num){
  2205. av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  2206. return -1;
  2207. }
  2208. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  2209. else pred+= abs_diff_pic_num;
  2210. pred &= h->max_pic_num - 1;
  2211. for(i= h->ref_count[list]-1; i>=index; i--){
  2212. if(h->ref_list[list][i].pic_id == pred && h->ref_list[list][i].long_ref==0)
  2213. break;
  2214. }
  2215. }else{
  2216. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  2217. for(i= h->ref_count[list]-1; i>=index; i--){
  2218. if(h->ref_list[list][i].pic_id == pic_id && h->ref_list[list][i].long_ref==1)
  2219. break;
  2220. }
  2221. }
  2222. if(i < index){
  2223. av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  2224. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  2225. }else if(i > index){
  2226. Picture tmp= h->ref_list[list][i];
  2227. for(; i>index; i--){
  2228. h->ref_list[list][i]= h->ref_list[list][i-1];
  2229. }
  2230. h->ref_list[list][index]= tmp;
  2231. }
  2232. }else if(reordering_of_pic_nums_idc==3)
  2233. break;
  2234. else{
  2235. av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  2236. return -1;
  2237. }
  2238. }
  2239. }
  2240. if(h->slice_type!=B_TYPE) break;
  2241. }
  2242. return 0;
  2243. }
  2244. static int pred_weight_table(H264Context *h){
  2245. MpegEncContext * const s = &h->s;
  2246. int list, i;
  2247. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  2248. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  2249. for(list=0; list<2; list++){
  2250. for(i=0; i<h->ref_count[list]; i++){
  2251. int luma_weight_flag, chroma_weight_flag;
  2252. luma_weight_flag= get_bits1(&s->gb);
  2253. if(luma_weight_flag){
  2254. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  2255. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  2256. }
  2257. chroma_weight_flag= get_bits1(&s->gb);
  2258. if(chroma_weight_flag){
  2259. int j;
  2260. for(j=0; j<2; j++){
  2261. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  2262. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  2263. }
  2264. }
  2265. }
  2266. if(h->slice_type != B_TYPE) break;
  2267. }
  2268. return 0;
  2269. }
  2270. /**
  2271. * instantaneos decoder refresh.
  2272. */
  2273. static void idr(H264Context *h){
  2274. int i;
  2275. for(i=0; i<h->long_ref_count; i++){
  2276. h->long_ref[i]->reference=0;
  2277. h->long_ref[i]= NULL;
  2278. }
  2279. h->long_ref_count=0;
  2280. for(i=0; i<h->short_ref_count; i++){
  2281. h->short_ref[i]->reference=0;
  2282. h->short_ref[i]= NULL;
  2283. }
  2284. h->short_ref_count=0;
  2285. }
  2286. /**
  2287. *
  2288. * @return the removed picture or NULL if an error occures
  2289. */
  2290. static Picture * remove_short(H264Context *h, int frame_num){
  2291. MpegEncContext * const s = &h->s;
  2292. int i;
  2293. if(s->avctx->debug&FF_DEBUG_MMCO)
  2294. av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  2295. for(i=0; i<h->short_ref_count; i++){
  2296. Picture *pic= h->short_ref[i];
  2297. if(s->avctx->debug&FF_DEBUG_MMCO)
  2298. av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  2299. if(pic->frame_num == frame_num){
  2300. h->short_ref[i]= NULL;
  2301. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
  2302. h->short_ref_count--;
  2303. return pic;
  2304. }
  2305. }
  2306. return NULL;
  2307. }
  2308. /**
  2309. *
  2310. * @return the removed picture or NULL if an error occures
  2311. */
  2312. static Picture * remove_long(H264Context *h, int i){
  2313. Picture *pic;
  2314. if(i >= h->long_ref_count) return NULL;
  2315. pic= h->long_ref[i];
  2316. if(pic==NULL) return NULL;
  2317. h->long_ref[i]= NULL;
  2318. memmove(&h->long_ref[i], &h->long_ref[i+1], (h->long_ref_count - i - 1)*sizeof(Picture*));
  2319. h->long_ref_count--;
  2320. return pic;
  2321. }
  2322. /**
  2323. * Executes the reference picture marking (memory management control operations).
  2324. */
  2325. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  2326. MpegEncContext * const s = &h->s;
  2327. int i;
  2328. int current_is_long=0;
  2329. Picture *pic;
  2330. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  2331. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  2332. for(i=0; i<mmco_count; i++){
  2333. if(s->avctx->debug&FF_DEBUG_MMCO)
  2334. 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);
  2335. switch(mmco[i].opcode){
  2336. case MMCO_SHORT2UNUSED:
  2337. pic= remove_short(h, mmco[i].short_frame_num);
  2338. if(pic==NULL) return -1;
  2339. pic->reference= 0;
  2340. break;
  2341. case MMCO_SHORT2LONG:
  2342. pic= remove_long(h, mmco[i].long_index);
  2343. if(pic) pic->reference=0;
  2344. h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
  2345. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  2346. break;
  2347. case MMCO_LONG2UNUSED:
  2348. pic= remove_long(h, mmco[i].long_index);
  2349. if(pic==NULL) return -1;
  2350. pic->reference= 0;
  2351. break;
  2352. case MMCO_LONG:
  2353. pic= remove_long(h, mmco[i].long_index);
  2354. if(pic) pic->reference=0;
  2355. h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
  2356. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  2357. h->long_ref_count++;
  2358. current_is_long=1;
  2359. break;
  2360. case MMCO_SET_MAX_LONG:
  2361. assert(mmco[i].long_index <= 16);
  2362. while(mmco[i].long_index < h->long_ref_count){
  2363. pic= remove_long(h, mmco[i].long_index);
  2364. pic->reference=0;
  2365. }
  2366. while(mmco[i].long_index > h->long_ref_count){
  2367. h->long_ref[ h->long_ref_count++ ]= NULL;
  2368. }
  2369. break;
  2370. case MMCO_RESET:
  2371. while(h->short_ref_count){
  2372. pic= remove_short(h, h->short_ref[0]->frame_num);
  2373. pic->reference=0;
  2374. }
  2375. while(h->long_ref_count){
  2376. pic= remove_long(h, h->long_ref_count-1);
  2377. pic->reference=0;
  2378. }
  2379. break;
  2380. default: assert(0);
  2381. }
  2382. }
  2383. if(!current_is_long){
  2384. pic= remove_short(h, s->current_picture_ptr->frame_num);
  2385. if(pic){
  2386. pic->reference=0;
  2387. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  2388. }
  2389. if(h->short_ref_count)
  2390. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  2391. h->short_ref[0]= s->current_picture_ptr;
  2392. h->short_ref[0]->long_ref=0;
  2393. h->short_ref_count++;
  2394. }
  2395. return 0;
  2396. }
  2397. static int decode_ref_pic_marking(H264Context *h){
  2398. MpegEncContext * const s = &h->s;
  2399. int i;
  2400. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  2401. s->broken_link= get_bits1(&s->gb) -1;
  2402. h->mmco[0].long_index= get_bits1(&s->gb) - 1; // current_long_term_idx
  2403. if(h->mmco[0].long_index == -1)
  2404. h->mmco_index= 0;
  2405. else{
  2406. h->mmco[0].opcode= MMCO_LONG;
  2407. h->mmco_index= 1;
  2408. }
  2409. }else{
  2410. if(get_bits1(&s->gb)){ // adaptive_ref_pic_marking_mode_flag
  2411. for(i= h->mmco_index; i<MAX_MMCO_COUNT; i++) {
  2412. MMCOOpcode opcode= get_ue_golomb(&s->gb);;
  2413. h->mmco[i].opcode= opcode;
  2414. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  2415. 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
  2416. /* if(h->mmco[i].short_frame_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_frame_num ] == NULL){
  2417. fprintf(stderr, "illegal short ref in memory management control operation %d\n", mmco);
  2418. return -1;
  2419. }*/
  2420. }
  2421. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  2422. h->mmco[i].long_index= get_ue_golomb(&s->gb);
  2423. 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){
  2424. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  2425. return -1;
  2426. }
  2427. }
  2428. if(opcode > MMCO_LONG){
  2429. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  2430. return -1;
  2431. }
  2432. }
  2433. h->mmco_index= i;
  2434. }else{
  2435. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  2436. if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){ //FIXME fields
  2437. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  2438. h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  2439. h->mmco_index= 1;
  2440. }else
  2441. h->mmco_index= 0;
  2442. }
  2443. }
  2444. return 0;
  2445. }
  2446. static int init_poc(H264Context *h){
  2447. MpegEncContext * const s = &h->s;
  2448. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  2449. int field_poc[2];
  2450. if(h->nal_unit_type == NAL_IDR_SLICE){
  2451. h->frame_num_offset= 0;
  2452. }else{
  2453. if(h->frame_num < h->prev_frame_num)
  2454. h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
  2455. else
  2456. h->frame_num_offset= h->prev_frame_num_offset;
  2457. }
  2458. if(h->sps.poc_type==0){
  2459. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  2460. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  2461. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  2462. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  2463. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  2464. else
  2465. h->poc_msb = h->prev_poc_msb;
  2466. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  2467. field_poc[0] =
  2468. field_poc[1] = h->poc_msb + h->poc_lsb;
  2469. if(s->picture_structure == PICT_FRAME)
  2470. field_poc[1] += h->delta_poc_bottom;
  2471. }else if(h->sps.poc_type==1){
  2472. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  2473. int i;
  2474. if(h->sps.poc_cycle_length != 0)
  2475. abs_frame_num = h->frame_num_offset + h->frame_num;
  2476. else
  2477. abs_frame_num = 0;
  2478. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  2479. abs_frame_num--;
  2480. expected_delta_per_poc_cycle = 0;
  2481. for(i=0; i < h->sps.poc_cycle_length; i++)
  2482. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  2483. if(abs_frame_num > 0){
  2484. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  2485. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  2486. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  2487. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  2488. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  2489. } else
  2490. expectedpoc = 0;
  2491. if(h->nal_ref_idc == 0)
  2492. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  2493. field_poc[0] = expectedpoc + h->delta_poc[0];
  2494. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  2495. if(s->picture_structure == PICT_FRAME)
  2496. field_poc[1] += h->delta_poc[1];
  2497. }else{
  2498. int poc;
  2499. if(h->nal_unit_type == NAL_IDR_SLICE){
  2500. poc= 0;
  2501. }else{
  2502. if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
  2503. else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
  2504. }
  2505. field_poc[0]= poc;
  2506. field_poc[1]= poc;
  2507. }
  2508. if(s->picture_structure != PICT_BOTTOM_FIELD)
  2509. s->current_picture_ptr->field_poc[0]= field_poc[0];
  2510. if(s->picture_structure != PICT_TOP_FIELD)
  2511. s->current_picture_ptr->field_poc[1]= field_poc[1];
  2512. if(s->picture_structure == PICT_FRAME) // FIXME field pix?
  2513. s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]);
  2514. return 0;
  2515. }
  2516. /**
  2517. * decodes a slice header.
  2518. * this will allso call MPV_common_init() and frame_start() as needed
  2519. */
  2520. static int decode_slice_header(H264Context *h){
  2521. MpegEncContext * const s = &h->s;
  2522. int first_mb_in_slice, pps_id;
  2523. int num_ref_idx_active_override_flag;
  2524. static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
  2525. s->current_picture.reference= h->nal_ref_idc != 0;
  2526. first_mb_in_slice= get_ue_golomb(&s->gb);
  2527. h->slice_type= get_ue_golomb(&s->gb);
  2528. if(h->slice_type > 9){
  2529. 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);
  2530. }
  2531. if(h->slice_type > 4){
  2532. h->slice_type -= 5;
  2533. h->slice_type_fixed=1;
  2534. }else
  2535. h->slice_type_fixed=0;
  2536. h->slice_type= slice_type_map[ h->slice_type ];
  2537. s->pict_type= h->slice_type; // to make a few old func happy, its wrong though
  2538. pps_id= get_ue_golomb(&s->gb);
  2539. if(pps_id>255){
  2540. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  2541. return -1;
  2542. }
  2543. h->pps= h->pps_buffer[pps_id];
  2544. if(h->pps.slice_group_count == 0){
  2545. av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referenced\n");
  2546. return -1;
  2547. }
  2548. h->sps= h->sps_buffer[ h->pps.sps_id ];
  2549. if(h->sps.log2_max_frame_num == 0){
  2550. av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referenced\n");
  2551. return -1;
  2552. }
  2553. s->mb_width= h->sps.mb_width;
  2554. s->mb_height= h->sps.mb_height;
  2555. h->b_stride= s->mb_width*4;
  2556. h->b8_stride= s->mb_width*2;
  2557. s->mb_x = first_mb_in_slice % s->mb_width;
  2558. s->mb_y = first_mb_in_slice / s->mb_width; //FIXME AFFW
  2559. s->width = 16*s->mb_width - 2*(h->sps.crop_left + h->sps.crop_right );
  2560. if(h->sps.frame_mbs_only_flag)
  2561. s->height= 16*s->mb_height - 2*(h->sps.crop_top + h->sps.crop_bottom);
  2562. else
  2563. s->height= 16*s->mb_height - 4*(h->sps.crop_top + h->sps.crop_bottom); //FIXME recheck
  2564. if (s->context_initialized
  2565. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  2566. free_tables(h);
  2567. MPV_common_end(s);
  2568. }
  2569. if (!s->context_initialized) {
  2570. if (MPV_common_init(s) < 0)
  2571. return -1;
  2572. alloc_tables(h);
  2573. s->avctx->width = s->width;
  2574. s->avctx->height = s->height;
  2575. s->avctx->sample_aspect_ratio= h->sps.sar;
  2576. }
  2577. if(first_mb_in_slice == 0){
  2578. frame_start(h);
  2579. }
  2580. s->current_picture_ptr->frame_num= //FIXME frame_num cleanup
  2581. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  2582. if(h->sps.frame_mbs_only_flag){
  2583. s->picture_structure= PICT_FRAME;
  2584. }else{
  2585. if(get_bits1(&s->gb)) //field_pic_flag
  2586. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  2587. else
  2588. s->picture_structure= PICT_FRAME;
  2589. }
  2590. if(s->picture_structure==PICT_FRAME){
  2591. h->curr_pic_num= h->frame_num;
  2592. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  2593. }else{
  2594. h->curr_pic_num= 2*h->frame_num;
  2595. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  2596. }
  2597. if(h->nal_unit_type == NAL_IDR_SLICE){
  2598. get_ue_golomb(&s->gb); /* idr_pic_id */
  2599. }
  2600. if(h->sps.poc_type==0){
  2601. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  2602. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  2603. h->delta_poc_bottom= get_se_golomb(&s->gb);
  2604. }
  2605. }
  2606. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  2607. h->delta_poc[0]= get_se_golomb(&s->gb);
  2608. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  2609. h->delta_poc[1]= get_se_golomb(&s->gb);
  2610. }
  2611. init_poc(h);
  2612. if(h->pps.redundant_pic_cnt_present){
  2613. h->redundant_pic_count= get_ue_golomb(&s->gb);
  2614. }
  2615. //set defaults, might be overriden a few line later
  2616. h->ref_count[0]= h->pps.ref_count[0];
  2617. h->ref_count[1]= h->pps.ref_count[1];
  2618. if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){
  2619. if(h->slice_type == B_TYPE){
  2620. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  2621. }
  2622. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  2623. if(num_ref_idx_active_override_flag){
  2624. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  2625. if(h->slice_type==B_TYPE)
  2626. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  2627. if(h->ref_count[0] > 32 || h->ref_count[1] > 32){
  2628. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  2629. return -1;
  2630. }
  2631. }
  2632. }
  2633. if(first_mb_in_slice == 0){
  2634. fill_default_ref_list(h);
  2635. }
  2636. decode_ref_pic_list_reordering(h);
  2637. if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
  2638. || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
  2639. pred_weight_table(h);
  2640. if(s->current_picture.reference)
  2641. decode_ref_pic_marking(h);
  2642. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac )
  2643. h->cabac_init_idc = get_ue_golomb(&s->gb);
  2644. h->last_qscale_diff = 0;
  2645. s->qscale = h->pps.init_qp + get_se_golomb(&s->gb);
  2646. if(s->qscale<0 || s->qscale>51){
  2647. av_log(s->avctx, AV_LOG_ERROR, "QP %d out of range\n", s->qscale);
  2648. return -1;
  2649. }
  2650. //FIXME qscale / qp ... stuff
  2651. if(h->slice_type == SP_TYPE){
  2652. get_bits1(&s->gb); /* sp_for_switch_flag */
  2653. }
  2654. if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
  2655. get_se_golomb(&s->gb); /* slice_qs_delta */
  2656. }
  2657. h->deblocking_filter = 1;
  2658. h->slice_alpha_c0_offset = 0;
  2659. h->slice_beta_offset = 0;
  2660. if( h->pps.deblocking_filter_parameters_present ) {
  2661. h->deblocking_filter= get_ue_golomb(&s->gb);
  2662. if(h->deblocking_filter < 2)
  2663. h->deblocking_filter^= 1; // 1<->0
  2664. if( h->deblocking_filter ) {
  2665. h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
  2666. h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
  2667. }
  2668. }
  2669. #if 0 //FMO
  2670. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  2671. slice_group_change_cycle= get_bits(&s->gb, ?);
  2672. #endif
  2673. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  2674. 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",
  2675. first_mb_in_slice,
  2676. av_get_pict_type_char(h->slice_type),
  2677. pps_id, h->frame_num,
  2678. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  2679. h->ref_count[0], h->ref_count[1],
  2680. s->qscale,
  2681. h->deblocking_filter
  2682. );
  2683. }
  2684. return 0;
  2685. }
  2686. /**
  2687. *
  2688. */
  2689. static inline int get_level_prefix(GetBitContext *gb){
  2690. unsigned int buf;
  2691. int log;
  2692. OPEN_READER(re, gb);
  2693. UPDATE_CACHE(re, gb);
  2694. buf=GET_CACHE(re, gb);
  2695. log= 32 - av_log2(buf);
  2696. #ifdef TRACE
  2697. print_bin(buf>>(32-log), log);
  2698. printf("%5d %2d %3d lpr @%5d in %s get_level_prefix\n", buf>>(32-log), log, log-1, get_bits_count(gb), __FILE__);
  2699. #endif
  2700. LAST_SKIP_BITS(re, gb, log);
  2701. CLOSE_READER(re, gb);
  2702. return log-1;
  2703. }
  2704. /**
  2705. * decodes a residual block.
  2706. * @param n block index
  2707. * @param scantable scantable
  2708. * @param max_coeff number of coefficients in the block
  2709. * @return <0 if an error occured
  2710. */
  2711. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, int qp, int max_coeff){
  2712. MpegEncContext * const s = &h->s;
  2713. const uint16_t *qmul= dequant_coeff[qp];
  2714. 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};
  2715. int level[16], run[16];
  2716. int suffix_length, zeros_left, coeff_num, coeff_token, total_coeff, i, trailing_ones;
  2717. //FIXME put trailing_onex into the context
  2718. if(n == CHROMA_DC_BLOCK_INDEX){
  2719. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  2720. total_coeff= coeff_token>>2;
  2721. }else{
  2722. if(n == LUMA_DC_BLOCK_INDEX){
  2723. total_coeff= pred_non_zero_count(h, 0);
  2724. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  2725. total_coeff= coeff_token>>2;
  2726. }else{
  2727. total_coeff= pred_non_zero_count(h, n);
  2728. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  2729. total_coeff= coeff_token>>2;
  2730. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  2731. }
  2732. }
  2733. //FIXME set last_non_zero?
  2734. if(total_coeff==0)
  2735. return 0;
  2736. trailing_ones= coeff_token&3;
  2737. tprintf("trailing:%d, total:%d\n", trailing_ones, total_coeff);
  2738. assert(total_coeff<=16);
  2739. for(i=0; i<trailing_ones; i++){
  2740. level[i]= 1 - 2*get_bits1(gb);
  2741. }
  2742. suffix_length= total_coeff > 10 && trailing_ones < 3;
  2743. for(; i<total_coeff; i++){
  2744. const int prefix= get_level_prefix(gb);
  2745. int level_code, mask;
  2746. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  2747. if(suffix_length)
  2748. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  2749. else
  2750. level_code= (prefix<<suffix_length); //part
  2751. }else if(prefix==14){
  2752. if(suffix_length)
  2753. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  2754. else
  2755. level_code= prefix + get_bits(gb, 4); //part
  2756. }else if(prefix==15){
  2757. level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part
  2758. if(suffix_length==0) level_code+=15; //FIXME doesnt make (much)sense
  2759. }else{
  2760. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  2761. return -1;
  2762. }
  2763. if(i==trailing_ones && i<3) level_code+= 2; //FIXME split first iteration
  2764. mask= -(level_code&1);
  2765. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  2766. if(suffix_length==0) suffix_length=1; //FIXME split first iteration
  2767. #if 1
  2768. if(ABS(level[i]) > (3<<(suffix_length-1)) && suffix_length<6) suffix_length++;
  2769. #else
  2770. if((2+level_code)>>1) > (3<<(suffix_length-1)) && suffix_length<6) suffix_length++;
  2771. /* ? == prefix > 2 or sth */
  2772. #endif
  2773. tprintf("level: %d suffix_length:%d\n", level[i], suffix_length);
  2774. }
  2775. if(total_coeff == max_coeff)
  2776. zeros_left=0;
  2777. else{
  2778. if(n == CHROMA_DC_BLOCK_INDEX)
  2779. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  2780. else
  2781. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  2782. }
  2783. for(i=0; i<total_coeff-1; i++){
  2784. if(zeros_left <=0)
  2785. break;
  2786. else if(zeros_left < 7){
  2787. run[i]= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  2788. }else{
  2789. run[i]= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  2790. }
  2791. zeros_left -= run[i];
  2792. }
  2793. if(zeros_left<0){
  2794. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  2795. return -1;
  2796. }
  2797. for(; i<total_coeff-1; i++){
  2798. run[i]= 0;
  2799. }
  2800. run[i]= zeros_left;
  2801. coeff_num=-1;
  2802. if(n > 24){
  2803. for(i=total_coeff-1; i>=0; i--){ //FIXME merge into rundecode?
  2804. int j;
  2805. coeff_num += run[i] + 1; //FIXME add 1 earlier ?
  2806. j= scantable[ coeff_num ];
  2807. block[j]= level[i];
  2808. }
  2809. }else{
  2810. for(i=total_coeff-1; i>=0; i--){ //FIXME merge into rundecode?
  2811. int j;
  2812. coeff_num += run[i] + 1; //FIXME add 1 earlier ?
  2813. j= scantable[ coeff_num ];
  2814. block[j]= level[i] * qmul[j];
  2815. // printf("%d %d ", block[j], qmul[j]);
  2816. }
  2817. }
  2818. return 0;
  2819. }
  2820. /**
  2821. * decodes a macroblock
  2822. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  2823. */
  2824. static int decode_mb_cavlc(H264Context *h){
  2825. MpegEncContext * const s = &h->s;
  2826. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2827. int mb_type, partition_count, cbp;
  2828. s->dsp.clear_blocks(h->mb); //FIXME avoid if allready clear (move after skip handlong?
  2829. tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  2830. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  2831. down the code */
  2832. if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){
  2833. if(s->mb_skip_run==-1)
  2834. s->mb_skip_run= get_ue_golomb(&s->gb);
  2835. if (s->mb_skip_run--) {
  2836. int mx, my;
  2837. /* skip mb */
  2838. //FIXME b frame
  2839. mb_type= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0;
  2840. memset(h->non_zero_count[mb_xy], 0, 16);
  2841. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  2842. if(h->sps.mb_aff && s->mb_skip_run==0 && (s->mb_y&1)==0){
  2843. h->mb_field_decoding_flag= get_bits1(&s->gb);
  2844. }
  2845. if(h->mb_field_decoding_flag)
  2846. mb_type|= MB_TYPE_INTERLACED;
  2847. fill_caches(h, mb_type); //FIXME check what is needed and what not ...
  2848. pred_pskip_motion(h, &mx, &my);
  2849. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  2850. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  2851. write_back_motion(h, mb_type);
  2852. s->current_picture.mb_type[mb_xy]= mb_type; //FIXME SKIP type
  2853. s->current_picture.qscale_table[mb_xy]= s->qscale;
  2854. h->slice_table[ mb_xy ]= h->slice_num;
  2855. h->prev_mb_skiped= 1;
  2856. return 0;
  2857. }
  2858. }
  2859. if(h->sps.mb_aff /* && !field pic FIXME needed? */){
  2860. if((s->mb_y&1)==0)
  2861. h->mb_field_decoding_flag = get_bits1(&s->gb);
  2862. }else
  2863. h->mb_field_decoding_flag=0; //FIXME som ed note ?!
  2864. h->prev_mb_skiped= 0;
  2865. mb_type= get_ue_golomb(&s->gb);
  2866. if(h->slice_type == B_TYPE){
  2867. if(mb_type < 23){
  2868. partition_count= b_mb_type_info[mb_type].partition_count;
  2869. mb_type= b_mb_type_info[mb_type].type;
  2870. }else{
  2871. mb_type -= 23;
  2872. goto decode_intra_mb;
  2873. }
  2874. }else if(h->slice_type == P_TYPE /*|| h->slice_type == SP_TYPE */){
  2875. if(mb_type < 5){
  2876. partition_count= p_mb_type_info[mb_type].partition_count;
  2877. mb_type= p_mb_type_info[mb_type].type;
  2878. }else{
  2879. mb_type -= 5;
  2880. goto decode_intra_mb;
  2881. }
  2882. }else{
  2883. assert(h->slice_type == I_TYPE);
  2884. decode_intra_mb:
  2885. if(mb_type > 25){
  2886. 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);
  2887. return -1;
  2888. }
  2889. partition_count=0;
  2890. cbp= i_mb_type_info[mb_type].cbp;
  2891. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  2892. mb_type= i_mb_type_info[mb_type].type;
  2893. }
  2894. if(h->mb_field_decoding_flag)
  2895. mb_type |= MB_TYPE_INTERLACED;
  2896. s->current_picture.mb_type[mb_xy]= mb_type;
  2897. h->slice_table[ mb_xy ]= h->slice_num;
  2898. if(IS_INTRA_PCM(mb_type)){
  2899. const uint8_t *ptr;
  2900. int x, y;
  2901. // we assume these blocks are very rare so we dont optimize it
  2902. align_get_bits(&s->gb);
  2903. ptr= s->gb.buffer + get_bits_count(&s->gb);
  2904. for(y=0; y<16; y++){
  2905. const int index= 4*(y&3) + 64*(y>>2);
  2906. for(x=0; x<16; x++){
  2907. h->mb[index + (x&3) + 16*(x>>2)]= *(ptr++);
  2908. }
  2909. }
  2910. for(y=0; y<8; y++){
  2911. const int index= 256 + 4*(y&3) + 32*(y>>2);
  2912. for(x=0; x<8; x++){
  2913. h->mb[index + (x&3) + 16*(x>>2)]= *(ptr++);
  2914. }
  2915. }
  2916. for(y=0; y<8; y++){
  2917. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  2918. for(x=0; x<8; x++){
  2919. h->mb[index + (x&3) + 16*(x>>2)]= *(ptr++);
  2920. }
  2921. }
  2922. skip_bits(&s->gb, 384); //FIXME check /fix the bitstream readers
  2923. //FIXME deblock filter, non_zero_count_cache init ...
  2924. memset(h->non_zero_count[mb_xy], 16, 16);
  2925. s->current_picture.qscale_table[mb_xy]= s->qscale;
  2926. return 0;
  2927. }
  2928. fill_caches(h, mb_type);
  2929. //mb_pred
  2930. if(IS_INTRA(mb_type)){
  2931. // init_top_left_availability(h);
  2932. if(IS_INTRA4x4(mb_type)){
  2933. int i;
  2934. // fill_intra4x4_pred_table(h);
  2935. for(i=0; i<16; i++){
  2936. const int mode_coded= !get_bits1(&s->gb);
  2937. const int predicted_mode= pred_intra_mode(h, i);
  2938. int mode;
  2939. if(mode_coded){
  2940. const int rem_mode= get_bits(&s->gb, 3);
  2941. if(rem_mode<predicted_mode)
  2942. mode= rem_mode;
  2943. else
  2944. mode= rem_mode + 1;
  2945. }else{
  2946. mode= predicted_mode;
  2947. }
  2948. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  2949. }
  2950. write_back_intra_pred_mode(h);
  2951. if( check_intra4x4_pred_mode(h) < 0)
  2952. return -1;
  2953. }else{
  2954. h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
  2955. if(h->intra16x16_pred_mode < 0)
  2956. return -1;
  2957. }
  2958. h->chroma_pred_mode= get_ue_golomb(&s->gb);
  2959. h->chroma_pred_mode= check_intra_pred_mode(h, h->chroma_pred_mode);
  2960. if(h->chroma_pred_mode < 0)
  2961. return -1;
  2962. }else if(partition_count==4){
  2963. int i, j, sub_partition_count[4], list, ref[2][4];
  2964. if(h->slice_type == B_TYPE){
  2965. for(i=0; i<4; i++){
  2966. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  2967. if(h->sub_mb_type[i] >=13){
  2968. 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);
  2969. return -1;
  2970. }
  2971. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  2972. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  2973. }
  2974. }else{
  2975. assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE); //FIXME SP correct ?
  2976. for(i=0; i<4; i++){
  2977. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  2978. if(h->sub_mb_type[i] >=4){
  2979. 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);
  2980. return -1;
  2981. }
  2982. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  2983. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  2984. }
  2985. }
  2986. for(list=0; list<2; list++){
  2987. const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  2988. if(ref_count == 0) continue;
  2989. for(i=0; i<4; i++){
  2990. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  2991. ref[list][i] = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
  2992. }else{
  2993. //FIXME
  2994. ref[list][i] = -1;
  2995. }
  2996. }
  2997. }
  2998. for(list=0; list<2; list++){
  2999. const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  3000. if(ref_count == 0) continue;
  3001. for(i=0; i<4; i++){
  3002. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  3003. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  3004. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  3005. const int sub_mb_type= h->sub_mb_type[i];
  3006. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  3007. for(j=0; j<sub_partition_count[i]; j++){
  3008. int mx, my;
  3009. const int index= 4*i + block_width*j;
  3010. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  3011. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  3012. mx += get_se_golomb(&s->gb);
  3013. my += get_se_golomb(&s->gb);
  3014. tprintf("final mv:%d %d\n", mx, my);
  3015. if(IS_SUB_8X8(sub_mb_type)){
  3016. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]=
  3017. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  3018. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]=
  3019. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  3020. }else if(IS_SUB_8X4(sub_mb_type)){
  3021. mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx;
  3022. mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my;
  3023. }else if(IS_SUB_4X8(sub_mb_type)){
  3024. mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx;
  3025. mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my;
  3026. }else{
  3027. assert(IS_SUB_4X4(sub_mb_type));
  3028. mv_cache[ 0 ][0]= mx;
  3029. mv_cache[ 0 ][1]= my;
  3030. }
  3031. }
  3032. }else{
  3033. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  3034. p[0] = p[1]=
  3035. p[8] = p[9]= 0;
  3036. }
  3037. }
  3038. }
  3039. }else if(!IS_DIRECT(mb_type)){
  3040. int list, mx, my, i;
  3041. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  3042. if(IS_16X16(mb_type)){
  3043. for(list=0; list<2; list++){
  3044. if(h->ref_count[0]>0){
  3045. if(IS_DIR(mb_type, 0, list)){
  3046. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  3047. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  3048. }
  3049. }
  3050. }
  3051. for(list=0; list<2; list++){
  3052. if(IS_DIR(mb_type, 0, list)){
  3053. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  3054. mx += get_se_golomb(&s->gb);
  3055. my += get_se_golomb(&s->gb);
  3056. tprintf("final mv:%d %d\n", mx, my);
  3057. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  3058. }
  3059. }
  3060. }
  3061. else if(IS_16X8(mb_type)){
  3062. for(list=0; list<2; list++){
  3063. if(h->ref_count[list]>0){
  3064. for(i=0; i<2; i++){
  3065. if(IS_DIR(mb_type, i, list)){
  3066. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  3067. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  3068. }
  3069. }
  3070. }
  3071. }
  3072. for(list=0; list<2; list++){
  3073. for(i=0; i<2; i++){
  3074. if(IS_DIR(mb_type, i, list)){
  3075. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  3076. mx += get_se_golomb(&s->gb);
  3077. my += get_se_golomb(&s->gb);
  3078. tprintf("final mv:%d %d\n", mx, my);
  3079. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  3080. }
  3081. }
  3082. }
  3083. }else{
  3084. assert(IS_8X16(mb_type));
  3085. for(list=0; list<2; list++){
  3086. if(h->ref_count[list]>0){
  3087. for(i=0; i<2; i++){
  3088. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  3089. const int val= get_te0_golomb(&s->gb, h->ref_count[list]);
  3090. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  3091. }
  3092. }
  3093. }
  3094. }
  3095. for(list=0; list<2; list++){
  3096. for(i=0; i<2; i++){
  3097. if(IS_DIR(mb_type, i, list)){
  3098. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  3099. mx += get_se_golomb(&s->gb);
  3100. my += get_se_golomb(&s->gb);
  3101. tprintf("final mv:%d %d\n", mx, my);
  3102. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  3103. }
  3104. }
  3105. }
  3106. }
  3107. }
  3108. if(IS_INTER(mb_type))
  3109. write_back_motion(h, mb_type);
  3110. if(!IS_INTRA16x16(mb_type)){
  3111. cbp= get_ue_golomb(&s->gb);
  3112. if(cbp > 47){
  3113. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%d) at %d %d\n", cbp, s->mb_x, s->mb_y);
  3114. return -1;
  3115. }
  3116. if(IS_INTRA4x4(mb_type))
  3117. cbp= golomb_to_intra4x4_cbp[cbp];
  3118. else
  3119. cbp= golomb_to_inter_cbp[cbp];
  3120. }
  3121. if(cbp || IS_INTRA16x16(mb_type)){
  3122. int i8x8, i4x4, chroma_idx;
  3123. int chroma_qp, dquant;
  3124. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  3125. const uint8_t *scan, *dc_scan;
  3126. // fill_non_zero_count_cache(h);
  3127. if(IS_INTERLACED(mb_type)){
  3128. scan= field_scan;
  3129. dc_scan= luma_dc_field_scan;
  3130. }else{
  3131. scan= zigzag_scan;
  3132. dc_scan= luma_dc_zigzag_scan;
  3133. }
  3134. dquant= get_se_golomb(&s->gb);
  3135. if( dquant > 25 || dquant < -26 ){
  3136. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  3137. return -1;
  3138. }
  3139. s->qscale += dquant;
  3140. if(((unsigned)s->qscale) > 51){
  3141. if(s->qscale<0) s->qscale+= 52;
  3142. else s->qscale-= 52;
  3143. }
  3144. h->chroma_qp= chroma_qp= get_chroma_qp(h, s->qscale);
  3145. if(IS_INTRA16x16(mb_type)){
  3146. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, s->qscale, 16) < 0){
  3147. return -1; //FIXME continue if partotioned and other retirn -1 too
  3148. }
  3149. assert((cbp&15) == 0 || (cbp&15) == 15);
  3150. if(cbp&15){
  3151. for(i8x8=0; i8x8<4; i8x8++){
  3152. for(i4x4=0; i4x4<4; i4x4++){
  3153. const int index= i4x4 + 4*i8x8;
  3154. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, s->qscale, 15) < 0 ){
  3155. return -1;
  3156. }
  3157. }
  3158. }
  3159. }else{
  3160. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  3161. }
  3162. }else{
  3163. for(i8x8=0; i8x8<4; i8x8++){
  3164. if(cbp & (1<<i8x8)){
  3165. for(i4x4=0; i4x4<4; i4x4++){
  3166. const int index= i4x4 + 4*i8x8;
  3167. if( decode_residual(h, gb, h->mb + 16*index, index, scan, s->qscale, 16) <0 ){
  3168. return -1;
  3169. }
  3170. }
  3171. }else{
  3172. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  3173. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  3174. }
  3175. }
  3176. }
  3177. if(cbp&0x30){
  3178. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  3179. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, chroma_qp, 4) < 0){
  3180. return -1;
  3181. }
  3182. }
  3183. if(cbp&0x20){
  3184. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  3185. for(i4x4=0; i4x4<4; i4x4++){
  3186. const int index= 16 + 4*chroma_idx + i4x4;
  3187. if( decode_residual(h, gb, h->mb + 16*index, index, scan + 1, chroma_qp, 15) < 0){
  3188. return -1;
  3189. }
  3190. }
  3191. }
  3192. }else{
  3193. uint8_t * const nnz= &h->non_zero_count_cache[0];
  3194. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  3195. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  3196. }
  3197. }else{
  3198. uint8_t * const nnz= &h->non_zero_count_cache[0];
  3199. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  3200. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  3201. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  3202. }
  3203. s->current_picture.qscale_table[mb_xy]= s->qscale;
  3204. write_back_non_zero_count(h);
  3205. return 0;
  3206. }
  3207. static int decode_cabac_mb_type( H264Context *h ) {
  3208. MpegEncContext * const s = &h->s;
  3209. if( h->slice_type == I_TYPE ) {
  3210. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  3211. int ctx = 0;
  3212. int mb_type;
  3213. if( s->mb_x > 0 && !IS_INTRA4x4( s->current_picture.mb_type[mb_xy-1] ) )
  3214. ctx++;
  3215. if( s->mb_y > 0 && !IS_INTRA4x4( s->current_picture.mb_type[mb_xy-s->mb_stride] ) )
  3216. ctx++;
  3217. if( get_cabac( &h->cabac, &h->cabac_state[3+ctx] ) == 0 )
  3218. return 0; /* I4x4 */
  3219. if( get_cabac_terminate( &h->cabac ) )
  3220. return 25; /* PCM */
  3221. mb_type = 1; /* I16x16 */
  3222. if( get_cabac( &h->cabac, &h->cabac_state[3+3] ) )
  3223. mb_type += 12; /* cbp_luma != 0 */
  3224. if( get_cabac( &h->cabac, &h->cabac_state[3+4] ) ) {
  3225. if( get_cabac( &h->cabac, &h->cabac_state[3+5] ) )
  3226. mb_type += 4 * 2; /* cbp_chroma == 2 */
  3227. else
  3228. mb_type += 4 * 1; /* cbp_chroma == 1 */
  3229. }
  3230. if( get_cabac( &h->cabac, &h->cabac_state[3+6] ) )
  3231. mb_type += 2;
  3232. if( get_cabac( &h->cabac, &h->cabac_state[3+7] ) )
  3233. mb_type += 1;
  3234. return mb_type;
  3235. } else if( h->slice_type == P_TYPE ) {
  3236. if( get_cabac( &h->cabac, &h->cabac_state[14] ) == 0 ) {
  3237. /* P-type */
  3238. if( get_cabac( &h->cabac, &h->cabac_state[15] ) == 0 ) {
  3239. if( get_cabac( &h->cabac, &h->cabac_state[16] ) == 0 )
  3240. return 0; /* P_L0_D16x16; */
  3241. else
  3242. return 3; /* P_8x8; */
  3243. } else {
  3244. if( get_cabac( &h->cabac, &h->cabac_state[17] ) == 0 )
  3245. return 1; /* P_L0_D16x8; */
  3246. else
  3247. return 2; /* P_L0_D8x16; */
  3248. }
  3249. } else {
  3250. int mb_type;
  3251. /* I-type */
  3252. if( get_cabac( &h->cabac, &h->cabac_state[17] ) == 0 )
  3253. return 5+0; /* I_4x4 */
  3254. if( get_cabac_terminate( &h->cabac ) )
  3255. return 5+25; /*I_PCM */
  3256. mb_type = 5+1; /* I16x16 */
  3257. if( get_cabac( &h->cabac, &h->cabac_state[17+1] ) )
  3258. mb_type += 12; /* cbp_luma != 0 */
  3259. if( get_cabac( &h->cabac, &h->cabac_state[17+2] ) ) {
  3260. if( get_cabac( &h->cabac, &h->cabac_state[17+2] ) )
  3261. mb_type += 4 * 2; /* cbp_chroma == 2 */
  3262. else
  3263. mb_type += 4 * 1; /* cbp_chroma == 1 */
  3264. }
  3265. if( get_cabac( &h->cabac, &h->cabac_state[17+3] ) )
  3266. mb_type += 2;
  3267. if( get_cabac( &h->cabac, &h->cabac_state[17+3] ) )
  3268. mb_type += 1;
  3269. return mb_type;
  3270. }
  3271. } else {
  3272. /* TODO do others frames types */
  3273. return -1;
  3274. }
  3275. }
  3276. static int decode_cabac_mb_skip( H264Context *h) {
  3277. MpegEncContext * const s = &h->s;
  3278. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  3279. const int mba_xy = mb_xy - 1;
  3280. const int mbb_xy = mb_xy - s->mb_stride;
  3281. int ctx = 0;
  3282. if( s->mb_x > 0 && !IS_SKIP( s->current_picture.mb_type[mba_xy] ) )
  3283. ctx++;
  3284. if( s->mb_y > 0 && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ) )
  3285. ctx++;
  3286. if( h->slice_type == P_TYPE || h->slice_type == SP_TYPE)
  3287. return get_cabac( &h->cabac, &h->cabac_state[11+ctx] );
  3288. else /* B-frame */
  3289. return get_cabac( &h->cabac, &h->cabac_state[24+ctx] );
  3290. }
  3291. static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
  3292. int mode = 0;
  3293. if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
  3294. return pred_mode;
  3295. if( get_cabac( &h->cabac, &h->cabac_state[69] ) )
  3296. mode += 1;
  3297. if( get_cabac( &h->cabac, &h->cabac_state[69] ) )
  3298. mode += 2;
  3299. if( get_cabac( &h->cabac, &h->cabac_state[69] ) )
  3300. mode += 4;
  3301. if( mode >= pred_mode )
  3302. return mode + 1;
  3303. else
  3304. return mode;
  3305. }
  3306. static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
  3307. MpegEncContext * const s = &h->s;
  3308. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  3309. const int mba_xy = mb_xy - 1;
  3310. const int mbb_xy = mb_xy - s->mb_stride;
  3311. int ctx = 0;
  3312. if( s->mb_x > 0 &&
  3313. ( IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) || IS_INTRA16x16( s->current_picture.mb_type[mba_xy] ) ) &&
  3314. h->chroma_pred_mode_table[mba_xy] != 0 ) {
  3315. ctx++;
  3316. }
  3317. if( s->mb_y > 0 &&
  3318. ( IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) || IS_INTRA16x16( s->current_picture.mb_type[mbb_xy] ) ) &&
  3319. h->chroma_pred_mode_table[mbb_xy] != 0 ) {
  3320. ctx++;
  3321. }
  3322. if( get_cabac( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
  3323. return 0;
  3324. if( get_cabac( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  3325. return 1;
  3326. if( get_cabac( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  3327. return 2;
  3328. else
  3329. return 3;
  3330. }
  3331. static const uint8_t block_idx_x[16] = {
  3332. 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
  3333. };
  3334. static const uint8_t block_idx_y[16] = {
  3335. 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
  3336. };
  3337. static const uint8_t block_idx_xy[4][4] = {
  3338. { 0, 2, 8, 10},
  3339. { 1, 3, 9, 11},
  3340. { 4, 6, 12, 14},
  3341. { 5, 7, 13, 15}
  3342. };
  3343. static int decode_cabac_mb_cbp_luma( H264Context *h) {
  3344. MpegEncContext * const s = &h->s;
  3345. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  3346. int cbp = 0;
  3347. int i8x8;
  3348. h->cbp_table[mb_xy] = 0; /* FIXME aaahahahah beurk */
  3349. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  3350. int mba_xy = -1;
  3351. int mbb_xy = -1;
  3352. int x, y;
  3353. int ctx = 0;
  3354. x = block_idx_x[4*i8x8];
  3355. y = block_idx_y[4*i8x8];
  3356. if( x > 0 )
  3357. mba_xy = mb_xy;
  3358. else if( s->mb_x > 0 )
  3359. mba_xy = mb_xy - 1;
  3360. if( y > 0 )
  3361. mbb_xy = mb_xy;
  3362. else if( s->mb_y > 0 )
  3363. mbb_xy = mb_xy - s->mb_stride;
  3364. if( mba_xy >= 0 ) {
  3365. int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
  3366. if( IS_SKIP( s->current_picture.mb_type[mba_xy] ) || ((h->cbp_table[mba_xy] >> i8x8a)&0x01) == 0 )
  3367. ctx++;
  3368. }
  3369. if( mbb_xy >= 0 ) {
  3370. int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
  3371. if( IS_SKIP( s->current_picture.mb_type[mbb_xy] ) || ((h->cbp_table[mbb_xy] >> i8x8b)&0x01) == 0 )
  3372. ctx += 2;
  3373. }
  3374. if( get_cabac( &h->cabac, &h->cabac_state[73 + ctx] ) ) {
  3375. cbp |= 1 << i8x8;
  3376. h->cbp_table[mb_xy] = cbp; /* FIXME aaahahahah beurk */
  3377. }
  3378. }
  3379. return cbp;
  3380. }
  3381. static int decode_cabac_mb_cbp_chroma( H264Context *h) {
  3382. MpegEncContext * const s = &h->s;
  3383. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  3384. int ctx;
  3385. int cbp_a, cbp_b;
  3386. if( s->mb_x > 0 && !IS_SKIP( s->current_picture.mb_type[mb_xy-1] ) )
  3387. cbp_a = (h->cbp_table[mb_xy-1]>>4)&0x03;
  3388. else
  3389. cbp_a = -1;
  3390. if( s->mb_y > 0 && !IS_SKIP( s->current_picture.mb_type[mb_xy-s->mb_stride] ) )
  3391. cbp_b = (h->cbp_table[mb_xy-s->mb_stride]>>4)&0x03;
  3392. else
  3393. cbp_b = -1;
  3394. ctx = 0;
  3395. if( cbp_a > 0 ) ctx++;
  3396. if( cbp_b > 0 ) ctx += 2;
  3397. if( get_cabac( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
  3398. return 0;
  3399. ctx = 4;
  3400. if( cbp_a == 2 ) ctx++;
  3401. if( cbp_b == 2 ) ctx += 2;
  3402. if( get_cabac( &h->cabac, &h->cabac_state[77 + ctx] ) )
  3403. return 2;
  3404. else
  3405. return 1;
  3406. }
  3407. static int decode_cabac_mb_dqp( H264Context *h) {
  3408. MpegEncContext * const s = &h->s;
  3409. int mbn_xy;
  3410. int ctx = 0;
  3411. int val = 0;
  3412. if( s->mb_x > 0 )
  3413. mbn_xy = s->mb_x + s->mb_y*s->mb_stride - 1;
  3414. else
  3415. mbn_xy = s->mb_width - 1 + (s->mb_y-1)*s->mb_stride;
  3416. if( mbn_xy >= 0 && h->last_qscale_diff != 0 && ( IS_INTRA16x16(s->current_picture.mb_type[mbn_xy] ) || (h->cbp_table[mbn_xy]&0x3f) ) )
  3417. ctx++;
  3418. while( get_cabac( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
  3419. if( ctx < 2 )
  3420. ctx = 2;
  3421. else
  3422. ctx = 3;
  3423. val++;
  3424. }
  3425. if( val&0x01 )
  3426. return (val + 1)/2;
  3427. else
  3428. return -(val + 1)/2;
  3429. }
  3430. static int get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) {
  3431. MpegEncContext * const s = &h->s;
  3432. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  3433. int mba_xy = -1;
  3434. int mbb_xy = -1;
  3435. int nza = -1;
  3436. int nzb = -1;
  3437. int ctx = 0;
  3438. if( cat == 0 ) {
  3439. if( s->mb_x > 0 ) {
  3440. mba_xy = mb_xy - 1;
  3441. if( IS_INTRA16x16(s->current_picture.mb_type[mba_xy] ) )
  3442. nza = h->cbp_table[mba_xy]&0x100;
  3443. }
  3444. if( s->mb_y > 0 ) {
  3445. mbb_xy = mb_xy - s->mb_stride;
  3446. if( IS_INTRA16x16(s->current_picture.mb_type[mbb_xy] ) )
  3447. nzb = h->cbp_table[mbb_xy]&0x100;
  3448. }
  3449. } else if( cat == 1 || cat == 2 ) {
  3450. int i8x8a, i8x8b;
  3451. int x, y;
  3452. x = block_idx_x[idx];
  3453. y = block_idx_y[idx];
  3454. if( x > 0 )
  3455. mba_xy = mb_xy;
  3456. else if( s->mb_x > 0 )
  3457. mba_xy = mb_xy - 1;
  3458. if( y > 0 )
  3459. mbb_xy = mb_xy;
  3460. else if( s->mb_y > 0 )
  3461. mbb_xy = mb_xy - s->mb_stride;
  3462. if( mba_xy >= 0 ) {
  3463. i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
  3464. if( !IS_SKIP(s->current_picture.mb_type[mba_xy] ) &&
  3465. !IS_INTRA_PCM(s->current_picture.mb_type[mba_xy] ) &&
  3466. ((h->cbp_table[mba_xy]&0x0f)>>i8x8a))
  3467. nza = h->non_zero_count_cache[scan8[idx] - 1];
  3468. }
  3469. if( mbb_xy >= 0 ) {
  3470. i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
  3471. if( !IS_SKIP(s->current_picture.mb_type[mbb_xy] ) &&
  3472. !IS_INTRA_PCM(s->current_picture.mb_type[mbb_xy] ) &&
  3473. ((h->cbp_table[mbb_xy]&0x0f)>>i8x8b))
  3474. nzb = h->non_zero_count_cache[scan8[idx] - 8];
  3475. }
  3476. } else if( cat == 3 ) {
  3477. if( s->mb_x > 0 ) {
  3478. mba_xy = mb_xy - 1;
  3479. if( !IS_SKIP(s->current_picture.mb_type[mba_xy] ) &&
  3480. !IS_INTRA_PCM(s->current_picture.mb_type[mba_xy] ) &&
  3481. (h->cbp_table[mba_xy]&0x30) )
  3482. nza = (h->cbp_table[mba_xy]>>(6+idx))&0x01;
  3483. }
  3484. if( s->mb_y > 0 ) {
  3485. mbb_xy = mb_xy - s->mb_stride;
  3486. if( !IS_SKIP(s->current_picture.mb_type[mbb_xy] ) &&
  3487. !IS_INTRA_PCM(s->current_picture.mb_type[mbb_xy] ) &&
  3488. (h->cbp_table[mbb_xy]&0x30) )
  3489. nzb = (h->cbp_table[mbb_xy]>>(6+idx))&0x01;
  3490. }
  3491. } else if( cat == 4 ) {
  3492. int idxc = idx % 4 ;
  3493. if( idxc == 1 || idxc == 3 )
  3494. mba_xy = mb_xy;
  3495. else if( s->mb_x > 0 )
  3496. mba_xy = mb_xy -1;
  3497. if( idxc == 2 || idxc == 3 )
  3498. mbb_xy = mb_xy;
  3499. else if( s->mb_y > 0 )
  3500. mbb_xy = mb_xy - s->mb_stride;
  3501. if( mba_xy >= 0 &&
  3502. !IS_SKIP(s->current_picture.mb_type[mba_xy] ) &&
  3503. !IS_INTRA_PCM(s->current_picture.mb_type[mba_xy] ) &&
  3504. (h->cbp_table[mba_xy]&0x30) == 0x20 )
  3505. nza = h->non_zero_count_cache[scan8[16+idx] - 1];
  3506. if( mbb_xy >= 0 &&
  3507. !IS_SKIP(s->current_picture.mb_type[mbb_xy] ) &&
  3508. !IS_INTRA_PCM(s->current_picture.mb_type[mbb_xy] ) &&
  3509. (h->cbp_table[mbb_xy]&0x30) == 0x20 )
  3510. nzb = h->non_zero_count_cache[scan8[16+idx] - 8];
  3511. }
  3512. if( ( mba_xy < 0 && IS_INTRA( s->current_picture.mb_type[mb_xy] ) ) ||
  3513. ( mba_xy >= 0 && IS_INTRA_PCM(s->current_picture.mb_type[mba_xy] ) ) ||
  3514. nza > 0 )
  3515. ctx++;
  3516. if( ( mbb_xy < 0 && IS_INTRA( s->current_picture.mb_type[mb_xy] ) ) ||
  3517. ( mbb_xy >= 0 && IS_INTRA_PCM(s->current_picture.mb_type[mbb_xy] ) ) ||
  3518. nzb > 0 )
  3519. ctx += 2;
  3520. return ctx + 4 * cat;
  3521. }
  3522. static int decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, int qp, int max_coeff) {
  3523. const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
  3524. const uint16_t *qmul= dequant_coeff[qp];
  3525. static const int significant_coeff_flag_offset[5] = { 0, 15, 29, 44, 47 };
  3526. static const int last_significant_coeff_flag_offset[5] = { 0, 15, 29, 44, 47 };
  3527. static const int coeff_abs_level_m1_offset[5] = { 0, 10, 20, 30, 39 };
  3528. int coeff[16];
  3529. int last = 0;
  3530. int coeff_count = 0;
  3531. int nz[16] = {0};
  3532. int i;
  3533. int abslevel1 = 0;
  3534. int abslevelgt1 = 0;
  3535. /* cat: 0-> DC 16x16 n = 0
  3536. * 1-> AC 16x16 n = luma4x4idx
  3537. * 2-> Luma4x4 n = luma4x4idx
  3538. * 3-> DC Chroma n = iCbCr
  3539. * 4-> AC Chroma n = 4 * iCbCr + chroma4x4idx
  3540. */
  3541. /* read coded block flag */
  3542. if( get_cabac( &h->cabac, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n ) ] ) == 0 ) {
  3543. if( cat == 1 || cat == 2 )
  3544. h->non_zero_count_cache[scan8[n]] = 0;
  3545. else if( cat == 4 )
  3546. h->non_zero_count_cache[scan8[16+n]] = 0;
  3547. return 0;
  3548. }
  3549. while( last < max_coeff - 1 ) {
  3550. int ctx = FFMIN( last, max_coeff - 2 );
  3551. if( get_cabac( &h->cabac, &h->cabac_state[105+significant_coeff_flag_offset[cat]+ctx] ) == 0 ) {
  3552. nz[last++] = 0;
  3553. }
  3554. else {
  3555. nz[last++] = 1;
  3556. coeff_count++;
  3557. if( get_cabac( &h->cabac, &h->cabac_state[166+last_significant_coeff_flag_offset[cat]+ctx] ) ) {
  3558. while( last < max_coeff ) {
  3559. nz[last++] = 0;
  3560. }
  3561. break;
  3562. }
  3563. }
  3564. }
  3565. if( last == max_coeff -1 ) {
  3566. nz[last++] = 1;
  3567. coeff_count++;
  3568. }
  3569. if( cat == 0 && coeff_count > 0 )
  3570. h->cbp_table[mb_xy] |= 0x100;
  3571. else if( cat == 1 || cat == 2 )
  3572. h->non_zero_count_cache[scan8[n]] = coeff_count;
  3573. else if( cat == 3 && coeff_count > 0 )
  3574. h->cbp_table[mb_xy] |= 0x40 << n;
  3575. else if( cat == 4 )
  3576. h->non_zero_count_cache[scan8[16+n]] = coeff_count;
  3577. for( i = coeff_count - 1; i >= 0; i-- ) {
  3578. int coeff_abs_m1;
  3579. int ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 + 1 )) + coeff_abs_level_m1_offset[cat];
  3580. if( get_cabac( &h->cabac, &h->cabac_state[227+ctx] ) == 0 ) {
  3581. coeff_abs_m1 = 0;
  3582. } else {
  3583. coeff_abs_m1 = 1;
  3584. ctx = 5 + FFMIN( 4, abslevelgt1 ) + coeff_abs_level_m1_offset[cat];
  3585. while( coeff_abs_m1 < 14 && get_cabac( &h->cabac, &h->cabac_state[227+ctx] ) ) {
  3586. coeff_abs_m1++;
  3587. }
  3588. }
  3589. if( coeff_abs_m1 >= 14 ) {
  3590. int j = 0;
  3591. while( get_cabac_bypass( &h->cabac ) ) {
  3592. coeff_abs_m1 += 1 << j;
  3593. j++;
  3594. }
  3595. while( j-- ) {
  3596. if( get_cabac_bypass( &h->cabac ) )
  3597. coeff_abs_m1 += 1 << j ;
  3598. }
  3599. }
  3600. if( get_cabac_bypass( &h->cabac ) )
  3601. coeff[i] = -1 *( coeff_abs_m1 + 1 );
  3602. else
  3603. coeff[i] = coeff_abs_m1 + 1;
  3604. if( coeff_abs_m1 == 0 )
  3605. abslevel1++;
  3606. else
  3607. abslevelgt1++;
  3608. }
  3609. if( cat == 0 || cat == 3 ) { /* DC */
  3610. int j;
  3611. for( i = 0, j = 0; j < coeff_count; i++ ) {
  3612. if( nz[i] ) {
  3613. block[scantable[i]] = coeff[j];
  3614. j++;
  3615. }
  3616. }
  3617. } else { /* AC */
  3618. int j;
  3619. for( i = 0, j = 0; j < coeff_count; i++ ) {
  3620. if( nz[i] ) {
  3621. block[scantable[i]] = coeff[j] * qmul[scantable[i]];
  3622. j++;
  3623. }
  3624. }
  3625. }
  3626. return 0;
  3627. }
  3628. /**
  3629. * decodes a macroblock
  3630. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  3631. */
  3632. static int decode_mb_cabac(H264Context *h) {
  3633. MpegEncContext * const s = &h->s;
  3634. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  3635. int mb_type, partition_count, cbp = 0;
  3636. s->dsp.clear_blocks(h->mb); //FIXME avoid if allready clear (move after skip handlong?)
  3637. if( h->slice_type == B_TYPE ) {
  3638. av_log( h->s.avctx, AV_LOG_ERROR, "B-frame not supported with CABAC\n" );
  3639. return -1;
  3640. }
  3641. if( h->sps.mb_aff ) {
  3642. av_log( h->s.avctx, AV_LOG_ERROR, "Fields not supported with CABAC\n" );
  3643. return -1;
  3644. }
  3645. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) {
  3646. /* read skip flags */
  3647. if( decode_cabac_mb_skip( h ) ) {
  3648. int mx, my;
  3649. /* skip mb */
  3650. mb_type= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
  3651. memset(h->non_zero_count[mb_xy], 0, 16);
  3652. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  3653. #if 0
  3654. if(h->sps.mb_aff && s->mb_skip_run==0 && (s->mb_y&1)==0){
  3655. h->mb_field_decoding_flag= get_bits1(&s->gb);
  3656. }
  3657. if(h->mb_field_decoding_flag)
  3658. mb_type|= MB_TYPE_INTERLACED;
  3659. #endif
  3660. fill_caches(h, mb_type); //FIXME check what is needed and what not ...
  3661. pred_pskip_motion(h, &mx, &my);
  3662. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  3663. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  3664. write_back_motion(h, mb_type);
  3665. s->current_picture.mb_type[mb_xy]= mb_type; //FIXME SKIP type
  3666. s->current_picture.qscale_table[mb_xy]= s->qscale;
  3667. h->slice_table[ mb_xy ]= h->slice_num;
  3668. h->cbp_table[mb_xy] = 0;
  3669. h->last_qscale_diff = 0;
  3670. h->prev_mb_skiped= 1;
  3671. return 0;
  3672. }
  3673. }
  3674. h->prev_mb_skiped = 0;
  3675. if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
  3676. av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
  3677. return -1;
  3678. }
  3679. //av_log( s->avctx, AV_LOG_ERROR, "mb_type=%d\n", mb_type );
  3680. if( h->slice_type == P_TYPE ) {
  3681. if( mb_type < 5) {
  3682. partition_count= p_mb_type_info[mb_type].partition_count;
  3683. mb_type= p_mb_type_info[mb_type].type;
  3684. av_log( h->s.avctx, AV_LOG_ERROR, "gni P-type not yet supported\n" );
  3685. return -1;
  3686. } else {
  3687. mb_type -= 5;
  3688. goto decode_intra_mb;
  3689. }
  3690. } else {
  3691. assert(h->slice_type == I_TYPE);
  3692. decode_intra_mb:
  3693. partition_count = 0;
  3694. cbp= i_mb_type_info[mb_type].cbp;
  3695. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  3696. mb_type= i_mb_type_info[mb_type].type;
  3697. }
  3698. #if 0
  3699. if(h->mb_field_decoding_flag)
  3700. mb_type |= MB_TYPE_INTERLACED;
  3701. #endif
  3702. s->current_picture.mb_type[mb_xy]= mb_type;
  3703. h->slice_table[ mb_xy ]= h->slice_num;
  3704. if(IS_INTRA_PCM(mb_type)) {
  3705. /* TODO */
  3706. h->cbp_table[mb_xy] = 0xf +4*2;
  3707. s->current_picture.qscale_table[mb_xy]= s->qscale;
  3708. return -1;
  3709. }
  3710. fill_caches(h, mb_type);
  3711. if( IS_INTRA( mb_type ) ) {
  3712. if( IS_INTRA4x4( mb_type ) ) {
  3713. int i;
  3714. for( i = 0; i < 16; i++ ) {
  3715. int pred = pred_intra_mode( h, i );
  3716. h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  3717. //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
  3718. }
  3719. write_back_intra_pred_mode(h);
  3720. if( check_intra4x4_pred_mode(h) < 0 ) return -1;
  3721. } else {
  3722. h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );
  3723. if( h->intra16x16_pred_mode < 0 ) return -1;
  3724. }
  3725. h->chroma_pred_mode_table[mb_xy] =
  3726. h->chroma_pred_mode = decode_cabac_mb_chroma_pre_mode( h );
  3727. h->chroma_pred_mode= check_intra_pred_mode( h, h->chroma_pred_mode );
  3728. if( h->chroma_pred_mode < 0 ) return -1;
  3729. } else if( partition_count == 4 ) {
  3730. /* TODO */
  3731. return -1;
  3732. } else if( !IS_DIRECT(mb_type) ) {
  3733. /* TODO */
  3734. return -1;
  3735. }
  3736. if( IS_INTER( mb_type ) )
  3737. write_back_motion( h, mb_type );
  3738. if( !IS_INTRA16x16( mb_type ) ) {
  3739. cbp = decode_cabac_mb_cbp_luma( h );
  3740. cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
  3741. }
  3742. //av_log( NULL, AV_LOG_ERROR, "cbp=%d\n", cbp );
  3743. h->cbp_table[mb_xy] = cbp;
  3744. if( cbp || IS_INTRA16x16( mb_type ) ) {
  3745. const uint8_t *scan, *dc_scan;
  3746. int dqp;
  3747. if(IS_INTERLACED(mb_type)){
  3748. scan= field_scan;
  3749. dc_scan= luma_dc_field_scan;
  3750. }else{
  3751. scan= zigzag_scan;
  3752. dc_scan= luma_dc_zigzag_scan;
  3753. }
  3754. h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
  3755. s->qscale += dqp;
  3756. if(((unsigned)s->qscale) > 51){
  3757. if(s->qscale<0) s->qscale+= 52;
  3758. else s->qscale-= 52;
  3759. }
  3760. h->chroma_qp = get_chroma_qp(h, s->qscale);
  3761. if( IS_INTRA16x16( mb_type ) ) {
  3762. int i;
  3763. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
  3764. if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, s->qscale, 16) < 0)
  3765. return -1;
  3766. if( cbp&15 ) {
  3767. for( i = 0; i < 16; i++ ) {
  3768. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
  3769. if( decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, s->qscale, 15) < 0 )
  3770. return -1;
  3771. }
  3772. } else {
  3773. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  3774. }
  3775. } else {
  3776. int i8x8, i4x4;
  3777. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  3778. if( cbp & (1<<i8x8) ) {
  3779. for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
  3780. const int index = 4*i8x8 + i4x4;
  3781. //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
  3782. if( decode_cabac_residual(h, h->mb + 16*index, 2, index, scan, s->qscale, 16) < 0 )
  3783. return -1;
  3784. }
  3785. } else {
  3786. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  3787. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  3788. }
  3789. }
  3790. }
  3791. if( cbp&0x30 ){
  3792. int c;
  3793. for( c = 0; c < 2; c++ ) {
  3794. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
  3795. if( decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, h->chroma_qp, 4) < 0)
  3796. return -1;
  3797. }
  3798. }
  3799. if( cbp&0x20 ) {
  3800. int c, i;
  3801. for( c = 0; c < 2; c++ ) {
  3802. for( i = 0; i < 4; i++ ) {
  3803. const int index = 16 + 4 * c + i;
  3804. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
  3805. if( decode_cabac_residual(h, h->mb + 16*index, 4, index - 16, scan + 1, h->chroma_qp, 15) < 0)
  3806. return -1;
  3807. }
  3808. }
  3809. } else {
  3810. uint8_t * const nnz= &h->non_zero_count_cache[0];
  3811. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  3812. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  3813. }
  3814. } else {
  3815. memset( &h->non_zero_count_cache[8], 0, 8*5 );
  3816. }
  3817. s->current_picture.qscale_table[mb_xy]= s->qscale;
  3818. write_back_non_zero_count(h);
  3819. return 0;
  3820. }
  3821. static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  3822. int i, d;
  3823. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  3824. const int alpha = alpha_table[index_a];
  3825. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  3826. for( i = 0; i < 4; i++ ) {
  3827. if( bS[i] == 0 ) {
  3828. pix += 4 * stride;
  3829. continue;
  3830. }
  3831. if( bS[i] < 4 ) {
  3832. const int tc0 = tc0_table[index_a][bS[i] - 1];
  3833. /* 4px edge length */
  3834. for( d = 0; d < 4; d++ ) {
  3835. const int p0 = pix[-1];
  3836. const int p1 = pix[-2];
  3837. const int p2 = pix[-3];
  3838. const int q0 = pix[0];
  3839. const int q1 = pix[1];
  3840. const int q2 = pix[2];
  3841. if( ABS( p0 - q0 ) < alpha &&
  3842. ABS( p1 - p0 ) < beta &&
  3843. ABS( q1 - q0 ) < beta ) {
  3844. int tc = tc0;
  3845. int i_delta;
  3846. if( ABS( p2 - p0 ) < beta ) {
  3847. pix[-2] = p1 + clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  3848. tc++;
  3849. }
  3850. if( ABS( q2 - q0 ) < beta ) {
  3851. pix[1] = q1 + clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  3852. tc++;
  3853. }
  3854. i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  3855. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  3856. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  3857. }
  3858. pix += stride;
  3859. }
  3860. }else{
  3861. /* 4px edge length */
  3862. for( d = 0; d < 4; d++ ) {
  3863. const int p0 = pix[-1];
  3864. const int p1 = pix[-2];
  3865. const int p2 = pix[-3];
  3866. const int q0 = pix[0];
  3867. const int q1 = pix[1];
  3868. const int q2 = pix[2];
  3869. if( ABS( p0 - q0 ) < alpha &&
  3870. ABS( p1 - p0 ) < beta &&
  3871. ABS( q1 - q0 ) < beta ) {
  3872. if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  3873. if( ABS( p2 - p0 ) < beta)
  3874. {
  3875. const int p3 = pix[-4];
  3876. /* p0', p1', p2' */
  3877. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  3878. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  3879. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  3880. } else {
  3881. /* p0' */
  3882. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  3883. }
  3884. if( ABS( q2 - q0 ) < beta)
  3885. {
  3886. const int q3 = pix[3];
  3887. /* q0', q1', q2' */
  3888. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  3889. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  3890. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  3891. } else {
  3892. /* q0' */
  3893. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  3894. }
  3895. }else{
  3896. /* p0', q0' */
  3897. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  3898. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  3899. }
  3900. }
  3901. pix += stride;
  3902. }
  3903. }
  3904. }
  3905. }
  3906. static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  3907. int i, d;
  3908. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  3909. const int alpha = alpha_table[index_a];
  3910. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  3911. for( i = 0; i < 4; i++ ) {
  3912. if( bS[i] == 0 ) {
  3913. pix += 2 * stride;
  3914. continue;
  3915. }
  3916. if( bS[i] < 4 ) {
  3917. const int tc = tc0_table[index_a][bS[i] - 1] + 1;
  3918. /* 2px edge length (because we use same bS than the one for luma) */
  3919. for( d = 0; d < 2; d++ ){
  3920. const int p0 = pix[-1];
  3921. const int p1 = pix[-2];
  3922. const int q0 = pix[0];
  3923. const int q1 = pix[1];
  3924. if( ABS( p0 - q0 ) < alpha &&
  3925. ABS( p1 - p0 ) < beta &&
  3926. ABS( q1 - q0 ) < beta ) {
  3927. const int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  3928. pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
  3929. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  3930. }
  3931. pix += stride;
  3932. }
  3933. }else{
  3934. /* 2px edge length (because we use same bS than the one for luma) */
  3935. for( d = 0; d < 2; d++ ){
  3936. const int p0 = pix[-1];
  3937. const int p1 = pix[-2];
  3938. const int q0 = pix[0];
  3939. const int q1 = pix[1];
  3940. if( ABS( p0 - q0 ) < alpha &&
  3941. ABS( p1 - p0 ) < beta &&
  3942. ABS( q1 - q0 ) < beta ) {
  3943. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  3944. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  3945. }
  3946. pix += stride;
  3947. }
  3948. }
  3949. }
  3950. }
  3951. static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  3952. int i, d;
  3953. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  3954. const int alpha = alpha_table[index_a];
  3955. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  3956. const int pix_next = stride;
  3957. for( i = 0; i < 4; i++ ) {
  3958. if( bS[i] == 0 ) {
  3959. pix += 4;
  3960. continue;
  3961. }
  3962. if( bS[i] < 4 ) {
  3963. const int tc0 = tc0_table[index_a][bS[i] - 1];
  3964. /* 4px edge length */
  3965. for( d = 0; d < 4; d++ ) {
  3966. const int p0 = pix[-1*pix_next];
  3967. const int p1 = pix[-2*pix_next];
  3968. const int p2 = pix[-3*pix_next];
  3969. const int q0 = pix[0];
  3970. const int q1 = pix[1*pix_next];
  3971. const int q2 = pix[2*pix_next];
  3972. if( ABS( p0 - q0 ) < alpha &&
  3973. ABS( p1 - p0 ) < beta &&
  3974. ABS( q1 - q0 ) < beta ) {
  3975. int tc = tc0;
  3976. int i_delta;
  3977. if( ABS( p2 - p0 ) < beta ) {
  3978. pix[-2*pix_next] = p1 + clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  3979. tc++;
  3980. }
  3981. if( ABS( q2 - q0 ) < beta ) {
  3982. pix[pix_next] = q1 + clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  3983. tc++;
  3984. }
  3985. i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  3986. pix[-pix_next] = clip_uint8( p0 + i_delta ); /* p0' */
  3987. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  3988. }
  3989. pix++;
  3990. }
  3991. }else{
  3992. /* 4px edge length */
  3993. for( d = 0; d < 4; d++ ) {
  3994. const int p0 = pix[-1*pix_next];
  3995. const int p1 = pix[-2*pix_next];
  3996. const int p2 = pix[-3*pix_next];
  3997. const int q0 = pix[0];
  3998. const int q1 = pix[1*pix_next];
  3999. const int q2 = pix[2*pix_next];
  4000. if( ABS( p0 - q0 ) < alpha &&
  4001. ABS( p1 - p0 ) < beta &&
  4002. ABS( q1 - q0 ) < beta ) {
  4003. const int p3 = pix[-4*pix_next];
  4004. const int q3 = pix[ 3*pix_next];
  4005. if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  4006. if( ABS( p2 - p0 ) < beta) {
  4007. /* p0', p1', p2' */
  4008. pix[-1*pix_next] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  4009. pix[-2*pix_next] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  4010. pix[-3*pix_next] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  4011. } else {
  4012. /* p0' */
  4013. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  4014. }
  4015. if( ABS( q2 - q0 ) < beta) {
  4016. /* q0', q1', q2' */
  4017. pix[0*pix_next] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  4018. pix[1*pix_next] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  4019. pix[2*pix_next] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  4020. } else {
  4021. /* q0' */
  4022. pix[0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  4023. }
  4024. }else{
  4025. /* p0', q0' */
  4026. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  4027. pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  4028. }
  4029. }
  4030. pix++;
  4031. }
  4032. }
  4033. }
  4034. }
  4035. static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
  4036. int i, d;
  4037. const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
  4038. const int alpha = alpha_table[index_a];
  4039. const int beta = beta_table[clip( qp + h->slice_beta_offset, 0, 51 )];
  4040. const int pix_next = stride;
  4041. for( i = 0; i < 4; i++ )
  4042. {
  4043. if( bS[i] == 0 ) {
  4044. pix += 2;
  4045. continue;
  4046. }
  4047. if( bS[i] < 4 ) {
  4048. int tc = tc0_table[index_a][bS[i] - 1] + 1;
  4049. /* 2px edge length (see deblocking_filter_edgecv) */
  4050. for( d = 0; d < 2; d++ ) {
  4051. const int p0 = pix[-1*pix_next];
  4052. const int p1 = pix[-2*pix_next];
  4053. const int q0 = pix[0];
  4054. const int q1 = pix[1*pix_next];
  4055. if( ABS( p0 - q0 ) < alpha &&
  4056. ABS( p1 - p0 ) < beta &&
  4057. ABS( q1 - q0 ) < beta ) {
  4058. int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  4059. pix[-pix_next] = clip_uint8( p0 + i_delta ); /* p0' */
  4060. pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
  4061. }
  4062. pix++;
  4063. }
  4064. }else{
  4065. /* 2px edge length (see deblocking_filter_edgecv) */
  4066. for( d = 0; d < 2; d++ ) {
  4067. const int p0 = pix[-1*pix_next];
  4068. const int p1 = pix[-2*pix_next];
  4069. const int q0 = pix[0];
  4070. const int q1 = pix[1*pix_next];
  4071. if( ABS( p0 - q0 ) < alpha &&
  4072. ABS( p1 - p0 ) < beta &&
  4073. ABS( q1 - q0 ) < beta ) {
  4074. pix[-pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  4075. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  4076. }
  4077. pix++;
  4078. }
  4079. }
  4080. }
  4081. }
  4082. static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr) {
  4083. MpegEncContext * const s = &h->s;
  4084. const int mb_xy= mb_x + mb_y*s->mb_stride;
  4085. int linesize, uvlinesize;
  4086. int dir;
  4087. /* FIXME Implement deblocking filter for field MB */
  4088. if( h->sps.mb_aff ) {
  4089. return;
  4090. }
  4091. linesize = s->linesize;
  4092. uvlinesize = s->uvlinesize;
  4093. /* dir : 0 -> vertical edge, 1 -> horizontal edge */
  4094. for( dir = 0; dir < 2; dir++ )
  4095. {
  4096. int start = 0;
  4097. int edge;
  4098. /* test picture boundary */
  4099. if( ( dir == 0 && mb_x == 0 ) || ( dir == 1 && mb_y == 0 ) ) {
  4100. start = 1;
  4101. }
  4102. /* FIXME test slice boundary */
  4103. if( h->deblocking_filter == 2 ) {
  4104. }
  4105. /* Calculate bS */
  4106. for( edge = start; edge < 4; edge++ ) {
  4107. /* mbn_xy: neighbour macroblock (how that works for field ?) */
  4108. int mbn_xy = edge > 0 ? mb_xy : ( dir == 0 ? mb_xy -1 : mb_xy - s->mb_stride );
  4109. int bS[4];
  4110. int qp;
  4111. if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) ||
  4112. IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) {
  4113. bS[0] = bS[1] = bS[2] = bS[3] = ( edge == 0 ? 4 : 3 );
  4114. } else {
  4115. int i;
  4116. for( i = 0; i < 4; i++ ) {
  4117. int x = dir == 0 ? edge : i;
  4118. int y = dir == 0 ? i : edge;
  4119. int b_idx= 8 + 4 + x + 8*y;
  4120. int bn_idx= b_idx - (dir ? 8:1);
  4121. if( h->non_zero_count_cache[b_idx] != 0 ||
  4122. h->non_zero_count_cache[bn_idx] != 0 ) {
  4123. bS[i] = 2;
  4124. }
  4125. else if( h->slice_type == P_TYPE ) {
  4126. if( h->ref_cache[0][b_idx] != h->ref_cache[0][bn_idx] ||
  4127. ABS( h->mv_cache[0][b_idx][0] - h->mv_cache[0][bn_idx][0] ) >= 4 ||
  4128. ABS( h->mv_cache[0][b_idx][1] - h->mv_cache[0][bn_idx][1] ) >= 4 )
  4129. bS[i] = 1;
  4130. else
  4131. bS[i] = 0;
  4132. }
  4133. else {
  4134. /* FIXME Add support for B frame */
  4135. return;
  4136. }
  4137. }
  4138. if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
  4139. continue;
  4140. }
  4141. /* Filter edge */
  4142. qp = ( s->qscale + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  4143. if( dir == 0 ) {
  4144. filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
  4145. if( (edge&1) == 0 ) {
  4146. int chroma_qp = ( h->chroma_qp +
  4147. get_chroma_qp( h, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  4148. filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp );
  4149. filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp );
  4150. }
  4151. } else {
  4152. filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
  4153. if( (edge&1) == 0 ) {
  4154. int chroma_qp = ( h->chroma_qp +
  4155. get_chroma_qp( h, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  4156. filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  4157. filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  4158. }
  4159. }
  4160. }
  4161. }
  4162. }
  4163. static int decode_slice(H264Context *h){
  4164. MpegEncContext * const s = &h->s;
  4165. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  4166. s->mb_skip_run= -1;
  4167. if( h->pps.cabac ) {
  4168. int i;
  4169. /* realign */
  4170. align_get_bits( &s->gb );
  4171. /* init cabac */
  4172. ff_init_cabac_states( &h->cabac, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64 );
  4173. ff_init_cabac_decoder( &h->cabac,
  4174. s->gb.buffer + get_bits_count(&s->gb)/8,
  4175. ( s->gb.size_in_bits - get_bits_count(&s->gb) ) );
  4176. /* calculate pre-state */
  4177. for( i= 0; i < 399; i++ ) {
  4178. int pre;
  4179. if( h->slice_type == I_TYPE )
  4180. pre = clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
  4181. else
  4182. 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 );
  4183. if( pre <= 63 )
  4184. h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
  4185. else
  4186. h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
  4187. }
  4188. for(;;){
  4189. int ret = decode_mb_cabac(h);
  4190. int eos = get_cabac_terminate( &h->cabac ); /* End of Slice flag */
  4191. hl_decode_mb(h);
  4192. /* XXX: useless as decode_mb_cabac it doesn't support that ... */
  4193. if( ret >= 0 && h->sps.mb_aff ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  4194. s->mb_y++;
  4195. ret = decode_mb_cabac(h);
  4196. eos = get_cabac_terminate( &h->cabac );
  4197. hl_decode_mb(h);
  4198. s->mb_y--;
  4199. }
  4200. if( ret < 0 ) {
  4201. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  4202. 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);
  4203. return -1;
  4204. }
  4205. if( ++s->mb_x >= s->mb_width ) {
  4206. s->mb_x = 0;
  4207. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  4208. if( ++s->mb_y >= s->mb_height ) {
  4209. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  4210. }
  4211. }
  4212. if( eos || s->mb_y >= s->mb_height ) {
  4213. 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);
  4214. return 0;
  4215. }
  4216. #if 0
  4217. /* TODO test over-reading in cabac code */
  4218. else if( read too much in h->cabac ) {
  4219. 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);
  4220. return -1;
  4221. }
  4222. #endif
  4223. }
  4224. } else {
  4225. for(;;){
  4226. int ret = decode_mb_cavlc(h);
  4227. hl_decode_mb(h);
  4228. if(ret>=0 && h->sps.mb_aff){ //FIXME optimal? or let mb_decode decode 16x32 ?
  4229. s->mb_y++;
  4230. ret = decode_mb_cavlc(h);
  4231. hl_decode_mb(h);
  4232. s->mb_y--;
  4233. }
  4234. if(ret<0){
  4235. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  4236. 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);
  4237. return -1;
  4238. }
  4239. if(++s->mb_x >= s->mb_width){
  4240. s->mb_x=0;
  4241. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  4242. if(++s->mb_y >= s->mb_height){
  4243. tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  4244. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  4245. 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);
  4246. return 0;
  4247. }else{
  4248. 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);
  4249. return -1;
  4250. }
  4251. }
  4252. }
  4253. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  4254. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  4255. 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);
  4256. return 0;
  4257. }else{
  4258. 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);
  4259. return -1;
  4260. }
  4261. }
  4262. }
  4263. }
  4264. #if 0
  4265. for(;s->mb_y < s->mb_height; s->mb_y++){
  4266. for(;s->mb_x < s->mb_width; s->mb_x++){
  4267. int ret= decode_mb(h);
  4268. hl_decode_mb(h);
  4269. if(ret<0){
  4270. fprintf(stderr, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  4271. 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);
  4272. return -1;
  4273. }
  4274. if(++s->mb_x >= s->mb_width){
  4275. s->mb_x=0;
  4276. if(++s->mb_y >= s->mb_height){
  4277. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  4278. 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);
  4279. return 0;
  4280. }else{
  4281. 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);
  4282. return -1;
  4283. }
  4284. }
  4285. }
  4286. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  4287. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  4288. 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);
  4289. return 0;
  4290. }else{
  4291. 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);
  4292. return -1;
  4293. }
  4294. }
  4295. }
  4296. s->mb_x=0;
  4297. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  4298. }
  4299. #endif
  4300. return -1; //not reached
  4301. }
  4302. static inline int decode_vui_parameters(H264Context *h, SPS *sps){
  4303. MpegEncContext * const s = &h->s;
  4304. int aspect_ratio_info_present_flag, aspect_ratio_idc;
  4305. aspect_ratio_info_present_flag= get_bits1(&s->gb);
  4306. if( aspect_ratio_info_present_flag ) {
  4307. aspect_ratio_idc= get_bits(&s->gb, 8);
  4308. if( aspect_ratio_idc == EXTENDED_SAR ) {
  4309. sps->sar.num= get_bits(&s->gb, 16);
  4310. sps->sar.den= get_bits(&s->gb, 16);
  4311. }else if(aspect_ratio_idc < 16){
  4312. sps->sar= pixel_aspect[aspect_ratio_idc];
  4313. }else{
  4314. av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
  4315. return -1;
  4316. }
  4317. }else{
  4318. sps->sar.num=
  4319. sps->sar.den= 0;
  4320. }
  4321. // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
  4322. #if 0
  4323. | overscan_info_present_flag |0 |u(1) |
  4324. | if( overscan_info_present_flag ) | | |
  4325. | overscan_appropriate_flag |0 |u(1) |
  4326. | video_signal_type_present_flag |0 |u(1) |
  4327. | if( video_signal_type_present_flag ) { | | |
  4328. | video_format |0 |u(3) |
  4329. | video_full_range_flag |0 |u(1) |
  4330. | colour_description_present_flag |0 |u(1) |
  4331. | if( colour_description_present_flag ) { | | |
  4332. | colour_primaries |0 |u(8) |
  4333. | transfer_characteristics |0 |u(8) |
  4334. | matrix_coefficients |0 |u(8) |
  4335. | } | | |
  4336. | } | | |
  4337. | chroma_location_info_present_flag |0 |u(1) |
  4338. | if ( chroma_location_info_present_flag ) { | | |
  4339. | chroma_sample_location_type_top_field |0 |ue(v) |
  4340. | chroma_sample_location_type_bottom_field |0 |ue(v) |
  4341. | } | | |
  4342. | timing_info_present_flag |0 |u(1) |
  4343. | if( timing_info_present_flag ) { | | |
  4344. | num_units_in_tick |0 |u(32) |
  4345. | time_scale |0 |u(32) |
  4346. | fixed_frame_rate_flag |0 |u(1) |
  4347. | } | | |
  4348. | nal_hrd_parameters_present_flag |0 |u(1) |
  4349. | if( nal_hrd_parameters_present_flag = = 1) | | |
  4350. | hrd_parameters( ) | | |
  4351. | vcl_hrd_parameters_present_flag |0 |u(1) |
  4352. | if( vcl_hrd_parameters_present_flag = = 1) | | |
  4353. | hrd_parameters( ) | | |
  4354. | if( ( nal_hrd_parameters_present_flag = = 1 | || | |
  4355. | | | |
  4356. |( vcl_hrd_parameters_present_flag = = 1 ) ) | | |
  4357. | low_delay_hrd_flag |0 |u(1) |
  4358. | bitstream_restriction_flag |0 |u(1) |
  4359. | if( bitstream_restriction_flag ) { |0 |u(1) |
  4360. | motion_vectors_over_pic_boundaries_flag |0 |u(1) |
  4361. | max_bytes_per_pic_denom |0 |ue(v) |
  4362. | max_bits_per_mb_denom |0 |ue(v) |
  4363. | log2_max_mv_length_horizontal |0 |ue(v) |
  4364. | log2_max_mv_length_vertical |0 |ue(v) |
  4365. | num_reorder_frames |0 |ue(v) |
  4366. | max_dec_frame_buffering |0 |ue(v) |
  4367. | } | | |
  4368. |} | | |
  4369. #endif
  4370. return 0;
  4371. }
  4372. static inline int decode_seq_parameter_set(H264Context *h){
  4373. MpegEncContext * const s = &h->s;
  4374. int profile_idc, level_idc;
  4375. int sps_id, i;
  4376. SPS *sps;
  4377. profile_idc= get_bits(&s->gb, 8);
  4378. get_bits1(&s->gb); //constraint_set0_flag
  4379. get_bits1(&s->gb); //constraint_set1_flag
  4380. get_bits1(&s->gb); //constraint_set2_flag
  4381. get_bits(&s->gb, 5); // reserved
  4382. level_idc= get_bits(&s->gb, 8);
  4383. sps_id= get_ue_golomb(&s->gb);
  4384. sps= &h->sps_buffer[ sps_id ];
  4385. sps->profile_idc= profile_idc;
  4386. sps->level_idc= level_idc;
  4387. sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
  4388. sps->poc_type= get_ue_golomb(&s->gb);
  4389. if(sps->poc_type == 0){ //FIXME #define
  4390. sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
  4391. } else if(sps->poc_type == 1){//FIXME #define
  4392. sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
  4393. sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
  4394. sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
  4395. sps->poc_cycle_length= get_ue_golomb(&s->gb);
  4396. for(i=0; i<sps->poc_cycle_length; i++)
  4397. sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
  4398. }
  4399. if(sps->poc_type > 2){
  4400. av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
  4401. return -1;
  4402. }
  4403. sps->ref_frame_count= get_ue_golomb(&s->gb);
  4404. sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
  4405. sps->mb_width= get_ue_golomb(&s->gb) + 1;
  4406. sps->mb_height= get_ue_golomb(&s->gb) + 1;
  4407. sps->frame_mbs_only_flag= get_bits1(&s->gb);
  4408. if(!sps->frame_mbs_only_flag)
  4409. sps->mb_aff= get_bits1(&s->gb);
  4410. else
  4411. sps->mb_aff= 0;
  4412. sps->direct_8x8_inference_flag= get_bits1(&s->gb);
  4413. sps->crop= get_bits1(&s->gb);
  4414. if(sps->crop){
  4415. sps->crop_left = get_ue_golomb(&s->gb);
  4416. sps->crop_right = get_ue_golomb(&s->gb);
  4417. sps->crop_top = get_ue_golomb(&s->gb);
  4418. sps->crop_bottom= get_ue_golomb(&s->gb);
  4419. if(sps->crop_left || sps->crop_top){
  4420. av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completly supported, this could look slightly wrong ...\n");
  4421. }
  4422. }else{
  4423. sps->crop_left =
  4424. sps->crop_right =
  4425. sps->crop_top =
  4426. sps->crop_bottom= 0;
  4427. }
  4428. sps->vui_parameters_present_flag= get_bits1(&s->gb);
  4429. if( sps->vui_parameters_present_flag )
  4430. decode_vui_parameters(h, sps);
  4431. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  4432. 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",
  4433. sps_id, sps->profile_idc, sps->level_idc,
  4434. sps->poc_type,
  4435. sps->ref_frame_count,
  4436. sps->mb_width, sps->mb_height,
  4437. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  4438. sps->direct_8x8_inference_flag ? "8B8" : "",
  4439. sps->crop_left, sps->crop_right,
  4440. sps->crop_top, sps->crop_bottom,
  4441. sps->vui_parameters_present_flag ? "VUI" : ""
  4442. );
  4443. }
  4444. return 0;
  4445. }
  4446. static inline int decode_picture_parameter_set(H264Context *h){
  4447. MpegEncContext * const s = &h->s;
  4448. int pps_id= get_ue_golomb(&s->gb);
  4449. PPS *pps= &h->pps_buffer[pps_id];
  4450. pps->sps_id= get_ue_golomb(&s->gb);
  4451. pps->cabac= get_bits1(&s->gb);
  4452. pps->pic_order_present= get_bits1(&s->gb);
  4453. pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
  4454. if(pps->slice_group_count > 1 ){
  4455. pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
  4456. av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
  4457. switch(pps->mb_slice_group_map_type){
  4458. case 0:
  4459. #if 0
  4460. | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
  4461. | run_length[ i ] |1 |ue(v) |
  4462. #endif
  4463. break;
  4464. case 2:
  4465. #if 0
  4466. | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
  4467. |{ | | |
  4468. | top_left_mb[ i ] |1 |ue(v) |
  4469. | bottom_right_mb[ i ] |1 |ue(v) |
  4470. | } | | |
  4471. #endif
  4472. break;
  4473. case 3:
  4474. case 4:
  4475. case 5:
  4476. #if 0
  4477. | slice_group_change_direction_flag |1 |u(1) |
  4478. | slice_group_change_rate_minus1 |1 |ue(v) |
  4479. #endif
  4480. break;
  4481. case 6:
  4482. #if 0
  4483. | slice_group_id_cnt_minus1 |1 |ue(v) |
  4484. | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
  4485. |) | | |
  4486. | slice_group_id[ i ] |1 |u(v) |
  4487. #endif
  4488. break;
  4489. }
  4490. }
  4491. pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  4492. pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  4493. if(pps->ref_count[0] > 32 || pps->ref_count[1] > 32){
  4494. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
  4495. return -1;
  4496. }
  4497. pps->weighted_pred= get_bits1(&s->gb);
  4498. pps->weighted_bipred_idc= get_bits(&s->gb, 2);
  4499. pps->init_qp= get_se_golomb(&s->gb) + 26;
  4500. pps->init_qs= get_se_golomb(&s->gb) + 26;
  4501. pps->chroma_qp_index_offset= get_se_golomb(&s->gb);
  4502. pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
  4503. pps->constrained_intra_pred= get_bits1(&s->gb);
  4504. pps->redundant_pic_cnt_present = get_bits1(&s->gb);
  4505. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  4506. 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",
  4507. pps_id, pps->sps_id,
  4508. pps->cabac ? "CABAC" : "CAVLC",
  4509. pps->slice_group_count,
  4510. pps->ref_count[0], pps->ref_count[1],
  4511. pps->weighted_pred ? "weighted" : "",
  4512. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset,
  4513. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  4514. pps->constrained_intra_pred ? "CONSTR" : "",
  4515. pps->redundant_pic_cnt_present ? "REDU" : ""
  4516. );
  4517. }
  4518. return 0;
  4519. }
  4520. /**
  4521. * finds the end of the current frame in the bitstream.
  4522. * @return the position of the first byte of the next frame, or -1
  4523. */
  4524. static int find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
  4525. ParseContext *pc= &s->parse_context;
  4526. int i;
  4527. uint32_t state;
  4528. //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
  4529. // mb_addr= pc->mb_addr - 1;
  4530. state= pc->state;
  4531. //FIXME this will fail with slices
  4532. for(i=0; i<buf_size; i++){
  4533. state= (state<<8) | buf[i];
  4534. if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  4535. if(pc->frame_start_found){
  4536. pc->state=-1;
  4537. pc->frame_start_found= 0;
  4538. return i-3;
  4539. }
  4540. pc->frame_start_found= 1;
  4541. }
  4542. }
  4543. pc->state= state;
  4544. return END_NOT_FOUND;
  4545. }
  4546. static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){
  4547. MpegEncContext * const s = &h->s;
  4548. AVCodecContext * const avctx= s->avctx;
  4549. int buf_index=0;
  4550. #if 0
  4551. int i;
  4552. for(i=0; i<32; i++){
  4553. printf("%X ", buf[i]);
  4554. }
  4555. #endif
  4556. for(;;){
  4557. int consumed;
  4558. int dst_length;
  4559. int bit_length;
  4560. uint8_t *ptr;
  4561. // start code prefix search
  4562. for(; buf_index + 3 < buf_size; buf_index++){
  4563. // this should allways succeed in the first iteration
  4564. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  4565. break;
  4566. }
  4567. if(buf_index+3 >= buf_size) break;
  4568. buf_index+=3;
  4569. ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, buf_size - buf_index);
  4570. if(ptr[dst_length - 1] == 0) dst_length--;
  4571. bit_length= 8*dst_length - decode_rbsp_trailing(ptr + dst_length - 1);
  4572. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  4573. av_log(h->s.avctx, AV_LOG_DEBUG, "NAL %d at %d length %d\n", h->nal_unit_type, buf_index, dst_length);
  4574. }
  4575. buf_index += consumed;
  4576. if(h->nal_ref_idc < s->hurry_up)
  4577. continue;
  4578. switch(h->nal_unit_type){
  4579. case NAL_IDR_SLICE:
  4580. idr(h); //FIXME ensure we dont loose some frames if there is reordering
  4581. case NAL_SLICE:
  4582. init_get_bits(&s->gb, ptr, bit_length);
  4583. h->intra_gb_ptr=
  4584. h->inter_gb_ptr= &s->gb;
  4585. s->data_partitioning = 0;
  4586. if(decode_slice_header(h) < 0) return -1;
  4587. if(h->redundant_pic_count==0)
  4588. decode_slice(h);
  4589. break;
  4590. case NAL_DPA:
  4591. init_get_bits(&s->gb, ptr, bit_length);
  4592. h->intra_gb_ptr=
  4593. h->inter_gb_ptr= NULL;
  4594. s->data_partitioning = 1;
  4595. if(decode_slice_header(h) < 0) return -1;
  4596. break;
  4597. case NAL_DPB:
  4598. init_get_bits(&h->intra_gb, ptr, bit_length);
  4599. h->intra_gb_ptr= &h->intra_gb;
  4600. break;
  4601. case NAL_DPC:
  4602. init_get_bits(&h->inter_gb, ptr, bit_length);
  4603. h->inter_gb_ptr= &h->inter_gb;
  4604. if(h->redundant_pic_count==0 && h->intra_gb_ptr && s->data_partitioning)
  4605. decode_slice(h);
  4606. break;
  4607. case NAL_SEI:
  4608. break;
  4609. case NAL_SPS:
  4610. init_get_bits(&s->gb, ptr, bit_length);
  4611. decode_seq_parameter_set(h);
  4612. if(s->flags& CODEC_FLAG_LOW_DELAY)
  4613. s->low_delay=1;
  4614. avctx->has_b_frames= !s->low_delay;
  4615. break;
  4616. case NAL_PPS:
  4617. init_get_bits(&s->gb, ptr, bit_length);
  4618. decode_picture_parameter_set(h);
  4619. break;
  4620. case NAL_PICTURE_DELIMITER:
  4621. break;
  4622. case NAL_FILTER_DATA:
  4623. break;
  4624. }
  4625. //FIXME move after where irt is set
  4626. s->current_picture.pict_type= s->pict_type;
  4627. s->current_picture.key_frame= s->pict_type == I_TYPE;
  4628. }
  4629. if(!s->current_picture_ptr) return buf_index; //no frame
  4630. h->prev_frame_num_offset= h->frame_num_offset;
  4631. h->prev_frame_num= h->frame_num;
  4632. if(s->current_picture_ptr->reference){
  4633. h->prev_poc_msb= h->poc_msb;
  4634. h->prev_poc_lsb= h->poc_lsb;
  4635. }
  4636. if(s->current_picture_ptr->reference)
  4637. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  4638. else
  4639. assert(h->mmco_index==0);
  4640. ff_er_frame_end(s);
  4641. MPV_frame_end(s);
  4642. return buf_index;
  4643. }
  4644. /**
  4645. * retunrs the number of bytes consumed for building the current frame
  4646. */
  4647. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  4648. if(s->flags&CODEC_FLAG_TRUNCATED){
  4649. pos -= s->parse_context.last_index;
  4650. if(pos<0) pos=0; // FIXME remove (uneeded?)
  4651. return pos;
  4652. }else{
  4653. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  4654. if(pos+10>buf_size) pos=buf_size; // oops ;)
  4655. return pos;
  4656. }
  4657. }
  4658. static int decode_frame(AVCodecContext *avctx,
  4659. void *data, int *data_size,
  4660. uint8_t *buf, int buf_size)
  4661. {
  4662. H264Context *h = avctx->priv_data;
  4663. MpegEncContext *s = &h->s;
  4664. AVFrame *pict = data;
  4665. int buf_index;
  4666. s->flags= avctx->flags;
  4667. s->flags2= avctx->flags2;
  4668. *data_size = 0;
  4669. /* no supplementary picture */
  4670. if (buf_size == 0) {
  4671. return 0;
  4672. }
  4673. if(s->flags&CODEC_FLAG_TRUNCATED){
  4674. int next= find_frame_end(s, buf, buf_size);
  4675. if( ff_combine_frame(s, next, &buf, &buf_size) < 0 )
  4676. return buf_size;
  4677. //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);
  4678. }
  4679. if(s->avctx->extradata_size && s->picture_number==0){
  4680. if(0 < decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) )
  4681. return -1;
  4682. }
  4683. buf_index=decode_nal_units(h, buf, buf_size);
  4684. if(buf_index < 0)
  4685. return -1;
  4686. //FIXME do something with unavailable reference frames
  4687. // if(ret==FRAME_SKIPED) return get_consumed_bytes(s, buf_index, buf_size);
  4688. #if 0
  4689. if(s->pict_type==B_TYPE || s->low_delay){
  4690. *pict= *(AVFrame*)&s->current_picture;
  4691. } else {
  4692. *pict= *(AVFrame*)&s->last_picture;
  4693. }
  4694. #endif
  4695. if(!s->current_picture_ptr){
  4696. av_log(h->s.avctx, AV_LOG_DEBUG, "error, NO frame\n");
  4697. return -1;
  4698. }
  4699. *pict= *(AVFrame*)&s->current_picture; //FIXME
  4700. ff_print_debug_info(s, pict);
  4701. assert(pict->data[0]);
  4702. //printf("out %d\n", (int)pict->data[0]);
  4703. #if 0 //?
  4704. /* Return the Picture timestamp as the frame number */
  4705. /* we substract 1 because it is added on utils.c */
  4706. avctx->frame_number = s->picture_number - 1;
  4707. #endif
  4708. #if 0
  4709. /* dont output the last pic after seeking */
  4710. if(s->last_picture_ptr || s->low_delay)
  4711. //Note this isnt a issue as a IDR pic should flush teh buffers
  4712. #endif
  4713. *data_size = sizeof(AVFrame);
  4714. return get_consumed_bytes(s, buf_index, buf_size);
  4715. }
  4716. #if 0
  4717. static inline void fill_mb_avail(H264Context *h){
  4718. MpegEncContext * const s = &h->s;
  4719. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4720. if(s->mb_y){
  4721. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  4722. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  4723. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  4724. }else{
  4725. h->mb_avail[0]=
  4726. h->mb_avail[1]=
  4727. h->mb_avail[2]= 0;
  4728. }
  4729. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  4730. h->mb_avail[4]= 1; //FIXME move out
  4731. h->mb_avail[5]= 0; //FIXME move out
  4732. }
  4733. #endif
  4734. #if 0 //selftest
  4735. #define COUNT 8000
  4736. #define SIZE (COUNT*40)
  4737. int main(){
  4738. int i;
  4739. uint8_t temp[SIZE];
  4740. PutBitContext pb;
  4741. GetBitContext gb;
  4742. // int int_temp[10000];
  4743. DSPContext dsp;
  4744. AVCodecContext avctx;
  4745. dsputil_init(&dsp, &avctx);
  4746. init_put_bits(&pb, temp, SIZE);
  4747. printf("testing unsigned exp golomb\n");
  4748. for(i=0; i<COUNT; i++){
  4749. START_TIMER
  4750. set_ue_golomb(&pb, i);
  4751. STOP_TIMER("set_ue_golomb");
  4752. }
  4753. flush_put_bits(&pb);
  4754. init_get_bits(&gb, temp, 8*SIZE);
  4755. for(i=0; i<COUNT; i++){
  4756. int j, s;
  4757. s= show_bits(&gb, 24);
  4758. START_TIMER
  4759. j= get_ue_golomb(&gb);
  4760. if(j != i){
  4761. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  4762. // return -1;
  4763. }
  4764. STOP_TIMER("get_ue_golomb");
  4765. }
  4766. init_put_bits(&pb, temp, SIZE);
  4767. printf("testing signed exp golomb\n");
  4768. for(i=0; i<COUNT; i++){
  4769. START_TIMER
  4770. set_se_golomb(&pb, i - COUNT/2);
  4771. STOP_TIMER("set_se_golomb");
  4772. }
  4773. flush_put_bits(&pb);
  4774. init_get_bits(&gb, temp, 8*SIZE);
  4775. for(i=0; i<COUNT; i++){
  4776. int j, s;
  4777. s= show_bits(&gb, 24);
  4778. START_TIMER
  4779. j= get_se_golomb(&gb);
  4780. if(j != i - COUNT/2){
  4781. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  4782. // return -1;
  4783. }
  4784. STOP_TIMER("get_se_golomb");
  4785. }
  4786. printf("testing 4x4 (I)DCT\n");
  4787. DCTELEM block[16];
  4788. uint8_t src[16], ref[16];
  4789. uint64_t error= 0, max_error=0;
  4790. for(i=0; i<COUNT; i++){
  4791. int j;
  4792. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  4793. for(j=0; j<16; j++){
  4794. ref[j]= random()%255;
  4795. src[j]= random()%255;
  4796. }
  4797. h264_diff_dct_c(block, src, ref, 4);
  4798. //normalize
  4799. for(j=0; j<16; j++){
  4800. // printf("%d ", block[j]);
  4801. block[j]= block[j]*4;
  4802. if(j&1) block[j]= (block[j]*4 + 2)/5;
  4803. if(j&4) block[j]= (block[j]*4 + 2)/5;
  4804. }
  4805. // printf("\n");
  4806. h264_add_idct_c(ref, block, 4);
  4807. /* for(j=0; j<16; j++){
  4808. printf("%d ", ref[j]);
  4809. }
  4810. printf("\n");*/
  4811. for(j=0; j<16; j++){
  4812. int diff= ABS(src[j] - ref[j]);
  4813. error+= diff*diff;
  4814. max_error= FFMAX(max_error, diff);
  4815. }
  4816. }
  4817. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  4818. #if 0
  4819. printf("testing quantizer\n");
  4820. for(qp=0; qp<52; qp++){
  4821. for(i=0; i<16; i++)
  4822. src1_block[i]= src2_block[i]= random()%255;
  4823. }
  4824. #endif
  4825. printf("Testing NAL layer\n");
  4826. uint8_t bitstream[COUNT];
  4827. uint8_t nal[COUNT*2];
  4828. H264Context h;
  4829. memset(&h, 0, sizeof(H264Context));
  4830. for(i=0; i<COUNT; i++){
  4831. int zeros= i;
  4832. int nal_length;
  4833. int consumed;
  4834. int out_length;
  4835. uint8_t *out;
  4836. int j;
  4837. for(j=0; j<COUNT; j++){
  4838. bitstream[j]= (random() % 255) + 1;
  4839. }
  4840. for(j=0; j<zeros; j++){
  4841. int pos= random() % COUNT;
  4842. while(bitstream[pos] == 0){
  4843. pos++;
  4844. pos %= COUNT;
  4845. }
  4846. bitstream[pos]=0;
  4847. }
  4848. START_TIMER
  4849. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  4850. if(nal_length<0){
  4851. printf("encoding failed\n");
  4852. return -1;
  4853. }
  4854. out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
  4855. STOP_TIMER("NAL")
  4856. if(out_length != COUNT){
  4857. printf("incorrect length %d %d\n", out_length, COUNT);
  4858. return -1;
  4859. }
  4860. if(consumed != nal_length){
  4861. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  4862. return -1;
  4863. }
  4864. if(memcmp(bitstream, out, COUNT)){
  4865. printf("missmatch\n");
  4866. return -1;
  4867. }
  4868. }
  4869. printf("Testing RBSP\n");
  4870. return 0;
  4871. }
  4872. #endif
  4873. static int decode_end(AVCodecContext *avctx)
  4874. {
  4875. H264Context *h = avctx->priv_data;
  4876. MpegEncContext *s = &h->s;
  4877. free_tables(h); //FIXME cleanup init stuff perhaps
  4878. MPV_common_end(s);
  4879. // memset(h, 0, sizeof(H264Context));
  4880. return 0;
  4881. }
  4882. AVCodec h264_decoder = {
  4883. "h264",
  4884. CODEC_TYPE_VIDEO,
  4885. CODEC_ID_H264,
  4886. sizeof(H264Context),
  4887. decode_init,
  4888. NULL,
  4889. decode_end,
  4890. decode_frame,
  4891. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  4892. };
  4893. #include "svq3.c"