FFmpeg
v4l2enc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Clément Bœsch
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/imgutils.h"
22 #include "libavutil/pixdesc.h"
23 #include "libavformat/avformat.h"
24 #include "v4l2-common.h"
25 
26 typedef struct {
27  AVClass *class;
28  int fd;
29 } V4L2Context;
30 
32 {
33  int res = 0, flags = O_RDWR;
34  struct v4l2_format fmt = {
35  .type = V4L2_BUF_TYPE_VIDEO_OUTPUT
36  };
37  V4L2Context *s = s1->priv_data;
38  AVCodecParameters *par;
39  uint32_t v4l2_pixfmt;
40 
41  if (s1->flags & AVFMT_FLAG_NONBLOCK)
42  flags |= O_NONBLOCK;
43 
44  s->fd = open(s1->url, flags);
45  if (s->fd < 0) {
46  res = AVERROR(errno);
47  av_log(s1, AV_LOG_ERROR, "Unable to open V4L2 device '%s'\n", s1->url);
48  return res;
49  }
50 
51  if (s1->nb_streams != 1 ||
52  s1->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
54  "V4L2 output device supports only a single raw video stream\n");
55  return AVERROR(EINVAL);
56  }
57 
58  par = s1->streams[0]->codecpar;
59 
60  if(par->codec_id == AV_CODEC_ID_RAWVIDEO) {
61  v4l2_pixfmt = ff_fmt_ff2v4l(par->format, AV_CODEC_ID_RAWVIDEO);
62  } else {
63  v4l2_pixfmt = ff_fmt_ff2v4l(AV_PIX_FMT_NONE, par->codec_id);
64  }
65 
66  if (!v4l2_pixfmt) { // XXX: try to force them one by one?
67  av_log(s1, AV_LOG_ERROR, "Unknown V4L2 pixel format equivalent for %s\n",
69  return AVERROR(EINVAL);
70  }
71 
72  if (ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
73  res = AVERROR(errno);
74  av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n", av_err2str(res));
75  return res;
76  }
77 
78  fmt.fmt.pix.width = par->width;
79  fmt.fmt.pix.height = par->height;
80  fmt.fmt.pix.pixelformat = v4l2_pixfmt;
81  fmt.fmt.pix.sizeimage = av_image_get_buffer_size(par->format, par->width, par->height, 1);
82 
83  if (ioctl(s->fd, VIDIOC_S_FMT, &fmt) < 0) {
84  res = AVERROR(errno);
85  av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_S_FMT): %s\n", av_err2str(res));
86  return res;
87  }
88 
89  return res;
90 }
91 
93 {
94  const V4L2Context *s = s1->priv_data;
95  if (write(s->fd, pkt->data, pkt->size) == -1)
96  return AVERROR(errno);
97  return 0;
98 }
99 
101 {
102  const V4L2Context *s = s1->priv_data;
103  close(s->fd);
104  return 0;
105 }
106 
107 static const AVClass v4l2_class = {
108  .class_name = "V4L2 outdev",
109  .item_name = av_default_item_name,
110  .version = LIBAVUTIL_VERSION_INT,
112 };
113 
115  .name = "video4linux2,v4l2",
116  .long_name = NULL_IF_CONFIG_SMALL("Video4Linux2 output device"),
117  .priv_data_size = sizeof(V4L2Context),
118  .audio_codec = AV_CODEC_ID_NONE,
119  .video_codec = AV_CODEC_ID_RAWVIDEO,
123  .flags = AVFMT_NOFILE,
124  .priv_class = &v4l2_class,
125 };
AVOutputFormat::name
const char * name
Definition: avformat.h:510
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
v4l2_class
static const AVClass v4l2_class
Definition: v4l2enc.c:107
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:53
AV_CODEC_ID_RAWVIDEO
@ AV_CODEC_ID_RAWVIDEO
Definition: codec_id.h:63
pixdesc.h
V4L2Context::fd
int fd
Definition: v4l2enc.c:28
AVPacket::data
uint8_t * data
Definition: packet.h:374
ff_fmt_ff2v4l
uint32_t ff_fmt_ff2v4l(enum AVPixelFormat pix_fmt, enum AVCodecID codec_id)
Definition: v4l2-common.c:73
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:127
s1
#define s1
Definition: regdef.h:38
v4l2-common.h
write_header
static av_cold int write_header(AVFormatContext *s1)
Definition: v4l2enc.c:31
AVFormatContext
Format I/O context.
Definition: avformat.h:1213
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
V4L2Context
Definition: v4l2_context.h:37
write_trailer
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:100
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT
@ AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT
Definition: log.h:40
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:121
ff_v4l2_muxer
const AVOutputFormat ff_v4l2_muxer
Definition: v4l2enc.c:114
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:470
av_image_get_buffer_size
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
Return the size in bytes of the amount of data required to store an image with the given parameters.
Definition: imgutils.c:466
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:48
AVOutputFormat
Definition: avformat.h:509
AVCodecParameters::height
int height
Definition: codec_par.h:128
AVFMT_FLAG_NONBLOCK
#define AVFMT_FLAG_NONBLOCK
Do not block when reading packets from input.
Definition: avformat.h:1334
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
avformat.h
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecParameters::format
int format
Definition: codec_par.h:85
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:61
AVPacket
This structure stores compressed data.
Definition: packet.h:351
imgutils.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
write_packet
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:92
av_get_pix_fmt_name
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2582