FFmpeg  2.1.1
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 "v4l2-common.h"
22 #include "avdevice.h"
23 
24 typedef struct {
25  int fd;
26 } V4L2Context;
27 
29 {
30  int res = 0, flags = O_RDWR;
31  struct v4l2_format fmt = {
32  .type = V4L2_BUF_TYPE_VIDEO_OUTPUT
33  };
34  V4L2Context *s = s1->priv_data;
35  AVCodecContext *enc_ctx;
36  uint32_t v4l2_pixfmt;
37 
38  if (s1->flags & AVFMT_FLAG_NONBLOCK)
39  flags |= O_NONBLOCK;
40 
41  s->fd = open(s1->filename, flags);
42  if (s->fd < 0) {
43  res = AVERROR(errno);
44  av_log(s1, AV_LOG_ERROR, "Unable to open V4L2 device '%s'\n", s1->filename);
45  return res;
46  }
47 
48  if (s1->nb_streams != 1 ||
51  av_log(s1, AV_LOG_ERROR,
52  "V4L2 output device supports only a single raw video stream\n");
53  return AVERROR(EINVAL);
54  }
55 
56  enc_ctx = s1->streams[0]->codec;
57 
58  v4l2_pixfmt = avpriv_fmt_ff2v4l(enc_ctx->pix_fmt, AV_CODEC_ID_RAWVIDEO);
59  if (!v4l2_pixfmt) { // XXX: try to force them one by one?
60  av_log(s1, AV_LOG_ERROR, "Unknown V4L2 pixel format equivalent for %s\n",
61  av_get_pix_fmt_name(enc_ctx->pix_fmt));
62  return AVERROR(EINVAL);
63  }
64 
65  if (ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
66  res = AVERROR(errno);
67  av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n", av_err2str(res));
68  return res;
69  }
70 
71  fmt.fmt.pix.width = enc_ctx->width;
72  fmt.fmt.pix.height = enc_ctx->height;
73  fmt.fmt.pix.pixelformat = v4l2_pixfmt;
74  fmt.fmt.pix.sizeimage = av_image_get_buffer_size(enc_ctx->pix_fmt, enc_ctx->width, enc_ctx->height, 1);
75 
76  if (ioctl(s->fd, VIDIOC_S_FMT, &fmt) < 0) {
77  res = AVERROR(errno);
78  av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_S_FMT): %s\n", av_err2str(res));
79  return res;
80  }
81 
82  return res;
83 }
84 
86 {
87  const V4L2Context *s = s1->priv_data;
88  if (write(s->fd, pkt->data, pkt->size) == -1)
89  return AVERROR(errno);
90  return 0;
91 }
92 
94 {
95  const V4L2Context *s = s1->priv_data;
96  close(s->fd);
97  return 0;
98 }
99 
101  .name = "v4l2",
102  .long_name = NULL_IF_CONFIG_SMALL("Video4Linux2 output device"),
103  .priv_data_size = sizeof(V4L2Context),
104  .audio_codec = AV_CODEC_ID_NONE,
105  .video_codec = AV_CODEC_ID_RAWVIDEO,
109  .flags = AVFMT_NOFILE,
110 };
#define AVFMT_FLAG_NONBLOCK
Do not block when reading packets from input.
Definition: avformat.h:1048
const char * s
Definition: avisynth_c.h:668
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:686
int size
Definition: avcodec.h:1064
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:1860
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1342
#define av_cold
Definition: avcodec.h:653
Format I/O context.
Definition: avformat.h:968
static uint8_t * res
Definition: ffhash.c:43
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:347
static av_cold int write_header(AVFormatContext *s1)
Definition: v4l2enc.c:28
#define av_err2str(errnum)
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:317
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
void * priv_data
Format private data.
Definition: avformat.h:988
char filename[1024]
input or output filename
Definition: avformat.h:1018
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
AVOutputFormat ff_v4l2_muxer
Definition: v4l2enc.c:100
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:1015
int width
picture width / height.
Definition: avcodec.h:1314
AVStream ** streams
Definition: avformat.h:1016
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:93
const char * name
Definition: avformat.h:395
int fd
Definition: v4l2enc.c:25
enum AVMediaType codec_type
Definition: avcodec.h:1154
uint32_t avpriv_fmt_ff2v4l(enum AVPixelFormat pix_fmt, enum AVCodecID codec_id)
Definition: v4l2-common.c:55
enum AVCodecID codec_id
Definition: avcodec.h:1157
main external API structure.
Definition: avcodec.h:1146
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:538
Main libavdevice API header.
uint8_t * data
Definition: avcodec.h:1063
#define s1
Definition: regdef.h:38
static int flags
Definition: cpu.c:45
#define AVERROR(e)
static AVPacket pkt
Definition: demuxing.c:52
This structure stores compressed data.
Definition: avcodec.h:1040
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:85