blob: aaa496795a561de9d7269ce5443a093fd332c399 [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)); \
henryhsuc7c06f92014-11-24 15:57:08 +080033 if ((ret && errno != EAGAIN) || g_log_level >= 2) \
34 fprintf(stderr, "SYS_ioctl: %s(%lu): fd=%d, ret=%d, errno=%d\n",\
35 v4l_cmd2str(cmd), _IOC_NR((unsigned long)cmd), fd, ret, \
36 errno); \
Wu-Cheng Lice8947e2014-11-13 17:34:16 +080037 ret; \
38 })
henryhsu58be50c2014-10-30 11:49:19 +080039
40#if HAVE_VISIBILITY
41#define PLUGIN_PUBLIC __attribute__ ((__visibility__("default")))
42#else
43#define PLUGIN_PUBLIC
44#endif
45
Wu-Cheng Li11ae3aa2014-12-18 17:11:48 +080046#define RK3288_VPU_NAME "rk3288-vpu-enc"
Wu-Cheng Lice8947e2014-11-13 17:34:16 +080047#define DEFAULT_FRAME_RATE 30
48#define DEFAULT_BITRATE 1000000
henryhsuc7c06f92014-11-24 15:57:08 +080049#define PENDING_BUFFER_QUEUE_SIZE VIDEO_MAX_FRAME
Wu-Cheng Lice8947e2014-11-13 17:34:16 +080050
henryhsuc7c06f92014-11-24 15:57:08 +080051/*
52 * struct pending_buffer - A v4l2 buffer pending for QBUF.
53 * @buffer: v4l2 buffer for QBUF.
54 * @planes: plane info of v4l2 buffer.
55 * @next_runtime_param: runtime parameters like framerate, bitrate, and
56 * keyframe for the next buffer.
57 */
Wu-Cheng Lice8947e2014-11-13 17:34:16 +080058struct pending_buffer {
59 struct v4l2_buffer buffer;
60 struct v4l2_plane planes[VIDEO_MAX_PLANES];
henryhsuc7c06f92014-11-24 15:57:08 +080061 struct rk_vepu_runtime_param next_runtime_param;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +080062};
henryhsuc7c06f92014-11-24 15:57:08 +080063
64/*
65 * struct pending_buffer_queue - a ring buffer of pending buffers.
66 * @count: the number of buffers stored in the array.
67 * @front: the index of the first ready buffer.
68 * @buf_array: pending buffer array.
69 */
70struct pending_buffer_queue {
71 uint32_t count;
72 int32_t front;
73 struct pending_buffer buf_array[PENDING_BUFFER_QUEUE_SIZE];
74};
Wu-Cheng Lice8947e2014-11-13 17:34:16 +080075
76/*
77 * struct encoder_context - the context of an encoder instance.
78 * @enc: Encoder instance returned from rk_vepu_create().
79 * @mutex: The mutex to protect encoder_context.
Wu-Cheng Lice8947e2014-11-13 17:34:16 +080080 * @output_streamon_type: Type of output interface when it streams on.
81 * @capture_streamon_type: Type of capture interface when it streams on.
henryhsuc7c06f92014-11-24 15:57:08 +080082 * @init_param: Encoding parameters like input format, resolution, and etc.
83 * These parameters will be passed to encoder at libvpu
84 * initialization.
85 * @runtime_param: Runtime parameters like framerate, bitrate, and
86 * keyframe. This is only used for receiving ext_ctrls
87 * before streamon and pending buffer queue is empty.
Wu-Cheng Lice8947e2014-11-13 17:34:16 +080088 * @pending_buffers: The pending v4l2 buffers waiting for the encoding
89 * configuration. After a previous buffer is dequeued,
90 * one buffer from the queue can be queued.
91 * @can_qbuf: Indicate that we can queue one source buffer. This is true only
92 * when the parameters to pass together with the source buffer are
93 * ready; those params are received on dequeing the previous
94 * destination buffer.
95 * @get_param_payload: Payload of V4L2_CID_PRIVATE_RK3288_GET_PARAMS. This is
96 * used to update the encoder configuration by
97 * rk_vepu_update_config().
98 * @get_param_payload_size: The size of get_param_payload.
99 * @v4l2_ctrls: v4l2 controls for VIDIOC_S_EXT_CTRLS.
100 */
101struct encoder_context {
102 void *enc;
103 pthread_mutex_t mutex;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800104 enum v4l2_buf_type output_streamon_type;
105 enum v4l2_buf_type capture_streamon_type;
henryhsuc7c06f92014-11-24 15:57:08 +0800106 struct rk_vepu_init_param init_param;
107 struct rk_vepu_runtime_param runtime_param;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800108 struct pending_buffer_queue pending_buffers;
109 bool can_qbuf;
110 void *get_param_payload;
111 size_t get_param_payload_size;
112 struct v4l2_ext_control v4l2_ctrls[MAX_NUM_GET_CONFIG_CTRLS];
113};
114
115static void *plugin_init(int fd);
116static void plugin_close(void *dev_ops_priv);
117static int plugin_ioctl(void *dev_ops_priv, int fd, unsigned long int cmd,
118 void *arg);
119
120/* Functions to handle various ioctl. */
121static int ioctl_streamon_locked(
122 struct encoder_context *ctx, int fd, enum v4l2_buf_type *type);
123static int ioctl_streamoff_locked(
124 struct encoder_context *ctx, int fd, enum v4l2_buf_type *type);
125static int ioctl_qbuf_locked(struct encoder_context *ctx, int fd,
126 struct v4l2_buffer *buffer);
127static int ioctl_dqbuf_locked(struct encoder_context *ctx, int fd,
128 struct v4l2_buffer *buffer);
henryhsuc7c06f92014-11-24 15:57:08 +0800129static int ioctl_s_ext_ctrls_locked(struct encoder_context *ctx, int fd,
130 struct v4l2_ext_controls *ext_ctrls);
131static int ioctl_s_parm_locked(struct encoder_context *ctx, int fd,
132 struct v4l2_streamparm *parms);
133static int ioctl_reqbufs_locked(struct encoder_context *ctx, int fd,
134 struct v4l2_requestbuffers *reqbufs);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800135
136/* Helper functions to manipulate the pending buffer queue. */
137
henryhsuc7c06f92014-11-24 15:57:08 +0800138static void queue_init(struct pending_buffer_queue *queue);
139static bool queue_empty(struct pending_buffer_queue *queue);
140static bool queue_full(struct pending_buffer_queue *queue);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800141/* Insert a buffer to the tail of the queue. */
henryhsuc7c06f92014-11-24 15:57:08 +0800142static int queue_push_back(struct pending_buffer_queue *queue,
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800143 struct v4l2_buffer *buffer);
henryhsuc7c06f92014-11-24 15:57:08 +0800144/* Remove a buffer from the head of the queue. */
145static void queue_pop_front(struct pending_buffer_queue *queue);
146static struct pending_buffer *queue_front(struct pending_buffer_queue *queue);
147static struct pending_buffer *queue_back(struct pending_buffer_queue *queue);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800148
Wu-Cheng Li11ae3aa2014-12-18 17:11:48 +0800149/* Returns true if the fd is Rockchip encoder device. */
150bool is_rockchip_encoder(int fd);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800151/* Set encoder configuration to the driver. */
henryhsuc7c06f92014-11-24 15:57:08 +0800152int set_encoder_config_locked(struct encoder_context *ctx, int fd,
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800153 uint32_t buffer_index, size_t num_ctrls, uint32_t ctrls_ids[],
154 void **payloads, uint32_t payload_sizes[]);
155/* QBUF a buffer from the pending buffer queue if it is not empty. */
henryhsuc7c06f92014-11-24 15:57:08 +0800156static int qbuf_if_pending_buffer_exists_locked(struct encoder_context *ctx,
157 int fd);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800158/* Get the encoder parameters using G_FMT and initialize libvpu. */
159static int initialize_libvpu(struct encoder_context *ctx, int fd);
160/* Return the string represenation of a libv4l command for debugging. */
161static const char *v4l_cmd2str(unsigned long int cmd);
Alpha Linfa5caae2014-11-17 09:48:35 +0800162/* Get the log level from the environment variable LIBV4L_PLUGIN_LOG_LEVEL. */
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800163static void get_log_level();
164static pthread_once_t g_get_log_level_once = PTHREAD_ONCE_INIT;
165
henryhsu58be50c2014-10-30 11:49:19 +0800166static void *plugin_init(int fd)
167{
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800168 int ret;
169 struct v4l2_query_ext_ctrl ext_ctrl;
170
171 pthread_once(&g_get_log_level_once, get_log_level);
172
173 VLOG_FD(1, "");
Wu-Cheng Li11ae3aa2014-12-18 17:11:48 +0800174 if (!is_rockchip_encoder(fd))
175 return NULL;
176
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800177 struct encoder_context *ctx = (struct encoder_context *)
178 calloc(1, sizeof(struct encoder_context));
179 if (ctx == NULL) {
180 errno = ENOMEM;
181 goto fail;
182 }
183 ret = pthread_mutex_init(&ctx->mutex, NULL);
184 if (ret)
185 goto fail;
henryhsuc7c06f92014-11-24 15:57:08 +0800186 queue_init(&ctx->pending_buffers);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800187
188 memset(&ext_ctrl, 0, sizeof(ext_ctrl));
189 ext_ctrl.id = V4L2_CID_PRIVATE_RK3288_GET_PARAMS;
190 ret = SYS_IOCTL(fd, VIDIOC_QUERY_EXT_CTRL, &ext_ctrl);
191 if (ret) {
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800192 goto fail;
193 }
194 ctx->get_param_payload_size = ext_ctrl.elem_size;
195 ctx->get_param_payload = calloc(1, ctx->get_param_payload_size);
196 if (ctx->get_param_payload == NULL) {
197 errno = ENOMEM;
198 goto fail;
199 }
henryhsuc7c06f92014-11-24 15:57:08 +0800200 ctx->runtime_param.framerate_numer = DEFAULT_FRAME_RATE;
201 ctx->runtime_param.framerate_denom = 1;
202 ctx->runtime_param.bitrate = DEFAULT_BITRATE;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800203 VLOG_FD(1, "Success. ctx=%p", ctx);
204 return ctx;
205
206fail:
207 plugin_close(ctx);
henryhsu58be50c2014-10-30 11:49:19 +0800208 return NULL;
209}
210
211static void plugin_close(void *dev_ops_priv)
212{
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800213 struct encoder_context *ctx = (struct encoder_context *)dev_ops_priv;
214
215 VLOG(1, "ctx=%p", ctx);
216 if (ctx == NULL)
217 return;
218
219 pthread_mutex_lock(&ctx->mutex);
220 if (ctx->enc)
221 rk_vepu_deinit(ctx->enc);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800222 free(ctx->get_param_payload);
223 ctx->get_param_payload = NULL;
224 pthread_mutex_unlock(&ctx->mutex);
225 pthread_mutex_destroy(&ctx->mutex);
226
227 free(ctx);
henryhsu58be50c2014-10-30 11:49:19 +0800228}
229
230static int plugin_ioctl(void *dev_ops_priv, int fd,
231 unsigned long int cmd, void *arg)
232{
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800233 int ret;
234 struct encoder_context *ctx = (struct encoder_context *)dev_ops_priv;
235
236 VLOG_FD(1, "%s(%lu)", v4l_cmd2str(cmd), _IOC_NR(cmd));
237
238 pthread_mutex_lock(&ctx->mutex);
239 switch (cmd) {
240 case VIDIOC_STREAMON:
henryhsuc7c06f92014-11-24 15:57:08 +0800241 ret = ioctl_streamon_locked(ctx, fd, arg);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800242 break;
243
244 case VIDIOC_STREAMOFF:
henryhsuc7c06f92014-11-24 15:57:08 +0800245 ret = ioctl_streamoff_locked(ctx, fd, arg);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800246 break;
247
248 case VIDIOC_QBUF:
henryhsuc7c06f92014-11-24 15:57:08 +0800249 ret = ioctl_qbuf_locked(ctx, fd, arg);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800250 break;
251
252 case VIDIOC_DQBUF:
henryhsuc7c06f92014-11-24 15:57:08 +0800253 ret = ioctl_dqbuf_locked(ctx, fd, arg);
254 break;
255
256 case VIDIOC_S_EXT_CTRLS:
257 ret = ioctl_s_ext_ctrls_locked(ctx, fd, arg);
258 break;
259
260 case VIDIOC_S_PARM:
261 ret = ioctl_s_parm_locked(ctx, fd, arg);
262 break;
263
264 case VIDIOC_REQBUFS:
265 ret = ioctl_reqbufs_locked(ctx, fd, arg);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800266 break;
267
268 default:
269 ret = SYS_IOCTL(fd, cmd, arg);
270 break;
271 }
272 pthread_mutex_unlock(&ctx->mutex);
273 return ret;
274}
275
276static int ioctl_streamon_locked(
277 struct encoder_context *ctx, int fd, enum v4l2_buf_type *type)
278{
279 int ret = SYS_IOCTL(fd, VIDIOC_STREAMON, type);
280 if (ret)
281 return ret;
282
283 if (V4L2_TYPE_IS_OUTPUT(*type))
284 ctx->output_streamon_type = *type;
285 else
286 ctx->capture_streamon_type = *type;
287 if (ctx->output_streamon_type && ctx->capture_streamon_type) {
288 ret = initialize_libvpu(ctx, fd);
289 if (ret)
290 return ret;
291 ctx->can_qbuf = true;
henryhsuc7c06f92014-11-24 15:57:08 +0800292 return qbuf_if_pending_buffer_exists_locked(ctx, fd);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800293 }
294 return 0;
295}
296
297static int ioctl_streamoff_locked(
298 struct encoder_context *ctx, int fd, enum v4l2_buf_type *type)
299{
300 int ret = SYS_IOCTL(fd, VIDIOC_STREAMOFF, type);
301 if (ret)
302 return ret;
303
304 if (V4L2_TYPE_IS_OUTPUT(*type))
305 ctx->output_streamon_type = 0;
306 else
307 ctx->capture_streamon_type = 0;
308 return 0;
309}
310
311static int ioctl_qbuf_locked(struct encoder_context *ctx, int fd,
312 struct v4l2_buffer *buffer)
313{
314 size_t num_ctrls = 0;
315 uint32_t *ctrl_ids = NULL, *payload_sizes = NULL;
316 void **payloads = NULL;
317 int ret;
318
319 if (!V4L2_TYPE_IS_OUTPUT(buffer->type)) {
320 return SYS_IOCTL(fd, VIDIOC_QBUF, buffer);
321 }
322
323 if (!ctx->can_qbuf) {
324 VLOG_FD(1, "Put buffer (%d) in the pending queue.",
325 buffer->index);
326 /*
327 * The last frame is not encoded yet. Put the buffer to the
328 * pending queue.
329 */
henryhsuc7c06f92014-11-24 15:57:08 +0800330 return queue_push_back(&ctx->pending_buffers, buffer);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800331 }
332 /* Get the encoder configuration from the library. */
333 if (rk_vepu_get_config(ctx->enc, &num_ctrls, &ctrl_ids, &payloads,
334 &payload_sizes)) {
335 VLOG_FD(0, "rk_vepu_get_config failed");
336 return -EIO;
337 }
338 /* Set the encoder configuration to the driver. */
henryhsuc7c06f92014-11-24 15:57:08 +0800339 ret = set_encoder_config_locked(ctx, fd, buffer->index, num_ctrls, ctrl_ids,
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800340 payloads, payload_sizes);
341 if (ret)
342 return ret;
343
Jeffy Chen756f2a42014-12-15 10:32:22 +0800344 /* TODO: change this to config_store after the header is updated. */
345 buffer->reserved2 = buffer->index + 1;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800346 ret = SYS_IOCTL(fd, VIDIOC_QBUF, buffer);
347 if (ret == 0)
348 ctx->can_qbuf = false;
349 else
350 VLOG(0, "QBUF failed. errno=%d", errno);
351 return ret;
352}
353
354static int ioctl_dqbuf_locked(struct encoder_context *ctx, int fd,
355 struct v4l2_buffer *buffer)
356{
357 struct v4l2_ext_controls ext_ctrls;
358 struct v4l2_ext_control v4l2_ctrl;
359 int ret;
henryhsuc7c06f92014-11-24 15:57:08 +0800360 uint32_t bytesused;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800361
362 if (V4L2_TYPE_IS_OUTPUT(buffer->type)) {
363 return SYS_IOCTL(fd, VIDIOC_DQBUF, buffer);
364 }
365
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800366 ret = SYS_IOCTL(fd, VIDIOC_DQBUF, buffer);
367 if (ret)
368 return ret;
369
Wu-Cheng Lif1e616b2014-11-21 15:11:34 +0800370 assert(!ctx->can_qbuf);
371
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800372 /* Get the encoder configuration and update the library. */
373 memset(ctx->get_param_payload, 0, ctx->get_param_payload_size);
374 memset(&v4l2_ctrl, 0, sizeof(v4l2_ctrl));
375 v4l2_ctrl.id = V4L2_CID_PRIVATE_RK3288_GET_PARAMS;
376 v4l2_ctrl.size = ctx->get_param_payload_size;
Wu-Cheng Liadc8ec82014-11-21 15:51:59 +0800377 v4l2_ctrl.ptr = ctx->get_param_payload;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800378 memset(&ext_ctrls, 0, sizeof(ext_ctrls));
379 /* TODO: change this to config_store after the header is updated. */
henryhsuc7c06f92014-11-24 15:57:08 +0800380 ext_ctrls.ctrl_class = 0;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800381 ext_ctrls.count = 1;
382 ext_ctrls.controls = &v4l2_ctrl;
383 ret = SYS_IOCTL(fd, VIDIOC_G_EXT_CTRLS, &ext_ctrls);
henryhsuc7c06f92014-11-24 15:57:08 +0800384 if (ret)
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800385 return ret;
henryhsuc7c06f92014-11-24 15:57:08 +0800386 bytesused = V4L2_TYPE_IS_MULTIPLANAR(buffer->type) ?
387 buffer->m.planes[0].bytesused : buffer->bytesused;
388
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800389 if (rk_vepu_update_config(ctx->enc, v4l2_ctrl.ptr, v4l2_ctrl.size,
henryhsuc7c06f92014-11-24 15:57:08 +0800390 bytesused)) {
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800391 VLOG_FD(0, "rk_vepu_update_config failed.");
392 return -EIO;
393 }
394 ctx->can_qbuf = true;
henryhsuc7c06f92014-11-24 15:57:08 +0800395 return qbuf_if_pending_buffer_exists_locked(ctx, fd);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800396}
397
henryhsuc7c06f92014-11-24 15:57:08 +0800398static int ioctl_s_ext_ctrls_locked(struct encoder_context *ctx, int fd,
399 struct v4l2_ext_controls *ext_ctrls)
400{
401 size_t i;
402 struct rk_vepu_runtime_param *runtime_param_ptr;
403
404 bool no_pending_buffer = queue_empty(&ctx->pending_buffers);
405 /*
406 * If buffer queue is empty, update parameters directly.
407 * If buffer queue is not empty, save parameters to the last buffer. And
408 * these values will be sent again when the buffer is ready to deliver.
409 */
410 if (!no_pending_buffer) {
411 struct pending_buffer *element = queue_back(&ctx->pending_buffers);
412 runtime_param_ptr = &element->next_runtime_param;
413 } else {
414 runtime_param_ptr = &ctx->runtime_param;
415 }
416
417 /*
418 * Check each extension control to update keyframe and bitrate
419 * parameters.
420 */
421 for (i = 0; i < ext_ctrls->count; i++) {
422 switch (ext_ctrls->controls[i].id) {
423 case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
424 if (ext_ctrls->controls[i].value ==
Tomasz Figaf37b1cb2015-02-23 15:32:42 +0900425 V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_I_FRAME)
426 runtime_param_ptr->keyframe_request = true;
henryhsuc7c06f92014-11-24 15:57:08 +0800427 break;
428 case V4L2_CID_MPEG_VIDEO_BITRATE:
429 runtime_param_ptr->bitrate = ext_ctrls->controls[i].value;
430 break;
431 default:
432 break;
433 }
434 }
435
436 if (no_pending_buffer && ctx->enc) {
437 if (rk_vepu_update_parameter(ctx->enc, runtime_param_ptr)) {
438 VLOG_FD(0, "rk_vepu_update_parameter failed.");
439 return -EIO;
440 }
441 memset(runtime_param_ptr, 0, sizeof(struct rk_vepu_runtime_param));
442 }
443 /* Driver should ignore keyframe and bitrate controls */
444 return SYS_IOCTL(fd, VIDIOC_S_EXT_CTRLS, ext_ctrls);
445}
446
447static int ioctl_s_parm_locked(struct encoder_context *ctx, int fd,
448 struct v4l2_streamparm *parms)
449{
450 if (V4L2_TYPE_IS_OUTPUT(parms->type)
451 && parms->parm.output.timeperframe.denominator) {
452 struct rk_vepu_runtime_param *runtime_param_ptr;
453 bool no_pending_buffer = queue_empty(&ctx->pending_buffers);
454 struct pending_buffer *element = queue_back(&ctx->pending_buffers);
455
456 runtime_param_ptr = no_pending_buffer ? &ctx->runtime_param :
457 &element->next_runtime_param;
458 runtime_param_ptr->framerate_numer =
459 parms->parm.output.timeperframe.denominator;
460 runtime_param_ptr->framerate_denom =
461 parms->parm.output.timeperframe.numerator;
462
463 if (!no_pending_buffer || !ctx->enc)
464 return 0;
465 if (rk_vepu_update_parameter(ctx->enc, runtime_param_ptr)) {
466 VLOG_FD(0, "rk_vepu_update_parameter failed.");
467 return -EIO;
468 }
469 memset(runtime_param_ptr, 0, sizeof(struct rk_vepu_runtime_param));
470 return 0;
471 }
472 return SYS_IOCTL(fd, VIDIOC_S_PARM, parms);
473}
474
475static int ioctl_reqbufs_locked(struct encoder_context *ctx, int fd,
476 struct v4l2_requestbuffers *reqbufs)
477{
478 int ret = SYS_IOCTL(fd, VIDIOC_REQBUFS, reqbufs);
479 if (ret)
480 return ret;
481 queue_init(&ctx->pending_buffers);
482 return 0;
483}
484
Wu-Cheng Li11ae3aa2014-12-18 17:11:48 +0800485bool is_rockchip_encoder(int fd) {
486 struct v4l2_capability cap;
487 memset(&cap, 0, sizeof(cap));
488 int ret = SYS_IOCTL(fd, VIDIOC_QUERYCAP, &cap);
489 if (ret)
490 return false;
Alpha Linfa5caae2014-11-17 09:48:35 +0800491 return strcmp(RK3288_VPU_NAME, (const char *)cap.driver) == 0;
Wu-Cheng Li11ae3aa2014-12-18 17:11:48 +0800492}
493
henryhsuc7c06f92014-11-24 15:57:08 +0800494int set_encoder_config_locked(struct encoder_context *ctx, int fd,
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800495 uint32_t buffer_index, size_t num_ctrls, uint32_t ctrl_ids[],
henryhsuc7c06f92014-11-24 15:57:08 +0800496 void **payloads, uint32_t payload_sizes[])
497{
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800498 size_t i;
499 struct v4l2_ext_controls ext_ctrls;
500
501 if (num_ctrls <= 0)
502 return 0;
503
504 assert(num_ctrls <= MAX_NUM_GET_CONFIG_CTRLS);
henryhsuc7c06f92014-11-24 15:57:08 +0800505 if (num_ctrls > MAX_NUM_GET_CONFIG_CTRLS) {
506 VLOG_FD(0, "The number of controls exceeds limit.");
507 return -EIO;
508 }
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800509 memset(&ext_ctrls, 0, sizeof(ext_ctrls));
510 /* TODO: change this to config_store after the header is updated. */
511 ext_ctrls.ctrl_class = buffer_index + 1;
512 ext_ctrls.count = num_ctrls;
513 ext_ctrls.controls = ctx->v4l2_ctrls;
514 memset(ctx->v4l2_ctrls, 0, sizeof(ctx->v4l2_ctrls));
515 for (i = 0; i < num_ctrls; ++i) {
516 ctx->v4l2_ctrls[i].id = ctrl_ids[i];
517 ctx->v4l2_ctrls[i].ptr = payloads[i];
518 ctx->v4l2_ctrls[i].size = payload_sizes[i];
519 }
520 int ret = SYS_IOCTL(fd, VIDIOC_S_EXT_CTRLS, &ext_ctrls);
521 if (ret) {
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800522 return ret;
523 }
524 return 0;
525}
526
henryhsuc7c06f92014-11-24 15:57:08 +0800527static int qbuf_if_pending_buffer_exists_locked(struct encoder_context *ctx,
528 int fd)
529{
530 if (!queue_empty(&ctx->pending_buffers)) {
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800531 int ret;
henryhsuc7c06f92014-11-24 15:57:08 +0800532 struct pending_buffer *element = queue_front(&ctx->pending_buffers);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800533 VLOG_FD(1, "QBUF a buffer (%d) from the pending queue.",
534 element->buffer.index);
henryhsuc7c06f92014-11-24 15:57:08 +0800535 if (rk_vepu_update_parameter(ctx->enc, &element->next_runtime_param)) {
536 VLOG_FD(0, "rk_vepu_update_parameter failed.");
537 return -EIO;
538 }
Tomasz Figaaed2ed92015-02-25 14:05:53 +0900539 memset(&element->next_runtime_param, 0,
540 sizeof(element->next_runtime_param));
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800541 ret = ioctl_qbuf_locked(ctx, fd, &element->buffer);
542 if (ret)
543 return ret;
henryhsuc7c06f92014-11-24 15:57:08 +0800544 queue_pop_front(&ctx->pending_buffers);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800545 }
546 return 0;
547}
548
henryhsuc7c06f92014-11-24 15:57:08 +0800549static int initialize_libvpu(struct encoder_context *ctx, int fd)
550{
551 struct rk_vepu_init_param init_param;
552 memset(&init_param, 0, sizeof(init_param));
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800553
Alpha Linfa5caae2014-11-17 09:48:35 +0800554 /* Get the input format. */
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800555 struct v4l2_format format;
556 memset(&format, 0, sizeof(format));
557 format.type = ctx->output_streamon_type;
558 int ret = SYS_IOCTL(fd, VIDIOC_G_FMT, &format);
Alpha Linfa5caae2014-11-17 09:48:35 +0800559 if (ret)
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800560 return ret;
henryhsuc7c06f92014-11-24 15:57:08 +0800561 init_param.input_format = format.fmt.pix_mp.pixelformat;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800562
Alpha Linfa5caae2014-11-17 09:48:35 +0800563 /* Get the output format. */
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800564 memset(&format, 0, sizeof(format));
565 format.type = ctx->capture_streamon_type;
566 ret = SYS_IOCTL(fd, VIDIOC_G_FMT, &format);
Alpha Linfa5caae2014-11-17 09:48:35 +0800567 if (ret)
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800568 return ret;
henryhsuc7c06f92014-11-24 15:57:08 +0800569 init_param.output_format = format.fmt.pix_mp.pixelformat;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800570
Alpha Linfa5caae2014-11-17 09:48:35 +0800571 /* Get the cropped size. */
572 struct v4l2_crop crop;
573 memset(&crop, 0, sizeof(crop));
574 crop.type = ctx->output_streamon_type;
575 ret = SYS_IOCTL(fd, VIDIOC_G_CROP, &crop);
576 if (ret)
577 return ret;
578 init_param.width = crop.c.width;
579 init_param.height = crop.c.height;
580
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800581 /*
582 * If the encoder library has initialized and parameters have not
583 * changed, skip the initialization.
584 */
henryhsuc7c06f92014-11-24 15:57:08 +0800585 if (ctx->enc && memcmp(&init_param, &ctx->init_param, sizeof(init_param))) {
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800586 rk_vepu_deinit(ctx->enc);
henryhsuc7c06f92014-11-24 15:57:08 +0800587 ctx->enc = NULL;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800588 }
henryhsuc7c06f92014-11-24 15:57:08 +0800589 if (!ctx->enc) {
590 memcpy(&ctx->init_param, &init_param, sizeof(init_param));
591 ctx->enc = rk_vepu_init(&init_param);
592 if (ctx->enc == NULL) {
593 VLOG_FD(0, "Failed to initialize encoder library.");
594 return -EIO;
595 }
596 }
597 if (rk_vepu_update_parameter(ctx->enc, &ctx->runtime_param)) {
598 VLOG_FD(0, "rk_vepu_update_parameter failed.");
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800599 return -EIO;
600 }
henryhsuc7c06f92014-11-24 15:57:08 +0800601 memset(&ctx->runtime_param, 0, sizeof(struct rk_vepu_runtime_param));
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800602 return 0;
603}
604
henryhsuc7c06f92014-11-24 15:57:08 +0800605static void queue_init(struct pending_buffer_queue *queue)
606{
607 memset(queue, 0, sizeof(struct pending_buffer_queue));
608}
609
610static bool queue_empty(struct pending_buffer_queue *queue)
611{
612 return queue->count == 0;
613}
614
615static bool queue_full(struct pending_buffer_queue *queue)
616{
617 return queue->count == PENDING_BUFFER_QUEUE_SIZE;
618}
619
620static int queue_push_back(struct pending_buffer_queue *queue,
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800621 struct v4l2_buffer *buffer)
622{
henryhsuc7c06f92014-11-24 15:57:08 +0800623 if (queue_full(queue))
624 return -ENOMEM;
625 int rear = (queue->front + queue->count) % PENDING_BUFFER_QUEUE_SIZE;
626 queue->count++;
627 struct pending_buffer *entry = &queue->buf_array[rear];
628 memset(entry, 0, sizeof(struct pending_buffer));
629
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800630 memcpy(&entry->buffer, buffer, sizeof(*buffer));
631 if (V4L2_TYPE_IS_MULTIPLANAR(buffer->type)) {
632 memset(entry->planes, 0,
633 sizeof(struct v4l2_plane) * VIDEO_MAX_PLANES);
634 memcpy(entry->planes, buffer->m.planes,
635 sizeof(struct v4l2_plane) * buffer->length);
636 entry->buffer.m.planes = entry->planes;
637 }
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800638 return 0;
639}
640
henryhsuc7c06f92014-11-24 15:57:08 +0800641static void queue_pop_front(struct pending_buffer_queue *queue)
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800642{
henryhsuc7c06f92014-11-24 15:57:08 +0800643 assert(!queue_empty(queue));
644 queue->count--;
645 queue->front = (queue->front + 1) % PENDING_BUFFER_QUEUE_SIZE;
646}
647
648static struct pending_buffer *queue_front(struct pending_buffer_queue *queue)
649{
650 if (queue_empty(queue))
651 return NULL;
652 return &queue->buf_array[queue->front];
653}
654
655static struct pending_buffer *queue_back(struct pending_buffer_queue *queue)
656{
657 if (queue_empty(queue))
658 return NULL;
659 return &queue->buf_array[(queue->front + queue->count - 1) %
660 PENDING_BUFFER_QUEUE_SIZE];
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800661}
662
663static void get_log_level()
664{
665 char *log_level_str = getenv("LIBV4L_PLUGIN_LOG_LEVEL");
666 if (log_level_str != NULL)
667 g_log_level = strtol(log_level_str, NULL, 10);
668}
669
670static const char* v4l_cmd2str(unsigned long int cmd)
671{
672 switch (cmd) {
673 case VIDIOC_QUERYCAP:
674 return "VIDIOC_QUERYCAP";
675 case VIDIOC_TRY_FMT:
676 return "VIDIOC_TRY_FMT";
677 case VIDIOC_S_FMT:
678 return "VIDIOC_S_FMT";
679 case VIDIOC_G_FMT:
680 return "VIDIOC_G_FMT";
681 case VIDIOC_ENUM_FMT:
682 return "VIDIOC_ENUM_FMT";
683 case VIDIOC_S_PARM:
684 return "VIDIOC_S_PARM";
685 case VIDIOC_G_PARM:
686 return "VIDIOC_G_PARM";
687 case VIDIOC_QBUF:
688 return "VIDIOC_QBUF";
689 case VIDIOC_DQBUF:
690 return "VIDIOC_DQBUF";
691 case VIDIOC_PREPARE_BUF:
692 return "VIDIOC_PREPARE_BUF";
693 case VIDIOC_CREATE_BUFS:
694 return "VIDIOC_CREATE_BUFS";
695 case VIDIOC_REQBUFS:
696 return "VIDIOC_REQBUFS";
697 case VIDIOC_STREAMON:
698 return "VIDIOC_STREAMON";
699 case VIDIOC_STREAMOFF:
700 return "VIDIOC_STREAMOFF";
701 case VIDIOC_S_CROP:
702 return "VIDIOC_S_CROP";
703 case VIDIOC_S_CTRL:
704 return "VIDIOC_S_CTRL";
705 case VIDIOC_G_EXT_CTRLS:
706 return "VIDIOC_G_EXT_CTRLS";
707 case VIDIOC_S_EXT_CTRLS:
708 return "VIDIOC_S_EXT_CTRLS";
709 case VIDIOC_QUERYBUF:
710 return "VIDIOC_QUERYBUF";
711 default:
712 return "UNKNOWN";
713 }
henryhsu58be50c2014-10-30 11:49:19 +0800714}
715
716PLUGIN_PUBLIC const struct libv4l_dev_ops libv4l2_plugin = {
717 .init = &plugin_init,
718 .close = &plugin_close,
719 .ioctl = &plugin_ioctl,
720};