ffmpeg.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. #include <fstream>
  5. extern "C" {
  6. #include <libavutil/opt.h>
  7. #include <libavcodec/avcodec.h>
  8. #include <libavformat/avformat.h>
  9. #include <libavutil/channel_layout.h>
  10. #include <libavutil/samplefmt.h>
  11. #include <libswresample/swresample.h>
  12. }
  13. int main(int argc, char* argv[]) {
  14. // from buff
  15. FILE* fp;
  16. fp = fopen(argv[1], "rb");
  17. if (fp == nullptr)
  18. {
  19. return -1;
  20. }
  21. fseek(fp, 0, SEEK_END);
  22. uint32_t n_file_len = ftell(fp);
  23. fseek(fp, 0, SEEK_SET);
  24. char* buf = (char *)malloc(n_file_len);
  25. memset(buf, 0, n_file_len);
  26. fread(buf, 1, n_file_len, fp);
  27. fclose(fp);
  28. AVIOContext* avio_ctx = avio_alloc_context(
  29. (unsigned char*)buf, // buffer
  30. n_file_len, // buffer size
  31. 0, // write flag (0 for read-only)
  32. nullptr, // opaque pointer (not used here)
  33. nullptr, // read callback (not used here)
  34. nullptr, // write callback (not used here)
  35. nullptr // seek callback (not used here)
  36. );
  37. AVFormatContext* formatContext = avformat_alloc_context();
  38. formatContext->pb = avio_ctx;
  39. if (avformat_open_input(&formatContext, "", NULL, NULL) != 0) {
  40. printf("Error: Could not open input file.");
  41. avformat_close_input(&formatContext);
  42. avformat_free_context(formatContext);
  43. return -1;
  44. }
  45. // from file
  46. // AVFormatContext* formatContext = avformat_alloc_context();
  47. // if (avformat_open_input(&formatContext, argv[1], NULL, NULL) != 0) {
  48. // printf("Error: Could not open input file.");
  49. // avformat_close_input(&formatContext);
  50. // avformat_free_context(formatContext);
  51. // return -1;
  52. // }
  53. if (avformat_find_stream_info(formatContext, NULL) < 0) {
  54. printf("Error: Could not find stream information.");
  55. avformat_close_input(&formatContext);
  56. avformat_free_context(formatContext);
  57. return -1;
  58. }
  59. const AVCodec* codec = NULL;
  60. AVCodecParameters* codecParameters = NULL;
  61. int audioStreamIndex = av_find_best_stream(formatContext, AVMEDIA_TYPE_AUDIO, -1, -1, &codec, 0);
  62. if (audioStreamIndex >= 0) {
  63. codecParameters = formatContext->streams[audioStreamIndex]->codecpar;
  64. }
  65. AVCodecContext* codecContext = avcodec_alloc_context3(codec);
  66. if (!codecContext) {
  67. fprintf(stderr, "Failed to allocate codec context\n");
  68. avformat_close_input(&formatContext);
  69. return -1;
  70. }
  71. if (avcodec_parameters_to_context(codecContext, codecParameters) != 0) {
  72. printf("Error: Could not copy codec parameters to codec context.");
  73. avcodec_free_context(&codecContext);
  74. avformat_close_input(&formatContext);
  75. avformat_free_context(formatContext);
  76. return -1;
  77. }
  78. if (avcodec_open2(codecContext, codec, NULL) < 0) {
  79. printf("Error: Could not open audio decoder.");
  80. avcodec_free_context(&codecContext);
  81. avformat_close_input(&formatContext);
  82. avformat_free_context(formatContext);
  83. return -1;
  84. }
  85. SwrContext *swr_ctx = swr_alloc_set_opts(
  86. nullptr, // allocate a new context
  87. AV_CH_LAYOUT_MONO, // output channel layout (stereo)
  88. AV_SAMPLE_FMT_S16, // output sample format (signed 16-bit)
  89. 16000, // output sample rate (same as input)
  90. av_get_default_channel_layout(codecContext->channels), // input channel layout
  91. codecContext->sample_fmt, // input sample format
  92. codecContext->sample_rate, // input sample rate
  93. 0, // logging level
  94. nullptr // parent context
  95. );
  96. if (swr_ctx == nullptr) {
  97. std::cerr << "Could not initialize resampler" << std::endl;
  98. avcodec_free_context(&codecContext);
  99. avformat_close_input(&formatContext);
  100. avformat_free_context(formatContext);
  101. return -1;
  102. }
  103. if (swr_init(swr_ctx) != 0) {
  104. std::cerr << "Could not initialize resampler" << std::endl;
  105. swr_free(&swr_ctx);
  106. avcodec_free_context(&codecContext);
  107. avformat_close_input(&formatContext);
  108. avformat_free_context(formatContext);
  109. return -1;
  110. }
  111. // to pcm
  112. FILE *out_file = fopen("output.pcm", "wb");
  113. AVPacket* packet = av_packet_alloc();
  114. AVFrame* frame = av_frame_alloc();
  115. std::vector<uint8_t> resampled_buffer;
  116. while (av_read_frame(formatContext, packet) >= 0) {
  117. if (packet->stream_index == audioStreamIndex) {
  118. if (avcodec_send_packet(codecContext, packet) >= 0) {
  119. while (avcodec_receive_frame(codecContext, frame) >= 0) {
  120. // Resample audio if necessary
  121. int in_samples = frame->nb_samples;
  122. uint8_t **in_data = frame->extended_data;
  123. int out_samples = av_rescale_rnd(in_samples,
  124. 16000,
  125. codecContext->sample_rate,
  126. AV_ROUND_DOWN);
  127. int resampled_size = out_samples * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16);
  128. if (resampled_buffer.size() < resampled_size) {
  129. resampled_buffer.resize(resampled_size);
  130. }
  131. uint8_t *resampled_data = resampled_buffer.data();
  132. int ret = swr_convert(
  133. swr_ctx,
  134. &resampled_data, // output buffer
  135. resampled_size, // output buffer size
  136. (const uint8_t **)(frame->data), //(const uint8_t **)(frame->extended_data)
  137. in_samples // input buffer size
  138. );
  139. if (ret < 0) {
  140. std::cerr << "Error resampling audio" << std::endl;
  141. break;
  142. }
  143. fwrite(resampled_buffer.data(), sizeof(int8_t), resampled_size, out_file);
  144. }
  145. }
  146. }
  147. av_packet_unref(packet);
  148. }
  149. fclose(out_file);
  150. avio_context_free(&avio_ctx);
  151. avformat_close_input(&formatContext);
  152. avformat_free_context(formatContext);
  153. avcodec_free_context(&codecContext);
  154. swr_free(&swr_ctx);
  155. av_packet_free(&packet);
  156. av_frame_free(&frame);
  157. }