Merge branch 'master' of github.com:ZoneMinder/ZoneMinder

This commit is contained in:
Isaac Connor 2019-09-13 16:06:47 -04:00
commit 1653270afd
4 changed files with 83 additions and 24 deletions

View File

@ -434,6 +434,35 @@ unsigned int zm_av_packet_ref( AVPacket *dst, AVPacket *src ) {
dst->stream_index = src->stream_index; dst->stream_index = src->stream_index;
return 0; return 0;
} }
const char *avcodec_get_name(enum AVCodecID id) {
const AVCodecDescriptor *cd;
if ( id == AV_CODEC_ID_NONE)
return "none";
cd = avcodec_descriptor_get(id);
if (cd)
return cd->name;
AVCodec *codec;
codec = avcodec_find_decoder(id);
if (codec)
return codec->name;
codec = avcodec_find_encoder(id);
if (codec)
return codec->name;
return "unknown codec";
}
void av_packet_rescale_ts(
AVPacket *pkt,
AVRational src_tb,
AVRational dst_tb
) {
if ( pkt->pts != AV_NOPTS_VALUE)
pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
if ( pkt->dts != AV_NOPTS_VALUE)
pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
if ( pkt->duration != AV_NOPTS_VALUE)
pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
}
#endif #endif
bool is_video_stream( AVStream * stream ) { bool is_video_stream( AVStream * stream ) {
@ -646,7 +675,7 @@ int zm_resample_audio(
#if defined(HAVE_LIBSWRESAMPLE) #if defined(HAVE_LIBSWRESAMPLE)
SwrContext *resample_ctx, SwrContext *resample_ctx,
#else #else
#if defined(HAVE_LIBSWRESAMPLE) #if defined(HAVE_LIBAVRESAMPLE)
AVAudioResampleContext *resample_ctx, AVAudioResampleContext *resample_ctx,
#endif #endif
#endif #endif
@ -689,9 +718,28 @@ int zm_resample_audio(
} }
#endif #endif
#endif #endif
zm_dump_frame(out_frame, "Out frame after resample delay"); zm_dump_frame(out_frame, "Out frame after resample");
return 1; return 1;
} }
int zm_resample_get_delay(
#if defined(HAVE_LIBSWRESAMPLE)
SwrContext *resample_ctx,
#else
#if defined(HAVE_LIBAVRESAMPLE)
AVAudioResampleContext *resample_ctx,
#endif
#endif
int time_base
) {
#if defined(HAVE_LIBSWRESAMPLE)
return sws_get_delay(resample_ctx, time_base);
#else
#if defined(HAVE_LIBAVRESAMPLE)
return avresample_available(resample_ctx);
#endif
#endif
}
#endif #endif
int zm_add_samples_to_fifo(AVAudioFifo *fifo, AVFrame *frame) { int zm_add_samples_to_fifo(AVAudioFifo *fifo, AVFrame *frame) {
@ -727,3 +775,4 @@ int zm_get_samples_from_fifo(AVAudioFifo *fifo, AVFrame *frame) {
zm_dump_frame(frame, "Out frame after fifo read"); zm_dump_frame(frame, "Out frame after fifo read");
return 1; return 1;
} }

View File

@ -337,6 +337,9 @@ void zm_dump_codecpar(const AVCodecParameters *par);
#else #else
unsigned int zm_av_packet_ref( AVPacket *dst, AVPacket *src ); unsigned int zm_av_packet_ref( AVPacket *dst, AVPacket *src );
#define zm_av_packet_unref( packet ) av_free_packet( packet ) #define zm_av_packet_unref( packet ) av_free_packet( packet )
const char *avcodec_get_name(AVCodecID id);
void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb);
#endif #endif
#if LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0) #if LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0)
#define zm_avcodec_decode_video( context, rawFrame, frameComplete, packet ) avcodec_decode_video2( context, rawFrame, frameComplete, packet ) #define zm_avcodec_decode_video( context, rawFrame, frameComplete, packet ) avcodec_decode_video2( context, rawFrame, frameComplete, packet )
@ -375,15 +378,28 @@ int zm_resample_audio(
#if defined(HAVE_LIBSWRESAMPLE) #if defined(HAVE_LIBSWRESAMPLE)
SwrContext *resample_ctx, SwrContext *resample_ctx,
#else #else
#if defined(HAVE_LIBSWRESAMPLE) #if defined(HAVE_LIBAVRESAMPLE)
AVAudioResampleContext *resample_ctx, AVAudioResampleContext *resample_ctx,
#endif #endif
#endif #endif
AVFrame *in_frame, AVFrame *in_frame,
AVFrame *out_frame AVFrame *out_frame
); );
int zm_resample_get_delay(
#if defined(HAVE_LIBSWRESAMPLE)
SwrContext *resample_ctx,
#else
#if defined(HAVE_LIBAVRESAMPLE)
AVAudioResampleContext *resample_ctx,
#endif #endif
#endif
int time_base
);
#endif
int zm_add_samples_to_fifo(AVAudioFifo *fifo, AVFrame *frame); int zm_add_samples_to_fifo(AVAudioFifo *fifo, AVFrame *frame);
int zm_get_samples_from_fifo(AVAudioFifo *fifo, AVFrame *frame); int zm_get_samples_from_fifo(AVAudioFifo *fifo, AVFrame *frame);
#endif // ZM_FFMPEG_H #endif // ZM_FFMPEG_H

