2013-09-12 17:52:10 -05:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @brief Source file for FFmpegWriter class
|
|
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org>, Fabrice Bellard
|
|
|
|
|
*
|
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.
|
|
|
|
|
*
|
2021-10-16 01:26:26 -04:00
|
|
|
* @ref License
|
2012-07-19 15:03:55 -05:00
|
|
|
*/
|
|
|
|
|
|
2024-05-08 12:16:41 +02:00
|
|
|
// Copyright (c) 2008-2024 OpenShot Studios, LLC, Fabrice Bellard
|
2021-10-16 01:26:26 -04:00
|
|
|
//
|
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
|
|
2022-01-12 01:08:31 -05:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <cmath>
|
|
|
|
|
#include <ctime>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2021-10-27 06:49:27 -04:00
|
|
|
#include "FFmpegUtilities.h"
|
|
|
|
|
|
2020-10-18 07:43:37 -04:00
|
|
|
#include "FFmpegWriter.h"
|
2021-01-26 10:52:04 -05:00
|
|
|
#include "Exceptions.h"
|
2021-10-27 14:34:05 -04:00
|
|
|
#include "Frame.h"
|
2022-01-12 01:08:31 -05:00
|
|
|
#include "OpenMPUtilities.h"
|
|
|
|
|
#include "Settings.h"
|
|
|
|
|
#include "ZmqLogger.h"
|
2020-03-04 23:52:42 -05:00
|
|
|
|
2012-07-12 15:55:41 -05:00
|
|
|
using namespace openshot;
|
|
|
|
|
|
2019-06-11 10:49:45 +03:00
|
|
|
// Multiplexer parameters temporary storage
|
|
|
|
|
AVDictionary *mux_dict = NULL;
|
|
|
|
|
|
2021-06-04 21:32:29 -04:00
|
|
|
#if USE_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))) {
|
2020-03-04 23:52:42 -05:00
|
|
|
std::clog << "Failed to create HW frame context.\n";
|
2019-01-29 12:38:52 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
frames_ctx = (AVHWFramesContext *)(hw_frames_ref->data);
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
frames_ctx->format = hw_en_av_pix_fmt;
|
2019-01-29 12:38:52 -08:00
|
|
|
frames_ctx->sw_format = AV_PIX_FMT_NV12;
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
frames_ctx->width = width;
|
|
|
|
|
frames_ctx->height = height;
|
2019-01-29 12:38:52 -08:00
|
|
|
frames_ctx->initial_pool_size = 20;
|
|
|
|
|
if ((err = av_hwframe_ctx_init(hw_frames_ref)) < 0) {
|
2020-03-04 23:52:42 -05:00
|
|
|
std::clog << "Failed to initialize HW frame context. " <<
|
2021-08-11 03:58:45 -04:00
|
|
|
"Error code: " << av_err2string(err) << "\n";
|
2019-01-29 12:38:52 -08:00
|
|
|
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
|
|
|
}
|
2021-06-04 21:32:29 -04:00
|
|
|
#endif // USE_HW_ACCEL
|
2018-08-31 21:36:23 -07:00
|
|
|
|
2020-06-08 16:02:02 -04:00
|
|
|
FFmpegWriter::FFmpegWriter(const std::string& path) :
|
FFmpegWriter: Macro & member cleanup
- The `fmt` class member, which was of type AVFormat*, was really
just an unnecessary copy of `(AVFormatContext*)oc->oformat`.
But we were ASSIGNING into its members, which we were definitely
not supposed to be doing. (And in recent FFmpegs, now that
`AVFormat` has been `const`d, we can't.) It's gone; now we just
use `oc->oformat` anywhere we used to access `fmt`.
- The preprocessor macro to allocate a new _stream_ was a mess of
cross purposes: It did allocate a stream, but then it also
allocated a new AvCodecCtx on newer FFmpeg releases. Worse (and
always galling to me), it proceeded to assign to a variable
that WASN'T passed in to the macro, just taking it on faith that
it would only be used where that variable was defined. That's
just... ugh. So I broke it apart into two steps (stream creation
and context allocation), realized the stream creation code was
the same for all ffmpeg versions and didn't need to be a macro
at all, and now a 4-parameter, 6-line magical macro has been
replaced with a simple, zero-side-effect one-liner.
- I also cleaned up the add_video_stream() code to be more like
the add_audio_stream() code, since they were bad-different for
no discernible reason.
2022-02-24 07:29:08 -05:00
|
|
|
path(path), 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),
|
2024-02-10 20:28:47 -06:00
|
|
|
initial_audio_input_frame_size(0), img_convert_ctx(NULL), num_of_rescalers(1),
|
2021-06-26 17:24:15 -05:00
|
|
|
rescaler_position(0), video_codec_ctx(NULL), audio_codec_ctx(NULL), is_writing(false), video_timestamp(0), audio_timestamp(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
|
|
|
|
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());
|
FFmpegWriter: Macro & member cleanup
- The `fmt` class member, which was of type AVFormat*, was really
just an unnecessary copy of `(AVFormatContext*)oc->oformat`.
But we were ASSIGNING into its members, which we were definitely
not supposed to be doing. (And in recent FFmpegs, now that
`AVFormat` has been `const`d, we can't.) It's gone; now we just
use `oc->oformat` anywhere we used to access `fmt`.
- The preprocessor macro to allocate a new _stream_ was a mess of
cross purposes: It did allocate a stream, but then it also
allocated a new AvCodecCtx on newer FFmpeg releases. Worse (and
always galling to me), it proceeded to assign to a variable
that WASN'T passed in to the macro, just taking it on faith that
it would only be used where that variable was defined. That's
just... ugh. So I broke it apart into two steps (stream creation
and context allocation), realized the stream creation code was
the same for all ffmpeg versions and didn't need to be a macro
at all, and now a 4-parameter, 6-line magical macro has been
replaced with a simple, zero-side-effect one-liner.
- I also cleaned up the add_video_stream() code to be more like
the add_audio_stream() code, since they were bad-different for
no discernible reason.
2022-02-24 07:29:08 -05:00
|
|
|
if (!oc) {
|
|
|
|
|
throw OutOfMemory(
|
|
|
|
|
"Could not allocate memory for AVFormatContext.", path);
|
|
|
|
|
}
|
2012-07-20 00:13:16 -05:00
|
|
|
|
FFmpegWriter: Macro & member cleanup
- The `fmt` class member, which was of type AVFormat*, was really
just an unnecessary copy of `(AVFormatContext*)oc->oformat`.
But we were ASSIGNING into its members, which we were definitely
not supposed to be doing. (And in recent FFmpegs, now that
`AVFormat` has been `const`d, we can't.) It's gone; now we just
use `oc->oformat` anywhere we used to access `fmt`.
- The preprocessor macro to allocate a new _stream_ was a mess of
cross purposes: It did allocate a stream, but then it also
allocated a new AvCodecCtx on newer FFmpeg releases. Worse (and
always galling to me), it proceeded to assign to a variable
that WASN'T passed in to the macro, just taking it on faith that
it would only be used where that variable was defined. That's
just... ugh. So I broke it apart into two steps (stream creation
and context allocation), realized the stream creation code was
the same for all ffmpeg versions and didn't need to be a macro
at all, and now a 4-parameter, 6-line magical macro has been
replaced with a simple, zero-side-effect one-liner.
- I also cleaned up the add_video_stream() code to be more like
the add_audio_stream() code, since they were bad-different for
no discernible reason.
2022-02-24 07:29:08 -05:00
|
|
|
// Determine what format to use when encoding this output filename
|
|
|
|
|
oc->oformat = av_guess_format(NULL, path.c_str(), NULL);
|
|
|
|
|
if (oc->oformat == nullptr) {
|
|
|
|
|
throw InvalidFormat(
|
|
|
|
|
"Could not deduce output format from file extension.", path);
|
|
|
|
|
}
|
2012-07-19 15:03:55 -05:00
|
|
|
|
FFmpegWriter: Macro & member cleanup
- The `fmt` class member, which was of type AVFormat*, was really
just an unnecessary copy of `(AVFormatContext*)oc->oformat`.
But we were ASSIGNING into its members, which we were definitely
not supposed to be doing. (And in recent FFmpegs, now that
`AVFormat` has been `const`d, we can't.) It's gone; now we just
use `oc->oformat` anywhere we used to access `fmt`.
- The preprocessor macro to allocate a new _stream_ was a mess of
cross purposes: It did allocate a stream, but then it also
allocated a new AvCodecCtx on newer FFmpeg releases. Worse (and
always galling to me), it proceeded to assign to a variable
that WASN'T passed in to the macro, just taking it on faith that
it would only be used where that variable was defined. That's
just... ugh. So I broke it apart into two steps (stream creation
and context allocation), realized the stream creation code was
the same for all ffmpeg versions and didn't need to be a macro
at all, and now a 4-parameter, 6-line magical macro has been
replaced with a simple, zero-side-effect one-liner.
- I also cleaned up the add_video_stream() code to be more like
the add_audio_stream() code, since they were bad-different for
no discernible reason.
2022-02-24 07:29:08 -05:00
|
|
|
// Update video codec name
|
|
|
|
|
if (oc->oformat->video_codec != AV_CODEC_ID_NONE && info.has_video)
|
|
|
|
|
info.vcodec = avcodec_find_encoder(oc->oformat->video_codec)->name;
|
2012-07-20 00:13:16 -05:00
|
|
|
|
FFmpegWriter: Macro & member cleanup
- The `fmt` class member, which was of type AVFormat*, was really
just an unnecessary copy of `(AVFormatContext*)oc->oformat`.
But we were ASSIGNING into its members, which we were definitely
not supposed to be doing. (And in recent FFmpegs, now that
`AVFormat` has been `const`d, we can't.) It's gone; now we just
use `oc->oformat` anywhere we used to access `fmt`.
- The preprocessor macro to allocate a new _stream_ was a mess of
cross purposes: It did allocate a stream, but then it also
allocated a new AvCodecCtx on newer FFmpeg releases. Worse (and
always galling to me), it proceeded to assign to a variable
that WASN'T passed in to the macro, just taking it on faith that
it would only be used where that variable was defined. That's
just... ugh. So I broke it apart into two steps (stream creation
and context allocation), realized the stream creation code was
the same for all ffmpeg versions and didn't need to be a macro
at all, and now a 4-parameter, 6-line magical macro has been
replaced with a simple, zero-side-effect one-liner.
- I also cleaned up the add_video_stream() code to be more like
the add_audio_stream() code, since they were bad-different for
no discernible reason.
2022-02-24 07:29:08 -05:00
|
|
|
// Update audio codec name
|
|
|
|
|
if (oc->oformat->audio_codec != AV_CODEC_ID_NONE && info.has_audio)
|
|
|
|
|
info.acodec = avcodec_find_encoder(oc->oformat->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() {
|
2022-01-12 01:08:31 -05:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"FFmpegWriter::initialize_streams",
|
FFmpegWriter: Macro & member cleanup
- The `fmt` class member, which was of type AVFormat*, was really
just an unnecessary copy of `(AVFormatContext*)oc->oformat`.
But we were ASSIGNING into its members, which we were definitely
not supposed to be doing. (And in recent FFmpegs, now that
`AVFormat` has been `const`d, we can't.) It's gone; now we just
use `oc->oformat` anywhere we used to access `fmt`.
- The preprocessor macro to allocate a new _stream_ was a mess of
cross purposes: It did allocate a stream, but then it also
allocated a new AvCodecCtx on newer FFmpeg releases. Worse (and
always galling to me), it proceeded to assign to a variable
that WASN'T passed in to the macro, just taking it on faith that
it would only be used where that variable was defined. That's
just... ugh. So I broke it apart into two steps (stream creation
and context allocation), realized the stream creation code was
the same for all ffmpeg versions and didn't need to be a macro
at all, and now a 4-parameter, 6-line magical macro has been
replaced with a simple, zero-side-effect one-liner.
- I also cleaned up the add_video_stream() code to be more like
the add_audio_stream() code, since they were bad-different for
no discernible reason.
2022-02-24 07:29:08 -05:00
|
|
|
"oc->oformat->video_codec", oc->oformat->video_codec,
|
|
|
|
|
"oc->oformat->audio_codec", oc->oformat->audio_codec,
|
2022-01-12 01:08:31 -05:00
|
|
|
"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;
|
FFmpegWriter: Macro & member cleanup
- The `fmt` class member, which was of type AVFormat*, was really
just an unnecessary copy of `(AVFormatContext*)oc->oformat`.
But we were ASSIGNING into its members, which we were definitely
not supposed to be doing. (And in recent FFmpegs, now that
`AVFormat` has been `const`d, we can't.) It's gone; now we just
use `oc->oformat` anywhere we used to access `fmt`.
- The preprocessor macro to allocate a new _stream_ was a mess of
cross purposes: It did allocate a stream, but then it also
allocated a new AvCodecCtx on newer FFmpeg releases. Worse (and
always galling to me), it proceeded to assign to a variable
that WASN'T passed in to the macro, just taking it on faith that
it would only be used where that variable was defined. That's
just... ugh. So I broke it apart into two steps (stream creation
and context allocation), realized the stream creation code was
the same for all ffmpeg versions and didn't need to be a macro
at all, and now a 4-parameter, 6-line magical macro has been
replaced with a simple, zero-side-effect one-liner.
- I also cleaned up the add_video_stream() code to be more like
the add_audio_stream() code, since they were bad-different for
no discernible reason.
2022-02-24 07:29:08 -05:00
|
|
|
if (oc->oformat->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
|
|
|
|
FFmpegWriter: Macro & member cleanup
- The `fmt` class member, which was of type AVFormat*, was really
just an unnecessary copy of `(AVFormatContext*)oc->oformat`.
But we were ASSIGNING into its members, which we were definitely
not supposed to be doing. (And in recent FFmpegs, now that
`AVFormat` has been `const`d, we can't.) It's gone; now we just
use `oc->oformat` anywhere we used to access `fmt`.
- The preprocessor macro to allocate a new _stream_ was a mess of
cross purposes: It did allocate a stream, but then it also
allocated a new AvCodecCtx on newer FFmpeg releases. Worse (and
always galling to me), it proceeded to assign to a variable
that WASN'T passed in to the macro, just taking it on faith that
it would only be used where that variable was defined. That's
just... ugh. So I broke it apart into two steps (stream creation
and context allocation), realized the stream creation code was
the same for all ffmpeg versions and didn't need to be a macro
at all, and now a 4-parameter, 6-line magical macro has been
replaced with a simple, zero-side-effect one-liner.
- I also cleaned up the add_video_stream() code to be more like
the add_audio_stream() code, since they were bad-different for
no discernible reason.
2022-02-24 07:29:08 -05:00
|
|
|
if (oc->oformat->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) {
|
2021-11-21 23:25:37 -05:00
|
|
|
const AVCodec *new_codec;
|
2018-08-31 21:36:23 -07:00
|
|
|
// Check if the codec selected is a hardware accelerated codec
|
2021-06-04 21:32:29 -04:00
|
|
|
#if USE_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
|
|
|
}
|
2020-03-04 23:58:16 -05:00
|
|
|
#else // unknown OS
|
2018-08-31 21:36:23 -07:00
|
|
|
new_codec = avcodec_find_encoder_by_name(codec.c_str());
|
2020-03-04 23:58:16 -05:00
|
|
|
#endif //__linux__/_WIN32/__APPLE__
|
2021-06-04 21:32:29 -04:00
|
|
|
#else // USE_HW_ACCEL
|
2018-08-31 21:36:23 -07:00
|
|
|
new_codec = avcodec_find_encoder_by_name(codec.c_str());
|
2021-06-04 21:32:29 -04:00
|
|
|
#endif // USE_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;
|
|
|
|
|
}
|
|
|
|
|
}
|
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;
|
|
|
|
|
}
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06: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;
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06: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
|
|
|
|
2022-01-12 01:08:31 -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
|
2020-03-05 01:38:25 -05:00
|
|
|
FFmpegWriter::SetVideoOptions(
|
|
|
|
|
true, codec, fps, width, height,
|
|
|
|
|
openshot::Fraction(1, 1), false, true, bit_rate
|
|
|
|
|
);
|
2020-01-31 03:53:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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) {
|
2021-11-21 23:25:37 -05:00
|
|
|
const AVCodec *new_codec = avcodec_find_encoder_by_name(codec.c_str());
|
2012-07-19 15:03:55 -05:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
2020-08-08 03:38:30 -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-03-05 01:38:25 -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
|
2020-03-22 12:08:40 -04:00
|
|
|
c = AV_GET_CODEC_PAR_CONTEXT(st, video_codec_ctx);
|
2020-06-16 10:22:52 -07:00
|
|
|
// Was a codec / stream found?
|
|
|
|
|
if (c) {
|
|
|
|
|
if (info.interlaced_frame) {
|
|
|
|
|
c->field_order = info.top_field_first ? AV_FIELD_TT : AV_FIELD_BB;
|
2020-07-08 17:11:47 -07:00
|
|
|
// We only use these two version and ignore AV_FIELD_TB and AV_FIELD_BT
|
|
|
|
|
// Otherwise we would need to change the whole export window
|
2020-06-16 10:22:52 -07:00
|
|
|
}
|
|
|
|
|
}
|
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
|
2020-03-22 12:08:40 -04:00
|
|
|
c = AV_GET_CODEC_PAR_CONTEXT(st, audio_codec_ctx);
|
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
|
2021-06-04 21:32:29 -04:00
|
|
|
#if USE_HW_ACCEL
|
2020-03-04 23:58:16 -05:00
|
|
|
if (hw_en_on) {
|
|
|
|
|
av_opt_set_int(c->priv_data, "qp", std::min(std::stoi(value),63), 0); // 0-63
|
|
|
|
|
} else
|
2021-06-04 21:32:29 -04:00
|
|
|
#endif // USE_HW_ACCEL
|
2020-03-04 23:58:16 -05:00
|
|
|
{
|
2019-08-23 19:46:05 +02:00
|
|
|
switch (c->codec_id) {
|
2020-10-13 07:00:58 -04:00
|
|
|
#if (LIBAVCODEC_VERSION_MAJOR >= 58)
|
2020-03-05 01:38:25 -05:00
|
|
|
// FFmpeg 4.0+
|
2020-03-04 23:58:16 -05:00
|
|
|
case AV_CODEC_ID_AV1 :
|
|
|
|
|
c->bit_rate = 0;
|
|
|
|
|
av_opt_set_int(c->priv_data, "qp", std::min(std::stoi(value),63), 0); // 0-63
|
|
|
|
|
break;
|
2020-10-13 07:00:58 -04:00
|
|
|
#endif
|
2020-03-04 23:58:16 -05:00
|
|
|
case AV_CODEC_ID_VP8 :
|
|
|
|
|
c->bit_rate = 10000000;
|
|
|
|
|
av_opt_set_int(c->priv_data, "qp", std::max(std::min(std::stoi(value), 63), 4), 0); // 4-63
|
|
|
|
|
break;
|
|
|
|
|
case AV_CODEC_ID_VP9 :
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
c->bit_rate = 0; // Must be zero!
|
2020-03-04 23:58:16 -05:00
|
|
|
av_opt_set_int(c->priv_data, "qp", std::min(std::stoi(value), 63), 0); // 0-63
|
|
|
|
|
if (std::stoi(value) == 0) {
|
|
|
|
|
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 :
|
|
|
|
|
av_opt_set_int(c->priv_data, "qp", std::min(std::stoi(value), 51), 0); // 0-51
|
|
|
|
|
if (std::stoi(value) == 0) {
|
|
|
|
|
av_opt_set(c->priv_data, "preset", "veryslow", 0);
|
2019-11-21 14:35:09 +02:00
|
|
|
c->pix_fmt = PIX_FMT_YUV444P; // no chroma subsampling
|
2020-03-04 23:58:16 -05:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case AV_CODEC_ID_HEVC :
|
|
|
|
|
av_opt_set_int(c->priv_data, "qp", std::min(std::stoi(value), 51), 0); // 0-51
|
|
|
|
|
if (std::stoi(value) == 0) {
|
|
|
|
|
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
|
|
|
|
|
av_opt_set_int(c->priv_data, "qp", std::min(std::stoi(value), 63), 0); // 0-63
|
|
|
|
|
c->bit_rate = 0;
|
2019-08-23 19:46:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} 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
|
2021-06-04 21:32:29 -04:00
|
|
|
#if USE_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
|
2021-06-04 21:32:29 -04:00
|
|
|
#endif // USE_HW_ACCEL
|
2019-04-18 14:04:37 -05:00
|
|
|
{
|
|
|
|
|
switch (c->codec_id) {
|
|
|
|
|
#if (LIBAVCODEC_VERSION_MAJOR >= 58)
|
2020-08-08 03:38:30 -04:00
|
|
|
// FFmpeg 4.0+
|
2019-04-18 14:04:37 -05:00
|
|
|
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 :
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
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);
|
2019-11-21 14:35:09 +02:00
|
|
|
c->pix_fmt = PIX_FMT_YUV444P; // no chroma subsampling
|
2019-04-18 14:04:37 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
} 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)
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// FFmpeg 4.0+
|
2020-02-16 10:00:18 -08:00
|
|
|
switch (c->codec_id) {
|
|
|
|
|
case AV_CODEC_ID_AV1 :
|
|
|
|
|
c->bit_rate = 0;
|
2020-09-13 08:42:17 -07:00
|
|
|
if (strstr(info.vcodec.c_str(), "svtav1") != NULL) {
|
2020-02-16 10:00:18 -08:00
|
|
|
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
|
2020-06-15 14:41:38 -07:00
|
|
|
// 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
|
2020-06-15 14:41:38 -07:00
|
|
|
// TODO Let user choose number of tiles
|
2020-02-16 19:16:45 -08:00
|
|
|
// 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;
|
|
|
|
|
}
|
2020-08-08 03:38:30 -04:00
|
|
|
#endif // FFmpeg 4.0+
|
2019-04-18 14:04:37 -05:00
|
|
|
} 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
|
|
|
|
2022-01-12 01:08:31 -05: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
|
|
|
|
2022-01-12 01:08:31 -05: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
|
FFmpegWriter: Macro & member cleanup
- The `fmt` class member, which was of type AVFormat*, was really
just an unnecessary copy of `(AVFormatContext*)oc->oformat`.
But we were ASSIGNING into its members, which we were definitely
not supposed to be doing. (And in recent FFmpegs, now that
`AVFormat` has been `const`d, we can't.) It's gone; now we just
use `oc->oformat` anywhere we used to access `fmt`.
- The preprocessor macro to allocate a new _stream_ was a mess of
cross purposes: It did allocate a stream, but then it also
allocated a new AvCodecCtx on newer FFmpeg releases. Worse (and
always galling to me), it proceeded to assign to a variable
that WASN'T passed in to the macro, just taking it on faith that
it would only be used where that variable was defined. That's
just... ugh. So I broke it apart into two steps (stream creation
and context allocation), realized the stream creation code was
the same for all ffmpeg versions and didn't need to be a macro
at all, and now a 4-parameter, 6-line magical macro has been
replaced with a simple, zero-side-effect one-liner.
- I also cleaned up the add_video_stream() code to be more like
the add_audio_stream() code, since they were bad-different for
no discernible reason.
2022-02-24 07:29:08 -05:00
|
|
|
if (!(oc->oformat->flags & AVFMT_NOFILE)) {
|
2012-07-25 17:20:02 -05:00
|
|
|
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) {
|
2022-01-12 01:08:31 -05: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.
|
2021-11-11 07:03:10 -05:00
|
|
|
void FFmpegWriter::WriteFrame(std::shared_ptr<openshot::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);
|
|
|
|
|
|
2022-01-12 01:08:31 -05:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"FFmpegWriter::WriteFrame",
|
|
|
|
|
"frame->number", frame->number,
|
|
|
|
|
"is_writing", is_writing);
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2024-02-10 20:28:47 -06:00
|
|
|
// Write frames to video file
|
|
|
|
|
write_frame(frame);
|
2012-08-28 15:53:18 -05:00
|
|
|
|
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.
|
2024-02-10 20:28:47 -06:00
|
|
|
void FFmpegWriter::write_frame(std::shared_ptr<Frame> frame) {
|
2012-08-28 15:53:18 -05:00
|
|
|
// Flip writing flag
|
|
|
|
|
is_writing = true;
|
|
|
|
|
|
2016-02-02 01:09:02 -06:00
|
|
|
// Create blank exception
|
|
|
|
|
bool has_error_encoding_video = false;
|
|
|
|
|
|
2024-02-10 20:28:47 -06:00
|
|
|
// Process audio frame
|
|
|
|
|
if (info.has_audio && audio_st)
|
|
|
|
|
write_audio_packets(false, frame);
|
2012-08-20 02:59:35 -05:00
|
|
|
|
2024-02-10 20:28:47 -06:00
|
|
|
// Process video frame
|
|
|
|
|
if (info.has_video && video_st)
|
|
|
|
|
process_video_packet(frame);
|
2012-08-20 02:59:35 -05:00
|
|
|
|
2024-02-10 20:28:47 -06:00
|
|
|
if (info.has_video && video_st) {
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Does this frame's AVFrame still exist
|
|
|
|
|
if (av_frames.count(frame)) {
|
|
|
|
|
// Get AVFrame
|
2024-02-10 20:28:47 -06:00
|
|
|
AVFrame *frame_final = av_frames[frame];
|
|
|
|
|
|
|
|
|
|
// Write frame to video file
|
|
|
|
|
if (!write_video_packet(frame, frame_final)) {
|
|
|
|
|
has_error_encoding_video = true;
|
|
|
|
|
}
|
2023-10-08 19:56:02 -05:00
|
|
|
|
|
|
|
|
// Deallocate buffer and AVFrame
|
2024-02-10 20:28:47 -06:00
|
|
|
av_freep(&(frame_final->data[0]));
|
|
|
|
|
AV_FREE_FRAME(&frame_final);
|
2023-10-08 19:56:02 -05:00
|
|
|
av_frames.erase(frame);
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
}
|
2023-07-18 18:21:57 -05:00
|
|
|
}
|
2012-08-20 02:59:35 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Done writing
|
|
|
|
|
is_writing = false;
|
2012-08-20 02:59:35 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -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) {
|
2022-01-12 01:08:31 -05: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() {
|
2023-10-08 19:56:02 -05:00
|
|
|
// Process final audio frame (if any)
|
|
|
|
|
if (info.has_audio && audio_st)
|
2024-02-10 20:28:47 -06:00
|
|
|
write_audio_packets(true, NULL);
|
2023-10-08 19:56:02 -05:00
|
|
|
|
2012-10-28 03:35:50 -05:00
|
|
|
// 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() {
|
2020-03-22 12:08:40 -04:00
|
|
|
if (info.has_audio && audio_codec_ctx && AV_GET_CODEC_TYPE(audio_st) == AVMEDIA_TYPE_AUDIO && AV_GET_CODEC_ATTRIBUTES(audio_st, audio_codec_ctx)->frame_size <= 1)
|
2018-03-28 15:09:55 -05:00
|
|
|
return;
|
2018-08-11 18:22:18 -05:00
|
|
|
#if (LIBAVFORMAT_VERSION_MAJOR < 58)
|
2020-03-04 23:58:16 -05:00
|
|
|
// FFmpeg < 4.0
|
2020-03-22 12:08:40 -04:00
|
|
|
if (info.has_video && video_codec_ctx && 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)
|
2018-03-28 15:09:55 -05:00
|
|
|
return;
|
2020-03-01 16:59:06 -08:00
|
|
|
#else
|
2020-07-08 17:39:04 -04:00
|
|
|
if (info.has_video && video_codec_ctx && AV_GET_CODEC_TYPE(video_st) == AVMEDIA_TYPE_VIDEO && AV_FIND_DECODER_CODEC_ID(video_st) == AV_CODEC_ID_RAWVIDEO)
|
2018-03-28 15:09:55 -05:00
|
|
|
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
|
|
|
// FLUSH VIDEO ENCODER
|
2022-03-16 23:58:06 -04:00
|
|
|
if (info.has_video) {
|
2012-10-28 03:35:50 -05:00
|
|
|
for (;;) {
|
|
|
|
|
|
|
|
|
|
// Increment PTS (in frames and scaled to the codec's timebase)
|
2022-03-16 23:58:06 -04:00
|
|
|
video_timestamp += av_rescale_q(1, av_make_q(info.fps.den, info.fps.num), video_codec_ctx->time_base);
|
2012-10-28 03:35:50 -05:00
|
|
|
|
2022-03-16 23:58:06 -04:00
|
|
|
#if IS_FFMPEG_3_2
|
|
|
|
|
AVPacket* pkt = av_packet_alloc();
|
|
|
|
|
#else
|
|
|
|
|
AVPacket* pkt;
|
|
|
|
|
av_init_packet(pkt);
|
|
|
|
|
#endif
|
|
|
|
|
pkt->data = NULL;
|
|
|
|
|
pkt->size = 0;
|
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
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Encode video packet (latest version of FFmpeg)
|
|
|
|
|
error_code = avcodec_send_frame(video_codec_ctx, NULL);
|
|
|
|
|
got_packet = 0;
|
|
|
|
|
while (error_code >= 0) {
|
|
|
|
|
error_code = avcodec_receive_packet(video_codec_ctx, pkt);
|
|
|
|
|
if (error_code == AVERROR(EAGAIN)|| error_code == AVERROR_EOF) {
|
|
|
|
|
got_packet = 0;
|
|
|
|
|
// Write packet
|
|
|
|
|
avcodec_flush_buffers(video_codec_ctx);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
av_packet_rescale_ts(pkt, video_codec_ctx->time_base, video_st->time_base);
|
|
|
|
|
pkt->stream_index = video_st->index;
|
|
|
|
|
error_code = av_interleaved_write_frame(oc, pkt);
|
|
|
|
|
}
|
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
|
|
|
// Encode video packet (older than FFmpeg 3.2)
|
2022-03-16 23:58:06 -04:00
|
|
|
error_code = avcodec_encode_video2(video_codec_ctx, pkt, NULL, &got_packet);
|
2012-11-17 03:13:00 -06:00
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
#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) {
|
2022-01-12 01:08:31 -05:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"FFmpegWriter::flush_encoders ERROR ["
|
|
|
|
|
+ av_err2string(error_code) + "]",
|
|
|
|
|
"error_code", error_code);
|
2012-10-28 03:35:50 -05:00
|
|
|
}
|
|
|
|
|
if (!got_packet) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// set the timestamp
|
2022-03-16 23:58:06 -04:00
|
|
|
av_packet_rescale_ts(pkt, video_codec_ctx->time_base, video_st->time_base);
|
|
|
|
|
pkt->stream_index = video_st->index;
|
2012-10-28 03:35:50 -05:00
|
|
|
|
|
|
|
|
// Write packet
|
2022-03-16 23:58:06 -04:00
|
|
|
error_code = av_interleaved_write_frame(oc, pkt);
|
2015-10-01 13:00:50 -05:00
|
|
|
if (error_code < 0) {
|
2022-01-12 01:08:31 -05:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"FFmpegWriter::flush_encoders ERROR ["
|
|
|
|
|
+ av_err2string(error_code) + "]",
|
|
|
|
|
"error_code", error_code);
|
2012-10-28 03:35:50 -05:00
|
|
|
}
|
|
|
|
|
}
|
2022-03-16 23:58:06 -04:00
|
|
|
}
|
2012-10-28 03:35:50 -05:00
|
|
|
|
2019-04-18 14:04:37 -05:00
|
|
|
// FLUSH AUDIO ENCODER
|
2021-06-25 15:57:52 -05:00
|
|
|
if (info.has_audio) {
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
for (;;) {
|
2022-03-16 23:58:06 -04:00
|
|
|
#if IS_FFMPEG_3_2
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
AVPacket* pkt = av_packet_alloc();
|
2022-03-16 23:58:06 -04:00
|
|
|
#else
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
AVPacket* pkt;
|
|
|
|
|
av_init_packet(pkt);
|
2022-03-16 23:58:06 -04:00
|
|
|
#endif
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
pkt->data = NULL;
|
|
|
|
|
pkt->size = 0;
|
|
|
|
|
pkt->pts = pkt->dts = audio_timestamp;
|
2012-10-28 03:35:50 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
/* encode the image */
|
|
|
|
|
int error_code = 0;
|
|
|
|
|
int got_packet = 0;
|
2019-04-18 14:04:37 -05:00
|
|
|
#if IS_FFMPEG_3_2
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
error_code = avcodec_send_frame(audio_codec_ctx, NULL);
|
2019-04-18 14:04:37 -05:00
|
|
|
#else
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
error_code = avcodec_encode_audio2(audio_codec_ctx, pkt, NULL, &got_packet);
|
2019-04-18 14:04:37 -05:00
|
|
|
#endif
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
if (error_code < 0) {
|
|
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"FFmpegWriter::flush_encoders ERROR ["
|
|
|
|
|
+ av_err2string(error_code) + "]",
|
|
|
|
|
"error_code", error_code);
|
|
|
|
|
}
|
|
|
|
|
if (!got_packet) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2012-10-28 03:35:50 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06: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 = audio_timestamp;
|
2012-10-28 03:35:50 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Scale the PTS to the audio stream timebase (which is sometimes different than the codec's timebase)
|
|
|
|
|
av_packet_rescale_ts(pkt, audio_codec_ctx->time_base, audio_st->time_base);
|
2012-10-28 03:35:50 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// set stream
|
|
|
|
|
pkt->stream_index = audio_st->index;
|
|
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2012-10-28 03:35:50 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Write packet
|
|
|
|
|
error_code = av_interleaved_write_frame(oc, pkt);
|
|
|
|
|
if (error_code < 0) {
|
|
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"FFmpegWriter::flush_encoders ERROR ["
|
|
|
|
|
+ av_err2string(error_code) + "]",
|
|
|
|
|
"error_code", error_code);
|
|
|
|
|
}
|
2015-06-01 00:20:14 -07:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Increment PTS by duration of packet
|
|
|
|
|
audio_timestamp += pkt->duration;
|
2021-06-26 17:24:15 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// deallocate memory for packet
|
|
|
|
|
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)
|
|
|
|
|
{
|
2021-06-04 21:32:29 -04:00
|
|
|
#if USE_HW_ACCEL
|
2020-02-10 01:50:31 -05:00
|
|
|
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
|
|
|
}
|
2021-06-04 21:32:29 -04:00
|
|
|
#endif // USE_HW_ACCEL
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
|
|
|
|
|
// Free any previous memory allocations
|
|
|
|
|
if (video_codec_ctx != nullptr) {
|
|
|
|
|
AV_FREE_CONTEXT(video_codec_ctx);
|
|
|
|
|
av_free(video_codec_ctx);
|
|
|
|
|
}
|
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;
|
|
|
|
|
}
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
|
|
|
|
|
// Free any previous memory allocations
|
|
|
|
|
if (audio_codec_ctx != nullptr) {
|
|
|
|
|
AV_FREE_CONTEXT(audio_codec_ctx);
|
|
|
|
|
av_free(audio_codec_ctx);
|
|
|
|
|
}
|
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();
|
|
|
|
|
|
FFmpegWriter: Macro & member cleanup
- The `fmt` class member, which was of type AVFormat*, was really
just an unnecessary copy of `(AVFormatContext*)oc->oformat`.
But we were ASSIGNING into its members, which we were definitely
not supposed to be doing. (And in recent FFmpegs, now that
`AVFormat` has been `const`d, we can't.) It's gone; now we just
use `oc->oformat` anywhere we used to access `fmt`.
- The preprocessor macro to allocate a new _stream_ was a mess of
cross purposes: It did allocate a stream, but then it also
allocated a new AvCodecCtx on newer FFmpeg releases. Worse (and
always galling to me), it proceeded to assign to a variable
that WASN'T passed in to the macro, just taking it on faith that
it would only be used where that variable was defined. That's
just... ugh. So I broke it apart into two steps (stream creation
and context allocation), realized the stream creation code was
the same for all ffmpeg versions and didn't need to be a macro
at all, and now a 4-parameter, 6-line magical macro has been
replaced with a simple, zero-side-effect one-liner.
- I also cleaned up the add_video_stream() code to be more like
the add_audio_stream() code, since they were bad-different for
no discernible reason.
2022-02-24 07:29:08 -05:00
|
|
|
if (!(oc->oformat->flags & AVFMT_NOFILE)) {
|
2012-07-25 17:20:02 -05:00
|
|
|
/* 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
|
2022-03-16 23:58:06 -04:00
|
|
|
video_timestamp = 0;
|
|
|
|
|
audio_timestamp = 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
|
|
|
// Find the audio codec
|
2021-11-21 23:25:37 -05:00
|
|
|
const 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);
|
|
|
|
|
|
2020-03-22 12:19:11 -04:00
|
|
|
// Free any previous memory allocations
|
FFmpegWriter: Macro & member cleanup
- The `fmt` class member, which was of type AVFormat*, was really
just an unnecessary copy of `(AVFormatContext*)oc->oformat`.
But we were ASSIGNING into its members, which we were definitely
not supposed to be doing. (And in recent FFmpegs, now that
`AVFormat` has been `const`d, we can't.) It's gone; now we just
use `oc->oformat` anywhere we used to access `fmt`.
- The preprocessor macro to allocate a new _stream_ was a mess of
cross purposes: It did allocate a stream, but then it also
allocated a new AvCodecCtx on newer FFmpeg releases. Worse (and
always galling to me), it proceeded to assign to a variable
that WASN'T passed in to the macro, just taking it on faith that
it would only be used where that variable was defined. That's
just... ugh. So I broke it apart into two steps (stream creation
and context allocation), realized the stream creation code was
the same for all ffmpeg versions and didn't need to be a macro
at all, and now a 4-parameter, 6-line magical macro has been
replaced with a simple, zero-side-effect one-liner.
- I also cleaned up the add_video_stream() code to be more like
the add_audio_stream() code, since they were bad-different for
no discernible reason.
2022-02-24 07:29:08 -05:00
|
|
|
if (audio_codec_ctx != nullptr) {
|
2020-03-22 12:19:11 -04:00
|
|
|
AV_FREE_CONTEXT(audio_codec_ctx);
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-24 12:50:17 -05:00
|
|
|
// Create a new audio stream
|
FFmpegWriter: Macro & member cleanup
- The `fmt` class member, which was of type AVFormat*, was really
just an unnecessary copy of `(AVFormatContext*)oc->oformat`.
But we were ASSIGNING into its members, which we were definitely
not supposed to be doing. (And in recent FFmpegs, now that
`AVFormat` has been `const`d, we can't.) It's gone; now we just
use `oc->oformat` anywhere we used to access `fmt`.
- The preprocessor macro to allocate a new _stream_ was a mess of
cross purposes: It did allocate a stream, but then it also
allocated a new AvCodecCtx on newer FFmpeg releases. Worse (and
always galling to me), it proceeded to assign to a variable
that WASN'T passed in to the macro, just taking it on faith that
it would only be used where that variable was defined. That's
just... ugh. So I broke it apart into two steps (stream creation
and context allocation), realized the stream creation code was
the same for all ffmpeg versions and didn't need to be a macro
at all, and now a 4-parameter, 6-line magical macro has been
replaced with a simple, zero-side-effect one-liner.
- I also cleaned up the add_video_stream() code to be more like
the add_audio_stream() code, since they were bad-different for
no discernible reason.
2022-02-24 07:29:08 -05:00
|
|
|
AVStream* st = avformat_new_stream(oc, codec);
|
|
|
|
|
if (!st)
|
|
|
|
|
throw OutOfMemory("Could not allocate memory for the video stream.", path);
|
|
|
|
|
|
|
|
|
|
// Allocate a new codec context for the stream
|
|
|
|
|
ALLOC_CODEC_CTX(audio_codec_ctx, codec, st)
|
|
|
|
|
#if (LIBAVFORMAT_VERSION_MAJOR >= 58)
|
|
|
|
|
st->codecpar->codec_id = codec->id;
|
|
|
|
|
#endif
|
|
|
|
|
AVCodecContext* c = audio_codec_ctx;
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
c->codec_id = codec->id;
|
|
|
|
|
c->codec_type = AVMEDIA_TYPE_AUDIO;
|
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;
|
2024-05-08 12:16:41 +02:00
|
|
|
#if !HAVE_CH_LAYOUT
|
2012-07-25 17:20:02 -05:00
|
|
|
c->channels = info.channels;
|
2024-05-08 12:16:41 +02:00
|
|
|
#endif
|
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;
|
|
|
|
|
|
2024-05-10 17:51:16 -05:00
|
|
|
auto channel_layout = info.channel_layout;
|
2024-05-08 12:16:41 +02:00
|
|
|
#if HAVE_CH_LAYOUT
|
|
|
|
|
// Set a valid number of channels (or throw error)
|
|
|
|
|
AVChannelLayout ch_layout;
|
|
|
|
|
av_channel_layout_from_mask(&ch_layout, info.channel_layout);
|
|
|
|
|
if (codec->ch_layouts) {
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; av_channel_layout_check(&codec->ch_layouts[i]); i++)
|
|
|
|
|
if (av_channel_layout_compare(&ch_layout, &codec->ch_layouts[i])) {
|
|
|
|
|
// Set valid channel layout
|
|
|
|
|
av_channel_layout_copy(&c->ch_layout, &ch_layout);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (!av_channel_layout_check(&codec->ch_layouts[i]))
|
|
|
|
|
throw InvalidChannels("An invalid channel layout was detected (i.e. MONO / STEREO).", path);
|
|
|
|
|
} else
|
|
|
|
|
// Set valid channel layout
|
|
|
|
|
av_channel_layout_copy(&c->ch_layout, &ch_layout);
|
|
|
|
|
#else
|
2012-07-27 17:44:18 -05:00
|
|
|
// Set a valid number of channels (or throw error)
|
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);
|
2024-05-10 17:43:08 -05:00
|
|
|
} else
|
|
|
|
|
// Set valid channel layout
|
|
|
|
|
c->channel_layout = channel_layout;
|
2024-05-08 12:16:41 +02:00
|
|
|
#endif
|
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)
|
2020-03-04 23:58:16 -05:00
|
|
|
// FFmpeg 3.0+
|
2018-08-11 18:22:18 -05:00
|
|
|
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);
|
2022-01-12 01:08:31 -05:00
|
|
|
|
2024-05-08 12:16:41 +02:00
|
|
|
int nb_channels;
|
|
|
|
|
const char* nb_channels_label;
|
|
|
|
|
const char* channel_layout_label;
|
|
|
|
|
|
|
|
|
|
#if HAVE_CH_LAYOUT
|
|
|
|
|
nb_channels = c->ch_layout.nb_channels;
|
|
|
|
|
channel_layout = c->ch_layout.u.mask;
|
|
|
|
|
nb_channels_label = "c->ch_layout.nb_channels";
|
|
|
|
|
channel_layout_label = "c->ch_layout.u.mask";
|
|
|
|
|
#else
|
|
|
|
|
nb_channels = c->channels;
|
|
|
|
|
channel_layout = c->channel_layout;
|
|
|
|
|
nb_channels_label = "c->channels";
|
|
|
|
|
channel_layout_label = "c->channel_layout";
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-01-12 01:08:31 -05:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"FFmpegWriter::add_audio_stream",
|
|
|
|
|
"c->codec_id", c->codec_id,
|
|
|
|
|
"c->bit_rate", c->bit_rate,
|
2024-05-08 12:16:41 +02:00
|
|
|
nb_channels_label, nb_channels,
|
2022-01-12 01:08:31 -05:00
|
|
|
"c->sample_fmt", c->sample_fmt,
|
2024-05-08 12:16:41 +02:00
|
|
|
channel_layout_label, channel_layout,
|
2022-01-12 01:08:31 -05:00
|
|
|
"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() {
|
2018-03-28 15:09:55 -05:00
|
|
|
// Find the video codec
|
2021-11-21 23:25:37 -05:00
|
|
|
const 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);
|
|
|
|
|
|
FFmpegWriter: Macro & member cleanup
- The `fmt` class member, which was of type AVFormat*, was really
just an unnecessary copy of `(AVFormatContext*)oc->oformat`.
But we were ASSIGNING into its members, which we were definitely
not supposed to be doing. (And in recent FFmpegs, now that
`AVFormat` has been `const`d, we can't.) It's gone; now we just
use `oc->oformat` anywhere we used to access `fmt`.
- The preprocessor macro to allocate a new _stream_ was a mess of
cross purposes: It did allocate a stream, but then it also
allocated a new AvCodecCtx on newer FFmpeg releases. Worse (and
always galling to me), it proceeded to assign to a variable
that WASN'T passed in to the macro, just taking it on faith that
it would only be used where that variable was defined. That's
just... ugh. So I broke it apart into two steps (stream creation
and context allocation), realized the stream creation code was
the same for all ffmpeg versions and didn't need to be a macro
at all, and now a 4-parameter, 6-line magical macro has been
replaced with a simple, zero-side-effect one-liner.
- I also cleaned up the add_video_stream() code to be more like
the add_audio_stream() code, since they were bad-different for
no discernible reason.
2022-02-24 07:29:08 -05:00
|
|
|
// Free any previous memory allocations
|
|
|
|
|
if (video_codec_ctx != nullptr) {
|
|
|
|
|
AV_FREE_CONTEXT(video_codec_ctx);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-28 15:09:55 -05:00
|
|
|
// Create a new video stream
|
FFmpegWriter: Macro & member cleanup
- The `fmt` class member, which was of type AVFormat*, was really
just an unnecessary copy of `(AVFormatContext*)oc->oformat`.
But we were ASSIGNING into its members, which we were definitely
not supposed to be doing. (And in recent FFmpegs, now that
`AVFormat` has been `const`d, we can't.) It's gone; now we just
use `oc->oformat` anywhere we used to access `fmt`.
- The preprocessor macro to allocate a new _stream_ was a mess of
cross purposes: It did allocate a stream, but then it also
allocated a new AvCodecCtx on newer FFmpeg releases. Worse (and
always galling to me), it proceeded to assign to a variable
that WASN'T passed in to the macro, just taking it on faith that
it would only be used where that variable was defined. That's
just... ugh. So I broke it apart into two steps (stream creation
and context allocation), realized the stream creation code was
the same for all ffmpeg versions and didn't need to be a macro
at all, and now a 4-parameter, 6-line magical macro has been
replaced with a simple, zero-side-effect one-liner.
- I also cleaned up the add_video_stream() code to be more like
the add_audio_stream() code, since they were bad-different for
no discernible reason.
2022-02-24 07:29:08 -05:00
|
|
|
AVStream* st = avformat_new_stream(oc, codec);
|
|
|
|
|
if (!st)
|
|
|
|
|
throw OutOfMemory("Could not allocate memory for the video stream.", path);
|
|
|
|
|
|
|
|
|
|
// Allocate a new codec context for the stream
|
|
|
|
|
ALLOC_CODEC_CTX(video_codec_ctx, codec, st)
|
|
|
|
|
#if (LIBAVFORMAT_VERSION_MAJOR >= 58)
|
|
|
|
|
st->codecpar->codec_id = codec->id;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
AVCodecContext* c = video_codec_ctx;
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
c->codec_id = codec->id;
|
|
|
|
|
c->codec_type = AVMEDIA_TYPE_VIDEO;
|
2012-07-19 15:03:55 -05:00
|
|
|
|
2021-10-07 13:45:16 -05:00
|
|
|
// Set sample aspect ratio
|
FFmpegWriter: Macro & member cleanup
- The `fmt` class member, which was of type AVFormat*, was really
just an unnecessary copy of `(AVFormatContext*)oc->oformat`.
But we were ASSIGNING into its members, which we were definitely
not supposed to be doing. (And in recent FFmpegs, now that
`AVFormat` has been `const`d, we can't.) It's gone; now we just
use `oc->oformat` anywhere we used to access `fmt`.
- The preprocessor macro to allocate a new _stream_ was a mess of
cross purposes: It did allocate a stream, but then it also
allocated a new AvCodecCtx on newer FFmpeg releases. Worse (and
always galling to me), it proceeded to assign to a variable
that WASN'T passed in to the macro, just taking it on faith that
it would only be used where that variable was defined. That's
just... ugh. So I broke it apart into two steps (stream creation
and context allocation), realized the stream creation code was
the same for all ffmpeg versions and didn't need to be a macro
at all, and now a 4-parameter, 6-line magical macro has been
replaced with a simple, zero-side-effect one-liner.
- I also cleaned up the add_video_stream() code to be more like
the add_audio_stream() code, since they were bad-different for
no discernible reason.
2022-02-24 07:29:08 -05:00
|
|
|
c->sample_aspect_ratio.num = info.pixel_ratio.num;
|
|
|
|
|
c->sample_aspect_ratio.den = info.pixel_ratio.den;
|
2021-10-07 13:45:16 -05:00
|
|
|
|
2015-08-24 23:49:45 -05:00
|
|
|
/* Init video encoder options */
|
2020-06-15 14:41:38 -07:00
|
|
|
if (info.video_bit_rate >= 1000
|
2020-06-04 08:28:33 -07:00
|
|
|
#if (LIBAVCODEC_VERSION_MAJOR >= 58)
|
|
|
|
|
&& c->codec_id != AV_CODEC_ID_AV1
|
2020-06-15 14:41:38 -07:00
|
|
|
#endif
|
2020-06-04 08:28:33 -07:00
|
|
|
) {
|
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) {
|
2020-07-07 10:14:02 -07:00
|
|
|
if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
|
2020-07-07 10:20:48 -07:00
|
|
|
c->qmin = 2;
|
2020-07-07 10:14:02 -07:00
|
|
|
c->qmax = 30;
|
|
|
|
|
}
|
2019-03-08 14:41:40 -08:00
|
|
|
}
|
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_MAJOR >= 58)
|
2020-03-04 23:58:16 -05:00
|
|
|
// FFmpeg 4.0+
|
2019-03-10 13:09:47 -07:00
|
|
|
case AV_CODEC_ID_AV1 :
|
2020-06-15 14:33:14 -07:00
|
|
|
// TODO: Set `crf` or `qp` according to bitrate, as bitrate is not supported by these encoders yet.
|
2020-06-15 14:41:38 -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
|
|
|
|
|
}
|
2020-09-13 08:42:17 -07:00
|
|
|
if (strstr(info.vcodec.c_str(), "svtav1") != NULL) {
|
2020-06-15 14:41:38 -07:00
|
|
|
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
|
|
|
|
|
// TODO: Allow user to chose their own number of tiles
|
|
|
|
|
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-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
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06: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;
|
2020-03-04 23:58:16 -05:00
|
|
|
// AVCodecContext->framerate was added in FFmpeg 2.6
|
2019-12-11 05:42:41 -05:00
|
|
|
#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;
|
|
|
|
|
|
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)
|
2020-03-04 23:58:16 -05:00
|
|
|
// FFmpeg 3.0+
|
2018-08-11 18:22:18 -05:00
|
|
|
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) {
|
FFmpegWriter: Macro & member cleanup
- The `fmt` class member, which was of type AVFormat*, was really
just an unnecessary copy of `(AVFormatContext*)oc->oformat`.
But we were ASSIGNING into its members, which we were definitely
not supposed to be doing. (And in recent FFmpegs, now that
`AVFormat` has been `const`d, we can't.) It's gone; now we just
use `oc->oformat` anywhere we used to access `fmt`.
- The preprocessor macro to allocate a new _stream_ was a mess of
cross purposes: It did allocate a stream, but then it also
allocated a new AvCodecCtx on newer FFmpeg releases. Worse (and
always galling to me), it proceeded to assign to a variable
that WASN'T passed in to the macro, just taking it on faith that
it would only be used where that variable was defined. That's
just... ugh. So I broke it apart into two steps (stream creation
and context allocation), realized the stream creation code was
the same for all ffmpeg versions and didn't need to be a macro
at all, and now a 4-parameter, 6-line magical macro has been
replaced with a simple, zero-side-effect one-liner.
- I also cleaned up the add_video_stream() code to be more like
the add_audio_stream() code, since they were bad-different for
no discernible reason.
2022-02-24 07:29:08 -05:00
|
|
|
if (oc->oformat->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)
|
2020-03-04 23:58:16 -05:00
|
|
|
// FFmpeg < 4.0
|
FFmpegWriter: Macro & member cleanup
- The `fmt` class member, which was of type AVFormat*, was really
just an unnecessary copy of `(AVFormatContext*)oc->oformat`.
But we were ASSIGNING into its members, which we were definitely
not supposed to be doing. (And in recent FFmpegs, now that
`AVFormat` has been `const`d, we can't.) It's gone; now we just
use `oc->oformat` anywhere we used to access `fmt`.
- The preprocessor macro to allocate a new _stream_ was a mess of
cross purposes: It did allocate a stream, but then it also
allocated a new AvCodecCtx on newer FFmpeg releases. Worse (and
always galling to me), it proceeded to assign to a variable
that WASN'T passed in to the macro, just taking it on faith that
it would only be used where that variable was defined. That's
just... ugh. So I broke it apart into two steps (stream creation
and context allocation), realized the stream creation code was
the same for all ffmpeg versions and didn't need to be a macro
at all, and now a 4-parameter, 6-line magical macro has been
replaced with a simple, zero-side-effect one-liner.
- I also cleaned up the add_video_stream() code to be more like
the add_audio_stream() code, since they were bad-different for
no discernible reason.
2022-02-24 07:29:08 -05:00
|
|
|
if (strcmp(oc->oformat->name, "gif") != 0)
|
2019-01-29 12:38:52 -08:00
|
|
|
// 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);
|
2022-01-12 01:08:31 -05:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"FFmpegWriter::add_video_stream ("
|
FFmpegWriter: Macro & member cleanup
- The `fmt` class member, which was of type AVFormat*, was really
just an unnecessary copy of `(AVFormatContext*)oc->oformat`.
But we were ASSIGNING into its members, which we were definitely
not supposed to be doing. (And in recent FFmpegs, now that
`AVFormat` has been `const`d, we can't.) It's gone; now we just
use `oc->oformat` anywhere we used to access `fmt`.
- The preprocessor macro to allocate a new _stream_ was a mess of
cross purposes: It did allocate a stream, but then it also
allocated a new AvCodecCtx on newer FFmpeg releases. Worse (and
always galling to me), it proceeded to assign to a variable
that WASN'T passed in to the macro, just taking it on faith that
it would only be used where that variable was defined. That's
just... ugh. So I broke it apart into two steps (stream creation
and context allocation), realized the stream creation code was
the same for all ffmpeg versions and didn't need to be a macro
at all, and now a 4-parameter, 6-line magical macro has been
replaced with a simple, zero-side-effect one-liner.
- I also cleaned up the add_video_stream() code to be more like
the add_audio_stream() code, since they were bad-different for
no discernible reason.
2022-02-24 07:29:08 -05:00
|
|
|
+ (std::string)oc->oformat->name + " : "
|
2022-01-12 01:08:31 -05:00
|
|
|
+ (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);
|
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) {
|
2021-11-21 23:25:37 -05:00
|
|
|
const AVCodec *codec;
|
2020-03-22 12:08:40 -04:00
|
|
|
AV_GET_CODEC_FROM_STREAM(st, audio_codec_ctx)
|
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)
|
2020-03-22 12:08:40 -04:00
|
|
|
audio_codec_ctx->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)
|
2020-03-22 12:08:40 -04:00
|
|
|
codec = avcodec_find_encoder(audio_codec_ctx->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
|
2020-03-22 12:08:40 -04:00
|
|
|
if (avcodec_open2(audio_codec_ctx, codec, &opts) < 0)
|
2019-08-23 19:46:05 +02:00
|
|
|
throw InvalidCodec("Could not open audio codec", path);
|
2020-03-22 12:08:40 -04:00
|
|
|
AV_COPY_PARAMS_FROM_CONTEXT(st, audio_codec_ctx);
|
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
|
2020-03-22 12:08:40 -04:00
|
|
|
if (audio_codec_ctx->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
|
2020-03-22 12:08:40 -04:00
|
|
|
audio_input_frame_size = audio_codec_ctx->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);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 01:08:31 -05:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"FFmpegWriter::open_audio",
|
|
|
|
|
"audio_codec_ctx->thread_count", audio_codec_ctx->thread_count,
|
|
|
|
|
"audio_input_frame_size", audio_input_frame_size,
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
"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) {
|
2021-11-21 23:25:37 -05:00
|
|
|
const AVCodec *codec;
|
2020-03-22 12:08:40 -04:00
|
|
|
AV_GET_CODEC_FROM_STREAM(st, video_codec_ctx)
|
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)
|
2020-03-22 12:08:40 -04:00
|
|
|
video_codec_ctx->thread_count = std::min(FF_NUM_PROCESSORS, 16);
|
2012-08-28 22:53:12 -05:00
|
|
|
|
2021-06-04 21:32:29 -04:00
|
|
|
#if USE_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;
|
2020-03-04 23:52:42 -05:00
|
|
|
std::clog << "Encoding Device Nr: " << adapter_num << "\n";
|
2019-01-30 20:44:36 -08:00
|
|
|
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;
|
2020-03-04 23:58:16 -05:00
|
|
|
#elif defined(_WIN32) || 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 ) {
|
2020-03-04 23:58:16 -05:00
|
|
|
#elif defined(_WIN32) || defined(__APPLE__)
|
2019-01-29 12:38:52 -08:00
|
|
|
if( adapter_ptr != NULL ) {
|
2019-04-18 14:04:37 -05:00
|
|
|
#endif
|
2022-01-12 01:08:31 -05:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"Encode Device present using device",
|
|
|
|
|
"adapter", adapter_num);
|
2019-01-30 20:44:36 -08:00
|
|
|
}
|
|
|
|
|
else {
|
2022-01-12 01:08:31 -05:00
|
|
|
adapter_ptr = NULL; // use default
|
|
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"Encode Device not present, using default");
|
|
|
|
|
}
|
|
|
|
|
if (av_hwdevice_ctx_create(&hw_device_ctx,
|
|
|
|
|
hw_en_av_device_type, adapter_ptr, NULL, 0) < 0)
|
|
|
|
|
{
|
|
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"FFmpegWriter::open_video ERROR creating hwdevice, Codec name:",
|
|
|
|
|
info.vcodec.c_str(), -1);
|
|
|
|
|
throw InvalidCodec("Could not create hwdevice", path);
|
2019-01-29 12:38:52 -08:00
|
|
|
}
|
|
|
|
|
}
|
2021-06-04 21:32:29 -04:00
|
|
|
#endif // USE_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 */
|
2020-03-22 12:08:40 -04:00
|
|
|
if (video_codec_ctx->max_b_frames && video_codec_ctx->codec_id != AV_CODEC_ID_MPEG4 && video_codec_ctx->codec_id != AV_CODEC_ID_MPEG1VIDEO && video_codec_ctx->codec_id != AV_CODEC_ID_MPEG2VIDEO)
|
|
|
|
|
video_codec_ctx->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);
|
|
|
|
|
|
2021-06-04 21:32:29 -04:00
|
|
|
#if USE_HW_ACCEL
|
2019-01-29 12:38:52 -08:00
|
|
|
if (hw_en_on && hw_en_supported) {
|
2020-03-22 12:08:40 -04:00
|
|
|
video_codec_ctx->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;
|
2020-03-22 12:08:40 -04:00
|
|
|
if (av_opt_get_int(video_codec_ctx->priv_data, "qp", 0, &qp) != 0 || qp == 0) {
|
2019-08-23 19:46:05 +02:00
|
|
|
// unless "qp" was set for CQP, switch to VBR RC mode
|
2020-03-22 12:08:40 -04:00
|
|
|
av_opt_set(video_codec_ctx->priv_data, "rc_mode", "VBR", 0);
|
2019-08-23 19:46:05 +02:00
|
|
|
|
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).
|
2020-03-22 12:08:40 -04:00
|
|
|
video_codec_ctx->rc_max_rate = video_codec_ctx->bit_rate;
|
2019-08-23 19:46:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 12:08:40 -04:00
|
|
|
switch (video_codec_ctx->codec_id) {
|
2019-08-23 19:46:05 +02:00
|
|
|
case AV_CODEC_ID_H264:
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
video_codec_ctx->max_b_frames = 0; // At least this GPU doesn't support b-frames
|
2020-03-22 12:08:40 -04:00
|
|
|
video_codec_ctx->profile = FF_PROFILE_H264_BASELINE | FF_PROFILE_H264_CONSTRAINED;
|
|
|
|
|
av_opt_set(video_codec_ctx->priv_data, "preset", "slow", 0);
|
|
|
|
|
av_opt_set(video_codec_ctx->priv_data, "tune", "zerolatency", 0);
|
|
|
|
|
av_opt_set(video_codec_ctx->priv_data, "vprofile", "baseline", AV_OPT_SEARCH_CHILDREN);
|
2019-08-23 19:46:05 +02:00
|
|
|
break;
|
|
|
|
|
case AV_CODEC_ID_HEVC:
|
|
|
|
|
// tested to work with defaults
|
|
|
|
|
break;
|
|
|
|
|
case AV_CODEC_ID_VP9:
|
|
|
|
|
// tested to work with defaults
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2022-01-12 01:08:31 -05:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"No codec-specific options defined for this codec. HW encoding may fail",
|
2020-03-22 12:08:40 -04:00
|
|
|
"codec_id", video_codec_ctx->codec_id);
|
2019-08-23 19:46:05 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-29 12:38:52 -08:00
|
|
|
// set hw_frames_ctx for encoder's AVCodecContext
|
|
|
|
|
int err;
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
if ((err = set_hwframe_ctx(video_codec_ctx, hw_device_ctx, info.width, info.height)) < 0)
|
2022-01-12 01:08:31 -05:00
|
|
|
{
|
|
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"FFmpegWriter::open_video (set_hwframe_ctx) ERROR faled to set hwframe context",
|
|
|
|
|
"width", info.width,
|
|
|
|
|
"height", info.height,
|
|
|
|
|
av_err2string(err), -1);
|
2019-01-29 12:38:52 -08:00
|
|
|
}
|
|
|
|
|
}
|
2021-06-04 21:32:29 -04:00
|
|
|
#endif // USE_HW_ACCEL
|
2018-08-31 21:36:23 -07:00
|
|
|
|
2012-07-25 17:20:02 -05:00
|
|
|
/* open the codec */
|
2020-03-22 12:08:40 -04:00
|
|
|
if (avcodec_open2(video_codec_ctx, codec, &opts) < 0)
|
2019-08-23 19:46:05 +02:00
|
|
|
throw InvalidCodec("Could not open video codec", path);
|
2020-03-22 12:08:40 -04:00
|
|
|
AV_COPY_PARAMS_FROM_CONTEXT(st, video_codec_ctx);
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 01:08:31 -05:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"FFmpegWriter::open_video",
|
|
|
|
|
"video_codec_ctx->thread_count", video_codec_ctx->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
|
2024-02-10 20:28:47 -06:00
|
|
|
void FFmpegWriter::write_audio_packets(bool is_final, std::shared_ptr<openshot::Frame> frame) {
|
|
|
|
|
if (!frame && !is_final)
|
|
|
|
|
return;
|
|
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06: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;
|
|
|
|
|
ChannelLayout channel_layout_in_frame = LAYOUT_MONO; // default channel layout
|
2012-07-24 12:50:17 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Create a new array (to hold all S16 audio samples, for the current queued frames
|
2024-02-10 20:28:47 -06:00
|
|
|
unsigned int all_queued_samples_size = sizeof(int16_t) * AVCODEC_MAX_AUDIO_FRAME_SIZE;
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
int16_t *all_queued_samples = (int16_t *) av_malloc(all_queued_samples_size);
|
|
|
|
|
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
|
|
|
|
2024-02-10 20:28:47 -06:00
|
|
|
// Get audio sample array
|
|
|
|
|
float *frame_samples_float = NULL;
|
2023-10-08 19:56:02 -05:00
|
|
|
|
2024-02-10 20:28:47 -06:00
|
|
|
// Get the audio details from this frame
|
|
|
|
|
if (frame) {
|
2023-10-08 19:56:02 -05:00
|
|
|
sample_rate_in_frame = frame->SampleRate();
|
|
|
|
|
samples_in_frame = frame->GetAudioSamplesCount();
|
|
|
|
|
channels_in_frame = frame->GetAudioChannelsCount();
|
|
|
|
|
channel_layout_in_frame = frame->ChannelsLayout();
|
|
|
|
|
|
|
|
|
|
// Get samples interleaved together (c1 c2 c1 c2 c1 c2)
|
|
|
|
|
frame_samples_float = frame->GetInterleavedAudioSamples(&samples_in_frame);
|
2024-02-10 20:28:47 -06:00
|
|
|
}
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2024-02-10 20:28:47 -06:00
|
|
|
// Calculate total samples
|
|
|
|
|
total_frame_samples = samples_in_frame * channels_in_frame;
|
2012-08-20 02:59:35 -05:00
|
|
|
|
2024-02-10 20:28:47 -06:00
|
|
|
// Translate audio sample values back to 16 bit integers with saturation
|
|
|
|
|
const int16_t max16 = 32767;
|
|
|
|
|
const int16_t min16 = -32768;
|
|
|
|
|
for (int s = 0; s < total_frame_samples; s++, frame_position++) {
|
|
|
|
|
float valF = frame_samples_float[s] * (1 << 15);
|
|
|
|
|
int16_t conv;
|
|
|
|
|
if (valF > max16) {
|
|
|
|
|
conv = max16;
|
|
|
|
|
} else if (valF < min16) {
|
|
|
|
|
conv = min16;
|
|
|
|
|
} else {
|
|
|
|
|
conv = int(valF + 32768.5) - 32768; // +0.5 is for rounding
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
}
|
2015-02-05 00:06:07 -06:00
|
|
|
|
2024-02-10 20:28:47 -06:00
|
|
|
// Copy into buffer
|
|
|
|
|
all_queued_samples[frame_position] = conv;
|
|
|
|
|
}
|
2015-03-01 22:36:39 -06:00
|
|
|
|
2024-02-10 20:28:47 -06:00
|
|
|
// Deallocate float array
|
|
|
|
|
delete[] frame_samples_float;
|
2015-03-04 21:33:29 -06:00
|
|
|
|
|
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// 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;
|
2015-08-02 20:32:33 -05:00
|
|
|
|
2015-02-05 00:06:07 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06: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-10-27 21:36:08 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Keep track of the original sample format
|
|
|
|
|
AVSampleFormat output_sample_fmt = audio_codec_ctx->sample_fmt;
|
2012-08-20 02:59:35 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
AVFrame *audio_frame = NULL;
|
|
|
|
|
if (!is_final) {
|
|
|
|
|
// Create input frame (and allocate arrays)
|
|
|
|
|
audio_frame = AV_ALLOCATE_FRAME();
|
|
|
|
|
AV_RESET_FRAME(audio_frame);
|
|
|
|
|
audio_frame->nb_samples = total_frame_samples / channels_in_frame;
|
2012-10-26 17:15:17 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Fill input frame with sample data
|
|
|
|
|
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 ["
|
|
|
|
|
+ av_err2string(error_code) + "]",
|
|
|
|
|
"error_code", error_code);
|
|
|
|
|
}
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Do not convert audio to planar format (yet). We need to keep everything interleaved at this point.
|
|
|
|
|
switch (audio_codec_ctx->sample_fmt) {
|
|
|
|
|
case AV_SAMPLE_FMT_FLTP: {
|
|
|
|
|
output_sample_fmt = AV_SAMPLE_FMT_FLT;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AV_SAMPLE_FMT_S32P: {
|
|
|
|
|
output_sample_fmt = AV_SAMPLE_FMT_S32;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AV_SAMPLE_FMT_S16P: {
|
|
|
|
|
output_sample_fmt = AV_SAMPLE_FMT_S16;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case AV_SAMPLE_FMT_U8P: {
|
|
|
|
|
output_sample_fmt = AV_SAMPLE_FMT_U8;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: {
|
|
|
|
|
// This is only here to silence unused-enum warnings
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -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
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Create output frame (and allocate arrays)
|
|
|
|
|
AVFrame *audio_converted = AV_ALLOCATE_FRAME();
|
|
|
|
|
AV_RESET_FRAME(audio_converted);
|
|
|
|
|
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);
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06: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);
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// setup resample context
|
|
|
|
|
if (!avr) {
|
|
|
|
|
avr = SWR_ALLOC();
|
2024-05-08 12:16:41 +02:00
|
|
|
#if HAVE_CH_LAYOUT
|
|
|
|
|
AVChannelLayout in_chlayout;
|
|
|
|
|
AVChannelLayout out_chlayout;
|
|
|
|
|
av_channel_layout_from_mask(&in_chlayout, channel_layout_in_frame);
|
|
|
|
|
av_channel_layout_from_mask(&out_chlayout, info.channel_layout);
|
|
|
|
|
av_opt_set_chlayout(avr, "in_chlayout", &in_chlayout, 0);
|
|
|
|
|
av_opt_set_chlayout(avr, "out_chlayout", &out_chlayout, 0);
|
|
|
|
|
#else
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
av_opt_set_int(avr, "in_channel_layout", channel_layout_in_frame, 0);
|
|
|
|
|
av_opt_set_int(avr, "out_channel_layout", info.channel_layout, 0);
|
2024-05-08 12:16:41 +02:00
|
|
|
av_opt_set_int(avr, "in_channels", channels_in_frame, 0);
|
|
|
|
|
av_opt_set_int(avr, "out_channels", info.channels, 0);
|
|
|
|
|
#endif
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06: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);
|
|
|
|
|
SWR_INIT(avr);
|
|
|
|
|
}
|
|
|
|
|
// Convert audio samples
|
|
|
|
|
int 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
|
|
|
|
|
);
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Set remaining samples
|
|
|
|
|
remaining_frame_samples = total_frame_samples;
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Create a new array (to hold all resampled S16 audio samples)
|
|
|
|
|
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) )
|
|
|
|
|
);
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Copy audio samples over original samples
|
|
|
|
|
memcpy(all_resampled_samples, audio_converted->data[0],
|
|
|
|
|
static_cast<size_t>(nb_samples)
|
|
|
|
|
* info.channels
|
|
|
|
|
* av_get_bytes_per_sample(output_sample_fmt));
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Remove converted audio
|
|
|
|
|
av_freep(&(audio_frame->data[0]));
|
|
|
|
|
AV_FREE_FRAME(&audio_frame);
|
|
|
|
|
av_freep(&audio_converted->data[0]);
|
|
|
|
|
AV_FREE_FRAME(&audio_converted);
|
|
|
|
|
all_queued_samples = NULL; // this array cleared with above call
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"FFmpegWriter::write_audio_packets (Successfully completed 1st resampling)",
|
|
|
|
|
"nb_samples", nb_samples,
|
|
|
|
|
"remaining_frame_samples", remaining_frame_samples);
|
|
|
|
|
}
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Loop until no more samples
|
|
|
|
|
while (remaining_frame_samples > 0 || is_final) {
|
|
|
|
|
// Get remaining samples needed for this packet
|
|
|
|
|
int remaining_packet_samples = (audio_input_frame_size * info.channels) - audio_input_position;
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Determine how many samples we need
|
|
|
|
|
int diff = 0;
|
|
|
|
|
if (remaining_frame_samples >= remaining_packet_samples) {
|
|
|
|
|
diff = remaining_packet_samples;
|
|
|
|
|
} else {
|
|
|
|
|
diff = remaining_frame_samples;
|
|
|
|
|
}
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Copy frame samples into the packet samples array
|
|
|
|
|
if (!is_final)
|
|
|
|
|
//TODO: Make this more sane
|
|
|
|
|
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,
|
|
|
|
|
static_cast<size_t>(diff)
|
|
|
|
|
* av_get_bytes_per_sample(output_sample_fmt)
|
|
|
|
|
);
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Increment counters
|
|
|
|
|
audio_input_position += diff;
|
|
|
|
|
samples_position += diff * (av_get_bytes_per_sample(output_sample_fmt) / av_get_bytes_per_sample(AV_SAMPLE_FMT_S16));
|
|
|
|
|
remaining_frame_samples -= diff;
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Do we have enough samples to proceed?
|
|
|
|
|
if (audio_input_position < (audio_input_frame_size * info.channels) && !is_final)
|
|
|
|
|
// Not enough samples to encode... so wait until the next frame
|
|
|
|
|
break;
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Convert to planar (if needed by audio codec)
|
|
|
|
|
AVFrame *frame_final = AV_ALLOCATE_FRAME();
|
|
|
|
|
AV_RESET_FRAME(frame_final);
|
|
|
|
|
if (av_sample_fmt_is_planar(audio_codec_ctx->sample_fmt)) {
|
|
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"FFmpegWriter::write_audio_packets (2nd resampling for Planar formats)",
|
|
|
|
|
"in_sample_fmt", output_sample_fmt,
|
|
|
|
|
"out_sample_fmt", audio_codec_ctx->sample_fmt,
|
|
|
|
|
"in_sample_rate", info.sample_rate,
|
|
|
|
|
"out_sample_rate", info.sample_rate,
|
|
|
|
|
"in_channels", info.channels,
|
|
|
|
|
"out_channels", info.channels
|
|
|
|
|
);
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// setup resample context
|
|
|
|
|
if (!avr_planar) {
|
|
|
|
|
avr_planar = SWR_ALLOC();
|
2024-05-08 12:16:41 +02:00
|
|
|
#if HAVE_CH_LAYOUT
|
|
|
|
|
AVChannelLayout layout;
|
|
|
|
|
av_channel_layout_from_mask(&layout, info.channel_layout);
|
|
|
|
|
av_opt_set_chlayout(avr_planar, "in_chlayout", &layout, 0);
|
|
|
|
|
av_opt_set_chlayout(avr_planar, "out_chlayout", &layout, 0);
|
|
|
|
|
#else
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
av_opt_set_int(avr_planar, "in_channel_layout", info.channel_layout, 0);
|
|
|
|
|
av_opt_set_int(avr_planar, "out_channel_layout", info.channel_layout, 0);
|
2024-05-08 12:16:41 +02:00
|
|
|
av_opt_set_int(avr_planar, "in_channels", info.channels, 0);
|
|
|
|
|
av_opt_set_int(avr_planar, "out_channels", info.channels, 0);
|
|
|
|
|
#endif
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06: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_ctx->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);
|
|
|
|
|
SWR_INIT(avr_planar);
|
|
|
|
|
}
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Create input frame (and allocate arrays)
|
|
|
|
|
audio_frame = AV_ALLOCATE_FRAME();
|
|
|
|
|
AV_RESET_FRAME(audio_frame);
|
|
|
|
|
audio_frame->nb_samples = audio_input_position / info.channels;
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Create a new array
|
|
|
|
|
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) )
|
|
|
|
|
);
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Copy audio into buffer for frame
|
|
|
|
|
memcpy(final_samples_planar, samples,
|
|
|
|
|
static_cast<size_t>(audio_frame->nb_samples)
|
|
|
|
|
* info.channels
|
|
|
|
|
* av_get_bytes_per_sample(output_sample_fmt));
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Fill input frame with sample data
|
|
|
|
|
avcodec_fill_audio_frame(audio_frame, info.channels, output_sample_fmt,
|
|
|
|
|
(uint8_t *) final_samples_planar, audio_encoder_buffer_size, 0);
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Create output frame (and allocate arrays)
|
|
|
|
|
frame_final->nb_samples = audio_input_frame_size;
|
2024-05-08 12:16:41 +02:00
|
|
|
#if HAVE_CH_LAYOUT
|
|
|
|
|
av_channel_layout_from_mask(&frame_final->ch_layout, info.channel_layout);
|
|
|
|
|
#else
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
frame_final->channels = info.channels;
|
|
|
|
|
frame_final->channel_layout = info.channel_layout;
|
2024-05-08 12:16:41 +02:00
|
|
|
#endif
|
|
|
|
|
frame_final->format = audio_codec_ctx->sample_fmt;
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
av_samples_alloc(frame_final->data, frame_final->linesize, info.channels,
|
|
|
|
|
frame_final->nb_samples, audio_codec_ctx->sample_fmt, 0);
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Convert audio samples
|
|
|
|
|
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
|
|
|
|
|
);
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Copy audio samples over original samples
|
|
|
|
|
const auto copy_length = static_cast<size_t>(nb_samples)
|
|
|
|
|
* av_get_bytes_per_sample(audio_codec_ctx->sample_fmt)
|
|
|
|
|
* info.channels;
|
2021-09-27 20:36:56 -04:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
if (nb_samples > 0)
|
|
|
|
|
memcpy(samples, frame_final->data[0], copy_length);
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// deallocate AVFrame
|
|
|
|
|
av_freep(&(audio_frame->data[0]));
|
|
|
|
|
AV_FREE_FRAME(&audio_frame);
|
|
|
|
|
all_queued_samples = NULL; // this array cleared with above call
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"FFmpegWriter::write_audio_packets (Successfully completed 2nd resampling for Planar formats)",
|
|
|
|
|
"nb_samples", nb_samples);
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
} else {
|
|
|
|
|
// Create a new array
|
|
|
|
|
const auto buf_size = static_cast<size_t>(audio_input_position)
|
|
|
|
|
* (av_get_bytes_per_sample(audio_codec_ctx->sample_fmt) /
|
|
|
|
|
av_get_bytes_per_sample(AV_SAMPLE_FMT_S16)
|
|
|
|
|
);
|
|
|
|
|
final_samples = reinterpret_cast<int16_t*>(
|
|
|
|
|
av_malloc(sizeof(int16_t) * buf_size));
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Copy audio into buffer for frame
|
|
|
|
|
memcpy(final_samples, samples,
|
|
|
|
|
audio_input_position * av_get_bytes_per_sample(audio_codec_ctx->sample_fmt));
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Init the nb_samples property
|
|
|
|
|
frame_final->nb_samples = audio_input_frame_size;
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Fill the final_frame AVFrame with audio (non planar)
|
2024-05-08 12:16:41 +02:00
|
|
|
#if HAVE_CH_LAYOUT
|
|
|
|
|
int nb_channels = audio_codec_ctx->ch_layout.nb_channels;
|
|
|
|
|
#else
|
|
|
|
|
int nb_channels = audio_codec_ctx->channels;
|
|
|
|
|
#endif
|
|
|
|
|
avcodec_fill_audio_frame(frame_final, nb_channels,
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
audio_codec_ctx->sample_fmt, (uint8_t *) final_samples,
|
|
|
|
|
audio_encoder_buffer_size, 0);
|
|
|
|
|
}
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Set the AVFrame's PTS
|
|
|
|
|
frame_final->pts = audio_timestamp;
|
2021-06-26 15:48:16 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Init the packet
|
2022-03-16 23:58:06 -04:00
|
|
|
#if IS_FFMPEG_3_2
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
AVPacket* pkt = av_packet_alloc();
|
2022-03-16 23:58:06 -04:00
|
|
|
#else
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
AVPacket* pkt;
|
|
|
|
|
av_init_packet(pkt);
|
2022-03-16 23:58:06 -04:00
|
|
|
#endif
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
pkt->data = audio_encoder_buffer;
|
|
|
|
|
pkt->size = audio_encoder_buffer_size;
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Set the packet's PTS prior to encoding
|
|
|
|
|
pkt->pts = pkt->dts = audio_timestamp;
|
2021-02-17 19:44:44 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06: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
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06: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_ctx, frame_final);
|
|
|
|
|
if (ret < 0 && ret != AVERROR(EINVAL) && ret != AVERROR_EOF) {
|
|
|
|
|
avcodec_send_frame(audio_codec_ctx, NULL);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (ret >= 0)
|
|
|
|
|
pkt->size = 0;
|
|
|
|
|
ret = avcodec_receive_packet(audio_codec_ctx, pkt);
|
|
|
|
|
if (ret >= 0)
|
|
|
|
|
frame_finished = 1;
|
|
|
|
|
if(ret == AVERROR(EINVAL) || ret == AVERROR_EOF) {
|
|
|
|
|
avcodec_flush_buffers(audio_codec_ctx);
|
|
|
|
|
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
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Encode audio (older versions of FFmpeg)
|
|
|
|
|
int error_code = avcodec_encode_audio2(audio_codec_ctx, pkt, frame_final, &got_packet_ptr);
|
2019-04-18 14:04:37 -05:00
|
|
|
#endif
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06: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
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06: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 = audio_timestamp;
|
2012-10-27 21:36:08 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Scale the PTS to the audio stream timebase (which is sometimes different than the codec's timebase)
|
|
|
|
|
av_packet_rescale_ts(pkt, audio_codec_ctx->time_base, audio_st->time_base);
|
2012-10-27 21:36:08 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// set stream
|
|
|
|
|
pkt->stream_index = audio_st->index;
|
|
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2012-10-27 21:36:08 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
/* write the compressed frame in the media file */
|
|
|
|
|
error_code = av_interleaved_write_frame(oc, pkt);
|
|
|
|
|
}
|
2012-08-20 02:59:35 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
if (error_code < 0) {
|
|
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"FFmpegWriter::write_audio_packets ERROR ["
|
|
|
|
|
+ av_err2string(error_code) + "]",
|
|
|
|
|
"error_code", error_code);
|
|
|
|
|
}
|
2012-10-27 21:36:08 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Increment PTS (no pkt.duration, so calculate with maths)
|
|
|
|
|
audio_timestamp += FFMIN(audio_input_frame_size, audio_input_position);
|
2021-06-25 18:54:27 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// deallocate AVFrame
|
|
|
|
|
av_freep(&(frame_final->data[0]));
|
|
|
|
|
AV_FREE_FRAME(&frame_final);
|
2012-10-27 21:36:08 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// deallocate memory for packet
|
|
|
|
|
AV_FREE_PACKET(pkt);
|
2012-08-20 02:59:35 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Reset position
|
|
|
|
|
audio_input_position = 0;
|
|
|
|
|
is_final = false;
|
|
|
|
|
}
|
2012-08-20 02:59:35 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Delete arrays (if needed)
|
|
|
|
|
if (all_resampled_samples) {
|
|
|
|
|
av_freep(&all_resampled_samples);
|
|
|
|
|
all_resampled_samples = NULL;
|
|
|
|
|
}
|
|
|
|
|
if (all_queued_samples) {
|
|
|
|
|
av_freep(&all_queued_samples);
|
|
|
|
|
all_queued_samples = NULL;
|
|
|
|
|
}
|
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) {
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Determine the height & width of the source image
|
|
|
|
|
int source_image_width = frame->GetWidth();
|
|
|
|
|
int source_image_height = frame->GetHeight();
|
2012-12-03 13:03:04 -06:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -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
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06: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
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06: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;
|
2012-08-24 15:57:49 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Allocate an RGB frame & final output frame
|
|
|
|
|
int bytes_source = 0;
|
|
|
|
|
int bytes_final = 0;
|
|
|
|
|
AVFrame *frame_source = NULL;
|
|
|
|
|
const uchar *pixels = NULL;
|
2012-08-22 17:31:12 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Get a list of pixels from source image
|
|
|
|
|
pixels = frame->GetPixels();
|
2012-08-22 17:31:12 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Init AVFrame for source image & final (converted image)
|
|
|
|
|
frame_source = allocate_avframe(PIX_FMT_RGBA, source_image_width, source_image_height, &bytes_source, (uint8_t *) pixels);
|
2019-04-18 14:04:37 -05:00
|
|
|
#if IS_FFMPEG_3_2
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
AVFrame *frame_final;
|
2021-06-04 21:32:29 -04:00
|
|
|
#if USE_HW_ACCEL
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
if (hw_en_on && hw_en_supported) {
|
|
|
|
|
frame_final = allocate_avframe(AV_PIX_FMT_NV12, info.width, info.height, &bytes_final, NULL);
|
|
|
|
|
} else
|
2021-06-04 21:32:29 -04:00
|
|
|
#endif // USE_HW_ACCEL
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06: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
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
AVFrame *frame_final = allocate_avframe(video_codec_ctx->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
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Fill with data
|
|
|
|
|
AV_COPY_PICTURE_DATA(frame_source, (uint8_t *) pixels, PIX_FMT_RGBA, source_image_width, source_image_height);
|
|
|
|
|
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
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Resize & convert pixel format
|
|
|
|
|
sws_scale(scaler, frame_source->data, frame_source->linesize, 0,
|
|
|
|
|
source_image_height, frame_final->data, frame_final->linesize);
|
2012-08-20 00:02:09 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Add resized AVFrame to av_frames map
|
|
|
|
|
add_avframe(frame, frame_final);
|
2012-08-20 00:02:09 -05:00
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// Deallocate memory
|
|
|
|
|
AV_FREE_FRAME(&frame_source);
|
2012-08-20 00:02:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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)
|
2020-03-04 23:58:16 -05:00
|
|
|
// FFmpeg 4.0+
|
2022-01-12 01:08:31 -05: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
|
2022-01-12 01:08:31 -05:00
|
|
|
// TODO: Should we have moved away from oc->oformat->flags / AVFMT_RAWPICTURE
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
// on ffmpeg < 4.0 as well?
|
|
|
|
|
// Does AV_CODEC_ID_RAWVIDEO not work in ffmpeg 3.x?
|
2022-01-12 01:08:31 -05:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"FFmpegWriter::write_video_packet",
|
2020-08-08 03:38:30 -04:00
|
|
|
"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.
|
2022-03-16 23:58:06 -04:00
|
|
|
#if IS_FFMPEG_3_2
|
|
|
|
|
AVPacket* pkt = av_packet_alloc();
|
|
|
|
|
#else
|
2022-02-24 10:34:41 -05:00
|
|
|
AVPacket* pkt;
|
2022-03-16 23:58:06 -04:00
|
|
|
av_init_packet(pkt);
|
|
|
|
|
#endif
|
|
|
|
|
|
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description).
* Add empty constructor for Profile class, and new Profile unit tets
* Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09
* Clear setfill flag after creating Key() output
* Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier.
* - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track)
- Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them)
* Fix some whitespace issues
* Fix inline documentation mistype
* Fixed missing reuse licensing on new example profile files
* Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that
* - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts
- Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually
* - Fixing whitespace issues
- Code clean-up / line wrapping / etc...
2023-02-02 16:29:38 -06:00
|
|
|
av_packet_from_data(
|
2022-03-13 20:00:43 -04:00
|
|
|
pkt, frame_final->data[0],
|
|
|
|
|
frame_final->linesize[0] * frame_final->height);
|
2012-08-03 03:23:10 -05:00
|
|
|
|
2022-02-24 10:34:41 -05:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
|
|
|
|
pkt->stream_index = video_st->index;
|
2012-08-03 03:23:10 -05:00
|
|
|
|
2021-06-25 15:57:52 -05:00
|
|
|
// Set PTS (in frames and scaled to the codec's timebase)
|
2022-02-24 10:34:41 -05:00
|
|
|
pkt->pts = video_timestamp;
|
2012-10-27 21:36:08 -05:00
|
|
|
|
2012-08-04 01:11:12 -05:00
|
|
|
/* write the compressed frame in the media file */
|
2022-02-24 10:34:41 -05:00
|
|
|
int error_code = av_interleaved_write_frame(oc, pkt);
|
2019-04-18 14:04:37 -05:00
|
|
|
if (error_code < 0) {
|
2022-01-12 01:08:31 -05:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"FFmpegWriter::write_video_packet ERROR ["
|
|
|
|
|
+ av_err2string(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
|
2022-02-24 10:34:41 -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
|
|
|
|
2022-03-16 23:58:06 -04:00
|
|
|
#if IS_FFMPEG_3_2
|
|
|
|
|
AVPacket* pkt = av_packet_alloc();
|
|
|
|
|
#else
|
|
|
|
|
AVPacket* pkt;
|
|
|
|
|
av_init_packet(pkt);
|
|
|
|
|
#endif
|
|
|
|
|
pkt->data = NULL;
|
|
|
|
|
pkt->size = 0;
|
|
|
|
|
pkt->pts = pkt->dts = AV_NOPTS_VALUE;
|
2012-10-24 11:10:35 -05:00
|
|
|
|
2012-10-26 17:15:17 -05:00
|
|
|
// Assign the initial AVFrame PTS from the frame counter
|
2021-06-26 17:24:15 -05:00
|
|
|
frame_final->pts = video_timestamp;
|
2021-06-04 21:32:29 -04:00
|
|
|
#if USE_HW_ACCEL
|
2019-01-29 12:38:52 -08:00
|
|
|
if (hw_en_on && hw_en_supported) {
|
|
|
|
|
if (!(hw_frame = av_frame_alloc())) {
|
2020-03-04 23:52:42 -05:00
|
|
|
std::clog << "Error code: av_hwframe_alloc\n";
|
2019-01-29 12:38:52 -08:00
|
|
|
}
|
2020-03-22 12:08:40 -04:00
|
|
|
if (av_hwframe_get_buffer(video_codec_ctx->hw_frames_ctx, hw_frame, 0) < 0) {
|
2020-03-04 23:52:42 -05:00
|
|
|
std::clog << "Error code: av_hwframe_get_buffer\n";
|
2019-01-29 12:38:52 -08:00
|
|
|
}
|
|
|
|
|
if (!hw_frame->hw_frames_ctx) {
|
2020-03-04 23:52:42 -05:00
|
|
|
std::clog << "Error hw_frames_ctx.\n";
|
2019-01-29 12:38:52 -08:00
|
|
|
}
|
|
|
|
|
hw_frame->format = AV_PIX_FMT_NV12;
|
|
|
|
|
if ( av_hwframe_transfer_data(hw_frame, frame_final, 0) < 0) {
|
2020-03-04 23:52:42 -05:00
|
|
|
std::clog << "Error while transferring frame data to surface.\n";
|
2019-01-29 12:38:52 -08:00
|
|
|
}
|
|
|
|
|
av_frame_copy_props(hw_frame, frame_final);
|
|
|
|
|
}
|
2021-06-04 21:32:29 -04:00
|
|
|
#endif // USE_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
|
2022-03-16 23:58:06 -04:00
|
|
|
// Write video packet
|
2019-01-29 12:38:52 -08:00
|
|
|
int ret;
|
2019-04-18 14:04:37 -05:00
|
|
|
|
2021-06-04 21:32:29 -04:00
|
|
|
#if USE_HW_ACCEL
|
2019-01-29 12:38:52 -08:00
|
|
|
if (hw_en_on && hw_en_supported) {
|
2022-03-16 23:58:06 -04:00
|
|
|
ret = avcodec_send_frame(video_codec_ctx, hw_frame); //hw_frame!!!
|
2020-02-10 01:50:31 -05:00
|
|
|
} else
|
2021-06-04 21:32:29 -04:00
|
|
|
#endif // USE_HW_ACCEL
|
2020-02-10 01:50:31 -05:00
|
|
|
{
|
2020-03-22 12:08:40 -04:00
|
|
|
ret = avcodec_send_frame(video_codec_ctx, frame_final);
|
2019-04-18 14:04:37 -05:00
|
|
|
}
|
2018-03-28 15:09:55 -05:00
|
|
|
error_code = ret;
|
|
|
|
|
if (ret < 0 ) {
|
2022-01-12 01:08:31 -05:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"FFmpegWriter::write_video_packet (Frame not sent)");
|
2018-09-18 15:31:34 -07:00
|
|
|
if (ret == AVERROR(EAGAIN) ) {
|
2020-08-08 03:38:30 -04:00
|
|
|
std::clog << "Frame EAGAIN\n";
|
2019-01-29 12:38:52 -08:00
|
|
|
}
|
2018-09-18 15:31:34 -07:00
|
|
|
if (ret == AVERROR_EOF ) {
|
2020-08-08 03:38:30 -04:00
|
|
|
std::clog << "Frame AVERROR_EOF\n";
|
2019-01-29 12:38:52 -08:00
|
|
|
}
|
2020-03-22 12:08:40 -04:00
|
|
|
avcodec_send_frame(video_codec_ctx, NULL);
|
2018-03-28 15:09:55 -05:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
while (ret >= 0) {
|
2022-03-16 23:58:06 -04:00
|
|
|
ret = avcodec_receive_packet(video_codec_ctx, pkt);
|
2019-04-18 14:04:37 -05:00
|
|
|
|
|
|
|
|
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
|
2020-03-22 12:08:40 -04:00
|
|
|
avcodec_flush_buffers(video_codec_ctx);
|
2018-03-28 15:09:55 -05:00
|
|
|
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
|
|
|
|
|
// Write video packet (older than FFmpeg 3.2)
|
2022-03-16 23:58:06 -04:00
|
|
|
error_code = avcodec_encode_video2(video_codec_ctx, pkt, frame_final, &got_packet_ptr);
|
2019-04-18 14:04:37 -05:00
|
|
|
if (error_code != 0) {
|
2022-01-12 01:08:31 -05:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"FFmpegWriter::write_video_packet ERROR ["
|
|
|
|
|
+ av_err2string(error_code) + "]",
|
|
|
|
|
"error_code", error_code);
|
2019-04-18 14:04:37 -05:00
|
|
|
}
|
|
|
|
|
if (got_packet_ptr == 0) {
|
2022-01-12 01:08:31 -05:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"FFmpegWriter::write_video_packet (Frame gotpacket error)");
|
2019-04-18 14:04:37 -05:00
|
|
|
}
|
2020-02-10 01:50:31 -05:00
|
|
|
#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) {
|
|
|
|
|
// set the timestamp
|
2022-03-16 23:58:06 -04:00
|
|
|
av_packet_rescale_ts(pkt, video_codec_ctx->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 */
|
2022-03-16 23:58:06 -04:00
|
|
|
int result = av_interleaved_write_frame(oc, pkt);
|
2020-03-04 23:52:42 -05:00
|
|
|
if (result < 0) {
|
2022-01-12 01:08:31 -05:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"FFmpegWriter::write_video_packet ERROR ["
|
|
|
|
|
+ av_err2string(result) + "]",
|
|
|
|
|
"result", result);
|
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
|
|
|
|
|
|
|
|
// Deallocate packet
|
2022-03-16 23:58:06 -04:00
|
|
|
AV_FREE_PACKET(pkt);
|
2021-06-04 21:32:29 -04:00
|
|
|
#if USE_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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-04 21:32:29 -04:00
|
|
|
#endif // USE_HW_ACCEL
|
2012-08-03 03:23:10 -05:00
|
|
|
}
|
2016-02-02 01:09:02 -06:00
|
|
|
|
2022-03-16 23:58:06 -04:00
|
|
|
// Increment PTS (in frames and scaled to the codec's timebase)
|
|
|
|
|
video_timestamp += av_rescale_q(1, av_make_q(info.fps.den, info.fps.num), video_codec_ctx->time_base);
|
2021-07-03 17:01:55 -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
|
2021-06-04 21:32:29 -04:00
|
|
|
#if USE_HW_ACCEL
|
2019-01-29 12:38:52 -08:00
|
|
|
if (hw_en_on && hw_en_supported) {
|
2020-03-04 23:52:42 -05: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
|
2021-06-04 21:32:29 -04:00
|
|
|
#endif // USE_HW_ACCEL
|
2019-01-29 12:38:52 -08:00
|
|
|
{
|
2020-03-04 23:52:42 -05: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, 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();
|
|
|
|
|
}
|