Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern "C"
- {
- #include <libavcodec/avcodec.h>
- }
- #include <fstream>
- #include <vector>
- #include <iostream>
- //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;
- }
- /*
- #define INBUF_SIZE 4096
- #define AUDIO_INBUF_SIZE 20480
- #define AUDIO_REFILL_THRESH 4096
- int main()
- {
- avcodec_register_all();
- char *filename = "test.mp2";
- char *outfilename = "old.wav";
- AVCodec *codec;
- AVCodecContext *c= NULL;
- int len;
- FILE *f, *outfile;
- uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
- AVPacket avpkt;
- AVFrame *decoded_frame = NULL;
- av_init_packet(&avpkt);
- printf("Decode audio file %s to %s\n", filename, outfilename);
- codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
- if (!codec) {
- fprintf(stderr, "Codec not found\n");
- exit(1);
- }
- c = avcodec_alloc_context3(codec);
- if (!c) {
- fprintf(stderr, "Could not allocate audio codec context\n");
- exit(1);
- }
- if (avcodec_open2(c, codec, NULL) < 0) {
- fprintf(stderr, "Could not open codec\n");
- exit(1);
- }
- 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);
- }
- avpkt.data = inbuf;
- avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
- while (avpkt.size > 0) {
- int got_frame = 0;
- if (!decoded_frame) {
- if (!(decoded_frame = avcodec_alloc_frame())) {
- fprintf(stderr, "Could not allocate audio frame\n");
- exit(1);
- }
- } else
- avcodec_get_frame_defaults(decoded_frame);
- len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
- if (len < 0) {
- fprintf(stderr, "Error while decoding\n");
- exit(1);
- }
- if (got_frame) {
- int data_size = av_samples_get_buffer_size(NULL, c->channels,
- decoded_frame->nb_samples,
- c->sample_fmt, 1);
- fwrite(decoded_frame->data[0], 1, data_size, outfile);
- }
- avpkt.size -= len;
- avpkt.data += len;
- avpkt.dts =
- avpkt.pts = AV_NOPTS_VALUE;
- if (avpkt.size < AUDIO_REFILL_THRESH) {
- memmove(inbuf, avpkt.data, avpkt.size);
- avpkt.data = inbuf;
- len = fread(avpkt.data + avpkt.size, 1,
- AUDIO_INBUF_SIZE - avpkt.size, f);
- if (len > 0)
- avpkt.size += len;
- }
- }
- fclose(outfile);
- fclose(f);
- avcodec_close(c);
- av_free(c);
- avcodec_free_frame(&decoded_frame);
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement