gstreamer/sys/fbdev/gstfbdevsink.c
Peter Seiderer df4ef4be39 fbdevsink: fix bytes per pixel calculation
Simple pipeline

	$ gst-launch-1.0 videotestsrc ! fbdevsink

crashes with SIGSEGV in case the frambuffer xres is smaller
than the virtual xres resolution, e.g.:

	$ fbset

  mode "800x480-0"
          # D: 0.000 MHz, H: 0.000 kHz, V: 0.000 Hz
          geometry 800 480 1920 1200 16
          timings 0 0 0 0 0 0 0
          accel true
          rgba 5/11,6/5,5/0,0/0
  endmode

Debug:
	$ gdb gst-launch-1.0
  (gdb) run videotestsrc ! fbdevsink
  (gdb) where
  #0  0xb6bd2d24 in __memcpy_neon ()
      at ../sysdeps/arm/armv7/multiarch/memcpy_impl.S:591
  #1  0xb69b04e8 in gst_fbdevsink_show_frame (videosink=0x10a3378,
      buf=0xb5c08838) at gstfbdevsink.c:269
  #2  0xb69e88c4 in gst_base_sink_do_preroll (sink=sink@entry=0x10a3378,
      obj=0xb5c08838, obj@entry=0xa0) at gstbasesink.c:2281
  #3  0xb69e92bc in gst_base_sink_do_sync (basesink=basesink@entry=0x10a3378,
      obj=0xa0, obj@entry=0xb5c08838, late=0x0, late@entry=0xb6548ba0,
      step_end=0x140, step_end@entry=0xb6548ba4) at gstbasesink.c:2500
  #4  0xb69ea67c in gst_base_sink_chain_unlocked (
      basesink=basesink@entry=0x10a3378, obj=0x0, obj@entry=0xb5c08838,
      is_list=is_list@entry=0, pad=<optimized out>) at gstbasesink.c:3486
  #5  0xb69ec1c0 in gst_base_sink_chain_main (basesink=0x10a3378,
      pad=<optimized out>, obj=0xb5c08838, is_list=0) at gstbasesink.c:3647
  #6  0xb6eb5b10 in gst_pad_chain_data_unchecked (pad=0x10a6170,
      type=<optimized out>, data=0xb5c08838) at gstpad.c:4086
  #7  0xb6eb7a34 in gst_pad_push_data (pad=pad@entry=0x10a6020,
      type=type@entry=4112, data=0xb5c08838) at gstpad.c:4338
  #8  0xb6ebf344 in gst_pad_push (pad=pad@entry=0x10a6020,
      buffer=<optimized out>) at gstpad.c:4454
  #9  0xb69f22f0 in gst_base_src_loop (pad=0x10a6020) at gstbasesrc.c:2845
  #10 0xb6eeddfc in gst_task_func (task=0x10a8828) at gsttask.c:331
  #11 0xb6d485a0 in g_thread_pool_thread_proxy (data=<optimized out>)
   at gthreadpoQuit
  (gdb) frame 1
  #1  0xb69b04e8 in gst_fbdevsink_show_frame (videosink=0x10a3378,
      buf=0xb5c08838) at gstfbdevsink.c:269
  269     gstfbdevsink.c: No such file or directory.
  (gdb) p fbdevsink
  $1 = (GstFBDEVSink *) 0x10a3378
  (gdb) p *fbdevsink
  $2 = {videosink = {element = {element = {object = {object = {
              g_type_instance = {g_class = 0x10a2d60}, ref_count = 3,
              qdata = 0x0}, lock = {p = 0x0, i = {0, 0}},
            name = 0x10a2f30 "fbdevsink0", parent = 0x10a70a0, flags = 32,
            control_bindings = 0x0, control_rate = 100000000,
            last_sync = 18446744073709551615, _gst_reserved = 0x0},
          state_lock = {p = 0x109f9a8, i = {0, 0}}, state_cond = {p = 0x0, i = {
              3, 0}}, state_cookie = 2, target_state = GST_STATE_PAUSED,
          current_state = GST_STATE_READY, next_state = GST_STATE_PAUSED,
          pending_state = GST_STATE_PAUSED,
          last_return = GST_STATE_CHANGE_ASYNC, bus = 0x108bcb8, clock = 0x0,
          base_time = 0, start_time = 0, numpads = 1, pads = 0x109cc20,
          numsrcpads = 0, srcpads = 0x0, numsinkpads = 1, sinkpads = 0x109cc30,
          pads_cookie = 1, _gst_reserved = {0x0, 0x0, 0x0, 0x0}},
        sinkpad = 0x10a6170, pad_mode = GST_PAD_MODE_PUSH, offset = 0,
        can_activate_pull = 0, can_activate_push = 1, preroll_lock = {p = 0x1,
          i = {1, 0}}, preroll_cond = {p = 0x0, i = {0, 0}}, eos = 0,
        need_preroll = 1, have_preroll = 0, playing_async = 1,
        have_newsegment = 1, segment = {flags = GST_SEGMENT_FLAG_NONE, rate = 1,
          applied_rate = 1, format = GST_FORMAT_TIME, base = 0, offset = 0,
          start = 0, stop = 18446744073709551615, time = 0, position = 33333333,
          duration = 18446744073709551615, _gst_reserved = {0x0, 0x0, 0x0,
            0x0}}, clock_id = 0x0, sync = 1, flushing = 0, running = 0,
        max_lateness = 20000000, priv = 0x10a3188, _gst_reserved = {
          0x0 <repeats 20 times>}}, width = 0, height = 0, priv = 0x10a3180,
      _gst_reserved = {0x0, 0x0, 0x0, 0x0}}, fixinfo = {
      id = '\000' <repeats 15 times>, smem_start = 1078984704,
      smem_len = 4608000, type = 0, type_aux = 0, visual = 2, xpanstep = 1,
      ypanstep = 1, ywrapstep = 0, line_length = 3840, mmio_start = 0,
      mmio_len = 0, accel = 0, capabilities = 0, reserved = {0, 0}}, varinfo = {
    xres = 800, yres = 480, xres_virtual = 1920, yres_virtual = 1200,
    xoffset = 0, yoffset = 0, bits_per_pixel = 16, grayscale = 0, red = {
      offset = 11, length = 5, msb_right = 0}, green = {offset = 5,
      length = 6, msb_right = 0}, blue = {offset = 0, length = 5,
      msb_right = 0}, transp = {offset = 0, length = 0, msb_right = 0},
      nonstd = 0, activate = 0, height = 4294967295, width = 4294967295,
      accel_flags = 1, pixclock = 0, left_margin = 0, right_margin = 0,
      upper_margin = 0, lower_margin = 0, hsync_len = 0, vsync_len = 0,
      sync = 0, vmode = 0, rotate = 0, colorspace = 0, reserved = {0, 0, 0, 0}},
    fd = 5,
    framebuffer = 0xb654a000 <error: Cannot access memory at address 0xb654a000>, device = 0x10a38d8 "/dev/fb0", width = 320, height = 240, cx = 240, cy = 120,
    linelen = 1280, lines = 240, bytespp = 4, fps_n = 30, fps_d = 1}
  (gdb) p map
  $3 = {memory = 0xb5d24008, flags = GST_MAP_READ,
    data = 0xb5d24058 '\377' <repeats 90 times>, "\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\340\377\377\a\377\a\377\a\377\a\377\a\377\a\377\a\377\a\377\a"...,
    size = 153600, maxsize = 153603, user_data = {0x0,
      0xb69e3ba4 <gst_base_sink_set_last_buffer_unlocked+92>, 0x10a3378, 0x0},
    _gst_reserved = {0x1, 0x10a3378, 0xb6f50dd8 <_gst_debug_min>, 0xb5c08838}}
  (gdb) p i
  $4 = 121

