box: Remove redundant 'if's for freeing.
[L-SMASH.git] / common / bstream.c
blobd7f40a33437b6bc9d8ff58d206a72686667158dd
1 /*****************************************************************************
2 * bstream.c
3 *****************************************************************************
4 * Copyright (C) 2010-2014 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 #include "internal.h" /* must be placed first */
25 #include <string.h>
26 #include <limits.h>
28 lsmash_bs_t *lsmash_bs_create( void )
30 lsmash_bs_t *bs = lsmash_malloc_zero( sizeof(lsmash_bs_t) );
31 if( !bs )
32 return NULL;
33 bs->unseekable = 1;
34 bs->buffer.internal = 1;
35 bs->buffer.max_size = BS_MAX_DEFAULT_READ_SIZE;
36 return bs;
39 static void bs_buffer_free( lsmash_bs_t *bs )
41 if( bs->buffer.internal
42 && bs->buffer.data )
43 lsmash_free( bs->buffer.data );
44 bs->buffer.data = NULL;
45 bs->buffer.alloc = 0;
46 bs->buffer.store = 0;
47 bs->buffer.pos = 0;
50 void lsmash_bs_cleanup( lsmash_bs_t *bs )
52 if( !bs )
53 return;
54 bs_buffer_free( bs );
55 lsmash_free( bs );
58 int lsmash_bs_set_empty_stream( lsmash_bs_t *bs, uint8_t *data, size_t size )
60 if( !bs )
61 return -1;
62 bs->stream = NULL; /* empty stream */
63 bs->eof = 1; /* unreadable because of empty stream */
64 bs->eob = 0; /* readable on the buffer */
65 bs->error = 0;
66 bs->unseekable = 1; /* only seek on the buffer */
67 bs->written = size; /* behave as if the size of the empty stream is 'size' */
68 bs->offset = size; /* behave as if the poiter of the stream is at the end */
69 bs->buffer.unseekable = 0; /* only seek on the buffer */
70 bs->buffer.internal = 0; /* must not be allocated internally */
71 bs->buffer.data = data;
72 bs->buffer.store = size;
73 bs->buffer.alloc = size;
74 bs->buffer.pos = 0;
75 bs->buffer.max_size = 0; /* make no sense */
76 bs->buffer.count = 0;
77 bs->read = NULL;
78 bs->write = NULL;
79 bs->seek = NULL;
80 return 0;
83 void lsmash_bs_empty( lsmash_bs_t *bs )
85 if( !bs )
86 return;
87 if( bs->buffer.data )
88 memset( bs->buffer.data, 0, bs->buffer.alloc );
89 bs->buffer.store = 0;
90 bs->buffer.pos = 0;
93 static void bs_alloc( lsmash_bs_t *bs, size_t alloc )
95 if( (bs->buffer.alloc >= alloc) || bs->error )
96 return;
97 if( !bs->buffer.internal )
99 /* We cannot re-allocate the memory block. */
100 bs->error = 1;
101 return;
103 alloc += (1 << 16);
104 alloc = LSMASH_MAX( alloc, bs->buffer.max_size );
105 uint8_t *data;
106 if( !bs->buffer.data )
107 data = lsmash_malloc( alloc );
108 else
109 data = lsmash_realloc( bs->buffer.data, alloc );
110 if( !data )
112 bs_buffer_free( bs );
113 bs->error = 1;
114 return;
116 bs->buffer.internal = 1;
117 bs->buffer.data = data;
118 bs->buffer.alloc = alloc;
121 static uint64_t bs_estimate_seek_offset( lsmash_bs_t *bs, int64_t offset, int whence )
123 /* Calculate the offset after the seek. */
124 uint64_t dst_offset;
125 if( whence == SEEK_SET )
127 assert( offset >= 0 );
128 if( bs->written < offset )
129 dst_offset = bs->written;
130 else
131 dst_offset = offset;
133 else if( whence == SEEK_CUR )
135 if( offset < 0 && bs->offset < -offset )
136 dst_offset = 0;
137 else if( offset > 0 && bs->written < bs->offset + offset )
138 dst_offset = bs->written;
139 else
140 dst_offset = bs->offset + offset;
142 else /* if( whence == SEEK_END ) */
144 assert( offset <= 0 );
145 if( bs->written < -offset )
146 dst_offset = 0;
147 else
148 dst_offset = bs->written + offset;
150 return dst_offset;
153 /* TODO: Support offset > INT64_MAX */
154 int64_t lsmash_bs_write_seek( lsmash_bs_t *bs, int64_t offset, int whence )
156 if( bs->unseekable || (whence != SEEK_SET && whence != SEEK_CUR && whence != SEEK_END) )
157 return -1;
158 /* Try to seek the stream. */
159 int64_t ret = bs->seek( bs->stream, offset, whence );
160 if( ret < 0 )
161 return ret;
162 bs->offset = bs_estimate_seek_offset( bs, offset, whence );
163 bs->eof = 0;
164 bs->eob = 0;
165 return ret;
168 /* TODO: Support offset > INT64_MAX */
169 int64_t lsmash_bs_read_seek( lsmash_bs_t *bs, int64_t offset, int whence )
171 if( whence != SEEK_SET && whence != SEEK_CUR && whence != SEEK_END )
172 return -1;
173 if( whence == SEEK_CUR )
174 offset -= lsmash_bs_get_remaining_buffer_size( bs );
175 /* Check whether we can seek on the buffer. */
176 if( !bs->buffer.unseekable )
178 assert( bs->offset >= bs->buffer.store );
179 uint64_t dst_offset = bs_estimate_seek_offset( bs, offset, whence );
180 uint64_t offset_s = bs->offset - bs->buffer.store;
181 uint64_t offset_e = bs->offset;
182 if( bs->unseekable || (dst_offset >= offset_s && dst_offset < offset_e) )
184 /* OK, we can. So, seek on the buffer. */
185 bs->buffer.pos = dst_offset - offset_s;
186 bs->eob = 0;
187 return lsmash_bs_get_stream_pos( bs );
190 if( bs->unseekable )
191 return -1;
192 /* Try to seek the stream. */
193 int64_t ret = bs->seek( bs->stream, offset, whence );
194 if( ret < 0 )
195 return ret;
196 bs->offset = ret;
197 bs->written = LSMASH_MAX( bs->written, bs->offset );
198 bs->eof = 0;
199 bs->eob = 0;
200 /* The data on the buffer is invalid. */
201 lsmash_bs_empty( bs );
202 return ret;
205 static void bs_dispose_past_data( lsmash_bs_t *bs )
207 /* Move remainder bytes. */
208 assert( bs->buffer.store >= bs->buffer.pos );
209 size_t remainder = lsmash_bs_get_remaining_buffer_size( bs );
210 if( bs->buffer.pos && remainder )
211 memmove( lsmash_bs_get_buffer_data_start( bs ), lsmash_bs_get_buffer_data( bs ), remainder );
212 bs->buffer.store = remainder;
213 bs->buffer.pos = 0;
216 /*---- bitstream writer ----*/
217 void lsmash_bs_put_byte( lsmash_bs_t *bs, uint8_t value )
219 if( bs->buffer.internal
220 || bs->buffer.data )
222 bs_alloc( bs, bs->buffer.store + 1 );
223 if( bs->error )
224 return;
225 bs->buffer.data[ bs->buffer.store ] = value;
227 ++ bs->buffer.store;
230 void lsmash_bs_put_bytes( lsmash_bs_t *bs, uint32_t size, void *value )
232 if( size == 0 || !value )
233 return;
234 if( bs->buffer.internal
235 || bs->buffer.data )
237 bs_alloc( bs, bs->buffer.store + size );
238 if( bs->error )
239 return;
240 memcpy( lsmash_bs_get_buffer_data_end( bs ), value, size );
242 bs->buffer.store += size;
245 void lsmash_bs_put_be16( lsmash_bs_t *bs, uint16_t value )
247 lsmash_bs_put_byte( bs, value >> 8 );
248 lsmash_bs_put_byte( bs, value );
251 void lsmash_bs_put_be24( lsmash_bs_t *bs, uint32_t value )
253 lsmash_bs_put_byte( bs, value >> 16 );
254 lsmash_bs_put_be16( bs, value );
257 void lsmash_bs_put_be32( lsmash_bs_t *bs, uint32_t value )
259 lsmash_bs_put_be16( bs, value >> 16 );
260 lsmash_bs_put_be16( bs, value );
263 void lsmash_bs_put_be64( lsmash_bs_t *bs, uint64_t value )
265 lsmash_bs_put_be32( bs, value >> 32 );
266 lsmash_bs_put_be32( bs, value );
269 void lsmash_bs_put_byte_from_64( lsmash_bs_t *bs, uint64_t value )
271 lsmash_bs_put_byte( bs, value );
274 void lsmash_bs_put_be16_from_64( lsmash_bs_t *bs, uint64_t value )
276 lsmash_bs_put_be16( bs, value );
279 void lsmash_bs_put_be24_from_64( lsmash_bs_t *bs, uint64_t value )
281 lsmash_bs_put_be24( bs, value );
284 void lsmash_bs_put_be32_from_64( lsmash_bs_t *bs, uint64_t value )
286 lsmash_bs_put_be32( bs, value );
289 void lsmash_bs_put_le16( lsmash_bs_t *bs, uint16_t value )
291 lsmash_bs_put_byte( bs, value );
292 lsmash_bs_put_byte( bs, value >> 8 );
295 void lsmash_bs_put_le32( lsmash_bs_t *bs, uint32_t value )
297 lsmash_bs_put_le16( bs, value );
298 lsmash_bs_put_le16( bs, value >> 16 );
301 int lsmash_bs_flush_buffer( lsmash_bs_t *bs )
303 if( !bs )
304 return -1;
305 if( bs->buffer.store == 0
306 || (bs->stream && bs->write && !bs->buffer.data) )
307 return 0;
308 if( bs->error
309 || (bs->stream && bs->write && bs->write( bs->stream, lsmash_bs_get_buffer_data_start( bs ), bs->buffer.store ) != bs->buffer.store) )
311 bs_buffer_free( bs );
312 bs->error = 1;
313 return -1;
315 if( bs->write )
317 bs->written += bs->buffer.store;
318 bs->offset += bs->buffer.store;
320 bs->buffer.store = 0;
321 return 0;
324 int lsmash_bs_write_data( lsmash_bs_t *bs, uint8_t *buf, size_t size )
326 if( !bs || size > INT_MAX )
327 return -1;
328 if( !buf || size == 0 )
329 return 0;
330 if( bs->error || !bs->stream )
332 bs_buffer_free( bs );
333 bs->error = 1;
334 return -1;
336 int write_size = bs->write( bs->stream, buf, size );
337 bs->written += write_size;
338 bs->offset += write_size;
339 return write_size != size ? -1 : 0;
342 void *lsmash_bs_export_data( lsmash_bs_t *bs, uint32_t *length )
344 if( !bs || !bs->buffer.data || bs->buffer.store == 0 || bs->error )
345 return NULL;
346 void *buf = lsmash_memdup( lsmash_bs_get_buffer_data_start( bs ), bs->buffer.store );
347 if( !buf )
348 return NULL;
349 if( length )
350 *length = bs->buffer.store;
351 return buf;
353 /*---- ----*/
355 /*---- bitstream reader ----*/
356 static void bs_fill_buffer( lsmash_bs_t *bs )
358 if( bs->eof || bs->error )
359 return;
360 if( !bs->read || !bs->stream || bs->buffer.max_size == 0 )
362 bs->eof = 1;
363 return;
365 if( !bs->buffer.data )
367 bs_alloc( bs, bs->buffer.max_size );
368 if( bs->error )
369 return;
371 /* Read bytes from the stream to fill the buffer. */
372 bs_dispose_past_data( bs );
373 while( bs->buffer.alloc > bs->buffer.store )
375 uint64_t invalid_buffer_size = bs->buffer.alloc - bs->buffer.store;
376 int max_read_size = LSMASH_MIN( invalid_buffer_size, bs->buffer.max_size );
377 int read_size = bs->read( bs->stream, lsmash_bs_get_buffer_data_end( bs ), max_read_size );
378 if( read_size == 0 )
380 bs->eof = 1;
381 return;
383 else if( read_size < 0 )
385 bs->error = 1;
386 return;
388 bs->buffer.unseekable = 0;
389 bs->buffer.store += read_size;
390 bs->offset += read_size;
391 bs->written = LSMASH_MAX( bs->written, bs->offset );
395 uint8_t lsmash_bs_show_byte( lsmash_bs_t *bs, uint32_t offset )
397 if( bs->error )
398 return 0;
399 if( offset >= lsmash_bs_get_remaining_buffer_size( bs ) )
401 bs_fill_buffer( bs );
402 if( bs->error )
403 return 0;
404 if( offset >= lsmash_bs_get_remaining_buffer_size( bs ) )
406 if( bs->eof )
407 /* No more read from both the stream and the buffer. */
408 return 0;
409 /* We need increase the buffer size. */
410 bs_alloc( bs, bs->buffer.pos + offset + 1 );
411 bs_fill_buffer( bs );
412 if( bs->error )
413 return 0;
416 return bs->buffer.data[ bs->buffer.pos + offset ];
419 uint16_t lsmash_bs_show_be16( lsmash_bs_t *bs, uint32_t offset )
421 return ((uint16_t)lsmash_bs_show_byte( bs, offset ) << 8)
422 | ((uint16_t)lsmash_bs_show_byte( bs, offset + 1 ));
425 uint32_t lsmash_bs_show_be24( lsmash_bs_t *bs, uint32_t offset )
427 return ((uint32_t)lsmash_bs_show_byte( bs, offset ) << 16)
428 | ((uint32_t)lsmash_bs_show_byte( bs, offset + 1 ) << 8)
429 | ((uint32_t)lsmash_bs_show_byte( bs, offset + 2 ));
432 uint32_t lsmash_bs_show_be32( lsmash_bs_t *bs, uint32_t offset )
434 return ((uint32_t)lsmash_bs_show_byte( bs, offset ) << 24)
435 | ((uint32_t)lsmash_bs_show_byte( bs, offset + 1 ) << 16)
436 | ((uint32_t)lsmash_bs_show_byte( bs, offset + 2 ) << 8)
437 | ((uint32_t)lsmash_bs_show_byte( bs, offset + 3 ));
440 uint64_t lsmash_bs_show_be64( lsmash_bs_t *bs, uint32_t offset )
442 return ((uint64_t)lsmash_bs_show_byte( bs, offset ) << 56)
443 | ((uint64_t)lsmash_bs_show_byte( bs, offset + 1 ) << 48)
444 | ((uint64_t)lsmash_bs_show_byte( bs, offset + 2 ) << 40)
445 | ((uint64_t)lsmash_bs_show_byte( bs, offset + 3 ) << 32)
446 | ((uint64_t)lsmash_bs_show_byte( bs, offset + 4 ) << 24)
447 | ((uint64_t)lsmash_bs_show_byte( bs, offset + 5 ) << 16)
448 | ((uint64_t)lsmash_bs_show_byte( bs, offset + 6 ) << 8)
449 | ((uint64_t)lsmash_bs_show_byte( bs, offset + 7 ));
452 uint8_t lsmash_bs_get_byte( lsmash_bs_t *bs )
454 if( bs->eob || bs->error )
455 return 0;
456 assert( bs->buffer.pos <= bs->buffer.store );
457 if( bs->buffer.pos == bs->buffer.store )
459 bs_fill_buffer( bs );
460 if( bs->error )
461 return 0;
462 if( bs->buffer.pos == bs->buffer.store && bs->eof )
464 /* No more read from both the stream and the buffer. */
465 bs->eob = 1;
466 return 0;
469 ++ bs->buffer.count; /* increment counter */
470 return bs->buffer.data[ bs->buffer.pos ++ ];
473 void lsmash_bs_skip_bytes( lsmash_bs_t *bs, uint32_t size )
475 if( bs->eob || bs->error || size == 0 )
476 return;
477 uint64_t remainder;
478 uint64_t offset = 0;
479 while( size > lsmash_bs_get_remaining_buffer_size( bs ) )
481 remainder = lsmash_bs_get_remaining_buffer_size( bs );
482 offset += remainder;
483 size -= remainder;
484 bs->buffer.pos = bs->buffer.store;
485 if( bs->eof )
487 /* No more read from both the stream and the buffer. */
488 bs->eob = 1;
489 break;
491 else
493 bs_fill_buffer( bs );
494 if( bs->error )
495 break;
498 remainder = LSMASH_MIN( size, lsmash_bs_get_remaining_buffer_size( bs ) );
499 offset += remainder;
500 bs->buffer.pos += remainder;
501 bs->buffer.count += offset;
504 void lsmash_bs_skip_bytes_64( lsmash_bs_t *bs, uint64_t size )
506 while( size )
508 uint64_t skip_bytes = LSMASH_MIN( size, UINT32_MAX );
509 lsmash_bs_skip_bytes( bs, (uint32_t)skip_bytes );
510 size -= skip_bytes;
511 if( bs->eob )
512 return;
516 static int64_t bs_get_bytes( lsmash_bs_t *bs, uint32_t size, uint8_t *buf )
518 size_t remainder;
519 size_t remain_size = size;
520 uintptr_t offset = 0;
521 while( remain_size > lsmash_bs_get_remaining_buffer_size( bs ) )
523 remainder = lsmash_bs_get_remaining_buffer_size( bs );
524 memcpy( buf + offset, lsmash_bs_get_buffer_data( bs ), remainder );
525 offset += remainder;
526 remain_size -= remainder;
527 bs->buffer.pos = bs->buffer.store;
528 if( bs->eof )
530 /* No more read from both the stream and the buffer. */
531 bs->eob = 1;
532 break;
534 else
536 bs_fill_buffer( bs );
537 if( bs->error )
539 bs->buffer.count += offset;
540 return -1;
544 remainder = LSMASH_MIN( remain_size, lsmash_bs_get_remaining_buffer_size( bs ) );
545 memcpy( buf + offset, lsmash_bs_get_buffer_data( bs ), remainder );
546 offset += remainder;
547 bs->buffer.pos += remainder;
548 bs->buffer.count += offset;
549 if( offset < size )
550 memset( buf + offset, 0, size - offset );
551 return (int64_t)offset;
554 uint8_t *lsmash_bs_get_bytes( lsmash_bs_t *bs, uint32_t size )
556 if( bs->eob || bs->error || size == 0 )
557 return NULL;
558 uint8_t *value = lsmash_malloc( size );
559 if( !value )
561 bs->error = 1;
562 return NULL;
564 if( bs_get_bytes( bs, size, value ) < 0 )
566 lsmash_free( value );
567 return NULL;
569 return value;
572 int64_t lsmash_bs_get_bytes_ex( lsmash_bs_t *bs, uint32_t size, uint8_t *value )
574 if( size == 0 )
575 return 0;
576 if( bs->eob || bs->error )
577 return -1;
578 return bs_get_bytes( bs, size, value );
581 uint16_t lsmash_bs_get_be16( lsmash_bs_t *bs )
583 uint16_t value = lsmash_bs_get_byte( bs );
584 return (value<<8) | lsmash_bs_get_byte( bs );
587 uint32_t lsmash_bs_get_be24( lsmash_bs_t *bs )
589 uint32_t value = lsmash_bs_get_byte( bs );
590 return (value<<16) | lsmash_bs_get_be16( bs );
593 uint32_t lsmash_bs_get_be32( lsmash_bs_t *bs )
595 uint32_t value = lsmash_bs_get_be16( bs );
596 return (value<<16) | lsmash_bs_get_be16( bs );
599 uint64_t lsmash_bs_get_be64( lsmash_bs_t *bs )
601 uint64_t value = lsmash_bs_get_be32( bs );
602 return (value<<32) | lsmash_bs_get_be32( bs );
605 uint64_t lsmash_bs_get_byte_to_64( lsmash_bs_t *bs )
607 return lsmash_bs_get_byte( bs );
610 uint64_t lsmash_bs_get_be16_to_64( lsmash_bs_t *bs )
612 return lsmash_bs_get_be16( bs );
615 uint64_t lsmash_bs_get_be24_to_64( lsmash_bs_t *bs )
617 return lsmash_bs_get_be24( bs );
620 uint64_t lsmash_bs_get_be32_to_64( lsmash_bs_t *bs )
622 return lsmash_bs_get_be32( bs );
625 int lsmash_bs_read( lsmash_bs_t *bs, uint32_t size )
627 if( !bs || size > INT_MAX )
628 return -1;
629 if( size == 0 )
630 return 0;
631 bs_alloc( bs, bs->buffer.store + size );
632 if( bs->error || !bs->stream )
634 bs->error = 1;
635 return -1;
637 int read_size = bs->read( bs->stream, lsmash_bs_get_buffer_data_end( bs ), size );
638 if( read_size == 0 )
640 bs->eof = 1;
641 return 0;
643 else if( read_size < 0 )
645 bs->error = 1;
646 return -1;
648 bs->buffer.store += read_size;
649 bs->offset += read_size;
650 bs->written = LSMASH_MAX( bs->written, bs->offset );
651 return read_size;
654 int lsmash_bs_read_data( lsmash_bs_t *bs, uint8_t *buf, size_t *size )
656 if( !bs || !size || *size > INT_MAX )
657 return -1;
658 if( !buf || *size == 0 )
659 return 0;
660 if( bs->error || !bs->stream )
662 bs->error = 1;
663 return -1;
665 int read_size = bs->read( bs->stream, buf, *size );
666 if( read_size == 0 )
667 bs->eof = 1;
668 else if( read_size < 0 )
670 bs->error = 1;
671 return -1;
673 bs->buffer.unseekable = 1;
674 bs->offset += read_size;
675 *size = read_size;
676 bs->written = LSMASH_MAX( bs->written, bs->offset );
677 return 0;
680 int lsmash_bs_import_data( lsmash_bs_t *bs, void* data, uint32_t length )
682 if( !bs || bs->error || !data || length == 0 )
683 return -1;
684 bs_alloc( bs, bs->buffer.store + length );
685 if( bs->error || !bs->buffer.data ) /* means, failed to alloc. */
687 bs_buffer_free( bs );
688 return -1;
690 memcpy( lsmash_bs_get_buffer_data_end( bs ), data, length );
691 bs->buffer.store += length;
692 return 0;
694 /*---- ----*/
696 /*---- bitstream ----*/
697 void lsmash_bits_init( lsmash_bits_t *bits, lsmash_bs_t *bs )
699 debug_if( !bits || !bs )
700 return;
701 bits->bs = bs;
702 bits->store = 0;
703 bits->cache = 0;
706 lsmash_bits_t *lsmash_bits_create( lsmash_bs_t *bs )
708 debug_if( !bs )
709 return NULL;
710 lsmash_bits_t *bits = (lsmash_bits_t *)lsmash_malloc( sizeof(lsmash_bits_t) );
711 if( !bits )
712 return NULL;
713 lsmash_bits_init( bits, bs );
714 return bits;
717 void lsmash_bits_empty( lsmash_bits_t *bits )
719 debug_if( !bits )
720 return;
721 lsmash_bs_empty( bits->bs );
722 bits->store = 0;
723 bits->cache = 0;
726 #define BITS_IN_BYTE 8
727 void lsmash_bits_put_align( lsmash_bits_t *bits )
729 debug_if( !bits )
730 return;
731 if( !bits->store )
732 return;
733 lsmash_bs_put_byte( bits->bs, bits->cache << ( BITS_IN_BYTE - bits->store ) );
736 void lsmash_bits_get_align( lsmash_bits_t *bits )
738 debug_if( !bits )
739 return;
740 bits->store = 0;
741 bits->cache = 0;
744 /* Must be used ONLY for bits struct created with isom_create_bits.
745 Otherwise, just lsmash_free() the bits struct. */
746 void lsmash_bits_cleanup( lsmash_bits_t *bits )
748 debug_if( !bits )
749 return;
750 lsmash_free( bits );
753 /* we can change value's type to unsigned int for 64-bit operation if needed. */
754 static inline uint8_t lsmash_bits_mask_lsb8( uint32_t value, uint32_t width )
756 return (uint8_t)( value & ~( ~0U << width ) );
759 void lsmash_bits_put( lsmash_bits_t *bits, uint32_t width, uint64_t value )
761 debug_if( !bits || !width )
762 return;
763 if( bits->store )
765 if( bits->store + width < BITS_IN_BYTE )
767 /* cache can contain all of value's bits. */
768 bits->cache <<= width;
769 bits->cache |= lsmash_bits_mask_lsb8( value, width );
770 bits->store += width;
771 return;
773 /* flush cache with value's some leading bits. */
774 uint32_t free_bits = BITS_IN_BYTE - bits->store;
775 bits->cache <<= free_bits;
776 bits->cache |= lsmash_bits_mask_lsb8( value >> (width -= free_bits), free_bits );
777 lsmash_bs_put_byte( bits->bs, bits->cache );
778 bits->store = 0;
779 bits->cache = 0;
781 /* cache is empty here. */
782 /* byte unit operation. */
783 while( width > BITS_IN_BYTE )
784 lsmash_bs_put_byte( bits->bs, (uint8_t)(value >> (width -= BITS_IN_BYTE)) );
785 /* bit unit operation for residual. */
786 if( width )
788 bits->cache = lsmash_bits_mask_lsb8( value, width );
789 bits->store = width;
793 uint64_t lsmash_bits_get( lsmash_bits_t *bits, uint32_t width )
795 debug_if( !bits || !width )
796 return 0;
797 uint64_t value = 0;
798 if( bits->store )
800 if( bits->store >= width )
802 /* cache contains all of bits required. */
803 bits->store -= width;
804 return lsmash_bits_mask_lsb8( bits->cache >> bits->store, width );
806 /* fill value's leading bits with cache's residual. */
807 value = lsmash_bits_mask_lsb8( bits->cache, bits->store );
808 width -= bits->store;
809 bits->store = 0;
810 bits->cache = 0;
812 /* cache is empty here. */
813 /* byte unit operation. */
814 while( width > BITS_IN_BYTE )
816 value <<= BITS_IN_BYTE;
817 width -= BITS_IN_BYTE;
818 value |= lsmash_bs_get_byte( bits->bs );
820 /* bit unit operation for residual. */
821 if( width )
823 bits->cache = lsmash_bs_get_byte( bits->bs );
824 bits->store = BITS_IN_BYTE - width;
825 value <<= width;
826 value |= lsmash_bits_mask_lsb8( bits->cache >> bits->store, width );
828 return value;
831 /****
832 bitstream with bytestream for adhoc operation
833 ****/
835 lsmash_bits_t* lsmash_bits_adhoc_create()
837 lsmash_bs_t* bs = lsmash_bs_create();
838 if( !bs )
839 return NULL;
840 lsmash_bits_t* bits = lsmash_bits_create( bs );
841 if( !bits )
843 lsmash_bs_cleanup( bs );
844 return NULL;
846 return bits;
849 void lsmash_bits_adhoc_cleanup( lsmash_bits_t* bits )
851 if( !bits )
852 return;
853 lsmash_bs_cleanup( bits->bs );
854 lsmash_bits_cleanup( bits );
857 void* lsmash_bits_export_data( lsmash_bits_t* bits, uint32_t* length )
859 lsmash_bits_put_align( bits );
860 return lsmash_bs_export_data( bits->bs, length );
863 int lsmash_bits_import_data( lsmash_bits_t* bits, void* data, uint32_t length )
865 return lsmash_bs_import_data( bits->bs, data, length );
867 /*---- ----*/
869 /*---- basic I/O ----*/
870 int lsmash_fread_wrapper( void *opaque, uint8_t *buf, int size )
872 int read_size = fread( buf, 1, size, (FILE *)opaque );
873 return ferror( (FILE *)opaque ) ? -1 : read_size;
876 int lsmash_fwrite_wrapper( void *opaque, uint8_t *buf, int size )
878 return fwrite( buf, 1, size, (FILE *)opaque );
881 int64_t lsmash_fseek_wrapper( void *opaque, int64_t offset, int whence )
883 if( lsmash_fseek( (FILE *)opaque, offset, whence ) != 0 )
884 return -1;
885 return lsmash_ftell( (FILE *)opaque );
888 lsmash_multiple_buffers_t *lsmash_create_multiple_buffers( uint32_t number_of_buffers, uint32_t buffer_size )
890 if( (uint64_t)number_of_buffers * buffer_size > UINT32_MAX )
891 return NULL;
892 lsmash_multiple_buffers_t *multiple_buffer = lsmash_malloc( sizeof(lsmash_multiple_buffers_t) );
893 if( !multiple_buffer )
894 return NULL;
895 multiple_buffer->buffers = lsmash_malloc( number_of_buffers * buffer_size );
896 if( !multiple_buffer->buffers )
898 lsmash_free( multiple_buffer );
899 return NULL;
901 multiple_buffer->number_of_buffers = number_of_buffers;
902 multiple_buffer->buffer_size = buffer_size;
903 return multiple_buffer;
906 void *lsmash_withdraw_buffer( lsmash_multiple_buffers_t *multiple_buffer, uint32_t buffer_number )
908 if( !multiple_buffer || !buffer_number || buffer_number > multiple_buffer->number_of_buffers )
909 return NULL;
910 return (uint8_t *)multiple_buffer->buffers + (buffer_number - 1) * multiple_buffer->buffer_size;
913 lsmash_multiple_buffers_t *lsmash_resize_multiple_buffers( lsmash_multiple_buffers_t *multiple_buffer, uint32_t buffer_size )
915 if( !multiple_buffer )
916 return NULL;
917 if( buffer_size == multiple_buffer->buffer_size )
918 return multiple_buffer;
919 if( (uint64_t)multiple_buffer->number_of_buffers * buffer_size > UINT32_MAX )
920 return NULL;
921 uint8_t *temp;
922 if( buffer_size > multiple_buffer->buffer_size )
924 temp = lsmash_realloc( multiple_buffer->buffers, multiple_buffer->number_of_buffers * buffer_size );
925 if( !temp )
926 return NULL;
927 for( uint32_t i = multiple_buffer->number_of_buffers - 1; i ; i-- )
928 memmove( temp + buffer_size, temp + i * multiple_buffer->buffer_size, multiple_buffer->buffer_size );
930 else
932 for( uint32_t i = 1; i < multiple_buffer->number_of_buffers; i++ )
933 memmove( (uint8_t *)multiple_buffer->buffers + buffer_size,
934 (uint8_t *)multiple_buffer->buffers + i * multiple_buffer->buffer_size,
935 multiple_buffer->buffer_size );
936 temp = lsmash_realloc( multiple_buffer->buffers, multiple_buffer->number_of_buffers * buffer_size );
937 if( !temp )
938 return NULL;
940 multiple_buffer->buffers = temp;
941 multiple_buffer->buffer_size = buffer_size;
942 return multiple_buffer;
945 void lsmash_destroy_multiple_buffers( lsmash_multiple_buffers_t *multiple_buffer )
947 if( !multiple_buffer )
948 return;
949 if( multiple_buffer->buffers )
950 lsmash_free( multiple_buffer->buffers );
951 lsmash_free( multiple_buffer );