Advertisement
Guest User

Using std::vector as buffer

a guest
Aug 1st, 2013
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.53 KB | None | 0 0
  1. //This example will take in an MP2 file and decode it
  2. int main()
  3. {
  4.     std::cout << "Decode MP2 file " << "test.mp2" << " to WAV. \n"
  5.     << "The wav file name is decodedAudio.wav\n";
  6.    
  7.     //First we will register the codecs availible from FFMPEG
  8.     avcodec_register_all();
  9.    
  10.     //We will find the decoder for MP3
  11.     AVCodec *mp2Codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
  12.    
  13.     //If the codec was not found then fail.
  14.     if(!mp2Codec)
  15.     {
  16.         std::cout << "Unable to find the MP2 decoder.\n";
  17.         exit(-1);
  18.     }
  19.    
  20.     //Since the codec was succefully found we now will allocate the
  21.     //memory required to set different properties on the decoder
  22.     AVCodecContext *mp2DecoderProperties = avcodec_alloc_context3(mp2Codec);
  23.    
  24.     //Check that the AVCodecContext successfully was allocated
  25.     //Otherwise fail out of the example
  26.     if(!mp2DecoderProperties)
  27.     {
  28.         std::cout << "Unable to allocate enough memory for mp2DecoderProperties\n";
  29.     }
  30.    
  31.     //Now we know the codec is availible and that the mp3DecoderProperties
  32.     //is allocated, lets open the decoder if not fail out
  33.     if(avcodec_open2(mp2DecoderProperties, mp2Codec, NULL) < 0)
  34.     {
  35.         std::cout << "Unable to open the mp2Decoder\n";
  36.         exit(-2);
  37.     }
  38.    
  39.     //Lets open the file stream to the file we wish to decode
  40.     std::ifstream inputAudioFile;
  41.     inputAudioFile.open("test.mp2");
  42.     inputAudioFile.exceptions(std::ifstream::badbit | std::ifstream::failbit | std::ifstream::eofbit);
  43.    
  44.     //OPTION 1 Decode entire file in memory
  45.     //Create an input buffer for the decoder to read through
  46.     std::vector<uint8_t> rawMP3File;
  47.    
  48.     //Seek the entire file to find it's complete size
  49.     inputAudioFile.seekg(0, std::ios::end);
  50.     std::streampos rawInputFileLength(inputAudioFile.tellg());
  51.    
  52.     if (rawInputFileLength)
  53.     {
  54.         //Seek back to the beginning of the audio file
  55.         inputAudioFile.seekg(0, std::ios::beg);
  56.        
  57.         //Resize the vector to accomodate for the file
  58.         rawMP3File.resize(static_cast<std::size_t>(rawInputFileLength) + FF_INPUT_BUFFER_PADDING_SIZE);
  59.        
  60.         //Read in the file to the vector
  61.         inputAudioFile.read((char *)&rawMP3File.front(), static_cast<std::size_t>(rawInputFileLength));
  62.         inputAudioFile.close();
  63.     }
  64.     else
  65.     {
  66.         std::cout << "Unable to determine the size of the file.\n";
  67.         inputAudioFile.close();
  68.         exit(-3);
  69.     }
  70.  
  71.     //Lets create a AVPacket and set the whole file as the input buffer
  72.     AVPacket currentProcessingAudioPacket;
  73.    
  74.     //Initialize the AVPacket for use
  75.     av_init_packet(&currentProcessingAudioPacket);
  76.    
  77.     //Set the packet data pointer and size (in our case the whole file)
  78.     currentProcessingAudioPacket.data = &rawMP3File.front();
  79.     currentProcessingAudioPacket.size = rawMP3File.size();
  80.    
  81.     //The decoded frame is the remaining audio which will then be
  82.     //written to disk. (or otherwise reencoded to another format)
  83.     //Attempt to allocate a AVFrame
  84.     AVFrame *decodedFrame = avcodec_alloc_frame();
  85.    
  86.     //Ensure that that the frame was able to be allocated
  87.     if(!decodedFrame)
  88.     {
  89.         std::cout << "Could not allocate memory for the AVFrame\n";
  90.         exit(-5);
  91.     }
  92.    
  93.     //Used to tell the number of bytes that have been successfully processed
  94.     int bytesProcessed = 0;
  95.    
  96.     //Open the output file for writing
  97.     std::ofstream decodedAudioFile;
  98.     decodedAudioFile.open("decodedAudio");
  99.    
  100.     int loop = 0;
  101.     //Now we get to start processing the audio
  102.     while((currentProcessingAudioPacket.size - FF_INPUT_BUFFER_PADDING_SIZE) > 0)
  103.     {
  104.         std::cout << "Buffer Size is: " << av_samples_get_buffer_size(NULL, mp2DecoderProperties->channels,
  105.                                                                       decodedFrame->nb_samples, mp2DecoderProperties->sample_fmt, 1) << '\n';
  106.         //Flag to notify when a frame is completely decoded and
  107.         //ready to be written to disk.
  108.         int completedFrame = 0;
  109.        
  110.         //Set the AVFrame to it's default values
  111.         avcodec_get_frame_defaults(decodedFrame);
  112.  
  113.        
  114.         //Decode a frame's worth on audio
  115.         bytesProcessed = avcodec_decode_audio4(mp2DecoderProperties, decodedFrame, &completedFrame, &currentProcessingAudioPacket);
  116.        
  117.         //If there was an error, do error handling here or fatal fail out
  118.         if(bytesProcessed < 0)
  119.         {
  120.             std::cout << "An error occured while decoding the Audio packet\n";
  121.             exit(-6);
  122.         }
  123.        
  124.         //If we successfully got a complete decoded frame write it to disk
  125.         if(completedFrame)
  126.         {
  127.             //Write the result to disk
  128.             decodedAudioFile.write((char *) decodedFrame->data[0], av_samples_get_buffer_size(NULL, mp2DecoderProperties->channels,
  129.                                                                                             decodedFrame->nb_samples, mp2DecoderProperties->sample_fmt, 1));
  130.         }
  131.        
  132.         currentProcessingAudioPacket.size -= bytesProcessed;
  133.         currentProcessingAudioPacket.data += bytesProcessed;
  134.         currentProcessingAudioPacket.dts =
  135.         currentProcessingAudioPacket.pts = AV_NOPTS_VALUE;
  136.        
  137.         std::cout  << " on loop " << loop++ << '\n';
  138.     }
  139.    
  140.     decodedAudioFile.close();
  141.    
  142.     avcodec_close(mp2DecoderProperties);
  143.     av_free(mp2DecoderProperties);
  144.     avcodec_free_frame(&decodedFrame);
  145.    
  146.     return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement