box: Add member offset in parent box struct.
[L-SMASH.git] / codecs / qt_wfex.c
blobc41ca078bc369a834750cdff4a1aa93246101e30
1 /*****************************************************************************
2 * qt_wfex.c
3 *****************************************************************************
4 * Copyright (C) 2014-2017 L-SMASH project
6 * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 *****************************************************************************/
21 /* This file is available under an ISC license. */
23 #include "common/internal.h" /* must be placed first */
25 #include "core/box.h"
27 /*********************************************************************************
28 QuickTime Waveform Audio
29 **********************************************************************************/
31 int waveform_audio_update_bitrate( isom_stbl_t *stbl, isom_mdhd_t *mdhd, uint32_t sample_description_index )
33 isom_sample_entry_t *sample_entry = (isom_sample_entry_t *)lsmash_get_entry_data( &stbl->stsd->list, sample_description_index );
34 if( LSMASH_IS_NON_EXISTING_BOX( sample_entry ) )
35 return LSMASH_ERR_INVALID_DATA;
36 isom_box_t *ext = isom_get_extension_box( &sample_entry->extensions, QT_BOX_TYPE_WAVE );
37 if( LSMASH_IS_NON_EXISTING_BOX( ext ) )
38 return LSMASH_ERR_INVALID_DATA;
39 uint8_t *exdata = NULL;
40 uint32_t exdata_size = 0;
41 if( ext->manager & LSMASH_BINARY_CODED_BOX )
42 exdata = isom_get_child_box_position( ext->binary, ext->size, sample_entry->type, &exdata_size );
43 else
45 isom_wave_t *wave = (isom_wave_t *)ext;
46 isom_box_t *wave_ext = isom_get_extension_box( &wave->extensions, sample_entry->type );
47 if( !(wave_ext->manager & LSMASH_BINARY_CODED_BOX) )
48 return LSMASH_ERR_INVALID_DATA;
49 exdata = wave_ext->binary;
50 exdata_size = wave_ext->size;
52 /* Check whether exdata is valid or not. */
53 if( !exdata || exdata_size < ISOM_BASEBOX_COMMON_SIZE + 18 )
54 return LSMASH_ERR_INVALID_DATA;
55 exdata += ISOM_BASEBOX_COMMON_SIZE;
56 uint16_t cbSize = LSMASH_GET_LE16( &exdata[16] );
57 if( exdata_size < ISOM_BASEBOX_COMMON_SIZE + 18 + cbSize )
58 return LSMASH_ERR_INVALID_DATA;
59 /* WAVEFORMATEX.nAvgBytesPerSec */
60 uint32_t bufferSizeDB;
61 uint32_t maxBitrate;
62 uint32_t avgBitrate;
63 int err = isom_calculate_bitrate_description( stbl, mdhd, &bufferSizeDB, &maxBitrate, &avgBitrate, sample_description_index );
64 if( err < 0 )
65 return err;
66 uint32_t nAvgBytesPerSec = avgBitrate / 8;
67 LSMASH_SET_LE32( &exdata[8], nAvgBytesPerSec );
68 if( lsmash_check_codec_type_identical( sample_entry->type, QT_CODEC_TYPE_FULLMP3_AUDIO )
69 || lsmash_check_codec_type_identical( sample_entry->type, QT_CODEC_TYPE_MP3_AUDIO ) )
71 /* MPEGLAYER3WAVEFORMAT.nBlockSize */
72 uint32_t nSamplesPerSec = LSMASH_GET_LE32( &exdata[ 4] );
73 uint16_t nFramesPerBlock = LSMASH_GET_LE16( &exdata[26] );
74 uint16_t padding = 0; /* FIXME? */
75 uint16_t nBlockSize = (144 * (avgBitrate / nSamplesPerSec) + padding) * nFramesPerBlock;
76 LSMASH_SET_LE16( &exdata[24], nBlockSize );
78 return 0;