hi again,

when I write the samples values before and after the encoding/decoding
in apiexample.c, I get very different values. I don't expect the
values to be exactly equal, because of the encoding/decoding process,
but they are really different. Moreover, samples contains 460800
values (about 5s, 44.1kHz stereo), while the decoded values array
contains only 50288 values.

The modified apiexample.c is attached, as well as the 5000 first
values 1/of the samples, 2/the decoded samples.

Anybody can help?

thank you

jul
/*
 * copyright (c) 2001 Fabrice Bellard
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//

/*
 * @file apiexample.c
 * avcodec API use example.
 *
 * Note that this library only handles codecs (mpeg, mpeg4, etc...),
 * not file formats (avi, vob, etc...). See library 'libavformat' for the
 * format handling
*/

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

#define PI 3.14159265358979323846

#ifdef HAVE_AV_CONFIG_H
#undef HAVE_AV_CONFIG_H
#endif

#include "ffmpeg/avcodec.h"
#include "ffmpeg/avformat.h"

#define INBUF_SIZE 4096

//
// Audio encoding example
//
void audio_encode_example(const char *filename)
{
    AVCodec *codec;
    AVCodecContext *c= NULL;
    int frame_size, i, j, out_size, outbuf_size;
    FILE *f;
    short *samples;
    float t, tincr;
    uint8_t *outbuf;

    printf("Audio encoding\n");

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

    c= avcodec_alloc_context();

    // put sample parameters //
    c->bit_rate = 64000;
    c->sample_rate = 44100;
    c->channels = 2;

    // open it //
    if (avcodec_open(c, codec) < 0) {
        fprintf(stderr, "could not open codec\n");
        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 = 10000;
    outbuf = malloc(outbuf_size);

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

    //file containing raw data
    FILE *rawdatafile;
    rawdatafile = fopen("/tmp/samples.dat","w");

    // encode a single tone sound //
    t = 0;
    tincr = 2 * PI * 440.0 / c->sample_rate;
    for(i=0;i<200;i++) {
        for(j=0;j<frame_size;j++) {
            samples[2*j] = (int)(sin(t) * 10000);
            samples[2*j+1] = samples[2*j];

            fprintf(rawdatafile,"%hd\n%hd\n",samples[2*j],samples[2*j+1]);

            t += tincr;
        }
        // encode the samples //
        out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);
        fwrite(outbuf, 1, out_size, f);
    }

    fclose(f);
    free(outbuf);
    free(samples);

    fclose(rawdatafile);

    avcodec_close(c);
    av_free(c);
}

//
//  Audio decoding.
//
int audio_decode_example(const char *outfilename, const char *filename)
{
    AVCodec *aCodec;
    AVCodecContext *aCodecCtx;
    int out_size, size, len;
    FILE *f, *outfile;
    uint8_t *outbuf;
    uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;

    printf("Audio decoding\n");

////////// original code /////////////////////////////////////////////

/*
    // find the mpeg audio decoder
    codec = avcodec_find_decoder(CODEC_ID_MP2);
    if (!codec) {
        fprintf(stderr, "codec not found\n");
        exit(1);
    }

    c= avcodec_alloc_context();
*/


////////// code from http://www.dranger.com/ffmpeg/tutorial03.c /////////////

    AVFormatContext *pFormatCtx;
    int i, audioStream;

    // Open video file
    if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0) {
        printf("Couldn't open file\n");
        return -1; // Couldn't open file
    }

    // Retrieve stream information
    if(av_find_stream_info(pFormatCtx)<0)
        return -1; // Couldn't find stream information

    // Dump information about file onto standard error
    dump_format(pFormatCtx, 0, filename, 0);

    // Find the first video stream
    audioStream=-1;
    for(i=0; i<pFormatCtx->nb_streams; i++) {
        if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_AUDIO && audioStream < 0) {
            audioStream=i;
        }
    }
    
    if(audioStream==-1)
        return -1;

    aCodecCtx=pFormatCtx->streams[audioStream]->codec;
    
    aCodec = avcodec_find_decoder(aCodecCtx->codec_id);
    if(!aCodec) {
        fprintf(stderr, "Unsupported codec!\n");
        return -1;
    }
  

//////////////////////////////////////////////////////////////////////////////


    // open it //
    if (avcodec_open(aCodecCtx, aCodec) < 0) {
        fprintf(stderr, "could not open codec\n");
        exit(1);
    }

    outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);

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

    //file containing the decoded data
    FILE *decodeddatafile;
    decodeddatafile = fopen("/tmp/decodedsamples.dat","w");

    // decode until eof //
    inbuf_ptr = inbuf;
    for(;;) {
        size = fread(inbuf, 1, INBUF_SIZE, f);
        if (size == 0)
            break;

        inbuf_ptr = inbuf;
        while (size > 0) {
            len = avcodec_decode_audio(aCodecCtx, (short *)outbuf, &out_size,
                                       inbuf_ptr, size);
            if (len < 0) {
                fprintf(stderr, "Error while decoding\n");
                exit(1);
            }
            if (out_size > 0) {
                // if a frame has been decoded, output it //
                fwrite(outbuf, 1, out_size, outfile);

                int j;
                for(j=0;j<out_size;j++)
                    fprintf(decodeddatafile,"%d\n",outbuf[j]);
            }
            size -= len;
            inbuf_ptr += len;
        }
    }

    fclose(outfile);
    fclose(f);
    free(outbuf);

    fclose(decodeddatafile);

    avcodec_close(aCodecCtx);
    av_free(aCodecCtx);

}

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

    const char *filename=argv[1];

    // register all formats and codecs
    av_register_all();
    

    if (argc <= 1) {
        audio_encode_example("/tmp/test.mp2");
        audio_decode_example("/tmp/test.sw", "/tmp/test.mp2");

    } else {
        filename = argv[1];
    }

    return 0;
}

Attachment: samples.dat
Description: MOPAC data

Attachment: decodedsamples.dat
Description: MOPAC data

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

Reply via email to