Advertisement
Guest User

Revision 1

a guest
Jan 3rd, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. void AudioLibav::DecodeAudio(std::vector<char> *inputAudio)
  2. {
  3.  
  4. //Wasteful?
  5. avcodec_register_all();
  6.  
  7. unsigned int audioVectorPosition = 0;
  8.  
  9. AVCodec *codec;
  10. AVCodecContext *codecContext = NULL;
  11. int length;
  12. FILE *memoutfile;
  13. uint8_t memBuffer[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
  14. AVPacket memPkt;
  15. AVFrame *decoded_frame_mem = NULL;
  16.  
  17. av_init_packet(&memPkt);
  18.  
  19. printf("Audio decoding\n");
  20.  
  21. /* find the mpeg audio decoder */
  22. codec = avcodec_find_decoder(AV_CODEC_ID_PCM_S16LE);
  23. if (!codec)
  24. {
  25. fprintf(stderr, "codec not found\n");
  26. exit(1);
  27. }
  28.  
  29. codecContext = avcodec_alloc_context3(codec);
  30.  
  31. #warning Bad assumption
  32. codecContext->channels = 2;
  33.  
  34. /* open it */
  35. if (avcodec_open2(codecContext, codec, NULL) < 0) {
  36. fprintf(stderr, "could not open codec\n");
  37. exit(1);
  38. }
  39.  
  40. memoutfile = fopen("testrawmem", "wb");
  41. if (!memoutfile) {
  42. av_free(codecContext);
  43. exit(1);
  44. }
  45.  
  46. /* decode until eof */
  47. memPkt.data = memBuffer;
  48. memPkt.size = AUDIO_INBUF_SIZE;
  49. memcpy(memBuffer, &inputAudio->at(0), AUDIO_INBUF_SIZE);
  50.  
  51. inputAudioVectorPosition = AUDIO_INBUF_SIZE;
  52.  
  53. while (memPkt.size > 0)
  54. {
  55. int got_frame_mem = 0;
  56.  
  57. if(!decoded_frame_mem)
  58. {
  59. if (!(decoded_frame_mem = avcodec_alloc_frame()))
  60. {
  61. fprintf(stderr, "out of memory\n");
  62. exit(1);
  63. }
  64. }
  65. else
  66. {
  67. avcodec_get_frame_defaults(decoded_frame_mem);
  68. }
  69.  
  70. length = avcodec_decode_audio4(codecContext, decoded_frame_mem, &got_frame_mem, &memPkt);
  71.  
  72. if (length < 0)
  73. {
  74. fprintf(stderr, "Error while decoding\n");
  75. exit(1);
  76. }
  77. if(got_frame_mem )
  78. {
  79. int data_size_mem = av_samples_get_buffer_size(NULL, codecContext->channels, decoded_frame_mem->nb_samples, codecContext->sample_fmt, 1);
  80.  
  81.  
  82. fwrite(decoded_frame_mem->data[0], 1, data_size_mem, memoutfile);
  83. }
  84.  
  85. memPkt.size -= length;
  86. memPkt.data += length;
  87.  
  88. if(memPkt.size < AUDIO_REFILL_THRESH)
  89. {
  90. memmove(memBuffer, memPkt.data, memPkt.size);
  91. memPkt.data = memBuffer;
  92. //std::cout << "Mempacket data: " << memPkt.data << " MemPacket size: " << memPkt.size << " memAUDIO BUFF - size: " << AUDIO_INBUF_SIZE - memPkt.size;
  93. if((inputAudio->size() - inputAudioVectorPosition) < (AUDIO_INBUF_SIZE - memPkt.size))
  94. {
  95. memcpy( (memPkt.data + memPkt.size) , &inputAudio->at(inputAudioVectorPosition), (inputAudio->size() - inputAudioVectorPosition));
  96. inputAudioVectorPosition = inputAudio->size() - 1;
  97. #warning Bad Assumption?
  98. //memlen = AUDIO_INBUF_SIZE - memPkt.size;
  99. length = 0;
  100. }
  101. else
  102. {
  103. memcpy( (memPkt.data + memPkt.size) , &inputAudio->at(inputAudioVectorPosition), AUDIO_INBUF_SIZE - memPkt.size);
  104. inputAudioVectorPosition = inputAudioVectorPosition + (AUDIO_INBUF_SIZE - memPkt.size);
  105. length = AUDIO_INBUF_SIZE - memPkt.size;
  106. }
  107.  
  108. if(length > 0)
  109. {
  110. memPkt.size += length;
  111. }
  112.  
  113. }
  114.  
  115. }
  116.  
  117. fclose(memoutfile);
  118.  
  119. avcodec_close(codecContext);
  120. av_free(codecContext);
  121. avcodec_free_frame(&decoded_frame_mem);
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement