Logo
ApraPipes 1.0
Loading...
Searching...
No Matches
H264DecoderV4L2Helper.h
1#pragma once
2/*
3 * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of NVIDIA CORPORATION nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
32#define DECODER_DEV "/dev/nvhost-nvdec"
33#define MAX_BUFFERS 32
34#define CHUNK_SIZE 4000000
38#define MAX_PLANES 3
39#define MIN(a, b) (((a) < (b)) ? (a) : (b))
40#include "Frame.h"
41#include <fstream>
42#include <linux/videodev2.h>
43#include <queue>
44
61class Buffer
62{
63public:
67 typedef struct
68 {
69 uint32_t width;
70 uint32_t height;
72 uint32_t bytesperpixel;
74 uint32_t stride;
75 uint32_t sizeimage;
77
81 typedef struct
82 {
85 unsigned char *data;
86 uint32_t bytesused;
88 int fd;
90 uint32_t mem_offset;
92 uint32_t length;
94
95 Buffer() {}
96
97 Buffer(enum v4l2_buf_type buf_type, enum v4l2_memory memory_type,
98 uint32_t index);
99
100 Buffer(enum v4l2_buf_type buf_type, enum v4l2_memory memory_type,
101 uint32_t n_planes, BufferPlaneFormat *fmt, uint32_t index);
102
103 ~Buffer();
104
111 int map();
116 void unmap();
117
118 enum v4l2_buf_type buf_type;
119 enum v4l2_memory memory_type;
122 uint32_t index;
124 uint32_t n_planes;
125 BufferPlane planes[MAX_PLANES];
131 int fill_buffer_plane_format(uint32_t *num_planes,
132 Buffer::BufferPlaneFormat *planefmts,
133 uint32_t width, uint32_t height, uint32_t raw_pixfmt);
134
135private:
136 bool mapped;
137};
138
140{
141public:
152 typedef struct
153 {
155 uint32_t out_pixfmt;
158 enum v4l2_memory op_mem_type;
159 enum v4l2_memory cp_mem_type;
160 enum v4l2_buf_type op_buf_type;
161 enum v4l2_buf_type cp_buf_type;
162 Buffer::BufferPlaneFormat op_planefmts[MAX_PLANES];
163 Buffer::BufferPlaneFormat cp_planefmts[MAX_PLANES];
168
170
173
174 pthread_mutex_t queue_lock;
175 pthread_cond_t queue_cond;
177
178 bool in_error = false;
179 bool eos;
183 int fd;
185 int dmabuff_fd[MAX_BUFFERS];
186 } context_t;
187
196 void read_input_chunk_frame_sp(void* inputFrameBuffer, size_t inputFrameSize, Buffer *buffer);
197
208 int sendFrames(int dmaOutFrameFd, frame_sp outFrame);
209
221 int set_capture_plane_format(context_t *ctx, uint32_t pixfmt,
222 uint32_t width, uint32_t height);
223
233
246 // static void* capture_wrapper(void* object);
247
248 static void *capture_thread(void *arg);
249
261 bool decode_process(context_t &ctx, void* inputFrameBuffer, size_t inputFrameSize);
262
275 int dq_event(context_t *ctx, struct v4l2_event &event, uint32_t max_wait_ms);
276
296 int dq_buffer(context_t *ctx, struct v4l2_buffer &v4l2_buf, Buffer **buffer,
297 enum v4l2_buf_type buf_type, enum v4l2_memory memory_type, uint32_t num_retries);
298
313 int q_buffer(context_t *ctx, struct v4l2_buffer &v4l2_buf, Buffer *buffer,
314 enum v4l2_buf_type buf_type, enum v4l2_memory memory_type, int num_planes);
315
328 int req_buffers_on_capture_plane(context_t *ctx, enum v4l2_buf_type buf_type,
329 enum v4l2_memory mem_type, int num_buffers);
330
343 int req_buffers_on_output_plane(context_t *ctx, enum v4l2_buf_type buf_type,
344 enum v4l2_memory mem_type, int num_buffers);
345
357 int set_output_plane_format(context_t &ctx, uint32_t pixfmt, uint32_t sizeimage);
358
370 int set_ext_controls(int fd, uint32_t id, uint32_t value);
371
383 int subscribe_event(int fd, uint32_t type, uint32_t id, uint32_t flags);
384
385 int process(void* inputFrameBuffer, size_t inputFrameSize, uint64_t inputFrameTS);
386
387 bool init(std::function<void(frame_sp &)> send, std::function<frame_sp()> makeFrame);
388
389 bool initializeDecoder();
390
391 void closeAllThreads(frame_sp eosFrame);
392
393 void deQueAllBuffers();
394protected:
395 boost::shared_ptr<Buffer> mBuffer;
397 std::function<frame_sp()> makeFrame;
398 std::function<void(frame_sp &)> send;
399 int ret = 0;
400 std::queue<uint64_t> framesTimestampEntry;
401 std::mutex m;
402};
Class representing a buffer.
Definition H264DecoderV4L2Helper.h:62
bool mapped
Definition H264DecoderV4L2Helper.h:136
uint32_t n_planes
Definition H264DecoderV4L2Helper.h:124
enum v4l2_buf_type buf_type
Definition H264DecoderV4L2Helper.h:118
uint32_t index
Definition H264DecoderV4L2Helper.h:122
int map()
Definition H264DecoderV4L2Helper.cpp:183
~Buffer()
Definition H264DecoderV4L2Helper.cpp:174
int fill_buffer_plane_format(uint32_t *num_planes, Buffer::BufferPlaneFormat *planefmts, uint32_t width, uint32_t height, uint32_t raw_pixfmt)
Definition H264DecoderV4L2Helper.cpp:245
enum v4l2_memory memory_type
Definition H264DecoderV4L2Helper.h:119
BufferPlane planes[MAX_PLANES]
Definition H264DecoderV4L2Helper.h:125
void unmap()
Definition H264DecoderV4L2Helper.cpp:224
Buffer()
Definition H264DecoderV4L2Helper.h:95
Definition H264DecoderV4L2Helper.h:140
int sendFrames(int dmaOutFrameFd, frame_sp outFrame)
Writes a plane data of the buffer to a file.
Definition H264DecoderV4L2Helper.cpp:312
int dq_buffer(context_t *ctx, struct v4l2_buffer &v4l2_buf, Buffer **buffer, enum v4l2_buf_type buf_type, enum v4l2_memory memory_type, uint32_t num_retries)
Dequeues a buffer from the plane.
Definition H264DecoderV4L2Helper.cpp:880
int req_buffers_on_capture_plane(context_t *ctx, enum v4l2_buf_type buf_type, enum v4l2_memory mem_type, int num_buffers)
Requests for buffers on the decoder capture plane.
Definition H264DecoderV4L2Helper.cpp:995
int set_capture_plane_format(context_t *ctx, uint32_t pixfmt, uint32_t width, uint32_t height)
Sets the format on the decoder capture plane.
Definition H264DecoderV4L2Helper.cpp:326
int ret
Definition H264DecoderV4L2Helper.h:399
bool initializeDecoder()
Definition H264DecoderV4L2Helper.cpp:1138
static void * capture_thread(void *arg)
Callback function on capture thread.
Definition H264DecoderV4L2Helper.cpp:606
void query_set_capture(context_t *ctx)
Query and Set Capture plane.
Definition H264DecoderV4L2Helper.cpp:375
int process(void *inputFrameBuffer, size_t inputFrameSize, uint64_t inputFrameTS)
Definition H264DecoderV4L2Helper.cpp:1314
int set_output_plane_format(context_t &ctx, uint32_t pixfmt, uint32_t sizeimage)
Sets the format on the decoder output plane.
Definition H264DecoderV4L2Helper.cpp:1071
std::queue< uint64_t > framesTimestampEntry
Definition H264DecoderV4L2Helper.h:400
int req_buffers_on_output_plane(context_t *ctx, enum v4l2_buf_type buf_type, enum v4l2_memory mem_type, int num_buffers)
Requests for buffers on the decoder output plane.
Definition H264DecoderV4L2Helper.cpp:1033
boost::shared_ptr< Buffer > mBuffer
Definition H264DecoderV4L2Helper.h:395
int dq_event(context_t *ctx, struct v4l2_event &event, uint32_t max_wait_ms)
Dequeues an event.
Definition H264DecoderV4L2Helper.cpp:855
int set_ext_controls(int fd, uint32_t id, uint32_t value)
Sets the value of controls.
Definition H264DecoderV4L2Helper.cpp:1097
bool decode_process(context_t &ctx, void *inputFrameBuffer, size_t inputFrameSize)
Decode processing function for blocking mode.
Definition H264DecoderV4L2Helper.cpp:787
std::function< void(frame_sp &)> send
Definition H264DecoderV4L2Helper.h:398
~h264DecoderV4L2Helper()
Definition H264DecoderV4L2Helper.h:151
h264DecoderV4L2Helper()
Struct defining the decoder context. The video decoder device node is /dev/nvhost-nvdec....
Definition H264DecoderV4L2Helper.h:150
bool init(std::function< void(frame_sp &)> send, std::function< frame_sp()> makeFrame)
Definition H264DecoderV4L2Helper.cpp:1131
int subscribe_event(int fd, uint32_t type, uint32_t id, uint32_t flags)
Subscribes to an V4L2 event.
Definition H264DecoderV4L2Helper.cpp:1115
void closeAllThreads(frame_sp eosFrame)
Definition H264DecoderV4L2Helper.cpp:1414
std::function< frame_sp()> makeFrame
Definition H264DecoderV4L2Helper.h:397
void deQueAllBuffers()
Definition H264DecoderV4L2Helper.cpp:1420
int q_buffer(context_t *ctx, struct v4l2_buffer &v4l2_buf, Buffer *buffer, enum v4l2_buf_type buf_type, enum v4l2_memory memory_type, int num_planes)
Queues a buffer on the plane.
Definition H264DecoderV4L2Helper.cpp:950
std::mutex m
Definition H264DecoderV4L2Helper.h:401
context_t ctx
Definition H264DecoderV4L2Helper.h:396
void read_input_chunk_frame_sp(void *inputFrameBuffer, size_t inputFrameSize, Buffer *buffer)
Reads the encoded data from a file to the buffer structure.
Definition H264DecoderV4L2Helper.cpp:285
Definition H264DecoderV4L2Helper.h:68
uint32_t sizeimage
Definition H264DecoderV4L2Helper.h:75
uint32_t height
Definition H264DecoderV4L2Helper.h:70
uint32_t stride
Definition H264DecoderV4L2Helper.h:74
uint32_t bytesperpixel
Definition H264DecoderV4L2Helper.h:72
uint32_t width
Definition H264DecoderV4L2Helper.h:69
Definition H264DecoderV4L2Helper.h:82
int fd
Definition H264DecoderV4L2Helper.h:88
unsigned char * data
Definition H264DecoderV4L2Helper.h:85
BufferPlaneFormat fmt
Definition H264DecoderV4L2Helper.h:83
uint32_t mem_offset
Definition H264DecoderV4L2Helper.h:90
uint32_t length
Definition H264DecoderV4L2Helper.h:92
uint32_t bytesused
Definition H264DecoderV4L2Helper.h:86
Definition H264DecoderV4L2Helper.h:153
pthread_cond_t queue_cond
Definition H264DecoderV4L2Helper.h:175
int fd
Definition H264DecoderV4L2Helper.h:183
uint32_t display_width
Definition H264DecoderV4L2Helper.h:156
uint32_t display_height
Definition H264DecoderV4L2Helper.h:157
enum v4l2_memory op_mem_type
Definition H264DecoderV4L2Helper.h:158
uint32_t cp_num_planes
Definition H264DecoderV4L2Helper.h:164
bool got_eos
Definition H264DecoderV4L2Helper.h:180
pthread_mutex_t queue_lock
Definition H264DecoderV4L2Helper.h:174
enum v4l2_buf_type op_buf_type
Definition H264DecoderV4L2Helper.h:160
pthread_t dec_capture_thread
Definition H264DecoderV4L2Helper.h:176
uint32_t decode_pixfmt
Definition H264DecoderV4L2Helper.h:154
Buffer ** op_buffers
Definition H264DecoderV4L2Helper.h:171
bool eos
Definition H264DecoderV4L2Helper.h:179
enum v4l2_buf_type cp_buf_type
Definition H264DecoderV4L2Helper.h:161
uint32_t out_pixfmt
Definition H264DecoderV4L2Helper.h:155
Buffer ** cp_buffers
Definition H264DecoderV4L2Helper.h:172
uint32_t op_num_planes
Definition H264DecoderV4L2Helper.h:165
uint32_t num_queued_op_buffers
Definition H264DecoderV4L2Helper.h:169
bool op_streamon
Definition H264DecoderV4L2Helper.h:181
enum v4l2_memory cp_mem_type
Definition H264DecoderV4L2Helper.h:159
int dst_dma_fd
Definition H264DecoderV4L2Helper.h:184
bool cp_streamon
Definition H264DecoderV4L2Helper.h:182
uint32_t op_num_buffers
Definition H264DecoderV4L2Helper.h:167
uint32_t cp_num_buffers
Definition H264DecoderV4L2Helper.h:166