]> code.ossystems Code Review - meta-freescale.git/blob
1cead36d4be219aeed7736c9db638df59cbd1d23
[meta-freescale.git] /
1 From 05bbd82dd527afa44d6b403b02a0dbd198c96859 Mon Sep 17 00:00:00 2001
2 From: Jian Li <jian.li@freescale.com>
3 Date: Fri, 6 Nov 2015 15:00:19 +0800
4 Subject: [PATCH 14/18] MMFMWK-6930 [glplugin] Accelerate gldownload with
5  directviv API
6 MIME-Version: 1.0
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
9
10 1) Propose a physical buffer pool to upstream in gldownload
11 2) Bind the physical buffer with texture via dirctviv
12 3) In gldownload, wrap the physical buffer to gstbuffer, pass to
13    downstream plugins.
14
15 Upstream-Status: Inappropriate [i.MX specific]
16
17 Signed-off-by: Jian Li <jian.li@freescale.com>
18 Signed-off-by: Lyon Wang <lyon.wang@freescale.com>
19 ---
20  ext/gl/gstgldownloadelement.c           |  91 ++++++++++++
21  gst-libs/gst/gl/Makefile.am             |   4 +
22  gst-libs/gst/gl/gstglbufferpool.c       |  12 ++
23  gst-libs/gst/gl/gstglphymemory.c        | 254 ++++++++++++++++++++++++++++++++
24  gst-libs/gst/gl/gstglphymemory.h        |  43 ++++++
25  gst-libs/gst/gl/gstglvivdirecttexture.c | 147 +++++++++---------
26  gst-libs/gst/gl/gstglvivdirecttexture.h |   3 +
27  7 files changed, 484 insertions(+), 70 deletions(-)
28  create mode 100644 gst-libs/gst/gl/gstglphymemory.c
29  create mode 100644 gst-libs/gst/gl/gstglphymemory.h
30
31 diff --git a/ext/gl/gstgldownloadelement.c b/ext/gl/gstgldownloadelement.c
32 index ff931fa..9ea0146 100644
33 --- a/ext/gl/gstgldownloadelement.c
34 +++ b/ext/gl/gstgldownloadelement.c
35 @@ -23,6 +23,7 @@
36  #endif
37  
38  #include <gst/gl/gl.h>
39 +#include <gst/gl/gstglphymemory.h>
40  #include "gstgldownloadelement.h"
41  
42  GST_DEBUG_CATEGORY_STATIC (gst_gl_download_element_debug);
43 @@ -45,6 +46,8 @@ gst_gl_download_element_prepare_output_buffer (GstBaseTransform * bt,
44      GstBuffer * buffer, GstBuffer ** outbuf);
45  static GstFlowReturn gst_gl_download_element_transform (GstBaseTransform * bt,
46      GstBuffer * buffer, GstBuffer * outbuf);
47 +static gboolean gst_gl_download_element_propose_allocation (GstBaseTransform *
48 +    bt, GstQuery * decide_query, GstQuery * query);
49  
50  static GstStaticPadTemplate gst_gl_download_element_src_pad_template =
51      GST_STATIC_PAD_TEMPLATE ("src",
52 @@ -70,6 +73,7 @@ gst_gl_download_element_class_init (GstGLDownloadElementClass * klass)
53    bt_class->prepare_output_buffer =
54        gst_gl_download_element_prepare_output_buffer;
55    bt_class->transform = gst_gl_download_element_transform;
56 +  bt_class->propose_allocation = gst_gl_download_element_propose_allocation;
57  
58    bt_class->passthrough_on_same_caps = TRUE;
59  
60 @@ -160,9 +164,24 @@ static GstFlowReturn
61  gst_gl_download_element_prepare_output_buffer (GstBaseTransform * bt,
62      GstBuffer * inbuf, GstBuffer ** outbuf)
63  {
64 +  GstGLDownloadElement *download = GST_GL_DOWNLOAD_ELEMENT (bt);
65    GstCaps *src_caps = gst_pad_get_current_caps (bt->srcpad);
66    GstCapsFeatures *features = NULL;
67    gint i, n;
68 +  GstGLMemory *glmem;
69 +
70 +  glmem = gst_buffer_peek_memory (inbuf, 0);
71 +  if (gst_is_gl_physical_memory (glmem)) {
72 +    GstGLContext *context = GST_GL_BASE_FILTER (bt)->context;
73 +    GstVideoInfo info;
74 +
75 +    gst_video_info_from_caps (&info, src_caps);
76 +    *outbuf = gst_gl_phymem_buffer_to_gstbuffer (context, &info, inbuf);
77 +
78 +    GST_DEBUG_OBJECT (download, "gl download with direct viv.");
79 +
80 +    return GST_FLOW_OK;
81 +  }
82  
83    *outbuf = inbuf;
84  
85 @@ -194,3 +213,75 @@ gst_gl_download_element_transform (GstBaseTransform * bt,
86  {
87    return GST_FLOW_OK;
88  }
89 +
90 +static gboolean
91 +gst_gl_download_element_propose_allocation (GstBaseTransform * bt,
92 +    GstQuery * decide_query, GstQuery * query)
93 +{
94 +  GstGLContext *context = GST_GL_BASE_FILTER (bt)->context;
95 +  GstGLDownloadElement *download = GST_GL_DOWNLOAD_ELEMENT (bt);
96 +  GstAllocationParams params;
97 +  GstAllocator *allocator = NULL;
98 +  GstBufferPool *pool = NULL;
99 +  guint n_pools, i;
100 +  GstVideoInfo info;
101 +  GstCaps *caps;
102 +  GstStructure *config;
103 +  gsize size;
104 +
105 +  gst_query_parse_allocation (query, &caps, NULL);
106 +  if (!gst_video_info_from_caps (&info, caps)) {
107 +    GST_WARNING_OBJECT (bt, "invalid caps specified");
108 +    return FALSE;
109 +  }
110 +
111 +  GST_DEBUG_OBJECT (bt, "video format is %s", gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (&info)));
112 +
113 +  gst_allocation_params_init (&params);
114 +  if (gst_is_gl_physical_memory_supported_fmt (&info)) {
115 +    allocator = gst_phy_mem_allocator_obtain ();
116 +    GST_DEBUG_OBJECT (bt, "obtain physical memory allocator %p.", allocator);
117 +  }
118 +
119 +  if (!allocator)
120 +    allocator = gst_allocator_find (GST_GL_MEMORY_ALLOCATOR_NAME);
121 +
122 +  if (!allocator) {
123 +    GST_ERROR_OBJECT (bt, "Can't obtain physical memory allocator.");
124 +    return FALSE;
125 +  }
126 +
127 +  gst_query_add_allocation_param (query, allocator, &params);
128 +  gst_object_unref (allocator);
129 +
130 +  n_pools = gst_query_get_n_allocation_pools (query);
131 +  for (i = 0; i < n_pools; i++) {
132 +    gst_query_parse_nth_allocation_pool (query, i, &pool, NULL, NULL, NULL);
133 +    gst_object_unref (pool);
134 +    pool = NULL;
135 +  }
136 +
137 +  //new buffer pool
138 +  pool = gst_gl_buffer_pool_new (context);
139 +  config = gst_buffer_pool_get_config (pool);
140 +
141 +  /* the normal size of a frame */
142 +  size = info.size;
143 +  gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
144 +  gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_GL_SYNC_META);
145 +
146 +  if (!gst_buffer_pool_set_config (pool, config)) {
147 +    gst_object_unref (pool);
148 +    GST_WARNING_OBJECT (bt, "failed setting config");
149 +    return FALSE;
150 +  }
151 +
152 +  GST_DEBUG_OBJECT (download, "create pool %p", pool);
153 +
154 +  //propose 3 buffers for better performance
155 +  gst_query_add_allocation_pool (query, pool, size, 3, 0);
156 +
157 +  gst_object_unref (pool);
158 +
159 +  return TRUE;
160 +}
161 diff --git a/gst-libs/gst/gl/Makefile.am b/gst-libs/gst/gl/Makefile.am
162 index c396603..5c05230 100644
163 --- a/gst-libs/gst/gl/Makefile.am
164 +++ b/gst-libs/gst/gl/Makefile.am
165 @@ -34,6 +34,7 @@ libgstgl_@GST_API_VERSION@_la_SOURCES = \
166         gstgloverlaycompositor.c \
167         gstglquery.c \
168         gstglvivdirecttexture.c \
169 +       gstglphymemory.c \
170         gstglcontrolbindingproxy.c
171  
172  libgstgl_@GST_API_VERSION@includedir = $(includedir)/gstreamer-@GST_API_VERSION@/gst/gl
173 @@ -68,6 +69,7 @@ libgstgl_@GST_API_VERSION@include_HEADERS = \
174         gstgl_fwd.h \
175         gstgl_enums.h \
176         gstglvivdirecttexture.h \
177 +       gstglphymemory.h \
178         gl.h
179  
180  noinst_HEADERS = \
181 @@ -84,6 +86,8 @@ libgstgl_@GST_API_VERSION@_la_LIBADD = \
182         $(GST_LIBS) \
183         $(GL_LIBS)
184  
185 +libgstgl_@GST_API_VERSION@_la_LIBADD += -lgstfsl-$(GST_API_VERSION)
186 +
187  if HAVE_WINDOW_WIN32
188  SUBDIRS += win32
189  libgstgl_@GST_API_VERSION@_la_LIBADD += win32/libgstgl-win32.la
190 diff --git a/gst-libs/gst/gl/gstglbufferpool.c b/gst-libs/gst/gl/gstglbufferpool.c
191 index 90536b0..71c726a 100644
192 --- a/gst-libs/gst/gl/gstglbufferpool.c
193 +++ b/gst-libs/gst/gl/gstglbufferpool.c
194 @@ -30,6 +30,8 @@
195  #include <gst/gl/egl/gsteglimagememory.h>
196  #endif
197  
198 +#include <gst/gl/gstglphymemory.h>
199 +
200  /**
201   * SECTION:gstglbufferpool
202   * @short_description: buffer pool for #GstGLMemory objects
203 @@ -290,6 +292,16 @@ gst_gl_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
204    }
205  #endif
206  
207 +  if ((g_strcmp0 (priv->allocator->mem_type, GST_GL_PHY_MEM_ALLOCATOR) == 0)) {
208 +    GstAllocator* allocator = (GstAllocator*) gst_phy_mem_allocator_obtain ();
209 +    if (!gst_gl_physical_memory_setup_buffer (allocator, buf, priv->gl_params)) {
210 +      GST_ERROR_OBJECT (pool, "Can't create physcial buffer.");
211 +      return GST_FLOW_ERROR;
212 +    }
213 +    *buffer = buf;
214 +    return GST_FLOW_OK;
215 +  }
216 +
217    alloc = GST_GL_MEMORY_ALLOCATOR (priv->allocator);
218    if (!gst_gl_memory_setup_buffer (alloc, buf, priv->gl_params))
219      goto mem_create_failed;
220 diff --git a/gst-libs/gst/gl/gstglphymemory.c b/gst-libs/gst/gl/gstglphymemory.c
221 new file mode 100644
222 index 0000000..52ae41f
223 --- /dev/null
224 +++ b/gst-libs/gst/gl/gstglphymemory.c
225 @@ -0,0 +1,254 @@
226 +/*
227 + * GStreamer
228 + * Copyright (c) 2015, Freescale Semiconductor, Inc. 
229 + *
230 + * This library is free software; you can redistribute it and/or
231 + * modify it under the terms of the GNU Library General Public
232 + * License as published by the Free Software Foundation; either
233 + * version 2 of the License, or (at your option) any later version.
234 + *
235 + * This library is distributed in the hope that it will be useful,
236 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
237 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
238 + * Library General Public License for more details.
239 + *
240 + * You should have received a copy of the GNU Library General Public
241 + * License along with this library; if not, write to the
242 + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
243 + * Boston, MA 02110-1301, USA.
244 + */
245 +
246 +#ifdef HAVE_CONFIG_H
247 +#include "config.h"
248 +#endif
249 +
250 +#include "gstglvivdirecttexture.h"
251 +#include "gstglphymemory.h"
252 +#include "g2d.h"
253 +
254 +GST_DEBUG_CATEGORY_STATIC (GST_CAT_GL_PHY_MEMORY);
255 +#define GST_CAT_DEFAULT GST_CAT_GL_PHY_MEMORY
256 +
257 +typedef struct _GstPhyMemAllocator GstPhyMemAllocator;
258 +typedef struct _GstPhyMemAllocatorClass GstPhyMemAllocatorClass;
259 +
260 +struct _GstPhyMemAllocator
261 +{
262 +  GstAllocatorPhyMem parent;
263 +};
264 +
265 +struct _GstPhyMemAllocatorClass
266 +{
267 +  GstAllocatorPhyMemClass parent_class;
268 +};
269 +
270 +GType gst_phy_mem_allocator_get_type (void);
271 +G_DEFINE_TYPE (GstPhyMemAllocator, gst_phy_mem_allocator, GST_TYPE_ALLOCATOR_PHYMEM);
272 +
273 +static int
274 +alloc_phymem (GstAllocatorPhyMem *allocator, PhyMemBlock *memblk)
275 +{
276 +  struct g2d_buf *pbuf = NULL;
277 +
278 +  memblk->size = PAGE_ALIGN(memblk->size);
279 +
280 +  pbuf = g2d_alloc (memblk->size, 0);
281 +  if (!pbuf) {
282 +    GST_ERROR("G2D allocate %u bytes memory failed: %s",
283 +        memblk->size, strerror(errno));
284 +    return -1;
285 +  }
286 +
287 +  memblk->vaddr = (guchar*) pbuf->buf_vaddr;
288 +  memblk->paddr = (guchar*) pbuf->buf_paddr;
289 +  memblk->user_data = (gpointer) pbuf;
290 +  GST_DEBUG("G2D allocated memory (%p)", memblk->paddr);
291 +
292 +  return 1;
293 +}
294 +
295 +static int
296 +free_phymem (GstAllocatorPhyMem *allocator, PhyMemBlock *memblk)
297 +{
298 +  GST_DEBUG("G2D free memory (%p)", memblk->paddr);
299 +  gint ret = g2d_free ((struct g2d_buf*)(memblk->user_data));
300 +  memblk->user_data = NULL;
301 +  memblk->vaddr = NULL;
302 +  memblk->paddr = NULL;
303 +  memblk->size = 0;
304 +
305 +  return ret;
306 +}
307 +
308 +static void
309 +gst_phy_mem_allocator_class_init (GstPhyMemAllocatorClass * klass)
310 +{
311 +  GstAllocatorPhyMemClass *phy_allocator_klass = (GstAllocatorPhyMemClass *) klass;
312 +
313 +  phy_allocator_klass->alloc_phymem = alloc_phymem;
314 +  phy_allocator_klass->free_phymem = free_phymem;
315 +}
316 +
317 +static void
318 +gst_phy_mem_allocator_init (GstPhyMemAllocator * allocator)
319 +{
320 +  GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
321 +
322 +  alloc->mem_type = GST_GL_PHY_MEM_ALLOCATOR;
323 +}
324 +
325 +
326 +static gpointer
327 +gst_phy_mem_allocator_init_instance (gpointer data)
328 +{
329 +  GstAllocator *allocator =
330 +      g_object_new (gst_phy_mem_allocator_get_type (), NULL);
331 +
332 +  GST_DEBUG_CATEGORY_INIT (GST_CAT_GL_PHY_MEMORY, "glphymemory", 0,
333 +      "GLPhysical Memory");
334 +
335 +  gst_allocator_register (GST_GL_PHY_MEM_ALLOCATOR, gst_object_ref (allocator));
336 +
337 +  return allocator;
338 +}
339 +
340 +static void
341 +_finish_texture (GstGLContext * ctx, gpointer *data)
342 +{
343 +  GstGLFuncs *gl = ctx->gl_vtable;
344 +
345 +  gl->Finish ();
346 +}
347 +
348 +static void
349 +gst_gl_phy_mem_destroy (GstMemory *mem)
350 +{
351 +  gst_memory_unref (mem);
352 +}
353 +
354 +
355 +GstAllocator *
356 +gst_phy_mem_allocator_obtain (void)
357 +{
358 +  static GOnce once = G_ONCE_INIT;
359 +
360 +  g_once (&once, gst_phy_mem_allocator_init_instance, NULL);
361 +
362 +  g_return_val_if_fail (once.retval != NULL, NULL);
363 +
364 +  return (GstAllocator *) (g_object_ref (once.retval));
365 +}
366 +
367 +gboolean
368 +gst_is_gl_physical_memory (GstMemory * mem)
369 +{
370 +  GstGLBaseMemory *glmem;
371 +  g_return_val_if_fail (gst_is_gl_memory (mem), FALSE);
372 +
373 +  glmem = (GstGLBaseMemory*) mem;
374 +
375 +  if (glmem->user_data
376 +      && GST_IS_MINI_OBJECT_TYPE(glmem->user_data, GST_TYPE_MEMORY))
377 +    return gst_memory_is_type ((GstMemory*)glmem->user_data, GST_GL_PHY_MEM_ALLOCATOR);
378 +  else
379 +    return FALSE;
380 +}
381 +
382 +gboolean
383 +gst_is_gl_physical_memory_supported_fmt (GstVideoInfo * info)
384 +{
385 +  if (GST_VIDEO_INFO_IS_RGB(info)
386 +      && gst_gl_is_directviv_supported_format (GST_VIDEO_INFO_FORMAT (info))) {
387 +    return TRUE;
388 +  }
389 +  else
390 +    return FALSE;
391 +}
392 +
393 +gboolean
394 +gst_gl_physical_memory_setup_buffer (GstAllocator * allocator, GstBuffer *buffer, 
395 +    GstGLVideoAllocationParams * params)
396 +{
397 +  GstGLBaseMemoryAllocator *gl_alloc;
398 +  GstMemory *mem = NULL;
399 +  PhyMemBlock *memblk = NULL;
400 +  GstGLMemory *glmem = NULL;
401 +  gsize size;
402 +
403 +  GstVideoInfo * info = params->v_info;
404 +  GstVideoAlignment * valign = params->valign;
405 +
406 +  GST_DEBUG ("glphymemory setup buffer format %s", gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (info)));
407 +  
408 +  if (!gst_is_gl_physical_memory_supported_fmt (info)) {
409 +    GST_DEBUG ("Not support format.");
410 +    return FALSE;
411 +  }
412 +
413 +  //allocator = (GstAllocator*) gst_phy_mem_allocator_obtain ();
414 +  size = gst_gl_get_plane_data_size (info, valign, 0);
415 +  mem = gst_allocator_alloc (allocator, size, params->parent.alloc_params);
416 +  if (!mem) {
417 +    GST_DEBUG ("Can't allocate physical memory size %d", size);
418 +    return FALSE;
419 +  }
420 +
421 +  memblk = gst_memory_query_phymem_block (mem);
422 +  if (!memblk) {
423 +    GST_ERROR("Can't find physic memory block.");
424 +    return FALSE;
425 +  }
426 +
427 +  gl_alloc =
428 +      GST_GL_BASE_MEMORY_ALLOCATOR (gst_gl_memory_allocator_get_default
429 +      (params->parent.context));
430 +
431 +  params->plane = 0;
432 +  params->parent.user_data = mem;
433 +  params->parent.notify = gst_gl_phy_mem_destroy;
434 +
435 +  glmem = (GstGLMemory *)gst_gl_base_memory_alloc (gl_alloc, (GstGLAllocationParams *) params);
436 +  if (!glmem) {
437 +    GST_ERROR("Can't get gl memory.");
438 +    return FALSE;
439 +  }
440 +
441 +  gst_buffer_append_memory (buffer, (GstMemory *) glmem);
442 +
443 +  gst_buffer_add_video_meta_full (buffer, 0,
444 +      GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
445 +      GST_VIDEO_INFO_HEIGHT (info), 1, info->offset, info->stride);
446 +
447 +  gst_gl_viv_direct_bind_data(params->parent.context, glmem->tex_id, 
448 +      GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
449 +      GST_VIDEO_INFO_HEIGHT (info), memblk->vaddr, memblk->paddr);
450 +
451 +  return TRUE;
452 +}
453 +
454 +GstBuffer *
455 +gst_gl_phymem_buffer_to_gstbuffer (GstGLContext * ctx,
456 +    GstVideoInfo * info, GstBuffer *glbuf)
457 +{
458 +  GstBuffer *buf;
459 +  GstGLBaseMemory *glmem;
460 +
461 +  gst_gl_context_thread_add (ctx, (GstGLContextThreadFunc) _finish_texture, NULL);
462 +
463 +  glmem = gst_buffer_peek_memory (glbuf, 0);
464 +
465 +  buf = gst_buffer_new ();
466 +  gst_buffer_append_memory (buf, (GstMemory *) glmem->user_data);
467 +  gst_memory_ref ((GstMemory *)glmem->user_data);
468 +
469 +  gst_buffer_add_video_meta_full (buf, 0,
470 +      GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
471 +      GST_VIDEO_INFO_HEIGHT (info), 1, info->offset, info->stride);
472 +  GST_BUFFER_FLAGS (buf) = GST_BUFFER_FLAGS (glbuf);
473 +  GST_BUFFER_PTS (buf) = GST_BUFFER_PTS (glbuf);
474 +  GST_BUFFER_DTS (buf) = GST_BUFFER_DTS (glbuf);
475 +  GST_BUFFER_DURATION (buf) = GST_BUFFER_DURATION (glbuf);
476 +
477 +  return buf;
478 +}
479 +
480 diff --git a/gst-libs/gst/gl/gstglphymemory.h b/gst-libs/gst/gl/gstglphymemory.h
481 new file mode 100644
482 index 0000000..b1a69e7
483 --- /dev/null
484 +++ b/gst-libs/gst/gl/gstglphymemory.h
485 @@ -0,0 +1,43 @@
486 +/*
487 + * GStreamer
488 + * Copyright (c) 2015, Freescale Semiconductor, Inc. 
489 + *
490 + * This library is free software; you can redistribute it and/or
491 + * modify it under the terms of the GNU Library General Public
492 + * License as published by the Free Software Foundation; either
493 + * version 2 of the License, or (at your option) any later version.
494 + *
495 + * This library is distributed in the hope that it will be useful,
496 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
497 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
498 + * Library General Public License for more details.
499 + *
500 + * You should have received a copy of the GNU Library General Public
501 + * License along with this library; if not, write to the
502 + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
503 + * Boston, MA 02110-1301, USA.
504 + */
505 +
506 +#ifndef _GST_GL_PHY_MEMORY_H_
507 +#define _GST_GL_PHY_MEMORY_H_
508 +
509 +#include <gst/gst.h>
510 +#include <gst/gstmemory.h>
511 +#include <gst/video/video.h>
512 +#include <gst/imx-mm/gstallocatorphymem.h>
513 +
514 +#include <gst/gl/gl.h>
515 +
516 +G_BEGIN_DECLS
517 +
518 +#define GST_GL_PHY_MEM_ALLOCATOR "GLPhyMemory"
519 +
520 +GstAllocator *gst_phy_mem_allocator_obtain (void);
521 +gboolean gst_is_gl_physical_memory (GstMemory * mem);
522 +gboolean gst_is_gl_physical_memory_supported_fmt (GstVideoInfo * info);
523 +gboolean gst_gl_physical_memory_setup_buffer (GstAllocator * allocator, GstBuffer *buffer, GstGLVideoAllocationParams * params);
524 +GstBuffer * gst_gl_phymem_buffer_to_gstbuffer (GstGLContext * ctx, GstVideoInfo * info, GstBuffer *glbuf);
525 +
526 +G_END_DECLS
527 +
528 +#endif /* _GST_GL_PHY_MEMORY_H_ */
529 diff --git a/gst-libs/gst/gl/gstglvivdirecttexture.c b/gst-libs/gst/gl/gstglvivdirecttexture.c
530 index c19b617..e8e0b82 100644
531 --- a/gst-libs/gst/gl/gstglvivdirecttexture.c
532 +++ b/gst-libs/gst/gl/gstglvivdirecttexture.c
533 @@ -22,6 +22,7 @@
534  #include "config.h"
535  #endif
536  
537 +#include <gst/imx-mm/gstallocatorphymem.h>
538  #include "gl.h"
539  
540  GST_DEBUG_CATEGORY_EXTERN (gst_gl_upload_debug);
541 @@ -37,17 +38,28 @@ typedef struct {
542    gboolean ret;
543  } GstVivDirectTexture; 
544  
545 +typedef struct {
546 +  GstVideoFormat gst_fmt;
547 +  guint viv_fmt;
548 +} VIV_FMT_MAP;
549 +
550 +static VIV_FMT_MAP viv_fmt_map_table[] = {
551 +  {GST_VIDEO_FORMAT_I420, GL_VIV_I420},
552 +  {GST_VIDEO_FORMAT_YV12, GL_VIV_YV12},
553 +  {GST_VIDEO_FORMAT_NV12, GL_VIV_NV12},
554 +  {GST_VIDEO_FORMAT_NV21, GL_VIV_NV21},
555 +  {GST_VIDEO_FORMAT_YUY2, GL_VIV_YUY2},
556 +  {GST_VIDEO_FORMAT_UYVY, GL_VIV_UYVY},
557 +  {GST_VIDEO_FORMAT_RGBA, GL_RGBA},
558 +  {GST_VIDEO_FORMAT_RGBx, GL_RGBA},
559 +  {GST_VIDEO_FORMAT_BGRA, GL_BGRA_EXT},
560 +  {GST_VIDEO_FORMAT_RGB16, GL_RGB565_OES}
561 +};
562 +
563  gboolean
564  gst_is_physical_buffer (GstBuffer *buffer)
565  {
566 -
567 -  GstMemory *mem;
568 -
569 -  mem = gst_buffer_peek_memory (buffer, 0);
570 -  if (!mem->allocator)
571 -    return FALSE;
572 -
573 -  return g_type_check_instance_is_a (mem->allocator, g_type_from_name("GstAllocatorPhyMem"));
574 +  return gst_buffer_is_phymem (buffer);
575  }
576  
577  static void
578 @@ -65,32 +77,64 @@ _do_viv_direct_tex_bind_mem (GstGLContext * context, GstVivDirectTexture * viv_t
579  }
580  
581  gboolean
582 +gst_gl_is_directviv_supported_format (GstVideoFormat fmt)
583 +{
584 +  gint i;
585 +  gboolean ret = FALSE;
586 +
587 +  for (i=0; i<sizeof(viv_fmt_map_table)/sizeof(VIV_FMT_MAP); i++) {
588 +    if (fmt == viv_fmt_map_table[i].gst_fmt) {
589 +      ret = TRUE;
590 +      break;
591 +    }
592 +  }
593 +
594 +  return ret;
595 +}
596 +
597 +gboolean
598 +gst_gl_viv_direct_bind_data (GstGLContext * context,
599 +    guint tex_id, GstVideoFormat fmt, gint width, gint height, 
600 +    gpointer * vaddr, gpointer *paddr)
601 +{
602 +  guint viv_fmt = GL_NONE;
603 +  gint i;
604 +
605 +  for (i=0; i<sizeof(viv_fmt_map_table)/sizeof(VIV_FMT_MAP); i++) {
606 +    if (fmt == viv_fmt_map_table[i].gst_fmt) {
607 +      viv_fmt = viv_fmt_map_table[i].viv_fmt;
608 +      break;
609 +    }
610 +  }
611 +
612 +  if (viv_fmt == GL_NONE) {
613 +    GST_ERROR ("Not supported format %d for viv direct texture upload.", fmt);
614 +    return FALSE;
615 +  }
616 +
617 +  GstVivDirectTexture viv_tex = {tex_id, width, height, viv_fmt, vaddr, paddr, FALSE};
618 +  gst_gl_context_thread_add (context, (GstGLContextThreadFunc) _do_viv_direct_tex_bind_mem, &viv_tex);
619 +
620 +  return viv_tex.ret;
621 +}
622 +
623 +gboolean
624  gst_gl_viv_direct_bind_gstbuffer (GstGLContext * context, guint tex_id, GstVideoInfo * info, GstBuffer * buffer)
625  {
626 -  typedef struct {
627 -    guint8 *vaddr;
628 -    guint8 *paddr;
629 -    guint8 *caddr;
630 -    gsize size;
631 -    gpointer *user_data;
632 -  } PhyMemBlock;
633 -  //Note: structure PhyMemBlock is copied from gst1.0-fsl-plugin/libs/allocator/gstallocatorphymem.h
634 -
635 -  typedef struct {
636 -    GstMemory mem;
637 -    guint8 *vaddr;
638 -    guint8 *paddr;
639 -    PhyMemBlock block;
640 -  } GstMemoryPhy;
641 -  //Note: structure GstMemoryPhy is copied from gst1.0-fsl-plugin/libs/allocator/gstallocatorphymem.c
642 -
643 -  GstMemory *mem = gst_buffer_peek_memory (buffer, 0);
644 -  GstMemoryPhy *memphy = (GstMemoryPhy*) mem;
645 -  PhyMemBlock *memblk = &memphy->block;
646 -
647 -  GstVideoFormat fmt = GST_VIDEO_INFO_FORMAT (info);
648 +  PhyMemBlock *memblk;
649 +  GstVideoMeta *vmeta;
650 +  GstVideoFormat fmt;
651    gint width, height;
652 -  GstVideoMeta *vmeta = gst_buffer_get_video_meta (buffer);
653 +
654 +  memblk = gst_buffer_query_phymem_block (buffer);
655 +  if (!memblk)
656 +    return FALSE;
657 +
658 +  width = GST_VIDEO_INFO_WIDTH (info);
659 +  height = GST_VIDEO_INFO_HEIGHT (info);
660 +
661 +  vmeta = gst_buffer_get_video_meta (buffer);
662 +  fmt = GST_VIDEO_INFO_FORMAT (info);
663    if (vmeta && (fmt == GST_VIDEO_FORMAT_I420 || fmt == GST_VIDEO_FORMAT_NV12)) {
664      width = vmeta->stride[0];
665      height = vmeta->offset[1] / width;
666 @@ -100,44 +144,7 @@ gst_gl_viv_direct_bind_gstbuffer (GstGLContext * context, guint tex_id, GstVideo
667      height = GST_VIDEO_INFO_HEIGHT (info);
668    }
669  
670 -  guint viv_fmt;
671 -  switch (fmt) {
672 -    case GST_VIDEO_FORMAT_I420:
673 -      viv_fmt = GL_VIV_I420;
674 -      break;
675 -    case GST_VIDEO_FORMAT_YV12:
676 -      viv_fmt = GL_VIV_YV12;
677 -      break;
678 -    case GST_VIDEO_FORMAT_NV12:
679 -      viv_fmt = GL_VIV_NV12;
680 -      break;
681 -    case GST_VIDEO_FORMAT_NV21:
682 -      viv_fmt = GL_VIV_NV21;
683 -      break;
684 -    case GST_VIDEO_FORMAT_YUY2:
685 -      viv_fmt = GL_VIV_YUY2;
686 -      break;
687 -    case GST_VIDEO_FORMAT_UYVY:
688 -      viv_fmt = GL_VIV_UYVY;
689 -      break;
690 -    case GST_VIDEO_FORMAT_RGBA:
691 -      viv_fmt = GL_RGBA;
692 -      break;
693 -    case GST_VIDEO_FORMAT_BGRA:
694 -      viv_fmt = GL_BGRA_EXT;
695 -      break;
696 -    case GST_VIDEO_FORMAT_RGB16:
697 -      viv_fmt = GL_RGB565_OES;
698 -      break;
699 -    default:
700 -      GST_ERROR ("Not supported format %d for viv direct texture upload.", fmt);
701 -      viv_fmt = GL_NONE;
702 -      return FALSE;
703 -  }
704 -
705 -  GstVivDirectTexture viv_tex = {tex_id, width, height, viv_fmt, memblk->vaddr, memblk->paddr, FALSE};
706 -  gst_gl_context_thread_add (context, (GstGLContextThreadFunc) _do_viv_direct_tex_bind_mem, &viv_tex);
707 -
708 -  return viv_tex.ret;
709 +  return gst_gl_viv_direct_bind_data (context, tex_id, fmt, width, height, memblk->vaddr, memblk->paddr);
710  }
711  
712 +
713 diff --git a/gst-libs/gst/gl/gstglvivdirecttexture.h b/gst-libs/gst/gl/gstglvivdirecttexture.h
714 index fa88e1a..9a2d123 100644
715 --- a/gst-libs/gst/gl/gstglvivdirecttexture.h
716 +++ b/gst-libs/gst/gl/gstglvivdirecttexture.h
717 @@ -28,6 +28,9 @@
718  G_BEGIN_DECLS
719  
720  gboolean gst_is_physical_buffer (GstBuffer *buffer);
721 +gboolean gst_gl_is_directviv_supported_format (GstVideoFormat fmt);
722 +gboolean gst_gl_viv_direct_bind_data (GstGLContext * context, guint tex_id, GstVideoFormat fmt, gint width, gint height, 
723 +    gpointer * vaddr, gpointer *paddr);
724  gboolean gst_gl_viv_direct_bind_gstbuffer (GstGLContext * context, guint tex_id, GstVideoInfo * info, GstBuffer * buffer);
725  
726  G_END_DECLS
727 -- 
728 1.9.1
729