|
| 1 | +/**************************************************************************** |
| 2 | + * apps/system/nxcodec/nxcodec.c |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | + * contributor license agreements. See the NOTICE file distributed with |
| 6 | + * this work for additional information regarding copyright ownership. The |
| 7 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance with the |
| 9 | + * License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | + * License for the specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + ****************************************************************************/ |
| 20 | + |
| 21 | +/**************************************************************************** |
| 22 | + * Included Files |
| 23 | + ****************************************************************************/ |
| 24 | + |
| 25 | +#include <sys/ioctl.h> |
| 26 | +#include <stdio.h> |
| 27 | +#include <stdlib.h> |
| 28 | +#include <unistd.h> |
| 29 | +#include <fcntl.h> |
| 30 | +#include <errno.h> |
| 31 | + |
| 32 | +#include "nxcodec.h" |
| 33 | + |
| 34 | +/**************************************************************************** |
| 35 | + * Private Functions |
| 36 | + ****************************************************************************/ |
| 37 | + |
| 38 | +static inline bool nxcodec_splane_video(FAR struct v4l2_capability *cap) |
| 39 | +{ |
| 40 | + return (cap->capabilities & V4L2_CAP_VIDEO_M2M) || |
| 41 | + ((cap->capabilities & V4L2_CAP_STREAMING) && |
| 42 | + (cap->capabilities & (V4L2_CAP_VIDEO_OUTPUT | |
| 43 | + V4L2_CAP_VIDEO_CAPTURE))); |
| 44 | +} |
| 45 | + |
| 46 | +static inline bool nxcodec_mplane_video(FAR struct v4l2_capability *cap) |
| 47 | +{ |
| 48 | + return (cap->capabilities & V4L2_CAP_VIDEO_M2M_MPLANE) || |
| 49 | + ((cap->capabilities & V4L2_CAP_STREAMING) && |
| 50 | + (cap->capabilities & (V4L2_CAP_VIDEO_OUTPUT_MPLANE | |
| 51 | + V4L2_CAP_VIDEO_CAPTURE_MPLANE))); |
| 52 | +} |
| 53 | + |
| 54 | +static int nxcodec_prepare_contexts(FAR nxcodec_t *codec) |
| 55 | +{ |
| 56 | + struct v4l2_capability cap; |
| 57 | + int ret; |
| 58 | + |
| 59 | + memset(&cap, 0, sizeof(cap)); |
| 60 | + ret = ioctl(codec->fd, VIDIOC_QUERYCAP, &cap); |
| 61 | + if (ret < 0) |
| 62 | + { |
| 63 | + return -errno; |
| 64 | + } |
| 65 | + |
| 66 | + if (nxcodec_mplane_video(&cap)) |
| 67 | + { |
| 68 | + codec->capture.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
| 69 | + codec->output.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; |
| 70 | + return 0; |
| 71 | + } |
| 72 | + |
| 73 | + if (nxcodec_splane_video(&cap)) |
| 74 | + { |
| 75 | + codec->capture.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 76 | + codec->output.type = V4L2_BUF_TYPE_VIDEO_OUTPUT; |
| 77 | + return 0; |
| 78 | + } |
| 79 | + |
| 80 | + return -EINVAL; |
| 81 | +} |
| 82 | + |
| 83 | +/**************************************************************************** |
| 84 | + * Public Functions |
| 85 | + ****************************************************************************/ |
| 86 | + |
| 87 | +int nxcodec_init(FAR nxcodec_t *codec) |
| 88 | +{ |
| 89 | + int ret; |
| 90 | + |
| 91 | + codec->fd = open(codec->devname, O_RDWR | O_NONBLOCK); |
| 92 | + if (codec->fd < 0) |
| 93 | + { |
| 94 | + return -errno; |
| 95 | + } |
| 96 | + |
| 97 | + ret = nxcodec_prepare_contexts(codec); |
| 98 | + if (ret < 0) |
| 99 | + { |
| 100 | + goto err0; |
| 101 | + } |
| 102 | + |
| 103 | + ret = nxcodec_context_get_format(&codec->output); |
| 104 | + if (ret < 0) |
| 105 | + { |
| 106 | + printf("v4l2 output format not supported\n"); |
| 107 | + goto err0; |
| 108 | + } |
| 109 | + |
| 110 | + ret = nxcodec_context_get_format(&codec->capture); |
| 111 | + if (ret < 0) |
| 112 | + { |
| 113 | + printf("v4l2 capture format not supported\n"); |
| 114 | + goto err0; |
| 115 | + } |
| 116 | + |
| 117 | + if (codec->output.fdesc.pixelformat != |
| 118 | + codec->output.format.fmt.pix.pixelformat) |
| 119 | + { |
| 120 | + ret = -EINVAL; |
| 121 | + goto err0; |
| 122 | + } |
| 123 | + |
| 124 | + codec->output.format.type = codec->output.type; |
| 125 | + |
| 126 | + ret = nxcodec_context_set_format(&codec->output); |
| 127 | + if (ret < 0) |
| 128 | + { |
| 129 | + printf("can't set v4l2 output format\n"); |
| 130 | + goto err0; |
| 131 | + } |
| 132 | + |
| 133 | + codec->output.fd = open(codec->output.filename, O_RDONLY); |
| 134 | + if (codec->output.fd < 0) |
| 135 | + { |
| 136 | + printf("Failed to open input file %s \n", codec->output.filename); |
| 137 | + ret = -errno; |
| 138 | + goto err0; |
| 139 | + } |
| 140 | + |
| 141 | + if (codec->capture.fdesc.pixelformat != |
| 142 | + codec->capture.format.fmt.pix.pixelformat) |
| 143 | + { |
| 144 | + ret = -EINVAL; |
| 145 | + goto err1; |
| 146 | + } |
| 147 | + |
| 148 | + codec->capture.format.type = codec->capture.type; |
| 149 | + |
| 150 | + ret = nxcodec_context_set_format(&codec->capture); |
| 151 | + if (ret < 0) |
| 152 | + { |
| 153 | + printf("can't to set v4l2 capture format\n"); |
| 154 | + goto err1; |
| 155 | + } |
| 156 | + |
| 157 | + codec->capture.fd = open(codec->capture.filename, |
| 158 | + O_WRONLY | O_CREAT, 0644); |
| 159 | + if (codec->capture.fd < 0) |
| 160 | + { |
| 161 | + printf("Failed to open input file %s \n", codec->capture.filename); |
| 162 | + ret = -errno; |
| 163 | + goto err1; |
| 164 | + } |
| 165 | + |
| 166 | + return 0; |
| 167 | + |
| 168 | +err1: |
| 169 | + close(codec->output.fd); |
| 170 | +err0: |
| 171 | + close(codec->fd); |
| 172 | + return ret; |
| 173 | +} |
| 174 | + |
| 175 | +int nxcodec_start(FAR nxcodec_t *codec) |
| 176 | +{ |
| 177 | + int ret; |
| 178 | + |
| 179 | + ret = nxcodec_context_init(&codec->output); |
| 180 | + if (ret < 0) |
| 181 | + { |
| 182 | + printf("can't request output buffers\n"); |
| 183 | + return ret; |
| 184 | + } |
| 185 | + |
| 186 | + ret = nxcodec_context_set_status(&codec->output, VIDIOC_STREAMON); |
| 187 | + if (ret < 0) |
| 188 | + { |
| 189 | + printf("set output VIDIOC_STREAMON failed\n"); |
| 190 | + goto err0; |
| 191 | + } |
| 192 | + |
| 193 | + ret = nxcodec_context_init(&codec->capture); |
| 194 | + if (ret < 0) |
| 195 | + { |
| 196 | + printf("can't request capture buffers\n"); |
| 197 | + goto err0; |
| 198 | + } |
| 199 | + |
| 200 | + ret = nxcodec_context_set_status(&codec->capture, VIDIOC_STREAMON); |
| 201 | + if (ret < 0) |
| 202 | + { |
| 203 | + printf("set capture VIDIOC_STREAMON failed\n"); |
| 204 | + goto err1; |
| 205 | + } |
| 206 | + |
| 207 | + ret = nxcodec_context_enqueue_frame(&codec->output); |
| 208 | + if (ret < 0 && ret != -EAGAIN) |
| 209 | + { |
| 210 | + goto err1; |
| 211 | + } |
| 212 | + |
| 213 | + return 0; |
| 214 | + |
| 215 | +err1: |
| 216 | + nxcodec_context_uninit(&codec->capture); |
| 217 | +err0: |
| 218 | + nxcodec_context_uninit(&codec->output); |
| 219 | + return ret; |
| 220 | +} |
| 221 | + |
| 222 | +int nxcodec_stop(FAR nxcodec_t *codec) |
| 223 | +{ |
| 224 | + int ret; |
| 225 | + |
| 226 | + if (!codec) |
| 227 | + { |
| 228 | + return 0; |
| 229 | + } |
| 230 | + |
| 231 | + nxcodec_context_uninit(&codec->output); |
| 232 | + |
| 233 | + ret = nxcodec_context_set_status(&codec->output, VIDIOC_STREAMOFF); |
| 234 | + if (ret < 0) |
| 235 | + { |
| 236 | + printf("set output VIDIOC_STREAMOFF failed\n"); |
| 237 | + return ret; |
| 238 | + } |
| 239 | + |
| 240 | + nxcodec_context_uninit(&codec->capture); |
| 241 | + |
| 242 | + ret = nxcodec_context_set_status(&codec->capture, VIDIOC_STREAMOFF); |
| 243 | + if (ret < 0) |
| 244 | + { |
| 245 | + printf("set capture VIDIOC_STREAMOFF failed\n"); |
| 246 | + return ret; |
| 247 | + } |
| 248 | + |
| 249 | + return 0; |
| 250 | +} |
| 251 | + |
| 252 | +int nxcodec_uninit(FAR nxcodec_t *codec) |
| 253 | +{ |
| 254 | + close(codec->capture.fd); |
| 255 | + close(codec->output.fd); |
| 256 | + close(codec->fd); |
| 257 | + |
| 258 | + return 0; |
| 259 | +} |
0 commit comments