1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2012-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 "common/internal.h" /* must be placed first */
33 #include "description.h"
35 typedef isom_wave_t lsmash_qt_decoder_parameters_t
;
37 static void global_destruct_specific_data( void *data
)
41 lsmash_codec_global_header_t
*global
= (lsmash_codec_global_header_t
*)data
;
42 if( global
->header_data
)
43 lsmash_free( global
->header_data
);
44 lsmash_free( global
);
47 static int isom_is_qt_video( lsmash_codec_type_t type
)
49 return lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_2VUY_VIDEO
)
50 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_APCH_VIDEO
)
51 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_APCN_VIDEO
)
52 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_APCS_VIDEO
)
53 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_APCO_VIDEO
)
54 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_AP4H_VIDEO
)
55 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_CFHD_VIDEO
)
56 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_CIVD_VIDEO
)
57 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_DVC_VIDEO
)
58 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_DVCP_VIDEO
)
59 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_DVPP_VIDEO
)
60 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_DV5N_VIDEO
)
61 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_DV5P_VIDEO
)
62 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_DVH2_VIDEO
)
63 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_DVH3_VIDEO
)
64 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_DVH5_VIDEO
)
65 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_DVH6_VIDEO
)
66 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_DVHP_VIDEO
)
67 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_DVHQ_VIDEO
)
68 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_DV10_VIDEO
)
69 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_DVOO_VIDEO
)
70 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_DVOR_VIDEO
)
71 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_DVTV_VIDEO
)
72 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_DVVT_VIDEO
)
73 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_FLIC_VIDEO
)
74 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_GIF_VIDEO
)
75 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_H261_VIDEO
)
76 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_H263_VIDEO
)
77 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_HD10_VIDEO
)
78 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_JPEG_VIDEO
)
79 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_M105_VIDEO
)
80 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_MJPA_VIDEO
)
81 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_MJPB_VIDEO
)
82 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_PNG_VIDEO
)
83 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_PNTG_VIDEO
)
84 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_RAW_VIDEO
)
85 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_RLE_VIDEO
)
86 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_RPZA_VIDEO
)
87 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_SHR0_VIDEO
)
88 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_SHR1_VIDEO
)
89 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_SHR2_VIDEO
)
90 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_SHR3_VIDEO
)
91 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_SHR4_VIDEO
)
92 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_SVQ1_VIDEO
)
93 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_SVQ3_VIDEO
)
94 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_TGA_VIDEO
)
95 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_TIFF_VIDEO
)
96 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_ULRA_VIDEO
)
97 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_ULRG_VIDEO
)
98 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_ULY2_VIDEO
)
99 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_ULY0_VIDEO
)
100 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_ULH2_VIDEO
)
101 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_ULH0_VIDEO
)
102 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_V210_VIDEO
)
103 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_V216_VIDEO
)
104 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_V308_VIDEO
)
105 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_V408_VIDEO
)
106 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_V410_VIDEO
)
107 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_YUV2_VIDEO
)
108 || lsmash_check_codec_type_identical( type
, QT_CODEC_TYPE_WRLE_VIDEO
);
111 static int isom_is_nalff( lsmash_codec_type_t type
)
113 return lsmash_check_codec_type_identical( type
, ISOM_CODEC_TYPE_AVC1_VIDEO
)
114 || lsmash_check_codec_type_identical( type
, ISOM_CODEC_TYPE_AVC2_VIDEO
)
115 || lsmash_check_codec_type_identical( type
, ISOM_CODEC_TYPE_AVC3_VIDEO
)
116 || lsmash_check_codec_type_identical( type
, ISOM_CODEC_TYPE_AVC4_VIDEO
)
117 || lsmash_check_codec_type_identical( type
, ISOM_CODEC_TYPE_AVCP_VIDEO
)
118 || lsmash_check_codec_type_identical( type
, ISOM_CODEC_TYPE_HVC1_VIDEO
)
119 || lsmash_check_codec_type_identical( type
, ISOM_CODEC_TYPE_HEV1_VIDEO
);
122 int lsmash_convert_crop_into_clap( lsmash_crop_t crop
, uint32_t width
, uint32_t height
, lsmash_clap_t
*clap
)
124 if( !clap
|| crop
.top
.d
== 0 || crop
.bottom
.d
== 0 || crop
.left
.d
== 0 || crop
.right
.d
== 0 )
126 uint64_t vertical_crop_lcm
= lsmash_get_lcm( crop
.top
.d
, crop
.bottom
.d
);
127 uint64_t horizontal_crop_lcm
= lsmash_get_lcm( crop
.left
.d
, crop
.right
.d
);
128 lsmash_rational_u64_t clap_height
;
129 lsmash_rational_u64_t clap_width
;
130 lsmash_rational_s64_t clap_horizontal_offset
;
131 lsmash_rational_s64_t clap_vertical_offset
;
132 clap_height
.d
= vertical_crop_lcm
;
133 clap_width
.d
= horizontal_crop_lcm
;
134 clap_horizontal_offset
.d
= 2 * vertical_crop_lcm
;
135 clap_vertical_offset
.d
= 2 * horizontal_crop_lcm
;
136 clap_height
.n
= height
* vertical_crop_lcm
137 - (crop
.top
.n
* (vertical_crop_lcm
/ crop
.top
.d
) + crop
.bottom
.n
* (vertical_crop_lcm
/ crop
.bottom
.d
));
138 clap_width
.n
= width
* horizontal_crop_lcm
139 - (crop
.left
.n
* (horizontal_crop_lcm
/ crop
.left
.d
) + crop
.right
.n
* (horizontal_crop_lcm
/ crop
.right
.d
));
140 clap_horizontal_offset
.n
= (int64_t)(crop
.left
.n
* (horizontal_crop_lcm
/ crop
.left
.d
))
141 - crop
.right
.n
* (horizontal_crop_lcm
/ crop
.right
.d
);
142 clap_vertical_offset
.n
= (int64_t)(crop
.top
.n
* (vertical_crop_lcm
/ crop
.top
.d
))
143 - crop
.bottom
.n
* (vertical_crop_lcm
/ crop
.bottom
.d
);
144 lsmash_reduce_fraction( &clap_height
.n
, &clap_height
.d
);
145 lsmash_reduce_fraction( &clap_width
.n
, &clap_width
.d
);
146 lsmash_reduce_fraction_su( &clap_vertical_offset
.n
, &clap_vertical_offset
.d
);
147 lsmash_reduce_fraction_su( &clap_horizontal_offset
.n
, &clap_horizontal_offset
.d
);
148 clap
->height
= (lsmash_rational_u32_t
){ clap_height
.n
, clap_height
.d
};
149 clap
->width
= (lsmash_rational_u32_t
){ clap_width
.n
, clap_width
.d
};
150 clap
->vertical_offset
= (lsmash_rational_s32_t
){ clap_vertical_offset
.n
, clap_vertical_offset
.d
};
151 clap
->horizontal_offset
= (lsmash_rational_s32_t
){ clap_horizontal_offset
.n
, clap_horizontal_offset
.d
};
155 int lsmash_convert_clap_into_crop( lsmash_clap_t clap
, uint32_t width
, uint32_t height
, lsmash_crop_t
*crop
)
157 if( !crop
|| clap
.height
.d
== 0 || clap
.vertical_offset
.d
== 0 || clap
.width
.d
== 0 || clap
.horizontal_offset
.d
== 0 )
159 uint64_t clap_vertical_lcm
= lsmash_get_lcm( clap
.height
.d
, clap
.vertical_offset
.d
);
160 uint64_t clap_horizontal_lcm
= lsmash_get_lcm( clap
.width
.d
, clap
.horizontal_offset
.d
);
161 lsmash_rational_u64_t crop_top
;
162 lsmash_rational_u64_t crop_bottom
;
163 lsmash_rational_u64_t crop_left
;
164 lsmash_rational_u64_t crop_right
;
165 crop_top
.d
= 2 * clap_vertical_lcm
;
166 crop_bottom
.d
= 2 * clap_vertical_lcm
;
167 crop_left
.d
= 2 * clap_horizontal_lcm
;
168 crop_right
.d
= 2 * clap_horizontal_lcm
;
169 crop_top
.n
= (height
* crop_top
.d
- clap
.height
.n
* (crop_top
.d
/ clap
.height
.d
)) / 2
170 + clap
.vertical_offset
.n
* (crop_top
.d
/ clap
.vertical_offset
.d
);
171 crop_bottom
.n
= (height
* crop_bottom
.d
- clap
.height
.n
* (crop_bottom
.d
/ clap
.height
.d
)) / 2
172 - clap
.vertical_offset
.n
* (crop_bottom
.d
/ clap
.vertical_offset
.d
);
173 crop_left
.n
= (width
* crop_left
.d
- clap
.width
.n
* (crop_left
.d
/ clap
.width
.d
)) / 2
174 + clap
.horizontal_offset
.n
* (crop_left
.d
/ clap
.horizontal_offset
.d
);
175 crop_right
.n
= (width
* crop_right
.d
- clap
.width
.n
* (crop_right
.d
/ clap
.width
.d
)) / 2
176 - clap
.horizontal_offset
.n
* (crop_right
.d
/ clap
.horizontal_offset
.d
);
177 lsmash_reduce_fraction( &crop_top
.n
, &crop_top
.d
);
178 lsmash_reduce_fraction( &crop_bottom
.n
, &crop_bottom
.d
);
179 lsmash_reduce_fraction( &crop_left
.n
, &crop_left
.d
);
180 lsmash_reduce_fraction( &crop_right
.n
, &crop_right
.d
);
181 crop
->top
= (lsmash_rational_u32_t
){ crop_top
.n
, crop_top
.d
};
182 crop
->bottom
= (lsmash_rational_u32_t
){ crop_bottom
.n
, crop_bottom
.d
};
183 crop
->left
= (lsmash_rational_u32_t
){ crop_left
.n
, crop_left
.d
};
184 crop
->right
= (lsmash_rational_u32_t
){ crop_right
.n
, crop_right
.d
};
188 static void isom_destruct_nothing( void *data
)
193 static int isom_initialize_structured_codec_specific_data( lsmash_codec_specific_t
*specific
)
195 extern void mp4sys_destruct_decoder_config( void * );
196 extern void h264_destruct_specific_data( void * );
197 extern void hevc_destruct_specific_data( void * );
198 extern void vc1_destruct_specific_data( void * );
199 extern void dts_destruct_specific_data( void * );
200 switch( specific
->type
)
202 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_MP4SYS_DECODER_CONFIG
:
203 specific
->size
= sizeof(lsmash_mp4sys_decoder_parameters_t
);
204 specific
->destruct
= mp4sys_destruct_decoder_config
;
206 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264
:
207 specific
->size
= sizeof(lsmash_h264_specific_parameters_t
);
208 specific
->destruct
= h264_destruct_specific_data
;
210 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_HEVC
:
211 specific
->size
= sizeof(lsmash_hevc_specific_parameters_t
);
212 specific
->destruct
= hevc_destruct_specific_data
;
214 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_VC_1
:
215 specific
->size
= sizeof(lsmash_vc1_specific_parameters_t
);
216 specific
->destruct
= vc1_destruct_specific_data
;
218 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_AC_3
:
219 specific
->size
= sizeof(lsmash_ac3_specific_parameters_t
);
220 specific
->destruct
= lsmash_free
;
222 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_EC_3
:
223 specific
->size
= sizeof(lsmash_eac3_specific_parameters_t
);
224 specific
->destruct
= lsmash_free
;
226 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_DTS
:
227 specific
->size
= sizeof(lsmash_dts_specific_parameters_t
);
228 specific
->destruct
= dts_destruct_specific_data
;
230 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_ALAC
:
231 specific
->size
= sizeof(lsmash_alac_specific_parameters_t
);
232 specific
->destruct
= lsmash_free
;
234 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_SAMPLE_SCALE
:
235 specific
->size
= sizeof(lsmash_isom_sample_scale_t
);
236 specific
->destruct
= lsmash_free
;
238 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264_BITRATE
:
239 specific
->size
= sizeof(lsmash_h264_bitrate_t
);
240 specific
->destruct
= lsmash_free
;
242 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_COMMON
:
243 specific
->size
= sizeof(lsmash_qt_video_common_t
);
244 specific
->destruct
= lsmash_free
;
246 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_COMMON
:
247 specific
->size
= sizeof(lsmash_qt_audio_common_t
);
248 specific
->destruct
= lsmash_free
;
250 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_FORMAT_SPECIFIC_FLAGS
:
251 specific
->size
= sizeof(lsmash_qt_audio_format_specific_flags_t
);
252 specific
->destruct
= lsmash_free
;
254 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_CODEC_GLOBAL_HEADER
:
255 specific
->size
= sizeof(lsmash_codec_global_header_t
);
256 specific
->destruct
= global_destruct_specific_data
;
258 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_FIELD_INFO
:
259 specific
->size
= sizeof(lsmash_qt_field_info_t
);
260 specific
->destruct
= lsmash_free
;
262 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_PIXEL_FORMAT
:
263 specific
->size
= sizeof(lsmash_qt_pixel_format_t
);
264 specific
->destruct
= lsmash_free
;
266 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_SIGNIFICANT_BITS
:
267 specific
->size
= sizeof(lsmash_qt_significant_bits_t
);
268 specific
->destruct
= lsmash_free
;
270 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_CHANNEL_LAYOUT
:
271 specific
->size
= sizeof(lsmash_qt_audio_channel_layout_t
);
272 specific
->destruct
= lsmash_free
;
276 specific
->destruct
= isom_destruct_nothing
;
279 specific
->data
.structured
= lsmash_malloc_zero( specific
->size
);
280 if( !specific
->data
.structured
)
283 specific
->destruct
= NULL
;
289 static inline int isom_initialize_codec_specific_data( lsmash_codec_specific_t
*specific
,
290 lsmash_codec_specific_data_type type
,
291 lsmash_codec_specific_format format
)
293 specific
->type
= type
;
294 specific
->format
= format
;
295 if( format
== LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
)
297 if( isom_initialize_structured_codec_specific_data( specific
) )
302 specific
->data
.unstructured
= NULL
;
304 specific
->destruct
= (lsmash_codec_specific_destructor_t
)lsmash_free
;
309 void lsmash_destroy_codec_specific_data( lsmash_codec_specific_t
*specific
)
313 if( specific
->destruct
)
315 if( specific
->format
== LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
)
317 if( specific
->data
.structured
)
318 specific
->destruct( specific
->data
.structured
);
322 if( specific
->data
.unstructured
)
323 specific
->destruct( specific
->data
.unstructured
);
326 lsmash_free( specific
);
329 lsmash_codec_specific_t
*lsmash_create_codec_specific_data( lsmash_codec_specific_data_type type
, lsmash_codec_specific_format format
)
331 lsmash_codec_specific_t
*specific
= lsmash_malloc( sizeof(lsmash_codec_specific_t
) );
334 if( isom_initialize_codec_specific_data( specific
, type
, format
) )
336 lsmash_destroy_codec_specific_data( specific
);
342 static int isom_duplicate_structured_specific_data( lsmash_codec_specific_t
*dst
, lsmash_codec_specific_t
*src
)
344 extern int mp4sys_copy_decoder_config( lsmash_codec_specific_t
*, lsmash_codec_specific_t
* );
345 extern int h264_copy_codec_specific( lsmash_codec_specific_t
*, lsmash_codec_specific_t
* );
346 extern int hevc_copy_codec_specific( lsmash_codec_specific_t
*, lsmash_codec_specific_t
* );
347 extern int vc1_copy_codec_specific( lsmash_codec_specific_t
*, lsmash_codec_specific_t
* );
348 extern int dts_copy_codec_specific( lsmash_codec_specific_t
*, lsmash_codec_specific_t
* );
349 void *src_data
= src
->data
.structured
;
350 void *dst_data
= dst
->data
.structured
;
353 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_MP4SYS_DECODER_CONFIG
:
354 return mp4sys_copy_decoder_config( dst
, src
);
355 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264
:
356 return h264_copy_codec_specific( dst
, src
);
357 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_HEVC
:
358 return hevc_copy_codec_specific( dst
, src
);
359 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_VC_1
:
360 return vc1_copy_codec_specific( dst
, src
);
361 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_AC_3
:
362 *(lsmash_ac3_specific_parameters_t
*)dst_data
= *(lsmash_ac3_specific_parameters_t
*)src_data
;
364 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_EC_3
:
365 *(lsmash_eac3_specific_parameters_t
*)dst_data
= *(lsmash_eac3_specific_parameters_t
*)src_data
;
367 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_DTS
:
368 return dts_copy_codec_specific( dst
, src
);
369 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_ALAC
:
370 *(lsmash_alac_specific_parameters_t
*)dst_data
= *(lsmash_alac_specific_parameters_t
*)src_data
;
372 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_SAMPLE_SCALE
:
373 *(lsmash_isom_sample_scale_t
*)dst_data
= *(lsmash_isom_sample_scale_t
*)src_data
;
375 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264_BITRATE
:
376 *(lsmash_h264_bitrate_t
*)dst_data
= *(lsmash_h264_bitrate_t
*)src_data
;
378 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_COMMON
:
379 *(lsmash_qt_video_common_t
*)dst_data
= *(lsmash_qt_video_common_t
*)src_data
;
381 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_COMMON
:
382 *(lsmash_qt_audio_common_t
*)dst_data
= *(lsmash_qt_audio_common_t
*)src_data
;
384 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_FORMAT_SPECIFIC_FLAGS
:
385 *(lsmash_qt_audio_format_specific_flags_t
*)dst_data
= *(lsmash_qt_audio_format_specific_flags_t
*)src_data
;
387 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_CODEC_GLOBAL_HEADER
:
389 lsmash_codec_global_header_t
*src_global
= (lsmash_codec_global_header_t
*)src_data
;
390 if( src_global
->header_data
&& src_global
->header_size
)
392 lsmash_codec_global_header_t
*dst_global
= (lsmash_codec_global_header_t
*)dst_data
;
393 dst_global
->header_data
= lsmash_memdup( src_global
->header_data
, src_global
->header_size
);
394 if( !dst_global
->header_data
)
396 dst_global
->header_size
= src_global
->header_size
;
400 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_FIELD_INFO
:
401 *(lsmash_qt_field_info_t
*)dst_data
= *(lsmash_qt_field_info_t
*)src_data
;
403 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_PIXEL_FORMAT
:
404 *(lsmash_qt_pixel_format_t
*)dst_data
= *(lsmash_qt_pixel_format_t
*)src_data
;
406 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_SIGNIFICANT_BITS
:
407 *(lsmash_qt_significant_bits_t
*)dst_data
= *(lsmash_qt_significant_bits_t
*)src_data
;
409 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_GAMMA_LEVEL
:
410 *(lsmash_qt_gamma_t
*)dst_data
= *(lsmash_qt_gamma_t
*)src_data
;
412 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_CHANNEL_LAYOUT
:
413 *(lsmash_qt_audio_channel_layout_t
*)dst_data
= *(lsmash_qt_audio_channel_layout_t
*)src_data
;
420 lsmash_codec_specific_t
*isom_duplicate_codec_specific_data( lsmash_codec_specific_t
*specific
)
424 lsmash_codec_specific_t
*dup
= lsmash_create_codec_specific_data( specific
->type
, specific
->format
);
427 if( specific
->format
== LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
)
429 if( isom_duplicate_structured_specific_data( dup
, specific
) )
431 lsmash_destroy_codec_specific_data( dup
);
437 dup
->data
.unstructured
= lsmash_memdup( specific
->data
.unstructured
, specific
->size
);
438 if( !dup
->data
.unstructured
)
440 lsmash_destroy_codec_specific_data( dup
);
444 dup
->size
= specific
->size
;
448 static size_t isom_description_read_box_common( uint8_t **p_data
, uint64_t *size
, lsmash_box_type_t
*type
)
450 uint8_t *orig
= *p_data
;
451 uint8_t *data
= *p_data
;
452 *size
= LSMASH_GET_BE32( &data
[0] );
453 type
->fourcc
= LSMASH_GET_BE32( &data
[4] );
454 data
+= ISOM_BASEBOX_COMMON_SIZE
;
457 *size
= LSMASH_GET_BE64( data
);
461 if( type
->fourcc
== ISOM_BOX_TYPE_UUID
.fourcc
)
463 type
->user
.fourcc
= LSMASH_GET_BE32( &data
[0] );
464 memcpy( type
->user
.id
, &data
[4], 12 );
469 uint8_t *isom_get_child_box_position( uint8_t *parent_data
, uint32_t parent_size
, lsmash_box_type_t child_type
, uint32_t *child_size
)
471 if( !parent_data
|| !child_size
|| parent_size
< ISOM_BASEBOX_COMMON_SIZE
)
473 uint8_t *data
= parent_data
;
475 lsmash_box_type_t type
;
476 uint32_t offset
= isom_description_read_box_common( &data
, &size
, &type
);
477 if( size
!= parent_size
)
479 uint8_t *end
= parent_data
+ parent_size
;
480 for( uint8_t *pos
= data
; pos
+ ISOM_BASEBOX_COMMON_SIZE
<= end
; )
482 offset
= isom_description_read_box_common( &pos
, &size
, &type
);
483 if( lsmash_check_box_type_identical( type
, child_type
) )
488 pos
+= size
- offset
; /* Move to the next box. */
493 static int isom_construct_global_specific_header( lsmash_codec_specific_t
*dst
, lsmash_codec_specific_t
*src
)
495 if( src
->size
< ISOM_BASEBOX_COMMON_SIZE
)
497 lsmash_codec_global_header_t
*global
= (lsmash_codec_global_header_t
*)dst
->data
.structured
;
498 uint8_t *data
= src
->data
.unstructured
;
499 uint64_t size
= LSMASH_GET_BE32( data
);
500 data
+= ISOM_BASEBOX_COMMON_SIZE
;
501 global
->header_size
= size
- ISOM_BASEBOX_COMMON_SIZE
;
504 size
= LSMASH_GET_BE64( data
);
506 global
->header_size
-= 8;
508 if( size
!= src
->size
)
510 if( global
->header_size
)
512 global
->header_data
= lsmash_memdup( data
, global
->header_size
);
513 if( !global
->header_data
)
519 static int isom_construct_audio_channel_layout( lsmash_codec_specific_t
*dst
, lsmash_codec_specific_t
*src
)
521 if( src
->size
< ISOM_FULLBOX_COMMON_SIZE
+ 12 )
523 lsmash_qt_audio_channel_layout_t
*layout
= (lsmash_qt_audio_channel_layout_t
*)dst
->data
.structured
;
524 uint8_t *data
= src
->data
.unstructured
;
525 uint64_t size
= LSMASH_GET_BE32( data
);
526 data
+= ISOM_FULLBOX_COMMON_SIZE
;
529 size
= LSMASH_GET_BE64( data
);
532 if( size
!= src
->size
)
534 layout
->channelLayoutTag
= LSMASH_GET_BE32( &data
[0] );
535 layout
->channelBitmap
= LSMASH_GET_BE32( &data
[4] );
540 static int codec_construct_qt_audio_decompression_info( lsmash_codec_specific_t
*dst
, lsmash_codec_specific_t
*src
)
542 if( src
->size
< ISOM_BASEBOX_COMMON_SIZE
)
544 uint8_t *data
= src
->data
.unstructured
;
547 uint32_t offset
= isom_description_read_box_common( &data
, &size
, &type
);
548 if( size
!= src
->size
)
550 uint8_t *end
= src
->data
.unstructured
+ src
->size
;
551 isom_wave_t
*wave
= lsmash_malloc_zero( sizeof(isom_wave_t
) );
554 wave
->type
= QT_BOX_TYPE_WAVE
;
555 for( uint8_t *pos
= data
; pos
+ ISOM_BASEBOX_COMMON_SIZE
<= end
; )
557 offset
= isom_description_read_box_common( &pos
, &size
, &type
);
560 case QT_BOX_TYPE_FRMA
:
564 isom_frma_t
*frma
= isom_add_frma( wave
);
567 frma
->data_format
= LSMASH_GET_BE32( pos
);
571 case QT_BOX_TYPE_ENDA
:
575 isom_enda_t
*enda
= isom_add_enda( wave
);
578 enda
->littleEndian
= LSMASH_GET_BE16( pos
);
581 case QT_BOX_TYPE_MP4A
:
585 isom_mp4a_t
*mp4a
= isom_add_mp4a( wave
);
588 mp4a
->unknown
= LSMASH_GET_BE32( pos
);
592 case QT_BOX_TYPE_TERMINATOR
:
594 if( !isom_add_terminator( wave
) )
600 isom_unknown_box_t
*box
= lsmash_malloc_zero( sizeof(isom_unknown_box_t
) );
603 isom_init_box_common( box
, wave
, type
, isom_remove_unknown_box
);
604 box
->unknown_size
= size
- offset
;
605 box
->unknown_field
= lsmash_memdup( pos
, box
->unknown_size
);
606 if( !box
->unknown_field
)
611 if( lsmash_add_entry( &wave
->extensions
, box
) )
613 isom_remove_unknown_box( box
);
616 pos
+= box
->unknown_size
;
625 /* structured <-> unstructured conversion might be irreversible by CODEC
626 * since structured formats we defined don't always have all contents included in unstructured data. */
627 lsmash_codec_specific_t
*lsmash_convert_codec_specific_format( lsmash_codec_specific_t
*specific
, lsmash_codec_specific_format format
)
629 if( !specific
|| format
== LSMASH_CODEC_SPECIFIC_FORMAT_UNSPECIFIED
)
631 if( format
== specific
->format
)
632 return isom_duplicate_codec_specific_data( specific
);
633 lsmash_codec_specific_t
*dst
= lsmash_create_codec_specific_data( specific
->type
, format
);
634 if( format
== LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
)
635 /* structured -> unstructured */
636 switch( specific
->type
)
638 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_MP4SYS_DECODER_CONFIG
:
639 dst
->data
.unstructured
= lsmash_create_mp4sys_decoder_config( (lsmash_mp4sys_decoder_parameters_t
*)specific
->data
.structured
, &dst
->size
);
640 if( !dst
->data
.unstructured
)
643 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264
:
644 dst
->data
.unstructured
= lsmash_create_h264_specific_info( (lsmash_h264_specific_parameters_t
*)specific
->data
.structured
, &dst
->size
);
645 if( !dst
->data
.unstructured
)
648 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_HEVC
:
649 dst
->data
.unstructured
= lsmash_create_hevc_specific_info( (lsmash_hevc_specific_parameters_t
*)specific
->data
.structured
, &dst
->size
);
650 if( !dst
->data
.unstructured
)
653 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_VC_1
:
654 dst
->data
.unstructured
= lsmash_create_vc1_specific_info( (lsmash_vc1_specific_parameters_t
*)specific
->data
.structured
, &dst
->size
);
655 if( !dst
->data
.unstructured
)
658 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_AC_3
:
659 dst
->data
.unstructured
= lsmash_create_ac3_specific_info( (lsmash_ac3_specific_parameters_t
*)specific
->data
.structured
, &dst
->size
);
660 if( !dst
->data
.unstructured
)
663 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_EC_3
:
664 dst
->data
.unstructured
= lsmash_create_eac3_specific_info( (lsmash_eac3_specific_parameters_t
*)specific
->data
.structured
, &dst
->size
);
665 if( !dst
->data
.unstructured
)
668 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_DTS
:
669 dst
->data
.unstructured
= lsmash_create_dts_specific_info( (lsmash_dts_specific_parameters_t
*)specific
->data
.structured
, &dst
->size
);
670 if( !dst
->data
.unstructured
)
673 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_ALAC
:
674 dst
->data
.unstructured
= lsmash_create_alac_specific_info( (lsmash_alac_specific_parameters_t
*)specific
->data
.structured
, &dst
->size
);
675 if( !dst
->data
.unstructured
)
678 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_CODEC_GLOBAL_HEADER
:
680 lsmash_bs_t
*bs
= lsmash_bs_create();
683 lsmash_codec_global_header_t
*global
= specific
->data
.structured
;
684 lsmash_bs_put_be32( bs
, ISOM_BASEBOX_COMMON_SIZE
+ global
->header_size
);
685 lsmash_bs_put_be32( bs
, QT_BOX_TYPE_GLBL
.fourcc
);
686 lsmash_bs_put_bytes( bs
, global
->header_size
, global
->header_data
);
687 dst
->data
.unstructured
= lsmash_bs_export_data( bs
, &dst
->size
);
688 lsmash_bs_cleanup( bs
);
689 if( !dst
->data
.unstructured
|| dst
->size
!= (ISOM_BASEBOX_COMMON_SIZE
+ global
->header_size
) )
696 else if( format
== LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
)
698 /* unstructured -> structured */
699 extern int mp4sys_construct_decoder_config( lsmash_codec_specific_t
*, lsmash_codec_specific_t
* );
700 extern int h264_construct_specific_parameters( lsmash_codec_specific_t
*, lsmash_codec_specific_t
* );
701 extern int hevc_construct_specific_parameters( lsmash_codec_specific_t
*, lsmash_codec_specific_t
* );
702 extern int vc1_construct_specific_parameters( lsmash_codec_specific_t
*, lsmash_codec_specific_t
* );
703 extern int ac3_construct_specific_parameters( lsmash_codec_specific_t
*, lsmash_codec_specific_t
* );
704 extern int eac3_construct_specific_parameters( lsmash_codec_specific_t
*, lsmash_codec_specific_t
* );
705 extern int dts_construct_specific_parameters( lsmash_codec_specific_t
*, lsmash_codec_specific_t
* );
706 extern int alac_construct_specific_parameters( lsmash_codec_specific_t
*, lsmash_codec_specific_t
* );
709 lsmash_codec_specific_data_type data_type
;
710 int (*constructor
)( lsmash_codec_specific_t
*, lsmash_codec_specific_t
* );
711 } codec_specific_format_constructor_table
[] =
713 { LSMASH_CODEC_SPECIFIC_DATA_TYPE_MP4SYS_DECODER_CONFIG
, mp4sys_construct_decoder_config
},
714 { LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264
, h264_construct_specific_parameters
},
715 { LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_HEVC
, hevc_construct_specific_parameters
},
716 { LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_VC_1
, vc1_construct_specific_parameters
},
717 { LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_AC_3
, ac3_construct_specific_parameters
},
718 { LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_EC_3
, eac3_construct_specific_parameters
},
719 { LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_DTS
, dts_construct_specific_parameters
},
720 { LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_ALAC
, alac_construct_specific_parameters
},
721 { LSMASH_CODEC_SPECIFIC_DATA_TYPE_CODEC_GLOBAL_HEADER
, isom_construct_global_specific_header
},
722 { LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_CHANNEL_LAYOUT
, isom_construct_audio_channel_layout
},
723 { LSMASH_CODEC_SPECIFIC_DATA_TYPE_UNKNOWN
, NULL
}
725 int (*constructor
)( lsmash_codec_specific_t
*, lsmash_codec_specific_t
* ) = NULL
;
726 for( int i
= 0; codec_specific_format_constructor_table
[i
].constructor
; i
++ )
727 if( specific
->type
== codec_specific_format_constructor_table
[i
].data_type
)
729 constructor
= codec_specific_format_constructor_table
[i
].constructor
;
732 if( constructor
&& !constructor( dst
, specific
) )
736 lsmash_destroy_codec_specific_data( dst
);
740 static inline void isom_set_default_compressorname( char *compressorname
, lsmash_codec_type_t sample_type
)
742 static struct compressorname_table_tag
744 lsmash_codec_type_t type
;
746 } compressorname_table
[32] = { { LSMASH_CODEC_TYPE_INITIALIZER
, { '\0' } } };
747 if( compressorname_table
[0].name
[0] == '\0' )
750 #define ADD_COMPRESSORNAME_TABLE( type, name ) compressorname_table[i++] = (struct compressorname_table_tag){ type, name }
751 ADD_COMPRESSORNAME_TABLE( ISOM_CODEC_TYPE_AVC1_VIDEO
, "\012AVC Coding" );
752 ADD_COMPRESSORNAME_TABLE( ISOM_CODEC_TYPE_AVC2_VIDEO
, "\012AVC Coding" );
753 ADD_COMPRESSORNAME_TABLE( ISOM_CODEC_TYPE_AVC3_VIDEO
, "\012AVC Coding" );
754 ADD_COMPRESSORNAME_TABLE( ISOM_CODEC_TYPE_AVC4_VIDEO
, "\012AVC Coding" );
755 ADD_COMPRESSORNAME_TABLE( ISOM_CODEC_TYPE_AVCP_VIDEO
, "\016AVC Parameters" );
756 ADD_COMPRESSORNAME_TABLE( ISOM_CODEC_TYPE_HVC1_VIDEO
, "\013HEVC Coding" );
757 ADD_COMPRESSORNAME_TABLE( ISOM_CODEC_TYPE_HEV1_VIDEO
, "\013HEVC Coding" );
758 ADD_COMPRESSORNAME_TABLE( ISOM_CODEC_TYPE_SVC1_VIDEO
, "\012SVC Coding" );
759 ADD_COMPRESSORNAME_TABLE( ISOM_CODEC_TYPE_MVC1_VIDEO
, "\012MVC Coding" );
760 ADD_COMPRESSORNAME_TABLE( ISOM_CODEC_TYPE_MVC2_VIDEO
, "\012MVC Coding" );
761 ADD_COMPRESSORNAME_TABLE( QT_CODEC_TYPE_APCH_VIDEO
, "\023Apple ProRes 422 (HQ)" );
762 ADD_COMPRESSORNAME_TABLE( QT_CODEC_TYPE_APCN_VIDEO
, "\023Apple ProRes 422 (SD)" );
763 ADD_COMPRESSORNAME_TABLE( QT_CODEC_TYPE_APCS_VIDEO
, "\023Apple ProRes 422 (LT)" );
764 ADD_COMPRESSORNAME_TABLE( QT_CODEC_TYPE_APCO_VIDEO
, "\026Apple ProRes 422 (Proxy)" );
765 ADD_COMPRESSORNAME_TABLE( QT_CODEC_TYPE_AP4H_VIDEO
, "\019Apple ProRes 4444" );
766 ADD_COMPRESSORNAME_TABLE( QT_CODEC_TYPE_DVPP_VIDEO
, "\014DVCPRO - PAL" );
767 ADD_COMPRESSORNAME_TABLE( QT_CODEC_TYPE_DV5N_VIDEO
, "\017DVCPRO50 - NTSC" );
768 ADD_COMPRESSORNAME_TABLE( QT_CODEC_TYPE_DV5P_VIDEO
, "\016DVCPRO50 - PAL" );
769 ADD_COMPRESSORNAME_TABLE( QT_CODEC_TYPE_DVH2_VIDEO
, "\019DVCPRO HD 1080p25" );
770 ADD_COMPRESSORNAME_TABLE( QT_CODEC_TYPE_DVH3_VIDEO
, "\019DVCPRO HD 1080p30" );
771 ADD_COMPRESSORNAME_TABLE( QT_CODEC_TYPE_DVH5_VIDEO
, "\019DVCPRO HD 1080i50" );
772 ADD_COMPRESSORNAME_TABLE( QT_CODEC_TYPE_DVH6_VIDEO
, "\019DVCPRO HD 1080i60" );
773 ADD_COMPRESSORNAME_TABLE( QT_CODEC_TYPE_DVHP_VIDEO
, "\018DVCPRO HD 720p60" );
774 ADD_COMPRESSORNAME_TABLE( QT_CODEC_TYPE_DVHQ_VIDEO
, "\018DVCPRO HD 720p50" );
775 ADD_COMPRESSORNAME_TABLE( QT_CODEC_TYPE_ULRA_VIDEO
, "\017Ut Video (ULRA)" );
776 ADD_COMPRESSORNAME_TABLE( QT_CODEC_TYPE_ULRG_VIDEO
, "\017Ut Video (ULRG)" );
777 ADD_COMPRESSORNAME_TABLE( QT_CODEC_TYPE_ULY0_VIDEO
, "\017Ut Video (ULY0)" );
778 ADD_COMPRESSORNAME_TABLE( QT_CODEC_TYPE_ULY2_VIDEO
, "\017Ut Video (ULY2)" );
779 ADD_COMPRESSORNAME_TABLE( QT_CODEC_TYPE_ULH0_VIDEO
, "\017Ut Video (ULH0)" );
780 ADD_COMPRESSORNAME_TABLE( QT_CODEC_TYPE_ULH2_VIDEO
, "\017Ut Video (ULH2)" );
781 ADD_COMPRESSORNAME_TABLE( LSMASH_CODEC_TYPE_UNSPECIFIED
, { '\0' } );
782 #undef ADD_COMPRESSORNAME_TABLE
784 for( int i
= 0; compressorname_table
[i
].name
[0] != '\0'; i
++ )
785 if( lsmash_check_codec_type_identical( sample_type
, compressorname_table
[i
].type
) )
787 strcpy( compressorname
, compressorname_table
[i
].name
);
792 lsmash_codec_specific_t
*isom_get_codec_specific( lsmash_codec_specific_list_t
*opaque
, lsmash_codec_specific_data_type type
)
794 for( lsmash_entry_t
*entry
= opaque
->list
.head
; entry
; entry
= entry
->next
)
796 lsmash_codec_specific_t
*specific
= (lsmash_codec_specific_t
*)entry
->data
;
797 if( !specific
|| specific
->type
!= type
)
804 static int isom_check_valid_summary( lsmash_summary_t
*summary
)
809 temp_box
.type
= summary
->sample_type
;
810 temp_box
.manager
= summary
->summary_type
== LSMASH_SUMMARY_TYPE_AUDIO
? LSMASH_AUDIO_DESCRIPTION
: 0;
811 if( isom_is_lpcm_audio( &temp_box
) )
813 if( isom_get_codec_specific( summary
->opaque
, LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_FORMAT_SPECIFIC_FLAGS
) )
817 if( isom_is_uncompressed_ycbcr( summary
->sample_type
) )
819 if( isom_get_codec_specific( summary
->opaque
, LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_FIELD_INFO
) )
821 if( !lsmash_check_codec_type_identical( summary
->sample_type
, QT_CODEC_TYPE_V216_VIDEO
) )
827 lsmash_codec_type_t sample_type
= summary
->sample_type
;
828 lsmash_codec_specific_data_type required_data_type
= LSMASH_CODEC_SPECIFIC_DATA_TYPE_UNSPECIFIED
;
829 if( lsmash_check_codec_type_identical( sample_type
, ISOM_CODEC_TYPE_AVC1_VIDEO
)
830 || lsmash_check_codec_type_identical( sample_type
, ISOM_CODEC_TYPE_AVC2_VIDEO
)
831 || lsmash_check_codec_type_identical( sample_type
, ISOM_CODEC_TYPE_AVC3_VIDEO
)
832 || lsmash_check_codec_type_identical( sample_type
, ISOM_CODEC_TYPE_AVC4_VIDEO
) )
833 required_data_type
= LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264
;
834 else if( lsmash_check_codec_type_identical( sample_type
, ISOM_CODEC_TYPE_HVC1_VIDEO
)
835 || lsmash_check_codec_type_identical( sample_type
, ISOM_CODEC_TYPE_HEV1_VIDEO
) )
836 required_data_type
= LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_HEVC
;
837 else if( lsmash_check_codec_type_identical( sample_type
, ISOM_CODEC_TYPE_VC_1_VIDEO
) )
838 required_data_type
= LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_VC_1
;
839 else if( lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_ULRA_VIDEO
)
840 || lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_ULRG_VIDEO
)
841 || lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_ULY0_VIDEO
)
842 || lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_ULY2_VIDEO
)
843 || lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_ULH0_VIDEO
)
844 || lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_ULH2_VIDEO
) )
845 required_data_type
= LSMASH_CODEC_SPECIFIC_DATA_TYPE_CODEC_GLOBAL_HEADER
;
846 else if( lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_V216_VIDEO
) )
847 required_data_type
= LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_SIGNIFICANT_BITS
;
848 else if( lsmash_check_codec_type_identical( sample_type
, ISOM_CODEC_TYPE_MP4V_VIDEO
)
849 || lsmash_check_codec_type_identical( sample_type
, ISOM_CODEC_TYPE_MP4A_AUDIO
)
850 || lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_MP4A_AUDIO
) )
851 required_data_type
= LSMASH_CODEC_SPECIFIC_DATA_TYPE_MP4SYS_DECODER_CONFIG
;
852 else if( lsmash_check_codec_type_identical( sample_type
, ISOM_CODEC_TYPE_AC_3_AUDIO
) )
853 required_data_type
= LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_AC_3
;
854 else if( lsmash_check_codec_type_identical( sample_type
, ISOM_CODEC_TYPE_EC_3_AUDIO
) )
855 required_data_type
= LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_EC_3
;
856 else if( lsmash_check_codec_type_identical( sample_type
, ISOM_CODEC_TYPE_DTSC_AUDIO
)
857 || lsmash_check_codec_type_identical( sample_type
, ISOM_CODEC_TYPE_DTSE_AUDIO
)
858 || lsmash_check_codec_type_identical( sample_type
, ISOM_CODEC_TYPE_DTSH_AUDIO
)
859 || lsmash_check_codec_type_identical( sample_type
, ISOM_CODEC_TYPE_DTSL_AUDIO
) )
860 required_data_type
= LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_DTS
;
861 else if( lsmash_check_codec_type_identical( sample_type
, ISOM_CODEC_TYPE_ALAC_AUDIO
)
862 || lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_ALAC_AUDIO
) )
863 required_data_type
= LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_ALAC
;
864 if( required_data_type
== LSMASH_CODEC_SPECIFIC_DATA_TYPE_UNSPECIFIED
)
866 return isom_get_codec_specific( summary
->opaque
, required_data_type
) ? 0 : -1;
869 static lsmash_box_type_t
isom_guess_video_codec_specific_box_type( lsmash_codec_type_t active_codec_type
, lsmash_compact_box_type_t fourcc
)
871 lsmash_box_type_t box_type
= LSMASH_BOX_TYPE_INITIALIZER
;
872 box_type
.fourcc
= fourcc
;
873 #define GUESS_VIDEO_CODEC_SPECIFIC_BOX_TYPE( codec_type, predefined_box_type ) \
874 else if( (codec_type.user.fourcc == 0 \
875 || lsmash_check_codec_type_identical( active_codec_type, codec_type )) \
876 && box_type.fourcc == predefined_box_type.fourcc ) \
877 box_type = predefined_box_type
879 GUESS_VIDEO_CODEC_SPECIFIC_BOX_TYPE( ISOM_CODEC_TYPE_AVC1_VIDEO
, ISOM_BOX_TYPE_AVCC
);
880 GUESS_VIDEO_CODEC_SPECIFIC_BOX_TYPE( ISOM_CODEC_TYPE_AVC2_VIDEO
, ISOM_BOX_TYPE_AVCC
);
881 GUESS_VIDEO_CODEC_SPECIFIC_BOX_TYPE( ISOM_CODEC_TYPE_AVC3_VIDEO
, ISOM_BOX_TYPE_AVCC
);
882 GUESS_VIDEO_CODEC_SPECIFIC_BOX_TYPE( ISOM_CODEC_TYPE_AVC4_VIDEO
, ISOM_BOX_TYPE_AVCC
);
883 GUESS_VIDEO_CODEC_SPECIFIC_BOX_TYPE( ISOM_CODEC_TYPE_AVCP_VIDEO
, ISOM_BOX_TYPE_AVCC
);
884 GUESS_VIDEO_CODEC_SPECIFIC_BOX_TYPE( ISOM_CODEC_TYPE_HVC1_VIDEO
, ISOM_BOX_TYPE_HVCC
);
885 GUESS_VIDEO_CODEC_SPECIFIC_BOX_TYPE( ISOM_CODEC_TYPE_HEV1_VIDEO
, ISOM_BOX_TYPE_HVCC
);
886 GUESS_VIDEO_CODEC_SPECIFIC_BOX_TYPE( ISOM_CODEC_TYPE_VC_1_VIDEO
, ISOM_BOX_TYPE_DVC1
);
887 GUESS_VIDEO_CODEC_SPECIFIC_BOX_TYPE( ISOM_CODEC_TYPE_MP4V_VIDEO
, ISOM_BOX_TYPE_ESDS
);
888 GUESS_VIDEO_CODEC_SPECIFIC_BOX_TYPE( LSMASH_CODEC_TYPE_UNSPECIFIED
, ISOM_BOX_TYPE_BTRT
);
889 GUESS_VIDEO_CODEC_SPECIFIC_BOX_TYPE( LSMASH_CODEC_TYPE_UNSPECIFIED
, QT_BOX_TYPE_FIEL
);
890 GUESS_VIDEO_CODEC_SPECIFIC_BOX_TYPE( LSMASH_CODEC_TYPE_UNSPECIFIED
, QT_BOX_TYPE_CSPC
);
891 GUESS_VIDEO_CODEC_SPECIFIC_BOX_TYPE( LSMASH_CODEC_TYPE_UNSPECIFIED
, QT_BOX_TYPE_SGBT
);
892 GUESS_VIDEO_CODEC_SPECIFIC_BOX_TYPE( LSMASH_CODEC_TYPE_UNSPECIFIED
, QT_BOX_TYPE_GAMA
);
893 GUESS_VIDEO_CODEC_SPECIFIC_BOX_TYPE( LSMASH_CODEC_TYPE_UNSPECIFIED
, QT_BOX_TYPE_GLBL
);
894 #undef GUESS_VIDEO_CODEC_SPECIFIC_BOX_TYPE
898 int isom_setup_visual_description( isom_stsd_t
*stsd
, lsmash_codec_type_t sample_type
, lsmash_video_summary_t
*summary
)
900 if( !summary
|| !stsd
|| !stsd
->parent
|| !stsd
->parent
->parent
901 || !stsd
->parent
->parent
->parent
|| !stsd
->parent
->parent
->parent
->parent
)
903 if( isom_check_valid_summary( (lsmash_summary_t
*)summary
) )
905 isom_visual_entry_t
*visual
= isom_add_visual_description( stsd
, sample_type
);
908 visual
->data_reference_index
= summary
->data_ref_index
;
910 visual
->revision_level
= 0;
912 visual
->temporalQuality
= 0;
913 visual
->spatialQuality
= 0;
914 visual
->width
= (uint16_t)summary
->width
;
915 visual
->height
= (uint16_t)summary
->height
;
916 visual
->horizresolution
= 0x00480000;
917 visual
->vertresolution
= 0x00480000;
918 visual
->dataSize
= 0;
919 visual
->frame_count
= 1;
920 visual
->depth
= isom_is_qt_video( summary
->sample_type
) || isom_is_nalff( summary
->sample_type
)
921 ? summary
->depth
: 0x0018;
922 visual
->color_table_ID
= -1;
923 if( summary
->compressorname
[0] == '\0' )
924 isom_set_default_compressorname( visual
->compressorname
, sample_type
);
927 memcpy( visual
->compressorname
, summary
->compressorname
, 32 );
928 visual
->compressorname
[32] = '\0';
930 for( lsmash_entry_t
*entry
= summary
->opaque
->list
.head
; entry
; entry
= entry
->next
)
932 lsmash_codec_specific_t
*specific
= (lsmash_codec_specific_t
*)entry
->data
;
935 if( specific
->type
== LSMASH_CODEC_SPECIFIC_DATA_TYPE_UNKNOWN
936 && specific
->format
== LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
)
937 continue; /* LSMASH_CODEC_SPECIFIC_DATA_TYPE_UNKNOWN + LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED is not supported. */
938 switch( specific
->type
)
940 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_COMMON
:
942 if( specific
->format
== LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
)
944 lsmash_qt_video_common_t
*data
= (lsmash_qt_video_common_t
*)specific
->data
.structured
;
945 visual
->revision_level
= data
->revision_level
;
946 visual
->vendor
= data
->vendor
;
947 visual
->temporalQuality
= data
->temporalQuality
;
948 visual
->spatialQuality
= data
->spatialQuality
;
949 visual
->horizresolution
= data
->horizontal_resolution
;
950 visual
->vertresolution
= data
->vertical_resolution
;
951 visual
->dataSize
= data
->dataSize
;
952 visual
->frame_count
= data
->frame_count
;
953 visual
->color_table_ID
= data
->color_table_ID
;
954 if( data
->color_table_ID
== 0 )
956 lsmash_qt_color_table_t
*src_ct
= &data
->color_table
;
957 uint16_t element_count
= LSMASH_MIN( src_ct
->size
+ 1, 256 );
958 isom_qt_color_array_t
*dst_array
= lsmash_malloc_zero( element_count
* sizeof(isom_qt_color_array_t
) );
961 isom_qt_color_table_t
*dst_ct
= &visual
->color_table
;
962 dst_ct
->array
= dst_array
;
963 dst_ct
->seed
= src_ct
->seed
;
964 dst_ct
->flags
= src_ct
->flags
;
965 dst_ct
->size
= src_ct
->size
;
966 for( uint16_t i
= 0; i
< element_count
; i
++ )
968 dst_array
[i
].value
= src_ct
->array
[i
].unused
;
969 dst_array
[i
].r
= src_ct
->array
[i
].r
;
970 dst_array
[i
].g
= src_ct
->array
[i
].g
;
971 dst_array
[i
].b
= src_ct
->array
[i
].b
;
976 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_SAMPLE_SCALE
:
978 lsmash_codec_specific_t
*cs
= lsmash_convert_codec_specific_format( specific
, LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
981 lsmash_isom_sample_scale_t
*data
= (lsmash_isom_sample_scale_t
*)cs
->data
.structured
;
982 isom_stsl_t
*stsl
= isom_add_stsl( visual
);
985 lsmash_destroy_codec_specific_data( cs
);
988 stsl
->constraint_flag
= data
->constraint_flag
;
989 stsl
->scale_method
= data
->scale_method
;
990 stsl
->display_center_x
= data
->display_center_x
;
991 stsl
->display_center_y
= data
->display_center_y
;
992 lsmash_destroy_codec_specific_data( cs
);
995 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264_BITRATE
:
997 lsmash_codec_specific_t
*cs
= lsmash_convert_codec_specific_format( specific
, LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
1000 lsmash_h264_bitrate_t
*data
= (lsmash_h264_bitrate_t
*)cs
->data
.structured
;
1001 isom_btrt_t
*btrt
= isom_add_btrt( visual
);
1004 lsmash_destroy_codec_specific_data( cs
);
1007 btrt
->bufferSizeDB
= data
->bufferSizeDB
;
1008 btrt
->maxBitrate
= data
->maxBitrate
;
1009 btrt
->avgBitrate
= data
->avgBitrate
;
1010 lsmash_destroy_codec_specific_data( cs
);
1013 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_FIELD_INFO
:
1015 lsmash_codec_specific_t
*cs
= lsmash_convert_codec_specific_format( specific
, LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
1018 lsmash_qt_field_info_t
*data
= (lsmash_qt_field_info_t
*)cs
->data
.structured
;
1019 isom_fiel_t
*fiel
= isom_add_fiel( visual
);
1022 lsmash_destroy_codec_specific_data( cs
);
1025 fiel
->fields
= data
->fields
;
1026 fiel
->detail
= data
->detail
;
1027 lsmash_destroy_codec_specific_data( cs
);
1030 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_PIXEL_FORMAT
:
1032 lsmash_codec_specific_t
*cs
= lsmash_convert_codec_specific_format( specific
, LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
1035 lsmash_qt_pixel_format_t
*data
= (lsmash_qt_pixel_format_t
*)cs
->data
.structured
;
1036 isom_cspc_t
*cspc
= isom_add_cspc( visual
);
1039 lsmash_destroy_codec_specific_data( cs
);
1042 cspc
->pixel_format
= data
->pixel_format
;
1043 lsmash_destroy_codec_specific_data( cs
);
1046 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_SIGNIFICANT_BITS
:
1048 lsmash_codec_specific_t
*cs
= lsmash_convert_codec_specific_format( specific
, LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
1051 lsmash_qt_significant_bits_t
*data
= (lsmash_qt_significant_bits_t
*)cs
->data
.structured
;
1052 isom_sgbt_t
*sgbt
= isom_add_sgbt( visual
);
1055 lsmash_destroy_codec_specific_data( cs
);
1058 sgbt
->significantBits
= data
->significantBits
;
1059 lsmash_destroy_codec_specific_data( cs
);
1062 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_GAMMA_LEVEL
:
1064 lsmash_codec_specific_t
*cs
= lsmash_convert_codec_specific_format( specific
, LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
1067 lsmash_qt_gamma_t
*data
= (lsmash_qt_gamma_t
*)cs
->data
.structured
;
1068 isom_gama_t
*gama
= isom_add_gama( visual
);
1071 lsmash_destroy_codec_specific_data( cs
);
1074 gama
->level
= data
->level
;
1075 lsmash_destroy_codec_specific_data( cs
);
1078 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_CODEC_GLOBAL_HEADER
:
1080 lsmash_codec_specific_t
*cs
= lsmash_convert_codec_specific_format( specific
, LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
1083 lsmash_codec_global_header_t
*data
= (lsmash_codec_global_header_t
*)cs
->data
.structured
;
1084 isom_glbl_t
*glbl
= isom_add_glbl( visual
);
1087 lsmash_destroy_codec_specific_data( cs
);
1090 glbl
->header_size
= data
->header_size
;
1091 glbl
->header_data
= lsmash_memdup( data
->header_data
, data
->header_size
);
1092 lsmash_destroy_codec_specific_data( cs
);
1093 if( !glbl
->header_data
)
1095 isom_remove_box_by_itself( glbl
);
1102 lsmash_codec_specific_t
*cs
= lsmash_convert_codec_specific_format( specific
, LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
);
1105 if( cs
->size
< ISOM_BASEBOX_COMMON_SIZE
)
1107 lsmash_destroy_codec_specific_data( cs
);
1110 uint8_t *data
= cs
->data
.unstructured
;
1111 lsmash_compact_box_type_t fourcc
= LSMASH_4CC( data
[4], data
[5], data
[6], data
[7] );
1112 lsmash_box_type_t box_type
= isom_guess_video_codec_specific_box_type( visual
->type
, fourcc
);
1113 /* Append the extension. */
1114 int ret
= isom_add_extension_binary( visual
, box_type
, LSMASH_BOX_PRECEDENCE_HM
, cs
->data
.unstructured
, cs
->size
);
1115 cs
->data
.unstructured
= NULL
; /* Avoid freeing the binary data of the extension. */
1116 lsmash_destroy_codec_specific_data( cs
);
1123 isom_trak_t
*trak
= (isom_trak_t
*)visual
->parent
->parent
->parent
->parent
->parent
;
1124 int qt_compatible
= trak
->file
->qt_compatible
;
1125 isom_tapt_t
*tapt
= trak
->tapt
;
1126 isom_stsl_t
*stsl
= (isom_stsl_t
*)isom_get_extension_box_format( &visual
->extensions
, ISOM_BOX_TYPE_STSL
);
1127 int set_aperture_modes
= qt_compatible
/* Track Aperture Modes is only available under QuickTime file format. */
1128 && (!stsl
|| stsl
->scale_method
== 0) /* Sample scaling method might conflict with this feature. */
1129 && tapt
&& tapt
->clef
&& tapt
->prof
&& tapt
->enof
/* Check if required boxes exist. */
1130 && ((isom_stsd_t
*)visual
->parent
)->list
.entry_count
== 1; /* Multiple sample description might conflict with this, so in that case, disable this feature. */
1131 if( !set_aperture_modes
)
1132 isom_remove_box_by_itself( trak
->tapt
);
1133 int uncompressed_ycbcr
= qt_compatible
&& isom_is_uncompressed_ycbcr( visual
->type
);
1134 /* Set up Clean Aperture. */
1135 if( set_aperture_modes
|| uncompressed_ycbcr
1136 || (summary
->clap
.width
.d
&& summary
->clap
.height
.d
&& summary
->clap
.horizontal_offset
.d
&& summary
->clap
.vertical_offset
.d
) )
1138 isom_clap_t
*clap
= isom_add_clap( visual
);
1141 if( summary
->clap
.width
.d
&& summary
->clap
.height
.d
&& summary
->clap
.horizontal_offset
.d
&& summary
->clap
.vertical_offset
.d
)
1143 clap
->cleanApertureWidthN
= summary
->clap
.width
.n
;
1144 clap
->cleanApertureWidthD
= summary
->clap
.width
.d
;
1145 clap
->cleanApertureHeightN
= summary
->clap
.height
.n
;
1146 clap
->cleanApertureHeightD
= summary
->clap
.height
.d
;
1147 clap
->horizOffN
= summary
->clap
.horizontal_offset
.n
;
1148 clap
->horizOffD
= summary
->clap
.horizontal_offset
.d
;
1149 clap
->vertOffN
= summary
->clap
.vertical_offset
.n
;
1150 clap
->vertOffD
= summary
->clap
.vertical_offset
.d
;
1154 clap
->cleanApertureWidthN
= summary
->width
;
1155 clap
->cleanApertureWidthD
= 1;
1156 clap
->cleanApertureHeightN
= summary
->height
;
1157 clap
->cleanApertureHeightD
= 1;
1158 clap
->horizOffN
= 0;
1159 clap
->horizOffD
= 1;
1164 /* Set up Pixel Aspect Ratio. */
1165 if( set_aperture_modes
|| (summary
->par_h
&& summary
->par_v
) )
1167 isom_pasp_t
*pasp
= isom_add_pasp( visual
);
1170 pasp
->hSpacing
= LSMASH_MAX( summary
->par_h
, 1 );
1171 pasp
->vSpacing
= LSMASH_MAX( summary
->par_v
, 1 );
1173 /* Set up Color Parameter. */
1174 if( uncompressed_ycbcr
1175 || summary
->color
.primaries_index
1176 || summary
->color
.transfer_index
1177 || summary
->color
.matrix_index
1178 || (trak
->file
->isom_compatible
&& summary
->color
.full_range
) )
1180 isom_colr_t
*colr
= isom_add_colr( visual
);
1183 /* Set 'nclc' to parameter type, we don't support 'prof'. */
1184 uint16_t primaries
= summary
->color
.primaries_index
;
1185 uint16_t transfer
= summary
->color
.transfer_index
;
1186 uint16_t matrix
= summary
->color
.matrix_index
;
1187 if( qt_compatible
&& !trak
->file
->isom_compatible
)
1189 colr
->manager
|= LSMASH_QTFF_BASE
;
1190 colr
->type
= QT_BOX_TYPE_COLR
;
1191 colr
->color_parameter_type
= QT_COLOR_PARAMETER_TYPE_NCLC
;
1192 colr
->primaries_index
= (primaries
== 1 || primaries
== 5 || primaries
== 6)
1193 ? primaries
: QT_PRIMARIES_INDEX_UNSPECIFIED
;
1194 colr
->transfer_function_index
= (transfer
== 1 || transfer
== 7)
1195 ? transfer
: QT_TRANSFER_INDEX_UNSPECIFIED
;
1196 colr
->matrix_index
= (matrix
== 1 || matrix
== 6 || matrix
== 7)
1197 ? matrix
: QT_MATRIX_INDEX_UNSPECIFIED
;
1201 colr
->type
= ISOM_BOX_TYPE_COLR
;
1202 colr
->color_parameter_type
= ISOM_COLOR_PARAMETER_TYPE_NCLX
;
1203 colr
->primaries_index
= (primaries
== 1 || (primaries
>= 4 && primaries
<= 7))
1204 ? primaries
: ISOM_PRIMARIES_INDEX_UNSPECIFIED
;
1205 colr
->transfer_function_index
= (transfer
== 1 || (transfer
>= 4 && transfer
<= 8) || (transfer
>= 11 && transfer
<= 13))
1206 ? transfer
: ISOM_TRANSFER_INDEX_UNSPECIFIED
;
1207 colr
->matrix_index
= (matrix
== 1 || (matrix
>= 4 && matrix
<= 8))
1208 ? matrix
: ISOM_MATRIX_INDEX_UNSPECIFIED
;
1209 colr
->full_range_flag
= summary
->color
.full_range
;
1212 /* Set up Track Apeture Modes. */
1213 if( set_aperture_modes
)
1215 uint32_t width
= visual
->width
<< 16;
1216 uint32_t height
= visual
->height
<< 16;
1217 isom_clap_t
*clap
= (isom_clap_t
*)isom_get_extension_box_format( &visual
->extensions
, ISOM_BOX_TYPE_CLAP
);
1218 isom_pasp_t
*pasp
= (isom_pasp_t
*)isom_get_extension_box_format( &visual
->extensions
, ISOM_BOX_TYPE_PASP
);
1219 double clap_width
= ((double)clap
->cleanApertureWidthN
/ clap
->cleanApertureWidthD
) * (1<<16);
1220 double clap_height
= ((double)clap
->cleanApertureHeightN
/ clap
->cleanApertureHeightD
) * (1<<16);
1221 double par
= (double)pasp
->hSpacing
/ pasp
->vSpacing
;
1224 tapt
->clef
->width
= clap_width
* par
;
1225 tapt
->clef
->height
= clap_height
;
1226 tapt
->prof
->width
= width
* par
;
1227 tapt
->prof
->height
= height
;
1231 tapt
->clef
->width
= clap_width
;
1232 tapt
->clef
->height
= clap_height
/ par
;
1233 tapt
->prof
->width
= width
;
1234 tapt
->prof
->height
= height
/ par
;
1236 tapt
->enof
->width
= width
;
1237 tapt
->enof
->height
= height
;
1241 isom_remove_box_by_itself( visual
);
1245 static int isom_append_audio_es_descriptor_extension( isom_box_t
*box
, lsmash_audio_summary_t
*summary
)
1247 uint32_t esds_size
= 0;
1248 uint8_t *esds_data
= NULL
;
1249 lsmash_codec_specific_t
*specific
= isom_get_codec_specific( summary
->opaque
, LSMASH_CODEC_SPECIFIC_DATA_TYPE_MP4SYS_DECODER_CONFIG
);
1252 if( specific
->format
== LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
)
1254 esds_size
= specific
->size
;
1255 esds_data
= lsmash_memdup( specific
->data
.unstructured
, specific
->size
);
1261 esds_data
= lsmash_create_mp4sys_decoder_config( (lsmash_mp4sys_decoder_parameters_t
*)specific
->data
.structured
, &esds_size
);
1265 isom_esds_t
*esds
= isom_add_esds( box
);
1268 lsmash_free( esds_data
);
1271 lsmash_bs_t bs
= { 0 };
1272 bs
.buffer
.data
= esds_data
+ ISOM_FULLBOX_COMMON_SIZE
;
1273 bs
.buffer
.alloc
= esds_size
- ISOM_FULLBOX_COMMON_SIZE
;
1274 bs
.buffer
.store
= bs
.buffer
.alloc
;
1275 esds
->ES
= mp4sys_get_descriptor( &bs
, NULL
);
1276 lsmash_free( esds_data
);
1279 isom_remove_box_by_itself( esds
);
1285 static int isom_append_channel_layout_extension( lsmash_codec_specific_t
*specific
, void *parent
, uint32_t channels
)
1288 if( isom_get_extension_box( &((isom_box_t
*)parent
)->extensions
, QT_BOX_TYPE_CHAN
) )
1289 return 0; /* Audio Channel Layout Box is already present. */
1290 lsmash_codec_specific_t
*cs
= lsmash_convert_codec_specific_format( specific
, LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
1293 lsmash_qt_audio_channel_layout_t
*data
= (lsmash_qt_audio_channel_layout_t
*)cs
->data
.structured
;
1294 lsmash_channel_layout_tag channelLayoutTag
= data
->channelLayoutTag
;
1295 lsmash_channel_bitmap channelBitmap
= data
->channelBitmap
;
1296 if( channelLayoutTag
== QT_CHANNEL_LAYOUT_USE_CHANNEL_DESCRIPTIONS
/* We don't support the feature of Channel Descriptions. */
1297 || (channelLayoutTag
== QT_CHANNEL_LAYOUT_USE_CHANNEL_BITMAP
&& (!channelBitmap
|| channelBitmap
> QT_CHANNEL_BIT_FULL
)) )
1299 channelLayoutTag
= QT_CHANNEL_LAYOUT_UNKNOWN
| channels
;
1302 lsmash_destroy_codec_specific_data( cs
);
1303 /* Don't create Audio Channel Layout Box if the channel layout is unknown. */
1304 if( (channelLayoutTag
^ QT_CHANNEL_LAYOUT_UNKNOWN
) >> 16 )
1306 isom_chan_t
*chan
= isom_add_chan( parent
);
1309 chan
->channelLayoutTag
= channelLayoutTag
;
1310 chan
->channelBitmap
= channelBitmap
;
1311 chan
->numberChannelDescriptions
= 0;
1312 chan
->channelDescriptions
= NULL
;
1317 static int isom_set_qtff_mp4a_description( isom_audio_entry_t
*audio
, lsmash_audio_summary_t
*summary
)
1319 isom_wave_t
*wave
= isom_add_wave( audio
);
1321 if( !(frma
= isom_add_frma( wave
))
1322 || !isom_add_mp4a( wave
)
1323 || !isom_add_terminator( wave
) )
1325 lsmash_remove_entry_tail( &audio
->extensions
, wave
->destruct
);
1328 frma
->data_format
= audio
->type
.fourcc
;
1329 /* Add ES Descriptor Box. */
1330 if( isom_append_audio_es_descriptor_extension( (isom_box_t
*)wave
, summary
) )
1333 audio
->type
= QT_CODEC_TYPE_MP4A_AUDIO
;
1334 audio
->version
= (summary
->channels
> 2 || summary
->frequency
> UINT16_MAX
) ? 2 : 1;
1335 audio
->channelcount
= audio
->version
== 2 ? 3 : LSMASH_MIN( summary
->channels
, 2 );
1336 audio
->samplesize
= 16;
1337 audio
->compression_ID
= QT_AUDIO_COMPRESSION_ID_VARIABLE_COMPRESSION
;
1338 audio
->packet_size
= 0;
1339 if( audio
->version
== 1 )
1341 audio
->samplerate
= summary
->frequency
<< 16;
1342 audio
->samplesPerPacket
= summary
->samples_in_frame
;
1343 audio
->bytesPerPacket
= 1; /* Apparently, this field is set to 1. */
1344 audio
->bytesPerFrame
= audio
->bytesPerPacket
* summary
->channels
;
1345 audio
->bytesPerSample
= 2;
1347 else /* audio->version == 2 */
1349 audio
->samplerate
= 0x00010000;
1350 audio
->sizeOfStructOnly
= 72;
1351 audio
->audioSampleRate
= (union {double d
; uint64_t i
;}){summary
->frequency
}.i
;
1352 audio
->numAudioChannels
= summary
->channels
;
1353 audio
->always7F000000
= 0x7F000000;
1354 audio
->constBitsPerChannel
= 0; /* compressed audio */
1355 audio
->formatSpecificFlags
= 0;
1356 audio
->constBytesPerAudioPacket
= 0; /* variable */
1357 audio
->constLPCMFramesPerAudioPacket
= summary
->samples_in_frame
;
1362 static int isom_set_isom_mp4a_description( isom_audio_entry_t
*audio
, lsmash_audio_summary_t
*summary
)
1364 if( summary
->summary_type
!= LSMASH_SUMMARY_TYPE_AUDIO
)
1366 /* Check objectTypeIndication. */
1367 lsmash_mp4sys_object_type_indication objectTypeIndication
= lsmash_mp4sys_get_object_type_indication( (lsmash_summary_t
*)summary
);
1368 switch( objectTypeIndication
)
1370 case MP4SYS_OBJECT_TYPE_Audio_ISO_14496_3
:
1371 case MP4SYS_OBJECT_TYPE_Audio_ISO_13818_7_Main_Profile
:
1372 case MP4SYS_OBJECT_TYPE_Audio_ISO_13818_7_LC_Profile
:
1373 case MP4SYS_OBJECT_TYPE_Audio_ISO_13818_7_SSR_Profile
:
1374 case MP4SYS_OBJECT_TYPE_Audio_ISO_13818_3
: /* Legacy Interface */
1375 case MP4SYS_OBJECT_TYPE_Audio_ISO_11172_3
: /* Legacy Interface */
1380 /* Add ES Descriptor Box. */
1381 if( isom_append_audio_es_descriptor_extension( (isom_box_t
*)audio
, summary
) )
1383 /* In pure mp4 file, these "template" fields shall be default values according to the spec.
1384 But not pure - hybrid with other spec - mp4 file can take other values.
1385 Which is to say, these template values shall be ignored in terms of mp4, except some object_type_indications.
1386 see 14496-14, "Template fields used". */
1387 audio
->type
= ISOM_CODEC_TYPE_MP4A_AUDIO
;
1389 audio
->revision_level
= 0;
1391 audio
->channelcount
= 2;
1392 audio
->samplesize
= 16;
1393 audio
->compression_ID
= 0;
1394 audio
->packet_size
= 0;
1395 /* WARNING: This field cannot retain frequency above 65535Hz.
1396 This is not "FIXME", I just honestly implemented what the spec says.
1397 BTW, who ever expects sampling frequency takes fixed-point decimal??? */
1398 audio
->samplerate
= summary
->frequency
<= UINT16_MAX
? summary
->frequency
<< 16 : 0;
1402 static int isom_set_qtff_lpcm_description( isom_audio_entry_t
*audio
, lsmash_audio_summary_t
*summary
)
1404 lsmash_qt_audio_format_specific_flags_t
*lpcm
= NULL
;
1405 for( lsmash_entry_t
*entry
= summary
->opaque
->list
.head
; entry
; entry
= entry
->next
)
1407 lsmash_codec_specific_t
*specific
= (lsmash_codec_specific_t
*)entry
->data
;
1410 if( specific
->type
== LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_FORMAT_SPECIFIC_FLAGS
1411 && specific
->format
== LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
)
1413 lpcm
= (lsmash_qt_audio_format_specific_flags_t
*)specific
->data
.structured
;
1419 audio
->manager
|= LSMASH_QTFF_BASE
;
1420 lsmash_codec_type_t sample_type
= audio
->type
;
1421 /* Convert the sample type into 'lpcm' if the description doesn't match the format or version = 2 fields are needed. */
1422 if( (lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_RAW_AUDIO
)
1423 && (summary
->sample_size
!= 8 || (lpcm
->format_flags
& QT_LPCM_FORMAT_FLAG_FLOAT
)))
1424 || (lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_FL32_AUDIO
)
1425 && (summary
->sample_size
!= 32 || !(lpcm
->format_flags
& QT_LPCM_FORMAT_FLAG_FLOAT
)))
1426 || (lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_FL64_AUDIO
)
1427 && (summary
->sample_size
!= 64 || !(lpcm
->format_flags
& QT_LPCM_FORMAT_FLAG_FLOAT
)))
1428 || (lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_IN24_AUDIO
)
1429 && (summary
->sample_size
!= 24 || (lpcm
->format_flags
& QT_LPCM_FORMAT_FLAG_FLOAT
)))
1430 || (lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_IN32_AUDIO
)
1431 && (summary
->sample_size
!= 32 || (lpcm
->format_flags
& QT_LPCM_FORMAT_FLAG_FLOAT
)))
1432 || (lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_23NI_AUDIO
)
1433 && (summary
->sample_size
!= 32 || (lpcm
->format_flags
& QT_LPCM_FORMAT_FLAG_FLOAT
) || (lpcm
->format_flags
& QT_LPCM_FORMAT_FLAG_BIG_ENDIAN
)))
1434 || (lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_SOWT_AUDIO
)
1435 && (summary
->sample_size
!= 16 || (lpcm
->format_flags
& QT_LPCM_FORMAT_FLAG_FLOAT
) || (lpcm
->format_flags
& QT_LPCM_FORMAT_FLAG_BIG_ENDIAN
)))
1436 || (lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_TWOS_AUDIO
)
1437 && ((summary
->sample_size
!= 16 && summary
->sample_size
!= 8) || (lpcm
->format_flags
& QT_LPCM_FORMAT_FLAG_FLOAT
) || !(lpcm
->format_flags
& QT_LPCM_FORMAT_FLAG_BIG_ENDIAN
)))
1438 || (lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_NONE_AUDIO
)
1439 && ((summary
->sample_size
!= 16 && summary
->sample_size
!= 8) || (lpcm
->format_flags
& QT_LPCM_FORMAT_FLAG_FLOAT
) || !(lpcm
->format_flags
& QT_LPCM_FORMAT_FLAG_BIG_ENDIAN
)))
1440 || (lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_NOT_SPECIFIED
)
1441 && ((summary
->sample_size
!= 16 && summary
->sample_size
!= 8) || (lpcm
->format_flags
& QT_LPCM_FORMAT_FLAG_FLOAT
) || !(lpcm
->format_flags
& QT_LPCM_FORMAT_FLAG_BIG_ENDIAN
)))
1442 || (summary
->channels
> 2 || summary
->frequency
> UINT16_MAX
|| summary
->sample_size
% 8) )
1444 audio
->type
= QT_CODEC_TYPE_LPCM_AUDIO
;
1447 else if( lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_LPCM_AUDIO
) )
1449 else if( summary
->sample_size
> 16
1450 || (!lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_RAW_AUDIO
)
1451 && !lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_TWOS_AUDIO
)
1452 && !lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_NONE_AUDIO
)
1453 && !lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_NOT_SPECIFIED
)) )
1455 /* Set up constBytesPerAudioPacket field.
1456 * We use constBytesPerAudioPacket as the actual size of LPCM audio frame even when version is not 2. */
1457 audio
->constBytesPerAudioPacket
= (summary
->sample_size
* summary
->channels
) / 8;
1458 /* Set up other fields in this description by its version. */
1459 if( audio
->version
== 2 )
1461 audio
->channelcount
= 3;
1462 audio
->samplesize
= 16;
1463 audio
->compression_ID
= -2;
1464 audio
->samplerate
= 0x00010000;
1465 audio
->sizeOfStructOnly
= 72;
1466 audio
->audioSampleRate
= (union {double d
; uint64_t i
;}){summary
->frequency
}.i
;
1467 audio
->numAudioChannels
= summary
->channels
;
1468 audio
->always7F000000
= 0x7F000000;
1469 audio
->constBitsPerChannel
= summary
->sample_size
;
1470 audio
->constLPCMFramesPerAudioPacket
= 1;
1471 audio
->formatSpecificFlags
= lpcm
->format_flags
;
1472 if( lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_TWOS_AUDIO
) && summary
->sample_size
!= 8 )
1473 audio
->formatSpecificFlags
|= QT_LPCM_FORMAT_FLAG_BIG_ENDIAN
;
1474 if( lpcm
->format_flags
& QT_LPCM_FORMAT_FLAG_FLOAT
)
1475 audio
->formatSpecificFlags
&= ~QT_LPCM_FORMAT_FLAG_SIGNED_INTEGER
;
1476 if( lpcm
->format_flags
& QT_LPCM_FORMAT_FLAG_PACKED
)
1477 audio
->formatSpecificFlags
&= ~QT_LPCM_FORMAT_FLAG_ALIGNED_HIGH
;
1479 else if( audio
->version
== 1 )
1481 audio
->channelcount
= summary
->channels
;
1482 audio
->samplesize
= 16;
1483 /* Audio formats other than 'raw ' and 'twos' are treated as compressed audio. */
1484 if( lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_RAW_AUDIO
)
1485 || lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_TWOS_AUDIO
) )
1486 audio
->compression_ID
= QT_AUDIO_COMPRESSION_ID_NOT_COMPRESSED
;
1488 audio
->compression_ID
= QT_AUDIO_COMPRESSION_ID_FIXED_COMPRESSION
;
1489 audio
->samplerate
= summary
->frequency
<< 16;
1490 audio
->samplesPerPacket
= 1;
1491 audio
->bytesPerPacket
= summary
->sample_size
/ 8;
1492 audio
->bytesPerFrame
= audio
->bytesPerPacket
* summary
->channels
; /* sample_size field in stsz box is NOT used. */
1493 audio
->bytesPerSample
= 1 + (summary
->sample_size
!= 8);
1494 if( lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_FL32_AUDIO
)
1495 || lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_FL64_AUDIO
)
1496 || lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_IN24_AUDIO
)
1497 || lsmash_check_codec_type_identical( sample_type
, QT_CODEC_TYPE_IN32_AUDIO
) )
1499 isom_wave_t
*wave
= isom_add_wave( audio
);
1502 if( !(frma
= isom_add_frma( wave
))
1503 || !(enda
= isom_add_enda( wave
))
1504 || !isom_add_terminator( wave
) )
1506 lsmash_remove_entry_tail( &audio
->extensions
, wave
->destruct
);
1509 frma
->data_format
= sample_type
.fourcc
;
1510 enda
->littleEndian
= !(lpcm
->format_flags
& QT_LPCM_FORMAT_FLAG_BIG_ENDIAN
);
1513 else /* audio->version == 0 */
1515 audio
->channelcount
= summary
->channels
;
1516 audio
->samplesize
= summary
->sample_size
;
1517 audio
->compression_ID
= QT_AUDIO_COMPRESSION_ID_NOT_COMPRESSED
;
1518 audio
->samplerate
= summary
->frequency
<< 16;
1523 static int isom_set_isom_dts_description( isom_audio_entry_t
*audio
, lsmash_audio_summary_t
*summary
)
1526 audio
->revision_level
= 0;
1528 audio
->channelcount
= summary
->channels
;
1529 audio
->samplesize
= 16;
1530 audio
->compression_ID
= 0;
1531 audio
->packet_size
= 0;
1532 switch( summary
->frequency
)
1534 case 12000 : /* Invalid? (No reference in the spec) */
1539 case 384000 : /* Invalid? (No reference in the spec) */
1540 audio
->samplerate
= 48000 << 16;
1546 case 352800 : /* Invalid? (No reference in the spec) */
1547 audio
->samplerate
= 44100 << 16;
1549 case 8000 : /* Invalid? (No reference in the spec) */
1554 audio
->samplerate
= 32000 << 16;
1557 audio
->samplerate
= 0;
1563 static lsmash_box_type_t
isom_guess_audio_codec_specific_box_type( lsmash_codec_type_t active_codec_type
, lsmash_compact_box_type_t fourcc
)
1565 lsmash_box_type_t box_type
= LSMASH_BOX_TYPE_INITIALIZER
;
1566 box_type
.fourcc
= fourcc
;
1567 #define GUESS_AUDIO_CODEC_SPECIFIC_BOX_TYPE( codec_type, predefined_box_type ) \
1568 else if( (codec_type.user.fourcc == 0 \
1569 || lsmash_check_codec_type_identical( active_codec_type, codec_type )) \
1570 && box_type.fourcc == predefined_box_type.fourcc ) \
1571 box_type = predefined_box_type
1573 GUESS_AUDIO_CODEC_SPECIFIC_BOX_TYPE( ISOM_CODEC_TYPE_AC_3_AUDIO
, ISOM_BOX_TYPE_DAC3
);
1574 GUESS_AUDIO_CODEC_SPECIFIC_BOX_TYPE( ISOM_CODEC_TYPE_EC_3_AUDIO
, ISOM_BOX_TYPE_DEC3
);
1575 GUESS_AUDIO_CODEC_SPECIFIC_BOX_TYPE( ISOM_CODEC_TYPE_DTSC_AUDIO
, ISOM_BOX_TYPE_DDTS
);
1576 GUESS_AUDIO_CODEC_SPECIFIC_BOX_TYPE( ISOM_CODEC_TYPE_DTSE_AUDIO
, ISOM_BOX_TYPE_DDTS
);
1577 GUESS_AUDIO_CODEC_SPECIFIC_BOX_TYPE( ISOM_CODEC_TYPE_DTSH_AUDIO
, ISOM_BOX_TYPE_DDTS
);
1578 GUESS_AUDIO_CODEC_SPECIFIC_BOX_TYPE( ISOM_CODEC_TYPE_DTSL_AUDIO
, ISOM_BOX_TYPE_DDTS
);
1579 GUESS_AUDIO_CODEC_SPECIFIC_BOX_TYPE( ISOM_CODEC_TYPE_ALAC_AUDIO
, ISOM_BOX_TYPE_ALAC
);
1580 GUESS_AUDIO_CODEC_SPECIFIC_BOX_TYPE( ISOM_CODEC_TYPE_MP4A_AUDIO
, ISOM_BOX_TYPE_ESDS
);
1581 GUESS_AUDIO_CODEC_SPECIFIC_BOX_TYPE( QT_CODEC_TYPE_ALAC_AUDIO
, QT_BOX_TYPE_ALAC
);
1582 GUESS_AUDIO_CODEC_SPECIFIC_BOX_TYPE( QT_CODEC_TYPE_MP4A_AUDIO
, QT_BOX_TYPE_ESDS
);
1583 GUESS_AUDIO_CODEC_SPECIFIC_BOX_TYPE( QT_CODEC_TYPE_FULLMP3_AUDIO
, QT_CODEC_TYPE_MP3_AUDIO
);
1584 GUESS_AUDIO_CODEC_SPECIFIC_BOX_TYPE( QT_CODEC_TYPE_ADPCM2_AUDIO
, QT_CODEC_TYPE_ADPCM2_AUDIO
);
1585 GUESS_AUDIO_CODEC_SPECIFIC_BOX_TYPE( QT_CODEC_TYPE_ADPCM17_AUDIO
, QT_CODEC_TYPE_ADPCM17_AUDIO
);
1586 GUESS_AUDIO_CODEC_SPECIFIC_BOX_TYPE( QT_CODEC_TYPE_GSM49_AUDIO
, QT_CODEC_TYPE_GSM49_AUDIO
);
1587 GUESS_AUDIO_CODEC_SPECIFIC_BOX_TYPE( LSMASH_CODEC_TYPE_UNSPECIFIED
, QT_BOX_TYPE_CHAN
);
1588 GUESS_AUDIO_CODEC_SPECIFIC_BOX_TYPE( LSMASH_CODEC_TYPE_UNSPECIFIED
, QT_BOX_TYPE_GLBL
);
1589 GUESS_AUDIO_CODEC_SPECIFIC_BOX_TYPE( LSMASH_CODEC_TYPE_UNSPECIFIED
, QT_BOX_TYPE_WAVE
);
1590 #undef GUESS_AUDIO_CODEC_SPECIFIC_BOX_TYPE
1596 uint16_t wFormatTag
;
1598 uint32_t nSamplesPerSec
;
1599 uint32_t nAvgBytesPerSec
;
1600 uint16_t nBlockAlign
;
1601 uint16_t wBitsPerSample
;
1605 static lsmash_bs_t
*isom_create_waveform_audio_info
1607 wave_format_ex_t
*wfx
,
1608 lsmash_box_type_t type
1611 lsmash_bs_t
*bs
= lsmash_bs_create();
1614 lsmash_bs_put_be32( bs
, ISOM_BASEBOX_COMMON_SIZE
+ 18 + wfx
->cbSize
);
1615 lsmash_bs_put_be32( bs
, type
.fourcc
);
1616 lsmash_bs_put_le16( bs
, wfx
->wFormatTag
);
1617 lsmash_bs_put_le16( bs
, wfx
->nChannels
);
1618 lsmash_bs_put_le32( bs
, wfx
->nSamplesPerSec
);
1619 lsmash_bs_put_le32( bs
, wfx
->nAvgBytesPerSec
);
1620 lsmash_bs_put_le16( bs
, wfx
->nBlockAlign
);
1621 lsmash_bs_put_le16( bs
, wfx
->wBitsPerSample
);
1622 lsmash_bs_put_le16( bs
, wfx
->cbSize
);
1626 static int isom_setup_waveform_audio_info
1629 isom_audio_entry_t
*audio
,
1630 lsmash_audio_summary_t
*summary
,
1631 uint32_t samples_per_packet
,
1632 uint32_t bytes_per_frame
,
1633 uint32_t sample_size
1636 wave_format_ex_t wfx
;
1637 wfx
.wFormatTag
= 0x0000; /* WAVE_FORMAT_UNKNOWN */
1638 wfx
.nChannels
= summary
->channels
;
1639 wfx
.nSamplesPerSec
= summary
->frequency
;
1640 wfx
.nAvgBytesPerSec
= 0;
1641 wfx
.nBlockAlign
= bytes_per_frame
;
1642 wfx
.wBitsPerSample
= sample_size
;
1644 lsmash_bs_t
*bs
= NULL
;
1645 if( lsmash_check_codec_type_identical( audio
->type
, QT_CODEC_TYPE_ADPCM2_AUDIO
) )
1647 /* ADPCMWAVEFORMAT */
1648 wfx
.wFormatTag
= 0x0002; /* WAVE_FORMAT_ADPCM */
1650 bs
= isom_create_waveform_audio_info( &wfx
, audio
->type
);
1653 uint16_t wSamplesPerBlock
= samples_per_packet
; /* nBlockAlign * 2 / nChannels - 12 */
1654 uint16_t wNumCoef
= 7; /* Microsoft ADPCM uses just 7 coefficients. */
1659 } aCoef
[7] = { { 256, 0 }, { 512, -256 }, { 0,0 }, { 192,64 }, { 240,0 }, { 460, -208 }, { 392,-232 } };
1660 lsmash_bs_put_le16( bs
, wSamplesPerBlock
);
1661 lsmash_bs_put_le16( bs
, wNumCoef
);
1662 for( int i
= 0; i
< 7; i
++ )
1664 lsmash_bs_put_le16( bs
, aCoef
[i
].iCoef1
);
1665 lsmash_bs_put_le16( bs
, aCoef
[i
].iCoef2
);
1668 else if( lsmash_check_codec_type_identical( audio
->type
, QT_CODEC_TYPE_ADPCM17_AUDIO
) )
1670 /* IMAADPCMWAVEFORMAT */
1671 wfx
.wFormatTag
= 0x0011; /* WAVE_FORMAT_DVI_ADPCM / WAVE_FORMAT_IMA_ADPCM */
1673 bs
= isom_create_waveform_audio_info( &wfx
, audio
->type
);
1676 uint16_t wSamplesPerBlock
= samples_per_packet
;
1677 lsmash_bs_put_le16( bs
, wSamplesPerBlock
);
1679 else if( lsmash_check_codec_type_identical( audio
->type
, QT_CODEC_TYPE_GSM49_AUDIO
) )
1681 /* GSM610WAVEFORMAT */
1682 wfx
.wFormatTag
= 0x0031; /* WAVE_FORMAT_GSM610 */
1684 bs
= isom_create_waveform_audio_info( &wfx
, audio
->type
);
1687 uint16_t wSamplesPerBlock
= samples_per_packet
;
1688 lsmash_bs_put_le16( bs
, wSamplesPerBlock
);
1690 else if( lsmash_check_codec_type_identical( audio
->type
, QT_CODEC_TYPE_FULLMP3_AUDIO
)
1691 || lsmash_check_codec_type_identical( audio
->type
, QT_CODEC_TYPE_MP3_AUDIO
) )
1693 /* MPEGLAYER3WAVEFORMAT */
1694 wfx
.wFormatTag
= 0x0055; /* WAVE_FORMAT_MPEGLAYER3 */
1695 wfx
.nBlockAlign
= 1; /* ? */
1696 wfx
.wBitsPerSample
= 0; /* undefined */
1698 bs
= isom_create_waveform_audio_info( &wfx
, audio
->type
);
1701 uint16_t wID
= 1; /* MPEGLAYER3_ID_MPEG */
1702 uint32_t fdwFlags
= 0; /* We don't know whether the stream is padded or not here. */
1703 uint16_t nBlockSize
= 0; /* (144 * (bitrate / nSamplesPerSec) + padding) * nFramesPerBlock */
1704 uint16_t nFramesPerBlock
= 1; /* the number of audio frames per block */
1705 uint16_t nCodecDelay
= 0; /* Encoder delay in samples is unknown. */
1706 lsmash_bs_put_le16( bs
, wID
);
1707 lsmash_bs_put_le32( bs
, fdwFlags
);
1708 lsmash_bs_put_le16( bs
, nBlockSize
);
1709 lsmash_bs_put_le16( bs
, nFramesPerBlock
);
1710 lsmash_bs_put_le16( bs
, nCodecDelay
);
1718 uint8_t *wfx_data
= lsmash_bs_export_data( bs
, &wfx_size
);
1719 lsmash_bs_cleanup( bs
);
1722 if( wfx_size
!= ISOM_BASEBOX_COMMON_SIZE
+ 18 + wfx
.cbSize
1723 || isom_add_extension_binary( wave
, audio
->type
, LSMASH_BOX_PRECEDENCE_HM
, wfx_data
, wfx_size
) < 0 )
1725 lsmash_free( wfx_data
);
1731 static int isom_set_qtff_sound_decompression_parameters
1733 isom_audio_entry_t
*audio
,
1734 lsmash_audio_summary_t
*summary
,
1735 lsmash_qt_audio_format_specific_flag
*format_flags
,
1736 uint32_t samples_per_packet
,
1737 uint32_t bytes_per_frame
,
1738 uint32_t sample_size
1741 /* A 'wave' extension itself shall be absent in the opaque CODEC specific info list.
1742 * So, create a 'wave' extension here and append it as an extension to the audio sample description. */
1743 isom_wave_t
*wave
= isom_add_wave( audio
);
1744 if( !isom_add_frma ( wave
)
1745 || !isom_add_terminator( wave
) )
1747 lsmash_remove_entry_tail( &audio
->extensions
, wave
->destruct
);
1750 wave
->frma
->data_format
= audio
->type
.fourcc
;
1751 /* Append extensions from the opaque CODEC specific info list to 'wave' extension. */
1752 int waveform_audio_info_present
= 0;
1753 int requires_waveform_audio_info
= isom_is_waveform_audio( audio
->type
);
1754 for( lsmash_entry_t
*entry
= summary
->opaque
->list
.head
; entry
; entry
= entry
->next
)
1756 lsmash_codec_specific_t
*specific
= (lsmash_codec_specific_t
*)entry
->data
;
1759 if( specific
->type
== LSMASH_CODEC_SPECIFIC_DATA_TYPE_UNKNOWN
1760 && specific
->format
== LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
)
1761 continue; /* LSMASH_CODEC_SPECIFIC_DATA_TYPE_UNKNOWN + LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED is not supported. */
1762 switch( specific
->type
)
1764 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_COMMON
:
1765 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_CODEC_GLOBAL_HEADER
:
1766 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_FORMAT_SPECIFIC_FLAGS
:
1767 continue; /* These cannot be an extension for 'wave' extension. */
1768 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_CHANNEL_LAYOUT
:
1769 /* (Legacy?) ALAC might have an Audio Channel Layout Box inside 'wave' extension. */
1773 if( lsmash_check_codec_type_identical( audio
->type
, QT_CODEC_TYPE_ALAC_AUDIO
) )
1775 if( isom_append_channel_layout_extension( specific
, wave
, summary
->channels
) )
1781 assert( specific
->format
== LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
1782 || specific
->type
== LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_DECOMPRESSION_PARAMETERS
);
1783 lsmash_codec_specific_t
*cs
= lsmash_convert_codec_specific_format( specific
, LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
);
1786 if( cs
->size
< ISOM_BASEBOX_COMMON_SIZE
)
1788 lsmash_destroy_codec_specific_data( cs
);
1791 uint8_t *box_data
= cs
->data
.unstructured
;
1792 uint64_t box_size
= cs
->size
;
1793 lsmash_compact_box_type_t fourcc
= LSMASH_4CC( box_data
[4], box_data
[5], box_data
[6], box_data
[7] );
1794 if( audio
->version
== 2 && fourcc
== QT_BOX_TYPE_ENDA
.fourcc
)
1796 /* Don't append a 'enda' extension if version == 2.
1797 * Endianness is indicated in QuickTime audio format specific flags. */
1798 if( box_size
>= ISOM_BASEBOX_COMMON_SIZE
+ 2 )
1800 /* Override endianness indicated in format specific flags. */
1801 if( box_data
[9] == 1 )
1802 *format_flags
&= ~QT_AUDIO_FORMAT_FLAG_BIG_ENDIAN
;
1804 *format_flags
|= QT_AUDIO_FORMAT_FLAG_BIG_ENDIAN
;
1806 lsmash_destroy_codec_specific_data( cs
);
1809 lsmash_box_type_t box_type
= isom_guess_audio_codec_specific_box_type( audio
->type
, fourcc
);
1810 if( lsmash_check_box_type_identical( box_type
, QT_BOX_TYPE_WAVE
) )
1812 /* It is insane to appened a 'wave' extension to a 'wave' extension. */
1813 lsmash_destroy_codec_specific_data( cs
);
1816 box_type
= lsmash_form_qtff_box_type( box_type
.fourcc
);
1817 /* Determine 'precedence'. */
1818 uint64_t precedence
;
1819 if( lsmash_check_box_type_identical( box_type
, QT_BOX_TYPE_FRMA
) )
1820 precedence
= LSMASH_BOX_PRECEDENCE_QTFF_FRMA
;
1821 else if( lsmash_check_box_type_identical( box_type
, QT_BOX_TYPE_ESDS
) )
1822 precedence
= LSMASH_BOX_PRECEDENCE_QTFF_ESDS
;
1823 else if( lsmash_check_box_type_identical( box_type
, QT_BOX_TYPE_ENDA
) )
1824 precedence
= LSMASH_BOX_PRECEDENCE_QTFF_ENDA
;
1825 else if( lsmash_check_box_type_identical( box_type
, QT_BOX_TYPE_MP4A
) )
1826 precedence
= LSMASH_BOX_PRECEDENCE_QTFF_MP4A
;
1827 else if( lsmash_check_box_type_identical( box_type
, QT_BOX_TYPE_TERMINATOR
) )
1828 precedence
= LSMASH_BOX_PRECEDENCE_QTFF_TERMINATOR
;
1830 precedence
= LSMASH_BOX_PRECEDENCE_HM
;
1831 /* Append the extension. */
1832 int ret
= isom_add_extension_binary( wave
, box_type
, precedence
, cs
->data
.unstructured
, cs
->size
);
1833 cs
->data
.unstructured
= NULL
; /* Avoid freeing the binary data of the extension. */
1834 lsmash_destroy_codec_specific_data( cs
);
1837 if( isom_is_waveform_audio( box_type
) )
1838 waveform_audio_info_present
= 1;
1843 if( requires_waveform_audio_info
&& !waveform_audio_info_present
1844 && isom_setup_waveform_audio_info( wave
, audio
, summary
, samples_per_packet
, bytes_per_frame
, sample_size
) < 0 )
1849 static int isom_set_qtff_template_audio_description( isom_audio_entry_t
*audio
, lsmash_audio_summary_t
*summary
)
1851 audio
->manager
|= LSMASH_QTFF_BASE
;
1852 audio
->type
= lsmash_form_qtff_box_type( audio
->type
.fourcc
);
1853 audio
->version
= (summary
->channels
> 2 || summary
->frequency
> UINT16_MAX
) ? 2 : 1;
1854 /* Try to get QuickTime audio format specific flags. */
1855 lsmash_qt_audio_format_specific_flag format_flags
= QT_AUDIO_FORMAT_FLAG_BIG_ENDIAN
;
1856 for( lsmash_entry_t
*entry
= summary
->opaque
->list
.head
; entry
; entry
= entry
->next
)
1858 lsmash_codec_specific_t
*specific
= (lsmash_codec_specific_t
*)entry
->data
;
1860 || !specific
->data
.structured
)
1862 if( specific
->type
== LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_FORMAT_SPECIFIC_FLAGS
1863 && specific
->format
== LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
)
1865 /* A format specific flags is found.
1866 * Force audio sample description version == 2. */
1867 format_flags
= ((lsmash_qt_audio_format_specific_flags_t
*)specific
->data
.structured
)->format_flags
;
1872 uint32_t samples_per_packet
;
1873 uint32_t bytes_per_frame
;
1874 uint32_t sample_size
;
1875 if( !((summary
->samples_in_frame
== 0 || summary
->bytes_per_frame
== 0 || summary
->sample_size
== 0)
1876 && isom_get_implicit_qt_fixed_comp_audio_sample_quants( audio
, &samples_per_packet
, &bytes_per_frame
, &sample_size
)) )
1878 samples_per_packet
= summary
->samples_in_frame
;
1879 bytes_per_frame
= summary
->bytes_per_frame
;
1880 sample_size
= summary
->sample_size
;
1882 if( (!lsmash_check_codec_type_identical( audio
->type
, QT_CODEC_TYPE_MAC3_AUDIO
)
1883 && !lsmash_check_codec_type_identical( audio
->type
, QT_CODEC_TYPE_MAC6_AUDIO
)
1884 && !lsmash_check_codec_type_identical( audio
->type
, QT_CODEC_TYPE_AGSM_AUDIO
)
1885 && !lsmash_check_codec_type_identical( audio
->type
, QT_CODEC_TYPE_ALAW_AUDIO
)
1886 && !lsmash_check_codec_type_identical( audio
->type
, QT_CODEC_TYPE_ULAW_AUDIO
))
1887 && isom_set_qtff_sound_decompression_parameters( audio
, summary
, &format_flags
, samples_per_packet
, bytes_per_frame
, sample_size
) < 0 )
1889 /* Set up common audio description fields. */
1890 audio
->samplesize
= 16;
1891 audio
->packet_size
= 0;
1892 if( audio
->version
== 2 )
1894 audio
->channelcount
= 3;
1895 audio
->compression_ID
= QT_AUDIO_COMPRESSION_ID_VARIABLE_COMPRESSION
;
1896 audio
->samplerate
= 0x00010000;
1897 audio
->sizeOfStructOnly
= 72;
1898 audio
->audioSampleRate
= (union {double d
; uint64_t i
;}){summary
->frequency
}.i
;
1899 audio
->numAudioChannels
= summary
->channels
;
1900 audio
->always7F000000
= 0x7F000000;
1901 audio
->constBitsPerChannel
= 0;
1902 audio
->constBytesPerAudioPacket
= bytes_per_frame
;
1903 audio
->constLPCMFramesPerAudioPacket
= samples_per_packet
;
1904 if( lsmash_check_codec_type_identical( audio
->type
, QT_CODEC_TYPE_ALAC_AUDIO
) )
1906 switch( sample_size
)
1909 audio
->formatSpecificFlags
= QT_ALAC_FORMAT_FLAG_16BIT_SOURCE_DATA
;
1912 audio
->formatSpecificFlags
= QT_ALAC_FORMAT_FLAG_20BIT_SOURCE_DATA
;
1915 audio
->formatSpecificFlags
= QT_ALAC_FORMAT_FLAG_24BIT_SOURCE_DATA
;
1918 audio
->formatSpecificFlags
= QT_ALAC_FORMAT_FLAG_32BIT_SOURCE_DATA
;
1926 if( format_flags
& QT_AUDIO_FORMAT_FLAG_FLOAT
)
1927 format_flags
&= ~QT_AUDIO_FORMAT_FLAG_SIGNED_INTEGER
;
1928 if( format_flags
& QT_AUDIO_FORMAT_FLAG_PACKED
)
1929 format_flags
&= ~QT_AUDIO_FORMAT_FLAG_ALIGNED_HIGH
;
1930 audio
->formatSpecificFlags
= format_flags
;
1933 else /* if( audio->version == 1 ) */
1935 audio
->channelcount
= LSMASH_MIN( summary
->channels
, 2 );
1936 audio
->compression_ID
= QT_AUDIO_COMPRESSION_ID_FIXED_COMPRESSION
;
1937 audio
->samplerate
= summary
->frequency
<< 16;
1938 audio
->samplesPerPacket
= samples_per_packet
;
1939 audio
->bytesPerPacket
= bytes_per_frame
/ summary
->channels
;
1940 audio
->bytesPerFrame
= bytes_per_frame
; /* sample_size field in stsz box is NOT used. */
1941 audio
->bytesPerSample
= 1 + (sample_size
!= 8);
1946 static void isom_set_samplerate_division_of_media_timescale( isom_audio_entry_t
*audio
, int strict
)
1948 if( audio
->parent
/* stsd */
1949 && audio
->parent
->parent
/* stbl */
1950 && audio
->parent
->parent
->parent
/* minf */
1951 && audio
->parent
->parent
->parent
->parent
/* mdia */
1952 && lsmash_check_box_type_identical( audio
->parent
->parent
->parent
->parent
->type
, ISOM_BOX_TYPE_MDIA
)
1953 && ((isom_mdia_t
*)audio
->parent
->parent
->parent
->parent
)->mdhd
)
1955 /* Make an effort to match the timescale with samplerate, or be an integer multiple of it. */
1956 uint32_t orig_timescale
= ((isom_mdia_t
*)audio
->parent
->parent
->parent
->parent
)->mdhd
->timescale
;
1957 uint32_t timescale
= orig_timescale
;
1959 while( timescale
> UINT16_MAX
&& timescale
> 1 && i
<= UINT32_MAX
)
1961 if( timescale
% i
== 0 )
1966 if( timescale
!= orig_timescale
&& strict
)
1967 lsmash_log( NULL
, LSMASH_LOG_WARNING
, "samplerate does not match the media timescale.\n" );
1968 if( timescale
<= UINT16_MAX
&& timescale
> 1 )
1970 audio
->samplerate
= timescale
<< 16;
1974 audio
->samplerate
= 0;
1977 static int isom_set_isom_template_audio_description( isom_audio_entry_t
*audio
, lsmash_audio_summary_t
*summary
)
1979 audio
->version
= 0; /* reserved */
1980 audio
->revision_level
= 0; /* reserved */
1981 audio
->vendor
= 0; /* reserved */
1982 audio
->channelcount
= 2; /* template */
1983 audio
->samplesize
= 16; /* template */
1984 audio
->compression_ID
= 0; /* pre_defined */
1985 audio
->packet_size
= 0; /* reserved */
1986 /* template : default output audio sampling rate at playback */
1987 if( summary
->frequency
<= UINT16_MAX
)
1988 audio
->samplerate
= summary
->frequency
<< 16;
1990 isom_set_samplerate_division_of_media_timescale( audio
, 0 );
1994 static int isom_set_isom_amr_audio_description( isom_audio_entry_t
*audio
, int wb
)
1996 /* For AMR-NB and AMR-WB stream, these fields are not meaningful. */
1997 audio
->version
= 0; /* always 0 */
1998 audio
->revision_level
= 0; /* always 0 */
1999 audio
->vendor
= 0; /* always 0 */
2000 audio
->channelcount
= 2; /* always 2 although the actual number of channels is always 1 */
2001 audio
->samplesize
= 16; /* always 16 */
2002 audio
->compression_ID
= 0; /* always 0 */
2003 audio
->packet_size
= 0; /* always 0 */
2004 /* Set samplerate by trying to copy from Media Header Box of this media though the
2005 * actual samplerate is 8000 Hz for AMR-NB and 16000 Hz for AMR-WB.
2006 * 3GPP and 3GPP2 has no restriction for media timescale. Therefore, users should
2007 * set suitable media timescale by themselves within the bounds of common sense. */
2008 isom_set_samplerate_division_of_media_timescale( audio
, 1 );
2009 if( audio
->samplerate
== 0 )
2010 /* Set hard-coded but correct samplerate in the CODEC level. */
2011 audio
->samplerate
= wb
? 8000 : 16000;
2015 int isom_setup_audio_description( isom_stsd_t
*stsd
, lsmash_codec_type_t sample_type
, lsmash_audio_summary_t
*summary
)
2017 if( !stsd
|| !stsd
->file
|| !summary
)
2019 if( isom_check_valid_summary( (lsmash_summary_t
*)summary
) )
2021 isom_audio_entry_t
*audio
= isom_add_audio_description( stsd
, sample_type
);
2024 audio
->data_reference_index
= summary
->data_ref_index
;
2025 lsmash_file_t
*file
= stsd
->file
;
2026 lsmash_codec_type_t audio_type
= audio
->type
;
2028 if( lsmash_check_codec_type_identical( audio_type
, ISOM_CODEC_TYPE_MP4A_AUDIO
)
2029 || lsmash_check_codec_type_identical( audio_type
, QT_CODEC_TYPE_MP4A_AUDIO
) )
2031 if( (file
->ftyp
&& file
->ftyp
->major_brand
== ISOM_BRAND_TYPE_QT
)
2032 || (!file
->ftyp
&& (file
->qt_compatible
|| (file
->moov
&& !file
->moov
->iods
))) )
2033 ret
= isom_set_qtff_mp4a_description( audio
, summary
);
2035 ret
= isom_set_isom_mp4a_description( audio
, summary
);
2037 else if( isom_is_lpcm_audio( audio
) )
2038 ret
= isom_set_qtff_lpcm_description( audio
, summary
);
2039 else if( lsmash_check_codec_type_identical( audio_type
, ISOM_CODEC_TYPE_DTSC_AUDIO
)
2040 || lsmash_check_codec_type_identical( audio_type
, ISOM_CODEC_TYPE_DTSE_AUDIO
)
2041 || lsmash_check_codec_type_identical( audio_type
, ISOM_CODEC_TYPE_DTSH_AUDIO
)
2042 || lsmash_check_codec_type_identical( audio_type
, ISOM_CODEC_TYPE_DTSL_AUDIO
) )
2043 ret
= isom_set_isom_dts_description( audio
, summary
);
2044 else if( file
->qt_compatible
)
2045 ret
= isom_set_qtff_template_audio_description( audio
, summary
);
2046 else if( lsmash_check_codec_type_identical( audio_type
, ISOM_CODEC_TYPE_SAMR_AUDIO
) )
2047 ret
= isom_set_isom_amr_audio_description( audio
, 0 );
2048 else if( lsmash_check_codec_type_identical( audio_type
, ISOM_CODEC_TYPE_SAWB_AUDIO
) )
2049 ret
= isom_set_isom_amr_audio_description( audio
, 1 );
2051 ret
= isom_set_isom_template_audio_description( audio
, summary
);
2054 /* Don't use audio_type since audio->type might have changed. */
2055 for( lsmash_entry_t
*entry
= summary
->opaque
->list
.head
; entry
; entry
= entry
->next
)
2057 lsmash_codec_specific_t
*specific
= (lsmash_codec_specific_t
*)entry
->data
;
2060 if( specific
->type
== LSMASH_CODEC_SPECIFIC_DATA_TYPE_UNKNOWN
2061 && specific
->format
== LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
)
2062 continue; /* LSMASH_CODEC_SPECIFIC_DATA_TYPE_UNKNOWN + LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED is not supported. */
2063 switch( specific
->type
)
2065 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_COMMON
:
2067 if( specific
->format
== LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
)
2068 continue; /* Ignore since not fatal. */
2069 lsmash_qt_audio_common_t
*data
= (lsmash_qt_audio_common_t
*)specific
->data
.structured
;
2070 audio
->revision_level
= data
->revision_level
;
2071 audio
->vendor
= data
->vendor
;
2072 if( audio
->version
== 1
2073 && !isom_is_lpcm_audio( audio
)
2074 && data
->compression_ID
!= QT_AUDIO_COMPRESSION_ID_NOT_COMPRESSED
)
2076 /* Compressed audio must not be set to QT_AUDIO_COMPRESSION_ID_NOT_COMPRESSED. */
2077 audio
->compression_ID
= data
->compression_ID
;
2078 if( audio
->compression_ID
== QT_AUDIO_COMPRESSION_ID_VARIABLE_COMPRESSION
)
2080 /* For variable compression, bytesPerPacket and bytesPerFrame are reserved and should be set to 0. */
2081 audio
->bytesPerPacket
= 0;
2082 audio
->bytesPerFrame
= 0;
2087 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_CHANNEL_LAYOUT
:
2089 if( !file
->qt_compatible
2090 && !lsmash_check_codec_type_identical( audio
->type
, ISOM_CODEC_TYPE_ALAC_AUDIO
)
2091 && !lsmash_check_codec_type_identical( audio
->type
, QT_CODEC_TYPE_ALAC_AUDIO
) )
2093 if( isom_append_channel_layout_extension( specific
, audio
, summary
->channels
) )
2097 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_CODEC_GLOBAL_HEADER
:
2099 lsmash_codec_specific_t
*cs
= lsmash_convert_codec_specific_format( specific
, LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
2102 lsmash_codec_global_header_t
*data
= (lsmash_codec_global_header_t
*)cs
->data
.structured
;
2103 isom_glbl_t
*glbl
= isom_add_glbl( audio
);
2106 lsmash_destroy_codec_specific_data( cs
);
2109 glbl
->header_size
= data
->header_size
;
2110 glbl
->header_data
= lsmash_memdup( data
->header_data
, data
->header_size
);
2111 lsmash_destroy_codec_specific_data( cs
);
2112 if( !glbl
->header_data
)
2114 isom_remove_box_by_itself( glbl
);
2119 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_FORMAT_SPECIFIC_FLAGS
:
2120 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_DECOMPRESSION_PARAMETERS
:
2121 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_MP4SYS_DECODER_CONFIG
:
2122 break; /* shall be set up already */
2123 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_ALAC
:
2124 if( file
->qt_compatible
)
2125 continue; /* shall be set up already */
2128 lsmash_codec_specific_t
*cs
= lsmash_convert_codec_specific_format( specific
, LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
);
2131 if( cs
->size
< ISOM_BASEBOX_COMMON_SIZE
)
2133 lsmash_destroy_codec_specific_data( cs
);
2136 uint8_t *box_data
= cs
->data
.unstructured
;
2137 lsmash_compact_box_type_t fourcc
= LSMASH_4CC( box_data
[4], box_data
[5], box_data
[6], box_data
[7] );
2138 lsmash_box_type_t box_type
= isom_guess_audio_codec_specific_box_type( audio
->type
, fourcc
);
2139 if( lsmash_check_box_type_identical( box_type
, QT_BOX_TYPE_WAVE
) )
2141 /* CODEC specific info shall be already inside 'wave' extension. */
2142 lsmash_destroy_codec_specific_data( cs
);
2145 /* Append the extension. */
2146 ret
= isom_add_extension_binary( audio
, box_type
, LSMASH_BOX_PRECEDENCE_HM
, cs
->data
.unstructured
, cs
->size
);
2147 cs
->data
.unstructured
= NULL
; /* Avoid freeing the binary data of the extension. */
2148 lsmash_destroy_codec_specific_data( cs
);
2155 if( audio
->version
== 0 )
2156 audio
->compression_ID
= QT_AUDIO_COMPRESSION_ID_NOT_COMPRESSED
;
2157 else if( audio
->version
== 2 )
2158 audio
->compression_ID
= QT_AUDIO_COMPRESSION_ID_VARIABLE_COMPRESSION
;
2161 isom_remove_box_by_itself( audio
);
2165 int isom_setup_tx3g_description( isom_stsd_t
*stsd
, lsmash_summary_t
*summary
)
2167 isom_tx3g_entry_t
*tx3g
= isom_add_tx3g_description( stsd
);
2170 /* We create a dummy font record to make valid font_ID in the sample description.
2171 * The specification (3GPP TS 26.245) does not forbid the value 0 for the identifier,
2172 * but we set 1 to it as track_ID begins from 1. */
2173 tx3g
->data_reference_index
= summary
->data_ref_index
;
2174 tx3g
->font_ID
= 1; /* ID of the default font record */
2175 isom_ftab_t
*ftab
= isom_add_ftab( tx3g
);
2178 isom_font_record_t
*font
= lsmash_malloc( sizeof(isom_font_record_t
) );
2181 if( lsmash_add_entry( ftab
->list
, font
) < 0 )
2183 lsmash_free( font
);
2186 const char font_names
[] = "Serif,Sans-serif,Monospace";
2188 font
->font_name_length
= sizeof(font_names
);
2189 font
->font_name
= lsmash_memdup( font_names
, sizeof(font_names
) );
2190 if( !font
->font_name
)
2194 isom_remove_box_by_itself( tx3g
);
2198 static lsmash_codec_specific_data_type
isom_get_codec_specific_data_type( lsmash_compact_box_type_t extension_fourcc
)
2200 static struct codec_specific_data_type_table_tag
2202 lsmash_compact_box_type_t extension_fourcc
;
2203 lsmash_codec_specific_data_type data_type
;
2204 } codec_specific_data_type_table
[32] = { { 0, LSMASH_CODEC_SPECIFIC_DATA_TYPE_UNKNOWN
} };
2205 if( codec_specific_data_type_table
[0].data_type
== LSMASH_CODEC_SPECIFIC_DATA_TYPE_UNKNOWN
)
2208 #define ADD_CODEC_SPECIFIC_DATA_TYPE_TABLE_ELEMENT( extension_type, data_type ) \
2209 codec_specific_data_type_table[i++] = (struct codec_specific_data_type_table_tag){ extension_type.fourcc, data_type }
2210 ADD_CODEC_SPECIFIC_DATA_TYPE_TABLE_ELEMENT( ISOM_BOX_TYPE_AVCC
, LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264
);
2211 ADD_CODEC_SPECIFIC_DATA_TYPE_TABLE_ELEMENT( ISOM_BOX_TYPE_HVCC
, LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_HEVC
);
2212 ADD_CODEC_SPECIFIC_DATA_TYPE_TABLE_ELEMENT( ISOM_BOX_TYPE_DVC1
, LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_VC_1
);
2213 ADD_CODEC_SPECIFIC_DATA_TYPE_TABLE_ELEMENT( ISOM_BOX_TYPE_DAC3
, LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_AC_3
);
2214 ADD_CODEC_SPECIFIC_DATA_TYPE_TABLE_ELEMENT( ISOM_BOX_TYPE_DEC3
, LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_EC_3
);
2215 ADD_CODEC_SPECIFIC_DATA_TYPE_TABLE_ELEMENT( ISOM_BOX_TYPE_DDTS
, LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_DTS
);
2216 ADD_CODEC_SPECIFIC_DATA_TYPE_TABLE_ELEMENT( ISOM_BOX_TYPE_ALAC
, LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_ALAC
);
2217 ADD_CODEC_SPECIFIC_DATA_TYPE_TABLE_ELEMENT( ISOM_BOX_TYPE_ESDS
, LSMASH_CODEC_SPECIFIC_DATA_TYPE_MP4SYS_DECODER_CONFIG
);
2218 ADD_CODEC_SPECIFIC_DATA_TYPE_TABLE_ELEMENT( ISOM_BOX_TYPE_STSL
, LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_SAMPLE_SCALE
);
2219 ADD_CODEC_SPECIFIC_DATA_TYPE_TABLE_ELEMENT( ISOM_BOX_TYPE_BTRT
, LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264_BITRATE
);
2220 //ADD_CODEC_SPECIFIC_DATA_TYPE_TABLE_ELEMENT( QT_BOX_TYPE_ALAC, LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_ALAC );
2221 //ADD_CODEC_SPECIFIC_DATA_TYPE_TABLE_ELEMENT( QT_BOX_TYPE_ESDS, LSMASH_CODEC_SPECIFIC_DATA_TYPE_MP4SYS_DECODER_CONFIG );
2222 ADD_CODEC_SPECIFIC_DATA_TYPE_TABLE_ELEMENT( QT_BOX_TYPE_FIEL
, LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_FIELD_INFO
);
2223 ADD_CODEC_SPECIFIC_DATA_TYPE_TABLE_ELEMENT( QT_BOX_TYPE_CSPC
, LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_PIXEL_FORMAT
);
2224 ADD_CODEC_SPECIFIC_DATA_TYPE_TABLE_ELEMENT( QT_BOX_TYPE_SGBT
, LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_SIGNIFICANT_BITS
);
2225 ADD_CODEC_SPECIFIC_DATA_TYPE_TABLE_ELEMENT( QT_BOX_TYPE_GAMA
, LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_GAMMA_LEVEL
);
2226 ADD_CODEC_SPECIFIC_DATA_TYPE_TABLE_ELEMENT( QT_BOX_TYPE_CHAN
, LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_CHANNEL_LAYOUT
);
2227 ADD_CODEC_SPECIFIC_DATA_TYPE_TABLE_ELEMENT( QT_BOX_TYPE_GLBL
, LSMASH_CODEC_SPECIFIC_DATA_TYPE_CODEC_GLOBAL_HEADER
);
2228 ADD_CODEC_SPECIFIC_DATA_TYPE_TABLE_ELEMENT( LSMASH_BOX_TYPE_UNSPECIFIED
, LSMASH_CODEC_SPECIFIC_DATA_TYPE_UNKNOWN
);
2229 #undef ADD_CODEC_SPECIFIC_DATA_TYPE_TABLE_ELEMENT
2231 lsmash_codec_specific_data_type data_type
= LSMASH_CODEC_SPECIFIC_DATA_TYPE_UNKNOWN
;
2232 for( int i
= 0; codec_specific_data_type_table
[i
].data_type
!= LSMASH_CODEC_SPECIFIC_DATA_TYPE_UNKNOWN
; i
++ )
2233 if( extension_fourcc
== codec_specific_data_type_table
[i
].extension_fourcc
)
2235 data_type
= codec_specific_data_type_table
[i
].data_type
;
2241 lsmash_summary_t
*isom_create_video_summary_from_description( isom_sample_entry_t
*sample_entry
)
2245 isom_visual_entry_t
*visual
= (isom_visual_entry_t
*)sample_entry
;
2246 lsmash_video_summary_t
*summary
= (lsmash_video_summary_t
*)lsmash_create_summary( LSMASH_SUMMARY_TYPE_VIDEO
);
2249 summary
->sample_type
= visual
->type
;
2250 summary
->data_ref_index
= visual
->data_reference_index
;
2251 summary
->width
= visual
->width
;
2252 summary
->height
= visual
->height
;
2253 summary
->depth
= visual
->depth
;
2254 memcpy( summary
->compressorname
, visual
->compressorname
, 32 );
2255 summary
->compressorname
[32] = '\0';
2256 if( isom_is_qt_video( summary
->sample_type
) )
2258 lsmash_codec_specific_t
*specific
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_COMMON
,
2259 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
2262 lsmash_qt_video_common_t
*data
= (lsmash_qt_video_common_t
*)specific
->data
.structured
;
2263 data
->revision_level
= visual
->revision_level
;
2264 data
->vendor
= visual
->vendor
;
2265 data
->temporalQuality
= visual
->temporalQuality
;
2266 data
->spatialQuality
= visual
->spatialQuality
;
2267 data
->horizontal_resolution
= visual
->horizresolution
;
2268 data
->vertical_resolution
= visual
->vertresolution
;
2269 data
->dataSize
= visual
->dataSize
;
2270 data
->frame_count
= visual
->frame_count
;
2271 data
->color_table_ID
= visual
->color_table_ID
;
2272 if( visual
->color_table_ID
== 0 )
2274 isom_qt_color_table_t
*src_ct
= &visual
->color_table
;
2275 if( !src_ct
->array
)
2277 uint16_t element_count
= LSMASH_MIN( src_ct
->size
+ 1, 256 );
2278 lsmash_qt_color_table_t
*dst_ct
= &data
->color_table
;
2279 dst_ct
->seed
= src_ct
->seed
;
2280 dst_ct
->flags
= src_ct
->flags
;
2281 dst_ct
->size
= src_ct
->size
;
2282 for( uint16_t i
= 0; i
< element_count
; i
++ )
2284 dst_ct
->array
[i
].unused
= src_ct
->array
[i
].value
;
2285 dst_ct
->array
[i
].r
= src_ct
->array
[i
].r
;
2286 dst_ct
->array
[i
].g
= src_ct
->array
[i
].g
;
2287 dst_ct
->array
[i
].b
= src_ct
->array
[i
].b
;
2290 if( lsmash_add_entry( &summary
->opaque
->list
, specific
) )
2292 lsmash_destroy_codec_specific_data( specific
);
2296 for( lsmash_entry_t
*entry
= visual
->extensions
.head
; entry
; entry
= entry
->next
)
2298 isom_box_t
*box
= (isom_box_t
*)entry
->data
;
2301 if( !(box
->manager
& LSMASH_BINARY_CODED_BOX
) )
2303 lsmash_codec_specific_t
*specific
= NULL
;
2304 if( lsmash_check_box_type_identical( box
->type
, ISOM_BOX_TYPE_CLAP
) )
2306 isom_clap_t
*clap
= (isom_clap_t
*)box
;
2307 summary
->clap
.width
.n
= clap
->cleanApertureWidthN
;
2308 summary
->clap
.width
.d
= clap
->cleanApertureWidthD
;
2309 summary
->clap
.height
.n
= clap
->cleanApertureHeightN
;
2310 summary
->clap
.height
.d
= clap
->cleanApertureHeightD
;
2311 summary
->clap
.horizontal_offset
.n
= clap
->horizOffN
;
2312 summary
->clap
.horizontal_offset
.d
= clap
->horizOffD
;
2313 summary
->clap
.vertical_offset
.n
= clap
->vertOffN
;
2314 summary
->clap
.vertical_offset
.d
= clap
->vertOffD
;
2317 else if( lsmash_check_box_type_identical( box
->type
, ISOM_BOX_TYPE_PASP
) )
2319 isom_pasp_t
*pasp
= (isom_pasp_t
*)box
;
2320 summary
->par_h
= pasp
->hSpacing
;
2321 summary
->par_v
= pasp
->vSpacing
;
2324 else if( lsmash_check_box_type_identical( box
->type
, ISOM_BOX_TYPE_COLR
)
2325 || lsmash_check_box_type_identical( box
->type
, QT_BOX_TYPE_COLR
) )
2327 isom_colr_t
*colr
= (isom_colr_t
*)box
;
2328 summary
->color
.primaries_index
= colr
->primaries_index
;
2329 summary
->color
.transfer_index
= colr
->transfer_function_index
;
2330 summary
->color
.matrix_index
= colr
->matrix_index
;
2331 summary
->color
.full_range
= colr
->full_range_flag
;
2334 else if( lsmash_check_box_type_identical( box
->type
, ISOM_BOX_TYPE_STSL
) )
2336 specific
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_SAMPLE_SCALE
,
2337 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
2340 isom_stsl_t
*stsl
= (isom_stsl_t
*)box
;
2341 lsmash_isom_sample_scale_t
*data
= (lsmash_isom_sample_scale_t
*)specific
->data
.structured
;
2342 data
->constraint_flag
= stsl
->constraint_flag
;
2343 data
->scale_method
= stsl
->scale_method
;
2344 data
->display_center_x
= stsl
->display_center_x
;
2345 data
->display_center_y
= stsl
->display_center_y
;
2347 else if( lsmash_check_box_type_identical( box
->type
, ISOM_BOX_TYPE_BTRT
) )
2349 specific
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264_BITRATE
,
2350 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
2353 isom_btrt_t
*btrt
= (isom_btrt_t
*)box
;
2354 lsmash_h264_bitrate_t
*data
= (lsmash_h264_bitrate_t
*)specific
->data
.structured
;
2355 data
->bufferSizeDB
= btrt
->bufferSizeDB
;
2356 data
->maxBitrate
= btrt
->maxBitrate
;
2357 data
->avgBitrate
= btrt
->avgBitrate
;
2359 else if( lsmash_check_box_type_identical( box
->type
, QT_BOX_TYPE_FIEL
) )
2361 specific
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_FIELD_INFO
,
2362 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
2365 isom_fiel_t
*fiel
= (isom_fiel_t
*)box
;
2366 lsmash_qt_field_info_t
*data
= (lsmash_qt_field_info_t
*)specific
->data
.structured
;
2367 data
->fields
= fiel
->fields
;
2368 data
->detail
= fiel
->detail
;
2370 else if( lsmash_check_box_type_identical( box
->type
, QT_BOX_TYPE_CSPC
) )
2372 specific
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_PIXEL_FORMAT
,
2373 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
2376 isom_cspc_t
*cspc
= (isom_cspc_t
*)box
;
2377 lsmash_qt_pixel_format_t
*data
= (lsmash_qt_pixel_format_t
*)specific
->data
.structured
;
2378 data
->pixel_format
= cspc
->pixel_format
;
2380 else if( lsmash_check_box_type_identical( box
->type
, QT_BOX_TYPE_SGBT
) )
2382 specific
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_SIGNIFICANT_BITS
,
2383 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
2386 isom_sgbt_t
*sgbt
= (isom_sgbt_t
*)box
;
2387 lsmash_qt_significant_bits_t
*data
= (lsmash_qt_significant_bits_t
*)specific
->data
.structured
;
2388 data
->significantBits
= sgbt
->significantBits
;
2390 else if( lsmash_check_box_type_identical( box
->type
, QT_BOX_TYPE_GLBL
) )
2392 specific
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_CODEC_GLOBAL_HEADER
,
2393 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
2396 isom_glbl_t
*glbl
= (isom_glbl_t
*)box
;
2397 lsmash_codec_global_header_t
*data
= (lsmash_codec_global_header_t
*)specific
->data
.structured
;
2398 data
->header_size
= glbl
->header_size
;
2399 data
->header_data
= lsmash_memdup( glbl
->header_data
, glbl
->header_size
);
2400 if( !data
->header_data
)
2402 lsmash_destroy_codec_specific_data( specific
);
2408 if( lsmash_add_entry( &summary
->opaque
->list
, specific
) )
2410 lsmash_destroy_codec_specific_data( specific
);
2416 if( box
->size
< ISOM_BASEBOX_COMMON_SIZE
)
2418 uint8_t *data
= box
->binary
;
2419 lsmash_compact_box_type_t fourcc
= LSMASH_4CC( data
[4], data
[5], data
[6], data
[7] );
2420 lsmash_codec_specific_data_type type
= isom_get_codec_specific_data_type( fourcc
);
2421 lsmash_codec_specific_t
*specific
= lsmash_create_codec_specific_data( type
, LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
);
2424 specific
->size
= box
->size
;
2425 specific
->data
.unstructured
= lsmash_memdup( box
->binary
, box
->size
);
2426 if( !specific
->data
.unstructured
2427 || lsmash_add_entry( &summary
->opaque
->list
, specific
) )
2429 lsmash_destroy_codec_specific_data( specific
);
2434 return (lsmash_summary_t
*)summary
;
2436 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
2440 static int isom_append_structured_mp4sys_decoder_config( lsmash_codec_specific_list_t
*opaque
, isom_esds_t
*esds
)
2442 lsmash_bs_t
*bs
= lsmash_bs_create();
2445 /* Put box size, type, version and flags fields. */
2446 lsmash_bs_put_be32( bs
, 0 );
2447 lsmash_bs_put_be32( bs
, ISOM_BOX_TYPE_ESDS
.fourcc
);
2448 lsmash_bs_put_be32( bs
, 0 );
2449 /* Put ES Descriptor. */
2450 mp4sys_update_descriptor_size( esds
->ES
);
2451 mp4sys_write_descriptor( bs
, esds
->ES
);
2452 /* Export ES Descriptor Box as binary string. */
2454 uint8_t *esds_data
= lsmash_bs_export_data( bs
, &esds_size
);
2455 lsmash_bs_cleanup( bs
);
2458 /* Update box size. */
2459 LSMASH_SET_BE32( esds_data
, esds_size
);
2460 lsmash_codec_specific_data_type type
= isom_get_codec_specific_data_type( ISOM_BOX_TYPE_ESDS
.fourcc
);
2461 lsmash_codec_specific_t
*specific
= lsmash_create_codec_specific_data( type
, LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
);
2464 lsmash_free( esds_data
);
2467 specific
->data
.unstructured
= esds_data
;
2468 specific
->size
= esds_size
;
2469 /* Convert unstructured CODEC specific data format into structured, and append it to the opaque list. */
2470 lsmash_codec_specific_t
*conv
= lsmash_convert_codec_specific_format( specific
, LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
2471 lsmash_destroy_codec_specific_data( specific
);
2474 if( lsmash_add_entry( &opaque
->list
, conv
) )
2476 lsmash_destroy_codec_specific_data( conv
);
2482 lsmash_summary_t
*isom_create_audio_summary_from_description( isom_sample_entry_t
*sample_entry
)
2484 if( !sample_entry
|| !sample_entry
->file
|| !sample_entry
->parent
)
2486 isom_audio_entry_t
*audio
= (isom_audio_entry_t
*)sample_entry
;
2487 lsmash_audio_summary_t
*summary
= (lsmash_audio_summary_t
*)lsmash_create_summary( LSMASH_SUMMARY_TYPE_AUDIO
);
2490 summary
->sample_type
= audio
->type
;
2491 summary
->data_ref_index
= audio
->data_reference_index
;
2492 summary
->sample_size
= audio
->samplesize
;
2493 summary
->channels
= audio
->channelcount
;
2494 summary
->frequency
= audio
->samplerate
>> 16;
2495 if( ((isom_stsd_t
*)audio
->parent
)->version
== 0
2496 && audio
->file
->qt_compatible
2497 && isom_is_qt_audio( audio
->type
) )
2499 if( audio
->version
== 0 )
2500 isom_get_implicit_qt_fixed_comp_audio_sample_quants( audio
, &summary
->samples_in_frame
, &summary
->bytes_per_frame
, &summary
->sample_size
);
2501 else if( audio
->version
== 1 )
2503 summary
->channels
= audio
->bytesPerPacket
? audio
->bytesPerFrame
/ audio
->bytesPerPacket
: audio
->channelcount
;
2504 summary
->sample_size
= audio
->bytesPerPacket
* 8;
2505 summary
->samples_in_frame
= audio
->samplesPerPacket
;
2506 summary
->bytes_per_frame
= audio
->bytesPerFrame
;
2508 else if( audio
->version
== 2 )
2510 summary
->frequency
= (union {uint64_t i
; double d
;}){audio
->audioSampleRate
}.d
;
2511 summary
->channels
= audio
->numAudioChannels
;
2512 summary
->sample_size
= audio
->constBitsPerChannel
;
2513 summary
->samples_in_frame
= audio
->constLPCMFramesPerAudioPacket
;
2514 summary
->bytes_per_frame
= audio
->constBytesPerAudioPacket
;
2516 lsmash_codec_specific_t
*specific
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_COMMON
,
2517 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
2520 lsmash_qt_audio_common_t
*common
= (lsmash_qt_audio_common_t
*)specific
->data
.structured
;
2521 common
->revision_level
= audio
->revision_level
;
2522 common
->vendor
= audio
->vendor
;
2523 common
->compression_ID
= audio
->compression_ID
;
2524 if( lsmash_add_entry( &summary
->opaque
->list
, specific
) )
2526 lsmash_destroy_codec_specific_data( specific
);
2529 if( isom_is_lpcm_audio( audio
) )
2531 specific
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_FORMAT_SPECIFIC_FLAGS
,
2532 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
2535 lsmash_qt_audio_format_specific_flags_t
*data
= (lsmash_qt_audio_format_specific_flags_t
*)specific
->data
.structured
;
2536 if( audio
->version
== 2 )
2537 data
->format_flags
= audio
->formatSpecificFlags
;
2540 data
->format_flags
= 0;
2541 /* Here, don't override samplesize.
2542 * We should trust samplesize field in the description for misused CODEC indentifier. */
2543 lsmash_codec_type_t audio_type
= audio
->type
;
2544 if( lsmash_check_codec_type_identical( audio_type
, QT_CODEC_TYPE_FL32_AUDIO
)
2545 || lsmash_check_codec_type_identical( audio_type
, QT_CODEC_TYPE_FL64_AUDIO
) )
2546 data
->format_flags
= QT_LPCM_FORMAT_FLAG_FLOAT
;
2547 else if( lsmash_check_codec_type_identical( audio_type
, QT_CODEC_TYPE_TWOS_AUDIO
)
2548 || lsmash_check_codec_type_identical( audio_type
, QT_CODEC_TYPE_NONE_AUDIO
)
2549 || lsmash_check_codec_type_identical( audio_type
, QT_CODEC_TYPE_NOT_SPECIFIED
) )
2551 if( lsmash_check_codec_type_identical( audio_type
, QT_CODEC_TYPE_TWOS_AUDIO
) )
2552 data
->format_flags
= QT_LPCM_FORMAT_FLAG_BIG_ENDIAN
| QT_AUDIO_FORMAT_FLAG_SIGNED_INTEGER
;
2553 if( summary
->sample_size
> 8 )
2554 data
->format_flags
= QT_LPCM_FORMAT_FLAG_BIG_ENDIAN
;
2557 isom_wave_t
*wave
= (isom_wave_t
*)isom_get_extension_box_format( &audio
->extensions
, QT_BOX_TYPE_WAVE
);
2558 if( wave
&& wave
->enda
&& !wave
->enda
->littleEndian
)
2559 data
->format_flags
|= QT_LPCM_FORMAT_FLAG_BIG_ENDIAN
;
2560 if( lsmash_add_entry( &summary
->opaque
->list
, specific
) )
2562 lsmash_destroy_codec_specific_data( specific
);
2566 else if( audio
->version
== 2
2567 && (lsmash_check_codec_type_identical( audio
->type
, ISOM_CODEC_TYPE_ALAC_AUDIO
)
2568 || lsmash_check_codec_type_identical( audio
->type
, QT_CODEC_TYPE_ALAC_AUDIO
)) )
2569 switch( audio
->formatSpecificFlags
)
2571 case QT_ALAC_FORMAT_FLAG_16BIT_SOURCE_DATA
:
2572 summary
->sample_size
= 16;
2574 case QT_ALAC_FORMAT_FLAG_20BIT_SOURCE_DATA
:
2575 summary
->sample_size
= 20;
2577 case QT_ALAC_FORMAT_FLAG_24BIT_SOURCE_DATA
:
2578 summary
->sample_size
= 24;
2580 case QT_ALAC_FORMAT_FLAG_32BIT_SOURCE_DATA
:
2581 summary
->sample_size
= 32;
2587 else if( lsmash_check_codec_type_identical( audio
->type
, ISOM_CODEC_TYPE_SAMR_AUDIO
) )
2589 summary
->channels
= 1;
2590 summary
->frequency
= 8000;
2592 else if( lsmash_check_codec_type_identical( audio
->type
, ISOM_CODEC_TYPE_SAWB_AUDIO
) )
2594 summary
->channels
= 1;
2595 summary
->frequency
= 16000;
2597 uint32_t actual_sampling_rate
= 0;
2598 for( lsmash_entry_t
*entry
= audio
->extensions
.head
; entry
; entry
= entry
->next
)
2600 isom_box_t
*box
= (isom_box_t
*)entry
->data
;
2603 if( !(box
->manager
& LSMASH_BINARY_CODED_BOX
) )
2605 if( lsmash_check_box_type_identical( box
->type
, QT_BOX_TYPE_CHAN
) )
2607 lsmash_codec_specific_t
*specific
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_CHANNEL_LAYOUT
,
2608 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
2611 isom_chan_t
*chan
= (isom_chan_t
*)box
;
2612 lsmash_qt_audio_channel_layout_t
*data
= (lsmash_qt_audio_channel_layout_t
*)specific
->data
.structured
;
2613 data
->channelLayoutTag
= chan
->channelLayoutTag
;
2614 data
->channelBitmap
= chan
->channelBitmap
;
2615 if( lsmash_add_entry( &summary
->opaque
->list
, specific
) )
2617 lsmash_destroy_codec_specific_data( specific
);
2621 else if( lsmash_check_box_type_identical( box
->type
, ISOM_BOX_TYPE_ESDS
)
2622 || lsmash_check_box_type_identical( box
->type
, QT_BOX_TYPE_ESDS
) )
2624 isom_esds_t
*esds
= (isom_esds_t
*)box
;
2625 if( mp4sys_setup_summary_from_DecoderSpecificInfo( summary
, esds
->ES
)
2626 || isom_append_structured_mp4sys_decoder_config( summary
->opaque
, esds
) )
2629 else if( lsmash_check_box_type_identical( box
->type
, ISOM_BOX_TYPE_SRAT
) )
2631 isom_srat_t
*srat
= (isom_srat_t
*)box
;
2632 actual_sampling_rate
= srat
->sampling_rate
;
2634 else if( lsmash_check_box_type_identical( box
->type
, QT_BOX_TYPE_WAVE
) )
2636 /* Don't append 'wave' extension itself to the opaque CODEC specific info list. */
2637 isom_wave_t
*wave
= (isom_wave_t
*)box
;
2638 lsmash_bs_t
*bs
= lsmash_bs_create();
2641 for( lsmash_entry_t
*wave_entry
= wave
->extensions
.head
; wave_entry
; wave_entry
= wave_entry
->next
)
2643 isom_box_t
*wave_ext
= (isom_box_t
*)wave_entry
->data
;
2646 lsmash_box_type_t box_type
= LSMASH_BOX_TYPE_INITIALIZER
;
2647 if( !(wave_ext
->manager
& LSMASH_BINARY_CODED_BOX
) )
2649 box_type
= wave_ext
->type
;
2650 if( lsmash_check_box_type_identical( box_type
, QT_BOX_TYPE_ENDA
) )
2652 isom_enda_t
*enda
= (isom_enda_t
*)wave_ext
;
2653 isom_bs_put_box_common( bs
, enda
);
2654 lsmash_bs_put_be16( bs
, enda
->littleEndian
);
2656 else if( lsmash_check_box_type_identical( box_type
, QT_BOX_TYPE_MP4A
) )
2658 isom_mp4a_t
*mp4a
= (isom_mp4a_t
*)wave_ext
;
2659 isom_bs_put_box_common( bs
, mp4a
);
2660 lsmash_bs_put_be32( bs
, mp4a
->unknown
);
2662 else if( lsmash_check_box_type_identical( box_type
, QT_BOX_TYPE_CHAN
) )
2664 isom_chan_t
*chan
= (isom_chan_t
*)wave_ext
;
2665 isom_bs_put_box_common( bs
, chan
);
2666 lsmash_bs_put_be32( bs
, chan
->channelLayoutTag
);
2667 lsmash_bs_put_be32( bs
, chan
->channelBitmap
);
2668 lsmash_bs_put_be32( bs
, chan
->numberChannelDescriptions
);
2669 if( chan
->channelDescriptions
)
2670 for( uint32_t i
= 0; i
< chan
->numberChannelDescriptions
; i
++ )
2672 isom_channel_description_t
*channelDescriptions
= (isom_channel_description_t
*)(&chan
->channelDescriptions
[i
]);
2673 if( !channelDescriptions
)
2675 lsmash_bs_cleanup( bs
);
2678 lsmash_bs_put_be32( bs
, channelDescriptions
->channelLabel
);
2679 lsmash_bs_put_be32( bs
, channelDescriptions
->channelFlags
);
2680 lsmash_bs_put_be32( bs
, channelDescriptions
->coordinates
[0] );
2681 lsmash_bs_put_be32( bs
, channelDescriptions
->coordinates
[1] );
2682 lsmash_bs_put_be32( bs
, channelDescriptions
->coordinates
[2] );
2685 else if( lsmash_check_box_type_identical( box_type
, QT_BOX_TYPE_ESDS
) )
2687 isom_esds_t
*esds
= (isom_esds_t
*)wave_ext
;
2689 || mp4sys_setup_summary_from_DecoderSpecificInfo( summary
, esds
->ES
)
2690 || isom_append_structured_mp4sys_decoder_config( summary
->opaque
, esds
) )
2692 lsmash_bs_cleanup( bs
);
2698 /* Skip Format Box and Terminator Box since they are mandatory and fixed structure. */
2703 if( wave_ext
->size
< ISOM_BASEBOX_COMMON_SIZE
)
2705 uint8_t *data
= wave_ext
->binary
;
2706 box_type
.fourcc
= LSMASH_4CC( data
[4], data
[5], data
[6], data
[7] );
2707 lsmash_bs_put_bytes( bs
, wave_ext
->size
, wave_ext
->binary
);
2709 /* Export as binary string. */
2711 uint8_t *box_data
= lsmash_bs_export_data( bs
, &box_size
);
2712 lsmash_bs_empty( bs
);
2715 lsmash_bs_cleanup( bs
);
2718 /* Append as an unstructured CODEC specific info. */
2719 lsmash_codec_specific_data_type type
;
2720 if( box_type
.fourcc
== QT_BOX_TYPE_CHAN
.fourcc
)
2721 /* Complete audio channel layout is stored as binary string.
2722 * We distinguish it from one of the outside of 'wave' extension here. */
2723 type
= LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_DECOMPRESSION_PARAMETERS
;
2726 type
= isom_get_codec_specific_data_type( box_type
.fourcc
);
2727 if( type
== LSMASH_CODEC_SPECIFIC_DATA_TYPE_UNKNOWN
)
2728 type
= LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_DECOMPRESSION_PARAMETERS
;
2730 lsmash_codec_specific_t
*specific
= lsmash_create_codec_specific_data( type
, LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
);
2733 lsmash_bs_cleanup( bs
);
2736 specific
->data
.unstructured
= box_data
;
2737 specific
->size
= box_size
;
2738 if( lsmash_add_entry( &summary
->opaque
->list
, specific
) )
2740 lsmash_destroy_codec_specific_data( specific
);
2741 lsmash_bs_cleanup( bs
);
2745 lsmash_bs_cleanup( bs
);
2750 if( box
->size
< ISOM_BASEBOX_COMMON_SIZE
)
2752 uint8_t *data
= box
->binary
;
2753 lsmash_compact_box_type_t fourcc
= LSMASH_4CC( data
[4], data
[5], data
[6], data
[7] );
2754 lsmash_codec_specific_data_type type
= isom_get_codec_specific_data_type( fourcc
);
2755 lsmash_codec_specific_t
*specific
= lsmash_create_codec_specific_data( type
, LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
);
2758 specific
->size
= box
->size
;
2759 specific
->data
.unstructured
= lsmash_memdup( box
->binary
, box
->size
);
2760 if( !specific
->data
.unstructured
2761 || lsmash_add_entry( &summary
->opaque
->list
, specific
) )
2763 lsmash_destroy_codec_specific_data( specific
);
2766 if( specific
->type
== LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_DTS
2767 || specific
->type
== LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_AC_3
2768 || specific
->type
== LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_EC_3
)
2770 specific
= lsmash_convert_codec_specific_format( specific
, LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
2773 switch( specific
->type
)
2775 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_DTS
:
2777 lsmash_dts_specific_parameters_t
*param
= (lsmash_dts_specific_parameters_t
*)specific
->data
.structured
;
2778 summary
->sample_size
= param
->pcmSampleDepth
;
2779 summary
->samples_in_frame
= (summary
->frequency
* (512 << param
->FrameDuration
)) / param
->DTSSamplingFrequency
;
2782 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_AC_3
:
2784 lsmash_ac3_specific_parameters_t
*param
= (lsmash_ac3_specific_parameters_t
*)specific
->data
.structured
;
2785 summary
->frequency
= ac3_get_sample_rate( param
);
2786 summary
->channels
= ac3_get_channel_count( param
);
2787 summary
->samples_in_frame
= 1536;
2790 case LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_EC_3
:
2792 lsmash_eac3_specific_parameters_t
*param
= (lsmash_eac3_specific_parameters_t
*)specific
->data
.structured
;
2793 eac3_update_sample_rate( &summary
->frequency
, param
, NULL
);
2794 eac3_update_channel_count( &summary
->channels
, param
);
2795 summary
->samples_in_frame
= 1536;
2801 lsmash_destroy_codec_specific_data( specific
);
2805 /* Set the actual sampling rate. */
2806 if( actual_sampling_rate
)
2807 summary
->frequency
= actual_sampling_rate
;
2808 return (lsmash_summary_t
*)summary
;
2810 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
2814 lsmash_codec_specific_t
*lsmash_get_codec_specific_data( lsmash_summary_t
*summary
, uint32_t extension_number
)
2816 if( !summary
|| !summary
->opaque
)
2819 for( lsmash_entry_t
*entry
= summary
->opaque
->list
.head
; entry
; entry
= entry
->next
)
2820 if( ++i
== extension_number
)
2821 return (lsmash_codec_specific_t
*)entry
->data
;
2825 uint32_t lsmash_count_codec_specific_data( lsmash_summary_t
*summary
)
2827 if( !summary
|| !summary
->opaque
)
2829 return summary
->opaque
->list
.entry_count
;
2832 int isom_compare_opaque_extensions( lsmash_summary_t
*a
, lsmash_summary_t
*b
)
2835 uint32_t in_number_of_extensions
= lsmash_count_codec_specific_data( a
);
2836 uint32_t out_number_of_extensions
= lsmash_count_codec_specific_data( b
);
2837 if( out_number_of_extensions
!= in_number_of_extensions
)
2839 uint32_t active_number_of_extensions
= in_number_of_extensions
;
2840 uint32_t identical_count
= 0;
2841 for( uint32_t j
= 1; j
<= in_number_of_extensions
; j
++ )
2843 lsmash_codec_specific_t
*in_cs_orig
= lsmash_get_codec_specific_data( a
, j
);
2844 lsmash_codec_specific_t
*in_cs
;
2845 lsmash_codec_specific_format compare_format
= LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
;
2846 if( in_cs_orig
->format
== LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
)
2848 if( in_cs_orig
->type
== LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_COMMON
2849 || in_cs_orig
->type
== LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_COMMON
2850 || in_cs_orig
->type
== LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_FORMAT_SPECIFIC_FLAGS
)
2852 compare_format
= LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
;
2857 in_cs
= lsmash_convert_codec_specific_format( in_cs_orig
, LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
);
2860 /* We don't support the format converter of this data type. */
2861 --active_number_of_extensions
;
2868 for( uint32_t k
= 1; k
<= out_number_of_extensions
; k
++ )
2870 lsmash_codec_specific_t
*out_cs_orig
= lsmash_get_codec_specific_data( b
, k
);
2871 if( out_cs_orig
->type
!= in_cs_orig
->type
)
2873 lsmash_codec_specific_t
*out_cs
;
2874 if( out_cs_orig
->format
== LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
)
2876 if( compare_format
== LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
)
2877 out_cs
= out_cs_orig
;
2880 out_cs
= lsmash_convert_codec_specific_format( out_cs_orig
, LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
);
2886 out_cs
= out_cs_orig
;
2888 if( compare_format
== LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
)
2889 identical
= out_cs
->size
== in_cs
->size
&& !memcmp( out_cs
->data
.unstructured
, in_cs
->data
.unstructured
, in_cs
->size
);
2892 if( in_cs
->type
== LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_VIDEO_COMMON
)
2894 lsmash_qt_video_common_t
*in_data
= (lsmash_qt_video_common_t
*)in_cs
->data
.structured
;
2895 lsmash_qt_video_common_t
*out_data
= (lsmash_qt_video_common_t
*)out_cs
->data
.structured
;
2896 identical
= in_data
->revision_level
== out_data
->revision_level
2897 && in_data
->vendor
== out_data
->vendor
2898 && in_data
->temporalQuality
== out_data
->temporalQuality
2899 && in_data
->spatialQuality
== out_data
->spatialQuality
2900 && in_data
->horizontal_resolution
== out_data
->horizontal_resolution
2901 && in_data
->vertical_resolution
== out_data
->vertical_resolution
2902 && in_data
->dataSize
== out_data
->dataSize
2903 && in_data
->frame_count
== out_data
->frame_count
2904 && in_data
->color_table_ID
== out_data
->color_table_ID
;
2906 else if( in_cs
->type
== LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_COMMON
)
2908 lsmash_qt_audio_common_t
*in_data
= (lsmash_qt_audio_common_t
*)in_cs
->data
.structured
;
2909 lsmash_qt_audio_common_t
*out_data
= (lsmash_qt_audio_common_t
*)out_cs
->data
.structured
;
2910 identical
= in_data
->revision_level
== out_data
->revision_level
2911 && in_data
->vendor
== out_data
->vendor
2912 && in_data
->compression_ID
== out_data
->compression_ID
;
2916 lsmash_qt_audio_format_specific_flags_t
*in_data
= (lsmash_qt_audio_format_specific_flags_t
*)in_cs
->data
.structured
;
2917 lsmash_qt_audio_format_specific_flags_t
*out_data
= (lsmash_qt_audio_format_specific_flags_t
*)out_cs
->data
.structured
;
2918 identical
= (in_data
->format_flags
== out_data
->format_flags
);
2921 if( out_cs
!= out_cs_orig
)
2922 lsmash_destroy_codec_specific_data( out_cs
);
2929 if( in_cs
!= in_cs_orig
)
2930 lsmash_destroy_codec_specific_data( in_cs
);
2932 return (identical_count
!= active_number_of_extensions
);
2935 int isom_get_implicit_qt_fixed_comp_audio_sample_quants
2937 isom_audio_entry_t
*audio
,
2938 uint32_t *samples_per_packet
,
2939 uint32_t *constant_bytes_per_frame
,
2940 uint32_t *sample_size
2943 if( lsmash_check_codec_type_identical( audio
->type
, QT_CODEC_TYPE_MAC3_AUDIO
) )
2945 *samples_per_packet
= 6;
2946 *constant_bytes_per_frame
= 2 * audio
->channelcount
;
2949 else if( lsmash_check_codec_type_identical( audio
->type
, QT_CODEC_TYPE_MAC6_AUDIO
) )
2951 *samples_per_packet
= 6;
2952 *constant_bytes_per_frame
= audio
->channelcount
;
2955 else if( lsmash_check_codec_type_identical( audio
->type
, QT_CODEC_TYPE_ADPCM17_AUDIO
) )
2957 *samples_per_packet
= 64;
2958 *constant_bytes_per_frame
= 34 * audio
->channelcount
;
2961 else if( lsmash_check_codec_type_identical( audio
->type
, QT_CODEC_TYPE_AGSM_AUDIO
) )
2963 *samples_per_packet
= 160;
2964 *constant_bytes_per_frame
= 33;
2967 else if( lsmash_check_codec_type_identical( audio
->type
, QT_CODEC_TYPE_ALAW_AUDIO
)
2968 || lsmash_check_codec_type_identical( audio
->type
, QT_CODEC_TYPE_ULAW_AUDIO
) )
2970 *samples_per_packet
= 1;
2971 *constant_bytes_per_frame
= audio
->channelcount
;