[FFmpeg-user] Encode pcm file to aac
liukfei
liukfei at gmail.com
Tue Jan 7 09:51:04 CET 2014
Hi All,
I tried to encode a pcm file to aac file by using ffmpeg native aac encoder,
and I can be sure the pcm file sounds good(Without any noise). And after I
finished the process(pcm --> aac), the aac file I got has *a little noise*.
Can anyone tell me the reason? I am using FFmpeg release 2.0. and here is
the codes:
...
c->sample_fmt = AV_SAMPLE_FMT_FLTP;
c->bit_rate = 96000;
c->sample_rate = 44100;
c->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
c->channels = 1;
c->channel_layout = av_get_default_channel_layout(c->channels);
*Cause the pcm data is using the AV_SAMPLE_FMT_S16P format(I decode a mp3
file to pcm, so I know the format), so I have to convert it to the FLTP to
be suitable for the native aac encoder.*
ret = avcodec_open2(c, codec, NULL);
src_nb_samples = c->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE ?
10000 : c->frame_size;
ret = av_samples_alloc_array_and_samples(&src_samples_data,
&src_samples_linesize, c->channels, src_nb_samples,
AV_SAMPLE_FMT_S16P, 0);
max_dst_nb_samples = src_nb_samples;
*//I decode the mp3 file to pcm, so I know the sample format of the pcm*
if (pcm_file_sample_format != AV_SAMPLE_FMT_FLTP) {
swr_ctx = swr_alloc();
av_opt_set_int(swr_ctx, "in_channel_count", 1, 0);
av_opt_set_int(swr_ctx, "out_channel_count", 1, 0);
av_opt_set_int(swr_ctx, "in_channel_layout",
av_get_default_channel_layout(1), 0);
av_opt_set_int(swr_ctx, "out_channel_layout",
av_get_default_channel_layout(1), 0);
av_opt_set_int(swr_ctx, "in_sample_rate", c->sample_rate, 0);
av_opt_set_int(swr_ctx, "out_sample_rate", c->sample_rate, 0);
av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", AV_SAMPLE_FMT_S16P, 0);
av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", c->sample_fmt, 0);
/* initialize the resampling context */
if ((ret = swr_init(swr_ctx)) < 0) {
fprintf(stderr, "Failed to initialize the resampling context\n");
exit(1);
}
av_samples_alloc_array_and_samples(&dst_samples_data,
&dst_samples_linesize, c->channels, max_dst_nb_samples,
c->sample_fmt, 0);
} else {
dst_samples_data = src_samples_data;
}
dst_samples_size = av_samples_get_buffer_size(NULL, c->channels,
max_dst_nb_samples, c->sample_fmt, 0);
*After the convert job, then I start to write the audio frame(it will be
executed for several times, until the global dest datas has no children).*
AVCodecContext *c;
AVPacket pkt = { 0 }; // data and size must be 0;
AVFrame *frame = av_frame_alloc();
int got_packet, ret, dst_nb_samples;
av_init_packet(&pkt);
c = st->codec;
unsigned char *data = gDestDatas[vIndex++];
src_samples_data[0] = data;
if (swr_ctx) {
/* compute destination number of samples */
dst_nb_samples = av_rescale_rnd(
swr_get_delay(swr_ctx, c->sample_rate) + src_nb_samples,
c->sample_rate, c->sample_rate, AV_ROUND_UP);
if (dst_nb_samples > max_dst_nb_samples) {
av_free(dst_samples_data[0]);
ret = av_samples_alloc(dst_samples_data, &dst_samples_linesize,
c->channels, dst_nb_samples, c->sample_fmt, 0);
max_dst_nb_samples = dst_nb_samples;
dst_samples_size = av_samples_get_buffer_size(NULL, c->channels,
dst_nb_samples, c->sample_fmt, 0);
}
/* convert to destination format */
ret = swr_convert(swr_ctx, dst_samples_data, dst_nb_samples,
(const uint8_t **) src_samples_data, src_nb_samples);
} else {
dst_nb_samples = src_nb_samples;
}
frame->nb_samples = dst_nb_samples;
avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
dst_samples_data[0], dst_samples_size, 0);
ret = avcodec_encode_audio2(c, &pkt, frame, &got_packet);
pkt.stream_index = st->index;
ret = av_interleaved_write_frame(oc, &pkt);
That is all codes, I am not sure if it is the native aac encoder issue, but
I can not compile the libfaac with ffmpeg, so if anyone can figure out the
reason(noise), thanks very much!
--
View this message in context: http://ffmpeg-users.933282.n4.nabble.com/Encode-pcm-file-to-aac-tp4663106.html
Sent from the FFmpeg-users mailing list archive at Nabble.com.
More information about the ffmpeg-user
mailing list