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 91b23a8760
commit 26a9797909
1 changed files with 131 additions and 169 deletions

View File

@ -281,7 +281,7 @@ bool VideoStore::open() {
}
}
av_buffer_unref(&hw_frames_ref);
}
} // end if hwdevice_type != NONE
#endif
AVDictionary *opts = 0;
@ -289,7 +289,7 @@ bool VideoStore::open() {
Debug(2, "Options? %s", Options.c_str());
ret = av_dict_parse_string(&opts, Options.c_str(), "=", ",#\n", 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 {
AVDictionaryEntry *e = 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);
}
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);
if (hw_device_ctx) av_buffer_unref(&hw_device_ctx);
#endif
} // end foreach codec
if (!video_out_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;
} // end if can't open codec
Debug(2, "Success opening codec");
@ -350,38 +349,12 @@ bool VideoStore::open() {
#else
avcodec_copy_context(video_out_stream->codec, video_out_ctx);
#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;
if (audio_in_stream and audio_in_ctx) {
Debug(2, "Have audio_in_stream %p", audio_in_stream);
if (
#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
) {
if (CODEC(audio_in_stream)->codec_id != AV_CODEC_ID_AAC) {
audio_out_codec = avcodec_find_encoder(AV_CODEC_ID_AAC);
if (!audio_out_codec) {
Error("Could not find codec for AAC");
@ -501,7 +474,7 @@ bool VideoStore::open() {
AVDictionary *opts = nullptr;
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) {
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 while have buffered samples in the resampler
Debug(2, "av_audio_fifo_size = %d", av_audio_fifo_size(fifo));
@ -641,7 +613,7 @@ VideoStore::~VideoStore() {
flush_codecs();
// Flush Queues
Debug(1, "Flushing interleaved queues");
Debug(4, "Flushing interleaved queues");
av_interleaved_write_frame(oc, nullptr);
Debug(1, "Writing trailer");
@ -655,7 +627,7 @@ VideoStore::~VideoStore() {
// When will we not be using a file ?
if (!(out_format->flags & AVFMT_NOFILE)) {
/* Close the out file. */
Debug(2, "Closing");
Debug(4, "Closing");
if (int rc = avio_close(oc->pb)) {
Error("Error closing avio %s", av_err2str(rc));
}
@ -732,7 +704,7 @@ VideoStore::~VideoStore() {
bool VideoStore::setup_resampler() {
#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;
#else
int ret;
@ -740,17 +712,14 @@ bool VideoStore::setup_resampler() {
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
// Newer ffmpeg wants to keep everything separate... so have to lookup our own
// decoder, can't reuse the one from the camera.
audio_in_codec =
avcodec_find_decoder(audio_in_stream->codecpar->codec_id);
audio_in_codec = avcodec_find_decoder(audio_in_stream->codecpar->codec_id);
audio_in_ctx = avcodec_alloc_context3(audio_in_codec);
// Copy params from instream to ctx
ret = avcodec_parameters_to_context(
audio_in_ctx, audio_in_stream->codecpar);
ret = avcodec_parameters_to_context(audio_in_ctx, audio_in_stream->codecpar);
if (ret < 0) {
Error("Unable to copy audio params to ctx %s",
av_make_error_string(ret).c_str());
}
#else
// codec is already open in ffmpeg_camera
audio_in_ctx = audio_in_stream->codec;
@ -806,8 +775,7 @@ bool VideoStore::setup_resampler() {
if (found) {
Debug(3, "Sample rate is good %d", audio_out_ctx->sample_rate);
} else {
audio_out_ctx->sample_rate =
audio_out_codec->supported_samplerates[0];
audio_out_ctx->sample_rate = audio_out_codec->supported_samplerates[0];
Debug(1, "Sample rate is no good, setting to (%d)",
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
swscale.Convert(zm_packet->in_frame, out_frame);
} else {
Error("Have neither in_frame or image in packet %d!",
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.den);
int64_t duration = 0;
if (zm_packet->in_frame) {
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
);
} else if (video_last_pts != AV_NOPTS_VALUE) {
duration =
av_rescale_q(
duration = av_rescale_q(
zm_packet->in_frame->pts - video_last_pts,
video_in_stream->time_base,
video_out_stream->time_base);
@ -1184,7 +1149,9 @@ int VideoStore::writeVideoFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
duration
);
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
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);
} // end if in_frmae
opkt.duration = duration;
} else { // Passthrough
AVPacket *ipkt = &zm_packet->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);
ZM_DUMP_STREAM_PACKET(video_out_stream, opkt, "after pts adjustment");
} // end if codec matches
@ -1232,15 +1197,14 @@ int VideoStore::writeVideoFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
} // end int VideoStore::writeVideoFramePacket( AVPacket *ipkt )
int VideoStore::writeAudioFramePacket(const std::shared_ptr<ZMPacket> &zm_packet) {
AVPacket *ipkt = &zm_packet->packet;
int ret;
if (!audio_out_stream) {
Debug(1, "Called writeAudioFramePacket when no audio_out_stream");
return 0;
// 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");
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
input_frame = nullptr;
} // end while there is data in the resampler
} else {
av_init_packet(&opkt);
opkt.data = ipkt->data;
@ -1344,8 +1307,7 @@ int VideoStore::write_packet(AVPacket *pkt, AVStream *stream) {
int ret = av_interleaved_write_frame(oc, pkt);
if (ret != 0) {
Error("Error writing packet: %s",
av_make_error_string(ret).c_str());
Error("Error writing packet: %s", av_make_error_string(ret).c_str());
} else {
Debug(4, "Success writing packet");
}