Hi all,

Thanks for your valuable inputs.  Am attaching audio_encode_example() using new 
API of
avcodec_encode_audio2().  Its working for me now!

Regards,
abhishek

--- On Thu, 1/3/12, Don Moir <[email protected]> wrote:

From: Don Moir <[email protected]>
Subject: Re: [Libav-user] [FFmpeg-devel]  audio_encode_example
To: [email protected]
Date: Thursday, 1 March, 2012, 3:28 AM

> i've read ffmpeg-0.10\doc\examples\decoding_encoding.c. Its having
> audio_encode_example() using avcodec_encode_audio() (which is deprecated).

> Can someone provide audio_encode_example() using new API of
> avcodec_encode_audio2()?

If you look in libavcodec\utils.c you will see that avcodec_encode_audio now
calls avcodec_encode_audio2. You can look at the code for
avcodec_encode_audio (utils.c) and check how it now handles
avcodec_encode_audio2 and that should be enough for you to move to
avcodec_encode_audio2.

_______________________________________________
Libav-user mailing list
[email protected]
http://ffmpeg.org/mailman/listinfo/libav-user
/*
 * Copyright (c) 2001 Fabrice Bellard
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/**
 * @file
 * libavcodec API use example.
 *
 * Note that libavcodec only handles codecs (mpeg, mpeg4, etc...),
 * not file formats (avi, vob, mp4, mov, mkv, mxf, flv, mpegts, mpegps, 
etc...). See library 'libavformat' for the
 * format handling
 */
#include "libavutil/imgutils.h"
#include "libavutil/opt.h"
#include "libavcodec/avcodec.h"
#include "libavutil/mathematics.h"
#include "libavutil/samplefmt.h"

#define INBUF_SIZE 4096
#define AUDIO_INBUF_SIZE 320
#define AUDIO_REFILL_THRESH 4096

/*
 * Audio encoding example
 */
static void audio_encode_example(const char *outfilename, const char *filename)
{
        struct timeval tv1,tv2;
        unsigned long int timetaken = 0;
        unsigned long int totaltimetaken = 0;
        unsigned long int noOfSamples = 0;
        unsigned long int noOfCycles = 0;
        unsigned long int mcps_mega = 0;
        double mcps = 0.0;

    AVCodec *codec;
    AVCodecContext *c= NULL;
    int frame_size, i, j, out_size, outbuf_size, ret, len;
    FILE *f, *outfile;
        uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
    short *samples;
    float t, tincr;
    uint8_t *outbuf;
        AVPacket avpkt;
        AVFrame *encoded_frame = NULL;
        int got_packet_ptr = 0;

        av_init_packet(&avpkt);

    printf("Audio encoding\n");

    /* find the MP2 encoder */
    codec = avcodec_find_encoder(CODEC_ID_ADPCM_G722);
    if (!codec) {
        fprintf(stderr, "codec not found\n");
        exit(1);
    }

    c = avcodec_alloc_context3(codec);

    /* put sample parameters */
    c->bit_rate = 64000;
    c->sample_rate = 16000;
    c->channels = 1;
    c->sample_fmt = AV_SAMPLE_FMT_S16;

    /* open it */
    if (avcodec_open2(c, codec, 0) < 0) {
        fprintf(stderr, "could not open codec\n");
        exit(1);
    }

        f = fopen(filename, "rb");
    if (!f) {
        fprintf(stderr, "could not open %s\n", filename);
        exit(1);
    }

    outfile = fopen(outfilename, "wb");
    if (!outfile) {
        fprintf(stderr, "could not open %s\n", outfilename);
                av_free(c);
        exit(1);
    }

    /* the codec gives us the frame size, in samples */
    frame_size = c->frame_size;
    samples = malloc(frame_size * 2 * c->channels);
    outbuf_size = 160;
    outbuf = malloc(outbuf_size);

        /* encode until eof */
        avpkt.data = outbuf;
    avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
        avpkt.size /= 2;

    while (avpkt.size > 0) {

        int got_frame = 0;

        if (!encoded_frame) {
            if (!(encoded_frame = avcodec_alloc_frame())) {
                fprintf(stderr, "out of memory\n");
                exit(1);
            }
        } else
            avcodec_get_frame_defaults(encoded_frame);

        encoded_frame->nb_samples  = AUDIO_INBUF_SIZE /
                             (c->channels * 
av_get_bytes_per_sample(c->sample_fmt));

        if ((ret = avcodec_fill_audio_frame(encoded_frame, c->channels, 
c->sample_fmt,
                                            inbuf, AUDIO_INBUF_SIZE, 1)) < 0) {
            av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
            exit(1);
        }


                gettimeofday(&tv1,NULL);

            if (avcodec_encode_audio2(c, &avpkt, encoded_frame, 
&got_packet_ptr) < 0) {
                av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
                    exit(1);
                }

                /* for getting the finish time */
        gettimeofday(&tv2,NULL);
                /* total time taken for execution which is used for profiling  
*/
                timetaken = (tv2.tv_sec*1000000 + tv2.tv_usec) - 
(tv1.tv_sec*1000000 + tv1.tv_usec);
                totaltimetaken += timetaken;

        ret = avpkt.size;

        if (got_packet_ptr) {
            /* if a frame has been encoded, output it */
            int data_size = av_samples_get_buffer_size(NULL, c->channels,
                                                       
encoded_frame->nb_samples,
                                                       c->sample_fmt, 1);
            //fwrite(encoded_frame->data[0], 1, data_size, outfile);
                        fwrite(avpkt.data, 1, avpkt.size, outfile);
                        av_free_packet(&avpkt);
        }

                avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
                avpkt.size = avpkt.size / 2;
        avpkt.dts =
        avpkt.pts = AV_NOPTS_VALUE;

#if 0
                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);
                        printf("memmove done\n");
            avpkt.data = inbuf;
            len = fread(avpkt.data + avpkt.size, 1,
                        AUDIO_INBUF_SIZE - avpkt.size, f);
                        printf("fread done\n");
            if (len > 0)
                avpkt.size += len;
                        printf("refill done\n");
        }
#endif
    }

        /* to display total time taken for the execution */
        printf("totaltime taken = %ld \n", totaltimetaken);

    fclose(outfile);
    fclose(f);

    avcodec_close(c);
    av_free(c);
    av_free(encoded_frame);

    free(outbuf);
    free(samples);
}

int main(int argc, char **argv)
{
    const char *filename;

    /* must be called before using avcodec lib */
    //avcodec_init();

    /* register all the codecs */
    avcodec_register_all();

    if (argc <= 1) {
        audio_encode_example("/tmp/Test4.inp.adp", "/tmp/Test4.inp");
        filename = "/tmp/Test4.inp";
    } else {
        filename = argv[1];
    }

    return 0;
}
_______________________________________________
Libav-user mailing list
[email protected]
http://ffmpeg.org/mailman/listinfo/libav-user

Reply via email to