Hi All, I want to convert any audio [libav recognised] format to raw
wave content of 1 channel and 8000 bitrate. Following is the source
code, however the read samples are always null. Could you please point
out the obvious msitake that I have overlooked. Any help is
appriciated.

TIA.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>

#define RATE 8000
#define CHANNELS 1
#define SAMPLES_SIZE  ((AVCODEC_MAX_AUDIO_FRAME_SIZE *
2)+FF_INPUT_BUFFER_PADDING_SIZE)


AVStream* get_stream(struct AVFormatContext *ic, enum CodecType type) {
    unsigned int i;
    for(i = 0; i < ic->nb_streams; i++) {
        if(ic->streams[i]->codec->codec_type == type) {
            return ic->streams[i];
        }
    }
    return NULL;
}


static int init_audio_pcm(AVCodecContext * codec_context){
    AVCodec * codec = avcodec_find_encoder(CODEC_ID_PCM_S16LE);
    if (!codec){
        return -1;
    }

    if (avcodec_open(codec_context, codec) < 0)     {
        return -1;
    }

    codec_context->sample_rate = RATE;
    codec_context->channels = CHANNELS;
    codec_context->bit_rate = 8000;
    return 0;
}

int main(int argc, char* argv[])
{
    AVFormatContext *in_format_ctx;
    AVStream* in_audio_st ;
    AVCodec *in_audio_codec;
    AVPacket in_packet;
    int ret_val;
    int16_t *samples, *resamples, *audio_out;
    int samples_size, new_sample_size , audio_out_size, frame_bytes;

    /* output */
    AVCodecContext *out_codec_ctx;
    ReSampleContext *resample_ctx;

    avcodec_init();
    avcodec_register_all();
    av_register_all();

    
av_log_set_level(AV_LOG_DEBUG|AV_LOG_VERBOSE|AV_LOG_INFO|AV_LOG_WARNING|AV_LOG_ERROR|AV_LOG_FATAL|AV_LOG_PANIC);

    if((ret_val=av_open_input_file(&in_format_ctx, argv[1], NULL, 0, NULL))!=0){
        return -1;
    }

    if(ret_val=av_find_stream_info(in_format_ctx)<0){
        return -1;
    }
    dump_format(in_format_ctx, 0, argv[1], 0);
    if(!((get_stream(in_format_ctx,CODEC_TYPE_VIDEO)==NULL) &&
((in_audio_st=get_stream(in_format_ctx,CODEC_TYPE_AUDIO))!=NULL) )) {
        return -1;
    }
    in_audio_codec = avcodec_find_decoder(in_audio_st->codec->codec_id);
    if(in_audio_codec == NULL) {
        return -1;
    }

    if(avcodec_open(in_audio_st->codec, in_audio_codec) < 0) {
        return -1;
    }

    av_init_packet(&in_packet);
    samples=(int16_t *)av_malloc(SAMPLES_SIZE);
    resamples=(int16_t *)av_malloc(SAMPLES_SIZE);
    audio_out=(int16_t *)av_malloc(SAMPLES_SIZE);
    if(samples == NULL || resamples == NULL||audio_out == NULL) {
        return -1;
    }

    out_codec_ctx=avcodec_alloc_context();
    if(out_codec_ctx == NULL) {
        return -1;
    }

    ret_val=init_audio_pcm(out_codec_ctx);
    if(ret_val < 0){
        return -1;
    }
    frame_bytes = out_codec_ctx->frame_size * 2 * out_codec_ctx->channels;
    
resample_ctx=av_audio_resample_init(CHANNELS,in_audio_st->codec->channels,RATE,in_audio_st->codec->sample_rate,
SAMPLE_FMT_S16,in_audio_st->codec->sample_fmt,16, 10, 0, 0.8);
    if(resample_ctx == NULL) {
        return -1;
    }

    while ((ret_val = av_read_frame(in_format_ctx, &in_packet)) >= 0) {
        if (  in_packet.size <1  ) {
            continue;
        }
        samples_size = SAMPLES_SIZE;
        ret_val = avcodec_decode_audio3(in_audio_st->codec,(int16_t*)
samples, &samples_size, &in_packet);

        if(in_packet.size > ret_val) {
            continue;
        }

        if(samples_size < 0 ) {
            continue;
        }
        if(samples_size > (AVCODEC_MAX_AUDIO_FRAME_SIZE * 2)){
            continue;
        }
        if(samples_size == 0) {
            continue;
        }

        new_sample_size = audio_resample(resample_ctx, resamples,
samples,samples_size/( sizeof( short )*CHANNELS ) );
        audio_out_size = avcodec_encode_audio(out_codec_ctx,
audio_out, new_sample_size, (short*)resamples);

        av_free_packet(&in_packet);
    }

    av_close_input_file(in_format_ctx);
    audio_resample_close(resample_ctx);
    return 0;
}
_______________________________________________
libav-user mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user

Reply via email to