Jetson Linux API Reference

38.2 Release
NvBuffer.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2016-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: BSD-3-Clause
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
38 #ifndef __NV_BUFFER_H__
39 #define __NV_BUFFER_H__
40 
41 #include <linux/videodev2.h>
42 #include <pthread.h>
43 #include <stdint.h>
44 
45 #include "v4l2_nv_extensions.h"
46 #include "nvbufsurface.h"
47 
61 #define MAX_PLANES 3
62 
88 class NvBuffer
89 {
90 public:
94  typedef struct
95  {
96  uint32_t width;
97  uint32_t height;
99  uint32_t bytesperpixel;
101  uint32_t stride;
102  uint32_t sizeimage;
104 
108  typedef struct
109  {
112  unsigned char *data;
113  uint32_t bytesused;
115  int fd;
117  uint32_t mem_offset;
119  uint32_t length;
120  } NvBufferPlane;
121 
139  NvBuffer(enum v4l2_buf_type buf_type, enum v4l2_memory memory_type,
140  uint32_t n_planes, NvBufferPlaneFormat *fmt, uint32_t index);
141 
160  NvBuffer(uint32_t pixfmt, uint32_t width, uint32_t height, uint32_t index);
161 
178  NvBuffer(uint32_t size, uint32_t index);
179 
186  ~NvBuffer();
187 
196  int map();
201  void unmap();
202 
213  int allocateMemory();
220  void deallocateMemory();
221 
229  int ref();
238  int unref();
239 
240  const enum v4l2_buf_type buf_type;
241  const enum v4l2_memory memory_type;
244  const uint32_t index;
246  uint32_t n_planes;
268  static int fill_buffer_plane_format(uint32_t *num_planes,
270  uint32_t width, uint32_t height, uint32_t raw_pixfmt);
271 private:
272  uint32_t ref_count;
273  pthread_mutex_t ref_lock;
276  bool mapped;
278  bool allocated;
280  NvBuffer *shared_buffer;
287  NvBuffer(const NvBuffer& that);
291  void operator=(NvBuffer const&);
292 
293  friend class NvV4l2ElementPlane;
294 };
296 #endif
NvBuffer::NvBufferPlane::mem_offset
uint32_t mem_offset
Holds the offset of the first valid byte from the data pointer.
Definition: NvBuffer.h:117
NvBuffer::index
const uint32_t index
Holds the buffer index.
Definition: NvBuffer.h:244
NvBuffer::NvBufferPlane::data
unsigned char * data
Holds a pointer to the plane memory.
Definition: NvBuffer.h:112
NvBuffer::NvBufferPlane
Holds the buffer plane parameters.
Definition: NvBuffer.h:108
NvBuffer::NvBufferPlane::fmt
NvBufferPlaneFormat fmt
Holds the format of the plane.
Definition: NvBuffer.h:110
NvBuffer::NvBufferPlaneFormat::width
uint32_t width
Holds the width of the plane in pixels.
Definition: NvBuffer.h:96
NvBuffer::fill_buffer_plane_format
static int fill_buffer_plane_format(uint32_t *num_planes, NvBuffer::NvBufferPlaneFormat *planefmts, uint32_t width, uint32_t height, uint32_t raw_pixfmt)
Fills the NvBuffer::NvBufferPlaneFormat array.
NvBuffer::deallocateMemory
void deallocateMemory()
Deallocates buffer memory.
MAX_PLANES
#define MAX_PLANES
Specifies the maximum number of planes a buffer can contain.
Definition: NvBuffer.h:61
NvBuffer::allocateMemory
int allocateMemory()
Allocates software memory for the buffer.
NvBuffer::~NvBuffer
~NvBuffer()
Destroys an NvBuffer object.
NvBuffer::unmap
void unmap()
Unmaps the contents of the buffer from memory.
NvBuffer::n_planes
uint32_t n_planes
Holds the number of planes in the buffer.
Definition: NvBuffer.h:246
NvBuffer::buf_type
enum v4l2_buf_type buf_type
Type of the buffer.
Definition: NvBuffer.h:240
NvBuffer::NvBufferPlane::fd
int fd
Holds the file descriptor (FD) of the plane of the exported buffer, in the case of V4L2 MMAP buffers.
Definition: NvBuffer.h:115
NvBuffer::planes
NvBufferPlane planes[MAX_PLANES]
Holds the data pointer, plane file descriptor (FD), plane format, etc.
Definition: NvBuffer.h:247
NvBuffer::NvBuffer
NvBuffer(enum v4l2_buf_type buf_type, enum v4l2_memory memory_type, uint32_t n_planes, NvBufferPlaneFormat *fmt, uint32_t index)
Creates a new NvBuffer object.
NvV4l2ElementPlane
Defines a helper class for operations performed on a V4L2 Element plane.
Definition: NvV4l2ElementPlane.h:104
NvBuffer::NvBufferPlaneFormat::bytesperpixel
uint32_t bytesperpixel
Holds the bytes used to represent one pixel in the plane.
Definition: NvBuffer.h:99
NvBuffer::ref
int ref()
Increases the reference count of the buffer.
NvBuffer::memory_type
enum v4l2_memory memory_type
Type of memory associated with the buffer.
Definition: NvBuffer.h:241
v4l2_nv_extensions.h
NvBuffer::NvBufferPlane::length
uint32_t length
Holds the size of the buffer in bytes.
Definition: NvBuffer.h:119
NvBuffer::NvBufferPlaneFormat::sizeimage
uint32_t sizeimage
Holds the size of the plane in bytes.
Definition: NvBuffer.h:102
NvBuffer::map
int map()
Maps the contents of the buffer to memory.
NvBuffer::NvBufferPlaneFormat
Holds the buffer plane format.
Definition: NvBuffer.h:94
NvBuffer
Class representing a buffer.
Definition: NvBuffer.h:88
NvBuffer::NvBufferPlaneFormat::stride
uint32_t stride
Holds the stride of the plane in bytes.
Definition: NvBuffer.h:101
NvBuffer::NvBufferPlaneFormat::height
uint32_t height
Holds the height of the plane in pixels.
Definition: NvBuffer.h:97
NvBuffer::unref
int unref()
Decreases the reference count of the buffer.
NvBuffer::NvBufferPlane::bytesused
uint32_t bytesused
Holds the number of valid bytes in the plane.
Definition: NvBuffer.h:113