<div dir="ltr"><div>Hi experts,</div><div>This is my first mail to ffmpeg , apologies if make some mistakes.</div><div>I tried many online example codes for decoding aac audio to wav file.<br>including example codes which is for MP2 codec.</div><div>below is one such example code , it doubles the decoded file size (.wav) but not playable.<br>Can any one point in right direction or help me in correcting the below code.</div><div>Thanks in advance </div><div><br></div><div><br></div><div><br>#include <stdio.h><br>#include <stdlib.h><br>#include <string.h></div><div>#include <libavutil/frame.h><br>#include <libavutil/mem.h></div><div>#include <libavcodec/avcodec.h></div><div>#define AUDIO_INBUF_SIZE 4096<br>#define AUDIO_REFILL_THRESH 4096</div><div> static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame,<br>  FILE *outfile)<br>{<br> int i, ch;<br> int ret, data_size;<br> <br> /* send the packet with the compressed data to the decoder */<br> ret = avcodec_send_packet(dec_ctx, pkt);<br> if (ret < 0) {<br>  fprintf(stderr, "Error submitting the packet to the decoder\n");<br>  exit(1);<br> }</div><div> /* read all the output frames (in general there may be any number of them */<br> while (ret >= 0) <br> {<br>  ret = avcodec_receive_frame(dec_ctx, frame);<br>  if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)<br>   return;<br>  else if (ret < 0) <br>  {<br>   fprintf(stderr, "Error during decoding\n");<br>   exit(1);<br>  }<br>  data_size = av_get_bytes_per_sample(dec_ctx->sample_fmt);<br>  if (data_size < 0) {<br>   /* This should not occur, checking just for paranoia */<br>   fprintf(stderr, "Failed to calculate data size\n");<br>   exit(1);<br>  }<br>  for (i = 0; i < frame->nb_samples; i++)<br>   for (ch = 0; ch < dec_ctx->channels; ch++)<br>    fwrite(frame->data[ch] + data_size * i, 1, data_size, outfile);<br> }<br>}</div><div>int main(int argc, char **argv)<br>{<br> const char *outfilename, *filename;<br> const AVCodec *codec;<br> AVCodecContext *c = NULL;<br> AVCodecParserContext *parser = NULL;<br> int len, ret;<br> FILE *f, *outfile;<br> uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];<br> uint8_t *data;<br> size_t   data_size;<br> AVPacket *pkt;<br> AVFrame *decoded_frame = NULL;</div><div> if (argc <= 2) {<br>  fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);<br>  exit(0);<br> }<br> filename = argv[1];<br> outfilename = argv[2];<br> pkt = av_packet_alloc();<br> <br> /* find the MPEG audio decoder */<br> codec = avcodec_find_decoder(AV_CODEC_ID_AAC);<br> if (!codec) {<br>  fprintf(stderr, "Codec not found\n");<br>  exit(1);<br> }</div><div> parser = av_parser_init(codec->id);<br> if (!parser) {<br>  fprintf(stderr, "Parser not found\n");<br>  exit(1);<br> }</div><div> c = avcodec_alloc_context3(codec);<br> c->channels = 2;<br> c->sample_rate = 44100;<br> c->bit_rate = 32;<br> if (!c) {<br>  fprintf(stderr, "Could not allocate audio codec context\n");<br>  exit(1);<br> }</div><div> /* open it */<br> if (avcodec_open2(c, codec, NULL) < 0) {<br>  fprintf(stderr, "Could not open codec\n");<br>  exit(1);<br> }</div><div> f = fopen(filename, "rb");<br> if (!f) {<br>  fprintf(stderr, "Could not open %s\n", filename);<br>  exit(1);<br> }<br> outfile = fopen(outfilename, "wb");<br> if (!outfile) {<br>  av_free(c);<br>  exit(1);<br> }</div><div> /* decode until eof */<br> data = inbuf;<br> data_size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);<br> while (data_size > 0) {<br>  if (!decoded_frame) {<br>   if (!(decoded_frame = av_frame_alloc())) {<br>    fprintf(stderr, "Could not allocate audio frame\n");<br>    exit(1);<br>   }<br>  }</div><div>  ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,<br>   data, data_size,<br>   AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);<br>  if (ret < 0) {<br>   fprintf(stderr, "Error while parsing\n");<br>   exit(1);<br>  }<br>  data += ret;<br>  data_size -= ret;</div><div>  if (pkt->size)<br>   decode(c, pkt, decoded_frame, outfile);</div><div>  if (data_size < AUDIO_REFILL_THRESH) {<br>   memmove(inbuf, data, data_size);<br>   data = inbuf;<br>   len = fread(data + data_size, 1,<br>    AUDIO_INBUF_SIZE - data_size, f);<br>   if (len > 0)<br>    data_size += len;<br>  }<br> }</div><div> /* flush the decoder */<br> pkt->data = NULL;<br> pkt->size = 0;<br> decode(c, pkt, decoded_frame, outfile);</div><div> fclose(outfile);<br> fclose(f);</div><div> avcodec_free_context(&c);<br> av_parser_close(parser);<br> av_frame_free(&decoded_frame);<br> av_packet_free(&pkt);</div><div> return 0;<br>}</div><div> <br>Regards,<br>Aasim<br>+91 8197948544<br clear="all"></div><div><div class="gmail_signature" dir="ltr"><div dir="ltr">Regards,<div>Aasim</div><div>+91 8197948544</div></div></div></div></div>