summaryrefslogtreecommitdiff
path: root/gpr/source/lib/vc5_common/stream.h
blob: df70a4fd6a4bc41b14f056d956c729f7daef200f (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
/*! @file stream.h
 *
 *  @brief The stream abstracts the methods used by bitstreams to output bytes
 *
 *  @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.
 */

#ifndef STREAM_H
#define STREAM_H

//! Type of stream (binary file or memory buffer)
typedef enum _stream_type
{
	STREAM_TYPE_UNKNOWN = 0,		//!< Unknown type of stream
	STREAM_TYPE_FILE,				//!< Simple binary file
	STREAM_TYPE_MEMORY,				//!< Buffer in memory

} STREAM_TYPE;

/*!
	@brief Stream access (read or write)

	The stream provided with the reference decoder only supports
	read access.
*/
typedef enum _stream_access
{
	STREAM_ACCESS_UNKNOWN = 0,
	STREAM_ACCESS_READ,
	STREAM_ACCESS_WRITE,

} STREAM_ACCESS;


/*!
	@brief Declaration of the data structure for a byte stream

	The byte stream encapsulates the location of encoded images and the
	means for reading (writing) encoded images samples.  The byte stream
	could be a binary file that has been opened for reading (writing) or
	a buffer in memory.  The reference codec uses a binary file as the byte
	stream so the functionality for streams attached to memory buffers has
	not been tested.

	It is intended that the byte stream can be enhanced to read (write)
	encoded images from (into) a track in a media container.
*/
#define STREAM_CACHE_SIZE 16
typedef struct _stream
{
	STREAM_TYPE type;		//!< Type of stream (file or memory buffer)
	STREAM_ACCESS access;	//!< Type of access (read or write)

	//! Union of parameters for different types of streams
	union _location
	{
		//! Parameters for a binary file stream
		struct _file
		{
			FILE *iobuf;	//!< Binary file that contains the stream
			BITWORD cache[STREAM_CACHE_SIZE];
			int		cache_index;

		} file;			//!< Parameters for a stream in a binary file

        //! Parameters for a stream bound to a memory buffer
		struct _memory
		{
			void *buffer;	//!< Memory buffer that contains the stream
			size_t size;	//!< Length of the stream (in bytes)

		} memory;		//!< Parameters for a stream in a memory buffer

		//TODO: Add other stream types for media containers

	} location;		//!< Location of the byte stream (file or memory buffer)

	size_t byte_count;		//!< Number of bytes read or written to the stream

} STREAM;

#ifdef __cplusplus
extern "C" {
#endif
    
    CODEC_ERROR OpenStream(STREAM *stream, const char *pathname);

    CODEC_ERROR CreateStream(STREAM *stream, const char *pathname);

    CODEC_ERROR RewindStream(STREAM *stream);

    BITWORD GetWord(STREAM *stream);

    uint8_t GetByte(STREAM *stream);

    CODEC_ERROR SkipBytes(STREAM *stream, size_t size);

    CODEC_ERROR PutWord(STREAM *stream, BITWORD word);

    CODEC_ERROR PutByte(STREAM *stream, uint8_t byte);

    CODEC_ERROR PadBytes(STREAM *stream, size_t size);

    CODEC_ERROR FlushStream(STREAM *stream);

    CODEC_ERROR OpenStreamBuffer(STREAM *stream, void *buffer, size_t size);

    CODEC_ERROR CreateStreamBuffer(STREAM *stream, void *buffer, size_t size);

    CODEC_ERROR GetStreamBuffer(STREAM *stream, void **buffer_out, size_t *size_out);

    CODEC_ERROR GetBlock(STREAM *stream, void *buffer, size_t size, size_t offset);

    CODEC_ERROR PutBlock(STREAM *stream, void *buffer, size_t size, size_t offset);

#ifdef __cplusplus
}
#endif

#endif