Advertisement
Guest User

Untitled

a guest
Sep 25th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. extern "C" {
  5.     #include <libavutil/hwcontext.h>
  6.     #include <libavutil/hwcontext_cuda.h>
  7.     #include <libavformat/avformat.h>
  8.     #include <libavcodec/avcodec.h>
  9.     #include <libavutil/error.h>
  10.     #include <libavutil/opt.h>
  11.     #include <libavutil/hwcontext.h>
  12.     #include <libavutil/hwcontext_cuda.h>
  13. }
  14.  
  15.  
  16. // Cuda
  17. #include <cuda.h>
  18. #include <cudaGL.h>
  19.  
  20. int screenWidth = 1280;
  21. int screenHeight = 720;
  22.  
  23. CUcontext* m_cuContext;
  24. CUgraphicsResource* cuInpTexRes;
  25. CUDA_MEMCPY2D_st* m_memCpyStruct;
  26.  
  27. // ffmpeg
  28. AVCodecContext* c;
  29. AVFrame* frame;
  30. AVBufferRef* m_avBufferRefFrame;
  31. AVBufferRef* m_avBufferRefDevice;
  32. AVHWDeviceContext* hwDevContext;
  33. AVCUDADeviceContext* cudaDevCtx;
  34. AVHWFramesContext* frameCtxPtr;
  35.  
  36. //test
  37. FILE* outfile;
  38.  
  39. std::string getDeviceName() {
  40.     CUdevice cuDevice = 0;
  41.     cuDeviceGet(&cuDevice, 0);
  42.     char szDeviceName[80];
  43.     cuDeviceGetName(szDeviceName, sizeof(szDeviceName), cuDevice);
  44.     return szDeviceName;
  45. }
  46.  
  47. int initFFmpeg() {
  48.     CUresult res;
  49.     CUcontext oldCtx;
  50.     // Get device name
  51.     cuInit(0);
  52.     CUdevice cuDevice = 0;
  53.     cuDeviceGet(&cuDevice, 0);
  54.     char szDeviceName[80];
  55.     cuDeviceGetName(szDeviceName, sizeof(szDeviceName), cuDevice);
  56.  
  57.     int ret = av_hwdevice_ctx_create(&m_avBufferRefDevice, AV_HWDEVICE_TYPE_CUDA, szDeviceName, NULL, NULL);
  58.  
  59.     if (ret < 0) {
  60.         return -1;
  61.     }
  62.  
  63.     hwDevContext = (AVHWDeviceContext*)(m_avBufferRefDevice->data);
  64.     cudaDevCtx = (AVCUDADeviceContext*)(hwDevContext->hwctx);
  65.     m_cuContext = &(cudaDevCtx->cuda_ctx);
  66.  
  67.     m_avBufferRefFrame = av_hwframe_ctx_alloc(m_avBufferRefDevice);
  68.     //Setup some values before initialization
  69.     frameCtxPtr = (AVHWFramesContext*)(m_avBufferRefFrame->data);
  70.     frameCtxPtr->width = screenWidth;
  71.     frameCtxPtr->height = screenHeight;
  72.     frameCtxPtr->sw_format = AV_PIX_FMT_RGB0;
  73.     frameCtxPtr->format = AV_PIX_FMT_CUDA;
  74.     frameCtxPtr->device_ref = m_avBufferRefDevice;
  75.     frameCtxPtr->device_ctx = (AVHWDeviceContext*)m_avBufferRefDevice->data;
  76.  
  77.     ret = av_hwframe_ctx_init(m_avBufferRefFrame);
  78.     if (ret < 0) {
  79.         return -1;
  80.     }
  81.  
  82.     res = cuCtxPopCurrent_v2(&oldCtx); // THIS IS ALLOWED TO FAIL
  83.  
  84.     AVCodec* codec = avcodec_find_encoder_by_name("h264_nvenc");
  85.     c = avcodec_alloc_context3(codec);
  86.  
  87.     //Assign some hardware accel specific data to AvCodecContext
  88.     c->hw_device_ctx = m_avBufferRefDevice;//This must be done BEFORE avcodec_open2()
  89.     c->pix_fmt = AV_PIX_FMT_CUDA; //Since this is a cuda buffer, although its really opengl with a cuda ptr
  90.     c->hw_frames_ctx = m_avBufferRefFrame;
  91.     c->codec_type = AVMEDIA_TYPE_VIDEO;
  92.     c->sw_pix_fmt = AV_PIX_FMT_RGB0;
  93.  
  94.     /* resolution must be a multiple of two */
  95.     c->width = screenWidth;
  96.     c->height = screenHeight;
  97.     c->time_base = { 1, 30 };
  98.     c->framerate = { 30, 1 };
  99.     c->bit_rate = 8 * 1000 * 1000;
  100.  
  101.     ret = avcodec_open2(c, codec, NULL);
  102.     if (ret < 0) {
  103.         return -1;
  104.     }
  105.    
  106.     std::cout << "freeing context" << std::endl;
  107.     avcodec_free_context(&c);
  108.     std::cout << "Context freed" << std::endl;
  109.     av_buffer_unref(&m_avBufferRefDevice);
  110.     av_buffer_unref(&m_avBufferRefFrame);
  111.     av_frame_free(&frame);
  112. }
  113.  
  114.  
  115. int main(int argc, char *argv[]) {
  116.     if(initFFmpeg() == -1) {
  117.         return EXIT_FAILURE;
  118.     }
  119.    
  120.     return EXIT_SUCCESS;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement