[Libav-user] Problem converting from AV_SAMPLE_FMT_S16 to AV_SAMPLE_FMT_FLTP

Adev Dev androiddevmar11 at gmail.com
Tue Jan 5 16:37:57 CET 2016


My application creates AAC files. The input is feed with frames in
AV_SAMPLE_FMT_S16 format. I am doing conversion to AV_SAMPLE_FMT_FLTP and
encoding these frames using avcodec_encode_audio2 function.

The problem is that function "avcodec_encode_audio2" crashes without error.
When I feed input with frames with AV_SAMPLE_FMT_FLTP, conversion is
working and encoding is correct as well.

I assume that there is something wrong with frames conversion. Do you have
any ideas what is wrong? Thanks. Below there is function I use to convert
samples:

static AVFrame *convert_frame_format(AVFrame *frame) {

    int src_nb_channels = input_codec_context->channels;
    long long int src_channel_layout =
av_get_default_channel_layout(src_nb_channels);

    if (1 || (input_codec_context->sample_fmt !=
output_codec_context->sample_fmt) ||
        (frame->channel_layout != output_codec_context->channel_layout)){

        SwrContext *swr = swr_alloc();
        av_opt_set_int(swr, "in_channel_layout", src_channel_layout, 0);
        av_opt_set_int(swr, "out_channel_layout",
output_codec_context->channel_layout, 0);
        av_opt_set_int(swr, "in_sample_rate", frame->sample_rate, 0);
        av_opt_set_int(swr, "out_sample_rate",
output_codec_context->sample_rate, 0);
        av_opt_set_sample_fmt(swr, "in_sample_fmt",
input_codec_context->sample_fmt, 0);
        av_opt_set_sample_fmt(swr, "out_sample_fmt",
output_codec_context->sample_fmt, 0);
        av_opt_set_int(swr, "internal_sample_fmt",
output_codec_context->sample_fmt, 0);

        swr_init(swr);
        uint8_t *outputBuffer;
        int linesize;

        av_samples_alloc(&outputBuffer,
                         &linesize,
                         OUTPUT_CHANNELS,
                         frame->nb_samples,
                         output_codec_context->sample_fmt,
                         0);;
       swr_convert(swr, &outputBuffer, frame->nb_samples, (const uint8_t
**) &frame->data[0],
                    frame->nb_samples);
       //LOGE("MaKr convert_frame_format 1.4 outputBuffer: %c %c %c",
outputBuffer[0], outputBuffer[500], outputBuffer[1000]);
       swr_free(&swr);
       AVFrame *new_frame = NULL;
       utility_init_output_frame(&new_frame, DEFAULT_AAC_FRAME_SIZE,
OUTPUT_CHANNELS,
                                 output_codec_context->sample_fmt,
output_codec_context->sample_rate);
       new_frame->data[0] = outputBuffer;
       return new_frame;
    }
    return frame;

}

int utility_init_output_frame(AVFrame **frame, int frame_size, int
channels, int sample_fmt,
                             int sample_rate) {

    int error;
    if (!(*frame = av_frame_alloc())) {
        return AVERROR_EXIT;
    }

    (*frame)->nb_samples = frame_size;
    (*frame)->channels = channels;
    (*frame)->channel_layout = av_get_default_channel_layout(channels);
    (*frame)->format = sample_fmt;
    (*frame)->sample_rate = sample_rate;

    if ((error = av_frame_get_buffer(*frame, 0)) < 0) {
        av_frame_free(frame);
        return error;
    }
    return 0;

}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20160105/b6c93f77/attachment.html>


More information about the Libav-user mailing list