code style and remove orientation setting when encoding. frames are already rotated

This commit is contained in:
Isaac Connor 2021-05-16 17:05:40 -04:00
parent e18f9676cd
commit 2d9161551d
1 changed files with 131 additions and 169 deletions

View File

@ -281,7 +281,7 @@ bool VideoStore::open() {
} }
} }
av_buffer_unref(&hw_frames_ref); av_buffer_unref(&hw_frames_ref);
} } // end if hwdevice_type != NONE
#endif #endif
AVDictionary *opts = 0; AVDictionary *opts = 0;
@ -289,7 +289,7 @@ bool VideoStore::open() {
Debug(2, "Options? %s", Options.c_str()); Debug(2, "Options? %s", Options.c_str());
ret = av_dict_parse_string(&opts, Options.c_str(), "=", ",#\n", 0); ret = av_dict_parse_string(&opts, Options.c_str(), "=", ",#\n", 0);
if (ret < 0) { if (ret < 0) {
Warning("Could not parse ffmpeg encoder options list '%s'\n", Options.c_str()); Warning("Could not parse ffmpeg encoder options list '%s'", Options.c_str());
} else { } else {
AVDictionaryEntry *e = nullptr; AVDictionaryEntry *e = nullptr;
while ((e = av_dict_get(opts, "", e, AV_DICT_IGNORE_SUFFIX)) != nullptr) { while ((e = av_dict_get(opts, "", e, AV_DICT_IGNORE_SUFFIX)) != nullptr) {
@ -318,16 +318,15 @@ bool VideoStore::open() {
Warning("Encoder Option %s not recognized by ffmpeg codec", e->key); Warning("Encoder Option %s not recognized by ffmpeg codec", e->key);
} }
if (video_out_codec) break; if (video_out_codec) break;
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
// We allocate and copy in newer ffmpeg, so need to free it
avcodec_free_context(&video_out_ctx); avcodec_free_context(&video_out_ctx);
if (hw_device_ctx) av_buffer_unref(&hw_device_ctx); if (hw_device_ctx) av_buffer_unref(&hw_device_ctx);
#endif
} // end foreach codec } // end foreach codec
if (!video_out_codec) { if (!video_out_codec) {
Error("Can't open video codec!"); Error("Can't open video codec!");
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
// We allocate and copy in newer ffmpeg, so need to free it
avcodec_free_context(&video_out_ctx);
#endif
return false; return false;
} // end if can't open codec } // end if can't open codec
Debug(2, "Success opening codec"); Debug(2, "Success opening codec");
@ -350,38 +349,12 @@ bool VideoStore::open() {
#else #else
avcodec_copy_context(video_out_stream->codec, video_out_ctx); avcodec_copy_context(video_out_stream->codec, video_out_ctx);
#endif #endif
// Only set orientation if doing passthrough, otherwise the frame image will be rotated
Monitor::Orientation orientation = monitor->getOrientation();
if ( orientation ) {
Debug(3, "Have orientation %d", orientation);
if ( orientation == Monitor::ROTATE_0 ) {
} else if ( orientation == Monitor::ROTATE_90 ) {
ret = av_dict_set(&video_out_stream->metadata, "rotate", "90", 0);
if ( ret < 0 ) Warning("%s:%d: title set failed", __FILE__, __LINE__);
} else if ( orientation == Monitor::ROTATE_180 ) {
ret = av_dict_set(&video_out_stream->metadata, "rotate", "180", 0);
if ( ret < 0 ) Warning("%s:%d: title set failed", __FILE__, __LINE__);
} else if ( orientation == Monitor::ROTATE_270 ) {
ret = av_dict_set(&video_out_stream->metadata, "rotate", "270", 0);
if ( ret < 0 ) Warning("%s:%d: title set failed", __FILE__, __LINE__);
} else {
Warning("Unsupported Orientation(%d)", orientation);
}
} // end if orientation
video_out_stream->time_base = video_in_stream ? video_in_stream->time_base : AV_TIME_BASE_Q; video_out_stream->time_base = video_in_stream ? video_in_stream->time_base : AV_TIME_BASE_Q;
if (audio_in_stream and audio_in_ctx) { if (audio_in_stream and audio_in_ctx) {
Debug(2, "Have audio_in_stream %p", audio_in_stream); Debug(2, "Have audio_in_stream %p", audio_in_stream);
if ( if (CODEC(audio_in_stream)->codec_id != AV_CODEC_ID_AAC) {
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
audio_in_stream->codecpar->codec_id
#else
audio_in_stream->codec->codec_id
#endif
!= AV_CODEC_ID_AAC
) {
audio_out_codec = avcodec_find_encoder(AV_CODEC_ID_AAC); audio_out_codec = avcodec_find_encoder(AV_CODEC_ID_AAC);
if (!audio_out_codec) { if (!audio_out_codec) {
Error("Could not find codec for AAC"); Error("Could not find codec for AAC");
@ -501,7 +474,7 @@ bool VideoStore::open() {
AVDictionary *opts = nullptr; AVDictionary *opts = nullptr;
std::string option_string = monitor->GetEncoderOptions(); std::string option_string = monitor->GetEncoderOptions();
ret = av_dict_parse_string(&opts, option_string.c_str(), "=", ",\n", 0); ret = av_dict_parse_string(&opts, option_string.c_str(), "=", "#,\n", 0);
if (ret < 0) { if (ret < 0) {
Warning("Could not parse ffmpeg output options '%s'", option_string.c_str()); Warning("Could not parse ffmpeg output options '%s'", option_string.c_str());
} }
@ -591,7 +564,6 @@ void VideoStore::flush_codecs() {
} }
} // end if data returned from fifo } // end if data returned from fifo
} }
} // end while have buffered samples in the resampler } // end while have buffered samples in the resampler
Debug(2, "av_audio_fifo_size = %d", av_audio_fifo_size(fifo)); Debug(2, "av_audio_fifo_size = %d", av_audio_fifo_size(fifo));
@ -641,7 +613,7 @@ VideoStore::~VideoStore() {
flush_codecs(); flush_codecs();
// Flush Queues // Flush Queues
Debug(1, "Flushing interleaved queues"); Debug(4, "Flushing interleaved queues");
av_interleaved_write_frame(oc, nullptr); av_interleaved_write_frame(oc, nullptr);
Debug(1, "Writing trailer"); Debug(1, "Writing trailer");
@ -655,7 +627,7 @@ VideoStore::~VideoStore() {
// When will we not be using a file ? // When will we not be using a file ?
if (!(out_format->flags & AVFMT_NOFILE)) { if (!(out_format->flags & AVFMT_NOFILE)) {
/* Close the out file. */ /* Close the out file. */
Debug(2, "Closing"); Debug(4, "Closing");
if (int rc = avio_close(oc->pb)) { if (int rc = avio_close(oc->pb)) {
Error("Error closing avio %s", av_err2str(rc)); Error("Error closing avio %s", av_err2str(rc));
} }
@ -732,7 +704,7 @@ VideoStore::~VideoStore() {
bool VideoStore::setup_resampler() { bool VideoStore::setup_resampler() {
#if !defined(HAVE_LIBSWRESAMPLE) && !defined(HAVE_LIBAVRESAMPLE) #if !defined(HAVE_LIBSWRESAMPLE) && !defined(HAVE_LIBAVRESAMPLE)
Error("%s", "Not built with resample library. Cannot do audio conversion to AAC"); Error("Not built with resample library. Cannot do audio conversion to AAC");
return false; return false;
#else #else
int ret; int ret;
@ -740,17 +712,14 @@ bool VideoStore::setup_resampler() {
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0) #if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
// Newer ffmpeg wants to keep everything separate... so have to lookup our own // Newer ffmpeg wants to keep everything separate... so have to lookup our own
// decoder, can't reuse the one from the camera. // decoder, can't reuse the one from the camera.
audio_in_codec = audio_in_codec = avcodec_find_decoder(audio_in_stream->codecpar->codec_id);
avcodec_find_decoder(audio_in_stream->codecpar->codec_id);
audio_in_ctx = avcodec_alloc_context3(audio_in_codec); audio_in_ctx = avcodec_alloc_context3(audio_in_codec);
// Copy params from instream to ctx // Copy params from instream to ctx
ret = avcodec_parameters_to_context( ret = avcodec_parameters_to_context(audio_in_ctx, audio_in_stream->codecpar);
audio_in_ctx, audio_in_stream->codecpar);
if (ret < 0) { if (ret < 0) {
Error("Unable to copy audio params to ctx %s", Error("Unable to copy audio params to ctx %s",
av_make_error_string(ret).c_str()); av_make_error_string(ret).c_str());
} }
#else #else
// 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;
@ -806,8 +775,7 @@ bool VideoStore::setup_resampler() {
if (found) { if (found) {
Debug(3, "Sample rate is good %d", audio_out_ctx->sample_rate); Debug(3, "Sample rate is good %d", audio_out_ctx->sample_rate);
} else { } else {
audio_out_ctx->sample_rate = audio_out_ctx->sample_rate = audio_out_codec->supported_samplerates[0];
audio_out_codec->supported_samplerates[0];
Debug(1, "Sample rate is no good, setting to (%d)", Debug(1, "Sample rate is no good, setting to (%d)",
audio_out_codec->supported_samplerates[0]); audio_out_codec->supported_samplerates[0]);
} }
@ -1056,7 +1024,6 @@ int VideoStore::writeVideoFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
} }
// Go straight to out frame // Go straight to out frame
swscale.Convert(zm_packet->in_frame, out_frame); swscale.Convert(zm_packet->in_frame, out_frame);
} else { } else {
Error("Have neither in_frame or image in packet %d!", Error("Have neither in_frame or image in packet %d!",
zm_packet->image_index); zm_packet->image_index);
@ -1154,7 +1121,6 @@ int VideoStore::writeVideoFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
video_out_stream->time_base.num, video_out_stream->time_base.num,
video_out_stream->time_base.den); video_out_stream->time_base.den);
int64_t duration = 0; int64_t duration = 0;
if (zm_packet->in_frame) { if (zm_packet->in_frame) {
if (zm_packet->in_frame->pkt_duration) { if (zm_packet->in_frame->pkt_duration) {
@ -1172,8 +1138,7 @@ int VideoStore::writeVideoFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
video_out_stream->time_base.den video_out_stream->time_base.den
); );
} else if (video_last_pts != AV_NOPTS_VALUE) { } else if (video_last_pts != AV_NOPTS_VALUE) {
duration = duration = av_rescale_q(
av_rescale_q(
zm_packet->in_frame->pts - video_last_pts, zm_packet->in_frame->pts - video_last_pts,
video_in_stream->time_base, video_in_stream->time_base,
video_out_stream->time_base); video_out_stream->time_base);
@ -1184,7 +1149,9 @@ int VideoStore::writeVideoFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
duration duration
); );
if (duration <= 0) { if (duration <= 0) {
duration = zm_packet->in_frame->pkt_duration ? zm_packet->in_frame->pkt_duration : av_rescale_q(1, video_in_stream->time_base, video_out_stream->time_base); duration = zm_packet->in_frame->pkt_duration ?
zm_packet->in_frame->pkt_duration :
av_rescale_q(1, video_in_stream->time_base, video_out_stream->time_base);
} }
} // end if in_frmae->pkt_duration } // end if in_frmae->pkt_duration
video_last_pts = zm_packet->in_frame->pts; video_last_pts = zm_packet->in_frame->pts;
@ -1192,7 +1159,6 @@ int VideoStore::writeVideoFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
//duration = av_rescale_q(zm_packet->out_frame->pts - video_last_pts, video_in_stream->time_base, video_out_stream->time_base); //duration = av_rescale_q(zm_packet->out_frame->pts - video_last_pts, video_in_stream->time_base, video_out_stream->time_base);
} // end if in_frmae } // end if in_frmae
opkt.duration = duration; opkt.duration = duration;
} else { // Passthrough } else { // Passthrough
AVPacket *ipkt = &zm_packet->packet; AVPacket *ipkt = &zm_packet->packet;
ZM_DUMP_STREAM_PACKET(video_in_stream, (*ipkt), "Doing passthrough, just copy packet"); ZM_DUMP_STREAM_PACKET(video_in_stream, (*ipkt), "Doing passthrough, just copy packet");
@ -1220,7 +1186,6 @@ int VideoStore::writeVideoFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
} }
av_packet_rescale_ts(&opkt, video_in_stream->time_base, video_out_stream->time_base); av_packet_rescale_ts(&opkt, video_in_stream->time_base, video_out_stream->time_base);
ZM_DUMP_STREAM_PACKET(video_out_stream, opkt, "after pts adjustment"); ZM_DUMP_STREAM_PACKET(video_out_stream, opkt, "after pts adjustment");
} // end if codec matches } // end if codec matches
@ -1232,15 +1197,14 @@ int VideoStore::writeVideoFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
} // end int VideoStore::writeVideoFramePacket( AVPacket *ipkt ) } // end int VideoStore::writeVideoFramePacket( AVPacket *ipkt )
int VideoStore::writeAudioFramePacket(const std::shared_ptr<ZMPacket> &zm_packet) { int VideoStore::writeAudioFramePacket(const std::shared_ptr<ZMPacket> &zm_packet) {
AVPacket *ipkt = &zm_packet->packet;
int ret;
if (!audio_out_stream) { if (!audio_out_stream) {
Debug(1, "Called writeAudioFramePacket when no audio_out_stream"); Debug(1, "Called writeAudioFramePacket when no audio_out_stream");
return 0; return 0;
// FIXME -ve return codes do not free packet in ffmpeg_camera at the moment // FIXME -ve return codes do not free packet in ffmpeg_camera at the moment
} }
AVPacket *ipkt = &zm_packet->packet;
int ret;
ZM_DUMP_STREAM_PACKET(audio_in_stream, (*ipkt), "input packet"); ZM_DUMP_STREAM_PACKET(audio_in_stream, (*ipkt), "input packet");
if (!audio_first_dts) { if (!audio_first_dts) {
@ -1295,7 +1259,6 @@ int VideoStore::writeAudioFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
// This will send a null frame, emptying out the resample buffer // This will send a null frame, emptying out the resample buffer
input_frame = nullptr; input_frame = nullptr;
} // end while there is data in the resampler } // end while there is data in the resampler
} else { } else {
av_init_packet(&opkt); av_init_packet(&opkt);
opkt.data = ipkt->data; opkt.data = ipkt->data;
@ -1344,8 +1307,7 @@ int VideoStore::write_packet(AVPacket *pkt, AVStream *stream) {
int ret = av_interleaved_write_frame(oc, pkt); int ret = av_interleaved_write_frame(oc, pkt);
if (ret != 0) { if (ret != 0) {
Error("Error writing packet: %s", Error("Error writing packet: %s", av_make_error_string(ret).c_str());
av_make_error_string(ret).c_str());
} else { } else {
Debug(4, "Success writing packet"); Debug(4, "Success writing packet");
} }