FFmpeg
Loading...
Searching...
No Matches
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 "libavutil/mem.h"
31#include "avdevice.h"
32
38
39static const struct rgb_pixfmt_map_entry rgb_pixfmt_map[] = {
40 // bpp, red_offset, green_offset, blue_offset, alpha_offset, pixfmt
41 { 32, 0, 8, 16, 24, AV_PIX_FMT_RGBA },
42 { 32, 16, 8, 0, 24, AV_PIX_FMT_BGRA },
43 { 32, 8, 16, 24, 0, AV_PIX_FMT_ARGB },
44 { 32, 3, 2, 8, 0, AV_PIX_FMT_ABGR },
45 { 24, 0, 8, 16, 0, AV_PIX_FMT_RGB24 },
46 { 24, 16, 8, 0, 0, AV_PIX_FMT_BGR24 },
47 { 16, 11, 5, 0, 0, AV_PIX_FMT_RGB565 },
48};
49
50enum AVPixelFormat ff_get_pixfmt_from_fb_varinfo(struct fb_var_screeninfo *varinfo)
51{
52 int i;
53
54 for (i = 0; i < FF_ARRAY_ELEMS(rgb_pixfmt_map); i++) {
56 if (entry->bits_per_pixel == varinfo->bits_per_pixel &&
57 entry->red_offset == varinfo->red.offset &&
58 entry->green_offset == varinfo->green.offset &&
59 entry->blue_offset == varinfo->blue.offset)
60 return entry->pixfmt;
61 }
62
63 return AV_PIX_FMT_NONE;
64}
65
66const char *ff_fbdev_default_device(void)
67{
68 const char *dev = getenv("FRAMEBUFFER");
69 if (!dev)
70 dev = "/dev/fb0";
71 return dev;
72}
73
75{
76 struct fb_var_screeninfo varinfo;
77 struct fb_fix_screeninfo fixinfo;
78 char device_file[12];
79 AVDeviceInfo *device = NULL;
80 int i, fd, ret = 0;
81 const char *default_device = ff_fbdev_default_device();
82
83 if (!device_list)
84 return AVERROR(EINVAL);
85
86 for (i = 0; i <= 31; i++) {
87 snprintf(device_file, sizeof(device_file), "/dev/fb%d", i);
88
89 if ((fd = avpriv_open(device_file, O_RDWR)) < 0) {
90 int err = AVERROR(errno);
91 if (err != AVERROR(ENOENT))
92 av_log(NULL, AV_LOG_ERROR, "Could not open framebuffer device '%s': %s\n",
93 device_file, av_err2str(err));
94 continue;
95 }
96 if (ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) == -1)
97 goto fail_device;
98 if (ioctl(fd, FBIOGET_FSCREENINFO, &fixinfo) == -1)
99 goto fail_device;
100
101 device = av_mallocz(sizeof(AVDeviceInfo));
102 if (!device) {
103 ret = AVERROR(ENOMEM);
104 goto fail_device;
105 }
106 device->device_name = av_strdup(device_file);
107 device->device_description = av_strdup(fixinfo.id);
108 if (!device->device_name || !device->device_description) {
109 ret = AVERROR(ENOMEM);
110 goto fail_device;
111 }
112
113 if ((ret = av_dynarray_add_nofree(&device_list->devices,
114 &device_list->nb_devices, device)) < 0)
115 goto fail_device;
116
117 if (default_device && !strcmp(device->device_name, default_device)) {
118 device_list->default_device = device_list->nb_devices - 1;
119 default_device = NULL;
120 }
121 close(fd);
122 continue;
123
124 fail_device:
125 if (device) {
126 av_freep(&device->device_name);
128 av_freep(&device);
129 }
130 if (fd >= 0)
131 close(fd);
132 if (ret < 0)
133 return ret;
134 }
135 return 0;
136}
#define entry
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
Main libavdevice API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
common internal and external API header
#define NULL
Definition coverity.c:32
static const struct rgb_pixfmt_map_entry rgb_pixfmt_map[]
const char * ff_fbdev_default_device(void)
enum AVPixelFormat ff_get_pixfmt_from_fb_varinfo(struct fb_var_screeninfo *varinfo)
int ff_fbdev_get_device_list(AVDeviceInfoList *device_list)
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
Add an element to a dynamic array.
Definition mem.c:313
int avpriv_open(const char *filename, int flags,...)
A wrapper for open() setting O_CLOEXEC.
Definition file_open.c:67
Memory handling functions.
#define av_strdup(s)
Definition ops_static.c:55
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition pixfmt.h:99
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition pixfmt.h:102
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition pixfmt.h:101
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition pixfmt.h:100
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition pixfmt.h:76
#define AV_PIX_FMT_RGB565
Definition pixfmt.h:532
#define FF_ARRAY_ELEMS(a)
#define snprintf
Definition snprintf.h:34
List of devices.
Definition avdevice.h:343
int nb_devices
number of autodetected devices
Definition avdevice.h:345
int default_device
index of default device or -1 if no default
Definition avdevice.h:346
AVDeviceInfo ** devices
list of autodetected devices
Definition avdevice.h:344
Structure describes basic parameters of the device.
Definition avdevice.h:333
char * device_description
human friendly name
Definition avdevice.h:335
char * device_name
device name, format depends on device
Definition avdevice.h:334
int green_offset
int bits_per_pixel
int red_offset
enum AVPixelFormat pixfmt
int alpha_offset
int blue_offset
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)