GoProStreamDemo.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /* GoProStreamDemo.cpp/Open GoPro, Version 2.0 (C) Copyright 2021 GoPro, Inc. (http://gopro.com/OpenGoPro). */
  2. /* This copyright was auto-generated on Sat Mar 5 01:05:52 UTC 2022 */
  3. // GoProStreamDemo.cpp : Defines the entry point for the application.
  4. //
  5. #include "stdafx.h"
  6. #include "GoProStreamDemo.h"
  7. #include <thread>
  8. #include "UDPSocketCapture.h"
  9. #include <d2d1.h>
  10. extern "C"
  11. {
  12. #include "libavcodec\avcodec.h"
  13. }
  14. #define BUF_SIZE_1080 3110400 // 1920 * 1080 + (1920 *1080)/2
  15. #define BUF_SIZE_720 1382400 // 1280 * 720 + (1280 *720)/2
  16. int BUF_SIZE = BUF_SIZE_1080;
  17. int HEIGHT = 1080;
  18. int WIDTH = 1920;
  19. #ifndef max
  20. #define max(a,b) (((a) > (b)) ? (a) : (b))
  21. #endif
  22. #ifndef min
  23. #define min(a,b) (((a) < (b)) ? (a) : (b))
  24. #endif
  25. int quitNow = 0;
  26. HWND myWin = 0;
  27. std::thread myThread;
  28. std::thread capThread;
  29. HBITMAP myBitmap;
  30. COLORREF* imgBuf = (COLORREF*)calloc(2073600, sizeof(COLORREF));
  31. void yuv2rgb(uint8_t yValue, uint8_t uValue, uint8_t vValue,
  32. uint8_t* r, uint8_t* g, uint8_t* b);
  33. void CaptureFrames();
  34. #define MAX_LOADSTRING 100
  35. // Global Variables:
  36. HINSTANCE hInst; // current instance
  37. WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
  38. WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
  39. // Forward declarations of functions included in this code module:
  40. ATOM MyRegisterClass(HINSTANCE hInstance);
  41. BOOL InitInstance(HINSTANCE, int);
  42. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  43. INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
  44. UDPSocketCapture capper;
  45. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  46. _In_opt_ HINSTANCE hPrevInstance,
  47. _In_ LPWSTR lpCmdLine,
  48. _In_ int nCmdShow)
  49. {
  50. UNREFERENCED_PARAMETER(hPrevInstance);
  51. UNREFERENCED_PARAMETER(lpCmdLine);
  52. // Initialize global strings
  53. LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  54. LoadStringW(hInstance, IDC_GOPROSTREAMDEMO, szWindowClass, MAX_LOADSTRING);
  55. MyRegisterClass(hInstance);
  56. // Perform application initialization:
  57. if (!InitInstance (hInstance, nCmdShow))
  58. {
  59. return FALSE;
  60. }
  61. HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_GOPROSTREAMDEMO));
  62. #ifdef USE_PREVIEW_STREAM
  63. HEIGHT = 720;
  64. WIDTH = 1280;
  65. BUF_SIZE = BUF_SIZE_720;
  66. #endif
  67. MSG msg;
  68. capper.Start();
  69. capThread = std::thread(CaptureFrames);
  70. capThread.detach();
  71. // Main message loop:
  72. while (GetMessage(&msg, nullptr, 0, 0))
  73. {
  74. if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  75. {
  76. TranslateMessage(&msg);
  77. DispatchMessage(&msg);
  78. }
  79. }
  80. capper.Stop();
  81. return (int) msg.wParam;
  82. }
  83. //
  84. // FUNCTION: MyRegisterClass()
  85. //
  86. // PURPOSE: Registers the window class.
  87. //
  88. ATOM MyRegisterClass(HINSTANCE hInstance)
  89. {
  90. WNDCLASSEXW wcex;
  91. wcex.cbSize = sizeof(WNDCLASSEX);
  92. wcex.style = CS_HREDRAW | CS_VREDRAW;
  93. wcex.lpfnWndProc = WndProc;
  94. wcex.cbClsExtra = 0;
  95. wcex.cbWndExtra = 0;
  96. wcex.hInstance = hInstance;
  97. wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_GOPROSTREAMDEMO));
  98. wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
  99. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  100. wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_GOPROSTREAMDEMO);
  101. wcex.lpszClassName = szWindowClass;
  102. wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  103. return RegisterClassExW(&wcex);
  104. }
  105. //
  106. // FUNCTION: InitInstance(HINSTANCE, int)
  107. //
  108. // PURPOSE: Saves instance handle and creates main window
  109. //
  110. // COMMENTS:
  111. //
  112. // In this function, we save the instance handle in a global variable and
  113. // create and display the main program window.
  114. //
  115. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  116. {
  117. hInst = hInstance; // Store instance handle in our global variable
  118. HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  119. CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
  120. if (!hWnd)
  121. {
  122. return FALSE;
  123. }
  124. myWin = hWnd;
  125. ShowWindow(hWnd, nCmdShow);
  126. UpdateWindow(hWnd);
  127. return TRUE;
  128. }
  129. //
  130. // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  131. //
  132. // PURPOSE: Processes messages for the main window.
  133. //
  134. // WM_COMMAND - process the application menu
  135. // WM_PAINT - Paint the main window
  136. // WM_DESTROY - post a quit message and return
  137. //
  138. //
  139. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  140. {
  141. switch (message)
  142. {
  143. case WM_COMMAND:
  144. {
  145. int wmId = LOWORD(wParam);
  146. // Parse the menu selections:
  147. switch (wmId)
  148. {
  149. case IDM_ABOUT:
  150. DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  151. break;
  152. case IDM_EXIT:
  153. DestroyWindow(hWnd);
  154. break;
  155. default:
  156. return DefWindowProc(hWnd, message, wParam, lParam);
  157. }
  158. }
  159. break;
  160. case WM_PAINT:
  161. {
  162. RECT rect;
  163. int width;
  164. int height;
  165. if (GetWindowRect(hWnd, &rect))
  166. {
  167. width = rect.right - rect.left;
  168. height = rect.bottom - rect.top;
  169. }
  170. PAINTSTRUCT ps;
  171. HDC hdc = BeginPaint(hWnd, &ps);
  172. // TODO: Add any drawing code that uses hdc here...
  173. /////////////////
  174. #ifdef SHOW_CAM_OUTPUT
  175. // Creating temp bitmap
  176. HBITMAP map = CreateBitmap(WIDTH,
  177. HEIGHT,
  178. 1,
  179. 32,
  180. (void*)imgBuf);
  181. HDC src = CreateCompatibleDC(hdc);
  182. SelectObject(src, map);
  183. BitBlt(hdc,
  184. 10,
  185. 10,
  186. WIDTH,
  187. HEIGHT,
  188. src,
  189. 0,
  190. 0,
  191. SRCCOPY);
  192. DeleteObject(map);
  193. DeleteDC(src);
  194. #endif
  195. EndPaint(hWnd, &ps);
  196. }
  197. break;
  198. case WM_DESTROY:
  199. quitNow = 1;
  200. PostQuitMessage(0);
  201. break;
  202. default:
  203. return DefWindowProc(hWnd, message, wParam, lParam);
  204. }
  205. return 0;
  206. }
  207. // Message handler for about box.
  208. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  209. {
  210. UNREFERENCED_PARAMETER(lParam);
  211. switch (message)
  212. {
  213. case WM_INITDIALOG:
  214. return (INT_PTR)TRUE;
  215. case WM_COMMAND:
  216. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  217. {
  218. EndDialog(hDlg, LOWORD(wParam));
  219. return (INT_PTR)TRUE;
  220. }
  221. break;
  222. }
  223. return (INT_PTR)FALSE;
  224. }
  225. void CaptureFrames()
  226. {
  227. uint8_t* buf = (uint8_t*)malloc(BUF_SIZE_1080);
  228. static int countstart = 0;
  229. long bytesReturned = 0;
  230. //std::this_thread::sleep_for(std::chrono::seconds(6));
  231. while (!quitNow)
  232. {
  233. capper.GetBuffer(buf, BUF_SIZE, bytesReturned);
  234. if (bytesReturned > 100)
  235. {
  236. //for 1080 frames
  237. //0-2073599 = Y
  238. //2073600 - 2591999 = U
  239. //2592000 - 3110399 = V
  240. uint8_t r, g, b;
  241. int uindex = HEIGHT*WIDTH, vindex = HEIGHT*WIDTH + ((HEIGHT*WIDTH)/4);
  242. int strider = 0;
  243. #ifdef SHOW_CAM_OUTPUT
  244. #ifndef SHOW_CAM_OUTPUT_ORIGINAL
  245. for (int i = 0; i < HEIGHT * WIDTH; i++)
  246. {
  247. int lineNumber = i / WIDTH;
  248. int place = i % WIDTH;
  249. int oddline = ((lineNumber) % 2);
  250. strider = ((place) / 2) + (((lineNumber / 2) + (lineNumber%2)) * (WIDTH/2));
  251. if (oddline)
  252. strider -= (WIDTH/2);
  253. if (place == WIDTH-1)
  254. strider += 0;
  255. yuv2rgb(buf[i], buf[uindex + strider], buf[vindex+strider], &r, &g, &b);
  256. imgBuf[i] = RGB(b, g, r);
  257. //imgBuf[i] = RGB(buf[i], buf[i], buf[i]);
  258. }
  259. #endif
  260. #endif
  261. #ifdef SHOW_CAM_OUTPUT
  262. #ifdef SHOW_CAM_OUTPUT_ORIGINAL
  263. //this is the Y channel
  264. for (int j = 0, i = 0; i < HEIGHT*WIDTH; j++, i++)
  265. {
  266. imgBuf[j] = RGB(buf[i], buf[i], buf[i]);
  267. }
  268. //uncomment this to see UV channels
  269. /*
  270. for (int j = 0, i = HEIGHT*WIDTH; i < BUF_SIZE; j++, i++)
  271. {
  272. imgBuf[j] = RGB(buf[i], buf[i], buf[i]);
  273. }
  274. */
  275. #endif
  276. #endif
  277. #ifdef SHOW_CAM_OUTPUT
  278. InvalidateRect(myWin, NULL, false);
  279. UpdateWindow(myWin);
  280. #endif
  281. }
  282. else
  283. {
  284. bytesReturned = 0;
  285. }
  286. std::this_thread::sleep_for(std::chrono::milliseconds(20));
  287. }
  288. free(buf);
  289. }
  290. void yuv2rgb(uint8_t yValue, uint8_t uValue, uint8_t vValue,
  291. uint8_t* r, uint8_t* g, uint8_t* b)
  292. {
  293. /*int rTmp = yValue + (1.403 * vValue);
  294. int gTmp = yValue - (0.344 * uValue) - (0.714 * vValue);
  295. int bTmp = yValue + (1.770 * uValue);*/
  296. /*int rTmp = yValue + (1.402 * (vValue - 128));
  297. int gTmp = yValue - (0.34414 * (uValue - 128)) - (0.71414 * (vValue - 128));
  298. int bTmp = yValue + (1.772 * (uValue - 128));*/
  299. int rTmp = yValue + (1.370705 * (vValue - 128));
  300. int gTmp = yValue - (0.698001 * (vValue - 128)) - (0.337633 * (uValue - 128));
  301. int bTmp = yValue + (1.732446 * (uValue - 128));
  302. *r = min(max(rTmp, 0), 255);
  303. *g = min(max(gTmp, 0), 255);
  304. *b = min(max(bTmp, 0), 255);
  305. }