Advertisement
Guest User

Attempt to readin from a std::vector<char>

a guest
Jan 2nd, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.53 KB | None | 0 0
  1. void AudioLibav::DecodeAudio(std::vector<char> *audio)
  2. {
  3.     //Wasteful?
  4.     avcodec_register_all();
  5.    
  6.    
  7.     int vectorPosition = 0;
  8.    
  9.     AVCodec *codec;
  10.     AVCodecContext *c= NULL;
  11.     int len, memlen;
  12.     FILE *f, *outfile;
  13.     uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
  14.     uint8_t memBuffer[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
  15.     AVPacket avpkt, memPkt;
  16.     AVFrame *decoded_frame = NULL;
  17.    
  18.     av_init_packet(&avpkt);
  19.    
  20.     printf("Audio decoding\n");
  21.    
  22.    
  23.     /* find the mpeg audio decoder */
  24.     codec = avcodec_find_decoder(AV_CODEC_ID_PCM_S16LE);
  25.     if (!codec) {
  26.         fprintf(stderr, "codec not found\n");
  27.         exit(1);
  28.     }
  29.    
  30.     c = avcodec_alloc_context3(codec);
  31.    
  32.    
  33.     //Bad assumption
  34.     c->channels = 2;
  35.    
  36.     /* open it */
  37.     if (avcodec_open2(c, codec, NULL) < 0) {
  38.         fprintf(stderr, "could not open codec\n");
  39.         exit(1);
  40.     }
  41.    
  42.     f = fopen("test.wav", "rb");
  43.     if (!f) {
  44.         //fprintf(stderr, "could not open %s\n", filename);
  45.         exit(1);
  46.     }
  47.     outfile = fopen("testraw", "wb");
  48.     if (!outfile) {
  49.         av_free(c);
  50.         exit(1);
  51.     }
  52.    
  53.     /* decode until eof */
  54.     //avpkt.data = inbuf;
  55.     //avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
  56.     memPkt.data = memBuffer;
  57.     if(audio->size() < AUDIO_INBUF_SIZE)
  58.     {
  59.         memPkt.size = audio->size();
  60.         memcpy(memBuffer, &audio->at(0), audio->size());
  61.         vectorPosition = audio->size();
  62.     }
  63.     else
  64.     {
  65.         memPkt.size = AUDIO_INBUF_SIZE;
  66.         memcpy(memBuffer, &audio->at(0), AUDIO_INBUF_SIZE);
  67.         vectorPosition += AUDIO_INBUF_SIZE;
  68.     }
  69.    
  70.     std::cout << "AVPacket Size: " << avpkt.size;
  71.    
  72.     //while (avpkt.size > 0) {
  73.     while (memPkt.size > 0) {
  74.         std::cout << "Mempacket size: " << memPkt.size << '\n';
  75.         int got_frame = 0;
  76.        
  77.         if (!decoded_frame) {
  78.             if (!(decoded_frame = avcodec_alloc_frame())) {
  79.                 fprintf(stderr, "out of memory\n");
  80.                 exit(1);
  81.             }
  82.         } else
  83.             avcodec_get_frame_defaults(decoded_frame);
  84.        
  85.         //len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
  86.         memlen = avcodec_decode_audio4(c, decoded_frame, &got_frame, &memPkt);
  87.         if (memlen < 0) {
  88.             fprintf(stderr, "Error while decoding\n");
  89.             exit(1);
  90.         }
  91.         if (got_frame) {
  92.             /* if a frame has been decoded, output it */
  93.             int data_size = av_samples_get_buffer_size(NULL, c->channels,
  94.                                                        decoded_frame->nb_samples,
  95.                                                        c->sample_fmt, 1);
  96.             fwrite(decoded_frame->data[0], 1, data_size, outfile);
  97.         }
  98.         //avpkt.size -= len;
  99.         //avpkt.data += len;
  100.         memPkt.size -= memlen;
  101.         memPkt.data += memlen;
  102.        
  103.         if(memPkt.size < AUDIO_REFILL_THRESH)
  104.         {
  105.             memmove(memBuffer, memPkt.data, memPkt.size);
  106.            
  107.             if((audio->size() - vectorPosition) < (AUDIO_INBUF_SIZE - memPkt.size) )
  108.             {
  109.                 memcpy(memBuffer, &audio->at(vectorPosition), (audio->size() - vectorPosition));
  110.                 memlen =  (audio->size() - vectorPosition);
  111.                 //vectorPosition = audio->size();
  112.             }
  113.             else
  114.             {
  115.                 memcpy(memPkt.data + memPkt.size, &audio->at(vectorPosition), AUDIO_INBUF_SIZE - memPkt.size);
  116.                 vectorPosition += AUDIO_INBUF_SIZE - memPkt.size;
  117.                 memlen = AUDIO_INBUF_SIZE - memPkt.size;
  118.             }
  119.            
  120.             if(memlen > 0)
  121.             {
  122.                 memPkt.size += memlen;
  123.             }
  124.            
  125.         }
  126.         //if (avpkt.size < AUDIO_REFILL_THRESH) {
  127.             /* Refill the input buffer, to avoid trying to decode
  128.              * incomplete frames. Instead of this, one could also use
  129.              * a parser, or use a proper container format through
  130.              * libavformat. */
  131.         //    memmove(inbuf, avpkt.data, avpkt.size);
  132.         //    avpkt.data = inbuf;
  133.         //    len = fread(avpkt.data + avpkt.size, 1,
  134.         //                AUDIO_INBUF_SIZE - avpkt.size, f);
  135.         //    if (len > 0)
  136.         //        avpkt.size += len;
  137.         //}
  138.     }
  139.    
  140.     fclose(outfile);
  141.     fclose(f);
  142.    
  143.     avcodec_close(c);
  144.     av_free(c);
  145.     avcodec_free_frame(&decoded_frame);
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement