Advertisement
Guest User

Untitled

a guest
Jul 30th, 2014
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. AVCodec *codec;
  2. AVCodecContext *avCtx;
  3. AVFrame * decoded_frame = NULL;
  4. uint8_t *outbuf = static_cast<uint8_t *>(malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE));
  5. AVPacket avPacket;
  6.  
  7. void convBuf(char * input, int numBytes, ofstream& myFile) {
  8.  
  9. //copy bytes from buffer
  10. uint8_t inputBytes[numBytes + FF_INPUT_BUFFER_PADDING_SIZE];
  11. memset(inputBytes, 0, numBytes + FF_INPUT_BUFFER_PADDING_SIZE);
  12. memcpy(inputBytes, input, numBytes);
  13. printf("AVPacket initialisedn");
  14.  
  15. avPacket.size = numBytes; //input buffer size
  16. avPacket.data = inputBytes; // the input buffer
  17. int len;
  18.  
  19. while (avPacket.size > 0) {
  20.  
  21. int got_frame = 0;
  22. if (!decoded_frame) {
  23.  
  24. if (!(decoded_frame = avcodec_alloc_frame())) {
  25. printf("out of memory");
  26. return;
  27. }
  28. } else {
  29. avcodec_get_frame_defaults(decoded_frame);
  30. }
  31.  
  32. //-------------------->> returns always -22
  33. len = avcodec_decode_audio4(avCtx, decoded_frame, &got_frame, &avPacket);
  34.  
  35. if (len < 0) {
  36. printf("Error while decoding:%d",len);
  37. return;
  38. }
  39.  
  40. if (got_frame) {
  41. //do something with the decoded frame
  42. }
  43. avPacket.size -= len;
  44. avPacket.data += len;
  45. }
  46.  
  47. return;
  48. }
  49.  
  50.  
  51.  
  52. main(){
  53. av_register_all();
  54.  
  55. codec = avcodec_find_decoder(CODEC_ID_AAC);
  56.  
  57. if (codec == NULL) {
  58. printf("Cant find AAC codecn");
  59. return 0;
  60. }
  61. printf("AAC codec foundn");
  62.  
  63. //set parameters
  64. avCtx = avcodec_alloc_context3(codec);
  65. avCtx->channels = 1;
  66. avCtx->sample_rate = 44100;
  67. avCtx->bit_rate=16;
  68.  
  69. avCtx->sample_fmt = AV_SAMPLE_FMT_S16;
  70.  
  71. if (avCtx == NULL) {
  72. printf("Could not allocate codec contextn");
  73. return 0;
  74. }
  75. printf("codec context allocatedn");
  76.  
  77. if (avcodec_open2(avCtx, codec, NULL) < 0) {
  78. printf("Could not open codecn");
  79. return 0;
  80. }
  81. printf("AAC codec opened");
  82. av_init_packet(&avPacket);
  83. ifstream file("audio_adts.m4a", ios :: binary);
  84.  
  85. //Main reader loop
  86. while(1) {
  87. memset(buf ,0 , 4096);
  88. file.read(buf,framesize);
  89. convBuf(buf,framesize,myFile);
  90. }
  91. myFile.close();
  92.  
  93. printf("Freeing memoryn");
  94. av_free_packet(&avPacket);
  95. avcodec_close(avCtx);
  96. return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement