write: Fix type size for mdcv luminance
[L-SMASH.git] / codecs / nalu.c
blob203803d8c74c48bf185219464423c2e9d8712b40
1 /*****************************************************************************
2 * nalu.c
3 *****************************************************************************
4 * Copyright (C) 2010-2017 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 "common/internal.h" /* must be placed first */
25 #include <string.h>
27 #include "core/box.h"
29 #include "nalu.h"
31 isom_dcr_ps_entry_t *isom_create_ps_entry
33 uint8_t *ps,
34 uint32_t ps_size
37 isom_dcr_ps_entry_t *entry = lsmash_malloc( sizeof(isom_dcr_ps_entry_t) );
38 if( !entry )
39 return NULL;
40 entry->nalUnit = lsmash_memdup( ps, ps_size );
41 if( !entry->nalUnit )
43 lsmash_free( entry );
44 return NULL;
46 entry->nalUnitLength = ps_size;
47 entry->unused = 0;
48 return entry;
51 void isom_remove_dcr_ps
53 isom_dcr_ps_entry_t *ps
56 if( !ps )
57 return;
58 lsmash_free( ps->nalUnit );
59 lsmash_free( ps );
62 /* Convert EBSP (Encapsulated Byte Sequence Packets) to RBSP (Raw Byte Sequence Packets). */
63 static uint8_t *nalu_remove_emulation_prevention
65 uint8_t *src,
66 uint64_t src_length,
67 uint8_t *dst
70 uint8_t *src_end = src + src_length;
71 while( src < src_end )
72 if( ((src + 2) < src_end) && !src[0] && !src[1] && (src[2] == 0x03) )
74 /* 0x000003 -> 0x0000 */
75 *dst++ = *src++;
76 *dst++ = *src++;
77 src++; /* Skip emulation_prevention_three_byte (0x03). */
79 else
80 *dst++ = *src++;
81 return dst;
84 int nalu_import_rbsp_from_ebsp
86 lsmash_bits_t *bits,
87 uint8_t *rbsp_buffer,
88 uint64_t *rbsp_size,
89 uint8_t *ebsp,
90 uint64_t ebsp_size
93 uint8_t *rbsp_start = rbsp_buffer;
94 uint8_t *rbsp_end = nalu_remove_emulation_prevention( ebsp, ebsp_size, rbsp_buffer );
95 *rbsp_size = (uint64_t)(rbsp_end - rbsp_start);
96 if( *rbsp_size > ebsp_size )
97 return LSMASH_ERR_INVALID_DATA;
98 return lsmash_bits_import_data( bits, rbsp_start, *rbsp_size );
101 int nalu_check_more_rbsp_data
103 lsmash_bits_t *bits
106 lsmash_bs_t *bs = bits->bs;
107 lsmash_buffer_t *buffer = &bs->buffer;
108 if( buffer->pos < buffer->store && !(bits->store == 0 && (buffer->store == buffer->pos + 1)) )
109 return 1; /* rbsp_trailing_bits will be placed at the next or later byte.
110 * Note: bs->pos points at the next byte if bits->store isn't empty. */
111 if( bits->store == 0 )
113 if( buffer->store == buffer->pos + 1 )
114 return buffer->data[ buffer->pos ] != 0x80;
115 /* No rbsp_trailing_bits is present in RBSP data. */
116 bs->error = 1;
117 return 0;
119 /* Check whether remainder of bits is identical to rbsp_trailing_bits. */
120 uint8_t remainder_bits = bits->cache & ~(~0U << bits->store);
121 uint8_t rbsp_trailing_bits = 1U << (bits->store - 1);
122 return remainder_bits != rbsp_trailing_bits;
125 int nalu_get_max_ps_length
127 lsmash_entry_list_t *ps_list,
128 uint32_t *max_ps_length
131 *max_ps_length = 0;
132 for( lsmash_entry_t *entry = ps_list->head; entry; entry = entry->next )
134 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
135 if( !ps )
136 return LSMASH_ERR_NAMELESS;
137 if( ps->unused )
138 continue;
139 *max_ps_length = LSMASH_MAX( *max_ps_length, ps->nalUnitLength );
141 return 0;
144 int nalu_get_ps_count
146 lsmash_entry_list_t *ps_list,
147 uint32_t *ps_count
150 *ps_count = 0;
151 for( lsmash_entry_t *entry = ps_list ? ps_list->head : NULL; entry; entry = entry->next )
153 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
154 if( !ps )
155 return LSMASH_ERR_NAMELESS;
156 if( ps->unused )
157 continue;
158 ++(*ps_count);
160 return 0;
163 int nalu_check_same_ps_existence
165 lsmash_entry_list_t *ps_list,
166 void *ps_data,
167 uint32_t ps_length
170 for( lsmash_entry_t *entry = ps_list->head; entry; entry = entry->next )
172 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
173 if( !ps )
174 return LSMASH_ERR_NAMELESS;
175 if( ps->unused )
176 continue;
177 if( ps->nalUnitLength == ps_length && !memcmp( ps->nalUnit, ps_data, ps_length ) )
178 return 1; /* The same parameter set already exists. */
180 return 0;
183 int nalu_get_dcr_ps
185 lsmash_bs_t *bs,
186 lsmash_entry_list_t *list,
187 uint8_t entry_count
190 for( uint8_t i = 0; i < entry_count; i++ )
192 isom_dcr_ps_entry_t *data = lsmash_malloc( sizeof(isom_dcr_ps_entry_t) );
193 if( !data )
194 return LSMASH_ERR_MEMORY_ALLOC;
195 if( lsmash_list_add_entry( list, data ) < 0 )
197 lsmash_free( data );
198 return LSMASH_ERR_MEMORY_ALLOC;
200 data->nalUnitLength = lsmash_bs_get_be16( bs );
201 data->nalUnit = lsmash_bs_get_bytes( bs, data->nalUnitLength );
202 if( !data->nalUnit )
204 lsmash_list_remove_entries( list );
205 return LSMASH_ERR_NAMELESS;
208 return 0;
211 /* Return the offset from the beginning of stream if a start code is found.
212 * Return NALU_NO_START_CODE_FOUND otherwise. */
213 uint64_t nalu_find_first_start_code
215 lsmash_bs_t *bs
218 uint64_t first_sc_head_pos = 0;
219 while( 1 )
221 if( lsmash_bs_is_error( bs ) )
222 return NALU_IO_ERROR;
223 if( lsmash_bs_is_end( bs, first_sc_head_pos + NALU_LONG_START_CODE_LENGTH ) )
224 return NALU_NO_START_CODE_FOUND;
225 /* Invalid if encountered any value of non-zero before the first start code. */
226 if( lsmash_bs_show_byte( bs, first_sc_head_pos ) )
227 return NALU_NO_START_CODE_FOUND;
228 /* The first NALU of an AU in decoding order shall have long start code (0x00000001). */
229 if( 0x00000001 == lsmash_bs_show_be32( bs, first_sc_head_pos ) )
230 break;
231 ++first_sc_head_pos;
233 return first_sc_head_pos;
236 uint64_t nalu_get_codeNum
238 lsmash_bits_t *bits
241 uint32_t leadingZeroBits = 0;
242 for( int b = 0; !b; leadingZeroBits++ )
243 b = lsmash_bits_get( bits, 1 );
244 --leadingZeroBits;
245 return ((uint64_t)1 << leadingZeroBits) - 1 + lsmash_bits_get( bits, leadingZeroBits );
248 int nalu_update_bitrate( isom_stbl_t *stbl, isom_mdhd_t *mdhd, uint32_t sample_description_index )
250 isom_visual_entry_t *sample_entry = (isom_visual_entry_t *)lsmash_list_get_entry_data( &stbl->stsd->list, sample_description_index );
251 if( LSMASH_IS_NON_EXISTING_BOX( sample_entry ) )
252 return LSMASH_ERR_INVALID_DATA;
253 isom_btrt_t *btrt = (isom_btrt_t *)isom_get_extension_box_format( &sample_entry->extensions, ISOM_BOX_TYPE_BTRT );
254 if( LSMASH_IS_EXISTING_BOX( btrt ) )
256 uint32_t bufferSizeDB;
257 uint32_t maxBitrate;
258 uint32_t avgBitrate;
259 int err = isom_calculate_bitrate_description( stbl, mdhd, &bufferSizeDB, &maxBitrate, &avgBitrate, sample_description_index );
260 if( err < 0 )
261 return err;
262 btrt->bufferSizeDB = bufferSizeDB;
263 btrt->maxBitrate = maxBitrate;
264 btrt->avgBitrate = avgBitrate;
266 return 0;