Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This example will take in an MP2 file and decode it
- int main()
- {
- std::cout << "Decode MP2 file " << "test.mp2" << " to WAV. \n"
- << "The wav file name is decodedAudio.wav\n";
- //First we will register the codecs availible from FFMPEG
- avcodec_register_all();
- //We will find the decoder for MP3
- AVCodec *mp2Codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
- //If the codec was not found then fail.
- if(!mp2Codec)
- {
- std::cout << "Unable to find the MP2 decoder.\n";
- exit(-1);
- }
- //Since the codec was succefully found we now will allocate the
- //memory required to set different properties on the decoder
- AVCodecContext *mp2DecoderProperties = avcodec_alloc_context3(mp2Codec);
- //Check that the AVCodecContext successfully was allocated
- //Otherwise fail out of the example
- if(!mp2DecoderProperties)
- {
- std::cout << "Unable to allocate enough memory for mp2DecoderProperties\n";
- }
- //Now we know the codec is availible and that the mp3DecoderProperties
- //is allocated, lets open the decoder if not fail out
- if(avcodec_open2(mp2DecoderProperties, mp2Codec, NULL) < 0)
- {
- std::cout << "Unable to open the mp2Decoder\n";
- exit(-2);
- }
- //Lets open the file stream to the file we wish to decode
- std::ifstream inputAudioFile;
- inputAudioFile.open("test.mp2");
- inputAudioFile.exceptions(std::ifstream::badbit | std::ifstream::failbit | std::ifstream::eofbit);
- //OPTION 1 Decode entire file in memory
- //Create an input buffer for the decoder to read through
- std::vector<uint8_t> rawMP3File;
- //Seek the entire file to find it's complete size
- inputAudioFile.seekg(0, std::ios::end);
- std::streampos rawInputFileLength(inputAudioFile.tellg());
- if (rawInputFileLength)
- {
- //Seek back to the beginning of the audio file
- inputAudioFile.seekg(0, std::ios::beg);
- //Resize the vector to accomodate for the file
- rawMP3File.resize(static_cast<std::size_t>(rawInputFileLength) + FF_INPUT_BUFFER_PADDING_SIZE);
- //Read in the file to the vector
- inputAudioFile.read((char *)&rawMP3File.front(), static_cast<std::size_t>(rawInputFileLength));
- inputAudioFile.close();
- }
- else
- {
- std::cout << "Unable to determine the size of the file.\n";
- inputAudioFile.close();
- exit(-3);
- }
- //Lets create a AVPacket and set the whole file as the input buffer
- AVPacket currentProcessingAudioPacket;
- //Initialize the AVPacket for use
- av_init_packet(¤tProcessingAudioPacket);
- //Set the packet data pointer and size (in our case the whole file)
- currentProcessingAudioPacket.data = &rawMP3File.front();
- currentProcessingAudioPacket.size = rawMP3File.size();
- //The decoded frame is the remaining audio which will then be
- //written to disk. (or otherwise reencoded to another format)
- //Attempt to allocate a AVFrame
- AVFrame *decodedFrame = avcodec_alloc_frame();
- //Ensure that that the frame was able to be allocated
- if(!decodedFrame)
- {
- std::cout << "Could not allocate memory for the AVFrame\n";
- exit(-5);
- }
- //Used to tell the number of bytes that have been successfully processed
- int bytesProcessed = 0;
- //Open the output file for writing
- std::ofstream decodedAudioFile;
- decodedAudioFile.open("decodedAudio");
- int loop = 0;
- //Now we get to start processing the audio
- while((currentProcessingAudioPacket.size - FF_INPUT_BUFFER_PADDING_SIZE) > 0)
- {
- std::cout << "Buffer Size is: " << av_samples_get_buffer_size(NULL, mp2DecoderProperties->channels,
- decodedFrame->nb_samples, mp2DecoderProperties->sample_fmt, 1) << '\n';
- //Flag to notify when a frame is completely decoded and
- //ready to be written to disk.
- int completedFrame = 0;
- //Set the AVFrame to it's default values
- avcodec_get_frame_defaults(decodedFrame);
- //Decode a frame's worth on audio
- bytesProcessed = avcodec_decode_audio4(mp2DecoderProperties, decodedFrame, &completedFrame, ¤tProcessingAudioPacket);
- //If there was an error, do error handling here or fatal fail out
- if(bytesProcessed < 0)
- {
- std::cout << "An error occured while decoding the Audio packet\n";
- exit(-6);
- }
- //If we successfully got a complete decoded frame write it to disk
- if(completedFrame)
- {
- //Write the result to disk
- decodedAudioFile.write((char *) decodedFrame->data[0], av_samples_get_buffer_size(NULL, mp2DecoderProperties->channels,
- decodedFrame->nb_samples, mp2DecoderProperties->sample_fmt, 1));
- }
- currentProcessingAudioPacket.size -= bytesProcessed;
- currentProcessingAudioPacket.data += bytesProcessed;
- currentProcessingAudioPacket.dts =
- currentProcessingAudioPacket.pts = AV_NOPTS_VALUE;
- std::cout << " on loop " << loop++ << '\n';
- }
- decodedAudioFile.close();
- avcodec_close(mp2DecoderProperties);
- av_free(mp2DecoderProperties);
- avcodec_free_frame(&decodedFrame);
- return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement