| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * V4L2 buffer helper functions. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2017 Alexis Ballier <aballier@gentoo.org> | ||
| 5 | * Copyright (C) 2017 Jorge Ramirez <jorge.ramirez-ortiz@linaro.org> | ||
| 6 | * | ||
| 7 | * This file is part of FFmpeg. | ||
| 8 | * | ||
| 9 | * FFmpeg is free software; you can redistribute it and/or | ||
| 10 | * modify it under the terms of the GNU Lesser General Public | ||
| 11 | * License as published by the Free Software Foundation; either | ||
| 12 | * version 2.1 of the License, or (at your option) any later version. | ||
| 13 | * | ||
| 14 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 17 | * Lesser General Public License for more details. | ||
| 18 | * | ||
| 19 | * You should have received a copy of the GNU Lesser General Public | ||
| 20 | * License along with FFmpeg; if not, write to the Free Software | ||
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 22 | */ | ||
| 23 | |||
| 24 | #include <linux/videodev2.h> | ||
| 25 | #include <sys/ioctl.h> | ||
| 26 | #include <sys/mman.h> | ||
| 27 | #include <unistd.h> | ||
| 28 | #include <fcntl.h> | ||
| 29 | #include <poll.h> | ||
| 30 | #include "libavcodec/avcodec.h" | ||
| 31 | #include "libavutil/pixdesc.h" | ||
| 32 | #include "libavutil/refstruct.h" | ||
| 33 | #include "v4l2_context.h" | ||
| 34 | #include "v4l2_buffers.h" | ||
| 35 | #include "v4l2_m2m.h" | ||
| 36 | |||
| 37 | #ifndef USEC_PER_SEC | ||
| 38 | #define USEC_PER_SEC 1000000 | ||
| 39 | #endif | ||
| 40 | |||
| 41 | static AVRational v4l2_timebase = { 1, USEC_PER_SEC }; | ||
| 42 | |||
| 43 | ✗ | static inline V4L2m2mContext *buf_to_m2mctx(V4L2Buffer *buf) | |
| 44 | { | ||
| 45 | ✗ | return V4L2_TYPE_IS_OUTPUT(buf->context->type) ? | |
| 46 | ✗ | container_of(buf->context, V4L2m2mContext, output) : | |
| 47 | ✗ | container_of(buf->context, V4L2m2mContext, capture); | |
| 48 | } | ||
| 49 | |||
| 50 | ✗ | static inline AVCodecContext *logger(V4L2Buffer *buf) | |
| 51 | { | ||
| 52 | ✗ | return buf_to_m2mctx(buf)->avctx; | |
| 53 | } | ||
| 54 | |||
| 55 | ✗ | static inline AVRational v4l2_get_timebase(V4L2Buffer *avbuf) | |
| 56 | { | ||
| 57 | ✗ | V4L2m2mContext *s = buf_to_m2mctx(avbuf); | |
| 58 | |||
| 59 | ✗ | if (s->avctx->pkt_timebase.num) | |
| 60 | ✗ | return s->avctx->pkt_timebase; | |
| 61 | ✗ | return s->avctx->time_base; | |
| 62 | } | ||
| 63 | |||
| 64 | ✗ | static inline void v4l2_set_pts(V4L2Buffer *out, int64_t pts) | |
| 65 | { | ||
| 66 | int64_t v4l2_pts; | ||
| 67 | |||
| 68 | ✗ | if (pts == AV_NOPTS_VALUE) | |
| 69 | ✗ | pts = 0; | |
| 70 | |||
| 71 | /* convert pts to v4l2 timebase */ | ||
| 72 | ✗ | v4l2_pts = av_rescale_q(pts, v4l2_get_timebase(out), v4l2_timebase); | |
| 73 | ✗ | out->buf.timestamp.tv_usec = v4l2_pts % USEC_PER_SEC; | |
| 74 | ✗ | out->buf.timestamp.tv_sec = v4l2_pts / USEC_PER_SEC; | |
| 75 | ✗ | } | |
| 76 | |||
| 77 | ✗ | static inline int64_t v4l2_get_pts(V4L2Buffer *avbuf) | |
| 78 | { | ||
| 79 | int64_t v4l2_pts; | ||
| 80 | |||
| 81 | /* convert pts back to encoder timebase */ | ||
| 82 | ✗ | v4l2_pts = (int64_t)avbuf->buf.timestamp.tv_sec * USEC_PER_SEC + | |
| 83 | ✗ | avbuf->buf.timestamp.tv_usec; | |
| 84 | |||
| 85 | ✗ | return av_rescale_q(v4l2_pts, v4l2_timebase, v4l2_get_timebase(avbuf)); | |
| 86 | } | ||
| 87 | |||
| 88 | ✗ | static enum AVColorPrimaries v4l2_get_color_primaries(V4L2Buffer *buf) | |
| 89 | { | ||
| 90 | enum v4l2_ycbcr_encoding ycbcr; | ||
| 91 | enum v4l2_colorspace cs; | ||
| 92 | |||
| 93 | ✗ | cs = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ? | |
| 94 | ✗ | buf->context->format.fmt.pix_mp.colorspace : | |
| 95 | ✗ | buf->context->format.fmt.pix.colorspace; | |
| 96 | |||
| 97 | ✗ | ycbcr = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ? | |
| 98 | ✗ | buf->context->format.fmt.pix_mp.ycbcr_enc: | |
| 99 | ✗ | buf->context->format.fmt.pix.ycbcr_enc; | |
| 100 | |||
| 101 | ✗ | switch(ycbcr) { | |
| 102 | ✗ | case V4L2_YCBCR_ENC_XV709: | |
| 103 | ✗ | case V4L2_YCBCR_ENC_709: return AVCOL_PRI_BT709; | |
| 104 | ✗ | case V4L2_YCBCR_ENC_XV601: | |
| 105 | ✗ | case V4L2_YCBCR_ENC_601:return AVCOL_PRI_BT470M; | |
| 106 | ✗ | default: | |
| 107 | ✗ | break; | |
| 108 | } | ||
| 109 | |||
| 110 | ✗ | switch(cs) { | |
| 111 | ✗ | case V4L2_COLORSPACE_470_SYSTEM_BG: return AVCOL_PRI_BT470BG; | |
| 112 | ✗ | case V4L2_COLORSPACE_SMPTE170M: return AVCOL_PRI_SMPTE170M; | |
| 113 | ✗ | case V4L2_COLORSPACE_SMPTE240M: return AVCOL_PRI_SMPTE240M; | |
| 114 | ✗ | case V4L2_COLORSPACE_BT2020: return AVCOL_PRI_BT2020; | |
| 115 | ✗ | default: | |
| 116 | ✗ | break; | |
| 117 | } | ||
| 118 | |||
| 119 | ✗ | return AVCOL_PRI_UNSPECIFIED; | |
| 120 | } | ||
| 121 | |||
| 122 | ✗ | static enum AVColorRange v4l2_get_color_range(V4L2Buffer *buf) | |
| 123 | { | ||
| 124 | enum v4l2_quantization qt; | ||
| 125 | |||
| 126 | ✗ | qt = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ? | |
| 127 | ✗ | buf->context->format.fmt.pix_mp.quantization : | |
| 128 | ✗ | buf->context->format.fmt.pix.quantization; | |
| 129 | |||
| 130 | ✗ | switch (qt) { | |
| 131 | ✗ | case V4L2_QUANTIZATION_LIM_RANGE: return AVCOL_RANGE_MPEG; | |
| 132 | ✗ | case V4L2_QUANTIZATION_FULL_RANGE: return AVCOL_RANGE_JPEG; | |
| 133 | ✗ | default: | |
| 134 | ✗ | break; | |
| 135 | } | ||
| 136 | |||
| 137 | ✗ | return AVCOL_RANGE_UNSPECIFIED; | |
| 138 | } | ||
| 139 | |||
| 140 | ✗ | static enum AVColorSpace v4l2_get_color_space(V4L2Buffer *buf) | |
| 141 | { | ||
| 142 | enum v4l2_ycbcr_encoding ycbcr; | ||
| 143 | enum v4l2_colorspace cs; | ||
| 144 | |||
| 145 | ✗ | cs = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ? | |
| 146 | ✗ | buf->context->format.fmt.pix_mp.colorspace : | |
| 147 | ✗ | buf->context->format.fmt.pix.colorspace; | |
| 148 | |||
| 149 | ✗ | ycbcr = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ? | |
| 150 | ✗ | buf->context->format.fmt.pix_mp.ycbcr_enc: | |
| 151 | ✗ | buf->context->format.fmt.pix.ycbcr_enc; | |
| 152 | |||
| 153 | ✗ | switch(cs) { | |
| 154 | ✗ | case V4L2_COLORSPACE_SRGB: return AVCOL_SPC_RGB; | |
| 155 | ✗ | case V4L2_COLORSPACE_REC709: return AVCOL_SPC_BT709; | |
| 156 | ✗ | case V4L2_COLORSPACE_470_SYSTEM_M: return AVCOL_SPC_FCC; | |
| 157 | ✗ | case V4L2_COLORSPACE_470_SYSTEM_BG: return AVCOL_SPC_BT470BG; | |
| 158 | ✗ | case V4L2_COLORSPACE_SMPTE170M: return AVCOL_SPC_SMPTE170M; | |
| 159 | ✗ | case V4L2_COLORSPACE_SMPTE240M: return AVCOL_SPC_SMPTE240M; | |
| 160 | ✗ | case V4L2_COLORSPACE_BT2020: | |
| 161 | ✗ | if (ycbcr == V4L2_YCBCR_ENC_BT2020_CONST_LUM) | |
| 162 | ✗ | return AVCOL_SPC_BT2020_CL; | |
| 163 | else | ||
| 164 | ✗ | return AVCOL_SPC_BT2020_NCL; | |
| 165 | ✗ | default: | |
| 166 | ✗ | break; | |
| 167 | } | ||
| 168 | |||
| 169 | ✗ | return AVCOL_SPC_UNSPECIFIED; | |
| 170 | } | ||
| 171 | |||
| 172 | ✗ | static enum AVColorTransferCharacteristic v4l2_get_color_trc(V4L2Buffer *buf) | |
| 173 | { | ||
| 174 | enum v4l2_ycbcr_encoding ycbcr; | ||
| 175 | enum v4l2_xfer_func xfer; | ||
| 176 | enum v4l2_colorspace cs; | ||
| 177 | |||
| 178 | ✗ | cs = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ? | |
| 179 | ✗ | buf->context->format.fmt.pix_mp.colorspace : | |
| 180 | ✗ | buf->context->format.fmt.pix.colorspace; | |
| 181 | |||
| 182 | ✗ | ycbcr = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ? | |
| 183 | ✗ | buf->context->format.fmt.pix_mp.ycbcr_enc: | |
| 184 | ✗ | buf->context->format.fmt.pix.ycbcr_enc; | |
| 185 | |||
| 186 | ✗ | xfer = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ? | |
| 187 | ✗ | buf->context->format.fmt.pix_mp.xfer_func: | |
| 188 | ✗ | buf->context->format.fmt.pix.xfer_func; | |
| 189 | |||
| 190 | ✗ | switch (xfer) { | |
| 191 | ✗ | case V4L2_XFER_FUNC_709: return AVCOL_TRC_BT709; | |
| 192 | ✗ | case V4L2_XFER_FUNC_SRGB: return AVCOL_TRC_IEC61966_2_1; | |
| 193 | ✗ | default: | |
| 194 | ✗ | break; | |
| 195 | } | ||
| 196 | |||
| 197 | ✗ | switch (cs) { | |
| 198 | ✗ | case V4L2_COLORSPACE_470_SYSTEM_M: return AVCOL_TRC_GAMMA22; | |
| 199 | ✗ | case V4L2_COLORSPACE_470_SYSTEM_BG: return AVCOL_TRC_GAMMA28; | |
| 200 | ✗ | case V4L2_COLORSPACE_SMPTE170M: return AVCOL_TRC_SMPTE170M; | |
| 201 | ✗ | case V4L2_COLORSPACE_SMPTE240M: return AVCOL_TRC_SMPTE240M; | |
| 202 | ✗ | default: | |
| 203 | ✗ | break; | |
| 204 | } | ||
| 205 | |||
| 206 | ✗ | switch (ycbcr) { | |
| 207 | ✗ | case V4L2_YCBCR_ENC_XV709: | |
| 208 | ✗ | case V4L2_YCBCR_ENC_XV601: return AVCOL_TRC_BT1361_ECG; | |
| 209 | ✗ | default: | |
| 210 | ✗ | break; | |
| 211 | } | ||
| 212 | |||
| 213 | ✗ | return AVCOL_TRC_UNSPECIFIED; | |
| 214 | } | ||
| 215 | |||
| 216 | ✗ | static void v4l2_get_interlacing(AVFrame *frame, V4L2Buffer *buf) | |
| 217 | { | ||
| 218 | ✗ | enum v4l2_field field = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ? | |
| 219 | ✗ | buf->context->format.fmt.pix_mp.field : | |
| 220 | ✗ | buf->context->format.fmt.pix.field; | |
| 221 | |||
| 222 | ✗ | switch (field) { | |
| 223 | ✗ | case V4L2_FIELD_INTERLACED: | |
| 224 | case V4L2_FIELD_INTERLACED_TB: | ||
| 225 | ✗ | frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST; | |
| 226 | /* fallthrough */ | ||
| 227 | ✗ | case V4L2_FIELD_INTERLACED_BT: | |
| 228 | ✗ | frame->flags |= AV_FRAME_FLAG_INTERLACED; | |
| 229 | ✗ | break; | |
| 230 | } | ||
| 231 | ✗ | } | |
| 232 | |||
| 233 | ✗ | static void v4l2_free_buffer(void *opaque, uint8_t *unused) | |
| 234 | { | ||
| 235 | ✗ | V4L2Buffer* avbuf = opaque; | |
| 236 | ✗ | V4L2m2mContext *s = buf_to_m2mctx(avbuf); | |
| 237 | |||
| 238 | ✗ | if (atomic_fetch_sub(&avbuf->context_refcount, 1) == 1) { | |
| 239 | ✗ | atomic_fetch_sub_explicit(&s->refcount, 1, memory_order_acq_rel); | |
| 240 | |||
| 241 | ✗ | if (s->reinit) { | |
| 242 | ✗ | if (!atomic_load(&s->refcount)) | |
| 243 | ✗ | sem_post(&s->refsync); | |
| 244 | } else { | ||
| 245 | ✗ | if (s->draining && V4L2_TYPE_IS_OUTPUT(avbuf->context->type)) { | |
| 246 | /* no need to queue more buffers to the driver */ | ||
| 247 | ✗ | avbuf->status = V4L2BUF_AVAILABLE; | |
| 248 | } | ||
| 249 | ✗ | else if (avbuf->context->streamon) | |
| 250 | ✗ | ff_v4l2_buffer_enqueue(avbuf); | |
| 251 | } | ||
| 252 | |||
| 253 | ✗ | av_refstruct_unref(&avbuf->context_ref); | |
| 254 | } | ||
| 255 | ✗ | } | |
| 256 | |||
| 257 | ✗ | static int v4l2_buf_increase_ref(V4L2Buffer *in) | |
| 258 | { | ||
| 259 | ✗ | V4L2m2mContext *s = buf_to_m2mctx(in); | |
| 260 | |||
| 261 | ✗ | if (in->context_ref) | |
| 262 | ✗ | atomic_fetch_add(&in->context_refcount, 1); | |
| 263 | else { | ||
| 264 | ✗ | in->context_ref = av_refstruct_ref(s->self_ref); | |
| 265 | |||
| 266 | ✗ | in->context_refcount = 1; | |
| 267 | } | ||
| 268 | |||
| 269 | ✗ | in->status = V4L2BUF_RET_USER; | |
| 270 | ✗ | atomic_fetch_add_explicit(&s->refcount, 1, memory_order_relaxed); | |
| 271 | |||
| 272 | ✗ | return 0; | |
| 273 | } | ||
| 274 | |||
| 275 | ✗ | static int v4l2_buf_to_bufref(V4L2Buffer *in, int plane, AVBufferRef **buf) | |
| 276 | { | ||
| 277 | int ret; | ||
| 278 | |||
| 279 | ✗ | if (plane >= in->num_planes) | |
| 280 | ✗ | return AVERROR(EINVAL); | |
| 281 | |||
| 282 | /* even though most encoders return 0 in data_offset encoding vp8 does require this value */ | ||
| 283 | ✗ | *buf = av_buffer_create((char *)in->plane_info[plane].mm_addr + in->planes[plane].data_offset, | |
| 284 | in->plane_info[plane].length, v4l2_free_buffer, in, 0); | ||
| 285 | ✗ | if (!*buf) | |
| 286 | ✗ | return AVERROR(ENOMEM); | |
| 287 | |||
| 288 | ✗ | ret = v4l2_buf_increase_ref(in); | |
| 289 | ✗ | if (ret) | |
| 290 | ✗ | av_buffer_unref(buf); | |
| 291 | |||
| 292 | ✗ | return ret; | |
| 293 | } | ||
| 294 | |||
| 295 | ✗ | static int v4l2_bufref_to_buf(V4L2Buffer *out, int plane, const uint8_t* data, int size, int offset) | |
| 296 | { | ||
| 297 | unsigned int bytesused, length; | ||
| 298 | |||
| 299 | ✗ | if (plane >= out->num_planes) | |
| 300 | ✗ | return AVERROR(EINVAL); | |
| 301 | |||
| 302 | ✗ | length = out->plane_info[plane].length; | |
| 303 | ✗ | bytesused = FFMIN(size+offset, length); | |
| 304 | |||
| 305 | ✗ | memcpy((uint8_t*)out->plane_info[plane].mm_addr+offset, data, FFMIN(size, length-offset)); | |
| 306 | |||
| 307 | ✗ | if (V4L2_TYPE_IS_MULTIPLANAR(out->buf.type)) { | |
| 308 | ✗ | out->planes[plane].bytesused = bytesused; | |
| 309 | ✗ | out->planes[plane].length = length; | |
| 310 | } else { | ||
| 311 | ✗ | out->buf.bytesused = bytesused; | |
| 312 | ✗ | out->buf.length = length; | |
| 313 | } | ||
| 314 | |||
| 315 | ✗ | return 0; | |
| 316 | } | ||
| 317 | |||
| 318 | ✗ | static int v4l2_buffer_buf_to_swframe(AVFrame *frame, V4L2Buffer *avbuf) | |
| 319 | { | ||
| 320 | int i, ret; | ||
| 321 | |||
| 322 | ✗ | frame->format = avbuf->context->av_pix_fmt; | |
| 323 | |||
| 324 | ✗ | for (i = 0; i < avbuf->num_planes; i++) { | |
| 325 | ✗ | ret = v4l2_buf_to_bufref(avbuf, i, &frame->buf[i]); | |
| 326 | ✗ | if (ret) | |
| 327 | ✗ | return ret; | |
| 328 | |||
| 329 | ✗ | frame->linesize[i] = avbuf->plane_info[i].bytesperline; | |
| 330 | ✗ | frame->data[i] = frame->buf[i]->data; | |
| 331 | } | ||
| 332 | |||
| 333 | /* fixup special cases */ | ||
| 334 | ✗ | switch (avbuf->context->av_pix_fmt) { | |
| 335 | ✗ | case AV_PIX_FMT_NV12: | |
| 336 | case AV_PIX_FMT_NV21: | ||
| 337 | ✗ | if (avbuf->num_planes > 1) | |
| 338 | ✗ | break; | |
| 339 | ✗ | frame->linesize[1] = avbuf->plane_info[0].bytesperline; | |
| 340 | ✗ | frame->data[1] = frame->buf[0]->data + avbuf->plane_info[0].bytesperline * avbuf->context->format.fmt.pix_mp.height; | |
| 341 | ✗ | break; | |
| 342 | |||
| 343 | ✗ | case AV_PIX_FMT_YUV420P: | |
| 344 | ✗ | if (avbuf->num_planes > 1) | |
| 345 | ✗ | break; | |
| 346 | ✗ | frame->linesize[1] = avbuf->plane_info[0].bytesperline >> 1; | |
| 347 | ✗ | frame->linesize[2] = avbuf->plane_info[0].bytesperline >> 1; | |
| 348 | ✗ | frame->data[1] = frame->buf[0]->data + avbuf->plane_info[0].bytesperline * avbuf->context->format.fmt.pix_mp.height; | |
| 349 | ✗ | frame->data[2] = frame->data[1] + ((avbuf->plane_info[0].bytesperline * avbuf->context->format.fmt.pix_mp.height) >> 2); | |
| 350 | ✗ | break; | |
| 351 | |||
| 352 | ✗ | default: | |
| 353 | ✗ | break; | |
| 354 | } | ||
| 355 | |||
| 356 | ✗ | return 0; | |
| 357 | } | ||
| 358 | |||
| 359 | ✗ | static int v4l2_buffer_swframe_to_buf(const AVFrame *frame, V4L2Buffer *out) | |
| 360 | { | ||
| 361 | int i, ret; | ||
| 362 | ✗ | struct v4l2_format fmt = out->context->format; | |
| 363 | ✗ | int pixel_format = V4L2_TYPE_IS_MULTIPLANAR(fmt.type) ? | |
| 364 | ✗ | fmt.fmt.pix_mp.pixelformat : fmt.fmt.pix.pixelformat; | |
| 365 | ✗ | int height = V4L2_TYPE_IS_MULTIPLANAR(fmt.type) ? | |
| 366 | ✗ | fmt.fmt.pix_mp.height : fmt.fmt.pix.height; | |
| 367 | ✗ | int is_planar_format = 0; | |
| 368 | |||
| 369 | ✗ | switch (pixel_format) { | |
| 370 | ✗ | case V4L2_PIX_FMT_YUV420M: | |
| 371 | case V4L2_PIX_FMT_YVU420M: | ||
| 372 | #ifdef V4L2_PIX_FMT_YUV422M | ||
| 373 | case V4L2_PIX_FMT_YUV422M: | ||
| 374 | #endif | ||
| 375 | #ifdef V4L2_PIX_FMT_YVU422M | ||
| 376 | case V4L2_PIX_FMT_YVU422M: | ||
| 377 | #endif | ||
| 378 | #ifdef V4L2_PIX_FMT_YUV444M | ||
| 379 | case V4L2_PIX_FMT_YUV444M: | ||
| 380 | #endif | ||
| 381 | #ifdef V4L2_PIX_FMT_YVU444M | ||
| 382 | case V4L2_PIX_FMT_YVU444M: | ||
| 383 | #endif | ||
| 384 | case V4L2_PIX_FMT_NV12M: | ||
| 385 | case V4L2_PIX_FMT_NV21M: | ||
| 386 | case V4L2_PIX_FMT_NV12MT_16X16: | ||
| 387 | case V4L2_PIX_FMT_NV12MT: | ||
| 388 | case V4L2_PIX_FMT_NV16M: | ||
| 389 | case V4L2_PIX_FMT_NV61M: | ||
| 390 | ✗ | is_planar_format = 1; | |
| 391 | } | ||
| 392 | |||
| 393 | ✗ | if (!is_planar_format) { | |
| 394 | ✗ | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format); | |
| 395 | ✗ | int planes_nb = 0; | |
| 396 | ✗ | int offset = 0; | |
| 397 | |||
| 398 | ✗ | for (i = 0; i < desc->nb_components; i++) | |
| 399 | ✗ | planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1); | |
| 400 | |||
| 401 | ✗ | for (i = 0; i < planes_nb; i++) { | |
| 402 | ✗ | int size, h = height; | |
| 403 | ✗ | if (i == 1 || i == 2) { | |
| 404 | ✗ | h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h); | |
| 405 | } | ||
| 406 | ✗ | size = frame->linesize[i] * h; | |
| 407 | ✗ | ret = v4l2_bufref_to_buf(out, 0, frame->data[i], size, offset); | |
| 408 | ✗ | if (ret) | |
| 409 | ✗ | return ret; | |
| 410 | ✗ | offset += size; | |
| 411 | } | ||
| 412 | ✗ | return 0; | |
| 413 | } | ||
| 414 | |||
| 415 | ✗ | for (i = 0; i < out->num_planes; i++) { | |
| 416 | ✗ | ret = v4l2_bufref_to_buf(out, i, frame->buf[i]->data, frame->buf[i]->size, 0); | |
| 417 | ✗ | if (ret) | |
| 418 | ✗ | return ret; | |
| 419 | } | ||
| 420 | |||
| 421 | ✗ | return 0; | |
| 422 | } | ||
| 423 | |||
| 424 | /****************************************************************************** | ||
| 425 | * | ||
| 426 | * V4L2Buffer interface | ||
| 427 | * | ||
| 428 | ******************************************************************************/ | ||
| 429 | |||
| 430 | ✗ | int ff_v4l2_buffer_avframe_to_buf(const AVFrame *frame, V4L2Buffer *out) | |
| 431 | { | ||
| 432 | ✗ | v4l2_set_pts(out, frame->pts); | |
| 433 | |||
| 434 | ✗ | return v4l2_buffer_swframe_to_buf(frame, out); | |
| 435 | } | ||
| 436 | |||
| 437 | ✗ | int ff_v4l2_buffer_buf_to_avframe(AVFrame *frame, V4L2Buffer *avbuf) | |
| 438 | { | ||
| 439 | int ret; | ||
| 440 | |||
| 441 | ✗ | av_frame_unref(frame); | |
| 442 | |||
| 443 | /* 1. get references to the actual data */ | ||
| 444 | ✗ | ret = v4l2_buffer_buf_to_swframe(frame, avbuf); | |
| 445 | ✗ | if (ret) | |
| 446 | ✗ | return ret; | |
| 447 | |||
| 448 | /* 2. get frame information */ | ||
| 449 | ✗ | if (avbuf->buf.flags & V4L2_BUF_FLAG_KEYFRAME) | |
| 450 | ✗ | frame->flags |= AV_FRAME_FLAG_KEY; | |
| 451 | ✗ | frame->color_primaries = v4l2_get_color_primaries(avbuf); | |
| 452 | ✗ | frame->colorspace = v4l2_get_color_space(avbuf); | |
| 453 | ✗ | frame->color_range = v4l2_get_color_range(avbuf); | |
| 454 | ✗ | frame->color_trc = v4l2_get_color_trc(avbuf); | |
| 455 | ✗ | frame->pts = v4l2_get_pts(avbuf); | |
| 456 | ✗ | frame->pkt_dts = AV_NOPTS_VALUE; | |
| 457 | ✗ | v4l2_get_interlacing(frame, avbuf); | |
| 458 | |||
| 459 | /* these values are updated also during re-init in v4l2_process_driver_event */ | ||
| 460 | ✗ | frame->height = avbuf->context->height; | |
| 461 | ✗ | frame->width = avbuf->context->width; | |
| 462 | ✗ | frame->sample_aspect_ratio = avbuf->context->sample_aspect_ratio; | |
| 463 | |||
| 464 | /* 3. report errors upstream */ | ||
| 465 | ✗ | if (avbuf->buf.flags & V4L2_BUF_FLAG_ERROR) { | |
| 466 | ✗ | av_log(logger(avbuf), AV_LOG_ERROR, "%s: driver decode error\n", avbuf->context->name); | |
| 467 | ✗ | frame->decode_error_flags |= FF_DECODE_ERROR_INVALID_BITSTREAM; | |
| 468 | } | ||
| 469 | |||
| 470 | ✗ | return 0; | |
| 471 | } | ||
| 472 | |||
| 473 | ✗ | int ff_v4l2_buffer_buf_to_avpkt(AVPacket *pkt, V4L2Buffer *avbuf) | |
| 474 | { | ||
| 475 | int ret; | ||
| 476 | |||
| 477 | ✗ | av_packet_unref(pkt); | |
| 478 | ✗ | ret = v4l2_buf_to_bufref(avbuf, 0, &pkt->buf); | |
| 479 | ✗ | if (ret) | |
| 480 | ✗ | return ret; | |
| 481 | |||
| 482 | ✗ | pkt->size = V4L2_TYPE_IS_MULTIPLANAR(avbuf->buf.type) ? avbuf->buf.m.planes[0].bytesused : avbuf->buf.bytesused; | |
| 483 | ✗ | pkt->data = pkt->buf->data; | |
| 484 | |||
| 485 | ✗ | if (avbuf->buf.flags & V4L2_BUF_FLAG_KEYFRAME) | |
| 486 | ✗ | pkt->flags |= AV_PKT_FLAG_KEY; | |
| 487 | |||
| 488 | ✗ | if (avbuf->buf.flags & V4L2_BUF_FLAG_ERROR) { | |
| 489 | ✗ | av_log(logger(avbuf), AV_LOG_ERROR, "%s driver encode error\n", avbuf->context->name); | |
| 490 | ✗ | pkt->flags |= AV_PKT_FLAG_CORRUPT; | |
| 491 | } | ||
| 492 | |||
| 493 | ✗ | pkt->dts = pkt->pts = v4l2_get_pts(avbuf); | |
| 494 | |||
| 495 | ✗ | return 0; | |
| 496 | } | ||
| 497 | |||
| 498 | ✗ | int ff_v4l2_buffer_avpkt_to_buf(const AVPacket *pkt, V4L2Buffer *out) | |
| 499 | { | ||
| 500 | int ret; | ||
| 501 | |||
| 502 | ✗ | ret = v4l2_bufref_to_buf(out, 0, pkt->data, pkt->size, 0); | |
| 503 | ✗ | if (ret) | |
| 504 | ✗ | return ret; | |
| 505 | |||
| 506 | ✗ | v4l2_set_pts(out, pkt->pts); | |
| 507 | |||
| 508 | ✗ | if (pkt->flags & AV_PKT_FLAG_KEY) | |
| 509 | ✗ | out->flags = V4L2_BUF_FLAG_KEYFRAME; | |
| 510 | |||
| 511 | ✗ | return 0; | |
| 512 | } | ||
| 513 | |||
| 514 | ✗ | int ff_v4l2_buffer_initialize(V4L2Buffer* avbuf, int index) | |
| 515 | { | ||
| 516 | ✗ | V4L2Context *ctx = avbuf->context; | |
| 517 | int ret, i; | ||
| 518 | |||
| 519 | ✗ | avbuf->buf.memory = V4L2_MEMORY_MMAP; | |
| 520 | ✗ | avbuf->buf.type = ctx->type; | |
| 521 | ✗ | avbuf->buf.index = index; | |
| 522 | |||
| 523 | ✗ | if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) { | |
| 524 | ✗ | avbuf->buf.length = VIDEO_MAX_PLANES; | |
| 525 | ✗ | avbuf->buf.m.planes = avbuf->planes; | |
| 526 | } | ||
| 527 | |||
| 528 | ✗ | ret = ioctl(buf_to_m2mctx(avbuf)->fd, VIDIOC_QUERYBUF, &avbuf->buf); | |
| 529 | ✗ | if (ret < 0) | |
| 530 | ✗ | return AVERROR(errno); | |
| 531 | |||
| 532 | ✗ | if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) { | |
| 533 | ✗ | avbuf->num_planes = 0; | |
| 534 | /* in MP, the V4L2 API states that buf.length means num_planes */ | ||
| 535 | ✗ | for (i = 0; i < avbuf->buf.length; i++) { | |
| 536 | ✗ | if (avbuf->buf.m.planes[i].length) | |
| 537 | ✗ | avbuf->num_planes++; | |
| 538 | } | ||
| 539 | } else | ||
| 540 | ✗ | avbuf->num_planes = 1; | |
| 541 | |||
| 542 | ✗ | for (i = 0; i < avbuf->num_planes; i++) { | |
| 543 | |||
| 544 | ✗ | avbuf->plane_info[i].bytesperline = V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ? | |
| 545 | ✗ | ctx->format.fmt.pix_mp.plane_fmt[i].bytesperline : | |
| 546 | ✗ | ctx->format.fmt.pix.bytesperline; | |
| 547 | |||
| 548 | ✗ | if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) { | |
| 549 | ✗ | avbuf->plane_info[i].length = avbuf->buf.m.planes[i].length; | |
| 550 | ✗ | avbuf->plane_info[i].mm_addr = mmap(NULL, avbuf->buf.m.planes[i].length, | |
| 551 | PROT_READ | PROT_WRITE, MAP_SHARED, | ||
| 552 | ✗ | buf_to_m2mctx(avbuf)->fd, avbuf->buf.m.planes[i].m.mem_offset); | |
| 553 | } else { | ||
| 554 | ✗ | avbuf->plane_info[i].length = avbuf->buf.length; | |
| 555 | ✗ | avbuf->plane_info[i].mm_addr = mmap(NULL, avbuf->buf.length, | |
| 556 | PROT_READ | PROT_WRITE, MAP_SHARED, | ||
| 557 | ✗ | buf_to_m2mctx(avbuf)->fd, avbuf->buf.m.offset); | |
| 558 | } | ||
| 559 | |||
| 560 | ✗ | if (avbuf->plane_info[i].mm_addr == MAP_FAILED) | |
| 561 | ✗ | return AVERROR(ENOMEM); | |
| 562 | } | ||
| 563 | |||
| 564 | ✗ | avbuf->status = V4L2BUF_AVAILABLE; | |
| 565 | |||
| 566 | ✗ | if (V4L2_TYPE_IS_OUTPUT(ctx->type)) | |
| 567 | ✗ | return 0; | |
| 568 | |||
| 569 | ✗ | if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) { | |
| 570 | ✗ | avbuf->buf.m.planes = avbuf->planes; | |
| 571 | ✗ | avbuf->buf.length = avbuf->num_planes; | |
| 572 | |||
| 573 | } else { | ||
| 574 | ✗ | avbuf->buf.bytesused = avbuf->planes[0].bytesused; | |
| 575 | ✗ | avbuf->buf.length = avbuf->planes[0].length; | |
| 576 | } | ||
| 577 | |||
| 578 | ✗ | return ff_v4l2_buffer_enqueue(avbuf); | |
| 579 | } | ||
| 580 | |||
| 581 | ✗ | int ff_v4l2_buffer_enqueue(V4L2Buffer* avbuf) | |
| 582 | { | ||
| 583 | int ret; | ||
| 584 | |||
| 585 | ✗ | avbuf->buf.flags = avbuf->flags; | |
| 586 | |||
| 587 | ✗ | ret = ioctl(buf_to_m2mctx(avbuf)->fd, VIDIOC_QBUF, &avbuf->buf); | |
| 588 | ✗ | if (ret < 0) | |
| 589 | ✗ | return AVERROR(errno); | |
| 590 | |||
| 591 | ✗ | avbuf->status = V4L2BUF_IN_DRIVER; | |
| 592 | |||
| 593 | ✗ | return 0; | |
| 594 | } | ||
| 595 |