1/*
2 * Copyright (C) 2010 Igalia S.L
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21#include "ImageGStreamer.h"
22
23#if ENABLE(VIDEO) && USE(GSTREAMER)
24
25#include "GStreamerUtilities.h"
26
27#include <cairo.h>
28#include <gst/gst.h>
29#include <gst/video/gstvideometa.h>
30
31
32using namespace std;
33using namespace WebCore;
34
35ImageGStreamer::ImageGStreamer(GstBuffer* buffer, GstCaps* caps)
36{
37    GstVideoInfo videoInfo;
38    gst_video_info_init(&videoInfo);
39    if (!gst_video_info_from_caps(&videoInfo, caps))
40        return;
41
42    // Right now the TextureMapper only supports chromas with one plane
43    ASSERT(GST_VIDEO_INFO_N_PLANES(&videoInfo) == 1);
44
45    if (!gst_video_frame_map(&m_videoFrame, &videoInfo, buffer, GST_MAP_READ))
46        return;
47
48    unsigned char* bufferData = reinterpret_cast<unsigned char*>(GST_VIDEO_FRAME_PLANE_DATA(&m_videoFrame, 0));
49
50    cairo_format_t cairoFormat;
51#if G_BYTE_ORDER == G_LITTLE_ENDIAN
52    cairoFormat = (GST_VIDEO_FRAME_FORMAT(&m_videoFrame) == GST_VIDEO_FORMAT_BGRA) ? CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24;
53#else
54    cairoFormat = (GST_VIDEO_FRAME_FORMAT(&m_videoFrame) == GST_VIDEO_FORMAT_ARGB) ? CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24;
55#endif
56
57    int stride = GST_VIDEO_FRAME_PLANE_STRIDE(&m_videoFrame, 0);
58    int width = GST_VIDEO_FRAME_WIDTH(&m_videoFrame);
59    int height = GST_VIDEO_FRAME_HEIGHT(&m_videoFrame);
60
61    RefPtr<cairo_surface_t> surface = adoptRef(cairo_image_surface_create_for_data(bufferData, cairoFormat, width, height, stride));
62    ASSERT(cairo_surface_status(surface.get()) == CAIRO_STATUS_SUCCESS);
63    m_image = BitmapImage::create(surface.release());
64
65    if (GstVideoCropMeta* cropMeta = gst_buffer_get_video_crop_meta(buffer))
66        setCropRect(FloatRect(cropMeta->x, cropMeta->y, cropMeta->width, cropMeta->height));
67}
68
69ImageGStreamer::~ImageGStreamer()
70{
71    if (m_image)
72        m_image.clear();
73
74    m_image = 0;
75
76    // We keep the buffer memory mapped until the image is destroyed because the internal
77    // cairo_surface_t was created using cairo_image_surface_create_for_data().
78    gst_video_frame_unmap(&m_videoFrame);
79}
80#endif // USE(GSTREAMER)
81