aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/ffmpeg-2.1/libavutil/buffer.pas
blob: e4f40fc9ad3662dd3bd4a29746f81404bfaf09a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
(*
 * AVOptions
 * copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * This is a part of Pascal porting of ffmpeg.
 * - Originally by Victor Zinetz for Delphi and Free Pascal on Windows.
 * - For Mac OS X, some modifications were made by The Creative CAT, denoted as CAT
 *   in the source codes.
 * - Changes and updates by the UltraStar Deluxe Team
 *
 * Conversion of libavutil/buffer.h
 * avutil version 52.48.100
 *
 *)

const
  (**
   * Always treat the buffer as read-only, even when it has only one
   * reference.
  *)
  AV_BUFFER_FLAG_READONLY = (1 << 0);

type
  (**
   * @defgroup lavu_buffer AVBuffer
   * @ingroup lavu_data
   *
   * @{
   * AVBuffer is an API for reference-counted data buffers.
   *
   * There are two core objects in this API -- AVBuffer and AVBufferRef. AVBuffer
   * represents the data buffer itself; it is opaque and not meant to be accessed
   * by the caller directly, but only through AVBufferRef. However, the caller may
   * e.g. compare two AVBuffer pointers to check whether two different references
   * are describing the same data buffer. AVBufferRef represents a single
   * reference to an AVBuffer and it is the object that may be manipulated by the
   * caller directly.
   *
   * There are two functions provided for creating a new AVBuffer with a single
   * reference -- av_buffer_alloc() to just allocate a new buffer, and
   * av_buffer_create() to wrap an existing array in an AVBuffer. From an existing
   * reference, additional references may be created with av_buffer_ref().
   * Use av_buffer_unref() to free a reference (this will automatically free the
   * data once all the references are freed).
   *
   * The convention throughout this API and the rest of FFmpeg is such that the
   * buffer is considered writable if there exists only one reference to it (and
   * it has not been marked as read-only). The av_buffer_is_writable() function is
   * provided to check whether this is true and av_buffer_make_writable() will
   * automatically create a new writable buffer when necessary.
   * Of course nothing prevents the calling code from violating this convention,
   * however that is safe only when all the existing references are under its
   * control.
   *
   * @note Referencing and unreferencing the buffers is thread-safe and thus
   * may be done from multiple threads simultaneously without any need for
   * additional locking.
   *
   * @note Two different references to the same buffer can point to different
   * parts of the buffer (i.e. their AVBufferRef.data will not be equal).
   *)

  (**
   * A reference counted buffer type. It is opaque and is meant to be used through
   * references (AVBufferRef).
  *)
  TAVBuffer = record
  end;

  PPAVBufferRef = ^PAVBufferRef;
  PAVBufferRef = ^TAVBufferRef;
  TAVBufferRef = record
    buffer: TAVBuffer;
    (**
     * The data buffer. It is considered writable if and only if
     * this is the only reference to the buffer, in which case
     * av_buffer_is_writable() returns 1.
    *)
    data:   PByte;
    (**
     * Size of data in bytes.
    *)
    size: cint;
  end;

  //callbacks used in the functions av_buffer_create and av_buffer_pool_init respectively
  TFree  = procedure(opaque: pointer; data: pbyte);
  TAlloc = function(size: cint): PAVBufferRef;

  (**
   * The buffer pool. This structure is opaque and not meant to be accessed
   * directly. It is allocated with av_buffer_pool_init() and freed with
   * av_buffer_pool_uninit().
  *)
  PPAVBufferPool = ^PAVBufferPool;
  PAVBufferPool = ^TAVBufferPool;
  TAVBufferPool = record
  end;


  (**
   * Allocate an AVBuffer of the given size using av_malloc().
   *
   * @return an AVBufferRef of given size or NULL when out of memory
  *)
  function av_buffer_alloc(size: cint): PAVBufferRef;
    cdecl; external av__util;

  (**
   * Create an AVBuffer from an existing array.
   *
   * If this function is successful, data is owned by the AVBuffer. The caller may
   * only access data through the returned AVBufferRef and references derived from
   * it.
   * If this function fails, data is left untouched.
   * @param data   data array
   * @param size   size of data in bytes
   * @param free   a callback for freeing this buffer's data
   * @param opaque parameter to be got for processing or passed to free
   * @param flags  a combination of AV_BUFFER_FLAG_*
   *
   * @return an AVBufferRef referring to data on success, NULL on failure.
  *)

  function av_buffer_create(data: PByte; size: cint;
                                free: TFree;
                                opaque: pointer; flags: cint): PAVBufferRef;
    cdecl; external av__util;

  (**
   * Default free callback, which calls av_free() on the buffer data.
   * This function is meant to be passed to av_buffer_create(), not called
   * directly.
  *)
  procedure av_buffer_default_free(opaque: pointer; data: pbyte);
    cdecl; external av__util;

  (**
   * Create a new reference to an AVBuffer.
   *
   * @return a new AVBufferRef referring to the same AVBuffer as buf or NULL on
   * failure.
  *)
  function av_buffer_ref(buf: PAVBufferRef): PAVBufferRef;
    cdecl; external av__util;

  (**
   * Free a given reference and automatically free the buffer if there are no more
   * references to it.
   *
   * @param buf the reference to be freed. The pointer is set to NULL on return.
  *)
  procedure av_buffer_unref(buf: PPAVBufferRef);
    cdecl; external av__util;

  (**
   * @return 1 if the caller may write to the data referred to by buf (which is
   * true if and only if buf is the only reference to the underlying AVBuffer).
   * Return 0 otherwise.
   * A positive answer is valid until av_buffer_ref() is called on buf.
  *)
  function av_buffer_is_writable(buf: {const} PAVBufferRef): cint;
    cdecl; external av__util;

  (**
   * @return the opaque parameter set by av_buffer_create.
   *)
  procedure av_buffer_get_opaque(buf: {const} PAVBufferRef);
    cdecl; external av__util;

  function av_buffer_get_ref_count(buf: {const} PAVBufferRef): cint;
    cdecl; external av__util;

  (**
   * Create a writable reference from a given buffer reference, avoiding data copy
   * if possible.
   *
   * @param buf buffer reference to make writable. On success, buf is either left
   *            untouched, or it is unreferenced and a new writable AVBufferRef is
   *            written in its place. On failure, buf is left untouched.
   * @return 0 on success, a negative AVERROR on failure.
   *)
  function av_buffer_make_writable(buf: PPAVBufferRef): cint;
    cdecl; external av__util;

  (**
   * Reallocate a given buffer.
   *
   * @param buf  a buffer reference to reallocate. On success, buf will be
   *             unreferenced and a new reference with the required size will be
   *             written in its place. On failure buf will be left untouched. *buf
   *             may be NULL, then a new buffer is allocated.
   * @param size required new buffer size.
   * @return 0 on success, a negative AVERROR on failure.
   *
   * @note the buffer is actually reallocated with av_realloc() only if it was
   * initially allocated through av_buffer_realloc(NULL) and there is only one
   * reference to it (i.e. the one passed to this function). In all other cases
   * a new buffer is allocated and the data is copied.
   *)
  function av_buffer_realloc(buf: PPAVBufferRef; size: cint): cint;
    cdecl; external av__util;

  (**
   * @}
   *)

  (**
   * @defgroup lavu_bufferpool AVBufferPool
   * @ingroup lavu_data
   *
   * @{
   * AVBufferPool is an API for a lock-free thread-safe pool of AVBuffers.
   *
   * Frequently allocating and freeing large buffers may be slow. AVBufferPool is
   * meant to solve this in cases when the caller needs a set of buffers of the
   * same size (the most obvious use case being buffers for raw video or audio
   * frames).
   *
   * At the beginning, the user must call av_buffer_pool_init() to create the
   * buffer pool. Then whenever a buffer is needed, call av_buffer_pool_get() to
   * get a reference to a new buffer, similar to av_buffer_alloc(). This new
   * reference works in all aspects the same way as the one created by
   * av_buffer_alloc(). However, when the last reference to this buffer is
   * unreferenced, it is returned to the pool instead of being freed and will be
   * reused for subsequent av_buffer_pool_get() calls.
   *
   * When the caller is done with the pool and no longer needs to allocate any new
   * buffers, av_buffer_pool_uninit() must be called to mark the pool as freeable.
   * Once all the buffers are released, it will automatically be freed.
   *
   * Allocating and releasing buffers with this API is thread-safe as long as
   * either the default alloc callback is used, or the user-supplied one is
   * thread-safe.
   *)

  (**
   * Allocate and initialize a buffer pool.
   *
   * @param size size of each buffer in this pool
   * @param alloc a function that will be used to allocate new buffers when the
   * pool is empty. May be NULL, then the default allocator will be used
   * (av_buffer_alloc()).
   * @return newly created buffer pool on success, NULL on error.
  *)
  function av_buffer_pool_init(size: cint; alloc: TAlloc): PAVBufferRef;
    cdecl; external av__util;

  (**
   * Mark the pool as being available for freeing. It will actually be freed only
   * once all the allocated buffers associated with the pool are released. Thus it
   * is safe to call this function while some of the allocated buffers are still
   * in use.
   *
   * @param pool pointer to the pool to be freed. It will be set to NULL.
   * @see av_buffer_pool_can_uninit()
   *)
  procedure av_buffer_pool_uninit(pool: PPAVBufferPool);
    cdecl; external av__util;

  (**
   * Allocate a new AVBuffer, reusing an old buffer from the pool when available.
   * This function may be called simultaneously from multiple threads.
   *
   * @return a reference to the new buffer on success, NULL on error.
   *)
  function av_buffer_pool_get(pool: PAVBufferPool): PAVBufferRef;
    cdecl; external av__util;

  (**
   * @{
   *)