You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
7.5KB

  1. /*
  2. * Copyright (c) 2013 Lukasz Marek
  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 <unistd.h>
  21. #include <fcntl.h>
  22. #include <sys/ioctl.h>
  23. #include <sys/mman.h>
  24. #include <linux/fb.h>
  25. #include "libavutil/pixdesc.h"
  26. #include "libavutil/log.h"
  27. #include "libavutil/mem.h"
  28. #include "libavutil/opt.h"
  29. #include "libavformat/avformat.h"
  30. #include "fbdev_common.h"
  31. typedef struct {
  32. AVClass *class; ///< class for private options
  33. int xoffset; ///< x coordinate of top left corner
  34. int yoffset; ///< y coordinate of top left corner
  35. struct fb_var_screeninfo varinfo; ///< framebuffer variable info
  36. struct fb_fix_screeninfo fixinfo; ///< framebuffer fixed info
  37. int fd; ///< framebuffer device file descriptor
  38. int index; ///< index of a video stream
  39. uint8_t *data; ///< framebuffer data
  40. } FBDevContext;
  41. static av_cold int fbdev_write_header(AVFormatContext *h)
  42. {
  43. FBDevContext *fbdev = h->priv_data;
  44. enum AVPixelFormat pix_fmt;
  45. AVStream *st = NULL;
  46. int ret, flags = O_RDWR;
  47. int i;
  48. for (i = 0; i < h->nb_streams; i++) {
  49. if (h->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  50. if (!st) {
  51. fbdev->index = i;
  52. st = h->streams[i];
  53. } else {
  54. av_log(h, AV_LOG_WARNING, "More than one video stream found. First one is used.\n");
  55. break;
  56. }
  57. }
  58. }
  59. if (!st) {
  60. av_log(h, AV_LOG_ERROR, "No video stream found.\n");
  61. return AVERROR(EINVAL);
  62. }
  63. if ((fbdev->fd = avpriv_open(h->filename, flags)) == -1) {
  64. ret = AVERROR(errno);
  65. av_log(h, AV_LOG_ERROR,
  66. "Could not open framebuffer device '%s': %s\n",
  67. h->filename, av_err2str(ret));
  68. return ret;
  69. }
  70. if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0) {
  71. ret = AVERROR(errno);
  72. av_log(h, AV_LOG_ERROR, "FBIOGET_VSCREENINFO: %s\n", av_err2str(ret));
  73. goto fail;
  74. }
  75. if (ioctl(fbdev->fd, FBIOGET_FSCREENINFO, &fbdev->fixinfo) < 0) {
  76. ret = AVERROR(errno);
  77. av_log(h, AV_LOG_ERROR, "FBIOGET_FSCREENINFO: %s\n", av_err2str(ret));
  78. goto fail;
  79. }
  80. pix_fmt = ff_get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
  81. if (pix_fmt == AV_PIX_FMT_NONE) {
  82. ret = AVERROR(EINVAL);
  83. av_log(h, AV_LOG_ERROR, "Framebuffer pixel format not supported.\n");
  84. goto fail;
  85. }
  86. fbdev->data = mmap(NULL, fbdev->fixinfo.smem_len, PROT_WRITE, MAP_SHARED, fbdev->fd, 0);
  87. if (fbdev->data == MAP_FAILED) {
  88. ret = AVERROR(errno);
  89. av_log(h, AV_LOG_ERROR, "Error in mmap(): %s\n", av_err2str(ret));
  90. goto fail;
  91. }
  92. return 0;
  93. fail:
  94. close(fbdev->fd);
  95. return ret;
  96. }
  97. static int fbdev_write_packet(AVFormatContext *h, AVPacket *pkt)
  98. {
  99. FBDevContext *fbdev = h->priv_data;
  100. uint8_t *pin, *pout;
  101. enum AVPixelFormat fb_pix_fmt;
  102. int disp_height;
  103. int bytes_to_copy;
  104. AVCodecContext *codec_ctx = h->streams[fbdev->index]->codec;
  105. enum AVPixelFormat video_pix_fmt = codec_ctx->pix_fmt;
  106. int video_width = codec_ctx->width;
  107. int video_height = codec_ctx->height;
  108. int bytes_per_pixel = ((codec_ctx->bits_per_coded_sample + 7) >> 3);
  109. int src_line_size = video_width * bytes_per_pixel;
  110. int i;
  111. if (fbdev->index != pkt->stream_index)
  112. return 0;
  113. if (ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->varinfo) < 0)
  114. av_log(h, AV_LOG_WARNING,
  115. "Error refreshing variable info: %s\n", av_err2str(AVERROR(errno)));
  116. fb_pix_fmt = ff_get_pixfmt_from_fb_varinfo(&fbdev->varinfo);
  117. if (fb_pix_fmt != video_pix_fmt) {
  118. av_log(h, AV_LOG_ERROR, "Pixel format %s is not supported, use %s\n",
  119. av_get_pix_fmt_name(video_pix_fmt), av_get_pix_fmt_name(fb_pix_fmt));
  120. return AVERROR(EINVAL);
  121. }
  122. disp_height = FFMIN(fbdev->varinfo.yres, video_height);
  123. bytes_to_copy = FFMIN(fbdev->varinfo.xres, video_width) * bytes_per_pixel;
  124. pin = pkt->data;
  125. pout = fbdev->data +
  126. bytes_per_pixel * fbdev->varinfo.xoffset +
  127. fbdev->varinfo.yoffset * fbdev->fixinfo.line_length;
  128. if (fbdev->xoffset) {
  129. if (fbdev->xoffset < 0) {
  130. if (-fbdev->xoffset >= video_width) //nothing to display
  131. return 0;
  132. bytes_to_copy += fbdev->xoffset * bytes_per_pixel;
  133. pin -= fbdev->xoffset * bytes_per_pixel;
  134. } else {
  135. int diff = (video_width + fbdev->xoffset) - fbdev->varinfo.xres;
  136. if (diff > 0) {
  137. if (diff >= video_width) //nothing to display
  138. return 0;
  139. bytes_to_copy -= diff * bytes_per_pixel;
  140. }
  141. pout += bytes_per_pixel * fbdev->xoffset;
  142. }
  143. }
  144. if (fbdev->yoffset) {
  145. if (fbdev->yoffset < 0) {
  146. if (-fbdev->yoffset >= video_height) //nothing to display
  147. return 0;
  148. disp_height += fbdev->yoffset;
  149. pin -= fbdev->yoffset * src_line_size;
  150. } else {
  151. int diff = (video_height + fbdev->yoffset) - fbdev->varinfo.yres;
  152. if (diff > 0) {
  153. if (diff >= video_height) //nothing to display
  154. return 0;
  155. disp_height -= diff;
  156. }
  157. pout += fbdev->yoffset * fbdev->fixinfo.line_length;
  158. }
  159. }
  160. for (i = 0; i < disp_height; i++) {
  161. memcpy(pout, pin, bytes_to_copy);
  162. pout += fbdev->fixinfo.line_length;
  163. pin += src_line_size;
  164. }
  165. return 0;
  166. }
  167. static av_cold int fbdev_write_trailer(AVFormatContext *h)
  168. {
  169. FBDevContext *fbdev = h->priv_data;
  170. munmap(fbdev->data, fbdev->fixinfo.smem_len);
  171. close(fbdev->fd);
  172. return 0;
  173. }
  174. #define OFFSET(x) offsetof(FBDevContext, x)
  175. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  176. static const AVOption options[] = {
  177. { "xoffset", "set x coordinate of top left corner", OFFSET(xoffset), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, ENC },
  178. { "yoffset", "set y coordinate of top left corner", OFFSET(yoffset), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, ENC },
  179. { NULL }
  180. };
  181. static const AVClass fbdev_class = {
  182. .class_name = "fbdev outdev",
  183. .item_name = av_default_item_name,
  184. .option = options,
  185. .version = LIBAVUTIL_VERSION_INT,
  186. };
  187. AVOutputFormat ff_fbdev_muxer = {
  188. .name = "fbdev",
  189. .long_name = NULL_IF_CONFIG_SMALL("Linux framebuffer"),
  190. .priv_data_size = sizeof(FBDevContext),
  191. .audio_codec = AV_CODEC_ID_NONE,
  192. .video_codec = AV_CODEC_ID_RAWVIDEO,
  193. .write_header = fbdev_write_header,
  194. .write_packet = fbdev_write_packet,
  195. .write_trailer = fbdev_write_trailer,
  196. .flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS,
  197. .priv_class = &fbdev_class,
  198. };