Fix this by changing the fbdevsink->bytespp calculation using
the frame buffer xres_virtual size instead of xres.

https://bugzilla.gnome.org/show_bug.cgi?id=762482
2016-02-23 11:36:57 +02:00

453 lines
12 KiB
C

/* GStreamer fbdev plugin
* Copyright (C) 2007 Sean D'Epagnier <sean@depagnier.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/* currently the driver does not switch modes, instead uses current mode.
the video is centered and cropped if needed to fit onscreen.
Whatever bitdepth is set is used, and tested to work for 16, 24, 32 bits
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <signal.h>
#include <string.h>
#include <sys/time.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
#include "_stdint.h"
#include "gstfbdevsink.h"
enum
{
ARG_0,
ARG_DEVICE
};
#if 0
static void gst_fbdevsink_get_times (GstBaseSink * basesink,
GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
#endif
static GstFlowReturn gst_fbdevsink_show_frame (GstVideoSink * videosink,
GstBuffer * buff);
static gboolean gst_fbdevsink_start (GstBaseSink * bsink);
static gboolean gst_fbdevsink_stop (GstBaseSink * bsink);
static GstCaps *gst_fbdevsink_getcaps (GstBaseSink * bsink, GstCaps * filter);
static gboolean gst_fbdevsink_setcaps (GstBaseSink * bsink, GstCaps * caps);
static void gst_fbdevsink_finalize (GObject * object);
static void gst_fbdevsink_set_property (GObject * object,
guint prop_id, const GValue * value, GParamSpec * pspec);
static void gst_fbdevsink_get_property (GObject * object,
guint prop_id, GValue * value, GParamSpec * pspec);
static GstStateChangeReturn gst_fbdevsink_change_state (GstElement * element,
GstStateChange transition);
#define VIDEO_CAPS "{ RGB, BGR, BGRx, xBGR, RGB, RGBx, xRGB, RGB15, RGB16 }"
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (VIDEO_CAPS))
);
#define parent_class gst_fbdevsink_parent_class
G_DEFINE_TYPE (GstFBDEVSink, gst_fbdevsink, GST_TYPE_VIDEO_SINK);
static void
gst_fbdevsink_init (GstFBDEVSink * fbdevsink)
{
/* nothing to do here yet */
}
#if 0
static void
gst_fbdevsink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
GstClockTime * start, GstClockTime * end)
{
GstFBDEVSink *fbdevsink;
fbdevsink = GST_FBDEVSINK (basesink);
if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
*start = GST_BUFFER_TIMESTAMP (buffer);
if (GST_BUFFER_DURATION_IS_VALID (buffer)) {
*end = *start + GST_BUFFER_DURATION (buffer);
} else {
if (fbdevsink->fps_n > 0) {
*end = *start +
gst_util_uint64_scale_int (GST_SECOND, fbdevsink->fps_d,
fbdevsink->fps_n);
}
}
}
}
#endif
static GstCaps *
gst_fbdevsink_getcaps (GstBaseSink * bsink, GstCaps * filter)
{
GstFBDEVSink *fbdevsink;
GstVideoFormat format;
GstCaps *caps;
uint32_t rmask;
uint32_t gmask;
uint32_t bmask;
uint32_t tmask;
int endianness, depth, bpp;
fbdevsink = GST_FBDEVSINK (bsink);
caps = gst_static_pad_template_get_caps (&sink_template);
/* FIXME: locking */
if (!fbdevsink->framebuffer)
goto done;
bpp = fbdevsink->varinfo.bits_per_pixel;
rmask = ((1 << fbdevsink->varinfo.red.length) - 1)
<< fbdevsink->varinfo.red.offset;
gmask = ((1 << fbdevsink->varinfo.green.length) - 1)
<< fbdevsink->varinfo.green.offset;
bmask = ((1 << fbdevsink->varinfo.blue.length) - 1)
<< fbdevsink->varinfo.blue.offset;
tmask = ((1 << fbdevsink->varinfo.transp.length) - 1)
<< fbdevsink->varinfo.transp.offset;
depth = fbdevsink->varinfo.red.length + fbdevsink->varinfo.green.length
+ fbdevsink->varinfo.blue.length;
switch (fbdevsink->varinfo.bits_per_pixel) {
case 32:
/* swap endianness of masks */
rmask = GUINT32_SWAP_LE_BE (rmask);
gmask = GUINT32_SWAP_LE_BE (gmask);
bmask = GUINT32_SWAP_LE_BE (bmask);
tmask = GUINT32_SWAP_LE_BE (tmask);
depth += fbdevsink->varinfo.transp.length;
endianness = G_BIG_ENDIAN;
break;
case 24:{
/* swap red and blue masks */
tmask = rmask;
rmask = bmask;
bmask = tmask;
tmask = 0;
endianness = G_BIG_ENDIAN;
break;
}
case 15:
case 16:
tmask = 0;
endianness = G_LITTLE_ENDIAN;
break;
default:
goto unsupported_bpp;
}
format = gst_video_format_from_masks (depth, bpp, endianness, rmask, gmask,
bmask, tmask);
if (format == GST_VIDEO_FORMAT_UNKNOWN)
goto unknown_format;
caps = gst_caps_make_writable (caps);
gst_caps_set_simple (caps, "format", G_TYPE_STRING,
gst_video_format_to_string (format), NULL);
done:
if (filter != NULL) {
GstCaps *icaps;
icaps = gst_caps_intersect (caps, filter);
gst_caps_unref (caps);
caps = icaps;
}
return caps;
/* ERRORS */
unsupported_bpp:
{
GST_WARNING_OBJECT (bsink, "unsupported bit depth: %d", bpp);
return NULL;
}
unknown_format:
{
GST_WARNING_OBJECT (bsink, "could not map fbdev format to GstVideoFormat: "
"depth=%u, bpp=%u, endianness=%u, rmask=0x%08x, gmask=0x%08x, "
"bmask=0x%08x, tmask=0x%08x", depth, bpp, endianness, rmask, gmask,
bmask, tmask);
return NULL;
}
}
static gboolean
gst_fbdevsink_setcaps (GstBaseSink * bsink, GstCaps * vscapslist)
{
GstFBDEVSink *fbdevsink;
GstStructure *structure;
const GValue *fps;
fbdevsink = GST_FBDEVSINK (bsink);
structure = gst_caps_get_structure (vscapslist, 0);
fps = gst_structure_get_value (structure, "framerate");
fbdevsink->fps_n = gst_value_get_fraction_numerator (fps);
fbdevsink->fps_d = gst_value_get_fraction_denominator (fps);
gst_structure_get_int (structure, "width", &fbdevsink->width);
gst_structure_get_int (structure, "height", &fbdevsink->height);
/* calculate centering and scanlengths for the video */
fbdevsink->bytespp = fbdevsink->fixinfo.line_length / fbdevsink->varinfo.xres_virtual;
fbdevsink->cx = ((int) fbdevsink->varinfo.xres - fbdevsink->width) / 2;
if (fbdevsink->cx < 0)
fbdevsink->cx = 0;
fbdevsink->cy = ((int) fbdevsink->varinfo.yres - fbdevsink->height) / 2;
if (fbdevsink->cy < 0)
fbdevsink->cy = 0;
fbdevsink->linelen = fbdevsink->width * fbdevsink->bytespp;
if (fbdevsink->linelen > fbdevsink->fixinfo.line_length)
fbdevsink->linelen = fbdevsink->fixinfo.line_length;
fbdevsink->lines = fbdevsink->height;
if (fbdevsink->lines > fbdevsink->varinfo.yres)
fbdevsink->lines = fbdevsink->varinfo.yres;
return TRUE;
}
static GstFlowReturn
gst_fbdevsink_show_frame (GstVideoSink * videosink, GstBuffer * buf)
{
GstFBDEVSink *fbdevsink;
GstMapInfo map;
int i;
fbdevsink = GST_FBDEVSINK (videosink);
/* optimization could remove this memcpy by allocating the buffer
in framebuffer memory, but would only work when xres matches
the video width */
if (!gst_buffer_map (buf, &map, GST_MAP_READ))
return GST_FLOW_ERROR;
for (i = 0; i < fbdevsink->lines; i++) {
memcpy (fbdevsink->framebuffer
+ (i + fbdevsink->cy) * fbdevsink->fixinfo.line_length
+ fbdevsink->cx * fbdevsink->bytespp,
map.data + i * fbdevsink->width * fbdevsink->bytespp,
fbdevsink->linelen);
}
gst_buffer_unmap (buf, &map);
return GST_FLOW_OK;
}
static gboolean
gst_fbdevsink_start (GstBaseSink * bsink)
{
GstFBDEVSink *fbdevsink;
fbdevsink = GST_FBDEVSINK (bsink);
if (!fbdevsink->device) {
fbdevsink->device = g_strdup ("/dev/fb0");
}
fbdevsink->fd = open (fbdevsink->device, O_RDWR);
if (fbdevsink->fd == -1)
return FALSE;
/* get the fixed screen info */
if (ioctl (fbdevsink->fd, FBIOGET_FSCREENINFO, &fbdevsink->fixinfo))
return FALSE;
/* get the variable screen info */
if (ioctl (fbdevsink->fd, FBIOGET_VSCREENINFO, &fbdevsink->varinfo))
return FALSE;
/* map the framebuffer */
fbdevsink->framebuffer = mmap (0, fbdevsink->fixinfo.smem_len,
PROT_WRITE, MAP_SHARED, fbdevsink->fd, 0);
if (fbdevsink->framebuffer == MAP_FAILED)
return FALSE;
return TRUE;
}
static gboolean
gst_fbdevsink_stop (GstBaseSink * bsink)
{
GstFBDEVSink *fbdevsink;
fbdevsink = GST_FBDEVSINK (bsink);
if (munmap (fbdevsink->framebuffer, fbdevsink->fixinfo.smem_len))
return FALSE;
if (close (fbdevsink->fd))
return FALSE;
return TRUE;
}
static void
gst_fbdevsink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstFBDEVSink *fbdevsink;
fbdevsink = GST_FBDEVSINK (object);
switch (prop_id) {
case ARG_DEVICE:{
g_free (fbdevsink->device);
fbdevsink->device = g_value_dup_string (value);
break;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_fbdevsink_get_property (GObject * object, guint prop_id, GValue * value,
GParamSpec * pspec)
{
GstFBDEVSink *fbdevsink;
fbdevsink = GST_FBDEVSINK (object);
switch (prop_id) {
case ARG_DEVICE:{
g_value_set_string (value, fbdevsink->device);
break;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static GstStateChangeReturn
gst_fbdevsink_change_state (GstElement * element, GstStateChange transition)
{
GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
g_return_val_if_fail (GST_IS_FBDEVSINK (element), GST_STATE_CHANGE_FAILURE);
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
switch (transition) {
default:
break;
}
return ret;
}
static gboolean
plugin_init (GstPlugin * plugin)
{
if (!gst_element_register (plugin, "fbdevsink", GST_RANK_NONE,
GST_TYPE_FBDEVSINK))
return FALSE;
return TRUE;
}
static void
gst_fbdevsink_class_init (GstFBDEVSinkClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
GstBaseSinkClass *basesink_class;
GstVideoSinkClass *videosink_class;
gobject_class = (GObjectClass *) klass;
gstelement_class = (GstElementClass *) klass;
basesink_class = (GstBaseSinkClass *) klass;
videosink_class = (GstVideoSinkClass *) klass;
gobject_class->set_property = gst_fbdevsink_set_property;
gobject_class->get_property = gst_fbdevsink_get_property;
gobject_class->finalize = gst_fbdevsink_finalize;
gstelement_class->change_state =
GST_DEBUG_FUNCPTR (gst_fbdevsink_change_state);
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DEVICE,
g_param_spec_string ("device", "device",
"The framebuffer device eg: /dev/fb0", NULL, G_PARAM_READWRITE));
basesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_fbdevsink_setcaps);
basesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_fbdevsink_getcaps);
#if 0
basesink_class->get_times = GST_DEBUG_FUNCPTR (gst_fbdevsink_get_times);
#endif
basesink_class->start = GST_DEBUG_FUNCPTR (gst_fbdevsink_start);
basesink_class->stop = GST_DEBUG_FUNCPTR (gst_fbdevsink_stop);
videosink_class->show_frame = GST_DEBUG_FUNCPTR (gst_fbdevsink_show_frame);
gst_element_class_set_static_metadata (gstelement_class, "fbdev video sink",
"Sink/Video", "Linux framebuffer videosink",
"Sean D'Epagnier <sean@depagnier.com>");
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&sink_template));
}
static void
gst_fbdevsink_finalize (GObject * object)
{
GstFBDEVSink *fbdevsink = GST_FBDEVSINK (object);
g_free (fbdevsink->device);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
GST_VERSION_MINOR,
fbdevsink,
"Linux framebuffer video sink",
plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)