mirror of
https://github.com/Danile71/go-rtsp.git
synced 2025-12-24 12:58:02 +08:00
add convert AAC -> Wav Pcm
This commit is contained in:
34
decoder.go
34
decoder.go
@@ -70,17 +70,37 @@ func (decoder *decoder) Decode(packet *C.AVPacket) (pkt *Packet, err error) {
|
||||
|
||||
pkt.data = make([]byte, int(encPacket.size))
|
||||
copy(pkt.data, *(*[]byte)(unsafe.Pointer(&encPacket.data)))
|
||||
case C.AVMEDIA_TYPE_AUDIO:
|
||||
var encPacket C.AVPacket
|
||||
defer C.av_packet_unref(&encPacket)
|
||||
|
||||
if cerr = C.rtsp_avcodec_encode_wav(decoder.codecCtx, frame, &encPacket); cerr != C.int(0) {
|
||||
err = fmt.Errorf("ffmpeg: rtsp_avcodec_encode_wav failed: %d", cerr)
|
||||
case C.AVMEDIA_TYPE_AUDIO:
|
||||
switch frame.format {
|
||||
case C.AV_SAMPLE_FMT_FLTP:
|
||||
var encPacket C.AVPacket
|
||||
defer C.av_packet_unref(&encPacket)
|
||||
|
||||
if cerr = C.rtsp_avcodec_encode_resample_wav(decoder.codecCtx, frame, &encPacket); cerr < C.int(0) {
|
||||
err = fmt.Errorf("ffmpeg: rtsp_avcodec_encode_resample_wav failed: %d %d %d", cerr, C.AVERROR_INPUT_CHANGED, C.AVERROR_OUTPUT_CHANGED)
|
||||
return
|
||||
}
|
||||
pkt.data = make([]byte, int(encPacket.size))
|
||||
copy(pkt.data, *(*[]byte)(unsafe.Pointer(&encPacket.data)))
|
||||
|
||||
case C.AV_SAMPLE_FMT_S16:
|
||||
var encPacket C.AVPacket
|
||||
defer C.av_packet_unref(&encPacket)
|
||||
|
||||
if cerr = C.rtsp_avcodec_encode_wav(decoder.codecCtx, frame, &encPacket); cerr != C.int(0) {
|
||||
err = fmt.Errorf("ffmpeg: rtsp_avcodec_encode_wav failed: %d", cerr)
|
||||
return
|
||||
}
|
||||
|
||||
pkt.data = make([]byte, int(encPacket.size))
|
||||
copy(pkt.data, *(*[]byte)(unsafe.Pointer(&encPacket.data)))
|
||||
|
||||
default:
|
||||
err = fmt.Errorf("ffmpeg: audio format %d not supported: %d", frame.format)
|
||||
return
|
||||
}
|
||||
|
||||
pkt.data = make([]byte, int(encPacket.size))
|
||||
copy(pkt.data, *(*[]byte)(unsafe.Pointer(&encPacket.data)))
|
||||
default:
|
||||
}
|
||||
|
||||
|
||||
69
ffmpeg.c
69
ffmpeg.c
@@ -3,6 +3,7 @@
|
||||
#include <libavutil/avutil.h>
|
||||
#include <libavutil/imgutils.h>
|
||||
#include <libswresample/swresample.h>
|
||||
#include <libavresample/avresample.h>
|
||||
#include <libswscale/swscale.h>
|
||||
#include <libavutil/opt.h>
|
||||
#include <string.h>
|
||||
@@ -27,6 +28,10 @@ int rtsp_encode(AVCodecContext *avctx, AVPacket *pkt, int *got_packet, AVFrame *
|
||||
|
||||
*got_packet = 0;
|
||||
|
||||
if (!frame) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = avcodec_send_frame(avctx, frame);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
@@ -41,7 +46,7 @@ int rtsp_encode(AVCodecContext *avctx, AVPacket *pkt, int *got_packet, AVFrame *
|
||||
}
|
||||
|
||||
int rtsp_avcodec_encode_wav(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket *packet) {
|
||||
AVCodec *wavCodec = avcodec_find_encoder(AV_CODEC_ID_PCM_ALAW);
|
||||
AVCodec *wavCodec = avcodec_find_encoder(AV_CODEC_ID_PCM_S16LE );
|
||||
int ret = -1;
|
||||
|
||||
if (!wavCodec) {
|
||||
@@ -54,17 +59,17 @@ int rtsp_avcodec_encode_wav(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket
|
||||
return ret;
|
||||
}
|
||||
|
||||
wavContext->time_base= (AVRational){1,25};
|
||||
wavContext->bit_rate = 64000;
|
||||
wavContext->bit_rate = pCodecCtx->bit_rate;
|
||||
wavContext->sample_fmt = AV_SAMPLE_FMT_S16;
|
||||
wavContext->sample_rate = 44100;
|
||||
wavContext->channels = 1;
|
||||
|
||||
wavContext->sample_rate = pFrame->sample_rate;
|
||||
wavContext->channel_layout = pFrame->channel_layout;
|
||||
wavContext->channels = pFrame->channels;
|
||||
|
||||
ret = avcodec_open2(wavContext, wavCodec, NULL);
|
||||
if (ret < 0) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
|
||||
int gotFrame;
|
||||
|
||||
ret = rtsp_encode(wavContext, packet, &gotFrame, pFrame);
|
||||
@@ -144,3 +149,53 @@ int rtsp_avcodec_encode_jpeg_nv12(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVP
|
||||
return ret;
|
||||
}
|
||||
|
||||
int rtsp_avcodec_encode_resample_wav(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket *packet) {
|
||||
int ret = 0;
|
||||
SwrContext *swr_ctx;
|
||||
|
||||
AVFrame *nFrame = av_frame_alloc();
|
||||
|
||||
// Without this, there is no sound at all at the output (PTS stuff I guess)
|
||||
ret = av_frame_copy_props(nFrame, pFrame);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
nFrame->channel_layout = pFrame->channel_layout;
|
||||
nFrame->sample_rate = pFrame->sample_rate;
|
||||
nFrame->format = AV_SAMPLE_FMT_S16;
|
||||
|
||||
/* set options */
|
||||
swr_ctx = swr_alloc_set_opts(NULL, // we're allocating a new context
|
||||
pFrame->channel_layout, // out_ch_layout
|
||||
AV_SAMPLE_FMT_S16, // out_sample_fmt
|
||||
pFrame->sample_rate, // out_sample_rate
|
||||
|
||||
pFrame->channel_layout, // in_ch_layout
|
||||
pCodecCtx->sample_fmt, // in_sample_fmt
|
||||
pFrame->sample_rate, // in_sample_rate
|
||||
|
||||
0, // log_offset
|
||||
NULL); // log_ctx
|
||||
|
||||
ret = swr_init(swr_ctx);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = swr_convert_frame(swr_ctx, nFrame, pFrame);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = rtsp_avcodec_encode_wav(pCodecCtx,nFrame,packet);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
swr_close(swr_ctx);
|
||||
swr_free(&swr_ctx);
|
||||
av_frame_free(&nFrame);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package rtsp
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -lavformat -lavutil -lavcodec -lswresample -lswscale
|
||||
#cgo LDFLAGS: -lavformat -lavutil -lavcodec -lswresample -lswscale -lm
|
||||
#include "ffmpeg.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
3
ffmpeg.h
3
ffmpeg.h
@@ -7,4 +7,5 @@ int rtsp_avcodec_encode_jpeg(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket
|
||||
int rtsp_avcodec_encode_jpeg_nv12(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket *packet);
|
||||
struct AVStream * stream_at(struct AVFormatContext *c, int idx);
|
||||
void ffmpeginit();
|
||||
int rtsp_avcodec_encode_wav(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket *packet);
|
||||
int rtsp_avcodec_encode_wav(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket *packet);
|
||||
int rtsp_avcodec_encode_resample_wav(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket *packet);
|
||||
Reference in New Issue
Block a user