blob: bbf573bd6c2d600c219c2faf60515bc86d5a69da [file] [log] [blame]
henryhsu58be50c2014-10-30 11:49:19 +08001/* Copyright 2014 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
Wu-Cheng Lice8947e2014-11-13 17:34:16 +08006#include <assert.h>
7#include <errno.h>
8#include <linux/videodev2.h>
9#include <pthread.h>
henryhsu58be50c2014-10-30 11:49:19 +080010#include <unistd.h>
Wu-Cheng Lice8947e2014-11-13 17:34:16 +080011#include <stdbool.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <sys/queue.h>
henryhsu58be50c2014-10-30 11:49:19 +080016#include <sys/syscall.h>
Wu-Cheng Lice8947e2014-11-13 17:34:16 +080017#include "config.h" /* For HAVE_VISIBILITY */
henryhsu58be50c2014-10-30 11:49:19 +080018#include "libv4l-plugin.h"
Alpha Linfa5caae2014-11-17 09:48:35 +080019#include "libvpu/rk_vepu_debug.h"
Wu-Cheng Lidcec4332014-11-13 15:49:21 +080020#include "libvpu/rk_vepu_interface.h"
henryhsu58be50c2014-10-30 11:49:19 +080021
Wu-Cheng Lice8947e2014-11-13 17:34:16 +080022#define VLOG(log_level, str, ...) ((g_log_level >= log_level) ? \
23 (void) fprintf(stderr, "%s: " str "\n", __func__, ##__VA_ARGS__) \
24 : (void) 0)
25
26#define VLOG_FD(log_level, str, ...) ((g_log_level >= log_level) ? \
27 (void) fprintf(stderr, \
28 "%s: fd=%d. " str "\n", __func__, fd, ##__VA_ARGS__) : (void) 0)
29
30#define SYS_IOCTL(fd, cmd, arg) ({ \
31 int ret = syscall(SYS_ioctl, (int)(fd), (unsigned long)(cmd), \
32 (void *)(arg)); \
Hirokazu Hondacfb9e182017-07-19 17:54:08 +090033 if (g_log_level >= 2 || (ret && errno != EAGAIN && \
34 (cmd != VIDIOC_ENUM_FMT || errno != EINVAL))) \
henryhsuc7c06f92014-11-24 15:57:08 +080035 fprintf(stderr, "SYS_ioctl: %s(%lu): fd=%d, ret=%d, errno=%d\n",\
36 v4l_cmd2str(cmd), _IOC_NR((unsigned long)cmd), fd, ret, \
37 errno); \
Wu-Cheng Lice8947e2014-11-13 17:34:16 +080038 ret; \
39 })
henryhsu58be50c2014-10-30 11:49:19 +080040
41#if HAVE_VISIBILITY
42#define PLUGIN_PUBLIC __attribute__ ((__visibility__("default")))
43#else
44#define PLUGIN_PUBLIC
45#endif
46
Wu-Cheng Li11ae3aa2014-12-18 17:11:48 +080047#define RK3288_VPU_NAME "rk3288-vpu-enc"
Wu-Cheng Lice8947e2014-11-13 17:34:16 +080048#define DEFAULT_FRAME_RATE 30
49#define DEFAULT_BITRATE 1000000
henryhsuc7c06f92014-11-24 15:57:08 +080050#define PENDING_BUFFER_QUEUE_SIZE VIDEO_MAX_FRAME
Wu-Cheng Lice8947e2014-11-13 17:34:16 +080051
henryhsuc7c06f92014-11-24 15:57:08 +080052/*
53 * struct pending_buffer - A v4l2 buffer pending for QBUF.
54 * @buffer: v4l2 buffer for QBUF.
55 * @planes: plane info of v4l2 buffer.
56 * @next_runtime_param: runtime parameters like framerate, bitrate, and
57 * keyframe for the next buffer.
58 */
Wu-Cheng Lice8947e2014-11-13 17:34:16 +080059struct pending_buffer {
60 struct v4l2_buffer buffer;
61 struct v4l2_plane planes[VIDEO_MAX_PLANES];
henryhsuc7c06f92014-11-24 15:57:08 +080062 struct rk_vepu_runtime_param next_runtime_param;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +080063};
henryhsuc7c06f92014-11-24 15:57:08 +080064
65/*
66 * struct pending_buffer_queue - a ring buffer of pending buffers.
67 * @count: the number of buffers stored in the array.
68 * @front: the index of the first ready buffer.
69 * @buf_array: pending buffer array.
70 */
71struct pending_buffer_queue {
72 uint32_t count;
73 int32_t front;
74 struct pending_buffer buf_array[PENDING_BUFFER_QUEUE_SIZE];
75};
Wu-Cheng Lice8947e2014-11-13 17:34:16 +080076
77/*
78 * struct encoder_context - the context of an encoder instance.
79 * @enc: Encoder instance returned from rk_vepu_create().
80 * @mutex: The mutex to protect encoder_context.
Wu-Cheng Lice8947e2014-11-13 17:34:16 +080081 * @output_streamon_type: Type of output interface when it streams on.
82 * @capture_streamon_type: Type of capture interface when it streams on.
henryhsuc7c06f92014-11-24 15:57:08 +080083 * @init_param: Encoding parameters like input format, resolution, and etc.
84 * These parameters will be passed to encoder at libvpu
85 * initialization.
86 * @runtime_param: Runtime parameters like framerate, bitrate, and
87 * keyframe. This is only used for receiving ext_ctrls
88 * before streamon and pending buffer queue is empty.
Wu-Cheng Lice8947e2014-11-13 17:34:16 +080089 * @pending_buffers: The pending v4l2 buffers waiting for the encoding
90 * configuration. After a previous buffer is dequeued,
91 * one buffer from the queue can be queued.
92 * @can_qbuf: Indicate that we can queue one source buffer. This is true only
93 * when the parameters to pass together with the source buffer are
94 * ready; those params are received on dequeing the previous
95 * destination buffer.
96 * @get_param_payload: Payload of V4L2_CID_PRIVATE_RK3288_GET_PARAMS. This is
97 * used to update the encoder configuration by
98 * rk_vepu_update_config().
99 * @get_param_payload_size: The size of get_param_payload.
100 * @v4l2_ctrls: v4l2 controls for VIDIOC_S_EXT_CTRLS.
Chih-Yu Huang7762c812018-02-28 11:10:59 +0900101 * @flushing: Indicate the encoder is flushing frame.
102 * @eos_buffer: The last buffer to be flushed. Reset to NULL after the buffer
103 * is enqueued to the driver.
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800104 */
105struct encoder_context {
106 void *enc;
107 pthread_mutex_t mutex;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800108 enum v4l2_buf_type output_streamon_type;
109 enum v4l2_buf_type capture_streamon_type;
henryhsuc7c06f92014-11-24 15:57:08 +0800110 struct rk_vepu_init_param init_param;
111 struct rk_vepu_runtime_param runtime_param;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800112 struct pending_buffer_queue pending_buffers;
113 bool can_qbuf;
114 void *get_param_payload;
115 size_t get_param_payload_size;
116 struct v4l2_ext_control v4l2_ctrls[MAX_NUM_GET_CONFIG_CTRLS];
Chih-Yu Huang7762c812018-02-28 11:10:59 +0900117 bool flushing;
118 struct pending_buffer *eos_buffer;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800119};
120
121static void *plugin_init(int fd);
122static void plugin_close(void *dev_ops_priv);
123static int plugin_ioctl(void *dev_ops_priv, int fd, unsigned long int cmd,
124 void *arg);
125
126/* Functions to handle various ioctl. */
127static int ioctl_streamon_locked(
128 struct encoder_context *ctx, int fd, enum v4l2_buf_type *type);
129static int ioctl_streamoff_locked(
130 struct encoder_context *ctx, int fd, enum v4l2_buf_type *type);
131static int ioctl_qbuf_locked(struct encoder_context *ctx, int fd,
132 struct v4l2_buffer *buffer);
133static int ioctl_dqbuf_locked(struct encoder_context *ctx, int fd,
134 struct v4l2_buffer *buffer);
henryhsuc7c06f92014-11-24 15:57:08 +0800135static int ioctl_s_ext_ctrls_locked(struct encoder_context *ctx, int fd,
136 struct v4l2_ext_controls *ext_ctrls);
137static int ioctl_s_parm_locked(struct encoder_context *ctx, int fd,
138 struct v4l2_streamparm *parms);
139static int ioctl_reqbufs_locked(struct encoder_context *ctx, int fd,
140 struct v4l2_requestbuffers *reqbufs);
Chih-Yu Huang7762c812018-02-28 11:10:59 +0900141static int ioctl_encoder_cmd_locked(struct encoder_context *ctx, int fd,
142 struct v4l2_encoder_cmd *argp);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800143
144/* Helper functions to manipulate the pending buffer queue. */
145
henryhsuc7c06f92014-11-24 15:57:08 +0800146static void queue_init(struct pending_buffer_queue *queue);
147static bool queue_empty(struct pending_buffer_queue *queue);
148static bool queue_full(struct pending_buffer_queue *queue);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800149/* Insert a buffer to the tail of the queue. */
henryhsuc7c06f92014-11-24 15:57:08 +0800150static int queue_push_back(struct pending_buffer_queue *queue,
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800151 struct v4l2_buffer *buffer);
henryhsuc7c06f92014-11-24 15:57:08 +0800152/* Remove a buffer from the head of the queue. */
153static void queue_pop_front(struct pending_buffer_queue *queue);
154static struct pending_buffer *queue_front(struct pending_buffer_queue *queue);
155static struct pending_buffer *queue_back(struct pending_buffer_queue *queue);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800156
Wu-Cheng Li11ae3aa2014-12-18 17:11:48 +0800157/* Returns true if the fd is Rockchip encoder device. */
158bool is_rockchip_encoder(int fd);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800159/* Set encoder configuration to the driver. */
henryhsuc7c06f92014-11-24 15:57:08 +0800160int set_encoder_config_locked(struct encoder_context *ctx, int fd,
Francois BUERGISSERaac7c202019-05-27 09:56:08 +0900161 size_t num_ctrls, uint32_t ctrls_ids[], void **payloads,
162 uint32_t payload_sizes[]);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800163/* QBUF a buffer from the pending buffer queue if it is not empty. */
henryhsuc7c06f92014-11-24 15:57:08 +0800164static int qbuf_if_pending_buffer_exists_locked(struct encoder_context *ctx,
165 int fd);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800166/* Get the encoder parameters using G_FMT and initialize libvpu. */
167static int initialize_libvpu(struct encoder_context *ctx, int fd);
168/* Return the string represenation of a libv4l command for debugging. */
169static const char *v4l_cmd2str(unsigned long int cmd);
Alpha Linfa5caae2014-11-17 09:48:35 +0800170/* Get the log level from the environment variable LIBV4L_PLUGIN_LOG_LEVEL. */
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800171static void get_log_level();
172static pthread_once_t g_get_log_level_once = PTHREAD_ONCE_INIT;
173
henryhsu58be50c2014-10-30 11:49:19 +0800174static void *plugin_init(int fd)
175{
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800176 int ret;
177 struct v4l2_query_ext_ctrl ext_ctrl;
178
179 pthread_once(&g_get_log_level_once, get_log_level);
180
181 VLOG_FD(1, "");
Wu-Cheng Li11ae3aa2014-12-18 17:11:48 +0800182 if (!is_rockchip_encoder(fd))
183 return NULL;
184
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800185 struct encoder_context *ctx = (struct encoder_context *)
186 calloc(1, sizeof(struct encoder_context));
187 if (ctx == NULL) {
188 errno = ENOMEM;
189 goto fail;
190 }
191 ret = pthread_mutex_init(&ctx->mutex, NULL);
192 if (ret)
193 goto fail;
henryhsuc7c06f92014-11-24 15:57:08 +0800194 queue_init(&ctx->pending_buffers);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800195
196 memset(&ext_ctrl, 0, sizeof(ext_ctrl));
197 ext_ctrl.id = V4L2_CID_PRIVATE_RK3288_GET_PARAMS;
198 ret = SYS_IOCTL(fd, VIDIOC_QUERY_EXT_CTRL, &ext_ctrl);
199 if (ret) {
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800200 goto fail;
201 }
202 ctx->get_param_payload_size = ext_ctrl.elem_size;
203 ctx->get_param_payload = calloc(1, ctx->get_param_payload_size);
204 if (ctx->get_param_payload == NULL) {
205 errno = ENOMEM;
206 goto fail;
207 }
henryhsuc7c06f92014-11-24 15:57:08 +0800208 ctx->runtime_param.framerate_numer = DEFAULT_FRAME_RATE;
209 ctx->runtime_param.framerate_denom = 1;
210 ctx->runtime_param.bitrate = DEFAULT_BITRATE;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800211 VLOG_FD(1, "Success. ctx=%p", ctx);
212 return ctx;
213
214fail:
215 plugin_close(ctx);
henryhsu58be50c2014-10-30 11:49:19 +0800216 return NULL;
217}
218
219static void plugin_close(void *dev_ops_priv)
220{
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800221 struct encoder_context *ctx = (struct encoder_context *)dev_ops_priv;
222
223 VLOG(1, "ctx=%p", ctx);
224 if (ctx == NULL)
225 return;
226
227 pthread_mutex_lock(&ctx->mutex);
228 if (ctx->enc)
229 rk_vepu_deinit(ctx->enc);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800230 free(ctx->get_param_payload);
231 ctx->get_param_payload = NULL;
232 pthread_mutex_unlock(&ctx->mutex);
233 pthread_mutex_destroy(&ctx->mutex);
234
235 free(ctx);
henryhsu58be50c2014-10-30 11:49:19 +0800236}
237
238static int plugin_ioctl(void *dev_ops_priv, int fd,
239 unsigned long int cmd, void *arg)
240{
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800241 int ret;
242 struct encoder_context *ctx = (struct encoder_context *)dev_ops_priv;
243
244 VLOG_FD(1, "%s(%lu)", v4l_cmd2str(cmd), _IOC_NR(cmd));
245
246 pthread_mutex_lock(&ctx->mutex);
247 switch (cmd) {
248 case VIDIOC_STREAMON:
henryhsuc7c06f92014-11-24 15:57:08 +0800249 ret = ioctl_streamon_locked(ctx, fd, arg);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800250 break;
251
252 case VIDIOC_STREAMOFF:
henryhsuc7c06f92014-11-24 15:57:08 +0800253 ret = ioctl_streamoff_locked(ctx, fd, arg);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800254 break;
255
256 case VIDIOC_QBUF:
henryhsuc7c06f92014-11-24 15:57:08 +0800257 ret = ioctl_qbuf_locked(ctx, fd, arg);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800258 break;
259
260 case VIDIOC_DQBUF:
henryhsuc7c06f92014-11-24 15:57:08 +0800261 ret = ioctl_dqbuf_locked(ctx, fd, arg);
262 break;
263
264 case VIDIOC_S_EXT_CTRLS:
265 ret = ioctl_s_ext_ctrls_locked(ctx, fd, arg);
266 break;
267
268 case VIDIOC_S_PARM:
269 ret = ioctl_s_parm_locked(ctx, fd, arg);
270 break;
271
272 case VIDIOC_REQBUFS:
273 ret = ioctl_reqbufs_locked(ctx, fd, arg);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800274 break;
275
Chih-Yu Huang7762c812018-02-28 11:10:59 +0900276 case VIDIOC_ENCODER_CMD:
277 ret = ioctl_encoder_cmd_locked(ctx, fd, arg);
278 break;
279
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800280 default:
281 ret = SYS_IOCTL(fd, cmd, arg);
282 break;
283 }
284 pthread_mutex_unlock(&ctx->mutex);
285 return ret;
286}
287
288static int ioctl_streamon_locked(
289 struct encoder_context *ctx, int fd, enum v4l2_buf_type *type)
290{
291 int ret = SYS_IOCTL(fd, VIDIOC_STREAMON, type);
292 if (ret)
293 return ret;
294
295 if (V4L2_TYPE_IS_OUTPUT(*type))
296 ctx->output_streamon_type = *type;
297 else
298 ctx->capture_streamon_type = *type;
299 if (ctx->output_streamon_type && ctx->capture_streamon_type) {
300 ret = initialize_libvpu(ctx, fd);
301 if (ret)
302 return ret;
303 ctx->can_qbuf = true;
henryhsuc7c06f92014-11-24 15:57:08 +0800304 return qbuf_if_pending_buffer_exists_locked(ctx, fd);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800305 }
306 return 0;
307}
308
309static int ioctl_streamoff_locked(
310 struct encoder_context *ctx, int fd, enum v4l2_buf_type *type)
311{
312 int ret = SYS_IOCTL(fd, VIDIOC_STREAMOFF, type);
313 if (ret)
314 return ret;
315
316 if (V4L2_TYPE_IS_OUTPUT(*type))
317 ctx->output_streamon_type = 0;
318 else
319 ctx->capture_streamon_type = 0;
320 return 0;
321}
322
323static int ioctl_qbuf_locked(struct encoder_context *ctx, int fd,
324 struct v4l2_buffer *buffer)
325{
326 size_t num_ctrls = 0;
327 uint32_t *ctrl_ids = NULL, *payload_sizes = NULL;
328 void **payloads = NULL;
329 int ret;
330
331 if (!V4L2_TYPE_IS_OUTPUT(buffer->type)) {
332 return SYS_IOCTL(fd, VIDIOC_QBUF, buffer);
333 }
334
335 if (!ctx->can_qbuf) {
336 VLOG_FD(1, "Put buffer (%d) in the pending queue.",
337 buffer->index);
338 /*
339 * The last frame is not encoded yet. Put the buffer to the
340 * pending queue.
341 */
henryhsuc7c06f92014-11-24 15:57:08 +0800342 return queue_push_back(&ctx->pending_buffers, buffer);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800343 }
344 /* Get the encoder configuration from the library. */
345 if (rk_vepu_get_config(ctx->enc, &num_ctrls, &ctrl_ids, &payloads,
346 &payload_sizes)) {
347 VLOG_FD(0, "rk_vepu_get_config failed");
348 return -EIO;
349 }
350 /* Set the encoder configuration to the driver. */
Francois BUERGISSERaac7c202019-05-27 09:56:08 +0900351 ret = set_encoder_config_locked(ctx, fd, num_ctrls, ctrl_ids, payloads,
352 payload_sizes);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800353 if (ret)
354 return ret;
355
356 ret = SYS_IOCTL(fd, VIDIOC_QBUF, buffer);
357 if (ret == 0)
358 ctx->can_qbuf = false;
359 else
360 VLOG(0, "QBUF failed. errno=%d", errno);
361 return ret;
362}
363
364static int ioctl_dqbuf_locked(struct encoder_context *ctx, int fd,
365 struct v4l2_buffer *buffer)
366{
367 struct v4l2_ext_controls ext_ctrls;
368 struct v4l2_ext_control v4l2_ctrl;
369 int ret;
henryhsuc7c06f92014-11-24 15:57:08 +0800370 uint32_t bytesused;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800371
372 if (V4L2_TYPE_IS_OUTPUT(buffer->type)) {
373 return SYS_IOCTL(fd, VIDIOC_DQBUF, buffer);
374 }
375
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800376 ret = SYS_IOCTL(fd, VIDIOC_DQBUF, buffer);
377 if (ret)
378 return ret;
379
Chih-Yu Huang7762c812018-02-28 11:10:59 +0900380 /* Get the encoder configuration and update the library.
381 * Because we dequeue a null frame to indicate flush is finished, do not
382 * update the config with this frame. */
henryhsuc7c06f92014-11-24 15:57:08 +0800383 bytesused = V4L2_TYPE_IS_MULTIPLANAR(buffer->type) ?
384 buffer->m.planes[0].bytesused : buffer->bytesused;
Chih-Yu Huang7762c812018-02-28 11:10:59 +0900385 if (bytesused) {
386 memset(ctx->get_param_payload, 0, ctx->get_param_payload_size);
387 memset(&v4l2_ctrl, 0, sizeof(v4l2_ctrl));
388 v4l2_ctrl.id = V4L2_CID_PRIVATE_RK3288_GET_PARAMS;
389 v4l2_ctrl.size = ctx->get_param_payload_size;
390 v4l2_ctrl.ptr = ctx->get_param_payload;
391 memset(&ext_ctrls, 0, sizeof(ext_ctrls));
Chih-Yu Huang7762c812018-02-28 11:10:59 +0900392 ext_ctrls.count = 1;
393 ext_ctrls.controls = &v4l2_ctrl;
394 ret = SYS_IOCTL(fd, VIDIOC_G_EXT_CTRLS, &ext_ctrls);
395 if (ret)
396 return ret;
henryhsuc7c06f92014-11-24 15:57:08 +0800397
Chih-Yu Huang7762c812018-02-28 11:10:59 +0900398 if (rk_vepu_update_config(ctx->enc, v4l2_ctrl.ptr,
399 v4l2_ctrl.size, bytesused)) {
400 VLOG_FD(0, "rk_vepu_update_config failed.");
401 return -EIO;
402 }
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800403 }
Chih-Yu Huang7762c812018-02-28 11:10:59 +0900404
405 /* When flushing buffer, we don't queue any new buffer.
406 * So we ignore checking can_qbuf and trying to qbuf new buffer. */
407 assert(ctx->flushing || !ctx->can_qbuf);
408 if (buffer->flags & V4L2_BUF_FLAG_LAST)
409 ctx->flushing = false;
410 if (ctx->flushing)
411 return 0;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800412 ctx->can_qbuf = true;
henryhsuc7c06f92014-11-24 15:57:08 +0800413 return qbuf_if_pending_buffer_exists_locked(ctx, fd);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800414}
415
henryhsuc7c06f92014-11-24 15:57:08 +0800416static int ioctl_s_ext_ctrls_locked(struct encoder_context *ctx, int fd,
417 struct v4l2_ext_controls *ext_ctrls)
418{
419 size_t i;
420 struct rk_vepu_runtime_param *runtime_param_ptr;
421
422 bool no_pending_buffer = queue_empty(&ctx->pending_buffers);
423 /*
424 * If buffer queue is empty, update parameters directly.
425 * If buffer queue is not empty, save parameters to the last buffer. And
426 * these values will be sent again when the buffer is ready to deliver.
427 */
428 if (!no_pending_buffer) {
429 struct pending_buffer *element = queue_back(&ctx->pending_buffers);
430 runtime_param_ptr = &element->next_runtime_param;
431 } else {
432 runtime_param_ptr = &ctx->runtime_param;
433 }
434
435 /*
436 * Check each extension control to update keyframe and bitrate
437 * parameters.
438 */
439 for (i = 0; i < ext_ctrls->count; i++) {
440 switch (ext_ctrls->controls[i].id) {
Heng-Ruey Hsu61eb2ec2016-05-20 18:11:53 +0800441 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
442 runtime_param_ptr->keyframe_request = true;
henryhsuc7c06f92014-11-24 15:57:08 +0800443 break;
444 case V4L2_CID_MPEG_VIDEO_BITRATE:
445 runtime_param_ptr->bitrate = ext_ctrls->controls[i].value;
446 break;
447 default:
448 break;
449 }
450 }
451
452 if (no_pending_buffer && ctx->enc) {
453 if (rk_vepu_update_parameter(ctx->enc, runtime_param_ptr)) {
454 VLOG_FD(0, "rk_vepu_update_parameter failed.");
455 return -EIO;
456 }
457 memset(runtime_param_ptr, 0, sizeof(struct rk_vepu_runtime_param));
458 }
459 /* Driver should ignore keyframe and bitrate controls */
460 return SYS_IOCTL(fd, VIDIOC_S_EXT_CTRLS, ext_ctrls);
461}
462
463static int ioctl_s_parm_locked(struct encoder_context *ctx, int fd,
464 struct v4l2_streamparm *parms)
465{
466 if (V4L2_TYPE_IS_OUTPUT(parms->type)
467 && parms->parm.output.timeperframe.denominator) {
468 struct rk_vepu_runtime_param *runtime_param_ptr;
469 bool no_pending_buffer = queue_empty(&ctx->pending_buffers);
470 struct pending_buffer *element = queue_back(&ctx->pending_buffers);
471
472 runtime_param_ptr = no_pending_buffer ? &ctx->runtime_param :
473 &element->next_runtime_param;
474 runtime_param_ptr->framerate_numer =
475 parms->parm.output.timeperframe.denominator;
476 runtime_param_ptr->framerate_denom =
477 parms->parm.output.timeperframe.numerator;
478
479 if (!no_pending_buffer || !ctx->enc)
480 return 0;
481 if (rk_vepu_update_parameter(ctx->enc, runtime_param_ptr)) {
482 VLOG_FD(0, "rk_vepu_update_parameter failed.");
483 return -EIO;
484 }
485 memset(runtime_param_ptr, 0, sizeof(struct rk_vepu_runtime_param));
486 return 0;
487 }
488 return SYS_IOCTL(fd, VIDIOC_S_PARM, parms);
489}
490
491static int ioctl_reqbufs_locked(struct encoder_context *ctx, int fd,
492 struct v4l2_requestbuffers *reqbufs)
493{
494 int ret = SYS_IOCTL(fd, VIDIOC_REQBUFS, reqbufs);
495 if (ret)
496 return ret;
497 queue_init(&ctx->pending_buffers);
498 return 0;
499}
500
Chih-Yu Huang7762c812018-02-28 11:10:59 +0900501static int ioctl_encoder_cmd_locked(struct encoder_context *ctx, int fd,
502 struct v4l2_encoder_cmd *argp)
503{
504 if (argp->cmd == V4L2_ENC_CMD_STOP) {
505 if (ctx->flushing) {
506 VLOG_FD(0, "previous stop command is not handled yet.");
507 return -EINVAL;
508 }
509 ctx->flushing = true;
510 /* If there is any pending buffer, then send the stop command
511 * after we enqueue the last buffer. Otherwise, we just send
512 * the command to the kernel instantly. */
513 if (!queue_empty(&ctx->pending_buffers)) {
514 ctx->eos_buffer = queue_back(&ctx->pending_buffers);
515 return 0;
516 }
517 }
518 return SYS_IOCTL(fd, VIDIOC_ENCODER_CMD, argp);
519}
520
Wu-Cheng Li11ae3aa2014-12-18 17:11:48 +0800521bool is_rockchip_encoder(int fd) {
522 struct v4l2_capability cap;
523 memset(&cap, 0, sizeof(cap));
524 int ret = SYS_IOCTL(fd, VIDIOC_QUERYCAP, &cap);
525 if (ret)
526 return false;
Alpha Linfa5caae2014-11-17 09:48:35 +0800527 return strcmp(RK3288_VPU_NAME, (const char *)cap.driver) == 0;
Wu-Cheng Li11ae3aa2014-12-18 17:11:48 +0800528}
529
henryhsuc7c06f92014-11-24 15:57:08 +0800530int set_encoder_config_locked(struct encoder_context *ctx, int fd,
Francois BUERGISSERaac7c202019-05-27 09:56:08 +0900531 size_t num_ctrls, uint32_t ctrl_ids[], void **payloads,
532 uint32_t payload_sizes[])
henryhsuc7c06f92014-11-24 15:57:08 +0800533{
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800534 size_t i;
535 struct v4l2_ext_controls ext_ctrls;
536
537 if (num_ctrls <= 0)
538 return 0;
539
540 assert(num_ctrls <= MAX_NUM_GET_CONFIG_CTRLS);
henryhsuc7c06f92014-11-24 15:57:08 +0800541 if (num_ctrls > MAX_NUM_GET_CONFIG_CTRLS) {
542 VLOG_FD(0, "The number of controls exceeds limit.");
543 return -EIO;
544 }
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800545 memset(&ext_ctrls, 0, sizeof(ext_ctrls));
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800546 ext_ctrls.count = num_ctrls;
547 ext_ctrls.controls = ctx->v4l2_ctrls;
548 memset(ctx->v4l2_ctrls, 0, sizeof(ctx->v4l2_ctrls));
549 for (i = 0; i < num_ctrls; ++i) {
550 ctx->v4l2_ctrls[i].id = ctrl_ids[i];
551 ctx->v4l2_ctrls[i].ptr = payloads[i];
552 ctx->v4l2_ctrls[i].size = payload_sizes[i];
553 }
554 int ret = SYS_IOCTL(fd, VIDIOC_S_EXT_CTRLS, &ext_ctrls);
555 if (ret) {
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800556 return ret;
557 }
558 return 0;
559}
560
henryhsuc7c06f92014-11-24 15:57:08 +0800561static int qbuf_if_pending_buffer_exists_locked(struct encoder_context *ctx,
562 int fd)
563{
564 if (!queue_empty(&ctx->pending_buffers)) {
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800565 int ret;
henryhsuc7c06f92014-11-24 15:57:08 +0800566 struct pending_buffer *element = queue_front(&ctx->pending_buffers);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800567 VLOG_FD(1, "QBUF a buffer (%d) from the pending queue.",
568 element->buffer.index);
henryhsuc7c06f92014-11-24 15:57:08 +0800569 if (rk_vepu_update_parameter(ctx->enc, &element->next_runtime_param)) {
570 VLOG_FD(0, "rk_vepu_update_parameter failed.");
571 return -EIO;
572 }
Tomasz Figaaed2ed92015-02-25 14:05:53 +0900573 memset(&element->next_runtime_param, 0,
574 sizeof(element->next_runtime_param));
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800575 ret = ioctl_qbuf_locked(ctx, fd, &element->buffer);
576 if (ret)
577 return ret;
henryhsuc7c06f92014-11-24 15:57:08 +0800578 queue_pop_front(&ctx->pending_buffers);
Chih-Yu Huang7762c812018-02-28 11:10:59 +0900579 /* QBUF the last buffer to be flushed, send stop command. */
580 if (element == ctx->eos_buffer) {
581 ctx->eos_buffer = NULL;
582 struct v4l2_encoder_cmd argp = {
583 .cmd = V4L2_ENC_CMD_STOP,
584 };
585 return SYS_IOCTL(fd, VIDIOC_ENCODER_CMD, &argp);
586 }
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800587 }
588 return 0;
589}
590
henryhsuc7c06f92014-11-24 15:57:08 +0800591static int initialize_libvpu(struct encoder_context *ctx, int fd)
592{
593 struct rk_vepu_init_param init_param;
594 memset(&init_param, 0, sizeof(init_param));
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800595
Alpha Linfa5caae2014-11-17 09:48:35 +0800596 /* Get the input format. */
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800597 struct v4l2_format format;
598 memset(&format, 0, sizeof(format));
599 format.type = ctx->output_streamon_type;
600 int ret = SYS_IOCTL(fd, VIDIOC_G_FMT, &format);
Alpha Linfa5caae2014-11-17 09:48:35 +0800601 if (ret)
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800602 return ret;
henryhsuc7c06f92014-11-24 15:57:08 +0800603 init_param.input_format = format.fmt.pix_mp.pixelformat;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800604
Alpha Linfa5caae2014-11-17 09:48:35 +0800605 /* Get the output format. */
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800606 memset(&format, 0, sizeof(format));
607 format.type = ctx->capture_streamon_type;
608 ret = SYS_IOCTL(fd, VIDIOC_G_FMT, &format);
Alpha Linfa5caae2014-11-17 09:48:35 +0800609 if (ret)
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800610 return ret;
henryhsuc7c06f92014-11-24 15:57:08 +0800611 init_param.output_format = format.fmt.pix_mp.pixelformat;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800612
Alpha Linfa5caae2014-11-17 09:48:35 +0800613 /* Get the cropped size. */
614 struct v4l2_crop crop;
615 memset(&crop, 0, sizeof(crop));
616 crop.type = ctx->output_streamon_type;
617 ret = SYS_IOCTL(fd, VIDIOC_G_CROP, &crop);
618 if (ret)
619 return ret;
620 init_param.width = crop.c.width;
621 init_param.height = crop.c.height;
622
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800623 /*
624 * If the encoder library has initialized and parameters have not
625 * changed, skip the initialization.
626 */
henryhsuc7c06f92014-11-24 15:57:08 +0800627 if (ctx->enc && memcmp(&init_param, &ctx->init_param, sizeof(init_param))) {
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800628 rk_vepu_deinit(ctx->enc);
henryhsuc7c06f92014-11-24 15:57:08 +0800629 ctx->enc = NULL;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800630 }
henryhsuc7c06f92014-11-24 15:57:08 +0800631 if (!ctx->enc) {
632 memcpy(&ctx->init_param, &init_param, sizeof(init_param));
633 ctx->enc = rk_vepu_init(&init_param);
634 if (ctx->enc == NULL) {
635 VLOG_FD(0, "Failed to initialize encoder library.");
636 return -EIO;
637 }
638 }
639 if (rk_vepu_update_parameter(ctx->enc, &ctx->runtime_param)) {
640 VLOG_FD(0, "rk_vepu_update_parameter failed.");
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800641 return -EIO;
642 }
henryhsuc7c06f92014-11-24 15:57:08 +0800643 memset(&ctx->runtime_param, 0, sizeof(struct rk_vepu_runtime_param));
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800644 return 0;
645}
646
henryhsuc7c06f92014-11-24 15:57:08 +0800647static void queue_init(struct pending_buffer_queue *queue)
648{
649 memset(queue, 0, sizeof(struct pending_buffer_queue));
650}
651
652static bool queue_empty(struct pending_buffer_queue *queue)
653{
654 return queue->count == 0;
655}
656
657static bool queue_full(struct pending_buffer_queue *queue)
658{
659 return queue->count == PENDING_BUFFER_QUEUE_SIZE;
660}
661
662static int queue_push_back(struct pending_buffer_queue *queue,
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800663 struct v4l2_buffer *buffer)
664{
henryhsuc7c06f92014-11-24 15:57:08 +0800665 if (queue_full(queue))
666 return -ENOMEM;
667 int rear = (queue->front + queue->count) % PENDING_BUFFER_QUEUE_SIZE;
668 queue->count++;
669 struct pending_buffer *entry = &queue->buf_array[rear];
670 memset(entry, 0, sizeof(struct pending_buffer));
671
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800672 memcpy(&entry->buffer, buffer, sizeof(*buffer));
673 if (V4L2_TYPE_IS_MULTIPLANAR(buffer->type)) {
674 memset(entry->planes, 0,
675 sizeof(struct v4l2_plane) * VIDEO_MAX_PLANES);
676 memcpy(entry->planes, buffer->m.planes,
677 sizeof(struct v4l2_plane) * buffer->length);
678 entry->buffer.m.planes = entry->planes;
679 }
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800680 return 0;
681}
682
henryhsuc7c06f92014-11-24 15:57:08 +0800683static void queue_pop_front(struct pending_buffer_queue *queue)
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800684{
henryhsuc7c06f92014-11-24 15:57:08 +0800685 assert(!queue_empty(queue));
686 queue->count--;
687 queue->front = (queue->front + 1) % PENDING_BUFFER_QUEUE_SIZE;
688}
689
690static struct pending_buffer *queue_front(struct pending_buffer_queue *queue)
691{
692 if (queue_empty(queue))
693 return NULL;
694 return &queue->buf_array[queue->front];
695}
696
697static struct pending_buffer *queue_back(struct pending_buffer_queue *queue)
698{
699 if (queue_empty(queue))
700 return NULL;
701 return &queue->buf_array[(queue->front + queue->count - 1) %
702 PENDING_BUFFER_QUEUE_SIZE];
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800703}
704
705static void get_log_level()
706{
707 char *log_level_str = getenv("LIBV4L_PLUGIN_LOG_LEVEL");
708 if (log_level_str != NULL)
709 g_log_level = strtol(log_level_str, NULL, 10);
710}
711
712static const char* v4l_cmd2str(unsigned long int cmd)
713{
714 switch (cmd) {
715 case VIDIOC_QUERYCAP:
716 return "VIDIOC_QUERYCAP";
717 case VIDIOC_TRY_FMT:
718 return "VIDIOC_TRY_FMT";
719 case VIDIOC_S_FMT:
720 return "VIDIOC_S_FMT";
721 case VIDIOC_G_FMT:
722 return "VIDIOC_G_FMT";
723 case VIDIOC_ENUM_FMT:
724 return "VIDIOC_ENUM_FMT";
725 case VIDIOC_S_PARM:
726 return "VIDIOC_S_PARM";
727 case VIDIOC_G_PARM:
728 return "VIDIOC_G_PARM";
729 case VIDIOC_QBUF:
730 return "VIDIOC_QBUF";
731 case VIDIOC_DQBUF:
732 return "VIDIOC_DQBUF";
733 case VIDIOC_PREPARE_BUF:
734 return "VIDIOC_PREPARE_BUF";
735 case VIDIOC_CREATE_BUFS:
736 return "VIDIOC_CREATE_BUFS";
737 case VIDIOC_REQBUFS:
738 return "VIDIOC_REQBUFS";
739 case VIDIOC_STREAMON:
740 return "VIDIOC_STREAMON";
741 case VIDIOC_STREAMOFF:
742 return "VIDIOC_STREAMOFF";
743 case VIDIOC_S_CROP:
744 return "VIDIOC_S_CROP";
745 case VIDIOC_S_CTRL:
746 return "VIDIOC_S_CTRL";
747 case VIDIOC_G_EXT_CTRLS:
748 return "VIDIOC_G_EXT_CTRLS";
749 case VIDIOC_S_EXT_CTRLS:
750 return "VIDIOC_S_EXT_CTRLS";
751 case VIDIOC_QUERYBUF:
752 return "VIDIOC_QUERYBUF";
Chih-Yu Huang7762c812018-02-28 11:10:59 +0900753 case VIDIOC_ENCODER_CMD:
754 return "VIDIOC_ENCODER_CMD";
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800755 default:
756 return "UNKNOWN";
757 }
henryhsu58be50c2014-10-30 11:49:19 +0800758}
759
760PLUGIN_PUBLIC const struct libv4l_dev_ops libv4l2_plugin = {
761 .init = &plugin_init,
762 .close = &plugin_close,
763 .ioctl = &plugin_ioctl,
764};