Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- AVCodec *codec;
- AVCodecContext *avCtx;
- AVFrame * decoded_frame = NULL;
- uint8_t *outbuf = static_cast<uint8_t *>(malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE));
- AVPacket avPacket;
- void convBuf(char * input, int numBytes, ofstream& myFile) {
- //copy bytes from buffer
- uint8_t inputBytes[numBytes + FF_INPUT_BUFFER_PADDING_SIZE];
- memset(inputBytes, 0, numBytes + FF_INPUT_BUFFER_PADDING_SIZE);
- memcpy(inputBytes, input, numBytes);
- printf("AVPacket initialisedn");
- avPacket.size = numBytes; //input buffer size
- avPacket.data = inputBytes; // the input buffer
- int len;
- while (avPacket.size > 0) {
- int got_frame = 0;
- if (!decoded_frame) {
- if (!(decoded_frame = avcodec_alloc_frame())) {
- printf("out of memory");
- return;
- }
- } else {
- avcodec_get_frame_defaults(decoded_frame);
- }
- //-------------------->> returns always -22
- len = avcodec_decode_audio4(avCtx, decoded_frame, &got_frame, &avPacket);
- if (len < 0) {
- printf("Error while decoding:%d",len);
- return;
- }
- if (got_frame) {
- //do something with the decoded frame
- }
- avPacket.size -= len;
- avPacket.data += len;
- }
- return;
- }
- main(){
- av_register_all();
- codec = avcodec_find_decoder(CODEC_ID_AAC);
- if (codec == NULL) {
- printf("Cant find AAC codecn");
- return 0;
- }
- printf("AAC codec foundn");
- //set parameters
- avCtx = avcodec_alloc_context3(codec);
- avCtx->channels = 1;
- avCtx->sample_rate = 44100;
- avCtx->bit_rate=16;
- avCtx->sample_fmt = AV_SAMPLE_FMT_S16;
- if (avCtx == NULL) {
- printf("Could not allocate codec contextn");
- return 0;
- }
- printf("codec context allocatedn");
- if (avcodec_open2(avCtx, codec, NULL) < 0) {
- printf("Could not open codecn");
- return 0;
- }
- printf("AAC codec opened");
- av_init_packet(&avPacket);
- ifstream file("audio_adts.m4a", ios :: binary);
- //Main reader loop
- while(1) {
- memset(buf ,0 , 4096);
- file.read(buf,framesize);
- convBuf(buf,framesize,myFile);
- }
- myFile.close();
- printf("Freeing memoryn");
- av_free_packet(&avPacket);
- avcodec_close(avCtx);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement