Advertisement
0201m

FFMPEG 2

Mar 27th, 2015
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.22 KB | None | 0 0
  1. /* -LICENSE-START-
  2. ** Copyright (c) 2013 Blackmagic Design
  3. **
  4. ** Permission is hereby granted, free of charge, to any person or organization
  5. ** obtaining a copy of the software and accompanying documentation covered by
  6. ** this license (the "Software") to use, reproduce, display, distribute,
  7. ** execute, and transmit the Software, and to prepare derivative works of the
  8. ** Software, and to permit third-parties to whom the Software is furnished to
  9. ** do so, all subject to the following:
  10. **
  11. ** The copyright notices in the Software and this entire statement, including
  12. ** the above license grant, this restriction and the following disclaimer,
  13. ** must be included in all copies of the Software, in whole or in part, and
  14. ** all derivative works of the Software, unless such copies or derivative
  15. ** works are solely in the form of machine-executable object code generated by
  16. ** a source language processor.
  17. **
  18. ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. ** FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  21. ** SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  22. ** FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  23. ** ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. ** DEALINGS IN THE SOFTWARE.
  25. ** -LICENSE-END-
  26. */
  27.  
  28.  
  29. #include "stdafx.h"
  30. #include <gl\gl.h>
  31. #include <gl\freeglut.h>
  32. #include "PreviewWindow.h"
  33. #include <stdio.h>
  34. #include <string>
  35. #include <cstring>
  36. #include <sstream>
  37. #include <iostream>
  38.  
  39. FILE* ffmpeg;
  40. int* buffer;
  41. int viewWidth2;
  42. int viewHeight2;
  43. bool isCapturing = FALSE;
  44. bool valueSet = FALSE;
  45.  
  46. PreviewWindow::PreviewWindow()
  47. : m_deckLinkScreenPreviewHelper(NULL), m_refCount(1), m_previewBox(NULL), m_previewBoxDC(NULL), m_openGLctx(NULL)
  48. {
  49.  
  50. }
  51.  
  52.  
  53. PreviewWindow::~PreviewWindow()
  54. {
  55.     if (m_deckLinkScreenPreviewHelper != NULL)
  56.     {
  57.         m_deckLinkScreenPreviewHelper->Release();
  58.         m_deckLinkScreenPreviewHelper = NULL;
  59.     }
  60.  
  61.     if (m_openGLctx != NULL)
  62.     {
  63.         wglDeleteContext(m_openGLctx);
  64.         m_openGLctx = NULL;
  65.     }
  66.  
  67.     if (m_previewBoxDC != NULL)
  68.     {
  69.         m_previewBox->ReleaseDC(m_previewBoxDC);
  70.         m_previewBoxDC = NULL;
  71.     }
  72. }
  73.  
  74. bool        PreviewWindow::init(CStatic *previewBox)
  75. {
  76.     m_previewBox = previewBox;
  77.  
  78.     // Create the DeckLink screen preview helper
  79.     if (CoCreateInstance(CLSID_CDeckLinkGLScreenPreviewHelper, NULL, CLSCTX_ALL, IID_IDeckLinkGLScreenPreviewHelper, (void**)&m_deckLinkScreenPreviewHelper) != S_OK)
  80.         return false;
  81.  
  82.     // Initialise OpenGL
  83.     return initOpenGL();
  84. }
  85.  
  86. bool        PreviewWindow::initOpenGL()
  87. {
  88.     PIXELFORMATDESCRIPTOR   pixelFormatDesc;
  89.     int                     pixelFormat;
  90.     bool                    result = false;
  91.  
  92.     //
  93.     // Here, we create an OpenGL context attached to the screen preview box
  94.     // so we can use it later on when we need to draw preview frames.
  95.  
  96.     // Get the preview box drawing context
  97.     m_previewBoxDC = m_previewBox->GetDC();
  98.     if (m_previewBoxDC == NULL)
  99.         return false;
  100.  
  101.     // Ensure the preview box DC uses ARGB pixel format
  102.     ZeroMemory(&pixelFormatDesc, sizeof(pixelFormatDesc));
  103.     pixelFormatDesc.nSize = sizeof(pixelFormatDesc);
  104.     pixelFormatDesc.nVersion = 1;
  105.     pixelFormatDesc.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
  106.     pixelFormatDesc.iPixelType = PFD_TYPE_RGBA;
  107.     pixelFormatDesc.cColorBits = 32;
  108.     pixelFormatDesc.cDepthBits = 16;
  109.     pixelFormatDesc.cAlphaBits = 8;
  110.     pixelFormatDesc.iLayerType = PFD_MAIN_PLANE;
  111.     pixelFormat = ChoosePixelFormat(m_previewBoxDC->m_hDC, &pixelFormatDesc);
  112.     if (SetPixelFormat(m_previewBoxDC->m_hDC, pixelFormat, &pixelFormatDesc) == false)
  113.         return false;
  114.  
  115.     // Create OpenGL rendering context
  116.     m_openGLctx = wglCreateContext(m_previewBoxDC->m_hDC);
  117.     if (m_openGLctx == NULL)
  118.         return false;
  119.     // Make the new OpenGL context the current rendering context so
  120.     // we can initialise the DeckLink preview helper
  121.     if (wglMakeCurrent(m_previewBoxDC->m_hDC, m_openGLctx) == FALSE)
  122.         return false;
  123.  
  124.     if (m_deckLinkScreenPreviewHelper->InitializeGL() == S_OK)
  125.         result = true;
  126.  
  127.     // Reset the OpenGL rendering context
  128.     wglMakeCurrent(NULL, NULL);
  129.  
  130.     return result;
  131. }
  132.  
  133. HRESULT         PreviewWindow::DrawFrame(IDeckLinkVideoFrame* theFrame)
  134. {
  135.     GLfloat width = theFrame->GetWidth();
  136.     GLfloat height = theFrame->GetHeight();
  137.     GLfloat aspect = width / height;
  138.     int viewportY = 0;
  139.     int viewportX = 0;
  140.     CRect rect;
  141.  
  142.     m_previewBox->GetClientRect(&rect);
  143.  
  144.     int previewWidth = rect.Width();
  145.     int previewHeight = rect.Height();
  146.  
  147.     int viewWidth = previewWidth;
  148.     int viewHeight = previewWidth / aspect;
  149.  
  150.     if (viewHeight > previewHeight) {
  151.         viewHeight = previewHeight;
  152.         viewWidth = previewHeight * aspect;
  153.     }
  154.     viewWidth2 = theFrame->GetWidth();
  155.     viewHeight2 = theFrame->GetHeight();
  156.  
  157.     viewportY = (previewHeight - viewHeight) / 2;
  158.     viewportX = (previewWidth - viewWidth) / 2;
  159.  
  160.     // Make sure we are initialised
  161.     if ((m_deckLinkScreenPreviewHelper == NULL) || (m_previewBoxDC == NULL) || (m_openGLctx == NULL))
  162.         return E_FAIL;
  163.  
  164.     // First, pass the frame to the DeckLink screen preview helper
  165.     m_deckLinkScreenPreviewHelper->SetFrame(theFrame);
  166.  
  167.  
  168.     // Then set the OpenGL rendering context to the one we created before
  169.     wglMakeCurrent(m_previewBoxDC->m_hDC, m_openGLctx);
  170.  
  171.     //set the viewport X/Y coordinates + letter-/pillarbox
  172.  
  173.     glViewport(viewportX, viewportY, viewWidth, viewHeight);
  174.  
  175.     if (isCapturing){
  176.         glReadPixels(0, 0, viewWidth2, viewHeight2, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  177.         fwrite(buffer, sizeof(int)*viewWidth2 * viewHeight2, 1, ffmpeg);
  178.     }
  179.     // and let the helper take care of the drawing
  180.     m_deckLinkScreenPreviewHelper->PaintGL();
  181.    
  182.     // Last, reset the OpenGL rendering context
  183.     wglMakeCurrent(NULL, NULL);
  184.  
  185.     return S_OK;
  186. }
  187.  
  188. void PreviewWindow::startCapture(){
  189.     if (!isCapturing){
  190.         isCapturing = TRUE;
  191.        
  192.         char cmd[1024];
  193.         sprintf(cmd, "ffmpeg -r 25 -f rawvideo -pix_fmt rgba -s %dx%d -i - "
  194.             "-threads 0 -preset fast -y -pix_fmt yuv420p -crf 21 -vf vflip output.mp4",
  195.             viewWidth2, viewHeight2);
  196.  
  197.  
  198.         // open pipe to ffmpeg's stdin in binary write mode
  199.         ffmpeg = _popen(cmd, "wb");
  200.  
  201.         buffer = new int[viewWidth2 * viewHeight2];
  202.         //glutSwapBuffers();
  203.     }
  204.     else{
  205.         isCapturing = FALSE;
  206.         _pclose(ffmpeg);
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement