summaryrefslogtreecommitdiff
path: root/gpr/source/lib/vc5_common/stream.c
blob: 59ecc7da3f453ead3dc15571bd86ef074964b4e4 (plain)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*! @file stream.h
 *
 *  @brief This module implements a byte stream abstraction that hides the details
 *  of how a stream of bytes is read or written on demand by the bitstream.
 *  The    byte stream can be bound to a binary file opened for reading (writing),
 *  to a buffer in memory, or to a module that reads (writes) a video track
 *  in a media container.
 *
 *  @version 1.0.0
 *
 *  (C) Copyright 2018 GoPro Inc (http://gopro.com/).
 *
 *  Licensed under either:
 *  - Apache License, Version 2.0, http://www.apache.org/licenses/LICENSE-2.0
 *  - MIT license, http://opensource.org/licenses/MIT
 *  at your option.
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include "common.h"

// Local functions
CODEC_ERROR GetBlockFile(STREAM *stream, void *buffer, size_t size, size_t offset);
CODEC_ERROR PutBlockFile(STREAM *stream, void *buffer, size_t size, size_t offset);
CODEC_ERROR GetBlockMemory(STREAM *stream, void *buffer, size_t size, size_t offset);
CODEC_ERROR PutBlockMemory(STREAM *stream, void *buffer, size_t size, size_t offset);

/*!
	@brief Open a stream for reading bytes from the specified file

*/
CODEC_ERROR OpenStream(STREAM *stream, const char *pathname)
{
	assert(stream != NULL);
	if (! (stream != NULL)) {
		return CODEC_ERROR_NULLPTR;
	}

	// Clear all members of the stream data structure
	memset(stream, 0, sizeof(STREAM));

	// Open the file and bind it to the stream
	stream->location.file.iobuf = fopen(pathname, "rb");
	assert(stream->location.file.iobuf != NULL);
	if (! (stream->location.file.iobuf != NULL)) {
		return CODEC_ERROR_OPEN_FILE_FAILED;
	}

	// Set the stream type and access
	stream->type = STREAM_TYPE_FILE;
	stream->access = STREAM_ACCESS_READ;

	// Clear the number of bytes read from the stream
	stream->byte_count = 0;

	return CODEC_ERROR_OKAY;
}

/*!
	@brief Create a stream for writing bytes to a specified file

*/
CODEC_ERROR CreateStream(STREAM *stream, const char *pathname)
{
	assert(stream != NULL);
	if (! (stream != NULL)) {
		return CODEC_ERROR_NULLPTR;
	}

	// Clear all members of the stream data structure
	memset(stream, 0, sizeof(STREAM));

	// Open the file and bind it to the stream
	stream->location.file.iobuf = fopen(pathname, "wb+");
	assert(stream->location.file.iobuf != NULL);
	if (! (stream->location.file.iobuf != NULL)) {
		return CODEC_ERROR_CREATE_FILE_FAILED;
	}

	// Set the stream type and access
	stream->type = STREAM_TYPE_FILE;
	stream->access = STREAM_ACCESS_WRITE;

	// Clear the number of bytes written to the stream
	stream->byte_count = 0;

	return CODEC_ERROR_OKAY;
}

/*!
	@brief Read a word from a byte stream

	This routine is used by the bitstream to read a word from a byte stream.
	A word is the number of bytes that can be stored in the internal buffer
	used by the bitstream.

	@todo Need to modify the routine to return an indication of end of file
	or an error reading from the byte stream.
*/
BITWORD GetWord(STREAM *stream)
{
    BITWORD buffer = 0;
    size_t bytes_read = sizeof(buffer);
    
    assert(stream != NULL);
    
    switch (stream->type)
    {
        case STREAM_TYPE_FILE:
            bytes_read = fread(&buffer, 1, sizeof(buffer), stream->location.file.iobuf);
            assert(bytes_read == sizeof(buffer));
            break;
            
        case STREAM_TYPE_MEMORY:
            memcpy(&buffer, (uint8_t *)stream->location.memory.buffer + stream->byte_count, sizeof(buffer));
            break;
            
        default:
            assert(0);
            break;
    }
    
    if (bytes_read > 0)
        stream->byte_count += sizeof(buffer);
    
	return buffer;
}

/*!
	@brief Read a byte from a byte stream
*/
uint8_t GetByte(STREAM *stream)
{
    assert(stream != NULL);
    int byte = 0;
    
    switch (stream->type)
    {
        case STREAM_TYPE_FILE:
            byte = fgetc(stream->location.file.iobuf);
            break;
            
        case STREAM_TYPE_MEMORY:
            byte = ((uint8_t *)stream->location.memory.buffer)[stream->byte_count];            
            break;
            
        default:
            assert(0);
            break;
    }
    
    stream->byte_count++;
    assert(byte >= 0 && (byte & ~0xFF) == 0);
    
	return (uint8_t)byte;
}

/*!
	@brief Write a word to a byte stream

	This routine is used by the bitstream to write a word to a byte stream.
	A word is the number of bytes that can be stored in the internal buffer
	used by the bitstream.

	@todo Need to modify the routine to return an indication of an error
	writing to the byte stream.
*/
CODEC_ERROR PutWord(STREAM *stream, BITWORD word)
{
	size_t written;

    word = Swap32(word);
    
	assert(stream != NULL);
    
	switch (stream->type)
	{
	case STREAM_TYPE_FILE:
		written = fwrite(&word, sizeof(word), 1, stream->location.file.iobuf);
		if (written == 0)
			return CODEC_ERROR_FILE_WRITE_FAILED;
		break;

	case STREAM_TYPE_MEMORY:
        {
            uint8_t* buffer = (uint8_t *)stream->location.memory.buffer + stream->byte_count;
            
            memcpy(buffer, &word, sizeof(word));            
        }
		break;

	default:
		assert(0);
		break;
	}

	stream->byte_count += sizeof(word);
    
	return CODEC_ERROR_OKAY;
}

/*!
	@brief Write a byte to a byte stream
*/
CODEC_ERROR PutByte(STREAM *stream, uint8_t byte)
{
	assert(stream != NULL);

	//assert(byte >= 0 && (byte & ~0xFF) == 0);

	switch (stream->type)
	{
	case STREAM_TYPE_FILE:
		if (fputc(byte, stream->location.file.iobuf) == EOF)
			return CODEC_ERROR_FILE_WRITE_FAILED;
		break;

	case STREAM_TYPE_MEMORY:
		((uint8_t *)stream->location.memory.buffer)[stream->byte_count] = byte;
		break;

	default:
		assert(0);
		break;
	}

	stream->byte_count++;

	return CODEC_ERROR_OKAY;
}

/*!
	@brief Rewind the stream to the beginning of the buffer or file
*/
CODEC_ERROR RewindStream(STREAM *stream)
{
	assert(stream != NULL);
	if (! (stream != NULL)) {
		return CODEC_ERROR_NULLPTR;
	}
	
    if( stream->type == STREAM_TYPE_FILE )
    {
        if (stream->location.file.iobuf != NULL) {
            assert(fseek(stream->location.file.iobuf, 0, SEEK_SET) == 0);
            return CODEC_ERROR_BITSTREAM;
        }
    }

	stream->byte_count = 0;

	return CODEC_ERROR_OKAY;
}

/*!
	@brief Skip the specified number of bytes in the stream
*/
CODEC_ERROR SkipBytes(STREAM *stream, size_t size)
{
	for (; size > 0; size--)
	{
		(void)GetByte(stream);
	}
	return CODEC_ERROR_OKAY;
}

/*!
	@brief Pad the specified number of bytes in the stream
*/
CODEC_ERROR PadBytes(STREAM *stream, size_t size)
{
	const uint8_t byte = 0;
	for (; size > 0; size--)
	{
		PutByte(stream, byte);
	}
	return CODEC_ERROR_OKAY;
}

/*!
	@brief Write the stream buffer to the file
*/
CODEC_ERROR FlushStream(STREAM *stream)
{
	assert(stream != NULL);
	if (! (stream != NULL)) {
		return CODEC_ERROR_NULLPTR;
	}

	if (stream->type == STREAM_TYPE_FILE)
	{
		int result = fflush(stream->location.file.iobuf);
		if (result != 0) {
			return CODEC_ERROR_FILE_FLUSH_FAILED;
		}
	}

	return CODEC_ERROR_OKAY;
}

/*!
	@brief Create a byte stream for reading from a memory location
 */
CODEC_ERROR OpenStreamBuffer(STREAM *stream, void *buffer, size_t size)
{
    assert(stream != NULL);
    if (! (stream != NULL)) {
        return CODEC_ERROR_NULLPTR;
    }
    
    // Clear all members of the stream data structure
    memset(stream, 0, sizeof(STREAM));
    
    // Bind the stream to the buffer
    stream->location.memory.buffer = buffer;
    stream->location.memory.size = size;
    
    // Set the stream type and access
    stream->type = STREAM_TYPE_MEMORY;
    stream->access = STREAM_ACCESS_READ;
    
    // Clear the number of bytes written to the stream
    stream->byte_count = 0;
    
    return CODEC_ERROR_OKAY;
}

/*!
	@brief Create a byte stream for writing to a memory location
*/
CODEC_ERROR CreateStreamBuffer(STREAM *stream, void *buffer, size_t size)
{
	assert(stream != NULL);
	if (! (stream != NULL)) {
		return CODEC_ERROR_NULLPTR;
	}

	// Clear all members of the stream data structure
	memset(stream, 0, sizeof(STREAM));

	// Bind the stream to the buffer
	stream->location.memory.buffer = buffer;
	stream->location.memory.size = size;

	// Set the stream type and access
	stream->type = STREAM_TYPE_MEMORY;
	stream->access = STREAM_ACCESS_WRITE;

	// Clear the number of bytes written to the stream
	stream->byte_count = 0;
    
	return CODEC_ERROR_OKAY;
}

/*!
	@brief Return the starting address and number of bytes in a buffer

	This routine is used to get the address and count of the bytes written
	to a memory stream (buffer).
*/
CODEC_ERROR GetStreamBuffer(STREAM *stream, void **buffer_out, size_t *size_out)
{
	assert(stream != NULL);
	if (! (stream != NULL)) {
		return CODEC_ERROR_NULLPTR;
	}

	assert(stream->type == STREAM_TYPE_MEMORY);

	if (buffer_out != NULL) {
		*buffer_out = stream->location.memory.buffer;
	}

	if (size_out != NULL) {
		*size_out = stream->byte_count;
	}

	return CODEC_ERROR_OKAY;
}

/*!
	@brief Read a block of data at the specified offset in the byte stream
*/
CODEC_ERROR GetBlock(STREAM *stream, void *buffer, size_t size, size_t offset)
{
	switch (stream->type)
	{
	case STREAM_TYPE_FILE:
		return GetBlockFile(stream, buffer, size, offset);
		break;

	case STREAM_TYPE_MEMORY:
		return GetBlockMemory(stream, buffer, size, offset);
		break;

	case STREAM_TYPE_UNKNOWN:
		assert(0);
		break;
	}

	return CODEC_ERROR_UNEXPECTED;
}

/*!
	@brief Read a block of data from a file stream
*/
CODEC_ERROR GetBlockFile(STREAM *stream, void *buffer, size_t size, size_t offset)
{
	FILE *file = stream->location.file.iobuf;
	fpos_t position;

    (void)file;
    (void)position;
    
	// Save the current position in the file
	if (fgetpos(file, &position) != 0) {
		return CODEC_ERROR_FILE_GET_POSITION;
	}

	// Seek to the specified offset
	assert(offset <= LONG_MAX);
	if (fseek(file, (long)offset, SEEK_SET) != 0) {
		return CODEC_ERROR_FILE_SEEK;
	}

	// Read data from the file
	if (fread(buffer, size, 1, file) != 1) {
		return CODEC_ERROR_FILE_READ;
	}

	// Return to the previous position in the file
	// if (fseek(file, (long)position, SEEK_SET) != 0) {
	if (fsetpos(file, &position) != 0) {
		return CODEC_ERROR_FILE_SEEK;
	}

	return CODEC_ERROR_OKAY;
}

/*!
	@brief Read a block of data from a memory stream (buffer)
*/
CODEC_ERROR GetBlockMemory(STREAM *stream, void *buffer, size_t size, size_t offset)
{
	uint8_t *block = (uint8_t *)stream->location.memory.buffer + offset;
	memcpy(buffer, block, size);
	return CODEC_ERROR_OKAY;
}

/*!
	@brief Write a block of data at the specified offset in the byte stream
*/
CODEC_ERROR PutBlock(STREAM *stream, void *buffer, size_t size, size_t offset)
{
	switch (stream->type)
	{
	case STREAM_TYPE_FILE:
		return PutBlockFile(stream, buffer, size, offset);
		break;

	case STREAM_TYPE_MEMORY:
		return PutBlockMemory(stream, buffer, size, offset);
		break;

	case STREAM_TYPE_UNKNOWN:
		assert(0);
		break;
	}

	return CODEC_ERROR_UNEXPECTED;
}

/*!
	@brief Write a block of data at the specified offset in a file stream
*/
CODEC_ERROR PutBlockFile(STREAM *stream, void *buffer, size_t size, size_t offset)
{
	FILE *file = stream->location.file.iobuf;
	fpos_t position;

    (void)file;
    (void)position;
    
	// Save the current position in the file
	if (fgetpos(file, &position) != 0) {
		return CODEC_ERROR_FILE_GET_POSITION;
	}

	// Seek to the specified offset and write to the file
	assert(offset <= LONG_MAX);
	if (fseek(file, (long)offset, SEEK_SET) != 0) {
		return CODEC_ERROR_FILE_SEEK;
	}

	// Write data to the file
	if (fwrite(buffer, size, 1, file) != 1) {
		return CODEC_ERROR_FILE_WRITE;
	}

	// Return to the previous position in the file
	// if (fseek(file, (long)position, SEEK_SET) != 0) {
	if (fsetpos(file, &position) != 0) {
		return CODEC_ERROR_FILE_SEEK;
	}

	return CODEC_ERROR_OKAY;
}

/*!
	@brief Write a block of data at the specified offset in a memory stream
*/
CODEC_ERROR PutBlockMemory(STREAM *stream, void *buffer, size_t size, size_t offset)
{
	uint8_t *block = (uint8_t *)stream->location.memory.buffer + offset;
	memcpy(block, buffer, size);
	return CODEC_ERROR_OKAY;
}