Thanks for the help<br>I tried to write in normal way to the container<br>FLAC to Ogg, MP2 to Wav - works without problems, Vorbis to Ogg - writes garbage <br>in the container the distorted data are located<br><a href="https://dl.dropbox.com/u/2114502/flac.ogg">https://dl.dropbox.com/u/2114502/flac.ogg</a><br>
<a href="https://dl.dropbox.com/u/2114502/vorbis.ogg">https://dl.dropbox.com/u/2114502/vorbis.ogg</a><br>maybe this codec has any specific parameters ???<br><br>static void audio_encode_example(const char *filename)<br>{<br>
AVFormatContext* out_format_context; <br> AVStream * audio_stream ;<br><br> AVCodec *codec;<br> AVCodecContext *c= NULL;<br> AVFrame *frame;<br> AVPacket pkt;<br> int i, j, k, ret, got_output;<br> int buffer_size;<br>
FILE *f;<br> uint16_t *samples;<br> float t, tincr;<br><br> printf("Encode audio file %s\n", filename);<br><br> out_format_context = avformat_alloc_context();<br> out_format_context->oformat = av_guess_format(NULL, filename, NULL);<br>
<br> if (out_format_context->oformat == NULL){<br> fprintf(stderr, "Could not guess output format\n");<br> exit(1);<br> }<br> audio_stream = av_new_stream(out_format_context, 0);<br><br>
if (!audio_stream) {<br> fprintf(stderr, "Could not alloc audio stream!");<br> exit(1);<br> }<br><br> /* find encoder */<br> //codec = avcodec_find_encoder(AV_CODEC_ID_MP2);<br> codec = avcodec_find_encoder(AV_CODEC_ID_VORBIS);<br>
//codec = avcodec_find_encoder(AV_CODEC_ID_FLAC);<br><br> if (!codec) {<br> fprintf(stderr, "Codec not found\n");<br> exit(1);<br> }<br><br> //c = avcodec_alloc_context3(codec);<br> c=audio_stream->codec;<br>
<br> c->bit_rate = 64000;<br><br> c->sample_fmt = *codec->sample_fmts;<br><br> c->sample_rate = select_sample_rate(codec);<br> c->channel_layout = select_channel_layout(codec);<br> c->channels = av_get_channel_layout_nb_channels(c->channel_layout);<br>
<br> if(out_format_context->oformat->flags & AVFMT_GLOBALHEADER)<br> c->flags |= CODEC_FLAG_GLOBAL_HEADER;<br><br> /* open it */<br> if (avcodec_open2(c, codec, NULL) < 0) {<br> fprintf(stderr, "Could not open codec\n");<br>
exit(1);<br> }<br> if(avio_open(&out_format_context->pb, filename, AVIO_FLAG_WRITE)<0){<br> fprintf(stderr, "Error occurred when opening output file");<br> exit(1);<br>
}<br><br> if (avformat_write_header(out_format_context, NULL) < 0) {<br> fprintf(stderr, "Error occurred when opening output file");<br> exit(1);<br> }<br><br> frame = avcodec_alloc_frame();<br>
if (!frame) {<br> fprintf(stderr, "Could not allocate audio frame\n");<br> exit(1);<br> }<br><br> frame->nb_samples = c->frame_size;<br> frame->format = c->sample_fmt;<br>
frame->channel_layout = c->channel_layout;<br><br> buffer_size = av_samples_get_buffer_size(NULL, c->channels, c->frame_size,<br> c->sample_fmt, 0);<br> samples = (uint16_t*)av_malloc(buffer_size);<br>
if (!samples) {<br> fprintf(stderr, "Could not allocate %d bytes for samples buffer\n",<br> buffer_size);<br> exit(1);<br> }<br><br> ret = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,<br>
(const uint8_t*)samples, buffer_size, 0);<br> if (ret < 0) {<br> fprintf(stderr, "Could not setup audio frame\n");<br> exit(1);<br> }<br> t = 0;<br> tincr = 2 * M_PI * 440.0 / c->sample_rate;<br>
for(i=0;i<200;i++) {<br> av_init_packet(&pkt);<br> pkt.data = NULL; <br> pkt.size = 0;<br><br> for (j = 0; j < c->frame_size; j++) {<br> samples[2*j] = (int)(sin(t) * 10000);<br>
<br> for (k = 1; k < c->channels; k++)<br> samples[2*j + k] = samples[2*j];<br> t += tincr;<br> }<br><br> ret = avcodec_encode_audio2(c, &pkt, frame, &got_output);<br>
if (ret < 0) {<br> fprintf(stderr, "Error encoding audio frame\n");<br> exit(1);<br> }<br> if (got_output) {<br> pkt.pts=pkt.dts=AV_NOPTS_VALUE;<br> //pkt.pts=pkt.dts=time;<br>
int ret = av_interleaved_write_frame(out_format_context, &pkt);<br> if (ret < 0) {<br> fprintf(stderr,"Error while write audio");<br> } <br> //fwrite(pkt.data, 1, pkt.size, f);<br>
av_free_packet(&pkt);<br> }<br> }<br><br> for (got_output = 1; got_output; i++) {<br> ret = avcodec_encode_audio2(c, &pkt, NULL, &got_output);<br> if (ret < 0) {<br> fprintf(stderr, "Error encoding frame\n");<br>
exit(1);<br> }<br><br> if (got_output) {<br> pkt.pts=pkt.dts=AV_NOPTS_VALUE;<br> int ret = av_interleaved_write_frame(out_format_context, &pkt);<br> if (ret < 0) {<br>
fprintf(stderr,"Error while write audio");<br> } <br> //fwrite(pkt.data, 1, pkt.size, f);<br> av_free_packet(&pkt);<br> }<br> }<br> av_write_trailer(out_format_context);<br>
avio_close(out_format_context->pb);<br><br> av_freep(&samples);<br> av_freep(&frame);<br> avcodec_close(c);<br> av_free(c);<br>}<br><br>int main(int argc, char **argv)<br>{<br> //avcodec_register_all();<br>
av_register_all();<br> //audio_encode_example("test.wav");<br> audio_encode_example("test.ogg");<br> printf("Encoding is completed...\n");<br> getch();<br> return 0;<br>}<br>
<br><div class="gmail_quote">2013/1/10 Carl Eugen Hoyos <span dir="ltr"><<a href="mailto:cehoyos@ag.or.at" target="_blank">cehoyos@ag.or.at</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">แฬลหำมฮฤา ๒ีศฬฯื <A.Rukhlov@...> writes:<br>
<br>
> Signal of 440 Hz it is coded and written to the container.<br>
</div>> AAC, MP2, AC3 works without problems.BUT categorically<br>
> Vorbis doesn't want to workfunction avcodec_encode_audio2()<br>
<div class="im">> works without mistakes and even something returns, but<br>
> these data are damaged<br>
<br>
</div>Is it possible that you are trying to write<br>
a raw vorbis file (as opposed to muxing<br>
vorbis in a container format)?<br>
I don't think raw vorbis is defined.<br>
(And generally, you should never "fwrite()"<br>
what the encoder returns, but feed a muxer,<br>
this can be the adts, mp2 or ac3 muxer if<br>
you want raw files.)<br>
<br>
Carl Eugen<br>
<br>
_______________________________________________<br>
Libav-user mailing list<br>
<a href="mailto:Libav-user@ffmpeg.org">Libav-user@ffmpeg.org</a><br>
<a href="http://ffmpeg.org/mailman/listinfo/libav-user" target="_blank">http://ffmpeg.org/mailman/listinfo/libav-user</a><br>
</blockquote></div><br>