2013-09-12 17:52:10 -05:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @brief Source file for FFmpegWriter class
|
|
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org>, Fabrice Bellard
|
|
|
|
|
*
|
2019-06-09 08:31:04 -04:00
|
|
|
* @ref License
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* LICENSE
|
2013-09-12 17:52:10 -05:00
|
|
|
*
|
2019-06-11 06:51:37 -04:00
|
|
|
* Copyright (c) 2008-2019 OpenShot Studios, LLC, Fabrice Bellard
|
2013-09-12 17:52:10 -05:00
|
|
|
* (http://www.openshotstudios.com). This file is part of
|
|
|
|
|
* OpenShot Library (http://www.openshot.org), an open-source project
|
|
|
|
|
* dedicated to delivering high quality video editing and animation solutions
|
|
|
|
|
* to the world.
|
|
|
|
|
*
|
2012-07-19 15:03:55 -05:00
|
|
|
* This file is originally based on the Libavformat API example, and then modified
|
|
|
|
|
* by the libopenshot project.
|
|
|
|
|
*
|
2014-03-29 18:49:22 -05:00
|
|
|
* OpenShot Library (libopenshot) is free software: you can redistribute it
|
2014-07-11 16:52:14 -05:00
|
|
|
* and/or modify it under the terms of the GNU Lesser General Public License
|
2014-03-29 18:49:22 -05:00
|
|
|
* as published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
* License, or (at your option) any later version.
|
2012-07-19 15:03:55 -05:00
|
|
|
*
|
2014-03-29 18:49:22 -05:00
|
|
|
* OpenShot Library (libopenshot) is distributed in the hope that it will be
|
|
|
|
|
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2014-07-11 16:52:14 -05:00
|
|
|
* GNU Lesser General Public License for more details.
|
2012-07-19 15:03:55 -05:00
|
|
|
*
|
2014-07-11 16:52:14 -05:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2014-03-29 18:49:22 -05:00
|
|
|
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
|
2012-07-19 15:03:55 -05:00
|
|
|
*/
|
|
|
|
|
|
2012-07-12 15:55:41 -05:00
|
|
|
#include "../include/FFmpegWriter.h"
|
|
|
|
|
|
|
|
|
|
using namespace openshot;
|
|
|
|
|
|
2020-02-10 01:50:31 -05:00
|
|
|
#if HAVE_HW_ACCEL
|
2018-09-18 12:38:53 -07:00
|
|
|
#pragma message "You are compiling with experimental hardware encode"
|
|
|
|
|
#else
|
|
|
|
|
#pragma message "You are compiling only with software encode"
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-06-11 10:49:45 +03:00
|
|
|
// Multiplexer parameters temporary storage
|
|
|
|
|
AVDictionary *mux_dict = NULL;
|
|
|
|
|
|
2020-02-10 01:50:31 -05:00
|
|
|
#if HAVE_HW_ACCEL
|
2018-08-31 21:36:23 -07:00
|
|
|
int hw_en_on = 1; // Is set in UI
|
|
|
|
|
int hw_en_supported = 0; // Is set by FFmpegWriter
|
2018-09-08 16:31:03 -07:00
|
|
|
AVPixelFormat hw_en_av_pix_fmt = AV_PIX_FMT_NONE;
|
|
|
|
|
AVHWDeviceType hw_en_av_device_type = AV_HWDEVICE_TYPE_VAAPI;
|
2018-08-31 21:36:23 -07:00
|
|
|
static AVBufferRef *hw_device_ctx = NULL;
|
|
|
|
|
AVFrame *hw_frame = NULL;
|
|
|
|
|
|
|
|
|
|
static int set_hwframe_ctx(AVCodecContext *ctx, AVBufferRef *hw_device_ctx, int64_t width, int64_t height)
|
|
|
|
|
{
|
2019-01-29 12:38:52 -08:00
|
|
|
AVBufferRef *hw_frames_ref;
|
|
|
|
|
AVHWFramesContext *frames_ctx = NULL;
|
|
|
|
|
int err = 0;
|
2018-08-31 21:36:23 -07:00
|
|
|
|
2019-01-29 12:38:52 -08:00
|
|
|
if (!(hw_frames_ref = av_hwframe_ctx_alloc(hw_device_ctx))) {
|
|
|
|
|
fprintf(stderr, "Failed to create HW frame context.\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
frames_ctx = (AVHWFramesContext *)(hw_frames_ref->data);
|
|
|
|
|
frames_ctx->format = hw_en_av_pix_fmt;
|
|
|
|
|
frames_ctx->sw_format = AV_PIX_FMT_NV12;
|
|
|
|
|
frames_ctx->width = width;
|
|
|
|
|
frames_ctx->height = height;
|
|
|
|
|
frames_ctx->initial_pool_size = 20;
|
|
|
|
|
if ((err = av_hwframe_ctx_init(hw_frames_ref)) < 0) {
|
|
|
|
|
fprintf(stderr, "Failed to initialize HW frame context."
|
|
|
|
|
"Error code: %s\n",av_err2str(err));
|
|
|
|
|
av_buffer_unref(&hw_frames_ref);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
ctx->hw_frames_ctx = av_buffer_ref(hw_frames_ref);
|
|
|
|
|
if (!ctx->hw_frames_ctx)
|
|
|
|
|
err = AVERROR(ENOMEM);
|
2018-08-31 21:36:23 -07:00
|
|
|
|
2019-01-29 12:38:52 -08:00
|
|
|
av_buffer_unref(&hw_frames_ref);
|
|
|
|
|
return err;
|
2018-08-31 21:36:23 -07:00
|
|
|
}
|
2020-02-10 01:50:31 -05:00
|
|
|
#endif // HAVE_HW_ACCEL
|
2018-08-31 21:36:23 -07:00
|
|
|
|
2019-08-04 23:10:59 -04:00
|
|
|
FFmpegWriter::FFmpegWriter(std::string path) :
|
2020-04-22 02:02:55 -04:00
|
|
|
path(path), fmt(NULL), oc(NULL), audio_st(NULL), video_st(NULL), samples(NULL),
|
2012-08-04 01:11:12 -05:00
|
|
|
audio_outbuf(NULL), audio_outbuf_size(0), audio_input_frame_size(0), audio_input_position(0),
|
2015-02-26 17:33:09 -06:00
|
|
|
initial_audio_input_frame_size(0), img_convert_ctx(NULL), cache_size(8), num_of_rescalers(32),
|
2014-01-28 17:17:38 -06:00
|
|
|
rescaler_position(0), video_codec(NULL), audio_codec(NULL), is_writing(false), write_video_count(0), write_audio_count(0),
|
2015-02-05 00:06:07 -06:00
|
|
|
original_sample_rate(0), original_channels(0), avr(NULL), avr_planar(NULL), is_open(false), prepare_streams(false),
|
2019-04-18 14:04:37 -05:00
|
|
|
write_header(false), write_trailer(false), audio_encoder_buffer_size(0), audio_encoder_buffer(NULL) {
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2012-07-19 17:10:40 -05:00
|
|
|
// Disable audio & video (so they can be independently enabled)
|
|
|
|
|
info.has_audio = false;
|
|
|
|
|
info.has_video = false;
|
|
|
|
|
|
2012-07-12 15:55:41 -05:00
|
|
|
// Initialize FFMpeg, and register all formats and codecs
|
2018-08-11 18:22:18 -05:00
|
|
|
AV_REGISTER_ALL
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
// auto detect format
|
2012-07-19 15:03:55 -05:00
|
|
|
auto_detect_format();
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-05 00:06:07 -06:00
|
|
|
// Open the writer
|
2019-04-18 14:04:37 -05:00
|
|
|
void FFmpegWriter::Open() {
|
|
|
|
|
if (!is_open) {
|
2019-01-28 12:28:35 -06:00
|
|
|
// Open the writer
|
|
|
|
|
is_open = true;
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2019-01-28 12:28:35 -06:00
|
|
|
// Prepare streams (if needed)
|
|
|
|
|
if (!prepare_streams)
|
|
|
|
|
PrepareStreams();
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2019-01-28 12:28:35 -06:00
|
|
|
// Now that all the parameters are set, we can open the audio and video codecs and allocate the necessary encode buffers
|
|
|
|
|
if (info.has_video && video_st)
|
|
|
|
|
open_video(oc, video_st);
|
|
|
|
|
if (info.has_audio && audio_st)
|
|
|
|
|
open_audio(oc, audio_st);
|
|
|
|
|
|
|
|
|
|
// Write header (if needed)
|
|
|
|
|
if (!write_header)
|
|
|
|
|
WriteHeader();
|
|
|
|
|
}
|
2015-02-05 00:06:07 -06:00
|
|
|
}
|
|
|
|
|
|
2012-07-19 15:03:55 -05:00
|
|
|
// auto detect format (from path)
|
2019-04-18 14:04:37 -05:00
|
|
|
void FFmpegWriter::auto_detect_format() {
|
2012-07-19 15:03:55 -05:00
|
|
|
// Auto detect the output format from the name. default is mpeg.
|
|
|
|
|
fmt = av_guess_format(NULL, path.c_str(), NULL);
|
2012-07-20 00:13:16 -05:00
|
|
|
if (!fmt)
|
|
|
|
|
throw InvalidFormat("Could not deduce output format from file extension.", path);
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
// Allocate the output media context
|
2018-03-28 15:09:55 -05:00
|
|
|
AV_OUTPUT_CONTEXT(&oc, path.c_str());
|
2012-07-25 17:20:02 -05:00
|
|
|
if (!oc)
|
|
|
|
|
throw OutOfMemory("Could not allocate memory for AVFormatContext.", path);
|
2012-07-20 00:13:16 -05:00
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
// Set the AVOutputFormat for the current AVFormatContext
|
|
|
|
|
oc->oformat = fmt;
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
// Update codec names
|
2015-08-24 23:49:45 -05:00
|
|
|
if (fmt->video_codec != AV_CODEC_ID_NONE && info.has_video)
|
2012-07-25 17:20:02 -05:00
|
|
|
// Update video codec name
|
|
|
|
|
info.vcodec = avcodec_find_encoder(fmt->video_codec)->name;
|
2012-07-20 00:13:16 -05:00
|
|
|
|
2015-08-24 23:49:45 -05:00
|
|
|
if (fmt->audio_codec != AV_CODEC_ID_NONE && info.has_audio)
|
2012-07-25 17:20:02 -05:00
|
|
|
// Update audio codec name
|
|
|
|
|
info.acodec = avcodec_find_encoder(fmt->audio_codec)->name;
|
2012-07-19 15:03:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// initialize streams
|
2019-04-18 14:04:37 -05:00
|
|
|
void FFmpegWriter::initialize_streams() {
|
2019-07-03 14:14:02 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::initialize_streams", "fmt->video_codec", fmt->video_codec, "fmt->audio_codec", fmt->audio_codec, "AV_CODEC_ID_NONE", AV_CODEC_ID_NONE);
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
// Add the audio and video streams using the default format codecs and initialize the codecs
|
|
|
|
|
video_st = NULL;
|
|
|
|
|
audio_st = NULL;
|
2015-02-05 16:00:18 -06:00
|
|
|
if (fmt->video_codec != AV_CODEC_ID_NONE && info.has_video)
|
2012-07-25 17:20:02 -05:00
|
|
|
// Add video stream
|
|
|
|
|
video_st = add_video_stream();
|
2012-07-24 12:50:17 -05:00
|
|
|
|
2015-02-05 16:00:18 -06:00
|
|
|
if (fmt->audio_codec != AV_CODEC_ID_NONE && info.has_audio)
|
2012-07-25 17:20:02 -05:00
|
|
|
// Add audio stream
|
|
|
|
|
audio_st = add_audio_stream();
|
2012-07-12 15:55:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set video export options
|
2019-08-04 23:10:59 -04:00
|
|
|
void FFmpegWriter::SetVideoOptions(bool has_video, std::string codec, Fraction fps, int width, int height, Fraction pixel_ratio, bool interlaced, bool top_field_first, int bit_rate) {
|
2012-07-19 15:03:55 -05:00
|
|
|
// Set the video options
|
2019-04-18 14:04:37 -05:00
|
|
|
if (codec.length() > 0) {
|
2018-08-31 21:36:23 -07:00
|
|
|
AVCodec *new_codec;
|
|
|
|
|
// Check if the codec selected is a hardware accelerated codec
|
2020-02-10 01:50:31 -05:00
|
|
|
#if HAVE_HW_ACCEL
|
2019-08-23 19:46:05 +02:00
|
|
|
#if defined(__linux__)
|
|
|
|
|
if (strstr(codec.c_str(), "_vaapi") != NULL) {
|
2018-08-31 21:36:23 -07:00
|
|
|
new_codec = avcodec_find_encoder_by_name(codec.c_str());
|
|
|
|
|
hw_en_on = 1;
|
2019-01-29 12:38:52 -08:00
|
|
|
hw_en_supported = 1;
|
|
|
|
|
hw_en_av_pix_fmt = AV_PIX_FMT_VAAPI;
|
|
|
|
|
hw_en_av_device_type = AV_HWDEVICE_TYPE_VAAPI;
|
2019-08-23 19:46:05 +02:00
|
|
|
} else if (strstr(codec.c_str(), "_nvenc") != NULL) {
|
|
|
|
|
new_codec = avcodec_find_encoder_by_name(codec.c_str());
|
|
|
|
|
hw_en_on = 1;
|
|
|
|
|
hw_en_supported = 1;
|
|
|
|
|
hw_en_av_pix_fmt = AV_PIX_FMT_CUDA;
|
|
|
|
|
hw_en_av_device_type = AV_HWDEVICE_TYPE_CUDA;
|
|
|
|
|
} else {
|
|
|
|
|
new_codec = avcodec_find_encoder_by_name(codec.c_str());
|
|
|
|
|
hw_en_on = 0;
|
|
|
|
|
hw_en_supported = 0;
|
2019-01-29 12:38:52 -08:00
|
|
|
}
|
2019-04-18 14:04:37 -05:00
|
|
|
#elif defined(_WIN32)
|
2019-08-23 19:46:05 +02:00
|
|
|
if (strstr(codec.c_str(), "_dxva2") != NULL) {
|
2018-09-06 08:28:50 -07:00
|
|
|
new_codec = avcodec_find_encoder_by_name(codec.c_str());
|
|
|
|
|
hw_en_on = 1;
|
2019-01-29 12:38:52 -08:00
|
|
|
hw_en_supported = 1;
|
|
|
|
|
hw_en_av_pix_fmt = AV_PIX_FMT_DXVA2_VLD;
|
|
|
|
|
hw_en_av_device_type = AV_HWDEVICE_TYPE_DXVA2;
|
2019-08-23 19:46:05 +02:00
|
|
|
} else if (strstr(codec.c_str(), "_nvenc") != NULL) {
|
|
|
|
|
new_codec = avcodec_find_encoder_by_name(codec.c_str());
|
|
|
|
|
hw_en_on = 1;
|
|
|
|
|
hw_en_supported = 1;
|
|
|
|
|
hw_en_av_pix_fmt = AV_PIX_FMT_CUDA;
|
|
|
|
|
hw_en_av_device_type = AV_HWDEVICE_TYPE_CUDA;
|
|
|
|
|
} else {
|
|
|
|
|
new_codec = avcodec_find_encoder_by_name(codec.c_str());
|
|
|
|
|
hw_en_on = 0;
|
|
|
|
|
hw_en_supported = 0;
|
2018-09-06 08:28:50 -07:00
|
|
|
}
|
2019-04-18 14:04:37 -05:00
|
|
|
#elif defined(__APPLE__)
|
2019-08-23 19:46:05 +02:00
|
|
|
if (strstr(codec.c_str(), "_videotoolbox") != NULL) {
|
2018-09-06 08:28:50 -07:00
|
|
|
new_codec = avcodec_find_encoder_by_name(codec.c_str());
|
|
|
|
|
hw_en_on = 1;
|
2019-01-29 12:38:52 -08:00
|
|
|
hw_en_supported = 1;
|
2019-04-18 16:41:11 -07:00
|
|
|
hw_en_av_pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX;
|
|
|
|
|
hw_en_av_device_type = AV_HWDEVICE_TYPE_VIDEOTOOLBOX;
|
2019-08-23 19:46:05 +02:00
|
|
|
} else {
|
2018-09-06 08:28:50 -07:00
|
|
|
new_codec = avcodec_find_encoder_by_name(codec.c_str());
|
|
|
|
|
hw_en_on = 0;
|
2019-01-29 12:38:52 -08:00
|
|
|
hw_en_supported = 0;
|
2018-08-31 21:36:23 -07:00
|
|
|
}
|
2019-09-13 08:24:15 -04:00
|
|
|
#else // is FFmpeg 3 but not linux
|
2018-08-31 21:36:23 -07:00
|
|
|
new_codec = avcodec_find_encoder_by_name(codec.c_str());
|
2019-09-13 08:24:15 -04:00
|
|
|
#endif //__linux__
|
2019-04-18 14:04:37 -05:00
|
|
|
#else // not ffmpeg 3
|
2018-08-31 21:36:23 -07:00
|
|
|
new_codec = avcodec_find_encoder_by_name(codec.c_str());
|
2020-02-10 01:50:31 -05:00
|
|
|
#endif // HAVE_HW_ACCEL
|
2012-07-19 15:03:55 -05:00
|
|
|
if (new_codec == NULL)
|
2017-08-01 01:19:07 -05:00
|
|
|
throw InvalidCodec("A valid video codec could not be found for this file.", path);
|
2012-07-25 17:20:02 -05:00
|
|
|
else {
|
2012-07-19 15:03:55 -05:00
|
|
|
// Set video codec
|
|
|
|
|
info.vcodec = new_codec->name;
|
2012-07-12 15:55:41 -05:00
|
|
|
|
2012-07-19 15:03:55 -05:00
|
|
|
// Update video codec in fmt
|
|
|
|
|
fmt->video_codec = new_codec->id;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-18 14:04:37 -05:00
|
|
|
if (fps.num > 0) {
|
2012-07-19 15:03:55 -05:00
|
|
|
// Set frames per second (if provided)
|
|
|
|
|
info.fps.num = fps.num;
|
|
|
|
|
info.fps.den = fps.den;
|
|
|
|
|
|
|
|
|
|
// Set the timebase (inverse of fps)
|
|
|
|
|
info.video_timebase.num = info.fps.den;
|
|
|
|
|
info.video_timebase.den = info.fps.num;
|
|
|
|
|
}
|
|
|
|
|
if (width >= 1)
|
|
|
|
|
info.width = width;
|
|
|
|
|
if (height >= 1)
|
|
|
|
|
info.height = height;
|
2019-04-18 14:04:37 -05:00
|
|
|
if (pixel_ratio.num > 0) {
|
2012-07-19 15:03:55 -05:00
|
|
|
info.pixel_ratio.num = pixel_ratio.num;
|
|
|
|
|
info.pixel_ratio.den = pixel_ratio.den;
|
|
|
|
|
}
|
2019-04-18 14:04:37 -05:00
|
|
|
if (bit_rate >= 1000) // bit_rate is the bitrate in b/s
|
2018-09-16 18:14:31 -07:00
|
|
|
info.video_bit_rate = bit_rate;
|
2020-02-16 10:00:18 -08:00
|
|
|
if ((bit_rate >= 0) && (bit_rate < 256)) // bit_rate is the bitrate in crf
|
2019-01-28 12:28:35 -06:00
|
|
|
info.video_bit_rate = bit_rate;
|
2012-07-19 15:03:55 -05:00
|
|
|
|
|
|
|
|
info.interlaced_frame = interlaced;
|
|
|
|
|
info.top_field_first = top_field_first;
|
|
|
|
|
|
|
|
|
|
// Calculate the DAR (display aspect ratio)
|
|
|
|
|
Fraction size(info.width * info.pixel_ratio.num, info.height * info.pixel_ratio.den);
|
|
|
|
|
|
|
|
|
|
// Reduce size fraction
|
|
|
|
|
size.Reduce();
|
|
|
|
|
|
|
|
|
|
// Set the ratio based on the reduced fraction
|
|
|
|
|
info.display_ratio.num = size.num;
|
|
|
|
|
info.display_ratio.den = size.den;
|
2012-07-19 17:10:40 -05:00
|
|
|
|
2016-04-21 01:39:17 -05:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::SetVideoOptions (" + codec + ")", "width", width, "height", height, "size.num", size.num, "size.den", size.den, "fps.num", fps.num, "fps.den", fps.den);
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2012-07-20 00:13:16 -05:00
|
|
|
// Enable / Disable video
|
|
|
|
|
info.has_video = has_video;
|
2012-07-12 15:55:41 -05:00
|
|
|
}
|
|
|
|
|
|
2020-01-31 03:53:53 -05:00
|
|
|
// Set video export options (overloaded function)
|
2020-02-14 11:42:31 -05:00
|
|
|
void FFmpegWriter::SetVideoOptions(std::string codec, int width, int height, Fraction fps, int bit_rate) {
|
2020-01-31 03:53:53 -05:00
|
|
|
// Call full signature with some default parameters
|
|
|
|
|
FFmpegWriter::SetVideoOptions(true, codec, fps, width, height,
|
|
|
|
|
openshot::Fraction(1, 1), false, true, bit_rate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-07-12 15:55:41 -05:00
|
|
|
// Set audio export options
|
2019-08-04 23:10:59 -04:00
|
|
|
void FFmpegWriter::SetAudioOptions(bool has_audio, std::string codec, int sample_rate, int channels, ChannelLayout channel_layout, int bit_rate) {
|
2012-07-19 15:03:55 -05:00
|
|
|
// Set audio options
|
2019-04-18 14:04:37 -05:00
|
|
|
if (codec.length() > 0) {
|
2012-07-19 15:03:55 -05:00
|
|
|
AVCodec *new_codec = avcodec_find_encoder_by_name(codec.c_str());
|
|
|
|
|
if (new_codec == NULL)
|
|
|
|
|
throw InvalidCodec("A valid audio codec could not be found for this file.", path);
|
2019-04-18 14:04:37 -05:00
|
|
|
else {
|
2012-07-19 15:03:55 -05:00
|
|
|
// Set audio codec
|
|
|
|
|
info.acodec = new_codec->name;
|
2012-07-12 15:55:41 -05:00
|
|
|
|
2012-07-19 15:03:55 -05:00
|
|
|
// Update audio codec in fmt
|
|
|
|
|
fmt->audio_codec = new_codec->id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (sample_rate > 7999)
|
|
|
|
|
info.sample_rate = sample_rate;
|
|
|
|
|
if (channels > 0)
|
|
|
|
|
info.channels = channels;
|
|
|
|
|
if (bit_rate > 999)
|
|
|
|
|
info.audio_bit_rate = bit_rate;
|
2015-02-05 00:06:07 -06:00
|
|
|
info.channel_layout = channel_layout;
|
2012-07-19 17:10:40 -05:00
|
|
|
|
2014-01-28 17:17:38 -06:00
|
|
|
// init resample options (if zero)
|
|
|
|
|
if (original_sample_rate == 0)
|
|
|
|
|
original_sample_rate = info.sample_rate;
|
|
|
|
|
if (original_channels == 0)
|
|
|
|
|
original_channels = info.channels;
|
|
|
|
|
|
2019-07-03 14:14:02 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::SetAudioOptions (" + codec + ")", "sample_rate", sample_rate, "channels", channels, "bit_rate", bit_rate);
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2012-07-20 00:13:16 -05:00
|
|
|
// Enable / Disable audio
|
|
|
|
|
info.has_audio = has_audio;
|
2012-07-12 15:55:41 -05:00
|
|
|
}
|
|
|
|
|
|
2020-01-31 03:53:53 -05:00
|
|
|
|
|
|
|
|
// Set audio export options (overloaded function)
|
|
|
|
|
void FFmpegWriter::SetAudioOptions(std::string codec, int sample_rate, int bit_rate) {
|
|
|
|
|
// Call full signature with some default parameters
|
2020-02-14 11:42:31 -05:00
|
|
|
FFmpegWriter::SetAudioOptions(true, codec, sample_rate, 2,
|
|
|
|
|
openshot::LAYOUT_STEREO, bit_rate);
|
2020-01-31 03:53:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-07-12 15:55:41 -05:00
|
|
|
// Set custom options (some codecs accept additional params)
|
2019-08-04 23:10:59 -04:00
|
|
|
void FFmpegWriter::SetOption(StreamType stream, std::string name, std::string value) {
|
2012-08-03 03:23:10 -05:00
|
|
|
// Declare codec context
|
2012-08-04 01:11:12 -05:00
|
|
|
AVCodecContext *c = NULL;
|
2018-03-28 15:09:55 -05:00
|
|
|
AVStream *st = NULL;
|
2019-08-04 23:10:59 -04:00
|
|
|
std::stringstream convert(value);
|
2012-08-03 03:23:10 -05:00
|
|
|
|
2018-03-28 15:09:55 -05:00
|
|
|
if (info.has_video && stream == VIDEO_STREAM && video_st) {
|
|
|
|
|
st = video_st;
|
|
|
|
|
// Get codec context
|
|
|
|
|
c = AV_GET_CODEC_PAR_CONTEXT(st, video_codec);
|
2019-04-18 14:04:37 -05:00
|
|
|
} else if (info.has_audio && stream == AUDIO_STREAM && audio_st) {
|
2018-03-28 15:09:55 -05:00
|
|
|
st = audio_st;
|
|
|
|
|
// Get codec context
|
|
|
|
|
c = AV_GET_CODEC_PAR_CONTEXT(st, audio_codec);
|
2019-04-18 14:04:37 -05:00
|
|
|
} else
|
2015-02-05 00:06:07 -06:00
|
|
|
throw NoStreamsFound("The stream was not found. Be sure to call PrepareStreams() first.", path);
|
2012-08-03 03:23:10 -05:00
|
|
|
|
2012-08-04 01:11:12 -05:00
|
|
|
// Init AVOption
|
|
|
|
|
const AVOption *option = NULL;
|
2012-08-03 03:23:10 -05:00
|
|
|
|
2012-08-04 01:11:12 -05:00
|
|
|
// Was a codec / stream found?
|
|
|
|
|
if (c)
|
|
|
|
|
// Find AVOption (if it exists)
|
2018-03-28 15:09:55 -05:00
|
|
|
option = AV_OPTION_FIND(c->priv_data, name.c_str());
|
2012-08-04 01:11:12 -05:00
|
|
|
|
|
|
|
|
// Was option found?
|
2012-08-11 03:39:00 -05:00
|
|
|
if (option || (name == "g" || name == "qmin" || name == "qmax" || name == "max_b_frames" || name == "mb_decision" ||
|
2019-04-18 14:04:37 -05:00
|
|
|
name == "level" || name == "profile" || name == "slices" || name == "rc_min_rate" || name == "rc_max_rate" ||
|
2020-02-16 10:00:18 -08:00
|
|
|
name == "rc_buffer_size" || name == "crf" || name == "cqp" || name == "qp")) {
|
2012-08-11 02:59:03 -05:00
|
|
|
// Check for specific named options
|
|
|
|
|
if (name == "g")
|
|
|
|
|
// Set gop_size
|
|
|
|
|
convert >> c->gop_size;
|
|
|
|
|
|
|
|
|
|
else if (name == "qmin")
|
2012-08-11 20:28:05 -05:00
|
|
|
// Minimum quantizer
|
2012-08-11 02:59:03 -05:00
|
|
|
convert >> c->qmin;
|
|
|
|
|
|
|
|
|
|
else if (name == "qmax")
|
2012-08-11 20:28:05 -05:00
|
|
|
// Maximum quantizer
|
2012-08-11 02:59:03 -05:00
|
|
|
convert >> c->qmax;
|
|
|
|
|
|
2012-08-11 03:19:52 -05:00
|
|
|
else if (name == "max_b_frames")
|
2012-08-11 20:28:05 -05:00
|
|
|
// Maximum number of B-frames between non-B-frames
|
2012-08-11 03:19:52 -05:00
|
|
|
convert >> c->max_b_frames;
|
|
|
|
|
|
|
|
|
|
else if (name == "mb_decision")
|
2012-08-11 20:28:05 -05:00
|
|
|
// Macroblock decision mode
|
2012-08-11 03:19:52 -05:00
|
|
|
convert >> c->mb_decision;
|
|
|
|
|
|
2012-08-11 03:39:00 -05:00
|
|
|
else if (name == "level")
|
2012-08-11 20:28:05 -05:00
|
|
|
// Set codec level
|
2012-08-11 03:39:00 -05:00
|
|
|
convert >> c->level;
|
|
|
|
|
|
|
|
|
|
else if (name == "profile")
|
2012-08-11 20:28:05 -05:00
|
|
|
// Set codec profile
|
2012-08-11 03:39:00 -05:00
|
|
|
convert >> c->profile;
|
|
|
|
|
|
2012-08-11 20:28:05 -05:00
|
|
|
else if (name == "slices")
|
|
|
|
|
// Indicates number of picture subdivisions
|
|
|
|
|
convert >> c->slices;
|
|
|
|
|
|
|
|
|
|
else if (name == "rc_min_rate")
|
|
|
|
|
// Minimum bitrate
|
|
|
|
|
convert >> c->rc_min_rate;
|
|
|
|
|
|
|
|
|
|
else if (name == "rc_max_rate")
|
|
|
|
|
// Maximum bitrate
|
|
|
|
|
convert >> c->rc_max_rate;
|
|
|
|
|
|
2015-08-24 23:49:45 -05:00
|
|
|
else if (name == "rc_buffer_size")
|
|
|
|
|
// Buffer size
|
|
|
|
|
convert >> c->rc_buffer_size;
|
|
|
|
|
|
2019-08-23 19:46:05 +02:00
|
|
|
else if (name == "cqp") {
|
|
|
|
|
// encode quality and special settings like lossless
|
|
|
|
|
// This might be better in an extra methods as more options
|
|
|
|
|
// and way to set quality are possible
|
|
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 39, 101)
|
2020-02-10 01:50:31 -05:00
|
|
|
#if HAVE_HW_ACCEL
|
2019-08-23 19:46:05 +02:00
|
|
|
if (hw_en_on) {
|
2019-10-29 16:31:36 -04:00
|
|
|
av_opt_set_int(c->priv_data, "qp", std::min(std::stoi(value),63), 0); // 0-63
|
2019-08-23 19:46:05 +02:00
|
|
|
} else
|
2020-02-10 01:50:31 -05:00
|
|
|
#endif // HAVE_HW_ACCEL
|
2019-08-23 19:46:05 +02:00
|
|
|
{
|
|
|
|
|
switch (c->codec_id) {
|
|
|
|
|
#if (LIBAVCODEC_VERSION_MAJOR >= 58)
|
|
|
|
|
case AV_CODEC_ID_AV1 :
|
|
|
|
|
c->bit_rate = 0;
|
2019-10-29 16:31:36 -04:00
|
|
|
av_opt_set_int(c->priv_data, "qp", std::min(std::stoi(value),63), 0); // 0-63
|
2019-08-23 19:46:05 +02:00
|
|
|
break;
|
|
|
|
|
#endif
|
|
|
|
|
case AV_CODEC_ID_VP8 :
|
|
|
|
|
c->bit_rate = 10000000;
|
2019-10-29 16:31:36 -04:00
|
|
|
av_opt_set_int(c->priv_data, "qp", std::max(std::min(std::stoi(value), 63), 4), 0); // 4-63
|
2019-08-23 19:46:05 +02:00
|
|
|
break;
|
|
|
|
|
case AV_CODEC_ID_VP9 :
|
|
|
|
|
c->bit_rate = 0; // Must be zero!
|
2019-10-29 16:31:36 -04:00
|
|
|
av_opt_set_int(c->priv_data, "qp", std::min(std::stoi(value), 63), 0); // 0-63
|
|
|
|
|
if (std::stoi(value) == 0) {
|
2019-08-23 19:46:05 +02:00
|
|
|
av_opt_set(c->priv_data, "preset", "veryslow", 0);
|
|
|
|
|
av_opt_set_int(c->priv_data, "lossless", 1, 0);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case AV_CODEC_ID_H264 :
|
2019-10-29 16:31:36 -04:00
|
|
|
av_opt_set_int(c->priv_data, "qp", std::min(std::stoi(value), 51), 0); // 0-51
|
|
|
|
|
if (std::stoi(value) == 0) {
|
2019-08-23 19:46:05 +02:00
|
|
|
av_opt_set(c->priv_data, "preset", "veryslow", 0);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2019-12-11 05:42:41 -05:00
|
|
|
case AV_CODEC_ID_HEVC :
|
2019-10-29 16:31:36 -04:00
|
|
|
av_opt_set_int(c->priv_data, "qp", std::min(std::stoi(value), 51), 0); // 0-51
|
|
|
|
|
if (std::stoi(value) == 0) {
|
2019-08-23 19:46:05 +02:00
|
|
|
av_opt_set(c->priv_data, "preset", "veryslow", 0);
|
|
|
|
|
av_opt_set_int(c->priv_data, "lossless", 1, 0);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// For all other codecs assume a range of 0-63
|
2019-10-29 16:31:36 -04:00
|
|
|
av_opt_set_int(c->priv_data, "qp", std::min(std::stoi(value), 63), 0); // 0-63
|
2019-08-23 19:46:05 +02:00
|
|
|
c->bit_rate = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
} else if (name == "crf") {
|
2019-01-25 00:15:07 -06:00
|
|
|
// encode quality and special settings like lossless
|
|
|
|
|
// This might be better in an extra methods as more options
|
|
|
|
|
// and way to set quality are possible
|
2019-04-18 14:04:37 -05:00
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 39, 101)
|
2020-02-10 01:50:31 -05:00
|
|
|
#if HAVE_HW_ACCEL
|
2019-04-18 14:04:37 -05:00
|
|
|
if (hw_en_on) {
|
|
|
|
|
double mbs = 15000000.0;
|
|
|
|
|
if (info.video_bit_rate > 0) {
|
|
|
|
|
if (info.video_bit_rate > 42) {
|
|
|
|
|
mbs = 380000.0;
|
2019-01-29 12:38:52 -08:00
|
|
|
}
|
2019-04-18 14:04:37 -05:00
|
|
|
else {
|
2019-10-29 16:31:36 -04:00
|
|
|
mbs *= std::pow(0.912,info.video_bit_rate);
|
2019-01-25 00:15:07 -06:00
|
|
|
}
|
2019-01-31 09:42:26 -08:00
|
|
|
}
|
2019-04-18 14:04:37 -05:00
|
|
|
c->bit_rate = (int)(mbs);
|
|
|
|
|
} else
|
2020-02-10 01:50:31 -05:00
|
|
|
#endif // HAVE_HW_ACCEL
|
2019-04-18 14:04:37 -05:00
|
|
|
{
|
|
|
|
|
switch (c->codec_id) {
|
|
|
|
|
#if (LIBAVCODEC_VERSION_MAJOR >= 58)
|
|
|
|
|
case AV_CODEC_ID_AV1 :
|
|
|
|
|
c->bit_rate = 0;
|
2020-06-15 14:03:29 -07:00
|
|
|
// AV1 only supports "crf" quality values
|
2019-10-29 16:31:36 -04:00
|
|
|
av_opt_set_int(c->priv_data, "crf", std::min(std::stoi(value),63), 0);
|
2019-04-18 14:04:37 -05:00
|
|
|
break;
|
|
|
|
|
#endif
|
|
|
|
|
case AV_CODEC_ID_VP8 :
|
|
|
|
|
c->bit_rate = 10000000;
|
2019-10-29 16:31:36 -04:00
|
|
|
av_opt_set_int(c->priv_data, "crf", std::max(std::min(std::stoi(value), 63), 4), 0); // 4-63
|
2019-04-18 14:04:37 -05:00
|
|
|
break;
|
|
|
|
|
case AV_CODEC_ID_VP9 :
|
|
|
|
|
c->bit_rate = 0; // Must be zero!
|
2019-10-29 16:31:36 -04:00
|
|
|
av_opt_set_int(c->priv_data, "crf", std::min(std::stoi(value), 63), 0); // 0-63
|
|
|
|
|
if (std::stoi(value) == 0) {
|
2019-04-18 14:04:37 -05:00
|
|
|
av_opt_set(c->priv_data, "preset", "veryslow", 0);
|
|
|
|
|
av_opt_set_int(c->priv_data, "lossless", 1, 0);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case AV_CODEC_ID_H264 :
|
2019-10-29 16:31:36 -04:00
|
|
|
av_opt_set_int(c->priv_data, "crf", std::min(std::stoi(value), 51), 0); // 0-51
|
|
|
|
|
if (std::stoi(value) == 0) {
|
2019-04-18 14:04:37 -05:00
|
|
|
av_opt_set(c->priv_data, "preset", "veryslow", 0);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2019-12-11 05:42:41 -05:00
|
|
|
case AV_CODEC_ID_HEVC :
|
2020-02-17 10:56:00 -08:00
|
|
|
if (strstr(info.vcodec.c_str(), "svt_hevc") != NULL) {
|
|
|
|
|
av_opt_set_int(c->priv_data, "preset", 7, 0);
|
|
|
|
|
av_opt_set_int(c->priv_data, "forced-idr",1,0);
|
|
|
|
|
av_opt_set_int(c->priv_data, "qp",std::min(std::stoi(value), 51),0);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
av_opt_set_int(c->priv_data, "crf", std::min(std::stoi(value), 51), 0); // 0-51
|
|
|
|
|
}
|
2019-10-29 16:31:36 -04:00
|
|
|
if (std::stoi(value) == 0) {
|
2019-04-18 14:04:37 -05:00
|
|
|
av_opt_set(c->priv_data, "preset", "veryslow", 0);
|
|
|
|
|
av_opt_set_int(c->priv_data, "lossless", 1, 0);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// If this codec doesn't support crf calculate a bitrate
|
|
|
|
|
// TODO: find better formula
|
|
|
|
|
double mbs = 15000000.0;
|
|
|
|
|
if (info.video_bit_rate > 0) {
|
|
|
|
|
if (info.video_bit_rate > 42) {
|
|
|
|
|
mbs = 380000.0;
|
|
|
|
|
} else {
|
2019-10-29 16:31:36 -04:00
|
|
|
mbs *= std::pow(0.912, info.video_bit_rate);
|
2019-04-18 14:04:37 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
c->bit_rate = (int) (mbs);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-16 10:00:18 -08:00
|
|
|
#endif
|
|
|
|
|
} else if (name == "qp") {
|
|
|
|
|
// encode quality and special settings like lossless
|
|
|
|
|
// This might be better in an extra methods as more options
|
|
|
|
|
// and way to set quality are possible
|
|
|
|
|
#if (LIBAVCODEC_VERSION_MAJOR >= 58)
|
|
|
|
|
switch (c->codec_id) {
|
|
|
|
|
case AV_CODEC_ID_AV1 :
|
|
|
|
|
c->bit_rate = 0;
|
|
|
|
|
if (strstr(info.vcodec.c_str(), "svt_av1") != NULL) {
|
|
|
|
|
av_opt_set_int(c->priv_data, "qp", std::min(std::stoi(value),63), 0);
|
|
|
|
|
}
|
2020-02-16 19:16:45 -08:00
|
|
|
else if (strstr(info.vcodec.c_str(), "rav1e") != NULL) {
|
|
|
|
|
// Set number of tiles to a fixed value
|
|
|
|
|
// TODO Let user choose number of tiles
|
2020-02-16 10:00:18 -08:00
|
|
|
av_opt_set_int(c->priv_data, "qp", std::min(std::stoi(value),255), 0);
|
|
|
|
|
}
|
2020-02-16 19:16:45 -08:00
|
|
|
else if (strstr(info.vcodec.c_str(), "aom") != NULL) {
|
|
|
|
|
// Set number of tiles to a fixed value
|
|
|
|
|
// TODO Let user choose number of tiles
|
|
|
|
|
// libaom doesn't have qp only crf
|
2020-02-16 10:00:18 -08:00
|
|
|
av_opt_set_int(c->priv_data, "crf", std::min(std::stoi(value),63), 0);
|
|
|
|
|
}
|
2020-02-16 19:16:45 -08:00
|
|
|
else {
|
|
|
|
|
av_opt_set_int(c->priv_data, "crf", std::min(std::stoi(value),63), 0);
|
|
|
|
|
}
|
2020-02-17 10:56:00 -08:00
|
|
|
case AV_CODEC_ID_HEVC :
|
|
|
|
|
c->bit_rate = 0;
|
|
|
|
|
if (strstr(info.vcodec.c_str(), "svt_hevc") != NULL) {
|
|
|
|
|
av_opt_set_int(c->priv_data, "qp", std::min(std::stoi(value),51), 0);
|
|
|
|
|
av_opt_set_int(c->priv_data, "preset", 7, 0);
|
|
|
|
|
av_opt_set_int(c->priv_data, "forced-idr",1,0);
|
|
|
|
|
}
|
2020-02-16 10:00:18 -08:00
|
|
|
break;
|
|
|
|
|
}
|
2019-04-18 14:04:37 -05:00
|
|
|
#endif
|
|
|
|
|
} else {
|
2012-08-11 02:59:03 -05:00
|
|
|
// Set AVOption
|
2018-03-28 15:09:55 -05:00
|
|
|
AV_OPTION_SET(st, c->priv_data, name.c_str(), value.c_str(), c);
|
2019-04-18 14:04:37 -05:00
|
|
|
}
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2019-08-04 23:10:59 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::SetOption (" + (std::string)name + ")", "stream == VIDEO_STREAM", stream == VIDEO_STREAM);
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2019-06-11 10:49:45 +03:00
|
|
|
// Muxing dictionary is not part of the codec context.
|
|
|
|
|
// Just reusing SetOption function to set popular multiplexing presets.
|
|
|
|
|
} else if (name == "muxing_preset") {
|
|
|
|
|
if (value == "mp4_faststart") {
|
|
|
|
|
// 'moov' box to the beginning; only for MOV, MP4
|
|
|
|
|
av_dict_set(&mux_dict, "movflags", "faststart", 0);
|
|
|
|
|
} else if (value == "mp4_fragmented") {
|
|
|
|
|
// write selfcontained fragmented file, minimum length of the fragment 8 sec; only for MOV, MP4
|
|
|
|
|
av_dict_set(&mux_dict, "movflags", "frag_keyframe", 0);
|
|
|
|
|
av_dict_set(&mux_dict, "min_frag_duration", "8000000", 0);
|
2019-08-23 19:46:05 +02:00
|
|
|
}
|
2019-04-18 14:04:37 -05:00
|
|
|
} else {
|
2012-08-04 01:11:12 -05:00
|
|
|
throw InvalidOptions("The option is not valid for this codec.", path);
|
2019-04-18 14:04:37 -05:00
|
|
|
}
|
2012-07-12 15:55:41 -05:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-28 21:57:56 -05:00
|
|
|
/// Determine if codec name is valid
|
2019-08-04 23:10:59 -04:00
|
|
|
bool FFmpegWriter::IsValidCodec(std::string codec_name) {
|
2018-06-29 15:06:34 -05:00
|
|
|
// Initialize FFMpeg, and register all formats and codecs
|
2018-08-11 18:22:18 -05:00
|
|
|
AV_REGISTER_ALL
|
2018-06-29 15:06:34 -05:00
|
|
|
|
2018-06-28 21:57:56 -05:00
|
|
|
// Find the codec (if any)
|
|
|
|
|
if (avcodec_find_encoder_by_name(codec_name.c_str()) == NULL)
|
|
|
|
|
return false;
|
|
|
|
|
else
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-11 03:19:52 -05:00
|
|
|
// Prepare & initialize streams and open codecs
|
2019-04-18 14:04:37 -05:00
|
|
|
void FFmpegWriter::PrepareStreams() {
|
2012-07-19 17:10:40 -05:00
|
|
|
if (!info.has_audio && !info.has_video)
|
2012-07-20 00:13:16 -05:00
|
|
|
throw InvalidOptions("No video or audio options have been set. You must set has_video or has_audio (or both).", path);
|
2012-07-19 17:10:40 -05:00
|
|
|
|
2019-07-03 14:14:02 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::PrepareStreams [" + path + "]", "info.has_audio", info.has_audio, "info.has_video", info.has_video);
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2012-08-03 03:23:10 -05:00
|
|
|
// Initialize the streams (i.e. add the streams)
|
2012-07-19 15:03:55 -05:00
|
|
|
initialize_streams();
|
2012-07-12 15:55:41 -05:00
|
|
|
|
2015-02-05 00:06:07 -06:00
|
|
|
// Mark as 'prepared'
|
|
|
|
|
prepare_streams = true;
|
2012-08-11 03:19:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write the file header (after the options are set)
|
2019-04-18 14:04:37 -05:00
|
|
|
void FFmpegWriter::WriteHeader() {
|
2012-08-11 03:19:52 -05:00
|
|
|
if (!info.has_audio && !info.has_video)
|
|
|
|
|
throw InvalidOptions("No video or audio options have been set. You must set has_video or has_audio (or both).", path);
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
// Open the output file, if needed
|
|
|
|
|
if (!(fmt->flags & AVFMT_NOFILE)) {
|
|
|
|
|
if (avio_open(&oc->pb, path.c_str(), AVIO_FLAG_WRITE) < 0)
|
|
|
|
|
throw InvalidFile("Could not open or write file.", path);
|
|
|
|
|
}
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
// Force the output filename (which doesn't always happen for some reason)
|
2019-08-06 11:59:55 -04:00
|
|
|
AV_SET_FILENAME(oc, path.c_str());
|
2018-03-04 03:10:59 -06:00
|
|
|
|
|
|
|
|
// Add general metadata (if any)
|
2019-08-04 23:10:59 -04:00
|
|
|
for (std::map<std::string, std::string>::iterator iter = info.metadata.begin(); iter != info.metadata.end(); ++iter) {
|
2018-03-04 03:10:59 -06:00
|
|
|
av_dict_set(&oc->metadata, iter->first.c_str(), iter->second.c_str(), 0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-11 10:49:45 +03:00
|
|
|
// Set multiplexing parameters
|
|
|
|
|
AVDictionary *dict = NULL;
|
|
|
|
|
|
|
|
|
|
bool is_mp4 = strcmp(oc->oformat->name, "mp4");
|
|
|
|
|
bool is_mov = strcmp(oc->oformat->name, "mov");
|
|
|
|
|
// Set dictionary preset only for MP4 and MOV files
|
|
|
|
|
if (is_mp4 || is_mov)
|
|
|
|
|
av_dict_copy(&dict, mux_dict, 0);
|
|
|
|
|
|
2019-08-06 11:59:55 -04:00
|
|
|
// Write the stream header
|
2019-06-11 10:49:45 +03:00
|
|
|
if (avformat_write_header(oc, &dict) != 0) {
|
2019-08-23 19:46:05 +02:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::WriteHeader (avformat_write_header)");
|
2019-04-18 14:04:37 -05:00
|
|
|
throw InvalidFile("Could not write header to file.", path);
|
2018-03-28 15:09:55 -05:00
|
|
|
};
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2019-06-11 10:49:45 +03:00
|
|
|
// Free multiplexing dictionaries sets
|
|
|
|
|
if (dict) av_dict_free(&dict);
|
|
|
|
|
if (mux_dict) av_dict_free(&mux_dict);
|
|
|
|
|
|
2015-02-05 00:06:07 -06:00
|
|
|
// Mark as 'written'
|
|
|
|
|
write_header = true;
|
|
|
|
|
|
2019-07-03 14:14:02 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::WriteHeader");
|
2012-07-12 15:55:41 -05:00
|
|
|
}
|
|
|
|
|
|
2012-08-20 00:02:09 -05:00
|
|
|
// Add a frame to the queue waiting to be encoded.
|
2019-04-18 14:04:37 -05:00
|
|
|
void FFmpegWriter::WriteFrame(std::shared_ptr<Frame> frame) {
|
2015-02-05 00:06:07 -06:00
|
|
|
// Check for open reader (or throw exception)
|
|
|
|
|
if (!is_open)
|
|
|
|
|
throw WriterClosed("The FFmpegWriter is closed. Call Open() before calling this method.", path);
|
|
|
|
|
|
2012-08-20 00:02:09 -05:00
|
|
|
// Add frame pointer to "queue", waiting to be processed the next
|
|
|
|
|
// time the WriteFrames() method is called.
|
2012-08-20 02:59:35 -05:00
|
|
|
if (info.has_video && video_st)
|
2012-08-28 15:53:18 -05:00
|
|
|
spooled_video_frames.push_back(frame);
|
2012-08-20 02:59:35 -05:00
|
|
|
|
|
|
|
|
if (info.has_audio && audio_st)
|
2012-08-28 15:53:18 -05:00
|
|
|
spooled_audio_frames.push_back(frame);
|
2012-08-20 14:26:49 -05:00
|
|
|
|
2019-07-03 14:14:02 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::WriteFrame", "frame->number", frame->number, "spooled_video_frames.size()", spooled_video_frames.size(), "spooled_audio_frames.size()", spooled_audio_frames.size(), "cache_size", cache_size, "is_writing", is_writing);
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2012-08-20 14:26:49 -05:00
|
|
|
// Write the frames once it reaches the correct cache size
|
2019-12-15 14:22:59 -05:00
|
|
|
if ((int)spooled_video_frames.size() == cache_size || (int)spooled_audio_frames.size() == cache_size) {
|
2012-08-28 15:53:18 -05:00
|
|
|
// Is writer currently writing?
|
|
|
|
|
if (!is_writing)
|
|
|
|
|
// Write frames to video file
|
|
|
|
|
write_queued_frames();
|
|
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
else {
|
2012-08-28 15:53:18 -05:00
|
|
|
// Write frames to video file
|
|
|
|
|
write_queued_frames();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-12 00:54:53 -05:00
|
|
|
// Keep track of the last frame added
|
|
|
|
|
last_frame = frame;
|
2012-08-20 00:02:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write all frames in the queue to the video file.
|
2019-04-18 14:04:37 -05:00
|
|
|
void FFmpegWriter::write_queued_frames() {
|
2019-07-03 14:14:02 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_queued_frames", "spooled_video_frames.size()", spooled_video_frames.size(), "spooled_audio_frames.size()", spooled_audio_frames.size());
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2012-08-28 15:53:18 -05:00
|
|
|
// Flip writing flag
|
|
|
|
|
is_writing = true;
|
|
|
|
|
|
|
|
|
|
// Transfer spool to queue
|
|
|
|
|
queued_video_frames = spooled_video_frames;
|
|
|
|
|
queued_audio_frames = spooled_audio_frames;
|
|
|
|
|
|
|
|
|
|
// Empty spool
|
|
|
|
|
spooled_video_frames.clear();
|
|
|
|
|
spooled_audio_frames.clear();
|
|
|
|
|
|
2014-04-02 16:48:27 -05:00
|
|
|
// Set the number of threads in OpenMP
|
|
|
|
|
omp_set_num_threads(OPEN_MP_NUM_PROCESSORS);
|
|
|
|
|
// Allow nested OpenMP sections
|
2012-11-12 17:21:21 -06:00
|
|
|
omp_set_nested(true);
|
2014-04-02 16:48:27 -05:00
|
|
|
|
2016-02-02 01:09:02 -06:00
|
|
|
// Create blank exception
|
|
|
|
|
bool has_error_encoding_video = false;
|
|
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
#pragma omp parallel
|
2012-08-20 00:02:09 -05:00
|
|
|
{
|
2019-04-18 14:04:37 -05:00
|
|
|
#pragma omp single
|
2012-08-20 00:02:09 -05:00
|
|
|
{
|
2012-08-20 02:59:35 -05:00
|
|
|
// Process all audio frames (in a separate thread)
|
2012-08-28 15:53:18 -05:00
|
|
|
if (info.has_audio && audio_st && !queued_audio_frames.empty())
|
2012-10-28 03:35:50 -05:00
|
|
|
write_audio_packets(false);
|
2012-08-20 02:59:35 -05:00
|
|
|
|
|
|
|
|
// Loop through each queued image frame
|
2019-04-18 14:04:37 -05:00
|
|
|
while (!queued_video_frames.empty()) {
|
2012-08-20 00:02:09 -05:00
|
|
|
// Get front frame (from the queue)
|
2017-08-20 17:37:39 -05:00
|
|
|
std::shared_ptr<Frame> frame = queued_video_frames.front();
|
2012-08-20 00:02:09 -05:00
|
|
|
|
|
|
|
|
// Add to processed queue
|
|
|
|
|
processed_frames.push_back(frame);
|
|
|
|
|
|
|
|
|
|
// Encode and add the frame to the output file
|
|
|
|
|
if (info.has_video && video_st)
|
|
|
|
|
process_video_packet(frame);
|
|
|
|
|
|
|
|
|
|
// Remove front item
|
2012-08-28 15:53:18 -05:00
|
|
|
queued_video_frames.pop_front();
|
2012-08-20 00:02:09 -05:00
|
|
|
|
|
|
|
|
} // end while
|
2012-08-28 15:53:18 -05:00
|
|
|
} // end omp single
|
2012-08-20 00:02:09 -05:00
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
#pragma omp single
|
2012-08-20 00:02:09 -05:00
|
|
|
{
|
2012-08-28 15:53:18 -05:00
|
|
|
// Loop back through the frames (in order), and write them to the video file
|
2019-04-18 14:04:37 -05:00
|
|
|
while (!processed_frames.empty()) {
|
2012-08-28 15:53:18 -05:00
|
|
|
// Get front frame (from the queue)
|
2017-08-20 17:37:39 -05:00
|
|
|
std::shared_ptr<Frame> frame = processed_frames.front();
|
2012-08-20 00:02:09 -05:00
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
if (info.has_video && video_st) {
|
2012-08-28 15:53:18 -05:00
|
|
|
// Add to deallocate queue (so we can remove the AVFrames when we are done)
|
|
|
|
|
deallocate_frames.push_back(frame);
|
|
|
|
|
|
|
|
|
|
// Does this frame's AVFrame still exist
|
2019-04-18 14:04:37 -05:00
|
|
|
if (av_frames.count(frame)) {
|
2012-08-28 15:53:18 -05:00
|
|
|
// Get AVFrame
|
|
|
|
|
AVFrame *frame_final = av_frames[frame];
|
|
|
|
|
|
|
|
|
|
// Write frame to video file
|
2016-02-02 01:09:02 -06:00
|
|
|
bool success = write_video_packet(frame, frame_final);
|
|
|
|
|
if (!success)
|
|
|
|
|
has_error_encoding_video = true;
|
2012-08-28 15:53:18 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove front item
|
|
|
|
|
processed_frames.pop_front();
|
2012-08-20 14:26:49 -05:00
|
|
|
}
|
2012-08-20 00:02:09 -05:00
|
|
|
|
2012-08-28 15:53:18 -05:00
|
|
|
// Loop through, and deallocate AVFrames
|
2019-04-18 14:04:37 -05:00
|
|
|
while (!deallocate_frames.empty()) {
|
2012-08-28 15:53:18 -05:00
|
|
|
// Get front frame (from the queue)
|
2017-08-20 17:37:39 -05:00
|
|
|
std::shared_ptr<Frame> frame = deallocate_frames.front();
|
2012-08-03 03:23:10 -05:00
|
|
|
|
2012-08-28 15:53:18 -05:00
|
|
|
// Does this frame's AVFrame still exist
|
2019-04-18 14:04:37 -05:00
|
|
|
if (av_frames.count(frame)) {
|
2012-08-28 15:53:18 -05:00
|
|
|
// Get AVFrame
|
|
|
|
|
AVFrame *av_frame = av_frames[frame];
|
2012-08-20 02:59:35 -05:00
|
|
|
|
2015-09-23 00:27:28 -05:00
|
|
|
// Deallocate AVPicture and AVFrame
|
2016-07-27 13:18:55 -05:00
|
|
|
av_freep(&(av_frame->data[0]));
|
2015-09-23 00:27:28 -05:00
|
|
|
AV_FREE_FRAME(&av_frame);
|
2012-08-28 15:53:18 -05:00
|
|
|
av_frames.erase(frame);
|
|
|
|
|
}
|
2012-08-20 02:59:35 -05:00
|
|
|
|
2012-08-28 15:53:18 -05:00
|
|
|
// Remove front item
|
|
|
|
|
deallocate_frames.pop_front();
|
|
|
|
|
}
|
2012-08-20 02:59:35 -05:00
|
|
|
|
2012-08-28 15:53:18 -05:00
|
|
|
// Done writing
|
|
|
|
|
is_writing = false;
|
|
|
|
|
|
|
|
|
|
} // end omp single
|
2018-08-31 21:36:23 -07:00
|
|
|
|
2012-08-28 15:53:18 -05:00
|
|
|
} // end omp parallel
|
2012-08-20 02:59:35 -05:00
|
|
|
|
2016-02-02 01:09:02 -06:00
|
|
|
// Raise exception from main thread
|
|
|
|
|
if (has_error_encoding_video)
|
|
|
|
|
throw ErrorEncodingVideo("Error while writing raw video frame", -1);
|
2012-07-12 15:55:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write a block of frames from a reader
|
2019-04-18 14:04:37 -05:00
|
|
|
void FFmpegWriter::WriteFrame(ReaderBase *reader, int64_t start, int64_t length) {
|
2019-07-03 14:14:02 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::WriteFrame (from Reader)", "start", start, "length", length);
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2012-08-20 14:26:49 -05:00
|
|
|
// Loop through each frame (and encoded it)
|
2019-04-18 14:04:37 -05:00
|
|
|
for (int64_t number = start; number <= length; number++) {
|
2012-08-20 14:26:49 -05:00
|
|
|
// Get the frame
|
2017-08-20 17:37:39 -05:00
|
|
|
std::shared_ptr<Frame> f = reader->GetFrame(number);
|
2012-08-20 14:26:49 -05:00
|
|
|
|
|
|
|
|
// Encode frame
|
|
|
|
|
WriteFrame(f);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-12 15:55:41 -05:00
|
|
|
|
|
|
|
|
// Write the file trailer (after all frames are written)
|
2019-04-18 14:04:37 -05:00
|
|
|
void FFmpegWriter::WriteTrailer() {
|
2012-10-28 03:35:50 -05:00
|
|
|
// Write any remaining queued frames to video file
|
|
|
|
|
write_queued_frames();
|
|
|
|
|
|
|
|
|
|
// Process final audio frame (if any)
|
|
|
|
|
if (info.has_audio && audio_st)
|
|
|
|
|
write_audio_packets(true);
|
|
|
|
|
|
|
|
|
|
// Flush encoders (who sometimes hold on to frames)
|
|
|
|
|
flush_encoders();
|
2012-10-12 00:54:53 -05:00
|
|
|
|
|
|
|
|
/* write the trailer, if any. The trailer must be written
|
2012-07-25 17:20:02 -05:00
|
|
|
* before you close the CodecContexts open when you wrote the
|
|
|
|
|
* header; otherwise write_trailer may try to use memory that
|
|
|
|
|
* was freed on av_codec_close() */
|
|
|
|
|
av_write_trailer(oc);
|
2015-02-05 00:06:07 -06:00
|
|
|
|
|
|
|
|
// Mark as 'written'
|
|
|
|
|
write_trailer = true;
|
|
|
|
|
|
2019-07-03 14:14:02 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::WriteTrailer");
|
2012-07-19 15:03:55 -05:00
|
|
|
}
|
2012-07-12 15:55:41 -05:00
|
|
|
|
2012-10-28 03:35:50 -05:00
|
|
|
// Flush encoders
|
2019-04-18 14:04:37 -05:00
|
|
|
void FFmpegWriter::flush_encoders() {
|
2018-03-28 15:09:55 -05:00
|
|
|
if (info.has_audio && audio_codec && AV_GET_CODEC_TYPE(audio_st) == AVMEDIA_TYPE_AUDIO && AV_GET_CODEC_ATTRIBUTES(audio_st, audio_codec)->frame_size <= 1)
|
|
|
|
|
return;
|
2018-08-11 18:22:18 -05:00
|
|
|
#if (LIBAVFORMAT_VERSION_MAJOR < 58)
|
2018-03-28 15:09:55 -05:00
|
|
|
if (info.has_video && video_codec && AV_GET_CODEC_TYPE(video_st) == AVMEDIA_TYPE_VIDEO && (oc->oformat->flags & AVFMT_RAWPICTURE) && AV_FIND_DECODER_CODEC_ID(video_st) == AV_CODEC_ID_RAWVIDEO)
|
|
|
|
|
return;
|
2020-03-01 16:59:06 -08:00
|
|
|
#else
|
|
|
|
|
if (info.has_video && video_codec && AV_GET_CODEC_TYPE(video_st) == AVMEDIA_TYPE_VIDEO && AV_FIND_DECODER_CODEC_ID(video_st) == AV_CODEC_ID_RAWVIDEO)
|
|
|
|
|
return;
|
2018-08-11 18:22:18 -05:00
|
|
|
#endif
|
2012-10-28 03:35:50 -05:00
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
int error_code = 0;
|
|
|
|
|
int stop_encoding = 1;
|
2012-10-28 03:35:50 -05:00
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
// FLUSH VIDEO ENCODER
|
|
|
|
|
if (info.has_video)
|
2012-10-28 03:35:50 -05:00
|
|
|
for (;;) {
|
|
|
|
|
|
|
|
|
|
// Increment PTS (in frames and scaled to the codec's timebase)
|
2019-04-18 14:04:37 -05:00
|
|
|
write_video_count += av_rescale_q(1, (AVRational) {info.fps.den, info.fps.num}, video_codec->time_base);
|
2012-10-28 03:35:50 -05:00
|
|
|
|
|
|
|
|
AVPacket pkt;
|
|
|
|
|
av_init_packet(&pkt);
|
|
|
|
|
pkt.data = NULL;
|
|
|
|
|
pkt.size = 0;
|
|
|
|
|
|
2012-11-17 03:13:00 -06:00
|
|
|
// Pointer for video buffer (if using old FFmpeg version)
|
|
|
|
|
uint8_t *video_outbuf = NULL;
|
|
|
|
|
|
2012-10-28 03:35:50 -05:00
|
|
|
/* encode the image */
|
|
|
|
|
int got_packet = 0;
|
2012-11-17 03:13:00 -06:00
|
|
|
int error_code = 0;
|
|
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
#if IS_FFMPEG_3_2
|
2018-03-28 15:09:55 -05:00
|
|
|
#pragma omp critical (write_video_packet)
|
|
|
|
|
{
|
|
|
|
|
// Encode video packet (latest version of FFmpeg)
|
|
|
|
|
error_code = avcodec_send_frame(video_codec, NULL);
|
|
|
|
|
got_packet = 0;
|
2018-07-16 21:18:35 +10:00
|
|
|
while (error_code >= 0) {
|
|
|
|
|
error_code = avcodec_receive_packet(video_codec, &pkt);
|
|
|
|
|
if (error_code == AVERROR(EAGAIN)|| error_code == AVERROR_EOF) {
|
|
|
|
|
got_packet = 0;
|
|
|
|
|
// Write packet
|
|
|
|
|
avcodec_flush_buffers(video_codec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (pkt.pts != AV_NOPTS_VALUE)
|
|
|
|
|
pkt.pts = av_rescale_q(pkt.pts, video_codec->time_base, video_st->time_base);
|
|
|
|
|
if (pkt.dts != AV_NOPTS_VALUE)
|
|
|
|
|
pkt.dts = av_rescale_q(pkt.dts, video_codec->time_base, video_st->time_base);
|
|
|
|
|
if (pkt.duration > 0)
|
|
|
|
|
pkt.duration = av_rescale_q(pkt.duration, video_codec->time_base, video_st->time_base);
|
|
|
|
|
pkt.stream_index = video_st->index;
|
|
|
|
|
error_code = av_interleaved_write_frame(oc, &pkt);
|
|
|
|
|
}
|
2018-03-28 15:09:55 -05:00
|
|
|
}
|
2019-04-18 14:04:37 -05:00
|
|
|
#else // IS_FFMPEG_3_2
|
2012-11-17 03:13:00 -06:00
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
#if LIBAVFORMAT_VERSION_MAJOR >= 54
|
|
|
|
|
// Encode video packet (older than FFmpeg 3.2)
|
|
|
|
|
error_code = avcodec_encode_video2(video_codec, &pkt, NULL, &got_packet);
|
2012-11-17 03:13:00 -06:00
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
#else
|
|
|
|
|
// Encode video packet (even older version of FFmpeg)
|
|
|
|
|
int video_outbuf_size = 0;
|
2012-11-17 03:13:00 -06:00
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
/* encode the image */
|
|
|
|
|
int out_size = avcodec_encode_video(video_codec, NULL, video_outbuf_size, NULL);
|
2012-11-17 03:13:00 -06:00
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
/* if zero size, it means the image was buffered */
|
|
|
|
|
if (out_size > 0) {
|
|
|
|
|
if(video_codec->coded_frame->key_frame)
|
|
|
|
|
pkt.flags |= AV_PKT_FLAG_KEY;
|
|
|
|
|
pkt.data= video_outbuf;
|
|
|
|
|
pkt.size= out_size;
|
2018-03-28 15:09:55 -05:00
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
// got data back (so encode this frame)
|
|
|
|
|
got_packet = 1;
|
|
|
|
|
}
|
|
|
|
|
#endif // LIBAVFORMAT_VERSION_MAJOR >= 54
|
|
|
|
|
#endif // IS_FFMPEG_3_2
|
2012-11-17 03:13:00 -06:00
|
|
|
|
2012-10-28 03:35:50 -05:00
|
|
|
if (error_code < 0) {
|
2019-08-04 23:10:59 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::flush_encoders ERROR [" + (std::string) av_err2str(error_code) + "]", "error_code", error_code);
|
2012-10-28 03:35:50 -05:00
|
|
|
}
|
|
|
|
|
if (!got_packet) {
|
|
|
|
|
stop_encoding = 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Override PTS (in frames and scaled to the codec's timebase)
|
|
|
|
|
//pkt.pts = write_video_count;
|
|
|
|
|
|
|
|
|
|
// set the timestamp
|
|
|
|
|
if (pkt.pts != AV_NOPTS_VALUE)
|
|
|
|
|
pkt.pts = av_rescale_q(pkt.pts, video_codec->time_base, video_st->time_base);
|
|
|
|
|
if (pkt.dts != AV_NOPTS_VALUE)
|
|
|
|
|
pkt.dts = av_rescale_q(pkt.dts, video_codec->time_base, video_st->time_base);
|
|
|
|
|
if (pkt.duration > 0)
|
|
|
|
|
pkt.duration = av_rescale_q(pkt.duration, video_codec->time_base, video_st->time_base);
|
|
|
|
|
pkt.stream_index = video_st->index;
|
|
|
|
|
|
|
|
|
|
// Write packet
|
2012-11-16 17:15:44 -06:00
|
|
|
error_code = av_interleaved_write_frame(oc, &pkt);
|
2015-10-01 13:00:50 -05:00
|
|
|
if (error_code < 0) {
|
2019-08-04 23:10:59 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::flush_encoders ERROR [" + (std::string)av_err2str(error_code) + "]", "error_code", error_code);
|
2012-10-28 03:35:50 -05:00
|
|
|
}
|
2012-11-17 03:13:00 -06:00
|
|
|
|
|
|
|
|
// Deallocate memory (if needed)
|
|
|
|
|
if (video_outbuf)
|
2016-07-31 15:15:48 -05:00
|
|
|
av_freep(&video_outbuf);
|
2012-10-28 03:35:50 -05:00
|
|
|
}
|
|
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
// FLUSH AUDIO ENCODER
|
|
|
|
|
if (info.has_audio)
|
2012-10-28 03:35:50 -05:00
|
|
|
for (;;) {
|
|
|
|
|
|
|
|
|
|
// Increment PTS (in samples and scaled to the codec's timebase)
|
2012-12-03 13:03:04 -06:00
|
|
|
#if LIBAVFORMAT_VERSION_MAJOR >= 54
|
|
|
|
|
// for some reason, it requires me to multiply channels X 2
|
|
|
|
|
write_audio_count += av_rescale_q(audio_input_position / (audio_codec->channels * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16)), (AVRational){1, info.sample_rate}, audio_codec->time_base);
|
|
|
|
|
#else
|
|
|
|
|
write_audio_count += av_rescale_q(audio_input_position / audio_codec->channels, (AVRational){1, info.sample_rate}, audio_codec->time_base);
|
|
|
|
|
#endif
|
2012-10-28 03:35:50 -05:00
|
|
|
|
|
|
|
|
AVPacket pkt;
|
|
|
|
|
av_init_packet(&pkt);
|
|
|
|
|
pkt.data = NULL;
|
|
|
|
|
pkt.size = 0;
|
|
|
|
|
pkt.pts = pkt.dts = write_audio_count;
|
|
|
|
|
|
|
|
|
|
/* encode the image */
|
|
|
|
|
int got_packet = 0;
|
2019-04-18 14:04:37 -05:00
|
|
|
#if IS_FFMPEG_3_2
|
2018-03-28 15:09:55 -05:00
|
|
|
avcodec_send_frame(audio_codec, NULL);
|
|
|
|
|
got_packet = 0;
|
2019-04-18 14:04:37 -05:00
|
|
|
#else
|
2012-11-16 17:15:44 -06:00
|
|
|
error_code = avcodec_encode_audio2(audio_codec, &pkt, NULL, &got_packet);
|
2019-04-18 14:04:37 -05:00
|
|
|
#endif
|
2012-10-28 03:35:50 -05:00
|
|
|
if (error_code < 0) {
|
2019-08-04 23:10:59 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::flush_encoders ERROR [" + (std::string)av_err2str(error_code) + "]", "error_code", error_code);
|
2012-10-28 03:35:50 -05:00
|
|
|
}
|
|
|
|
|
if (!got_packet) {
|
|
|
|
|
stop_encoding = 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Since the PTS can change during encoding, set the value again. This seems like a huge hack,
|
|
|
|
|
// but it fixes lots of PTS related issues when I do this.
|
|
|
|
|
pkt.pts = pkt.dts = write_audio_count;
|
|
|
|
|
|
|
|
|
|
// Scale the PTS to the audio stream timebase (which is sometimes different than the codec's timebase)
|
|
|
|
|
if (pkt.pts != AV_NOPTS_VALUE)
|
|
|
|
|
pkt.pts = av_rescale_q(pkt.pts, audio_codec->time_base, audio_st->time_base);
|
|
|
|
|
if (pkt.dts != AV_NOPTS_VALUE)
|
|
|
|
|
pkt.dts = av_rescale_q(pkt.dts, audio_codec->time_base, audio_st->time_base);
|
|
|
|
|
if (pkt.duration > 0)
|
|
|
|
|
pkt.duration = av_rescale_q(pkt.duration, audio_codec->time_base, audio_st->time_base);
|
|
|
|
|
|
|
|
|
|
// set stream
|
|
|
|
|
pkt.stream_index = audio_st->index;
|
|
|
|
|
pkt.flags |= AV_PKT_FLAG_KEY;
|
|
|
|
|
|
|
|
|
|
// Write packet
|
2012-12-03 04:51:17 -06:00
|
|
|
error_code = av_interleaved_write_frame(oc, &pkt);
|
2015-10-01 13:00:50 -05:00
|
|
|
if (error_code < 0) {
|
2019-08-04 23:10:59 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::flush_encoders ERROR [" + (std::string)av_err2str(error_code) + "]", "error_code", error_code);
|
2012-10-28 03:35:50 -05:00
|
|
|
}
|
2015-06-01 00:20:14 -07:00
|
|
|
|
|
|
|
|
// deallocate memory for packet
|
2017-08-20 17:37:39 -05:00
|
|
|
AV_FREE_PACKET(&pkt);
|
2012-10-28 03:35:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-19 15:03:55 -05:00
|
|
|
// Close the video codec
|
2019-05-08 14:53:23 -07:00
|
|
|
void FFmpegWriter::close_video(AVFormatContext *oc, AVStream *st)
|
|
|
|
|
{
|
2020-02-10 01:50:31 -05:00
|
|
|
#if HAVE_HW_ACCEL
|
|
|
|
|
if (hw_en_on && hw_en_supported) {
|
|
|
|
|
if (hw_device_ctx) {
|
|
|
|
|
av_buffer_unref(&hw_device_ctx);
|
|
|
|
|
hw_device_ctx = NULL;
|
2019-01-29 12:38:52 -08:00
|
|
|
}
|
2020-02-10 01:50:31 -05:00
|
|
|
}
|
|
|
|
|
#endif // HAVE_HW_ACCEL
|
2012-07-19 15:03:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close the audio codec
|
2019-05-08 14:53:23 -07:00
|
|
|
void FFmpegWriter::close_audio(AVFormatContext *oc, AVStream *st)
|
|
|
|
|
{
|
2015-08-02 20:32:33 -05:00
|
|
|
// Clear buffers
|
2012-07-25 17:20:02 -05:00
|
|
|
delete[] samples;
|
|
|
|
|
delete[] audio_outbuf;
|
2015-08-02 20:32:33 -05:00
|
|
|
delete[] audio_encoder_buffer;
|
|
|
|
|
samples = NULL;
|
|
|
|
|
audio_outbuf = NULL;
|
|
|
|
|
audio_encoder_buffer = NULL;
|
2012-08-05 15:17:37 -05:00
|
|
|
|
2015-02-05 00:06:07 -06:00
|
|
|
// Deallocate resample buffer
|
2015-03-01 22:36:39 -06:00
|
|
|
if (avr) {
|
2018-08-11 18:22:18 -05:00
|
|
|
SWR_CLOSE(avr);
|
|
|
|
|
SWR_FREE(&avr);
|
2015-03-01 22:36:39 -06:00
|
|
|
avr = NULL;
|
|
|
|
|
}
|
2015-06-01 00:20:14 -07:00
|
|
|
|
|
|
|
|
if (avr_planar) {
|
2018-08-11 18:22:18 -05:00
|
|
|
SWR_CLOSE(avr_planar);
|
|
|
|
|
SWR_FREE(&avr_planar);
|
2015-06-01 00:20:14 -07:00
|
|
|
avr_planar = NULL;
|
|
|
|
|
}
|
2012-07-12 15:55:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close the writer
|
2019-04-18 14:04:37 -05:00
|
|
|
void FFmpegWriter::Close() {
|
2015-02-05 00:06:07 -06:00
|
|
|
// Write trailer (if needed)
|
|
|
|
|
if (!write_trailer)
|
|
|
|
|
WriteTrailer();
|
|
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
// Close each codec
|
|
|
|
|
if (video_st)
|
|
|
|
|
close_video(oc, video_st);
|
|
|
|
|
if (audio_st)
|
|
|
|
|
close_audio(oc, audio_st);
|
2012-07-12 15:55:41 -05:00
|
|
|
|
2012-08-24 15:57:49 -05:00
|
|
|
// Deallocate image scalers
|
|
|
|
|
if (image_rescalers.size() > 0)
|
|
|
|
|
RemoveScalers();
|
|
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
if (!(fmt->flags & AVFMT_NOFILE)) {
|
|
|
|
|
/* close the output file */
|
|
|
|
|
avio_close(oc->pb);
|
|
|
|
|
}
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2012-10-26 17:15:17 -05:00
|
|
|
// Reset frame counters
|
2012-10-28 23:38:24 -05:00
|
|
|
write_video_count = 0;
|
|
|
|
|
write_audio_count = 0;
|
2012-10-26 02:11:39 -05:00
|
|
|
|
2019-05-08 14:53:23 -07:00
|
|
|
// Free the context which frees the streams too
|
|
|
|
|
avformat_free_context(oc);
|
|
|
|
|
oc = NULL;
|
2015-02-05 00:06:07 -06:00
|
|
|
|
|
|
|
|
// Close writer
|
|
|
|
|
is_open = false;
|
|
|
|
|
prepare_streams = false;
|
|
|
|
|
write_header = false;
|
|
|
|
|
write_trailer = false;
|
|
|
|
|
|
2019-07-03 14:14:02 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::Close");
|
2012-07-12 15:55:41 -05:00
|
|
|
}
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2012-08-20 02:59:35 -05:00
|
|
|
// Add an AVFrame to the cache
|
2019-04-18 14:04:37 -05:00
|
|
|
void FFmpegWriter::add_avframe(std::shared_ptr<Frame> frame, AVFrame *av_frame) {
|
2012-08-20 02:59:35 -05:00
|
|
|
// Add AVFrame to map (if it does not already exist)
|
2019-04-18 14:04:37 -05:00
|
|
|
if (!av_frames.count(frame)) {
|
2012-08-20 14:26:49 -05:00
|
|
|
// Add av_frame
|
2012-08-20 02:59:35 -05:00
|
|
|
av_frames[frame] = av_frame;
|
2019-04-18 14:04:37 -05:00
|
|
|
} else {
|
2012-08-20 02:59:35 -05:00
|
|
|
// Do not add, and deallocate this AVFrame
|
2015-09-23 00:27:28 -05:00
|
|
|
AV_FREE_FRAME(&av_frame);
|
2012-08-20 02:59:35 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-19 15:03:55 -05:00
|
|
|
// Add an audio output stream
|
2019-04-18 14:04:37 -05:00
|
|
|
AVStream *FFmpegWriter::add_audio_stream() {
|
2012-07-25 17:20:02 -05:00
|
|
|
AVCodecContext *c;
|
|
|
|
|
AVStream *st;
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
// Find the audio codec
|
2012-07-19 15:03:55 -05:00
|
|
|
AVCodec *codec = avcodec_find_encoder_by_name(info.acodec.c_str());
|
2012-07-25 17:20:02 -05:00
|
|
|
if (codec == NULL)
|
2012-07-19 15:03:55 -05:00
|
|
|
throw InvalidCodec("A valid audio codec could not be found for this file.", path);
|
|
|
|
|
|
2012-07-24 12:50:17 -05:00
|
|
|
// Create a new audio stream
|
2018-03-28 15:09:55 -05:00
|
|
|
AV_FORMAT_NEW_STREAM(oc, audio_codec, codec, st)
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
c->codec_id = codec->id;
|
2012-11-17 01:57:57 -06:00
|
|
|
#if LIBAVFORMAT_VERSION_MAJOR >= 53
|
2012-07-25 17:20:02 -05:00
|
|
|
c->codec_type = AVMEDIA_TYPE_AUDIO;
|
2012-11-17 01:57:57 -06:00
|
|
|
#else
|
|
|
|
|
c->codec_type = CODEC_TYPE_AUDIO;
|
|
|
|
|
#endif
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
// Set the sample parameters
|
|
|
|
|
c->bit_rate = info.audio_bit_rate;
|
|
|
|
|
c->channels = info.channels;
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2012-07-27 17:44:18 -05:00
|
|
|
// Set valid sample rate (or throw error)
|
|
|
|
|
if (codec->supported_samplerates) {
|
|
|
|
|
int i;
|
2019-01-29 12:38:52 -08:00
|
|
|
for (i = 0; codec->supported_samplerates[i] != 0; i++)
|
2019-04-18 14:04:37 -05:00
|
|
|
if (info.sample_rate == codec->supported_samplerates[i]) {
|
2019-01-29 12:38:52 -08:00
|
|
|
// Set the valid sample rate
|
|
|
|
|
c->sample_rate = info.sample_rate;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-04-18 14:04:37 -05:00
|
|
|
if (codec->supported_samplerates[i] == 0)
|
|
|
|
|
throw InvalidSampleRate("An invalid sample rate was detected for this codec.", path);
|
2012-07-27 17:44:18 -05:00
|
|
|
} else
|
|
|
|
|
// Set sample rate
|
|
|
|
|
c->sample_rate = info.sample_rate;
|
|
|
|
|
|
2012-07-30 02:37:19 -05:00
|
|
|
|
2012-07-27 17:44:18 -05:00
|
|
|
// Set a valid number of channels (or throw error)
|
2019-12-15 14:22:59 -05:00
|
|
|
const uint64_t channel_layout = info.channel_layout;
|
2012-08-02 17:19:55 -05:00
|
|
|
if (codec->channel_layouts) {
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; codec->channel_layouts[i] != 0; i++)
|
2019-04-18 14:04:37 -05:00
|
|
|
if (channel_layout == codec->channel_layouts[i]) {
|
2012-08-02 17:19:55 -05:00
|
|
|
// Set valid channel layout
|
|
|
|
|
c->channel_layout = channel_layout;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (codec->channel_layouts[i] == 0)
|
|
|
|
|
throw InvalidChannels("An invalid channel layout was detected (i.e. MONO / STEREO).", path);
|
|
|
|
|
} else
|
|
|
|
|
// Set valid channel layout
|
|
|
|
|
c->channel_layout = channel_layout;
|
2012-07-30 02:37:19 -05:00
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
// Choose a valid sample_fmt
|
|
|
|
|
if (codec->sample_fmts) {
|
2019-04-18 14:04:37 -05:00
|
|
|
for (int i = 0; codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
|
2012-07-25 17:20:02 -05:00
|
|
|
// Set sample format to 1st valid format (and then exit loop)
|
|
|
|
|
c->sample_fmt = codec->sample_fmts[i];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (c->sample_fmt == AV_SAMPLE_FMT_NONE) {
|
|
|
|
|
// Default if no sample formats found
|
|
|
|
|
c->sample_fmt = AV_SAMPLE_FMT_S16;
|
|
|
|
|
}
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
// some formats want stream headers to be separate
|
|
|
|
|
if (oc->oformat->flags & AVFMT_GLOBALHEADER)
|
2018-08-11 18:22:18 -05:00
|
|
|
#if (LIBAVCODEC_VERSION_MAJOR >= 57)
|
|
|
|
|
c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
|
|
|
|
|
#else
|
2012-07-25 17:20:02 -05:00
|
|
|
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
|
2018-08-11 18:22:18 -05:00
|
|
|
#endif
|
2012-07-25 17:20:02 -05:00
|
|
|
|
2018-03-28 15:09:55 -05:00
|
|
|
AV_COPY_PARAMS_FROM_CONTEXT(st, c);
|
2016-04-21 01:39:17 -05:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::add_audio_stream", "c->codec_id", c->codec_id, "c->bit_rate", c->bit_rate, "c->channels", c->channels, "c->sample_fmt", c->sample_fmt, "c->channel_layout", c->channel_layout, "c->sample_rate", c->sample_rate);
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
return st;
|
2012-07-19 15:03:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add a video output stream
|
2019-04-18 14:04:37 -05:00
|
|
|
AVStream *FFmpegWriter::add_video_stream() {
|
2012-07-25 17:20:02 -05:00
|
|
|
AVCodecContext *c;
|
|
|
|
|
AVStream *st;
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2018-03-28 15:09:55 -05:00
|
|
|
// Find the video codec
|
2012-07-19 15:03:55 -05:00
|
|
|
AVCodec *codec = avcodec_find_encoder_by_name(info.vcodec.c_str());
|
2012-07-27 02:35:43 -05:00
|
|
|
if (codec == NULL)
|
2012-07-19 15:03:55 -05:00
|
|
|
throw InvalidCodec("A valid video codec could not be found for this file.", path);
|
|
|
|
|
|
2018-03-28 15:09:55 -05:00
|
|
|
// Create a new video stream
|
|
|
|
|
AV_FORMAT_NEW_STREAM(oc, video_codec, codec, st)
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
c->codec_id = codec->id;
|
2012-11-17 01:57:57 -06:00
|
|
|
#if LIBAVFORMAT_VERSION_MAJOR >= 53
|
2012-07-25 17:20:02 -05:00
|
|
|
c->codec_type = AVMEDIA_TYPE_VIDEO;
|
2012-11-17 01:57:57 -06:00
|
|
|
#else
|
|
|
|
|
c->codec_type = CODEC_TYPE_VIDEO;
|
|
|
|
|
#endif
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2015-08-24 23:49:45 -05:00
|
|
|
/* Init video encoder options */
|
2020-06-04 08:28:33 -07:00
|
|
|
if (info.video_bit_rate >= 1000
|
|
|
|
|
#if (LIBAVCODEC_VERSION_MAJOR >= 58)
|
|
|
|
|
&& c->codec_id != AV_CODEC_ID_AV1
|
|
|
|
|
#endif
|
|
|
|
|
) {
|
2018-09-16 18:14:31 -07:00
|
|
|
c->bit_rate = info.video_bit_rate;
|
2019-03-08 14:41:40 -08:00
|
|
|
if (info.video_bit_rate >= 1500000) {
|
|
|
|
|
c->qmin = 2;
|
|
|
|
|
c->qmax = 30;
|
|
|
|
|
}
|
2019-03-08 16:52:34 -08:00
|
|
|
// Here should be the setting for low fixed bitrate
|
|
|
|
|
// Defaults are used because mpeg2 otherwise had problems
|
2019-04-18 14:04:37 -05:00
|
|
|
} else {
|
2020-06-04 08:28:33 -07:00
|
|
|
// Check if codec supports crf or qp
|
2019-03-10 13:09:47 -07:00
|
|
|
switch (c->codec_id) {
|
2019-04-18 14:04:37 -05:00
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 39, 101)
|
|
|
|
|
#if (LIBAVCODEC_VERSION_MAJOR >= 58)
|
2019-03-10 13:09:47 -07:00
|
|
|
case AV_CODEC_ID_AV1 :
|
2020-06-15 14:04:49 -07:00
|
|
|
// TODO: Set `crf` or `qp` according to bitrate, as bitrate is not supported by these encoders yet.
|
2020-06-04 08:28:33 -07:00
|
|
|
if (info.video_bit_rate >= 1000) {
|
|
|
|
|
c->bit_rate = 0;
|
|
|
|
|
if (strstr(info.vcodec.c_str(), "aom") != NULL) {
|
|
|
|
|
int calculated_quality = 35;
|
|
|
|
|
if (info.video_bit_rate < 500000) calculated_quality = 50;
|
|
|
|
|
if (info.video_bit_rate > 5000000) calculated_quality = 10;
|
|
|
|
|
av_opt_set_int(c->priv_data, "crf", calculated_quality, 0);
|
|
|
|
|
info.video_bit_rate = calculated_quality;
|
|
|
|
|
} else {
|
|
|
|
|
int calculated_quality = 50;
|
|
|
|
|
if (info.video_bit_rate < 500000) calculated_quality = 60;
|
|
|
|
|
if (info.video_bit_rate > 5000000) calculated_quality = 15;
|
|
|
|
|
av_opt_set_int(c->priv_data, "qp", calculated_quality, 0);
|
|
|
|
|
info.video_bit_rate = calculated_quality;
|
|
|
|
|
} // medium
|
|
|
|
|
}
|
|
|
|
|
if (strstr(info.vcodec.c_str(), "svt_av1") != NULL) {
|
|
|
|
|
av_opt_set_int(c->priv_data, "preset", 6, 0);
|
|
|
|
|
av_opt_set_int(c->priv_data, "forced-idr",1,0);
|
|
|
|
|
}
|
|
|
|
|
else if (strstr(info.vcodec.c_str(), "rav1e") != NULL) {
|
|
|
|
|
av_opt_set_int(c->priv_data, "speed", 7, 0);
|
|
|
|
|
av_opt_set_int(c->priv_data, "tile-rows", 2, 0);
|
|
|
|
|
av_opt_set_int(c->priv_data, "tile-columns", 4, 0);
|
|
|
|
|
}
|
|
|
|
|
else if (strstr(info.vcodec.c_str(), "aom") != NULL) {
|
|
|
|
|
// Set number of tiles to a fixed value
|
2020-06-15 14:02:26 -07:00
|
|
|
// TODO: Allow user to chose their own number of tiles
|
2020-06-04 08:28:33 -07:00
|
|
|
av_opt_set_int(c->priv_data, "tile-rows", 1, 0); // log2 of number of rows
|
|
|
|
|
av_opt_set_int(c->priv_data, "tile-columns", 2, 0); // log2 of number of columns
|
|
|
|
|
av_opt_set_int(c->priv_data, "row-mt", 1, 0); // use multiple cores
|
|
|
|
|
av_opt_set_int(c->priv_data, "cpu-used", 3, 0); // default is 1, usable is 4
|
|
|
|
|
}
|
|
|
|
|
//break;
|
2019-04-18 14:04:37 -05:00
|
|
|
#endif
|
2019-03-10 13:09:47 -07:00
|
|
|
case AV_CODEC_ID_VP9 :
|
2019-12-11 05:42:41 -05:00
|
|
|
case AV_CODEC_ID_HEVC :
|
2019-04-18 14:04:37 -05:00
|
|
|
#endif
|
2019-03-10 13:28:56 -07:00
|
|
|
case AV_CODEC_ID_VP8 :
|
|
|
|
|
case AV_CODEC_ID_H264 :
|
2019-03-10 13:09:47 -07:00
|
|
|
if (info.video_bit_rate < 40) {
|
|
|
|
|
c->qmin = 0;
|
|
|
|
|
c->qmax = 63;
|
2019-04-18 14:04:37 -05:00
|
|
|
} else {
|
2019-03-10 13:09:47 -07:00
|
|
|
c->qmin = info.video_bit_rate - 5;
|
|
|
|
|
c->qmax = 63;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// Here should be the setting for codecs that don't support crf
|
|
|
|
|
// For now defaults are used
|
|
|
|
|
break;
|
2019-03-10 10:42:48 -07:00
|
|
|
}
|
2018-09-16 18:14:31 -07:00
|
|
|
}
|
2019-01-27 10:07:40 -08:00
|
|
|
|
|
|
|
|
//TODO: Implement variable bitrate feature (which actually works). This implementation throws
|
2017-09-28 16:03:01 -05:00
|
|
|
//invalid bitrate errors and rc buffer underflow errors, etc...
|
|
|
|
|
//c->rc_min_rate = info.video_bit_rate;
|
|
|
|
|
//c->rc_max_rate = info.video_bit_rate;
|
|
|
|
|
//c->rc_buffer_size = FFMAX(c->rc_max_rate, 15000000) * 112L / 15000000 * 16384;
|
|
|
|
|
//if ( !c->rc_initial_buffer_occupancy )
|
|
|
|
|
// c->rc_initial_buffer_occupancy = c->rc_buffer_size * 3/4;
|
2015-08-24 23:49:45 -05:00
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
/* resolution must be a multiple of two */
|
|
|
|
|
// TODO: require /2 height and width
|
|
|
|
|
c->width = info.width;
|
|
|
|
|
c->height = info.height;
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
/* time base: this is the fundamental unit of time (in seconds) in terms
|
|
|
|
|
of which frame timestamps are represented. for fixed-fps content,
|
|
|
|
|
timebase should be 1/framerate and timestamp increments should be
|
|
|
|
|
identically 1. */
|
|
|
|
|
c->time_base.num = info.video_timebase.num;
|
2012-08-11 20:28:05 -05:00
|
|
|
c->time_base.den = info.video_timebase.den;
|
2019-12-11 05:42:41 -05:00
|
|
|
// AVCodecContext->framerate was added in FFmpeg 2.2
|
|
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(56, 26, 0)
|
2018-03-28 15:09:55 -05:00
|
|
|
c->framerate = av_inv_q(c->time_base);
|
2019-04-18 14:04:37 -05:00
|
|
|
#endif
|
2018-03-28 15:09:55 -05:00
|
|
|
st->avg_frame_rate = av_inv_q(c->time_base);
|
2017-11-08 00:01:42 -06:00
|
|
|
st->time_base.num = info.video_timebase.num;
|
|
|
|
|
st->time_base.den = info.video_timebase.den;
|
2020-06-01 13:24:07 -05:00
|
|
|
#if (LIBAVFORMAT_VERSION_MAJOR >= 58)
|
2020-06-02 13:30:05 -05:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
2020-06-01 13:24:07 -05:00
|
|
|
st->codec->time_base.num = info.video_timebase.num;
|
|
|
|
|
st->codec->time_base.den = info.video_timebase.den;
|
2020-06-02 13:30:05 -05:00
|
|
|
#pragma GCC diagnostic pop
|
2020-06-01 13:24:07 -05:00
|
|
|
#endif
|
2017-11-08 00:01:42 -06:00
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
c->gop_size = 12; /* TODO: add this to "info"... emit one intra frame every twelve frames at most */
|
2015-02-05 00:06:07 -06:00
|
|
|
c->max_b_frames = 10;
|
2015-02-05 16:00:18 -06:00
|
|
|
if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO)
|
2012-07-25 17:20:02 -05:00
|
|
|
/* just for testing, we also add B frames */
|
|
|
|
|
c->max_b_frames = 2;
|
2015-02-05 16:00:18 -06:00
|
|
|
if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO)
|
2012-07-25 17:20:02 -05:00
|
|
|
/* Needed to avoid using macroblocks in which some coeffs overflow.
|
|
|
|
|
This does not happen with normal video, it just happens here as
|
|
|
|
|
the motion of the chroma plane does not match the luma plane. */
|
|
|
|
|
c->mb_decision = 2;
|
|
|
|
|
// some formats want stream headers to be separate
|
|
|
|
|
if (oc->oformat->flags & AVFMT_GLOBALHEADER)
|
2018-08-11 18:22:18 -05:00
|
|
|
#if (LIBAVCODEC_VERSION_MAJOR >= 57)
|
|
|
|
|
c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
|
|
|
|
|
#else
|
2012-07-25 17:20:02 -05:00
|
|
|
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
|
2018-08-11 18:22:18 -05:00
|
|
|
#endif
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2014-03-29 15:39:43 -05:00
|
|
|
// Find all supported pixel formats for this codec
|
2019-04-18 14:04:37 -05:00
|
|
|
const PixelFormat *supported_pixel_formats = codec->pix_fmts;
|
2019-01-29 12:38:52 -08:00
|
|
|
while (supported_pixel_formats != NULL && *supported_pixel_formats != PIX_FMT_NONE) {
|
|
|
|
|
// Assign the 1st valid pixel format (if one is missing)
|
|
|
|
|
if (c->pix_fmt == PIX_FMT_NONE)
|
|
|
|
|
c->pix_fmt = *supported_pixel_formats;
|
|
|
|
|
++supported_pixel_formats;
|
|
|
|
|
}
|
2014-03-29 15:39:43 -05:00
|
|
|
|
2019-01-29 12:38:52 -08:00
|
|
|
// Codec doesn't have any pix formats?
|
|
|
|
|
if (c->pix_fmt == PIX_FMT_NONE) {
|
2019-04-18 14:04:37 -05:00
|
|
|
if (fmt->video_codec == AV_CODEC_ID_RAWVIDEO) {
|
2019-01-29 12:38:52 -08:00
|
|
|
// Raw video should use RGB24
|
|
|
|
|
c->pix_fmt = PIX_FMT_RGB24;
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2018-08-11 18:22:18 -05:00
|
|
|
#if (LIBAVFORMAT_VERSION_MAJOR < 58)
|
2019-01-29 12:38:52 -08:00
|
|
|
if (strcmp(fmt->name, "gif") != 0)
|
|
|
|
|
// If not GIF format, skip the encoding process
|
|
|
|
|
// Set raw picture flag (so we don't encode this video)
|
|
|
|
|
oc->oformat->flags |= AVFMT_RAWPICTURE;
|
2018-08-11 18:22:18 -05:00
|
|
|
#endif
|
2019-01-29 12:38:52 -08:00
|
|
|
} else {
|
|
|
|
|
// Set the default codec
|
|
|
|
|
c->pix_fmt = PIX_FMT_YUV420P;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-29 15:39:43 -05:00
|
|
|
|
2018-03-28 15:09:55 -05:00
|
|
|
AV_COPY_PARAMS_FROM_CONTEXT(st, c);
|
2018-08-11 18:22:18 -05:00
|
|
|
#if (LIBAVFORMAT_VERSION_MAJOR < 58)
|
2019-08-04 23:10:59 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::add_video_stream (" + (std::string)fmt->name + " : " + (std::string)av_get_pix_fmt_name(c->pix_fmt) + ")", "c->codec_id", c->codec_id, "c->bit_rate", c->bit_rate, "c->pix_fmt", c->pix_fmt, "oc->oformat->flags", oc->oformat->flags, "AVFMT_RAWPICTURE", AVFMT_RAWPICTURE);
|
2018-08-11 18:22:18 -05:00
|
|
|
#else
|
2019-08-04 23:10:59 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::add_video_stream (" + (std::string)fmt->name + " : " + (std::string)av_get_pix_fmt_name(c->pix_fmt) + ")", "c->codec_id", c->codec_id, "c->bit_rate", c->bit_rate, "c->pix_fmt", c->pix_fmt, "oc->oformat->flags", oc->oformat->flags);
|
2018-08-11 18:22:18 -05:00
|
|
|
#endif
|
2014-03-29 15:39:43 -05:00
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
return st;
|
2012-07-19 15:03:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// open audio codec
|
2019-04-18 14:04:37 -05:00
|
|
|
void FFmpegWriter::open_audio(AVFormatContext *oc, AVStream *st) {
|
2012-07-25 17:20:02 -05:00
|
|
|
AVCodec *codec;
|
2018-03-28 15:09:55 -05:00
|
|
|
AV_GET_CODEC_FROM_STREAM(st, audio_codec)
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2017-08-21 16:36:22 -05:00
|
|
|
// Set number of threads equal to number of processors (not to exceed 16)
|
2019-08-04 23:51:02 -04:00
|
|
|
audio_codec->thread_count = std::min(FF_NUM_PROCESSORS, 16);
|
2012-08-28 22:53:12 -05:00
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
// Find the audio encoder
|
2017-11-10 01:02:01 -06:00
|
|
|
codec = avcodec_find_encoder_by_name(info.acodec.c_str());
|
|
|
|
|
if (!codec)
|
|
|
|
|
codec = avcodec_find_encoder(audio_codec->codec_id);
|
2012-07-25 17:20:02 -05:00
|
|
|
if (!codec)
|
|
|
|
|
throw InvalidCodec("Could not find codec", path);
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2018-06-29 15:06:34 -05:00
|
|
|
// Init options
|
|
|
|
|
AVDictionary *opts = NULL;
|
|
|
|
|
av_dict_set(&opts, "strict", "experimental", 0);
|
|
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
// Open the codec
|
2018-06-29 15:06:34 -05:00
|
|
|
if (avcodec_open2(audio_codec, codec, &opts) < 0)
|
2019-08-23 19:46:05 +02:00
|
|
|
throw InvalidCodec("Could not open audio codec", path);
|
2018-03-28 15:09:55 -05:00
|
|
|
AV_COPY_PARAMS_FROM_CONTEXT(st, audio_codec);
|
2012-07-24 12:50:17 -05:00
|
|
|
|
2018-06-29 15:06:34 -05:00
|
|
|
// Free options
|
|
|
|
|
av_dict_free(&opts);
|
|
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
// Calculate the size of the input frame (i..e how many samples per packet), and the output buffer
|
|
|
|
|
// TODO: Ugly hack for PCM codecs (will be removed ASAP with new PCM support to compute the input frame size in samples
|
2012-08-24 15:57:49 -05:00
|
|
|
if (audio_codec->frame_size <= 1) {
|
2012-07-25 17:20:02 -05:00
|
|
|
// No frame size found... so calculate
|
2012-08-01 03:17:02 -05:00
|
|
|
audio_input_frame_size = 50000 / info.channels;
|
2012-07-24 12:50:17 -05:00
|
|
|
|
2018-03-28 15:09:55 -05:00
|
|
|
int s = AV_FIND_DECODER_CODEC_ID(st);
|
|
|
|
|
switch (s) {
|
2019-04-18 14:04:37 -05:00
|
|
|
case AV_CODEC_ID_PCM_S16LE:
|
|
|
|
|
case AV_CODEC_ID_PCM_S16BE:
|
|
|
|
|
case AV_CODEC_ID_PCM_U16LE:
|
|
|
|
|
case AV_CODEC_ID_PCM_U16BE:
|
|
|
|
|
audio_input_frame_size >>= 1;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2012-07-25 17:20:02 -05:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Set frame size based on the codec
|
2015-08-02 20:32:33 -05:00
|
|
|
audio_input_frame_size = audio_codec->frame_size;
|
2012-07-25 17:20:02 -05:00
|
|
|
}
|
2012-07-24 12:50:17 -05:00
|
|
|
|
2012-08-02 17:19:55 -05:00
|
|
|
// Set the initial frame size (since it might change during resampling)
|
|
|
|
|
initial_audio_input_frame_size = audio_input_frame_size;
|
|
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
// Allocate array for samples
|
2015-08-02 20:32:33 -05:00
|
|
|
samples = new int16_t[AVCODEC_MAX_AUDIO_FRAME_SIZE];
|
2012-07-24 12:50:17 -05:00
|
|
|
|
2012-08-04 01:11:12 -05:00
|
|
|
// Set audio output buffer (used to store the encoded audio)
|
2015-08-02 20:32:33 -05:00
|
|
|
audio_outbuf_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
|
2012-08-04 01:11:12 -05:00
|
|
|
audio_outbuf = new uint8_t[audio_outbuf_size];
|
2012-07-24 12:50:17 -05:00
|
|
|
|
2015-08-02 20:32:33 -05:00
|
|
|
// Set audio packet encoding buffer
|
|
|
|
|
audio_encoder_buffer_size = AUDIO_PACKET_ENCODING_SIZE;
|
|
|
|
|
audio_encoder_buffer = new uint8_t[audio_encoder_buffer_size];
|
|
|
|
|
|
2018-03-04 03:10:59 -06:00
|
|
|
// Add audio metadata (if any)
|
2019-08-04 23:10:59 -04:00
|
|
|
for (std::map<std::string, std::string>::iterator iter = info.metadata.begin(); iter != info.metadata.end(); ++iter) {
|
2018-03-04 03:10:59 -06:00
|
|
|
av_dict_set(&st->metadata, iter->first.c_str(), iter->second.c_str(), 0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 14:14:02 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::open_audio", "audio_codec->thread_count", audio_codec->thread_count, "audio_input_frame_size", audio_input_frame_size, "buffer_size", AVCODEC_MAX_AUDIO_FRAME_SIZE + MY_INPUT_BUFFER_PADDING_SIZE);
|
2012-07-19 15:03:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// open video codec
|
2019-04-18 14:04:37 -05:00
|
|
|
void FFmpegWriter::open_video(AVFormatContext *oc, AVStream *st) {
|
2012-07-25 17:20:02 -05:00
|
|
|
AVCodec *codec;
|
2018-03-28 15:09:55 -05:00
|
|
|
AV_GET_CODEC_FROM_STREAM(st, video_codec)
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2017-08-21 16:36:22 -05:00
|
|
|
// Set number of threads equal to number of processors (not to exceed 16)
|
2019-08-04 23:51:02 -04:00
|
|
|
video_codec->thread_count = std::min(FF_NUM_PROCESSORS, 16);
|
2012-08-28 22:53:12 -05:00
|
|
|
|
2020-02-10 01:50:31 -05:00
|
|
|
#if HAVE_HW_ACCEL
|
2019-01-29 12:38:52 -08:00
|
|
|
if (hw_en_on && hw_en_supported) {
|
2019-01-30 20:44:36 -08:00
|
|
|
//char *dev_hw = NULL;
|
2019-01-29 12:38:52 -08:00
|
|
|
char adapter[256];
|
|
|
|
|
char *adapter_ptr = NULL;
|
|
|
|
|
int adapter_num;
|
2018-09-18 14:45:56 -05:00
|
|
|
// Use the hw device given in the environment variable HW_EN_DEVICE_SET or the default if not set
|
2019-01-30 20:44:36 -08:00
|
|
|
adapter_num = openshot::Settings::Instance()->HW_EN_DEVICE_SET;
|
|
|
|
|
fprintf(stderr, "\n\nEncodiing Device Nr: %d\n", adapter_num);
|
|
|
|
|
if (adapter_num < 3 && adapter_num >=0) {
|
2019-04-18 14:04:37 -05:00
|
|
|
#if defined(__linux__)
|
2019-01-29 12:38:52 -08:00
|
|
|
snprintf(adapter,sizeof(adapter),"/dev/dri/renderD%d", adapter_num+128);
|
|
|
|
|
// Maybe 127 is better because the first card would be 1?!
|
|
|
|
|
adapter_ptr = adapter;
|
2019-04-18 14:04:37 -05:00
|
|
|
#elif defined(_WIN32)
|
2019-01-29 12:38:52 -08:00
|
|
|
adapter_ptr = NULL;
|
2019-04-18 14:04:37 -05:00
|
|
|
#elif defined(__APPLE__)
|
2019-01-29 12:38:52 -08:00
|
|
|
adapter_ptr = NULL;
|
2019-04-18 14:04:37 -05:00
|
|
|
#endif
|
2019-01-29 12:38:52 -08:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
adapter_ptr = NULL; // Just to be sure
|
|
|
|
|
}
|
2018-09-19 21:37:12 -07:00
|
|
|
// Check if it is there and writable
|
2019-04-18 14:04:37 -05:00
|
|
|
#if defined(__linux__)
|
2019-02-01 03:38:44 -08:00
|
|
|
if( adapter_ptr != NULL && access( adapter_ptr, W_OK ) == 0 ) {
|
2019-04-18 14:04:37 -05:00
|
|
|
#elif defined(_WIN32)
|
2019-01-29 12:38:52 -08:00
|
|
|
if( adapter_ptr != NULL ) {
|
2019-04-18 14:04:37 -05:00
|
|
|
#elif defined(__APPLE__)
|
2019-01-29 12:38:52 -08:00
|
|
|
if( adapter_ptr != NULL ) {
|
2019-04-18 14:04:37 -05:00
|
|
|
#endif
|
2019-08-23 19:46:05 +02:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("Encode Device present using device", "adapter", adapter_num);
|
2019-01-30 20:44:36 -08:00
|
|
|
}
|
|
|
|
|
else {
|
2019-01-29 12:38:52 -08:00
|
|
|
adapter_ptr = NULL; // use default
|
2019-07-03 14:14:02 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("Encode Device not present using default");
|
2019-01-29 12:38:52 -08:00
|
|
|
}
|
|
|
|
|
if (av_hwdevice_ctx_create(&hw_device_ctx, hw_en_av_device_type,
|
|
|
|
|
adapter_ptr, NULL, 0) < 0) {
|
2019-07-03 14:14:02 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::open_video : Codec name: ", info.vcodec.c_str(), -1, " ERROR creating\n", -1);
|
2019-01-29 12:38:52 -08:00
|
|
|
throw InvalidCodec("Could not create hwdevice", path);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-10 01:50:31 -05:00
|
|
|
#endif // HAVE_HW_ACCEL
|
2019-04-18 14:04:37 -05:00
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
/* find the video encoder */
|
2017-11-10 01:02:01 -06:00
|
|
|
codec = avcodec_find_encoder_by_name(info.vcodec.c_str());
|
|
|
|
|
if (!codec)
|
2018-03-28 15:09:55 -05:00
|
|
|
codec = avcodec_find_encoder(AV_FIND_DECODER_CODEC_ID(st));
|
2012-07-25 17:20:02 -05:00
|
|
|
if (!codec)
|
|
|
|
|
throw InvalidCodec("Could not find codec", path);
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
/* Force max_b_frames to 0 in some cases (i.e. for mjpeg image sequences */
|
|
|
|
|
if (video_codec->max_b_frames && video_codec->codec_id != AV_CODEC_ID_MPEG4 && video_codec->codec_id != AV_CODEC_ID_MPEG1VIDEO && video_codec->codec_id != AV_CODEC_ID_MPEG2VIDEO)
|
|
|
|
|
video_codec->max_b_frames = 0;
|
2017-08-01 01:19:07 -05:00
|
|
|
|
2018-06-29 15:06:34 -05:00
|
|
|
// Init options
|
|
|
|
|
AVDictionary *opts = NULL;
|
|
|
|
|
av_dict_set(&opts, "strict", "experimental", 0);
|
|
|
|
|
|
2020-02-10 01:50:31 -05:00
|
|
|
#if HAVE_HW_ACCEL
|
2019-01-29 12:38:52 -08:00
|
|
|
if (hw_en_on && hw_en_supported) {
|
|
|
|
|
video_codec->pix_fmt = hw_en_av_pix_fmt;
|
2019-08-23 19:46:05 +02:00
|
|
|
|
|
|
|
|
// for the list of possible options, see the list of codec-specific options:
|
|
|
|
|
// e.g. ffmpeg -h encoder=h264_vaapi or ffmpeg -h encoder=hevc_vaapi
|
|
|
|
|
// and "man ffmpeg-codecs"
|
|
|
|
|
|
|
|
|
|
// For VAAPI, it is safer to explicitly set rc_mode instead of relying on auto-selection
|
|
|
|
|
// which is ffmpeg version-specific.
|
|
|
|
|
if (hw_en_av_pix_fmt == AV_PIX_FMT_VAAPI) {
|
|
|
|
|
int64_t qp;
|
|
|
|
|
if (av_opt_get_int(video_codec->priv_data, "qp", 0, &qp) != 0 || qp == 0) {
|
|
|
|
|
// unless "qp" was set for CQP, switch to VBR RC mode
|
|
|
|
|
av_opt_set(video_codec->priv_data, "rc_mode", "VBR", 0);
|
|
|
|
|
|
2019-12-27 09:54:57 -08:00
|
|
|
// In the current state (ffmpeg-4.2-4 libva-mesa-driver-19.1.5-1) to use VBR,
|
2019-08-23 19:46:05 +02:00
|
|
|
// one has to specify both bit_rate and maxrate, otherwise a small low quality file is generated on Intel iGPU).
|
|
|
|
|
video_codec->rc_max_rate = video_codec->bit_rate;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (video_codec->codec_id) {
|
|
|
|
|
case AV_CODEC_ID_H264:
|
|
|
|
|
video_codec->max_b_frames = 0; // At least this GPU doesn't support b-frames
|
|
|
|
|
video_codec->profile = FF_PROFILE_H264_BASELINE | FF_PROFILE_H264_CONSTRAINED;
|
|
|
|
|
av_opt_set(video_codec->priv_data, "preset", "slow", 0);
|
|
|
|
|
av_opt_set(video_codec->priv_data, "tune", "zerolatency", 0);
|
|
|
|
|
av_opt_set(video_codec->priv_data, "vprofile", "baseline", AV_OPT_SEARCH_CHILDREN);
|
|
|
|
|
break;
|
|
|
|
|
case AV_CODEC_ID_HEVC:
|
|
|
|
|
// tested to work with defaults
|
|
|
|
|
break;
|
|
|
|
|
case AV_CODEC_ID_VP9:
|
|
|
|
|
// tested to work with defaults
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("No codec-specific options defined for this codec. HW encoding may fail",
|
|
|
|
|
"codec_id", video_codec->codec_id);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-29 12:38:52 -08:00
|
|
|
// set hw_frames_ctx for encoder's AVCodecContext
|
|
|
|
|
int err;
|
|
|
|
|
if ((err = set_hwframe_ctx(video_codec, hw_device_ctx, info.width, info.height)) < 0) {
|
2019-08-23 19:46:05 +02:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::open_video (set_hwframe_ctx) ERROR faled to set hwframe context",
|
|
|
|
|
"width", info.width, "height", info.height, av_err2str(err), -1);
|
2019-01-29 12:38:52 -08:00
|
|
|
}
|
|
|
|
|
}
|
2020-02-10 01:50:31 -05:00
|
|
|
#endif // HAVE_HW_ACCEL
|
2018-08-31 21:36:23 -07:00
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
/* open the codec */
|
2018-06-29 15:06:34 -05:00
|
|
|
if (avcodec_open2(video_codec, codec, &opts) < 0)
|
2019-08-23 19:46:05 +02:00
|
|
|
throw InvalidCodec("Could not open video codec", path);
|
2018-03-28 15:09:55 -05:00
|
|
|
AV_COPY_PARAMS_FROM_CONTEXT(st, video_codec);
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2018-06-29 15:06:34 -05:00
|
|
|
// Free options
|
|
|
|
|
av_dict_free(&opts);
|
|
|
|
|
|
2018-03-04 03:10:59 -06:00
|
|
|
// Add video metadata (if any)
|
2019-08-04 23:10:59 -04:00
|
|
|
for (std::map<std::string, std::string>::iterator iter = info.metadata.begin(); iter != info.metadata.end(); ++iter) {
|
2018-03-04 03:10:59 -06:00
|
|
|
av_dict_set(&st->metadata, iter->first.c_str(), iter->second.c_str(), 0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-04 16:06:54 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::open_video", "video_codec->thread_count", video_codec->thread_count);
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2012-07-19 15:03:55 -05:00
|
|
|
}
|
|
|
|
|
|
2012-08-20 02:59:35 -05:00
|
|
|
// write all queued frames' audio to the video file
|
2019-04-18 14:04:37 -05:00
|
|
|
void FFmpegWriter::write_audio_packets(bool is_final) {
|
|
|
|
|
#pragma omp task firstprivate(is_final)
|
2012-08-20 02:59:35 -05:00
|
|
|
{
|
|
|
|
|
// Init audio buffers / variables
|
|
|
|
|
int total_frame_samples = 0;
|
|
|
|
|
int frame_position = 0;
|
|
|
|
|
int channels_in_frame = 0;
|
|
|
|
|
int sample_rate_in_frame = 0;
|
|
|
|
|
int samples_in_frame = 0;
|
2015-02-05 00:06:07 -06:00
|
|
|
ChannelLayout channel_layout_in_frame = LAYOUT_MONO; // default channel layout
|
2012-07-24 12:50:17 -05:00
|
|
|
|
2012-08-20 02:59:35 -05:00
|
|
|
// Create a new array (to hold all S16 audio samples, for the current queued frames
|
2019-12-18 18:27:04 -08:00
|
|
|
unsigned int all_queued_samples_size = sizeof(int16_t) * (queued_audio_frames.size() * AVCODEC_MAX_AUDIO_FRAME_SIZE);
|
|
|
|
|
int16_t *all_queued_samples = (int16_t *) av_malloc(all_queued_samples_size);
|
2019-04-18 14:04:37 -05:00
|
|
|
int16_t *all_resampled_samples = NULL;
|
|
|
|
|
int16_t *final_samples_planar = NULL;
|
|
|
|
|
int16_t *final_samples = NULL;
|
2012-07-24 12:50:17 -05:00
|
|
|
|
2012-08-20 02:59:35 -05:00
|
|
|
// Loop through each queued audio frame
|
2019-04-18 14:04:37 -05:00
|
|
|
while (!queued_audio_frames.empty()) {
|
2012-08-20 02:59:35 -05:00
|
|
|
// Get front frame (from the queue)
|
2017-08-20 17:37:39 -05:00
|
|
|
std::shared_ptr<Frame> frame = queued_audio_frames.front();
|
2012-08-20 02:59:35 -05:00
|
|
|
|
|
|
|
|
// Get the audio details from this frame
|
2015-02-05 00:06:07 -06:00
|
|
|
sample_rate_in_frame = frame->SampleRate();
|
|
|
|
|
samples_in_frame = frame->GetAudioSamplesCount();
|
2012-08-20 02:59:35 -05:00
|
|
|
channels_in_frame = frame->GetAudioChannelsCount();
|
2015-02-05 00:06:07 -06:00
|
|
|
channel_layout_in_frame = frame->ChannelsLayout();
|
|
|
|
|
|
2012-08-20 02:59:35 -05:00
|
|
|
|
|
|
|
|
// Get audio sample array
|
2019-04-18 14:04:37 -05:00
|
|
|
float *frame_samples_float = NULL;
|
2015-02-05 00:06:07 -06:00
|
|
|
// Get samples interleaved together (c1 c2 c1 c2 c1 c2)
|
2015-02-26 17:33:09 -06:00
|
|
|
frame_samples_float = frame->GetInterleavedAudioSamples(sample_rate_in_frame, NULL, &samples_in_frame);
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2012-08-20 02:59:35 -05:00
|
|
|
|
|
|
|
|
// Calculate total samples
|
|
|
|
|
total_frame_samples = samples_in_frame * channels_in_frame;
|
|
|
|
|
|
2020-03-16 14:41:49 +02:00
|
|
|
// Translate audio sample values back to 16 bit integers with saturation
|
|
|
|
|
float valF;
|
|
|
|
|
int16_t conv;
|
|
|
|
|
const int16_t max16 = 32767;
|
|
|
|
|
const int16_t min16 = -32768;
|
|
|
|
|
for (int s = 0; s < total_frame_samples; s++, frame_position++) {
|
|
|
|
|
valF = frame_samples_float[s] * (1 << 15);
|
|
|
|
|
if (valF > max16)
|
|
|
|
|
conv = max16;
|
|
|
|
|
else if (valF < min16)
|
|
|
|
|
conv = min16;
|
|
|
|
|
else
|
|
|
|
|
conv = int(valF + 32768.5) - 32768; // +0.5 is for rounding
|
2012-08-20 02:59:35 -05:00
|
|
|
|
2020-03-16 14:41:49 +02:00
|
|
|
// Copy into buffer
|
|
|
|
|
all_queued_samples[frame_position] = conv;
|
|
|
|
|
}
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2012-08-20 02:59:35 -05:00
|
|
|
// Deallocate float array
|
|
|
|
|
delete[] frame_samples_float;
|
|
|
|
|
|
|
|
|
|
// Remove front item
|
2012-08-28 15:53:18 -05:00
|
|
|
queued_audio_frames.pop_front();
|
2012-08-20 02:59:35 -05:00
|
|
|
|
|
|
|
|
} // end while
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Update total samples (since we've combined all queued frames)
|
|
|
|
|
total_frame_samples = frame_position;
|
|
|
|
|
int remaining_frame_samples = total_frame_samples;
|
|
|
|
|
int samples_position = 0;
|
|
|
|
|
|
|
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_audio_packets", "is_final", is_final, "total_frame_samples", total_frame_samples, "channel_layout_in_frame", channel_layout_in_frame, "channels_in_frame", channels_in_frame, "samples_in_frame", samples_in_frame, "LAYOUT_MONO", LAYOUT_MONO);
|
2012-08-20 02:59:35 -05:00
|
|
|
|
2015-02-05 00:06:07 -06:00
|
|
|
// Keep track of the original sample format
|
|
|
|
|
AVSampleFormat output_sample_fmt = audio_codec->sample_fmt;
|
2012-08-20 02:59:35 -05:00
|
|
|
|
2015-03-04 15:26:08 -06:00
|
|
|
AVFrame *audio_frame = NULL;
|
2019-04-18 14:04:37 -05:00
|
|
|
if (!is_final) {
|
2015-02-05 00:06:07 -06:00
|
|
|
// Create input frame (and allocate arrays)
|
2015-09-23 00:27:28 -05:00
|
|
|
audio_frame = AV_ALLOCATE_FRAME();
|
|
|
|
|
AV_RESET_FRAME(audio_frame);
|
2015-02-05 00:06:07 -06:00
|
|
|
audio_frame->nb_samples = total_frame_samples / channels_in_frame;
|
2012-08-20 02:59:35 -05:00
|
|
|
|
2015-02-05 00:06:07 -06:00
|
|
|
// Fill input frame with sample data
|
2019-12-18 18:27:04 -08:00
|
|
|
int error_code = avcodec_fill_audio_frame(audio_frame, channels_in_frame, AV_SAMPLE_FMT_S16, (uint8_t *) all_queued_samples, all_queued_samples_size, 0);
|
|
|
|
|
if (error_code < 0) {
|
|
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_audio_packets ERROR [" + (std::string) av_err2str(error_code) + "]", "error_code", error_code);
|
|
|
|
|
}
|
2015-02-05 00:06:07 -06:00
|
|
|
|
|
|
|
|
// Do not convert audio to planar format (yet). We need to keep everything interleaved at this point.
|
2019-04-18 14:04:37 -05:00
|
|
|
switch (audio_codec->sample_fmt) {
|
|
|
|
|
case AV_SAMPLE_FMT_FLTP: {
|
2015-02-05 00:06:07 -06:00
|
|
|
output_sample_fmt = AV_SAMPLE_FMT_FLT;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-04-18 14:04:37 -05:00
|
|
|
case AV_SAMPLE_FMT_S32P: {
|
2015-02-05 00:06:07 -06:00
|
|
|
output_sample_fmt = AV_SAMPLE_FMT_S32;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-04-18 14:04:37 -05:00
|
|
|
case AV_SAMPLE_FMT_S16P: {
|
2015-02-05 00:06:07 -06:00
|
|
|
output_sample_fmt = AV_SAMPLE_FMT_S16;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-04-18 14:04:37 -05:00
|
|
|
case AV_SAMPLE_FMT_U8P: {
|
2015-02-05 00:06:07 -06:00
|
|
|
output_sample_fmt = AV_SAMPLE_FMT_U8;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-12-15 07:14:01 -05:00
|
|
|
default: {
|
|
|
|
|
// This is only here to silence unused-enum warnings
|
|
|
|
|
break;
|
|
|
|
|
}
|
2012-08-20 02:59:35 -05:00
|
|
|
}
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2015-03-01 22:36:39 -06:00
|
|
|
// Update total samples & input frame size (due to bigger or smaller data types)
|
|
|
|
|
total_frame_samples *= (float(info.sample_rate) / sample_rate_in_frame); // adjust for different byte sizes
|
|
|
|
|
total_frame_samples *= (float(info.channels) / channels_in_frame); // adjust for different # of channels
|
|
|
|
|
|
|
|
|
|
// Create output frame (and allocate arrays)
|
2015-09-23 00:27:28 -05:00
|
|
|
AVFrame *audio_converted = AV_ALLOCATE_FRAME();
|
|
|
|
|
AV_RESET_FRAME(audio_converted);
|
2015-03-01 22:36:39 -06:00
|
|
|
audio_converted->nb_samples = total_frame_samples / channels_in_frame;
|
|
|
|
|
av_samples_alloc(audio_converted->data, audio_converted->linesize, info.channels, audio_converted->nb_samples, output_sample_fmt, 0);
|
|
|
|
|
|
2016-04-21 01:39:17 -05:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_audio_packets (1st resampling)", "in_sample_fmt", AV_SAMPLE_FMT_S16, "out_sample_fmt", output_sample_fmt, "in_sample_rate", sample_rate_in_frame, "out_sample_rate", info.sample_rate, "in_channels", channels_in_frame, "out_channels", info.channels);
|
2015-03-01 22:36:39 -06:00
|
|
|
|
2015-02-05 00:06:07 -06:00
|
|
|
// setup resample context
|
|
|
|
|
if (!avr) {
|
2018-08-11 18:22:18 -05:00
|
|
|
avr = SWR_ALLOC();
|
2019-04-18 14:04:37 -05:00
|
|
|
av_opt_set_int(avr, "in_channel_layout", channel_layout_in_frame, 0);
|
2015-02-05 00:06:07 -06:00
|
|
|
av_opt_set_int(avr, "out_channel_layout", info.channel_layout, 0);
|
2019-04-18 14:04:37 -05:00
|
|
|
av_opt_set_int(avr, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
|
|
|
|
|
av_opt_set_int(avr, "out_sample_fmt", output_sample_fmt, 0); // planar not allowed here
|
|
|
|
|
av_opt_set_int(avr, "in_sample_rate", sample_rate_in_frame, 0);
|
|
|
|
|
av_opt_set_int(avr, "out_sample_rate", info.sample_rate, 0);
|
|
|
|
|
av_opt_set_int(avr, "in_channels", channels_in_frame, 0);
|
|
|
|
|
av_opt_set_int(avr, "out_channels", info.channels, 0);
|
2018-08-11 18:22:18 -05:00
|
|
|
SWR_INIT(avr);
|
2015-02-05 00:06:07 -06:00
|
|
|
}
|
|
|
|
|
int nb_samples = 0;
|
|
|
|
|
|
|
|
|
|
// Convert audio samples
|
2019-04-18 14:04:37 -05:00
|
|
|
nb_samples = SWR_CONVERT(avr, // audio resample context
|
|
|
|
|
audio_converted->data, // output data pointers
|
|
|
|
|
audio_converted->linesize[0], // output plane size, in bytes. (0 if unknown)
|
|
|
|
|
audio_converted->nb_samples, // maximum number of samples that the output buffer can hold
|
|
|
|
|
audio_frame->data, // input data pointers
|
|
|
|
|
audio_frame->linesize[0], // input plane size, in bytes (0 if unknown)
|
|
|
|
|
audio_frame->nb_samples); // number of input samples to convert
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2019-12-27 09:54:57 -08:00
|
|
|
// Set remaining samples
|
|
|
|
|
remaining_frame_samples = nb_samples * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16);
|
|
|
|
|
|
2015-03-04 15:26:08 -06:00
|
|
|
// Create a new array (to hold all resampled S16 audio samples)
|
2019-04-18 14:04:37 -05:00
|
|
|
all_resampled_samples = (int16_t *) av_malloc(
|
|
|
|
|
sizeof(int16_t) * nb_samples * info.channels * (av_get_bytes_per_sample(output_sample_fmt) / av_get_bytes_per_sample(AV_SAMPLE_FMT_S16)));
|
2015-03-04 15:26:08 -06:00
|
|
|
|
2015-02-05 00:06:07 -06:00
|
|
|
// Copy audio samples over original samples
|
2015-08-02 20:32:33 -05:00
|
|
|
memcpy(all_resampled_samples, audio_converted->data[0], nb_samples * info.channels * av_get_bytes_per_sample(output_sample_fmt));
|
2015-03-01 22:36:39 -06:00
|
|
|
|
2015-03-04 15:26:08 -06:00
|
|
|
// Remove converted audio
|
2016-07-27 13:18:55 -05:00
|
|
|
av_freep(&(audio_frame->data[0]));
|
2019-01-31 09:42:26 -08:00
|
|
|
AV_FREE_FRAME(&audio_frame);
|
2016-07-27 13:18:55 -05:00
|
|
|
av_freep(&audio_converted->data[0]);
|
2019-01-31 09:42:26 -08:00
|
|
|
AV_FREE_FRAME(&audio_converted);
|
2015-06-01 00:20:14 -07:00
|
|
|
all_queued_samples = NULL; // this array cleared with above call
|
2015-03-04 15:26:08 -06:00
|
|
|
|
2019-07-03 14:14:02 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_audio_packets (Successfully completed 1st resampling)", "nb_samples", nb_samples, "remaining_frame_samples", remaining_frame_samples);
|
2012-08-02 17:19:55 -05:00
|
|
|
}
|
2012-07-24 12:50:17 -05:00
|
|
|
|
2012-08-20 02:59:35 -05:00
|
|
|
// Loop until no more samples
|
2019-04-18 14:04:37 -05:00
|
|
|
while (remaining_frame_samples > 0 || is_final) {
|
2012-08-20 02:59:35 -05:00
|
|
|
// Get remaining samples needed for this packet
|
2015-08-02 20:32:33 -05:00
|
|
|
int remaining_packet_samples = (audio_input_frame_size * info.channels) - audio_input_position;
|
2012-08-04 01:11:12 -05:00
|
|
|
|
2012-08-20 02:59:35 -05:00
|
|
|
// Determine how many samples we need
|
|
|
|
|
int diff = 0;
|
|
|
|
|
if (remaining_frame_samples >= remaining_packet_samples)
|
|
|
|
|
diff = remaining_packet_samples;
|
|
|
|
|
else if (remaining_frame_samples < remaining_packet_samples)
|
|
|
|
|
diff = remaining_frame_samples;
|
2012-07-24 12:50:17 -05:00
|
|
|
|
2012-08-20 02:59:35 -05:00
|
|
|
// Copy frame samples into the packet samples array
|
2019-04-18 14:04:37 -05:00
|
|
|
if (!is_final)
|
2015-08-02 20:32:33 -05:00
|
|
|
//TODO: Make this more sane
|
2019-04-18 14:04:37 -05:00
|
|
|
memcpy(samples + (audio_input_position * (av_get_bytes_per_sample(output_sample_fmt) / av_get_bytes_per_sample(AV_SAMPLE_FMT_S16))),
|
|
|
|
|
all_resampled_samples + samples_position, diff * av_get_bytes_per_sample(output_sample_fmt));
|
2012-07-27 13:48:58 -05:00
|
|
|
|
2012-08-20 02:59:35 -05:00
|
|
|
// Increment counters
|
|
|
|
|
audio_input_position += diff;
|
2015-08-02 20:32:33 -05:00
|
|
|
samples_position += diff * (av_get_bytes_per_sample(output_sample_fmt) / av_get_bytes_per_sample(AV_SAMPLE_FMT_S16));
|
2012-08-20 02:59:35 -05:00
|
|
|
remaining_frame_samples -= diff;
|
|
|
|
|
remaining_packet_samples -= diff;
|
|
|
|
|
|
|
|
|
|
// Do we have enough samples to proceed?
|
2019-04-18 14:04:37 -05:00
|
|
|
if (audio_input_position < (audio_input_frame_size * info.channels) && !is_final)
|
2012-08-20 02:59:35 -05:00
|
|
|
// Not enough samples to encode... so wait until the next frame
|
|
|
|
|
break;
|
|
|
|
|
|
2015-02-05 00:06:07 -06:00
|
|
|
// Convert to planar (if needed by audio codec)
|
2015-09-23 00:27:28 -05:00
|
|
|
AVFrame *frame_final = AV_ALLOCATE_FRAME();
|
|
|
|
|
AV_RESET_FRAME(frame_final);
|
2019-04-18 14:04:37 -05:00
|
|
|
if (av_sample_fmt_is_planar(audio_codec->sample_fmt)) {
|
2016-04-21 01:39:17 -05:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_audio_packets (2nd resampling for Planar formats)", "in_sample_fmt", output_sample_fmt, "out_sample_fmt", audio_codec->sample_fmt, "in_sample_rate", info.sample_rate, "out_sample_rate", info.sample_rate, "in_channels", info.channels, "out_channels", info.channels);
|
2015-03-01 22:36:39 -06:00
|
|
|
|
2015-02-05 00:06:07 -06:00
|
|
|
// setup resample context
|
|
|
|
|
if (!avr_planar) {
|
2018-08-11 18:22:18 -05:00
|
|
|
avr_planar = SWR_ALLOC();
|
2019-04-18 14:04:37 -05:00
|
|
|
av_opt_set_int(avr_planar, "in_channel_layout", info.channel_layout, 0);
|
2015-02-05 00:06:07 -06:00
|
|
|
av_opt_set_int(avr_planar, "out_channel_layout", info.channel_layout, 0);
|
2019-04-18 14:04:37 -05:00
|
|
|
av_opt_set_int(avr_planar, "in_sample_fmt", output_sample_fmt, 0);
|
|
|
|
|
av_opt_set_int(avr_planar, "out_sample_fmt", audio_codec->sample_fmt, 0); // planar not allowed here
|
|
|
|
|
av_opt_set_int(avr_planar, "in_sample_rate", info.sample_rate, 0);
|
|
|
|
|
av_opt_set_int(avr_planar, "out_sample_rate", info.sample_rate, 0);
|
|
|
|
|
av_opt_set_int(avr_planar, "in_channels", info.channels, 0);
|
|
|
|
|
av_opt_set_int(avr_planar, "out_channels", info.channels, 0);
|
2018-08-11 18:22:18 -05:00
|
|
|
SWR_INIT(avr_planar);
|
2015-02-05 00:06:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create input frame (and allocate arrays)
|
2015-09-23 00:27:28 -05:00
|
|
|
audio_frame = AV_ALLOCATE_FRAME();
|
|
|
|
|
AV_RESET_FRAME(audio_frame);
|
2015-02-05 00:06:07 -06:00
|
|
|
audio_frame->nb_samples = audio_input_position / info.channels;
|
2015-03-04 21:33:29 -06:00
|
|
|
|
|
|
|
|
// Create a new array
|
2019-04-18 14:04:37 -05:00
|
|
|
final_samples_planar = (int16_t *) av_malloc(
|
|
|
|
|
sizeof(int16_t) * audio_frame->nb_samples * info.channels * (av_get_bytes_per_sample(output_sample_fmt) / av_get_bytes_per_sample(AV_SAMPLE_FMT_S16)));
|
2015-03-04 21:33:29 -06:00
|
|
|
|
|
|
|
|
// Copy audio into buffer for frame
|
2015-08-02 20:32:33 -05:00
|
|
|
memcpy(final_samples_planar, samples, audio_frame->nb_samples * info.channels * av_get_bytes_per_sample(output_sample_fmt));
|
2015-02-05 00:06:07 -06:00
|
|
|
|
|
|
|
|
// Fill input frame with sample data
|
2015-03-04 21:33:29 -06:00
|
|
|
avcodec_fill_audio_frame(audio_frame, info.channels, output_sample_fmt, (uint8_t *) final_samples_planar,
|
2019-04-18 14:04:37 -05:00
|
|
|
audio_encoder_buffer_size, 0);
|
2015-02-05 00:06:07 -06:00
|
|
|
|
|
|
|
|
// Create output frame (and allocate arrays)
|
2015-08-02 20:32:33 -05:00
|
|
|
frame_final->nb_samples = audio_input_frame_size;
|
|
|
|
|
av_samples_alloc(frame_final->data, frame_final->linesize, info.channels, frame_final->nb_samples, audio_codec->sample_fmt, 0);
|
2015-02-05 00:06:07 -06:00
|
|
|
|
|
|
|
|
// Convert audio samples
|
2019-04-18 14:04:37 -05:00
|
|
|
int nb_samples = SWR_CONVERT(avr_planar, // audio resample context
|
|
|
|
|
frame_final->data, // output data pointers
|
|
|
|
|
frame_final->linesize[0], // output plane size, in bytes. (0 if unknown)
|
|
|
|
|
frame_final->nb_samples, // maximum number of samples that the output buffer can hold
|
|
|
|
|
audio_frame->data, // input data pointers
|
|
|
|
|
audio_frame->linesize[0], // input plane size, in bytes (0 if unknown)
|
|
|
|
|
audio_frame->nb_samples); // number of input samples to convert
|
2015-02-05 00:06:07 -06:00
|
|
|
|
|
|
|
|
// Copy audio samples over original samples
|
2015-08-02 20:32:33 -05:00
|
|
|
if (nb_samples > 0)
|
|
|
|
|
memcpy(samples, frame_final->data[0], nb_samples * av_get_bytes_per_sample(audio_codec->sample_fmt) * info.channels);
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2015-03-04 21:33:29 -06:00
|
|
|
// deallocate AVFrame
|
2019-01-29 12:38:52 -08:00
|
|
|
av_freep(&(audio_frame->data[0]));
|
|
|
|
|
AV_FREE_FRAME(&audio_frame);
|
|
|
|
|
all_queued_samples = NULL; // this array cleared with above call
|
2015-03-01 22:36:39 -06:00
|
|
|
|
2019-07-03 14:14:02 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_audio_packets (Successfully completed 2nd resampling for Planar formats)", "nb_samples", nb_samples);
|
2015-02-05 00:06:07 -06:00
|
|
|
|
|
|
|
|
} else {
|
2015-03-04 21:33:29 -06:00
|
|
|
// Create a new array
|
2019-04-18 14:04:37 -05:00
|
|
|
final_samples = (int16_t *) av_malloc(
|
|
|
|
|
sizeof(int16_t) * audio_input_position * (av_get_bytes_per_sample(audio_codec->sample_fmt) / av_get_bytes_per_sample(AV_SAMPLE_FMT_S16)));
|
2015-03-04 21:33:29 -06:00
|
|
|
|
|
|
|
|
// Copy audio into buffer for frame
|
|
|
|
|
memcpy(final_samples, samples, audio_input_position * av_get_bytes_per_sample(audio_codec->sample_fmt));
|
|
|
|
|
|
2015-08-02 20:32:33 -05:00
|
|
|
// Init the nb_samples property
|
|
|
|
|
frame_final->nb_samples = audio_input_frame_size;
|
|
|
|
|
|
2015-02-05 00:06:07 -06:00
|
|
|
// Fill the final_frame AVFrame with audio (non planar)
|
2015-03-04 21:33:29 -06:00
|
|
|
avcodec_fill_audio_frame(frame_final, audio_codec->channels, audio_codec->sample_fmt, (uint8_t *) final_samples,
|
2019-04-18 14:04:37 -05:00
|
|
|
audio_encoder_buffer_size, 0);
|
2015-02-05 00:06:07 -06:00
|
|
|
}
|
|
|
|
|
|
2015-08-02 20:32:33 -05:00
|
|
|
// Increment PTS (in samples)
|
2015-09-23 00:27:28 -05:00
|
|
|
write_audio_count += FFMIN(audio_input_frame_size, audio_input_position);
|
2012-10-27 21:36:08 -05:00
|
|
|
frame_final->pts = write_audio_count; // Set the AVFrame's PTS
|
|
|
|
|
|
2012-08-20 02:59:35 -05:00
|
|
|
// Init the packet
|
|
|
|
|
AVPacket pkt;
|
|
|
|
|
av_init_packet(&pkt);
|
2015-08-02 20:32:33 -05:00
|
|
|
pkt.data = audio_encoder_buffer;
|
|
|
|
|
pkt.size = audio_encoder_buffer_size;
|
2012-08-20 02:59:35 -05:00
|
|
|
|
2012-10-27 21:36:08 -05:00
|
|
|
// Set the packet's PTS prior to encoding
|
|
|
|
|
pkt.pts = pkt.dts = write_audio_count;
|
2012-10-26 17:15:17 -05:00
|
|
|
|
2012-10-27 21:36:08 -05:00
|
|
|
/* encode the audio samples */
|
|
|
|
|
int got_packet_ptr = 0;
|
2012-08-20 02:59:35 -05:00
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
#if IS_FFMPEG_3_2
|
2018-03-28 15:09:55 -05:00
|
|
|
// Encode audio (latest version of FFmpeg)
|
|
|
|
|
int error_code;
|
|
|
|
|
int ret = 0;
|
|
|
|
|
int frame_finished = 0;
|
|
|
|
|
error_code = ret = avcodec_send_frame(audio_codec, frame_final);
|
|
|
|
|
if (ret < 0 && ret != AVERROR(EINVAL) && ret != AVERROR_EOF) {
|
|
|
|
|
avcodec_send_frame(audio_codec, NULL);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (ret >= 0)
|
|
|
|
|
pkt.size = 0;
|
|
|
|
|
ret = avcodec_receive_packet(audio_codec, &pkt);
|
|
|
|
|
if (ret >= 0)
|
|
|
|
|
frame_finished = 1;
|
|
|
|
|
if(ret == AVERROR(EINVAL) || ret == AVERROR_EOF) {
|
|
|
|
|
avcodec_flush_buffers(audio_codec);
|
|
|
|
|
ret = 0;
|
|
|
|
|
}
|
|
|
|
|
if (ret >= 0) {
|
|
|
|
|
ret = frame_finished;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!pkt.data && !frame_finished)
|
|
|
|
|
{
|
|
|
|
|
ret = -1;
|
|
|
|
|
}
|
|
|
|
|
got_packet_ptr = ret;
|
2019-04-18 14:04:37 -05:00
|
|
|
#else
|
2018-03-28 15:09:55 -05:00
|
|
|
// Encode audio (older versions of FFmpeg)
|
|
|
|
|
int error_code = avcodec_encode_audio2(audio_codec, &pkt, frame_final, &got_packet_ptr);
|
2019-04-18 14:04:37 -05:00
|
|
|
#endif
|
2012-10-27 21:36:08 -05:00
|
|
|
/* if zero size, it means the image was buffered */
|
|
|
|
|
if (error_code == 0 && got_packet_ptr) {
|
2012-08-20 02:59:35 -05:00
|
|
|
|
2012-10-27 21:36:08 -05:00
|
|
|
// Since the PTS can change during encoding, set the value again. This seems like a huge hack,
|
|
|
|
|
// but it fixes lots of PTS related issues when I do this.
|
|
|
|
|
pkt.pts = pkt.dts = write_audio_count;
|
|
|
|
|
|
|
|
|
|
// Scale the PTS to the audio stream timebase (which is sometimes different than the codec's timebase)
|
|
|
|
|
if (pkt.pts != AV_NOPTS_VALUE)
|
|
|
|
|
pkt.pts = av_rescale_q(pkt.pts, audio_codec->time_base, audio_st->time_base);
|
|
|
|
|
if (pkt.dts != AV_NOPTS_VALUE)
|
|
|
|
|
pkt.dts = av_rescale_q(pkt.dts, audio_codec->time_base, audio_st->time_base);
|
|
|
|
|
if (pkt.duration > 0)
|
|
|
|
|
pkt.duration = av_rescale_q(pkt.duration, audio_codec->time_base, audio_st->time_base);
|
|
|
|
|
|
|
|
|
|
// set stream
|
|
|
|
|
pkt.stream_index = audio_st->index;
|
|
|
|
|
pkt.flags |= AV_PKT_FLAG_KEY;
|
|
|
|
|
|
|
|
|
|
/* write the compressed frame in the media file */
|
2012-11-16 17:15:44 -06:00
|
|
|
int error_code = av_interleaved_write_frame(oc, &pkt);
|
2019-04-18 14:04:37 -05:00
|
|
|
if (error_code < 0) {
|
2019-08-04 23:10:59 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_audio_packets ERROR [" + (std::string) av_err2str(error_code) + "]", "error_code", error_code);
|
2012-10-27 21:36:08 -05:00
|
|
|
}
|
2012-08-20 02:59:35 -05:00
|
|
|
}
|
|
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
if (error_code < 0) {
|
2019-08-04 23:10:59 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_audio_packets ERROR [" + (std::string) av_err2str(error_code) + "]", "error_code", error_code);
|
2012-12-03 04:51:17 -06:00
|
|
|
}
|
2012-10-27 21:36:08 -05:00
|
|
|
|
|
|
|
|
// deallocate AVFrame
|
2016-07-27 13:18:55 -05:00
|
|
|
av_freep(&(frame_final->data[0]));
|
2019-01-31 09:42:26 -08:00
|
|
|
AV_FREE_FRAME(&frame_final);
|
2012-10-27 21:36:08 -05:00
|
|
|
|
2012-08-20 02:59:35 -05:00
|
|
|
// deallocate memory for packet
|
2017-08-20 17:37:39 -05:00
|
|
|
AV_FREE_PACKET(&pkt);
|
2012-08-20 02:59:35 -05:00
|
|
|
|
|
|
|
|
// Reset position
|
|
|
|
|
audio_input_position = 0;
|
2019-04-18 14:04:37 -05:00
|
|
|
is_final = false;
|
2012-08-20 02:59:35 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-01 00:20:14 -07:00
|
|
|
// Delete arrays (if needed)
|
2015-03-04 21:33:29 -06:00
|
|
|
if (all_resampled_samples) {
|
2016-07-31 13:56:38 -05:00
|
|
|
av_freep(&all_resampled_samples);
|
2015-03-04 21:33:29 -06:00
|
|
|
all_resampled_samples = NULL;
|
2015-03-04 15:26:08 -06:00
|
|
|
}
|
2015-06-01 00:20:14 -07:00
|
|
|
if (all_queued_samples) {
|
2016-07-31 13:56:38 -05:00
|
|
|
av_freep(&all_queued_samples);
|
2015-06-01 00:20:14 -07:00
|
|
|
all_queued_samples = NULL;
|
|
|
|
|
}
|
2012-08-20 02:59:35 -05:00
|
|
|
|
|
|
|
|
} // end task
|
2012-07-24 12:50:17 -05:00
|
|
|
}
|
|
|
|
|
|
2012-08-03 03:23:10 -05:00
|
|
|
// Allocate an AVFrame object
|
2019-04-18 14:04:37 -05:00
|
|
|
AVFrame *FFmpegWriter::allocate_avframe(PixelFormat pix_fmt, int width, int height, int *buffer_size, uint8_t *new_buffer) {
|
2012-08-03 03:23:10 -05:00
|
|
|
// Create an RGB AVFrame
|
|
|
|
|
AVFrame *new_av_frame = NULL;
|
|
|
|
|
|
|
|
|
|
// Allocate an AVFrame structure
|
2015-09-23 00:27:28 -05:00
|
|
|
new_av_frame = AV_ALLOCATE_FRAME();
|
2012-08-03 03:23:10 -05:00
|
|
|
if (new_av_frame == NULL)
|
2012-08-04 01:11:12 -05:00
|
|
|
throw OutOfMemory("Could not allocate AVFrame", path);
|
2012-08-03 03:23:10 -05:00
|
|
|
|
|
|
|
|
// Determine required buffer size and allocate buffer
|
2018-03-28 15:09:55 -05:00
|
|
|
*buffer_size = AV_GET_IMAGE_SIZE(pix_fmt, width, height);
|
2012-08-03 03:23:10 -05:00
|
|
|
|
2015-06-01 00:20:14 -07:00
|
|
|
// Create buffer (if not provided)
|
2019-04-18 14:04:37 -05:00
|
|
|
if (!new_buffer) {
|
2015-06-01 00:20:14 -07:00
|
|
|
// New Buffer
|
2019-04-18 14:04:37 -05:00
|
|
|
new_buffer = (uint8_t *) av_malloc(*buffer_size * sizeof(uint8_t));
|
2015-06-01 00:20:14 -07:00
|
|
|
// Attach buffer to AVFrame
|
2018-03-28 15:09:55 -05:00
|
|
|
AV_COPY_PICTURE_DATA(new_av_frame, new_buffer, pix_fmt, width, height);
|
2015-09-29 20:23:08 -05:00
|
|
|
new_av_frame->width = width;
|
|
|
|
|
new_av_frame->height = height;
|
|
|
|
|
new_av_frame->format = pix_fmt;
|
2015-06-01 00:20:14 -07:00
|
|
|
}
|
2012-08-03 03:23:10 -05:00
|
|
|
|
|
|
|
|
// return AVFrame
|
|
|
|
|
return new_av_frame;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-20 00:02:09 -05:00
|
|
|
// process video frame
|
2019-04-18 14:04:37 -05:00
|
|
|
void FFmpegWriter::process_video_packet(std::shared_ptr<Frame> frame) {
|
2012-08-24 15:57:49 -05:00
|
|
|
// Determine the height & width of the source image
|
2012-08-22 17:31:12 -05:00
|
|
|
int source_image_width = frame->GetWidth();
|
|
|
|
|
int source_image_height = frame->GetHeight();
|
2012-12-03 13:03:04 -06:00
|
|
|
|
|
|
|
|
// Do nothing if size is 1x1 (i.e. no image in this frame)
|
|
|
|
|
if (source_image_height == 1 && source_image_width == 1)
|
|
|
|
|
return;
|
2012-08-22 17:31:12 -05:00
|
|
|
|
2012-08-24 15:57:49 -05:00
|
|
|
// Init rescalers (if not initialized yet)
|
|
|
|
|
if (image_rescalers.size() == 0)
|
|
|
|
|
InitScalers(source_image_width, source_image_height);
|
2012-08-03 03:23:10 -05:00
|
|
|
|
2012-08-24 15:57:49 -05:00
|
|
|
// Get a unique rescaler (for this thread)
|
|
|
|
|
SwsContext *scaler = image_rescalers[rescaler_position];
|
|
|
|
|
rescaler_position++;
|
|
|
|
|
if (rescaler_position == num_of_rescalers)
|
|
|
|
|
rescaler_position = 0;
|
|
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
#pragma omp task firstprivate(frame, scaler, source_image_width, source_image_height)
|
2012-08-20 00:02:09 -05:00
|
|
|
{
|
|
|
|
|
// Allocate an RGB frame & final output frame
|
|
|
|
|
int bytes_source = 0;
|
|
|
|
|
int bytes_final = 0;
|
2012-08-22 17:31:12 -05:00
|
|
|
AVFrame *frame_source = NULL;
|
2015-06-01 00:20:14 -07:00
|
|
|
const uchar *pixels = NULL;
|
2012-08-22 17:31:12 -05:00
|
|
|
|
2012-11-29 16:32:48 -06:00
|
|
|
// Get a list of pixels from source image
|
2015-06-01 00:20:14 -07:00
|
|
|
pixels = frame->GetPixels();
|
2012-08-22 17:31:12 -05:00
|
|
|
|
|
|
|
|
// Init AVFrame for source image & final (converted image)
|
2019-04-18 14:04:37 -05:00
|
|
|
frame_source = allocate_avframe(PIX_FMT_RGBA, source_image_width, source_image_height, &bytes_source, (uint8_t *) pixels);
|
|
|
|
|
#if IS_FFMPEG_3_2
|
|
|
|
|
AVFrame *frame_final;
|
2020-02-10 01:50:31 -05:00
|
|
|
#if HAVE_HW_ACCEL
|
2019-01-29 12:38:52 -08:00
|
|
|
if (hw_en_on && hw_en_supported) {
|
|
|
|
|
frame_final = allocate_avframe(AV_PIX_FMT_NV12, info.width, info.height, &bytes_final, NULL);
|
2020-02-10 01:50:31 -05:00
|
|
|
} else
|
|
|
|
|
#endif // HAVE_HW_ACCEL
|
|
|
|
|
{
|
2019-01-29 12:38:52 -08:00
|
|
|
frame_final = allocate_avframe((AVPixelFormat)(video_st->codecpar->format), info.width, info.height, &bytes_final, NULL);
|
|
|
|
|
}
|
2019-04-18 14:04:37 -05:00
|
|
|
#else
|
2015-06-01 00:20:14 -07:00
|
|
|
AVFrame *frame_final = allocate_avframe(video_codec->pix_fmt, info.width, info.height, &bytes_final, NULL);
|
2020-02-10 01:50:31 -05:00
|
|
|
#endif // IS_FFMPEG_3_2
|
2012-08-03 03:23:10 -05:00
|
|
|
|
2015-06-01 00:20:14 -07:00
|
|
|
// Fill with data
|
2019-04-18 14:04:37 -05:00
|
|
|
AV_COPY_PICTURE_DATA(frame_source, (uint8_t *) pixels, PIX_FMT_RGBA, source_image_width, source_image_height);
|
2019-07-03 14:14:02 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::process_video_packet", "frame->number", frame->number, "bytes_source", bytes_source, "bytes_final", bytes_final);
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2012-08-20 00:02:09 -05:00
|
|
|
// Resize & convert pixel format
|
|
|
|
|
sws_scale(scaler, frame_source->data, frame_source->linesize, 0,
|
2019-04-18 14:04:37 -05:00
|
|
|
source_image_height, frame_final->data, frame_final->linesize);
|
2012-08-20 00:02:09 -05:00
|
|
|
|
|
|
|
|
// Add resized AVFrame to av_frames map
|
2019-04-18 14:04:37 -05:00
|
|
|
#pragma omp critical (av_frames_section)
|
2012-08-20 02:59:35 -05:00
|
|
|
add_avframe(frame, frame_final);
|
2012-08-20 00:02:09 -05:00
|
|
|
|
|
|
|
|
// Deallocate memory
|
2015-09-23 00:27:28 -05:00
|
|
|
AV_FREE_FRAME(&frame_source);
|
2012-08-20 00:02:09 -05:00
|
|
|
|
|
|
|
|
} // end task
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// write video frame
|
2019-04-18 14:04:37 -05:00
|
|
|
bool FFmpegWriter::write_video_packet(std::shared_ptr<Frame> frame, AVFrame *frame_final) {
|
2018-08-11 18:22:18 -05:00
|
|
|
#if (LIBAVFORMAT_VERSION_MAJOR >= 58)
|
2019-07-03 14:14:02 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_video_packet", "frame->number", frame->number, "oc->oformat->flags", oc->oformat->flags);
|
2020-03-01 16:59:06 -08:00
|
|
|
|
2020-03-01 17:10:39 -08:00
|
|
|
if (AV_GET_CODEC_TYPE(video_st) == AVMEDIA_TYPE_VIDEO && AV_FIND_DECODER_CODEC_ID(video_st) == AV_CODEC_ID_RAWVIDEO) {
|
2018-08-11 18:22:18 -05:00
|
|
|
#else
|
2019-07-03 14:14:02 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_video_packet", "frame->number", frame->number, "oc->oformat->flags & AVFMT_RAWPICTURE", oc->oformat->flags & AVFMT_RAWPICTURE);
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2012-08-03 03:23:10 -05:00
|
|
|
if (oc->oformat->flags & AVFMT_RAWPICTURE) {
|
2020-03-01 16:59:06 -08:00
|
|
|
#endif
|
2012-08-03 03:23:10 -05:00
|
|
|
// Raw video case.
|
|
|
|
|
AVPacket pkt;
|
|
|
|
|
av_init_packet(&pkt);
|
|
|
|
|
|
|
|
|
|
pkt.flags |= AV_PKT_FLAG_KEY;
|
2019-04-18 14:04:37 -05:00
|
|
|
pkt.stream_index = video_st->index;
|
|
|
|
|
pkt.data = (uint8_t *) frame_final->data;
|
|
|
|
|
pkt.size = sizeof(AVPicture);
|
2012-08-03 03:23:10 -05:00
|
|
|
|
2012-11-17 01:57:57 -06:00
|
|
|
// Increment PTS (in frames and scaled to the codec's timebase)
|
2019-04-18 14:04:37 -05:00
|
|
|
write_video_count += av_rescale_q(1, (AVRational) {info.fps.den, info.fps.num}, video_codec->time_base);
|
2012-10-27 21:36:08 -05:00
|
|
|
pkt.pts = write_video_count;
|
|
|
|
|
|
2012-08-04 01:11:12 -05:00
|
|
|
/* write the compressed frame in the media file */
|
2012-11-16 17:15:44 -06:00
|
|
|
int error_code = av_interleaved_write_frame(oc, &pkt);
|
2019-04-18 14:04:37 -05:00
|
|
|
if (error_code < 0) {
|
2019-08-04 23:10:59 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_video_packet ERROR [" + (std::string) av_err2str(error_code) + "]", "error_code", error_code);
|
2016-02-02 01:09:02 -06:00
|
|
|
return false;
|
2012-08-04 01:11:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Deallocate packet
|
2017-08-20 17:37:39 -05:00
|
|
|
AV_FREE_PACKET(&pkt);
|
2012-08-03 03:23:10 -05:00
|
|
|
|
2018-08-11 18:22:18 -05:00
|
|
|
} else
|
2019-01-31 09:42:26 -08:00
|
|
|
{
|
2012-08-03 03:23:10 -05:00
|
|
|
|
2012-10-24 11:10:35 -05:00
|
|
|
AVPacket pkt;
|
|
|
|
|
av_init_packet(&pkt);
|
|
|
|
|
pkt.data = NULL;
|
|
|
|
|
pkt.size = 0;
|
2012-10-27 21:36:08 -05:00
|
|
|
pkt.pts = pkt.dts = AV_NOPTS_VALUE;
|
2012-10-24 11:10:35 -05:00
|
|
|
|
2012-11-17 01:57:57 -06:00
|
|
|
// Pointer for video buffer (if using old FFmpeg version)
|
|
|
|
|
uint8_t *video_outbuf = NULL;
|
|
|
|
|
|
2012-10-27 21:36:08 -05:00
|
|
|
// Increment PTS (in frames and scaled to the codec's timebase)
|
2019-04-18 14:04:37 -05:00
|
|
|
write_video_count += av_rescale_q(1, (AVRational) {info.fps.den, info.fps.num}, video_codec->time_base);
|
2012-10-26 17:15:17 -05:00
|
|
|
|
|
|
|
|
// Assign the initial AVFrame PTS from the frame counter
|
|
|
|
|
frame_final->pts = write_video_count;
|
2020-02-10 01:50:31 -05:00
|
|
|
#if HAVE_HW_ACCEL
|
2019-01-29 12:38:52 -08:00
|
|
|
if (hw_en_on && hw_en_supported) {
|
|
|
|
|
if (!(hw_frame = av_frame_alloc())) {
|
|
|
|
|
fprintf(stderr, "Error code: av_hwframe_alloc\n");
|
|
|
|
|
}
|
|
|
|
|
if (av_hwframe_get_buffer(video_codec->hw_frames_ctx, hw_frame, 0) < 0) {
|
|
|
|
|
fprintf(stderr, "Error code: av_hwframe_get_buffer\n");
|
|
|
|
|
}
|
|
|
|
|
if (!hw_frame->hw_frames_ctx) {
|
|
|
|
|
fprintf(stderr, "Error hw_frames_ctx.\n");
|
|
|
|
|
}
|
|
|
|
|
hw_frame->format = AV_PIX_FMT_NV12;
|
|
|
|
|
if ( av_hwframe_transfer_data(hw_frame, frame_final, 0) < 0) {
|
|
|
|
|
fprintf(stderr, "Error while transferring frame data to surface.\n");
|
|
|
|
|
}
|
|
|
|
|
av_frame_copy_props(hw_frame, frame_final);
|
|
|
|
|
}
|
2020-02-10 01:50:31 -05:00
|
|
|
#endif // HAVE_HW_ACCEL
|
2012-08-03 03:23:10 -05:00
|
|
|
/* encode the image */
|
2012-10-24 11:10:35 -05:00
|
|
|
int got_packet_ptr = 0;
|
2012-11-17 01:57:57 -06:00
|
|
|
int error_code = 0;
|
2019-04-18 14:04:37 -05:00
|
|
|
#if IS_FFMPEG_3_2
|
2018-03-28 15:09:55 -05:00
|
|
|
// Write video packet (latest version of FFmpeg)
|
2019-01-29 12:38:52 -08:00
|
|
|
int ret;
|
2019-04-18 14:04:37 -05:00
|
|
|
|
2020-02-10 01:50:31 -05:00
|
|
|
#if HAVE_HW_ACCEL
|
2019-01-29 12:38:52 -08:00
|
|
|
if (hw_en_on && hw_en_supported) {
|
|
|
|
|
ret = avcodec_send_frame(video_codec, hw_frame); //hw_frame!!!
|
2020-02-10 01:50:31 -05:00
|
|
|
} else
|
|
|
|
|
#endif // HAVE_HW_ACCEL
|
|
|
|
|
{
|
2019-04-18 14:04:37 -05:00
|
|
|
ret = avcodec_send_frame(video_codec, frame_final);
|
|
|
|
|
}
|
2018-03-28 15:09:55 -05:00
|
|
|
error_code = ret;
|
|
|
|
|
if (ret < 0 ) {
|
2019-07-03 14:14:02 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_video_packet (Frame not sent)");
|
2018-09-18 15:31:34 -07:00
|
|
|
if (ret == AVERROR(EAGAIN) ) {
|
2019-08-04 23:51:02 -04:00
|
|
|
std::cerr << "Frame EAGAIN" << "\n";
|
2019-01-29 12:38:52 -08:00
|
|
|
}
|
2018-09-18 15:31:34 -07:00
|
|
|
if (ret == AVERROR_EOF ) {
|
2019-08-04 23:51:02 -04:00
|
|
|
std::cerr << "Frame AVERROR_EOF" << "\n";
|
2019-01-29 12:38:52 -08:00
|
|
|
}
|
2018-03-28 15:09:55 -05:00
|
|
|
avcodec_send_frame(video_codec, NULL);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
while (ret >= 0) {
|
|
|
|
|
ret = avcodec_receive_packet(video_codec, &pkt);
|
2019-04-18 14:04:37 -05:00
|
|
|
|
|
|
|
|
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
|
2018-03-28 15:09:55 -05:00
|
|
|
avcodec_flush_buffers(video_codec);
|
|
|
|
|
got_packet_ptr = 0;
|
2019-04-18 14:04:37 -05:00
|
|
|
break;
|
2018-03-28 15:09:55 -05:00
|
|
|
}
|
|
|
|
|
if (ret == 0) {
|
|
|
|
|
got_packet_ptr = 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2012-11-17 01:57:57 -06:00
|
|
|
}
|
2018-03-28 15:09:55 -05:00
|
|
|
}
|
2019-04-18 14:04:37 -05:00
|
|
|
#else
|
|
|
|
|
#if LIBAVFORMAT_VERSION_MAJOR >= 54
|
|
|
|
|
// Write video packet (older than FFmpeg 3.2)
|
|
|
|
|
error_code = avcodec_encode_video2(video_codec, &pkt, frame_final, &got_packet_ptr);
|
|
|
|
|
if (error_code != 0) {
|
2019-08-04 23:51:02 -04:00
|
|
|
std::cerr << "Frame AVERROR_EOF" << "\n";
|
2019-04-18 14:04:37 -05:00
|
|
|
}
|
|
|
|
|
if (got_packet_ptr == 0) {
|
2019-08-04 23:51:02 -04:00
|
|
|
std::cerr << "Frame gotpacket error" << "\n";
|
2019-04-18 14:04:37 -05:00
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
// Write video packet (even older versions of FFmpeg)
|
|
|
|
|
int video_outbuf_size = 200000;
|
|
|
|
|
video_outbuf = (uint8_t*) av_malloc(200000);
|
2018-03-28 15:09:55 -05:00
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
/* encode the image */
|
|
|
|
|
int out_size = avcodec_encode_video(video_codec, video_outbuf, video_outbuf_size, frame_final);
|
2018-03-28 15:09:55 -05:00
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
/* if zero size, it means the image was buffered */
|
|
|
|
|
if (out_size > 0) {
|
|
|
|
|
if(video_codec->coded_frame->key_frame)
|
|
|
|
|
pkt.flags |= AV_PKT_FLAG_KEY;
|
|
|
|
|
pkt.data= video_outbuf;
|
|
|
|
|
pkt.size= out_size;
|
2018-03-28 15:09:55 -05:00
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
// got data back (so encode this frame)
|
|
|
|
|
got_packet_ptr = 1;
|
|
|
|
|
}
|
2020-02-10 01:50:31 -05:00
|
|
|
#endif // LIBAVFORMAT_VERSION_MAJOR >= 54
|
|
|
|
|
#endif // IS_FFMPEG_3_2
|
2012-08-03 03:23:10 -05:00
|
|
|
|
|
|
|
|
/* if zero size, it means the image was buffered */
|
2012-10-24 11:10:35 -05:00
|
|
|
if (error_code == 0 && got_packet_ptr) {
|
2012-08-03 03:23:10 -05:00
|
|
|
|
2012-10-28 03:35:50 -05:00
|
|
|
// Since the PTS can change during encoding, set the value again. This seems like a huge hack,
|
|
|
|
|
// but it fixes lots of PTS related issues when I do this.
|
|
|
|
|
//pkt.pts = pkt.dts = write_video_count;
|
|
|
|
|
|
2012-10-24 11:10:35 -05:00
|
|
|
// set the timestamp
|
|
|
|
|
if (pkt.pts != AV_NOPTS_VALUE)
|
|
|
|
|
pkt.pts = av_rescale_q(pkt.pts, video_codec->time_base, video_st->time_base);
|
2012-10-27 21:36:08 -05:00
|
|
|
if (pkt.dts != AV_NOPTS_VALUE)
|
|
|
|
|
pkt.dts = av_rescale_q(pkt.dts, video_codec->time_base, video_st->time_base);
|
|
|
|
|
if (pkt.duration > 0)
|
|
|
|
|
pkt.duration = av_rescale_q(pkt.duration, video_codec->time_base, video_st->time_base);
|
|
|
|
|
pkt.stream_index = video_st->index;
|
2012-08-03 03:23:10 -05:00
|
|
|
|
|
|
|
|
/* write the compressed frame in the media file */
|
2012-11-16 17:15:44 -06:00
|
|
|
int error_code = av_interleaved_write_frame(oc, &pkt);
|
2019-04-18 14:04:37 -05:00
|
|
|
if (error_code < 0) {
|
2019-08-04 23:10:59 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::write_video_packet ERROR [" + (std::string) av_err2str(error_code) + "]", "error_code", error_code);
|
2016-02-02 01:09:02 -06:00
|
|
|
return false;
|
2012-08-04 01:11:12 -05:00
|
|
|
}
|
2012-08-03 03:23:10 -05:00
|
|
|
}
|
2012-10-24 11:10:35 -05:00
|
|
|
|
2012-11-17 01:57:57 -06:00
|
|
|
// Deallocate memory (if needed)
|
|
|
|
|
if (video_outbuf)
|
|
|
|
|
delete[] video_outbuf;
|
|
|
|
|
|
2012-10-24 11:10:35 -05:00
|
|
|
// Deallocate packet
|
2017-08-20 17:37:39 -05:00
|
|
|
AV_FREE_PACKET(&pkt);
|
2020-02-10 01:50:31 -05:00
|
|
|
#if HAVE_HW_ACCEL
|
2019-01-29 12:38:52 -08:00
|
|
|
if (hw_en_on && hw_en_supported) {
|
|
|
|
|
if (hw_frame) {
|
|
|
|
|
av_frame_free(&hw_frame);
|
|
|
|
|
hw_frame = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-10 01:50:31 -05:00
|
|
|
#endif // HAVE_HW_ACCEL
|
2012-08-03 03:23:10 -05:00
|
|
|
}
|
2016-02-02 01:09:02 -06:00
|
|
|
|
|
|
|
|
// Success
|
|
|
|
|
return true;
|
2012-08-04 01:11:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Output the ffmpeg info about this format, streams, and codecs (i.e. dump format)
|
2019-04-18 14:04:37 -05:00
|
|
|
void FFmpegWriter::OutputStreamInfo() {
|
2012-08-04 01:11:12 -05:00
|
|
|
// output debug info
|
|
|
|
|
av_dump_format(oc, 0, path.c_str(), 1);
|
2012-07-24 12:50:17 -05:00
|
|
|
}
|
2012-08-24 15:57:49 -05:00
|
|
|
|
|
|
|
|
// Init a collection of software rescalers (thread safe)
|
2019-04-18 14:04:37 -05:00
|
|
|
void FFmpegWriter::InitScalers(int source_width, int source_height) {
|
2019-01-09 16:50:40 -06:00
|
|
|
int scale_mode = SWS_FAST_BILINEAR;
|
|
|
|
|
if (openshot::Settings::Instance()->HIGH_QUALITY_SCALING) {
|
2019-11-17 16:34:50 -06:00
|
|
|
scale_mode = SWS_BICUBIC;
|
2019-01-09 16:50:40 -06:00
|
|
|
}
|
|
|
|
|
|
2012-08-24 15:57:49 -05:00
|
|
|
// Init software rescalers vector (many of them, one for each thread)
|
2019-04-18 14:04:37 -05:00
|
|
|
for (int x = 0; x < num_of_rescalers; x++) {
|
2012-08-24 15:57:49 -05:00
|
|
|
// Init the software scaler from FFMpeg
|
2020-02-10 01:50:31 -05:00
|
|
|
#if HAVE_HW_ACCEL
|
2019-01-29 12:38:52 -08:00
|
|
|
if (hw_en_on && hw_en_supported) {
|
2019-11-17 16:36:31 -06:00
|
|
|
img_convert_ctx = sws_getContext(source_width, source_height, PIX_FMT_RGBA, info.width, info.height, AV_PIX_FMT_NV12, scale_mode, NULL, NULL, NULL);
|
2019-01-29 12:38:52 -08:00
|
|
|
} else
|
2020-02-10 01:50:31 -05:00
|
|
|
#endif // HAVE_HW_ACCEL
|
2019-01-29 12:38:52 -08:00
|
|
|
{
|
2019-11-17 16:36:31 -06:00
|
|
|
img_convert_ctx = sws_getContext(source_width, source_height, PIX_FMT_RGBA, info.width, info.height, AV_GET_CODEC_PIXEL_FORMAT(video_st, video_st->codec), scale_mode,
|
2019-04-18 14:04:37 -05:00
|
|
|
NULL, NULL, NULL);
|
2019-01-29 12:38:52 -08:00
|
|
|
}
|
2012-08-24 15:57:49 -05:00
|
|
|
|
|
|
|
|
// Add rescaler to vector
|
|
|
|
|
image_rescalers.push_back(img_convert_ctx);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 17:17:38 -06:00
|
|
|
// Set audio resample options
|
|
|
|
|
void FFmpegWriter::ResampleAudio(int sample_rate, int channels) {
|
|
|
|
|
original_sample_rate = sample_rate;
|
|
|
|
|
original_channels = channels;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-24 15:57:49 -05:00
|
|
|
// Remove & deallocate all software scalers
|
2019-04-18 14:04:37 -05:00
|
|
|
void FFmpegWriter::RemoveScalers() {
|
2012-08-24 15:57:49 -05:00
|
|
|
// Close all rescalers
|
|
|
|
|
for (int x = 0; x < num_of_rescalers; x++)
|
|
|
|
|
sws_freeContext(image_rescalers[x]);
|
|
|
|
|
|
|
|
|
|
// Clear vector
|
|
|
|
|
image_rescalers.clear();
|
|
|
|
|
}
|