Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / sys / applemedia / coremediabuffer.c
1 /*
2  * Copyright (C) 2009 Ole André Vadla Ravnås <oravnas@cisco.com>
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
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.
18  */
19
20 #include "coremediabuffer.h"
21
22 G_DEFINE_TYPE (GstCoreMediaBuffer, gst_core_media_buffer, GST_TYPE_BUFFER);
23
24 static void
25 gst_core_media_buffer_init (GstCoreMediaBuffer * self)
26 {
27   GST_BUFFER_FLAG_SET (self, GST_BUFFER_FLAG_READONLY);
28 }
29
30 static void
31 gst_core_media_buffer_finalize (GstMiniObject * mini_object)
32 {
33   GstCoreMediaBuffer *self = GST_CORE_MEDIA_BUFFER_CAST (mini_object);
34
35   if (self->image_buf != NULL) {
36     GstCVApi *cv = self->ctx->cv;
37     cv->CVPixelBufferUnlockBaseAddress (self->image_buf,
38         kCVPixelBufferLock_ReadOnly);
39   }
40   self->ctx->cm->FigSampleBufferRelease (self->sample_buf);
41   g_object_unref (self->ctx);
42
43   GST_MINI_OBJECT_CLASS (gst_core_media_buffer_parent_class)->finalize
44       (mini_object);
45 }
46
47 GstBuffer *
48 gst_core_media_buffer_new (GstCoreMediaCtx * ctx, CMSampleBufferRef sample_buf)
49 {
50   GstCVApi *cv = ctx->cv;
51   GstCMApi *cm = ctx->cm;
52   CVImageBufferRef image_buf;
53   CVPixelBufferRef pixel_buf;
54   CMBlockBufferRef block_buf;
55   Byte *data = NULL;
56   UInt32 size;
57   OSStatus status;
58   GstCoreMediaBuffer *buf;
59
60   image_buf = cm->CMSampleBufferGetImageBuffer (sample_buf);
61   pixel_buf = NULL;
62   block_buf = cm->CMSampleBufferGetDataBuffer (sample_buf);
63
64   if (image_buf != NULL &&
65       CFGetTypeID (image_buf) == cv->CVPixelBufferGetTypeID ()) {
66     pixel_buf = (CVPixelBufferRef) image_buf;
67
68     if (cv->CVPixelBufferLockBaseAddress (pixel_buf,
69             kCVPixelBufferLock_ReadOnly) != kCVReturnSuccess) {
70       goto error;
71     }
72
73     if (cv->CVPixelBufferIsPlanar (pixel_buf)) {
74       gint plane_count, plane_idx;
75
76       data = cv->CVPixelBufferGetBaseAddressOfPlane (pixel_buf, 0);
77
78       size = 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);
83       }
84     } else {
85       data = cv->CVPixelBufferGetBaseAddress (pixel_buf);
86       size = cv->CVPixelBufferGetBytesPerRow (pixel_buf) *
87           cv->CVPixelBufferGetHeight (pixel_buf);
88     }
89   } else if (block_buf != NULL) {
90     status = cm->CMBlockBufferGetDataPointer (block_buf, 0, 0, 0, &data);
91     if (status != noErr)
92       goto error;
93     size = cm->CMBlockBufferGetDataLength (block_buf);
94   } else {
95     goto error;
96   }
97
98   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;
105
106   GST_BUFFER_DATA (buf) = data;
107   GST_BUFFER_SIZE (buf) = size;
108
109   return GST_BUFFER_CAST (buf);
110
111 error:
112   return NULL;
113 }
114
115 CVPixelBufferRef
116 gst_core_media_buffer_get_pixel_buffer (GstCoreMediaBuffer * buf)
117 {
118   return buf->ctx->cv->CVPixelBufferRetain (buf->pixel_buf);
119 }
120
121 static void
122 gst_core_media_buffer_class_init (GstCoreMediaBufferClass * klass)
123 {
124   GstMiniObjectClass *miniobject_class = GST_MINI_OBJECT_CLASS (klass);
125
126   miniobject_class->finalize = gst_core_media_buffer_finalize;
127 }