Encode Sequence of Point Cloud Frames to H.264 using Nvidia Encoder












0















I have a sequence of point cloud frames. When all these sequences are played one after the other, it is like playing a video. I'd like to use NVidia's Video Encoder API (NVENC) to encode the original point cloud sequences to a H.264 encoded file.



Each frame of the point cloud has say N number of points, where each point is represented using (X,Y,Z) coordinates, and each such point has an RGB value.



I know there is a camera position/viewing angle, lets for now assume this to be constant.



I used OpenGL to read the file and render each frame one after the other. Here is a snippet of the code that renders just one frame.



glPushAttrib(GL_ALL_ATTRIB_BITS);
glPushMatrix();

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(1,0,2,0,0,0,0,2,0);

glPointSize(1.0);

glBegin(GL_POINTS);
for (int n = 0; n < (npoints*3); n+=3){
glColor3f(color_buffer[n], color_buffer[n+1], color_buffer[n+2]);
glVertex3f(point_buffer[n], point_buffer[n+1], point_buffer[n+2]);
}
glEnd();

glFlush();


How do I now proceed to using NVENC to encode to H.264? NVidia Video SDK sample code for OpenGL uses this however I am not sure how to change this for my purpose.



void EncodeGL(char *szInFilePath, char *szOutFilePath, int nWidth, int nHeight,
NV_ENC_BUFFER_FORMAT eFormat, NvEncoderInitParam *encodeCLIOptions)
{
std::ifstream fpIn(szInFilePath, std::ifstream::in | std::ifstream::binary);
if (!fpIn)
{
std::ostringstream err;
err << "Unable to open input file: " << szInFilePath << std::endl;
throw std::invalid_argument(err.str());
}

std::ofstream fpOut(szOutFilePath, std::ios::out | std::ios::binary);
if (!fpOut)
{
std::ostringstream err;
err << "Unable to open output file: " << szOutFilePath << std::endl;
throw std::invalid_argument(err.str());
}

NvEncoderGL enc(nWidth, nHeight, eFormat);

NV_ENC_INITIALIZE_PARAMS initializeParams = { NV_ENC_INITIALIZE_PARAMS_VER };
NV_ENC_CONFIG encodeConfig = { NV_ENC_CONFIG_VER };
initializeParams.encodeConfig = &encodeConfig;
enc.CreateDefaultEncoderParams(&initializeParams, encodeCLIOptions->GetEncodeGUID(),
encodeCLIOptions->GetPresetGUID());

encodeCLIOptions->SetInitParams(&initializeParams, eFormat);

enc.CreateEncoder(&initializeParams);

int nFrameSize = enc.GetFrameSize();
std::unique_ptr<uint8_t> pHostFrame(new uint8_t[nFrameSize]);
int nFrame = 0;
while (true)
{
std::streamsize nRead = fpIn.read(reinterpret_cast<char*>(pHostFrame.get()), nFrameSize).gcount();

const NvEncInputFrame* encoderInputFrame = enc.GetNextInputFrame();
NV_ENC_INPUT_RESOURCE_OPENGL_TEX *pResource = (NV_ENC_INPUT_RESOURCE_OPENGL_TEX *)encoderInputFrame->inputPtr;

glBindTexture(pResource->target, pResource->texture);
glTexSubImage2D(pResource->target, 0, 0, 0,
nWidth, nHeight * 3 / 2,
GL_RED, GL_UNSIGNED_BYTE, pHostFrame.get());
glBindTexture(pResource->target, 0);

std::vector<std::vector<uint8_t>> vPacket;
if (nRead == nFrameSize)
{
enc.EncodeFrame(vPacket);
}
else
{
enc.EndEncode(vPacket);
}
nFrame += (int)vPacket.size();
for (std::vector<uint8_t> &packet : vPacket)
{
fpOut.write(reinterpret_cast<char*>(packet.data()), packet.size());
}
if (nRead != nFrameSize) break;
}

enc.DestroyEncoder();

fpOut.close();
fpIn.close();

std::cout << "Total frames encoded: " << nFrame << std::endl;
std::cout << "Saved in file " << szOutFilePath << std::endl;
}


eFormat here is set to NV_ENC_BUFFER_FORMAT_IYUV, I will need NV_ENC_BUFFER_FORMAT_ARGB I guess?



I am also not sure how to get the frame height and width using OpenGL. Will I have to get it manually by calculating the distances between min_x and max_x coordinates, etc.?










