不明原因的问题(用的新api,和老的代码不太一样
来源:8-3 实战音频重采样2

慕妹4365742
2024-06-27
#include <libavcodec/avcodec.h>
#include <libswresample/swresample.h>
#include <libavutil/channel_layout.h>
#include <string.h>
}
#include <Windows.h>
//#pragma execution_character_set(“utf-8”)
void show_dshow_device() {
AVFormatContext* pFormatCtx = avformat_alloc_context();
AVDictionary* options = NULL;
av_dict_set(&options, “list_devices”, “true”, 0); //0表示不区分大小写
const AVInputFormat* iformat = av_find_input_format(“dshow”);
printf(“Device Info=====\n”);
avformat_open_input(&pFormatCtx, “video=dummy”, iformat, &options);
printf("================================\n");
avformat_free_context(pFormatCtx);
}
int main() {
AVFormatContext* fmt_ctx = nullptr;
const char* devicename = “audio=@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{6118AFF5-408C-4835-B01F-0BB761EA0EDE}”;
//const wchar_t* devicename = “audio=麦克风 (Realtek® Audio)”;
avdevice_register_all();
const AVInputFormat* iformat = av_find_input_format(“dshow”);
show_dshow_device();
AVDictionary* options = nullptr;
av_dict_set(&options, "rtbufsize", "64M", 0); // 设置实时缓冲区大小为64MB
int ret = avformat_open_input(&fmt_ctx, devicename, iformat, &options);
for (int i = 0; i < fmt_ctx->nb_streams; ++i) {
AVStream* stream = fmt_ctx->streams[i];
if (stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
int sample_fmt = stream->codecpar->format;
std::cout << sample_fmt << std::endl;
// 现在 sample_fmt 可以是 AV_SAMPLE_FMT_S16 或 AV_SAMPLE_FMT_FLT
// 处理你的逻辑...
}
}
char errors[2024];
if (ret < 0) {
av_strerror(ret, errors, 1024);
std::cout << errors;
}
FILE *outfile = fopen("D:\\videoStudy\\tryFile\\try.pcm", "wb+");
AVPacket* pkt = av_packet_alloc();
av_init_packet(pkt);
int count = 0;
SwrContext* swr_ctx = swr_alloc(); // 分配 SwrContext
AVChannelLayout layout1 = AV_CHANNEL_LAYOUT_STEREO;
AVChannelLayout layout2 = AV_CHANNEL_LAYOUT_STEREO;
int rett = swr_alloc_set_opts2(&swr_ctx, // we're allocating a new context
&layout1, // out_ch_layout
AV_SAMPLE_FMT_S16, // out_sample_fmt
44100, // out_sample_rate
&layout2, // in_ch_layout
AV_SAMPLE_FMT_FLTP, // in_sample_fmt
48000, // in_sample_rate
0, // log_offset
NULL); // log_ctx
if (!swr_ctx) {
std::cout << "swr_ctx is null" << std::endl;
}
if (swr_init(swr_ctx) < 0) {
std::cout << "swr_ctx init failed" << std::endl;
}
uint8_t** src_data = NULL;
int src_linesize = 0;
av_samples_alloc_array_and_samples(&src_data, &src_linesize, 2, 22050, AV_SAMPLE_FMT_S16, 0);
uint8_t** dst_data = NULL;
int dst_linesize = 0;
av_samples_alloc_array_and_samples(&dst_data, &dst_linesize, 2, 24000, AV_SAMPLE_FMT_FLTP, 0);
//std::ofstream outputFile("D:\\videoStudy\\tryFile\\try.pcm", std::ios::out | std::ios::binary);
while ((ret = av_read_frame(fmt_ctx, pkt)) == 0 && count ++ < 24) {
ret = av_read_frame(fmt_ctx, pkt);
// device not ready, sleep 1s
if (ret == -35) {
av_log(NULL, AV_LOG_WARNING, "not ready\n");
Sleep(1);
continue;
}
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "read data error\n");
break;
}
av_log(NULL, AV_LOG_INFO,
"pkt size is %d(%p),count=%d \n",
pkt->size, pkt->data, count);
// write file
//std::cout << pkt->data << std::endl;
memcpy((void*)src_data[0], (const void*)pkt->data, pkt->size);
//std::cout << (*(src_data[0])).size << std::endl;
swr_convert(swr_ctx, dst_data, dst_linesize, (const uint8_t**)src_data, src_linesize);
//outputFile << pkt->data;
//fwrite()
fwrite((const void*)dst_data[0], 1, dst_linesize, outfile);
fflush(outfile);
av_packet_unref(pkt);
}
fclose(outfile);
//outputFile.close();
av_packet_free(&pkt);
avformat_close_input(&fmt_ctx);
auto re = avutil_version();
std::cout << re;
av_log_set_level(AV_LOG_DEBUG);
av_log(NULL, AV_LOG_DEBUG, "oxxk");
return 0;
}
每次会报错提示访问冲突,但是没有找到越界,不知道原因
1回答
-
李超
2024-06-27
通过调试器看一下dst_linesize 的大小是多少,是不是越界了?
052024-06-28
相似问题