[Libav-user] ffmpeg on android

Marcelo Paes Rech marcelopaesrech at gmail.com
Sun Jan 12 13:48:25 CET 2014


Hi guys,

I am trying to do a stream player in android but I need to read a array of
bytes and then decode every packet of array of bytes. In the end started to
make my AAC decoder work with the ffmpeg example of decoder. But I ran into
the same problem of this guy:

http://stackoverflow.com/questions/13499480/decode-aac-to-pcm-with-ffmpeg-on-android

But the decoder does not work. I am receiving a error as follows:

TNS filter order %d is greater than maximum %d.
Error while decoding: -1

If I use the ffmpeg to decode it works fine.

ffmpeg -i audio.mp4 audio.wav

Environment:
ffmpeg 1.2
Android ndkr9b

Follows my source code:

    AVCodec *codec;

    AVCodecContext *c= NULL;

    int len;

    FILE *f, *outfile;

    uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];

    AVPacket avpkt;

    AVFrame *decoded_frame = NULL;

    av_log_set_callback(&my_ffmpeg_log);

    av_init_packet(&avpkt);


    printf("Decode audio file %s to %s\n", filename, outfilename);


    /* find the mpeg audio decoder */

    codec = avcodec_find_decoder(AV_CODEC_ID_AAC);

    if (!codec) {

        LOGV("Codec not found\n");

        return;

    }


    c = avcodec_alloc_context3(codec);

    c->channels = 2;

    c->sample_rate = 48000;


    if (!c) {

        LOGV("Could not  allocate audio codec context\n");

        return;

    }


    /* open it */

    if (avcodec_open2(c, codec, NULL) < 0) {

        LOGV("Could not open codec\n");

        return;

    }


    f = fopen(filename, "rb");

    if (!f) {

        LOGV("Could not open %s\n", filename);

        return;

    }

    outfile = fopen(outfilename, "wb");

    if (!outfile) {

        av_free(c);

        return;

    }


    /* decode until eof */

    avpkt.data = inbuf;

    avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);


    while (avpkt.size > 0) {

        int got_frame = 0;


        if (!decoded_frame) {

            if (!(decoded_frame = avcodec_alloc_frame())) {

                LOGV("Could not allocate audio frame\n");

                return;

            }

        } else

            avcodec_get_frame_defaults(decoded_frame);


        len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);

        if (len < 0) {

            LOGV("Error while decoding: %d\n", len);

            return;

        }

        if (got_frame) {

            /* if a frame has been decoded, output it */

            int data_size = av_samples_get_buffer_size(NULL, c->channels,


decoded_frame->nb_samples,

                                                       c->sample_fmt, 1);

            fwrite(decoded_frame->data[0], 1, data_size, outfile);

        }

        avpkt.size -= len;

        avpkt.data += len;

        avpkt.dts =

        avpkt.pts = AV_NOPTS_VALUE;

        if (avpkt.size < AUDIO_REFILL_THRESH) {

            /* Refill the input buffer, to avoid trying to decode

             * incomplete frames. Instead of this, one could also use

             * a parser, or use a proper container format through

             * libavformat. */

            memmove(inbuf, avpkt.data, avpkt.size);

            avpkt.data = inbuf;

            len = fread(avpkt.data + avpkt.size, 1,

                        AUDIO_INBUF_SIZE - avpkt.size, f);

            if (len > 0)

                avpkt.size += len;

        }

    }


    fclose(outfile);

    fclose(f);


    avcodec_close(c);

    av_free(c);

    avcodec_free_frame(&decoded_frame);

-- 
*Atenciosamente, Marcelo Paes Rech.*
E-mail: marcelopaesrech at gmail.com
Blog: http://marcelopaesrech.blogspot.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20140112/ca6eb0d1/attachment.html>


More information about the Libav-user mailing list