1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2010-2012 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 */
31 uint64_t lsmash_bs_get_pos( lsmash_bs_t
*bs
)
36 void lsmash_bs_empty( lsmash_bs_t
*bs
)
40 memset( bs
->data
, 0, bs
->alloc
);
45 void lsmash_bs_free( lsmash_bs_t
*bs
)
55 void lsmash_bs_alloc( lsmash_bs_t
*bs
, uint64_t size
)
57 if( (bs
->alloc
>= size
) || bs
->error
)
59 uint64_t alloc
= size
+ (1<<16);
62 data
= malloc( alloc
);
64 data
= realloc( bs
->data
, alloc
);
75 /*---- bitstream writer ----*/
76 void lsmash_bs_put_byte( lsmash_bs_t
*bs
, uint8_t value
)
78 lsmash_bs_alloc( bs
, bs
->store
+ 1 );
81 bs
->data
[bs
->store
++] = value
;
84 void lsmash_bs_put_bytes( lsmash_bs_t
*bs
, uint32_t size
, void *value
)
88 lsmash_bs_alloc( bs
, bs
->store
+ size
);
91 memcpy( bs
->data
+ bs
->store
, value
, size
);
95 void lsmash_bs_put_be16( lsmash_bs_t
*bs
, uint16_t value
)
97 lsmash_bs_put_byte( bs
, (uint8_t)((value
>>8)&0xff) );
98 lsmash_bs_put_byte( bs
, (uint8_t)(value
&0xff) );
101 void lsmash_bs_put_be24( lsmash_bs_t
*bs
, uint32_t value
)
103 lsmash_bs_put_byte( bs
, (uint8_t)((value
>>16)&0xff) );
104 lsmash_bs_put_be16( bs
, (uint16_t)(value
&0xffff) );
107 void lsmash_bs_put_be32( lsmash_bs_t
*bs
, uint32_t value
)
109 lsmash_bs_put_be16( bs
, (uint16_t)((value
>>16)&0xffff) );
110 lsmash_bs_put_be16( bs
, (uint16_t)(value
&0xffff) );
113 void lsmash_bs_put_be64( lsmash_bs_t
*bs
, uint64_t value
)
115 lsmash_bs_put_be32( bs
, (uint32_t)((value
>>32)&0xffffffff) );
116 lsmash_bs_put_be32( bs
, (uint32_t)(value
&0xffffffff) );
119 void lsmash_bs_put_byte_from_64( lsmash_bs_t
*bs
, uint64_t value
)
121 lsmash_bs_put_byte( bs
, (uint8_t)(value
&0xff) );
124 void lsmash_bs_put_be16_from_64( lsmash_bs_t
*bs
, uint64_t value
)
126 lsmash_bs_put_be16( bs
, (uint16_t)(value
&0xffff) );
129 void lsmash_bs_put_be24_from_64( lsmash_bs_t
*bs
, uint64_t value
)
131 lsmash_bs_put_be24( bs
, (uint32_t)(value
&0xffffff) );
134 void lsmash_bs_put_be32_from_64( lsmash_bs_t
*bs
, uint64_t value
)
136 lsmash_bs_put_be32( bs
, (uint32_t)(value
&0xffffffff) );
139 int lsmash_bs_write_data( lsmash_bs_t
*bs
)
143 if( !bs
->store
|| !bs
->data
)
145 if( bs
->error
|| !bs
->stream
|| fwrite( bs
->data
, 1, bs
->store
, bs
->stream
) != bs
->store
)
147 lsmash_bs_free( bs
);
151 bs
->written
+= bs
->store
;
156 lsmash_bs_t
* lsmash_bs_create( char* filename
)
158 lsmash_bs_t
* bs
= lsmash_malloc_zero( sizeof(lsmash_bs_t
) );
161 if( filename
&& (bs
->stream
= fopen( filename
, "wb" )) == NULL
)
169 void lsmash_bs_cleanup( lsmash_bs_t
*bs
)
174 fclose( bs
->stream
);
175 lsmash_bs_free( bs
);
179 void* lsmash_bs_export_data( lsmash_bs_t
*bs
, uint32_t* length
)
181 if( !bs
|| !bs
->data
|| bs
->store
== 0 || bs
->error
)
183 void *buf
= lsmash_memdup( bs
->data
, bs
->store
);
192 /*---- bitstream reader ----*/
193 uint8_t lsmash_bs_show_byte( lsmash_bs_t
*bs
, uint32_t offset
)
195 if( bs
->error
|| !bs
->data
)
197 if( bs
->pos
+ offset
> bs
->store
)
199 lsmash_bs_free( bs
);
203 return bs
->data
[bs
->pos
+ offset
];
206 uint8_t lsmash_bs_get_byte( lsmash_bs_t
*bs
)
208 if( bs
->error
|| !bs
->data
)
210 if( bs
->pos
+ 1 > bs
->store
)
212 lsmash_bs_free( bs
);
216 return bs
->data
[bs
->pos
++];
219 uint8_t *lsmash_bs_get_bytes( lsmash_bs_t
*bs
, uint32_t size
)
221 if( bs
->error
|| !size
)
223 if( bs
->pos
+ size
> bs
->store
)
225 lsmash_bs_free( bs
);
229 uint8_t *value
= lsmash_memdup( bs
->data
+ bs
->pos
, size
);
232 lsmash_bs_free( bs
);
240 uint16_t lsmash_bs_get_be16( lsmash_bs_t
*bs
)
242 uint16_t value
= lsmash_bs_get_byte( bs
);
243 return (value
<<8) | lsmash_bs_get_byte( bs
);
246 uint32_t lsmash_bs_get_be24( lsmash_bs_t
*bs
)
248 uint32_t value
= lsmash_bs_get_byte( bs
);
249 return (value
<<16) | lsmash_bs_get_be16( bs
);
252 uint32_t lsmash_bs_get_be32( lsmash_bs_t
*bs
)
254 uint32_t value
= lsmash_bs_get_be16( bs
);
255 return (value
<<16) | lsmash_bs_get_be16( bs
);
258 uint64_t lsmash_bs_get_be64( lsmash_bs_t
*bs
)
260 uint64_t value
= lsmash_bs_get_be32( bs
);
261 return (value
<<32) | lsmash_bs_get_be32( bs
);
264 uint64_t lsmash_bs_get_byte_to_64( lsmash_bs_t
*bs
)
266 return lsmash_bs_get_byte( bs
);
269 uint64_t lsmash_bs_get_be16_to_64( lsmash_bs_t
*bs
)
271 return lsmash_bs_get_be16( bs
);
274 uint64_t lsmash_bs_get_be24_to_64( lsmash_bs_t
*bs
)
276 return lsmash_bs_get_be24( bs
);
279 uint64_t lsmash_bs_get_be32_to_64( lsmash_bs_t
*bs
)
281 return lsmash_bs_get_be32( bs
);
284 int lsmash_bs_read_data( lsmash_bs_t
*bs
, uint32_t size
)
290 lsmash_bs_alloc( bs
, bs
->store
+ size
);
291 if( bs
->error
|| !bs
->stream
)
293 lsmash_bs_free( bs
);
297 uint64_t read_size
= fread( bs
->data
+ bs
->store
, 1, size
, bs
->stream
);
298 if( read_size
!= size
&& !feof( bs
->stream
) )
303 bs
->store
+= read_size
;
307 int lsmash_bs_import_data( lsmash_bs_t
*bs
, void* data
, uint32_t length
)
309 if( !bs
|| bs
->error
|| !data
|| length
== 0 )
311 lsmash_bs_alloc( bs
, bs
->store
+ length
);
312 if( bs
->error
|| !bs
->data
) /* means, failed to alloc. */
314 lsmash_bs_free( bs
);
317 memcpy( bs
->data
+ bs
->store
, data
, length
);
323 /*---- bitstream ----*/
324 void lsmash_bits_init( lsmash_bits_t
*bits
, lsmash_bs_t
*bs
)
326 debug_if( !bits
|| !bs
)
333 lsmash_bits_t
*lsmash_bits_create( lsmash_bs_t
*bs
)
337 lsmash_bits_t
*bits
= (lsmash_bits_t
*)malloc( sizeof(lsmash_bits_t
) );
340 lsmash_bits_init( bits
, bs
);
344 void lsmash_bits_empty( lsmash_bits_t
*bits
)
348 lsmash_bs_empty( bits
->bs
);
353 #define BITS_IN_BYTE 8
354 void lsmash_bits_put_align( lsmash_bits_t
*bits
)
360 lsmash_bs_put_byte( bits
->bs
, bits
->cache
<< ( BITS_IN_BYTE
- bits
->store
) );
363 void lsmash_bits_get_align( lsmash_bits_t
*bits
)
371 /* Must be used ONLY for bits struct created with isom_create_bits.
372 Otherwise, just free() the bits struct. */
373 void lsmash_bits_cleanup( lsmash_bits_t
*bits
)
380 /* we can change value's type to unsigned int for 64-bit operation if needed. */
381 static inline uint8_t lsmash_bits_mask_lsb8( uint32_t value
, uint32_t width
)
383 return (uint8_t)( value
& ~( ~0U << width
) );
386 /* We can change value's type to unsigned int for 64-bit operation if needed. */
387 void lsmash_bits_put( lsmash_bits_t
*bits
, uint32_t width
, uint32_t value
)
389 debug_if( !bits
|| !width
)
393 if( bits
->store
+ width
< BITS_IN_BYTE
)
395 /* cache can contain all of value's bits. */
396 bits
->cache
<<= width
;
397 bits
->cache
|= lsmash_bits_mask_lsb8( value
, width
);
398 bits
->store
+= width
;
401 /* flush cache with value's some leading bits. */
402 uint32_t free_bits
= BITS_IN_BYTE
- bits
->store
;
403 bits
->cache
<<= free_bits
;
404 bits
->cache
|= lsmash_bits_mask_lsb8( value
>> (width
-= free_bits
), free_bits
);
405 lsmash_bs_put_byte( bits
->bs
, bits
->cache
);
409 /* cache is empty here. */
410 /* byte unit operation. */
411 while( width
> BITS_IN_BYTE
)
412 lsmash_bs_put_byte( bits
->bs
, (uint8_t)(value
>> (width
-= BITS_IN_BYTE
)) );
413 /* bit unit operation for residual. */
416 bits
->cache
= lsmash_bits_mask_lsb8( value
, width
);
421 /* We can change value's type to unsigned int for 64-bit operation if needed. */
422 uint32_t lsmash_bits_get( lsmash_bits_t
*bits
, uint32_t width
)
424 debug_if( !bits
|| !width
)
429 if( bits
->store
>= width
)
431 /* cache contains all of bits required. */
432 bits
->store
-= width
;
433 return lsmash_bits_mask_lsb8( bits
->cache
>> bits
->store
, width
);
435 /* fill value's leading bits with cache's residual. */
436 value
= lsmash_bits_mask_lsb8( bits
->cache
, bits
->store
);
437 width
-= bits
->store
;
441 /* cache is empty here. */
442 /* byte unit operation. */
443 while( width
> BITS_IN_BYTE
)
445 value
<<= BITS_IN_BYTE
;
446 width
-= BITS_IN_BYTE
;
447 value
|= lsmash_bs_get_byte( bits
->bs
);
449 /* bit unit operation for residual. */
452 bits
->cache
= lsmash_bs_get_byte( bits
->bs
);
453 bits
->store
= BITS_IN_BYTE
- width
;
455 value
|= lsmash_bits_mask_lsb8( bits
->cache
>> bits
->store
, width
);
461 bitstream with bytestream for adhoc operation
464 lsmash_bits_t
* lsmash_bits_adhoc_create()
466 lsmash_bs_t
* bs
= lsmash_bs_create( NULL
); /* no file writing */
469 lsmash_bits_t
* bits
= lsmash_bits_create( bs
);
472 lsmash_bs_cleanup( bs
);
478 void lsmash_bits_adhoc_cleanup( lsmash_bits_t
* bits
)
482 lsmash_bs_cleanup( bits
->bs
);
483 lsmash_bits_cleanup( bits
);
486 void* lsmash_bits_export_data( lsmash_bits_t
* bits
, uint32_t* length
)
488 lsmash_bits_put_align( bits
);
489 return lsmash_bs_export_data( bits
->bs
, length
);
492 int lsmash_bits_import_data( lsmash_bits_t
* bits
, void* data
, uint32_t length
)
494 return lsmash_bs_import_data( bits
->bs
, data
, length
);
499 void lsmash_init_entry_list( lsmash_entry_list_t
*list
)
503 list
->last_accessed_entry
= NULL
;
504 list
->last_accessed_number
= 0;
505 list
->entry_count
= 0;
508 lsmash_entry_list_t
*lsmash_create_entry_list( void )
510 lsmash_entry_list_t
*list
= malloc( sizeof(lsmash_entry_list_t
) );
513 lsmash_init_entry_list( list
);
517 int lsmash_add_entry( lsmash_entry_list_t
*list
, void *data
)
521 lsmash_entry_t
*entry
= malloc( sizeof(lsmash_entry_t
) );
525 entry
->prev
= list
->tail
;
528 list
->tail
->next
= entry
;
532 list
->entry_count
+= 1;
536 int lsmash_remove_entry_direct( lsmash_entry_list_t
*list
, lsmash_entry_t
*entry
, void* eliminator
)
542 lsmash_entry_t
*next
= entry
->next
;
543 lsmash_entry_t
*prev
= entry
->prev
;
544 if( entry
== list
->head
)
548 if( entry
== list
->tail
)
553 ((lsmash_entry_data_eliminator
)eliminator
)( entry
->data
);
554 if( entry
== list
->last_accessed_entry
)
557 list
->last_accessed_entry
= next
;
560 list
->last_accessed_entry
= prev
;
561 list
->last_accessed_number
-= 1;
565 list
->last_accessed_entry
= NULL
;
566 list
->last_accessed_number
= 0;
571 /* We can't know the current entry number immediately,
572 * so discard the last accessed entry info because time is wasted to know it. */
573 list
->last_accessed_entry
= NULL
;
574 list
->last_accessed_number
= 0;
577 list
->entry_count
-= 1;
581 int lsmash_remove_entry( lsmash_entry_list_t
*list
, uint32_t entry_number
, void* eliminator
)
583 lsmash_entry_t
*entry
= lsmash_get_entry( list
, entry_number
);
584 return lsmash_remove_entry_direct( list
, entry
, eliminator
);
587 void lsmash_remove_entries( lsmash_entry_list_t
*list
, void* eliminator
)
593 for( lsmash_entry_t
*entry
= list
->head
; entry
; )
595 lsmash_entry_t
*next
= entry
->next
;
597 ((lsmash_entry_data_eliminator
)eliminator
)( entry
->data
);
601 lsmash_init_entry_list( list
);
604 void lsmash_remove_list( lsmash_entry_list_t
*list
, void* eliminator
)
608 lsmash_remove_entries( list
, eliminator
);
612 lsmash_entry_t
*lsmash_get_entry( lsmash_entry_list_t
*list
, uint32_t entry_number
)
614 if( !list
|| !entry_number
|| entry_number
> list
->entry_count
)
617 lsmash_entry_t
*entry
= NULL
;
618 if( list
->last_accessed_entry
)
620 if( entry_number
== list
->last_accessed_number
)
621 entry
= list
->last_accessed_entry
;
622 else if( entry_number
== list
->last_accessed_number
+ 1 )
623 entry
= list
->last_accessed_entry
->next
;
624 else if( entry_number
== list
->last_accessed_number
- 1 )
625 entry
= list
->last_accessed_entry
->prev
;
633 if( entry_number
<= (list
->entry_count
>> 1) )
635 /* Look for from the head. */
636 uint32_t distance_plus_one
= entry_number
;
637 for( entry
= list
->head
; entry
&& --distance_plus_one
; entry
= entry
->next
);
641 /* Look for from the tail. */
642 uint32_t distance
= list
->entry_count
- entry_number
;
643 for( entry
= list
->tail
; entry
&& distance
--; entry
= entry
->prev
);
648 list
->last_accessed_entry
= entry
;
649 list
->last_accessed_number
= entry_number
;
654 void *lsmash_get_entry_data( lsmash_entry_list_t
*list
, uint32_t entry_number
)
656 lsmash_entry_t
*entry
= lsmash_get_entry( list
, entry_number
);
657 return entry
? entry
->data
: NULL
;
662 double lsmash_fixed2double( uint64_t value
, int frac_width
)
664 return value
/ (double)(1ULL << frac_width
);
667 float lsmash_int2float32( uint32_t value
)
669 return (union {uint32_t i
; float f
;}){value
}.f
;
672 double lsmash_int2float64( uint64_t value
)
674 return (union {uint64_t i
; double d
;}){value
}.d
;
678 /*---- allocator ----*/
679 void *lsmash_malloc_zero( size_t size
)
683 void *p
= malloc( size
);
686 memset( p
, 0, size
);
690 void *lsmash_memdup( void *src
, size_t size
)
694 void *dst
= malloc( size
);
697 memcpy( dst
, src
, size
);
701 lsmash_multiple_buffers_t
*lsmash_create_multiple_buffers( uint32_t number_of_buffers
, uint32_t buffer_size
)
703 if( (uint64_t)number_of_buffers
* buffer_size
> UINT32_MAX
)
705 lsmash_multiple_buffers_t
*multiple_buffer
= malloc( sizeof(lsmash_multiple_buffers_t
) );
706 if( !multiple_buffer
)
708 multiple_buffer
->buffers
= malloc( number_of_buffers
* buffer_size
);
709 if( !multiple_buffer
->buffers
)
711 free( multiple_buffer
);
714 multiple_buffer
->number_of_buffers
= number_of_buffers
;
715 multiple_buffer
->buffer_size
= buffer_size
;
716 return multiple_buffer
;
719 void *lsmash_withdraw_buffer( lsmash_multiple_buffers_t
*multiple_buffer
, uint32_t buffer_number
)
721 if( !multiple_buffer
|| !buffer_number
|| buffer_number
> multiple_buffer
->number_of_buffers
)
723 return multiple_buffer
->buffers
+ (buffer_number
- 1) * multiple_buffer
->buffer_size
;
726 lsmash_multiple_buffers_t
*lsmash_resize_multiple_buffers( lsmash_multiple_buffers_t
*multiple_buffer
, uint32_t buffer_size
)
728 if( !multiple_buffer
)
730 if( buffer_size
== multiple_buffer
->buffer_size
)
731 return multiple_buffer
;
732 if( (uint64_t)multiple_buffer
->number_of_buffers
* buffer_size
> UINT32_MAX
)
735 if( buffer_size
> multiple_buffer
->buffer_size
)
737 temp
= realloc( multiple_buffer
->buffers
, multiple_buffer
->number_of_buffers
* buffer_size
);
740 for( uint32_t i
= multiple_buffer
->number_of_buffers
- 1; i
; i
-- )
741 memmove( temp
+ buffer_size
, temp
+ i
* multiple_buffer
->buffer_size
, multiple_buffer
->buffer_size
);
745 for( uint32_t i
= 1; i
< multiple_buffer
->number_of_buffers
; i
++ )
746 memmove( multiple_buffer
->buffers
+ buffer_size
, multiple_buffer
->buffers
+ i
* multiple_buffer
->buffer_size
, multiple_buffer
->buffer_size
);
747 temp
= realloc( multiple_buffer
->buffers
, multiple_buffer
->number_of_buffers
* buffer_size
);
751 multiple_buffer
->buffers
= temp
;
752 multiple_buffer
->buffer_size
= buffer_size
;
753 return multiple_buffer
;
756 void lsmash_destroy_multiple_buffers( lsmash_multiple_buffers_t
*multiple_buffer
)
758 if( !multiple_buffer
)
760 if( multiple_buffer
->buffers
)
761 free( multiple_buffer
->buffers
);
762 free( multiple_buffer
);
767 void lsmash_log( lsmash_log_level level
, const char* message
, ... )
772 va_start( args
, message
);
775 case LSMASH_LOG_ERROR
:
778 case LSMASH_LOG_WARNING
:
781 case LSMASH_LOG_INFO
:
789 fprintf( stderr
, "[%s]: ", prefix
);
790 vfprintf( stderr
, message
, args
);
794 uint32_t lsmash_count_bits( uint32_t bits
)
796 bits
= (bits
& 0x55555555) + ((bits
>> 1) & 0x55555555);
797 bits
= (bits
& 0x33333333) + ((bits
>> 2) & 0x33333333);
798 bits
= (bits
& 0x0f0f0f0f) + ((bits
>> 4) & 0x0f0f0f0f);
799 bits
= (bits
& 0x00ff00ff) + ((bits
>> 8) & 0x00ff00ff);
800 return (bits
& 0x0000ffff) + ((bits
>> 16) & 0x0000ffff);
803 /* for qsort function */
804 int lsmash_compare_dts( const lsmash_media_ts_t
*a
, const lsmash_media_ts_t
*b
)
806 int64_t diff
= (int64_t)(a
->dts
- b
->dts
);
807 return diff
> 0 ? 1 : (diff
== 0 ? 0 : -1);
810 int lsmash_compare_cts( const lsmash_media_ts_t
*a
, const lsmash_media_ts_t
*b
)
812 int64_t diff
= (int64_t)(a
->cts
- b
->cts
);
813 return diff
> 0 ? 1 : (diff
== 0 ? 0 : -1);