blob: 24e9683d7937d43a457b09088516a9fec50ea4fd [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
Heng-Ruey Hsu61eb2ec2016-05-20 18:11:53 +0800344 buffer->config_store = buffer->index + 1;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800345 ret = SYS_IOCTL(fd, VIDIOC_QBUF, buffer);
346 if (ret == 0)
347 ctx->can_qbuf = false;
348 else
349 VLOG(0, "QBUF failed. errno=%d", errno);
350 return ret;
351}
352
353static int ioctl_dqbuf_locked(struct encoder_context *ctx, int fd,
354 struct v4l2_buffer *buffer)
355{
356 struct v4l2_ext_controls ext_ctrls;
357 struct v4l2_ext_control v4l2_ctrl;
358 int ret;
henryhsuc7c06f92014-11-24 15:57:08 +0800359 uint32_t bytesused;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800360
361 if (V4L2_TYPE_IS_OUTPUT(buffer->type)) {
362 return SYS_IOCTL(fd, VIDIOC_DQBUF, buffer);
363 }
364
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800365 ret = SYS_IOCTL(fd, VIDIOC_DQBUF, buffer);
366 if (ret)
367 return ret;
368
Wu-Cheng Lif1e616b2014-11-21 15:11:34 +0800369 assert(!ctx->can_qbuf);
370
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800371 /* Get the encoder configuration and update the library. */
372 memset(ctx->get_param_payload, 0, ctx->get_param_payload_size);
373 memset(&v4l2_ctrl, 0, sizeof(v4l2_ctrl));
374 v4l2_ctrl.id = V4L2_CID_PRIVATE_RK3288_GET_PARAMS;
375 v4l2_ctrl.size = ctx->get_param_payload_size;
Wu-Cheng Liadc8ec82014-11-21 15:51:59 +0800376 v4l2_ctrl.ptr = ctx->get_param_payload;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800377 memset(&ext_ctrls, 0, sizeof(ext_ctrls));
378 /* TODO: change this to config_store after the header is updated. */
henryhsuc7c06f92014-11-24 15:57:08 +0800379 ext_ctrls.ctrl_class = 0;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800380 ext_ctrls.count = 1;
381 ext_ctrls.controls = &v4l2_ctrl;
382 ret = SYS_IOCTL(fd, VIDIOC_G_EXT_CTRLS, &ext_ctrls);
henryhsuc7c06f92014-11-24 15:57:08 +0800383 if (ret)
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800384 return ret;
henryhsuc7c06f92014-11-24 15:57:08 +0800385 bytesused = V4L2_TYPE_IS_MULTIPLANAR(buffer->type) ?
386 buffer->m.planes[0].bytesused : buffer->bytesused;
387
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800388 if (rk_vepu_update_config(ctx->enc, v4l2_ctrl.ptr, v4l2_ctrl.size,
henryhsuc7c06f92014-11-24 15:57:08 +0800389 bytesused)) {
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800390 VLOG_FD(0, "rk_vepu_update_config failed.");
391 return -EIO;
392 }
393 ctx->can_qbuf = true;
henryhsuc7c06f92014-11-24 15:57:08 +0800394 return qbuf_if_pending_buffer_exists_locked(ctx, fd);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800395}
396
henryhsuc7c06f92014-11-24 15:57:08 +0800397static int ioctl_s_ext_ctrls_locked(struct encoder_context *ctx, int fd,
398 struct v4l2_ext_controls *ext_ctrls)
399{
400 size_t i;
401 struct rk_vepu_runtime_param *runtime_param_ptr;
402
403 bool no_pending_buffer = queue_empty(&ctx->pending_buffers);
404 /*
405 * If buffer queue is empty, update parameters directly.
406 * If buffer queue is not empty, save parameters to the last buffer. And
407 * these values will be sent again when the buffer is ready to deliver.
408 */
409 if (!no_pending_buffer) {
410 struct pending_buffer *element = queue_back(&ctx->pending_buffers);
411 runtime_param_ptr = &element->next_runtime_param;
412 } else {
413 runtime_param_ptr = &ctx->runtime_param;
414 }
415
416 /*
417 * Check each extension control to update keyframe and bitrate
418 * parameters.
419 */
420 for (i = 0; i < ext_ctrls->count; i++) {
421 switch (ext_ctrls->controls[i].id) {
Heng-Ruey Hsu61eb2ec2016-05-20 18:11:53 +0800422 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
423 runtime_param_ptr->keyframe_request = true;
henryhsuc7c06f92014-11-24 15:57:08 +0800424 break;
425 case V4L2_CID_MPEG_VIDEO_BITRATE:
426 runtime_param_ptr->bitrate = ext_ctrls->controls[i].value;
427 break;
428 default:
429 break;
430 }
431 }
432
433 if (no_pending_buffer && ctx->enc) {
434 if (rk_vepu_update_parameter(ctx->enc, runtime_param_ptr)) {
435 VLOG_FD(0, "rk_vepu_update_parameter failed.");
436 return -EIO;
437 }
438 memset(runtime_param_ptr, 0, sizeof(struct rk_vepu_runtime_param));
439 }
440 /* Driver should ignore keyframe and bitrate controls */
441 return SYS_IOCTL(fd, VIDIOC_S_EXT_CTRLS, ext_ctrls);
442}
443
444static int ioctl_s_parm_locked(struct encoder_context *ctx, int fd,
445 struct v4l2_streamparm *parms)
446{
447 if (V4L2_TYPE_IS_OUTPUT(parms->type)
448 && parms->parm.output.timeperframe.denominator) {
449 struct rk_vepu_runtime_param *runtime_param_ptr;
450 bool no_pending_buffer = queue_empty(&ctx->pending_buffers);
451 struct pending_buffer *element = queue_back(&ctx->pending_buffers);
452
453 runtime_param_ptr = no_pending_buffer ? &ctx->runtime_param :
454 &element->next_runtime_param;
455 runtime_param_ptr->framerate_numer =
456 parms->parm.output.timeperframe.denominator;
457 runtime_param_ptr->framerate_denom =
458 parms->parm.output.timeperframe.numerator;
459
460 if (!no_pending_buffer || !ctx->enc)
461 return 0;
462 if (rk_vepu_update_parameter(ctx->enc, runtime_param_ptr)) {
463 VLOG_FD(0, "rk_vepu_update_parameter failed.");
464 return -EIO;
465 }
466 memset(runtime_param_ptr, 0, sizeof(struct rk_vepu_runtime_param));
467 return 0;
468 }
469 return SYS_IOCTL(fd, VIDIOC_S_PARM, parms);
470}
471
472static int ioctl_reqbufs_locked(struct encoder_context *ctx, int fd,
473 struct v4l2_requestbuffers *reqbufs)
474{
475 int ret = SYS_IOCTL(fd, VIDIOC_REQBUFS, reqbufs);
476 if (ret)
477 return ret;
478 queue_init(&ctx->pending_buffers);
479 return 0;
480}
481
Wu-Cheng Li11ae3aa2014-12-18 17:11:48 +0800482bool is_rockchip_encoder(int fd) {
483 struct v4l2_capability cap;
484 memset(&cap, 0, sizeof(cap));
485 int ret = SYS_IOCTL(fd, VIDIOC_QUERYCAP, &cap);
486 if (ret)
487 return false;
Alpha Linfa5caae2014-11-17 09:48:35 +0800488 return strcmp(RK3288_VPU_NAME, (const char *)cap.driver) == 0;
Wu-Cheng Li11ae3aa2014-12-18 17:11:48 +0800489}
490
henryhsuc7c06f92014-11-24 15:57:08 +0800491int set_encoder_config_locked(struct encoder_context *ctx, int fd,
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800492 uint32_t buffer_index, size_t num_ctrls, uint32_t ctrl_ids[],
henryhsuc7c06f92014-11-24 15:57:08 +0800493 void **payloads, uint32_t payload_sizes[])
494{
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800495 size_t i;
496 struct v4l2_ext_controls ext_ctrls;
497
498 if (num_ctrls <= 0)
499 return 0;
500
501 assert(num_ctrls <= MAX_NUM_GET_CONFIG_CTRLS);
henryhsuc7c06f92014-11-24 15:57:08 +0800502 if (num_ctrls > MAX_NUM_GET_CONFIG_CTRLS) {
503 VLOG_FD(0, "The number of controls exceeds limit.");
504 return -EIO;
505 }
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800506 memset(&ext_ctrls, 0, sizeof(ext_ctrls));
507 /* TODO: change this to config_store after the header is updated. */
508 ext_ctrls.ctrl_class = buffer_index + 1;
509 ext_ctrls.count = num_ctrls;
510 ext_ctrls.controls = ctx->v4l2_ctrls;
511 memset(ctx->v4l2_ctrls, 0, sizeof(ctx->v4l2_ctrls));
512 for (i = 0; i < num_ctrls; ++i) {
513 ctx->v4l2_ctrls[i].id = ctrl_ids[i];
514 ctx->v4l2_ctrls[i].ptr = payloads[i];
515 ctx->v4l2_ctrls[i].size = payload_sizes[i];
516 }
517 int ret = SYS_IOCTL(fd, VIDIOC_S_EXT_CTRLS, &ext_ctrls);
518 if (ret) {
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800519 return ret;
520 }
521 return 0;
522}
523
henryhsuc7c06f92014-11-24 15:57:08 +0800524static int qbuf_if_pending_buffer_exists_locked(struct encoder_context *ctx,
525 int fd)
526{
527 if (!queue_empty(&ctx->pending_buffers)) {
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800528 int ret;
henryhsuc7c06f92014-11-24 15:57:08 +0800529 struct pending_buffer *element = queue_front(&ctx->pending_buffers);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800530 VLOG_FD(1, "QBUF a buffer (%d) from the pending queue.",
531 element->buffer.index);
henryhsuc7c06f92014-11-24 15:57:08 +0800532 if (rk_vepu_update_parameter(ctx->enc, &element->next_runtime_param)) {
533 VLOG_FD(0, "rk_vepu_update_parameter failed.");
534 return -EIO;
535 }
Tomasz Figaaed2ed92015-02-25 14:05:53 +0900536 memset(&element->next_runtime_param, 0,
537 sizeof(element->next_runtime_param));
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800538 ret = ioctl_qbuf_locked(ctx, fd, &element->buffer);
539 if (ret)
540 return ret;
henryhsuc7c06f92014-11-24 15:57:08 +0800541 queue_pop_front(&ctx->pending_buffers);
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800542 }
543 return 0;
544}
545
henryhsuc7c06f92014-11-24 15:57:08 +0800546static int initialize_libvpu(struct encoder_context *ctx, int fd)
547{
548 struct rk_vepu_init_param init_param;
549 memset(&init_param, 0, sizeof(init_param));
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800550
Alpha Linfa5caae2014-11-17 09:48:35 +0800551 /* Get the input format. */
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800552 struct v4l2_format format;
553 memset(&format, 0, sizeof(format));
554 format.type = ctx->output_streamon_type;
555 int ret = SYS_IOCTL(fd, VIDIOC_G_FMT, &format);
Alpha Linfa5caae2014-11-17 09:48:35 +0800556 if (ret)
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800557 return ret;
henryhsuc7c06f92014-11-24 15:57:08 +0800558 init_param.input_format = format.fmt.pix_mp.pixelformat;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800559
Alpha Linfa5caae2014-11-17 09:48:35 +0800560 /* Get the output format. */
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800561 memset(&format, 0, sizeof(format));
562 format.type = ctx->capture_streamon_type;
563 ret = SYS_IOCTL(fd, VIDIOC_G_FMT, &format);
Alpha Linfa5caae2014-11-17 09:48:35 +0800564 if (ret)
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800565 return ret;
henryhsuc7c06f92014-11-24 15:57:08 +0800566 init_param.output_format = format.fmt.pix_mp.pixelformat;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800567
Alpha Linfa5caae2014-11-17 09:48:35 +0800568 /* Get the cropped size. */
569 struct v4l2_crop crop;
570 memset(&crop, 0, sizeof(crop));
571 crop.type = ctx->output_streamon_type;
572 ret = SYS_IOCTL(fd, VIDIOC_G_CROP, &crop);
573 if (ret)
574 return ret;
575 init_param.width = crop.c.width;
576 init_param.height = crop.c.height;
577
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800578 /*
579 * If the encoder library has initialized and parameters have not
580 * changed, skip the initialization.
581 */
henryhsuc7c06f92014-11-24 15:57:08 +0800582 if (ctx->enc && memcmp(&init_param, &ctx->init_param, sizeof(init_param))) {
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800583 rk_vepu_deinit(ctx->enc);
henryhsuc7c06f92014-11-24 15:57:08 +0800584 ctx->enc = NULL;
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800585 }
henryhsuc7c06f92014-11-24 15:57:08 +0800586 if (!ctx->enc) {
587 memcpy(&ctx->init_param, &init_param, sizeof(init_param));
588 ctx->enc = rk_vepu_init(&init_param);
589 if (ctx->enc == NULL) {
590 VLOG_FD(0, "Failed to initialize encoder library.");
591 return -EIO;
592 }
593 }
594 if (rk_vepu_update_parameter(ctx->enc, &ctx->runtime_param)) {
595 VLOG_FD(0, "rk_vepu_update_parameter failed.");
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800596 return -EIO;
597 }
henryhsuc7c06f92014-11-24 15:57:08 +0800598 memset(&ctx->runtime_param, 0, sizeof(struct rk_vepu_runtime_param));
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800599 return 0;
600}
601
henryhsuc7c06f92014-11-24 15:57:08 +0800602static void queue_init(struct pending_buffer_queue *queue)
603{
604 memset(queue, 0, sizeof(struct pending_buffer_queue));
605}
606
607static bool queue_empty(struct pending_buffer_queue *queue)
608{
609 return queue->count == 0;
610}
611
612static bool queue_full(struct pending_buffer_queue *queue)
613{
614 return queue->count == PENDING_BUFFER_QUEUE_SIZE;
615}
616
617static int queue_push_back(struct pending_buffer_queue *queue,
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800618 struct v4l2_buffer *buffer)
619{
henryhsuc7c06f92014-11-24 15:57:08 +0800620 if (queue_full(queue))
621 return -ENOMEM;
622 int rear = (queue->front + queue->count) % PENDING_BUFFER_QUEUE_SIZE;
623 queue->count++;
624 struct pending_buffer *entry = &queue->buf_array[rear];
625 memset(entry, 0, sizeof(struct pending_buffer));
626
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800627 memcpy(&entry->buffer, buffer, sizeof(*buffer));
628 if (V4L2_TYPE_IS_MULTIPLANAR(buffer->type)) {
629 memset(entry->planes, 0,
630 sizeof(struct v4l2_plane) * VIDEO_MAX_PLANES);
631 memcpy(entry->planes, buffer->m.planes,
632 sizeof(struct v4l2_plane) * buffer->length);
633 entry->buffer.m.planes = entry->planes;
634 }
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800635 return 0;
636}
637
henryhsuc7c06f92014-11-24 15:57:08 +0800638static void queue_pop_front(struct pending_buffer_queue *queue)
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800639{
henryhsuc7c06f92014-11-24 15:57:08 +0800640 assert(!queue_empty(queue));
641 queue->count--;
642 queue->front = (queue->front + 1) % PENDING_BUFFER_QUEUE_SIZE;
643}
644
645static struct pending_buffer *queue_front(struct pending_buffer_queue *queue)
646{
647 if (queue_empty(queue))
648 return NULL;
649 return &queue->buf_array[queue->front];
650}
651
652static struct pending_buffer *queue_back(struct pending_buffer_queue *queue)
653{
654 if (queue_empty(queue))
655 return NULL;
656 return &queue->buf_array[(queue->front + queue->count - 1) %
657 PENDING_BUFFER_QUEUE_SIZE];
Wu-Cheng Lice8947e2014-11-13 17:34:16 +0800658}
659
660static void get_log_level()
661{
662 char *log_level_str = getenv("LIBV4L_PLUGIN_LOG_LEVEL");
663 if (log_level_str != NULL)
664 g_log_level = strtol(log_level_str, NULL, 10);
665}
666
667static const char* v4l_cmd2str(unsigned long int cmd)
668{
669 switch (cmd) {
670 case VIDIOC_QUERYCAP:
671 return "VIDIOC_QUERYCAP";
672 case VIDIOC_TRY_FMT:
673 return "VIDIOC_TRY_FMT";
674 case VIDIOC_S_FMT:
675 return "VIDIOC_S_FMT";
676 case VIDIOC_G_FMT:
677 return "VIDIOC_G_FMT";
678 case VIDIOC_ENUM_FMT:
679 return "VIDIOC_ENUM_FMT";
680 case VIDIOC_S_PARM:
681 return "VIDIOC_S_PARM";
682 case VIDIOC_G_PARM:
683 return "VIDIOC_G_PARM";
684 case VIDIOC_QBUF:
685 return "VIDIOC_QBUF";
686 case VIDIOC_DQBUF:
687 return "VIDIOC_DQBUF";
688 case VIDIOC_PREPARE_BUF:
689 return "VIDIOC_PREPARE_BUF";
690 case VIDIOC_CREATE_BUFS:
691 return "VIDIOC_CREATE_BUFS";
692 case VIDIOC_REQBUFS:
693 return "VIDIOC_REQBUFS";
694 case VIDIOC_STREAMON:
695 return "VIDIOC_STREAMON";
696 case VIDIOC_STREAMOFF:
697 return "VIDIOC_STREAMOFF";
698 case VIDIOC_S_CROP:
699 return "VIDIOC_S_CROP";
700 case VIDIOC_S_CTRL:
701 return "VIDIOC_S_CTRL";
702 case VIDIOC_G_EXT_CTRLS:
703 return "VIDIOC_G_EXT_CTRLS";
704 case VIDIOC_S_EXT_CTRLS:
705 return "VIDIOC_S_EXT_CTRLS";
706 case VIDIOC_QUERYBUF:
707 return "VIDIOC_QUERYBUF";
708 default:
709 return "UNKNOWN";
710 }
henryhsu58be50c2014-10-30 11:49:19 +0800711}
712
713PLUGIN_PUBLIC const struct libv4l_dev_ops libv4l2_plugin = {
714 .init = &plugin_init,
715 .close = &plugin_close,
716 .ioctl = &plugin_ioctl,
717};