v4l2enc.c 3.8 KB

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