Here's some more evolution of the program which unfortunately segfaults.
the av_init_packet(pkt) call is the problem. I thought this was how I should allocate the packet but I guess that function requires an already initialised AVPacket structure. What is the reccommended way to initially allocate an AVPacket ?

#include <iostream>

extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
}

using namespace std;

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


    if (argc <= 1) {
        cout << "usage audio_convert <fielname>\n" << endl;
    }

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

    // data structures
    AVFormatContext *pFormatCtx;

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

    // get stream info
    av_find_stream_info(pFormatCtx);

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

    // get first audio stream
    AVCodecContext *pCodecCtx;
    int audioStream=-1;
    for(int 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) {
        cout << "No audio stream found" << endl;
    }

    // try to find it's codec and open it
    AVCodec *pCodec;
    pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
    if(pCodec==NULL) {
        cout << "can't find codec" << endl;
    }
    avcodec_open(pCodecCtx,pCodec);

    cout << "got the codec for the input stream" << endl;

// now try to get packets of samples from the input stream...i.e. decoding
    AVPacket *pkt;
    av_init_packet(pkt);
    av_free(pkt);

    // cleanup
    av_close_input_file(pFormatCtx);
    return 0;
}

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

Reply via email to