fbdev_common.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2011 Stefano Sabatini
  3. * Copyright (c) 2009 Giliard B. de Freitas <giliarde@gmail.com>
  4. * Copyright (C) 2002 Gunnar Monell <gmo@linux.nu>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <unistd.h>
  23. #include <fcntl.h>
  24. #include <sys/ioctl.h>
  25. #include <stdlib.h>
  26. #include "fbdev_common.h"
  27. #include "libavutil/common.h"
  28. #include "libavutil/file_open.h"
  29. #include "libavutil/mem.h"
  30. #include "avdevice.h"
  31. struct rgb_pixfmt_map_entry {
  32. int bits_per_pixel;
  33. int red_offset, green_offset, blue_offset, alpha_offset;
  34. enum AVPixelFormat pixfmt;
  35. };
  36. static const struct rgb_pixfmt_map_entry rgb_pixfmt_map[] = {
  37. // bpp, red_offset, green_offset, blue_offset, alpha_offset, pixfmt
  38. { 32, 0, 8, 16, 24, AV_PIX_FMT_RGBA },
  39. { 32, 16, 8, 0, 24, AV_PIX_FMT_BGRA },
  40. { 32, 8, 16, 24, 0, AV_PIX_FMT_ARGB },
  41. { 32, 3, 2, 8, 0, AV_PIX_FMT_ABGR },
  42. { 24, 0, 8, 16, 0, AV_PIX_FMT_RGB24 },
  43. { 24, 16, 8, 0, 0, AV_PIX_FMT_BGR24 },
  44. { 16, 11, 5, 0, 0, AV_PIX_FMT_RGB565 },
  45. };
  46. enum AVPixelFormat ff_get_pixfmt_from_fb_varinfo(struct fb_var_screeninfo *varinfo)
  47. {
  48. int i;
  49. for (i = 0; i < FF_ARRAY_ELEMS(rgb_pixfmt_map); i++) {
  50. const struct rgb_pixfmt_map_entry *entry = &rgb_pixfmt_map[i];
  51. if (entry->bits_per_pixel == varinfo->bits_per_pixel &&
  52. entry->red_offset == varinfo->red.offset &&
  53. entry->green_offset == varinfo->green.offset &&
  54. entry->blue_offset == varinfo->blue.offset)
  55. return entry->pixfmt;
  56. }
  57. return AV_PIX_FMT_NONE;
  58. }
  59. const char *ff_fbdev_default_device(void)
  60. {
  61. const char *dev = getenv("FRAMEBUFFER");
  62. if (!dev)
  63. dev = "/dev/fb0";
  64. return dev;
  65. }
  66. int ff_fbdev_get_device_list(AVDeviceInfoList *device_list)
  67. {
  68. struct fb_var_screeninfo varinfo;
  69. struct fb_fix_screeninfo fixinfo;
  70. char device_file[12];
  71. AVDeviceInfo *device = NULL;
  72. int i, fd, ret = 0;
  73. const char *default_device = ff_fbdev_default_device();
  74. if (!device_list)
  75. return AVERROR(EINVAL);
  76. for (i = 0; i <= 31; i++) {
  77. snprintf(device_file, sizeof(device_file), "/dev/fb%d", i);
  78. if ((fd = avpriv_open(device_file, O_RDWR)) < 0) {
  79. int err = AVERROR(errno);
  80. if (err != AVERROR(ENOENT))
  81. av_log(NULL, AV_LOG_ERROR, "Could not open framebuffer device '%s': %s\n",
  82. device_file, av_err2str(err));
  83. continue;
  84. }
  85. if (ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) == -1)
  86. goto fail_device;
  87. if (ioctl(fd, FBIOGET_FSCREENINFO, &fixinfo) == -1)
  88. goto fail_device;
  89. device = av_mallocz(sizeof(AVDeviceInfo));
  90. if (!device) {
  91. ret = AVERROR(ENOMEM);
  92. goto fail_device;
  93. }
  94. device->device_name = av_strdup(device_file);
  95. device->device_description = av_strdup(fixinfo.id);
  96. if (!device->device_name || !device->device_description) {
  97. ret = AVERROR(ENOMEM);
  98. goto fail_device;
  99. }
  100. if ((ret = av_dynarray_add_nofree(&device_list->devices,
  101. &device_list->nb_devices, device)) < 0)
  102. goto fail_device;
  103. if (default_device && !strcmp(device->device_name, default_device)) {
  104. device_list->default_device = device_list->nb_devices - 1;
  105. default_device = NULL;
  106. }
  107. close(fd);
  108. continue;
  109. fail_device:
  110. if (device) {
  111. av_freep(&device->device_name);
  112. av_freep(&device->device_description);
  113. av_freep(&device);
  114. }
  115. if (fd >= 0)
  116. close(fd);
  117. if (ret < 0)
  118. return ret;
  119. }
  120. return 0;
  121. }