【发布时间】:2017-05-26 04:31:11
【问题描述】:
我正在尝试使用 libavcodec 解码作品。我可以单独使用 libopus 库来做到这一点。但我正在尝试使用 libavcodec 来实现相同的目标。我试图弄清楚为什么它在我的情况下不起作用。我有一个 rtp 流并试图对其进行解码。解码包中的结果与输入相同。解码帧通常包含 pcm 值,而不是我收到实际发送的作品帧。请帮帮我。
av_register_all();
avcodec_register_all();
AVCodec *codec;
AVCodecContext *c = NULL;
AVPacket avpkt;
AVFrame *decoded_frame = NULL;
av_init_packet(&avpkt);
codec = avcodec_find_decoder(AV_CODEC_ID_OPUS);
if (!codec) {
printf("Codec not found\n");
exit(1);
}
c = avcodec_alloc_context3(codec);
if (!c) {
printf("Could not allocate audio codec context\n");
exit(1);
}
/* put sample parameters */
c->sample_rate = 48000;
c->request_sample_fmt = AV_SAMPLE_FMT_FLT;
c->channels = 2;
/* open it */
if (avcodec_open2(c, codec, NULL) < 0) {
printf("Could not open codec\n");
exit(1);
}
AVPacket avpkt;
AVFrame *decoded_frame = NULL;
av_init_packet(&avpkt);
avpkt.data = Buffer; // Buffer is packet data here
avpkt.size = len; // length of the packet
int i, ch;
if (!decoded_frame) {
if (!(decoded_frame = av_frame_alloc())) {
RELAY_SERVER_PRINT("Could not allocate audio frame\n");
exit(1);
}
}
int ret;
int got_frame = 0;
ret = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
if (ret < 0) {
fprintf(stderr, "Error decoding audio frame (%s)\n", av_err2str(ret));
return ret;
}
printf("length %i\n", decoded_frame->pkt_size);
【问题讨论】:
-
具体出了什么问题?初始化步骤是否失败?还是解码步骤?还是所有呼叫都成功但音频数据听起来不正确?你是怎么测试的?在将数据作为输入提供给解码器之前,您是否通过 RTP 有效负载解析器运行了 RTP 数据?运行代码时,您是否在 stderr 上收到任何消息?
-
我确实解析了 RTP 。在 Buffer 中,它只有 OPUS 数据包。我与 liboupus 的 opus_decode 并行工作,它工作正常。但是使用 libavcodec 不是。主要问题它不会产生任何错误。 init or nothing 失败。 avcodec_decode_audio4 向我返回一个帧,该帧具有我发送用于解码的 opus 数据包的数据包大小。我没有收到任何错误消息。这是主要问题。
-
avcodec_decode_audio4() 中的“client_sockt_num_1”是错字吗?
-
是的,我在这里粘贴时打错了,
-
got_frame 实际上是 1 吗?在某些情况下,got_frame 可以为零,这意味着它仍在摄取音频数据并且还没有任何输出......
标签: ffmpeg libavcodec opus