[Libav-user] Encoding & muxing to m4a problem

Phu Nguyen Anh yaiba243 at gmail.com
Thu Aug 8 02:11:23 CEST 2013


Hi there, 
I'm a newbie to libav and now having trouble encoding & mux a sound sample array to m4a file. The process done without any problem, but output file is not hearable and the size is very small, e.g. I have around 88000 sound samples but output file size is only around 1.8KB.
My code below is mostly base on decoding_encoding.c & muxing.c samples from ffmpeg, but no pts, dts parts are set as I donot understand how to set them for audio encoding yet. Maybe problem comes from that?
Please help.

~~~~~
avcodec_register_all();
av_register_all();

codec = avcodec_find_encoder(CODEC_ID_AAC);
c = avcodec_alloc_context3(codec);
c->bit_rate = 64000;
c->sample_fmt = AV_SAMPLE_FMT_FLTP;
c->sample_rate = rate;
c->channel_layout = AV_CH_LAYOUT_MONO;
c->channels = 1;
c->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;

// Open it
avcodec_open2(c, codec, NULL) < 0);

frame = avcodec_alloc_frame();
frame->nb_samples     = c->frame_size;
frame->format         = c->sample_fmt;
frame->channel_layout = c->channel_layout;

buffer_size = av_samples_get_buffer_size(NULL, c->channels, c->frame_size,
                                         c->sample_fmt, 0);
samples = (uint16_t *)av_malloc(buffer_size);

// setup the data pointers in the AVFrame
avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
                               (const uint8_t*)samples, buffer_size, 0);


// allocate the output media context
avformat_alloc_output_context2(&oc, NULL, NULL, filename);
ofmt = oc->oformat;
audio_st = avformat_new_stream(oc, NULL);
audio_st->codec = c;
if (oc->oformat->flags & AVFMT_GLOBALHEADER)
    c->flags |= CODEC_FLAG_GLOBAL_HEADER;

// open the output file
if (!(ofmt->flags & AVFMT_NOFILE)) {
    avio_open(&oc->pb, filename, AVIO_FLAG_WRITE);
}
ret = avformat_write_header(oc, NULL);

// captureBuffer is a sound buffer captured with parameters: mono, 16bit/sample, sample rate = 22050
captureBufPtr = captureBuffer;
samplesLeft = samplesCaptured;
samplesToCopy = 0;
if (frame) {
    frame->pts = 0;
}
// Encoding loop
for (;samplesLeft > 0;) {
    av_init_packet(&pkt);
    pkt.data = NULL; // packet data will be allocated by the encoder
    pkt.size = 0;

    samplesToCopy = samplesLeft >= frame->nb_samples? frame->nb_samples : samplesLeft;
    memcpy(samples, captureBufPtr, samplesToCopy * 2);
    captureBufPtr += samplesToCopy * 2;
    samplesLeft -= samplesToCopy;

    ret = avcodec_encode_audio2(c, &pkt, frame, &got_output);
    ret = av_interleaved_write_frame(oc, &pkt);
av_free_packet(&pkt);

}

// Get delayed frame
for (got_output = 1; got_output;) {
    ret = avcodec_encode_audio2(c, &pkt, NULL, &got_output);
    if (got_output) {
    ret = av_interleaved_write_frame(oc, &pkt);
av_free_packet(&pkt);
    }
}

if (oc) {
av_write_trailer(oc);
}


~~~~~


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20130808/25264c5c/attachment.html>


More information about the Libav-user mailing list