forked from MaddTheSane/perian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ff_dataref.c
267 lines (224 loc) · 6.48 KB
/
ff_dataref.c
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
/*****************************************************************************
*
* Avi Import Component dataref interface for libavformat
*
* Copyright(C) 2006 Christoph Naegeli <[email protected]>
*
* 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.1 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
*
****************************************************************************/
#include <QuickTime/QuickTime.h>
#include <libavformat/avformat.h>
#include <libavformat/avio_internal.h>
struct _dataref_private {
Handle dataRef;
OSType dataRefType;
DataHandler dh;
int64_t pos;
int64_t size;
unsigned char supportsWideOffsets;
};
typedef struct _dataref_private dataref_private;
/* DataRef Wrapper for QuickTime */
static int dataref_update_filesize(dataref_private *private)
{
wide fsize;
ComponentResult result = DataHGetFileSize64(private->dh, &fsize);
if(result == noErr) {
private->size = (int64_t)fsize.lo;
private->size += (int64_t)fsize.hi << 32;
// Our data handler supports wide offsets. Remember for later use.
private->supportsWideOffsets = 1;
} else {
long size32;
result = DataHGetFileSize(private->dh, &size32);
require_noerr(result, bail);
private->size = size32;
}
bail:
return result;
}
/* !!! THIS FUNCTION ASSUMES h->priv_data IS VALID in contrary to the other open functions
* found in ffmpeg */
static int dataref_open(URLContext *h, const char *filename, int flags)
{
ComponentResult result;
dataref_private *private;
long access = 0;
if((flags & AVIO_FLAG_READ_WRITE) == AVIO_FLAG_READ_WRITE) {
access = kDataHCanRead | kDataHCanWrite;
} else if(flags & AVIO_FLAG_WRITE) {
access = kDataHCanWrite;
} else {
access = kDataHCanRead;
}
private = h->priv_data;
result = OpenAComponent(GetDataHandler(private->dataRef, private->dataRefType, access), &private->dh);
require_noerr(result,bail);
result = DataHSetDataRef(private->dh, private->dataRef);
require_noerr(result,bail);
if(access & kDataHCanRead) {
result = DataHOpenForRead(private->dh);
require_noerr(result,bail);
}
if(access & kDataHCanWrite) {
result = DataHOpenForWrite(private->dh);
require_noerr(result,bail);
}
private->pos = 0ll;
private->size = 0ll;
result = dataref_update_filesize(private);
require_noerr(result,bail);
bail:
return result;
} /* dataref_open() */
static int dataref_read(URLContext *h, unsigned char *buf, int size)
{
int result;
dataref_private *p = (dataref_private*)h->priv_data;
int read;
if (p->pos + size > p->size) {
// it tried to read past the end
// but since we cache the size, it might be wrong
// try to update the size before clipping the request
dataref_update_filesize(p);
}
if (p->pos >= p->size)
return 0; // can't read past the end
else
read = FFMIN(size, p->size - p->pos);
if(p->supportsWideOffsets) {
wide offset;
offset.hi = p->pos >> 32;
offset.lo = (UInt32)p->pos;
result = DataHScheduleData64(p->dh, (Ptr)buf, &offset, (long)read, 0, NULL, NULL);
if (result == badComponentSelector && offset.hi == 0) {
result = DataHScheduleData(p->dh, (Ptr)buf, offset.lo, (long)read, 0, NULL, NULL);
}
} else {
result = DataHScheduleData(p->dh, (Ptr)buf, (long)p->pos, (long)read, 0, NULL, NULL);
}
if(result != noErr) {
read = -1;
goto bail;
}
p->pos += read;
bail:
return (int)read;
} /* dataref_read() */
static int dataref_write(URLContext *h, const unsigned char *buf, int size)
{
int result;
int written = size;
dataref_private *p = (dataref_private*)h->priv_data;
if(p->supportsWideOffsets) {
wide offset;
offset.hi = p->pos >> 32;
offset.lo = (UInt32)p->pos;
result = DataHWrite64(p->dh, (Ptr)buf, &offset, size, NULL, 0);
} else {
result = DataHWrite(p->dh, (Ptr)buf, (long)p->pos, size, NULL, 0);
}
if(result != noErr) {
written = -1;
goto bail;
}
p->pos += written;
bail:
return written;
} /* dataref_write() */
static int64_t dataref_seek(URLContext *h, int64_t pos, int whence)
{
dataref_private *p = (dataref_private*)h->priv_data;
switch(whence) {
case SEEK_SET:
p->pos = pos;
break;
case SEEK_CUR:
p->pos += pos;
break;
case SEEK_END:
dataref_update_filesize(p);
p->pos = p->size + pos;
break;
case AVSEEK_SIZE:
dataref_update_filesize(p);
return p->size;
default:
return -1;
}
return p->pos;
} /* dataref_seek() */
static int dataref_close(URLContext *h)
{
dataref_private *p = (dataref_private*)h->priv_data;
CloseComponent(p->dh);
p->dh = 0;
p->pos = 0;
p->size = 0;
av_free(p);
h->priv_data = NULL;
return 0;
} /* dataref_close() */
URLProtocol dataref_protocol = {
"Data Ref",
dataref_open,
dataref_read,
dataref_write,
dataref_seek,
dataref_close,
};
/* This is the public function to open bytecontext withs datarefs */
OSStatus url_open_dataref(AVIOContext **pb, Handle dataRef, OSType dataRefType, DataHandler *dataHandler, Boolean *wideSupport, int64_t *dataSize)
{
URLContext *uc;
URLProtocol *up;
OSStatus err;
dataref_private *private;
private = av_mallocz(sizeof(dataref_private));
private->dataRef = dataRef;
private->dataRefType = dataRefType;
up = &dataref_protocol;
//FIXME use url_alloc() with better fake protocol, and avoid using avio_internal
uc = av_mallocz(sizeof(URLContext));
if(!uc) {
err = -ENOMEM;
return err;
}
uc->filename = NULL;
uc->prot = up;
uc->flags = AVIO_FLAG_READ; // we're just using the read access...
uc->is_streamed = 0; // not streamed...
uc->max_packet_size = 0; // stream file
uc->priv_data = private;
uc->is_connected = 1;
err = up->url_open(uc, uc->filename, AVIO_FLAG_READ);
if(err < 0) {
av_free(uc);
return err;
}
err = ffio_fdopen(pb, uc);
if(err < 0) {
ffurl_close(uc);
return err;
}
if(dataHandler)
*dataHandler = private->dh;
if(wideSupport)
*wideSupport = private->supportsWideOffsets;
if(dataSize)
*dataSize = private->size;
return noErr;
} /* url_open_dataref() */