Guest User

Untitled

a guest
May 10th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. // ffm_console.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. extern "C" {
  7. #include "ffmpeg/inttypes.h"
  8. #include "ffmpeg/include/libavutil/error.h"
  9. #include "ffmpeg/include/libavutil/samplefmt.h"
  10. #include "ffmpeg/include/libavcodec/avcodec.h"
  11. #include "ffmpeg/include/libavformat/avformat.h"
  12. #include "ffmpeg/include/libavutil/file.h"
  13. #include "ffmpeg/include/libavutil/frame.h"
  14. #include "ffmpeg/include/libswresample/swresample.h"
  15. #include "ffmpeg/include/libavutil/channel_layout.h"
  16. }
  17.  
  18. AVFormatContext *pFormatCtx;
  19. AVOutputFormat *pOutputFormatCtx;
  20. AVCodecContext *c = NULL;
  21. AVCodec *aCodec;
  22. AVPacket avpkt;
  23. FILE * f;
  24. static SwrContext * swrctx = NULL;
  25.  
  26. AVFrame *decoded_frame = NULL;
  27.  
  28. #define AUDIO_INBUF_SIZE 20480
  29. #define AUDIO_REFILL_THRESH 4096
  30.  
  31. uint8_t buffer[4096 *2];
  32. uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
  33.  
  34. int _tmain(int argc, _TCHAR* argv[])
  35. {
  36. int errcode;
  37. int ret;
  38.  
  39. avcodec_register_all();
  40. av_register_all();
  41.  
  42. const char *filename = "f:\\test.at3";
  43.  
  44. if ((errcode = avformat_open_input(&pFormatCtx, filename, NULL, NULL)) < 0)
  45. {
  46. char errDescr[4096];
  47. av_strerror(errcode, errDescr, 4096);
  48. return -1;
  49. }
  50.  
  51. aCodec = avcodec_find_decoder(AV_CODEC_ID_ATRAC3P);
  52. if (aCodec == NULL)
  53. return -1;
  54.  
  55. c = pFormatCtx->streams[0]->codec;
  56.  
  57.  
  58. int res = avcodec_open2(c, aCodec, NULL);
  59.  
  60. f = fopen("f:\\test.at3", "rb");
  61. if (!f)
  62. return -1;
  63.  
  64. // ----------------------------------------------------------
  65. // decode part
  66. uint8_t sample_buffer[44100];
  67. int samples = 0;
  68. uint8_t *out = sample_buffer;
  69.  
  70. FILE * outfile = fopen("f:\\output_at3.raw", "wb");
  71. av_init_packet(&avpkt);
  72.  
  73. while (1)
  74. {
  75.  
  76. avpkt.data = inbuf;
  77. avpkt.size = fread(inbuf, 1, c->block_align, f);
  78.  
  79. memset(sample_buffer,0,44100);
  80. int got_frame = 0;
  81. if (!decoded_frame)
  82. {
  83. if (!(decoded_frame = av_frame_alloc()))
  84. return -1;
  85. }
  86.  
  87. av_read_frame(pFormatCtx, &avpkt);
  88.  
  89. int len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
  90. if (len < 0)
  91. return -1;
  92.  
  93. int64_t def_chans = av_get_default_channel_layout(decoded_frame->channels);
  94. if (got_frame)
  95. {
  96. 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);
  97. if (!swrctx || swr_init(swrctx) < 0)
  98. {
  99. avcodec_close(c);
  100. return -1;
  101. }
  102.  
  103. samples = swr_convert(swrctx, &out, decoded_frame->nb_samples, (const uint8_t**)decoded_frame->extended_data, decoded_frame->nb_samples);
  104. if (samples < 0)
  105. return -1;
  106. swr_free(&swrctx);
  107.  
  108. fwrite(sample_buffer,1,decoded_frame->nb_samples * 4,outfile);
  109. }
  110. }
  111. fclose(outfile);
  112. fclose(f);
  113.  
  114. return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment