FFmpeg
fbdev_common.c
Go to the documentation of this file.
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 
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <sys/ioctl.h>
26 #include <stdlib.h>
27 #include "fbdev_common.h"
28 #include "libavutil/common.h"
29 #include "libavutil/file_open.h"
30 #include "avdevice.h"
31 
36 };
37 
38 static const struct rgb_pixfmt_map_entry rgb_pixfmt_map[] = {
39  // bpp, red_offset, green_offset, blue_offset, alpha_offset, pixfmt
40  { 32, 0, 8, 16, 24, AV_PIX_FMT_RGBA },
41  { 32, 16, 8, 0, 24, AV_PIX_FMT_BGRA },
42  { 32, 8, 16, 24, 0, AV_PIX_FMT_ARGB },
43  { 32, 3, 2, 8, 0, AV_PIX_FMT_ABGR },
44  { 24, 0, 8, 16, 0, AV_PIX_FMT_RGB24 },
45  { 24, 16, 8, 0, 0, AV_PIX_FMT_BGR24 },
46  { 16, 11, 5, 0, 0, AV_PIX_FMT_RGB565 },
47 };
48 
49 enum AVPixelFormat ff_get_pixfmt_from_fb_varinfo(struct fb_var_screeninfo *varinfo)
50 {
51  int i;
52 
53  for (i = 0; i < FF_ARRAY_ELEMS(rgb_pixfmt_map); i++) {
54  const struct rgb_pixfmt_map_entry *entry = &rgb_pixfmt_map[i];
55  if (entry->bits_per_pixel == varinfo->bits_per_pixel &&
56  entry->red_offset == varinfo->red.offset &&
57  entry->green_offset == varinfo->green.offset &&
58  entry->blue_offset == varinfo->blue.offset)
59  return entry->pixfmt;
60  }
61 
62  return AV_PIX_FMT_NONE;
63 }
64 
65 const char *ff_fbdev_default_device(void)
66 {
67  const char *dev = getenv("FRAMEBUFFER");
68  if (!dev)
69  dev = "/dev/fb0";
70  return dev;
71 }
72 
74 {
75  struct fb_var_screeninfo varinfo;
76  struct fb_fix_screeninfo fixinfo;
77  char device_file[12];
78  AVDeviceInfo *device = NULL;
79  int i, fd, ret = 0;
80  const char *default_device = ff_fbdev_default_device();
81 
82  if (!device_list)
83  return AVERROR(EINVAL);
84 
85  for (i = 0; i <= 31; i++) {
86  snprintf(device_file, sizeof(device_file), "/dev/fb%d", i);
87 
88  if ((fd = avpriv_open(device_file, O_RDWR)) < 0) {
89  int err = AVERROR(errno);
90  if (err != AVERROR(ENOENT))
91  av_log(NULL, AV_LOG_ERROR, "Could not open framebuffer device '%s': %s\n",
92  device_file, av_err2str(err));
93  continue;
94  }
95  if (ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) == -1)
96  goto fail_device;
97  if (ioctl(fd, FBIOGET_FSCREENINFO, &fixinfo) == -1)
98  goto fail_device;
99 
100  device = av_mallocz(sizeof(AVDeviceInfo));
101  if (!device) {
102  ret = AVERROR(ENOMEM);
103  goto fail_device;
104  }
105  device->device_name = av_strdup(device_file);
106  device->device_description = av_strdup(fixinfo.id);
107  if (!device->device_name || !device->device_description) {
108  ret = AVERROR(ENOMEM);
109  goto fail_device;
110  }
111 
112  if ((ret = av_dynarray_add_nofree(&device_list->devices,
113  &device_list->nb_devices, device)) < 0)
114  goto fail_device;
115 
116  if (default_device && !strcmp(device->device_name, default_device)) {
117  device_list->default_device = device_list->nb_devices - 1;
118  default_device = NULL;
119  }
120  close(fd);
121  continue;
122 
123  fail_device:
124  if (device) {
125  av_freep(&device->device_name);
126  av_freep(&device->device_description);
127  av_freep(&device);
128  }
129  if (fd >= 0)
130  close(fd);
131  if (ret < 0)
132  return ret;
133  }
134  return 0;
135 }
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVDeviceInfo::device_name
char * device_name
device name, format depends on device
Definition: avdevice.h:334
AVDeviceInfoList::nb_devices
int nb_devices
number of autodetected devices
Definition: avdevice.h:345
rgb_pixfmt_map
static const struct rgb_pixfmt_map_entry rgb_pixfmt_map[]
Definition: fbdev_common.c:38
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:76
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:102
rgb_pixfmt_map_entry::alpha_offset
int alpha_offset
Definition: fbdev_common.c:34
AVDeviceInfoList::devices
AVDeviceInfo ** devices
list of autodetected devices
Definition: avdevice.h:344
ff_get_pixfmt_from_fb_varinfo
enum AVPixelFormat ff_get_pixfmt_from_fb_varinfo(struct fb_var_screeninfo *varinfo)
Definition: fbdev_common.c:49
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
ff_fbdev_get_device_list
int ff_fbdev_get_device_list(AVDeviceInfoList *device_list)
Definition: fbdev_common.c:73
avpriv_open
int avpriv_open(const char *filename, int flags,...)
A wrapper for open() setting O_CLOEXEC.
Definition: file_open.c:67
file_open.h
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:100
rgb_pixfmt_map_entry::red_offset
int red_offset
Definition: fbdev_common.c:34
NULL
#define NULL
Definition: coverity.c:32
rgb_pixfmt_map_entry::pixfmt
enum AVPixelFormat pixfmt
Definition: fbdev_common.c:35
AV_PIX_FMT_ABGR
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:101
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:75
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:121
ff_fbdev_default_device
const char * ff_fbdev_default_device(void)
Definition: fbdev_common.c:65
AVDeviceInfo
Structure describes basic parameters of the device.
Definition: avdevice.h:333
avdevice.h
fbdev_common.h
AVDeviceInfo::device_description
char * device_description
human friendly name
Definition: avdevice.h:335
AV_PIX_FMT_ARGB
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:99
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
common.h
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:254
rgb_pixfmt_map_entry::green_offset
int green_offset
Definition: fbdev_common.c:34
AV_PIX_FMT_RGB565
#define AV_PIX_FMT_RGB565
Definition: pixfmt.h:465
ret
ret
Definition: filter_design.txt:187
rgb_pixfmt_map_entry::bits_per_pixel
int bits_per_pixel
Definition: fbdev_common.c:33
AVDeviceInfoList
List of devices.
Definition: avdevice.h:343
av_dynarray_add_nofree
int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
Add an element to a dynamic array.
Definition: mem.c:313
AVDeviceInfoList::default_device
int default_device
index of default device or -1 if no default
Definition: avdevice.h:346
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:270
rgb_pixfmt_map_entry
Definition: fbdev_common.c:32
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
rgb_pixfmt_map_entry::blue_offset
int blue_offset
Definition: fbdev_common.c:34
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
snprintf
#define snprintf
Definition: snprintf.h:34