<div dir="ltr">No, I didn't get clear. I want to pass the decoded output as input, so that I get original audio file(.mp3). I need a file to achieve this. Actually I tried the example programs on ffmpeg and libavcodec, <div><br></div><div>this is my audio encode c file,</div><div><br></div><div><div>#include <stdint.h></div><div>#include <stdio.h></div><div>#include <stdlib.h></div><div>#include <libavcodec/avcodec.h></div><div>#include <libavutil/channel_layout.h></div><div>#include <libavutil/common.h></div><div>#include <libavutil/frame.h></div><div>#include <libavutil/samplefmt.h></div><div><br></div><div>/* check that a given sample format is supported by the encoder */</div><div><br></div><div>static int check_sample_fmt(const AVCodec *codec, enum AVSampleFormat sample_fmt)</div><div>{</div><div> const enum AVSampleFormat *p = codec->sample_fmts;</div><div> while (*p != AV_SAMPLE_FMT_NONE) {</div><div> if (*p == sample_fmt)</div><div> return 1;</div><div> p++;</div><div> }</div><div> return 0;</div><div>}</div><div>/* just pick the highest supported samplerate */</div><div><br></div><div>static int select_sample_rate(const AVCodec *codec)</div><div>{</div><div> const int *p;</div><div> int best_samplerate = 0;</div><div> if (!codec->supported_samplerates)</div><div> return 44100;</div><div> p = codec->supported_samplerates;</div><div> while (*p) {</div><div> if (!best_samplerate || abs(44100 - *p) < abs(44100 - best_samplerate))</div><div> best_samplerate = *p;</div><div> p++;</div><div> }</div><div> return best_samplerate;</div><div>}</div><div>/* select layout with the highest channel count */</div><div><br></div><div>static int select_channel_layout(const AVCodec *codec)</div><div>{</div><div> const uint64_t *p;</div><div> uint64_t best_ch_layout = 0;</div><div> int best_nb_channels = 0;</div><div> if (!codec->channel_layouts)</div><div> return AV_CH_LAYOUT_STEREO;</div><div> p = codec->channel_layouts;</div><div> while (*p) {</div><div> int nb_channels = av_get_channel_layout_nb_channels(*p);</div><div> if (nb_channels > best_nb_channels) {</div><div> best_ch_layout = *p;</div><div> best_nb_channels = nb_channels;</div><div> }</div><div> p++;</div><div> }</div><div> return best_ch_layout;</div><div>}</div><div>static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt,</div><div> FILE *output)</div><div>{</div><div> int ret;</div><div> /* send the frame for encoding */</div><div> ret = avcodec_send_frame(ctx, frame);</div><div> if (ret < 0) {</div><div> fprintf(stderr, "Error sending the frame to the encoder\n");</div><div> exit(1);</div><div> }</div><div> /* read all the available output packets (in general there may be any</div><div> * number of them */</div><div> while (ret >= 0) {</div><div> ret = avcodec_receive_packet(ctx, pkt);</div><div> if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)</div><div> return;</div><div> else if (ret < 0) {</div><div> fprintf(stderr, "Error encoding audio frame\n");</div><div> exit(1);</div><div> }</div><div> fwrite(pkt->data, 1, pkt->size, output);</div><div> av_packet_unref(pkt);</div><div> }</div><div>}</div><div>int main(int argc, char **argv)</div><div>{</div><div> const char *filename;</div><div> const AVCodec *codec;</div><div> AVCodecContext *c= NULL;</div><div> AVFrame *frame;</div><div> AVPacket *pkt;</div><div> int i, j, k, ret;</div><div> FILE *f;</div><div> uint16_t *samples;</div><div> float t, tincr;</div><div> if (argc <= 1) {</div><div> fprintf(stderr, "Usage: %s <output file>\n", argv[0]);</div><div> return 0;</div><div> }</div><div> filename = argv[1];</div><div> /* find the MP2 encoder */</div><div> </div><div> avcodec_register_all();</div><div><br></div><div> codec = avcodec_find_encoder(AV_CODEC_ID_MP2);</div><div> if (!codec) {</div><div> fprintf(stderr, "Codec not found\n");</div><div> exit(1);</div><div> }</div><div> c = avcodec_alloc_context3(codec);</div><div> if (!c) {</div><div> fprintf(stderr, "Could not allocate audio codec context\n");</div><div> exit(1);</div><div> }</div><div> /* put sample parameters */</div><div> c->bit_rate = 64000;</div><div> /* check that the encoder supports s16 pcm input */</div><div> c->sample_fmt = AV_SAMPLE_FMT_S16;</div><div> if (!check_sample_fmt(codec, c->sample_fmt)) {</div><div> fprintf(stderr, "Encoder does not support sample format %s",</div><div> av_get_sample_fmt_name(c->sample_fmt));</div><div> exit(1);</div><div> }</div><div> /* select other audio parameters supported by the encoder */</div><div> c->sample_rate = select_sample_rate(codec);</div><div> c->channel_layout = select_channel_layout(codec);</div><div> c->channels = av_get_channel_layout_nb_channels(c->channel_layout);</div><div> /* open it */</div><div> if (avcodec_open2(c, codec, NULL) < 0) {</div><div> fprintf(stderr, "Could not open codec\n");</div><div> exit(1);</div><div> }</div><div> f = fopen(filename, "wb");</div><div> if (!f) {</div><div> fprintf(stderr, "Could not open %s\n", filename);</div><div> exit(1);</div><div> }</div><div> /* packet for holding encoded output */</div><div> pkt = av_packet_alloc();</div><div> if (!pkt) {</div><div> fprintf(stderr, "could not allocate the packet\n");</div><div> exit(1);</div><div> }</div><div> /* frame containing input raw audio */</div><div> frame = av_frame_alloc();</div><div> if (!frame) {</div><div> fprintf(stderr, "Could not allocate audio frame\n");</div><div> exit(1);</div><div> }</div><div> frame->nb_samples = c->frame_size;</div><div> frame->format = c->sample_fmt;</div><div> frame->channel_layout = c->channel_layout;</div><div> /* allocate the data buffers */</div><div> ret = av_frame_get_buffer(frame, 0);</div><div> if (ret < 0) {</div><div> fprintf(stderr, "Could not allocate audio data buffers\n");</div><div> exit(1);</div><div> }</div><div> /* encode a single tone sound */</div><div> t = 0;</div><div> tincr = 2 * M_PI * 440.0 / c->sample_rate;</div><div> for (i = 0; i < 200; i++) {</div><div> /* make sure the frame is writable -- makes a copy if the encoder</div><div> * kept a reference internally */</div><div> ret = av_frame_make_writable(frame);</div><div> if (ret < 0)</div><div> exit(1);</div><div> samples = (uint16_t*)frame->data[0];</div><div> for (j = 0; j < c->frame_size; j++) {</div><div> samples[2*j] = (int)(sin(t) * 10000);</div><div> for (k = 1; k < c->channels; k++)</div><div> samples[2*j + k] = samples[2*j];</div><div> t += tincr;</div><div> }</div><div> encode(c, frame, pkt, f);</div><div> }</div><div> /* flush the encoder */</div><div> encode(c, NULL, pkt, f);</div><div> fclose(f);</div><div> av_frame_free(&frame);</div><div> av_packet_free(&pkt);</div><div> avcodec_free_context(&c);</div><div> return 0;</div><div>}</div><div><br></div></div><div>I am not getting exactly. </div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Aug 23, 2018 at 5:03 PM Strahinja Radman <<a href="mailto:dr.strashni@gmail.com">dr.strashni@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="EN-US" link="blue" vlink="#954F72"><div class="m_680854661276189716WordSection1"><p class="MsoNormal">Completely true, I should have been more clear. I thought that he wanted to play it in VLC or some other media player.</p><p class="MsoNormal"><u></u> <u></u></p><div style="border:none;border-top:solid #e1e1e1 1.0pt;padding:3.0pt 0in 0in 0in"><p class="MsoNormal" style="border:none;padding:0in"><b>From: </b><a href="mailto:ceffmpeg@gmail.com" target="_blank">Carl Eugen Hoyos</a><br><b>Sent: </b>Thursday, August 23, 2018 1:32 PM<br><b>To: </b><a href="mailto:libav-user@ffmpeg.org" target="_blank">This list is about using libavcodec, libavformat, libavutil, libavdevice and libavfilter.</a><br><b>Subject: </b>Re: [Libav-user] How to get back the original audio file</p></div><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">2018-08-23 13:10 GMT+02:00, Strahinja Radman <<a href="mailto:dr.strashni@gmail.com" target="_blank">dr.strashni@gmail.com</a>>:</p><p class="MsoNormal">> You basically wrote raw PCM into a plain file. That file is not usable</p><p class="MsoNormal">> without the right header.</p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">This is not true, many programs (including FFmpeg) handle raw</p><p class="MsoNormal">PCM audio just fine.</p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">Please do not top-post here, Carl Eugen</p><p class="MsoNormal">_______________________________________________</p><p class="MsoNormal">Libav-user mailing list</p><p class="MsoNormal"><a href="mailto:Libav-user@ffmpeg.org" target="_blank">Libav-user@ffmpeg.org</a></p><p class="MsoNormal"><a href="http://ffmpeg.org/mailman/listinfo/libav-user" target="_blank">http://ffmpeg.org/mailman/listinfo/libav-user</a></p><p class="MsoNormal"><u></u> <u></u></p></div></div>_______________________________________________<br>
Libav-user mailing list<br>
<a href="mailto:Libav-user@ffmpeg.org" target="_blank">Libav-user@ffmpeg.org</a><br>
<a href="http://ffmpeg.org/mailman/listinfo/libav-user" rel="noreferrer" target="_blank">http://ffmpeg.org/mailman/listinfo/libav-user</a><br>
</blockquote></div>