~ubuntu-branches/ubuntu/quantal/gstreamer-vaapi/quantal

« back to all changes in this revision

Viewing changes to gst-libs/gst/vaapi/gstvaapiutils_tsb.c

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2012-02-10 14:35:09 UTC
  • Revision ID: package-import@ubuntu.com-20120210143509-wq9j8uqb5leu1iik
Tags: upstream-0.3.4
ImportĀ upstreamĀ versionĀ 0.3.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  gstvaapiutils_tsb.c - Timestamp buffer store
 
3
 *
 
4
 *  Copyright (C) 2011 Intel Corporation
 
5
 *
 
6
 *  This library is free software; you can redistribute it and/or
 
7
 *  modify it under the terms of the GNU Lesser General Public License
 
8
 *  as published by the Free Software Foundation; either version 2.1
 
9
 *  of the License, or (at your option) any later version.
 
10
 *
 
11
 *  This library is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 *  Lesser General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU Lesser General Public
 
17
 *  License along with this library; if not, write to the Free
 
18
 *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
19
 *  Boston, MA 02110-1301 USA
 
20
 */
 
21
 
 
22
#include "gstvaapiutils_tsb.h"
 
23
#include <glib.h>
 
24
 
 
25
typedef struct _GstVaapiTSBEntry        GstVaapiTSBEntry;
 
26
 
 
27
struct _GstVaapiTSB {
 
28
    GList              *list;
 
29
};
 
30
 
 
31
struct _GstVaapiTSBEntry {
 
32
    GstBuffer          *buffer;
 
33
    guint32             buffer_size;
 
34
    guint32             offset;
 
35
};
 
36
 
 
37
static GstVaapiTSBEntry *
 
38
gst_vaapi_tsb_entry_new(GstBuffer *buffer)
 
39
{
 
40
    GstVaapiTSBEntry *e;
 
41
 
 
42
    e = g_slice_new(GstVaapiTSBEntry);
 
43
    if (!e)
 
44
        return NULL;
 
45
 
 
46
    e->buffer      = gst_buffer_ref(buffer);
 
47
    e->buffer_size = GST_BUFFER_SIZE(buffer);
 
48
    e->offset      = 0;
 
49
    return e;
 
50
}
 
51
 
 
52
static void
 
53
gst_vaapi_tsb_entry_destroy(GstVaapiTSBEntry *e)
 
54
{
 
55
    if (!e)
 
56
        return;
 
57
 
 
58
    if (e->buffer) {
 
59
        gst_buffer_unref(e->buffer);
 
60
        e->buffer = NULL;
 
61
    }
 
62
    g_slice_free(GstVaapiTSBEntry, e);
 
63
}
 
64
 
 
65
/**
 
66
 * gst_vaapi_tsb_new:
 
67
 *
 
68
 * Creates a new #GstVaapiTSB.
 
69
 *
 
70
 * Return value: a new #GstVaapiTSB
 
71
 */
 
72
GstVaapiTSB *
 
73
gst_vaapi_tsb_new()
 
74
{
 
75
    GstVaapiTSB *tsb;
 
76
 
 
77
    tsb = g_new(GstVaapiTSB, 1);
 
78
    if (!tsb)
 
79
        return NULL;
 
80
 
 
81
    tsb->list = NULL;
 
82
    return tsb;
 
83
}
 
84
 
 
85
/**
 
86
 * gst_vaapi_tsb_destroy:
 
87
 * @tsb: a #GstVaapiTSB
 
88
 *
 
89
 * Destroys the #GstVaapiTSB. All buffers are unreferenced.
 
90
 */
 
91
void
 
92
gst_vaapi_tsb_destroy(GstVaapiTSB *tsb)
 
93
{
 
94
    if (!tsb)
 
95
        return;
 
96
 
 
97
    if (tsb->list) {
 
98
        g_list_foreach(tsb->list, (GFunc)gst_vaapi_tsb_entry_destroy, NULL);
 
99
        g_list_free(tsb->list);
 
100
        tsb->list = NULL;
 
101
    }
 
102
    g_free(tsb);
 
103
}
 
104
 
 
105
/**
 
106
 * gst_vaapi_tsb_push:
 
107
 * @tsb: a #GstVaapiTSB
 
108
 * @buffer: a #GstBuffer
 
109
 *
 
110
 * Pushes @buffer to the timestamp buffer store. The TSB owns and
 
111
 * maintains an extra reference to the buffer.
 
112
 *
 
113
 * Return value: %TRUE if success, %FALSE otherwise
 
114
 */
 
