VideoDecoderDecore.cpp
上传用户:hxb_1234
上传日期:2010-03-30
资源大小:8328k
文件大小:10k
源码类别:

VC书籍

开发平台:

Visual C++

  1. /**************************************************************************************
  2.  *                                                                                    *
  3.  *                                                                                    *
  4.  **************************************************************************************/
  5. #include "VideoDecoderDecore.h"
  6. #include <windows.h>
  7. #include <commctrl.h>
  8. static BOOL APIENTRY DecoreSettingsDlgProc( HWND hDlg, UINT message, UINT wParam, LONG lParam) {
  9. MediaVideoDecoderDecore *dec = (MediaVideoDecoderDecore *) GetWindowLong(hDlg, DWL_USER);
  10. switch(message) {
  11. case WM_INITDIALOG:
  12. SetWindowLong(hDlg, DWL_USER, lParam);
  13. dec = (MediaVideoDecoderDecore *)lParam;
  14. SendMessage(GetDlgItem(hDlg, IDC_DECORE_SLIDER), TBM_SETRANGE, (WPARAM) (BOOL) FALSE, (LPARAM) MAKELONG(0, 6)); 
  15. SendMessage(GetDlgItem(hDlg, IDC_DECORE_SLIDER), TBM_SETPOS, TRUE, dec->postprocessing/10);
  16. return TRUE;
  17. case WM_SYSCOMMAND:
  18. if (wParam == SC_CLOSE)
  19. {
  20. EndDialog (hDlg, TRUE);
  21. return (TRUE);
  22. }
  23. break;
  24. case WM_COMMAND:
  25.         switch (wParam)
  26. {
  27. DEC_SET dec_set;
  28. case ID_DECORE_OK:
  29. dec->postprocessing = 10*SendMessage(GetDlgItem(hDlg, IDC_DECORE_SLIDER), TBM_GETPOS, 0, 0);
  30. dec_set.postproc_level = dec->postprocessing;
  31. if(dec->decoreFunction)
  32. dec->decoreFunction(DECORE_PLAYA_ID, DEC_OPT_SETPP, &dec_set, NULL);
  33. EndDialog (hDlg, TRUE);
  34. break;
  35. case ID_DECORE_APPLY:
  36. dec->postprocessing = 10*SendMessage(GetDlgItem(hDlg, IDC_DECORE_SLIDER), TBM_GETPOS, 0, 0);
  37. dec_set.postproc_level = dec->postprocessing;
  38. if(dec->decoreFunction)
  39. dec->decoreFunction(DECORE_PLAYA_ID, DEC_OPT_SETPP, &dec_set, NULL);
  40. break;
  41. case ID_DECORE_CLOSE :
  42. EndDialog (hDlg, TRUE);
  43. break;
  44. }
  45. break;
  46. case WM_DESTROY:
  47. return TRUE;
  48. }
  49. return FALSE;
  50. }
  51. MediaVideoDecoderDecore::MediaVideoDecoderDecore()
  52. {
  53. this->decaps      = NULL;
  54. this->inputBuffer = new MediaBuffer();
  55. this->invertFlag     = 1;
  56. this->postprocessing = 60;
  57. this->decoreFunction = NULL;
  58. }
  59. MediaVideoDecoderDecore::~MediaVideoDecoderDecore()
  60. {
  61. delete this->inputBuffer;
  62. }
  63. media_type_t  MediaVideoDecoderDecore::GetType()
  64. {
  65. return MEDIA_TYPE_VIDEO_DECODER;
  66. }
  67. char         *MediaVideoDecoderDecore::GetName()
  68. {
  69. return "OpenDivX Video Decoder";
  70. }
  71. MP_RESULT     MediaVideoDecoderDecore::Connect(MediaItem *item)
  72. {
  73. if(item != NULL && item->GetType() == MEDIA_TYPE_DECAPS) {
  74. this->decaps = (MediaItemDecaps *) item;
  75. if(strstr(this->decaps->GetVideoCompression(0), "DIVX") != NULL || 
  76. strstr(this->decaps->GetVideoCompression(0), "divx") != NULL ||
  77. strstr(this->decaps->GetVideoCompression(0), "DVX1") != NULL ||
  78. strstr(this->decaps->GetVideoCompression(0), "dvx1") != NULL ) {
  79. this->hDivXDll       = NULL;
  80. this->decoreFunction = NULL;
  81. this->hDivXDll = LoadLibrary("DivX.dll");
  82. if(this->hDivXDll != NULL) {
  83. this->decoreFunction = (decoreFunc) GetProcAddress(this->hDivXDll, "decore");
  84. if(this->decoreFunction == NULL) {
  85. MP_ERROR("Can't find the "decore" entry point in DivX.dll. Please Install the latest DivX4Windows Package");
  86. this->decaps= NULL;
  87. return MP_RESULT_ERROR;
  88. }
  89. }
  90. else {
  91. MP_ERROR("Please instal the DivX For Windows package first.");
  92. this->decaps= NULL;
  93. return MP_RESULT_ERROR;
  94. }
  95. this->decParam.x_dim         = this->decaps->GetVideoWidth(0);
  96. this->decParam.y_dim         = this->decaps->GetVideoHeight(0);
  97. this->decParam.output_format = DEC_RGB565;
  98. this->decoreFunction(DECORE_PLAYA_ID, DEC_OPT_MEMORY_REQS, (LPVOID)&this->decParam, (LPVOID)&this->decMem);
  99. this->decParam.buffers.mp4_edged_ref_buffers = malloc(this->decMem.mp4_edged_ref_buffers_size);
  100. this->decParam.buffers.mp4_edged_for_buffers = malloc(this->decMem.mp4_edged_for_buffers_size);
  101. this->decParam.buffers.mp4_display_buffers   = malloc(this->decMem.mp4_display_buffers_size);
  102. this->decParam.buffers.mp4_state             = malloc(this->decMem.mp4_state_size);
  103. this->decParam.buffers.mp4_tables            = malloc(this->decMem.mp4_tables_size);
  104. this->decParam.buffers.mp4_stream            = malloc(this->decMem.mp4_stream_size);
  105. memset(this->decParam.buffers.mp4_state,  0, this->decMem.mp4_state_size);
  106. memset(this->decParam.buffers.mp4_tables, 0, this->decMem.mp4_tables_size);
  107. memset(this->decParam.buffers.mp4_stream, 0, this->decMem.mp4_stream_size);
  108. this->decoreFunction(DECORE_PLAYA_ID, DEC_OPT_INIT, (LPVOID)&this->decParam, NULL);
  109. this->inputBuffer->Alloc(DECORE_INPUT_SIZE);
  110. this->videoMode = VIDEO_MODE_UYVY;
  111. DEC_SET dec_set;
  112. dec_set.postproc_level = this->postprocessing;
  113. this->decoreFunction(DECORE_PLAYA_ID, DEC_OPT_SETPP, &dec_set, NULL);
  114. return MP_RESULT_OK;
  115. }
  116. }
  117. this->decaps = NULL;
  118. return MP_RESULT_ERROR;
  119. }
  120. MP_RESULT     MediaVideoDecoderDecore::ReleaseConnections()
  121. {
  122. /*
  123.  * Do Cleanup here!
  124.  */
  125. this->decaps = NULL;
  126. this->inputBuffer->Free();
  127. if(this->decoreFunction)
  128. this->decoreFunction(DECORE_PLAYA_ID, DEC_OPT_RELEASE, NULL, NULL);
  129. if(this->hDivXDll) {
  130. FreeLibrary(this->hDivXDll);
  131. this->hDivXDll       = NULL;
  132. this->decoreFunction = NULL;
  133. }
  134. return MP_RESULT_OK;
  135. }
  136. DWORD         MediaVideoDecoderDecore::GetCaps()
  137. {
  138. return MEDIA_CAPS_CAN_CONFIGURE;
  139. }
  140. MP_RESULT     MediaVideoDecoderDecore::Configure(HINSTANCE hInstance, HWND hwnd)
  141. {
  142. HWND hDlg;
  143.     hDlg = CreateDialogParam(hInstance, (LPCSTR)MAKEINTRESOURCE(IDD_DECORE_SETTINGS), hwnd, (DLGPROC) DecoreSettingsDlgProc, (LPARAM)(LPVOID)this);
  144.     
  145.     ShowWindow(hDlg, SW_SHOW);
  146. return MP_RESULT_OK;
  147. }
  148. unsigned int MediaVideoDecoderDecore::GetFrameSize() 
  149. {
  150. switch(this->videoMode) {
  151. case VIDEO_MODE_RGB8:
  152. case VIDEO_MODE_YUV9:
  153. return 0;
  154. break;
  155. case VIDEO_MODE_RGB16:
  156. return this->decParam.x_dim*this->decParam.y_dim*2;
  157. break;
  158. case VIDEO_MODE_RGB24:
  159. return this->decParam.x_dim*this->decParam.y_dim*3;
  160. break;
  161. case VIDEO_MODE_RGB32:
  162. return this->decParam.x_dim*this->decParam.y_dim*4;
  163. break;
  164. case VIDEO_MODE_YUV12:
  165. return this->decParam.x_dim*this->decParam.y_dim*12/8;
  166. break;
  167. case VIDEO_MODE_YUY2:
  168. return this->decParam.x_dim*this->decParam.y_dim*2;
  169. break;
  170. case VIDEO_MODE_UYVY:
  171. return this->decParam.x_dim*this->decParam.y_dim*2;
  172. break;
  173. }
  174. return 0;
  175. }
  176. media_video_mode_t MediaVideoDecoderDecore::GetVideoMode() 
  177. {
  178. return this->videoMode;
  179. }
  180. BOOL MediaVideoDecoderDecore::GetInvertFlag() 
  181. {
  182. return this->invertFlag;
  183. }
  184. MP_RESULT          MediaVideoDecoderDecore::SetQuality(DWORD quality)
  185. {
  186. this->postprocessing = quality;
  187. if(this->decoreFunction) {
  188. DEC_SET dec_set;
  189. dec_set.postproc_level = this->postprocessing;
  190. this->decoreFunction(DECORE_PLAYA_ID, DEC_OPT_SETPP, &dec_set, NULL);
  191. }
  192. return MP_RESULT_OK;
  193. }
  194. DWORD          MediaVideoDecoderDecore::GetQuality()
  195. {
  196. return this->postprocessing;
  197. }
  198. MP_RESULT          MediaVideoDecoderDecore::SetVideoMode(media_video_mode_t mode)
  199. {
  200. if(this->decoreFunction == NULL) {
  201. return MP_RESULT_ERROR;
  202. }
  203. this->videoMode = mode;
  204. switch(mode) {
  205. case VIDEO_MODE_RGB8:
  206. case VIDEO_MODE_YUV9:
  207. return MP_RESULT_ERROR;
  208. break;
  209. case VIDEO_MODE_RGB16:
  210. this->decParam.output_format = DEC_RGB565;
  211. this->invertFlag = 1;
  212. if(this->decoreFunction(DECORE_PLAYA_ID, DEC_OPT_INIT, (LPVOID)&this->decParam, NULL) == DEC_OK)
  213. return MP_RESULT_OK;
  214. break;
  215. case VIDEO_MODE_RGB24:
  216. this->decParam.output_format = DEC_RGB24;
  217. this->invertFlag = 1;
  218. if(this->decoreFunction(DECORE_PLAYA_ID, DEC_OPT_INIT, (LPVOID)&this->decParam, NULL) == DEC_OK)
  219. return MP_RESULT_OK;;
  220. break;
  221. case VIDEO_MODE_RGB32:
  222. this->decParam.output_format = DEC_RGB32;
  223. this->invertFlag = 1;
  224. if(this->decoreFunction(DECORE_PLAYA_ID, DEC_OPT_INIT, (LPVOID)&this->decParam, NULL) == DEC_OK)
  225. return MP_RESULT_OK;;
  226. break;
  227. case VIDEO_MODE_YUV12:
  228. this->decParam.output_format = DEC_420;
  229. this->invertFlag = 0;
  230. if(this->decoreFunction(DECORE_PLAYA_ID, DEC_OPT_INIT, (LPVOID)&this->decParam, NULL) == DEC_OK)
  231. return MP_RESULT_OK;;
  232. break;
  233. case VIDEO_MODE_YUY2:
  234. this->decParam.output_format = DEC_YUV2;
  235. this->invertFlag = 0;
  236. if(this->decoreFunction(DECORE_PLAYA_ID, DEC_OPT_INIT, (LPVOID)&this->decParam, NULL) == DEC_OK)
  237. return MP_RESULT_OK;;
  238. break;
  239. case VIDEO_MODE_UYVY:
  240. this->decParam.output_format = DEC_UYVY;
  241. this->invertFlag = 0;
  242. if(this->decoreFunction(DECORE_PLAYA_ID, DEC_OPT_INIT, (LPVOID)&this->decParam, NULL) == DEC_OK)
  243. return MP_RESULT_OK;;
  244. break;
  245. }
  246. return MP_RESULT_ERROR;
  247. }
  248. MP_RESULT          MediaVideoDecoderDecore::Decompress(MediaBuffer *mb_out, unsigned int stride)
  249. {
  250. unsigned int size;
  251. if(this->decaps && mb_out && this->decoreFunction) {
  252. size = this->decaps->GetNextVideoFrameSize(0);
  253. if(size > this->inputBuffer->GetSize())
  254. this->inputBuffer->ReAlloc(size);
  255. if(this->decaps->ReadVideoFrame(0, this->inputBuffer) != MP_RESULT_OK) {
  256. return MP_RESULT_ERROR;
  257. }
  258. this->decFrame.length      = size;
  259. this->decFrame.bitstream   = this->inputBuffer->GetData();
  260. this->decFrame.bmp         = mb_out->GetData();
  261. this->decFrame.stride      = stride;
  262. this->decFrame.render_flag = 1;
  263.  
  264. if(size != 0) {
  265. this->decoreFunction(DECORE_PLAYA_ID, 0, &this->decFrame, NULL);
  266. }
  267. return MP_RESULT_OK;
  268. }
  269. return MP_RESULT_ERROR;
  270. }
  271. MP_RESULT          MediaVideoDecoderDecore::Drop(MediaBuffer *mb_out, unsigned int stride)
  272. {
  273. unsigned int size;
  274. if(this->decaps && mb_out && this->decoreFunction) {
  275. size = this->decaps->GetNextVideoFrameSize(0);
  276. if(size > this->inputBuffer->GetSize())
  277. this->inputBuffer->ReAlloc(size);
  278. this->decaps->ReadVideoFrame(0, this->inputBuffer);
  279. this->decFrame.length      = size;
  280. this->decFrame.bitstream   = this->inputBuffer->GetData();
  281. this->decFrame.bmp         = mb_out->GetData();
  282. this->decFrame.render_flag = 0;
  283.   
  284. this->decoreFunction(DECORE_PLAYA_ID, 0, &this->decFrame, NULL);
  285. }
  286. return MP_RESULT_ERROR;
  287. }