NvUtils.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2016-2022, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of NVIDIA CORPORATION nor the names of its
  13. * contributors may be used to endorse or promote products derived
  14. * from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  20. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  24. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "NvUtils.h"
  29. #include "NvBuffer.h"
  30. #include "NvLogging.h"
  31. #include <fstream>
  32. #include <sstream>
  33. #include <string>
  34. #include "nvbufsurface.h"
  35. int
  36. read_video_frame(std::ifstream * stream, NvBuffer & buffer)
  37. {
  38. uint32_t i, j;
  39. char *data;
  40. for (i = 0; i < buffer.n_planes; i++)
  41. {
  42. NvBuffer::NvBufferPlane &plane = buffer.planes[i];
  43. std::streamsize bytes_to_read =
  44. plane.fmt.bytesperpixel * plane.fmt.width;
  45. data = (char *) plane.data;
  46. plane.bytesused = 0;
  47. for (j = 0; j < plane.fmt.height; j++)
  48. {
  49. stream->read(data, bytes_to_read);
  50. if (stream->gcount() < bytes_to_read)
  51. return -1;
  52. data += plane.fmt.stride;
  53. }
  54. plane.bytesused = plane.fmt.stride * plane.fmt.height;
  55. }
  56. return 0;
  57. }
  58. int
  59. write_video_frame(std::ofstream * stream, NvBuffer &buffer)
  60. {
  61. uint32_t i, j;
  62. char *data;
  63. for (i = 0; i < buffer.n_planes; i++)
  64. {
  65. NvBuffer::NvBufferPlane &plane = buffer.planes[i];
  66. size_t bytes_to_write =
  67. plane.fmt.bytesperpixel * plane.fmt.width;
  68. data = (char *) plane.data;
  69. for (j = 0; j < plane.fmt.height; j++)
  70. {
  71. stream->write(data, bytes_to_write);
  72. if (!stream->good())
  73. return -1;
  74. data += plane.fmt.stride;
  75. }
  76. }
  77. return 0;
  78. }
  79. int
  80. read_dmabuf(int dmabuf_fd,
  81. unsigned int plane,
  82. std::ifstream * stream)
  83. {
  84. if (dmabuf_fd <= 0)
  85. return -1;
  86. int ret = -1;
  87. NvBufSurface *nvbuf_surf = 0;
  88. ret = NvBufSurfaceFromFd(dmabuf_fd, (void**)(&nvbuf_surf));
  89. if (ret != 0)
  90. {
  91. return -1;
  92. }
  93. NvBufSurfaceMap(nvbuf_surf, 0, plane, NVBUF_MAP_READ_WRITE);
  94. NvBufSurfaceSyncForCpu (nvbuf_surf, 0, plane);
  95. for (uint i = 0; i < nvbuf_surf->surfaceList->planeParams.height[plane]; ++i)
  96. {
  97. stream->read((char *)nvbuf_surf->surfaceList->mappedAddr.addr[plane] + i * nvbuf_surf->surfaceList->planeParams.pitch[plane],
  98. nvbuf_surf->surfaceList->planeParams.width[plane] * nvbuf_surf->surfaceList->planeParams.bytesPerPix[plane]);
  99. if (!stream->good())
  100. return -1;
  101. }
  102. NvBufSurfaceSyncForDevice (nvbuf_surf, 0, plane);
  103. ret = NvBufSurfaceUnMap(nvbuf_surf, 0, plane);
  104. if (ret < 0)
  105. {
  106. printf("Error while Unmapping buffer\n");
  107. return ret;
  108. }
  109. return 0;
  110. }
  111. int
  112. dump_dmabuf(int dmabuf_fd,
  113. unsigned int plane,
  114. std::ofstream * stream)
  115. {
  116. if (dmabuf_fd <= 0)
  117. return -1;
  118. int ret = -1;
  119. NvBufSurface *nvbuf_surf = 0;
  120. ret = NvBufSurfaceFromFd(dmabuf_fd, (void**)(&nvbuf_surf));
  121. if (ret != 0)
  122. {
  123. return -1;
  124. }
  125. ret = NvBufSurfaceMap(nvbuf_surf, 0, plane, NVBUF_MAP_READ_WRITE);
  126. if (ret < 0)
  127. {
  128. printf("NvBufSurfaceMap failed\n");
  129. return ret;
  130. }
  131. NvBufSurfaceSyncForCpu (nvbuf_surf, 0, plane);
  132. for (uint i = 0; i < nvbuf_surf->surfaceList->planeParams.height[plane]; ++i)
  133. {
  134. stream->write((char *)nvbuf_surf->surfaceList->mappedAddr.addr[plane] + i * nvbuf_surf->surfaceList->planeParams.pitch[plane],
  135. nvbuf_surf->surfaceList->planeParams.width[plane] * nvbuf_surf->surfaceList->planeParams.bytesPerPix[plane]);
  136. if (!stream->good())
  137. return -1;
  138. }
  139. ret = NvBufSurfaceUnMap(nvbuf_surf, 0, plane);
  140. if (ret < 0)
  141. {
  142. printf("NvBufSurfaceUnMap failed\n");
  143. return ret;
  144. }
  145. return 0;
  146. }
  147. int
  148. parse_csv_recon_file(std::ifstream * stream, std::string * recon_params)
  149. {
  150. int parse_count = 0;
  151. std::string ref_line, ref_parsed_word;
  152. getline(*stream, ref_line);
  153. std::stringstream recon_ref_stream(ref_line);
  154. while (getline(recon_ref_stream, ref_parsed_word, ','))
  155. {
  156. if (parse_count >= 4)
  157. {
  158. printf("Only YUV data is supported in reconstructed pictures \n");
  159. return -1;
  160. }
  161. recon_params[parse_count] = ref_parsed_word;
  162. parse_count++;
  163. }
  164. return 0;
  165. }