1 /*
2  * gst-plugins-bad
3  * Copyright (C) 2012 Edward Hervey <edward@collabora.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include "gstvdpvideobufferpool.h"
27 #include "gstvdpvideomemory.h"
28 
29 GST_DEBUG_CATEGORY_STATIC (gst_vdp_vidbufpool_debug);
30 #define GST_CAT_DEFAULT gst_vdp_vidbufpool_debug
31 
32 static void gst_vdp_video_buffer_pool_finalize (GObject * object);
33 
34 #define DEBUG_INIT \
35     GST_DEBUG_CATEGORY_INIT (gst_vdp_vidbufpool_debug, "vdpvideopool", 0, \
36     "VDPAU Video bufferpool");
37 
38 #define gst_vdp_video_buffer_pool_parent_class parent_class
39 G_DEFINE_TYPE_WITH_CODE (GstVdpVideoBufferPool, gst_vdp_video_buffer_pool,
40     GST_TYPE_BUFFER_POOL, DEBUG_INIT);
41 
42 static const gchar **
gst_vdp_video_buffer_pool_get_options(GstBufferPool * pool)43 gst_vdp_video_buffer_pool_get_options (GstBufferPool * pool)
44 {
45   static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META,
46     GST_BUFFER_POOL_OPTION_VDP_VIDEO_META, NULL
47   };
48 
49   return options;
50 }
51 
52 static gboolean
gst_vdp_video_buffer_pool_set_config(GstBufferPool * pool,GstStructure * config)53 gst_vdp_video_buffer_pool_set_config (GstBufferPool * pool,
54     GstStructure * config)
55 {
56   GstVdpVideoBufferPool *vdppool = GST_VDP_VIDEO_BUFFER_POOL_CAST (pool);
57   GstVideoInfo info;
58   GstCaps *caps;
59 
60   if (!gst_buffer_pool_config_get_params (config, &caps, NULL, NULL, NULL))
61     goto wrong_config;
62 
63   if (caps == NULL)
64     goto no_caps;
65 
66   /* now parse the caps from the config */
67   if (!gst_video_info_from_caps (&info, caps))
68     goto wrong_caps;
69 
70   GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, info.width, info.height,
71       caps);
72 
73   if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_UNKNOWN)
74     goto unknown_format;
75 
76   vdppool->info = info;
77 
78   /* enable metadata based on config of the pool */
79   vdppool->add_videometa =
80       gst_buffer_pool_config_has_option (config,
81       GST_BUFFER_POOL_OPTION_VIDEO_META);
82 
83   /* parse extra alignment info */
84   vdppool->add_vdpmeta = gst_buffer_pool_config_has_option (config,
85       GST_BUFFER_POOL_OPTION_VDP_VIDEO_META);
86 
87   return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config);
88 
89   /* ERRORS */
90 wrong_config:
91   {
92     GST_WARNING_OBJECT (pool, "invalid config");
93     return FALSE;
94   }
95 no_caps:
96   {
97     GST_WARNING_OBJECT (pool, "no caps in config");
98     return FALSE;
99   }
100 wrong_caps:
101   {
102     GST_WARNING_OBJECT (pool,
103         "failed getting geometry from caps %" GST_PTR_FORMAT, caps);
104     return FALSE;
105   }
106 unknown_format:
107   {
108     GST_WARNING_OBJECT (vdppool, "failed to get format from caps %"
109         GST_PTR_FORMAT, caps);
110     GST_ELEMENT_ERROR (vdppool, RESOURCE, WRITE,
111         ("Failed to create output image buffer of %dx%d pixels",
112             info.width, info.height),
113         ("Invalid input caps %" GST_PTR_FORMAT, caps));
114     return FALSE;
115   }
116 }
117 
118 /* This function handles GstBuffer creation */
119 static GstFlowReturn
gst_vdp_video_buffer_pool_alloc(GstBufferPool * pool,GstBuffer ** buffer,GstBufferPoolAcquireParams * params)120 gst_vdp_video_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
121     GstBufferPoolAcquireParams * params)
122 {
123   GstVdpVideoBufferPool *vdppool = GST_VDP_VIDEO_BUFFER_POOL_CAST (pool);
124   GstVideoInfo *info;
125   GstBuffer *buf;
126   GstMemory *vdp_mem;
127 
128   info = &vdppool->info;
129 
130   if (!(buf = gst_buffer_new ()))
131     goto no_buffer;
132 
133   if (!(vdp_mem = gst_vdp_video_memory_alloc (vdppool->device, info)))
134     goto mem_create_failed;
135 
136   gst_buffer_append_memory (buf, vdp_mem);
137 
138   if (vdppool->add_videometa) {
139     GstVideoMeta *vmeta;
140 
141     GST_DEBUG_OBJECT (pool, "adding GstVideoMeta");
142     /* these are just the defaults for now */
143     vmeta = gst_buffer_add_video_meta (buf, 0, GST_VIDEO_INFO_FORMAT (info),
144         GST_VIDEO_INFO_WIDTH (info), GST_VIDEO_INFO_HEIGHT (info));
145     vmeta->map = gst_vdp_video_memory_map;
146     vmeta->unmap = gst_vdp_video_memory_unmap;
147   }
148 
149   *buffer = buf;
150 
151   return GST_FLOW_OK;
152 
153   /* ERROR */
154 no_buffer:
155   {
156     GST_WARNING_OBJECT (pool, "can't create image");
157     return GST_FLOW_ERROR;
158   }
159 
160 mem_create_failed:
161   {
162     GST_WARNING_OBJECT (pool, "Could create GstVdpVideo Memory");
163     return GST_FLOW_ERROR;
164   }
165 }
166 
167 
168 GstBufferPool *
gst_vdp_video_buffer_pool_new(GstVdpDevice * device)169 gst_vdp_video_buffer_pool_new (GstVdpDevice * device)
170 {
171   GstVdpVideoBufferPool *pool;
172 
173   pool = g_object_new (GST_TYPE_VDP_VIDEO_BUFFER_POOL, NULL);
174   g_object_ref_sink (pool);
175   pool->device = gst_object_ref (device);
176 
177   GST_LOG_OBJECT (pool, "new VdpVideo buffer pool %p", pool);
178 
179   return GST_BUFFER_POOL_CAST (pool);
180 }
181 
182 static void
gst_vdp_video_buffer_pool_class_init(GstVdpVideoBufferPoolClass * klass)183 gst_vdp_video_buffer_pool_class_init (GstVdpVideoBufferPoolClass * klass)
184 {
185   GObjectClass *gobject_class = (GObjectClass *) klass;
186   GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;
187 
188   gobject_class->finalize = gst_vdp_video_buffer_pool_finalize;
189 
190   gstbufferpool_class->get_options = gst_vdp_video_buffer_pool_get_options;
191   gstbufferpool_class->set_config = gst_vdp_video_buffer_pool_set_config;
192   gstbufferpool_class->alloc_buffer = gst_vdp_video_buffer_pool_alloc;
193 }
194 
195 static void
gst_vdp_video_buffer_pool_init(GstVdpVideoBufferPool * pool)196 gst_vdp_video_buffer_pool_init (GstVdpVideoBufferPool * pool)
197 {
198 
199 }
200 
201 static void
gst_vdp_video_buffer_pool_finalize(GObject * object)202 gst_vdp_video_buffer_pool_finalize (GObject * object)
203 {
204   GstVdpVideoBufferPool *pool = GST_VDP_VIDEO_BUFFER_POOL_CAST (object);
205 
206   GST_LOG_OBJECT (pool, "finalize VdpVideo buffer pool %p", pool);
207 
208   gst_object_unref (pool->device);
209 
210   G_OBJECT_CLASS (gst_vdp_video_buffer_pool_parent_class)->finalize (object);
211 }
212