1 /*****************************************************************************
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 */
28 lsmash_bs_t
*lsmash_bs_create( void )
30 lsmash_bs_t
*bs
= lsmash_malloc_zero( sizeof(lsmash_bs_t
) );
34 bs
->buffer
.internal
= 1;
35 bs
->buffer
.max_size
= BS_MAX_DEFAULT_READ_SIZE
;
39 static void bs_buffer_free( lsmash_bs_t
*bs
)
41 if( bs
->buffer
.internal
43 lsmash_free( bs
->buffer
.data
);
44 bs
->buffer
.data
= NULL
;
50 void lsmash_bs_cleanup( lsmash_bs_t
*bs
)
58 int lsmash_bs_set_empty_stream( lsmash_bs_t
*bs
, uint8_t *data
, size_t size
)
62 bs
->stream
= NULL
; /* empty stream */
63 bs
->eof
= 1; /* unreadable because of empty stream */
64 bs
->eob
= 0; /* readable on the buffer */
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
;
75 bs
->buffer
.max_size
= 0; /* make no sense */
83 void lsmash_bs_empty( lsmash_bs_t
*bs
)
88 memset( bs
->buffer
.data
, 0, bs
->buffer
.alloc
);
93 static void bs_alloc( lsmash_bs_t
*bs
, size_t alloc
)
95 if( (bs
->buffer
.alloc
>= alloc
) || bs
->error
)
97 if( !bs
->buffer
.internal
)
99 /* We cannot re-allocate the memory block. */
104 alloc
= LSMASH_MAX( alloc
, bs
->buffer
.max_size
);
106 if( !bs
->buffer
.data
)
107 data
= lsmash_malloc( alloc
);
109 data
= lsmash_realloc( bs
->buffer
.data
, alloc
);
112 bs_buffer_free( bs
);
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. */
125 if( whence
== SEEK_SET
)
127 assert( offset
>= 0 );
128 if( bs
->written
< offset
)
129 dst_offset
= bs
->written
;
133 else if( whence
== SEEK_CUR
)
135 if( offset
< 0 && bs
->offset
< -offset
)
137 else if( offset
> 0 && bs
->written
< bs
->offset
+ offset
)
138 dst_offset
= bs
->written
;
140 dst_offset
= bs
->offset
+ offset
;
142 else /* if( whence == SEEK_END ) */
144 assert( offset
<= 0 );
145 if( bs
->written
< -offset
)
148 dst_offset
= bs
->written
+ 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
) )
158 /* Try to seek the stream. */
159 int64_t ret
= bs
->seek( bs
->stream
, offset
, whence
);
162 bs
->offset
= bs_estimate_seek_offset( bs
, offset
, whence
);
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
)
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
;
187 return lsmash_bs_get_stream_pos( bs
);
192 /* Try to seek the stream. */
193 int64_t ret
= bs
->seek( bs
->stream
, offset
, whence
);
197 bs
->written
= LSMASH_MAX( bs
->written
, bs
->offset
);
200 /* The data on the buffer is invalid. */
201 lsmash_bs_empty( bs
);
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
;
216 /*---- bitstream writer ----*/
217 void lsmash_bs_put_byte( lsmash_bs_t
*bs
, uint8_t value
)
219 if( bs
->buffer
.internal
222 bs_alloc( bs
, bs
->buffer
.store
+ 1 );
225 bs
->buffer
.data
[ bs
->buffer
.store
] = value
;
230 void lsmash_bs_put_bytes( lsmash_bs_t
*bs
, uint32_t size
, void *value
)
232 if( size
== 0 || !value
)
234 if( bs
->buffer
.internal
237 bs_alloc( bs
, bs
->buffer
.store
+ size
);
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
)
305 if( bs
->buffer
.store
== 0
306 || (bs
->stream
&& bs
->write
&& !bs
->buffer
.data
) )
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
);
317 bs
->written
+= bs
->buffer
.store
;
318 bs
->offset
+= bs
->buffer
.store
;
320 bs
->buffer
.store
= 0;
324 int lsmash_bs_write_data( lsmash_bs_t
*bs
, uint8_t *buf
, size_t size
)
326 if( !bs
|| size
> INT_MAX
)
328 if( !buf
|| size
== 0 )
330 if( bs
->error
|| !bs
->stream
)
332 bs_buffer_free( bs
);
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
)
346 void *buf
= lsmash_memdup( lsmash_bs_get_buffer_data_start( bs
), bs
->buffer
.store
);
350 *length
= bs
->buffer
.store
;
355 /*---- bitstream reader ----*/
356 static void bs_fill_buffer( lsmash_bs_t
*bs
)
358 if( bs
->eof
|| bs
->error
)
360 if( !bs
->read
|| !bs
->stream
|| bs
->buffer
.max_size
== 0 )
365 if( !bs
->buffer
.data
)
367 bs_alloc( bs
, bs
->buffer
.max_size
);
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
);
383 else if( read_size
< 0 )
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
)
399 if( offset
>= lsmash_bs_get_remaining_buffer_size( bs
) )
401 bs_fill_buffer( bs
);
404 if( offset
>= lsmash_bs_get_remaining_buffer_size( bs
) )
407 /* No more read from both the stream and the buffer. */
409 /* We need increase the buffer size. */
410 bs_alloc( bs
, bs
->buffer
.pos
+ offset
+ 1 );
411 bs_fill_buffer( bs
);
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
)
456 assert( bs
->buffer
.pos
<= bs
->buffer
.store
);
457 if( bs
->buffer
.pos
== bs
->buffer
.store
)
459 bs_fill_buffer( bs
);
462 if( bs
->buffer
.pos
== bs
->buffer
.store
&& bs
->eof
)
464 /* No more read from both the stream and the buffer. */
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 )
479 while( size
> lsmash_bs_get_remaining_buffer_size( bs
) )
481 remainder
= lsmash_bs_get_remaining_buffer_size( bs
);
484 bs
->buffer
.pos
= bs
->buffer
.store
;
487 /* No more read from both the stream and the buffer. */
493 bs_fill_buffer( bs
);
498 remainder
= LSMASH_MIN( size
, lsmash_bs_get_remaining_buffer_size( bs
) );
500 bs
->buffer
.pos
+= remainder
;
501 bs
->buffer
.count
+= offset
;
504 void lsmash_bs_skip_bytes_64( lsmash_bs_t
*bs
, uint64_t size
)
508 uint64_t skip_bytes
= LSMASH_MIN( size
, UINT32_MAX
);
509 lsmash_bs_skip_bytes( bs
, (uint32_t)skip_bytes
);
516 static int64_t bs_get_bytes( lsmash_bs_t
*bs
, uint32_t size
, uint8_t *buf
)
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
);
526 remain_size
-= remainder
;
527 bs
->buffer
.pos
= bs
->buffer
.store
;
530 /* No more read from both the stream and the buffer. */
536 bs_fill_buffer( bs
);
539 bs
->buffer
.count
+= offset
;
544 remainder
= LSMASH_MIN( remain_size
, lsmash_bs_get_remaining_buffer_size( bs
) );
545 memcpy( buf
+ offset
, lsmash_bs_get_buffer_data( bs
), remainder
);
547 bs
->buffer
.pos
+= remainder
;
548 bs
->buffer
.count
+= offset
;
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 )
558 uint8_t *value
= lsmash_malloc( size
);
564 if( bs_get_bytes( bs
, size
, value
) < 0 )
566 lsmash_free( value
);
572 int64_t lsmash_bs_get_bytes_ex( lsmash_bs_t
*bs
, uint32_t size
, uint8_t *value
)
576 if( bs
->eob
|| bs
->error
)
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
)
631 bs_alloc( bs
, bs
->buffer
.store
+ size
);
632 if( bs
->error
|| !bs
->stream
)
637 int read_size
= bs
->read( bs
->stream
, lsmash_bs_get_buffer_data_end( bs
), size
);
643 else if( read_size
< 0 )
648 bs
->buffer
.store
+= read_size
;
649 bs
->offset
+= read_size
;
650 bs
->written
= LSMASH_MAX( bs
->written
, bs
->offset
);
654 int lsmash_bs_read_data( lsmash_bs_t
*bs
, uint8_t *buf
, size_t *size
)
656 if( !bs
|| !size
|| *size
> INT_MAX
)
658 if( !buf
|| *size
== 0 )
660 if( bs
->error
|| !bs
->stream
)
665 int read_size
= bs
->read( bs
->stream
, buf
, *size
);
668 else if( read_size
< 0 )
673 bs
->buffer
.unseekable
= 1;
674 bs
->offset
+= read_size
;
676 bs
->written
= LSMASH_MAX( bs
->written
, bs
->offset
);
680 int lsmash_bs_import_data( lsmash_bs_t
*bs
, void* data
, uint32_t length
)
682 if( !bs
|| bs
->error
|| !data
|| length
== 0 )
684 bs_alloc( bs
, bs
->buffer
.store
+ length
);
685 if( bs
->error
|| !bs
->buffer
.data
) /* means, failed to alloc. */
687 bs_buffer_free( bs
);
690 memcpy( lsmash_bs_get_buffer_data_end( bs
), data
, length
);
691 bs
->buffer
.store
+= length
;
696 /*---- bitstream ----*/
697 void lsmash_bits_init( lsmash_bits_t
*bits
, lsmash_bs_t
*bs
)
699 debug_if( !bits
|| !bs
)
706 lsmash_bits_t
*lsmash_bits_create( lsmash_bs_t
*bs
)
710 lsmash_bits_t
*bits
= (lsmash_bits_t
*)lsmash_malloc( sizeof(lsmash_bits_t
) );
713 lsmash_bits_init( bits
, bs
);
717 void lsmash_bits_empty( lsmash_bits_t
*bits
)
721 lsmash_bs_empty( bits
->bs
);
726 #define BITS_IN_BYTE 8
727 void lsmash_bits_put_align( lsmash_bits_t
*bits
)
733 lsmash_bs_put_byte( bits
->bs
, bits
->cache
<< ( BITS_IN_BYTE
- bits
->store
) );
736 void lsmash_bits_get_align( lsmash_bits_t
*bits
)
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
)
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
)
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
;
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
);
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. */
788 bits
->cache
= lsmash_bits_mask_lsb8( value
, width
);
793 uint64_t lsmash_bits_get( lsmash_bits_t
*bits
, uint32_t width
)
795 debug_if( !bits
|| !width
)
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
;
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. */
823 bits
->cache
= lsmash_bs_get_byte( bits
->bs
);
824 bits
->store
= BITS_IN_BYTE
- width
;
826 value
|= lsmash_bits_mask_lsb8( bits
->cache
>> bits
->store
, width
);
832 bitstream with bytestream for adhoc operation
835 lsmash_bits_t
* lsmash_bits_adhoc_create()
837 lsmash_bs_t
* bs
= lsmash_bs_create();
840 lsmash_bits_t
* bits
= lsmash_bits_create( bs
);
843 lsmash_bs_cleanup( bs
);
849 void lsmash_bits_adhoc_cleanup( lsmash_bits_t
* bits
)
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
);
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 )
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
)
892 lsmash_multiple_buffers_t
*multiple_buffer
= lsmash_malloc( sizeof(lsmash_multiple_buffers_t
) );
893 if( !multiple_buffer
)
895 multiple_buffer
->buffers
= lsmash_malloc( number_of_buffers
* buffer_size
);
896 if( !multiple_buffer
->buffers
)
898 lsmash_free( multiple_buffer
);
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
)
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
)
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
)
922 if( buffer_size
> multiple_buffer
->buffer_size
)
924 temp
= lsmash_realloc( multiple_buffer
->buffers
, multiple_buffer
->number_of_buffers
* buffer_size
);
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
);
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
);
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
)
949 if( multiple_buffer
->buffers
)
950 lsmash_free( multiple_buffer
->buffers
);
951 lsmash_free( multiple_buffer
);