summaryrefslogtreecommitdiff
path: root/audio_codec/libraac/rm_parser_internal.h (plain)
blob: 4bf95a1d92992f2db853985e84af4d8db3ad4d9c
1/* ***** BEGIN LICENSE BLOCK *****
2 * Source last modified: $Id: rm_parser_internal.h,v 1.2.2.1 2005/05/04 18:21:36 hubbe Exp $
3 *
4 * REALNETWORKS CONFIDENTIAL--NOT FOR DISTRIBUTION IN SOURCE CODE FORM
5 * Portions Copyright (c) 1995-2005 RealNetworks, Inc.
6 * All Rights Reserved.
7 *
8 * The contents of this file, and the files included with this file,
9 * are subject to the current version of the Real Format Source Code
10 * Porting and Optimization License, available at
11 * https://helixcommunity.org/2005/license/realformatsource (unless
12 * RealNetworks otherwise expressly agrees in writing that you are
13 * subject to a different license). You may also obtain the license
14 * terms directly from RealNetworks. You may not use this file except
15 * in compliance with the Real Format Source Code Porting and
16 * Optimization License. There are no redistribution rights for the
17 * source code of this file. Please see the Real Format Source Code
18 * Porting and Optimization License for the rights, obligations and
19 * limitations governing use of the contents of the file.
20 *
21 * RealNetworks is the developer of the Original Code and owns the
22 * copyrights in the portions it created.
23 *
24 * This file, and the files included with this file, is distributed and
25 * made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND,
26 * EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL
27 * SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT
29 * OR NON-INFRINGEMENT.
30 *
31 * Technology Compatibility Kit Test Suite(s) Location:
32 * https://rarvcode-tck.helixcommunity.org
33 *
34 * Contributor(s):
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38#ifndef RM_PARSER_INTERNAL_H
39#define RM_PARSER_INTERNAL_H
40
41#include "../include/helix_types.h"
42#include "../include/helix_result.h"
43#include "../include/rm_memory.h"
44#include "../include/rm_error.h"
45#include "../include/rm_parse.h"
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50
51 /* Defines */
52#define RM_HEADER_OBJECT 0x2E524D46 /* '.RMF' */
53#define RM_PROPERTIES_OBJECT 0x50524F50 /* 'PROP' */
54#define RM_MEDIA_PROPERTIES_OBJECT 0x4D445052 /* 'MDPR' */
55#define RM_CONTENT_OBJECT 0x434F4E54 /* 'CONT' */
56#define RM_DATA_OBJECT 0x44415441 /* 'DATA' */
57#define RM_INDEX_OBJECT 0x494E4458 /* 'INDX' */
58#define RM_MULTIHEADER_OBJECT 0x4D4C5449 /* 'MLTI' */
59
60#define RM_NO_STREAM_SET 0xFFFFFFFF
61#define RM_INDEX_MIN_ENTRIES 32
62#define RM_INDEX_MAX_ENTRIES 2048
63#define RM_INDEX_MIN_TIME_GRANULARITY 50 /* in milliseconds */
64#define RM_INDEX_MAX_TIME_GRANULARITY 15000 /* in milliseconds */
65
66 /*
67 * We explicitly define the size for structs
68 * to be read. That's because we can't use sizeof()
69 * to determine how much to read since struct
70 * alignment will make that value different.
71 */
72#define RM_PARSER_GENERIC_HDR_SIZE 10
73#define RM_PARSER_INDEX_HEADER_SIZE 20
74#define RM_PARSER_INDEX_RECORD_SIZE 14
75#define RM_PARSER_PACKET_HEADER_SIZE 12
76#define RM_PARSER_DATA_CHUNK_HEADER_SIZE 18
77
78 /* generic header -- most headers start like this */
79 struct rm_generic_hdr {
80 UINT32 id; /* unique identifier for this header */
81 UINT32 size; /* size of header in file */
82 UINT16 version; /* struct version */
83 };
84
85 /* rm file header -- all real media files start with this header */
86 struct rm_file_hdr {
87 UINT32 id; /* unique identifier for this header */
88 UINT32 size; /* size of header in file */
89 UINT16 version; /* struct version */
90 UINT32 file_version; /* revision number for realmedia file */
91 UINT32 num_headers; /* num headers in file (including this one) */
92 };
93
94 /* rm properties header -- summarizes data for entire clip */
95 struct rm_properties_hdr {
96 UINT32 id; /* unique identifier for this header */
97 UINT32 size; /* size of header in file */
98 UINT16 version; /* struct version */
99 UINT32 max_bit_rate; /* maximum bit rate of clip */
100 UINT32 avg_bit_rate; /* average bit rate of clip */
101 UINT32 max_pkt_size; /* max length of packet in clip (bytes) */
102 UINT32 avg_pkt_size; /* averge length of packet in clip (bytes) */
103 UINT32 num_pkts; /* total packets in clip (all streams) */
104 UINT32 duration; /* duration of clip (milliseconds) */
105 UINT32 preroll; /* preroll time (milliseconds) */
106 UINT32 index_offset; /* offset to beginning of index data */
107 UINT32 data_offset; /* offset to beginning of packet data */
108 UINT16 num_streams; /* total number of streams in clip */
109 UINT32 flags; /* various and sundry */
110 };
111
112 /* rm content header -- title, author, copyright */
113 struct rm_content_hdr {
114 UINT32 id; /* unique identifier for this header */
115 UINT32 size; /* size of header in file */
116 UINT16 version; /* struct version */
117 UINT16 title_sz; /* length of title field */
118 char* title; /* title of clip */
119 UINT16 author_sz; /* length of author field */
120 char* author; /* author of clip */
121 UINT16 copyright_sz; /* length of copyright field */
122 char* copyright; /* copyright of clip */
123 UINT16 comment_sz; /* length of comment field */
124 char* comment; /* comment */
125 };
126
127 /* rm media properties header -- stream info, one for each stream */
128 struct rm_media_props_hdr {
129 UINT32 id; /* unique identifier for this header */
130 UINT32 size; /* size of header in file */
131 UINT16 version; /* struct version */
132 UINT16 stream_num; /* stream number */
133 UINT32 max_bit_rate; /* maximum bit rate of stream */
134 UINT32 avg_bit_rate; /* average bit rate of stream */
135 UINT32 max_pkt_size; /* max length of packet in stream (bytes) */
136 UINT32 avg_pkt_size; /* averge length of packet in stream (bytes) */
137 UINT32 start_time; /* start time of stream -- clip relative */
138 UINT32 preroll; /* preroll time (milliseconds) */
139 UINT32 duration; /* duration of stream (milliseconds) */
140 BYTE stream_name_sz; /* length of stream_name field */
141 char* stream_name; /* string name of stream */
142 BYTE mime_type_sz; /* length of mime_type field */
143 char* mime_type; /* MIME type of stream */
144 UINT32 type_spec_sz; /* length of type_spec field */
145 BYTE* type_spec; /* type specific data buffer */
146 UINT32 start_offset; /* for non-interleaved stream only */
147 };
148
149 /* rm name value header -- implements name/value pairs */
150 struct rm_name_value_map {
151 UINT32 size; /* size of header in file */
152 UINT16 version; /* struct version */
153 BYTE name_sz; /* length of name field */
154 char* name; /* name of property */
155 UINT32 type; /* type of property */
156 UINT16 value_sz; /* length of value field */
157 BYTE* value; /* value of property */
158 };
159
160 /* rm logical stream header -- used for surestream */
161 struct rm_logical_stream_hdr {
162 UINT32 size; /* size of header in file */
163 UINT16 version; /* struct version */
164 UINT16 num_physical_streams; /* number of actual streams */
165 UINT16* physical_stream_num; /* array of stream numbers */
166 UINT32* data_offsets; /* array of offsets to stream packets */
167 UINT16 num_rules; /* number of ASM rules */
168 UINT16* rule_stream_map; /* maps rules to streams */
169 UINT16 num_props; /* number of name value properties */
170 struct rm_name_value_map* props; /* array of name value properties */
171 };
172
173 /* rm meta information -- clip meta data */
174 struct rm_meta_data_hdr {
175 UINT32 size; /* size of header in file */
176 UINT16 version; /* struct version */
177 UINT16 num_props; /* number of name value properties */
178 struct rm_name_value_map* props; /* array of name value properties */
179 };
180
181 /* rm data header -- this occurs before the start of packet data */
182 struct rm_data_hdr {
183 UINT32 id; /* unique identifier for this header */
184 UINT32 size; /* size of header in file */
185 UINT16 version; /* struct version */
186 UINT32 num_pkts; /* total number of packets in segment */
187 UINT32 next_data_hdr; /* offset of next segment */
188 };
189
190 /* rm index header -- this occurs before the start of index data */
191 struct rm_index_hdr {
192 UINT32 id; /* unique identifier for this header */
193 UINT32 size; /* size of header in file */
194 UINT16 version; /* struct version */
195 UINT32 num_recs; /* number of index records */
196 UINT16 stream_num; /* stream number being indexed */
197 UINT32 next_index_hdr; /* offset of next header */
198 };
199
200 /* rm index record -- describes an index entry */
201 struct rm_index_rec {
202 UINT16 version; /* record version */
203 UINT32 timestamp; /* packet timestamp */
204 UINT32 offset; /* offset of packet in file */
205 UINT32 num_pkts; /* number of packets before this one in clip */
206 };
207
208 /* rm packet header */
209 struct rm_pkt_hdr_base {
210 UINT16 version; /* struct version */
211 UINT16 length; /* length of header */
212 };
213
214 struct rm_pkt_hdr {
215 UINT16 version; /* struct version */
216 UINT16 length; /* length of header */
217 UINT16 stream_num; /* stream number */
218 UINT32 timestamp; /* packet timestamp */
219 UINT16 flags; /* keyframe, etc. */
220 BYTE header_len;
221 };
222
223 struct rm_pkt_hdr1 {
224 UINT16 version; /* struct version */
225 UINT16 length; /* length of header */
226 UINT16 stream_num; /* stream number */
227 UINT32 timestamp; /* packet timestamp */
228 UINT16 asm_rule; /* for surestream */
229 BYTE asm_flags;
230 };
231
232 /*
233 * Internal struct which tracks stream info
234 */
235 struct rm_seek_table_entry {
236 UINT32 ulTime;
237 UINT32 ulOffset;
238 };
239
240 struct rm_seek_table {
241 struct rm_seek_table_entry* pEntry;
242 UINT32 ulMaxEntries;
243 UINT32 ulNumEntries;
244 UINT32 ulLastTime;
245 UINT32 ulRangeTime;
246 UINT32 ulTimeGranularity;
247 };
248
249 struct rm_keyframe_packet {
250 UINT32 ulTimestamp;
251 UINT32 ulFileOffset;
252 HXBOOL bValid;
253 };
254
255 struct rm_stream_info {
256 UINT32 ulLastTimeStamp;
257 UINT32 ulLastRule;
258 struct rm_keyframe_packet keyFramePacket;
259 struct rm_seek_table seekTable;
260 HX_BITFIELD bIsRealAudio : 1;
261 HX_BITFIELD bIsRealVideo : 1;
262 HX_BITFIELD bIsRealEvent : 1;
263 HX_BITFIELD bStreamDone : 1;
264 HX_BITFIELD bNeedKeyframe : 1;
265 HX_BITFIELD bSeenFirstPacketTimeStamp : 1;
266 };
267
268 /*
269 * Parser struct
270 */
271 typedef struct rm_parser_internal_struct {
272 rm_error_func_ptr fpError;
273 void* pUserError;
274 rm_malloc_func_ptr fpMalloc;
275 rm_free_func_ptr fpFree;
276 void* pUserMem;
277 rm_read_func_ptr fpRead;
278 rm_seek_func_ptr fpSeek;
279 void* pUserRead;
280 struct rm_file_hdr fileHdr;
281 struct rm_properties_hdr propHdr;
282 struct rm_content_hdr contHdr;
283 UINT32 ulNumStreams;
284 UINT32 ulNumMediaPropsHdrs;
285 UINT32 ulNumMediaPropsHdrsAlloc;
286 struct rm_media_props_hdr* pMediaPropsHdr;
287 UINT32 ulNumLogicalStreamHdrs;
288 struct rm_logical_stream_hdr* pLogicalStreamHdr;
289 struct rm_logical_stream_hdr* pLogicalFileInfo;
290 BYTE* pReadBuffer; /* Read buffer */
291 UINT32 ulReadBufferSize; /* Allocated size of the read buffer */
292 UINT32 ulNumBytesRead; /* Current number of bytes read into the buffer */
293 UINT32 ulCurFileOffset; /* Current read offset into the file */
294 UINT32 ulStreamNumMapSize;
295 UINT32* pulStreamNumMap;
296 UINT32 ulMaxDuration;
297 struct rm_stream_info* pStreamInfo;
298 struct rm_data_hdr* pDataHdr;
299 HXBOOL bIsRealDataType;
300 UINT32 ulMinFirstPacketTime;
301 rm_stream_header* pStreamHdr;
302 UINT32 ulKeyframesNeeded;
303 UINT32 ulInterleavedStreamsFlag;
304 UINT32 ulCurrentStream;
305 } rm_parser_internal;
306
307 /*
308 * Internal parser functions
309 */
310 HX_RESULT rm_parseri_unpack_generic_hdr(rm_parser_internal* pInt, struct rm_generic_hdr* h);
311 HX_RESULT rm_parseri_unpack_file_hdr(rm_parser_internal* pInt);
312 HX_RESULT rm_parseri_unpack_properties_hdr(rm_parser_internal* pInt);
313 HX_RESULT rm_parseri_unpack_content_hdr(rm_parser_internal* pInt);
314 void rm_parseri_cleanup_content_hdr(rm_parser_internal* pInt);
315 HX_RESULT rm_parseri_unpack_media_props_hdr(rm_parser_internal* pInt,
316 struct rm_media_props_hdr* h);
317 void rm_parseri_cleanup_media_props_hdr(rm_parser_internal* pInt,
318 struct rm_media_props_hdr* h);
319 void rm_parseri_cleanup_all_media_props_hdrs(rm_parser_internal* pInt);
320 HX_RESULT rm_parseri_unpack_name_value_map(rm_parser_internal* pInt,
321 struct rm_name_value_map* pMap,
322 BYTE** ppBuf, UINT32* pulLen);
323 void rm_parseri_cleanup_name_value_map(rm_parser_internal* pInt,
324 struct rm_name_value_map* h);
325 void rm_parseri_cleanup_all_name_value_maps(rm_parser_internal* pInt,
326 struct rm_logical_stream_hdr* h);
327 HX_RESULT rm_parseri_unpack_logical_stream_hdr(rm_parser_internal* pInt,
328 struct rm_media_props_hdr* mh,
329 struct rm_logical_stream_hdr* h);
330 UINT32 rm_parseri_get_num_logical_streams(rm_parser_internal* pInt);
331 HX_RESULT rm_parseri_unpack_all_logical_stream_hdrs(rm_parser_internal* pInt);
332 void rm_parseri_cleanup_logical_stream_hdr(rm_parser_internal* pInt,
333 struct rm_logical_stream_hdr* h);
334 void rm_parseri_cleanup_all_logical_stream_hdrs(rm_parser_internal* pInt);
335 HXBOOL rm_parseri_is_logical_fileinfo_present(rm_parser_internal* pInt, UINT32* pulIndx);
336 void rm_parseri_cleanup_logical_fileinfo_hdr(rm_parser_internal* pInt);
337 HX_RESULT rm_parseri_allocate_media_props_hdrs(rm_parser_internal* pInt);
338 void rm_parseri_cleanup_read_buffer(rm_parser_internal* pInt);
339 UINT32 rm_parseri_get_media_props_hdr_stream_num(rm_parser_internal* pInt);
340 HXBOOL rm_parseri_is_logical_stream_mime_type(const char* pszMimeType);
341 HXBOOL rm_parseri_is_logical_fileinfo_mime_type(const char* pszMimeType);
342 HX_RESULT rm_parseri_read_next_header(rm_parser_internal* pInt, UINT32* pulID);
343 HX_RESULT rm_parseri_setup_interleaved_streams(rm_parser_internal* pInt);
344 HX_RESULT rm_parseri_read_all_headers(rm_parser_internal* pInt);
345 UINT32 rm_parseri_get_num_file_properties(rm_parser_internal* pInt);
346 HX_RESULT rm_parseri_get_file_properties(rm_parser_internal* pInt,
347 rm_property* pProp, UINT32 ulNumProp);
348 HXBOOL rm_parseri_has_real_data_type(rm_parser_internal* pInt);
349 HX_RESULT rm_parseri_set_rm_property(rm_parser_internal* pInt, rm_property* pProp,
350 const char* pszName, UINT32 ulType,
351 void* pValue, UINT32 ulValueLen);
352 void rm_parseri_cleanup_rm_property(rm_parser_internal* pInt, rm_property* pProp);
353 void rm_parseri_cleanup_stream_num_map(rm_parser_internal* pInt);
354 void rm_parseri_cleanup_stream_info(rm_parser_internal* pInt, struct rm_stream_info* pInfo);
355 void rm_parseri_cleanup_stream_info_array(rm_parser_internal* pInt);
356 HX_RESULT rm_parseri_create_stream_structures(rm_parser_internal* pInt);
357 HX_RESULT rm_parseri_examine_initial_packets(rm_parser_internal* pInt, UINT32 StreamNum);
358 HX_RESULT rm_parseri_unpack_data_hdr(rm_parser_internal* pInt, UINT32 StreamNum);
359 HX_RESULT rm_parseri_unpack_pkt_hdr(rm_parser_internal* pInt,
360 struct rm_pkt_hdr* pPktHdr);
361 HX_RESULT rm_parseri_unpack_index_hdr(rm_parser_internal* pInt,
362 struct rm_index_hdr* hdr);
363 HX_RESULT rm_parseri_unpack_index_rec(rm_parser_internal* pInt,
364 struct rm_index_rec* rec);
365 HX_RESULT rm_parseri_read_next_index_rec(rm_parser_internal* pInt,
366 struct rm_index_rec* rec);
367 UINT32 rm_parseri_translate_stream_number(rm_parser_internal* pInt, UINT32 ulNum);
368 HX_RESULT rm_parseri_create_all_stream_headers(rm_parser_internal* pInt);
369 HX_RESULT rm_parseri_create_stream_header(rm_parser_internal* pInt, UINT32 i,
370 rm_stream_header* hdr);
371 void rm_parseri_cleanup_all_stream_headers(rm_parser_internal* pInt);
372 void rm_parseri_cleanup_stream_header(rm_parser_internal* pInt, rm_stream_header* hdr);
373 UINT32 rm_parseri_count_set_stream_header_props(rm_parser_internal* pInt, UINT32 i,
374 rm_stream_header* hdr,
375 rm_property* pProp, UINT32 ulNumProps);
376 char* rm_parseri_create_asm_rulebook(rm_parser_internal* pInt, HXBOOL bIsVideo,
377 HXBOOL bIsEvent, HXBOOL bHasOpaqueData,
378 UINT32 ulMaxBitRate, UINT32 ulAvgBitRate,
379 HXBOOL* pbHasPreDataProps);
380 HX_RESULT rm_parseri_read_next_packet(rm_parser_internal* pInt, rm_packet** ppPacket);
381 UINT32 rm_parseri_file_read(rm_parser_internal* pInt, UINT32 ulBytesToRead, UINT32 ulReadBufferOffset);
382 UINT32 rm_parseri_file_read_buffer(rm_parser_internal* pInt, BYTE* pBuf, UINT32 ulBytesToRead);
383 void rm_parseri_file_seek(rm_parser_internal* pInt, UINT32 ulOffset, UINT32 ulOrigin);
384 void rm_parseri_set_stream_size(rm_parser_internal* pInt, UINT32 stream_size);
385 HX_RESULT rm_parseri_copy_stream_header(rm_parser_internal* pInt, UINT32 i, rm_stream_header* pHdr);
386 HX_RESULT rm_parseri_update_seek_table(rm_parser_internal* pInt, UINT32 ulStreamNum,
387 UINT32 ulTime, UINT32 ulOffset, UINT32 ulFlags);
388 HX_RESULT rm_parseri_update_time_range(rm_parser_internal* pInt, UINT32 ulStreamNum,
389 UINT32 ulTime);
390 HX_RESULT rm_parseri_search_all_seek_tables(rm_parser_internal* pInt, UINT32 ulSeekTime,
391 UINT32* pulFoundTime, UINT32* pulFoundOffset);
392 HX_RESULT rm_parseri_search_seek_table(struct rm_seek_table* pTable, UINT32 ulSeekTime, INT32 lDirection,
393 UINT32* pulFoundTime, UINT32* pulFoundOffset, UINT32* pulFoundIndex);
394 HX_RESULT rm_parseri_search_index_chunk(rm_parser_internal* pInt, UINT32 ulSeekTime);
395 HX_RESULT rm_parseri_seek(rm_parser_internal* pInt, UINT32 ulSeekTime);
396 HXBOOL rm_parseri_is_all_keyframes_found(rm_parser_internal* pInt);
397 HXBOOL rm_parseri_is_a_keyframe_found(rm_parser_internal* pInt);
398 HX_RESULT rm_parseri_find_first_packet_after_seek_time(rm_parser_internal* pInt,
399 UINT32 ulSeekTime,
400 UINT32 ulInitialOffset);
401 HX_RESULT rm_parseri_find_first_keyframe(rm_parser_internal* pInt);
402 void* rm_parseri_malloc(rm_parser_internal* pInt, UINT32 ulSize);
403 void rm_parseri_free(rm_parser_internal* pInt, void* pMem);
404 void rm_parseri_error(rm_parser_internal* pInt, HX_RESULT err, const char* pszMsg);
405
406 HX_RESULT rm_parseri_build_seek_table(rm_parser_internal* pInt);
407 HX_RESULT rm_parseri_search_seek_tables(rm_parser_internal* pInt,
408 INT32 lStreamNumber,
409 UINT32 ulSeekTime,
410 INT32 lDirection,
411 UINT32* pulFoundTime,
412 UINT32* pulFoundOffset,
413 UINT32* pulFoundIndex);
414 UINT32 rm_parseri_get_stream_number(rm_parser_internal* pInt, UINT32 ulNum);
415 void rm_parseri_cleanup_all_data_headers(rm_parser_internal* pInt);
416#ifdef __cplusplus
417}
418#endif
419
420#endif /* #ifndef RM_PARSER_INTERNAL_H */
421