2 * Copyright (C) 2009 Ole André Vadla Ravnås <oravnas@cisco.com>
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.
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.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include "coremediabuffer.h"
22 G_DEFINE_TYPE (GstCoreMediaBuffer, gst_core_media_buffer, GST_TYPE_BUFFER);
25 gst_core_media_buffer_init (GstCoreMediaBuffer * self)
27 GST_BUFFER_FLAG_SET (self, GST_BUFFER_FLAG_READONLY);
31 gst_core_media_buffer_finalize (GstMiniObject * mini_object)
33 GstCoreMediaBuffer *self = GST_CORE_MEDIA_BUFFER_CAST (mini_object);
35 if (self->image_buf != NULL) {
36 GstCVApi *cv = self->ctx->cv;
37 cv->CVPixelBufferUnlockBaseAddress (self->image_buf,
38 kCVPixelBufferLock_ReadOnly);
40 self->ctx->cm->FigSampleBufferRelease (self->sample_buf);
41 g_object_unref (self->ctx);
43 GST_MINI_OBJECT_CLASS (gst_core_media_buffer_parent_class)->finalize
48 gst_core_media_buffer_new (GstCoreMediaCtx * ctx, CMSampleBufferRef sample_buf)
50 GstCVApi *cv = ctx->cv;
51 GstCMApi *cm = ctx->cm;
52 CVImageBufferRef image_buf;
53 CVPixelBufferRef pixel_buf;
54 CMBlockBufferRef block_buf;
58 GstCoreMediaBuffer *buf;
60 image_buf = cm->CMSampleBufferGetImageBuffer (sample_buf);
62 block_buf = cm->CMSampleBufferGetDataBuffer (sample_buf);
64 if (image_buf != NULL &&
65 CFGetTypeID (image_buf) == cv->CVPixelBufferGetTypeID ()) {
66 pixel_buf = (CVPixelBufferRef) image_buf;
68 if (cv->CVPixelBufferLockBaseAddress (pixel_buf,
69 kCVPixelBufferLock_ReadOnly) != kCVReturnSuccess) {
73 if (cv->CVPixelBufferIsPlanar (pixel_buf)) {
74 gint plane_count, plane_idx;
76 data = cv->CVPixelBufferGetBaseAddressOfPlane (pixel_buf, 0);
79 plane_count = cv->CVPixelBufferGetPlaneCount (pixel_buf);
80 for (plane_idx = 0; plane_idx != plane_count; plane_idx++) {
81 size += cv->CVPixelBufferGetBytesPerRowOfPlane (pixel_buf, plane_idx) *
82 cv->CVPixelBufferGetHeightOfPlane (pixel_buf, plane_idx);
85 data = cv->CVPixelBufferGetBaseAddress (pixel_buf);
86 size = cv->CVPixelBufferGetBytesPerRow (pixel_buf) *
87 cv->CVPixelBufferGetHeight (pixel_buf);
89 } else if (block_buf != NULL) {
90 status = cm->CMBlockBufferGetDataPointer (block_buf, 0, 0, 0, &data);
93 size = cm->CMBlockBufferGetDataLength (block_buf);
99 GST_CORE_MEDIA_BUFFER (gst_mini_object_new (GST_TYPE_CORE_MEDIA_BUFFER));
100 buf->ctx = g_object_ref (ctx);
101 buf->sample_buf = cm->FigSampleBufferRetain (sample_buf);
102 buf->image_buf = image_buf;
103 buf->pixel_buf = pixel_buf;
104 buf->block_buf = block_buf;
106 GST_BUFFER_DATA (buf) = data;
107 GST_BUFFER_SIZE (buf) = size;
109 return GST_BUFFER_CAST (buf);
116 gst_core_media_buffer_get_pixel_buffer (GstCoreMediaBuffer * buf)
118 return buf->ctx->cv->CVPixelBufferRetain (buf->pixel_buf);
122 gst_core_media_buffer_class_init (GstCoreMediaBufferClass * klass)
124 GstMiniObjectClass *miniobject_class = GST_MINI_OBJECT_CLASS (klass);
126 miniobject_class->finalize = gst_core_media_buffer_finalize;