v4l2enc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #include "v4l2-common.h"
  21. #include "avdevice.h"
  22. typedef struct {
  23. AVClass *class;
  24. int fd;
  25. } V4L2Context;
  26. static av_cold int write_header(AVFormatContext *s1)
  27. {
  28. int res = 0, flags = O_RDWR;
  29. struct v4l2_format fmt = {
  30. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT
  31. };
  32. V4L2Context *s = s1->priv_data;
  33. AVCodecContext *enc_ctx;
  34. uint32_t v4l2_pixfmt;
  35. if (s1->flags & AVFMT_FLAG_NONBLOCK)
  36. flags |= O_NONBLOCK;
  37. s->fd = open(s1->filename, flags);
  38. if (s->fd < 0) {
  39. res = AVERROR(errno);
  40. av_log(s1, AV_LOG_ERROR, "Unable to open V4L2 device '%s'\n", s1->filename);
  41. return res;
  42. }
  43. if (s1->nb_streams != 1 ||
  44. s1->streams[0]->codec->codec_type != AVMEDIA_TYPE_VIDEO ||
  45. s1->streams[0]->codec->codec_id != AV_CODEC_ID_RAWVIDEO) {
  46. av_log(s1, AV_LOG_ERROR,
  47. "V4L2 output device supports only a single raw video stream\n");
  48. return AVERROR(EINVAL);
  49. }
  50. enc_ctx = s1->streams[0]->codec;
  51. v4l2_pixfmt = avpriv_fmt_ff2v4l(enc_ctx->pix_fmt, AV_CODEC_ID_RAWVIDEO);
  52. if (!v4l2_pixfmt) { // XXX: try to force them one by one?
  53. av_log(s1, AV_LOG_ERROR, "Unknown V4L2 pixel format equivalent for %s\n",
  54. av_get_pix_fmt_name(enc_ctx->pix_fmt));
  55. return AVERROR(EINVAL);
  56. }
  57. if (ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
  58. res = AVERROR(errno);
  59. av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n", av_err2str(res));
  60. return res;
  61. }
  62. fmt.fmt.pix.width = enc_ctx->width;
  63. fmt.fmt.pix.height = enc_ctx->height;
  64. fmt.fmt.pix.pixelformat = v4l2_pixfmt;
  65. fmt.fmt.pix.sizeimage = av_image_get_buffer_size(enc_ctx->pix_fmt, enc_ctx->width, enc_ctx->height, 1);
  66. if (ioctl(s->fd, VIDIOC_S_FMT, &fmt) < 0) {
  67. res = AVERROR(errno);
  68. av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_S_FMT): %s\n", av_err2str(res));
  69. return res;
  70. }
  71. return res;
  72. }
  73. static int write_packet(AVFormatContext *s1, AVPacket *pkt)
  74. {
  75. const V4L2Context *s = s1->priv_data;
  76. if (write(s->fd, pkt->data, pkt->size) == -1)
  77. return AVERROR(errno);
  78. return 0;
  79. }
  80. static int write_trailer(AVFormatContext *s1)
  81. {
  82. const V4L2Context *s = s1->priv_data;
  83. close(s->fd);
  84. return 0;
  85. }
  86. AVOutputFormat ff_v4l2_muxer = {
  87. .name = "v4l2",
  88. .long_name = NULL_IF_CONFIG_SMALL("Video4Linux2 output device"),
  89. .priv_data_size = sizeof(V4L2Context),
  90. .audio_codec = AV_CODEC_ID_NONE,
  91. .video_codec = AV_CODEC_ID_RAWVIDEO,
  92. .write_header = write_header,
  93. .write_packet = write_packet,
  94. .write_trailer = write_trailer,
  95. .flags = AVFMT_NOFILE,
  96. };