115
gboolean
 
116
gst_vaapi_tsb_push(GstVaapiTSB *tsb, GstBuffer *buffer)
 
117
{
 
118
    GList *l;
 
119
    GstVaapiTSBEntry *e;
 
120
 
 
121
    if (!tsb)
 
122
        return FALSE;
 
123
 
 
124
    e = gst_vaapi_tsb_entry_new(buffer);
 
125
    if (!e)
 
126
        return FALSE;
 
127
 
 
128
    l = g_list_append(tsb->list, e);
 
129
    if (!l)
 
130
        return FALSE;
 
131
 
 
132
    tsb->list = l;
 
133
    return TRUE;
 
134
}
 
135
 
 
136
/**
 
137
 * gst_vaapi_tsb_pop:
 
138
 * @tsb: a #GstVaapiTSB
 
139
 * @size: number of bytes to remove from the TSB
 
140
 *
 
141
 * Removes @size bytes from the @tsb.
 
142
 */
 
143
void
 
144
gst_vaapi_tsb_pop(GstVaapiTSB *tsb, gsize size)
 
145
{
 
146
    GList *l;
 
147
    GstVaapiTSBEntry *e;
 
148
    guint32 n;
 
149
 
 
150
    if (!tsb || !tsb->list)
 
151
        return;
 
152
 
 
153
    l = tsb->list;
 
154
    e = l->data;
 
155
    while (size > 0) {
 
156
        n = MIN(e->buffer_size - e->offset, size);
 
157
        e->offset += n;
 
158
        size -= n;
 
159
        if (e->offset == e->buffer_size) {
 
160
            gst_vaapi_tsb_entry_destroy(e);
 
161
            l = l->next;
 
162
            g_list_free_1(tsb->list);
 
163
            tsb->list = l;
 
164
            if (!l)
 
165
                return;
 
166
            e = l->data;
 
167
        }
 
168
    }
 
169
}
 
170
 
 
171
/**
 
172
 * gst_vaapi_tsb_peek:
 
173
 * @tsb: a #GstVaapiTSB
 
174
 *
 
175
 * Returns the current #GstBuffer.
 
176
 *
 
177
 * Return value: current #GstBuffer, or %NULL if none was found
 
178
 */
 
179
GstBuffer *
 
180
gst_vaapi_tsb_peek(GstVaapiTSB *tsb)
 
181
{
 
182
    GstVaapiTSBEntry *e;
 
183
 
 
184
    if (!tsb || !tsb->list)
 
185
        return NULL;
 
186
 
 
187
    e = tsb->list->data;
 
188
    if (!e)
 
189
        return NULL;
 
190
 
 
191
    return e->buffer;
 
192
}
 
193
 
 
194
/**
 
195
 * gst_vaapi_tsb_get_timestamp:
 
196
 * @tsb: a #GstVaapiTSB
 
197
 *
 
198
 * Returns the timestamp for the current #GstBuffer.
 
199
 *
 
200
 * Return value: current #GstBuffer timestamp, or %GST_CLOCK_TIME_NONE if none was found
 
201
 */
 
202
GstClockTime
 
203
gst_vaapi_tsb_get_timestamp(GstVaapiTSB *tsb)
 
204
{
 
205
    GstBuffer *buffer;
 
206
 
 
207
    buffer = gst_vaapi_tsb_peek(tsb);
 
208
    if (!buffer)
 
209
        return GST_CLOCK_TIME_NONE;
 
210
 
 
211
    return GST_BUFFER_TIMESTAMP(buffer);
 
212
}
 
213
 
 
214
/**
 
215
 * gst_vaapi_tsb_get_size:
 
216
 * @tsb: a #GstVaapiTSB
 
217
 *
 
218
 * Returns the size of the #GstVaapiTSB.
 
219
 *
 
220
 * Return value: how many bytes left to consume from @tsb
 
221
 */
 
222
gsize
 
223
gst_vaapi_tsb_get_size(GstVaapiTSB *tsb)
 
224
{
 
225
    GList *l;
 
226
    GstVaapiTSBEntry *e;
 
227
    guint32 size = 0;
 
228
 
 
229
    if (!tsb || !tsb->list)
 
230
        return 0;
 
231
 
 
232
    for (l = tsb->list; l != NULL; l = l->next) {
 
233
        e = l->data;
 
234
        size += e->buffer_size - e->offset;
 
235
    }
 
236
    return size;
 
237
}