FFmpeg
Loading...
Searching...
No Matches
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"
24#include "libavformat/mux.h"
25#include "v4l2-common.h"
26
27typedef struct {
28 AVClass *class;
29 int fd;
31
33{
34 int res = 0, flags = O_RDWR;
35 struct v4l2_format fmt = {
36 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT
37 };
38 V4L2Context *s = s1->priv_data;
40 uint32_t v4l2_pixfmt;
41
42 if (s1->flags & AVFMT_FLAG_NONBLOCK)
43 flags |= O_NONBLOCK;
44
45 s->fd = open(s1->url, flags);
46 if (s->fd < 0) {
47 res = AVERROR(errno);
48 av_log(s1, AV_LOG_ERROR, "Unable to open V4L2 device '%s'\n", s1->url);
49 return res;
50 }
51
52 if (s1->nb_streams != 1 ||
55 "V4L2 output device supports only a single raw video stream\n");
56 return AVERROR(EINVAL);
57 }
58
59 par = s1->streams[0]->codecpar;
60
61 if(par->codec_id == AV_CODEC_ID_RAWVIDEO) {
62 v4l2_pixfmt = ff_fmt_ff2v4l(par->format, AV_CODEC_ID_RAWVIDEO);
63 } else {
64 v4l2_pixfmt = ff_fmt_ff2v4l(AV_PIX_FMT_NONE, par->codec_id);
65 }
66
67 if (!v4l2_pixfmt) { // XXX: try to force them one by one?
68 av_log(s1, AV_LOG_ERROR, "Unknown V4L2 pixel format equivalent for %s\n",
70 return AVERROR(EINVAL);
71 }
72
73 if (ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
74 res = AVERROR(errno);
75 av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n", av_err2str(res));
76 return res;
77 }
78
79 fmt.fmt.pix.width = par->width;
80 fmt.fmt.pix.height = par->height;
81 fmt.fmt.pix.pixelformat = v4l2_pixfmt;
82 fmt.fmt.pix.sizeimage = av_image_get_buffer_size(par->format, par->width, par->height, 1);
83
84 if (ioctl(s->fd, VIDIOC_S_FMT, &fmt) < 0) {
85 res = AVERROR(errno);
86 av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_S_FMT): %s\n", av_err2str(res));
87 return res;
88 }
89
90 return res;
91}
92
94{
95 const V4L2Context *s = s1->priv_data;
96 if (write(s->fd, pkt->data, pkt->size) == -1)
97 return AVERROR(errno);
98 return 0;
99}
100
102{
103 const V4L2Context *s = s1->priv_data;
104 close(s->fd);
105 return 0;
106}
107
108static const AVClass v4l2_class = {
109 .class_name = "V4L2 outdev",
110 .item_name = av_default_item_name,
111 .version = LIBAVUTIL_VERSION_INT,
113};
114
116 .p.name = "video4linux2,v4l2",
117 .p.long_name = NULL_IF_CONFIG_SMALL("Video4Linux2 output device"),
118 .priv_data_size = sizeof(V4L2Context),
119 .p.audio_codec = AV_CODEC_ID_NONE,
120 .p.video_codec = AV_CODEC_ID_RAWVIDEO,
121 .write_header = write_header,
122 .write_packet = write_packet,
123 .write_trailer = write_trailer,
124 .p.flags = AVFMT_NOFILE,
125 .p.priv_class = &v4l2_class,
126};
const FFOutputFormat ff_v4l2_muxer
Definition v4l2enc.c:115
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
Main libavformat public API header.
#define AVFMT_FLAG_NONBLOCK
Do not block when reading packets from input.
Definition avformat.h:1487
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition avformat.h:488
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define s(width, name)
Definition cbs_vp9.c:198
static AVPacket * pkt
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
Definition ffmpeg_mux.c:204
static void write_header(FFV1Context *f)
Definition ffv1enc.c:384
@ AV_CODEC_ID_RAWVIDEO
Definition codec_id.h:63
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
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
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
misc image utilities
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
@ AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT
Definition log.h:41
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:3380
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
Describe the class of an AVClass context structure.
Definition log.h:76
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1333
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition avformat.h:1389
int flags
Flags modifying the (de)muxer behaviour.
Definition avformat.h:1484
char * url
input or output URL.
Definition avformat.h:1449
void * priv_data
Format private data.
Definition avformat.h:1361
AVStream ** streams
A list of all streams in the file.
Definition avformat.h:1401
This structure stores compressed data.
Definition packet.h:580
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
#define av_log(a,...)
uint32_t ff_fmt_ff2v4l(enum AVPixelFormat pix_fmt, enum AVCodecID codec_id)
Definition v4l2-common.c:79
static const AVClass v4l2_class
Definition v4l2.c:1173
static av_cold int write_header(AVFormatContext *s1)
Definition v4l2enc.c:32
static int write_trailer(AVFormatContext *s1)
Definition v4l2enc.c:101
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition v4l2enc.c:93