Fix caching the last accessed entry number when removing a entry.
[L-SMASH.git] / utils.h
blob4a4df11612724050c068142eee023781b60be829
1 /*****************************************************************************
2 * utils.h:
3 *****************************************************************************
4 * Copyright (C) 2010 L-SMASH project
6 * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 *****************************************************************************/
21 /* This file is available under an ISC license. */
23 #ifndef LSMASH_UTIL_H
24 #define LSMASH_UTIL_H
26 #define debug_if(x) if(x)
28 #define LSMASH_MAX( a, b ) ((a) > (b) ? (a) : (b))
29 #define LSMASH_MIN( a, b ) ((a) < (b) ? (a) : (b))
31 /*---- bytestream ----*/
33 typedef struct
35 FILE *stream; /* I/O stream */
36 uint8_t error;
37 uint8_t *data; /* buffer for reading/writing */
38 uint64_t store; /* valid data size on buffer */
39 uint64_t alloc; /* total buffer size including invalid area */
40 uint64_t pos; /* data position on buffer to be read next */
41 uint64_t written; /* data size written into "stream" already */
42 } lsmash_bs_t;
44 uint64_t lsmash_bs_get_pos( lsmash_bs_t *bs );
45 void lsmash_bs_empty( lsmash_bs_t *bs );
46 void lsmash_bs_free( lsmash_bs_t *bs );
47 void lsmash_bs_alloc( lsmash_bs_t *bs, uint64_t size );
48 lsmash_bs_t* lsmash_bs_create( char* filename );
49 void lsmash_bs_cleanup( lsmash_bs_t *bs );
51 /*---- bytestream writer ----*/
53 void lsmash_bs_put_byte( lsmash_bs_t *bs, uint8_t value );
54 void lsmash_bs_put_bytes( lsmash_bs_t *bs, void *value, uint32_t size );
55 void lsmash_bs_put_be16( lsmash_bs_t *bs, uint16_t value );
56 void lsmash_bs_put_be24( lsmash_bs_t *bs, uint32_t value );
57 void lsmash_bs_put_be32( lsmash_bs_t *bs, uint32_t value );
58 void lsmash_bs_put_be64( lsmash_bs_t *bs, uint64_t value );
59 void lsmash_bs_put_byte_from_64( lsmash_bs_t *bs, uint64_t value );
60 void lsmash_bs_put_be16_from_64( lsmash_bs_t *bs, uint64_t value );
61 void lsmash_bs_put_be24_from_64( lsmash_bs_t *bs, uint64_t value );
62 void lsmash_bs_put_be32_from_64( lsmash_bs_t *bs, uint64_t value );
63 int lsmash_bs_write_data( lsmash_bs_t *bs );
65 void* lsmash_bs_export_data( lsmash_bs_t *bs, uint32_t* length );
67 /*---- bytestream reader ----*/
68 uint8_t lsmash_bs_get_byte( lsmash_bs_t *bs );
69 uint8_t *lsmash_bs_get_bytes( lsmash_bs_t *bs, uint32_t size );
70 uint16_t lsmash_bs_get_be16( lsmash_bs_t *bs );
71 uint32_t lsmash_bs_get_be24( lsmash_bs_t *bs );
72 uint32_t lsmash_bs_get_be32( lsmash_bs_t *bs );
73 uint64_t lsmash_bs_get_be64( lsmash_bs_t *bs );
74 uint64_t lsmash_bs_get_byte_to_64( lsmash_bs_t *bs );
75 uint64_t lsmash_bs_get_be16_to_64( lsmash_bs_t *bs );
76 uint64_t lsmash_bs_get_be24_to_64( lsmash_bs_t *bs );
77 uint64_t lsmash_bs_get_be32_to_64( lsmash_bs_t *bs );
78 int lsmash_bs_read_data( lsmash_bs_t *bs, uint32_t size );
80 /*---- bitstream ----*/
81 typedef struct {
82 lsmash_bs_t* bs;
83 uint8_t store;
84 uint8_t cache;
85 } lsmash_bits_t;
87 void lsmash_bits_init( lsmash_bits_t* bits, lsmash_bs_t *bs );
88 lsmash_bits_t* lsmash_bits_create( lsmash_bs_t *bs );
89 void lsmash_bits_put_align( lsmash_bits_t *bits );
90 void lsmash_bits_get_align( lsmash_bits_t *bits );
91 void lsmash_bits_cleanup( lsmash_bits_t *bits );
93 /*---- bitstream writer ----*/
94 void lsmash_bits_put( lsmash_bits_t *bits, uint32_t value, uint32_t width );
95 uint32_t lsmash_bits_get( lsmash_bits_t *bits, uint32_t width );
96 lsmash_bits_t* lsmash_bits_adhoc_create();
97 void lsmash_bits_adhoc_cleanup( lsmash_bits_t* bits );
98 void* lsmash_bits_export_data( lsmash_bits_t* bits, uint32_t* length );
99 int lsmash_bits_import_data( lsmash_bits_t* bits, void* data, uint32_t length );
101 /*---- list ----*/
103 typedef struct lsmash_entry_tag lsmash_entry_t;
105 struct lsmash_entry_tag
107 lsmash_entry_t *next;
108 lsmash_entry_t *prev;
109 void *data;
112 typedef struct
114 lsmash_entry_t *head;
115 lsmash_entry_t *tail;
116 lsmash_entry_t *last_accessed_entry;
117 uint32_t last_accessed_number;
118 uint32_t entry_count;
119 } lsmash_entry_list_t;
121 typedef void (*lsmash_entry_data_eliminator)(void* data); /* very same as free() of standard c lib; void free(void *); */
123 void lsmash_init_entry_list( lsmash_entry_list_t *list );
124 lsmash_entry_list_t *lsmash_create_entry_list( void );
125 int lsmash_add_entry( lsmash_entry_list_t *list, void *data );
126 int lsmash_remove_entry_direct( lsmash_entry_list_t *list, lsmash_entry_t *entry, void* eliminator );
127 int lsmash_remove_entry( lsmash_entry_list_t *list, uint32_t entry_number, void* eliminator );
128 void lsmash_remove_entries( lsmash_entry_list_t *list, void* eliminator );
129 void lsmash_remove_list( lsmash_entry_list_t *list, void* eliminator );
131 lsmash_entry_t *lsmash_get_entry( lsmash_entry_list_t *list, uint32_t entry_number );
132 void *lsmash_get_entry_data( lsmash_entry_list_t *list, uint32_t entry_number );
134 /*---- type ----*/
135 double lsmash_fixed2double( uint64_t value, int frac_width );
136 float lsmash_int2float32( uint32_t value );
137 double lsmash_int2float64( uint64_t value );
139 /*---- allocator ----*/
140 void *lsmash_memdup( void *src, size_t size );
142 #endif