[Libav-user] libavcodec vorbis encoding
Александр Рухлов
A.Rukhlov at gmail.com
Thu Jan 10 16:37:05 CET 2013
Thanks for the help
I tried to write in normal way to the container
FLAC to Ogg, MP2 to Wav - works without problems, Vorbis to Ogg - writes
garbage
in the container the distorted data are located
https://dl.dropbox.com/u/2114502/flac.ogg
https://dl.dropbox.com/u/2114502/vorbis.ogg
maybe this codec has any specific parameters ???
static void audio_encode_example(const char *filename)
{
AVFormatContext* out_format_context;
AVStream * audio_stream ;
AVCodec *codec;
AVCodecContext *c= NULL;
AVFrame *frame;
AVPacket pkt;
int i, j, k, ret, got_output;
int buffer_size;
FILE *f;
uint16_t *samples;
float t, tincr;
printf("Encode audio file %s\n", filename);
out_format_context = avformat_alloc_context();
out_format_context->oformat = av_guess_format(NULL, filename, NULL);
if (out_format_context->oformat == NULL){
fprintf(stderr, "Could not guess output format\n");
exit(1);
}
audio_stream = av_new_stream(out_format_context, 0);
if (!audio_stream) {
fprintf(stderr, "Could not alloc audio stream!");
exit(1);
}
/* find encoder */
//codec = avcodec_find_encoder(AV_CODEC_ID_MP2);
codec = avcodec_find_encoder(AV_CODEC_ID_VORBIS);
//codec = avcodec_find_encoder(AV_CODEC_ID_FLAC);
if (!codec) {
fprintf(stderr, "Codec not found\n");
exit(1);
}
//c = avcodec_alloc_context3(codec);
c=audio_stream->codec;
c->bit_rate = 64000;
c->sample_fmt = *codec->sample_fmts;
c->sample_rate = select_sample_rate(codec);
c->channel_layout = select_channel_layout(codec);
c->channels =
av_get_channel_layout_nb_channels(c->channel_layout);
if(out_format_context->oformat->flags & AVFMT_GLOBALHEADER)
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
/* open it */
if (avcodec_open2(c, codec, NULL) < 0) {
fprintf(stderr, "Could not open codec\n");
exit(1);
}
if(avio_open(&out_format_context->pb, filename, AVIO_FLAG_WRITE)<0){
fprintf(stderr, "Error occurred when opening output file");
exit(1);
}
if (avformat_write_header(out_format_context, NULL) < 0) {
fprintf(stderr, "Error occurred when opening output file");
exit(1);
}
frame = avcodec_alloc_frame();
if (!frame) {
fprintf(stderr, "Could not allocate audio frame\n");
exit(1);
}
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);
if (!samples) {
fprintf(stderr, "Could not allocate %d bytes for samples buffer\n",
buffer_size);
exit(1);
}
ret = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
(const uint8_t*)samples, buffer_size, 0);
if (ret < 0) {
fprintf(stderr, "Could not setup audio frame\n");
exit(1);
}
t = 0;
tincr = 2 * M_PI * 440.0 / c->sample_rate;
for(i=0;i<200;i++) {
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;
for (j = 0; j < c->frame_size; j++) {
samples[2*j] = (int)(sin(t) * 10000);
for (k = 1; k < c->channels; k++)
samples[2*j + k] = samples[2*j];
t += tincr;
}
ret = avcodec_encode_audio2(c, &pkt, frame, &got_output);
if (ret < 0) {
fprintf(stderr, "Error encoding audio frame\n");
exit(1);
}
if (got_output) {
pkt.pts=pkt.dts=AV_NOPTS_VALUE;
//pkt.pts=pkt.dts=time;
int ret = av_interleaved_write_frame(out_format_context, &pkt);
if (ret < 0) {
fprintf(stderr,"Error while write audio");
}
//fwrite(pkt.data, 1, pkt.size, f);
av_free_packet(&pkt);
}
}
for (got_output = 1; got_output; i++) {
ret = avcodec_encode_audio2(c, &pkt, NULL, &got_output);
if (ret < 0) {
fprintf(stderr, "Error encoding frame\n");
exit(1);
}
if (got_output) {
pkt.pts=pkt.dts=AV_NOPTS_VALUE;
int ret = av_interleaved_write_frame(out_format_context, &pkt);
if (ret < 0) {
fprintf(stderr,"Error while write audio");
}
//fwrite(pkt.data, 1, pkt.size, f);
av_free_packet(&pkt);
}
}
av_write_trailer(out_format_context);
avio_close(out_format_context->pb);
av_freep(&samples);
av_freep(&frame);
avcodec_close(c);
av_free(c);
}
int main(int argc, char **argv)
{
//avcodec_register_all();
av_register_all();
//audio_encode_example("test.wav");
audio_encode_example("test.ogg");
printf("Encoding is completed...\n");
getch();
return 0;
}
2013/1/10 Carl Eugen Hoyos <cehoyos at ag.or.at>
> Александр Рухлов <A.Rukhlov at ...> writes:
>
> > Signal of 440 Hz it is coded and written to the container.
> > AAC, MP2, AC3 works without problems.BUT categorically
> > Vorbis doesn't want to workfunction avcodec_encode_audio2()
> > works without mistakes and even something returns, but
> > these data are damaged
>
> Is it possible that you are trying to write
> a raw vorbis file (as opposed to muxing
> vorbis in a container format)?
> I don't think raw vorbis is defined.
> (And generally, you should never "fwrite()"
> what the encoder returns, but feed a muxer,
> this can be the adts, mp2 or ac3 muxer if
> you want raw files.)
>
> Carl Eugen
>
> _______________________________________________
> Libav-user mailing list
> Libav-user at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/libav-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20130110/673e3dba/attachment.html>
More information about the Libav-user
mailing list