title: mp3 decoding using ffmpeg API (Header missing)
body: <p>i have been trying to decode an MP3 file to pcm, using ffmpeg API, but i keep getting an error </p>
  
  <p>[mp3 @ 0x8553020]Header missing</p>
  
  <p>this is the code i use :</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  #ifdef HAVE_AV_CONFIG_H
  #undef HAVE_AV_CONFIG_H
  #endif
  
  #include "libavcodec/avcodec.h"
  #include "libavutil/mathematics.h"
  
  #define INBUF_SIZE 4096
  #define AUDIO_INBUF_SIZE 20480
  #define AUDIO_REFILL_THRESH 4096
  
  
  static void audio_decode_example(const char *outfilename, const char *filename)
  {
      AVCodec *codec;
      AVCodecContext *c= NULL;
      int out_size, len;
      FILE *f, *outfile;
      uint8_t *outbuf;
      uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
      AVPacket avpkt;
  
      av_init_packet(&amp;avpkt);
  
      printf("Audio decoding\n");
  
      /* find the mpeg audio decoder */
      codec = avcodec_find_decoder(CODEC_ID_MP3ON4);
      if (!codec) {
      fprintf(stderr, "codec not found\n");
      exit(1);
      }
  
      c= avcodec_alloc_context();
  
      /* open it */
      if (avcodec_open(c, codec) &lt; 0) {
      fprintf(stderr, "could not open codec\n");
      exit(1);
      }
  
      outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
  
      f = fopen(filename, "rb");
      if (!f) {
      fprintf(stderr, "could not open %s\n", filename);
      exit(1);
      }
      outfile = fopen(outfilename, "wb");
      if (!outfile) {
      av_free(c);
      exit(1);
      }
  
      /* decode until eof */
      avpkt.data = inbuf;
      avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
  
      while (avpkt.size &gt; 0) {
      out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
      len = avcodec_decode_audio3(c, (short *)outbuf, &amp;out_size, &amp;avpkt);
      if (len &lt; 0) {
          fprintf(stderr, "Error while decoding\n");
          exit(1);
      }
      if (out_size &gt; 0) {
          /* if a frame has been decoded, output it */
          fwrite(outbuf, 1, out_size, outfile);
      }
      avpkt.size -= len;
      avpkt.data += len;
      if (avpkt.size &lt; AUDIO_REFILL_THRESH) {
          /* Refill the input buffer, to avoid trying to decode
           * incomplete frames. Instead of this, one could also use
           * a parser, or use a proper container format through
           * libavformat. */
          memmove(inbuf, avpkt.data, avpkt.size);
          avpkt.data = inbuf;
          len = fread(avpkt.data + avpkt.size, 1,
                      AUDIO_INBUF_SIZE - avpkt.size, f);
          if (len &gt; 0)
              avpkt.size += len;
      }
      }
  
      fclose(outfile);
      fclose(f);
      free(outbuf);
  
      avcodec_close(c);
      av_free(c);
  }
  
  int main(int argc, char **argv)
  {
      const char *filename;
  
      /* must be called before using avcodec lib */
      avcodec_init();
  
      /* register all the codecs */
      avcodec_register_all();
  
      audio_decode_example("test.wav", argv[1]);
  
      return 0;
  }
  </code></pre>
  
  <p>when i use the same code to directly play the sound, like this :</p>
  
  <pre><code>if (out_size &gt; 0) {
      /* if a frame has been decoded, output it *
      play_sound(outbuf, out_size);
  }
  </code></pre>
  
  <p>i have no problem at all with some files, other mp3 files just gives an error without even starting ... is there any ideas ?</p>
  
  <p>PS: this code is from libavcodec/api-example.c , modified as needed</p>