Downsample audio to 48000 Khz if the input has a bigger sample rate.
This commit is contained in:
1 parent
bbc16bce68
commit
04c20bbc67
6 files changed
+62
-12
No files matched your search
@@ -26,7 +26,8 @@ extern NSString * const SBDolbyPlIIMixdown;
|
||||
- (void)addSample:(MP42SampleBuffer *)sample;
|
||||
- (nullable MP42SampleBuffer *)copyEncodedSample;
|
||||
|
||||
- (NSData *)magicCookie;
|
||||
@property (nonatomic, readonly) NSData *magicCookie;
|
||||
@property (nonatomic, readonly) double sampleRate;
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
||||
|
||||
@@ -97,6 +97,14 @@
|
||||
return _encoder.magicCookie;
|
||||
}
|
||||
|
||||
- (double)sampleRate {
|
||||
double sampleRate = self.decoder.outputFormat.mSampleRate;
|
||||
if (sampleRate > 48000) {
|
||||
return 48000;
|
||||
}
|
||||
return sampleRate;
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[_decoder release];
|
||||
[_encoder release];
|
||||
|
||||
+13
-3
@@ -269,13 +269,20 @@ static void configureDescriptors(MP42DecodeContext *context, AVFrame *frame)
|
||||
{
|
||||
// Get real channels number and layout
|
||||
int nb_channels = av_get_channel_layout_nb_channels(frame->channel_layout);
|
||||
int sample_rate = frame->sample_rate;
|
||||
|
||||
// Reset the channels per frame
|
||||
// Reset the channels per frame and sample rate
|
||||
if (context->inputFormat->mChannelsPerFrame == context->outputFormat->mChannelsPerFrame) {
|
||||
context->outputFormat->mChannelsPerFrame = nb_channels;
|
||||
context->inputFormat->mChannelsPerFrame = nb_channels;
|
||||
}
|
||||
context->outputFormat->mChannelsPerFrame = context->outputFormat->mChannelsPerFrame;
|
||||
context->inputFormat->mSampleRate = sample_rate;
|
||||
|
||||
if (sample_rate > 48000) {
|
||||
sample_rate = 48000;
|
||||
}
|
||||
|
||||
context->outputFormat->mSampleRate = sample_rate;
|
||||
context->outputFormat->mBytesPerPacket = 4 * context->outputFormat->mChannelsPerFrame;
|
||||
context->outputFormat->mBytesPerFrame = context->outputFormat->mBytesPerPacket * context->outputFormat->mFramesPerPacket;
|
||||
|
||||
@@ -313,6 +320,7 @@ static int resample(MP42DecodeContext *context, AVFrame *frame, uint8_t **output
|
||||
context->resampler = hb_audio_resample_init(AV_SAMPLE_FMT_FLT,
|
||||
context->out_layout,
|
||||
context->matrix_encoding,
|
||||
context->outputFormat->mSampleRate,
|
||||
0);
|
||||
}
|
||||
|
||||
@@ -327,7 +335,7 @@ static int resample(MP42DecodeContext *context, AVFrame *frame, uint8_t **output
|
||||
double surround_mix_level, center_mix_level;
|
||||
AVDownmixInfo * downmix_info;
|
||||
|
||||
downmix_info = (AVDownmixInfo*)side_data->data;
|
||||
downmix_info = (AVDownmixInfo *)side_data->data;
|
||||
if (context->matrix_encoding == AV_MATRIX_ENCODING_DOLBY ||
|
||||
context->matrix_encoding == AV_MATRIX_ENCODING_DPLII)
|
||||
{
|
||||
@@ -346,6 +354,8 @@ static int resample(MP42DecodeContext *context, AVFrame *frame, uint8_t **output
|
||||
}
|
||||
hb_audio_resample_set_channel_layout(context->resampler,
|
||||
frame->channel_layout);
|
||||
hb_audio_resample_set_sample_rate(context->resampler,
|
||||
frame->sample_rate);
|
||||
hb_audio_resample_set_sample_fmt(context->resampler,
|
||||
frame->format);
|
||||
if (hb_audio_resample_update(context->resampler))
|
||||
|
||||
+7
-3
@@ -113,6 +113,9 @@
|
||||
}
|
||||
|
||||
helper->converter = audioConverter;
|
||||
// The audio converter might downsample the audio,
|
||||
// so update the track timescale here.
|
||||
timeScale = audioConverter.sampleRate;
|
||||
}
|
||||
if ([track isMemberOfClass:[MP42SubtitleTrack class]] && (track.sourceFormat == kMP42SubtitleCodecType_VobSub || track.sourceFormat == kMP42SubtitleCodecType_PGS) && track.needConversion) {
|
||||
MP42BitmapSubConverter *subConverter = [[MP42BitmapSubConverter alloc] initWithTrack:(MP42SubtitleTrack *)track
|
||||
@@ -248,14 +251,15 @@
|
||||
// AAC audio track
|
||||
else if ([track isMemberOfClass:[MP42AudioTrack class]] &&
|
||||
(track.format == kMP42AudioCodecType_MPEG4AAC || track.format == kMP42AudioCodecType_MPEG4AAC_HE)) {
|
||||
|
||||
dstTrackId = MP4AddAudioTrack(_fileHandle,
|
||||
timeScale,
|
||||
1024, MP4_MPEG4_AUDIO_TYPE);
|
||||
|
||||
if (!track.needConversion && [magicCookie length]) {
|
||||
if (!track.needConversion && magicCookie.length) {
|
||||
MP4SetTrackESConfiguration(_fileHandle, dstTrackId,
|
||||
[magicCookie bytes],
|
||||
[magicCookie length]);
|
||||
magicCookie.bytes,
|
||||
magicCookie.length);
|
||||
}
|
||||
|
||||
[helper->importer setActiveTrack:track];
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
|
||||
hb_audio_resample_t* hb_audio_resample_init(enum AVSampleFormat sample_fmt,
|
||||
uint64_t channel_layout, int matrix_encoding,
|
||||
int normalize_mix)
|
||||
double sample_rate, int normalize_mix)
|
||||
{
|
||||
hb_audio_resample_t *resample = calloc(1, sizeof(hb_audio_resample_t));
|
||||
if (resample == NULL)
|
||||
@@ -60,6 +60,7 @@ hb_audio_resample_t* hb_audio_resample_init(enum AVSampleFormat sample_fmt,
|
||||
// requested output channel_layout, sample_fmt
|
||||
resample->out.channels = av_get_channel_layout_nb_channels(channel_layout);
|
||||
resample->out.channel_layout = channel_layout;
|
||||
resample->out.sample_rate = sample_rate;
|
||||
resample->out.matrix_encoding = matrix_encoding;
|
||||
resample->out.normalize_mix_level = normalize_mix;
|
||||
resample->out.sample_fmt = sample_fmt;
|
||||
@@ -68,6 +69,7 @@ hb_audio_resample_t* hb_audio_resample_init(enum AVSampleFormat sample_fmt,
|
||||
// set default input characteristics
|
||||
resample->in.sample_fmt = resample->out.sample_fmt;
|
||||
resample->in.channel_layout = resample->out.channel_layout;
|
||||
resample->in.sample_rate = resample->out.sample_rate;
|
||||
resample->in.lfe_mix_level = HB_MIXLEV_ZERO;
|
||||
resample->in.center_mix_level = HB_MIXLEV_DEFAULT;
|
||||
resample->in.surround_mix_level = HB_MIXLEV_DEFAULT;
|
||||
@@ -95,6 +97,15 @@ void hb_audio_resample_set_channel_layout(hb_audio_resample_t *resample,
|
||||
}
|
||||
}
|
||||
|
||||
void hb_audio_resample_set_sample_rate(hb_audio_resample_t *resample,
|
||||
double sample_rate)
|
||||
{
|
||||
if (resample != NULL)
|
||||
{
|
||||
resample->in.sample_rate = sample_rate;
|
||||
}
|
||||
}
|
||||
|
||||
void hb_audio_resample_set_mix_levels(hb_audio_resample_t *resample,
|
||||
double surround_mix_level,
|
||||
double center_mix_level,
|
||||
@@ -135,6 +146,7 @@ int hb_audio_resample_update(hb_audio_resample_t *resample)
|
||||
(resample->resample_needed &&
|
||||
(resample->resample.sample_fmt != resample->in.sample_fmt ||
|
||||
resample->resample.channel_layout != resample->in.channel_layout ||
|
||||
resample->resample.sample_rate != resample->in.sample_rate ||
|
||||
resample->resample.lfe_mix_level != resample->in.lfe_mix_level ||
|
||||
resample->resample.center_mix_level != resample->in.center_mix_level ||
|
||||
resample->resample.surround_mix_level != resample->in.surround_mix_level));
|
||||
@@ -155,6 +167,8 @@ int hb_audio_resample_update(hb_audio_resample_t *resample)
|
||||
resample->out.sample_fmt, 0);
|
||||
av_opt_set_int(resample->avresample, "out_channel_layout",
|
||||
resample->out.channel_layout, 0);
|
||||
av_opt_set_int(resample->avresample, "out_sample_rate",
|
||||
resample->out.sample_rate, 0);
|
||||
av_opt_set_int(resample->avresample, "matrix_encoding",
|
||||
resample->out.matrix_encoding, 0);
|
||||
av_opt_set_int(resample->avresample, "normalize_mix_level",
|
||||
@@ -169,6 +183,8 @@ int hb_audio_resample_update(hb_audio_resample_t *resample)
|
||||
resample->in.sample_fmt, 0);
|
||||
av_opt_set_int(resample->avresample, "in_channel_layout",
|
||||
resample->in.channel_layout, 0);
|
||||
av_opt_set_int(resample->avresample, "in_sample_rate",
|
||||
resample->in.sample_rate, 0);
|
||||
av_opt_set_double(resample->avresample, "lfe_mix_level",
|
||||
resample->in.lfe_mix_level, 0);
|
||||
av_opt_set_double(resample->avresample, "center_mix_level",
|
||||
@@ -191,6 +207,7 @@ int hb_audio_resample_update(hb_audio_resample_t *resample)
|
||||
resample->resample.channel_layout = resample->in.channel_layout;
|
||||
resample->resample.channels =
|
||||
av_get_channel_layout_nb_channels(resample->in.channel_layout);
|
||||
resample->resample.sample_rate = resample->in.sample_rate;
|
||||
resample->resample.lfe_mix_level = resample->in.lfe_mix_level;
|
||||
resample->resample.center_mix_level = resample->in.center_mix_level;
|
||||
resample->resample.surround_mix_level = resample->in.surround_mix_level;
|
||||
@@ -237,14 +254,18 @@ int hb_audio_resample(hb_audio_resample_t *resample,
|
||||
av_samples_get_buffer_size(&in_linesize,
|
||||
resample->resample.channels, nsamples,
|
||||
resample->resample.sample_fmt, 0);
|
||||
int out_samples = avresample_available(resample->avresample) +
|
||||
av_rescale_rnd(avresample_get_delay(resample->avresample) +
|
||||
nsamples, resample->out.sample_rate, resample->in.sample_rate, AV_ROUND_UP);
|
||||
|
||||
out_size = av_samples_get_buffer_size(&out_linesize,
|
||||
resample->out.channels, nsamples,
|
||||
resample->out.channels, out_samples,
|
||||
resample->out.sample_fmt, 0);
|
||||
out = malloc(out_size + AV_INPUT_BUFFER_PADDING_SIZE);
|
||||
|
||||
out_samples = avresample_convert(resample->avresample,
|
||||
&out, out_linesize, nsamples,
|
||||
samples, in_linesize, nsamples);
|
||||
&out, out_linesize, out_samples,
|
||||
samples, in_linesize, nsamples);
|
||||
|
||||
if (out_samples <= 0)
|
||||
{
|
||||
|
||||
@@ -33,6 +33,7 @@ typedef struct
|
||||
struct
|
||||
{
|
||||
uint64_t channel_layout;
|
||||
double sample_rate;
|
||||
double lfe_mix_level;
|
||||
double center_mix_level;
|
||||
double surround_mix_level;
|
||||
@@ -43,6 +44,7 @@ typedef struct
|
||||
{
|
||||
int channels;
|
||||
uint64_t channel_layout;
|
||||
double sample_rate;
|
||||
double lfe_mix_level;
|
||||
double center_mix_level;
|
||||
double surround_mix_level;
|
||||
@@ -55,6 +57,7 @@ typedef struct
|
||||
int sample_size;
|
||||
int normalize_mix_level;
|
||||
uint64_t channel_layout;
|
||||
double sample_rate;
|
||||
enum AVSampleFormat sample_fmt;
|
||||
enum AVMatrixEncoding matrix_encoding;
|
||||
} out;
|
||||
@@ -68,7 +71,7 @@ typedef struct
|
||||
*/
|
||||
hb_audio_resample_t* hb_audio_resample_init(enum AVSampleFormat sample_fmt,
|
||||
uint64_t channel_layout, int matrix_encoding,
|
||||
int normalize_mix);
|
||||
double sample_rate, int normalize_mix);
|
||||
|
||||
/* The following functions set the audio input characteristics.
|
||||
*
|
||||
@@ -79,6 +82,9 @@ hb_audio_resample_t* hb_audio_resample_init(enum AVSampleFormat sample_fmt,
|
||||
void hb_audio_resample_set_channel_layout(hb_audio_resample_t *resample,
|
||||
uint64_t channel_layout);
|
||||
|
||||
void hb_audio_resample_set_sample_rate(hb_audio_resample_t *resample,
|
||||
double sample_rate);
|
||||
|
||||
void hb_audio_resample_set_mix_levels(hb_audio_resample_t *resample,
|
||||
double surround_mix_level,
|
||||
double center_mix_level,
|
||||
|
||||
Reference in new issue
Block a user