I am attempting to update some code to work with the new float implementation 
of the ac3 encoder.  I made what I believe
are the necessary changes, but the audio produced is just a buzz.  mplayer 
generates these errors:

[ac3 @ 0xd86060]exponent out-of-range
[ac3 @ 0xd86060]error decoding the audio block
[ac3 @ 0xd86060]delta bit allocation strategy reserved
[ac3 @ 0xd86060]error decoding the audio block
[ac3 @ 0xd86060]delta bit allocation strategy reserved
[ac3 @ 0xd86060]error decoding the audio block
[ac3 @ 0xd86060]delta bit allocation strategy reserved
...

I have generated a minimal test program to demonstrate the problem I am having. 
 This program works with the fixed point
implementation from ffmpeg r25689, but does not work with current git head.  
There is a define to change from fixed to
float mode at the top of the code so I can switch easily between.  The program 
takes as input a raw stereo pcm 48khz
file and outputs a raw ac3 file.

Could someone knowledgeable take a look and see if there is anything obvious I 
am doing incorrectly?
#include "libavcodec/avcodec.h"

#define AC3_SAMPLES_PER_FRAME 1536
#define AC3_MAX_CODED_FRAME_SIZE 3840
//#define FLOAT_AC3 1

static void encode( FILE *pcm_file, FILE *ac3_file, AVCodecContext *ctx )
{
    int ii, size;

    int input_samples = AC3_SAMPLES_PER_FRAME * 2;
    int output_bytes = AC3_MAX_CODED_FRAME_SIZE;

    float *ff_buf = malloc( input_samples * sizeof( float ) );
    int16_t *samples = malloc( input_samples * sizeof( int16_t ) );
    uint8_t *out_buf = malloc( output_bytes );

    while (fread(samples, sizeof(int16_t), input_samples, pcm_file))
    {
#if defined(FLOAT_AC3)
        for (ii = 0; ii < input_samples; ii++)
        {
            ff_buf[ii] = (float)samples[ii];
        }

        size = avcodec_encode_audio( ctx, out_buf, output_bytes, 
(short*)ff_buf);
#else
        size = avcodec_encode_audio( ctx, out_buf, output_bytes, samples);
#endif

        if (size > 0)
        {
            fwrite(out_buf, 1, size, ac3_file);
        }

    }
}

int main(int argc, char *argv[])
{
    AVCodec * codec;
    AVCodecContext * ctx;
    FILE *pcm_file = NULL;
    FILE *ac3_file = NULL;

    pcm_file = fopen(argv[1], "rb");
    ac3_file = fopen(argv[2], "wb+");

    av_register_all();
    codec = avcodec_find_encoder( CODEC_ID_AC3 );
    if( !codec )
    {
        printf( "encac3Init: avcodec_find_encoder failed\n" );
        return 1;
    }

    ctx = avcodec_alloc_context();
    avcodec_get_context_defaults3(ctx, codec);

    ctx->channel_layout = CH_LAYOUT_STEREO;

    ctx->codec_id = CODEC_ID_AC3;
    ctx->bit_rate = 192 * 1000;
    ctx->sample_rate = 48000;
    ctx->channels = 2;
    ctx->time_base = (AVRational){1, ctx->sample_rate};
#if defined(FLOAT_AC3)
    ctx->sample_fmt = AV_SAMPLE_FMT_FLT;
#else
    ctx->sample_fmt = AV_SAMPLE_FMT_S16;
#endif

    if( avcodec_open( ctx, codec ) )
    {
        printf( "encac3Init: avcodec_open failed\n" );
    }
    encode(pcm_file, ac3_file, ctx);
}
_______________________________________________
libav-user mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user

Reply via email to