dec_api.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #include "dec_api.h"
  2. #include "global.h"
  3. #include "nalu.h"
  4. #include "image.h"
  5. #include "meminput.h"
  6. #include "output.h"
  7. #include "fmo.h"
  8. #include "erc_api.h"
  9. #include "parset.h"
  10. #include "memcache.h"
  11. #include "block.h"
  12. #include "optim.h"
  13. #include "mc_prediction.h"
  14. #include "vlc.h"
  15. #include <stddef.h> // for offsetof
  16. #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  17. OptimizedFunctions opt;
  18. DecoderParams *alloc_decoder();
  19. void Configure(VideoParameters *p_Vid, InputParameters *p_Inp);
  20. void malloc_slice(InputParameters *p_Inp, VideoParameters *p_Vid);
  21. void init (VideoParameters *p_Vid);
  22. void free_slice (Slice *currSlice);
  23. void free_img( VideoParameters *p_Vid);
  24. int sse2_flag = 0, mmx_flag=0, sse_flag=0, sse3_flag=0, sse4_1_flag=0;
  25. int H264_Init()
  26. {
  27. int flags_edx, flags_ecx;
  28. #ifdef H264_IPP
  29. ippStaticInit();
  30. #endif
  31. #ifdef _M_IX86
  32. _asm {
  33. mov eax, 1
  34. cpuid
  35. mov flags_edx, edx
  36. mov flags_ecx, ecx
  37. }
  38. mmx_flag = flags_edx & 0x00800000;
  39. sse_flag = flags_edx & 0x02000000;
  40. sse2_flag = flags_edx & 0x04000000;
  41. sse3_flag = flags_ecx & 0x00000001;
  42. sse4_1_flag= flags_ecx & (1 << 19);
  43. #elif defined(_M_X64)
  44. sse2_flag = 1;
  45. #endif
  46. #ifdef _M_IX86
  47. /* if you get any compile errors here, you need to change biari.asm */
  48. BUILD_BUG_ON(offsetof(TextureInfoContexts, map_contexts) != 436);
  49. BUILD_BUG_ON(offsetof(TextureInfoContexts, last_contexts) != 3252);
  50. BUILD_BUG_ON(offsetof(TextureInfoContexts, one_contexts) != 6068);
  51. BUILD_BUG_ON(offsetof(TextureInfoContexts, abs_contexts) != 6508);
  52. BUILD_BUG_ON(offsetof(Macroblock, p_Slice) != 0);
  53. BUILD_BUG_ON(offsetof(Macroblock, p_Vid) != 4);
  54. BUILD_BUG_ON(offsetof(Macroblock, qp) != 60);
  55. BUILD_BUG_ON(offsetof(Macroblock, qpc) != 64);
  56. BUILD_BUG_ON(offsetof(Macroblock, qp_scaled) != 72);
  57. BUILD_BUG_ON(offsetof(Macroblock, cbp_blk) != 248);
  58. BUILD_BUG_ON(offsetof(Macroblock, mb_field) != 344);
  59. BUILD_BUG_ON(offsetof(Macroblock, read_and_store_CBP_block_bit) != 400);
  60. BUILD_BUG_ON(offsetof(Slice, tex_ctx) != 100);
  61. BUILD_BUG_ON(offsetof(Slice, mb_rec) != 1696);
  62. BUILD_BUG_ON(offsetof(Slice, mb_pred) != 928);
  63. BUILD_BUG_ON(offsetof(Slice, coeff) != 15632);
  64. BUILD_BUG_ON(offsetof(Slice, coeff_ctr) != 15760);
  65. BUILD_BUG_ON(offsetof(Slice, pos) != 15764);
  66. BUILD_BUG_ON(offsetof(Slice, cof) != 2464);
  67. BUILD_BUG_ON(offsetof(Slice, last_dquant) != 88);
  68. BUILD_BUG_ON(offsetof(Slice, mot_ctx) != 96);
  69. BUILD_BUG_ON(offsetof(Slice, slice_type) != 64);
  70. BUILD_BUG_ON(offsetof(StorablePicture, structure) != 0);
  71. BUILD_BUG_ON(offsetof(StorablePicture, chroma_qp_offset) != 158688);
  72. BUILD_BUG_ON(offsetof(StorablePicture, motion) != 158524);
  73. BUILD_BUG_ON(offsetof(StorablePicture, plane_images) != 158512);
  74. BUILD_BUG_ON(offsetof(StorablePicture, imgY) != 158512);
  75. BUILD_BUG_ON(offsetof(VideoParameters, structure) != 697200);
  76. BUILD_BUG_ON(offsetof(VideoParameters, bitdepth_chroma_qp_scale) != 697456);
  77. BUILD_BUG_ON(offsetof(VideoParameters, dec_picture) != 698192);
  78. BUILD_BUG_ON(offsetof(DecodingEnvironment, Dcodestrm_len) != 16);
  79. BUILD_BUG_ON(offsetof(DecodingEnvironment, Dcodestrm) != 12);
  80. BUILD_BUG_ON(offsetof(DecodingEnvironment, DbitsLeft) != 8);
  81. BUILD_BUG_ON(offsetof(DecodingEnvironment, Dvalue) != 4);
  82. BUILD_BUG_ON(offsetof(DecodingEnvironment, Drange) != 0);
  83. BUILD_BUG_ON(sizeof(BiContextType) != 4);
  84. BUILD_BUG_ON(offsetof(BiContextType, state) != 0);
  85. BUILD_BUG_ON(offsetof(BiContextType, MPS) != 2);
  86. BUILD_BUG_ON(offsetof(OptimizedFunctions, copy_image_data_16x16_stride) != 32);
  87. #endif
  88. if (sse2_flag)
  89. {
  90. //opt.itrans4x4 = itrans4x4_mmx;
  91. opt.itrans8x8 = itrans8x8_sse2;
  92. opt.weighted_mc_prediction16x16 = weighted_mc_prediction16x16_sse2;
  93. opt.weighted_mc_prediction16x8 = weighted_mc_prediction16x8_sse2;
  94. opt.weighted_mc_prediction8x8 = weighted_mc_prediction8x8_sse2;
  95. opt.weighted_bi_prediction16x16 = weighted_bi_prediction16x16_sse2;
  96. opt.weighted_bi_prediction16x8 = weighted_bi_prediction16x8_sse2;
  97. opt.weighted_bi_prediction8x8 = weighted_bi_prediction8x8_sse2;
  98. opt.bi_prediction8x8 = bi_prediction8x8_sse2;
  99. opt.copy_image_data_16x16_stride = copy_image_data_16x16_stride_sse;
  100. opt.code_from_bitstream_2d_5_4 = code_from_bitstream_2d_5_4_sse2;
  101. opt.code_from_bitstream_2d_17_4 = code_from_bitstream_2d_17_4_sse2;
  102. opt.code_from_bitstream_2d_16_1 = code_from_bitstream_2d_16_1_sse2;
  103. }
  104. else if (sse_flag && mmx_flag)
  105. {
  106. //opt.itrans4x4 = itrans4x4_mmx;
  107. opt.itrans8x8 = itrans8x8_c;//itrans8x8_mmx;
  108. opt.weighted_mc_prediction16x16 = weighted_mc_prediction16x16_ipp;
  109. opt.weighted_mc_prediction16x8 = weighted_mc_prediction16x8_ipp;
  110. opt.weighted_mc_prediction8x8 = weighted_mc_prediction8x8_ipp;
  111. opt.weighted_bi_prediction16x16 = weighted_bi_prediction16x16_ipp;
  112. opt.weighted_bi_prediction16x8 = weighted_bi_prediction16x8_ipp;
  113. opt.weighted_bi_prediction8x8 = weighted_bi_prediction8x8_ipp;
  114. opt.bi_prediction8x8 = bi_prediction8x8_ipp;
  115. opt.copy_image_data_16x16_stride = copy_image_data_16x16_stride_sse;
  116. opt.code_from_bitstream_2d_5_4 = code_from_bitstream_2d_5_4_c;
  117. opt.code_from_bitstream_2d_17_4 = code_from_bitstream_2d_17_4_c;
  118. opt.code_from_bitstream_2d_16_1 = code_from_bitstream_2d_16_1_c;
  119. }
  120. else
  121. return 0;
  122. return 1;
  123. }
  124. h264_decoder_t H264_CreateDecoder()
  125. {
  126. DecoderParams *decoder=alloc_decoder();
  127. if (decoder)
  128. {
  129. InputParameters *p_Inp = decoder->p_Inp;
  130. Configure(decoder->p_Vid, p_Inp);
  131. p_Inp->intra_profile_deblocking = 1;
  132. initBitsFile(decoder->p_Vid);
  133. malloc_slice(decoder->p_Inp, decoder->p_Vid);
  134. init_old_slice(decoder->p_Vid->old_slice);
  135. init(decoder->p_Vid);
  136. init_out_buffer(decoder->p_Vid);
  137. decoder->p_Vid->current_mb_nr = -4711; // initialized to an impossible value for debugging -- correct value is taken from slice header
  138. }
  139. return decoder;
  140. }
  141. void H264_DestroyDecoder(h264_decoder_t d)
  142. {
  143. DecoderParams *decoder = (DecoderParams *)d;
  144. if (decoder)
  145. {
  146. free_slice(decoder->p_Vid->currentSlice);
  147. FmoFinit(decoder->p_Vid);
  148. free_global_buffers(decoder->p_Vid);
  149. flush_dpb(decoder->p_Vid);
  150. #if (PAIR_FIELDS_IN_OUTPUT)
  151. flush_pending_output(decoder->p_Vid);
  152. #endif
  153. out_storable_pictures_destroy(decoder->p_Vid);
  154. ercClose(decoder->p_Vid, decoder->p_Vid->erc_errorVar);
  155. CleanUpPPS(decoder->p_Vid);
  156. free_dpb(decoder->p_Vid);
  157. uninit_out_buffer(decoder->p_Vid);
  158. image_cache_flush(&decoder->p_Vid->image_cache[0]);
  159. image_cache_flush(&decoder->p_Vid->image_cache[1]);
  160. motion_cache_flush(&decoder->p_Vid->motion_cache);
  161. FreeNALU(decoder->p_Vid->nalu);
  162. free (decoder->p_Inp);
  163. free_img (decoder->p_Vid);
  164. free(decoder);
  165. }
  166. }
  167. void H264_DecodeFrame(h264_decoder_t d, const void *buffer, size_t bufferlen, uint64_t time_code)
  168. {
  169. DecoderParams *decoder = (DecoderParams *)d;
  170. int ret;
  171. memory_input_t *mem_input = decoder->p_Vid->mem_input;
  172. mem_input->user_buffer=buffer;
  173. mem_input->user_buffer_size=bufferlen;
  174. mem_input->user_buffer_read=0;
  175. __try
  176. {
  177. ret = decode_one_frame(decoder->p_Vid, time_code);
  178. }
  179. __except(EXCEPTION_EXECUTE_HANDLER)
  180. {
  181. int x;
  182. x=0;
  183. }
  184. #ifdef _M_IX86
  185. _mm_empty();
  186. #endif
  187. }
  188. void H264_GetPicture(h264_decoder_t d, StorablePicture **pic)
  189. {
  190. DecoderParams *decoder = (DecoderParams *)d;
  191. if (pic)
  192. {
  193. out_storable_picture_get(decoder->p_Vid, pic);
  194. }
  195. }
  196. static double GetAspectRatio(const vui_seq_parameters_t *vui)
  197. {
  198. int aspect_ratio_width=1, aspect_ratio_height=1;
  199. if (vui->aspect_ratio_info_present_flag)
  200. {
  201. switch(vui->aspect_ratio_idc)
  202. {
  203. case VUI_AR_UNDEFINED:
  204. case VUI_AR_SQUARE:
  205. aspect_ratio_width = 1;
  206. aspect_ratio_height = 1;
  207. break;
  208. case VUI_AR_12_11:
  209. aspect_ratio_width = 12;
  210. aspect_ratio_height = 11;
  211. break;
  212. case VUI_AR_10_11:
  213. aspect_ratio_width = 10;
  214. aspect_ratio_height = 11;
  215. break;
  216. case VUI_AR_16_11:
  217. aspect_ratio_width = 16;
  218. aspect_ratio_height = 11;
  219. break;
  220. case VUI_AR_40_33:
  221. aspect_ratio_width = 40;
  222. aspect_ratio_height = 33;
  223. break;
  224. case VUI_AR_24_11:
  225. aspect_ratio_width = 24;
  226. aspect_ratio_height = 11;
  227. break;
  228. case VUI_AR_20_11:
  229. aspect_ratio_width = 20;
  230. aspect_ratio_height = 11;
  231. break;
  232. case VUI_AR_32_11:
  233. aspect_ratio_width = 32;
  234. aspect_ratio_height = 11;
  235. break;
  236. case VUI_AR_80_33:
  237. aspect_ratio_width = 80;
  238. aspect_ratio_height = 33;
  239. break;
  240. case VUI_AR_18_11:
  241. aspect_ratio_width = 18;
  242. aspect_ratio_height = 11;
  243. break;
  244. case VUI_AR_15_11:
  245. aspect_ratio_width = 15;
  246. aspect_ratio_height = 11;
  247. break;
  248. case VUI_AR_64_33:
  249. aspect_ratio_width = 64;
  250. aspect_ratio_height = 33;
  251. break;
  252. case VUI_AR_160_99:
  253. aspect_ratio_width = 160;
  254. aspect_ratio_height = 99;
  255. break;
  256. case VUI_AR_4_3:
  257. aspect_ratio_width = 4;
  258. aspect_ratio_height = 3;
  259. break;
  260. case VUI_AR_3_2:
  261. aspect_ratio_width = 3;
  262. aspect_ratio_height = 2;
  263. break;;
  264. case VUI_AR_2_1:
  265. aspect_ratio_width = 2;
  266. aspect_ratio_height = 1;
  267. break;;
  268. case VUI_EXTENDED_SAR:
  269. default:
  270. aspect_ratio_width = vui->sar_width;
  271. aspect_ratio_height = vui->sar_height;
  272. break;
  273. }
  274. }
  275. return (double)aspect_ratio_width / (double)aspect_ratio_height;
  276. }
  277. const FrameFormat *H264_GetOutputFormat(h264_decoder_t d, double *aspect_ratio)
  278. {
  279. DecoderParams *decoder = (DecoderParams *)d;
  280. if (decoder && decoder->p_Inp)
  281. {
  282. if (decoder->p_Vid->active_sps)
  283. *aspect_ratio = GetAspectRatio(&decoder->p_Vid->active_sps->vui_seq_parameters);
  284. return &decoder->p_Inp->output;
  285. }
  286. else
  287. return 0;
  288. }
  289. void H264_Flush(h264_decoder_t d)
  290. {
  291. DecoderParams *decoder = (DecoderParams *)d;
  292. if (decoder && decoder->p_Vid)
  293. {
  294. StorablePicture *pic=0;
  295. exit_picture(decoder->p_Vid, &decoder->p_Vid->dec_picture);
  296. if (pic)
  297. free_storable_picture(decoder->p_Vid, pic);
  298. pic=0;
  299. decoder->p_Vid->frame_num = 0;
  300. decoder->p_Vid->pre_frame_num = INT_MIN;
  301. decoder->p_Vid->PreviousFrameNum=0;
  302. decoder->p_Vid->PreviousFrameNumOffset = 0;
  303. decoder->p_Vid->PrevPicOrderCntLsb = 0;
  304. decoder->p_Vid->PrevPicOrderCntMsb = 0;
  305. flush_dpb(decoder->p_Vid);
  306. do
  307. {
  308. pic=0;
  309. out_storable_picture_get(decoder->p_Vid, &pic);
  310. if (pic)
  311. free_storable_picture(decoder->p_Vid, pic);
  312. } while (pic);
  313. decoder->p_Vid->mem_input->resetting = 1;
  314. }
  315. }
  316. void H264_FreePicture(h264_decoder_t d, StorablePicture *p)
  317. {
  318. DecoderParams *decoder = (DecoderParams *)d;
  319. if (decoder && decoder->p_Vid && p)
  320. {
  321. free_storable_picture(decoder->p_Vid, p);
  322. }
  323. }
  324. void H264_EndOfStream(h264_decoder_t d)
  325. {
  326. DecoderParams *decoder = (DecoderParams *)d;
  327. if (decoder && decoder->p_Vid)
  328. {
  329. if (decoder->p_Vid->dec_picture)
  330. exit_picture(decoder->p_Vid, &decoder->p_Vid->dec_picture);
  331. else
  332. flush_dpb(decoder->p_Vid);
  333. }
  334. }
  335. void H264_HurryUp(h264_decoder_t d, int state)
  336. {
  337. DecoderParams *decoder = (DecoderParams *)d;
  338. if (decoder && decoder->p_Vid)
  339. {
  340. memory_input_t *mem_input = decoder->p_Vid->mem_input;
  341. if (mem_input)
  342. mem_input->skip_b_frames = state;
  343. }
  344. }