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>