View File

@ -273,9 +273,7 @@ VideoStore::VideoStore(
out_frame = NULL; out_frame = NULL;
#if defined(HAVE_LIBSWRESAMPLE) || defined(HAVE_LIBAVRESAMPLE) #if defined(HAVE_LIBSWRESAMPLE) || defined(HAVE_LIBAVRESAMPLE)
resample_ctx = NULL; resample_ctx = NULL;
#if defined(HAVE_LIBSWRESAMPLE)
fifo = NULL; fifo = NULL;
#endif
#endif #endif
video_first_pts = 0; video_first_pts = 0;
video_first_dts = 0; video_first_dts = 0;
@ -445,21 +443,12 @@ VideoStore::~VideoStore() {
/* /*
* At the end of the file, we pass the remaining samples to * At the end of the file, we pass the remaining samples to
* the encoder. */ * the encoder. */
while ( swr_get_delay(resample_ctx, audio_out_ctx->sample_rate) ) { while ( zm_resample_get_delay(resample_ctx, audio_out_ctx->sample_rate) ) {
swr_convert_frame(resample_ctx, out_frame, NULL); zm_resample_audio(resample_ctx, out_frame, NULL);
int ret = av_audio_fifo_realloc(fifo, av_audio_fifo_size(fifo) + out_frame->nb_samples);
if ( ret < 0 ) { if ( zm_add_samples_to_fifo(fifo, out_frame) ) {
Error("Could not reallocate FIFO to %d samples",
av_audio_fifo_size(fifo) + out_frame->nb_samples);
} else {
/** Store the new samples in the FIFO buffer. */
ret = av_audio_fifo_write(fifo, (void **)out_frame->data, out_frame->nb_samples);
if ( ret < out_frame->nb_samples ) {
Error("Could not write data to FIFO. %d written, expecting %d. Reason %s",
ret, out_frame->nb_samples, av_make_error_string(ret).c_str());
}
// Should probably set the frame size to what is reported FIXME // Should probably set the frame size to what is reported FIXME
if ( av_audio_fifo_read(fifo, (void **)out_frame->data, frame_size) ) { if ( zm_get_samples_from_fifo(fifo, out_frame) ) {
if ( zm_send_frame_receive_packet(audio_out_ctx, out_frame, pkt) ) { if ( zm_send_frame_receive_packet(audio_out_ctx, out_frame, pkt) ) {
pkt.stream_index = audio_out_stream->index; pkt.stream_index = audio_out_stream->index;
@ -586,11 +575,11 @@ VideoStore::~VideoStore() {
#if defined(HAVE_LIBAVRESAMPLE) || defined(HAVE_LIBSWRESAMPLE) #if defined(HAVE_LIBAVRESAMPLE) || defined(HAVE_LIBSWRESAMPLE)
if ( resample_ctx ) { if ( resample_ctx ) {
#if defined(HAVE_LIBSWRESAMPLE)
if ( fifo ) { if ( fifo ) {
av_audio_fifo_free(fifo); av_audio_fifo_free(fifo);
fifo = NULL; fifo = NULL;
} }
#if defined(HAVE_LIBSWRESAMPLE)
swr_free(&resample_ctx); swr_free(&resample_ctx);
#else #else
#if defined(HAVE_LIBAVRESAMPLE) #if defined(HAVE_LIBAVRESAMPLE)
@ -645,7 +634,12 @@ bool VideoStore::setup_resampler() {
// codec is already open in ffmpeg_camera // codec is already open in ffmpeg_camera
audio_in_ctx = audio_in_stream->codec; audio_in_ctx = audio_in_stream->codec;
audio_in_codec = reinterpret_cast<const AVCodec *>(audio_in_ctx->codec); audio_in_codec = reinterpret_cast<const AVCodec *>(audio_in_ctx->codec);
//audio_in_codec = avcodec_find_decoder(audio_in_stream->codec->codec_id); if ( !audio_in_codec ) {
audio_in_codec = avcodec_find_decoder(audio_in_stream->codec->codec_id);
}
if ( !audio_in_codec ) {
return false;
}
#endif #endif
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0) #if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
@ -779,13 +773,13 @@ bool VideoStore::setup_resampler() {
return false; return false;
} }
#if defined(HAVE_LIBSWRESAMPLE)
if ( !(fifo = av_audio_fifo_alloc( if ( !(fifo = av_audio_fifo_alloc(
audio_out_ctx->sample_fmt, audio_out_ctx->sample_fmt,
audio_out_ctx->channels, 1)) ) { audio_out_ctx->channels, 1)) ) {
Error("Could not allocate FIFO"); Error("Could not allocate FIFO");
return false; return false;
} }
#if defined(HAVE_LIBSWRESAMPLE)
resample_ctx = swr_alloc_set_opts(NULL, resample_ctx = swr_alloc_set_opts(NULL,
audio_out_ctx->channel_layout, audio_out_ctx->channel_layout,
audio_out_ctx->sample_fmt, audio_out_ctx->sample_fmt,
@ -980,7 +974,7 @@ int VideoStore::writeAudioFramePacket(AVPacket *ipkt) {
write_packet(&opkt, audio_out_stream); write_packet(&opkt, audio_out_stream);
zm_av_packet_unref(&opkt); zm_av_packet_unref(&opkt);
if ( swr_get_delay(resample_ctx, out_frame->sample_rate) < out_frame->nb_samples) if ( zm_resample_get_delay(resample_ctx, out_frame->sample_rate) < out_frame->nb_samples)
break; break;
// This will send a null frame, emptying out the resample buffer // This will send a null frame, emptying out the resample buffer
input_frame = NULL; input_frame = NULL;

View File

@ -47,12 +47,12 @@ private:
AVCodecContext *audio_out_ctx; AVCodecContext *audio_out_ctx;
#ifdef HAVE_LIBSWRESAMPLE #ifdef HAVE_LIBSWRESAMPLE
SwrContext *resample_ctx; SwrContext *resample_ctx;
AVAudioFifo *fifo;
#else #else
#ifdef HAVE_LIBAVRESAMPLE #ifdef HAVE_LIBAVRESAMPLE
AVAudioResampleContext* resample_ctx; AVAudioResampleContext* resample_ctx;
#endif #endif
#endif #endif
AVAudioFifo *fifo;
uint8_t *converted_in_samples; uint8_t *converted_in_samples;
const char *filename; const char *filename;