<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
.MsoChpDefault
{mso-style-type:export-only;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style></head><body lang=EN-US link=blue vlink="#954F72"><div class=WordSection1><p class=MsoNormal>Read a frame one by one from bin file, restore the data to the same fields in AVFrame structure that you have read it from and send it to the encoder. </p><p class=MsoNormal><o:p> </o:p></p><div style='mso-element:para-border-div;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:ramana.jajula9@gmail.com">Ramana Jajula</a><br><b>Sent: </b>Thursday, August 23, 2018 2:10 PM<br><b>To: </b><a href="mailto:libav-user@ffmpeg.org">libav-user@ffmpeg.org</a><br><b>Subject: </b>Re: [Libav-user] How to get back the original audio file</p></div><p class=MsoNormal><o:p> </o:p></p><div><p class=MsoNormal>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, </p><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>this is my audio encode c file,</p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><div><p class=MsoNormal>#include <stdint.h></p></div><div><p class=MsoNormal>#include <stdio.h></p></div><div><p class=MsoNormal>#include <stdlib.h></p></div><div><p class=MsoNormal>#include <libavcodec/avcodec.h></p></div><div><p class=MsoNormal>#include <libavutil/channel_layout.h></p></div><div><p class=MsoNormal>#include <libavutil/common.h></p></div><div><p class=MsoNormal>#include <libavutil/frame.h></p></div><div><p class=MsoNormal>#include <libavutil/samplefmt.h></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>/* check that a given sample format is supported by the encoder */</p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>static int check_sample_fmt(const AVCodec *codec, enum AVSampleFormat sample_fmt)</p></div><div><p class=MsoNormal>{</p></div><div><p class=MsoNormal> const enum AVSampleFormat *p = codec->sample_fmts;</p></div><div><p class=MsoNormal> while (*p != AV_SAMPLE_FMT_NONE) {</p></div><div><p class=MsoNormal> if (*p == sample_fmt)</p></div><div><p class=MsoNormal> return 1;</p></div><div><p class=MsoNormal> p++;</p></div><div><p class=MsoNormal> }</p></div><div><p class=MsoNormal> return 0;</p></div><div><p class=MsoNormal>}</p></div><div><p class=MsoNormal>/* just pick the highest supported samplerate */</p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>static int select_sample_rate(const AVCodec *codec)</p></div><div><p class=MsoNormal>{</p></div><div><p class=MsoNormal> const int *p;</p></div><div><p class=MsoNormal> int best_samplerate = 0;</p></div><div><p class=MsoNormal> if (!codec->supported_samplerates)</p></div><div><p class=MsoNormal> return 44100;</p></div><div><p class=MsoNormal> p = codec->supported_samplerates;</p></div><div><p class=MsoNormal> while (*p) {</p></div><div><p class=MsoNormal> if (!best_samplerate || abs(44100 - *p) < abs(44100 - best_samplerate))</p></div><div><p class=MsoNormal> best_samplerate = *p;</p></div><div><p class=MsoNormal> p++;</p></div><div><p class=MsoNormal> }</p></div><div><p class=MsoNormal> return best_samplerate;</p></div><div><p class=MsoNormal>}</p></div><div><p class=MsoNormal>/* select layout with the highest channel count */</p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>static int select_channel_layout(const AVCodec *codec)</p></div><div><p class=MsoNormal>{</p></div><div><p class=MsoNormal> const uint64_t *p;</p></div><div><p class=MsoNormal> uint64_t best_ch_layout = 0;</p></div><div><p class=MsoNormal> int best_nb_channels = 0;</p></div><div><p class=MsoNormal> if (!codec->channel_layouts)</p></div><div><p class=MsoNormal> return AV_CH_LAYOUT_STEREO;</p></div><div><p class=MsoNormal> p = codec->channel_layouts;</p></div><div><p class=MsoNormal> while (*p) {</p></div><div><p class=MsoNormal> int nb_channels = av_get_channel_layout_nb_channels(*p);</p></div><div><p class=MsoNormal> if (nb_channels > best_nb_channels) {</p></div><div><p class=MsoNormal> best_ch_layout = *p;</p></div><div><p class=MsoNormal> best_nb_channels = nb_channels;</p></div><div><p class=MsoNormal> }</p></div><div><p class=MsoNormal> p++;</p></div><div><p class=MsoNormal> }</p></div><div><p class=MsoNormal> return best_ch_layout;</p></div><div><p class=MsoNormal>}</p></div><div><p class=MsoNormal>static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt,</p></div><div><p class=MsoNormal> FILE *output)</p></div><div><p class=MsoNormal>{</p></div><div><p class=MsoNormal> int ret;</p></div><div><p class=MsoNormal> /* send the frame for encoding */</p></div><div><p class=MsoNormal> ret = avcodec_send_frame(ctx, frame);</p></div><div><p class=MsoNormal> if (ret < 0) {</p></div><div><p class=MsoNormal> fprintf(stderr, "Error sending the frame to the encoder\n");</p></div><div><p class=MsoNormal> exit(1);</p></div><div><p class=MsoNormal> }</p></div><div><p class=MsoNormal> /* read all the available output packets (in general there may be any</p></div><div><p class=MsoNormal> * number of them */</p></div><div><p class=MsoNormal> while (ret >= 0) {</p></div><div><p class=MsoNormal> ret = avcodec_receive_packet(ctx, pkt);</p></div><div><p class=MsoNormal> if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)</p></div><div><p class=MsoNormal> return;</p></div><div><p class=MsoNormal> else if (ret < 0) {</p></div><div><p class=MsoNormal> fprintf(stderr, "Error encoding audio frame\n");</p></div><div><p class=MsoNormal> exit(1);</p></div><div><p class=MsoNormal> }</p></div><div><p class=MsoNormal> fwrite(pkt->data, 1, pkt->size, output);</p></div><div><p class=MsoNormal> av_packet_unref(pkt);</p></div><div><p class=MsoNormal> }</p></div><div><p class=MsoNormal>}</p></div><div><p class=MsoNormal>int main(int argc, char **argv)</p></div><div><p class=MsoNormal>{</p></div><div><p class=MsoNormal> const char *filename;</p></div><div><p class=MsoNormal> const AVCodec *codec;</p></div><div><p class=MsoNormal> AVCodecContext *c= NULL;</p></div><div><p class=MsoNormal> AVFrame *frame;</p></div><div><p class=MsoNormal> AVPacket *pkt;</p></div><div><p class=MsoNormal> int i, j, k, ret;</p></div><div><p class=MsoNormal> FILE *f;</p></div><div><p class=MsoNormal> uint16_t *samples;</p></div><div><p class=MsoNormal> float t, tincr;</p></div><div><p class=MsoNormal> if (argc <= 1) {</p></div><div><p class=MsoNormal> fprintf(stderr, "Usage: %s <output file>\n", argv[0]);</p></div><div><p class=MsoNormal> return 0;</p></div><div><p class=MsoNormal> }</p></div><div><p class=MsoNormal> filename = argv[1];</p></div><div><p class=MsoNormal> /* find the MP2 encoder */</p></div><div><p class=MsoNormal> </p></div><div><p class=MsoNormal> avcodec_register_all();</p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal> codec = avcodec_find_encoder(AV_CODEC_ID_MP2);</p></div><div><p class=MsoNormal> if (!codec) {</p></div><div><p class=MsoNormal> fprintf(stderr, "Codec not found\n");</p></div><div><p class=MsoNormal> exit(1);</p></div><div><p class=MsoNormal> }</p></div><div><p class=MsoNormal> c = avcodec_alloc_context3(codec);</p></div><div><p class=MsoNormal> if (!c) {</p></div><div><p class=MsoNormal> fprintf(stderr, "Could not allocate audio codec context\n");</p></div><div><p class=MsoNormal> exit(1);</p></div><div><p class=MsoNormal> }</p></div><div><p class=MsoNormal> /* put sample parameters */</p></div><div><p class=MsoNormal> c->bit_rate = 64000;</p></div><div><p class=MsoNormal> /* check that the encoder supports s16 pcm input */</p></div><div><p class=MsoNormal> c->sample_fmt = AV_SAMPLE_FMT_S16;</p></div><div><p class=MsoNormal> if (!check_sample_fmt(codec, c->sample_fmt)) {</p></div><div><p class=MsoNormal> fprintf(stderr, "Encoder does not support sample format %s",</p></div><div><p class=MsoNormal> av_get_sample_fmt_name(c->sample_fmt));</p></div><div><p class=MsoNormal> exit(1);</p></div><div><p class=MsoNormal> }</p></div><div><p class=MsoNormal> /* select other audio parameters supported by the encoder */</p></div><div><p class=MsoNormal> c->sample_rate = select_sample_rate(codec);</p></div><div><p class=MsoNormal> c->channel_layout = select_channel_layout(codec);</p></div><div><p class=MsoNormal> c->channels = av_get_channel_layout_nb_channels(c->channel_layout);</p></div><div><p class=MsoNormal> /* open it */</p></div><div><p class=MsoNormal> if (avcodec_open2(c, codec, NULL) < 0) {</p></div><div><p class=MsoNormal> fprintf(stderr, "Could not open codec\n");</p></div><div><p class=MsoNormal> exit(1);</p></div><div><p class=MsoNormal> }</p></div><div><p class=MsoNormal> f = fopen(filename, "wb");</p></div><div><p class=MsoNormal> if (!f) {</p></div><div><p class=MsoNormal> fprintf(stderr, "Could not open %s\n", filename);</p></div><div><p class=MsoNormal> exit(1);</p></div><div><p class=MsoNormal> }</p></div><div><p class=MsoNormal> /* packet for holding encoded output */</p></div><div><p class=MsoNormal> pkt = av_packet_alloc();</p></div><div><p class=MsoNormal> if (!pkt) {</p></div><div><p class=MsoNormal> fprintf(stderr, "could not allocate the packet\n");</p></div><div><p class=MsoNormal> exit(1);</p></div><div><p class=MsoNormal> }</p></div><div><p class=MsoNormal> /* frame containing input raw audio */</p></div><div><p class=MsoNormal> frame = av_frame_alloc();</p></div><div><p class=MsoNormal> if (!frame) {</p></div><div><p class=MsoNormal> fprintf(stderr, "Could not allocate audio frame\n");</p></div><div><p class=MsoNormal> exit(1);</p></div><div><p class=MsoNormal> }</p></div><div><p class=MsoNormal> frame->nb_samples = c->frame_size;</p></div><div><p class=MsoNormal> frame->format = c->sample_fmt;</p></div><div><p class=MsoNormal> frame->channel_layout = c->channel_layout;</p></div><div><p class=MsoNormal> /* allocate the data buffers */</p></div><div><p class=MsoNormal> ret = av_frame_get_buffer(frame, 0);</p></div><div><p class=MsoNormal> if (ret < 0) {</p></div><div><p class=MsoNormal> fprintf(stderr, "Could not allocate audio data buffers\n");</p></div><div><p class=MsoNormal> exit(1);</p></div><div><p class=MsoNormal> }</p></div><div><p class=MsoNormal> /* encode a single tone sound */</p></div><div><p class=MsoNormal> t = 0;</p></div><div><p class=MsoNormal> tincr = 2 * M_PI * 440.0 / c->sample_rate;</p></div><div><p class=MsoNormal> for (i = 0; i < 200; i++) {</p></div><div><p class=MsoNormal> /* make sure the frame is writable -- makes a copy if the encoder</p></div><div><p class=MsoNormal> * kept a reference internally */</p></div><div><p class=MsoNormal> ret = av_frame_make_writable(frame);</p></div><div><p class=MsoNormal> if (ret < 0)</p></div><div><p class=MsoNormal> exit(1);</p></div><div><p class=MsoNormal> samples = (uint16_t*)frame->data[0];</p></div><div><p class=MsoNormal> for (j = 0; j < c->frame_size; j++) {</p></div><div><p class=MsoNormal> samples[2*j] = (int)(sin(t) * 10000);</p></div><div><p class=MsoNormal> for (k = 1; k < c->channels; k++)</p></div><div><p class=MsoNormal> samples[2*j + k] = samples[2*j];</p></div><div><p class=MsoNormal> t += tincr;</p></div><div><p class=MsoNormal> }</p></div><div><p class=MsoNormal> encode(c, frame, pkt, f);</p></div><div><p class=MsoNormal> }</p></div><div><p class=MsoNormal> /* flush the encoder */</p></div><div><p class=MsoNormal> encode(c, NULL, pkt, f);</p></div><div><p class=MsoNormal> fclose(f);</p></div><div><p class=MsoNormal> av_frame_free(&frame);</p></div><div><p class=MsoNormal> av_packet_free(&pkt);</p></div><div><p class=MsoNormal> avcodec_free_context(&c);</p></div><div><p class=MsoNormal> return 0;</p></div><div><p class=MsoNormal>}</p></div><div><p class=MsoNormal><o:p> </o:p></p></div></div><div><p class=MsoNormal>I am not getting exactly. </p></div></div><p class=MsoNormal><o:p> </o:p></p><div><div><p class=MsoNormal>On Thu, Aug 23, 2018 at 5:03 PM Strahinja Radman <<a href="mailto:dr.strashni@gmail.com">dr.strashni@gmail.com</a>> wrote:</p></div><blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in'><div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>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 style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> </p><div style='border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in'><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><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 style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> </p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>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 style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>> You basically wrote raw PCM into a plain file. That file is not usable</p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>> without the right header.</p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> </p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>This is not true, many programs (including FFmpeg) handle raw</p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>PCM audio just fine.</p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> </p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>Please do not top-post here, Carl Eugen</p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>_______________________________________________</p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>Libav-user mailing list</p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><a href="mailto:Libav-user@ffmpeg.org" target="_blank">Libav-user@ffmpeg.org</a></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><a href="http://ffmpeg.org/mailman/listinfo/libav-user" target="_blank">http://ffmpeg.org/mailman/listinfo/libav-user</a></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> </p></div></div></blockquote></div><p class=MsoNormal style='margin-left:4.8pt'>_______________________________________________<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" target="_blank">http://ffmpeg.org/mailman/listinfo/libav-user</a></p><p class=MsoNormal><o:p> </o:p></p></div></body></html>