FFmpeg
v4l2_buffers.c
Go to the documentation of this file.
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 
42 
44 {
45  return V4L2_TYPE_IS_OUTPUT(buf->context->type) ?
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 
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 */
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 
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 
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 
138 }
139 
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 
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  case V4L2_XFER_FUNC_SMPTE240M: return AVCOL_TRC_SMPTE240M;
194  case V4L2_XFER_FUNC_NONE: return AVCOL_TRC_LINEAR;
195  case V4L2_XFER_FUNC_SMPTE2084: return AVCOL_TRC_SMPTE2084;
196  default:
197  break;
198  }
199 
200  switch (cs) {
201  case V4L2_COLORSPACE_470_SYSTEM_M: return AVCOL_TRC_GAMMA22;
202  case V4L2_COLORSPACE_470_SYSTEM_BG: return AVCOL_TRC_GAMMA28;
203  case V4L2_COLORSPACE_SMPTE170M: return AVCOL_TRC_SMPTE170M;
204  case V4L2_COLORSPACE_SMPTE240M: return AVCOL_TRC_SMPTE240M;
205  default:
206  break;
207  }
208 
209  switch (ycbcr) {
210  case V4L2_YCBCR_ENC_XV709:
211  case V4L2_YCBCR_ENC_XV601: return AVCOL_TRC_BT1361_ECG;
212  default:
213  break;
214  }
215 
216  return AVCOL_TRC_UNSPECIFIED;
217 }
218 
220 {
221  enum v4l2_field field = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
222  buf->context->format.fmt.pix_mp.field :
223  buf->context->format.fmt.pix.field;
224 
225  switch (field) {
226  case V4L2_FIELD_INTERLACED:
227  case V4L2_FIELD_INTERLACED_TB:
229  /* fallthrough */
230  case V4L2_FIELD_INTERLACED_BT:
232  break;
233  }
234 }
235 
236 static void v4l2_free_buffer(void *opaque, uint8_t *unused)
237 {
238  V4L2Buffer* avbuf = opaque;
239  V4L2m2mContext *s = buf_to_m2mctx(avbuf);
240 
241  if (atomic_fetch_sub(&avbuf->context_refcount, 1) == 1) {
242  atomic_fetch_sub_explicit(&s->refcount, 1, memory_order_acq_rel);
243 
244  if (s->reinit) {
245  if (!atomic_load(&s->refcount))
246  sem_post(&s->refsync);
247  } else {
248  if (s->draining && V4L2_TYPE_IS_OUTPUT(avbuf->context->type)) {
249  /* no need to queue more buffers to the driver */
250  avbuf->status = V4L2BUF_AVAILABLE;
251  }
252  else if (avbuf->context->streamon)
253  ff_v4l2_buffer_enqueue(avbuf);
254  }
255 
257  }
258 }
259 
261 {
263 
264  if (in->context_ref)
266  else {
267  in->context_ref = av_refstruct_ref(s->self_ref);
268 
269  in->context_refcount = 1;
270  }
271 
272  in->status = V4L2BUF_RET_USER;
273  atomic_fetch_add_explicit(&s->refcount, 1, memory_order_relaxed);
274 
275  return 0;
276 }
277 
278 static int v4l2_buf_to_bufref(V4L2Buffer *in, int plane, AVBufferRef **buf)
279 {
280  int ret;
281 
282  if (plane >= in->num_planes)
283  return AVERROR(EINVAL);
284 
285  /* even though most encoders return 0 in data_offset encoding vp8 does require this value */
286  *buf = av_buffer_create((char *)in->plane_info[plane].mm_addr + in->planes[plane].data_offset,
287  in->plane_info[plane].length, v4l2_free_buffer, in, 0);
288  if (!*buf)
289  return AVERROR(ENOMEM);
290 
292  if (ret)
293  av_buffer_unref(buf);
294 
295  return ret;
296 }
297 
298 static int v4l2_bufref_to_buf(V4L2Buffer *out, int plane, const uint8_t* data, int size, int offset)
299 {
300  unsigned int bytesused, length;
301 
302  if (plane >= out->num_planes)
303  return AVERROR(EINVAL);
304 
305  length = out->plane_info[plane].length;
306  bytesused = FFMIN(size+offset, length);
307 
308  memcpy((uint8_t*)out->plane_info[plane].mm_addr+offset, data, FFMIN(size, length-offset));
309 
310  if (V4L2_TYPE_IS_MULTIPLANAR(out->buf.type)) {
311  out->planes[plane].bytesused = bytesused;
312  out->planes[plane].length = length;
313  } else {
314  out->buf.bytesused = bytesused;
315  out->buf.length = length;
316  }
317 
318  return 0;
319 }
320 
322 {
323  int i, ret;
324 
325  frame->format = avbuf->context->av_pix_fmt;
326 
327  for (i = 0; i < avbuf->num_planes; i++) {
328  ret = v4l2_buf_to_bufref(avbuf, i, &frame->buf[i]);
329  if (ret)
330  return ret;
331 
332  frame->linesize[i] = avbuf->plane_info[i].bytesperline;
333  frame->data[i] = frame->buf[i]->data;
334  }
335 
336  /* fixup special cases */
337  switch (avbuf->context->av_pix_fmt) {
338  case AV_PIX_FMT_NV12:
339  case AV_PIX_FMT_NV21:
340  if (avbuf->num_planes > 1)
341  break;
342  frame->linesize[1] = avbuf->plane_info[0].bytesperline;
343  frame->data[1] = frame->buf[0]->data + avbuf->plane_info[0].bytesperline * avbuf->context->format.fmt.pix_mp.height;
344  break;
345 
346  case AV_PIX_FMT_YUV420P:
347  if (avbuf->num_planes > 1)
348  break;
349  frame->linesize[1] = avbuf->plane_info[0].bytesperline >> 1;
350  frame->linesize[2] = avbuf->plane_info[0].bytesperline >> 1;
351  frame->data[1] = frame->buf[0]->data + avbuf->plane_info[0].bytesperline * avbuf->context->format.fmt.pix_mp.height;
352  frame->data[2] = frame->data[1] + ((avbuf->plane_info[0].bytesperline * avbuf->context->format.fmt.pix_mp.height) >> 2);
353  break;
354 
355  default:
356  break;
357  }
358 
359  return 0;
360 }
361 
363 {
364  int i, ret;
365  struct v4l2_format fmt = out->context->format;
366  int pixel_format = V4L2_TYPE_IS_MULTIPLANAR(fmt.type) ?
367  fmt.fmt.pix_mp.pixelformat : fmt.fmt.pix.pixelformat;
368  int height = V4L2_TYPE_IS_MULTIPLANAR(fmt.type) ?
369  fmt.fmt.pix_mp.height : fmt.fmt.pix.height;
370  int is_planar_format = 0;
371 
372  switch (pixel_format) {
373  case V4L2_PIX_FMT_YUV420M:
374  case V4L2_PIX_FMT_YVU420M:
375 #ifdef V4L2_PIX_FMT_YUV422M
376  case V4L2_PIX_FMT_YUV422M:
377 #endif
378 #ifdef V4L2_PIX_FMT_YVU422M
379  case V4L2_PIX_FMT_YVU422M:
380 #endif
381 #ifdef V4L2_PIX_FMT_YUV444M
382  case V4L2_PIX_FMT_YUV444M:
383 #endif
384 #ifdef V4L2_PIX_FMT_YVU444M
385  case V4L2_PIX_FMT_YVU444M:
386 #endif
387  case V4L2_PIX_FMT_NV12M:
388  case V4L2_PIX_FMT_NV21M:
389  case V4L2_PIX_FMT_NV12MT_16X16:
390  case V4L2_PIX_FMT_NV12MT:
391  case V4L2_PIX_FMT_NV16M:
392  case V4L2_PIX_FMT_NV61M:
393  is_planar_format = 1;
394  }
395 
396  if (!is_planar_format) {
398  int planes_nb = 0;
399  int offset = 0;
400 
401  for (i = 0; i < desc->nb_components; i++)
402  planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
403 
404  for (i = 0; i < planes_nb; i++) {
405  int size, h = height;
406  if (i == 1 || i == 2) {
407  h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
408  }
409  size = frame->linesize[i] * h;
410  ret = v4l2_bufref_to_buf(out, 0, frame->data[i], size, offset);
411  if (ret)
412  return ret;
413  offset += size;
414  }
415  return 0;
416  }
417 
418  for (i = 0; i < out->num_planes; i++) {
419  ret = v4l2_bufref_to_buf(out, i, frame->buf[i]->data, frame->buf[i]->size, 0);
420  if (ret)
421  return ret;
422  }
423 
424  return 0;
425 }
426 
427 /******************************************************************************
428  *
429  * V4L2Buffer interface
430  *
431  ******************************************************************************/
432 
434 {
435  v4l2_set_pts(out, frame->pts);
436 
438 }
439 
441 {
442  int ret;
443 
445 
446  /* 1. get references to the actual data */
448  if (ret)
449  return ret;
450 
451  /* 2. get frame information */
452  if (avbuf->buf.flags & V4L2_BUF_FLAG_KEYFRAME)
453  frame->flags |= AV_FRAME_FLAG_KEY;
454  frame->color_primaries = v4l2_get_color_primaries(avbuf);
455  frame->colorspace = v4l2_get_color_space(avbuf);
456  frame->color_range = v4l2_get_color_range(avbuf);
457  frame->color_trc = v4l2_get_color_trc(avbuf);
458  frame->pts = v4l2_get_pts(avbuf);
459  frame->pkt_dts = AV_NOPTS_VALUE;
460  v4l2_get_interlacing(frame, avbuf);
461 
462  /* these values are updated also during re-init in v4l2_process_driver_event */
463  frame->height = avbuf->context->height;
464  frame->width = avbuf->context->width;
465  frame->sample_aspect_ratio = avbuf->context->sample_aspect_ratio;
466 
467  /* 3. report errors upstream */
468  if (avbuf->buf.flags & V4L2_BUF_FLAG_ERROR) {
469  av_log(logger(avbuf), AV_LOG_ERROR, "%s: driver decode error\n", avbuf->context->name);
470  frame->decode_error_flags |= FF_DECODE_ERROR_INVALID_BITSTREAM;
471  }
472 
473  return 0;
474 }
475 
477 {
478  int ret;
479 
481  ret = v4l2_buf_to_bufref(avbuf, 0, &pkt->buf);
482  if (ret)
483  return ret;
484 
485  pkt->size = V4L2_TYPE_IS_MULTIPLANAR(avbuf->buf.type) ? avbuf->buf.m.planes[0].bytesused : avbuf->buf.bytesused;
486  pkt->data = pkt->buf->data;
487 
488  if (avbuf->buf.flags & V4L2_BUF_FLAG_KEYFRAME)
490 
491  if (avbuf->buf.flags & V4L2_BUF_FLAG_ERROR) {
492  av_log(logger(avbuf), AV_LOG_ERROR, "%s driver encode error\n", avbuf->context->name);
494  }
495 
496  pkt->dts = pkt->pts = v4l2_get_pts(avbuf);
497 
498  return 0;
499 }
500 
502 {
503  int ret;
504 
505  ret = v4l2_bufref_to_buf(out, 0, pkt->data, pkt->size, 0);
506  if (ret)
507  return ret;
508 
509  v4l2_set_pts(out, pkt->pts);
510 
511  if (pkt->flags & AV_PKT_FLAG_KEY)
512  out->flags = V4L2_BUF_FLAG_KEYFRAME;
513 
514  return 0;
515 }
516 
518 {
519  V4L2Context *ctx = avbuf->context;
520  int ret, i;
521 
522  avbuf->buf.memory = V4L2_MEMORY_MMAP;
523  avbuf->buf.type = ctx->type;
524  avbuf->buf.index = index;
525 
526  if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
527  avbuf->buf.length = VIDEO_MAX_PLANES;
528  avbuf->buf.m.planes = avbuf->planes;
529  }
530 
531  ret = ioctl(buf_to_m2mctx(avbuf)->fd, VIDIOC_QUERYBUF, &avbuf->buf);
532  if (ret < 0)
533  return AVERROR(errno);
534 
535  if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
536  avbuf->num_planes = 0;
537  /* in MP, the V4L2 API states that buf.length means num_planes */
538  for (i = 0; i < avbuf->buf.length; i++) {
539  if (avbuf->buf.m.planes[i].length)
540  avbuf->num_planes++;
541  }
542  } else
543  avbuf->num_planes = 1;
544 
545  for (i = 0; i < avbuf->num_planes; i++) {
546 
547  avbuf->plane_info[i].bytesperline = V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ?
548  ctx->format.fmt.pix_mp.plane_fmt[i].bytesperline :
549  ctx->format.fmt.pix.bytesperline;
550 
551  if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
552  avbuf->plane_info[i].length = avbuf->buf.m.planes[i].length;
553  avbuf->plane_info[i].mm_addr = mmap(NULL, avbuf->buf.m.planes[i].length,
554  PROT_READ | PROT_WRITE, MAP_SHARED,
555  buf_to_m2mctx(avbuf)->fd, avbuf->buf.m.planes[i].m.mem_offset);
556  } else {
557  avbuf->plane_info[i].length = avbuf->buf.length;
558  avbuf->plane_info[i].mm_addr = mmap(NULL, avbuf->buf.length,
559  PROT_READ | PROT_WRITE, MAP_SHARED,
560  buf_to_m2mctx(avbuf)->fd, avbuf->buf.m.offset);
561  }
562 
563  if (avbuf->plane_info[i].mm_addr == MAP_FAILED)
564  return AVERROR(ENOMEM);
565  }
566 
567  avbuf->status = V4L2BUF_AVAILABLE;
568 
569  if (V4L2_TYPE_IS_OUTPUT(ctx->type))
570  return 0;
571 
572  if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
573  avbuf->buf.m.planes = avbuf->planes;
574  avbuf->buf.length = avbuf->num_planes;
575 
576  } else {
577  avbuf->buf.bytesused = avbuf->planes[0].bytesused;
578  avbuf->buf.length = avbuf->planes[0].length;
579  }
580 
581  return ff_v4l2_buffer_enqueue(avbuf);
582 }
583 
585 {
586  int ret;
587 
588  avbuf->buf.flags = avbuf->flags;
589 
590  ret = ioctl(buf_to_m2mctx(avbuf)->fd, VIDIOC_QBUF, &avbuf->buf);
591  if (ret < 0)
592  return AVERROR(errno);
593 
594  avbuf->status = V4L2BUF_IN_DRIVER;
595 
596  return 0;
597 }
V4L2Buffer::num_planes
int num_planes
Definition: v4l2_buffers.h:60
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: packet.c:432
V4L2Context::av_pix_fmt
enum AVPixelFormat av_pix_fmt
AVPixelFormat corresponding to this buffer context.
Definition: v4l2_context.h:54
v4l2_get_color_space
static enum AVColorSpace v4l2_get_color_space(V4L2Buffer *buf)
Definition: v4l2_buffers.c:140
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVColorTransferCharacteristic
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition: pixfmt.h:666
out
FILE * out
Definition: movenc.c:55
v4l2_buffers.h
atomic_fetch_add
#define atomic_fetch_add(object, operand)
Definition: stdatomic.h:137
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3456
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
AVCOL_TRC_LINEAR
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
Definition: pixfmt.h:675
int64_t
long long int64_t
Definition: coverity.c:34
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:226
ff_v4l2_buffer_buf_to_avpkt
int ff_v4l2_buffer_buf_to_avpkt(AVPacket *pkt, V4L2Buffer *avbuf)
Extracts the data from a V4L2Buffer to an AVPacket.
Definition: v4l2_buffers.c:476
V4L2m2mContext
Definition: v4l2_m2m.h:43
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:427
V4L2Buffer::V4L2Plane_info::length
size_t length
Definition: v4l2_buffers.h:57
pixdesc.h
v4l2_set_pts
static void v4l2_set_pts(V4L2Buffer *out, int64_t pts)
Definition: v4l2_buffers.c:64
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:777
AVPacket::data
uint8_t * data
Definition: packet.h:588
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:669
data
const char data[16]
Definition: mxf.c:149
AVCOL_SPC_RGB
@ AVCOL_SPC_RGB
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
Definition: pixfmt.h:701
USEC_PER_SEC
#define USEC_PER_SEC
Definition: v4l2_buffers.c:38
atomic_fetch_sub
#define atomic_fetch_sub(object, operand)
Definition: stdatomic.h:140
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVColorPrimaries
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition: pixfmt.h:636
logger
static AVCodecContext * logger(V4L2Buffer *buf)
Definition: v4l2_buffers.c:50
AVCOL_SPC_BT2020_CL
@ AVCOL_SPC_BT2020_CL
ITU-R BT2020 constant luminance system.
Definition: pixfmt.h:712
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:643
v4l2_get_timebase
static AVRational v4l2_get_timebase(V4L2Buffer *avbuf)
Definition: v4l2_buffers.c:55
AV_FRAME_FLAG_TOP_FIELD_FIRST
#define AV_FRAME_FLAG_TOP_FIELD_FIRST
A flag to mark frames where the top field is displayed first if the content is interlaced.
Definition: frame.h:655
v4l2_bufref_to_buf
static int v4l2_bufref_to_buf(V4L2Buffer *out, int plane, const uint8_t *data, int size, int offset)
Definition: v4l2_buffers.c:298
AVCOL_SPC_BT470BG
@ AVCOL_SPC_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
Definition: pixfmt.h:706
AVCOL_TRC_IEC61966_2_1
@ AVCOL_TRC_IEC61966_2_1
IEC 61966-2-1 (sRGB or sYCC)
Definition: pixfmt.h:680
v4l2_buf_increase_ref
static int v4l2_buf_increase_ref(V4L2Buffer *in)
Definition: v4l2_buffers.c:260
V4L2Buffer::buf
struct v4l2_buffer buf
Definition: v4l2_buffers.h:63
ff_v4l2_buffer_buf_to_avframe
int ff_v4l2_buffer_buf_to_avframe(AVFrame *frame, V4L2Buffer *avbuf)
Extracts the data from a V4L2Buffer to an AVFrame.
Definition: v4l2_buffers.c:440
AVCOL_TRC_GAMMA28
@ AVCOL_TRC_GAMMA28
also ITU-R BT470BG
Definition: pixfmt.h:672
pts
static int64_t pts
Definition: transcode_aac.c:644
V4L2Context::streamon
int streamon
Whether the stream has been started (VIDIOC_STREAMON has been sent).
Definition: v4l2_context.h:88
refstruct.h
AVCOL_TRC_GAMMA22
@ AVCOL_TRC_GAMMA22
also ITU-R BT470M / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:671
V4L2Buffer::context
struct V4L2Context * context
Definition: v4l2_buffers.h:45
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
ff_v4l2_buffer_avframe_to_buf
int ff_v4l2_buffer_avframe_to_buf(const AVFrame *frame, V4L2Buffer *out)
Extracts the data from an AVFrame to a V4L2Buffer.
Definition: v4l2_buffers.c:433
AV_PKT_FLAG_CORRUPT
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: packet.h:644
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:642
V4L2Buffer
V4L2Buffer (wrapper for v4l2_buffer management)
Definition: v4l2_buffers.h:43
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVCOL_TRC_BT1361_ECG
@ AVCOL_TRC_BT1361_ECG
ITU-R BT1361 Extended Colour Gamut.
Definition: pixfmt.h:679
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:60
AVCOL_SPC_SMPTE170M
@ AVCOL_SPC_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC / functionally identical to above
Definition: pixfmt.h:707
V4L2BUF_RET_USER
@ V4L2BUF_RET_USER
Definition: v4l2_buffers.h:37
ctx
AVFormatContext * ctx
Definition: movenc.c:49
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
v4l2_get_color_primaries
static enum AVColorPrimaries v4l2_get_color_primaries(V4L2Buffer *buf)
Definition: v4l2_buffers.c:88
field
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this field
Definition: writing_filters.txt:78
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
AVCOL_PRI_SMPTE240M
@ AVCOL_PRI_SMPTE240M
identical to above, also called "SMPTE C" even though it uses D65
Definition: pixfmt.h:645
atomic_load
#define atomic_load(object)
Definition: stdatomic.h:93
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:639
AVCOL_PRI_BT470BG
@ AVCOL_PRI_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:643
AVCOL_PRI_SMPTE170M
@ AVCOL_PRI_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:644
ff_v4l2_buffer_enqueue
int ff_v4l2_buffer_enqueue(V4L2Buffer *avbuf)
Enqueues a V4L2Buffer.
Definition: v4l2_buffers.c:584
AVPacket::buf
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: packet.h:571
v4l2_buffer_swframe_to_buf
static int v4l2_buffer_swframe_to_buf(const AVFrame *frame, V4L2Buffer *out)
Definition: v4l2_buffers.c:362
V4L2Context
Definition: v4l2_context.h:37
NULL
#define NULL
Definition: coverity.c:32
FF_DECODE_ERROR_INVALID_BITSTREAM
#define FF_DECODE_ERROR_INVALID_BITSTREAM
Definition: frame.h:715
av_buffer_unref
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:139
container_of
#define container_of(ptr, type, member)
Definition: v4l2_m2m.h:35
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
V4L2Context::type
enum v4l2_buf_type type
Type of this buffer context.
Definition: v4l2_context.h:48
AVCOL_PRI_BT709
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
Definition: pixfmt.h:638
V4L2Buffer::V4L2Plane_info::mm_addr
void * mm_addr
Definition: v4l2_buffers.h:56
v4l2_get_color_range
static enum AVColorRange v4l2_get_color_range(V4L2Buffer *buf)
Definition: v4l2_buffers.c:122
atomic_fetch_sub_explicit
#define atomic_fetch_sub_explicit(object, operand, order)
Definition: stdatomic.h:152
AVCOL_RANGE_UNSPECIFIED
@ AVCOL_RANGE_UNSPECIFIED
Definition: pixfmt.h:743
index
int index
Definition: gxfenc.c:90
ff_v4l2_buffer_avpkt_to_buf
int ff_v4l2_buffer_avpkt_to_buf(const AVPacket *pkt, V4L2Buffer *out)
Extracts the data from an AVPacket to a V4L2Buffer.
Definition: v4l2_buffers.c:501
av_buffer_create
AVBufferRef * av_buffer_create(uint8_t *data, size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:55
AVCOL_PRI_BT2020
@ AVCOL_PRI_BT2020
ITU-R BT2020.
Definition: pixfmt.h:647
V4L2Buffer::context_ref
const struct V4L2m2mContext * context_ref
Definition: v4l2_buffers.h:50
V4L2BUF_AVAILABLE
@ V4L2BUF_AVAILABLE
Definition: v4l2_buffers.h:35
AVCOL_TRC_SMPTE2084
@ AVCOL_TRC_SMPTE2084
SMPTE ST 2084 for 10-, 12-, 14- and 16-bit systems.
Definition: pixfmt.h:683
V4L2Context::width
int width
Width and height of the frames it produces (in case of a capture context, e.g.
Definition: v4l2_context.h:72
AVPacket::size
int size
Definition: packet.h:589
AVCOL_TRC_SMPTE240M
@ AVCOL_TRC_SMPTE240M
Definition: pixfmt.h:674
height
#define height
Definition: dsp.h:89
V4L2Buffer::plane_info
struct V4L2Buffer::V4L2Plane_info plane_info[VIDEO_MAX_PLANES]
V4L2BUF_IN_DRIVER
@ V4L2BUF_IN_DRIVER
Definition: v4l2_buffers.h:36
size
int size
Definition: twinvq_data.h:10344
atomic_fetch_add_explicit
#define atomic_fetch_add_explicit(object, operand, order)
Definition: stdatomic.h:149
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:247
v4l2_get_pts
static int64_t v4l2_get_pts(V4L2Buffer *avbuf)
Definition: v4l2_buffers.c:77
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:587
v4l2_timebase
static AVRational v4l2_timebase
Definition: v4l2_buffers.c:41
av_refstruct_ref
void * av_refstruct_ref(void *obj)
Create a new reference to an object managed via this API, i.e.
Definition: refstruct.c:140
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:594
V4L2Buffer::context_refcount
atomic_uint context_refcount
Definition: v4l2_buffers.h:51
AVCOL_TRC_BT709
@ AVCOL_TRC_BT709
also ITU-R BT1361
Definition: pixfmt.h:668
av_refstruct_unref
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition: refstruct.c:120
V4L2m2mContext::avctx
AVCodecContext * avctx
Definition: v4l2_m2m.h:52
AVCOL_SPC_SMPTE240M
@ AVCOL_SPC_SMPTE240M
derived from 170M primaries and D65 white point, 170M is derived from BT470 System M's primaries
Definition: pixfmt.h:708
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:581
AVCOL_SPC_BT2020_NCL
@ AVCOL_SPC_BT2020_NCL
ITU-R BT2020 non-constant luminance system.
Definition: pixfmt.h:711
v4l2_buffer_buf_to_swframe
static int v4l2_buffer_buf_to_swframe(AVFrame *frame, V4L2Buffer *avbuf)
Definition: v4l2_buffers.c:321
AVColorSpace
AVColorSpace
YUV colorspace type.
Definition: pixfmt.h:700
V4L2Buffer::flags
int flags
Definition: v4l2_buffers.h:66
V4L2Buffer::planes
struct v4l2_plane planes[VIDEO_MAX_PLANES]
Definition: v4l2_buffers.h:64
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
V4L2Buffer::V4L2Plane_info::bytesperline
int bytesperline
Definition: v4l2_buffers.h:55
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:496
AV_PIX_FMT_NV21
@ AV_PIX_FMT_NV21
as above, but U and V bytes are swapped
Definition: pixfmt.h:97
v4l2_context.h
AVCOL_SPC_UNSPECIFIED
@ AVCOL_SPC_UNSPECIFIED
Definition: pixfmt.h:703
v4l2_buf_to_bufref
static int v4l2_buf_to_bufref(V4L2Buffer *in, int plane, AVBufferRef **buf)
Definition: v4l2_buffers.c:278
AV_FRAME_FLAG_INTERLACED
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
Definition: frame.h:650
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:760
avcodec.h
AVCOL_PRI_BT470M
@ AVCOL_PRI_BT470M
also FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
Definition: pixfmt.h:641
ret
ret
Definition: filter_design.txt:187
AV_PIX_FMT_NV12
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:96
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:265
buf_to_m2mctx
static V4L2m2mContext * buf_to_m2mctx(V4L2Buffer *buf)
Definition: v4l2_buffers.c:43
V4L2Context::height
int height
Definition: v4l2_context.h:72
AVCodecContext
main external API structure.
Definition: avcodec.h:431
AVCOL_SPC_FCC
@ AVCOL_SPC_FCC
FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
Definition: pixfmt.h:705
sem_post
#define sem_post(psem)
Definition: semaphore.h:26
AVCOL_TRC_SMPTE170M
@ AVCOL_TRC_SMPTE170M
also ITU-R BT601-6 525 or 625 / ITU-R BT1358 525 or 625 / ITU-R BT1700 NTSC
Definition: pixfmt.h:673
v4l2_free_buffer
static void v4l2_free_buffer(void *opaque, uint8_t *unused)
Definition: v4l2_buffers.c:236
V4L2Context::name
const char * name
context name.
Definition: v4l2_context.h:41
desc
const char * desc
Definition: libsvtav1.c:78
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
AVPacket
This structure stores compressed data.
Definition: packet.h:565
V4L2Context::format
struct v4l2_format format
Format returned by the driver after initializing the buffer context.
Definition: v4l2_context.h:66
v4l2_m2m.h
ff_v4l2_buffer_initialize
int ff_v4l2_buffer_initialize(V4L2Buffer *avbuf, int index)
Initializes a V4L2Buffer.
Definition: v4l2_buffers.c:517
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
v4l2_get_color_trc
static enum AVColorTransferCharacteristic v4l2_get_color_trc(V4L2Buffer *buf)
Definition: v4l2_buffers.c:172
h
h
Definition: vp9dsp_template.c:2070
AVCOL_SPC_BT709
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
Definition: pixfmt.h:702
AVColorRange
AVColorRange
Visual content value range.
Definition: pixfmt.h:742
V4L2Context::sample_aspect_ratio
AVRational sample_aspect_ratio
Definition: v4l2_context.h:73
v4l2_get_interlacing
static void v4l2_get_interlacing(AVFrame *frame, V4L2Buffer *buf)
Definition: v4l2_buffers.c:219
V4L2Buffer::status
enum V4L2Buffer_status status
Definition: v4l2_buffers.h:67