share|improve this question



























    0















    I have a sequence of point cloud frames. When all these sequences are played one after the other, it is like playing a video. I'd like to use NVidia's Video Encoder API (NVENC) to encode the original point cloud sequences to a H.264 encoded file.



    Each frame of the point cloud has say N number of points, where each point is represented using (X,Y,Z) coordinates, and each such point has an RGB value.



    I know there is a camera position/viewing angle, lets for now assume this to be constant.



    I used OpenGL to read the file and render each frame one after the other. Here is a snippet of the code that renders just one frame.



    glPushAttrib(GL_ALL_ATTRIB_BITS);
    glPushMatrix();

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    gluLookAt(1,0,2,0,0,0,0,2,0);

    glPointSize(1.0);

    glBegin(GL_POINTS);
    for (int n = 0; n < (npoints*3); n+=3){
    glColor3f(color_buffer[n], color_buffer[n+1], color_buffer[n+2]);
    glVertex3f(point_buffer[n], point_buffer[n+1], point_buffer[n+2]);
    }
    glEnd();

    glFlush();


    How do I now proceed to using NVENC to encode to H.264? NVidia Video SDK sample code for OpenGL uses this however I am not sure how to change this for my purpose.



    void EncodeGL(char *szInFilePath, char *szOutFilePath, int nWidth, int nHeight,
    NV_ENC_BUFFER_FORMAT eFormat, NvEncoderInitParam *encodeCLIOptions)
    {
    std::ifstream fpIn(szInFilePath, std::ifstream::in | std::ifstream::binary);
    if (!fpIn)
    {
    std::ostringstream err;
    err << "Unable to open input file: " << szInFilePath << std::endl;
    throw std::invalid_argument(err.str());
    }

    std::ofstream fpOut(szOutFilePath, std::ios::out | std::ios::binary);
    if (!fpOut)
    {
    std::ostringstream err;
    err << "Unable to open output file: " << szOutFilePath << std::endl;
    throw std::invalid_argument(err.str());
    }

    NvEncoderGL enc(nWidth, nHeight, eFormat);

    NV_ENC_INITIALIZE_PARAMS initializeParams = { NV_ENC_INITIALIZE_PARAMS_VER };
    NV_ENC_CONFIG encodeConfig = { NV_ENC_CONFIG_VER };
    initializeParams.encodeConfig = &encodeConfig;
    enc.CreateDefaultEncoderParams(&initializeParams, encodeCLIOptions->GetEncodeGUID(),
    encodeCLIOptions->GetPresetGUID());

    encodeCLIOptions->SetInitParams(&initializeParams, eFormat);

    enc.CreateEncoder(&initializeParams);

    int nFrameSize = enc.GetFrameSize();
    std::unique_ptr<uint8_t> pHostFrame(new uint8_t[nFrameSize]);
    int nFrame = 0;
    while (true)
    {
    std::streamsize nRead = fpIn.read(reinterpret_cast<char*>(pHostFrame.get()), nFrameSize).gcount();

    const NvEncInputFrame* encoderInputFrame = enc.GetNextInputFrame();
    NV_ENC_INPUT_RESOURCE_OPENGL_TEX *pResource = (NV_ENC_INPUT_RESOURCE_OPENGL_TEX *)encoderInputFrame->inputPtr;

    glBindTexture(pResource->target, pResource->texture);
    glTexSubImage2D(pResource->target, 0, 0, 0,
    nWidth, nHeight * 3 / 2,
    GL_RED, GL_UNSIGNED_BYTE, pHostFrame.get());
    glBindTexture(pResource->target, 0);

    std::vector<std::vector<uint8_t>> vPacket;
    if (nRead == nFrameSize)
    {
    enc.EncodeFrame(vPacket);
    }
    else
    {
    enc.EndEncode(vPacket);
    }
    nFrame += (int)vPacket.size();
    for (std::vector<uint8_t> &packet : vPacket)
    {
    fpOut.write(reinterpret_cast<char*>(packet.data()), packet.size());
    }
    if (nRead != nFrameSize) break;
    }

    enc.DestroyEncoder();

    fpOut.close();
    fpIn.close();

    std::cout << "Total frames encoded: " << nFrame << std::endl;
    std::cout << "Saved in file " << szOutFilePath << std::endl;
    }


    eFormat here is set to NV_ENC_BUFFER_FORMAT_IYUV, I will need NV_ENC_BUFFER_FORMAT_ARGB I guess?



    I am also not sure how to get the frame height and width using OpenGL. Will I have to get it manually by calculating the distances between min_x and max_x coordinates, etc.?










    share|improve this question

























      0












      0








      0








      I have a sequence of point cloud frames. When all these sequences are played one after the other, it is like playing a video. I'd like to use NVidia's Video Encoder API (NVENC) to encode the original point cloud sequences to a H.264 encoded file.



      Each frame of the point cloud has say N number of points, where each point is represented using (X,Y,Z) coordinates, and each such point has an RGB value.



      I know there is a camera position/viewing angle, lets for now assume this to be constant.



      I used OpenGL to read the file and render each frame one after the other. Here is a snippet of the code that renders just one frame.



      glPushAttrib(GL_ALL_ATTRIB_BITS);
      glPushMatrix();

      glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();

      gluLookAt(1,0,2,0,0,0,0,2,0);

      glPointSize(1.0);

      glBegin(GL_POINTS);
      for (int n = 0; n < (npoints*3); n+=3){
      glColor3f(color_buffer[n], color_buffer[n+1], color_buffer[n+2]);
      glVertex3f(point_buffer[n], point_buffer[n+1], point_buffer[n+2]);
      }
      glEnd();

      glFlush();


      How do I now proceed to using NVENC to encode to H.264? NVidia Video SDK sample code for OpenGL uses this however I am not sure how to change this for my purpose.



      void EncodeGL(char *szInFilePath, char *szOutFilePath, int nWidth, int nHeight,
      NV_ENC_BUFFER_FORMAT eFormat, NvEncoderInitParam *encodeCLIOptions)
      {
      std::ifstream fpIn(szInFilePath, std::ifstream::in | std::ifstream::binary);
      if (!fpIn)
      {
      std::ostringstream err;
      err << "Unable to open input file: " << szInFilePath << std::endl;
      throw std::invalid_argument(err.str());
      }

      std::ofstream fpOut(szOutFilePath, std::ios::out | std::ios::binary);
      if (!fpOut)
      {
      std::ostringstream err;
      err << "Unable to open output file: " << szOutFilePath << std::endl;
      throw std::invalid_argument(err.str());
      }

      NvEncoderGL enc(nWidth, nHeight, eFormat);

      NV_ENC_INITIALIZE_PARAMS initializeParams = { NV_ENC_INITIALIZE_PARAMS_VER };
      NV_ENC_CONFIG encodeConfig = { NV_ENC_CONFIG_VER };
      initializeParams.encodeConfig = &encodeConfig;
      enc.CreateDefaultEncoderParams(&initializeParams, encodeCLIOptions->GetEncodeGUID(),
      encodeCLIOptions->GetPresetGUID());

      encodeCLIOptions->SetInitParams(&initializeParams, eFormat);

      enc.CreateEncoder(&initializeParams);

      int nFrameSize = enc.GetFrameSize();
      std::unique_ptr<uint8_t> pHostFrame(new uint8_t[nFrameSize]);
      int nFrame = 0;
      while (true)
      {
      std::streamsize nRead = fpIn.read(reinterpret_cast<char*>(pHostFrame.get()), nFrameSize).gcount();

      const NvEncInputFrame* encoderInputFrame = enc.GetNextInputFrame();
      NV_ENC_INPUT_RESOURCE_OPENGL_TEX *pResource = (NV_ENC_INPUT_RESOURCE_OPENGL_TEX *)encoderInputFrame->inputPtr;

      glBindTexture(pResource->target, pResource->texture);
      glTexSubImage2D(pResource->target, 0, 0, 0,
      nWidth, nHeight * 3 / 2,
      GL_RED, GL_UNSIGNED_BYTE, pHostFrame.get());
      glBindTexture(pResource->target, 0);

      std::vector<std::vector<uint8_t>> vPacket;
      if (nRead == nFrameSize)
      {
      enc.EncodeFrame(vPacket);
      }
      else
      {
      enc.EndEncode(vPacket);
      }
      nFrame += (int)vPacket.size();
      for (std::vector<uint8_t> &packet : vPacket)
      {
      fpOut.write(reinterpret_cast<char*>(packet.data()), packet.size());
      }
      if (nRead != nFrameSize) break;
      }

      enc.DestroyEncoder();

      fpOut.close();
      fpIn.close();

      std::cout << "Total frames encoded: " << nFrame << std::endl;
      std::cout << "Saved in file " << szOutFilePath << std::endl;
      }


      eFormat here is set to NV_ENC_BUFFER_FORMAT_IYUV, I will need NV_ENC_BUFFER_FORMAT_ARGB I guess?



      I am also not sure how to get the frame height and width using OpenGL. Will I have to get it manually by calculating the distances between min_x and max_x coordinates, etc.?










      share|improve this question














      I have a sequence of point cloud frames. When all these sequences are played one after the other, it is like playing a video. I'd like to use NVidia's Video Encoder API (NVENC) to encode the original point cloud sequences to a H.264 encoded file.



      Each frame of the point cloud has say N number of points, where each point is represented using (X,Y,Z) coordinates, and each such point has an RGB value.



      I know there is a camera position/viewing angle, lets for now assume this to be constant.



      I used OpenGL to read the file and render each frame one after the other. Here is a snippet of the code that renders just one frame.



      glPushAttrib(GL_ALL_ATTRIB_BITS);
      glPushMatrix();

      glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();

      gluLookAt(1,0,2,0,0,0,0,2,0);

      glPointSize(1.0);

      glBegin(GL_POINTS);
      for (int n = 0; n < (npoints*3); n+=3){
      glColor3f(color_buffer[n], color_buffer[n+1], color_buffer[n+2]);
      glVertex3f(point_buffer[n], point_buffer[n+1], point_buffer[n+2]);
      }
      glEnd();

      glFlush();


      How do I now proceed to using NVENC to encode to H.264? NVidia Video SDK sample code for OpenGL uses this however I am not sure how to change this for my purpose.



      void EncodeGL(char *szInFilePath, char *szOutFilePath, int nWidth, int nHeight,
      NV_ENC_BUFFER_FORMAT eFormat, NvEncoderInitParam *encodeCLIOptions)
      {
      std::ifstream fpIn(szInFilePath, std::ifstream::in | std::ifstream::binary);
      if (!fpIn)
      {
      std::ostringstream err;
      err << "Unable to open input file: " << szInFilePath << std::endl;
      throw std::invalid_argument(err.str());
      }

      std::ofstream fpOut(szOutFilePath, std::ios::out | std::ios::binary);
      if (!fpOut)
      {
      std::ostringstream err;
      err << "Unable to open output file: " << szOutFilePath << std::endl;
      throw std::invalid_argument(err.str());
      }

      NvEncoderGL enc(nWidth, nHeight, eFormat);

      NV_ENC_INITIALIZE_PARAMS initializeParams = { NV_ENC_INITIALIZE_PARAMS_VER };
      NV_ENC_CONFIG encodeConfig = { NV_ENC_CONFIG_VER };
      initializeParams.encodeConfig = &encodeConfig;
      enc.CreateDefaultEncoderParams(&initializeParams, encodeCLIOptions->GetEncodeGUID(),
      encodeCLIOptions->GetPresetGUID());

      encodeCLIOptions->SetInitParams(&initializeParams, eFormat);

      enc.CreateEncoder(&initializeParams);

      int nFrameSize = enc.GetFrameSize();
      std::unique_ptr<uint8_t> pHostFrame(new uint8_t[nFrameSize]);
      int nFrame = 0;
      while (true)
      {
      std::streamsize nRead = fpIn.read(reinterpret_cast<char*>(pHostFrame.get()), nFrameSize).gcount();

      const NvEncInputFrame* encoderInputFrame = enc.GetNextInputFrame();
      NV_ENC_INPUT_RESOURCE_OPENGL_TEX *pResource = (NV_ENC_INPUT_RESOURCE_OPENGL_TEX *)encoderInputFrame->inputPtr;

      glBindTexture(pResource->target, pResource->texture);
      glTexSubImage2D(pResource->target, 0, 0, 0,
      nWidth, nHeight * 3 / 2,
      GL_RED, GL_UNSIGNED_BYTE, pHostFrame.get());
      glBindTexture(pResource->target, 0);

      std::vector<std::vector<uint8_t>> vPacket;
      if (nRead == nFrameSize)
      {
      enc.EncodeFrame(vPacket);
      }
      else
      {
      enc.EndEncode(vPacket);
      }
      nFrame += (int)vPacket.size();
      for (std::vector<uint8_t> &packet : vPacket)
      {
      fpOut.write(reinterpret_cast<char*>(packet.data()), packet.size());
      }
      if (nRead != nFrameSize) break;
      }

      enc.DestroyEncoder();

      fpOut.close();
      fpIn.close();

      std::cout << "Total frames encoded: " << nFrame << std::endl;
      std::cout << "Saved in file " << szOutFilePath << std::endl;
      }


      eFormat here is set to NV_ENC_BUFFER_FORMAT_IYUV, I will need NV_ENC_BUFFER_FORMAT_ARGB I guess?



      I am also not sure how to get the frame height and width using OpenGL. Will I have to get it manually by calculating the distances between min_x and max_x coordinates, etc.?







      opengl nvidia video-encoding point-clouds






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 27 '18 at 8:34









      KaaL-KaaL-

      1614




      1614
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53495564%2fencode-sequence-of-point-cloud-frames-to-h-264-using-nvidia-encoder%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53495564%2fencode-sequence-of-point-cloud-frames-to-h-264-using-nvidia-encoder%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Lallio

          Unable to find Lightning Node

          Futebolista