I pulled apart the api_example.c and looked at a few of the threads in the archives and came up with the program below to try to decode some audio to raw samples. The dump_format seems to give the right stuff and I manage to decode the first set of samples from the file but after that everything fails. I guess there must be something small in the program that I'm missing as it *almost* seems to work now. Why am I getting "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE" when as far as I can tell I've made the input buffer (using av_new_packet) and the output buffer at least this size ?
Can anyone help me get over the final hurdle ?

/**
* to build : gcc -o decode decode.c -lavcodec -lavdevice -lavformat -lavutil -lz -lvorbis -lvorbisenc -lvorbisfile -lmp3lame -lbz2
 */

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


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


int main(int argc, char **argv)
{

    // declare variables
    int i;
    AVFormatContext *pFormatCtx;
    AVCodecContext *pCodecCtx;
    int audioStream;
    AVCodec *codec;
    short *outbuf;
    int num_samples;
    int out_size;
    FILE *outfile;
    int frame_count;
    AVPacket avpkt;
    int len;

    if (argc !=3) {
printf("Decode audio from a media file to raw samples.\nUsage : decode <input filename> <output filename>\n");
        return 1;
    }

    // initialise libavformat /libavcodec
    avcodec_init();
    avcodec_register_all();
    av_register_all();

    // data structures


    // open input file
    av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL);

    // get streams info
    av_find_stream_info(pFormatCtx);

    // dump the format
    dump_format(pFormatCtx, 0, argv[1], 0);

    // get first audio stream , pCodecCtx will point to it

    audioStream=-1;
    for(i=0; i<pFormatCtx->nb_streams; i++) {
        pCodecCtx=pFormatCtx->streams[i]->codec;
        if(pCodecCtx->codec_type == AVMEDIA_TYPE_AUDIO) {
            audioStream=i;
            break;
        }
    }
    if(audioStream==-1) {
        printf("No audio stream found\n");
        exit(1);
    }

    // try to find it's codec and open it
    codec=avcodec_find_decoder(pCodecCtx->codec_id);
    if(codec==NULL) {
        printf("can't find codec\n");
        exit(1);
    }
    if (avcodec_open(pCodecCtx,codec) != 0) {
        printf("can't open codec\n");
        exit(1);
    }

    // output samples buffers
    num_samples=AVCODEC_MAX_AUDIO_FRAME_SIZE;
    out_size=sizeof(short)*num_samples;
    outbuf = (short*) malloc(num_samples);

    // open the output file to write sample data into it
    outfile = fopen(argv[2], "wb");
    if (!outfile) {
        exit(1);
    }

    // initialise the packet
av_new_packet(&avpkt,AVCODEC_MAX_AUDIO_FRAME_SIZE+FF_INPUT_BUFFER_PADDING_SIZE);

    // do the decoding
    frame_count=0;
    while(av_read_frame(pFormatCtx, &avpkt)>=0) {
        ++frame_count;
        while (avpkt.size>0) {
len = avcodec_decode_audio3(pCodecCtx, outbuf, &out_size, &avpkt);
            if(len < 0) {
                printf("failed to decode frame %d\n", frame_count);
                break;
            } else {
printf("decoded %d samples for frame %d\n", len, frame_count);
            }
            avpkt.data += len;
            avpkt.size -= len;
            fwrite(outbuf, 1, out_size, outfile);
        }
    }
    fclose(outfile);
    free(outbuf);
    avcodec_close(pCodecCtx);
    return 0;
}

output...

[mp3 @ 0x101008000]max_analyze_duration reached
[mp3 @ 0x101008000]Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from 'music.mp3':
  Metadata:
TCON : FILM STYLES,DRAMA$FILM STYLES,FANTASY / ADVENTURE$DRAMA,ADVENTURE$TRAILER MUSIC,ACTION/ADVENTURE$DRAMA,CHASE$DRAMA,ACTION$FILM STYLES,SCIENCE FICTION$FILM STYLES,ACTION TIT3 : Punchy, dramatic electronic action - Need for Speed Most Wanted
    TOPE            : Linford, Paul(BMI)<MPM ARTS MUSIC>
    TCOM            : Linford, Paul(BMI)<MPM ARTS MUSIC>
    TALB            : NEED FOR SPEED - MOST WANTED - MUSIC FROM THE EA GAME
    TLEN            : 183000
    TPUB            : BMI
    TPE1            : EA
    TIT2            : Get Out of Trouble
    TOAL            : EA_0010_005.01
TSSE : LAME 3.96.1 - Metadata by Soundminer Inc. www.soundminer.com TPE4 : <MAGIC><Encoder>Metadata enbedded by Soundminer.</Encoder><kAudioFilePropertyDataFormat>2 ch, 48000 Hz, &apos;lpcm&apos; (0x0000000C)16-bit </kAudioFilePropertyDataFormat><Channels type="NSNumber">2</Channels><BWDate>2007-03-28</BWDate><Category>FILM STYLES,DRAMA$FILM STYLES,FANTASY / ADVENTURE$DRAMA,ADVENTURE$TRAILER MUSIC,ACTION/ADVENTURE$DRAMA,CHASE$DRAMA,ACTION$FILM STYLES,SCIENCE FICTION$FILM STYLES,ACTION</Category><BWCodingHistory>Metadata added by Soundminer.</BWCodingHistory><TrackTitle>Ge
  Duration: 00:03:06.93, start: 0.000000, bitrate: 320 kb/s
    Stream #0.0: Audio: mp3, 44100 Hz, 2 channels, s16, 320 kb/s
[mp3 @ 0x101009200]buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE
decoded 1044 samples for frame 1
failed to decode frame 2
failed to decode frame 3
failed to decode frame 4
failed to decode frame 5
failed to decode frame 6
failed to decode frame 7

_______________________________________________
libav-user mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user

Reply via email to