1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2010-2015 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
)
42 lsmash_free( bs
->buffer
.data
);
43 bs
->buffer
.data
= NULL
;
49 void lsmash_bs_cleanup( lsmash_bs_t
*bs
)
57 int lsmash_bs_set_empty_stream( lsmash_bs_t
*bs
, uint8_t *data
, size_t size
)
60 return LSMASH_ERR_FUNCTION_PARAM
;
61 bs
->stream
= NULL
; /* empty stream */
62 bs
->eof
= 1; /* unreadable because of empty stream */
63 bs
->eob
= 0; /* readable on the buffer */
65 bs
->unseekable
= 1; /* only seek on the buffer */
66 bs
->written
= size
; /* behave as if the size of the empty stream is 'size' */
67 bs
->offset
= size
; /* behave as if the poiter of the stream is at the end */
68 bs
->buffer
.unseekable
= 0; /* only seek on the buffer */
69 bs
->buffer
.internal
= 0; /* must not be allocated internally */
70 bs
->buffer
.data
= data
;
71 bs
->buffer
.store
= size
;
72 bs
->buffer
.alloc
= size
;
74 bs
->buffer
.max_size
= 0; /* make no sense */
82 void lsmash_bs_empty( lsmash_bs_t
*bs
)
87 memset( bs
->buffer
.data
, 0, bs
->buffer
.alloc
);
92 static void bs_alloc( lsmash_bs_t
*bs
, size_t alloc
)
94 if( (bs
->buffer
.alloc
>= alloc
) || bs
->error
)
96 if( !bs
->buffer
.internal
)
98 /* We cannot re-allocate the memory block. */
102 alloc
= LSMASH_MAX( alloc
, bs
->buffer
.max_size
);
104 if( !bs
->buffer
.data
)
105 data
= lsmash_malloc( alloc
);
107 data
= lsmash_realloc( bs
->buffer
.data
, alloc
);
110 bs_buffer_free( bs
);
114 bs
->buffer
.internal
= 1;
115 bs
->buffer
.data
= data
;
116 bs
->buffer
.alloc
= alloc
;
119 static uint64_t bs_estimate_seek_offset( lsmash_bs_t
*bs
, int64_t offset
, int whence
)
121 /* Calculate the offset after the seek. */
123 if( whence
== SEEK_SET
)
125 assert( offset
>= 0 );
126 if( bs
->written
< offset
)
127 dst_offset
= bs
->written
;
131 else if( whence
== SEEK_CUR
)
133 if( offset
< 0 && bs
->offset
< -offset
)
135 else if( offset
> 0 && bs
->written
< bs
->offset
+ offset
)
136 dst_offset
= bs
->written
;
138 dst_offset
= bs
->offset
+ offset
;
140 else /* if( whence == SEEK_END ) */
142 assert( offset
<= 0 );
143 if( bs
->written
< -offset
)
146 dst_offset
= bs
->written
+ offset
;
151 /* TODO: Support offset > INT64_MAX */
152 int64_t lsmash_bs_write_seek( lsmash_bs_t
*bs
, int64_t offset
, int whence
)
155 return LSMASH_ERR_NAMELESS
;
156 if( whence
!= SEEK_SET
&& whence
!= SEEK_CUR
&& whence
!= SEEK_END
)
157 return LSMASH_ERR_FUNCTION_PARAM
;
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
)
172 return LSMASH_ERR_FUNCTION_PARAM
;
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
);
191 return LSMASH_ERR_NAMELESS
;
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
)
304 return LSMASH_ERR_FUNCTION_PARAM
;
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
);
313 return LSMASH_ERR_NAMELESS
;
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
)
327 return LSMASH_ERR_FUNCTION_PARAM
;
328 if( !buf
|| size
== 0 )
330 if( bs
->error
|| !bs
->stream
)
332 bs_buffer_free( bs
);
334 return LSMASH_ERR_NAMELESS
;
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
? LSMASH_ERR_NAMELESS
: 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
;
540 return LSMASH_ERR_NAMELESS
;
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
)
577 return LSMASH_ERR_NAMELESS
;
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 uint16_t lsmash_bs_get_le16( lsmash_bs_t
*bs
)
627 uint16_t value
= lsmash_bs_get_byte( bs
);
628 return value
| (lsmash_bs_get_byte( bs
) << 8);
631 uint32_t lsmash_bs_get_le32( lsmash_bs_t
*bs
)
633 uint32_t value
= lsmash_bs_get_le16( bs
);
634 return value
| (lsmash_bs_get_le16( bs
) << 16);
637 int lsmash_bs_read( lsmash_bs_t
*bs
, uint32_t size
)
639 if( !bs
|| size
> INT_MAX
)
640 return LSMASH_ERR_FUNCTION_PARAM
;
643 bs_alloc( bs
, bs
->buffer
.store
+ size
);
644 if( bs
->error
|| !bs
->stream
)
647 return LSMASH_ERR_NAMELESS
;
649 int read_size
= bs
->read( bs
->stream
, lsmash_bs_get_buffer_data_end( bs
), size
);
655 else if( read_size
< 0 )
658 return LSMASH_ERR_NAMELESS
;
660 bs
->buffer
.store
+= read_size
;
661 bs
->offset
+= read_size
;
662 bs
->written
= LSMASH_MAX( bs
->written
, bs
->offset
);
666 int lsmash_bs_read_data( lsmash_bs_t
*bs
, uint8_t *buf
, size_t *size
)
668 if( !bs
|| !size
|| *size
> INT_MAX
)
669 return LSMASH_ERR_FUNCTION_PARAM
;
670 if( !buf
|| *size
== 0 )
672 if( bs
->error
|| !bs
->stream
)
675 return LSMASH_ERR_NAMELESS
;
677 int read_size
= bs
->read( bs
->stream
, buf
, *size
);
680 else if( read_size
< 0 )
683 return LSMASH_ERR_NAMELESS
;
685 bs
->buffer
.unseekable
= 1;
686 bs
->offset
+= read_size
;
688 bs
->written
= LSMASH_MAX( bs
->written
, bs
->offset
);
692 int lsmash_bs_import_data( lsmash_bs_t
*bs
, void *data
, uint32_t length
)
694 if( !bs
|| !data
|| length
== 0 )
695 return LSMASH_ERR_FUNCTION_PARAM
;
697 return LSMASH_ERR_NAMELESS
;
698 bs_alloc( bs
, bs
->buffer
.store
+ length
);
699 if( bs
->error
|| !bs
->buffer
.data
) /* means, failed to alloc. */
701 bs_buffer_free( bs
);
702 return LSMASH_ERR_NAMELESS
;
704 memcpy( lsmash_bs_get_buffer_data_end( bs
), data
, length
);
705 bs
->buffer
.store
+= length
;
710 /*---- basic I/O ----*/
711 int lsmash_fread_wrapper( void *opaque
, uint8_t *buf
, int size
)
713 int read_size
= fread( buf
, 1, size
, (FILE *)opaque
);
714 return ferror( (FILE *)opaque
) ? LSMASH_ERR_NAMELESS
: read_size
;
717 int lsmash_fwrite_wrapper( void *opaque
, uint8_t *buf
, int size
)
719 return fwrite( buf
, 1, size
, (FILE *)opaque
);
722 int64_t lsmash_fseek_wrapper( void *opaque
, int64_t offset
, int whence
)
724 if( lsmash_fseek( (FILE *)opaque
, offset
, whence
) != 0 )
725 return LSMASH_ERR_NAMELESS
;
726 return lsmash_ftell( (FILE *)opaque
);