Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ffm_console.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- extern "C" {
- #include "ffmpeg/inttypes.h"
- #include "ffmpeg/include/libavutil/error.h"
- #include "ffmpeg/include/libavutil/samplefmt.h"
- #include "ffmpeg/include/libavcodec/avcodec.h"
- #include "ffmpeg/include/libavformat/avformat.h"
- #include "ffmpeg/include/libavutil/file.h"
- #include "ffmpeg/include/libavutil/frame.h"
- #include "ffmpeg/include/libswresample/swresample.h"
- #include "ffmpeg/include/libavutil/channel_layout.h"
- }
- AVFormatContext *pFormatCtx;
- AVOutputFormat *pOutputFormatCtx;
- AVCodecContext *c = NULL;
- AVCodec *aCodec;
- AVPacket avpkt;
- FILE * f;
- static SwrContext * swrctx = NULL;
- AVFrame *decoded_frame = NULL;
- #define AUDIO_INBUF_SIZE 20480
- #define AUDIO_REFILL_THRESH 4096
- uint8_t buffer[4096 *2];
- uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
- int _tmain(int argc, _TCHAR* argv[])
- {
- int errcode;
- int ret;
- avcodec_register_all();
- av_register_all();
- const char *filename = "f:\\test.at3";
- if ((errcode = avformat_open_input(&pFormatCtx, filename, NULL, NULL)) < 0)
- {
- char errDescr[4096];
- av_strerror(errcode, errDescr, 4096);
- return -1;
- }
- aCodec = avcodec_find_decoder(AV_CODEC_ID_ATRAC3P);
- if (aCodec == NULL)
- return -1;
- c = pFormatCtx->streams[0]->codec;
- int res = avcodec_open2(c, aCodec, NULL);
- f = fopen("f:\\test.at3", "rb");
- if (!f)
- return -1;
- // ----------------------------------------------------------
- // decode part
- uint8_t sample_buffer[44100];
- int samples = 0;
- uint8_t *out = sample_buffer;
- FILE * outfile = fopen("f:\\output_at3.raw", "wb");
- av_init_packet(&avpkt);
- while (1)
- {
- avpkt.data = inbuf;
- avpkt.size = fread(inbuf, 1, c->block_align, f);
- memset(sample_buffer,0,44100);
- int got_frame = 0;
- if (!decoded_frame)
- {
- if (!(decoded_frame = av_frame_alloc()))
- return -1;
- }
- av_read_frame(pFormatCtx, &avpkt);
- int len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
- if (len < 0)
- return -1;
- int64_t def_chans = av_get_default_channel_layout(decoded_frame->channels);
- if (got_frame)
- {
- swrctx = swr_alloc_set_opts(swrctx, AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_S16, c->sample_rate, def_chans, c->sample_fmt, c->sample_rate, 0, NULL);
- if (!swrctx || swr_init(swrctx) < 0)
- {
- avcodec_close(c);
- return -1;
- }
- samples = swr_convert(swrctx, &out, decoded_frame->nb_samples, (const uint8_t**)decoded_frame->extended_data, decoded_frame->nb_samples);
- if (samples < 0)
- return -1;
- swr_free(&swrctx);
- fwrite(sample_buffer,1,decoded_frame->nb_samples * 4,outfile);
- }
- }
- fclose(outfile);
- fclose(f);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment